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