blob: c898f14a5b7d9dab8c690350c2b79efc1877d0d6 [file] [log] [blame]
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001/*
Paul E. McKenney29766f12006-06-27 02:54:02 -07002 * Read-Copy Update module-based torture test facility
Paul E. McKenneya241ec62005-10-30 15:03:12 -08003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Josh Triplettb772e1d2006-10-04 02:17:13 -070018 * Copyright (C) IBM Corporation, 2005, 2006
Paul E. McKenneya241ec62005-10-30 15:03:12 -080019 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070021 * Josh Triplett <josh@freedesktop.org>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080022 *
23 * See also: Documentation/RCU/torture.txt
24 */
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/kthread.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
32#include <linux/smp.h>
33#include <linux/rcupdate.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080037#include <linux/bitops.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080038#include <linux/completion.h>
39#include <linux/moduleparam.h>
40#include <linux/percpu.h>
41#include <linux/notifier.h>
Paul E. McKenney343e9092008-12-15 16:13:07 -080042#include <linux/reboot.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070043#include <linux/freezer.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080044#include <linux/cpu.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080045#include <linux/delay.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080046#include <linux/stat.h>
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070047#include <linux/srcu.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070048#include <linux/slab.h>
Paul E. McKenney52494532012-11-14 16:26:40 -080049#include <linux/trace_clock.h>
Harvey Harrisonf07767f2008-10-20 10:23:38 -070050#include <asm/byteorder.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080051
52MODULE_LICENSE("GPL");
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -070053MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080054
Josh Triplett48022112006-10-03 23:26:16 +020055static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
Josh Triplettb772e1d2006-10-04 02:17:13 -070056static int nfakewriters = 4; /* # fake writer threads */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070057static int stat_interval = 60; /* Interval between stats, in seconds. */
58 /* Zero means "only at end of test". */
Rusty Russelld8e8ed92011-12-15 13:43:24 +103059static bool verbose; /* Print more debug info. */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070060static bool test_no_idle_hz = true;
61 /* Test RCU support for tickless idle CPUs. */
Paul E. McKenneyd120f652008-06-18 05:21:44 -070062static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
63static int stutter = 5; /* Start/stop testing interval (in sec) */
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070064static int irqreader = 1; /* RCU readers from irq (timers). */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070065static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
66static int fqs_holdoff; /* Hold time within burst (us). */
Paul E. McKenneybf66f182010-01-04 15:09:10 -080067static int fqs_stutter = 3; /* Wait time between bursts (s). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -080068static int n_barrier_cbs; /* Number of callbacks to test RCU barriers. */
Paul E. McKenneyd2818df2013-04-23 17:05:42 -070069static int object_debug; /* Test object-debug double call_rcu()?. */
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -080070static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -080071static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070072static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -080073static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
74static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
Paul E. McKenney8e8be452010-09-02 16:16:14 -070075static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
76static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
77static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
Josh Triplett20d2e422006-10-04 02:17:15 -070078static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080079
Josh Triplettd6ad6712007-03-06 01:42:13 -080080module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080081MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080082module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070083MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Paul E. McKenney3721bc12011-07-21 16:00:17 -070084module_param(stat_interval, int, 0644);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080085MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080086module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080087MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080088module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080089MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080090module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080091MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070092module_param(stutter, int, 0444);
93MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070094module_param(irqreader, int, 0444);
95MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Paul E. McKenneybf66f182010-01-04 15:09:10 -080096module_param(fqs_duration, int, 0444);
97MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
98module_param(fqs_holdoff, int, 0444);
99MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
100module_param(fqs_stutter, int, 0444);
101MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800102module_param(n_barrier_cbs, int, 0444);
103MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
Paul E. McKenneyd2818df2013-04-23 17:05:42 -0700104module_param(object_debug, int, 0444);
105MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800106module_param(onoff_interval, int, 0444);
107MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -0800108module_param(onoff_holdoff, int, 0444);
109MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700110module_param(shutdown_secs, int, 0444);
111MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800112module_param(stall_cpu, int, 0444);
113MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
114module_param(stall_cpu_holdoff, int, 0444);
115MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
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.");
118module_param(test_boost_interval, int, 0444);
119MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
120module_param(test_boost_duration, int, 0444);
121MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
Josh Triplettd6ad6712007-03-06 01:42:13 -0800122module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700123MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700124
125#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800126#define PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700127 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800128#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700129 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800130#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700131 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800132
133static char printk_buf[4096];
134
135static int nrealreaders;
136static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700137static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800138static struct task_struct **reader_tasks;
139static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800140static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700141static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800142static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700143static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700144static struct task_struct *shutdown_task;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800145#ifdef CONFIG_HOTPLUG_CPU
146static struct task_struct *onoff_task;
147#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800148static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800149static struct task_struct **barrier_cbs_tasks;
150static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800151
152#define RCU_TORTURE_PIPE_LEN 10
153
154struct rcu_torture {
155 struct rcu_head rtort_rcu;
156 int rtort_pipe_count;
157 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800158 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800159};
160
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800161static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700162static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700163static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800164static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
165static DEFINE_SPINLOCK(rcu_torture_lock);
166static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
167 { 0 };
168static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
169 { 0 };
170static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700171static atomic_t n_rcu_torture_alloc;
172static atomic_t n_rcu_torture_alloc_fail;
173static atomic_t n_rcu_torture_free;
174static atomic_t n_rcu_torture_mberror;
175static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800176static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700177static long n_rcu_torture_boost_ktrerror;
178static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700179static long n_rcu_torture_boost_failure;
180static long n_rcu_torture_boosts;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700181static long n_rcu_torture_timers;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800182static long n_offline_attempts;
183static long n_offline_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700184static unsigned long sum_offline;
185static int min_offline = -1;
186static int max_offline;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800187static long n_online_attempts;
188static long n_online_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700189static unsigned long sum_online;
190static int min_online = -1;
191static int max_online;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800192static long n_barrier_attempts;
193static long n_barrier_successes;
Josh Triplette3033732006-10-04 02:17:14 -0700194static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600195static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800196
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700197static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700198
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700199#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
200#define RCUTORTURE_RUNNABLE_INIT 1
201#else
202#define RCUTORTURE_RUNNABLE_INIT 0
203#endif
204int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
Paul E. McKenneybb3bf702011-11-04 13:24:07 -0700205module_param(rcutorture_runnable, int, 0444);
206MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700207
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700208#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700209#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700210#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700211#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700212#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700213
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500214#ifdef CONFIG_RCU_TRACE
215static u64 notrace rcu_trace_clock_local(void)
216{
217 u64 ts = trace_clock_local();
218 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
219 return ts;
220}
221#else /* #ifdef CONFIG_RCU_TRACE */
222static u64 notrace rcu_trace_clock_local(void)
223{
224 return 0ULL;
225}
226#endif /* #else #ifdef CONFIG_RCU_TRACE */
227
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700228static unsigned long shutdown_time; /* jiffies to system shutdown. */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700229static unsigned long boost_starttime; /* jiffies of next boost test start. */
230DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
231 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800232static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700233static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800234static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
235static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
236static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700237
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800238/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
239
240#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
241#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
242#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
243static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700244/*
245 * Protect fullstop transitions and spawning of kthreads.
246 */
247static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800248
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700249/* Forward reference. */
250static void rcu_torture_cleanup(void);
251
Paul E. McKenney343e9092008-12-15 16:13:07 -0800252/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800253 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800254 */
255static int
256rcutorture_shutdown_notify(struct notifier_block *unused1,
257 unsigned long unused2, void *unused3)
258{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800259 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800260 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800261 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800262 else
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700263 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800264 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800265 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800266 return NOTIFY_DONE;
267}
268
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800269/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800270 * Absorb kthreads into a kernel function that won't return, so that
271 * they won't ever access module text or data again.
272 */
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400273static void rcutorture_shutdown_absorb(const char *title)
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800274{
275 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700276 pr_notice(
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800277 "rcutorture thread %s parking due to system shutdown\n",
278 title);
279 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
280 }
281}
282
283/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800284 * Allocate an element from the rcu_tortures pool.
285 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800286static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800287rcu_torture_alloc(void)
288{
289 struct list_head *p;
290
Ingo Molnaradac1662006-01-25 19:50:12 +0100291 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800292 if (list_empty(&rcu_torture_freelist)) {
293 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100294 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800295 return NULL;
296 }
297 atomic_inc(&n_rcu_torture_alloc);
298 p = rcu_torture_freelist.next;
299 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100300 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800301 return container_of(p, struct rcu_torture, rtort_free);
302}
303
304/*
305 * Free an element to the rcu_tortures pool.
306 */
307static void
308rcu_torture_free(struct rcu_torture *p)
309{
310 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100311 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800312 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100313 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800314}
315
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800316struct rcu_random_state {
317 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700318 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800319};
320
321#define RCU_RANDOM_MULT 39916801 /* prime */
322#define RCU_RANDOM_ADD 479001701 /* prime */
323#define RCU_RANDOM_REFRESH 10000
324
325#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
326
327/*
328 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700329 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800330 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700331static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800332rcu_random(struct rcu_random_state *rrsp)
333{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800334 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200335 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800336 rrsp->rrs_count = RCU_RANDOM_REFRESH;
337 }
338 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
339 return swahw32(rrsp->rrs_state);
340}
341
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700342static void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400343rcu_stutter_wait(const char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700344{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800345 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700346 if (rcutorture_runnable)
347 schedule_timeout_interruptible(1);
348 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700349 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800350 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800351 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700352}
353
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800354/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700355 * Operations vector for selecting different types of tests.
356 */
357
358struct rcu_torture_ops {
359 void (*init)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700360 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700361 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700362 void (*readunlock)(int idx);
363 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700364 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700365 void (*sync)(void);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800366 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
Paul E. McKenney23269742008-05-12 21:21:05 +0200367 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800368 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700369 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700370 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700371 int can_boost;
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400372 const char *name;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700373};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700374
375static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700376
377/*
378 * Definitions for rcu torture testing.
379 */
380
Josh Tripletta49a4af2006-09-29 01:59:30 -0700381static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700382{
383 rcu_read_lock();
384 return 0;
385}
386
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700387static void rcu_read_delay(struct rcu_random_state *rrsp)
388{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700389 const unsigned long shortdelay_us = 200;
390 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700391
Josh Triplettb8d57a72009-09-08 15:54:35 -0700392 /* We want a short delay sometimes to make a reader delay the grace
393 * period, and we want a long delay occasionally to trigger
394 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700395
Josh Triplettb8d57a72009-09-08 15:54:35 -0700396 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
397 mdelay(longdelay_ms);
398 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
399 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800400#ifdef CONFIG_PREEMPT
401 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
402 preempt_schedule(); /* No QS if preempt_disable() in effect */
403#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700404}
405
Josh Tripletta49a4af2006-09-29 01:59:30 -0700406static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700407{
408 rcu_read_unlock();
409}
410
411static int rcu_torture_completed(void)
412{
413 return rcu_batches_completed();
414}
415
416static void
417rcu_torture_cb(struct rcu_head *p)
418{
419 int i;
420 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
421
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800422 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700423 /* Test is ending, just drop callbacks on the floor. */
424 /* The next initialization will pick up the pieces. */
425 return;
426 }
427 i = rp->rtort_pipe_count;
428 if (i > RCU_TORTURE_PIPE_LEN)
429 i = RCU_TORTURE_PIPE_LEN;
430 atomic_inc(&rcu_torture_wcount[i]);
431 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
432 rp->rtort_mbtest = 0;
433 rcu_torture_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700434 } else {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700435 cur_ops->deferred_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700436 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700437}
438
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800439static int rcu_no_completed(void)
440{
441 return 0;
442}
443
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700444static void rcu_torture_deferred_free(struct rcu_torture *p)
445{
446 call_rcu(&p->rtort_rcu, rcu_torture_cb);
447}
448
449static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700450 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700451 .readlock = rcu_torture_read_lock,
452 .read_delay = rcu_read_delay,
453 .readunlock = rcu_torture_read_unlock,
454 .completed = rcu_torture_completed,
455 .deferred_free = rcu_torture_deferred_free,
456 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800457 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700458 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800459 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700460 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700461 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700462 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700463 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700464};
465
Josh Triplette3033732006-10-04 02:17:14 -0700466static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
467{
468 int i;
469 struct rcu_torture *rp;
470 struct rcu_torture *rp1;
471
472 cur_ops->sync();
473 list_add(&p->rtort_free, &rcu_torture_removed);
474 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
475 i = rp->rtort_pipe_count;
476 if (i > RCU_TORTURE_PIPE_LEN)
477 i = RCU_TORTURE_PIPE_LEN;
478 atomic_inc(&rcu_torture_wcount[i]);
479 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
480 rp->rtort_mbtest = 0;
481 list_del(&rp->rtort_free);
482 rcu_torture_free(rp);
483 }
484 }
485}
486
487static void rcu_sync_torture_init(void)
488{
489 INIT_LIST_HEAD(&rcu_torture_removed);
490}
491
Josh Triplett20d2e422006-10-04 02:17:15 -0700492static struct rcu_torture_ops rcu_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700493 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700494 .readlock = rcu_torture_read_lock,
495 .read_delay = rcu_read_delay,
496 .readunlock = rcu_torture_read_unlock,
497 .completed = rcu_torture_completed,
498 .deferred_free = rcu_sync_torture_deferred_free,
499 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800500 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700501 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800502 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700503 .stats = NULL,
504 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700505 .can_boost = rcu_can_boost(),
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700506 .name = "rcu_sync"
Josh Triplett20d2e422006-10-04 02:17:15 -0700507};
508
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800509static struct rcu_torture_ops rcu_expedited_ops = {
510 .init = rcu_sync_torture_init,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800511 .readlock = rcu_torture_read_lock,
512 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
513 .readunlock = rcu_torture_read_unlock,
514 .completed = rcu_no_completed,
515 .deferred_free = rcu_sync_torture_deferred_free,
516 .sync = synchronize_rcu_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800517 .call = NULL,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800518 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800519 .fqs = rcu_force_quiescent_state,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800520 .stats = NULL,
521 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700522 .can_boost = rcu_can_boost(),
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800523 .name = "rcu_expedited"
524};
525
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700526/*
527 * Definitions for rcu_bh torture testing.
528 */
529
Josh Tripletta49a4af2006-09-29 01:59:30 -0700530static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700531{
532 rcu_read_lock_bh();
533 return 0;
534}
535
Josh Tripletta49a4af2006-09-29 01:59:30 -0700536static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700537{
538 rcu_read_unlock_bh();
539}
540
541static int rcu_bh_torture_completed(void)
542{
543 return rcu_batches_completed_bh();
544}
545
546static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
547{
548 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
549}
550
551static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700552 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700553 .readlock = rcu_bh_torture_read_lock,
554 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
555 .readunlock = rcu_bh_torture_read_unlock,
556 .completed = rcu_bh_torture_completed,
557 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700558 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800559 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700560 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800561 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700562 .stats = NULL,
563 .irq_capable = 1,
564 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700565};
566
Josh Triplett11a14702006-10-04 02:17:16 -0700567static struct rcu_torture_ops rcu_bh_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700568 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700569 .readlock = rcu_bh_torture_read_lock,
570 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
571 .readunlock = rcu_bh_torture_read_unlock,
572 .completed = rcu_bh_torture_completed,
573 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700574 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800575 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700576 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800577 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700578 .stats = NULL,
579 .irq_capable = 1,
580 .name = "rcu_bh_sync"
Josh Triplett11a14702006-10-04 02:17:16 -0700581};
582
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700583static struct rcu_torture_ops rcu_bh_expedited_ops = {
584 .init = rcu_sync_torture_init,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700585 .readlock = rcu_bh_torture_read_lock,
586 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
587 .readunlock = rcu_bh_torture_read_unlock,
588 .completed = rcu_bh_torture_completed,
589 .deferred_free = rcu_sync_torture_deferred_free,
590 .sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800591 .call = NULL,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700592 .cb_barrier = NULL,
593 .fqs = rcu_bh_force_quiescent_state,
594 .stats = NULL,
595 .irq_capable = 1,
596 .name = "rcu_bh_expedited"
597};
598
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700599/*
600 * Definitions for srcu torture testing.
601 */
602
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800603DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700604
Josh Triplett012d3ca2006-12-06 20:40:19 -0800605static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700606{
607 return srcu_read_lock(&srcu_ctl);
608}
609
610static void srcu_read_delay(struct rcu_random_state *rrsp)
611{
612 long delay;
613 const long uspertick = 1000000 / HZ;
614 const long longdelay = 10;
615
616 /* We want there to be long-running readers, but not all the time. */
617
618 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
619 if (!delay)
620 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800621 else
622 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700623}
624
Josh Triplett012d3ca2006-12-06 20:40:19 -0800625static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700626{
627 srcu_read_unlock(&srcu_ctl, idx);
628}
629
630static int srcu_torture_completed(void)
631{
632 return srcu_batches_completed(&srcu_ctl);
633}
634
Lai Jiangshan9059c942012-03-19 16:12:14 +0800635static void srcu_torture_deferred_free(struct rcu_torture *rp)
636{
637 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
638}
639
Josh Triplettb772e1d2006-10-04 02:17:13 -0700640static void srcu_torture_synchronize(void)
641{
642 synchronize_srcu(&srcu_ctl);
643}
644
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700645static void srcu_torture_call(struct rcu_head *head,
646 void (*func)(struct rcu_head *head))
647{
648 call_srcu(&srcu_ctl, head, func);
649}
650
651static void srcu_torture_barrier(void)
652{
653 srcu_barrier(&srcu_ctl);
654}
655
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700656static int srcu_torture_stats(char *page)
657{
658 int cnt = 0;
659 int cpu;
660 int idx = srcu_ctl.completed & 0x1;
661
662 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
663 torture_type, TORTURE_FLAG, idx);
664 for_each_possible_cpu(cpu) {
Paul E. McKenneycef50122012-02-05 07:42:44 -0800665 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700666 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
667 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
668 }
669 cnt += sprintf(&page[cnt], "\n");
670 return cnt;
671}
672
673static struct rcu_torture_ops srcu_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800674 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700675 .readlock = srcu_torture_read_lock,
676 .read_delay = srcu_read_delay,
677 .readunlock = srcu_torture_read_unlock,
678 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800679 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700680 .sync = srcu_torture_synchronize,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700681 .call = srcu_torture_call,
682 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700683 .stats = srcu_torture_stats,
684 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700685};
686
Lai Jiangshan9059c942012-03-19 16:12:14 +0800687static struct rcu_torture_ops srcu_sync_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800688 .init = rcu_sync_torture_init,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800689 .readlock = srcu_torture_read_lock,
690 .read_delay = srcu_read_delay,
691 .readunlock = srcu_torture_read_unlock,
692 .completed = srcu_torture_completed,
693 .deferred_free = rcu_sync_torture_deferred_free,
694 .sync = srcu_torture_synchronize,
695 .call = NULL,
696 .cb_barrier = NULL,
697 .stats = srcu_torture_stats,
698 .name = "srcu_sync"
699};
700
Paul E. McKenney804bb832009-10-25 19:03:52 -0700701static void srcu_torture_synchronize_expedited(void)
702{
703 synchronize_srcu_expedited(&srcu_ctl);
704}
705
706static struct rcu_torture_ops srcu_expedited_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800707 .init = rcu_sync_torture_init,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700708 .readlock = srcu_torture_read_lock,
709 .read_delay = srcu_read_delay,
710 .readunlock = srcu_torture_read_unlock,
711 .completed = srcu_torture_completed,
712 .deferred_free = rcu_sync_torture_deferred_free,
713 .sync = srcu_torture_synchronize_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800714 .call = NULL,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700715 .cb_barrier = NULL,
716 .stats = srcu_torture_stats,
717 .name = "srcu_expedited"
718};
719
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700720/*
721 * Definitions for sched torture testing.
722 */
723
724static int sched_torture_read_lock(void)
725{
726 preempt_disable();
727 return 0;
728}
729
730static void sched_torture_read_unlock(int idx)
731{
732 preempt_enable();
733}
734
Paul E. McKenney23269742008-05-12 21:21:05 +0200735static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
736{
737 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
738}
739
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700740static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700741 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700742 .readlock = sched_torture_read_lock,
743 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
744 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800745 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700746 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700747 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700748 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800749 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700750 .stats = NULL,
751 .irq_capable = 1,
752 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700753};
754
Paul E. McKenney804bb832009-10-25 19:03:52 -0700755static struct rcu_torture_ops sched_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700756 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700757 .readlock = sched_torture_read_lock,
758 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
759 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800760 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700761 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700762 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700763 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800764 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700765 .stats = NULL,
766 .name = "sched_sync"
767};
768
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700769static struct rcu_torture_ops sched_expedited_ops = {
770 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700771 .readlock = sched_torture_read_lock,
772 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
773 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800774 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700775 .deferred_free = rcu_sync_torture_deferred_free,
776 .sync = synchronize_sched_expedited,
777 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800778 .fqs = rcu_sched_force_quiescent_state,
Tejun Heo969c7922010-05-06 18:49:21 +0200779 .stats = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700780 .irq_capable = 1,
781 .name = "sched_expedited"
Paul E. McKenney23269742008-05-12 21:21:05 +0200782};
783
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700784/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700785 * RCU torture priority-boost testing. Runs one real-time thread per
786 * CPU for moderate bursts, repeatedly registering RCU callbacks and
787 * spinning waiting for them to be invoked. If a given callback takes
788 * too long to be invoked, we assume that priority inversion has occurred.
789 */
790
791struct rcu_boost_inflight {
792 struct rcu_head rcu;
793 int inflight;
794};
795
796static void rcu_torture_boost_cb(struct rcu_head *head)
797{
798 struct rcu_boost_inflight *rbip =
799 container_of(head, struct rcu_boost_inflight, rcu);
800
801 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
802 rbip->inflight = 0;
803}
804
805static int rcu_torture_boost(void *arg)
806{
807 unsigned long call_rcu_time;
808 unsigned long endtime;
809 unsigned long oldstarttime;
810 struct rcu_boost_inflight rbi = { .inflight = 0 };
811 struct sched_param sp;
812
813 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
814
815 /* Set real-time priority. */
816 sp.sched_priority = 1;
817 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
818 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
819 n_rcu_torture_boost_rterror++;
820 }
821
Paul E. McKenney561190e2011-03-30 09:10:44 -0700822 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700823 /* Each pass through the following loop does one boost-test cycle. */
824 do {
825 /* Wait for the next test interval. */
826 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700827 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -0800828 schedule_timeout_interruptible(oldstarttime - jiffies);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700829 rcu_stutter_wait("rcu_torture_boost");
830 if (kthread_should_stop() ||
831 fullstop != FULLSTOP_DONTSTOP)
832 goto checkwait;
833 }
834
835 /* Do one boost-test interval. */
836 endtime = oldstarttime + test_boost_duration * HZ;
837 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700838 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700839 /* If we don't have a callback in flight, post one. */
840 if (!rbi.inflight) {
841 smp_mb(); /* RCU core before ->inflight = 1. */
842 rbi.inflight = 1;
843 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
844 if (jiffies - call_rcu_time >
845 test_boost_duration * HZ - HZ / 2) {
846 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
847 n_rcu_torture_boost_failure++;
848 }
849 call_rcu_time = jiffies;
850 }
851 cond_resched();
852 rcu_stutter_wait("rcu_torture_boost");
853 if (kthread_should_stop() ||
854 fullstop != FULLSTOP_DONTSTOP)
855 goto checkwait;
856 }
857
858 /*
859 * Set the start time of the next test interval.
860 * Yes, this is vulnerable to long delays, but such
861 * delays simply cause a false negative for the next
862 * interval. Besides, we are running at RT priority,
863 * so delays should be relatively rare.
864 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700865 while (oldstarttime == boost_starttime &&
866 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700867 if (mutex_trylock(&boost_mutex)) {
868 boost_starttime = jiffies +
869 test_boost_interval * HZ;
870 n_rcu_torture_boosts++;
871 mutex_unlock(&boost_mutex);
872 break;
873 }
874 schedule_timeout_uninterruptible(1);
875 }
876
877 /* Go do the stutter. */
878checkwait: rcu_stutter_wait("rcu_torture_boost");
879 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
880
881 /* Clean up and exit. */
882 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
883 rcutorture_shutdown_absorb("rcu_torture_boost");
884 while (!kthread_should_stop() || rbi.inflight)
885 schedule_timeout_uninterruptible(1);
886 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
Paul E. McKenney9d681972011-06-21 01:48:03 -0700887 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700888 return 0;
889}
890
891/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800892 * RCU torture force-quiescent-state kthread. Repeatedly induces
893 * bursts of calls to force_quiescent_state(), increasing the probability
894 * of occurrence of some important types of race conditions.
895 */
896static int
897rcu_torture_fqs(void *arg)
898{
899 unsigned long fqs_resume_time;
900 int fqs_burst_remaining;
901
902 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
903 do {
904 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700905 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
906 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800907 schedule_timeout_interruptible(1);
908 }
909 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700910 while (fqs_burst_remaining > 0 &&
911 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800912 cur_ops->fqs();
913 udelay(fqs_holdoff);
914 fqs_burst_remaining -= fqs_holdoff;
915 }
916 rcu_stutter_wait("rcu_torture_fqs");
917 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
918 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
919 rcutorture_shutdown_absorb("rcu_torture_fqs");
920 while (!kthread_should_stop())
921 schedule_timeout_uninterruptible(1);
922 return 0;
923}
924
925/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800926 * RCU torture writer kthread. Repeatedly substitutes a new structure
927 * for that pointed to by rcu_torture_current, freeing the old structure
928 * after a series of grace periods (the "pipeline").
929 */
930static int
931rcu_torture_writer(void *arg)
932{
933 int i;
934 long oldbatch = rcu_batches_completed();
935 struct rcu_torture *rp;
936 struct rcu_torture *old_rp;
937 static DEFINE_RCU_RANDOM(rand);
938
939 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800940 set_user_nice(current, 19);
941
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800942 do {
943 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700944 rp = rcu_torture_alloc();
945 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800946 continue;
947 rp->rtort_pipe_count = 0;
948 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700949 old_rp = rcu_dereference_check(rcu_torture_current,
950 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800951 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800952 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700953 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700954 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800955 i = old_rp->rtort_pipe_count;
956 if (i > RCU_TORTURE_PIPE_LEN)
957 i = RCU_TORTURE_PIPE_LEN;
958 atomic_inc(&rcu_torture_wcount[i]);
959 old_rp->rtort_pipe_count++;
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700960 cur_ops->deferred_free(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800961 }
Paul E. McKenney4a298652011-04-03 21:33:51 -0700962 rcutorture_record_progress(++rcu_torture_current_version);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700963 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800964 rcu_stutter_wait("rcu_torture_writer");
965 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800966 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800967 rcutorture_shutdown_absorb("rcu_torture_writer");
968 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800969 schedule_timeout_uninterruptible(1);
970 return 0;
971}
972
973/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700974 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
975 * delay between calls.
976 */
977static int
978rcu_torture_fakewriter(void *arg)
979{
980 DEFINE_RCU_RANDOM(rand);
981
982 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
983 set_user_nice(current, 19);
984
985 do {
986 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
987 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -0700988 if (cur_ops->cb_barrier != NULL &&
989 rcu_random(&rand) % (nfakewriters * 8) == 0)
990 cur_ops->cb_barrier();
991 else
992 cur_ops->sync();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800993 rcu_stutter_wait("rcu_torture_fakewriter");
994 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700995
996 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800997 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
998 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -0700999 schedule_timeout_uninterruptible(1);
1000 return 0;
1001}
1002
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001003void rcutorture_trace_dump(void)
1004{
1005 static atomic_t beenhere = ATOMIC_INIT(0);
1006
1007 if (atomic_read(&beenhere))
1008 return;
1009 if (atomic_xchg(&beenhere, 1) != 0)
1010 return;
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001011 ftrace_dump(DUMP_ALL);
1012}
1013
Josh Triplettb772e1d2006-10-04 02:17:13 -07001014/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001015 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1016 * incrementing the corresponding element of the pipeline array. The
1017 * counter in the element should never be greater than 1, otherwise, the
1018 * RCU implementation is broken.
1019 */
1020static void rcu_torture_timer(unsigned long unused)
1021{
1022 int idx;
1023 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -08001024 int completed_end;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001025 static DEFINE_RCU_RANDOM(rand);
1026 static DEFINE_SPINLOCK(rand_lock);
1027 struct rcu_torture *p;
1028 int pipe_count;
Paul E. McKenney52494532012-11-14 16:26:40 -08001029 unsigned long long ts;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001030
1031 idx = cur_ops->readlock();
1032 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -05001033 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001034 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001035 rcu_read_lock_bh_held() ||
1036 rcu_read_lock_sched_held() ||
1037 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001038 if (p == NULL) {
1039 /* Leave because rcu_torture_writer is not yet underway */
1040 cur_ops->readunlock(idx);
1041 return;
1042 }
1043 if (p->rtort_mbtest == 0)
1044 atomic_inc(&n_rcu_torture_mberror);
1045 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001046 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001047 n_rcu_torture_timers++;
1048 spin_unlock(&rand_lock);
1049 preempt_disable();
1050 pipe_count = p->rtort_pipe_count;
1051 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1052 /* Should not happen, but... */
1053 pipe_count = RCU_TORTURE_PIPE_LEN;
1054 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001055 completed_end = cur_ops->completed();
1056 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -08001057 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
1058 completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001059 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001060 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001061 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001062 completed = completed_end - completed;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001063 if (completed > RCU_TORTURE_PIPE_LEN) {
1064 /* Should not happen, but... */
1065 completed = RCU_TORTURE_PIPE_LEN;
1066 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001067 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001068 preempt_enable();
1069 cur_ops->readunlock(idx);
1070}
1071
1072/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001073 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1074 * incrementing the corresponding element of the pipeline array. The
1075 * counter in the element should never be greater than 1, otherwise, the
1076 * RCU implementation is broken.
1077 */
1078static int
1079rcu_torture_reader(void *arg)
1080{
1081 int completed;
Paul E. McKenney52494532012-11-14 16:26:40 -08001082 int completed_end;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001083 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001084 DEFINE_RCU_RANDOM(rand);
1085 struct rcu_torture *p;
1086 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001087 struct timer_list t;
Paul E. McKenney52494532012-11-14 16:26:40 -08001088 unsigned long long ts;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001089
1090 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001091 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001092 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001093 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001094
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001095 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001096 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001097 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001098 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001099 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001100 idx = cur_ops->readlock();
1101 completed = cur_ops->completed();
Steven Rostedte4aa0da2013-02-04 13:36:13 -05001102 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001103 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001104 rcu_read_lock_bh_held() ||
1105 rcu_read_lock_sched_held() ||
1106 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001107 if (p == NULL) {
1108 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001109 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001110 schedule_timeout_interruptible(HZ);
1111 continue;
1112 }
Paul E. McKenney996417d2005-11-18 01:10:50 -08001113 if (p->rtort_mbtest == 0)
1114 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001115 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001116 preempt_disable();
1117 pipe_count = p->rtort_pipe_count;
1118 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1119 /* Should not happen, but... */
1120 pipe_count = RCU_TORTURE_PIPE_LEN;
1121 }
Paul E. McKenney52494532012-11-14 16:26:40 -08001122 completed_end = cur_ops->completed();
1123 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -08001124 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1125 ts, completed, completed_end);
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001126 rcutorture_trace_dump();
Paul E. McKenney52494532012-11-14 16:26:40 -08001127 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001128 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney52494532012-11-14 16:26:40 -08001129 completed = completed_end - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001130 if (completed > RCU_TORTURE_PIPE_LEN) {
1131 /* Should not happen, but... */
1132 completed = RCU_TORTURE_PIPE_LEN;
1133 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001134 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001135 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001136 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001137 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001138 rcu_stutter_wait("rcu_torture_reader");
1139 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001140 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001141 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001142 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001143 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001144 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001145 schedule_timeout_uninterruptible(1);
1146 return 0;
1147}
1148
1149/*
1150 * Create an RCU-torture statistics message in the specified buffer.
1151 */
1152static int
1153rcu_torture_printk(char *page)
1154{
1155 int cnt = 0;
1156 int cpu;
1157 int i;
1158 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1159 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1160
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001161 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001162 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1163 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1164 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1165 }
1166 }
1167 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1168 if (pipesummary[i] != 0)
1169 break;
1170 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001171 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001172 cnt += sprintf(&page[cnt],
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001173 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001174 rcu_torture_current,
1175 rcu_torture_current_version,
1176 list_empty(&rcu_torture_freelist),
1177 atomic_read(&n_rcu_torture_alloc),
1178 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001179 atomic_read(&n_rcu_torture_free));
1180 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001181 atomic_read(&n_rcu_torture_mberror),
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001182 n_rcu_torture_boost_ktrerror,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001183 n_rcu_torture_boost_rterror);
1184 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001185 n_rcu_torture_boost_failure,
1186 n_rcu_torture_boosts,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001187 n_rcu_torture_timers);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001188 cnt += sprintf(&page[cnt],
1189 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1190 n_online_successes, n_online_attempts,
1191 n_offline_successes, n_offline_attempts,
1192 min_online, max_online,
1193 min_offline, max_offline,
1194 sum_online, sum_offline, HZ);
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001195 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001196 n_barrier_successes,
1197 n_barrier_attempts,
1198 n_rcu_torture_barrier_error);
1199 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001200 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001201 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001202 n_rcu_torture_boost_ktrerror != 0 ||
1203 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001204 n_rcu_torture_boost_failure != 0 ||
1205 i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001206 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001207 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001208 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001209 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001210 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1211 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1212 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001213 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001214 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001215 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001216 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001217 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001218 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1219 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1220 cnt += sprintf(&page[cnt], " %d",
1221 atomic_read(&rcu_torture_wcount[i]));
1222 }
1223 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -07001224 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001225 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001226 return cnt;
1227}
1228
1229/*
1230 * Print torture statistics. Caller must ensure that there is only
1231 * one call to this function at a given time!!! This is normally
1232 * accomplished by relying on the module system to only have one copy
1233 * of the module loaded, and then by giving the rcu_torture_stats
1234 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1235 * thread is not running).
1236 */
1237static void
1238rcu_torture_stats_print(void)
1239{
1240 int cnt;
1241
1242 cnt = rcu_torture_printk(printk_buf);
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001243 pr_alert("%s", printk_buf);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001244}
1245
1246/*
1247 * Periodically prints torture statistics, if periodic statistics printing
1248 * was specified via the stat_interval module parameter.
1249 *
1250 * No need to worry about fullstop here, since this one doesn't reference
1251 * volatile state or register callbacks.
1252 */
1253static int
1254rcu_torture_stats(void *arg)
1255{
1256 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1257 do {
1258 schedule_timeout_interruptible(stat_interval * HZ);
1259 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001260 rcutorture_shutdown_absorb("rcu_torture_stats");
1261 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001262 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1263 return 0;
1264}
1265
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001266static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1267
1268/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1269 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1270 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001271static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001272{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001273 int i;
1274
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001275 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001276 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001277
1278 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001279 if (num_online_cpus() == 1) {
1280 put_online_cpus();
1281 return;
1282 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001283
1284 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001285 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001286
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001287 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001288
Josh Triplettc8e5b162007-05-08 00:33:20 -07001289 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001290 for (i = 0; i < nrealreaders; i++)
1291 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001292 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001293 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001294 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001295 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001296 for (i = 0; i < nfakewriters; i++)
1297 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001298 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001299 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001300 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001301 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001302 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001303 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001304 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -08001305 if (stutter_task)
1306 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1307 if (fqs_task)
1308 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1309 if (shutdown_task)
1310 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1311#ifdef CONFIG_HOTPLUG_CPU
1312 if (onoff_task)
1313 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1314#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1315 if (stall_task)
1316 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1317 if (barrier_cbs_tasks)
1318 for (i = 0; i < n_barrier_cbs; i++)
1319 if (barrier_cbs_tasks[i])
1320 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1321 shuffle_tmp_mask);
1322 if (barrier_task)
1323 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001324
1325 if (rcu_idle_cpu == -1)
1326 rcu_idle_cpu = num_online_cpus() - 1;
1327 else
1328 rcu_idle_cpu--;
1329
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001330 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001331}
1332
1333/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1334 * system to become idle at a time and cut off its timer ticks. This is meant
1335 * to test the support for such tickless idle CPU in RCU.
1336 */
1337static int
1338rcu_torture_shuffle(void *arg)
1339{
1340 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1341 do {
1342 schedule_timeout_interruptible(shuffle_interval * HZ);
1343 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001344 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1345 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001346 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1347 return 0;
1348}
1349
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001350/* Cause the rcutorture test to "stutter", starting and stopping all
1351 * threads periodically.
1352 */
1353static int
1354rcu_torture_stutter(void *arg)
1355{
1356 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1357 do {
1358 schedule_timeout_interruptible(stutter * HZ);
1359 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001360 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001361 schedule_timeout_interruptible(stutter * HZ);
1362 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001363 rcutorture_shutdown_absorb("rcu_torture_stutter");
1364 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001365 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1366 return 0;
1367}
1368
Paul E. McKenney95c38322006-03-24 03:15:58 -08001369static inline void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04001370rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001371{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001372 pr_alert("%s" TORTURE_FLAG
1373 "--- %s: nreaders=%d nfakewriters=%d "
1374 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1375 "shuffle_interval=%d stutter=%d irqreader=%d "
1376 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1377 "test_boost=%d/%d test_boost_interval=%d "
1378 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001379 "stall_cpu=%d stall_cpu_holdoff=%d "
1380 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001381 "onoff_interval=%d onoff_holdoff=%d\n",
1382 torture_type, tag, nrealreaders, nfakewriters,
1383 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1384 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1385 test_boost, cur_ops->can_boost,
1386 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001387 stall_cpu, stall_cpu_holdoff,
1388 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001389 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001390}
1391
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001392static struct notifier_block rcutorture_shutdown_nb = {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001393 .notifier_call = rcutorture_shutdown_notify,
1394};
1395
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001396static void rcutorture_booster_cleanup(int cpu)
1397{
1398 struct task_struct *t;
1399
1400 if (boost_tasks[cpu] == NULL)
1401 return;
1402 mutex_lock(&boost_mutex);
1403 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1404 t = boost_tasks[cpu];
1405 boost_tasks[cpu] = NULL;
1406 mutex_unlock(&boost_mutex);
1407
1408 /* This must be outside of the mutex, otherwise deadlock! */
1409 kthread_stop(t);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001410 boost_tasks[cpu] = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001411}
1412
1413static int rcutorture_booster_init(int cpu)
1414{
1415 int retval;
1416
1417 if (boost_tasks[cpu] != NULL)
1418 return 0; /* Already created, nothing more to do. */
1419
1420 /* Don't allow time recalculation while creating a new task. */
1421 mutex_lock(&boost_mutex);
1422 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001423 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1424 cpu_to_node(cpu),
1425 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001426 if (IS_ERR(boost_tasks[cpu])) {
1427 retval = PTR_ERR(boost_tasks[cpu]);
1428 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1429 n_rcu_torture_boost_ktrerror++;
1430 boost_tasks[cpu] = NULL;
1431 mutex_unlock(&boost_mutex);
1432 return retval;
1433 }
1434 kthread_bind(boost_tasks[cpu], cpu);
1435 wake_up_process(boost_tasks[cpu]);
1436 mutex_unlock(&boost_mutex);
1437 return 0;
1438}
1439
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001440/*
1441 * Cause the rcutorture test to shutdown the system after the test has
1442 * run for the time specified by the shutdown_secs module parameter.
1443 */
1444static int
1445rcu_torture_shutdown(void *arg)
1446{
1447 long delta;
1448 unsigned long jiffies_snap;
1449
1450 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1451 jiffies_snap = ACCESS_ONCE(jiffies);
1452 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1453 !kthread_should_stop()) {
1454 delta = shutdown_time - jiffies_snap;
1455 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001456 pr_alert("%s" TORTURE_FLAG
1457 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1458 torture_type, delta);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001459 schedule_timeout_interruptible(delta);
1460 jiffies_snap = ACCESS_ONCE(jiffies);
1461 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001462 if (kthread_should_stop()) {
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001463 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1464 return 0;
1465 }
1466
1467 /* OK, shut down the system. */
1468
1469 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1470 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1471 rcu_torture_cleanup(); /* Get the success/failure message. */
1472 kernel_power_off(); /* Shut down the system. */
1473 return 0;
1474}
1475
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001476#ifdef CONFIG_HOTPLUG_CPU
1477
1478/*
1479 * Execute random CPU-hotplug operations at the interval specified
1480 * by the onoff_interval.
1481 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001482static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001483rcu_torture_onoff(void *arg)
1484{
1485 int cpu;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001486 unsigned long delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001487 int maxcpu = -1;
1488 DEFINE_RCU_RANDOM(rand);
Paul E. McKenney48983262012-09-24 16:08:31 -07001489 int ret;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001490 unsigned long starttime;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001491
1492 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1493 for_each_online_cpu(cpu)
1494 maxcpu = cpu;
1495 WARN_ON(maxcpu < 0);
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -08001496 if (onoff_holdoff > 0) {
1497 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1498 schedule_timeout_interruptible(onoff_holdoff * HZ);
1499 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1500 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001501 while (!kthread_should_stop()) {
1502 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001503 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001504 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001505 pr_alert("%s" TORTURE_FLAG
1506 "rcu_torture_onoff task: offlining %d\n",
1507 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001508 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001509 n_offline_attempts++;
Paul E. McKenney48983262012-09-24 16:08:31 -07001510 ret = cpu_down(cpu);
1511 if (ret) {
1512 if (verbose)
1513 pr_alert("%s" TORTURE_FLAG
1514 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1515 torture_type, cpu, ret);
1516 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001517 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001518 pr_alert("%s" TORTURE_FLAG
1519 "rcu_torture_onoff task: offlined %d\n",
1520 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001521 n_offline_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001522 delta = jiffies - starttime;
1523 sum_offline += delta;
1524 if (min_offline < 0) {
1525 min_offline = delta;
1526 max_offline = delta;
1527 }
1528 if (min_offline > delta)
1529 min_offline = delta;
1530 if (max_offline < delta)
1531 max_offline = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001532 }
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001533 } else if (cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001534 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001535 pr_alert("%s" TORTURE_FLAG
1536 "rcu_torture_onoff task: onlining %d\n",
1537 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001538 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001539 n_online_attempts++;
1540 if (cpu_up(cpu) == 0) {
1541 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001542 pr_alert("%s" TORTURE_FLAG
1543 "rcu_torture_onoff task: onlined %d\n",
1544 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001545 n_online_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001546 delta = jiffies - starttime;
1547 sum_online += delta;
1548 if (min_online < 0) {
1549 min_online = delta;
1550 max_online = delta;
1551 }
1552 if (min_online > delta)
1553 min_online = delta;
1554 if (max_online < delta)
1555 max_online = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001556 }
1557 }
1558 schedule_timeout_interruptible(onoff_interval * HZ);
1559 }
1560 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1561 return 0;
1562}
1563
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001564static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001565rcu_torture_onoff_init(void)
1566{
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001567 int ret;
1568
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001569 if (onoff_interval <= 0)
1570 return 0;
1571 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1572 if (IS_ERR(onoff_task)) {
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001573 ret = PTR_ERR(onoff_task);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001574 onoff_task = NULL;
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001575 return ret;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001576 }
1577 return 0;
1578}
1579
1580static void rcu_torture_onoff_cleanup(void)
1581{
1582 if (onoff_task == NULL)
1583 return;
1584 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1585 kthread_stop(onoff_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001586 onoff_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001587}
1588
1589#else /* #ifdef CONFIG_HOTPLUG_CPU */
1590
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001591static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001592rcu_torture_onoff_init(void)
1593{
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001594 return 0;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001595}
1596
1597static void rcu_torture_onoff_cleanup(void)
1598{
1599}
1600
1601#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1602
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001603/*
1604 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1605 * induces a CPU stall for the time specified by stall_cpu.
1606 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001607static int rcu_torture_stall(void *args)
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001608{
1609 unsigned long stop_at;
1610
1611 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1612 if (stall_cpu_holdoff > 0) {
1613 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1614 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1615 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1616 }
1617 if (!kthread_should_stop()) {
1618 stop_at = get_seconds() + stall_cpu;
1619 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001620 pr_alert("rcu_torture_stall start.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001621 rcu_read_lock();
1622 preempt_disable();
1623 while (ULONG_CMP_LT(get_seconds(), stop_at))
1624 continue; /* Induce RCU CPU stall warning. */
1625 preempt_enable();
1626 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001627 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001628 }
1629 rcutorture_shutdown_absorb("rcu_torture_stall");
1630 while (!kthread_should_stop())
1631 schedule_timeout_interruptible(10 * HZ);
1632 return 0;
1633}
1634
1635/* Spawn CPU-stall kthread, if stall_cpu specified. */
1636static int __init rcu_torture_stall_init(void)
1637{
1638 int ret;
1639
1640 if (stall_cpu <= 0)
1641 return 0;
1642 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1643 if (IS_ERR(stall_task)) {
1644 ret = PTR_ERR(stall_task);
1645 stall_task = NULL;
1646 return ret;
1647 }
1648 return 0;
1649}
1650
1651/* Clean up after the CPU-stall kthread, if one was spawned. */
1652static void rcu_torture_stall_cleanup(void)
1653{
1654 if (stall_task == NULL)
1655 return;
1656 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1657 kthread_stop(stall_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001658 stall_task = NULL;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001659}
1660
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001661/* Callback function for RCU barrier testing. */
1662void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1663{
1664 atomic_inc(&barrier_cbs_invoked);
1665}
1666
1667/* kthread function to register callbacks used to test RCU barriers. */
1668static int rcu_torture_barrier_cbs(void *arg)
1669{
1670 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001671 bool lastphase = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001672 struct rcu_head rcu;
1673
1674 init_rcu_head_on_stack(&rcu);
1675 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1676 set_user_nice(current, 19);
1677 do {
1678 wait_event(barrier_cbs_wq[myid],
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001679 barrier_phase != lastphase ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001680 kthread_should_stop() ||
1681 fullstop != FULLSTOP_DONTSTOP);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001682 lastphase = barrier_phase;
1683 smp_mb(); /* ensure barrier_phase load before ->call(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001684 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1685 break;
1686 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1687 if (atomic_dec_and_test(&barrier_cbs_count))
1688 wake_up(&barrier_wq);
1689 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1690 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1691 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1692 while (!kthread_should_stop())
1693 schedule_timeout_interruptible(1);
1694 cur_ops->cb_barrier();
1695 destroy_rcu_head_on_stack(&rcu);
1696 return 0;
1697}
1698
1699/* kthread function to drive and coordinate RCU barrier testing. */
1700static int rcu_torture_barrier(void *arg)
1701{
1702 int i;
1703
1704 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1705 do {
1706 atomic_set(&barrier_cbs_invoked, 0);
1707 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001708 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1709 barrier_phase = !barrier_phase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001710 for (i = 0; i < n_barrier_cbs; i++)
1711 wake_up(&barrier_cbs_wq[i]);
1712 wait_event(barrier_wq,
1713 atomic_read(&barrier_cbs_count) == 0 ||
1714 kthread_should_stop() ||
1715 fullstop != FULLSTOP_DONTSTOP);
1716 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1717 break;
1718 n_barrier_attempts++;
1719 cur_ops->cb_barrier();
1720 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1721 n_rcu_torture_barrier_error++;
1722 WARN_ON_ONCE(1);
1723 }
1724 n_barrier_successes++;
1725 schedule_timeout_interruptible(HZ / 10);
1726 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1727 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
Paul E. McKenney143aa672012-05-24 18:17:48 -07001728 rcutorture_shutdown_absorb("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001729 while (!kthread_should_stop())
1730 schedule_timeout_interruptible(1);
1731 return 0;
1732}
1733
1734/* Initialize RCU barrier testing. */
1735static int rcu_torture_barrier_init(void)
1736{
1737 int i;
1738 int ret;
1739
1740 if (n_barrier_cbs == 0)
1741 return 0;
1742 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001743 pr_alert("%s" TORTURE_FLAG
1744 " Call or barrier ops missing for %s,\n",
1745 torture_type, cur_ops->name);
1746 pr_alert("%s" TORTURE_FLAG
1747 " RCU barrier testing omitted from run.\n",
1748 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001749 return 0;
1750 }
1751 atomic_set(&barrier_cbs_count, 0);
1752 atomic_set(&barrier_cbs_invoked, 0);
1753 barrier_cbs_tasks =
1754 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1755 GFP_KERNEL);
1756 barrier_cbs_wq =
1757 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1758 GFP_KERNEL);
Sasha Levinde5e6432012-12-20 14:11:28 -05001759 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001760 return -ENOMEM;
1761 for (i = 0; i < n_barrier_cbs; i++) {
1762 init_waitqueue_head(&barrier_cbs_wq[i]);
1763 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
Lai Jiangshan9059c942012-03-19 16:12:14 +08001764 (void *)(long)i,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001765 "rcu_torture_barrier_cbs");
1766 if (IS_ERR(barrier_cbs_tasks[i])) {
1767 ret = PTR_ERR(barrier_cbs_tasks[i]);
1768 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1769 barrier_cbs_tasks[i] = NULL;
1770 return ret;
1771 }
1772 }
1773 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1774 "rcu_torture_barrier");
1775 if (IS_ERR(barrier_task)) {
1776 ret = PTR_ERR(barrier_task);
1777 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1778 barrier_task = NULL;
1779 }
1780 return 0;
1781}
1782
1783/* Clean up after RCU barrier testing. */
1784static void rcu_torture_barrier_cleanup(void)
1785{
1786 int i;
1787
1788 if (barrier_task != NULL) {
1789 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1790 kthread_stop(barrier_task);
1791 barrier_task = NULL;
1792 }
1793 if (barrier_cbs_tasks != NULL) {
1794 for (i = 0; i < n_barrier_cbs; i++) {
1795 if (barrier_cbs_tasks[i] != NULL) {
1796 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1797 kthread_stop(barrier_cbs_tasks[i]);
1798 barrier_cbs_tasks[i] = NULL;
1799 }
1800 }
1801 kfree(barrier_cbs_tasks);
1802 barrier_cbs_tasks = NULL;
1803 }
1804 if (barrier_cbs_wq != NULL) {
1805 kfree(barrier_cbs_wq);
1806 barrier_cbs_wq = NULL;
1807 }
1808}
1809
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001810static int rcutorture_cpu_notify(struct notifier_block *self,
1811 unsigned long action, void *hcpu)
1812{
1813 long cpu = (long)hcpu;
1814
1815 switch (action) {
1816 case CPU_ONLINE:
1817 case CPU_DOWN_FAILED:
1818 (void)rcutorture_booster_init(cpu);
1819 break;
1820 case CPU_DOWN_PREPARE:
1821 rcutorture_booster_cleanup(cpu);
1822 break;
1823 default:
1824 break;
1825 }
1826 return NOTIFY_OK;
1827}
1828
1829static struct notifier_block rcutorture_cpu_nb = {
1830 .notifier_call = rcutorture_cpu_notify,
1831};
1832
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001833static void
1834rcu_torture_cleanup(void)
1835{
1836 int i;
1837
Paul E. McKenney343e9092008-12-15 16:13:07 -08001838 mutex_lock(&fullstop_mutex);
Paul E. McKenney4a298652011-04-03 21:33:51 -07001839 rcutorture_record_test_transition();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001840 if (fullstop == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001841 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001842 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001843 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001844 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001845 if (cur_ops->cb_barrier != NULL)
1846 cur_ops->cb_barrier();
1847 return;
1848 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001849 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001850 mutex_unlock(&fullstop_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001851 unregister_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001852 rcu_torture_barrier_cleanup();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001853 rcu_torture_stall_cleanup();
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001854 if (stutter_task) {
1855 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1856 kthread_stop(stutter_task);
1857 }
1858 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001859 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001860 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1861 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001862 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001863 }
1864 shuffler_task = NULL;
1865
Josh Triplettc8e5b162007-05-08 00:33:20 -07001866 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001867 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1868 kthread_stop(writer_task);
1869 }
1870 writer_task = NULL;
1871
Josh Triplettc8e5b162007-05-08 00:33:20 -07001872 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001873 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001874 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001875 VERBOSE_PRINTK_STRING(
1876 "Stopping rcu_torture_reader task");
1877 kthread_stop(reader_tasks[i]);
1878 }
1879 reader_tasks[i] = NULL;
1880 }
1881 kfree(reader_tasks);
1882 reader_tasks = NULL;
1883 }
1884 rcu_torture_current = NULL;
1885
Josh Triplettc8e5b162007-05-08 00:33:20 -07001886 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001887 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001888 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001889 VERBOSE_PRINTK_STRING(
1890 "Stopping rcu_torture_fakewriter task");
1891 kthread_stop(fakewriter_tasks[i]);
1892 }
1893 fakewriter_tasks[i] = NULL;
1894 }
1895 kfree(fakewriter_tasks);
1896 fakewriter_tasks = NULL;
1897 }
1898
Josh Triplettc8e5b162007-05-08 00:33:20 -07001899 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001900 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1901 kthread_stop(stats_task);
1902 }
1903 stats_task = NULL;
1904
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001905 if (fqs_task) {
1906 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1907 kthread_stop(fqs_task);
1908 }
1909 fqs_task = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001910 if ((test_boost == 1 && cur_ops->can_boost) ||
1911 test_boost == 2) {
1912 unregister_cpu_notifier(&rcutorture_cpu_nb);
1913 for_each_possible_cpu(i)
1914 rcutorture_booster_cleanup(i);
1915 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001916 if (shutdown_task != NULL) {
1917 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1918 kthread_stop(shutdown_task);
1919 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001920 shutdown_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001921 rcu_torture_onoff_cleanup();
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001922
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001923 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001924
1925 if (cur_ops->cb_barrier != NULL)
1926 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001927
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001928 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001929
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001930 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001931 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney091541b2012-01-10 12:51:14 -08001932 else if (n_online_successes != n_online_attempts ||
1933 n_offline_successes != n_offline_attempts)
1934 rcu_torture_print_module_parms(cur_ops,
1935 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001936 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001937 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001938}
1939
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001940#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1941static void rcu_torture_leak_cb(struct rcu_head *rhp)
1942{
1943}
1944
1945static void rcu_torture_err_cb(struct rcu_head *rhp)
1946{
1947 /*
1948 * This -might- happen due to race conditions, but is unlikely.
1949 * The scenario that leads to this happening is that the
1950 * first of the pair of duplicate callbacks is queued,
1951 * someone else starts a grace period that includes that
1952 * callback, then the second of the pair must wait for the
1953 * next grace period. Unlikely, but can happen. If it
1954 * does happen, the debug-objects subsystem won't have splatted.
1955 */
1956 pr_alert("rcutorture: duplicated callback was invoked.\n");
1957}
1958#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1959
1960/*
1961 * Verify that double-free causes debug-objects to complain, but only
1962 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1963 * cannot be carried out.
1964 */
1965static void rcu_test_debug_objects(void)
1966{
1967#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1968 struct rcu_head rh1;
1969 struct rcu_head rh2;
1970
1971 init_rcu_head_on_stack(&rh1);
1972 init_rcu_head_on_stack(&rh2);
1973 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1974
1975 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1976 preempt_disable(); /* Prevent preemption from interrupting test. */
1977 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1978 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1979 local_irq_disable(); /* Make it harder to start a new grace period. */
1980 call_rcu(&rh2, rcu_torture_leak_cb);
1981 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1982 local_irq_enable();
1983 rcu_read_unlock();
1984 preempt_enable();
1985
1986 /* Wait for them all to get done so we can safely return. */
1987 rcu_barrier();
1988 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1989 destroy_rcu_head_on_stack(&rh1);
1990 destroy_rcu_head_on_stack(&rh2);
1991#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1992 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1993#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1994}
1995
Josh Triplett6f8bc502007-05-08 00:25:24 -07001996static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001997rcu_torture_init(void)
1998{
1999 int i;
2000 int cpu;
2001 int firsterr = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002002 int retval;
Josh Triplettade5fb82007-05-08 00:33:22 -07002003 static struct rcu_torture_ops *torture_ops[] =
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08002004 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -07002005 &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
Paul E. McKenney751a68b2012-05-07 13:44:44 -07002006 &srcu_ops, &srcu_sync_ops, &srcu_expedited_ops,
Paul E. McKenney804bb832009-10-25 19:03:52 -07002007 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002008
Paul E. McKenney343e9092008-12-15 16:13:07 -08002009 mutex_lock(&fullstop_mutex);
2010
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002011 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07002012 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07002013 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07002014 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07002015 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07002016 }
Josh Triplettade5fb82007-05-08 00:33:22 -07002017 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07002018 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2019 torture_type);
2020 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07002021 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07002022 pr_alert(" %s", torture_ops[i]->name);
2023 pr_alert("\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08002024 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002025 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07002026 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002027 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07002028 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002029 fqs_duration = 0;
2030 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07002031 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07002032 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
2033
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002034 if (nreaders >= 0)
2035 nrealreaders = nreaders;
2036 else
2037 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002038 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08002039 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002040
2041 /* Set up the freelist. */
2042
2043 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07002044 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08002045 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002046 list_add_tail(&rcu_tortures[i].rtort_free,
2047 &rcu_torture_freelist);
2048 }
2049
2050 /* Initialize the statistics so that each run gets its own numbers. */
2051
2052 rcu_torture_current = NULL;
2053 rcu_torture_current_version = 0;
2054 atomic_set(&n_rcu_torture_alloc, 0);
2055 atomic_set(&n_rcu_torture_alloc_fail, 0);
2056 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08002057 atomic_set(&n_rcu_torture_mberror, 0);
2058 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002059 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002060 n_rcu_torture_boost_ktrerror = 0;
2061 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002062 n_rcu_torture_boost_failure = 0;
2063 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002064 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2065 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002066 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002067 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2068 per_cpu(rcu_torture_count, cpu)[i] = 0;
2069 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2070 }
2071 }
2072
2073 /* Start up the kthreads. */
2074
2075 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
Paul E. McKenney60f53782012-08-25 15:27:40 -07002076 writer_task = kthread_create(rcu_torture_writer, NULL,
2077 "rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002078 if (IS_ERR(writer_task)) {
2079 firsterr = PTR_ERR(writer_task);
2080 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
2081 writer_task = NULL;
2082 goto unwind;
2083 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002084 wake_up_process(writer_task);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002085 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002086 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002087 if (fakewriter_tasks == NULL) {
2088 VERBOSE_PRINTK_ERRSTRING("out of memory");
2089 firsterr = -ENOMEM;
2090 goto unwind;
2091 }
2092 for (i = 0; i < nfakewriters; i++) {
2093 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2094 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002095 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07002096 if (IS_ERR(fakewriter_tasks[i])) {
2097 firsterr = PTR_ERR(fakewriter_tasks[i]);
2098 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2099 fakewriter_tasks[i] = NULL;
2100 goto unwind;
2101 }
2102 }
Josh Triplett2860aab2006-10-04 02:17:11 -07002103 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002104 GFP_KERNEL);
2105 if (reader_tasks == NULL) {
2106 VERBOSE_PRINTK_ERRSTRING("out of memory");
2107 firsterr = -ENOMEM;
2108 goto unwind;
2109 }
2110 for (i = 0; i < nrealreaders; i++) {
2111 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2112 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2113 "rcu_torture_reader");
2114 if (IS_ERR(reader_tasks[i])) {
2115 firsterr = PTR_ERR(reader_tasks[i]);
2116 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2117 reader_tasks[i] = NULL;
2118 goto unwind;
2119 }
2120 }
2121 if (stat_interval > 0) {
2122 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2123 stats_task = kthread_run(rcu_torture_stats, NULL,
2124 "rcu_torture_stats");
2125 if (IS_ERR(stats_task)) {
2126 firsterr = PTR_ERR(stats_task);
2127 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2128 stats_task = NULL;
2129 goto unwind;
2130 }
2131 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002132 if (test_no_idle_hz) {
2133 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002134
2135 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2136 firsterr = -ENOMEM;
2137 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2138 goto unwind;
2139 }
2140
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002141 /* Create the shuffler thread */
2142 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2143 "rcu_torture_shuffle");
2144 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002145 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002146 firsterr = PTR_ERR(shuffler_task);
2147 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2148 shuffler_task = NULL;
2149 goto unwind;
2150 }
2151 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07002152 if (stutter < 0)
2153 stutter = 0;
2154 if (stutter) {
2155 /* Create the stutter thread */
2156 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2157 "rcu_torture_stutter");
2158 if (IS_ERR(stutter_task)) {
2159 firsterr = PTR_ERR(stutter_task);
2160 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2161 stutter_task = NULL;
2162 goto unwind;
2163 }
2164 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002165 if (fqs_duration < 0)
2166 fqs_duration = 0;
2167 if (fqs_duration) {
2168 /* Create the stutter thread */
2169 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2170 "rcu_torture_fqs");
2171 if (IS_ERR(fqs_task)) {
2172 firsterr = PTR_ERR(fqs_task);
2173 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2174 fqs_task = NULL;
2175 goto unwind;
2176 }
2177 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002178 if (test_boost_interval < 1)
2179 test_boost_interval = 1;
2180 if (test_boost_duration < 2)
2181 test_boost_duration = 2;
2182 if ((test_boost == 1 && cur_ops->can_boost) ||
2183 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002184
2185 boost_starttime = jiffies + test_boost_interval * HZ;
2186 register_cpu_notifier(&rcutorture_cpu_nb);
2187 for_each_possible_cpu(i) {
2188 if (cpu_is_offline(i))
2189 continue; /* Heuristic: CPU can go offline. */
2190 retval = rcutorture_booster_init(i);
2191 if (retval < 0) {
2192 firsterr = retval;
2193 goto unwind;
2194 }
2195 }
2196 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002197 if (shutdown_secs > 0) {
2198 shutdown_time = jiffies + shutdown_secs * HZ;
Paul E. McKenney60f53782012-08-25 15:27:40 -07002199 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2200 "rcu_torture_shutdown");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002201 if (IS_ERR(shutdown_task)) {
2202 firsterr = PTR_ERR(shutdown_task);
2203 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2204 shutdown_task = NULL;
2205 goto unwind;
2206 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002207 wake_up_process(shutdown_task);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002208 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002209 i = rcu_torture_onoff_init();
2210 if (i != 0) {
2211 firsterr = i;
2212 goto unwind;
2213 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002214 register_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002215 i = rcu_torture_stall_init();
2216 if (i != 0) {
2217 firsterr = i;
2218 goto unwind;
2219 }
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002220 retval = rcu_torture_barrier_init();
2221 if (retval != 0) {
2222 firsterr = retval;
2223 goto unwind;
2224 }
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07002225 if (object_debug)
2226 rcu_test_debug_objects();
Paul E. McKenney4a298652011-04-03 21:33:51 -07002227 rcutorture_record_test_transition();
Paul E. McKenney343e9092008-12-15 16:13:07 -08002228 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002229 return 0;
2230
2231unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08002232 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002233 rcu_torture_cleanup();
2234 return firsterr;
2235}
2236
2237module_init(rcu_torture_init);
2238module_exit(rcu_torture_cleanup);