Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * Read-Copy Update module-based torture test facility
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software
  16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17 *
  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 * See also:  Documentation/RCU/torture.txt
  24 */
  25#include <linux/types.h>
  26#include <linux/kernel.h>
  27#include <linux/init.h>
  28#include <linux/module.h>
  29#include <linux/kthread.h>
  30#include <linux/err.h>
  31#include <linux/spinlock.h>
  32#include <linux/smp.h>
  33#include <linux/rcupdate.h>
  34#include <linux/interrupt.h>
  35#include <linux/sched.h>
  36#include <linux/atomic.h>
  37#include <linux/bitops.h>
  38#include <linux/completion.h>
  39#include <linux/moduleparam.h>
  40#include <linux/percpu.h>
  41#include <linux/notifier.h>
  42#include <linux/reboot.h>
  43#include <linux/freezer.h>
  44#include <linux/cpu.h>
  45#include <linux/delay.h>
  46#include <linux/stat.h>
  47#include <linux/srcu.h>
  48#include <linux/slab.h>
  49#include <asm/byteorder.h>
  50
  51MODULE_LICENSE("GPL");
  52MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  53	      "Josh Triplett <josh@freedesktop.org>");
  54
  55static int nreaders = -1;	/* # reader threads, defaults to 2*ncpus */
  56static int nfakewriters = 4;	/* # fake writer threads */
  57static int stat_interval;	/* Interval between stats, in seconds. */
  58				/*  Defaults to "only at end of test". */
  59static int verbose;		/* Print more debug info. */
  60static int test_no_idle_hz;	/* Test RCU's support for tickless idle CPUs. */
  61static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  62static int stutter = 5;		/* Start/stop testing interval (in sec) */
  63static int irqreader = 1;	/* RCU readers from irq (timers). */
  64static int fqs_duration = 0;	/* Duration of bursts (us), 0 to disable. */
  65static int fqs_holdoff = 0;	/* Hold time within burst (us). */
  66static int fqs_stutter = 3;	/* Wait time between bursts (s). */
 
 
 
 
 
 
  67static int test_boost = 1;	/* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
  68static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
  69static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
  70static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  71
  72module_param(nreaders, int, 0444);
  73MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  74module_param(nfakewriters, int, 0444);
  75MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  76module_param(stat_interval, int, 0444);
  77MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  78module_param(verbose, bool, 0444);
  79MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  80module_param(test_no_idle_hz, bool, 0444);
  81MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  82module_param(shuffle_interval, int, 0444);
  83MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  84module_param(stutter, int, 0444);
  85MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  86module_param(irqreader, int, 0444);
  87MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  88module_param(fqs_duration, int, 0444);
  89MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  90module_param(fqs_holdoff, int, 0444);
  91MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  92module_param(fqs_stutter, int, 0444);
  93MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
 
 
 
 
 
 
 
 
 
 
 
 
  94module_param(test_boost, int, 0444);
  95MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  96module_param(test_boost_interval, int, 0444);
  97MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
  98module_param(test_boost_duration, int, 0444);
  99MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
 100module_param(torture_type, charp, 0444);
 101MODULE_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
 111static char printk_buf[4096];
 112
 113static int nrealreaders;
 114static struct task_struct *writer_task;
 115static struct task_struct **fakewriter_tasks;
 116static struct task_struct **reader_tasks;
 117static struct task_struct *stats_task;
 118static struct task_struct *shuffler_task;
 119static struct task_struct *stutter_task;
 120static struct task_struct *fqs_task;
 121static struct task_struct *boost_tasks[NR_CPUS];
 
 
 
 
 
 
 
 122
 123#define RCU_TORTURE_PIPE_LEN 10
 124
 125struct rcu_torture {
 126	struct rcu_head rtort_rcu;
 127	int rtort_pipe_count;
 128	struct list_head rtort_free;
 129	int rtort_mbtest;
 130};
 131
 132static LIST_HEAD(rcu_torture_freelist);
 133static struct rcu_torture __rcu *rcu_torture_current;
 134static unsigned long rcu_torture_current_version;
 135static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
 136static DEFINE_SPINLOCK(rcu_torture_lock);
 137static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
 138	{ 0 };
 139static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
 140	{ 0 };
 141static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
 142static atomic_t n_rcu_torture_alloc;
 143static atomic_t n_rcu_torture_alloc_fail;
 144static atomic_t n_rcu_torture_free;
 145static atomic_t n_rcu_torture_mberror;
 146static atomic_t n_rcu_torture_error;
 
 147static long n_rcu_torture_boost_ktrerror;
 148static long n_rcu_torture_boost_rterror;
 149static long n_rcu_torture_boost_failure;
 150static long n_rcu_torture_boosts;
 151static long n_rcu_torture_timers;
 
 
 
 
 
 
 152static struct list_head rcu_torture_removed;
 153static cpumask_var_t shuffle_tmp_mask;
 154
 155static int stutter_pause_test;
 156
 157#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
 158#define RCUTORTURE_RUNNABLE_INIT 1
 159#else
 160#define RCUTORTURE_RUNNABLE_INIT 0
 161#endif
 162int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
 
 
 163
 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
 
 170static unsigned long boost_starttime;	/* jiffies of next boost test start. */
 171DEFINE_MUTEX(boost_mutex);		/* protect setting boost_starttime */
 172					/*  and boost task create/destroy. */
 
 
 
 
 173
 174/* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
 175
 176#define FULLSTOP_DONTSTOP 0	/* Normal operation. */
 177#define FULLSTOP_SHUTDOWN 1	/* System shutdown with rcutorture running. */
 178#define FULLSTOP_RMMOD    2	/* Normal rmmod of rcutorture. */
 179static int fullstop = FULLSTOP_RMMOD;
 180/*
 181 * Protect fullstop transitions and spawning of kthreads.
 182 */
 183static DEFINE_MUTEX(fullstop_mutex);
 184
 
 
 
 185/*
 186 * Detect and respond to a system shutdown.
 187 */
 188static int
 189rcutorture_shutdown_notify(struct notifier_block *unused1,
 190			   unsigned long unused2, void *unused3)
 191{
 192	mutex_lock(&fullstop_mutex);
 193	if (fullstop == FULLSTOP_DONTSTOP)
 194		fullstop = FULLSTOP_SHUTDOWN;
 195	else
 196		printk(KERN_WARNING /* but going down anyway, so... */
 197		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
 198	mutex_unlock(&fullstop_mutex);
 199	return NOTIFY_DONE;
 200}
 201
 202/*
 203 * Absorb kthreads into a kernel function that won't return, so that
 204 * they won't ever access module text or data again.
 205 */
 206static void rcutorture_shutdown_absorb(char *title)
 207{
 208	if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
 209		printk(KERN_NOTICE
 210		       "rcutorture thread %s parking due to system shutdown\n",
 211		       title);
 212		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
 213	}
 214}
 215
 216/*
 217 * Allocate an element from the rcu_tortures pool.
 218 */
 219static struct rcu_torture *
 220rcu_torture_alloc(void)
 221{
 222	struct list_head *p;
 223
 224	spin_lock_bh(&rcu_torture_lock);
 225	if (list_empty(&rcu_torture_freelist)) {
 226		atomic_inc(&n_rcu_torture_alloc_fail);
 227		spin_unlock_bh(&rcu_torture_lock);
 228		return NULL;
 229	}
 230	atomic_inc(&n_rcu_torture_alloc);
 231	p = rcu_torture_freelist.next;
 232	list_del_init(p);
 233	spin_unlock_bh(&rcu_torture_lock);
 234	return container_of(p, struct rcu_torture, rtort_free);
 235}
 236
 237/*
 238 * Free an element to the rcu_tortures pool.
 239 */
 240static void
 241rcu_torture_free(struct rcu_torture *p)
 242{
 243	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
 249struct rcu_random_state {
 250	unsigned long rrs_state;
 251	long rrs_count;
 252};
 253
 254#define RCU_RANDOM_MULT 39916801  /* prime */
 255#define RCU_RANDOM_ADD	479001701 /* prime */
 256#define RCU_RANDOM_REFRESH 10000
 257
 258#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
 259
 260/*
 261 * Crude but fast random-number generator.  Uses a linear congruential
 262 * generator, with occasional help from cpu_clock().
 263 */
 264static unsigned long
 265rcu_random(struct rcu_random_state *rrsp)
 266{
 267	if (--rrsp->rrs_count < 0) {
 268		rrsp->rrs_state += (unsigned long)local_clock();
 269		rrsp->rrs_count = RCU_RANDOM_REFRESH;
 270	}
 271	rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
 272	return swahw32(rrsp->rrs_state);
 273}
 274
 275static void
 276rcu_stutter_wait(char *title)
 277{
 278	while (stutter_pause_test || !rcutorture_runnable) {
 279		if (rcutorture_runnable)
 280			schedule_timeout_interruptible(1);
 281		else
 282			schedule_timeout_interruptible(round_jiffies_relative(HZ));
 283		rcutorture_shutdown_absorb(title);
 284	}
 285}
 286
 287/*
 288 * Operations vector for selecting different types of tests.
 289 */
 290
 291struct rcu_torture_ops {
 292	void (*init)(void);
 293	void (*cleanup)(void);
 294	int (*readlock)(void);
 295	void (*read_delay)(struct rcu_random_state *rrsp);
 296	void (*readunlock)(int idx);
 297	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	char *name;
 306};
 307
 308static struct rcu_torture_ops *cur_ops;
 309
 310/*
 311 * Definitions for rcu torture testing.
 312 */
 313
 314static int rcu_torture_read_lock(void) __acquires(RCU)
 315{
 316	rcu_read_lock();
 317	return 0;
 318}
 319
 320static void rcu_read_delay(struct rcu_random_state *rrsp)
 321{
 322	const unsigned long shortdelay_us = 200;
 323	const unsigned long longdelay_ms = 50;
 324
 325	/* We want a short delay sometimes to make a reader delay the grace
 326	 * period, and we want a long delay occasionally to trigger
 327	 * force_quiescent_state. */
 328
 329	if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
 330		mdelay(longdelay_ms);
 331	if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
 332		udelay(shortdelay_us);
 333#ifdef CONFIG_PREEMPT
 334	if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
 335		preempt_schedule();  /* No QS if preempt_disable() in effect */
 336#endif
 337}
 338
 339static void rcu_torture_read_unlock(int idx) __releases(RCU)
 340{
 341	rcu_read_unlock();
 342}
 343
 344static int rcu_torture_completed(void)
 345{
 346	return rcu_batches_completed();
 347}
 348
 349static void
 350rcu_torture_cb(struct rcu_head *p)
 351{
 352	int i;
 353	struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
 354
 355	if (fullstop != FULLSTOP_DONTSTOP) {
 356		/* Test is ending, just drop callbacks on the floor. */
 357		/* The next initialization will pick up the pieces. */
 358		return;
 359	}
 360	i = rp->rtort_pipe_count;
 361	if (i > RCU_TORTURE_PIPE_LEN)
 362		i = RCU_TORTURE_PIPE_LEN;
 363	atomic_inc(&rcu_torture_wcount[i]);
 364	if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
 365		rp->rtort_mbtest = 0;
 366		rcu_torture_free(rp);
 367	} else
 368		cur_ops->deferred_free(rp);
 369}
 370
 371static int rcu_no_completed(void)
 372{
 373	return 0;
 374}
 375
 376static void rcu_torture_deferred_free(struct rcu_torture *p)
 377{
 378	call_rcu(&p->rtort_rcu, rcu_torture_cb);
 379}
 380
 381static struct rcu_torture_ops rcu_ops = {
 382	.init		= NULL,
 383	.cleanup	= NULL,
 384	.readlock	= rcu_torture_read_lock,
 385	.read_delay	= rcu_read_delay,
 386	.readunlock	= rcu_torture_read_unlock,
 387	.completed	= rcu_torture_completed,
 388	.deferred_free	= rcu_torture_deferred_free,
 389	.sync		= synchronize_rcu,
 
 390	.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
 398static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
 399{
 400	int i;
 401	struct rcu_torture *rp;
 402	struct rcu_torture *rp1;
 403
 404	cur_ops->sync();
 405	list_add(&p->rtort_free, &rcu_torture_removed);
 406	list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
 407		i = rp->rtort_pipe_count;
 408		if (i > RCU_TORTURE_PIPE_LEN)
 409			i = RCU_TORTURE_PIPE_LEN;
 410		atomic_inc(&rcu_torture_wcount[i]);
 411		if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
 412			rp->rtort_mbtest = 0;
 413			list_del(&rp->rtort_free);
 414			rcu_torture_free(rp);
 415		}
 416	}
 417}
 418
 419static void rcu_sync_torture_init(void)
 420{
 421	INIT_LIST_HEAD(&rcu_torture_removed);
 422}
 423
 424static struct rcu_torture_ops rcu_sync_ops = {
 425	.init		= rcu_sync_torture_init,
 426	.cleanup	= NULL,
 427	.readlock	= rcu_torture_read_lock,
 428	.read_delay	= rcu_read_delay,
 429	.readunlock	= rcu_torture_read_unlock,
 430	.completed	= rcu_torture_completed,
 431	.deferred_free	= rcu_sync_torture_deferred_free,
 432	.sync		= synchronize_rcu,
 
 433	.cb_barrier	= NULL,
 434	.fqs		= rcu_force_quiescent_state,
 435	.stats		= NULL,
 436	.irq_capable	= 1,
 437	.can_boost	= rcu_can_boost(),
 438	.name		= "rcu_sync"
 439};
 440
 441static struct rcu_torture_ops rcu_expedited_ops = {
 442	.init		= rcu_sync_torture_init,
 443	.cleanup	= NULL,
 444	.readlock	= rcu_torture_read_lock,
 445	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 446	.readunlock	= rcu_torture_read_unlock,
 447	.completed	= rcu_no_completed,
 448	.deferred_free	= rcu_sync_torture_deferred_free,
 449	.sync		= synchronize_rcu_expedited,
 
 450	.cb_barrier	= NULL,
 451	.fqs		= rcu_force_quiescent_state,
 452	.stats		= NULL,
 453	.irq_capable	= 1,
 454	.can_boost	= rcu_can_boost(),
 455	.name		= "rcu_expedited"
 456};
 457
 458/*
 459 * Definitions for rcu_bh torture testing.
 460 */
 461
 462static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
 463{
 464	rcu_read_lock_bh();
 465	return 0;
 466}
 467
 468static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
 469{
 470	rcu_read_unlock_bh();
 471}
 472
 473static int rcu_bh_torture_completed(void)
 474{
 475	return rcu_batches_completed_bh();
 476}
 477
 478static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
 479{
 480	call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
 481}
 482
 483struct rcu_bh_torture_synchronize {
 484	struct rcu_head head;
 485	struct completion completion;
 486};
 487
 488static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
 489{
 490	struct rcu_bh_torture_synchronize *rcu;
 491
 492	rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
 493	complete(&rcu->completion);
 494}
 495
 496static void rcu_bh_torture_synchronize(void)
 497{
 498	struct rcu_bh_torture_synchronize rcu;
 499
 500	init_rcu_head_on_stack(&rcu.head);
 501	init_completion(&rcu.completion);
 502	call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
 503	wait_for_completion(&rcu.completion);
 504	destroy_rcu_head_on_stack(&rcu.head);
 505}
 506
 507static struct rcu_torture_ops rcu_bh_ops = {
 508	.init		= NULL,
 509	.cleanup	= NULL,
 510	.readlock	= rcu_bh_torture_read_lock,
 511	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 512	.readunlock	= rcu_bh_torture_read_unlock,
 513	.completed	= rcu_bh_torture_completed,
 514	.deferred_free	= rcu_bh_torture_deferred_free,
 515	.sync		= rcu_bh_torture_synchronize,
 
 516	.cb_barrier	= rcu_barrier_bh,
 517	.fqs		= rcu_bh_force_quiescent_state,
 518	.stats		= NULL,
 519	.irq_capable	= 1,
 520	.name		= "rcu_bh"
 521};
 522
 523static struct rcu_torture_ops rcu_bh_sync_ops = {
 524	.init		= rcu_sync_torture_init,
 525	.cleanup	= NULL,
 526	.readlock	= rcu_bh_torture_read_lock,
 527	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 528	.readunlock	= rcu_bh_torture_read_unlock,
 529	.completed	= rcu_bh_torture_completed,
 530	.deferred_free	= rcu_sync_torture_deferred_free,
 531	.sync		= rcu_bh_torture_synchronize,
 
 532	.cb_barrier	= NULL,
 533	.fqs		= rcu_bh_force_quiescent_state,
 534	.stats		= NULL,
 535	.irq_capable	= 1,
 536	.name		= "rcu_bh_sync"
 537};
 538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 539/*
 540 * Definitions for srcu torture testing.
 541 */
 542
 543static struct srcu_struct srcu_ctl;
 544
 545static void srcu_torture_init(void)
 546{
 547	init_srcu_struct(&srcu_ctl);
 548	rcu_sync_torture_init();
 549}
 550
 551static void srcu_torture_cleanup(void)
 552{
 553	synchronize_srcu(&srcu_ctl);
 554	cleanup_srcu_struct(&srcu_ctl);
 555}
 556
 557static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
 558{
 559	return srcu_read_lock(&srcu_ctl);
 560}
 561
 562static void srcu_read_delay(struct rcu_random_state *rrsp)
 563{
 564	long delay;
 565	const long uspertick = 1000000 / HZ;
 566	const long longdelay = 10;
 567
 568	/* We want there to be long-running readers, but not all the time. */
 569
 570	delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
 571	if (!delay)
 572		schedule_timeout_interruptible(longdelay);
 573	else
 574		rcu_read_delay(rrsp);
 575}
 576
 577static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
 578{
 579	srcu_read_unlock(&srcu_ctl, idx);
 580}
 581
 582static int srcu_torture_completed(void)
 583{
 584	return srcu_batches_completed(&srcu_ctl);
 585}
 586
 
 
 
 
 
 587static void srcu_torture_synchronize(void)
 588{
 589	synchronize_srcu(&srcu_ctl);
 590}
 591
 592static int srcu_torture_stats(char *page)
 593{
 594	int cnt = 0;
 595	int cpu;
 596	int idx = srcu_ctl.completed & 0x1;
 597
 598	cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
 599		       torture_type, TORTURE_FLAG, idx);
 600	for_each_possible_cpu(cpu) {
 601		cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
 602			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
 603			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
 604	}
 605	cnt += sprintf(&page[cnt], "\n");
 606	return cnt;
 607}
 608
 609static struct rcu_torture_ops srcu_ops = {
 610	.init		= srcu_torture_init,
 611	.cleanup	= srcu_torture_cleanup,
 612	.readlock	= srcu_torture_read_lock,
 613	.read_delay	= srcu_read_delay,
 614	.readunlock	= srcu_torture_read_unlock,
 615	.completed	= srcu_torture_completed,
 616	.deferred_free	= rcu_sync_torture_deferred_free,
 617	.sync		= srcu_torture_synchronize,
 
 618	.cb_barrier	= NULL,
 619	.stats		= srcu_torture_stats,
 620	.name		= "srcu"
 621};
 622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 623static void srcu_torture_synchronize_expedited(void)
 624{
 625	synchronize_srcu_expedited(&srcu_ctl);
 626}
 627
 628static struct rcu_torture_ops srcu_expedited_ops = {
 629	.init		= srcu_torture_init,
 630	.cleanup	= srcu_torture_cleanup,
 631	.readlock	= srcu_torture_read_lock,
 632	.read_delay	= srcu_read_delay,
 633	.readunlock	= srcu_torture_read_unlock,
 634	.completed	= srcu_torture_completed,
 635	.deferred_free	= rcu_sync_torture_deferred_free,
 636	.sync		= srcu_torture_synchronize_expedited,
 
 637	.cb_barrier	= NULL,
 638	.stats		= srcu_torture_stats,
 639	.name		= "srcu_expedited"
 640};
 641
 642/*
 643 * Definitions for sched torture testing.
 644 */
 645
 646static int sched_torture_read_lock(void)
 647{
 648	preempt_disable();
 649	return 0;
 650}
 651
 652static void sched_torture_read_unlock(int idx)
 653{
 654	preempt_enable();
 655}
 656
 657static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
 658{
 659	call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
 660}
 661
 662static void sched_torture_synchronize(void)
 663{
 664	synchronize_sched();
 665}
 666
 667static struct rcu_torture_ops sched_ops = {
 668	.init		= rcu_sync_torture_init,
 669	.cleanup	= NULL,
 670	.readlock	= sched_torture_read_lock,
 671	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 672	.readunlock	= sched_torture_read_unlock,
 673	.completed	= rcu_no_completed,
 674	.deferred_free	= rcu_sched_torture_deferred_free,
 675	.sync		= sched_torture_synchronize,
 676	.cb_barrier	= rcu_barrier_sched,
 677	.fqs		= rcu_sched_force_quiescent_state,
 678	.stats		= NULL,
 679	.irq_capable	= 1,
 680	.name		= "sched"
 681};
 682
 683static struct rcu_torture_ops sched_sync_ops = {
 684	.init		= rcu_sync_torture_init,
 685	.cleanup	= NULL,
 686	.readlock	= sched_torture_read_lock,
 687	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 688	.readunlock	= sched_torture_read_unlock,
 689	.completed	= rcu_no_completed,
 690	.deferred_free	= rcu_sync_torture_deferred_free,
 691	.sync		= sched_torture_synchronize,
 692	.cb_barrier	= NULL,
 693	.fqs		= rcu_sched_force_quiescent_state,
 694	.stats		= NULL,
 695	.name		= "sched_sync"
 696};
 697
 698static struct rcu_torture_ops sched_expedited_ops = {
 699	.init		= rcu_sync_torture_init,
 700	.cleanup	= NULL,
 701	.readlock	= sched_torture_read_lock,
 702	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 703	.readunlock	= sched_torture_read_unlock,
 704	.completed	= rcu_no_completed,
 705	.deferred_free	= rcu_sync_torture_deferred_free,
 706	.sync		= synchronize_sched_expedited,
 707	.cb_barrier	= NULL,
 708	.fqs		= rcu_sched_force_quiescent_state,
 709	.stats		= NULL,
 710	.irq_capable	= 1,
 711	.name		= "sched_expedited"
 712};
 713
 714/*
 715 * 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
 731	smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
 732	rbip->inflight = 0;
 733}
 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
 743	VERBOSE_PRINTK_STRING("rcu_torture_boost started");
 744
 745	/* Set real-time priority. */
 746	sp.sched_priority = 1;
 747	if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
 748		VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
 749		n_rcu_torture_boost_rterror++;
 750	}
 751
 752	init_rcu_head_on_stack(&rbi.rcu);
 753	/* Each pass through the following loop does one boost-test cycle. */
 754	do {
 755		/* Wait for the next test interval. */
 756		oldstarttime = boost_starttime;
 757		while (jiffies - oldstarttime > ULONG_MAX / 2) {
 758			schedule_timeout_uninterruptible(1);
 759			rcu_stutter_wait("rcu_torture_boost");
 760			if (kthread_should_stop() ||
 761			    fullstop != FULLSTOP_DONTSTOP)
 762				goto checkwait;
 763		}
 764
 765		/* Do one boost-test interval. */
 766		endtime = oldstarttime + test_boost_duration * HZ;
 767		call_rcu_time = jiffies;
 768		while (jiffies - endtime > ULONG_MAX / 2) {
 769			/* If we don't have a callback in flight, post one. */
 770			if (!rbi.inflight) {
 771				smp_mb(); /* RCU core before ->inflight = 1. */
 772				rbi.inflight = 1;
 773				call_rcu(&rbi.rcu, rcu_torture_boost_cb);
 774				if (jiffies - call_rcu_time >
 775					 test_boost_duration * HZ - HZ / 2) {
 776					VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
 777					n_rcu_torture_boost_failure++;
 778				}
 779				call_rcu_time = jiffies;
 780			}
 781			cond_resched();
 782			rcu_stutter_wait("rcu_torture_boost");
 783			if (kthread_should_stop() ||
 784			    fullstop != FULLSTOP_DONTSTOP)
 785				goto checkwait;
 786		}
 787
 788		/*
 789		 * Set the start time of the next test interval.
 790		 * Yes, this is vulnerable to long delays, but such
 791		 * delays simply cause a false negative for the next
 792		 * interval.  Besides, we are running at RT priority,
 793		 * so delays should be relatively rare.
 794		 */
 795		while (oldstarttime == boost_starttime) {
 
 796			if (mutex_trylock(&boost_mutex)) {
 797				boost_starttime = jiffies +
 798						  test_boost_interval * HZ;
 799				n_rcu_torture_boosts++;
 800				mutex_unlock(&boost_mutex);
 801				break;
 802			}
 803			schedule_timeout_uninterruptible(1);
 804		}
 805
 806		/* Go do the stutter. */
 807checkwait:	rcu_stutter_wait("rcu_torture_boost");
 808	} while (!kthread_should_stop() && fullstop  == FULLSTOP_DONTSTOP);
 809
 810	/* Clean up and exit. */
 811	VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
 812	destroy_rcu_head_on_stack(&rbi.rcu);
 813	rcutorture_shutdown_absorb("rcu_torture_boost");
 814	while (!kthread_should_stop() || rbi.inflight)
 815		schedule_timeout_uninterruptible(1);
 816	smp_mb(); /* order accesses to ->inflight before stack-frame death. */
 
 817	return 0;
 818}
 819
 820/*
 821 * RCU torture force-quiescent-state kthread.  Repeatedly induces
 822 * bursts of calls to force_quiescent_state(), increasing the probability
 823 * of occurrence of some important types of race conditions.
 824 */
 825static int
 826rcu_torture_fqs(void *arg)
 827{
 828	unsigned long fqs_resume_time;
 829	int fqs_burst_remaining;
 830
 831	VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
 832	do {
 833		fqs_resume_time = jiffies + fqs_stutter * HZ;
 834		while (jiffies - fqs_resume_time > LONG_MAX) {
 
 835			schedule_timeout_interruptible(1);
 836		}
 837		fqs_burst_remaining = fqs_duration;
 838		while (fqs_burst_remaining > 0) {
 
 839			cur_ops->fqs();
 840			udelay(fqs_holdoff);
 841			fqs_burst_remaining -= fqs_holdoff;
 842		}
 843		rcu_stutter_wait("rcu_torture_fqs");
 844	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
 845	VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
 846	rcutorture_shutdown_absorb("rcu_torture_fqs");
 847	while (!kthread_should_stop())
 848		schedule_timeout_uninterruptible(1);
 849	return 0;
 850}
 851
 852/*
 853 * RCU torture writer kthread.  Repeatedly substitutes a new structure
 854 * for that pointed to by rcu_torture_current, freeing the old structure
 855 * after a series of grace periods (the "pipeline").
 856 */
 857static int
 858rcu_torture_writer(void *arg)
 859{
 860	int i;
 861	long oldbatch = rcu_batches_completed();
 862	struct rcu_torture *rp;
 863	struct rcu_torture *old_rp;
 864	static DEFINE_RCU_RANDOM(rand);
 865
 866	VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
 867	set_user_nice(current, 19);
 868
 869	do {
 870		schedule_timeout_uninterruptible(1);
 871		rp = rcu_torture_alloc();
 872		if (rp == NULL)
 873			continue;
 874		rp->rtort_pipe_count = 0;
 875		udelay(rcu_random(&rand) & 0x3ff);
 876		old_rp = rcu_dereference_check(rcu_torture_current,
 877					       current == writer_task);
 878		rp->rtort_mbtest = 1;
 879		rcu_assign_pointer(rcu_torture_current, rp);
 880		smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
 881		if (old_rp) {
 882			i = old_rp->rtort_pipe_count;
 883			if (i > RCU_TORTURE_PIPE_LEN)
 884				i = RCU_TORTURE_PIPE_LEN;
 885			atomic_inc(&rcu_torture_wcount[i]);
 886			old_rp->rtort_pipe_count++;
 887			cur_ops->deferred_free(old_rp);
 888		}
 889		rcutorture_record_progress(++rcu_torture_current_version);
 890		oldbatch = cur_ops->completed();
 891		rcu_stutter_wait("rcu_torture_writer");
 892	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
 893	VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
 894	rcutorture_shutdown_absorb("rcu_torture_writer");
 895	while (!kthread_should_stop())
 896		schedule_timeout_uninterruptible(1);
 897	return 0;
 898}
 899
 900/*
 901 * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
 902 * delay between calls.
 903 */
 904static int
 905rcu_torture_fakewriter(void *arg)
 906{
 907	DEFINE_RCU_RANDOM(rand);
 908
 909	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
 910	set_user_nice(current, 19);
 911
 912	do {
 913		schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
 914		udelay(rcu_random(&rand) & 0x3ff);
 915		cur_ops->sync();
 916		rcu_stutter_wait("rcu_torture_fakewriter");
 917	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
 918
 919	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
 920	rcutorture_shutdown_absorb("rcu_torture_fakewriter");
 921	while (!kthread_should_stop())
 922		schedule_timeout_uninterruptible(1);
 923	return 0;
 924}
 925
 
 
 
 
 
 
 
 
 
 
 
 
 926/*
 927 * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
 928 * incrementing the corresponding element of the pipeline array.  The
 929 * counter in the element should never be greater than 1, otherwise, the
 930 * RCU implementation is broken.
 931 */
 932static void rcu_torture_timer(unsigned long unused)
 933{
 934	int idx;
 935	int completed;
 936	static DEFINE_RCU_RANDOM(rand);
 937	static DEFINE_SPINLOCK(rand_lock);
 938	struct rcu_torture *p;
 939	int pipe_count;
 940
 941	idx = cur_ops->readlock();
 942	completed = cur_ops->completed();
 943	p = rcu_dereference_check(rcu_torture_current,
 944				  rcu_read_lock_bh_held() ||
 945				  rcu_read_lock_sched_held() ||
 946				  srcu_read_lock_held(&srcu_ctl));
 947	if (p == NULL) {
 948		/* Leave because rcu_torture_writer is not yet underway */
 949		cur_ops->readunlock(idx);
 950		return;
 951	}
 
 952	if (p->rtort_mbtest == 0)
 953		atomic_inc(&n_rcu_torture_mberror);
 954	spin_lock(&rand_lock);
 955	cur_ops->read_delay(&rand);
 956	n_rcu_torture_timers++;
 957	spin_unlock(&rand_lock);
 958	preempt_disable();
 959	pipe_count = p->rtort_pipe_count;
 960	if (pipe_count > RCU_TORTURE_PIPE_LEN) {
 961		/* Should not happen, but... */
 962		pipe_count = RCU_TORTURE_PIPE_LEN;
 963	}
 
 
 964	__this_cpu_inc(rcu_torture_count[pipe_count]);
 965	completed = cur_ops->completed() - completed;
 966	if (completed > RCU_TORTURE_PIPE_LEN) {
 967		/* Should not happen, but... */
 968		completed = RCU_TORTURE_PIPE_LEN;
 969	}
 970	__this_cpu_inc(rcu_torture_batch[completed]);
 971	preempt_enable();
 972	cur_ops->readunlock(idx);
 973}
 974
 975/*
 976 * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
 977 * incrementing the corresponding element of the pipeline array.  The
 978 * counter in the element should never be greater than 1, otherwise, the
 979 * RCU implementation is broken.
 980 */
 981static int
 982rcu_torture_reader(void *arg)
 983{
 984	int completed;
 985	int idx;
 986	DEFINE_RCU_RANDOM(rand);
 987	struct rcu_torture *p;
 988	int pipe_count;
 989	struct timer_list t;
 990
 991	VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
 992	set_user_nice(current, 19);
 993	if (irqreader && cur_ops->irq_capable)
 994		setup_timer_on_stack(&t, rcu_torture_timer, 0);
 995
 996	do {
 997		if (irqreader && cur_ops->irq_capable) {
 998			if (!timer_pending(&t))
 999				mod_timer(&t, jiffies + 1);
1000		}
1001		idx = cur_ops->readlock();
1002		completed = cur_ops->completed();
1003		p = rcu_dereference_check(rcu_torture_current,
1004					  rcu_read_lock_bh_held() ||
1005					  rcu_read_lock_sched_held() ||
1006					  srcu_read_lock_held(&srcu_ctl));
1007		if (p == NULL) {
1008			/* Wait for rcu_torture_writer to get underway */
1009			cur_ops->readunlock(idx);
1010			schedule_timeout_interruptible(HZ);
1011			continue;
1012		}
 
1013		if (p->rtort_mbtest == 0)
1014			atomic_inc(&n_rcu_torture_mberror);
1015		cur_ops->read_delay(&rand);
1016		preempt_disable();
1017		pipe_count = p->rtort_pipe_count;
1018		if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1019			/* Should not happen, but... */
1020			pipe_count = RCU_TORTURE_PIPE_LEN;
1021		}
 
 
1022		__this_cpu_inc(rcu_torture_count[pipe_count]);
1023		completed = cur_ops->completed() - completed;
1024		if (completed > RCU_TORTURE_PIPE_LEN) {
1025			/* Should not happen, but... */
1026			completed = RCU_TORTURE_PIPE_LEN;
1027		}
1028		__this_cpu_inc(rcu_torture_batch[completed]);
1029		preempt_enable();
1030		cur_ops->readunlock(idx);
1031		schedule();
1032		rcu_stutter_wait("rcu_torture_reader");
1033	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1034	VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1035	rcutorture_shutdown_absorb("rcu_torture_reader");
1036	if (irqreader && cur_ops->irq_capable)
1037		del_timer_sync(&t);
1038	while (!kthread_should_stop())
1039		schedule_timeout_uninterruptible(1);
1040	return 0;
1041}
1042
1043/*
1044 * Create an RCU-torture statistics message in the specified buffer.
1045 */
1046static int
1047rcu_torture_printk(char *page)
1048{
1049	int cnt = 0;
1050	int cpu;
1051	int i;
1052	long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1053	long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1054
1055	for_each_possible_cpu(cpu) {
1056		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1057			pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1058			batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1059		}
1060	}
1061	for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1062		if (pipesummary[i] != 0)
1063			break;
1064	}
1065	cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1066	cnt += sprintf(&page[cnt],
1067		       "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
1068		       "rtmbe: %d rtbke: %ld rtbre: %ld "
1069		       "rtbf: %ld rtb: %ld nt: %ld",
 
 
1070		       rcu_torture_current,
1071		       rcu_torture_current_version,
1072		       list_empty(&rcu_torture_freelist),
1073		       atomic_read(&n_rcu_torture_alloc),
1074		       atomic_read(&n_rcu_torture_alloc_fail),
1075		       atomic_read(&n_rcu_torture_free),
1076		       atomic_read(&n_rcu_torture_mberror),
1077		       n_rcu_torture_boost_ktrerror,
1078		       n_rcu_torture_boost_rterror,
1079		       n_rcu_torture_boost_failure,
1080		       n_rcu_torture_boosts,
1081		       n_rcu_torture_timers);
 
 
 
 
 
 
 
 
1082	if (atomic_read(&n_rcu_torture_mberror) != 0 ||
 
1083	    n_rcu_torture_boost_ktrerror != 0 ||
1084	    n_rcu_torture_boost_rterror != 0 ||
1085	    n_rcu_torture_boost_failure != 0)
1086		cnt += sprintf(&page[cnt], " !!!");
1087	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1088	if (i > 1) {
1089		cnt += sprintf(&page[cnt], "!!! ");
1090		atomic_inc(&n_rcu_torture_error);
1091		WARN_ON_ONCE(1);
1092	}
1093	cnt += sprintf(&page[cnt], "Reader Pipe: ");
1094	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1095		cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1096	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1097	cnt += sprintf(&page[cnt], "Reader Batch: ");
1098	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1099		cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1100	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1101	cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1102	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1103		cnt += sprintf(&page[cnt], " %d",
1104			       atomic_read(&rcu_torture_wcount[i]));
1105	}
1106	cnt += sprintf(&page[cnt], "\n");
1107	if (cur_ops->stats)
1108		cnt += cur_ops->stats(&page[cnt]);
1109	return cnt;
1110}
1111
1112/*
1113 * Print torture statistics.  Caller must ensure that there is only
1114 * one call to this function at a given time!!!  This is normally
1115 * accomplished by relying on the module system to only have one copy
1116 * of the module loaded, and then by giving the rcu_torture_stats
1117 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1118 * thread is not running).
1119 */
1120static void
1121rcu_torture_stats_print(void)
1122{
1123	int cnt;
1124
1125	cnt = rcu_torture_printk(printk_buf);
1126	printk(KERN_ALERT "%s", printk_buf);
1127}
1128
1129/*
1130 * Periodically prints torture statistics, if periodic statistics printing
1131 * was specified via the stat_interval module parameter.
1132 *
1133 * No need to worry about fullstop here, since this one doesn't reference
1134 * volatile state or register callbacks.
1135 */
1136static int
1137rcu_torture_stats(void *arg)
1138{
1139	VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1140	do {
1141		schedule_timeout_interruptible(stat_interval * HZ);
1142		rcu_torture_stats_print();
1143		rcutorture_shutdown_absorb("rcu_torture_stats");
1144	} while (!kthread_should_stop());
1145	VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1146	return 0;
1147}
1148
1149static int rcu_idle_cpu;	/* Force all torture tasks off this CPU */
1150
1151/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1152 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1153 */
1154static void rcu_torture_shuffle_tasks(void)
1155{
1156	int i;
1157
1158	cpumask_setall(shuffle_tmp_mask);
1159	get_online_cpus();
1160
1161	/* No point in shuffling if there is only one online CPU (ex: UP) */
1162	if (num_online_cpus() == 1) {
1163		put_online_cpus();
1164		return;
1165	}
1166
1167	if (rcu_idle_cpu != -1)
1168		cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1169
1170	set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1171
1172	if (reader_tasks) {
1173		for (i = 0; i < nrealreaders; i++)
1174			if (reader_tasks[i])
1175				set_cpus_allowed_ptr(reader_tasks[i],
1176						     shuffle_tmp_mask);
1177	}
1178
1179	if (fakewriter_tasks) {
1180		for (i = 0; i < nfakewriters; i++)
1181			if (fakewriter_tasks[i])
1182				set_cpus_allowed_ptr(fakewriter_tasks[i],
1183						     shuffle_tmp_mask);
1184	}
1185
1186	if (writer_task)
1187		set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1188
1189	if (stats_task)
1190		set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1191
1192	if (rcu_idle_cpu == -1)
1193		rcu_idle_cpu = num_online_cpus() - 1;
1194	else
1195		rcu_idle_cpu--;
1196
1197	put_online_cpus();
1198}
1199
1200/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1201 * system to become idle at a time and cut off its timer ticks. This is meant
1202 * to test the support for such tickless idle CPU in RCU.
1203 */
1204static int
1205rcu_torture_shuffle(void *arg)
1206{
1207	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1208	do {
1209		schedule_timeout_interruptible(shuffle_interval * HZ);
1210		rcu_torture_shuffle_tasks();
1211		rcutorture_shutdown_absorb("rcu_torture_shuffle");
1212	} while (!kthread_should_stop());
1213	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1214	return 0;
1215}
1216
1217/* Cause the rcutorture test to "stutter", starting and stopping all
1218 * threads periodically.
1219 */
1220static int
1221rcu_torture_stutter(void *arg)
1222{
1223	VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1224	do {
1225		schedule_timeout_interruptible(stutter * HZ);
1226		stutter_pause_test = 1;
1227		if (!kthread_should_stop())
1228			schedule_timeout_interruptible(stutter * HZ);
1229		stutter_pause_test = 0;
1230		rcutorture_shutdown_absorb("rcu_torture_stutter");
1231	} while (!kthread_should_stop());
1232	VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1233	return 0;
1234}
1235
1236static inline void
1237rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
1238{
1239	printk(KERN_ALERT "%s" TORTURE_FLAG
1240		"--- %s: nreaders=%d nfakewriters=%d "
1241		"stat_interval=%d verbose=%d test_no_idle_hz=%d "
1242		"shuffle_interval=%d stutter=%d irqreader=%d "
1243		"fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1244		"test_boost=%d/%d test_boost_interval=%d "
1245		"test_boost_duration=%d\n",
 
1246		torture_type, tag, nrealreaders, nfakewriters,
1247		stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1248		stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1249		test_boost, cur_ops->can_boost,
1250		test_boost_interval, test_boost_duration);
 
1251}
1252
1253static struct notifier_block rcutorture_shutdown_nb = {
1254	.notifier_call = rcutorture_shutdown_notify,
1255};
1256
1257static void rcutorture_booster_cleanup(int cpu)
1258{
1259	struct task_struct *t;
1260
1261	if (boost_tasks[cpu] == NULL)
1262		return;
1263	mutex_lock(&boost_mutex);
1264	VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1265	t = boost_tasks[cpu];
1266	boost_tasks[cpu] = NULL;
1267	mutex_unlock(&boost_mutex);
1268
1269	/* This must be outside of the mutex, otherwise deadlock! */
1270	kthread_stop(t);
 
1271}
1272
1273static int rcutorture_booster_init(int cpu)
1274{
1275	int retval;
1276
1277	if (boost_tasks[cpu] != NULL)
1278		return 0;  /* Already created, nothing more to do. */
1279
1280	/* Don't allow time recalculation while creating a new task. */
1281	mutex_lock(&boost_mutex);
1282	VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1283	boost_tasks[cpu] = kthread_create(rcu_torture_boost, NULL,
1284					  "rcu_torture_boost");
 
1285	if (IS_ERR(boost_tasks[cpu])) {
1286		retval = PTR_ERR(boost_tasks[cpu]);
1287		VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1288		n_rcu_torture_boost_ktrerror++;
1289		boost_tasks[cpu] = NULL;
1290		mutex_unlock(&boost_mutex);
1291		return retval;
1292	}
1293	kthread_bind(boost_tasks[cpu], cpu);
1294	wake_up_process(boost_tasks[cpu]);
1295	mutex_unlock(&boost_mutex);
1296	return 0;
1297}
1298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1299static int rcutorture_cpu_notify(struct notifier_block *self,
1300				 unsigned long action, void *hcpu)
1301{
1302	long cpu = (long)hcpu;
1303
1304	switch (action) {
1305	case CPU_ONLINE:
1306	case CPU_DOWN_FAILED:
1307		(void)rcutorture_booster_init(cpu);
1308		break;
1309	case CPU_DOWN_PREPARE:
1310		rcutorture_booster_cleanup(cpu);
1311		break;
1312	default:
1313		break;
1314	}
1315	return NOTIFY_OK;
1316}
1317
1318static struct notifier_block rcutorture_cpu_nb = {
1319	.notifier_call = rcutorture_cpu_notify,
1320};
1321
1322static void
1323rcu_torture_cleanup(void)
1324{
1325	int i;
1326
1327	mutex_lock(&fullstop_mutex);
1328	rcutorture_record_test_transition();
1329	if (fullstop == FULLSTOP_SHUTDOWN) {
1330		printk(KERN_WARNING /* but going down anyway, so... */
1331		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1332		mutex_unlock(&fullstop_mutex);
1333		schedule_timeout_uninterruptible(10);
1334		if (cur_ops->cb_barrier != NULL)
1335			cur_ops->cb_barrier();
1336		return;
1337	}
1338	fullstop = FULLSTOP_RMMOD;
1339	mutex_unlock(&fullstop_mutex);
1340	unregister_reboot_notifier(&rcutorture_shutdown_nb);
 
 
1341	if (stutter_task) {
1342		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1343		kthread_stop(stutter_task);
1344	}
1345	stutter_task = NULL;
1346	if (shuffler_task) {
1347		VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1348		kthread_stop(shuffler_task);
1349		free_cpumask_var(shuffle_tmp_mask);
1350	}
1351	shuffler_task = NULL;
1352
1353	if (writer_task) {
1354		VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1355		kthread_stop(writer_task);
1356	}
1357	writer_task = NULL;
1358
1359	if (reader_tasks) {
1360		for (i = 0; i < nrealreaders; i++) {
1361			if (reader_tasks[i]) {
1362				VERBOSE_PRINTK_STRING(
1363					"Stopping rcu_torture_reader task");
1364				kthread_stop(reader_tasks[i]);
1365			}
1366			reader_tasks[i] = NULL;
1367		}
1368		kfree(reader_tasks);
1369		reader_tasks = NULL;
1370	}
1371	rcu_torture_current = NULL;
1372
1373	if (fakewriter_tasks) {
1374		for (i = 0; i < nfakewriters; i++) {
1375			if (fakewriter_tasks[i]) {
1376				VERBOSE_PRINTK_STRING(
1377					"Stopping rcu_torture_fakewriter task");
1378				kthread_stop(fakewriter_tasks[i]);
1379			}
1380			fakewriter_tasks[i] = NULL;
1381		}
1382		kfree(fakewriter_tasks);
1383		fakewriter_tasks = NULL;
1384	}
1385
1386	if (stats_task) {
1387		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1388		kthread_stop(stats_task);
1389	}
1390	stats_task = NULL;
1391
1392	if (fqs_task) {
1393		VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1394		kthread_stop(fqs_task);
1395	}
1396	fqs_task = NULL;
1397	if ((test_boost == 1 && cur_ops->can_boost) ||
1398	    test_boost == 2) {
1399		unregister_cpu_notifier(&rcutorture_cpu_nb);
1400		for_each_possible_cpu(i)
1401			rcutorture_booster_cleanup(i);
1402	}
 
 
 
 
 
 
1403
1404	/* Wait for all RCU callbacks to fire.  */
1405
1406	if (cur_ops->cb_barrier != NULL)
1407		cur_ops->cb_barrier();
1408
1409	rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1410
1411	if (cur_ops->cleanup)
1412		cur_ops->cleanup();
1413	if (atomic_read(&n_rcu_torture_error))
1414		rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
 
 
 
 
1415	else
1416		rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1417}
1418
1419static int __init
1420rcu_torture_init(void)
1421{
1422	int i;
1423	int cpu;
1424	int firsterr = 0;
 
1425	static struct rcu_torture_ops *torture_ops[] =
1426		{ &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1427		  &rcu_bh_ops, &rcu_bh_sync_ops,
1428		  &srcu_ops, &srcu_expedited_ops,
 
1429		  &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1430
1431	mutex_lock(&fullstop_mutex);
1432
1433	/* Process args and tell the world that the torturer is on the job. */
1434	for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1435		cur_ops = torture_ops[i];
1436		if (strcmp(torture_type, cur_ops->name) == 0)
1437			break;
1438	}
1439	if (i == ARRAY_SIZE(torture_ops)) {
1440		printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1441		       torture_type);
1442		printk(KERN_ALERT "rcu-torture types:");
1443		for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1444			printk(KERN_ALERT " %s", torture_ops[i]->name);
1445		printk(KERN_ALERT "\n");
1446		mutex_unlock(&fullstop_mutex);
1447		return -EINVAL;
1448	}
1449	if (cur_ops->fqs == NULL && fqs_duration != 0) {
1450		printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1451				  "fqs_duration, fqs disabled.\n");
1452		fqs_duration = 0;
1453	}
1454	if (cur_ops->init)
1455		cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1456
1457	if (nreaders >= 0)
1458		nrealreaders = nreaders;
1459	else
1460		nrealreaders = 2 * num_online_cpus();
1461	rcu_torture_print_module_parms(cur_ops, "Start of test");
1462	fullstop = FULLSTOP_DONTSTOP;
1463
1464	/* Set up the freelist. */
1465
1466	INIT_LIST_HEAD(&rcu_torture_freelist);
1467	for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1468		rcu_tortures[i].rtort_mbtest = 0;
1469		list_add_tail(&rcu_tortures[i].rtort_free,
1470			      &rcu_torture_freelist);
1471	}
1472
1473	/* Initialize the statistics so that each run gets its own numbers. */
1474
1475	rcu_torture_current = NULL;
1476	rcu_torture_current_version = 0;
1477	atomic_set(&n_rcu_torture_alloc, 0);
1478	atomic_set(&n_rcu_torture_alloc_fail, 0);
1479	atomic_set(&n_rcu_torture_free, 0);
1480	atomic_set(&n_rcu_torture_mberror, 0);
1481	atomic_set(&n_rcu_torture_error, 0);
 
1482	n_rcu_torture_boost_ktrerror = 0;
1483	n_rcu_torture_boost_rterror = 0;
1484	n_rcu_torture_boost_failure = 0;
1485	n_rcu_torture_boosts = 0;
1486	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1487		atomic_set(&rcu_torture_wcount[i], 0);
1488	for_each_possible_cpu(cpu) {
1489		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1490			per_cpu(rcu_torture_count, cpu)[i] = 0;
1491			per_cpu(rcu_torture_batch, cpu)[i] = 0;
1492		}
1493	}
1494
1495	/* Start up the kthreads. */
1496
1497	VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1498	writer_task = kthread_run(rcu_torture_writer, NULL,
1499				  "rcu_torture_writer");
1500	if (IS_ERR(writer_task)) {
1501		firsterr = PTR_ERR(writer_task);
1502		VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1503		writer_task = NULL;
1504		goto unwind;
1505	}
1506	fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1507				   GFP_KERNEL);
1508	if (fakewriter_tasks == NULL) {
1509		VERBOSE_PRINTK_ERRSTRING("out of memory");
1510		firsterr = -ENOMEM;
1511		goto unwind;
1512	}
1513	for (i = 0; i < nfakewriters; i++) {
1514		VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1515		fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1516						  "rcu_torture_fakewriter");
1517		if (IS_ERR(fakewriter_tasks[i])) {
1518			firsterr = PTR_ERR(fakewriter_tasks[i]);
1519			VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1520			fakewriter_tasks[i] = NULL;
1521			goto unwind;
1522		}
1523	}
1524	reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1525			       GFP_KERNEL);
1526	if (reader_tasks == NULL) {
1527		VERBOSE_PRINTK_ERRSTRING("out of memory");
1528		firsterr = -ENOMEM;
1529		goto unwind;
1530	}
1531	for (i = 0; i < nrealreaders; i++) {
1532		VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1533		reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
1534					      "rcu_torture_reader");
1535		if (IS_ERR(reader_tasks[i])) {
1536			firsterr = PTR_ERR(reader_tasks[i]);
1537			VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
1538			reader_tasks[i] = NULL;
1539			goto unwind;
1540		}
1541	}
1542	if (stat_interval > 0) {
1543		VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
1544		stats_task = kthread_run(rcu_torture_stats, NULL,
1545					"rcu_torture_stats");
1546		if (IS_ERR(stats_task)) {
1547			firsterr = PTR_ERR(stats_task);
1548			VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
1549			stats_task = NULL;
1550			goto unwind;
1551		}
1552	}
1553	if (test_no_idle_hz) {
1554		rcu_idle_cpu = num_online_cpus() - 1;
1555
1556		if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
1557			firsterr = -ENOMEM;
1558			VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
1559			goto unwind;
1560		}
1561
1562		/* Create the shuffler thread */
1563		shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
1564					  "rcu_torture_shuffle");
1565		if (IS_ERR(shuffler_task)) {
1566			free_cpumask_var(shuffle_tmp_mask);
1567			firsterr = PTR_ERR(shuffler_task);
1568			VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
1569			shuffler_task = NULL;
1570			goto unwind;
1571		}
1572	}
1573	if (stutter < 0)
1574		stutter = 0;
1575	if (stutter) {
1576		/* Create the stutter thread */
1577		stutter_task = kthread_run(rcu_torture_stutter, NULL,
1578					  "rcu_torture_stutter");
1579		if (IS_ERR(stutter_task)) {
1580			firsterr = PTR_ERR(stutter_task);
1581			VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
1582			stutter_task = NULL;
1583			goto unwind;
1584		}
1585	}
1586	if (fqs_duration < 0)
1587		fqs_duration = 0;
1588	if (fqs_duration) {
1589		/* Create the stutter thread */
1590		fqs_task = kthread_run(rcu_torture_fqs, NULL,
1591				       "rcu_torture_fqs");
1592		if (IS_ERR(fqs_task)) {
1593			firsterr = PTR_ERR(fqs_task);
1594			VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
1595			fqs_task = NULL;
1596			goto unwind;
1597		}
1598	}
1599	if (test_boost_interval < 1)
1600		test_boost_interval = 1;
1601	if (test_boost_duration < 2)
1602		test_boost_duration = 2;
1603	if ((test_boost == 1 && cur_ops->can_boost) ||
1604	    test_boost == 2) {
1605		int retval;
1606
1607		boost_starttime = jiffies + test_boost_interval * HZ;
1608		register_cpu_notifier(&rcutorture_cpu_nb);
1609		for_each_possible_cpu(i) {
1610			if (cpu_is_offline(i))
1611				continue;  /* Heuristic: CPU can go offline. */
1612			retval = rcutorture_booster_init(i);
1613			if (retval < 0) {
1614				firsterr = retval;
1615				goto unwind;
1616			}
1617		}
1618	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1619	register_reboot_notifier(&rcutorture_shutdown_nb);
 
 
 
 
 
 
 
 
 
 
1620	rcutorture_record_test_transition();
1621	mutex_unlock(&fullstop_mutex);
1622	return 0;
1623
1624unwind:
1625	mutex_unlock(&fullstop_mutex);
1626	rcu_torture_cleanup();
1627	return firsterr;
1628}
1629
1630module_init(rcu_torture_init);
1631module_exit(rcu_torture_cleanup);
v3.5.6
   1/*
   2 * Read-Copy Update module-based torture test facility
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software
  16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17 *
  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 * See also:  Documentation/RCU/torture.txt
  24 */
  25#include <linux/types.h>
  26#include <linux/kernel.h>
  27#include <linux/init.h>
  28#include <linux/module.h>
  29#include <linux/kthread.h>
  30#include <linux/err.h>
  31#include <linux/spinlock.h>
  32#include <linux/smp.h>
  33#include <linux/rcupdate.h>
  34#include <linux/interrupt.h>
  35#include <linux/sched.h>
  36#include <linux/atomic.h>
  37#include <linux/bitops.h>
  38#include <linux/completion.h>
  39#include <linux/moduleparam.h>
  40#include <linux/percpu.h>
  41#include <linux/notifier.h>
  42#include <linux/reboot.h>
  43#include <linux/freezer.h>
  44#include <linux/cpu.h>
  45#include <linux/delay.h>
  46#include <linux/stat.h>
  47#include <linux/srcu.h>
  48#include <linux/slab.h>
  49#include <asm/byteorder.h>
  50
  51MODULE_LICENSE("GPL");
  52MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  53	      "Josh Triplett <josh@freedesktop.org>");
  54
  55static int nreaders = -1;	/* # reader threads, defaults to 2*ncpus */
  56static int nfakewriters = 4;	/* # fake writer threads */
  57static int stat_interval;	/* Interval between stats, in seconds. */
  58				/*  Defaults to "only at end of test". */
  59static bool verbose;		/* Print more debug info. */
  60static bool test_no_idle_hz;	/* Test RCU's support for tickless idle CPUs. */
  61static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  62static int stutter = 5;		/* Start/stop testing interval (in sec) */
  63static int irqreader = 1;	/* RCU readers from irq (timers). */
  64static int fqs_duration;	/* Duration of bursts (us), 0 to disable. */
  65static int fqs_holdoff;		/* Hold time within burst (us). */
  66static int fqs_stutter = 3;	/* Wait time between bursts (s). */
  67static int n_barrier_cbs;	/* Number of callbacks to test RCU barriers. */
  68static int onoff_interval;	/* Wait time between CPU hotplugs, 0=disable. */
  69static int onoff_holdoff;	/* Seconds after boot before CPU hotplugs. */
  70static int shutdown_secs;	/* Shutdown time (s).  <=0 for no shutdown. */
  71static int stall_cpu;		/* CPU-stall duration (s).  0 for no stall. */
  72static int stall_cpu_holdoff = 10; /* Time to wait until stall (s).  */
  73static int test_boost = 1;	/* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
  74static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
  75static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
  76static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  77
  78module_param(nreaders, int, 0444);
  79MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  80module_param(nfakewriters, int, 0444);
  81MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  82module_param(stat_interval, int, 0644);
  83MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  84module_param(verbose, bool, 0444);
  85MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  86module_param(test_no_idle_hz, bool, 0444);
  87MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  88module_param(shuffle_interval, int, 0444);
  89MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  90module_param(stutter, int, 0444);
  91MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  92module_param(irqreader, int, 0444);
  93MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  94module_param(fqs_duration, int, 0444);
  95MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  96module_param(fqs_holdoff, int, 0444);
  97MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  98module_param(fqs_stutter, int, 0444);
  99MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
 100module_param(n_barrier_cbs, int, 0444);
 101MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
 102module_param(onoff_interval, int, 0444);
 103MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
 104module_param(onoff_holdoff, int, 0444);
 105MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
 106module_param(shutdown_secs, int, 0444);
 107MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
 108module_param(stall_cpu, int, 0444);
 109MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
 110module_param(stall_cpu_holdoff, int, 0444);
 111MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
 112module_param(test_boost, int, 0444);
 113MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
 114module_param(test_boost_interval, int, 0444);
 115MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
 116module_param(test_boost_duration, int, 0444);
 117MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
 118module_param(torture_type, charp, 0444);
 119MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
 120
 121#define TORTURE_FLAG "-torture:"
 122#define PRINTK_STRING(s) \
 123	do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
 124#define VERBOSE_PRINTK_STRING(s) \
 125	do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
 126#define VERBOSE_PRINTK_ERRSTRING(s) \
 127	do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
 128
 129static char printk_buf[4096];
 130
 131static int nrealreaders;
 132static struct task_struct *writer_task;
 133static struct task_struct **fakewriter_tasks;
 134static struct task_struct **reader_tasks;
 135static struct task_struct *stats_task;
 136static struct task_struct *shuffler_task;
 137static struct task_struct *stutter_task;
 138static struct task_struct *fqs_task;
 139static struct task_struct *boost_tasks[NR_CPUS];
 140static struct task_struct *shutdown_task;
 141#ifdef CONFIG_HOTPLUG_CPU
 142static struct task_struct *onoff_task;
 143#endif /* #ifdef CONFIG_HOTPLUG_CPU */
 144static struct task_struct *stall_task;
 145static struct task_struct **barrier_cbs_tasks;
 146static struct task_struct *barrier_task;
 147
 148#define RCU_TORTURE_PIPE_LEN 10
 149
 150struct rcu_torture {
 151	struct rcu_head rtort_rcu;
 152	int rtort_pipe_count;
 153	struct list_head rtort_free;
 154	int rtort_mbtest;
 155};
 156
 157static LIST_HEAD(rcu_torture_freelist);
 158static struct rcu_torture __rcu *rcu_torture_current;
 159static unsigned long rcu_torture_current_version;
 160static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
 161static DEFINE_SPINLOCK(rcu_torture_lock);
 162static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
 163	{ 0 };
 164static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
 165	{ 0 };
 166static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
 167static atomic_t n_rcu_torture_alloc;
 168static atomic_t n_rcu_torture_alloc_fail;
 169static atomic_t n_rcu_torture_free;
 170static atomic_t n_rcu_torture_mberror;
 171static atomic_t n_rcu_torture_error;
 172static long n_rcu_torture_barrier_error;
 173static long n_rcu_torture_boost_ktrerror;
 174static long n_rcu_torture_boost_rterror;
 175static long n_rcu_torture_boost_failure;
 176static long n_rcu_torture_boosts;
 177static long n_rcu_torture_timers;
 178static long n_offline_attempts;
 179static long n_offline_successes;
 180static long n_online_attempts;
 181static long n_online_successes;
 182static long n_barrier_attempts;
 183static long n_barrier_successes;
 184static struct list_head rcu_torture_removed;
 185static cpumask_var_t shuffle_tmp_mask;
 186
 187static int stutter_pause_test;
 188
 189#if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
 190#define RCUTORTURE_RUNNABLE_INIT 1
 191#else
 192#define RCUTORTURE_RUNNABLE_INIT 0
 193#endif
 194int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
 195module_param(rcutorture_runnable, int, 0444);
 196MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
 197
 198#if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
 199#define rcu_can_boost() 1
 200#else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
 201#define rcu_can_boost() 0
 202#endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
 203
 204static unsigned long shutdown_time;	/* jiffies to system shutdown. */
 205static unsigned long boost_starttime;	/* jiffies of next boost test start. */
 206DEFINE_MUTEX(boost_mutex);		/* protect setting boost_starttime */
 207					/*  and boost task create/destroy. */
 208static atomic_t barrier_cbs_count;	/* Barrier callbacks registered. */
 209static atomic_t barrier_cbs_invoked;	/* Barrier callbacks invoked. */
 210static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
 211static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
 212
 213/* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
 214
 215#define FULLSTOP_DONTSTOP 0	/* Normal operation. */
 216#define FULLSTOP_SHUTDOWN 1	/* System shutdown with rcutorture running. */
 217#define FULLSTOP_RMMOD    2	/* Normal rmmod of rcutorture. */
 218static int fullstop = FULLSTOP_RMMOD;
 219/*
 220 * Protect fullstop transitions and spawning of kthreads.
 221 */
 222static DEFINE_MUTEX(fullstop_mutex);
 223
 224/* Forward reference. */
 225static void rcu_torture_cleanup(void);
 226
 227/*
 228 * Detect and respond to a system shutdown.
 229 */
 230static int
 231rcutorture_shutdown_notify(struct notifier_block *unused1,
 232			   unsigned long unused2, void *unused3)
 233{
 234	mutex_lock(&fullstop_mutex);
 235	if (fullstop == FULLSTOP_DONTSTOP)
 236		fullstop = FULLSTOP_SHUTDOWN;
 237	else
 238		printk(KERN_WARNING /* but going down anyway, so... */
 239		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
 240	mutex_unlock(&fullstop_mutex);
 241	return NOTIFY_DONE;
 242}
 243
 244/*
 245 * Absorb kthreads into a kernel function that won't return, so that
 246 * they won't ever access module text or data again.
 247 */
 248static void rcutorture_shutdown_absorb(char *title)
 249{
 250	if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
 251		printk(KERN_NOTICE
 252		       "rcutorture thread %s parking due to system shutdown\n",
 253		       title);
 254		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
 255	}
 256}
 257
 258/*
 259 * Allocate an element from the rcu_tortures pool.
 260 */
 261static struct rcu_torture *
 262rcu_torture_alloc(void)
 263{
 264	struct list_head *p;
 265
 266	spin_lock_bh(&rcu_torture_lock);
 267	if (list_empty(&rcu_torture_freelist)) {
 268		atomic_inc(&n_rcu_torture_alloc_fail);
 269		spin_unlock_bh(&rcu_torture_lock);
 270		return NULL;
 271	}
 272	atomic_inc(&n_rcu_torture_alloc);
 273	p = rcu_torture_freelist.next;
 274	list_del_init(p);
 275	spin_unlock_bh(&rcu_torture_lock);
 276	return container_of(p, struct rcu_torture, rtort_free);
 277}
 278
 279/*
 280 * Free an element to the rcu_tortures pool.
 281 */
 282static void
 283rcu_torture_free(struct rcu_torture *p)
 284{
 285	atomic_inc(&n_rcu_torture_free);
 286	spin_lock_bh(&rcu_torture_lock);
 287	list_add_tail(&p->rtort_free, &rcu_torture_freelist);
 288	spin_unlock_bh(&rcu_torture_lock);
 289}
 290
 291struct rcu_random_state {
 292	unsigned long rrs_state;
 293	long rrs_count;
 294};
 295
 296#define RCU_RANDOM_MULT 39916801  /* prime */
 297#define RCU_RANDOM_ADD	479001701 /* prime */
 298#define RCU_RANDOM_REFRESH 10000
 299
 300#define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
 301
 302/*
 303 * Crude but fast random-number generator.  Uses a linear congruential
 304 * generator, with occasional help from cpu_clock().
 305 */
 306static unsigned long
 307rcu_random(struct rcu_random_state *rrsp)
 308{
 309	if (--rrsp->rrs_count < 0) {
 310		rrsp->rrs_state += (unsigned long)local_clock();
 311		rrsp->rrs_count = RCU_RANDOM_REFRESH;
 312	}
 313	rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
 314	return swahw32(rrsp->rrs_state);
 315}
 316
 317static void
 318rcu_stutter_wait(char *title)
 319{
 320	while (stutter_pause_test || !rcutorture_runnable) {
 321		if (rcutorture_runnable)
 322			schedule_timeout_interruptible(1);
 323		else
 324			schedule_timeout_interruptible(round_jiffies_relative(HZ));
 325		rcutorture_shutdown_absorb(title);
 326	}
 327}
 328
 329/*
 330 * Operations vector for selecting different types of tests.
 331 */
 332
 333struct rcu_torture_ops {
 334	void (*init)(void);
 335	void (*cleanup)(void);
 336	int (*readlock)(void);
 337	void (*read_delay)(struct rcu_random_state *rrsp);
 338	void (*readunlock)(int idx);
 339	int (*completed)(void);
 340	void (*deferred_free)(struct rcu_torture *p);
 341	void (*sync)(void);
 342	void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
 343	void (*cb_barrier)(void);
 344	void (*fqs)(void);
 345	int (*stats)(char *page);
 346	int irq_capable;
 347	int can_boost;
 348	char *name;
 349};
 350
 351static struct rcu_torture_ops *cur_ops;
 352
 353/*
 354 * Definitions for rcu torture testing.
 355 */
 356
 357static int rcu_torture_read_lock(void) __acquires(RCU)
 358{
 359	rcu_read_lock();
 360	return 0;
 361}
 362
 363static void rcu_read_delay(struct rcu_random_state *rrsp)
 364{
 365	const unsigned long shortdelay_us = 200;
 366	const unsigned long longdelay_ms = 50;
 367
 368	/* We want a short delay sometimes to make a reader delay the grace
 369	 * period, and we want a long delay occasionally to trigger
 370	 * force_quiescent_state. */
 371
 372	if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
 373		mdelay(longdelay_ms);
 374	if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
 375		udelay(shortdelay_us);
 376#ifdef CONFIG_PREEMPT
 377	if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
 378		preempt_schedule();  /* No QS if preempt_disable() in effect */
 379#endif
 380}
 381
 382static void rcu_torture_read_unlock(int idx) __releases(RCU)
 383{
 384	rcu_read_unlock();
 385}
 386
 387static int rcu_torture_completed(void)
 388{
 389	return rcu_batches_completed();
 390}
 391
 392static void
 393rcu_torture_cb(struct rcu_head *p)
 394{
 395	int i;
 396	struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
 397
 398	if (fullstop != FULLSTOP_DONTSTOP) {
 399		/* Test is ending, just drop callbacks on the floor. */
 400		/* The next initialization will pick up the pieces. */
 401		return;
 402	}
 403	i = rp->rtort_pipe_count;
 404	if (i > RCU_TORTURE_PIPE_LEN)
 405		i = RCU_TORTURE_PIPE_LEN;
 406	atomic_inc(&rcu_torture_wcount[i]);
 407	if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
 408		rp->rtort_mbtest = 0;
 409		rcu_torture_free(rp);
 410	} else
 411		cur_ops->deferred_free(rp);
 412}
 413
 414static int rcu_no_completed(void)
 415{
 416	return 0;
 417}
 418
 419static void rcu_torture_deferred_free(struct rcu_torture *p)
 420{
 421	call_rcu(&p->rtort_rcu, rcu_torture_cb);
 422}
 423
 424static struct rcu_torture_ops rcu_ops = {
 425	.init		= NULL,
 426	.cleanup	= NULL,
 427	.readlock	= rcu_torture_read_lock,
 428	.read_delay	= rcu_read_delay,
 429	.readunlock	= rcu_torture_read_unlock,
 430	.completed	= rcu_torture_completed,
 431	.deferred_free	= rcu_torture_deferred_free,
 432	.sync		= synchronize_rcu,
 433	.call		= call_rcu,
 434	.cb_barrier	= rcu_barrier,
 435	.fqs		= rcu_force_quiescent_state,
 436	.stats		= NULL,
 437	.irq_capable	= 1,
 438	.can_boost	= rcu_can_boost(),
 439	.name		= "rcu"
 440};
 441
 442static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
 443{
 444	int i;
 445	struct rcu_torture *rp;
 446	struct rcu_torture *rp1;
 447
 448	cur_ops->sync();
 449	list_add(&p->rtort_free, &rcu_torture_removed);
 450	list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
 451		i = rp->rtort_pipe_count;
 452		if (i > RCU_TORTURE_PIPE_LEN)
 453			i = RCU_TORTURE_PIPE_LEN;
 454		atomic_inc(&rcu_torture_wcount[i]);
 455		if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
 456			rp->rtort_mbtest = 0;
 457			list_del(&rp->rtort_free);
 458			rcu_torture_free(rp);
 459		}
 460	}
 461}
 462
 463static void rcu_sync_torture_init(void)
 464{
 465	INIT_LIST_HEAD(&rcu_torture_removed);
 466}
 467
 468static struct rcu_torture_ops rcu_sync_ops = {
 469	.init		= rcu_sync_torture_init,
 470	.cleanup	= NULL,
 471	.readlock	= rcu_torture_read_lock,
 472	.read_delay	= rcu_read_delay,
 473	.readunlock	= rcu_torture_read_unlock,
 474	.completed	= rcu_torture_completed,
 475	.deferred_free	= rcu_sync_torture_deferred_free,
 476	.sync		= synchronize_rcu,
 477	.call		= NULL,
 478	.cb_barrier	= NULL,
 479	.fqs		= rcu_force_quiescent_state,
 480	.stats		= NULL,
 481	.irq_capable	= 1,
 482	.can_boost	= rcu_can_boost(),
 483	.name		= "rcu_sync"
 484};
 485
 486static struct rcu_torture_ops rcu_expedited_ops = {
 487	.init		= rcu_sync_torture_init,
 488	.cleanup	= NULL,
 489	.readlock	= rcu_torture_read_lock,
 490	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 491	.readunlock	= rcu_torture_read_unlock,
 492	.completed	= rcu_no_completed,
 493	.deferred_free	= rcu_sync_torture_deferred_free,
 494	.sync		= synchronize_rcu_expedited,
 495	.call		= NULL,
 496	.cb_barrier	= NULL,
 497	.fqs		= rcu_force_quiescent_state,
 498	.stats		= NULL,
 499	.irq_capable	= 1,
 500	.can_boost	= rcu_can_boost(),
 501	.name		= "rcu_expedited"
 502};
 503
 504/*
 505 * Definitions for rcu_bh torture testing.
 506 */
 507
 508static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
 509{
 510	rcu_read_lock_bh();
 511	return 0;
 512}
 513
 514static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
 515{
 516	rcu_read_unlock_bh();
 517}
 518
 519static int rcu_bh_torture_completed(void)
 520{
 521	return rcu_batches_completed_bh();
 522}
 523
 524static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
 525{
 526	call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
 527}
 528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 529static struct rcu_torture_ops rcu_bh_ops = {
 530	.init		= NULL,
 531	.cleanup	= NULL,
 532	.readlock	= rcu_bh_torture_read_lock,
 533	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 534	.readunlock	= rcu_bh_torture_read_unlock,
 535	.completed	= rcu_bh_torture_completed,
 536	.deferred_free	= rcu_bh_torture_deferred_free,
 537	.sync		= synchronize_rcu_bh,
 538	.call		= call_rcu_bh,
 539	.cb_barrier	= rcu_barrier_bh,
 540	.fqs		= rcu_bh_force_quiescent_state,
 541	.stats		= NULL,
 542	.irq_capable	= 1,
 543	.name		= "rcu_bh"
 544};
 545
 546static struct rcu_torture_ops rcu_bh_sync_ops = {
 547	.init		= rcu_sync_torture_init,
 548	.cleanup	= NULL,
 549	.readlock	= rcu_bh_torture_read_lock,
 550	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 551	.readunlock	= rcu_bh_torture_read_unlock,
 552	.completed	= rcu_bh_torture_completed,
 553	.deferred_free	= rcu_sync_torture_deferred_free,
 554	.sync		= synchronize_rcu_bh,
 555	.call		= NULL,
 556	.cb_barrier	= NULL,
 557	.fqs		= rcu_bh_force_quiescent_state,
 558	.stats		= NULL,
 559	.irq_capable	= 1,
 560	.name		= "rcu_bh_sync"
 561};
 562
 563static struct rcu_torture_ops rcu_bh_expedited_ops = {
 564	.init		= rcu_sync_torture_init,
 565	.cleanup	= NULL,
 566	.readlock	= rcu_bh_torture_read_lock,
 567	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 568	.readunlock	= rcu_bh_torture_read_unlock,
 569	.completed	= rcu_bh_torture_completed,
 570	.deferred_free	= rcu_sync_torture_deferred_free,
 571	.sync		= synchronize_rcu_bh_expedited,
 572	.call		= NULL,
 573	.cb_barrier	= NULL,
 574	.fqs		= rcu_bh_force_quiescent_state,
 575	.stats		= NULL,
 576	.irq_capable	= 1,
 577	.name		= "rcu_bh_expedited"
 578};
 579
 580/*
 581 * Definitions for srcu torture testing.
 582 */
 583
 584static struct srcu_struct srcu_ctl;
 585
 586static void srcu_torture_init(void)
 587{
 588	init_srcu_struct(&srcu_ctl);
 589	rcu_sync_torture_init();
 590}
 591
 592static void srcu_torture_cleanup(void)
 593{
 594	synchronize_srcu(&srcu_ctl);
 595	cleanup_srcu_struct(&srcu_ctl);
 596}
 597
 598static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
 599{
 600	return srcu_read_lock(&srcu_ctl);
 601}
 602
 603static void srcu_read_delay(struct rcu_random_state *rrsp)
 604{
 605	long delay;
 606	const long uspertick = 1000000 / HZ;
 607	const long longdelay = 10;
 608
 609	/* We want there to be long-running readers, but not all the time. */
 610
 611	delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
 612	if (!delay)
 613		schedule_timeout_interruptible(longdelay);
 614	else
 615		rcu_read_delay(rrsp);
 616}
 617
 618static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
 619{
 620	srcu_read_unlock(&srcu_ctl, idx);
 621}
 622
 623static int srcu_torture_completed(void)
 624{
 625	return srcu_batches_completed(&srcu_ctl);
 626}
 627
 628static void srcu_torture_deferred_free(struct rcu_torture *rp)
 629{
 630	call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
 631}
 632
 633static void srcu_torture_synchronize(void)
 634{
 635	synchronize_srcu(&srcu_ctl);
 636}
 637
 638static int srcu_torture_stats(char *page)
 639{
 640	int cnt = 0;
 641	int cpu;
 642	int idx = srcu_ctl.completed & 0x1;
 643
 644	cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
 645		       torture_type, TORTURE_FLAG, idx);
 646	for_each_possible_cpu(cpu) {
 647		cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
 648			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
 649			       per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
 650	}
 651	cnt += sprintf(&page[cnt], "\n");
 652	return cnt;
 653}
 654
 655static struct rcu_torture_ops srcu_ops = {
 656	.init		= srcu_torture_init,
 657	.cleanup	= srcu_torture_cleanup,
 658	.readlock	= srcu_torture_read_lock,
 659	.read_delay	= srcu_read_delay,
 660	.readunlock	= srcu_torture_read_unlock,
 661	.completed	= srcu_torture_completed,
 662	.deferred_free	= srcu_torture_deferred_free,
 663	.sync		= srcu_torture_synchronize,
 664	.call		= NULL,
 665	.cb_barrier	= NULL,
 666	.stats		= srcu_torture_stats,
 667	.name		= "srcu"
 668};
 669
 670static struct rcu_torture_ops srcu_sync_ops = {
 671	.init		= srcu_torture_init,
 672	.cleanup	= srcu_torture_cleanup,
 673	.readlock	= srcu_torture_read_lock,
 674	.read_delay	= srcu_read_delay,
 675	.readunlock	= srcu_torture_read_unlock,
 676	.completed	= srcu_torture_completed,
 677	.deferred_free	= rcu_sync_torture_deferred_free,
 678	.sync		= srcu_torture_synchronize,
 679	.call		= NULL,
 680	.cb_barrier	= NULL,
 681	.stats		= srcu_torture_stats,
 682	.name		= "srcu_sync"
 683};
 684
 685static int srcu_torture_read_lock_raw(void) __acquires(&srcu_ctl)
 686{
 687	return srcu_read_lock_raw(&srcu_ctl);
 688}
 689
 690static void srcu_torture_read_unlock_raw(int idx) __releases(&srcu_ctl)
 691{
 692	srcu_read_unlock_raw(&srcu_ctl, idx);
 693}
 694
 695static struct rcu_torture_ops srcu_raw_ops = {
 696	.init		= srcu_torture_init,
 697	.cleanup	= srcu_torture_cleanup,
 698	.readlock	= srcu_torture_read_lock_raw,
 699	.read_delay	= srcu_read_delay,
 700	.readunlock	= srcu_torture_read_unlock_raw,
 701	.completed	= srcu_torture_completed,
 702	.deferred_free	= srcu_torture_deferred_free,
 703	.sync		= srcu_torture_synchronize,
 704	.call		= NULL,
 705	.cb_barrier	= NULL,
 706	.stats		= srcu_torture_stats,
 707	.name		= "srcu_raw"
 708};
 709
 710static struct rcu_torture_ops srcu_raw_sync_ops = {
 711	.init		= srcu_torture_init,
 712	.cleanup	= srcu_torture_cleanup,
 713	.readlock	= srcu_torture_read_lock_raw,
 714	.read_delay	= srcu_read_delay,
 715	.readunlock	= srcu_torture_read_unlock_raw,
 716	.completed	= srcu_torture_completed,
 717	.deferred_free	= rcu_sync_torture_deferred_free,
 718	.sync		= srcu_torture_synchronize,
 719	.call		= NULL,
 720	.cb_barrier	= NULL,
 721	.stats		= srcu_torture_stats,
 722	.name		= "srcu_raw_sync"
 723};
 724
 725static void srcu_torture_synchronize_expedited(void)
 726{
 727	synchronize_srcu_expedited(&srcu_ctl);
 728}
 729
 730static struct rcu_torture_ops srcu_expedited_ops = {
 731	.init		= srcu_torture_init,
 732	.cleanup	= srcu_torture_cleanup,
 733	.readlock	= srcu_torture_read_lock,
 734	.read_delay	= srcu_read_delay,
 735	.readunlock	= srcu_torture_read_unlock,
 736	.completed	= srcu_torture_completed,
 737	.deferred_free	= rcu_sync_torture_deferred_free,
 738	.sync		= srcu_torture_synchronize_expedited,
 739	.call		= NULL,
 740	.cb_barrier	= NULL,
 741	.stats		= srcu_torture_stats,
 742	.name		= "srcu_expedited"
 743};
 744
 745/*
 746 * Definitions for sched torture testing.
 747 */
 748
 749static int sched_torture_read_lock(void)
 750{
 751	preempt_disable();
 752	return 0;
 753}
 754
 755static void sched_torture_read_unlock(int idx)
 756{
 757	preempt_enable();
 758}
 759
 760static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
 761{
 762	call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
 763}
 764
 
 
 
 
 
 765static struct rcu_torture_ops sched_ops = {
 766	.init		= rcu_sync_torture_init,
 767	.cleanup	= NULL,
 768	.readlock	= sched_torture_read_lock,
 769	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 770	.readunlock	= sched_torture_read_unlock,
 771	.completed	= rcu_no_completed,
 772	.deferred_free	= rcu_sched_torture_deferred_free,
 773	.sync		= synchronize_sched,
 774	.cb_barrier	= rcu_barrier_sched,
 775	.fqs		= rcu_sched_force_quiescent_state,
 776	.stats		= NULL,
 777	.irq_capable	= 1,
 778	.name		= "sched"
 779};
 780
 781static struct rcu_torture_ops sched_sync_ops = {
 782	.init		= rcu_sync_torture_init,
 783	.cleanup	= NULL,
 784	.readlock	= sched_torture_read_lock,
 785	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 786	.readunlock	= sched_torture_read_unlock,
 787	.completed	= rcu_no_completed,
 788	.deferred_free	= rcu_sync_torture_deferred_free,
 789	.sync		= synchronize_sched,
 790	.cb_barrier	= NULL,
 791	.fqs		= rcu_sched_force_quiescent_state,
 792	.stats		= NULL,
 793	.name		= "sched_sync"
 794};
 795
 796static struct rcu_torture_ops sched_expedited_ops = {
 797	.init		= rcu_sync_torture_init,
 798	.cleanup	= NULL,
 799	.readlock	= sched_torture_read_lock,
 800	.read_delay	= rcu_read_delay,  /* just reuse rcu's version. */
 801	.readunlock	= sched_torture_read_unlock,
 802	.completed	= rcu_no_completed,
 803	.deferred_free	= rcu_sync_torture_deferred_free,
 804	.sync		= synchronize_sched_expedited,
 805	.cb_barrier	= NULL,
 806	.fqs		= rcu_sched_force_quiescent_state,
 807	.stats		= NULL,
 808	.irq_capable	= 1,
 809	.name		= "sched_expedited"
 810};
 811
 812/*
 813 * RCU torture priority-boost testing.  Runs one real-time thread per
 814 * CPU for moderate bursts, repeatedly registering RCU callbacks and
 815 * spinning waiting for them to be invoked.  If a given callback takes
 816 * too long to be invoked, we assume that priority inversion has occurred.
 817 */
 818
 819struct rcu_boost_inflight {
 820	struct rcu_head rcu;
 821	int inflight;
 822};
 823
 824static void rcu_torture_boost_cb(struct rcu_head *head)
 825{
 826	struct rcu_boost_inflight *rbip =
 827		container_of(head, struct rcu_boost_inflight, rcu);
 828
 829	smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
 830	rbip->inflight = 0;
 831}
 832
 833static int rcu_torture_boost(void *arg)
 834{
 835	unsigned long call_rcu_time;
 836	unsigned long endtime;
 837	unsigned long oldstarttime;
 838	struct rcu_boost_inflight rbi = { .inflight = 0 };
 839	struct sched_param sp;
 840
 841	VERBOSE_PRINTK_STRING("rcu_torture_boost started");
 842
 843	/* Set real-time priority. */
 844	sp.sched_priority = 1;
 845	if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
 846		VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
 847		n_rcu_torture_boost_rterror++;
 848	}
 849
 850	init_rcu_head_on_stack(&rbi.rcu);
 851	/* Each pass through the following loop does one boost-test cycle. */
 852	do {
 853		/* Wait for the next test interval. */
 854		oldstarttime = boost_starttime;
 855		while (ULONG_CMP_LT(jiffies, oldstarttime)) {
 856			schedule_timeout_uninterruptible(1);
 857			rcu_stutter_wait("rcu_torture_boost");
 858			if (kthread_should_stop() ||
 859			    fullstop != FULLSTOP_DONTSTOP)
 860				goto checkwait;
 861		}
 862
 863		/* Do one boost-test interval. */
 864		endtime = oldstarttime + test_boost_duration * HZ;
 865		call_rcu_time = jiffies;
 866		while (ULONG_CMP_LT(jiffies, endtime)) {
 867			/* If we don't have a callback in flight, post one. */
 868			if (!rbi.inflight) {
 869				smp_mb(); /* RCU core before ->inflight = 1. */
 870				rbi.inflight = 1;
 871				call_rcu(&rbi.rcu, rcu_torture_boost_cb);
 872				if (jiffies - call_rcu_time >
 873					 test_boost_duration * HZ - HZ / 2) {
 874					VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
 875					n_rcu_torture_boost_failure++;
 876				}
 877				call_rcu_time = jiffies;
 878			}
 879			cond_resched();
 880			rcu_stutter_wait("rcu_torture_boost");
 881			if (kthread_should_stop() ||
 882			    fullstop != FULLSTOP_DONTSTOP)
 883				goto checkwait;
 884		}
 885
 886		/*
 887		 * Set the start time of the next test interval.
 888		 * Yes, this is vulnerable to long delays, but such
 889		 * delays simply cause a false negative for the next
 890		 * interval.  Besides, we are running at RT priority,
 891		 * so delays should be relatively rare.
 892		 */
 893		while (oldstarttime == boost_starttime &&
 894		       !kthread_should_stop()) {
 895			if (mutex_trylock(&boost_mutex)) {
 896				boost_starttime = jiffies +
 897						  test_boost_interval * HZ;
 898				n_rcu_torture_boosts++;
 899				mutex_unlock(&boost_mutex);
 900				break;
 901			}
 902			schedule_timeout_uninterruptible(1);
 903		}
 904
 905		/* Go do the stutter. */
 906checkwait:	rcu_stutter_wait("rcu_torture_boost");
 907	} while (!kthread_should_stop() && fullstop  == FULLSTOP_DONTSTOP);
 908
 909	/* Clean up and exit. */
 910	VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
 
 911	rcutorture_shutdown_absorb("rcu_torture_boost");
 912	while (!kthread_should_stop() || rbi.inflight)
 913		schedule_timeout_uninterruptible(1);
 914	smp_mb(); /* order accesses to ->inflight before stack-frame death. */
 915	destroy_rcu_head_on_stack(&rbi.rcu);
 916	return 0;
 917}
 918
 919/*
 920 * RCU torture force-quiescent-state kthread.  Repeatedly induces
 921 * bursts of calls to force_quiescent_state(), increasing the probability
 922 * of occurrence of some important types of race conditions.
 923 */
 924static int
 925rcu_torture_fqs(void *arg)
 926{
 927	unsigned long fqs_resume_time;
 928	int fqs_burst_remaining;
 929
 930	VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
 931	do {
 932		fqs_resume_time = jiffies + fqs_stutter * HZ;
 933		while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
 934		       !kthread_should_stop()) {
 935			schedule_timeout_interruptible(1);
 936		}
 937		fqs_burst_remaining = fqs_duration;
 938		while (fqs_burst_remaining > 0 &&
 939		       !kthread_should_stop()) {
 940			cur_ops->fqs();
 941			udelay(fqs_holdoff);
 942			fqs_burst_remaining -= fqs_holdoff;
 943		}
 944		rcu_stutter_wait("rcu_torture_fqs");
 945	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
 946	VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
 947	rcutorture_shutdown_absorb("rcu_torture_fqs");
 948	while (!kthread_should_stop())
 949		schedule_timeout_uninterruptible(1);
 950	return 0;
 951}
 952
 953/*
 954 * RCU torture writer kthread.  Repeatedly substitutes a new structure
 955 * for that pointed to by rcu_torture_current, freeing the old structure
 956 * after a series of grace periods (the "pipeline").
 957 */
 958static int
 959rcu_torture_writer(void *arg)
 960{
 961	int i;
 962	long oldbatch = rcu_batches_completed();
 963	struct rcu_torture *rp;
 964	struct rcu_torture *old_rp;
 965	static DEFINE_RCU_RANDOM(rand);
 966
 967	VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
 968	set_user_nice(current, 19);
 969
 970	do {
 971		schedule_timeout_uninterruptible(1);
 972		rp = rcu_torture_alloc();
 973		if (rp == NULL)
 974			continue;
 975		rp->rtort_pipe_count = 0;
 976		udelay(rcu_random(&rand) & 0x3ff);
 977		old_rp = rcu_dereference_check(rcu_torture_current,
 978					       current == writer_task);
 979		rp->rtort_mbtest = 1;
 980		rcu_assign_pointer(rcu_torture_current, rp);
 981		smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
 982		if (old_rp) {
 983			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++;
 988			cur_ops->deferred_free(old_rp);
 989		}
 990		rcutorture_record_progress(++rcu_torture_current_version);
 991		oldbatch = cur_ops->completed();
 992		rcu_stutter_wait("rcu_torture_writer");
 993	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
 994	VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
 995	rcutorture_shutdown_absorb("rcu_torture_writer");
 996	while (!kthread_should_stop())
 997		schedule_timeout_uninterruptible(1);
 998	return 0;
 999}
1000
1001/*
1002 * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
1003 * delay between calls.
1004 */
1005static int
1006rcu_torture_fakewriter(void *arg)
1007{
1008	DEFINE_RCU_RANDOM(rand);
1009
1010	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
1011	set_user_nice(current, 19);
1012
1013	do {
1014		schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
1015		udelay(rcu_random(&rand) & 0x3ff);
1016		cur_ops->sync();
1017		rcu_stutter_wait("rcu_torture_fakewriter");
1018	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1019
1020	VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
1021	rcutorture_shutdown_absorb("rcu_torture_fakewriter");
1022	while (!kthread_should_stop())
1023		schedule_timeout_uninterruptible(1);
1024	return 0;
1025}
1026
1027void rcutorture_trace_dump(void)
1028{
1029	static atomic_t beenhere = ATOMIC_INIT(0);
1030
1031	if (atomic_read(&beenhere))
1032		return;
1033	if (atomic_xchg(&beenhere, 1) != 0)
1034		return;
1035	do_trace_rcu_torture_read(cur_ops->name, (struct rcu_head *)~0UL);
1036	ftrace_dump(DUMP_ALL);
1037}
1038
1039/*
1040 * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
1041 * incrementing the corresponding element of the pipeline array.  The
1042 * counter in the element should never be greater than 1, otherwise, the
1043 * RCU implementation is broken.
1044 */
1045static void rcu_torture_timer(unsigned long unused)
1046{
1047	int idx;
1048	int completed;
1049	static DEFINE_RCU_RANDOM(rand);
1050	static DEFINE_SPINLOCK(rand_lock);
1051	struct rcu_torture *p;
1052	int pipe_count;
1053
1054	idx = cur_ops->readlock();
1055	completed = cur_ops->completed();
1056	p = rcu_dereference_check(rcu_torture_current,
1057				  rcu_read_lock_bh_held() ||
1058				  rcu_read_lock_sched_held() ||
1059				  srcu_read_lock_held(&srcu_ctl));
1060	if (p == NULL) {
1061		/* Leave because rcu_torture_writer is not yet underway */
1062		cur_ops->readunlock(idx);
1063		return;
1064	}
1065	do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
1066	if (p->rtort_mbtest == 0)
1067		atomic_inc(&n_rcu_torture_mberror);
1068	spin_lock(&rand_lock);
1069	cur_ops->read_delay(&rand);
1070	n_rcu_torture_timers++;
1071	spin_unlock(&rand_lock);
1072	preempt_disable();
1073	pipe_count = p->rtort_pipe_count;
1074	if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1075		/* Should not happen, but... */
1076		pipe_count = RCU_TORTURE_PIPE_LEN;
1077	}
1078	if (pipe_count > 1)
1079		rcutorture_trace_dump();
1080	__this_cpu_inc(rcu_torture_count[pipe_count]);
1081	completed = cur_ops->completed() - completed;
1082	if (completed > RCU_TORTURE_PIPE_LEN) {
1083		/* Should not happen, but... */
1084		completed = RCU_TORTURE_PIPE_LEN;
1085	}
1086	__this_cpu_inc(rcu_torture_batch[completed]);
1087	preempt_enable();
1088	cur_ops->readunlock(idx);
1089}
1090
1091/*
1092 * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
1093 * incrementing the corresponding element of the pipeline array.  The
1094 * counter in the element should never be greater than 1, otherwise, the
1095 * RCU implementation is broken.
1096 */
1097static int
1098rcu_torture_reader(void *arg)
1099{
1100	int completed;
1101	int idx;
1102	DEFINE_RCU_RANDOM(rand);
1103	struct rcu_torture *p;
1104	int pipe_count;
1105	struct timer_list t;
1106
1107	VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
1108	set_user_nice(current, 19);
1109	if (irqreader && cur_ops->irq_capable)
1110		setup_timer_on_stack(&t, rcu_torture_timer, 0);
1111
1112	do {
1113		if (irqreader && cur_ops->irq_capable) {
1114			if (!timer_pending(&t))
1115				mod_timer(&t, jiffies + 1);
1116		}
1117		idx = cur_ops->readlock();
1118		completed = cur_ops->completed();
1119		p = rcu_dereference_check(rcu_torture_current,
1120					  rcu_read_lock_bh_held() ||
1121					  rcu_read_lock_sched_held() ||
1122					  srcu_read_lock_held(&srcu_ctl));
1123		if (p == NULL) {
1124			/* Wait for rcu_torture_writer to get underway */
1125			cur_ops->readunlock(idx);
1126			schedule_timeout_interruptible(HZ);
1127			continue;
1128		}
1129		do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
1130		if (p->rtort_mbtest == 0)
1131			atomic_inc(&n_rcu_torture_mberror);
1132		cur_ops->read_delay(&rand);
1133		preempt_disable();
1134		pipe_count = p->rtort_pipe_count;
1135		if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1136			/* Should not happen, but... */
1137			pipe_count = RCU_TORTURE_PIPE_LEN;
1138		}
1139		if (pipe_count > 1)
1140			rcutorture_trace_dump();
1141		__this_cpu_inc(rcu_torture_count[pipe_count]);
1142		completed = cur_ops->completed() - completed;
1143		if (completed > RCU_TORTURE_PIPE_LEN) {
1144			/* Should not happen, but... */
1145			completed = RCU_TORTURE_PIPE_LEN;
1146		}
1147		__this_cpu_inc(rcu_torture_batch[completed]);
1148		preempt_enable();
1149		cur_ops->readunlock(idx);
1150		schedule();
1151		rcu_stutter_wait("rcu_torture_reader");
1152	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1153	VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1154	rcutorture_shutdown_absorb("rcu_torture_reader");
1155	if (irqreader && cur_ops->irq_capable)
1156		del_timer_sync(&t);
1157	while (!kthread_should_stop())
1158		schedule_timeout_uninterruptible(1);
1159	return 0;
1160}
1161
1162/*
1163 * Create an RCU-torture statistics message in the specified buffer.
1164 */
1165static int
1166rcu_torture_printk(char *page)
1167{
1168	int cnt = 0;
1169	int cpu;
1170	int i;
1171	long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1172	long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1173
1174	for_each_possible_cpu(cpu) {
1175		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1176			pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1177			batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1178		}
1179	}
1180	for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1181		if (pipesummary[i] != 0)
1182			break;
1183	}
1184	cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
1185	cnt += sprintf(&page[cnt],
1186		       "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
1187		       "rtmbe: %d rtbke: %ld rtbre: %ld "
1188		       "rtbf: %ld rtb: %ld nt: %ld "
1189		       "onoff: %ld/%ld:%ld/%ld "
1190		       "barrier: %ld/%ld:%ld",
1191		       rcu_torture_current,
1192		       rcu_torture_current_version,
1193		       list_empty(&rcu_torture_freelist),
1194		       atomic_read(&n_rcu_torture_alloc),
1195		       atomic_read(&n_rcu_torture_alloc_fail),
1196		       atomic_read(&n_rcu_torture_free),
1197		       atomic_read(&n_rcu_torture_mberror),
1198		       n_rcu_torture_boost_ktrerror,
1199		       n_rcu_torture_boost_rterror,
1200		       n_rcu_torture_boost_failure,
1201		       n_rcu_torture_boosts,
1202		       n_rcu_torture_timers,
1203		       n_online_successes,
1204		       n_online_attempts,
1205		       n_offline_successes,
1206		       n_offline_attempts,
1207		       n_barrier_successes,
1208		       n_barrier_attempts,
1209		       n_rcu_torture_barrier_error);
1210	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1211	if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1212	    n_rcu_torture_barrier_error != 0 ||
1213	    n_rcu_torture_boost_ktrerror != 0 ||
1214	    n_rcu_torture_boost_rterror != 0 ||
1215	    n_rcu_torture_boost_failure != 0 ||
1216	    i > 1) {
 
 
1217		cnt += sprintf(&page[cnt], "!!! ");
1218		atomic_inc(&n_rcu_torture_error);
1219		WARN_ON_ONCE(1);
1220	}
1221	cnt += sprintf(&page[cnt], "Reader Pipe: ");
1222	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1223		cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
1224	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1225	cnt += sprintf(&page[cnt], "Reader Batch: ");
1226	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1227		cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
1228	cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
1229	cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
1230	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1231		cnt += sprintf(&page[cnt], " %d",
1232			       atomic_read(&rcu_torture_wcount[i]));
1233	}
1234	cnt += sprintf(&page[cnt], "\n");
1235	if (cur_ops->stats)
1236		cnt += cur_ops->stats(&page[cnt]);
1237	return cnt;
1238}
1239
1240/*
1241 * Print torture statistics.  Caller must ensure that there is only
1242 * one call to this function at a given time!!!  This is normally
1243 * accomplished by relying on the module system to only have one copy
1244 * of the module loaded, and then by giving the rcu_torture_stats
1245 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1246 * thread is not running).
1247 */
1248static void
1249rcu_torture_stats_print(void)
1250{
1251	int cnt;
1252
1253	cnt = rcu_torture_printk(printk_buf);
1254	printk(KERN_ALERT "%s", printk_buf);
1255}
1256
1257/*
1258 * Periodically prints torture statistics, if periodic statistics printing
1259 * was specified via the stat_interval module parameter.
1260 *
1261 * No need to worry about fullstop here, since this one doesn't reference
1262 * volatile state or register callbacks.
1263 */
1264static int
1265rcu_torture_stats(void *arg)
1266{
1267	VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1268	do {
1269		schedule_timeout_interruptible(stat_interval * HZ);
1270		rcu_torture_stats_print();
1271		rcutorture_shutdown_absorb("rcu_torture_stats");
1272	} while (!kthread_should_stop());
1273	VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1274	return 0;
1275}
1276
1277static int rcu_idle_cpu;	/* Force all torture tasks off this CPU */
1278
1279/* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1280 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1281 */
1282static void rcu_torture_shuffle_tasks(void)
1283{
1284	int i;
1285
1286	cpumask_setall(shuffle_tmp_mask);
1287	get_online_cpus();
1288
1289	/* No point in shuffling if there is only one online CPU (ex: UP) */
1290	if (num_online_cpus() == 1) {
1291		put_online_cpus();
1292		return;
1293	}
1294
1295	if (rcu_idle_cpu != -1)
1296		cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1297
1298	set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1299
1300	if (reader_tasks) {
1301		for (i = 0; i < nrealreaders; i++)
1302			if (reader_tasks[i])
1303				set_cpus_allowed_ptr(reader_tasks[i],
1304						     shuffle_tmp_mask);
1305	}
1306
1307	if (fakewriter_tasks) {
1308		for (i = 0; i < nfakewriters; i++)
1309			if (fakewriter_tasks[i])
1310				set_cpus_allowed_ptr(fakewriter_tasks[i],
1311						     shuffle_tmp_mask);
1312	}
1313
1314	if (writer_task)
1315		set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1316
1317	if (stats_task)
1318		set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1319
1320	if (rcu_idle_cpu == -1)
1321		rcu_idle_cpu = num_online_cpus() - 1;
1322	else
1323		rcu_idle_cpu--;
1324
1325	put_online_cpus();
1326}
1327
1328/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1329 * system to become idle at a time and cut off its timer ticks. This is meant
1330 * to test the support for such tickless idle CPU in RCU.
1331 */
1332static int
1333rcu_torture_shuffle(void *arg)
1334{
1335	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1336	do {
1337		schedule_timeout_interruptible(shuffle_interval * HZ);
1338		rcu_torture_shuffle_tasks();
1339		rcutorture_shutdown_absorb("rcu_torture_shuffle");
1340	} while (!kthread_should_stop());
1341	VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1342	return 0;
1343}
1344
1345/* Cause the rcutorture test to "stutter", starting and stopping all
1346 * threads periodically.
1347 */
1348static int
1349rcu_torture_stutter(void *arg)
1350{
1351	VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1352	do {
1353		schedule_timeout_interruptible(stutter * HZ);
1354		stutter_pause_test = 1;
1355		if (!kthread_should_stop())
1356			schedule_timeout_interruptible(stutter * HZ);
1357		stutter_pause_test = 0;
1358		rcutorture_shutdown_absorb("rcu_torture_stutter");
1359	} while (!kthread_should_stop());
1360	VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1361	return 0;
1362}
1363
1364static inline void
1365rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
1366{
1367	printk(KERN_ALERT "%s" TORTURE_FLAG
1368		"--- %s: nreaders=%d nfakewriters=%d "
1369		"stat_interval=%d verbose=%d test_no_idle_hz=%d "
1370		"shuffle_interval=%d stutter=%d irqreader=%d "
1371		"fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1372		"test_boost=%d/%d test_boost_interval=%d "
1373		"test_boost_duration=%d shutdown_secs=%d "
1374		"onoff_interval=%d onoff_holdoff=%d\n",
1375		torture_type, tag, nrealreaders, nfakewriters,
1376		stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1377		stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1378		test_boost, cur_ops->can_boost,
1379		test_boost_interval, test_boost_duration, shutdown_secs,
1380		onoff_interval, onoff_holdoff);
1381}
1382
1383static struct notifier_block rcutorture_shutdown_nb = {
1384	.notifier_call = rcutorture_shutdown_notify,
1385};
1386
1387static void rcutorture_booster_cleanup(int cpu)
1388{
1389	struct task_struct *t;
1390
1391	if (boost_tasks[cpu] == NULL)
1392		return;
1393	mutex_lock(&boost_mutex);
1394	VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1395	t = boost_tasks[cpu];
1396	boost_tasks[cpu] = NULL;
1397	mutex_unlock(&boost_mutex);
1398
1399	/* This must be outside of the mutex, otherwise deadlock! */
1400	kthread_stop(t);
1401	boost_tasks[cpu] = NULL;
1402}
1403
1404static int rcutorture_booster_init(int cpu)
1405{
1406	int retval;
1407
1408	if (boost_tasks[cpu] != NULL)
1409		return 0;  /* Already created, nothing more to do. */
1410
1411	/* Don't allow time recalculation while creating a new task. */
1412	mutex_lock(&boost_mutex);
1413	VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1414	boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1415						  cpu_to_node(cpu),
1416						  "rcu_torture_boost");
1417	if (IS_ERR(boost_tasks[cpu])) {
1418		retval = PTR_ERR(boost_tasks[cpu]);
1419		VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1420		n_rcu_torture_boost_ktrerror++;
1421		boost_tasks[cpu] = NULL;
1422		mutex_unlock(&boost_mutex);
1423		return retval;
1424	}
1425	kthread_bind(boost_tasks[cpu], cpu);
1426	wake_up_process(boost_tasks[cpu]);
1427	mutex_unlock(&boost_mutex);
1428	return 0;
1429}
1430
1431/*
1432 * Cause the rcutorture test to shutdown the system after the test has
1433 * run for the time specified by the shutdown_secs module parameter.
1434 */
1435static int
1436rcu_torture_shutdown(void *arg)
1437{
1438	long delta;
1439	unsigned long jiffies_snap;
1440
1441	VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1442	jiffies_snap = ACCESS_ONCE(jiffies);
1443	while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1444	       !kthread_should_stop()) {
1445		delta = shutdown_time - jiffies_snap;
1446		if (verbose)
1447			printk(KERN_ALERT "%s" TORTURE_FLAG
1448			       "rcu_torture_shutdown task: %lu "
1449			       "jiffies remaining\n",
1450			       torture_type, delta);
1451		schedule_timeout_interruptible(delta);
1452		jiffies_snap = ACCESS_ONCE(jiffies);
1453	}
1454	if (kthread_should_stop()) {
1455		VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1456		return 0;
1457	}
1458
1459	/* OK, shut down the system. */
1460
1461	VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1462	shutdown_task = NULL;	/* Avoid self-kill deadlock. */
1463	rcu_torture_cleanup();	/* Get the success/failure message. */
1464	kernel_power_off();	/* Shut down the system. */
1465	return 0;
1466}
1467
1468#ifdef CONFIG_HOTPLUG_CPU
1469
1470/*
1471 * Execute random CPU-hotplug operations at the interval specified
1472 * by the onoff_interval.
1473 */
1474static int __cpuinit
1475rcu_torture_onoff(void *arg)
1476{
1477	int cpu;
1478	int maxcpu = -1;
1479	DEFINE_RCU_RANDOM(rand);
1480
1481	VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1482	for_each_online_cpu(cpu)
1483		maxcpu = cpu;
1484	WARN_ON(maxcpu < 0);
1485	if (onoff_holdoff > 0) {
1486		VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1487		schedule_timeout_interruptible(onoff_holdoff * HZ);
1488		VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1489	}
1490	while (!kthread_should_stop()) {
1491		cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
1492		if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
1493			if (verbose)
1494				printk(KERN_ALERT "%s" TORTURE_FLAG
1495				       "rcu_torture_onoff task: offlining %d\n",
1496				       torture_type, cpu);
1497			n_offline_attempts++;
1498			if (cpu_down(cpu) == 0) {
1499				if (verbose)
1500					printk(KERN_ALERT "%s" TORTURE_FLAG
1501					       "rcu_torture_onoff task: "
1502					       "offlined %d\n",
1503					       torture_type, cpu);
1504				n_offline_successes++;
1505			}
1506		} else if (cpu_is_hotpluggable(cpu)) {
1507			if (verbose)
1508				printk(KERN_ALERT "%s" TORTURE_FLAG
1509				       "rcu_torture_onoff task: onlining %d\n",
1510				       torture_type, cpu);
1511			n_online_attempts++;
1512			if (cpu_up(cpu) == 0) {
1513				if (verbose)
1514					printk(KERN_ALERT "%s" TORTURE_FLAG
1515					       "rcu_torture_onoff task: "
1516					       "onlined %d\n",
1517					       torture_type, cpu);
1518				n_online_successes++;
1519			}
1520		}
1521		schedule_timeout_interruptible(onoff_interval * HZ);
1522	}
1523	VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1524	return 0;
1525}
1526
1527static int __cpuinit
1528rcu_torture_onoff_init(void)
1529{
1530	int ret;
1531
1532	if (onoff_interval <= 0)
1533		return 0;
1534	onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1535	if (IS_ERR(onoff_task)) {
1536		ret = PTR_ERR(onoff_task);
1537		onoff_task = NULL;
1538		return ret;
1539	}
1540	return 0;
1541}
1542
1543static void rcu_torture_onoff_cleanup(void)
1544{
1545	if (onoff_task == NULL)
1546		return;
1547	VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1548	kthread_stop(onoff_task);
1549	onoff_task = NULL;
1550}
1551
1552#else /* #ifdef CONFIG_HOTPLUG_CPU */
1553
1554static int
1555rcu_torture_onoff_init(void)
1556{
1557	return 0;
1558}
1559
1560static void rcu_torture_onoff_cleanup(void)
1561{
1562}
1563
1564#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1565
1566/*
1567 * CPU-stall kthread.  It waits as specified by stall_cpu_holdoff, then
1568 * induces a CPU stall for the time specified by stall_cpu.
1569 */
1570static int __cpuinit rcu_torture_stall(void *args)
1571{
1572	unsigned long stop_at;
1573
1574	VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1575	if (stall_cpu_holdoff > 0) {
1576		VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1577		schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1578		VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1579	}
1580	if (!kthread_should_stop()) {
1581		stop_at = get_seconds() + stall_cpu;
1582		/* RCU CPU stall is expected behavior in following code. */
1583		printk(KERN_ALERT "rcu_torture_stall start.\n");
1584		rcu_read_lock();
1585		preempt_disable();
1586		while (ULONG_CMP_LT(get_seconds(), stop_at))
1587			continue;  /* Induce RCU CPU stall warning. */
1588		preempt_enable();
1589		rcu_read_unlock();
1590		printk(KERN_ALERT "rcu_torture_stall end.\n");
1591	}
1592	rcutorture_shutdown_absorb("rcu_torture_stall");
1593	while (!kthread_should_stop())
1594		schedule_timeout_interruptible(10 * HZ);
1595	return 0;
1596}
1597
1598/* Spawn CPU-stall kthread, if stall_cpu specified. */
1599static int __init rcu_torture_stall_init(void)
1600{
1601	int ret;
1602
1603	if (stall_cpu <= 0)
1604		return 0;
1605	stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1606	if (IS_ERR(stall_task)) {
1607		ret = PTR_ERR(stall_task);
1608		stall_task = NULL;
1609		return ret;
1610	}
1611	return 0;
1612}
1613
1614/* Clean up after the CPU-stall kthread, if one was spawned. */
1615static void rcu_torture_stall_cleanup(void)
1616{
1617	if (stall_task == NULL)
1618		return;
1619	VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1620	kthread_stop(stall_task);
1621	stall_task = NULL;
1622}
1623
1624/* Callback function for RCU barrier testing. */
1625void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1626{
1627	atomic_inc(&barrier_cbs_invoked);
1628}
1629
1630/* kthread function to register callbacks used to test RCU barriers. */
1631static int rcu_torture_barrier_cbs(void *arg)
1632{
1633	long myid = (long)arg;
1634	struct rcu_head rcu;
1635
1636	init_rcu_head_on_stack(&rcu);
1637	VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1638	set_user_nice(current, 19);
1639	do {
1640		wait_event(barrier_cbs_wq[myid],
1641			   atomic_read(&barrier_cbs_count) == n_barrier_cbs ||
1642			   kthread_should_stop() ||
1643			   fullstop != FULLSTOP_DONTSTOP);
1644		if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1645			break;
1646		cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1647		if (atomic_dec_and_test(&barrier_cbs_count))
1648			wake_up(&barrier_wq);
1649	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1650	VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1651	rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1652	while (!kthread_should_stop())
1653		schedule_timeout_interruptible(1);
1654	cur_ops->cb_barrier();
1655	destroy_rcu_head_on_stack(&rcu);
1656	return 0;
1657}
1658
1659/* kthread function to drive and coordinate RCU barrier testing. */
1660static int rcu_torture_barrier(void *arg)
1661{
1662	int i;
1663
1664	VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1665	do {
1666		atomic_set(&barrier_cbs_invoked, 0);
1667		atomic_set(&barrier_cbs_count, n_barrier_cbs);
1668		/* wake_up() path contains the required barriers. */
1669		for (i = 0; i < n_barrier_cbs; i++)
1670			wake_up(&barrier_cbs_wq[i]);
1671		wait_event(barrier_wq,
1672			   atomic_read(&barrier_cbs_count) == 0 ||
1673			   kthread_should_stop() ||
1674			   fullstop != FULLSTOP_DONTSTOP);
1675		if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1676			break;
1677		n_barrier_attempts++;
1678		cur_ops->cb_barrier();
1679		if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1680			n_rcu_torture_barrier_error++;
1681			WARN_ON_ONCE(1);
1682		}
1683		n_barrier_successes++;
1684		schedule_timeout_interruptible(HZ / 10);
1685	} while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1686	VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
1687	rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1688	while (!kthread_should_stop())
1689		schedule_timeout_interruptible(1);
1690	return 0;
1691}
1692
1693/* Initialize RCU barrier testing. */
1694static int rcu_torture_barrier_init(void)
1695{
1696	int i;
1697	int ret;
1698
1699	if (n_barrier_cbs == 0)
1700		return 0;
1701	if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1702		printk(KERN_ALERT "%s" TORTURE_FLAG
1703		       " Call or barrier ops missing for %s,\n",
1704		       torture_type, cur_ops->name);
1705		printk(KERN_ALERT "%s" TORTURE_FLAG
1706		       " RCU barrier testing omitted from run.\n",
1707		       torture_type);
1708		return 0;
1709	}
1710	atomic_set(&barrier_cbs_count, 0);
1711	atomic_set(&barrier_cbs_invoked, 0);
1712	barrier_cbs_tasks =
1713		kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1714			GFP_KERNEL);
1715	barrier_cbs_wq =
1716		kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1717			GFP_KERNEL);
1718	if (barrier_cbs_tasks == NULL || barrier_cbs_wq == 0)
1719		return -ENOMEM;
1720	for (i = 0; i < n_barrier_cbs; i++) {
1721		init_waitqueue_head(&barrier_cbs_wq[i]);
1722		barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
1723						   (void *)(long)i,
1724						   "rcu_torture_barrier_cbs");
1725		if (IS_ERR(barrier_cbs_tasks[i])) {
1726			ret = PTR_ERR(barrier_cbs_tasks[i]);
1727			VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1728			barrier_cbs_tasks[i] = NULL;
1729			return ret;
1730		}
1731	}
1732	barrier_task = kthread_run(rcu_torture_barrier, NULL,
1733				   "rcu_torture_barrier");
1734	if (IS_ERR(barrier_task)) {
1735		ret = PTR_ERR(barrier_task);
1736		VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1737		barrier_task = NULL;
1738	}
1739	return 0;
1740}
1741
1742/* Clean up after RCU barrier testing. */
1743static void rcu_torture_barrier_cleanup(void)
1744{
1745	int i;
1746
1747	if (barrier_task != NULL) {
1748		VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1749		kthread_stop(barrier_task);
1750		barrier_task = NULL;
1751	}
1752	if (barrier_cbs_tasks != NULL) {
1753		for (i = 0; i < n_barrier_cbs; i++) {
1754			if (barrier_cbs_tasks[i] != NULL) {
1755				VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1756				kthread_stop(barrier_cbs_tasks[i]);
1757				barrier_cbs_tasks[i] = NULL;
1758			}
1759		}
1760		kfree(barrier_cbs_tasks);
1761		barrier_cbs_tasks = NULL;
1762	}
1763	if (barrier_cbs_wq != NULL) {
1764		kfree(barrier_cbs_wq);
1765		barrier_cbs_wq = NULL;
1766	}
1767}
1768
1769static int rcutorture_cpu_notify(struct notifier_block *self,
1770				 unsigned long action, void *hcpu)
1771{
1772	long cpu = (long)hcpu;
1773
1774	switch (action) {
1775	case CPU_ONLINE:
1776	case CPU_DOWN_FAILED:
1777		(void)rcutorture_booster_init(cpu);
1778		break;
1779	case CPU_DOWN_PREPARE:
1780		rcutorture_booster_cleanup(cpu);
1781		break;
1782	default:
1783		break;
1784	}
1785	return NOTIFY_OK;
1786}
1787
1788static struct notifier_block rcutorture_cpu_nb = {
1789	.notifier_call = rcutorture_cpu_notify,
1790};
1791
1792static void
1793rcu_torture_cleanup(void)
1794{
1795	int i;
1796
1797	mutex_lock(&fullstop_mutex);
1798	rcutorture_record_test_transition();
1799	if (fullstop == FULLSTOP_SHUTDOWN) {
1800		printk(KERN_WARNING /* but going down anyway, so... */
1801		       "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1802		mutex_unlock(&fullstop_mutex);
1803		schedule_timeout_uninterruptible(10);
1804		if (cur_ops->cb_barrier != NULL)
1805			cur_ops->cb_barrier();
1806		return;
1807	}
1808	fullstop = FULLSTOP_RMMOD;
1809	mutex_unlock(&fullstop_mutex);
1810	unregister_reboot_notifier(&rcutorture_shutdown_nb);
1811	rcu_torture_barrier_cleanup();
1812	rcu_torture_stall_cleanup();
1813	if (stutter_task) {
1814		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1815		kthread_stop(stutter_task);
1816	}
1817	stutter_task = NULL;
1818	if (shuffler_task) {
1819		VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1820		kthread_stop(shuffler_task);
1821		free_cpumask_var(shuffle_tmp_mask);
1822	}
1823	shuffler_task = NULL;
1824
1825	if (writer_task) {
1826		VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1827		kthread_stop(writer_task);
1828	}
1829	writer_task = NULL;
1830
1831	if (reader_tasks) {
1832		for (i = 0; i < nrealreaders; i++) {
1833			if (reader_tasks[i]) {
1834				VERBOSE_PRINTK_STRING(
1835					"Stopping rcu_torture_reader task");
1836				kthread_stop(reader_tasks[i]);
1837			}
1838			reader_tasks[i] = NULL;
1839		}
1840		kfree(reader_tasks);
1841		reader_tasks = NULL;
1842	}
1843	rcu_torture_current = NULL;
1844
1845	if (fakewriter_tasks) {
1846		for (i = 0; i < nfakewriters; i++) {
1847			if (fakewriter_tasks[i]) {
1848				VERBOSE_PRINTK_STRING(
1849					"Stopping rcu_torture_fakewriter task");
1850				kthread_stop(fakewriter_tasks[i]);
1851			}
1852			fakewriter_tasks[i] = NULL;
1853		}
1854		kfree(fakewriter_tasks);
1855		fakewriter_tasks = NULL;
1856	}
1857
1858	if (stats_task) {
1859		VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1860		kthread_stop(stats_task);
1861	}
1862	stats_task = NULL;
1863
1864	if (fqs_task) {
1865		VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1866		kthread_stop(fqs_task);
1867	}
1868	fqs_task = NULL;
1869	if ((test_boost == 1 && cur_ops->can_boost) ||
1870	    test_boost == 2) {
1871		unregister_cpu_notifier(&rcutorture_cpu_nb);
1872		for_each_possible_cpu(i)
1873			rcutorture_booster_cleanup(i);
1874	}
1875	if (shutdown_task != NULL) {
1876		VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1877		kthread_stop(shutdown_task);
1878	}
1879	shutdown_task = NULL;
1880	rcu_torture_onoff_cleanup();
1881
1882	/* Wait for all RCU callbacks to fire.  */
1883
1884	if (cur_ops->cb_barrier != NULL)
1885		cur_ops->cb_barrier();
1886
1887	rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1888
1889	if (cur_ops->cleanup)
1890		cur_ops->cleanup();
1891	if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1892		rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1893	else if (n_online_successes != n_online_attempts ||
1894		 n_offline_successes != n_offline_attempts)
1895		rcu_torture_print_module_parms(cur_ops,
1896					       "End of test: RCU_HOTPLUG");
1897	else
1898		rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1899}
1900
1901static int __init
1902rcu_torture_init(void)
1903{
1904	int i;
1905	int cpu;
1906	int firsterr = 0;
1907	int retval;
1908	static struct rcu_torture_ops *torture_ops[] =
1909		{ &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
1910		  &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
1911		  &srcu_ops, &srcu_sync_ops, &srcu_raw_ops,
1912		  &srcu_raw_sync_ops, &srcu_expedited_ops,
1913		  &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
1914
1915	mutex_lock(&fullstop_mutex);
1916
1917	/* Process args and tell the world that the torturer is on the job. */
1918	for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1919		cur_ops = torture_ops[i];
1920		if (strcmp(torture_type, cur_ops->name) == 0)
1921			break;
1922	}
1923	if (i == ARRAY_SIZE(torture_ops)) {
1924		printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
1925		       torture_type);
1926		printk(KERN_ALERT "rcu-torture types:");
1927		for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1928			printk(KERN_ALERT " %s", torture_ops[i]->name);
1929		printk(KERN_ALERT "\n");
1930		mutex_unlock(&fullstop_mutex);
1931		return -EINVAL;
1932	}
1933	if (cur_ops->fqs == NULL && fqs_duration != 0) {
1934		printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
1935				  "fqs_duration, fqs disabled.\n");
1936		fqs_duration = 0;
1937	}
1938	if (cur_ops->init)
1939		cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1940
1941	if (nreaders >= 0)
1942		nrealreaders = nreaders;
1943	else
1944		nrealreaders = 2 * num_online_cpus();
1945	rcu_torture_print_module_parms(cur_ops, "Start of test");
1946	fullstop = FULLSTOP_DONTSTOP;
1947
1948	/* Set up the freelist. */
1949
1950	INIT_LIST_HEAD(&rcu_torture_freelist);
1951	for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1952		rcu_tortures[i].rtort_mbtest = 0;
1953		list_add_tail(&rcu_tortures[i].rtort_free,
1954			      &rcu_torture_freelist);
1955	}
1956
1957	/* Initialize the statistics so that each run gets its own numbers. */
1958
1959	rcu_torture_current = NULL;
1960	rcu_torture_current_version = 0;
1961	atomic_set(&n_rcu_torture_alloc, 0);
1962	atomic_set(&n_rcu_torture_alloc_fail, 0);
1963	atomic_set(&n_rcu_torture_free, 0);
1964	atomic_set(&n_rcu_torture_mberror, 0);
1965	atomic_set(&n_rcu_torture_error, 0);
1966	n_rcu_torture_barrier_error = 0;
1967	n_rcu_torture_boost_ktrerror = 0;
1968	n_rcu_torture_boost_rterror = 0;
1969	n_rcu_torture_boost_failure = 0;
1970	n_rcu_torture_boosts = 0;
1971	for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1972		atomic_set(&rcu_torture_wcount[i], 0);
1973	for_each_possible_cpu(cpu) {
1974		for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1975			per_cpu(rcu_torture_count, cpu)[i] = 0;
1976			per_cpu(rcu_torture_batch, cpu)[i] = 0;
1977		}
1978	}
1979
1980	/* Start up the kthreads. */
1981
1982	VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1983	writer_task = kthread_run(rcu_torture_writer, NULL,
1984				  "rcu_torture_writer");
1985	if (IS_ERR(writer_task)) {
1986		firsterr = PTR_ERR(writer_task);
1987		VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1988		writer_task = NULL;
1989		goto unwind;
1990	}
1991	fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1992				   GFP_KERNEL);
1993	if (fakewriter_tasks == NULL) {
1994		VERBOSE_PRINTK_ERRSTRING("out of memory");
1995		firsterr = -ENOMEM;
1996		goto unwind;
1997	}
1998	for (i = 0; i < nfakewriters; i++) {
1999		VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
2000		fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
2001						  "rcu_torture_fakewriter");
2002		if (IS_ERR(fakewriter_tasks[i])) {
2003			firsterr = PTR_ERR(fakewriter_tasks[i]);
2004			VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
2005			fakewriter_tasks[i] = NULL;
2006			goto unwind;
2007		}
2008	}
2009	reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
2010			       GFP_KERNEL);
2011	if (reader_tasks == NULL) {
2012		VERBOSE_PRINTK_ERRSTRING("out of memory");
2013		firsterr = -ENOMEM;
2014		goto unwind;
2015	}
2016	for (i = 0; i < nrealreaders; i++) {
2017		VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
2018		reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2019					      "rcu_torture_reader");
2020		if (IS_ERR(reader_tasks[i])) {
2021			firsterr = PTR_ERR(reader_tasks[i]);
2022			VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2023			reader_tasks[i] = NULL;
2024			goto unwind;
2025		}
2026	}
2027	if (stat_interval > 0) {
2028		VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2029		stats_task = kthread_run(rcu_torture_stats, NULL,
2030					"rcu_torture_stats");
2031		if (IS_ERR(stats_task)) {
2032			firsterr = PTR_ERR(stats_task);
2033			VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2034			stats_task = NULL;
2035			goto unwind;
2036		}
2037	}
2038	if (test_no_idle_hz) {
2039		rcu_idle_cpu = num_online_cpus() - 1;
2040
2041		if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2042			firsterr = -ENOMEM;
2043			VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2044			goto unwind;
2045		}
2046
2047		/* Create the shuffler thread */
2048		shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2049					  "rcu_torture_shuffle");
2050		if (IS_ERR(shuffler_task)) {
2051			free_cpumask_var(shuffle_tmp_mask);
2052			firsterr = PTR_ERR(shuffler_task);
2053			VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2054			shuffler_task = NULL;
2055			goto unwind;
2056		}
2057	}
2058	if (stutter < 0)
2059		stutter = 0;
2060	if (stutter) {
2061		/* Create the stutter thread */
2062		stutter_task = kthread_run(rcu_torture_stutter, NULL,
2063					  "rcu_torture_stutter");
2064		if (IS_ERR(stutter_task)) {
2065			firsterr = PTR_ERR(stutter_task);
2066			VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2067			stutter_task = NULL;
2068			goto unwind;
2069		}
2070	}
2071	if (fqs_duration < 0)
2072		fqs_duration = 0;
2073	if (fqs_duration) {
2074		/* Create the stutter thread */
2075		fqs_task = kthread_run(rcu_torture_fqs, NULL,
2076				       "rcu_torture_fqs");
2077		if (IS_ERR(fqs_task)) {
2078			firsterr = PTR_ERR(fqs_task);
2079			VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2080			fqs_task = NULL;
2081			goto unwind;
2082		}
2083	}
2084	if (test_boost_interval < 1)
2085		test_boost_interval = 1;
2086	if (test_boost_duration < 2)
2087		test_boost_duration = 2;
2088	if ((test_boost == 1 && cur_ops->can_boost) ||
2089	    test_boost == 2) {
 
2090
2091		boost_starttime = jiffies + test_boost_interval * HZ;
2092		register_cpu_notifier(&rcutorture_cpu_nb);
2093		for_each_possible_cpu(i) {
2094			if (cpu_is_offline(i))
2095				continue;  /* Heuristic: CPU can go offline. */
2096			retval = rcutorture_booster_init(i);
2097			if (retval < 0) {
2098				firsterr = retval;
2099				goto unwind;
2100			}
2101		}
2102	}
2103	if (shutdown_secs > 0) {
2104		shutdown_time = jiffies + shutdown_secs * HZ;
2105		shutdown_task = kthread_run(rcu_torture_shutdown, NULL,
2106					    "rcu_torture_shutdown");
2107		if (IS_ERR(shutdown_task)) {
2108			firsterr = PTR_ERR(shutdown_task);
2109			VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2110			shutdown_task = NULL;
2111			goto unwind;
2112		}
2113	}
2114	i = rcu_torture_onoff_init();
2115	if (i != 0) {
2116		firsterr = i;
2117		goto unwind;
2118	}
2119	register_reboot_notifier(&rcutorture_shutdown_nb);
2120	i = rcu_torture_stall_init();
2121	if (i != 0) {
2122		firsterr = i;
2123		goto unwind;
2124	}
2125	retval = rcu_torture_barrier_init();
2126	if (retval != 0) {
2127		firsterr = retval;
2128		goto unwind;
2129	}
2130	rcutorture_record_test_transition();
2131	mutex_unlock(&fullstop_mutex);
2132	return 0;
2133
2134unwind:
2135	mutex_unlock(&fullstop_mutex);
2136	rcu_torture_cleanup();
2137	return firsterr;
2138}
2139
2140module_init(rcu_torture_init);
2141module_exit(rcu_torture_cleanup);