Thread (44 messages) 44 messages, 4 authors, 2024-02-01
STALE969d

[PATCH v3 3/3] clone: allow CLONE_THREAD | CLONE_PIDFD together

From: Tycho Andersen <tycho@tycho.pizza>
Date: 2024-01-23 15:35:11
Also in: lkml
Subsystem: exec & binfmt api, elf, kernel selftest framework, memory management - core, pidfd api, scheduler, the rest · Maintainers: Kees Cook, Shuah Khan, Shuah Khan, Andrew Morton, David Hildenbrand, Christian Brauner, Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot, Linus Torvalds

From: Tycho Andersen <redacted>

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 <redacted>
---
 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 44969cd472f0..25fccf7c08a7 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2296,9 +2296,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 c0624b127fab..a34208c2307e 100644
--- a/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c
+++ b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c
@@ -546,6 +546,22 @@ static int test_non_tgl_waitid_exec(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)();
@@ -557,6 +573,7 @@ struct pidfd_non_tgl_test {
 	T(test_non_tgl_exit_poll),
 	T(test_non_tgl_waitid_exit),
 	T(test_non_tgl_waitid_exec),
+	T(test_clone_thread_pidfd),
 };
 #undef T
 
-- 
2.34.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help