All of 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, 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: Re: [RFC PATCH v2 8/8] signal: move group_leader into signal_struct
Date: Sat, 5 Dec 2026 16:24:55 +0100	[thread overview]
Message-ID: <axQsx5oQQZVvd39o@redhat.com> (raw)
In-Reply-To: <20261204233428.14861-9-mason@kernel.org>

Oh, I'll _try_ to read this series tomorrow... Just one question
for now.

On 12/05, Chris Mason wrote:
>
> release_task() makes a thread look like its own group leader to BPF
> programs.
>
> All threads of a process have the same leader, but each task_struct has its
> own pointer to it. The leader can be freed while somebody still holds the
> thread, so release_task() has to reset the thread's pointer.
>
> The fix used here is to share one pointer in signal_struct, as Oleg
> Nesterov suggested, and to point it at init_task when the leader's last
> reference is dropped. A task now keeps its signal_struct until it's freed.
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This looks confusing, a task alredy keeps its signal_struct until it's freed?

> +void __put_task_struct(struct task_struct *tsk)
>  {
> -	struct task_struct *task = container_of(rhp, struct task_struct, rcu);
> +	struct signal_struct *sig = tsk->signal;
> +
> +	/*
> +	 * Somebody can still hold another thread of the group, and follow the
> +	 * pointer under RCU. So reset it a grace period before the leader is
> +	 * freed. It can't be NULL, as BPF programs don't check for that.
> +	 */
> +	if (READ_ONCE(sig->group_leader) == tsk)
> +		WRITE_ONCE(sig->group_leader, &init_task);

Yes, but perhaps we can avoid this? I am not sure, but I was thinking
about _something_ like below.

If this can work then bacct_add_tsk() can just do

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

without pid_alive() check.

Oleg.

--- x/kernel/fork.c
+++ x/kernel/fork.c
@@ -773,10 +773,15 @@ static inline void free_signal_struct(st
 	kmem_cache_free(signal_cachep, sig);
 }
 
-static inline void put_signal_struct(struct signal_struct *sig)
+static struct task_struct *put_signal_struct(struct signal_struct *sig)
 {
-	if (refcount_dec_and_test(&sig->sigcnt))
+	struct task_struct *leader = NULL;
+	if (refcount_dec_and_test(&sig->sigcnt)) {
+		leader = sig->group_leader;
 		free_signal_struct(sig);
+		free_task(leader);
+	}
+	return leader;
 }
 
 void __put_task_struct(struct task_struct *tsk)
@@ -792,9 +797,10 @@ void __put_task_struct(struct task_struc
 	security_task_free(tsk);
 	exit_creds(tsk);
 	delayacct_tsk_free(tsk);
-	put_signal_struct(tsk->signal);
 	sched_core_free(tsk);
-	free_task(tsk);
+	leader = put_signal_struct(tsk->signal);
+	if (tsk != leader);
+		free_task(tsk);
 }
 EXPORT_SYMBOL_GPL(__put_task_struct);
 



  parent reply	other threads:[~2026-12-05 15:25 UTC|newest]

Thread overview: 21+ 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 [this message]
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

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=axQsx5oQQZVvd39o@redhat.com \
    --to=oleg@redhat.com \
    --cc=akpm@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.