Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / kernel / trace / trace_stat.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
dbd0b4b3
FW
2/*
3 * Infrastructure for statistic tracing (histogram output).
4 *
8f184f27 5 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
dbd0b4b3
FW
6 *
7 * Based on the code from trace_branch.c which is
8 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
9 *
10 */
11
17911ff3 12#include <linux/security.h>
dbd0b4b3 13#include <linux/list.h>
5a0e3ad6 14#include <linux/slab.h>
8f184f27 15#include <linux/rbtree.h>
8434dc93 16#include <linux/tracefs.h>
002bb86d 17#include "trace_stat.h"
dbd0b4b3
FW
18#include "trace.h"
19
20
8f184f27
FW
21/*
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;
55922173 28 void *stat;
dbd0b4b3
FW
29};
30
034939b6 31/* A stat session is the stats output in one file */
0d64f834 32struct stat_session {
002bb86d 33 struct list_head session_list;
55922173 34 struct tracer_stat *ts;
8f184f27 35 struct rb_root stat_root;
55922173 36 struct mutex stat_mutex;
002bb86d 37 struct dentry *file;
034939b6 38};
dbd0b4b3 39
73d8b8bc 40/* All of the sessions currently in use. Each stat file embed one session */
002bb86d
FW
41static LIST_HEAD(all_stat_sessions);
42static DEFINE_MUTEX(all_stat_sessions_mutex);
43
44/* The root directory for all stat files */
55922173 45static struct dentry *stat_dir;
dbd0b4b3 46
9cd804ac 47static void __reset_stat_session(struct stat_session *session)
8f184f27 48{
9cd804ac 49 struct stat_node *snode, *n;
8f184f27 50
9cd804ac
CS
51 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);
8f184f27 54 kfree(snode);
8f184f27 55 }
dbd0b4b3 56
8f184f27 57 session->stat_root = RB_ROOT;
dbd0b4b3
FW
58}
59
636eacee
LZ
60static 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
0d64f834 67static void destroy_session(struct stat_session *session)
dbd0b4b3 68{
8434dc93 69 tracefs_remove(session->file);
636eacee 70 __reset_stat_session(session);
002bb86d
FW
71 mutex_destroy(&session->stat_mutex);
72 kfree(session);
73}
034939b6 74
80042c8f 75static int insert_stat(struct rb_root *root, void *stat, cmp_func_t cmp)
8f184f27
FW
76{
77 struct rb_node **new = &(root->rb_node), *parent = NULL;
dbd3fbdf
LZ
78 struct stat_node *data;
79
80 data = kzalloc(sizeof(*data), GFP_KERNEL);
81 if (!data)
82 return -ENOMEM;
83 data->stat = stat;
8f184f27
FW
84
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);
dbd3fbdf 105 return 0;
8f184f27
FW
106}
107
dbd0b4b3
FW
108/*
109 * For tracers that don't provide a stat_cmp callback.
dbd3fbdf
LZ
110 * This one will force an insertion as right-most node
111 * in the rbtree.
dbd0b4b3 112 */
80042c8f 113static int dummy_cmp(const void *p1, const void *p2)
dbd0b4b3 114{
b3dd7ba7 115 return -1;
dbd0b4b3
FW
116}
117
118/*
dbd3fbdf 119 * Initialize the stat rbtree at each trace_stat file opening.
dbd0b4b3
FW
120 * All of these copies and sorting are required on all opening
121 * since the stats could have changed between two file sessions.
122 */
0d64f834 123static int stat_seq_init(struct stat_session *session)
dbd0b4b3 124{
034939b6 125 struct tracer_stat *ts = session->ts;
dbd3fbdf 126 struct rb_root *root = &session->stat_root;
09833521 127 void *stat;
dbd0b4b3
FW
128 int ret = 0;
129 int i;
130
08b76731 131 guard(mutex)(&session->stat_mutex);
636eacee 132 __reset_stat_session(session);
dbd0b4b3 133
034939b6
FW
134 if (!ts->stat_cmp)
135 ts->stat_cmp = dummy_cmp;
dbd0b4b3 136
42548008 137 stat = ts->stat_start(ts);
09833521 138 if (!stat)
08b76731 139 return 0;
09833521 140
dbd3fbdf
LZ
141 ret = insert_stat(root, stat, ts->stat_cmp);
142 if (ret)
08b76731 143 return ret;
dbd0b4b3
FW
144
145 /*
dbd3fbdf 146 * Iterate over the tracer stat entries and store them in an rbtree.
dbd0b4b3
FW
147 */
148 for (i = 1; ; i++) {
09833521
SR
149 stat = ts->stat_next(stat, i);
150
151 /* End of insertion */
152 if (!stat)
153 break;
154
dbd3fbdf
LZ
155 ret = insert_stat(root, stat, ts->stat_cmp);
156 if (ret)
157 goto exit_free_rbtree;
dbd0b4b3 158 }
8f184f27 159
dbd0b4b3
FW
160 return ret;
161
dbd3fbdf 162exit_free_rbtree:
636eacee 163 __reset_stat_session(session);
dbd0b4b3
FW
164 return ret;
165}
166
167
168static void *stat_seq_start(struct seq_file *s, loff_t *pos)
169{
0d64f834 170 struct stat_session *session = s->private;
8f184f27 171 struct rb_node *node;
97d53202 172 int n = *pos;
8f184f27 173 int i;
dbd0b4b3 174
dbd3fbdf 175 /* Prevent from tracer switch or rbtree modification */
034939b6 176 mutex_lock(&session->stat_mutex);
dbd0b4b3
FW
177
178 /* If we are in the beginning of the file, print the headers */
97d53202
LZ
179 if (session->ts->stat_headers) {
180 if (n == 0)
181 return SEQ_START_TOKEN;
182 n--;
183 }
dbd0b4b3 184
8f184f27 185 node = rb_first(&session->stat_root);
97d53202 186 for (i = 0; node && i < n; i++)
8f184f27
FW
187 node = rb_next(node);
188
8f184f27 189 return node;
dbd0b4b3
FW
190}
191
192static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
193{
0d64f834 194 struct stat_session *session = s->private;
8f184f27
FW
195 struct rb_node *node = p;
196
197 (*pos)++;
dbd0b4b3 198
e6f48901 199 if (p == SEQ_START_TOKEN)
8f184f27 200 return rb_first(&session->stat_root);
e6f48901 201
8f184f27 202 return rb_next(node);
dbd0b4b3
FW
203}
204
034939b6 205static void stat_seq_stop(struct seq_file *s, void *p)
dbd0b4b3 206{
0d64f834 207 struct stat_session *session = s->private;
034939b6 208 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
209}
210
211static int stat_seq_show(struct seq_file *s, void *v)
212{
0d64f834 213 struct stat_session *session = s->private;
8f184f27 214 struct stat_node *l = container_of(v, struct stat_node, node);
ff288b27 215
e6f48901
LJ
216 if (v == SEQ_START_TOKEN)
217 return session->ts->stat_headers(s);
218
034939b6 219 return session->ts->stat_show(s, l->stat);
dbd0b4b3
FW
220}
221
222static const struct seq_operations trace_stat_seq_ops = {
55922173
IM
223 .start = stat_seq_start,
224 .next = stat_seq_next,
225 .stop = stat_seq_stop,
226 .show = stat_seq_show
dbd0b4b3
FW
227};
228
034939b6 229/* The session stat is refilled and resorted at each stat file opening */
dbd0b4b3
FW
230static int tracing_stat_open(struct inode *inode, struct file *file)
231{
232 int ret;
636eacee 233 struct seq_file *m;
0d64f834 234 struct stat_session *session = inode->i_private;
034939b6 235
17911ff3
SRV
236 ret = security_locked_down(LOCKDOWN_TRACEFS);
237 if (ret)
238 return ret;
239
636eacee
LZ
240 ret = stat_seq_init(session);
241 if (ret)
242 return ret;
243
dbd0b4b3 244 ret = seq_open(file, &trace_stat_seq_ops);
636eacee
LZ
245 if (ret) {
246 reset_stat_session(session);
247 return ret;
dbd0b4b3
FW
248 }
249
636eacee
LZ
250 m = file->private_data;
251 m->private = session;
dbd0b4b3
FW
252 return ret;
253}
254
dbd0b4b3 255/*
dbd3fbdf 256 * Avoid consuming memory with our now useless rbtree.
dbd0b4b3
FW
257 */
258static int tracing_stat_release(struct inode *i, struct file *f)
259{
0d64f834 260 struct stat_session *session = i->i_private;
034939b6 261
034939b6 262 reset_stat_session(session);
034939b6 263
636eacee 264 return seq_release(i, f);
dbd0b4b3
FW
265}
266
267static const struct file_operations tracing_stat_fops = {
268 .open = tracing_stat_open,
269 .read = seq_read,
270 .llseek = seq_lseek,
271 .release = tracing_stat_release
272};
273
002bb86d 274static int tracing_stat_init(void)
dbd0b4b3 275{
22c36b18 276 int ret;
dbd0b4b3 277
22c36b18
WY
278 ret = tracing_init_dentry();
279 if (ret)
afccc00f 280 return -ENODEV;
dbd0b4b3 281
22c36b18 282 stat_dir = tracefs_create_dir("trace_stat", NULL);
afccc00f 283 if (!stat_dir) {
a395d6a7 284 pr_warn("Could not create tracefs 'trace_stat' entry\n");
afccc00f
LH
285 return -ENOMEM;
286 }
dbd0b4b3
FW
287 return 0;
288}
002bb86d 289
0d64f834 290static int init_stat_file(struct stat_session *session)
002bb86d 291{
afccc00f
LH
292 int ret;
293
294 if (!stat_dir && (ret = tracing_stat_init()))
295 return ret;
002bb86d 296
21ccc9cd
SRV
297 session->file = tracefs_create_file(session->ts->name, TRACE_MODE_WRITE,
298 stat_dir, session,
299 &tracing_stat_fops);
002bb86d
FW
300 if (!session->file)
301 return -ENOMEM;
302 return 0;
303}
55922173
IM
304
305int register_stat_tracer(struct tracer_stat *trace)
306{
43bd1236 307 struct stat_session *session, *node;
08b76731 308 int ret;
55922173
IM
309
310 if (!trace)
311 return -EINVAL;
312
313 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
314 return -EINVAL;
315
08b76731
SR
316 guard(mutex)(&all_stat_sessions_mutex);
317
55922173 318 /* Already registered? */
43bd1236 319 list_for_each_entry(node, &all_stat_sessions, session_list) {
dfb6cd1e 320 if (node->ts == trace)
08b76731 321 return -EINVAL;
55922173 322 }
55922173
IM
323
324 /* Init the session */
8f184f27 325 session = kzalloc(sizeof(*session), GFP_KERNEL);
55922173 326 if (!session)
08b76731 327 return -ENOMEM;
55922173
IM
328
329 session->ts = trace;
330 INIT_LIST_HEAD(&session->session_list);
55922173 331 mutex_init(&session->stat_mutex);
55922173
IM
332
333 ret = init_stat_file(session);
334 if (ret) {
335 destroy_session(session);
08b76731 336 return ret;
55922173
IM
337 }
338
339 /* Register */
55922173 340 list_add_tail(&session->session_list, &all_stat_sessions);
55922173 341
08b76731 342 return 0;
55922173
IM
343}
344
345void unregister_stat_tracer(struct tracer_stat *trace)
346{
0d64f834 347 struct stat_session *node, *tmp;
55922173
IM
348
349 mutex_lock(&all_stat_sessions_mutex);
350 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
351 if (node->ts == trace) {
352 list_del(&node->session_list);
353 destroy_session(node);
354 break;
355 }
356 }
357 mutex_unlock(&all_stat_sessions_mutex);
358}