Re: [PATCH 1/2] sched: Skip class callbacks with SCHED_FLAG_KEEP_PARAMS
From: Andrea Righi
Date: Thu Jul 30 2026 - 05:07:03 EST
Hi Prateek,
On Thu, Jul 30, 2026 at 12:59:37PM +0530, K Prateek Nayak wrote:
> Hello Andrea,
>
> On 7/30/2026 11:20 AM, Andrea Righi wrote:
> > SCHED_FLAG_KEEP_PARAMS suppresses scheduler parameter and class changes,
> > but __sched_setscheduler() can still set DEQUEUE_CLASS when the class
> > computed from the requested policy differs from the task's current
> > class.
> >
> > This causes the switching_from(), switched_from(), switching_to() and
> > switched_to() callbacks to run even though p->sched_class remains
> > unchanged.
> >
> > Set DEQUEUE_CLASS only when the sched class is allowed to change.
> >
> > Fixes: 637b0682821b ("sched: Fold sched_class::switch{ing,ed}_{to,from}() into the change pattern")
> > Signed-off-by: Andrea Righi <arighi@xxxxxxxxxx>
> > ---
> > kernel/sched/syscalls.c | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> > index b215b0ead9a60..903b47f5d0b74 100644
> > --- a/kernel/sched/syscalls.c
> > +++ b/kernel/sched/syscalls.c
> > @@ -675,7 +675,8 @@ int __sched_setscheduler(struct task_struct *p,
> > prev_class = p->sched_class;
> > next_class = __setscheduler_class(policy, newprio);
> >
> > - if (prev_class != next_class)
> > + if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS) &&
> > + prev_class != next_class)
> > queue_flags |= DEQUEUE_CLASS;
>
> It seems there also exists a SCHED_FLAG_KEEP_POLICY which should be used
> to retain the policy. By a quick look at the syscall path,
> SCHED_FLAG_KEEP_PARAMS should just do an equivalent of:
>
> get_params(p, &attr, 0)
>
> and match the some properties of sched_attr with that of the task.
I think get_params() only copies the parameters relevant to the task's current
policy. For example, for a fair task it copies sched_nice, but not
sched_priority or the deadline parameters. Allowing an arbitrary cross-class
transition could still apply target-class parameters supplied by the caller
despite KEEP_PARAMS.
>
> The task is still free to switch classes if the attr->sched_policy
> differs given SCHED_FLAG_KEEP_PARAMS and SCHED_FLAG_KEEP_POLICY are two
> distinct flags and there is a separate SCHED_FLAG_KEEP_ALL to combines
> them both.
>
> I'm having a sneaky suspicion that the bit in sched_change guard below
> should actually check for SCHED_FLAG_KEEP_ALL like:
>
> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index b215b0ead9a6..baf1fc59069e 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c
> @@ -680,7 +680,7 @@ int __sched_setscheduler(struct task_struct *p,
>
> scoped_guard (sched_change, p, queue_flags) {
>
> - if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
> + if ((attr->sched_flags & SCHED_FLAG_KEEP_ALL) != SCHED_FLAG_KEEP_ALL) {
Changing this condition to SCHED_FLAG_KEEP_ALL would execute the full parameter
and class update when only SCHED_FLAG_KEEP_PARAMS is set, enabling the
problematic cross-class case.
IIUC, SCHED_FLAG_KEEP_POLICY is handled independently: the syscall replaces
attr->sched_policy with SETPARAM_POLICY, which is resolved to the task's current
policy under the rq lock. Parameter changes are still allowed in that case, so
the __setscheduler_params() block must run.
For this fix, I think DEQUEUE_CLASS should follow the existing update condition,
so that the class callbacks reflect whether p->sched_class is actually changed.
Thanks,
-Andrea
> __setscheduler_params(p, attr);
> p->sched_class = next_class;
> p->prio = newprio;
> ---
>
> >
> > scoped_guard (sched_change, p, queue_flags) {
>
> --
> Thanks and Regards,
> Prateek
>