blob: 146771d6d00722d82b9779cffaa45f14a4d5ab1a [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * ring buffer based function tracer
4 *
5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:
9 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code from the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
14 */
15#include <linux/ring_buffer.h>
16#include <generated/utsrelease.h>
17#include <linux/stacktrace.h>
18#include <linux/writeback.h>
19#include <linux/kallsyms.h>
David Brazdil0f672f62019-12-10 10:32:29 +000020#include <linux/security.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021#include <linux/seq_file.h>
22#include <linux/notifier.h>
23#include <linux/irqflags.h>
24#include <linux/debugfs.h>
25#include <linux/tracefs.h>
26#include <linux/pagemap.h>
27#include <linux/hardirq.h>
28#include <linux/linkage.h>
29#include <linux/uaccess.h>
30#include <linux/vmalloc.h>
31#include <linux/ftrace.h>
32#include <linux/module.h>
33#include <linux/percpu.h>
34#include <linux/splice.h>
35#include <linux/kdebug.h>
36#include <linux/string.h>
37#include <linux/mount.h>
38#include <linux/rwsem.h>
39#include <linux/slab.h>
40#include <linux/ctype.h>
41#include <linux/init.h>
42#include <linux/poll.h>
43#include <linux/nmi.h>
44#include <linux/fs.h>
45#include <linux/trace.h>
46#include <linux/sched/clock.h>
47#include <linux/sched/rt.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020048#include <linux/fsnotify.h>
49#include <linux/irq_work.h>
50#include <linux/workqueue.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051
52#include "trace.h"
53#include "trace_output.h"
54
55/*
56 * On boot up, the ring buffer is set to the minimum size, so that
57 * we do not waste memory on systems that are not using tracing.
58 */
59bool ring_buffer_expanded;
60
61/*
62 * We need to change this state when a selftest is running.
63 * A selftest will lurk into the ring-buffer to count the
64 * entries inserted during the selftest although some concurrent
65 * insertions into the ring-buffer such as trace_printk could occurred
66 * at the same time, giving false positive or negative results.
67 */
68static bool __read_mostly tracing_selftest_running;
69
70/*
Olivier Deprez157378f2022-04-04 15:47:50 +020071 * If boot-time tracing including tracers/events via kernel cmdline
72 * is running, we do not want to run SELFTEST.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 */
74bool __read_mostly tracing_selftest_disabled;
75
Olivier Deprez157378f2022-04-04 15:47:50 +020076#ifdef CONFIG_FTRACE_STARTUP_TEST
77void __init disable_tracing_selftest(const char *reason)
78{
79 if (!tracing_selftest_disabled) {
80 tracing_selftest_disabled = true;
81 pr_info("Ftrace startup test is disabled due to %s\n", reason);
82 }
83}
84#endif
85
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086/* Pipe tracepoints to printk */
87struct trace_iterator *tracepoint_print_iter;
88int tracepoint_printk;
89static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
90
91/* For tracers that don't implement custom flags */
92static struct tracer_opt dummy_tracer_opt[] = {
93 { }
94};
95
96static int
97dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
98{
99 return 0;
100}
101
102/*
103 * To prevent the comm cache from being overwritten when no
104 * tracing is active, only save the comm when a trace event
105 * occurred.
106 */
107static DEFINE_PER_CPU(bool, trace_taskinfo_save);
108
109/*
110 * Kill all tracing for good (never come back).
111 * It is initialized to 1 but will turn to zero if the initialization
112 * of the tracer is successful. But that is the only place that sets
113 * this back to zero.
114 */
115static int tracing_disabled = 1;
116
117cpumask_var_t __read_mostly tracing_buffer_mask;
118
119/*
120 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
121 *
122 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
123 * is set, then ftrace_dump is called. This will output the contents
124 * of the ftrace buffers to the console. This is very useful for
125 * capturing traces that lead to crashes and outputing it to a
126 * serial console.
127 *
128 * It is default off, but you can enable it with either specifying
129 * "ftrace_dump_on_oops" in the kernel command line, or setting
130 * /proc/sys/kernel/ftrace_dump_on_oops
131 * Set 1 if you want to dump buffers of all CPUs
132 * Set 2 if you want to dump the buffer of the CPU that triggered oops
133 */
134
135enum ftrace_dump_mode ftrace_dump_on_oops;
136
137/* When set, tracing will stop when a WARN*() is hit */
138int __disable_trace_on_warning;
139
140#ifdef CONFIG_TRACE_EVAL_MAP_FILE
141/* Map of enums to their values, for "eval_map" file */
142struct trace_eval_map_head {
143 struct module *mod;
144 unsigned long length;
145};
146
147union trace_eval_map_item;
148
149struct trace_eval_map_tail {
150 /*
151 * "end" is first and points to NULL as it must be different
152 * than "mod" or "eval_string"
153 */
154 union trace_eval_map_item *next;
155 const char *end; /* points to NULL */
156};
157
158static DEFINE_MUTEX(trace_eval_mutex);
159
160/*
161 * The trace_eval_maps are saved in an array with two extra elements,
162 * one at the beginning, and one at the end. The beginning item contains
163 * the count of the saved maps (head.length), and the module they
164 * belong to if not built in (head.mod). The ending item contains a
165 * pointer to the next array of saved eval_map items.
166 */
167union trace_eval_map_item {
168 struct trace_eval_map map;
169 struct trace_eval_map_head head;
170 struct trace_eval_map_tail tail;
171};
172
173static union trace_eval_map_item *trace_eval_maps;
174#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
175
Olivier Deprez157378f2022-04-04 15:47:50 +0200176int tracing_set_tracer(struct trace_array *tr, const char *buf);
Olivier Deprez0e641232021-09-23 10:07:05 +0200177static void ftrace_trace_userstack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +0200178 struct trace_buffer *buffer,
David Brazdil0f672f62019-12-10 10:32:29 +0000179 unsigned long flags, int pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180
181#define MAX_TRACER_SIZE 100
182static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
183static char *default_bootup_tracer;
184
185static bool allocate_snapshot;
186
187static int __init set_cmdline_ftrace(char *str)
188{
189 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
190 default_bootup_tracer = bootup_tracer_buf;
191 /* We are using ftrace early, expand it */
192 ring_buffer_expanded = true;
193 return 1;
194}
195__setup("ftrace=", set_cmdline_ftrace);
196
197static int __init set_ftrace_dump_on_oops(char *str)
198{
199 if (*str++ != '=' || !*str) {
200 ftrace_dump_on_oops = DUMP_ALL;
201 return 1;
202 }
203
204 if (!strcmp("orig_cpu", str)) {
205 ftrace_dump_on_oops = DUMP_ORIG;
206 return 1;
207 }
208
209 return 0;
210}
211__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
212
213static int __init stop_trace_on_warning(char *str)
214{
215 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
216 __disable_trace_on_warning = 1;
217 return 1;
218}
219__setup("traceoff_on_warning", stop_trace_on_warning);
220
221static int __init boot_alloc_snapshot(char *str)
222{
223 allocate_snapshot = true;
224 /* We also need the main ring buffer expanded */
225 ring_buffer_expanded = true;
226 return 1;
227}
228__setup("alloc_snapshot", boot_alloc_snapshot);
229
230
231static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
232
233static int __init set_trace_boot_options(char *str)
234{
235 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Olivier Deprez157378f2022-04-04 15:47:50 +0200236 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237}
238__setup("trace_options=", set_trace_boot_options);
239
240static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
241static char *trace_boot_clock __initdata;
242
243static int __init set_trace_boot_clock(char *str)
244{
245 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
246 trace_boot_clock = trace_boot_clock_buf;
Olivier Deprez157378f2022-04-04 15:47:50 +0200247 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248}
249__setup("trace_clock=", set_trace_boot_clock);
250
251static int __init set_tracepoint_printk(char *str)
252{
Olivier Deprez157378f2022-04-04 15:47:50 +0200253 /* Ignore the "tp_printk_stop_on_boot" param */
254 if (*str == '_')
255 return 0;
256
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
258 tracepoint_printk = 1;
259 return 1;
260}
261__setup("tp_printk", set_tracepoint_printk);
262
263unsigned long long ns2usecs(u64 nsec)
264{
265 nsec += 500;
266 do_div(nsec, 1000);
267 return nsec;
268}
269
Olivier Deprez157378f2022-04-04 15:47:50 +0200270static void
271trace_process_export(struct trace_export *export,
272 struct ring_buffer_event *event, int flag)
273{
274 struct trace_entry *entry;
275 unsigned int size = 0;
276
277 if (export->flags & flag) {
278 entry = ring_buffer_event_data(event);
279 size = ring_buffer_event_length(event);
280 export->write(export, entry, size);
281 }
282}
283
284static DEFINE_MUTEX(ftrace_export_lock);
285
286static struct trace_export __rcu *ftrace_exports_list __read_mostly;
287
288static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
289static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
290static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
291
292static inline void ftrace_exports_enable(struct trace_export *export)
293{
294 if (export->flags & TRACE_EXPORT_FUNCTION)
295 static_branch_inc(&trace_function_exports_enabled);
296
297 if (export->flags & TRACE_EXPORT_EVENT)
298 static_branch_inc(&trace_event_exports_enabled);
299
300 if (export->flags & TRACE_EXPORT_MARKER)
301 static_branch_inc(&trace_marker_exports_enabled);
302}
303
304static inline void ftrace_exports_disable(struct trace_export *export)
305{
306 if (export->flags & TRACE_EXPORT_FUNCTION)
307 static_branch_dec(&trace_function_exports_enabled);
308
309 if (export->flags & TRACE_EXPORT_EVENT)
310 static_branch_dec(&trace_event_exports_enabled);
311
312 if (export->flags & TRACE_EXPORT_MARKER)
313 static_branch_dec(&trace_marker_exports_enabled);
314}
315
316static void ftrace_exports(struct ring_buffer_event *event, int flag)
317{
318 struct trace_export *export;
319
320 preempt_disable_notrace();
321
322 export = rcu_dereference_raw_check(ftrace_exports_list);
323 while (export) {
324 trace_process_export(export, event, flag);
325 export = rcu_dereference_raw_check(export->next);
326 }
327
328 preempt_enable_notrace();
329}
330
331static inline void
332add_trace_export(struct trace_export **list, struct trace_export *export)
333{
334 rcu_assign_pointer(export->next, *list);
335 /*
336 * We are entering export into the list but another
337 * CPU might be walking that list. We need to make sure
338 * the export->next pointer is valid before another CPU sees
339 * the export pointer included into the list.
340 */
341 rcu_assign_pointer(*list, export);
342}
343
344static inline int
345rm_trace_export(struct trace_export **list, struct trace_export *export)
346{
347 struct trace_export **p;
348
349 for (p = list; *p != NULL; p = &(*p)->next)
350 if (*p == export)
351 break;
352
353 if (*p != export)
354 return -1;
355
356 rcu_assign_pointer(*p, (*p)->next);
357
358 return 0;
359}
360
361static inline void
362add_ftrace_export(struct trace_export **list, struct trace_export *export)
363{
364 ftrace_exports_enable(export);
365
366 add_trace_export(list, export);
367}
368
369static inline int
370rm_ftrace_export(struct trace_export **list, struct trace_export *export)
371{
372 int ret;
373
374 ret = rm_trace_export(list, export);
375 ftrace_exports_disable(export);
376
377 return ret;
378}
379
380int register_ftrace_export(struct trace_export *export)
381{
382 if (WARN_ON_ONCE(!export->write))
383 return -1;
384
385 mutex_lock(&ftrace_export_lock);
386
387 add_ftrace_export(&ftrace_exports_list, export);
388
389 mutex_unlock(&ftrace_export_lock);
390
391 return 0;
392}
393EXPORT_SYMBOL_GPL(register_ftrace_export);
394
395int unregister_ftrace_export(struct trace_export *export)
396{
397 int ret;
398
399 mutex_lock(&ftrace_export_lock);
400
401 ret = rm_ftrace_export(&ftrace_exports_list, export);
402
403 mutex_unlock(&ftrace_export_lock);
404
405 return ret;
406}
407EXPORT_SYMBOL_GPL(unregister_ftrace_export);
408
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409/* trace_flags holds trace_options default values */
410#define TRACE_DEFAULT_FLAGS \
411 (FUNCTION_DEFAULT_FLAGS | \
412 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
413 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
414 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
415 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
416
417/* trace_options that are only supported by global_trace */
418#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
419 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
420
421/* trace_flags that are default zero for instances */
422#define ZEROED_TRACE_FLAGS \
423 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
424
425/*
426 * The global_trace is the descriptor that holds the top-level tracing
427 * buffers for the live tracing.
428 */
429static struct trace_array global_trace = {
430 .trace_flags = TRACE_DEFAULT_FLAGS,
431};
432
433LIST_HEAD(ftrace_trace_arrays);
434
435int trace_array_get(struct trace_array *this_tr)
436{
437 struct trace_array *tr;
438 int ret = -ENODEV;
439
440 mutex_lock(&trace_types_lock);
441 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
442 if (tr == this_tr) {
443 tr->ref++;
444 ret = 0;
445 break;
446 }
447 }
448 mutex_unlock(&trace_types_lock);
449
450 return ret;
451}
452
453static void __trace_array_put(struct trace_array *this_tr)
454{
455 WARN_ON(!this_tr->ref);
456 this_tr->ref--;
457}
458
Olivier Deprez157378f2022-04-04 15:47:50 +0200459/**
460 * trace_array_put - Decrement the reference counter for this trace array.
461 *
462 * NOTE: Use this when we no longer need the trace array returned by
463 * trace_array_get_by_name(). This ensures the trace array can be later
464 * destroyed.
465 *
466 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467void trace_array_put(struct trace_array *this_tr)
468{
Olivier Deprez157378f2022-04-04 15:47:50 +0200469 if (!this_tr)
470 return;
471
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472 mutex_lock(&trace_types_lock);
473 __trace_array_put(this_tr);
474 mutex_unlock(&trace_types_lock);
475}
Olivier Deprez157378f2022-04-04 15:47:50 +0200476EXPORT_SYMBOL_GPL(trace_array_put);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477
David Brazdil0f672f62019-12-10 10:32:29 +0000478int tracing_check_open_get_tr(struct trace_array *tr)
479{
480 int ret;
481
482 ret = security_locked_down(LOCKDOWN_TRACEFS);
483 if (ret)
484 return ret;
485
486 if (tracing_disabled)
487 return -ENODEV;
488
489 if (tr && trace_array_get(tr) < 0)
490 return -ENODEV;
491
492 return 0;
493}
494
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495int call_filter_check_discard(struct trace_event_call *call, void *rec,
Olivier Deprez157378f2022-04-04 15:47:50 +0200496 struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 struct ring_buffer_event *event)
498{
499 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
500 !filter_match_preds(call->filter, rec)) {
501 __trace_event_discard_commit(buffer, event);
502 return 1;
503 }
504
505 return 0;
506}
507
508void trace_free_pid_list(struct trace_pid_list *pid_list)
509{
510 vfree(pid_list->pids);
511 kfree(pid_list);
512}
513
514/**
515 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
516 * @filtered_pids: The list of pids to check
517 * @search_pid: The PID to find in @filtered_pids
518 *
519 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
520 */
521bool
522trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
523{
524 /*
525 * If pid_max changed after filtered_pids was created, we
526 * by default ignore all pids greater than the previous pid_max.
527 */
528 if (search_pid >= filtered_pids->pid_max)
529 return false;
530
531 return test_bit(search_pid, filtered_pids->pids);
532}
533
534/**
535 * trace_ignore_this_task - should a task be ignored for tracing
536 * @filtered_pids: The list of pids to check
537 * @task: The task that should be ignored if not filtered
538 *
539 * Checks if @task should be traced or not from @filtered_pids.
540 * Returns true if @task should *NOT* be traced.
541 * Returns false if @task should be traced.
542 */
543bool
Olivier Deprez157378f2022-04-04 15:47:50 +0200544trace_ignore_this_task(struct trace_pid_list *filtered_pids,
545 struct trace_pid_list *filtered_no_pids,
546 struct task_struct *task)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547{
548 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200549 * If filterd_no_pids is not empty, and the task's pid is listed
550 * in filtered_no_pids, then return true.
551 * Otherwise, if filtered_pids is empty, that means we can
552 * trace all tasks. If it has content, then only trace pids
553 * within filtered_pids.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000554 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000555
Olivier Deprez157378f2022-04-04 15:47:50 +0200556 return (filtered_pids &&
557 !trace_find_filtered_pid(filtered_pids, task->pid)) ||
558 (filtered_no_pids &&
559 trace_find_filtered_pid(filtered_no_pids, task->pid));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560}
561
562/**
David Brazdil0f672f62019-12-10 10:32:29 +0000563 * trace_filter_add_remove_task - Add or remove a task from a pid_list
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000564 * @pid_list: The list to modify
565 * @self: The current task for fork or NULL for exit
566 * @task: The task to add or remove
567 *
568 * If adding a task, if @self is defined, the task is only added if @self
569 * is also included in @pid_list. This happens on fork and tasks should
570 * only be added when the parent is listed. If @self is NULL, then the
571 * @task pid will be removed from the list, which would happen on exit
572 * of a task.
573 */
574void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
575 struct task_struct *self,
576 struct task_struct *task)
577{
578 if (!pid_list)
579 return;
580
581 /* For forks, we only add if the forking task is listed */
582 if (self) {
583 if (!trace_find_filtered_pid(pid_list, self->pid))
584 return;
585 }
586
587 /* Sorry, but we don't support pid_max changing after setting */
588 if (task->pid >= pid_list->pid_max)
589 return;
590
591 /* "self" is set for forks, and NULL for exits */
592 if (self)
593 set_bit(task->pid, pid_list->pids);
594 else
595 clear_bit(task->pid, pid_list->pids);
596}
597
598/**
599 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
600 * @pid_list: The pid list to show
601 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
602 * @pos: The position of the file
603 *
604 * This is used by the seq_file "next" operation to iterate the pids
605 * listed in a trace_pid_list structure.
606 *
607 * Returns the pid+1 as we want to display pid of zero, but NULL would
608 * stop the iteration.
609 */
610void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
611{
612 unsigned long pid = (unsigned long)v;
613
614 (*pos)++;
615
616 /* pid already is +1 of the actual prevous bit */
617 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
618
619 /* Return pid + 1 to allow zero to be represented */
620 if (pid < pid_list->pid_max)
621 return (void *)(pid + 1);
622
623 return NULL;
624}
625
626/**
627 * trace_pid_start - Used for seq_file to start reading pid lists
628 * @pid_list: The pid list to show
629 * @pos: The position of the file
630 *
631 * This is used by seq_file "start" operation to start the iteration
632 * of listing pids.
633 *
634 * Returns the pid+1 as we want to display pid of zero, but NULL would
635 * stop the iteration.
636 */
637void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
638{
639 unsigned long pid;
640 loff_t l = 0;
641
642 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
643 if (pid >= pid_list->pid_max)
644 return NULL;
645
646 /* Return pid + 1 so that zero can be the exit value */
647 for (pid++; pid && l < *pos;
648 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
649 ;
650 return (void *)pid;
651}
652
653/**
654 * trace_pid_show - show the current pid in seq_file processing
655 * @m: The seq_file structure to write into
656 * @v: A void pointer of the pid (+1) value to display
657 *
658 * Can be directly used by seq_file operations to display the current
659 * pid value.
660 */
661int trace_pid_show(struct seq_file *m, void *v)
662{
663 unsigned long pid = (unsigned long)v - 1;
664
665 seq_printf(m, "%lu\n", pid);
666 return 0;
667}
668
669/* 128 should be much more than enough */
670#define PID_BUF_SIZE 127
671
672int trace_pid_write(struct trace_pid_list *filtered_pids,
673 struct trace_pid_list **new_pid_list,
674 const char __user *ubuf, size_t cnt)
675{
676 struct trace_pid_list *pid_list;
677 struct trace_parser parser;
678 unsigned long val;
679 int nr_pids = 0;
680 ssize_t read = 0;
681 ssize_t ret = 0;
682 loff_t pos;
683 pid_t pid;
684
685 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
686 return -ENOMEM;
687
688 /*
689 * Always recreate a new array. The write is an all or nothing
690 * operation. Always create a new array when adding new pids by
691 * the user. If the operation fails, then the current list is
692 * not modified.
693 */
694 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +0000695 if (!pid_list) {
696 trace_parser_put(&parser);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000697 return -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +0000698 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699
700 pid_list->pid_max = READ_ONCE(pid_max);
701
702 /* Only truncating will shrink pid_max */
703 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
704 pid_list->pid_max = filtered_pids->pid_max;
705
706 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
707 if (!pid_list->pids) {
David Brazdil0f672f62019-12-10 10:32:29 +0000708 trace_parser_put(&parser);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709 kfree(pid_list);
710 return -ENOMEM;
711 }
712
713 if (filtered_pids) {
714 /* copy the current bits to the new max */
715 for_each_set_bit(pid, filtered_pids->pids,
716 filtered_pids->pid_max) {
717 set_bit(pid, pid_list->pids);
718 nr_pids++;
719 }
720 }
721
722 while (cnt > 0) {
723
724 pos = 0;
725
726 ret = trace_get_user(&parser, ubuf, cnt, &pos);
727 if (ret < 0 || !trace_parser_loaded(&parser))
728 break;
729
730 read += ret;
731 ubuf += ret;
732 cnt -= ret;
733
734 ret = -EINVAL;
735 if (kstrtoul(parser.buffer, 0, &val))
736 break;
737 if (val >= pid_list->pid_max)
738 break;
739
740 pid = (pid_t)val;
741
742 set_bit(pid, pid_list->pids);
743 nr_pids++;
744
745 trace_parser_clear(&parser);
746 ret = 0;
747 }
748 trace_parser_put(&parser);
749
750 if (ret < 0) {
751 trace_free_pid_list(pid_list);
752 return ret;
753 }
754
755 if (!nr_pids) {
756 /* Cleared the list of pids */
757 trace_free_pid_list(pid_list);
758 read = ret;
759 pid_list = NULL;
760 }
761
762 *new_pid_list = pid_list;
763
764 return read;
765}
766
Olivier Deprez157378f2022-04-04 15:47:50 +0200767static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000768{
769 u64 ts;
770
771 /* Early boot up does not have a buffer yet */
772 if (!buf->buffer)
773 return trace_clock_local();
774
775 ts = ring_buffer_time_stamp(buf->buffer, cpu);
776 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
777
778 return ts;
779}
780
781u64 ftrace_now(int cpu)
782{
Olivier Deprez157378f2022-04-04 15:47:50 +0200783 return buffer_ftrace_now(&global_trace.array_buffer, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000784}
785
786/**
787 * tracing_is_enabled - Show if global_trace has been disabled
788 *
789 * Shows if the global trace has been enabled or not. It uses the
790 * mirror flag "buffer_disabled" to be used in fast paths such as for
791 * the irqsoff tracer. But it may be inaccurate due to races. If you
792 * need to know the accurate state, use tracing_is_on() which is a little
793 * slower, but accurate.
794 */
795int tracing_is_enabled(void)
796{
797 /*
798 * For quick access (irqsoff uses this in fast path), just
799 * return the mirror variable of the state of the ring buffer.
800 * It's a little racy, but we don't really care.
801 */
802 smp_rmb();
803 return !global_trace.buffer_disabled;
804}
805
806/*
807 * trace_buf_size is the size in bytes that is allocated
808 * for a buffer. Note, the number of bytes is always rounded
809 * to page size.
810 *
811 * This number is purposely set to a low number of 16384.
812 * If the dump on oops happens, it will be much appreciated
813 * to not have to wait for all that output. Anyway this can be
814 * boot time and run time configurable.
815 */
816#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
817
818static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
819
820/* trace_types holds a link list of available tracers. */
821static struct tracer *trace_types __read_mostly;
822
823/*
824 * trace_types_lock is used to protect the trace_types list.
825 */
826DEFINE_MUTEX(trace_types_lock);
827
828/*
829 * serialize the access of the ring buffer
830 *
831 * ring buffer serializes readers, but it is low level protection.
832 * The validity of the events (which returns by ring_buffer_peek() ..etc)
833 * are not protected by ring buffer.
834 *
835 * The content of events may become garbage if we allow other process consumes
836 * these events concurrently:
837 * A) the page of the consumed events may become a normal page
838 * (not reader page) in ring buffer, and this page will be rewrited
839 * by events producer.
840 * B) The page of the consumed events may become a page for splice_read,
841 * and this page will be returned to system.
842 *
843 * These primitives allow multi process access to different cpu ring buffer
844 * concurrently.
845 *
846 * These primitives don't distinguish read-only and read-consume access.
847 * Multi read-only access are also serialized.
848 */
849
850#ifdef CONFIG_SMP
851static DECLARE_RWSEM(all_cpu_access_lock);
852static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
853
854static inline void trace_access_lock(int cpu)
855{
856 if (cpu == RING_BUFFER_ALL_CPUS) {
857 /* gain it for accessing the whole ring buffer. */
858 down_write(&all_cpu_access_lock);
859 } else {
860 /* gain it for accessing a cpu ring buffer. */
861
862 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
863 down_read(&all_cpu_access_lock);
864
865 /* Secondly block other access to this @cpu ring buffer. */
866 mutex_lock(&per_cpu(cpu_access_lock, cpu));
867 }
868}
869
870static inline void trace_access_unlock(int cpu)
871{
872 if (cpu == RING_BUFFER_ALL_CPUS) {
873 up_write(&all_cpu_access_lock);
874 } else {
875 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
876 up_read(&all_cpu_access_lock);
877 }
878}
879
880static inline void trace_access_lock_init(void)
881{
882 int cpu;
883
884 for_each_possible_cpu(cpu)
885 mutex_init(&per_cpu(cpu_access_lock, cpu));
886}
887
888#else
889
890static DEFINE_MUTEX(access_lock);
891
892static inline void trace_access_lock(int cpu)
893{
894 (void)cpu;
895 mutex_lock(&access_lock);
896}
897
898static inline void trace_access_unlock(int cpu)
899{
900 (void)cpu;
901 mutex_unlock(&access_lock);
902}
903
904static inline void trace_access_lock_init(void)
905{
906}
907
908#endif
909
910#ifdef CONFIG_STACKTRACE
Olivier Deprez157378f2022-04-04 15:47:50 +0200911static void __ftrace_trace_stack(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912 unsigned long flags,
913 int skip, int pc, struct pt_regs *regs);
914static inline void ftrace_trace_stack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +0200915 struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916 unsigned long flags,
917 int skip, int pc, struct pt_regs *regs);
918
919#else
Olivier Deprez157378f2022-04-04 15:47:50 +0200920static inline void __ftrace_trace_stack(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921 unsigned long flags,
922 int skip, int pc, struct pt_regs *regs)
923{
924}
925static inline void ftrace_trace_stack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +0200926 struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 unsigned long flags,
928 int skip, int pc, struct pt_regs *regs)
929{
930}
931
932#endif
933
934static __always_inline void
935trace_event_setup(struct ring_buffer_event *event,
936 int type, unsigned long flags, int pc)
937{
938 struct trace_entry *ent = ring_buffer_event_data(event);
939
David Brazdil0f672f62019-12-10 10:32:29 +0000940 tracing_generic_entry_update(ent, type, flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941}
942
943static __always_inline struct ring_buffer_event *
Olivier Deprez157378f2022-04-04 15:47:50 +0200944__trace_buffer_lock_reserve(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945 int type,
946 unsigned long len,
947 unsigned long flags, int pc)
948{
949 struct ring_buffer_event *event;
950
951 event = ring_buffer_lock_reserve(buffer, len);
952 if (event != NULL)
953 trace_event_setup(event, type, flags, pc);
954
955 return event;
956}
957
958void tracer_tracing_on(struct trace_array *tr)
959{
Olivier Deprez157378f2022-04-04 15:47:50 +0200960 if (tr->array_buffer.buffer)
961 ring_buffer_record_on(tr->array_buffer.buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962 /*
963 * This flag is looked at when buffers haven't been allocated
964 * yet, or by some tracers (like irqsoff), that just want to
965 * know if the ring buffer has been disabled, but it can handle
966 * races of where it gets disabled but we still do a record.
967 * As the check is in the fast path of the tracers, it is more
968 * important to be fast than accurate.
969 */
970 tr->buffer_disabled = 0;
971 /* Make the flag seen by readers */
972 smp_wmb();
973}
974
975/**
976 * tracing_on - enable tracing buffers
977 *
978 * This function enables tracing buffers that may have been
979 * disabled with tracing_off.
980 */
981void tracing_on(void)
982{
983 tracer_tracing_on(&global_trace);
984}
985EXPORT_SYMBOL_GPL(tracing_on);
986
987
988static __always_inline void
Olivier Deprez157378f2022-04-04 15:47:50 +0200989__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000990{
991 __this_cpu_write(trace_taskinfo_save, true);
992
993 /* If this is the temp buffer, we need to commit fully */
994 if (this_cpu_read(trace_buffered_event) == event) {
995 /* Length is in event->array[0] */
996 ring_buffer_write(buffer, event->array[0], &event->array[1]);
997 /* Release the temp buffer */
998 this_cpu_dec(trace_buffered_event_cnt);
999 } else
1000 ring_buffer_unlock_commit(buffer, event);
1001}
1002
1003/**
1004 * __trace_puts - write a constant string into the trace buffer.
1005 * @ip: The address of the caller
1006 * @str: The constant string to write
1007 * @size: The size of the string.
1008 */
1009int __trace_puts(unsigned long ip, const char *str, int size)
1010{
1011 struct ring_buffer_event *event;
Olivier Deprez157378f2022-04-04 15:47:50 +02001012 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013 struct print_entry *entry;
1014 unsigned long irq_flags;
1015 int alloc;
1016 int pc;
1017
1018 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
1019 return 0;
1020
1021 pc = preempt_count();
1022
1023 if (unlikely(tracing_selftest_running || tracing_disabled))
1024 return 0;
1025
1026 alloc = sizeof(*entry) + size + 2; /* possible \n added */
1027
1028 local_save_flags(irq_flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001029 buffer = global_trace.array_buffer.buffer;
1030 ring_buffer_nest_start(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1032 irq_flags, pc);
Olivier Deprez157378f2022-04-04 15:47:50 +02001033 if (!event) {
1034 size = 0;
1035 goto out;
1036 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001037
1038 entry = ring_buffer_event_data(event);
1039 entry->ip = ip;
1040
1041 memcpy(&entry->buf, str, size);
1042
1043 /* Add a newline if necessary */
1044 if (entry->buf[size - 1] != '\n') {
1045 entry->buf[size] = '\n';
1046 entry->buf[size + 1] = '\0';
1047 } else
1048 entry->buf[size] = '\0';
1049
1050 __buffer_unlock_commit(buffer, event);
1051 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001052 out:
1053 ring_buffer_nest_end(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001054 return size;
1055}
1056EXPORT_SYMBOL_GPL(__trace_puts);
1057
1058/**
1059 * __trace_bputs - write the pointer to a constant string into trace buffer
1060 * @ip: The address of the caller
1061 * @str: The constant string to write to the buffer to
1062 */
1063int __trace_bputs(unsigned long ip, const char *str)
1064{
1065 struct ring_buffer_event *event;
Olivier Deprez157378f2022-04-04 15:47:50 +02001066 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067 struct bputs_entry *entry;
1068 unsigned long irq_flags;
1069 int size = sizeof(struct bputs_entry);
Olivier Deprez157378f2022-04-04 15:47:50 +02001070 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071 int pc;
1072
1073 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
1074 return 0;
1075
1076 pc = preempt_count();
1077
1078 if (unlikely(tracing_selftest_running || tracing_disabled))
1079 return 0;
1080
1081 local_save_flags(irq_flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001082 buffer = global_trace.array_buffer.buffer;
1083
1084 ring_buffer_nest_start(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001085 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
1086 irq_flags, pc);
1087 if (!event)
Olivier Deprez157378f2022-04-04 15:47:50 +02001088 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089
1090 entry = ring_buffer_event_data(event);
1091 entry->ip = ip;
1092 entry->str = str;
1093
1094 __buffer_unlock_commit(buffer, event);
1095 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
1096
Olivier Deprez157378f2022-04-04 15:47:50 +02001097 ret = 1;
1098 out:
1099 ring_buffer_nest_end(buffer);
1100 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101}
1102EXPORT_SYMBOL_GPL(__trace_bputs);
1103
1104#ifdef CONFIG_TRACER_SNAPSHOT
Olivier Deprez157378f2022-04-04 15:47:50 +02001105static void tracing_snapshot_instance_cond(struct trace_array *tr,
1106 void *cond_data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107{
1108 struct tracer *tracer = tr->current_trace;
1109 unsigned long flags;
1110
1111 if (in_nmi()) {
1112 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
1113 internal_trace_puts("*** snapshot is being ignored ***\n");
1114 return;
1115 }
1116
1117 if (!tr->allocated_snapshot) {
1118 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
1119 internal_trace_puts("*** stopping trace here! ***\n");
1120 tracing_off();
1121 return;
1122 }
1123
1124 /* Note, snapshot can not be used when the tracer uses it */
1125 if (tracer->use_max_tr) {
1126 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
1127 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
1128 return;
1129 }
1130
1131 local_irq_save(flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001132 update_max_tr(tr, current, smp_processor_id(), cond_data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001133 local_irq_restore(flags);
1134}
1135
David Brazdil0f672f62019-12-10 10:32:29 +00001136void tracing_snapshot_instance(struct trace_array *tr)
1137{
1138 tracing_snapshot_instance_cond(tr, NULL);
1139}
1140
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141/**
1142 * tracing_snapshot - take a snapshot of the current buffer.
1143 *
1144 * This causes a swap between the snapshot buffer and the current live
1145 * tracing buffer. You can use this to take snapshots of the live
1146 * trace when some condition is triggered, but continue to trace.
1147 *
1148 * Note, make sure to allocate the snapshot with either
1149 * a tracing_snapshot_alloc(), or by doing it manually
1150 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
1151 *
1152 * If the snapshot buffer is not allocated, it will stop tracing.
1153 * Basically making a permanent snapshot.
1154 */
1155void tracing_snapshot(void)
1156{
1157 struct trace_array *tr = &global_trace;
1158
1159 tracing_snapshot_instance(tr);
1160}
1161EXPORT_SYMBOL_GPL(tracing_snapshot);
1162
David Brazdil0f672f62019-12-10 10:32:29 +00001163/**
1164 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
1165 * @tr: The tracing instance to snapshot
1166 * @cond_data: The data to be tested conditionally, and possibly saved
1167 *
1168 * This is the same as tracing_snapshot() except that the snapshot is
1169 * conditional - the snapshot will only happen if the
1170 * cond_snapshot.update() implementation receiving the cond_data
1171 * returns true, which means that the trace array's cond_snapshot
1172 * update() operation used the cond_data to determine whether the
1173 * snapshot should be taken, and if it was, presumably saved it along
1174 * with the snapshot.
1175 */
1176void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1177{
1178 tracing_snapshot_instance_cond(tr, cond_data);
1179}
1180EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1181
1182/**
1183 * tracing_snapshot_cond_data - get the user data associated with a snapshot
1184 * @tr: The tracing instance
1185 *
1186 * When the user enables a conditional snapshot using
1187 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1188 * with the snapshot. This accessor is used to retrieve it.
1189 *
1190 * Should not be called from cond_snapshot.update(), since it takes
1191 * the tr->max_lock lock, which the code calling
1192 * cond_snapshot.update() has already done.
1193 *
1194 * Returns the cond_data associated with the trace array's snapshot.
1195 */
1196void *tracing_cond_snapshot_data(struct trace_array *tr)
1197{
1198 void *cond_data = NULL;
1199
Olivier Deprez92d4c212022-12-06 15:05:30 +01001200 local_irq_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00001201 arch_spin_lock(&tr->max_lock);
1202
1203 if (tr->cond_snapshot)
1204 cond_data = tr->cond_snapshot->cond_data;
1205
1206 arch_spin_unlock(&tr->max_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001207 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00001208
1209 return cond_data;
1210}
1211EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1212
Olivier Deprez157378f2022-04-04 15:47:50 +02001213static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1214 struct array_buffer *size_buf, int cpu_id);
1215static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001216
1217int tracing_alloc_snapshot_instance(struct trace_array *tr)
1218{
1219 int ret;
1220
1221 if (!tr->allocated_snapshot) {
1222
1223 /* allocate spare buffer */
1224 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Olivier Deprez157378f2022-04-04 15:47:50 +02001225 &tr->array_buffer, RING_BUFFER_ALL_CPUS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001226 if (ret < 0)
1227 return ret;
1228
1229 tr->allocated_snapshot = true;
1230 }
1231
1232 return 0;
1233}
1234
1235static void free_snapshot(struct trace_array *tr)
1236{
1237 /*
1238 * We don't free the ring buffer. instead, resize it because
1239 * The max_tr ring buffer has some state (e.g. ring->clock) and
1240 * we want preserve it.
1241 */
1242 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1243 set_buffer_entries(&tr->max_buffer, 1);
1244 tracing_reset_online_cpus(&tr->max_buffer);
1245 tr->allocated_snapshot = false;
1246}
1247
1248/**
1249 * tracing_alloc_snapshot - allocate snapshot buffer.
1250 *
1251 * This only allocates the snapshot buffer if it isn't already
1252 * allocated - it doesn't also take a snapshot.
1253 *
1254 * This is meant to be used in cases where the snapshot buffer needs
1255 * to be set up for events that can't sleep but need to be able to
1256 * trigger a snapshot.
1257 */
1258int tracing_alloc_snapshot(void)
1259{
1260 struct trace_array *tr = &global_trace;
1261 int ret;
1262
1263 ret = tracing_alloc_snapshot_instance(tr);
1264 WARN_ON(ret < 0);
1265
1266 return ret;
1267}
1268EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1269
1270/**
1271 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
1272 *
1273 * This is similar to tracing_snapshot(), but it will allocate the
1274 * snapshot buffer if it isn't already allocated. Use this only
1275 * where it is safe to sleep, as the allocation may sleep.
1276 *
1277 * This causes a swap between the snapshot buffer and the current live
1278 * tracing buffer. You can use this to take snapshots of the live
1279 * trace when some condition is triggered, but continue to trace.
1280 */
1281void tracing_snapshot_alloc(void)
1282{
1283 int ret;
1284
1285 ret = tracing_alloc_snapshot();
1286 if (ret < 0)
1287 return;
1288
1289 tracing_snapshot();
1290}
1291EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
David Brazdil0f672f62019-12-10 10:32:29 +00001292
1293/**
1294 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1295 * @tr: The tracing instance
1296 * @cond_data: User data to associate with the snapshot
1297 * @update: Implementation of the cond_snapshot update function
1298 *
1299 * Check whether the conditional snapshot for the given instance has
1300 * already been enabled, or if the current tracer is already using a
1301 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1302 * save the cond_data and update function inside.
1303 *
1304 * Returns 0 if successful, error otherwise.
1305 */
1306int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1307 cond_update_fn_t update)
1308{
1309 struct cond_snapshot *cond_snapshot;
1310 int ret = 0;
1311
1312 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1313 if (!cond_snapshot)
1314 return -ENOMEM;
1315
1316 cond_snapshot->cond_data = cond_data;
1317 cond_snapshot->update = update;
1318
1319 mutex_lock(&trace_types_lock);
1320
1321 ret = tracing_alloc_snapshot_instance(tr);
1322 if (ret)
1323 goto fail_unlock;
1324
1325 if (tr->current_trace->use_max_tr) {
1326 ret = -EBUSY;
1327 goto fail_unlock;
1328 }
1329
1330 /*
1331 * The cond_snapshot can only change to NULL without the
1332 * trace_types_lock. We don't care if we race with it going
1333 * to NULL, but we want to make sure that it's not set to
1334 * something other than NULL when we get here, which we can
1335 * do safely with only holding the trace_types_lock and not
1336 * having to take the max_lock.
1337 */
1338 if (tr->cond_snapshot) {
1339 ret = -EBUSY;
1340 goto fail_unlock;
1341 }
1342
Olivier Deprez92d4c212022-12-06 15:05:30 +01001343 local_irq_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00001344 arch_spin_lock(&tr->max_lock);
1345 tr->cond_snapshot = cond_snapshot;
1346 arch_spin_unlock(&tr->max_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001347 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00001348
1349 mutex_unlock(&trace_types_lock);
1350
1351 return ret;
1352
1353 fail_unlock:
1354 mutex_unlock(&trace_types_lock);
1355 kfree(cond_snapshot);
1356 return ret;
1357}
1358EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1359
1360/**
1361 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1362 * @tr: The tracing instance
1363 *
1364 * Check whether the conditional snapshot for the given instance is
1365 * enabled; if so, free the cond_snapshot associated with it,
1366 * otherwise return -EINVAL.
1367 *
1368 * Returns 0 if successful, error otherwise.
1369 */
1370int tracing_snapshot_cond_disable(struct trace_array *tr)
1371{
1372 int ret = 0;
1373
Olivier Deprez92d4c212022-12-06 15:05:30 +01001374 local_irq_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00001375 arch_spin_lock(&tr->max_lock);
1376
1377 if (!tr->cond_snapshot)
1378 ret = -EINVAL;
1379 else {
1380 kfree(tr->cond_snapshot);
1381 tr->cond_snapshot = NULL;
1382 }
1383
1384 arch_spin_unlock(&tr->max_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001385 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00001386
1387 return ret;
1388}
1389EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001390#else
1391void tracing_snapshot(void)
1392{
1393 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1394}
1395EXPORT_SYMBOL_GPL(tracing_snapshot);
David Brazdil0f672f62019-12-10 10:32:29 +00001396void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1397{
1398 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1399}
1400EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401int tracing_alloc_snapshot(void)
1402{
1403 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1404 return -ENODEV;
1405}
1406EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1407void tracing_snapshot_alloc(void)
1408{
1409 /* Give warning */
1410 tracing_snapshot();
1411}
1412EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
David Brazdil0f672f62019-12-10 10:32:29 +00001413void *tracing_cond_snapshot_data(struct trace_array *tr)
1414{
1415 return NULL;
1416}
1417EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1418int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1419{
1420 return -ENODEV;
1421}
1422EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1423int tracing_snapshot_cond_disable(struct trace_array *tr)
1424{
1425 return false;
1426}
1427EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001428#endif /* CONFIG_TRACER_SNAPSHOT */
1429
1430void tracer_tracing_off(struct trace_array *tr)
1431{
Olivier Deprez157378f2022-04-04 15:47:50 +02001432 if (tr->array_buffer.buffer)
1433 ring_buffer_record_off(tr->array_buffer.buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001434 /*
1435 * This flag is looked at when buffers haven't been allocated
1436 * yet, or by some tracers (like irqsoff), that just want to
1437 * know if the ring buffer has been disabled, but it can handle
1438 * races of where it gets disabled but we still do a record.
1439 * As the check is in the fast path of the tracers, it is more
1440 * important to be fast than accurate.
1441 */
1442 tr->buffer_disabled = 1;
1443 /* Make the flag seen by readers */
1444 smp_wmb();
1445}
1446
1447/**
1448 * tracing_off - turn off tracing buffers
1449 *
1450 * This function stops the tracing buffers from recording data.
1451 * It does not disable any overhead the tracers themselves may
1452 * be causing. This function simply causes all recording to
1453 * the ring buffers to fail.
1454 */
1455void tracing_off(void)
1456{
1457 tracer_tracing_off(&global_trace);
1458}
1459EXPORT_SYMBOL_GPL(tracing_off);
1460
1461void disable_trace_on_warning(void)
1462{
Olivier Deprez157378f2022-04-04 15:47:50 +02001463 if (__disable_trace_on_warning) {
1464 trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
1465 "Disabling tracing due to warning\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001466 tracing_off();
Olivier Deprez157378f2022-04-04 15:47:50 +02001467 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001468}
1469
1470/**
1471 * tracer_tracing_is_on - show real state of ring buffer enabled
1472 * @tr : the trace array to know if ring buffer is enabled
1473 *
1474 * Shows real state of the ring buffer if it is enabled or not.
1475 */
1476bool tracer_tracing_is_on(struct trace_array *tr)
1477{
Olivier Deprez157378f2022-04-04 15:47:50 +02001478 if (tr->array_buffer.buffer)
1479 return ring_buffer_record_is_on(tr->array_buffer.buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001480 return !tr->buffer_disabled;
1481}
1482
1483/**
1484 * tracing_is_on - show state of ring buffers enabled
1485 */
1486int tracing_is_on(void)
1487{
1488 return tracer_tracing_is_on(&global_trace);
1489}
1490EXPORT_SYMBOL_GPL(tracing_is_on);
1491
1492static int __init set_buf_size(char *str)
1493{
1494 unsigned long buf_size;
1495
1496 if (!str)
1497 return 0;
1498 buf_size = memparse(str, &str);
Olivier Deprez157378f2022-04-04 15:47:50 +02001499 /*
1500 * nr_entries can not be zero and the startup
1501 * tests require some buffer space. Therefore
1502 * ensure we have at least 4096 bytes of buffer.
1503 */
1504 trace_buf_size = max(4096UL, buf_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001505 return 1;
1506}
1507__setup("trace_buf_size=", set_buf_size);
1508
1509static int __init set_tracing_thresh(char *str)
1510{
1511 unsigned long threshold;
1512 int ret;
1513
1514 if (!str)
1515 return 0;
1516 ret = kstrtoul(str, 0, &threshold);
1517 if (ret < 0)
1518 return 0;
1519 tracing_thresh = threshold * 1000;
1520 return 1;
1521}
1522__setup("tracing_thresh=", set_tracing_thresh);
1523
1524unsigned long nsecs_to_usecs(unsigned long nsecs)
1525{
1526 return nsecs / 1000;
1527}
1528
1529/*
1530 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1531 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1532 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1533 * of strings in the order that the evals (enum) were defined.
1534 */
1535#undef C
1536#define C(a, b) b
1537
1538/* These must match the bit postions in trace_iterator_flags */
1539static const char *trace_options[] = {
1540 TRACE_FLAGS
1541 NULL
1542};
1543
1544static struct {
1545 u64 (*func)(void);
1546 const char *name;
1547 int in_ns; /* is this clock in nanoseconds? */
1548} trace_clocks[] = {
1549 { trace_clock_local, "local", 1 },
1550 { trace_clock_global, "global", 1 },
1551 { trace_clock_counter, "counter", 0 },
1552 { trace_clock_jiffies, "uptime", 0 },
1553 { trace_clock, "perf", 1 },
1554 { ktime_get_mono_fast_ns, "mono", 1 },
1555 { ktime_get_raw_fast_ns, "mono_raw", 1 },
1556 { ktime_get_boot_fast_ns, "boot", 1 },
1557 ARCH_TRACE_CLOCKS
1558};
1559
1560bool trace_clock_in_ns(struct trace_array *tr)
1561{
1562 if (trace_clocks[tr->clock_id].in_ns)
1563 return true;
1564
1565 return false;
1566}
1567
1568/*
1569 * trace_parser_get_init - gets the buffer for trace parser
1570 */
1571int trace_parser_get_init(struct trace_parser *parser, int size)
1572{
1573 memset(parser, 0, sizeof(*parser));
1574
1575 parser->buffer = kmalloc(size, GFP_KERNEL);
1576 if (!parser->buffer)
1577 return 1;
1578
1579 parser->size = size;
1580 return 0;
1581}
1582
1583/*
1584 * trace_parser_put - frees the buffer for trace parser
1585 */
1586void trace_parser_put(struct trace_parser *parser)
1587{
1588 kfree(parser->buffer);
1589 parser->buffer = NULL;
1590}
1591
1592/*
1593 * trace_get_user - reads the user input string separated by space
1594 * (matched by isspace(ch))
1595 *
1596 * For each string found the 'struct trace_parser' is updated,
1597 * and the function returns.
1598 *
1599 * Returns number of bytes read.
1600 *
1601 * See kernel/trace/trace.h for 'struct trace_parser' details.
1602 */
1603int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1604 size_t cnt, loff_t *ppos)
1605{
1606 char ch;
1607 size_t read = 0;
1608 ssize_t ret;
1609
1610 if (!*ppos)
1611 trace_parser_clear(parser);
1612
1613 ret = get_user(ch, ubuf++);
1614 if (ret)
1615 goto out;
1616
1617 read++;
1618 cnt--;
1619
1620 /*
1621 * The parser is not finished with the last write,
1622 * continue reading the user input without skipping spaces.
1623 */
1624 if (!parser->cont) {
1625 /* skip white space */
1626 while (cnt && isspace(ch)) {
1627 ret = get_user(ch, ubuf++);
1628 if (ret)
1629 goto out;
1630 read++;
1631 cnt--;
1632 }
1633
1634 parser->idx = 0;
1635
1636 /* only spaces were written */
1637 if (isspace(ch) || !ch) {
1638 *ppos += read;
1639 ret = read;
1640 goto out;
1641 }
1642 }
1643
1644 /* read the non-space input */
1645 while (cnt && !isspace(ch) && ch) {
1646 if (parser->idx < parser->size - 1)
1647 parser->buffer[parser->idx++] = ch;
1648 else {
1649 ret = -EINVAL;
1650 goto out;
1651 }
1652 ret = get_user(ch, ubuf++);
1653 if (ret)
1654 goto out;
1655 read++;
1656 cnt--;
1657 }
1658
1659 /* We either got finished input or we have to wait for another call. */
1660 if (isspace(ch) || !ch) {
1661 parser->buffer[parser->idx] = 0;
1662 parser->cont = false;
1663 } else if (parser->idx < parser->size - 1) {
1664 parser->cont = true;
1665 parser->buffer[parser->idx++] = ch;
1666 /* Make sure the parsed string always terminates with '\0'. */
1667 parser->buffer[parser->idx] = 0;
1668 } else {
1669 ret = -EINVAL;
1670 goto out;
1671 }
1672
1673 *ppos += read;
1674 ret = read;
1675
1676out:
1677 return ret;
1678}
1679
1680/* TODO add a seq_buf_to_buffer() */
1681static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1682{
1683 int len;
1684
1685 if (trace_seq_used(s) <= s->seq.readpos)
1686 return -EBUSY;
1687
1688 len = trace_seq_used(s) - s->seq.readpos;
1689 if (cnt > len)
1690 cnt = len;
1691 memcpy(buf, s->buffer + s->seq.readpos, cnt);
1692
1693 s->seq.readpos += cnt;
1694 return cnt;
1695}
1696
1697unsigned long __read_mostly tracing_thresh;
Olivier Deprez157378f2022-04-04 15:47:50 +02001698static const struct file_operations tracing_max_lat_fops;
1699
1700#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1701 defined(CONFIG_FSNOTIFY)
1702
1703static struct workqueue_struct *fsnotify_wq;
1704
1705static void latency_fsnotify_workfn(struct work_struct *work)
1706{
1707 struct trace_array *tr = container_of(work, struct trace_array,
1708 fsnotify_work);
1709 fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
1710}
1711
1712static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1713{
1714 struct trace_array *tr = container_of(iwork, struct trace_array,
1715 fsnotify_irqwork);
1716 queue_work(fsnotify_wq, &tr->fsnotify_work);
1717}
1718
1719static void trace_create_maxlat_file(struct trace_array *tr,
1720 struct dentry *d_tracer)
1721{
1722 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1723 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1724 tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
1725 d_tracer, &tr->max_latency,
1726 &tracing_max_lat_fops);
1727}
1728
1729__init static int latency_fsnotify_init(void)
1730{
1731 fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1732 WQ_UNBOUND | WQ_HIGHPRI, 0);
1733 if (!fsnotify_wq) {
1734 pr_err("Unable to allocate tr_max_lat_wq\n");
1735 return -ENOMEM;
1736 }
1737 return 0;
1738}
1739
1740late_initcall_sync(latency_fsnotify_init);
1741
1742void latency_fsnotify(struct trace_array *tr)
1743{
1744 if (!fsnotify_wq)
1745 return;
1746 /*
1747 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1748 * possible that we are called from __schedule() or do_idle(), which
1749 * could cause a deadlock.
1750 */
1751 irq_work_queue(&tr->fsnotify_irqwork);
1752}
1753
1754/*
1755 * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1756 * defined(CONFIG_FSNOTIFY)
1757 */
1758#else
1759
1760#define trace_create_maxlat_file(tr, d_tracer) \
1761 trace_create_file("tracing_max_latency", 0644, d_tracer, \
1762 &tr->max_latency, &tracing_max_lat_fops)
1763
1764#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001765
1766#ifdef CONFIG_TRACER_MAX_TRACE
1767/*
1768 * Copy the new maximum trace into the separate maximum-trace
1769 * structure. (this way the maximum trace is permanently saved,
1770 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
1771 */
1772static void
1773__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1774{
Olivier Deprez157378f2022-04-04 15:47:50 +02001775 struct array_buffer *trace_buf = &tr->array_buffer;
1776 struct array_buffer *max_buf = &tr->max_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001777 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1778 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1779
1780 max_buf->cpu = cpu;
1781 max_buf->time_start = data->preempt_timestamp;
1782
1783 max_data->saved_latency = tr->max_latency;
1784 max_data->critical_start = data->critical_start;
1785 max_data->critical_end = data->critical_end;
1786
David Brazdil0f672f62019-12-10 10:32:29 +00001787 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 max_data->pid = tsk->pid;
1789 /*
1790 * If tsk == current, then use current_uid(), as that does not use
1791 * RCU. The irq tracer can be called out of RCU scope.
1792 */
1793 if (tsk == current)
1794 max_data->uid = current_uid();
1795 else
1796 max_data->uid = task_uid(tsk);
1797
1798 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1799 max_data->policy = tsk->policy;
1800 max_data->rt_priority = tsk->rt_priority;
1801
1802 /* record this tasks comm */
1803 tracing_record_cmdline(tsk);
Olivier Deprez157378f2022-04-04 15:47:50 +02001804 latency_fsnotify(tr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001805}
1806
1807/**
1808 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1809 * @tr: tracer
1810 * @tsk: the task with the latency
1811 * @cpu: The cpu that initiated the trace.
David Brazdil0f672f62019-12-10 10:32:29 +00001812 * @cond_data: User data associated with a conditional snapshot
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813 *
1814 * Flip the buffers between the @tr and the max_tr and record information
1815 * about which task was the cause of this latency.
1816 */
1817void
David Brazdil0f672f62019-12-10 10:32:29 +00001818update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1819 void *cond_data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001820{
1821 if (tr->stop_count)
1822 return;
1823
1824 WARN_ON_ONCE(!irqs_disabled());
1825
1826 if (!tr->allocated_snapshot) {
1827 /* Only the nop tracer should hit this when disabling */
1828 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1829 return;
1830 }
1831
1832 arch_spin_lock(&tr->max_lock);
1833
Olivier Deprez157378f2022-04-04 15:47:50 +02001834 /* Inherit the recordable setting from array_buffer */
1835 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001836 ring_buffer_record_on(tr->max_buffer.buffer);
1837 else
1838 ring_buffer_record_off(tr->max_buffer.buffer);
1839
David Brazdil0f672f62019-12-10 10:32:29 +00001840#ifdef CONFIG_TRACER_SNAPSHOT
1841 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1842 goto out_unlock;
1843#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001844 swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001845
1846 __update_max_tr(tr, tsk, cpu);
David Brazdil0f672f62019-12-10 10:32:29 +00001847
1848 out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001849 arch_spin_unlock(&tr->max_lock);
1850}
1851
1852/**
1853 * update_max_tr_single - only copy one trace over, and reset the rest
David Brazdil0f672f62019-12-10 10:32:29 +00001854 * @tr: tracer
1855 * @tsk: task with the latency
1856 * @cpu: the cpu of the buffer to copy.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001857 *
1858 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1859 */
1860void
1861update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1862{
1863 int ret;
1864
1865 if (tr->stop_count)
1866 return;
1867
1868 WARN_ON_ONCE(!irqs_disabled());
1869 if (!tr->allocated_snapshot) {
1870 /* Only the nop tracer should hit this when disabling */
1871 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1872 return;
1873 }
1874
1875 arch_spin_lock(&tr->max_lock);
1876
Olivier Deprez157378f2022-04-04 15:47:50 +02001877 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001878
1879 if (ret == -EBUSY) {
1880 /*
1881 * We failed to swap the buffer due to a commit taking
1882 * place on this CPU. We fail to record, but we reset
1883 * the max trace buffer (no one writes directly to it)
1884 * and flag that it failed.
1885 */
1886 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1887 "Failed to swap buffers due to commit in progress\n");
1888 }
1889
1890 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1891
1892 __update_max_tr(tr, tsk, cpu);
1893 arch_spin_unlock(&tr->max_lock);
1894}
1895#endif /* CONFIG_TRACER_MAX_TRACE */
1896
David Brazdil0f672f62019-12-10 10:32:29 +00001897static int wait_on_pipe(struct trace_iterator *iter, int full)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001898{
1899 /* Iterators are static, they should be filled or empty */
1900 if (trace_buffer_iter(iter, iter->cpu_file))
1901 return 0;
1902
Olivier Deprez157378f2022-04-04 15:47:50 +02001903 return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001904 full);
1905}
1906
1907#ifdef CONFIG_FTRACE_STARTUP_TEST
1908static bool selftests_can_run;
1909
1910struct trace_selftests {
1911 struct list_head list;
1912 struct tracer *type;
1913};
1914
1915static LIST_HEAD(postponed_selftests);
1916
1917static int save_selftest(struct tracer *type)
1918{
1919 struct trace_selftests *selftest;
1920
1921 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1922 if (!selftest)
1923 return -ENOMEM;
1924
1925 selftest->type = type;
1926 list_add(&selftest->list, &postponed_selftests);
1927 return 0;
1928}
1929
1930static int run_tracer_selftest(struct tracer *type)
1931{
1932 struct trace_array *tr = &global_trace;
1933 struct tracer *saved_tracer = tr->current_trace;
1934 int ret;
1935
1936 if (!type->selftest || tracing_selftest_disabled)
1937 return 0;
1938
1939 /*
1940 * If a tracer registers early in boot up (before scheduling is
1941 * initialized and such), then do not run its selftests yet.
1942 * Instead, run it a little later in the boot process.
1943 */
1944 if (!selftests_can_run)
1945 return save_selftest(type);
1946
1947 /*
1948 * Run a selftest on this tracer.
1949 * Here we reset the trace buffer, and set the current
1950 * tracer to be this tracer. The tracer can then run some
1951 * internal tracing to verify that everything is in order.
1952 * If we fail, we do not register this tracer.
1953 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001954 tracing_reset_online_cpus(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955
1956 tr->current_trace = type;
1957
1958#ifdef CONFIG_TRACER_MAX_TRACE
1959 if (type->use_max_tr) {
1960 /* If we expanded the buffers, make sure the max is expanded too */
1961 if (ring_buffer_expanded)
1962 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1963 RING_BUFFER_ALL_CPUS);
1964 tr->allocated_snapshot = true;
1965 }
1966#endif
1967
1968 /* the test is responsible for initializing and enabling */
1969 pr_info("Testing tracer %s: ", type->name);
1970 ret = type->selftest(type, tr);
1971 /* the test is responsible for resetting too */
1972 tr->current_trace = saved_tracer;
1973 if (ret) {
1974 printk(KERN_CONT "FAILED!\n");
1975 /* Add the warning after printing 'FAILED' */
1976 WARN_ON(1);
1977 return -1;
1978 }
1979 /* Only reset on passing, to avoid touching corrupted buffers */
Olivier Deprez157378f2022-04-04 15:47:50 +02001980 tracing_reset_online_cpus(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001981
1982#ifdef CONFIG_TRACER_MAX_TRACE
1983 if (type->use_max_tr) {
1984 tr->allocated_snapshot = false;
1985
1986 /* Shrink the max buffer again */
1987 if (ring_buffer_expanded)
1988 ring_buffer_resize(tr->max_buffer.buffer, 1,
1989 RING_BUFFER_ALL_CPUS);
1990 }
1991#endif
1992
1993 printk(KERN_CONT "PASSED\n");
1994 return 0;
1995}
1996
1997static __init int init_trace_selftests(void)
1998{
1999 struct trace_selftests *p, *n;
2000 struct tracer *t, **last;
2001 int ret;
2002
2003 selftests_can_run = true;
2004
2005 mutex_lock(&trace_types_lock);
2006
2007 if (list_empty(&postponed_selftests))
2008 goto out;
2009
2010 pr_info("Running postponed tracer tests:\n");
2011
Olivier Deprez0e641232021-09-23 10:07:05 +02002012 tracing_selftest_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002013 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
David Brazdil0f672f62019-12-10 10:32:29 +00002014 /* This loop can take minutes when sanitizers are enabled, so
2015 * lets make sure we allow RCU processing.
2016 */
2017 cond_resched();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002018 ret = run_tracer_selftest(p->type);
2019 /* If the test fails, then warn and remove from available_tracers */
2020 if (ret < 0) {
2021 WARN(1, "tracer: %s failed selftest, disabling\n",
2022 p->type->name);
2023 last = &trace_types;
2024 for (t = trace_types; t; t = t->next) {
2025 if (t == p->type) {
2026 *last = t->next;
2027 break;
2028 }
2029 last = &t->next;
2030 }
2031 }
2032 list_del(&p->list);
2033 kfree(p);
2034 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002035 tracing_selftest_running = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002036
2037 out:
2038 mutex_unlock(&trace_types_lock);
2039
2040 return 0;
2041}
2042core_initcall(init_trace_selftests);
2043#else
2044static inline int run_tracer_selftest(struct tracer *type)
2045{
2046 return 0;
2047}
2048#endif /* CONFIG_FTRACE_STARTUP_TEST */
2049
2050static void add_tracer_options(struct trace_array *tr, struct tracer *t);
2051
2052static void __init apply_trace_boot_options(void);
2053
2054/**
2055 * register_tracer - register a tracer with the ftrace system.
David Brazdil0f672f62019-12-10 10:32:29 +00002056 * @type: the plugin for the tracer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002057 *
2058 * Register a new plugin tracer.
2059 */
2060int __init register_tracer(struct tracer *type)
2061{
2062 struct tracer *t;
2063 int ret = 0;
2064
2065 if (!type->name) {
2066 pr_info("Tracer must have a name\n");
2067 return -1;
2068 }
2069
2070 if (strlen(type->name) >= MAX_TRACER_SIZE) {
2071 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
2072 return -1;
2073 }
2074
Olivier Deprez0e641232021-09-23 10:07:05 +02002075 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002076 pr_warn("Can not register tracer %s due to lockdown\n",
Olivier Deprez0e641232021-09-23 10:07:05 +02002077 type->name);
2078 return -EPERM;
2079 }
2080
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002081 mutex_lock(&trace_types_lock);
2082
2083 tracing_selftest_running = true;
2084
2085 for (t = trace_types; t; t = t->next) {
2086 if (strcmp(type->name, t->name) == 0) {
2087 /* already found */
2088 pr_info("Tracer %s already registered\n",
2089 type->name);
2090 ret = -1;
2091 goto out;
2092 }
2093 }
2094
2095 if (!type->set_flag)
2096 type->set_flag = &dummy_set_flag;
2097 if (!type->flags) {
2098 /*allocate a dummy tracer_flags*/
2099 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
2100 if (!type->flags) {
2101 ret = -ENOMEM;
2102 goto out;
2103 }
2104 type->flags->val = 0;
2105 type->flags->opts = dummy_tracer_opt;
2106 } else
2107 if (!type->flags->opts)
2108 type->flags->opts = dummy_tracer_opt;
2109
2110 /* store the tracer for __set_tracer_option */
2111 type->flags->trace = type;
2112
2113 ret = run_tracer_selftest(type);
2114 if (ret < 0)
2115 goto out;
2116
2117 type->next = trace_types;
2118 trace_types = type;
2119 add_tracer_options(&global_trace, type);
2120
2121 out:
2122 tracing_selftest_running = false;
2123 mutex_unlock(&trace_types_lock);
2124
2125 if (ret || !default_bootup_tracer)
2126 goto out_unlock;
2127
2128 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
2129 goto out_unlock;
2130
2131 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
2132 /* Do we want this tracer to start on bootup? */
2133 tracing_set_tracer(&global_trace, type->name);
2134 default_bootup_tracer = NULL;
2135
2136 apply_trace_boot_options();
2137
2138 /* disable other selftests, since this will break it. */
Olivier Deprez157378f2022-04-04 15:47:50 +02002139 disable_tracing_selftest("running a tracer");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002140
2141 out_unlock:
2142 return ret;
2143}
2144
Olivier Deprez157378f2022-04-04 15:47:50 +02002145static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002146{
Olivier Deprez157378f2022-04-04 15:47:50 +02002147 struct trace_buffer *buffer = buf->buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002148
2149 if (!buffer)
2150 return;
2151
2152 ring_buffer_record_disable(buffer);
2153
2154 /* Make sure all commits have finished */
David Brazdil0f672f62019-12-10 10:32:29 +00002155 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002156 ring_buffer_reset_cpu(buffer, cpu);
2157
2158 ring_buffer_record_enable(buffer);
2159}
2160
Olivier Deprez157378f2022-04-04 15:47:50 +02002161void tracing_reset_online_cpus(struct array_buffer *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002162{
Olivier Deprez157378f2022-04-04 15:47:50 +02002163 struct trace_buffer *buffer = buf->buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002164
2165 if (!buffer)
2166 return;
2167
2168 ring_buffer_record_disable(buffer);
2169
2170 /* Make sure all commits have finished */
David Brazdil0f672f62019-12-10 10:32:29 +00002171 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002172
2173 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2174
Olivier Deprez157378f2022-04-04 15:47:50 +02002175 ring_buffer_reset_online_cpus(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176
2177 ring_buffer_record_enable(buffer);
2178}
2179
2180/* Must have trace_types_lock held */
2181void tracing_reset_all_online_cpus(void)
2182{
2183 struct trace_array *tr;
2184
2185 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2186 if (!tr->clear_trace)
2187 continue;
2188 tr->clear_trace = false;
Olivier Deprez157378f2022-04-04 15:47:50 +02002189 tracing_reset_online_cpus(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002190#ifdef CONFIG_TRACER_MAX_TRACE
2191 tracing_reset_online_cpus(&tr->max_buffer);
2192#endif
2193 }
2194}
2195
Olivier Deprez0e641232021-09-23 10:07:05 +02002196/*
2197 * The tgid_map array maps from pid to tgid; i.e. the value stored at index i
2198 * is the tgid last observed corresponding to pid=i.
2199 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002200static int *tgid_map;
2201
Olivier Deprez0e641232021-09-23 10:07:05 +02002202/* The maximum valid index into tgid_map. */
2203static size_t tgid_map_max;
2204
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002205#define SAVED_CMDLINES_DEFAULT 128
2206#define NO_CMDLINE_MAP UINT_MAX
Olivier Deprez92d4c212022-12-06 15:05:30 +01002207/*
2208 * Preemption must be disabled before acquiring trace_cmdline_lock.
2209 * The various trace_arrays' max_lock must be acquired in a context
2210 * where interrupt is disabled.
2211 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002212static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
2213struct saved_cmdlines_buffer {
2214 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
2215 unsigned *map_cmdline_to_pid;
2216 unsigned cmdline_num;
2217 int cmdline_idx;
2218 char *saved_cmdlines;
2219};
2220static struct saved_cmdlines_buffer *savedcmd;
2221
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002222static inline char *get_saved_cmdlines(int idx)
2223{
2224 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
2225}
2226
2227static inline void set_cmdline(int idx, const char *cmdline)
2228{
David Brazdil0f672f62019-12-10 10:32:29 +00002229 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230}
2231
2232static int allocate_cmdlines_buffer(unsigned int val,
2233 struct saved_cmdlines_buffer *s)
2234{
2235 s->map_cmdline_to_pid = kmalloc_array(val,
2236 sizeof(*s->map_cmdline_to_pid),
2237 GFP_KERNEL);
2238 if (!s->map_cmdline_to_pid)
2239 return -ENOMEM;
2240
2241 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
2242 if (!s->saved_cmdlines) {
2243 kfree(s->map_cmdline_to_pid);
2244 return -ENOMEM;
2245 }
2246
2247 s->cmdline_idx = 0;
2248 s->cmdline_num = val;
2249 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
2250 sizeof(s->map_pid_to_cmdline));
2251 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
2252 val * sizeof(*s->map_cmdline_to_pid));
2253
2254 return 0;
2255}
2256
2257static int trace_create_savedcmd(void)
2258{
2259 int ret;
2260
2261 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
2262 if (!savedcmd)
2263 return -ENOMEM;
2264
2265 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
2266 if (ret < 0) {
2267 kfree(savedcmd);
2268 savedcmd = NULL;
2269 return -ENOMEM;
2270 }
2271
2272 return 0;
2273}
2274
2275int is_tracing_stopped(void)
2276{
2277 return global_trace.stop_count;
2278}
2279
2280/**
2281 * tracing_start - quick start of the tracer
2282 *
2283 * If tracing is enabled but was stopped by tracing_stop,
2284 * this will start the tracer back up.
2285 */
2286void tracing_start(void)
2287{
Olivier Deprez157378f2022-04-04 15:47:50 +02002288 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002289 unsigned long flags;
2290
2291 if (tracing_disabled)
2292 return;
2293
2294 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2295 if (--global_trace.stop_count) {
2296 if (global_trace.stop_count < 0) {
2297 /* Someone screwed up their debugging */
2298 WARN_ON_ONCE(1);
2299 global_trace.stop_count = 0;
2300 }
2301 goto out;
2302 }
2303
2304 /* Prevent the buffers from switching */
2305 arch_spin_lock(&global_trace.max_lock);
2306
Olivier Deprez157378f2022-04-04 15:47:50 +02002307 buffer = global_trace.array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002308 if (buffer)
2309 ring_buffer_record_enable(buffer);
2310
2311#ifdef CONFIG_TRACER_MAX_TRACE
2312 buffer = global_trace.max_buffer.buffer;
2313 if (buffer)
2314 ring_buffer_record_enable(buffer);
2315#endif
2316
2317 arch_spin_unlock(&global_trace.max_lock);
2318
2319 out:
2320 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2321}
2322
2323static void tracing_start_tr(struct trace_array *tr)
2324{
Olivier Deprez157378f2022-04-04 15:47:50 +02002325 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002326 unsigned long flags;
2327
2328 if (tracing_disabled)
2329 return;
2330
2331 /* If global, we need to also start the max tracer */
2332 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2333 return tracing_start();
2334
2335 raw_spin_lock_irqsave(&tr->start_lock, flags);
2336
2337 if (--tr->stop_count) {
2338 if (tr->stop_count < 0) {
2339 /* Someone screwed up their debugging */
2340 WARN_ON_ONCE(1);
2341 tr->stop_count = 0;
2342 }
2343 goto out;
2344 }
2345
Olivier Deprez157378f2022-04-04 15:47:50 +02002346 buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002347 if (buffer)
2348 ring_buffer_record_enable(buffer);
2349
2350 out:
2351 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2352}
2353
2354/**
2355 * tracing_stop - quick stop of the tracer
2356 *
2357 * Light weight way to stop tracing. Use in conjunction with
2358 * tracing_start.
2359 */
2360void tracing_stop(void)
2361{
Olivier Deprez157378f2022-04-04 15:47:50 +02002362 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002363 unsigned long flags;
2364
2365 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2366 if (global_trace.stop_count++)
2367 goto out;
2368
2369 /* Prevent the buffers from switching */
2370 arch_spin_lock(&global_trace.max_lock);
2371
Olivier Deprez157378f2022-04-04 15:47:50 +02002372 buffer = global_trace.array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002373 if (buffer)
2374 ring_buffer_record_disable(buffer);
2375
2376#ifdef CONFIG_TRACER_MAX_TRACE
2377 buffer = global_trace.max_buffer.buffer;
2378 if (buffer)
2379 ring_buffer_record_disable(buffer);
2380#endif
2381
2382 arch_spin_unlock(&global_trace.max_lock);
2383
2384 out:
2385 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2386}
2387
2388static void tracing_stop_tr(struct trace_array *tr)
2389{
Olivier Deprez157378f2022-04-04 15:47:50 +02002390 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002391 unsigned long flags;
2392
2393 /* If global, we need to also stop the max tracer */
2394 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2395 return tracing_stop();
2396
2397 raw_spin_lock_irqsave(&tr->start_lock, flags);
2398 if (tr->stop_count++)
2399 goto out;
2400
Olivier Deprez157378f2022-04-04 15:47:50 +02002401 buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002402 if (buffer)
2403 ring_buffer_record_disable(buffer);
2404
2405 out:
2406 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2407}
2408
2409static int trace_save_cmdline(struct task_struct *tsk)
2410{
Olivier Deprez0e641232021-09-23 10:07:05 +02002411 unsigned tpid, idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002412
2413 /* treat recording of idle task as a success */
2414 if (!tsk->pid)
2415 return 1;
2416
Olivier Deprez0e641232021-09-23 10:07:05 +02002417 tpid = tsk->pid & (PID_MAX_DEFAULT - 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002418
2419 /*
2420 * It's not the end of the world if we don't get
2421 * the lock, but we also don't want to spin
2422 * nor do we want to disable interrupts,
2423 * so if we miss here, then better luck next time.
Olivier Deprez92d4c212022-12-06 15:05:30 +01002424 *
2425 * This is called within the scheduler and wake up, so interrupts
2426 * had better been disabled and run queue lock been held.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002427 */
Olivier Deprez92d4c212022-12-06 15:05:30 +01002428 lockdep_assert_preemption_disabled();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002429 if (!arch_spin_trylock(&trace_cmdline_lock))
2430 return 0;
2431
Olivier Deprez0e641232021-09-23 10:07:05 +02002432 idx = savedcmd->map_pid_to_cmdline[tpid];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002433 if (idx == NO_CMDLINE_MAP) {
2434 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
2435
Olivier Deprez0e641232021-09-23 10:07:05 +02002436 savedcmd->map_pid_to_cmdline[tpid] = idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002437 savedcmd->cmdline_idx = idx;
2438 }
2439
Olivier Deprez0e641232021-09-23 10:07:05 +02002440 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002441 set_cmdline(idx, tsk->comm);
2442
2443 arch_spin_unlock(&trace_cmdline_lock);
2444
2445 return 1;
2446}
2447
2448static void __trace_find_cmdline(int pid, char comm[])
2449{
2450 unsigned map;
Olivier Deprez0e641232021-09-23 10:07:05 +02002451 int tpid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002452
2453 if (!pid) {
2454 strcpy(comm, "<idle>");
2455 return;
2456 }
2457
2458 if (WARN_ON_ONCE(pid < 0)) {
2459 strcpy(comm, "<XXX>");
2460 return;
2461 }
2462
Olivier Deprez0e641232021-09-23 10:07:05 +02002463 tpid = pid & (PID_MAX_DEFAULT - 1);
2464 map = savedcmd->map_pid_to_cmdline[tpid];
2465 if (map != NO_CMDLINE_MAP) {
2466 tpid = savedcmd->map_cmdline_to_pid[map];
2467 if (tpid == pid) {
2468 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
2469 return;
2470 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002471 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002472 strcpy(comm, "<...>");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002473}
2474
2475void trace_find_cmdline(int pid, char comm[])
2476{
2477 preempt_disable();
2478 arch_spin_lock(&trace_cmdline_lock);
2479
2480 __trace_find_cmdline(pid, comm);
2481
2482 arch_spin_unlock(&trace_cmdline_lock);
2483 preempt_enable();
2484}
2485
Olivier Deprez0e641232021-09-23 10:07:05 +02002486static int *trace_find_tgid_ptr(int pid)
2487{
2488 /*
2489 * Pairs with the smp_store_release in set_tracer_flag() to ensure that
2490 * if we observe a non-NULL tgid_map then we also observe the correct
2491 * tgid_map_max.
2492 */
2493 int *map = smp_load_acquire(&tgid_map);
2494
2495 if (unlikely(!map || pid > tgid_map_max))
2496 return NULL;
2497
2498 return &map[pid];
2499}
2500
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002501int trace_find_tgid(int pid)
2502{
Olivier Deprez0e641232021-09-23 10:07:05 +02002503 int *ptr = trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002504
Olivier Deprez0e641232021-09-23 10:07:05 +02002505 return ptr ? *ptr : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002506}
2507
2508static int trace_save_tgid(struct task_struct *tsk)
2509{
Olivier Deprez0e641232021-09-23 10:07:05 +02002510 int *ptr;
2511
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002512 /* treat recording of idle task as a success */
2513 if (!tsk->pid)
2514 return 1;
2515
Olivier Deprez0e641232021-09-23 10:07:05 +02002516 ptr = trace_find_tgid_ptr(tsk->pid);
2517 if (!ptr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002518 return 0;
2519
Olivier Deprez0e641232021-09-23 10:07:05 +02002520 *ptr = tsk->tgid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002521 return 1;
2522}
2523
2524static bool tracing_record_taskinfo_skip(int flags)
2525{
2526 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2527 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002528 if (!__this_cpu_read(trace_taskinfo_save))
2529 return true;
2530 return false;
2531}
2532
2533/**
2534 * tracing_record_taskinfo - record the task info of a task
2535 *
David Brazdil0f672f62019-12-10 10:32:29 +00002536 * @task: task to record
2537 * @flags: TRACE_RECORD_CMDLINE for recording comm
2538 * TRACE_RECORD_TGID for recording tgid
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002539 */
2540void tracing_record_taskinfo(struct task_struct *task, int flags)
2541{
2542 bool done;
2543
2544 if (tracing_record_taskinfo_skip(flags))
2545 return;
2546
2547 /*
2548 * Record as much task information as possible. If some fail, continue
2549 * to try to record the others.
2550 */
2551 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2552 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2553
2554 /* If recording any information failed, retry again soon. */
2555 if (!done)
2556 return;
2557
2558 __this_cpu_write(trace_taskinfo_save, false);
2559}
2560
2561/**
2562 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2563 *
David Brazdil0f672f62019-12-10 10:32:29 +00002564 * @prev: previous task during sched_switch
2565 * @next: next task during sched_switch
2566 * @flags: TRACE_RECORD_CMDLINE for recording comm
2567 * TRACE_RECORD_TGID for recording tgid
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002568 */
2569void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2570 struct task_struct *next, int flags)
2571{
2572 bool done;
2573
2574 if (tracing_record_taskinfo_skip(flags))
2575 return;
2576
2577 /*
2578 * Record as much task information as possible. If some fail, continue
2579 * to try to record the others.
2580 */
2581 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2582 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2583 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2584 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
2585
2586 /* If recording any information failed, retry again soon. */
2587 if (!done)
2588 return;
2589
2590 __this_cpu_write(trace_taskinfo_save, false);
2591}
2592
2593/* Helpers to record a specific task information */
2594void tracing_record_cmdline(struct task_struct *task)
2595{
2596 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2597}
2598
2599void tracing_record_tgid(struct task_struct *task)
2600{
2601 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
2602}
2603
2604/*
2605 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2606 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2607 * simplifies those functions and keeps them in sync.
2608 */
2609enum print_line_t trace_handle_return(struct trace_seq *s)
2610{
2611 return trace_seq_has_overflowed(s) ?
2612 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2613}
2614EXPORT_SYMBOL_GPL(trace_handle_return);
2615
2616void
David Brazdil0f672f62019-12-10 10:32:29 +00002617tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2618 unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002619{
2620 struct task_struct *tsk = current;
2621
2622 entry->preempt_count = pc & 0xff;
2623 entry->pid = (tsk) ? tsk->pid : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002624 entry->type = type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002625 entry->flags =
2626#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2627 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
2628#else
2629 TRACE_FLAG_IRQS_NOSUPPORT |
2630#endif
2631 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
2632 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
2633 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
2634 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2635 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
2636}
2637EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
2638
2639struct ring_buffer_event *
Olivier Deprez157378f2022-04-04 15:47:50 +02002640trace_buffer_lock_reserve(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002641 int type,
2642 unsigned long len,
2643 unsigned long flags, int pc)
2644{
2645 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
2646}
2647
2648DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2649DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2650static int trace_buffered_event_ref;
2651
2652/**
2653 * trace_buffered_event_enable - enable buffering events
2654 *
2655 * When events are being filtered, it is quicker to use a temporary
2656 * buffer to write the event data into if there's a likely chance
2657 * that it will not be committed. The discard of the ring buffer
2658 * is not as fast as committing, and is much slower than copying
2659 * a commit.
2660 *
2661 * When an event is to be filtered, allocate per cpu buffers to
2662 * write the event data into, and if the event is filtered and discarded
2663 * it is simply dropped, otherwise, the entire data is to be committed
2664 * in one shot.
2665 */
2666void trace_buffered_event_enable(void)
2667{
2668 struct ring_buffer_event *event;
2669 struct page *page;
2670 int cpu;
2671
2672 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2673
2674 if (trace_buffered_event_ref++)
2675 return;
2676
2677 for_each_tracing_cpu(cpu) {
2678 page = alloc_pages_node(cpu_to_node(cpu),
2679 GFP_KERNEL | __GFP_NORETRY, 0);
2680 if (!page)
2681 goto failed;
2682
2683 event = page_address(page);
2684 memset(event, 0, sizeof(*event));
2685
2686 per_cpu(trace_buffered_event, cpu) = event;
2687
2688 preempt_disable();
2689 if (cpu == smp_processor_id() &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002690 __this_cpu_read(trace_buffered_event) !=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002691 per_cpu(trace_buffered_event, cpu))
2692 WARN_ON_ONCE(1);
2693 preempt_enable();
2694 }
2695
2696 return;
2697 failed:
2698 trace_buffered_event_disable();
2699}
2700
2701static void enable_trace_buffered_event(void *data)
2702{
2703 /* Probably not needed, but do it anyway */
2704 smp_rmb();
2705 this_cpu_dec(trace_buffered_event_cnt);
2706}
2707
2708static void disable_trace_buffered_event(void *data)
2709{
2710 this_cpu_inc(trace_buffered_event_cnt);
2711}
2712
2713/**
2714 * trace_buffered_event_disable - disable buffering events
2715 *
2716 * When a filter is removed, it is faster to not use the buffered
2717 * events, and to commit directly into the ring buffer. Free up
2718 * the temp buffers when there are no more users. This requires
2719 * special synchronization with current events.
2720 */
2721void trace_buffered_event_disable(void)
2722{
2723 int cpu;
2724
2725 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2726
2727 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2728 return;
2729
2730 if (--trace_buffered_event_ref)
2731 return;
2732
2733 preempt_disable();
2734 /* For each CPU, set the buffer as used. */
2735 smp_call_function_many(tracing_buffer_mask,
2736 disable_trace_buffered_event, NULL, 1);
2737 preempt_enable();
2738
2739 /* Wait for all current users to finish */
David Brazdil0f672f62019-12-10 10:32:29 +00002740 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002741
2742 for_each_tracing_cpu(cpu) {
2743 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2744 per_cpu(trace_buffered_event, cpu) = NULL;
2745 }
2746 /*
2747 * Make sure trace_buffered_event is NULL before clearing
2748 * trace_buffered_event_cnt.
2749 */
2750 smp_wmb();
2751
2752 preempt_disable();
2753 /* Do the work on each cpu */
2754 smp_call_function_many(tracing_buffer_mask,
2755 enable_trace_buffered_event, NULL, 1);
2756 preempt_enable();
2757}
2758
Olivier Deprez157378f2022-04-04 15:47:50 +02002759static struct trace_buffer *temp_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002760
2761struct ring_buffer_event *
Olivier Deprez157378f2022-04-04 15:47:50 +02002762trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002763 struct trace_event_file *trace_file,
2764 int type, unsigned long len,
2765 unsigned long flags, int pc)
2766{
2767 struct ring_buffer_event *entry;
2768 int val;
2769
Olivier Deprez157378f2022-04-04 15:47:50 +02002770 *current_rb = trace_file->tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002771
2772 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
2773 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2774 (entry = this_cpu_read(trace_buffered_event))) {
2775 /* Try to use the per cpu buffer first */
2776 val = this_cpu_inc_return(trace_buffered_event_cnt);
Olivier Deprez0e641232021-09-23 10:07:05 +02002777 if ((len < (PAGE_SIZE - sizeof(*entry) - sizeof(entry->array[0]))) && val == 1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002778 trace_event_setup(entry, type, flags, pc);
2779 entry->array[0] = len;
2780 return entry;
2781 }
2782 this_cpu_dec(trace_buffered_event_cnt);
2783 }
2784
2785 entry = __trace_buffer_lock_reserve(*current_rb,
2786 type, len, flags, pc);
2787 /*
2788 * If tracing is off, but we have triggers enabled
2789 * we still need to look at the event data. Use the temp_buffer
Olivier Deprez0e641232021-09-23 10:07:05 +02002790 * to store the trace event for the trigger to use. It's recursive
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002791 * safe and will not be recorded anywhere.
2792 */
2793 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2794 *current_rb = temp_buffer;
2795 entry = __trace_buffer_lock_reserve(*current_rb,
2796 type, len, flags, pc);
2797 }
2798 return entry;
2799}
2800EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2801
Olivier Deprez92d4c212022-12-06 15:05:30 +01002802static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002803static DEFINE_MUTEX(tracepoint_printk_mutex);
2804
2805static void output_printk(struct trace_event_buffer *fbuffer)
2806{
2807 struct trace_event_call *event_call;
Olivier Deprez157378f2022-04-04 15:47:50 +02002808 struct trace_event_file *file;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002809 struct trace_event *event;
2810 unsigned long flags;
2811 struct trace_iterator *iter = tracepoint_print_iter;
2812
2813 /* We should never get here if iter is NULL */
2814 if (WARN_ON_ONCE(!iter))
2815 return;
2816
2817 event_call = fbuffer->trace_file->event_call;
2818 if (!event_call || !event_call->event.funcs ||
2819 !event_call->event.funcs->trace)
2820 return;
2821
Olivier Deprez157378f2022-04-04 15:47:50 +02002822 file = fbuffer->trace_file;
2823 if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2824 (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2825 !filter_match_preds(file->filter, fbuffer->entry)))
2826 return;
2827
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002828 event = &fbuffer->trace_file->event_call->event;
2829
Olivier Deprez92d4c212022-12-06 15:05:30 +01002830 raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002831 trace_seq_init(&iter->seq);
2832 iter->ent = fbuffer->entry;
2833 event_call->event.funcs->trace(iter, 0, event);
2834 trace_seq_putc(&iter->seq, 0);
2835 printk("%s", iter->seq.buffer);
2836
Olivier Deprez92d4c212022-12-06 15:05:30 +01002837 raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002838}
2839
2840int tracepoint_printk_sysctl(struct ctl_table *table, int write,
Olivier Deprez157378f2022-04-04 15:47:50 +02002841 void *buffer, size_t *lenp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002842 loff_t *ppos)
2843{
2844 int save_tracepoint_printk;
2845 int ret;
2846
2847 mutex_lock(&tracepoint_printk_mutex);
2848 save_tracepoint_printk = tracepoint_printk;
2849
2850 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2851
2852 /*
2853 * This will force exiting early, as tracepoint_printk
2854 * is always zero when tracepoint_printk_iter is not allocated
2855 */
2856 if (!tracepoint_print_iter)
2857 tracepoint_printk = 0;
2858
2859 if (save_tracepoint_printk == tracepoint_printk)
2860 goto out;
2861
2862 if (tracepoint_printk)
2863 static_key_enable(&tracepoint_printk_key.key);
2864 else
2865 static_key_disable(&tracepoint_printk_key.key);
2866
2867 out:
2868 mutex_unlock(&tracepoint_printk_mutex);
2869
2870 return ret;
2871}
2872
2873void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2874{
2875 if (static_key_false(&tracepoint_printk_key.key))
2876 output_printk(fbuffer);
2877
Olivier Deprez157378f2022-04-04 15:47:50 +02002878 if (static_branch_unlikely(&trace_event_exports_enabled))
2879 ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
2880 event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002881 fbuffer->event, fbuffer->entry,
Olivier Deprez157378f2022-04-04 15:47:50 +02002882 fbuffer->flags, fbuffer->pc, fbuffer->regs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002883}
2884EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2885
2886/*
2887 * Skip 3:
2888 *
2889 * trace_buffer_unlock_commit_regs()
2890 * trace_event_buffer_commit()
2891 * trace_event_raw_event_xxx()
2892 */
2893# define STACK_SKIP 3
2894
2895void trace_buffer_unlock_commit_regs(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +02002896 struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002897 struct ring_buffer_event *event,
2898 unsigned long flags, int pc,
2899 struct pt_regs *regs)
2900{
2901 __buffer_unlock_commit(buffer, event);
2902
2903 /*
2904 * If regs is not set, then skip the necessary functions.
2905 * Note, we can still get here via blktrace, wakeup tracer
2906 * and mmiotrace, but that's ok if they lose a function or
2907 * two. They are not that meaningful.
2908 */
2909 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Olivier Deprez0e641232021-09-23 10:07:05 +02002910 ftrace_trace_userstack(tr, buffer, flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002911}
2912
2913/*
2914 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2915 */
2916void
Olivier Deprez157378f2022-04-04 15:47:50 +02002917trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002918 struct ring_buffer_event *event)
2919{
2920 __buffer_unlock_commit(buffer, event);
2921}
2922
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002923void
2924trace_function(struct trace_array *tr,
2925 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2926 int pc)
2927{
2928 struct trace_event_call *call = &event_function;
Olivier Deprez157378f2022-04-04 15:47:50 +02002929 struct trace_buffer *buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002930 struct ring_buffer_event *event;
2931 struct ftrace_entry *entry;
2932
2933 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2934 flags, pc);
2935 if (!event)
2936 return;
2937 entry = ring_buffer_event_data(event);
2938 entry->ip = ip;
2939 entry->parent_ip = parent_ip;
2940
2941 if (!call_filter_check_discard(call, entry, buffer, event)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002942 if (static_branch_unlikely(&trace_function_exports_enabled))
2943 ftrace_exports(event, TRACE_EXPORT_FUNCTION);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002944 __buffer_unlock_commit(buffer, event);
2945 }
2946}
2947
2948#ifdef CONFIG_STACKTRACE
2949
David Brazdil0f672f62019-12-10 10:32:29 +00002950/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2951#define FTRACE_KSTACK_NESTING 4
2952
2953#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2954
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002955struct ftrace_stack {
David Brazdil0f672f62019-12-10 10:32:29 +00002956 unsigned long calls[FTRACE_KSTACK_ENTRIES];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002957};
2958
David Brazdil0f672f62019-12-10 10:32:29 +00002959
2960struct ftrace_stacks {
2961 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2962};
2963
2964static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002965static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2966
Olivier Deprez157378f2022-04-04 15:47:50 +02002967static void __ftrace_trace_stack(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002968 unsigned long flags,
2969 int skip, int pc, struct pt_regs *regs)
2970{
2971 struct trace_event_call *call = &event_kernel_stack;
2972 struct ring_buffer_event *event;
David Brazdil0f672f62019-12-10 10:32:29 +00002973 unsigned int size, nr_entries;
2974 struct ftrace_stack *fstack;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002975 struct stack_entry *entry;
David Brazdil0f672f62019-12-10 10:32:29 +00002976 int stackidx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002977
2978 /*
2979 * Add one, for this function and the call to save_stack_trace()
2980 * If regs is set, then these functions will not be in the way.
2981 */
2982#ifndef CONFIG_UNWINDER_ORC
2983 if (!regs)
David Brazdil0f672f62019-12-10 10:32:29 +00002984 skip++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002985#endif
2986
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002987 preempt_disable_notrace();
2988
David Brazdil0f672f62019-12-10 10:32:29 +00002989 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2990
2991 /* This should never happen. If it does, yell once and skip */
Olivier Deprez0e641232021-09-23 10:07:05 +02002992 if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
David Brazdil0f672f62019-12-10 10:32:29 +00002993 goto out;
2994
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002995 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002996 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2997 * interrupt will either see the value pre increment or post
2998 * increment. If the interrupt happens pre increment it will have
2999 * restored the counter when it returns. We just need a barrier to
3000 * keep gcc from moving things around.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003001 */
3002 barrier();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003003
David Brazdil0f672f62019-12-10 10:32:29 +00003004 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
3005 size = ARRAY_SIZE(fstack->calls);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003006
David Brazdil0f672f62019-12-10 10:32:29 +00003007 if (regs) {
3008 nr_entries = stack_trace_save_regs(regs, fstack->calls,
3009 size, skip);
3010 } else {
3011 nr_entries = stack_trace_save(fstack->calls, size, skip);
3012 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003013
David Brazdil0f672f62019-12-10 10:32:29 +00003014 size = nr_entries * sizeof(unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003015 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
Olivier Deprez0e641232021-09-23 10:07:05 +02003016 (sizeof(*entry) - sizeof(entry->caller)) + size,
3017 flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003018 if (!event)
3019 goto out;
3020 entry = ring_buffer_event_data(event);
3021
David Brazdil0f672f62019-12-10 10:32:29 +00003022 memcpy(&entry->caller, fstack->calls, size);
3023 entry->size = nr_entries;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003024
3025 if (!call_filter_check_discard(call, entry, buffer, event))
3026 __buffer_unlock_commit(buffer, event);
3027
3028 out:
3029 /* Again, don't let gcc optimize things here */
3030 barrier();
3031 __this_cpu_dec(ftrace_stack_reserve);
3032 preempt_enable_notrace();
3033
3034}
3035
3036static inline void ftrace_trace_stack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +02003037 struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003038 unsigned long flags,
3039 int skip, int pc, struct pt_regs *regs)
3040{
3041 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
3042 return;
3043
3044 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
3045}
3046
3047void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
3048 int pc)
3049{
Olivier Deprez157378f2022-04-04 15:47:50 +02003050 struct trace_buffer *buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003051
3052 if (rcu_is_watching()) {
3053 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
3054 return;
3055 }
3056
3057 /*
3058 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
3059 * but if the above rcu_is_watching() failed, then the NMI
3060 * triggered someplace critical, and rcu_irq_enter() should
3061 * not be called from NMI.
3062 */
3063 if (unlikely(in_nmi()))
3064 return;
3065
3066 rcu_irq_enter_irqson();
3067 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
3068 rcu_irq_exit_irqson();
3069}
3070
3071/**
3072 * trace_dump_stack - record a stack back trace in the trace buffer
3073 * @skip: Number of functions to skip (helper handlers)
3074 */
3075void trace_dump_stack(int skip)
3076{
3077 unsigned long flags;
3078
3079 if (tracing_disabled || tracing_selftest_running)
3080 return;
3081
3082 local_save_flags(flags);
3083
3084#ifndef CONFIG_UNWINDER_ORC
3085 /* Skip 1 to skip this function. */
3086 skip++;
3087#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02003088 __ftrace_trace_stack(global_trace.array_buffer.buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003089 flags, skip, preempt_count(), NULL);
3090}
David Brazdil0f672f62019-12-10 10:32:29 +00003091EXPORT_SYMBOL_GPL(trace_dump_stack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003092
David Brazdil0f672f62019-12-10 10:32:29 +00003093#ifdef CONFIG_USER_STACKTRACE_SUPPORT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003094static DEFINE_PER_CPU(int, user_stack_count);
3095
David Brazdil0f672f62019-12-10 10:32:29 +00003096static void
Olivier Deprez0e641232021-09-23 10:07:05 +02003097ftrace_trace_userstack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +02003098 struct trace_buffer *buffer, unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003099{
3100 struct trace_event_call *call = &event_user_stack;
3101 struct ring_buffer_event *event;
3102 struct userstack_entry *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003103
Olivier Deprez0e641232021-09-23 10:07:05 +02003104 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003105 return;
3106
3107 /*
3108 * NMIs can not handle page faults, even with fix ups.
3109 * The save user stack can (and often does) fault.
3110 */
3111 if (unlikely(in_nmi()))
3112 return;
3113
3114 /*
3115 * prevent recursion, since the user stack tracing may
3116 * trigger other kernel events.
3117 */
3118 preempt_disable();
3119 if (__this_cpu_read(user_stack_count))
3120 goto out;
3121
3122 __this_cpu_inc(user_stack_count);
3123
3124 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
3125 sizeof(*entry), flags, pc);
3126 if (!event)
3127 goto out_drop_count;
3128 entry = ring_buffer_event_data(event);
3129
3130 entry->tgid = current->tgid;
3131 memset(&entry->caller, 0, sizeof(entry->caller));
3132
David Brazdil0f672f62019-12-10 10:32:29 +00003133 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003134 if (!call_filter_check_discard(call, entry, buffer, event))
3135 __buffer_unlock_commit(buffer, event);
3136
3137 out_drop_count:
3138 __this_cpu_dec(user_stack_count);
3139 out:
3140 preempt_enable();
3141}
David Brazdil0f672f62019-12-10 10:32:29 +00003142#else /* CONFIG_USER_STACKTRACE_SUPPORT */
Olivier Deprez0e641232021-09-23 10:07:05 +02003143static void ftrace_trace_userstack(struct trace_array *tr,
Olivier Deprez157378f2022-04-04 15:47:50 +02003144 struct trace_buffer *buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00003145 unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003146{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003147}
David Brazdil0f672f62019-12-10 10:32:29 +00003148#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003149
3150#endif /* CONFIG_STACKTRACE */
3151
3152/* created for use with alloc_percpu */
3153struct trace_buffer_struct {
3154 int nesting;
3155 char buffer[4][TRACE_BUF_SIZE];
3156};
3157
Olivier Deprez157378f2022-04-04 15:47:50 +02003158static struct trace_buffer_struct __percpu *trace_percpu_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003159
3160/*
3161 * Thise allows for lockless recording. If we're nested too deeply, then
3162 * this returns NULL.
3163 */
3164static char *get_trace_buf(void)
3165{
3166 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
3167
Olivier Deprez157378f2022-04-04 15:47:50 +02003168 if (!trace_percpu_buffer || buffer->nesting >= 4)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003169 return NULL;
3170
3171 buffer->nesting++;
3172
3173 /* Interrupts must see nesting incremented before we use the buffer */
3174 barrier();
Olivier Deprez0e641232021-09-23 10:07:05 +02003175 return &buffer->buffer[buffer->nesting - 1][0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003176}
3177
3178static void put_trace_buf(void)
3179{
3180 /* Don't let the decrement of nesting leak before this */
3181 barrier();
3182 this_cpu_dec(trace_percpu_buffer->nesting);
3183}
3184
3185static int alloc_percpu_trace_buffer(void)
3186{
Olivier Deprez157378f2022-04-04 15:47:50 +02003187 struct trace_buffer_struct __percpu *buffers;
3188
3189 if (trace_percpu_buffer)
3190 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003191
3192 buffers = alloc_percpu(struct trace_buffer_struct);
Olivier Deprez157378f2022-04-04 15:47:50 +02003193 if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003194 return -ENOMEM;
3195
3196 trace_percpu_buffer = buffers;
3197 return 0;
3198}
3199
3200static int buffers_allocated;
3201
3202void trace_printk_init_buffers(void)
3203{
3204 if (buffers_allocated)
3205 return;
3206
3207 if (alloc_percpu_trace_buffer())
3208 return;
3209
3210 /* trace_printk() is for debug use only. Don't use it in production. */
3211
3212 pr_warn("\n");
3213 pr_warn("**********************************************************\n");
3214 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3215 pr_warn("** **\n");
3216 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3217 pr_warn("** **\n");
3218 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3219 pr_warn("** unsafe for production use. **\n");
3220 pr_warn("** **\n");
3221 pr_warn("** If you see this message and you are not debugging **\n");
3222 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3223 pr_warn("** **\n");
3224 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3225 pr_warn("**********************************************************\n");
3226
3227 /* Expand the buffers to set size */
3228 tracing_update_buffers();
3229
3230 buffers_allocated = 1;
3231
3232 /*
3233 * trace_printk_init_buffers() can be called by modules.
3234 * If that happens, then we need to start cmdline recording
3235 * directly here. If the global_trace.buffer is already
3236 * allocated here, then this was called by module code.
3237 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003238 if (global_trace.array_buffer.buffer)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003239 tracing_start_cmdline_record();
3240}
David Brazdil0f672f62019-12-10 10:32:29 +00003241EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003242
3243void trace_printk_start_comm(void)
3244{
3245 /* Start tracing comms if trace printk is set */
3246 if (!buffers_allocated)
3247 return;
3248 tracing_start_cmdline_record();
3249}
3250
3251static void trace_printk_start_stop_comm(int enabled)
3252{
3253 if (!buffers_allocated)
3254 return;
3255
3256 if (enabled)
3257 tracing_start_cmdline_record();
3258 else
3259 tracing_stop_cmdline_record();
3260}
3261
3262/**
3263 * trace_vbprintk - write binary msg to tracing buffer
David Brazdil0f672f62019-12-10 10:32:29 +00003264 * @ip: The address of the caller
3265 * @fmt: The string format to write to the buffer
3266 * @args: Arguments for @fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003267 */
3268int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
3269{
3270 struct trace_event_call *call = &event_bprint;
3271 struct ring_buffer_event *event;
Olivier Deprez157378f2022-04-04 15:47:50 +02003272 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003273 struct trace_array *tr = &global_trace;
3274 struct bprint_entry *entry;
3275 unsigned long flags;
3276 char *tbuffer;
3277 int len = 0, size, pc;
3278
3279 if (unlikely(tracing_selftest_running || tracing_disabled))
3280 return 0;
3281
3282 /* Don't pollute graph traces with trace_vprintk internals */
3283 pause_graph_tracing();
3284
3285 pc = preempt_count();
3286 preempt_disable_notrace();
3287
3288 tbuffer = get_trace_buf();
3289 if (!tbuffer) {
3290 len = 0;
3291 goto out_nobuffer;
3292 }
3293
3294 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3295
3296 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02003297 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003298
3299 local_save_flags(flags);
3300 size = sizeof(*entry) + sizeof(u32) * len;
Olivier Deprez157378f2022-04-04 15:47:50 +02003301 buffer = tr->array_buffer.buffer;
3302 ring_buffer_nest_start(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003303 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3304 flags, pc);
3305 if (!event)
3306 goto out;
3307 entry = ring_buffer_event_data(event);
3308 entry->ip = ip;
3309 entry->fmt = fmt;
3310
3311 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
3312 if (!call_filter_check_discard(call, entry, buffer, event)) {
3313 __buffer_unlock_commit(buffer, event);
3314 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
3315 }
3316
3317out:
Olivier Deprez157378f2022-04-04 15:47:50 +02003318 ring_buffer_nest_end(buffer);
3319out_put:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003320 put_trace_buf();
3321
3322out_nobuffer:
3323 preempt_enable_notrace();
3324 unpause_graph_tracing();
3325
3326 return len;
3327}
3328EXPORT_SYMBOL_GPL(trace_vbprintk);
3329
3330__printf(3, 0)
3331static int
Olivier Deprez157378f2022-04-04 15:47:50 +02003332__trace_array_vprintk(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003333 unsigned long ip, const char *fmt, va_list args)
3334{
3335 struct trace_event_call *call = &event_print;
3336 struct ring_buffer_event *event;
3337 int len = 0, size, pc;
3338 struct print_entry *entry;
3339 unsigned long flags;
3340 char *tbuffer;
3341
3342 if (tracing_disabled || tracing_selftest_running)
3343 return 0;
3344
3345 /* Don't pollute graph traces with trace_vprintk internals */
3346 pause_graph_tracing();
3347
3348 pc = preempt_count();
3349 preempt_disable_notrace();
3350
3351
3352 tbuffer = get_trace_buf();
3353 if (!tbuffer) {
3354 len = 0;
3355 goto out_nobuffer;
3356 }
3357
3358 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
3359
3360 local_save_flags(flags);
3361 size = sizeof(*entry) + len + 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02003362 ring_buffer_nest_start(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003363 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3364 flags, pc);
3365 if (!event)
3366 goto out;
3367 entry = ring_buffer_event_data(event);
3368 entry->ip = ip;
3369
3370 memcpy(&entry->buf, tbuffer, len + 1);
3371 if (!call_filter_check_discard(call, entry, buffer, event)) {
3372 __buffer_unlock_commit(buffer, event);
3373 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
3374 }
3375
3376out:
Olivier Deprez157378f2022-04-04 15:47:50 +02003377 ring_buffer_nest_end(buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003378 put_trace_buf();
3379
3380out_nobuffer:
3381 preempt_enable_notrace();
3382 unpause_graph_tracing();
3383
3384 return len;
3385}
3386
3387__printf(3, 0)
3388int trace_array_vprintk(struct trace_array *tr,
3389 unsigned long ip, const char *fmt, va_list args)
3390{
Olivier Deprez157378f2022-04-04 15:47:50 +02003391 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003392}
3393
Olivier Deprez157378f2022-04-04 15:47:50 +02003394/**
3395 * trace_array_printk - Print a message to a specific instance
3396 * @tr: The instance trace_array descriptor
3397 * @ip: The instruction pointer that this is called from.
3398 * @fmt: The format to print (printf format)
3399 *
3400 * If a subsystem sets up its own instance, they have the right to
3401 * printk strings into their tracing instance buffer using this
3402 * function. Note, this function will not write into the top level
3403 * buffer (use trace_printk() for that), as writing into the top level
3404 * buffer should only have events that can be individually disabled.
3405 * trace_printk() is only used for debugging a kernel, and should not
3406 * be ever encorporated in normal use.
3407 *
3408 * trace_array_printk() can be used, as it will not add noise to the
3409 * top level tracing buffer.
3410 *
3411 * Note, trace_array_init_printk() must be called on @tr before this
3412 * can be used.
3413 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003414__printf(3, 0)
3415int trace_array_printk(struct trace_array *tr,
3416 unsigned long ip, const char *fmt, ...)
3417{
3418 int ret;
3419 va_list ap;
3420
Olivier Deprez0e641232021-09-23 10:07:05 +02003421 if (!tr)
3422 return -ENOENT;
3423
Olivier Deprez157378f2022-04-04 15:47:50 +02003424 /* This is only allowed for created instances */
3425 if (tr == &global_trace)
3426 return 0;
3427
3428 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3429 return 0;
3430
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003431 va_start(ap, fmt);
3432 ret = trace_array_vprintk(tr, ip, fmt, ap);
3433 va_end(ap);
3434 return ret;
3435}
David Brazdil0f672f62019-12-10 10:32:29 +00003436EXPORT_SYMBOL_GPL(trace_array_printk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003437
Olivier Deprez157378f2022-04-04 15:47:50 +02003438/**
3439 * trace_array_init_printk - Initialize buffers for trace_array_printk()
3440 * @tr: The trace array to initialize the buffers for
3441 *
3442 * As trace_array_printk() only writes into instances, they are OK to
3443 * have in the kernel (unlike trace_printk()). This needs to be called
3444 * before trace_array_printk() can be used on a trace_array.
3445 */
3446int trace_array_init_printk(struct trace_array *tr)
3447{
3448 if (!tr)
3449 return -ENOENT;
3450
3451 /* This is only allowed for created instances */
3452 if (tr == &global_trace)
3453 return -EINVAL;
3454
3455 return alloc_percpu_trace_buffer();
3456}
3457EXPORT_SYMBOL_GPL(trace_array_init_printk);
3458
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003459__printf(3, 4)
Olivier Deprez157378f2022-04-04 15:47:50 +02003460int trace_array_printk_buf(struct trace_buffer *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003461 unsigned long ip, const char *fmt, ...)
3462{
3463 int ret;
3464 va_list ap;
3465
3466 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3467 return 0;
3468
3469 va_start(ap, fmt);
3470 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3471 va_end(ap);
3472 return ret;
3473}
3474
3475__printf(2, 0)
3476int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3477{
3478 return trace_array_vprintk(&global_trace, ip, fmt, args);
3479}
3480EXPORT_SYMBOL_GPL(trace_vprintk);
3481
3482static void trace_iterator_increment(struct trace_iterator *iter)
3483{
3484 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3485
3486 iter->idx++;
3487 if (buf_iter)
Olivier Deprez157378f2022-04-04 15:47:50 +02003488 ring_buffer_iter_advance(buf_iter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003489}
3490
3491static struct trace_entry *
3492peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3493 unsigned long *lost_events)
3494{
3495 struct ring_buffer_event *event;
3496 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3497
Olivier Deprez157378f2022-04-04 15:47:50 +02003498 if (buf_iter) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003499 event = ring_buffer_iter_peek(buf_iter, ts);
Olivier Deprez157378f2022-04-04 15:47:50 +02003500 if (lost_events)
3501 *lost_events = ring_buffer_iter_dropped(buf_iter) ?
3502 (unsigned long)-1 : 0;
3503 } else {
3504 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003505 lost_events);
Olivier Deprez157378f2022-04-04 15:47:50 +02003506 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003507
3508 if (event) {
3509 iter->ent_size = ring_buffer_event_length(event);
3510 return ring_buffer_event_data(event);
3511 }
3512 iter->ent_size = 0;
3513 return NULL;
3514}
3515
3516static struct trace_entry *
3517__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3518 unsigned long *missing_events, u64 *ent_ts)
3519{
Olivier Deprez157378f2022-04-04 15:47:50 +02003520 struct trace_buffer *buffer = iter->array_buffer->buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003521 struct trace_entry *ent, *next = NULL;
3522 unsigned long lost_events = 0, next_lost = 0;
3523 int cpu_file = iter->cpu_file;
3524 u64 next_ts = 0, ts;
3525 int next_cpu = -1;
3526 int next_size = 0;
3527 int cpu;
3528
3529 /*
3530 * If we are in a per_cpu trace file, don't bother by iterating over
3531 * all cpu and peek directly.
3532 */
3533 if (cpu_file > RING_BUFFER_ALL_CPUS) {
3534 if (ring_buffer_empty_cpu(buffer, cpu_file))
3535 return NULL;
3536 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3537 if (ent_cpu)
3538 *ent_cpu = cpu_file;
3539
3540 return ent;
3541 }
3542
3543 for_each_tracing_cpu(cpu) {
3544
3545 if (ring_buffer_empty_cpu(buffer, cpu))
3546 continue;
3547
3548 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3549
3550 /*
3551 * Pick the entry with the smallest timestamp:
3552 */
3553 if (ent && (!next || ts < next_ts)) {
3554 next = ent;
3555 next_cpu = cpu;
3556 next_ts = ts;
3557 next_lost = lost_events;
3558 next_size = iter->ent_size;
3559 }
3560 }
3561
3562 iter->ent_size = next_size;
3563
3564 if (ent_cpu)
3565 *ent_cpu = next_cpu;
3566
3567 if (ent_ts)
3568 *ent_ts = next_ts;
3569
3570 if (missing_events)
3571 *missing_events = next_lost;
3572
3573 return next;
3574}
3575
Olivier Deprez157378f2022-04-04 15:47:50 +02003576#define STATIC_TEMP_BUF_SIZE 128
3577static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
3578
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003579/* Find the next real entry, without updating the iterator itself */
3580struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3581 int *ent_cpu, u64 *ent_ts)
3582{
Olivier Deprez157378f2022-04-04 15:47:50 +02003583 /* __find_next_entry will reset ent_size */
3584 int ent_size = iter->ent_size;
3585 struct trace_entry *entry;
3586
3587 /*
3588 * If called from ftrace_dump(), then the iter->temp buffer
3589 * will be the static_temp_buf and not created from kmalloc.
3590 * If the entry size is greater than the buffer, we can
3591 * not save it. Just return NULL in that case. This is only
3592 * used to add markers when two consecutive events' time
3593 * stamps have a large delta. See trace_print_lat_context()
3594 */
3595 if (iter->temp == static_temp_buf &&
3596 STATIC_TEMP_BUF_SIZE < ent_size)
3597 return NULL;
3598
3599 /*
3600 * The __find_next_entry() may call peek_next_entry(), which may
3601 * call ring_buffer_peek() that may make the contents of iter->ent
3602 * undefined. Need to copy iter->ent now.
3603 */
3604 if (iter->ent && iter->ent != iter->temp) {
3605 if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3606 !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
3607 void *temp;
3608 temp = kmalloc(iter->ent_size, GFP_KERNEL);
3609 if (!temp)
3610 return NULL;
3611 kfree(iter->temp);
3612 iter->temp = temp;
3613 iter->temp_size = iter->ent_size;
3614 }
3615 memcpy(iter->temp, iter->ent, iter->ent_size);
3616 iter->ent = iter->temp;
3617 }
3618 entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3619 /* Put back the original ent_size */
3620 iter->ent_size = ent_size;
3621
3622 return entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003623}
3624
3625/* Find the next real entry, and increment the iterator to the next entry */
3626void *trace_find_next_entry_inc(struct trace_iterator *iter)
3627{
3628 iter->ent = __find_next_entry(iter, &iter->cpu,
3629 &iter->lost_events, &iter->ts);
3630
3631 if (iter->ent)
3632 trace_iterator_increment(iter);
3633
3634 return iter->ent ? iter : NULL;
3635}
3636
3637static void trace_consume(struct trace_iterator *iter)
3638{
Olivier Deprez157378f2022-04-04 15:47:50 +02003639 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003640 &iter->lost_events);
3641}
3642
3643static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3644{
3645 struct trace_iterator *iter = m->private;
3646 int i = (int)*pos;
3647 void *ent;
3648
3649 WARN_ON_ONCE(iter->leftover);
3650
3651 (*pos)++;
3652
3653 /* can't go backwards */
3654 if (iter->idx > i)
3655 return NULL;
3656
3657 if (iter->idx < 0)
3658 ent = trace_find_next_entry_inc(iter);
3659 else
3660 ent = iter;
3661
3662 while (ent && iter->idx < i)
3663 ent = trace_find_next_entry_inc(iter);
3664
3665 iter->pos = *pos;
3666
3667 return ent;
3668}
3669
3670void tracing_iter_reset(struct trace_iterator *iter, int cpu)
3671{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003672 struct ring_buffer_iter *buf_iter;
3673 unsigned long entries = 0;
3674 u64 ts;
3675
Olivier Deprez157378f2022-04-04 15:47:50 +02003676 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003677
3678 buf_iter = trace_buffer_iter(iter, cpu);
3679 if (!buf_iter)
3680 return;
3681
3682 ring_buffer_iter_reset(buf_iter);
3683
3684 /*
3685 * We could have the case with the max latency tracers
3686 * that a reset never took place on a cpu. This is evident
3687 * by the timestamp being before the start of the buffer.
3688 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003689 while (ring_buffer_iter_peek(buf_iter, &ts)) {
3690 if (ts >= iter->array_buffer->time_start)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003691 break;
3692 entries++;
Olivier Deprez157378f2022-04-04 15:47:50 +02003693 ring_buffer_iter_advance(buf_iter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003694 }
3695
Olivier Deprez157378f2022-04-04 15:47:50 +02003696 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003697}
3698
3699/*
3700 * The current tracer is copied to avoid a global locking
3701 * all around.
3702 */
3703static void *s_start(struct seq_file *m, loff_t *pos)
3704{
3705 struct trace_iterator *iter = m->private;
3706 struct trace_array *tr = iter->tr;
3707 int cpu_file = iter->cpu_file;
3708 void *p = NULL;
3709 loff_t l = 0;
3710 int cpu;
3711
3712 /*
3713 * copy the tracer to avoid using a global lock all around.
3714 * iter->trace is a copy of current_trace, the pointer to the
3715 * name may be used instead of a strcmp(), as iter->trace->name
3716 * will point to the same string as current_trace->name.
3717 */
3718 mutex_lock(&trace_types_lock);
3719 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3720 *iter->trace = *tr->current_trace;
3721 mutex_unlock(&trace_types_lock);
3722
3723#ifdef CONFIG_TRACER_MAX_TRACE
3724 if (iter->snapshot && iter->trace->use_max_tr)
3725 return ERR_PTR(-EBUSY);
3726#endif
3727
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003728 if (*pos != iter->pos) {
3729 iter->ent = NULL;
3730 iter->cpu = 0;
3731 iter->idx = -1;
3732
3733 if (cpu_file == RING_BUFFER_ALL_CPUS) {
3734 for_each_tracing_cpu(cpu)
3735 tracing_iter_reset(iter, cpu);
3736 } else
3737 tracing_iter_reset(iter, cpu_file);
3738
3739 iter->leftover = 0;
3740 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3741 ;
3742
3743 } else {
3744 /*
3745 * If we overflowed the seq_file before, then we want
3746 * to just reuse the trace_seq buffer again.
3747 */
3748 if (iter->leftover)
3749 p = iter;
3750 else {
3751 l = *pos - 1;
3752 p = s_next(m, p, &l);
3753 }
3754 }
3755
3756 trace_event_read_lock();
3757 trace_access_lock(cpu_file);
3758 return p;
3759}
3760
3761static void s_stop(struct seq_file *m, void *p)
3762{
3763 struct trace_iterator *iter = m->private;
3764
3765#ifdef CONFIG_TRACER_MAX_TRACE
3766 if (iter->snapshot && iter->trace->use_max_tr)
3767 return;
3768#endif
3769
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003770 trace_access_unlock(iter->cpu_file);
3771 trace_event_read_unlock();
3772}
3773
3774static void
Olivier Deprez157378f2022-04-04 15:47:50 +02003775get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
David Brazdil0f672f62019-12-10 10:32:29 +00003776 unsigned long *entries, int cpu)
3777{
3778 unsigned long count;
3779
3780 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3781 /*
3782 * If this buffer has skipped entries, then we hold all
3783 * entries for the trace and we need to ignore the
3784 * ones before the time stamp.
3785 */
3786 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3787 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3788 /* total is the same as the entries */
3789 *total = count;
3790 } else
3791 *total = count +
3792 ring_buffer_overrun_cpu(buf->buffer, cpu);
3793 *entries = count;
3794}
3795
3796static void
Olivier Deprez157378f2022-04-04 15:47:50 +02003797get_total_entries(struct array_buffer *buf,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003798 unsigned long *total, unsigned long *entries)
3799{
David Brazdil0f672f62019-12-10 10:32:29 +00003800 unsigned long t, e;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003801 int cpu;
3802
3803 *total = 0;
3804 *entries = 0;
3805
3806 for_each_tracing_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +00003807 get_total_entries_cpu(buf, &t, &e, cpu);
3808 *total += t;
3809 *entries += e;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003810 }
3811}
3812
David Brazdil0f672f62019-12-10 10:32:29 +00003813unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3814{
3815 unsigned long total, entries;
3816
3817 if (!tr)
3818 tr = &global_trace;
3819
Olivier Deprez157378f2022-04-04 15:47:50 +02003820 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
David Brazdil0f672f62019-12-10 10:32:29 +00003821
3822 return entries;
3823}
3824
3825unsigned long trace_total_entries(struct trace_array *tr)
3826{
3827 unsigned long total, entries;
3828
3829 if (!tr)
3830 tr = &global_trace;
3831
Olivier Deprez157378f2022-04-04 15:47:50 +02003832 get_total_entries(&tr->array_buffer, &total, &entries);
David Brazdil0f672f62019-12-10 10:32:29 +00003833
3834 return entries;
3835}
3836
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003837static void print_lat_help_header(struct seq_file *m)
3838{
Olivier Deprez0e641232021-09-23 10:07:05 +02003839 seq_puts(m, "# _------=> CPU# \n"
3840 "# / _-----=> irqs-off \n"
3841 "# | / _----=> need-resched \n"
3842 "# || / _---=> hardirq/softirq \n"
3843 "# ||| / _--=> preempt-depth \n"
3844 "# |||| / delay \n"
3845 "# cmd pid ||||| time | caller \n"
3846 "# \\ / ||||| \\ | / \n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003847}
3848
Olivier Deprez157378f2022-04-04 15:47:50 +02003849static void print_event_info(struct array_buffer *buf, struct seq_file *m)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003850{
3851 unsigned long total;
3852 unsigned long entries;
3853
3854 get_total_entries(buf, &total, &entries);
3855 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3856 entries, total, num_online_cpus());
3857 seq_puts(m, "#\n");
3858}
3859
Olivier Deprez157378f2022-04-04 15:47:50 +02003860static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003861 unsigned int flags)
3862{
3863 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3864
3865 print_event_info(buf, m);
3866
Olivier Deprez0e641232021-09-23 10:07:05 +02003867 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
3868 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003869}
3870
Olivier Deprez157378f2022-04-04 15:47:50 +02003871static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003872 unsigned int flags)
3873{
3874 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Olivier Deprez0e641232021-09-23 10:07:05 +02003875 const char *space = " ";
3876 int prec = tgid ? 12 : 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003877
David Brazdil0f672f62019-12-10 10:32:29 +00003878 print_event_info(buf, m);
3879
Olivier Deprez0e641232021-09-23 10:07:05 +02003880 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3881 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3882 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3883 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3884 seq_printf(m, "# %.*s||| / delay\n", prec, space);
3885 seq_printf(m, "# TASK-PID %.*s CPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3886 seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003887}
3888
3889void
3890print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3891{
3892 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Olivier Deprez157378f2022-04-04 15:47:50 +02003893 struct array_buffer *buf = iter->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003894 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
3895 struct tracer *type = iter->trace;
3896 unsigned long entries;
3897 unsigned long total;
3898 const char *name = "preemption";
3899
3900 name = type->name;
3901
3902 get_total_entries(buf, &total, &entries);
3903
3904 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
3905 name, UTS_RELEASE);
3906 seq_puts(m, "# -----------------------------------"
3907 "---------------------------------\n");
3908 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
3909 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
3910 nsecs_to_usecs(data->saved_latency),
3911 entries,
3912 total,
3913 buf->cpu,
3914#if defined(CONFIG_PREEMPT_NONE)
3915 "server",
3916#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3917 "desktop",
3918#elif defined(CONFIG_PREEMPT)
3919 "preempt",
Olivier Deprez157378f2022-04-04 15:47:50 +02003920#elif defined(CONFIG_PREEMPT_RT)
3921 "preempt_rt",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003922#else
3923 "unknown",
3924#endif
3925 /* These are reserved for later use */
3926 0, 0, 0, 0);
3927#ifdef CONFIG_SMP
3928 seq_printf(m, " #P:%d)\n", num_online_cpus());
3929#else
3930 seq_puts(m, ")\n");
3931#endif
3932 seq_puts(m, "# -----------------\n");
3933 seq_printf(m, "# | task: %.16s-%d "
3934 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
3935 data->comm, data->pid,
3936 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
3937 data->policy, data->rt_priority);
3938 seq_puts(m, "# -----------------\n");
3939
3940 if (data->critical_start) {
3941 seq_puts(m, "# => started at: ");
3942 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3943 trace_print_seq(m, &iter->seq);
3944 seq_puts(m, "\n# => ended at: ");
3945 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3946 trace_print_seq(m, &iter->seq);
3947 seq_puts(m, "\n#\n");
3948 }
3949
3950 seq_puts(m, "#\n");
3951}
3952
3953static void test_cpu_buff_start(struct trace_iterator *iter)
3954{
3955 struct trace_seq *s = &iter->seq;
3956 struct trace_array *tr = iter->tr;
3957
3958 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
3959 return;
3960
3961 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3962 return;
3963
3964 if (cpumask_available(iter->started) &&
3965 cpumask_test_cpu(iter->cpu, iter->started))
3966 return;
3967
Olivier Deprez157378f2022-04-04 15:47:50 +02003968 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003969 return;
3970
3971 if (cpumask_available(iter->started))
3972 cpumask_set_cpu(iter->cpu, iter->started);
3973
3974 /* Don't print started cpu buffer for the first entry of the trace */
3975 if (iter->idx > 1)
3976 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3977 iter->cpu);
3978}
3979
3980static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
3981{
3982 struct trace_array *tr = iter->tr;
3983 struct trace_seq *s = &iter->seq;
3984 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
3985 struct trace_entry *entry;
3986 struct trace_event *event;
3987
3988 entry = iter->ent;
3989
3990 test_cpu_buff_start(iter);
3991
3992 event = ftrace_find_event(entry->type);
3993
3994 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3995 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3996 trace_print_lat_context(iter);
3997 else
3998 trace_print_context(iter);
3999 }
4000
4001 if (trace_seq_has_overflowed(s))
4002 return TRACE_TYPE_PARTIAL_LINE;
4003
4004 if (event)
4005 return event->funcs->trace(iter, sym_flags, event);
4006
4007 trace_seq_printf(s, "Unknown type %d\n", entry->type);
4008
4009 return trace_handle_return(s);
4010}
4011
4012static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
4013{
4014 struct trace_array *tr = iter->tr;
4015 struct trace_seq *s = &iter->seq;
4016 struct trace_entry *entry;
4017 struct trace_event *event;
4018
4019 entry = iter->ent;
4020
4021 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
4022 trace_seq_printf(s, "%d %d %llu ",
4023 entry->pid, iter->cpu, iter->ts);
4024
4025 if (trace_seq_has_overflowed(s))
4026 return TRACE_TYPE_PARTIAL_LINE;
4027
4028 event = ftrace_find_event(entry->type);
4029 if (event)
4030 return event->funcs->raw(iter, 0, event);
4031
4032 trace_seq_printf(s, "%d ?\n", entry->type);
4033
4034 return trace_handle_return(s);
4035}
4036
4037static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
4038{
4039 struct trace_array *tr = iter->tr;
4040 struct trace_seq *s = &iter->seq;
4041 unsigned char newline = '\n';
4042 struct trace_entry *entry;
4043 struct trace_event *event;
4044
4045 entry = iter->ent;
4046
4047 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4048 SEQ_PUT_HEX_FIELD(s, entry->pid);
4049 SEQ_PUT_HEX_FIELD(s, iter->cpu);
4050 SEQ_PUT_HEX_FIELD(s, iter->ts);
4051 if (trace_seq_has_overflowed(s))
4052 return TRACE_TYPE_PARTIAL_LINE;
4053 }
4054
4055 event = ftrace_find_event(entry->type);
4056 if (event) {
4057 enum print_line_t ret = event->funcs->hex(iter, 0, event);
4058 if (ret != TRACE_TYPE_HANDLED)
4059 return ret;
4060 }
4061
4062 SEQ_PUT_FIELD(s, newline);
4063
4064 return trace_handle_return(s);
4065}
4066
4067static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
4068{
4069 struct trace_array *tr = iter->tr;
4070 struct trace_seq *s = &iter->seq;
4071 struct trace_entry *entry;
4072 struct trace_event *event;
4073
4074 entry = iter->ent;
4075
4076 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4077 SEQ_PUT_FIELD(s, entry->pid);
4078 SEQ_PUT_FIELD(s, iter->cpu);
4079 SEQ_PUT_FIELD(s, iter->ts);
4080 if (trace_seq_has_overflowed(s))
4081 return TRACE_TYPE_PARTIAL_LINE;
4082 }
4083
4084 event = ftrace_find_event(entry->type);
4085 return event ? event->funcs->binary(iter, 0, event) :
4086 TRACE_TYPE_HANDLED;
4087}
4088
4089int trace_empty(struct trace_iterator *iter)
4090{
4091 struct ring_buffer_iter *buf_iter;
4092 int cpu;
4093
4094 /* If we are looking at one CPU buffer, only check that one */
4095 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4096 cpu = iter->cpu_file;
4097 buf_iter = trace_buffer_iter(iter, cpu);
4098 if (buf_iter) {
4099 if (!ring_buffer_iter_empty(buf_iter))
4100 return 0;
4101 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02004102 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004103 return 0;
4104 }
4105 return 1;
4106 }
4107
4108 for_each_tracing_cpu(cpu) {
4109 buf_iter = trace_buffer_iter(iter, cpu);
4110 if (buf_iter) {
4111 if (!ring_buffer_iter_empty(buf_iter))
4112 return 0;
4113 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02004114 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004115 return 0;
4116 }
4117 }
4118
4119 return 1;
4120}
4121
4122/* Called with trace_event_read_lock() held. */
4123enum print_line_t print_trace_line(struct trace_iterator *iter)
4124{
4125 struct trace_array *tr = iter->tr;
4126 unsigned long trace_flags = tr->trace_flags;
4127 enum print_line_t ret;
4128
4129 if (iter->lost_events) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004130 if (iter->lost_events == (unsigned long)-1)
4131 trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4132 iter->cpu);
4133 else
4134 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4135 iter->cpu, iter->lost_events);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004136 if (trace_seq_has_overflowed(&iter->seq))
4137 return TRACE_TYPE_PARTIAL_LINE;
4138 }
4139
4140 if (iter->trace && iter->trace->print_line) {
4141 ret = iter->trace->print_line(iter);
4142 if (ret != TRACE_TYPE_UNHANDLED)
4143 return ret;
4144 }
4145
4146 if (iter->ent->type == TRACE_BPUTS &&
4147 trace_flags & TRACE_ITER_PRINTK &&
4148 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4149 return trace_print_bputs_msg_only(iter);
4150
4151 if (iter->ent->type == TRACE_BPRINT &&
4152 trace_flags & TRACE_ITER_PRINTK &&
4153 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4154 return trace_print_bprintk_msg_only(iter);
4155
4156 if (iter->ent->type == TRACE_PRINT &&
4157 trace_flags & TRACE_ITER_PRINTK &&
4158 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4159 return trace_print_printk_msg_only(iter);
4160
4161 if (trace_flags & TRACE_ITER_BIN)
4162 return print_bin_fmt(iter);
4163
4164 if (trace_flags & TRACE_ITER_HEX)
4165 return print_hex_fmt(iter);
4166
4167 if (trace_flags & TRACE_ITER_RAW)
4168 return print_raw_fmt(iter);
4169
4170 return print_trace_fmt(iter);
4171}
4172
4173void trace_latency_header(struct seq_file *m)
4174{
4175 struct trace_iterator *iter = m->private;
4176 struct trace_array *tr = iter->tr;
4177
4178 /* print nothing if the buffers are empty */
4179 if (trace_empty(iter))
4180 return;
4181
4182 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4183 print_trace_header(m, iter);
4184
4185 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
4186 print_lat_help_header(m);
4187}
4188
4189void trace_default_header(struct seq_file *m)
4190{
4191 struct trace_iterator *iter = m->private;
4192 struct trace_array *tr = iter->tr;
4193 unsigned long trace_flags = tr->trace_flags;
4194
4195 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4196 return;
4197
4198 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4199 /* print nothing if the buffers are empty */
4200 if (trace_empty(iter))
4201 return;
4202 print_trace_header(m, iter);
4203 if (!(trace_flags & TRACE_ITER_VERBOSE))
4204 print_lat_help_header(m);
4205 } else {
4206 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4207 if (trace_flags & TRACE_ITER_IRQ_INFO)
Olivier Deprez157378f2022-04-04 15:47:50 +02004208 print_func_help_header_irq(iter->array_buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004209 m, trace_flags);
4210 else
Olivier Deprez157378f2022-04-04 15:47:50 +02004211 print_func_help_header(iter->array_buffer, m,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004212 trace_flags);
4213 }
4214 }
4215}
4216
4217static void test_ftrace_alive(struct seq_file *m)
4218{
4219 if (!ftrace_is_dead())
4220 return;
4221 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4222 "# MAY BE MISSING FUNCTION EVENTS\n");
4223}
4224
4225#ifdef CONFIG_TRACER_MAX_TRACE
4226static void show_snapshot_main_help(struct seq_file *m)
4227{
4228 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
4229 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4230 "# Takes a snapshot of the main buffer.\n"
4231 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
4232 "# (Doesn't have to be '2' works with any number that\n"
4233 "# is not a '0' or '1')\n");
4234}
4235
4236static void show_snapshot_percpu_help(struct seq_file *m)
4237{
4238 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
4239#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4240 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4241 "# Takes a snapshot of the main buffer for this cpu.\n");
4242#else
4243 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4244 "# Must use main snapshot file to allocate.\n");
4245#endif
4246 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
4247 "# (Doesn't have to be '2' works with any number that\n"
4248 "# is not a '0' or '1')\n");
4249}
4250
4251static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4252{
4253 if (iter->tr->allocated_snapshot)
4254 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
4255 else
4256 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
4257
4258 seq_puts(m, "# Snapshot commands:\n");
4259 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4260 show_snapshot_main_help(m);
4261 else
4262 show_snapshot_percpu_help(m);
4263}
4264#else
4265/* Should never be called */
4266static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4267#endif
4268
4269static int s_show(struct seq_file *m, void *v)
4270{
4271 struct trace_iterator *iter = v;
4272 int ret;
4273
4274 if (iter->ent == NULL) {
4275 if (iter->tr) {
4276 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4277 seq_puts(m, "#\n");
4278 test_ftrace_alive(m);
4279 }
4280 if (iter->snapshot && trace_empty(iter))
4281 print_snapshot_help(m, iter);
4282 else if (iter->trace && iter->trace->print_header)
4283 iter->trace->print_header(m);
4284 else
4285 trace_default_header(m);
4286
4287 } else if (iter->leftover) {
4288 /*
4289 * If we filled the seq_file buffer earlier, we
4290 * want to just show it now.
4291 */
4292 ret = trace_print_seq(m, &iter->seq);
4293
4294 /* ret should this time be zero, but you never know */
4295 iter->leftover = ret;
4296
4297 } else {
4298 print_trace_line(iter);
4299 ret = trace_print_seq(m, &iter->seq);
4300 /*
4301 * If we overflow the seq_file buffer, then it will
4302 * ask us for this data again at start up.
4303 * Use that instead.
4304 * ret is 0 if seq_file write succeeded.
4305 * -1 otherwise.
4306 */
4307 iter->leftover = ret;
4308 }
4309
4310 return 0;
4311}
4312
4313/*
4314 * Should be used after trace_array_get(), trace_types_lock
4315 * ensures that i_cdev was already initialized.
4316 */
4317static inline int tracing_get_cpu(struct inode *inode)
4318{
4319 if (inode->i_cdev) /* See trace_create_cpu_file() */
4320 return (long)inode->i_cdev - 1;
4321 return RING_BUFFER_ALL_CPUS;
4322}
4323
4324static const struct seq_operations tracer_seq_ops = {
4325 .start = s_start,
4326 .next = s_next,
4327 .stop = s_stop,
4328 .show = s_show,
4329};
4330
4331static struct trace_iterator *
4332__tracing_open(struct inode *inode, struct file *file, bool snapshot)
4333{
4334 struct trace_array *tr = inode->i_private;
4335 struct trace_iterator *iter;
4336 int cpu;
4337
4338 if (tracing_disabled)
4339 return ERR_PTR(-ENODEV);
4340
4341 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
4342 if (!iter)
4343 return ERR_PTR(-ENOMEM);
4344
4345 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
4346 GFP_KERNEL);
4347 if (!iter->buffer_iter)
4348 goto release;
4349
4350 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02004351 * trace_find_next_entry() may need to save off iter->ent.
4352 * It will place it into the iter->temp buffer. As most
4353 * events are less than 128, allocate a buffer of that size.
4354 * If one is greater, then trace_find_next_entry() will
4355 * allocate a new buffer to adjust for the bigger iter->ent.
4356 * It's not critical if it fails to get allocated here.
4357 */
4358 iter->temp = kmalloc(128, GFP_KERNEL);
4359 if (iter->temp)
4360 iter->temp_size = 128;
4361
4362 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004363 * We make a copy of the current tracer to avoid concurrent
4364 * changes on it while we are reading.
4365 */
4366 mutex_lock(&trace_types_lock);
4367 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
4368 if (!iter->trace)
4369 goto fail;
4370
4371 *iter->trace = *tr->current_trace;
4372
4373 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
4374 goto fail;
4375
4376 iter->tr = tr;
4377
4378#ifdef CONFIG_TRACER_MAX_TRACE
4379 /* Currently only the top directory has a snapshot */
4380 if (tr->current_trace->print_max || snapshot)
Olivier Deprez157378f2022-04-04 15:47:50 +02004381 iter->array_buffer = &tr->max_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004382 else
4383#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02004384 iter->array_buffer = &tr->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004385 iter->snapshot = snapshot;
4386 iter->pos = -1;
4387 iter->cpu_file = tracing_get_cpu(inode);
4388 mutex_init(&iter->mutex);
4389
4390 /* Notify the tracer early; before we stop tracing. */
Olivier Deprez157378f2022-04-04 15:47:50 +02004391 if (iter->trace->open)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004392 iter->trace->open(iter);
4393
4394 /* Annotate start of buffers if we had overruns */
Olivier Deprez157378f2022-04-04 15:47:50 +02004395 if (ring_buffer_overruns(iter->array_buffer->buffer))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004396 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4397
4398 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4399 if (trace_clocks[tr->clock_id].in_ns)
4400 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4401
Olivier Deprez157378f2022-04-04 15:47:50 +02004402 /*
4403 * If pause-on-trace is enabled, then stop the trace while
4404 * dumping, unless this is the "snapshot" file
4405 */
4406 if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004407 tracing_stop_tr(tr);
4408
4409 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
4410 for_each_tracing_cpu(cpu) {
4411 iter->buffer_iter[cpu] =
Olivier Deprez157378f2022-04-04 15:47:50 +02004412 ring_buffer_read_prepare(iter->array_buffer->buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00004413 cpu, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004414 }
4415 ring_buffer_read_prepare_sync();
4416 for_each_tracing_cpu(cpu) {
4417 ring_buffer_read_start(iter->buffer_iter[cpu]);
4418 tracing_iter_reset(iter, cpu);
4419 }
4420 } else {
4421 cpu = iter->cpu_file;
4422 iter->buffer_iter[cpu] =
Olivier Deprez157378f2022-04-04 15:47:50 +02004423 ring_buffer_read_prepare(iter->array_buffer->buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00004424 cpu, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004425 ring_buffer_read_prepare_sync();
4426 ring_buffer_read_start(iter->buffer_iter[cpu]);
4427 tracing_iter_reset(iter, cpu);
4428 }
4429
4430 mutex_unlock(&trace_types_lock);
4431
4432 return iter;
4433
4434 fail:
4435 mutex_unlock(&trace_types_lock);
4436 kfree(iter->trace);
Olivier Deprez157378f2022-04-04 15:47:50 +02004437 kfree(iter->temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004438 kfree(iter->buffer_iter);
4439release:
4440 seq_release_private(inode, file);
4441 return ERR_PTR(-ENOMEM);
4442}
4443
4444int tracing_open_generic(struct inode *inode, struct file *filp)
4445{
David Brazdil0f672f62019-12-10 10:32:29 +00004446 int ret;
4447
4448 ret = tracing_check_open_get_tr(NULL);
4449 if (ret)
4450 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004451
4452 filp->private_data = inode->i_private;
4453 return 0;
4454}
4455
4456bool tracing_is_disabled(void)
4457{
4458 return (tracing_disabled) ? true: false;
4459}
4460
4461/*
4462 * Open and update trace_array ref count.
4463 * Must have the current trace_array passed to it.
4464 */
David Brazdil0f672f62019-12-10 10:32:29 +00004465int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004466{
4467 struct trace_array *tr = inode->i_private;
David Brazdil0f672f62019-12-10 10:32:29 +00004468 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004469
David Brazdil0f672f62019-12-10 10:32:29 +00004470 ret = tracing_check_open_get_tr(tr);
4471 if (ret)
4472 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004473
4474 filp->private_data = inode->i_private;
4475
4476 return 0;
4477}
4478
4479static int tracing_release(struct inode *inode, struct file *file)
4480{
4481 struct trace_array *tr = inode->i_private;
4482 struct seq_file *m = file->private_data;
4483 struct trace_iterator *iter;
4484 int cpu;
4485
4486 if (!(file->f_mode & FMODE_READ)) {
4487 trace_array_put(tr);
4488 return 0;
4489 }
4490
4491 /* Writes do not use seq_file */
4492 iter = m->private;
4493 mutex_lock(&trace_types_lock);
4494
4495 for_each_tracing_cpu(cpu) {
4496 if (iter->buffer_iter[cpu])
4497 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4498 }
4499
4500 if (iter->trace && iter->trace->close)
4501 iter->trace->close(iter);
4502
Olivier Deprez157378f2022-04-04 15:47:50 +02004503 if (!iter->snapshot && tr->stop_count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004504 /* reenable tracing if it was previously enabled */
4505 tracing_start_tr(tr);
4506
4507 __trace_array_put(tr);
4508
4509 mutex_unlock(&trace_types_lock);
4510
4511 mutex_destroy(&iter->mutex);
4512 free_cpumask_var(iter->started);
Olivier Deprez157378f2022-04-04 15:47:50 +02004513 kfree(iter->temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004514 kfree(iter->trace);
4515 kfree(iter->buffer_iter);
4516 seq_release_private(inode, file);
4517
4518 return 0;
4519}
4520
4521static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4522{
4523 struct trace_array *tr = inode->i_private;
4524
4525 trace_array_put(tr);
4526 return 0;
4527}
4528
4529static int tracing_single_release_tr(struct inode *inode, struct file *file)
4530{
4531 struct trace_array *tr = inode->i_private;
4532
4533 trace_array_put(tr);
4534
4535 return single_release(inode, file);
4536}
4537
4538static int tracing_open(struct inode *inode, struct file *file)
4539{
4540 struct trace_array *tr = inode->i_private;
4541 struct trace_iterator *iter;
David Brazdil0f672f62019-12-10 10:32:29 +00004542 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004543
David Brazdil0f672f62019-12-10 10:32:29 +00004544 ret = tracing_check_open_get_tr(tr);
4545 if (ret)
4546 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004547
4548 /* If this file was open for write, then erase contents */
4549 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4550 int cpu = tracing_get_cpu(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02004551 struct array_buffer *trace_buf = &tr->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004552
4553#ifdef CONFIG_TRACER_MAX_TRACE
4554 if (tr->current_trace->print_max)
4555 trace_buf = &tr->max_buffer;
4556#endif
4557
4558 if (cpu == RING_BUFFER_ALL_CPUS)
4559 tracing_reset_online_cpus(trace_buf);
4560 else
David Brazdil0f672f62019-12-10 10:32:29 +00004561 tracing_reset_cpu(trace_buf, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004562 }
4563
4564 if (file->f_mode & FMODE_READ) {
4565 iter = __tracing_open(inode, file, false);
4566 if (IS_ERR(iter))
4567 ret = PTR_ERR(iter);
4568 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4569 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4570 }
4571
4572 if (ret < 0)
4573 trace_array_put(tr);
4574
4575 return ret;
4576}
4577
4578/*
4579 * Some tracers are not suitable for instance buffers.
4580 * A tracer is always available for the global array (toplevel)
4581 * or if it explicitly states that it is.
4582 */
4583static bool
4584trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4585{
4586 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4587}
4588
4589/* Find the next tracer that this trace array may use */
4590static struct tracer *
4591get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4592{
4593 while (t && !trace_ok_for_array(t, tr))
4594 t = t->next;
4595
4596 return t;
4597}
4598
4599static void *
4600t_next(struct seq_file *m, void *v, loff_t *pos)
4601{
4602 struct trace_array *tr = m->private;
4603 struct tracer *t = v;
4604
4605 (*pos)++;
4606
4607 if (t)
4608 t = get_tracer_for_array(tr, t->next);
4609
4610 return t;
4611}
4612
4613static void *t_start(struct seq_file *m, loff_t *pos)
4614{
4615 struct trace_array *tr = m->private;
4616 struct tracer *t;
4617 loff_t l = 0;
4618
4619 mutex_lock(&trace_types_lock);
4620
4621 t = get_tracer_for_array(tr, trace_types);
4622 for (; t && l < *pos; t = t_next(m, t, &l))
4623 ;
4624
4625 return t;
4626}
4627
4628static void t_stop(struct seq_file *m, void *p)
4629{
4630 mutex_unlock(&trace_types_lock);
4631}
4632
4633static int t_show(struct seq_file *m, void *v)
4634{
4635 struct tracer *t = v;
4636
4637 if (!t)
4638 return 0;
4639
4640 seq_puts(m, t->name);
4641 if (t->next)
4642 seq_putc(m, ' ');
4643 else
4644 seq_putc(m, '\n');
4645
4646 return 0;
4647}
4648
4649static const struct seq_operations show_traces_seq_ops = {
4650 .start = t_start,
4651 .next = t_next,
4652 .stop = t_stop,
4653 .show = t_show,
4654};
4655
4656static int show_traces_open(struct inode *inode, struct file *file)
4657{
4658 struct trace_array *tr = inode->i_private;
4659 struct seq_file *m;
4660 int ret;
4661
David Brazdil0f672f62019-12-10 10:32:29 +00004662 ret = tracing_check_open_get_tr(tr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004663 if (ret)
4664 return ret;
4665
David Brazdil0f672f62019-12-10 10:32:29 +00004666 ret = seq_open(file, &show_traces_seq_ops);
4667 if (ret) {
4668 trace_array_put(tr);
4669 return ret;
4670 }
4671
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004672 m = file->private_data;
4673 m->private = tr;
4674
4675 return 0;
4676}
4677
David Brazdil0f672f62019-12-10 10:32:29 +00004678static int show_traces_release(struct inode *inode, struct file *file)
4679{
4680 struct trace_array *tr = inode->i_private;
4681
4682 trace_array_put(tr);
4683 return seq_release(inode, file);
4684}
4685
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004686static ssize_t
4687tracing_write_stub(struct file *filp, const char __user *ubuf,
4688 size_t count, loff_t *ppos)
4689{
4690 return count;
4691}
4692
4693loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
4694{
4695 int ret;
4696
4697 if (file->f_mode & FMODE_READ)
4698 ret = seq_lseek(file, offset, whence);
4699 else
4700 file->f_pos = ret = 0;
4701
4702 return ret;
4703}
4704
4705static const struct file_operations tracing_fops = {
4706 .open = tracing_open,
4707 .read = seq_read,
4708 .write = tracing_write_stub,
4709 .llseek = tracing_lseek,
4710 .release = tracing_release,
4711};
4712
4713static const struct file_operations show_traces_fops = {
4714 .open = show_traces_open,
4715 .read = seq_read,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004716 .llseek = seq_lseek,
David Brazdil0f672f62019-12-10 10:32:29 +00004717 .release = show_traces_release,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004718};
4719
4720static ssize_t
4721tracing_cpumask_read(struct file *filp, char __user *ubuf,
4722 size_t count, loff_t *ppos)
4723{
4724 struct trace_array *tr = file_inode(filp)->i_private;
4725 char *mask_str;
4726 int len;
4727
4728 len = snprintf(NULL, 0, "%*pb\n",
4729 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4730 mask_str = kmalloc(len, GFP_KERNEL);
4731 if (!mask_str)
4732 return -ENOMEM;
4733
4734 len = snprintf(mask_str, len, "%*pb\n",
4735 cpumask_pr_args(tr->tracing_cpumask));
4736 if (len >= count) {
4737 count = -EINVAL;
4738 goto out_err;
4739 }
4740 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
4741
4742out_err:
4743 kfree(mask_str);
4744
4745 return count;
4746}
4747
Olivier Deprez157378f2022-04-04 15:47:50 +02004748int tracing_set_cpumask(struct trace_array *tr,
4749 cpumask_var_t tracing_cpumask_new)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004750{
Olivier Deprez157378f2022-04-04 15:47:50 +02004751 int cpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004752
Olivier Deprez157378f2022-04-04 15:47:50 +02004753 if (!tr)
4754 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004755
4756 local_irq_disable();
4757 arch_spin_lock(&tr->max_lock);
4758 for_each_tracing_cpu(cpu) {
4759 /*
4760 * Increase/decrease the disabled counter if we are
4761 * about to flip a bit in the cpumask:
4762 */
4763 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
4764 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004765 atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4766 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004767 }
4768 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
4769 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004770 atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4771 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004772 }
4773 }
4774 arch_spin_unlock(&tr->max_lock);
4775 local_irq_enable();
4776
4777 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Olivier Deprez157378f2022-04-04 15:47:50 +02004778
4779 return 0;
4780}
4781
4782static ssize_t
4783tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4784 size_t count, loff_t *ppos)
4785{
4786 struct trace_array *tr = file_inode(filp)->i_private;
4787 cpumask_var_t tracing_cpumask_new;
4788 int err;
4789
4790 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4791 return -ENOMEM;
4792
4793 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4794 if (err)
4795 goto err_free;
4796
4797 err = tracing_set_cpumask(tr, tracing_cpumask_new);
4798 if (err)
4799 goto err_free;
4800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004801 free_cpumask_var(tracing_cpumask_new);
4802
4803 return count;
4804
Olivier Deprez157378f2022-04-04 15:47:50 +02004805err_free:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004806 free_cpumask_var(tracing_cpumask_new);
4807
4808 return err;
4809}
4810
4811static const struct file_operations tracing_cpumask_fops = {
4812 .open = tracing_open_generic_tr,
4813 .read = tracing_cpumask_read,
4814 .write = tracing_cpumask_write,
4815 .release = tracing_release_generic_tr,
4816 .llseek = generic_file_llseek,
4817};
4818
4819static int tracing_trace_options_show(struct seq_file *m, void *v)
4820{
4821 struct tracer_opt *trace_opts;
4822 struct trace_array *tr = m->private;
4823 u32 tracer_flags;
4824 int i;
4825
4826 mutex_lock(&trace_types_lock);
4827 tracer_flags = tr->current_trace->flags->val;
4828 trace_opts = tr->current_trace->flags->opts;
4829
4830 for (i = 0; trace_options[i]; i++) {
4831 if (tr->trace_flags & (1 << i))
4832 seq_printf(m, "%s\n", trace_options[i]);
4833 else
4834 seq_printf(m, "no%s\n", trace_options[i]);
4835 }
4836
4837 for (i = 0; trace_opts[i].name; i++) {
4838 if (tracer_flags & trace_opts[i].bit)
4839 seq_printf(m, "%s\n", trace_opts[i].name);
4840 else
4841 seq_printf(m, "no%s\n", trace_opts[i].name);
4842 }
4843 mutex_unlock(&trace_types_lock);
4844
4845 return 0;
4846}
4847
4848static int __set_tracer_option(struct trace_array *tr,
4849 struct tracer_flags *tracer_flags,
4850 struct tracer_opt *opts, int neg)
4851{
4852 struct tracer *trace = tracer_flags->trace;
4853 int ret;
4854
4855 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
4856 if (ret)
4857 return ret;
4858
4859 if (neg)
4860 tracer_flags->val &= ~opts->bit;
4861 else
4862 tracer_flags->val |= opts->bit;
4863 return 0;
4864}
4865
4866/* Try to assign a tracer specific option */
4867static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
4868{
4869 struct tracer *trace = tr->current_trace;
4870 struct tracer_flags *tracer_flags = trace->flags;
4871 struct tracer_opt *opts = NULL;
4872 int i;
4873
4874 for (i = 0; tracer_flags->opts[i].name; i++) {
4875 opts = &tracer_flags->opts[i];
4876
4877 if (strcmp(cmp, opts->name) == 0)
4878 return __set_tracer_option(tr, trace->flags, opts, neg);
4879 }
4880
4881 return -EINVAL;
4882}
4883
4884/* Some tracers require overwrite to stay enabled */
4885int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4886{
4887 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4888 return -1;
4889
4890 return 0;
4891}
4892
4893int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
4894{
Olivier Deprez0e641232021-09-23 10:07:05 +02004895 int *map;
4896
4897 if ((mask == TRACE_ITER_RECORD_TGID) ||
4898 (mask == TRACE_ITER_RECORD_CMD))
4899 lockdep_assert_held(&event_mutex);
4900
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004901 /* do nothing if flag is already set */
4902 if (!!(tr->trace_flags & mask) == !!enabled)
4903 return 0;
4904
4905 /* Give the tracer a chance to approve the change */
4906 if (tr->current_trace->flag_changed)
4907 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
4908 return -EINVAL;
4909
4910 if (enabled)
4911 tr->trace_flags |= mask;
4912 else
4913 tr->trace_flags &= ~mask;
4914
4915 if (mask == TRACE_ITER_RECORD_CMD)
4916 trace_event_enable_cmd_record(enabled);
4917
4918 if (mask == TRACE_ITER_RECORD_TGID) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004919 if (!tgid_map) {
4920 tgid_map_max = pid_max;
4921 map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
4922 GFP_KERNEL);
4923
4924 /*
4925 * Pairs with smp_load_acquire() in
4926 * trace_find_tgid_ptr() to ensure that if it observes
4927 * the tgid_map we just allocated then it also observes
4928 * the corresponding tgid_map_max value.
4929 */
4930 smp_store_release(&tgid_map, map);
4931 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004932 if (!tgid_map) {
4933 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4934 return -ENOMEM;
4935 }
4936
4937 trace_event_enable_tgid_record(enabled);
4938 }
4939
4940 if (mask == TRACE_ITER_EVENT_FORK)
4941 trace_event_follow_fork(tr, enabled);
4942
4943 if (mask == TRACE_ITER_FUNC_FORK)
4944 ftrace_pid_follow_fork(tr, enabled);
4945
4946 if (mask == TRACE_ITER_OVERWRITE) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004947 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004948#ifdef CONFIG_TRACER_MAX_TRACE
4949 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
4950#endif
4951 }
4952
4953 if (mask == TRACE_ITER_PRINTK) {
4954 trace_printk_start_stop_comm(enabled);
4955 trace_printk_control(enabled);
4956 }
4957
4958 return 0;
4959}
4960
Olivier Deprez157378f2022-04-04 15:47:50 +02004961int trace_set_options(struct trace_array *tr, char *option)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004962{
4963 char *cmp;
4964 int neg = 0;
4965 int ret;
4966 size_t orig_len = strlen(option);
David Brazdil0f672f62019-12-10 10:32:29 +00004967 int len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004968
4969 cmp = strstrip(option);
4970
David Brazdil0f672f62019-12-10 10:32:29 +00004971 len = str_has_prefix(cmp, "no");
4972 if (len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004973 neg = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00004974
4975 cmp += len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004976
Olivier Deprez0e641232021-09-23 10:07:05 +02004977 mutex_lock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004978 mutex_lock(&trace_types_lock);
4979
4980 ret = match_string(trace_options, -1, cmp);
4981 /* If no option could be set, test the specific tracer options */
4982 if (ret < 0)
4983 ret = set_tracer_option(tr, cmp, neg);
4984 else
4985 ret = set_tracer_flag(tr, 1 << ret, !neg);
4986
4987 mutex_unlock(&trace_types_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02004988 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004989
4990 /*
4991 * If the first trailing whitespace is replaced with '\0' by strstrip,
4992 * turn it back into a space.
4993 */
4994 if (orig_len > strlen(option))
4995 option[strlen(option)] = ' ';
4996
4997 return ret;
4998}
4999
5000static void __init apply_trace_boot_options(void)
5001{
5002 char *buf = trace_boot_options_buf;
5003 char *option;
5004
5005 while (true) {
5006 option = strsep(&buf, ",");
5007
5008 if (!option)
5009 break;
5010
5011 if (*option)
5012 trace_set_options(&global_trace, option);
5013
5014 /* Put back the comma to allow this to be called again */
5015 if (buf)
5016 *(buf - 1) = ',';
5017 }
5018}
5019
5020static ssize_t
5021tracing_trace_options_write(struct file *filp, const char __user *ubuf,
5022 size_t cnt, loff_t *ppos)
5023{
5024 struct seq_file *m = filp->private_data;
5025 struct trace_array *tr = m->private;
5026 char buf[64];
5027 int ret;
5028
5029 if (cnt >= sizeof(buf))
5030 return -EINVAL;
5031
5032 if (copy_from_user(buf, ubuf, cnt))
5033 return -EFAULT;
5034
5035 buf[cnt] = 0;
5036
5037 ret = trace_set_options(tr, buf);
5038 if (ret < 0)
5039 return ret;
5040
5041 *ppos += cnt;
5042
5043 return cnt;
5044}
5045
5046static int tracing_trace_options_open(struct inode *inode, struct file *file)
5047{
5048 struct trace_array *tr = inode->i_private;
5049 int ret;
5050
David Brazdil0f672f62019-12-10 10:32:29 +00005051 ret = tracing_check_open_get_tr(tr);
5052 if (ret)
5053 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005054
5055 ret = single_open(file, tracing_trace_options_show, inode->i_private);
5056 if (ret < 0)
5057 trace_array_put(tr);
5058
5059 return ret;
5060}
5061
5062static const struct file_operations tracing_iter_fops = {
5063 .open = tracing_trace_options_open,
5064 .read = seq_read,
5065 .llseek = seq_lseek,
5066 .release = tracing_single_release_tr,
5067 .write = tracing_trace_options_write,
5068};
5069
5070static const char readme_msg[] =
5071 "tracing mini-HOWTO:\n\n"
5072 "# echo 0 > tracing_on : quick way to disable tracing\n"
5073 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
5074 " Important files:\n"
5075 " trace\t\t\t- The static contents of the buffer\n"
5076 "\t\t\t To clear the buffer write into this file: echo > trace\n"
5077 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
5078 " current_tracer\t- function and latency tracers\n"
5079 " available_tracers\t- list of configured tracers for current_tracer\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005080 " error_log\t- error log for failed commands (that support it)\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005081 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
5082 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
5083 " trace_clock\t\t-change the clock used to order events\n"
5084 " local: Per cpu clock but may not be synced across CPUs\n"
5085 " global: Synced across CPUs but slows tracing down.\n"
5086 " counter: Not a clock, but just an increment\n"
5087 " uptime: Jiffy counter from time of boot\n"
5088 " perf: Same clock that perf events use\n"
5089#ifdef CONFIG_X86_64
5090 " x86-tsc: TSC cycle counter\n"
5091#endif
5092 "\n timestamp_mode\t-view the mode used to timestamp events\n"
5093 " delta: Delta difference against a buffer-wide timestamp\n"
5094 " absolute: Absolute (standalone) timestamp\n"
5095 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
5096 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
5097 " tracing_cpumask\t- Limit which CPUs to trace\n"
5098 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
5099 "\t\t\t Remove sub-buffer with rmdir\n"
5100 " trace_options\t\t- Set format or modify how tracing happens\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005101 "\t\t\t Disable an option by prefixing 'no' to the\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005102 "\t\t\t option name\n"
5103 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
5104#ifdef CONFIG_DYNAMIC_FTRACE
5105 "\n available_filter_functions - list of functions that can be filtered on\n"
5106 " set_ftrace_filter\t- echo function name in here to only trace these\n"
5107 "\t\t\t functions\n"
5108 "\t accepts: func_full_name or glob-matching-pattern\n"
5109 "\t modules: Can select a group via module\n"
5110 "\t Format: :mod:<module-name>\n"
5111 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
5112 "\t triggers: a command to perform when function is hit\n"
5113 "\t Format: <function>:<trigger>[:count]\n"
5114 "\t trigger: traceon, traceoff\n"
5115 "\t\t enable_event:<system>:<event>\n"
5116 "\t\t disable_event:<system>:<event>\n"
5117#ifdef CONFIG_STACKTRACE
5118 "\t\t stacktrace\n"
5119#endif
5120#ifdef CONFIG_TRACER_SNAPSHOT
5121 "\t\t snapshot\n"
5122#endif
5123 "\t\t dump\n"
5124 "\t\t cpudump\n"
5125 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
5126 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
5127 "\t The first one will disable tracing every time do_fault is hit\n"
5128 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
5129 "\t The first time do trap is hit and it disables tracing, the\n"
5130 "\t counter will decrement to 2. If tracing is already disabled,\n"
5131 "\t the counter will not decrement. It only decrements when the\n"
5132 "\t trigger did work\n"
5133 "\t To remove trigger without count:\n"
5134 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
5135 "\t To remove trigger with a count:\n"
5136 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
5137 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
5138 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
5139 "\t modules: Can select a group via module command :mod:\n"
5140 "\t Does not accept triggers\n"
5141#endif /* CONFIG_DYNAMIC_FTRACE */
5142#ifdef CONFIG_FUNCTION_TRACER
5143 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
5144 "\t\t (function)\n"
Olivier Deprez157378f2022-04-04 15:47:50 +02005145 " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5146 "\t\t (function)\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005147#endif
5148#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5149 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
5150 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
5151 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
5152#endif
5153#ifdef CONFIG_TRACER_SNAPSHOT
5154 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
5155 "\t\t\t snapshot buffer. Read the contents for more\n"
5156 "\t\t\t information\n"
5157#endif
5158#ifdef CONFIG_STACK_TRACER
5159 " stack_trace\t\t- Shows the max stack trace when active\n"
5160 " stack_max_size\t- Shows current max stack size that was traced\n"
5161 "\t\t\t Write into this file to reset the max size (trigger a\n"
5162 "\t\t\t new trace)\n"
5163#ifdef CONFIG_DYNAMIC_FTRACE
5164 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
5165 "\t\t\t traces\n"
5166#endif
5167#endif /* CONFIG_STACK_TRACER */
David Brazdil0f672f62019-12-10 10:32:29 +00005168#ifdef CONFIG_DYNAMIC_EVENTS
5169 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5170 "\t\t\t Write into this file to define/undefine new trace events.\n"
5171#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005172#ifdef CONFIG_KPROBE_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00005173 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005174 "\t\t\t Write into this file to define/undefine new trace events.\n"
5175#endif
5176#ifdef CONFIG_UPROBE_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00005177 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005178 "\t\t\t Write into this file to define/undefine new trace events.\n"
5179#endif
5180#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
5181 "\t accepts: event-definitions (one definition per line)\n"
5182 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
5183 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005184#ifdef CONFIG_HIST_TRIGGERS
5185 "\t s:[synthetic/]<event> <field> [<field>]\n"
5186#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005187 "\t -:[<group>/]<event>\n"
5188#ifdef CONFIG_KPROBE_EVENTS
5189 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Olivier Deprez157378f2022-04-04 15:47:50 +02005190 "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005191#endif
5192#ifdef CONFIG_UPROBE_EVENTS
Olivier Deprez157378f2022-04-04 15:47:50 +02005193 " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005194#endif
5195 "\t args: <name>=fetcharg[:type]\n"
5196 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005197#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
5198 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
5199#else
5200 "\t $stack<index>, $stack, $retval, $comm,\n"
5201#endif
5202 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5203 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
5204 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5205 "\t <type>\\[<array-size>\\]\n"
5206#ifdef CONFIG_HIST_TRIGGERS
5207 "\t field: <stype> <name>;\n"
5208 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5209 "\t [unsigned] char/int/long\n"
5210#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005211#endif
5212 " events/\t\t- Directory containing all trace event subsystems:\n"
5213 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
5214 " events/<system>/\t- Directory containing all trace events for <system>:\n"
5215 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5216 "\t\t\t events\n"
5217 " filter\t\t- If set, only events passing filter are traced\n"
5218 " events/<system>/<event>/\t- Directory containing control files for\n"
5219 "\t\t\t <event>:\n"
5220 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
5221 " filter\t\t- If set, only events passing filter are traced\n"
5222 " trigger\t\t- If set, a command to perform when event is hit\n"
5223 "\t Format: <trigger>[:count][if <filter>]\n"
5224 "\t trigger: traceon, traceoff\n"
5225 "\t enable_event:<system>:<event>\n"
5226 "\t disable_event:<system>:<event>\n"
5227#ifdef CONFIG_HIST_TRIGGERS
5228 "\t enable_hist:<system>:<event>\n"
5229 "\t disable_hist:<system>:<event>\n"
5230#endif
5231#ifdef CONFIG_STACKTRACE
5232 "\t\t stacktrace\n"
5233#endif
5234#ifdef CONFIG_TRACER_SNAPSHOT
5235 "\t\t snapshot\n"
5236#endif
5237#ifdef CONFIG_HIST_TRIGGERS
5238 "\t\t hist (see below)\n"
5239#endif
5240 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
5241 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
5242 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
5243 "\t events/block/block_unplug/trigger\n"
5244 "\t The first disables tracing every time block_unplug is hit.\n"
5245 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
5246 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
5247 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
5248 "\t Like function triggers, the counter is only decremented if it\n"
5249 "\t enabled or disabled tracing.\n"
5250 "\t To remove a trigger without a count:\n"
5251 "\t echo '!<trigger> > <system>/<event>/trigger\n"
5252 "\t To remove a trigger with a count:\n"
5253 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
5254 "\t Filters can be ignored when removing a trigger.\n"
5255#ifdef CONFIG_HIST_TRIGGERS
5256 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
5257 "\t Format: hist:keys=<field1[,field2,...]>\n"
5258 "\t [:values=<field1[,field2,...]>]\n"
5259 "\t [:sort=<field1[,field2,...]>]\n"
5260 "\t [:size=#entries]\n"
5261 "\t [:pause][:continue][:clear]\n"
5262 "\t [:name=histname1]\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005263 "\t [:<handler>.<action>]\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005264 "\t [if <filter>]\n\n"
Olivier Deprez0e641232021-09-23 10:07:05 +02005265 "\t Note, special fields can be used as well:\n"
5266 "\t common_timestamp - to record current timestamp\n"
5267 "\t common_cpu - to record the CPU the event happened on\n"
5268 "\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005269 "\t When a matching event is hit, an entry is added to a hash\n"
5270 "\t table using the key(s) and value(s) named, and the value of a\n"
5271 "\t sum called 'hitcount' is incremented. Keys and values\n"
5272 "\t correspond to fields in the event's format description. Keys\n"
5273 "\t can be any field, or the special string 'stacktrace'.\n"
5274 "\t Compound keys consisting of up to two fields can be specified\n"
5275 "\t by the 'keys' keyword. Values must correspond to numeric\n"
5276 "\t fields. Sort keys consisting of up to two fields can be\n"
5277 "\t specified using the 'sort' keyword. The sort direction can\n"
5278 "\t be modified by appending '.descending' or '.ascending' to a\n"
5279 "\t sort field. The 'size' parameter can be used to specify more\n"
5280 "\t or fewer than the default 2048 entries for the hashtable size.\n"
5281 "\t If a hist trigger is given a name using the 'name' parameter,\n"
5282 "\t its histogram data will be shared with other triggers of the\n"
5283 "\t same name, and trigger hits will update this common data.\n\n"
5284 "\t Reading the 'hist' file for the event will dump the hash\n"
5285 "\t table in its entirety to stdout. If there are multiple hist\n"
5286 "\t triggers attached to an event, there will be a table for each\n"
5287 "\t trigger in the output. The table displayed for a named\n"
5288 "\t trigger will be the same as any other instance having the\n"
5289 "\t same name. The default format used to display a given field\n"
5290 "\t can be modified by appending any of the following modifiers\n"
5291 "\t to the field name, as applicable:\n\n"
5292 "\t .hex display a number as a hex value\n"
5293 "\t .sym display an address as a symbol\n"
5294 "\t .sym-offset display an address as a symbol and offset\n"
5295 "\t .execname display a common_pid as a program name\n"
5296 "\t .syscall display a syscall id as a syscall name\n"
5297 "\t .log2 display log2 value rather than raw number\n"
5298 "\t .usecs display a common_timestamp in microseconds\n\n"
5299 "\t The 'pause' parameter can be used to pause an existing hist\n"
5300 "\t trigger or to start a hist trigger but not log any events\n"
5301 "\t until told to do so. 'continue' can be used to start or\n"
5302 "\t restart a paused hist trigger.\n\n"
5303 "\t The 'clear' parameter will clear the contents of a running\n"
5304 "\t hist trigger and leave its current paused/active state\n"
5305 "\t unchanged.\n\n"
5306 "\t The enable_hist and disable_hist triggers can be used to\n"
5307 "\t have one event conditionally start and stop another event's\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005308 "\t already-attached hist trigger. The syntax is analogous to\n"
5309 "\t the enable_event and disable_event triggers.\n\n"
5310 "\t Hist trigger handlers and actions are executed whenever a\n"
5311 "\t a histogram entry is added or updated. They take the form:\n\n"
5312 "\t <handler>.<action>\n\n"
5313 "\t The available handlers are:\n\n"
5314 "\t onmatch(matching.event) - invoke on addition or update\n"
5315 "\t onmax(var) - invoke if var exceeds current max\n"
5316 "\t onchange(var) - invoke action if var changes\n\n"
5317 "\t The available actions are:\n\n"
5318 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5319 "\t save(field,...) - save current event fields\n"
5320#ifdef CONFIG_TRACER_SNAPSHOT
Olivier Deprez157378f2022-04-04 15:47:50 +02005321 "\t snapshot() - snapshot the trace buffer\n\n"
5322#endif
5323#ifdef CONFIG_SYNTH_EVENTS
5324 " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5325 "\t Write into this file to define/undefine new synthetic events.\n"
5326 "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005327#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005328#endif
5329;
5330
5331static ssize_t
5332tracing_readme_read(struct file *filp, char __user *ubuf,
5333 size_t cnt, loff_t *ppos)
5334{
5335 return simple_read_from_buffer(ubuf, cnt, ppos,
5336 readme_msg, strlen(readme_msg));
5337}
5338
5339static const struct file_operations tracing_readme_fops = {
5340 .open = tracing_open_generic,
5341 .read = tracing_readme_read,
5342 .llseek = generic_file_llseek,
5343};
5344
5345static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
5346{
Olivier Deprez0e641232021-09-23 10:07:05 +02005347 int pid = ++(*pos);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005348
Olivier Deprez0e641232021-09-23 10:07:05 +02005349 return trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005350}
5351
5352static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
5353{
Olivier Deprez0e641232021-09-23 10:07:05 +02005354 int pid = *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005355
Olivier Deprez0e641232021-09-23 10:07:05 +02005356 return trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005357}
5358
5359static void saved_tgids_stop(struct seq_file *m, void *v)
5360{
5361}
5362
5363static int saved_tgids_show(struct seq_file *m, void *v)
5364{
Olivier Deprez0e641232021-09-23 10:07:05 +02005365 int *entry = (int *)v;
5366 int pid = entry - tgid_map;
5367 int tgid = *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005368
Olivier Deprez0e641232021-09-23 10:07:05 +02005369 if (tgid == 0)
5370 return SEQ_SKIP;
5371
5372 seq_printf(m, "%d %d\n", pid, tgid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005373 return 0;
5374}
5375
5376static const struct seq_operations tracing_saved_tgids_seq_ops = {
5377 .start = saved_tgids_start,
5378 .stop = saved_tgids_stop,
5379 .next = saved_tgids_next,
5380 .show = saved_tgids_show,
5381};
5382
5383static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5384{
David Brazdil0f672f62019-12-10 10:32:29 +00005385 int ret;
5386
5387 ret = tracing_check_open_get_tr(NULL);
5388 if (ret)
5389 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005390
5391 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5392}
5393
5394
5395static const struct file_operations tracing_saved_tgids_fops = {
5396 .open = tracing_saved_tgids_open,
5397 .read = seq_read,
5398 .llseek = seq_lseek,
5399 .release = seq_release,
5400};
5401
5402static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
5403{
5404 unsigned int *ptr = v;
5405
5406 if (*pos || m->count)
5407 ptr++;
5408
5409 (*pos)++;
5410
5411 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5412 ptr++) {
5413 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
5414 continue;
5415
5416 return ptr;
5417 }
5418
5419 return NULL;
5420}
5421
5422static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5423{
5424 void *v;
5425 loff_t l = 0;
5426
5427 preempt_disable();
5428 arch_spin_lock(&trace_cmdline_lock);
5429
5430 v = &savedcmd->map_cmdline_to_pid[0];
5431 while (l <= *pos) {
5432 v = saved_cmdlines_next(m, v, &l);
5433 if (!v)
5434 return NULL;
5435 }
5436
5437 return v;
5438}
5439
5440static void saved_cmdlines_stop(struct seq_file *m, void *v)
5441{
5442 arch_spin_unlock(&trace_cmdline_lock);
5443 preempt_enable();
5444}
5445
5446static int saved_cmdlines_show(struct seq_file *m, void *v)
5447{
5448 char buf[TASK_COMM_LEN];
5449 unsigned int *pid = v;
5450
5451 __trace_find_cmdline(*pid, buf);
5452 seq_printf(m, "%d %s\n", *pid, buf);
5453 return 0;
5454}
5455
5456static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5457 .start = saved_cmdlines_start,
5458 .next = saved_cmdlines_next,
5459 .stop = saved_cmdlines_stop,
5460 .show = saved_cmdlines_show,
5461};
5462
5463static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5464{
David Brazdil0f672f62019-12-10 10:32:29 +00005465 int ret;
5466
5467 ret = tracing_check_open_get_tr(NULL);
5468 if (ret)
5469 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005470
5471 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
5472}
5473
5474static const struct file_operations tracing_saved_cmdlines_fops = {
5475 .open = tracing_saved_cmdlines_open,
5476 .read = seq_read,
5477 .llseek = seq_lseek,
5478 .release = seq_release,
5479};
5480
5481static ssize_t
5482tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5483 size_t cnt, loff_t *ppos)
5484{
5485 char buf[64];
5486 int r;
5487
Olivier Deprez92d4c212022-12-06 15:05:30 +01005488 preempt_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005489 arch_spin_lock(&trace_cmdline_lock);
5490 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
5491 arch_spin_unlock(&trace_cmdline_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01005492 preempt_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005493
5494 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5495}
5496
5497static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5498{
5499 kfree(s->saved_cmdlines);
5500 kfree(s->map_cmdline_to_pid);
5501 kfree(s);
5502}
5503
5504static int tracing_resize_saved_cmdlines(unsigned int val)
5505{
5506 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5507
5508 s = kmalloc(sizeof(*s), GFP_KERNEL);
5509 if (!s)
5510 return -ENOMEM;
5511
5512 if (allocate_cmdlines_buffer(val, s) < 0) {
5513 kfree(s);
5514 return -ENOMEM;
5515 }
5516
Olivier Deprez92d4c212022-12-06 15:05:30 +01005517 preempt_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005518 arch_spin_lock(&trace_cmdline_lock);
5519 savedcmd_temp = savedcmd;
5520 savedcmd = s;
5521 arch_spin_unlock(&trace_cmdline_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01005522 preempt_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005523 free_saved_cmdlines_buffer(savedcmd_temp);
5524
5525 return 0;
5526}
5527
5528static ssize_t
5529tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5530 size_t cnt, loff_t *ppos)
5531{
5532 unsigned long val;
5533 int ret;
5534
5535 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5536 if (ret)
5537 return ret;
5538
5539 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5540 if (!val || val > PID_MAX_DEFAULT)
5541 return -EINVAL;
5542
5543 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5544 if (ret < 0)
5545 return ret;
5546
5547 *ppos += cnt;
5548
5549 return cnt;
5550}
5551
5552static const struct file_operations tracing_saved_cmdlines_size_fops = {
5553 .open = tracing_open_generic,
5554 .read = tracing_saved_cmdlines_size_read,
5555 .write = tracing_saved_cmdlines_size_write,
5556};
5557
5558#ifdef CONFIG_TRACE_EVAL_MAP_FILE
5559static union trace_eval_map_item *
5560update_eval_map(union trace_eval_map_item *ptr)
5561{
5562 if (!ptr->map.eval_string) {
5563 if (ptr->tail.next) {
5564 ptr = ptr->tail.next;
5565 /* Set ptr to the next real item (skip head) */
5566 ptr++;
5567 } else
5568 return NULL;
5569 }
5570 return ptr;
5571}
5572
5573static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
5574{
5575 union trace_eval_map_item *ptr = v;
5576
5577 /*
5578 * Paranoid! If ptr points to end, we don't want to increment past it.
5579 * This really should never happen.
5580 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005581 (*pos)++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005582 ptr = update_eval_map(ptr);
5583 if (WARN_ON_ONCE(!ptr))
5584 return NULL;
5585
5586 ptr++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005587 ptr = update_eval_map(ptr);
5588
5589 return ptr;
5590}
5591
5592static void *eval_map_start(struct seq_file *m, loff_t *pos)
5593{
5594 union trace_eval_map_item *v;
5595 loff_t l = 0;
5596
5597 mutex_lock(&trace_eval_mutex);
5598
5599 v = trace_eval_maps;
5600 if (v)
5601 v++;
5602
5603 while (v && l < *pos) {
5604 v = eval_map_next(m, v, &l);
5605 }
5606
5607 return v;
5608}
5609
5610static void eval_map_stop(struct seq_file *m, void *v)
5611{
5612 mutex_unlock(&trace_eval_mutex);
5613}
5614
5615static int eval_map_show(struct seq_file *m, void *v)
5616{
5617 union trace_eval_map_item *ptr = v;
5618
5619 seq_printf(m, "%s %ld (%s)\n",
5620 ptr->map.eval_string, ptr->map.eval_value,
5621 ptr->map.system);
5622
5623 return 0;
5624}
5625
5626static const struct seq_operations tracing_eval_map_seq_ops = {
5627 .start = eval_map_start,
5628 .next = eval_map_next,
5629 .stop = eval_map_stop,
5630 .show = eval_map_show,
5631};
5632
5633static int tracing_eval_map_open(struct inode *inode, struct file *filp)
5634{
David Brazdil0f672f62019-12-10 10:32:29 +00005635 int ret;
5636
5637 ret = tracing_check_open_get_tr(NULL);
5638 if (ret)
5639 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005640
5641 return seq_open(filp, &tracing_eval_map_seq_ops);
5642}
5643
5644static const struct file_operations tracing_eval_map_fops = {
5645 .open = tracing_eval_map_open,
5646 .read = seq_read,
5647 .llseek = seq_lseek,
5648 .release = seq_release,
5649};
5650
5651static inline union trace_eval_map_item *
5652trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
5653{
5654 /* Return tail of array given the head */
5655 return ptr + ptr->head.length + 1;
5656}
5657
5658static void
5659trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
5660 int len)
5661{
5662 struct trace_eval_map **stop;
5663 struct trace_eval_map **map;
5664 union trace_eval_map_item *map_array;
5665 union trace_eval_map_item *ptr;
5666
5667 stop = start + len;
5668
5669 /*
5670 * The trace_eval_maps contains the map plus a head and tail item,
5671 * where the head holds the module and length of array, and the
5672 * tail holds a pointer to the next list.
5673 */
5674 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
5675 if (!map_array) {
5676 pr_warn("Unable to allocate trace eval mapping\n");
5677 return;
5678 }
5679
5680 mutex_lock(&trace_eval_mutex);
5681
5682 if (!trace_eval_maps)
5683 trace_eval_maps = map_array;
5684 else {
5685 ptr = trace_eval_maps;
5686 for (;;) {
5687 ptr = trace_eval_jmp_to_tail(ptr);
5688 if (!ptr->tail.next)
5689 break;
5690 ptr = ptr->tail.next;
5691
5692 }
5693 ptr->tail.next = map_array;
5694 }
5695 map_array->head.mod = mod;
5696 map_array->head.length = len;
5697 map_array++;
5698
5699 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5700 map_array->map = **map;
5701 map_array++;
5702 }
5703 memset(map_array, 0, sizeof(*map_array));
5704
5705 mutex_unlock(&trace_eval_mutex);
5706}
5707
5708static void trace_create_eval_file(struct dentry *d_tracer)
5709{
5710 trace_create_file("eval_map", 0444, d_tracer,
5711 NULL, &tracing_eval_map_fops);
5712}
5713
5714#else /* CONFIG_TRACE_EVAL_MAP_FILE */
5715static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5716static inline void trace_insert_eval_map_file(struct module *mod,
5717 struct trace_eval_map **start, int len) { }
5718#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
5719
5720static void trace_insert_eval_map(struct module *mod,
5721 struct trace_eval_map **start, int len)
5722{
5723 struct trace_eval_map **map;
5724
5725 if (len <= 0)
5726 return;
5727
5728 map = start;
5729
5730 trace_event_eval_update(map, len);
5731
5732 trace_insert_eval_map_file(mod, start, len);
5733}
5734
5735static ssize_t
5736tracing_set_trace_read(struct file *filp, char __user *ubuf,
5737 size_t cnt, loff_t *ppos)
5738{
5739 struct trace_array *tr = filp->private_data;
5740 char buf[MAX_TRACER_SIZE+2];
5741 int r;
5742
5743 mutex_lock(&trace_types_lock);
5744 r = sprintf(buf, "%s\n", tr->current_trace->name);
5745 mutex_unlock(&trace_types_lock);
5746
5747 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5748}
5749
5750int tracer_init(struct tracer *t, struct trace_array *tr)
5751{
Olivier Deprez157378f2022-04-04 15:47:50 +02005752 tracing_reset_online_cpus(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005753 return t->init(tr);
5754}
5755
Olivier Deprez157378f2022-04-04 15:47:50 +02005756static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005757{
5758 int cpu;
5759
5760 for_each_tracing_cpu(cpu)
5761 per_cpu_ptr(buf->data, cpu)->entries = val;
5762}
5763
5764#ifdef CONFIG_TRACER_MAX_TRACE
5765/* resize @tr's buffer to the size of @size_tr's entries */
Olivier Deprez157378f2022-04-04 15:47:50 +02005766static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5767 struct array_buffer *size_buf, int cpu_id)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005768{
5769 int cpu, ret = 0;
5770
5771 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5772 for_each_tracing_cpu(cpu) {
5773 ret = ring_buffer_resize(trace_buf->buffer,
5774 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
5775 if (ret < 0)
5776 break;
5777 per_cpu_ptr(trace_buf->data, cpu)->entries =
5778 per_cpu_ptr(size_buf->data, cpu)->entries;
5779 }
5780 } else {
5781 ret = ring_buffer_resize(trace_buf->buffer,
5782 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
5783 if (ret == 0)
5784 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5785 per_cpu_ptr(size_buf->data, cpu_id)->entries;
5786 }
5787
5788 return ret;
5789}
5790#endif /* CONFIG_TRACER_MAX_TRACE */
5791
5792static int __tracing_resize_ring_buffer(struct trace_array *tr,
5793 unsigned long size, int cpu)
5794{
5795 int ret;
5796
5797 /*
5798 * If kernel or user changes the size of the ring buffer
5799 * we use the size that was given, and we can forget about
5800 * expanding it later.
5801 */
5802 ring_buffer_expanded = true;
5803
5804 /* May be called before buffers are initialized */
Olivier Deprez157378f2022-04-04 15:47:50 +02005805 if (!tr->array_buffer.buffer)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005806 return 0;
5807
Olivier Deprez157378f2022-04-04 15:47:50 +02005808 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005809 if (ret < 0)
5810 return ret;
5811
5812#ifdef CONFIG_TRACER_MAX_TRACE
5813 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5814 !tr->current_trace->use_max_tr)
5815 goto out;
5816
5817 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
5818 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02005819 int r = resize_buffer_duplicate_size(&tr->array_buffer,
5820 &tr->array_buffer, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005821 if (r < 0) {
5822 /*
5823 * AARGH! We are left with different
5824 * size max buffer!!!!
5825 * The max buffer is our "snapshot" buffer.
5826 * When a tracer needs a snapshot (one of the
5827 * latency tracers), it swaps the max buffer
5828 * with the saved snap shot. We succeeded to
5829 * update the size of the main buffer, but failed to
5830 * update the size of the max buffer. But when we tried
5831 * to reset the main buffer to the original size, we
5832 * failed there too. This is very unlikely to
5833 * happen, but if it does, warn and kill all
5834 * tracing.
5835 */
5836 WARN_ON(1);
5837 tracing_disabled = 1;
5838 }
5839 return ret;
5840 }
5841
5842 if (cpu == RING_BUFFER_ALL_CPUS)
5843 set_buffer_entries(&tr->max_buffer, size);
5844 else
5845 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
5846
5847 out:
5848#endif /* CONFIG_TRACER_MAX_TRACE */
5849
5850 if (cpu == RING_BUFFER_ALL_CPUS)
Olivier Deprez157378f2022-04-04 15:47:50 +02005851 set_buffer_entries(&tr->array_buffer, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005852 else
Olivier Deprez157378f2022-04-04 15:47:50 +02005853 per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005854
5855 return ret;
5856}
5857
Olivier Deprez157378f2022-04-04 15:47:50 +02005858ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5859 unsigned long size, int cpu_id)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005860{
5861 int ret = size;
5862
5863 mutex_lock(&trace_types_lock);
5864
5865 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5866 /* make sure, this cpu is enabled in the mask */
5867 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5868 ret = -EINVAL;
5869 goto out;
5870 }
5871 }
5872
5873 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
5874 if (ret < 0)
5875 ret = -ENOMEM;
5876
5877out:
5878 mutex_unlock(&trace_types_lock);
5879
5880 return ret;
5881}
5882
5883
5884/**
5885 * tracing_update_buffers - used by tracing facility to expand ring buffers
5886 *
5887 * To save on memory when the tracing is never used on a system with it
5888 * configured in. The ring buffers are set to a minimum size. But once
5889 * a user starts to use the tracing facility, then they need to grow
5890 * to their default size.
5891 *
5892 * This function is to be called when a tracer is about to be used.
5893 */
5894int tracing_update_buffers(void)
5895{
5896 int ret = 0;
5897
5898 mutex_lock(&trace_types_lock);
5899 if (!ring_buffer_expanded)
5900 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
5901 RING_BUFFER_ALL_CPUS);
5902 mutex_unlock(&trace_types_lock);
5903
5904 return ret;
5905}
5906
5907struct trace_option_dentry;
5908
5909static void
5910create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
5911
5912/*
5913 * Used to clear out the tracer before deletion of an instance.
5914 * Must have trace_types_lock held.
5915 */
5916static void tracing_set_nop(struct trace_array *tr)
5917{
5918 if (tr->current_trace == &nop_trace)
5919 return;
5920
5921 tr->current_trace->enabled--;
5922
5923 if (tr->current_trace->reset)
5924 tr->current_trace->reset(tr);
5925
5926 tr->current_trace = &nop_trace;
5927}
5928
Olivier Deprez92d4c212022-12-06 15:05:30 +01005929static bool tracer_options_updated;
5930
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005931static void add_tracer_options(struct trace_array *tr, struct tracer *t)
5932{
5933 /* Only enable if the directory has been created already. */
5934 if (!tr->dir)
5935 return;
5936
Olivier Deprez92d4c212022-12-06 15:05:30 +01005937 /* Only create trace option files after update_tracer_options finish */
5938 if (!tracer_options_updated)
5939 return;
5940
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005941 create_trace_option_files(tr, t);
5942}
5943
Olivier Deprez157378f2022-04-04 15:47:50 +02005944int tracing_set_tracer(struct trace_array *tr, const char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005945{
5946 struct tracer *t;
5947#ifdef CONFIG_TRACER_MAX_TRACE
5948 bool had_max_tr;
5949#endif
5950 int ret = 0;
5951
5952 mutex_lock(&trace_types_lock);
5953
5954 if (!ring_buffer_expanded) {
5955 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
5956 RING_BUFFER_ALL_CPUS);
5957 if (ret < 0)
5958 goto out;
5959 ret = 0;
5960 }
5961
5962 for (t = trace_types; t; t = t->next) {
5963 if (strcmp(t->name, buf) == 0)
5964 break;
5965 }
5966 if (!t) {
5967 ret = -EINVAL;
5968 goto out;
5969 }
5970 if (t == tr->current_trace)
5971 goto out;
5972
David Brazdil0f672f62019-12-10 10:32:29 +00005973#ifdef CONFIG_TRACER_SNAPSHOT
5974 if (t->use_max_tr) {
Olivier Deprez92d4c212022-12-06 15:05:30 +01005975 local_irq_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00005976 arch_spin_lock(&tr->max_lock);
5977 if (tr->cond_snapshot)
5978 ret = -EBUSY;
5979 arch_spin_unlock(&tr->max_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01005980 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00005981 if (ret)
5982 goto out;
5983 }
5984#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005985 /* Some tracers won't work on kernel command line */
5986 if (system_state < SYSTEM_RUNNING && t->noboot) {
5987 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5988 t->name);
5989 goto out;
5990 }
5991
5992 /* Some tracers are only allowed for the top level buffer */
5993 if (!trace_ok_for_array(t, tr)) {
5994 ret = -EINVAL;
5995 goto out;
5996 }
5997
5998 /* If trace pipe files are being read, we can't change the tracer */
Olivier Deprez0e641232021-09-23 10:07:05 +02005999 if (tr->trace_ref) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006000 ret = -EBUSY;
6001 goto out;
6002 }
6003
6004 trace_branch_disable();
6005
6006 tr->current_trace->enabled--;
6007
6008 if (tr->current_trace->reset)
6009 tr->current_trace->reset(tr);
6010
Olivier Deprez92d4c212022-12-06 15:05:30 +01006011#ifdef CONFIG_TRACER_MAX_TRACE
6012 had_max_tr = tr->current_trace->use_max_tr;
6013
David Brazdil0f672f62019-12-10 10:32:29 +00006014 /* Current trace needs to be nop_trace before synchronize_rcu */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006015 tr->current_trace = &nop_trace;
6016
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006017 if (had_max_tr && !t->use_max_tr) {
6018 /*
6019 * We need to make sure that the update_max_tr sees that
6020 * current_trace changed to nop_trace to keep it from
6021 * swapping the buffers after we resize it.
6022 * The update_max_tr is called from interrupts disabled
6023 * so a synchronized_sched() is sufficient.
6024 */
David Brazdil0f672f62019-12-10 10:32:29 +00006025 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006026 free_snapshot(tr);
6027 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006028
Olivier Deprez92d4c212022-12-06 15:05:30 +01006029 if (t->use_max_tr && !tr->allocated_snapshot) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006030 ret = tracing_alloc_snapshot_instance(tr);
6031 if (ret < 0)
6032 goto out;
6033 }
Olivier Deprez92d4c212022-12-06 15:05:30 +01006034#else
6035 tr->current_trace = &nop_trace;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006036#endif
6037
6038 if (t->init) {
6039 ret = tracer_init(t, tr);
6040 if (ret)
6041 goto out;
6042 }
6043
6044 tr->current_trace = t;
6045 tr->current_trace->enabled++;
6046 trace_branch_enable(tr);
6047 out:
6048 mutex_unlock(&trace_types_lock);
6049
6050 return ret;
6051}
6052
6053static ssize_t
6054tracing_set_trace_write(struct file *filp, const char __user *ubuf,
6055 size_t cnt, loff_t *ppos)
6056{
6057 struct trace_array *tr = filp->private_data;
6058 char buf[MAX_TRACER_SIZE+1];
6059 int i;
6060 size_t ret;
6061 int err;
6062
6063 ret = cnt;
6064
6065 if (cnt > MAX_TRACER_SIZE)
6066 cnt = MAX_TRACER_SIZE;
6067
6068 if (copy_from_user(buf, ubuf, cnt))
6069 return -EFAULT;
6070
6071 buf[cnt] = 0;
6072
6073 /* strip ending whitespace. */
6074 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
6075 buf[i] = 0;
6076
6077 err = tracing_set_tracer(tr, buf);
6078 if (err)
6079 return err;
6080
6081 *ppos += ret;
6082
6083 return ret;
6084}
6085
6086static ssize_t
6087tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
6088 size_t cnt, loff_t *ppos)
6089{
6090 char buf[64];
6091 int r;
6092
6093 r = snprintf(buf, sizeof(buf), "%ld\n",
6094 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
6095 if (r > sizeof(buf))
6096 r = sizeof(buf);
6097 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6098}
6099
6100static ssize_t
6101tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
6102 size_t cnt, loff_t *ppos)
6103{
6104 unsigned long val;
6105 int ret;
6106
6107 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6108 if (ret)
6109 return ret;
6110
6111 *ptr = val * 1000;
6112
6113 return cnt;
6114}
6115
6116static ssize_t
6117tracing_thresh_read(struct file *filp, char __user *ubuf,
6118 size_t cnt, loff_t *ppos)
6119{
6120 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
6121}
6122
6123static ssize_t
6124tracing_thresh_write(struct file *filp, const char __user *ubuf,
6125 size_t cnt, loff_t *ppos)
6126{
6127 struct trace_array *tr = filp->private_data;
6128 int ret;
6129
6130 mutex_lock(&trace_types_lock);
6131 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
6132 if (ret < 0)
6133 goto out;
6134
6135 if (tr->current_trace->update_thresh) {
6136 ret = tr->current_trace->update_thresh(tr);
6137 if (ret < 0)
6138 goto out;
6139 }
6140
6141 ret = cnt;
6142out:
6143 mutex_unlock(&trace_types_lock);
6144
6145 return ret;
6146}
6147
6148#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
6149
6150static ssize_t
6151tracing_max_lat_read(struct file *filp, char __user *ubuf,
6152 size_t cnt, loff_t *ppos)
6153{
6154 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
6155}
6156
6157static ssize_t
6158tracing_max_lat_write(struct file *filp, const char __user *ubuf,
6159 size_t cnt, loff_t *ppos)
6160{
6161 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
6162}
6163
6164#endif
6165
6166static int tracing_open_pipe(struct inode *inode, struct file *filp)
6167{
6168 struct trace_array *tr = inode->i_private;
6169 struct trace_iterator *iter;
David Brazdil0f672f62019-12-10 10:32:29 +00006170 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006171
David Brazdil0f672f62019-12-10 10:32:29 +00006172 ret = tracing_check_open_get_tr(tr);
6173 if (ret)
6174 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006175
6176 mutex_lock(&trace_types_lock);
6177
6178 /* create a buffer to store the information to pass to userspace */
6179 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6180 if (!iter) {
6181 ret = -ENOMEM;
6182 __trace_array_put(tr);
6183 goto out;
6184 }
6185
6186 trace_seq_init(&iter->seq);
6187 iter->trace = tr->current_trace;
6188
6189 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
6190 ret = -ENOMEM;
6191 goto fail;
6192 }
6193
6194 /* trace pipe does not show start of buffer */
6195 cpumask_setall(iter->started);
6196
6197 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
6198 iter->iter_flags |= TRACE_FILE_LAT_FMT;
6199
6200 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6201 if (trace_clocks[tr->clock_id].in_ns)
6202 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6203
6204 iter->tr = tr;
Olivier Deprez157378f2022-04-04 15:47:50 +02006205 iter->array_buffer = &tr->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006206 iter->cpu_file = tracing_get_cpu(inode);
6207 mutex_init(&iter->mutex);
6208 filp->private_data = iter;
6209
6210 if (iter->trace->pipe_open)
6211 iter->trace->pipe_open(iter);
6212
6213 nonseekable_open(inode, filp);
6214
Olivier Deprez0e641232021-09-23 10:07:05 +02006215 tr->trace_ref++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006216out:
6217 mutex_unlock(&trace_types_lock);
6218 return ret;
6219
6220fail:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006221 kfree(iter);
6222 __trace_array_put(tr);
6223 mutex_unlock(&trace_types_lock);
6224 return ret;
6225}
6226
6227static int tracing_release_pipe(struct inode *inode, struct file *file)
6228{
6229 struct trace_iterator *iter = file->private_data;
6230 struct trace_array *tr = inode->i_private;
6231
6232 mutex_lock(&trace_types_lock);
6233
Olivier Deprez0e641232021-09-23 10:07:05 +02006234 tr->trace_ref--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006235
6236 if (iter->trace->pipe_close)
6237 iter->trace->pipe_close(iter);
6238
6239 mutex_unlock(&trace_types_lock);
6240
6241 free_cpumask_var(iter->started);
6242 mutex_destroy(&iter->mutex);
6243 kfree(iter);
6244
6245 trace_array_put(tr);
6246
6247 return 0;
6248}
6249
6250static __poll_t
6251trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
6252{
6253 struct trace_array *tr = iter->tr;
6254
6255 /* Iterators are static, they should be filled or empty */
6256 if (trace_buffer_iter(iter, iter->cpu_file))
6257 return EPOLLIN | EPOLLRDNORM;
6258
6259 if (tr->trace_flags & TRACE_ITER_BLOCK)
6260 /*
6261 * Always select as readable when in blocking mode
6262 */
6263 return EPOLLIN | EPOLLRDNORM;
6264 else
Olivier Deprez157378f2022-04-04 15:47:50 +02006265 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
Olivier Deprez92d4c212022-12-06 15:05:30 +01006266 filp, poll_table, iter->tr->buffer_percent);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006267}
6268
6269static __poll_t
6270tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6271{
6272 struct trace_iterator *iter = filp->private_data;
6273
6274 return trace_poll(iter, filp, poll_table);
6275}
6276
6277/* Must be called with iter->mutex held. */
6278static int tracing_wait_pipe(struct file *filp)
6279{
6280 struct trace_iterator *iter = filp->private_data;
6281 int ret;
6282
6283 while (trace_empty(iter)) {
6284
6285 if ((filp->f_flags & O_NONBLOCK)) {
6286 return -EAGAIN;
6287 }
6288
6289 /*
6290 * We block until we read something and tracing is disabled.
6291 * We still block if tracing is disabled, but we have never
6292 * read anything. This allows a user to cat this file, and
6293 * then enable tracing. But after we have read something,
6294 * we give an EOF when tracing is again disabled.
6295 *
6296 * iter->pos will be 0 if we haven't read anything.
6297 */
6298 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
6299 break;
6300
6301 mutex_unlock(&iter->mutex);
6302
David Brazdil0f672f62019-12-10 10:32:29 +00006303 ret = wait_on_pipe(iter, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006304
6305 mutex_lock(&iter->mutex);
6306
6307 if (ret)
6308 return ret;
6309 }
6310
6311 return 1;
6312}
6313
6314/*
6315 * Consumer reader.
6316 */
6317static ssize_t
6318tracing_read_pipe(struct file *filp, char __user *ubuf,
6319 size_t cnt, loff_t *ppos)
6320{
6321 struct trace_iterator *iter = filp->private_data;
6322 ssize_t sret;
6323
6324 /*
6325 * Avoid more than one consumer on a single file descriptor
6326 * This is just a matter of traces coherency, the ring buffer itself
6327 * is protected.
6328 */
6329 mutex_lock(&iter->mutex);
6330
6331 /* return any leftover data */
6332 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6333 if (sret != -EBUSY)
6334 goto out;
6335
6336 trace_seq_init(&iter->seq);
6337
6338 if (iter->trace->read) {
6339 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6340 if (sret)
6341 goto out;
6342 }
6343
6344waitagain:
6345 sret = tracing_wait_pipe(filp);
6346 if (sret <= 0)
6347 goto out;
6348
6349 /* stop when tracing is finished */
6350 if (trace_empty(iter)) {
6351 sret = 0;
6352 goto out;
6353 }
6354
6355 if (cnt >= PAGE_SIZE)
6356 cnt = PAGE_SIZE - 1;
6357
6358 /* reset all but tr, trace, and overruns */
6359 memset(&iter->seq, 0,
6360 sizeof(struct trace_iterator) -
6361 offsetof(struct trace_iterator, seq));
6362 cpumask_clear(iter->started);
David Brazdil0f672f62019-12-10 10:32:29 +00006363 trace_seq_init(&iter->seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006364 iter->pos = -1;
6365
6366 trace_event_read_lock();
6367 trace_access_lock(iter->cpu_file);
6368 while (trace_find_next_entry_inc(iter) != NULL) {
6369 enum print_line_t ret;
6370 int save_len = iter->seq.seq.len;
6371
6372 ret = print_trace_line(iter);
6373 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6374 /* don't print partial lines */
6375 iter->seq.seq.len = save_len;
6376 break;
6377 }
6378 if (ret != TRACE_TYPE_NO_CONSUME)
6379 trace_consume(iter);
6380
6381 if (trace_seq_used(&iter->seq) >= cnt)
6382 break;
6383
6384 /*
6385 * Setting the full flag means we reached the trace_seq buffer
6386 * size and we should leave by partial output condition above.
6387 * One of the trace_seq_* functions is not used properly.
6388 */
6389 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6390 iter->ent->type);
6391 }
6392 trace_access_unlock(iter->cpu_file);
6393 trace_event_read_unlock();
6394
6395 /* Now copy what we have to the user */
6396 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6397 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
6398 trace_seq_init(&iter->seq);
6399
6400 /*
6401 * If there was nothing to send to user, in spite of consuming trace
6402 * entries, go back to wait for more entries.
6403 */
6404 if (sret == -EBUSY)
6405 goto waitagain;
6406
6407out:
6408 mutex_unlock(&iter->mutex);
6409
6410 return sret;
6411}
6412
6413static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6414 unsigned int idx)
6415{
6416 __free_page(spd->pages[idx]);
6417}
6418
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006419static size_t
6420tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
6421{
6422 size_t count;
6423 int save_len;
6424 int ret;
6425
6426 /* Seq buffer is page-sized, exactly what we need. */
6427 for (;;) {
6428 save_len = iter->seq.seq.len;
6429 ret = print_trace_line(iter);
6430
6431 if (trace_seq_has_overflowed(&iter->seq)) {
6432 iter->seq.seq.len = save_len;
6433 break;
6434 }
6435
6436 /*
6437 * This should not be hit, because it should only
6438 * be set if the iter->seq overflowed. But check it
6439 * anyway to be safe.
6440 */
6441 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6442 iter->seq.seq.len = save_len;
6443 break;
6444 }
6445
6446 count = trace_seq_used(&iter->seq) - save_len;
6447 if (rem < count) {
6448 rem = 0;
6449 iter->seq.seq.len = save_len;
6450 break;
6451 }
6452
6453 if (ret != TRACE_TYPE_NO_CONSUME)
6454 trace_consume(iter);
6455 rem -= count;
6456 if (!trace_find_next_entry_inc(iter)) {
6457 rem = 0;
6458 iter->ent = NULL;
6459 break;
6460 }
6461 }
6462
6463 return rem;
6464}
6465
6466static ssize_t tracing_splice_read_pipe(struct file *filp,
6467 loff_t *ppos,
6468 struct pipe_inode_info *pipe,
6469 size_t len,
6470 unsigned int flags)
6471{
6472 struct page *pages_def[PIPE_DEF_BUFFERS];
6473 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6474 struct trace_iterator *iter = filp->private_data;
6475 struct splice_pipe_desc spd = {
6476 .pages = pages_def,
6477 .partial = partial_def,
6478 .nr_pages = 0, /* This gets updated below. */
6479 .nr_pages_max = PIPE_DEF_BUFFERS,
Olivier Deprez157378f2022-04-04 15:47:50 +02006480 .ops = &default_pipe_buf_ops,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006481 .spd_release = tracing_spd_release_pipe,
6482 };
6483 ssize_t ret;
6484 size_t rem;
6485 unsigned int i;
6486
6487 if (splice_grow_spd(pipe, &spd))
6488 return -ENOMEM;
6489
6490 mutex_lock(&iter->mutex);
6491
6492 if (iter->trace->splice_read) {
6493 ret = iter->trace->splice_read(iter, filp,
6494 ppos, pipe, len, flags);
6495 if (ret)
6496 goto out_err;
6497 }
6498
6499 ret = tracing_wait_pipe(filp);
6500 if (ret <= 0)
6501 goto out_err;
6502
6503 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
6504 ret = -EFAULT;
6505 goto out_err;
6506 }
6507
6508 trace_event_read_lock();
6509 trace_access_lock(iter->cpu_file);
6510
6511 /* Fill as many pages as possible. */
6512 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
6513 spd.pages[i] = alloc_page(GFP_KERNEL);
6514 if (!spd.pages[i])
6515 break;
6516
6517 rem = tracing_fill_pipe_page(rem, iter);
6518
6519 /* Copy the data into the page, so we can start over. */
6520 ret = trace_seq_to_buffer(&iter->seq,
6521 page_address(spd.pages[i]),
6522 trace_seq_used(&iter->seq));
6523 if (ret < 0) {
6524 __free_page(spd.pages[i]);
6525 break;
6526 }
6527 spd.partial[i].offset = 0;
6528 spd.partial[i].len = trace_seq_used(&iter->seq);
6529
6530 trace_seq_init(&iter->seq);
6531 }
6532
6533 trace_access_unlock(iter->cpu_file);
6534 trace_event_read_unlock();
6535 mutex_unlock(&iter->mutex);
6536
6537 spd.nr_pages = i;
6538
6539 if (i)
6540 ret = splice_to_pipe(pipe, &spd);
6541 else
6542 ret = 0;
6543out:
6544 splice_shrink_spd(&spd);
6545 return ret;
6546
6547out_err:
6548 mutex_unlock(&iter->mutex);
6549 goto out;
6550}
6551
6552static ssize_t
6553tracing_entries_read(struct file *filp, char __user *ubuf,
6554 size_t cnt, loff_t *ppos)
6555{
6556 struct inode *inode = file_inode(filp);
6557 struct trace_array *tr = inode->i_private;
6558 int cpu = tracing_get_cpu(inode);
6559 char buf[64];
6560 int r = 0;
6561 ssize_t ret;
6562
6563 mutex_lock(&trace_types_lock);
6564
6565 if (cpu == RING_BUFFER_ALL_CPUS) {
6566 int cpu, buf_size_same;
6567 unsigned long size;
6568
6569 size = 0;
6570 buf_size_same = 1;
6571 /* check if all cpu sizes are same */
6572 for_each_tracing_cpu(cpu) {
6573 /* fill in the size from first enabled cpu */
6574 if (size == 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02006575 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6576 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006577 buf_size_same = 0;
6578 break;
6579 }
6580 }
6581
6582 if (buf_size_same) {
6583 if (!ring_buffer_expanded)
6584 r = sprintf(buf, "%lu (expanded: %lu)\n",
6585 size >> 10,
6586 trace_buf_size >> 10);
6587 else
6588 r = sprintf(buf, "%lu\n", size >> 10);
6589 } else
6590 r = sprintf(buf, "X\n");
6591 } else
Olivier Deprez157378f2022-04-04 15:47:50 +02006592 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006593
6594 mutex_unlock(&trace_types_lock);
6595
6596 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6597 return ret;
6598}
6599
6600static ssize_t
6601tracing_entries_write(struct file *filp, const char __user *ubuf,
6602 size_t cnt, loff_t *ppos)
6603{
6604 struct inode *inode = file_inode(filp);
6605 struct trace_array *tr = inode->i_private;
6606 unsigned long val;
6607 int ret;
6608
6609 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6610 if (ret)
6611 return ret;
6612
6613 /* must have at least 1 entry */
6614 if (!val)
6615 return -EINVAL;
6616
6617 /* value is in KB */
6618 val <<= 10;
6619 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
6620 if (ret < 0)
6621 return ret;
6622
6623 *ppos += cnt;
6624
6625 return cnt;
6626}
6627
6628static ssize_t
6629tracing_total_entries_read(struct file *filp, char __user *ubuf,
6630 size_t cnt, loff_t *ppos)
6631{
6632 struct trace_array *tr = filp->private_data;
6633 char buf[64];
6634 int r, cpu;
6635 unsigned long size = 0, expanded_size = 0;
6636
6637 mutex_lock(&trace_types_lock);
6638 for_each_tracing_cpu(cpu) {
Olivier Deprez157378f2022-04-04 15:47:50 +02006639 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006640 if (!ring_buffer_expanded)
6641 expanded_size += trace_buf_size >> 10;
6642 }
6643 if (ring_buffer_expanded)
6644 r = sprintf(buf, "%lu\n", size);
6645 else
6646 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6647 mutex_unlock(&trace_types_lock);
6648
6649 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6650}
6651
6652static ssize_t
6653tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6654 size_t cnt, loff_t *ppos)
6655{
6656 /*
6657 * There is no need to read what the user has written, this function
6658 * is just to make sure that there is no error when "echo" is used
6659 */
6660
6661 *ppos += cnt;
6662
6663 return cnt;
6664}
6665
6666static int
6667tracing_free_buffer_release(struct inode *inode, struct file *filp)
6668{
6669 struct trace_array *tr = inode->i_private;
6670
6671 /* disable tracing ? */
6672 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
6673 tracer_tracing_off(tr);
6674 /* resize the ring buffer to 0 */
6675 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
6676
6677 trace_array_put(tr);
6678
6679 return 0;
6680}
6681
6682static ssize_t
6683tracing_mark_write(struct file *filp, const char __user *ubuf,
6684 size_t cnt, loff_t *fpos)
6685{
6686 struct trace_array *tr = filp->private_data;
6687 struct ring_buffer_event *event;
6688 enum event_trigger_type tt = ETT_NONE;
Olivier Deprez157378f2022-04-04 15:47:50 +02006689 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006690 struct print_entry *entry;
6691 unsigned long irq_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006692 ssize_t written;
6693 int size;
6694 int len;
6695
6696/* Used in tracing_mark_raw_write() as well */
David Brazdil0f672f62019-12-10 10:32:29 +00006697#define FAULTED_STR "<faulted>"
6698#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006699
6700 if (tracing_disabled)
6701 return -EINVAL;
6702
6703 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6704 return -EINVAL;
6705
6706 if (cnt > TRACE_BUF_SIZE)
6707 cnt = TRACE_BUF_SIZE;
6708
6709 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6710
6711 local_save_flags(irq_flags);
6712 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6713
6714 /* If less than "<faulted>", then make sure we can still add that */
6715 if (cnt < FAULTED_SIZE)
6716 size += FAULTED_SIZE - cnt;
6717
Olivier Deprez157378f2022-04-04 15:47:50 +02006718 buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006719 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6720 irq_flags, preempt_count());
6721 if (unlikely(!event))
6722 /* Ring buffer disabled, return as if not open for write */
6723 return -EBADF;
6724
6725 entry = ring_buffer_event_data(event);
6726 entry->ip = _THIS_IP_;
6727
6728 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6729 if (len) {
David Brazdil0f672f62019-12-10 10:32:29 +00006730 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006731 cnt = FAULTED_SIZE;
6732 written = -EFAULT;
6733 } else
6734 written = cnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006735
6736 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6737 /* do not add \n before testing triggers, but add \0 */
6738 entry->buf[cnt] = '\0';
6739 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6740 }
6741
6742 if (entry->buf[cnt - 1] != '\n') {
6743 entry->buf[cnt] = '\n';
6744 entry->buf[cnt + 1] = '\0';
6745 } else
6746 entry->buf[cnt] = '\0';
6747
Olivier Deprez157378f2022-04-04 15:47:50 +02006748 if (static_branch_unlikely(&trace_marker_exports_enabled))
6749 ftrace_exports(event, TRACE_EXPORT_MARKER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006750 __buffer_unlock_commit(buffer, event);
6751
6752 if (tt)
6753 event_triggers_post_call(tr->trace_marker_file, tt);
6754
6755 if (written > 0)
6756 *fpos += written;
6757
6758 return written;
6759}
6760
6761/* Limit it for now to 3K (including tag) */
6762#define RAW_DATA_MAX_SIZE (1024*3)
6763
6764static ssize_t
6765tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6766 size_t cnt, loff_t *fpos)
6767{
6768 struct trace_array *tr = filp->private_data;
6769 struct ring_buffer_event *event;
Olivier Deprez157378f2022-04-04 15:47:50 +02006770 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006771 struct raw_data_entry *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006772 unsigned long irq_flags;
6773 ssize_t written;
6774 int size;
6775 int len;
6776
6777#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6778
6779 if (tracing_disabled)
6780 return -EINVAL;
6781
6782 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6783 return -EINVAL;
6784
6785 /* The marker must at least have a tag id */
6786 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6787 return -EINVAL;
6788
6789 if (cnt > TRACE_BUF_SIZE)
6790 cnt = TRACE_BUF_SIZE;
6791
6792 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6793
6794 local_save_flags(irq_flags);
6795 size = sizeof(*entry) + cnt;
6796 if (cnt < FAULT_SIZE_ID)
6797 size += FAULT_SIZE_ID - cnt;
6798
Olivier Deprez157378f2022-04-04 15:47:50 +02006799 buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006800 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6801 irq_flags, preempt_count());
6802 if (!event)
6803 /* Ring buffer disabled, return as if not open for write */
6804 return -EBADF;
6805
6806 entry = ring_buffer_event_data(event);
6807
6808 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6809 if (len) {
6810 entry->id = -1;
David Brazdil0f672f62019-12-10 10:32:29 +00006811 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006812 written = -EFAULT;
6813 } else
6814 written = cnt;
6815
6816 __buffer_unlock_commit(buffer, event);
6817
6818 if (written > 0)
6819 *fpos += written;
6820
6821 return written;
6822}
6823
6824static int tracing_clock_show(struct seq_file *m, void *v)
6825{
6826 struct trace_array *tr = m->private;
6827 int i;
6828
6829 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
6830 seq_printf(m,
6831 "%s%s%s%s", i ? " " : "",
6832 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6833 i == tr->clock_id ? "]" : "");
6834 seq_putc(m, '\n');
6835
6836 return 0;
6837}
6838
6839int tracing_set_clock(struct trace_array *tr, const char *clockstr)
6840{
6841 int i;
6842
6843 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6844 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6845 break;
6846 }
6847 if (i == ARRAY_SIZE(trace_clocks))
6848 return -EINVAL;
6849
6850 mutex_lock(&trace_types_lock);
6851
6852 tr->clock_id = i;
6853
Olivier Deprez157378f2022-04-04 15:47:50 +02006854 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006855
6856 /*
6857 * New clock may not be consistent with the previous clock.
6858 * Reset the buffer so that it doesn't have incomparable timestamps.
6859 */
Olivier Deprez157378f2022-04-04 15:47:50 +02006860 tracing_reset_online_cpus(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006861
6862#ifdef CONFIG_TRACER_MAX_TRACE
6863 if (tr->max_buffer.buffer)
6864 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
6865 tracing_reset_online_cpus(&tr->max_buffer);
6866#endif
6867
6868 mutex_unlock(&trace_types_lock);
6869
6870 return 0;
6871}
6872
6873static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6874 size_t cnt, loff_t *fpos)
6875{
6876 struct seq_file *m = filp->private_data;
6877 struct trace_array *tr = m->private;
6878 char buf[64];
6879 const char *clockstr;
6880 int ret;
6881
6882 if (cnt >= sizeof(buf))
6883 return -EINVAL;
6884
6885 if (copy_from_user(buf, ubuf, cnt))
6886 return -EFAULT;
6887
6888 buf[cnt] = 0;
6889
6890 clockstr = strstrip(buf);
6891
6892 ret = tracing_set_clock(tr, clockstr);
6893 if (ret)
6894 return ret;
6895
6896 *fpos += cnt;
6897
6898 return cnt;
6899}
6900
6901static int tracing_clock_open(struct inode *inode, struct file *file)
6902{
6903 struct trace_array *tr = inode->i_private;
6904 int ret;
6905
David Brazdil0f672f62019-12-10 10:32:29 +00006906 ret = tracing_check_open_get_tr(tr);
6907 if (ret)
6908 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006909
6910 ret = single_open(file, tracing_clock_show, inode->i_private);
6911 if (ret < 0)
6912 trace_array_put(tr);
6913
6914 return ret;
6915}
6916
6917static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6918{
6919 struct trace_array *tr = m->private;
6920
6921 mutex_lock(&trace_types_lock);
6922
Olivier Deprez157378f2022-04-04 15:47:50 +02006923 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006924 seq_puts(m, "delta [absolute]\n");
6925 else
6926 seq_puts(m, "[delta] absolute\n");
6927
6928 mutex_unlock(&trace_types_lock);
6929
6930 return 0;
6931}
6932
6933static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6934{
6935 struct trace_array *tr = inode->i_private;
6936 int ret;
6937
David Brazdil0f672f62019-12-10 10:32:29 +00006938 ret = tracing_check_open_get_tr(tr);
6939 if (ret)
6940 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006941
6942 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6943 if (ret < 0)
6944 trace_array_put(tr);
6945
6946 return ret;
6947}
6948
6949int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6950{
6951 int ret = 0;
6952
6953 mutex_lock(&trace_types_lock);
6954
6955 if (abs && tr->time_stamp_abs_ref++)
6956 goto out;
6957
6958 if (!abs) {
6959 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6960 ret = -EINVAL;
6961 goto out;
6962 }
6963
6964 if (--tr->time_stamp_abs_ref)
6965 goto out;
6966 }
6967
Olivier Deprez157378f2022-04-04 15:47:50 +02006968 ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006969
6970#ifdef CONFIG_TRACER_MAX_TRACE
6971 if (tr->max_buffer.buffer)
6972 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6973#endif
6974 out:
6975 mutex_unlock(&trace_types_lock);
6976
6977 return ret;
6978}
6979
6980struct ftrace_buffer_info {
6981 struct trace_iterator iter;
6982 void *spare;
6983 unsigned int spare_cpu;
6984 unsigned int read;
6985};
6986
6987#ifdef CONFIG_TRACER_SNAPSHOT
6988static int tracing_snapshot_open(struct inode *inode, struct file *file)
6989{
6990 struct trace_array *tr = inode->i_private;
6991 struct trace_iterator *iter;
6992 struct seq_file *m;
David Brazdil0f672f62019-12-10 10:32:29 +00006993 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006994
David Brazdil0f672f62019-12-10 10:32:29 +00006995 ret = tracing_check_open_get_tr(tr);
6996 if (ret)
6997 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006998
6999 if (file->f_mode & FMODE_READ) {
7000 iter = __tracing_open(inode, file, true);
7001 if (IS_ERR(iter))
7002 ret = PTR_ERR(iter);
7003 } else {
7004 /* Writes still need the seq_file to hold the private data */
7005 ret = -ENOMEM;
7006 m = kzalloc(sizeof(*m), GFP_KERNEL);
7007 if (!m)
7008 goto out;
7009 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
7010 if (!iter) {
7011 kfree(m);
7012 goto out;
7013 }
7014 ret = 0;
7015
7016 iter->tr = tr;
Olivier Deprez157378f2022-04-04 15:47:50 +02007017 iter->array_buffer = &tr->max_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007018 iter->cpu_file = tracing_get_cpu(inode);
7019 m->private = iter;
7020 file->private_data = m;
7021 }
7022out:
7023 if (ret < 0)
7024 trace_array_put(tr);
7025
7026 return ret;
7027}
7028
7029static ssize_t
7030tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
7031 loff_t *ppos)
7032{
7033 struct seq_file *m = filp->private_data;
7034 struct trace_iterator *iter = m->private;
7035 struct trace_array *tr = iter->tr;
7036 unsigned long val;
7037 int ret;
7038
7039 ret = tracing_update_buffers();
7040 if (ret < 0)
7041 return ret;
7042
7043 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7044 if (ret)
7045 return ret;
7046
7047 mutex_lock(&trace_types_lock);
7048
7049 if (tr->current_trace->use_max_tr) {
7050 ret = -EBUSY;
7051 goto out;
7052 }
7053
Olivier Deprez92d4c212022-12-06 15:05:30 +01007054 local_irq_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00007055 arch_spin_lock(&tr->max_lock);
7056 if (tr->cond_snapshot)
7057 ret = -EBUSY;
7058 arch_spin_unlock(&tr->max_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01007059 local_irq_enable();
David Brazdil0f672f62019-12-10 10:32:29 +00007060 if (ret)
7061 goto out;
7062
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007063 switch (val) {
7064 case 0:
7065 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
7066 ret = -EINVAL;
7067 break;
7068 }
7069 if (tr->allocated_snapshot)
7070 free_snapshot(tr);
7071 break;
7072 case 1:
7073/* Only allow per-cpu swap if the ring buffer supports it */
7074#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
7075 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
7076 ret = -EINVAL;
7077 break;
7078 }
7079#endif
David Brazdil0f672f62019-12-10 10:32:29 +00007080 if (tr->allocated_snapshot)
7081 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Olivier Deprez157378f2022-04-04 15:47:50 +02007082 &tr->array_buffer, iter->cpu_file);
David Brazdil0f672f62019-12-10 10:32:29 +00007083 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007084 ret = tracing_alloc_snapshot_instance(tr);
David Brazdil0f672f62019-12-10 10:32:29 +00007085 if (ret < 0)
7086 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007087 local_irq_disable();
7088 /* Now, we're going to swap */
7089 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
David Brazdil0f672f62019-12-10 10:32:29 +00007090 update_max_tr(tr, current, smp_processor_id(), NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007091 else
7092 update_max_tr_single(tr, current, iter->cpu_file);
7093 local_irq_enable();
7094 break;
7095 default:
7096 if (tr->allocated_snapshot) {
7097 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
7098 tracing_reset_online_cpus(&tr->max_buffer);
7099 else
David Brazdil0f672f62019-12-10 10:32:29 +00007100 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007101 }
7102 break;
7103 }
7104
7105 if (ret >= 0) {
7106 *ppos += cnt;
7107 ret = cnt;
7108 }
7109out:
7110 mutex_unlock(&trace_types_lock);
7111 return ret;
7112}
7113
7114static int tracing_snapshot_release(struct inode *inode, struct file *file)
7115{
7116 struct seq_file *m = file->private_data;
7117 int ret;
7118
7119 ret = tracing_release(inode, file);
7120
7121 if (file->f_mode & FMODE_READ)
7122 return ret;
7123
7124 /* If write only, the seq_file is just a stub */
7125 if (m)
7126 kfree(m->private);
7127 kfree(m);
7128
7129 return 0;
7130}
7131
7132static int tracing_buffers_open(struct inode *inode, struct file *filp);
7133static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
7134 size_t count, loff_t *ppos);
7135static int tracing_buffers_release(struct inode *inode, struct file *file);
7136static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7137 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
7138
7139static int snapshot_raw_open(struct inode *inode, struct file *filp)
7140{
7141 struct ftrace_buffer_info *info;
7142 int ret;
7143
David Brazdil0f672f62019-12-10 10:32:29 +00007144 /* The following checks for tracefs lockdown */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007145 ret = tracing_buffers_open(inode, filp);
7146 if (ret < 0)
7147 return ret;
7148
7149 info = filp->private_data;
7150
7151 if (info->iter.trace->use_max_tr) {
7152 tracing_buffers_release(inode, filp);
7153 return -EBUSY;
7154 }
7155
7156 info->iter.snapshot = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02007157 info->iter.array_buffer = &info->iter.tr->max_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007158
7159 return ret;
7160}
7161
7162#endif /* CONFIG_TRACER_SNAPSHOT */
7163
7164
7165static const struct file_operations tracing_thresh_fops = {
7166 .open = tracing_open_generic,
7167 .read = tracing_thresh_read,
7168 .write = tracing_thresh_write,
7169 .llseek = generic_file_llseek,
7170};
7171
7172#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
7173static const struct file_operations tracing_max_lat_fops = {
7174 .open = tracing_open_generic,
7175 .read = tracing_max_lat_read,
7176 .write = tracing_max_lat_write,
7177 .llseek = generic_file_llseek,
7178};
7179#endif
7180
7181static const struct file_operations set_tracer_fops = {
7182 .open = tracing_open_generic,
7183 .read = tracing_set_trace_read,
7184 .write = tracing_set_trace_write,
7185 .llseek = generic_file_llseek,
7186};
7187
7188static const struct file_operations tracing_pipe_fops = {
7189 .open = tracing_open_pipe,
7190 .poll = tracing_poll_pipe,
7191 .read = tracing_read_pipe,
7192 .splice_read = tracing_splice_read_pipe,
7193 .release = tracing_release_pipe,
7194 .llseek = no_llseek,
7195};
7196
7197static const struct file_operations tracing_entries_fops = {
7198 .open = tracing_open_generic_tr,
7199 .read = tracing_entries_read,
7200 .write = tracing_entries_write,
7201 .llseek = generic_file_llseek,
7202 .release = tracing_release_generic_tr,
7203};
7204
7205static const struct file_operations tracing_total_entries_fops = {
7206 .open = tracing_open_generic_tr,
7207 .read = tracing_total_entries_read,
7208 .llseek = generic_file_llseek,
7209 .release = tracing_release_generic_tr,
7210};
7211
7212static const struct file_operations tracing_free_buffer_fops = {
7213 .open = tracing_open_generic_tr,
7214 .write = tracing_free_buffer_write,
7215 .release = tracing_free_buffer_release,
7216};
7217
7218static const struct file_operations tracing_mark_fops = {
7219 .open = tracing_open_generic_tr,
7220 .write = tracing_mark_write,
7221 .llseek = generic_file_llseek,
7222 .release = tracing_release_generic_tr,
7223};
7224
7225static const struct file_operations tracing_mark_raw_fops = {
7226 .open = tracing_open_generic_tr,
7227 .write = tracing_mark_raw_write,
7228 .llseek = generic_file_llseek,
7229 .release = tracing_release_generic_tr,
7230};
7231
7232static const struct file_operations trace_clock_fops = {
7233 .open = tracing_clock_open,
7234 .read = seq_read,
7235 .llseek = seq_lseek,
7236 .release = tracing_single_release_tr,
7237 .write = tracing_clock_write,
7238};
7239
7240static const struct file_operations trace_time_stamp_mode_fops = {
7241 .open = tracing_time_stamp_mode_open,
7242 .read = seq_read,
7243 .llseek = seq_lseek,
7244 .release = tracing_single_release_tr,
7245};
7246
7247#ifdef CONFIG_TRACER_SNAPSHOT
7248static const struct file_operations snapshot_fops = {
7249 .open = tracing_snapshot_open,
7250 .read = seq_read,
7251 .write = tracing_snapshot_write,
7252 .llseek = tracing_lseek,
7253 .release = tracing_snapshot_release,
7254};
7255
7256static const struct file_operations snapshot_raw_fops = {
7257 .open = snapshot_raw_open,
7258 .read = tracing_buffers_read,
7259 .release = tracing_buffers_release,
7260 .splice_read = tracing_buffers_splice_read,
7261 .llseek = no_llseek,
7262};
7263
7264#endif /* CONFIG_TRACER_SNAPSHOT */
7265
David Brazdil0f672f62019-12-10 10:32:29 +00007266#define TRACING_LOG_ERRS_MAX 8
7267#define TRACING_LOG_LOC_MAX 128
7268
7269#define CMD_PREFIX " Command: "
7270
7271struct err_info {
7272 const char **errs; /* ptr to loc-specific array of err strings */
7273 u8 type; /* index into errs -> specific err string */
7274 u8 pos; /* MAX_FILTER_STR_VAL = 256 */
7275 u64 ts;
7276};
7277
7278struct tracing_log_err {
7279 struct list_head list;
7280 struct err_info info;
7281 char loc[TRACING_LOG_LOC_MAX]; /* err location */
7282 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
7283};
7284
7285static DEFINE_MUTEX(tracing_err_log_lock);
7286
7287static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
7288{
7289 struct tracing_log_err *err;
7290
7291 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
7292 err = kzalloc(sizeof(*err), GFP_KERNEL);
7293 if (!err)
7294 err = ERR_PTR(-ENOMEM);
Olivier Deprez157378f2022-04-04 15:47:50 +02007295 else
7296 tr->n_err_log_entries++;
David Brazdil0f672f62019-12-10 10:32:29 +00007297
7298 return err;
7299 }
7300
7301 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
7302 list_del(&err->list);
7303
7304 return err;
7305}
7306
7307/**
7308 * err_pos - find the position of a string within a command for error careting
7309 * @cmd: The tracing command that caused the error
7310 * @str: The string to position the caret at within @cmd
7311 *
7312 * Finds the position of the first occurence of @str within @cmd. The
7313 * return value can be passed to tracing_log_err() for caret placement
7314 * within @cmd.
7315 *
7316 * Returns the index within @cmd of the first occurence of @str or 0
7317 * if @str was not found.
7318 */
7319unsigned int err_pos(char *cmd, const char *str)
7320{
7321 char *found;
7322
7323 if (WARN_ON(!strlen(cmd)))
7324 return 0;
7325
7326 found = strstr(cmd, str);
7327 if (found)
7328 return found - cmd;
7329
7330 return 0;
7331}
7332
7333/**
7334 * tracing_log_err - write an error to the tracing error log
7335 * @tr: The associated trace array for the error (NULL for top level array)
7336 * @loc: A string describing where the error occurred
7337 * @cmd: The tracing command that caused the error
7338 * @errs: The array of loc-specific static error strings
7339 * @type: The index into errs[], which produces the specific static err string
7340 * @pos: The position the caret should be placed in the cmd
7341 *
7342 * Writes an error into tracing/error_log of the form:
7343 *
7344 * <loc>: error: <text>
7345 * Command: <cmd>
7346 * ^
7347 *
7348 * tracing/error_log is a small log file containing the last
7349 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7350 * unless there has been a tracing error, and the error log can be
7351 * cleared and have its memory freed by writing the empty string in
7352 * truncation mode to it i.e. echo > tracing/error_log.
7353 *
7354 * NOTE: the @errs array along with the @type param are used to
7355 * produce a static error string - this string is not copied and saved
7356 * when the error is logged - only a pointer to it is saved. See
7357 * existing callers for examples of how static strings are typically
7358 * defined for use with tracing_log_err().
7359 */
7360void tracing_log_err(struct trace_array *tr,
7361 const char *loc, const char *cmd,
7362 const char **errs, u8 type, u8 pos)
7363{
7364 struct tracing_log_err *err;
7365
7366 if (!tr)
7367 tr = &global_trace;
7368
7369 mutex_lock(&tracing_err_log_lock);
7370 err = get_tracing_log_err(tr);
7371 if (PTR_ERR(err) == -ENOMEM) {
7372 mutex_unlock(&tracing_err_log_lock);
7373 return;
7374 }
7375
7376 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7377 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7378
7379 err->info.errs = errs;
7380 err->info.type = type;
7381 err->info.pos = pos;
7382 err->info.ts = local_clock();
7383
7384 list_add_tail(&err->list, &tr->err_log);
7385 mutex_unlock(&tracing_err_log_lock);
7386}
7387
7388static void clear_tracing_err_log(struct trace_array *tr)
7389{
7390 struct tracing_log_err *err, *next;
7391
7392 mutex_lock(&tracing_err_log_lock);
7393 list_for_each_entry_safe(err, next, &tr->err_log, list) {
7394 list_del(&err->list);
7395 kfree(err);
7396 }
7397
7398 tr->n_err_log_entries = 0;
7399 mutex_unlock(&tracing_err_log_lock);
7400}
7401
7402static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7403{
7404 struct trace_array *tr = m->private;
7405
7406 mutex_lock(&tracing_err_log_lock);
7407
7408 return seq_list_start(&tr->err_log, *pos);
7409}
7410
7411static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7412{
7413 struct trace_array *tr = m->private;
7414
7415 return seq_list_next(v, &tr->err_log, pos);
7416}
7417
7418static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7419{
7420 mutex_unlock(&tracing_err_log_lock);
7421}
7422
7423static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7424{
7425 u8 i;
7426
7427 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7428 seq_putc(m, ' ');
7429 for (i = 0; i < pos; i++)
7430 seq_putc(m, ' ');
7431 seq_puts(m, "^\n");
7432}
7433
7434static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7435{
7436 struct tracing_log_err *err = v;
7437
7438 if (err) {
7439 const char *err_text = err->info.errs[err->info.type];
7440 u64 sec = err->info.ts;
7441 u32 nsec;
7442
7443 nsec = do_div(sec, NSEC_PER_SEC);
7444 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7445 err->loc, err_text);
7446 seq_printf(m, "%s", err->cmd);
7447 tracing_err_log_show_pos(m, err->info.pos);
7448 }
7449
7450 return 0;
7451}
7452
7453static const struct seq_operations tracing_err_log_seq_ops = {
7454 .start = tracing_err_log_seq_start,
7455 .next = tracing_err_log_seq_next,
7456 .stop = tracing_err_log_seq_stop,
7457 .show = tracing_err_log_seq_show
7458};
7459
7460static int tracing_err_log_open(struct inode *inode, struct file *file)
7461{
7462 struct trace_array *tr = inode->i_private;
7463 int ret = 0;
7464
7465 ret = tracing_check_open_get_tr(tr);
7466 if (ret)
7467 return ret;
7468
7469 /* If this file was opened for write, then erase contents */
7470 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
7471 clear_tracing_err_log(tr);
7472
7473 if (file->f_mode & FMODE_READ) {
7474 ret = seq_open(file, &tracing_err_log_seq_ops);
7475 if (!ret) {
7476 struct seq_file *m = file->private_data;
7477 m->private = tr;
7478 } else {
7479 trace_array_put(tr);
7480 }
7481 }
7482 return ret;
7483}
7484
7485static ssize_t tracing_err_log_write(struct file *file,
7486 const char __user *buffer,
7487 size_t count, loff_t *ppos)
7488{
7489 return count;
7490}
7491
7492static int tracing_err_log_release(struct inode *inode, struct file *file)
7493{
7494 struct trace_array *tr = inode->i_private;
7495
7496 trace_array_put(tr);
7497
7498 if (file->f_mode & FMODE_READ)
7499 seq_release(inode, file);
7500
7501 return 0;
7502}
7503
7504static const struct file_operations tracing_err_log_fops = {
7505 .open = tracing_err_log_open,
7506 .write = tracing_err_log_write,
7507 .read = seq_read,
7508 .llseek = seq_lseek,
7509 .release = tracing_err_log_release,
7510};
7511
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007512static int tracing_buffers_open(struct inode *inode, struct file *filp)
7513{
7514 struct trace_array *tr = inode->i_private;
7515 struct ftrace_buffer_info *info;
7516 int ret;
7517
David Brazdil0f672f62019-12-10 10:32:29 +00007518 ret = tracing_check_open_get_tr(tr);
7519 if (ret)
7520 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007521
Olivier Deprez157378f2022-04-04 15:47:50 +02007522 info = kvzalloc(sizeof(*info), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007523 if (!info) {
7524 trace_array_put(tr);
7525 return -ENOMEM;
7526 }
7527
7528 mutex_lock(&trace_types_lock);
7529
7530 info->iter.tr = tr;
7531 info->iter.cpu_file = tracing_get_cpu(inode);
7532 info->iter.trace = tr->current_trace;
Olivier Deprez157378f2022-04-04 15:47:50 +02007533 info->iter.array_buffer = &tr->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007534 info->spare = NULL;
7535 /* Force reading ring buffer for first read */
7536 info->read = (unsigned int)-1;
7537
7538 filp->private_data = info;
7539
Olivier Deprez0e641232021-09-23 10:07:05 +02007540 tr->trace_ref++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007541
7542 mutex_unlock(&trace_types_lock);
7543
7544 ret = nonseekable_open(inode, filp);
7545 if (ret < 0)
7546 trace_array_put(tr);
7547
7548 return ret;
7549}
7550
7551static __poll_t
7552tracing_buffers_poll(struct file *filp, poll_table *poll_table)
7553{
7554 struct ftrace_buffer_info *info = filp->private_data;
7555 struct trace_iterator *iter = &info->iter;
7556
7557 return trace_poll(iter, filp, poll_table);
7558}
7559
7560static ssize_t
7561tracing_buffers_read(struct file *filp, char __user *ubuf,
7562 size_t count, loff_t *ppos)
7563{
7564 struct ftrace_buffer_info *info = filp->private_data;
7565 struct trace_iterator *iter = &info->iter;
7566 ssize_t ret = 0;
7567 ssize_t size;
7568
7569 if (!count)
7570 return 0;
7571
7572#ifdef CONFIG_TRACER_MAX_TRACE
7573 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7574 return -EBUSY;
7575#endif
7576
7577 if (!info->spare) {
Olivier Deprez157378f2022-04-04 15:47:50 +02007578 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007579 iter->cpu_file);
7580 if (IS_ERR(info->spare)) {
7581 ret = PTR_ERR(info->spare);
7582 info->spare = NULL;
7583 } else {
7584 info->spare_cpu = iter->cpu_file;
7585 }
7586 }
7587 if (!info->spare)
7588 return ret;
7589
7590 /* Do we have previous read data to read? */
7591 if (info->read < PAGE_SIZE)
7592 goto read;
7593
7594 again:
7595 trace_access_lock(iter->cpu_file);
Olivier Deprez157378f2022-04-04 15:47:50 +02007596 ret = ring_buffer_read_page(iter->array_buffer->buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007597 &info->spare,
7598 count,
7599 iter->cpu_file, 0);
7600 trace_access_unlock(iter->cpu_file);
7601
7602 if (ret < 0) {
7603 if (trace_empty(iter)) {
7604 if ((filp->f_flags & O_NONBLOCK))
7605 return -EAGAIN;
7606
David Brazdil0f672f62019-12-10 10:32:29 +00007607 ret = wait_on_pipe(iter, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007608 if (ret)
7609 return ret;
7610
7611 goto again;
7612 }
7613 return 0;
7614 }
7615
7616 info->read = 0;
7617 read:
7618 size = PAGE_SIZE - info->read;
7619 if (size > count)
7620 size = count;
7621
7622 ret = copy_to_user(ubuf, info->spare + info->read, size);
7623 if (ret == size)
7624 return -EFAULT;
7625
7626 size -= ret;
7627
7628 *ppos += size;
7629 info->read += size;
7630
7631 return size;
7632}
7633
7634static int tracing_buffers_release(struct inode *inode, struct file *file)
7635{
7636 struct ftrace_buffer_info *info = file->private_data;
7637 struct trace_iterator *iter = &info->iter;
7638
7639 mutex_lock(&trace_types_lock);
7640
Olivier Deprez0e641232021-09-23 10:07:05 +02007641 iter->tr->trace_ref--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007642
7643 __trace_array_put(iter->tr);
7644
7645 if (info->spare)
Olivier Deprez157378f2022-04-04 15:47:50 +02007646 ring_buffer_free_read_page(iter->array_buffer->buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007647 info->spare_cpu, info->spare);
Olivier Deprez157378f2022-04-04 15:47:50 +02007648 kvfree(info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007649
7650 mutex_unlock(&trace_types_lock);
7651
7652 return 0;
7653}
7654
7655struct buffer_ref {
Olivier Deprez157378f2022-04-04 15:47:50 +02007656 struct trace_buffer *buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007657 void *page;
7658 int cpu;
David Brazdil0f672f62019-12-10 10:32:29 +00007659 refcount_t refcount;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007660};
7661
David Brazdil0f672f62019-12-10 10:32:29 +00007662static void buffer_ref_release(struct buffer_ref *ref)
7663{
7664 if (!refcount_dec_and_test(&ref->refcount))
7665 return;
7666 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7667 kfree(ref);
7668}
7669
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007670static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7671 struct pipe_buffer *buf)
7672{
7673 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7674
David Brazdil0f672f62019-12-10 10:32:29 +00007675 buffer_ref_release(ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007676 buf->private = 0;
7677}
7678
David Brazdil0f672f62019-12-10 10:32:29 +00007679static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007680 struct pipe_buffer *buf)
7681{
7682 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7683
David Brazdil0f672f62019-12-10 10:32:29 +00007684 if (refcount_read(&ref->refcount) > INT_MAX/2)
7685 return false;
7686
7687 refcount_inc(&ref->refcount);
7688 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007689}
7690
7691/* Pipe buffer operations for a buffer. */
7692static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007693 .release = buffer_pipe_buf_release,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007694 .get = buffer_pipe_buf_get,
7695};
7696
7697/*
7698 * Callback from splice_to_pipe(), if we need to release some pages
7699 * at the end of the spd in case we error'ed out in filling the pipe.
7700 */
7701static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7702{
7703 struct buffer_ref *ref =
7704 (struct buffer_ref *)spd->partial[i].private;
7705
David Brazdil0f672f62019-12-10 10:32:29 +00007706 buffer_ref_release(ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007707 spd->partial[i].private = 0;
7708}
7709
7710static ssize_t
7711tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7712 struct pipe_inode_info *pipe, size_t len,
7713 unsigned int flags)
7714{
7715 struct ftrace_buffer_info *info = file->private_data;
7716 struct trace_iterator *iter = &info->iter;
7717 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7718 struct page *pages_def[PIPE_DEF_BUFFERS];
7719 struct splice_pipe_desc spd = {
7720 .pages = pages_def,
7721 .partial = partial_def,
7722 .nr_pages_max = PIPE_DEF_BUFFERS,
7723 .ops = &buffer_pipe_buf_ops,
7724 .spd_release = buffer_spd_release,
7725 };
7726 struct buffer_ref *ref;
7727 int entries, i;
7728 ssize_t ret = 0;
7729
7730#ifdef CONFIG_TRACER_MAX_TRACE
7731 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7732 return -EBUSY;
7733#endif
7734
7735 if (*ppos & (PAGE_SIZE - 1))
7736 return -EINVAL;
7737
7738 if (len & (PAGE_SIZE - 1)) {
7739 if (len < PAGE_SIZE)
7740 return -EINVAL;
7741 len &= PAGE_MASK;
7742 }
7743
7744 if (splice_grow_spd(pipe, &spd))
7745 return -ENOMEM;
7746
7747 again:
7748 trace_access_lock(iter->cpu_file);
Olivier Deprez157378f2022-04-04 15:47:50 +02007749 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007750
7751 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
7752 struct page *page;
7753 int r;
7754
7755 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
7756 if (!ref) {
7757 ret = -ENOMEM;
7758 break;
7759 }
7760
David Brazdil0f672f62019-12-10 10:32:29 +00007761 refcount_set(&ref->refcount, 1);
Olivier Deprez157378f2022-04-04 15:47:50 +02007762 ref->buffer = iter->array_buffer->buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007763 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
7764 if (IS_ERR(ref->page)) {
7765 ret = PTR_ERR(ref->page);
7766 ref->page = NULL;
7767 kfree(ref);
7768 break;
7769 }
7770 ref->cpu = iter->cpu_file;
7771
7772 r = ring_buffer_read_page(ref->buffer, &ref->page,
7773 len, iter->cpu_file, 1);
7774 if (r < 0) {
7775 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7776 ref->page);
7777 kfree(ref);
7778 break;
7779 }
7780
7781 page = virt_to_page(ref->page);
7782
7783 spd.pages[i] = page;
7784 spd.partial[i].len = PAGE_SIZE;
7785 spd.partial[i].offset = 0;
7786 spd.partial[i].private = (unsigned long)ref;
7787 spd.nr_pages++;
7788 *ppos += PAGE_SIZE;
7789
Olivier Deprez157378f2022-04-04 15:47:50 +02007790 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007791 }
7792
7793 trace_access_unlock(iter->cpu_file);
7794 spd.nr_pages = i;
7795
7796 /* did we read anything? */
7797 if (!spd.nr_pages) {
7798 if (ret)
7799 goto out;
7800
7801 ret = -EAGAIN;
7802 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
7803 goto out;
7804
David Brazdil0f672f62019-12-10 10:32:29 +00007805 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007806 if (ret)
7807 goto out;
7808
7809 goto again;
7810 }
7811
7812 ret = splice_to_pipe(pipe, &spd);
7813out:
7814 splice_shrink_spd(&spd);
7815
7816 return ret;
7817}
7818
7819static const struct file_operations tracing_buffers_fops = {
7820 .open = tracing_buffers_open,
7821 .read = tracing_buffers_read,
7822 .poll = tracing_buffers_poll,
7823 .release = tracing_buffers_release,
7824 .splice_read = tracing_buffers_splice_read,
7825 .llseek = no_llseek,
7826};
7827
7828static ssize_t
7829tracing_stats_read(struct file *filp, char __user *ubuf,
7830 size_t count, loff_t *ppos)
7831{
7832 struct inode *inode = file_inode(filp);
7833 struct trace_array *tr = inode->i_private;
Olivier Deprez157378f2022-04-04 15:47:50 +02007834 struct array_buffer *trace_buf = &tr->array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007835 int cpu = tracing_get_cpu(inode);
7836 struct trace_seq *s;
7837 unsigned long cnt;
7838 unsigned long long t;
7839 unsigned long usec_rem;
7840
7841 s = kmalloc(sizeof(*s), GFP_KERNEL);
7842 if (!s)
7843 return -ENOMEM;
7844
7845 trace_seq_init(s);
7846
7847 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
7848 trace_seq_printf(s, "entries: %ld\n", cnt);
7849
7850 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
7851 trace_seq_printf(s, "overrun: %ld\n", cnt);
7852
7853 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
7854 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7855
7856 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
7857 trace_seq_printf(s, "bytes: %ld\n", cnt);
7858
7859 if (trace_clocks[tr->clock_id].in_ns) {
7860 /* local or global for trace_clock */
7861 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
7862 usec_rem = do_div(t, USEC_PER_SEC);
7863 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7864 t, usec_rem);
7865
7866 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
7867 usec_rem = do_div(t, USEC_PER_SEC);
7868 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7869 } else {
7870 /* counter or tsc mode for trace_clock */
7871 trace_seq_printf(s, "oldest event ts: %llu\n",
7872 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
7873
7874 trace_seq_printf(s, "now ts: %llu\n",
7875 ring_buffer_time_stamp(trace_buf->buffer, cpu));
7876 }
7877
7878 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
7879 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7880
7881 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
7882 trace_seq_printf(s, "read events: %ld\n", cnt);
7883
7884 count = simple_read_from_buffer(ubuf, count, ppos,
7885 s->buffer, trace_seq_used(s));
7886
7887 kfree(s);
7888
7889 return count;
7890}
7891
7892static const struct file_operations tracing_stats_fops = {
7893 .open = tracing_open_generic_tr,
7894 .read = tracing_stats_read,
7895 .llseek = generic_file_llseek,
7896 .release = tracing_release_generic_tr,
7897};
7898
7899#ifdef CONFIG_DYNAMIC_FTRACE
7900
7901static ssize_t
7902tracing_read_dyn_info(struct file *filp, char __user *ubuf,
7903 size_t cnt, loff_t *ppos)
7904{
Olivier Deprez157378f2022-04-04 15:47:50 +02007905 ssize_t ret;
7906 char *buf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007907 int r;
7908
Olivier Deprez157378f2022-04-04 15:47:50 +02007909 /* 256 should be plenty to hold the amount needed */
7910 buf = kmalloc(256, GFP_KERNEL);
7911 if (!buf)
7912 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007913
Olivier Deprez157378f2022-04-04 15:47:50 +02007914 r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
7915 ftrace_update_tot_cnt,
7916 ftrace_number_of_pages,
7917 ftrace_number_of_groups);
7918
7919 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7920 kfree(buf);
7921 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007922}
7923
7924static const struct file_operations tracing_dyn_info_fops = {
7925 .open = tracing_open_generic,
7926 .read = tracing_read_dyn_info,
7927 .llseek = generic_file_llseek,
7928};
7929#endif /* CONFIG_DYNAMIC_FTRACE */
7930
7931#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7932static void
7933ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
7934 struct trace_array *tr, struct ftrace_probe_ops *ops,
7935 void *data)
7936{
7937 tracing_snapshot_instance(tr);
7938}
7939
7940static void
7941ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
7942 struct trace_array *tr, struct ftrace_probe_ops *ops,
7943 void *data)
7944{
7945 struct ftrace_func_mapper *mapper = data;
7946 long *count = NULL;
7947
7948 if (mapper)
7949 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7950
7951 if (count) {
7952
7953 if (*count <= 0)
7954 return;
7955
7956 (*count)--;
7957 }
7958
7959 tracing_snapshot_instance(tr);
7960}
7961
7962static int
7963ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7964 struct ftrace_probe_ops *ops, void *data)
7965{
7966 struct ftrace_func_mapper *mapper = data;
7967 long *count = NULL;
7968
7969 seq_printf(m, "%ps:", (void *)ip);
7970
7971 seq_puts(m, "snapshot");
7972
7973 if (mapper)
7974 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7975
7976 if (count)
7977 seq_printf(m, ":count=%ld\n", *count);
7978 else
7979 seq_puts(m, ":unlimited\n");
7980
7981 return 0;
7982}
7983
7984static int
7985ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
7986 unsigned long ip, void *init_data, void **data)
7987{
7988 struct ftrace_func_mapper *mapper = *data;
7989
7990 if (!mapper) {
7991 mapper = allocate_ftrace_func_mapper();
7992 if (!mapper)
7993 return -ENOMEM;
7994 *data = mapper;
7995 }
7996
7997 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
7998}
7999
8000static void
8001ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
8002 unsigned long ip, void *data)
8003{
8004 struct ftrace_func_mapper *mapper = data;
8005
8006 if (!ip) {
8007 if (!mapper)
8008 return;
8009 free_ftrace_func_mapper(mapper, NULL);
8010 return;
8011 }
8012
8013 ftrace_func_mapper_remove_ip(mapper, ip);
8014}
8015
8016static struct ftrace_probe_ops snapshot_probe_ops = {
8017 .func = ftrace_snapshot,
8018 .print = ftrace_snapshot_print,
8019};
8020
8021static struct ftrace_probe_ops snapshot_count_probe_ops = {
8022 .func = ftrace_count_snapshot,
8023 .print = ftrace_snapshot_print,
8024 .init = ftrace_snapshot_init,
8025 .free = ftrace_snapshot_free,
8026};
8027
8028static int
8029ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
8030 char *glob, char *cmd, char *param, int enable)
8031{
8032 struct ftrace_probe_ops *ops;
8033 void *count = (void *)-1;
8034 char *number;
8035 int ret;
8036
8037 if (!tr)
8038 return -ENODEV;
8039
8040 /* hash funcs only work with set_ftrace_filter */
8041 if (!enable)
8042 return -EINVAL;
8043
8044 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
8045
8046 if (glob[0] == '!')
8047 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
8048
8049 if (!param)
8050 goto out_reg;
8051
8052 number = strsep(&param, ":");
8053
8054 if (!strlen(number))
8055 goto out_reg;
8056
8057 /*
8058 * We use the callback data field (which is a pointer)
8059 * as our counter.
8060 */
8061 ret = kstrtoul(number, 0, (unsigned long *)&count);
8062 if (ret)
8063 return ret;
8064
8065 out_reg:
8066 ret = tracing_alloc_snapshot_instance(tr);
8067 if (ret < 0)
8068 goto out;
8069
8070 ret = register_ftrace_function_probe(glob, tr, ops, count);
8071
8072 out:
8073 return ret < 0 ? ret : 0;
8074}
8075
8076static struct ftrace_func_command ftrace_snapshot_cmd = {
8077 .name = "snapshot",
8078 .func = ftrace_trace_snapshot_callback,
8079};
8080
8081static __init int register_snapshot_cmd(void)
8082{
8083 return register_ftrace_command(&ftrace_snapshot_cmd);
8084}
8085#else
8086static inline __init int register_snapshot_cmd(void) { return 0; }
8087#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
8088
8089static struct dentry *tracing_get_dentry(struct trace_array *tr)
8090{
8091 if (WARN_ON(!tr->dir))
8092 return ERR_PTR(-ENODEV);
8093
8094 /* Top directory uses NULL as the parent */
8095 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
8096 return NULL;
8097
8098 /* All sub buffers have a descriptor */
8099 return tr->dir;
8100}
8101
8102static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
8103{
8104 struct dentry *d_tracer;
8105
8106 if (tr->percpu_dir)
8107 return tr->percpu_dir;
8108
8109 d_tracer = tracing_get_dentry(tr);
8110 if (IS_ERR(d_tracer))
8111 return NULL;
8112
8113 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
8114
Olivier Deprez157378f2022-04-04 15:47:50 +02008115 MEM_FAIL(!tr->percpu_dir,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008116 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
8117
8118 return tr->percpu_dir;
8119}
8120
8121static struct dentry *
8122trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
8123 void *data, long cpu, const struct file_operations *fops)
8124{
8125 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
8126
8127 if (ret) /* See tracing_get_cpu() */
8128 d_inode(ret)->i_cdev = (void *)(cpu + 1);
8129 return ret;
8130}
8131
8132static void
8133tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
8134{
8135 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
8136 struct dentry *d_cpu;
8137 char cpu_dir[30]; /* 30 characters should be more than enough */
8138
8139 if (!d_percpu)
8140 return;
8141
8142 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8143 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
8144 if (!d_cpu) {
8145 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
8146 return;
8147 }
8148
8149 /* per cpu trace_pipe */
8150 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
8151 tr, cpu, &tracing_pipe_fops);
8152
8153 /* per cpu trace */
8154 trace_create_cpu_file("trace", 0644, d_cpu,
8155 tr, cpu, &tracing_fops);
8156
8157 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
8158 tr, cpu, &tracing_buffers_fops);
8159
8160 trace_create_cpu_file("stats", 0444, d_cpu,
8161 tr, cpu, &tracing_stats_fops);
8162
8163 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
8164 tr, cpu, &tracing_entries_fops);
8165
8166#ifdef CONFIG_TRACER_SNAPSHOT
8167 trace_create_cpu_file("snapshot", 0644, d_cpu,
8168 tr, cpu, &snapshot_fops);
8169
8170 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
8171 tr, cpu, &snapshot_raw_fops);
8172#endif
8173}
8174
8175#ifdef CONFIG_FTRACE_SELFTEST
8176/* Let selftest have access to static functions in this file */
8177#include "trace_selftest.c"
8178#endif
8179
8180static ssize_t
8181trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
8182 loff_t *ppos)
8183{
8184 struct trace_option_dentry *topt = filp->private_data;
8185 char *buf;
8186
8187 if (topt->flags->val & topt->opt->bit)
8188 buf = "1\n";
8189 else
8190 buf = "0\n";
8191
8192 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8193}
8194
8195static ssize_t
8196trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
8197 loff_t *ppos)
8198{
8199 struct trace_option_dentry *topt = filp->private_data;
8200 unsigned long val;
8201 int ret;
8202
8203 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8204 if (ret)
8205 return ret;
8206
8207 if (val != 0 && val != 1)
8208 return -EINVAL;
8209
8210 if (!!(topt->flags->val & topt->opt->bit) != val) {
8211 mutex_lock(&trace_types_lock);
8212 ret = __set_tracer_option(topt->tr, topt->flags,
8213 topt->opt, !val);
8214 mutex_unlock(&trace_types_lock);
8215 if (ret)
8216 return ret;
8217 }
8218
8219 *ppos += cnt;
8220
8221 return cnt;
8222}
8223
8224
8225static const struct file_operations trace_options_fops = {
8226 .open = tracing_open_generic,
8227 .read = trace_options_read,
8228 .write = trace_options_write,
8229 .llseek = generic_file_llseek,
8230};
8231
8232/*
8233 * In order to pass in both the trace_array descriptor as well as the index
8234 * to the flag that the trace option file represents, the trace_array
8235 * has a character array of trace_flags_index[], which holds the index
8236 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
8237 * The address of this character array is passed to the flag option file
8238 * read/write callbacks.
8239 *
8240 * In order to extract both the index and the trace_array descriptor,
8241 * get_tr_index() uses the following algorithm.
8242 *
8243 * idx = *ptr;
8244 *
8245 * As the pointer itself contains the address of the index (remember
8246 * index[1] == 1).
8247 *
8248 * Then to get the trace_array descriptor, by subtracting that index
8249 * from the ptr, we get to the start of the index itself.
8250 *
8251 * ptr - idx == &index[0]
8252 *
8253 * Then a simple container_of() from that pointer gets us to the
8254 * trace_array descriptor.
8255 */
8256static void get_tr_index(void *data, struct trace_array **ptr,
8257 unsigned int *pindex)
8258{
8259 *pindex = *(unsigned char *)data;
8260
8261 *ptr = container_of(data - *pindex, struct trace_array,
8262 trace_flags_index);
8263}
8264
8265static ssize_t
8266trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
8267 loff_t *ppos)
8268{
8269 void *tr_index = filp->private_data;
8270 struct trace_array *tr;
8271 unsigned int index;
8272 char *buf;
8273
8274 get_tr_index(tr_index, &tr, &index);
8275
8276 if (tr->trace_flags & (1 << index))
8277 buf = "1\n";
8278 else
8279 buf = "0\n";
8280
8281 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8282}
8283
8284static ssize_t
8285trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
8286 loff_t *ppos)
8287{
8288 void *tr_index = filp->private_data;
8289 struct trace_array *tr;
8290 unsigned int index;
8291 unsigned long val;
8292 int ret;
8293
8294 get_tr_index(tr_index, &tr, &index);
8295
8296 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8297 if (ret)
8298 return ret;
8299
8300 if (val != 0 && val != 1)
8301 return -EINVAL;
8302
Olivier Deprez0e641232021-09-23 10:07:05 +02008303 mutex_lock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008304 mutex_lock(&trace_types_lock);
8305 ret = set_tracer_flag(tr, 1 << index, val);
8306 mutex_unlock(&trace_types_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02008307 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008308
8309 if (ret < 0)
8310 return ret;
8311
8312 *ppos += cnt;
8313
8314 return cnt;
8315}
8316
8317static const struct file_operations trace_options_core_fops = {
8318 .open = tracing_open_generic,
8319 .read = trace_options_core_read,
8320 .write = trace_options_core_write,
8321 .llseek = generic_file_llseek,
8322};
8323
8324struct dentry *trace_create_file(const char *name,
8325 umode_t mode,
8326 struct dentry *parent,
8327 void *data,
8328 const struct file_operations *fops)
8329{
8330 struct dentry *ret;
8331
8332 ret = tracefs_create_file(name, mode, parent, data, fops);
8333 if (!ret)
8334 pr_warn("Could not create tracefs '%s' entry\n", name);
8335
8336 return ret;
8337}
8338
8339
8340static struct dentry *trace_options_init_dentry(struct trace_array *tr)
8341{
8342 struct dentry *d_tracer;
8343
8344 if (tr->options)
8345 return tr->options;
8346
8347 d_tracer = tracing_get_dentry(tr);
8348 if (IS_ERR(d_tracer))
8349 return NULL;
8350
8351 tr->options = tracefs_create_dir("options", d_tracer);
8352 if (!tr->options) {
8353 pr_warn("Could not create tracefs directory 'options'\n");
8354 return NULL;
8355 }
8356
8357 return tr->options;
8358}
8359
8360static void
8361create_trace_option_file(struct trace_array *tr,
8362 struct trace_option_dentry *topt,
8363 struct tracer_flags *flags,
8364 struct tracer_opt *opt)
8365{
8366 struct dentry *t_options;
8367
8368 t_options = trace_options_init_dentry(tr);
8369 if (!t_options)
8370 return;
8371
8372 topt->flags = flags;
8373 topt->opt = opt;
8374 topt->tr = tr;
8375
8376 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
8377 &trace_options_fops);
8378
8379}
8380
8381static void
8382create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
8383{
8384 struct trace_option_dentry *topts;
8385 struct trace_options *tr_topts;
8386 struct tracer_flags *flags;
8387 struct tracer_opt *opts;
8388 int cnt;
8389 int i;
8390
8391 if (!tracer)
8392 return;
8393
8394 flags = tracer->flags;
8395
8396 if (!flags || !flags->opts)
8397 return;
8398
8399 /*
8400 * If this is an instance, only create flags for tracers
8401 * the instance may have.
8402 */
8403 if (!trace_ok_for_array(tracer, tr))
8404 return;
8405
8406 for (i = 0; i < tr->nr_topts; i++) {
8407 /* Make sure there's no duplicate flags. */
8408 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
8409 return;
8410 }
8411
8412 opts = flags->opts;
8413
8414 for (cnt = 0; opts[cnt].name; cnt++)
8415 ;
8416
8417 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
8418 if (!topts)
8419 return;
8420
8421 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
8422 GFP_KERNEL);
8423 if (!tr_topts) {
8424 kfree(topts);
8425 return;
8426 }
8427
8428 tr->topts = tr_topts;
8429 tr->topts[tr->nr_topts].tracer = tracer;
8430 tr->topts[tr->nr_topts].topts = topts;
8431 tr->nr_topts++;
8432
8433 for (cnt = 0; opts[cnt].name; cnt++) {
8434 create_trace_option_file(tr, &topts[cnt], flags,
8435 &opts[cnt]);
Olivier Deprez157378f2022-04-04 15:47:50 +02008436 MEM_FAIL(topts[cnt].entry == NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008437 "Failed to create trace option: %s",
8438 opts[cnt].name);
8439 }
8440}
8441
8442static struct dentry *
8443create_trace_option_core_file(struct trace_array *tr,
8444 const char *option, long index)
8445{
8446 struct dentry *t_options;
8447
8448 t_options = trace_options_init_dentry(tr);
8449 if (!t_options)
8450 return NULL;
8451
8452 return trace_create_file(option, 0644, t_options,
8453 (void *)&tr->trace_flags_index[index],
8454 &trace_options_core_fops);
8455}
8456
8457static void create_trace_options_dir(struct trace_array *tr)
8458{
8459 struct dentry *t_options;
8460 bool top_level = tr == &global_trace;
8461 int i;
8462
8463 t_options = trace_options_init_dentry(tr);
8464 if (!t_options)
8465 return;
8466
8467 for (i = 0; trace_options[i]; i++) {
8468 if (top_level ||
8469 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
8470 create_trace_option_core_file(tr, trace_options[i], i);
8471 }
8472}
8473
8474static ssize_t
8475rb_simple_read(struct file *filp, char __user *ubuf,
8476 size_t cnt, loff_t *ppos)
8477{
8478 struct trace_array *tr = filp->private_data;
8479 char buf[64];
8480 int r;
8481
8482 r = tracer_tracing_is_on(tr);
8483 r = sprintf(buf, "%d\n", r);
8484
8485 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8486}
8487
8488static ssize_t
8489rb_simple_write(struct file *filp, const char __user *ubuf,
8490 size_t cnt, loff_t *ppos)
8491{
8492 struct trace_array *tr = filp->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02008493 struct trace_buffer *buffer = tr->array_buffer.buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008494 unsigned long val;
8495 int ret;
8496
8497 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8498 if (ret)
8499 return ret;
8500
8501 if (buffer) {
8502 mutex_lock(&trace_types_lock);
8503 if (!!val == tracer_tracing_is_on(tr)) {
8504 val = 0; /* do nothing */
8505 } else if (val) {
8506 tracer_tracing_on(tr);
8507 if (tr->current_trace->start)
8508 tr->current_trace->start(tr);
8509 } else {
8510 tracer_tracing_off(tr);
8511 if (tr->current_trace->stop)
8512 tr->current_trace->stop(tr);
8513 }
8514 mutex_unlock(&trace_types_lock);
8515 }
8516
8517 (*ppos)++;
8518
8519 return cnt;
8520}
8521
8522static const struct file_operations rb_simple_fops = {
8523 .open = tracing_open_generic_tr,
8524 .read = rb_simple_read,
8525 .write = rb_simple_write,
8526 .release = tracing_release_generic_tr,
8527 .llseek = default_llseek,
8528};
8529
David Brazdil0f672f62019-12-10 10:32:29 +00008530static ssize_t
8531buffer_percent_read(struct file *filp, char __user *ubuf,
8532 size_t cnt, loff_t *ppos)
8533{
8534 struct trace_array *tr = filp->private_data;
8535 char buf[64];
8536 int r;
8537
8538 r = tr->buffer_percent;
8539 r = sprintf(buf, "%d\n", r);
8540
8541 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8542}
8543
8544static ssize_t
8545buffer_percent_write(struct file *filp, const char __user *ubuf,
8546 size_t cnt, loff_t *ppos)
8547{
8548 struct trace_array *tr = filp->private_data;
8549 unsigned long val;
8550 int ret;
8551
8552 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8553 if (ret)
8554 return ret;
8555
8556 if (val > 100)
8557 return -EINVAL;
8558
8559 if (!val)
8560 val = 1;
8561
8562 tr->buffer_percent = val;
8563
8564 (*ppos)++;
8565
8566 return cnt;
8567}
8568
8569static const struct file_operations buffer_percent_fops = {
8570 .open = tracing_open_generic_tr,
8571 .read = buffer_percent_read,
8572 .write = buffer_percent_write,
8573 .release = tracing_release_generic_tr,
8574 .llseek = default_llseek,
8575};
8576
8577static struct dentry *trace_instance_dir;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008578
8579static void
8580init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
8581
8582static int
Olivier Deprez157378f2022-04-04 15:47:50 +02008583allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008584{
8585 enum ring_buffer_flags rb_flags;
8586
8587 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
8588
8589 buf->tr = tr;
8590
8591 buf->buffer = ring_buffer_alloc(size, rb_flags);
8592 if (!buf->buffer)
8593 return -ENOMEM;
8594
8595 buf->data = alloc_percpu(struct trace_array_cpu);
8596 if (!buf->data) {
8597 ring_buffer_free(buf->buffer);
8598 buf->buffer = NULL;
8599 return -ENOMEM;
8600 }
8601
8602 /* Allocate the first page for all buffers */
Olivier Deprez157378f2022-04-04 15:47:50 +02008603 set_buffer_entries(&tr->array_buffer,
8604 ring_buffer_size(tr->array_buffer.buffer, 0));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008605
8606 return 0;
8607}
8608
8609static int allocate_trace_buffers(struct trace_array *tr, int size)
8610{
8611 int ret;
8612
Olivier Deprez157378f2022-04-04 15:47:50 +02008613 ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008614 if (ret)
8615 return ret;
8616
8617#ifdef CONFIG_TRACER_MAX_TRACE
8618 ret = allocate_trace_buffer(tr, &tr->max_buffer,
8619 allocate_snapshot ? size : 1);
Olivier Deprez157378f2022-04-04 15:47:50 +02008620 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
8621 ring_buffer_free(tr->array_buffer.buffer);
8622 tr->array_buffer.buffer = NULL;
8623 free_percpu(tr->array_buffer.data);
8624 tr->array_buffer.data = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008625 return -ENOMEM;
8626 }
8627 tr->allocated_snapshot = allocate_snapshot;
8628
8629 /*
8630 * Only the top level trace array gets its snapshot allocated
8631 * from the kernel command line.
8632 */
8633 allocate_snapshot = false;
8634#endif
Olivier Deprez0e641232021-09-23 10:07:05 +02008635
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008636 return 0;
8637}
8638
Olivier Deprez157378f2022-04-04 15:47:50 +02008639static void free_trace_buffer(struct array_buffer *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008640{
8641 if (buf->buffer) {
8642 ring_buffer_free(buf->buffer);
8643 buf->buffer = NULL;
8644 free_percpu(buf->data);
8645 buf->data = NULL;
8646 }
8647}
8648
8649static void free_trace_buffers(struct trace_array *tr)
8650{
8651 if (!tr)
8652 return;
8653
Olivier Deprez157378f2022-04-04 15:47:50 +02008654 free_trace_buffer(&tr->array_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008655
8656#ifdef CONFIG_TRACER_MAX_TRACE
8657 free_trace_buffer(&tr->max_buffer);
8658#endif
8659}
8660
8661static void init_trace_flags_index(struct trace_array *tr)
8662{
8663 int i;
8664
8665 /* Used by the trace options files */
8666 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8667 tr->trace_flags_index[i] = i;
8668}
8669
8670static void __update_tracer_options(struct trace_array *tr)
8671{
8672 struct tracer *t;
8673
8674 for (t = trace_types; t; t = t->next)
8675 add_tracer_options(tr, t);
8676}
8677
8678static void update_tracer_options(struct trace_array *tr)
8679{
8680 mutex_lock(&trace_types_lock);
Olivier Deprez92d4c212022-12-06 15:05:30 +01008681 tracer_options_updated = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008682 __update_tracer_options(tr);
8683 mutex_unlock(&trace_types_lock);
8684}
8685
Olivier Deprez157378f2022-04-04 15:47:50 +02008686/* Must have trace_types_lock held */
8687struct trace_array *trace_array_find(const char *instance)
8688{
8689 struct trace_array *tr, *found = NULL;
8690
8691 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8692 if (tr->name && strcmp(tr->name, instance) == 0) {
8693 found = tr;
8694 break;
8695 }
8696 }
8697
8698 return found;
8699}
8700
8701struct trace_array *trace_array_find_get(const char *instance)
8702{
8703 struct trace_array *tr;
8704
8705 mutex_lock(&trace_types_lock);
8706 tr = trace_array_find(instance);
8707 if (tr)
8708 tr->ref++;
8709 mutex_unlock(&trace_types_lock);
8710
8711 return tr;
8712}
8713
8714static int trace_array_create_dir(struct trace_array *tr)
8715{
8716 int ret;
8717
8718 tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
8719 if (!tr->dir)
8720 return -EINVAL;
8721
8722 ret = event_trace_add_tracer(tr->dir, tr);
8723 if (ret) {
8724 tracefs_remove(tr->dir);
8725 return ret;
8726 }
8727
8728 init_tracer_tracefs(tr, tr->dir);
8729 __update_tracer_options(tr);
8730
8731 return ret;
8732}
8733
8734static struct trace_array *trace_array_create(const char *name)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008735{
8736 struct trace_array *tr;
8737 int ret;
8738
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008739 ret = -ENOMEM;
8740 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8741 if (!tr)
Olivier Deprez157378f2022-04-04 15:47:50 +02008742 return ERR_PTR(ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008743
8744 tr->name = kstrdup(name, GFP_KERNEL);
8745 if (!tr->name)
8746 goto out_free_tr;
8747
8748 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8749 goto out_free_tr;
8750
8751 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
8752
8753 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8754
8755 raw_spin_lock_init(&tr->start_lock);
8756
8757 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8758
8759 tr->current_trace = &nop_trace;
8760
8761 INIT_LIST_HEAD(&tr->systems);
8762 INIT_LIST_HEAD(&tr->events);
8763 INIT_LIST_HEAD(&tr->hist_vars);
David Brazdil0f672f62019-12-10 10:32:29 +00008764 INIT_LIST_HEAD(&tr->err_log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008765
8766 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
8767 goto out_free_tr;
8768
Olivier Deprez157378f2022-04-04 15:47:50 +02008769 if (ftrace_allocate_ftrace_ops(tr) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008770 goto out_free_tr;
8771
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008772 ftrace_init_trace_array(tr);
8773
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008774 init_trace_flags_index(tr);
Olivier Deprez157378f2022-04-04 15:47:50 +02008775
8776 if (trace_instance_dir) {
8777 ret = trace_array_create_dir(tr);
8778 if (ret)
8779 goto out_free_tr;
8780 } else
8781 __trace_early_add_events(tr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008782
8783 list_add(&tr->list, &ftrace_trace_arrays);
8784
Olivier Deprez157378f2022-04-04 15:47:50 +02008785 tr->ref++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008786
David Brazdil0f672f62019-12-10 10:32:29 +00008787 return tr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008788
8789 out_free_tr:
Olivier Deprez157378f2022-04-04 15:47:50 +02008790 ftrace_free_ftrace_ops(tr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008791 free_trace_buffers(tr);
8792 free_cpumask_var(tr->tracing_cpumask);
8793 kfree(tr->name);
8794 kfree(tr);
8795
David Brazdil0f672f62019-12-10 10:32:29 +00008796 return ERR_PTR(ret);
8797}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008798
David Brazdil0f672f62019-12-10 10:32:29 +00008799static int instance_mkdir(const char *name)
8800{
Olivier Deprez157378f2022-04-04 15:47:50 +02008801 struct trace_array *tr;
8802 int ret;
8803
8804 mutex_lock(&event_mutex);
8805 mutex_lock(&trace_types_lock);
8806
8807 ret = -EEXIST;
8808 if (trace_array_find(name))
8809 goto out_unlock;
8810
8811 tr = trace_array_create(name);
8812
8813 ret = PTR_ERR_OR_ZERO(tr);
8814
8815out_unlock:
8816 mutex_unlock(&trace_types_lock);
8817 mutex_unlock(&event_mutex);
8818 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008819}
8820
Olivier Deprez157378f2022-04-04 15:47:50 +02008821/**
8822 * trace_array_get_by_name - Create/Lookup a trace array, given its name.
8823 * @name: The name of the trace array to be looked up/created.
8824 *
8825 * Returns pointer to trace array with given name.
8826 * NULL, if it cannot be created.
8827 *
8828 * NOTE: This function increments the reference counter associated with the
8829 * trace array returned. This makes sure it cannot be freed while in use.
8830 * Use trace_array_put() once the trace array is no longer needed.
8831 * If the trace_array is to be freed, trace_array_destroy() needs to
8832 * be called after the trace_array_put(), or simply let user space delete
8833 * it from the tracefs instances directory. But until the
8834 * trace_array_put() is called, user space can not delete it.
8835 *
8836 */
8837struct trace_array *trace_array_get_by_name(const char *name)
8838{
8839 struct trace_array *tr;
8840
8841 mutex_lock(&event_mutex);
8842 mutex_lock(&trace_types_lock);
8843
8844 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8845 if (tr->name && strcmp(tr->name, name) == 0)
8846 goto out_unlock;
8847 }
8848
8849 tr = trace_array_create(name);
8850
8851 if (IS_ERR(tr))
8852 tr = NULL;
8853out_unlock:
8854 if (tr)
8855 tr->ref++;
8856
8857 mutex_unlock(&trace_types_lock);
8858 mutex_unlock(&event_mutex);
8859 return tr;
8860}
8861EXPORT_SYMBOL_GPL(trace_array_get_by_name);
8862
David Brazdil0f672f62019-12-10 10:32:29 +00008863static int __remove_instance(struct trace_array *tr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008864{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008865 int i;
8866
Olivier Deprez157378f2022-04-04 15:47:50 +02008867 /* Reference counter for a newly created trace array = 1. */
8868 if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
David Brazdil0f672f62019-12-10 10:32:29 +00008869 return -EBUSY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008870
8871 list_del(&tr->list);
8872
8873 /* Disable all the flags that were enabled coming in */
8874 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8875 if ((1 << i) & ZEROED_TRACE_FLAGS)
8876 set_tracer_flag(tr, 1 << i, 0);
8877 }
8878
8879 tracing_set_nop(tr);
8880 clear_ftrace_function_probes(tr);
8881 event_trace_del_tracer(tr);
8882 ftrace_clear_pids(tr);
8883 ftrace_destroy_function_files(tr);
Olivier Deprez157378f2022-04-04 15:47:50 +02008884 tracefs_remove(tr->dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008885 free_trace_buffers(tr);
8886
8887 for (i = 0; i < tr->nr_topts; i++) {
8888 kfree(tr->topts[i].topts);
8889 }
8890 kfree(tr->topts);
8891
8892 free_cpumask_var(tr->tracing_cpumask);
8893 kfree(tr->name);
8894 kfree(tr);
8895
David Brazdil0f672f62019-12-10 10:32:29 +00008896 return 0;
8897}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008898
Olivier Deprez0e641232021-09-23 10:07:05 +02008899int trace_array_destroy(struct trace_array *this_tr)
David Brazdil0f672f62019-12-10 10:32:29 +00008900{
Olivier Deprez0e641232021-09-23 10:07:05 +02008901 struct trace_array *tr;
David Brazdil0f672f62019-12-10 10:32:29 +00008902 int ret;
8903
Olivier Deprez0e641232021-09-23 10:07:05 +02008904 if (!this_tr)
David Brazdil0f672f62019-12-10 10:32:29 +00008905 return -EINVAL;
8906
8907 mutex_lock(&event_mutex);
8908 mutex_lock(&trace_types_lock);
8909
Olivier Deprez0e641232021-09-23 10:07:05 +02008910 ret = -ENODEV;
8911
8912 /* Making sure trace array exists before destroying it. */
8913 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8914 if (tr == this_tr) {
8915 ret = __remove_instance(tr);
8916 break;
8917 }
8918 }
David Brazdil0f672f62019-12-10 10:32:29 +00008919
8920 mutex_unlock(&trace_types_lock);
8921 mutex_unlock(&event_mutex);
8922
8923 return ret;
8924}
8925EXPORT_SYMBOL_GPL(trace_array_destroy);
8926
8927static int instance_rmdir(const char *name)
8928{
8929 struct trace_array *tr;
8930 int ret;
8931
8932 mutex_lock(&event_mutex);
8933 mutex_lock(&trace_types_lock);
8934
8935 ret = -ENODEV;
Olivier Deprez157378f2022-04-04 15:47:50 +02008936 tr = trace_array_find(name);
8937 if (tr)
8938 ret = __remove_instance(tr);
David Brazdil0f672f62019-12-10 10:32:29 +00008939
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008940 mutex_unlock(&trace_types_lock);
8941 mutex_unlock(&event_mutex);
8942
8943 return ret;
8944}
8945
8946static __init void create_trace_instances(struct dentry *d_tracer)
8947{
Olivier Deprez157378f2022-04-04 15:47:50 +02008948 struct trace_array *tr;
8949
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008950 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8951 instance_mkdir,
8952 instance_rmdir);
Olivier Deprez157378f2022-04-04 15:47:50 +02008953 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008954 return;
Olivier Deprez157378f2022-04-04 15:47:50 +02008955
8956 mutex_lock(&event_mutex);
8957 mutex_lock(&trace_types_lock);
8958
8959 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8960 if (!tr->name)
8961 continue;
8962 if (MEM_FAIL(trace_array_create_dir(tr) < 0,
8963 "Failed to create instance directory\n"))
8964 break;
8965 }
8966
8967 mutex_unlock(&trace_types_lock);
8968 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008969}
8970
8971static void
8972init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
8973{
8974 struct trace_event_file *file;
8975 int cpu;
8976
8977 trace_create_file("available_tracers", 0444, d_tracer,
8978 tr, &show_traces_fops);
8979
8980 trace_create_file("current_tracer", 0644, d_tracer,
8981 tr, &set_tracer_fops);
8982
8983 trace_create_file("tracing_cpumask", 0644, d_tracer,
8984 tr, &tracing_cpumask_fops);
8985
8986 trace_create_file("trace_options", 0644, d_tracer,
8987 tr, &tracing_iter_fops);
8988
8989 trace_create_file("trace", 0644, d_tracer,
8990 tr, &tracing_fops);
8991
8992 trace_create_file("trace_pipe", 0444, d_tracer,
8993 tr, &tracing_pipe_fops);
8994
8995 trace_create_file("buffer_size_kb", 0644, d_tracer,
8996 tr, &tracing_entries_fops);
8997
8998 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8999 tr, &tracing_total_entries_fops);
9000
9001 trace_create_file("free_buffer", 0200, d_tracer,
9002 tr, &tracing_free_buffer_fops);
9003
9004 trace_create_file("trace_marker", 0220, d_tracer,
9005 tr, &tracing_mark_fops);
9006
9007 file = __find_event_file(tr, "ftrace", "print");
9008 if (file && file->dir)
9009 trace_create_file("trigger", 0644, file->dir, file,
9010 &event_trigger_fops);
9011 tr->trace_marker_file = file;
9012
9013 trace_create_file("trace_marker_raw", 0220, d_tracer,
9014 tr, &tracing_mark_raw_fops);
9015
9016 trace_create_file("trace_clock", 0644, d_tracer, tr,
9017 &trace_clock_fops);
9018
9019 trace_create_file("tracing_on", 0644, d_tracer,
9020 tr, &rb_simple_fops);
9021
9022 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
9023 &trace_time_stamp_mode_fops);
9024
David Brazdil0f672f62019-12-10 10:32:29 +00009025 tr->buffer_percent = 50;
9026
9027 trace_create_file("buffer_percent", 0444, d_tracer,
9028 tr, &buffer_percent_fops);
9029
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009030 create_trace_options_dir(tr);
9031
9032#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Olivier Deprez157378f2022-04-04 15:47:50 +02009033 trace_create_maxlat_file(tr, d_tracer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009034#endif
9035
9036 if (ftrace_create_function_files(tr, d_tracer))
Olivier Deprez157378f2022-04-04 15:47:50 +02009037 MEM_FAIL(1, "Could not allocate function filter files");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009038
9039#ifdef CONFIG_TRACER_SNAPSHOT
9040 trace_create_file("snapshot", 0644, d_tracer,
9041 tr, &snapshot_fops);
9042#endif
9043
David Brazdil0f672f62019-12-10 10:32:29 +00009044 trace_create_file("error_log", 0644, d_tracer,
9045 tr, &tracing_err_log_fops);
9046
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009047 for_each_tracing_cpu(cpu)
9048 tracing_init_tracefs_percpu(tr, cpu);
9049
9050 ftrace_init_tracefs(tr, d_tracer);
9051}
9052
9053static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
9054{
9055 struct vfsmount *mnt;
9056 struct file_system_type *type;
9057
9058 /*
9059 * To maintain backward compatibility for tools that mount
9060 * debugfs to get to the tracing facility, tracefs is automatically
9061 * mounted to the debugfs/tracing directory.
9062 */
9063 type = get_fs_type("tracefs");
9064 if (!type)
9065 return NULL;
9066 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
9067 put_filesystem(type);
9068 if (IS_ERR(mnt))
9069 return NULL;
9070 mntget(mnt);
9071
9072 return mnt;
9073}
9074
9075/**
9076 * tracing_init_dentry - initialize top level trace array
9077 *
9078 * This is called when creating files or directories in the tracing
9079 * directory. It is called via fs_initcall() by any of the boot up code
9080 * and expects to return the dentry of the top level tracing directory.
9081 */
Olivier Deprez157378f2022-04-04 15:47:50 +02009082int tracing_init_dentry(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009083{
9084 struct trace_array *tr = &global_trace;
9085
Olivier Deprez0e641232021-09-23 10:07:05 +02009086 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02009087 pr_warn("Tracing disabled due to lockdown\n");
9088 return -EPERM;
Olivier Deprez0e641232021-09-23 10:07:05 +02009089 }
9090
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009091 /* The top level trace array uses NULL as parent */
9092 if (tr->dir)
Olivier Deprez157378f2022-04-04 15:47:50 +02009093 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009094
Olivier Deprez157378f2022-04-04 15:47:50 +02009095 if (WARN_ON(!tracefs_initialized()))
9096 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009097
9098 /*
9099 * As there may still be users that expect the tracing
9100 * files to exist in debugfs/tracing, we must automount
9101 * the tracefs file system there, so older tools still
9102 * work with the newer kerenl.
9103 */
9104 tr->dir = debugfs_create_automount("tracing", NULL,
9105 trace_automount, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009106
Olivier Deprez157378f2022-04-04 15:47:50 +02009107 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009108}
9109
9110extern struct trace_eval_map *__start_ftrace_eval_maps[];
9111extern struct trace_eval_map *__stop_ftrace_eval_maps[];
9112
9113static void __init trace_eval_init(void)
9114{
9115 int len;
9116
9117 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
9118 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
9119}
9120
9121#ifdef CONFIG_MODULES
9122static void trace_module_add_evals(struct module *mod)
9123{
9124 if (!mod->num_trace_evals)
9125 return;
9126
9127 /*
9128 * Modules with bad taint do not have events created, do
9129 * not bother with enums either.
9130 */
9131 if (trace_module_has_bad_taint(mod))
9132 return;
9133
9134 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
9135}
9136
9137#ifdef CONFIG_TRACE_EVAL_MAP_FILE
9138static void trace_module_remove_evals(struct module *mod)
9139{
9140 union trace_eval_map_item *map;
9141 union trace_eval_map_item **last = &trace_eval_maps;
9142
9143 if (!mod->num_trace_evals)
9144 return;
9145
9146 mutex_lock(&trace_eval_mutex);
9147
9148 map = trace_eval_maps;
9149
9150 while (map) {
9151 if (map->head.mod == mod)
9152 break;
9153 map = trace_eval_jmp_to_tail(map);
9154 last = &map->tail.next;
9155 map = map->tail.next;
9156 }
9157 if (!map)
9158 goto out;
9159
9160 *last = trace_eval_jmp_to_tail(map)->tail.next;
9161 kfree(map);
9162 out:
9163 mutex_unlock(&trace_eval_mutex);
9164}
9165#else
9166static inline void trace_module_remove_evals(struct module *mod) { }
9167#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
9168
9169static int trace_module_notify(struct notifier_block *self,
9170 unsigned long val, void *data)
9171{
9172 struct module *mod = data;
9173
9174 switch (val) {
9175 case MODULE_STATE_COMING:
9176 trace_module_add_evals(mod);
9177 break;
9178 case MODULE_STATE_GOING:
9179 trace_module_remove_evals(mod);
9180 break;
9181 }
9182
Olivier Deprez157378f2022-04-04 15:47:50 +02009183 return NOTIFY_OK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009184}
9185
9186static struct notifier_block trace_module_nb = {
9187 .notifier_call = trace_module_notify,
9188 .priority = 0,
9189};
9190#endif /* CONFIG_MODULES */
9191
9192static __init int tracer_init_tracefs(void)
9193{
Olivier Deprez157378f2022-04-04 15:47:50 +02009194 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009195
9196 trace_access_lock_init();
9197
Olivier Deprez157378f2022-04-04 15:47:50 +02009198 ret = tracing_init_dentry();
9199 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009200 return 0;
9201
9202 event_trace_init();
9203
Olivier Deprez157378f2022-04-04 15:47:50 +02009204 init_tracer_tracefs(&global_trace, NULL);
9205 ftrace_init_tracefs_toplevel(&global_trace, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009206
Olivier Deprez157378f2022-04-04 15:47:50 +02009207 trace_create_file("tracing_thresh", 0644, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009208 &global_trace, &tracing_thresh_fops);
9209
Olivier Deprez157378f2022-04-04 15:47:50 +02009210 trace_create_file("README", 0444, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009211 NULL, &tracing_readme_fops);
9212
Olivier Deprez157378f2022-04-04 15:47:50 +02009213 trace_create_file("saved_cmdlines", 0444, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009214 NULL, &tracing_saved_cmdlines_fops);
9215
Olivier Deprez157378f2022-04-04 15:47:50 +02009216 trace_create_file("saved_cmdlines_size", 0644, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009217 NULL, &tracing_saved_cmdlines_size_fops);
9218
Olivier Deprez157378f2022-04-04 15:47:50 +02009219 trace_create_file("saved_tgids", 0444, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009220 NULL, &tracing_saved_tgids_fops);
9221
9222 trace_eval_init();
9223
Olivier Deprez157378f2022-04-04 15:47:50 +02009224 trace_create_eval_file(NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009225
9226#ifdef CONFIG_MODULES
9227 register_module_notifier(&trace_module_nb);
9228#endif
9229
9230#ifdef CONFIG_DYNAMIC_FTRACE
Olivier Deprez157378f2022-04-04 15:47:50 +02009231 trace_create_file("dyn_ftrace_total_info", 0444, NULL,
9232 NULL, &tracing_dyn_info_fops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009233#endif
9234
Olivier Deprez157378f2022-04-04 15:47:50 +02009235 create_trace_instances(NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009236
9237 update_tracer_options(&global_trace);
9238
9239 return 0;
9240}
9241
9242static int trace_panic_handler(struct notifier_block *this,
9243 unsigned long event, void *unused)
9244{
9245 if (ftrace_dump_on_oops)
9246 ftrace_dump(ftrace_dump_on_oops);
9247 return NOTIFY_OK;
9248}
9249
9250static struct notifier_block trace_panic_notifier = {
9251 .notifier_call = trace_panic_handler,
9252 .next = NULL,
9253 .priority = 150 /* priority: INT_MAX >= x >= 0 */
9254};
9255
9256static int trace_die_handler(struct notifier_block *self,
9257 unsigned long val,
9258 void *data)
9259{
9260 switch (val) {
9261 case DIE_OOPS:
9262 if (ftrace_dump_on_oops)
9263 ftrace_dump(ftrace_dump_on_oops);
9264 break;
9265 default:
9266 break;
9267 }
9268 return NOTIFY_OK;
9269}
9270
9271static struct notifier_block trace_die_notifier = {
9272 .notifier_call = trace_die_handler,
9273 .priority = 200
9274};
9275
9276/*
9277 * printk is set to max of 1024, we really don't need it that big.
9278 * Nothing should be printing 1000 characters anyway.
9279 */
9280#define TRACE_MAX_PRINT 1000
9281
9282/*
9283 * Define here KERN_TRACE so that we have one place to modify
9284 * it if we decide to change what log level the ftrace dump
9285 * should be at.
9286 */
9287#define KERN_TRACE KERN_EMERG
9288
9289void
9290trace_printk_seq(struct trace_seq *s)
9291{
9292 /* Probably should print a warning here. */
9293 if (s->seq.len >= TRACE_MAX_PRINT)
9294 s->seq.len = TRACE_MAX_PRINT;
9295
9296 /*
9297 * More paranoid code. Although the buffer size is set to
9298 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
9299 * an extra layer of protection.
9300 */
9301 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
9302 s->seq.len = s->seq.size - 1;
9303
9304 /* should be zero ended, but we are paranoid. */
9305 s->buffer[s->seq.len] = 0;
9306
9307 printk(KERN_TRACE "%s", s->buffer);
9308
9309 trace_seq_init(s);
9310}
9311
9312void trace_init_global_iter(struct trace_iterator *iter)
9313{
9314 iter->tr = &global_trace;
9315 iter->trace = iter->tr->current_trace;
9316 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Olivier Deprez157378f2022-04-04 15:47:50 +02009317 iter->array_buffer = &global_trace.array_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009318
9319 if (iter->trace && iter->trace->open)
9320 iter->trace->open(iter);
9321
9322 /* Annotate start of buffers if we had overruns */
Olivier Deprez157378f2022-04-04 15:47:50 +02009323 if (ring_buffer_overruns(iter->array_buffer->buffer))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009324 iter->iter_flags |= TRACE_FILE_ANNOTATE;
9325
9326 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
9327 if (trace_clocks[iter->tr->clock_id].in_ns)
9328 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
9329}
9330
9331void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
9332{
9333 /* use static because iter can be a bit big for the stack */
9334 static struct trace_iterator iter;
9335 static atomic_t dump_running;
9336 struct trace_array *tr = &global_trace;
9337 unsigned int old_userobj;
9338 unsigned long flags;
9339 int cnt = 0, cpu;
9340
9341 /* Only allow one dump user at a time. */
9342 if (atomic_inc_return(&dump_running) != 1) {
9343 atomic_dec(&dump_running);
9344 return;
9345 }
9346
9347 /*
9348 * Always turn off tracing when we dump.
9349 * We don't need to show trace output of what happens
9350 * between multiple crashes.
9351 *
9352 * If the user does a sysrq-z, then they can re-enable
9353 * tracing with echo 1 > tracing_on.
9354 */
9355 tracing_off();
9356
9357 local_irq_save(flags);
9358 printk_nmi_direct_enter();
9359
9360 /* Simulate the iterator */
9361 trace_init_global_iter(&iter);
Olivier Deprez157378f2022-04-04 15:47:50 +02009362 /* Can not use kmalloc for iter.temp */
9363 iter.temp = static_temp_buf;
9364 iter.temp_size = STATIC_TEMP_BUF_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009365
9366 for_each_tracing_cpu(cpu) {
Olivier Deprez157378f2022-04-04 15:47:50 +02009367 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009368 }
9369
9370 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
9371
9372 /* don't look at user memory in panic mode */
9373 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
9374
9375 switch (oops_dump_mode) {
9376 case DUMP_ALL:
9377 iter.cpu_file = RING_BUFFER_ALL_CPUS;
9378 break;
9379 case DUMP_ORIG:
9380 iter.cpu_file = raw_smp_processor_id();
9381 break;
9382 case DUMP_NONE:
9383 goto out_enable;
9384 default:
9385 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
9386 iter.cpu_file = RING_BUFFER_ALL_CPUS;
9387 }
9388
9389 printk(KERN_TRACE "Dumping ftrace buffer:\n");
9390
9391 /* Did function tracer already get disabled? */
9392 if (ftrace_is_dead()) {
9393 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
9394 printk("# MAY BE MISSING FUNCTION EVENTS\n");
9395 }
9396
9397 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02009398 * We need to stop all tracing on all CPUS to read
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009399 * the next buffer. This is a bit expensive, but is
9400 * not done often. We fill all what we can read,
9401 * and then release the locks again.
9402 */
9403
9404 while (!trace_empty(&iter)) {
9405
9406 if (!cnt)
9407 printk(KERN_TRACE "---------------------------------\n");
9408
9409 cnt++;
9410
David Brazdil0f672f62019-12-10 10:32:29 +00009411 trace_iterator_reset(&iter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009412 iter.iter_flags |= TRACE_FILE_LAT_FMT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009413
9414 if (trace_find_next_entry_inc(&iter) != NULL) {
9415 int ret;
9416
9417 ret = print_trace_line(&iter);
9418 if (ret != TRACE_TYPE_NO_CONSUME)
9419 trace_consume(&iter);
9420 }
9421 touch_nmi_watchdog();
9422
9423 trace_printk_seq(&iter.seq);
9424 }
9425
9426 if (!cnt)
9427 printk(KERN_TRACE " (ftrace buffer empty)\n");
9428 else
9429 printk(KERN_TRACE "---------------------------------\n");
9430
9431 out_enable:
9432 tr->trace_flags |= old_userobj;
9433
9434 for_each_tracing_cpu(cpu) {
Olivier Deprez157378f2022-04-04 15:47:50 +02009435 atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009436 }
9437 atomic_dec(&dump_running);
9438 printk_nmi_direct_exit();
9439 local_irq_restore(flags);
9440}
9441EXPORT_SYMBOL_GPL(ftrace_dump);
9442
9443int trace_run_command(const char *buf, int (*createfn)(int, char **))
9444{
9445 char **argv;
9446 int argc, ret;
9447
9448 argc = 0;
9449 ret = 0;
9450 argv = argv_split(GFP_KERNEL, buf, &argc);
9451 if (!argv)
9452 return -ENOMEM;
9453
9454 if (argc)
9455 ret = createfn(argc, argv);
9456
9457 argv_free(argv);
9458
9459 return ret;
9460}
9461
9462#define WRITE_BUFSIZE 4096
9463
9464ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
9465 size_t count, loff_t *ppos,
9466 int (*createfn)(int, char **))
9467{
9468 char *kbuf, *buf, *tmp;
9469 int ret = 0;
9470 size_t done = 0;
9471 size_t size;
9472
9473 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
9474 if (!kbuf)
9475 return -ENOMEM;
9476
9477 while (done < count) {
9478 size = count - done;
9479
9480 if (size >= WRITE_BUFSIZE)
9481 size = WRITE_BUFSIZE - 1;
9482
9483 if (copy_from_user(kbuf, buffer + done, size)) {
9484 ret = -EFAULT;
9485 goto out;
9486 }
9487 kbuf[size] = '\0';
9488 buf = kbuf;
9489 do {
9490 tmp = strchr(buf, '\n');
9491 if (tmp) {
9492 *tmp = '\0';
9493 size = tmp - buf + 1;
9494 } else {
9495 size = strlen(buf);
9496 if (done + size < count) {
9497 if (buf != kbuf)
9498 break;
9499 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
9500 pr_warn("Line length is too long: Should be less than %d\n",
9501 WRITE_BUFSIZE - 2);
9502 ret = -EINVAL;
9503 goto out;
9504 }
9505 }
9506 done += size;
9507
9508 /* Remove comments */
9509 tmp = strchr(buf, '#');
9510
9511 if (tmp)
9512 *tmp = '\0';
9513
9514 ret = trace_run_command(buf, createfn);
9515 if (ret)
9516 goto out;
9517 buf += size;
9518
9519 } while (done < count);
9520 }
9521 ret = done;
9522
9523out:
9524 kfree(kbuf);
9525
9526 return ret;
9527}
9528
9529__init static int tracer_alloc_buffers(void)
9530{
9531 int ring_buf_size;
9532 int ret = -ENOMEM;
9533
Olivier Deprez0e641232021-09-23 10:07:05 +02009534
9535 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02009536 pr_warn("Tracing disabled due to lockdown\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02009537 return -EPERM;
9538 }
9539
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009540 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02009541 * Make sure we don't accidentally add more trace options
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009542 * than we have bits for.
9543 */
9544 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
9545
9546 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
9547 goto out;
9548
9549 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
9550 goto out_free_buffer_mask;
9551
9552 /* Only allocate trace_printk buffers if a trace_printk exists */
Olivier Deprez0e641232021-09-23 10:07:05 +02009553 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009554 /* Must be called before global_trace.buffer is allocated */
9555 trace_printk_init_buffers();
9556
9557 /* To save memory, keep the ring buffer size to its minimum */
9558 if (ring_buffer_expanded)
9559 ring_buf_size = trace_buf_size;
9560 else
9561 ring_buf_size = 1;
9562
9563 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
9564 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
9565
9566 raw_spin_lock_init(&global_trace.start_lock);
9567
9568 /*
9569 * The prepare callbacks allocates some memory for the ring buffer. We
Olivier Deprez157378f2022-04-04 15:47:50 +02009570 * don't free the buffer if the CPU goes down. If we were to free
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009571 * the buffer, then the user would lose any trace that was in the
9572 * buffer. The memory will be removed once the "instance" is removed.
9573 */
9574 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
9575 "trace/RB:preapre", trace_rb_cpu_prepare,
9576 NULL);
9577 if (ret < 0)
9578 goto out_free_cpumask;
9579 /* Used for event triggers */
9580 ret = -ENOMEM;
9581 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
9582 if (!temp_buffer)
9583 goto out_rm_hp_state;
9584
9585 if (trace_create_savedcmd() < 0)
9586 goto out_free_temp_buffer;
9587
9588 /* TODO: make the number of buffers hot pluggable with CPUS */
9589 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02009590 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009591 goto out_free_savedcmd;
9592 }
9593
9594 if (global_trace.buffer_disabled)
9595 tracing_off();
9596
9597 if (trace_boot_clock) {
9598 ret = tracing_set_clock(&global_trace, trace_boot_clock);
9599 if (ret < 0)
9600 pr_warn("Trace clock %s not defined, going back to default\n",
9601 trace_boot_clock);
9602 }
9603
9604 /*
9605 * register_tracer() might reference current_trace, so it
9606 * needs to be set before we register anything. This is
9607 * just a bootstrap of current_trace anyway.
9608 */
9609 global_trace.current_trace = &nop_trace;
9610
9611 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9612
9613 ftrace_init_global_array_ops(&global_trace);
9614
9615 init_trace_flags_index(&global_trace);
9616
9617 register_tracer(&nop_trace);
9618
9619 /* Function tracing may start here (via kernel command line) */
9620 init_function_trace();
9621
9622 /* All seems OK, enable tracing */
9623 tracing_disabled = 0;
9624
9625 atomic_notifier_chain_register(&panic_notifier_list,
9626 &trace_panic_notifier);
9627
9628 register_die_notifier(&trace_die_notifier);
9629
9630 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
9631
9632 INIT_LIST_HEAD(&global_trace.systems);
9633 INIT_LIST_HEAD(&global_trace.events);
9634 INIT_LIST_HEAD(&global_trace.hist_vars);
David Brazdil0f672f62019-12-10 10:32:29 +00009635 INIT_LIST_HEAD(&global_trace.err_log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009636 list_add(&global_trace.list, &ftrace_trace_arrays);
9637
9638 apply_trace_boot_options();
9639
9640 register_snapshot_cmd();
9641
9642 return 0;
9643
9644out_free_savedcmd:
9645 free_saved_cmdlines_buffer(savedcmd);
9646out_free_temp_buffer:
9647 ring_buffer_free(temp_buffer);
9648out_rm_hp_state:
9649 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
9650out_free_cpumask:
9651 free_cpumask_var(global_trace.tracing_cpumask);
9652out_free_buffer_mask:
9653 free_cpumask_var(tracing_buffer_mask);
9654out:
9655 return ret;
9656}
9657
9658void __init early_trace_init(void)
9659{
9660 if (tracepoint_printk) {
9661 tracepoint_print_iter =
9662 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02009663 if (MEM_FAIL(!tracepoint_print_iter,
9664 "Failed to allocate trace iterator\n"))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009665 tracepoint_printk = 0;
9666 else
9667 static_key_enable(&tracepoint_printk_key.key);
9668 }
9669 tracer_alloc_buffers();
9670}
9671
9672void __init trace_init(void)
9673{
9674 trace_event_init();
9675}
9676
9677__init static int clear_boot_tracer(void)
9678{
9679 /*
9680 * The default tracer at boot buffer is an init section.
9681 * This function is called in lateinit. If we did not
9682 * find the boot tracer, then clear it out, to prevent
9683 * later registration from accessing the buffer that is
9684 * about to be freed.
9685 */
9686 if (!default_bootup_tracer)
9687 return 0;
9688
9689 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
9690 default_bootup_tracer);
9691 default_bootup_tracer = NULL;
9692
9693 return 0;
9694}
9695
9696fs_initcall(tracer_init_tracefs);
9697late_initcall_sync(clear_boot_tracer);
9698
9699#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
9700__init static int tracing_set_default_clock(void)
9701{
9702 /* sched_clock_stable() is determined in late_initcall */
9703 if (!trace_boot_clock && !sched_clock_stable()) {
Olivier Deprez0e641232021-09-23 10:07:05 +02009704 if (security_locked_down(LOCKDOWN_TRACEFS)) {
9705 pr_warn("Can not set tracing clock due to lockdown\n");
9706 return -EPERM;
9707 }
9708
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009709 printk(KERN_WARNING
9710 "Unstable clock detected, switching default tracing clock to \"global\"\n"
9711 "If you want to keep using the local clock, then add:\n"
9712 " \"trace_clock=local\"\n"
9713 "on the kernel command line\n");
9714 tracing_set_clock(&global_trace, "global");
9715 }
9716
9717 return 0;
9718}
9719late_initcall_sync(tracing_set_default_clock);
9720#endif