Back to home page

LXR

 
 

    


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