blob: 31dea01c85fd50fb3297627dc81556a146f249c5 [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>
Harvey Harrisonf07767f2008-10-20 10:23:38 -070049#include <asm/byteorder.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080050
51MODULE_LICENSE("GPL");
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -070052MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080053
Josh Triplett48022112006-10-03 23:26:16 +020054static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
Josh Triplettb772e1d2006-10-04 02:17:13 -070055static int nfakewriters = 4; /* # fake writer threads */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070056static int stat_interval = 60; /* Interval between stats, in seconds. */
57 /* Zero means "only at end of test". */
Rusty Russelld8e8ed92011-12-15 13:43:24 +103058static bool verbose; /* Print more debug info. */
Paul E. McKenneyab840f72012-07-23 12:30:22 -070059static bool test_no_idle_hz = true;
60 /* Test RCU support for tickless idle CPUs. */
Paul E. McKenneyd120f652008-06-18 05:21:44 -070061static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
62static int stutter = 5; /* Start/stop testing interval (in sec) */
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070063static int irqreader = 1; /* RCU readers from irq (timers). */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070064static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
65static int fqs_holdoff; /* Hold time within burst (us). */
Paul E. McKenneybf66f182010-01-04 15:09:10 -080066static int fqs_stutter = 3; /* Wait time between bursts (s). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -080067static int n_barrier_cbs; /* Number of callbacks to test RCU barriers. */
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -080068static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -080069static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -070070static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -080071static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
72static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
Paul E. McKenney8e8be452010-09-02 16:16:14 -070073static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
74static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
75static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
Josh Triplett20d2e422006-10-04 02:17:15 -070076static char *torture_type = "rcu"; /* What RCU implementation to torture. */
Paul E. McKenneya241ec62005-10-30 15:03:12 -080077
Josh Triplettd6ad6712007-03-06 01:42:13 -080078module_param(nreaders, int, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080079MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
Josh Triplettd6ad6712007-03-06 01:42:13 -080080module_param(nfakewriters, int, 0444);
Josh Triplettb772e1d2006-10-04 02:17:13 -070081MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
Paul E. McKenney3721bc12011-07-21 16:00:17 -070082module_param(stat_interval, int, 0644);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080083MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080084module_param(verbose, bool, 0444);
Paul E. McKenneya241ec62005-10-30 15:03:12 -080085MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
Josh Triplettd6ad6712007-03-06 01:42:13 -080086module_param(test_no_idle_hz, bool, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080087MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
Josh Triplettd6ad6712007-03-06 01:42:13 -080088module_param(shuffle_interval, int, 0444);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -080089MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
Paul E. McKenneyd120f652008-06-18 05:21:44 -070090module_param(stutter, int, 0444);
91MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
Paul E. McKenney0729fbf2008-06-25 12:24:52 -070092module_param(irqreader, int, 0444);
93MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
Paul E. McKenneybf66f182010-01-04 15:09:10 -080094module_param(fqs_duration, int, 0444);
95MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
96module_param(fqs_holdoff, int, 0444);
97MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
98module_param(fqs_stutter, int, 0444);
99MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800100module_param(n_barrier_cbs, int, 0444);
101MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800102module_param(onoff_interval, int, 0444);
103MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -0800104module_param(onoff_holdoff, int, 0444);
105MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700106module_param(shutdown_secs, int, 0444);
107MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800108module_param(stall_cpu, int, 0444);
109MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
110module_param(stall_cpu_holdoff, int, 0444);
111MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700112module_param(test_boost, int, 0444);
113MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
114module_param(test_boost_interval, int, 0444);
115MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
116module_param(test_boost_duration, int, 0444);
117MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
Josh Triplettd6ad6712007-03-06 01:42:13 -0800118module_param(torture_type, charp, 0444);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700119MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700120
121#define TORTURE_FLAG "-torture:"
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800122#define PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700123 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800124#define VERBOSE_PRINTK_STRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700125 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800126#define VERBOSE_PRINTK_ERRSTRING(s) \
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700127 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800128
129static char printk_buf[4096];
130
131static int nrealreaders;
132static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700133static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800134static struct task_struct **reader_tasks;
135static struct task_struct *stats_task;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -0800136static struct task_struct *shuffler_task;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700137static struct task_struct *stutter_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800138static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700139static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700140static struct task_struct *shutdown_task;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800141#ifdef CONFIG_HOTPLUG_CPU
142static struct task_struct *onoff_task;
143#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800144static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800145static struct task_struct **barrier_cbs_tasks;
146static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800147
148#define RCU_TORTURE_PIPE_LEN 10
149
150struct rcu_torture {
151 struct rcu_head rtort_rcu;
152 int rtort_pipe_count;
153 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800154 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800155};
156
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800157static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700158static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700159static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800160static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
161static DEFINE_SPINLOCK(rcu_torture_lock);
162static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
163 { 0 };
164static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
165 { 0 };
166static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700167static atomic_t n_rcu_torture_alloc;
168static atomic_t n_rcu_torture_alloc_fail;
169static atomic_t n_rcu_torture_free;
170static atomic_t n_rcu_torture_mberror;
171static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800172static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700173static long n_rcu_torture_boost_ktrerror;
174static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700175static long n_rcu_torture_boost_failure;
176static long n_rcu_torture_boosts;
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700177static long n_rcu_torture_timers;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800178static long n_offline_attempts;
179static long n_offline_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700180static unsigned long sum_offline;
181static int min_offline = -1;
182static int max_offline;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -0800183static long n_online_attempts;
184static long n_online_successes;
Paul E. McKenney13dbf912012-07-23 12:05:55 -0700185static unsigned long sum_online;
186static int min_online = -1;
187static int max_online;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800188static long n_barrier_attempts;
189static long n_barrier_successes;
Josh Triplette3033732006-10-04 02:17:14 -0700190static struct list_head rcu_torture_removed;
Rusty Russell73d0a4b2009-03-30 22:05:16 -0600191static cpumask_var_t shuffle_tmp_mask;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800192
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700193static int stutter_pause_test;
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700194
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700195#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
196#define RCUTORTURE_RUNNABLE_INIT 1
197#else
198#define RCUTORTURE_RUNNABLE_INIT 0
199#endif
200int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
Paul E. McKenneybb3bf702011-11-04 13:24:07 -0700201module_param(rcutorture_runnable, int, 0444);
202MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
Paul E. McKenney31a72bc2008-06-18 09:26:49 -0700203
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700204#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700205#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700206#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700207#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700208#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700209
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700210static unsigned long shutdown_time; /* jiffies to system shutdown. */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700211static unsigned long boost_starttime; /* jiffies of next boost test start. */
212DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
213 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800214static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700215static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800216static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
217static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
218static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700219
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800220/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
221
222#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
223#define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
224#define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
225static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700226/*
227 * Protect fullstop transitions and spawning of kthreads.
228 */
229static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800230
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -0700231/* Forward reference. */
232static void rcu_torture_cleanup(void);
233
Paul E. McKenney343e9092008-12-15 16:13:07 -0800234/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800235 * Detect and respond to a system shutdown.
Paul E. McKenney343e9092008-12-15 16:13:07 -0800236 */
237static int
238rcutorture_shutdown_notify(struct notifier_block *unused1,
239 unsigned long unused2, void *unused3)
240{
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800241 mutex_lock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800242 if (fullstop == FULLSTOP_DONTSTOP)
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800243 fullstop = FULLSTOP_SHUTDOWN;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800244 else
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700245 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800246 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenneyc59ab972009-01-04 18:28:27 -0800247 mutex_unlock(&fullstop_mutex);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800248 return NOTIFY_DONE;
249}
250
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800251/*
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800252 * Absorb kthreads into a kernel function that won't return, so that
253 * they won't ever access module text or data again.
254 */
255static void rcutorture_shutdown_absorb(char *title)
256{
257 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -0700258 pr_notice(
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800259 "rcutorture thread %s parking due to system shutdown\n",
260 title);
261 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
262 }
263}
264
265/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800266 * Allocate an element from the rcu_tortures pool.
267 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800268static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800269rcu_torture_alloc(void)
270{
271 struct list_head *p;
272
Ingo Molnaradac1662006-01-25 19:50:12 +0100273 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800274 if (list_empty(&rcu_torture_freelist)) {
275 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100276 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800277 return NULL;
278 }
279 atomic_inc(&n_rcu_torture_alloc);
280 p = rcu_torture_freelist.next;
281 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100282 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800283 return container_of(p, struct rcu_torture, rtort_free);
284}
285
286/*
287 * Free an element to the rcu_tortures pool.
288 */
289static void
290rcu_torture_free(struct rcu_torture *p)
291{
292 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100293 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800294 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100295 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800296}
297
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800298struct rcu_random_state {
299 unsigned long rrs_state;
Josh Triplett75cfef32006-10-04 02:17:12 -0700300 long rrs_count;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800301};
302
303#define RCU_RANDOM_MULT 39916801 /* prime */
304#define RCU_RANDOM_ADD 479001701 /* prime */
305#define RCU_RANDOM_REFRESH 10000
306
307#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
308
309/*
310 * Crude but fast random-number generator. Uses a linear congruential
Paul E. McKenneyc17ac852007-10-16 23:27:19 -0700311 * generator, with occasional help from cpu_clock().
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800312 */
Josh Triplett75cfef32006-10-04 02:17:12 -0700313static unsigned long
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800314rcu_random(struct rcu_random_state *rrsp)
315{
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800316 if (--rrsp->rrs_count < 0) {
Peter Zijlstrac6763292010-05-25 10:48:51 +0200317 rrsp->rrs_state += (unsigned long)local_clock();
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800318 rrsp->rrs_count = RCU_RANDOM_REFRESH;
319 }
320 rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
321 return swahw32(rrsp->rrs_state);
322}
323
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700324static void
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800325rcu_stutter_wait(char *title)
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700326{
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800327 while (stutter_pause_test || !rcutorture_runnable) {
Paul E. McKenneye3d7be22008-06-22 13:06:38 -0700328 if (rcutorture_runnable)
329 schedule_timeout_interruptible(1);
330 else
Paul E. McKenney3ccf79f2008-06-22 14:02:55 -0700331 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800332 rcutorture_shutdown_absorb(title);
Paul E. McKenney343e9092008-12-15 16:13:07 -0800333 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -0700334}
335
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800336/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700337 * Operations vector for selecting different types of tests.
338 */
339
340struct rcu_torture_ops {
341 void (*init)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700342 int (*readlock)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700343 void (*read_delay)(struct rcu_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700344 void (*readunlock)(int idx);
345 int (*completed)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700346 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700347 void (*sync)(void);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800348 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
Paul E. McKenney23269742008-05-12 21:21:05 +0200349 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800350 void (*fqs)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700351 int (*stats)(char *page);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700352 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700353 int can_boost;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700354 char *name;
355};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700356
357static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700358
359/*
360 * Definitions for rcu torture testing.
361 */
362
Josh Tripletta49a4af2006-09-29 01:59:30 -0700363static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700364{
365 rcu_read_lock();
366 return 0;
367}
368
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700369static void rcu_read_delay(struct rcu_random_state *rrsp)
370{
Josh Triplettb8d57a72009-09-08 15:54:35 -0700371 const unsigned long shortdelay_us = 200;
372 const unsigned long longdelay_ms = 50;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700373
Josh Triplettb8d57a72009-09-08 15:54:35 -0700374 /* We want a short delay sometimes to make a reader delay the grace
375 * period, and we want a long delay occasionally to trigger
376 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700377
Josh Triplettb8d57a72009-09-08 15:54:35 -0700378 if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
379 mdelay(longdelay_ms);
380 if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
381 udelay(shortdelay_us);
Lai Jiangshane546f482010-06-21 16:57:42 +0800382#ifdef CONFIG_PREEMPT
383 if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
384 preempt_schedule(); /* No QS if preempt_disable() in effect */
385#endif
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700386}
387
Josh Tripletta49a4af2006-09-29 01:59:30 -0700388static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700389{
390 rcu_read_unlock();
391}
392
393static int rcu_torture_completed(void)
394{
395 return rcu_batches_completed();
396}
397
398static void
399rcu_torture_cb(struct rcu_head *p)
400{
401 int i;
402 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
403
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800404 if (fullstop != FULLSTOP_DONTSTOP) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700405 /* Test is ending, just drop callbacks on the floor. */
406 /* The next initialization will pick up the pieces. */
407 return;
408 }
409 i = rp->rtort_pipe_count;
410 if (i > RCU_TORTURE_PIPE_LEN)
411 i = RCU_TORTURE_PIPE_LEN;
412 atomic_inc(&rcu_torture_wcount[i]);
413 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
414 rp->rtort_mbtest = 0;
415 rcu_torture_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700416 } else {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700417 cur_ops->deferred_free(rp);
Paul E. McKenneyc701d5d2012-06-28 08:08:25 -0700418 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700419}
420
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800421static int rcu_no_completed(void)
422{
423 return 0;
424}
425
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700426static void rcu_torture_deferred_free(struct rcu_torture *p)
427{
428 call_rcu(&p->rtort_rcu, rcu_torture_cb);
429}
430
431static struct rcu_torture_ops rcu_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700432 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700433 .readlock = rcu_torture_read_lock,
434 .read_delay = rcu_read_delay,
435 .readunlock = rcu_torture_read_unlock,
436 .completed = rcu_torture_completed,
437 .deferred_free = rcu_torture_deferred_free,
438 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800439 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700440 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800441 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700442 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700443 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700444 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700445 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700446};
447
Josh Triplette3033732006-10-04 02:17:14 -0700448static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
449{
450 int i;
451 struct rcu_torture *rp;
452 struct rcu_torture *rp1;
453
454 cur_ops->sync();
455 list_add(&p->rtort_free, &rcu_torture_removed);
456 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
457 i = rp->rtort_pipe_count;
458 if (i > RCU_TORTURE_PIPE_LEN)
459 i = RCU_TORTURE_PIPE_LEN;
460 atomic_inc(&rcu_torture_wcount[i]);
461 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
462 rp->rtort_mbtest = 0;
463 list_del(&rp->rtort_free);
464 rcu_torture_free(rp);
465 }
466 }
467}
468
469static void rcu_sync_torture_init(void)
470{
471 INIT_LIST_HEAD(&rcu_torture_removed);
472}
473
Josh Triplett20d2e422006-10-04 02:17:15 -0700474static struct rcu_torture_ops rcu_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700475 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700476 .readlock = rcu_torture_read_lock,
477 .read_delay = rcu_read_delay,
478 .readunlock = rcu_torture_read_unlock,
479 .completed = rcu_torture_completed,
480 .deferred_free = rcu_sync_torture_deferred_free,
481 .sync = synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800482 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700483 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800484 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700485 .stats = NULL,
486 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700487 .can_boost = rcu_can_boost(),
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700488 .name = "rcu_sync"
Josh Triplett20d2e422006-10-04 02:17:15 -0700489};
490
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800491static struct rcu_torture_ops rcu_expedited_ops = {
492 .init = rcu_sync_torture_init,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800493 .readlock = rcu_torture_read_lock,
494 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
495 .readunlock = rcu_torture_read_unlock,
496 .completed = rcu_no_completed,
497 .deferred_free = rcu_sync_torture_deferred_free,
498 .sync = synchronize_rcu_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800499 .call = NULL,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800500 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800501 .fqs = rcu_force_quiescent_state,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800502 .stats = NULL,
503 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700504 .can_boost = rcu_can_boost(),
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800505 .name = "rcu_expedited"
506};
507
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700508/*
509 * Definitions for rcu_bh torture testing.
510 */
511
Josh Tripletta49a4af2006-09-29 01:59:30 -0700512static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700513{
514 rcu_read_lock_bh();
515 return 0;
516}
517
Josh Tripletta49a4af2006-09-29 01:59:30 -0700518static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700519{
520 rcu_read_unlock_bh();
521}
522
523static int rcu_bh_torture_completed(void)
524{
525 return rcu_batches_completed_bh();
526}
527
528static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
529{
530 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
531}
532
533static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700534 .init = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700535 .readlock = rcu_bh_torture_read_lock,
536 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
537 .readunlock = rcu_bh_torture_read_unlock,
538 .completed = rcu_bh_torture_completed,
539 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700540 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800541 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700542 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800543 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700544 .stats = NULL,
545 .irq_capable = 1,
546 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700547};
548
Josh Triplett11a14702006-10-04 02:17:16 -0700549static struct rcu_torture_ops rcu_bh_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700550 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700551 .readlock = rcu_bh_torture_read_lock,
552 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
553 .readunlock = rcu_bh_torture_read_unlock,
554 .completed = rcu_bh_torture_completed,
555 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700556 .sync = synchronize_rcu_bh,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800557 .call = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700558 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800559 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700560 .stats = NULL,
561 .irq_capable = 1,
562 .name = "rcu_bh_sync"
Josh Triplett11a14702006-10-04 02:17:16 -0700563};
564
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700565static struct rcu_torture_ops rcu_bh_expedited_ops = {
566 .init = rcu_sync_torture_init,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700567 .readlock = rcu_bh_torture_read_lock,
568 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
569 .readunlock = rcu_bh_torture_read_unlock,
570 .completed = rcu_bh_torture_completed,
571 .deferred_free = rcu_sync_torture_deferred_free,
572 .sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800573 .call = NULL,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700574 .cb_barrier = NULL,
575 .fqs = rcu_bh_force_quiescent_state,
576 .stats = NULL,
577 .irq_capable = 1,
578 .name = "rcu_bh_expedited"
579};
580
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700581/*
582 * Definitions for srcu torture testing.
583 */
584
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800585DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700586
Josh Triplett012d3ca2006-12-06 20:40:19 -0800587static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700588{
589 return srcu_read_lock(&srcu_ctl);
590}
591
592static void srcu_read_delay(struct rcu_random_state *rrsp)
593{
594 long delay;
595 const long uspertick = 1000000 / HZ;
596 const long longdelay = 10;
597
598 /* We want there to be long-running readers, but not all the time. */
599
600 delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
601 if (!delay)
602 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800603 else
604 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700605}
606
Josh Triplett012d3ca2006-12-06 20:40:19 -0800607static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700608{
609 srcu_read_unlock(&srcu_ctl, idx);
610}
611
612static int srcu_torture_completed(void)
613{
614 return srcu_batches_completed(&srcu_ctl);
615}
616
Lai Jiangshan9059c942012-03-19 16:12:14 +0800617static void srcu_torture_deferred_free(struct rcu_torture *rp)
618{
619 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
620}
621
Josh Triplettb772e1d2006-10-04 02:17:13 -0700622static void srcu_torture_synchronize(void)
623{
624 synchronize_srcu(&srcu_ctl);
625}
626
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700627static void srcu_torture_call(struct rcu_head *head,
628 void (*func)(struct rcu_head *head))
629{
630 call_srcu(&srcu_ctl, head, func);
631}
632
633static void srcu_torture_barrier(void)
634{
635 srcu_barrier(&srcu_ctl);
636}
637
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700638static int srcu_torture_stats(char *page)
639{
640 int cnt = 0;
641 int cpu;
642 int idx = srcu_ctl.completed & 0x1;
643
644 cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
645 torture_type, TORTURE_FLAG, idx);
646 for_each_possible_cpu(cpu) {
Paul E. McKenneycef50122012-02-05 07:42:44 -0800647 cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700648 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
649 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
650 }
651 cnt += sprintf(&page[cnt], "\n");
652 return cnt;
653}
654
655static struct rcu_torture_ops srcu_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800656 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700657 .readlock = srcu_torture_read_lock,
658 .read_delay = srcu_read_delay,
659 .readunlock = srcu_torture_read_unlock,
660 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800661 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700662 .sync = srcu_torture_synchronize,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700663 .call = srcu_torture_call,
664 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700665 .stats = srcu_torture_stats,
666 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700667};
668
Lai Jiangshan9059c942012-03-19 16:12:14 +0800669static struct rcu_torture_ops srcu_sync_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800670 .init = rcu_sync_torture_init,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800671 .readlock = srcu_torture_read_lock,
672 .read_delay = srcu_read_delay,
673 .readunlock = srcu_torture_read_unlock,
674 .completed = srcu_torture_completed,
675 .deferred_free = rcu_sync_torture_deferred_free,
676 .sync = srcu_torture_synchronize,
677 .call = NULL,
678 .cb_barrier = NULL,
679 .stats = srcu_torture_stats,
680 .name = "srcu_sync"
681};
682
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800683static int srcu_torture_read_lock_raw(void) __acquires(&srcu_ctl)
684{
685 return srcu_read_lock_raw(&srcu_ctl);
686}
687
688static void srcu_torture_read_unlock_raw(int idx) __releases(&srcu_ctl)
689{
690 srcu_read_unlock_raw(&srcu_ctl, idx);
691}
692
693static struct rcu_torture_ops srcu_raw_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800694 .init = rcu_sync_torture_init,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800695 .readlock = srcu_torture_read_lock_raw,
696 .read_delay = srcu_read_delay,
697 .readunlock = srcu_torture_read_unlock_raw,
698 .completed = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800699 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800700 .sync = srcu_torture_synchronize,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800701 .call = NULL,
Paul E. McKenney101db7b2011-12-05 15:36:31 -0800702 .cb_barrier = NULL,
703 .stats = srcu_torture_stats,
704 .name = "srcu_raw"
705};
706
Lai Jiangshan9059c942012-03-19 16:12:14 +0800707static struct rcu_torture_ops srcu_raw_sync_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800708 .init = rcu_sync_torture_init,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800709 .readlock = srcu_torture_read_lock_raw,
710 .read_delay = srcu_read_delay,
711 .readunlock = srcu_torture_read_unlock_raw,
712 .completed = srcu_torture_completed,
713 .deferred_free = rcu_sync_torture_deferred_free,
714 .sync = srcu_torture_synchronize,
715 .call = NULL,
716 .cb_barrier = NULL,
717 .stats = srcu_torture_stats,
718 .name = "srcu_raw_sync"
719};
720
Paul E. McKenney804bb832009-10-25 19:03:52 -0700721static void srcu_torture_synchronize_expedited(void)
722{
723 synchronize_srcu_expedited(&srcu_ctl);
724}
725
726static struct rcu_torture_ops srcu_expedited_ops = {
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800727 .init = rcu_sync_torture_init,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700728 .readlock = srcu_torture_read_lock,
729 .read_delay = srcu_read_delay,
730 .readunlock = srcu_torture_read_unlock,
731 .completed = srcu_torture_completed,
732 .deferred_free = rcu_sync_torture_deferred_free,
733 .sync = srcu_torture_synchronize_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800734 .call = NULL,
Paul E. McKenney804bb832009-10-25 19:03:52 -0700735 .cb_barrier = NULL,
736 .stats = srcu_torture_stats,
737 .name = "srcu_expedited"
738};
739
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700740/*
741 * Definitions for sched torture testing.
742 */
743
744static int sched_torture_read_lock(void)
745{
746 preempt_disable();
747 return 0;
748}
749
750static void sched_torture_read_unlock(int idx)
751{
752 preempt_enable();
753}
754
Paul E. McKenney23269742008-05-12 21:21:05 +0200755static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
756{
757 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
758}
759
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700760static struct rcu_torture_ops sched_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700761 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700762 .readlock = sched_torture_read_lock,
763 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
764 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800765 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700766 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700767 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700768 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800769 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700770 .stats = NULL,
771 .irq_capable = 1,
772 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700773};
774
Paul E. McKenney804bb832009-10-25 19:03:52 -0700775static struct rcu_torture_ops sched_sync_ops = {
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700776 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700777 .readlock = sched_torture_read_lock,
778 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
779 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800780 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700781 .deferred_free = rcu_sync_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700782 .sync = synchronize_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700783 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800784 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700785 .stats = NULL,
786 .name = "sched_sync"
787};
788
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700789static struct rcu_torture_ops sched_expedited_ops = {
790 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700791 .readlock = sched_torture_read_lock,
792 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
793 .readunlock = sched_torture_read_unlock,
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800794 .completed = rcu_no_completed,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700795 .deferred_free = rcu_sync_torture_deferred_free,
796 .sync = synchronize_sched_expedited,
797 .cb_barrier = NULL,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800798 .fqs = rcu_sched_force_quiescent_state,
Tejun Heo969c7922010-05-06 18:49:21 +0200799 .stats = NULL,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700800 .irq_capable = 1,
801 .name = "sched_expedited"
Paul E. McKenney23269742008-05-12 21:21:05 +0200802};
803
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700804/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700805 * RCU torture priority-boost testing. Runs one real-time thread per
806 * CPU for moderate bursts, repeatedly registering RCU callbacks and
807 * spinning waiting for them to be invoked. If a given callback takes
808 * too long to be invoked, we assume that priority inversion has occurred.
809 */
810
811struct rcu_boost_inflight {
812 struct rcu_head rcu;
813 int inflight;
814};
815
816static void rcu_torture_boost_cb(struct rcu_head *head)
817{
818 struct rcu_boost_inflight *rbip =
819 container_of(head, struct rcu_boost_inflight, rcu);
820
821 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
822 rbip->inflight = 0;
823}
824
825static int rcu_torture_boost(void *arg)
826{
827 unsigned long call_rcu_time;
828 unsigned long endtime;
829 unsigned long oldstarttime;
830 struct rcu_boost_inflight rbi = { .inflight = 0 };
831 struct sched_param sp;
832
833 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
834
835 /* Set real-time priority. */
836 sp.sched_priority = 1;
837 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
838 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
839 n_rcu_torture_boost_rterror++;
840 }
841
Paul E. McKenney561190e2011-03-30 09:10:44 -0700842 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700843 /* Each pass through the following loop does one boost-test cycle. */
844 do {
845 /* Wait for the next test interval. */
846 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700847 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700848 schedule_timeout_uninterruptible(1);
849 rcu_stutter_wait("rcu_torture_boost");
850 if (kthread_should_stop() ||
851 fullstop != FULLSTOP_DONTSTOP)
852 goto checkwait;
853 }
854
855 /* Do one boost-test interval. */
856 endtime = oldstarttime + test_boost_duration * HZ;
857 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700858 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700859 /* If we don't have a callback in flight, post one. */
860 if (!rbi.inflight) {
861 smp_mb(); /* RCU core before ->inflight = 1. */
862 rbi.inflight = 1;
863 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
864 if (jiffies - call_rcu_time >
865 test_boost_duration * HZ - HZ / 2) {
866 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
867 n_rcu_torture_boost_failure++;
868 }
869 call_rcu_time = jiffies;
870 }
871 cond_resched();
872 rcu_stutter_wait("rcu_torture_boost");
873 if (kthread_should_stop() ||
874 fullstop != FULLSTOP_DONTSTOP)
875 goto checkwait;
876 }
877
878 /*
879 * Set the start time of the next test interval.
880 * Yes, this is vulnerable to long delays, but such
881 * delays simply cause a false negative for the next
882 * interval. Besides, we are running at RT priority,
883 * so delays should be relatively rare.
884 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700885 while (oldstarttime == boost_starttime &&
886 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700887 if (mutex_trylock(&boost_mutex)) {
888 boost_starttime = jiffies +
889 test_boost_interval * HZ;
890 n_rcu_torture_boosts++;
891 mutex_unlock(&boost_mutex);
892 break;
893 }
894 schedule_timeout_uninterruptible(1);
895 }
896
897 /* Go do the stutter. */
898checkwait: rcu_stutter_wait("rcu_torture_boost");
899 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
900
901 /* Clean up and exit. */
902 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
903 rcutorture_shutdown_absorb("rcu_torture_boost");
904 while (!kthread_should_stop() || rbi.inflight)
905 schedule_timeout_uninterruptible(1);
906 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
Paul E. McKenney9d681972011-06-21 01:48:03 -0700907 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700908 return 0;
909}
910
911/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800912 * RCU torture force-quiescent-state kthread. Repeatedly induces
913 * bursts of calls to force_quiescent_state(), increasing the probability
914 * of occurrence of some important types of race conditions.
915 */
916static int
917rcu_torture_fqs(void *arg)
918{
919 unsigned long fqs_resume_time;
920 int fqs_burst_remaining;
921
922 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
923 do {
924 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700925 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
926 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800927 schedule_timeout_interruptible(1);
928 }
929 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700930 while (fqs_burst_remaining > 0 &&
931 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800932 cur_ops->fqs();
933 udelay(fqs_holdoff);
934 fqs_burst_remaining -= fqs_holdoff;
935 }
936 rcu_stutter_wait("rcu_torture_fqs");
937 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
938 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
939 rcutorture_shutdown_absorb("rcu_torture_fqs");
940 while (!kthread_should_stop())
941 schedule_timeout_uninterruptible(1);
942 return 0;
943}
944
945/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800946 * RCU torture writer kthread. Repeatedly substitutes a new structure
947 * for that pointed to by rcu_torture_current, freeing the old structure
948 * after a series of grace periods (the "pipeline").
949 */
950static int
951rcu_torture_writer(void *arg)
952{
953 int i;
954 long oldbatch = rcu_batches_completed();
955 struct rcu_torture *rp;
956 struct rcu_torture *old_rp;
957 static DEFINE_RCU_RANDOM(rand);
958
959 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800960 set_user_nice(current, 19);
961
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800962 do {
963 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700964 rp = rcu_torture_alloc();
965 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800966 continue;
967 rp->rtort_pipe_count = 0;
968 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700969 old_rp = rcu_dereference_check(rcu_torture_current,
970 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800971 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800972 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700973 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700974 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800975 i = old_rp->rtort_pipe_count;
976 if (i > RCU_TORTURE_PIPE_LEN)
977 i = RCU_TORTURE_PIPE_LEN;
978 atomic_inc(&rcu_torture_wcount[i]);
979 old_rp->rtort_pipe_count++;
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700980 cur_ops->deferred_free(old_rp);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800981 }
Paul E. McKenney4a298652011-04-03 21:33:51 -0700982 rcutorture_record_progress(++rcu_torture_current_version);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700983 oldbatch = cur_ops->completed();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800984 rcu_stutter_wait("rcu_torture_writer");
985 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800986 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -0800987 rcutorture_shutdown_absorb("rcu_torture_writer");
988 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800989 schedule_timeout_uninterruptible(1);
990 return 0;
991}
992
993/*
Josh Triplettb772e1d2006-10-04 02:17:13 -0700994 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
995 * delay between calls.
996 */
997static int
998rcu_torture_fakewriter(void *arg)
999{
1000 DEFINE_RCU_RANDOM(rand);
1001
1002 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
1003 set_user_nice(current, 19);
1004
1005 do {
1006 schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
1007 udelay(rcu_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -07001008 if (cur_ops->cb_barrier != NULL &&
1009 rcu_random(&rand) % (nfakewriters * 8) == 0)
1010 cur_ops->cb_barrier();
1011 else
1012 cur_ops->sync();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001013 rcu_stutter_wait("rcu_torture_fakewriter");
1014 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001015
1016 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001017 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
1018 while (!kthread_should_stop())
Josh Triplettb772e1d2006-10-04 02:17:13 -07001019 schedule_timeout_uninterruptible(1);
1020 return 0;
1021}
1022
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001023void rcutorture_trace_dump(void)
1024{
1025 static atomic_t beenhere = ATOMIC_INIT(0);
1026
1027 if (atomic_read(&beenhere))
1028 return;
1029 if (atomic_xchg(&beenhere, 1) != 0)
1030 return;
1031 do_trace_rcu_torture_read(cur_ops->name, (struct rcu_head *)~0UL);
1032 ftrace_dump(DUMP_ALL);
1033}
1034
Josh Triplettb772e1d2006-10-04 02:17:13 -07001035/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001036 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1037 * incrementing the corresponding element of the pipeline array. The
1038 * counter in the element should never be greater than 1, otherwise, the
1039 * RCU implementation is broken.
1040 */
1041static void rcu_torture_timer(unsigned long unused)
1042{
1043 int idx;
1044 int completed;
1045 static DEFINE_RCU_RANDOM(rand);
1046 static DEFINE_SPINLOCK(rand_lock);
1047 struct rcu_torture *p;
1048 int pipe_count;
1049
1050 idx = cur_ops->readlock();
1051 completed = cur_ops->completed();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001052 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001053 rcu_read_lock_bh_held() ||
1054 rcu_read_lock_sched_held() ||
1055 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001056 if (p == NULL) {
1057 /* Leave because rcu_torture_writer is not yet underway */
1058 cur_ops->readunlock(idx);
1059 return;
1060 }
Paul E. McKenney7129d382012-01-31 16:46:34 -08001061 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001062 if (p->rtort_mbtest == 0)
1063 atomic_inc(&n_rcu_torture_mberror);
1064 spin_lock(&rand_lock);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001065 cur_ops->read_delay(&rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001066 n_rcu_torture_timers++;
1067 spin_unlock(&rand_lock);
1068 preempt_disable();
1069 pipe_count = p->rtort_pipe_count;
1070 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1071 /* Should not happen, but... */
1072 pipe_count = RCU_TORTURE_PIPE_LEN;
1073 }
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001074 if (pipe_count > 1)
1075 rcutorture_trace_dump();
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001076 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001077 completed = cur_ops->completed() - completed;
1078 if (completed > RCU_TORTURE_PIPE_LEN) {
1079 /* Should not happen, but... */
1080 completed = RCU_TORTURE_PIPE_LEN;
1081 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001082 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001083 preempt_enable();
1084 cur_ops->readunlock(idx);
1085}
1086
1087/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001088 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1089 * incrementing the corresponding element of the pipeline array. The
1090 * counter in the element should never be greater than 1, otherwise, the
1091 * RCU implementation is broken.
1092 */
1093static int
1094rcu_torture_reader(void *arg)
1095{
1096 int completed;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001097 int idx;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001098 DEFINE_RCU_RANDOM(rand);
1099 struct rcu_torture *p;
1100 int pipe_count;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001101 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001102
1103 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001104 set_user_nice(current, 19);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001105 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001106 setup_timer_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001107
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001108 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001109 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001110 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001111 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001112 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001113 idx = cur_ops->readlock();
1114 completed = cur_ops->completed();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001115 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001116 rcu_read_lock_bh_held() ||
1117 rcu_read_lock_sched_held() ||
1118 srcu_read_lock_held(&srcu_ctl));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001119 if (p == NULL) {
1120 /* Wait for rcu_torture_writer to get underway */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001121 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001122 schedule_timeout_interruptible(HZ);
1123 continue;
1124 }
Paul E. McKenney7129d382012-01-31 16:46:34 -08001125 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001126 if (p->rtort_mbtest == 0)
1127 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001128 cur_ops->read_delay(&rand);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001129 preempt_disable();
1130 pipe_count = p->rtort_pipe_count;
1131 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1132 /* Should not happen, but... */
1133 pipe_count = RCU_TORTURE_PIPE_LEN;
1134 }
Paul E. McKenney91afaf32011-10-02 07:44:32 -07001135 if (pipe_count > 1)
1136 rcutorture_trace_dump();
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001137 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001138 completed = cur_ops->completed() - completed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001139 if (completed > RCU_TORTURE_PIPE_LEN) {
1140 /* Should not happen, but... */
1141 completed = RCU_TORTURE_PIPE_LEN;
1142 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001143 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001144 preempt_enable();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001145 cur_ops->readunlock(idx);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001146 schedule();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001147 rcu_stutter_wait("rcu_torture_reader");
1148 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001149 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001150 rcutorture_shutdown_absorb("rcu_torture_reader");
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001151 if (irqreader && cur_ops->irq_capable)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001152 del_timer_sync(&t);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001153 while (!kthread_should_stop())
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001154 schedule_timeout_uninterruptible(1);
1155 return 0;
1156}
1157
1158/*
1159 * Create an RCU-torture statistics message in the specified buffer.
1160 */
1161static int
1162rcu_torture_printk(char *page)
1163{
1164 int cnt = 0;
1165 int cpu;
1166 int i;
1167 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1168 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1169
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001170 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001171 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1172 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1173 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1174 }
1175 }
1176 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1177 if (pipesummary[i] != 0)
1178 break;
1179 }
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001180 cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001181 cnt += sprintf(&page[cnt],
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001182 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001183 rcu_torture_current,
1184 rcu_torture_current_version,
1185 list_empty(&rcu_torture_freelist),
1186 atomic_read(&n_rcu_torture_alloc),
1187 atomic_read(&n_rcu_torture_alloc_fail),
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001188 atomic_read(&n_rcu_torture_free));
1189 cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001190 atomic_read(&n_rcu_torture_mberror),
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001191 n_rcu_torture_boost_ktrerror,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001192 n_rcu_torture_boost_rterror);
1193 cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001194 n_rcu_torture_boost_failure,
1195 n_rcu_torture_boosts,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001196 n_rcu_torture_timers);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001197 cnt += sprintf(&page[cnt],
1198 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1199 n_online_successes, n_online_attempts,
1200 n_offline_successes, n_offline_attempts,
1201 min_online, max_online,
1202 min_offline, max_offline,
1203 sum_online, sum_offline, HZ);
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -07001204 cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001205 n_barrier_successes,
1206 n_barrier_attempts,
1207 n_rcu_torture_barrier_error);
1208 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001209 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001210 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001211 n_rcu_torture_boost_ktrerror != 0 ||
1212 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001213 n_rcu_torture_boost_failure != 0 ||
1214 i > 1) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001215 cnt += sprintf(&page[cnt], "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001216 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001217 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001218 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001219 cnt += sprintf(&page[cnt], "Reader Pipe: ");
1220 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1221 cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001222 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001223 cnt += sprintf(&page[cnt], "Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001224 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001225 cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001226 cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001227 cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1228 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1229 cnt += sprintf(&page[cnt], " %d",
1230 atomic_read(&rcu_torture_wcount[i]));
1231 }
1232 cnt += sprintf(&page[cnt], "\n");
Josh Triplettc8e5b162007-05-08 00:33:20 -07001233 if (cur_ops->stats)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001234 cnt += cur_ops->stats(&page[cnt]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001235 return cnt;
1236}
1237
1238/*
1239 * Print torture statistics. Caller must ensure that there is only
1240 * one call to this function at a given time!!! This is normally
1241 * accomplished by relying on the module system to only have one copy
1242 * of the module loaded, and then by giving the rcu_torture_stats
1243 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1244 * thread is not running).
1245 */
1246static void
1247rcu_torture_stats_print(void)
1248{
1249 int cnt;
1250
1251 cnt = rcu_torture_printk(printk_buf);
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001252 pr_alert("%s", printk_buf);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001253}
1254
1255/*
1256 * Periodically prints torture statistics, if periodic statistics printing
1257 * was specified via the stat_interval module parameter.
1258 *
1259 * No need to worry about fullstop here, since this one doesn't reference
1260 * volatile state or register callbacks.
1261 */
1262static int
1263rcu_torture_stats(void *arg)
1264{
1265 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1266 do {
1267 schedule_timeout_interruptible(stat_interval * HZ);
1268 rcu_torture_stats_print();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001269 rcutorture_shutdown_absorb("rcu_torture_stats");
1270 } while (!kthread_should_stop());
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001271 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1272 return 0;
1273}
1274
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001275static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1276
1277/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1278 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1279 */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -07001280static void rcu_torture_shuffle_tasks(void)
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001281{
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001282 int i;
1283
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001284 cpumask_setall(shuffle_tmp_mask);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001285 get_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001286
1287 /* No point in shuffling if there is only one online CPU (ex: UP) */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001288 if (num_online_cpus() == 1) {
1289 put_online_cpus();
1290 return;
1291 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001292
1293 if (rcu_idle_cpu != -1)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001294 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001295
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001296 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001297
Josh Triplettc8e5b162007-05-08 00:33:20 -07001298 if (reader_tasks) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001299 for (i = 0; i < nrealreaders; i++)
1300 if (reader_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001301 set_cpus_allowed_ptr(reader_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001302 shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001303 }
1304
Josh Triplettc8e5b162007-05-08 00:33:20 -07001305 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001306 for (i = 0; i < nfakewriters; i++)
1307 if (fakewriter_tasks[i])
Mike Travisf70316d2008-04-04 18:11:06 -07001308 set_cpus_allowed_ptr(fakewriter_tasks[i],
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001309 shuffle_tmp_mask);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001310 }
1311
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001312 if (writer_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001313 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001314
1315 if (stats_task)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001316 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001317
1318 if (rcu_idle_cpu == -1)
1319 rcu_idle_cpu = num_online_cpus() - 1;
1320 else
1321 rcu_idle_cpu--;
1322
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01001323 put_online_cpus();
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001324}
1325
1326/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1327 * system to become idle at a time and cut off its timer ticks. This is meant
1328 * to test the support for such tickless idle CPU in RCU.
1329 */
1330static int
1331rcu_torture_shuffle(void *arg)
1332{
1333 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1334 do {
1335 schedule_timeout_interruptible(shuffle_interval * HZ);
1336 rcu_torture_shuffle_tasks();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001337 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1338 } while (!kthread_should_stop());
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001339 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1340 return 0;
1341}
1342
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001343/* Cause the rcutorture test to "stutter", starting and stopping all
1344 * threads periodically.
1345 */
1346static int
1347rcu_torture_stutter(void *arg)
1348{
1349 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1350 do {
1351 schedule_timeout_interruptible(stutter * HZ);
1352 stutter_pause_test = 1;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001353 if (!kthread_should_stop())
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001354 schedule_timeout_interruptible(stutter * HZ);
1355 stutter_pause_test = 0;
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001356 rcutorture_shutdown_absorb("rcu_torture_stutter");
1357 } while (!kthread_should_stop());
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001358 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1359 return 0;
1360}
1361
Paul E. McKenney95c38322006-03-24 03:15:58 -08001362static inline void
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001363rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001364{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001365 pr_alert("%s" TORTURE_FLAG
1366 "--- %s: nreaders=%d nfakewriters=%d "
1367 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1368 "shuffle_interval=%d stutter=%d irqreader=%d "
1369 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1370 "test_boost=%d/%d test_boost_interval=%d "
1371 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001372 "stall_cpu=%d stall_cpu_holdoff=%d "
1373 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001374 "onoff_interval=%d onoff_holdoff=%d\n",
1375 torture_type, tag, nrealreaders, nfakewriters,
1376 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1377 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1378 test_boost, cur_ops->can_boost,
1379 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001380 stall_cpu, stall_cpu_holdoff,
1381 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001382 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001383}
1384
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001385static struct notifier_block rcutorture_shutdown_nb = {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001386 .notifier_call = rcutorture_shutdown_notify,
1387};
1388
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001389static void rcutorture_booster_cleanup(int cpu)
1390{
1391 struct task_struct *t;
1392
1393 if (boost_tasks[cpu] == NULL)
1394 return;
1395 mutex_lock(&boost_mutex);
1396 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1397 t = boost_tasks[cpu];
1398 boost_tasks[cpu] = NULL;
1399 mutex_unlock(&boost_mutex);
1400
1401 /* This must be outside of the mutex, otherwise deadlock! */
1402 kthread_stop(t);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001403 boost_tasks[cpu] = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001404}
1405
1406static int rcutorture_booster_init(int cpu)
1407{
1408 int retval;
1409
1410 if (boost_tasks[cpu] != NULL)
1411 return 0; /* Already created, nothing more to do. */
1412
1413 /* Don't allow time recalculation while creating a new task. */
1414 mutex_lock(&boost_mutex);
1415 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001416 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1417 cpu_to_node(cpu),
1418 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001419 if (IS_ERR(boost_tasks[cpu])) {
1420 retval = PTR_ERR(boost_tasks[cpu]);
1421 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1422 n_rcu_torture_boost_ktrerror++;
1423 boost_tasks[cpu] = NULL;
1424 mutex_unlock(&boost_mutex);
1425 return retval;
1426 }
1427 kthread_bind(boost_tasks[cpu], cpu);
1428 wake_up_process(boost_tasks[cpu]);
1429 mutex_unlock(&boost_mutex);
1430 return 0;
1431}
1432
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001433/*
1434 * Cause the rcutorture test to shutdown the system after the test has
1435 * run for the time specified by the shutdown_secs module parameter.
1436 */
1437static int
1438rcu_torture_shutdown(void *arg)
1439{
1440 long delta;
1441 unsigned long jiffies_snap;
1442
1443 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1444 jiffies_snap = ACCESS_ONCE(jiffies);
1445 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1446 !kthread_should_stop()) {
1447 delta = shutdown_time - jiffies_snap;
1448 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001449 pr_alert("%s" TORTURE_FLAG
1450 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1451 torture_type, delta);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001452 schedule_timeout_interruptible(delta);
1453 jiffies_snap = ACCESS_ONCE(jiffies);
1454 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001455 if (kthread_should_stop()) {
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001456 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1457 return 0;
1458 }
1459
1460 /* OK, shut down the system. */
1461
1462 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1463 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1464 rcu_torture_cleanup(); /* Get the success/failure message. */
1465 kernel_power_off(); /* Shut down the system. */
1466 return 0;
1467}
1468
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001469#ifdef CONFIG_HOTPLUG_CPU
1470
1471/*
1472 * Execute random CPU-hotplug operations at the interval specified
1473 * by the onoff_interval.
1474 */
Heiko Carstens44100302011-12-27 15:04:26 +01001475static int __cpuinit
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001476rcu_torture_onoff(void *arg)
1477{
1478 int cpu;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001479 unsigned long delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001480 int maxcpu = -1;
1481 DEFINE_RCU_RANDOM(rand);
Paul E. McKenney48983262012-09-24 16:08:31 -07001482 int ret;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001483 unsigned long starttime;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001484
1485 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1486 for_each_online_cpu(cpu)
1487 maxcpu = cpu;
1488 WARN_ON(maxcpu < 0);
Paul E. McKenney9b9ec9b2012-01-17 14:36:51 -08001489 if (onoff_holdoff > 0) {
1490 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1491 schedule_timeout_interruptible(onoff_holdoff * HZ);
1492 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1493 }
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001494 while (!kthread_should_stop()) {
1495 cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001496 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001497 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001498 pr_alert("%s" TORTURE_FLAG
1499 "rcu_torture_onoff task: offlining %d\n",
1500 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001501 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001502 n_offline_attempts++;
Paul E. McKenney48983262012-09-24 16:08:31 -07001503 ret = cpu_down(cpu);
1504 if (ret) {
1505 if (verbose)
1506 pr_alert("%s" TORTURE_FLAG
1507 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1508 torture_type, cpu, ret);
1509 } else {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001510 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001511 pr_alert("%s" TORTURE_FLAG
1512 "rcu_torture_onoff task: offlined %d\n",
1513 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001514 n_offline_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001515 delta = jiffies - starttime;
1516 sum_offline += delta;
1517 if (min_offline < 0) {
1518 min_offline = delta;
1519 max_offline = delta;
1520 }
1521 if (min_offline > delta)
1522 min_offline = delta;
1523 if (max_offline < delta)
1524 max_offline = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001525 }
Paul E. McKenneyf2202422011-12-03 15:09:28 -08001526 } else if (cpu_is_hotpluggable(cpu)) {
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001527 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001528 pr_alert("%s" TORTURE_FLAG
1529 "rcu_torture_onoff task: onlining %d\n",
1530 torture_type, cpu);
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001531 starttime = jiffies;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001532 n_online_attempts++;
1533 if (cpu_up(cpu) == 0) {
1534 if (verbose)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001535 pr_alert("%s" TORTURE_FLAG
1536 "rcu_torture_onoff task: onlined %d\n",
1537 torture_type, cpu);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001538 n_online_successes++;
Paul E. McKenney13dbf912012-07-23 12:05:55 -07001539 delta = jiffies - starttime;
1540 sum_online += delta;
1541 if (min_online < 0) {
1542 min_online = delta;
1543 max_online = delta;
1544 }
1545 if (min_online > delta)
1546 min_online = delta;
1547 if (max_online < delta)
1548 max_online = delta;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001549 }
1550 }
1551 schedule_timeout_interruptible(onoff_interval * HZ);
1552 }
1553 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1554 return 0;
1555}
1556
Heiko Carstens44100302011-12-27 15:04:26 +01001557static int __cpuinit
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001558rcu_torture_onoff_init(void)
1559{
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001560 int ret;
1561
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001562 if (onoff_interval <= 0)
1563 return 0;
1564 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1565 if (IS_ERR(onoff_task)) {
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001566 ret = PTR_ERR(onoff_task);
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001567 onoff_task = NULL;
Julia Lawall3c1b1ce2012-02-02 07:52:54 -08001568 return ret;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001569 }
1570 return 0;
1571}
1572
1573static void rcu_torture_onoff_cleanup(void)
1574{
1575 if (onoff_task == NULL)
1576 return;
1577 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1578 kthread_stop(onoff_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001579 onoff_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001580}
1581
1582#else /* #ifdef CONFIG_HOTPLUG_CPU */
1583
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001584static int
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001585rcu_torture_onoff_init(void)
1586{
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001587 return 0;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001588}
1589
1590static void rcu_torture_onoff_cleanup(void)
1591{
1592}
1593
1594#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1595
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001596/*
1597 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1598 * induces a CPU stall for the time specified by stall_cpu.
1599 */
1600static int __cpuinit rcu_torture_stall(void *args)
1601{
1602 unsigned long stop_at;
1603
1604 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1605 if (stall_cpu_holdoff > 0) {
1606 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1607 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1608 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1609 }
1610 if (!kthread_should_stop()) {
1611 stop_at = get_seconds() + stall_cpu;
1612 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001613 pr_alert("rcu_torture_stall start.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001614 rcu_read_lock();
1615 preempt_disable();
1616 while (ULONG_CMP_LT(get_seconds(), stop_at))
1617 continue; /* Induce RCU CPU stall warning. */
1618 preempt_enable();
1619 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001620 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001621 }
1622 rcutorture_shutdown_absorb("rcu_torture_stall");
1623 while (!kthread_should_stop())
1624 schedule_timeout_interruptible(10 * HZ);
1625 return 0;
1626}
1627
1628/* Spawn CPU-stall kthread, if stall_cpu specified. */
1629static int __init rcu_torture_stall_init(void)
1630{
1631 int ret;
1632
1633 if (stall_cpu <= 0)
1634 return 0;
1635 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1636 if (IS_ERR(stall_task)) {
1637 ret = PTR_ERR(stall_task);
1638 stall_task = NULL;
1639 return ret;
1640 }
1641 return 0;
1642}
1643
1644/* Clean up after the CPU-stall kthread, if one was spawned. */
1645static void rcu_torture_stall_cleanup(void)
1646{
1647 if (stall_task == NULL)
1648 return;
1649 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1650 kthread_stop(stall_task);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001651 stall_task = NULL;
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001652}
1653
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001654/* Callback function for RCU barrier testing. */
1655void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1656{
1657 atomic_inc(&barrier_cbs_invoked);
1658}
1659
1660/* kthread function to register callbacks used to test RCU barriers. */
1661static int rcu_torture_barrier_cbs(void *arg)
1662{
1663 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001664 bool lastphase = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001665 struct rcu_head rcu;
1666
1667 init_rcu_head_on_stack(&rcu);
1668 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1669 set_user_nice(current, 19);
1670 do {
1671 wait_event(barrier_cbs_wq[myid],
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001672 barrier_phase != lastphase ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001673 kthread_should_stop() ||
1674 fullstop != FULLSTOP_DONTSTOP);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001675 lastphase = barrier_phase;
1676 smp_mb(); /* ensure barrier_phase load before ->call(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001677 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1678 break;
1679 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1680 if (atomic_dec_and_test(&barrier_cbs_count))
1681 wake_up(&barrier_wq);
1682 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1683 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1684 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1685 while (!kthread_should_stop())
1686 schedule_timeout_interruptible(1);
1687 cur_ops->cb_barrier();
1688 destroy_rcu_head_on_stack(&rcu);
1689 return 0;
1690}
1691
1692/* kthread function to drive and coordinate RCU barrier testing. */
1693static int rcu_torture_barrier(void *arg)
1694{
1695 int i;
1696
1697 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1698 do {
1699 atomic_set(&barrier_cbs_invoked, 0);
1700 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001701 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1702 barrier_phase = !barrier_phase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001703 for (i = 0; i < n_barrier_cbs; i++)
1704 wake_up(&barrier_cbs_wq[i]);
1705 wait_event(barrier_wq,
1706 atomic_read(&barrier_cbs_count) == 0 ||
1707 kthread_should_stop() ||
1708 fullstop != FULLSTOP_DONTSTOP);
1709 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1710 break;
1711 n_barrier_attempts++;
1712 cur_ops->cb_barrier();
1713 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1714 n_rcu_torture_barrier_error++;
1715 WARN_ON_ONCE(1);
1716 }
1717 n_barrier_successes++;
1718 schedule_timeout_interruptible(HZ / 10);
1719 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1720 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
Paul E. McKenney143aa672012-05-24 18:17:48 -07001721 rcutorture_shutdown_absorb("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001722 while (!kthread_should_stop())
1723 schedule_timeout_interruptible(1);
1724 return 0;
1725}
1726
1727/* Initialize RCU barrier testing. */
1728static int rcu_torture_barrier_init(void)
1729{
1730 int i;
1731 int ret;
1732
1733 if (n_barrier_cbs == 0)
1734 return 0;
1735 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001736 pr_alert("%s" TORTURE_FLAG
1737 " Call or barrier ops missing for %s,\n",
1738 torture_type, cur_ops->name);
1739 pr_alert("%s" TORTURE_FLAG
1740 " RCU barrier testing omitted from run.\n",
1741 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001742 return 0;
1743 }
1744 atomic_set(&barrier_cbs_count, 0);
1745 atomic_set(&barrier_cbs_invoked, 0);
1746 barrier_cbs_tasks =
1747 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1748 GFP_KERNEL);
1749 barrier_cbs_wq =
1750 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1751 GFP_KERNEL);
1752 if (barrier_cbs_tasks == NULL || barrier_cbs_wq == 0)
1753 return -ENOMEM;
1754 for (i = 0; i < n_barrier_cbs; i++) {
1755 init_waitqueue_head(&barrier_cbs_wq[i]);
1756 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
Lai Jiangshan9059c942012-03-19 16:12:14 +08001757 (void *)(long)i,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001758 "rcu_torture_barrier_cbs");
1759 if (IS_ERR(barrier_cbs_tasks[i])) {
1760 ret = PTR_ERR(barrier_cbs_tasks[i]);
1761 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1762 barrier_cbs_tasks[i] = NULL;
1763 return ret;
1764 }
1765 }
1766 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1767 "rcu_torture_barrier");
1768 if (IS_ERR(barrier_task)) {
1769 ret = PTR_ERR(barrier_task);
1770 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1771 barrier_task = NULL;
1772 }
1773 return 0;
1774}
1775
1776/* Clean up after RCU barrier testing. */
1777static void rcu_torture_barrier_cleanup(void)
1778{
1779 int i;
1780
1781 if (barrier_task != NULL) {
1782 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1783 kthread_stop(barrier_task);
1784 barrier_task = NULL;
1785 }
1786 if (barrier_cbs_tasks != NULL) {
1787 for (i = 0; i < n_barrier_cbs; i++) {
1788 if (barrier_cbs_tasks[i] != NULL) {
1789 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1790 kthread_stop(barrier_cbs_tasks[i]);
1791 barrier_cbs_tasks[i] = NULL;
1792 }
1793 }
1794 kfree(barrier_cbs_tasks);
1795 barrier_cbs_tasks = NULL;
1796 }
1797 if (barrier_cbs_wq != NULL) {
1798 kfree(barrier_cbs_wq);
1799 barrier_cbs_wq = NULL;
1800 }
1801}
1802
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001803static int rcutorture_cpu_notify(struct notifier_block *self,
1804 unsigned long action, void *hcpu)
1805{
1806 long cpu = (long)hcpu;
1807
1808 switch (action) {
1809 case CPU_ONLINE:
1810 case CPU_DOWN_FAILED:
1811 (void)rcutorture_booster_init(cpu);
1812 break;
1813 case CPU_DOWN_PREPARE:
1814 rcutorture_booster_cleanup(cpu);
1815 break;
1816 default:
1817 break;
1818 }
1819 return NOTIFY_OK;
1820}
1821
1822static struct notifier_block rcutorture_cpu_nb = {
1823 .notifier_call = rcutorture_cpu_notify,
1824};
1825
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001826static void
1827rcu_torture_cleanup(void)
1828{
1829 int i;
1830
Paul E. McKenney343e9092008-12-15 16:13:07 -08001831 mutex_lock(&fullstop_mutex);
Paul E. McKenney4a298652011-04-03 21:33:51 -07001832 rcutorture_record_test_transition();
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001833 if (fullstop == FULLSTOP_SHUTDOWN) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001834 pr_warn(/* but going down anyway, so... */
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001835 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001836 mutex_unlock(&fullstop_mutex);
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001837 schedule_timeout_uninterruptible(10);
Paul E. McKenney343e9092008-12-15 16:13:07 -08001838 if (cur_ops->cb_barrier != NULL)
1839 cur_ops->cb_barrier();
1840 return;
1841 }
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001842 fullstop = FULLSTOP_RMMOD;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001843 mutex_unlock(&fullstop_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001844 unregister_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001845 rcu_torture_barrier_cleanup();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001846 rcu_torture_stall_cleanup();
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001847 if (stutter_task) {
1848 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1849 kthread_stop(stutter_task);
1850 }
1851 stutter_task = NULL;
Josh Triplettc8e5b162007-05-08 00:33:20 -07001852 if (shuffler_task) {
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001853 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1854 kthread_stop(shuffler_task);
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001855 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001856 }
1857 shuffler_task = NULL;
1858
Josh Triplettc8e5b162007-05-08 00:33:20 -07001859 if (writer_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001860 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1861 kthread_stop(writer_task);
1862 }
1863 writer_task = NULL;
1864
Josh Triplettc8e5b162007-05-08 00:33:20 -07001865 if (reader_tasks) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001866 for (i = 0; i < nrealreaders; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001867 if (reader_tasks[i]) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001868 VERBOSE_PRINTK_STRING(
1869 "Stopping rcu_torture_reader task");
1870 kthread_stop(reader_tasks[i]);
1871 }
1872 reader_tasks[i] = NULL;
1873 }
1874 kfree(reader_tasks);
1875 reader_tasks = NULL;
1876 }
1877 rcu_torture_current = NULL;
1878
Josh Triplettc8e5b162007-05-08 00:33:20 -07001879 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001880 for (i = 0; i < nfakewriters; i++) {
Josh Triplettc8e5b162007-05-08 00:33:20 -07001881 if (fakewriter_tasks[i]) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001882 VERBOSE_PRINTK_STRING(
1883 "Stopping rcu_torture_fakewriter task");
1884 kthread_stop(fakewriter_tasks[i]);
1885 }
1886 fakewriter_tasks[i] = NULL;
1887 }
1888 kfree(fakewriter_tasks);
1889 fakewriter_tasks = NULL;
1890 }
1891
Josh Triplettc8e5b162007-05-08 00:33:20 -07001892 if (stats_task) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001893 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1894 kthread_stop(stats_task);
1895 }
1896 stats_task = NULL;
1897
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001898 if (fqs_task) {
1899 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1900 kthread_stop(fqs_task);
1901 }
1902 fqs_task = NULL;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001903 if ((test_boost == 1 && cur_ops->can_boost) ||
1904 test_boost == 2) {
1905 unregister_cpu_notifier(&rcutorture_cpu_nb);
1906 for_each_possible_cpu(i)
1907 rcutorture_booster_cleanup(i);
1908 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001909 if (shutdown_task != NULL) {
1910 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1911 kthread_stop(shutdown_task);
1912 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001913 shutdown_task = NULL;
Paul E. McKenneyb58bdcc2011-11-16 17:48:21 -08001914 rcu_torture_onoff_cleanup();
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001915
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001916 /* Wait for all RCU callbacks to fire. */
Paul E. McKenney23269742008-05-12 21:21:05 +02001917
1918 if (cur_ops->cb_barrier != NULL)
1919 cur_ops->cb_barrier();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001920
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001921 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001922
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001923 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001924 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney091541b2012-01-10 12:51:14 -08001925 else if (n_online_successes != n_online_attempts ||
1926 n_offline_successes != n_offline_attempts)
1927 rcu_torture_print_module_parms(cur_ops,
1928 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001929 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001930 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001931}
1932
Josh Triplett6f8bc502007-05-08 00:25:24 -07001933static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001934rcu_torture_init(void)
1935{
1936 int i;
1937 int cpu;
1938 int firsterr = 0;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001939 int retval;
Josh Triplettade5fb82007-05-08 00:33:22 -07001940 static struct rcu_torture_ops *torture_ops[] =
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001941 { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -07001942 &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
Paul E. McKenney751a68b2012-05-07 13:44:44 -07001943 &srcu_ops, &srcu_sync_ops, &srcu_expedited_ops,
1944 &srcu_raw_ops, &srcu_raw_sync_ops,
Paul E. McKenney804bb832009-10-25 19:03:52 -07001945 &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001946
Paul E. McKenney343e9092008-12-15 16:13:07 -08001947 mutex_lock(&fullstop_mutex);
1948
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001949 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001950 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001951 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001952 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001953 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001954 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001955 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001956 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1957 torture_type);
1958 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001959 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001960 pr_alert(" %s", torture_ops[i]->name);
1961 pr_alert("\n");
Paul E. McKenney343e9092008-12-15 16:13:07 -08001962 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya71fca52009-09-18 10:28:19 -07001963 return -EINVAL;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001964 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001965 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001966 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001967 fqs_duration = 0;
1968 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001969 if (cur_ops->init)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001970 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1971
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001972 if (nreaders >= 0)
1973 nrealreaders = nreaders;
1974 else
1975 nrealreaders = 2 * num_online_cpus();
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001976 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneyc9d557c2009-01-07 14:33:30 -08001977 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001978
1979 /* Set up the freelist. */
1980
1981 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001982 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001983 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001984 list_add_tail(&rcu_tortures[i].rtort_free,
1985 &rcu_torture_freelist);
1986 }
1987
1988 /* Initialize the statistics so that each run gets its own numbers. */
1989
1990 rcu_torture_current = NULL;
1991 rcu_torture_current_version = 0;
1992 atomic_set(&n_rcu_torture_alloc, 0);
1993 atomic_set(&n_rcu_torture_alloc_fail, 0);
1994 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001995 atomic_set(&n_rcu_torture_mberror, 0);
1996 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001997 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001998 n_rcu_torture_boost_ktrerror = 0;
1999 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002000 n_rcu_torture_boost_failure = 0;
2001 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002002 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
2003 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002004 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002005 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
2006 per_cpu(rcu_torture_count, cpu)[i] = 0;
2007 per_cpu(rcu_torture_batch, cpu)[i] = 0;
2008 }
2009 }
2010
2011 /* Start up the kthreads. */
2012
2013 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
Paul E. McKenney60f53782012-08-25 15:27:40 -07002014 writer_task = kthread_create(rcu_torture_writer, NULL,
2015 "rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002016 if (IS_ERR(writer_task)) {
2017 firsterr = PTR_ERR(writer_task);
2018 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
2019 writer_task = NULL;
2020 goto unwind;
2021 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002022 wake_up_process(writer_task);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002023 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002024 GFP_KERNEL);
Josh Triplettb772e1d2006-10-04 02:17:13 -07002025 if (fakewriter_tasks == NULL) {
2026 VERBOSE_PRINTK_ERRSTRING("out of memory");
2027 firsterr = -ENOMEM;
2028 goto unwind;
2029 }
2030 for (i = 0; i < nfakewriters; i++) {
2031 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2032 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -07002033 "rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07002034 if (IS_ERR(fakewriter_tasks[i])) {
2035 firsterr = PTR_ERR(fakewriter_tasks[i]);
2036 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2037 fakewriter_tasks[i] = NULL;
2038 goto unwind;
2039 }
2040 }
Josh Triplett2860aab2006-10-04 02:17:11 -07002041 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002042 GFP_KERNEL);
2043 if (reader_tasks == NULL) {
2044 VERBOSE_PRINTK_ERRSTRING("out of memory");
2045 firsterr = -ENOMEM;
2046 goto unwind;
2047 }
2048 for (i = 0; i < nrealreaders; i++) {
2049 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2050 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2051 "rcu_torture_reader");
2052 if (IS_ERR(reader_tasks[i])) {
2053 firsterr = PTR_ERR(reader_tasks[i]);
2054 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2055 reader_tasks[i] = NULL;
2056 goto unwind;
2057 }
2058 }
2059 if (stat_interval > 0) {
2060 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2061 stats_task = kthread_run(rcu_torture_stats, NULL,
2062 "rcu_torture_stats");
2063 if (IS_ERR(stats_task)) {
2064 firsterr = PTR_ERR(stats_task);
2065 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2066 stats_task = NULL;
2067 goto unwind;
2068 }
2069 }
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002070 if (test_no_idle_hz) {
2071 rcu_idle_cpu = num_online_cpus() - 1;
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002072
2073 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2074 firsterr = -ENOMEM;
2075 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2076 goto unwind;
2077 }
2078
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002079 /* Create the shuffler thread */
2080 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2081 "rcu_torture_shuffle");
2082 if (IS_ERR(shuffler_task)) {
Rusty Russell73d0a4b2009-03-30 22:05:16 -06002083 free_cpumask_var(shuffle_tmp_mask);
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08002084 firsterr = PTR_ERR(shuffler_task);
2085 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2086 shuffler_task = NULL;
2087 goto unwind;
2088 }
2089 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07002090 if (stutter < 0)
2091 stutter = 0;
2092 if (stutter) {
2093 /* Create the stutter thread */
2094 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2095 "rcu_torture_stutter");
2096 if (IS_ERR(stutter_task)) {
2097 firsterr = PTR_ERR(stutter_task);
2098 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2099 stutter_task = NULL;
2100 goto unwind;
2101 }
2102 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08002103 if (fqs_duration < 0)
2104 fqs_duration = 0;
2105 if (fqs_duration) {
2106 /* Create the stutter thread */
2107 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2108 "rcu_torture_fqs");
2109 if (IS_ERR(fqs_task)) {
2110 firsterr = PTR_ERR(fqs_task);
2111 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2112 fqs_task = NULL;
2113 goto unwind;
2114 }
2115 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002116 if (test_boost_interval < 1)
2117 test_boost_interval = 1;
2118 if (test_boost_duration < 2)
2119 test_boost_duration = 2;
2120 if ((test_boost == 1 && cur_ops->can_boost) ||
2121 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002122
2123 boost_starttime = jiffies + test_boost_interval * HZ;
2124 register_cpu_notifier(&rcutorture_cpu_nb);
2125 for_each_possible_cpu(i) {
2126 if (cpu_is_offline(i))
2127 continue; /* Heuristic: CPU can go offline. */
2128 retval = rcutorture_booster_init(i);
2129 if (retval < 0) {
2130 firsterr = retval;
2131 goto unwind;
2132 }
2133 }
2134 }
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002135 if (shutdown_secs > 0) {
2136 shutdown_time = jiffies + shutdown_secs * HZ;
Paul E. McKenney60f53782012-08-25 15:27:40 -07002137 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2138 "rcu_torture_shutdown");
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002139 if (IS_ERR(shutdown_task)) {
2140 firsterr = PTR_ERR(shutdown_task);
2141 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2142 shutdown_task = NULL;
2143 goto unwind;
2144 }
Paul E. McKenney60f53782012-08-25 15:27:40 -07002145 wake_up_process(shutdown_task);
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07002146 }
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002147 i = rcu_torture_onoff_init();
2148 if (i != 0) {
2149 firsterr = i;
2150 goto unwind;
2151 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07002152 register_reboot_notifier(&rcutorture_shutdown_nb);
Paul E. McKenney37e377d2012-02-17 22:12:18 -08002153 i = rcu_torture_stall_init();
2154 if (i != 0) {
2155 firsterr = i;
2156 goto unwind;
2157 }
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08002158 retval = rcu_torture_barrier_init();
2159 if (retval != 0) {
2160 firsterr = retval;
2161 goto unwind;
2162 }
Paul E. McKenney4a298652011-04-03 21:33:51 -07002163 rcutorture_record_test_transition();
Paul E. McKenney343e9092008-12-15 16:13:07 -08002164 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002165 return 0;
2166
2167unwind:
Paul E. McKenney343e9092008-12-15 16:13:07 -08002168 mutex_unlock(&fullstop_mutex);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002169 rcu_torture_cleanup();
2170 return firsterr;
2171}
2172
2173module_init(rcu_torture_init);
2174module_exit(rcu_torture_cleanup);