Re: [PATCH] sched/fair: Use atomic_long_sub() in clear_tg_load_avg()

From: Peter Zijlstra

Date: Mon Nov 30 2026 - 07:04:19 EST


On Mon, Nov 30, 2026 at 04:34:59PM +0800, Li kunyu wrote:
> clear_tg_load_avg() currently builds negative deltas with temporary
> variables dl and dr, then calls atomic_long_add() to subtract the
> cfs_rq's load and runnable contributions from the task group's shared
> load_avg and runnable_avg.
>
> Use atomic_long_sub() directly instead. This makes the subtraction
> explicit and drops the temporary variables.
>
> Signed-off-by: Li kunyu <likunyu@xxxxxxxxxxxxxx>
> ---
> kernel/sched/fair.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index da0d9e19c457..5338ae9b5565 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -5370,7 +5370,6 @@ static inline void update_tg_load_avg(struct cfs_rq *cfs_rq)
>
> static inline void clear_tg_load_avg(struct cfs_rq *cfs_rq)
> {
> - long dl, dr;
> u64 now;
>
> /*
> @@ -5380,10 +5379,8 @@ static inline void clear_tg_load_avg(struct cfs_rq *cfs_rq)
> return;
>
> now = rq_clock(rq_of(cfs_rq));
> - dl = 0 - cfs_rq->tg_load_avg_contrib;
> - dr = 0 - cfs_rq->tg_runnable_avg_contrib;
> - atomic_long_add(dl, &cfs_rq->tg->load_avg);
> - atomic_long_add(dr, &cfs_rq->tg->runnable_avg);
> + atomic_long_sub(cfs_rq->tg_load_avg_contrib, &cfs_rq->tg->load_avg);
> + atomic_long_sub(cfs_rq->tg_runnable_avg_contrib, &cfs_rq->tg->runnable_avg);
> cfs_rq->tg_load_avg_contrib = 0;
> cfs_rq->tg_runnable_avg_contrib = 0;
> cfs_rq->last_update_tg_load_avg = now;

Yeah, no. This way the code has the same form as the function above.