mirror of https://lore.kernel.org/lkml/
 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 01/12] sched: Reject SCHED_FLAG_KEEP_PARAMS before reserving deadline bandwidth
Date: Wed, 04 Nov 2026 15:30:13 +0000	[thread overview]
Message-ID: <sashiko-outbox-197505@kernel.org> (raw)
In-Reply-To: <fc36341b93c2e994ccec71a5ab6d5c8e76b3d73c.1793804635.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] sched: bandwidth leak in __sched_setscheduler() under SCHED_FLAG_KEEP_PARAMS
--

commit 7875cab1acc5b85b363d87da8587a9ad5e4642f8
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

sched: Reject SCHED_FLAG_KEEP_PARAMS before reserving deadline bandwidth

This commit moves the sched_dl_overflow() check and sched_reset_on_fork
update in __sched_setscheduler() after the SCHED_FLAG_KEEP_PARAMS class
change check so rejected requests do not leak deadline bandwidth.

> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index 3fa447aabe7fb..f0b7b2d52614f 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c
> @@ -655,17 +655,6 @@ int __sched_setscheduler(struct task_struct *p,
>  		goto recheck;
>  	}
>  
> -	/*
> -	 * If setscheduling to SCHED_DEADLINE (or changing the parameters
> -	 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
> -	 * is available.
> -	 */
> -	if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
> -		retval = -EBUSY;
> -		goto unlock;
> -	}
> -
> -	p->sched_reset_on_fork = reset_on_fork;
>  	oldprio = p->prio;
>  
>  	newprio = __normal_prio(policy, attr->sched_priority, attr->sched_nice);
> @@ -699,6 +688,19 @@ int __sched_setscheduler(struct task_struct *p,
>  		queue_flags |= DEQUEUE_CLASS;
>  	}
>  
> +	/*
> +	 * If setscheduling to SCHED_DEADLINE (or changing the parameters
> +	 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
> +	 * is available. Last of the checks: nothing gives back the
> +	 * bandwidth it reserves.
> +	 */
> +	if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
> +		retval = -EBUSY;
> +		goto unlock;
> +	}

[Severity: High]
Can __sched_setscheduler() in kernel/sched/syscalls.c still leak
dl_b->total_bw or corrupt rq->dl bandwidth accounting when
SCHED_FLAG_KEEP_PARAMS is set?

When attr->sched_flags & SCHED_FLAG_KEEP_PARAMS is set, the sched_change
block skips __setscheduler_params(p, attr):

kernel/sched/syscalls.c:__sched_setscheduler() {
	...
	scoped_guard (sched_change, p, queue_flags) {

		if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
			__setscheduler_params(p, attr);
			p->sched_class = next_class;
			p->prio = newprio;
			__setscheduler_dl_pi(newprio, policy, p, scope);
		}
		__setscheduler_uclamp(p, attr);
	...
}

Because __setscheduler_params() is skipped, neither p->policy nor
p->dl.dl_bw is updated. However, SCHED_FLAG_KEEP_PARAMS is only rejected
when prev_class != next_class, and sched_dl_overflow(p, policy, attr) is
still called even when SCHED_FLAG_KEEP_PARAMS is set.

This allows sched_dl_overflow() to modify deadline bandwidth without
updating p->dl.dl_bw or p->policy in two paths:

1. If a non-deadline task (e.g. p->policy == SCHED_NORMAL, so
!task_has_dl_policy(p) and p->dl.dl_bw == 0) is PI-boosted by a
SCHED_DEADLINE waiter via rt_mutex_setprio(), p->sched_class is
&dl_sched_class while p->policy remains SCHED_NORMAL. Calling
sys_sched_setattr() with sched_policy = SCHED_DEADLINE and
SCHED_FLAG_KEEP_PARAMS populates attr.sched_runtime = p->se.slice via
get_params(). In __sched_setscheduler(), both prev_class and next_class
are &dl_sched_class, so the prev_class != next_class check is bypassed.
sched_dl_overflow() then sees dl_policy(policy) && !task_has_dl_policy(p)
and adds new_bw to dl_b->total_bw via __dl_add(), while the
SCHED_FLAG_KEEP_PARAMS check skips __setscheduler_params(), leaving
p->policy as SCHED_NORMAL and p->dl.dl_bw as 0. When p deboosts, new_bw is
never subtracted from dl_b->total_bw.

2. In SYSCALL_DEFINE3(sched_setattr), get_params(p, &attr, 0) reads p's
deadline parameters without holding p->pi_lock or rq->__lock:

kernel/sched/syscalls.c:SYSCALL_DEFINE3(sched_setattr) {
	...
	if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS)
		get_params(p, &attr, 0);

	return sched_setattr(p, &attr);
}

If a concurrent sched_setattr() updates a SCHED_DEADLINE task's parameters
before __sched_setscheduler() acquires task_rq_lock(p, &rf), attr holds
stale parameters where new_bw != p->dl.dl_bw while prev_class ==
next_class == &dl_sched_class. sched_dl_overflow() then updates
dl_b->total_bw and calls dl_change_utilization(p, new_bw) to update
rq->dl.this_bw and rq->dl.running_bw to the stale new_bw, while
__setscheduler_params() is skipped, leaving p->dl.dl_bw out of sync with
dl_b->total_bw and rq->dl.

Should __sched_setscheduler() also reject SCHED_FLAG_KEEP_PARAMS when
policy != p->policy, and skip sched_dl_overflow() when
SCHED_FLAG_KEEP_PARAMS is set?

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

  reply	other threads:[~2026-11-04 15:30 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 15:10 [PATCH 00/12] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 01/12] sched: Reject SCHED_FLAG_KEEP_PARAMS before reserving deadline bandwidth Mathieu Desnoyers
2026-11-04 15:30   ` sashiko-bot [this message]
2026-11-04 15:52     ` Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 02/12] sched/deadline: Do not divide by zero active CPUs of a root domain Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 03/12] sched/deadline: Do not BUG() on a deadline task with no active CPU allowed Mathieu Desnoyers
2026-11-04 15:39   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 04/12] sched/deadline: Do not lose a throttled task switched out and back Mathieu Desnoyers
2026-11-04 15:42   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 05/12] sched/deadline: Do not release a departing sleeper's bandwidth twice Mathieu Desnoyers
2026-11-04 15:33   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 06/12] selftests/sched: Add a test of SCHED_DEADLINE policy round trips Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 07/12] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-04 15:47   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 08/12] selftests/sched: Test SCHED_DEADLINE " Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 09/12] sched/deadline: Do not take a waking task's bandwidth off a runqueue it left Mathieu Desnoyers
2026-11-04 15:45   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 10/12] sched/debug: Show the deadline bandwidth of each runqueue Mathieu Desnoyers
2026-11-04 15:10 ` [PATCH 11/12] sched/deadline: Rebuild the root domains' accounting without cpusets too Mathieu Desnoyers
2026-11-04 15:45   ` sashiko-bot
2026-11-04 15:10 ` [PATCH 12/12] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting Mathieu Desnoyers
2026-11-04 15:52   ` sashiko-bot
2026-11-04 20:24 ` [PATCH 00/12] sched/deadline: bandwidth accounting fixes and selftests 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-197505@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

all inboxes | Powered by JetHome®