Skip to contentSkip to conversation

hwmon: add a check of devm_add_action

PATCH Superseded Kang Chen 2 participants 8 messages Feb 27, 2023 – Mar 10, 2023 Hardware monitoring

lore
Share & tools
lore

Kang Chen

author
#0 lore raw

PATCH 1/1 hwmon: add a check of devm_add_action

devm_add_action may fails, do the cleanup when if fails.

---
 drivers/hwmon/g762.c        | 6 +++++-
 drivers/hwmon/nzxt-smart2.c | 4 +++-
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/hwmon/g762.c +5 -1
index 64a0599b2..d06d8bf20 100644
@@ -620,7 +620,11 @@ static int g762_of_clock_enable(struct i2c_client *client)
 	data = i2c_get_clientdata(client);
 	data->clk = clk;
 
-	devm_add_action(&client->dev, g762_of_clock_disable, data);
+	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
+	if (ret) {
+		dev_err(&client->dev, "failed to add disable clock action\n");
+		goto clk_unprep;
+	}
 	return 0;
 
  clk_unprep:
drivers/hwmon/nzxt-smart2.c +3 -1
index 2b93ba896..725974cb3 100644
@@ -737,8 +737,10 @@ 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,
+	ret = devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
 			&drvdata->mutex);
+	if (ret)
+		return ret;
 
 	ret = hid_parse(hdev);
 	if (ret)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 6 lines · Kang Chen

devm_add_action may fails, do the cleanup when if fails.

---
  drivers/hwmon/g762.c        | 6 +++++-
  drivers/hwmon/nzxt-smart2.c | 4 +++-

Two patches, please.

Guenter

Quoted 35 lines
  2 files changed, 8 insertions(+), 2 deletions(-)
drivers/hwmon/g762.c +5 -1
index 64a0599b2..d06d8bf20 100644
@@ -620,7 +620,11 @@ static int g762_of_clock_enable(struct i2c_client *client)
  	data = i2c_get_clientdata(client);
  	data->clk = clk;
 
-	devm_add_action(&client->dev, g762_of_clock_disable, data);
+	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
+	if (ret) {
+		dev_err(&client->dev, "failed to add disable clock action\n");
+		goto clk_unprep;
+	}
  	return 0;
 
   clk_unprep:
drivers/hwmon/nzxt-smart2.c +3 -1
index 2b93ba896..725974cb3 100644
@@ -737,8 +737,10 @@ 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,
+	ret = devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
  			&drvdata->mutex);
+	if (ret)
+		return ret;
 
  	ret = hid_parse(hdev);
  	if (ret)
author
#2 lore raw

PATCH v2 1/2 hwmon: g762: add a check of devm_add_action in g762_of_clock_enable

From: Kang Chen <[email protected]>

devm_add_action may fails, check it and do the cleanup.

---
v2 -> v1: split the patch

 drivers/hwmon/g762.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwmon/g762.c +6 -1
index 64a0599b2..e2c3c34f0 100644
@@ -620,7 +620,12 @@ static int g762_of_clock_enable(struct i2c_client *client)
 	data = i2c_get_clientdata(client);
 	data->clk = clk;
 
-	devm_add_action(&client->dev, g762_of_clock_disable, data);
+	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
+	if (ret) {
+		dev_err(&client->dev, "failed to add disable clock action\n");
+		goto clk_unprep;
+	}
+
 	return 0;
 
  clk_unprep:
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 2/2 hwmon: nzxt-smart2: add a check of devm_add_action in nzxt_smart2_hid_probe

From: Kang Chen <[email protected]>

devm_add_action may fails, check it and return early.

---
v2 -> v1: split the patch

 drivers/hwmon/nzxt-smart2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +3 -1
index 2b93ba896..725974cb3 100644
@@ -737,8 +737,10 @@ 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,
+	ret = devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
 			&drvdata->mutex);
+	if (ret)
+		return ret;
 
 	ret = hid_parse(hdev);
 	if (ret)
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH v2 2/2] hwmon: nzxt-smart2: add a check of devm_add_action in nzxt_smart2_hid_probe

Quoted 16 lines · On 2/26/23 19:09, void0red wrote:

From: Kang Chen <[email protected]>

devm_add_action may fails, check it and return early.

---
v2 -> v1: split the patch

  drivers/hwmon/nzxt-smart2.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +0 -0
index 2b93ba896..725974cb3 100644

drivers/hwmon/nzxt-smart2.c static int nzxt_smart2_hid_probe(struct hid_device *hdev, · line 741

Quoted 6 lines
drivers/hwmon/nzxt-smart2.c +1 -1
@@ -737,8 +737,10 @@ 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,
+	ret = devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
  			&drvdata->mutex);

Please watch for multi-line alignment. Also, turns out the original code is wrong: Type casting a function pointer argument like this, while it typically works in practice, is undefined in C. The function pointer passed to devm_add_action() needs to point to a local function with void * argument, and that function needs to call mutex_destroy() with the same argument. Also, based on the context, this needs to call devm_add_action_or_reset() to ensure that the destroy function is called on error.

Thanks,
Guenter

Quoted 5 lines
drivers/hwmon/nzxt-smart2.c +2 -0
+	if (ret)
+		return ret;
 
  	ret = hid_parse(hdev);
  	if (ret)
author
#5 lore raw

PATCH v3 1/1 hwmon: nzxt-smart2: handle failure of devm_add_action in nzxt_smart2_hid_probe

From: Kang Chen <[email protected]>

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.

---
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(-)
drivers/hwmon/nzxt-smart2.c +8 -2
index 2b93ba896..340002581 100644
@@ -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)
Signature · 1 line
2.34.1
maintainer Applied
#6 lore raw

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

Quoted 10 lines · On Mon, Feb 27, 2023 at 05:15:34PM +0800, void0red wrote:

From: Kang Chen <[email protected]>

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.

Applied.

Thanks,
Guenter

Quoted 35 lines

---
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(-)
drivers/hwmon/nzxt-smart2.c +8 -2
index 2b93ba896..340002581 100644
@@ -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)
maintainer Applied
#7 lore raw

Re: [PATCH v2 1/2] hwmon: g762: add a check of devm_add_action in g762_of_clock_enable

In reply to void0red
Quoted 5 lines · On Mon, Feb 27, 2023 at 11:09:12AM +0800, void0red wrote:

From: Kang Chen <[email protected]>

devm_add_action may fails, check it and do the cleanup.

Applied.

Thanks,
Guenter

Quoted 24 lines

---
v2 -> v1: split the patch

 drivers/hwmon/g762.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwmon/g762.c +6 -1
index 64a0599b2..e2c3c34f0 100644
@@ -620,7 +620,12 @@ static int g762_of_clock_enable(struct i2c_client *client)
 	data = i2c_get_clientdata(client);
 	data->clk = clk;
 
-	devm_add_action(&client->dev, g762_of_clock_disable, data);
+	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
+	if (ret) {
+		dev_err(&client->dev, "failed to add disable clock action\n");
+		goto clk_unprep;
+	}
+
 	return 0;
 
  clk_unprep: