fault-inject.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/export.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 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. EXPORT_SYMBOL_GPL(setup_fault_attr);
  36. static void fail_dump(struct fault_attr *attr)
  37. {
  38. if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {
  39. printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
  40. "name %pd, interval %lu, probability %lu, "
  41. "space %d, times %d\n", attr->dname,
  42. attr->interval, attr->probability,
  43. atomic_read(&attr->space),
  44. atomic_read(&attr->times));
  45. if (attr->verbose > 1)
  46. dump_stack();
  47. }
  48. }
  49. #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
  50. static bool fail_task(struct fault_attr *attr, struct task_struct *task)
  51. {
  52. return in_task() && task->make_it_fail;
  53. }
  54. #define MAX_STACK_TRACE_DEPTH 32
  55. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  56. static bool fail_stacktrace(struct fault_attr *attr)
  57. {
  58. struct stack_trace trace;
  59. int depth = attr->stacktrace_depth;
  60. unsigned long entries[MAX_STACK_TRACE_DEPTH];
  61. int n;
  62. bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
  63. if (depth == 0)
  64. return found;
  65. trace.nr_entries = 0;
  66. trace.entries = entries;
  67. trace.max_entries = depth;
  68. trace.skip = 1;
  69. save_stack_trace(&trace);
  70. for (n = 0; n < trace.nr_entries; n++) {
  71. if (attr->reject_start <= entries[n] &&
  72. entries[n] < attr->reject_end)
  73. return false;
  74. if (attr->require_start <= entries[n] &&
  75. entries[n] < attr->require_end)
  76. found = true;
  77. }
  78. return found;
  79. }
  80. #else
  81. static inline bool fail_stacktrace(struct fault_attr *attr)
  82. {
  83. return true;
  84. }
  85. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  86. /*
  87. * This code is stolen from failmalloc-1.0
  88. * http://www.nongnu.org/failmalloc/
  89. */
  90. bool should_fail(struct fault_attr *attr, ssize_t size)
  91. {
  92. if (in_task()) {
  93. unsigned int fail_nth = READ_ONCE(current->fail_nth);
  94. if (fail_nth) {
  95. if (!WRITE_ONCE(current->fail_nth, fail_nth - 1))
  96. goto fail;
  97. return false;
  98. }
  99. }
  100. /* No need to check any other properties if the probability is 0 */
  101. if (attr->probability == 0)
  102. return false;
  103. if (attr->task_filter && !fail_task(attr, current))
  104. return false;
  105. if (atomic_read(&attr->times) == 0)
  106. return false;
  107. if (atomic_read(&attr->space) > size) {
  108. atomic_sub(size, &attr->space);
  109. return false;
  110. }
  111. if (attr->interval > 1) {
  112. attr->count++;
  113. if (attr->count % attr->interval)
  114. return false;
  115. }
  116. if (attr->probability <= prandom_u32() % 100)
  117. return false;
  118. if (!fail_stacktrace(attr))
  119. return false;
  120. fail:
  121. fail_dump(attr);
  122. if (atomic_read(&attr->times) != -1)
  123. atomic_dec_not_zero(&attr->times);
  124. return true;
  125. }
  126. EXPORT_SYMBOL_GPL(should_fail);
  127. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  128. static int debugfs_ul_set(void *data, u64 val)
  129. {
  130. *(unsigned long *)data = val;
  131. return 0;
  132. }
  133. static int debugfs_ul_get(void *data, u64 *val)
  134. {
  135. *val = *(unsigned long *)data;
  136. return 0;
  137. }
  138. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  139. static struct dentry *debugfs_create_ul(const char *name, umode_t mode,
  140. struct dentry *parent, unsigned long *value)
  141. {
  142. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  143. }
  144. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  145. static int debugfs_stacktrace_depth_set(void *data, u64 val)
  146. {
  147. *(unsigned long *)data =
  148. min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
  149. return 0;
  150. }
  151. DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
  152. debugfs_stacktrace_depth_set, "%llu\n");
  153. static struct dentry *debugfs_create_stacktrace_depth(
  154. const char *name, umode_t mode,
  155. struct dentry *parent, unsigned long *value)
  156. {
  157. return debugfs_create_file(name, mode, parent, value,
  158. &fops_stacktrace_depth);
  159. }
  160. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  161. struct dentry *fault_create_debugfs_attr(const char *name,
  162. struct dentry *parent, struct fault_attr *attr)
  163. {
  164. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  165. struct dentry *dir;
  166. dir = debugfs_create_dir(name, parent);
  167. if (!dir)
  168. return ERR_PTR(-ENOMEM);
  169. if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
  170. goto fail;
  171. if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
  172. goto fail;
  173. if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
  174. goto fail;
  175. if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
  176. goto fail;
  177. if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
  178. goto fail;
  179. if (!debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir,
  180. &attr->ratelimit_state.interval))
  181. goto fail;
  182. if (!debugfs_create_u32("verbose_ratelimit_burst", mode, dir,
  183. &attr->ratelimit_state.burst))
  184. goto fail;
  185. if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
  186. goto fail;
  187. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  188. if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
  189. &attr->stacktrace_depth))
  190. goto fail;
  191. if (!debugfs_create_ul("require-start", mode, dir,
  192. &attr->require_start))
  193. goto fail;
  194. if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
  195. goto fail;
  196. if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
  197. goto fail;
  198. if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
  199. goto fail;
  200. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  201. attr->dname = dget(dir);
  202. return dir;
  203. fail:
  204. debugfs_remove_recursive(dir);
  205. return ERR_PTR(-ENOMEM);
  206. }
  207. EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
  208. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */