Skip to contentSkip to conversation

tracing/osnoise: Synchronization fixes

PATCH Superseded Crystal Wood 2 participants 11 messages Oct 24

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover tracing/osnoise: Synchronization fixes

Various improvements to synchronization in osnoise/timerlat.

v5:
- Removed "Split out per-cpu variable resets" due to Sashiko feedback about races with NMIs and other tracing hooks; the main motivation for that patch was problems with osn->sampling and tlat->thread, both of which are now explicitly cleared.

- Fixed some patch shuffling bugs in fd detachment that were revealed by dropping the reset patch

- Added "Start all eligible cpus from hotplug workfn"

- Removed "Always check OSN_WORKLOAD" which is no longer needed after the hotplug workfn patch

v4: fixed a problem with the v3 fix spotted by Sashiko

https://lore.kernel.org/all/[email protected]/

v3: make sure osn->sampling gets cleared on stop, even for non-workload osnoise (reported by Sashiko)

https://lore.kernel.org/all/[email protected]/

v2: addresses issues raised by Sashiko, as well as a few more problems discovered in additional testing.

https://lore.kernel.org/all/[email protected]/

v1: https://lore.kernel.org/all/[email protected]/

Crystal Wood (8):
tracing/osnoise: Variable cleanup
tracing/osnoise: Start all eligible cpus from hotplug workfn tracing/osnoise: Hold a ref on osn->kthread tracing/osnoise: Per-cpu mutexes and fd detachment tracing/osnoise: kthreads: Verify CPU after migrate_disable() tracing/osnoise: Take trace_types_lock in timerlat_fd_open tracing/osnoise: Print taint message for timerlat tracing/osnoise: Refuse timerlat fd open if tracer inactive

 Documentation/trace/timerlat-tracer.rst |   5 +-
 kernel/trace/trace_osnoise.c            | 395 +++++++++++++++---------
 2 files changed, 246 insertions(+), 154 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v5 1/8 tracing/osnoise: Variable cleanup

Add cpu_osn_var/cpu_tmr_var() accessors and use them.

Timerlat threads aren't always kthreads, so rename the variable to just "thread".

rel_period isn't used at all, so remove it.

---
v2: new patch

---
 kernel/trace/trace_osnoise.c | 53 +++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 22 deletions(-)
kernel/trace/trace_osnoise.c +31 -22
index 0e1265acd1cc..953936bd5eec 100644
@@ -250,6 +250,11 @@ static inline struct osnoise_variables *this_cpu_osn_var(void)
 	return this_cpu_ptr(&per_cpu_osnoise_var);
 }
 
+static inline struct osnoise_variables *cpu_osn_var(int cpu)
+{
+	return per_cpu_ptr(&per_cpu_osnoise_var, cpu);
+}
+
 /*
  * Protect the interface.
  */
@@ -260,9 +265,8 @@ static struct mutex interface_lock;
  * Runtime information for the timer mode.
  */
 struct timerlat_variables {
-	struct task_struct	*kthread;
+	struct task_struct	*thread;
 	struct hrtimer		timer;
-	u64			rel_period;
 	u64			abs_period;
 	bool			tracing_thread;
 	u64			count;
@@ -284,6 +288,11 @@ static inline struct timerlat_variables *this_cpu_tmr_var(void)
 	return this_cpu_ptr(&per_cpu_timerlat_var);
 }
 
+static inline struct timerlat_variables *cpu_tmr_var(int cpu)
+{
+	return per_cpu_ptr(&per_cpu_timerlat_var, cpu);
+}
+
 /*
  * tlat_var_reset - Reset the values of the given timerlat_variables
  */
@@ -300,8 +309,8 @@ static inline void tlat_var_reset(void)
 	 * zeroing the structure is perfect.
 	 */
 	for_each_online_cpu(cpu) {
-		tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
-		if (tlat_var->kthread)
+		tlat_var = cpu_tmr_var(cpu);
+		if (tlat_var->thread)
 			hrtimer_cancel(&tlat_var->timer);
 		memset(tlat_var, 0, sizeof(*tlat_var));
 	}
@@ -330,7 +339,7 @@ static inline void osn_var_reset(void)
 	 * zeroing the structure is perfect.
 	 */
 	for_each_online_cpu(cpu) {
-		osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
+		osn_var = cpu_osn_var(cpu);
 		memset(osn_var, 0, sizeof(*osn_var));
 	}
 }
@@ -1226,9 +1235,9 @@ static void trace_sched_migrate_callback(void *data, struct task_struct *p, int
 	struct osnoise_variables *osn_var;
 	long cpu = task_cpu(p);
 
-	osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
+	osn_var = cpu_osn_var(cpu);
 	if (osn_var->pid == p->pid && dest_cpu != cpu) {
-		per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
+		cpu_tmr_var(cpu)->uthread_migrate = 1;
 		osnoise_taint("timerlat user-thread migrated\n");
 		osnoise_stop_exception("timerlat user-thread migrated", cpu);
 	}
@@ -1826,13 +1835,13 @@ static enum hrtimer_restart timerlat_irq(struct hrtimer *timer)
 			osnoise_stop_tracing();
 			notify_new_max_latency(diff);
 
-			wake_up_process(tlat->kthread);
+			wake_up_process(tlat->thread);
 
 			return HRTIMER_NORESTART;
 		}
 	}
 
-	wake_up_process(tlat->kthread);
+	wake_up_process(tlat->thread);
 
 	if (osnoise_data.print_stack)
 		timerlat_save_stack(0);
@@ -1925,7 +1934,7 @@ static int timerlat_main(void *data)
 	tlat->tracing_thread = false;
 
 	hrtimer_setup(&tlat->timer, timerlat_irq, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
-	tlat->kthread = current;
+	tlat->thread = current;
 	osn_var->pid = current->pid;
 	/*
 	 * Annotate the arrival time.
@@ -1980,7 +1989,7 @@ static void stop_kthread(unsigned int cpu)
 {
 	struct task_struct *kthread;
 
-	kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
+	kthread = xchg_relaxed(&cpu_osn_var(cpu)->kthread, NULL);
 	if (kthread) {
 		if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask) &&
 		    !WARN_ON(!test_bit(OSN_WORKLOAD, &osnoise_options))) {
@@ -2000,7 +2009,7 @@ static void stop_kthread(unsigned int cpu)
 			/*
 			 * This is set in the osnoise tracer case.
 			 */
-			per_cpu(per_cpu_osnoise_var, cpu).sampling = false;
+			cpu_osn_var(cpu)->sampling = false;
 			barrier();
 		}
 	}
@@ -2034,7 +2043,7 @@ static int start_kthread(unsigned int cpu)
 	char comm[24];
 
 	/* Do not start a new thread if it is already running */
-	if (per_cpu(per_cpu_osnoise_var, cpu).kthread)
+	if (cpu_osn_var(cpu)->kthread)
 		return 0;
 
 	if (timerlat_enabled()) {
@@ -2043,7 +2052,7 @@ static int start_kthread(unsigned int cpu)
 	} else {
 		/* if no workload, just return */
 		if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
-			per_cpu(per_cpu_osnoise_var, cpu).sampling = true;
+			cpu_osn_var(cpu)->sampling = true;
 			barrier();
 			return 0;
 		}
@@ -2057,7 +2066,7 @@ static int start_kthread(unsigned int cpu)
 		return -ENOMEM;
 	}
 
-	per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
+	cpu_osn_var(cpu)->kthread = kthread;
 	cpumask_set_cpu(cpu, &kthread_cpumask);
 
 	return 0;
@@ -2090,7 +2099,7 @@ static int start_per_cpu_kthreads(void)
 		if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask)) {
 			struct task_struct *kthread;
 
-			kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
+			kthread = xchg_relaxed(&cpu_osn_var(cpu)->kthread, NULL);
 			if (!WARN_ON(!kthread))
 				kthread_stop(kthread);
 		}
