Skip to content

Commit 14a40ff

Browse files
committed
sched: replace PF_THREAD_BOUND with PF_NO_SETAFFINITY
PF_THREAD_BOUND was originally used to mark kernel threads which were bound to a specific CPU using kthread_bind() and a task with the flag set allows cpus_allowed modifications only to itself. Workqueue is currently abusing it to prevent userland from meddling with cpus_allowed of workqueue workers. What we need is a flag to prevent userland from messing with cpus_allowed of certain kernel tasks. In kernel, anyone can (incorrectly) squash the flag, and, for worker-type usages, restricting cpus_allowed modification to the task itself doesn't provide meaningful extra proection as other tasks can inject work items to the task anyway. This patch replaces PF_THREAD_BOUND with PF_NO_SETAFFINITY. sched_setaffinity() checks the flag and return -EINVAL if set. set_cpus_allowed_ptr() is no longer affected by the flag. This will allow simplifying workqueue worker CPU affinity management. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Lai Jiangshan <laijs@cn.fujitsu.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de>
1 parent 2e109a2 commit 14a40ff

6 files changed

Lines changed: 19 additions & 24 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut,
17931793
#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
17941794
#define PF_SPREAD_PAGE 0x01000000 /* Spread page cache over cpuset */
17951795
#define PF_SPREAD_SLAB 0x02000000 /* Spread some slab caches over cpuset */
1796-
#define PF_THREAD_BOUND 0x04000000 /* Thread bound to specific cpu */
1796+
#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
17971797
#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
17981798
#define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */
17991799
#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */

‎kernel/cgroup.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,11 +2224,11 @@ static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
22242224
tsk = tsk->group_leader;
22252225

22262226
/*
2227-
* Workqueue threads may acquire PF_THREAD_BOUND and become
2227+
* Workqueue threads may acquire PF_NO_SETAFFINITY and become
22282228
* trapped in a cpuset, or RT worker may be born in a cgroup
22292229
* with no rt_runtime allocated. Just say no.
22302230
*/
2231-
if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2231+
if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
22322232
ret = -EINVAL;
22332233
rcu_read_unlock();
22342234
goto out_unlock_cgroup;

‎kernel/cpuset.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,16 +1388,16 @@ static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
13881388

13891389
cgroup_taskset_for_each(task, cgrp, tset) {
13901390
/*
1391-
* Kthreads bound to specific cpus cannot be moved to a new
1392-
* cpuset; we cannot change their cpu affinity and
1393-
* isolating such threads by their set of allowed nodes is
1394-
* unnecessary. Thus, cpusets are not applicable for such
1395-
* threads. This prevents checking for success of
1396-
* set_cpus_allowed_ptr() on all attached tasks before
1397-
* cpus_allowed may be changed.
1391+
* Kthreads which disallow setaffinity shouldn't be moved
1392+
* to a new cpuset; we don't want to change their cpu
1393+
* affinity and isolating such threads by their set of
1394+
* allowed nodes is unnecessary. Thus, cpusets are not
1395+
* applicable for such threads. This prevents checking for
1396+
* success of set_cpus_allowed_ptr() on all attached tasks
1397+
* before cpus_allowed may be changed.
13981398
*/
13991399
ret = -EINVAL;
1400-
if (task->flags & PF_THREAD_BOUND)
1400+
if (task->flags & PF_NO_SETAFFINITY)
14011401
goto out_unlock;
14021402
ret = security_task_setscheduler(task);
14031403
if (ret)

‎kernel/kthread.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static void __kthread_bind(struct task_struct *p, unsigned int cpu)
260260
{
261261
/* It's safe because the task is inactive. */
262262
do_set_cpus_allowed(p, cpumask_of(cpu));
263-
p->flags |= PF_THREAD_BOUND;
263+
p->flags |= PF_NO_SETAFFINITY;
264264
}
265265

266266
/**

‎kernel/sched/core.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,6 +4126,10 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
41264126
get_task_struct(p);
41274127
rcu_read_unlock();
41284128

4129+
if (p->flags & PF_NO_SETAFFINITY) {
4130+
retval = -EINVAL;
4131+
goto out_put_task;
4132+
}
41294133
if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
41304134
retval = -ENOMEM;
41314135
goto out_put_task;
@@ -4773,11 +4777,6 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
47734777
goto out;
47744778
}
47754779

4776-
if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) {
4777-
ret = -EINVAL;
4778-
goto out;
4779-
}
4780-
47814780
do_set_cpus_allowed(p, new_mask);
47824781

47834782
/* Can the task run on the task's current CPU? If so, we're done */

‎kernel/workqueue.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,12 +1757,8 @@ static struct worker *create_worker(struct worker_pool *pool)
17571757
set_user_nice(worker->task, pool->attrs->nice);
17581758
set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
17591759

1760-
/*
1761-
* %PF_THREAD_BOUND is used to prevent userland from meddling with
1762-
* cpumask of workqueue workers. This is an abuse. We need
1763-
* %PF_NO_SETAFFINITY.
1764-
*/
1765-
worker->task->flags |= PF_THREAD_BOUND;
1760+
/* prevent userland from meddling with cpumask of workqueue workers */
1761+
worker->task->flags |= PF_NO_SETAFFINITY;
17661762

17671763
/*
17681764
* The caller is responsible for ensuring %POOL_DISASSOCIATED
@@ -3876,7 +3872,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
38763872
}
38773873

38783874
wq->rescuer = rescuer;
3879-
rescuer->task->flags |= PF_THREAD_BOUND;
3875+
rescuer->task->flags |= PF_NO_SETAFFINITY;
38803876
wake_up_process(rescuer->task);
38813877
}
38823878

0 commit comments

Comments
 (0)