diff --git a/io_uring/io-wq.c b/io_uring/io-wq.c index 04265bf8d319d..e9e867fe5c35f 100644 --- a/io_uring/io-wq.c +++ b/io_uring/io-wq.c @@ -623,6 +623,34 @@ static void io_worker_handle_work(struct io_worker *worker) } while (1); } +/* + * The CPUs a worker of @wqe runs on: those of the io-wq affinity that the + * worker's cpuset allows, or the cpuset if they have none in common, as the + * scheduler does with a task's requested affinity when its cpuset changes. + * Read the cpuset each time: it can change while the io-wq exists. + */ +static void io_wq_worker_cpus(struct io_wqe *wqe, struct task_struct *tsk, + struct cpumask *cpus) +{ + cpuset_cpus_allowed(tsk, cpus); + if (cpumask_intersects(cpus, wqe->cpu_mask)) + cpumask_and(cpus, cpus, wqe->cpu_mask); +} + +/* Is the current worker on a CPU that a new worker would not get? */ +static bool io_wq_worker_off_cpus(struct io_wqe *wqe) +{ + cpumask_var_t cpus; + bool off; + + if (!alloc_cpumask_var(&cpus, GFP_KERNEL)) + return false; + io_wq_worker_cpus(wqe, current, cpus); + off = !cpumask_test_cpu(raw_smp_processor_id(), cpus); + free_cpumask_var(cpus); + return off; +} + static int io_wqe_worker(void *data) { struct io_worker *worker = data; @@ -670,8 +698,7 @@ static int io_wqe_worker(void *data) } if (!ret) { last_timeout = true; - exit_mask = !cpumask_test_cpu(raw_smp_processor_id(), - wqe->cpu_mask); + exit_mask = io_wq_worker_off_cpus(wqe); } } @@ -721,9 +748,16 @@ void io_wq_worker_sleeping(struct task_struct *tsk) static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker, struct task_struct *tsk) { + cpumask_var_t cpus; + tsk->worker_private = worker; worker->task = tsk; - set_cpus_allowed_ptr(tsk, wqe->cpu_mask); + /* Without a mask, the worker keeps its creator's CPUs. */ + if (alloc_cpumask_var(&cpus, GFP_KERNEL)) { + io_wq_worker_cpus(wqe, tsk, cpus); + set_cpus_allowed_ptr(tsk, cpus); + free_cpumask_var(cpus); + } raw_spin_lock(&wqe->lock); hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); @@ -1157,7 +1191,6 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) { int ret, node, i; struct io_wq *wq; - cpumask_var_t allowed_mask; if (WARN_ON_ONCE(!data->free_work || !data->do_work)) return ERR_PTR(-EINVAL); @@ -1177,9 +1210,6 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) wq->do_work = data->do_work; ret = -ENOMEM; - if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL)) - goto err; - cpuset_cpus_allowed(current, allowed_mask); for_each_node(node) { struct io_wqe *wqe; int alloc_node = node; @@ -1192,8 +1222,7 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) wq->wqes[node] = wqe; if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL)) goto err; - if (!cpumask_and(wqe->cpu_mask, cpumask_of_node(node), allowed_mask)) - cpumask_copy(wqe->cpu_mask, allowed_mask); + cpumask_copy(wqe->cpu_mask, cpumask_of_node(node)); wqe->node = alloc_node; wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = @@ -1227,7 +1256,6 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) free_cpumask_var(wq->wqes[node]->cpu_mask); kfree(wq->wqes[node]); } - free_cpumask_var(allowed_mask); err_wq: kfree(wq); return ERR_PTR(ret); @@ -1389,8 +1417,7 @@ int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask) else ret = -EINVAL; } else { - if (!cpumask_and(wqe->cpu_mask, cpumask_of_node(i), allowed_mask)) - cpumask_copy(wqe->cpu_mask, allowed_mask); + cpumask_copy(wqe->cpu_mask, cpumask_of_node(i)); } } rcu_read_unlock();