Linux-PM Archive on lore.kernel.org
 help / color / Atom feed
From: Leonard Crestez <leonard.crestez@nxp.com>
To: Chanwoo Choi <cw00.choi@samsung.com>
Cc: "MyungJoo Ham" <myungjoo.ham@samsung.com>,
	"Matthias Kaehlcke" <mka@chromium.org>,
	"Kyungmin Park" <kyungmin.park@samsung.com>,
	"Artur Świgoń" <a.swigon@partner.samsung.com>,
	"Saravana Kannan" <saravanak@google.com>,
	"Krzysztof Kozlowski" <krzk@kernel.org>,
	"Alexandre Bailon" <abailon@baylibre.com>,
	"Georgi Djakov" <georgi.djakov@linaro.org>,
	"Abel Vesa" <abel.vesa@nxp.com>, "Jacky Bai" <ping.bai@nxp.com>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Lukasz Luba" <l.luba@partner.samsung.com>,
	dl-linux-imx <linux-imx@nxp.com>,
	"linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH v9 4/8] PM / devfreq: Move more initialization before registration
Date: Fri, 1 Nov 2019 14:36:08 +0000
Message-ID: <VI1PR04MB70231670A8CF3CD8BDCA28EBEE620@VI1PR04MB7023.eurprd04.prod.outlook.com> (raw)
In-Reply-To: <6f9334b3-01f9-a7c5-a87b-7e8a77c8d6e0@samsung.com>

