Commit | Line | Data |
---|---|---|
51b1130e PM |
1 | /* |
2 | * Common functions for in-kernel torture tests. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, you can access it online at | |
16 | * http://www.gnu.org/licenses/gpl-2.0.html. | |
17 | * | |
18 | * Copyright (C) IBM Corporation, 2014 | |
19 | * | |
20 | * Author: Paul E. McKenney <paulmck@us.ibm.com> | |
21 | * Based on kernel/rcu/torture.c. | |
22 | */ | |
23 | #include <linux/types.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/module.h> | |
27 | #include <linux/kthread.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/smp.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/sched.h> | |
33 | #include <linux/atomic.h> | |
34 | #include <linux/bitops.h> | |
35 | #include <linux/completion.h> | |
36 | #include <linux/moduleparam.h> | |
37 | #include <linux/percpu.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <linux/reboot.h> | |
40 | #include <linux/freezer.h> | |
41 | #include <linux/cpu.h> | |
42 | #include <linux/delay.h> | |
43 | #include <linux/stat.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/trace_clock.h> | |
46 | #include <asm/byteorder.h> | |
47 | #include <linux/torture.h> | |
48 | ||
49 | MODULE_LICENSE("GPL"); | |
50 | MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>"); | |
51 | ||
f67a3356 PM |
52 | int fullstop = FULLSTOP_RMMOD; |
53 | EXPORT_SYMBOL_GPL(fullstop); | |
54 | DEFINE_MUTEX(fullstop_mutex); | |
55 | EXPORT_SYMBOL_GPL(fullstop_mutex); | |
56 | ||
51b1130e PM |
57 | #define TORTURE_RANDOM_MULT 39916801 /* prime */ |
58 | #define TORTURE_RANDOM_ADD 479001701 /* prime */ | |
59 | #define TORTURE_RANDOM_REFRESH 10000 | |
60 | ||
61 | /* | |
62 | * Crude but fast random-number generator. Uses a linear congruential | |
63 | * generator, with occasional help from cpu_clock(). | |
64 | */ | |
65 | unsigned long | |
66 | torture_random(struct torture_random_state *trsp) | |
67 | { | |
68 | if (--trsp->trs_count < 0) { | |
69 | trsp->trs_state += (unsigned long)local_clock(); | |
70 | trsp->trs_count = TORTURE_RANDOM_REFRESH; | |
71 | } | |
72 | trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + | |
73 | TORTURE_RANDOM_ADD; | |
74 | return swahw32(trsp->trs_state); | |
75 | } | |
76 | EXPORT_SYMBOL_GPL(torture_random); | |
f67a3356 | 77 | |
3808dc9f PM |
78 | /* |
79 | * Variables for shuffling. The idea is to ensure that each CPU stays | |
80 | * idle for an extended period to test interactions with dyntick idle, | |
81 | * as well as interactions with any per-CPU varibles. | |
82 | */ | |
83 | struct shuffle_task { | |
84 | struct list_head st_l; | |
85 | struct task_struct *st_t; | |
86 | }; | |
87 | ||
88 | static long shuffle_interval; /* In jiffies. */ | |
89 | static struct task_struct *shuffler_task; | |
90 | static cpumask_var_t shuffle_tmp_mask; | |
91 | static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ | |
92 | static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); | |
93 | static DEFINE_MUTEX(shuffle_task_mutex); | |
94 | ||
95 | /* | |
96 | * Register a task to be shuffled. If there is no memory, just splat | |
97 | * and don't bother registering. | |
98 | */ | |
99 | void torture_shuffle_task_register(struct task_struct *tp) | |
100 | { | |
101 | struct shuffle_task *stp; | |
102 | ||
103 | if (WARN_ON_ONCE(tp == NULL)) | |
104 | return; | |
105 | stp = kmalloc(sizeof(*stp), GFP_KERNEL); | |
106 | if (WARN_ON_ONCE(stp == NULL)) | |
107 | return; | |
108 | stp->st_t = tp; | |
109 | mutex_lock(&shuffle_task_mutex); | |
110 | list_add(&stp->st_l, &shuffle_task_list); | |
111 | mutex_unlock(&shuffle_task_mutex); | |
112 | } | |
113 | EXPORT_SYMBOL_GPL(torture_shuffle_task_register); | |
114 | ||
115 | /* | |
116 | * Unregister all tasks, for example, at the end of the torture run. | |
117 | */ | |
118 | static void torture_shuffle_task_unregister_all(void) | |
119 | { | |
120 | struct shuffle_task *stp; | |
121 | struct shuffle_task *p; | |
122 | ||
123 | mutex_lock(&shuffle_task_mutex); | |
124 | list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { | |
125 | list_del(&stp->st_l); | |
126 | kfree(stp); | |
127 | } | |
128 | mutex_unlock(&shuffle_task_mutex); | |
129 | } | |
130 | ||
131 | /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. | |
132 | * A special case is when shuffle_idle_cpu = -1, in which case we allow | |
133 | * the tasks to run on all CPUs. | |
134 | */ | |
135 | static void torture_shuffle_tasks(void) | |
136 | { | |
137 | struct shuffle_task *stp; | |
138 | ||
139 | cpumask_setall(shuffle_tmp_mask); | |
140 | get_online_cpus(); | |
141 | ||
142 | /* No point in shuffling if there is only one online CPU (ex: UP) */ | |
143 | if (num_online_cpus() == 1) { | |
144 | put_online_cpus(); | |
145 | return; | |
146 | } | |
147 | ||
148 | /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ | |
149 | shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); | |
150 | if (shuffle_idle_cpu >= nr_cpu_ids) | |
151 | shuffle_idle_cpu = -1; | |
152 | if (shuffle_idle_cpu != -1) { | |
153 | cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); | |
154 | if (cpumask_empty(shuffle_tmp_mask)) { | |
155 | put_online_cpus(); | |
156 | return; | |
157 | } | |
158 | } | |
159 | ||
160 | mutex_lock(&shuffle_task_mutex); | |
161 | list_for_each_entry(stp, &shuffle_task_list, st_l) | |
162 | set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); | |
163 | mutex_unlock(&shuffle_task_mutex); | |
164 | ||
165 | put_online_cpus(); | |
166 | } | |
167 | ||
168 | /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the | |
169 | * system to become idle at a time and cut off its timer ticks. This is meant | |
170 | * to test the support for such tickless idle CPU in RCU. | |
171 | */ | |
172 | static int torture_shuffle(void *arg) | |
173 | { | |
174 | VERBOSE_TOROUT_STRING("torture_shuffle task started"); | |
175 | do { | |
176 | schedule_timeout_interruptible(shuffle_interval); | |
177 | torture_shuffle_tasks(); | |
178 | torture_shutdown_absorb("torture_shuffle"); | |
179 | } while (!torture_must_stop()); | |
180 | VERBOSE_TOROUT_STRING("torture_shuffle task stopping"); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | /* | |
185 | * Start the shuffler, with shuffint in jiffies. | |
186 | */ | |
187 | int torture_shuffle_init(long shuffint) | |
188 | { | |
189 | int ret; | |
190 | ||
191 | shuffle_interval = shuffint; | |
192 | ||
193 | shuffle_idle_cpu = -1; | |
194 | ||
195 | if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { | |
196 | VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); | |
197 | return -ENOMEM; | |
198 | } | |
199 | ||
200 | /* Create the shuffler thread */ | |
201 | shuffler_task = kthread_run(torture_shuffle, NULL, "torture_shuffle"); | |
202 | if (IS_ERR(shuffler_task)) { | |
203 | ret = PTR_ERR(shuffler_task); | |
204 | free_cpumask_var(shuffle_tmp_mask); | |
205 | VERBOSE_TOROUT_ERRSTRING("Failed to create shuffler"); | |
206 | shuffler_task = NULL; | |
207 | return ret; | |
208 | } | |
209 | torture_shuffle_task_register(shuffler_task); | |
210 | return 0; | |
211 | } | |
212 | EXPORT_SYMBOL_GPL(torture_shuffle_init); | |
213 | ||
214 | /* | |
215 | * Stop the shuffling. | |
216 | */ | |
217 | void torture_shuffle_cleanup(void) | |
218 | { | |
219 | torture_shuffle_task_unregister_all(); | |
220 | if (shuffler_task) { | |
221 | VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); | |
222 | kthread_stop(shuffler_task); | |
223 | free_cpumask_var(shuffle_tmp_mask); | |
224 | } | |
225 | shuffler_task = NULL; | |
226 | } | |
227 | EXPORT_SYMBOL_GPL(torture_shuffle_cleanup); | |
228 | ||
f67a3356 PM |
229 | /* |
230 | * Absorb kthreads into a kernel function that won't return, so that | |
231 | * they won't ever access module text or data again. | |
232 | */ | |
233 | void torture_shutdown_absorb(const char *title) | |
234 | { | |
235 | while (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { | |
236 | pr_notice( | |
237 | "torture thread %s parking due to system shutdown\n", | |
238 | title); | |
239 | schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); | |
240 | } | |
241 | } | |
242 | EXPORT_SYMBOL_GPL(torture_shutdown_absorb); |