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

From: Peter Zijlstra
Date: Fri Oct 07 2022 - 08:47:44 EST


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