1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/trace.c
5 * Copyright (C) 2006 Linus Torvalds
7 * Trace facility for suspend/resume problems, when none of the
8 * devices may be working.
10 #define pr_fmt(fmt) "PM: " fmt
12 #include <linux/pm-trace.h>
13 #include <linux/export.h>
14 #include <linux/rtc.h>
15 #include <linux/suspend.h>
16 #include <linux/init.h>
18 #include <linux/mc146818rtc.h>
23 * Horrid, horrid, horrid.
25 * It turns out that the _only_ piece of hardware that actually
26 * keeps its value across a hard boot (and, more importantly, the
27 * POST init sequence) is literally the realtime clock.
29 * Never mind that an RTC chip has 114 bytes (and often a whole
30 * other bank of an additional 128 bytes) of nice SRAM that is
31 * _designed_ to keep data - the POST will clear it. So we literally
32 * can just use the few bytes of actual time data, which means that
33 * we're really limited.
35 * It means, for example, that we can't use the seconds at all
36 * (since the time between the hang and the boot might be more
37 * than a minute), and we'd better not depend on the low bits of
40 * There are the wday fields etc, but I wouldn't guarantee those
41 * are dependable either. And if the date isn't valid, either the
42 * hw or POST will do strange things.
47 * - day-of-month: 1-28
51 * Giving us a total range of 0-16128000 (0xf61800), ie less
52 * than 24 bits of actual data we can save across reboots.
54 * And if your box can't boot in less than three minutes,
57 * Now, almost 24 bits of data is pitifully small, so we need
58 * to be pretty dense if we want to use it for anything nice.
59 * What we do is that instead of saving off nice readable info,
60 * we save off _hashes_ of information that we can hopefully
61 * regenerate after the reboot.
63 * In particular, this means that we might be unlucky, and hit
64 * a case where we have a hash collision, and we end up not
65 * being able to tell for certain exactly which case happened.
66 * But that's hopefully unlikely.
68 * What we do is to take the bits we can fit, and split them
69 * into three parts (16*997*1009 = 16095568), and use the values
71 * - 0-15: user-settable
72 * - 0-996: file + line number
76 #define FILEHASH (997)
77 #define DEVHASH (1009)
79 #define DEVSEED (7919)
81 bool pm_trace_rtc_abused __read_mostly;
82 EXPORT_SYMBOL_GPL(pm_trace_rtc_abused);
84 static unsigned int dev_hash_value;
86 static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
88 unsigned int n = user + USERHASH*(file + FILEHASH*device);
91 static struct rtc_time time = {
96 .tm_mon = 5, // June - counting from zero
103 time.tm_year = (n % 100);
105 time.tm_mon = (n % 12);
107 time.tm_mday = (n % 28) + 1;
109 time.tm_hour = (n % 24);
111 time.tm_min = (n % 20) * 3;
113 mc146818_set_time(&time);
114 pm_trace_rtc_abused = true;
118 static unsigned int read_magic_time(void)
120 struct rtc_time time;
123 if (mc146818_get_time(&time) < 0) {
124 pr_err("Unable to read current time from RTC\n");
128 pr_info("RTC time: %ptRt, date: %ptRd\n", &time, &time);
129 val = time.tm_year; /* 100 years */
132 val += time.tm_mon * 100; /* 12 months */
133 val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
134 val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
135 val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
140 * This is just the sdbm hash function with a user-supplied
141 * seed and final size parameter.
143 static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
146 while ((c = *data++) != 0) {
147 seed = (seed << 16) + (seed << 6) - seed + c;
152 void set_trace_device(struct device *dev)
154 dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
156 EXPORT_SYMBOL(set_trace_device);
159 * We could just take the "tracedata" index into the .tracedata
160 * section instead. Generating a hash of the data gives us a
161 * chance to work across kernel versions, and perhaps more
162 * importantly it also gives us valid/invalid check (ie we will
163 * likely not give totally bogus reports - if the hash matches,
164 * it's not any guarantee, but it's a high _likelihood_ that
165 * the match is valid).
167 void generate_pm_trace(const void *tracedata, unsigned int user)
169 unsigned short lineno = *(unsigned short *)tracedata;
170 const char *file = *(const char **)(tracedata + 2);
171 unsigned int user_hash_value, file_hash_value;
173 if (!x86_platform.legacy.rtc)
176 user_hash_value = user % USERHASH;
177 file_hash_value = hash_string(lineno, file, FILEHASH);
178 set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
180 EXPORT_SYMBOL(generate_pm_trace);
182 extern char __tracedata_start[], __tracedata_end[];
183 static int show_file_hash(unsigned int value)
189 for (tracedata = __tracedata_start ; tracedata < __tracedata_end ;
190 tracedata += 2 + sizeof(unsigned long)) {
191 unsigned short lineno = *(unsigned short *)tracedata;
192 const char *file = *(const char **)(tracedata + 2);
193 unsigned int hash = hash_string(lineno, file, FILEHASH);
196 pr_info(" hash matches %s:%u\n", file, lineno);
202 static int show_dev_hash(unsigned int value)
205 struct list_head *entry;
208 entry = dpm_list.prev;
209 while (entry != &dpm_list) {
210 struct device * dev = to_device(entry);
211 unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
213 dev_info(dev, "hash matches\n");
222 static unsigned int hash_value_early_read;
224 int show_trace_dev_match(char *buf, size_t size)
226 unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
228 struct list_head *entry;
231 * It's possible that multiple devices will match the hash and we can't
232 * tell which is the culprit, so it's best to output them all.
235 entry = dpm_list.prev;
236 while (size && entry != &dpm_list) {
237 struct device *dev = to_device(entry);
238 unsigned int hash = hash_string(DEVSEED, dev_name(dev),
241 int len = snprintf(buf, size, "%s\n",
242 dev_driver_string(dev));
256 pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
259 case PM_POST_HIBERNATION:
260 case PM_POST_SUSPEND:
261 if (pm_trace_rtc_abused) {
262 pm_trace_rtc_abused = false;
263 pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
272 static struct notifier_block pm_trace_nb = {
273 .notifier_call = pm_trace_notify,
276 static int __init early_resume_init(void)
278 if (!x86_platform.legacy.rtc)
281 hash_value_early_read = read_magic_time();
282 register_pm_notifier(&pm_trace_nb);
286 static int __init late_resume_init(void)
288 unsigned int val = hash_value_early_read;
289 unsigned int user, file, dev;
291 if (!x86_platform.legacy.rtc)
294 user = val % USERHASH;
295 val = val / USERHASH;
296 file = val % FILEHASH;
297 val = val / FILEHASH;
298 dev = val /* % DEVHASH */;
300 pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
301 show_file_hash(file);
306 core_initcall(early_resume_init);
307 late_initcall(late_resume_init);