Skip to contentSkip to conversation

rtc: assorted bug fixes across RTC drivers

PATCH In review Lee Jones 3 participants 13 messages Nov 23 – Nov 24

Subsystems (8)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover rtc: assorted bug fixes across RTC drivers

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

Cc "George G. Davis" <[email protected]>
Cc "Mark A. Greer" <[email protected]>
Cc "Tianping.Fang" <[email protected]>
Cc Adriana Stancu <[email protected]>
Cc Alexander Bigga <[email protected]>
Cc Alexandre Belloni <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andrew Jeffery <[email protected]>
Cc Andrew Morton <[email protected]>
Cc AngeloGioacchino Del Regno <[email protected]>
Cc Avi Fishman <[email protected]>
Cc Benjamin Fair <[email protected]>
Cc dmitry pervushin <[email protected]>
Cc Dmitry Rakhchev <[email protected]>
Cc Domen Puncer <[email protected]>
Cc Eddie Huang <[email protected]>
Cc Enric Balletbo i Serra <[email protected]>
Cc Eric Liu <[email protected]>
Cc Gary Bisson <[email protected]>
Cc Hsin-Hsiung Wang <[email protected]>
Cc Imre Kaloz <[email protected]>
Cc Jacky Huang <[email protected]>
Cc Johan Hovold <[email protected]>
Cc John Rigby <[email protected]>
Cc Joshua Kinard <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Matthias Fuchs <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Medad CChien <[email protected]>
Cc Mia Lin <[email protected]>
Cc Ming-Jen Chen <[email protected]>
Cc Nancy Yuen <[email protected]>
Cc Nicolas Boichat <[email protected]>
Cc Nishanth Menon <[email protected]>
Cc Patrick Venture <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Peter Hartley <[email protected]>
Cc Piotr Ziecik <[email protected]>
Cc Ran Bi <[email protected]>
Cc Ryan Brue <[email protected]>
Cc Sean Wang <[email protected]>
Cc Sergei Poselenov <[email protected]>
Cc Shan-Chun Hung <[email protected]>
Cc Tali Perry <[email protected]>
Cc Tomer Maimon <[email protected]>
Cc Wim Van Sebroeck <[email protected]>
Cc Wolfgang Grandegger <[email protected]>
Cc Wolfram Sang <[email protected]>
Cc Yingjoe Chen <[email protected]>

Lee Jones (10):
rtc: rx8025: pass parent I2C device in clock_adjust_ppb sysfs
callbacks
rtc: mt6397: handle non-DT platform device matching in probe
rtc: ma35d1: allocate RTC device before requesting interrupt
rtc: mpc5121: get interrupts via platform_get_irq()
rtc: m41t80: use devm helpers for square-wave clock registration
rtc: nct3018y: fix clock provider use-after-free and leak on unbind
rtc: stmp3xxx: unregister child watchdog platform device on unbind
rtc: ds1685: check driver data for NULL in ds1685_rtc_poweroff()
rtc: omap: prevent NULL and dangling pointer dereferences in power-off
path
rtc: bq32k: use u8 register buffer in trickle_charge_bypass sysfs

 drivers/rtc/rtc-bq32k.c    |  6 ++++--
 drivers/rtc/rtc-ds1685.c   |  6 ++----
 drivers/rtc/rtc-m41t80.c   |  5 +++--
 drivers/rtc/rtc-ma35d1.c   | 14 ++++++-------
 drivers/rtc/rtc-mpc5121.c  | 41 ++++++++++++++------------------------
 drivers/rtc/rtc-mt6397.c   | 19 ++++++++++++++++++
 drivers/rtc/rtc-nct3018y.c |  3 ++-
 drivers/rtc/rtc-omap.c     | 21 +++++++++++--------
 drivers/rtc/rtc-rx8025.c   |  4 ++--
 drivers/rtc/rtc-stmp3xxx.c | 29 ++++++++++++++++-----------
 10 files changed, 84 insertions(+), 64 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH 1/10 rtc: rx8025: pass parent I2C device in clock_adjust_ppb sysfs callbacks

Since rx8025_attr_group was converted to rtc_add_group(), the device pointer passed to rx8025_sysfs_show_clock_adjust() and rx8025_sysfs_store_clock_adjust() is the RTC class device (&rtc->dev) rather than the parent I2C client device. Passing dev directly to rx8025_read_offset() and rx8025_set_offset() invokes to_i2c_client() on the RTC class device, causing type confusion and an invalid pointer dereference during SMBus register access.

Pass dev->parent to rx8025_read_offset() and rx8025_set_offset() so they operate on the underlying I2C client device.

---
 drivers/rtc/rtc-rx8025.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-rx8025.c +2 -2
index 1ccec7b5ae42..ab3a30982863 100644
@@ -482,7 +482,7 @@ static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
 	int err;
 
 	dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
-	err = rx8025_read_offset(dev, &adj);
+	err = rx8025_read_offset(dev->parent, &adj);
 	if (err)
 		return err;
 
@@ -500,7 +500,7 @@ static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
 	if (kstrtol(buf, 10, &adj) != 0)
 		return -EINVAL;
 
-	err = rx8025_set_offset(dev, -adj);
+	err = rx8025_set_offset(dev->parent, -adj);
 
 	return err ? err : count;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

PATCH 2/10 rtc: mt6397: handle non-DT platform device matching in probe

In reply to Lee Jones

The parent MediaTek PMIC MFD driver instantiates RTC platform subdevices even when the optional RTC child Device Tree node is omitted, and devices can also be bound via sysfs driver_override. In those cases, of_device_get_match_data() returns NULL, yet mtk_rtc_probe() succeeds with rtc->data left NULL and mtk_rtc_write_trigger() subsequently crashes when dereferencing rtc->data->wrtgr.

Provide a platform_device_id table covering the MediaTek PMIC RTC cells and fall back to platform_get_device_id() when no OF match data is present, returning -ENODEV if neither match path provides driver data.

Fixes 29ee40091e27 ("rtc: mt6397: Add support for the MediaTek MT6358 RTC")
---
 drivers/rtc/rtc-mt6397.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/rtc/rtc-mt6397.c +19 -0
index ab402f9f85c3..c40644ae509a 100644
@@ -343,6 +343,14 @@ static int mtk_rtc_probe(struct platform_device *pdev)
 	rtc->addr_base = res->start;
 
 	rtc->data = of_device_get_match_data(&pdev->dev);
+	if (!rtc->data) {
+		const struct platform_device_id *id = platform_get_device_id(pdev);
+
+		if (!id)
+			return -ENODEV;
+
+		rtc->data = (const struct mtk_rtc_data *)id->driver_data;
+	}
 
 	rtc->irq = platform_get_irq(pdev, 0);
 	if (rtc->irq < 0)
@@ -427,6 +435,16 @@ static const struct of_device_id mt6397_rtc_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
 
+static const struct platform_device_id mt6397_rtc_id[] = {
+	{ "mt6323-rtc", (kernel_ulong_t)&mt6397_rtc_data },
+	{ "mt6357-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6358-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6359-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6397-rtc", (kernel_ulong_t)&mt6397_rtc_data },
+	{ }
+};
+MODULE_DEVICE_TABLE(platform, mt6397_rtc_id);
+
 static struct platform_driver mtk_rtc_driver = {
 	.driver = {
 		.name = "mt6397-rtc",
@@ -434,6 +452,7 @@ static struct platform_driver mtk_rtc_driver = {
 		.pm = &mt6397_pm_ops,
 	},
 	.probe	= mtk_rtc_probe,
+	.id_table = mt6397_rtc_id,
 };
 
 module_platform_driver(mtk_rtc_driver);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

PATCH 3/10 rtc: ma35d1: allocate RTC device before requesting interrupt

In reply to Lee Jones

ma35d1_rtc_probe() registers ma35d1_rtc_interrupt() via devm_request_irq() before allocating rtc->rtcdev via devm_rtc_allocate_device(). During driver unbind or if devm_rtc_register_device() fails, devres unwinds in reverse order and frees struct rtc_device in devm_rtc_release_device() before devm_irq_release() unregisters the interrupt handler. An RTC interrupt arriving in that teardown window passes a dangling rtc->rtcdev pointer to rtc_update_irq(), dereferencing freed memory and scheduling a freed work_struct.

Allocate and initialise rtc->rtcdev before requesting the IRQ so that devres frees the interrupt handler before releasing the RTC device.

Fixes dc0684adf3b6 ("rtc: Add driver for Nuvoton ma35d1 rtc controller")
---
 drivers/rtc/rtc-ma35d1.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/rtc/rtc-ma35d1.c +7 -7
index eb0da8b091dc..beeef6db92d9 100644
@@ -235,13 +235,6 @@ static int ma35d1_rtc_probe(struct platform_device *pdev)
 			return dev_err_probe(&pdev->dev, ret, "rtc init failed\n");
 	}
 
-	rtc->irq_num = platform_get_irq(pdev, 0);
-
-	ret = devm_request_irq(&pdev->dev, rtc->irq_num, ma35d1_rtc_interrupt,
-			       IRQF_NO_SUSPEND, "ma35d1rtc", rtc);
-	if (ret)
-		return dev_err_probe(&pdev->dev, ret, "Failed to request rtc irq\n");
-
 	platform_set_drvdata(pdev, rtc);
 
 	device_init_wakeup(&pdev->dev, true);
@@ -254,6 +247,13 @@ static int ma35d1_rtc_probe(struct platform_device *pdev)
 	rtc->rtcdev->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	rtc->rtcdev->range_max = RTC_TIMESTAMP_END_2099;
 
+	rtc->irq_num = platform_get_irq(pdev, 0);
+
+	ret = devm_request_irq(&pdev->dev, rtc->irq_num, ma35d1_rtc_interrupt,
+			       IRQF_NO_SUSPEND, "ma35d1rtc", rtc);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "Failed to request rtc irq\n");
+
 	ret = devm_rtc_register_device(rtc->rtcdev);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret, "Failed to register rtc device\n");
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#4 lore raw

PATCH 4/10 rtc: mpc5121: get interrupts via platform_get_irq()

In reply to Lee Jones

mpc5121_rtc_probe() maps its interrupts with irq_of_parse_and_map() and requests them via devm_request_irq(), while the probe error paths and mpc5121_rtc_remove() explicitly call irq_dispose_mapping(). This tears down and frees the IRQ descriptors before devres runs free_irq(), operating on destroyed IRQ descriptors and leaking irqaction entries. It also passes IRQ 0 to devm_request_irq() when irq_of_parse_and_map() fails.

Obtain both interrupts via platform_get_irq(), propagate negative error codes directly, and drop the manual irq_dispose_mapping() calls.

---
 drivers/rtc/rtc-mpc5121.c | 41 ++++++++++++++-------------------------
 1 file changed, 15 insertions(+), 26 deletions(-)
drivers/rtc/rtc-mpc5121.c +15 -26
index e1cef77c1a0e..331851378e8a 100644
@@ -11,7 +11,6 @@
 #include <linux/module.h>
 #include <linux/rtc.h>
 #include <linux/of.h>
-#include <linux/of_irq.h>
 #include <linux/platform_device.h>
 #include <linux/io.h>
 #include <linux/slab.h>
@@ -72,8 +71,8 @@ struct mpc5121_rtc_regs {
 };
 
 struct mpc5121_rtc_data {
-	unsigned irq;
-	unsigned irq_periodic;
+	int irq;
+	int irq_periodic;
 	struct mpc5121_rtc_regs __iomem *regs;
 	struct rtc_device *rtc;
 	struct rtc_wkalrm wkalarm;
@@ -307,30 +306,34 @@ static int mpc5121_rtc_probe(struct platform_device *op)
 
 	platform_set_drvdata(op, rtc);
 
-	rtc->irq = irq_of_parse_and_map(op->dev.of_node, 1);
+	rtc->irq = platform_get_irq(op, 1);
+	if (rtc->irq < 0)
+		return rtc->irq;
+
 	err = devm_request_irq(&op->dev, rtc->irq, mpc5121_rtc_handler, 0,
 			       "mpc5121-rtc", &op->dev);
 	if (err) {
 		dev_err(&op->dev, "%s: could not request irq: %i\n",
 							__func__, rtc->irq);
-		goto out_dispose;
+		return err;
 	}
 
-	rtc->irq_periodic = irq_of_parse_and_map(op->dev.of_node, 0);
+	rtc->irq_periodic = platform_get_irq(op, 0);
+	if (rtc->irq_periodic < 0)
+		return rtc->irq_periodic;
+
 	err = devm_request_irq(&op->dev, rtc->irq_periodic,
 			       mpc5121_rtc_handler_upd, 0, "mpc5121-rtc_upd",
 			       &op->dev);
 	if (err) {
 		dev_err(&op->dev, "%s: could not request irq: %i\n",
 						__func__, rtc->irq_periodic);
-		goto out_dispose2;
+		return err;
 	}
 
 	rtc->rtc = devm_rtc_allocate_device(&op->dev);
-	if (IS_ERR(rtc->rtc)) {
-		err = PTR_ERR(rtc->rtc);
-		goto out_dispose2;
-	}
+	if (IS_ERR(rtc->rtc))
+		return PTR_ERR(rtc->rtc);
 
 	rtc->rtc->ops = &mpc5200_rtc_ops;
 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc->features);
@@ -356,18 +359,7 @@ static int mpc5121_rtc_probe(struct platform_device *op)
 		rtc->rtc->range_max = U32_MAX;
 	}
 
-	err = devm_rtc_register_device(rtc->rtc);
-	if (err)
-		goto out_dispose2;
-
-	return 0;
-
-out_dispose2:
-	irq_dispose_mapping(rtc->irq_periodic);
-out_dispose:
-	irq_dispose_mapping(rtc->irq);
-
-	return err;
+	return devm_rtc_register_device(rtc->rtc);
 }
 
 static void mpc5121_rtc_remove(struct platform_device *op)
@@ -378,9 +370,6 @@ static void mpc5121_rtc_remove(struct platform_device *op)
 	/* disable interrupt, so there are no nasty surprises */
 	out_8(&regs->alm_enable, 0);
 	out_8(&regs->int_enable, in_8(&regs->int_enable) & ~0x1);
-
-	irq_dispose_mapping(rtc->irq);
-	irq_dispose_mapping(rtc->irq_periodic);
 }
 
 #ifdef CONFIG_OF
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#5 lore raw

PATCH 5/10 rtc: m41t80: use devm helpers for square-wave clock registration

In reply to Lee Jones

m41t80_sqw_register_clk() registers m41t80->sqw and its OF clock provider using unmanaged clk_register() and of_clk_add_provider(), and neither probe failure nor driver unbind unregisters them. Because m41t80->sqw is embedded inside the devm-allocated m41t80_data structure, unbinding the device or failing devm_rtc_register_device() frees m41t80_data while leaving the clock and OF provider registered, causing a resource leak and a use-after-free when clock callbacks or debugfs clk_summary access sqw_to_m41t80_data().

Use devm_clk_register() and devm_of_clk_add_hw_provider() tied to the I2C client device so both the provider and clock are automatically unregistered before m41t80_data is freed.

Fixes 1373e77b4f10 ("rtc: m41t80: add clock provider support")
---
 drivers/rtc/rtc-m41t80.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/rtc/rtc-m41t80.c +3 -2
index cfab27aaa518..5f8d6dcdeb5b 100644
@@ -604,9 +604,10 @@ static struct clk *m41t80_sqw_register_clk(struct m41t80_data *m41t80)
 	of_property_read_string(node, "clock-output-names", &init.name);
 
 	/* register the clock */
-	clk = clk_register(&client->dev, &m41t80->sqw);
+	clk = devm_clk_register(&client->dev, &m41t80->sqw);
 	if (!IS_ERR(clk))
-		of_clk_add_provider(node, of_clk_src_simple_get, clk);
+		devm_of_clk_add_hw_provider(&client->dev, of_clk_hw_simple_get,
+					    &m41t80->sqw);
 
 	return clk;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#6 lore raw

PATCH 6/10 rtc: nct3018y: fix clock provider use-after-free and leak on unbind

In reply to Lee Jones

nct3018y_clkout_register_clk() registers the CLKOUT clock via devm_clk_register() but adds the OF clock provider with unmanaged of_clk_add_provider() and never removes it. When the driver unbinds or devm_rtc_register_device() fails, devres unregisters and frees struct clk and nct3018y state while leaving a dangling pointer on the global of_clk_providers list, leaking the provider entry and OF node reference and causing a use-after-free on subsequent clock lookups.

Use devm_of_clk_add_hw_provider() so the OF clock provider is removed automatically before the clock and driver state are released.

Fixes 5adbaed16cc6 ("rtc: Add NCT3018Y real time clock driver")
---
 drivers/rtc/rtc-nct3018y.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/rtc/rtc-nct3018y.c +2 -1
index c770450e48b4..8349ddd1a9d6 100644
@@ -479,7 +479,8 @@ static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
 	clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
 
 	if (!IS_ERR(clk))
-		of_clk_add_provider(node, of_clk_src_simple_get, clk);
+		devm_of_clk_add_hw_provider(&client->dev, of_clk_hw_simple_get,
+					    &nct3018y->clkout_hw);
 
 	return clk;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#7 lore raw

PATCH 7/10 rtc: stmp3xxx: unregister child watchdog platform device on unbind

In reply to Lee Jones

stmp3xxx_wdt_register() creates and registers the child "stmp3xxx_rtc_wdt" platform device without tracking or unregistering it when the parent RTC driver unbinds, and also assigns static wdt_pdata directly to dev.platform_data where platform_device_release() passes it to kfree(). After unbind, devres unmaps rtc_data->io and frees rtc_data while the parent drvdata is cleared, so subsequent watchdog operations or reboot notifications invoking stmp3xxx_wdt_set_timeout() dereference torn-down parent state.

Register the child platform device via platform_device_register_data() so its platform data is heap-allocated, and tie platform_device_unregister() to the parent device with devm_add_action_or_reset() so the watchdog device is torn down before parent resources are released.

Fixes 1a71fb84fda6 ("rtc: stmp3xxx: add wdt-accessor function")
---
 drivers/rtc/rtc-stmp3xxx.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
drivers/rtc/rtc-stmp3xxx.c +17 -12
index d79e282e3817..a5281b519540 100644
@@ -92,24 +92,29 @@ static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
 	}
 }
 
-static struct stmp3xxx_wdt_pdata wdt_pdata = {
+static const struct stmp3xxx_wdt_pdata wdt_pdata = {
 	.wdt_set_timeout = stmp3xxx_wdt_set_timeout,
 };
 
+static void stmp3xxx_wdt_unregister(void *data)
+{
+	platform_device_unregister(data);
+}
+
 static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
 {
-	int rc = -1;
-	struct platform_device *wdt_pdev =
-		platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
-
-	if (wdt_pdev) {
-		wdt_pdev->dev.parent = &rtc_pdev->dev;
-		wdt_pdev->dev.platform_data = &wdt_pdata;
-		rc = platform_device_add(wdt_pdev);
-		if (rc)
-			platform_device_put(wdt_pdev);
-	}
+	struct platform_device *wdt_pdev;
+	int rc;
 
+	wdt_pdev = platform_device_register_data(&rtc_pdev->dev,
+						 "stmp3xxx_rtc_wdt",
+						 rtc_pdev->id, &wdt_pdata,
+						 sizeof(wdt_pdata));
+	rc = PTR_ERR_OR_ZERO(wdt_pdev);
+	if (!rc)
+		rc = devm_add_action_or_reset(&rtc_pdev->dev,
+					      stmp3xxx_wdt_unregister,
+					      wdt_pdev);
 	if (rc)
 		dev_err(&rtc_pdev->dev,
 			"failed to register stmp3xxx_rtc_wdt\n");
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#8 lore raw

PATCH 8/10 rtc: ds1685: check driver data for NULL in ds1685_rtc_poweroff()

In reply to Lee Jones

ip32_poweroff() invokes ds1685_rtc_poweroff() with the static ip32_rtc_device platform device. If ds1685_rtc_probe() previously failed or the device was unbound, platform_get_drvdata() returns NULL even though pdev is non-NULL, causing a NULL pointer dereference at rtc->irq_num instead of entering the intended fallback spin loop.

Verify that platform_get_drvdata() is non-NULL alongside pdev before dereferencing rtc.

Fixes aaaf5fbf56f1 ("rtc: add driver for DS1685 family of real time clocks")
---
 drivers/rtc/rtc-ds1685.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/rtc/rtc-ds1685.c +2 -4
index f27d6ae4f783..c577adf8414d 100644
@@ -1370,14 +1370,12 @@ ds1685_rtc_poweroff(struct platform_device *pdev)
 	struct ds1685_priv *rtc;
 
 	/* Check for valid RTC data, else, spin forever. */
-	if (unlikely(!pdev)) {
+	rtc = pdev ? platform_get_drvdata(pdev) : NULL;
+	if (unlikely(!rtc)) {
 		pr_emerg("platform device data not available, spinning forever ...\n");
 		while(1);
 		unreachable();
 	} else {
-		/* Get the rtc data. */
-		rtc = platform_get_drvdata(pdev);
-
 		/*
 		 * Disable our IRQ.  We're powering down, so we're not
 		 * going to worry about cleaning up.  Most of that should
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#9 lore raw

PATCH 9/10 rtc: omap: prevent NULL and dangling pointer dereferences in power-off path

In reply to Lee Jones

omap_rtc_remove() only clears the global omap_rtc_power_off_rtc pointer if pm_power_off still equals omap_rtc_power_off. If another driver overwrote pm_power_off after probe, unbinding the RTC leaves omap_rtc_power_off_rtc pointing to freed memory. Furthermore, omap_rtc_power_off_program() is exported and called directly from am33xx_rtc_only_idle() during suspend without checking whether omap_rtc_power_off_rtc is NULL, causing a NULL pointer dereference if omap_rtc_power_off_rtc was never assigned or was cleared on unbind.

Always clear omap_rtc_power_off_rtc when removing the matching RTC instance, and return -ENODEV from omap_rtc_power_off_program() when omap_rtc_power_off_rtc is NULL.

Fixes 222a12fca604 ("rtc: omap: add support for pmic_power_en")
---
 drivers/rtc/rtc-omap.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/rtc/rtc-omap.c +13 -8
index 3c978edd6c2c..156444fab916 100644
@@ -416,6 +416,9 @@ int omap_rtc_power_off_program(void)
 	int seconds;
 	u32 val;
 
+	if (!rtc)
+		return -ENODEV;
+
 	rtc->type->unlock(rtc);
 	/* enable pmic_power_en control */
 	val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
@@ -481,17 +484,19 @@ EXPORT_SYMBOL(omap_rtc_power_off_program);
  */
 static void omap_rtc_power_off(void)
 {
+	struct omap_rtc *rtc = omap_rtc_power_off_rtc;
 	u32 val;
 
-	omap_rtc_power_off_program();
+	if (omap_rtc_power_off_program())
+		return;
 
 	/* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */
-	omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
-	val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
+	rtc->type->unlock(rtc);
+	val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
 	val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
 			OMAP_RTC_PMIC_EXT_WKUP_EN(0);
-	rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
-	omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
+	rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
+	rtc->type->lock(rtc);
 
 	/*
 	 * Wait for alarm to trigger (within one second) and external PMIC to
@@ -913,9 +918,9 @@ static void omap_rtc_remove(struct platform_device *pdev)
 	struct omap_rtc *rtc = platform_get_drvdata(pdev);
 	u8 reg;
 
-	if (pm_power_off == omap_rtc_power_off &&
-			omap_rtc_power_off_rtc == rtc) {
-		pm_power_off = NULL;
+	if (omap_rtc_power_off_rtc == rtc) {
+		if (pm_power_off == omap_rtc_power_off)
+			pm_power_off = NULL;
 		omap_rtc_power_off_rtc = NULL;
 	}
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#10 lore raw

Re: [PATCH 09/10] rtc: omap: prevent NULL and dangling pointer dereferences in power-off path

Quoted 8 lines · Lee Jones

omap_rtc_remove() only clears the global omap_rtc_power_off_rtc pointer if pm_power_off still equals omap_rtc_power_off. If another driver overwrote pm_power_off after probe, unbinding the RTC leaves omap_rtc_power_off_rtc pointing to freed memory. Furthermore, omap_rtc_power_off_program() is exported and called directly from am33xx_rtc_only_idle() during suspend without checking whether omap_rtc_power_off_rtc is NULL, causing a NULL pointer dereference if omap_rtc_power_off_rtc was never assigned or was cleared on unbind.

This sounds like two separate issues and the am33xx_rtc_only_idle call() was added later by commit 5a99ae0092fe ("soc: ti: pm33xx: AM437X: Add rtc_only with ddr in self-refresh support").

Quoted 5 lines

Always clear omap_rtc_power_off_rtc when removing the matching RTC instance, and return -ENODEV from omap_rtc_power_off_program() when omap_rtc_power_off_rtc is NULL.

Fixes 222a12fca604 ("rtc: omap: add support for pmic_power_en")

So this isn't the right fixes tag for that.

Quoted 33 lines
---
 drivers/rtc/rtc-omap.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/rtc/rtc-omap.c +6 -1
index 3c978edd6c2c..156444fab916 100644
@@ -416,6 +416,9 @@ int omap_rtc_power_off_program(void)
 	int seconds;
 	u32 val;
 
+	if (!rtc)
+		return -ENODEV;
+
 	rtc->type->unlock(rtc);
 	/* enable pmic_power_en control */
 	val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
@@ -481,17 +484,19 @@ EXPORT_SYMBOL(omap_rtc_power_off_program);
  */
 static void omap_rtc_power_off(void)
 {
+	struct omap_rtc *rtc = omap_rtc_power_off_rtc;
 	u32 val;
 
-	omap_rtc_power_off_program();
+	if (omap_rtc_power_off_program())
+		return;
 
 	/* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */

drivers/rtc/rtc-omap.c EXPORT_SYMBOL(omap_rtc_power_off_program); · line 499

Quoted 10 lines
drivers/rtc/rtc-omap.c +4 -4
-	omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
-	val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
+	rtc->type->unlock(rtc);
+	val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
 	val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
 			OMAP_RTC_PMIC_EXT_WKUP_EN(0);
-	rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
-	omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
+	rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
+	rtc->type->lock(rtc);

This is an unrelated change.

Quoted 4 lines
drivers/rtc/rtc-omap.c +0 -0
 	/*
 	 * Wait for alarm to trigger (within one second) and external PMIC to
@@ -913,9 +918,9 @@ static void omap_rtc_remove(struct platform_device *pdev)
 	struct omap_rtc *rtc = platform_get_drvdata(pdev);

drivers/rtc/rtc-omap.c static void omap_rtc_remove(struct platform_device *pdev) · line 925

Quoted 10 lines
drivers/rtc/rtc-omap.c +3 -3
 	u8 reg;
 
-	if (pm_power_off == omap_rtc_power_off &&
-			omap_rtc_power_off_rtc == rtc) {
-		pm_power_off = NULL;
+	if (omap_rtc_power_off_rtc == rtc) {
+		if (pm_power_off == omap_rtc_power_off)
+			pm_power_off = NULL;
 		omap_rtc_power_off_rtc = NULL;
 	}

You can only have one legacy power off handler, but leaving the omap_rtc_power_off_rtc pointer set after unbind wasn't an issue before the later added am33xx_rtc_only_idle() call.

So with the Fixes tag fixed and the unrelated changes in omap_rtc_power_off() dropped I guess this can all go in one patch.

Johan

author
#11 lore raw

PATCH 10/10 rtc: bq32k: use u8 register buffer in trickle_charge_bypass sysfs

In reply to Lee Jones

bq32k_sysfs_show_tricklecharge_bypass() and bq32k_sysfs_store_tricklecharge_bypass() pass an uninitialised int to 1-byte bq32k_read() and bq32k_write() transfers. This leaves three bytes of reg uninitialised on the stack, and on big-endian systems populates the most significant byte while bitwise operations with BQ32K_TCFE inspect and modify the uninitialised least significant byte, leaking a bit of stack memory on reads and failing to update the hardware register on writes.

Declare reg as u8 in both callbacks.

Fixes 4c466872d8ae ("rtc: bq32000: add support to enable disable the trickle charge FET bypass")
---
 drivers/rtc/rtc-bq32k.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/rtc/rtc-bq32k.c +4 -2
index 0f50cfce88cb..7e5080eeee3a 100644
@@ -204,7 +204,8 @@ static ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev,
 					       struct device_attribute *attr,
 					       char *buf)
 {
-	int reg, error;
+	u8 reg;
+	int error;
 
 	error = bq32k_read(dev, &reg, BQ32K_CFG2, 1);
 	if (error)
@@ -217,7 +218,8 @@ static ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev,
 						struct device_attribute *attr,
 						const char *buf, size_t count)
 {
-	int reg, enable, error;
+	int enable, error;
+	u8 reg;
 
 	if (kstrtoint(buf, 0, &enable))
 		return -EINVAL;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog