Skip to contentSkip to conversation

CPU hotplug: stop_machine()-free CPU hotplug

RFC PATCH Superseded Srivatsa S. Bhat 3 participants 38 messages Dec 7, 2012 – Dec 11, 2012

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover CPU hotplug: stop_machine()-free CPU hotplug

Hi,

This patchset removes CPU hotplug's dependence on stop_machine() from the CPU offline path and provides an alternative (set of APIs) to preempt_disable() to prevent CPUs from going offline, which can be invoked from atomic context.

This is an RFC patchset with only a few call-sites of preempt_disable() converted to the new APIs for now, and the main goal is to get feedback on the design of the new atomic APIs and see if it serves as a viable replacement for stop_machine()-free CPU hotplug. A brief description of the algorithm is available in the "Changes in vN" section.

Overview of the patches:
-----------------------

Patch 1 introduces the new APIs that can be used from atomic context, to prevent CPUs from going offline.

Patch 2 is a cleanup; it converts preprocessor macros to static inline functions.

Patches 3 to 8 convert various call-sites to use the new APIs.

Patch 9 is the one which actually removes stop_machine() from the CPU offline path.

Changes in v3:
--------------
* Dropped the _light() and _full() variants of the APIs. Provided a single
interface: get/put_online_cpus_atomic().

* Completely redesigned the synchronization mechanism again, to make it fast and scalable at the reader-side in the fast-path (when no hotplug writers are active). This new scheme also ensures that there is no possibility of deadlocks due to circular locking dependency. In summary, this provides the scalability and speed of per-cpu rwlocks (without actually using them), while avoiding the downside (deadlock possibilities) which is inherent in any per-cpu locking scheme that is meant to compete with preempt_disable()/enable() in terms of flexibility.

The problem with using per-cpu locking to replace preempt_disable()/enable was explained here:
https://lkml.org/lkml/2012/12/6/290

Basically we use per-cpu counters (for scalability) when no writers are active, and then switch to global rwlocks (for lock-safety) when a writer becomes active. It is a slightly complex scheme, but it is based on standard principles of distributed algorithms.

Changes in v2:
-------------
* Completely redesigned the synchronization scheme to avoid using any extra cpumasks.

* Provided APIs for 2 types of atomic hotplug readers: "light" (for light-weight) and "full". We wish to have more "light" readers than the "full" ones, to avoid indirectly inducing the "stop_machine effect" without even actually using stop_machine().

And the patches show that it _is_ generally true: 5 patches deal with "light" readers, whereas only 1 patch deals with a "full" reader.

Also, the "light" readers happen to be in very hot paths. So it makes a lot of sense to have such a distinction and a corresponding light-weight API.

Links to previous versions:
v2: https://lkml.org/lkml/2012/12/5/322
v1: https://lkml.org/lkml/2012/12/4/88

Comments and suggestions welcome!

--
Paul E. McKenney (1):
cpu: No more __stop_machine() in _cpu_down()

Srivatsa S. Bhat (8):
CPU hotplug: Provide APIs to prevent CPU offline from atomic context CPU hotplug: Convert preprocessor macros to static inline functions smp, cpu hotplug: Fix smp_call_function_*() to prevent CPU offline properly smp, cpu hotplug: Fix on_each_cpu_*() to prevent CPU offline properly sched, cpu hotplug: Use stable online cpus in try_to_wake_up() & select_task_rq() kick_process(), cpu-hotplug: Prevent offlining of target CPU properly yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly kvm, vmx: Add atomic synchronization with CPU Hotplug

  arch/x86/kvm/vmx.c  |    8 +-
 include/linux/cpu.h |    8 +-
 kernel/cpu.c        |  254 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/core.c |   22 ++++
 kernel/smp.c        |   63 ++++++++-----
 5 files changed, 321 insertions(+), 34 deletions(-)

Thanks,
Srivatsa S. Bhat
IBM Linux Technology Center

#1 lore raw

RFC PATCH v3 1/9 CPU hotplug: Provide APIs to prevent CPU offline from atomic context

There are places where preempt_disable() is used to prevent any CPU from going offline during the critical section. Let us call them as "atomic hotplug readers" ("atomic" because they run in atomic contexts).

Today, preempt_disable() works because the writer uses stop_machine(). But once stop_machine() is gone, the readers won't be able to prevent CPUs from going offline using preempt_disable().

The intent of this patch is to provide synchronization APIs for such atomic hotplug readers, to prevent (any) CPUs from going offline, without depending on stop_machine() at the writer-side. The new APIs will look something like this: get/put_online_cpus_atomic()

Some important design requirements and considerations:
-----------------------------------------------------

1. Scalable synchronization at the reader-side, especially in the fast-path

Any synchronization at the atomic hotplug readers side must be highly scalable - avoid global single-holder locks/counters etc. Because, these paths currently use the extremely fast preempt_disable(); our replacement to preempt_disable() should not become ridiculously costly and also should not serialize the readers among themselves needlessly.

At a minimum, the new APIs must be extremely fast at the reader side atleast in the fast-path, when no CPU offline writers are active.

2. preempt_disable() was recursive. The replacement should also be recursive.

3. No (new) lock-ordering restrictions

preempt_disable() was super-flexible. It didn't impose any ordering restrictions or rules for nesting. Our replacement should also be equally flexible and usable.

4. No deadlock possibilities

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

So, avoid per-cpu locking schemes (per-cpu locks/per-cpu atomic counters with spin-on-contention etc) as much as possible.

Implementation of the design:
----------------------------

We use global rwlocks for synchronization, because then we won't get into lock-ordering related problems (unlike per-cpu locks). However, global rwlocks lead to unnecessary cache-line bouncing even when there are no hotplug writers present, which can slow down the system needlessly.

Per-cpu counters can help solve the cache-line bouncing problem. So we actually use the best of both: per-cpu counters (no-waiting) at the reader side in the fast-path, and global rwlocks in the slowpath.

[ Fastpath = no writer is active; Slowpath = a writer is active ]

IOW, the hotplug readers just increment/decrement their per-cpu refcounts when no writer is active. When a writer becomes active, he signals all readers to switch to global rwlocks for the duration of the CPU offline operation. The readers ACK the writer's signal when it is safe for them (ie., when they are about to start a fresh, non-nested read-side critical section) and start using (holding) the global rwlock for read in their subsequent critical sections.

The hotplug writer waits for ACKs from every reader, and then acquires the global rwlock for write and takes the CPU offline. Then the writer signals all readers that the CPU offline is done, and that they can go back to using their per-cpu refcounts again.

Note that the lock-safety (despite the per-cpu scheme) comes from the fact that the readers can *choose* when to ACK the writer's signal (iow, when to switch to the global rwlock). And the readers don't wait on anybody based on the per-cpu counters. The only true synchronization that involves waiting at the reader-side in this scheme, is the one arising from the global rwlock, which is safe from the circular locking dependency problems mentioned above (because it is global).

Reader-writer locks and per-cpu counters are recursive, so they can be used in a nested fashion in the reader-path. And the "signal-and-ack-with-no- reader-spinning" design of switching the synchronization scheme ensures that you can safely nest and call these APIs in any way you want, just like preempt_disable()/enable.

Together, these satisfy all the requirements mentioned above.

I'm indebted to Michael Wang and Xiao Guangrong for their numerous thoughtful suggestions and ideas, which inspired and influenced many of the decisions in this as well as previous designs. Thanks a lot Michael and Xiao!

---

 include/linux/cpu.h |    4 +
 kernel/cpu.c        |  252 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 253 insertions(+), 3 deletions(-)
include/linux/cpu.h +4 -0
index ce7a074..cf24da1 100644
@@ -175,6 +175,8 @@ extern struct bus_type cpu_subsys;
 
 extern void get_online_cpus(void);
 extern void put_online_cpus(void);
+extern void get_online_cpus_atomic(void);
+extern void put_online_cpus_atomic(void);
 #define hotcpu_notifier(fn, pri)	cpu_notifier(fn, pri)
 #define register_hotcpu_notifier(nb)	register_cpu_notifier(nb)
 #define unregister_hotcpu_notifier(nb)	unregister_cpu_notifier(nb)
@@ -198,6 +200,8 @@ static inline void cpu_hotplug_driver_unlock(void)
 
 #define get_online_cpus()	do { } while (0)
 #define put_online_cpus()	do { } while (0)
+#define get_online_cpus_atomic()	do { } while (0)
+#define put_online_cpus_atomic()	do { } while (0)
 #define hotcpu_notifier(fn, pri)	do { (void)(fn); } while (0)
 /* These aren't inline functions due to a GCC bug. */
 #define register_hotcpu_notifier(nb)	({ (void)(nb); 0; })
kernel/cpu.c +249 -3

282 lines not on this page · raw message

#2 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 8 lines · Srivatsa S. Bhat

4. No deadlock possibilities

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

So, avoid per-cpu locking schemes (per-cpu locks/per-cpu atomic counters with spin-on-contention etc) as much as possible.

I really can't say I like this approach. percpu locking is very tricky to get right and difficult to get right and we should try our best to avoid implementing subsystem specific ones as much as possible. Also, I think the right approach would be auditing each get_online_cpus_atomic() callsites and figure out proper locking order rather than implementing a construct this unusual especially as hunting down the incorrect cases shouldn't be difficult given proper lockdep annotation.

lg_lock doesn't do local nesting and I'm not sure how big a deal that is as I don't know how many should be converted. But if nesting is an absolute necessity, it would be much better to implement generic rwlock variant (say, lg_rwlock) rather than implementing unusual cpuhotplug-specific percpu synchronization construct.

Thanks.

Signature · 1 line
tejun
#3 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Hello, again.

Quoted 5 lines · Tejun Heo

possible. Also, I think the right approach would be auditing each get_online_cpus_atomic() callsites and figure out proper locking order rather than implementing a construct this unusual especially as hunting down the incorrect cases shouldn't be difficult given proper lockdep annotation.

On the second look, it looks like you're implementing proper percpu_rwlock semantics as readers aren't supposed to induce circular dependency directly. Can you please work with Oleg to implement proper percpu-rwlock and use that for CPU hotplug rather than implementing it inside CPU hotplug?

Thanks.

Signature · 1 line
tejun
#4 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 11 lines · Tejun Heo

Hello, again.

Quoted 5 lines · Tejun Heo

possible. Also, I think the right approach would be auditing each get_online_cpus_atomic() callsites and figure out proper locking order rather than implementing a construct this unusual especially as hunting down the incorrect cases shouldn't be difficult given proper lockdep annotation.

On the second look, it looks like you're implementing proper percpu_rwlock semantics

Ah, nice! I didn't realize that I was actually doing what I intended to avoid! ;-)

Looking at the implementation of lglocks, and going by Oleg's earlier comment that we just need to replace spinlock_t with rwlock_t in them to get percpu_rwlocks, I was horrified at the kinds of circular locking dependencies that they would be prone to, unless used carefully.

So I devised this scheme to be safe, while still having relaxed rules. But if *this* is what percpu_rwlocks should ideally look like, then great! :-)

Quoted 2 lines

as readers aren't supposed to induce circular
dependency directly.

Yep, in this scheme, nobody will end up in circular dependency.

Quoted 4 lines

Can you please work with Oleg to implement
proper percpu-rwlock and use that for CPU hotplug rather than implementing it inside CPU hotplug?

Sure, I'd be more than happy to!

Regards,
Srivatsa S. Bhat

#5 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

In reply to Tejun Heo
Quoted 19 lines · Tejun Heo
Quoted 8 lines · Srivatsa S. Bhat

4. No deadlock possibilities

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

So, avoid per-cpu locking schemes (per-cpu locks/per-cpu atomic counters with spin-on-contention etc) as much as possible.

I really can't say I like this approach. percpu locking is very tricky to get right and difficult to get right and we should try our best to avoid implementing subsystem specific ones as much as possible. Also, I think the right approach would be auditing each get_online_cpus_atomic() callsites and figure out proper locking order rather than implementing a construct this unusual especially as hunting down the incorrect cases shouldn't be difficult given proper lockdep annotation.

Quoted 6 lines

lg_lock doesn't do local nesting and I'm not sure how big a deal that is as I don't know how many should be converted. But if nesting is an absolute necessity, it would be much better to implement generic rwlock variant (say, lg_rwlock) rather than implementing unusual cpuhotplug-specific percpu synchronization construct.

To be honest, at a certain point in time while designing this, I did realize that this was getting kinda overly complicated ;-) ... but I wanted to see how this would actually work out when finished and get some feedback on the same, hence I posted it out. But this also proves that we _can_ actually compete with the flexibility of preempt_disable() and still be safe with respect to locking, if we really want to ;-)

Regards,
Srivatsa S. Bhat

#6 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Hello, Srivatsa.

Quoted 6 lines · Srivatsa S. Bhat
Quoted 5 lines

lg_lock doesn't do local nesting and I'm not sure how big a deal that is as I don't know how many should be converted. But if nesting is an absolute necessity, it would be much better to implement generic rwlock variant (say, lg_rwlock) rather than implementing unusual cpuhotplug-specific percpu synchronization construct.

Quoted 6 lines

To be honest, at a certain point in time while designing this, I did realize that this was getting kinda overly complicated ;-) ... but I wanted to see how this would actually work out when finished and get some feedback on the same, hence I posted it out. But this also proves that we _can_ actually compete with the flexibility of preempt_disable() and still be safe with respect to locking, if we really want to ;-)

I got confused by comparison to preempt_disable() but you're right that percpu rwlock shouldn't be able to introduce locking dependency which doesn't exist with non-percpu rwlock. ie. write locking should be atomic w.r.t. to all readers. At the simplest, this can be implemented by writer backing out all the way if try-locking any CPU fails and retrying the whole thing. That should be correct but has the potential of starving the writer.

What we need here is a generic percpu-rwlock. I don't know which exact implementation strategy we should choose. Maybe your switching to global rwlock is the right solution. But, at any rate, I think it would be best to implement proper percpu-rwlock and then apply it to CPU hotplug. It's actually gonna be pretty fitting as get_online_cpus() is being converted to percpu-rwsem. IIUC, Oleg has been working on this for a while now. Oleg, what do you think?

Thanks.

Signature · 1 line
tejun
#7 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 16 lines · Tejun Heo

Hello, Srivatsa.

Quoted 6 lines · Srivatsa S. Bhat
Quoted 5 lines

lg_lock doesn't do local nesting and I'm not sure how big a deal that is as I don't know how many should be converted. But if nesting is an absolute necessity, it would be much better to implement generic rwlock variant (say, lg_rwlock) rather than implementing unusual cpuhotplug-specific percpu synchronization construct.

Quoted 6 lines

To be honest, at a certain point in time while designing this, I did realize that this was getting kinda overly complicated ;-) ... but I wanted to see how this would actually work out when finished and get some feedback on the same, hence I posted it out. But this also proves that we _can_ actually compete with the flexibility of preempt_disable() and still be safe with respect to locking, if we really want to ;-)

Quoted 4 lines

I got confused by comparison to preempt_disable() but you're right that percpu rwlock shouldn't be able to introduce locking dependency which doesn't exist with non-percpu rwlock. ie. write locking should be atomic w.r.t. to all readers.

Yep!

Quoted 5 lines

At the simplest, this can be
implemented by writer backing out all the way if try-locking any CPU fails and retrying the whole thing. That should be correct but has the potential of starving the writer.

Exactly! This is what I mentioned yesterday in the link below, and said that its not good because of writer starvation ("too wasteful"):
https://lkml.org/lkml/2012/12/6/290

Quoted 8 lines

What we need here is a generic percpu-rwlock. I don't know which exact implementation strategy we should choose. Maybe your switching to global rwlock is the right solution. But, at any rate, I think it would be best to implement proper percpu-rwlock and then apply it to CPU hotplug. It's actually gonna be pretty fitting as get_online_cpus() is being converted to percpu-rwsem. IIUC, Oleg has been working on this for a while now. Oleg, what do you think?

Hmm, that sounds good.

Regards,
Srivatsa S. Bhat

#8 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

In reply to Srivatsa S. Bhat
Quoted 9 lines · Srivatsa S. Bhat

Per-cpu counters can help solve the cache-line bouncing problem. So we actually use the best of both: per-cpu counters (no-waiting) at the reader side in the fast-path, and global rwlocks in the slowpath.

[ Fastpath = no writer is active; Slowpath = a writer is active ]

IOW, the hotplug readers just increment/decrement their per-cpu refcounts when no writer is active.

Plus LOCK and cli/sti. I do not pretend I really know how bad this is performance-wise though. And at first glance this look overcomplicated.

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path, you also want to improve the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

Also. After the quick reading this doesn't look correct, please see below.

