Re: [PATCH v10 4/5] sched: Handle set_cpus_allowed_ptr(), sched_setaffinity() & other races

From: Waiman Long
Date: Fri Oct 07 2022 - 14:59:57 EST


On 10/7/22 08:47, Peter Zijlstra wrote:
On Thu, Sep 22, 2022 at 02:00:40PM -0400, Waiman Long wrote:
Racing is possible between set_cpus_allowed_ptr() and sched_setaffinity()
or between multiple sched_setaffinity() calls from different
CPUs. To resolve these race conditions, we need to update both
user_cpus_ptr and cpus_mask in a single lock critical section instead
of separated ones. This requires moving the user_cpus_ptr update
to set_cpus_allowed_common() by putting the user_mask into a new
affinity_context structure and using it to pass information around
various functions.

This patch also changes the handling of the race between the
sched_setaffinity() call and the changing of cpumask of the current
cpuset. In case the new mask conflicts with newly updated cpuset,
the cpus_mask will be reset to the cpuset cpumask and an error value
of -EINVAL will be returned. If a previous user_cpus_ptr value exists,
it will be swapped back in and the new_mask will be further restricted
to what is allowed in the cpumask pointed to by the old user_cpus_ptr.

The potential race between sched_setaffinity() and a fork/clone()
syscall calling dup_user_cpus_ptr() is also being handled.
This is still arse-backwards... You're still fixing races you've
introduced earlier in the series.

Since I don't think telling you again is going to help; I've done it for
you :/ How's this then?

https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/affinity

Thank you very much for updating the patch series. Beside the minor nit that I talked about in the previous mail, the result looks good to me. Do you mind if I send another patch on top of your branch to make the adjustment or you want to do it yourself?

Cheers,
Longman