trace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. mc146818_get_time(&time);
  112. pr_info("RTC time: %ptRt, date: %ptRd\n", &time, &time);
  113. val = time.tm_year; /* 100 years */
  114. if (val > 100)
  115. val -= 100;
  116. val += time.tm_mon * 100; /* 12 months */
  117. val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
  118. val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
  119. val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
  120. return val;
  121. }
  122. /*
  123. * This is just the sdbm hash function with a user-supplied
  124. * seed and final size parameter.
  125. */
  126. static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
  127. {
  128. unsigned char c;
  129. while ((c = *data++) != 0) {
  130. seed = (seed << 16) + (seed << 6) - seed + c;
  131. }
  132. return seed % mod;
  133. }
  134. void set_trace_device(struct device *dev)
  135. {
  136. dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
  137. }
  138. EXPORT_SYMBOL(set_trace_device);
  139. /*
  140. * We could just take the "tracedata" index into the .tracedata
  141. * section instead. Generating a hash of the data gives us a
  142. * chance to work across kernel versions, and perhaps more
  143. * importantly it also gives us valid/invalid check (ie we will
  144. * likely not give totally bogus reports - if the hash matches,
  145. * it's not any guarantee, but it's a high _likelihood_ that
  146. * the match is valid).
  147. */
  148. void generate_pm_trace(const void *tracedata, unsigned int user)
  149. {
  150. unsigned short lineno = *(unsigned short *)tracedata;
  151. const char *file = *(const char **)(tracedata + 2);
  152. unsigned int user_hash_value, file_hash_value;
  153. if (!x86_platform.legacy.rtc)
  154. return;
  155. user_hash_value = user % USERHASH;
  156. file_hash_value = hash_string(lineno, file, FILEHASH);
  157. set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
  158. }
  159. EXPORT_SYMBOL(generate_pm_trace);
  160. extern char __tracedata_start[], __tracedata_end[];
  161. static int show_file_hash(unsigned int value)
  162. {
  163. int match;
  164. char *tracedata;
  165. match = 0;
  166. for (tracedata = __tracedata_start ; tracedata < __tracedata_end ;
  167. tracedata += 2 + sizeof(unsigned long)) {
  168. unsigned short lineno = *(unsigned short *)tracedata;
  169. const char *file = *(const char **)(tracedata + 2);
  170. unsigned int hash = hash_string(lineno, file, FILEHASH);
  171. if (hash != value)
  172. continue;
  173. pr_info(" hash matches %s:%u\n", file, lineno);
  174. match++;
  175. }
  176. return match;
  177. }
  178. static int show_dev_hash(unsigned int value)
  179. {
  180. int match = 0;
  181. struct list_head *entry;
  182. device_pm_lock();
  183. entry = dpm_list.prev;
  184. while (entry != &dpm_list) {
  185. struct device * dev = to_device(entry);
  186. unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
  187. if (hash == value) {
  188. dev_info(dev, "hash matches\n");
  189. match++;
  190. }
  191. entry = entry->prev;
  192. }
  193. device_pm_unlock();
  194. return match;
  195. }
  196. static unsigned int hash_value_early_read;
  197. int show_trace_dev_match(char *buf, size_t size)
  198. {
  199. unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
  200. int ret = 0;
  201. struct list_head *entry;
  202. /*
  203. * It's possible that multiple devices will match the hash and we can't
  204. * tell which is the culprit, so it's best to output them all.
  205. */
  206. device_pm_lock();
  207. entry = dpm_list.prev;
  208. while (size && entry != &dpm_list) {
  209. struct device *dev = to_device(entry);
  210. unsigned int hash = hash_string(DEVSEED, dev_name(dev),
  211. DEVHASH);
  212. if (hash == value) {
  213. int len = snprintf(buf, size, "%s\n",
  214. dev_driver_string(dev));
  215. if (len > size)
  216. len = size;
  217. buf += len;
  218. ret += len;
  219. size -= len;
  220. }
  221. entry = entry->prev;
  222. }
  223. device_pm_unlock();
  224. return ret;
  225. }
  226. static int
  227. pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
  228. {
  229. switch (mode) {
  230. case PM_POST_HIBERNATION:
  231. case PM_POST_SUSPEND:
  232. if (pm_trace_rtc_abused) {
  233. pm_trace_rtc_abused = false;
  234. pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. return 0;
  241. }
  242. static struct notifier_block pm_trace_nb = {
  243. .notifier_call = pm_trace_notify,
  244. };
  245. static int __init early_resume_init(void)
  246. {
  247. if (!x86_platform.legacy.rtc)
  248. return 0;
  249. hash_value_early_read = read_magic_time();
  250. register_pm_notifier(&pm_trace_nb);
  251. return 0;
  252. }
  253. static int __init late_resume_init(void)
  254. {
  255. unsigned int val = hash_value_early_read;
  256. unsigned int user, file, dev;
  257. if (!x86_platform.legacy.rtc)
  258. return 0;
  259. user = val % USERHASH;
  260. val = val / USERHASH;
  261. file = val % FILEHASH;
  262. val = val / FILEHASH;
  263. dev = val /* % DEVHASH */;
  264. pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
  265. show_file_hash(file);
  266. show_dev_hash(dev);
  267. return 0;
  268. }
  269. core_initcall(early_resume_init);
  270. late_initcall(late_resume_init);