Quoted 4 lines
kernel/cpu.c +4 -0
+void get_online_cpus_atomic(void)
+{
+	unsigned int cpu = smp_processor_id();
+	unsigned long flags;

kernel/cpu.c static void cpu_hotplug_done(void) · line 234

Quoted 10 lines
kernel/cpu.c +10 -0
+
+	preempt_disable();
+	local_irq_save(flags);
+
+	if (cpu_hotplug.active_writer == current)
+		goto out;
+
+	smp_rmb(); /* Paired with smp_wmb() in drop_writer_signal() */
+
+	if (likely(!writer_active(cpu))) {

WINDOW. Suppose that reader_active() == F.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		mark_reader_fastpath();
+		goto out;

Why take_cpu_down() can't do announce_cpu_offline_begin() + sync_all_readers() in between?

Looks like we should increment the counter first, then check writer_active(). And sync_atomic_reader() needs rmb between 2 atomic_read's.

Or. Again, suppose that reader_active() == F. But is_new_writer() == T.

kernel/cpu.c static void cpu_hotplug_done(void) · line 249

Quoted 8 lines
kernel/cpu.c +8 -0
+	if (is_new_writer(cpu)) {
+		/*
+		 * ACK the writer's signal only if this is a fresh read-side
+		 * critical section, and not just an extension of a running
+		 * (nested) read-side critical section.
+		 */
+		if (!reader_active(cpu)) {
+			ack_writer_signal();

What if take_cpu_down() does announce_cpu_offline_end() right before ack_writer_signal() ? In this case get_online_cpus_atomic() returns with writer_signal == -1. If nothing else this breaks the next raise_writer_signal().

Oleg.

#9 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 6 lines · Oleg Nesterov
Quoted 5 lines · Srivatsa S. Bhat

Per-cpu counters can help solve the cache-line bouncing problem. So we actually use the best of both: per-cpu counters (no-waiting) at the reader side in the fast-path, and global rwlocks in the slowpath.

Quoted 8 lines
Quoted 4 lines

[ Fastpath = no writer is active; Slowpath = a writer is active ]

IOW, the hotplug readers just increment/decrement their per-cpu refcounts when no writer is active.

Plus LOCK and cli/sti. I do not pretend I really know how bad this is performance-wise though. And at first glance this look overcomplicated.

Hehe, I agree ;-) But I couldn't think of any other way to get rid of the deadlock possibilities, other than using global rwlocks. So I designed a way in which we can switch between per-cpu counters and global rwlocks dynamically. Probably there is a smarter way to achieve what we want, dunno...

Quoted 3 lines

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path

We can't sleep in that code.. so that's a no-go.

Quoted 3 lines

, you also want to improve
the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

I hadn't considered that. Thinking of it, I don't think it would help us.. It won't get rid of the currently running preempt_disable() sections no?

Quoted 19 lines

Also. After the quick reading this doesn't look correct, please see below.

Quoted 4 lines
kernel/cpu.c +4 -0
+void get_online_cpus_atomic(void)
+{
+	unsigned int cpu = smp_processor_id();
+	unsigned long flags;

kernel/cpu.c static void cpu_hotplug_done(void) · line 234

Quoted 10 lines
kernel/cpu.c +10 -0
+
+	preempt_disable();
+	local_irq_save(flags);
+
+	if (cpu_hotplug.active_writer == current)
+		goto out;
+
+	smp_rmb(); /* Paired with smp_wmb() in drop_writer_signal() */
+
+	if (likely(!writer_active(cpu))) {

WINDOW. Suppose that reader_active() == F.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		mark_reader_fastpath();
+		goto out;

Why take_cpu_down() can't do announce_cpu_offline_begin() + sync_all_readers() in between?

Looks like we should increment the counter first, then check writer_active().

You are right, I missed the above race-conditions.

Quoted 2 lines

And sync_atomic_reader() needs rmb between 2 atomic_read's.

OK.

Quoted 17 lines

Or. Again, suppose that reader_active() == F. But is_new_writer() == T.

kernel/cpu.c static void cpu_hotplug_done(void) · line 249

Quoted 8 lines
kernel/cpu.c +8 -0
+	if (is_new_writer(cpu)) {
+		/*
+		 * ACK the writer's signal only if this is a fresh read-side
+		 * critical section, and not just an extension of a running
+		 * (nested) read-side critical section.
+		 */
+		if (!reader_active(cpu)) {
+			ack_writer_signal();

What if take_cpu_down() does announce_cpu_offline_end() right before ack_writer_signal() ? In this case get_online_cpus_atomic() returns with writer_signal == -1. If nothing else this breaks the next raise_writer_signal().

Oh, yes, this one is wrong too! We need to mark ourselves as active reader right in the beginning. And probably change the check to "reader_nested()" or something like that.

Thanks a lot Oleg!
Regards,
Srivatsa S. Bhat

#10 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 8 lines · Srivatsa S. Bhat
Quoted 3 lines · Oleg Nesterov

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path

We can't sleep in that code.. so that's a no-go.

But we can?

Note that I meant _cpu_down(), not get_online_cpus_atomic() or take_cpu_down().

Oleg.

#11 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 8 lines · Oleg Nesterov
Quoted 7 lines · Srivatsa S. Bhat
Quoted 3 lines · Oleg Nesterov

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path

Quoted 6 lines
Quoted 1 line

We can't sleep in that code.. so that's a no-go.

But we can?

Note that I meant _cpu_down(), not get_online_cpus_atomic() or take_cpu_down().

Maybe I'm missing something, but how would it help if we did a synchronize_sched() so early (in _cpu_down())? Another bunch of preempt_disable() sections could start immediately after our call to synchronize_sched() no? How would we deal with that?

What we need to ensure here is that all existing preempt_disable() sections are over and that *we* (meaning, the cpu offline writer) get to proceed immediately after that, making all the new readers wait for us. And that is possible only if we do our 'wait-for-readers' thing very close to our actual cpu offline operation (which is take_cpu_down). Moreover, the writer needs to remain stable (non-preemptible) while doing the wait-for-readers. Else (if the writer himself keeps getting scheduled in and out of the CPU) I can't see how he can possibly do justice to the wait.

Also, synchronize_sched() only helps us do the 'wait-for-existing-readers' logic. What would take care of the 'prevent-new-readers-from-going-ahead' logic?

To add to it all, synchronize_sched() waits for _all_ preempt_disable() sections to complete, whereas only a handful of them might actually care about CPU hotplug. Which is an unnecessary burden for the writer (ie., waiting for unrelated readers to complete).

I bet you meant something else. Sorry if I misunderstood it.

Regards,
Srivatsa S. Bhat

#12 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 14 lines · Srivatsa S. Bhat
Quoted 12 lines · Oleg Nesterov
Quoted 8 lines · Srivatsa S. Bhat
Quoted 3 lines · Oleg Nesterov

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path

We can't sleep in that code.. so that's a no-go.

But we can?

Quoted 7 lines
Quoted 2 lines

Note that I meant _cpu_down(), not get_online_cpus_atomic() or take_cpu_down().

Maybe I'm missing something, but how would it help if we did a synchronize_sched() so early (in _cpu_down())? Another bunch of preempt_disable() sections could start immediately after our call to synchronize_sched() no? How would we deal with that?

Sorry for confusion. Of course synchronize_sched() alone is not enough. But we can use it to synchronize with preempt-disabled section and avoid the barriers/atomic in the fast-path.

For example,

	bool writer_pending;
	DEFINE_RWLOCK(writer_rwlock);
	DEFINE_PER_CPU(int, reader_ctr);

	void get_online_cpus_atomic(void)
	{
		preempt_disable();
	
		if (likely(!writer_pending) || __this_cpu_read(reader_ctr)) {
			__this_cpu_inc(reader_ctr);
			return;
		}

		read_lock(&writer_rwlock);
		__this_cpu_inc(reader_ctr);
		read_unlock(&writer_rwlock);
	}

	// lacks release semantics, but we don't care
	void put_online_cpus_atomic(void)
	{
		__this_cpu_dec(reader_ctr);
		preempt_enable();
	}

Now, _cpu_down() does

	writer_pending = true;
	synchronize_sched();

before stop_one_cpu(). When synchronize_sched() returns, we know that every get_online_cpus_atomic() must see writer_pending == T. And, if any CPU incremented its reader_ctr we must see it is not zero.

take_cpu_down() does

	write_lock(&writer_rwlock);

	for_each_online_cpu(cpu) {
		while (per_cpu(reader_ctr, cpu))
			cpu_relax();
	}

and takes the lock.

However. This can lead to the deadlock we already discussed. So take_cpu_down() should do

retry:

	write_lock(&writer_rwlock);

	for_each_online_cpu(cpu) {
		if (per_cpu(reader_ctr, cpu)) {
			write_unlock(&writer_rwlock);
			goto retry;
		}
	}

to take the lock. But this is livelockable. However, I do not think it is possible to avoid the livelock.

Just in case, the code above is only for illustration, perhaps it is not 100% correct and perhaps we can do it better. cpu_hotplug.active_writer is ignored for simplicity, get/put should check current == active_writer.

Oleg.

#13 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 87 lines · Oleg Nesterov
Quoted 14 lines · Srivatsa S. Bhat
Quoted 12 lines · Oleg Nesterov
Quoted 8 lines · Srivatsa S. Bhat
Quoted 3 lines · Oleg Nesterov

But yes, it is easy to blame somebody else's code ;) And I can't suggest something better at least right now. If I understand correctly, we can not use, say, synchronize_sched() in _cpu_down() path

We can't sleep in that code.. so that's a no-go.

But we can?

Quoted 7 lines
Quoted 2 lines

Note that I meant _cpu_down(), not get_online_cpus_atomic() or take_cpu_down().

Maybe I'm missing something, but how would it help if we did a synchronize_sched() so early (in _cpu_down())? Another bunch of preempt_disable() sections could start immediately after our call to synchronize_sched() no? How would we deal with that?

Sorry for confusion. Of course synchronize_sched() alone is not enough. But we can use it to synchronize with preempt-disabled section and avoid the barriers/atomic in the fast-path.

For example,

	bool writer_pending;
	DEFINE_RWLOCK(writer_rwlock);
	DEFINE_PER_CPU(int, reader_ctr);

	void get_online_cpus_atomic(void)
	{
		preempt_disable();
	
		if (likely(!writer_pending) || __this_cpu_read(reader_ctr)) {
			__this_cpu_inc(reader_ctr);
			return;
		}

		read_lock(&writer_rwlock);
		__this_cpu_inc(reader_ctr);
		read_unlock(&writer_rwlock);
	}

	// lacks release semantics, but we don't care
	void put_online_cpus_atomic(void)
	{
		__this_cpu_dec(reader_ctr);
		preempt_enable();
	}

Now, _cpu_down() does

	writer_pending = true;
	synchronize_sched();

before stop_one_cpu(). When synchronize_sched() returns, we know that every get_online_cpus_atomic() must see writer_pending == T. And, if any CPU incremented its reader_ctr we must see it is not zero.

take_cpu_down() does

	write_lock(&writer_rwlock);

	for_each_online_cpu(cpu) {
		while (per_cpu(reader_ctr, cpu))
			cpu_relax();
	}

and takes the lock.

However. This can lead to the deadlock we already discussed. So take_cpu_down() should do

retry:

	write_lock(&writer_rwlock);

	for_each_online_cpu(cpu) {
		if (per_cpu(reader_ctr, cpu)) {
			write_unlock(&writer_rwlock);
			goto retry;
		}
	}
Quoted 7 lines

to take the lock. But this is livelockable. However, I do not think it is possible to avoid the livelock.

Just in case, the code above is only for illustration, perhaps it is not 100% correct and perhaps we can do it better. cpu_hotplug.active_writer is ignored for simplicity, get/put should check current == active_writer.

This approach (of using synchronize_sched()) also looks good. It is simple, yet effective, but unfortunately inefficient at the writer side (because he'll have to wait for a full synchronize_sched()).

So I would say that we should keep this approach as a fallback, if we don't come up with any better synchronization scheme (in terms of efficiency) at a comparable level of simplicity/complexity.

I have come up with a v4 that simplifies several aspects of the synchronization and makes it look a lot more simpler than v3. (Lesser race windows to take care of, implicit ACKs, no atomic ops etc..)

I'll post it soon. Let me know what you think...

Regards,
Srivatsa S. Bhat

#14 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Hello, Srivatsa.

Quoted 3 lines · Srivatsa S. Bhat

This approach (of using synchronize_sched()) also looks good. It is simple, yet effective, but unfortunately inefficient at the writer side (because he'll have to wait for a full synchronize_sched()).

While synchornize_sched() is heavier on the writer side than the originally posted version, it doesn't stall the whole machine and wouldn't introduce latencies to others. Shouldn't that be enough?

Thanks.

Signature · 1 line
tejun
#15 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 11 lines · Tejun Heo

Hello, Srivatsa.

Quoted 3 lines · Srivatsa S. Bhat

This approach (of using synchronize_sched()) also looks good. It is simple, yet effective, but unfortunately inefficient at the writer side (because he'll have to wait for a full synchronize_sched()).

While synchornize_sched() is heavier on the writer side than the originally posted version, it doesn't stall the whole machine and wouldn't introduce latencies to others. Shouldn't that be enough?

Short answer: Yes. But we can do better, with almost comparable code complexity. So I'm tempted to try that out.

Long answer:
Even in the synchronize_sched() approach, we still have to identify the readers who need to be converted to use the new get/put_online_cpus_atomic() APIs and convert them. Then, if we can come up with a scheme such that the writer has to wait only for those readers to complete, then why not?

If such a scheme ends up becoming too complicated, then I agree, we can use synchronize_sched() itself. (That's what I meant by saying that we'll use this as a fallback).

But even in this scheme which uses synchronize_sched(), we are already half-way through (we already use 2 types of sync schemes - counters and rwlocks). Just a little more logic can get rid of the unnecessary full-wait too.. So why not give it a shot?

Regards,
Srivatsa S. Bhat

#16 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Hello,

Quoted 22 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello, Srivatsa.

Quoted 3 lines · Srivatsa S. Bhat

This approach (of using synchronize_sched()) also looks good. It is simple, yet effective, but unfortunately inefficient at the writer side (because he'll have to wait for a full synchronize_sched()).

While synchornize_sched() is heavier on the writer side than the originally posted version, it doesn't stall the whole machine and wouldn't introduce latencies to others. Shouldn't that be enough?

Short answer: Yes. But we can do better, with almost comparable code complexity. So I'm tempted to try that out.

Long answer:
Even in the synchronize_sched() approach, we still have to identify the readers who need to be converted to use the new get/put_online_cpus_atomic() APIs and convert them. Then, if we can come up with a scheme such that the writer has to wait only for those readers to complete, then why not?

Quoted 8 lines

If such a scheme ends up becoming too complicated, then I agree, we can use synchronize_sched() itself. (That's what I meant by saying that we'll use this as a fallback).

But even in this scheme which uses synchronize_sched(), we are already half-way through (we already use 2 types of sync schemes - counters and rwlocks). Just a little more logic can get rid of the unnecessary full-wait too.. So why not give it a shot?

It's not really about the code complexity but making the reader side as light as possible. Please keep in mind that reader side is still *way* more hotter than the writer side. Before, the writer side was heavy to the extent which causes noticeable disruptions on the whole system and I think that's what we're trying to hunt down here. If we can shave of memory barriers from reader side by using synchornized_sched() on writer side, that is the *better* result, not worse.

Thanks.

Signature · 1 line
tejun
#17 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 34 lines · Tejun Heo

Hello,

Quoted 22 lines · Srivatsa S. Bhat
Quoted 11 lines · Tejun Heo

Hello, Srivatsa.

Quoted 3 lines · Srivatsa S. Bhat

This approach (of using synchronize_sched()) also looks good. It is simple, yet effective, but unfortunately inefficient at the writer side (because he'll have to wait for a full synchronize_sched()).

While synchornize_sched() is heavier on the writer side than the originally posted version, it doesn't stall the whole machine and wouldn't introduce latencies to others. Shouldn't that be enough?

Short answer: Yes. But we can do better, with almost comparable code complexity. So I'm tempted to try that out.

Long answer:
Even in the synchronize_sched() approach, we still have to identify the readers who need to be converted to use the new get/put_online_cpus_atomic() APIs and convert them. Then, if we can come up with a scheme such that the writer has to wait only for those readers to complete, then why not?

Quoted 8 lines

If such a scheme ends up becoming too complicated, then I agree, we can use synchronize_sched() itself. (That's what I meant by saying that we'll use this as a fallback).

But even in this scheme which uses synchronize_sched(), we are already half-way through (we already use 2 types of sync schemes - counters and rwlocks). Just a little more logic can get rid of the unnecessary full-wait too.. So why not give it a shot?

Quoted 9 lines

It's not really about the code complexity but making the reader side as light as possible. Please keep in mind that reader side is still *way* more hotter than the writer side. Before, the writer side was heavy to the extent which causes noticeable disruptions on the whole system and I think that's what we're trying to hunt down here. If we can shave of memory barriers from reader side by using synchornized_sched() on writer side, that is the *better* result, not worse.

That's a fair point. Hmmm...
Regards,
Srivatsa S. Bhat

#18 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

In reply to Srivatsa S. Bhat

Damn, sorry for noise. I missed this part...

Quoted 6 lines · Srivatsa S. Bhat
Quoted 1 line · Oleg Nesterov

the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

I hadn't considered that. Thinking of it, I don't think it would help us.. It won't get rid of the currently running preempt_disable() sections no?

Sure. But (again, this is only my feeling so far) given that get_online_cpus_atomic() does cli/sti, this can help to implement ensure-the-readers-must-see-the-pending-writer. IOW this might help to implement sync-with-readers.

Oleg.

#19 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 4 lines · Oleg Nesterov

Damn, sorry for noise. I missed this part...

Quoted 1 line · Srivatsa S. Bhat
Quoted 8 lines
Quoted 5 lines · Oleg Nesterov
Quoted 1 line

the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

I hadn't considered that. Thinking of it, I don't think it would help us.. It won't get rid of the currently running preempt_disable() sections no?

Sure. But (again, this is only my feeling so far) given that get_online_cpus_atomic() does cli/sti,

Ah, that one! Actually, the only reason I do that cli/sti is because, potentially interrupt handlers can be hotplug readers too. So we need to protect the portion of the code of get_online_cpus_atomic() which is not re-entrant.
(Which reminds me to try and reduce the length of cli/sti in that code, if possible).

Quoted 3 lines

this can help to implement ensure-the-readers-must-see-the-pending-writer. IOW this might help to implement sync-with-readers.

2 problems:

1. It won't help with cases like this:

preempt_disable()

	...
	preempt_disable()
		...
			<------- Here
		...
	preempt_enable()
	...

preempt_enable()

If the IPI hits at the point marked above, the IPI is useless, because, at that point, since we are already in a nested read-side critical section, we can't switch the synchronization protocol. We need to wait till we start a fresh non-nested read-side critical section, in order to switch to global rwlock. The reason is that preempt_enable() or put_online_cpus_atomic() can only undo what its predecessor (preempt_disable()/get_online_cpus_atomic()) did.

2. Part of the reason we want to get rid of stop_machine() is to avoid the latency it induces on _all_ CPUs just to take *one* CPU offline. If we use kick_all_cpus_sync(), we get into that territory again : we unfairly interrupt every CPU, _even when_ that CPU's existing preempt_disabled() sections might not actually be hotplug readers! (ie., not bothered about CPU Hotplug).

So, I think whatever synchronization scheme we develop, must not induce the very same problems that stop_machine() had. Otherwise, we can end up going a full-circle and coming back to the same stop_machine() scenario that we intended to get rid of.

(That's part of the reason why I initially tried to provide that _light() variant of the reader APIs in the previous version of the patchset, so that light readers can remain as undisturbed from cpu hotplug as possible, thereby avoiding indirectly inducing the "stop_machine effect", like I mentioned here:
https://lkml.org/lkml/2012/12/5/369)

Regards,
Srivatsa S. Bhat

#20 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 12 lines · Srivatsa S. Bhat
Quoted 10 lines · Oleg Nesterov

Damn, sorry for noise. I missed this part...

Quoted 6 lines · Srivatsa S. Bhat
Quoted 1 line · Oleg Nesterov

the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

I hadn't considered that. Thinking of it, I don't think it would help us.. It won't get rid of the currently running preempt_disable() sections no?

Quoted 6 lines
Quoted 2 lines

Sure. But (again, this is only my feeling so far) given that get_online_cpus_atomic() does cli/sti,

Ah, that one! Actually, the only reason I do that cli/sti is because, potentially interrupt handlers can be hotplug readers too. So we need to protect the portion of the code of get_online_cpus_atomic() which is not re-entrant.

Yes, I understand.

Quoted 8 lines
Quoted 3 lines

this can help to implement ensure-the-readers-must-see-the-pending-writer. IOW this might help to implement sync-with-readers.

2 problems:

1. It won't help with cases like this:

Quoted 9 lines
   preempt_disable()
	...
	preempt_disable()
		...
			<------- Here
		...
	preempt_enable()
	...
   preempt_enable()

No, I meant that kick_all_cpus_sync() can be used to synchronize with cli/sti in get_online_cpus_atomic(), just like synchronize_sched() does in the code I posted a minute ago.

Quoted 5 lines

2. Part of the reason we want to get rid of stop_machine() is to avoid the latency it induces on _all_ CPUs just to take *one* CPU offline. If we use kick_all_cpus_sync(), we get into that territory again : we unfairly interrupt every CPU, _even when_ that CPU's existing preempt_disabled() sections might not actually be hotplug readers! (ie., not bothered about CPU Hotplug).

I agree, that is why I said it is "too heavy".

Oleg.

#21 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 40 lines · Oleg Nesterov
Quoted 12 lines · Srivatsa S. Bhat
Quoted 10 lines · Oleg Nesterov

Damn, sorry for noise. I missed this part...

Quoted 6 lines · Srivatsa S. Bhat
Quoted 1 line · Oleg Nesterov

the latency. And I guess something like kick_all_cpus_sync() is "too heavy".

I hadn't considered that. Thinking of it, I don't think it would help us.. It won't get rid of the currently running preempt_disable() sections no?

Quoted 6 lines
Quoted 2 lines

Sure. But (again, this is only my feeling so far) given that get_online_cpus_atomic() does cli/sti,

Ah, that one! Actually, the only reason I do that cli/sti is because, potentially interrupt handlers can be hotplug readers too. So we need to protect the portion of the code of get_online_cpus_atomic() which is not re-entrant.

Yes, I understand.

Quoted 8 lines
Quoted 3 lines

this can help to implement ensure-the-readers-must-see-the-pending-writer. IOW this might help to implement sync-with-readers.

2 problems:

1. It won't help with cases like this:

Quoted 9 lines
   preempt_disable()
	...
	preempt_disable()
		...
			<------- Here
		...
	preempt_enable()
	...
   preempt_enable()
Quoted 4 lines

No, I meant that kick_all_cpus_sync() can be used to synchronize with cli/sti in get_online_cpus_atomic(), just like synchronize_sched() does in the code I posted a minute ago.

Ah, OK.

Quoted 8 lines
Quoted 5 lines

2. Part of the reason we want to get rid of stop_machine() is to avoid the latency it induces on _all_ CPUs just to take *one* CPU offline. If we use kick_all_cpus_sync(), we get into that territory again : we unfairly interrupt every CPU, _even when_ that CPU's existing preempt_disabled() sections might not actually be hotplug readers! (ie., not bothered about CPU Hotplug).

I agree, that is why I said it is "too heavy".

Got it :)

Regards,
Srivatsa S. Bhat

#22 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

In reply to Srivatsa S. Bhat
Quoted 6 lines · Srivatsa S. Bhat

4. No deadlock possibilities

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

OK, but this assumes that, contrary to what Steven said, read-write-read deadlock is not possible when it comes to rwlock_t. So far I think this is true and we can't deadlock. Steven?

However. If this is true, then compared to preempt_disable/stop_machine livelock is possible. Probably this is fine, we have the same problem with get_online_cpus(). But if we can accept this fact I feel we can simmplify this somehow... Can't prove, only feel ;)

Oleg.

#23 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 10 lines · Oleg Nesterov
Quoted 6 lines · Srivatsa S. Bhat

4. No deadlock possibilities

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

OK, but this assumes that, contrary to what Steven said, read-write-read deadlock is not possible when it comes to rwlock_t.

What I meant is, with a single (global) rwlock, you can't deadlock like that. But if you used per-cpu rwlocks and if we don't implement them properly, then we can end up in circular locking dependencies like shown above.

That is, if you take the same example and replace the lock with global rwlock, you won't deadlock:

Readers:

CPU 0 CPU 1
------ ------

1. spin_lock(&random_lock); read_lock(&my_rwlock);

2. read_lock(&my_rwlock); spin_lock(&random_lock);

Writer:

         CPU 2:
         ------

       write_lock(&my_rwlock);

Even if the writer does a write_lock() in-between steps 1 and 2 at the reader- side, nothing will happen. The writer would spin because CPU 1 already got the rwlock for read, and hence both CPU 0 and 1 go ahead. When they finish, the writer will get the lock and proceed. So, no deadlocks here. So, what I was pointing out here was, if somebody replaced this global rwlock with a "straight-forward" implementation of per-cpu rwlocks, he'll immediately end up in circular locking dependency deadlock between the 3 entities as explained in the link above.

Let me know if my assumptions are incorrect!

Quoted 8 lines

So far I think this
is true and we can't deadlock. Steven?

However. If this is true, then compared to preempt_disable/stop_machine livelock is possible. Probably this is fine, we have the same problem with get_online_cpus(). But if we can accept this fact I feel we can simmplify this somehow... Can't prove, only feel ;)

Not sure I follow..

Anyway, my point is that, we _can't_ implement per-cpu rwlocks like lglocks and expect it to work in this case. IOW, we can't do:

Reader-side:
-> read_lock() your per-cpu rwlock and proceed.

Writer-side:
-> for_each_online_cpu(cpu)

          write_lock(per-cpu rwlock of 'cpu');

Also, like Tejun said, one of the important measures for per-cpu rwlocks should be that, if a user replaces global rwlocks with percpu rwlocks (for performance reasons), he shouldn't suddenly end up in numerous deadlock possibilities which never existed before. The replacement should continue to remain safe, and perhaps improve the performance.

Regards,
Srivatsa S. Bhat

#24 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 6 lines · Srivatsa S. Bhat
Quoted 4 lines · Oleg Nesterov
Quoted 3 lines · Srivatsa S. Bhat

4. No deadlock possibilities

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Per-cpu locking is not the way to go if we want to have relaxed rules for lock-ordering. Because, we can end up in circular-locking dependencies as explained in https://lkml.org/lkml/2012/12/6/290

OK, but this assumes that, contrary to what Steven said, read-write-read deadlock is not possible when it comes to rwlock_t.

What I meant is, with a single (global) rwlock, you can't deadlock like that.

Ah. I greatly misunderstood Steven's email,

http://marc.info/?l=linux-pm&m=135482212307876

Somehow I didn't notice he described the deadlock with _two_ rwlock's, I wrongly thought that his point is that read_lock() is not recursive (like down_read).

Quoted 1 line

Let me know if my assumptions are incorrect!

No, sorry, I misunderstood Steven.

Quoted 6 lines
Quoted 4 lines

However. If this is true, then compared to preempt_disable/stop_machine livelock is possible. Probably this is fine, we have the same problem with get_online_cpus(). But if we can accept this fact I feel we can simmplify this somehow... Can't prove, only feel ;)

Not sure I follow..

I meant that write_lock_irqsave(&hotplug_rwlock) in take_cpu_down() can spin "forever".

Suppose that reader_acked() == T on every CPU, so that get_online_cpus_atomic() always takes read_lock(&hotplug_rwlock).

It is possible that this lock will be never released by readers,

	CPU_0				CPU_1

	get_online_cpus_atomic()
					get_online_cpus_atomic()
	put_online_cpus_atomic()

	get_online_cpus_atomic()
					put_online_cpus_atomic()

					get_online_cpus_atomic()
	put_online_cpus_atomic()

and so on.

Quoted 6 lines

Reader-side:

   -> read_lock() your per-cpu rwlock and proceed.

Writer-side:

   -> for_each_online_cpu(cpu)
          write_lock(per-cpu rwlock of 'cpu');

Yes, yes, this is clear.

Quoted 5 lines

Also, like Tejun said, one of the important measures for per-cpu rwlocks should be that, if a user replaces global rwlocks with percpu rwlocks (for performance reasons), he shouldn't suddenly end up in numerous deadlock possibilities which never existed before. The replacement should continue to remain safe, and perhaps improve the performance.

Sure, I agree.

Oleg.

#25 lore raw

Re: [RFC PATCH v3 1/9] CPU hotplug: Provide APIs to prevent CPU offline from atomic context

Quoted 30 lines · Oleg Nesterov
Quoted 8 lines · Srivatsa S. Bhat
Quoted 4 lines · Oleg Nesterov

However. If this is true, then compared to preempt_disable/stop_machine livelock is possible. Probably this is fine, we have the same problem with get_online_cpus(). But if we can accept this fact I feel we can simmplify this somehow... Can't prove, only feel ;)

Not sure I follow..

I meant that write_lock_irqsave(&hotplug_rwlock) in take_cpu_down() can spin "forever".

Suppose that reader_acked() == T on every CPU, so that get_online_cpus_atomic() always takes read_lock(&hotplug_rwlock).

It is possible that this lock will be never released by readers,

	CPU_0				CPU_1

	get_online_cpus_atomic()
					get_online_cpus_atomic()
	put_online_cpus_atomic()

	get_online_cpus_atomic()
					put_online_cpus_atomic()

					get_online_cpus_atomic()
	put_online_cpus_atomic()
Quoted 2 lines

and so on.

Right, and we can't do anything about it :(
Regards,
Srivatsa S. Bhat

#26 lore raw

RFC PATCH v3 2/9 CPU hotplug: Convert preprocessor macros to static inline functions

In reply to Srivatsa S. Bhat

On 12/05/2012 06:10 AM, Andrew Morton wrote:
"static inline C functions would be preferred if possible. Feel free to fix up the wrong crufty surrounding code as well ;-)"

Convert the macros in the CPU hotplug code to static inline C functions.

---

 include/linux/cpu.h |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/cpu.h +4 -4
index cf24da1..eb79f47 100644
@@ -198,10 +198,10 @@ static inline void cpu_hotplug_driver_unlock(void)
 
 #else		/* CONFIG_HOTPLUG_CPU */
 
-#define get_online_cpus()	do { } while (0)
-#define put_online_cpus()	do { } while (0)
-#define get_online_cpus_atomic()	do { } while (0)
-#define put_online_cpus_atomic()	do { } while (0)
+static inline void get_online_cpus(void) {}
+static inline void put_online_cpus(void) {}
+static inline void get_online_cpus_atomic(void) {}
+static inline void put_online_cpus_atomic(void) {}
 #define hotcpu_notifier(fn, pri)	do { (void)(fn); } while (0)
 /* These aren't inline functions due to a GCC bug. */
 #define register_hotcpu_notifier(nb)	({ (void)(nb); 0; })
#27 lore raw

RFC PATCH v3 3/9 smp, cpu hotplug: Fix smp_call_function_*() to prevent CPU offline properly

In reply to Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on preempt_disable() to prevent CPUs from going offline from under us.

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

---

 kernel/smp.c |   38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
kernel/smp.c +25 -13
index 29dd40a..ce1a866 100644
@@ -310,7 +310,8 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
 	 * prevent preemption and reschedule on another processor,
 	 * as well as CPU removal
 	 */
-	this_cpu = get_cpu();
+	get_online_cpus_atomic();
+	this_cpu = smp_processor_id();
 
 	/*
 	 * Can deadlock when called with interrupts disabled.
@@ -342,7 +343,7 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
 		}
 	}
 
-	put_cpu();
+	put_online_cpus_atomic();
 
 	return err;
 }
@@ -371,8 +372,10 @@ int smp_call_function_any(const struct cpumask *mask,
 	const struct cpumask *nodemask;
 	int ret;
 
+	get_online_cpus_atomic();
 	/* Try for same CPU (cheapest) */
-	cpu = get_cpu();
+	cpu = smp_processor_id();
+
 	if (cpumask_test_cpu(cpu, mask))
 		goto call;
 
@@ -388,7 +391,7 @@ int smp_call_function_any(const struct cpumask *mask,
 	cpu = cpumask_any_and(mask, cpu_online_mask);
 call:
 	ret = smp_call_function_single(cpu, func, info, wait);
-	put_cpu();
+	put_online_cpus_atomic();
 	return ret;
 }
 EXPORT_SYMBOL_GPL(smp_call_function_any);
@@ -409,14 +412,17 @@ void __smp_call_function_single(int cpu, struct call_single_data *data,
 	unsigned int this_cpu;
 	unsigned long flags;
 
-	this_cpu = get_cpu();
+	get_online_cpus_atomic();
+
+	this_cpu = smp_processor_id();
+
 	/*
 	 * Can deadlock when called with interrupts disabled.
 	 * We allow cpu's that are not yet online though, as no one else can
 	 * send smp call function interrupt to this cpu and as such deadlocks
 	 * can't happen.
 	 */
-	WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
+	WARN_ON_ONCE(cpu_online(this_cpu) && wait && irqs_disabled()
 		     && !oops_in_progress);
 
 	if (cpu == this_cpu) {
@@ -427,7 +433,7 @@ void __smp_call_function_single(int cpu, struct call_single_data *data,
 		csd_lock(data);
 		generic_exec_single(cpu, data, wait);
 	}
-	put_cpu();
+	put_online_cpus_atomic();
 }
 
 /**
@@ -451,6 +457,8 @@ void smp_call_function_many(const struct cpumask *mask,
 	unsigned long flags;
 	int refs, cpu, next_cpu, this_cpu = smp_processor_id();
 
+	get_online_cpus_atomic();
+
 	/*
 	 * Can deadlock when called with interrupts disabled.
 	 * We allow cpu's that are not yet online though, as no one else can
@@ -467,17 +475,18 @@ void smp_call_function_many(const struct cpumask *mask,
 
 	/* No online cpus?  We're done. */
 	if (cpu >= nr_cpu_ids)
-		return;
+		goto out_unlock;
 
 	/* Do we have another CPU which isn't us? */
 	next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
 	if (next_cpu == this_cpu)
-		next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
+		next_cpu = cpumask_next_and(next_cpu, mask,
+						cpu_online_mask);
 
 	/* Fastpath: do that cpu by itself. */
 	if (next_cpu >= nr_cpu_ids) {
 		smp_call_function_single(cpu, func, info, wait);
-		return;
+		goto out_unlock;
 	}
 
 	data = &__get_cpu_var(cfd_data);
@@ -523,7 +532,7 @@ void smp_call_function_many(const struct cpumask *mask,
 	/* Some callers race with other cpus changing the passed mask */
 	if (unlikely(!refs)) {
 		csd_unlock(&data->csd);
-		return;
+		goto out_unlock;
 	}
 
 	raw_spin_lock_irqsave(&call_function.lock, flags);
@@ -554,6 +563,9 @@ void smp_call_function_many(const struct cpumask *mask,
 	/* Optionally wait for the CPUs to complete */
 	if (wait)
 		csd_lock_wait(&data->csd);
+
+out_unlock:
+	put_online_cpus_atomic();
 }
 EXPORT_SYMBOL(smp_call_function_many);
 
@@ -574,9 +586,9 @@ EXPORT_SYMBOL(smp_call_function_many);
  */
 int smp_call_function(smp_call_func_t func, void *info, int wait)
 {
-	preempt_disable();
+	get_online_cpus_atomic();
 	smp_call_function_many(cpu_online_mask, func, info, wait);
-	preempt_enable();
+	put_online_cpus_atomic();
 
 	return 0;
 }
#28 lore raw

RFC PATCH v3 4/9 smp, cpu hotplug: Fix on_each_cpu_*() to prevent CPU offline properly

In reply to Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on preempt_disable() to prevent CPUs from going offline from under us.

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

---

 kernel/smp.c |   25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
kernel/smp.c +15 -10
index ce1a866..0031000 100644
@@ -688,12 +688,12 @@ int on_each_cpu(void (*func) (void *info), void *info, int wait)
 	unsigned long flags;
 	int ret = 0;
 
-	preempt_disable();
+	get_online_cpus_atomic();
 	ret = smp_call_function(func, info, wait);
 	local_irq_save(flags);
 	func(info);
 	local_irq_restore(flags);
-	preempt_enable();
+	put_online_cpus_atomic();
 	return ret;
 }
 EXPORT_SYMBOL(on_each_cpu);
@@ -715,7 +715,11 @@ EXPORT_SYMBOL(on_each_cpu);
 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
 			void *info, bool wait)
 {
-	int cpu = get_cpu();
+	int cpu;
+
+	get_online_cpus_atomic();
+
+	cpu = smp_processor_id();
 
 	smp_call_function_many(mask, func, info, wait);
 	if (cpumask_test_cpu(cpu, mask)) {
@@ -723,7 +727,7 @@ void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
 		func(info);
 		local_irq_enable();
 	}
-	put_cpu();
+	put_online_cpus_atomic();
 }
 EXPORT_SYMBOL(on_each_cpu_mask);
 
@@ -748,8 +752,9 @@ EXPORT_SYMBOL(on_each_cpu_mask);
  * The function might sleep if the GFP flags indicates a non
  * atomic allocation is allowed.
  *
- * Preemption is disabled to protect against CPUs going offline but not online.
- * CPUs going online during the call will not be seen or sent an IPI.
+ * We use get/put_online_cpus_atomic() to prevent CPUs from going
+ * offline in-between our operation. CPUs coming online during the
+ * call will not be seen or sent an IPI.
  *
  * You must not call this function with disabled interrupts or
  * from a hardware interrupt handler or from a bottom half handler.
@@ -764,26 +769,26 @@ void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
 	might_sleep_if(gfp_flags & __GFP_WAIT);
 
 	if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
-		preempt_disable();
+		get_online_cpus_atomic();
 		for_each_online_cpu(cpu)
 			if (cond_func(cpu, info))
 				cpumask_set_cpu(cpu, cpus);
 		on_each_cpu_mask(cpus, func, info, wait);
-		preempt_enable();
+		put_online_cpus_atomic();
 		free_cpumask_var(cpus);
 	} else {
 		/*
 		 * No free cpumask, bother. No matter, we'll
 		 * just have to IPI them one by one.
 		 */
-		preempt_disable();
+		get_online_cpus_atomic();
 		for_each_online_cpu(cpu)
 			if (cond_func(cpu, info)) {
 				ret = smp_call_function_single(cpu, func,
 								info, wait);
 				WARN_ON_ONCE(!ret);
 			}
-		preempt_enable();
+		put_online_cpus_atomic();
 	}
 }
 EXPORT_SYMBOL(on_each_cpu_cond);
#29 lore raw

RFC PATCH v3 5/9 sched, cpu hotplug: Use stable online cpus in try_to_wake_up() & select_task_rq()

In reply to Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on preempt_disable() to prevent CPUs from going offline from under us.

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

Scheduler functions such as try_to_wake_up() and select_task_rq() (and even select_fallback_rq()) deal with picking new CPUs to run tasks. So they need to synchronize with CPU offline operations.

---

 kernel/sched/core.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
kernel/sched/core.c +14 -0
index 2d8927f..f51e0aa 100644
@@ -1103,6 +1103,10 @@ EXPORT_SYMBOL_GPL(kick_process);
 #ifdef CONFIG_SMP
 /*
  * ->cpus_allowed is protected by both rq->lock and p->pi_lock
+ *
+ *  Must be called under get/put_online_cpus_atomic() or
+ *  equivalent, to avoid CPUs from going offline from underneath
+ *  us.
  */
 static int select_fallback_rq(int cpu, struct task_struct *p)
 {
@@ -1166,6 +1170,9 @@ out:
 
 /*
  * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
+ *
+ * Must be called under get/put_online_cpus_atomic(), to prevent
+ * CPUs from going offline from underneath us.
  */
 static inline
 int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
@@ -1406,6 +1413,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 	int cpu, success = 0;
 
 	smp_wmb();
+	get_online_cpus_atomic();
 	raw_spin_lock_irqsave(&p->pi_lock, flags);
 	if (!(p->state & state))
 		goto out;
@@ -1446,6 +1454,7 @@ stat:
 	ttwu_stat(p, cpu, wake_flags);
 out:
 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
+	put_online_cpus_atomic();
 
 	return success;
 }
@@ -1624,6 +1633,7 @@ void wake_up_new_task(struct task_struct *p)
 	unsigned long flags;
 	struct rq *rq;
 
+	get_online_cpus_atomic();
 	raw_spin_lock_irqsave(&p->pi_lock, flags);
 #ifdef CONFIG_SMP
 	/*
@@ -1644,6 +1654,7 @@ void wake_up_new_task(struct task_struct *p)
 		p->sched_class->task_woken(rq, p);
 #endif
 	task_rq_unlock(rq, p, &flags);
+	put_online_cpus_atomic();
 }
 
 #ifdef CONFIG_PREEMPT_NOTIFIERS
@@ -2541,6 +2552,7 @@ void sched_exec(void)
 	unsigned long flags;
 	int dest_cpu;
 
+	get_online_cpus_atomic();
 	raw_spin_lock_irqsave(&p->pi_lock, flags);
 	dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
 	if (dest_cpu == smp_processor_id())
@@ -2550,11 +2562,13 @@ void sched_exec(void)
 		struct migration_arg arg = { p, dest_cpu };
 
 		raw_spin_unlock_irqrestore(&p->pi_lock, flags);
+		put_online_cpus_atomic();
 		stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
 		return;
 	}
 unlock:
 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
+	put_online_cpus_atomic();
 }
 
 #endif
#30 lore raw

RFC PATCH v3 6/9 kick_process(), cpu-hotplug: Prevent offlining of target CPU properly

In reply to Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on preempt_disable() to prevent CPUs from going offline from under us.

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

---

 kernel/sched/core.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/core.c +2 -2
index f51e0aa..cff7656 100644
@@ -1091,11 +1091,11 @@ void kick_process(struct task_struct *p)
 {
 	int cpu;
 
-	preempt_disable();
+	get_online_cpus_atomic();
 	cpu = task_cpu(p);
 	if ((cpu != smp_processor_id()) && task_curr(p))
 		smp_send_reschedule(cpu);
-	preempt_enable();
+	put_online_cpus_atomic();
 }
 EXPORT_SYMBOL_GPL(kick_process);
 #endif /* CONFIG_SMP */
#31 lore raw

RFC PATCH v3 7/9 yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly

In reply to Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on local_irq_save() to prevent CPUs from going offline from under us.

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

---

 kernel/sched/core.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/core.c +3 -1
index cff7656..4b982bf 100644
@@ -4312,6 +4312,7 @@ bool __sched yield_to(struct task_struct *p, bool preempt)
 	unsigned long flags;
 	bool yielded = 0;
 
+	get_online_cpus_atomic();
 	local_irq_save(flags);
 	rq = this_rq();
 
@@ -4339,13 +4340,14 @@ again:
 		 * Make p's CPU reschedule; pick_next_entity takes care of
 		 * fairness.
 		 */
-		if (preempt && rq != p_rq)
+		if (preempt && rq != p_rq && cpu_online(task_cpu(p)))
 			resched_task(p_rq->curr);
 	}
 
 out:
 	double_rq_unlock(rq, p_rq);
 	local_irq_restore(flags);
+	put_online_cpus_atomic();
 
 	if (yielded)
 		schedule();
#32 lore raw

Re: [RFC PATCH v3 7/9] yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly

Quoted 3 lines · Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on local_irq_save() to prevent CPUs from going offline from under us.

OK, I guess we need to avoid resched_task()->smp_send_reschedule() after __cpu_disable() and before migrate_tasks().

But, whatever problem we have,

Quoted 2 lines

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

it should be solved, so...

kernel/sched/core.c again: · line 4343

Quoted 2 lines
kernel/sched/core.c +1 -1
-		if (preempt && rq != p_rq)
+		if (preempt && rq != p_rq && cpu_online(task_cpu(p)))

Why do we need this change?

Afaics, you could add BUG_ON(!cpu_online(...)) instead?

I am just curious.

Oleg.

#33 lore raw

Re: [RFC PATCH v3 7/9] yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly

Quoted 8 lines · Oleg Nesterov
Quoted 3 lines · Srivatsa S. Bhat

Once stop_machine() is gone from the CPU offline path, we won't be able to depend on local_irq_save() to prevent CPUs from going offline from under us.

OK, I guess we need to avoid resched_task()->smp_send_reschedule() after __cpu_disable() and before migrate_tasks().

Yes.

Quoted 7 lines

But, whatever problem we have,

Quoted 2 lines

Use the get/put_online_cpus_atomic() APIs to prevent CPUs from going offline, while invoking from atomic context.

it should be solved, so...

Quoted 9 lines

kernel/sched/core.c again: · line 4343

Quoted 2 lines
kernel/sched/core.c +1 -1
-		if (preempt && rq != p_rq)
+		if (preempt && rq != p_rq && cpu_online(task_cpu(p)))

Why do we need this change?

Afaics, you could add BUG_ON(!cpu_online(...)) instead?

I am just curious.

Oh, I think that's a remnant of v1 (which needed readers to use cpu_online_stable()). You're right, we don't need it. Or we could put a BUG_ON instead, like you suggested.

Regards,
Srivatsa S. Bhat

#34 lore raw

Re: [RFC PATCH v3 7/9] yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly

Quoted 7 lines · Srivatsa S. Bhat
Quoted 5 lines · Oleg Nesterov

kernel/sched/core.c again: · line 4343

Quoted 2 lines
kernel/sched/core.c +1 -1
-		if (preempt && rq != p_rq)
+		if (preempt && rq != p_rq && cpu_online(task_cpu(p)))

Why do we need this change?

Quoted 7 lines
Quoted 4 lines

Afaics, you could add BUG_ON(!cpu_online(...)) instead?

I am just curious.

Oh, I think that's a remnant of v1 (which needed readers to use cpu_online_stable()). You're right, we don't need it.

Ah OK, thanks.

Quoted 2 lines

Or we could put a
BUG_ON instead, like you suggested.

IMHO it would be better to simply drop this chunk.

Oleg.

#35 lore raw

Re: [RFC PATCH v3 7/9] yield_to(), cpu-hotplug: Prevent offlining of other CPUs properly

Quoted 16 lines · Oleg Nesterov
Quoted 7 lines · Srivatsa S. Bhat
Quoted 5 lines · Oleg Nesterov

kernel/sched/core.c again: · line 4343

Quoted 2 lines
kernel/sched/core.c +1 -1
-		if (preempt && rq != p_rq)
+		if (preempt && rq != p_rq && cpu_online(task_cpu(p)))

Why do we need this change?

Quoted 7 lines
Quoted 4 lines

Afaics, you could add BUG_ON(!cpu_online(...)) instead?

I am just curious.

Oh, I think that's a remnant of v1 (which needed readers to use cpu_online_stable()). You're right, we don't need it.

Quoted 7 lines

Ah OK, thanks.

Quoted 2 lines

Or we could put a
BUG_ON instead, like you suggested.

IMHO it would be better to simply drop this chunk.

Sure, will drop it. Its distracting, if nothing else ;-)

Regards,
Srivatsa S. Bhat

#36 lore raw

RFC PATCH v3 8/9 kvm, vmx: Add atomic synchronization with CPU Hotplug

In reply to Srivatsa S. Bhat

preempt_disable() will no longer help prevent CPUs from going offline, once stop_machine() gets removed from the CPU offline path. So use get/put_online_cpus_atomic() in vmx_vcpu_load() to prevent CPUs from going offline while clearing vmcs.

---

 arch/x86/kvm/vmx.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +6 -2
index f858159..d8a4cf1 100644
@@ -1519,10 +1519,14 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
 
-	if (!vmm_exclusive)
+	if (!vmm_exclusive) {
 		kvm_cpu_vmxon(phys_addr);
-	else if (vmx->loaded_vmcs->cpu != cpu)
+	} else if (vmx->loaded_vmcs->cpu != cpu) {
+		/* Prevent any CPU from going offline */
+		get_online_cpus_atomic();
 		loaded_vmcs_clear(vmx->loaded_vmcs);
+		put_online_cpus_atomic();
+	}
 
 	if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
 		per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
#37 lore raw

RFC PATCH v3 9/9 cpu: No more __stop_machine() in _cpu_down()

In reply to Srivatsa S. Bhat

From: Paul E. McKenney <[email protected]>

The _cpu_down() function invoked as part of the CPU-hotplug offlining process currently invokes __stop_machine(), which is slow and inflicts substantial real-time latencies on the entire system. This patch substitutes stop_one_cpu() for __stop_machine() in order to improve both performance and real-time latency.

This is currently unsafe, because there are a number of uses of preempt_disable() that are intended to block CPU-hotplug offlining. These will be fixed by using get/put_online_cpus_atomic(), but in the meantime, this commit is one way to help locate them.

[ [email protected]: Refer to the new sync primitives for
readers (in the changelog), and s/stop_cpus/stop_one_cpu ]

---

 kernel/cpu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cpu.c +1 -1
index d2076fa..71c65dc 100644
@@ -532,7 +532,7 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
 	}
 	smpboot_park_threads(cpu);
 
-	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
+	err = stop_one_cpu(cpu, take_cpu_down, &tcd_param);
 	if (err) {
 		/* CPU didn't die: tell everyone.  Can't complain. */
 		smpboot_unpark_threads(cpu);