blob: ddef618718786ca927a7729149d12c9ecc23a30b [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. McKenney2ec1f2d2013-06-12 15:12:21 -070068static bool gp_exp; /* Use expedited GP wait primitives. */
69static bool gp_normal; /* Use normal GP wait primitives. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -080070static int n_barrier_cbs; /* Number of callbacks to test RCU barriers. */
Paul E. McKenneyd2818df2013-04-23 17:05:42 -070071static int object_debug; /* Test object-debug double call_rcu()?. */
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -080072static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -080073static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070074static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -080075static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
76static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
Paul E. McKenney8e8be452010-09-02 16:16:14 -070077static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
78static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
79static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
Josh Triplett20d2e422006-10-04 02:17:15 -070080static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080081
Josh Triplettd6ad6712007-03-06 01:42:13 -080082module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080083MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080084module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070085MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Paul E. McKenney3721bc12011-07-21 16:00:17 -070086module_param(stat_interval, int, 0644);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080087MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080088module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080089MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080090module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080091MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080092module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080093MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070094module_param(stutter, int, 0444);
95MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070096module_param(irqreader, int, 0444);
97MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Paul E. McKenneybf66f182010-01-04 15:09:10 -080098module_param(fqs_duration, int, 0444);
99MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
100module_param(fqs_holdoff, int, 0444);
101MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
102module_param(fqs_stutter, int, 0444);
103MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700104module_param(gp_normal, bool, 0444);
105MODULE_PARM_DESC(gp_normal, "Use normal (non-expedited) GP wait primitives");
106module_param(gp_exp, bool, 0444);
107MODULE_PARM_DESC(gp_exp, "Use expedited GP wait primitives");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800108module_param(n_barrier_cbs, int, 0444);
109MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
Paul E. McKenneyd2818df2013-04-23 17:05:42 -0700110module_param(object_debug, int, 0444);
111MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800112module_param(onoff_interval, int, 0444);
113MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -0800114module_param(onoff_holdoff, int, 0444);
115MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700116module_param(shutdown_secs, int, 0444);
117MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800118module_param(stall_cpu, int, 0444);
119MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
120module_param(stall_cpu_holdoff, int, 0444);
121MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700122module_param(test_boost, int, 0444);
123MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
124module_param(test_boost_interval, int, 0444);
125MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
126module_param(test_boost_duration, int, 0444);
127MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
Josh Triplettd6ad6712007-03-06 01:42:13 -0800128module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700129MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700130
131#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800132#define PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700133 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800134#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700135 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800136#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700137 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800138
139static char printk_buf[4096];
140
141static int nrealreaders;
142static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700143static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800144static struct task_struct **reader_tasks;
145static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800146static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700147static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800148static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700149static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700150static struct task_struct *shutdown_task;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800151#ifdef CONFIG_HOTPLUG_CPU
152static struct task_struct *onoff_task;
153#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800154static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800155static struct task_struct **barrier_cbs_tasks;
156static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800157
158#define RCU_TORTURE_PIPE_LEN 10
159
160struct rcu_torture {
161 struct rcu_head rtort_rcu;
162 int rtort_pipe_count;
163 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800164 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800165};
166
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800167static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700168static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700169static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800170static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
171static DEFINE_SPINLOCK(rcu_torture_lock);
172static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
173 { 0 };
174static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
175 { 0 };
176static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700177static atomic_t n_rcu_torture_alloc;
178static atomic_t n_rcu_torture_alloc_fail;
179static atomic_t n_rcu_torture_free;
180static atomic_t n_rcu_torture_mberror;
181static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800182static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700183static long n_rcu_torture_boost_ktrerror;
184static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700185static long n_rcu_torture_boost_failure;
186static long n_rcu_torture_boosts;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700187static long n_rcu_torture_timers;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800188static long n_offline_attempts;
189static long n_offline_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700190static unsigned long sum_offline;
191static int min_offline = -1;
192static int max_offline;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800193static long n_online_attempts;
194static long n_online_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700195static unsigned long sum_online;
196static int min_online = -1;
197static int max_online;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800198static long n_barrier_attempts;
199static long n_barrier_successes;
Josh Triplette3033732006-10-04 02:17:14 -0700200static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600201static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800202
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700203static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700204
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700205#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
206#define RCUTORTURE_RUNNABLE_INIT 1
207#else
208#define RCUTORTURE_RUNNABLE_INIT 0
209#endif
210int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
Paul E. McKenneybb3bf702011-11-04 13:24:07 -0700211module_param(rcutorture_runnable, int, 0444);
212MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700213
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700214#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700215#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700216#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700217#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700218#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700219
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500220#ifdef CONFIG_RCU_TRACE
221static u64 notrace rcu_trace_clock_local(void)
222{
223 u64 ts = trace_clock_local();
224 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
225 return ts;
226}
227#else /* #ifdef CONFIG_RCU_TRACE */
228static u64 notrace rcu_trace_clock_local(void)
229{
230 return 0ULL;
231}
232#endif /* #else #ifdef CONFIG_RCU_TRACE */
233
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700234static unsigned long shutdown_time; /* jiffies to system shutdown. */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700235static unsigned long boost_starttime; /* jiffies of next boost test start. */
236DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
237 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800238static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700239static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800240static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
241static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
242static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700243
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800244/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
245
246#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
247#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
248#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
249static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700250/*
251 * Protect fullstop transitions and spawning of kthreads.
252 */
253static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800254
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700255/* Forward reference. */
256static void rcu_torture_cleanup(void);
257
Paul E. McKenney343e9092008-12-15 16:13:07 -0800258/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800259 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800260 */
261static int
262rcutorture_shutdown_notify(struct notifier_block *unused1,
263 unsigned long unused2, void *unused3)
264{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800265 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800266 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800267 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800268 else
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700269 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800270 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800271 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800272 return NOTIFY_DONE;
273}
274
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800275/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800276 * Absorb kthreads into a kernel function that won't return, so that
277 * they won't ever access module text or data again.
278 */
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400279static void rcutorture_shutdown_absorb(const char *title)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800280{
281 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700282 pr_notice(
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800283 "rcutorture thread %s parking due to system shutdown\n",
284 title);
285 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
286 }
287}
288
289/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800290 * Allocate an element from the rcu_tortures pool.
291 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800292static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800293rcu_torture_alloc(void)
294{
295 struct list_head *p;
296
Ingo Molnaradac1662006-01-25 19:50:12 +0100297 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800298 if (list_empty(&rcu_torture_freelist)) {
299 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100300 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800301 return NULL;
302 }
303 atomic_inc(&n_rcu_torture_alloc);
304 p = rcu_torture_freelist.next;
305 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100306 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800307 return container_of(p, struct rcu_torture, rtort_free);
308}
309
310/*
311 * Free an element to the rcu_tortures pool.
312 */
313static void
314rcu_torture_free(struct rcu_torture *p)
315{
316 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100317 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800318 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100319 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800320}
321
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800322struct rcu_random_state {
323 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700324 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800325};
326
327#define RCU_RANDOM_MULT 39916801 /* prime */
328#define RCU_RANDOM_ADD 479001701 /* prime */
329#define RCU_RANDOM_REFRESH 10000
330
331#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
332
333/*
334 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700335 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800336 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700337static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800338rcu_random(struct rcu_random_state *rrsp)
339{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800340 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200341 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800342 rrsp->rrs_count = RCU_RANDOM_REFRESH;
343 }
344 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
345 return swahw32(rrsp->rrs_state);
346}
347
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700348static void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400349rcu_stutter_wait(const char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700350{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800351 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700352 if (rcutorture_runnable)
353 schedule_timeout_interruptible(1);
354 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700355 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800356 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800357 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700358}
359
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800360/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700361 * Operations vector for selecting different types of tests.
362 */
363
364struct rcu_torture_ops {
365 void (*init)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700366 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700367 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700368 void (*readunlock)(int idx);
369 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700370 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700371 void (*sync)(void);
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700372 void (*exp_sync)(void);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800373 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
Paul E. McKenney23269742008-05-12 21:21:05 +0200374 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800375 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700376 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700377 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700378 int can_boost;
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400379 const char *name;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700380};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700381
382static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700383
384/*
385 * Definitions for rcu torture testing.
386 */
387
Josh Tripletta49a4af2006-09-29 01:59:30 -0700388static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700389{
390 rcu_read_lock();
391 return 0;
392}
393
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700394static void rcu_read_delay(struct rcu_random_state *rrsp)
395{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700396 const unsigned long shortdelay_us = 200;
397 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700398
Josh Triplettb8d57a72009-09-08 15:54:35 -0700399 /* We want a short delay sometimes to make a reader delay the grace
400 * period, and we want a long delay occasionally to trigger
401 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700402
Josh Triplettb8d57a72009-09-08 15:54:35 -0700403 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
404 mdelay(longdelay_ms);
405 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
406 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800407#ifdef CONFIG_PREEMPT
408 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
409 preempt_schedule(); /* No QS if preempt_disable() in effect */
410#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700411}
412
Josh Tripletta49a4af2006-09-29 01:59:30 -0700413static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700414{
415 rcu_read_unlock();
416}
417
418static int rcu_torture_completed(void)
419{
420 return rcu_batches_completed();
421}
422
423static void
424rcu_torture_cb(struct rcu_head *p)
425{
426 int i;
427 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
428
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800429 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700430 /* Test is ending, just drop callbacks on the floor. */
431 /* The next initialization will pick up the pieces. */
432 return;
433 }
434 i = rp->rtort_pipe_count;
435 if (i > RCU_TORTURE_PIPE_LEN)
436 i = RCU_TORTURE_PIPE_LEN;
437 atomic_inc(&rcu_torture_wcount[i]);
438 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
439 rp->rtort_mbtest = 0;
440 rcu_torture_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700441 } else {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700442 cur_ops->deferred_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700443 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700444}
445
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800446static int rcu_no_completed(void)
447{
448 return 0;
449}
450
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700451static void rcu_torture_deferred_free(struct rcu_torture *p)
452{
453 call_rcu(&p->rtort_rcu, rcu_torture_cb);
454}
455
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700456static void rcu_sync_torture_init(void)
457{
458 INIT_LIST_HEAD(&rcu_torture_removed);
459}
460
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700461static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700462 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700463 .readlock = rcu_torture_read_lock,
464 .read_delay = rcu_read_delay,
465 .readunlock = rcu_torture_read_unlock,
466 .completed = rcu_torture_completed,
467 .deferred_free = rcu_torture_deferred_free,
468 .sync = synchronize_rcu,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700469 .exp_sync = synchronize_rcu_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800470 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700471 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800472 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700473 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700474 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700475 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700476 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700477};
478
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700479/*
480 * Definitions for rcu_bh torture testing.
481 */
482
Josh Tripletta49a4af2006-09-29 01:59:30 -0700483static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700484{
485 rcu_read_lock_bh();
486 return 0;
487}
488
Josh Tripletta49a4af2006-09-29 01:59:30 -0700489static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700490{
491 rcu_read_unlock_bh();
492}
493
494static int rcu_bh_torture_completed(void)
495{
496 return rcu_batches_completed_bh();
497}
498
499static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
500{
501 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
502}
503
504static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700505 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700506 .readlock = rcu_bh_torture_read_lock,
507 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
508 .readunlock = rcu_bh_torture_read_unlock,
509 .completed = rcu_bh_torture_completed,
510 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700511 .sync = synchronize_rcu_bh,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700512 .exp_sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800513 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700514 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800515 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700516 .stats = NULL,
517 .irq_capable = 1,
518 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700519};
520
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700521/*
522 * Definitions for srcu torture testing.
523 */
524
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800525DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700526
Josh Triplett012d3ca2006-12-06 20:40:19 -0800527static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700528{
529 return srcu_read_lock(&srcu_ctl);
530}
531
532static void srcu_read_delay(struct rcu_random_state *rrsp)
533{
534 long delay;
535 const long uspertick = 1000000 / HZ;
536 const long longdelay = 10;
537
538 /* We want there to be long-running readers, but not all the time. */
539
540 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
541 if (!delay)
542 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800543 else
544 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700545}
546
Josh Triplett012d3ca2006-12-06 20:40:19 -0800547static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700548{
549 srcu_read_unlock(&srcu_ctl, idx);
550}
551
552static int srcu_torture_completed(void)
553{
554 return srcu_batches_completed(&srcu_ctl);
555}
556
Lai Jiangshan9059c942012-03-19 16:12:14 +0800557static void srcu_torture_deferred_free(struct rcu_torture *rp)
558{
559 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
560}
561
Josh Triplettb772e1d2006-10-04 02:17:13 -0700562static void srcu_torture_synchronize(void)
563{
564 synchronize_srcu(&srcu_ctl);
565}
566
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700567static void srcu_torture_call(struct rcu_head *head,
568 void (*func)(struct rcu_head *head))
569{
570 call_srcu(&srcu_ctl, head, func);
571}
572
573static void srcu_torture_barrier(void)
574{
575 srcu_barrier(&srcu_ctl);
576}
577
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700578static int srcu_torture_stats(char *page)
579{
580 int cnt = 0;
581 int cpu;
582 int idx = srcu_ctl.completed & 0x1;
583
584 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
585 torture_type, TORTURE_FLAG, idx);
586 for_each_possible_cpu(cpu) {
Paul E. McKenneycef50122012-02-05 07:42:44 -0800587 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700588 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
589 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
590 }
591 cnt += sprintf(&page[cnt], "\n");
592 return cnt;
593}
594
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700595static void srcu_torture_synchronize_expedited(void)
596{
597 synchronize_srcu_expedited(&srcu_ctl);
598}
599
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700600static struct rcu_torture_ops srcu_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800601 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700602 .readlock = srcu_torture_read_lock,
603 .read_delay = srcu_read_delay,
604 .readunlock = srcu_torture_read_unlock,
605 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800606 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700607 .sync = srcu_torture_synchronize,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700608 .exp_sync = srcu_torture_synchronize_expedited,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700609 .call = srcu_torture_call,
610 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700611 .stats = srcu_torture_stats,
612 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700613};
614
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700615/*
616 * Definitions for sched torture testing.
617 */
618
619static int sched_torture_read_lock(void)
620{
621 preempt_disable();
622 return 0;
623}
624
625static void sched_torture_read_unlock(int idx)
626{
627 preempt_enable();
628}
629
Paul E. McKenney23269742008-05-12 21:21:05 +0200630static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
631{
632 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
633}
634
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700635static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700636 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700637 .readlock = sched_torture_read_lock,
638 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
639 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800640 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700641 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700642 .sync = synchronize_sched,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700643 .exp_sync = synchronize_sched_expedited,
644 .call = call_rcu_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700645 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800646 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700647 .stats = NULL,
648 .irq_capable = 1,
649 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700650};
651
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700652/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700653 * RCU torture priority-boost testing. Runs one real-time thread per
654 * CPU for moderate bursts, repeatedly registering RCU callbacks and
655 * spinning waiting for them to be invoked. If a given callback takes
656 * too long to be invoked, we assume that priority inversion has occurred.
657 */
658
659struct rcu_boost_inflight {
660 struct rcu_head rcu;
661 int inflight;
662};
663
664static void rcu_torture_boost_cb(struct rcu_head *head)
665{
666 struct rcu_boost_inflight *rbip =
667 container_of(head, struct rcu_boost_inflight, rcu);
668
669 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
670 rbip->inflight = 0;
671}
672
673static int rcu_torture_boost(void *arg)
674{
675 unsigned long call_rcu_time;
676 unsigned long endtime;
677 unsigned long oldstarttime;
678 struct rcu_boost_inflight rbi = { .inflight = 0 };
679 struct sched_param sp;
680
681 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
682
683 /* Set real-time priority. */
684 sp.sched_priority = 1;
685 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
686 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
687 n_rcu_torture_boost_rterror++;
688 }
689
Paul E. McKenney561190e2011-03-30 09:10:44 -0700690 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700691 /* Each pass through the following loop does one boost-test cycle. */
692 do {
693 /* Wait for the next test interval. */
694 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700695 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -0800696 schedule_timeout_interruptible(oldstarttime - jiffies);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700697 rcu_stutter_wait("rcu_torture_boost");
698 if (kthread_should_stop() ||
699 fullstop != FULLSTOP_DONTSTOP)
700 goto checkwait;
701 }
702
703 /* Do one boost-test interval. */
704 endtime = oldstarttime + test_boost_duration * HZ;
705 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700706 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700707 /* If we don't have a callback in flight, post one. */
708 if (!rbi.inflight) {
709 smp_mb(); /* RCU core before ->inflight = 1. */
710 rbi.inflight = 1;
711 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
712 if (jiffies - call_rcu_time >
713 test_boost_duration * HZ - HZ / 2) {
714 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
715 n_rcu_torture_boost_failure++;
716 }
717 call_rcu_time = jiffies;
718 }
719 cond_resched();
720 rcu_stutter_wait("rcu_torture_boost");
721 if (kthread_should_stop() ||
722 fullstop != FULLSTOP_DONTSTOP)
723 goto checkwait;
724 }
725
726 /*
727 * Set the start time of the next test interval.
728 * Yes, this is vulnerable to long delays, but such
729 * delays simply cause a false negative for the next
730 * interval. Besides, we are running at RT priority,
731 * so delays should be relatively rare.
732 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700733 while (oldstarttime == boost_starttime &&
734 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700735 if (mutex_trylock(&boost_mutex)) {
736 boost_starttime = jiffies +
737 test_boost_interval * HZ;
738 n_rcu_torture_boosts++;
739 mutex_unlock(&boost_mutex);
740 break;
741 }
742 schedule_timeout_uninterruptible(1);
743 }
744
745 /* Go do the stutter. */
746checkwait: rcu_stutter_wait("rcu_torture_boost");
747 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
748
749 /* Clean up and exit. */
750 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
751 rcutorture_shutdown_absorb("rcu_torture_boost");
752 while (!kthread_should_stop() || rbi.inflight)
753 schedule_timeout_uninterruptible(1);
754 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
Paul E. McKenney9d681972011-06-21 01:48:03 -0700755 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700756 return 0;
757}
758
759/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800760 * RCU torture force-quiescent-state kthread. Repeatedly induces
761 * bursts of calls to force_quiescent_state(), increasing the probability
762 * of occurrence of some important types of race conditions.
763 */
764static int
765rcu_torture_fqs(void *arg)
766{
767 unsigned long fqs_resume_time;
768 int fqs_burst_remaining;
769
770 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
771 do {
772 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700773 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
774 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800775 schedule_timeout_interruptible(1);
776 }
777 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700778 while (fqs_burst_remaining > 0 &&
779 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800780 cur_ops->fqs();
781 udelay(fqs_holdoff);
782 fqs_burst_remaining -= fqs_holdoff;
783 }
784 rcu_stutter_wait("rcu_torture_fqs");
785 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
786 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
787 rcutorture_shutdown_absorb("rcu_torture_fqs");
788 while (!kthread_should_stop())
789 schedule_timeout_uninterruptible(1);
790 return 0;
791}
792
793/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800794 * RCU torture writer kthread. Repeatedly substitutes a new structure
795 * for that pointed to by rcu_torture_current, freeing the old structure
796 * after a series of grace periods (the "pipeline").
797 */
798static int
799rcu_torture_writer(void *arg)
800{
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700801 bool exp;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800802 int i;
803 long oldbatch = rcu_batches_completed();
804 struct rcu_torture *rp;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700805 struct rcu_torture *rp1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800806 struct rcu_torture *old_rp;
807 static DEFINE_RCU_RANDOM(rand);
808
809 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800810 set_user_nice(current, 19);
811
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800812 do {
813 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700814 rp = rcu_torture_alloc();
815 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800816 continue;
817 rp->rtort_pipe_count = 0;
818 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700819 old_rp = rcu_dereference_check(rcu_torture_current,
820 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800821 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800822 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700823 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700824 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800825 i = old_rp->rtort_pipe_count;
826 if (i > RCU_TORTURE_PIPE_LEN)
827 i = RCU_TORTURE_PIPE_LEN;
828 atomic_inc(&rcu_torture_wcount[i]);
829 old_rp->rtort_pipe_count++;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700830 if (gp_normal == gp_exp)
831 exp = !!(rcu_random(&rand) & 0x80);
832 else
833 exp = gp_exp;
834 if (!exp) {
835 cur_ops->deferred_free(old_rp);
836 } else {
837 cur_ops->exp_sync();
838 list_add(&old_rp->rtort_free,
839 &rcu_torture_removed);
840 list_for_each_entry_safe(rp, rp1,
841 &rcu_torture_removed,
842 rtort_free) {
843 i = rp->rtort_pipe_count;
844 if (i > RCU_TORTURE_PIPE_LEN)
845 i = RCU_TORTURE_PIPE_LEN;
846 atomic_inc(&rcu_torture_wcount[i]);
847 if (++rp->rtort_pipe_count >=
848 RCU_TORTURE_PIPE_LEN) {
849 rp->rtort_mbtest = 0;
850 list_del(&rp->rtort_free);
851 rcu_torture_free(rp);
852 }
853 }
854 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800855 }
Paul E. McKenney4a298652011-04-03 21:33:51 -0700856 rcutorture_record_progress(++rcu_torture_current_version);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700857 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800858 rcu_stutter_wait("rcu_torture_writer");
859 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800860 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800861 rcutorture_shutdown_absorb("rcu_torture_writer");
862 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800863 schedule_timeout_uninterruptible(1);
864 return 0;
865}
866
867/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700868 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
869 * delay between calls.
870 */
871static int
872rcu_torture_fakewriter(void *arg)
873{
874 DEFINE_RCU_RANDOM(rand);
875
876 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
877 set_user_nice(current, 19);
878
879 do {
880 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
881 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -0700882 if (cur_ops->cb_barrier != NULL &&
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700883 rcu_random(&rand) % (nfakewriters * 8) == 0) {
Paul E. McKenney72472a02012-05-29 17:50:51 -0700884 cur_ops->cb_barrier();
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700885 } else if (gp_normal == gp_exp) {
886 if (rcu_random(&rand) & 0x80)
887 cur_ops->sync();
888 else
889 cur_ops->exp_sync();
890 } else if (gp_normal) {
Paul E. McKenney72472a02012-05-29 17:50:51 -0700891 cur_ops->sync();
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700892 } else {
893 cur_ops->exp_sync();
894 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800895 rcu_stutter_wait("rcu_torture_fakewriter");
896 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700897
898 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800899 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
900 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -0700901 schedule_timeout_uninterruptible(1);
902 return 0;
903}
904
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700905void rcutorture_trace_dump(void)
906{
907 static atomic_t beenhere = ATOMIC_INIT(0);
908
909 if (atomic_read(&beenhere))
910 return;
911 if (atomic_xchg(&beenhere, 1) != 0)
912 return;
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700913 ftrace_dump(DUMP_ALL);
914}
915
Josh Triplettb772e1d2006-10-04 02:17:13 -0700916/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700917 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
918 * incrementing the corresponding element of the pipeline array. The
919 * counter in the element should never be greater than 1, otherwise, the
920 * RCU implementation is broken.
921 */
922static void rcu_torture_timer(unsigned long unused)
923{
924 int idx;
925 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -0800926 int completed_end;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700927 static DEFINE_RCU_RANDOM(rand);
928 static DEFINE_SPINLOCK(rand_lock);
929 struct rcu_torture *p;
930 int pipe_count;
Paul E. McKenney52494532012-11-14 16:26:40 -0800931 unsigned long long ts;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700932
933 idx = cur_ops->readlock();
934 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500935 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800936 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -0800937 rcu_read_lock_bh_held() ||
938 rcu_read_lock_sched_held() ||
939 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700940 if (p == NULL) {
941 /* Leave because rcu_torture_writer is not yet underway */
942 cur_ops->readunlock(idx);
943 return;
944 }
945 if (p->rtort_mbtest == 0)
946 atomic_inc(&n_rcu_torture_mberror);
947 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700948 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700949 n_rcu_torture_timers++;
950 spin_unlock(&rand_lock);
951 preempt_disable();
952 pipe_count = p->rtort_pipe_count;
953 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
954 /* Should not happen, but... */
955 pipe_count = RCU_TORTURE_PIPE_LEN;
956 }
Paul E. McKenney52494532012-11-14 16:26:40 -0800957 completed_end = cur_ops->completed();
958 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -0800959 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
960 completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700961 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -0800962 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900963 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -0800964 completed = completed_end - completed;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700965 if (completed > RCU_TORTURE_PIPE_LEN) {
966 /* Should not happen, but... */
967 completed = RCU_TORTURE_PIPE_LEN;
968 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900969 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700970 preempt_enable();
971 cur_ops->readunlock(idx);
972}
973
974/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800975 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
976 * incrementing the corresponding element of the pipeline array. The
977 * counter in the element should never be greater than 1, otherwise, the
978 * RCU implementation is broken.
979 */
980static int
981rcu_torture_reader(void *arg)
982{
983 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -0800984 int completed_end;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700985 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800986 DEFINE_RCU_RANDOM(rand);
987 struct rcu_torture *p;
988 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700989 struct timer_list t;
Paul E. McKenney52494532012-11-14 16:26:40 -0800990 unsigned long long ts;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800991
992 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800993 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700994 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700995 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800996
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800997 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700998 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700999 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001000 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001001 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001002 idx = cur_ops->readlock();
1003 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -05001004 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001005 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001006 rcu_read_lock_bh_held() ||
1007 rcu_read_lock_sched_held() ||
1008 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001009 if (p == NULL) {
1010 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001011 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001012 schedule_timeout_interruptible(HZ);
1013 continue;
1014 }
Paul E. McKenney996417d2005-11-18 01:10:50 -08001015 if (p->rtort_mbtest == 0)
1016 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001017 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001018 preempt_disable();
1019 pipe_count = p->rtort_pipe_count;
1020 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1021 /* Should not happen, but... */
1022 pipe_count = RCU_TORTURE_PIPE_LEN;
1023 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001024 completed_end = cur_ops->completed();
1025 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -08001026 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1027 ts, completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001028 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001029 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001030 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001031 completed = completed_end - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001032 if (completed > RCU_TORTURE_PIPE_LEN) {
1033 /* Should not happen, but... */
1034 completed = RCU_TORTURE_PIPE_LEN;
1035 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001036 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001037 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001038 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001039 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001040 rcu_stutter_wait("rcu_torture_reader");
1041 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001042 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001043 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001044 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001045 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001046 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001047 schedule_timeout_uninterruptible(1);
1048 return 0;
1049}
1050
1051/*
1052 * Create an RCU-torture statistics message in the specified buffer.
1053 */
1054static int
1055rcu_torture_printk(char *page)
1056{
1057 int cnt = 0;
1058 int cpu;
1059 int i;
1060 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1061 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1062
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001063 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001064 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1065 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1066 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1067 }
1068 }
1069 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1070 if (pipesummary[i] != 0)
1071 break;
1072 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001073 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001074 cnt += sprintf(&page[cnt],
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001075 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001076 rcu_torture_current,
1077 rcu_torture_current_version,
1078 list_empty(&rcu_torture_freelist),
1079 atomic_read(&n_rcu_torture_alloc),
1080 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001081 atomic_read(&n_rcu_torture_free));
1082 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001083 atomic_read(&n_rcu_torture_mberror),
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001084 n_rcu_torture_boost_ktrerror,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001085 n_rcu_torture_boost_rterror);
1086 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001087 n_rcu_torture_boost_failure,
1088 n_rcu_torture_boosts,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001089 n_rcu_torture_timers);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001090 cnt += sprintf(&page[cnt],
1091 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1092 n_online_successes, n_online_attempts,
1093 n_offline_successes, n_offline_attempts,
1094 min_online, max_online,
1095 min_offline, max_offline,
1096 sum_online, sum_offline, HZ);
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001097 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001098 n_barrier_successes,
1099 n_barrier_attempts,
1100 n_rcu_torture_barrier_error);
1101 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001102 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001103 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001104 n_rcu_torture_boost_ktrerror != 0 ||
1105 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001106 n_rcu_torture_boost_failure != 0 ||
1107 i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001108 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001109 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001110 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001111 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001112 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1113 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1114 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001115 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001116 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001117 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001118 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001119 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001120 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1121 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1122 cnt += sprintf(&page[cnt], " %d",
1123 atomic_read(&rcu_torture_wcount[i]));
1124 }
1125 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -07001126 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001127 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001128 return cnt;
1129}
1130
1131/*
1132 * Print torture statistics. Caller must ensure that there is only
1133 * one call to this function at a given time!!! This is normally
1134 * accomplished by relying on the module system to only have one copy
1135 * of the module loaded, and then by giving the rcu_torture_stats
1136 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1137 * thread is not running).
1138 */
1139static void
1140rcu_torture_stats_print(void)
1141{
1142 int cnt;
1143
1144 cnt = rcu_torture_printk(printk_buf);
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001145 pr_alert("%s", printk_buf);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001146}
1147
1148/*
1149 * Periodically prints torture statistics, if periodic statistics printing
1150 * was specified via the stat_interval module parameter.
1151 *
1152 * No need to worry about fullstop here, since this one doesn't reference
1153 * volatile state or register callbacks.
1154 */
1155static int
1156rcu_torture_stats(void *arg)
1157{
1158 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1159 do {
1160 schedule_timeout_interruptible(stat_interval * HZ);
1161 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001162 rcutorture_shutdown_absorb("rcu_torture_stats");
1163 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001164 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1165 return 0;
1166}
1167
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001168static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1169
1170/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1171 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1172 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001173static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001174{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001175 int i;
1176
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001177 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001178 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001179
1180 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001181 if (num_online_cpus() == 1) {
1182 put_online_cpus();
1183 return;
1184 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001185
1186 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001187 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001188
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001189 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001190
Josh Triplettc8e5b162007-05-08 00:33:20 -07001191 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001192 for (i = 0; i < nrealreaders; i++)
1193 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001194 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001195 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001196 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001197 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001198 for (i = 0; i < nfakewriters; i++)
1199 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001200 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001201 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001202 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001203 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001204 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001205 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001206 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -08001207 if (stutter_task)
1208 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1209 if (fqs_task)
1210 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1211 if (shutdown_task)
1212 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1213#ifdef CONFIG_HOTPLUG_CPU
1214 if (onoff_task)
1215 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1216#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1217 if (stall_task)
1218 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1219 if (barrier_cbs_tasks)
1220 for (i = 0; i < n_barrier_cbs; i++)
1221 if (barrier_cbs_tasks[i])
1222 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1223 shuffle_tmp_mask);
1224 if (barrier_task)
1225 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001226
1227 if (rcu_idle_cpu == -1)
1228 rcu_idle_cpu = num_online_cpus() - 1;
1229 else
1230 rcu_idle_cpu--;
1231
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001232 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001233}
1234
1235/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1236 * system to become idle at a time and cut off its timer ticks. This is meant
1237 * to test the support for such tickless idle CPU in RCU.
1238 */
1239static int
1240rcu_torture_shuffle(void *arg)
1241{
1242 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1243 do {
1244 schedule_timeout_interruptible(shuffle_interval * HZ);
1245 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001246 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1247 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001248 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1249 return 0;
1250}
1251
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001252/* Cause the rcutorture test to "stutter", starting and stopping all
1253 * threads periodically.
1254 */
1255static int
1256rcu_torture_stutter(void *arg)
1257{
1258 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1259 do {
1260 schedule_timeout_interruptible(stutter * HZ);
1261 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001262 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001263 schedule_timeout_interruptible(stutter * HZ);
1264 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001265 rcutorture_shutdown_absorb("rcu_torture_stutter");
1266 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001267 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1268 return 0;
1269}
1270
Paul E. McKenney95c38322006-03-24 03:15:58 -08001271static inline void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04001272rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001273{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001274 pr_alert("%s" TORTURE_FLAG
1275 "--- %s: nreaders=%d nfakewriters=%d "
1276 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1277 "shuffle_interval=%d stutter=%d irqreader=%d "
1278 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1279 "test_boost=%d/%d test_boost_interval=%d "
1280 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001281 "stall_cpu=%d stall_cpu_holdoff=%d "
1282 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001283 "onoff_interval=%d onoff_holdoff=%d\n",
1284 torture_type, tag, nrealreaders, nfakewriters,
1285 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1286 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1287 test_boost, cur_ops->can_boost,
1288 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001289 stall_cpu, stall_cpu_holdoff,
1290 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001291 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001292}
1293
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001294static struct notifier_block rcutorture_shutdown_nb = {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001295 .notifier_call = rcutorture_shutdown_notify,
1296};
1297
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001298static void rcutorture_booster_cleanup(int cpu)
1299{
1300 struct task_struct *t;
1301
1302 if (boost_tasks[cpu] == NULL)
1303 return;
1304 mutex_lock(&boost_mutex);
1305 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1306 t = boost_tasks[cpu];
1307 boost_tasks[cpu] = NULL;
1308 mutex_unlock(&boost_mutex);
1309
1310 /* This must be outside of the mutex, otherwise deadlock! */
1311 kthread_stop(t);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001312 boost_tasks[cpu] = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001313}
1314
1315static int rcutorture_booster_init(int cpu)
1316{
1317 int retval;
1318
1319 if (boost_tasks[cpu] != NULL)
1320 return 0; /* Already created, nothing more to do. */
1321
1322 /* Don't allow time recalculation while creating a new task. */
1323 mutex_lock(&boost_mutex);
1324 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001325 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1326 cpu_to_node(cpu),
1327 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001328 if (IS_ERR(boost_tasks[cpu])) {
1329 retval = PTR_ERR(boost_tasks[cpu]);
1330 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1331 n_rcu_torture_boost_ktrerror++;
1332 boost_tasks[cpu] = NULL;
1333 mutex_unlock(&boost_mutex);
1334 return retval;
1335 }
1336 kthread_bind(boost_tasks[cpu], cpu);
1337 wake_up_process(boost_tasks[cpu]);
1338 mutex_unlock(&boost_mutex);
1339 return 0;
1340}
1341
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001342/*
1343 * Cause the rcutorture test to shutdown the system after the test has
1344 * run for the time specified by the shutdown_secs module parameter.
1345 */
1346static int
1347rcu_torture_shutdown(void *arg)
1348{
1349 long delta;
1350 unsigned long jiffies_snap;
1351
1352 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1353 jiffies_snap = ACCESS_ONCE(jiffies);
1354 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1355 !kthread_should_stop()) {
1356 delta = shutdown_time - jiffies_snap;
1357 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001358 pr_alert("%s" TORTURE_FLAG
1359 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1360 torture_type, delta);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001361 schedule_timeout_interruptible(delta);
1362 jiffies_snap = ACCESS_ONCE(jiffies);
1363 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001364 if (kthread_should_stop()) {
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001365 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1366 return 0;
1367 }
1368
1369 /* OK, shut down the system. */
1370
1371 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1372 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1373 rcu_torture_cleanup(); /* Get the success/failure message. */
1374 kernel_power_off(); /* Shut down the system. */
1375 return 0;
1376}
1377
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001378#ifdef CONFIG_HOTPLUG_CPU
1379
1380/*
1381 * Execute random CPU-hotplug operations at the interval specified
1382 * by the onoff_interval.
1383 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001384static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001385rcu_torture_onoff(void *arg)
1386{
1387 int cpu;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001388 unsigned long delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001389 int maxcpu = -1;
1390 DEFINE_RCU_RANDOM(rand);
Paul E. McKenney48983262012-09-24 16:08:31 -07001391 int ret;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001392 unsigned long starttime;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001393
1394 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1395 for_each_online_cpu(cpu)
1396 maxcpu = cpu;
1397 WARN_ON(maxcpu < 0);
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -08001398 if (onoff_holdoff > 0) {
1399 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1400 schedule_timeout_interruptible(onoff_holdoff * HZ);
1401 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1402 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001403 while (!kthread_should_stop()) {
1404 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001405 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001406 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001407 pr_alert("%s" TORTURE_FLAG
1408 "rcu_torture_onoff task: offlining %d\n",
1409 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001410 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001411 n_offline_attempts++;
Paul E. McKenney48983262012-09-24 16:08:31 -07001412 ret = cpu_down(cpu);
1413 if (ret) {
1414 if (verbose)
1415 pr_alert("%s" TORTURE_FLAG
1416 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1417 torture_type, cpu, ret);
1418 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001419 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001420 pr_alert("%s" TORTURE_FLAG
1421 "rcu_torture_onoff task: offlined %d\n",
1422 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001423 n_offline_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001424 delta = jiffies - starttime;
1425 sum_offline += delta;
1426 if (min_offline < 0) {
1427 min_offline = delta;
1428 max_offline = delta;
1429 }
1430 if (min_offline > delta)
1431 min_offline = delta;
1432 if (max_offline < delta)
1433 max_offline = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001434 }
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001435 } else if (cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001436 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001437 pr_alert("%s" TORTURE_FLAG
1438 "rcu_torture_onoff task: onlining %d\n",
1439 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001440 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001441 n_online_attempts++;
1442 if (cpu_up(cpu) == 0) {
1443 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001444 pr_alert("%s" TORTURE_FLAG
1445 "rcu_torture_onoff task: onlined %d\n",
1446 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001447 n_online_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001448 delta = jiffies - starttime;
1449 sum_online += delta;
1450 if (min_online < 0) {
1451 min_online = delta;
1452 max_online = delta;
1453 }
1454 if (min_online > delta)
1455 min_online = delta;
1456 if (max_online < delta)
1457 max_online = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001458 }
1459 }
1460 schedule_timeout_interruptible(onoff_interval * HZ);
1461 }
1462 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1463 return 0;
1464}
1465
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001466static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001467rcu_torture_onoff_init(void)
1468{
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001469 int ret;
1470
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001471 if (onoff_interval <= 0)
1472 return 0;
1473 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1474 if (IS_ERR(onoff_task)) {
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001475 ret = PTR_ERR(onoff_task);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001476 onoff_task = NULL;
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001477 return ret;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001478 }
1479 return 0;
1480}
1481
1482static void rcu_torture_onoff_cleanup(void)
1483{
1484 if (onoff_task == NULL)
1485 return;
1486 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1487 kthread_stop(onoff_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001488 onoff_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001489}
1490
1491#else /* #ifdef CONFIG_HOTPLUG_CPU */
1492
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001493static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001494rcu_torture_onoff_init(void)
1495{
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001496 return 0;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001497}
1498
1499static void rcu_torture_onoff_cleanup(void)
1500{
1501}
1502
1503#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1504
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001505/*
1506 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1507 * induces a CPU stall for the time specified by stall_cpu.
1508 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001509static int rcu_torture_stall(void *args)
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001510{
1511 unsigned long stop_at;
1512
1513 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1514 if (stall_cpu_holdoff > 0) {
1515 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1516 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1517 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1518 }
1519 if (!kthread_should_stop()) {
1520 stop_at = get_seconds() + stall_cpu;
1521 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001522 pr_alert("rcu_torture_stall start.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001523 rcu_read_lock();
1524 preempt_disable();
1525 while (ULONG_CMP_LT(get_seconds(), stop_at))
1526 continue; /* Induce RCU CPU stall warning. */
1527 preempt_enable();
1528 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001529 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001530 }
1531 rcutorture_shutdown_absorb("rcu_torture_stall");
1532 while (!kthread_should_stop())
1533 schedule_timeout_interruptible(10 * HZ);
1534 return 0;
1535}
1536
1537/* Spawn CPU-stall kthread, if stall_cpu specified. */
1538static int __init rcu_torture_stall_init(void)
1539{
1540 int ret;
1541
1542 if (stall_cpu <= 0)
1543 return 0;
1544 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1545 if (IS_ERR(stall_task)) {
1546 ret = PTR_ERR(stall_task);
1547 stall_task = NULL;
1548 return ret;
1549 }
1550 return 0;
1551}
1552
1553/* Clean up after the CPU-stall kthread, if one was spawned. */
1554static void rcu_torture_stall_cleanup(void)
1555{
1556 if (stall_task == NULL)
1557 return;
1558 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1559 kthread_stop(stall_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001560 stall_task = NULL;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001561}
1562
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001563/* Callback function for RCU barrier testing. */
1564void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1565{
1566 atomic_inc(&barrier_cbs_invoked);
1567}
1568
1569/* kthread function to register callbacks used to test RCU barriers. */
1570static int rcu_torture_barrier_cbs(void *arg)
1571{
1572 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001573 bool lastphase = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001574 struct rcu_head rcu;
1575
1576 init_rcu_head_on_stack(&rcu);
1577 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1578 set_user_nice(current, 19);
1579 do {
1580 wait_event(barrier_cbs_wq[myid],
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001581 barrier_phase != lastphase ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001582 kthread_should_stop() ||
1583 fullstop != FULLSTOP_DONTSTOP);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001584 lastphase = barrier_phase;
1585 smp_mb(); /* ensure barrier_phase load before ->call(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001586 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1587 break;
1588 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1589 if (atomic_dec_and_test(&barrier_cbs_count))
1590 wake_up(&barrier_wq);
1591 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1592 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1593 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1594 while (!kthread_should_stop())
1595 schedule_timeout_interruptible(1);
1596 cur_ops->cb_barrier();
1597 destroy_rcu_head_on_stack(&rcu);
1598 return 0;
1599}
1600
1601/* kthread function to drive and coordinate RCU barrier testing. */
1602static int rcu_torture_barrier(void *arg)
1603{
1604 int i;
1605
1606 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1607 do {
1608 atomic_set(&barrier_cbs_invoked, 0);
1609 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001610 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1611 barrier_phase = !barrier_phase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001612 for (i = 0; i < n_barrier_cbs; i++)
1613 wake_up(&barrier_cbs_wq[i]);
1614 wait_event(barrier_wq,
1615 atomic_read(&barrier_cbs_count) == 0 ||
1616 kthread_should_stop() ||
1617 fullstop != FULLSTOP_DONTSTOP);
1618 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1619 break;
1620 n_barrier_attempts++;
1621 cur_ops->cb_barrier();
1622 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1623 n_rcu_torture_barrier_error++;
1624 WARN_ON_ONCE(1);
1625 }
1626 n_barrier_successes++;
1627 schedule_timeout_interruptible(HZ / 10);
1628 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1629 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
Paul E. McKenney143aa672012-05-24 18:17:48 -07001630 rcutorture_shutdown_absorb("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001631 while (!kthread_should_stop())
1632 schedule_timeout_interruptible(1);
1633 return 0;
1634}
1635
1636/* Initialize RCU barrier testing. */
1637static int rcu_torture_barrier_init(void)
1638{
1639 int i;
1640 int ret;
1641
1642 if (n_barrier_cbs == 0)
1643 return 0;
1644 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001645 pr_alert("%s" TORTURE_FLAG
1646 " Call or barrier ops missing for %s,\n",
1647 torture_type, cur_ops->name);
1648 pr_alert("%s" TORTURE_FLAG
1649 " RCU barrier testing omitted from run.\n",
1650 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001651 return 0;
1652 }
1653 atomic_set(&barrier_cbs_count, 0);
1654 atomic_set(&barrier_cbs_invoked, 0);
1655 barrier_cbs_tasks =
1656 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1657 GFP_KERNEL);
1658 barrier_cbs_wq =
1659 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1660 GFP_KERNEL);
Sasha Levinde5e6432012-12-20 14:11:28 -05001661 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001662 return -ENOMEM;
1663 for (i = 0; i < n_barrier_cbs; i++) {
1664 init_waitqueue_head(&barrier_cbs_wq[i]);
1665 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
Lai Jiangshan9059c942012-03-19 16:12:14 +08001666 (void *)(long)i,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001667 "rcu_torture_barrier_cbs");
1668 if (IS_ERR(barrier_cbs_tasks[i])) {
1669 ret = PTR_ERR(barrier_cbs_tasks[i]);
1670 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1671 barrier_cbs_tasks[i] = NULL;
1672 return ret;
1673 }
1674 }
1675 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1676 "rcu_torture_barrier");
1677 if (IS_ERR(barrier_task)) {
1678 ret = PTR_ERR(barrier_task);
1679 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1680 barrier_task = NULL;
1681 }
1682 return 0;
1683}
1684
1685/* Clean up after RCU barrier testing. */
1686static void rcu_torture_barrier_cleanup(void)
1687{
1688 int i;
1689
1690 if (barrier_task != NULL) {
1691 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1692 kthread_stop(barrier_task);
1693 barrier_task = NULL;
1694 }
1695 if (barrier_cbs_tasks != NULL) {
1696 for (i = 0; i < n_barrier_cbs; i++) {
1697 if (barrier_cbs_tasks[i] != NULL) {
1698 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1699 kthread_stop(barrier_cbs_tasks[i]);
1700 barrier_cbs_tasks[i] = NULL;
1701 }
1702 }
1703 kfree(barrier_cbs_tasks);
1704 barrier_cbs_tasks = NULL;
1705 }
1706 if (barrier_cbs_wq != NULL) {
1707 kfree(barrier_cbs_wq);
1708 barrier_cbs_wq = NULL;
1709 }
1710}
1711
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001712static int rcutorture_cpu_notify(struct notifier_block *self,
1713 unsigned long action, void *hcpu)
1714{
1715 long cpu = (long)hcpu;
1716
1717 switch (action) {
1718 case CPU_ONLINE:
1719 case CPU_DOWN_FAILED:
1720 (void)rcutorture_booster_init(cpu);
1721 break;
1722 case CPU_DOWN_PREPARE:
1723 rcutorture_booster_cleanup(cpu);
1724 break;
1725 default:
1726 break;
1727 }
1728 return NOTIFY_OK;
1729}
1730
1731static struct notifier_block rcutorture_cpu_nb = {
1732 .notifier_call = rcutorture_cpu_notify,
1733};
1734
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001735static void
1736rcu_torture_cleanup(void)
1737{
1738 int i;
1739
Paul E. McKenney343e9092008-12-15 16:13:07 -08001740 mutex_lock(&fullstop_mutex);
Paul E. McKenney4a298652011-04-03 21:33:51 -07001741 rcutorture_record_test_transition();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001742 if (fullstop == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001743 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001744 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001745 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001746 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001747 if (cur_ops->cb_barrier != NULL)
1748 cur_ops->cb_barrier();
1749 return;
1750 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001751 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001752 mutex_unlock(&fullstop_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001753 unregister_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001754 rcu_torture_barrier_cleanup();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001755 rcu_torture_stall_cleanup();
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001756 if (stutter_task) {
1757 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1758 kthread_stop(stutter_task);
1759 }
1760 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001761 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001762 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1763 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001764 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001765 }
1766 shuffler_task = NULL;
1767
Josh Triplettc8e5b162007-05-08 00:33:20 -07001768 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001769 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1770 kthread_stop(writer_task);
1771 }
1772 writer_task = NULL;
1773
Josh Triplettc8e5b162007-05-08 00:33:20 -07001774 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001775 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001776 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001777 VERBOSE_PRINTK_STRING(
1778 "Stopping rcu_torture_reader task");
1779 kthread_stop(reader_tasks[i]);
1780 }
1781 reader_tasks[i] = NULL;
1782 }
1783 kfree(reader_tasks);
1784 reader_tasks = NULL;
1785 }
1786 rcu_torture_current = NULL;
1787
Josh Triplettc8e5b162007-05-08 00:33:20 -07001788 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001789 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001790 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001791 VERBOSE_PRINTK_STRING(
1792 "Stopping rcu_torture_fakewriter task");
1793 kthread_stop(fakewriter_tasks[i]);
1794 }
1795 fakewriter_tasks[i] = NULL;
1796 }
1797 kfree(fakewriter_tasks);
1798 fakewriter_tasks = NULL;
1799 }
1800
Josh Triplettc8e5b162007-05-08 00:33:20 -07001801 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001802 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1803 kthread_stop(stats_task);
1804 }
1805 stats_task = NULL;
1806
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001807 if (fqs_task) {
1808 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1809 kthread_stop(fqs_task);
1810 }
1811 fqs_task = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001812 if ((test_boost == 1 && cur_ops->can_boost) ||
1813 test_boost == 2) {
1814 unregister_cpu_notifier(&rcutorture_cpu_nb);
1815 for_each_possible_cpu(i)
1816 rcutorture_booster_cleanup(i);
1817 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001818 if (shutdown_task != NULL) {
1819 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1820 kthread_stop(shutdown_task);
1821 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001822 shutdown_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001823 rcu_torture_onoff_cleanup();
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001824
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001825 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001826
1827 if (cur_ops->cb_barrier != NULL)
1828 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001829
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001830 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001831
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001832 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001833 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney091541b2012-01-10 12:51:14 -08001834 else if (n_online_successes != n_online_attempts ||
1835 n_offline_successes != n_offline_attempts)
1836 rcu_torture_print_module_parms(cur_ops,
1837 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001838 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001839 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001840}
1841
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001842#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1843static void rcu_torture_leak_cb(struct rcu_head *rhp)
1844{
1845}
1846
1847static void rcu_torture_err_cb(struct rcu_head *rhp)
1848{
1849 /*
1850 * This -might- happen due to race conditions, but is unlikely.
1851 * The scenario that leads to this happening is that the
1852 * first of the pair of duplicate callbacks is queued,
1853 * someone else starts a grace period that includes that
1854 * callback, then the second of the pair must wait for the
1855 * next grace period. Unlikely, but can happen. If it
1856 * does happen, the debug-objects subsystem won't have splatted.
1857 */
1858 pr_alert("rcutorture: duplicated callback was invoked.\n");
1859}
1860#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1861
1862/*
1863 * Verify that double-free causes debug-objects to complain, but only
1864 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1865 * cannot be carried out.
1866 */
1867static void rcu_test_debug_objects(void)
1868{
1869#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1870 struct rcu_head rh1;
1871 struct rcu_head rh2;
1872
1873 init_rcu_head_on_stack(&rh1);
1874 init_rcu_head_on_stack(&rh2);
1875 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1876
1877 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1878 preempt_disable(); /* Prevent preemption from interrupting test. */
1879 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1880 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1881 local_irq_disable(); /* Make it harder to start a new grace period. */
1882 call_rcu(&rh2, rcu_torture_leak_cb);
1883 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1884 local_irq_enable();
1885 rcu_read_unlock();
1886 preempt_enable();
1887
1888 /* Wait for them all to get done so we can safely return. */
1889 rcu_barrier();
1890 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1891 destroy_rcu_head_on_stack(&rh1);
1892 destroy_rcu_head_on_stack(&rh2);
1893#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1894 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1895#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1896}
1897
Josh Triplett6f8bc502007-05-08 00:25:24 -07001898static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001899rcu_torture_init(void)
1900{
1901 int i;
1902 int cpu;
1903 int firsterr = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001904 int retval;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001905 static struct rcu_torture_ops *torture_ops[] = {
1906 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
1907 };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001908
Paul E. McKenney343e9092008-12-15 16:13:07 -08001909 mutex_lock(&fullstop_mutex);
1910
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001911 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001912 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001913 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001914 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001915 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001916 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001917 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001918 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1919 torture_type);
1920 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001921 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001922 pr_alert(" %s", torture_ops[i]->name);
1923 pr_alert("\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001924 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001925 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001926 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001927 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001928 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001929 fqs_duration = 0;
1930 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001931 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001932 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1933
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001934 if (nreaders >= 0)
1935 nrealreaders = nreaders;
1936 else
1937 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001938 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001939 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001940
1941 /* Set up the freelist. */
1942
1943 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001944 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001945 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001946 list_add_tail(&rcu_tortures[i].rtort_free,
1947 &rcu_torture_freelist);
1948 }
1949
1950 /* Initialize the statistics so that each run gets its own numbers. */
1951
1952 rcu_torture_current = NULL;
1953 rcu_torture_current_version = 0;
1954 atomic_set(&n_rcu_torture_alloc, 0);
1955 atomic_set(&n_rcu_torture_alloc_fail, 0);
1956 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001957 atomic_set(&n_rcu_torture_mberror, 0);
1958 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001959 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001960 n_rcu_torture_boost_ktrerror = 0;
1961 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001962 n_rcu_torture_boost_failure = 0;
1963 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001964 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1965 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001966 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001967 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1968 per_cpu(rcu_torture_count, cpu)[i] = 0;
1969 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1970 }
1971 }
1972
1973 /* Start up the kthreads. */
1974
1975 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
Paul E. McKenney60f53782012-08-25 15:27:40 -07001976 writer_task = kthread_create(rcu_torture_writer, NULL,
1977 "rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001978 if (IS_ERR(writer_task)) {
1979 firsterr = PTR_ERR(writer_task);
1980 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1981 writer_task = NULL;
1982 goto unwind;
1983 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07001984 wake_up_process(writer_task);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001985 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001986 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001987 if (fakewriter_tasks == NULL) {
1988 VERBOSE_PRINTK_ERRSTRING("out of memory");
1989 firsterr = -ENOMEM;
1990 goto unwind;
1991 }
1992 for (i = 0; i < nfakewriters; i++) {
1993 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1994 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001995 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07001996 if (IS_ERR(fakewriter_tasks[i])) {
1997 firsterr = PTR_ERR(fakewriter_tasks[i]);
1998 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1999 fakewriter_tasks[i] = NULL;
2000 goto unwind;
2001 }
2002 }
Josh Triplett2860aab2006-10-04 02:17:11 -07002003 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002004 GFP_KERNEL);
2005 if (reader_tasks == NULL) {
2006 VERBOSE_PRINTK_ERRSTRING("out of memory");
2007 firsterr = -ENOMEM;
2008 goto unwind;
2009 }
2010 for (i = 0; i < nrealreaders; i++) {
2011 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2012 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2013 "rcu_torture_reader");
2014 if (IS_ERR(reader_tasks[i])) {
2015 firsterr = PTR_ERR(reader_tasks[i]);
2016 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2017 reader_tasks[i] = NULL;
2018 goto unwind;
2019 }
2020 }
2021 if (stat_interval > 0) {
2022 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2023 stats_task = kthread_run(rcu_torture_stats, NULL,
2024 "rcu_torture_stats");
2025 if (IS_ERR(stats_task)) {
2026 firsterr = PTR_ERR(stats_task);
2027 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2028 stats_task = NULL;
2029 goto unwind;
2030 }
2031 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002032 if (test_no_idle_hz) {
2033 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002034
2035 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2036 firsterr = -ENOMEM;
2037 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2038 goto unwind;
2039 }
2040
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002041 /* Create the shuffler thread */
2042 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2043 "rcu_torture_shuffle");
2044 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002045 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002046 firsterr = PTR_ERR(shuffler_task);
2047 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2048 shuffler_task = NULL;
2049 goto unwind;
2050 }
2051 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07002052 if (stutter < 0)
2053 stutter = 0;
2054 if (stutter) {
2055 /* Create the stutter thread */
2056 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2057 "rcu_torture_stutter");
2058 if (IS_ERR(stutter_task)) {
2059 firsterr = PTR_ERR(stutter_task);
2060 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2061 stutter_task = NULL;
2062 goto unwind;
2063 }
2064 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002065 if (fqs_duration < 0)
2066 fqs_duration = 0;
2067 if (fqs_duration) {
2068 /* Create the stutter thread */
2069 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2070 "rcu_torture_fqs");
2071 if (IS_ERR(fqs_task)) {
2072 firsterr = PTR_ERR(fqs_task);
2073 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2074 fqs_task = NULL;
2075 goto unwind;
2076 }
2077 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002078 if (test_boost_interval < 1)
2079 test_boost_interval = 1;
2080 if (test_boost_duration < 2)
2081 test_boost_duration = 2;
2082 if ((test_boost == 1 && cur_ops->can_boost) ||
2083 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002084
2085 boost_starttime = jiffies + test_boost_interval * HZ;
2086 register_cpu_notifier(&rcutorture_cpu_nb);
2087 for_each_possible_cpu(i) {
2088 if (cpu_is_offline(i))
2089 continue; /* Heuristic: CPU can go offline. */
2090 retval = rcutorture_booster_init(i);
2091 if (retval < 0) {
2092 firsterr = retval;
2093 goto unwind;
2094 }
2095 }
2096 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002097 if (shutdown_secs > 0) {
2098 shutdown_time = jiffies + shutdown_secs * HZ;
Paul E. McKenney60f53782012-08-25 15:27:40 -07002099 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2100 "rcu_torture_shutdown");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002101 if (IS_ERR(shutdown_task)) {
2102 firsterr = PTR_ERR(shutdown_task);
2103 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2104 shutdown_task = NULL;
2105 goto unwind;
2106 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002107 wake_up_process(shutdown_task);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002108 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002109 i = rcu_torture_onoff_init();
2110 if (i != 0) {
2111 firsterr = i;
2112 goto unwind;
2113 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002114 register_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002115 i = rcu_torture_stall_init();
2116 if (i != 0) {
2117 firsterr = i;
2118 goto unwind;
2119 }
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002120 retval = rcu_torture_barrier_init();
2121 if (retval != 0) {
2122 firsterr = retval;
2123 goto unwind;
2124 }
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07002125 if (object_debug)
2126 rcu_test_debug_objects();
Paul E. McKenney4a298652011-04-03 21:33:51 -07002127 rcutorture_record_test_transition();
Paul E. McKenney343e9092008-12-15 16:13:07 -08002128 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002129 return 0;
2130
2131unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08002132 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002133 rcu_torture_cleanup();
2134 return firsterr;
2135}
2136
2137module_init(rcu_torture_init);
2138module_exit(rcu_torture_cleanup);