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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Inject a hwpoison memory failure on a arbitrary pfn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) static struct dentry *hwpoison_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static int hwpoison_inject(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	unsigned long pfn = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct page *hpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	if (!pfn_valid(pfn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	p = pfn_to_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	hpage = compound_head(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (!hwpoison_filter_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		goto inject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	shake_page(hpage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	 * This implies unable to support non-LRU pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (!PageLRU(hpage) && !PageHuge(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 * do a racy check to make sure PG_hwpoison will only be set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 * the targeted owner (or on a free page).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 * memory_failure() will redo the check reliably inside page lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	err = hwpoison_filter(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) inject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	pr_info("Injecting memory failure at pfn %#lx\n", pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return memory_failure(pfn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int hwpoison_unpoison(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return unpoison_memory(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) DEFINE_DEBUGFS_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) DEFINE_DEBUGFS_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static void pfn_inject_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	debugfs_remove_recursive(hwpoison_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int pfn_inject_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
^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) 	 * Note that the below poison/unpoison interfaces do not involve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 * hardware status change, hence do not require hardware support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * They are mainly for testing hwpoison in software level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			    &hwpoison_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			    &unpoison_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	debugfs_create_u32("corrupt-filter-enable", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			   &hwpoison_filter_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	debugfs_create_u32("corrupt-filter-dev-major", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			   &hwpoison_filter_dev_major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	debugfs_create_u32("corrupt-filter-dev-minor", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			   &hwpoison_filter_dev_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	debugfs_create_u64("corrupt-filter-flags-mask", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			   &hwpoison_filter_flags_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	debugfs_create_u64("corrupt-filter-flags-value", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			   &hwpoison_filter_flags_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #ifdef CONFIG_MEMCG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	debugfs_create_u64("corrupt-filter-memcg", 0600, hwpoison_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			   &hwpoison_filter_memcg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) module_init(pfn_inject_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) module_exit(pfn_inject_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) MODULE_LICENSE("GPL");