Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / kernel / trace / trace_branch.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1f0d69a9
SR
2/*
3 * unlikely profiler
4 *
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 */
7#include <linux/kallsyms.h>
8#include <linux/seq_file.h>
9#include <linux/spinlock.h>
65c6dc6a 10#include <linux/irqflags.h>
1f0d69a9
SR
11#include <linux/uaccess.h>
12#include <linux/module.h>
13#include <linux/ftrace.h>
14#include <linux/hash.h>
15#include <linux/fs.h>
16#include <asm/local.h>
f633cef0 17
1f0d69a9 18#include "trace.h"
002bb86d 19#include "trace_stat.h"
f633cef0 20#include "trace_output.h"
1f0d69a9 21
2ed84eeb 22#ifdef CONFIG_BRANCH_TRACER
52f232cb 23
002bb86d 24static struct tracer branch_trace;
9f029e83
SR
25static int branch_tracing_enabled __read_mostly;
26static DEFINE_MUTEX(branch_tracing_mutex);
e302cf3f 27
9f029e83 28static struct trace_array *branch_tracer;
52f232cb
SR
29
30static void
068f530b 31probe_likely_condition(struct ftrace_likely_data *f, int val, int expect)
52f232cb 32{
9f029e83 33 struct trace_array *tr = branch_tracer;
13292494 34 struct trace_buffer *buffer;
52f232cb 35 struct ring_buffer_event *event;
9f029e83 36 struct trace_branch *entry;
0a987751 37 unsigned long flags;
36590c50 38 unsigned int trace_ctx;
52f232cb
SR
39 const char *p;
40
6224beb1
SRRH
41 if (current->trace_recursion & TRACE_BRANCH_BIT)
42 return;
43
52f232cb
SR
44 /*
45 * I would love to save just the ftrace_likely_data pointer, but
46 * this code can also be used by modules. Ugly things can happen
47 * if the module is unloaded, and then we go and read the
48 * pointer. This is slower, but much safer.
49 */
50
51 if (unlikely(!tr))
52 return;
53
6224beb1
SRRH
54 raw_local_irq_save(flags);
55 current->trace_recursion |= TRACE_BRANCH_BIT;
cf64792f 56 if (!tracer_tracing_is_on_cpu(tr, raw_smp_processor_id()))
52f232cb
SR
57 goto out;
58
36590c50 59 trace_ctx = tracing_gen_ctx_flags(flags);
1c5eb448 60 buffer = tr->array_buffer.buffer;
8f6e8a31 61 event = trace_buffer_lock_reserve(buffer, TRACE_BRANCH,
36590c50 62 sizeof(*entry), trace_ctx);
52f232cb
SR
63 if (!event)
64 goto out;
65
52f232cb 66 entry = ring_buffer_event_data(event);
52f232cb
SR
67
68 /* Strip off the path, only save the file */
068f530b
SRV
69 p = f->data.file + strlen(f->data.file);
70 while (p >= f->data.file && *p != '/')
52f232cb
SR
71 p--;
72 p++;
73
2aa746ec
JS
74 strscpy(entry->func, f->data.func);
75 strscpy(entry->file, p);
068f530b
SRV
76 entry->constant = f->constant;
77 entry->line = f->data.line;
52f232cb
SR
78 entry->correct = val == expect;
79
49e4154f 80 trace_buffer_unlock_commit_nostack(buffer, event);
52f232cb
SR
81
82 out:
6224beb1
SRRH
83 current->trace_recursion &= ~TRACE_BRANCH_BIT;
84 raw_local_irq_restore(flags);
52f232cb
SR
85}
86
87static inline
068f530b 88void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
52f232cb 89{
9f029e83 90 if (!branch_tracing_enabled)
52f232cb
SR
91 return;
92
93 probe_likely_condition(f, val, expect);
94}
95
9f029e83 96int enable_branch_tracing(struct trace_array *tr)
52f232cb 97{
9f029e83
SR
98 mutex_lock(&branch_tracing_mutex);
99 branch_tracer = tr;
52f232cb
SR
100 /*
101 * Must be seen before enabling. The reader is a condition
102 * where we do not need a matching rmb()
103 */
104 smp_wmb();
9f029e83
SR
105 branch_tracing_enabled++;
106 mutex_unlock(&branch_tracing_mutex);
52f232cb 107
f54fc98a 108 return 0;
52f232cb
SR
109}
110
9f029e83 111void disable_branch_tracing(void)
52f232cb 112{
9f029e83 113 mutex_lock(&branch_tracing_mutex);
52f232cb 114
9f029e83 115 if (!branch_tracing_enabled)
52f232cb
SR
116 goto out_unlock;
117
9f029e83 118 branch_tracing_enabled--;
52f232cb
SR
119
120 out_unlock:
9f029e83 121 mutex_unlock(&branch_tracing_mutex);
52f232cb 122}
80e5ea45 123
1c80025a 124static int branch_trace_init(struct trace_array *tr)
80e5ea45 125{
30616929 126 return enable_branch_tracing(tr);
80e5ea45
SR
127}
128
129static void branch_trace_reset(struct trace_array *tr)
130{
30616929 131 disable_branch_tracing();
80e5ea45
SR
132}
133
ae7462b4 134static enum print_line_t trace_branch_print(struct trace_iterator *iter,
a9a57763 135 int flags, struct trace_event *event)
f633cef0
SR
136{
137 struct trace_branch *field;
138
2c9b238e 139 trace_assign_type(field, iter->ent);
f633cef0 140
7d40f671
SRRH
141 trace_seq_printf(&iter->seq, "[%s] %s:%s:%d\n",
142 field->correct ? " ok " : " MISS ",
143 field->func,
144 field->file,
145 field->line);
146
147 return trace_handle_return(&iter->seq);
f633cef0
SR
148}
149
557055be
Z
150static void branch_print_header(struct seq_file *s)
151{
152 seq_puts(s, "# TASK-PID CPU# TIMESTAMP CORRECT"
d79ac28f
RV
153 " FUNC:FILE:LINE\n"
154 "# | | | | | "
155 " |\n");
557055be 156}
e302cf3f 157
a9a57763
SR
158static struct trace_event_functions trace_branch_funcs = {
159 .trace = trace_branch_print,
160};
161
f633cef0 162static struct trace_event trace_branch_event = {
ef18012b 163 .type = TRACE_BRANCH,
a9a57763 164 .funcs = &trace_branch_funcs,
f633cef0
SR
165};
166
002bb86d
FW
167static struct tracer branch_trace __read_mostly =
168{
169 .name = "branch",
170 .init = branch_trace_init,
171 .reset = branch_trace_reset,
172#ifdef CONFIG_FTRACE_SELFTEST
173 .selftest = trace_selftest_startup_branch,
174#endif /* CONFIG_FTRACE_SELFTEST */
557055be 175 .print_header = branch_print_header,
002bb86d
FW
176};
177
178__init static int init_branch_tracer(void)
179{
180 int ret;
181
9023c930 182 ret = register_trace_event(&trace_branch_event);
002bb86d
FW
183 if (!ret) {
184 printk(KERN_WARNING "Warning: could not register "
185 "branch events\n");
186 return 1;
187 }
188 return register_tracer(&branch_trace);
189}
6f415672 190core_initcall(init_branch_tracer);
002bb86d 191
52f232cb
SR
192#else
193static inline
068f530b 194void trace_likely_condition(struct ftrace_likely_data *f, int val, int expect)
52f232cb
SR
195{
196}
2ed84eeb 197#endif /* CONFIG_BRANCH_TRACER */
52f232cb 198
134e6a03 199void ftrace_likely_update(struct ftrace_likely_data *f, int val,
d45ae1f7 200 int expect, int is_constant)
1f0d69a9 201{
4a6c91fb
PZ
202 unsigned long flags = user_access_save();
203
d45ae1f7 204 /* A constant is always correct */
134e6a03
SRV
205 if (is_constant) {
206 f->constant++;
d45ae1f7 207 val = expect;
134e6a03 208 }
52f232cb
SR
209 /*
210 * I would love to have a trace point here instead, but the
211 * trace point code is so inundated with unlikely and likely
212 * conditions that the recursive nightmare that exists is too
213 * much to try to get working. At least for now.
214 */
068f530b 215 trace_likely_condition(f, val, expect);
52f232cb 216
1f0d69a9
SR
217 /* FIXME: Make this atomic! */
218 if (val == expect)
134e6a03 219 f->data.correct++;
1f0d69a9 220 else
134e6a03 221 f->data.incorrect++;
4a6c91fb
PZ
222
223 user_access_restore(flags);
1f0d69a9
SR
224}
225EXPORT_SYMBOL(ftrace_likely_update);
226
e302cf3f
FW
227extern unsigned long __start_annotated_branch_profile[];
228extern unsigned long __stop_annotated_branch_profile[];
1f0d69a9 229
e302cf3f 230static int annotated_branch_stat_headers(struct seq_file *m)
1f0d69a9 231{
d79ac28f
RV
232 seq_puts(m, " correct incorrect % "
233 " Function "
fa6f0cc7
RV
234 " File Line\n"
235 " ------- --------- - "
236 " -------- "
237 " ---- ----\n");
e302cf3f 238 return 0;
1f0d69a9
SR
239}
240
80042c8f 241static inline long get_incorrect_percent(const struct ftrace_branch_data *p)
1f0d69a9 242{
e302cf3f 243 long percent;
1f0d69a9 244
e302cf3f
FW
245 if (p->correct) {
246 percent = p->incorrect * 100;
247 percent /= p->correct + p->incorrect;
248 } else
249 percent = p->incorrect ? 100 : -1;
1f0d69a9 250
e302cf3f 251 return percent;
1f0d69a9
SR
252}
253
134e6a03 254static const char *branch_stat_process_file(struct ftrace_branch_data *p)
1f0d69a9 255{
1f0d69a9 256 const char *f;
1f0d69a9 257
1f0d69a9
SR
258 /* Only print the file, not the path */
259 f = p->file + strlen(p->file);
260 while (f >= p->file && *f != '/')
261 f--;
134e6a03
SRV
262 return ++f;
263}
264
265static void branch_stat_show(struct seq_file *m,
266 struct ftrace_branch_data *p, const char *f)
267{
268 long percent;
1f0d69a9 269
2bcd521a
SR
270 /*
271 * The miss is overlayed on correct, and hit on incorrect.
272 */
e302cf3f 273 percent = get_incorrect_percent(p);
1f0d69a9 274
bac28bfe 275 if (percent < 0)
fa6f0cc7 276 seq_puts(m, " X ");
bac28bfe
SR
277 else
278 seq_printf(m, "%3ld ", percent);
134e6a03 279
1f0d69a9 280 seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
134e6a03
SRV
281}
282
283static int branch_stat_show_normal(struct seq_file *m,
284 struct ftrace_branch_data *p, const char *f)
285{
286 seq_printf(m, "%8lu %8lu ", p->correct, p->incorrect);
287 branch_stat_show(m, p, f);
288 return 0;
289}
290
291static int annotate_branch_stat_show(struct seq_file *m, void *v)
292{
293 struct ftrace_likely_data *p = v;
294 const char *f;
295 int l;
296
297 f = branch_stat_process_file(&p->data);
298
299 if (!p->constant)
300 return branch_stat_show_normal(m, &p->data, f);
301
302 l = snprintf(NULL, 0, "/%lu", p->constant);
303 l = l > 8 ? 0 : 8 - l;
304
305 seq_printf(m, "%8lu/%lu %*lu ",
306 p->data.correct, p->constant, l, p->data.incorrect);
307 branch_stat_show(m, &p->data, f);
1f0d69a9
SR
308 return 0;
309}
310
42548008 311static void *annotated_branch_stat_start(struct tracer_stat *trace)
e302cf3f
FW
312{
313 return __start_annotated_branch_profile;
314}
1f0d69a9 315
e302cf3f
FW
316static void *
317annotated_branch_stat_next(void *v, int idx)
1f0d69a9 318{
134e6a03 319 struct ftrace_likely_data *p = v;
1f0d69a9 320
e302cf3f 321 ++p;
1f0d69a9 322
e302cf3f
FW
323 if ((void *)p >= (void *)__stop_annotated_branch_profile)
324 return NULL;
325
326 return p;
1f0d69a9
SR
327}
328
80042c8f 329static int annotated_branch_stat_cmp(const void *p1, const void *p2)
e302cf3f 330{
80042c8f
AS
331 const struct ftrace_branch_data *a = p1;
332 const struct ftrace_branch_data *b = p2;
e302cf3f
FW
333
334 long percent_a, percent_b;
335
336 percent_a = get_incorrect_percent(a);
337 percent_b = get_incorrect_percent(b);
338
339 if (percent_a < percent_b)
340 return -1;
341 if (percent_a > percent_b)
342 return 1;
ede55c9d
SR
343
344 if (a->incorrect < b->incorrect)
345 return -1;
346 if (a->incorrect > b->incorrect)
347 return 1;
348
349 /*
350 * Since the above shows worse (incorrect) cases
351 * first, we continue that by showing best (correct)
352 * cases last.
353 */
354 if (a->correct > b->correct)
355 return -1;
356 if (a->correct < b->correct)
357 return 1;
358
359 return 0;
e302cf3f 360}
1f0d69a9 361
002bb86d
FW
362static struct tracer_stat annotated_branch_stats = {
363 .name = "branch_annotated",
364 .stat_start = annotated_branch_stat_start,
365 .stat_next = annotated_branch_stat_next,
366 .stat_cmp = annotated_branch_stat_cmp,
367 .stat_headers = annotated_branch_stat_headers,
134e6a03 368 .stat_show = annotate_branch_stat_show
002bb86d
FW
369};
370
371__init static int init_annotated_branch_stats(void)
372{
373 int ret;
374
375 ret = register_stat_tracer(&annotated_branch_stats);
376 if (!ret) {
377 printk(KERN_WARNING "Warning: could not register "
378 "annotated branches stats\n");
379 return 1;
380 }
381 return 0;
382}
383fs_initcall(init_annotated_branch_stats);
384
2bcd521a 385#ifdef CONFIG_PROFILE_ALL_BRANCHES
2bcd521a 386
e302cf3f
FW
387extern unsigned long __start_branch_profile[];
388extern unsigned long __stop_branch_profile[];
1f0d69a9 389
e302cf3f
FW
390static int all_branch_stat_headers(struct seq_file *m)
391{
d79ac28f
RV
392 seq_puts(m, " miss hit % "
393 " Function "
fa6f0cc7
RV
394 " File Line\n"
395 " ------- --------- - "
396 " -------- "
397 " ---- ----\n");
e302cf3f
FW
398 return 0;
399}
1f0d69a9 400
42548008 401static void *all_branch_stat_start(struct tracer_stat *trace)
1f0d69a9 402{
e302cf3f
FW
403 return __start_branch_profile;
404}
405
406static void *
407all_branch_stat_next(void *v, int idx)
408{
409 struct ftrace_branch_data *p = v;
1f0d69a9 410
e302cf3f 411 ++p;
1f0d69a9 412
e302cf3f
FW
413 if ((void *)p >= (void *)__stop_branch_profile)
414 return NULL;
1f0d69a9 415
e302cf3f
FW
416 return p;
417}
2bcd521a 418
134e6a03
SRV
419static int all_branch_stat_show(struct seq_file *m, void *v)
420{
421 struct ftrace_branch_data *p = v;
422 const char *f;
423
424 f = branch_stat_process_file(p);
425 return branch_stat_show_normal(m, p, f);
426}
427
002bb86d
FW
428static struct tracer_stat all_branch_stats = {
429 .name = "branch_all",
034939b6
FW
430 .stat_start = all_branch_stat_start,
431 .stat_next = all_branch_stat_next,
432 .stat_headers = all_branch_stat_headers,
134e6a03 433 .stat_show = all_branch_stat_show
034939b6 434};
e302cf3f 435
002bb86d 436__init static int all_annotated_branch_stats(void)
e302cf3f 437{
e302cf3f 438 int ret;
002bb86d
FW
439
440 ret = register_stat_tracer(&all_branch_stats);
e302cf3f 441 if (!ret) {
002bb86d
FW
442 printk(KERN_WARNING "Warning: could not register "
443 "all branches stats\n");
e302cf3f
FW
444 return 1;
445 }
002bb86d 446 return 0;
e302cf3f 447}
002bb86d
FW
448fs_initcall(all_annotated_branch_stats);
449#endif /* CONFIG_PROFILE_ALL_BRANCHES */