@@ -2519,7 +2528,7 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 			return -EINVAL;
 		}
 	} else {
-		per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
+		cpu_tmr_var(cpu)->uthread_migrate = 1;
 		osnoise_taint("timerlat user thread migrate\n");
 		osnoise_stop_tracing();
 		migrate_enable();
@@ -2563,7 +2572,7 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 		}
 	} else {
 		tlat->tracing_thread = false;
-		tlat->kthread = current;
+		tlat->thread = current;
 
 		/* Annotate now to drift new period */
 		tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
@@ -2612,10 +2621,10 @@ static int timerlat_fd_release(struct inode *inode, struct file *file)
 	migrate_disable();
 	mutex_lock(&interface_lock);
 
-	osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
-	tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
+	osn_var = cpu_osn_var(cpu);
+	tlat_var = cpu_tmr_var(cpu);
 
-	if (tlat_var->kthread)
+	if (tlat_var->thread)
 		hrtimer_cancel(&tlat_var->timer);
 	memset(tlat_var, 0, sizeof(*tlat_var));
 
@@ -3095,7 +3104,7 @@ static void timerlat_tracer_stop(struct trace_array *tr)
 	 */
 	if (!osnoise_has_registered_instances()) {
 		for_each_online_cpu(cpu)
-			per_cpu(per_cpu_osnoise_var, cpu).sampling = 0;
+			cpu_osn_var(cpu)->sampling = 0;
 	}
 
 	osnoise_workload_stop();
Signature · 1 line
2.54.0
#2 lore raw

PATCH v5 2/8 tracing/osnoise: Start all eligible cpus from hotplug workfn

In reply to Crystal Wood

Previously, a start could be lost if multiple cpus get onlined before the workqueue has a chance to run.

---
v5: new patch

I have this in my notes as being a Sashiko comment, though I can't find the source now. I wasn't able to reproduce the race given how slow and serialized cpu hotplug operations are, but it's possible.

On the other hand, user threads detach on cpu offline with no mechanism for reattaching (other than userspace figuring it out and reopening the fd), and you really probably shouldn't be offlining cpus that you're measuring with tools like this, so you could make an argument for removing the restart mechanism entirely... it'd certainly shrink the potential bug surface.

This also eliminates the need for the "Always check OSN_WORKLOAD" patch.

---
 kernel/trace/trace_osnoise.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/trace/trace_osnoise.c +7 -7
index 953936bd5eec..0e744652946c 100644
@@ -2122,7 +2122,7 @@ static int start_per_cpu_kthreads(void)
 #ifdef CONFIG_HOTPLUG_CPU
 static void osnoise_hotplug_workfn(struct work_struct *dummy)
 {
-	unsigned int cpu = smp_processor_id();
+	unsigned int cpu;
 
 	guard(mutex)(&trace_types_lock);
 
@@ -2132,13 +2132,13 @@ static void osnoise_hotplug_workfn(struct work_struct *dummy)
 	guard(cpus_read_lock)();
 	guard(mutex)(&interface_lock);
 
-	if (!cpu_online(cpu))
+	if (!test_bit(OSN_WORKLOAD, &osnoise_options))
 		return;
 
-	if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
-		return;
-
-	start_kthread(cpu);
+	for_each_online_cpu(cpu) {
+		if (cpumask_test_cpu(cpu, &osnoise_cpumask))
+			start_kthread(cpu);
+	}
 }
 
 static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
@@ -2148,7 +2148,7 @@ static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
  */
 static int osnoise_cpu_init(unsigned int cpu)
 {
-	schedule_work_on(cpu, &osnoise_hotplug_work);
+	schedule_work(&osnoise_hotplug_work);
 	return 0;
 }
 
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH v5 2/8] tracing/osnoise: Start all eligible cpus from hotplug workfn

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] tracing/osnoise: osnoise_hotplug_workfn() skips sampling when !OSN_WORKLOAD --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] tracing/osnoise: osnoise_hotplug_workfn() skips sampling when !OSN_WORKLOAD --

commit 4bedb2088cbb62deefeaad638901751b0c1e9f6d
Author: Crystal Wood <[email protected]>

tracing/osnoise: Start all eligible cpus from hotplug workfn

This commit updates osnoise_hotplug_workfn() to iterate over all online CPUs in osnoise_cpumask and start their kthreads when scheduled from osnoise_cpu_init(), avoiding lost starts if multiple CPUs come online before the work item runs.

Quoted 20 lines
kernel/trace/trace_osnoise.c +2 -2
index aa764ae13047e..29fa4150b26c0 100644
@@ -2123,7 +2123,7 @@ static int start_per_cpu_kthreads(void)
 #ifdef CONFIG_HOTPLUG_CPU
 static void osnoise_hotplug_workfn(struct work_struct *dummy)
 {
-	unsigned int cpu = smp_processor_id();
+	unsigned int cpu;
 
 	guard(mutex)(&trace_types_lock);
 
@@ -2133,13 +2133,13 @@ static void osnoise_hotplug_workfn(struct work_struct *dummy)
 	guard(cpus_read_lock)();
 	guard(mutex)(&interface_lock);
 
-	if (!cpu_online(cpu))
+	if (!test_bit(OSN_WORKLOAD, &osnoise_options))
 		return;

kernel/trace/trace_osnoise.c static void osnoise_hotplug_workfn(struct work_struct *dummy) · line 2143

Quoted 10 lines
kernel/trace/trace_osnoise.c +4 -4
 
-	if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
-		return;
-
-	start_kthread(cpu);
+	for_each_online_cpu(cpu) {
+		if (cpumask_test_cpu(cpu, &osnoise_cpumask))
+			start_kthread(cpu);
+	}
 }

[Severity: Medium]
Does returning early in osnoise_hotplug_workfn() whenever OSN_WORKLOAD is clear leave per-CPU sampling disabled on newly onlined CPUs when running the osnoise tracer with NO_OSNOISE_WORKLOAD?

When timerlat is disabled (i.e. the osnoise tracer is active) and OSN_WORKLOAD is clear, start_kthread() is responsible for enabling sampling on the CPU without spawning a kthread:

kernel/trace/trace_osnoise.c:start_kthread() {
	...
	if (timerlat_enabled()) {
		snprintf(comm, 24, "timerlat/%d", cpu);
		main = timerlat_main;
	} else {
		/* if no workload, just return */
		if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
			cpu_osn_var(cpu)->sampling = true;
			barrier();
			return 0;
		}
		snprintf(comm, 24, "osnoise/%d", cpu);
	}
	...
}

