fault-inject.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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)
  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. EXPORT_SYMBOL_GPL(should_fail);
  109. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  110. static int debugfs_ul_set(void *data, u64 val)
  111. {
  112. *(unsigned long *)data = val;
  113. return 0;
  114. }
  115. static int debugfs_ul_get(void *data, u64 *val)
  116. {
  117. *val = *(unsigned long *)data;
  118. return 0;
  119. }
  120. DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
  121. static struct dentry *debugfs_create_ul(const char *name, umode_t mode,
  122. struct dentry *parent, unsigned long *value)
  123. {
  124. return debugfs_create_file(name, mode, parent, value, &fops_ul);
  125. }
  126. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  127. static int debugfs_stacktrace_depth_set(void *data, u64 val)
  128. {
  129. *(unsigned long *)data =
  130. min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
  131. return 0;
  132. }
  133. DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
  134. debugfs_stacktrace_depth_set, "%llu\n");
  135. static struct dentry *debugfs_create_stacktrace_depth(
  136. const char *name, umode_t mode,
  137. struct dentry *parent, unsigned long *value)
  138. {
  139. return debugfs_create_file(name, mode, parent, value,
  140. &fops_stacktrace_depth);
  141. }
  142. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  143. struct dentry *fault_create_debugfs_attr(const char *name,
  144. struct dentry *parent, struct fault_attr *attr)
  145. {
  146. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  147. struct dentry *dir;
  148. dir = debugfs_create_dir(name, parent);
  149. if (!dir)
  150. return ERR_PTR(-ENOMEM);
  151. if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
  152. goto fail;
  153. if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
  154. goto fail;
  155. if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
  156. goto fail;
  157. if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
  158. goto fail;
  159. if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
  160. goto fail;
  161. if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
  162. goto fail;
  163. #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
  164. if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
  165. &attr->stacktrace_depth))
  166. goto fail;
  167. if (!debugfs_create_ul("require-start", mode, dir,
  168. &attr->require_start))
  169. goto fail;
  170. if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
  171. goto fail;
  172. if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
  173. goto fail;
  174. if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
  175. goto fail;
  176. #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
  177. return dir;
  178. fail:
  179. debugfs_remove_recursive(dir);
  180. return ERR_PTR(-ENOMEM);
  181. }
  182. EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
  183. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */