blob: a583f1ce713dfcb9ae5ede5502efd4241655e328 [file] [log] [blame]
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001/*
Paul E. McKenney29766f12006-06-27 02:54:02 -07002 * Read-Copy Update module-based torture test facility
Paul E. McKenneya241ec62005-10-30 15:03:12 -08003 *
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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Josh Triplettb772e1d2006-10-04 02:17:13 -070018 * Copyright (C) IBM Corporation, 2005, 2006
Paul E. McKenneya241ec62005-10-30 15:03:12 -080019 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070021 * Josh Triplett <josh@freedesktop.org>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080022 *
23 * See also: Documentation/RCU/torture.txt
24 */
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/kthread.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
32#include <linux/smp.h>
33#include <linux/rcupdate.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080037#include <linux/bitops.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080038#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
Paul E. McKenney343e9092008-12-15 16:13:07 -080042#include <linux/reboot.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070043#include <linux/freezer.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080044#include <linux/cpu.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080045#include <linux/delay.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080046#include <linux/stat.h>
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070047#include <linux/srcu.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070048#include <linux/slab.h>
Paul E. McKenney52494532012-11-14 16:26:40 -080049#include <linux/trace_clock.h>
Harvey Harrisonf07767f2008-10-20 10:23:38 -070050#include <asm/byteorder.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080051
52MODULE_LICENSE("GPL");
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -070053MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080054
Josh Triplett48022112006-10-03 23:26:16 +020055static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
Josh Triplettb772e1d2006-10-04 02:17:13 -070056static int nfakewriters = 4; /* # fake writer threads */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070057static int stat_interval = 60; /* Interval between stats, in seconds. */
58 /* Zero means "only at end of test". */
Rusty Russelld8e8ed92011-12-15 13:43:24 +103059static bool verbose; /* Print more debug info. */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070060static bool test_no_idle_hz = true;
61 /* Test RCU support for tickless idle CPUs. */
Paul E. McKenneyd120f652008-06-18 05:21:44 -070062static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
63static int stutter = 5; /* Start/stop testing interval (in sec) */
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070064static int irqreader = 1; /* RCU readers from irq (timers). */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070065static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
66static int fqs_holdoff; /* Hold time within burst (us). */
Paul E. McKenneybf66f182010-01-04 15:09:10 -080067static int fqs_stutter = 3; /* Wait time between bursts (s). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -080068static int n_barrier_cbs; /* Number of callbacks to test RCU barriers. */
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -080069static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -080070static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070071static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -080072static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
73static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
Paul E. McKenney8e8be452010-09-02 16:16:14 -070074static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
75static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
76static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
Josh Triplett20d2e422006-10-04 02:17:15 -070077static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080078
Josh Triplettd6ad6712007-03-06 01:42:13 -080079module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080080MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080081module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070082MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Paul E. McKenney3721bc12011-07-21 16:00:17 -070083module_param(stat_interval, int, 0644);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080084MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080085module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080086MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080087module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080088MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080089module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080090MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070091module_param(stutter, int, 0444);
92MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070093module_param(irqreader, int, 0444);
94MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Paul E. McKenneybf66f182010-01-04 15:09:10 -080095module_param(fqs_duration, int, 0444);
96MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
97module_param(fqs_holdoff, int, 0444);
98MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
99module_param(fqs_stutter, int, 0444);
100MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800101module_param(n_barrier_cbs, int, 0444);
102MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800103module_param(onoff_interval, int, 0444);
104MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -0800105module_param(onoff_holdoff, int, 0444);
106MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700107module_param(shutdown_secs, int, 0444);
108MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800109module_param(stall_cpu, int, 0444);
110MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
111module_param(stall_cpu_holdoff, int, 0444);
112MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700113module_param(test_boost, int, 0444);
114MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
115module_param(test_boost_interval, int, 0444);
116MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
117module_param(test_boost_duration, int, 0444);
118MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
Josh Triplettd6ad6712007-03-06 01:42:13 -0800119module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700120MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700121
122#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800123#define PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700124 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800125#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700126 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800127#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700128 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800129
130static char printk_buf[4096];
131
132static int nrealreaders;
133static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700134static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800135static struct task_struct **reader_tasks;
136static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800137static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700138static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800139static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700140static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700141static struct task_struct *shutdown_task;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800142#ifdef CONFIG_HOTPLUG_CPU
143static struct task_struct *onoff_task;
144#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800145static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800146static struct task_struct **barrier_cbs_tasks;
147static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800148
149#define RCU_TORTURE_PIPE_LEN 10
150
151struct rcu_torture {
152 struct rcu_head rtort_rcu;
153 int rtort_pipe_count;
154 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800155 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800156};
157
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800158static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700159static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700160static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800161static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
162static DEFINE_SPINLOCK(rcu_torture_lock);
163static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
164 { 0 };
165static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
166 { 0 };
167static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700168static atomic_t n_rcu_torture_alloc;
169static atomic_t n_rcu_torture_alloc_fail;
170static atomic_t n_rcu_torture_free;
171static atomic_t n_rcu_torture_mberror;
172static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800173static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700174static long n_rcu_torture_boost_ktrerror;
175static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700176static long n_rcu_torture_boost_failure;
177static long n_rcu_torture_boosts;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700178static long n_rcu_torture_timers;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800179static long n_offline_attempts;
180static long n_offline_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700181static unsigned long sum_offline;
182static int min_offline = -1;
183static int max_offline;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800184static long n_online_attempts;
185static long n_online_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700186static unsigned long sum_online;
187static int min_online = -1;
188static int max_online;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800189static long n_barrier_attempts;
190static long n_barrier_successes;
Josh Triplette3033732006-10-04 02:17:14 -0700191static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600192static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800193
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700194static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700195
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700196#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
197#define RCUTORTURE_RUNNABLE_INIT 1
198#else
199#define RCUTORTURE_RUNNABLE_INIT 0
200#endif
201int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
Paul E. McKenneybb3bf702011-11-04 13:24:07 -0700202module_param(rcutorture_runnable, int, 0444);
203MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700204
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700205#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700206#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700207#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700208#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700209#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700210
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700211static unsigned long shutdown_time; /* jiffies to system shutdown. */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700212static unsigned long boost_starttime; /* jiffies of next boost test start. */
213DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
214 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800215static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700216static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800217static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
218static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
219static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700220
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800221/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
222
223#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
224#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
225#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
226static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700227/*
228 * Protect fullstop transitions and spawning of kthreads.
229 */
230static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800231
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700232/* Forward reference. */
233static void rcu_torture_cleanup(void);
234
Paul E. McKenney343e9092008-12-15 16:13:07 -0800235/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800236 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800237 */
238static int
239rcutorture_shutdown_notify(struct notifier_block *unused1,
240 unsigned long unused2, void *unused3)
241{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800242 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800243 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800244 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800245 else
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700246 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800247 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800248 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800249 return NOTIFY_DONE;
250}
251
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800252/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800253 * Absorb kthreads into a kernel function that won't return, so that
254 * they won't ever access module text or data again.
255 */
256static void rcutorture_shutdown_absorb(char *title)
257{
258 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700259 pr_notice(
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800260 "rcutorture thread %s parking due to system shutdown\n",
261 title);
262 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
263 }
264}
265
266/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800267 * Allocate an element from the rcu_tortures pool.
268 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800269static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800270rcu_torture_alloc(void)
271{
272 struct list_head *p;
273
Ingo Molnaradac1662006-01-25 19:50:12 +0100274 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800275 if (list_empty(&rcu_torture_freelist)) {
276 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100277 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800278 return NULL;
279 }
280 atomic_inc(&n_rcu_torture_alloc);
281 p = rcu_torture_freelist.next;
282 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100283 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800284 return container_of(p, struct rcu_torture, rtort_free);
285}
286
287/*
288 * Free an element to the rcu_tortures pool.
289 */
290static void
291rcu_torture_free(struct rcu_torture *p)
292{
293 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100294 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800295 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100296 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800297}
298
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800299struct rcu_random_state {
300 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700301 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800302};
303
304#define RCU_RANDOM_MULT 39916801 /* prime */
305#define RCU_RANDOM_ADD 479001701 /* prime */
306#define RCU_RANDOM_REFRESH 10000
307
308#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
309
310/*
311 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700312 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800313 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700314static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800315rcu_random(struct rcu_random_state *rrsp)
316{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800317 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200318 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800319 rrsp->rrs_count = RCU_RANDOM_REFRESH;
320 }
321 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
322 return swahw32(rrsp->rrs_state);
323}
324
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700325static void
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800326rcu_stutter_wait(char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700327{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800328 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700329 if (rcutorture_runnable)
330 schedule_timeout_interruptible(1);
331 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700332 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800333 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800334 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700335}
336
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800337/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700338 * Operations vector for selecting different types of tests.
339 */
340
341struct rcu_torture_ops {
342 void (*init)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700343 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700344 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700345 void (*readunlock)(int idx);
346 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700347 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700348 void (*sync)(void);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800349 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
Paul E. McKenney23269742008-05-12 21:21:05 +0200350 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800351 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700352 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700353 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700354 int can_boost;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700355 char *name;
356};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700357
358static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700359
360/*
361 * Definitions for rcu torture testing.
362 */
363
Josh Tripletta49a4af2006-09-29 01:59:30 -0700364static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700365{
366 rcu_read_lock();
367 return 0;
368}
369
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700370static void rcu_read_delay(struct rcu_random_state *rrsp)
371{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700372 const unsigned long shortdelay_us = 200;
373 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700374
Josh Triplettb8d57a72009-09-08 15:54:35 -0700375 /* We want a short delay sometimes to make a reader delay the grace
376 * period, and we want a long delay occasionally to trigger
377 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700378
Josh Triplettb8d57a72009-09-08 15:54:35 -0700379 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
380 mdelay(longdelay_ms);
381 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
382 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800383#ifdef CONFIG_PREEMPT
384 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
385 preempt_schedule(); /* No QS if preempt_disable() in effect */
386#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700387}
388
Josh Tripletta49a4af2006-09-29 01:59:30 -0700389static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700390{
391 rcu_read_unlock();
392}
393
394static int rcu_torture_completed(void)
395{
396 return rcu_batches_completed();
397}
398
399static void
400rcu_torture_cb(struct rcu_head *p)
401{
402 int i;
403 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
404
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800405 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700406 /* Test is ending, just drop callbacks on the floor. */
407 /* The next initialization will pick up the pieces. */
408 return;
409 }
410 i = rp->rtort_pipe_count;
411 if (i > RCU_TORTURE_PIPE_LEN)
412 i = RCU_TORTURE_PIPE_LEN;
413 atomic_inc(&rcu_torture_wcount[i]);
414 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
415 rp->rtort_mbtest = 0;
416 rcu_torture_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700417 } else {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700418 cur_ops->deferred_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700419 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700420}
421
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800422static int rcu_no_completed(void)
423{
424 return 0;
425}
426
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700427static void rcu_torture_deferred_free(struct rcu_torture *p)
428{
429 call_rcu(&p->rtort_rcu, rcu_torture_cb);
430}
431
432static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700433 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700434 .readlock = rcu_torture_read_lock,
435 .read_delay = rcu_read_delay,
436 .readunlock = rcu_torture_read_unlock,
437 .completed = rcu_torture_completed,
438 .deferred_free = rcu_torture_deferred_free,
439 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800440 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700441 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800442 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700443 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700444 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700445 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700446 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700447};
448
Josh Triplette3033732006-10-04 02:17:14 -0700449static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
450{
451 int i;
452 struct rcu_torture *rp;
453 struct rcu_torture *rp1;
454
455 cur_ops->sync();
456 list_add(&p->rtort_free, &rcu_torture_removed);
457 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
458 i = rp->rtort_pipe_count;
459 if (i > RCU_TORTURE_PIPE_LEN)
460 i = RCU_TORTURE_PIPE_LEN;
461 atomic_inc(&rcu_torture_wcount[i]);
462 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
463 rp->rtort_mbtest = 0;
464 list_del(&rp->rtort_free);
465 rcu_torture_free(rp);
466 }
467 }
468}
469
470static void rcu_sync_torture_init(void)
471{
472 INIT_LIST_HEAD(&rcu_torture_removed);
473}
474
Josh Triplett20d2e422006-10-04 02:17:15 -0700475static struct rcu_torture_ops rcu_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700476 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700477 .readlock = rcu_torture_read_lock,
478 .read_delay = rcu_read_delay,
479 .readunlock = rcu_torture_read_unlock,
480 .completed = rcu_torture_completed,
481 .deferred_free = rcu_sync_torture_deferred_free,
482 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800483 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700484 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800485 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700486 .stats = NULL,
487 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700488 .can_boost = rcu_can_boost(),
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700489 .name = "rcu_sync"
Josh Triplett20d2e422006-10-04 02:17:15 -0700490};
491
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800492static struct rcu_torture_ops rcu_expedited_ops = {
493 .init = rcu_sync_torture_init,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800494 .readlock = rcu_torture_read_lock,
495 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
496 .readunlock = rcu_torture_read_unlock,
497 .completed = rcu_no_completed,
498 .deferred_free = rcu_sync_torture_deferred_free,
499 .sync = synchronize_rcu_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800500 .call = NULL,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800501 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800502 .fqs = rcu_force_quiescent_state,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800503 .stats = NULL,
504 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700505 .can_boost = rcu_can_boost(),
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800506 .name = "rcu_expedited"
507};
508
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700509/*
510 * Definitions for rcu_bh torture testing.
511 */
512
Josh Tripletta49a4af2006-09-29 01:59:30 -0700513static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700514{
515 rcu_read_lock_bh();
516 return 0;
517}
518
Josh Tripletta49a4af2006-09-29 01:59:30 -0700519static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700520{
521 rcu_read_unlock_bh();
522}
523
524static int rcu_bh_torture_completed(void)
525{
526 return rcu_batches_completed_bh();
527}
528
529static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
530{
531 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
532}
533
534static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700535 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700536 .readlock = rcu_bh_torture_read_lock,
537 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
538 .readunlock = rcu_bh_torture_read_unlock,
539 .completed = rcu_bh_torture_completed,
540 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700541 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800542 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700543 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800544 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700545 .stats = NULL,
546 .irq_capable = 1,
547 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700548};
549
Josh Triplett11a14702006-10-04 02:17:16 -0700550static struct rcu_torture_ops rcu_bh_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700551 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700552 .readlock = rcu_bh_torture_read_lock,
553 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
554 .readunlock = rcu_bh_torture_read_unlock,
555 .completed = rcu_bh_torture_completed,
556 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700557 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800558 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700559 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800560 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700561 .stats = NULL,
562 .irq_capable = 1,
563 .name = "rcu_bh_sync"
Josh Triplett11a14702006-10-04 02:17:16 -0700564};
565
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700566static struct rcu_torture_ops rcu_bh_expedited_ops = {
567 .init = rcu_sync_torture_init,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700568 .readlock = rcu_bh_torture_read_lock,
569 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
570 .readunlock = rcu_bh_torture_read_unlock,
571 .completed = rcu_bh_torture_completed,
572 .deferred_free = rcu_sync_torture_deferred_free,
573 .sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800574 .call = NULL,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700575 .cb_barrier = NULL,
576 .fqs = rcu_bh_force_quiescent_state,
577 .stats = NULL,
578 .irq_capable = 1,
579 .name = "rcu_bh_expedited"
580};
581
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700582/*
583 * Definitions for srcu torture testing.
584 */
585
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800586DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700587
Josh Triplett012d3ca2006-12-06 20:40:19 -0800588static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700589{
590 return srcu_read_lock(&srcu_ctl);
591}
592
593static void srcu_read_delay(struct rcu_random_state *rrsp)
594{
595 long delay;
596 const long uspertick = 1000000 / HZ;
597 const long longdelay = 10;
598
599 /* We want there to be long-running readers, but not all the time. */
600
601 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
602 if (!delay)
603 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800604 else
605 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700606}
607
Josh Triplett012d3ca2006-12-06 20:40:19 -0800608static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700609{
610 srcu_read_unlock(&srcu_ctl, idx);
611}
612
613static int srcu_torture_completed(void)
614{
615 return srcu_batches_completed(&srcu_ctl);
616}
617
Lai Jiangshan9059c942012-03-19 16:12:14 +0800618static void srcu_torture_deferred_free(struct rcu_torture *rp)
619{
620 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
621}
622
Josh Triplettb772e1d2006-10-04 02:17:13 -0700623static void srcu_torture_synchronize(void)
624{
625 synchronize_srcu(&srcu_ctl);
626}
627
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700628static void srcu_torture_call(struct rcu_head *head,
629 void (*func)(struct rcu_head *head))
630{
631 call_srcu(&srcu_ctl, head, func);
632}
633
634static void srcu_torture_barrier(void)
635{
636 srcu_barrier(&srcu_ctl);
637}
638
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700639static int srcu_torture_stats(char *page)
640{
641 int cnt = 0;
642 int cpu;
643 int idx = srcu_ctl.completed & 0x1;
644
645 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
646 torture_type, TORTURE_FLAG, idx);
647 for_each_possible_cpu(cpu) {
Paul E. McKenneycef50122012-02-05 07:42:44 -0800648 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700649 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
650 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
651 }
652 cnt += sprintf(&page[cnt], "\n");
653 return cnt;
654}
655
656static struct rcu_torture_ops srcu_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800657 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700658 .readlock = srcu_torture_read_lock,
659 .read_delay = srcu_read_delay,
660 .readunlock = srcu_torture_read_unlock,
661 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800662 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700663 .sync = srcu_torture_synchronize,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700664 .call = srcu_torture_call,
665 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700666 .stats = srcu_torture_stats,
667 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700668};
669
Lai Jiangshan9059c942012-03-19 16:12:14 +0800670static struct rcu_torture_ops srcu_sync_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800671 .init = rcu_sync_torture_init,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800672 .readlock = srcu_torture_read_lock,
673 .read_delay = srcu_read_delay,
674 .readunlock = srcu_torture_read_unlock,
675 .completed = srcu_torture_completed,
676 .deferred_free = rcu_sync_torture_deferred_free,
677 .sync = srcu_torture_synchronize,
678 .call = NULL,
679 .cb_barrier = NULL,
680 .stats = srcu_torture_stats,
681 .name = "srcu_sync"
682};
683
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800684static int srcu_torture_read_lock_raw(void) __acquires(&srcu_ctl)
685{
686 return srcu_read_lock_raw(&srcu_ctl);
687}
688
689static void srcu_torture_read_unlock_raw(int idx) __releases(&srcu_ctl)
690{
691 srcu_read_unlock_raw(&srcu_ctl, idx);
692}
693
694static struct rcu_torture_ops srcu_raw_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800695 .init = rcu_sync_torture_init,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800696 .readlock = srcu_torture_read_lock_raw,
697 .read_delay = srcu_read_delay,
698 .readunlock = srcu_torture_read_unlock_raw,
699 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800700 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800701 .sync = srcu_torture_synchronize,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800702 .call = NULL,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800703 .cb_barrier = NULL,
704 .stats = srcu_torture_stats,
705 .name = "srcu_raw"
706};
707
Lai Jiangshan9059c942012-03-19 16:12:14 +0800708static struct rcu_torture_ops srcu_raw_sync_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800709 .init = rcu_sync_torture_init,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800710 .readlock = srcu_torture_read_lock_raw,
711 .read_delay = srcu_read_delay,
712 .readunlock = srcu_torture_read_unlock_raw,
713 .completed = srcu_torture_completed,
714 .deferred_free = rcu_sync_torture_deferred_free,
715 .sync = srcu_torture_synchronize,
716 .call = NULL,
717 .cb_barrier = NULL,
718 .stats = srcu_torture_stats,
719 .name = "srcu_raw_sync"
720};
721
Paul E. McKenney804bb832009-10-25 19:03:52 -0700722static void srcu_torture_synchronize_expedited(void)
723{
724 synchronize_srcu_expedited(&srcu_ctl);
725}
726
727static struct rcu_torture_ops srcu_expedited_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800728 .init = rcu_sync_torture_init,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700729 .readlock = srcu_torture_read_lock,
730 .read_delay = srcu_read_delay,
731 .readunlock = srcu_torture_read_unlock,
732 .completed = srcu_torture_completed,
733 .deferred_free = rcu_sync_torture_deferred_free,
734 .sync = srcu_torture_synchronize_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800735 .call = NULL,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700736 .cb_barrier = NULL,
737 .stats = srcu_torture_stats,
738 .name = "srcu_expedited"
739};
740
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700741/*
742 * Definitions for sched torture testing.
743 */
744
745static int sched_torture_read_lock(void)
746{
747 preempt_disable();
748 return 0;
749}
750
751static void sched_torture_read_unlock(int idx)
752{
753 preempt_enable();
754}
755
Paul E. McKenney23269742008-05-12 21:21:05 +0200756static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
757{
758 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
759}
760
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700761static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700762 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700763 .readlock = sched_torture_read_lock,
764 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
765 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800766 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700767 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700768 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700769 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800770 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700771 .stats = NULL,
772 .irq_capable = 1,
773 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700774};
775
Paul E. McKenney804bb832009-10-25 19:03:52 -0700776static struct rcu_torture_ops sched_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700777 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700778 .readlock = sched_torture_read_lock,
779 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
780 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800781 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700782 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700783 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700784 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800785 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700786 .stats = NULL,
787 .name = "sched_sync"
788};
789
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700790static struct rcu_torture_ops sched_expedited_ops = {
791 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700792 .readlock = sched_torture_read_lock,
793 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
794 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800795 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700796 .deferred_free = rcu_sync_torture_deferred_free,
797 .sync = synchronize_sched_expedited,
798 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800799 .fqs = rcu_sched_force_quiescent_state,
Tejun Heo969c7922010-05-06 18:49:21 +0200800 .stats = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700801 .irq_capable = 1,
802 .name = "sched_expedited"
Paul E. McKenney23269742008-05-12 21:21:05 +0200803};
804
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700805/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700806 * RCU torture priority-boost testing. Runs one real-time thread per
807 * CPU for moderate bursts, repeatedly registering RCU callbacks and
808 * spinning waiting for them to be invoked. If a given callback takes
809 * too long to be invoked, we assume that priority inversion has occurred.
810 */
811
812struct rcu_boost_inflight {
813 struct rcu_head rcu;
814 int inflight;
815};
816
817static void rcu_torture_boost_cb(struct rcu_head *head)
818{
819 struct rcu_boost_inflight *rbip =
820 container_of(head, struct rcu_boost_inflight, rcu);
821
822 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
823 rbip->inflight = 0;
824}
825
826static int rcu_torture_boost(void *arg)
827{
828 unsigned long call_rcu_time;
829 unsigned long endtime;
830 unsigned long oldstarttime;
831 struct rcu_boost_inflight rbi = { .inflight = 0 };
832 struct sched_param sp;
833
834 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
835
836 /* Set real-time priority. */
837 sp.sched_priority = 1;
838 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
839 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
840 n_rcu_torture_boost_rterror++;
841 }
842
Paul E. McKenney561190e2011-03-30 09:10:44 -0700843 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700844 /* Each pass through the following loop does one boost-test cycle. */
845 do {
846 /* Wait for the next test interval. */
847 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700848 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700849 schedule_timeout_uninterruptible(1);
850 rcu_stutter_wait("rcu_torture_boost");
851 if (kthread_should_stop() ||
852 fullstop != FULLSTOP_DONTSTOP)
853 goto checkwait;
854 }
855
856 /* Do one boost-test interval. */
857 endtime = oldstarttime + test_boost_duration * HZ;
858 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700859 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700860 /* If we don't have a callback in flight, post one. */
861 if (!rbi.inflight) {
862 smp_mb(); /* RCU core before ->inflight = 1. */
863 rbi.inflight = 1;
864 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
865 if (jiffies - call_rcu_time >
866 test_boost_duration * HZ - HZ / 2) {
867 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
868 n_rcu_torture_boost_failure++;
869 }
870 call_rcu_time = jiffies;
871 }
872 cond_resched();
873 rcu_stutter_wait("rcu_torture_boost");
874 if (kthread_should_stop() ||
875 fullstop != FULLSTOP_DONTSTOP)
876 goto checkwait;
877 }
878
879 /*
880 * Set the start time of the next test interval.
881 * Yes, this is vulnerable to long delays, but such
882 * delays simply cause a false negative for the next
883 * interval. Besides, we are running at RT priority,
884 * so delays should be relatively rare.
885 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700886 while (oldstarttime == boost_starttime &&
887 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700888 if (mutex_trylock(&boost_mutex)) {
889 boost_starttime = jiffies +
890 test_boost_interval * HZ;
891 n_rcu_torture_boosts++;
892 mutex_unlock(&boost_mutex);
893 break;
894 }
895 schedule_timeout_uninterruptible(1);
896 }
897
898 /* Go do the stutter. */
899checkwait: rcu_stutter_wait("rcu_torture_boost");
900 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
901
902 /* Clean up and exit. */
903 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
904 rcutorture_shutdown_absorb("rcu_torture_boost");
905 while (!kthread_should_stop() || rbi.inflight)
906 schedule_timeout_uninterruptible(1);
907 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
Paul E. McKenney9d681972011-06-21 01:48:03 -0700908 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700909 return 0;
910}
911
912/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800913 * RCU torture force-quiescent-state kthread. Repeatedly induces
914 * bursts of calls to force_quiescent_state(), increasing the probability
915 * of occurrence of some important types of race conditions.
916 */
917static int
918rcu_torture_fqs(void *arg)
919{
920 unsigned long fqs_resume_time;
921 int fqs_burst_remaining;
922
923 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
924 do {
925 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700926 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
927 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800928 schedule_timeout_interruptible(1);
929 }
930 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700931 while (fqs_burst_remaining > 0 &&
932 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800933 cur_ops->fqs();
934 udelay(fqs_holdoff);
935 fqs_burst_remaining -= fqs_holdoff;
936 }
937 rcu_stutter_wait("rcu_torture_fqs");
938 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
939 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
940 rcutorture_shutdown_absorb("rcu_torture_fqs");
941 while (!kthread_should_stop())
942 schedule_timeout_uninterruptible(1);
943 return 0;
944}
945
946/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800947 * RCU torture writer kthread. Repeatedly substitutes a new structure
948 * for that pointed to by rcu_torture_current, freeing the old structure
949 * after a series of grace periods (the "pipeline").
950 */
951static int
952rcu_torture_writer(void *arg)
953{
954 int i;
955 long oldbatch = rcu_batches_completed();
956 struct rcu_torture *rp;
957 struct rcu_torture *old_rp;
958 static DEFINE_RCU_RANDOM(rand);
959
960 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800961 set_user_nice(current, 19);
962
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800963 do {
964 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700965 rp = rcu_torture_alloc();
966 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800967 continue;
968 rp->rtort_pipe_count = 0;
969 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700970 old_rp = rcu_dereference_check(rcu_torture_current,
971 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800972 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800973 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700974 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700975 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800976 i = old_rp->rtort_pipe_count;
977 if (i > RCU_TORTURE_PIPE_LEN)
978 i = RCU_TORTURE_PIPE_LEN;
979 atomic_inc(&rcu_torture_wcount[i]);
980 old_rp->rtort_pipe_count++;
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700981 cur_ops->deferred_free(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800982 }
Paul E. McKenney4a298652011-04-03 21:33:51 -0700983 rcutorture_record_progress(++rcu_torture_current_version);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700984 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800985 rcu_stutter_wait("rcu_torture_writer");
986 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800987 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800988 rcutorture_shutdown_absorb("rcu_torture_writer");
989 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800990 schedule_timeout_uninterruptible(1);
991 return 0;
992}
993
994/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700995 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
996 * delay between calls.
997 */
998static int
999rcu_torture_fakewriter(void *arg)
1000{
1001 DEFINE_RCU_RANDOM(rand);
1002
1003 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
1004 set_user_nice(current, 19);
1005
1006 do {
1007 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
1008 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -07001009 if (cur_ops->cb_barrier != NULL &&
1010 rcu_random(&rand) % (nfakewriters * 8) == 0)
1011 cur_ops->cb_barrier();
1012 else
1013 cur_ops->sync();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001014 rcu_stutter_wait("rcu_torture_fakewriter");
1015 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001016
1017 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001018 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
1019 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -07001020 schedule_timeout_uninterruptible(1);
1021 return 0;
1022}
1023
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001024void rcutorture_trace_dump(void)
1025{
1026 static atomic_t beenhere = ATOMIC_INIT(0);
1027
1028 if (atomic_read(&beenhere))
1029 return;
1030 if (atomic_xchg(&beenhere, 1) != 0)
1031 return;
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001032 ftrace_dump(DUMP_ALL);
1033}
1034
Josh Triplettb772e1d2006-10-04 02:17:13 -07001035/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001036 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1037 * incrementing the corresponding element of the pipeline array. The
1038 * counter in the element should never be greater than 1, otherwise, the
1039 * RCU implementation is broken.
1040 */
1041static void rcu_torture_timer(unsigned long unused)
1042{
1043 int idx;
1044 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -08001045 int completed_end;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001046 static DEFINE_RCU_RANDOM(rand);
1047 static DEFINE_SPINLOCK(rand_lock);
1048 struct rcu_torture *p;
1049 int pipe_count;
Paul E. McKenney52494532012-11-14 16:26:40 -08001050 unsigned long long ts;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001051
1052 idx = cur_ops->readlock();
1053 completed = cur_ops->completed();
Paul E. McKenney52494532012-11-14 16:26:40 -08001054 ts = trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001055 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001056 rcu_read_lock_bh_held() ||
1057 rcu_read_lock_sched_held() ||
1058 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001059 if (p == NULL) {
1060 /* Leave because rcu_torture_writer is not yet underway */
1061 cur_ops->readunlock(idx);
1062 return;
1063 }
1064 if (p->rtort_mbtest == 0)
1065 atomic_inc(&n_rcu_torture_mberror);
1066 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001067 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001068 n_rcu_torture_timers++;
1069 spin_unlock(&rand_lock);
1070 preempt_disable();
1071 pipe_count = p->rtort_pipe_count;
1072 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1073 /* Should not happen, but... */
1074 pipe_count = RCU_TORTURE_PIPE_LEN;
1075 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001076 completed_end = cur_ops->completed();
1077 if (pipe_count > 1) {
1078 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
1079
1080 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
1081 completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001082 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001083 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001084 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001085 completed = completed_end - completed;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001086 if (completed > RCU_TORTURE_PIPE_LEN) {
1087 /* Should not happen, but... */
1088 completed = RCU_TORTURE_PIPE_LEN;
1089 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001090 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001091 preempt_enable();
1092 cur_ops->readunlock(idx);
1093}
1094
1095/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001096 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1097 * incrementing the corresponding element of the pipeline array. The
1098 * counter in the element should never be greater than 1, otherwise, the
1099 * RCU implementation is broken.
1100 */
1101static int
1102rcu_torture_reader(void *arg)
1103{
1104 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -08001105 int completed_end;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001106 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001107 DEFINE_RCU_RANDOM(rand);
1108 struct rcu_torture *p;
1109 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001110 struct timer_list t;
Paul E. McKenney52494532012-11-14 16:26:40 -08001111 unsigned long long ts;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001112
1113 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001114 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001115 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001116 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001117
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001118 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001119 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001120 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001121 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001122 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001123 idx = cur_ops->readlock();
1124 completed = cur_ops->completed();
Paul E. McKenney52494532012-11-14 16:26:40 -08001125 ts = trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001126 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001127 rcu_read_lock_bh_held() ||
1128 rcu_read_lock_sched_held() ||
1129 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001130 if (p == NULL) {
1131 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001132 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001133 schedule_timeout_interruptible(HZ);
1134 continue;
1135 }
Paul E. McKenney996417d2005-11-18 01:10:50 -08001136 if (p->rtort_mbtest == 0)
1137 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001138 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001139 preempt_disable();
1140 pipe_count = p->rtort_pipe_count;
1141 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1142 /* Should not happen, but... */
1143 pipe_count = RCU_TORTURE_PIPE_LEN;
1144 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001145 completed_end = cur_ops->completed();
1146 if (pipe_count > 1) {
1147 unsigned long __maybe_unused ts_rem =
1148 do_div(ts, NSEC_PER_USEC);
1149
1150 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1151 ts, completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001152 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001153 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001154 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001155 completed = completed_end - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001156 if (completed > RCU_TORTURE_PIPE_LEN) {
1157 /* Should not happen, but... */
1158 completed = RCU_TORTURE_PIPE_LEN;
1159 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001160 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001161 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001162 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001163 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001164 rcu_stutter_wait("rcu_torture_reader");
1165 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001166 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001167 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001168 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001169 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001170 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001171 schedule_timeout_uninterruptible(1);
1172 return 0;
1173}
1174
1175/*
1176 * Create an RCU-torture statistics message in the specified buffer.
1177 */
1178static int
1179rcu_torture_printk(char *page)
1180{
1181 int cnt = 0;
1182 int cpu;
1183 int i;
1184 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1185 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1186
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001187 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001188 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1189 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1190 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1191 }
1192 }
1193 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1194 if (pipesummary[i] != 0)
1195 break;
1196 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001197 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001198 cnt += sprintf(&page[cnt],
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001199 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001200 rcu_torture_current,
1201 rcu_torture_current_version,
1202 list_empty(&rcu_torture_freelist),
1203 atomic_read(&n_rcu_torture_alloc),
1204 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001205 atomic_read(&n_rcu_torture_free));
1206 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001207 atomic_read(&n_rcu_torture_mberror),
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001208 n_rcu_torture_boost_ktrerror,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001209 n_rcu_torture_boost_rterror);
1210 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001211 n_rcu_torture_boost_failure,
1212 n_rcu_torture_boosts,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001213 n_rcu_torture_timers);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001214 cnt += sprintf(&page[cnt],
1215 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1216 n_online_successes, n_online_attempts,
1217 n_offline_successes, n_offline_attempts,
1218 min_online, max_online,
1219 min_offline, max_offline,
1220 sum_online, sum_offline, HZ);
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001221 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001222 n_barrier_successes,
1223 n_barrier_attempts,
1224 n_rcu_torture_barrier_error);
1225 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001226 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001227 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001228 n_rcu_torture_boost_ktrerror != 0 ||
1229 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001230 n_rcu_torture_boost_failure != 0 ||
1231 i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001232 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001233 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001234 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001235 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001236 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1237 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1238 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001239 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001240 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001241 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001242 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001243 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001244 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1245 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1246 cnt += sprintf(&page[cnt], " %d",
1247 atomic_read(&rcu_torture_wcount[i]));
1248 }
1249 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -07001250 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001251 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001252 return cnt;
1253}
1254
1255/*
1256 * Print torture statistics. Caller must ensure that there is only
1257 * one call to this function at a given time!!! This is normally
1258 * accomplished by relying on the module system to only have one copy
1259 * of the module loaded, and then by giving the rcu_torture_stats
1260 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1261 * thread is not running).
1262 */
1263static void
1264rcu_torture_stats_print(void)
1265{
1266 int cnt;
1267
1268 cnt = rcu_torture_printk(printk_buf);
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001269 pr_alert("%s", printk_buf);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001270}
1271
1272/*
1273 * Periodically prints torture statistics, if periodic statistics printing
1274 * was specified via the stat_interval module parameter.
1275 *
1276 * No need to worry about fullstop here, since this one doesn't reference
1277 * volatile state or register callbacks.
1278 */
1279static int
1280rcu_torture_stats(void *arg)
1281{
1282 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1283 do {
1284 schedule_timeout_interruptible(stat_interval * HZ);
1285 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001286 rcutorture_shutdown_absorb("rcu_torture_stats");
1287 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001288 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1289 return 0;
1290}
1291
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001292static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1293
1294/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1295 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1296 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001297static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001298{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001299 int i;
1300
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001301 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001302 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001303
1304 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001305 if (num_online_cpus() == 1) {
1306 put_online_cpus();
1307 return;
1308 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001309
1310 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001311 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001312
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001313 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001314
Josh Triplettc8e5b162007-05-08 00:33:20 -07001315 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001316 for (i = 0; i < nrealreaders; i++)
1317 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001318 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001319 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001320 }
1321
Josh Triplettc8e5b162007-05-08 00:33:20 -07001322 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001323 for (i = 0; i < nfakewriters; i++)
1324 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001325 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001326 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001327 }
1328
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001329 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001330 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001331
1332 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001333 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001334
1335 if (rcu_idle_cpu == -1)
1336 rcu_idle_cpu = num_online_cpus() - 1;
1337 else
1338 rcu_idle_cpu--;
1339
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001340 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001341}
1342
1343/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1344 * system to become idle at a time and cut off its timer ticks. This is meant
1345 * to test the support for such tickless idle CPU in RCU.
1346 */
1347static int
1348rcu_torture_shuffle(void *arg)
1349{
1350 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1351 do {
1352 schedule_timeout_interruptible(shuffle_interval * HZ);
1353 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001354 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1355 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001356 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1357 return 0;
1358}
1359
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001360/* Cause the rcutorture test to "stutter", starting and stopping all
1361 * threads periodically.
1362 */
1363static int
1364rcu_torture_stutter(void *arg)
1365{
1366 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1367 do {
1368 schedule_timeout_interruptible(stutter * HZ);
1369 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001370 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001371 schedule_timeout_interruptible(stutter * HZ);
1372 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001373 rcutorture_shutdown_absorb("rcu_torture_stutter");
1374 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001375 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1376 return 0;
1377}
1378
Paul E. McKenney95c38322006-03-24 03:15:58 -08001379static inline void
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001380rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001381{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001382 pr_alert("%s" TORTURE_FLAG
1383 "--- %s: nreaders=%d nfakewriters=%d "
1384 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1385 "shuffle_interval=%d stutter=%d irqreader=%d "
1386 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1387 "test_boost=%d/%d test_boost_interval=%d "
1388 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001389 "stall_cpu=%d stall_cpu_holdoff=%d "
1390 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001391 "onoff_interval=%d onoff_holdoff=%d\n",
1392 torture_type, tag, nrealreaders, nfakewriters,
1393 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1394 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1395 test_boost, cur_ops->can_boost,
1396 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001397 stall_cpu, stall_cpu_holdoff,
1398 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001399 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001400}
1401
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001402static struct notifier_block rcutorture_shutdown_nb = {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001403 .notifier_call = rcutorture_shutdown_notify,
1404};
1405
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001406static void rcutorture_booster_cleanup(int cpu)
1407{
1408 struct task_struct *t;
1409
1410 if (boost_tasks[cpu] == NULL)
1411 return;
1412 mutex_lock(&boost_mutex);
1413 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1414 t = boost_tasks[cpu];
1415 boost_tasks[cpu] = NULL;
1416 mutex_unlock(&boost_mutex);
1417
1418 /* This must be outside of the mutex, otherwise deadlock! */
1419 kthread_stop(t);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001420 boost_tasks[cpu] = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001421}
1422
1423static int rcutorture_booster_init(int cpu)
1424{
1425 int retval;
1426
1427 if (boost_tasks[cpu] != NULL)
1428 return 0; /* Already created, nothing more to do. */
1429
1430 /* Don't allow time recalculation while creating a new task. */
1431 mutex_lock(&boost_mutex);
1432 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001433 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1434 cpu_to_node(cpu),
1435 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001436 if (IS_ERR(boost_tasks[cpu])) {
1437 retval = PTR_ERR(boost_tasks[cpu]);
1438 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1439 n_rcu_torture_boost_ktrerror++;
1440 boost_tasks[cpu] = NULL;
1441 mutex_unlock(&boost_mutex);
1442 return retval;
1443 }
1444 kthread_bind(boost_tasks[cpu], cpu);
1445 wake_up_process(boost_tasks[cpu]);
1446 mutex_unlock(&boost_mutex);
1447 return 0;
1448}
1449
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001450/*
1451 * Cause the rcutorture test to shutdown the system after the test has
1452 * run for the time specified by the shutdown_secs module parameter.
1453 */
1454static int
1455rcu_torture_shutdown(void *arg)
1456{
1457 long delta;
1458 unsigned long jiffies_snap;
1459
1460 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1461 jiffies_snap = ACCESS_ONCE(jiffies);
1462 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1463 !kthread_should_stop()) {
1464 delta = shutdown_time - jiffies_snap;
1465 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001466 pr_alert("%s" TORTURE_FLAG
1467 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1468 torture_type, delta);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001469 schedule_timeout_interruptible(delta);
1470 jiffies_snap = ACCESS_ONCE(jiffies);
1471 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001472 if (kthread_should_stop()) {
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001473 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1474 return 0;
1475 }
1476
1477 /* OK, shut down the system. */
1478
1479 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1480 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1481 rcu_torture_cleanup(); /* Get the success/failure message. */
1482 kernel_power_off(); /* Shut down the system. */
1483 return 0;
1484}
1485
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001486#ifdef CONFIG_HOTPLUG_CPU
1487
1488/*
1489 * Execute random CPU-hotplug operations at the interval specified
1490 * by the onoff_interval.
1491 */
Heiko Carstens44100302011-12-27 15:04:26 +01001492static int __cpuinit
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001493rcu_torture_onoff(void *arg)
1494{
1495 int cpu;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001496 unsigned long delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001497 int maxcpu = -1;
1498 DEFINE_RCU_RANDOM(rand);
Paul E. McKenney48983262012-09-24 16:08:31 -07001499 int ret;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001500 unsigned long starttime;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001501
1502 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1503 for_each_online_cpu(cpu)
1504 maxcpu = cpu;
1505 WARN_ON(maxcpu < 0);
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -08001506 if (onoff_holdoff > 0) {
1507 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1508 schedule_timeout_interruptible(onoff_holdoff * HZ);
1509 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1510 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001511 while (!kthread_should_stop()) {
1512 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001513 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001514 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001515 pr_alert("%s" TORTURE_FLAG
1516 "rcu_torture_onoff task: offlining %d\n",
1517 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001518 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001519 n_offline_attempts++;
Paul E. McKenney48983262012-09-24 16:08:31 -07001520 ret = cpu_down(cpu);
1521 if (ret) {
1522 if (verbose)
1523 pr_alert("%s" TORTURE_FLAG
1524 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1525 torture_type, cpu, ret);
1526 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001527 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001528 pr_alert("%s" TORTURE_FLAG
1529 "rcu_torture_onoff task: offlined %d\n",
1530 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001531 n_offline_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001532 delta = jiffies - starttime;
1533 sum_offline += delta;
1534 if (min_offline < 0) {
1535 min_offline = delta;
1536 max_offline = delta;
1537 }
1538 if (min_offline > delta)
1539 min_offline = delta;
1540 if (max_offline < delta)
1541 max_offline = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001542 }
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001543 } else if (cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001544 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001545 pr_alert("%s" TORTURE_FLAG
1546 "rcu_torture_onoff task: onlining %d\n",
1547 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001548 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001549 n_online_attempts++;
1550 if (cpu_up(cpu) == 0) {
1551 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001552 pr_alert("%s" TORTURE_FLAG
1553 "rcu_torture_onoff task: onlined %d\n",
1554 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001555 n_online_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001556 delta = jiffies - starttime;
1557 sum_online += delta;
1558 if (min_online < 0) {
1559 min_online = delta;
1560 max_online = delta;
1561 }
1562 if (min_online > delta)
1563 min_online = delta;
1564 if (max_online < delta)
1565 max_online = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001566 }
1567 }
1568 schedule_timeout_interruptible(onoff_interval * HZ);
1569 }
1570 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1571 return 0;
1572}
1573
Heiko Carstens44100302011-12-27 15:04:26 +01001574static int __cpuinit
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001575rcu_torture_onoff_init(void)
1576{
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001577 int ret;
1578
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001579 if (onoff_interval <= 0)
1580 return 0;
1581 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1582 if (IS_ERR(onoff_task)) {
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001583 ret = PTR_ERR(onoff_task);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001584 onoff_task = NULL;
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001585 return ret;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001586 }
1587 return 0;
1588}
1589
1590static void rcu_torture_onoff_cleanup(void)
1591{
1592 if (onoff_task == NULL)
1593 return;
1594 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1595 kthread_stop(onoff_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001596 onoff_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001597}
1598
1599#else /* #ifdef CONFIG_HOTPLUG_CPU */
1600
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001601static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001602rcu_torture_onoff_init(void)
1603{
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001604 return 0;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001605}
1606
1607static void rcu_torture_onoff_cleanup(void)
1608{
1609}
1610
1611#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1612
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001613/*
1614 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1615 * induces a CPU stall for the time specified by stall_cpu.
1616 */
1617static int __cpuinit rcu_torture_stall(void *args)
1618{
1619 unsigned long stop_at;
1620
1621 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1622 if (stall_cpu_holdoff > 0) {
1623 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1624 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1625 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1626 }
1627 if (!kthread_should_stop()) {
1628 stop_at = get_seconds() + stall_cpu;
1629 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001630 pr_alert("rcu_torture_stall start.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001631 rcu_read_lock();
1632 preempt_disable();
1633 while (ULONG_CMP_LT(get_seconds(), stop_at))
1634 continue; /* Induce RCU CPU stall warning. */
1635 preempt_enable();
1636 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001637 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001638 }
1639 rcutorture_shutdown_absorb("rcu_torture_stall");
1640 while (!kthread_should_stop())
1641 schedule_timeout_interruptible(10 * HZ);
1642 return 0;
1643}
1644
1645/* Spawn CPU-stall kthread, if stall_cpu specified. */
1646static int __init rcu_torture_stall_init(void)
1647{
1648 int ret;
1649
1650 if (stall_cpu <= 0)
1651 return 0;
1652 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1653 if (IS_ERR(stall_task)) {
1654 ret = PTR_ERR(stall_task);
1655 stall_task = NULL;
1656 return ret;
1657 }
1658 return 0;
1659}
1660
1661/* Clean up after the CPU-stall kthread, if one was spawned. */
1662static void rcu_torture_stall_cleanup(void)
1663{
1664 if (stall_task == NULL)
1665 return;
1666 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1667 kthread_stop(stall_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001668 stall_task = NULL;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001669}
1670
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001671/* Callback function for RCU barrier testing. */
1672void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1673{
1674 atomic_inc(&barrier_cbs_invoked);
1675}
1676
1677/* kthread function to register callbacks used to test RCU barriers. */
1678static int rcu_torture_barrier_cbs(void *arg)
1679{
1680 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001681 bool lastphase = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001682 struct rcu_head rcu;
1683
1684 init_rcu_head_on_stack(&rcu);
1685 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1686 set_user_nice(current, 19);
1687 do {
1688 wait_event(barrier_cbs_wq[myid],
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001689 barrier_phase != lastphase ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001690 kthread_should_stop() ||
1691 fullstop != FULLSTOP_DONTSTOP);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001692 lastphase = barrier_phase;
1693 smp_mb(); /* ensure barrier_phase load before ->call(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001694 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1695 break;
1696 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1697 if (atomic_dec_and_test(&barrier_cbs_count))
1698 wake_up(&barrier_wq);
1699 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1700 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1701 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1702 while (!kthread_should_stop())
1703 schedule_timeout_interruptible(1);
1704 cur_ops->cb_barrier();
1705 destroy_rcu_head_on_stack(&rcu);
1706 return 0;
1707}
1708
1709/* kthread function to drive and coordinate RCU barrier testing. */
1710static int rcu_torture_barrier(void *arg)
1711{
1712 int i;
1713
1714 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1715 do {
1716 atomic_set(&barrier_cbs_invoked, 0);
1717 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001718 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1719 barrier_phase = !barrier_phase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001720 for (i = 0; i < n_barrier_cbs; i++)
1721 wake_up(&barrier_cbs_wq[i]);
1722 wait_event(barrier_wq,
1723 atomic_read(&barrier_cbs_count) == 0 ||
1724 kthread_should_stop() ||
1725 fullstop != FULLSTOP_DONTSTOP);
1726 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1727 break;
1728 n_barrier_attempts++;
1729 cur_ops->cb_barrier();
1730 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1731 n_rcu_torture_barrier_error++;
1732 WARN_ON_ONCE(1);
1733 }
1734 n_barrier_successes++;
1735 schedule_timeout_interruptible(HZ / 10);
1736 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1737 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
Paul E. McKenney143aa672012-05-24 18:17:48 -07001738 rcutorture_shutdown_absorb("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001739 while (!kthread_should_stop())
1740 schedule_timeout_interruptible(1);
1741 return 0;
1742}
1743
1744/* Initialize RCU barrier testing. */
1745static int rcu_torture_barrier_init(void)
1746{
1747 int i;
1748 int ret;
1749
1750 if (n_barrier_cbs == 0)
1751 return 0;
1752 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001753 pr_alert("%s" TORTURE_FLAG
1754 " Call or barrier ops missing for %s,\n",
1755 torture_type, cur_ops->name);
1756 pr_alert("%s" TORTURE_FLAG
1757 " RCU barrier testing omitted from run.\n",
1758 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001759 return 0;
1760 }
1761 atomic_set(&barrier_cbs_count, 0);
1762 atomic_set(&barrier_cbs_invoked, 0);
1763 barrier_cbs_tasks =
1764 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1765 GFP_KERNEL);
1766 barrier_cbs_wq =
1767 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1768 GFP_KERNEL);
1769 if (barrier_cbs_tasks == NULL || barrier_cbs_wq == 0)
1770 return -ENOMEM;
1771 for (i = 0; i < n_barrier_cbs; i++) {
1772 init_waitqueue_head(&barrier_cbs_wq[i]);
1773 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
Lai Jiangshan9059c942012-03-19 16:12:14 +08001774 (void *)(long)i,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001775 "rcu_torture_barrier_cbs");
1776 if (IS_ERR(barrier_cbs_tasks[i])) {
1777 ret = PTR_ERR(barrier_cbs_tasks[i]);
1778 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1779 barrier_cbs_tasks[i] = NULL;
1780 return ret;
1781 }
1782 }
1783 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1784 "rcu_torture_barrier");
1785 if (IS_ERR(barrier_task)) {
1786 ret = PTR_ERR(barrier_task);
1787 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1788 barrier_task = NULL;
1789 }
1790 return 0;
1791}
1792
1793/* Clean up after RCU barrier testing. */
1794static void rcu_torture_barrier_cleanup(void)
1795{
1796 int i;
1797
1798 if (barrier_task != NULL) {
1799 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1800 kthread_stop(barrier_task);
1801 barrier_task = NULL;
1802 }
1803 if (barrier_cbs_tasks != NULL) {
1804 for (i = 0; i < n_barrier_cbs; i++) {
1805 if (barrier_cbs_tasks[i] != NULL) {
1806 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1807 kthread_stop(barrier_cbs_tasks[i]);
1808 barrier_cbs_tasks[i] = NULL;
1809 }
1810 }
1811 kfree(barrier_cbs_tasks);
1812 barrier_cbs_tasks = NULL;
1813 }
1814 if (barrier_cbs_wq != NULL) {
1815 kfree(barrier_cbs_wq);
1816 barrier_cbs_wq = NULL;
1817 }
1818}
1819
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001820static int rcutorture_cpu_notify(struct notifier_block *self,
1821 unsigned long action, void *hcpu)
1822{
1823 long cpu = (long)hcpu;
1824
1825 switch (action) {
1826 case CPU_ONLINE:
1827 case CPU_DOWN_FAILED:
1828 (void)rcutorture_booster_init(cpu);
1829 break;
1830 case CPU_DOWN_PREPARE:
1831 rcutorture_booster_cleanup(cpu);
1832 break;
1833 default:
1834 break;
1835 }
1836 return NOTIFY_OK;
1837}
1838
1839static struct notifier_block rcutorture_cpu_nb = {
1840 .notifier_call = rcutorture_cpu_notify,
1841};
1842
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001843static void
1844rcu_torture_cleanup(void)
1845{
1846 int i;
1847
Paul E. McKenney343e9092008-12-15 16:13:07 -08001848 mutex_lock(&fullstop_mutex);
Paul E. McKenney4a298652011-04-03 21:33:51 -07001849 rcutorture_record_test_transition();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001850 if (fullstop == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001851 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001852 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001853 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001854 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001855 if (cur_ops->cb_barrier != NULL)
1856 cur_ops->cb_barrier();
1857 return;
1858 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001859 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001860 mutex_unlock(&fullstop_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001861 unregister_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001862 rcu_torture_barrier_cleanup();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001863 rcu_torture_stall_cleanup();
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001864 if (stutter_task) {
1865 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1866 kthread_stop(stutter_task);
1867 }
1868 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001869 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001870 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1871 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001872 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001873 }
1874 shuffler_task = NULL;
1875
Josh Triplettc8e5b162007-05-08 00:33:20 -07001876 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001877 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1878 kthread_stop(writer_task);
1879 }
1880 writer_task = NULL;
1881
Josh Triplettc8e5b162007-05-08 00:33:20 -07001882 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001883 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001884 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001885 VERBOSE_PRINTK_STRING(
1886 "Stopping rcu_torture_reader task");
1887 kthread_stop(reader_tasks[i]);
1888 }
1889 reader_tasks[i] = NULL;
1890 }
1891 kfree(reader_tasks);
1892 reader_tasks = NULL;
1893 }
1894 rcu_torture_current = NULL;
1895
Josh Triplettc8e5b162007-05-08 00:33:20 -07001896 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001897 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001898 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001899 VERBOSE_PRINTK_STRING(
1900 "Stopping rcu_torture_fakewriter task");
1901 kthread_stop(fakewriter_tasks[i]);
1902 }
1903 fakewriter_tasks[i] = NULL;
1904 }
1905 kfree(fakewriter_tasks);
1906 fakewriter_tasks = NULL;
1907 }
1908
Josh Triplettc8e5b162007-05-08 00:33:20 -07001909 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001910 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1911 kthread_stop(stats_task);
1912 }
1913 stats_task = NULL;
1914
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001915 if (fqs_task) {
1916 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1917 kthread_stop(fqs_task);
1918 }
1919 fqs_task = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001920 if ((test_boost == 1 && cur_ops->can_boost) ||
1921 test_boost == 2) {
1922 unregister_cpu_notifier(&rcutorture_cpu_nb);
1923 for_each_possible_cpu(i)
1924 rcutorture_booster_cleanup(i);
1925 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001926 if (shutdown_task != NULL) {
1927 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1928 kthread_stop(shutdown_task);
1929 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001930 shutdown_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001931 rcu_torture_onoff_cleanup();
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001932
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001933 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001934
1935 if (cur_ops->cb_barrier != NULL)
1936 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001937
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001938 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001939
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001940 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001941 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney091541b2012-01-10 12:51:14 -08001942 else if (n_online_successes != n_online_attempts ||
1943 n_offline_successes != n_offline_attempts)
1944 rcu_torture_print_module_parms(cur_ops,
1945 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001946 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001947 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001948}
1949
Josh Triplett6f8bc502007-05-08 00:25:24 -07001950static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001951rcu_torture_init(void)
1952{
1953 int i;
1954 int cpu;
1955 int firsterr = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001956 int retval;
Josh Triplettade5fb82007-05-08 00:33:22 -07001957 static struct rcu_torture_ops *torture_ops[] =
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001958 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -07001959 &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
Paul E. McKenney751a68b2012-05-07 13:44:44 -07001960 &srcu_ops, &srcu_sync_ops, &srcu_expedited_ops,
1961 &srcu_raw_ops, &srcu_raw_sync_ops,
Paul E. McKenney804bb832009-10-25 19:03:52 -07001962 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001963
Paul E. McKenney343e9092008-12-15 16:13:07 -08001964 mutex_lock(&fullstop_mutex);
1965
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001966 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001967 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001968 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001969 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001970 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001971 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001972 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001973 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1974 torture_type);
1975 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001976 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001977 pr_alert(" %s", torture_ops[i]->name);
1978 pr_alert("\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001979 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001980 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001981 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001982 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001983 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001984 fqs_duration = 0;
1985 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001986 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001987 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1988
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001989 if (nreaders >= 0)
1990 nrealreaders = nreaders;
1991 else
1992 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001993 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001994 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001995
1996 /* Set up the freelist. */
1997
1998 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001999 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08002000 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002001 list_add_tail(&rcu_tortures[i].rtort_free,
2002 &rcu_torture_freelist);
2003 }
2004
2005 /* Initialize the statistics so that each run gets its own numbers. */
2006
2007 rcu_torture_current = NULL;
2008 rcu_torture_current_version = 0;
2009 atomic_set(&n_rcu_torture_alloc, 0);
2010 atomic_set(&n_rcu_torture_alloc_fail, 0);
2011 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08002012 atomic_set(&n_rcu_torture_mberror, 0);
2013 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002014 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002015 n_rcu_torture_boost_ktrerror = 0;
2016 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002017 n_rcu_torture_boost_failure = 0;
2018 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002019 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2020 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002021 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002022 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2023 per_cpu(rcu_torture_count, cpu)[i] = 0;
2024 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2025 }
2026 }
2027
2028 /* Start up the kthreads. */
2029
2030 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
Paul E. McKenney60f53782012-08-25 15:27:40 -07002031 writer_task = kthread_create(rcu_torture_writer, NULL,
2032 "rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002033 if (IS_ERR(writer_task)) {
2034 firsterr = PTR_ERR(writer_task);
2035 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
2036 writer_task = NULL;
2037 goto unwind;
2038 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002039 wake_up_process(writer_task);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002040 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002041 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002042 if (fakewriter_tasks == NULL) {
2043 VERBOSE_PRINTK_ERRSTRING("out of memory");
2044 firsterr = -ENOMEM;
2045 goto unwind;
2046 }
2047 for (i = 0; i < nfakewriters; i++) {
2048 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2049 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002050 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07002051 if (IS_ERR(fakewriter_tasks[i])) {
2052 firsterr = PTR_ERR(fakewriter_tasks[i]);
2053 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2054 fakewriter_tasks[i] = NULL;
2055 goto unwind;
2056 }
2057 }
Josh Triplett2860aab2006-10-04 02:17:11 -07002058 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002059 GFP_KERNEL);
2060 if (reader_tasks == NULL) {
2061 VERBOSE_PRINTK_ERRSTRING("out of memory");
2062 firsterr = -ENOMEM;
2063 goto unwind;
2064 }
2065 for (i = 0; i < nrealreaders; i++) {
2066 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2067 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2068 "rcu_torture_reader");
2069 if (IS_ERR(reader_tasks[i])) {
2070 firsterr = PTR_ERR(reader_tasks[i]);
2071 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2072 reader_tasks[i] = NULL;
2073 goto unwind;
2074 }
2075 }
2076 if (stat_interval > 0) {
2077 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2078 stats_task = kthread_run(rcu_torture_stats, NULL,
2079 "rcu_torture_stats");
2080 if (IS_ERR(stats_task)) {
2081 firsterr = PTR_ERR(stats_task);
2082 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2083 stats_task = NULL;
2084 goto unwind;
2085 }
2086 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002087 if (test_no_idle_hz) {
2088 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002089
2090 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2091 firsterr = -ENOMEM;
2092 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2093 goto unwind;
2094 }
2095
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002096 /* Create the shuffler thread */
2097 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2098 "rcu_torture_shuffle");
2099 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002100 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002101 firsterr = PTR_ERR(shuffler_task);
2102 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2103 shuffler_task = NULL;
2104 goto unwind;
2105 }
2106 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07002107 if (stutter < 0)
2108 stutter = 0;
2109 if (stutter) {
2110 /* Create the stutter thread */
2111 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2112 "rcu_torture_stutter");
2113 if (IS_ERR(stutter_task)) {
2114 firsterr = PTR_ERR(stutter_task);
2115 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2116 stutter_task = NULL;
2117 goto unwind;
2118 }
2119 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002120 if (fqs_duration < 0)
2121 fqs_duration = 0;
2122 if (fqs_duration) {
2123 /* Create the stutter thread */
2124 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2125 "rcu_torture_fqs");
2126 if (IS_ERR(fqs_task)) {
2127 firsterr = PTR_ERR(fqs_task);
2128 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2129 fqs_task = NULL;
2130 goto unwind;
2131 }
2132 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002133 if (test_boost_interval < 1)
2134 test_boost_interval = 1;
2135 if (test_boost_duration < 2)
2136 test_boost_duration = 2;
2137 if ((test_boost == 1 && cur_ops->can_boost) ||
2138 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002139
2140 boost_starttime = jiffies + test_boost_interval * HZ;
2141 register_cpu_notifier(&rcutorture_cpu_nb);
2142 for_each_possible_cpu(i) {
2143 if (cpu_is_offline(i))
2144 continue; /* Heuristic: CPU can go offline. */
2145 retval = rcutorture_booster_init(i);
2146 if (retval < 0) {
2147 firsterr = retval;
2148 goto unwind;
2149 }
2150 }
2151 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002152 if (shutdown_secs > 0) {
2153 shutdown_time = jiffies + shutdown_secs * HZ;
Paul E. McKenney60f53782012-08-25 15:27:40 -07002154 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2155 "rcu_torture_shutdown");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002156 if (IS_ERR(shutdown_task)) {
2157 firsterr = PTR_ERR(shutdown_task);
2158 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2159 shutdown_task = NULL;
2160 goto unwind;
2161 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002162 wake_up_process(shutdown_task);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002163 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002164 i = rcu_torture_onoff_init();
2165 if (i != 0) {
2166 firsterr = i;
2167 goto unwind;
2168 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002169 register_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002170 i = rcu_torture_stall_init();
2171 if (i != 0) {
2172 firsterr = i;
2173 goto unwind;
2174 }
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002175 retval = rcu_torture_barrier_init();
2176 if (retval != 0) {
2177 firsterr = retval;
2178 goto unwind;
2179 }
Paul E. McKenney4a298652011-04-03 21:33:51 -07002180 rcutorture_record_test_transition();
Paul E. McKenney343e9092008-12-15 16:13:07 -08002181 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002182 return 0;
2183
2184unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08002185 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002186 rcu_torture_cleanup();
2187 return firsterr;
2188}
2189
2190module_init(rcu_torture_init);
2191module_exit(rcu_torture_cleanup);