fault-inject.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/random.h>
  4. #include <linux/sched.h>
  5. #include <linux/stat.h>
  6. #include <linux/types.h>
  7. #include <linux/fs.h>
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/stacktrace.h>
  11. #include <linux/fault-inject.h>
  12. /*
  13. * setup_fault_attr() is a helper function for various __setup handlers, so it
  14. * returns 0 on error, because that is what __setup handlers do.
  15. */
  16. int __init setup_fault_attr(struct fault_attr *attr, char *str)
  17. {
  18. unsigned long probability;
  19. unsigned long interval;
  20. int times;
  21. int space;
  22. /* "<interval>,<probability>,<space>,<times>" */
  23. if (sscanf(str, "%lu,%lu,%d,%d",
  24. &interval, &probability, &space, &times) < 4) {
  25. printk(KERN_WARNING
  26. "FAULT_INJECTION: failed to parse arguments\n");
  27. return 0;
  28. }
  29. attr->probability = probability;
  30. attr->interval = interval;
  31. atomic_set(&attr->times, times);
  32. atomic_set(&attr->space, space);
  33. return 1;
  34. }
  35. static void fail_dump(struct fault_attr *attr)
  36. {
  37. if (attr->verbose > 0)
  38. printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure\n");
  39. if (attr->verbose > 1)
  40. dump_stack();
  41. }
  42. #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
  43. static bool fail_task(struct fault_attr *attr, struct task_struct *task)
  44. {
  45. return !in_interrupt() && task->make_it_fail;
  46. }
  47. #define MAX_STACK_TRACE_DEPTH 32
  48. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  49. static bool fail_stacktrace(struct fault_attr *attr)
  50. {
  51. struct stack_trace trace;
  52. int depth = attr->stacktrace_depth;
  53. unsigned long entries[MAX_STACK_TRACE_DEPTH];
  54. int n;
  55. bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
  56. if (depth == 0)
  57. return found;
  58. trace.nr_entries = 0;
  59. trace.entries = entries;
  60. trace.max_entries = depth;
  61. trace.skip = 1;
  62. save_stack_trace(&trace);
  63. for (n = 0; n < trace.nr_entries; n++) {
  64. if (attr->reject_start <= entries[n] &&
  65. entries[n] < attr->reject_end)
  66. return false;
  67. if (attr->require_start <= entries[n] &&
  68. entries[n] < attr->require_end)
  69. found = true;
  70. }
  71. return found;
  72. }
  73. #else
  74. static inline bool fail_stacktrace(struct fault_attr *attr)
  75. {
  76. return true;
  77. }
  78. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  79. /*
  80. * This code is stolen from failmalloc-1.0
  81. * http://www.nongnu.org/failmalloc/
  82. */
  83. bool should_fail(struct fault_attr *attr, ssize_t size)
  84. {
  85. if (attr->task_filter && !fail_task(attr, current))
  86. return false;
  87. if (atomic_read(&attr->times) == 0)
  88. return false;
  89. if (atomic_read(&attr->space) > size) {
  90. atomic_sub(size, &attr->space);
  91. return false;
  92. }
  93. if (attr->interval > 1) {
  94. attr->count++;
  95. if (attr->count % attr->interval)
  96. return false;
  97. }
  98. if (attr->probability <= random32() % 100)
  99. return false;
  100. if (!fail_stacktrace(attr))
  101. return false;
  102. fail_dump(attr);
  103. if (atomic_read(&attr->times) != -1)
  104. atomic_dec_not_zero(&attr->times);
  105. return true;
  106. }
  107. EXPORT_SYMBOL_GPL(should_fail);
  108. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  109. static int debugfs_ul_set(void *data, u64 val)
  110. {
  111. *(unsigned long *)data = val;
  112. return 0;
  113. }
  114. static int debugfs_ul_get(void *data, u64 *val)
  115. {
  116. *val = *(unsigned long *)data;
  117. return 0;
  118. }
  119. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  120. static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
  121. struct dentry *parent, unsigned long *value)
  122. {
  123. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  124. }
  125. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  126. static int debugfs_stacktrace_depth_set(void *data, u64 val)
  127. {
  128. *(unsigned long *)data =
  129. min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
  130. return 0;
  131. }
  132. DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
  133. debugfs_stacktrace_depth_set, "%llu\n");
  134. static struct dentry *debugfs_create_stacktrace_depth(
  135. const char *name, mode_t mode,
  136. struct dentry *parent, unsigned long *value)
  137. {
  138. return debugfs_create_file(name, mode, parent, value,
  139. &fops_stacktrace_depth);
  140. }
  141. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  142. static int debugfs_atomic_t_set(void *data, u64 val)
  143. {
  144. atomic_set((atomic_t *)data, val);
  145. return 0;
  146. }
  147. static int debugfs_atomic_t_get(void *data, u64 *val)
  148. {
  149. *val = atomic_read((atomic_t *)data);
  150. return 0;
  151. }
  152. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  153. debugfs_atomic_t_set, "%lld\n");
  154. static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
  155. struct dentry *parent, atomic_t *value)
  156. {
  157. return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
  158. }
  159. struct dentry *fault_create_debugfs_attr(const char *name,
  160. struct dentry *parent, struct fault_attr *attr)
  161. {
  162. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  163. struct dentry *dir;
  164. dir = debugfs_create_dir(name, parent);
  165. if (!dir)
  166. return ERR_PTR(-ENOMEM);
  167. if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
  168. goto fail;
  169. if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
  170. goto fail;
  171. if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
  172. goto fail;
  173. if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
  174. goto fail;
  175. if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
  176. goto fail;
  177. if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
  178. goto fail;
  179. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  180. if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
  181. &attr->stacktrace_depth))
  182. goto fail;
  183. if (!debugfs_create_ul("require-start", mode, dir,
  184. &attr->require_start))
  185. goto fail;
  186. if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
  187. goto fail;
  188. if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
  189. goto fail;
  190. if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
  191. goto fail;
  192. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  193. return dir;
  194. fail:
  195. debugfs_remove_recursive(dir);
  196. return ERR_PTR(-ENOMEM);
  197. }
  198. EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
  199. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */