The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] drivers: rtc: handle OTF clock changes
@ 2026-06-24 12:31 Elad Nachman
  2026-06-24 15:31 ` Alexandre Belloni
  2026-08-07 13:05 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Elad Nachman @ 2026-06-24 12:31 UTC (permalink / raw)
  To: alexandre.belloni, linux-rtc, linux-kernel; +Cc: enachman

From: Elad Nachman <enachman@marvell.com>

When processing expired RTC events and rearming them, use now
instead of expiry to prevent endless loops.
Issue seen with Armada 385 SOC.

Fixes commit 6610e0893b8b ("RTC: Rework RTC code to use timerqueue for events")
Signed-off-by: Elad Nachman <enachman@marvell.com>
---
 drivers/rtc/interface.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 96626f8068f9..c32ef95a07d4 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -126,6 +126,7 @@ EXPORT_SYMBOL_GPL(rtc_read_time);
 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
 {
 	int err, uie;
+	struct rtc_time new_tm;
 
 	err = rtc_valid_tm(tm);
 	if (err != 0)
@@ -159,6 +160,17 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
 	else
 		err = -EINVAL;
 
+	if (rtc && rtc->ops && rtc->ops->read_time) {
+		if (!rtc->ops->read_time(rtc->dev.parent, &new_tm)) {
+			pr_debug("new rtc time secs %d mins %d hours %d mday %d mon %d year %d way %d yday %d dst %d\n",
+					new_tm.tm_sec, new_tm.tm_min,
+					new_tm.tm_hour, new_tm.tm_mday,
+					new_tm.tm_mon, new_tm.tm_year,
+					new_tm.tm_wday, new_tm.tm_yday,
+					new_tm.tm_isdst);
+		}
+	}
+
 	pm_stay_awake(rtc->dev.parent);
 	mutex_unlock(&rtc->ops_lock);
 	/* A timer might have just expired */
@@ -999,7 +1011,7 @@ void rtc_timer_do_work(struct work_struct *work)
 		trace_rtc_timer_fired(timer);
 		/* Re-add/fwd periodic timers */
 		if (ktime_to_ns(timer->period)) {
-			timer->node.expires = ktime_add(timer->node.expires,
+			timer->node.expires = ktime_add(now,
 							timer->period);
 			timer->enabled = 1;
 			timerqueue_add(&rtc->timerqueue, &timer->node);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers: rtc: handle OTF clock changes
  2026-06-24 12:31 [PATCH] drivers: rtc: handle OTF clock changes Elad Nachman
@ 2026-06-24 15:31 ` Alexandre Belloni
  2026-06-28 17:34   ` [EXTERNAL] " Elad Nachman
  2026-08-07 13:05 ` Dan Carpenter
  1 sibling, 1 reply; 4+ messages in thread
From: Alexandre Belloni @ 2026-06-24 15:31 UTC (permalink / raw)
  To: Elad Nachman; +Cc: linux-rtc, linux-kernel

On 24/06/2026 15:31:03+0300, Elad Nachman wrote:
> From: Elad Nachman <enachman@marvell.com>
> 
> When processing expired RTC events and rearming them, use now
> instead of expiry to prevent endless loops.
> Issue seen with Armada 385 SOC.

The loop is not endless, it may be long however. How do you reproduce
this? Or maybe the question is what is enabling PIE on your system?

Your patch breaks existing code because it will expect to get one event
per elapsed period while your patch will cause events to be skipped.

> 
> Fixes commit 6610e0893b8b ("RTC: Rework RTC code to use timerqueue for events")
> Signed-off-by: Elad Nachman <enachman@marvell.com>
> ---
>  drivers/rtc/interface.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index 96626f8068f9..c32ef95a07d4 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -126,6 +126,7 @@ EXPORT_SYMBOL_GPL(rtc_read_time);
>  int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
>  {
>  	int err, uie;
> +	struct rtc_time new_tm;
>  
>  	err = rtc_valid_tm(tm);
>  	if (err != 0)
> @@ -159,6 +160,17 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
>  	else
>  		err = -EINVAL;
>  
> +	if (rtc && rtc->ops && rtc->ops->read_time) {
> +		if (!rtc->ops->read_time(rtc->dev.parent, &new_tm)) {
> +			pr_debug("new rtc time secs %d mins %d hours %d mday %d mon %d year %d way %d yday %d dst %d\n",
> +					new_tm.tm_sec, new_tm.tm_min,
> +					new_tm.tm_hour, new_tm.tm_mday,
> +					new_tm.tm_mon, new_tm.tm_year,
> +					new_tm.tm_wday, new_tm.tm_yday,
> +					new_tm.tm_isdst);
> +		}
> +	}
> +


This is unrelated to the patch.

>  	pm_stay_awake(rtc->dev.parent);
>  	mutex_unlock(&rtc->ops_lock);
>  	/* A timer might have just expired */
> @@ -999,7 +1011,7 @@ void rtc_timer_do_work(struct work_struct *work)
>  		trace_rtc_timer_fired(timer);
>  		/* Re-add/fwd periodic timers */
>  		if (ktime_to_ns(timer->period)) {
> -			timer->node.expires = ktime_add(timer->node.expires,
> +			timer->node.expires = ktime_add(now,
>  							timer->period);
>  			timer->enabled = 1;
>  			timerqueue_add(&rtc->timerqueue, &timer->node);
> -- 
> 2.25.1
> 

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [EXTERNAL] Re: [PATCH] drivers: rtc: handle OTF clock changes
  2026-06-24 15:31 ` Alexandre Belloni
@ 2026-06-28 17:34   ` Elad Nachman
  0 siblings, 0 replies; 4+ messages in thread
From: Elad Nachman @ 2026-06-28 17:34 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org

Hi Alexandre,

> 
> 
> From: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Sent: Wednesday, June 24, 2026 6:31 PM
> To: Elad Nachman <enachman@marvell.com>
> Cc: linux-rtc@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [EXTERNAL] Re: [PATCH] drivers: rtc: handle OTF clock changes
> 
> > From: Elad Nachman <mailto:enachman@marvell.com>
> >
> > When processing expired RTC events and rearming them, use now
> > instead of expiry to prevent endless loops.
> > Issue seen with Armada 385 SOC.
> 
> The loop is not endless, it may be long however. How do you reproduce

48 seconds are long enough to trigger RCU related watchdog which eventually causes NOS reboot...

> this? Or maybe the question is what is enabling PIE on your system?

Not only PIE but also UIE enablement leads to this behavior.
UIE is enabled by Chrony, and that is activated by SONIC NOS.
SONIC NOS was introduced a time modification test (forward/backward)
which triggers this issue, causing the following kernel soft lockup:

[  409.188746] rcu: blocking rcu_node structures (internal RCU debug):
[  436.242391] watchdog: BUG: soft lockup - CPU#1 stuck for 48s! [kworker/1:3:432]
[  436.250173] Kernel panic - not syncing: softlockup: hung tasks
[  436.256032] CPU: 1 PID: 432 Comm: kworker/1:3 Tainted: G           O L     6.1.0-29-2-armmp
[  436.272194] Workqueue: events rtc_timer_do_work
[  436.377383]  __irq_svc from _raw_spin_unlock_irqrestore+0x24/0x28
[  436.383510]  _raw_spin_unlock_irqrestore from __wake_up_common_lock+0x8c/0xc0
[  436.390685]  __wake_up_common_lock from __wake_up+0x20/0x28
[  436.396286]  __wake_up from rtc_handle_legacy_irq+0x58/0x6c
[  436.401890]  rtc_handle_legacy_irq from rtc_timer_do_work+0xe0/0x37c
[  436.408275]  rtc_timer_do_work from process_one_work+0x1f8/0x4b4
[  436.414317]  process_one_work from worker_thread+0x54/0x50c
[  436.419924]  worker_thread from kthread+0xd8/0xf4

> 
> Your patch breaks existing code because it will expect to get one event
> per elapsed period while your patch will cause events to be skipped.

I realize that, but the alternative of having soft lockup followed by reboot is worse in my opinion...

If you have any better idea, I am more than open to suggestions...

> 
> >
> > Fixes commit 6610e0893b8b ("RTC: Rework RTC code to use timerqueue for events")
> > Signed-off-by: Elad Nachman <mailto:enachman@marvell.com>
> > ---
> >  drivers/rtc/interface.c | 14 +++++++++++++-
> >  1 file changed, 13 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> > index 96626f8068f9..c32ef95a07d4 100644
> > --- a/drivers/rtc/interface.c
> > +++ b/drivers/rtc/interface.c
> > @@ -126,6 +126,7 @@ EXPORT_SYMBOL_GPL(rtc_read_time);
> >  int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
> >  {
> >  	int err, uie;
> > +	struct rtc_time new_tm;
> >
> >  	err = rtc_valid_tm(tm);
> >  	if (err != 0)
> > @@ -159,6 +160,17 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
> >  	else
> >  		err = -EINVAL;
> >
> > +	if (rtc && rtc->ops && rtc->ops->read_time) {
> > +		if (!rtc->ops->read_time(rtc->dev.parent, &new_tm)) {
> > +			pr_debug("new rtc time secs %d mins %d hours %d mday %d mon %d year %d way %d yday %d dst %d\n",
> > +					new_tm.tm_sec, new_tm.tm_min,
> > +					new_tm.tm_hour, new_tm.tm_mday,
> > +					new_tm.tm_mon, new_tm.tm_year,
> > +					new_tm.tm_wday, new_tm.tm_yday,
> > +					new_tm.tm_isdst);
> > +		}
> > +	}
> > +
> 
> 
> This is unrelated to the patch.

I can remove this easily once we find a solution to the main issue above...

> 
> >  	pm_stay_awake(rtc->dev.parent);
> >  	mutex_unlock(&rtc->ops_lock);
> >  	/* A timer might have just expired */
> > @@ -999,7 +1011,7 @@ void rtc_timer_do_work(struct work_struct *work)
> >  		trace_rtc_timer_fired(timer);
> >  		/* Re-add/fwd periodic timers */
> >  		if (ktime_to_ns(timer->period)) {
> > -			timer->node.expires = ktime_add(timer->node.expires,
> > +			timer->node.expires = ktime_add(now,
> >  							timer->period);
> >  			timer->enabled = 1;
> >  			timerqueue_add(&rtc->timerqueue, &timer->node);
> > --
> > 2.25.1
> >
> 
> --
> Alexandre Belloni, co-owner and COO, Bootlin
> Embedded Linux and Kernel engineering
> https://urldefense.proofpoint.com/v2/url?u=https-3A__bootlin.com&d=DwIBAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=eTeNTLEK5-TxXczjOcKPhANIFtlB9pP4lq9qhdlFrwQ&m=6PDPbjuCmjWGjRiUxkS5sPKd4lHU_NypV7pVhoFdBcLnksTSMiVrFYs7blEViGJR&s=IQM9cBpCUzoV4ONJnQs4yjzbjygO7zRYDW1jpJL3zWM&e=

Thanks,

Elad.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers: rtc: handle OTF clock changes
  2026-06-24 12:31 [PATCH] drivers: rtc: handle OTF clock changes Elad Nachman
  2026-06-24 15:31 ` Alexandre Belloni
@ 2026-08-07 13:05 ` Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-08-07 13:05 UTC (permalink / raw)
  To: oe-kbuild, Elad Nachman, alexandre.belloni, linux-rtc,
	linux-kernel
  Cc: lkp, oe-kbuild-all, enachman

Hi Elad,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Elad-Nachman/drivers-rtc-handle-OTF-clock-changes/20260806-231215
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
patch link:    https://lore.kernel.org/r/20260624123103.3523728-1-enachman%40marvell.com
patch subject: [PATCH] drivers: rtc: handle OTF clock changes
config: i386-randconfig-141 (https://download.01.org/0day-ci/archive/20260807/202608072059.hZ2oY5u0-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
smatch: v0.5.0-9187-g5189e3fb

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Closes: https://lore.kernel.org/r/202608072059.hZ2oY5u0-lkp@intel.com/

smatch warnings:
drivers/rtc/interface.c:163 rtc_set_time() warn: variable dereferenced before check 'rtc' (see line 135)

vim +/rtc +163 drivers/rtc/interface.c

ab6a2d70d18edc David Brownell    2007-05-08  126  int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
0c86edc0d49706 Alessandro Zummo  2006-03-27  127  {
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  128  	int err, uie;
37b82b16c5f104 Elad Nachman      2026-06-24  129  	struct rtc_time new_tm;
0c86edc0d49706 Alessandro Zummo  2006-03-27  130  
0c86edc0d49706 Alessandro Zummo  2006-03-27  131  	err = rtc_valid_tm(tm);
0c86edc0d49706 Alessandro Zummo  2006-03-27  132  	if (err != 0)
0c86edc0d49706 Alessandro Zummo  2006-03-27  133  		return err;
0c86edc0d49706 Alessandro Zummo  2006-03-27  134  
4c4e5df1f346f7 Baolin Wang       2018-01-08 @135  	err = rtc_valid_range(rtc, tm);
4c4e5df1f346f7 Baolin Wang       2018-01-08  136  	if (err)
4c4e5df1f346f7 Baolin Wang       2018-01-08  137  		return err;
71db049e7355f3 Alexandre Belloni 2018-02-17  138  
989515647e7832 Baolin Wang       2018-01-08  139  	rtc_subtract_offset(rtc, tm);
989515647e7832 Baolin Wang       2018-01-08  140  
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  141  #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  142  	uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  143  #else
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  144  	uie = rtc->uie_rtctimer.enabled;
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  145  #endif
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  146  	if (uie) {
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  147  		err = rtc_update_irq_enable(rtc, 0);
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  148  		if (err)
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  149  			return err;
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  150  	}
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  151  
0c86edc0d49706 Alessandro Zummo  2006-03-27  152  	err = mutex_lock_interruptible(&rtc->ops_lock);
0c86edc0d49706 Alessandro Zummo  2006-03-27  153  	if (err)
b68bb2632453a9 David Brownell    2008-07-29  154  		return err;
0c86edc0d49706 Alessandro Zummo  2006-03-27  155  
0c86edc0d49706 Alessandro Zummo  2006-03-27  156  	if (!rtc->ops)
0c86edc0d49706 Alessandro Zummo  2006-03-27  157  		err = -ENODEV;
bbccf83f6c4e1a Alessandro Zummo  2009-01-06  158  	else if (rtc->ops->set_time)
cd9662094edf41 David Brownell    2007-05-08  159  		err = rtc->ops->set_time(rtc->dev.parent, tm);
606cc43c720bde Alexandre Belloni 2019-03-20  160  	else
bbccf83f6c4e1a Alessandro Zummo  2009-01-06  161  		err = -EINVAL;
0c86edc0d49706 Alessandro Zummo  2006-03-27  162  
37b82b16c5f104 Elad Nachman      2026-06-24 @163  	if (rtc && rtc->ops && rtc->ops->read_time) {
                                                            ^^^
There is no point in checking "rtc" after we have already dereferenced
it.

37b82b16c5f104 Elad Nachman      2026-06-24  164  		if (!rtc->ops->read_time(rtc->dev.parent, &new_tm)) {
37b82b16c5f104 Elad Nachman      2026-06-24  165  			pr_debug("new rtc time secs %d mins %d hours %d mday %d mon %d year %d way %d yday %d dst %d\n",
37b82b16c5f104 Elad Nachman      2026-06-24  166  					new_tm.tm_sec, new_tm.tm_min,
37b82b16c5f104 Elad Nachman      2026-06-24  167  					new_tm.tm_hour, new_tm.tm_mday,
37b82b16c5f104 Elad Nachman      2026-06-24  168  					new_tm.tm_mon, new_tm.tm_year,
37b82b16c5f104 Elad Nachman      2026-06-24  169  					new_tm.tm_wday, new_tm.tm_yday,
37b82b16c5f104 Elad Nachman      2026-06-24  170  					new_tm.tm_isdst);
37b82b16c5f104 Elad Nachman      2026-06-24  171  		}
37b82b16c5f104 Elad Nachman      2026-06-24  172  	}
37b82b16c5f104 Elad Nachman      2026-06-24  173  
14d0e347ea2db5 Zoran Markovic    2013-06-26  174  	pm_stay_awake(rtc->dev.parent);
0c86edc0d49706 Alessandro Zummo  2006-03-27  175  	mutex_unlock(&rtc->ops_lock);
5f9679d29c7959 NeilBrown         2011-12-09  176  	/* A timer might have just expired */
5f9679d29c7959 NeilBrown         2011-12-09  177  	schedule_work(&rtc->irqwork);
29a1f599c0cc37 Baolin Wang       2017-12-14  178  
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  179  	if (uie) {
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  180  		err = rtc_update_irq_enable(rtc, 1);
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  181  		if (err)
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  182  			return err;
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  183  	}
7e7c005b4b1f1f Alexandre Belloni 2019-10-21  184  
29a1f599c0cc37 Baolin Wang       2017-12-14  185  	trace_rtc_set_time(rtc_tm_to_time64(tm), err);
0c86edc0d49706 Alessandro Zummo  2006-03-27  186  	return err;
0c86edc0d49706 Alessandro Zummo  2006-03-27  187  }

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-07 13:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-24 12:31 [PATCH] drivers: rtc: handle OTF clock changes Elad Nachman
2026-06-24 15:31 ` Alexandre Belloni
2026-06-28 17:34   ` [EXTERNAL] " Elad Nachman
2026-08-07 13:05 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox