[PATCH v1 3/6] workqueue: introduce schedule_on_each_cpu_cond

From: Gilad Ben-Yossef
Date: Thu May 03 2012 - 10:58:29 EST


Introduce the schedule_on_each_cpu_cond() function that schedules
a work item on each online CPU for which the supplied condition
function returns true.

This function should be used instead of schedule_on_each_cpu()
when only some of the CPUs have actual work to do and a predicate
function can tell if a certain CPU does or does not have work to do,
thus saving unneeded wakeups and schedules.

Signed-off-by: Gilad Ben-Yossef <gilad@xxxxxxxxxxxxx>
CC: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
CC: Tejun Heo <tj@xxxxxxxxxx>
CC: John Stultz <johnstul@xxxxxxxxxx>
CC: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
CC: KOSAKI Motohiro <kosaki.motohiro@xxxxxxxxxxxxxx>
CC: Mel Gorman <mel@xxxxxxxxx>
CC: Mike Frysinger <vapier@xxxxxxxxxx>
CC: David Rientjes <rientjes@xxxxxxxxxx>
CC: Hugh Dickins <hughd@xxxxxxxxxx>
CC: Minchan Kim <minchan.kim@xxxxxxxxx>
CC: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxx>
CC: Christoph Lameter <cl@xxxxxxxxx>
CC: Chris Metcalf <cmetcalf@xxxxxxxxxx>
CC: Hakan Akkan <hakanakkan@xxxxxxxxx>
CC: Max Krasnyansky <maxk@xxxxxxxxxxxx>
CC: Frederic Weisbecker <fweisbec@xxxxxxxxx>
CC: linux-kernel@xxxxxxxxxxxxxxx
CC: linux-mm@xxxxxxxxx
---
include/linux/workqueue.h | 2 ++
kernel/workqueue.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 20da95a..d7bb104 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -385,6 +385,8 @@ extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
extern int schedule_on_each_cpu(work_func_t func);
extern int schedule_on_each_cpu_mask(work_func_t func,
const struct cpumask *mask);
+extern int schedule_on_each_cpu_cond(work_func_t func,
+ bool (*cond_func)(int cpu));
extern int keventd_up(void);

int execute_in_process_context(work_func_t fn, struct execute_work *);
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 1c9782b..3322d30 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2828,6 +2828,43 @@ int schedule_on_each_cpu_mask(work_func_t func, const struct cpumask *mask)
}

/**
+ * schedule_on_each_cpu_cond - execute a function synchronously on each
+ * online CPU for which the supplied condition function returns true
+ * @func: the function to run on the selected CPUs
+ * @cond_func: the function to call to select the CPUs
+ *
+ * schedule_on_each_cpu_cond() executes @func on each online CPU for
+ * @cond_func returns true using the system workqueue and blocks until
+ * all CPUs have completed.
+ * schedule_on_each_cpu_cond() is very slow.
+ *
+ * RETURNS:
+ * 0 on success, -errno on failure.
+ */
+int schedule_on_each_cpu_cond(work_func_t func, bool (*cond_func)(int cpu))
+{
+ int cpu, ret;
+ cpumask_var_t mask;
+
+ if (unlikely(!zalloc_cpumask_var(&mask, GFP_KERNEL)))
+ return -ENOMEM;
+
+ get_online_cpus();
+
+ for_each_online_cpu(cpu)
+ if (cond_func(cpu))
+ cpumask_set_cpu(cpu, mask);
+
+ ret = schedule_on_each_cpu_mask(func, mask);
+
+ put_online_cpus();
+
+ free_cpumask_var(mask);
+
+ return ret;
+}
+
+/**
* schedule_on_each_cpu - execute a function synchronously on each online CPU
* @func: the function to call
*
--
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/