[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <11bcb7cb-2169-479f-9247-a48e48e9c54e@kylinos.cn>
Date: Fri, 5 Sep 2025 15:44:39 +0800
From: Zihuan Zhang <zhangzihuan@...inos.cn>
To: "Rafael J. Wysocki" <rafael@...nel.org>
Cc: Viresh Kumar <viresh.kumar@...aro.org>,
Catalin Marinas <catalin.marinas@....com>, Will Deacon <will@...nel.org>,
Borislav Petkov <bp@...en8.de>, Dave Hansen <dave.hansen@...ux.intel.com>,
Srinivas Pandruvada <srinivas.pandruvada@...ux.intel.com>,
Michael Ellerman <mpe@...erman.id.au>, Alim Akhtar
<alim.akhtar@...sung.com>, Thierry Reding <thierry.reding@...il.com>,
MyungJoo Ham <myungjoo.ham@...sung.com>,
Kyungmin Park <kyungmin.park@...sung.com>,
Chanwoo Choi <cw00.choi@...sung.com>,
Jani Nikula <jani.nikula@...ux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@...el.com>, Tvrtko Ursulin
<tursulin@...ulin.net>, David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>, Daniel Lezcano <daniel.lezcano@...nel.org>,
Sascha Hauer <s.hauer@...gutronix.de>, Shawn Guo <shawnguo@...nel.org>,
Eduardo Valentin <edubezval@...il.com>, Keerthy <j-keerthy@...com>,
Ben Horgan <ben.horgan@....com>, zhenglifeng <zhenglifeng1@...wei.com>,
Zhang Rui <rui.zhang@...el.com>, Len Brown <lenb@...nel.org>,
Lukasz Luba <lukasz.luba@....com>,
Pengutronix Kernel Team <kernel@...gutronix.de>,
Beata Michalska <beata.michalska@....com>, Fabio Estevam
<festevam@...il.com>, Pavel Machek <pavel@...nel.org>,
Sumit Gupta <sumitg@...dia.com>,
Prasanna Kumar T S M <ptsm@...ux.microsoft.com>,
Sudeep Holla <sudeep.holla@....com>, Yicong Yang <yangyicong@...ilicon.com>,
linux-pm@...r.kernel.org, linux-acpi@...r.kernel.org,
linuxppc-dev@...ts.ozlabs.org, linux-arm-kernel@...ts.infradead.org,
intel-gfx@...ts.freedesktop.org, dri-devel@...ts.freedesktop.org,
imx@...ts.linux.dev, linux-omap@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v4 07/10] powercap: dtpm_cpu: Use scope-based cleanup
helper
在 2025/9/4 21:17, Rafael J. Wysocki 写道:
> On Thu, Sep 4, 2025 at 12:38 PM Zihuan Zhang <zhangzihuan@...inos.cn> wrote:
>>
>> 在 2025/9/3 21:45, Rafael J. Wysocki 写道:
>>> On Wed, Sep 3, 2025 at 3:18 PM Zihuan Zhang <zhangzihuan@...inos.cn> wrote:
>>>> Replace the manual cpufreq_cpu_put() with __free(put_cpufreq_policy)
>>>> annotation for policy references. This reduces the risk of reference
>>>> counting mistakes and aligns the code with the latest kernel style.
>>>>
>>>> No functional change intended.
>>>>
>>>> Signed-off-by: Zihuan Zhang <zhangzihuan@...inos.cn>
>>>> ---
>>>> drivers/powercap/dtpm_cpu.c | 30 +++++++++++-------------------
>>>> 1 file changed, 11 insertions(+), 19 deletions(-)
>>>>
>>>> diff --git a/drivers/powercap/dtpm_cpu.c b/drivers/powercap/dtpm_cpu.c
>>>> index 99390ec1481f..f76594185fa2 100644
>>>> --- a/drivers/powercap/dtpm_cpu.c
>>>> +++ b/drivers/powercap/dtpm_cpu.c
>>>> @@ -144,19 +144,17 @@ static int update_pd_power_uw(struct dtpm *dtpm)
>>>> static void pd_release(struct dtpm *dtpm)
>>>> {
>>>> struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
>>>> - struct cpufreq_policy *policy;
>>>>
>>>> if (freq_qos_request_active(&dtpm_cpu->qos_req))
>>>> freq_qos_remove_request(&dtpm_cpu->qos_req);
>>>>
>>>> - policy = cpufreq_cpu_get(dtpm_cpu->cpu);
>>>> - if (policy) {
>>>> + struct cpufreq_policy *policy __free(put_cpufreq_policy) =
>>>> + cpufreq_cpu_get(dtpm_cpu->cpu);
>>>> +
>>>> + if (policy)
>>>> for_each_cpu(dtpm_cpu->cpu, policy->related_cpus)
>>>> per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL;
>>>>
>>>> - cpufreq_cpu_put(policy);
>>>> - }
>>>> -
>>>> kfree(dtpm_cpu);
>>>> }
>>>>
>>>> @@ -192,7 +190,6 @@ static int cpuhp_dtpm_cpu_online(unsigned int cpu)
>>>> static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
>>>> {
>>>> struct dtpm_cpu *dtpm_cpu;
>>>> - struct cpufreq_policy *policy;
>>>> struct em_perf_state *table;
>>>> struct em_perf_domain *pd;
>>>> char name[CPUFREQ_NAME_LEN];
>>>> @@ -202,21 +199,19 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
>>>> if (dtpm_cpu)
>>>> return 0;
>>>>
>>>> - policy = cpufreq_cpu_get(cpu);
>>>> + struct cpufreq_policy *policy __free(put_cpufreq_policy) =
>>>> + cpufreq_cpu_get(cpu);
>>>> +
>>>> if (!policy)
>>>> return 0;
>>>>
>>>> pd = em_cpu_get(cpu);
>>>> - if (!pd || em_is_artificial(pd)) {
>>>> - ret = -EINVAL;
>>>> - goto release_policy;
>>>> - }
>>>> + if (!pd || em_is_artificial(pd))
>>>> + return -EINVAL;
>>>>
>>>> dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
>>>> - if (!dtpm_cpu) {
>>>> - ret = -ENOMEM;
>>>> - goto release_policy;
>>>> - }
>>>> + if (!dtpm_cpu)
>>>> + return -ENOMEM;
>>>>
>>>> dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
>>>> dtpm_cpu->cpu = cpu;
>>>> @@ -239,7 +234,6 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
>>>> if (ret < 0)
>>>> goto out_dtpm_unregister;
>>> So this change kind of goes against another recommendation given in cleanup.h:
>>>
>>> * Lastly, given that the benefit of cleanup helpers is removal of
>>> * "goto", and that the "goto" statement can jump between scopes, the
>>> * expectation is that usage of "goto" and cleanup helpers is never
>>> * mixed in the same function. I.e. for a given routine, convert all
>>> * resources that need a "goto" cleanup to scope-based cleanup, or
>>> * convert none of them.
>>
>> Should I replace all the memory allocation cleanups here with `__free`?
>> That would allow us to drop all the `goto`s, but since this function has
>> quite a few of them, I’m concerned it might introduce new issues. What’s
>> your recommendation?
> Frankly, don't use __free() in this code at all, at least for the time being.
>
> There is a problem with dropping the reference to policy at the end of
> __dtpm_cpu_setup() because that policy may be subsequently indirectly
> used in set_pd_power_limit() which calls
> freq_qos_update_request(&dtpm_cpu->qos_req, freq) and
> dtpm_cpu->qos_req->qos is policy->constraints, so using it will cause
> policy->constraints to be dereferenced in freq_qos_apply() which will
> crash and burn if the policy goes away in the meantime. So AFAICS
> __dtpm_cpu_setup() shouldn't call cpufreq_cpu_put() at all and the
> policy should be released in pd_release() without acquiring a new
> reference to it.
>
Thanks a lot for the detailed explanation!
Powered by blists - more mailing lists