profiling: don't free prof_cpu_mask on init failure
PATCH v3 1/1 profiling: don't free prof_cpu_mask on init failure
From: Tristan Madani <[email protected]>
When profiling is enabled at runtime via /sys/kernel/profiling, profile_init() allocates prof_cpu_mask then 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 reads prof_cpu_mask on every tick, it can access the freed cpumask between the free and the next reboot.
Don't free prof_cpu_mask in the error path. The cpumask allocation already succeeded and is small; keeping it on this rare failure path avoids the stale access without additional synchronization.
Note: mainline removed prof_cpu_mask entirely in commit 7c51f7bbf057 ("profiling: remove prof_cpu_mask"). This is a minimal fix for stable trees where the variable is still present.
---
Changes in v3:
- Added comment explaining deliberate leak (Andrew Morton)
- Corrected Fixes tag from 22b8ce94708f to c309b917cab55 (Tetsuo Handa)
- Added stable-only context in commit message
Changes in v2:
- Remove the free_cpumask_var() call instead of adding a prof_on guard in profile_tick(), which still raced with the free (Tetsuo Handa)
kernel/profile.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/profile.c +8 -1
@@ -123,7 +123,14 @@ int __ref profile_init(void)if (prof_buffer)return 0 ; -free_cpumask_var (prof_cpu_mask); +/* +* Do not free prof_cpu_mask here. profile_tick() accesses it from +* timer interrupt context without synchronization, so freeing it +* while prof_on is set leads to a stale read. The cpumask is small +* and this error path is rare, so the leak is harmless. +* This code was removed entirely by commit 7c51f7bbf057 +* ("profiling: remove prof_cpu_mask") in v6.11. +*/ return -ENOMEM ; }
Signature · 1 line
2.47.3
Ummmm, not sure you should replace it with that long of a comment,
What you did was meant to cleanup useless crap. Let's not add more useless crap by adding 7 new lines. When it could be explained shorter IMHO, Here's a example, I mean, its not the best, but it works:
/* No need to free prof_cpu_mask()
* anymore, the error path is rare
* and the leak is harmless IMHO
*/
feel free to bikeshed. As per usual.
Also, here's some stable "rules" I tell most people
- Please please please add why this is bad in the commit description (you did that! Yay)
- A splat, please. It helps the stable folks not think that this is another "bug" (Do this if possible)
If I had to say "Bradley, just review this patch for what it does for f*ck sake", I'd say this patch is useful (sorry for my french!)
So.. here is the tag to add for V4,
Thanks!