Back to home page

LXR

 
 

    


0001 #include <linux/kernel.h>
0002 #include <linux/init.h>
0003 #include <linux/random.h>
0004 #include <linux/sched.h>
0005 #include <linux/stat.h>
0006 #include <linux/types.h>
0007 #include <linux/fs.h>
0008 #include <linux/export.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/stacktrace.h>
0011 #include <linux/fault-inject.h>
0012 
0013 /*
0014  * setup_fault_attr() is a helper function for various __setup handlers, so it
0015  * returns 0 on error, because that is what __setup handlers do.
0016  */
0017 int setup_fault_attr(struct fault_attr *attr, char *str)
0018 {
0019     unsigned long probability;
0020     unsigned long interval;
0021     int times;
0022     int space;
0023 
0024     /* "<interval>,<probability>,<space>,<times>" */
0025     if (sscanf(str, "%lu,%lu,%d,%d",
0026             &interval, &probability, &space, &times) < 4) {
0027         printk(KERN_WARNING
0028             "FAULT_INJECTION: failed to parse arguments\n");
0029         return 0;
0030     }
0031 
0032     attr->probability = probability;
0033     attr->interval = interval;
0034     atomic_set(&attr->times, times);
0035     atomic_set(&attr->space, space);
0036 
0037     return 1;
0038 }
0039 EXPORT_SYMBOL_GPL(setup_fault_attr);
0040 
0041 static void fail_dump(struct fault_attr *attr)
0042 {
0043     if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {
0044         printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
0045                "name %pd, interval %lu, probability %lu, "
0046                "space %d, times %d\n", attr->dname,
0047                attr->interval, attr->probability,
0048                atomic_read(&attr->space),
0049                atomic_read(&attr->times));
0050         if (attr->verbose > 1)
0051             dump_stack();
0052     }
0053 }
0054 
0055 #define atomic_dec_not_zero(v)      atomic_add_unless((v), -1, 0)
0056 
0057 static bool fail_task(struct fault_attr *attr, struct task_struct *task)
0058 {
0059     return !in_interrupt() && task->make_it_fail;
0060 }
0061 
0062 #define MAX_STACK_TRACE_DEPTH 32
0063 
0064 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
0065 
0066 static bool fail_stacktrace(struct fault_attr *attr)
0067 {
0068     struct stack_trace trace;
0069     int depth = attr->stacktrace_depth;
0070     unsigned long entries[MAX_STACK_TRACE_DEPTH];
0071     int n;
0072     bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
0073 
0074     if (depth == 0)
0075         return found;
0076 
0077     trace.nr_entries = 0;
0078     trace.entries = entries;
0079     trace.max_entries = depth;
0080     trace.skip = 1;
0081 
0082     save_stack_trace(&trace);
0083     for (n = 0; n < trace.nr_entries; n++) {
0084         if (attr->reject_start <= entries[n] &&
0085                    entries[n] < attr->reject_end)
0086             return false;
0087         if (attr->require_start <= entries[n] &&
0088                    entries[n] < attr->require_end)
0089             found = true;
0090     }
0091     return found;
0092 }
0093 
0094 #else
0095 
0096 static inline bool fail_stacktrace(struct fault_attr *attr)
0097 {
0098     return true;
0099 }
0100 
0101 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
0102 
0103 /*
0104  * This code is stolen from failmalloc-1.0
0105  * http://www.nongnu.org/failmalloc/
0106  */
0107 
0108 bool should_fail(struct fault_attr *attr, ssize_t size)
0109 {
0110     /* No need to check any other properties if the probability is 0 */
0111     if (attr->probability == 0)
0112         return false;
0113 
0114     if (attr->task_filter && !fail_task(attr, current))
0115         return false;
0116 
0117     if (atomic_read(&attr->times) == 0)
0118         return false;
0119 
0120     if (atomic_read(&attr->space) > size) {
0121         atomic_sub(size, &attr->space);
0122         return false;
0123     }
0124 
0125     if (attr->interval > 1) {
0126         attr->count++;
0127         if (attr->count % attr->interval)
0128             return false;
0129     }
0130 
0131     if (attr->probability <= prandom_u32() % 100)
0132         return false;
0133 
0134     if (!fail_stacktrace(attr))
0135         return false;
0136 
0137     fail_dump(attr);
0138 
0139     if (atomic_read(&attr->times) != -1)
0140         atomic_dec_not_zero(&attr->times);
0141 
0142     return true;
0143 }
0144 EXPORT_SYMBOL_GPL(should_fail);
0145 
0146 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
0147 
0148 static int debugfs_ul_set(void *data, u64 val)
0149 {
0150     *(unsigned long *)data = val;
0151     return 0;
0152 }
0153 
0154 static int debugfs_ul_get(void *data, u64 *val)
0155 {
0156     *val = *(unsigned long *)data;
0157     return 0;
0158 }
0159 
0160 DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
0161 
0162 static struct dentry *debugfs_create_ul(const char *name, umode_t mode,
0163                 struct dentry *parent, unsigned long *value)
0164 {
0165     return debugfs_create_file(name, mode, parent, value, &fops_ul);
0166 }
0167 
0168 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
0169 
0170 static int debugfs_stacktrace_depth_set(void *data, u64 val)
0171 {
0172     *(unsigned long *)data =
0173         min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
0174 
0175     return 0;
0176 }
0177 
0178 DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
0179             debugfs_stacktrace_depth_set, "%llu\n");
0180 
0181 static struct dentry *debugfs_create_stacktrace_depth(
0182     const char *name, umode_t mode,
0183     struct dentry *parent, unsigned long *value)
0184 {
0185     return debugfs_create_file(name, mode, parent, value,
0186                    &fops_stacktrace_depth);
0187 }
0188 
0189 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
0190 
0191 struct dentry *fault_create_debugfs_attr(const char *name,
0192             struct dentry *parent, struct fault_attr *attr)
0193 {
0194     umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
0195     struct dentry *dir;
0196 
0197     dir = debugfs_create_dir(name, parent);
0198     if (!dir)
0199         return ERR_PTR(-ENOMEM);
0200 
0201     if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
0202         goto fail;
0203     if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
0204         goto fail;
0205     if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
0206         goto fail;
0207     if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
0208         goto fail;
0209     if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
0210         goto fail;
0211     if (!debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir,
0212                 &attr->ratelimit_state.interval))
0213         goto fail;
0214     if (!debugfs_create_u32("verbose_ratelimit_burst", mode, dir,
0215                 &attr->ratelimit_state.burst))
0216         goto fail;
0217     if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
0218         goto fail;
0219 
0220 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
0221 
0222     if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
0223                 &attr->stacktrace_depth))
0224         goto fail;
0225     if (!debugfs_create_ul("require-start", mode, dir,
0226                 &attr->require_start))
0227         goto fail;
0228     if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
0229         goto fail;
0230     if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
0231         goto fail;
0232     if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
0233         goto fail;
0234 
0235 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
0236 
0237     attr->dname = dget(dir);
0238     return dir;
0239 fail:
0240     debugfs_remove_recursive(dir);
0241 
0242     return ERR_PTR(-ENOMEM);
0243 }
0244 EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
0245 
0246 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */