cec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/gfp.h>
  4. #include <linux/kernel.h>
  5. #include <asm/mce.h>
  6. #include "debugfs.h"
  7. /*
  8. * RAS Correctable Errors Collector
  9. *
  10. * This is a simple gadget which collects correctable errors and counts their
  11. * occurrence per physical page address.
  12. *
  13. * We've opted for possibly the simplest data structure to collect those - an
  14. * array of the size of a memory page. It stores 512 u64's with the following
  15. * structure:
  16. *
  17. * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
  18. *
  19. * The generation in the two highest order bits is two bits which are set to 11b
  20. * on every insertion. During the course of each entry's existence, the
  21. * generation field gets decremented during spring cleaning to 10b, then 01b and
  22. * then 00b.
  23. *
  24. * This way we're employing the natural numeric ordering to make sure that newly
  25. * inserted/touched elements have higher 12-bit counts (which we've manufactured)
  26. * and thus iterating over the array initially won't kick out those elements
  27. * which were inserted last.
  28. *
  29. * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
  30. * elements entered into the array, during which, we're decaying all elements.
  31. * If, after decay, an element gets inserted again, its generation is set to 11b
  32. * to make sure it has higher numerical count than other, older elements and
  33. * thus emulate an an LRU-like behavior when deleting elements to free up space
  34. * in the page.
  35. *
  36. * When an element reaches it's max count of count_threshold, we try to poison
  37. * it by assuming that errors triggered count_threshold times in a single page
  38. * are excessive and that page shouldn't be used anymore. count_threshold is
  39. * initialized to COUNT_MASK which is the maximum.
  40. *
  41. * That error event entry causes cec_add_elem() to return !0 value and thus
  42. * signal to its callers to log the error.
  43. *
  44. * To the question why we've chosen a page and moving elements around with
  45. * memmove(), it is because it is a very simple structure to handle and max data
  46. * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
  47. * We wanted to avoid the pointer traversal of more complex structures like a
  48. * linked list or some sort of a balancing search tree.
  49. *
  50. * Deleting an element takes O(n) but since it is only a single page, it should
  51. * be fast enough and it shouldn't happen all too often depending on error
  52. * patterns.
  53. */
  54. #undef pr_fmt
  55. #define pr_fmt(fmt) "RAS: " fmt
  56. /*
  57. * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
  58. * elements have stayed in the array without having been accessed again.
  59. */
  60. #define DECAY_BITS 2
  61. #define DECAY_MASK ((1ULL << DECAY_BITS) - 1)
  62. #define MAX_ELEMS (PAGE_SIZE / sizeof(u64))
  63. /*
  64. * Threshold amount of inserted elements after which we start spring
  65. * cleaning.
  66. */
  67. #define CLEAN_ELEMS (MAX_ELEMS >> DECAY_BITS)
  68. /* Bits which count the number of errors happened in this 4K page. */
  69. #define COUNT_BITS (PAGE_SHIFT - DECAY_BITS)
  70. #define COUNT_MASK ((1ULL << COUNT_BITS) - 1)
  71. #define FULL_COUNT_MASK (PAGE_SIZE - 1)
  72. /*
  73. * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
  74. */
  75. #define PFN(e) ((e) >> PAGE_SHIFT)
  76. #define DECAY(e) (((e) >> COUNT_BITS) & DECAY_MASK)
  77. #define COUNT(e) ((unsigned int)(e) & COUNT_MASK)
  78. #define FULL_COUNT(e) ((e) & (PAGE_SIZE - 1))
  79. static struct ce_array {
  80. u64 *array; /* container page */
  81. unsigned int n; /* number of elements in the array */
  82. unsigned int decay_count; /*
  83. * number of element insertions/increments
  84. * since the last spring cleaning.
  85. */
  86. u64 pfns_poisoned; /*
  87. * number of PFNs which got poisoned.
  88. */
  89. u64 ces_entered; /*
  90. * The number of correctable errors
  91. * entered into the collector.
  92. */
  93. u64 decays_done; /*
  94. * Times we did spring cleaning.
  95. */
  96. union {
  97. struct {
  98. __u32 disabled : 1, /* cmdline disabled */
  99. __resv : 31;
  100. };
  101. __u32 flags;
  102. };
  103. } ce_arr;
  104. static DEFINE_MUTEX(ce_mutex);
  105. static u64 dfs_pfn;
  106. /* Amount of errors after which we offline */
  107. static unsigned int count_threshold = COUNT_MASK;
  108. /*
  109. * The timer "decays" element count each timer_interval which is 24hrs by
  110. * default.
  111. */
  112. #define CEC_TIMER_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
  113. #define CEC_TIMER_MIN_INTERVAL 1 * 60 * 60 /* 1h */
  114. #define CEC_TIMER_MAX_INTERVAL 30 * 24 * 60 * 60 /* one month */
  115. static struct timer_list cec_timer;
  116. static u64 timer_interval = CEC_TIMER_DEFAULT_INTERVAL;
  117. /*
  118. * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
  119. * element in the array. On insertion and any access, it gets reset to max.
  120. */
  121. static void do_spring_cleaning(struct ce_array *ca)
  122. {
  123. int i;
  124. for (i = 0; i < ca->n; i++) {
  125. u8 decay = DECAY(ca->array[i]);
  126. if (!decay)
  127. continue;
  128. decay--;
  129. ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
  130. ca->array[i] |= (decay << COUNT_BITS);
  131. }
  132. ca->decay_count = 0;
  133. ca->decays_done++;
  134. }
  135. /*
  136. * @interval in seconds
  137. */
  138. static void cec_mod_timer(struct timer_list *t, unsigned long interval)
  139. {
  140. unsigned long iv;
  141. iv = interval * HZ + jiffies;
  142. mod_timer(t, round_jiffies(iv));
  143. }
  144. static void cec_timer_fn(unsigned long data)
  145. {
  146. struct ce_array *ca = (struct ce_array *)data;
  147. do_spring_cleaning(ca);
  148. cec_mod_timer(&cec_timer, timer_interval);
  149. }
  150. /*
  151. * @to: index of the smallest element which is >= then @pfn.
  152. *
  153. * Return the index of the pfn if found, otherwise negative value.
  154. */
  155. static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
  156. {
  157. int min = 0, max = ca->n - 1;
  158. u64 this_pfn;
  159. while (min <= max) {
  160. int i = (min + max) >> 1;
  161. this_pfn = PFN(ca->array[i]);
  162. if (this_pfn < pfn)
  163. min = i + 1;
  164. else if (this_pfn > pfn)
  165. max = i - 1;
  166. else if (this_pfn == pfn) {
  167. if (to)
  168. *to = i;
  169. return i;
  170. }
  171. }
  172. /*
  173. * When the loop terminates without finding @pfn, min has the index of
  174. * the element slot where the new @pfn should be inserted. The loop
  175. * terminates when min > max, which means the min index points to the
  176. * bigger element while the max index to the smaller element, in-between
  177. * which the new @pfn belongs to.
  178. *
  179. * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
  180. */
  181. if (to)
  182. *to = min;
  183. return -ENOKEY;
  184. }
  185. static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
  186. {
  187. WARN_ON(!to);
  188. if (!ca->n) {
  189. *to = 0;
  190. return -ENOKEY;
  191. }
  192. return __find_elem(ca, pfn, to);
  193. }
  194. static void del_elem(struct ce_array *ca, int idx)
  195. {
  196. /* Save us a function call when deleting the last element. */
  197. if (ca->n - (idx + 1))
  198. memmove((void *)&ca->array[idx],
  199. (void *)&ca->array[idx + 1],
  200. (ca->n - (idx + 1)) * sizeof(u64));
  201. ca->n--;
  202. }
  203. static u64 del_lru_elem_unlocked(struct ce_array *ca)
  204. {
  205. unsigned int min = FULL_COUNT_MASK;
  206. int i, min_idx = 0;
  207. for (i = 0; i < ca->n; i++) {
  208. unsigned int this = FULL_COUNT(ca->array[i]);
  209. if (min > this) {
  210. min = this;
  211. min_idx = i;
  212. }
  213. }
  214. del_elem(ca, min_idx);
  215. return PFN(ca->array[min_idx]);
  216. }
  217. /*
  218. * We return the 0th pfn in the error case under the assumption that it cannot
  219. * be poisoned and excessive CEs in there are a serious deal anyway.
  220. */
  221. static u64 __maybe_unused del_lru_elem(void)
  222. {
  223. struct ce_array *ca = &ce_arr;
  224. u64 pfn;
  225. if (!ca->n)
  226. return 0;
  227. mutex_lock(&ce_mutex);
  228. pfn = del_lru_elem_unlocked(ca);
  229. mutex_unlock(&ce_mutex);
  230. return pfn;
  231. }
  232. int cec_add_elem(u64 pfn)
  233. {
  234. struct ce_array *ca = &ce_arr;
  235. unsigned int to;
  236. int count, ret = 0;
  237. /*
  238. * We can be called very early on the identify_cpu() path where we are
  239. * not initialized yet. We ignore the error for simplicity.
  240. */
  241. if (!ce_arr.array || ce_arr.disabled)
  242. return -ENODEV;
  243. ca->ces_entered++;
  244. mutex_lock(&ce_mutex);
  245. if (ca->n == MAX_ELEMS)
  246. WARN_ON(!del_lru_elem_unlocked(ca));
  247. ret = find_elem(ca, pfn, &to);
  248. if (ret < 0) {
  249. /*
  250. * Shift range [to-end] to make room for one more element.
  251. */
  252. memmove((void *)&ca->array[to + 1],
  253. (void *)&ca->array[to],
  254. (ca->n - to) * sizeof(u64));
  255. ca->array[to] = (pfn << PAGE_SHIFT) |
  256. (DECAY_MASK << COUNT_BITS) | 1;
  257. ca->n++;
  258. ret = 0;
  259. goto decay;
  260. }
  261. count = COUNT(ca->array[to]);
  262. if (count < count_threshold) {
  263. ca->array[to] |= (DECAY_MASK << COUNT_BITS);
  264. ca->array[to]++;
  265. ret = 0;
  266. } else {
  267. u64 pfn = ca->array[to] >> PAGE_SHIFT;
  268. if (!pfn_valid(pfn)) {
  269. pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
  270. } else {
  271. /* We have reached max count for this page, soft-offline it. */
  272. pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
  273. memory_failure_queue(pfn, 0, MF_SOFT_OFFLINE);
  274. ca->pfns_poisoned++;
  275. }
  276. del_elem(ca, to);
  277. /*
  278. * Return a >0 value to denote that we've reached the offlining
  279. * threshold.
  280. */
  281. ret = 1;
  282. goto unlock;
  283. }
  284. decay:
  285. ca->decay_count++;
  286. if (ca->decay_count >= CLEAN_ELEMS)
  287. do_spring_cleaning(ca);
  288. unlock:
  289. mutex_unlock(&ce_mutex);
  290. return ret;
  291. }
  292. static int u64_get(void *data, u64 *val)
  293. {
  294. *val = *(u64 *)data;
  295. return 0;
  296. }
  297. static int pfn_set(void *data, u64 val)
  298. {
  299. *(u64 *)data = val;
  300. cec_add_elem(val);
  301. return 0;
  302. }
  303. DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
  304. static int decay_interval_set(void *data, u64 val)
  305. {
  306. *(u64 *)data = val;
  307. if (val < CEC_TIMER_MIN_INTERVAL)
  308. return -EINVAL;
  309. if (val > CEC_TIMER_MAX_INTERVAL)
  310. return -EINVAL;
  311. timer_interval = val;
  312. cec_mod_timer(&cec_timer, timer_interval);
  313. return 0;
  314. }
  315. DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
  316. static int count_threshold_set(void *data, u64 val)
  317. {
  318. *(u64 *)data = val;
  319. if (val > COUNT_MASK)
  320. val = COUNT_MASK;
  321. count_threshold = val;
  322. return 0;
  323. }
  324. DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
  325. static int array_dump(struct seq_file *m, void *v)
  326. {
  327. struct ce_array *ca = &ce_arr;
  328. u64 prev = 0;
  329. int i;
  330. mutex_lock(&ce_mutex);
  331. seq_printf(m, "{ n: %d\n", ca->n);
  332. for (i = 0; i < ca->n; i++) {
  333. u64 this = PFN(ca->array[i]);
  334. seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
  335. WARN_ON(prev > this);
  336. prev = this;
  337. }
  338. seq_printf(m, "}\n");
  339. seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
  340. ca->ces_entered, ca->pfns_poisoned);
  341. seq_printf(m, "Flags: 0x%x\n", ca->flags);
  342. seq_printf(m, "Timer interval: %lld seconds\n", timer_interval);
  343. seq_printf(m, "Decays: %lld\n", ca->decays_done);
  344. seq_printf(m, "Action threshold: %d\n", count_threshold);
  345. mutex_unlock(&ce_mutex);
  346. return 0;
  347. }
  348. static int array_open(struct inode *inode, struct file *filp)
  349. {
  350. return single_open(filp, array_dump, NULL);
  351. }
  352. static const struct file_operations array_ops = {
  353. .owner = THIS_MODULE,
  354. .open = array_open,
  355. .read = seq_read,
  356. .llseek = seq_lseek,
  357. .release = single_release,
  358. };
  359. static int __init create_debugfs_nodes(void)
  360. {
  361. struct dentry *d, *pfn, *decay, *count, *array;
  362. d = debugfs_create_dir("cec", ras_debugfs_dir);
  363. if (!d) {
  364. pr_warn("Error creating cec debugfs node!\n");
  365. return -1;
  366. }
  367. pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
  368. if (!pfn) {
  369. pr_warn("Error creating pfn debugfs node!\n");
  370. goto err;
  371. }
  372. array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
  373. if (!array) {
  374. pr_warn("Error creating array debugfs node!\n");
  375. goto err;
  376. }
  377. decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
  378. &timer_interval, &decay_interval_ops);
  379. if (!decay) {
  380. pr_warn("Error creating decay_interval debugfs node!\n");
  381. goto err;
  382. }
  383. count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
  384. &count_threshold, &count_threshold_ops);
  385. if (!count) {
  386. pr_warn("Error creating count_threshold debugfs node!\n");
  387. goto err;
  388. }
  389. return 0;
  390. err:
  391. debugfs_remove_recursive(d);
  392. return 1;
  393. }
  394. void __init cec_init(void)
  395. {
  396. if (ce_arr.disabled)
  397. return;
  398. ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
  399. if (!ce_arr.array) {
  400. pr_err("Error allocating CE array page!\n");
  401. return;
  402. }
  403. if (create_debugfs_nodes())
  404. return;
  405. setup_timer(&cec_timer, cec_timer_fn, (unsigned long)&ce_arr);
  406. cec_mod_timer(&cec_timer, CEC_TIMER_DEFAULT_INTERVAL);
  407. pr_info("Correctable Errors collector initialized.\n");
  408. }
  409. int __init parse_cec_param(char *str)
  410. {
  411. if (!str)
  412. return 0;
  413. if (*str == '=')
  414. str++;
  415. if (!strcmp(str, "cec_disable"))
  416. ce_arr.disabled = 1;
  417. else
  418. return 0;
  419. return 1;
  420. }