Skip to contentSkip to conversation

sched/fair: Cleanup load_balance() condition

PATCH In review Valentin Schneider 3 participants 6 messages Sep 26, 2018 – Nov 4, 2018 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 sched/fair: Cleanup load_balance() condition

The alignment of the condition is off, clean that up.

Also, logical operators have lower precedence than bitwise/relational operators, so remove one layer of parentheses to make the condition a bit simpler to follow.

---
 kernel/sched/fair.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched/fair.c +3 -3
index 6bd142d..9cf93ba 100644
@@ -8783,9 +8783,9 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 
 out_one_pinned:
 	/* tune up the balancing interval */
-	if (((env.flags & LBF_ALL_PINNED) &&
-			sd->balance_interval < MAX_PINNED_INTERVAL) ||
-			(sd->balance_interval < sd->max_interval))
+	if ((env.flags & LBF_ALL_PINNED &&
+	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
+	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
 
 	ld_moved = 0;
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 2/2 sched/fair: Don't increase sd->balance_interval on newidle balance

When load_balance() fails to move some load because of task affinity, we end up increasing sd->balance_interval to delay the next periodic balance in the hopes that next time we look, that annoying pinned task(s) will be gone.

However, idle_balance() pays no attention to sd->balance_interval, yet it will still lead to an increase in balance_interval in case of pinned tasks.

If we're going through several newidle balances (e.g. we have a periodic task), this can lead to a huge increase of the balance_interval in a very small amount of time.

To prevent that, don't increase the balance interval when going through a newidle balance.

This is a similar approach to what is done in commit 58b26c4c0257 ("sched: Increment cache_nice_tries only on periodic lb"), where we disregard newidle balance and rely on periodic balance for more stable results.

---
 kernel/sched/fair.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/sched/fair.c +11 -2
index 9cf93ba..4c33283 100644
@@ -8782,13 +8782,22 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 	sd->nr_balance_failed = 0;
 
 out_one_pinned:
+	ld_moved = 0;
+
+	/*
+	 * idle_balance() disregards balance intervals, so we could repeatedly
+	 * reach this code, which would lead to balance_interval skyrocketting
+	 * in a short amount of time. Skip the balance_interval increase logic
+	 * to avoid that.
+	 */
+	if (env.idle == CPU_NEWLY_IDLE)
+		goto out;
+
 	/* tune up the balancing interval */
 	if ((env.flags & LBF_ALL_PINNED &&
 	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
 	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
-
-	ld_moved = 0;
 out:
 	return ld_moved;
 }
Signature · 1 line
2.7.4
#2 lore raw

[tip:sched/core] sched/fair: Don't increase sd->balance_interval on newidle balance

sched/fair: Don't increase sd->balance_interval on newidle balance
Commit-ID:  3f130a37c442d5c4d66531b240ebe9abfef426b5
Gitweb:     https://git.kernel.org/tip/3f130a37c442d5c4d66531b240ebe9abfef426b5
Author:     Valentin Schneider <[email protected]>
AuthorDate: Wed, 26 Sep 2018 16:12:07 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sun, 4 Nov 2018 00:59:23 +0100

sched/fair: Don't increase sd->balance_interval on newidle balance

When load_balance() fails to move some load because of task affinity, we end up increasing sd->balance_interval to delay the next periodic balance in the hopes that next time we look, that annoying pinned task(s) will be gone.

However, idle_balance() pays no attention to sd->balance_interval, yet it will still lead to an increase in balance_interval in case of pinned tasks.

If we're going through several newidle balances (e.g. we have a periodic task), this can lead to a huge increase of the balance_interval in a very small amount of time.

To prevent that, don't increase the balance interval when going through a newidle balance.

This is a similar approach to what is done in commit 58b26c4c0257 ("sched: Increment cache_nice_tries only on periodic lb"), where we disregard newidle balance and rely on periodic balance for more stable results.

---
 kernel/sched/fair.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/sched/fair.c +11 -2
index 4e298931a715..a17ca4254427 100644
@@ -8876,13 +8876,22 @@ out_all_pinned:
 	sd->nr_balance_failed = 0;
 
 out_one_pinned:
+	ld_moved = 0;
+
+	/*
+	 * idle_balance() disregards balance intervals, so we could repeatedly
+	 * reach this code, which would lead to balance_interval skyrocketting
+	 * in a short amount of time. Skip the balance_interval increase logic
+	 * to avoid that.
+	 */
+	if (env.idle == CPU_NEWLY_IDLE)
+		goto out;
+
 	/* tune up the balancing interval */
 	if ((env.flags & LBF_ALL_PINNED &&
 	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
 	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
-
-	ld_moved = 0;
 out:
 	return ld_moved;
 }
#3 lore raw
In reply to Valentin Schneider

Hi,

Quoted 19 lines · Valentin Schneider

The alignment of the condition is off, clean that up.

Also, logical operators have lower precedence than bitwise/relational operators, so remove one layer of parentheses to make the condition a bit simpler to follow.

---
 kernel/sched/fair.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched/fair.c +0 -0
index 6bd142d..9cf93ba 100644
@@ -8783,9 +8783,9 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 
 out_one_pinned:

kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 8791

Quoted 10 lines
kernel/sched/fair.c +3 -3
 	/* tune up the balancing interval */
-	if (((env.flags & LBF_ALL_PINNED) &&
-			sd->balance_interval < MAX_PINNED_INTERVAL) ||
-			(sd->balance_interval < sd->max_interval))
+	if ((env.flags & LBF_ALL_PINNED &&
+	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
+	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
 
 	ld_moved = 0;

Is there anything else that should be done for these two patches?

Thanks,
Valentin

maintainer
#4 lore raw
Quoted 30 lines · Valentin Schneider

Hi,

Quoted 27 lines · Valentin Schneider

The alignment of the condition is off, clean that up.

Also, logical operators have lower precedence than bitwise/relational operators, so remove one layer of parentheses to make the condition a bit simpler to follow.

---
 kernel/sched/fair.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched/fair.c +3 -3
index 6bd142d..9cf93ba 100644
@@ -8783,9 +8783,9 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 
 out_one_pinned:
 	/* tune up the balancing interval */
-	if (((env.flags & LBF_ALL_PINNED) &&
-			sd->balance_interval < MAX_PINNED_INTERVAL) ||
-			(sd->balance_interval < sd->max_interval))
+	if ((env.flags & LBF_ALL_PINNED &&
+	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
+	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
 
Quoted 4 lines

kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 8791

Quoted 2 lines
kernel/sched/fair.c +0 -0
 	ld_moved = 0;

Is there anything else that should be done for these two patches?

Have them now, Thanks!

#5 lore raw

[tip:sched/core] sched/fair: Clean up load_balance() condition

In reply to Valentin Schneider
sched/fair: Clean up load_balance() condition
Commit-ID:  47b7aee14fd7e453370a5d15dfb11c958ca360f2
Gitweb:     https://git.kernel.org/tip/47b7aee14fd7e453370a5d15dfb11c958ca360f2
Author:     Valentin Schneider <[email protected]>
AuthorDate: Wed, 26 Sep 2018 16:12:06 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sun, 4 Nov 2018 00:59:22 +0100

sched/fair: Clean up load_balance() condition

The alignment of the condition is off, clean that up.

Also, logical operators have lower precedence than bitwise/relational operators, so remove one layer of parentheses to make the condition a bit simpler to follow.

---
 kernel/sched/fair.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched/fair.c +3 -3
index ee271bb661cc..4e298931a715 100644
@@ -8877,9 +8877,9 @@ out_all_pinned:
 
 out_one_pinned:
 	/* tune up the balancing interval */
-	if (((env.flags & LBF_ALL_PINNED) &&
-			sd->balance_interval < MAX_PINNED_INTERVAL) ||
-			(sd->balance_interval < sd->max_interval))
+	if ((env.flags & LBF_ALL_PINNED &&
+	     sd->balance_interval < MAX_PINNED_INTERVAL) ||
+	    sd->balance_interval < sd->max_interval)
 		sd->balance_interval *= 2;
 
 	ld_moved = 0;