mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zihuan Zhang <zhangzihuan@kylinos.cn>
To: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Viresh Kumar <viresh.kumar@linaro.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Alim Akhtar <alim.akhtar@samsung.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	MyungJoo Ham <myungjoo.ham@samsung.com>,
	Kyungmin Park <kyungmin.park@samsung.com>,
	Chanwoo Choi <cw00.choi@samsung.com>,
	Jani Nikula <jani.nikula@linux.intel.com>,
	Rodrigo Vivi <rodrigo.vivi@intel.com>,
	Tvrtko Ursulin <tursulin@ursulin.net>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Daniel Lezcano <daniel.lezcano@kernel.org>,
	Sascha Hauer <s.hauer@pengutronix.de>,
	Shawn Guo <shawnguo@kernel.org>,
	Eduardo Valentin <edubezval@gmail.com>,
	Keerthy <j-keerthy@ti.com>, Ben Horgan <ben.horgan@arm.com>,
	zhenglifeng <zhenglifeng1@huawei.com>,
	Zhang Rui <rui.zhang@intel.com>, Len Brown <lenb@kernel.org>,
	Lukasz Luba <lukasz.luba@arm.com>,
	Pengutronix Kernel Team <kernel@pengutronix.de>,
	Beata Michalska <beata.michalska@arm.com>,
	Fabio Estevam <festevam@gmail.com>,
	Pavel Machek <pavel@kernel.org>, Sumit Gupta <sumitg@nvidia.com>,
	Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
	Sudeep Holla <sudeep.holla@arm.com>,
	Yicong Yang <yangyicong@hisilicon.com>,
	linux-pm@vger.kernel.org, linux-acpi@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org,
	linux-arm-kernel@lists.infradead.org,
	intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	imx@lists.linux.dev, linux-omap@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 07/10] powercap: dtpm_cpu: Use scope-based cleanup helper
Date: Fri, 5 Sep 2025 15:44:39 +0800	[thread overview]
Message-ID: <11bcb7cb-2169-479f-9247-a48e48e9c54e@kylinos.cn> (raw)
In-Reply-To: <CAJZ5v0g__9g_dfA3=4GVi351f4CKBegKkW9nU81j+Qu=2Hfg1Q@mail.gmail.com>


在 2025/9/4 21:17, Rafael J. Wysocki 写道:
> On Thu, Sep 4, 2025 at 12:38 PM Zihuan Zhang <zhangzihuan@kylinos.cn> wrote:
>>
>> 在 2025/9/3 21:45, Rafael J. Wysocki 写道:
>>> On Wed, Sep 3, 2025 at 3:18 PM Zihuan Zhang <zhangzihuan@kylinos.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@kylinos.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!


  reply	other threads:[~2025-09-05  7:45 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-03 13:17 [PATCH v4 00/10] cpufreq: use __free() for all cpufreq_cpu_get() references Zihuan Zhang
2025-09-03 13:17 ` [PATCH v4 01/10] arm64: topology: Use scope-based cleanup helper Zihuan Zhang
2025-09-05  9:38   ` Jonathan Cameron
2025-09-03 13:17 ` [PATCH v4 02/10] ACPI: processor: thermal: " Zihuan Zhang
2025-09-03 13:23   ` Rafael J. Wysocki
2025-09-05  9:45     ` Jonathan Cameron
2025-09-03 13:17 ` [PATCH v4 03/10] cpufreq: intel_pstate: " Zihuan Zhang
2025-09-05  9:47   ` Jonathan Cameron
2025-09-05  9:48   ` Rafael J. Wysocki
2025-09-03 13:17 ` [PATCH v4 04/10] cpufreq: powernv: " Zihuan Zhang
2025-09-05  9:49   ` Jonathan Cameron
2025-09-03 13:17 ` [PATCH v4 05/10] PM / devfreq: " Zihuan Zhang
2025-09-05 10:01   ` Jonathan Cameron
2025-09-03 13:17 ` [PATCH v4 06/10] drm/i915: " Zihuan Zhang
2025-09-05 10:02   ` Jonathan Cameron
2025-09-03 13:17 ` [PATCH v4 07/10] powercap: dtpm_cpu: " Zihuan Zhang
2025-09-03 13:45   ` Rafael J. Wysocki
2025-09-04 10:37     ` Zihuan Zhang
2025-09-04 10:55       ` Krzysztof Kozlowski
2025-09-04 13:17       ` Rafael J. Wysocki
2025-09-05  7:44         ` Zihuan Zhang [this message]
2025-09-03 13:17 ` [PATCH v4 08/10] thermal: imx: " Zihuan Zhang
2025-09-05 10:05   ` Jonathan Cameron
2025-09-05 10:21     ` Zihuan Zhang
2025-09-03 13:17 ` [PATCH v4 09/10] thermal/drivers/ti-soc-thermal: " Zihuan Zhang
2025-09-05  6:57   ` Andreas Kemnade
2025-09-05  7:02     ` Krzysztof Kozlowski
2025-09-05  7:54     ` Zihuan Zhang
2025-09-03 13:17 ` [PATCH v4 10/10] PM: EM: " Zihuan Zhang
2025-09-03 13:21   ` Krzysztof Kozlowski
2025-09-03 13:41     ` Rafael J. Wysocki
2025-09-03 13:43       ` Krzysztof Kozlowski
2025-09-04  7:56         ` Zihuan Zhang

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=11bcb7cb-2169-479f-9247-a48e48e9c54e@kylinos.cn \
    --to=zhangzihuan@kylinos.cn \
    --cc=airlied@gmail.com \
    --cc=alim.akhtar@samsung.com \
    --cc=beata.michalska@arm.com \
    --cc=ben.horgan@arm.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=cw00.choi@samsung.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=edubezval@gmail.com \
    --cc=festevam@gmail.com \
    --cc=imx@lists.linux.dev \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=j-keerthy@ti.com \
    --cc=jani.nikula@linux.intel.com \
    --cc=kernel@pengutronix.de \
    --cc=kyungmin.park@samsung.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=lukasz.luba@arm.com \
    --cc=mpe@ellerman.id.au \
    --cc=myungjoo.ham@samsung.com \
    --cc=pavel@kernel.org \
    --cc=ptsm@linux.microsoft.com \
    --cc=rafael@kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=rui.zhang@intel.com \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=srinivas.pandruvada@linux.intel.com \
    --cc=sudeep.holla@arm.com \
    --cc=sumitg@nvidia.com \
    --cc=thierry.reding@gmail.com \
    --cc=tursulin@ursulin.net \
    --cc=viresh.kumar@linaro.org \
    --cc=will@kernel.org \
    --cc=yangyicong@hisilicon.com \
    --cc=zhenglifeng1@huawei.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

all inboxes | Powered by JetHome®