Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5733237ybp; Tue, 15 Oct 2019 04:20:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqw+mvply7sfgjTcWZBUwwBHo6+RB5yFW41yGF3rcR/3MAa1ZZeBenj1M1/QjgXRerz6MYbj X-Received: by 2002:a17:906:6d84:: with SMTP id h4mr26112468ejt.262.1571138406716; Tue, 15 Oct 2019 04:20:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571138406; cv=none; d=google.com; s=arc-20160816; b=jlKFXCLPoD+N0SCxwFCkyLiTILRnxfYsgD3IjnYZCbZthWlOVvQiShlXSI7irdkl5q H8Uv4KWTuuOnuLBGrE8apTNfJKIZCTZZBYui93RlHw1lGrsngv8O2ReWmS40QLiCA0/c RkH4OvyfCnN2KFyG7ZifQBNjb7S1hZ9hJqhS7LopXWaPr+DkLL6TfisxKGkFSfEY16tQ icjWzNCCzXG9Ha4KgIZ2MPDofnXonCKjDHN25RwslwKvZz9gcdCurT8vsfDn6T7n7sXi f0bpn+qKseH9Lcs1wSNUiN4my8tp6dQ1845SWbp7mWt8btKhqYSeHqMaw9yn6LSWI7VO zjsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=wpysk8IZ4ZgWYpw3HtR5q3Wh3fTDmu+hK5fcv2MSkAg=; b=yo4gBGpTJd3H2WueQzMc0IJ9lfbPfUytDfg73E+KwCK7rnxUW934UbRRHMPMTcDa4A 9pojzNqgzqz/hrN1NFMRmI21QgwkTd3tjn8n/hPD868wHSCr1gGPOjd9L6NAkDwFAlHZ aw+OHMjupS+n7T+KWYJE9/A311a5sjSYj8ioF8GrDV1CjuXh1/1q40HN4uC5Y/MuQPZY /rxxb2irc0sMjU7BEZzsGWcnQn9G5OfBPa14f2txFyV0LN+iAjOQYqPmTd3UYeRe5K9u AxZdPOgRKUasHhavHY4fwycBFN6bqesUmmuHo/D6FK+/maJ/CyeOQ0Fuyy8QD6Fj6N7V rZeA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 52si14569344edz.413.2019.10.15.04.19.43; Tue, 15 Oct 2019 04:20:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729972AbfJOJks (ORCPT + 99 others); Tue, 15 Oct 2019 05:40:48 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:33342 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725890AbfJOJks (ORCPT ); Tue, 15 Oct 2019 05:40:48 -0400 Received: from [185.81.136.22] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iKJJr-0003Ir-Nx; Tue, 15 Oct 2019 09:40:43 +0000 Date: Tue, 15 Oct 2019 11:40:41 +0200 From: Christian Brauner To: Christian Kellner Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Jann Horn , Christian Kellner , Christian Brauner , Andrew Morton , "Peter Zijlstra (Intel)" , Ingo Molnar , Michal Hocko , Thomas Gleixner , Elena Reshetova , Roman Gushchin , Andrea Arcangeli , Al Viro , Aleksa Sarai , "Dmitry V. Levin" Subject: Re: [PATCH v4 1/2] pidfd: add NSpid entries to fdinfo Message-ID: <20191015094039.gtc637trfxmnjzw7@wittgenstein> References: <20191011122323.7770-1-ckellner@redhat.com> <20191014162034.2185-1-ckellner@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191014162034.2185-1-ckellner@redhat.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 14, 2019 at 06:20:32PM +0200, Christian Kellner wrote: > From: Christian Kellner > > Currently, the fdinfo file contains the Pid field which shows the > pid a given pidfd refers to in the pid namespace of the procfs > instance. If pid namespaces are configured, also show an NSpid field > for easy retrieval of the pid in all descendant pid namespaces. If > the pid namespace of the process is not a descendant of the pid > namespace of the procfs instance 0 will be shown as its first NSpid > entry and no other entries will be shown. Add a block comment to > pidfd_show_fdinfo with a detailed explanation of Pid and NSpid fields. > > Co-developed-by: Christian Brauner > Signed-off-by: Christian Brauner > Signed-off-by: Christian Kellner Thanks! Reviewed-by: Christian Brauner > --- > Changes in v4: > - Reworked to properly handle the case where the pidfd is from a > different branch in the pid namespace hierarchy; also add block > comment with an in-depth explanation (Christian Brauner) > > kernel/fork.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 52 insertions(+), 1 deletion(-) > > diff --git a/kernel/fork.c b/kernel/fork.c > index bcdf53125210..782986962d47 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -1695,12 +1695,63 @@ static int pidfd_release(struct inode *inode, struct file *file) > } > > #ifdef CONFIG_PROC_FS > +/** > + * pidfd_show_fdinfo - print information about a pidfd > + * @m: proc fdinfo file > + * @f: file referencing a pidfd > + * > + * Pid: > + * This function will print the pid that a given pidfd refers to in the > + * pid namespace of the procfs instance. > + * If the pid namespace of the process is not a descendant of the pid > + * namespace of the procfs instance 0 will be shown as its pid. This is > + * similar to calling getppid() on a process whose parent is outside of > + * its pid namespace. > + * > + * NSpid: > + * If pid namespaces are supported then this function will also print > + * the pid of a given pidfd refers to for all descendant pid namespaces > + * starting from the current pid namespace of the instance, i.e. the > + * Pid field and the first entry in the NSpid field will be identical. > + * If the pid namespace of the process is not a descendant of the pid > + * namespace of the procfs instance 0 will be shown as its first NSpid > + * entry and no others will be shown. > + * Note that this differs from the Pid and NSpid fields in > + * /proc//status where Pid and NSpid are always shown relative to > + * the pid namespace of the procfs instance. The difference becomes > + * obvious when sending around a pidfd between pid namespaces from a > + * different branch of the tree, i.e. where no ancestoral relation is > + * present between the pid namespaces: > + * - create two new pid namespaces ns1 and ns2 in the initial pid > + * namespace (also take care to create new mount namespaces in the > + * new pid namespace and mount procfs) > + * - create a process with a pidfd in ns1 > + * - send pidfd from ns1 to ns2 > + * - read /proc/self/fdinfo/ and observe that both Pid and NSpid > + * have exactly one entry, which is 0 > + */ > static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) > { > struct pid_namespace *ns = proc_pid_ns(file_inode(m->file)); > struct pid *pid = f->private_data; > + pid_t nr = pid_nr_ns(pid, ns); > + > + seq_put_decimal_ull(m, "Pid:\t", nr); > > - seq_put_decimal_ull(m, "Pid:\t", pid_nr_ns(pid, ns)); > +#ifdef CONFIG_PID_NS > + seq_put_decimal_ull(m, "\nNSpid:\t", nr); > + if (nr) { > + int i; > + > + /* If nr is non-zero it means that 'pid' is valid and that Nit: multiline kernel comment style is usually /* * bla * bla */ but I'll just fix this up when applying. No need to resend. > + * ns, i.e. the pid namespace associated with the procfs > + * instance, is in the pid namespace hierarchy of pid. > + * Start at one below the already printed level. > + */ > + for (i = ns->level + 1; i <= pid->level; i++) > + seq_put_decimal_ull(m, "\t", pid->numbers[i].nr); > + } > +#endif > seq_putc(m, '\n'); > } > #endif > -- > 2.21.0 >