^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fail_function.c: Function-based error injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/error-injection.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/fault-inject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static int fei_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static void fei_post_handler(struct kprobe *kp, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * A dummy post handler is required to prohibit optimizing, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * jump optimization does not support execution path overriding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct fei_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct kprobe kp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static DEFINE_MUTEX(fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static LIST_HEAD(fei_attr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static DECLARE_FAULT_ATTR(fei_fault_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct dentry *fei_debugfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static unsigned long adjust_error_retval(unsigned long addr, unsigned long retv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) switch (get_injectable_error_type(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case EI_ETYPE_NULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (retv != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case EI_ETYPE_ERRNO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (retv < (unsigned long)-MAX_ERRNO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return (unsigned long)-EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case EI_ETYPE_ERRNO_NULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (retv != 0 && retv < (unsigned long)-MAX_ERRNO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return (unsigned long)-EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return retv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static struct fei_attr *fei_attr_new(const char *sym, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct fei_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) attr = kzalloc(sizeof(*attr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) attr->kp.symbol_name = kstrdup(sym, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!attr->kp.symbol_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) attr->kp.pre_handler = fei_kprobe_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) attr->kp.post_handler = fei_post_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) attr->retval = adjust_error_retval(addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) INIT_LIST_HEAD(&attr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return attr;
^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) static void fei_attr_free(struct fei_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) kfree(attr->kp.symbol_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) kfree(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static struct fei_attr *fei_attr_lookup(const char *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct fei_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) list_for_each_entry(attr, &fei_attr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!strcmp(attr->kp.symbol_name, sym))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static bool fei_attr_is_valid(struct fei_attr *_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct fei_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) list_for_each_entry(attr, &fei_attr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (attr == _attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int fei_retval_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct fei_attr *attr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned long retv = (unsigned long)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) mutex_lock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * Since this operation can be done after retval file is removed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * It is safer to check the attr is still valid before accessing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * its member.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!fei_attr_is_valid(attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (attr->kp.addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (adjust_error_retval((unsigned long)attr->kp.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) val) != retv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) attr->retval = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) mutex_unlock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int fei_retval_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct fei_attr *attr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mutex_lock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Here we also validate @attr to ensure it still exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!fei_attr_is_valid(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *val = attr->retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) mutex_unlock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) "%llx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void fei_debugfs_add_attr(struct fei_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void fei_debugfs_remove_attr(struct fei_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) dir = debugfs_lookup(attr->kp.symbol_name, fei_debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) debugfs_remove_recursive(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int fei_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct fei_attr *attr = container_of(kp, struct fei_attr, kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (should_fail(&fei_fault_attr, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) regs_set_return_value(regs, attr->retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) override_function_with_return(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) NOKPROBE_SYMBOL(fei_kprobe_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void *fei_seq_start(struct seq_file *m, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) mutex_lock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return seq_list_start(&fei_attr_list, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void fei_seq_stop(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) mutex_unlock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void *fei_seq_next(struct seq_file *m, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return seq_list_next(v, &fei_attr_list, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int fei_seq_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct fei_attr *attr = list_entry(v, struct fei_attr, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) seq_printf(m, "%ps\n", attr->kp.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static const struct seq_operations fei_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .start = fei_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .next = fei_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .stop = fei_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .show = fei_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int fei_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return seq_open(file, &fei_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void fei_attr_remove(struct fei_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) fei_debugfs_remove_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unregister_kprobe(&attr->kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) list_del(&attr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) fei_attr_free(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static void fei_attr_remove_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct fei_attr *attr, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) list_for_each_entry_safe(attr, n, &fei_attr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) fei_attr_remove(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static ssize_t fei_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct fei_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) char *buf, *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* cut off if it is too long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (count > KSYM_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) count = KSYM_NAME_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) buf = kmalloc(count + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (copy_from_user(buf, buffer, count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) buf[count] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) sym = strstrip(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) mutex_lock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Writing just spaces will remove all injection points */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (sym[0] == '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) fei_attr_remove_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* Writing !function will remove one injection point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (sym[0] == '!') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) attr = fei_attr_lookup(sym + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) fei_attr_remove(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) addr = kallsyms_lookup_name(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!within_error_injection_list(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (fei_attr_lookup(sym)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) attr = fei_attr_new(sym, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = register_kprobe(&attr->kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) fei_debugfs_add_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fei_attr_remove(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) list_add_tail(&attr->list, &fei_attr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) mutex_unlock(&fei_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static const struct file_operations fei_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .open = fei_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .write = fei_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int __init fei_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) dir = fault_create_debugfs_attr("fail_function", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) &fei_fault_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (IS_ERR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return PTR_ERR(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* injectable attribute is just a symlink of error_inject/list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) debugfs_create_symlink("injectable", dir, "../error_injection/list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) debugfs_create_file("inject", 0600, dir, NULL, &fei_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) fei_debugfs_dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) late_initcall(fei_debugfs_init);