1*540adea3SMasami Hiramatsu // SPDX-License-Identifier: GPL-2.0 2*540adea3SMasami Hiramatsu // error-inject.c: Function-level error injection table 3*540adea3SMasami Hiramatsu #include <linux/error-injection.h> 4*540adea3SMasami Hiramatsu #include <linux/debugfs.h> 5*540adea3SMasami Hiramatsu #include <linux/kallsyms.h> 6*540adea3SMasami Hiramatsu #include <linux/kprobes.h> 7*540adea3SMasami Hiramatsu #include <linux/module.h> 8*540adea3SMasami Hiramatsu #include <linux/mutex.h> 9*540adea3SMasami Hiramatsu #include <linux/list.h> 10*540adea3SMasami Hiramatsu #include <linux/slab.h> 11*540adea3SMasami Hiramatsu 12*540adea3SMasami Hiramatsu /* Whitelist of symbols that can be overridden for error injection. */ 13*540adea3SMasami Hiramatsu static LIST_HEAD(error_injection_list); 14*540adea3SMasami Hiramatsu static DEFINE_MUTEX(ei_mutex); 15*540adea3SMasami Hiramatsu struct ei_entry { 16*540adea3SMasami Hiramatsu struct list_head list; 17*540adea3SMasami Hiramatsu unsigned long start_addr; 18*540adea3SMasami Hiramatsu unsigned long end_addr; 19*540adea3SMasami Hiramatsu void *priv; 20*540adea3SMasami Hiramatsu }; 21*540adea3SMasami Hiramatsu 22*540adea3SMasami Hiramatsu bool within_error_injection_list(unsigned long addr) 23*540adea3SMasami Hiramatsu { 24*540adea3SMasami Hiramatsu struct ei_entry *ent; 25*540adea3SMasami Hiramatsu bool ret = false; 26*540adea3SMasami Hiramatsu 27*540adea3SMasami Hiramatsu mutex_lock(&ei_mutex); 28*540adea3SMasami Hiramatsu list_for_each_entry(ent, &error_injection_list, list) { 29*540adea3SMasami Hiramatsu if (addr >= ent->start_addr && addr < ent->end_addr) { 30*540adea3SMasami Hiramatsu ret = true; 31*540adea3SMasami Hiramatsu break; 32*540adea3SMasami Hiramatsu } 33*540adea3SMasami Hiramatsu } 34*540adea3SMasami Hiramatsu mutex_unlock(&ei_mutex); 35*540adea3SMasami Hiramatsu return ret; 36*540adea3SMasami Hiramatsu } 37*540adea3SMasami Hiramatsu 38*540adea3SMasami Hiramatsu /* 39*540adea3SMasami Hiramatsu * Lookup and populate the error_injection_list. 40*540adea3SMasami Hiramatsu * 41*540adea3SMasami Hiramatsu * For safety reasons we only allow certain functions to be overridden with 42*540adea3SMasami Hiramatsu * bpf_error_injection, so we need to populate the list of the symbols that have 43*540adea3SMasami Hiramatsu * been marked as safe for overriding. 44*540adea3SMasami Hiramatsu */ 45*540adea3SMasami Hiramatsu static void populate_error_injection_list(unsigned long *start, 46*540adea3SMasami Hiramatsu unsigned long *end, void *priv) 47*540adea3SMasami Hiramatsu { 48*540adea3SMasami Hiramatsu unsigned long *iter; 49*540adea3SMasami Hiramatsu struct ei_entry *ent; 50*540adea3SMasami Hiramatsu unsigned long entry, offset = 0, size = 0; 51*540adea3SMasami Hiramatsu 52*540adea3SMasami Hiramatsu mutex_lock(&ei_mutex); 53*540adea3SMasami Hiramatsu for (iter = start; iter < end; iter++) { 54*540adea3SMasami Hiramatsu entry = arch_deref_entry_point((void *)*iter); 55*540adea3SMasami Hiramatsu 56*540adea3SMasami Hiramatsu if (!kernel_text_address(entry) || 57*540adea3SMasami Hiramatsu !kallsyms_lookup_size_offset(entry, &size, &offset)) { 58*540adea3SMasami Hiramatsu pr_err("Failed to find error inject entry at %p\n", 59*540adea3SMasami Hiramatsu (void *)entry); 60*540adea3SMasami Hiramatsu continue; 61*540adea3SMasami Hiramatsu } 62*540adea3SMasami Hiramatsu 63*540adea3SMasami Hiramatsu ent = kmalloc(sizeof(*ent), GFP_KERNEL); 64*540adea3SMasami Hiramatsu if (!ent) 65*540adea3SMasami Hiramatsu break; 66*540adea3SMasami Hiramatsu ent->start_addr = entry; 67*540adea3SMasami Hiramatsu ent->end_addr = entry + size; 68*540adea3SMasami Hiramatsu ent->priv = priv; 69*540adea3SMasami Hiramatsu INIT_LIST_HEAD(&ent->list); 70*540adea3SMasami Hiramatsu list_add_tail(&ent->list, &error_injection_list); 71*540adea3SMasami Hiramatsu } 72*540adea3SMasami Hiramatsu mutex_unlock(&ei_mutex); 73*540adea3SMasami Hiramatsu } 74*540adea3SMasami Hiramatsu 75*540adea3SMasami Hiramatsu /* Markers of the _error_inject_whitelist section */ 76*540adea3SMasami Hiramatsu extern unsigned long __start_error_injection_whitelist[]; 77*540adea3SMasami Hiramatsu extern unsigned long __stop_error_injection_whitelist[]; 78*540adea3SMasami Hiramatsu 79*540adea3SMasami Hiramatsu static void __init populate_kernel_ei_list(void) 80*540adea3SMasami Hiramatsu { 81*540adea3SMasami Hiramatsu populate_error_injection_list(__start_error_injection_whitelist, 82*540adea3SMasami Hiramatsu __stop_error_injection_whitelist, 83*540adea3SMasami Hiramatsu NULL); 84*540adea3SMasami Hiramatsu } 85*540adea3SMasami Hiramatsu 86*540adea3SMasami Hiramatsu #ifdef CONFIG_MODULES 87*540adea3SMasami Hiramatsu static void module_load_ei_list(struct module *mod) 88*540adea3SMasami Hiramatsu { 89*540adea3SMasami Hiramatsu if (!mod->num_ei_funcs) 90*540adea3SMasami Hiramatsu return; 91*540adea3SMasami Hiramatsu 92*540adea3SMasami Hiramatsu populate_error_injection_list(mod->ei_funcs, 93*540adea3SMasami Hiramatsu mod->ei_funcs + mod->num_ei_funcs, mod); 94*540adea3SMasami Hiramatsu } 95*540adea3SMasami Hiramatsu 96*540adea3SMasami Hiramatsu static void module_unload_ei_list(struct module *mod) 97*540adea3SMasami Hiramatsu { 98*540adea3SMasami Hiramatsu struct ei_entry *ent, *n; 99*540adea3SMasami Hiramatsu 100*540adea3SMasami Hiramatsu if (!mod->num_ei_funcs) 101*540adea3SMasami Hiramatsu return; 102*540adea3SMasami Hiramatsu 103*540adea3SMasami Hiramatsu mutex_lock(&ei_mutex); 104*540adea3SMasami Hiramatsu list_for_each_entry_safe(ent, n, &error_injection_list, list) { 105*540adea3SMasami Hiramatsu if (ent->priv == mod) { 106*540adea3SMasami Hiramatsu list_del_init(&ent->list); 107*540adea3SMasami Hiramatsu kfree(ent); 108*540adea3SMasami Hiramatsu } 109*540adea3SMasami Hiramatsu } 110*540adea3SMasami Hiramatsu mutex_unlock(&ei_mutex); 111*540adea3SMasami Hiramatsu } 112*540adea3SMasami Hiramatsu 113*540adea3SMasami Hiramatsu /* Module notifier call back, checking error injection table on the module */ 114*540adea3SMasami Hiramatsu static int ei_module_callback(struct notifier_block *nb, 115*540adea3SMasami Hiramatsu unsigned long val, void *data) 116*540adea3SMasami Hiramatsu { 117*540adea3SMasami Hiramatsu struct module *mod = data; 118*540adea3SMasami Hiramatsu 119*540adea3SMasami Hiramatsu if (val == MODULE_STATE_COMING) 120*540adea3SMasami Hiramatsu module_load_ei_list(mod); 121*540adea3SMasami Hiramatsu else if (val == MODULE_STATE_GOING) 122*540adea3SMasami Hiramatsu module_unload_ei_list(mod); 123*540adea3SMasami Hiramatsu 124*540adea3SMasami Hiramatsu return NOTIFY_DONE; 125*540adea3SMasami Hiramatsu } 126*540adea3SMasami Hiramatsu 127*540adea3SMasami Hiramatsu static struct notifier_block ei_module_nb = { 128*540adea3SMasami Hiramatsu .notifier_call = ei_module_callback, 129*540adea3SMasami Hiramatsu .priority = 0 130*540adea3SMasami Hiramatsu }; 131*540adea3SMasami Hiramatsu 132*540adea3SMasami Hiramatsu static __init int module_ei_init(void) 133*540adea3SMasami Hiramatsu { 134*540adea3SMasami Hiramatsu return register_module_notifier(&ei_module_nb); 135*540adea3SMasami Hiramatsu } 136*540adea3SMasami Hiramatsu #else /* !CONFIG_MODULES */ 137*540adea3SMasami Hiramatsu #define module_ei_init() (0) 138*540adea3SMasami Hiramatsu #endif 139*540adea3SMasami Hiramatsu 140*540adea3SMasami Hiramatsu /* 141*540adea3SMasami Hiramatsu * error_injection/whitelist -- shows which functions can be overridden for 142*540adea3SMasami Hiramatsu * error injection. 143*540adea3SMasami Hiramatsu */ 144*540adea3SMasami Hiramatsu static void *ei_seq_start(struct seq_file *m, loff_t *pos) 145*540adea3SMasami Hiramatsu { 146*540adea3SMasami Hiramatsu mutex_lock(&ei_mutex); 147*540adea3SMasami Hiramatsu return seq_list_start(&error_injection_list, *pos); 148*540adea3SMasami Hiramatsu } 149*540adea3SMasami Hiramatsu 150*540adea3SMasami Hiramatsu static void ei_seq_stop(struct seq_file *m, void *v) 151*540adea3SMasami Hiramatsu { 152*540adea3SMasami Hiramatsu mutex_unlock(&ei_mutex); 153*540adea3SMasami Hiramatsu } 154*540adea3SMasami Hiramatsu 155*540adea3SMasami Hiramatsu static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos) 156*540adea3SMasami Hiramatsu { 157*540adea3SMasami Hiramatsu return seq_list_next(v, &error_injection_list, pos); 158*540adea3SMasami Hiramatsu } 159*540adea3SMasami Hiramatsu 160*540adea3SMasami Hiramatsu static int ei_seq_show(struct seq_file *m, void *v) 161*540adea3SMasami Hiramatsu { 162*540adea3SMasami Hiramatsu struct ei_entry *ent = list_entry(v, struct ei_entry, list); 163*540adea3SMasami Hiramatsu 164*540adea3SMasami Hiramatsu seq_printf(m, "%pf\n", (void *)ent->start_addr); 165*540adea3SMasami Hiramatsu return 0; 166*540adea3SMasami Hiramatsu } 167*540adea3SMasami Hiramatsu 168*540adea3SMasami Hiramatsu static const struct seq_operations ei_seq_ops = { 169*540adea3SMasami Hiramatsu .start = ei_seq_start, 170*540adea3SMasami Hiramatsu .next = ei_seq_next, 171*540adea3SMasami Hiramatsu .stop = ei_seq_stop, 172*540adea3SMasami Hiramatsu .show = ei_seq_show, 173*540adea3SMasami Hiramatsu }; 174*540adea3SMasami Hiramatsu 175*540adea3SMasami Hiramatsu static int ei_open(struct inode *inode, struct file *filp) 176*540adea3SMasami Hiramatsu { 177*540adea3SMasami Hiramatsu return seq_open(filp, &ei_seq_ops); 178*540adea3SMasami Hiramatsu } 179*540adea3SMasami Hiramatsu 180*540adea3SMasami Hiramatsu static const struct file_operations debugfs_ei_ops = { 181*540adea3SMasami Hiramatsu .open = ei_open, 182*540adea3SMasami Hiramatsu .read = seq_read, 183*540adea3SMasami Hiramatsu .llseek = seq_lseek, 184*540adea3SMasami Hiramatsu .release = seq_release, 185*540adea3SMasami Hiramatsu }; 186*540adea3SMasami Hiramatsu 187*540adea3SMasami Hiramatsu static int __init ei_debugfs_init(void) 188*540adea3SMasami Hiramatsu { 189*540adea3SMasami Hiramatsu struct dentry *dir, *file; 190*540adea3SMasami Hiramatsu 191*540adea3SMasami Hiramatsu dir = debugfs_create_dir("error_injection", NULL); 192*540adea3SMasami Hiramatsu if (!dir) 193*540adea3SMasami Hiramatsu return -ENOMEM; 194*540adea3SMasami Hiramatsu 195*540adea3SMasami Hiramatsu file = debugfs_create_file("list", 0444, dir, NULL, &debugfs_ei_ops); 196*540adea3SMasami Hiramatsu if (!file) { 197*540adea3SMasami Hiramatsu debugfs_remove(dir); 198*540adea3SMasami Hiramatsu return -ENOMEM; 199*540adea3SMasami Hiramatsu } 200*540adea3SMasami Hiramatsu 201*540adea3SMasami Hiramatsu return 0; 202*540adea3SMasami Hiramatsu } 203*540adea3SMasami Hiramatsu 204*540adea3SMasami Hiramatsu static int __init init_error_injection(void) 205*540adea3SMasami Hiramatsu { 206*540adea3SMasami Hiramatsu populate_kernel_ei_list(); 207*540adea3SMasami Hiramatsu 208*540adea3SMasami Hiramatsu if (!module_ei_init()) 209*540adea3SMasami Hiramatsu ei_debugfs_init(); 210*540adea3SMasami Hiramatsu 211*540adea3SMasami Hiramatsu return 0; 212*540adea3SMasami Hiramatsu } 213*540adea3SMasami Hiramatsu late_initcall(init_error_injection); 214