fault-inject.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  108. static int debugfs_ul_set(void *data, u64 val)
  109. {
  110. *(unsigned long *)data = val;
  111. return 0;
  112. }
  113. static int debugfs_ul_get(void *data, u64 *val)
  114. {
  115. *val = *(unsigned long *)data;
  116. return 0;
  117. }
  118. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  119. static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
  120. struct dentry *parent, unsigned long *value)
  121. {
  122. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  123. }
  124. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  125. static int debugfs_stacktrace_depth_set(void *data, u64 val)
  126. {
  127. *(unsigned long *)data =
  128. min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
  129. return 0;
  130. }
  131. DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
  132. debugfs_stacktrace_depth_set, "%llu\n");
  133. static struct dentry *debugfs_create_stacktrace_depth(
  134. const char *name, mode_t mode,
  135. struct dentry *parent, unsigned long *value)
  136. {
  137. return debugfs_create_file(name, mode, parent, value,
  138. &fops_stacktrace_depth);
  139. }
  140. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  141. static int debugfs_atomic_t_set(void *data, u64 val)
  142. {
  143. atomic_set((atomic_t *)data, val);
  144. return 0;
  145. }
  146. static int debugfs_atomic_t_get(void *data, u64 *val)
  147. {
  148. *val = atomic_read((atomic_t *)data);
  149. return 0;
  150. }
  151. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  152. debugfs_atomic_t_set, "%lld\n");
  153. static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
  154. struct dentry *parent, atomic_t *value)
  155. {
  156. return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
  157. }
  158. void cleanup_fault_attr_dentries(struct fault_attr *attr)
  159. {
  160. debugfs_remove(attr->dentries.probability_file);
  161. attr->dentries.probability_file = NULL;
  162. debugfs_remove(attr->dentries.interval_file);
  163. attr->dentries.interval_file = NULL;
  164. debugfs_remove(attr->dentries.times_file);
  165. attr->dentries.times_file = NULL;
  166. debugfs_remove(attr->dentries.space_file);
  167. attr->dentries.space_file = NULL;
  168. debugfs_remove(attr->dentries.verbose_file);
  169. attr->dentries.verbose_file = NULL;
  170. debugfs_remove(attr->dentries.task_filter_file);
  171. attr->dentries.task_filter_file = NULL;
  172. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  173. debugfs_remove(attr->dentries.stacktrace_depth_file);
  174. attr->dentries.stacktrace_depth_file = NULL;
  175. debugfs_remove(attr->dentries.require_start_file);
  176. attr->dentries.require_start_file = NULL;
  177. debugfs_remove(attr->dentries.require_end_file);
  178. attr->dentries.require_end_file = NULL;
  179. debugfs_remove(attr->dentries.reject_start_file);
  180. attr->dentries.reject_start_file = NULL;
  181. debugfs_remove(attr->dentries.reject_end_file);
  182. attr->dentries.reject_end_file = NULL;
  183. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  184. if (attr->dentries.dir)
  185. WARN_ON(!simple_empty(attr->dentries.dir));
  186. debugfs_remove(attr->dentries.dir);
  187. attr->dentries.dir = NULL;
  188. }
  189. int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
  190. {
  191. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  192. struct dentry *dir;
  193. memset(&attr->dentries, 0, sizeof(attr->dentries));
  194. dir = debugfs_create_dir(name, NULL);
  195. if (!dir)
  196. goto fail;
  197. attr->dentries.dir = dir;
  198. attr->dentries.probability_file =
  199. debugfs_create_ul("probability", mode, dir, &attr->probability);
  200. attr->dentries.interval_file =
  201. debugfs_create_ul("interval", mode, dir, &attr->interval);
  202. attr->dentries.times_file =
  203. debugfs_create_atomic_t("times", mode, dir, &attr->times);
  204. attr->dentries.space_file =
  205. debugfs_create_atomic_t("space", mode, dir, &attr->space);
  206. attr->dentries.verbose_file =
  207. debugfs_create_ul("verbose", mode, dir, &attr->verbose);
  208. attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
  209. mode, dir, &attr->task_filter);
  210. if (!attr->dentries.probability_file || !attr->dentries.interval_file ||
  211. !attr->dentries.times_file || !attr->dentries.space_file ||
  212. !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
  213. goto fail;
  214. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  215. attr->dentries.stacktrace_depth_file =
  216. debugfs_create_stacktrace_depth(
  217. "stacktrace-depth", mode, dir, &attr->stacktrace_depth);
  218. attr->dentries.require_start_file =
  219. debugfs_create_ul("require-start", mode, dir, &attr->require_start);
  220. attr->dentries.require_end_file =
  221. debugfs_create_ul("require-end", mode, dir, &attr->require_end);
  222. attr->dentries.reject_start_file =
  223. debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
  224. attr->dentries.reject_end_file =
  225. debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
  226. if (!attr->dentries.stacktrace_depth_file ||
  227. !attr->dentries.require_start_file ||
  228. !attr->dentries.require_end_file ||
  229. !attr->dentries.reject_start_file ||
  230. !attr->dentries.reject_end_file)
  231. goto fail;
  232. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  233. return 0;
  234. fail:
  235. cleanup_fault_attr_dentries(attr);
  236. return -ENOMEM;
  237. }
  238. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */