diff mbox series

[v3] hwmon: nzxt-smart2: handle failure of devm_add_action in nzxt_smart2_hid_probe

Message ID 20230227091534.907101-1-void0red@gmail.com (mailing list archive)
State Accepted
Headers show
Series [v3] hwmon: nzxt-smart2: handle failure of devm_add_action in nzxt_smart2_hid_probe | expand

Commit Message

void0red Feb. 27, 2023, 9:15 a.m. UTC
From: Kang Chen <void0red@gmail.com>

1. replace the devm_add_action with devm_add_action_or_reset to ensure
the mutex lock can be destroyed when it fails.
2. use local wrapper function mutex_fini instead of mutex_destroy to
avoid undefined behaviours.
3. add a check of devm_add_action_or_reset and return early when it fails.

Link: https://lore.kernel.org/all/f5043281-9b3e-e454-16fe-ef4cde36dfdb@roeck-us.net
Signed-off-by: Kang Chen <void0red@gmail.com>
---
v3 -> v2: use local function and devm_add_action_or_rest
v2 -> v1: split the patch

 drivers/hwmon/nzxt-smart2.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Guenter Roeck March 10, 2023, 4:42 p.m. UTC | #1
On Mon, Feb 27, 2023 at 05:15:34PM +0800, void0red wrote:
> From: Kang Chen <void0red@gmail.com>
> 
> 1. replace the devm_add_action with devm_add_action_or_reset to ensure
> the mutex lock can be destroyed when it fails.
> 2. use local wrapper function mutex_fini instead of mutex_destroy to
> avoid undefined behaviours.
> 3. add a check of devm_add_action_or_reset and return early when it fails.
> 
> Link: https://lore.kernel.org/all/f5043281-9b3e-e454-16fe-ef4cde36dfdb@roeck-us.net
> Signed-off-by: Kang Chen <void0red@gmail.com>

Applied.

Thanks,
Guenter

> ---
> v3 -> v2: use local function and devm_add_action_or_rest
> v2 -> v1: split the patch
> 
>  drivers/hwmon/nzxt-smart2.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/nzxt-smart2.c b/drivers/hwmon/nzxt-smart2.c
> index 2b93ba896..340002581 100644
> --- a/drivers/hwmon/nzxt-smart2.c
> +++ b/drivers/hwmon/nzxt-smart2.c
> @@ -721,6 +721,11 @@ static int __maybe_unused nzxt_smart2_hid_reset_resume(struct hid_device *hdev)
>  	return init_device(drvdata, drvdata->update_interval);
>  }
>  
> +static void mutex_fini(void *lock)
> +{
> +	mutex_destroy(lock);
> +}
> +
>  static int nzxt_smart2_hid_probe(struct hid_device *hdev,
>  				 const struct hid_device_id *id)
>  {
> @@ -737,8 +742,9 @@ static int nzxt_smart2_hid_probe(struct hid_device *hdev,
>  	init_waitqueue_head(&drvdata->wq);
>  
>  	mutex_init(&drvdata->mutex);
> -	devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
> -			&drvdata->mutex);
> +	ret = devm_add_action_or_reset(&hdev->dev, mutex_fini, &drvdata->mutex);
> +	if (ret)
> +		return ret;
>  
>  	ret = hid_parse(hdev);
>  	if (ret)
diff mbox series

Patch

diff --git a/drivers/hwmon/nzxt-smart2.c b/drivers/hwmon/nzxt-smart2.c
index 2b93ba896..340002581 100644
--- a/drivers/hwmon/nzxt-smart2.c
+++ b/drivers/hwmon/nzxt-smart2.c
@@ -721,6 +721,11 @@  static int __maybe_unused nzxt_smart2_hid_reset_resume(struct hid_device *hdev)
 	return init_device(drvdata, drvdata->update_interval);
 }
 
+static void mutex_fini(void *lock)
+{
+	mutex_destroy(lock);
+}
+
 static int nzxt_smart2_hid_probe(struct hid_device *hdev,
 				 const struct hid_device_id *id)
 {
@@ -737,8 +742,9 @@  static int nzxt_smart2_hid_probe(struct hid_device *hdev,
 	init_waitqueue_head(&drvdata->wq);
 
 	mutex_init(&drvdata->mutex);
-	devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
-			&drvdata->mutex);
+	ret = devm_add_action_or_reset(&hdev->dev, mutex_fini, &drvdata->mutex);
+	if (ret)
+		return ret;
 
 	ret = hid_parse(hdev);
 	if (ret)