[PATCH v2 1/2] smpboot: Allow restricting per-CPU threads to a CPU mask

From: Daniel Lezcano

Date: Wed Nov 18 2026 - 05:34:22 EST


smpboot_register_percpu_thread() creates a thread for every online CPU
and registers the descriptor for subsequent CPU hotplug operations.
There is currently no way to restrict these threads to a subset of
CPUs, even when the user only needs work performed on that subset.

For example, an idle injection device targets a specific CPU mask.
Creating threads outside that mask consumes resources unnecessarily.
Supporting multiple such devices also requires each descriptor to
manage only the threads associated with its own CPUs.

Add an optional CPU mask to struct smp_hotplug_thread and apply it
throughout the thread lifecycle. Registration creates threads only for
online CPUs in the mask. CPU bring-up creates threads for selected
CPUs that were offline at registration, while parking and unparking
are restricted to the same mask.

Apply the restriction to thread destruction as well, both during
unregistration and registration failure cleanup. This is necessary
when descriptors with disjoint masks share the same per-CPU task
storage: tearing down one descriptor must not stop or clear threads
owned by another descriptor.

Reject registrations that share task storage with an existing
descriptor and have overlapping CPU masks. Check for overlaps under
smpboot_threads_lock before creating any threads, warn once and return
-EBUSY on a conflict. Checking the masks also covers offline CPUs,
whose task pointers may not yet be populated. This prevents descriptors
from sharing a thread and interfering with each other's teardown.

Default a NULL mask to cpu_possible_mask at registration, preserving
the existing all-CPU behavior and allowing the lifecycle helpers to
use the mask unconditionally. Use scoped guards for the registration
locks so all error returns release them in the correct order.

The caller must keep the mask valid and unchanged until the descriptor
is unregistered. smpboot serializes the overlap check and descriptor
registration against other registration and unregistration operations.

Signed-off-by: Daniel Lezcano <daniel.lezcano@xxxxxxxxxxxxxxxx>
---
include/linux/smpboot.h | 6 ++++++
kernel/smpboot.c | 45 ++++++++++++++++++++++++++++++++---------
2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/include/linux/smpboot.h b/include/linux/smpboot.h
index 9d1bc65d226c..57b945d51b19 100644
--- a/include/linux/smpboot.h
+++ b/include/linux/smpboot.h
@@ -5,6 +5,7 @@
#include <linux/types.h>

struct task_struct;
+struct cpumask;
/* Cookie handed to the thread_fn*/
struct smpboot_thread_data;

@@ -27,6 +28,10 @@ struct smpboot_thread_data;
* unparked (cpu online)
* @selfparking: Thread is not parked by the park function.
* @thread_comm: The base name of the thread
+ * @cpumask: Optional mask of CPUs on which to create threads. Must remain
+ * valid and unchanged until the descriptor is unregistered.
+ * Registration defaults NULL to cpu_possible_mask. Descriptors
+ * sharing @store must have disjoint masks.
*/
struct smp_hotplug_thread {
struct task_struct * __percpu *store;
@@ -40,6 +45,7 @@ struct smp_hotplug_thread {
void (*unpark)(unsigned int cpu);
bool selfparking;
const char *thread_comm;
+ const struct cpumask *cpumask;
};

int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread);
diff --git a/kernel/smpboot.c b/kernel/smpboot.c
index 4503b60ce9bd..f997fe85f088 100644
--- a/kernel/smpboot.c
+++ b/kernel/smpboot.c
@@ -168,6 +168,9 @@ __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
struct smpboot_thread_data *td;

+ if (!cpumask_test_cpu(cpu, ht->cpumask))
+ return 0;
+
if (tsk)
return 0;

@@ -225,6 +228,9 @@ static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cp
{
struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);

+ if (!cpumask_test_cpu(cpu, ht->cpumask))
+ return;
+
if (!ht->selfparking)
kthread_unpark(tsk);
}
@@ -244,6 +250,9 @@ static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
{
struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);

+ if (!cpumask_test_cpu(cpu, ht->cpumask))
+ return;
+
if (tsk && !ht->selfparking)
kthread_park(tsk);
}
@@ -267,6 +276,9 @@ static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
for_each_possible_cpu(cpu) {
struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);

+ if (!cpumask_test_cpu(cpu, ht->cpumask))
+ continue;
+
if (tsk) {
kthread_stop_put(tsk);
*per_cpu_ptr(ht->store, cpu) = NULL;
@@ -279,28 +291,41 @@ static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
* to hotplug
* @plug_thread: Hotplug thread descriptor
*
- * Creates and starts the threads on all online cpus.
+ * Creates and starts the threads on online CPUs selected by the descriptor's
+ * cpumask, or all online CPUs if no mask is specified.
+ *
+ * Return: 0 on success, -EBUSY if a registered descriptor shares the same task
+ * storage and has an overlapping CPU mask, or a thread creation error.
*/
int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
{
+ struct smp_hotplug_thread *ht;
unsigned int cpu;
- int ret = 0;
+ int ret;
+
+ if (!plug_thread->cpumask)
+ plug_thread->cpumask = cpu_possible_mask;
+
+ guard(cpus_read_lock)();
+ guard(mutex)(&smpboot_threads_lock);
+
+ list_for_each_entry(ht, &hotplug_threads, list) {
+ if (ht->store != plug_thread->store)
+ continue;
+ if (WARN_ON_ONCE(cpumask_intersects(ht->cpumask, plug_thread->cpumask)))
+ return -EBUSY;
+ }

- cpus_read_lock();
- mutex_lock(&smpboot_threads_lock);
for_each_online_cpu(cpu) {
ret = __smpboot_create_thread(plug_thread, cpu);
if (ret) {
smpboot_destroy_threads(plug_thread);
- goto out;
+ return ret;
}
smpboot_unpark_thread(plug_thread, cpu);
}
list_add(&plug_thread->list, &hotplug_threads);
-out:
- mutex_unlock(&smpboot_threads_lock);
- cpus_read_unlock();
- return ret;
+ return 0;
}
EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);

@@ -308,7 +333,7 @@ EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
* smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
* @plug_thread: Hotplug thread descriptor
*
- * Stops all threads on all possible cpus.
+ * Stops all threads associated with the descriptor, including offline CPUs.
*/
void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
{
--
2.43.0