// SPDX-License-Identifier: GPL-2.0 /* * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs. */ #include <linux/mm.h> #include <linux/gfp.h> #include <linux/ras.h> #include <linux/kernel.h> #include <linux/workqueue.h> #include <asm/mce.h> #include "debugfs.h" /* * RAS Correctable Errors Collector * * This is a simple gadget which collects correctable errors and counts their * occurrence per physical page address. * * We've opted for possibly the simplest data structure to collect those - an * array of the size of a memory page. It stores 512 u64's with the following * structure: * * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0] * * The generation in the two highest order bits is two bits which are set to 11b * on every insertion. During the course of each entry's existence, the * generation field gets decremented during spring cleaning to 10b, then 01b and * then 00b. * * This way we're employing the natural numeric ordering to make sure that newly * inserted/touched elements have higher 12-bit counts (which we've manufactured) * and thus iterating over the array initially won't kick out those elements * which were inserted last. * * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of * elements entered into the array, during which, we're decaying all elements. * If, after decay, an element gets inserted again, its generation is set to 11b * to make sure it has higher numerical count than other, older elements and * thus emulate an LRU-like behavior when deleting elements to free up space * in the page. * * When an element reaches it's max count of action_threshold, we try to poison * it by assuming that errors triggered action_threshold times in a single page * are excessive and that page shouldn't be used anymore. action_threshold is * initialized to COUNT_MASK which is the maximum. * * That error event entry causes cec_add_elem() to return !0 value and thus * signal to its callers to log the error. * * To the question why we've chosen a page and moving elements around with * memmove(), it is because it is a very simple structure to handle and max data * movement is 4K which on highly optimized modern CPUs is almost unnoticeable. * We wanted to avoid the pointer traversal of more complex structures like a * linked list or some sort of a balancing search tree. * * Deleting an element takes O(n) but since it is only a single page, it should * be fast enough and it shouldn't happen all too often depending on error * patterns. */ #undef pr_fmt #define pr_fmt(fmt) … /* * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long * elements have stayed in the array without having been accessed again. */ #define DECAY_BITS … #define DECAY_MASK … #define MAX_ELEMS … /* * Threshold amount of inserted elements after which we start spring * cleaning. */ #define CLEAN_ELEMS … /* Bits which count the number of errors happened in this 4K page. */ #define COUNT_BITS … #define COUNT_MASK … #define FULL_COUNT_MASK … /* * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ] */ #define PFN(e) … #define DECAY(e) … #define COUNT(e) … #define FULL_COUNT(e) … static struct ce_array { … } ce_arr; static DEFINE_MUTEX(ce_mutex); static u64 dfs_pfn; /* Amount of errors after which we offline */ static u64 action_threshold = …; /* Each element "decays" each decay_interval which is 24hrs by default. */ #define CEC_DECAY_DEFAULT_INTERVAL … #define CEC_DECAY_MIN_INTERVAL … #define CEC_DECAY_MAX_INTERVAL … static struct delayed_work cec_work; static u64 decay_interval = …; /* * Decrement decay value. We're using DECAY_BITS bits to denote decay of an * element in the array. On insertion and any access, it gets reset to max. */ static void do_spring_cleaning(struct ce_array *ca) { … } /* * @interval in seconds */ static void cec_mod_work(unsigned long interval) { … } static void cec_work_fn(struct work_struct *work) { … } /* * @to: index of the smallest element which is >= then @pfn. * * Return the index of the pfn if found, otherwise negative value. */ static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) { … } static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) { … } static void del_elem(struct ce_array *ca, int idx) { … } static u64 del_lru_elem_unlocked(struct ce_array *ca) { … } /* * We return the 0th pfn in the error case under the assumption that it cannot * be poisoned and excessive CEs in there are a serious deal anyway. */ static u64 __maybe_unused del_lru_elem(void) { … } static bool sanity_check(struct ce_array *ca) { … } /** * cec_add_elem - Add an element to the CEC array. * @pfn: page frame number to insert * * Return values: * - <0: on error * - 0: on success * - >0: when the inserted pfn was offlined */ static int cec_add_elem(u64 pfn) { … } static int u64_get(void *data, u64 *val) { … } static int pfn_set(void *data, u64 val) { … } DEFINE_DEBUGFS_ATTRIBUTE(…); static int decay_interval_set(void *data, u64 val) { … } DEFINE_DEBUGFS_ATTRIBUTE(…); static int action_threshold_set(void *data, u64 val) { … } DEFINE_DEBUGFS_ATTRIBUTE(…); static const char * const bins[] = …; static int array_show(struct seq_file *m, void *v) { … } DEFINE_SHOW_ATTRIBUTE(…); static int __init create_debugfs_nodes(void) { … } static int cec_notifier(struct notifier_block *nb, unsigned long val, void *data) { … } static struct notifier_block cec_nb = …; static int __init cec_init(void) { … } late_initcall(cec_init); int __init parse_cec_param(char *str) { … }