Skip to contentSkip to conversation

sched/fair: Again ignore percpu threads for imbalance pulls

PATCH NAK Yihao Wu 3 participants 7 messages Dec 11, 2021 – Jan 18, 2022

lore
Share & tools
lore

Yihao Wu

author
#0 lore raw

PATCH 1/1 sched/fair: Again ignore percpu threads for imbalance pulls

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail. After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

    |sum_nr_running(node1) - sum_nr_running(node2)| > 100

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
 kernel/kthread.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +1 -5
index 4a4d7092a2d8..cb05d3ff2de4 100644
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
 bool kthread_is_per_cpu(struct task_struct *p)
 {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
 }
 
 /**
Signature · 1 line
2.32.0.604.gb1f3e1269
#1 lore raw
Quoted 32 lines · Yihao Wu

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail. After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

    |sum_nr_running(node1) - sum_nr_running(node2)| > 100

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
 kernel/kthread.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +0 -0
index 4a4d7092a2d8..cb05d3ff2de4 100644

kernel/kthread.c void kthread_set_per_cpu(struct task_struct *k, int cpu) · line 547

Quoted 10 lines
kernel/kthread.c +1 -5
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
 bool kthread_is_per_cpu(struct task_struct *p)
 {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
 }

NAK, this will break lots of things.

#2 lore raw
In reply to Yihao Wu
Quoted 9 lines · Yihao Wu

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail.

After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

    |sum_nr_running(node1) - sum_nr_running(node2)| > 100

Wakeup balance (aka find_idlest_cpu()) is different from periodic load balance (aka load_balance()) and doesn't use can_migrate_task(), so the incriminated commit shouldn't have impacted it (at least not in obvious ways...). Do you have any more details on that issue?

Quoted 11 lines

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

Reasoning about "proper" pcpu kthreads was simpler since they are static, see 3a7956e25e1d ("kthread: Fix PF_KTHREAD vs to_kthread() race")

Quoted 14 lines

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
 kernel/kthread.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +0 -0
index 4a4d7092a2d8..cb05d3ff2de4 100644

kernel/kthread.c void kthread_set_per_cpu(struct task_struct *k, int cpu) · line 547

Quoted 10 lines
kernel/kthread.c +1 -5
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
 bool kthread_is_per_cpu(struct task_struct *p)
 {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
 }

As Peter said, this is going to cause issues. If you look at kthread_set_per_cpu(), we also store a CPU value which we expect to be valid when kthread_is_per_cpu(), which that change is breaking.

AIUI what you want to patch is the actual usage in can_migrate_task()

Quoted 4 lines
kernel/kthread.c +0 -1
 /**
--

2.32.0.604.gb1f3e1269

author
#3 lore raw

Thanks a lot for the help, Valentin and Peter!

Quoted 11 lines · Valentin Schneider
Quoted 9 lines · Yihao Wu

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail.

After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

     |sum_nr_running(node1) - sum_nr_running(node2)| > 100
Quoted 4 lines

Wakeup balance (aka find_idlest_cpu()) is different from periodic load balance (aka load_balance()) and doesn't use can_migrate_task(), so the incriminated commit shouldn't have impacted it (at least not in obvious ways...). Do you have any more details on that issue

The original bugfix concerns only about load balance. While I found wake up balance is impacted too, after I observed regression in lmbench3 test suite. This is how it's impacted:

- Periodic load balance
- kthread_is_per_cpu? No
- env->flags |= LBF_SOME_PINNED
- sd_parent..imbalance being set to 1 because of LBF_SOME_PINNED

So far exactly the same as what Chandrasekhar describes in 2f5f4cce496e. Then imbalance connects periodic and wakeup balance.

- Wakeup balance(find_idlest_group)
- update_sg_wakeup_stats classifies local_sgs as group_imbalanced
- find_idlest_group chooses another NUMA node

wakeup balance keeps doing this until another NUMA node becomes so busy.
And another periodic load balance just shifts it around, makeing the previously overloaded node completely idle now.

(Thanks to the great schedviz tool, I observed that all workloads as a whole, is migrated between the two NUMA nodes in a ping-pong pattern, and with a period around 3ms)

The reason wake up balance suffers more is, in fork+exit test case, wakeup balance happens with much higher frequency. It exists in real world applications too I believe.

Quoted 7 lines
Quoted 7 lines

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

Quoted 9 lines
Quoted 5 lines

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

Reasoning about "proper" pcpu kthreads was simpler since they are static, see 3a7956e25e1d ("kthread: Fix PF_KTHREAD vs to_kthread() race")

Get it. Thanks.

Quoted 29 lines
Quoted 14 lines

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
  kernel/kthread.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +0 -0
index 4a4d7092a2d8..cb05d3ff2de4 100644

kernel/kthread.c void kthread_set_per_cpu(struct task_struct *k, int cpu) · line 547

Quoted 10 lines
kernel/kthread.c +1 -5
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
  bool kthread_is_per_cpu(struct task_struct *p)
  {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
  }

As Peter said, this is going to cause issues. If you look at kthread_set_per_cpu(), we also store a CPU value which we expect to be valid when kthread_is_per_cpu(), which that change is breaking.

Quoted 2 lines

AIUI what you want to patch is the actual usage in can_migrate_task()

Get it. Some may want a consistent view of kthread_is_per_cpu, kthread->cpu, and KTHREAD_IS_PER_CPU.

Are you suggesting to patch only can_migrate_task to check nr_cpus_allowed? Wouldn't it be confusing if it uses an alternative way to tell if p is a per-cpu kthread?

I haven't a better solution though. :(

Thanks,
Yihao Wu

Quoted 4 lines
Quoted 4 lines
kernel/kthread.c +0 -1
  /**
--

2.32.0.604.gb1f3e1269

#4 lore raw
Quoted 31 lines · Yihao Wu

Thanks a lot for the help, Valentin and Peter!

Quoted 11 lines · Valentin Schneider
Quoted 9 lines · Yihao Wu

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail.

After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

     |sum_nr_running(node1) - sum_nr_running(node2)| > 100
Quoted 4 lines

Wakeup balance (aka find_idlest_cpu()) is different from periodic load balance (aka load_balance()) and doesn't use can_migrate_task(), so the incriminated commit shouldn't have impacted it (at least not in obvious ways...). Do you have any more details on that issue

The original bugfix concerns only about load balance. While I found wake up balance is impacted too, after I observed regression in lmbench3 test suite. This is how it's impacted:

- Periodic load balance
- kthread_is_per_cpu? No
- env->flags |= LBF_SOME_PINNED
- sd_parent..imbalance being set to 1 because of LBF_SOME_PINNED

So far exactly the same as what Chandrasekhar describes in 2f5f4cce496e. Then imbalance connects periodic and wakeup balance.

Quoted 8 lines

- Wakeup balance(find_idlest_group)
- update_sg_wakeup_stats classifies local_sgs as group_imbalanced
- find_idlest_group chooses another NUMA node

wakeup balance keeps doing this until another NUMA node becomes so busy. And another periodic load balance just shifts it around, makeing the previously overloaded node completely idle now.

Oooh, right, I came to the same conclusion when I got that stress-ng regression report back then:

https://lore.kernel.org/all/[email protected]/

I pretty much gave up on that as the regression we caused by removing an obscure/accidental balance which I couldn't properly codify. I can give it another shot, but AFAICT that only affects fork/exec heavy workloads (that -13% was on something doing almost only forks) which is an odd case to support.

Quoted 55 lines

(Thanks to the great schedviz tool, I observed that all workloads as a whole, is migrated between the two NUMA nodes in a ping-pong pattern, and with a period around 3ms)

The reason wake up balance suffers more is, in fork+exit test case, wakeup balance happens with much higher frequency. It exists in real world applications too I believe.

Quoted 7 lines
Quoted 7 lines

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

Quoted 9 lines
Quoted 5 lines

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

Reasoning about "proper" pcpu kthreads was simpler since they are static, see 3a7956e25e1d ("kthread: Fix PF_KTHREAD vs to_kthread() race")

Get it. Thanks.

Quoted 29 lines
Quoted 14 lines

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
  kernel/kthread.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +0 -0
index 4a4d7092a2d8..cb05d3ff2de4 100644

kernel/kthread.c void kthread_set_per_cpu(struct task_struct *k, int cpu) · line 547

Quoted 10 lines
kernel/kthread.c +1 -5
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
  bool kthread_is_per_cpu(struct task_struct *p)
  {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
  }

As Peter said, this is going to cause issues. If you look at kthread_set_per_cpu(), we also store a CPU value which we expect to be valid when kthread_is_per_cpu(), which that change is breaking.

Quoted 8 lines
Quoted 2 lines

AIUI what you want to patch is the actual usage in can_migrate_task()

Get it. Some may want a consistent view of kthread_is_per_cpu, kthread->cpu, and KTHREAD_IS_PER_CPU.

Are you suggesting to patch only can_migrate_task to check nr_cpus_allowed?

Yes

Quoted 3 lines

Wouldn't it be confusing if it uses an alternative way to tell if p is a per-cpu kthread?

Well then it wouldn't catch just per-CPU kthreads, but rather any pinned task (kernel or otherwise). But then you have to check/test if that's a sane thing to :)

Quoted 10 lines

I haven't a better solution though. :(

Thanks,
Yihao Wu

Quoted 4 lines
Quoted 4 lines
kernel/kthread.c +0 -1
  /**
--

2.32.0.604.gb1f3e1269

author
#5 lore raw
Quoted 41 lines · Valentin Schneider
Quoted 31 lines · Yihao Wu

Thanks a lot for the help, Valentin and Peter!

Quoted 11 lines · Valentin Schneider
Quoted 9 lines · Yihao Wu

commit 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls") was meant to fix a performance issue, when load balance tries to migrate pinned kernel threads at MC domain level. This was destined to fail.

After it fails, it further makes wakeup balance at NUMA domain level messed up. The most severe case that I noticed and frequently occurs:

      |sum_nr_running(node1) - sum_nr_running(node2)| > 100
Quoted 4 lines

Wakeup balance (aka find_idlest_cpu()) is different from periodic load balance (aka load_balance()) and doesn't use can_migrate_task(), so the incriminated commit shouldn't have impacted it (at least not in obvious ways...). Do you have any more details on that issue

The original bugfix concerns only about load balance. While I found wake up balance is impacted too, after I observed regression in lmbench3 test suite. This is how it's impacted:

- Periodic load balance
- kthread_is_per_cpu? No
- env->flags |= LBF_SOME_PINNED
- sd_parent..imbalance being set to 1 because of LBF_SOME_PINNED

So far exactly the same as what Chandrasekhar describes in 2f5f4cce496e. Then imbalance connects periodic and wakeup balance.

Quoted 8 lines

- Wakeup balance(find_idlest_group)
- update_sg_wakeup_stats classifies local_sgs as group_imbalanced
- find_idlest_group chooses another NUMA node

wakeup balance keeps doing this until another NUMA node becomes so busy. And another periodic load balance just shifts it around, makeing the previously overloaded node completely idle now.

Quoted 5 lines

Oooh, right, I came to the same conclusion when I got that stress-ng regression report back then:

https://lore.kernel.org/all/[email protected]/

Shocked! I wasted weeks to locate almost the same regression. Why on earth haven't I read your discussion of half a year ago?

Quoted 2 lines

I pretty much gave up on that as the regression we caused by removing an obscure/accidental balance which I couldn't properly codify. I can give it

Strange, the regression reported to me says differently from yours.

                 4.19.91      before_2f5f4      after_2f5f4

my_report good bad bad
your_report N/A good bad

your_report says 2f5f4 introduces new regression. While my_report says 2f5f4 fails and leaves the old regression be ...

Maybe that's the reason why you give up on fixing it, yet I came to make can_migrate_task cover more cases (kernel_thread).

Quoted 4 lines

another shot, but AFAICT that only affects fork/exec heavy workloads (that -13% was on something doing almost only forks) which is an odd case to support.

Yes. They're indeed quite odd workloads.
- Apps with massive shortlived threads better change runtime model, or use a thread pool.
- Massive different apps on the same machine are even odder.

But I guess this problem affects normal workloads too, more or less but not significantly. Hard to tell exactly how much influence it has.

Quoted 58 lines
Quoted 58 lines

(Thanks to the great schedviz tool, I observed that all workloads as a whole, is migrated between the two NUMA nodes in a ping-pong pattern, and with a period around 3ms)

The reason wake up balance suffers more is, in fork+exit test case, wakeup balance happens with much higher frequency. It exists in real world applications too I believe.

Quoted 7 lines
Quoted 7 lines

However the original bugfix failed, because it covers only case 1) below.
1) Created by create_kthread
2) Created by kernel_thread
No kthread is assigned to task_struct in case 2 (Please refer to comments in free_kthread_struct) so it simply won't work.

Quoted 9 lines
Quoted 5 lines

The easist way to cover both cases is to check nr_cpus_allowed, just as discussed in the mailing list of the v1 version of the original fix.

* lmbench3.lat_proc -P 104 fork (2 NUMA, and 26 cores, 2 threads)

Reasoning about "proper" pcpu kthreads was simpler since they are static, see 3a7956e25e1d ("kthread: Fix PF_KTHREAD vs to_kthread() race")

Get it. Thanks.

Quoted 31 lines
Quoted 14 lines

w/out patch w/ patch
fork+exit latency 1660 ms 1520 ms ( 8.4%)

Fixes 2f5f4cce496e ("sched/fair: Ignore percpu threads for imbalance pulls")
---
   kernel/kthread.c | 6 +-----
   1 file changed, 1 insertion(+), 5 deletions(-)
kernel/kthread.c +0 -0
index 4a4d7092a2d8..cb05d3ff2de4 100644

kernel/kthread.c void kthread_set_per_cpu(struct task_struct *k, int cpu) · line 547

Quoted 10 lines
kernel/kthread.c +1 -5
@@ -543,11 +543,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)
 
   bool kthread_is_per_cpu(struct task_struct *p)
   {
-	struct kthread *kthread = __to_kthread(p);
-	if (!kthread)
-		return false;
-
-	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
+	return (p->flags & PF_KTHREAD) && p->nr_cpus_allowed == 1;
   }

As Peter said, this is going to cause issues. If you look at kthread_set_per_cpu(), we also store a CPU value which we expect to be valid when kthread_is_per_cpu(), which that change is breaking.

AIUI what you want to patch is the actual usage in can_migrate_task()

Quoted 8 lines
Quoted 5 lines

Get it. Some may want a consistent view of kthread_is_per_cpu, kthread->cpu, and KTHREAD_IS_PER_CPU.

Are you suggesting to patch only can_migrate_task to check nr_cpus_allowed?

Yes

Okay, I'll post a v2. And see if Peter likes it.

Quoted 8 lines
Quoted 3 lines

Wouldn't it be confusing if it uses an alternative way to tell if p is a per-cpu kthread?

Well then it wouldn't catch just per-CPU kthreads, but rather any pinned task (kernel or otherwise). But then you have to check/test if that's a sane thing to :)

Sounds like pain... and not an option :-D

Thanks,
Yihao Wu

Quoted 10 lines
Quoted 10 lines

I haven't a better solution though. :(

Thanks,
Yihao Wu

Quoted 4 lines
Quoted 4 lines
kernel/kthread.c +0 -1
   /**
--

2.32.0.604.gb1f3e1269

#6 lore raw
Quoted 7 lines · Yihao Wu
Quoted 6 lines · Valentin Schneider
Quoted 4 lines · Yihao Wu

wakeup balance keeps doing this until another NUMA node becomes so busy. And another periodic load balance just shifts it around, makeing the previously overloaded node completely idle now.

Quoted 9 lines
Quoted 5 lines

Oooh, right, I came to the same conclusion when I got that stress-ng regression report back then:

https://lore.kernel.org/all/[email protected]/

Shocked! I wasted weeks to locate almost the same regression. Why on earth haven't I read your discussion of half a year ago?

I've been there too :) It's a tricky thing, you have to at least do a bisection to find some commit, and then look up the ML if there's been any further discussion / report on it...

Quoted 6 lines
Quoted 2 lines

I pretty much gave up on that as the regression we caused by removing an obscure/accidental balance which I couldn't properly codify. I can give it

Strange, the regression reported to me says differently from yours.

                  4.19.91      before_2f5f4      after_2f5f4
Quoted 9 lines

my_report good bad bad
your_report N/A good bad

your_report says 2f5f4 introduces new regression. While my_report says 2f5f4 fails and leaves the old regression be ...

Maybe that's the reason why you give up on fixing it, yet I came to make can_migrate_task cover more cases (kernel_thread).

Huh; 2f5f4cce496e is actually a 5.10-stable backport of 9bcb959d05ee; what was the first bad commit for you?

Quoted 5 lines
Quoted 5 lines

another shot, but AFAICT that only affects fork/exec heavy workloads (that -13% was on something doing almost only forks) which is an odd case to support.

Quoted 8 lines

Yes. They're indeed quite odd workloads.
- Apps with massive shortlived threads better change runtime model, or use a thread pool.
- Massive different apps on the same machine are even odder.

But I guess this problem affects normal workloads too, more or less but not significantly. Hard to tell exactly how much influence it has.

Looking at my notes for the regression on that particular machine for that particular benchmark, the group_imbalanced logic triggers for ~1% of the forks, and the avg task lifespan was 6µs. IMO that's pretty extreme, fork-time balance becomes the only available balance point for the child tasks (IIRC benchmark has N stressors forking one child each) - as you said above a more realistic approach here should use a thread pool of some sort.