Re: [PATCH v5 1/3] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity()

From: Peter Zijlstra
Date: Wed Aug 17 2022 - 04:46:45 EST


On Tue, Aug 16, 2022 at 03:27:32PM -0400, Waiman Long wrote:
> @@ -8079,10 +8056,11 @@ int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask)
> #endif
>
> static int
> -__sched_setaffinity(struct task_struct *p, const struct cpumask *mask)
> +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask, bool save_mask)
> {
> int retval;
> cpumask_var_t cpus_allowed, new_mask;
> + struct cpumask *user_mask = NULL;
>
> if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL))
> return -ENOMEM;

Please move that retval down so the variable declarations are properly
ordered again.

> @@ -8098,8 +8076,33 @@ __sched_setaffinity(struct task_struct *p, const struct cpumask *mask)
> retval = dl_task_check_affinity(p, new_mask);
> if (retval)
> goto out_free_new_mask;
> +
> + /*
> + * Save the user requested mask into user_cpus_ptr if save_mask set.
> + * pi_lock is used for protecting user_cpus_ptr.
> + */
> + if (save_mask && !p->user_cpus_ptr) {
> + user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
> +
> + if (!user_mask) {
> + retval = -ENOMEM;
> + goto out_free_new_mask;
> + }
> + }
> + if (save_mask) {
> + unsigned long flags;
> +
> + raw_spin_lock_irqsave(&p->pi_lock, flags);
> + if (!p->user_cpus_ptr) {
> + p->user_cpus_ptr = user_mask;
> + user_mask = NULL;
> + }
> +
> + cpumask_copy(p->user_cpus_ptr, mask);
> + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
> + }

How about:

if (save_mask) {
if (!p->user_cpus_ptr) {
...
}
...
}

?