Skip to contentSkip to conversation

rtc: spacemit: handle regmap_test_bits() error return

PATCH Applied [email protected] 3 participants 4 messages Jul 24 – Aug 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rtc: spacemit: handle regmap_test_bits() error return

From: Surendra Singh Chouhan <[email protected]>

p1_rtc_read_time() called if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) to check if the RTC was enabled.

regmap_test_bits() returns 1 if the bit is set, 0 if not set, and a negative error code (e.g. -EIO) if reading the control register fails. Using !regmap_test_bits(...) evaluates a negative error code as boolean false, causing I2C/regmap read failures to be ignored and incorrectly proceeding to read time registers from a failing device.

Fix this by capturing the return value of regmap_test_bits() and returning the error code if negative, or -EINVAL if the RTC is disabled.

Fixes a6de182daa2b ("rtc: spacemit: support the SpacemiT P1 RTC")

---
v2:
- Use ternary shorthand (ret ?: -EINVAL) as suggested by Alex Elder.
- Collect Reviewed-by tag from Alex Elder.

 drivers/rtc/rtc-spacemit-p1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/rtc/rtc-spacemit-p1.c +3 -2
index 43ab62494bb4..1de7bd995d29 100644
@@ -57,8 +57,9 @@ static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
 	u8 time[6];
 	int ret;
 
-	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
-		return -EINVAL;		/* RTC is disabled */
+	ret = regmap_test_bits(regmap, RTC_CTRL, RTC_EN);
+	if (ret <= 0)
+		return ret ?: -EINVAL;	/* RTC is disabled or error */
 
 	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
 	if (ret)
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The RTC time read retry loop in p1_rtc_read_time is vulnerable to a torn read (time corruption) if a time roll-over occurs during the register read. --

--- Patch [1]: [PATCH v2] rtc: spacemit: handle regmap_test_bits() error return --- commit a044092d776fab1e96ae2f8b48ace64aa0ab2325
Author: Surendra Singh Chouhan <[email protected]>

rtc: spacemit: handle regmap_test_bits() error return

p1_rtc_read_time() called if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) to check if the RTC was enabled. This evaluates negative error codes as boolean false, causing read failures to be ignored. Fix this by capturing the return value of regmap_test_bits() and returning the error code.

Quoted 16 lines
drivers/rtc/rtc-spacemit-p1.c +3 -2
index 43ab62494bb4b..1de7bd995d296 100644
@@ -57,8 +57,9 @@ static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
 	u8 time[6];
 	int ret;
 
-	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
-		return -EINVAL;		/* RTC is disabled */
+	ret = regmap_test_bits(regmap, RTC_CTRL, RTC_EN);
+	if (ret <= 0)
+		return ret ?: -EINVAL;	/* RTC is disabled or error */
 
 	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
 	if (ret)

drivers/rtc/rtc-spacemit-p1.c

Quoted 8 lines
drivers/rtc/rtc-spacemit-p1.c +0 -0
 		return ret;
 
 	do {
 		seconds = time[0];
 		ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
 		if (ret)
 			return ret;
 	} while (time[0] != seconds && --count);

[Severity: High]
This is a pre-existing issue, but does this loop correctly detect time roll-overs that happen during the read?

If a time roll-over occurs during the second regmap_bulk_read() in p1_rtc_read_time(), specifically after reading time[0] (seconds) but before reading higher units, time[0] will match the seconds from the previous read.

This would cause the loop to terminate early and accept the newly read time array.

Could this return a torn time from the RTC (for example, 14:00:59 instead of 13:59:59 or 14:00:00)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 50 lines · On 24/07/2026 14:06:33+0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The RTC time read retry loop in p1_rtc_read_time is vulnerable to a torn read (time corruption) if a time roll-over occurs during the register read. --

--- Patch [1]: [PATCH v2] rtc: spacemit: handle regmap_test_bits() error return --- commit a044092d776fab1e96ae2f8b48ace64aa0ab2325
Author: Surendra Singh Chouhan <[email protected]>

rtc: spacemit: handle regmap_test_bits() error return

p1_rtc_read_time() called if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) to check if the RTC was enabled. This evaluates negative error codes as boolean false, causing read failures to be ignored. Fix this by capturing the return value of regmap_test_bits() and returning the error code.

Quoted 16 lines
drivers/rtc/rtc-spacemit-p1.c +3 -2
index 43ab62494bb4b..1de7bd995d296 100644
@@ -57,8 +57,9 @@ static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
 	u8 time[6];
 	int ret;
 
-	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
-		return -EINVAL;		/* RTC is disabled */
+	ret = regmap_test_bits(regmap, RTC_CTRL, RTC_EN);
+	if (ret <= 0)
+		return ret ?: -EINVAL;	/* RTC is disabled or error */
 
 	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
 	if (ret)

drivers/rtc/rtc-spacemit-p1.c

Quoted 8 lines
drivers/rtc/rtc-spacemit-p1.c +0 -0
 		return ret;
 
 	do {
 		seconds = time[0];
 		ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
 		if (ret)
 			return ret;
 	} while (time[0] != seconds && --count);

[Severity: High]
This is a pre-existing issue, but does this loop correctly detect time roll-overs that happen during the read?

If a time roll-over occurs during the second regmap_bulk_read() in p1_rtc_read_time(), specifically after reading time[0] (seconds) but before reading higher units, time[0] will match the seconds from the previous read.

Quoted 6 lines

This would cause the loop to terminate early and accept the newly read time array.

Could this return a torn time from the RTC (for example, 14:00:59 instead of 13:59:59 or 14:00:00)?

While this is a valid concern, I guess this RTC latches the registers

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer Applied
#3 lore raw
In reply to [email protected]
Quoted 10 lines · On Fri, 24 Jul 2026 19:28:03 +0530, [email protected] wrote:

p1_rtc_read_time() called if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN)) to check if the RTC was enabled.

regmap_test_bits() returns 1 if the bit is set, 0 if not set, and a negative error code (e.g. -EIO) if reading the control register fails. Using !regmap_test_bits(...) evaluates a negative error code as boolean false, causing I2C/regmap read failures to be ignored and incorrectly proceeding to read time registers from a failing device.

[...]

Applied, thanks!

[1/1] rtc: spacemit: handle regmap_test_bits() error return

      https://git.kernel.org/abelloni/c/092755367e1a

Best regards,

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