Skip to contentSkip to conversation

sched: sched: Fix rq->next_balance time updated to earlier than current time

PATCH In review Vincent Guittot 3 participants 3 messages Nov 12, 2021 – Nov 16, 2021 Scheduler

lore
Share & tools
lore

Vincent Guittot

author maintainer
#0 lore raw

PATCH 2/2 sched: sched: Fix rq->next_balance time updated to earlier than current time

From: Tim Chen <[email protected]>

In traces on newidle_balance(), this_rq->next_balance time goes backward and earlier than current time jiffies, e.g.

11.602 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739
11.624 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739
13.856 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73b
13.910 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b
14.637 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73c
14.666 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73c

It doesn't make sense to have a next_balance in the past. Fix newidle_balance() and update_next_balance() so the next balance time is at least jiffies+1.

---
 kernel/sched/fair.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/fair.c +6 -1
index a162b0ec8963..1050037578a9 100644
@@ -10138,7 +10138,10 @@ update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
 
 	/* used by idle balance, so cpu_busy = 0 */
 	interval = get_sd_balance_interval(sd, 0);
-	next = sd->last_balance + interval;
+	if (time_after(jiffies+1, sd->last_balance + interval))
+		next = jiffies+1;
+	else
+		next = sd->last_balance + interval;
 
 	if (time_after(*next_balance, next))
 		*next_balance = next;
@@ -10974,6 +10977,8 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
 
 out:
 	/* Move the next balance forward */
+	if (time_after(jiffies+1, this_rq->next_balance))
+		this_rq->next_balance = jiffies+1;
 	if (time_after(this_rq->next_balance, next_balance))
 		this_rq->next_balance = next_balance;
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 11 lines · Vincent Guittot

From: Tim Chen <[email protected]>

In traces on newidle_balance(), this_rq->next_balance time goes backward and earlier than current time jiffies, e.g.

11.602 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739
11.624 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739
13.856 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73b
13.910 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b
14.637 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73c
14.666 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73c

No explanation of what these numbers mean, or where they're taken from.

Quoted 3 lines

It doesn't make sense to have a next_balance in the past. Fix newidle_balance() and update_next_balance() so the next balance time is at least jiffies+1.

The changelog is deficient in that it doesn't explain how the times end up in the past, therefore we cannot evaluate if the provided solution is sufficient etc..

Quoted 23 lines
---
 kernel/sched/fair.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/fair.c +4 -1
index a162b0ec8963..1050037578a9 100644
@@ -10138,7 +10138,10 @@ update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
 
 	/* used by idle balance, so cpu_busy = 0 */
 	interval = get_sd_balance_interval(sd, 0);
-	next = sd->last_balance + interval;
+	if (time_after(jiffies+1, sd->last_balance + interval))
+		next = jiffies+1;
+	else
+		next = sd->last_balance + interval;
 
 	if (time_after(*next_balance, next))
 		*next_balance = next;

kernel/sched/fair.c static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) · line 10981

Quoted 5 lines
kernel/sched/fair.c +2 -0
@@ -10974,6 +10977,8 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
 
 out:
 	/* Move the next balance forward */
+	if (time_after(jiffies+1, this_rq->next_balance))
+		this_rq->next_balance = jiffies+1;

jiffies roll over here..

Also, what's the point of the update_next_balance() addition in the face of this one? AFAICT this hunk completely renders the other hunk useless.

kernel/sched/fair.c static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) · line 10983

Quoted 2 lines
kernel/sched/fair.c +0 -0
 	if (time_after(this_rq->next_balance, next_balance))
 		this_rq->next_balance = next_balance;

and you've violated your own premise :-)

Now, this pattern is repeated throughout, if it's a problem here, why isn't it a problem in say rebalance_domains() ?

Can we please unify the code across sites instead of growing different hacks in different places?

#2 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

From: Tim Chen <[email protected]>

In traces on newidle_balance(), this_rq->next_balance time goes backward and earlier than current time jiffies, e.g.

Quoted 8 lines
Quoted 6 lines
11.602 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739
11.624 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739
13.856 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73b
13.910 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b
14.637 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73c
14.666 (         ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73c

No explanation of what these numbers mean, or where they're taken from.

Sorry I should have added more explanation. I put a probe on newidle_balance and dump out the values of this_rq pointer, this_rq->next_balance and jiffies entering newidle_balance using the following commands:

perf probe 'newidle_balance this_rq this_rq->next_balance jiffies' perf trace -e probe:newidle_balance

In the first line of the trace, next_balance start off at 0x1004fb76c:

11.602 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739

and in the second line, next_balance actually goes backward to 0x1004fb731, and becomes less than the jiffies value 0x1004fb739.

11.624 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739

Quoted 40 lines
Quoted 4 lines

It doesn't make sense to have a next_balance in the past. Fix newidle_balance() and update_next_balance() so the next balance time is at least jiffies+1.

The changelog is deficient in that it doesn't explain how the times end up in the past, therefore we cannot evaluate if the provided solution is sufficient etc..

Quoted 23 lines
---
 kernel/sched/fair.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/fair.c +4 -1
index a162b0ec8963..1050037578a9 100644
@@ -10138,7 +10138,10 @@ update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
 
 	/* used by idle balance, so cpu_busy = 0 */
 	interval = get_sd_balance_interval(sd, 0);
-	next = sd->last_balance + interval;
+	if (time_after(jiffies+1, sd->last_balance + interval))
+		next = jiffies+1;
+	else
+		next = sd->last_balance + interval;
 
 	if (time_after(*next_balance, next))
 		*next_balance = next;

kernel/sched/fair.c static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) · line 10981

Quoted 5 lines
kernel/sched/fair.c +2 -0
@@ -10974,6 +10977,8 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
 
 out:
 	/* Move the next balance forward */
+	if (time_after(jiffies+1, this_rq->next_balance))
+		this_rq->next_balance = jiffies+1;

jiffies roll over here..

Quoted 7 lines

Also, what's the point of the update_next_balance() addition in the face of this one? AFAICT this hunk completely renders the other hunk useless.

kernel/sched/fair.c static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) · line 10983

Quoted 2 lines
kernel/sched/fair.c +0 -0
 	if (time_after(this_rq->next_balance, next_balance))
 		this_rq->next_balance = next_balance;

and you've violated your own premise :-)

Agree that this hunk is redundant. Should only keep the update_next_balance() hunk.

Quoted 3 lines

Now, this pattern is repeated throughout, if it's a problem here, why isn't it a problem in say rebalance_domains() ?

In rebalance_domains, next_balance is assigned an initial value of jiffies+60*HZ and could only increase.

So when we update this_rq-next_balance with next_balance computed, it should always be more than current jiffies.

Quoted 4 lines

Can we please unify the code across sites instead of growing different hacks in different places?

I'll take a closer look at the next_balance computation in other places.

Tim