Commit ba58d102 authored by Chen Yu's avatar Chen Yu Committed by Thomas Gleixner

timekeeping: Ignore the bogus sleep time if pm_trace is enabled

Power management suspend/resume tracing (ab)uses the RTC to store
suspend/resume information persistently. As a consequence the RTC value is
clobbered when timekeeping is resumed and tries to inject the sleep time.

Commit a4f8f666 ("timekeeping: Cap array access in timekeeping_debug")
plugged a out of bounds array access in the timekeeping debug code which
was caused by the clobbered RTC value, but we still use the clobbered RTC
value for sleep time injection into kernel timekeeping, which will result
in random adjustments depending on the stored "hash" value.

To prevent this keep track of the RTC clobbering and ignore the invalid RTC
timestamp at resume. If the system resumed successfully clear the flag,
which marks the RTC as unusable, warn the user about the RTC clobber and
recommend to adjust the RTC with 'ntpdate' or 'rdate'.

[jstultz: Fixed up pr_warn formating, and implemented suggestions from Ingo]
[ tglx: Rewrote changelog ]

Originally-from: Thomas Gleixner <>
Signed-off-by: default avatarChen Yu <>
Signed-off-by: default avatarJohn Stultz <>
Acked-by: Pavel Machek's avatarPavel Machek <>
Acked-by: default avatarThomas Gleixner <>
Cc: Prarit Bhargava <>
Cc: "Rafael J. Wysocki" <>
Cc: Richard Cochran <>
Cc: Xunlei Pang <>
Cc: Len Brown <>
Link: default avatarThomas Gleixner <>
parent a109ded2
......@@ -64,6 +64,15 @@ void mach_get_cmos_time(struct timespec *now)
unsigned int status, year, mon, day, hour, min, sec, century = 0;
unsigned long flags;
* If pm_trace abused the RTC as storage, set the timespec to 0,
* which tells the caller that this RTC value is unusable.
if (!pm_trace_rtc_valid()) {
now->tv_sec = now->tv_nsec = 0;
spin_lock_irqsave(&rtc_lock, flags);
......@@ -10,6 +10,7 @@
#include <linux/pm-trace.h>
#include <linux/export.h>
#include <linux/rtc.h>
#include <linux/suspend.h>
#include <linux/mc146818rtc.h>
......@@ -74,6 +75,9 @@
#define DEVSEED (7919)
bool pm_trace_rtc_abused __read_mostly;
static unsigned int dev_hash_value;
static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
......@@ -104,6 +108,7 @@ static int set_magic_time(unsigned int user, unsigned int file, unsigned int dev
time.tm_min = (n % 20) * 3;
n /= 20;
pm_trace_rtc_abused = true;
return n ? -1 : 0;
......@@ -239,9 +244,31 @@ int show_trace_dev_match(char *buf, size_t size)
return ret;
static int
pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
switch (mode) {
if (pm_trace_rtc_abused) {
pm_trace_rtc_abused = false;
pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
return 0;
static struct notifier_block pm_trace_nb = {
.notifier_call = pm_trace_notify,
static int early_resume_init(void)
hash_value_early_read = read_magic_time();
return 0;
......@@ -191,6 +191,13 @@ static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
static int cmos_read_time(struct device *dev, struct rtc_time *t)
* If pm_trace abused the RTC for storage, set the timespec to 0,
* which tells the caller that this RTC value is unusable.
if (!pm_trace_rtc_valid())
return -EIO;
/* REVISIT: if the clock has a "century" register, use
* that instead of the heuristic in mc146818_get_time().
* That'll make Y3K compatility (year > 2070) easy!
......@@ -16,6 +16,7 @@
#include <asm/mc146818rtc.h> /* register access macros */
#include <linux/bcd.h>
#include <linux/delay.h>
#include <linux/pm-trace.h>
#ifdef __KERNEL__
#include <linux/spinlock.h> /* spinlock_t */
#ifndef PM_TRACE_H
#define PM_TRACE_H
#include <linux/types.h>
#include <asm/pm-trace.h>
#include <linux/types.h>
extern int pm_trace_enabled;
extern bool pm_trace_rtc_abused;
static inline bool pm_trace_rtc_valid(void)
return !pm_trace_rtc_abused;
static inline int pm_trace_is_enabled(void)
......@@ -24,6 +30,7 @@ extern int show_trace_dev_match(char *buf, size_t size);
static inline bool pm_trace_rtc_valid(void) { return true; }
static inline int pm_trace_is_enabled(void) { return 0; }
#define TRACE_DEVICE(dev) do { } while (0)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment