Re: [PATCH v2] profiling: don't free prof_cpu_mask on init failure

From: Andrew Morton

Date: Wed Jun 24 2026 - 21:13:17 EST


On Mon, 22 Jun 2026 00:00:22 +0000 Tristan Madani <tristmd@xxxxxxxxx> wrote:

> From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
>
> When profiling is enabled at runtime via /sys/kernel/profiling,
> profile_setup() sets prof_on and 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 checks
> cpumask_available(prof_cpu_mask), it can access the freed cpumask
> between the free and the next reboot.
>
> Remove the free_cpumask_var() call from the error path. The cpumask
> allocation already succeeded and is small; keeping it on this rare
> failure path is harmless.
>
> ...
>
> --- a/kernel/profile.c
> +++ b/kernel/profile.c
> @@ -123,7 +123,6 @@ int __ref profile_init(void)
> if (prof_buffer)
> return 0;
>
> - free_cpumask_var(prof_cpu_mask);
> return -ENOMEM;
> }

Confused. Current mainline has no free_cpumask_var() here?

If we're to deliberately leak the mask here then let's have a little
comment explaining the reasoning, so we don't later receive "profiling:
fix memory leak" patches.