And when a CPU goes offline via osnoise_cpu_die() -> stop_kthread(), sampling is cleared on that CPU:

kernel/trace/trace_osnoise.c:stop_kthread() {
	...
	} else {
		/* if no workload, just return */
		if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
			/*
			 * This is set in the osnoise tracer case.
			 */
			cpu_osn_var(cpu)->sampling = false;
			barrier();
		}
	}
}

In start_per_cpu_kthreads(), the early return for !OSN_WORKLOAD is gated on timerlat_enabled():

kernel/trace/trace_osnoise.c:start_per_cpu_kthreads() {
	...
	if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
		if (timerlat_enabled())
			return 0;
	}
	...
}

Should osnoise_hotplug_workfn() likewise check timerlat_enabled() before returning early on !test_bit(OSN_WORKLOAD, &osnoise_options) (or let start_kthread() handle the !OSN_WORKLOAD case) so that cpu_osn_var(cpu)->sampling is set to true when a CPU in osnoise_cpumask comes online?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH v5 3/8 tracing/osnoise: Hold a ref on osn->kthread

In reply to Crystal Wood

Deal with early kthread termination by holding a reference to the task_struct and always running the cleanup code in stop_kthread(), rather than with hacks like osnoise_migration_pending().

This avoids a potential deadlock when a later patch switches from xchg to using a lock to protect osn->kthread updates, and also deals with any other situations where a kthread might exit early.

---
v2: New patch. I did this in response to Sashiko pointing out a deadlock in v1 with the use of osn->lock in osnoise_migration_pending. That might no longer be an issue now that detach_lock is split out, but this is still the cleaner and more robust approach.

---
 kernel/trace/trace_osnoise.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
kernel/trace/trace_osnoise.c +4 -13
index 0e744652946c..e710fc26e477 100644
@@ -1691,17 +1691,6 @@ static inline int osnoise_migration_pending(void)
 	 * thus: taint and leave to unblock the blocked thread.
 	 */
 	osnoise_taint("migration requested to osnoise threads, leaving.");
-
-	/*
-	 * Unset this thread from the threads managed by the interface.
-	 * The tracers are responsible for cleaning their env before
-	 * exiting.
-	 */
-	mutex_lock(&interface_lock);
-	this_cpu_osn_var()->kthread = NULL;
-	cpumask_clear_cpu(smp_processor_id(), &kthread_cpumask);
-	mutex_unlock(&interface_lock);
-
 	return 1;
 }
 
@@ -2001,8 +1990,8 @@ static void stop_kthread(unsigned int cpu)
 			 * by killing the thread. NOTE: this is a purpose specific file.
 			 */
 			kill_pid(kthread->thread_pid, SIGKILL, 1);
-			put_task_struct(kthread);
 		}
+		put_task_struct(kthread);
 	} else {
 		/* if no workload, just return */
 		if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
@@ -2059,15 +2048,17 @@ static int start_kthread(unsigned int cpu)
 		snprintf(comm, 24, "osnoise/%d", cpu);
 	}
 
-	kthread = kthread_run_on_cpu(main, NULL, cpu, comm);
+	kthread = kthread_create_on_cpu(main, NULL, cpu, comm);
 
 	if (IS_ERR(kthread)) {
 		pr_err(BANNER "could not start sampling thread\n");
 		return -ENOMEM;
 	}
 
+	get_task_struct(kthread);
 	cpu_osn_var(cpu)->kthread = kthread;
 	cpumask_set_cpu(cpu, &kthread_cpumask);
+	wake_up_process(kthread);
 
 	return 0;
 }
Signature · 1 line
2.54.0
#5 lore raw

PATCH v5 4/8 tracing/osnoise: Per-cpu mutexes and fd detachment

In reply to Crystal Wood

Clean up a variety of synchronization issues and related bandaids by having a per-cpu mutex that guards changes to kthread, and another than guards fd open/close/revoke.

Replace the SIGKILL hack for userspace timerlat threads (that doesn't even work, because we don't wait for the process to actually die) with a mutex-protected detachment mechanism. The mutex should be uncontended during normal timerlat_fd_read() usage.

This also eliminates the need for the "quick fix" of e6a53481da292 ("tracing/timerlat: Only clear timer if a kthread exists").

---
v2:
- Use a separate lock for detachment, so that we can wake the thread that holds the detach lock, while holding osn->lock to make sure the thread is valid.
- Return -EINVAL if detached while waiting, and -ERESTARTSYS on a signal
- Init mutex on every possible cpu (as pointed out by Sashiko)
- Update documentation for SIGKILL removal

