blob: d5a5465d2507dd086af64b5458e83137f9682baa [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
Paul E. McKenneyf5604f62014-02-26 06:38:59 -080015 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
Paul E. McKenneya241ec62005-10-30 15:03:12 -080017 *
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>
Josh Triplette0198b292014-07-30 16:08:42 -070021 * Josh Triplett <josh@joshtriplett.org>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080022 *
23 * See also: Documentation/RCU/torture.txt
24 */
Paul E. McKenney60500032018-05-15 12:25:05 -070025
26#define pr_fmt(fmt) fmt
27
Paul E. McKenneya241ec62005-10-30 15:03:12 -080028#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/kthread.h>
33#include <linux/err.h>
34#include <linux/spinlock.h>
35#include <linux/smp.h>
36#include <linux/rcupdate.h>
37#include <linux/interrupt.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010039#include <uapi/linux/sched/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080041#include <linux/bitops.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080042#include <linux/completion.h>
43#include <linux/moduleparam.h>
44#include <linux/percpu.h>
45#include <linux/notifier.h>
Paul E. McKenney343e9092008-12-15 16:13:07 -080046#include <linux/reboot.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070047#include <linux/freezer.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080048#include <linux/cpu.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080049#include <linux/delay.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080050#include <linux/stat.h>
Paul E. McKenneyb2896d22006-10-04 02:17:03 -070051#include <linux/srcu.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070052#include <linux/slab.h>
Paul E. McKenney52494532012-11-14 16:26:40 -080053#include <linux/trace_clock.h>
Harvey Harrisonf07767f2008-10-20 10:23:38 -070054#include <asm/byteorder.h>
Paul E. McKenney51b11302014-01-27 11:49:39 -080055#include <linux/torture.h>
Paul E. McKenney38706bc2014-08-18 21:12:17 -070056#include <linux/vmalloc.h>
Paul E. McKenney0032f4e2017-08-30 10:40:17 -070057#include <linux/sched/debug.h>
Paul E. McKenneya241ec62005-10-30 15:03:12 -080058
Paul E. McKenney25c36322017-05-03 09:51:55 -070059#include "rcu.h"
60
Paul E. McKenneya241ec62005-10-30 15:03:12 -080061MODULE_LICENSE("GPL");
Josh Triplette0198b292014-07-30 16:08:42 -070062MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
Paul E. McKenneya241ec62005-10-30 15:03:12 -080063
Paul E. McKenney4102ada2013-10-08 20:23:47 -070064
Paul E. McKenney38706bc2014-08-18 21:12:17 -070065torture_param(int, cbflood_inter_holdoff, HZ,
66 "Holdoff between floods (jiffies)");
67torture_param(int, cbflood_intra_holdoff, 1,
68 "Holdoff between bursts (jiffies)");
69torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
70torture_param(int, cbflood_n_per_burst, 20000,
71 "# callbacks per burst in flood");
Paul E. McKenney9e250222014-01-27 16:27:00 -080072torture_param(int, fqs_duration, 0,
73 "Duration of fqs bursts (us), 0 to disable");
74torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
75torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -070076torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
Paul E. McKenney9e250222014-01-27 16:27:00 -080077torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
78torture_param(bool, gp_normal, false,
79 "Use normal (non-expedited) GP wait primitives");
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -070080torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
Paul E. McKenney9e250222014-01-27 16:27:00 -080081torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
82torture_param(int, n_barrier_cbs, 0,
83 "# of callbacks/kthreads for barrier testing");
84torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
85torture_param(int, nreaders, -1, "Number of RCU reader threads");
86torture_param(int, object_debug, 0,
87 "Enable debug-object double call_rcu() testing");
88torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
89torture_param(int, onoff_interval, 0,
Paul E. McKenney028be122018-05-08 09:20:34 -070090 "Time between CPU hotplugs (jiffies), 0=disable");
Paul E. McKenney9e250222014-01-27 16:27:00 -080091torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
92torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
93torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
94torture_param(int, stall_cpu_holdoff, 10,
95 "Time to wait before starting stall (s).");
Paul E. McKenney2b1516e2017-08-18 16:11:37 -070096torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
Paul E. McKenney9e250222014-01-27 16:27:00 -080097torture_param(int, stat_interval, 60,
98 "Number of seconds between stats printk()s");
99torture_param(int, stutter, 5, "Number of seconds to run/halt test");
100torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
101torture_param(int, test_boost_duration, 4,
102 "Duration of each boost test, seconds.");
103torture_param(int, test_boost_interval, 7,
104 "Interval between boost tests, seconds.");
105torture_param(bool, test_no_idle_hz, true,
106 "Test support for tickless idle CPUs");
Paul E. McKenney90127d62018-05-09 10:29:18 -0700107torture_param(int, verbose, 1,
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800108 "Enable verbose debugging printk()s");
Paul E. McKenney9e250222014-01-27 16:27:00 -0800109
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800110static char *torture_type = "rcu";
Josh Triplettd6ad6712007-03-06 01:42:13 -0800111module_param(torture_type, charp, 0444);
Paul E. McKenneyd10453e2013-06-13 15:12:24 -0700112MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700113
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800114static int nrealreaders;
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700115static int ncbflooders;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800116static struct task_struct *writer_task;
Josh Triplettb772e1d2006-10-04 02:17:13 -0700117static struct task_struct **fakewriter_tasks;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800118static struct task_struct **reader_tasks;
119static struct task_struct *stats_task;
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700120static struct task_struct **cbflood_task;
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800121static struct task_struct *fqs_task;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700122static struct task_struct *boost_tasks[NR_CPUS];
Paul E. McKenneyc13f3752012-01-20 15:36:33 -0800123static struct task_struct *stall_task;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800124static struct task_struct **barrier_cbs_tasks;
125static struct task_struct *barrier_task;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800126
127#define RCU_TORTURE_PIPE_LEN 10
128
129struct rcu_torture {
130 struct rcu_head rtort_rcu;
131 int rtort_pipe_count;
132 struct list_head rtort_free;
Paul E. McKenney996417d2005-11-18 01:10:50 -0800133 int rtort_mbtest;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800134};
135
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800136static LIST_HEAD(rcu_torture_freelist);
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700137static struct rcu_torture __rcu *rcu_torture_current;
Paul E. McKenney4a298652011-04-03 21:33:51 -0700138static unsigned long rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800139static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140static DEFINE_SPINLOCK(rcu_torture_lock);
Paul E. McKenney67522be2016-03-01 08:52:19 -0800141static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
142static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800143static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700144static atomic_t n_rcu_torture_alloc;
145static atomic_t n_rcu_torture_alloc_fail;
146static atomic_t n_rcu_torture_free;
147static atomic_t n_rcu_torture_mberror;
148static atomic_t n_rcu_torture_error;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800149static long n_rcu_torture_barrier_error;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700150static long n_rcu_torture_boost_ktrerror;
151static long n_rcu_torture_boost_rterror;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700152static long n_rcu_torture_boost_failure;
153static long n_rcu_torture_boosts;
Paul E. McKenney8da9a592018-05-22 11:17:51 -0700154static atomic_long_t n_rcu_torture_timers;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800155static long n_barrier_attempts;
156static long n_barrier_successes;
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700157static atomic_long_t n_cbfloods;
Josh Triplette3033732006-10-04 02:17:14 -0700158static struct list_head rcu_torture_removed;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800159
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800160static int rcu_torture_writer_state;
161#define RTWS_FIXED_DELAY 0
162#define RTWS_DELAY 1
163#define RTWS_REPLACE 2
164#define RTWS_DEF_FREE 3
165#define RTWS_EXP_SYNC 4
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700166#define RTWS_COND_GET 5
167#define RTWS_COND_SYNC 6
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700168#define RTWS_SYNC 7
169#define RTWS_STUTTER 8
170#define RTWS_STOPPING 9
Paul E. McKenney18aff332015-11-17 13:35:28 -0800171static const char * const rcu_torture_writer_state_names[] = {
172 "RTWS_FIXED_DELAY",
173 "RTWS_DELAY",
174 "RTWS_REPLACE",
175 "RTWS_DEF_FREE",
176 "RTWS_EXP_SYNC",
177 "RTWS_COND_GET",
178 "RTWS_COND_SYNC",
179 "RTWS_SYNC",
180 "RTWS_STUTTER",
181 "RTWS_STOPPING",
182};
183
184static const char *rcu_torture_writer_state_getname(void)
185{
186 unsigned int i = READ_ONCE(rcu_torture_writer_state);
187
188 if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
189 return "???";
190 return rcu_torture_writer_state_names[i];
191}
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800192
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700193#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700194#define rcu_can_boost() 1
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700195#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700196#define rcu_can_boost() 0
Paul E. McKenney3acf4a92011-04-17 23:45:23 -0700197#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700198
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500199#ifdef CONFIG_RCU_TRACE
200static u64 notrace rcu_trace_clock_local(void)
201{
202 u64 ts = trace_clock_local();
Paul E. McKenneya3b7b6c2017-06-23 16:07:17 -0700203
204 (void)do_div(ts, NSEC_PER_USEC);
Steven Rostedte4aa0da2013-02-04 13:36:13 -0500205 return ts;
206}
207#else /* #ifdef CONFIG_RCU_TRACE */
208static u64 notrace rcu_trace_clock_local(void)
209{
210 return 0ULL;
211}
212#endif /* #else #ifdef CONFIG_RCU_TRACE */
213
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700214static unsigned long boost_starttime; /* jiffies of next boost test start. */
Pranith Kumar58ade2d2014-06-11 16:39:43 -0400215static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700216 /* and boost task create/destroy. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800217static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -0700218static bool barrier_phase; /* Test phase. */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800219static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
220static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
221static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700222
Paul E. McKenney343e9092008-12-15 16:13:07 -0800223/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800224 * Allocate an element from the rcu_tortures pool.
225 */
Adrian Bunk97a41e22006-01-08 01:02:17 -0800226static struct rcu_torture *
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800227rcu_torture_alloc(void)
228{
229 struct list_head *p;
230
Ingo Molnaradac1662006-01-25 19:50:12 +0100231 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800232 if (list_empty(&rcu_torture_freelist)) {
233 atomic_inc(&n_rcu_torture_alloc_fail);
Ingo Molnaradac1662006-01-25 19:50:12 +0100234 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800235 return NULL;
236 }
237 atomic_inc(&n_rcu_torture_alloc);
238 p = rcu_torture_freelist.next;
239 list_del_init(p);
Ingo Molnaradac1662006-01-25 19:50:12 +0100240 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800241 return container_of(p, struct rcu_torture, rtort_free);
242}
243
244/*
245 * Free an element to the rcu_tortures pool.
246 */
247static void
248rcu_torture_free(struct rcu_torture *p)
249{
250 atomic_inc(&n_rcu_torture_free);
Ingo Molnaradac1662006-01-25 19:50:12 +0100251 spin_lock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800252 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
Ingo Molnaradac1662006-01-25 19:50:12 +0100253 spin_unlock_bh(&rcu_torture_lock);
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800254}
255
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800256/*
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700257 * Operations vector for selecting different types of tests.
258 */
259
260struct rcu_torture_ops {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800261 int ttype;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700262 void (*init)(void);
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700263 void (*cleanup)(void);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700264 int (*readlock)(void);
Paul E. McKenney51b11302014-01-27 11:49:39 -0800265 void (*read_delay)(struct torture_random_state *rrsp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700266 void (*readunlock)(int idx);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700267 unsigned long (*get_gp_seq)(void);
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700268 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700269 void (*deferred_free)(struct rcu_torture *p);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700270 void (*sync)(void);
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700271 void (*exp_sync)(void);
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700272 unsigned long (*get_state)(void);
273 void (*cond_sync)(unsigned long oldstate);
Boqun Fengdb3e8db2015-07-29 13:29:39 +0800274 call_rcu_func_t call;
Paul E. McKenney23269742008-05-12 21:21:05 +0200275 void (*cb_barrier)(void);
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800276 void (*fqs)(void);
Joe Percheseea203f2014-07-14 09:16:15 -0400277 void (*stats)(void);
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700278 int irq_capable;
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700279 int can_boost;
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -0400280 const char *name;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700281};
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700282
283static struct rcu_torture_ops *cur_ops;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700284
285/*
286 * Definitions for rcu torture testing.
287 */
288
Josh Tripletta49a4af2006-09-29 01:59:30 -0700289static int rcu_torture_read_lock(void) __acquires(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700290{
291 rcu_read_lock();
292 return 0;
293}
294
Paul E. McKenney51b11302014-01-27 11:49:39 -0800295static void rcu_read_delay(struct torture_random_state *rrsp)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700296{
Paul E. McKenneyd0af39e2016-10-10 18:26:04 -0700297 unsigned long started;
298 unsigned long completed;
Josh Triplettb8d57a72009-09-08 15:54:35 -0700299 const unsigned long shortdelay_us = 200;
300 const unsigned long longdelay_ms = 50;
Paul E. McKenneyd0af39e2016-10-10 18:26:04 -0700301 unsigned long long ts;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700302
Josh Triplettb8d57a72009-09-08 15:54:35 -0700303 /* We want a short delay sometimes to make a reader delay the grace
304 * period, and we want a long delay occasionally to trigger
305 * force_quiescent_state. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700306
Paul E. McKenneyd0af39e2016-10-10 18:26:04 -0700307 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700308 started = cur_ops->get_gp_seq();
Paul E. McKenneyd0af39e2016-10-10 18:26:04 -0700309 ts = rcu_trace_clock_local();
Josh Triplettb8d57a72009-09-08 15:54:35 -0700310 mdelay(longdelay_ms);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700311 completed = cur_ops->get_gp_seq();
Paul E. McKenneyd0af39e2016-10-10 18:26:04 -0700312 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
313 started, completed);
314 }
Paul E. McKenney51b11302014-01-27 11:49:39 -0800315 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
Josh Triplettb8d57a72009-09-08 15:54:35 -0700316 udelay(shortdelay_us);
Paul E. McKenney51b11302014-01-27 11:49:39 -0800317 if (!preempt_count() &&
Paul E. McKenneye8302732017-10-16 11:23:42 -0700318 !(torture_random(rrsp) % (nrealreaders * 500)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700319 torture_preempt_schedule(); /* QS only if preemptible. */
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700320}
321
Josh Tripletta49a4af2006-09-29 01:59:30 -0700322static void rcu_torture_read_unlock(int idx) __releases(RCU)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700323{
324 rcu_read_unlock();
325}
326
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700327/*
328 * Update callback in the pipe. This should be invoked after a grace period.
329 */
330static bool
331rcu_torture_pipe_update_one(struct rcu_torture *rp)
332{
333 int i;
334
335 i = rp->rtort_pipe_count;
336 if (i > RCU_TORTURE_PIPE_LEN)
337 i = RCU_TORTURE_PIPE_LEN;
338 atomic_inc(&rcu_torture_wcount[i]);
339 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
340 rp->rtort_mbtest = 0;
341 return true;
342 }
343 return false;
344}
345
346/*
347 * Update all callbacks in the pipe. Suitable for synchronous grace-period
348 * primitives.
349 */
350static void
351rcu_torture_pipe_update(struct rcu_torture *old_rp)
352{
353 struct rcu_torture *rp;
354 struct rcu_torture *rp1;
355
356 if (old_rp)
357 list_add(&old_rp->rtort_free, &rcu_torture_removed);
358 list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
359 if (rcu_torture_pipe_update_one(rp)) {
360 list_del(&rp->rtort_free);
361 rcu_torture_free(rp);
362 }
363 }
364}
365
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700366static void
367rcu_torture_cb(struct rcu_head *p)
368{
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700369 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
370
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800371 if (torture_must_stop_irq()) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700372 /* Test is ending, just drop callbacks on the floor. */
373 /* The next initialization will pick up the pieces. */
374 return;
375 }
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700376 if (rcu_torture_pipe_update_one(rp))
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700377 rcu_torture_free(rp);
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700378 else
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700379 cur_ops->deferred_free(rp);
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700380}
381
Paul E. McKenney6b80da42014-11-21 14:19:26 -0800382static unsigned long rcu_no_completed(void)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800383{
384 return 0;
385}
386
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700387static void rcu_torture_deferred_free(struct rcu_torture *p)
388{
389 call_rcu(&p->rtort_rcu, rcu_torture_cb);
390}
391
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700392static void rcu_sync_torture_init(void)
393{
394 INIT_LIST_HEAD(&rcu_torture_removed);
395}
396
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700397static struct rcu_torture_ops rcu_ops = {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800398 .ttype = RCU_FLAVOR,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700399 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700400 .readlock = rcu_torture_read_lock,
401 .read_delay = rcu_read_delay,
402 .readunlock = rcu_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700403 .get_gp_seq = rcu_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700404 .gp_diff = rcu_seq_diff,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700405 .deferred_free = rcu_torture_deferred_free,
406 .sync = synchronize_rcu,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700407 .exp_sync = synchronize_rcu_expedited,
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700408 .get_state = get_state_synchronize_rcu,
409 .cond_sync = cond_synchronize_rcu,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800410 .call = call_rcu,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700411 .cb_barrier = rcu_barrier,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800412 .fqs = rcu_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700413 .stats = NULL,
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700414 .irq_capable = 1,
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700415 .can_boost = rcu_can_boost(),
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700416 .name = "rcu"
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700417};
418
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700419/*
420 * Definitions for rcu_bh torture testing.
421 */
422
Josh Tripletta49a4af2006-09-29 01:59:30 -0700423static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700424{
425 rcu_read_lock_bh();
426 return 0;
427}
428
Josh Tripletta49a4af2006-09-29 01:59:30 -0700429static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700430{
431 rcu_read_unlock_bh();
432}
433
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700434static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
435{
436 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
437}
438
439static struct rcu_torture_ops rcu_bh_ops = {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800440 .ttype = RCU_BH_FLAVOR,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700441 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700442 .readlock = rcu_bh_torture_read_lock,
443 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
444 .readunlock = rcu_bh_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700445 .get_gp_seq = rcu_bh_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700446 .gp_diff = rcu_seq_diff,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700447 .deferred_free = rcu_bh_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700448 .sync = synchronize_rcu_bh,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700449 .exp_sync = synchronize_rcu_bh_expedited,
Paul E. McKenneyfae4b542012-02-20 17:51:45 -0800450 .call = call_rcu_bh,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700451 .cb_barrier = rcu_barrier_bh,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800452 .fqs = rcu_bh_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700453 .stats = NULL,
454 .irq_capable = 1,
455 .name = "rcu_bh"
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700456};
457
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700458/*
Paul E. McKenneyff20e252014-02-06 08:45:56 -0800459 * Don't even think about trying any of these in real life!!!
460 * The names includes "busted", and they really means it!
461 * The only purpose of these functions is to provide a buggy RCU
462 * implementation to make sure that rcutorture correctly emits
463 * buggy-RCU error messages.
464 */
465static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
466{
467 /* This is a deliberate bug for testing purposes only! */
468 rcu_torture_cb(&p->rtort_rcu);
469}
470
471static void synchronize_rcu_busted(void)
472{
473 /* This is a deliberate bug for testing purposes only! */
474}
475
476static void
Boqun Fengb6a4ae72015-07-29 13:29:38 +0800477call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
Paul E. McKenneyff20e252014-02-06 08:45:56 -0800478{
479 /* This is a deliberate bug for testing purposes only! */
480 func(head);
481}
482
483static struct rcu_torture_ops rcu_busted_ops = {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800484 .ttype = INVALID_RCU_FLAVOR,
Paul E. McKenneyff20e252014-02-06 08:45:56 -0800485 .init = rcu_sync_torture_init,
486 .readlock = rcu_torture_read_lock,
487 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
488 .readunlock = rcu_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700489 .get_gp_seq = rcu_no_completed,
Paul E. McKenneyff20e252014-02-06 08:45:56 -0800490 .deferred_free = rcu_busted_torture_deferred_free,
491 .sync = synchronize_rcu_busted,
492 .exp_sync = synchronize_rcu_busted,
493 .call = call_rcu_busted,
494 .cb_barrier = NULL,
495 .fqs = NULL,
496 .stats = NULL,
497 .irq_capable = 1,
Paul E. McKenneyb3c98312017-06-06 16:39:00 -0700498 .name = "busted"
Paul E. McKenneyff20e252014-02-06 08:45:56 -0800499};
500
501/*
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700502 * Definitions for srcu torture testing.
503 */
504
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800505DEFINE_STATIC_SRCU(srcu_ctl);
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700506static struct srcu_struct srcu_ctld;
507static struct srcu_struct *srcu_ctlp = &srcu_ctl;
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700508
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700509static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700510{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700511 return srcu_read_lock(srcu_ctlp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700512}
513
Paul E. McKenney51b11302014-01-27 11:49:39 -0800514static void srcu_read_delay(struct torture_random_state *rrsp)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700515{
516 long delay;
517 const long uspertick = 1000000 / HZ;
518 const long longdelay = 10;
519
520 /* We want there to be long-running readers, but not all the time. */
521
Paul E. McKenney51b11302014-01-27 11:49:39 -0800522 delay = torture_random(rrsp) %
523 (nrealreaders * 2 * longdelay * uspertick);
Paul E. McKenney5e741fa2017-06-06 12:52:44 -0700524 if (!delay && in_task())
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700525 schedule_timeout_interruptible(longdelay);
Lai Jiangshane546f482010-06-21 16:57:42 +0800526 else
527 rcu_read_delay(rrsp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700528}
529
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700530static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700531{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700532 srcu_read_unlock(srcu_ctlp, idx);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700533}
534
Paul E. McKenney6b80da42014-11-21 14:19:26 -0800535static unsigned long srcu_torture_completed(void)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700536{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700537 return srcu_batches_completed(srcu_ctlp);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700538}
539
Lai Jiangshan9059c942012-03-19 16:12:14 +0800540static void srcu_torture_deferred_free(struct rcu_torture *rp)
541{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700542 call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
Lai Jiangshan9059c942012-03-19 16:12:14 +0800543}
544
Josh Triplettb772e1d2006-10-04 02:17:13 -0700545static void srcu_torture_synchronize(void)
546{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700547 synchronize_srcu(srcu_ctlp);
Josh Triplettb772e1d2006-10-04 02:17:13 -0700548}
549
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700550static void srcu_torture_call(struct rcu_head *head,
Boqun Fengb6a4ae72015-07-29 13:29:38 +0800551 rcu_callback_t func)
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700552{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700553 call_srcu(srcu_ctlp, head, func);
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700554}
555
556static void srcu_torture_barrier(void)
557{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700558 srcu_barrier(srcu_ctlp);
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700559}
560
Joe Percheseea203f2014-07-14 09:16:15 -0400561static void srcu_torture_stats(void)
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700562{
Paul E. McKenney115a1a52017-05-22 13:31:03 -0700563 srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700564}
565
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700566static void srcu_torture_synchronize_expedited(void)
567{
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700568 synchronize_srcu_expedited(srcu_ctlp);
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700569}
570
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700571static struct rcu_torture_ops srcu_ops = {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800572 .ttype = SRCU_FLAVOR,
Lai Jiangshancda4dc82012-10-13 01:14:17 +0800573 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700574 .readlock = srcu_torture_read_lock,
575 .read_delay = srcu_read_delay,
576 .readunlock = srcu_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700577 .get_gp_seq = srcu_torture_completed,
Lai Jiangshan9059c942012-03-19 16:12:14 +0800578 .deferred_free = srcu_torture_deferred_free,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700579 .sync = srcu_torture_synchronize,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700580 .exp_sync = srcu_torture_synchronize_expedited,
Paul E. McKenneye3f8d372012-05-08 10:21:50 -0700581 .call = srcu_torture_call,
582 .cb_barrier = srcu_torture_barrier,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700583 .stats = srcu_torture_stats,
Paul E. McKenney5e741fa2017-06-06 12:52:44 -0700584 .irq_capable = 1,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700585 .name = "srcu"
Paul E. McKenneyb2896d22006-10-04 02:17:03 -0700586};
587
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700588static void srcu_torture_init(void)
589{
590 rcu_sync_torture_init();
591 WARN_ON(init_srcu_struct(&srcu_ctld));
592 srcu_ctlp = &srcu_ctld;
593}
594
595static void srcu_torture_cleanup(void)
596{
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700597 static DEFINE_TORTURE_RANDOM(rand);
598
599 if (torture_random(&rand) & 0x800)
600 cleanup_srcu_struct(&srcu_ctld);
601 else
602 cleanup_srcu_struct_quiesced(&srcu_ctld);
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700603 srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
604}
605
606/* As above, but dynamically allocated. */
607static struct rcu_torture_ops srcud_ops = {
608 .ttype = SRCU_FLAVOR,
609 .init = srcu_torture_init,
610 .cleanup = srcu_torture_cleanup,
611 .readlock = srcu_torture_read_lock,
612 .read_delay = srcu_read_delay,
613 .readunlock = srcu_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700614 .get_gp_seq = srcu_torture_completed,
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700615 .deferred_free = srcu_torture_deferred_free,
616 .sync = srcu_torture_synchronize,
617 .exp_sync = srcu_torture_synchronize_expedited,
618 .call = srcu_torture_call,
619 .cb_barrier = srcu_torture_barrier,
620 .stats = srcu_torture_stats,
Paul E. McKenney5e741fa2017-06-06 12:52:44 -0700621 .irq_capable = 1,
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -0700622 .name = "srcud"
623};
624
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700625/*
626 * Definitions for sched torture testing.
627 */
628
629static int sched_torture_read_lock(void)
630{
631 preempt_disable();
632 return 0;
633}
634
635static void sched_torture_read_unlock(int idx)
636{
637 preempt_enable();
638}
639
Paul E. McKenney23269742008-05-12 21:21:05 +0200640static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
641{
642 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
643}
644
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700645static struct rcu_torture_ops sched_ops = {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800646 .ttype = RCU_SCHED_FLAVOR,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700647 .init = rcu_sync_torture_init,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700648 .readlock = sched_torture_read_lock,
649 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
650 .readunlock = sched_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700651 .get_gp_seq = rcu_sched_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700652 .gp_diff = rcu_seq_diff,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700653 .deferred_free = rcu_sched_torture_deferred_free,
Paul E. McKenneybdf2a432011-06-07 16:59:35 -0700654 .sync = synchronize_sched,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700655 .exp_sync = synchronize_sched_expedited,
Paul E. McKenney24560052015-05-30 10:11:24 -0700656 .get_state = get_state_synchronize_sched,
657 .cond_sync = cond_synchronize_sched,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700658 .call = call_rcu_sched,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700659 .cb_barrier = rcu_barrier_sched,
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800660 .fqs = rcu_sched_force_quiescent_state,
Paul E. McKenney0acc5122009-06-25 09:08:17 -0700661 .stats = NULL,
662 .irq_capable = 1,
663 .name = "sched"
Josh Triplett4b6c2cc2006-10-04 02:17:16 -0700664};
665
Paul E. McKenney69c60452014-07-01 11:59:36 -0700666/*
667 * Definitions for RCU-tasks torture testing.
668 */
669
670static int tasks_torture_read_lock(void)
671{
672 return 0;
673}
674
675static void tasks_torture_read_unlock(int idx)
676{
677}
678
679static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
680{
681 call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
682}
683
684static struct rcu_torture_ops tasks_ops = {
685 .ttype = RCU_TASKS_FLAVOR,
686 .init = rcu_sync_torture_init,
687 .readlock = tasks_torture_read_lock,
688 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
689 .readunlock = tasks_torture_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700690 .get_gp_seq = rcu_no_completed,
Paul E. McKenney69c60452014-07-01 11:59:36 -0700691 .deferred_free = rcu_tasks_torture_deferred_free,
692 .sync = synchronize_rcu_tasks,
693 .exp_sync = synchronize_rcu_tasks,
694 .call = call_rcu_tasks,
695 .cb_barrier = rcu_barrier_tasks,
696 .fqs = NULL,
697 .stats = NULL,
698 .irq_capable = 1,
699 .name = "tasks"
700};
701
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700702static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
703{
704 if (!cur_ops->gp_diff)
705 return new - old;
706 return cur_ops->gp_diff(new, old);
707}
708
Paul E. McKenney5be5d1a2015-06-30 08:57:57 -0700709static bool __maybe_unused torturing_tasks(void)
710{
711 return cur_ops == &tasks_ops;
712}
713
Paul E. McKenney72e9bb52006-06-27 02:54:03 -0700714/*
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700715 * RCU torture priority-boost testing. Runs one real-time thread per
716 * CPU for moderate bursts, repeatedly registering RCU callbacks and
717 * spinning waiting for them to be invoked. If a given callback takes
718 * too long to be invoked, we assume that priority inversion has occurred.
719 */
720
721struct rcu_boost_inflight {
722 struct rcu_head rcu;
723 int inflight;
724};
725
726static void rcu_torture_boost_cb(struct rcu_head *head)
727{
728 struct rcu_boost_inflight *rbip =
729 container_of(head, struct rcu_boost_inflight, rcu);
730
Paul E. McKenney6c7ed422015-04-13 11:58:08 -0700731 /* Ensure RCU-core accesses precede clearing ->inflight */
732 smp_store_release(&rbip->inflight, 0);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700733}
734
735static int rcu_torture_boost(void *arg)
736{
737 unsigned long call_rcu_time;
738 unsigned long endtime;
739 unsigned long oldstarttime;
740 struct rcu_boost_inflight rbi = { .inflight = 0 };
741 struct sched_param sp;
742
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -0800743 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700744
745 /* Set real-time priority. */
746 sp.sched_priority = 1;
747 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -0800748 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700749 n_rcu_torture_boost_rterror++;
750 }
751
Paul E. McKenney561190e2011-03-30 09:10:44 -0700752 init_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700753 /* Each pass through the following loop does one boost-test cycle. */
754 do {
755 /* Wait for the next test interval. */
756 oldstarttime = boost_starttime;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700757 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
Paul E. McKenney0e11c8e2013-01-10 16:21:07 -0800758 schedule_timeout_interruptible(oldstarttime - jiffies);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800759 stutter_wait("rcu_torture_boost");
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800760 if (torture_must_stop())
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700761 goto checkwait;
762 }
763
764 /* Do one boost-test interval. */
765 endtime = oldstarttime + test_boost_duration * HZ;
766 call_rcu_time = jiffies;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700767 while (ULONG_CMP_LT(jiffies, endtime)) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700768 /* If we don't have a callback in flight, post one. */
Paul E. McKenney6c7ed422015-04-13 11:58:08 -0700769 if (!smp_load_acquire(&rbi.inflight)) {
770 /* RCU core before ->inflight = 1. */
771 smp_store_release(&rbi.inflight, 1);
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700772 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
773 if (jiffies - call_rcu_time >
774 test_boost_duration * HZ - HZ / 2) {
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -0800775 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700776 n_rcu_torture_boost_failure++;
777 }
778 call_rcu_time = jiffies;
779 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800780 stutter_wait("rcu_torture_boost");
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800781 if (torture_must_stop())
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700782 goto checkwait;
783 }
784
785 /*
786 * Set the start time of the next test interval.
787 * Yes, this is vulnerable to long delays, but such
788 * delays simply cause a false negative for the next
789 * interval. Besides, we are running at RT priority,
790 * so delays should be relatively rare.
791 */
Paul E. McKenneyab8f11e2011-08-18 09:30:32 -0700792 while (oldstarttime == boost_starttime &&
793 !kthread_should_stop()) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700794 if (mutex_trylock(&boost_mutex)) {
795 boost_starttime = jiffies +
796 test_boost_interval * HZ;
797 n_rcu_torture_boosts++;
798 mutex_unlock(&boost_mutex);
799 break;
800 }
801 schedule_timeout_uninterruptible(1);
802 }
803
804 /* Go do the stutter. */
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800805checkwait: stutter_wait("rcu_torture_boost");
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800806 } while (!torture_must_stop());
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700807
808 /* Clean up and exit. */
Paul E. McKenney6c7ed422015-04-13 11:58:08 -0700809 while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800810 torture_shutdown_absorb("rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700811 schedule_timeout_uninterruptible(1);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800812 }
Paul E. McKenney9d681972011-06-21 01:48:03 -0700813 destroy_rcu_head_on_stack(&rbi.rcu);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800814 torture_kthread_stopping("rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700815 return 0;
816}
817
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700818static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
819{
820}
821
822/*
823 * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
824 * to call_rcu() or analogous, increasing the probability of occurrence
825 * of callback-overflow corner cases.
826 */
827static int
828rcu_torture_cbflood(void *arg)
829{
830 int err = 1;
831 int i;
832 int j;
833 struct rcu_head *rhp;
834
835 if (cbflood_n_per_burst > 0 &&
836 cbflood_inter_holdoff > 0 &&
837 cbflood_intra_holdoff > 0 &&
838 cur_ops->call &&
839 cur_ops->cb_barrier) {
Kees Cook42bc47b2018-06-12 14:27:11 -0700840 rhp = vmalloc(array3_size(cbflood_n_burst,
841 cbflood_n_per_burst,
842 sizeof(*rhp)));
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700843 err = !rhp;
844 }
845 if (err) {
846 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
Paul E. McKenney3a0af332015-06-22 18:11:31 -0700847 goto wait_for_stop;
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700848 }
849 VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
850 do {
851 schedule_timeout_interruptible(cbflood_inter_holdoff);
852 atomic_long_inc(&n_cbfloods);
853 WARN_ON(signal_pending(current));
854 for (i = 0; i < cbflood_n_burst; i++) {
855 for (j = 0; j < cbflood_n_per_burst; j++) {
856 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
857 rcu_torture_cbflood_cb);
858 }
859 schedule_timeout_interruptible(cbflood_intra_holdoff);
860 WARN_ON(signal_pending(current));
861 }
862 cur_ops->cb_barrier();
863 stutter_wait("rcu_torture_cbflood");
864 } while (!torture_must_stop());
Paul E. McKenneyb8969d12014-10-27 15:52:04 -0700865 vfree(rhp);
Paul E. McKenney3a0af332015-06-22 18:11:31 -0700866wait_for_stop:
Paul E. McKenney38706bc2014-08-18 21:12:17 -0700867 torture_kthread_stopping("rcu_torture_cbflood");
868 return 0;
869}
870
Paul E. McKenney8e8be452010-09-02 16:16:14 -0700871/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800872 * RCU torture force-quiescent-state kthread. Repeatedly induces
873 * bursts of calls to force_quiescent_state(), increasing the probability
874 * of occurrence of some important types of race conditions.
875 */
876static int
877rcu_torture_fqs(void *arg)
878{
879 unsigned long fqs_resume_time;
880 int fqs_burst_remaining;
881
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -0800882 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800883 do {
884 fqs_resume_time = jiffies + fqs_stutter * HZ;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700885 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
886 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800887 schedule_timeout_interruptible(1);
888 }
889 fqs_burst_remaining = fqs_duration;
Paul E. McKenney93898fb2011-08-17 12:39:34 -0700890 while (fqs_burst_remaining > 0 &&
891 !kthread_should_stop()) {
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800892 cur_ops->fqs();
893 udelay(fqs_holdoff);
894 fqs_burst_remaining -= fqs_holdoff;
895 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800896 stutter_wait("rcu_torture_fqs");
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800897 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800898 torture_kthread_stopping("rcu_torture_fqs");
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800899 return 0;
900}
901
902/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800903 * RCU torture writer kthread. Repeatedly substitutes a new structure
904 * for that pointed to by rcu_torture_current, freeing the old structure
905 * after a series of grace periods (the "pipeline").
906 */
907static int
908rcu_torture_writer(void *arg)
909{
Paul E. McKenney9efafb82015-12-31 18:11:47 -0800910 bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
Paul E. McKenney4bb3c5f2015-02-18 16:31:29 -0800911 int expediting = 0;
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700912 unsigned long gp_snap;
913 bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700914 bool gp_sync1 = gp_sync;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800915 int i;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800916 struct rcu_torture *rp;
917 struct rcu_torture *old_rp;
Paul E. McKenney51b11302014-01-27 11:49:39 -0800918 static DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700919 int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
920 RTWS_COND_GET, RTWS_SYNC };
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700921 int nsynctypes = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800922
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -0800923 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
Paul E. McKenneyf7c0e6a2017-12-08 11:37:24 -0800924 if (!can_expedite)
Paul E. McKenneyaa5a8982015-12-31 16:27:06 -0800925 pr_alert("%s" TORTURE_FLAG
Paul E. McKenneyf7c0e6a2017-12-08 11:37:24 -0800926 " GP expediting controlled from boot/sysfs for %s.\n",
Paul E. McKenneyaa5a8982015-12-31 16:27:06 -0800927 torture_type, cur_ops->name);
Ingo Molnardbdf65b2005-11-13 16:07:22 -0800928
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700929 /* Initialize synctype[] array. If none set, take default. */
Paul E. McKenneyc136f992015-02-19 12:15:19 -0800930 if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700931 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800932 if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700933 synctype[nsynctypes++] = RTWS_COND_GET;
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800934 pr_info("%s: Testing conditional GPs.\n", __func__);
935 } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -0800936 pr_alert("%s: gp_cond without primitives.\n", __func__);
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800937 }
938 if (gp_exp1 && cur_ops->exp_sync) {
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700939 synctype[nsynctypes++] = RTWS_EXP_SYNC;
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800940 pr_info("%s: Testing expedited GPs.\n", __func__);
941 } else if (gp_exp && !cur_ops->exp_sync) {
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -0800942 pr_alert("%s: gp_exp without primitives.\n", __func__);
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800943 }
944 if (gp_normal1 && cur_ops->deferred_free) {
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700945 synctype[nsynctypes++] = RTWS_DEF_FREE;
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800946 pr_info("%s: Testing asynchronous GPs.\n", __func__);
947 } else if (gp_normal && !cur_ops->deferred_free) {
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -0800948 pr_alert("%s: gp_normal without primitives.\n", __func__);
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800949 }
950 if (gp_sync1 && cur_ops->sync) {
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700951 synctype[nsynctypes++] = RTWS_SYNC;
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800952 pr_info("%s: Testing normal GPs.\n", __func__);
953 } else if (gp_sync && !cur_ops->sync) {
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -0800954 pr_alert("%s: gp_sync without primitives.\n", __func__);
Paul E. McKenneydb0c1a82017-12-08 12:23:10 -0800955 }
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700956 if (WARN_ONCE(nsynctypes == 0,
957 "rcu_torture_writer: No update-side primitives.\n")) {
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -0700958 /*
959 * No updates primitives, so don't try updating.
960 * The resulting test won't be testing much, hence the
961 * above WARN_ONCE().
962 */
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700963 rcu_torture_writer_state = RTWS_STOPPING;
964 torture_kthread_stopping("rcu_torture_writer");
965 }
966
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800967 do {
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800968 rcu_torture_writer_state = RTWS_FIXED_DELAY;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800969 schedule_timeout_uninterruptible(1);
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700970 rp = rcu_torture_alloc();
971 if (rp == NULL)
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800972 continue;
973 rp->rtort_pipe_count = 0;
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800974 rcu_torture_writer_state = RTWS_DELAY;
Paul E. McKenney51b11302014-01-27 11:49:39 -0800975 udelay(torture_random(&rand) & 0x3ff);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800976 rcu_torture_writer_state = RTWS_REPLACE;
Paul E. McKenney0ddea0e2010-09-19 21:06:14 -0700977 old_rp = rcu_dereference_check(rcu_torture_current,
978 current == writer_task);
Paul E. McKenney996417d2005-11-18 01:10:50 -0800979 rp->rtort_mbtest = 1;
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800980 rcu_assign_pointer(rcu_torture_current, rp);
Paul E. McKenney9b2619a2009-09-23 09:50:43 -0700981 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
Josh Triplettc8e5b162007-05-08 00:33:20 -0700982 if (old_rp) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -0800983 i = old_rp->rtort_pipe_count;
984 if (i > RCU_TORTURE_PIPE_LEN)
985 i = RCU_TORTURE_PIPE_LEN;
986 atomic_inc(&rcu_torture_wcount[i]);
987 old_rp->rtort_pipe_count++;
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700988 switch (synctype[torture_random(&rand) % nsynctypes]) {
989 case RTWS_DEF_FREE:
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800990 rcu_torture_writer_state = RTWS_DEF_FREE;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700991 cur_ops->deferred_free(old_rp);
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700992 break;
993 case RTWS_EXP_SYNC:
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -0800994 rcu_torture_writer_state = RTWS_EXP_SYNC;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -0700995 cur_ops->exp_sync();
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -0700996 rcu_torture_pipe_update(old_rp);
997 break;
998 case RTWS_COND_GET:
999 rcu_torture_writer_state = RTWS_COND_GET;
1000 gp_snap = cur_ops->get_state();
1001 i = torture_random(&rand) % 16;
1002 if (i != 0)
1003 schedule_timeout_interruptible(i);
1004 udelay(torture_random(&rand) % 1000);
1005 rcu_torture_writer_state = RTWS_COND_SYNC;
1006 cur_ops->cond_sync(gp_snap);
1007 rcu_torture_pipe_update(old_rp);
1008 break;
Paul E. McKenneyf0bf8fa2014-03-21 16:17:56 -07001009 case RTWS_SYNC:
1010 rcu_torture_writer_state = RTWS_SYNC;
1011 cur_ops->sync();
1012 rcu_torture_pipe_update(old_rp);
1013 break;
Paul E. McKenneya48f3fa2014-03-18 15:57:41 -07001014 default:
1015 WARN_ON_ONCE(1);
1016 break;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001017 }
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001018 }
Paul E. McKenney6bea2cc2018-05-16 15:30:36 -07001019 rcu_torture_current_version++;
Paul E. McKenney4bb3c5f2015-02-18 16:31:29 -08001020 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1021 if (can_expedite &&
1022 !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1023 WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1024 if (expediting >= 0)
1025 rcu_expedite_gp();
1026 else
1027 rcu_unexpedite_gp();
1028 if (++expediting > 3)
1029 expediting = -expediting;
Paul E. McKenneyf7c0e6a2017-12-08 11:37:24 -08001030 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1031 can_expedite = !rcu_gp_is_expedited() &&
1032 !rcu_gp_is_normal();
Paul E. McKenney4bb3c5f2015-02-18 16:31:29 -08001033 }
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001034 rcu_torture_writer_state = RTWS_STUTTER;
Paul E. McKenney628edaa2014-01-31 11:57:43 -08001035 stutter_wait("rcu_torture_writer");
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001036 } while (!torture_must_stop());
Paul E. McKenney4bb3c5f2015-02-18 16:31:29 -08001037 /* Reset expediting back to unexpedited. */
1038 if (expediting > 0)
1039 expediting = -expediting;
1040 while (can_expedite && expediting++ < 0)
1041 rcu_unexpedite_gp();
1042 WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
Paul E. McKenneyf7c0e6a2017-12-08 11:37:24 -08001043 if (!can_expedite)
1044 pr_alert("%s" TORTURE_FLAG
1045 " Dynamic grace-period expediting was disabled.\n",
1046 torture_type);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001047 rcu_torture_writer_state = RTWS_STOPPING;
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001048 torture_kthread_stopping("rcu_torture_writer");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001049 return 0;
1050}
1051
1052/*
Josh Triplettb772e1d2006-10-04 02:17:13 -07001053 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1054 * delay between calls.
1055 */
1056static int
1057rcu_torture_fakewriter(void *arg)
1058{
Paul E. McKenney51b11302014-01-27 11:49:39 -08001059 DEFINE_TORTURE_RANDOM(rand);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001060
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001061 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
Linus Torvalds971eae72014-03-31 11:21:19 -07001062 set_user_nice(current, MAX_NICE);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001063
1064 do {
Paul E. McKenney51b11302014-01-27 11:49:39 -08001065 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1066 udelay(torture_random(&rand) & 0x3ff);
Paul E. McKenney72472a02012-05-29 17:50:51 -07001067 if (cur_ops->cb_barrier != NULL &&
Paul E. McKenney51b11302014-01-27 11:49:39 -08001068 torture_random(&rand) % (nfakewriters * 8) == 0) {
Paul E. McKenney72472a02012-05-29 17:50:51 -07001069 cur_ops->cb_barrier();
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001070 } else if (gp_normal == gp_exp) {
Paul E. McKenneyeb033992017-12-08 10:48:41 -08001071 if (cur_ops->sync && torture_random(&rand) & 0x80)
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001072 cur_ops->sync();
Paul E. McKenneyeb033992017-12-08 10:48:41 -08001073 else if (cur_ops->exp_sync)
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001074 cur_ops->exp_sync();
Paul E. McKenneyeb033992017-12-08 10:48:41 -08001075 } else if (gp_normal && cur_ops->sync) {
Paul E. McKenney72472a02012-05-29 17:50:51 -07001076 cur_ops->sync();
Paul E. McKenneyeb033992017-12-08 10:48:41 -08001077 } else if (cur_ops->exp_sync) {
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001078 cur_ops->exp_sync();
1079 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -08001080 stutter_wait("rcu_torture_fakewriter");
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001081 } while (!torture_must_stop());
Josh Triplettb772e1d2006-10-04 02:17:13 -07001082
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001083 torture_kthread_stopping("rcu_torture_fakewriter");
Josh Triplettb772e1d2006-10-04 02:17:13 -07001084 return 0;
1085}
1086
Paul E. McKenneyf34c85852017-07-20 15:27:32 -07001087static void rcu_torture_timer_cb(struct rcu_head *rhp)
1088{
1089 kfree(rhp);
1090}
1091
Josh Triplettb772e1d2006-10-04 02:17:13 -07001092/*
Paul E. McKenney6b06aa72018-05-22 10:56:05 -07001093 * Do one read-side critical section, returning false if there was
1094 * no data to read. Can be invoked both from process context and
1095 * from a timer handler.
1096 */
1097static bool rcu_torture_one_read(struct torture_random_state *trsp)
1098{
1099 int idx;
1100 unsigned long started;
1101 unsigned long completed;
1102 struct rcu_torture *p;
1103 int pipe_count;
1104 unsigned long long ts;
1105
1106 idx = cur_ops->readlock();
1107 started = cur_ops->get_gp_seq();
1108 ts = rcu_trace_clock_local();
1109 p = rcu_dereference_check(rcu_torture_current,
1110 rcu_read_lock_bh_held() ||
1111 rcu_read_lock_sched_held() ||
1112 srcu_read_lock_held(srcu_ctlp) ||
1113 torturing_tasks());
1114 if (p == NULL) {
1115 /* Wait for rcu_torture_writer to get underway */
1116 cur_ops->readunlock(idx);
1117 return false;
1118 }
1119 if (p->rtort_mbtest == 0)
1120 atomic_inc(&n_rcu_torture_mberror);
1121 cur_ops->read_delay(trsp);
1122 preempt_disable();
1123 pipe_count = p->rtort_pipe_count;
1124 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1125 /* Should not happen, but... */
1126 pipe_count = RCU_TORTURE_PIPE_LEN;
1127 }
1128 completed = cur_ops->get_gp_seq();
1129 if (pipe_count > 1) {
1130 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1131 ts, started, completed);
1132 rcu_ftrace_dump(DUMP_ALL);
1133 }
1134 __this_cpu_inc(rcu_torture_count[pipe_count]);
1135 completed = rcutorture_seq_diff(completed, started);
1136 if (completed > RCU_TORTURE_PIPE_LEN) {
1137 /* Should not happen, but... */
1138 completed = RCU_TORTURE_PIPE_LEN;
1139 }
1140 __this_cpu_inc(rcu_torture_batch[completed]);
1141 preempt_enable();
1142 cur_ops->readunlock(idx);
1143 return true;
1144}
1145
Paul E. McKenney3025520e2018-05-22 11:38:47 -07001146static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
1147
Paul E. McKenney6b06aa72018-05-22 10:56:05 -07001148/*
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001149 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1150 * incrementing the corresponding element of the pipeline array. The
1151 * counter in the element should never be greater than 1, otherwise, the
1152 * RCU implementation is broken.
1153 */
Kees Cookfd30b712017-10-22 17:58:54 -07001154static void rcu_torture_timer(struct timer_list *unused)
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001155{
1156 int idx;
Paul E. McKenney917963d2014-11-21 17:10:16 -08001157 unsigned long started;
Paul E. McKenney6b80da42014-11-21 14:19:26 -08001158 unsigned long completed;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001159 struct rcu_torture *p;
1160 int pipe_count;
Paul E. McKenney3025520e2018-05-22 11:38:47 -07001161 struct torture_random_state *trsp;
Paul E. McKenney52494532012-11-14 16:26:40 -08001162 unsigned long long ts;
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001163
Paul E. McKenney3025520e2018-05-22 11:38:47 -07001164 trsp = this_cpu_ptr(&rcu_torture_timer_rand);
Paul E. McKenney8da9a592018-05-22 11:17:51 -07001165 atomic_long_inc(&n_rcu_torture_timers);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001166 idx = cur_ops->readlock();
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -07001167 started = cur_ops->get_gp_seq();
Steven Rostedte4aa0da2013-02-04 13:36:13 -05001168 ts = rcu_trace_clock_local();
Paul E. McKenney632ee202010-02-22 17:04:45 -08001169 p = rcu_dereference_check(rcu_torture_current,
Paul E. McKenney632ee202010-02-22 17:04:45 -08001170 rcu_read_lock_bh_held() ||
1171 rcu_read_lock_sched_held() ||
Paul E. McKenney5be5d1a2015-06-30 08:57:57 -07001172 srcu_read_lock_held(srcu_ctlp) ||
1173 torturing_tasks());
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001174 if (p == NULL) {
1175 /* Leave because rcu_torture_writer is not yet underway */
1176 cur_ops->readunlock(idx);
1177 return;
1178 }
1179 if (p->rtort_mbtest == 0)
1180 atomic_inc(&n_rcu_torture_mberror);
Paul E. McKenney3025520e2018-05-22 11:38:47 -07001181 cur_ops->read_delay(trsp);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001182 preempt_disable();
1183 pipe_count = p->rtort_pipe_count;
1184 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1185 /* Should not happen, but... */
1186 pipe_count = RCU_TORTURE_PIPE_LEN;
1187 }
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -07001188 completed = cur_ops->get_gp_seq();
Paul E. McKenney52494532012-11-14 16:26:40 -08001189 if (pipe_count > 1) {
Paul E. McKenney52494532012-11-14 16:26:40 -08001190 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
Paul E. McKenney917963d2014-11-21 17:10:16 -08001191 started, completed);
Paul E. McKenney274529b2016-03-21 19:46:04 -07001192 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney52494532012-11-14 16:26:40 -08001193 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001194 __this_cpu_inc(rcu_torture_count[pipe_count]);
Paul E. McKenneyd72193122018-05-15 15:24:41 -07001195 completed = rcutorture_seq_diff(completed, started);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001196 if (completed > RCU_TORTURE_PIPE_LEN) {
1197 /* Should not happen, but... */
1198 completed = RCU_TORTURE_PIPE_LEN;
1199 }
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001200 __this_cpu_inc(rcu_torture_batch[completed]);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001201 preempt_enable();
1202 cur_ops->readunlock(idx);
Paul E. McKenneyf34c85852017-07-20 15:27:32 -07001203
1204 /* Test call_rcu() invocation from interrupt handler. */
1205 if (cur_ops->call) {
1206 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1207
1208 if (rhp)
1209 cur_ops->call(rhp, rcu_torture_timer_cb);
1210 }
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001211}
1212
1213/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001214 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1215 * incrementing the corresponding element of the pipeline array. The
1216 * counter in the element should never be greater than 1, otherwise, the
1217 * RCU implementation is broken.
1218 */
1219static int
1220rcu_torture_reader(void *arg)
1221{
Paul E. McKenney51b11302014-01-27 11:49:39 -08001222 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001223 struct timer_list t;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001224
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001225 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
Linus Torvalds971eae72014-03-31 11:21:19 -07001226 set_user_nice(current, MAX_NICE);
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001227 if (irqreader && cur_ops->irq_capable)
Kees Cookfd30b712017-10-22 17:58:54 -07001228 timer_setup_on_stack(&t, rcu_torture_timer, 0);
Ingo Molnardbdf65b2005-11-13 16:07:22 -08001229
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001230 do {
Paul E. McKenney0acc5122009-06-25 09:08:17 -07001231 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001232 if (!timer_pending(&t))
Paul E. McKenney6155fec2010-02-22 17:05:04 -08001233 mod_timer(&t, jiffies + 1);
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001234 }
Paul E. McKenney6b06aa72018-05-22 10:56:05 -07001235 if (!rcu_torture_one_read(&rand))
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001236 schedule_timeout_interruptible(HZ);
Paul E. McKenney628edaa2014-01-31 11:57:43 -08001237 stutter_wait("rcu_torture_reader");
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001238 } while (!torture_must_stop());
Thomas Gleixner424c1b62014-03-23 08:58:27 -07001239 if (irqreader && cur_ops->irq_capable) {
Paul E. McKenney0729fbf2008-06-25 12:24:52 -07001240 del_timer_sync(&t);
Thomas Gleixner424c1b62014-03-23 08:58:27 -07001241 destroy_timer_on_stack(&t);
1242 }
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001243 torture_kthread_stopping("rcu_torture_reader");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001244 return 0;
1245}
1246
1247/*
Joe Percheseea203f2014-07-14 09:16:15 -04001248 * Print torture statistics. Caller must ensure that there is only
1249 * one call to this function at a given time!!! This is normally
1250 * accomplished by relying on the module system to only have one copy
1251 * of the module loaded, and then by giving the rcu_torture_stats
1252 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1253 * thread is not running).
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001254 */
Chen Gangd1008952013-11-07 10:30:25 +08001255static void
Joe Percheseea203f2014-07-14 09:16:15 -04001256rcu_torture_stats_print(void)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001257{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001258 int cpu;
1259 int i;
1260 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1261 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001262 static unsigned long rtcv_snap = ULONG_MAX;
Paul E. McKenney0032f4e2017-08-30 10:40:17 -07001263 static bool splatted;
Paul E. McKenney4ffa6692016-06-30 11:56:38 -07001264 struct task_struct *wtp;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001265
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001266 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001267 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1268 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1269 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1270 }
1271 }
1272 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1273 if (pipesummary[i] != 0)
1274 break;
1275 }
Joe Percheseea203f2014-07-14 09:16:15 -04001276
1277 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1278 pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1279 rcu_torture_current,
1280 rcu_torture_current_version,
1281 list_empty(&rcu_torture_freelist),
1282 atomic_read(&n_rcu_torture_alloc),
1283 atomic_read(&n_rcu_torture_alloc_fail),
1284 atomic_read(&n_rcu_torture_free));
SeongJae Park472213a2016-08-13 15:54:35 +09001285 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
Joe Percheseea203f2014-07-14 09:16:15 -04001286 atomic_read(&n_rcu_torture_mberror),
SeongJae Park472213a2016-08-13 15:54:35 +09001287 n_rcu_torture_barrier_error,
Joe Percheseea203f2014-07-14 09:16:15 -04001288 n_rcu_torture_boost_ktrerror,
1289 n_rcu_torture_boost_rterror);
1290 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1291 n_rcu_torture_boost_failure,
1292 n_rcu_torture_boosts,
Paul E. McKenney8da9a592018-05-22 11:17:51 -07001293 atomic_long_read(&n_rcu_torture_timers));
Joe Percheseea203f2014-07-14 09:16:15 -04001294 torture_onoff_stats();
Paul E. McKenney38706bc2014-08-18 21:12:17 -07001295 pr_cont("barrier: %ld/%ld:%ld ",
Joe Percheseea203f2014-07-14 09:16:15 -04001296 n_barrier_successes,
1297 n_barrier_attempts,
1298 n_rcu_torture_barrier_error);
Paul E. McKenney38706bc2014-08-18 21:12:17 -07001299 pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
Joe Percheseea203f2014-07-14 09:16:15 -04001300
1301 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001302 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001303 n_rcu_torture_barrier_error != 0 ||
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001304 n_rcu_torture_boost_ktrerror != 0 ||
1305 n_rcu_torture_boost_rterror != 0 ||
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001306 n_rcu_torture_boost_failure != 0 ||
1307 i > 1) {
Joe Percheseea203f2014-07-14 09:16:15 -04001308 pr_cont("%s", "!!! ");
Paul E. McKenney996417d2005-11-18 01:10:50 -08001309 atomic_inc(&n_rcu_torture_error);
Ingo Molnar5af970a2008-06-18 10:09:48 +02001310 WARN_ON_ONCE(1);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001311 }
Joe Percheseea203f2014-07-14 09:16:15 -04001312 pr_cont("Reader Pipe: ");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001313 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Joe Percheseea203f2014-07-14 09:16:15 -04001314 pr_cont(" %ld", pipesummary[i]);
1315 pr_cont("\n");
1316
1317 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1318 pr_cont("Reader Batch: ");
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001319 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
Joe Percheseea203f2014-07-14 09:16:15 -04001320 pr_cont(" %ld", batchsummary[i]);
1321 pr_cont("\n");
1322
1323 pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1324 pr_cont("Free-Block Circulation: ");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001325 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
Joe Percheseea203f2014-07-14 09:16:15 -04001326 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001327 }
Joe Percheseea203f2014-07-14 09:16:15 -04001328 pr_cont("\n");
1329
Josh Triplettc8e5b162007-05-08 00:33:20 -07001330 if (cur_ops->stats)
Joe Percheseea203f2014-07-14 09:16:15 -04001331 cur_ops->stats();
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001332 if (rtcv_snap == rcu_torture_current_version &&
1333 rcu_torture_current != NULL) {
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001334 int __maybe_unused flags = 0;
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001335 unsigned long __maybe_unused gp_seq = 0;
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001336
1337 rcutorture_get_gp_data(cur_ops->ttype,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001338 &flags, &gp_seq);
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001339 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001340 &flags, &gp_seq);
Paul E. McKenney4ffa6692016-06-30 11:56:38 -07001341 wtp = READ_ONCE(writer_task);
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001342 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
Paul E. McKenney18aff332015-11-17 13:35:28 -08001343 rcu_torture_writer_state_getname(),
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001344 rcu_torture_writer_state, gp_seq, flags,
Paul E. McKenney808de392017-06-19 10:03:22 -07001345 wtp == NULL ? ~0UL : wtp->state,
1346 wtp == NULL ? -1 : (int)task_cpu(wtp));
Paul E. McKenney0032f4e2017-08-30 10:40:17 -07001347 if (!splatted && wtp) {
1348 sched_show_task(wtp);
1349 splatted = true;
1350 }
Paul E. McKenneyafea2272014-03-12 07:10:41 -07001351 show_rcu_gp_kthreads();
Paul E. McKenney274529b2016-03-21 19:46:04 -07001352 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenneyad0dc7f2014-02-19 10:51:42 -08001353 }
1354 rtcv_snap = rcu_torture_current_version;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001355}
1356
1357/*
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001358 * Periodically prints torture statistics, if periodic statistics printing
1359 * was specified via the stat_interval module parameter.
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001360 */
1361static int
1362rcu_torture_stats(void *arg)
1363{
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001364 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001365 do {
1366 schedule_timeout_interruptible(stat_interval * HZ);
1367 rcu_torture_stats_print();
Paul E. McKenneyf67a3352014-01-29 07:40:27 -08001368 torture_shutdown_absorb("rcu_torture_stats");
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001369 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001370 torture_kthread_stopping("rcu_torture_stats");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001371 return 0;
1372}
1373
Paul E. McKenney95c38322006-03-24 03:15:58 -08001374static inline void
Steven Rostedt (Red Hat)e66c33d2013-07-12 16:50:28 -04001375rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
Paul E. McKenney95c38322006-03-24 03:15:58 -08001376{
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001377 pr_alert("%s" TORTURE_FLAG
1378 "--- %s: nreaders=%d nfakewriters=%d "
1379 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1380 "shuffle_interval=%d stutter=%d irqreader=%d "
1381 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1382 "test_boost=%d/%d test_boost_interval=%d "
1383 "test_boost_duration=%d shutdown_secs=%d "
Paul E. McKenney2b1516e2017-08-18 16:11:37 -07001384 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001385 "n_barrier_cbs=%d "
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001386 "onoff_interval=%d onoff_holdoff=%d\n",
1387 torture_type, tag, nrealreaders, nfakewriters,
1388 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1389 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1390 test_boost, cur_ops->can_boost,
1391 test_boost_interval, test_boost_duration, shutdown_secs,
Paul E. McKenney2b1516e2017-08-18 16:11:37 -07001392 stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
Paul E. McKenney67afeed2012-10-20 12:56:06 -07001393 n_barrier_cbs,
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001394 onoff_interval, onoff_holdoff);
Paul E. McKenney95c38322006-03-24 03:15:58 -08001395}
1396
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001397static int rcutorture_booster_cleanup(unsigned int cpu)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001398{
1399 struct task_struct *t;
1400
1401 if (boost_tasks[cpu] == NULL)
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001402 return 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001403 mutex_lock(&boost_mutex);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001404 t = boost_tasks[cpu];
1405 boost_tasks[cpu] = NULL;
1406 mutex_unlock(&boost_mutex);
1407
1408 /* This must be outside of the mutex, otherwise deadlock! */
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001409 torture_stop_kthread(rcu_torture_boost, t);
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001410 return 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001411}
1412
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001413static int rcutorture_booster_init(unsigned int cpu)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001414{
1415 int retval;
1416
1417 if (boost_tasks[cpu] != NULL)
1418 return 0; /* Already created, nothing more to do. */
1419
1420 /* Don't allow time recalculation while creating a new task. */
1421 mutex_lock(&boost_mutex);
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001422 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
Eric Dumazet1f288092011-06-16 15:53:18 -07001423 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1424 cpu_to_node(cpu),
1425 "rcu_torture_boost");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001426 if (IS_ERR(boost_tasks[cpu])) {
1427 retval = PTR_ERR(boost_tasks[cpu]);
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001428 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001429 n_rcu_torture_boost_ktrerror++;
1430 boost_tasks[cpu] = NULL;
1431 mutex_unlock(&boost_mutex);
1432 return retval;
1433 }
1434 kthread_bind(boost_tasks[cpu], cpu);
1435 wake_up_process(boost_tasks[cpu]);
1436 mutex_unlock(&boost_mutex);
1437 return 0;
1438}
1439
Paul E. McKenneyd5f546d2011-11-04 11:44:12 -07001440/*
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001441 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1442 * induces a CPU stall for the time specified by stall_cpu.
1443 */
Paul Gortmaker49fb4c62013-06-19 14:52:21 -04001444static int rcu_torture_stall(void *args)
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001445{
1446 unsigned long stop_at;
1447
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001448 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001449 if (stall_cpu_holdoff > 0) {
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001450 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001451 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001452 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001453 }
1454 if (!kthread_should_stop()) {
1455 stop_at = get_seconds() + stall_cpu;
1456 /* RCU CPU stall is expected behavior in following code. */
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001457 rcu_read_lock();
Paul E. McKenney2b1516e2017-08-18 16:11:37 -07001458 if (stall_cpu_irqsoff)
1459 local_irq_disable();
1460 else
1461 preempt_disable();
1462 pr_alert("rcu_torture_stall start on CPU %d.\n",
1463 smp_processor_id());
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001464 while (ULONG_CMP_LT(get_seconds(), stop_at))
1465 continue; /* Induce RCU CPU stall warning. */
Paul E. McKenney2b1516e2017-08-18 16:11:37 -07001466 if (stall_cpu_irqsoff)
1467 local_irq_enable();
1468 else
1469 preempt_enable();
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001470 rcu_read_unlock();
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001471 pr_alert("rcu_torture_stall end.\n");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001472 }
Paul E. McKenneyf67a3352014-01-29 07:40:27 -08001473 torture_shutdown_absorb("rcu_torture_stall");
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001474 while (!kthread_should_stop())
1475 schedule_timeout_interruptible(10 * HZ);
1476 return 0;
1477}
1478
1479/* Spawn CPU-stall kthread, if stall_cpu specified. */
1480static int __init rcu_torture_stall_init(void)
1481{
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001482 if (stall_cpu <= 0)
1483 return 0;
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001484 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
Paul E. McKenneyc13f3752012-01-20 15:36:33 -08001485}
1486
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001487/* Callback function for RCU barrier testing. */
Rashika Kheriab3b8a4d2014-02-27 17:16:57 +05301488static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001489{
1490 atomic_inc(&barrier_cbs_invoked);
1491}
1492
1493/* kthread function to register callbacks used to test RCU barriers. */
1494static int rcu_torture_barrier_cbs(void *arg)
1495{
1496 long myid = (long)arg;
Paul E. McKenneyc6ebcbb2012-05-28 19:21:41 -07001497 bool lastphase = 0;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001498 bool newphase;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001499 struct rcu_head rcu;
1500
1501 init_rcu_head_on_stack(&rcu);
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001502 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
Linus Torvalds971eae72014-03-31 11:21:19 -07001503 set_user_nice(current, MAX_NICE);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001504 do {
1505 wait_event(barrier_cbs_wq[myid],
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001506 (newphase =
Paul E. McKenney6c7ed422015-04-13 11:58:08 -07001507 smp_load_acquire(&barrier_phase)) != lastphase ||
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001508 torture_must_stop());
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001509 lastphase = newphase;
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001510 if (torture_must_stop())
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001511 break;
Paul E. McKenney6c7ed422015-04-13 11:58:08 -07001512 /*
1513 * The above smp_load_acquire() ensures barrier_phase load
Paul E. McKenneyaab05732016-05-02 12:20:51 -07001514 * is ordered before the following ->call().
Paul E. McKenney6c7ed422015-04-13 11:58:08 -07001515 */
Paul E. McKenney0aa67e72016-03-30 11:40:44 -07001516 local_irq_disable(); /* Just to test no-irq call_rcu(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001517 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
Paul E. McKenney0aa67e72016-03-30 11:40:44 -07001518 local_irq_enable();
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001519 if (atomic_dec_and_test(&barrier_cbs_count))
1520 wake_up(&barrier_wq);
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001521 } while (!torture_must_stop());
Paul E. McKenney69c60452014-07-01 11:59:36 -07001522 if (cur_ops->cb_barrier != NULL)
1523 cur_ops->cb_barrier();
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001524 destroy_rcu_head_on_stack(&rcu);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001525 torture_kthread_stopping("rcu_torture_barrier_cbs");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001526 return 0;
1527}
1528
1529/* kthread function to drive and coordinate RCU barrier testing. */
1530static int rcu_torture_barrier(void *arg)
1531{
1532 int i;
1533
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001534 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001535 do {
1536 atomic_set(&barrier_cbs_invoked, 0);
1537 atomic_set(&barrier_cbs_count, n_barrier_cbs);
Paul E. McKenney6c7ed422015-04-13 11:58:08 -07001538 /* Ensure barrier_phase ordered after prior assignments. */
1539 smp_store_release(&barrier_phase, !barrier_phase);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001540 for (i = 0; i < n_barrier_cbs; i++)
1541 wake_up(&barrier_cbs_wq[i]);
1542 wait_event(barrier_wq,
1543 atomic_read(&barrier_cbs_count) == 0 ||
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001544 torture_must_stop());
1545 if (torture_must_stop())
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001546 break;
1547 n_barrier_attempts++;
Paul E. McKenney78e4bc32013-09-24 15:04:06 -07001548 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001549 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1550 n_rcu_torture_barrier_error++;
Paul E. McKenney7602de4a2014-12-17 18:39:54 -08001551 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1552 atomic_read(&barrier_cbs_invoked),
1553 n_barrier_cbs);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001554 WARN_ON_ONCE(1);
1555 }
1556 n_barrier_successes++;
1557 schedule_timeout_interruptible(HZ / 10);
Paul E. McKenney36970bb2014-01-30 15:49:29 -08001558 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -08001559 torture_kthread_stopping("rcu_torture_barrier");
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001560 return 0;
1561}
1562
1563/* Initialize RCU barrier testing. */
1564static int rcu_torture_barrier_init(void)
1565{
1566 int i;
1567 int ret;
1568
Paul E. McKenneyd9eba7682015-05-14 15:35:43 -07001569 if (n_barrier_cbs <= 0)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001570 return 0;
1571 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001572 pr_alert("%s" TORTURE_FLAG
1573 " Call or barrier ops missing for %s,\n",
1574 torture_type, cur_ops->name);
1575 pr_alert("%s" TORTURE_FLAG
1576 " RCU barrier testing omitted from run.\n",
1577 torture_type);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001578 return 0;
1579 }
1580 atomic_set(&barrier_cbs_count, 0);
1581 atomic_set(&barrier_cbs_invoked, 0);
1582 barrier_cbs_tasks =
Paul E. McKenney68a675d2017-12-01 14:26:56 -08001583 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001584 GFP_KERNEL);
1585 barrier_cbs_wq =
Paul E. McKenney68a675d2017-12-01 14:26:56 -08001586 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
Sasha Levinde5e6432012-12-20 14:11:28 -05001587 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001588 return -ENOMEM;
1589 for (i = 0; i < n_barrier_cbs; i++) {
1590 init_waitqueue_head(&barrier_cbs_wq[i]);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001591 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1592 (void *)(long)i,
1593 barrier_cbs_tasks[i]);
1594 if (ret)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001595 return ret;
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001596 }
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001597 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001598}
1599
1600/* Clean up after RCU barrier testing. */
1601static void rcu_torture_barrier_cleanup(void)
1602{
1603 int i;
1604
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001605 torture_stop_kthread(rcu_torture_barrier, barrier_task);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001606 if (barrier_cbs_tasks != NULL) {
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001607 for (i = 0; i < n_barrier_cbs; i++)
1608 torture_stop_kthread(rcu_torture_barrier_cbs,
1609 barrier_cbs_tasks[i]);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001610 kfree(barrier_cbs_tasks);
1611 barrier_cbs_tasks = NULL;
1612 }
1613 if (barrier_cbs_wq != NULL) {
1614 kfree(barrier_cbs_wq);
1615 barrier_cbs_wq = NULL;
1616 }
1617}
1618
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001619static enum cpuhp_state rcutor_hp;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001620
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001621static void
1622rcu_torture_cleanup(void)
1623{
Paul E. McKenney034777d2018-04-19 08:43:11 -07001624 int flags = 0;
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001625 unsigned long gp_seq = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001626 int i;
1627
Davidlohr Buesod36a7a02014-09-11 20:40:21 -07001628 if (torture_cleanup_begin()) {
Paul E. McKenney343e9092008-12-15 16:13:07 -08001629 if (cur_ops->cb_barrier != NULL)
1630 cur_ops->cb_barrier();
1631 return;
1632 }
Paul E. McKenney3808dc92014-01-28 15:29:21 -08001633
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001634 rcu_torture_barrier_cleanup();
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001635 torture_stop_kthread(rcu_torture_stall, stall_task);
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001636 torture_stop_kthread(rcu_torture_writer, writer_task);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001637
Josh Triplettc8e5b162007-05-08 00:33:20 -07001638 if (reader_tasks) {
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001639 for (i = 0; i < nrealreaders; i++)
1640 torture_stop_kthread(rcu_torture_reader,
1641 reader_tasks[i]);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001642 kfree(reader_tasks);
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001643 }
1644 rcu_torture_current = NULL;
1645
Josh Triplettc8e5b162007-05-08 00:33:20 -07001646 if (fakewriter_tasks) {
Josh Triplettb772e1d2006-10-04 02:17:13 -07001647 for (i = 0; i < nfakewriters; i++) {
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001648 torture_stop_kthread(rcu_torture_fakewriter,
1649 fakewriter_tasks[i]);
Josh Triplettb772e1d2006-10-04 02:17:13 -07001650 }
1651 kfree(fakewriter_tasks);
1652 fakewriter_tasks = NULL;
1653 }
1654
Paul E. McKenneyaebc8262018-05-01 06:42:51 -07001655 rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
1656 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
1657 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
1658 cur_ops->name, gp_seq, flags);
Paul E. McKenney9c029b82014-02-04 11:47:08 -08001659 torture_stop_kthread(rcu_torture_stats, stats_task);
1660 torture_stop_kthread(rcu_torture_fqs, fqs_task);
Paul E. McKenney38706bc2014-08-18 21:12:17 -07001661 for (i = 0; i < ncbflooders; i++)
1662 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001663 if ((test_boost == 1 && cur_ops->can_boost) ||
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001664 test_boost == 2)
1665 cpuhp_remove_state(rcutor_hp);
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001666
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -07001667 /*
1668 * Wait for all RCU callbacks to fire, then do flavor-specific
1669 * cleanup operations.
1670 */
Paul E. McKenney23269742008-05-12 21:21:05 +02001671 if (cur_ops->cb_barrier != NULL)
1672 cur_ops->cb_barrier();
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -07001673 if (cur_ops->cleanup != NULL)
1674 cur_ops->cleanup();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001675
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001676 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001677
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001678 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001679 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
Paul E. McKenney2e9e8082014-01-28 15:58:22 -08001680 else if (torture_onoff_failures())
Paul E. McKenney091541b2012-01-10 12:51:14 -08001681 rcu_torture_print_module_parms(cur_ops,
1682 "End of test: RCU_HOTPLUG");
Paul E. McKenney95c38322006-03-24 03:15:58 -08001683 else
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001684 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
Davidlohr Buesod36a7a02014-09-11 20:40:21 -07001685 torture_cleanup_end();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001686}
1687
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001688#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1689static void rcu_torture_leak_cb(struct rcu_head *rhp)
1690{
1691}
1692
1693static void rcu_torture_err_cb(struct rcu_head *rhp)
1694{
1695 /*
1696 * This -might- happen due to race conditions, but is unlikely.
1697 * The scenario that leads to this happening is that the
1698 * first of the pair of duplicate callbacks is queued,
1699 * someone else starts a grace period that includes that
1700 * callback, then the second of the pair must wait for the
1701 * next grace period. Unlikely, but can happen. If it
1702 * does happen, the debug-objects subsystem won't have splatted.
1703 */
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -08001704 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001705}
1706#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1707
1708/*
1709 * Verify that double-free causes debug-objects to complain, but only
1710 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1711 * cannot be carried out.
1712 */
1713static void rcu_test_debug_objects(void)
1714{
1715#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1716 struct rcu_head rh1;
1717 struct rcu_head rh2;
1718
1719 init_rcu_head_on_stack(&rh1);
1720 init_rcu_head_on_stack(&rh2);
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -08001721 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001722
1723 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1724 preempt_disable(); /* Prevent preemption from interrupting test. */
1725 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1726 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1727 local_irq_disable(); /* Make it harder to start a new grace period. */
1728 call_rcu(&rh2, rcu_torture_leak_cb);
1729 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1730 local_irq_enable();
1731 rcu_read_unlock();
1732 preempt_enable();
1733
1734 /* Wait for them all to get done so we can safely return. */
1735 rcu_barrier();
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -08001736 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001737 destroy_rcu_head_on_stack(&rh1);
1738 destroy_rcu_head_on_stack(&rh2);
1739#else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
Paul E. McKenneye0d31a34c2017-12-01 15:22:38 -08001740 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001741#endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1742}
1743
Josh Triplett6f8bc5002007-05-08 00:25:24 -07001744static int __init
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001745rcu_torture_init(void)
1746{
1747 int i;
1748 int cpu;
1749 int firsterr = 0;
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001750 static struct rcu_torture_ops *torture_ops[] = {
Paul E. McKenneyca1d51e2015-04-14 12:28:22 -07001751 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
Paul E. McKenney7e427762017-05-25 08:05:00 -07001752 &sched_ops, &tasks_ops,
Paul E. McKenney2ec1f2d2013-06-12 15:12:21 -07001753 };
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001754
Paul E. McKenneya2f25772017-11-21 20:19:17 -08001755 if (!torture_init_begin(torture_type, verbose))
Paul E. McKenney52280842014-04-07 09:14:11 -07001756 return -EBUSY;
Paul E. McKenney343e9092008-12-15 16:13:07 -08001757
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001758 /* Process args and tell the world that the torturer is on the job. */
Josh Triplettade5fb82007-05-08 00:33:22 -07001759 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001760 cur_ops = torture_ops[i];
Josh Triplettade5fb82007-05-08 00:33:22 -07001761 if (strcmp(torture_type, cur_ops->name) == 0)
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001762 break;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001763 }
Josh Triplettade5fb82007-05-08 00:33:22 -07001764 if (i == ARRAY_SIZE(torture_ops)) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001765 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1766 torture_type);
1767 pr_alert("rcu-torture types:");
Paul E. McKenneycf886c42009-10-25 19:03:54 -07001768 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001769 pr_alert(" %s", torture_ops[i]->name);
1770 pr_alert("\n");
Paul E. McKenney889d4872015-08-24 11:37:58 -07001771 firsterr = -EINVAL;
1772 goto unwind;
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001773 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001774 if (cur_ops->fqs == NULL && fqs_duration != 0) {
Paul E. McKenney2caa1e42012-08-09 16:30:45 -07001775 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001776 fqs_duration = 0;
1777 }
Josh Triplettc8e5b162007-05-08 00:33:20 -07001778 if (cur_ops->init)
Paul E. McKenney889d4872015-08-24 11:37:58 -07001779 cur_ops->init();
Paul E. McKenney72e9bb52006-06-27 02:54:03 -07001780
Paul E. McKenney64e4b432014-03-12 10:26:35 -07001781 if (nreaders >= 0) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001782 nrealreaders = nreaders;
Paul E. McKenney64e4b432014-03-12 10:26:35 -07001783 } else {
Paul E. McKenney3838cc12015-03-12 13:55:48 -07001784 nrealreaders = num_online_cpus() - 2 - nreaders;
Paul E. McKenney64e4b432014-03-12 10:26:35 -07001785 if (nrealreaders <= 0)
1786 nrealreaders = 1;
1787 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001788 rcu_torture_print_module_parms(cur_ops, "Start of test");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001789
1790 /* Set up the freelist. */
1791
1792 INIT_LIST_HEAD(&rcu_torture_freelist);
Ahmed S. Darwish788e7702007-05-08 00:33:14 -07001793 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
Paul E. McKenney996417d2005-11-18 01:10:50 -08001794 rcu_tortures[i].rtort_mbtest = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001795 list_add_tail(&rcu_tortures[i].rtort_free,
1796 &rcu_torture_freelist);
1797 }
1798
1799 /* Initialize the statistics so that each run gets its own numbers. */
1800
1801 rcu_torture_current = NULL;
1802 rcu_torture_current_version = 0;
1803 atomic_set(&n_rcu_torture_alloc, 0);
1804 atomic_set(&n_rcu_torture_alloc_fail, 0);
1805 atomic_set(&n_rcu_torture_free, 0);
Paul E. McKenney996417d2005-11-18 01:10:50 -08001806 atomic_set(&n_rcu_torture_mberror, 0);
1807 atomic_set(&n_rcu_torture_error, 0);
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001808 n_rcu_torture_barrier_error = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001809 n_rcu_torture_boost_ktrerror = 0;
1810 n_rcu_torture_boost_rterror = 0;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001811 n_rcu_torture_boost_failure = 0;
1812 n_rcu_torture_boosts = 0;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001813 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1814 atomic_set(&rcu_torture_wcount[i], 0);
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001815 for_each_possible_cpu(cpu) {
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001816 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1817 per_cpu(rcu_torture_count, cpu)[i] = 0;
1818 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1819 }
1820 }
1821
1822 /* Start up the kthreads. */
1823
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001824 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1825 writer_task);
1826 if (firsterr)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001827 goto unwind;
Paul E. McKenney4444d852015-05-14 15:42:40 -07001828 if (nfakewriters > 0) {
Paul E. McKenney68a675d2017-12-01 14:26:56 -08001829 fakewriter_tasks = kcalloc(nfakewriters,
Paul E. McKenney4444d852015-05-14 15:42:40 -07001830 sizeof(fakewriter_tasks[0]),
1831 GFP_KERNEL);
1832 if (fakewriter_tasks == NULL) {
1833 VERBOSE_TOROUT_ERRSTRING("out of memory");
1834 firsterr = -ENOMEM;
1835 goto unwind;
1836 }
Josh Triplettb772e1d2006-10-04 02:17:13 -07001837 }
1838 for (i = 0; i < nfakewriters; i++) {
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001839 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1840 NULL, fakewriter_tasks[i]);
1841 if (firsterr)
Josh Triplettb772e1d2006-10-04 02:17:13 -07001842 goto unwind;
Josh Triplettb772e1d2006-10-04 02:17:13 -07001843 }
Paul E. McKenney68a675d2017-12-01 14:26:56 -08001844 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001845 GFP_KERNEL);
1846 if (reader_tasks == NULL) {
Paul E. McKenney5ccf60f2014-01-29 07:25:25 -08001847 VERBOSE_TOROUT_ERRSTRING("out of memory");
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001848 firsterr = -ENOMEM;
1849 goto unwind;
1850 }
1851 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001852 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1853 reader_tasks[i]);
1854 if (firsterr)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001855 goto unwind;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001856 }
1857 if (stat_interval > 0) {
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001858 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1859 stats_task);
1860 if (firsterr)
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001861 goto unwind;
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001862 }
Paul E. McKenneye8e255f2015-05-14 16:55:45 -07001863 if (test_no_idle_hz && shuffle_interval > 0) {
Paul E. McKenney3808dc92014-01-28 15:29:21 -08001864 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1865 if (firsterr)
Rusty Russell73d0a4b2009-03-30 22:05:16 -06001866 goto unwind;
Srivatsa Vaddagirid84f5202006-01-08 01:03:42 -08001867 }
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001868 if (stutter < 0)
1869 stutter = 0;
1870 if (stutter) {
Paul E. McKenney628edaa2014-01-31 11:57:43 -08001871 firsterr = torture_stutter_init(stutter * HZ);
1872 if (firsterr)
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001873 goto unwind;
Paul E. McKenneyd120f652008-06-18 05:21:44 -07001874 }
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001875 if (fqs_duration < 0)
1876 fqs_duration = 0;
1877 if (fqs_duration) {
Paul E. McKenney628edaa2014-01-31 11:57:43 -08001878 /* Create the fqs thread */
Paul E. McKenneyd0d06062014-03-17 20:56:45 -07001879 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1880 fqs_task);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -08001881 if (firsterr)
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001882 goto unwind;
Paul E. McKenneybf66f182010-01-04 15:09:10 -08001883 }
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001884 if (test_boost_interval < 1)
1885 test_boost_interval = 1;
1886 if (test_boost_duration < 2)
1887 test_boost_duration = 2;
1888 if ((test_boost == 1 && cur_ops->can_boost) ||
1889 test_boost == 2) {
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001890
1891 boost_starttime = jiffies + test_boost_interval * HZ;
Sebastian Andrzej Siewior0ffd3742016-08-18 14:57:22 +02001892
1893 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
1894 rcutorture_booster_init,
1895 rcutorture_booster_cleanup);
1896 if (firsterr < 0)
1897 goto unwind;
1898 rcutor_hp = firsterr;
Paul E. McKenney8e8be452010-09-02 16:16:14 -07001899 }
Paul E. McKenney01025eb2014-01-31 15:15:02 -08001900 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1901 if (firsterr)
Paul E. McKenneye991dbc2014-01-31 14:52:13 -08001902 goto unwind;
Paul E. McKenney028be122018-05-08 09:20:34 -07001903 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval);
Paul E. McKenney01025eb2014-01-31 15:15:02 -08001904 if (firsterr)
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001905 goto unwind;
Paul E. McKenney01025eb2014-01-31 15:15:02 -08001906 firsterr = rcu_torture_stall_init();
1907 if (firsterr)
Paul E. McKenney37e377d2012-02-17 22:12:18 -08001908 goto unwind;
Paul E. McKenney01025eb2014-01-31 15:15:02 -08001909 firsterr = rcu_torture_barrier_init();
1910 if (firsterr)
Paul E. McKenneyfae4b542012-02-20 17:51:45 -08001911 goto unwind;
Paul E. McKenneyd2818df2013-04-23 17:05:42 -07001912 if (object_debug)
1913 rcu_test_debug_objects();
Paul E. McKenney38706bc2014-08-18 21:12:17 -07001914 if (cbflood_n_burst > 0) {
1915 /* Create the cbflood threads */
1916 ncbflooders = (num_online_cpus() + 3) / 4;
1917 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
1918 GFP_KERNEL);
1919 if (!cbflood_task) {
1920 VERBOSE_TOROUT_ERRSTRING("out of memory");
1921 firsterr = -ENOMEM;
1922 goto unwind;
1923 }
1924 for (i = 0; i < ncbflooders; i++) {
1925 firsterr = torture_create_kthread(rcu_torture_cbflood,
1926 NULL,
1927 cbflood_task[i]);
1928 if (firsterr)
1929 goto unwind;
1930 }
1931 }
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -08001932 torture_init_end();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001933 return 0;
1934
1935unwind:
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -08001936 torture_init_end();
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001937 rcu_torture_cleanup();
1938 return firsterr;
1939}
1940
1941module_init(rcu_torture_init);
1942module_exit(rcu_torture_cleanup);