1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs.
8 #include <linux/kernel.h>
9 #include <linux/workqueue.h>
16 * RAS Correctable Errors Collector
18 * This is a simple gadget which collects correctable errors and counts their
19 * occurrence per physical page address.
21 * We've opted for possibly the simplest data structure to collect those - an
22 * array of the size of a memory page. It stores 512 u64's with the following
25 * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
27 * The generation in the two highest order bits is two bits which are set to 11b
28 * on every insertion. During the course of each entry's existence, the
29 * generation field gets decremented during spring cleaning to 10b, then 01b and
32 * This way we're employing the natural numeric ordering to make sure that newly
33 * inserted/touched elements have higher 12-bit counts (which we've manufactured)
34 * and thus iterating over the array initially won't kick out those elements
35 * which were inserted last.
37 * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
38 * elements entered into the array, during which, we're decaying all elements.
39 * If, after decay, an element gets inserted again, its generation is set to 11b
40 * to make sure it has higher numerical count than other, older elements and
41 * thus emulate an LRU-like behavior when deleting elements to free up space
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
47 * initialized to COUNT_MASK which is the maximum.
49 * That error event entry causes cec_add_elem() to return !0 value and thus
50 * signal to its callers to log the error.
52 * To the question why we've chosen a page and moving elements around with
53 * memmove(), it is because it is a very simple structure to handle and max data
54 * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
55 * We wanted to avoid the pointer traversal of more complex structures like a
56 * linked list or some sort of a balancing search tree.
58 * Deleting an element takes O(n) but since it is only a single page, it should
59 * be fast enough and it shouldn't happen all too often depending on error
64 #define pr_fmt(fmt) "RAS: " fmt
67 * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
68 * elements have stayed in the array without having been accessed again.
71 #define DECAY_MASK ((1ULL << DECAY_BITS) - 1)
72 #define MAX_ELEMS (PAGE_SIZE / sizeof(u64))
75 * Threshold amount of inserted elements after which we start spring
78 #define CLEAN_ELEMS (MAX_ELEMS >> DECAY_BITS)
80 /* Bits which count the number of errors happened in this 4K page. */
81 #define COUNT_BITS (PAGE_SHIFT - DECAY_BITS)
82 #define COUNT_MASK ((1ULL << COUNT_BITS) - 1)
83 #define FULL_COUNT_MASK (PAGE_SIZE - 1)
86 * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
89 #define PFN(e) ((e) >> PAGE_SHIFT)
90 #define DECAY(e) (((e) >> COUNT_BITS) & DECAY_MASK)
91 #define COUNT(e) ((unsigned int)(e) & COUNT_MASK)
92 #define FULL_COUNT(e) ((e) & (PAGE_SIZE - 1))
94 static struct ce_array
{
95 u64
*array
; /* container page */
96 unsigned int n
; /* number of elements in the array */
98 unsigned int decay_count
; /*
99 * number of element insertions/increments
100 * since the last spring cleaning.
103 u64 pfns_poisoned
; /*
104 * number of PFNs which got poisoned.
108 * The number of correctable errors
109 * entered into the collector.
113 * Times we did spring cleaning.
118 __u32 disabled
: 1, /* cmdline disabled */
125 static DEFINE_MUTEX(ce_mutex
);
128 /* Amount of errors after which we offline */
129 static u64 action_threshold
= COUNT_MASK
;
131 /* Each element "decays" each decay_interval which is 24hrs by default. */
132 #define CEC_DECAY_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
133 #define CEC_DECAY_MIN_INTERVAL 1 * 60 * 60 /* 1h */
134 #define CEC_DECAY_MAX_INTERVAL 30 * 24 * 60 * 60 /* one month */
135 static struct delayed_work cec_work
;
136 static u64 decay_interval
= CEC_DECAY_DEFAULT_INTERVAL
;
139 * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
140 * element in the array. On insertion and any access, it gets reset to max.
142 static void do_spring_cleaning(struct ce_array
*ca
)
146 for (i
= 0; i
< ca
->n
; i
++) {
147 u8 decay
= DECAY(ca
->array
[i
]);
154 ca
->array
[i
] &= ~(DECAY_MASK
<< COUNT_BITS
);
155 ca
->array
[i
] |= (decay
<< COUNT_BITS
);
162 * @interval in seconds
164 static void cec_mod_work(unsigned long interval
)
169 mod_delayed_work(system_wq
, &cec_work
, round_jiffies(iv
));
172 static void cec_work_fn(struct work_struct
*work
)
174 mutex_lock(&ce_mutex
);
175 do_spring_cleaning(&ce_arr
);
176 mutex_unlock(&ce_mutex
);
178 cec_mod_work(decay_interval
);
182 * @to: index of the smallest element which is >= then @pfn.
184 * Return the index of the pfn if found, otherwise negative value.
186 static int __find_elem(struct ce_array
*ca
, u64 pfn
, unsigned int *to
)
188 int min
= 0, max
= ca
->n
- 1;
192 int i
= (min
+ max
) >> 1;
194 this_pfn
= PFN(ca
->array
[i
]);
198 else if (this_pfn
> pfn
)
200 else if (this_pfn
== pfn
) {
209 * When the loop terminates without finding @pfn, min has the index of
210 * the element slot where the new @pfn should be inserted. The loop
211 * terminates when min > max, which means the min index points to the
212 * bigger element while the max index to the smaller element, in-between
213 * which the new @pfn belongs to.
215 * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
223 static int find_elem(struct ce_array
*ca
, u64 pfn
, unsigned int *to
)
231 return __find_elem(ca
, pfn
, to
);
234 static void del_elem(struct ce_array
*ca
, int idx
)
236 /* Save us a function call when deleting the last element. */
237 if (ca
->n
- (idx
+ 1))
238 memmove((void *)&ca
->array
[idx
],
239 (void *)&ca
->array
[idx
+ 1],
240 (ca
->n
- (idx
+ 1)) * sizeof(u64
));
245 static u64
del_lru_elem_unlocked(struct ce_array
*ca
)
247 unsigned int min
= FULL_COUNT_MASK
;
250 for (i
= 0; i
< ca
->n
; i
++) {
251 unsigned int this = FULL_COUNT(ca
->array
[i
]);
259 del_elem(ca
, min_idx
);
261 return PFN(ca
->array
[min_idx
]);
265 * We return the 0th pfn in the error case under the assumption that it cannot
266 * be poisoned and excessive CEs in there are a serious deal anyway.
268 static u64 __maybe_unused
del_lru_elem(void)
270 struct ce_array
*ca
= &ce_arr
;
276 mutex_lock(&ce_mutex
);
277 pfn
= del_lru_elem_unlocked(ca
);
278 mutex_unlock(&ce_mutex
);
283 static bool sanity_check(struct ce_array
*ca
)
289 for (i
= 0; i
< ca
->n
; i
++) {
290 u64
this = PFN(ca
->array
[i
]);
292 if (WARN(prev
> this, "prev: 0x%016llx <-> this: 0x%016llx\n", prev
, this))
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
]);
305 pr_info(" %03d: [%016llx|%03llx]\n", i
, this, FULL_COUNT(ca
->array
[i
]));
313 * cec_add_elem - Add an element to the CEC array.
314 * @pfn: page frame number to insert
319 * - >0: when the inserted pfn was offlined
321 static int cec_add_elem(u64 pfn
)
323 struct ce_array
*ca
= &ce_arr
;
324 int count
, err
, ret
= 0;
328 * We can be called very early on the identify_cpu() path where we are
329 * not initialized yet. We ignore the error for simplicity.
331 if (!ce_arr
.array
|| ce_arr
.disabled
)
334 mutex_lock(&ce_mutex
);
338 /* Array full, free the LRU slot. */
339 if (ca
->n
== MAX_ELEMS
)
340 WARN_ON(!del_lru_elem_unlocked(ca
));
342 err
= find_elem(ca
, pfn
, &to
);
345 * Shift range [to-end] to make room for one more element.
347 memmove((void *)&ca
->array
[to
+ 1],
348 (void *)&ca
->array
[to
],
349 (ca
->n
- to
) * sizeof(u64
));
351 ca
->array
[to
] = pfn
<< PAGE_SHIFT
;
355 /* Add/refresh element generation and increment count */
356 ca
->array
[to
] |= DECAY_MASK
<< COUNT_BITS
;
359 /* Check action threshold and soft-offline, if reached. */
360 count
= COUNT(ca
->array
[to
]);
361 if (count
>= action_threshold
) {
362 u64 pfn
= ca
->array
[to
] >> PAGE_SHIFT
;
364 if (!pfn_valid(pfn
)) {
365 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn
);
367 /* We have reached max count for this page, soft-offline it. */
368 pr_err("Soft-offlining pfn: 0x%llx\n", pfn
);
369 memory_failure_queue(pfn
, MF_SOFT_OFFLINE
);
376 * Return a >0 value to callers, to denote that we've reached
377 * the offlining threshold.
386 if (ca
->decay_count
>= CLEAN_ELEMS
)
387 do_spring_cleaning(ca
);
389 WARN_ON_ONCE(sanity_check(ca
));
392 mutex_unlock(&ce_mutex
);
397 static int u64_get(void *data
, u64
*val
)
404 static int pfn_set(void *data
, u64 val
)
413 DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops
, u64_get
, pfn_set
, "0x%llx\n");
415 static int decay_interval_set(void *data
, u64 val
)
417 if (val
< CEC_DECAY_MIN_INTERVAL
)
420 if (val
> CEC_DECAY_MAX_INTERVAL
)
424 decay_interval
= val
;
426 cec_mod_work(decay_interval
);
430 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops
, u64_get
, decay_interval_set
, "%lld\n");
432 static int action_threshold_set(void *data
, u64 val
)
436 if (val
> COUNT_MASK
)
439 action_threshold
= val
;
443 DEFINE_DEBUGFS_ATTRIBUTE(action_threshold_ops
, u64_get
, action_threshold_set
, "%lld\n");
445 static const char * const bins
[] = { "00", "01", "10", "11" };
447 static int array_show(struct seq_file
*m
, void *v
)
449 struct ce_array
*ca
= &ce_arr
;
452 mutex_lock(&ce_mutex
);
454 seq_printf(m
, "{ n: %d\n", ca
->n
);
455 for (i
= 0; i
< ca
->n
; i
++) {
456 u64
this = PFN(ca
->array
[i
]);
458 seq_printf(m
, " %3d: [%016llx|%s|%03llx]\n",
459 i
, this, bins
[DECAY(ca
->array
[i
])], COUNT(ca
->array
[i
]));
462 seq_printf(m
, "}\n");
464 seq_printf(m
, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
465 ca
->ces_entered
, ca
->pfns_poisoned
);
467 seq_printf(m
, "Flags: 0x%x\n", ca
->flags
);
469 seq_printf(m
, "Decay interval: %lld seconds\n", decay_interval
);
470 seq_printf(m
, "Decays: %lld\n", ca
->decays_done
);
472 seq_printf(m
, "Action threshold: %lld\n", action_threshold
);
474 mutex_unlock(&ce_mutex
);
479 DEFINE_SHOW_ATTRIBUTE(array
);
481 static int __init
create_debugfs_nodes(void)
483 struct dentry
*d
, *pfn
, *decay
, *count
, *array
, *dfs
;
485 dfs
= ras_get_debugfs_root();
487 pr_warn("Error getting RAS debugfs root!\n");
491 d
= debugfs_create_dir("cec", dfs
);
493 pr_warn("Error creating cec debugfs node!\n");
497 decay
= debugfs_create_file("decay_interval", S_IRUSR
| S_IWUSR
, d
,
498 &decay_interval
, &decay_interval_ops
);
500 pr_warn("Error creating decay_interval debugfs node!\n");
504 count
= debugfs_create_file("action_threshold", S_IRUSR
| S_IWUSR
, d
,
505 &action_threshold
, &action_threshold_ops
);
507 pr_warn("Error creating action_threshold debugfs node!\n");
511 if (!IS_ENABLED(CONFIG_RAS_CEC_DEBUG
))
514 pfn
= debugfs_create_file("pfn", S_IRUSR
| S_IWUSR
, d
, &dfs_pfn
, &pfn_ops
);
516 pr_warn("Error creating pfn debugfs node!\n");
520 array
= debugfs_create_file("array", S_IRUSR
, d
, NULL
, &array_fops
);
522 pr_warn("Error creating array debugfs node!\n");
529 debugfs_remove_recursive(d
);
534 static int cec_notifier(struct notifier_block
*nb
, unsigned long val
,
537 struct mce
*m
= (struct mce
*)data
;
542 /* We eat only correctable DRAM errors with usable addresses. */
543 if (mce_is_memory_error(m
) &&
544 mce_is_correctable(m
) &&
545 mce_usable_address(m
)) {
546 if (!cec_add_elem(m
->addr
>> PAGE_SHIFT
)) {
547 m
->kflags
|= MCE_HANDLED_CEC
;
555 static struct notifier_block cec_nb
= {
556 .notifier_call
= cec_notifier
,
557 .priority
= MCE_PRIO_CEC
,
560 static int __init
cec_init(void)
566 * Intel systems may avoid uncorrectable errors
567 * if pages with corrected errors are aggressively
570 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
)
571 action_threshold
= 2;
573 ce_arr
.array
= (void *)get_zeroed_page(GFP_KERNEL
);
575 pr_err("Error allocating CE array page!\n");
579 if (create_debugfs_nodes()) {
580 free_page((unsigned long)ce_arr
.array
);
584 INIT_DELAYED_WORK(&cec_work
, cec_work_fn
);
585 schedule_delayed_work(&cec_work
, CEC_DECAY_DEFAULT_INTERVAL
);
587 mce_register_decode_chain(&cec_nb
);
589 pr_info("Correctable Errors collector initialized.\n");
592 late_initcall(cec_init
);
594 int __init
parse_cec_param(char *str
)
602 if (!strcmp(str
, "cec_disable"))