Skip to content

Commit 3eca993

Browse files
Pavel TatashinKAGA-KOKO
authored andcommitted
timekeeping: Replace read_boot_clock64() with read_persistent_wall_and_boot_offset()
If architecture does not support exact boot time, it is challenging to estimate boot time without having a reference to the current persistent clock value. Yet, it cannot read the persistent clock time again, because this may lead to math discrepancies with the caller of read_boot_clock64() who have read the persistent clock at a different time. This is why it is better to provide two values simultaneously: the persistent clock value, and the boot time. Replace read_boot_clock64() with: read_persistent_wall_and_boot_offset(wall_time, boot_offset) Where wall_time is returned by read_persistent_clock() And boot_offset is wall_time - boot time, which defaults to 0. Signed-off-by: Pavel Tatashin <pasha.tatashin@oracle.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: steven.sistare@oracle.com Cc: daniel.m.jordan@oracle.com Cc: linux@armlinux.org.uk Cc: schwidefsky@de.ibm.com Cc: heiko.carstens@de.ibm.com Cc: john.stultz@linaro.org Cc: sboyd@codeaurora.org Cc: hpa@zytor.com Cc: douly.fnst@cn.fujitsu.com Cc: peterz@infradead.org Cc: prarit@redhat.com Cc: feng.tang@intel.com Cc: pmladek@suse.com Cc: gnomes@lxorguk.ukuu.org.uk Cc: linux-s390@vger.kernel.org Cc: boris.ostrovsky@oracle.com Cc: jgross@suse.com Cc: pbonzini@redhat.com Link: https://lkml.kernel.org/r/20180719205545.16512-16-pasha.tatashin@oracle.com
1 parent be2e0e4 commit 3eca993

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

include/linux/timekeeping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
243243
extern int persistent_clock_is_local;
244244

245245
extern void read_persistent_clock64(struct timespec64 *ts);
246-
extern void read_boot_clock64(struct timespec64 *ts);
246+
void read_persistent_clock_and_boot_offset(struct timespec64 *wall_clock,
247+
struct timespec64 *boot_offset);
247248
extern int update_persistent_clock64(struct timespec64 now);
248249

249250
/*

kernel/time/timekeeping.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/nmi.h>
1818
#include <linux/sched.h>
1919
#include <linux/sched/loadavg.h>
20+
#include <linux/sched/clock.h>
2021
#include <linux/syscore_ops.h>
2122
#include <linux/clocksource.h>
2223
#include <linux/jiffies.h>
@@ -1496,18 +1497,20 @@ void __weak read_persistent_clock64(struct timespec64 *ts64)
14961497
}
14971498

14981499
/**
1499-
* read_boot_clock64 - Return time of the system start.
1500+
* read_persistent_wall_and_boot_offset - Read persistent clock, and also offset
1501+
* from the boot.
15001502
*
15011503
* Weak dummy function for arches that do not yet support it.
1502-
* Function to read the exact time the system has been started.
1503-
* Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
1504-
*
1505-
* XXX - Do be sure to remove it once all arches implement it.
1504+
* wall_time - current time as returned by persistent clock
1505+
* boot_offset - offset that is defined as wall_time - boot_time
1506+
* default to 0.
15061507
*/
1507-
void __weak read_boot_clock64(struct timespec64 *ts)
1508+
void __weak __init
1509+
read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
1510+
struct timespec64 *boot_offset)
15081511
{
1509-
ts->tv_sec = 0;
1510-
ts->tv_nsec = 0;
1512+
read_persistent_clock64(wall_time);
1513+
*boot_offset = (struct timespec64){0};
15111514
}
15121515

15131516
/* Flag for if timekeeping_resume() has injected sleeptime */
@@ -1521,28 +1524,29 @@ static bool persistent_clock_exists;
15211524
*/
15221525
void __init timekeeping_init(void)
15231526
{
1527+
struct timespec64 wall_time, boot_offset, wall_to_mono;
15241528
struct timekeeper *tk = &tk_core.timekeeper;
15251529
struct clocksource *clock;
15261530
unsigned long flags;
1527-
struct timespec64 now, boot, tmp;
1528-
1529-
read_persistent_clock64(&now);
1530-
if (!timespec64_valid_strict(&now)) {
1531-
pr_warn("WARNING: Persistent clock returned invalid value!\n"
1532-
" Check your CMOS/BIOS settings.\n");
1533-
now.tv_sec = 0;
1534-
now.tv_nsec = 0;
1535-
} else if (now.tv_sec || now.tv_nsec)
1536-
persistent_clock_exists = true;
15371531

1538-
read_boot_clock64(&boot);
1539-
if (!timespec64_valid_strict(&boot)) {
1540-
pr_warn("WARNING: Boot clock returned invalid value!\n"
1541-
" Check your CMOS/BIOS settings.\n");
1542-
boot.tv_sec = 0;
1543-
boot.tv_nsec = 0;
1532+
read_persistent_wall_and_boot_offset(&wall_time, &boot_offset);
1533+
if (timespec64_valid_strict(&wall_time) &&
1534+
timespec64_to_ns(&wall_time) > 0) {
1535+
persistent_clock_exists = true;
1536+
} else {
1537+
pr_warn("Persistent clock returned invalid value");
1538+
wall_time = (struct timespec64){0};
15441539
}
15451540

1541+
if (timespec64_compare(&wall_time, &boot_offset) < 0)
1542+
boot_offset = (struct timespec64){0};
1543+
1544+
/*
1545+
* We want set wall_to_mono, so the following is true:
1546+
* wall time + wall_to_mono = boot time
1547+
*/
1548+
wall_to_mono = timespec64_sub(boot_offset, wall_time);
1549+
15461550
raw_spin_lock_irqsave(&timekeeper_lock, flags);
15471551
write_seqcount_begin(&tk_core.seq);
15481552
ntp_init();
@@ -1552,13 +1556,10 @@ void __init timekeeping_init(void)
15521556
clock->enable(clock);
15531557
tk_setup_internals(tk, clock);
15541558

1555-
tk_set_xtime(tk, &now);
1559+
tk_set_xtime(tk, &wall_time);
15561560
tk->raw_sec = 0;
1557-
if (boot.tv_sec == 0 && boot.tv_nsec == 0)
1558-
boot = tk_xtime(tk);
15591561

1560-
set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
1561-
tk_set_wall_to_mono(tk, tmp);
1562+
tk_set_wall_to_mono(tk, wall_to_mono);
15621563

15631564
timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
15641565

0 commit comments

Comments
 (0)