public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Tianxiang Chen <nanmu@xiaomi.com>
To: <rafael@kernel.org>
Cc: <viresh.kumar@linaro.org>, <linux-pm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <lingyue@xiaomi.com>,
	Tianxiang Chen <nanmu@xiaomi.com>
Subject: [PATCH] cpufreq: Fix race between suspend/resume and CPU hotplug
Date: Tue, 7 Apr 2026 17:35:29 +0800	[thread overview]
Message-ID: <20260407093529.4527-1-nanmu@xiaomi.com> (raw)

CPU hotplug operations can race with cpufreq_suspend()
and cpufreq_resume(), leading to null pointer dereferences
when accessing governor data. This occurs because there is
no synchronization between suspend/resume operations and
CPU hotplug, allowing concurrent access to
policy->governor_data while it is being freed or initialized.

Detailed race condition scenario:

1. Thread A (cpufreq_suspend) starts execution:
   - Iterates through active policies
   - Calls cpufreq_stop_governor(policy) for each policy
   - Sets cpufreq_suspended = true

2. Thread B (CPU hotplug) executes concurrently:
   - Calls cpu_down(cpu)
   - Calls cpuhp_cpufreq_offline(cpu)
   - Calls cpufreq_offline(cpu)
   - Inside cpufreq_offline():
     * Stops governor: policy->governor->stop(policy)
     * Exits governor: policy->governor->exit(policy)
     * Frees governor_data: kfree(policy->governor_data)
     * Sets policy->governor_data = NULL

3. Race window between step 1 and step 2:
   - Thread A is iterating policies and stopping governors
   - Thread B is concurrently executing CPU offline
   - Both threads may access the same policy->governor_data
   - Thread B frees governor_data while Thread A is still using it
   - Thread A accesses freed governor_data → null pointer dereference

Similarly, cpufreq_resume() can race with CPU hotplug where governor_data
is being initialized while hotplug is trying to access it, leading to
accessing uninitialized data.

Signed-off-by: Tianxiang Chen <nanmu@xiaomi.com>
---
 drivers/cpufreq/cpufreq.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 1f794524a1d9..8b03785764fa 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1979,6 +1979,7 @@ void cpufreq_suspend(void)
        if (!cpufreq_driver)
                return;

+       cpus_read_lock();
        if (!has_target() && !cpufreq_driver->suspend)
                goto suspend;

@@ -1998,6 +1999,7 @@ void cpufreq_suspend(void)

 suspend:
        cpufreq_suspended = true;
+       cpus_read_unlock();
 }

 /**
@@ -2017,10 +2019,11 @@ void cpufreq_resume(void)
        if (unlikely(!cpufreq_suspended))
                return;

+       cpus_read_lock();
        cpufreq_suspended = false;

        if (!has_target() && !cpufreq_driver->resume)
-               return;
+               goto out;

        pr_debug("%s: Resuming Governors\n", __func__);

@@ -2038,6 +2041,9 @@ void cpufreq_resume(void)
                                       __func__, policy->cpu);
                }
        }
+
+out:
+       cpus_read_unlock();
 }

 /**
--
2.34.1

#/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! This e-mail and its attachments contain confidential information from XIAOMI, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!******/#

             reply	other threads:[~2026-04-07  9:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-07  9:35 Tianxiang Chen [this message]
2026-04-07 11:50 ` [PATCH] cpufreq: Fix race between suspend/resume and CPU hotplug Rafael J. Wysocki
2026-04-08  1:46   ` [PATCH] cpufreq: fix race between hotplug and suspend Tianxiang Chen
2026-04-08 10:27     ` Rafael J. Wysocki
2026-04-08 14:19       ` [PATCH v2] cpufreq: Fix hotplug-suspend race during reboot Tianxiang Chen

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=20260407093529.4527-1-nanmu@xiaomi.com \
    --to=nanmu@xiaomi.com \
    --cc=lingyue@xiaomi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=viresh.kumar@linaro.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