blob: 673f8a128397e4d3a9080fd7dcc82f8b0b5f35ef [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 Petkov011d8262017-03-27 11:33:02 +0200297 if (ca->n == MAX_ELEMS)
298 WARN_ON(!del_lru_elem_unlocked(ca));
299
300 ret = find_elem(ca, pfn, &to);
301 if (ret < 0) {
302 /*
303 * Shift range [to-end] to make room for one more element.
304 */
305 memmove((void *)&ca->array[to + 1],
306 (void *)&ca->array[to],
307 (ca->n - to) * sizeof(u64));
308
309 ca->array[to] = (pfn << PAGE_SHIFT) |
310 (DECAY_MASK << COUNT_BITS) | 1;
311
312 ca->n++;
313
314 ret = 0;
315
316 goto decay;
317 }
318
319 count = COUNT(ca->array[to]);
320
321 if (count < count_threshold) {
322 ca->array[to] |= (DECAY_MASK << COUNT_BITS);
323 ca->array[to]++;
324
325 ret = 0;
326 } else {
327 u64 pfn = ca->array[to] >> PAGE_SHIFT;
328
329 if (!pfn_valid(pfn)) {
330 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
331 } else {
332 /* We have reached max count for this page, soft-offline it. */
333 pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
Eric W. Biederman83b57532017-07-09 18:14:01 -0500334 memory_failure_queue(pfn, MF_SOFT_OFFLINE);
Borislav Petkov011d8262017-03-27 11:33:02 +0200335 ca->pfns_poisoned++;
336 }
337
338 del_elem(ca, to);
339
340 /*
341 * Return a >0 value to denote that we've reached the offlining
342 * threshold.
343 */
344 ret = 1;
345
346 goto unlock;
347 }
348
349decay:
350 ca->decay_count++;
351
352 if (ca->decay_count >= CLEAN_ELEMS)
353 do_spring_cleaning(ca);
354
355unlock:
356 mutex_unlock(&ce_mutex);
357
358 return ret;
359}
360
361static int u64_get(void *data, u64 *val)
362{
363 *val = *(u64 *)data;
364
365 return 0;
366}
367
368static int pfn_set(void *data, u64 val)
369{
370 *(u64 *)data = val;
371
372 return cec_add_elem(val);
373}
374
375DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
376
377static int decay_interval_set(void *data, u64 val)
378{
379 *(u64 *)data = val;
380
Cong Wang0ade0b62019-04-16 14:33:51 -0700381 if (val < CEC_DECAY_MIN_INTERVAL)
Borislav Petkov011d8262017-03-27 11:33:02 +0200382 return -EINVAL;
383
Cong Wang0ade0b62019-04-16 14:33:51 -0700384 if (val > CEC_DECAY_MAX_INTERVAL)
Borislav Petkov011d8262017-03-27 11:33:02 +0200385 return -EINVAL;
386
Cong Wang0ade0b62019-04-16 14:33:51 -0700387 decay_interval = val;
Borislav Petkov011d8262017-03-27 11:33:02 +0200388
Cong Wang0ade0b62019-04-16 14:33:51 -0700389 cec_mod_work(decay_interval);
Borislav Petkov011d8262017-03-27 11:33:02 +0200390 return 0;
391}
392DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
393
394static int count_threshold_set(void *data, u64 val)
395{
396 *(u64 *)data = val;
397
398 if (val > COUNT_MASK)
399 val = COUNT_MASK;
400
401 count_threshold = val;
402
403 return 0;
404}
405DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
406
407static int array_dump(struct seq_file *m, void *v)
408{
409 struct ce_array *ca = &ce_arr;
410 u64 prev = 0;
411 int i;
412
413 mutex_lock(&ce_mutex);
414
415 seq_printf(m, "{ n: %d\n", ca->n);
416 for (i = 0; i < ca->n; i++) {
417 u64 this = PFN(ca->array[i]);
418
419 seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
420
421 WARN_ON(prev > this);
422
423 prev = this;
424 }
425
426 seq_printf(m, "}\n");
427
428 seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
429 ca->ces_entered, ca->pfns_poisoned);
430
431 seq_printf(m, "Flags: 0x%x\n", ca->flags);
432
Cong Wang0ade0b62019-04-16 14:33:51 -0700433 seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
Borislav Petkov011d8262017-03-27 11:33:02 +0200434 seq_printf(m, "Decays: %lld\n", ca->decays_done);
435
436 seq_printf(m, "Action threshold: %d\n", count_threshold);
437
438 mutex_unlock(&ce_mutex);
439
440 return 0;
441}
442
443static int array_open(struct inode *inode, struct file *filp)
444{
445 return single_open(filp, array_dump, NULL);
446}
447
448static const struct file_operations array_ops = {
449 .owner = THIS_MODULE,
450 .open = array_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = single_release,
454};
455
456static int __init create_debugfs_nodes(void)
457{
458 struct dentry *d, *pfn, *decay, *count, *array;
459
460 d = debugfs_create_dir("cec", ras_debugfs_dir);
461 if (!d) {
462 pr_warn("Error creating cec debugfs node!\n");
463 return -1;
464 }
465
466 pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
467 if (!pfn) {
468 pr_warn("Error creating pfn debugfs node!\n");
469 goto err;
470 }
471
472 array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
473 if (!array) {
474 pr_warn("Error creating array debugfs node!\n");
475 goto err;
476 }
477
478 decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
Cong Wang0ade0b62019-04-16 14:33:51 -0700479 &decay_interval, &decay_interval_ops);
Borislav Petkov011d8262017-03-27 11:33:02 +0200480 if (!decay) {
481 pr_warn("Error creating decay_interval debugfs node!\n");
482 goto err;
483 }
484
485 count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
486 &count_threshold, &count_threshold_ops);
Christophe JAILLET32288da2017-06-26 14:35:32 +0200487 if (!count) {
Borislav Petkov011d8262017-03-27 11:33:02 +0200488 pr_warn("Error creating count_threshold debugfs node!\n");
489 goto err;
490 }
491
492
493 return 0;
494
495err:
496 debugfs_remove_recursive(d);
497
498 return 1;
499}
500
501void __init cec_init(void)
502{
503 if (ce_arr.disabled)
504 return;
505
506 ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
507 if (!ce_arr.array) {
508 pr_err("Error allocating CE array page!\n");
509 return;
510 }
511
512 if (create_debugfs_nodes())
513 return;
514
Cong Wang0ade0b62019-04-16 14:33:51 -0700515 INIT_DELAYED_WORK(&cec_work, cec_work_fn);
516 schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
Borislav Petkov011d8262017-03-27 11:33:02 +0200517
518 pr_info("Correctable Errors collector initialized.\n");
519}
520
521int __init parse_cec_param(char *str)
522{
523 if (!str)
524 return 0;
525
526 if (*str == '=')
527 str++;
528
Nicolas Iooss69a33002017-10-02 11:28:35 +0200529 if (!strcmp(str, "cec_disable"))
Borislav Petkov011d8262017-03-27 11:33:02 +0200530 ce_arr.disabled = 1;
531 else
532 return 0;
533
534 return 1;
535}