Commit 0e8d6a93 authored by Thomas Gleixner's avatar Thomas Gleixner
Browse files

workqueue: Provide work_on_cpu_safe()

work_on_cpu() is not protected against CPU hotplug. For code which requires
to be either executed on an online CPU or to fail if the CPU is not
available the callsite would have to protect against CPU hotplug.

Provide a function which does get/put_online_cpus() around the call to
work_on_cpu() and fails the call with -ENODEV if the target CPU is not

Preparatory patch to convert several racy task affinity manipulations.
Signed-off-by: default avatarThomas Gleixner <>
Acked-by: default avatarTejun Heo <>
Cc: Fenghua Yu <>
Cc: Tony Luck <>
Cc: Herbert Xu <>
Cc: "Rafael J. Wysocki" <>
Cc: Peter Zijlstra <>
Cc: Benjamin Herrenschmidt <>
Cc: Sebastian Siewior <>
Cc: Lai Jiangshan <>
Cc: Viresh Kumar <>
Cc: Michael Ellerman <>
Cc: "David S. Miller" <>
Cc: Len Brown <>

Signed-off-by: default avatarThomas Gleixner <>
parent 048c9b95
......@@ -608,8 +608,13 @@ static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
return fn(arg);
static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
return fn(arg);
long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
#endif /* CONFIG_SMP */
......@@ -4735,6 +4735,29 @@ long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
return wfc.ret;
* work_on_cpu_safe - run a function in thread context on a particular cpu
* @cpu: the cpu to run on
* @fn: the function to run
* @arg: the function argument
* Disables CPU hotplug and calls work_on_cpu(). The caller must not hold
* any locks which would prevent @fn from completing.
* Return: The value @fn returns.
long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
long ret = -ENODEV;
if (cpu_online(cpu))
ret = work_on_cpu(cpu, fn, arg);
return ret;
#endif /* CONFIG_SMP */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment