[PATCH 7/8] cpu_hotplug: Introduce try_get_online_cpus()

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Gautham R Shenoy
Date: Tuesday, April 29, 2008 - 6:03 am

cpu_hotplug: Introduce try_get_online_cpus()

From: Gautham R Shenoy <ego@in.ibm.com>

Subsystems such as workqueues currently cannot use get_online_cpus()
since they deadlock with the workqueue CPU-Hotplug callback code.

For such cases, introduce a new API try_get_online_cpus() which
returns 0 when a cpu-hotplug operation is in progress. It behaves
like get_online_cpus() and returns 1 otherwise.

Based on the status of CPU-Hotplug, the subsystems can take appropriate
action.

Signed-off-by: Gautham R Shenoy <ego@in.ibm.com>
---

 include/linux/cpu.h |    2 ++
 kernel/cpu.c        |   22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/include/linux/cpu.h b/include/linux/cpu.h
index 0be8d65..d0be341 100644
--- a/include/linux/cpu.h
+++ b/include/linux/cpu.h
@@ -107,6 +107,7 @@ static inline void cpuhotplug_mutex_unlock(struct mutex *cpu_hp_mutex)
 }
 
 extern void get_online_cpus(void);
+extern int try_get_online_cpus(void);
 extern void put_online_cpus(void);
 #define hotcpu_notifier(fn, pri) {				\
 	static struct notifier_block fn##_nb =			\
@@ -126,6 +127,7 @@ static inline void cpuhotplug_mutex_unlock(struct mutex *cpu_hp_mutex)
 
 #define get_online_cpus()	do { } while (0)
 #define put_online_cpus()	do { } while (0)
+static inline int try_get_online_cpus(void) { return 1; }
 #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; })
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 8f1718f..52c1e4e 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -57,7 +57,29 @@ void __init cpu_hotplug_init(void)
 }
 
 #ifdef CONFIG_HOTPLUG_CPU
+int try_get_online_cpus(void)
+{
+	spin_lock(&cpu_hotplug.lock);
+	if (cpu_hotplug.active_writer == current)
+		goto out_unlock;
+
+	if (likely(!cpu_hotplug.active_writer))
+		goto out_success;
+
+	if (cpu_hotplug.refcount)
+		goto out_success;
+
+	spin_unlock(&cpu_hotplug.lock);
+	return 0;
+
+out_success:
+	cpu_hotplug.refcount++;
+out_unlock:
+	spin_unlock(&cpu_hotplug.lock);
+	cpu_hotplug_acquire_read(&cpu_hotplug.dep_map, 0, 1, _THIS_IP_);
+	return 1;
 
+}
 void get_online_cpus(void)
 {
 	might_sleep();
-- 
Thanks and Regards
gautham
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 1/8] lockdep: fix recursive read lock validation, Gautham R Shenoy, (Tue Apr 29, 5:57 am)
[PATCH 2/8] lockdep: reader-in-writer recursion, Gautham R Shenoy, (Tue Apr 29, 5:58 am)
[PATCH 3/8] lockdep: fix fib_hash softirq inversion, Gautham R Shenoy, (Tue Apr 29, 6:00 am)
[PATCH 4/8] net: af_netlink: deadlock, Gautham R Shenoy, (Tue Apr 29, 6:01 am)
[PATCH 5/8] cpu: cpu-hotplug deadlock, Gautham R Shenoy, (Tue Apr 29, 6:02 am)
[PATCH 6/8] lockdep: annotate cpu_hotplug, Gautham R Shenoy, (Tue Apr 29, 6:02 am)
[PATCH 7/8] cpu_hotplug: Introduce try_get_online_cpus(), Gautham R Shenoy, (Tue Apr 29, 6:03 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Bart Van Assche, (Tue Apr 29, 6:16 am)
Re: Hans Reiser, reiserfs developer, linux-os (Dick Johnson), (Tue Apr 29, 6:19 am)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Oleg Nesterov, (Tue Apr 29, 7:33 am)
Re: [PATCH 3/8] lockdep: fix fib_hash softirq inversion, Peter Zijlstra, (Tue Apr 29, 7:45 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Bart Van Assche, (Tue Apr 29, 8:03 am)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Peter Zijlstra, (Tue Apr 29, 8:09 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Bart Van Assche, (Tue Apr 29, 9:03 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Bart Van Assche, (Tue Apr 29, 9:29 am)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Oleg Nesterov, (Tue Apr 29, 9:45 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Peter Zijlstra, (Tue Apr 29, 10:04 am)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Peter Zijlstra, (Tue Apr 29, 10:31 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Bart Van Assche, (Tue Apr 29, 10:45 am)
Re: [PATCH 1/8] lockdep: fix recursive read lock validation, Peter Zijlstra, (Tue Apr 29, 10:58 am)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Gautham R Shenoy, (Tue Apr 29, 10:37 pm)
Re: [PATCH 5/8] cpu: cpu-hotplug deadlock, Oleg Nesterov, (Wed Apr 30, 4:43 am)