Linux-RTC Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] rtc: interface:: 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
@ 2020-08-01 11:20 Grant Feng
  2020-08-01 13:28 ` Alexandre Belloni
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Feng @ 2020-08-01 11:20 UTC (permalink / raw)
  To: von81, a.zummo, alexandre.belloni, linux-rtc

1969-12-31T23:59:59 is an error more clear than Invalid argument

For example, when the RTC clock is not set, it will print a kernel
error log every time someone tries to read the clock:
        ~ # hwclock -r
        hwclock: RTC_RD_TIME: Invalid argument

It's clear and easy to understand what happened if print
1969-12-31T23:59:59 in this situation:
        ~ # hwclock -r
        Wed Dec 31 23:59:59 1969  0.000000 seconds

Signed-off-by: Grant Feng <von81@163.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 794a4f036b99..e6b3f4163565 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -101,8 +101,20 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
 		rtc_add_offset(rtc, tm);
 
 		err = rtc_valid_tm(tm);
-		if (err < 0)
+		if (err < 0) {
 			dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
+
+			tm->tm_sec = 59;
+			tm->tm_min = 59;
+			tm->tm_hour = 23;
+			tm->tm_mday = 31;
+			tm->tm_mon = 11;
+			tm->tm_year = 69;
+			tm->tm_wday = 3;
+			tm->tm_yday = 365;
+			tm->tm_isdst = 0;
+		}
+		err = 0;
 	}
 	return err;
 }
-- 
2.17.1



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

* Re: [PATCH] rtc: interface:: 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
  2020-08-01 11:20 [PATCH] rtc: interface:: 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time Grant Feng
@ 2020-08-01 13:28 ` Alexandre Belloni
  2020-08-02  6:51   ` [PATCH] rtc: interface^ " Grant Feng
  0 siblings, 1 reply; 6+ messages in thread
From: Alexandre Belloni @ 2020-08-01 13:28 UTC (permalink / raw)
  To: Grant Feng; +Cc: a.zummo, linux-rtc

Hi,

On 01/08/2020 19:20:07+0800, Grant Feng wrote:
> 1969-12-31T23:59:59 is an error more clear than Invalid argument

Definitively not, 1969-12-31T23:59:59 is a valid date and should not be
returned when it is known the current date is not set in the RTC.

> 
> For example, when the RTC clock is not set, it will print a kernel
> error log every time someone tries to read the clock:
>         ~ # hwclock -r
>         hwclock: RTC_RD_TIME: Invalid argument
> 
> It's clear and easy to understand what happened if print
> 1969-12-31T23:59:59 in this situation:
>         ~ # hwclock -r
>         Wed Dec 31 23:59:59 1969  0.000000 seconds
> 

How do you know this is an error an not what is actually set on the RTC?

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

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

* Re: [PATCH] rtc: interface^ 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
  2020-08-01 13:28 ` Alexandre Belloni
@ 2020-08-02  6:51   ` Grant Feng
  2020-08-02 13:04     ` Alexandre Belloni
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Feng @ 2020-08-02  6:51 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: a.zummo, linux-rtc

Hi,


On 2020-08-01 21:28, Alexandre Belloni wrote:
> On 01/08/2020 19:20:07+0800, Grant Feng wrote:
>> 1969-12-31T23:59:59 is an error more clear than Invalid argument
> Definitively not, 1969-12-31T23:59:59 is a valid date and should not be
> returned when it is known the current date is not set in the RTC.
'rtc_valid_tm' is used to check rtc_time and 1969-12-31T23:59:59 is invalid.
when the RTC clock is not set, some rtc devices always return '0' or almost
random data, and different rtc devices may give different return data.
so, I think, it's usful to return a default date when the current date is
not set in the RTC.
>> For example, when the RTC clock is not set, it will print a kernel
>> error log every time someone tries to read the clock:
>>          ~ # hwclock -r
>>          hwclock: RTC_RD_TIME: Invalid argument
>>
>> It's clear and easy to understand what happened if print
>> 1969-12-31T23:59:59 in this situation:
>>          ~ # hwclock -r
>>          Wed Dec 31 23:59:59 1969  0.000000 seconds
>>
> How do you know this is an error an not what is actually set on the RTC?
'rtc_valid_tm' will check rtc_time when someone set the RTC, the time
should not be earlier than 1970-1-1T00:00:00. so 1969-12-31T23:59:59
can not be actually set on the RTC.
     When someone get
~ # hwclock -r
Wed Dec 31 23:59:59 1969  0.000000 seconds
     he knows: the RTC time doesn't match my watch, change it now.
but still lots of people don't know what happened if they see
~ # hwclock -r
hwclock: RTC_RD_TIME: Invalid argument


Grant Feng
Embedded Linux and Kernel engineering
RTCHIP Information Technology (Shanghai) Co., Ltd.



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

* Re: [PATCH] rtc: interface^ 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
  2020-08-02  6:51   ` [PATCH] rtc: interface^ " Grant Feng
@ 2020-08-02 13:04     ` Alexandre Belloni
  2020-08-02 13:35       ` Grant Feng
  2020-08-03  3:07       ` Grant Feng
  0 siblings, 2 replies; 6+ messages in thread
From: Alexandre Belloni @ 2020-08-02 13:04 UTC (permalink / raw)
  To: Grant Feng; +Cc: a.zummo, linux-rtc

On 02/08/2020 14:51:41+0800, Grant Feng wrote:
> On 2020-08-01 21:28, Alexandre Belloni wrote:
> > On 01/08/2020 19:20:07+0800, Grant Feng wrote:
> > > 1969-12-31T23:59:59 is an error more clear than Invalid argument
> > Definitively not, 1969-12-31T23:59:59 is a valid date and should not be
> > returned when it is known the current date is not set in the RTC.
> 'rtc_valid_tm' is used to check rtc_time and 1969-12-31T23:59:59 is invalid.
> when the RTC clock is not set, some rtc devices always return '0' or almost
> random data, and different rtc devices may give different return data.
> so, I think, it's usful to return a default date when the current date is
> not set in the RTC.

You are not solving the issue you mention here. If the RTC doesn't know
whether the date/time is invalid and the core think it is valid, then
your code will not run.

> > > For example, when the RTC clock is not set, it will print a kernel
> > > error log every time someone tries to read the clock:
> > >          ~ # hwclock -r
> > >          hwclock: RTC_RD_TIME: Invalid argument
> > > 
> > > It's clear and easy to understand what happened if print
> > > 1969-12-31T23:59:59 in this situation:
> > >          ~ # hwclock -r
> > >          Wed Dec 31 23:59:59 1969  0.000000 seconds
> > > 
> > How do you know this is an error an not what is actually set on the RTC?
> 'rtc_valid_tm' will check rtc_time when someone set the RTC, the time
> should not be earlier than 1970-1-1T00:00:00. so 1969-12-31T23:59:59
> can not be actually set on the RTC.
>     When someone get
> ~ # hwclock -r
> Wed Dec 31 23:59:59 1969  0.000000 seconds
>     he knows: the RTC time doesn't match my watch, change it now.
> but still lots of people don't know what happened if they see
> ~ # hwclock -r
> hwclock: RTC_RD_TIME: Invalid argument
> 

This makes userspace checking for errors way worse. Think about it, first
userspace will need to check for an error when calling the ioctl then it
will have to check the time and consider a vlid date invalid. Seriously,
if hwclock doesn't do what you want, you can either patch it or use
another tool.

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

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

* Re: [PATCH] rtc: interface^ 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
  2020-08-02 13:04     ` Alexandre Belloni
@ 2020-08-02 13:35       ` Grant Feng
  2020-08-03  3:07       ` Grant Feng
  1 sibling, 0 replies; 6+ messages in thread
From: Grant Feng @ 2020-08-02 13:35 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: a.zummo, linux-rtc

Thank you for explaining that.


On 2020-08-02 21:04, Alexandre Belloni wrote:
> On 02/08/2020 14:51:41+0800, Grant Feng wrote:
>> On 2020-08-01 21:28, Alexandre Belloni wrote:
>>> On 01/08/2020 19:20:07+0800, Grant Feng wrote:
>>>> 1969-12-31T23:59:59 is an error more clear than Invalid argument
>>> Definitively not, 1969-12-31T23:59:59 is a valid date and should not be
>>> returned when it is known the current date is not set in the RTC.
>> 'rtc_valid_tm' is used to check rtc_time and 1969-12-31T23:59:59 is invalid.
>> when the RTC clock is not set, some rtc devices always return '0' or almost
>> random data, and different rtc devices may give different return data.
>> so, I think, it's usful to return a default date when the current date is
>> not set in the RTC.
> You are not solving the issue you mention here. If the RTC doesn't know
> whether the date/time is invalid and the core think it is valid, then
> your code will not run.
>
>>>> For example, when the RTC clock is not set, it will print a kernel
>>>> error log every time someone tries to read the clock:
>>>>           ~ # hwclock -r
>>>>           hwclock: RTC_RD_TIME: Invalid argument
>>>>
>>>> It's clear and easy to understand what happened if print
>>>> 1969-12-31T23:59:59 in this situation:
>>>>           ~ # hwclock -r
>>>>           Wed Dec 31 23:59:59 1969  0.000000 seconds
>>>>
>>> How do you know this is an error an not what is actually set on the RTC?
>> 'rtc_valid_tm' will check rtc_time when someone set the RTC, the time
>> should not be earlier than 1970-1-1T00:00:00. so 1969-12-31T23:59:59
>> can not be actually set on the RTC.
>>      When someone get
>> ~ # hwclock -r
>> Wed Dec 31 23:59:59 1969  0.000000 seconds
>>      he knows: the RTC time doesn't match my watch, change it now.
>> but still lots of people don't know what happened if they see
>> ~ # hwclock -r
>> hwclock: RTC_RD_TIME: Invalid argument
>>
> This makes userspace checking for errors way worse. Think about it, first
> userspace will need to check for an error when calling the ioctl then it
> will have to check the time and consider a vlid date invalid. Seriously,
> if hwclock doesn't do what you want, you can either patch it or use
> another tool.
>


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

* Re: [PATCH] rtc: interface^ 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time
  2020-08-02 13:04     ` Alexandre Belloni
  2020-08-02 13:35       ` Grant Feng
@ 2020-08-03  3:07       ` Grant Feng
  1 sibling, 0 replies; 6+ messages in thread
