From patchwork Thu Nov 30 16:39:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 172000 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp521785vqy; Thu, 30 Nov 2023 08:41:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IEq7j+9ioZlusILcP2tKYEi75Wktd1jMyIBU2oVA3B8zDf6rP2BlNKY/Ak7OoA7ysJGyOQY X-Received: by 2002:a05:6a00:8c13:b0:6cb:c763:305a with SMTP id ih19-20020a056a008c1300b006cbc763305amr19513832pfb.24.1701362504770; Thu, 30 Nov 2023 08:41:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701362504; cv=none; d=google.com; s=arc-20160816; b=Bbc6mebKNcvI+4tH+S0Mqb7bumP2gB11wFgJ2e5VxNJ9TerrG6FyB+S2lnrM8Ky5bq 7SZMBSFvcpN3XMkEmpx9Ab42Q+6b6kH8W51UOac/rH8EZ7S9skhDo27hP7qLvIUR0yHe 0RwXyYxGrXhCHlj/9jctr7SA0i/KI6yybTwLn+74zRWE/K4lSo0rCgJDNEoykMMayVLb S6ZJfOTLJf/fRopz+JTQeRdG7b8niEqQFKUkNQWuE5Yp3+dbxjSxRs4UlbW4WUeu9uQv kJt+dEdbd83zbjYw5TbqHZfLtwPCeDhzOgbH1I4OuWW1zcrdUt/w0m0jnqO8aFhbnOCl uG4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:feedback-id:dkim-signature :dkim-signature; bh=GcRUTefwODfeWsEEU+T7P5vkTMTkxQIp7mTUU9VqXL8=; fh=cqt7tYfUjLVb4iAKPLlMbd5K0yJUwDstCHDT1mZCG8Y=; b=CA9lRJ/7NOS3wN/KgrK6ORplCFdzLvyPgrUsIXTgHDCge3DI06lN/L/q8RTn9kHNzT Xc7aDJwQqoOph6lcqOYHGOQSg+3PNlyETJ/2Vk7p2xWp6XT2S+dgWd0TajfJlBHvn0oj lUDodHNJxp+8yHvra8+FN3gZjvVvhMgDRhhDXsAdUawK/R0+3mFoss7Cc/M8Mcm4bH5E TYAYccCWwUjtxwGhqaLJ8YCV9qE/3Fv+t9zOkUlwZ2xwqzIHjhHfH2dMLu5lKWJtSTvk gw7raNmGOMQKuOjsEo3HYyvEghS7bpZerR6pfVISltD9or11DWOQCzkWplcR7JgOojsx 0NCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=xGCt99og; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=MRoPsenN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id t17-20020a056a0021d100b006cddc776c74si451612pfj.324.2023.11.30.08.41.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 08:41:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=xGCt99og; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=MRoPsenN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id CF1FB808206B; Thu, 30 Nov 2023 08:41:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345688AbjK3QlE (ORCPT + 99 others); Thu, 30 Nov 2023 11:41:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235171AbjK3Qkw (ORCPT ); Thu, 30 Nov 2023 11:40:52 -0500 Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51DB41A4; Thu, 30 Nov 2023 08:40:57 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id EA2735C006B; Thu, 30 Nov 2023 11:40:53 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 30 Nov 2023 11:40:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho.pizza; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:sender:subject :subject:to:to; s=fm3; t=1701362453; x=1701448853; bh=GcRUTefwOD feWsEEU+T7P5vkTMTkxQIp7mTUU9VqXL8=; b=xGCt99ogNyz5PF4Lg0iAzjC7qp FTlBXswcvclsYpRpetMTOZeHvn0VKjXlt5IxOKdgHGAdqFfr7wACfU37IXpvx/6t P8GYFuhEuY1+1HU/lQU4KGELsKb+Shoqwh6YBsDfec4s8aGLKT6QUYU26PSui/6I /4gT+bDjkIJGkOJp6Gbxa7FpsfUJjyWrNHakgInrdTcGADekAP8zHs0ntdDyfBnF qcu93zQEGRxPbQRSSR5Sh/2L5PmeQW/DnL/8cjpFNAWqGjyRdzu0y0eBBV8VyEuo nw4aOFHD4fteUzqZZFz14NyYmX9BKhwRd9BnVu3tyScO7/ArxN7XfBz0rPPg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1701362453; x=1701448853; bh=GcRUTefwODfeW sEEU+T7P5vkTMTkxQIp7mTUU9VqXL8=; b=MRoPsenNPeU3+ceAOVhtiJAXfV/uR GTn22jjH2BZ5z93PfBkF7NgpW7yARoJtGwnZ+pew+zZqqULn+2GKr02BLwdjYVg8 7VbnJ0yYaeLbvBy9Db+9x21UY4Lpk3ozF8v80qwwkvLX5G05+7gFGAE1Yb6R6xLD thYnzYkEMKNVz1cyfUx7xzcK+Cu84vVl1kxlXt0cZ4AVHTqcGF+nVl2XdG5yqcLP RxjinEvBgtwLrUbq6zpGD7XC5bLhk5BjrUrWxbLm3NrSk0ArDsLkPksVv5J4XINd XIAv97dzF9BE10dgkLytjqllQj8UE+NC3FTwCNdFFrxPzlN9noMKkwr0A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeijedgleduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgggfestdekredtredttdenucfhrhhomhepvfihtghhohcu tehnuggvrhhsvghnuceothihtghhohesthihtghhohdrphhiiiiirgeqnecuggftrfgrth htvghrnhepheeffeehleeftdfgjeegheelieefvdfghfeuudeuheehuefhhffhtefhiedv geegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepth ihtghhohesthihtghhohdrphhiiiiirg X-ME-Proxy: Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 30 Nov 2023 11:40:52 -0500 (EST) From: Tycho Andersen To: Christian Brauner Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen , Tycho Andersen Subject: [RFC 1/3] pidfd: allow pidfd_open() on non-thread-group leaders Date: Thu, 30 Nov 2023 09:39:44 -0700 Message-Id: <20231130163946.277502-1-tycho@tycho.pizza> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 30 Nov 2023 08:41:40 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784007890040652279 X-GMAIL-MSGID: 1784007890040652279 From: Tycho Andersen We are using the pidfd family of syscalls with the seccomp userspace notifier. When some thread triggers a seccomp notification, we want to do some things to its context (munge fd tables via pidfd_getfd(), maybe write to its memory, etc.). However, threads created with ~CLONE_FILES or ~CLONE_VM mean that we can't use the pidfd family of syscalls for this purpose, since their fd table or mm are distinct from the thread group leader's. In this patch, we relax this restriction for pidfd_open(). In order to avoid dangling poll() users we need to notify pidfd waiters when individual threads die, but once we do that all the other machinery seems to work ok viz. the tests. But I suppose there are more cases than just this one. Another weirdness is the open-coding of this vs. exporting using do_notify_pidfd(). This particular location is after __exit_signal() is called, which does __unhash_process() which kills ->thread_pid, so we need to use the copy we have locally, vs do_notify_pid() which accesses it via task_pid(). Maybe this suggests that the notification should live somewhere in __exit_signals()? I just put it here because I saw we were already testing if this task was the leader. Signed-off-by: Tycho Andersen --- kernel/exit.c | 29 +++++++++++++++++++---------- kernel/fork.c | 4 +--- kernel/pid.c | 11 +---------- 3 files changed, 21 insertions(+), 23 deletions(-) base-commit: 2cc14f52aeb78ce3f29677c2de1f06c0e91471ab diff --git a/kernel/exit.c b/kernel/exit.c index ee9f43bed49a..34eeefc7ee21 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -263,16 +263,25 @@ void release_task(struct task_struct *p) */ zap_leader = 0; leader = p->group_leader; - if (leader != p && thread_group_empty(leader) - && leader->exit_state == EXIT_ZOMBIE) { - /* - * If we were the last child thread and the leader has - * exited already, and the leader's parent ignores SIGCHLD, - * then we are the one who should release the leader. - */ - zap_leader = do_notify_parent(leader, leader->exit_signal); - if (zap_leader) - leader->exit_state = EXIT_DEAD; + if (leader != p) { + if (thread_group_empty(leader) + && leader->exit_state == EXIT_ZOMBIE) { + /* + * If we were the last child thread and the leader has + * exited already, and the leader's parent ignores SIGCHLD, + * then we are the one who should release the leader. + */ + zap_leader = do_notify_parent(leader, + leader->exit_signal); + if (zap_leader) + leader->exit_state = EXIT_DEAD; + } else { + /* + * wake up pidfd pollers anyway, they want to know this + * thread is dying. + */ + wake_up_all(&thread_pid->wait_pidfd); + } } write_unlock_irq(&tasklist_lock); diff --git a/kernel/fork.c b/kernel/fork.c index 10917c3e1f03..eef15c93f6cf 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2163,8 +2163,6 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re * Allocate a new file that stashes @pid and reserve a new pidfd number in the * caller's file descriptor table. The pidfd is reserved but not installed yet. * - * The helper verifies that @pid is used as a thread group leader. - * * If this function returns successfully the caller is responsible to either * call fd_install() passing the returned pidfd and pidfd file as arguments in * order to install the pidfd into its file descriptor table or they must use @@ -2182,7 +2180,7 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re */ int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret) { - if (!pid || !pid_has_task(pid, PIDTYPE_TGID)) + if (!pid) return -EINVAL; return __pidfd_prepare(pid, flags, ret); diff --git a/kernel/pid.c b/kernel/pid.c index 6500ef956f2f..4806798022d9 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -552,11 +552,6 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) * Return the task associated with @pidfd. The function takes a reference on * the returned task. The caller is responsible for releasing that reference. * - * Currently, the process identified by @pidfd is always a thread-group leader. - * This restriction currently exists for all aspects of pidfds including pidfd - * creation (CLONE_PIDFD cannot be used with CLONE_THREAD) and pidfd polling - * (only supports thread group leaders). - * * Return: On success, the task_struct associated with the pidfd. * On error, a negative errno number will be returned. */ @@ -615,11 +610,7 @@ int pidfd_create(struct pid *pid, unsigned int flags) * @flags: flags to pass * * This creates a new pid file descriptor with the O_CLOEXEC flag set for - * the process identified by @pid. Currently, the process identified by - * @pid must be a thread-group leader. This restriction currently exists - * for all aspects of pidfds including pidfd creation (CLONE_PIDFD cannot - * be used with CLONE_THREAD) and pidfd polling (only supports thread group - * leaders). + * the process identified by @pid. * * Return: On success, a cloexec pidfd is returned. * On error, a negative errno number will be returned. From patchwork Thu Nov 30 16:39:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 171999 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp521578vqy; Thu, 30 Nov 2023 08:41:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IHOmx8NZQ5aszy+pXuKEuW9ozx+awyh26dp9OL+5IPhV4AdLtxoHllUixUCgru5Vq21igi3 X-Received: by 2002:a17:903:234a:b0:1cf:e9b5:90ee with SMTP id c10-20020a170903234a00b001cfe9b590eemr16546764plh.24.1701362489065; Thu, 30 Nov 2023 08:41:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701362489; cv=none; d=google.com; s=arc-20160816; b=ZvokczROTCZBfovTjFV8kpED+KsN3D6Fwqq697KfOFz4xquhXdmDAC722Xy5SYSWoD zt/rDf4eOVFeFVu10gocIkyURO5C8R/RU/jcJH/NyPT/S2fjNfH1Rj9UfC0jv2JO77iO tbod+4Cqe9wAKEcRFePzhKj2UQugIRYB5uDsnG1+qONBygF4lpLRYq6Wl4KHoyvH6sOU V/U6jqbzXF1CF9vUeAi4nTpwrmZCoNHsJvVv4dbNZwU+5oN8kYT66GYjfKOzn2ozLkrF v5cFaHKWhp3v6SSRFWsxYJDag8ua0PgwcFydjGd6CrEw+JsD29JRrutMijeuKNhFFSeM VMkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=O7XDM5I4HDfFeLFMMof+StJQpaGDLxLBwRqMH7T4gUE=; fh=cqt7tYfUjLVb4iAKPLlMbd5K0yJUwDstCHDT1mZCG8Y=; b=BrJ1HAEtP9rgg+jZSmRN/8M8kQ02eH9toGiaTdr6GVRj/0HswRRtYFi4tH1ipfLjTh 6AUp+4L40CJVD3ac2/YepQj+K8nDc5xv8DigEo2vuZNkXUsRqfy8N0YzrK4xp6fg+L6b 5WqLrBxwUGPCgJKSiw2yGsTVli2RE8LnI/Njzib6H4ZLXe1Ww00ATfZrQ0EwGWzSjx1y NDL0J4DbBDYMBF113IplPbPYFMbyhKQZeOQ4rn0Llf1GeaSq4ahZgnl2np8BgGZ0lj/Q cwrlrq4Mr0gUNha22+4s9/NJdZtaAlXtYauOP/U5PaDeTZMgWmTfwKGl+PcneInrIF9L sw5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b="gRtc9F/d"; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=oaN3DPDt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id q7-20020a170902eb8700b001c60d334996si1559847plg.622.2023.11.30.08.41.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 08:41:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b="gRtc9F/d"; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=oaN3DPDt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 75D0E80417C7; Thu, 30 Nov 2023 08:41:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229483AbjK3Qk6 (ORCPT + 99 others); Thu, 30 Nov 2023 11:40:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231919AbjK3Qkw (ORCPT ); Thu, 30 Nov 2023 11:40:52 -0500 Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5352610D0; Thu, 30 Nov 2023 08:40:57 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 0887C5C002F; Thu, 30 Nov 2023 11:40:56 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Thu, 30 Nov 2023 11:40:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho.pizza; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701362456; x= 1701448856; bh=O7XDM5I4HDfFeLFMMof+StJQpaGDLxLBwRqMH7T4gUE=; b=g Rtc9F/dVbNU9ABp1p2Gi3bbX3ersvim1e9gYM/tH1IqLn0rYCX9UScbyELGSoVKx 9xSYuDQho0qHDJ4th+xGSgHSSLQ4Ely2dLlBk+P9WXjeFBTFCT3muFqjgLSsyNg7 5ZayYay1fULvZSIM/xHRrI1qvH9tPqYMWcJyg0BpCohizqhG5EId1nivVFu7SSha Aht96FVZ0DvsPOnljLDCjNUldpEAV+li5xhhJblqSLYN8L6fBZ/phsxoJMvOdj0E W1r0kk4tQyM0hZxFB9vzeKXEZGAZcelh6G5PjDmsFGdr7xAofocmlLzhZxRv2M6w uwVbpUiZw0iIbqYjFZppw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701362456; x= 1701448856; bh=O7XDM5I4HDfFeLFMMof+StJQpaGDLxLBwRqMH7T4gUE=; b=o aN3DPDtcjNQs2E+hRD8dRPS2+LzexfYU4za6xn3bghjrbJiSdW4eANioqjDxdZVL HuU0/QRsaEn00PFucfao4HZkc9x39L8D31S2vPoaclu6fkbgD/Y+0tMy/o7yadeF THoYj8cTAUxTnUg83KHlthXMhQHWMlz+Mrkqp/2jyCH1K1u8ctuprM27FMUVmxEu HMD8ZqnH8pwCGJEyD4Jwh3sAOug6movkrt8K3svM4COFXSzup0pv0kUEeazx1iad cFsgxWFFTcZgyuivGC7bQ1WFNZch7pXycaC7DA08RmD3ZTU9Kx2nG+MEDEf3p+Q9 LQg+IJ1xWauDaXjw7KHCg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeijedgleduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepvfihtghh ohcutehnuggvrhhsvghnuceothihtghhohesthihtghhohdrphhiiiiirgeqnecuggftrf grthhtvghrnhepvdegffehledvleejvdethffgieefveevhfeigefffffgheeguedtieek tdeigeeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epthihtghhohesthihtghhohdrphhiiiiirg X-ME-Proxy: Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 30 Nov 2023 11:40:54 -0500 (EST) From: Tycho Andersen To: Christian Brauner Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen , Tycho Andersen Subject: [RFC 2/3] selftests/pidfd: add non-thread-group leader tests Date: Thu, 30 Nov 2023 09:39:45 -0700 Message-Id: <20231130163946.277502-2-tycho@tycho.pizza> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130163946.277502-1-tycho@tycho.pizza> References: <20231130163946.277502-1-tycho@tycho.pizza> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 30 Nov 2023 08:41:09 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784007873457195114 X-GMAIL-MSGID: 1784007873457195114 From: Tycho Andersen This adds a family of tests for various behaviors of non-thread-group leaders. Maybe this should live in pidfd_open_test.c instead? We'd need some hoisting there then. Signed-off-by: Tycho Andersen --- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- .../selftests/pidfd/pidfd_non_tgl_test.c | 339 ++++++++++++++++++ 3 files changed, 342 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore index 973198a3ec3d..e7532e84a34a 100644 --- a/tools/testing/selftests/pidfd/.gitignore +++ b/tools/testing/selftests/pidfd/.gitignore @@ -6,3 +6,4 @@ pidfd_wait pidfd_fdinfo_test pidfd_getfd_test pidfd_setns_test +pidfd_non_tgl_test diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile index d731e3e76d5b..50e3aa9de05a 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile @@ -2,7 +2,8 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ - pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test + pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ + pidfd_non_tgl_test include ../lib.mk diff --git a/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c new file mode 100644 index 000000000000..e3992f2d88cf --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c @@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "pidfd.h" + +// glibc defaults to 8MB stacks +#define STACK_SIZE (8 * 1024 * 1024) +static char stack[STACK_SIZE]; + +static int thread_sleep(void *) +{ + while (1) + sleep(100); + return 1; +} + +static int fork_task_with_thread(int (*fn)(void *), int sk_pair[2], + pid_t *tgl, pid_t *thread, int *tgl_pidfd, + int *thread_pidfd) +{ + *tgl_pidfd = *thread_pidfd = -1; + + *tgl = fork(); + if (*tgl < 0) { + perror("fork"); + return -1; + } + + if (!*tgl) { + int flags = CLONE_THREAD | CLONE_VM | CLONE_SIGHAND; + pid_t t; + + t = clone(fn, stack + STACK_SIZE, flags, sk_pair); + if (t < 0) { + perror("clone"); + exit(1); + } + + close(sk_pair[1]); + + if (write(sk_pair[0], &t, sizeof(t)) != sizeof(t)) { + perror("read"); + exit(1); + } + + // wait to get killed for various reasons by the tests. + while (1) + sleep(100); + } + + errno = 0; + if (read(sk_pair[1], thread, sizeof(*thread)) != sizeof(*thread)) { + perror("read"); + goto cleanup; + } + + *tgl_pidfd = sys_pidfd_open(*tgl, 0); + if (*tgl_pidfd < 0) { + perror("pidfd_open tgl"); + goto cleanup; + } + + *thread_pidfd = sys_pidfd_open(*thread, 0); + if (*thread_pidfd < 0) { + perror("pidfd"); + goto cleanup; + } + + return 0; + +cleanup: + kill(*tgl, SIGKILL); + if (*tgl_pidfd >= 0) + close(*tgl_pidfd); + if (*thread_pidfd >= 0) + close(*thread_pidfd); + return -1; +} + +static int test_non_tgl_basic(void) +{ + pid_t tgl, thread; + int sk_pair[2], status; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_sleep, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + /* + * KILL of a thread should still kill everyone + */ + if (sys_pidfd_send_signal(thread_pidfd, SIGKILL, NULL, 0)) { + perror("pidfd_send_signal"); + goto cleanup; + } + + errno = 0; + if (waitpid(tgl, &status, 0) != tgl) { + perror("waitpid tgl"); + goto cleanup; + } + + if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL) { + ksft_print_msg("bad exit status %x\n", status); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + close(sk_pair[0]); + close(sk_pair[1]); + close(tgl_pidfd); + close(thread_pidfd); + return ret; +} + +static int thread_exec(void *arg) +{ + int *sk_pair = arg; + pid_t thread; + + if (read(sk_pair[0], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + exit(1); + } + + execlp("/bin/true", "/bin/true", NULL); + return 1; +} + +static int test_non_tgl_exec(void) +{ + pid_t tgl, thread; + int sk_pair[2]; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL, ready; + struct pollfd pollfd; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_exec, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + if (write(sk_pair[1], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + goto cleanup; + } + + // thread will exec(), so this pidfd should eventually be dead (i.e. + // poll should return). + pollfd.fd = thread_pidfd; + pollfd.events = POLLIN; + + ready = poll(&pollfd, 1, -1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 1) { + ksft_print_msg("bad poll result %d\n", ready); + goto cleanup; + } + + if (pollfd.revents != POLLIN) { + ksft_print_msg("bad poll revents: %x\n", pollfd.revents); + goto cleanup; + } + + errno = 0; + if (sys_pidfd_getfd(thread_pidfd, 0, 0) > 0) { + ksft_print_msg("got a real fd"); + goto cleanup; + } + + if (errno != ESRCH) { + ksft_print_msg("polling invalid thread didn't give ESRCH"); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + close(sk_pair[0]); + close(sk_pair[1]); + close(tgl_pidfd); + close(thread_pidfd); + return ret; +} + +int thread_wait_exit(void *arg) +{ + int *sk_pair = arg; + pid_t thread; + + if (read(sk_pair[0], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + exit(1); + } + + return 0; +} + +static int test_non_tgl_exit(void) +{ + pid_t tgl, thread; + int sk_pair[2], status; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL, ready; + struct pollfd pollfd; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_wait_exit, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + if (write(sk_pair[1], &thread, sizeof(thread)) != sizeof(thread)) { + perror("write"); + goto cleanup; + } + + // thread will exit, so this pidfd should eventually be dead (i.e. + // poll should return). + pollfd.fd = thread_pidfd; + pollfd.events = POLLIN; + + ready = poll(&pollfd, 1, -1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 1) { + ksft_print_msg("bad poll result %d\n", ready); + goto cleanup; + } + + if (pollfd.revents != POLLIN) { + ksft_print_msg("bad poll revents: %x\n", pollfd.revents); + goto cleanup; + } + + errno = 0; + if (sys_pidfd_getfd(thread_pidfd, 0, 0) > 0) { + ksft_print_msg("got a real pidfd"); + goto cleanup; + } + + if (errno != ESRCH) { + ksft_print_msg("polling invalid thread didn't give ESRCH"); + goto cleanup; + } + + close(thread_pidfd); + + // ok, but the thread group *leader* should still be alive + pollfd.fd = tgl_pidfd; + ready = poll(&pollfd, 1, 1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 0) { + ksft_print_msg("polling leader returned something?! %x", pollfd.revents); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + kill(tgl, SIGKILL); + if (waitpid(tgl, &status, 0) != tgl) { + perror("waitpid"); + return KSFT_FAIL; + } + + return ret; +} + +#define T(x) { x, #x } +struct pidfd_non_tgl_test { + int (*fn)(); + const char *name; +} tests[] = { + T(test_non_tgl_basic), + T(test_non_tgl_exec), + T(test_non_tgl_exit), +}; +#undef T + +int main(int argc, char *argv[]) +{ + int i, ret = EXIT_SUCCESS; + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + switch (tests[i].fn()) { + case KSFT_PASS: + ksft_test_result_pass("%s\n", tests[i].name); + break; + case KSFT_SKIP: + ksft_test_result_skip("%s\n", tests[i].name); + break; + default: + ret = EXIT_FAILURE; + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + return ret; +} From patchwork Thu Nov 30 16:39:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 171998 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp521345vqy; Thu, 30 Nov 2023 08:41:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGGwDKMwFgbn68VSE8tru+Zof5QOu6gNWgFTYhfIvQnoG7Jq29kQxSDJqAkxrRKk+1rj6iQ X-Received: by 2002:a05:6830:2093:b0:6d7:d732:48f1 with SMTP id y19-20020a056830209300b006d7d73248f1mr42674otq.36.1701362472099; Thu, 30 Nov 2023 08:41:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701362472; cv=none; d=google.com; s=arc-20160816; b=gWNmSwRKCj8NwxdWV/nqxYRP8TA1Mjum2ypuwwa9RKBvUuKOKdju1zEtMUGaEKIY0i PEm0LR/EuFdEYAawr8VVeJskfN4CJtx+yn/8+I2MtHdG6c8OUREoeEGjd8hOY15MeLzS BbGUp2gF9JCHM0K6CtGz+vmwrfTd7WMeg2AgFH2Uyw5nZezYxfdj2R1v6lUXaU9oI94Y pJzcfagoaDCesEYSyX1pSLzx6cGdCbliREWljnIpquL6zdpljJoMzQWHwiHbehng6l9Y Wu/bxyYihLyGLkN6YH4TEe8otR+/kuA5EK9dsS6mcLUgc4n4ZgWyNObATkg5r9bfQaR7 yhAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=4sDD4mN+pIoyX7m8FGv2uCwfMAv7UW1AZsJ3O0T01T8=; fh=cqt7tYfUjLVb4iAKPLlMbd5K0yJUwDstCHDT1mZCG8Y=; b=X5oLFZn2dY0JK5KCib2Pzg4OKxV6MUkBXPs2o4noIo7jSORBp7mFSG3kP2neflx2wg itvBbUGOFfjNO+ZZG35Te7OCEza4rnc76Xaq3r8MiYB6Zg75vGU1hUjUs/qil2HuYQso +/YJhHhv35dAgL/vvz2QLfUGCyyDT/dUJm1oJK/IDNqEbt4aGDi/UrVf2ZPX7UMmzjRE Oll67bom1qLcMNwQa8/w78iqMyVEBOBX66Iu/6MNQCBCkqV7LUstKgNXBFlJTYSQeVhO LhdeFWilqBQtrCJuXpn/7WvaoJv+rWWXsdbxkmt+QaC7mU0gdblyXWvuCG478zAxrd8q zFLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=bfcSzGWg; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=aANGV5K1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id ck12-20020a056a02090c00b00565dd108fd4si378688pgb.115.2023.11.30.08.41.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 08:41:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=bfcSzGWg; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=aANGV5K1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id DFB4B82A1746; Thu, 30 Nov 2023 08:41:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232137AbjK3Qkz (ORCPT + 99 others); Thu, 30 Nov 2023 11:40:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345658AbjK3Qkw (ORCPT ); Thu, 30 Nov 2023 11:40:52 -0500 Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12AE110E2; Thu, 30 Nov 2023 08:40:58 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 7D8C25C007F; Thu, 30 Nov 2023 11:40:57 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 30 Nov 2023 11:40:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho.pizza; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701362457; x= 1701448857; bh=4sDD4mN+pIoyX7m8FGv2uCwfMAv7UW1AZsJ3O0T01T8=; b=b fcSzGWgxiasYLEPtKTIOyLWvupsxC9+Bncp3fVIRu3jCqyyUltHLmbYBQDqwAWD8 wKkJ6ihbiucwniyQbywRdsp0/zyHrCMt6K0oU9Y/qECgFaPcd5ZAxCs4WoHBSKIg LbAUH6PV26VlDiHrUM/XsiCXXlnXQkPQH3YVsAVjowoAKEPKYjSPY6LtlScg0uYe tf80TuUe0dgekCqV2MFtDanTSy3V9LIriJEaDvClZo+PECOjVHe9yfUy3+iLFk9i 4v5GQtxpCL4VdG2V69yEiNZkv4Ljg7cXKrrDiEsJmydYacKDCVMOCdDzb3tlesrf REOXsb8aZiwiPsi+WDlAA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701362457; x= 1701448857; bh=4sDD4mN+pIoyX7m8FGv2uCwfMAv7UW1AZsJ3O0T01T8=; b=a ANGV5K1XzH9CPL28GtdWbUILbVM8yIuEp0TA0y0lHtx0q0Gz38FvjgG9wAPCAyBb CSPxlP+VoIQMA9FNzH240SeYuN8zz9r3T+FTN+0XJA39XaFYyiwu8DeZhpvEZ2O4 SWBf70fveyFAe9+nCF8BU1rerdd9AAdZcmBcMcuMhRor5jM9cL9Few/D0WAfe5Xm nZzNcDHfShuEdtq6qoNT2Y/6149jNxuAg5m6X8l6Rt4e8AKyrELSznf5cEo7qreA 88iONaDp7R4vru3HwI5TCvSa1C8RPfE1bvQOCaPBhbp4ggnTl4rs/2DFtM0I585L RcUisRmQ+cNOlcp99hM0w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeijedgleduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepvfihtghh ohcutehnuggvrhhsvghnuceothihtghhohesthihtghhohdrphhiiiiirgeqnecuggftrf grthhtvghrnhepvdegffehledvleejvdethffgieefveevhfeigefffffgheeguedtieek tdeigeeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epthihtghhohesthihtghhohdrphhiiiiirg X-ME-Proxy: Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 30 Nov 2023 11:40:56 -0500 (EST) From: Tycho Andersen To: Christian Brauner Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen , Tycho Andersen Subject: [RFC 3/3] clone: allow CLONE_THREAD | CLONE_PIDFD together Date: Thu, 30 Nov 2023 09:39:46 -0700 Message-Id: <20231130163946.277502-3-tycho@tycho.pizza> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130163946.277502-1-tycho@tycho.pizza> References: <20231130163946.277502-1-tycho@tycho.pizza> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 30 Nov 2023 08:41:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784007855573891012 X-GMAIL-MSGID: 1784007855573891012 From: Tycho Andersen This removes the restriction of CLONE_THREAD | CLONE_PIDFD being specified together. Assuming the previous patch sorts out all the thorny issues this should be safe. I've left it as a separate patch since it is not strictly necessary as a usecase for us, but might be nice? Perhaps we want to wait until someone actually needs it though. Signed-off-by: Tycho Andersen --- kernel/fork.c | 3 +-- .../selftests/pidfd/pidfd_non_tgl_test.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index eef15c93f6cf..ada476f38b56 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2302,9 +2302,8 @@ __latent_entropy struct task_struct *copy_process( /* * - CLONE_DETACHED is blocked so that we can potentially * reuse it later for CLONE_PIDFD. - * - CLONE_THREAD is blocked until someone really needs it. */ - if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) + if (clone_flags & CLONE_DETACHED) return ERR_PTR(-EINVAL); } diff --git a/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c index e3992f2d88cf..dfd6a2cd85a3 100644 --- a/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c +++ b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c @@ -305,6 +305,22 @@ static int test_non_tgl_exit(void) return ret; } +static int test_clone_thread_pidfd(void) +{ + pid_t pid; + int flags = CLONE_THREAD | CLONE_VM | CLONE_SIGHAND | CLONE_PIDFD; + int pidfd; + + pid = clone(thread_sleep, stack + STACK_SIZE, flags, NULL, &pidfd); + if (pid < 0) { + perror("clone"); + return KSFT_FAIL; + } + + close(pidfd); + return KSFT_PASS; +} + #define T(x) { x, #x } struct pidfd_non_tgl_test { int (*fn)(); @@ -313,6 +329,7 @@ struct pidfd_non_tgl_test { T(test_non_tgl_basic), T(test_non_tgl_exec), T(test_non_tgl_exit), + T(test_clone_thread_pidfd), }; #undef T