Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Inject a hwpoison memory failure on a arbitrary pfn */
0003 #include <linux/module.h>
0004 #include <linux/debugfs.h>
0005 #include <linux/kernel.h>
0006 #include <linux/mm.h>
0007 #include <linux/swap.h>
0008 #include <linux/pagemap.h>
0009 #include <linux/hugetlb.h>
0010 #include "internal.h"
0011 
0012 static struct dentry *hwpoison_dir;
0013 
0014 static int hwpoison_inject(void *data, u64 val)
0015 {
0016     unsigned long pfn = val;
0017     struct page *p;
0018     struct page *hpage;
0019     int err;
0020 
0021     if (!capable(CAP_SYS_ADMIN))
0022         return -EPERM;
0023 
0024     if (!pfn_valid(pfn))
0025         return -ENXIO;
0026 
0027     p = pfn_to_page(pfn);
0028     hpage = compound_head(p);
0029 
0030     if (!hwpoison_filter_enable)
0031         goto inject;
0032 
0033     shake_page(hpage);
0034     /*
0035      * This implies unable to support non-LRU pages except free page.
0036      */
0037     if (!PageLRU(hpage) && !PageHuge(p) && !is_free_buddy_page(p))
0038         return 0;
0039 
0040     /*
0041      * do a racy check to make sure PG_hwpoison will only be set for
0042      * the targeted owner (or on a free page).
0043      * memory_failure() will redo the check reliably inside page lock.
0044      */
0045     err = hwpoison_filter(hpage);
0046     if (err)
0047         return 0;
0048 
0049 inject:
0050     pr_info("Injecting memory failure at pfn %#lx\n", pfn);
0051     err = memory_failure(pfn, MF_SW_SIMULATED);
0052     return (err == -EOPNOTSUPP) ? 0 : err;
0053 }
0054 
0055 static int hwpoison_unpoison(void *data, u64 val)
0056 {
0057     if (!capable(CAP_SYS_ADMIN))
0058         return -EPERM;
0059 
0060     return unpoison_memory(val);
0061 }
0062 
0063 DEFINE_DEBUGFS_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
0064 DEFINE_DEBUGFS_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
0065 
0066 static void pfn_inject_exit(void)
0067 {
0068     hwpoison_filter_enable = 0;
0069     debugfs_remove_recursive(hwpoison_dir);
0070 }
0071 
0072 static int pfn_inject_init(void)
0073 {
0074     hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
0075 
0076     /*
0077      * Note that the below poison/unpoison interfaces do not involve
0078      * hardware status change, hence do not require hardware support.
0079      * They are mainly for testing hwpoison in software level.
0080      */
0081     debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir, NULL,
0082                 &hwpoison_fops);
0083 
0084     debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir, NULL,
0085                 &unpoison_fops);
0086 
0087     debugfs_create_u32("corrupt-filter-enable", 0600, hwpoison_dir,
0088                &hwpoison_filter_enable);
0089 
0090     debugfs_create_u32("corrupt-filter-dev-major", 0600, hwpoison_dir,
0091                &hwpoison_filter_dev_major);
0092 
0093     debugfs_create_u32("corrupt-filter-dev-minor", 0600, hwpoison_dir,
0094                &hwpoison_filter_dev_minor);
0095 
0096     debugfs_create_u64("corrupt-filter-flags-mask", 0600, hwpoison_dir,
0097                &hwpoison_filter_flags_mask);
0098 
0099     debugfs_create_u64("corrupt-filter-flags-value", 0600, hwpoison_dir,
0100                &hwpoison_filter_flags_value);
0101 
0102 #ifdef CONFIG_MEMCG
0103     debugfs_create_u64("corrupt-filter-memcg", 0600, hwpoison_dir,
0104                &hwpoison_filter_memcg);
0105 #endif
0106 
0107     return 0;
0108 }
0109 
0110 module_init(pfn_inject_init);
0111 module_exit(pfn_inject_exit);
0112 MODULE_LICENSE("GPL");