Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp991997rdb; Fri, 9 Feb 2024 07:15:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IFpIxktWlDcIv0J+IBOR5jhV0Gk9BwlGhsukGZc5dWcAGHge26UKh4EBq+yjapN/NrCg6RG X-Received: by 2002:a17:906:f213:b0:a36:fc06:5758 with SMTP id gt19-20020a170906f21300b00a36fc065758mr1324652ejb.0.1707491743054; Fri, 09 Feb 2024 07:15:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707491743; cv=pass; d=google.com; s=arc-20160816; b=ArU2eUBqrHNhbC/tKUkSW1DYj5FMDzERh+RxbriovEmd4RI2blnC72XtNrmoQTRw2i znY5MlrJ+FcmNQDBxC3QJWlRkiGU+4xkV8YqWKpf2pw4FM7YgzbohwEE5AfkVqLpUXmZ zrQtoef3rKu647ht3pH5cji/mCWUtUpl9YUI4MSANwivJe57GK0FpkZ+WrkIPANTdguV sQwKx19IGWGx/8O+HXJXIv02s103TWK1g7UZIIsm6+FZ4aQhU/BP+Gn0uWra+NxlmLF+ Ba3T7bRvvTYJHomCYDZaWa+A3hTtioIt97IVNsr5xHxqL17qplW2HO1ba8vLqBVmyQFM UJmQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=MTmWnvXMwmZRxYSxMv1bMu3fcrG6x+UhYFC2MBWKAJw=; fh=aGRWBYNhQPGha2/bwQERW2W/tCZ5YM1cz6O2MPhwQrY=; b=KwLT/9Y6ntwtzaP1G2tKqaJHRrwLq4yNZgOnkPoFKYw32DTw0EJ6mMzbS713Wq+thp ecHL6WIkFhrJZxW1jLm16KR/QdezqfV8dG1n9fltrf2ytEWBRHgoi1+qWOV7YtnfAYFP ljhQdHvrWxBdVAzWseDULBpTdO0bOdqk933N3BdtwJ24feF53aKuh7VY/YY2qxbkcmaM MsE7thP3iyhBX68VGJv+KmY3kt4orT5/DkjzUWtiwa0c3qKxaVK9J7rV3SAf6JAvFi6M Pf2yU2Cx9m1dk98pnD1SPfTyzrLH1IIMmHFJ3D5F4O8b5ZWtkkPxtC6mOKclB96TvLPk MdTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J1tyFmwb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-59523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59523-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCUtCi2LlHpq6GUMkZzgSH8HLjUOk34V2HiZo30HRIvniHT178BwaI88M4CsOgwy8xJleQupKvS+ULI9jmprONs7lcQ1ePi81Pt57Lph9Q== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id rn2-20020a170906d92200b00a3bfbc03c81si478846ejb.923.2024.02.09.07.15.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 07:15:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J1tyFmwb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-59523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59523-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A5C411F2B9EB for ; Fri, 9 Feb 2024 15:15:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D9D271B2F; Fri, 9 Feb 2024 15:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J1tyFmwb" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A5946EB65; Fri, 9 Feb 2024 15:15:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707491724; cv=none; b=FqmqiwFRNN29rEq2Bampqlbovekp+aiZWuiAH165auv+aJ4UwIlT4DmdLAnkdsWpv3uq50sbY8wGJnr73sdAuY1Y/1ilde4yj0L+EpIgfj2hQ+AKoAGR3Utq48ziE++l7bYHone1x1wAKQKEPBCxoEFCo8tpM/8Im8R39XGqReo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707491724; c=relaxed/simple; bh=vhoLSSlL/5ddek+ZFOhIOvsQoTmINNqk4DydXbkdtGc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aANSQgUeG9xQzCRAYkFtXegv2ESt3JeSvw2gcSu72OniYoLBVNY0ASU+b9rp7yr53NLkf+TlSO7AH3et+qvbMqJ+w9j8sy8KwT8F9r3QuV2Sc+uQj4Y/R33f9Rj8BJCzlN77Lr+MvYommIcacTDOfaJzFcz6ifceJitAO+BFZHg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J1tyFmwb; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52316C433F1; Fri, 9 Feb 2024 15:15:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707491723; bh=vhoLSSlL/5ddek+ZFOhIOvsQoTmINNqk4DydXbkdtGc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=J1tyFmwbVm6zKZ64DuT5j6bfXPbNK68FHowfNrIFTprDYA7b/2CUkMM5BJcQgv12E /s0lEnSuQlBHkCaoQxCoGv9MAdujszFLhG8a8DQT+KlZ0fFvr5YPOyWQtazx/aul3H VGo5T5dLkXXBiu8aPR8pZGbAV6cfbDdz8f2PplVXSX6yfTFu9yjZDdOYy5jGqL9TTO 4GD5npkOL3Upa98GAmQLj9LQlwexMtzi2MvxzEj04IR7hRB+G1CwDpxw8JZDxQVPYC 8PO130wg0RJf7r0b/fnUEdCXw/WTA7akiYT+VADlg86aNKNUmOWZV3gWiaPrKYU1TQ ZqUxFoHs6AbHw== Date: Fri, 9 Feb 2024 16:15:18 +0100 From: Christian Brauner To: Oleg Nesterov Cc: Andy Lutomirski , "Eric W. Biederman" , Tycho Andersen , 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 Message-ID: <20240209-stangen-feuerzeug-17c8662854c9@brauner> References: <20240209130620.GA8039@redhat.com> <20240209130650.GA8048@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 > --- 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 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 --- 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