History

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.

Pavel Tatashin committed 8 years ago

Commit 3eca9931 parent be2e0e42 files · +32 −30Browse files

Unified · Split

2 files
  1. include/linux/timekeeping.h +2 −1
  2. kernel/time/timekeeping.c +30 −29
include/linux/timekeeping.h+2−1
@@ -243,7 +243,8 @@ extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); · 242 unmodified lines
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+30−29
@@ -17,6 +17,7 @@ · 16 unmodified lines
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) · 1473 unmodified lines
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; · 7 unmodified lines
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) · 3 unmodified lines
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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t