blob: bb247beec447042cd010b3ea0b3db5df08f32e0c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -08002/*
3 * Infrastructure for statistic tracing (histogram output).
4 *
Frederic Weisbecker8f184f22009-05-16 06:24:36 +02005 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -08006 *
7 * Based on the code from trace_branch.c which is
8 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
9 *
10 */
11
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -040012#include <linux/security.h>
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080013#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020015#include <linux/rbtree.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050016#include <linux/tracefs.h>
Frederic Weisbecker002bb862009-01-10 11:34:13 -080017#include "trace_stat.h"
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080018#include "trace.h"
19
20
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020021/*
22 * List of stat red-black nodes from a tracer
23 * We use a such tree to sort quickly the stat
24 * entries from the tracer.
25 */
26struct stat_node {
27 struct rb_node node;
Ingo Molnar559221732009-01-15 11:31:21 +010028 void *stat;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080029};
30
Frederic Weisbecker034939b2009-01-08 10:03:56 -080031/* A stat session is the stats output in one file */
Frederic Weisbecker0d64f832009-05-16 05:58:49 +020032struct stat_session {
Frederic Weisbecker002bb862009-01-10 11:34:13 -080033 struct list_head session_list;
Ingo Molnar559221732009-01-15 11:31:21 +010034 struct tracer_stat *ts;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020035 struct rb_root stat_root;
Ingo Molnar559221732009-01-15 11:31:21 +010036 struct mutex stat_mutex;
Frederic Weisbecker002bb862009-01-10 11:34:13 -080037 struct dentry *file;
Frederic Weisbecker034939b2009-01-08 10:03:56 -080038};
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080039
Wenji Huang73d8b8b2009-02-17 01:10:02 -050040/* All of the sessions currently in use. Each stat file embed one session */
Frederic Weisbecker002bb862009-01-10 11:34:13 -080041static LIST_HEAD(all_stat_sessions);
42static DEFINE_MUTEX(all_stat_sessions_mutex);
43
44/* The root directory for all stat files */
Ingo Molnar559221732009-01-15 11:31:21 +010045static struct dentry *stat_dir;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080046
Li Zefan636eace2009-07-23 11:29:47 +080047static void __reset_stat_session(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080048{
Cody P Schafer9cd804a2013-11-01 15:38:46 -070049 struct stat_node *snode, *n;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080050
Cody P Schafer9cd804a2013-11-01 15:38:46 -070051 rbtree_postorder_for_each_entry_safe(snode, n, &session->stat_root, node) {
52 if (session->ts->stat_release)
53 session->ts->stat_release(snode->stat);
54 kfree(snode);
55 }
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080056
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020057 session->stat_root = RB_ROOT;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080058}
59
Li Zefan636eace2009-07-23 11:29:47 +080060static void reset_stat_session(struct stat_session *session)
61{
62 mutex_lock(&session->stat_mutex);
63 __reset_stat_session(session);
64 mutex_unlock(&session->stat_mutex);
65}
66
Frederic Weisbecker0d64f832009-05-16 05:58:49 +020067static void destroy_session(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080068{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050069 tracefs_remove(session->file);
Li Zefan636eace2009-07-23 11:29:47 +080070 __reset_stat_session(session);
Frederic Weisbecker002bb862009-01-10 11:34:13 -080071 mutex_destroy(&session->stat_mutex);
72 kfree(session);
73}
Frederic Weisbecker034939b2009-01-08 10:03:56 -080074
Andy Shevchenko80042c82019-10-07 16:56:56 +030075static int insert_stat(struct rb_root *root, void *stat, cmp_func_t cmp)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020076{
77 struct rb_node **new = &(root->rb_node), *parent = NULL;
Li Zefandbd3fbd2009-05-27 11:42:46 +080078 struct stat_node *data;
79
80 data = kzalloc(sizeof(*data), GFP_KERNEL);
81 if (!data)
82 return -ENOMEM;
83 data->stat = stat;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020084
85 /*
86 * Figure out where to put new node
87 * This is a descendent sorting
88 */
89 while (*new) {
90 struct stat_node *this;
91 int result;
92
93 this = container_of(*new, struct stat_node, node);
94 result = cmp(data->stat, this->stat);
95
96 parent = *new;
97 if (result >= 0)
98 new = &((*new)->rb_left);
99 else
100 new = &((*new)->rb_right);
101 }
102
103 rb_link_node(&data->node, parent, new);
104 rb_insert_color(&data->node, root);
Li Zefandbd3fbd2009-05-27 11:42:46 +0800105 return 0;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200106}
107
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800108/*
109 * For tracers that don't provide a stat_cmp callback.
Li Zefandbd3fbd2009-05-27 11:42:46 +0800110 * This one will force an insertion as right-most node
111 * in the rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800112 */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300113static int dummy_cmp(const void *p1, const void *p2)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800114{
Li Zefanb3dd7ba2009-05-27 11:04:26 +0800115 return -1;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800116}
117
118/*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800119 * Initialize the stat rbtree at each trace_stat file opening.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800120 * All of these copies and sorting are required on all opening
121 * since the stats could have changed between two file sessions.
122 */
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200123static int stat_seq_init(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800124{
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800125 struct tracer_stat *ts = session->ts;
Li Zefandbd3fbd2009-05-27 11:42:46 +0800126 struct rb_root *root = &session->stat_root;
Steven Rostedt09833522009-03-21 02:44:50 -0400127 void *stat;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800128 int ret = 0;
129 int i;
130
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800131 mutex_lock(&session->stat_mutex);
Li Zefan636eace2009-07-23 11:29:47 +0800132 __reset_stat_session(session);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800133
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800134 if (!ts->stat_cmp)
135 ts->stat_cmp = dummy_cmp;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800136
Steven Rostedt42548002009-03-24 13:38:36 -0400137 stat = ts->stat_start(ts);
Steven Rostedt09833522009-03-21 02:44:50 -0400138 if (!stat)
139 goto exit;
140
Li Zefandbd3fbd2009-05-27 11:42:46 +0800141 ret = insert_stat(root, stat, ts->stat_cmp);
142 if (ret)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800143 goto exit;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800144
145 /*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800146 * Iterate over the tracer stat entries and store them in an rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800147 */
148 for (i = 1; ; i++) {
Steven Rostedt09833522009-03-21 02:44:50 -0400149 stat = ts->stat_next(stat, i);
150
151 /* End of insertion */
152 if (!stat)
153 break;
154
Li Zefandbd3fbd2009-05-27 11:42:46 +0800155 ret = insert_stat(root, stat, ts->stat_cmp);
156 if (ret)
157 goto exit_free_rbtree;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800158 }
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200159
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800160exit:
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800161 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800162 return ret;
163
Li Zefandbd3fbd2009-05-27 11:42:46 +0800164exit_free_rbtree:
Li Zefan636eace2009-07-23 11:29:47 +0800165 __reset_stat_session(session);
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800166 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800167 return ret;
168}
169
170
171static void *stat_seq_start(struct seq_file *s, loff_t *pos)
172{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200173 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200174 struct rb_node *node;
Li Zefan97d53202009-08-17 16:52:53 +0800175 int n = *pos;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200176 int i;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800177
Li Zefandbd3fbd2009-05-27 11:42:46 +0800178 /* Prevent from tracer switch or rbtree modification */
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800179 mutex_lock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800180
181 /* If we are in the beginning of the file, print the headers */
Li Zefan97d53202009-08-17 16:52:53 +0800182 if (session->ts->stat_headers) {
183 if (n == 0)
184 return SEQ_START_TOKEN;
185 n--;
186 }
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800187
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200188 node = rb_first(&session->stat_root);
Li Zefan97d53202009-08-17 16:52:53 +0800189 for (i = 0; node && i < n; i++)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200190 node = rb_next(node);
191
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200192 return node;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800193}
194
195static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
196{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200197 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200198 struct rb_node *node = p;
199
200 (*pos)++;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800201
Lai Jiangshane6f48902009-03-25 16:27:17 +0800202 if (p == SEQ_START_TOKEN)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200203 return rb_first(&session->stat_root);
Lai Jiangshane6f48902009-03-25 16:27:17 +0800204
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200205 return rb_next(node);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800206}
207
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800208static void stat_seq_stop(struct seq_file *s, void *p)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800209{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200210 struct stat_session *session = s->private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800211 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800212}
213
214static int stat_seq_show(struct seq_file *s, void *v)
215{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200216 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200217 struct stat_node *l = container_of(v, struct stat_node, node);
Steven Rostedte8a9cbf2009-01-06 22:02:35 -0500218
Lai Jiangshane6f48902009-03-25 16:27:17 +0800219 if (v == SEQ_START_TOKEN)
220 return session->ts->stat_headers(s);
221
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800222 return session->ts->stat_show(s, l->stat);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800223}
224
225static const struct seq_operations trace_stat_seq_ops = {
Ingo Molnar559221732009-01-15 11:31:21 +0100226 .start = stat_seq_start,
227 .next = stat_seq_next,
228 .stop = stat_seq_stop,
229 .show = stat_seq_show
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800230};
231
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800232/* The session stat is refilled and resorted at each stat file opening */
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800233static int tracing_stat_open(struct inode *inode, struct file *file)
234{
235 int ret;
Li Zefan636eace2009-07-23 11:29:47 +0800236 struct seq_file *m;
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200237 struct stat_session *session = inode->i_private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800238
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -0400239 ret = security_locked_down(LOCKDOWN_TRACEFS);
240 if (ret)
241 return ret;
242
Li Zefan636eace2009-07-23 11:29:47 +0800243 ret = stat_seq_init(session);
244 if (ret)
245 return ret;
246
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800247 ret = seq_open(file, &trace_stat_seq_ops);
Li Zefan636eace2009-07-23 11:29:47 +0800248 if (ret) {
249 reset_stat_session(session);
250 return ret;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800251 }
252
Li Zefan636eace2009-07-23 11:29:47 +0800253 m = file->private_data;
254 m->private = session;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800255 return ret;
256}
257
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800258/*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800259 * Avoid consuming memory with our now useless rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800260 */
261static int tracing_stat_release(struct inode *i, struct file *f)
262{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200263 struct stat_session *session = i->i_private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800264
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800265 reset_stat_session(session);
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800266
Li Zefan636eace2009-07-23 11:29:47 +0800267 return seq_release(i, f);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800268}
269
270static const struct file_operations tracing_stat_fops = {
271 .open = tracing_stat_open,
272 .read = seq_read,
273 .llseek = seq_lseek,
274 .release = tracing_stat_release
275};
276
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800277static int tracing_stat_init(void)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800278{
Wei Yang22c36b12020-07-12 09:10:36 +0800279 int ret;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800280
Wei Yang22c36b12020-07-12 09:10:36 +0800281 ret = tracing_init_dentry();
282 if (ret)
Luis Henriquesafccc002014-09-09 22:49:41 +0100283 return -ENODEV;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800284
Wei Yang22c36b12020-07-12 09:10:36 +0800285 stat_dir = tracefs_create_dir("trace_stat", NULL);
Luis Henriquesafccc002014-09-09 22:49:41 +0100286 if (!stat_dir) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700287 pr_warn("Could not create tracefs 'trace_stat' entry\n");
Luis Henriquesafccc002014-09-09 22:49:41 +0100288 return -ENOMEM;
289 }
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800290 return 0;
291}
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800292
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200293static int init_stat_file(struct stat_session *session)
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800294{
Luis Henriquesafccc002014-09-09 22:49:41 +0100295 int ret;
296
297 if (!stat_dir && (ret = tracing_stat_init()))
298 return ret;
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800299
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -0400300 session->file = tracefs_create_file(session->ts->name, TRACE_MODE_WRITE,
301 stat_dir, session,
302 &tracing_stat_fops);
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800303 if (!session->file)
304 return -ENOMEM;
305 return 0;
306}
Ingo Molnar559221732009-01-15 11:31:21 +0100307
308int register_stat_tracer(struct tracer_stat *trace)
309{
Frederic Weisbecker43bd1232009-05-30 04:25:30 +0200310 struct stat_session *session, *node;
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500311 int ret = -EINVAL;
Ingo Molnar559221732009-01-15 11:31:21 +0100312
313 if (!trace)
314 return -EINVAL;
315
316 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
317 return -EINVAL;
318
319 /* Already registered? */
320 mutex_lock(&all_stat_sessions_mutex);
Frederic Weisbecker43bd1232009-05-30 04:25:30 +0200321 list_for_each_entry(node, &all_stat_sessions, session_list) {
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500322 if (node->ts == trace)
323 goto out;
Ingo Molnar559221732009-01-15 11:31:21 +0100324 }
Ingo Molnar559221732009-01-15 11:31:21 +0100325
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500326 ret = -ENOMEM;
Ingo Molnar559221732009-01-15 11:31:21 +0100327 /* Init the session */
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200328 session = kzalloc(sizeof(*session), GFP_KERNEL);
Ingo Molnar559221732009-01-15 11:31:21 +0100329 if (!session)
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500330 goto out;
Ingo Molnar559221732009-01-15 11:31:21 +0100331
332 session->ts = trace;
333 INIT_LIST_HEAD(&session->session_list);
Ingo Molnar559221732009-01-15 11:31:21 +0100334 mutex_init(&session->stat_mutex);
Ingo Molnar559221732009-01-15 11:31:21 +0100335
336 ret = init_stat_file(session);
337 if (ret) {
338 destroy_session(session);
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500339 goto out;
Ingo Molnar559221732009-01-15 11:31:21 +0100340 }
341
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500342 ret = 0;
Ingo Molnar559221732009-01-15 11:31:21 +0100343 /* Register */
Ingo Molnar559221732009-01-15 11:31:21 +0100344 list_add_tail(&session->session_list, &all_stat_sessions);
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500345 out:
Ingo Molnar559221732009-01-15 11:31:21 +0100346 mutex_unlock(&all_stat_sessions_mutex);
347
Steven Rostedt (VMware)dfb6cd12020-01-24 17:47:49 -0500348 return ret;
Ingo Molnar559221732009-01-15 11:31:21 +0100349}
350
351void unregister_stat_tracer(struct tracer_stat *trace)
352{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200353 struct stat_session *node, *tmp;
Ingo Molnar559221732009-01-15 11:31:21 +0100354
355 mutex_lock(&all_stat_sessions_mutex);
356 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
357 if (node->ts == trace) {
358 list_del(&node->session_list);
359 destroy_session(node);
360 break;
361 }
362 }
363 mutex_unlock(&all_stat_sessions_mutex);
364}