Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1445678rdb; Sat, 10 Feb 2024 02:23:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEe4aiWAT3MtOdU6gxfjkVGQIrRTolyxBBtlBOdYT5/180zZn71kPur3dA/XCiyPfKeGAvi X-Received: by 2002:a62:ce4e:0:b0:6e0:54bc:3591 with SMTP id y75-20020a62ce4e000000b006e054bc3591mr1810783pfg.10.1707560605082; Sat, 10 Feb 2024 02:23:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707560605; cv=pass; d=google.com; s=arc-20160816; b=q4hDxTrDpMqAEa8jdzcyjcG6prMDYkp93D83SdVc7S31et93ZTd5q1YkZA8JYSc7p8 1cQMIAqgyY8PCwb5XathpIbpnn4V3zWwFU5DPoMMVRkpggi9B1q4W1F0P+CUWlwc09r5 OoT+rCNmpX9ci1gz6YaZjGV02rl56bd+3Bg5ULJBWg28nZlpnlIBYCumobodaU/pLqfl JhtG+uJZS5Mejzc4nf9z+7nal2pME2ihsGfA/6amzlFdLKtVt/rsJONs9Afp/dtm+C0Q 33HOhj40DNlZz813SFaliTb99NJdUuULfZVWu4XcEzW/ZTfoIJGK6ZSAutuNlIU05Gj6 IlhA== 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=0aNi3omGJpji9MfAb47lmopiqN2gYqC28IKAzt1e0yc=; fh=Dks9fxN4GNX7la81qCGa8EMNV6J73Tuu8P8pKW/9cog=; b=sAJ3Thy1ERjCjzbSIPuqeYyLMBzlUykzYCef2Sl6yUtaMBcOSuXCLWu2Mk4tXRaT0w w/GIXHpnv6SkhyHsN/mQAFvxhqbQgI2BsC9351Uju1B7gN6nUGDp5/o+ncT2+RGvL2KS GkZlj+ntVUbRYGdl21xlA2c1pFGQ2RzT616xySJufi7KWmOq3ikScjst5/84/E6aMDAs tHHDuRWNOOi7Ug+6emQrWlQC7ohL9YIWnvsqJPpr8E4DxPr/3E3+dv1OHzXm0Zs5hWue 49DJtaGEDVuUhWtoXFn70omX3GAG8p/i51+MDg2we2WzAd5YZp7n/mSOnyxeyWRDPRej PgOw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jAVZeQQH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-60360-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60360-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; AJvYcCVUHO4B3t/305s819qy3BHkoDyH2ruP2GWRGOyN6RkF0ldkF9JwVp6jX5rtnRmHNpUY9kOd/UVT+P6afglTpTDkjO/41CUynU5k/Uu3jw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ka30-20020a056a00939e00b006e0901330e8si1909328pfb.368.2024.02.10.02.23.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 02:23:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60360-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jAVZeQQH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-60360-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60360-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8FA762862B6 for ; Sat, 10 Feb 2024 10:23:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB036364C7; Sat, 10 Feb 2024 10:23:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jAVZeQQH" 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 E9ED03611E; Sat, 10 Feb 2024 10:23:16 +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=1707560597; cv=none; b=rfmlGfP7mv5eysK3yvYIXpMfCc7+H9aSQMZFCDhHjN/QK9vcTteQIEe58/UVXIEXk4eMR+sw3MRMTJmIbwT11j1cIi9Ay3NR4KhFPdHHC9+wPjIeXTeweIdTzPIoS1jWFPTaYYJVHIxTFS95lDLJilL+S9mqWshVDc9d2m74AOc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707560597; c=relaxed/simple; bh=2NohwMntg56S0VFXTKPyYwyqPYjBkrLyO8gcG29B3Mo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DEoGw268O7YI9PiS3D1KksrMfqFYjkqV8tqtcD/05Y89VV2ZbOKI7PLgvlVcS86gqFODDXLro/l/DTSgTafsCWZ8Qhk7Rw7PPeiFHpqDwGRATlc6wHyWRhXKtGVVV9Y0FvZAi3MgTZGjyKh2JsdLCht5KzWbxez/dIn8xwEa07Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jAVZeQQH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0F9CC433F1; Sat, 10 Feb 2024 10:23:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707560596; bh=2NohwMntg56S0VFXTKPyYwyqPYjBkrLyO8gcG29B3Mo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jAVZeQQH9UC1scU5hGFRc4k61xLYvTY2ARezjvcemJ6ZzbJmGtMcr6vgUGkVrTJxi iDf2FCglJ5/zZR67xDI4FArAJ40ISIVoeARw1vFWTLymmavKmpmxNQWyijhChhMO2S +ljePcFN6cyPiQVTpURFw0z4WSXvPpToAqoW3tovzWxY+QFzi2sOjK/jDDnVXogfk7 G2LUErlmP3K7EQ79fgCfc9Le4BpFw7Wxhz5u1iA9DBYwaVWNrla8y8aFSpKtNqMy4J wOj7UBviwIvtJuHdfD9ftZcC8VjTEmug7ogVV5lOc35w65kMn8OnwlX2iOFbZWtNUd bJPbkYc1xhEUg== Date: Sat, 10 Feb 2024 11:23:12 +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: <20240210-abfinden-beimessen-2dbfea59b0da@brauner> References: <20240209130620.GA8039@redhat.com> <20240209130650.GA8048@redhat.com> <20240209-stangen-feuerzeug-17c8662854c9@brauner> <20240209154305.GC3282@redhat.com> <20240209-radeln-untrennbar-9d4ae05aa4cc@brauner> <20240209155644.GD3282@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: <20240209155644.GD3282@redhat.com> > and I forgot to mention, if you want to add PIDFD_SIGNAL_PRGP you can > look at __kill_pgrp_info(). Yeah, I did that and there's a semantical twist in the old kill(2) system call that made me think: (1) kill(-1234) => kill process group with id 1234 (2) kill(0) => kill process group of @current which implementation wise is indicated by __kill_pgrp_info(..., pid ? find_vpid(-pid) ? task_pgrp(current)) We're obviously not going to implement (2) as that doesn't really make a sense for pidfd_send_signal(). But (1) is also wrong for pidfd_send_signal(). If we'd ever implement (1) it should be via pidfd_open(1234, PIDFD_PROCESS_GROUP). So if PIDFD_PROCESS_GROUP is set then we want to send a signal to the process group that @pidfd is in. Unless there's reasons we can't do this. I tried to draft this and what I have is a totally uncompiled draft. I was unsure how best to cleanly express how to take the process group from the struct pid of that @pidfd. So I modeled it after how we do it for PIDTYPE_TGID. From 8d886b07cc1b17cc6dd3a9ebf19c51212282b6f5 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 reference by @pidfd (2) PIDFD_SIGNAL_THREAD_GROUP: send signal to thread-group of @pidfd (2) PIDFD_SIGNAL_PROCESS_GROUP: send signal to process-group of @pidfd There's a semantical quirk in the old kill(2) system call that made me think: (1) kill(-1234) => kill process group with id 1234 (2) kill(0) => kill process group of @current as indicated by __kill_pgrp_info(..., pid ? find_vpid(-pid) ? task_pgrp(current)) We're obviously not going to implement (2) as that doesn't really make a lot of sense for pidfd_send_signal(). But (1) is also wrong for pidfd_send_signal(). If we'd ever implement (1) it should be via pidfd_open(..., PIDFD_PROCESS_GROUP). Link: https://github.com/systemd/systemd/issues/31093 [1] Signed-off-by: Christian Brauner --- include/uapi/linux/pidfd.h | 5 +++++ kernel/signal.c | 46 ++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h index 2e6461459877..72ec000a97cd 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -10,4 +10,9 @@ #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) +#define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2) + #endif /* _UAPI_LINUX_PIDFD_H */ diff --git a/kernel/signal.c b/kernel/signal.c index 8b8169623850..f0f9a5a822b4 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -3873,6 +3873,23 @@ static struct pid *pidfd_to_pid(const struct file *file) return tgid_pidfd_to_pid(file); } +static int kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pid) +{ + struct task_struct *p; + int ret = -ESRCH; + + read_lock(&tasklist_lock); + p = pid_task(pid, PIDTYPE_PID); + if (p) + ret = __kill_pgrp_info(sig, info, task_pgrp(p)); + read_unlock(&tasklist_lock); + return ret; +} + +#define PIDFD_SEND_SIGNAL_FLAGS \ + (PIDFD_SIGNAL_THREAD | PIDFD_SIGNAL_THREAD_GROUP | \ + PIDFD_SIGNAL_PROCESS_GROUP) + /** * sys_pidfd_send_signal - Signal a process through a pidfd * @pidfd: file descriptor of the process @@ -3897,7 +3914,11 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, enum pid_type type; /* 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); @@ -3915,10 +3936,24 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, if (!access_pidfd_pidns(pid)) goto err; - if (f.file->f_flags & PIDFD_THREAD) + switch (flags) { + case 0: + /* Infer scope from the type of pidfd. */ + if (f.file->f_flags & PIDFD_THREAD) + type = PIDTYPE_PID; + else + type = PIDTYPE_TGID; + break; + case PIDFD_SIGNAL_THREAD: type = PIDTYPE_PID; - else + break; + case PIDFD_SIGNAL_THREAD_GROUP: type = PIDTYPE_TGID; + break; + case PIDFD_SIGNAL_PROCESS_GROUP: + type = PIDTYPE_PGID; + break; + } if (info) { ret = copy_siginfo_from_user_any(&kinfo, info); @@ -3938,7 +3973,10 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, prepare_kill_siginfo(sig, &kinfo, type); } - ret = kill_pid_info_type(sig, &kinfo, pid, type); + if (type == PIDFD_SIGNAL_PROCESS_GROUP) + ret = kill_pgrp_info(sig, &kinfo, pid); + else + ret = kill_pid_info_type(sig, &kinfo, pid, type); err: fdput(f); return ret; -- 2.43.0