trace.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/trace.c
  4. *
  5. * Copyright (C) 2006 Linus Torvalds
  6. *
  7. * Trace facility for suspend/resume problems, when none of the
  8. * devices may be working.
  9. */
  10. #define pr_fmt(fmt) "PM: " fmt
  11. #include <linux/pm-trace.h>
  12. #include <linux/export.h>
  13. #include <linux/rtc.h>
  14. #include <linux/suspend.h>
  15. #include <linux/init.h>
  16. #include <linux/mc146818rtc.h>
  17. #include "power.h"
  18. /*
  19. * Horrid, horrid, horrid.
  20. *
  21. * It turns out that the _only_ piece of hardware that actually
  22. * keeps its value across a hard boot (and, more importantly, the
  23. * POST init sequence) is literally the realtime clock.
  24. *
  25. * Never mind that an RTC chip has 114 bytes (and often a whole
  26. * other bank of an additional 128 bytes) of nice SRAM that is
  27. * _designed_ to keep data - the POST will clear it. So we literally
  28. * can just use the few bytes of actual time data, which means that
  29. * we're really limited.
  30. *
  31. * It means, for example, that we can't use the seconds at all
  32. * (since the time between the hang and the boot might be more
  33. * than a minute), and we'd better not depend on the low bits of
  34. * the minutes either.
  35. *
  36. * There are the wday fields etc, but I wouldn't guarantee those
  37. * are dependable either. And if the date isn't valid, either the
  38. * hw or POST will do strange things.
  39. *
  40. * So we're left with:
  41. * - year: 0-99
  42. * - month: 0-11
  43. * - day-of-month: 1-28
  44. * - hour: 0-23
  45. * - min: (0-30)*2
  46. *
  47. * Giving us a total range of 0-16128000 (0xf61800), ie less
  48. * than 24 bits of actual data we can save across reboots.
  49. *
  50. * And if your box can't boot in less than three minutes,
  51. * you're screwed.
  52. *
  53. * Now, almost 24 bits of data is pitifully small, so we need
  54. * to be pretty dense if we want to use it for anything nice.
  55. * What we do is that instead of saving off nice readable info,
  56. * we save off _hashes_ of information that we can hopefully
  57. * regenerate after the reboot.
  58. *
  59. * In particular, this means that we might be unlucky, and hit
  60. * a case where we have a hash collision, and we end up not
  61. * being able to tell for certain exactly which case happened.
  62. * But that's hopefully unlikely.
  63. *
  64. * What we do is to take the bits we can fit, and split them
  65. * into three parts (16*997*1009 = 16095568), and use the values
  66. * for:
  67. * - 0-15: user-settable
  68. * - 0-996: file + line number
  69. * - 0-1008: device
  70. */
  71. #define USERHASH (16)
  72. #define FILEHASH (997)
  73. #define DEVHASH (1009)
  74. #define DEVSEED (7919)
  75. bool pm_trace_rtc_abused __read_mostly;
  76. EXPORT_SYMBOL_GPL(pm_trace_rtc_abused);
  77. static unsigned int dev_hash_value;
  78. static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
  79. {
  80. unsigned int n = user + USERHASH*(file + FILEHASH*device);
  81. // June 7th, 2006
  82. static struct rtc_time time = {
  83. .tm_sec = 0,
  84. .tm_min = 0,
  85. .tm_hour = 0,
  86. .tm_mday = 7,
  87. .tm_mon = 5, // June - counting from zero
  88. .tm_year = 106,
  89. .tm_wday = 3,
  90. .tm_yday = 160,
  91. .tm_isdst = 1
  92. };
  93. time.tm_year = (n % 100);
  94. n /= 100;
  95. time.tm_mon = (n % 12);
  96. n /= 12;
  97. time.tm_mday = (n % 28) + 1;
  98. n /= 28;
  99. time.tm_hour = (n % 24);
  100. n /= 24;
  101. time.tm_min = (n % 20) * 3;
  102. n /= 20;
  103. mc146818_set_time(&time);
  104. pm_trace_rtc_abused = true;
  105. return n ? -1 : 0;
  106. }
  107. static unsigned int read_magic_time(void)
  108. {
  109. struct rtc_time time;
  110. unsigned int val;
  111. if (mc146818_get_time(&time) < 0) {
  112. pr_err("Unable to read current time from RTC\n");
  113. return 0;
  114. }
  115. pr_info("RTC time: %ptRt, date: %ptRd\n", &time, &time);
  116. val = time.tm_year; /* 100 years */
  117. if (val > 100)
  118. val -= 100;
  119. val += time.tm_mon * 100; /* 12 months */
  120. val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
  121. val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
  122. val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
  123. return val;
  124. }
  125. /*
  126. * This is just the sdbm hash function with a user-supplied
  127. * seed and final size parameter.
  128. */
  129. static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
  130. {
  131. unsigned char c;
  132. while ((c = *data++) != 0) {
  133. seed = (seed << 16) + (seed << 6) - seed + c;
  134. }
  135. return seed % mod;
  136. }
  137. void set_trace_device(struct device *dev)
  138. {
  139. dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
  140. }
  141. EXPORT_SYMBOL(set_trace_device);
  142. /*
  143. * We could just take the "tracedata" index into the .tracedata
  144. * section instead. Generating a hash of the data gives us a
  145. * chance to work across kernel versions, and perhaps more
  146. * importantly it also gives us valid/invalid check (ie we will
  147. * likely not give totally bogus reports - if the hash matches,
  148. * it's not any guarantee, but it's a high _likelihood_ that
  149. * the match is valid).
  150. */
  151. void generate_pm_trace(const void *tracedata, unsigned int user)
  152. {
  153. unsigned short lineno = *(unsigned short *)tracedata;
  154. const char *file = *(const char **)(tracedata + 2);
  155. unsigned int user_hash_value, file_hash_value;
  156. if (!x86_platform.legacy.rtc)
  157. return;
  158. user_hash_value = user % USERHASH;
  159. file_hash_value = hash_string(lineno, file, FILEHASH);
  160. set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
  161. }
  162. EXPORT_SYMBOL(generate_pm_trace);
  163. extern char __tracedata_start[], __tracedata_end[];
  164. static int show_file_hash(unsigned int value)
  165. {
  166. int match;
  167. char *tracedata;
  168. match = 0;
  169. for (tracedata = __tracedata_start ; tracedata < __tracedata_end ;
  170. tracedata += 2 + sizeof(unsigned long)) {
  171. unsigned short lineno = *(unsigned short *)tracedata;
  172. const char *file = *(const char **)(tracedata + 2);
  173. unsigned int hash = hash_string(lineno, file, FILEHASH);
  174. if (hash != value)
  175. continue;
  176. pr_info(" hash matches %s:%u\n", file, lineno);
  177. match++;
  178. }
  179. return match;
  180. }
  181. static int show_dev_hash(unsigned int value)
  182. {
  183. int match = 0;
  184. struct list_head *entry;
  185. device_pm_lock();
  186. entry = dpm_list.prev;
  187. while (entry != &dpm_list) {
  188. struct device * dev = to_device(entry);
  189. unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
  190. if (hash == value) {
  191. dev_info(dev, "hash matches\n");
  192. match++;
  193. }
  194. entry = entry->prev;
  195. }
  196. device_pm_unlock();
  197. return match;
  198. }
  199. static unsigned int hash_value_early_read;
  200. int show_trace_dev_match(char *buf, size_t size)
  201. {
  202. unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
  203. int ret = 0;
  204. struct list_head *entry;
  205. /*
  206. * It's possible that multiple devices will match the hash and we can't
  207. * tell which is the culprit, so it's best to output them all.
  208. */
  209. device_pm_lock();
  210. entry = dpm_list.prev;
  211. while (size && entry != &dpm_list) {
  212. struct device *dev = to_device(entry);
  213. unsigned int hash = hash_string(DEVSEED, dev_name(dev),
  214. DEVHASH);
  215. if (hash == value) {
  216. int len = snprintf(buf, size, "%s\n",
  217. dev_driver_string(dev));
  218. if (len > size)
  219. len = size;
  220. buf += len;
  221. ret += len;
  222. size -= len;
  223. }
  224. entry = entry->prev;
  225. }
  226. device_pm_unlock();
  227. return ret;
  228. }
  229. static int
  230. pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
  231. {
  232. switch (mode) {
  233. case PM_POST_HIBERNATION:
  234. case PM_POST_SUSPEND:
  235. if (pm_trace_rtc_abused) {
  236. pm_trace_rtc_abused = false;
  237. pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
  238. }
  239. break;
  240. default:
  241. break;
  242. }
  243. return 0;
  244. }
  245. static struct notifier_block pm_trace_nb = {
  246. .notifier_call = pm_trace_notify,
  247. };
  248. static int __init early_resume_init(void)
  249. {
  250. if (!x86_platform.legacy.rtc)
  251. return 0;
  252. hash_value_early_read = read_magic_time();
  253. register_pm_notifier(&pm_trace_nb);
  254. return 0;
  255. }
  256. static int __init late_resume_init(void)
  257. {
  258. unsigned int val = hash_value_early_read;
  259. unsigned int user, file, dev;
  260. if (!x86_platform.legacy.rtc)
  261. return 0;
  262. user = val % USERHASH;
  263. val = val / USERHASH;
  264. file = val % FILEHASH;
  265. val = val / FILEHASH;
  266. dev = val /* % DEVHASH */;
  267. pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
  268. show_file_hash(file);
  269. show_dev_hash(dev);
  270. return 0;
  271. }
  272. core_initcall(early_resume_init);
  273. late_initcall(late_resume_init);