1082dfb3cSPaul E. McKenney /* SPDX-License-Identifier: GPL-2.0+ */
251b1130eSPaul E. McKenney /*
351b1130eSPaul E. McKenney * Common functions for in-kernel torture tests.
451b1130eSPaul E. McKenney *
551b1130eSPaul E. McKenney * Copyright IBM Corporation, 2014
651b1130eSPaul E. McKenney *
7082dfb3cSPaul E. McKenney * Author: Paul E. McKenney <[email protected]>
851b1130eSPaul E. McKenney */
951b1130eSPaul E. McKenney
1051b1130eSPaul E. McKenney #ifndef __LINUX_TORTURE_H
1151b1130eSPaul E. McKenney #define __LINUX_TORTURE_H
1251b1130eSPaul E. McKenney
1351b1130eSPaul E. McKenney #include <linux/types.h>
1451b1130eSPaul E. McKenney #include <linux/cache.h>
1551b1130eSPaul E. McKenney #include <linux/spinlock.h>
1651b1130eSPaul E. McKenney #include <linux/threads.h>
17e1b6705bSYury Norov #include <linux/cpumask_types.h>
1851b1130eSPaul E. McKenney #include <linux/seqlock.h>
1951b1130eSPaul E. McKenney #include <linux/lockdep.h>
2051b1130eSPaul E. McKenney #include <linux/completion.h>
2151b1130eSPaul E. McKenney #include <linux/debugobjects.h>
2251b1130eSPaul E. McKenney #include <linux/bug.h>
2351b1130eSPaul E. McKenney #include <linux/compiler.h>
24ea115c24SKent Overstreet #include <linux/hrtimer.h>
2551b1130eSPaul E. McKenney
269e250225SPaul E. McKenney /* Definitions for a non-string torture-test module parameter. */
279e250225SPaul E. McKenney #define torture_param(type, name, init, msg) \
289e250225SPaul E. McKenney static type name = init; \
299e250225SPaul E. McKenney module_param(name, type, 0444); \
309e250225SPaul E. McKenney MODULE_PARM_DESC(name, msg);
319e250225SPaul E. McKenney
32c2884de3SPaul E. McKenney #define TORTURE_FLAG "-torture:"
33c2884de3SPaul E. McKenney #define TOROUT_STRING(s) \
34489bb3d2SSeongJae Park pr_alert("%s" TORTURE_FLAG " %s\n", torture_type, s)
35c2884de3SPaul E. McKenney #define VERBOSE_TOROUT_STRING(s) \
368a67a20bSPaul E. McKenney do { \
378a67a20bSPaul E. McKenney if (verbose) { \
388a67a20bSPaul E. McKenney verbose_torout_sleep(); \
398a67a20bSPaul E. McKenney pr_alert("%s" TORTURE_FLAG " %s\n", torture_type, s); \
408a67a20bSPaul E. McKenney } \
418a67a20bSPaul E. McKenney } while (0)
4281faa4f6SLi Zhijian #define TOROUT_ERRSTRING(s) \
4381faa4f6SLi Zhijian pr_alert("%s" TORTURE_FLAG "!!! %s\n", torture_type, s)
448a67a20bSPaul E. McKenney void verbose_torout_sleep(void);
45c2884de3SPaul E. McKenney
46efeff6b3SPaul E. McKenney #define torture_init_error(firsterr) \
47efeff6b3SPaul E. McKenney ({ \
48efeff6b3SPaul E. McKenney int ___firsterr = (firsterr); \
49efeff6b3SPaul E. McKenney \
50efeff6b3SPaul E. McKenney WARN_ONCE(!IS_MODULE(CONFIG_RCU_TORTURE_TEST) && ___firsterr < 0, "Torture-test initialization failed with error code %d\n", ___firsterr); \
51efeff6b3SPaul E. McKenney ___firsterr < 0; \
52efeff6b3SPaul E. McKenney })
53efeff6b3SPaul E. McKenney
542e9e8081SPaul E. McKenney /* Definitions for online/offline exerciser. */
551afb95feSPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
561afb95feSPaul E. McKenney int torture_num_online_cpus(void);
571afb95feSPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */
torture_num_online_cpus(void)581afb95feSPaul E. McKenney static inline int torture_num_online_cpus(void) { return 1; }
591afb95feSPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
603a6cb58fSPaul E. McKenney typedef void torture_ofl_func(void);
61d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_onl_attempts, long *n_onl_successes,
62d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_onl, int *max_onl);
63d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
64d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl);
653a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f);
66eea203feSJoe Perches void torture_onoff_stats(void);
672e9e8081SPaul E. McKenney bool torture_onoff_failures(void);
682e9e8081SPaul E. McKenney
699e250225SPaul E. McKenney /* Low-rider random number generator. */
7051b1130eSPaul E. McKenney struct torture_random_state {
7151b1130eSPaul E. McKenney unsigned long trs_state;
7251b1130eSPaul E. McKenney long trs_count;
7351b1130eSPaul E. McKenney };
7451b1130eSPaul E. McKenney #define DEFINE_TORTURE_RANDOM(name) struct torture_random_state name = { 0, 0 }
753025520eSPaul E. McKenney #define DEFINE_TORTURE_RANDOM_PERCPU(name) \
763025520eSPaul E. McKenney DEFINE_PER_CPU(struct torture_random_state, name)
7751b1130eSPaul E. McKenney unsigned long torture_random(struct torture_random_state *trsp);
torture_random_init(struct torture_random_state * trsp)784a5f133cSPaul E. McKenney static inline void torture_random_init(struct torture_random_state *trsp)
794a5f133cSPaul E. McKenney {
804a5f133cSPaul E. McKenney trsp->trs_state = 0;
814a5f133cSPaul E. McKenney trsp->trs_count = 0;
824a5f133cSPaul E. McKenney }
8351b1130eSPaul E. McKenney
84ae19aaafSPaul E. McKenney /* Definitions for high-resolution-timer sleeps. */
85a741deacSPaul E. McKenney int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, const enum hrtimer_mode mode,
86a741deacSPaul E. McKenney struct torture_random_state *trsp);
87ae19aaafSPaul E. McKenney int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp);
88ae19aaafSPaul E. McKenney int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp);
89ae19aaafSPaul E. McKenney int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp);
90ae19aaafSPaul E. McKenney int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp);
91ae19aaafSPaul E. McKenney
923808dc9fSPaul E. McKenney /* Task shuffler, which causes CPUs to occasionally go idle. */
933808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp);
943808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint);
953808dc9fSPaul E. McKenney
96e991dbc0SPaul E. McKenney /* Test auto-shutdown handling. */
97f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title);
98e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void));
99f67a3356SPaul E. McKenney
100628edaa5SPaul E. McKenney /* Task stuttering, which forces load/no-load transitions. */
101474e59b4SPaul E. McKenney bool stutter_wait(const char *title);
102ff3bf92dSPaul E. McKenney int torture_stutter_init(int s, int sgap);
103628edaa5SPaul E. McKenney
104b5daa8f3SPaul E. McKenney /* Initialization and cleanup. */
10590127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v);
106b5daa8f3SPaul E. McKenney void torture_init_end(void);
107*623b5280SPaul E. McKenney unsigned long get_torture_init_jiffies(void);
108d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void);
109d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void);
11036970bb9SPaul E. McKenney bool torture_must_stop(void);
11136970bb9SPaul E. McKenney bool torture_must_stop_irq(void);
1127fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title);
11347cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
11467d5404dSPaul E. McKenney char *f, struct task_struct **tp, void (*cbf)(struct task_struct *tp));
1159c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp);
11647cf29b9SPaul E. McKenney
11747cf29b9SPaul E. McKenney #define torture_create_kthread(n, arg, tp) \
11847cf29b9SPaul E. McKenney _torture_create_kthread(n, (arg), #n, "Creating " #n " task", \
11967d5404dSPaul E. McKenney "Failed to create " #n, &(tp), NULL)
12067d5404dSPaul E. McKenney #define torture_create_kthread_cb(n, arg, tp, cbf) \
12167d5404dSPaul E. McKenney _torture_create_kthread(n, (arg), #n, "Creating " #n " task", \
12267d5404dSPaul E. McKenney "Failed to create " #n, &(tp), cbf)
1239c029b86SPaul E. McKenney #define torture_stop_kthread(n, tp) \
1249c029b86SPaul E. McKenney _torture_stop_kthread("Stopping " #n " task", &(tp))
125b5daa8f3SPaul E. McKenney
1260cfecd7dSPaul E. McKenney /* Scheduler-related definitions. */
12701b1d88bSThomas Gleixner #ifdef CONFIG_PREEMPTION
128bd6c375bSFrederic Weisbecker #define torture_preempt_schedule() __preempt_schedule()
129cc1321c9SPaul E. McKenney #else
130be44ae62SRandy Dunlap #define torture_preempt_schedule() do { } while (0)
131cc1321c9SPaul E. McKenney #endif
132cc1321c9SPaul E. McKenney
1330cfecd7dSPaul E. McKenney #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST) || IS_ENABLED(CONFIG_LOCK_TORTURE_TEST) || IS_MODULE(CONFIG_LOCK_TORTURE_TEST)
1340203b485SPaul E. McKenney long torture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask, bool dowarn);
1350cfecd7dSPaul E. McKenney #endif
1360cfecd7dSPaul E. McKenney
13751b1130eSPaul E. McKenney #endif /* __LINUX_TORTURE_H */
138