From: Grant Feng @ 2020-08-03  3:07 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: a.zummo, linux-rtc

Thank you again for your patience to explain.
I get the log and now I get it.

commit 812318a094d0715194d9f686b22ee67e7dc59d93
Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
Date:   Wed Feb 21 11:44:26 2018 +0100

     rtc: cmos: let the core handle invalid time

     Setting the rtc to a valid time when the time is invalid is a bad 
practice,
     because then userspace doesn't know it shouldn't trust the RTC.

     Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

then you removed the following code fragment in "drivers/rtc/rtc-cmos.c":

-       cmos_read_time(&pdev->dev, &time);
-       ret = rtc_valid_tm(&time);
-       if (ret) {
-               struct rtc_time def_time = {
-                       .tm_year = 1,
-                       .tm_mday = 1,
-               };
-               cmos_set_time(&pdev->dev, &def_time);
-       }


On 2020-08-02 21:04, Alexandre Belloni wrote:
> On 02/08/2020 14:51:41+0800, Grant Feng wrote:
>> On 2020-08-01 21:28, Alexandre Belloni wrote:
>>> On 01/08/2020 19:20:07+0800, Grant Feng wrote:
>>>> 1969-12-31T23:59:59 is an error more clear than Invalid argument
>>> Definitively not, 1969-12-31T23:59:59 is a valid date and should not be
>>> returned when it is known the current date is not set in the RTC.
>> 'rtc_valid_tm' is used to check rtc_time and 1969-12-31T23:59:59 is invalid.
>> when the RTC clock is not set, some rtc devices always return '0' or almost
>> random data, and different rtc devices may give different return data.
>> so, I think, it's usful to return a default date when the current date is
>> not set in the RTC.
> You are not solving the issue you mention here. If the RTC doesn't know
> whether the date/time is invalid and the core think it is valid, then
> your code will not run.
>
>>>> For example, when the RTC clock is not set, it will print a kernel
>>>> error log every time someone tries to read the clock:
>>>>           ~ # hwclock -r
>>>>           hwclock: RTC_RD_TIME: Invalid argument
>>>>
>>>> It's clear and easy to understand what happened if print
>>>> 1969-12-31T23:59:59 in this situation:
>>>>           ~ # hwclock -r
>>>>           Wed Dec 31 23:59:59 1969  0.000000 seconds
>>>>
>>> How do you know this is an error an not what is actually set on the RTC?
>> 'rtc_valid_tm' will check rtc_time when someone set the RTC, the time
>> should not be earlier than 1970-1-1T00:00:00. so 1969-12-31T23:59:59
>> can not be actually set on the RTC.
>>      When someone get
>> ~ # hwclock -r
>> Wed Dec 31 23:59:59 1969  0.000000 seconds
>>      he knows: the RTC time doesn't match my watch, change it now.
>> but still lots of people don't know what happened if they see
>> ~ # hwclock -r
>> hwclock: RTC_RD_TIME: Invalid argument
>>
> This makes userspace checking for errors way worse. Think about it, first
> userspace will need to check for an error when calling the ioctl then it
> will have to check the time and consider a vlid date invalid. Seriously,
> if hwclock doesn't do what you want, you can either patch it or use
> another tool.
>


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

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-01 11:20 [PATCH] rtc: interface:: 1969-12-31T23:59:59 is set as rtc_time if rtc_time is invalid in __rtc_read_time Grant Feng
2020-08-01 13:28 ` Alexandre Belloni
2020-08-02  6:51   ` [PATCH] rtc: interface^ " Grant Feng
2020-08-02 13:04     ` Alexandre Belloni
2020-08-02 13:35       ` Grant Feng
2020-08-03  3:07       ` Grant Feng

Linux-RTC Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-rtc/0 linux-rtc/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-rtc linux-rtc/ https://lore.kernel.org/linux-rtc \
		linux-rtc@vger.kernel.org
	public-inbox-index linux-rtc

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-rtc


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git