mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] profiling: prevent stale prof_cpu_mask access on init failure
@ 2026-06-21 19:23 Tristan Madani
  2026-06-21 22:49 ` Tetsuo Handa
  2026-06-22  0:00 ` [PATCH v2] profiling: don't free prof_cpu_mask " Tristan Madani
  0 siblings, 2 replies; 9+ messages in thread
From: Tristan Madani @ 2026-06-21 19:23 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ingo Molnar, Dave Hansen, Tetsuo Handa, linux-kernel, stable,
	Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

When profiling is enabled at runtime via /sys/kernel/profiling,
profile_setup() sets prof_on and profile_init() allocates prof_cpu_mask
and attempts to allocate prof_buffer. If all prof_buffer allocations
fail, the error path frees prof_cpu_mask but leaves prof_on set.

Since profile_tick() runs from timer interrupt context and checks
cpumask_available(prof_cpu_mask) without first checking prof_on, it can
dereference the freed cpumask between the free and the next reboot.

Clear prof_on before freeing prof_cpu_mask so the profiling state remains
consistent on allocation failure. Also gate the cpumask access in
profile_tick() on prof_on to prevent accessing stale state during the
teardown window.

Fixes: 22b8ce94708f ("profiling: dynamically enable readprofile at runtime")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 kernel/profile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/profile.c b/kernel/profile.c
index 984f819b701c9..a166ad9512714 100644
--- a/kernel/profile.c
+++ b/kernel/profile.c
@@ -123,6 +123,7 @@ int __ref profile_init(void)
 	if (prof_buffer)
 		return 0;
 
+	prof_on = 0;
 	free_cpumask_var(prof_cpu_mask);
 	return -ENOMEM;
 }
@@ -325,7 +326,7 @@ void profile_tick(int type)
 {
 	struct pt_regs *regs = get_irq_regs();
 
-	if (!user_mode(regs) && cpumask_available(prof_cpu_mask) &&
+	if (!user_mode(regs) && prof_on && cpumask_available(prof_cpu_mask) &&
 	    cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
 		profile_hit(type, (void *)profile_pc(regs));
 }
-- 
2.47.3


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-06-26 15:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-21 19:23 [PATCH] profiling: prevent stale prof_cpu_mask access on init failure Tristan Madani
2026-06-21 22:49 ` Tetsuo Handa
2026-06-21 23:45   ` Tristan Madani
2026-06-22  0:00 ` [PATCH v2] profiling: don't free prof_cpu_mask " Tristan Madani
2026-06-22 11:32   ` Tetsuo Handa
2026-06-22 18:28     ` Tristan Madani
2026-06-22 22:50       ` Tetsuo Handa
2026-06-25  1:11   ` Andrew Morton
2026-06-26 15:36     ` Tristan Madani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®