Skip to content

Commit 234a503

Browse files
willdeaconPeter Zijlstra
authored andcommitted
sched: Reject CPU affinity changes based on task_cpu_possible_mask()
Reject explicit requests to change the affinity mask of a task via set_cpus_allowed_ptr() if the requested mask is not a subset of the mask returned by task_cpu_possible_mask(). This ensures that the 'cpus_mask' for a given task cannot contain CPUs which are incapable of executing it, except in cases where the affinity is forced. Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Valentin Schneider <[email protected]> Reviewed-by: Quentin Perret <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 97c0054 commit 234a503

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

kernel/sched/core.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,9 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
27092709
const struct cpumask *new_mask,
27102710
u32 flags)
27112711
{
2712+
const struct cpumask *cpu_allowed_mask = task_cpu_possible_mask(p);
27122713
const struct cpumask *cpu_valid_mask = cpu_active_mask;
2714+
bool kthread = p->flags & PF_KTHREAD;
27132715
unsigned int dest_cpu;
27142716
struct rq_flags rf;
27152717
struct rq *rq;
@@ -2718,7 +2720,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
27182720
rq = task_rq_lock(p, &rf);
27192721
update_rq_clock(rq);
27202722

2721-
if (p->flags & PF_KTHREAD || is_migration_disabled(p)) {
2723+
if (kthread || is_migration_disabled(p)) {
27222724
/*
27232725
* Kernel threads are allowed on online && !active CPUs,
27242726
* however, during cpu-hot-unplug, even these might get pushed
@@ -2732,6 +2734,11 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
27322734
cpu_valid_mask = cpu_online_mask;
27332735
}
27342736

2737+
if (!kthread && !cpumask_subset(new_mask, cpu_allowed_mask)) {
2738+
ret = -EINVAL;
2739+
goto out;
2740+
}
2741+
27352742
/*
27362743
* Must re-check here, to close a race against __kthread_bind(),
27372744
* sched_setaffinity() is not guaranteed to observe the flag.

0 commit comments

Comments
 (0)