The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>
Cc: linux-rt-devel@lists.linux.dev, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 21/37] sched: Make deadline policy and affinity changes wait for a cpuset attach
Date: Mon, 16 Nov 2026 19:31:06 +0000	[thread overview]
Message-ID: <sashiko-outbox-217566@kernel.org> (raw)
In-Reply-To: <20261116190549.230119-22-mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched: broken cpuset rollback in __sched_setaffinity() with
  SCA_DL_NOWAIT
--

commit 9d195b268d19186fd430b17e56b76c3ac49f0d39
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

sched: Make deadline policy and affinity changes wait for a cpuset attach

This commit introduces cpuset_lock_no_attach() and SCA_DL_NOWAIT so that
SCHED_DEADLINE policy changes in __sched_setscheduler() and affinity
changes in sched_setaffinity() wait for an in-progress cpuset attach to
finish.

> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 2a964286a7cd0..52c291de384e7 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3195,6 +3195,16 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
>  		goto out;
>  	}
>  
> +	/*
> +	 * sched_setaffinity() tested the policy with no lock, and did not
> +	 * wait for a cpuset attach in progress. @p became a deadline task
> +	 * since: let the caller wait and try again.
> +	 */
> +	if ((ctx->flags & SCA_DL_NOWAIT) && task_has_dl_policy(p)) {
> +		ret = -EAGAIN;
> +		goto out;
> +	}
> +
>  	/*
>  	 * Check under the same lock that __sched_setscheduler() holds for
>  	 * its admission test, so that p cannot become SCHED_DEADLINE
> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index 4f47b17151ed9..70b364b4f82dd 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c

[ ... ]

> @@ -1318,13 +1324,31 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
>  		return -ENOMEM;
>  	}
>  
> -	ac = (struct affinity_context){
> -		.new_mask  = in_mask,
> -		.user_mask = user_mask,
> -		.flags     = SCA_USER,
> -	};
> -
> -	retval = __sched_setaffinity(p, &ac);
> +	/*
> +	 * Wait for a cpuset attach in progress before changing the affinity
> +	 * of a deadline task. cpuset_can_attach() reserved the bandwidth of
> +	 * the task for the CPUs it moves to, and cpuset_attach() moves it
> +	 * there. A new affinity in between could change those CPUs.
> +	 *
> +	 * The policy is tested here with no lock. A task that becomes a
> +	 * deadline task after this test is found under its runqueue lock,
> +	 * with SCA_DL_NOWAIT. Wait and try again then.
> +	 */
> +	for (;;) {
> +		dl = task_has_dl_policy(p);
> +		ac = (struct affinity_context){
> +			.new_mask  = in_mask,
> +			.user_mask = user_mask,
> +			.flags     = SCA_USER | (dl ? 0 : SCA_DL_NOWAIT),
> +		};
> +		if (dl)
> +			cpuset_lock_no_attach();
> +		retval = __sched_setaffinity(p, &ac);
> +		if (dl)
> +			cpuset_unlock();
> +		if (dl || retval != -EAGAIN)
> +			break;
> +	}
>  	kfree(ac.user_mask);
>  
>  	return retval;

[Severity: Medium]
Can SCA_DL_NOWAIT cause the cpuset race rollback in __sched_setaffinity()
to fail and leave p->cpus_mask and p->user_cpus_ptr un-restored?

When p is initially not a deadline task, sched_setaffinity() passes
SCA_DL_NOWAIT without holding cpuset_mutex.  If the first
__set_cpus_allowed_ptr() succeeds while a concurrent cpuset update narrows
cpus_allowed and another thread changes p to SCHED_DEADLINE, the recovery
path in __sched_setaffinity() clears SCA_DL_CHECK but leaves SCA_DL_NOWAIT
set:

kernel/sched/syscalls.c:__sched_setaffinity() {
    ...
	cpuset_cpus_allowed(p, cpus_allowed);
	if (!cpumask_subset(new_mask, cpus_allowed)) {
    ...
		ctx->flags &= ~SCA_DL_CHECK;
		__set_cpus_allowed_ptr(p, ctx);
		retval = -EINVAL;
	}
    ...
}

CPU0                                  CPU1
sched_setaffinity(p)
  dl = false (SCA_DL_NOWAIT set)
  __sched_setaffinity(p, &ac)
    cpuset_cpus_allowed(p, ...)
    __set_cpus_allowed_ptr(p, ctx)
      sets p->cpus_mask = new_mask
      swaps p->user_cpus_ptr
        with ctx->user_mask
                                      cpuset narrows p's cpus_allowed
                                      sched_setattr(p, SCHED_DEADLINE)
    cpuset_cpus_allowed(p, ...)
    !cpumask_subset(new_mask, ...)
    ctx->flags &= ~SCA_DL_CHECK
    __set_cpus_allowed_ptr(p, ctx)
      SCA_DL_NOWAIT && dl_policy
      returns -EAGAIN (ignored)  <-- rollback skipped
    returns -EINVAL
  kfree(ac.user_mask)

Because the second __set_cpus_allowed_ptr() aborts with -EAGAIN and its
return value is ignored, p->cpus_mask is left containing CPUs outside the
task's cpuset cpus_allowed, and the rejected user_mask remains installed
in p->user_cpus_ptr while the previous user_cpus_ptr is freed.

Clearing SCA_DL_NOWAIT alongside SCA_DL_CHECK before the recovery
__set_cpus_allowed_ptr() call would allow the rollback to complete.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116190549.230119-1-mathieu.desnoyers@efficios.com?part=21

  reply	other threads:[~2026-11-16 19:31 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 19:05 [PATCH v6 00/37] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 01/37] sched: Refuse SCHED_FLAG_KEEP_PARAMS that would change the policy or the class Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 02/37] sched/deadline: Do not admit bandwidth for parameters that are kept Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 03/37] sched: Let a task boosted by a deadline waiter change its policy Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 04/37] sched/deadline: Do not divide by zero active CPUs of a root domain Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 05/37] sched/deadline: Refuse dl_server debugfs writes while the rq is offline Mathieu Desnoyers
2026-11-16 19:30   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 06/37] sched: Take cpuset_mutex for a deadline policy change only with pi Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 07/37] sched/deadline: Do not lose a throttled task switched out and back Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 08/37] sched/deadline: Do not replenish a boost from cleared parameters Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 09/37] sched/deadline: Reset the deadline and the runtime with the parameters Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 10/37] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-16 19:38   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 11/37] sched/deadline: Release the bandwidth of a task that leaves the class asleep Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 12/37] sched/deadline: Reject SCHED_DEADLINE on exited tasks Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 13/37] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 14/37] sched/deadline: Fix this_bw when a task is changed during a remote wakeup Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 15/37] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 16/37] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 17/37] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 18/37] sched/deadline: Check SCHED_DEADLINE affinity under the rq lock Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 19/37] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 20/37] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 21/37] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-16 19:31   ` sashiko-bot [this message]
2026-11-16 19:05 ` [PATCH v6 22/37] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 23/37] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 24/37] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 25/37] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 26/37] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 27/37] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 28/37] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 29/37] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 30/37] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 31/37] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 32/37] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 33/37] sched/deadline: Check the runtime of a task whose parameters change Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 34/37] sched/debug: Show the deadline bandwidth of each runqueue Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 35/37] selftests/sched: Add a test of SCHED_DEADLINE policy round trips Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 36/37] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 37/37] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting Mathieu Desnoyers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-217566@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox