[tip: sched/urgent] sched/core: Introduce __set_cpus_allowed_force()

From: tip-bot2 for Peter Zijlstra

Date: Fri Dec 04 2026 - 04:34:28 EST


The following commit has been merged into the sched/urgent branch of tip:

Commit-ID: e4ab146fb64a3ed31401987ab87f519151223479
Gitweb: https://git.kernel.org/tip/e4ab146fb64a3ed31401987ab87f519151223479
Author: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
AuthorDate: Tue, 09 Sep 2025 17:42:51 +02:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Fri, 04 Dec 2026 09:38:19 +01:00

sched/core: Introduce __set_cpus_allowed_force()

Create __set_cpus_allowed_force() in preparation for __select_fallback_rq(),
such that it can be used while already holding rq->lock.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
include/linux/sched.h | 1 +
kernel/sched/core.c | 14 +++++++++++---
2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323..adde124 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1934,6 +1934,7 @@ extern void dl_bw_free(int cpu, u64 dl_bw);

/* set_cpus_allowed_force() - consider using set_cpus_allowed_ptr() instead */
extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);
+extern void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);

/**
* set_cpus_allowed_ptr - set CPU affinity mask of a task
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8f5e15a..6db2ad9 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2866,6 +2866,9 @@ void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx
static void
do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)
{
+ lockdep_assert_held(&p->pi_lock);
+ lockdep_assert_rq_held(task_rq(p));
+
scoped_guard (sched_change, p, DEQUEUE_SAVE)
p->sched_class->set_cpus_allowed(p, ctx);
}
@@ -2874,7 +2877,7 @@ do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)
* Used for kthread_bind() and select_fallback_rq(), in both cases the user
* affinity (if any) should be destroyed too.
*/
-void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
{
struct affinity_context ac = {
.new_mask = new_mask,
@@ -2886,8 +2889,7 @@ void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mas
struct rcu_head rcu;
};

- scoped_guard (__task_rq_lock, p)
- do_set_cpus_allowed(p, &ac);
+ do_set_cpus_allowed(p, &ac);

/*
* Because this is called with p->pi_lock held, it is not possible
@@ -2897,6 +2899,12 @@ void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mas
kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
}

+void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+{
+ guard(__task_rq_lock)(p);
+ __set_cpus_allowed_force(p, new_mask);
+}
+
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
int node)
{