hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/ras/cec.c
....@@ -1,6 +1,10 @@
11 // SPDX-License-Identifier: GPL-2.0
2
+/*
3
+ * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs.
4
+ */
25 #include <linux/mm.h>
36 #include <linux/gfp.h>
7
+#include <linux/ras.h>
48 #include <linux/kernel.h>
59 #include <linux/workqueue.h>
610
....@@ -37,9 +41,9 @@
3741 * thus emulate an an LRU-like behavior when deleting elements to free up space
3842 * in the page.
3943 *
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
44
+ * When an element reaches it's max count of action_threshold, we try to poison
45
+ * it by assuming that errors triggered action_threshold times in a single page
46
+ * are excessive and that page shouldn't be used anymore. action_threshold is
4347 * initialized to COUNT_MASK which is the maximum.
4448 *
4549 * That error event entry causes cec_add_elem() to return !0 value and thus
....@@ -122,7 +126,7 @@
122126 static u64 dfs_pfn;
123127
124128 /* Amount of errors after which we offline */
125
-static unsigned int count_threshold = COUNT_MASK;
129
+static u64 action_threshold = COUNT_MASK;
126130
127131 /* Each element "decays" each decay_interval which is 24hrs by default. */
128132 #define CEC_DECAY_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
....@@ -276,12 +280,49 @@
276280 return pfn;
277281 }
278282
283
+static bool sanity_check(struct ce_array *ca)
284
+{
285
+ bool ret = false;
286
+ u64 prev = 0;
287
+ int i;
279288
280
-int cec_add_elem(u64 pfn)
289
+ for (i = 0; i < ca->n; i++) {
290
+ u64 this = PFN(ca->array[i]);
291
+
292
+ if (WARN(prev > this, "prev: 0x%016llx <-> this: 0x%016llx\n", prev, this))
293
+ ret = true;
294
+
295
+ prev = this;
296
+ }
297
+
298
+ if (!ret)
299
+ return ret;
300
+
301
+ pr_info("Sanity check dump:\n{ n: %d\n", ca->n);
302
+ for (i = 0; i < ca->n; i++) {
303
+ u64 this = PFN(ca->array[i]);
304
+
305
+ pr_info(" %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
306
+ }
307
+ pr_info("}\n");
308
+
309
+ return ret;
310
+}
311
+
312
+/**
313
+ * cec_add_elem - Add an element to the CEC array.
314
+ * @pfn: page frame number to insert
315
+ *
316
+ * Return values:
317
+ * - <0: on error
318
+ * - 0: on success
319
+ * - >0: when the inserted pfn was offlined
320
+ */
321
+static int cec_add_elem(u64 pfn)
281322 {
282323 struct ce_array *ca = &ce_arr;
283
- unsigned int to;
284
- int count, ret = 0;
324
+ int count, err, ret = 0;
325
+ unsigned int to = 0;
285326
286327 /*
287328 * We can be called very early on the identify_cpu() path where we are
....@@ -290,15 +331,16 @@
290331 if (!ce_arr.array || ce_arr.disabled)
291332 return -ENODEV;
292333
293
- ca->ces_entered++;
294
-
295334 mutex_lock(&ce_mutex);
296335
336
+ ca->ces_entered++;
337
+
338
+ /* Array full, free the LRU slot. */
297339 if (ca->n == MAX_ELEMS)
298340 WARN_ON(!del_lru_elem_unlocked(ca));
299341
300
- ret = find_elem(ca, pfn, &to);
301
- if (ret < 0) {
342
+ err = find_elem(ca, pfn, &to);
343
+ if (err < 0) {
302344 /*
303345 * Shift range [to-end] to make room for one more element.
304346 */
....@@ -306,24 +348,17 @@
306348 (void *)&ca->array[to],
307349 (ca->n - to) * sizeof(u64));
308350
309
- ca->array[to] = (pfn << PAGE_SHIFT) |
310
- (DECAY_MASK << COUNT_BITS) | 1;
311
-
351
+ ca->array[to] = pfn << PAGE_SHIFT;
312352 ca->n++;
313
-
314
- ret = 0;
315
-
316
- goto decay;
317353 }
318354
355
+ /* Add/refresh element generation and increment count */
356
+ ca->array[to] |= DECAY_MASK << COUNT_BITS;
357
+ ca->array[to]++;
358
+
359
+ /* Check action threshold and soft-offline, if reached. */
319360 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 {
361
+ if (count >= action_threshold) {
327362 u64 pfn = ca->array[to] >> PAGE_SHIFT;
328363
329364 if (!pfn_valid(pfn)) {
....@@ -338,19 +373,20 @@
338373 del_elem(ca, to);
339374
340375 /*
341
- * Return a >0 value to denote that we've reached the offlining
342
- * threshold.
376
+ * Return a >0 value to callers, to denote that we've reached
377
+ * the offlining threshold.
343378 */
344379 ret = 1;
345380
346381 goto unlock;
347382 }
348383
349
-decay:
350384 ca->decay_count++;
351385
352386 if (ca->decay_count >= CLEAN_ELEMS)
353387 do_spring_cleaning(ca);
388
+
389
+ WARN_ON_ONCE(sanity_check(ca));
354390
355391 unlock:
356392 mutex_unlock(&ce_mutex);
....@@ -378,38 +414,39 @@
378414
379415 static int decay_interval_set(void *data, u64 val)
380416 {
381
- *(u64 *)data = val;
382
-
383417 if (val < CEC_DECAY_MIN_INTERVAL)
384418 return -EINVAL;
385419
386420 if (val > CEC_DECAY_MAX_INTERVAL)
387421 return -EINVAL;
388422
423
+ *(u64 *)data = val;
389424 decay_interval = val;
390425
391426 cec_mod_work(decay_interval);
427
+
392428 return 0;
393429 }
394430 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
395431
396
-static int count_threshold_set(void *data, u64 val)
432
+static int action_threshold_set(void *data, u64 val)
397433 {
398434 *(u64 *)data = val;
399435
400436 if (val > COUNT_MASK)
401437 val = COUNT_MASK;
402438
403
- count_threshold = val;
439
+ action_threshold = val;
404440
405441 return 0;
406442 }
407
-DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
443
+DEFINE_DEBUGFS_ATTRIBUTE(action_threshold_ops, u64_get, action_threshold_set, "%lld\n");
408444
409
-static int array_dump(struct seq_file *m, void *v)
445
+static const char * const bins[] = { "00", "01", "10", "11" };
446
+
447
+static int array_show(struct seq_file *m, void *v)
410448 {
411449 struct ce_array *ca = &ce_arr;
412
- u64 prev = 0;
413450 int i;
414451
415452 mutex_lock(&ce_mutex);
....@@ -418,11 +455,8 @@
418455 for (i = 0; i < ca->n; i++) {
419456 u64 this = PFN(ca->array[i]);
420457
421
- seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
422
-
423
- WARN_ON(prev > this);
424
-
425
- prev = this;
458
+ seq_printf(m, " %3d: [%016llx|%s|%03llx]\n",
459
+ i, this, bins[DECAY(ca->array[i])], COUNT(ca->array[i]));
426460 }
427461
428462 seq_printf(m, "}\n");
....@@ -435,25 +469,14 @@
435469 seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
436470 seq_printf(m, "Decays: %lld\n", ca->decays_done);
437471
438
- seq_printf(m, "Action threshold: %d\n", count_threshold);
472
+ seq_printf(m, "Action threshold: %lld\n", action_threshold);
439473
440474 mutex_unlock(&ce_mutex);
441475
442476 return 0;
443477 }
444478
445
-static int array_open(struct inode *inode, struct file *filp)
446
-{
447
- return single_open(filp, array_dump, NULL);
448
-}
449
-
450
-static const struct file_operations array_ops = {
451
- .owner = THIS_MODULE,
452
- .open = array_open,
453
- .read = seq_read,
454
- .llseek = seq_lseek,
455
- .release = single_release,
456
-};
479
+DEFINE_SHOW_ATTRIBUTE(array);
457480
458481 static int __init create_debugfs_nodes(void)
459482 {
....@@ -465,18 +488,6 @@
465488 return -1;
466489 }
467490
468
- pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
469
- if (!pfn) {
470
- pr_warn("Error creating pfn debugfs node!\n");
471
- goto err;
472
- }
473
-
474
- array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
475
- if (!array) {
476
- pr_warn("Error creating array debugfs node!\n");
477
- goto err;
478
- }
479
-
480491 decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
481492 &decay_interval, &decay_interval_ops);
482493 if (!decay) {
....@@ -484,13 +495,27 @@
484495 goto err;
485496 }
486497
487
- count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
488
- &count_threshold, &count_threshold_ops);
498
+ count = debugfs_create_file("action_threshold", S_IRUSR | S_IWUSR, d,
499
+ &action_threshold, &action_threshold_ops);
489500 if (!count) {
490
- pr_warn("Error creating count_threshold debugfs node!\n");
501
+ pr_warn("Error creating action_threshold debugfs node!\n");
491502 goto err;
492503 }
493504
505
+ if (!IS_ENABLED(CONFIG_RAS_CEC_DEBUG))
506
+ return 0;
507
+
508
+ pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
509
+ if (!pfn) {
510
+ pr_warn("Error creating pfn debugfs node!\n");
511
+ goto err;
512
+ }
513
+
514
+ array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_fops);
515
+ if (!array) {
516
+ pr_warn("Error creating array debugfs node!\n");
517
+ goto err;
518
+ }
494519
495520 return 0;
496521
....@@ -500,25 +525,57 @@
500525 return 1;
501526 }
502527
503
-void __init cec_init(void)
528
+static int cec_notifier(struct notifier_block *nb, unsigned long val,
529
+ void *data)
530
+{
531
+ struct mce *m = (struct mce *)data;
532
+
533
+ if (!m)
534
+ return NOTIFY_DONE;
535
+
536
+ /* We eat only correctable DRAM errors with usable addresses. */
537
+ if (mce_is_memory_error(m) &&
538
+ mce_is_correctable(m) &&
539
+ mce_usable_address(m)) {
540
+ if (!cec_add_elem(m->addr >> PAGE_SHIFT)) {
541
+ m->kflags |= MCE_HANDLED_CEC;
542
+ return NOTIFY_OK;
543
+ }
544
+ }
545
+
546
+ return NOTIFY_DONE;
547
+}
548
+
549
+static struct notifier_block cec_nb = {
550
+ .notifier_call = cec_notifier,
551
+ .priority = MCE_PRIO_CEC,
552
+};
553
+
554
+static int __init cec_init(void)
504555 {
505556 if (ce_arr.disabled)
506
- return;
557
+ return -ENODEV;
507558
508559 ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
509560 if (!ce_arr.array) {
510561 pr_err("Error allocating CE array page!\n");
511
- return;
562
+ return -ENOMEM;
512563 }
513564
514
- if (create_debugfs_nodes())
515
- return;
565
+ if (create_debugfs_nodes()) {
566
+ free_page((unsigned long)ce_arr.array);
567
+ return -ENOMEM;
568
+ }
516569
517570 INIT_DELAYED_WORK(&cec_work, cec_work_fn);
518571 schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
519572
573
+ mce_register_decode_chain(&cec_nb);
574
+
520575 pr_info("Correctable Errors collector initialized.\n");
576
+ return 0;
521577 }
578
+late_initcall(cec_init);
522579
523580 int __init parse_cec_param(char *str)
524581 {