Linux cgroups development
 help / color / mirror / Atom feed
From: "Chris Mason" <mason@kernel.org>
To: brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: 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, mason@kernel.org
Subject: [RFC PATCH v2 7/8] signal: add task_group_leader()
Date: Sat, 05 Dec 2026 08:13:02 -0500	[thread overview]
Message-ID: <20261204233428.14861-8-mason@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>

This is a prep patch that allows us to move group_leader into
signal_struct. As Oleg Nesterov suggested, it adds a helper to read the
field, which warns if another process's leader is read without RCU or
tasklist_lock.

cgroup, cpuset, Yama and current_is_single_threaded() now use
thread_group_leader() or same_thread_group() instead of comparing with the
leader.

Signed-off-by: Chris Mason <mason@kernel.org>
---
 arch/x86/kernel/fpu/core.c               |  4 ++--
 arch/x86/kernel/fpu/xstate.c             |  6 +++---
 arch/x86/kernel/fpu/xstate.h             |  4 +++-
 arch/x86/kernel/signal.c                 |  4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c   |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c |  4 ++--
 drivers/gpu/drm/amd/amdkfd/kfd_process.c |  2 +-
 drivers/gpu/drm/panfrost/panfrost_gem.c  |  2 +-
 drivers/gpu/drm/panthor/panthor_gem.c    |  2 +-
 drivers/gpu/drm/panthor/panthor_sched.c  |  8 ++++----
 drivers/gpu/drm/scheduler/sched_entity.c |  9 +++++----
 drivers/iommu/iommufd/ioas.c             |  4 ++--
 drivers/iommu/iommufd/pages.c            |  4 ++--
 drivers/vfio/vfio_iommu_type1.c          |  6 +++---
 fs/binfmt_elf.c                          |  2 +-
 fs/binfmt_elf_fdpic.c                    |  2 +-
 fs/exec.c                                | 10 +++++-----
 include/linux/cgroup.h                   |  2 +-
 include/linux/sched/signal.h             | 21 ++++++++++++++++++---
 kernel/acct.c                            |  2 +-
 kernel/bpf/task_iter.c                   |  4 ++--
 kernel/cgroup/cgroup.c                   |  2 +-
 kernel/cgroup/cpuset.c                   |  4 ++--
 kernel/debug/kdb/kdb_main.c              |  2 +-
 kernel/exit.c                            |  4 ++--
 kernel/fork.c                            | 10 +++++-----
 kernel/signal.c                          |  6 +++---
 kernel/sys.c                             | 20 ++++++++++----------
 kernel/tsacct.c                          |  2 +-
 lib/is_single_threaded.c                 |  2 +-
 rust/helpers/task.c                      |  7 +++++++
 rust/kernel/task.rs                      |  9 +++++----
 security/apparmor/resource.c             |  2 +-
 security/yama/yama_lsm.c                 | 10 ++++------
 35 files changed, 105 insertions(+), 81 deletions(-)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 80a7f41cf3ad9..edbb18810d720 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -243,7 +243,7 @@ static void fpu_lock_guest_permissions(void)
 		return;
 
 	spin_lock_irq(&current->sighand->siglock);
-	fpuperm = &x86_task_fpu(current->group_leader)->guest_perm;
+	fpuperm = &x86_task_fpu(task_group_leader(current))->guest_perm;
 	perm = fpuperm->__state_perm;
 
 	/* First fpstate allocation locks down permissions. */
