rcutorture.c 46.1 KB
Newer Older
1
/*
2
 * Read-Copy Update module-based torture test facility
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
18
 * Copyright (C) IBM Corporation, 2005, 2006
19 20
 *
 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21
 *	  Josh Triplett <josh@freedesktop.org>
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * See also:  Documentation/RCU/torture.txt
 */
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/err.h>
#include <linux/spinlock.h>
#include <linux/smp.h>
#include <linux/rcupdate.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
A
Arun Sharma 已提交
36
#include <linux/atomic.h>
37 38 39 40 41
#include <linux/bitops.h>
#include <linux/completion.h>
#include <linux/moduleparam.h>
#include <linux/percpu.h>
#include <linux/notifier.h>
42
#include <linux/reboot.h>
43
#include <linux/freezer.h>
44 45 46
#include <linux/cpu.h>
#include <linux/delay.h>
#include <linux/stat.h>
47
#include <linux/srcu.h>
48
#include <linux/slab.h>
49
#include <asm/byteorder.h>
50 51

MODULE_LICENSE("GPL");
52
MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
53
	      "Josh Triplett <josh@freedesktop.org>");
54

55
static int nreaders = -1;	/* # reader threads, defaults to 2*ncpus */
56
static int nfakewriters = 4;	/* # fake writer threads */
57
static int stat_interval;	/* Interval between stats, in seconds. */
58
				/*  Defaults to "only at end of test". */
59 60
static int verbose;		/* Print more debug info. */
static int test_no_idle_hz;	/* Test RCU's support for tickless idle CPUs. */
61 62
static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
static int stutter = 5;		/* Start/stop testing interval (in sec) */
63
static int irqreader = 1;	/* RCU readers from irq (timers). */
64 65 66
static int fqs_duration = 0;	/* Duration of bursts (us), 0 to disable. */
static int fqs_holdoff = 0;	/* Hold time within burst (us). */
static int fqs_stutter = 3;	/* Wait time between bursts (s). */
67 68 69
static int test_boost = 1;	/* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
70
static char *torture_type = "rcu"; /* What RCU implementation to torture. */
71

72
module_param(nreaders, int, 0444);
73
MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
74
module_param(nfakewriters, int, 0444);
75
MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
76
module_param(stat_interval, int, 0444);
77
MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
78
module_param(verbose, bool, 0444);
79
MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
80
module_param(test_no_idle_hz, bool, 0444);
81
MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
82
module_param(shuffle_interval, int, 0444);
83
MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
84 85
module_param(stutter, int, 0444);
MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
86 87
module_param(irqreader, int, 0444);
MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
88 89 90 91 92 93
module_param(fqs_duration, int, 0444);
MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
module_param(fqs_holdoff, int, 0444);
MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
module_param(fqs_stutter, int, 0444);
MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
94 95 96 97 98 99
module_param(test_boost, int, 0444);
MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
module_param(test_boost_interval, int, 0444);
MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
module_param(test_boost_duration, int, 0444);
MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
100
module_param(torture_type, charp, 0444);
101
MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
102 103

#define TORTURE_FLAG "-torture:"
104
#define PRINTK_STRING(s) \
105
	do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
106
#define VERBOSE_PRINTK_STRING(s) \
107
	do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
108
#define VERBOSE_PRINTK_ERRSTRING(s) \
109
	do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
110 111 112 113 114

static char printk_buf[4096];

static int nrealreaders;
static struct task_struct *writer_task;
115
static struct task_struct **fakewriter_tasks;
116 117
static struct task_struct **reader_tasks;
static struct task_struct *stats_task;
118
static struct task_struct *shuffler_task;
119
static struct task_struct *stutter_task;
120
static struct task_struct *fqs_task;
121
static struct task_struct *boost_tasks[NR_CPUS];
122 123 124 125 126 127 128

#define RCU_TORTURE_PIPE_LEN 10

struct rcu_torture {
	struct rcu_head rtort_rcu;
	int rtort_pipe_count;
	struct list_head rtort_free;
129
	int rtort_mbtest;
130 131 132
};

static LIST_HEAD(rcu_torture_freelist);
133
static struct rcu_torture __rcu *rcu_torture_current;
134
static unsigned long rcu_torture_current_version;
135 136 137 138 139 140 141
static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
static DEFINE_SPINLOCK(rcu_torture_lock);
static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
	{ 0 };
static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
	{ 0 };
static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
142 143 144 145 146
static atomic_t n_rcu_torture_alloc;
static atomic_t n_rcu_torture_alloc_fail;
static atomic_t n_rcu_torture_free;
static atomic_t n_rcu_torture_mberror;
static atomic_t n_rcu_torture_error;
147 148 149 150
static long n_rcu_torture_boost_ktrerror;
static long n_rcu_torture_boost_rterror;
static long n_rcu_torture_boost_failure;
static long n_rcu_torture_boosts;
151
static long n_rcu_torture_timers;
152
static struct list_head rcu_torture_removed;
R
Rusty Russell 已提交
153
static cpumask_var_t shuffle_tmp_mask;
154

155
static int stutter_pause_test;
156

157 158 159 160 161 162 163
#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
#define RCUTORTURE_RUNNABLE_INIT 1
#else
#define RCUTORTURE_RUNNABLE_INIT 0
#endif
int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;

164
#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
165
#define rcu_can_boost() 1
166
#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
167
#define rcu_can_boost() 0
168
#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
169 170 171 172 173

static unsigned long boost_starttime;	/* jiffies of next boost test start. */
DEFINE_MUTEX(boost_mutex);		/* protect setting boost_starttime */
					/*  and boost task create/destroy. */

174 175 176 177 178 179
/* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */

#define FULLSTOP_DONTSTOP 0	/* Normal operation. */
#define FULLSTOP_SHUTDOWN 1	/* System shutdown with rcutorture running. */
#define FULLSTOP_RMMOD    2	/* Normal rmmod of rcutorture. */
static int fullstop = FULLSTOP_RMMOD;
180 181 182 183
/*
 * Protect fullstop transitions and spawning of kthreads.
 */
static DEFINE_MUTEX(fullstop_mutex);
184 185

/*
186
 * Detect and respond to a system shutdown.
187 188 189 190 191
 */
