Skip to contentSkip to conversation

smpboot: add missing get_online_cpus() when register

PATCH In review Lai Jiangshan 3 participants 4 messages Jul 31, 2014 – Jan 23, 2015 CPU hotplug

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 smpboot: add missing get_online_cpus() when register

If the smpboot_register_percpu_thread() is called after smpboot_create_threads() but before __cpu_up(), the smpboot thread of the online-ing CPU is not created, and it results a bug. So we use get_online_cpus() to prevent it.

smpboot_unregister_percpu_thread() travels all possible CPU, it doesn't need get_online_cpus() which is removed in the patch.

CC Thomas Gleixner <[email protected]>
Cc Rusty Russell <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Srivatsa S. Bhat <[email protected]>
---
 kernel/smpboot.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/smpboot.c +2 -2
index eb89e18..8adab87 100644
@@ -279,6 +279,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	unsigned int cpu;
 	int ret = 0;
 
+	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	for_each_online_cpu(cpu) {
 		ret = __smpboot_create_thread(plug_thread, cpu);
@@ -291,6 +292,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	list_add(&plug_thread->list, &hotplug_threads);
 out:
 	mutex_unlock(&smpboot_threads_lock);
+	put_online_cpus();
 	return ret;
 }
 EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
@@ -303,11 +305,9 @@ EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  */
 void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
 {
-	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	list_del(&plug_thread->list);
 	smpboot_destroy_threads(plug_thread);
 	mutex_unlock(&smpboot_threads_lock);
-	put_online_cpus();
 }
 EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 4 lines · Lai Jiangshan

If the smpboot_register_percpu_thread() is called after smpboot_create_threads() but before __cpu_up(), the smpboot thread of the online-ing CPU is not created, and it results a bug. So we use get_online_cpus() to prevent it.

Do you have an example of the bug to include? Maintainers are going to need to understand the implications of the problem before the [email protected] annotation is warranted.

Quoted 26 lines

smpboot_unregister_percpu_thread() travels all possible CPU, it doesn't need get_online_cpus() which is removed in the patch.

CC Thomas Gleixner <[email protected]>
Cc Rusty Russell <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Srivatsa S. Bhat <[email protected]>
---
 kernel/smpboot.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/smpboot.c +1 -0
index eb89e18..8adab87 100644
@@ -279,6 +279,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	unsigned int cpu;
 	int ret = 0;
 
+	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	for_each_online_cpu(cpu) {
 		ret = __smpboot_create_thread(plug_thread, cpu);

kernel/smpboot.c int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread) · line 297

Quoted 6 lines
kernel/smpboot.c +1 -0
@@ -291,6 +292,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	list_add(&plug_thread->list, &hotplug_threads);
 out:
 	mutex_unlock(&smpboot_threads_lock);
+	put_online_cpus();
 	return ret;
 }

I think the {get,put}_online_cpus() pair should be nested inside the smpboot_threads_lock for better lock ordering since not all cases smpboot_threads_lock will require it.

That way, you can also do put_online_cpus() before smpboot_destroy_threads(), which you have already proven doesn't need it:

@@ -280,14 +280,17 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
int ret = 0;

mutex_lock(&smpboot_threads_lock);
+ get_online_cpus();
for_each_online_cpu(cpu) {
ret = __smpboot_create_thread(plug_thread, cpu); if (ret) {
+ put_online_cpus();
smpboot_destroy_threads(plug_thread); goto out;
}
smpboot_unpark_thread(plug_thread, cpu);
}
+ put_online_cpus();
list_add(&plug_thread->list, &hotplug_threads);
out:
mutex_unlock(&smpboot_threads_lock);

kernel/smpboot.c EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread); · line 313

Quoted 13 lines
kernel/smpboot.c +0 -2
 EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
@@ -303,11 +305,9 @@ EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  */
 void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
 {
-	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	list_del(&plug_thread->list);
 	smpboot_destroy_threads(plug_thread);
 	mutex_unlock(&smpboot_threads_lock);
-	put_online_cpus();
 }
 EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);

This makes sense.

#2 lore raw
Quoted 8 lines · David Rientjes
Quoted 4 lines · Lai Jiangshan

If the smpboot_register_percpu_thread() is called after smpboot_create_threads() but before __cpu_up(), the smpboot thread of the online-ing CPU is not created, and it results a bug. So we use get_online_cpus() to prevent it.

Do you have an example of the bug to include?

Sorry, no, I don't have.

Quoted 3 lines

Maintainers are going to
need to understand the implications of the problem before the [email protected] annotation is warranted.

It is possible that smpboot_register_percpu_thread() can be called any time in current kernel. Repeating the module ehca and check while repeating online/offline the CPUs, the bug is possible to hit. I have not such devices to test.

Let Thomas make the choice.

Quoted 42 lines
Quoted 27 lines

smpboot_unregister_percpu_thread() travels all possible CPU, it doesn't need get_online_cpus() which is removed in the patch.

CC Thomas Gleixner <[email protected]>
Cc Rusty Russell <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Srivatsa S. Bhat <[email protected]>
---
 kernel/smpboot.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/smpboot.c +1 -0
index eb89e18..8adab87 100644
@@ -279,6 +279,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	unsigned int cpu;
 	int ret = 0;
 
+	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	for_each_online_cpu(cpu) {
 		ret = __smpboot_create_thread(plug_thread, cpu);

kernel/smpboot.c int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread) · line 297

Quoted 6 lines
kernel/smpboot.c +1 -0
@@ -291,6 +292,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	list_add(&plug_thread->list, &hotplug_threads);
 out:
 	mutex_unlock(&smpboot_threads_lock);
+	put_online_cpus();
 	return ret;
 }

I think the {get,put}_online_cpus() pair should be nested inside the smpboot_threads_lock for better lock ordering since not all cases smpboot_threads_lock will require it.

That way, you can also do put_online_cpus() before smpboot_destroy_threads(), which you have already proven doesn't need it:

kernel/smpboot.c int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread) · line 283

Quoted 4 lines
kernel/smpboot.c +1 -0
@@ -280,14 +280,17 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	int ret = 0;
 
 	mutex_lock(&smpboot_threads_lock);
+	get_online_cpus();

get_online_cpus() can't be nested in smpboot_threads_lock.

Quoted 31 lines
(quoted diff) +2 -0
 	for_each_online_cpu(cpu) {
 		ret = __smpboot_create_thread(plug_thread, cpu);
 		if (ret) {
+			put_online_cpus();
 			smpboot_destroy_threads(plug_thread);
 			goto out;
 		}
 		smpboot_unpark_thread(plug_thread, cpu);
 	}
+	put_online_cpus();
 	list_add(&plug_thread->list, &hotplug_threads);
 out:
 	mutex_unlock(&smpboot_threads_lock);
Quoted 4 lines
kernel/smpboot.c +0 -0
 EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
@@ -303,11 +305,9 @@ EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  */

kernel/smpboot.c EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread); · line 313

Quoted 10 lines
kernel/smpboot.c +0 -2
 void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
 {
-	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	list_del(&plug_thread->list);
 	smpboot_destroy_threads(plug_thread);
 	mutex_unlock(&smpboot_threads_lock);
-	put_online_cpus();
 }
 EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);

This makes sense.
.

#3 lore raw

[tip:core/urgent] smpboot: Add missing get_online_cpus() in smpboot_register_percpu_thread()

In reply to Lai Jiangshan
smpboot: Add missing get_online_cpus() in smpboot_register_percpu_thread()
Commit-ID:  4bee96860a65c3a62d332edac331b3cf936ba3ad
Gitweb:     http://git.kernel.org/tip/4bee96860a65c3a62d332edac331b3cf936ba3ad
Author:     Lai Jiangshan <[email protected]>
AuthorDate: Thu, 31 Jul 2014 11:30:17 +0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 23 Jan 2015 11:33:51 +0100

smpboot: Add missing get_online_cpus() in smpboot_register_percpu_thread()

The following race exists in the smpboot percpu threads management:

CPU0 CPU1
cpu_up(2)
get_online_cpus();

  smpboot_create_threads(2);
			     smpboot_register_percpu_thread();
			     for_each_online_cpu();
			       __smpboot_create_thread();

__cpu_up(2);

This results in a missing per cpu thread for the newly onlined cpu2 and in a NULL pointer dereference on a consecutive offline of that cpu.

Proctect smpboot_register_percpu_thread() with get_online_cpus() to prevent that.

[ tglx: Massaged changelog and removed the change in
smpboot_unregister_percpu_thread() because that's an optimization and therefor not stable material. ]

---
 kernel/smpboot.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/smpboot.c +2 -0
index f032fb5..40190f2 100644
@@ -280,6 +280,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	unsigned int cpu;
 	int ret = 0;
 
+	get_online_cpus();
 	mutex_lock(&smpboot_threads_lock);
 	for_each_online_cpu(cpu) {
 		ret = __smpboot_create_thread(plug_thread, cpu);
@@ -292,6 +293,7 @@ int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
 	list_add(&plug_thread->list, &hotplug_threads);
 out:
 	mutex_unlock(&smpboot_threads_lock);
+	put_online_cpus();
 	return ret;
 }
 EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);