Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp554504pxb; Mon, 7 Feb 2022 18:43:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJyhL0PE7BljoltHa8m8gBI+yTB3QVveGCAvoAjHij6XFAd7liUSuNhMarVJoTK9B2MdOxyy X-Received: by 2002:a17:906:eb0f:: with SMTP id mb15mr1988505ejb.326.1644288213002; Mon, 07 Feb 2022 18:43:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644288212; cv=none; d=google.com; s=arc-20160816; b=K1WyMMrLRyvKMGjNLyjIGa+SCrZ7AlOWrGFO7VCEn3OJ8dRJlXEL0UszKl3wBmMV1Y DNeHXPGA/oOqkdpeMQN9tjQcrUXHO/sxloLHx0JjKM//QspblGg6rbrM/Qw+A9kajsb+ TWwBOAWC+SUWqaHwGDQo+HAcaGSW3dvBE9ajO3OKMJOSSK648RePqQUV4e9Cc2g1iprK hcKE/Mmw8Ufatn2z8u7XOQc2cymgvRTd6c/9kpjDiHpxPCoCJoBWJsy2QiFBU0Jbq1zk gHjqW9RDw9q+2zGpbr6R/INKt/Gw0aEvRN446axuHoyYDY6SOUyq6cqGsfMyAFhmuT+N 5ZhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5buS5PBXwhVJzMNdjUoWCdPcUbZG0fotWpHqJz0cAXc=; b=L2BnOnms0ZL2r5yTLObdmDR1XqproR+kDLEde+uQWoAjh23sqb3ejVB/g7T8XF/eY7 vD8y+x+VUzDvxvw8q/QqeX6Zl6hAHFp7SpPIufohl3Repi/TqNmTsSsn/5CQeJLR8RJR xA4/5R32K4PLgCly7A0+I80xVny9fMDOBPV09sNtk3c/QhXtD5qjKwekVvv+nXr4aul/ qZITtGEZswtYIR0iQ1bfIh9akgEaG1SkxxMnG8FmtDN8IPtyfeA6znbdxjhaWxxgrtvS Gr6dhnxEpc9K2yJzZiJenNbrBEwdmRyjA0/0bmp1dxJyHNaPHopBwZENI9R8jT7aojQA oqUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eeaiR2hW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qf38si9316273ejc.14.2022.02.07.18.43.07; Mon, 07 Feb 2022 18:43:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eeaiR2hW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234614AbiBFLxC (ORCPT + 99 others); Sun, 6 Feb 2022 06:53:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234840AbiBFLqm (ORCPT ); Sun, 6 Feb 2022 06:46:42 -0500 X-Greylist: delayed 597 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sun, 06 Feb 2022 03:46:41 PST Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5E9BC06173B; Sun, 6 Feb 2022 03:46:41 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 932A2B80DDE; Sun, 6 Feb 2022 11:46:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFB01C340E9; Sun, 6 Feb 2022 11:46:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1644147999; bh=08m5dBhezDpkJrsoquueSgsCkzvWmB/UFruhAQbk/fw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eeaiR2hWEVJ0PeG+iZIEdp3ai73UVqPHFe9M/sj9B1PYkMprlP+SofDvzFUloeVrW d4BAQwgx5/w0wnpLLd2fbdObP3IDGwpXY5Zzqo21vEHgXM2gKs94PQR601A/M4lMTG k2vUmV3pP7EUALo985ByyhXGPBKMd/FRTTpldFy04mBww7ok1LTGcFGuWl8jz5mSI/ YPkAFkfV47c50blaRVfZwrNvjE6LGvpGJhVQnUPhP9MH4RXqAODEJip1uUJHjVCFjY AnWonzfi/vxo1Elzkpgx7nesEB/mwHDePWLx1rsgnab77N9j4SvSxxhHJIKYtZdj9J kd0GretrQsuKg== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 0D46A404A6; Sun, 6 Feb 2022 08:46:37 -0300 (-03) Date: Sun, 6 Feb 2022 08:46:37 -0300 From: Arnaldo Carvalho de Melo To: Leo Yan Cc: James Clark , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1] perf synthetic-events: Return error if procfs isn't mounted for PID namespaces Message-ID: References: <20211224124014.2492751-1-leo.yan@linaro.org> <20220204124849.GB2040169@leoy-ThinkPad-X240s> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220204124849.GB2040169@leoy-ThinkPad-X240s> X-Url: http://acmel.wordpress.com X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Fri, Feb 04, 2022 at 08:48:49PM +0800, Leo Yan escreveu: > On Fri, Jan 07, 2022 at 02:27:57PM +0000, James Clark wrote: > > On 24/12/2021 12:40, Leo Yan wrote: > > > For perf recording, it retrieves process info by iterating nodes in proc > > > fs. If we run perf in a non-root PID namespace with command: > > > > > > # unshare --fork --pid perf record -e cycles -a -- test_program > > > > > > ... in this case, unshare command creates a child PID namespace and > > > launches perf tool in it, but the issue is the proc fs is not mounted > > > for the non-root PID namespace, this leads to the perf tool gathering > > > process info from its parent PID namespace. > > > > > > > I had some concerns that this would prevent use of perf in docker, but docker > > does mount /proc so it is still working. And you've added a warning about how > > to fix the unshare command so I think this is ok. > > > > Reviewed-by: James Clark > > Thanks a lot for review, James. > > Arnaldo, Jiri, could you take a look for this patch? Thanks! Looks sane, there was discussion, a detailed set of steps to reproduce the problem was provided, thanks, applied. - Arnaldo > Leo > > > > We can use below command to observe the process nodes under proc fs: > > > > > > # unshare --pid --fork ls /proc > > > 1 137 1968 2128 3 342 48 62 78 crypto kcore net uptime > > > 10 138 2 2142 30 35 49 63 8 devices keys pagetypeinfo version > > > 11 139 20 2143 304 36 50 64 82 device-tree key-users partitions vmallocinfo > > > 12 14 2011 22 305 37 51 65 83 diskstats kmsg self vmstat > > > 128 140 2038 23 307 39 52 656 84 driver kpagecgroup slabinfo zoneinfo > > > 129 15 2074 24 309 4 53 67 9 execdomains kpagecount softirqs > > > 13 16 2094 241 31 40 54 68 asound fb kpageflags stat > > > 130 164 2096 242 310 41 55 69 buddyinfo filesystems loadavg swaps > > > 131 17 2098 25 317 42 56 70 bus fs locks sys > > > 132 175 21 26 32 43 57 71 cgroups interrupts meminfo sysrq-trigger > > > 133 179 2102 263 329 44 58 75 cmdline iomem misc sysvipc > > > 134 1875 2103 27 330 45 59 76 config.gz ioports modules thread-self > > > 135 19 2117 29 333 46 6 77 consoles irq mounts timer_list > > > 136 1941 2121 298 34 47 60 773 cpuinfo kallsyms mtd tty > > > > > > So it shows many existed tasks, since unshared command has not mounted > > > the proc fs for the new created PID namespace, it still accesses the > > > proc fs of the root PID namespace. This leads to two prominent issues: > > > > > > - Firstly, PID values are mismatched between thread info and samples. > > > The gathered thread info are coming from the proc fs of the root PID > > > namespace, but samples record its PID from the child PID namespace. > > > > > > - The second issue is profiled program 'test_program' returns its forked > > > PID number from the child PID namespace, perf tool wrongly uses this > > > PID number to retrieve the process info via the proc fs of the root > > > PID namespace. > > > > > > To avoid issues, we need to mount proc fs for the child PID namespace > > > with the option '--mount-proc' when use unshare command: > > > > > > # unshare --fork --pid --mount-proc perf record -e cycles -a -- test_program > > > > > > Conversely, when the proc fs of the root PID namespace is used by child > > > namespace, perf tool can detect the multiple PID levels and > > > nsinfo__is_in_root_namespace() returns false, this patch reports error > > > for this case: > > > > > > # unshare --fork --pid perf record -e cycles -a -- test_program > > > Couldn't synthesize bpf events. > > > Perf runs in non-root PID namespace but it tries to gather process info from its parent PID namespace. > > > Please mount the proc file system properly, e.g. add the option '--mount-proc' for unshare command. > > > > > > Signed-off-by: Leo Yan > > > --- > > > tools/perf/util/synthetic-events.c | 19 +++++++++++++++++++ > > > 1 file changed, 19 insertions(+) > > > > > > diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c > > > index 198982109f0f..cf7800347f77 100644 > > > --- a/tools/perf/util/synthetic-events.c > > > +++ b/tools/perf/util/synthetic-events.c > > > @@ -1784,6 +1784,25 @@ int __machine__synthesize_threads(struct machine *machine, struct perf_tool *too > > > perf_event__handler_t process, bool needs_mmap, > > > bool data_mmap, unsigned int nr_threads_synthesize) > > > { > > > + /* > > > + * When perf runs in non-root PID namespace, and the namespace's proc FS > > > + * is not mounted, nsinfo__is_in_root_namespace() returns false. > > > + * In this case, the proc FS is coming for the parent namespace, thus > > > + * perf tool will wrongly gather process info from its parent PID > > > + * namespace. > > > + * > > > + * To avoid the confusion that the perf tool runs in a child PID > > > + * namespace but it synthesizes thread info from its parent PID > > > + * namespace, returns failure with warning. > > > + */ > > > + if (!nsinfo__is_in_root_namespace()) { > > > + pr_err("Perf runs in non-root PID namespace but it tries to "); > > > + pr_err("gather process info from its parent PID namespace.\n"); > > > + pr_err("Please mount the proc file system properly, e.g. "); > > > + pr_err("add the option '--mount-proc' for unshare command.\n"); > > > + return -EPERM; > > > + } > > > + > > > if (target__has_task(target)) > > > return perf_event__synthesize_thread_map(tool, threads, process, machine, > > > needs_mmap, data_mmap); > > > -- - Arnaldo