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