Re: [PATCH] powercap: dtpm_cpu: Fix NULL pointer dereference race during CPU offlining

From: Elazar Leibovich
Date: Sat Jun 28 2025 - 18:13:16 EST


On Fri, Jun 27, 2025 at 11:07 PM Rafael J. Wysocki <rafael@xxxxxxxxxx> wrote:
> > * DTPM power calculations are triggered (e.g., via sysfs reads) while CPU is online
> > * The CPU goes offline during the calculation, before em_cpu_get() is called
> > * em_cpu_get() now returns NULL since the energy model was unregistered
>
> But energy models for CPUs are never unregistered.
>

Can't the following happen (extremely rare, but still):

CPU gets set to impossible during shutdown sequence, e.g.

// arch/alpha/kernel/process.c
common_shutdown_1(void *generic_ptr)
...
set_cpu_possible(boot_cpuid, false);

Just before `get_cpu_device` is called by `em_cpu_get`.
Then `get_cpu_device` returns NULL for impossible CPU, causing
`em_cpu_get` to return NULL.

It's not a common scenario, but it seems NULL checking doesn't cost much,
and can assure us no rare case is crashing the system.