Linux userland API discussions
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Andy Lutomirski <luto@amacapital.net>,
	 "Eric W. Biederman" <ebiederm@xmission.com>,
	Tycho Andersen <tycho@tycho.pizza>,
	linux-api@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] pidfd: change pidfd_send_signal() to respect PIDFD_THREAD
Date: Fri, 9 Feb 2024 16:15:18 +0100	[thread overview]
Message-ID: <20240209-stangen-feuerzeug-17c8662854c9@brauner> (raw)
In-Reply-To: <20240209130650.GA8048@redhat.com>

On Fri, Feb 09, 2024 at 02:06:50PM +0100, Oleg Nesterov wrote:
> Turn kill_pid_info() into kill_pid_info_type(), this allows to pass any
> pid_type to group_send_sig_info(), despite its name it should work fine
> even if type = PIDTYPE_PID.
> 
> Change pidfd_send_signal() to use PIDTYPE_PID or PIDTYPE_TGID depending
> on PIDFD_THREAD.
> 
> While at it kill another TODO comment in pidfd_show_fdinfo(). As Christian
> expains fdinfo reports f_flags, userspace can already detect PIDFD_THREAD.
> 
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---

How do you feel about the following (untested...) addition?
I've played with PIDFD_SIGNAL_PROCESS_GROUP as well but that code is
fairly new to me so I would need some more time.

From a473512ed8de2e864961f7009e2f20ce4e7a0778 Mon Sep 17 00:00:00 2001
From: Christian Brauner <brauner@kernel.org>
Date: Fri, 9 Feb 2024 15:49:45 +0100
Subject: [PATCH] [RFC] pidfd: allow to override signal scope in
 pidfd_send_signal()

Right now we determine the scope of the signal based on the type of
pidfd. There are use-cases where it's useful to override the scope of
the signal. For example in [1]. Add flags to determine the scope of the
signal:

(1) PIDFD_SIGNAL_THREAD: send signal to specific thread
(2) PIDFD_SIGNAL_THREAD_GROUP: send signal to thread-group

I've put off PIDFD_SIGNAL_PROCESS_GROUP for now since I need to stare at
the code a bit longer how this would work.

Link: https://github.com/systemd/systemd/issues/31093 [1]
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 include/uapi/linux/pidfd.h |  4 ++++
 kernel/signal.c            | 35 ++++++++++++++++++++++++++++-------
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h
index 2e6461459877..757ed5a668c6 100644
--- a/include/uapi/linux/pidfd.h
+++ b/include/uapi/linux/pidfd.h
@@ -10,4 +10,8 @@
 #define PIDFD_NONBLOCK	O_NONBLOCK
 #define PIDFD_THREAD	O_EXCL
 
+/* Flags for pidfd_send_signal(). */
+#define PIDFD_SIGNAL_THREAD		(1UL << 0)
+#define PIDFD_SIGNAL_THREAD_GROUP	(1UL << 1)
+
 #endif /* _UAPI_LINUX_PIDFD_H */
diff --git a/kernel/signal.c b/kernel/signal.c
index 9578ce17d85d..1d6586964099 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -3872,6 +3872,9 @@ static struct pid *pidfd_to_pid(const struct file *file)
 	return tgid_pidfd_to_pid(file);
 }
 
+#define PIDFD_SEND_SIGNAL_FLAGS \
+	(PIDFD_SIGNAL_THREAD | PIDFD_SIGNAL_THREAD_GROUP)
+
 /**
  * sys_pidfd_send_signal - Signal a process through a pidfd
  * @pidfd:  file descriptor of the process
@@ -3889,14 +3892,19 @@ static struct pid *pidfd_to_pid(const struct file *file)
 SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 		siginfo_t __user *, info, unsigned int, flags)
 {
-	int ret;
+	int ret, si_code;
 	struct fd f;
 	struct pid *pid;
 	kernel_siginfo_t kinfo;
 	bool thread;
+	enum pid_type si_scope;
 
 	/* Enforce flags be set to 0 until we add an extension. */
-	if (flags)
+	if (flags & ~PIDFD_SEND_SIGNAL_FLAGS)
+		return -EINVAL;
+
+	/* Ensure that only a single signal scope determining flag is set. */
+	if (hweight32(flags & PIDFD_SEND_SIGNAL_FLAGS) > 1)
 		return -EINVAL;
 
 	f = fdget(pidfd);
