[RFC PATCH 3/8] workqueue: Set PF_NO_SETAFFINITY instead of kthread_bind_mask()

From: Lai Jiangshan
Date: Thu Aug 04 2022 - 04:41:22 EST


From: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>

kthread_bind_mask() can't work correctly if spurious wakeup
happens before kthread_bind_mask().

And a spuriously wakeup worker's cpumask can be possibly changed
by a userspace if worker_attach_to_pool() is called earlier than
kthread_bind_mask().

To avoid the problem caused by spurious wokeup, set PF_NO_SETAFFINITY
at the starting of workers where kthread_bind_mask() can't be used
and luckily workqueue code binds cpumask by itself, all it needs is
only PF_NO_SETAFFINITY.

Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
Cc: Tejun Heo <tj@xxxxxxxxxx>,
Cc: Petr Mladek <pmladek@xxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>,
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>,
Cc: Wedson Almeida Filho <wedsonaf@xxxxxxxxxx>
Signed-off-by: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>
---
kernel/workqueue.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index f5b12c6778cc..82937c0fb21f 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1958,7 +1958,6 @@ static struct worker *create_worker(struct worker_pool *pool)
goto fail;

set_user_nice(worker->task, pool->attrs->nice);
- kthread_bind_mask(worker->task, pool->attrs->cpumask);

/* start the newly created worker */
wake_up_process(worker->task);
@@ -2380,6 +2379,8 @@ static int worker_thread(void *__worker)
struct worker *worker = __worker;
struct worker_pool *pool = worker->pool;

+ current->flags |= PF_NO_SETAFFINITY;
+
/* attach the worker to the pool */
worker_attach_to_pool(worker, pool);

@@ -2494,6 +2495,7 @@ static int rescuer_thread(void *__rescuer)
struct list_head *scheduled = &rescuer->scheduled;
bool should_stop;

+ current->flags |= PF_NO_SETAFFINITY;
set_user_nice(current, RESCUER_NICE_LEVEL);

/*
@@ -4279,7 +4281,6 @@ static int init_rescuer(struct workqueue_struct *wq)
}

wq->rescuer = rescuer;
- kthread_bind_mask(rescuer->task, cpu_possible_mask);
wake_up_process(rescuer->task);

return 0;
--
2.19.1.6.gb485710b