@@ -636,7 +636,7 @@ void fpstate_reset(struct fpu *fpu)
 static inline void fpu_inherit_perms(struct fpu *dst_fpu)
 {
 	if (fpu_state_size_dynamic()) {
-		struct fpu *src_fpu = x86_task_fpu(current->group_leader);
+		struct fpu *src_fpu = x86_task_fpu(task_group_leader(current));
 
 		spin_lock_irq(&current->sighand->siglock);
 		/* Fork also inherits the permissions of the parent */
diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index da303714379cd..4c805e09225c7 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1689,7 +1689,7 @@ static int fpstate_realloc(u64 xfeatures, unsigned int ksize,
 
 static int validate_sigaltstack(unsigned int usize)
 {
-	struct task_struct *thread, *leader = current->group_leader;
+	struct task_struct *thread, *leader = task_group_leader(current);
 	unsigned long framesize = get_sigframe_size();
 
 	lockdep_assert_held(&current->sighand->siglock);
@@ -1713,7 +1713,7 @@ static int __xstate_request_perm(u64 permitted, u64 requested, bool guest)
 	 * AVX512.
 	 */
 	bool compacted = cpu_feature_enabled(X86_FEATURE_XCOMPACTED);
-	struct fpu *fpu = x86_task_fpu(current->group_leader);
+	struct fpu *fpu = x86_task_fpu(task_group_leader(current));
 	struct fpu_state_perm *perm;
 	unsigned int ksize, usize;
 	u64 mask;
@@ -1820,7 +1820,7 @@ int __xfd_enable_feature(u64 xfd_err, struct fpu_guest *guest_fpu)
 		return -EPERM;
 	}
 
-	fpu = x86_task_fpu(current->group_leader);
+	fpu = x86_task_fpu(task_group_leader(current));
 	perm = guest_fpu ? &fpu->guest_perm : &fpu->perm;
 	ksize = perm->__state_size;
 	usize = perm->__user_state_size;
diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h
index 1ea762e930866..676f4db6caf63 100644
--- a/arch/x86/kernel/fpu/xstate.h
+++ b/arch/x86/kernel/fpu/xstate.h
@@ -2,6 +2,8 @@
 #ifndef __X86_KERNEL_FPU_XSTATE_H
 #define __X86_KERNEL_FPU_XSTATE_H
 
+#include <linux/sched/signal.h>
+
 #include <asm/cpufeature.h>
 #include <asm/fpu/xstate.h>
 #include <asm/fpu/xcr.h>
@@ -23,7 +25,7 @@ static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
 
 static inline u64 xstate_get_group_perm(bool guest)
 {
-	struct fpu *fpu = x86_task_fpu(current->group_leader);
+	struct fpu *fpu = x86_task_fpu(task_group_leader(current));
 	struct fpu_state_perm *perm;
 
 	/* Pairs with WRITE_ONCE() in xstate_request_perm() */
diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c
index 2404233336ab6..4fdc5b9af9916 100644
--- a/arch/x86/kernel/signal.c
+++ b/arch/x86/kernel/signal.c
@@ -423,14 +423,14 @@ bool sigaltstack_size_valid(size_t ss_size)
 	if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
 		return true;
 
-	fsize += x86_task_fpu(current->group_leader)->perm.__user_state_size;
+	fsize += x86_task_fpu(task_group_leader(current))->perm.__user_state_size;
 	if (likely(ss_size > fsize))
 		return true;
 
 	if (strict_sigaltstack_size)
 		return ss_size > fsize;
 
-	mask = x86_task_fpu(current->group_leader)->perm.__state_perm;
+	mask = x86_task_fpu(task_group_leader(current))->perm.__state_perm;
 	if (mask & XFEATURE_MASK_USER_DYNAMIC)
 		return ss_size > fsize;
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 326abc9b7f690..ea872eed0fcfd 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -1362,7 +1362,7 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
 
 	if (gtt->usertask)
 		put_task_struct(gtt->usertask);
-	gtt->usertask = current->group_leader;
+	gtt->usertask = task_group_leader(current);
 	get_task_struct(gtt->usertask);
 
 	return 0;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index 45f0f6aca754c..e9d64fc337d91 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -2595,7 +2595,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
 	get_task_comm(vm->task_info->task.comm, current);
 
 	vm->task_info->tgid = current->tgid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
+	get_task_comm(vm->task_info->process_name, task_group_leader(current));
 }
 
 /**
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index a46a4e498ac51..cab536027ec16 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -3670,7 +3670,7 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		ptrace_attached = true;
 	rcu_read_unlock();
 
-	if (process->lead_thread != current->group_leader
+	if (process->lead_thread != task_group_leader(current)
 	    && !ptrace_attached) {
 		dev_dbg(kfd_device, "Using KFD FD in wrong process\n");
 		retcode = -EBADF;
@@ -3794,7 +3794,7 @@ static int kfd_mmap(struct file *filep, struct vm_area_struct *vma)
 	if (!process)
 		return -ESRCH;
 
-	if (process->lead_thread != current->group_leader)
+	if (process->lead_thread != task_group_leader(current))
 		return -EBADF;
 
 	mmap_offset = vma->vm_pgoff << PAGE_SHIFT;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 458637a81f66c..1a72d9a206038 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1701,7 +1701,7 @@ struct kfd_process *create_process(const struct task_struct *thread,
 	 */
 	rcu_read_lock();
 	if (pid_alive(thread))
-		process->lead_thread = get_task_struct(thread->group_leader);
+		process->lead_thread = get_task_struct(task_group_leader(thread));
 	rcu_read_unlock();
 	if (!process->lead_thread) {
 		err = -ESRCH;
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index 518f24549b9de..219f94540068c 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -48,7 +48,7 @@ static void panfrost_gem_debugfs_bo_add(struct panfrost_device *pfdev,
 					struct panfrost_gem_object *bo)
 {
 	bo->debugfs.creator.tgid = current->tgid;
-	get_task_comm(bo->debugfs.creator.process_name, current->group_leader);
+	get_task_comm(bo->debugfs.creator.process_name, task_group_leader(current));
 
 	mutex_lock(&pfdev->debugfs.gems_lock);
 	list_add_tail(&bo->debugfs.node, &pfdev->debugfs.gems_list);
diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c
index 22b57ab6f9291..df3119b5fb39b 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.c
+++ b/drivers/gpu/drm/panthor/panthor_gem.c
@@ -54,7 +54,7 @@ static void panthor_gem_debugfs_bo_add(struct panthor_gem_object *bo)
 						    struct panthor_device, base);
 
 	bo->debugfs.creator.tgid = current->tgid;
-	get_task_comm(bo->debugfs.creator.process_name, current->group_leader);
+	get_task_comm(bo->debugfs.creator.process_name, task_group_leader(current));
 
 	mutex_lock(&ptdev->gems.lock);
 	list_add_tail(&bo->debugfs.node, &ptdev->gems.node);
diff --git a/drivers/gpu/drm/panthor/panthor_sched.c b/drivers/gpu/drm/panthor/panthor_sched.c
index a343c5180e01e..0fd46bf8a6531 100644
--- a/drivers/gpu/drm/panthor/panthor_sched.c
+++ b/drivers/gpu/drm/panthor/panthor_sched.c
@@ -647,12 +647,12 @@ struct panthor_group {
 		size_t kbo_sizes;
 	} fdinfo;
 
-	/** @task_info: Info of current->group_leader that created the group. */
+	/** @task_info: Info of the process that created the group. */
 	struct {
-		/** @task_info.pid: pid of current->group_leader */
+		/** @task_info.pid: pid of its thread group leader */
 		pid_t pid;
 
-		/** @task_info.comm: comm of current->group_leader */
+		/** @task_info.comm: comm of its thread group leader */
 		char comm[TASK_COMM_LEN];
 	} task_info;
 
@@ -3592,7 +3592,7 @@ group_create_queue(struct panthor_group *group,
 
 static void group_init_task_info(struct panthor_group *group)
 {
-	struct task_struct *task = current->group_leader;
+	struct task_struct *task = task_group_leader(current);
 
 	group->task_info.pid = task->pid;
 	get_task_comm(group->task_info.comm, task);
diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
index 03e68b4102d50..7e6227803226a 100644
--- a/drivers/gpu/drm/scheduler/sched_entity.c
+++ b/drivers/gpu/drm/scheduler/sched_entity.c
@@ -24,6 +24,7 @@
 #include <linux/export.h>
 #include <linux/slab.h>
 #include <linux/completion.h>
+#include <linux/sched/signal.h>
 
 #include <drm/drm_print.h>
 #include <drm/gpu_scheduler.h>
@@ -130,7 +131,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 
 	INIT_LIST_HEAD(&entity->list);
 	entity->guilty = guilty;
-	entity->last_user = current->group_leader;
+	entity->last_user = task_group_leader(current);
 	entity->num_sched_list = num_sched_list;
 	entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
 	RB_CLEAR_NODE(&entity->rb_tree_node);
@@ -374,8 +375,8 @@ long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
 	}
 
 	/* For a killed process disallow further enqueueing of jobs. */
-	last_user = cmpxchg(&entity->last_user, current->group_leader, NULL);
-	if (last_user == current->group_leader &&
+	last_user = cmpxchg(&entity->last_user, task_group_leader(current), NULL);
+	if (last_user == task_group_leader(current) &&
 	    (current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
 		drm_sched_entity_kill(entity);
 
@@ -632,7 +633,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 			trace_drm_sched_job_add_dep(sched_job, entry);
 	}
 	atomic_inc(entity->rq->sched->score);
-	WRITE_ONCE(entity->last_user, current->group_leader);
+	WRITE_ONCE(entity->last_user, task_group_leader(current));
 
 	/*
 	 * After the sched_job is pushed into the entity queue, it may be
diff --git a/drivers/iommu/iommufd/ioas.c b/drivers/iommu/iommufd/ioas.c
index 8fb5271a95060..8a163ddb7ed24 100644
--- a/drivers/iommu/iommufd/ioas.c
+++ b/drivers/iommu/iommufd/ioas.c
@@ -497,7 +497,7 @@ static int charge_current(unsigned long *npinned)
 {
 	struct iopt_pages tmp = {
 		.source_mm = current->mm,
-		.source_task = current->group_leader,
+		.source_task = task_group_leader(current),
 		.source_user = current_user(),
 	};
 	unsigned int account_mode;
@@ -538,7 +538,7 @@ static void change_mm(struct iopt_pages *pages)
 	mmgrab(pages->source_mm);
 	mmdrop(old_mm);
 
-	pages->source_task = current->group_leader;
+	pages->source_task = task_group_leader(current);
 	get_task_struct(pages->source_task);
 	put_task_struct(old_task);
 
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index fcdbde007a9ab..ff1b8cd85a22b 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -1401,8 +1401,8 @@ static struct iopt_pages *iopt_alloc_pages(unsigned long start_byte,
 		pages->account_mode = IOPT_PAGES_ACCOUNT_NONE;
 	else
 		pages->account_mode = IOPT_PAGES_ACCOUNT_USER;
-	pages->source_task = current->group_leader;
-	get_task_struct(current->group_leader);
+	pages->source_task = task_group_leader(current);
+	get_task_struct(task_group_leader(current));
 	pages->source_user = get_uid(current_user());
 	return pages;
 }
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index f7addfbe1aab4..83929ff28c0d7 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -1651,7 +1651,7 @@ static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu,
 
 static int vfio_change_dma_owner(struct vfio_dma *dma)
 {
-	struct task_struct *task = current->group_leader;
+	struct task_struct *task = task_group_leader(current);
 	struct mm_struct *mm = current->mm;
 	long npage = dma->locked_vm;
 	bool lock_cap;
@@ -1776,8 +1776,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 	 * the same task, to make debugging easier.  VM locked pages requires
 	 * an mm_struct, so grab the mm in case the task dies.
 	 */
-	get_task_struct(current->group_leader);
-	dma->task = current->group_leader;
+	get_task_struct(task_group_leader(current));
+	dma->task = task_group_leader(current);
 	dma->lock_cap = capable(CAP_IPC_LOCK);
 	dma->mm = current->mm;
 	mmgrab(dma->mm);
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index bf7f8f47548d3..5e96c9a7bfa6f 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1895,7 +1895,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
 	/*
 	 * Fill in the two process-wide notes.
 	 */
-	fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
+	fill_psinfo(psinfo, task_group_leader(dump_task), dump_task->mm);
 	info->size += notesize(&info->psinfo);
 
 	fill_siginfo_note(&info->signote, &info->csigdata, cprm->siginfo);
diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index d3872169f55e5..e4e43b1a5e738 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -1542,7 +1542,7 @@ static bool elf_fdpic_core_dump(struct coredump_params *cprm)
 	 * with info from their /proc.
 	 */
 
-	fill_psinfo(psinfo, current->group_leader, current->mm);
+	fill_psinfo(psinfo, task_group_leader(current), current->mm);
 	fill_note(&psinfo_note, PRPSINFO, sizeof(*psinfo), psinfo);
 	thread_status_size += notesize(&psinfo_note);
 
diff --git a/fs/exec.c b/fs/exec.c
index b3b80a47fb657..3bae1a6dc62c3 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -269,9 +269,9 @@ static int bprm_mm_init(struct linux_binprm *bprm)
 	bprm->user_ns = get_user_ns(current_user_ns());
 
 	/* Save current stack limit for all calculations made during exec. */
-	task_lock(current->group_leader);
+	task_lock(task_group_leader(current));
 	bprm->rlim_stack = current->signal->rlim[RLIMIT_STACK];
-	task_unlock(current->group_leader);
+	task_unlock(task_group_leader(current));
 
 #ifndef CONFIG_MMU
 	bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
@@ -963,7 +963,7 @@ static int de_thread(struct task_struct *tsk)
 	 * and to assume its PID:
 	 */
 	if (!thread_group_leader(tsk)) {
-		struct task_struct *leader = tsk->group_leader;
+		struct task_struct *leader = task_group_leader(tsk);
 
 		for (;;) {
 			cgroup_threadgroup_change_begin(tsk);
@@ -1402,9 +1402,9 @@ EXPORT_SYMBOL(setup_new_exec);
 void finalize_exec(struct linux_binprm *bprm)
 {
 	/* Store any stack rlimit changes before starting thread. */
-	task_lock(current->group_leader);
+	task_lock(task_group_leader(current));
 	current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack;
-	task_unlock(current->group_leader);
+	task_unlock(task_group_leader(current));
 }
 EXPORT_SYMBOL(finalize_exec);
 
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 2afb4cb2bb4fa..0a1a5b09d0d50 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -377,7 +377,7 @@ void css_task_iter_end(struct css_task_iter *it);
 	for ((leader) = cgroup_taskset_first((tset), &(dst_css));	\
 	     (leader);							\
 	     (leader) = cgroup_taskset_next((tset), &(dst_css)))	\
-		if ((leader) != (leader)->group_leader)			\
+		if (!thread_group_leader(leader))			\
 			;						\
 		else
 
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index cf73f4dd6946a..802e264c323d3 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -211,8 +211,8 @@ struct signal_struct {
 	 * because there is no reader checking a limit that actually needs
 	 * to get both rlim_cur and rlim_max atomically, and either one
 	 * alone is a single word that can safely be read normally.
-	 * getrlimit/setrlimit use task_lock(current->group_leader) to
-	 * protect this instead of the siglock, because they really
+	 * getrlimit/setrlimit use task_lock(task_group_leader(current))
+	 * to protect this instead of the siglock, because they really
 	 * have no need to disable irqs.
 	 */
 	struct rlimit rlim[RLIM_NLIMITS];
@@ -745,6 +745,21 @@ static inline bool thread_group_leader(struct task_struct *p)
 	return p->exit_signal >= 0;
 }
 
+/*
+ * Another process's leader can exit, or be replaced by exec, at any time. So
+ * hold RCU or tasklist_lock while you use it, and check pid_alive(@p) before
+ * you take a reference on it.
+ */
+static inline struct task_struct *task_group_leader(const struct task_struct *p)
+{
+	RCU_LOCKDEP_WARN(p->signal != current->signal &&
+			 !rcu_read_lock_any_held() &&
+			 !lockdep_tasklist_lock_is_held(),
+			 "another process's leader needs RCU or tasklist_lock");
+
+	return READ_ONCE(p->group_leader);
+}
+
 static inline
 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
 {
@@ -764,7 +779,7 @@ static inline struct task_struct *__next_thread(struct task_struct *p)
 
 static inline struct task_struct *next_thread(struct task_struct *p)
 {
-	return __next_thread(p) ?: p->group_leader;
+	return __next_thread(p) ?: task_group_leader(p);
 }
 
 static inline int thread_group_empty(struct task_struct *p)
diff --git a/kernel/acct.c b/kernel/acct.c
index c440d43479ca5..710ae0e65555a 100644
--- a/kernel/acct.c
+++ b/kernel/acct.c
@@ -459,7 +459,7 @@ static void fill_ac(struct bsd_acct_struct *acct)
 
 	/* calculate run_time in nsec*/
 	run_time = ktime_get_ns();
-	run_time -= current->group_leader->start_time;
+	run_time -= task_group_leader(current)->start_time;
 	/* convert nsec -> AHZ */
 	elapsed = nsec_to_AHZ(run_time);
 #if ACCT_VERSION == 3
diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index a5f0849500715..b17c5f9a50d4e 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -79,7 +79,7 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm
 	if (!next_tid)
 		goto retry;
 
-	if (skip_if_dup_files && task->files == task->group_leader->files)
+	if (skip_if_dup_files && task->files == task_group_leader(task)->files)
 		goto retry;
 
 	*tid = common->pid_visiting = next_tid;
@@ -126,7 +126,7 @@ static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *co
 			++*tid;
 			goto retry;
 		} else if (skip_if_dup_files && !thread_group_leader(task) &&
-			   task->files == task->group_leader->files) {
+			   task->files == task_group_leader(task)->files) {
 			put_task_struct(task);
 			task = NULL;
 			++*tid;
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 640af2d4551d1..61f88297de431 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3146,7 +3146,7 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
 	}
 
 	if (threadgroup)
-		tsk = tsk->group_leader;
+		tsk = task_group_leader(tsk);
 
 	/*
 	 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index db32d330cfbc2..fbb0c1e27b348 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -3252,7 +3252,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
 			goto out_unlock;
 
 		/* Update attach_ctx.old_cs to the latest group leader */
-		if (task == task->group_leader)
+		if (thread_group_leader(task))
 			attach_ctx.old_cs = task_cs(task);
 
 		if (setsched_check) {
@@ -3343,7 +3343,7 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)
 	cpuset_change_task_nodemask(task, &attach_ctx.nodemask_to);
 	cpuset1_update_task_spread_flags(cs, task);
 
-	if ((task != task->group_leader) || !attach_ctx.mems_updated)
+	if (!thread_group_leader(task) || !attach_ctx.mems_updated)
 		return;
 
 	/*
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index 40c332d2179d8..31c592761eb56 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -2351,7 +2351,7 @@ static int kdb_kill(int argc, const char **argv)
 		kdb_printf("The specified process isn't found.\n");
 		return 0;
 	}
-	p = p->group_leader;
+	p = task_group_leader(p);
 	kdb_send_sig(p, sig);
 	return 0;
 }
diff --git a/kernel/exit.c b/kernel/exit.c
index 8a5985821e17d..5046360b8a6a4 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -272,7 +272,7 @@ void release_task(struct task_struct *p)
 	 * group leader's parent process. (if it wants notification.)
 	 */
 	zap_leader = 0;
-	leader = p->group_leader;
+	leader = task_group_leader(p);
 	if (leader != p && thread_group_empty(leader)
 			&& leader->exit_state == EXIT_ZOMBIE) {
 		/* for pidfs_exit() and do_notify_parent() */
@@ -750,7 +750,7 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
 	forget_original_parent(tsk, &dead);
 
 	if (group_dead)
-		kill_orphaned_pgrp(tsk->group_leader, NULL);
+		kill_orphaned_pgrp(task_group_leader(tsk), NULL);
 
 	tsk->exit_state = EXIT_ZOMBIE;
 
diff --git a/kernel/fork.c b/kernel/fork.c
index c99d7147f1f1d..ff0e367b2a81d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1795,9 +1795,9 @@ static int copy_signal(u64 clone_flags, struct task_struct *tsk)
 	hrtimer_setup(&sig->real_timer, it_real_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 #endif
 
-	task_lock(current->group_leader);
+	task_lock(task_group_leader(current));
 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
-	task_unlock(current->group_leader);
+	task_unlock(task_group_leader(current));
 
 	posix_cpu_timers_init_group(sig);
 
@@ -2492,7 +2492,7 @@ __latent_entropy struct task_struct *copy_process(
 		if (clone_flags & CLONE_THREAD)
 			p->exit_signal = -1;
 		else
-			p->exit_signal = current->group_leader->exit_signal;
+			p->exit_signal = task_group_leader(current)->exit_signal;
 	} else {
 		p->parent_exec_id = current->self_exec_id;
 		p->exit_signal = args->exit_signal;
@@ -3135,7 +3135,7 @@ void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data
 	int res;
 
 	read_lock(&tasklist_lock);
-	leader = top = top->group_leader;
+	leader = top = task_group_leader(top);
 down:
 	for_each_thread(leader, parent) {
 		list_for_each_entry(child, &parent->children, sibling) {
@@ -3154,7 +3154,7 @@ void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data
 	if (leader != top) {
 		child = leader;
 		parent = child->real_parent;
-		leader = parent->group_leader;
+		leader = task_group_leader(parent);
 		goto up;
 	}
 out:
diff --git a/kernel/signal.c b/kernel/signal.c
index dd770e68faee0..f5e77076ca2a8 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2366,7 +2366,7 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
 	if (for_ptracer) {
 		parent = tsk->parent;
 	} else {
-		tsk = tsk->group_leader;
+		tsk = task_group_leader(tsk);
 		parent = tsk->real_parent;
 	}
 
@@ -2929,8 +2929,8 @@ bool get_signal(struct ksignal *ksig)
 		read_lock(&tasklist_lock);
 		do_notify_parent_cldstop(current, false, why);
 
-		if (ptrace_reparented(current->group_leader))
-			do_notify_parent_cldstop(current->group_leader,
+		if (ptrace_reparented(task_group_leader(current)))
+			do_notify_parent_cldstop(task_group_leader(current),
 						true, why);
 		read_unlock(&tasklist_lock);
 
diff --git a/kernel/sys.c b/kernel/sys.c
index 9cb8eac08b7d2..bfa0a7b5420e7 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1117,7 +1117,7 @@ COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
 {
 	struct task_struct *p;
-	struct task_struct *group_leader = current->group_leader;
+	struct task_struct *group_leader = task_group_leader(current);
 	struct pid *pids[PIDTYPE_MAX] = { 0 };
 	struct pid *pgrp;
 	int err;
@@ -1259,7 +1259,7 @@ SYSCALL_DEFINE1(getsid, pid_t, pid)
 
 static void set_special_pids(struct pid **pids, struct pid *pid)
 {
-	struct task_struct *curr = current->group_leader;
+	struct task_struct *curr = task_group_leader(current);
 
 	if (task_session(curr) != pid)
 		change_pid(pids, curr, PIDTYPE_SID, pid);
@@ -1270,7 +1270,7 @@ static void set_special_pids(struct pid **pids, struct pid *pid)
 
 int ksys_setsid(void)
 {
-	struct task_struct *group_leader = current->group_leader;
+	struct task_struct *group_leader = task_group_leader(current);
 	struct pid *sid = task_pid(group_leader);
 	struct pid *pids[PIDTYPE_MAX] = { 0 };
 	pid_t session = pid_vnr(sid);
@@ -1520,7 +1520,7 @@ static int do_prlimit(struct task_struct *tsk, unsigned int resource,
 
 	/* Holding a refcount on tsk protects tsk->signal from disappearing. */
 	rlim = tsk->signal->rlim + resource;
-	task_lock(tsk->group_leader);
+	task_lock(task_group_leader(tsk));
 	if (new_rlim) {
 		/*
 		 * Keep the capable check against init_user_ns until cgroups can
@@ -1538,7 +1538,7 @@ static int do_prlimit(struct task_struct *tsk, unsigned int resource,
 		if (new_rlim)
 			*rlim = *new_rlim;
 	}
-	task_unlock(tsk->group_leader);
+	task_unlock(task_group_leader(tsk));
 
 	/*
 	 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
@@ -1557,7 +1557,7 @@ static int do_prlimit(struct task_struct *tsk, unsigned int resource,
 		 * cannot update_rlimit_cpu on it, then the entire process is
 		 * exiting and we do not need to update at all.
 		 */
-		update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
+		update_rlimit_cpu(task_group_leader(tsk), new_rlim->rlim_cur);
 	}
 
 	return retval;
@@ -1636,9 +1636,9 @@ SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
 		return -EINVAL;
 
 	resource = array_index_nospec(resource, RLIM_NLIMITS);
-	task_lock(current->group_leader);
+	task_lock(task_group_leader(current));
 	x = current->signal->rlim[resource];
-	task_unlock(current->group_leader);
+	task_unlock(task_group_leader(current));
 	if (x.rlim_cur > 0x7FFFFFFF)
 		x.rlim_cur = 0x7FFFFFFF;
 	if (x.rlim_max > 0x7FFFFFFF)
@@ -1656,9 +1656,9 @@ COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
 		return -EINVAL;
 
 	resource = array_index_nospec(resource, RLIM_NLIMITS);
-	task_lock(current->group_leader);
+	task_lock(task_group_leader(current));
 	r = current->signal->rlim[resource];
-	task_unlock(current->group_leader);
+	task_unlock(task_group_leader(current));
 	if (r.rlim_cur > 0x7FFFFFFF)
 		r.rlim_cur = 0x7FFFFFFF;
 	if (r.rlim_max > 0x7FFFFFFF)
diff --git a/kernel/tsacct.c b/kernel/tsacct.c
index d8e941674fb13..7fb6d9fff3b60 100644
--- a/kernel/tsacct.c
+++ b/kernel/tsacct.c
@@ -32,7 +32,7 @@ void bacct_add_tsk(struct user_namespace *user_ns,
 	now_ns = ktime_get_ns();
 	/* store whole group time first, or tsk's own if it's been released */
 	rcu_read_lock();
-	delta = now_ns - (pid_alive(tsk) ? tsk->group_leader : tsk)->start_time;
+	delta = now_ns - (pid_alive(tsk) ? task_group_leader(tsk) : tsk)->start_time;
 	rcu_read_unlock();
 	/* Convert to micro seconds */
 	do_div(delta, NSEC_PER_USEC);
diff --git a/lib/is_single_threaded.c b/lib/is_single_threaded.c
index 8c98b20bfc414..efe9b6b35c55c 100644
--- a/lib/is_single_threaded.c
+++ b/lib/is_single_threaded.c
@@ -30,7 +30,7 @@ bool current_is_single_threaded(void)
 	for_each_process(p) {
 		if (unlikely(p->flags & PF_KTHREAD))
 			continue;
-		if (unlikely(p == task->group_leader))
+		if (unlikely(same_thread_group(p, task)))
 			continue;
 
 		for_each_thread(p, t) {
diff --git a/rust/helpers/task.c b/rust/helpers/task.c
index b46b1433a67e8..e62daa6067038 100644
--- a/rust/helpers/task.c
+++ b/rust/helpers/task.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/kernel.h>
+#include <linux/sched/signal.h>
 #include <linux/sched/task.h>
 
 __rust_helper void rust_helper_might_resched(void)
@@ -23,6 +24,12 @@ __rust_helper void rust_helper_put_task_struct(struct task_struct *t)
 	put_task_struct(t);
 }
 
+__rust_helper struct task_struct *
+rust_helper_task_group_leader(const struct task_struct *p)
+{
+	return task_group_leader(p);
+}
+
 __rust_helper kuid_t rust_helper_task_uid(struct task_struct *task)
 {
 	return task_uid(task);
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 3336df493decc..b83971543d5c9 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -334,14 +334,15 @@ pub fn active_pid_ns(&self) -> Option<&PidNamespace> {
     }
 
     /// Returns the group leader of the current task.
+    #[inline]
     pub fn group_leader(&self) -> &Task {
         // SAFETY: The group leader of a task never changes while the task is running, and `self`
         // is the current task, which is guaranteed running.
-        let ptr = unsafe { (*self.as_ptr()).group_leader };
+        let ptr = unsafe { bindings::task_group_leader(self.as_ptr()) };
 
-        // SAFETY: `current->group_leader` stays valid for at least the duration in which `current`
-        // is running, and the signature of this function ensures that the returned `&Task` can
-        // only be used while `current` is still valid, thus still running.
+        // SAFETY: The group leader stays valid for at least the duration in which `current` is
+        // running, and the signature of this function ensures that the returned `&Task` can only be
+        // used while `current` is still valid, thus still running.
         unsafe { &*ptr.cast() }
     }
 }
diff --git a/security/apparmor/resource.c b/security/apparmor/resource.c
index 64212b39ba4bb..76892a1e66bd5 100644
--- a/security/apparmor/resource.c
+++ b/security/apparmor/resource.c
@@ -199,7 +199,7 @@ void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l)
 			if (j == RLIMIT_CPU &&
 			    rlim->rlim_cur != RLIM_INFINITY &&
 			    IS_ENABLED(CONFIG_POSIX_TIMERS))
-				(void) update_rlimit_cpu(current->group_leader,
+				(void) update_rlimit_cpu(task_group_leader(current),
 							 rlim->rlim_cur);
 		}
 	}
diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index cef3776cf3b2d..c71588e237213 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -232,7 +232,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 		 * leader checking is handled later when walking the ancestry
 		 * at the time of PTRACE_ATTACH check.
 		 */
-		myself = current->group_leader;
+		myself = task_group_leader(current);
 
 		if (arg2 == 0) {
 			yama_ptracer_del(NULL, myself);
@@ -274,12 +274,10 @@ static int task_is_descendant(struct task_struct *parent,
 		return 0;
 
 	rcu_read_lock();
-	if (!thread_group_leader(parent))
-		parent = rcu_dereference(parent->group_leader);
 	while (walker->pid > 0) {
 		if (!thread_group_leader(walker))
-			walker = rcu_dereference(walker->group_leader);
-		if (walker == parent) {
+			walker = task_group_leader(walker);
+		if (same_thread_group(walker, parent)) {
 			rc = 1;
 			break;
 		}
@@ -319,7 +317,7 @@ static int ptracer_exception_found(struct task_struct *tracer,
 
 	/* Look for a PR_SET_PTRACER relationship. */
 	if (!thread_group_leader(tracee))
-		tracee = rcu_dereference(tracee->group_leader);
+		tracee = task_group_leader(tracee);
 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
 		if (relation->invalid)
 			continue;


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

Thread overview: 9+ 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 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
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:13 ` Chris Mason [this message]
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason

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=20261204233428.14861-8-mason@kernel.org \
    --to=mason@kernel.org \
    --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=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.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