Skip to contentSkip to conversation

cpu/hotplug: wait for cpuset_hotplug_work to finish on cpu onlining

PATCH Superseded Alexey Klimov 4 participants 9 messages Feb 4, 2021 – Feb 11, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu/hotplug: wait for cpuset_hotplug_work to finish on cpu onlining

When a CPU offlined and onlined via device_offline() and device_online() the userspace gets uevent notification. If, after receiving "online" uevent, userspace executes sched_setaffinity() on some task trying to move it to a recently onlined CPU, then it often fails with -EINVAL. Userspace needs to wait around 5..30 ms before sched_setaffinity() will succeed for the recently onlined CPU after receiving uevent.

If in_mask argument for sched_setaffinity() has only recently onlined CPU, it often fails with such flow:

sched_setaffinity()
cpuset_cpus_allowed()
guarantee_online_cpus() <-- cs->effective_cpus mask does not
contain recently onlined cpu
cpumask_and() <-- final new_mask is empty
__set_cpus_allowed_ptr()
cpumask_any_and_distribute() <-- returns dest_cpu equal to nr_cpu_ids returns -EINVAL

Cpusets used in guarantee_online_cpus() are updated using workqueue from cpuset_update_active_cpus() which in its turn is called from cpu hotplug callback sched_cpu_activate() hence it may not be observable by sched_setaffinity() if it is called immediately after uevent.
Out of line uevent can be avoided if we will ensure that cpuset_hotplug_work has run to completion using cpuset_wait_for_hotplug() after onlining the cpu in cpu_up() and in cpuhp_smt_enable().

---

Previous RFC patch and discussion is here:
https://lore.kernel.org/lkml/20201203171431.256675-1-aklimov-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org/

The commit a49e4629b5ed "cpuset: Make cpuset hotplug synchronous" would also get rid of the early uevent but it was reverted (deadlocks).

The nature of this bug is also described here (with different consequences):
https://lore.kernel.org/lkml/[email protected]/

Reproducer: https://gitlab.com/0xeafffffe/xlam

Currently with such changes the reproducer code continues to work without issues. The idea is to avoid the situation when userspace receives the event about onlined CPU which is not ready to take tasks for a while after uevent.

 kernel/cpu.c | 47 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 6 deletions(-)
kernel/cpu.c +41 -6
index 4e11e91010e1..ea728e75a74d 100644
@@ -15,6 +15,7 @@
 #include <linux/sched/smt.h>
 #include <linux/unistd.h>
 #include <linux/cpu.h>
+#include <linux/cpuset.h>
 #include <linux/oom.h>
 #include <linux/rcupdate.h>
 #include <linux/export.h>
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
 	err = _cpu_up(cpu, 0, target);
 out:
 	cpu_maps_update_done();
+
+	/* To avoid out of line uevent */
+	if (!err)
+		cpuset_wait_for_hotplug();
+
 	return err;
 }
 
@@ -2062,8 +2068,6 @@ static void cpuhp_offline_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
 	dev->offline = true;
-	/* Tell user space about the state change */
-	kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
 }
 
 static void cpuhp_online_cpu_device(unsigned int cpu)
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
 	dev->offline = false;
-	/* Tell user space about the state change */
-	kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 
 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
 {
-	int cpu, ret = 0;
+	struct device *dev;
+	cpumask_var_t mask;
+	int cpu, ret;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
+                return -ENOMEM;
 
+	ret = 0;
 	cpu_maps_update_begin();
 	for_each_online_cpu(cpu) {
 		if (topology_is_primary_thread(cpu))
@@ -2100,17 +2108,32 @@ int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
 		 * serialized against the regular offline usage.
 		 */
 		cpuhp_offline_cpu_device(cpu);
+		cpumask_set_cpu(cpu, mask);
 	}
 	if (!ret)
 		cpu_smt_control = ctrlval;
 	cpu_maps_update_done();
+
+	/* Tell user space about the state changes */
+	for_each_cpu(cpu, mask) {
+		dev = get_cpu_device(cpu);
+		kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
+	}
+
+	free_cpumask_var(mask);
 	return ret;
 }
 
 int cpuhp_smt_enable(void)
 {
-	int cpu, ret = 0;
+	struct device *dev;
+	cpumask_var_t mask;
+	int cpu, ret;
 
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
+                return -ENOMEM;
+
+	ret = 0;
 	cpu_maps_update_begin();
 	cpu_smt_control = CPU_SMT_ENABLED;
 	for_each_present_cpu(cpu) {
@@ -2122,8 +2145,20 @@ int cpuhp_smt_enable(void)
 			break;
 		/* See comment in cpuhp_smt_disable() */
 		cpuhp_online_cpu_device(cpu);
+		cpumask_set_cpu(cpu, mask);
 	}
 	cpu_maps_update_done();
+
+	/* To avoid out of line uevents */
+	cpuset_wait_for_hotplug();
+
+	/* Tell user space about the state changes */
+	for_each_cpu(cpu, mask) {
+		dev = get_cpu_device(cpu);
+		kobject_uevent(&dev->kobj, KOBJ_ONLINE);
+	}
+
+	free_cpumask_var(mask);
 	return ret;
 }
 #endif