v3:
- Clear osn_var->sampling in stop_kthread(), as reporteed by Sashiko for the osnoise non-workload case

v5:
- Fix some patch-shuffling bugs in timerlat_fd_detach() that were previously undone by the reset patch.

---
 Documentation/trace/timerlat-tracer.rst |   5 +-
 kernel/trace/trace_osnoise.c            | 308 +++++++++++++++---------
 2 files changed, 203 insertions(+), 110 deletions(-)
Documentation/trace/timerlat-tracer.rst +3 -2
index 68d429d454a5..ea94627e821b 100644
@@ -220,8 +220,9 @@ After reporting the total latency, timerlat will restart the cycle, arm
 a timer, and go to sleep for the following activation.
 
 If at any time one of the conditions is broken, e.g., the thread migrates
-while in user space, or the timerlat tracer is disabled, the SIG_KILL
-signal will be sent to the user-space thread.
+while in user space, or the timerlat tracer is disabled, the fd will be
+detached from the tracer, and subsequent usage will fail with EINVAL.
+Note that older kernels may deliver a SIGKILL instead.
 
 Here is an basic example of user-space code for timerlat::
 
kernel/trace/trace_osnoise.c +200 -108

522 lines not on this page · raw message

Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH v5 4/8] tracing/osnoise: Per-cpu mutexes and fd detachment

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] tracing/osnoise: broken state on signal restart in timerlat_fd_read() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] tracing/osnoise: broken state on signal restart in timerlat_fd_read()
- [Medium] tracing/osnoise: migration check precedes fd validation in timerlat_fd_read() --

commit 8df487d33bcb20519b9ce641f525b512ec06fe1d
Author: Crystal Wood <[email protected]>

tracing/osnoise: Per-cpu mutexes and fd detachment

This commit introduces per-CPU mutexes to synchronize kthread and timerlat fd lifecycle operations and replaces the SIGKILL mechanism for detaching user-space timerlat threads with a mutex-protected fd detachment mechanism.

Quoted 20 lines
Documentation/trace/timerlat-tracer.rst +3 -2
index 68d429d454a54..ea94627e821b5 100644
@@ -220,8 +220,9 @@ After reporting the total latency, timerlat will restart the cycle, arm
 a timer, and go to sleep for the following activation.
 
 If at any time one of the conditions is broken, e.g., the thread migrates
-while in user space, or the timerlat tracer is disabled, the SIG_KILL
-signal will be sent to the user-space thread.
+while in user space, or the timerlat tracer is disabled, the fd will be
+detached from the tracer, and subsequent usage will fail with EINVAL.
+Note that older kernels may deliver a SIGKILL instead.
 
 Here is an basic example of user-space code for timerlat::
 
kernel/trace/trace_osnoise.c +0 -0
index 40cbabb98a1ba..f383bc4df6b7b 100644

[ ... ]

