From patchwork Mon Nov 28 22:35:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 84463 Delivered-To: patches@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1348526qgi; Mon, 28 Nov 2016 14:35:30 -0800 (PST) X-Received: by 10.99.5.21 with SMTP id 21mr44241954pgf.32.1480372529936; Mon, 28 Nov 2016 14:35:29 -0800 (PST) Return-Path: Received: from mail-pf0-x22d.google.com (mail-pf0-x22d.google.com. [2607:f8b0:400e:c00::22d]) by mx.google.com with ESMTPS id u75si54439434pgc.144.2016.11.28.14.35.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Nov 2016 14:35:29 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22d as permitted sender) client-ip=2607:f8b0:400e:c00::22d; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22d as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x22d.google.com with SMTP id 189so27181750pfz.3 for ; Mon, 28 Nov 2016 14:35:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=w/qHT2NJFS7W20jgBKEZw/WW03vlre1awrnvQd1vUIU=; b=U1UKoNdhADrpT4CscoNDT+joiPtrUIzbVYTk5UYKNXKbqjfGpmim6oVpRiaRq+a8KZ gq8pnXa92I+sD2F0aCqRi3pQDIJzr/HdrcP1i9Q0GIik7BV7UghHmq8V0xp2F4cjVg9/ qn0nXrUxymIxqnriby2urMIL1lMNxE8HAGRZA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=w/qHT2NJFS7W20jgBKEZw/WW03vlre1awrnvQd1vUIU=; b=CPVcofyqlJaO3LWVUO9og1/eWr5MJvOVdK8ZGgFphya9lYal7mQ8Mqnw6v9j3WS/Oy PU/124zirpGYyNYRhR0w9wov5B3hKIb11g/teGxGW8XyJ2h8nEL0xOERYny6pAsYLc4+ iwGlg9/pzi0safUYnfzwSs610wOvJaEWL04GCa6lKGjHm6DVBMqs5VyWgBSdZWBVrLKs fT1TKHaUfCSEfC0KWzLfNUmPR4+mjMltmkVOcj4TRR+Y+aeOjX3/yseGL2jYFf1xN9NM hZP+PIksBFLr9hA0l9FP+BfBLhLMTUcg9CX71JmUfmESoXW6Mju96xh7KP7nzEKsxOXv tmyw== X-Gm-Message-State: AKaTC03Z8x6V7mOcPX22finHpq5hLI7j86lJr8lmq4mfmHBtEHzr8M8smC9r+CbjOmmb62Z5/h8= X-Received: by 10.84.214.15 with SMTP id h15mr54663280pli.135.1480372529590; Mon, 28 Nov 2016 14:35:29 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id r74sm89537352pfl.79.2016.11.28.14.35.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Nov 2016 14:35:28 -0800 (PST) From: John Stultz To: lkml Cc: Chen Yu , "Rafael J. Wysocki" , John Stultz , Xunlei Pang , Ingo Molnar , Len Brown , "H. Peter Anvin" , Pavel Machek , Thomas Gleixner , Prarit Bhargava , Richard Cochran Subject: [PATCH 2/7] timekeeping: Ignore the bogus sleep time if pm_trace is enabled Date: Mon, 28 Nov 2016 14:35:19 -0800 Message-Id: <1480372524-15181-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1480372524-15181-1-git-send-email-john.stultz@linaro.org> References: <1480372524-15181-1-git-send-email-john.stultz@linaro.org> From: Chen Yu Previously we encountered some memory overflow issues due to the bogus sleep time brought by inconsistent rtc, which is triggered when pm_trace is enabled, and we have fixed it in recent kernel. However it's improper in the first place to call __timekeeping_inject_sleeptime() in case that pm_trace is enabled simply because that "hash" time value will wreckage the timekeeping subsystem. This patch is originally written by Thomas, which would bypass the bogus rtc interval when pm_trace is enabled. Meanwhile, if system succeed to resume back with pm_trace set, the users are warned to adjust the bogus rtc either by 'ntpdate' or 'rdate', by resetting pm_trace_rtc_abused to false, otherwise above tools might not work as expected. Originally-from: Thomas Gleixner Cc: "Rafael J. Wysocki" Cc: John Stultz Cc: Xunlei Pang Cc: Ingo Molnar Cc: Len Brown Cc: "H. Peter Anvin" Cc: Pavel Machek Cc: Thomas Gleixner Cc: Prarit Bhargava Cc: Richard Cochran Acked-by: Pavel Machek Acked-by: Thomas Gleixner Signed-off-by: Chen Yu [jstultz: Fixed up pr_warn formating, and implemented suggestions from Ingo] Signed-off-by: John Stultz --- v2: Added suggestions from Ingo --- arch/x86/kernel/rtc.c | 9 +++++++++ drivers/base/power/trace.c | 26 ++++++++++++++++++++++++++ drivers/rtc/rtc-cmos.c | 7 +++++++ include/linux/mc146818rtc.h | 1 + include/linux/pm-trace.h | 9 ++++++++- 5 files changed, 51 insertions(+), 1 deletion(-) -- 2.7.4 diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c index 79c6311c..3282a88 100644 --- a/arch/x86/kernel/rtc.c +++ b/arch/x86/kernel/rtc.c @@ -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 bogus. + */ + if (!pm_trace_rtc_valid()) { + now->tv_sec = now->tv_nsec = 0; + return; + } + spin_lock_irqsave(&rtc_lock, flags); /* diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c index efec10b..15563c4 100644 --- a/drivers/base/power/trace.c +++ b/drivers/base/power/trace.c @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -74,6 +75,9 @@ #define DEVSEED (7919) +bool pm_trace_rtc_abused __read_mostly; +EXPORT_SYMBOL(pm_trace_rtc_abused); + 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; mc146818_set_time(&time); + pm_trace_rtc_abused = true; return n ? -1 : 0; } @@ -238,10 +243,31 @@ int show_trace_dev_match(char *buf, size_t size) device_pm_unlock(); return ret; } +static int pm_trace_notify(struct notifier_block *nb, + unsigned long mode, void *_unused) +{ + switch (mode) { + case PM_POST_HIBERNATION: + case PM_POST_SUSPEND: + 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.\n"); + } + break; + default: + break; + } + 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(); + register_pm_notifier(&pm_trace_nb); return 0; } diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c index dd3d598..9cf06b7 100644 --- a/drivers/rtc/rtc-cmos.c +++ b/drivers/rtc/rtc-cmos.c @@ -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 tell the caller that it 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! diff --git a/include/linux/mc146818rtc.h b/include/linux/mc146818rtc.h index a585b4b..0661af1 100644 --- a/include/linux/mc146818rtc.h +++ b/include/linux/mc146818rtc.h @@ -16,6 +16,7 @@ #include /* register access macros */ #include #include +#include #ifdef __KERNEL__ #include /* spinlock_t */ diff --git a/include/linux/pm-trace.h b/include/linux/pm-trace.h index ecbde7a..7b78793 100644 --- a/include/linux/pm-trace.h +++ b/include/linux/pm-trace.h @@ -1,11 +1,17 @@ #ifndef PM_TRACE_H #define PM_TRACE_H +#include #ifdef CONFIG_PM_TRACE #include -#include 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); #else +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)