mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Mario Limonciello <mario.limonciello@amd.com>
Cc: Perry Yuan <perry.yuan@amd.com>,
	"open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
	<linux-kernel@vger.kernel.org>,
	"open list:CPU FREQUENCY SCALING FRAMEWORK"
	<linux-pm@vger.kernel.org>, Sashiko <sashiko-bot@kernel.org>
Subject: Re: [PATCH] cpufreq/amd-pstate: Fix TOCTOU when changing driver mode via sysfs
Date: Thu, 1 Oct 2026 21:14:41 +0530	[thread overview]
Message-ID: <b673fe38-eb8c-47c0-83c3-732c9b122234@amd.com> (raw)
In-Reply-To: <20260922022347.1270746-1-mario.limonciello@amd.com>

Hello Mario

On 9/22/2026 7:53 AM, Mario Limonciello wrote:
> amd_pstate_update_status() evaluates mode_state_machine[cppc_state]
> [mode_idx] before taking amd_pstate_driver_lock, and then re-evaluates it
> again once the lock is held.  cppc_state is global and only stable under
> the lock, so concurrent sysfs writes race:
> 
>   CPU 0                               CPU 1
>   -----                               -----
>   // cppc_state == A, mode == B
>   if (mode_state_machine[A][B])       // sees non-NULL
>                                       if (mode_state_machine[A][B])
>                                         guard(&amd_pstate_driver_lock);
>                                         mode_state_machine[A][B](B);
>                                           cppc_state = B;
>     guard(&amd_pstate_driver_lock);
>     // cppc_state is now B
>     mode_state_machine[B][B](B);      // NULL -> NULL deref

Oof! Very interesting race that.

> 
> The re-read under the lock can resolve to a self-transition (NULL
> pointer, immediate NULL deref) or to an unexpected transition that
> redundantly runs amd_pstate_driver_cleanup(), double-freeing
> current_pstate_driver->attr.
> 
> Take the lock first, then read cppc_state and resolve the transition
> function exactly once into a local before calling it, so the check and
> the call observe the same state.
> 
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/bug/linux-f86ff3a6-55df-4eef-8e4d-62ac270dba25
> Fixes: 6f0b13f16f7a ("cpufreq/amd-pstate: Overhaul locking")
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

Feel free to include:

Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>

-- 
Thanks and Regards,
Prateek


      reply	other threads:[~2026-10-01 15:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-22  2:23 Mario Limonciello
2026-10-01 15:44 ` K Prateek Nayak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b673fe38-eb8c-47c0-83c3-732c9b122234@amd.com \
    --to=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=perry.yuan@amd.com \
    --cc=sashiko-bot@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®