Skip to contentSkip to conversation

tracing/hwlat: Bind the sampler kthread instead of setting its affinity

PATCH In review Wenwu Hou 3 participants 4 messages Nov 25

Subsystems (3)
lore
Share & tools
lore

Wenwu Hou

author
#0 lore raw

PATCH v2 1/1 tracing/hwlat: Bind the sampler kthread instead of setting its affinity

start_single_kthread() sets the affinity of the kthread it just created before waking it up, but kthread_affine_node() overrides that mask on the first wake-up. See commit 60ba9c38b931 ("kthread: Comment on the purpose and placement of kthread_affine_node() call").

In round-robin mode, move_to_next_cpu() compares save_cpumask against current->cpus_ptr, sees the replaced mask, and switches the tracer to MODE_NONE on its very first iteration:

hwlat_detector: cpumask changed while in round-robin mode, switching to mode none

So the tracer no longer migrates across tracing_cpumask. More importantly, tracing_cpumask is not honoured at all, and with isolcpus=/nohz_full= the thread cannot run on the isolated CPUs it is meant to sample.

Use kthread_bind_mask() to preserve the initial affinity, including isolated CPUs. Clear PF_NO_SETAFFINITY once the single sampler thread starts to allow userspace affinity changes and cgroup migration.

The mask is now computed and validated before kthread_create(), which also avoids creating a thread when tracing_cpumask has no online CPU in common:
cpumask_first() returned nr_cpu_ids there and cpumask_set_cpu() then wrote past the end of the mask.

Tested on a 4-vCPU KVM guest, bit 0 of the mask is CPU 0:

  mode        mask    before                    after
  none        0b0101  Cpus_allowed_list: 0-3    0,2
  round-robin 0b1111  mode -> none, no migrate  migrates 1 2 3 0
  round-robin 0b0101  mask 0-3, no migrate      migrates 0 2
  per-cpu     0b0110  2 threads, cpus 1,2      2 threads, cpus 1,2
Fixes d1a89197589c4 ("kthread: Default affine kthread to its preferred NUMA node")

---
Changes in v2:
- Clear PF_NO_SETAFFINITY under current->pi_lock when the single sampler thread starts, preserving userspace affinity changes and cgroup migration. Keep PF_NO_SETAFFINITY set for per-CPU threads.
- Update the commit message

v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

 kernel/trace/trace_hwlat.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
