torture.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Common functions for in-kernel torture tests.
  4. *
  5. * Copyright IBM Corporation, 2014
  6. *
  7. * Author: Paul E. McKenney <[email protected]>
  8. */
  9. #ifndef __LINUX_TORTURE_H
  10. #define __LINUX_TORTURE_H
  11. #include <linux/types.h>
  12. #include <linux/cache.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/threads.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/seqlock.h>
  17. #include <linux/lockdep.h>
  18. #include <linux/completion.h>
  19. #include <linux/debugobjects.h>
  20. #include <linux/bug.h>
  21. #include <linux/compiler.h>
  22. /* Definitions for a non-string torture-test module parameter. */
  23. #define torture_param(type, name, init, msg) \
  24. static type name = init; \
  25. module_param(name, type, 0444); \
  26. MODULE_PARM_DESC(name, msg);
  27. #define TORTURE_FLAG "-torture:"
  28. #define TOROUT_STRING(s) \
  29. pr_alert("%s" TORTURE_FLAG " %s\n", torture_type, s)
  30. #define VERBOSE_TOROUT_STRING(s) \
  31. do { \
  32. if (verbose) { \
  33. verbose_torout_sleep(); \
  34. pr_alert("%s" TORTURE_FLAG " %s\n", torture_type, s); \
  35. } \
  36. } while (0)
  37. #define TOROUT_ERRSTRING(s) \
  38. pr_alert("%s" TORTURE_FLAG "!!! %s\n", torture_type, s)
  39. void verbose_torout_sleep(void);
  40. #define torture_init_error(firsterr) \
  41. ({ \
  42. int ___firsterr = (firsterr); \
  43. \
  44. WARN_ONCE(!IS_MODULE(CONFIG_RCU_TORTURE_TEST) && ___firsterr < 0, "Torture-test initialization failed with error code %d\n", ___firsterr); \
  45. ___firsterr < 0; \
  46. })
  47. /* Definitions for online/offline exerciser. */
  48. #ifdef CONFIG_HOTPLUG_CPU
  49. int torture_num_online_cpus(void);
  50. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  51. static inline int torture_num_online_cpus(void) { return 1; }
  52. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  53. typedef void torture_ofl_func(void);
  54. bool torture_offline(int cpu, long *n_onl_attempts, long *n_onl_successes,
  55. unsigned long *sum_offl, int *min_onl, int *max_onl);
  56. bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
  57. unsigned long *sum_onl, int *min_onl, int *max_onl);
  58. int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f);
  59. void torture_onoff_stats(void);
  60. bool torture_onoff_failures(void);
  61. /* Low-rider random number generator. */
  62. struct torture_random_state {
  63. unsigned long trs_state;
  64. long trs_count;
  65. };
  66. #define DEFINE_TORTURE_RANDOM(name) struct torture_random_state name = { 0, 0 }
  67. #define DEFINE_TORTURE_RANDOM_PERCPU(name) \
  68. DEFINE_PER_CPU(struct torture_random_state, name)
  69. unsigned long torture_random(struct torture_random_state *trsp);
  70. static inline void torture_random_init(struct torture_random_state *trsp)
  71. {
  72. trsp->trs_state = 0;
  73. trsp->trs_count = 0;
  74. }
  75. /* Definitions for high-resolution-timer sleeps. */
  76. int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, struct torture_random_state *trsp);
  77. int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp);
  78. int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp);
  79. int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp);
  80. int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp);
  81. /* Task shuffler, which causes CPUs to occasionally go idle. */
  82. void torture_shuffle_task_register(struct task_struct *tp);
  83. int torture_shuffle_init(long shuffint);
  84. /* Test auto-shutdown handling. */
  85. void torture_shutdown_absorb(const char *title);
  86. int torture_shutdown_init(int ssecs, void (*cleanup)(void));
  87. /* Task stuttering, which forces load/no-load transitions. */
  88. bool stutter_wait(const char *title);
  89. int torture_stutter_init(int s, int sgap);
  90. /* Initialization and cleanup. */
  91. bool torture_init_begin(char *ttype, int v);
  92. void torture_init_end(void);
  93. bool torture_cleanup_begin(void);
  94. void torture_cleanup_end(void);
  95. bool torture_must_stop(void);
  96. bool torture_must_stop_irq(void);
  97. void torture_kthread_stopping(char *title);
  98. int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
  99. char *f, struct task_struct **tp);
  100. void _torture_stop_kthread(char *m, struct task_struct **tp);
  101. #define torture_create_kthread(n, arg, tp) \
  102. _torture_create_kthread(n, (arg), #n, "Creating " #n " task", \
  103. "Failed to create " #n, &(tp))
  104. #define torture_stop_kthread(n, tp) \
  105. _torture_stop_kthread("Stopping " #n " task", &(tp))
  106. #ifdef CONFIG_PREEMPTION
  107. #define torture_preempt_schedule() __preempt_schedule()
  108. #else
  109. #define torture_preempt_schedule() do { } while (0)
  110. #endif
  111. #endif /* __LINUX_TORTURE_H */