2020-12-06 22:10:21

by Thomas Gleixner

[permalink] [raw]
Subject: [patch 4/8] rtc: core: Make the sync offset default more realistic

The offset which is used to steer the start of an RTC synchronization
update via rtc_set_ntp_time() is huge. The math behind this is:

tsched twrite(t2.tv_sec - 1) t2 (seconds increment)

twrite - tsched is the transport time for the write to hit the device.

t2 - twrite depends on the chip and is for most chips one second.

The rtc_set_ntp_time() calculation of tsched is:

tsched = t2 - 1sec - (t2 - twrite)

The default for the sync offset is 500ms which means that twrite - tsched
is 500ms assumed that t2 - twrite is one second.

This is 0.5 seconds off for RTCs which are directly accessible by IO writes
and probably for the majority of i2C/SPI based RTC off by an order of
magnitude. Set it to 10ms which should bring it closer to reality.

The default can be adjusted by drivers (rtc_cmos does so) and could be
adjusted further by a calibration method which is an orthogonal problem.

Signed-off-by: Thomas Gleixner <[email protected]>
---
drivers/rtc/class.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -201,7 +201,7 @@ static struct rtc_device *rtc_allocate_d
device_initialize(&rtc->dev);

/* Drivers can revise this default after allocating the device. */
- rtc->set_offset_nsec = NSEC_PER_SEC / 2;
+ rtc->set_offset_nsec = 10 * NSEC_PER_MSEC;

rtc->irq_freq = 1;
rtc->max_user_freq = 64;


2020-12-07 20:58:48

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [patch 4/8] rtc: core: Make the sync offset default more realistic

On Sun, Dec 06, 2020 at 10:46:17PM +0100, Thomas Gleixner wrote:
> The offset which is used to steer the start of an RTC synchronization
> update via rtc_set_ntp_time() is huge. The math behind this is:
>
> tsched twrite(t2.tv_sec - 1) t2 (seconds increment)
>
> twrite - tsched is the transport time for the write to hit the device.
>
> t2 - twrite depends on the chip and is for most chips one second.
>
> The rtc_set_ntp_time() calculation of tsched is:
>
> tsched = t2 - 1sec - (t2 - twrite)
>
> The default for the sync offset is 500ms which means that twrite - tsched
> is 500ms assumed that t2 - twrite is one second.
>
> This is 0.5 seconds off for RTCs which are directly accessible by IO writes
> and probably for the majority of i2C/SPI based RTC off by an order of
> magnitude. Set it to 10ms which should bring it closer to reality.
>
> The default can be adjusted by drivers (rtc_cmos does so) and could be
> adjusted further by a calibration method which is an orthogonal problem.
>
> Signed-off-by: Thomas Gleixner <[email protected]>
> drivers/rtc/class.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> +++ b/drivers/rtc/class.c
> @@ -201,7 +201,7 @@ static struct rtc_device *rtc_allocate_d
> device_initialize(&rtc->dev);
>
> /* Drivers can revise this default after allocating the device. */
> - rtc->set_offset_nsec = NSEC_PER_SEC / 2;
> + rtc->set_offset_nsec = 10 * NSEC_PER_MSEC;

So the old value is clearly wrong for CMOS, and I have a strong
feeling this was an error and it should have been -NSEC_PER_SEC/2

I have no idea if CMOS behavior or 0s behavior is more common in the
rtclib drivers, but it seems since nobody noticed the huge offset
mistake in 3 years it doesn't actually really matter.

Reviewed-by: Jason Gunthorpe <[email protected]>

Jason

2020-12-11 10:28:03

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [patch 4/8] rtc: core: Make the sync offset default more realistic

Hi,

On 06/12/2020 22:46:17+0100, Thomas Gleixner wrote:
> The offset which is used to steer the start of an RTC synchronization
> update via rtc_set_ntp_time() is huge. The math behind this is:
>
> tsched twrite(t2.tv_sec - 1) t2 (seconds increment)
>
> twrite - tsched is the transport time for the write to hit the device.
>
> t2 - twrite depends on the chip and is for most chips one second.
>
> The rtc_set_ntp_time() calculation of tsched is:
>
> tsched = t2 - 1sec - (t2 - twrite)
>
> The default for the sync offset is 500ms which means that twrite - tsched
> is 500ms assumed that t2 - twrite is one second.
>
> This is 0.5 seconds off for RTCs which are directly accessible by IO writes
> and probably for the majority of i2C/SPI based RTC off by an order of
> magnitude. Set it to 10ms which should bring it closer to reality.
>
> The default can be adjusted by drivers (rtc_cmos does so) and could be
> adjusted further by a calibration method which is an orthogonal problem.
>
> Signed-off-by: Thomas Gleixner <[email protected]>
> ---
> drivers/rtc/class.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> --- a/drivers/rtc/class.c
> +++ b/drivers/rtc/class.c
> @@ -201,7 +201,7 @@ static struct rtc_device *rtc_allocate_d
> device_initialize(&rtc->dev);
>
> /* Drivers can revise this default after allocating the device. */
> - rtc->set_offset_nsec = NSEC_PER_SEC / 2;
> + rtc->set_offset_nsec = 10 * NSEC_PER_MSEC;

I did retest, on a slow 100kHz i2c bus, with a fairly inconvenient RTC,
The maximum offset to set the RTC was 4845533ns so I'd say 10ms is too
large. Should we make that 5ms ?

Apart from that, on the series, you can add my
Acked-by: Alexandre Belloni <[email protected]>

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

2020-12-11 10:39:29

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [patch 4/8] rtc: core: Make the sync offset default more realistic

On 11/12/2020 01:23:57+0100, Thomas Gleixner wrote:
> Alexandre,
>
> On Fri, Dec 11 2020 at 00:59, Alexandre Belloni wrote:
> > On 06/12/2020 22:46:17+0100, Thomas Gleixner wrote:
> >> /* Drivers can revise this default after allocating the device. */
> >> - rtc->set_offset_nsec = NSEC_PER_SEC / 2;
> >> + rtc->set_offset_nsec = 10 * NSEC_PER_MSEC;
> >
> > I did retest, on a slow 100kHz i2c bus, with a fairly inconvenient RTC,
> > The maximum offset to set the RTC was 4845533ns so I'd say 10ms is too
> > large. Should we make that 5ms ?
>
> Sure. As I said I pulled the 10 out of thin air.
>
> > Apart from that, on the series, you can add my
> > Acked-by: Alexandre Belloni <[email protected]>
>
> I'll amend the s/10/5/ throughout the series while queueing the whole
> pile in the timers/core branch unless you want it to be handled
> differently.
>

That's fine for me.

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

Subject: [tip: timers/core] rtc: core: Make the sync offset default more realistic

The following commit has been merged into the timers/core branch of tip:

Commit-ID: 354c796b9270eb4780e59e3bdb83a3ae4930a832
Gitweb: https://git.kernel.org/tip/354c796b9270eb4780e59e3bdb83a3ae4930a832
Author: Thomas Gleixner <[email protected]>
AuthorDate: Sun, 06 Dec 2020 22:46:17 +01:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Fri, 11 Dec 2020 10:40:52 +01:00

rtc: core: Make the sync offset default more realistic

The offset which is used to steer the start of an RTC synchronization
update via rtc_set_ntp_time() is huge. The math behind this is:

tsched twrite(t2.tv_sec - 1) t2 (seconds increment)

twrite - tsched is the transport time for the write to hit the device.

t2 - twrite depends on the chip and is for most chips one second.

The rtc_set_ntp_time() calculation of tsched is:

tsched = t2 - 1sec - (t2 - twrite)

The default for the sync offset is 500ms which means that twrite - tsched
is 500ms assumed that t2 - twrite is one second.

This is 0.5 seconds off for RTCs which are directly accessible by IO writes
and probably for the majority of i2C/SPI based RTC off by an order of
magnitude. Set it to 5ms which should bring it closer to reality.

The default can be adjusted by drivers (rtc_cmos does so) and could be
adjusted further by a calibration method which is an orthogonal problem.

Signed-off-by: Thomas Gleixner <[email protected]>
Reviewed-by: Jason Gunthorpe <[email protected]>
Acked-by: Alexandre Belloni <[email protected]>
Link: https://lore.kernel.org/r/[email protected]

---
drivers/rtc/class.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 7c88d19..d795737 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -201,7 +201,7 @@ static struct rtc_device *rtc_allocate_device(void)
device_initialize(&rtc->dev);

/* Drivers can revise this default after allocating the device. */
- rtc->set_offset_nsec = NSEC_PER_SEC / 2;
+ rtc->set_offset_nsec = 5 * NSEC_PER_MSEC;

rtc->irq_freq = 1;
rtc->max_user_freq = 64;

2020-12-11 23:35:45

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [patch 4/8] rtc: core: Make the sync offset default more realistic

Alexandre,

On Fri, Dec 11 2020 at 00:59, Alexandre Belloni wrote:
> On 06/12/2020 22:46:17+0100, Thomas Gleixner wrote:
>> /* Drivers can revise this default after allocating the device. */
>> - rtc->set_offset_nsec = NSEC_PER_SEC / 2;
>> + rtc->set_offset_nsec = 10 * NSEC_PER_MSEC;
>
> I did retest, on a slow 100kHz i2c bus, with a fairly inconvenient RTC,
> The maximum offset to set the RTC was 4845533ns so I'd say 10ms is too
> large. Should we make that 5ms ?

Sure. As I said I pulled the 10 out of thin air.

> Apart from that, on the series, you can add my
> Acked-by: Alexandre Belloni <[email protected]>

I'll amend the s/10/5/ throughout the series while queueing the whole
pile in the timers/core branch unless you want it to be handled
differently.

Thanks,

tglx