kernel/trace/trace_hwlat.c +25 -8
index 3fe274b84f1c2..52478ad8030d0 100644
@@ -353,8 +353,21 @@ static void move_to_next_cpu(void)
  */
 static int kthread_fn(void *data)
 {
+	unsigned long flags;
 	u64 interval;
 
+	/*
+	 * kthread_bind_mask() sets PF_NO_SETAFFINITY to keep the initial
+	 * affinity from being overridden by kthread_affine_node(). Clear it
+	 * now to allow userspace affinity changes and cgroup migration, but
+	 * keep per-CPU threads bound to their CPUs.
+	 */
+	if (hwlat_data.thread_mode != MODE_PER_CPU) {
+		raw_spin_lock_irqsave(&current->pi_lock, flags);
+		current->flags &= ~PF_NO_SETAFFINITY;
+		raw_spin_unlock_irqrestore(&current->pi_lock, flags);
+	}
+
 	while (!kthread_should_stop()) {
 
 		if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
@@ -423,15 +436,12 @@ static int start_single_kthread(struct trace_array *tr)
 	if (kdata->kthread)
 		goto out_put_cpus;
 
-	kthread = kthread_create(kthread_fn, NULL, "hwlatd");
-	if (IS_ERR(kthread)) {
-		pr_err(BANNER "could not start sampling thread\n");
-		cpus_read_unlock();
-		return -ENOMEM;
-	}
-
 	/* Just pick the first CPU on first iteration */
 	cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
+	if (cpumask_empty(current_mask)) {
+		cpus_read_unlock();
+		return -EINVAL;
+	}
 
 	if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) {
 		next_cpu = cpumask_first(current_mask);
@@ -440,7 +450,14 @@ static int start_single_kthread(struct trace_array *tr)
 
 	}
 
-	set_cpus_allowed_ptr(kthread, current_mask);
+	kthread = kthread_create(kthread_fn, NULL, "hwlatd");
+	if (IS_ERR(kthread)) {
+		pr_err(BANNER "could not start sampling thread\n");
+		cpus_read_unlock();
+		return -ENOMEM;
+	}
+
+	kthread_bind_mask(kthread, current_mask);
 
 	kdata->kthread = kthread;
 	wake_up_process(kthread);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 59 lines · Wenwu Hou

start_single_kthread() sets the affinity of the kthread it just created before waking it up, but kthread_affine_node() overrides that mask on the first wake-up. See commit 60ba9c38b931 ("kthread: Comment on the purpose and placement of kthread_affine_node() call").

In round-robin mode, move_to_next_cpu() compares save_cpumask against current->cpus_ptr, sees the replaced mask, and switches the tracer to MODE_NONE on its very first iteration:

hwlat_detector: cpumask changed while in round-robin mode, switching to mode none

So the tracer no longer migrates across tracing_cpumask. More importantly, tracing_cpumask is not honoured at all, and with isolcpus=/nohz_full= the thread cannot run on the isolated CPUs it is meant to sample.

Use kthread_bind_mask() to preserve the initial affinity, including isolated CPUs. Clear PF_NO_SETAFFINITY once the single sampler thread starts to allow userspace affinity changes and cgroup migration.

The mask is now computed and validated before kthread_create(), which also avoids creating a thread when tracing_cpumask has no online CPU in common:
cpumask_first() returned nr_cpu_ids there and cpumask_set_cpu() then wrote past the end of the mask.

Tested on a 4-vCPU KVM guest, bit 0 of the mask is CPU 0:

  mode        mask    before                    after
  none        0b0101  Cpus_allowed_list: 0-3    0,2
  round-robin 0b1111  mode -> none, no migrate  migrates 1 2 3 0
  round-robin 0b0101  mask 0-3, no migrate      migrates 0 2
  per-cpu     0b0110  2 threads, cpus 1,2      2 threads, cpus 1,2
Fixes d1a89197589c4 ("kthread: Default affine kthread to its preferred NUMA node")

---
Changes in v2:
- Clear PF_NO_SETAFFINITY under current->pi_lock when the single sampler thread starts, preserving userspace affinity changes and cgroup migration. Keep PF_NO_SETAFFINITY set for per-CPU threads.
- Update the commit message

v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

 kernel/trace/trace_hwlat.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
kernel/trace/trace_hwlat.c +1 -0
index 3fe274b84f1c2..52478ad8030d0 100644
@@ -353,8 +353,21 @@ static void move_to_next_cpu(void)
  */
 static int kthread_fn(void *data)
 {
+	unsigned long flags;
 	u64 interval;
 

kernel/trace/trace_hwlat.c static void move_to_next_cpu(void) · line 368

Quoted 10 lines
kernel/trace/trace_hwlat.c +10 -0
+	/*
+	 * kthread_bind_mask() sets PF_NO_SETAFFINITY to keep the initial
+	 * affinity from being overridden by kthread_affine_node(). Clear it
+	 * now to allow userspace affinity changes and cgroup migration, but
+	 * keep per-CPU threads bound to their CPUs.
+	 */
+	if (hwlat_data.thread_mode != MODE_PER_CPU) {
+		raw_spin_lock_irqsave(&current->pi_lock, flags);
+		current->flags &= ~PF_NO_SETAFFINITY;
+		raw_spin_unlock_irqrestore(&current->pi_lock, flags);

This is not pretty but moreover, is it a good idea to let the user override that affinity?

Thanks.

Quoted 45 lines
kernel/trace/trace_hwlat.c +14 -8
+	}
+
 	while (!kthread_should_stop()) {
 
 		if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
@@ -423,15 +436,12 @@ static int start_single_kthread(struct trace_array *tr)
 	if (kdata->kthread)
 		goto out_put_cpus;
 
-	kthread = kthread_create(kthread_fn, NULL, "hwlatd");
-	if (IS_ERR(kthread)) {
-		pr_err(BANNER "could not start sampling thread\n");
-		cpus_read_unlock();
-		return -ENOMEM;
-	}
-
 	/* Just pick the first CPU on first iteration */
 	cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
+	if (cpumask_empty(current_mask)) {
+		cpus_read_unlock();
+		return -EINVAL;
+	}
 
 	if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) {
 		next_cpu = cpumask_first(current_mask);
@@ -440,7 +450,14 @@ static int start_single_kthread(struct trace_array *tr)
 
 	}
 
-	set_cpus_allowed_ptr(kthread, current_mask);
+	kthread = kthread_create(kthread_fn, NULL, "hwlatd");
+	if (IS_ERR(kthread)) {
+		pr_err(BANNER "could not start sampling thread\n");
+		cpus_read_unlock();
+		return -ENOMEM;
+	}
+
+	kthread_bind_mask(kthread, current_mask);
 
 	kdata->kthread = kthread;
 	wake_up_process(kthread);
Signature · 1 line
2.43.0
Signature · 2 lines
Frederic Weisbecker
SUSE Labs
maintainer
#2 lore raw
Quoted 5 lines · On Wed, 25 Nov 2026 14:37:24 +0100 Frederic Weisbecker <[email protected]>…
Quoted 5 lines

static int kthread_fn(void *data)
{

kernel/trace/trace_hwlat.c +1 -0
+	unsigned long flags;
 	u64 interval;
Quoted 13 lines

kernel/trace/trace_hwlat.c static void move_to_next_cpu(void) · line 368

Quoted 10 lines
kernel/trace/trace_hwlat.c +10 -0
+	/*
+	 * kthread_bind_mask() sets PF_NO_SETAFFINITY to keep the initial
+	 * affinity from being overridden by kthread_affine_node(). Clear it
+	 * now to allow userspace affinity changes and cgroup migration, but
+	 * keep per-CPU threads bound to their CPUs.
+	 */
+	if (hwlat_data.thread_mode != MODE_PER_CPU) {
+		raw_spin_lock_irqsave(&current->pi_lock, flags);
+		current->flags &= ~PF_NO_SETAFFINITY;
+		raw_spin_unlock_irqrestore(&current->pi_lock, flags);  

This is not pretty but moreover, is it a good idea to let the user override that affinity?

No, but we used to. Perhaps we should make that not possible instead? :-/

-- Steve

#3 lore raw
Quoted 22 lines · Steven Rostedt
Quoted 5 lines · On Wed, 25 Nov 2026 14:37:24 +0100 Frederic Weisbecker <[email protected]>…
Quoted 5 lines

static int kthread_fn(void *data)
{

kernel/trace/trace_hwlat.c +1 -0
+	unsigned long flags;
 	u64 interval;
Quoted 13 lines

kernel/trace/trace_hwlat.c static void move_to_next_cpu(void) · line 368

Quoted 10 lines
kernel/trace/trace_hwlat.c +10 -0
+	/*
+	 * kthread_bind_mask() sets PF_NO_SETAFFINITY to keep the initial
+	 * affinity from being overridden by kthread_affine_node(). Clear it
+	 * now to allow userspace affinity changes and cgroup migration, but
+	 * keep per-CPU threads bound to their CPUs.
+	 */
+	if (hwlat_data.thread_mode != MODE_PER_CPU) {
+		raw_spin_lock_irqsave(&current->pi_lock, flags);
+		current->flags &= ~PF_NO_SETAFFINITY;
+		raw_spin_unlock_irqrestore(&current->pi_lock, flags);  

This is not pretty but moreover, is it a good idea to let the user override that affinity?

Quoted 1 line

No, but we used to. Perhaps we should make that not possible instead? :-/

Right, the affinity should only be changed by the kernel if tracing_cpumask is written. Otherwise it doesn't seem to make sense to let the user play with the affinity.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs