blob: 3929cd451511ef18b03c33d33a34d9b263151153 [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
Paul E. McKenney4102ada2013-10-08 20:23:47 -070055MODULE_ALIAS("rcutorture");
56#ifdef MODULE_PARAM_PREFIX
57#undef MODULE_PARAM_PREFIX
58#endif
59#define MODULE_PARAM_PREFIX "rcutorture."
60
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070061static int fqs_duration;
Paul E. McKenneybf66f182010-01-04 15:09:10 -080062module_param(fqs_duration, int, 0444);
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070063MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us), 0 to disable");
64static int fqs_holdoff;
Paul E. McKenneybf66f182010-01-04 15:09:10 -080065module_param(fqs_holdoff, int, 0444);
66MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070067static int fqs_stutter = 3;
Paul E. McKenneybf66f182010-01-04 15:09:10 -080068module_param(fqs_stutter, int, 0444);
69MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070070static bool gp_exp;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -070071module_param(gp_exp, bool, 0444);
72MODULE_PARM_DESC(gp_exp, "Use expedited GP wait primitives");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070073static bool gp_normal;
74module_param(gp_normal, bool, 0444);
75MODULE_PARM_DESC(gp_normal, "Use normal (non-expedited) GP wait primitives");
76static int irqreader = 1;
77module_param(irqreader, int, 0444);
78MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
79static int n_barrier_cbs;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -080080module_param(n_barrier_cbs, int, 0444);
81MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070082static int nfakewriters = 4;
83module_param(nfakewriters, int, 0444);
84MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
85static int nreaders = -1;
86module_param(nreaders, int, 0444);
87MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
88static int object_debug;
Paul E. McKenneyd2818df2013-04-23 17:05:42 -070089module_param(object_debug, int, 0444);
90MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070091static int onoff_holdoff;
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -080092module_param(onoff_holdoff, int, 0444);
93MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -070094static int onoff_interval;
95module_param(onoff_interval, int, 0444);
96MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
97static int shuffle_interval = 3;
98module_param(shuffle_interval, int, 0444);
99MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
100static int shutdown_secs;
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700101module_param(shutdown_secs, int, 0444);
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700102MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), <= zero to disable.");
103static int stall_cpu;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800104module_param(stall_cpu, int, 0444);
105MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700106static int stall_cpu_holdoff = 10;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800107module_param(stall_cpu_holdoff, int, 0444);
108MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700109static int stat_interval = 60;
110module_param(stat_interval, int, 0644);
111MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
112static int stutter = 5;
113module_param(stutter, int, 0444);
114MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
115static int test_boost = 1;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700116module_param(test_boost, int, 0444);
117MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700118static int test_boost_duration = 4;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700119module_param(test_boost_duration, int, 0444);
120MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700121static int test_boost_interval = 7;
122module_param(test_boost_interval, int, 0444);
123MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
124static bool test_no_idle_hz = true;
125module_param(test_no_idle_hz, bool, 0444);
126MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
127static char *torture_type = "rcu";
Josh Triplettd6ad6712007-03-06 01:42:13 -0800128module_param(torture_type, charp, 0444);
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700129MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
130static bool verbose;
131module_param(verbose, bool, 0444);
132MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700133
134#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800135#define PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700136 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800137#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700138 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800139#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700140 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800141
142static char printk_buf[4096];
143
144static int nrealreaders;
145static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700146static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800147static struct task_struct **reader_tasks;
148static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800149static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700150static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800151static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700152static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700153static struct task_struct *shutdown_task;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800154#ifdef CONFIG_HOTPLUG_CPU
155static struct task_struct *onoff_task;
156#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800157static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800158static struct task_struct **barrier_cbs_tasks;
159static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800160
161#define RCU_TORTURE_PIPE_LEN 10
162
163struct rcu_torture {
164 struct rcu_head rtort_rcu;
165 int rtort_pipe_count;
166 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800167 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800168};
169
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800170static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700171static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700172static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800173static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
174static DEFINE_SPINLOCK(rcu_torture_lock);
175static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
176 { 0 };
177static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
178 { 0 };
179static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700180static atomic_t n_rcu_torture_alloc;
181static atomic_t n_rcu_torture_alloc_fail;
182static atomic_t n_rcu_torture_free;
183static atomic_t n_rcu_torture_mberror;
184static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800185static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700186static long n_rcu_torture_boost_ktrerror;
187static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700188static long n_rcu_torture_boost_failure;
189static long n_rcu_torture_boosts;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700190static long n_rcu_torture_timers;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800191static long n_offline_attempts;
192static long n_offline_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700193static unsigned long sum_offline;
194static int min_offline = -1;
195static int max_offline;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800196static long n_online_attempts;
197static long n_online_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700198static unsigned long sum_online;
199static int min_online = -1;
200static int max_online;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800201static long n_barrier_attempts;
202static long n_barrier_successes;
Josh Triplette3033732006-10-04 02:17:14 -0700203static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600204static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800205
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700206static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700207
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700208#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
209#define RCUTORTURE_RUNNABLE_INIT 1
210#else
211#define RCUTORTURE_RUNNABLE_INIT 0
212#endif
213int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
Paul E. McKenneybb3bf702011-11-04 13:24:07 -0700214module_param(rcutorture_runnable, int, 0444);
215MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700216
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700217#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700218#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700219#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700220#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700221#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700222
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500223#ifdef CONFIG_RCU_TRACE
224static u64 notrace rcu_trace_clock_local(void)
225{
226 u64 ts = trace_clock_local();
227 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
228 return ts;
229}
230#else /* #ifdef CONFIG_RCU_TRACE */
231static u64 notrace rcu_trace_clock_local(void)
232{
233 return 0ULL;
234}
235#endif /* #else #ifdef CONFIG_RCU_TRACE */
236
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700237static unsigned long shutdown_time; /* jiffies to system shutdown. */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700238static unsigned long boost_starttime; /* jiffies of next boost test start. */
239DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
240 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800241static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700242static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800243static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
244static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
245static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700246
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800247/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
248
249#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
250#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
251#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
252static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700253/*
254 * Protect fullstop transitions and spawning of kthreads.
255 */
256static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800257
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700258/* Forward reference. */
259static void rcu_torture_cleanup(void);
260
Paul E. McKenney343e9092008-12-15 16:13:07 -0800261/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800262 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800263 */
264static int
265rcutorture_shutdown_notify(struct notifier_block *unused1,
266 unsigned long unused2, void *unused3)
267{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800268 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800269 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800270 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800271 else
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700272 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800273 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800274 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800275 return NOTIFY_DONE;
276}
277
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800278/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800279 * Absorb kthreads into a kernel function that won't return, so that
280 * they won't ever access module text or data again.
281 */
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400282static void rcutorture_shutdown_absorb(const char *title)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800283{
284 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700285 pr_notice(
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800286 "rcutorture thread %s parking due to system shutdown\n",
287 title);
288 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
289 }
290}
291
292/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800293 * Allocate an element from the rcu_tortures pool.
294 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800295static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800296rcu_torture_alloc(void)
297{
298 struct list_head *p;
299
Ingo Molnaradac1662006-01-25 19:50:12 +0100300 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800301 if (list_empty(&rcu_torture_freelist)) {
302 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100303 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800304 return NULL;
305 }
306 atomic_inc(&n_rcu_torture_alloc);
307 p = rcu_torture_freelist.next;
308 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100309 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800310 return container_of(p, struct rcu_torture, rtort_free);
311}
312
313/*
314 * Free an element to the rcu_tortures pool.
315 */
316static void
317rcu_torture_free(struct rcu_torture *p)
318{
319 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100320 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800321 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100322 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800323}
324
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800325struct rcu_random_state {
326 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700327 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800328};
329
330#define RCU_RANDOM_MULT 39916801 /* prime */
331#define RCU_RANDOM_ADD 479001701 /* prime */
332#define RCU_RANDOM_REFRESH 10000
333
334#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
335
336/*
337 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700338 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800339 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700340static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800341rcu_random(struct rcu_random_state *rrsp)
342{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800343 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200344 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800345 rrsp->rrs_count = RCU_RANDOM_REFRESH;
346 }
347 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
348 return swahw32(rrsp->rrs_state);
349}
350
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700351static void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400352rcu_stutter_wait(const char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700353{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800354 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700355 if (rcutorture_runnable)
356 schedule_timeout_interruptible(1);
357 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700358 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800359 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800360 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700361}
362
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800363/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700364 * Operations vector for selecting different types of tests.
365 */
366
367struct rcu_torture_ops {
368 void (*init)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700369 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700370 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700371 void (*readunlock)(int idx);
372 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700373 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700374 void (*sync)(void);
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700375 void (*exp_sync)(void);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800376 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
Paul E. McKenney23269742008-05-12 21:21:05 +0200377 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800378 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700379 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700380 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700381 int can_boost;
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400382 const char *name;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700383};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700384
385static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700386
387/*
388 * Definitions for rcu torture testing.
389 */
390
Josh Tripletta49a4af2006-09-29 01:59:30 -0700391static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700392{
393 rcu_read_lock();
394 return 0;
395}
396
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700397static void rcu_read_delay(struct rcu_random_state *rrsp)
398{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700399 const unsigned long shortdelay_us = 200;
400 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700401
Josh Triplettb8d57a72009-09-08 15:54:35 -0700402 /* We want a short delay sometimes to make a reader delay the grace
403 * period, and we want a long delay occasionally to trigger
404 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700405
Josh Triplettb8d57a72009-09-08 15:54:35 -0700406 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
407 mdelay(longdelay_ms);
408 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
409 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800410#ifdef CONFIG_PREEMPT
411 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
412 preempt_schedule(); /* No QS if preempt_disable() in effect */
413#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700414}
415
Josh Tripletta49a4af2006-09-29 01:59:30 -0700416static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700417{
418 rcu_read_unlock();
419}
420
421static int rcu_torture_completed(void)
422{
423 return rcu_batches_completed();
424}
425
426static void
427rcu_torture_cb(struct rcu_head *p)
428{
429 int i;
430 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
431
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800432 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700433 /* Test is ending, just drop callbacks on the floor. */
434 /* The next initialization will pick up the pieces. */
435 return;
436 }
437 i = rp->rtort_pipe_count;
438 if (i > RCU_TORTURE_PIPE_LEN)
439 i = RCU_TORTURE_PIPE_LEN;
440 atomic_inc(&rcu_torture_wcount[i]);
441 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
442 rp->rtort_mbtest = 0;
443 rcu_torture_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700444 } else {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700445 cur_ops->deferred_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700446 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700447}
448
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800449static int rcu_no_completed(void)
450{
451 return 0;
452}
453
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700454static void rcu_torture_deferred_free(struct rcu_torture *p)
455{
456 call_rcu(&p->rtort_rcu, rcu_torture_cb);
457}
458
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700459static void rcu_sync_torture_init(void)
460{
461 INIT_LIST_HEAD(&rcu_torture_removed);
462}
463
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700464static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700465 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700466 .readlock = rcu_torture_read_lock,
467 .read_delay = rcu_read_delay,
468 .readunlock = rcu_torture_read_unlock,
469 .completed = rcu_torture_completed,
470 .deferred_free = rcu_torture_deferred_free,
471 .sync = synchronize_rcu,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700472 .exp_sync = synchronize_rcu_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800473 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700474 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800475 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700476 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700477 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700478 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700479 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700480};
481
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700482/*
483 * Definitions for rcu_bh torture testing.
484 */
485
Josh Tripletta49a4af2006-09-29 01:59:30 -0700486static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700487{
488 rcu_read_lock_bh();
489 return 0;
490}
491
Josh Tripletta49a4af2006-09-29 01:59:30 -0700492static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700493{
494 rcu_read_unlock_bh();
495}
496
497static int rcu_bh_torture_completed(void)
498{
499 return rcu_batches_completed_bh();
500}
501
502static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
503{
504 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
505}
506
507static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700508 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700509 .readlock = rcu_bh_torture_read_lock,
510 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
511 .readunlock = rcu_bh_torture_read_unlock,
512 .completed = rcu_bh_torture_completed,
513 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700514 .sync = synchronize_rcu_bh,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700515 .exp_sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800516 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700517 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800518 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700519 .stats = NULL,
520 .irq_capable = 1,
521 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700522};
523
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700524/*
525 * Definitions for srcu torture testing.
526 */
527
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800528DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700529
Josh Triplett012d3ca2006-12-06 20:40:19 -0800530static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700531{
532 return srcu_read_lock(&srcu_ctl);
533}
534
535static void srcu_read_delay(struct rcu_random_state *rrsp)
536{
537 long delay;
538 const long uspertick = 1000000 / HZ;
539 const long longdelay = 10;
540
541 /* We want there to be long-running readers, but not all the time. */
542
543 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
544 if (!delay)
545 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800546 else
547 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700548}
549
Josh Triplett012d3ca2006-12-06 20:40:19 -0800550static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700551{
552 srcu_read_unlock(&srcu_ctl, idx);
553}
554
555static int srcu_torture_completed(void)
556{
557 return srcu_batches_completed(&srcu_ctl);
558}
559
Lai Jiangshan9059c942012-03-19 16:12:14 +0800560static void srcu_torture_deferred_free(struct rcu_torture *rp)
561{
562 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
563}
564
Josh Triplettb772e1d2006-10-04 02:17:13 -0700565static void srcu_torture_synchronize(void)
566{
567 synchronize_srcu(&srcu_ctl);
568}
569
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700570static void srcu_torture_call(struct rcu_head *head,
571 void (*func)(struct rcu_head *head))
572{
573 call_srcu(&srcu_ctl, head, func);
574}
575
576static void srcu_torture_barrier(void)
577{
578 srcu_barrier(&srcu_ctl);
579}
580
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700581static int srcu_torture_stats(char *page)
582{
583 int cnt = 0;
584 int cpu;
585 int idx = srcu_ctl.completed & 0x1;
586
587 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
588 torture_type, TORTURE_FLAG, idx);
589 for_each_possible_cpu(cpu) {
Paul E. McKenneycef50122012-02-05 07:42:44 -0800590 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700591 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
592 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
593 }
594 cnt += sprintf(&page[cnt], "\n");
595 return cnt;
596}
597
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700598static void srcu_torture_synchronize_expedited(void)
599{
600 synchronize_srcu_expedited(&srcu_ctl);
601}
602
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700603static struct rcu_torture_ops srcu_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800604 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700605 .readlock = srcu_torture_read_lock,
606 .read_delay = srcu_read_delay,
607 .readunlock = srcu_torture_read_unlock,
608 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800609 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700610 .sync = srcu_torture_synchronize,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700611 .exp_sync = srcu_torture_synchronize_expedited,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700612 .call = srcu_torture_call,
613 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700614 .stats = srcu_torture_stats,
615 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700616};
617
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700618/*
619 * Definitions for sched torture testing.
620 */
621
622static int sched_torture_read_lock(void)
623{
624 preempt_disable();
625 return 0;
626}
627
628static void sched_torture_read_unlock(int idx)
629{
630 preempt_enable();
631}
632
Paul E. McKenney23269742008-05-12 21:21:05 +0200633static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
634{
635 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
636}
637
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700638static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700639 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700640 .readlock = sched_torture_read_lock,
641 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
642 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800643 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700644 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700645 .sync = synchronize_sched,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700646 .exp_sync = synchronize_sched_expedited,
647 .call = call_rcu_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700648 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800649 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700650 .stats = NULL,
651 .irq_capable = 1,
652 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700653};
654
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700655/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700656 * RCU torture priority-boost testing. Runs one real-time thread per
657 * CPU for moderate bursts, repeatedly registering RCU callbacks and
658 * spinning waiting for them to be invoked. If a given callback takes
659 * too long to be invoked, we assume that priority inversion has occurred.
660 */
661
662struct rcu_boost_inflight {
663 struct rcu_head rcu;
664 int inflight;
665};
666
667static void rcu_torture_boost_cb(struct rcu_head *head)
668{
669 struct rcu_boost_inflight *rbip =
670 container_of(head, struct rcu_boost_inflight, rcu);
671
672 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
673 rbip->inflight = 0;
674}
675
676static int rcu_torture_boost(void *arg)
677{
678 unsigned long call_rcu_time;
679 unsigned long endtime;
680 unsigned long oldstarttime;
681 struct rcu_boost_inflight rbi = { .inflight = 0 };
682 struct sched_param sp;
683
684 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
685
686 /* Set real-time priority. */
687 sp.sched_priority = 1;
688 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
689 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
690 n_rcu_torture_boost_rterror++;
691 }
692
Paul E. McKenney561190e2011-03-30 09:10:44 -0700693 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700694 /* Each pass through the following loop does one boost-test cycle. */
695 do {
696 /* Wait for the next test interval. */
697 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700698 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -0800699 schedule_timeout_interruptible(oldstarttime - jiffies);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700700 rcu_stutter_wait("rcu_torture_boost");
701 if (kthread_should_stop() ||
702 fullstop != FULLSTOP_DONTSTOP)
703 goto checkwait;
704 }
705
706 /* Do one boost-test interval. */
707 endtime = oldstarttime + test_boost_duration * HZ;
708 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700709 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700710 /* If we don't have a callback in flight, post one. */
711 if (!rbi.inflight) {
712 smp_mb(); /* RCU core before ->inflight = 1. */
713 rbi.inflight = 1;
714 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
715 if (jiffies - call_rcu_time >
716 test_boost_duration * HZ - HZ / 2) {
717 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
718 n_rcu_torture_boost_failure++;
719 }
720 call_rcu_time = jiffies;
721 }
722 cond_resched();
723 rcu_stutter_wait("rcu_torture_boost");
724 if (kthread_should_stop() ||
725 fullstop != FULLSTOP_DONTSTOP)
726 goto checkwait;
727 }
728
729 /*
730 * Set the start time of the next test interval.
731 * Yes, this is vulnerable to long delays, but such
732 * delays simply cause a false negative for the next
733 * interval. Besides, we are running at RT priority,
734 * so delays should be relatively rare.
735 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700736 while (oldstarttime == boost_starttime &&
737 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700738 if (mutex_trylock(&boost_mutex)) {
739 boost_starttime = jiffies +
740 test_boost_interval * HZ;
741 n_rcu_torture_boosts++;
742 mutex_unlock(&boost_mutex);
743 break;
744 }
745 schedule_timeout_uninterruptible(1);
746 }
747
748 /* Go do the stutter. */
749checkwait: rcu_stutter_wait("rcu_torture_boost");
750 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
751
752 /* Clean up and exit. */
753 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
754 rcutorture_shutdown_absorb("rcu_torture_boost");
755 while (!kthread_should_stop() || rbi.inflight)
756 schedule_timeout_uninterruptible(1);
757 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
Paul E. McKenney9d681972011-06-21 01:48:03 -0700758 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700759 return 0;
760}
761
762/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800763 * RCU torture force-quiescent-state kthread. Repeatedly induces
764 * bursts of calls to force_quiescent_state(), increasing the probability
765 * of occurrence of some important types of race conditions.
766 */
767static int
768rcu_torture_fqs(void *arg)
769{
770 unsigned long fqs_resume_time;
771 int fqs_burst_remaining;
772
773 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
774 do {
775 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700776 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
777 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800778 schedule_timeout_interruptible(1);
779 }
780 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700781 while (fqs_burst_remaining > 0 &&
782 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800783 cur_ops->fqs();
784 udelay(fqs_holdoff);
785 fqs_burst_remaining -= fqs_holdoff;
786 }
787 rcu_stutter_wait("rcu_torture_fqs");
788 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
789 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
790 rcutorture_shutdown_absorb("rcu_torture_fqs");
791 while (!kthread_should_stop())
792 schedule_timeout_uninterruptible(1);
793 return 0;
794}
795
796/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800797 * RCU torture writer kthread. Repeatedly substitutes a new structure
798 * for that pointed to by rcu_torture_current, freeing the old structure
799 * after a series of grace periods (the "pipeline").
800 */
801static int
802rcu_torture_writer(void *arg)
803{
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700804 bool exp;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800805 int i;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800806 struct rcu_torture *rp;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700807 struct rcu_torture *rp1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800808 struct rcu_torture *old_rp;
809 static DEFINE_RCU_RANDOM(rand);
810
811 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800812 set_user_nice(current, 19);
813
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800814 do {
815 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700816 rp = rcu_torture_alloc();
817 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800818 continue;
819 rp->rtort_pipe_count = 0;
820 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700821 old_rp = rcu_dereference_check(rcu_torture_current,
822 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800823 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800824 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700825 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700826 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800827 i = old_rp->rtort_pipe_count;
828 if (i > RCU_TORTURE_PIPE_LEN)
829 i = RCU_TORTURE_PIPE_LEN;
830 atomic_inc(&rcu_torture_wcount[i]);
831 old_rp->rtort_pipe_count++;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700832 if (gp_normal == gp_exp)
833 exp = !!(rcu_random(&rand) & 0x80);
834 else
835 exp = gp_exp;
836 if (!exp) {
837 cur_ops->deferred_free(old_rp);
838 } else {
839 cur_ops->exp_sync();
840 list_add(&old_rp->rtort_free,
841 &rcu_torture_removed);
842 list_for_each_entry_safe(rp, rp1,
843 &rcu_torture_removed,
844 rtort_free) {
845 i = rp->rtort_pipe_count;
846 if (i > RCU_TORTURE_PIPE_LEN)
847 i = RCU_TORTURE_PIPE_LEN;
848 atomic_inc(&rcu_torture_wcount[i]);
849 if (++rp->rtort_pipe_count >=
850 RCU_TORTURE_PIPE_LEN) {
851 rp->rtort_mbtest = 0;
852 list_del(&rp->rtort_free);
853 rcu_torture_free(rp);
854 }
855 }
856 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800857 }
Paul E. McKenney4a298652011-04-03 21:33:51 -0700858 rcutorture_record_progress(++rcu_torture_current_version);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800859 rcu_stutter_wait("rcu_torture_writer");
860 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800861 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800862 rcutorture_shutdown_absorb("rcu_torture_writer");
863 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800864 schedule_timeout_uninterruptible(1);
865 return 0;
866}
867
868/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700869 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
870 * delay between calls.
871 */
872static int
873rcu_torture_fakewriter(void *arg)
874{
875 DEFINE_RCU_RANDOM(rand);
876
877 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
878 set_user_nice(current, 19);
879
880 do {
881 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
882 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -0700883 if (cur_ops->cb_barrier != NULL &&
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700884 rcu_random(&rand) % (nfakewriters * 8) == 0) {
Paul E. McKenney72472a02012-05-29 17:50:51 -0700885 cur_ops->cb_barrier();
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700886 } else if (gp_normal == gp_exp) {
887 if (rcu_random(&rand) & 0x80)
888 cur_ops->sync();
889 else
890 cur_ops->exp_sync();
891 } else if (gp_normal) {
Paul E. McKenney72472a02012-05-29 17:50:51 -0700892 cur_ops->sync();
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700893 } else {
894 cur_ops->exp_sync();
895 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800896 rcu_stutter_wait("rcu_torture_fakewriter");
897 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700898
899 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800900 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
901 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -0700902 schedule_timeout_uninterruptible(1);
903 return 0;
904}
905
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700906void rcutorture_trace_dump(void)
907{
908 static atomic_t beenhere = ATOMIC_INIT(0);
909
910 if (atomic_read(&beenhere))
911 return;
912 if (atomic_xchg(&beenhere, 1) != 0)
913 return;
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700914 ftrace_dump(DUMP_ALL);
915}
916
Josh Triplettb772e1d2006-10-04 02:17:13 -0700917/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700918 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
919 * incrementing the corresponding element of the pipeline array. The
920 * counter in the element should never be greater than 1, otherwise, the
921 * RCU implementation is broken.
922 */
923static void rcu_torture_timer(unsigned long unused)
924{
925 int idx;
926 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -0800927 int completed_end;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700928 static DEFINE_RCU_RANDOM(rand);
929 static DEFINE_SPINLOCK(rand_lock);
930 struct rcu_torture *p;
931 int pipe_count;
Paul E. McKenney52494532012-11-14 16:26:40 -0800932 unsigned long long ts;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700933
934 idx = cur_ops->readlock();
935 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500936 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -0800937 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -0800938 rcu_read_lock_bh_held() ||
939 rcu_read_lock_sched_held() ||
940 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700941 if (p == NULL) {
942 /* Leave because rcu_torture_writer is not yet underway */
943 cur_ops->readunlock(idx);
944 return;
945 }
946 if (p->rtort_mbtest == 0)
947 atomic_inc(&n_rcu_torture_mberror);
948 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700949 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700950 n_rcu_torture_timers++;
951 spin_unlock(&rand_lock);
952 preempt_disable();
953 pipe_count = p->rtort_pipe_count;
954 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
955 /* Should not happen, but... */
956 pipe_count = RCU_TORTURE_PIPE_LEN;
957 }
Paul E. McKenney52494532012-11-14 16:26:40 -0800958 completed_end = cur_ops->completed();
959 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -0800960 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
961 completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700962 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -0800963 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900964 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -0800965 completed = completed_end - completed;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700966 if (completed > RCU_TORTURE_PIPE_LEN) {
967 /* Should not happen, but... */
968 completed = RCU_TORTURE_PIPE_LEN;
969 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900970 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700971 preempt_enable();
972 cur_ops->readunlock(idx);
973}
974
975/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800976 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
977 * incrementing the corresponding element of the pipeline array. The
978 * counter in the element should never be greater than 1, otherwise, the
979 * RCU implementation is broken.
980 */
981static int
982rcu_torture_reader(void *arg)
983{
984 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -0800985 int completed_end;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700986 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800987 DEFINE_RCU_RANDOM(rand);
988 struct rcu_torture *p;
989 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700990 struct timer_list t;
Paul E. McKenney52494532012-11-14 16:26:40 -0800991 unsigned long long ts;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800992
993 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800994 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700995 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -0700996 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800997
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800998 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700999 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001000 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001001 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001002 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001003 idx = cur_ops->readlock();
1004 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -05001005 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001006 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001007 rcu_read_lock_bh_held() ||
1008 rcu_read_lock_sched_held() ||
1009 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001010 if (p == NULL) {
1011 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001012 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001013 schedule_timeout_interruptible(HZ);
1014 continue;
1015 }
Paul E. McKenney996417d2005-11-18 01:10:50 -08001016 if (p->rtort_mbtest == 0)
1017 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001018 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001019 preempt_disable();
1020 pipe_count = p->rtort_pipe_count;
1021 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1022 /* Should not happen, but... */
1023 pipe_count = RCU_TORTURE_PIPE_LEN;
1024 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001025 completed_end = cur_ops->completed();
1026 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -08001027 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1028 ts, completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001029 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001030 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001031 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001032 completed = completed_end - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001033 if (completed > RCU_TORTURE_PIPE_LEN) {
1034 /* Should not happen, but... */
1035 completed = RCU_TORTURE_PIPE_LEN;
1036 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001037 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001038 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001039 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001040 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001041 rcu_stutter_wait("rcu_torture_reader");
1042 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001043 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001044 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001045 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001046 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001047 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001048 schedule_timeout_uninterruptible(1);
1049 return 0;
1050}
1051
1052/*
1053 * Create an RCU-torture statistics message in the specified buffer.
1054 */
1055static int
1056rcu_torture_printk(char *page)
1057{
1058 int cnt = 0;
1059 int cpu;
1060 int i;
1061 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1062 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1063
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001064 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001065 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1066 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1067 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1068 }
1069 }
1070 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1071 if (pipesummary[i] != 0)
1072 break;
1073 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001074 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001075 cnt += sprintf(&page[cnt],
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001076 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001077 rcu_torture_current,
1078 rcu_torture_current_version,
1079 list_empty(&rcu_torture_freelist),
1080 atomic_read(&n_rcu_torture_alloc),
1081 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001082 atomic_read(&n_rcu_torture_free));
1083 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001084 atomic_read(&n_rcu_torture_mberror),
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001085 n_rcu_torture_boost_ktrerror,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001086 n_rcu_torture_boost_rterror);
1087 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001088 n_rcu_torture_boost_failure,
1089 n_rcu_torture_boosts,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001090 n_rcu_torture_timers);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001091 cnt += sprintf(&page[cnt],
1092 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1093 n_online_successes, n_online_attempts,
1094 n_offline_successes, n_offline_attempts,
1095 min_online, max_online,
1096 min_offline, max_offline,
1097 sum_online, sum_offline, HZ);
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001098 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001099 n_barrier_successes,
1100 n_barrier_attempts,
1101 n_rcu_torture_barrier_error);
1102 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001103 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001104 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001105 n_rcu_torture_boost_ktrerror != 0 ||
1106 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001107 n_rcu_torture_boost_failure != 0 ||
1108 i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001109 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001110 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001111 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001112 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001113 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1114 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1115 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001116 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001117 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001118 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001119 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001120 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001121 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1122 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1123 cnt += sprintf(&page[cnt], " %d",
1124 atomic_read(&rcu_torture_wcount[i]));
1125 }
1126 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -07001127 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001128 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001129 return cnt;
1130}
1131
1132/*
1133 * Print torture statistics. Caller must ensure that there is only
1134 * one call to this function at a given time!!! This is normally
1135 * accomplished by relying on the module system to only have one copy
1136 * of the module loaded, and then by giving the rcu_torture_stats
1137 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1138 * thread is not running).
1139 */
1140static void
1141rcu_torture_stats_print(void)
1142{
1143 int cnt;
1144
1145 cnt = rcu_torture_printk(printk_buf);
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001146 pr_alert("%s", printk_buf);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001147}
1148
1149/*
1150 * Periodically prints torture statistics, if periodic statistics printing
1151 * was specified via the stat_interval module parameter.
1152 *
1153 * No need to worry about fullstop here, since this one doesn't reference
1154 * volatile state or register callbacks.
1155 */
1156static int
1157rcu_torture_stats(void *arg)
1158{
1159 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1160 do {
1161 schedule_timeout_interruptible(stat_interval * HZ);
1162 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001163 rcutorture_shutdown_absorb("rcu_torture_stats");
1164 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001165 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1166 return 0;
1167}
1168
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001169static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1170
1171/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1172 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1173 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001174static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001175{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001176 int i;
1177
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001178 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001179 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001180
1181 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001182 if (num_online_cpus() == 1) {
1183 put_online_cpus();
1184 return;
1185 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001186
1187 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001188 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001189
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001190 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001191
Josh Triplettc8e5b162007-05-08 00:33:20 -07001192 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001193 for (i = 0; i < nrealreaders; i++)
1194 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001195 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001196 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001197 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001198 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001199 for (i = 0; i < nfakewriters; i++)
1200 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001201 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001202 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001203 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001204 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001205 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001206 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001207 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -08001208 if (stutter_task)
1209 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1210 if (fqs_task)
1211 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1212 if (shutdown_task)
1213 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1214#ifdef CONFIG_HOTPLUG_CPU
1215 if (onoff_task)
1216 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1217#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1218 if (stall_task)
1219 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1220 if (barrier_cbs_tasks)
1221 for (i = 0; i < n_barrier_cbs; i++)
1222 if (barrier_cbs_tasks[i])
1223 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1224 shuffle_tmp_mask);
1225 if (barrier_task)
1226 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001227
1228 if (rcu_idle_cpu == -1)
1229 rcu_idle_cpu = num_online_cpus() - 1;
1230 else
1231 rcu_idle_cpu--;
1232
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001233 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001234}
1235
1236/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1237 * system to become idle at a time and cut off its timer ticks. This is meant
1238 * to test the support for such tickless idle CPU in RCU.
1239 */
1240static int
1241rcu_torture_shuffle(void *arg)
1242{
1243 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1244 do {
1245 schedule_timeout_interruptible(shuffle_interval * HZ);
1246 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001247 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1248 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001249 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1250 return 0;
1251}
1252
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001253/* Cause the rcutorture test to "stutter", starting and stopping all
1254 * threads periodically.
1255 */
1256static int
1257rcu_torture_stutter(void *arg)
1258{
1259 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1260 do {
1261 schedule_timeout_interruptible(stutter * HZ);
1262 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001263 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001264 schedule_timeout_interruptible(stutter * HZ);
1265 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001266 rcutorture_shutdown_absorb("rcu_torture_stutter");
1267 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001268 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1269 return 0;
1270}
1271
Paul E. McKenney95c38322006-03-24 03:15:58 -08001272static inline void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04001273rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001274{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001275 pr_alert("%s" TORTURE_FLAG
1276 "--- %s: nreaders=%d nfakewriters=%d "
1277 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1278 "shuffle_interval=%d stutter=%d irqreader=%d "
1279 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1280 "test_boost=%d/%d test_boost_interval=%d "
1281 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001282 "stall_cpu=%d stall_cpu_holdoff=%d "
1283 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001284 "onoff_interval=%d onoff_holdoff=%d\n",
1285 torture_type, tag, nrealreaders, nfakewriters,
1286 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1287 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1288 test_boost, cur_ops->can_boost,
1289 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001290 stall_cpu, stall_cpu_holdoff,
1291 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001292 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001293}
1294
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001295static struct notifier_block rcutorture_shutdown_nb = {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001296 .notifier_call = rcutorture_shutdown_notify,
1297};
1298
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001299static void rcutorture_booster_cleanup(int cpu)
1300{
1301 struct task_struct *t;
1302
1303 if (boost_tasks[cpu] == NULL)
1304 return;
1305 mutex_lock(&boost_mutex);
1306 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1307 t = boost_tasks[cpu];
1308 boost_tasks[cpu] = NULL;
1309 mutex_unlock(&boost_mutex);
1310
1311 /* This must be outside of the mutex, otherwise deadlock! */
1312 kthread_stop(t);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001313 boost_tasks[cpu] = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001314}
1315
1316static int rcutorture_booster_init(int cpu)
1317{
1318 int retval;
1319
1320 if (boost_tasks[cpu] != NULL)
1321 return 0; /* Already created, nothing more to do. */
1322
1323 /* Don't allow time recalculation while creating a new task. */
1324 mutex_lock(&boost_mutex);
1325 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001326 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1327 cpu_to_node(cpu),
1328 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001329 if (IS_ERR(boost_tasks[cpu])) {
1330 retval = PTR_ERR(boost_tasks[cpu]);
1331 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1332 n_rcu_torture_boost_ktrerror++;
1333 boost_tasks[cpu] = NULL;
1334 mutex_unlock(&boost_mutex);
1335 return retval;
1336 }
1337 kthread_bind(boost_tasks[cpu], cpu);
1338 wake_up_process(boost_tasks[cpu]);
1339 mutex_unlock(&boost_mutex);
1340 return 0;
1341}
1342
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001343/*
1344 * Cause the rcutorture test to shutdown the system after the test has
1345 * run for the time specified by the shutdown_secs module parameter.
1346 */
1347static int
1348rcu_torture_shutdown(void *arg)
1349{
1350 long delta;
1351 unsigned long jiffies_snap;
1352
1353 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1354 jiffies_snap = ACCESS_ONCE(jiffies);
1355 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1356 !kthread_should_stop()) {
1357 delta = shutdown_time - jiffies_snap;
1358 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001359 pr_alert("%s" TORTURE_FLAG
1360 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1361 torture_type, delta);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001362 schedule_timeout_interruptible(delta);
1363 jiffies_snap = ACCESS_ONCE(jiffies);
1364 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001365 if (kthread_should_stop()) {
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001366 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1367 return 0;
1368 }
1369
1370 /* OK, shut down the system. */
1371
1372 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1373 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1374 rcu_torture_cleanup(); /* Get the success/failure message. */
1375 kernel_power_off(); /* Shut down the system. */
1376 return 0;
1377}
1378
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001379#ifdef CONFIG_HOTPLUG_CPU
1380
1381/*
1382 * Execute random CPU-hotplug operations at the interval specified
1383 * by the onoff_interval.
1384 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001385static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001386rcu_torture_onoff(void *arg)
1387{
1388 int cpu;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001389 unsigned long delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001390 int maxcpu = -1;
1391 DEFINE_RCU_RANDOM(rand);
Paul E. McKenney48983262012-09-24 16:08:31 -07001392 int ret;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001393 unsigned long starttime;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001394
1395 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1396 for_each_online_cpu(cpu)
1397 maxcpu = cpu;
1398 WARN_ON(maxcpu < 0);
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -08001399 if (onoff_holdoff > 0) {
1400 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1401 schedule_timeout_interruptible(onoff_holdoff * HZ);
1402 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1403 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001404 while (!kthread_should_stop()) {
1405 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001406 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001407 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001408 pr_alert("%s" TORTURE_FLAG
1409 "rcu_torture_onoff task: offlining %d\n",
1410 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001411 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001412 n_offline_attempts++;
Paul E. McKenney48983262012-09-24 16:08:31 -07001413 ret = cpu_down(cpu);
1414 if (ret) {
1415 if (verbose)
1416 pr_alert("%s" TORTURE_FLAG
1417 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1418 torture_type, cpu, ret);
1419 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001420 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001421 pr_alert("%s" TORTURE_FLAG
1422 "rcu_torture_onoff task: offlined %d\n",
1423 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001424 n_offline_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001425 delta = jiffies - starttime;
1426 sum_offline += delta;
1427 if (min_offline < 0) {
1428 min_offline = delta;
1429 max_offline = delta;
1430 }
1431 if (min_offline > delta)
1432 min_offline = delta;
1433 if (max_offline < delta)
1434 max_offline = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001435 }
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001436 } else if (cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001437 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001438 pr_alert("%s" TORTURE_FLAG
1439 "rcu_torture_onoff task: onlining %d\n",
1440 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001441 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001442 n_online_attempts++;
Paul E. McKenney7a6a4102013-06-21 06:24:56 -07001443 ret = cpu_up(cpu);
1444 if (ret) {
1445 if (verbose)
1446 pr_alert("%s" TORTURE_FLAG
1447 "rcu_torture_onoff task: online %d failed: errno %d\n",
1448 torture_type, cpu, ret);
1449 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001450 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001451 pr_alert("%s" TORTURE_FLAG
1452 "rcu_torture_onoff task: onlined %d\n",
1453 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001454 n_online_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001455 delta = jiffies - starttime;
1456 sum_online += delta;
1457 if (min_online < 0) {
1458 min_online = delta;
1459 max_online = delta;
1460 }
1461 if (min_online > delta)
1462 min_online = delta;
1463 if (max_online < delta)
1464 max_online = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001465 }
1466 }
1467 schedule_timeout_interruptible(onoff_interval * HZ);
1468 }
1469 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1470 return 0;
1471}
1472
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001473static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001474rcu_torture_onoff_init(void)
1475{
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001476 int ret;
1477
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001478 if (onoff_interval <= 0)
1479 return 0;
1480 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1481 if (IS_ERR(onoff_task)) {
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001482 ret = PTR_ERR(onoff_task);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001483 onoff_task = NULL;
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001484 return ret;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001485 }
1486 return 0;
1487}
1488
1489static void rcu_torture_onoff_cleanup(void)
1490{
1491 if (onoff_task == NULL)
1492 return;
1493 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1494 kthread_stop(onoff_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001495 onoff_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001496}
1497
1498#else /* #ifdef CONFIG_HOTPLUG_CPU */
1499
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001500static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001501rcu_torture_onoff_init(void)
1502{
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001503 return 0;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001504}
1505
1506static void rcu_torture_onoff_cleanup(void)
1507{
1508}
1509
1510#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1511
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001512/*
1513 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1514 * induces a CPU stall for the time specified by stall_cpu.
1515 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001516static int rcu_torture_stall(void *args)
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001517{
1518 unsigned long stop_at;
1519
1520 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1521 if (stall_cpu_holdoff > 0) {
1522 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1523 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1524 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1525 }
1526 if (!kthread_should_stop()) {
1527 stop_at = get_seconds() + stall_cpu;
1528 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001529 pr_alert("rcu_torture_stall start.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001530 rcu_read_lock();
1531 preempt_disable();
1532 while (ULONG_CMP_LT(get_seconds(), stop_at))
1533 continue; /* Induce RCU CPU stall warning. */
1534 preempt_enable();
1535 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001536 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001537 }
1538 rcutorture_shutdown_absorb("rcu_torture_stall");
1539 while (!kthread_should_stop())
1540 schedule_timeout_interruptible(10 * HZ);
1541 return 0;
1542}
1543
1544/* Spawn CPU-stall kthread, if stall_cpu specified. */
1545static int __init rcu_torture_stall_init(void)
1546{
1547 int ret;
1548
1549 if (stall_cpu <= 0)
1550 return 0;
1551 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1552 if (IS_ERR(stall_task)) {
1553 ret = PTR_ERR(stall_task);
1554 stall_task = NULL;
1555 return ret;
1556 }
1557 return 0;
1558}
1559
1560/* Clean up after the CPU-stall kthread, if one was spawned. */
1561static void rcu_torture_stall_cleanup(void)
1562{
1563 if (stall_task == NULL)
1564 return;
1565 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1566 kthread_stop(stall_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001567 stall_task = NULL;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001568}
1569
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001570/* Callback function for RCU barrier testing. */
1571void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1572{
1573 atomic_inc(&barrier_cbs_invoked);
1574}
1575
1576/* kthread function to register callbacks used to test RCU barriers. */
1577static int rcu_torture_barrier_cbs(void *arg)
1578{
1579 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001580 bool lastphase = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001581 struct rcu_head rcu;
1582
1583 init_rcu_head_on_stack(&rcu);
1584 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1585 set_user_nice(current, 19);
1586 do {
1587 wait_event(barrier_cbs_wq[myid],
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001588 barrier_phase != lastphase ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001589 kthread_should_stop() ||
1590 fullstop != FULLSTOP_DONTSTOP);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001591 lastphase = barrier_phase;
1592 smp_mb(); /* ensure barrier_phase load before ->call(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001593 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1594 break;
1595 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1596 if (atomic_dec_and_test(&barrier_cbs_count))
1597 wake_up(&barrier_wq);
1598 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1599 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1600 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1601 while (!kthread_should_stop())
1602 schedule_timeout_interruptible(1);
1603 cur_ops->cb_barrier();
1604 destroy_rcu_head_on_stack(&rcu);
1605 return 0;
1606}
1607
1608/* kthread function to drive and coordinate RCU barrier testing. */
1609static int rcu_torture_barrier(void *arg)
1610{
1611 int i;
1612
1613 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1614 do {
1615 atomic_set(&barrier_cbs_invoked, 0);
1616 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001617 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1618 barrier_phase = !barrier_phase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001619 for (i = 0; i < n_barrier_cbs; i++)
1620 wake_up(&barrier_cbs_wq[i]);
1621 wait_event(barrier_wq,
1622 atomic_read(&barrier_cbs_count) == 0 ||
1623 kthread_should_stop() ||
1624 fullstop != FULLSTOP_DONTSTOP);
1625 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1626 break;
1627 n_barrier_attempts++;
1628 cur_ops->cb_barrier();
1629 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1630 n_rcu_torture_barrier_error++;
1631 WARN_ON_ONCE(1);
1632 }
1633 n_barrier_successes++;
1634 schedule_timeout_interruptible(HZ / 10);
1635 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1636 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
Paul E. McKenney143aa672012-05-24 18:17:48 -07001637 rcutorture_shutdown_absorb("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001638 while (!kthread_should_stop())
1639 schedule_timeout_interruptible(1);
1640 return 0;
1641}
1642
1643/* Initialize RCU barrier testing. */
1644static int rcu_torture_barrier_init(void)
1645{
1646 int i;
1647 int ret;
1648
1649 if (n_barrier_cbs == 0)
1650 return 0;
1651 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001652 pr_alert("%s" TORTURE_FLAG
1653 " Call or barrier ops missing for %s,\n",
1654 torture_type, cur_ops->name);
1655 pr_alert("%s" TORTURE_FLAG
1656 " RCU barrier testing omitted from run.\n",
1657 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001658 return 0;
1659 }
1660 atomic_set(&barrier_cbs_count, 0);
1661 atomic_set(&barrier_cbs_invoked, 0);
1662 barrier_cbs_tasks =
1663 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1664 GFP_KERNEL);
1665 barrier_cbs_wq =
1666 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1667 GFP_KERNEL);
Sasha Levinde5e6432012-12-20 14:11:28 -05001668 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001669 return -ENOMEM;
1670 for (i = 0; i < n_barrier_cbs; i++) {
1671 init_waitqueue_head(&barrier_cbs_wq[i]);
1672 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
Lai Jiangshan9059c942012-03-19 16:12:14 +08001673 (void *)(long)i,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001674 "rcu_torture_barrier_cbs");
1675 if (IS_ERR(barrier_cbs_tasks[i])) {
1676 ret = PTR_ERR(barrier_cbs_tasks[i]);
1677 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1678 barrier_cbs_tasks[i] = NULL;
1679 return ret;
1680 }
1681 }
1682 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1683 "rcu_torture_barrier");
1684 if (IS_ERR(barrier_task)) {
1685 ret = PTR_ERR(barrier_task);
1686 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1687 barrier_task = NULL;
1688 }
1689 return 0;
1690}
1691
1692/* Clean up after RCU barrier testing. */
1693static void rcu_torture_barrier_cleanup(void)
1694{
1695 int i;
1696
1697 if (barrier_task != NULL) {
1698 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1699 kthread_stop(barrier_task);
1700 barrier_task = NULL;
1701 }
1702 if (barrier_cbs_tasks != NULL) {
1703 for (i = 0; i < n_barrier_cbs; i++) {
1704 if (barrier_cbs_tasks[i] != NULL) {
1705 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1706 kthread_stop(barrier_cbs_tasks[i]);
1707 barrier_cbs_tasks[i] = NULL;
1708 }
1709 }
1710 kfree(barrier_cbs_tasks);
1711 barrier_cbs_tasks = NULL;
1712 }
1713 if (barrier_cbs_wq != NULL) {
1714 kfree(barrier_cbs_wq);
1715 barrier_cbs_wq = NULL;
1716 }
1717}
1718
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001719static int rcutorture_cpu_notify(struct notifier_block *self,
1720 unsigned long action, void *hcpu)
1721{
1722 long cpu = (long)hcpu;
1723
1724 switch (action) {
1725 case CPU_ONLINE:
1726 case CPU_DOWN_FAILED:
1727 (void)rcutorture_booster_init(cpu);
1728 break;
1729 case CPU_DOWN_PREPARE:
1730 rcutorture_booster_cleanup(cpu);
1731 break;
1732 default:
1733 break;
1734 }
1735 return NOTIFY_OK;
1736}
1737
1738static struct notifier_block rcutorture_cpu_nb = {
1739 .notifier_call = rcutorture_cpu_notify,
1740};
1741
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001742static void
1743rcu_torture_cleanup(void)
1744{
1745 int i;
1746
Paul E. McKenney343e9092008-12-15 16:13:07 -08001747 mutex_lock(&fullstop_mutex);
Paul E. McKenney4a298652011-04-03 21:33:51 -07001748 rcutorture_record_test_transition();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001749 if (fullstop == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001750 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001751 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001752 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001753 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001754 if (cur_ops->cb_barrier != NULL)
1755 cur_ops->cb_barrier();
1756 return;
1757 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001758 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001759 mutex_unlock(&fullstop_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001760 unregister_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001761 rcu_torture_barrier_cleanup();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001762 rcu_torture_stall_cleanup();
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001763 if (stutter_task) {
1764 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1765 kthread_stop(stutter_task);
1766 }
1767 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001768 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001769 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1770 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001771 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001772 }
1773 shuffler_task = NULL;
1774
Josh Triplettc8e5b162007-05-08 00:33:20 -07001775 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001776 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1777 kthread_stop(writer_task);
1778 }
1779 writer_task = NULL;
1780
Josh Triplettc8e5b162007-05-08 00:33:20 -07001781 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001782 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001783 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001784 VERBOSE_PRINTK_STRING(
1785 "Stopping rcu_torture_reader task");
1786 kthread_stop(reader_tasks[i]);
1787 }
1788 reader_tasks[i] = NULL;
1789 }
1790 kfree(reader_tasks);
1791 reader_tasks = NULL;
1792 }
1793 rcu_torture_current = NULL;
1794
Josh Triplettc8e5b162007-05-08 00:33:20 -07001795 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001796 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001797 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001798 VERBOSE_PRINTK_STRING(
1799 "Stopping rcu_torture_fakewriter task");
1800 kthread_stop(fakewriter_tasks[i]);
1801 }
1802 fakewriter_tasks[i] = NULL;
1803 }
1804 kfree(fakewriter_tasks);
1805 fakewriter_tasks = NULL;
1806 }
1807
Josh Triplettc8e5b162007-05-08 00:33:20 -07001808 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001809 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1810 kthread_stop(stats_task);
1811 }
1812 stats_task = NULL;
1813
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001814 if (fqs_task) {
1815 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1816 kthread_stop(fqs_task);
1817 }
1818 fqs_task = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001819 if ((test_boost == 1 && cur_ops->can_boost) ||
1820 test_boost == 2) {
1821 unregister_cpu_notifier(&rcutorture_cpu_nb);
1822 for_each_possible_cpu(i)
1823 rcutorture_booster_cleanup(i);
1824 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001825 if (shutdown_task != NULL) {
1826 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1827 kthread_stop(shutdown_task);
1828 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001829 shutdown_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001830 rcu_torture_onoff_cleanup();
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001831
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001832 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001833
1834 if (cur_ops->cb_barrier != NULL)
1835 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001836
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001837 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001838
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001839 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001840 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney091541b2012-01-10 12:51:14 -08001841 else if (n_online_successes != n_online_attempts ||
1842 n_offline_successes != n_offline_attempts)
1843 rcu_torture_print_module_parms(cur_ops,
1844 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001845 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001846 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001847}
1848
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001849#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1850static void rcu_torture_leak_cb(struct rcu_head *rhp)
1851{
1852}
1853
1854static void rcu_torture_err_cb(struct rcu_head *rhp)
1855{
1856 /*
1857 * This -might- happen due to race conditions, but is unlikely.
1858 * The scenario that leads to this happening is that the
1859 * first of the pair of duplicate callbacks is queued,
1860 * someone else starts a grace period that includes that
1861 * callback, then the second of the pair must wait for the
1862 * next grace period. Unlikely, but can happen. If it
1863 * does happen, the debug-objects subsystem won't have splatted.
1864 */
1865 pr_alert("rcutorture: duplicated callback was invoked.\n");
1866}
1867#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1868
1869/*
1870 * Verify that double-free causes debug-objects to complain, but only
1871 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1872 * cannot be carried out.
1873 */
1874static void rcu_test_debug_objects(void)
1875{
1876#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1877 struct rcu_head rh1;
1878 struct rcu_head rh2;
1879
1880 init_rcu_head_on_stack(&rh1);
1881 init_rcu_head_on_stack(&rh2);
1882 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1883
1884 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1885 preempt_disable(); /* Prevent preemption from interrupting test. */
1886 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1887 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1888 local_irq_disable(); /* Make it harder to start a new grace period. */
1889 call_rcu(&rh2, rcu_torture_leak_cb);
1890 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1891 local_irq_enable();
1892 rcu_read_unlock();
1893 preempt_enable();
1894
1895 /* Wait for them all to get done so we can safely return. */
1896 rcu_barrier();
1897 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1898 destroy_rcu_head_on_stack(&rh1);
1899 destroy_rcu_head_on_stack(&rh2);
1900#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1901 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1902#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1903}
1904
Josh Triplett6f8bc502007-05-08 00:25:24 -07001905static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001906rcu_torture_init(void)
1907{
1908 int i;
1909 int cpu;
1910 int firsterr = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001911 int retval;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001912 static struct rcu_torture_ops *torture_ops[] = {
1913 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
1914 };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001915
Paul E. McKenney343e9092008-12-15 16:13:07 -08001916 mutex_lock(&fullstop_mutex);
1917
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001918 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001919 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001920 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001921 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001922 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001923 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001924 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001925 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1926 torture_type);
1927 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001928 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001929 pr_alert(" %s", torture_ops[i]->name);
1930 pr_alert("\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001931 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001932 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001933 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001934 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001935 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001936 fqs_duration = 0;
1937 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001938 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001939 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1940
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001941 if (nreaders >= 0)
1942 nrealreaders = nreaders;
1943 else
1944 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001945 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001946 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001947
1948 /* Set up the freelist. */
1949
1950 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001951 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001952 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001953 list_add_tail(&rcu_tortures[i].rtort_free,
1954 &rcu_torture_freelist);
1955 }
1956
1957 /* Initialize the statistics so that each run gets its own numbers. */
1958
1959 rcu_torture_current = NULL;
1960 rcu_torture_current_version = 0;
1961 atomic_set(&n_rcu_torture_alloc, 0);
1962 atomic_set(&n_rcu_torture_alloc_fail, 0);
1963 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001964 atomic_set(&n_rcu_torture_mberror, 0);
1965 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001966 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001967 n_rcu_torture_boost_ktrerror = 0;
1968 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001969 n_rcu_torture_boost_failure = 0;
1970 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001971 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1972 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001973 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001974 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1975 per_cpu(rcu_torture_count, cpu)[i] = 0;
1976 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1977 }
1978 }
1979
1980 /* Start up the kthreads. */
1981
1982 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
Paul E. McKenney60f53782012-08-25 15:27:40 -07001983 writer_task = kthread_create(rcu_torture_writer, NULL,
1984 "rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001985 if (IS_ERR(writer_task)) {
1986 firsterr = PTR_ERR(writer_task);
1987 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1988 writer_task = NULL;
1989 goto unwind;
1990 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07001991 wake_up_process(writer_task);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001992 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001993 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001994 if (fakewriter_tasks == NULL) {
1995 VERBOSE_PRINTK_ERRSTRING("out of memory");
1996 firsterr = -ENOMEM;
1997 goto unwind;
1998 }
1999 for (i = 0; i < nfakewriters; i++) {
2000 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2001 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002002 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07002003 if (IS_ERR(fakewriter_tasks[i])) {
2004 firsterr = PTR_ERR(fakewriter_tasks[i]);
2005 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2006 fakewriter_tasks[i] = NULL;
2007 goto unwind;
2008 }
2009 }
Josh Triplett2860aab2006-10-04 02:17:11 -07002010 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002011 GFP_KERNEL);
2012 if (reader_tasks == NULL) {
2013 VERBOSE_PRINTK_ERRSTRING("out of memory");
2014 firsterr = -ENOMEM;
2015 goto unwind;
2016 }
2017 for (i = 0; i < nrealreaders; i++) {
2018 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2019 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2020 "rcu_torture_reader");
2021 if (IS_ERR(reader_tasks[i])) {
2022 firsterr = PTR_ERR(reader_tasks[i]);
2023 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2024 reader_tasks[i] = NULL;
2025 goto unwind;
2026 }
2027 }
2028 if (stat_interval > 0) {
2029 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2030 stats_task = kthread_run(rcu_torture_stats, NULL,
2031 "rcu_torture_stats");
2032 if (IS_ERR(stats_task)) {
2033 firsterr = PTR_ERR(stats_task);
2034 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2035 stats_task = NULL;
2036 goto unwind;
2037 }
2038 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002039 if (test_no_idle_hz) {
2040 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002041
2042 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2043 firsterr = -ENOMEM;
2044 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2045 goto unwind;
2046 }
2047
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002048 /* Create the shuffler thread */
2049 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2050 "rcu_torture_shuffle");
2051 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002052 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002053 firsterr = PTR_ERR(shuffler_task);
2054 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2055 shuffler_task = NULL;
2056 goto unwind;
2057 }
2058 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07002059 if (stutter < 0)
2060 stutter = 0;
2061 if (stutter) {
2062 /* Create the stutter thread */
2063 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2064 "rcu_torture_stutter");
2065 if (IS_ERR(stutter_task)) {
2066 firsterr = PTR_ERR(stutter_task);
2067 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2068 stutter_task = NULL;
2069 goto unwind;
2070 }
2071 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002072 if (fqs_duration < 0)
2073 fqs_duration = 0;
2074 if (fqs_duration) {
2075 /* Create the stutter thread */
2076 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2077 "rcu_torture_fqs");
2078 if (IS_ERR(fqs_task)) {
2079 firsterr = PTR_ERR(fqs_task);
2080 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2081 fqs_task = NULL;
2082 goto unwind;
2083 }
2084 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002085 if (test_boost_interval < 1)
2086 test_boost_interval = 1;
2087 if (test_boost_duration < 2)
2088 test_boost_duration = 2;
2089 if ((test_boost == 1 && cur_ops->can_boost) ||
2090 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002091
2092 boost_starttime = jiffies + test_boost_interval * HZ;
2093 register_cpu_notifier(&rcutorture_cpu_nb);
2094 for_each_possible_cpu(i) {
2095 if (cpu_is_offline(i))
2096 continue; /* Heuristic: CPU can go offline. */
2097 retval = rcutorture_booster_init(i);
2098 if (retval < 0) {
2099 firsterr = retval;
2100 goto unwind;
2101 }
2102 }
2103 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002104 if (shutdown_secs > 0) {
2105 shutdown_time = jiffies + shutdown_secs * HZ;
Paul E. McKenney60f53782012-08-25 15:27:40 -07002106 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2107 "rcu_torture_shutdown");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002108 if (IS_ERR(shutdown_task)) {
2109 firsterr = PTR_ERR(shutdown_task);
2110 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2111 shutdown_task = NULL;
2112 goto unwind;
2113 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002114 wake_up_process(shutdown_task);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002115 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002116 i = rcu_torture_onoff_init();
2117 if (i != 0) {
2118 firsterr = i;
2119 goto unwind;
2120 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002121 register_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002122 i = rcu_torture_stall_init();
2123 if (i != 0) {
2124 firsterr = i;
2125 goto unwind;
2126 }
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002127 retval = rcu_torture_barrier_init();
2128 if (retval != 0) {
2129 firsterr = retval;
2130 goto unwind;
2131 }
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07002132 if (object_debug)
2133 rcu_test_debug_objects();
Paul E. McKenney4a298652011-04-03 21:33:51 -07002134 rcutorture_record_test_transition();
Paul E. McKenney343e9092008-12-15 16:13:07 -08002135 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002136 return 0;
2137
2138unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08002139 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002140 rcu_torture_cleanup();
2141 return firsterr;
2142}
2143
2144module_init(rcu_torture_init);
2145module_exit(rcu_torture_cleanup);