Subject: [PATCH 13/16] smp: add func to IPI cpus based on parameter func
From: Sebastian Andrzej Siewior <[email-masked]>
To: Steven Rostedt <[email-masked]>
Cc: [email-masked], [email-masked], Carsten Emde <[email-masked]>, Gilad Ben-Yossef <[email-masked]>, Chris Metcalf <[email-masked]>, Christoph Lameter <[email-masked]>, Frederic Weisbecker <[email-masked]>, Russell King <[email-masked]>, Pekka Enberg <[email-masked]>, Matt Mackall <[email-masked]>, Sasha Levin <[email-masked]>, Rik van Riel <[email-masked]>, Andi Kleen <[email-masked]>, Alexander Viro <[email-masked]>, Avi Kivity <[email-masked]>, Kosaki Motohiro <[email-masked]>, Milton Miller
Date: Wed, 13 Feb 2013 17:12:08 +0100
List-Id: <linux-rt-users.vger.kernel.org>

From: Gilad Ben-Yossef <[email-masked]>

Add the on_each_cpu_cond() function that wraps on_each_cpu_mask() and
calculates the cpumask of cpus to IPI by calling a function supplied as a
parameter in order to determine whether to IPI each specific cpu.

The function works around allocation failure of cpumask variable in
CONFIG_CPUMASK_OFFSTACK=y by itereating over cpus sending an IPI a time
via smp_call_function_single().

The function is useful since it allows to seperate the specific code that
decided in each case whether to IPI a specific cpu for a specific request
from the common boilerplate code of handling creating the mask, handling
failures etc.

[[email-masked]: s/gfpflags/gfp_flags/]
[[email-masked]: avoid double-evaluation of `info' (per Michal), parenthesise evaluation of `cond_func']
[[email-masked]: s/CPU/CPUs, use all 80 cols in comment]
Signed-off-by: Gilad Ben-Yossef <[email-masked]>
Cc: Chris Metcalf <[email-masked]>
Cc: Christoph Lameter <[email-masked]>
Acked-by: Peter Zijlstra <[email-masked]>
Cc: Frederic Weisbecker <[email-masked]>
Cc: Russell King <[email-masked]>
Cc: Pekka Enberg <[email-masked]>
Cc: Matt Mackall <[email-masked]>
Cc: Sasha Levin <[email-masked]>
Cc: Rik van Riel <[email-masked]>
Cc: Andi Kleen <[email-masked]>
Cc: Alexander Viro <[email-masked]>
Cc: Avi Kivity <[email-masked]>
Acked-by: Michal Nazarewicz <[email-masked]>
Cc: Kosaki Motohiro <[email-masked]>
Cc: Milton Miller <[email-masked]>
Reviewed-by: "Srivatsa S. Bhat" <[email-masked]>
Signed-off-by: Andrew Morton <[email-masked]>
Signed-off-by: Linus Torvalds <[email-masked]>

Signed-off-by: Sebastian Andrzej Siewior <[email-masked]>
---
 include/linux/smp.h |   24 ++++++++++++++++++++
 kernel/smp.c        |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

Index: linux-3.2.35-rt53/include/linux/smp.h
===================================================================
--- linux-3.2.35-rt53.orig/include/linux/smp.h
+++ linux-3.2.35-rt53/include/linux/smp.h
@@ -108,6 +108,15 @@ void on_each_cpu_mask(const struct cpuma
 		void *info, bool wait);
 
 /*
+ * Call a function on each processor for which the supplied function
+ * cond_func returns a positive value. This may include the local
+ * processor.
+ */
+void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
+		smp_call_func_t func, void *info, bool wait,
+		gfp_t gfp_flags);
+
+/*
  * Mark the boot cpu "online" so that it can call console drivers in
  * printk() and can access its per-cpu storage.
  */
@@ -152,6 +161,21 @@ static inline int up_smp_call_function(s
 			local_irq_enable();		\
 		}					\
 	} while (0)
+/*
+ * Preemption is disabled here to make sure the cond_func is called under the
+ * same condtions in UP and SMP.
+ */
+#define on_each_cpu_cond(cond_func, func, info, wait, gfp_flags)\
+	do {							\
+		void *__info = (info);				\
+		preempt_disable();				\
+		if ((cond_func)(0, __info)) {			\
+			local_irq_disable();			\
+			(func)(__info);				\
+			local_irq_enable();			\
+		}						\
+		preempt_enable();				\
+	} while (0)
 
 static inline void smp_send_reschedule(int cpu) { }
 #define num_booting_cpus()			1
Index: linux-3.2.35-rt53/kernel/smp.c
===================================================================
--- linux-3.2.35-rt53.orig/kernel/smp.c
+++ linux-3.2.35-rt53/kernel/smp.c
@@ -741,3 +741,64 @@ void on_each_cpu_mask(const struct cpuma
 	put_cpu();
 }
 EXPORT_SYMBOL(on_each_cpu_mask);
+
+/*
+ * on_each_cpu_cond(): Call a function on each processor for which
+ * the supplied function cond_func returns true, optionally waiting
+ * for all the required CPUs to finish. This may include the local
+ * processor.
+ * @cond_func:	A callback function that is passed a cpu id and
+ *		the the info parameter. The function is called
+ *		with preemption disabled. The function should
+ *		return a blooean value indicating whether to IPI
+ *		the specified CPU.
+ * @func:	The function to run on all applicable CPUs.
+ *		This must be fast and non-blocking.
+ * @info:	An arbitrary pointer to pass to both functions.
+ * @wait:	If true, wait (atomically) until function has
+ *		completed on other CPUs.
+ * @gfp_flags:	GFP flags to use when allocating the cpumask
+ *		used internally by the function.
+ *
+ * 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.
+ *
+ * You must not call this function with disabled interrupts or
+ * from a hardware interrupt handler or from a bottom half handler.
+ */
+void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
+			smp_call_func_t func, void *info, bool wait,
+			gfp_t gfp_flags)
+{
+	cpumask_var_t cpus;
+	int cpu, ret;
+
+	might_sleep_if(gfp_flags & __GFP_WAIT);
+
+	if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
+		preempt_disable();
+		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();
+		free_cpumask_var(cpus);
+	} else {
+		/*
+		 * No free cpumask, bother. No matter, we'll
+		 * just have to IPI them one by one.
+		 */
+		preempt_disable();
+		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();
+	}
+}
+EXPORT_SYMBOL(on_each_cpu_cond);