Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/ras.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/mce.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "debugfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * RAS Correctable Errors Collector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * This is a simple gadget which collects correctable errors and counts their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * occurrence per physical page address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * We've opted for possibly the simplest data structure to collect those - an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * array of the size of a memory page. It stores 512 u64's with the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * structure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * The generation in the two highest order bits is two bits which are set to 11b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * on every insertion. During the course of each entry's existence, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * generation field gets decremented during spring cleaning to 10b, then 01b and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * then 00b.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * This way we're employing the natural numeric ordering to make sure that newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * inserted/touched elements have higher 12-bit counts (which we've manufactured)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * and thus iterating over the array initially won't kick out those elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * which were inserted last.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * elements entered into the array, during which, we're decaying all elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * If, after decay, an element gets inserted again, its generation is set to 11b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * to make sure it has higher numerical count than other, older elements and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * thus emulate an an LRU-like behavior when deleting elements to free up space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * in the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * When an element reaches it's max count of action_threshold, we try to poison
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * it by assuming that errors triggered action_threshold times in a single page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * are excessive and that page shouldn't be used anymore. action_threshold is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * initialized to COUNT_MASK which is the maximum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * That error event entry causes cec_add_elem() to return !0 value and thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * signal to its callers to log the error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * To the question why we've chosen a page and moving elements around with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * memmove(), it is because it is a very simple structure to handle and max data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * We wanted to avoid the pointer traversal of more complex structures like a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * linked list or some sort of a balancing search tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * Deleting an element takes O(n) but since it is only a single page, it should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * be fast enough and it shouldn't happen all too often depending on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * patterns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #undef pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define pr_fmt(fmt) "RAS: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * elements have stayed in the array without having been accessed again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define DECAY_BITS		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define DECAY_MASK		((1ULL << DECAY_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define MAX_ELEMS		(PAGE_SIZE / sizeof(u64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * Threshold amount of inserted elements after which we start spring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * cleaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define CLEAN_ELEMS		(MAX_ELEMS >> DECAY_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) /* Bits which count the number of errors happened in this 4K page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define COUNT_BITS		(PAGE_SHIFT - DECAY_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define COUNT_MASK		((1ULL << COUNT_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define FULL_COUNT_MASK		(PAGE_SIZE - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #define PFN(e)			((e) >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define DECAY(e)		(((e) >> COUNT_BITS) & DECAY_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define COUNT(e)		((unsigned int)(e) & COUNT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define FULL_COUNT(e)		((e) & (PAGE_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static struct ce_array {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u64 *array;			/* container page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned int n;			/* number of elements in the array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	unsigned int decay_count;	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 					 * number of element insertions/increments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 					 * since the last spring cleaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	u64 pfns_poisoned;		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 					 * number of PFNs which got poisoned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	u64 ces_entered;		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 					 * The number of correctable errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					 * entered into the collector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u64 decays_done;		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					 * Times we did spring cleaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			__u32	disabled : 1,	/* cmdline disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			__resv   : 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		__u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } ce_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static DEFINE_MUTEX(ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static u64 dfs_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* Amount of errors after which we offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static u64 action_threshold = COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Each element "decays" each decay_interval which is 24hrs by default. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define CEC_DECAY_DEFAULT_INTERVAL	24 * 60 * 60	/* 24 hrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define CEC_DECAY_MIN_INTERVAL		 1 * 60 * 60	/* 1h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define CEC_DECAY_MAX_INTERVAL	   30 *	24 * 60 * 60	/* one month */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct delayed_work cec_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static u64 decay_interval = CEC_DECAY_DEFAULT_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * element in the array. On insertion and any access, it gets reset to max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void do_spring_cleaning(struct ce_array *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	for (i = 0; i < ca->n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		u8 decay = DECAY(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (!decay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		decay--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		ca->array[i] |= (decay << COUNT_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	ca->decay_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ca->decays_done++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * @interval in seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void cec_mod_work(unsigned long interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	unsigned long iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	iv = interval * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	mod_delayed_work(system_wq, &cec_work, round_jiffies(iv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void cec_work_fn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	mutex_lock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	do_spring_cleaning(&ce_arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	mutex_unlock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	cec_mod_work(decay_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * @to: index of the smallest element which is >= then @pfn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * Return the index of the pfn if found, otherwise negative value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int min = 0, max = ca->n - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	u64 this_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	while (min <= max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		int i = (min + max) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		this_pfn = PFN(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (this_pfn < pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			min = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		else if (this_pfn > pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			max = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		else if (this_pfn == pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			if (to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				*to = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * When the loop terminates without finding @pfn, min has the index of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * the element slot where the new @pfn should be inserted. The loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * terminates when min > max, which means the min index points to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * bigger element while the max index to the smaller element, in-between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * which the new @pfn belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		*to = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	WARN_ON(!to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (!ca->n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		*to = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return __find_elem(ca, pfn, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static void del_elem(struct ce_array *ca, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* Save us a function call when deleting the last element. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (ca->n - (idx + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		memmove((void *)&ca->array[idx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			(void *)&ca->array[idx + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			(ca->n - (idx + 1)) * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	ca->n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static u64 del_lru_elem_unlocked(struct ce_array *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	unsigned int min = FULL_COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	int i, min_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	for (i = 0; i < ca->n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		unsigned int this = FULL_COUNT(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (min > this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			min = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			min_idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	del_elem(ca, min_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return PFN(ca->array[min_idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  * We return the 0th pfn in the error case under the assumption that it cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * be poisoned and excessive CEs in there are a serious deal anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static u64 __maybe_unused del_lru_elem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct ce_array *ca = &ce_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	u64 pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (!ca->n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	mutex_lock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	pfn = del_lru_elem_unlocked(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	mutex_unlock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static bool sanity_check(struct ce_array *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	u64 prev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	for (i = 0; i < ca->n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		u64 this = PFN(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (WARN(prev > this, "prev: 0x%016llx <-> this: 0x%016llx\n", prev, this))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		prev = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	pr_info("Sanity check dump:\n{ n: %d\n", ca->n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	for (i = 0; i < ca->n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		u64 this = PFN(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		pr_info(" %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	pr_info("}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * cec_add_elem - Add an element to the CEC array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * @pfn:	page frame number to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * Return values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * - <0:	on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * -  0:	on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * - >0:	when the inserted pfn was offlined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int cec_add_elem(u64 pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct ce_array *ca = &ce_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	int count, err, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	unsigned int to = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	 * We can be called very early on the identify_cpu() path where we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	 * not initialized yet. We ignore the error for simplicity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (!ce_arr.array || ce_arr.disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_lock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	ca->ces_entered++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	/* Array full, free the LRU slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (ca->n == MAX_ELEMS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		WARN_ON(!del_lru_elem_unlocked(ca));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	err = find_elem(ca, pfn, &to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		 * Shift range [to-end] to make room for one more element.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		memmove((void *)&ca->array[to + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			(void *)&ca->array[to],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			(ca->n - to) * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		ca->array[to] = pfn << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		ca->n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/* Add/refresh element generation and increment count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	ca->array[to] |= DECAY_MASK << COUNT_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	ca->array[to]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	/* Check action threshold and soft-offline, if reached. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	count = COUNT(ca->array[to]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (count >= action_threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		u64 pfn = ca->array[to] >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (!pfn_valid(pfn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			/* We have reached max count for this page, soft-offline it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			memory_failure_queue(pfn, MF_SOFT_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			ca->pfns_poisoned++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		del_elem(ca, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		 * Return a >0 value to callers, to denote that we've reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		 * the offlining threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	ca->decay_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (ca->decay_count >= CLEAN_ELEMS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		do_spring_cleaning(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	WARN_ON_ONCE(sanity_check(ca));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	mutex_unlock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int u64_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	*val = *(u64 *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int pfn_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	*(u64 *)data = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	cec_add_elem(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static int decay_interval_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (val < CEC_DECAY_MIN_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (val > CEC_DECAY_MAX_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	*(u64 *)data   = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	decay_interval = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	cec_mod_work(decay_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static int action_threshold_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	*(u64 *)data = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (val > COUNT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		val = COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	action_threshold = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) DEFINE_DEBUGFS_ATTRIBUTE(action_threshold_ops, u64_get, action_threshold_set, "%lld\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static const char * const bins[] = { "00", "01", "10", "11" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static int array_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct ce_array *ca = &ce_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	mutex_lock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	seq_printf(m, "{ n: %d\n", ca->n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	for (i = 0; i < ca->n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		u64 this = PFN(ca->array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		seq_printf(m, " %3d: [%016llx|%s|%03llx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			   i, this, bins[DECAY(ca->array[i])], COUNT(ca->array[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	seq_printf(m, "}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		   ca->ces_entered, ca->pfns_poisoned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	seq_printf(m, "Flags: 0x%x\n", ca->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	seq_printf(m, "Decays: %lld\n", ca->decays_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	seq_printf(m, "Action threshold: %lld\n", action_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	mutex_unlock(&ce_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) DEFINE_SHOW_ATTRIBUTE(array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static int __init create_debugfs_nodes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	struct dentry *d, *pfn, *decay, *count, *array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	d = debugfs_create_dir("cec", ras_debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (!d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		pr_warn("Error creating cec debugfs node!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				    &decay_interval, &decay_interval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (!decay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		pr_warn("Error creating decay_interval debugfs node!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	count = debugfs_create_file("action_threshold", S_IRUSR | S_IWUSR, d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 				    &action_threshold, &action_threshold_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (!count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		pr_warn("Error creating action_threshold debugfs node!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (!IS_ENABLED(CONFIG_RAS_CEC_DEBUG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (!pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		pr_warn("Error creating pfn debugfs node!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if (!array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		pr_warn("Error creating array debugfs node!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	debugfs_remove_recursive(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int cec_notifier(struct notifier_block *nb, unsigned long val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	struct mce *m = (struct mce *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (!m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	/* We eat only correctable DRAM errors with usable addresses. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (mce_is_memory_error(m) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	    mce_is_correctable(m)  &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	    mce_usable_address(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		if (!cec_add_elem(m->addr >> PAGE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			m->kflags |= MCE_HANDLED_CEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static struct notifier_block cec_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	.notifier_call	= cec_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	.priority	= MCE_PRIO_CEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static int __init cec_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (ce_arr.disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (!ce_arr.array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		pr_err("Error allocating CE array page!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (create_debugfs_nodes()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		free_page((unsigned long)ce_arr.array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	INIT_DELAYED_WORK(&cec_work, cec_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	mce_register_decode_chain(&cec_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	pr_info("Correctable Errors collector initialized.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) late_initcall(cec_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) int __init parse_cec_param(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (*str == '=')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (!strcmp(str, "cec_disable"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		ce_arr.disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }