From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33B91D0C606 for ; Fri, 25 Oct 2024 12:50:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B7966B0082; Fri, 25 Oct 2024 08:50:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53F886B0083; Fri, 25 Oct 2024 08:50:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B9486B0085; Fri, 25 Oct 2024 08:50:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1A06C6B0082 for ; Fri, 25 Oct 2024 08:50:27 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C3D52C0866 for ; Fri, 25 Oct 2024 12:50:05 +0000 (UTC) X-FDA: 82712107002.09.8F05562 Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) by imf28.hostedemail.com (Postfix) with ESMTP id 3801EC0010 for ; Fri, 25 Oct 2024 12:50:05 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lNnDk8RF; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729860499; a=rsa-sha256; cv=none; b=AjoUCYKssMm17RryBaKSt/29QFJ5CMLmaarXAj4OOdR9YFKgpyB4p1l3FlPb4pfIqs4p/W Q9T9NtV8lEBLKLa8CSKT1VSht8Skh8iuRIbLwj9FEdVUoEp0LX6oZ6Lag80FJJYuwlYDe0 wozK/bsZ/L5K70K4C13IK54yetexUI4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lNnDk8RF; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729860499; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2EmHSEMXHuqWTqs2dMjBSQS45igTAqZ1OnjsJbkbpFc=; b=6IuelmSuBtYMf901noMCUxduwEcvLMhjdOG7NucA0aLndVQQyoJIcLMGvzrMk1ihjV3R/V xDC5w9LNJ4kOOsJnb3yKaYMMfUwbvENniC5NrC0s9OvQ0MDOReoKWVq2hxIRFXNWod1bDs fXTVbgqLe9x/TaZ6l2cNq7y5ILvjkzQ= Received: by mail-ua1-f51.google.com with SMTP id a1e0cc1a2514c-84fd50f2117so676600241.1 for ; Fri, 25 Oct 2024 05:50:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729860624; x=1730465424; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2EmHSEMXHuqWTqs2dMjBSQS45igTAqZ1OnjsJbkbpFc=; b=lNnDk8RFmjxxznuwKycYLU/K8dl6WgSQlM1I/S2ZNcLy/6Xi3rEtRBktcYGLZ7v8qA 9ONjoenYaotv3+7Vk+JxUP3vDgCwVgVonzs6S2h2epIxCknLW5pdyydmFKHTyIrvORyl jeFug5zx9y0zMo05BSc09PO/bxlwnsRBM6j6PUZTmNofWUGwW4gzPuB2T+H91hLLdQSp W/zSb+G0U7rqkJxixZCDC1pDDDCOB3wUCmWgjoPKW8d2WVySHJ+Nz33ja2BW6lxkH978 pSflqJSrXtDY+Z4AuLqV7CTl9HhL3vPB5JDv9VZr9ku7dsFbRer1Mq/N2wfmdjaYCcQl GYNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729860624; x=1730465424; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2EmHSEMXHuqWTqs2dMjBSQS45igTAqZ1OnjsJbkbpFc=; b=I1a2K9aDG+0xMw9FRmaChTTtuxl9n60fGDJtLptdf5saE2JiQPuFY0XixzmsFcfe1U ewzBQzaODBOlJr/42nUjeDcWV0uNJsOpAYaqWqTNi9AmedmWvwDkzpBX+KICBZlYkdAs 0oMkRXWpVxRv+0vedeHxDw5FLA5Kq1BSq+Y8vVrP1RIfL0erjerl8w6StFqr5BgDKDEW lFDz0SYt9IEe8HqOaDNfYdu51zU/jF5yg4AJBmChXGbcJnozq1Gpv0gPundgoQEqXA/O PAKKUfQnpHrt+YiFl2DpYe02NeqNbpD5l7O4r/b50bMc5ZRkjO5Nx1be5qVA7YtpUGBX jLaA== X-Forwarded-Encrypted: i=1; AJvYcCVqUzdCgamKrlUFh5Ezjg/tNMTVU+rsQmGP6ETGOOeepV7K1bsJnveV5Y2VkUQY+ESyzmBkLxvQYQ==@kvack.org X-Gm-Message-State: AOJu0YwuKTHJ8928LFhzBdO356CqGm9eVgYdDj5LUU/4B+EK4VEEGoE/ xbZJyfB8vKc6RBOdpkqHOu80HpjOk4GsBo1Vmc9UVi5dbwONj/EvTnEON4qJzmyCOEwBHIQe/hI nRjoo+buitYVPNP6KwyvtXyDp02TPCqg0 X-Google-Smtp-Source: AGHT+IEbGP+hlLzUqhS0RCLyGPfc8jr7OmiybYsOLWuFXt/b9D6XHhcoNY++XU11x3iEj0RjmGWAgyNjztqN5HcHO+I= X-Received: by 2002:a05:6102:945:b0:4a3:c516:3173 with SMTP id ada2fe7eead31-4a871032c42mr7398496137.12.1729860623491; Fri, 25 Oct 2024 05:50:23 -0700 (PDT) MIME-Version: 1.0 References: <788fdfcc9ef602b408951d68097918d6ae379395.1729848252.git.lorenzo.stoakes@oracle.com> In-Reply-To: <788fdfcc9ef602b408951d68097918d6ae379395.1729848252.git.lorenzo.stoakes@oracle.com> From: Pedro Falcato Date: Fri, 25 Oct 2024 13:50:12 +0100 Message-ID: Subject: Re: [PATCH v5 2/5] pidfd: add PIDFD_SELF_* sentinels to refer to own thread/process To: Lorenzo Stoakes Cc: Christian Brauner , Shuah Khan , "Liam R . Howlett" , Suren Baghdasaryan , Vlastimil Babka , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Sang , John Hubbard Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3801EC0010 X-Stat-Signature: ezeoik6bhnn3thbkxicfpfbsg5qmwzoe X-Rspam-User: X-HE-Tag: 1729860605-681973 X-HE-Meta: U2FsdGVkX19QNAE8fmFLW/RmVwDX8+renJEqlzm4EYdEj9xBJpIWAyEVA5NWU0+79zxj9mUhr28dKu6sHghP1Kb+pQMamG7zu7y0ESkGdVYwLuSXjOa/ZNGwLF4EvzqYhmSJo5JkiTfLDCwq6mo0k5GsdsLyq9EsaLYppBdqkOlEdLtg7nONuw1dVEL3uApF0JCZM02k3Z8LzuaKstmflSIw0e+uWzgImhmbgStZbmr/dEomVcAkK7qL5YPJB8jfIF0MWb8ySp7+60UQH94tsWOrgmd+p9M9Mz0a3C7iboor+lmaKOQrLeC4z8GECzABApN0TsbJM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fPIQDPW7m9kyRGaVte5gec= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Oct 25, 2024 at 10:41=E2=80=AFAM Lorenzo Stoakes wrote: > > It is useful to be able to utilise the pidfd mechanism to reference the > current thread or process (from a userland point of view - thread group > leader from the kernel's point of view). > > Therefore introduce PIDFD_SELF_THREAD to refer to the current thread, and > PIDFD_SELF_THREAD_GROUP to refer to the current thread group leader. > > For convenience and to avoid confusion from userland's perspective we ali= as > these: > > * PIDFD_SELF is an alias for PIDFD_SELF_THREAD - This is nearly always wh= at > the user will want to use, as they would find it surprising if for > instance fd's were unshared()'d and they wanted to invoke pidfd_getfd() > and that failed. > > * PIDFD_SELF_PROCESS is an alias for PIDFD_SELF_THREAD_GROUP - Most users > have no concept of thread groups or what a thread group leader is, and > from userland's perspective and nomenclature this is what userland > considers to be a process. > > Due to the refactoring of the central __pidfd_get_pid() function we can > implement this functionality centrally, providing the use of this sentine= l > in most functionality which utilises pidfd's. > > We need to explicitly adjust kernel_waitid_prepare() to permit this (thou= gh > it wouldn't really make sense to use this there, we provide the ability f= or > consistency). > > We explicitly disallow use of this in setns(), which would otherwise have > required explicit custom handling, as it doesn't make sense to set the > current calling thread to join the namespace of itself. > > As the callers of pidfd_get_pid() expect an increased reference count on > the pid we do so in the self case, reducing churn and avoiding any breaka= ge > from existing logic which decrements this reference count. > > This change implicitly provides PIDFD_SELF_* support in the waitid(P_PIDF= S, > ...), process_madvise(), process_mrelease(), pidfd_send_signal(), and > pidfd_getfd() system calls. > > Things such as polling a pidfs and general fd operations are not supporte= d, > this strictly provides the sentinel for APIs which explicitly accept a > pidfd. > > Reviewed-by: Shakeel Butt > Signed-off-by: Lorenzo Stoakes > --- > include/linux/pid.h | 8 ++++-- > include/uapi/linux/pidfd.h | 15 +++++++++++ > kernel/exit.c | 3 ++- > kernel/nsproxy.c | 1 + > kernel/pid.c | 51 ++++++++++++++++++++++++-------------- > 5 files changed, 57 insertions(+), 21 deletions(-) > > diff --git a/include/linux/pid.h b/include/linux/pid.h > index d466890e1b35..3b2ac7567a88 100644 > --- a/include/linux/pid.h > +++ b/include/linux/pid.h > @@ -78,11 +78,15 @@ struct file; > * __pidfd_get_pid() - Retrieve a pid associated with the specified pidf= d. > * > * @pidfd: The pidfd whose pid we want, or the fd of a /proc/ = file if > - * @alloc_proc is also set. > + * @alloc_proc is also set, or PIDFD_SELF_* to refer to the= current > + * thread or thread group leader. > * @allow_proc: If set, then an fd of a /proc/ file can be passed i= nstead > * of a pidfd, and this will be used to determine the pid. > + > * @flags: Output variable, if non-NULL, then the file->f_flags of = the > - * pidfd will be set here. > + * pidfd will be set here or If PIDFD_SELF_THREAD is set, t= his is > + * set to PIDFD_THREAD, otherwise if PIDFD_SELF_THREAD_GROU= P then > + * this is set to zero. > * > * Returns: If successful, the pid associated with the pidfd, otherwise = an > * error. > diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h > index 565fc0629fff..0ca2ebf906fd 100644 > --- a/include/uapi/linux/pidfd.h > +++ b/include/uapi/linux/pidfd.h > @@ -29,4 +29,19 @@ > #define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9) > #define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10) > > +/* > + * Special sentinel values which can be used to refer to the current thr= ead or > + * thread group leader (which from a userland perspective is the process= ). > + */ > +#define PIDFD_SELF PIDFD_SELF_THREAD > +#define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP > + > +#define PIDFD_SELF_THREAD -100 /* Current thread. */ This conflicts with AT_FDCWD, might be worth changing? > +#define PIDFD_SELF_THREAD_GROUP -200 /* Current thread group lead= er. */ We might want to pick some range outside of the negative errno space (-4096 IIRC), since we have plenty of values to pick from (2^31 at least). > +static inline int pidfd_is_self_sentinel(pid_t pid) > +{ > + return pid =3D=3D PIDFD_SELF_THREAD || pid =3D=3D PIDFD_SELF_THRE= AD_GROUP; > +} Do we want this in the uapi header? Even if this is useful, it might come with several drawbacks such as breaking scripts that parse kernel headers (and a quick git grep suggests we do have static inlines in headers, but in rather obscure ones) and breaking C89: :8:8: error: unknown type name 'inline' 8 | static inline int pidfd_is_self_sentinel(pid_t pid) :) > + > #endif /* _UAPI_LINUX_PIDFD_H */ > diff --git a/kernel/exit.c b/kernel/exit.c > index 619f0014c33b..3eb20f8252ee 100644 > --- a/kernel/exit.c > +++ b/kernel/exit.c > @@ -71,6 +71,7 @@ > #include > #include > > +#include > #include > > #include > @@ -1739,7 +1740,7 @@ int kernel_waitid_prepare(struct wait_opts *wo, int= which, pid_t upid, > break; > case P_PIDFD: > type =3D PIDTYPE_PID; > - if (upid < 0) > + if (upid < 0 && !pidfd_is_self_sentinel(upid)) > return -EINVAL; > > pid =3D pidfd_get_pid(upid, &f_flags); > diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c > index dc952c3b05af..d239f7eeaa1f 100644 > --- a/kernel/nsproxy.c > +++ b/kernel/nsproxy.c > @@ -550,6 +550,7 @@ SYSCALL_DEFINE2(setns, int, fd, int, flags) > struct nsset nsset =3D {}; > int err =3D 0; > > + /* If fd is PIDFD_SELF_*, implicitly fail here, as invalid. */ > if (!fd_file(f)) > return -EBADF; > > diff --git a/kernel/pid.c b/kernel/pid.c > index 94c97559e5c5..8742157b36f8 100644 > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -535,33 +535,48 @@ struct pid *find_ge_pid(int nr, struct pid_namespac= e *ns) > } > EXPORT_SYMBOL_GPL(find_ge_pid); > > +static struct pid *pidfd_get_pid_self(unsigned int pidfd, unsigned int *= flags) > +{ > + bool is_thread =3D pidfd =3D=3D PIDFD_SELF_THREAD; > + enum pid_type type =3D is_thread ? PIDTYPE_PID : PIDTYPE_TGID; > + struct pid *pid =3D *task_pid_ptr(current, type); > + > + /* The caller expects an elevated reference count. */ > + get_pid(pid); It would be really really nice to avoid the get here, but I imagine it'll take some refactoring around put_pid's? > + return pid; > +} > + > struct pid *__pidfd_get_pid(unsigned int pidfd, bool allow_proc, > unsigned int *flags) > { > - struct pid *pid; > - struct fd f =3D fdget(pidfd); > - struct file *file =3D fd_file(f); > + if (pidfd_is_self_sentinel(pidfd)) { > + return pidfd_get_pid_self(pidfd, flags); > + } else { Skipping the else here might make the rest of the code more legible (since the sentinel branch returns anyway...). --=20 Pedro