Linux-Rockchip Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bradley Morgan <bradley.morgan@linux.dev>
To: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>,
	rafael@kernel.org, tglx@linutronix.de
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Peter Ziljstra <peterz@infradead.org>,
	Heiko Stuebner <heiko@sntech.de>,
	linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org,
	Thomas Gleixner <tglx@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] smpboot: Allow restricting per-CPU threads to a CPU mask
Date: Wed, 18 Nov 2026 16:00:43 +0000	[thread overview]
Message-ID: <0453960B-FF17-4E49-8672-FC0FDA3EF5DE@linux.dev> (raw)
In-Reply-To: <7a1e55d24c39d70caa157e6e2c823d9be5db07e7.1794992905.git.daniel.lezcano@oss.qualcomm.com>

On 18 November 2026 10:33:48 GMT, Daniel Lezcano
<daniel.lezcano@oss.qualcomm.com> wrote:
>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.

Cheers,

Reviewed-by: Bradley Morgan <bradley.morgan@linux.dev>

>
>Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
>---
> 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)
> {
>


--- Thanks!

_______________________________________________
Linux-rockchip mailing list
Linux-rockchip@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-rockchip

  reply	other threads:[~2026-11-18 16:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:33 [PATCH v2 0/2] powercap: idle_inject: Create threads on demand Daniel Lezcano
2026-11-18 10:33 ` [PATCH v2 1/2] smpboot: Allow restricting per-CPU threads to a CPU mask Daniel Lezcano
2026-11-18 16:00   ` Bradley Morgan [this message]
2026-11-18 16:15   ` [PATCH RESEND " Daniel Lezcano
2026-11-18 10:33 ` [PATCH v2 2/2] powercap: idle_inject: Create per-CPU threads on registration Daniel Lezcano
2026-11-18 16:15   ` [PATCH RESEND " Daniel Lezcano
2026-11-18 15:57 ` [PATCH v2 0/2] powercap: idle_inject: Create threads on demand Rafael J. Wysocki (Intel)
2026-11-18 16:15 ` [PATCH RESEND " Daniel Lezcano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0453960B-FF17-4E49-8672-FC0FDA3EF5DE@linux.dev \
    --to=bradley.morgan@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=daniel.lezcano@oss.qualcomm.com \
    --cc=heiko@sntech.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=tglx@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox