]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/locking/locktorture.c
locktorture: Teach about lock debugging
[karo-tx-linux.git] / kernel / locking / locktorture.c
1 /*
2  * Module-based torture test facility for locking
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/mutex.h>
31 #include <linux/smp.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <linux/atomic.h>
35 #include <linux/bitops.h>
36 #include <linux/completion.h>
37 #include <linux/moduleparam.h>
38 #include <linux/percpu.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/freezer.h>
42 #include <linux/cpu.h>
43 #include <linux/delay.h>
44 #include <linux/stat.h>
45 #include <linux/slab.h>
46 #include <linux/trace_clock.h>
47 #include <asm/byteorder.h>
48 #include <linux/torture.h>
49
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
52
53 torture_param(int, nwriters_stress, -1,
54              "Number of write-locking stress-test threads");
55 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
56 torture_param(int, onoff_interval, 0,
57              "Time between CPU hotplugs (s), 0=disable");
58 torture_param(int, shuffle_interval, 3,
59              "Number of jiffies between shuffles, 0=disable");
60 torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
61 torture_param(int, stat_interval, 60,
62              "Number of seconds between stats printk()s");
63 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
64 torture_param(bool, verbose, true,
65              "Enable verbose debugging printk()s");
66
67 static bool debug_lock = false;
68 static char *torture_type = "spin_lock";
69 module_param(torture_type, charp, 0444);
70 MODULE_PARM_DESC(torture_type,
71                  "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
72
73 static atomic_t n_lock_torture_errors;
74
75 static struct task_struct *stats_task;
76 static struct task_struct **writer_tasks;
77
78 static int nrealwriters_stress;
79 static bool lock_is_write_held;
80
81 struct lock_writer_stress_stats {
82         long n_write_lock_fail;
83         long n_write_lock_acquired;
84 };
85 static struct lock_writer_stress_stats *lwsa;
86
87 #if defined(MODULE)
88 #define LOCKTORTURE_RUNNABLE_INIT 1
89 #else
90 #define LOCKTORTURE_RUNNABLE_INIT 0
91 #endif
92 int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
93 module_param(torture_runnable, int, 0444);
94 MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
95
96 /* Forward reference. */
97 static void lock_torture_cleanup(void);
98
99 /*
100  * Operations vector for selecting different types of tests.
101  */
102 struct lock_torture_ops {
103         void (*init)(void);
104         int (*writelock)(void);
105         void (*write_delay)(struct torture_random_state *trsp);
106         void (*writeunlock)(void);
107         unsigned long flags;
108         const char *name;
109 };
110
111 static struct lock_torture_ops *cur_ops;
112
113 /*
114  * Definitions for lock torture testing.
115  */
116
117 static int torture_lock_busted_write_lock(void)
118 {
119         return 0;  /* BUGGY, do not use in real life!!! */
120 }
121
122 static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
123 {
124         const unsigned long longdelay_us = 100;
125
126         /* We want a long delay occasionally to force massive contention.  */
127         if (!(torture_random(trsp) %
128               (nrealwriters_stress * 2000 * longdelay_us)))
129                 mdelay(longdelay_us);
130 #ifdef CONFIG_PREEMPT
131         if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
132                 preempt_schedule();  /* Allow test to be preempted. */
133 #endif
134 }
135
136 static void torture_lock_busted_write_unlock(void)
137 {
138           /* BUGGY, do not use in real life!!! */
139 }
140
141 static struct lock_torture_ops lock_busted_ops = {
142         .writelock      = torture_lock_busted_write_lock,
143         .write_delay    = torture_lock_busted_write_delay,
144         .writeunlock    = torture_lock_busted_write_unlock,
145         .name           = "lock_busted"
146 };
147
148 static DEFINE_SPINLOCK(torture_spinlock);
149
150 static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
151 {
152         spin_lock(&torture_spinlock);
153         return 0;
154 }
155
156 static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
157 {
158         const unsigned long shortdelay_us = 2;
159         const unsigned long longdelay_us = 100;
160
161         /* We want a short delay mostly to emulate likely code, and
162          * we want a long delay occasionally to force massive contention.
163          */
164         if (!(torture_random(trsp) %
165               (nrealwriters_stress * 2000 * longdelay_us)))
166                 mdelay(longdelay_us);
167         if (!(torture_random(trsp) %
168               (nrealwriters_stress * 2 * shortdelay_us)))
169                 udelay(shortdelay_us);
170 #ifdef CONFIG_PREEMPT
171         if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
172                 preempt_schedule();  /* Allow test to be preempted. */
173 #endif
174 }
175
176 static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
177 {
178         spin_unlock(&torture_spinlock);
179 }
180
181 static struct lock_torture_ops spin_lock_ops = {
182         .writelock      = torture_spin_lock_write_lock,
183         .write_delay    = torture_spin_lock_write_delay,
184         .writeunlock    = torture_spin_lock_write_unlock,
185         .name           = "spin_lock"
186 };
187
188 static int torture_spin_lock_write_lock_irq(void)
189 __acquires(torture_spinlock_irq)
190 {
191         unsigned long flags;
192
193         spin_lock_irqsave(&torture_spinlock, flags);
194         cur_ops->flags = flags;
195         return 0;
196 }
197
198 static void torture_lock_spin_write_unlock_irq(void)
199 __releases(torture_spinlock)
200 {
201         spin_unlock_irqrestore(&torture_spinlock, cur_ops->flags);
202 }
203
204 static struct lock_torture_ops spin_lock_irq_ops = {
205         .writelock      = torture_spin_lock_write_lock_irq,
206         .write_delay    = torture_spin_lock_write_delay,
207         .writeunlock    = torture_lock_spin_write_unlock_irq,
208         .name           = "spin_lock_irq"
209 };
210
211 static DEFINE_MUTEX(torture_mutex);
212
213 static int torture_mutex_lock(void) __acquires(torture_mutex)
214 {
215         mutex_lock(&torture_mutex);
216         return 0;
217 }
218
219 static void torture_mutex_delay(struct torture_random_state *trsp)
220 {
221         const unsigned long longdelay_ms = 100;
222
223         /* We want a long delay occasionally to force massive contention.  */
224         if (!(torture_random(trsp) %
225               (nrealwriters_stress * 2000 * longdelay_ms)))
226                 mdelay(longdelay_ms * 5);
227         else
228                 mdelay(longdelay_ms / 5);
229 #ifdef CONFIG_PREEMPT
230         if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
231                 preempt_schedule();  /* Allow test to be preempted. */
232 #endif
233 }
234
235 static void torture_mutex_unlock(void) __releases(torture_mutex)
236 {
237         mutex_unlock(&torture_mutex);
238 }
239
240 static struct lock_torture_ops mutex_lock_ops = {
241         .writelock      = torture_mutex_lock,
242         .write_delay    = torture_mutex_delay,
243         .writeunlock    = torture_mutex_unlock,
244         .name           = "mutex_lock"
245 };
246
247 /*
248  * Lock torture writer kthread.  Repeatedly acquires and releases
249  * the lock, checking for duplicate acquisitions.
250  */
251 static int lock_torture_writer(void *arg)
252 {
253         struct lock_writer_stress_stats *lwsp = arg;
254         static DEFINE_TORTURE_RANDOM(rand);
255
256         VERBOSE_TOROUT_STRING("lock_torture_writer task started");
257         set_user_nice(current, MAX_NICE);
258
259         do {
260                 if ((torture_random(&rand) & 0xfffff) == 0)
261                         schedule_timeout_uninterruptible(1);
262                 cur_ops->writelock();
263                 if (WARN_ON_ONCE(lock_is_write_held))
264                         lwsp->n_write_lock_fail++;
265                 lock_is_write_held = 1;
266                 lwsp->n_write_lock_acquired++;
267                 cur_ops->write_delay(&rand);
268                 lock_is_write_held = 0;
269                 cur_ops->writeunlock();
270                 stutter_wait("lock_torture_writer");
271         } while (!torture_must_stop());
272         torture_kthread_stopping("lock_torture_writer");
273         return 0;
274 }
275
276 /*
277  * Create an lock-torture-statistics message in the specified buffer.
278  */
279 static void lock_torture_printk(char *page)
280 {
281         bool fail = 0;
282         int i;
283         long max = 0;
284         long min = lwsa[0].n_write_lock_acquired;
285         long long sum = 0;
286
287         for (i = 0; i < nrealwriters_stress; i++) {
288                 if (lwsa[i].n_write_lock_fail)
289                         fail = true;
290                 sum += lwsa[i].n_write_lock_acquired;
291                 if (max < lwsa[i].n_write_lock_fail)
292                         max = lwsa[i].n_write_lock_fail;
293                 if (min > lwsa[i].n_write_lock_fail)
294                         min = lwsa[i].n_write_lock_fail;
295         }
296         page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
297         page += sprintf(page,
298                         "Writes:  Total: %lld  Max/Min: %ld/%ld %s  Fail: %d %s\n",
299                         sum, max, min, max / 2 > min ? "???" : "",
300                         fail, fail ? "!!!" : "");
301         if (fail)
302                 atomic_inc(&n_lock_torture_errors);
303 }
304
305 /*
306  * Print torture statistics.  Caller must ensure that there is only one
307  * call to this function at a given time!!!  This is normally accomplished
308  * by relying on the module system to only have one copy of the module
309  * loaded, and then by giving the lock_torture_stats kthread full control
310  * (or the init/cleanup functions when lock_torture_stats thread is not
311  * running).
312  */
313 static void lock_torture_stats_print(void)
314 {
315         int size = nrealwriters_stress * 200 + 8192;
316         char *buf;
317
318         buf = kmalloc(size, GFP_KERNEL);
319         if (!buf) {
320                 pr_err("lock_torture_stats_print: Out of memory, need: %d",
321                        size);
322                 return;
323         }
324         lock_torture_printk(buf);
325         pr_alert("%s", buf);
326         kfree(buf);
327 }
328
329 /*
330  * Periodically prints torture statistics, if periodic statistics printing
331  * was specified via the stat_interval module parameter.
332  *
333  * No need to worry about fullstop here, since this one doesn't reference
334  * volatile state or register callbacks.
335  */
336 static int lock_torture_stats(void *arg)
337 {
338         VERBOSE_TOROUT_STRING("lock_torture_stats task started");
339         do {
340                 schedule_timeout_interruptible(stat_interval * HZ);
341                 lock_torture_stats_print();
342                 torture_shutdown_absorb("lock_torture_stats");
343         } while (!torture_must_stop());
344         torture_kthread_stopping("lock_torture_stats");
345         return 0;
346 }
347
348 static inline void
349 lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
350                                 const char *tag)
351 {
352         pr_alert("%s" TORTURE_FLAG
353                  "--- %s%s: nwriters_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
354                  torture_type, tag, debug_lock ? " [debug]": "",
355                  nrealwriters_stress, stat_interval, verbose,
356                  shuffle_interval, stutter, shutdown_secs,
357                  onoff_interval, onoff_holdoff);
358 }
359
360 static void lock_torture_cleanup(void)
361 {
362         int i;
363
364         if (torture_cleanup())
365                 return;
366
367         if (writer_tasks) {
368                 for (i = 0; i < nrealwriters_stress; i++)
369                         torture_stop_kthread(lock_torture_writer,
370                                              writer_tasks[i]);
371                 kfree(writer_tasks);
372                 writer_tasks = NULL;
373         }
374
375         torture_stop_kthread(lock_torture_stats, stats_task);
376         lock_torture_stats_print();  /* -After- the stats thread is stopped! */
377
378         if (atomic_read(&n_lock_torture_errors))
379                 lock_torture_print_module_parms(cur_ops,
380                                                 "End of test: FAILURE");
381         else if (torture_onoff_failures())
382                 lock_torture_print_module_parms(cur_ops,
383                                                 "End of test: LOCK_HOTPLUG");
384         else
385                 lock_torture_print_module_parms(cur_ops,
386                                                 "End of test: SUCCESS");
387 }
388
389 static int __init lock_torture_init(void)
390 {
391         int i;
392         int firsterr = 0;
393         static struct lock_torture_ops *torture_ops[] = {
394                 &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops, &mutex_lock_ops,
395         };
396
397         if (!torture_init_begin(torture_type, verbose, &torture_runnable))
398                 return -EBUSY;
399
400         /* Process args and tell the world that the torturer is on the job. */
401         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
402                 cur_ops = torture_ops[i];
403                 if (strcmp(torture_type, cur_ops->name) == 0)
404                         break;
405         }
406         if (i == ARRAY_SIZE(torture_ops)) {
407                 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
408                          torture_type);
409                 pr_alert("lock-torture types:");
410                 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
411                         pr_alert(" %s", torture_ops[i]->name);
412                 pr_alert("\n");
413                 torture_init_end();
414                 return -EINVAL;
415         }
416         if (cur_ops->init)
417                 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
418
419         if (nwriters_stress >= 0)
420                 nrealwriters_stress = nwriters_stress;
421         else
422                 nrealwriters_stress = 2 * num_online_cpus();
423
424 #ifdef CONFIG_DEBUG_MUTEXES
425         if (strncmp(torture_type, "mutex", 5) == 0)
426                 debug_lock = true;
427 #endif
428 #ifdef CONFIG_DEBUG_SPINLOCK
429         if (strncmp(torture_type, "spin", 4) == 0)
430                 debug_lock = true;
431 #endif
432         lock_torture_print_module_parms(cur_ops, "Start of test");
433
434         /* Initialize the statistics so that each run gets its own numbers. */
435
436         lock_is_write_held = 0;
437         lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
438         if (lwsa == NULL) {
439                 VERBOSE_TOROUT_STRING("lwsa: Out of memory");
440                 firsterr = -ENOMEM;
441                 goto unwind;
442         }
443         for (i = 0; i < nrealwriters_stress; i++) {
444                 lwsa[i].n_write_lock_fail = 0;
445                 lwsa[i].n_write_lock_acquired = 0;
446         }
447
448         /* Start up the kthreads. */
449
450         if (onoff_interval > 0) {
451                 firsterr = torture_onoff_init(onoff_holdoff * HZ,
452                                               onoff_interval * HZ);
453                 if (firsterr)
454                         goto unwind;
455         }
456         if (shuffle_interval > 0) {
457                 firsterr = torture_shuffle_init(shuffle_interval);
458                 if (firsterr)
459                         goto unwind;
460         }
461         if (shutdown_secs > 0) {
462                 firsterr = torture_shutdown_init(shutdown_secs,
463                                                  lock_torture_cleanup);
464                 if (firsterr)
465                         goto unwind;
466         }
467         if (stutter > 0) {
468                 firsterr = torture_stutter_init(stutter);
469                 if (firsterr)
470                         goto unwind;
471         }
472
473         writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
474                                GFP_KERNEL);
475         if (writer_tasks == NULL) {
476                 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
477                 firsterr = -ENOMEM;
478                 goto unwind;
479         }
480         for (i = 0; i < nrealwriters_stress; i++) {
481                 firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
482                                                   writer_tasks[i]);
483                 if (firsterr)
484                         goto unwind;
485         }
486         if (stat_interval > 0) {
487                 firsterr = torture_create_kthread(lock_torture_stats, NULL,
488                                                   stats_task);
489                 if (firsterr)
490                         goto unwind;
491         }
492         torture_init_end();
493         return 0;
494
495 unwind:
496         torture_init_end();
497         lock_torture_cleanup();
498         return firsterr;
499 }
500
501 module_init(lock_torture_init);
502 module_exit(lock_torture_cleanup);