Re: [PATCH 2/2] sched/uclamp: Add uclamp_is_used() check before enable it

From: Christian Loehle
Date: Thu Feb 13 2025 - 10:04:15 EST


On 2/13/25 09:15, Xuewen Yan wrote:
> Because the static_branch_enable() would get the cpus_read_lock(),
> and sometimes users may frequently set the uclamp value of tasks,
> and the uclamp_validate() would call the static_branch_enable()
> frequently, so add the uclamp_is_used() check to prevent calling
> the cpus_read_lock() frequently.
>
> Signed-off-by: Xuewen Yan <xuewen.yan@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 456d339be98f..d718fddadb03 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c
> @@ -368,7 +368,8 @@ static int uclamp_validate(struct task_struct *p,
> * blocking operation which obviously cannot be done while holding
> * scheduler locks.
> */
> - static_branch_enable(&sched_uclamp_used);
> + if (!uclamp_is_used())
> + static_branch_enable(&sched_uclamp_used);
>
> return 0;
> }

Given that we never disable sched_uclamp_used once active this
is fine.
Reviewed-by: Christian Loehle <christian.loehle@xxxxxxx>