From: Christian Loehle <christian.loehle@arm.com>
To: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>,
rafael@kernel.org, viresh.kumar@linaro.org, mingo@redhat.com,
peterz@infradead.org, juri.lelli@redhat.com,
vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
vschneid@redhat.com, kprateek.nayak@amd.com,
liaochang1@huawei.com
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] cpufreq: schedutil: Publish util hooks only after all sg_cpu are initialized
Date: Thu, 16 Jul 2026 13:25:32 +0100 [thread overview]
Message-ID: <6abab7b9-71cf-47d4-b7dd-def43be935bc@arm.com> (raw)
In-Reply-To: <20260716115159.848403-1-zhongqiu.han@oss.qualcomm.com>
On 7/16/26 12:51, Zhongqiu Han wrote:
> Commit 16a03c71bba0 ("cpufreq: schedutil: Merge initialization code of
> sg_cpu in single loop") merged the per-CPU initialization and the
> utilization-hook registration into a single loop in sugov_start().
>
> For a shared cpufreq policy this re-introduces the race originally fixed
> by commit ab2f7cf141aa ("cpufreq: schedutil: Fix sugov_start() versus
> sugov_update_shared() race").
>
> The scheduler's util path reaches the hook under RCU-sched and never takes
> policy->rwsem, so the rwsem held across sugov_start() cannot serialize the
> two. Once the first CPU's hook is published, sugov_update_shared() may run
> and, via sugov_next_freq_shared(), read/write each sibling sugov_cpu
> (iowait_boost, util, bw_min, ...) concurrently with the memset() still
> initializing them, with no lock common to both sides: the update side holds
> sg_policy->update_lock while the init side holds only policy->rwsem, which
> the scheduler's util path never takes.
>
> The walk only accesses scalar members, never a pointer like ->sg_policy,
> so it does not crash today; it merely uses stale (or zero on first start)
> values that skew the frequency selection and tracepoints. It is still a
> genuine data race, and a latent crash once any pointer member is
> dereferenced there.
>
> Restore the two-phase approach: initialize all per-CPU structures first,
> and only then publish the per-CPU utilization update hooks.
>
> Fixes: 16a03c71bba0 ("cpufreq: schedutil: Merge initialization code of sg_cpu in single loop")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
> ---
> kernel/sched/cpufreq_schedutil.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
> index dc0835ceee98..6ad080de8644 100644
> --- a/kernel/sched/cpufreq_schedutil.c
> +++ b/kernel/sched/cpufreq_schedutil.c
> @@ -872,8 +872,19 @@ static int sugov_start(struct cpufreq_policy *policy)
> memset(sg_cpu, 0, sizeof(*sg_cpu));
> sg_cpu->cpu = cpu;
> sg_cpu->sg_policy = sg_policy;
> + }
> +
> + /*
> + * Publish the hooks only after all per-CPU data is initialized, so a
> + * shared policy's sugov_update_shared() never reads an uninitialized
> + * sibling sugov_cpu.
> + */
> + for_each_cpu(cpu, policy->cpus) {
> + struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
> +
> cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu);
> }
> +
> return 0;
> }
>
Good catch!
Reviewed-by: Christian Loehle <christian.loehle@arm.com>
next prev parent reply other threads:[~2026-07-16 12:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-16 11:51 [PATCH] cpufreq: schedutil: Publish util hooks only after all sg_cpu are initialized Zhongqiu Han
2026-07-16 12:25 ` Christian Loehle [this message]
2026-07-22 13:22 ` Rafael J. Wysocki (Intel)
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=6abab7b9-71cf-47d4-b7dd-def43be935bc@arm.com \
--to=christian.loehle@arm.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=liaochang1@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=rostedt@goodmis.org \
--cc=stable@vger.kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=viresh.kumar@linaro.org \
--cc=vschneid@redhat.com \
--cc=zhongqiu.han@oss.qualcomm.com \
/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