blob: f57e869dfea2833772212097b7aceb255fa33321 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Borislav Petkov011d8262017-03-27 11:33:02 +02002#include <linux/mm.h>
3#include <linux/gfp.h>
4#include <linux/kernel.h>
Cong Wang0ade0b62019-04-16 14:33:51 -07005#include <linux/workqueue.h>
Borislav Petkov011d8262017-03-27 11:33:02 +02006
7#include <asm/mce.h>
8
9#include "debugfs.h"
10
11/*
12 * RAS Correctable Errors Collector
13 *
14 * This is a simple gadget which collects correctable errors and counts their
15 * occurrence per physical page address.
16 *
17 * We've opted for possibly the simplest data structure to collect those - an
18 * array of the size of a memory page. It stores 512 u64's with the following
19 * structure:
20 *
21 * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
22 *
23 * The generation in the two highest order bits is two bits which are set to 11b
24 * on every insertion. During the course of each entry's existence, the
25 * generation field gets decremented during spring cleaning to 10b, then 01b and
26 * then 00b.
27 *
28 * This way we're employing the natural numeric ordering to make sure that newly
29 * inserted/touched elements have higher 12-bit counts (which we've manufactured)
30 * and thus iterating over the array initially won't kick out those elements
31 * which were inserted last.
32 *
33 * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
34 * elements entered into the array, during which, we're decaying all elements.
35 * If, after decay, an element gets inserted again, its generation is set to 11b
36 * to make sure it has higher numerical count than other, older elements and
37 * thus emulate an an LRU-like behavior when deleting elements to free up space
38 * in the page.
39 *
40 * When an element reaches it's max count of count_threshold, we try to poison
41 * it by assuming that errors triggered count_threshold times in a single page
42 * are excessive and that page shouldn't be used anymore. count_threshold is
43 * initialized to COUNT_MASK which is the maximum.
44 *
45 * That error event entry causes cec_add_elem() to return !0 value and thus
46 * signal to its callers to log the error.
47 *
48 * To the question why we've chosen a page and moving elements around with
49 * memmove(), it is because it is a very simple structure to handle and max data
50 * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
51 * We wanted to avoid the pointer traversal of more complex structures like a
52 * linked list or some sort of a balancing search tree.
53 *
54 * Deleting an element takes O(n) but since it is only a single page, it should
55 * be fast enough and it shouldn't happen all too often depending on error
56 * patterns.
57 */
58
59#undef pr_fmt
60#define pr_fmt(fmt) "RAS: " fmt
61
62/*
63 * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
64 * elements have stayed in the array without having been accessed again.
65 */
66#define DECAY_BITS 2
67#define DECAY_MASK ((1ULL << DECAY_BITS) - 1)
68#define MAX_ELEMS (PAGE_SIZE / sizeof(u64))
69
70/*
71 * Threshold amount of inserted elements after which we start spring
72 * cleaning.
73 */
74#define CLEAN_ELEMS (MAX_ELEMS >> DECAY_BITS)
75
76/* Bits which count the number of errors happened in this 4K page. */
77#define COUNT_BITS (PAGE_SHIFT - DECAY_BITS)
78#define COUNT_MASK ((1ULL << COUNT_BITS) - 1)
79#define FULL_COUNT_MASK (PAGE_SIZE - 1)
80
81/*
82 * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
83 */
84
85#define PFN(e) ((e) >> PAGE_SHIFT)
86#define DECAY(e) (((e) >> COUNT_BITS) & DECAY_MASK)
87#define COUNT(e) ((unsigned int)(e) & COUNT_MASK)
88#define FULL_COUNT(e) ((e) & (PAGE_SIZE - 1))
89
90static struct ce_array {
91 u64 *array; /* container page */
92 unsigned int n; /* number of elements in the array */
93
94 unsigned int decay_count; /*
95 * number of element insertions/increments
96 * since the last spring cleaning.
97 */
98
99 u64 pfns_poisoned; /*
100 * number of PFNs which got poisoned.
101 */
102
103 u64 ces_entered; /*
104 * The number of correctable errors
105 * entered into the collector.
106 */
107
108 u64 decays_done; /*
109 * Times we did spring cleaning.
110 */
111
112 union {
113 struct {
114 __u32 disabled : 1, /* cmdline disabled */
115 __resv : 31;
116 };
117 __u32 flags;
118 };
119} ce_arr;
120
121static DEFINE_MUTEX(ce_mutex);
122static u64 dfs_pfn;
123
124/* Amount of errors after which we offline */
125static unsigned int count_threshold = COUNT_MASK;
126
Cong Wang0ade0b62019-04-16 14:33:51 -0700127/* Each element "decays" each decay_interval which is 24hrs by default. */
128#define CEC_DECAY_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
129#define CEC_DECAY_MIN_INTERVAL 1 * 60 * 60 /* 1h */
130#define CEC_DECAY_MAX_INTERVAL 30 * 24 * 60 * 60 /* one month */
131static struct delayed_work cec_work;
132static u64 decay_interval = CEC_DECAY_DEFAULT_INTERVAL;
Borislav Petkov011d8262017-03-27 11:33:02 +0200133
134/*
135 * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
136 * element in the array. On insertion and any access, it gets reset to max.
137 */
138static void do_spring_cleaning(struct ce_array *ca)
139{
140 int i;
141
142 for (i = 0; i < ca->n; i++) {
143 u8 decay = DECAY(ca->array[i]);
144
145 if (!decay)
146 continue;
147
148 decay--;
149
150 ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
151 ca->array[i] |= (decay << COUNT_BITS);
152 }
153 ca->decay_count = 0;
154 ca->decays_done++;
155}
156
157/*
158 * @interval in seconds
159 */
Cong Wang0ade0b62019-04-16 14:33:51 -0700160static void cec_mod_work(unsigned long interval)
Borislav Petkov011d8262017-03-27 11:33:02 +0200161{
162 unsigned long iv;
163
Cong Wang0ade0b62019-04-16 14:33:51 -0700164 iv = interval * HZ;
165 mod_delayed_work(system_wq, &cec_work, round_jiffies(iv));
Borislav Petkov011d8262017-03-27 11:33:02 +0200166}
167
Cong Wang0ade0b62019-04-16 14:33:51 -0700168static void cec_work_fn(struct work_struct *work)
Borislav Petkov011d8262017-03-27 11:33:02 +0200169{
Cong Wang0ade0b62019-04-16 14:33:51 -0700170 mutex_lock(&ce_mutex);
Kees Cook254db5b2017-10-21 08:37:11 -0700171 do_spring_cleaning(&ce_arr);
Cong Wang0ade0b62019-04-16 14:33:51 -0700172 mutex_unlock(&ce_mutex);
Borislav Petkov011d8262017-03-27 11:33:02 +0200173
Cong Wang0ade0b62019-04-16 14:33:51 -0700174 cec_mod_work(decay_interval);
Borislav Petkov011d8262017-03-27 11:33:02 +0200175}
176
177/*
178 * @to: index of the smallest element which is >= then @pfn.
179 *
180 * Return the index of the pfn if found, otherwise negative value.
181 */
182static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
183{
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200184 int min = 0, max = ca->n - 1;
Borislav Petkov011d8262017-03-27 11:33:02 +0200185 u64 this_pfn;
Borislav Petkov011d8262017-03-27 11:33:02 +0200186
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200187 while (min <= max) {
188 int i = (min + max) >> 1;
Borislav Petkov011d8262017-03-27 11:33:02 +0200189
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200190 this_pfn = PFN(ca->array[i]);
Borislav Petkov011d8262017-03-27 11:33:02 +0200191
192 if (this_pfn < pfn)
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200193 min = i + 1;
Borislav Petkov011d8262017-03-27 11:33:02 +0200194 else if (this_pfn > pfn)
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200195 max = i - 1;
196 else if (this_pfn == pfn) {
197 if (to)
198 *to = i;
199
200 return i;
Borislav Petkov011d8262017-03-27 11:33:02 +0200201 }
202 }
203
Borislav Petkovf3c74b32019-04-20 13:27:51 +0200204 /*
205 * When the loop terminates without finding @pfn, min has the index of
206 * the element slot where the new @pfn should be inserted. The loop
207 * terminates when min > max, which means the min index points to the
208 * bigger element while the max index to the smaller element, in-between
209 * which the new @pfn belongs to.
210 *
211 * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
212 */
Borislav Petkov011d8262017-03-27 11:33:02 +0200213 if (to)
214 *to = min;
215
Borislav Petkov011d8262017-03-27 11:33:02 +0200216 return -ENOKEY;
217}
218
219static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
220{
221 WARN_ON(!to);
222
223 if (!ca->n) {
224 *to = 0;
225 return -ENOKEY;
226 }
227 return __find_elem(ca, pfn, to);
228}
229
230static void del_elem(struct ce_array *ca, int idx)
231{
232 /* Save us a function call when deleting the last element. */
233 if (ca->n - (idx + 1))
234 memmove((void *)&ca->array[idx],
235 (void *)&ca->array[idx + 1],
236 (ca->n - (idx + 1)) * sizeof(u64));
237
238 ca->n--;
239}
240
241static u64 del_lru_elem_unlocked(struct ce_array *ca)
242{
243 unsigned int min = FULL_COUNT_MASK;
244 int i, min_idx = 0;
245
246 for (i = 0; i < ca->n; i++) {
247 unsigned int this = FULL_COUNT(ca->array[i]);
248
249 if (min > this) {
250 min = this;
251 min_idx = i;
252 }
253 }
254
255 del_elem(ca, min_idx);
256
257 return PFN(ca->array[min_idx]);
258}
259
260/*
261 * We return the 0th pfn in the error case under the assumption that it cannot
262 * be poisoned and excessive CEs in there are a serious deal anyway.
263 */
264static u64 __maybe_unused del_lru_elem(void)
265{
266 struct ce_array *ca = &ce_arr;
267 u64 pfn;
268
269 if (!ca->n)
270 return 0;
271
272 mutex_lock(&ce_mutex);
273 pfn = del_lru_elem_unlocked(ca);
274 mutex_unlock(&ce_mutex);
275
276 return pfn;
277}
278
279
280int cec_add_elem(u64 pfn)
281{
282 struct ce_array *ca = &ce_arr;
283 unsigned int to;
284 int count, ret = 0;
285
286 /*
287 * We can be called very early on the identify_cpu() path where we are
288 * not initialized yet. We ignore the error for simplicity.
289 */
290 if (!ce_arr.array || ce_arr.disabled)
291 return -ENODEV;
292
Borislav Petkov011d8262017-03-27 11:33:02 +0200293 mutex_lock(&ce_mutex);
294
WANG Chao09cbd212019-04-18 03:41:14 +0000295 ca->ces_entered++;
296
Borislav Petkovde0e0622019-04-20 14:06:37 +0200297 /* Array full, free the LRU slot. */
Borislav Petkov011d8262017-03-27 11:33:02 +0200298 if (ca->n == MAX_ELEMS)
299 WARN_ON(!del_lru_elem_unlocked(ca));
300
301 ret = find_elem(ca, pfn, &to);
302 if (ret < 0) {
303 /*
304 * Shift range [to-end] to make room for one more element.
305 */
306 memmove((void *)&ca->array[to + 1],
307 (void *)&ca->array[to],
308 (ca->n - to) * sizeof(u64));
309
Borislav Petkovde0e0622019-04-20 14:06:37 +0200310 ca->array[to] = pfn << PAGE_SHIFT;
Borislav Petkov011d8262017-03-27 11:33:02 +0200311 ca->n++;
Borislav Petkov011d8262017-03-27 11:33:02 +0200312 }
313
Borislav Petkovde0e0622019-04-20 14:06:37 +0200314 /* Add/refresh element generation and increment count */
315 ca->array[to] |= DECAY_MASK << COUNT_BITS;
316 ca->array[to]++;
317
318 /* Check action threshold and soft-offline, if reached. */
Borislav Petkov011d8262017-03-27 11:33:02 +0200319 count = COUNT(ca->array[to]);
Borislav Petkovde0e0622019-04-20 14:06:37 +0200320 if (count >= count_threshold) {
Borislav Petkov011d8262017-03-27 11:33:02 +0200321 u64 pfn = ca->array[to] >> PAGE_SHIFT;
322
323 if (!pfn_valid(pfn)) {
324 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
325 } else {
326 /* We have reached max count for this page, soft-offline it. */
327 pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
Eric W. Biederman83b57532017-07-09 18:14:01 -0500328 memory_failure_queue(pfn, MF_SOFT_OFFLINE);
Borislav Petkov011d8262017-03-27 11:33:02 +0200329 ca->pfns_poisoned++;
330 }
331
332 del_elem(ca, to);
333
334 /*
Borislav Petkovde0e0622019-04-20 14:06:37 +0200335 * Return a >0 value to callers, to denote that we've reached
336 * the offlining threshold.
Borislav Petkov011d8262017-03-27 11:33:02 +0200337 */
338 ret = 1;
339
340 goto unlock;
341 }
342
Borislav Petkov011d8262017-03-27 11:33:02 +0200343 ca->decay_count++;
344
345 if (ca->decay_count >= CLEAN_ELEMS)
346 do_spring_cleaning(ca);
347
348unlock:
349 mutex_unlock(&ce_mutex);
350
351 return ret;
352}
353
354static int u64_get(void *data, u64 *val)
355{
356 *val = *(u64 *)data;
357
358 return 0;
359}
360
361static int pfn_set(void *data, u64 val)
362{
363 *(u64 *)data = val;
364
Borislav Petkov6d8e2942019-04-20 12:53:05 +0200365 cec_add_elem(val);
366
367 return 0;
Borislav Petkov011d8262017-03-27 11:33:02 +0200368}
369
370DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
371
372static int decay_interval_set(void *data, u64 val)
373{
Cong Wang0ade0b62019-04-16 14:33:51 -0700374 if (val < CEC_DECAY_MIN_INTERVAL)
Borislav Petkov011d8262017-03-27 11:33:02 +0200375 return -EINVAL;
376
Cong Wang0ade0b62019-04-16 14:33:51 -0700377 if (val > CEC_DECAY_MAX_INTERVAL)
Borislav Petkov011d8262017-03-27 11:33:02 +0200378 return -EINVAL;
379
Borislav Petkov5cc6b16e2019-04-20 21:33:08 +0200380 *(u64 *)data = val;
Cong Wang0ade0b62019-04-16 14:33:51 -0700381 decay_interval = val;
Borislav Petkov011d8262017-03-27 11:33:02 +0200382
Cong Wang0ade0b62019-04-16 14:33:51 -0700383 cec_mod_work(decay_interval);
Borislav Petkov5cc6b16e2019-04-20 21:33:08 +0200384
Borislav Petkov011d8262017-03-27 11:33:02 +0200385 return 0;
386}
387DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
388
389static int count_threshold_set(void *data, u64 val)
390{
391 *(u64 *)data = val;
392
393 if (val > COUNT_MASK)
394 val = COUNT_MASK;
395
396 count_threshold = val;
397
398 return 0;
399}
400DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
401
402static int array_dump(struct seq_file *m, void *v)
403{
404 struct ce_array *ca = &ce_arr;
405 u64 prev = 0;
406 int i;
407
408 mutex_lock(&ce_mutex);
409
410 seq_printf(m, "{ n: %d\n", ca->n);
411 for (i = 0; i < ca->n; i++) {
412 u64 this = PFN(ca->array[i]);
413
414 seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
415
416 WARN_ON(prev > this);
417
418 prev = this;
419 }
420
421 seq_printf(m, "}\n");
422
423 seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
424 ca->ces_entered, ca->pfns_poisoned);
425
426 seq_printf(m, "Flags: 0x%x\n", ca->flags);
427
Cong Wang0ade0b62019-04-16 14:33:51 -0700428 seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
Borislav Petkov011d8262017-03-27 11:33:02 +0200429 seq_printf(m, "Decays: %lld\n", ca->decays_done);
430
431 seq_printf(m, "Action threshold: %d\n", count_threshold);
432
433 mutex_unlock(&ce_mutex);
434
435 return 0;
436}
437
438static int array_open(struct inode *inode, struct file *filp)
439{
440 return single_open(filp, array_dump, NULL);
441}
442
443static const struct file_operations array_ops = {
444 .owner = THIS_MODULE,
445 .open = array_open,
446 .read = seq_read,
447 .llseek = seq_lseek,
448 .release = single_release,
449};
450
451static int __init create_debugfs_nodes(void)
452{
453 struct dentry *d, *pfn, *decay, *count, *array;
454
455 d = debugfs_create_dir("cec", ras_debugfs_dir);
456 if (!d) {
457 pr_warn("Error creating cec debugfs node!\n");
458 return -1;
459 }
460
461 pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
462 if (!pfn) {
463 pr_warn("Error creating pfn debugfs node!\n");
464 goto err;
465 }
466
467 array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
468 if (!array) {
469 pr_warn("Error creating array debugfs node!\n");
470 goto err;
471 }
472
473 decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
Cong Wang0ade0b62019-04-16 14:33:51 -0700474 &decay_interval, &decay_interval_ops);
Borislav Petkov011d8262017-03-27 11:33:02 +0200475 if (!decay) {
476 pr_warn("Error creating decay_interval debugfs node!\n");
477 goto err;
478 }
479
480 count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
481 &count_threshold, &count_threshold_ops);
Christophe JAILLET32288da2017-06-26 14:35:32 +0200482 if (!count) {
Borislav Petkov011d8262017-03-27 11:33:02 +0200483 pr_warn("Error creating count_threshold debugfs node!\n");
484 goto err;
485 }
486
487
488 return 0;
489
490err:
491 debugfs_remove_recursive(d);
492
493 return 1;
494}
495
496void __init cec_init(void)
497{
498 if (ce_arr.disabled)
499 return;
500
501 ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
502 if (!ce_arr.array) {
503 pr_err("Error allocating CE array page!\n");
504 return;
505 }
506
Borislav Petkovd0e375e2019-04-20 21:39:24 +0200507 if (create_debugfs_nodes()) {
508 free_page((unsigned long)ce_arr.array);
Borislav Petkov011d8262017-03-27 11:33:02 +0200509 return;
Borislav Petkovd0e375e2019-04-20 21:39:24 +0200510 }
Borislav Petkov011d8262017-03-27 11:33:02 +0200511
Cong Wang0ade0b62019-04-16 14:33:51 -0700512 INIT_DELAYED_WORK(&cec_work, cec_work_fn);
513 schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
Borislav Petkov011d8262017-03-27 11:33:02 +0200514
515 pr_info("Correctable Errors collector initialized.\n");
516}
517
518int __init parse_cec_param(char *str)
519{
520 if (!str)
521 return 0;
522
523 if (*str == '=')
524 str++;
525
Nicolas Iooss69a33002017-10-02 11:28:35 +0200526 if (!strcmp(str, "cec_disable"))
Borislav Petkov011d8262017-03-27 11:33:02 +0200527 ce_arr.disabled = 1;
528 else
529 return 0;
530
531 return 1;
532}