1/*
2 * Read-Copy Update tracing for classic implementation
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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright IBM Corporation, 2008
19 *
20 * Papers:  http://www.rdrop.com/users/paulmck/RCU
21 *
22 * For detailed explanation of Read-Copy Update mechanism see -
23 *		Documentation/RCU
24 *
25 */
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/spinlock.h>
30#include <linux/smp.h>
31#include <linux/rcupdate.h>
32#include <linux/interrupt.h>
33#include <linux/sched.h>
34#include <linux/atomic.h>
35#include <linux/bitops.h>
36#include <linux/module.h>
37#include <linux/completion.h>
38#include <linux/moduleparam.h>
39#include <linux/percpu.h>
40#include <linux/notifier.h>
41#include <linux/cpu.h>
42#include <linux/mutex.h>
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
45
46#define RCU_TREE_NONCORE
47#include "tree.h"
48
49DECLARE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
50
51static int r_open(struct inode *inode, struct file *file,
52					const struct seq_operations *op)
53{
54	int ret = seq_open(file, op);
55	if (!ret) {
56		struct seq_file *m = (struct seq_file *)file->private_data;
57		m->private = inode->i_private;
58	}
59	return ret;
60}
61
62static void *r_start(struct seq_file *m, loff_t *pos)
63{
64	struct rcu_state *rsp = (struct rcu_state *)m->private;
65	*pos = cpumask_next(*pos - 1, cpu_possible_mask);
66	if ((*pos) < nr_cpu_ids)
67		return per_cpu_ptr(rsp->rda, *pos);
68	return NULL;
69}
70
71static void *r_next(struct seq_file *m, void *v, loff_t *pos)
72{
73	(*pos)++;
74	return r_start(m, pos);
75}
76
77static void r_stop(struct seq_file *m, void *v)
78{
79}
80
81static int show_rcubarrier(struct seq_file *m, void *v)
82{
83	struct rcu_state *rsp = (struct rcu_state *)m->private;
84	seq_printf(m, "bcc: %d nbd: %lu\n",
85		   atomic_read(&rsp->barrier_cpu_count),
86		   rsp->n_barrier_done);
87	return 0;
88}
89
90static int rcubarrier_open(struct inode *inode, struct file *file)
91{
92	return single_open(file, show_rcubarrier, inode->i_private);
93}
94
95static const struct file_operations rcubarrier_fops = {
96	.owner = THIS_MODULE,
97	.open = rcubarrier_open,
98	.read = seq_read,
99	.llseek = no_llseek,
100	.release = single_release,
101};
102
103#ifdef CONFIG_RCU_BOOST
104
105static char convert_kthread_status(unsigned int kthread_status)
106{
107	if (kthread_status > RCU_KTHREAD_MAX)
108		return '?';
109	return "SRWOY"[kthread_status];
110}
111
112#endif /* #ifdef CONFIG_RCU_BOOST */
113
114static void print_one_rcu_data(struct seq_file *m, struct rcu_data *rdp)
115{
116	long ql, qll;
117
118	if (!rdp->beenonline)
119		return;
120	seq_printf(m, "%3d%cc=%ld g=%ld pq=%d/%d qp=%d",
121		   rdp->cpu,
122		   cpu_is_offline(rdp->cpu) ? '!' : ' ',
123		   ulong2long(rdp->completed), ulong2long(rdp->gpnum),
124		   rdp->passed_quiesce,
125		   rdp->rcu_qs_ctr_snap == per_cpu(rcu_qs_ctr, rdp->cpu),
126		   rdp->qs_pending);
127	seq_printf(m, " dt=%d/%llx/%d df=%lu",
128		   atomic_read(&rdp->dynticks->dynticks),
129		   rdp->dynticks->dynticks_nesting,
130		   rdp->dynticks->dynticks_nmi_nesting,
131		   rdp->dynticks_fqs);
132	seq_printf(m, " of=%lu", rdp->offline_fqs);
133	rcu_nocb_q_lengths(rdp, &ql, &qll);
134	qll += rdp->qlen_lazy;
135	ql += rdp->qlen;
136	seq_printf(m, " ql=%ld/%ld qs=%c%c%c%c",
137		   qll, ql,
138		   ".N"[rdp->nxttail[RCU_NEXT_READY_TAIL] !=
139			rdp->nxttail[RCU_NEXT_TAIL]],
140		   ".R"[rdp->nxttail[RCU_WAIT_TAIL] !=
141			rdp->nxttail[RCU_NEXT_READY_TAIL]],
142		   ".W"[rdp->nxttail[RCU_DONE_TAIL] !=
143			rdp->nxttail[RCU_WAIT_TAIL]],
144		   ".D"[&rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL]]);
145#ifdef CONFIG_RCU_BOOST
146	seq_printf(m, " kt=%d/%c ktl=%x",
147		   per_cpu(rcu_cpu_has_work, rdp->cpu),
148		   convert_kthread_status(per_cpu(rcu_cpu_kthread_status,
149					  rdp->cpu)),
150		   per_cpu(rcu_cpu_kthread_loops, rdp->cpu) & 0xffff);
151#endif /* #ifdef CONFIG_RCU_BOOST */
152	seq_printf(m, " b=%ld", rdp->blimit);
153	seq_printf(m, " ci=%lu nci=%lu co=%lu ca=%lu\n",
154		   rdp->n_cbs_invoked, rdp->n_nocbs_invoked,
155		   rdp->n_cbs_orphaned, rdp->n_cbs_adopted);
156}
157
158static int show_rcudata(struct seq_file *m, void *v)
159{
160	print_one_rcu_data(m, (struct rcu_data *)v);
161	return 0;
162}
163
164static const struct seq_operations rcudate_op = {
165	.start = r_start,
166	.next  = r_next,
167	.stop  = r_stop,
168	.show  = show_rcudata,
169};
170
171static int rcudata_open(struct inode *inode, struct file *file)
172{
173	return r_open(inode, file, &rcudate_op);
174}
175
176static const struct file_operations rcudata_fops = {
177	.owner = THIS_MODULE,
178	.open = rcudata_open,
179	.read = seq_read,
180	.llseek = no_llseek,
181	.release = seq_release,
182};
183
184static int show_rcuexp(struct seq_file *m, void *v)
185{
186	struct rcu_state *rsp = (struct rcu_state *)m->private;
187
188	seq_printf(m, "s=%lu d=%lu w=%lu tf=%lu wd1=%lu wd2=%lu n=%lu sc=%lu dt=%lu dl=%lu dx=%lu\n",
189		   atomic_long_read(&rsp->expedited_start),
190		   atomic_long_read(&rsp->expedited_done),
191		   atomic_long_read(&rsp->expedited_wrap),
192		   atomic_long_read(&rsp->expedited_tryfail),
193		   atomic_long_read(&rsp->expedited_workdone1),
194		   atomic_long_read(&rsp->expedited_workdone2),
195		   atomic_long_read(&rsp->expedited_normal),
196		   atomic_long_read(&rsp->expedited_stoppedcpus),
197		   atomic_long_read(&rsp->expedited_done_tries),
198		   atomic_long_read(&rsp->expedited_done_lost),
199		   atomic_long_read(&rsp->expedited_done_exit));
200	return 0;
201}
202
203static int rcuexp_open(struct inode *inode, struct file *file)
204{
205	return single_open(file, show_rcuexp, inode->i_private);
206}
207
208static const struct file_operations rcuexp_fops = {
209	.owner = THIS_MODULE,
210	.open = rcuexp_open,
211	.read = seq_read,
212	.llseek = no_llseek,
213	.release = single_release,
214};
215
216#ifdef CONFIG_RCU_BOOST
217
218static void print_one_rcu_node_boost(struct seq_file *m, struct rcu_node *rnp)
219{
220	seq_printf(m, "%d:%d tasks=%c%c%c%c kt=%c ntb=%lu neb=%lu nnb=%lu ",
221		   rnp->grplo, rnp->grphi,
222		   "T."[list_empty(&rnp->blkd_tasks)],
223		   "N."[!rnp->gp_tasks],
224		   "E."[!rnp->exp_tasks],
225		   "B."[!rnp->boost_tasks],
226		   convert_kthread_status(rnp->boost_kthread_status),
227		   rnp->n_tasks_boosted, rnp->n_exp_boosts,
228		   rnp->n_normal_boosts);
229	seq_printf(m, "j=%04x bt=%04x\n",
230		   (int)(jiffies & 0xffff),
231		   (int)(rnp->boost_time & 0xffff));
232	seq_printf(m, "    balk: nt=%lu egt=%lu bt=%lu nb=%lu ny=%lu nos=%lu\n",
233		   rnp->n_balk_blkd_tasks,
234		   rnp->n_balk_exp_gp_tasks,
235		   rnp->n_balk_boost_tasks,
236		   rnp->n_balk_notblocked,
237		   rnp->n_balk_notyet,
238		   rnp->n_balk_nos);
239}
240
241static int show_rcu_node_boost(struct seq_file *m, void *unused)
242{
243	struct rcu_node *rnp;
244
245	rcu_for_each_leaf_node(&rcu_preempt_state, rnp)
246		print_one_rcu_node_boost(m, rnp);
247	return 0;
248}
249
250static int rcu_node_boost_open(struct inode *inode, struct file *file)
251{
252	return single_open(file, show_rcu_node_boost, NULL);
253}
254
255static const struct file_operations rcu_node_boost_fops = {
256	.owner = THIS_MODULE,
257	.open = rcu_node_boost_open,
258	.read = seq_read,
259	.llseek = no_llseek,
260	.release = single_release,
261};
262
263#endif /* #ifdef CONFIG_RCU_BOOST */
264
265static void print_one_rcu_state(struct seq_file *m, struct rcu_state *rsp)
266{
267	unsigned long gpnum;
268	int level = 0;
269	struct rcu_node *rnp;
270
271	gpnum = rsp->gpnum;
272	seq_printf(m, "c=%ld g=%ld s=%d jfq=%ld j=%x ",
273		   ulong2long(rsp->completed), ulong2long(gpnum),
274		   rsp->fqs_state,
275		   (long)(rsp->jiffies_force_qs - jiffies),
276		   (int)(jiffies & 0xffff));
277	seq_printf(m, "nfqs=%lu/nfqsng=%lu(%lu) fqlh=%lu oqlen=%ld/%ld\n",
278		   rsp->n_force_qs, rsp->n_force_qs_ngp,
279		   rsp->n_force_qs - rsp->n_force_qs_ngp,
280		   ACCESS_ONCE(rsp->n_force_qs_lh), rsp->qlen_lazy, rsp->qlen);
281	for (rnp = &rsp->node[0]; rnp - &rsp->node[0] < rcu_num_nodes; rnp++) {
282		if (rnp->level != level) {
283			seq_puts(m, "\n");
284			level = rnp->level;
285		}
286		seq_printf(m, "%lx/%lx->%lx %c%c>%c %d:%d ^%d    ",
287			   rnp->qsmask, rnp->qsmaskinit, rnp->qsmaskinitnext,
288			   ".G"[rnp->gp_tasks != NULL],
289			   ".E"[rnp->exp_tasks != NULL],
290			   ".T"[!list_empty(&rnp->blkd_tasks)],
291			   rnp->grplo, rnp->grphi, rnp->grpnum);
292	}
293	seq_puts(m, "\n");
294}
295
296static int show_rcuhier(struct seq_file *m, void *v)
297{
298	struct rcu_state *rsp = (struct rcu_state *)m->private;
299	print_one_rcu_state(m, rsp);
300	return 0;
301}
302
303static int rcuhier_open(struct inode *inode, struct file *file)
304{
305	return single_open(file, show_rcuhier, inode->i_private);
306}
307
308static const struct file_operations rcuhier_fops = {
309	.owner = THIS_MODULE,
310	.open = rcuhier_open,
311	.read = seq_read,
312	.llseek = no_llseek,
313	.release = single_release,
314};
315
316static void show_one_rcugp(struct seq_file *m, struct rcu_state *rsp)
317{
318	unsigned long flags;
319	unsigned long completed;
320	unsigned long gpnum;
321	unsigned long gpage;
322	unsigned long gpmax;
323	struct rcu_node *rnp = &rsp->node[0];
324
325	raw_spin_lock_irqsave(&rnp->lock, flags);
326	completed = ACCESS_ONCE(rsp->completed);
327	gpnum = ACCESS_ONCE(rsp->gpnum);
328	if (completed == gpnum)
329		gpage = 0;
330	else
331		gpage = jiffies - rsp->gp_start;
332	gpmax = rsp->gp_max;
333	raw_spin_unlock_irqrestore(&rnp->lock, flags);
334	seq_printf(m, "completed=%ld  gpnum=%ld  age=%ld  max=%ld\n",
335		   ulong2long(completed), ulong2long(gpnum), gpage, gpmax);
336}
337
338static int show_rcugp(struct seq_file *m, void *v)
339{
340	struct rcu_state *rsp = (struct rcu_state *)m->private;
341	show_one_rcugp(m, rsp);
342	return 0;
343}
344
345static int rcugp_open(struct inode *inode, struct file *file)
346{
347	return single_open(file, show_rcugp, inode->i_private);
348}
349
350static const struct file_operations rcugp_fops = {
351	.owner = THIS_MODULE,
352	.open = rcugp_open,
353	.read = seq_read,
354	.llseek = no_llseek,
355	.release = single_release,
356};
357
358static void print_one_rcu_pending(struct seq_file *m, struct rcu_data *rdp)
359{
360	if (!rdp->beenonline)
361		return;
362	seq_printf(m, "%3d%cnp=%ld ",
363		   rdp->cpu,
364		   cpu_is_offline(rdp->cpu) ? '!' : ' ',
365		   rdp->n_rcu_pending);
366	seq_printf(m, "qsp=%ld rpq=%ld cbr=%ld cng=%ld ",
367		   rdp->n_rp_qs_pending,
368		   rdp->n_rp_report_qs,
369		   rdp->n_rp_cb_ready,
370		   rdp->n_rp_cpu_needs_gp);
371	seq_printf(m, "gpc=%ld gps=%ld nn=%ld ndw%ld\n",
372		   rdp->n_rp_gp_completed,
373		   rdp->n_rp_gp_started,
374		   rdp->n_rp_nocb_defer_wakeup,
375		   rdp->n_rp_need_nothing);
376}
377
378static int show_rcu_pending(struct seq_file *m, void *v)
379{
380	print_one_rcu_pending(m, (struct rcu_data *)v);
381	return 0;
382}
383
384static const struct seq_operations rcu_pending_op = {
385	.start = r_start,
386	.next  = r_next,
387	.stop  = r_stop,
388	.show  = show_rcu_pending,
389};
390
391static int rcu_pending_open(struct inode *inode, struct file *file)
392{
393	return r_open(inode, file, &rcu_pending_op);
394}
395
396static const struct file_operations rcu_pending_fops = {
397	.owner = THIS_MODULE,
398	.open = rcu_pending_open,
399	.read = seq_read,
400	.llseek = no_llseek,
401	.release = seq_release,
402};
403
404static int show_rcutorture(struct seq_file *m, void *unused)
405{
406	seq_printf(m, "rcutorture test sequence: %lu %s\n",
407		   rcutorture_testseq >> 1,
408		   (rcutorture_testseq & 0x1) ? "(test in progress)" : "");
409	seq_printf(m, "rcutorture update version number: %lu\n",
410		   rcutorture_vernum);
411	return 0;
412}
413
414static int rcutorture_open(struct inode *inode, struct file *file)
415{
416	return single_open(file, show_rcutorture, NULL);
417}
418
419static const struct file_operations rcutorture_fops = {
420	.owner = THIS_MODULE,
421	.open = rcutorture_open,
422	.read = seq_read,
423	.llseek = seq_lseek,
424	.release = single_release,
425};
426
427static struct dentry *rcudir;
428
429static int __init rcutree_trace_init(void)
430{
431	struct rcu_state *rsp;
432	struct dentry *retval;
433	struct dentry *rspdir;
434
435	rcudir = debugfs_create_dir("rcu", NULL);
436	if (!rcudir)
437		goto free_out;
438
439	for_each_rcu_flavor(rsp) {
440		rspdir = debugfs_create_dir(rsp->name, rcudir);
441		if (!rspdir)
442			goto free_out;
443
444		retval = debugfs_create_file("rcudata", 0444,
445				rspdir, rsp, &rcudata_fops);
446		if (!retval)
447			goto free_out;
448
449		retval = debugfs_create_file("rcuexp", 0444,
450				rspdir, rsp, &rcuexp_fops);
451		if (!retval)
452			goto free_out;
453
454		retval = debugfs_create_file("rcu_pending", 0444,
455				rspdir, rsp, &rcu_pending_fops);
456		if (!retval)
457			goto free_out;
458
459		retval = debugfs_create_file("rcubarrier", 0444,
460				rspdir, rsp, &rcubarrier_fops);
461		if (!retval)
462			goto free_out;
463
464#ifdef CONFIG_RCU_BOOST
465		if (rsp == &rcu_preempt_state) {
466			retval = debugfs_create_file("rcuboost", 0444,
467				rspdir, NULL, &rcu_node_boost_fops);
468			if (!retval)
469				goto free_out;
470		}
471#endif
472
473		retval = debugfs_create_file("rcugp", 0444,
474				rspdir, rsp, &rcugp_fops);
475		if (!retval)
476			goto free_out;
477
478		retval = debugfs_create_file("rcuhier", 0444,
479				rspdir, rsp, &rcuhier_fops);
480		if (!retval)
481			goto free_out;
482	}
483
484	retval = debugfs_create_file("rcutorture", 0444, rcudir,
485						NULL, &rcutorture_fops);
486	if (!retval)
487		goto free_out;
488	return 0;
489free_out:
490	debugfs_remove_recursive(rcudir);
491	return 1;
492}
493
494static void __exit rcutree_trace_cleanup(void)
495{
496	debugfs_remove_recursive(rcudir);
497}
498
499
500module_init(rcutree_trace_init);
501module_exit(rcutree_trace_cleanup);
502
503MODULE_AUTHOR("Paul E. McKenney");
504MODULE_DESCRIPTION("Read-Copy Update tracing for hierarchical implementation");
505MODULE_LICENSE("GPL");
506