Skip to content

Commit 0d032a4

Browse files
KAGA-KOKOPeter Zijlstra
authored andcommitted
sched/mmcid: Prevent pointless work in mm_update_cpus_allowed()
mm_update_cpus_allowed() is not required to be invoked for affinity changes due to migrate_disable() and migrate_enable(). migrate_disable() restricts the task temporarily to a CPU on which the task was already allowed to run, so nothing changes. migrate_enable() restores the actual task affinity mask. If that mask changed between migrate_disable() and migrate_enable() then that change was already accounted for. Move the invocation to the proper place to avoid that. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://patch.msgid.link/20251119172549.385208276@linutronix.de
1 parent b08ef5f commit 0d032a4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

kernel/sched/core.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,7 @@ void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx
26842684

26852685
cpumask_copy(&p->cpus_mask, ctx->new_mask);
26862686
p->nr_cpus_allowed = cpumask_weight(ctx->new_mask);
2687+
mm_update_cpus_allowed(p->mm, ctx->new_mask);
26872688

26882689
/*
26892690
* Swap in a new user_cpus_ptr if SCA_USER flag set
@@ -2730,7 +2731,6 @@ __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)
27302731
put_prev_task(rq, p);
27312732

27322733
p->sched_class->set_cpus_allowed(p, ctx);
2733-
mm_update_cpus_allowed(p->mm, ctx->new_mask);
27342734

27352735
if (queued)
27362736
enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
@@ -10376,12 +10376,17 @@ void call_trace_sched_update_nr_running(struct rq *rq, int count)
1037610376
*/
1037710377
static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
1037810378
{
10379-
struct cpumask *mm_allowed = mm_cpus_allowed(mm);
10379+
struct cpumask *mm_allowed;
1038010380

1038110381
if (!mm)
1038210382
return;
10383-
/* The mm_cpus_allowed is the union of each thread allowed CPUs masks. */
10383+
10384+
/*
10385+
* mm::mm_cid::mm_cpus_allowed is the superset of each threads
10386+
* allowed CPUs mask which means it can only grow.
10387+
*/
1038410388
guard(raw_spinlock)(&mm->mm_cid.lock);
10389+
mm_allowed = mm_cpus_allowed(mm);
1038510390
cpumask_or(mm_allowed, mm_allowed, affmsk);
1038610391
WRITE_ONCE(mm->mm_cid.nr_cpus_allowed, cpumask_weight(mm_allowed));
1038710392
}

0 commit comments

Comments
 (0)