Signature · 1 line
2.30.0
maintainer
#1 lore raw
Quoted 14 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
 	err = _cpu_up(cpu, 0, target);
 out:
 	cpu_maps_update_done();
+
+	/* To avoid out of line uevent */
+	if (!err)
+		cpuset_wait_for_hotplug();
+
 	return err;
 }
 

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 7 lines
kernel/cpu.c +0 -2
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
 	dev->offline = false;
-	/* Tell user space about the state change */
-	kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

#2 lore raw
Quoted 18 lines · Peter Zijlstra
Quoted 16 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
      err = _cpu_up(cpu, 0, target);
 out:
      cpu_maps_update_done();
+
+     /* To avoid out of line uevent */
+     if (!err)
+             cpuset_wait_for_hotplug();
+
      return err;
 }
 
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
      struct device *dev = get_cpu_device(cpu);
 
Quoted 8 lines

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 5 lines
kernel/cpu.c +0 -2
      dev->offline = false;
-     /* Tell user space about the state change */
-     kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

The patch doesn't change the number of kobject_uevent()s. The userspace will get the same number of uevents as before the patch (at least if I can rely on my eyes).
Or is there a concern that now the uevents are sent in a row sequentially which might abuse userspace uevents handling machinery?

Best regards,
Alexey

maintainer
#3 lore raw
Quoted 28 lines · Alexey Klimov
Quoted 18 lines · Peter Zijlstra
Quoted 16 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
      err = _cpu_up(cpu, 0, target);
 out:
      cpu_maps_update_done();
+
+     /* To avoid out of line uevent */
+     if (!err)
+             cpuset_wait_for_hotplug();
+
      return err;
 }
 
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
      struct device *dev = get_cpu_device(cpu);
 
Quoted 8 lines

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 5 lines
kernel/cpu.c +0 -2
      dev->offline = false;
-     /* Tell user space about the state change */
-     kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

Quoted 3 lines

The patch doesn't change the number of kobject_uevent()s. The userspace will get the same number of uevents as before the patch (at least if I can rely on my eyes).

bringup_hibernate_cpu() didn't used to generate an event, it does now. Same for bringup_nonboot_cpus().

Also, looking again, you don't seem to be reinstating the OFFLINE event you took out.

#4 lore raw
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Alexey Klimov
Quoted 26 lines · Peter Zijlstra
Quoted 14 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
      err = _cpu_up(cpu, 0, target);
 out:
      cpu_maps_update_done();
+
+     /* To avoid out of line uevent */
+     if (!err)
+             cpuset_wait_for_hotplug();
+
      return err;
 }
 

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 7 lines
kernel/cpu.c +0 -2
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
      struct device *dev = get_cpu_device(cpu);
 
      dev->offline = false;
-     /* Tell user space about the state change */
-     kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

Quoted 6 lines
Quoted 3 lines

The patch doesn't change the number of kobject_uevent()s. The userspace will get the same number of uevents as before the patch (at least if I can rely on my eyes).

bringup_hibernate_cpu() didn't used to generate an event, it does now. Same for bringup_nonboot_cpus().

Both of those call cpu_up(), which only gets a cpuset_wait_for_hotplug() in this patch. No new events generated from that, right, it's just a wrapper for a flush_work()?

Quoted 2 lines

Also, looking again, you don't seem to be reinstating the OFFLINE event you took out.

It seems to be reinstated in cpuhp_smt_disable()?

#8 lore raw
In reply to Alexey Klimov
Quoted 19 lines · Alexey Klimov

When a CPU offlined and onlined via device_offline() and device_online() the userspace gets uevent notification. If, after receiving "online" uevent, userspace executes sched_setaffinity() on some task trying to move it to a recently onlined CPU, then it often fails with -EINVAL. Userspace needs to wait around 5..30 ms before sched_setaffinity() will succeed for the recently onlined CPU after receiving uevent.

If in_mask argument for sched_setaffinity() has only recently onlined CPU, it often fails with such flow:

sched_setaffinity()
cpuset_cpus_allowed()
guarantee_online_cpus() <-- cs->effective_cpus mask does not
contain recently onlined cpu
cpumask_and() <-- final new_mask is empty
__set_cpus_allowed_ptr()
cpumask_any_and_distribute() <-- returns dest_cpu equal to nr_cpu_ids returns -EINVAL

