vintozver
Posts: 39
Joined: Thu Jun 01, 2017 2:16 am
Location: Bellevue, WA, USA

Raspbian hw clock reset after reboot

Sun Jul 16, 2017 2:47 am

Hi,

I noticed very weird behavior with Raspbian Stretch (9.0).
Preface
I decided to "upgrade" the PI and added Witty Pi hat and Power Relay hats.
I removed fake-hwclock package to make sure that OS will use the real RTC.
I expected system time to be set from RTC, then NTP sync some time, then sync RTC with system time (in UTC).
Please, let's take any conversations like "you need to set up witty pi properly" aside. I know what I'm doing.
Issue: RTC is set to "2016-11-03 17:00:00" every time after reboot.
How found
I suspected systemd, ntp and other processed which are running from root. I disabled all of them to make sure RTC is untouched.
Problem persisted.
Then, I compiled the kernel with audit support. Installed auditd and set hooks on /dev/rtc & /dev/rtc0. So far, nothing.
I dug deeper. I modified the kernel module to log the set time attempt:

Code: Select all

diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index b92044cf..50903b99 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -419,6 +419,17 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
        int             tmp;
        u8              *buf = ds1307->regs;

+       char current_task_comm[TASK_COMM_LEN];
+       struct task_struct *current_task = NULL;
+
+       memset(current_task_comm, 0, TASK_COMM_LEN);
+       current_task = get_current();
+       if (current_task != NULL)
+       {
+               get_task_comm(current_task_comm, current_task);
+       }
+       printk(KERN_WARNING "RTC write by process %s\n", current_task_comm);
+
        dev_dbg(dev, "%s secs=%d, mins=%d, "
                "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
                "write", t->tm_sec, t->tm_min,
Results:

[email protected]:~ $ dmesg | grep RTC
[ 11.196021] RTC write by process kworker/1:1
[ 710.241864] RTC write by process kworker/1:1

So, kernel is setting the RTC by itself, isn't it?

Is somebody having the same issue? Should we fix it? Or any ideas?

alphanumeric
Posts: 2473
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Raspbian hw clock reset after reboot

Sun Jul 16, 2017 8:40 pm

Battery OK? inserted with the correct polarity? I'd probably replace it anyway.

vintozver
Posts: 39
Joined: Thu Jun 01, 2017 2:16 am
Location: Bellevue, WA, USA

Re: Raspbian hw clock reset after reboot

Mon Jul 17, 2017 11:34 pm

alphanumeric wrote:Battery OK? inserted with the correct polarity? I'd probably replace it anyway.
:) come on :) there's only way to insert it. I checked with the multi meter (and made sure that the chip receives 3.3V) before even digging into details.

So, I found a "violator". It's kernel ...
CONFIG_RTC_SYSTOHC=y for RPI kernel. Also, device "rtc0" is set to write to.
https://git.kernel.org/pub/scm/linux/ke ... time/ntp.c
This way, kernel will write the time to RTC when user space reports time as synched.
Proof: timedatectl will show ntp synched: yes right after reboot.

Mitigation: I rebuilt the kernel with CONFIG_RTC_SYSTOHC=n.
Solution: find out why the user space reports time as synched even if it's not.

Good idea is to disable hw clock writes by the kernel and let user space do this job (by using hwclock.sh or similar way, like systemd-timedated).

alphanumeric
Posts: 2473
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Raspbian hw clock reset after reboot

Mon Jul 17, 2017 11:47 pm

On the DS1307, and two DS3231 breakout boards I have, the battery can easily be inserted upside down, that's why I asked.

hollymcr
Posts: 15
Joined: Thu Feb 11, 2016 7:15 pm

Re: Raspbian hw clock reset after reboot

Tue Apr 03, 2018 4:57 pm

vintozver wrote:
Mon Jul 17, 2017 11:34 pm
This way, kernel will write the time to RTC when user space reports time as synched.
Proof: timedatectl will show ntp synched: yes right after reboot.
Did you ever find a good solution to this?

I am working with a board that has an onboard RTC but which doesn't present itself to the Pi as such (I have to send serial command to read/write the RTC). I was hoping to determine whether NTP time sync had been successful, and write the current timestamp to the RTC if successful, or read the old one back if unsuccessful.

Like you I note that timedatectl reports sync:yes after a reboot with no network access, and with fake-hwclock disabled I'm left with a date of 2016-11-03. (Aside: Where is the 2016-11-03 timestamp coming from? At this point, just knowing that "anything before 2017 means sync failed" would be a start but without knowing where that timestamp comes from I don't want to write code that's going to fail on a newer Pi.)

vintozver
Posts: 39
Joined: Thu Jun 01, 2017 2:16 am
Location: Bellevue, WA, USA

Re: Raspbian hw clock reset after reboot

Mon Jun 11, 2018 1:18 am

The "solution" is to rebuild the kernel with the CONFIG_RTC_SYSTOHC=n (see my earlier post)
I will need to dig deeper into timedatectl at this moment cause my interest (and needs) are coming back and striking me to the back ...

vintozver
Posts: 39
Joined: Thu Jun 01, 2017 2:16 am
Location: Bellevue, WA, USA

Re: Raspbian hw clock reset after reboot

Mon Jun 11, 2018 1:42 am

The freshest idea and solution (which works well for me) is to remove ntp daemon (apt-get purge ntp) so the NTP daemon won't confuse systemd. I have just tested it - works fine.

pi[email protected]:~ $ timedatectl
Local time: Sun 2018-06-10 18:41:01 PDT
Universal time: Mon 2018-06-11 01:41:01 UTC
RTC time: Mon 2018-06-11 01:41:01
Time zone: America/Los_Angeles (PDT, -0700)
Network time on: yes
NTP synchronized: no
RTC in local TZ: no

Return to “Advanced users”