static int
rcutorture_shutdown_notify(struct notifier_block *unused1,
			   unsigned long unused2, void *unused3)
{
P
Paul E. McKenney 已提交
192
	mutex_lock(&fullstop_mutex);
193
	if (fullstop == FULLSTOP_DONTSTOP)
P
Paul E. McKenney 已提交
194
		fullstop = FULLSTOP_SHUTDOWN;
195 196 197
	else
		printk(KERN_WARNING /* but going down anyway, so... */
		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
P
Paul E. McKenney 已提交
198
	mutex_unlock(&fullstop_mutex);
199 200 201
	return NOTIFY_DONE;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215
/*
 * Absorb kthreads into a kernel function that won't return, so that
 * they won't ever access module text or data again.
 */
static void rcutorture_shutdown_absorb(char *title)
{
	if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
		printk(KERN_NOTICE
		       "rcutorture thread %s parking due to system shutdown\n",
		       title);
		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
	}
}

216 217 218
/*
 * Allocate an element from the rcu_tortures pool.
 */
A
Adrian Bunk 已提交
219
static struct rcu_torture *
220 221 222 223
rcu_torture_alloc(void)
{
	struct list_head *p;

224
	spin_lock_bh(&rcu_torture_lock);
225 226
	if (list_empty(&rcu_torture_freelist)) {
		atomic_inc(&n_rcu_torture_alloc_fail);
227
		spin_unlock_bh(&rcu_torture_lock);
228 229 230 231 232
		return NULL;
	}
	atomic_inc(&n_rcu_torture_alloc);
	p = rcu_torture_freelist.next;
	list_del_init(p);
233
	spin_unlock_bh(&rcu_torture_lock);
234 235 236 237 238 239 240 241 242 243
	return container_of(p, struct rcu_torture, rtort_free);
}

/*
 * Free an element to the rcu_tortures pool.
 */
static void
rcu_torture_free(struct rcu_torture *p)
{
	atomic_inc(&n_rcu_torture_free);
244
	spin_lock_bh(&rcu_torture_lock);
245
	list_add_tail(&p->rtort_free, &rcu_torture_freelist);
246
	spin_unlock_bh(&rcu_torture_lock);
247 248 249 250
}

struct rcu_random_state {
	unsigned long rrs_state;
251
	long rrs_count;
252 253 254 255 256 257 258 259 260 261
};

#define RCU_RANDOM_MULT 39916801  /* prime */
#define RCU_RANDOM_ADD	479001701 /* prime */
#define RCU_RANDOM_REFRESH 10000

#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }

/*
 * Crude but fast random-number generator.  Uses a linear congruential
262
 * generator, with occasional help from cpu_clock().
263
 */
264
static unsigned long
265 266 267
rcu_random(struct rcu_random_state *rrsp)
{
	if (--rrsp->rrs_count < 0) {
268
		rrsp->rrs_state += (unsigned long)local_clock();
269 270 271 272 273 274
		rrsp->rrs_count = RCU_RANDOM_REFRESH;
	}
	rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
	return swahw32(rrsp->rrs_state);
}

275
static void
276
rcu_stutter_wait(char *title)
277
{
278
	while (stutter_pause_test || !rcutorture_runnable) {
279 280 281
		if (rcutorture_runnable)
			schedule_timeout_interruptible(1);
		else
282
			schedule_timeout_interruptible(round_jiffies_relative(HZ));
283
		rcutorture_shutdown_absorb(title);
284
	}
285 286
}

287 288 289 290 291 292 293 294
/*
 * Operations vector for selecting different types of tests.
 */

struct rcu_torture_ops {
	void (*init)(void);
	void (*cleanup)(void);
	int (*readlock)(void);
295
	void (*read_delay)(struct rcu_random_state *rrsp);
296 297
	void (*readunlock)(int idx);
	int (*completed)(void);
298
	void (*deferred_free)(struct rcu_torture *p);
299
	void (*sync)(void);
300
	void (*cb_barrier)(void);
301
	void (*fqs)(void);
302
	int (*stats)(char *page);
303
	int irq_capable;
304
	int can_boost;
305 306
	char *name;
};
307 308

static struct rcu_torture_ops *cur_ops;
309 310 311 312 313

/*
 * Definitions for rcu torture testing.
 */

314
static int rcu_torture_read_lock(void) __acquires(RCU)
315 316 317 318 319
{
	rcu_read_lock();
	return 0;
}

320 321
static void rcu_read_delay(struct rcu_random_state *rrsp)
{
322 323
	const unsigned long shortdelay_us = 200;
	const unsigned long longdelay_ms = 50;
324

325 326 327
	/* We want a short delay sometimes to make a reader delay the grace
	 * period, and we want a long delay occasionally to trigger
	 * force_quiescent_state. */
328

329 330 331 332
	if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
		mdelay(longdelay_ms);
	if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
		udelay(shortdelay_us);
333 334 335 336
#ifdef CONFIG_PREEMPT
	if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
		preempt_schedule();  /* No QS if preempt_disable() in effect */
#endif
337 338
}

339
static void rcu_torture_read_unlock(int idx) __releases(RCU)
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
	rcu_read_unlock();
}

static int rcu_torture_completed(void)
{
	return rcu_batches_completed();
}

static void
rcu_torture_cb(struct rcu_head *p)
{
	int i;
	struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);

355
	if (fullstop != FULLSTOP_DONTSTOP) {
356 357 358 359 360 361 362 363 364 365 366 367
		/* Test is ending, just drop callbacks on the floor. */
		/* The next initialization will pick up the pieces. */
		return;
	}
	i = rp->rtort_pipe_count;
	if (i > RCU_TORTURE_PIPE_LEN)
		i = RCU_TORTURE_PIPE_LEN;
	atomic_inc(&rcu_torture_wcount[i]);
	if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
		rp->rtort_mbtest = 0;
		rcu_torture_free(rp);
	} else
368
		cur_ops->deferred_free(rp);
369 370
}

371 372 373 374 375
static int rcu_no_completed(void)
{
	return 0;
}

376 377 378 379 380 381
static void rcu_torture_deferred_free(struct rcu_torture *p)
{
	call_rcu(&p->rtort_rcu, rcu_torture_cb);
}

static struct rcu_torture_ops rcu_ops = {
382 383 384 385 386 387 388 389 390
	.init		= NULL,
	.cleanup	= NULL,
	.readlock	= rcu_torture_read_lock,
	.read_delay	= rcu_read_delay,
	.readunlock	= rcu_torture_read_unlock,
	.completed	= rcu_torture_completed,
	.deferred_free	= rcu_torture_deferred_free,
	.sync		= synchronize_rcu,
	.cb_barrier	= rcu_barrier,
391
	.fqs		= rcu_force_quiescent_state,
392
	.stats		= NULL,
393
	.irq_capable	= 1,
394
	.can_boost	= rcu_can_boost(),
395
	.name		= "rcu"
396 397
};

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
{
	int i;
	struct rcu_torture *rp;
	struct rcu_torture *rp1;

	cur_ops->sync();
	list_add(&p->rtort_free, &rcu_torture_removed);
	list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
		i = rp->rtort_pipe_count;
		if (i > RCU_TORTURE_PIPE_LEN)
			i = RCU_TORTURE_PIPE_LEN;
		atomic_inc(&rcu_torture_wcount[i]);
		if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
			rp->rtort_mbtest = 0;
			list_del(&rp->rtort_free);
			rcu_torture_free(rp);
		}
	}
}

static void rcu_sync_torture_init(void)
{
	INIT_LIST_HEAD(&rcu_torture_removed);
}

424
static struct rcu_torture_ops rcu_sync_ops = {
425 426 427 428 429 430 431 432 433
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= rcu_torture_read_lock,
	.read_delay	= rcu_read_delay,
	.readunlock	= rcu_torture_read_unlock,
	.completed	= rcu_torture_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= synchronize_rcu,
	.cb_barrier	= NULL,
434
	.fqs		= rcu_force_quiescent_state,
435 436
	.stats		= NULL,
	.irq_capable	= 1,
437
	.can_boost	= rcu_can_boost(),
438
	.name		= "rcu_sync"
439 440
};

441 442 443 444 445 446 447 448 449 450
static struct rcu_torture_ops rcu_expedited_ops = {
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= rcu_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= rcu_torture_read_unlock,
	.completed	= rcu_no_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= synchronize_rcu_expedited,
	.cb_barrier	= NULL,
451
	.fqs		= rcu_force_quiescent_state,
452 453
	.stats		= NULL,
	.irq_capable	= 1,
454
	.can_boost	= rcu_can_boost(),
455 456 457
	.name		= "rcu_expedited"
};

458 459 460 461
/*
 * Definitions for rcu_bh torture testing.
 */

462
static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
463 464 465 466 467
{
	rcu_read_lock_bh();
	return 0;
}

468
static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
{
	rcu_read_unlock_bh();
}

static int rcu_bh_torture_completed(void)
{
	return rcu_batches_completed_bh();
}

static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
{
	call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
}

static struct rcu_torture_ops rcu_bh_ops = {
484 485 486 487 488 489 490
	.init		= NULL,
	.cleanup	= NULL,
	.readlock	= rcu_bh_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= rcu_bh_torture_read_unlock,
	.completed	= rcu_bh_torture_completed,
	.deferred_free	= rcu_bh_torture_deferred_free,
491
	.sync		= synchronize_rcu_bh,
492
	.cb_barrier	= rcu_barrier_bh,
493
	.fqs		= rcu_bh_force_quiescent_state,
494 495 496
	.stats		= NULL,
	.irq_capable	= 1,
	.name		= "rcu_bh"
497 498
};

499
static struct rcu_torture_ops rcu_bh_sync_ops = {
500 501 502 503 504 505 506
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= rcu_bh_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= rcu_bh_torture_read_unlock,
	.completed	= rcu_bh_torture_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
507
	.sync		= synchronize_rcu_bh,
508
	.cb_barrier	= NULL,
509
	.fqs		= rcu_bh_force_quiescent_state,
510 511 512
	.stats		= NULL,
	.irq_capable	= 1,
	.name		= "rcu_bh_sync"
513 514
};

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
static struct rcu_torture_ops rcu_bh_expedited_ops = {
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= rcu_bh_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= rcu_bh_torture_read_unlock,
	.completed	= rcu_bh_torture_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= synchronize_rcu_bh_expedited,
	.cb_barrier	= NULL,
	.fqs		= rcu_bh_force_quiescent_state,
	.stats		= NULL,
	.irq_capable	= 1,
	.name		= "rcu_bh_expedited"
};

531 532 533 534 535 536 537 538 539
/*
 * Definitions for srcu torture testing.
 */

static struct srcu_struct srcu_ctl;

static void srcu_torture_init(void)
{
	init_srcu_struct(&srcu_ctl);
540
	rcu_sync_torture_init();
541 542 543 544 545 546 547 548
}

static void srcu_torture_cleanup(void)
{
	synchronize_srcu(&srcu_ctl);
	cleanup_srcu_struct(&srcu_ctl);
}

549
static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
{
	return srcu_read_lock(&srcu_ctl);
}

static void srcu_read_delay(struct rcu_random_state *rrsp)
{
	long delay;
	const long uspertick = 1000000 / HZ;
	const long longdelay = 10;

	/* We want there to be long-running readers, but not all the time. */

	delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
	if (!delay)
		schedule_timeout_interruptible(longdelay);
565 566
	else
		rcu_read_delay(rrsp);
567 568
}

569
static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
570 571 572 573 574 575 576 577 578
{
	srcu_read_unlock(&srcu_ctl, idx);
}

static int srcu_torture_completed(void)
{
	return srcu_batches_completed(&srcu_ctl);
}

579 580 581 582 583
static void srcu_torture_synchronize(void)
{
	synchronize_srcu(&srcu_ctl);
}

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
static int srcu_torture_stats(char *page)
{
	int cnt = 0;
	int cpu;
	int idx = srcu_ctl.completed & 0x1;

	cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
		       torture_type, TORTURE_FLAG, idx);
	for_each_possible_cpu(cpu) {
		cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
	}
	cnt += sprintf(&page[cnt], "\n");
	return cnt;
}

static struct rcu_torture_ops srcu_ops = {
602 603 604 605 606 607 608 609 610 611 612
	.init		= srcu_torture_init,
	.cleanup	= srcu_torture_cleanup,
	.readlock	= srcu_torture_read_lock,
	.read_delay	= srcu_read_delay,
	.readunlock	= srcu_torture_read_unlock,
	.completed	= srcu_torture_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= srcu_torture_synchronize,
	.cb_barrier	= NULL,
	.stats		= srcu_torture_stats,
	.name		= "srcu"
613 614
};

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
static void srcu_torture_synchronize_expedited(void)
{
	synchronize_srcu_expedited(&srcu_ctl);
}

static struct rcu_torture_ops srcu_expedited_ops = {
	.init		= srcu_torture_init,
	.cleanup	= srcu_torture_cleanup,
	.readlock	= srcu_torture_read_lock,
	.read_delay	= srcu_read_delay,
	.readunlock	= srcu_torture_read_unlock,
	.completed	= srcu_torture_completed,
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= srcu_torture_synchronize_expedited,
	.cb_barrier	= NULL,
	.stats		= srcu_torture_stats,
	.name		= "srcu_expedited"
};

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
/*
 * Definitions for sched torture testing.
 */

static int sched_torture_read_lock(void)
{
	preempt_disable();
	return 0;
}

static void sched_torture_read_unlock(int idx)
{
	preempt_enable();
}

649 650 651 652 653
static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
{
	call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
}

654
static struct rcu_torture_ops sched_ops = {
655 656 657 658 659
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= sched_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= sched_torture_read_unlock,
660
	.completed	= rcu_no_completed,
661
	.deferred_free	= rcu_sched_torture_deferred_free,
662
	.sync		= synchronize_sched,
663
	.cb_barrier	= rcu_barrier_sched,
664
	.fqs		= rcu_sched_force_quiescent_state,
665 666 667
	.stats		= NULL,
	.irq_capable	= 1,
	.name		= "sched"
668 669
};

670
static struct rcu_torture_ops sched_sync_ops = {
671 672 673 674 675
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= sched_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= sched_torture_read_unlock,
676
	.completed	= rcu_no_completed,
677
	.deferred_free	= rcu_sync_torture_deferred_free,
678
	.sync		= synchronize_sched,
679
	.cb_barrier	= NULL,
680
	.fqs		= rcu_sched_force_quiescent_state,
681 682 683 684 685 686 687 688 689 690
	.stats		= NULL,
	.name		= "sched_sync"
};

static struct rcu_torture_ops sched_expedited_ops = {
	.init		= rcu_sync_torture_init,
	.cleanup	= NULL,
	.readlock	= sched_torture_read_lock,
	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
	.readunlock	= sched_torture_read_unlock,
691
	.completed	= rcu_no_completed,
692 693 694
	.deferred_free	= rcu_sync_torture_deferred_free,
	.sync		= synchronize_sched_expedited,
	.cb_barrier	= NULL,
695
	.fqs		= rcu_sched_force_quiescent_state,
696
	.stats		= NULL,
697 698
	.irq_capable	= 1,
	.name		= "sched_expedited"
699 700
};

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
/*
 * RCU torture priority-boost testing.  Runs one real-time thread per
 * CPU for moderate bursts, repeatedly registering RCU callbacks and
 * spinning waiting for them to be invoked.  If a given callback takes
 * too long to be invoked, we assume that priority inversion has occurred.
 */

struct rcu_boost_inflight {
	struct rcu_head rcu;
	int inflight;
};

static void rcu_torture_boost_cb(struct rcu_head *head)
{
	struct rcu_boost_inflight *rbip =
		container_of(head, struct rcu_boost_inflight, rcu);

	smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
	rbip->inflight = 0;
}

static int rcu_torture_boost(void *arg)
{
	unsigned long call_rcu_time;
	unsigned long endtime;
	unsigned long oldstarttime;
	struct rcu_boost_inflight rbi = { .inflight = 0 };
	struct sched_param sp;

	VERBOSE_PRINTK_STRING("rcu_torture_boost started");

	/* Set real-time priority. */
	sp.sched_priority = 1;
	if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
		VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
		n_rcu_torture_boost_rterror++;
	}

739
	init_rcu_head_on_stack(&rbi.rcu);
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	/* Each pass through the following loop does one boost-test cycle. */
	do {
		/* Wait for the next test interval. */
		oldstarttime = boost_starttime;
		while (jiffies - oldstarttime > ULONG_MAX / 2) {
			schedule_timeout_uninterruptible(1);
			rcu_stutter_wait("rcu_torture_boost");
			if (kthread_should_stop() ||
			    fullstop != FULLSTOP_DONTSTOP)
				goto checkwait;
		}

		/* Do one boost-test interval. */
		endtime = oldstarttime + test_boost_duration * HZ;
		call_rcu_time = jiffies;
		while (jiffies - endtime > ULONG_MAX / 2) {
			/* If we don't have a callback in flight, post one. */
			if (!rbi.inflight) {
				smp_mb(); /* RCU core before ->inflight = 1. */
				rbi.inflight = 1;
				call_rcu(&rbi.rcu, rcu_torture_boost_cb);
				if (jiffies - call_rcu_time >
					 test_boost_duration * HZ - HZ / 2) {
					VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
					n_rcu_torture_boost_failure++;
				}
				call_rcu_time = jiffies;
			}
			cond_resched();
			rcu_stutter_wait("rcu_torture_boost");
			if (kthread_should_stop() ||
			    fullstop != FULLSTOP_DONTSTOP)
				goto checkwait;
		}

		/*
		 * Set the start time of the next test interval.
		 * Yes, this is vulnerable to long delays, but such
		 * delays simply cause a false negative for the next
		 * interval.  Besides, we are running at RT priority,
		 * so delays should be relatively rare.
		 */
		while (oldstarttime == boost_starttime) {
			if (mutex_trylock(&boost_mutex)) {
				boost_starttime = jiffies +
						  test_boost_interval * HZ;
				n_rcu_torture_boosts++;
				mutex_unlock(&boost_mutex);
				break;
			}
			schedule_timeout_uninterruptible(1);
		}

		/* Go do the stutter. */
checkwait:	rcu_stutter_wait("rcu_torture_boost");
	} while (!kthread_should_stop() && fullstop  == FULLSTOP_DONTSTOP);

	/* Clean up and exit. */
	VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
799
	destroy_rcu_head_on_stack(&rbi.rcu);
800 801 802 803 804 805 806
	rcutorture_shutdown_absorb("rcu_torture_boost");
	while (!kthread_should_stop() || rbi.inflight)
		schedule_timeout_uninterruptible(1);
	smp_mb(); /* order accesses to ->inflight before stack-frame death. */
	return 0;
}

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
/*
 * RCU torture force-quiescent-state kthread.  Repeatedly induces
 * bursts of calls to force_quiescent_state(), increasing the probability
 * of occurrence of some important types of race conditions.
 */
static int
rcu_torture_fqs(void *arg)
{
	unsigned long fqs_resume_time;
	int fqs_burst_remaining;

	VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
	do {
		fqs_resume_time = jiffies + fqs_stutter * HZ;
		while (jiffies - fqs_resume_time > LONG_MAX) {
			schedule_timeout_interruptible(1);
		}
		fqs_burst_remaining = fqs_duration;
		while (fqs_burst_remaining > 0) {
			cur_ops->fqs();
			udelay(fqs_holdoff);
			fqs_burst_remaining -= fqs_holdoff;
		}
		rcu_stutter_wait("rcu_torture_fqs");
	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
	VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
	rcutorture_shutdown_absorb("rcu_torture_fqs");
	while (!kthread_should_stop())
		schedule_timeout_uninterruptible(1);
	return 0;
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
/*
 * RCU torture writer kthread.  Repeatedly substitutes a new structure
 * for that pointed to by rcu_torture_current, freeing the old structure
 * after a series of grace periods (the "pipeline").
 */
static int
rcu_torture_writer(void *arg)
{
	int i;
	long oldbatch = rcu_batches_completed();
	struct rcu_torture *rp;
	struct rcu_torture *old_rp;
	static DEFINE_RCU_RANDOM(rand);

	VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
854 855
	set_user_nice(current, 19);

856 857
	do {
		schedule_timeout_uninterruptible(1);
858 859
		rp = rcu_torture_alloc();
		if (rp == NULL)
860 861 862
			continue;
		rp->rtort_pipe_count = 0;
		udelay(rcu_random(&rand) & 0x3ff);
863 864
		old_rp = rcu_dereference_check(rcu_torture_current,
					       current == writer_task);
865
		rp->rtort_mbtest = 1;
866
		rcu_assign_pointer(rcu_torture_current, rp);
867
		smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
868
		if (old_rp) {
869 870 871 872 873
			i = old_rp->rtort_pipe_count;
			if (i > RCU_TORTURE_PIPE_LEN)
				i = RCU_TORTURE_PIPE_LEN;
			atomic_inc(&rcu_torture_wcount[i]);
			old_rp->rtort_pipe_count++;
874
			cur_ops->deferred_free(old_rp);
875
		}
876
		rcutorture_record_progress(++rcu_torture_current_version);
877
		oldbatch = cur_ops->completed();
878 879
		rcu_stutter_wait("rcu_torture_writer");
	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
880
	VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
881 882
	rcutorture_shutdown_absorb("rcu_torture_writer");
	while (!kthread_should_stop())
883 884 885 886
		schedule_timeout_uninterruptible(1);
	return 0;
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
/*
 * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
 * delay between calls.
 */
static int
rcu_torture_fakewriter(void *arg)
{
	DEFINE_RCU_RANDOM(rand);

	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
	set_user_nice(current, 19);

	do {
		schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
		udelay(rcu_random(&rand) & 0x3ff);
		cur_ops->sync();
903 904
		rcu_stutter_wait("rcu_torture_fakewriter");
	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
905 906

	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
907 908
	rcutorture_shutdown_absorb("rcu_torture_fakewriter");
	while (!kthread_should_stop())
909 910 911 912
		schedule_timeout_uninterruptible(1);
	return 0;
}

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
/*
 * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
 * incrementing the corresponding element of the pipeline array.  The
 * counter in the element should never be greater than 1, otherwise, the
 * RCU implementation is broken.
 */
static void rcu_torture_timer(unsigned long unused)
{
	int idx;
	int completed;
	static DEFINE_RCU_RANDOM(rand);
	static DEFINE_SPINLOCK(rand_lock);
	struct rcu_torture *p;
	int pipe_count;

	idx = cur_ops->readlock();
	completed = cur_ops->completed();
930 931 932 933
	p = rcu_dereference_check(rcu_torture_current,
				  rcu_read_lock_bh_held() ||
				  rcu_read_lock_sched_held() ||
				  srcu_read_lock_held(&srcu_ctl));
934 935 936 937 938 939 940 941
	if (p == NULL) {
		/* Leave because rcu_torture_writer is not yet underway */
		cur_ops->readunlock(idx);
		return;
	}
	if (p->rtort_mbtest == 0)
		atomic_inc(&n_rcu_torture_mberror);
	spin_lock(&rand_lock);
942
	cur_ops->read_delay(&rand);
943 944 945 946 947 948 949 950
	n_rcu_torture_timers++;
	spin_unlock(&rand_lock);
	preempt_disable();
	pipe_count = p->rtort_pipe_count;
	if (pipe_count > RCU_TORTURE_PIPE_LEN) {
		/* Should not happen, but... */
		pipe_count = RCU_TORTURE_PIPE_LEN;
	}
R
Rusty Russell 已提交
951
	__this_cpu_inc(rcu_torture_count[pipe_count]);
952 953 954 955 956
	completed = cur_ops->completed() - completed;
	if (completed > RCU_TORTURE_PIPE_LEN) {
		/* Should not happen, but... */
		completed = RCU_TORTURE_PIPE_LEN;
	}
R
Rusty Russell 已提交
957
	__this_cpu_inc(rcu_torture_batch[completed]);
958 959 960 961
	preempt_enable();
	cur_ops->readunlock(idx);
}

962 963 964 965 966 967 968 969 970 971
/*
 * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
 * incrementing the corresponding element of the pipeline array.  The
 * counter in the element should never be greater than 1, otherwise, the
 * RCU implementation is broken.
 */
static int
rcu_torture_reader(void *arg)
{
	int completed;
972
	int idx;
973 974 975
	DEFINE_RCU_RANDOM(rand);
	struct rcu_torture *p;
	int pipe_count;
976
	struct timer_list t;
977 978

	VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
979
	set_user_nice(current, 19);
980
	if (irqreader && cur_ops->irq_capable)
981
		setup_timer_on_stack(&t, rcu_torture_timer, 0);
982

983
	do {
984
		if (irqreader && cur_ops->irq_capable) {
985
			if (!timer_pending(&t))
986
				mod_timer(&t, jiffies + 1);
987
		}
988 989
		idx = cur_ops->readlock();
		completed = cur_ops->completed();
990 991 992 993
		p = rcu_dereference_check(rcu_torture_current,
					  rcu_read_lock_bh_held() ||
					  rcu_read_lock_sched_held() ||
					  srcu_read_lock_held(&srcu_ctl));
994 995
		if (p == NULL) {
			/* Wait for rcu_torture_writer to get underway */
996
			cur_ops->readunlock(idx);
997 998 999
			schedule_timeout_interruptible(HZ);
			continue;
		}
1000 1001
		if (p->rtort_mbtest == 0)
			atomic_inc(&n_rcu_torture_mberror);
1002
		cur_ops->read_delay(&rand);
1003 1004 1005 1006 1007 1008
		preempt_disable();
		pipe_count = p->rtort_pipe_count;
		if (pipe_count > RCU_TORTURE_PIPE_LEN) {
			/* Should not happen, but... */
			pipe_count = RCU_TORTURE_PIPE_LEN;
		}
R
Rusty Russell 已提交
1009
		__this_cpu_inc(rcu_torture_count[pipe_count]);
1010
		completed = cur_ops->completed() - completed;
1011 1012 1013 1014
		if (completed > RCU_TORTURE_PIPE_LEN) {
			/* Should not happen, but... */
			completed = RCU_TORTURE_PIPE_LEN;
		}
R
Rusty Russell 已提交
1015
		__this_cpu_inc(rcu_torture_batch[completed]);
1016
		preempt_enable();
1017
		cur_ops->readunlock(idx);
1018
		schedule();
1019 1020
		rcu_stutter_wait("rcu_torture_reader");
	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1021
	VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1022
	rcutorture_shutdown_absorb("rcu_torture_reader");
1023
	if (irqreader && cur_ops->irq_capable)
1024
		del_timer_sync(&t);
1025
	while (!kthread_should_stop())
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		schedule_timeout_uninterruptible(1);
	return 0;
}

/*
 * Create an RCU-torture statistics message in the specified buffer.
 */
static int
rcu_torture_printk(char *page)
{
	int cnt = 0;
	int cpu;
	int i;
	long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
	long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };

1042
	for_each_possible_cpu(cpu) {
1043 1044 1045 1046 1047 1048 1049 1050 1051
		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
			pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
			batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
		}
	}
	for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
		if (pipesummary[i] != 0)
			break;
	}
1052
	cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1053
	cnt += sprintf(&page[cnt],
1054
		       "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
1055
		       "rtmbe: %d rtbke: %ld rtbre: %ld "
1056
		       "rtbf: %ld rtb: %ld nt: %ld",
1057 1058 1059 1060 1061
		       rcu_torture_current,
		       rcu_torture_current_version,
		       list_empty(&rcu_torture_freelist),
		       atomic_read(&n_rcu_torture_alloc),
		       atomic_read(&n_rcu_torture_alloc_fail),
1062
		       atomic_read(&n_rcu_torture_free),
1063
		       atomic_read(&n_rcu_torture_mberror),
1064 1065 1066 1067
		       n_rcu_torture_boost_ktrerror,
		       n_rcu_torture_boost_rterror,
		       n_rcu_torture_boost_failure,
		       n_rcu_torture_boosts,
1068
		       n_rcu_torture_timers);
1069 1070 1071 1072
	if (atomic_read(&n_rcu_torture_mberror) != 0 ||
	    n_rcu_torture_boost_ktrerror != 0 ||
	    n_rcu_torture_boost_rterror != 0 ||
	    n_rcu_torture_boost_failure != 0)
1073
		cnt += sprintf(&page[cnt], " !!!");
1074
	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1075
	if (i > 1) {
1076
		cnt += sprintf(&page[cnt], "!!! ");
1077
		atomic_inc(&n_rcu_torture_error);
1078
		WARN_ON_ONCE(1);
1079
	}
1080 1081 1082
	cnt += sprintf(&page[cnt], "Reader Pipe: ");
	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
		cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1083
	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1084
	cnt += sprintf(&page[cnt], "Reader Batch: ");
1085
	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1086
		cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1087
	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1088 1089 1090 1091 1092 1093
	cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
		cnt += sprintf(&page[cnt], " %d",
			       atomic_read(&rcu_torture_wcount[i]));
	}
	cnt += sprintf(&page[cnt], "\n");
1094
	if (cur_ops->stats)
1095
		cnt += cur_ops->stats(&page[cnt]);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	return cnt;
}

/*
 * Print torture statistics.  Caller must ensure that there is only
 * one call to this function at a given time!!!  This is normally
 * accomplished by relying on the module system to only have one copy
 * of the module loaded, and then by giving the rcu_torture_stats
 * kthread full control (or the init/cleanup functions when rcu_torture_stats
 * thread is not running).
 */
static void
rcu_torture_stats_print(void)
{
	int cnt;

	cnt = rcu_torture_printk(printk_buf);
	printk(KERN_ALERT "%s", printk_buf);
}

/*
 * Periodically prints torture statistics, if periodic statistics printing
 * was specified via the stat_interval module parameter.
 *
 * No need to worry about fullstop here, since this one doesn't reference
 * volatile state or register callbacks.
 */
static int
rcu_torture_stats(void *arg)
{
	VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
	do {
		schedule_timeout_interruptible(stat_interval * HZ);
		rcu_torture_stats_print();
1130 1131
		rcutorture_shutdown_absorb("rcu_torture_stats");
	} while (!kthread_should_stop());
1132 1133 1134 1135
	VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
	return 0;
}

1136 1137 1138 1139 1140
static int rcu_idle_cpu;	/* Force all torture tasks off this CPU */

/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
 */
1141
static void rcu_torture_shuffle_tasks(void)
1142 1143 1144
{
	int i;

R
Rusty Russell 已提交
1145
	cpumask_setall(shuffle_tmp_mask);
1146
	get_online_cpus();
1147 1148

	/* No point in shuffling if there is only one online CPU (ex: UP) */
1149 1150 1151 1152
	if (num_online_cpus() == 1) {
		put_online_cpus();
		return;
	}
1153 1154

	if (rcu_idle_cpu != -1)
R
Rusty Russell 已提交
1155
		cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1156

R
Rusty Russell 已提交
1157
	set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1158

1159
	if (reader_tasks) {
1160 1161
		for (i = 0; i < nrealreaders; i++)
			if (reader_tasks[i])
1162
				set_cpus_allowed_ptr(reader_tasks[i],
R
Rusty Russell 已提交
1163
						     shuffle_tmp_mask);
1164 1165
	}

1166
	if (fakewriter_tasks) {
1167 1168
		for (i = 0; i < nfakewriters; i++)
			if (fakewriter_tasks[i])
1169
				set_cpus_allowed_ptr(fakewriter_tasks[i],
R
Rusty Russell 已提交
1170
						     shuffle_tmp_mask);
1171 1172
	}

1173
	if (writer_task)
R
Rusty Russell 已提交
1174
		set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1175 1176

	if (stats_task)
R
Rusty Russell 已提交
1177
		set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1178 1179 1180 1181 1182 1183

	if (rcu_idle_cpu == -1)
		rcu_idle_cpu = num_online_cpus() - 1;
	else
		rcu_idle_cpu--;

1184
	put_online_cpus();
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
}

/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
 * system to become idle at a time and cut off its timer ticks. This is meant
 * to test the support for such tickless idle CPU in RCU.
 */
static int
rcu_torture_shuffle(void *arg)
{
	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
	do {
		schedule_timeout_interruptible(shuffle_interval * HZ);
		rcu_torture_shuffle_tasks();
1198 1199
		rcutorture_shutdown_absorb("rcu_torture_shuffle");
	} while (!kthread_should_stop());
1200 1201 1202 1203
	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
	return 0;
}

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
/* Cause the rcutorture test to "stutter", starting and stopping all
 * threads periodically.
 */
static int
rcu_torture_stutter(void *arg)
{
	VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
	do {
		schedule_timeout_interruptible(stutter * HZ);
		stutter_pause_test = 1;
1214
		if (!kthread_should_stop())
1215 1216
			schedule_timeout_interruptible(stutter * HZ);
		stutter_pause_test = 0;
1217 1218
		rcutorture_shutdown_absorb("rcu_torture_stutter");
	} while (!kthread_should_stop());
1219 1220 1221 1222
	VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
	return 0;
}

1223
static inline void
1224
rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
1225
{
1226 1227
	printk(KERN_ALERT "%s" TORTURE_FLAG
		"--- %s: nreaders=%d nfakewriters=%d "
1228
		"stat_interval=%d verbose=%d test_no_idle_hz=%d "
1229
		"shuffle_interval=%d stutter=%d irqreader=%d "
1230 1231 1232
		"fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
		"test_boost=%d/%d test_boost_interval=%d "
		"test_boost_duration=%d\n",
1233
		torture_type, tag, nrealreaders, nfakewriters,
1234
		stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1235 1236 1237
		stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
		test_boost, cur_ops->can_boost,
		test_boost_interval, test_boost_duration);
1238 1239
}

1240
static struct notifier_block rcutorture_shutdown_nb = {
1241 1242 1243
	.notifier_call = rcutorture_shutdown_notify,
};

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static void rcutorture_booster_cleanup(int cpu)
{
	struct task_struct *t;

	if (boost_tasks[cpu] == NULL)
		return;
	mutex_lock(&boost_mutex);
	VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
	t = boost_tasks[cpu];
	boost_tasks[cpu] = NULL;
	mutex_unlock(&boost_mutex);

	/* This must be outside of the mutex, otherwise deadlock! */
	kthread_stop(t);
}

static int rcutorture_booster_init(int cpu)
{
	int retval;

	if (boost_tasks[cpu] != NULL)
		return 0;  /* Already created, nothing more to do. */

	/* Don't allow time recalculation while creating a new task. */
	mutex_lock(&boost_mutex);
	VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
E
Eric Dumazet 已提交
1270 1271 1272
	boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
						  cpu_to_node(cpu),
						  "rcu_torture_boost");
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
	if (IS_ERR(boost_tasks[cpu])) {
		retval = PTR_ERR(boost_tasks[cpu]);
		VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
		n_rcu_torture_boost_ktrerror++;
		boost_tasks[cpu] = NULL;
		mutex_unlock(&boost_mutex);
		return retval;
	}
	kthread_bind(boost_tasks[cpu], cpu);
	wake_up_process(boost_tasks[cpu]);
	mutex_unlock(&boost_mutex);
	return 0;
}

static int rcutorture_cpu_notify(struct notifier_block *self,
				 unsigned long action, void *hcpu)
{
	long cpu = (long)hcpu;

	switch (action) {
	case CPU_ONLINE:
	case CPU_DOWN_FAILED:
		(void)rcutorture_booster_init(cpu);
		break;
	case CPU_DOWN_PREPARE:
		rcutorture_booster_cleanup(cpu);
		break;
	default:
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block rcutorture_cpu_nb = {
	.notifier_call = rcutorture_cpu_notify,
};

1310 1311 1312 1313 1314
static void
rcu_torture_cleanup(void)
{
	int i;

1315
	mutex_lock(&fullstop_mutex);
1316
	rcutorture_record_test_transition();
1317 1318 1319
	if (fullstop == FULLSTOP_SHUTDOWN) {
		printk(KERN_WARNING /* but going down anyway, so... */
		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1320
		mutex_unlock(&fullstop_mutex);
1321
		schedule_timeout_uninterruptible(10);
1322 1323 1324 1325
		if (cur_ops->cb_barrier != NULL)
			cur_ops->cb_barrier();
		return;
	}
1326
	fullstop = FULLSTOP_RMMOD;
1327
	mutex_unlock(&fullstop_mutex);
1328
	unregister_reboot_notifier(&rcutorture_shutdown_nb);
1329 1330 1331 1332 1333
	if (stutter_task) {
		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
		kthread_stop(stutter_task);
	}
	stutter_task = NULL;
1334
	if (shuffler_task) {
1335 1336
		VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
		kthread_stop(shuffler_task);
R
Rusty Russell 已提交
1337
		free_cpumask_var(shuffle_tmp_mask);
1338 1339 1340
	}
	shuffler_task = NULL;

1341
	if (writer_task) {
1342 1343 1344 1345 1346
		VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
		kthread_stop(writer_task);
	}
	writer_task = NULL;

1347
	if (reader_tasks) {
1348
		for (i = 0; i < nrealreaders; i++) {
1349
			if (reader_tasks[i]) {
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
				VERBOSE_PRINTK_STRING(
					"Stopping rcu_torture_reader task");
				kthread_stop(reader_tasks[i]);
			}
			reader_tasks[i] = NULL;
		}
		kfree(reader_tasks);
		reader_tasks = NULL;
	}
	rcu_torture_current = NULL;

1361
	if (fakewriter_tasks) {
1362
		for (i = 0; i < nfakewriters; i++) {
1363
			if (fakewriter_tasks[i]) {
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
				VERBOSE_PRINTK_STRING(
					"Stopping rcu_torture_fakewriter task");
				kthread_stop(fakewriter_tasks[i]);
			}
			fakewriter_tasks[i] = NULL;
		}
		kfree(fakewriter_tasks);
		fakewriter_tasks = NULL;
	}

1374
	if (stats_task) {
1375 1376 1377 1378 1379
		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
		kthread_stop(stats_task);
	}
	stats_task = NULL;

1380 1381 1382 1383 1384
	if (fqs_task) {
		VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
		kthread_stop(fqs_task);
	}
	fqs_task = NULL;
1385 1386 1387 1388 1389 1390
	if ((test_boost == 1 && cur_ops->can_boost) ||
	    test_boost == 2) {
		unregister_cpu_notifier(&rcutorture_cpu_nb);
		for_each_possible_cpu(i)
			rcutorture_booster_cleanup(i);
	}
1391

1392
	/* Wait for all RCU callbacks to fire.  */
1393 1394 1395

	if (cur_ops->cb_barrier != NULL)
		cur_ops->cb_barrier();
1396 1397

	rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1398

1399
	if (cur_ops->cleanup)
1400
		cur_ops->cleanup();
1401
	if (atomic_read(&n_rcu_torture_error))
1402
		rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1403
	else
1404
		rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1405 1406
}

1407
static int __init
1408 1409 1410 1411 1412
rcu_torture_init(void)
{
	int i;
	int cpu;
	int firsterr = 0;
1413
	static struct rcu_torture_ops *torture_ops[] =
1414
		{ &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1415
		  &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
1416 1417
		  &srcu_ops, &srcu_expedited_ops,
		  &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1418

1419 1420
	mutex_lock(&fullstop_mutex);

1421
	/* Process args and tell the world that the torturer is on the job. */
1422
	for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1423
		cur_ops = torture_ops[i];
1424
		if (strcmp(torture_type, cur_ops->name) == 0)
1425 1426
			break;
	}
1427
	if (i == ARRAY_SIZE(torture_ops)) {
1428
		printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1429
		       torture_type);
1430 1431 1432 1433
		printk(KERN_ALERT "rcu-torture types:");
		for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
			printk(KERN_ALERT " %s", torture_ops[i]->name);
		printk(KERN_ALERT "\n");
1434
		mutex_unlock(&fullstop_mutex);
1435
		return -EINVAL;
1436
	}
1437 1438 1439 1440 1441
	if (cur_ops->fqs == NULL && fqs_duration != 0) {
		printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
				  "fqs_duration, fqs disabled.\n");
		fqs_duration = 0;
	}
1442
	if (cur_ops->init)
1443 1444
		cur_ops->init(); /* no "goto unwind" prior to this point!!! */

1445 1446 1447 1448
	if (nreaders >= 0)
		nrealreaders = nreaders;
	else
		nrealreaders = 2 * num_online_cpus();
1449
	rcu_torture_print_module_parms(cur_ops, "Start of test");
1450
	fullstop = FULLSTOP_DONTSTOP;
1451 1452 1453 1454

	/* Set up the freelist. */

	INIT_LIST_HEAD(&rcu_torture_freelist);
1455
	for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1456
		rcu_tortures[i].rtort_mbtest = 0;
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
		list_add_tail(&rcu_tortures[i].rtort_free,
			      &rcu_torture_freelist);
	}

	/* Initialize the statistics so that each run gets its own numbers. */

	rcu_torture_current = NULL;
	rcu_torture_current_version = 0;
	atomic_set(&n_rcu_torture_alloc, 0);
	atomic_set(&n_rcu_torture_alloc_fail, 0);
	atomic_set(&n_rcu_torture_free, 0);
1468 1469
	atomic_set(&n_rcu_torture_mberror, 0);
	atomic_set(&n_rcu_torture_error, 0);
1470 1471 1472 1473
	n_rcu_torture_boost_ktrerror = 0;
	n_rcu_torture_boost_rterror = 0;
	n_rcu_torture_boost_failure = 0;
	n_rcu_torture_boosts = 0;
1474 1475
	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
		atomic_set(&rcu_torture_wcount[i], 0);
1476
	for_each_possible_cpu(cpu) {
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
			per_cpu(rcu_torture_count, cpu)[i] = 0;
			per_cpu(rcu_torture_batch, cpu)[i] = 0;
		}
	}

	/* Start up the kthreads. */

	VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
	writer_task = kthread_run(rcu_torture_writer, NULL,
				  "rcu_torture_writer");
	if (IS_ERR(writer_task)) {
		firsterr = PTR_ERR(writer_task);
		VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
		writer_task = NULL;
		goto unwind;
	}
1494
	fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1495
				   GFP_KERNEL);
