fault-inject.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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->probability, attr->interval,
  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_interrupt() && 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. /* No need to check any other properties if the probability is 0 */
  93. if (attr->probability == 0)
  94. return false;
  95. if (attr->task_filter && !fail_task(attr, current))
  96. return false;
  97. if (atomic_read(&attr->times) == 0)
  98. return false;
  99. if (atomic_read(&attr->space) > size) {
  100. atomic_sub(size, &attr->space);
  101. return false;
  102. }
  103. if (attr->interval > 1) {
  104. attr->count++;
  105. if (attr->count % attr->interval)
  106. return false;
  107. }
  108. if (attr->probability <= prandom_u32() % 100)
  109. return false;
  110. if (!fail_stacktrace(attr))
  111. return false;
  112. fail_dump(attr);
  113. if (atomic_read(&attr->times) != -1)
  114. atomic_dec_not_zero(&attr->times);
  115. return true;
  116. }
  117. EXPORT_SYMBOL_GPL(should_fail);
  118. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  119. static int debugfs_ul_set(void *data, u64 val)
  120. {
  121. *(unsigned long *)data = val;
  122. return 0;
  123. }
  124. static int debugfs_ul_get(void *data, u64 *val)
  125. {
  126. *val = *(unsigned long *)data;
  127. return 0;
  128. }
  129. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  130. static struct dentry *debugfs_create_ul(const char *name, umode_t mode,
  131. struct dentry *parent, unsigned long *value)
  132. {
  133. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  134. }
  135. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  136. static int debugfs_stacktrace_depth_set(void *data, u64 val)
  137. {
  138. *(unsigned long *)data =
  139. min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
  140. return 0;
  141. }
  142. DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
  143. debugfs_stacktrace_depth_set, "%llu\n");
  144. static struct dentry *debugfs_create_stacktrace_depth(
  145. const char *name, umode_t mode,
  146. struct dentry *parent, unsigned long *value)
  147. {
  148. return debugfs_create_file(name, mode, parent, value,
  149. &fops_stacktrace_depth);
  150. }
  151. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  152. struct dentry *fault_create_debugfs_attr(const char *name,
  153. struct dentry *parent, struct fault_attr *attr)
  154. {
  155. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  156. struct dentry *dir;
  157. dir = debugfs_create_dir(name, parent);
  158. if (!dir)
  159. return ERR_PTR(-ENOMEM);
  160. if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
  161. goto fail;
  162. if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
  163. goto fail;
  164. if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
  165. goto fail;
  166. if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
  167. goto fail;
  168. if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
  169. goto fail;
  170. if (!debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir,
  171. &attr->ratelimit_state.interval))
  172. goto fail;
  173. if (!debugfs_create_u32("verbose_ratelimit_burst", mode, dir,
  174. &attr->ratelimit_state.burst))
  175. goto fail;
  176. if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
  177. goto fail;
  178. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  179. if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
  180. &attr->stacktrace_depth))
  181. goto fail;
  182. if (!debugfs_create_ul("require-start", mode, dir,
  183. &attr->require_start))
  184. goto fail;
  185. if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
  186. goto fail;
  187. if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
  188. goto fail;
  189. if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
  190. goto fail;
  191. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  192. attr->dname = dget(dir);
  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 */