Thread (14 messages) flat view 14 messages, 4 authors, 3d ago

Re: [PATCH v4 3/4] HID: wiimote: led_probe with scoped cleanup

From: "David Rheinsberg" <david@readahead.eu>
Date: 2026-09-20 08:17:38

Hi

On Mon, Aug 17, 2026, at 11:38 PM, Rafael Passos wrote:
quoted hunk ↗ jump to hunk
Cleanup code in led probe function, using the modern scoped cleanup.
This prevents mistakes in future changes to this function.

Signed-off-by: Rafael Passos <redacted>
---
 drivers/hid/hid-wiimote-modules.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/drivers/hid/hid-wiimote-modules.c 
b/drivers/hid/hid-wiimote-modules.c
index 3cd614466740..47fa6a8ecdae 100644
--- a/drivers/hid/hid-wiimote-modules.c
+++ b/drivers/hid/hid-wiimote-modules.c
@@ -341,11 +341,11 @@ static int wiimod_led_probe(const struct 
wiimod_ops *ops,
 {
 	struct device *dev = &wdata->hdev->dev;
 	size_t namesz = strlen(dev_name(dev)) + 9;
-	struct led_classdev *led;
 	char *name;
 	int ret;

-	led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
+	struct led_classdev *led __free(kfree) =
+		kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
We usually do not allow mixed declarations and code. So the variable declaration including the `__free(kfree)` should be moved to the top.

Otherwise, this looks good:

Reviewed-by: David Rheinsberg <david@readahead.eu>

Thanks!
quoted hunk ↗ jump to hunk
 	if (!led)
 		return -ENOMEM;
@@ -359,8 +359,12 @@ static int wiimod_led_probe(const struct wiimod_ops *ops,

 	wdata->leds[ops->arg] = led;
 	ret = led_classdev_register(dev, led);
-	if (ret)
-		goto err_free;
+	if (ret) {
+		wdata->leds[ops->arg] = NULL;
+		return ret;
+	}
+
+	retain_and_null_ptr(led);

 	/* enable LED1 to stop initial LED-blinking */
 	if (ops->arg == 0) {
@@ -369,11 +373,6 @@ static int wiimod_led_probe(const struct wiimod_ops *ops,
 	}

 	return 0;
-
-err_free:
-	wdata->leds[ops->arg] = NULL;
-	kfree(led);
-	return ret;
 }

 static void wiimod_led_remove(const struct wiimod_ops *ops,
-- 
2.55.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help