1496 1497 1498 1499 1500 1501 1502 1503
	if (fakewriter_tasks == NULL) {
		VERBOSE_PRINTK_ERRSTRING("out of memory");
		firsterr = -ENOMEM;
		goto unwind;
	}
	for (i = 0; i < nfakewriters; i++) {
		VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
		fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1504
						  "rcu_torture_fakewriter");
1505 1506 1507 1508 1509 1510 1511
		if (IS_ERR(fakewriter_tasks[i])) {
			firsterr = PTR_ERR(fakewriter_tasks[i]);
			VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
			fakewriter_tasks[i] = NULL;
			goto unwind;
		}
	}
1512
	reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
			       GFP_KERNEL);
	if (reader_tasks == NULL) {
		VERBOSE_PRINTK_ERRSTRING("out of memory");
		firsterr = -ENOMEM;
		goto unwind;
	}
	for (i = 0; i < nrealreaders; i++) {
		VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
		reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
					      "rcu_torture_reader");
		if (IS_ERR(reader_tasks[i])) {
			firsterr = PTR_ERR(reader_tasks[i]);
			VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
			reader_tasks[i] = NULL;
			goto unwind;
		}
	}
	if (stat_interval > 0) {
		VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
		stats_task = kthread_run(rcu_torture_stats, NULL,
					"rcu_torture_stats");
		if (IS_ERR(stats_task)) {
			firsterr = PTR_ERR(stats_task);
			VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
			stats_task = NULL;
			goto unwind;
		}
	}
1541 1542
	if (test_no_idle_hz) {
		rcu_idle_cpu = num_online_cpus() - 1;
R
Rusty Russell 已提交
1543 1544 1545 1546 1547 1548 1549

		if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
			firsterr = -ENOMEM;
			VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
			goto unwind;
		}

1550 1551 1552 1553
		/* Create the shuffler thread */
		shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
					  "rcu_torture_shuffle");
		if (IS_ERR(shuffler_task)) {
R
Rusty Russell 已提交
1554
			free_cpumask_var(shuffle_tmp_mask);
1555 1556 1557 1558 1559 1560
			firsterr = PTR_ERR(shuffler_task);
			VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
			shuffler_task = NULL;
			goto unwind;
		}
	}
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	if (stutter < 0)
		stutter = 0;
	if (stutter) {
		/* Create the stutter thread */
		stutter_task = kthread_run(rcu_torture_stutter, NULL,
					  "rcu_torture_stutter");
		if (IS_ERR(stutter_task)) {
			firsterr = PTR_ERR(stutter_task);
			VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
			stutter_task = NULL;
			goto unwind;
		}
	}
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	if (fqs_duration < 0)
		fqs_duration = 0;
	if (fqs_duration) {
		/* Create the stutter thread */
		fqs_task = kthread_run(rcu_torture_fqs, NULL,
				       "rcu_torture_fqs");
		if (IS_ERR(fqs_task)) {
			firsterr = PTR_ERR(fqs_task);
			VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
			fqs_task = NULL;
			goto unwind;
		}
	}
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	if (test_boost_interval < 1)
		test_boost_interval = 1;
	if (test_boost_duration < 2)
		test_boost_duration = 2;
	if ((test_boost == 1 && cur_ops->can_boost) ||
	    test_boost == 2) {
		int retval;

		boost_starttime = jiffies + test_boost_interval * HZ;
		register_cpu_notifier(&rcutorture_cpu_nb);
		for_each_possible_cpu(i) {
			if (cpu_is_offline(i))
				continue;  /* Heuristic: CPU can go offline. */
			retval = rcutorture_booster_init(i);
			if (retval < 0) {
				firsterr = retval;
				goto unwind;
			}
		}
	}
	register_reboot_notifier(&rcutorture_shutdown_nb);
1608
	rcutorture_record_test_transition();
1609
	mutex_unlock(&fullstop_mutex);
1610 1611 1612
	return 0;

unwind:
1613
	mutex_unlock(&fullstop_mutex);
1614 1615 1616 1617 1618 1619
	rcu_torture_cleanup();
	return firsterr;
}

module_init(rcu_torture_init);
module_exit(rcu_torture_cleanup);