Quoted 32 lines
kernel/trace/trace_osnoise.c +4 -3
@@ -2498,12 +2574,13 @@ static ssize_t
 timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 		  loff_t *ppos)
 {
-	long cpu = (long) file->private_data;
+	struct fd_data *fdd = file->private_data;
 	struct osnoise_variables *osn_var;
 	struct timerlat_variables *tlat;
 	struct timerlat_sample s;
 	s64 diff;
 	u64 now;
+	int ret = 0;
 
 	migrate_disable();
 
@@ -2514,13 +2591,13 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 	 * we can do about it.
 	 * So, if the thread is running on another CPU, stop the machinery.
 	 */
-	if (cpu == smp_processor_id()) {
+	if (fdd->cpu == smp_processor_id()) {
 		if (tlat->uthread_migrate) {
 			migrate_enable();
 			return -EINVAL;
 		}
 	} else {
-		cpu_tmr_var(cpu)->uthread_migrate = 1;
+		cpu_tmr_var(fdd->cpu)->uthread_migrate = 1;
 		osnoise_taint("timerlat user thread migrate\n");
 		osnoise_stop_tracing();
 		migrate_enable();

kernel/trace/trace_osnoise.c timerlat_fd_read(struct file *file, char __user *ubuf, size_t count, · line 2614

Quoted 9 lines
kernel/trace/trace_osnoise.c +6 -0
@@ -2529,6 +2606,13 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 
 	osn_var = this_cpu_osn_var();
 
+	/* In normal usage, this should always be uncontended. */
+	mutex_lock(&tlat->detach_lock);
+	if (fdd != osn_var->fdd || current != fdd->thread) {
+		ret = -EINVAL;
+		goto out;
+	}

[Severity: Medium]
In timerlat_fd_read(), the CPU migration check runs before acquiring tlat->detach_lock and before verifying that fdd == osn_var->fdd and current == fdd->thread.

Because stop_kthread() now detaches the fd via timerlat_fd_detach() instead of sending SIGKILL, the user-space thread stays alive with an open file descriptor after detachment. If that thread later migrates to another CPU and calls read() on the detached fd (or if another thread sharing the file table calls read() on a CPU other than fdd->cpu), timerlat_fd_read() takes the fdd->cpu != smp_processor_id() branch:

	cpu_tmr_var(fdd->cpu)->uthread_migrate = 1;
	osnoise_taint("timerlat user thread migrate\n");
	osnoise_stop_tracing();

This taints the tracer, stops tracing (or panics if OSN_PANIC_ON_STOP is set), and leaves cpu_tmr_var(fdd->cpu)->uthread_migrate set to 1. Because fdd is already detached (fdd != osn_var->fdd), a subsequent close() in timerlat_fd_release():

	mutex_lock(&osn_var->lock);
	if (fdd == osn_var->fdd)
		timerlat_fd_detach(fdd);
	mutex_unlock(&osn_var->lock);

will not call timerlat_fd_detach() to zero tlat->zero, and timerlat_fd_open() does not clear tlat->uthread_migrate either, leaving new timerlat fd sessions on fdd->cpu broken until the tracer is restarted.

Also, Documentation/trace/timerlat-tracer.rst states that if the thread migrates while in user space, the fd will be detached from the tracer, but neither trace_sched_migrate_callback() nor timerlat_fd_read() calls timerlat_fd_detach().

Should timerlat_fd_read() validate that fdd is still attached and owned by current before acting on migration, and should migration detach the fd?

Quoted 16 lines
kernel/trace/trace_osnoise.c +0 -3
 	/*
 	 * The timerlat in user-space runs in a different order:
 	 * the read() starts from the execution of the previous occurrence,
@@ -2563,9 +2647,6 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 			}
 		}
 	} else {
-		tlat->tracing_thread = false;
-		tlat->thread = current;
-
 		/* Annotate now to drift new period */
 		tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
 
@@ -2573,7 +2654,19 @@ timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
 	}
 

kernel/trace/trace_osnoise.c timerlat_fd_read(struct file *file, char __user *ubuf, size_t count, · line 2664

Quoted 10 lines
kernel/trace/trace_osnoise.c +8 -1
 	/* wait for the next period */
-	wait_next_period(tlat);
+	if (wait_next_period(tlat) < 0) {
+		if (signal_pending(current))
+			ret = -ERESTARTSYS;
+		else
+			ret = -EINVAL;
+
+		goto out;
+	}

[Severity: Medium]
What happens when wait_next_period() in timerlat_fd_read() is interrupted by a signal and returns -ERESTARTSYS?

Before calling wait_next_period(), timerlat_fd_read() has either set osn_var->sampling = 1 (on the first read) or already recorded the THREAD_URET latency sample for the previous wakeup (on subsequent reads). Inside wait_next_period(), tlat->abs_period is advanced to next_abs_period, and when signal_pending(current) is true, the timer is cancelled via hrtimer_cancel(&tlat->timer) and -1 is returned:

	if (tlat->detach || signal_pending(current)) {
		hrtimer_cancel(&tlat->timer);
		return -1;
	}

When the read() syscall is restarted (or retried by user space after EINTR), osn_var->sampling is still 1, so timerlat_fd_read() enters the if (likely(osn_var->sampling)) branch at the top of the function:

	if (likely(osn_var->sampling)) {
		now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
		diff = now - tlat->abs_period;

		/*
		 * it was not a timer firing, but some other signal?
		 */
		if (diff < 0)
			goto out;

		s.seqnum = tlat->count;
		s.timer_latency = diff;
		s.context = THREAD_URET;

		record_timerlat_sample(&s);

While now is still earlier than the future tlat->abs_period, diff < 0 is true and timerlat_fd_read() jumps to out and returns 0 (EOF) without arming the timer or sleeping. Once now reaches tlat->abs_period, diff >= 0 and timerlat_fd_read() records a bogus or duplicate THREAD_URET sample and checks stop_tracing_total for a timer activation that was cancelled and never fired.

Does timerlat_fd_read() need to avoid treating the next read() after a signal interruption as a wakeup from a completed timer cycle?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#7 lore raw

PATCH v5 5/8 tracing/osnoise: kthreads: Verify CPU after migrate_disable()

In reply to Crystal Wood

Pass osn vars as the kthread arg, and make sure it matches where ve're running in case of (e.g.) hotplug issues.

---
v2:
- Do it for osnoise too, not just timerlat
- I haven't been able to reproduce this in a while, and I'm not seeing the hole in the hotplug sync that would allow it, but I also don't see what would have allowed it in v1 (maybe it was in some pre-v1 iteration? or maybe just a tiny race window).

So we can maybe drop this patch, though it still feels better to explicitly splat if problems happen rather than rely on this_cpu_osn_var() blowing up due to the affinity being set to multiple cpus.

---
 kernel/trace/trace_osnoise.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
kernel/trace/trace_osnoise.c +18 -3
index 117937d7bf88..9206123ad445 100644
@@ -236,6 +236,7 @@ struct osnoise_variables {
 	struct mutex		lock; /* covers kthread and fdd changes */
 	struct task_struct	*kthread;
 	struct fd_data		*fdd;
+	int			cpu;
 
 	struct_group(zero,
 		bool			sampling;
@@ -1723,6 +1724,7 @@ static inline int osnoise_migration_pending(void)
  */
 static int osnoise_main(void *data)
 {
+	struct osnoise_variables *osn = data;
 	unsigned long flags;
 
 	/*
@@ -1733,6 +1735,11 @@ static int osnoise_main(void *data)
 	 * flag.
 	 */
 	migrate_disable();
+	if (WARN_ON_ONCE(osn->cpu != smp_processor_id())) {
+		migrate_enable();
+		return 1;
+	}
+
 	raw_spin_lock_irqsave(&current->pi_lock, flags);
 	current->flags &= ~(PF_NO_SETAFFINITY);
 	raw_spin_unlock_irqrestore(&current->pi_lock, flags);
@@ -1923,8 +1930,8 @@ static int wait_next_period(struct timerlat_variables *tlat)
  */
 static int timerlat_main(void *data)
 {
-	struct osnoise_variables *osn_var = this_cpu_osn_var();
-	struct timerlat_variables *tlat = this_cpu_tmr_var();
+	struct osnoise_variables *osn_var = data;
+	struct timerlat_variables *tlat;
 	struct timerlat_sample s;
 	struct sched_param sp;
 	unsigned long flags;
@@ -1944,6 +1951,13 @@ static int timerlat_main(void *data)
 	 * flag.
 	 */
 	migrate_disable();
+	if (WARN_ON_ONCE(osn_var->cpu != smp_processor_id())) {
+		migrate_enable();
+		return 1;
+	}
+
+	tlat = this_cpu_tmr_var();
+
 	raw_spin_lock_irqsave(&current->pi_lock, flags);
 	current->flags &= ~(PF_NO_SETAFFINITY);
 	raw_spin_unlock_irqrestore(&current->pi_lock, flags);
@@ -2076,7 +2090,7 @@ static int start_kthread(unsigned int cpu)
 		snprintf(comm, 24, "osnoise/%d", cpu);
 	}
 
-	kthread = kthread_create_on_cpu(main, NULL, cpu, comm);
+	kthread = kthread_create_on_cpu(main, cpu_osn_var(cpu), cpu, comm);
 
 	if (IS_ERR(kthread)) {
 		pr_err(BANNER "could not start sampling thread\n");
@@ -3251,6 +3265,7 @@ __init static int init_osnoise_tracer(void)
 		struct osnoise_variables *osn = cpu_osn_var(cpu);
 
 		mutex_init(&osn->lock);
+		osn->cpu = cpu;
 	}
 
 	cpumask_copy(&osnoise_cpumask, cpu_all_mask);
Signature · 1 line
2.54.0
#8 lore raw

PATCH v5 6/8 tracing/osnoise: Take trace_types_lock in timerlat_fd_open

In reply to Crystal Wood

This prevents a user fd from being opened when osnoise_options_write() has already stopped threads but not yet set OSN_WORKLOAD, and matches what all callers of start_kthread() do.

The original intent was probably for interface_lock to cover things like this, but it never covered the full race window, and currently can't due to lock order issues stemming from using interface_lock in osnoise_sleep(). There does not appear to be any remaining reason to take interface_lock in timerlat_fd_open(), so remove it.

---
v2: convert rest of function to guards

Do we really need that interface_lock in osnoise_sleep()? Technically we should probably be grabbing it every time we access osnoise_data, but we don't seem to do so anywhere else. Perhaps this was due to concerns about the atomicity of "period - runtime", but that's just synchronization theater given that userspace has no way of writing to both atomically.

In any case, we shouldn't use the same lock for protecting osnoise_data members that we use for major reconfiguration procedures.

---
 kernel/trace/trace_osnoise.c | 43 +++++++++---------------------------
 1 file changed, 10 insertions(+), 33 deletions(-)
kernel/trace/trace_osnoise.c +10 -33
index 9206123ad445..9c7470ef4b03 100644
@@ -2494,29 +2494,24 @@ static int timerlat_fd_open(struct inode *inode, struct file *file)
 	struct osnoise_variables *osn_var;
 	struct timerlat_variables *tlat;
 	long cpu = (long) inode->i_cdev;
-	int ret = 0;
 
-	mutex_lock(&interface_lock);
+	guard(mutex)(&trace_types_lock);
 
 	/*
 	 * This file is accessible only if timerlat is enabled, and
 	 * NO_OSNOISE_WORKLOAD is set.
 	 */
-	if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options)) {
-		mutex_unlock(&interface_lock);
+	if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options))
 		return -EINVAL;
-	}
 
-	migrate_disable();
+	guard(migrate)();
 
 	osn_var = this_cpu_osn_var();
 	tlat = this_cpu_tmr_var();
-	mutex_lock(&osn_var->lock);
+	guard(mutex)(&osn_var->lock);
 
-	if (osn_var->fdd) {
-		ret = -EBUSY;
-		goto err;
-	}
+	if (osn_var->fdd)
+		return -EBUSY;
 
 	WARN_ON_ONCE(osn_var->kthread);
 
@@ -2527,16 +2522,12 @@ static int timerlat_fd_open(struct inode *inode, struct file *file)
 	 * worth doing this basic acceptance test to avoid obviusly wrong
 	 * setup.
 	 */
-	if (current->nr_cpus_allowed > 1 ||  cpu != smp_processor_id()) {
-		ret = -EPERM;
-		goto err;
-	}
+	if (current->nr_cpus_allowed > 1 ||  cpu != smp_processor_id())
+		return -EPERM;
 
 	fdd = kmalloc(sizeof(*fdd), GFP_KERNEL);
-	if (!fdd) {
-		ret = -ENOMEM;
-		goto err;
-	}
+	if (!fdd)
+		return -ENOMEM;
 
 	/*
 	 * From now on, it is good to go.
@@ -2553,25 +2544,11 @@ static int timerlat_fd_open(struct inode *inode, struct file *file)
 	osn_var->pid = current->pid;
 	osn_var->fdd = fdd;
 
-	/*
-	 * Setup is done.
-	 */
-	mutex_unlock(&osn_var->lock);
-	mutex_unlock(&interface_lock);
-
 	tlat = this_cpu_tmr_var();
 	tlat->count = 0;
 
 	hrtimer_setup(&tlat->timer, timerlat_irq, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
-
-	migrate_enable();
 	return 0;
-
-err:
-	mutex_unlock(&osn_var->lock);
-	mutex_unlock(&interface_lock);
-	migrate_enable();
-	return ret;
 };
 
 /*
Signature · 1 line
2.54.0
#9 lore raw

PATCH v5 7/8 tracing/osnoise: Print taint message for timerlat

In reply to Crystal Wood

Provide taint information the same as is done in print_osnoise_headers().

---
v2: new patch

---
 kernel/trace/trace_osnoise.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_osnoise.c +6 -0
index 9c7470ef4b03..2bc23a4c1b06 100644
@@ -576,6 +576,9 @@ static void record_osnoise_sample(struct osnoise_sample *sample)
 #ifdef CONFIG_PREEMPT_RT
 static void print_timerlat_headers(struct seq_file *s)
 {
+	if (osnoise_data.tainted)
+		seq_puts(s, "# timerlat is tainted!\n");
+
 	seq_puts(s, "#                                _-------=> irqs-off\n");
 	seq_puts(s, "#                               / _------=> need-resched\n");
 	seq_puts(s, "#                              | / _-----=> need-resched-lazy\n");
@@ -593,6 +596,9 @@ static void print_timerlat_headers(struct seq_file *s)
 #else /* CONFIG_PREEMPT_RT */
 static void print_timerlat_headers(struct seq_file *s)
 {
+	if (osnoise_data.tainted)
+		seq_puts(s, "# timerlat is tainted!\n");
+
 	seq_puts(s, "#                                _-----=> irqs-off\n");
 	seq_puts(s, "#                               / _----=> need-resched\n");
 	seq_puts(s, "#                              | / _---=> hardirq/softirq\n");
Signature · 1 line
2.54.0
#10 lore raw

PATCH v5 8/8 tracing/osnoise: Refuse timerlat fd open if tracer inactive

In reply to Crystal Wood

Besides being generally bad, allowing an fd open when there are no active instances means that osnoise_options_write() and osnoise_cpus_write() will leave it active during reconfiguration, causing at least WARN splats.

---
v2: new patch

---
 kernel/trace/trace_osnoise.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_osnoise.c +2 -1
index 2bc23a4c1b06..5835b9d77bb8 100644
@@ -2507,7 +2507,8 @@ static int timerlat_fd_open(struct inode *inode, struct file *file)
 	 * This file is accessible only if timerlat is enabled, and
 	 * NO_OSNOISE_WORKLOAD is set.
 	 */
-	if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options))
+	if (!osnoise_has_registered_instances() || !timerlat_enabled() ||
+	    test_bit(OSN_WORKLOAD, &osnoise_options))
 		return -EINVAL;
 
 	guard(migrate)();
Signature · 1 line
2.54.0