cec.c 12 KB

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