2023-10-12 09:01:30

by Hao Jia

[permalink] [raw]
Subject: [PATCH v2] sched/core: Fix RQCF_ACT_SKIP leak

Igor Raits and Bagas Sanjaya report a RQCF_ACT_SKIP leak warning.
Link: https://lore.kernel.org/all/[email protected]

This warning may be triggered in the following situations:

CPU0 CPU1

__schedule()
*rq->clock_update_flags <<= 1;* unregister_fair_sched_group()
pick_next_task_fair+0x4a/0x410 destroy_cfs_bandwidth()
newidle_balance+0x115/0x3e0 for_each_possible_cpu(i) *i=0*
rq_unpin_lock(this_rq, rf) __cfsb_csd_unthrottle()
raw_spin_rq_unlock(this_rq)
rq_lock(*CPU0_rq*, &rf)
rq_clock_start_loop_update()
rq->clock_update_flags & RQCF_ACT_SKIP <--
raw_spin_rq_lock(this_rq)

The purpose of RQCF_ACT_SKIP is to skip the update rq clock,
but the update is very early in __schedule(), but we clear
RQCF_*_SKIP very late, causing it to span that gap above
and triggering this warning.

In __schedule() we can clear the RQCF_*_SKIP flag immediately
after update_rq_clock() to avoid this RQCF_ACT_SKIP leak warning.
And set rq->clock_update_flags to RQCF_UPDATED to avoid
rq->clock_update_flags < RQCF_ACT_SKIP warning that may be triggered later.

Fixes: ebb83d84e49b ("sched/core: Avoid multiple calling update_rq_clock() in __cfsb_csd_unthrottle()")
Cc: [email protected]
Reported-by: Igor Raits <[email protected]>
Reported-by: Bagas Sanjaya <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]
Suggested-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Hao Jia <[email protected]>
---
kernel/sched/core.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 802551e0009b..afb8d213155b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5374,8 +5374,6 @@ context_switch(struct rq *rq, struct task_struct *prev,
/* switch_mm_cid() requires the memory barriers above. */
switch_mm_cid(rq, prev, next);

- rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
-
prepare_lock_switch(rq, next, rf);

/* Here we just switch the register state and the stack. */
@@ -6615,6 +6613,7 @@ static void __sched notrace __schedule(unsigned int sched_mode)
/* Promote REQ to ACT */
rq->clock_update_flags <<= 1;
update_rq_clock(rq);
+ rq->clock_update_flags = RQCF_UPDATED;

switch_count = &prev->nivcsw;

@@ -6694,8 +6693,6 @@ static void __sched notrace __schedule(unsigned int sched_mode)
/* Also unlocks the rq: */
rq = context_switch(rq, prev, next, &rf);
} else {
- rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
-
rq_unpin_lock(rq, &rf);
__balance_callbacks(rq);
raw_spin_rq_unlock_irq(rq);
--
2.39.2


2023-10-20 10:48:35

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH v2] sched/core: Fix RQCF_ACT_SKIP leak

On Thu, Oct 12, 2023 at 05:00:03PM +0800, Hao Jia wrote:
> Igor Raits and Bagas Sanjaya report a RQCF_ACT_SKIP leak warning.
> Link: https://lore.kernel.org/all/[email protected]
>
> This warning may be triggered in the following situations:
>
> CPU0 CPU1
>
> __schedule()
> *rq->clock_update_flags <<= 1;* unregister_fair_sched_group()
> pick_next_task_fair+0x4a/0x410 destroy_cfs_bandwidth()
> newidle_balance+0x115/0x3e0 for_each_possible_cpu(i) *i=0*
> rq_unpin_lock(this_rq, rf) __cfsb_csd_unthrottle()
> raw_spin_rq_unlock(this_rq)
> rq_lock(*CPU0_rq*, &rf)
> rq_clock_start_loop_update()
> rq->clock_update_flags & RQCF_ACT_SKIP <--
> raw_spin_rq_lock(this_rq)
>
> The purpose of RQCF_ACT_SKIP is to skip the update rq clock,
> but the update is very early in __schedule(), but we clear
> RQCF_*_SKIP very late, causing it to span that gap above
> and triggering this warning.
>
> In __schedule() we can clear the RQCF_*_SKIP flag immediately
> after update_rq_clock() to avoid this RQCF_ACT_SKIP leak warning.
> And set rq->clock_update_flags to RQCF_UPDATED to avoid
> rq->clock_update_flags < RQCF_ACT_SKIP warning that may be triggered later.
>

Thanks!