Quoted 7 lines

Cpusets used in guarantee_online_cpus() are updated using workqueue from cpuset_update_active_cpus() which in its turn is called from cpu hotplug callback sched_cpu_activate() hence it may not be observable by sched_setaffinity() if it is called immediately after uevent.
Out of line uevent can be avoided if we will ensure that cpuset_hotplug_work has run to completion using cpuset_wait_for_hotplug() after onlining the cpu in cpu_up() and in cpuhp_smt_enable().

Nice writeup. I just have some nits, patch looks ok otherwise.

kernel/cpu.c static int cpu_up(unsigned int cpu, enum cpuhp_state target) · line 1286

Quoted 6 lines
kernel/cpu.c +2 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
 	err = _cpu_up(cpu, 0, target);
 out:
 	cpu_maps_update_done();
+
+	/* To avoid out of line uevent */

Not sure this will make sense out of context. Maybe,

/*
* Wait for cpuset updates to cpumasks to finish. Later on this path
* may generate uevents whose consumers rely on the updates. */

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2078

Quoted 11 lines
kernel/cpu.c +0 -0
@@ -2062,8 +2068,6 @@ static void cpuhp_offline_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
 	dev->offline = true;
 }
 
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
 	dev->offline = false;
 }

You could get rid of these functions and just put the few remaining bits in the callers. They each have only one.

#6 lore raw
In reply to Peter Zijlstra
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
 	err = _cpu_up(cpu, 0, target);
 out:
 	cpu_maps_update_done();
+
+	/* To avoid out of line uevent */
+	if (!err)
+		cpuset_wait_for_hotplug();
+
 	return err;
 }
 
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
 	struct device *dev = get_cpu_device(cpu);
 
Quoted 8 lines

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 5 lines
kernel/cpu.c +0 -2
 	dev->offline = false;
-	/* Tell user space about the state change */
-	kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

And the resume latency will incur 5-30ms * nr_cpu_ids.

Since you just care about device_online(), isn't cpu_device_up() a better place for the wait? This function is special helper for device_online(), leaving suspend/resume and kexec paths free from having to do this unnecessary wait.

Thanks

--
Qais Yousef

#7 lore raw
Quoted 28 lines · Qais Yousef
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Alexey Klimov
kernel/cpu.c +5 -0
@@ -1281,6 +1282,11 @@ static int cpu_up(unsigned int cpu, enum cpuhp_state target)
    err = _cpu_up(cpu, 0, target);
 out:
    cpu_maps_update_done();
+
+   /* To avoid out of line uevent */
+   if (!err)
+           cpuset_wait_for_hotplug();
+
    return err;
 }
 
@@ -2071,14 +2075,18 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
    struct device *dev = get_cpu_device(cpu);
 
Quoted 8 lines

kernel/cpu.c static void cpuhp_online_cpu_device(unsigned int cpu) · line 2079

Quoted 5 lines
kernel/cpu.c +0 -2
    dev->offline = false;
-   /* Tell user space about the state change */
-   kobject_uevent(&dev->kobj, KOBJ_ONLINE);
 }
 

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

Quoted 5 lines

And the resume latency will incur 5-30ms * nr_cpu_ids.

Since you just care about device_online(), isn't cpu_device_up() a better place for the wait? This function is special helper for device_online(), leaving suspend/resume and kexec paths free from having to do this unnecessary wait.

Yup, the same idea here once Peter mentioned bringup_nonboot_cpus() and bringup_hibernate_cpu().

Best regards,
Alexey

#5 lore raw
In reply to Daniel Jordan
Quoted 2 lines · Daniel Jordan

Peter Zijlstra <[email protected]> writes:

[...]

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

One concequence of this is that you'll now get a bunch of notifications across things like suspend/hybernate.

The patch doesn't change the number of kobject_uevent()s. The userspace will get the same number of uevents as before the patch (at least if I can rely on my eyes).

bringup_hibernate_cpu() didn't used to generate an event, it does now. Same for bringup_nonboot_cpus().

Quoted 8 lines

Both of those call cpu_up(), which only gets a cpuset_wait_for_hotplug() in this patch. No new events generated from that, right, it's just a wrapper for a flush_work()?

Quoted 2 lines

Also, looking again, you don't seem to be reinstating the OFFLINE event you took out.

It seems to be reinstated in cpuhp_smt_disable()?

Peter, what Daniel said. cpuset_wait_for_hotplug() doesn't generate an event.

The offline event was moved below in the same function:

+
+ /* Tell user space about the state changes */
+ for_each_cpu(cpu, mask) {
+ dev = get_cpu_device(cpu);
+ kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
+ }
+
+ free_cpumask_var(mask);

Daniel,
thanks for your comments. I'll update the patch and resend.

Best regards,
Alexey