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.
Showing the changes to kernel/workqueue.c (1 of 6 files). Show all
Unified · Split
| @@ -1757,12 +1757,8 @@ static struct worker *create_worker(struct worker_pool *pool) · 1756 unmodified lines | ||
| 1757 | 1757 | set_user_nice(worker->task, pool->attrs->nice); |
| 1758 | 1758 | set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); |
| 1759 | 1759 | |
| 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; | |
| 1766 | 1762 | |
| 1767 | 1763 | /* |
| 1768 | 1764 | * The caller is responsible for ensuring %POOL_DISASSOCIATED |
| @@ -3876,7 +3872,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt, · 2107 unmodified lines | ||
| 3876 | 3872 | } |
| 3877 | 3873 | |
| 3878 | 3874 | wq->rescuer = rescuer; |
| 3879 | − rescuer->task->flags |= PF_THREAD_BOUND; | |
| 3875 | + rescuer->task->flags |= PF_NO_SETAFFINITY; | |
| 3880 | 3876 | wake_up_process(rescuer->task); |
| 3881 | 3877 | } |
| 3882 | 3878 | |