Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jie Zhan <zhanjie9@hisilicon.com>
To: Yaxiong Tian <tianyaxiong@kylinos.cn>, <cwchoi00@gmail.com>,
	<cw00.choi@samsung.com>, <myungjoo.ham@samsung.com>,
	<kyungmin.park@samsung.com>, <linux-pm@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>
Cc: <linuxarm@huawei.com>, <zhenglifeng1@huawei.com>,
	<zhangpengjie2@huawei.com>, <lihuisong@huawei.com>,
	<prime.zeng@hisilicon.com>
Subject: Re: [PATCH v2 5/6] devfreq: Get and put module refcount when switching governor
Date: Mon, 18 May 2026 16:39:51 +0800	[thread overview]
Message-ID: <66a14821-fd14-440e-b75f-98bc37eedba7@hisilicon.com> (raw)
In-Reply-To: <6f160e25-9903-4f49-8a95-1192b7acb96d@hisilicon.com>



On 5/15/2026 6:39 PM, Jie Zhan wrote:
> 
> 
> On 5/14/2026 2:50 PM, Yaxiong Tian wrote:
>>
>> 在 2026/5/13 17:38, Jie Zhan 写道:
>>> A governor module can be dynamically inserted or removed if compiled as a
>>> kernel module.  'devfreq->governor' would become NULL if the governor
>>> module is removed when it's in use.
>>>
>>> To prevent the governor module from being removed (except for force
>>> unload) when it's in use, get and put a refcount of the governor module
>>> when starting and stopping the governor.
>>>
>>> As a result, unloading a governor module in use returns an error, e.g.:
>>>    $ cat governor
>>>    performance
>>>    $ rmmod governor_performance
>>>    rmmod: ERROR: Module governor_performance is in use
>>>
>>> Signed-off-by: Jie Zhan <zhanjie9@hisilicon.com>
>>> ---
>>>   drivers/devfreq/devfreq.c | 17 ++++++++++++++++-
>>>   1 file changed, 16 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
>>> index e1363ab69173..2ea42325d030 100644
>>> --- a/drivers/devfreq/devfreq.c
>>> +++ b/drivers/devfreq/devfreq.c
>>> @@ -345,24 +345,37 @@ static int devfreq_set_governor(struct devfreq *df,
>>>                    __func__, df->governor->name, ret);
>>>               return ret;
>>>           }
>>> +        module_put(old_gov->owner);
>>>       }
>>>         /* Start the new governor */
>>> +    if (!try_module_get(new_gov->owner)) {
>>> +        df->governor = NULL;
>>> +        return -EINVAL;
>>> +    }
>>> +
>>
>> Here, new_gov has already been checked in try_then_request_governor() for whether the module can be obtained.
>>
>> I think it might be more reasonable to merge try_then_request_governor() and devfreq_set_governor(), so that when new_gov cannot be obtained, the operation of the old governor is not affected.
>>
> Well yeah, indeed! I'll take a look at how to implement this.
> Thanks for the suggestion.
> 
> Jie
Hi Yaxiong,

A follow-up after digging deeper.

1. To be clear, the try_module_get() / module_put() pairs in
try_then_request_governor() and devfreq_set_governor() protect different
ranges.  Assuming no FORCE_UNLOAD, the former protects the governor module
from the time it's queried until it's set as the working governor, while
the latter protects when the governor is in use.

However, I realized the former protection is no longer needed since I
removed the governor list lock in v2.  devfreq_remove_governor() and the
'request and set' governor sequence are already synchronized by
'devfreq_list_lock'. Thus, patch 6 should be dropped.

2. devfreq_add_device() needs error prints because it's a probe path.
governor_store() doesn't want error prints since it's not a good practice
to print on a sysfs read/write.  Merging the two functions will violate
either of them.

Thanks,
Jie
>>>       df->governor = new_gov;
>>>       ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
>>>       if (ret) {
>>>           dev_warn(dev, "%s: Governor %s not started(%d)\n",
>>>                __func__, df->governor->name, ret);
>>> +        module_put(new_gov->owner);
>>>             /* Restore previous governor */
>>>           df->governor = old_gov;
>>>           if (!df->governor)
>>>               return ret;
>>>   +        if (!try_module_get(old_gov->owner)) {
>>> +            df->governor = NULL;
>>> +            return -EINVAL;
>>> +        }
>>> +
>>>           ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
>>>           if (ret) {
>>>               dev_err(dev, "%s: restore Governor %s failed (%d)\n",
>>>                   __func__, df->governor->name, ret);
>>> +            module_put(old_gov->owner);
>>>               df->governor = NULL;
>>>               return ret;
>>>           }
>>> @@ -1040,9 +1053,11 @@ int devfreq_remove_device(struct devfreq *devfreq)
>>>         devfreq_cooling_unregister(devfreq->cdev);
>>>   -    if (devfreq->governor)
>>> +    if (devfreq->governor) {
>>>           devfreq->governor->event_handler(devfreq,
>>>                            DEVFREQ_GOV_STOP, NULL);
>>> +        module_put(devfreq->governor->owner);
>>> +    }
>>>       device_unregister(&devfreq->dev);
>>>         return 0;


  reply	other threads:[~2026-05-18  8:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13  9:38 [PATCH v2 0/6] devfreq: Add refcounts for governor modules Jie Zhan
2026-05-13  9:38 ` [PATCH v2 1/6] devfreq: Use mutex guard in governor_store() Jie Zhan
2026-05-14  6:02   ` Yaxiong Tian
2026-05-13  9:38 ` [PATCH v2 2/6] devfreq: Use mutex guard in devfreq_add/remove_governor() Jie Zhan
2026-05-14  6:02   ` Yaxiong Tian
2026-05-13  9:38 ` [PATCH v2 3/6] devfreq: Factor out devfreq_set_governor() Jie Zhan
2026-05-14  6:09   ` Yaxiong Tian
2026-05-15 10:32     ` Jie Zhan
2026-05-13  9:38 ` [PATCH v2 4/6] devfreq: Add module owner to devfreq governor Jie Zhan
2026-05-14  6:14   ` Yaxiong Tian
2026-05-15 10:32     ` Jie Zhan
2026-05-13  9:38 ` [PATCH v2 5/6] devfreq: Get and put module refcount when switching governor Jie Zhan
2026-05-14  6:50   ` Yaxiong Tian
2026-05-15 10:39     ` Jie Zhan
2026-05-18  8:39       ` Jie Zhan [this message]
2026-05-18  9:00         ` Yaxiong Tian
2026-05-13  9:38 ` [PATCH v2 6/6] devfreq: Get module refcount in try_then_request_governor() Jie Zhan

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=66a14821-fd14-440e-b75f-98bc37eedba7@hisilicon.com \
    --to=zhanjie9@hisilicon.com \
    --cc=cw00.choi@samsung.com \
    --cc=cwchoi00@gmail.com \
    --cc=kyungmin.park@samsung.com \
    --cc=lihuisong@huawei.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linuxarm@huawei.com \
    --cc=myungjoo.ham@samsung.com \
    --cc=prime.zeng@hisilicon.com \
    --cc=tianyaxiong@kylinos.cn \
    --cc=zhangpengjie2@huawei.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