public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] devfreq: Use lockdep asserts instead of manual checks for locked mutex
@ 2020-05-12  6:41 ` Krzysztof Kozlowski
  2020-05-12  8:06   ` Chanwoo Choi
  0 siblings, 1 reply; 2+ messages in thread
From: Krzysztof Kozlowski @ 2020-05-12  6:41 UTC (permalink / raw)
  To: MyungJoo Ham, Kyungmin Park, Chanwoo Choi, linux-pm, linux-kernel
  Cc: Krzysztof Kozlowski

Instead of warning when mutex_is_locked(), just use the lockdep
framework.  The code is smaller and checks could be disabled for
production environments (it is useful only during development).

Put asserts at beginning of function, even before validating arguments.

The behavior of update_devfreq() is now changed because lockdep assert
will only print a warning, not return with EINVAL.

Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>
---
 drivers/devfreq/devfreq.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index ef3d2bc3d1ac..52b9c3e141f3 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -60,12 +60,12 @@ static struct devfreq *find_device_devfreq(struct device *dev)
 {
 	struct devfreq *tmp_devfreq;
 
+	lockdep_assert_held(&devfreq_list_lock);
+
 	if (IS_ERR_OR_NULL(dev)) {
 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
 		return ERR_PTR(-EINVAL);
 	}
-	WARN(!mutex_is_locked(&devfreq_list_lock),
-	     "devfreq_list_lock must be locked.");
 
 	list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
 		if (tmp_devfreq->dev.parent == dev)
@@ -258,12 +258,12 @@ static struct devfreq_governor *find_devfreq_governor(const char *name)
 {
 	struct devfreq_governor *tmp_governor;
 
+	lockdep_assert_held(&devfreq_list_lock);
+
 	if (IS_ERR_OR_NULL(name)) {
 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
 		return ERR_PTR(-EINVAL);
 	}
-	WARN(!mutex_is_locked(&devfreq_list_lock),
-	     "devfreq_list_lock must be locked.");
 
 	list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
 		if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
@@ -289,12 +289,12 @@ static struct devfreq_governor *try_then_request_governor(const char *name)
 	struct devfreq_governor *governor;
 	int err = 0;
 
+	lockdep_assert_held(&devfreq_list_lock);
+
 	if (IS_ERR_OR_NULL(name)) {
 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
 		return ERR_PTR(-EINVAL);
 	}
-	WARN(!mutex_is_locked(&devfreq_list_lock),
-	     "devfreq_list_lock must be locked.");
 
 	governor = find_devfreq_governor(name);
 	if (IS_ERR(governor)) {
@@ -392,10 +392,7 @@ int update_devfreq(struct devfreq *devfreq)
 	int err = 0;
 	u32 flags = 0;
 
-	if (!mutex_is_locked(&devfreq->lock)) {
-		WARN(true, "devfreq->lock must be locked by the caller.\n");
-		return -EINVAL;
-	}
+	lockdep_assert_held(&devfreq->lock);
 
 	if (!devfreq->governor)
 		return -EINVAL;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] devfreq: Use lockdep asserts instead of manual checks for locked mutex
  2020-05-12  6:41 ` [PATCH] devfreq: Use lockdep asserts instead of manual checks for locked mutex Krzysztof Kozlowski
@ 2020-05-12  8:06   ` Chanwoo Choi
  0 siblings, 0 replies; 2+ messages in thread
From: Chanwoo Choi @ 2020-05-12  8:06 UTC (permalink / raw)
  To: Krzysztof Kozlowski, MyungJoo Ham, Kyungmin Park, linux-pm,
	linux-kernel

Hi Krzysztof,

On 5/12/20 3:41 PM, Krzysztof Kozlowski wrote:
> Instead of warning when mutex_is_locked(), just use the lockdep
> framework.  The code is smaller and checks could be disabled for
> production environments (it is useful only during development).
> 
> Put asserts at beginning of function, even before validating arguments.
> 
> The behavior of update_devfreq() is now changed because lockdep assert
> will only print a warning, not return with EINVAL.
> 
> Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>
> ---
>  drivers/devfreq/devfreq.c | 17 +++++++----------
>  1 file changed, 7 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index ef3d2bc3d1ac..52b9c3e141f3 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -60,12 +60,12 @@ static struct devfreq *find_device_devfreq(struct device *dev)
>  {
>  	struct devfreq *tmp_devfreq;
>  
> +	lockdep_assert_held(&devfreq_list_lock);
> +
>  	if (IS_ERR_OR_NULL(dev)) {
>  		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
>  		return ERR_PTR(-EINVAL);
>  	}
> -	WARN(!mutex_is_locked(&devfreq_list_lock),
> -	     "devfreq_list_lock must be locked.");
>  
>  	list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
>  		if (tmp_devfreq->dev.parent == dev)
> @@ -258,12 +258,12 @@ static struct devfreq_governor *find_devfreq_governor(const char *name)
>  {
>  	struct devfreq_governor *tmp_governor;
>  
> +	lockdep_assert_held(&devfreq_list_lock);
> +
>  	if (IS_ERR_OR_NULL(name)) {
>  		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
>  		return ERR_PTR(-EINVAL);
>  	}
> -	WARN(!mutex_is_locked(&devfreq_list_lock),
> -	     "devfreq_list_lock must be locked.");
>  
>  	list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
>  		if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
> @@ -289,12 +289,12 @@ static struct devfreq_governor *try_then_request_governor(const char *name)
>  	struct devfreq_governor *governor;
>  	int err = 0;
>  
> +	lockdep_assert_held(&devfreq_list_lock);
> +
>  	if (IS_ERR_OR_NULL(name)) {
>  		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
>  		return ERR_PTR(-EINVAL);
>  	}
> -	WARN(!mutex_is_locked(&devfreq_list_lock),
> -	     "devfreq_list_lock must be locked.");
>  
>  	governor = find_devfreq_governor(name);
>  	if (IS_ERR(governor)) {
> @@ -392,10 +392,7 @@ int update_devfreq(struct devfreq *devfreq)
>  	int err = 0;
>  	u32 flags = 0;
>  
> -	if (!mutex_is_locked(&devfreq->lock)) {
> -		WARN(true, "devfreq->lock must be locked by the caller.\n");
> -		return -EINVAL;
> -	}
> +	lockdep_assert_held(&devfreq->lock);
>  
>  	if (!devfreq->governor)
>  		return -EINVAL;
> 

It is reasonable. It looks good.
Applied it. Thank


-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-12  7:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20200512064211epcas1p1f8e6cac8f2927bd703e133282313dd43@epcas1p1.samsung.com>
2020-05-12  6:41 ` [PATCH] devfreq: Use lockdep asserts instead of manual checks for locked mutex Krzysztof Kozlowski
2020-05-12  8:06   ` Chanwoo Choi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox