Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Chris Mason <mason@kernel.org>
Cc: brauner@kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Felix Kuehling <Felix.Kuehling@amd.com>,
	amd-gfx@lists.freedesktop.org, akpm@linux-foundation.org,
	peterz@infradead.org, mingo@redhat.com, kees@kernel.org,
	viro@zeniv.linux.org.uk, jack@suse.cz, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com, bsingharora@gmail.com,
	eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev,
	martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev,
	sched-ext@lists.linux.dev, cgroups@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH] introduce signal_struct->group_leader
Date: Sun, 6 Dec 2026 16:58:50 +0100	[thread overview]
Message-ID: <axWGOnzsTXMjcyRj@redhat.com> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>

which currently "mirrors" task_struct->group_leader pointers. Once we
change the current users of ->group_leader to use task_group_leader()
we can kill the task_struct->group_leader member.

Not for inclusion, just a draft to get an early review from AI.
I am sure I missed something, I didn't even try to check this patch,
not to mention test it.

Why. task->group_leader points to nowhere if task exits, and people
often use it incorrectly. For example bacct_add_tsk() does

	delta = now_ns - tsk->group_leader->start_time;

and tsk->group_leader can already be freed. With this patch it can do

	scoped_guard (rcu)
		delta = now_ns - task_group_leader(tsk)->start_time;

task_group_leader(tsk) always returns an rcu-safe pointer.

If same_thread_group(tsk, current) == T than this pointer is stable
and can be dereferenced lockless.

Otherwise, it must be dereferenced under rcu/tasklist and its
->usage may be zero if the leader has already exited.

NOTE! the patch moves cgroup_task_free() from __put_task_struct() to
free_task() to ensure that signal->group_leader still has ->cgroups
even if it has alredy exited/reaped but there is another reference
to its sub-thread which keeps ->signal.

Is it fine? In particular in the err paths in copy_process() ?
Is it enough?

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 fs/exec.c                    |  1 +
 include/linux/sched/signal.h |  9 +++++++++
 init/init_task.c             |  1 +
 kernel/fork.c                | 20 ++++++++++++++++----
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..9030fc6919d4 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1015,6 +1015,7 @@ static int de_thread(struct task_struct *tsk)
 		list_replace_rcu(&leader->tasks, &tsk->tasks);
 		list_replace_init(&leader->sibling, &tsk->sibling);
 
+		rcu_assign_pointer(sig->group_leader, tsk);
 		tsk->group_leader = tsk;
 		leader->group_leader = tsk;
 
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 85c71db615e3..357e1b5b22b1 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -99,6 +99,9 @@ struct signal_struct {
 	int			quick_threads;
 	struct list_head	thread_head;
 
+	// rcu-safe but ->usage can be 0
+	struct task_struct      *group_leader;
+
 	wait_queue_head_t	wait_chldexit;	/* for wait4() */
 
 	/* current thread group signal load-balancing target: */
@@ -256,6 +259,12 @@ struct signal_struct {
 						 */
 } __randomize_layout;
 
+static inline struct task_struct *task_group_leader(const struct task_struct *p)
+{
+	// assert same_thread_group() || rcu || tasklist
+	return rcu_dereference_raw(p->signal->group_leader);
+}
+
 /*
  * Bits in flags field of signal_struct.
  */
diff --git a/init/init_task.c b/init/init_task.c
index ce7c2b07d855..7bef6abd6ab2 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -21,6 +21,7 @@
 
 static struct signal_struct init_signals = {
 	.nr_threads	= 1,
+	.group_leader	= &init_task,
 	.thread_head	= LIST_HEAD_INIT(init_task.thread_node),
 	.wait_chldexit	= __WAIT_QUEUE_HEAD_INITIALIZER(init_signals.wait_chldexit),
 	.shared_pending	= {
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb..127ab96408f8 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -532,6 +532,10 @@ void put_task_stack(struct task_struct *tsk)
 
 void free_task(struct task_struct *tsk)
 {
+	// false if called by bad_fork_free:
+	if (tsk->exit_state)
+		cgroup_task_free(tsk);
+
 #ifdef CONFIG_SECCOMP
 	WARN_ON_ONCE(tsk->seccomp.filter);
 #endif
@@ -775,26 +779,33 @@ static inline void free_signal_struct(struct signal_struct *sig)
 
 static inline void put_signal_struct(struct signal_struct *sig)
 {
-	if (refcount_dec_and_test(&sig->sigcnt))
+	if (refcount_dec_and_test(&sig->sigcnt)) {
+		// can't do this in free_signal_struct()
+		free_task(READ_ONCE(sig->group_leader));
 		free_signal_struct(sig);
+	}
 }
 
 void __put_task_struct(struct task_struct *tsk)
 {
+	bool free;
+
 	WARN_ON(!tsk->exit_state);
 	WARN_ON(refcount_read(&tsk->usage));
 	WARN_ON(tsk == current);
 
 	unwind_task_free(tsk);
 	io_uring_free(tsk);
-	cgroup_task_free(tsk);
 	task_numa_free(tsk, true);
 	security_task_free(tsk);
 	exit_creds(tsk);
 	delayacct_tsk_free(tsk);
-	put_signal_struct(tsk->signal);
 	sched_core_free(tsk);
-	free_task(tsk);
+	// Explain why this check can't be false positive/negative
+	free = rcu_access_pointer(tsk->signal->group_leader) != tsk;
+	put_signal_struct(tsk->signal);
+	if (free)
+		free_task(tsk);
 }
 EXPORT_SYMBOL_GPL(__put_task_struct);
 
@@ -1770,6 +1781,7 @@ static int copy_signal(u64 clone_flags, struct task_struct *tsk)
 	atomic_set(&sig->live, 1);
 	refcount_set(&sig->sigcnt, 1);
 
+	RCU_INIT_POINTER(sig->group_leader, tsk);
 	/* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
 	sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
 	tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
-- 
2.55.0




  parent reply	other threads:[~2026-12-06 15:59 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 15:35   ` Oleg Nesterov
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:36   ` sashiko-bot
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
2026-12-05 13:12 ` [PATCH v2 5/8] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-05 13:12 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:40   ` sashiko-bot
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 15:29   ` Oleg Nesterov
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 15:24   ` Oleg Nesterov
2026-12-06 11:00     ` Oleg Nesterov
2026-12-06 12:34       ` Chris Mason
2026-12-06 14:39         ` Oleg Nesterov
2026-12-06 15:09           ` Chris Mason
2026-12-05 15:59 ` [PATCH] change next_thread() to use list_next_entry_circular() Oleg Nesterov
2026-12-05 16:11   ` sashiko-bot
2026-12-05 16:19   ` Oleg Nesterov
2026-12-05 17:46   ` Chris Mason
2026-12-05 18:17     ` Oleg Nesterov
2026-12-06 15:58 ` Oleg Nesterov [this message]
2026-12-06 16:23   ` [PATCH] introduce signal_struct->group_leader sashiko-bot

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=axWGOnzsTXMjcyRj@redhat.com \
    --to=oleg@redhat.com \
    --cc=Felix.Kuehling@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsingharora@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=yonghong.song@linux.dev \
    /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