fault-inject.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/kallsyms.h>
  12. #include <linux/fault-inject.h>
  13. /*
  14. * setup_fault_attr() is a helper function for various __setup handlers, so it
  15. * returns 0 on error, because that is what __setup handlers do.
  16. */
  17. int __init setup_fault_attr(struct fault_attr *attr, char *str)
  18. {
  19. unsigned long probability;
  20. unsigned long interval;
  21. int times;
  22. int space;
  23. /* "<interval>,<probability>,<space>,<times>" */
  24. if (sscanf(str, "%lu,%lu,%d,%d",
  25. &interval, &probability, &space, &times) < 4) {
  26. printk(KERN_WARNING
  27. "FAULT_INJECTION: failed to parse arguments\n");
  28. return 0;
  29. }
  30. attr->probability = probability;
  31. attr->interval = interval;
  32. atomic_set(&attr->times, times);
  33. atomic_set(&attr->space, space);
  34. return 1;
  35. }
  36. static void fail_dump(struct fault_attr *attr)
  37. {
  38. if (attr->verbose > 0)
  39. printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure\n");
  40. if (attr->verbose > 1)
  41. dump_stack();
  42. }
  43. #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
  44. static bool fail_task(struct fault_attr *attr, struct task_struct *task)
  45. {
  46. return !in_interrupt() && task->make_it_fail;
  47. }
  48. #define MAX_STACK_TRACE_DEPTH 32
  49. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  50. static bool fail_stacktrace(struct fault_attr *attr)
  51. {
  52. struct stack_trace trace;
  53. int depth = attr->stacktrace_depth;
  54. unsigned long entries[MAX_STACK_TRACE_DEPTH];
  55. int n;
  56. bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
  57. if (depth == 0)
  58. return found;
  59. trace.nr_entries = 0;
  60. trace.entries = entries;
  61. trace.max_entries = depth;
  62. trace.skip = 1;
  63. save_stack_trace(&trace);
  64. for (n = 0; n < trace.nr_entries; n++) {
  65. if (attr->reject_start <= entries[n] &&
  66. entries[n] < attr->reject_end)
  67. return false;
  68. if (attr->require_start <= entries[n] &&
  69. entries[n] < attr->require_end)
  70. found = true;
  71. }
  72. return found;
  73. }
  74. #else
  75. static inline bool fail_stacktrace(struct fault_attr *attr)
  76. {
  77. return true;
  78. }
  79. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  80. /*
  81. * This code is stolen from failmalloc-1.0
  82. * http://www.nongnu.org/failmalloc/
  83. */
  84. bool should_fail(struct fault_attr *attr, ssize_t size)
  85. {
  86. if (attr->task_filter && !fail_task(attr, current))
  87. return false;
  88. if (atomic_read(&attr->times) == 0)
  89. return false;
  90. if (atomic_read(&attr->space) > size) {
  91. atomic_sub(size, &attr->space);
  92. return false;
  93. }
  94. if (attr->interval > 1) {
  95. attr->count++;
  96. if (attr->count % attr->interval)
  97. return false;
  98. }
  99. if (attr->probability <= random32() % 100)
  100. return false;
  101. if (!fail_stacktrace(attr))
  102. return false;
  103. fail_dump(attr);
  104. if (atomic_read(&attr->times) != -1)
  105. atomic_dec_not_zero(&attr->times);
  106. return true;
  107. }
  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. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  115. static int debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data, u64 val)
  116. {
  117. *(unsigned long *)data =
  118. val < MAX_STACK_TRACE_DEPTH ?
  119. val : MAX_STACK_TRACE_DEPTH;
  120. return 0;
  121. }
  122. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  123. static int debugfs_ul_get(void *data, u64 *val)
  124. {
  125. *val = *(unsigned long *)data;
  126. return 0;
  127. }
  128. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  129. static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
  130. struct dentry *parent, unsigned long *value)
  131. {
  132. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  133. }
  134. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  135. DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH, debugfs_ul_get,
  136. debugfs_ul_set_MAX_STACK_TRACE_DEPTH, "%llu\n");
  137. static struct dentry *debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
  138. const char *name, mode_t mode,
  139. struct dentry *parent, unsigned long *value)
  140. {
  141. return debugfs_create_file(name, mode, parent, value,
  142. &fops_ul_MAX_STACK_TRACE_DEPTH);
  143. }
  144. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  145. static int debugfs_atomic_t_set(void *data, u64 val)
  146. {
  147. atomic_set((atomic_t *)data, val);
  148. return 0;
  149. }
  150. static int debugfs_atomic_t_get(void *data, u64 *val)
  151. {
  152. *val = atomic_read((atomic_t *)data);
  153. return 0;
  154. }
  155. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  156. debugfs_atomic_t_set, "%lld\n");
  157. static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
  158. struct dentry *parent, atomic_t *value)
  159. {
  160. return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
  161. }
  162. void cleanup_fault_attr_dentries(struct fault_attr *attr)
  163. {
  164. debugfs_remove(attr->dentries.probability_file);
  165. attr->dentries.probability_file = NULL;
  166. debugfs_remove(attr->dentries.interval_file);
  167. attr->dentries.interval_file = NULL;
  168. debugfs_remove(attr->dentries.times_file);
  169. attr->dentries.times_file = NULL;
  170. debugfs_remove(attr->dentries.space_file);
  171. attr->dentries.space_file = NULL;
  172. debugfs_remove(attr->dentries.verbose_file);
  173. attr->dentries.verbose_file = NULL;
  174. debugfs_remove(attr->dentries.task_filter_file);
  175. attr->dentries.task_filter_file = NULL;
  176. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  177. debugfs_remove(attr->dentries.stacktrace_depth_file);
  178. attr->dentries.stacktrace_depth_file = NULL;
  179. debugfs_remove(attr->dentries.require_start_file);
  180. attr->dentries.require_start_file = NULL;
  181. debugfs_remove(attr->dentries.require_end_file);
  182. attr->dentries.require_end_file = NULL;
  183. debugfs_remove(attr->dentries.reject_start_file);
  184. attr->dentries.reject_start_file = NULL;
  185. debugfs_remove(attr->dentries.reject_end_file);
  186. attr->dentries.reject_end_file = NULL;
  187. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  188. if (attr->dentries.dir)
  189. WARN_ON(!simple_empty(attr->dentries.dir));
  190. debugfs_remove(attr->dentries.dir);
  191. attr->dentries.dir = NULL;
  192. }
  193. int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
  194. {
  195. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  196. struct dentry *dir;
  197. memset(&attr->dentries, 0, sizeof(attr->dentries));
  198. dir = debugfs_create_dir(name, NULL);
  199. if (!dir)
  200. goto fail;
  201. attr->dentries.dir = dir;
  202. attr->dentries.probability_file =
  203. debugfs_create_ul("probability", mode, dir, &attr->probability);
  204. attr->dentries.interval_file =
  205. debugfs_create_ul("interval", mode, dir, &attr->interval);
  206. attr->dentries.times_file =
  207. debugfs_create_atomic_t("times", mode, dir, &attr->times);
  208. attr->dentries.space_file =
  209. debugfs_create_atomic_t("space", mode, dir, &attr->space);
  210. attr->dentries.verbose_file =
  211. debugfs_create_ul("verbose", mode, dir, &attr->verbose);
  212. attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
  213. mode, dir, &attr->task_filter);
  214. if (!attr->dentries.probability_file || !attr->dentries.interval_file ||
  215. !attr->dentries.times_file || !attr->dentries.space_file ||
  216. !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
  217. goto fail;
  218. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  219. attr->dentries.stacktrace_depth_file =
  220. debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
  221. "stacktrace-depth", mode, dir, &attr->stacktrace_depth);
  222. attr->dentries.require_start_file =
  223. debugfs_create_ul("require-start", mode, dir, &attr->require_start);
  224. attr->dentries.require_end_file =
  225. debugfs_create_ul("require-end", mode, dir, &attr->require_end);
  226. attr->dentries.reject_start_file =
  227. debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
  228. attr->dentries.reject_end_file =
  229. debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
  230. if (!attr->dentries.stacktrace_depth_file ||
  231. !attr->dentries.require_start_file ||
  232. !attr->dentries.require_end_file ||
  233. !attr->dentries.reject_start_file ||
  234. !attr->dentries.reject_end_file)
  235. goto fail;
  236. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  237. return 0;
  238. fail:
  239. cleanup_fault_attr_dentries(attr);
  240. return -ENOMEM;
  241. }
  242. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */