1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/mm.h> 3 #include <linux/gfp.h> 4 #include <linux/kernel.h> 5 #include <linux/workqueue.h> 6 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 90 static 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 121 static DEFINE_MUTEX(ce_mutex); 122 static u64 dfs_pfn; 123 124 /* Amount of errors after which we offline */ 125 static unsigned int count_threshold = COUNT_MASK; 126 127 /* 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 */ 131 static struct delayed_work cec_work; 132 static u64 decay_interval = CEC_DECAY_DEFAULT_INTERVAL; 133 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 */ 138 static 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 */ 160 static void cec_mod_work(unsigned long interval) 161 { 162 unsigned long iv; 163 164 iv = interval * HZ; 165 mod_delayed_work(system_wq, &cec_work, round_jiffies(iv)); 166 } 167 168 static void cec_work_fn(struct work_struct *work) 169 { 170 mutex_lock(&ce_mutex); 171 do_spring_cleaning(&ce_arr); 172 mutex_unlock(&ce_mutex); 173 174 cec_mod_work(decay_interval); 175 } 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 */ 182 static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) 183 { 184 int min = 0, max = ca->n - 1; 185 u64 this_pfn; 186 187 while (min <= max) { 188 int i = (min + max) >> 1; 189 190 this_pfn = PFN(ca->array[i]); 191 192 if (this_pfn < pfn) 193 min = i + 1; 194 else if (this_pfn > pfn) 195 max = i - 1; 196 else if (this_pfn == pfn) { 197 if (to) 198 *to = i; 199 200 return i; 201 } 202 } 203 204 /* 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 */ 213 if (to) 214 *to = min; 215 216 return -ENOKEY; 217 } 218 219 static 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 230 static 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 241 static 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 */ 264 static 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 280 int 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 293 mutex_lock(&ce_mutex); 294 295 ca->ces_entered++; 296 297 /* Array full, free the LRU slot. */ 298 if (ca->n == MAX_ELEMS) 299 WARN_ON(!del_lru_elem_unlocked(ca)); 300 301 ret = find_elem(ca, pfn, &to); 302 if (ret < 0) { 303 /* 304 * Shift range [to-end] to make room for one more element. 305 */ 306 memmove((void *)&ca->array[to + 1], 307 (void *)&ca->array[to], 308 (ca->n - to) * sizeof(u64)); 309 310 ca->array[to] = pfn << PAGE_SHIFT; 311 ca->n++; 312 } 313 314 /* Add/refresh element generation and increment count */ 315 ca->array[to] |= DECAY_MASK << COUNT_BITS; 316 ca->array[to]++; 317 318 /* Check action threshold and soft-offline, if reached. */ 319 count = COUNT(ca->array[to]); 320 if (count >= count_threshold) { 321 u64 pfn = ca->array[to] >> PAGE_SHIFT; 322 323 if (!pfn_valid(pfn)) { 324 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn); 325 } else { 326 /* We have reached max count for this page, soft-offline it. */ 327 pr_err("Soft-offlining pfn: 0x%llx\n", pfn); 328 memory_failure_queue(pfn, MF_SOFT_OFFLINE); 329 ca->pfns_poisoned++; 330 } 331 332 del_elem(ca, to); 333 334 /* 335 * Return a >0 value to callers, to denote that we've reached 336 * the offlining threshold. 337 */ 338 ret = 1; 339 340 goto unlock; 341 } 342 343 ca->decay_count++; 344 345 if (ca->decay_count >= CLEAN_ELEMS) 346 do_spring_cleaning(ca); 347 348 unlock: 349 mutex_unlock(&ce_mutex); 350 351 return ret; 352 } 353 354 static int u64_get(void *data, u64 *val) 355 { 356 *val = *(u64 *)data; 357 358 return 0; 359 } 360 361 static int pfn_set(void *data, u64 val) 362 { 363 *(u64 *)data = val; 364 365 cec_add_elem(val); 366 367 return 0; 368 } 369 370 DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n"); 371 372 static int decay_interval_set(void *data, u64 val) 373 { 374 if (val < CEC_DECAY_MIN_INTERVAL) 375 return -EINVAL; 376 377 if (val > CEC_DECAY_MAX_INTERVAL) 378 return -EINVAL; 379 380 *(u64 *)data = val; 381 decay_interval = val; 382 383 cec_mod_work(decay_interval); 384 385 return 0; 386 } 387 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n"); 388 389 static int count_threshold_set(void *data, u64 val) 390 { 391 *(u64 *)data = val; 392 393 if (val > COUNT_MASK) 394 val = COUNT_MASK; 395 396 count_threshold = val; 397 398 return 0; 399 } 400 DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n"); 401 402 static int array_dump(struct seq_file *m, void *v) 403 { 404 struct ce_array *ca = &ce_arr; 405 u64 prev = 0; 406 int i; 407 408 mutex_lock(&ce_mutex); 409 410 seq_printf(m, "{ n: %d\n", ca->n); 411 for (i = 0; i < ca->n; i++) { 412 u64 this = PFN(ca->array[i]); 413 414 seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i])); 415 416 WARN_ON(prev > this); 417 418 prev = this; 419 } 420 421 seq_printf(m, "}\n"); 422 423 seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n", 424 ca->ces_entered, ca->pfns_poisoned); 425 426 seq_printf(m, "Flags: 0x%x\n", ca->flags); 427 428 seq_printf(m, "Decay interval: %lld seconds\n", decay_interval); 429 seq_printf(m, "Decays: %lld\n", ca->decays_done); 430 431 seq_printf(m, "Action threshold: %d\n", count_threshold); 432 433 mutex_unlock(&ce_mutex); 434 435 return 0; 436 } 437 438 static int array_open(struct inode *inode, struct file *filp) 439 { 440 return single_open(filp, array_dump, NULL); 441 } 442 443 static const struct file_operations array_ops = { 444 .owner = THIS_MODULE, 445 .open = array_open, 446 .read = seq_read, 447 .llseek = seq_lseek, 448 .release = single_release, 449 }; 450 451 static int __init create_debugfs_nodes(void) 452 { 453 struct dentry *d, *pfn, *decay, *count, *array; 454 455 d = debugfs_create_dir("cec", ras_debugfs_dir); 456 if (!d) { 457 pr_warn("Error creating cec debugfs node!\n"); 458 return -1; 459 } 460 461 pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops); 462 if (!pfn) { 463 pr_warn("Error creating pfn debugfs node!\n"); 464 goto err; 465 } 466 467 array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops); 468 if (!array) { 469 pr_warn("Error creating array debugfs node!\n"); 470 goto err; 471 } 472 473 decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d, 474 &decay_interval, &decay_interval_ops); 475 if (!decay) { 476 pr_warn("Error creating decay_interval debugfs node!\n"); 477 goto err; 478 } 479 480 count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d, 481 &count_threshold, &count_threshold_ops); 482 if (!count) { 483 pr_warn("Error creating count_threshold debugfs node!\n"); 484 goto err; 485 } 486 487 488 return 0; 489 490 err: 491 debugfs_remove_recursive(d); 492 493 return 1; 494 } 495 496 void __init cec_init(void) 497 { 498 if (ce_arr.disabled) 499 return; 500 501 ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL); 502 if (!ce_arr.array) { 503 pr_err("Error allocating CE array page!\n"); 504 return; 505 } 506 507 if (create_debugfs_nodes()) { 508 free_page((unsigned long)ce_arr.array); 509 return; 510 } 511 512 INIT_DELAYED_WORK(&cec_work, cec_work_fn); 513 schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL); 514 515 pr_info("Correctable Errors collector initialized.\n"); 516 } 517 518 int __init parse_cec_param(char *str) 519 { 520 if (!str) 521 return 0; 522 523 if (*str == '=') 524 str++; 525 526 if (!strcmp(str, "cec_disable")) 527 ce_arr.disabled = 1; 528 else 529 return 0; 530 531 return 1; 532 } 533