Skip to contentSkip to conversation

rtc: Fix error handling in devm_rtc_allocate_device

PATCH In review Ma Ke 2 participants 2 messages Nov 9, 2025

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 rtc: Fix error handling in devm_rtc_allocate_device

In rtc_allocate_device(), device_initialize() sets the reference count to 1. In rtc_allocate_device(), when devm_add_action_or_reset() or dev_set_name() fails after successful device initialization via device_initialize(), rtc_allocate_device() returns an error without properly calling put_device() and releasing the reference count.

Add proper error handling that calls put_device() in all error paths after device_initialize(), ensuring proper resource cleanup.

Found by code review.

Fixes 3068a254d551 ("rtc: introduce new registration method")
---
 drivers/rtc/class.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/rtc/class.c +6 -2
index b1a2be1f9e3b..db5f33a22b14 100644
@@ -379,13 +379,17 @@ struct rtc_device *devm_rtc_allocate_device(struct device *dev)
 	rtc->dev.parent = dev;
 	err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
 	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
 	err = dev_set_name(&rtc->dev, "rtc%d", id);
 	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
 	return rtc;
+
+err_put_device:
+	put_device(&rtc->dev);
+	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
 
Signature · 1 line
2.17.1
#1 lore raw

Le 09/11/2025 à 07:08, Ma Ke a écrit :

Quoted 5 lines

In rtc_allocate_device(), device_initialize() sets the reference count to 1. In rtc_allocate_device(), when devm_add_action_or_reset() or dev_set_name() fails after successful device initialization via device_initialize(), rtc_allocate_device() returns an error without properly calling put_device() and releasing the reference count.

The correct error handling is already in place and your patch looks wrong.

Quoted 2 lines

Add proper error handling that calls put_device() in all error paths after device_initialize(), ensuring proper resource cleanup.

This is precisely the purpose of devm_add_action_or_reset(). Look at it and at the devm_rtc_release_device() which does what you expect.

CJ

Quoted 34 lines

Found by code review.

Fixes 3068a254d551 ("rtc: introduce new registration method")
---
  drivers/rtc/class.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/rtc/class.c +6 -2
index b1a2be1f9e3b..db5f33a22b14 100644
@@ -379,13 +379,17 @@ struct rtc_device *devm_rtc_allocate_device(struct device *dev)
  	rtc->dev.parent = dev;
  	err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
  	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
  	err = dev_set_name(&rtc->dev, "rtc%d", id);
  	if (err)
-		return ERR_PTR(err);
+		goto err_put_device;
 
  	return rtc;
+
+err_put_device:
+	put_device(&rtc->dev);
+	return ERR_PTR(err);
  }
  EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);