Re: [RFC] sched/fair: avoid redundant updates for unchanged default slices

From: Peter Zijlstra

Date: Mon Oct 12 2026 - 07:01:15 EST


On Fri, Oct 09, 2026 at 11:49:40PM +0800, Chengfeng Lin wrote:
> Hi,
>
> Repeated sched_setparam() calls with unchanged parameters still take the
> parameter-update path for a SCHED_OTHER task using the default slice.
> A small prototype reduced this cost from 618.18 to 163.23 ns per call on
> v7.2, saving about 455 ns (73.6%). I would appreciate feedback on this
> optimization.
>

> This
> matters after an RT priority-inheritance boost has ended. Existing
> permission and LSM checks remain before the shortcut; explicit uclamp
> updates and reset-on-fork handling are unchanged.

Pff, so much text to filter just to find the issue. So you're saying
that you saw PI cost increase due to this change?

> Would this be a useful shortcut, or are there other updates that this
> unchanged-parameter case must retain?
>
> Thanks,
> Chengfeng
>
> Assisted-by: LLM
>
> ---
> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c
> @@ -491,6 +491,39 @@ req_priv:
> return 0;
> }
>
> +static bool fair_params_changed(struct task_struct *p,
> + const struct sched_attr *attr)
> +{
> + if (attr->sched_nice != task_nice(p))
> + return true;
> +
> + if (attr->sched_runtime == p->se.slice)
> + return false;
> +
> + /* Preserve the existing path for PI-boosted and sched_ext tasks. */
> + if (p->sched_class != &fair_sched_class)
> + return true;
> +
> +#ifdef CONFIG_UCLAMP_TASK
> + /*
> + * Unchanged fair parameters can still require an implicit clamp reset,
> + * for example after a task was PI-boosted into the RT class.
> + */
> + if ((!p->uclamp_req[UCLAMP_MIN].user_defined &&
> + p->uclamp_req[UCLAMP_MIN].value != uclamp_none(UCLAMP_MIN)) ||
> + (!p->uclamp_req[UCLAMP_MAX].user_defined &&
> + p->uclamp_req[UCLAMP_MAX].value != uclamp_none(UCLAMP_MAX)))
> + return true;
> +#endif
> +
> + /*
> + * A zero runtime requests the default slice, whereas se.slice stores
> + * its actual duration. Keep the update if the mode or default changed.
> + */
> + return attr->sched_runtime || p->se.custom_slice ||
> + p->se.slice != READ_ONCE(sysctl_sched_base_slice);
> +}

Egads, this is unreadable. Also, perhaps wrong. Suppose the case where
attr->sched_runtime == se->slice, but !se->custom_slice. Or suppose the
slice matches, but you still need the change due to uclamp.

Anyway, are you trying to say something like so?

if (!!attr->sched_runtime != !!se->custom_slice)
return true;

if (attr->sched_runtime && attr->sched_runtime != se->slice)
return true;