On 01.11.2019 10:25, Chanwoo Choi wrote:
> On 19. 10. 31. 오후 10:31, Leonard Crestez wrote:
>> On 31.10.2019 05:10, Chanwoo Choi wrote:
>>> Hi Leonard,
>>>
>>> This patch didn't get the acked-by from devfreq maintainer.
>>> I think that we need to discuss this patch with more time.
>>> Also, it is possible to make it as the separate patch
>>> from this series.
>>>
>>> IMHO, if you make the separate patch for this and
>>> resend the separate patch on later, I think that
>>> we can merge the remained patch related to PM_QOS.
>>
>> The devfreq initialization cleanups are required for dev_pm_qos support,
>> otherwise lockdep warnings are triggered. I can post the cleanups as a
>> separate series but the PM QoS one would depend on the cleanups.
>>
>> Do you prefer multiple smaller series?
> 
> After read the v10, I think v9 is better than v10
> for this issue. >>
>> I try to order my patches with uncontroversial fixes and cleanups first
>> so in theory the earlier parts could be applied separately. It's very
>> rare to see series partially applied though.
>>
>> Earlier objection was that devm should be kept, I think this can be
>> accomplished by splitting device_register into device_initialize and
>> device_add.
>>
>>> On 19. 10. 3. 오전 4:25, Leonard Crestez wrote:
>>>> In general it is a better to initialize an object before making it
>>>> accessible externally (through device_register).
>>>>
>>>> This makes it possible to avoid remove locking the partially initialized
>>>> object and simplifies the code. However devm is not available before
>>>> device_register (only after the device_initialize step) so the two
>>>> allocations need to be managed manually.
>>>>
>>>> Signed-off-by: Leonard Crestez <leonard.crestez@nxp.com>
>>>> Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
>>>> ---
>>>>    drivers/devfreq/devfreq.c | 43 +++++++++++++++++++++++----------------
>>>>    1 file changed, 25 insertions(+), 18 deletions(-)
>>>>
>>>> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
>>>> index 3e0e936185a3..0b40f40ee7aa 100644
>>>> --- a/drivers/devfreq/devfreq.c
>>>> +++ b/drivers/devfreq/devfreq.c
>>>> @@ -591,10 +591,12 @@ static void devfreq_dev_release(struct device *dev)
>>>>    	mutex_unlock(&devfreq_list_lock);
>>>>    
>>>>    	if (devfreq->profile->exit)
>>>>    		devfreq->profile->exit(devfreq->dev.parent);
>>>>    
>>>> +	kfree(devfreq->time_in_state);
>>>> +	kfree(devfreq->trans_table);
>>>>    	mutex_destroy(&devfreq->lock);
>>>>    	kfree(devfreq);
>>>>    }
>>>>    
>>>>    /**
>>>> @@ -674,44 +676,43 @@ struct devfreq *devfreq_add_device(struct device *dev,
>>>>    	devfreq->max_freq = devfreq->scaling_max_freq;
>>>>    
>>>>    	devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
>>>>    	atomic_set(&devfreq->suspend_count, 0);
>>>>    
>>>> -	dev_set_name(&devfreq->dev, "devfreq%d",
>>>> -				atomic_inc_return(&devfreq_no));
>>>> -	err = device_register(&devfreq->dev);
>>>> -	if (err) {
>>>> -		mutex_unlock(&devfreq->lock);
>>>> -		put_device(&devfreq->dev);
>>>> -		goto err_out;
>>>> -	}
>>>> -
>>>> -	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
>>>> +	devfreq->trans_table = kzalloc(
>>>>    			array3_size(sizeof(unsigned int),
>>>>    				    devfreq->profile->max_state,
>>>>    				    devfreq->profile->max_state),
>>>>    			GFP_KERNEL);
>>>>    	if (!devfreq->trans_table) {
>>>>    		mutex_unlock(&devfreq->lock);
>>>>    		err = -ENOMEM;
>>>> -		goto err_devfreq;
>>>> +		goto err_dev;
>>>>    	}
>>>>    
>>>> -	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
>>>> -			devfreq->profile->max_state,
>>>> -			sizeof(unsigned long),
>>>> -			GFP_KERNEL);
>>>> +	devfreq->time_in_state = kcalloc(devfreq->profile->max_state,
>>>> +					 sizeof(unsigned long),
>>>> +					 GFP_KERNEL);
>>>>    	if (!devfreq->time_in_state) {
>>>>    		mutex_unlock(&devfreq->lock);
>>>>    		err = -ENOMEM;
>>>> -		goto err_devfreq;
>>>> +		goto err_dev;
>>>>    	}
>>>>    
>>>>    	devfreq->last_stat_updated = jiffies;
>>>>    
>>>>    	srcu_init_notifier_head(&devfreq->transition_notifier_list);
>>>>    
>>>> +	dev_set_name(&devfreq->dev, "devfreq%d",
>>>> +				atomic_inc_return(&devfreq_no));
>>>> +	err = device_register(&devfreq->dev);
>>>> +	if (err) {
>>>> +		mutex_unlock(&devfreq->lock);
>>>> +		put_device(&devfreq->dev);
>>>> +		goto err_out;
> 
> err_out -> err_dev
> When failed to register, have to free resource.

According documentation on device_register resources need to be freed 
via put_device (which calls dev.release = devfreq_dev_release).

This chunk isn't new; it just appears so because other code was moved 
around it.

> 
>>>> +	}
>>>> +
>>>>    	mutex_unlock(&devfreq->lock);
>>>>    
>>>>    	mutex_lock(&devfreq_list_lock);
>>>>    
>>>>    	governor = try_then_request_governor(devfreq->governor_name);
>>>> @@ -737,14 +738,20 @@ struct devfreq *devfreq_add_device(struct device *dev,
>>>>    
>>>>    	return devfreq;
>>>>    
>>>>    err_init:
>>>>    	mutex_unlock(&devfreq_list_lock);
>>>> -err_devfreq:
>>>>    	devfreq_remove_device(devfreq);
>>>> -	devfreq = NULL;
>>>> +	return ERR_PTR(err);
> 
> 
> It is not proper to return on the middle
> of the exception handling. Need to consider more clean method.

Current code already does "devfreq = NULL" in order to skip the kfree 
below. There are already many cleanup/exit paths, it's just not very 
obvious.

Cleanup for errors before and after "device_register" is different and 
this is required by device core.

This can be improved by splitting device_register into device_initialize 
and device_add: this means that all early error paths do put_device and 
there is no longer any need to manually kfree(devfreq) because 
devfreq_dev_release is called on all cleanup paths. It also preserves devm:

https://patchwork.kernel.org/patch/11221873/

However there is still a difference between cleanup before/after 
device_add: if something fails after device_add you need to call 
devfreq_remove_device (which does device_unregister) and otherwise 
device_put.
>>>> +
>>>>    err_dev:
>>>> +	/*
>>>> +	 * Cleanup path for errors that happen before registration.
>>>> +	 * Otherwise we rely on devfreq_dev_release.
>>>> +	 */
>>>> +	kfree(devfreq->time_in_state);
>>>> +	kfree(devfreq->trans_table);
>>>>    	kfree(devfreq);
>>>>    err_out:
>>>>    	return ERR_PTR(err);
>>>>    }
>>>>    EXPORT_SYMBOL(devfreq_add_device);

  reply index

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-02 19:25 [PATCH v9 0/8] PM / devfreq: Add dev_pm_qos support Leonard Crestez
2019-10-02 19:25 ` [PATCH v9 1/8] PM / devfreq: Don't fail devfreq_dev_release if not in list Leonard Crestez
2019-10-02 19:25 ` [PATCH v9 2/8] PM / devfreq: Fix devfreq_notifier_call returning errno Leonard Crestez
2019-10-02 21:24   ` Matthias Kaehlcke
2019-10-02 23:47     ` Leonard Crestez
2019-10-31  2:29   ` Chanwoo Choi
2019-10-02 19:25 ` [PATCH v9 3/8] PM / devfreq: Set scaling_max_freq to max on OPP notifier error Leonard Crestez
2019-10-02 21:33   ` Matthias Kaehlcke
2019-10-31  2:21   ` Chanwoo Choi
2019-10-02 19:25 ` [PATCH v9 4/8] PM / devfreq: Move more initialization before registration Leonard Crestez
2019-10-31  3:15   ` Chanwoo Choi
2019-10-31 13:31     ` Leonard Crestez
2019-11-01  8:31       ` Chanwoo Choi
2019-11-01 14:36         ` Leonard Crestez [this message]
2019-10-02 19:25 ` [PATCH v9 5/8] PM / devfreq: Don't take lock in devfreq_add_device Leonard Crestez
2019-10-02 19:25 ` [PATCH v9 6/8] PM / devfreq: Introduce get_freq_range helper Leonard Crestez
2019-10-03 18:19   ` Matthias Kaehlcke
2019-10-03 19:16     ` Leonard Crestez
2019-10-03 19:36       ` Matthias Kaehlcke
2019-10-11 18:29     ` Matthias Kaehlcke
2019-10-31  2:44       ` Chanwoo Choi
2019-10-31  2:42   ` Chanwoo Choi
2019-10-31 13:12     ` Leonard Crestez
2019-10-02 19:25 ` [PATCH v9 7/8] PM / devfreq: Add PM QoS support Leonard Crestez
2019-10-02 22:22   ` Matthias Kaehlcke
2019-10-04 17:04   ` Matthias Kaehlcke
2019-10-31  3:01   ` Chanwoo Choi
2019-10-31 13:21     ` Leonard Crestez
2019-10-02 19:25 ` [PATCH v9 8/8] PM / devfreq: Use PM QoS for sysfs min/max_freq Leonard Crestez
2019-10-04 17:05   ` Matthias Kaehlcke
2019-10-31  3:10   ` Chanwoo Choi
2019-10-23 14:06 ` [PATCH v9 0/8] PM / devfreq: Add dev_pm_qos support Leonard Crestez
2019-10-23 16:34   ` Matthias Kaehlcke
2019-10-24 16:21     ` Leonard Crestez

Reply instructions:

You may reply publically 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=VI1PR04MB70231670A8CF3CD8BDCA28EBEE620@VI1PR04MB7023.eurprd04.prod.outlook.com \
    --to=leonard.crestez@nxp.com \
    --cc=a.swigon@partner.samsung.com \
    --cc=abailon@baylibre.com \
    --cc=abel.vesa@nxp.com \
    --cc=cw00.choi@samsung.com \
    --cc=georgi.djakov@linaro.org \
    --cc=krzk@kernel.org \
    --cc=kyungmin.park@samsung.com \
    --cc=l.luba@partner.samsung.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-pm@vger.kernel.org \
    --cc=mka@chromium.org \
    --cc=myungjoo.ham@samsung.com \
    --cc=ping.bai@nxp.com \
    --cc=saravanak@google.com \
    --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

Linux-PM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-pm/0 linux-pm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-pm linux-pm/ https://lore.kernel.org/linux-pm \
		linux-pm@vger.kernel.org
	public-inbox-index linux-pm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-pm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git