Skip to contentSkip to conversation

rtc: sysfs: Bail out earlier if no new groups provided

PATCH Applied Andy Shevchenko 2 participants 2 messages Jul 2, 2025 – Jul 23, 2025 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: sysfs: Bail out earlier if no new groups provided

When there is no new groups provided, no need to reallocate memory, copy the old ones and free them in order to do nothing. Do nothing instead.

---
 drivers/rtc/sysfs.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/rtc/sysfs.c +8 -4
index 2230241285d0..4ab05e105a76 100644
@@ -310,17 +310,21 @@ int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
 	size_t old_cnt = 0, add_cnt = 0, new_cnt;
 	const struct attribute_group **groups, **old;
 
-	if (!grps)
+	if (grps) {
+		for (groups = grps; *groups; groups++)
+			add_cnt++;
+		/* No need to modify current groups if nothing new is provided */
+		if (add_cnt == 0)
+			return 0;
+	} else {
 		return -EINVAL;
+	}
 
 	groups = rtc->dev.groups;
 	if (groups)
 		for (; *groups; groups++)
 			old_cnt++;
 
-	for (groups = grps; *groups; groups++)
-		add_cnt++;
-
 	new_cnt = old_cnt + add_cnt + 1;
 	groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
 	if (!groups)
Signature · 1 line
2.47.2
maintainer Applied
#1 lore raw
Quoted 5 lines · Andy Shevchenko

When there is no new groups provided, no need to reallocate memory, copy the old ones and free them in order to do nothing. Do nothing instead.

Applied, thanks!

[1/1] rtc: sysfs: Bail out earlier if no new groups provided

      https://git.kernel.org/abelloni/c/bbe8d4fef308

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com