@@ -3914,7 +3922,22 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 	if (!access_pidfd_pidns(pid))
 		goto err;
 
-	thread = f.file->f_flags & PIDFD_THREAD;
+	switch (flags) {
+	case 0:
+		/* Infer scope from the type of pidfd. */
+		thread = (f.file->f_flags & PIDFD_THREAD);
+		si_scope = thread ? PIDTYPE_PID : PIDTYPE_TGID;
+		si_code = thread ? SI_TKILL : SI_USER;
+		break;
+	case PIDFD_SIGNAL_THREAD:
+		si_scope = PIDTYPE_PID;
+		si_code = SI_TKILL;
+		break;
+	case PIDFD_SIGNAL_THREAD_GROUP:
+		si_scope = PIDTYPE_TGID;
+		si_code = SI_USER;
+		break;
+	}
 
 	if (info) {
 		ret = copy_siginfo_from_user_any(&kinfo, info);
@@ -3931,12 +3954,10 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 		    (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
 			goto err;
 	} else {
-		prepare_kill_siginfo(sig, &kinfo,
-				     thread ? SI_TKILL : SI_USER);
+		prepare_kill_siginfo(sig, &kinfo, si_code);
 	}
 
-	ret = kill_pid_info_type(sig, &kinfo, pid,
-				 thread ? PIDTYPE_PID : PIDTYPE_TGID);
+	ret = kill_pid_info_type(sig, &kinfo, pid, si_scope);
 err:
 	fdput(f);
 	return ret;
-- 
2.43.0


  parent reply	other threads:[~2024-02-09 15:15 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-09 13:06 [PATCH v2 1/2] signal: add the "int si_code" arg to prepare_kill_siginfo() Oleg Nesterov
2024-02-09 13:06 ` [PATCH v2 2/2] pidfd: change pidfd_send_signal() to respect PIDFD_THREAD Oleg Nesterov
2024-02-09 15:11   ` Christian Brauner
2024-02-09 15:15   ` Christian Brauner [this message]
2024-02-09 15:43     ` Oleg Nesterov
2024-02-09 15:49       ` Christian Brauner
2024-02-09 15:56         ` Oleg Nesterov
2024-02-10 10:23           ` Christian Brauner
2024-02-10 12:30             ` Oleg Nesterov
2024-02-10 12:47               ` Oleg Nesterov
2024-02-10 12:54               ` Christian Brauner
2024-02-10 13:15                 ` Oleg Nesterov
2024-02-10 14:26                   ` Christian Brauner
2024-02-10 16:51                     ` Oleg Nesterov
2024-02-10 17:22                       ` Christian Brauner
2024-02-14 12:36                       ` Oleg Nesterov
2024-02-16 12:28                         ` Christian Brauner
2024-02-16 13:06                           ` Oleg Nesterov
2024-02-16 14:46                             ` Christian Brauner
2024-02-16 18:12                               ` Oleg Nesterov
2024-02-20  8:34                                 ` Christian Brauner
2024-02-20  9:02                                   ` Oleg Nesterov
2024-02-20  9:22                                     ` Christian Brauner
2024-02-20 11:00                                       ` Oleg Nesterov
2024-02-20 12:59                                         ` Christian Brauner
2024-02-20 16:22                                           ` Oleg Nesterov
2024-02-21  7:42                                             ` Christian Brauner
2024-02-21 12:55                                               ` Oleg Nesterov
2024-02-21 13:35                                                 ` Christian Brauner
2024-02-09 19:08   ` Tycho Andersen
2024-02-09 15:10 ` [PATCH v2 1/2] signal: add the "int si_code" arg to prepare_kill_siginfo() Christian Brauner
2024-02-09 16:13 ` Christian Brauner
2024-02-09 16:22 ` Eric W. Biederman
2024-02-09 16:39   ` Oleg Nesterov
2024-02-09 19:36     ` Christian Brauner
2024-02-09 19:53       ` Oleg Nesterov
2024-02-09 20:01       ` Tycho Andersen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240209-stangen-feuerzeug-17c8662854c9@brauner \
    --to=brauner@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=oleg@redhat.com \
    --cc=tycho@tycho.pizza \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox