blob: 5240ba9a82db8b78249b1023be219f0d75881175 [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>
48
49#include "trace.h"
50#include "trace_output.h"
51
52/*
53 * On boot up, the ring buffer is set to the minimum size, so that
54 * we do not waste memory on systems that are not using tracing.
55 */
56bool ring_buffer_expanded;
57
58/*
59 * We need to change this state when a selftest is running.
60 * A selftest will lurk into the ring-buffer to count the
61 * entries inserted during the selftest although some concurrent
62 * insertions into the ring-buffer such as trace_printk could occurred
63 * at the same time, giving false positive or negative results.
64 */
65static bool __read_mostly tracing_selftest_running;
66
67/*
68 * If a tracer is running, we do not want to run SELFTEST.
69 */
70bool __read_mostly tracing_selftest_disabled;
71
72/* Pipe tracepoints to printk */
73struct trace_iterator *tracepoint_print_iter;
74int tracepoint_printk;
75static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
76
77/* For tracers that don't implement custom flags */
78static struct tracer_opt dummy_tracer_opt[] = {
79 { }
80};
81
82static int
83dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
84{
85 return 0;
86}
87
88/*
89 * To prevent the comm cache from being overwritten when no
90 * tracing is active, only save the comm when a trace event
91 * occurred.
92 */
93static DEFINE_PER_CPU(bool, trace_taskinfo_save);
94
95/*
96 * Kill all tracing for good (never come back).
97 * It is initialized to 1 but will turn to zero if the initialization
98 * of the tracer is successful. But that is the only place that sets
99 * this back to zero.
100 */
101static int tracing_disabled = 1;
102
103cpumask_var_t __read_mostly tracing_buffer_mask;
104
105/*
106 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 *
108 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
109 * is set, then ftrace_dump is called. This will output the contents
110 * of the ftrace buffers to the console. This is very useful for
111 * capturing traces that lead to crashes and outputing it to a
112 * serial console.
113 *
114 * It is default off, but you can enable it with either specifying
115 * "ftrace_dump_on_oops" in the kernel command line, or setting
116 * /proc/sys/kernel/ftrace_dump_on_oops
117 * Set 1 if you want to dump buffers of all CPUs
118 * Set 2 if you want to dump the buffer of the CPU that triggered oops
119 */
120
121enum ftrace_dump_mode ftrace_dump_on_oops;
122
123/* When set, tracing will stop when a WARN*() is hit */
124int __disable_trace_on_warning;
125
126#ifdef CONFIG_TRACE_EVAL_MAP_FILE
127/* Map of enums to their values, for "eval_map" file */
128struct trace_eval_map_head {
129 struct module *mod;
130 unsigned long length;
131};
132
133union trace_eval_map_item;
134
135struct trace_eval_map_tail {
136 /*
137 * "end" is first and points to NULL as it must be different
138 * than "mod" or "eval_string"
139 */
140 union trace_eval_map_item *next;
141 const char *end; /* points to NULL */
142};
143
144static DEFINE_MUTEX(trace_eval_mutex);
145
146/*
147 * The trace_eval_maps are saved in an array with two extra elements,
148 * one at the beginning, and one at the end. The beginning item contains
149 * the count of the saved maps (head.length), and the module they
150 * belong to if not built in (head.mod). The ending item contains a
151 * pointer to the next array of saved eval_map items.
152 */
153union trace_eval_map_item {
154 struct trace_eval_map map;
155 struct trace_eval_map_head head;
156 struct trace_eval_map_tail tail;
157};
158
159static union trace_eval_map_item *trace_eval_maps;
160#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
161
162static int tracing_set_tracer(struct trace_array *tr, const char *buf);
Olivier Deprez0e641232021-09-23 10:07:05 +0200163static void ftrace_trace_userstack(struct trace_array *tr,
164 struct ring_buffer *buffer,
David Brazdil0f672f62019-12-10 10:32:29 +0000165 unsigned long flags, int pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166
167#define MAX_TRACER_SIZE 100
168static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
169static char *default_bootup_tracer;
170
171static bool allocate_snapshot;
172
173static int __init set_cmdline_ftrace(char *str)
174{
175 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
176 default_bootup_tracer = bootup_tracer_buf;
177 /* We are using ftrace early, expand it */
178 ring_buffer_expanded = true;
179 return 1;
180}
181__setup("ftrace=", set_cmdline_ftrace);
182
183static int __init set_ftrace_dump_on_oops(char *str)
184{
185 if (*str++ != '=' || !*str) {
186 ftrace_dump_on_oops = DUMP_ALL;
187 return 1;
188 }
189
190 if (!strcmp("orig_cpu", str)) {
191 ftrace_dump_on_oops = DUMP_ORIG;
192 return 1;
193 }
194
195 return 0;
196}
197__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
198
199static int __init stop_trace_on_warning(char *str)
200{
201 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
202 __disable_trace_on_warning = 1;
203 return 1;
204}
205__setup("traceoff_on_warning", stop_trace_on_warning);
206
207static int __init boot_alloc_snapshot(char *str)
208{
209 allocate_snapshot = true;
210 /* We also need the main ring buffer expanded */
211 ring_buffer_expanded = true;
212 return 1;
213}
214__setup("alloc_snapshot", boot_alloc_snapshot);
215
216
217static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
218
219static int __init set_trace_boot_options(char *str)
220{
221 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
222 return 0;
223}
224__setup("trace_options=", set_trace_boot_options);
225
226static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
227static char *trace_boot_clock __initdata;
228
229static int __init set_trace_boot_clock(char *str)
230{
231 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
232 trace_boot_clock = trace_boot_clock_buf;
233 return 0;
234}
235__setup("trace_clock=", set_trace_boot_clock);
236
237static int __init set_tracepoint_printk(char *str)
238{
239 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
240 tracepoint_printk = 1;
241 return 1;
242}
243__setup("tp_printk", set_tracepoint_printk);
244
245unsigned long long ns2usecs(u64 nsec)
246{
247 nsec += 500;
248 do_div(nsec, 1000);
249 return nsec;
250}
251
252/* trace_flags holds trace_options default values */
253#define TRACE_DEFAULT_FLAGS \
254 (FUNCTION_DEFAULT_FLAGS | \
255 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
256 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
257 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
258 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
259
260/* trace_options that are only supported by global_trace */
261#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
262 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
263
264/* trace_flags that are default zero for instances */
265#define ZEROED_TRACE_FLAGS \
266 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
267
268/*
269 * The global_trace is the descriptor that holds the top-level tracing
270 * buffers for the live tracing.
271 */
272static struct trace_array global_trace = {
273 .trace_flags = TRACE_DEFAULT_FLAGS,
274};
275
276LIST_HEAD(ftrace_trace_arrays);
277
278int trace_array_get(struct trace_array *this_tr)
279{
280 struct trace_array *tr;
281 int ret = -ENODEV;
282
283 mutex_lock(&trace_types_lock);
284 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
285 if (tr == this_tr) {
286 tr->ref++;
287 ret = 0;
288 break;
289 }
290 }
291 mutex_unlock(&trace_types_lock);
292
293 return ret;
294}
295
296static void __trace_array_put(struct trace_array *this_tr)
297{
298 WARN_ON(!this_tr->ref);
299 this_tr->ref--;
300}
301
302void trace_array_put(struct trace_array *this_tr)
303{
304 mutex_lock(&trace_types_lock);
305 __trace_array_put(this_tr);
306 mutex_unlock(&trace_types_lock);
307}
308
David Brazdil0f672f62019-12-10 10:32:29 +0000309int tracing_check_open_get_tr(struct trace_array *tr)
310{
311 int ret;
312
313 ret = security_locked_down(LOCKDOWN_TRACEFS);
314 if (ret)
315 return ret;
316
317 if (tracing_disabled)
318 return -ENODEV;
319
320 if (tr && trace_array_get(tr) < 0)
321 return -ENODEV;
322
323 return 0;
324}
325
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326int call_filter_check_discard(struct trace_event_call *call, void *rec,
327 struct ring_buffer *buffer,
328 struct ring_buffer_event *event)
329{
330 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
331 !filter_match_preds(call->filter, rec)) {
332 __trace_event_discard_commit(buffer, event);
333 return 1;
334 }
335
336 return 0;
337}
338
339void trace_free_pid_list(struct trace_pid_list *pid_list)
340{
341 vfree(pid_list->pids);
342 kfree(pid_list);
343}
344
345/**
346 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
347 * @filtered_pids: The list of pids to check
348 * @search_pid: The PID to find in @filtered_pids
349 *
350 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
351 */
352bool
353trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
354{
355 /*
356 * If pid_max changed after filtered_pids was created, we
357 * by default ignore all pids greater than the previous pid_max.
358 */
359 if (search_pid >= filtered_pids->pid_max)
360 return false;
361
362 return test_bit(search_pid, filtered_pids->pids);
363}
364
365/**
366 * trace_ignore_this_task - should a task be ignored for tracing
367 * @filtered_pids: The list of pids to check
368 * @task: The task that should be ignored if not filtered
369 *
370 * Checks if @task should be traced or not from @filtered_pids.
371 * Returns true if @task should *NOT* be traced.
372 * Returns false if @task should be traced.
373 */
374bool
375trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
376{
377 /*
378 * Return false, because if filtered_pids does not exist,
379 * all pids are good to trace.
380 */
381 if (!filtered_pids)
382 return false;
383
384 return !trace_find_filtered_pid(filtered_pids, task->pid);
385}
386
387/**
David Brazdil0f672f62019-12-10 10:32:29 +0000388 * trace_filter_add_remove_task - Add or remove a task from a pid_list
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 * @pid_list: The list to modify
390 * @self: The current task for fork or NULL for exit
391 * @task: The task to add or remove
392 *
393 * If adding a task, if @self is defined, the task is only added if @self
394 * is also included in @pid_list. This happens on fork and tasks should
395 * only be added when the parent is listed. If @self is NULL, then the
396 * @task pid will be removed from the list, which would happen on exit
397 * of a task.
398 */
399void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
400 struct task_struct *self,
401 struct task_struct *task)
402{
403 if (!pid_list)
404 return;
405
406 /* For forks, we only add if the forking task is listed */
407 if (self) {
408 if (!trace_find_filtered_pid(pid_list, self->pid))
409 return;
410 }
411
412 /* Sorry, but we don't support pid_max changing after setting */
413 if (task->pid >= pid_list->pid_max)
414 return;
415
416 /* "self" is set for forks, and NULL for exits */
417 if (self)
418 set_bit(task->pid, pid_list->pids);
419 else
420 clear_bit(task->pid, pid_list->pids);
421}
422
423/**
424 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
425 * @pid_list: The pid list to show
426 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
427 * @pos: The position of the file
428 *
429 * This is used by the seq_file "next" operation to iterate the pids
430 * listed in a trace_pid_list structure.
431 *
432 * Returns the pid+1 as we want to display pid of zero, but NULL would
433 * stop the iteration.
434 */
435void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
436{
437 unsigned long pid = (unsigned long)v;
438
439 (*pos)++;
440
441 /* pid already is +1 of the actual prevous bit */
442 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
443
444 /* Return pid + 1 to allow zero to be represented */
445 if (pid < pid_list->pid_max)
446 return (void *)(pid + 1);
447
448 return NULL;
449}
450
451/**
452 * trace_pid_start - Used for seq_file to start reading pid lists
453 * @pid_list: The pid list to show
454 * @pos: The position of the file
455 *
456 * This is used by seq_file "start" operation to start the iteration
457 * of listing pids.
458 *
459 * Returns the pid+1 as we want to display pid of zero, but NULL would
460 * stop the iteration.
461 */
462void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
463{
464 unsigned long pid;
465 loff_t l = 0;
466
467 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
468 if (pid >= pid_list->pid_max)
469 return NULL;
470
471 /* Return pid + 1 so that zero can be the exit value */
472 for (pid++; pid && l < *pos;
473 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
474 ;
475 return (void *)pid;
476}
477
478/**
479 * trace_pid_show - show the current pid in seq_file processing
480 * @m: The seq_file structure to write into
481 * @v: A void pointer of the pid (+1) value to display
482 *
483 * Can be directly used by seq_file operations to display the current
484 * pid value.
485 */
486int trace_pid_show(struct seq_file *m, void *v)
487{
488 unsigned long pid = (unsigned long)v - 1;
489
490 seq_printf(m, "%lu\n", pid);
491 return 0;
492}
493
494/* 128 should be much more than enough */
495#define PID_BUF_SIZE 127
496
497int trace_pid_write(struct trace_pid_list *filtered_pids,
498 struct trace_pid_list **new_pid_list,
499 const char __user *ubuf, size_t cnt)
500{
501 struct trace_pid_list *pid_list;
502 struct trace_parser parser;
503 unsigned long val;
504 int nr_pids = 0;
505 ssize_t read = 0;
506 ssize_t ret = 0;
507 loff_t pos;
508 pid_t pid;
509
510 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
511 return -ENOMEM;
512
513 /*
514 * Always recreate a new array. The write is an all or nothing
515 * operation. Always create a new array when adding new pids by
516 * the user. If the operation fails, then the current list is
517 * not modified.
518 */
519 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +0000520 if (!pid_list) {
521 trace_parser_put(&parser);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522 return -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +0000523 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524
525 pid_list->pid_max = READ_ONCE(pid_max);
526
527 /* Only truncating will shrink pid_max */
528 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
529 pid_list->pid_max = filtered_pids->pid_max;
530
531 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
532 if (!pid_list->pids) {
David Brazdil0f672f62019-12-10 10:32:29 +0000533 trace_parser_put(&parser);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 kfree(pid_list);
535 return -ENOMEM;
536 }
537
538 if (filtered_pids) {
539 /* copy the current bits to the new max */
540 for_each_set_bit(pid, filtered_pids->pids,
541 filtered_pids->pid_max) {
542 set_bit(pid, pid_list->pids);
543 nr_pids++;
544 }
545 }
546
547 while (cnt > 0) {
548
549 pos = 0;
550
551 ret = trace_get_user(&parser, ubuf, cnt, &pos);
552 if (ret < 0 || !trace_parser_loaded(&parser))
553 break;
554
555 read += ret;
556 ubuf += ret;
557 cnt -= ret;
558
559 ret = -EINVAL;
560 if (kstrtoul(parser.buffer, 0, &val))
561 break;
562 if (val >= pid_list->pid_max)
563 break;
564
565 pid = (pid_t)val;
566
567 set_bit(pid, pid_list->pids);
568 nr_pids++;
569
570 trace_parser_clear(&parser);
571 ret = 0;
572 }
573 trace_parser_put(&parser);
574
575 if (ret < 0) {
576 trace_free_pid_list(pid_list);
577 return ret;
578 }
579
580 if (!nr_pids) {
581 /* Cleared the list of pids */
582 trace_free_pid_list(pid_list);
583 read = ret;
584 pid_list = NULL;
585 }
586
587 *new_pid_list = pid_list;
588
589 return read;
590}
591
592static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
593{
594 u64 ts;
595
596 /* Early boot up does not have a buffer yet */
597 if (!buf->buffer)
598 return trace_clock_local();
599
600 ts = ring_buffer_time_stamp(buf->buffer, cpu);
601 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
602
603 return ts;
604}
605
606u64 ftrace_now(int cpu)
607{
608 return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
609}
610
611/**
612 * tracing_is_enabled - Show if global_trace has been disabled
613 *
614 * Shows if the global trace has been enabled or not. It uses the
615 * mirror flag "buffer_disabled" to be used in fast paths such as for
616 * the irqsoff tracer. But it may be inaccurate due to races. If you
617 * need to know the accurate state, use tracing_is_on() which is a little
618 * slower, but accurate.
619 */
620int tracing_is_enabled(void)
621{
622 /*
623 * For quick access (irqsoff uses this in fast path), just
624 * return the mirror variable of the state of the ring buffer.
625 * It's a little racy, but we don't really care.
626 */
627 smp_rmb();
628 return !global_trace.buffer_disabled;
629}
630
631/*
632 * trace_buf_size is the size in bytes that is allocated
633 * for a buffer. Note, the number of bytes is always rounded
634 * to page size.
635 *
636 * This number is purposely set to a low number of 16384.
637 * If the dump on oops happens, it will be much appreciated
638 * to not have to wait for all that output. Anyway this can be
639 * boot time and run time configurable.
640 */
641#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
642
643static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
644
645/* trace_types holds a link list of available tracers. */
646static struct tracer *trace_types __read_mostly;
647
648/*
649 * trace_types_lock is used to protect the trace_types list.
650 */
651DEFINE_MUTEX(trace_types_lock);
652
653/*
654 * serialize the access of the ring buffer
655 *
656 * ring buffer serializes readers, but it is low level protection.
657 * The validity of the events (which returns by ring_buffer_peek() ..etc)
658 * are not protected by ring buffer.
659 *
660 * The content of events may become garbage if we allow other process consumes
661 * these events concurrently:
662 * A) the page of the consumed events may become a normal page
663 * (not reader page) in ring buffer, and this page will be rewrited
664 * by events producer.
665 * B) The page of the consumed events may become a page for splice_read,
666 * and this page will be returned to system.
667 *
668 * These primitives allow multi process access to different cpu ring buffer
669 * concurrently.
670 *
671 * These primitives don't distinguish read-only and read-consume access.
672 * Multi read-only access are also serialized.
673 */
674
675#ifdef CONFIG_SMP
676static DECLARE_RWSEM(all_cpu_access_lock);
677static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
678
679static inline void trace_access_lock(int cpu)
680{
681 if (cpu == RING_BUFFER_ALL_CPUS) {
682 /* gain it for accessing the whole ring buffer. */
683 down_write(&all_cpu_access_lock);
684 } else {
685 /* gain it for accessing a cpu ring buffer. */
686
687 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
688 down_read(&all_cpu_access_lock);
689
690 /* Secondly block other access to this @cpu ring buffer. */
691 mutex_lock(&per_cpu(cpu_access_lock, cpu));
692 }
693}
694
695static inline void trace_access_unlock(int cpu)
696{
697 if (cpu == RING_BUFFER_ALL_CPUS) {
698 up_write(&all_cpu_access_lock);
699 } else {
700 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
701 up_read(&all_cpu_access_lock);
702 }
703}
704
705static inline void trace_access_lock_init(void)
706{
707 int cpu;
708
709 for_each_possible_cpu(cpu)
710 mutex_init(&per_cpu(cpu_access_lock, cpu));
711}
712
713#else
714
715static DEFINE_MUTEX(access_lock);
716
717static inline void trace_access_lock(int cpu)
718{
719 (void)cpu;
720 mutex_lock(&access_lock);
721}
722
723static inline void trace_access_unlock(int cpu)
724{
725 (void)cpu;
726 mutex_unlock(&access_lock);
727}
728
729static inline void trace_access_lock_init(void)
730{
731}
732
733#endif
734
735#ifdef CONFIG_STACKTRACE
736static void __ftrace_trace_stack(struct ring_buffer *buffer,
737 unsigned long flags,
738 int skip, int pc, struct pt_regs *regs);
739static inline void ftrace_trace_stack(struct trace_array *tr,
740 struct ring_buffer *buffer,
741 unsigned long flags,
742 int skip, int pc, struct pt_regs *regs);
743
744#else
745static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
746 unsigned long flags,
747 int skip, int pc, struct pt_regs *regs)
748{
749}
750static inline void ftrace_trace_stack(struct trace_array *tr,
751 struct ring_buffer *buffer,
752 unsigned long flags,
753 int skip, int pc, struct pt_regs *regs)
754{
755}
756
757#endif
758
759static __always_inline void
760trace_event_setup(struct ring_buffer_event *event,
761 int type, unsigned long flags, int pc)
762{
763 struct trace_entry *ent = ring_buffer_event_data(event);
764
David Brazdil0f672f62019-12-10 10:32:29 +0000765 tracing_generic_entry_update(ent, type, flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000766}
767
768static __always_inline struct ring_buffer_event *
769__trace_buffer_lock_reserve(struct ring_buffer *buffer,
770 int type,
771 unsigned long len,
772 unsigned long flags, int pc)
773{
774 struct ring_buffer_event *event;
775
776 event = ring_buffer_lock_reserve(buffer, len);
777 if (event != NULL)
778 trace_event_setup(event, type, flags, pc);
779
780 return event;
781}
782
783void tracer_tracing_on(struct trace_array *tr)
784{
785 if (tr->trace_buffer.buffer)
786 ring_buffer_record_on(tr->trace_buffer.buffer);
787 /*
788 * This flag is looked at when buffers haven't been allocated
789 * yet, or by some tracers (like irqsoff), that just want to
790 * know if the ring buffer has been disabled, but it can handle
791 * races of where it gets disabled but we still do a record.
792 * As the check is in the fast path of the tracers, it is more
793 * important to be fast than accurate.
794 */
795 tr->buffer_disabled = 0;
796 /* Make the flag seen by readers */
797 smp_wmb();
798}
799
800/**
801 * tracing_on - enable tracing buffers
802 *
803 * This function enables tracing buffers that may have been
804 * disabled with tracing_off.
805 */
806void tracing_on(void)
807{
808 tracer_tracing_on(&global_trace);
809}
810EXPORT_SYMBOL_GPL(tracing_on);
811
812
813static __always_inline void
814__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
815{
816 __this_cpu_write(trace_taskinfo_save, true);
817
818 /* If this is the temp buffer, we need to commit fully */
819 if (this_cpu_read(trace_buffered_event) == event) {
820 /* Length is in event->array[0] */
821 ring_buffer_write(buffer, event->array[0], &event->array[1]);
822 /* Release the temp buffer */
823 this_cpu_dec(trace_buffered_event_cnt);
824 } else
825 ring_buffer_unlock_commit(buffer, event);
826}
827
828/**
829 * __trace_puts - write a constant string into the trace buffer.
830 * @ip: The address of the caller
831 * @str: The constant string to write
832 * @size: The size of the string.
833 */
834int __trace_puts(unsigned long ip, const char *str, int size)
835{
836 struct ring_buffer_event *event;
837 struct ring_buffer *buffer;
838 struct print_entry *entry;
839 unsigned long irq_flags;
840 int alloc;
841 int pc;
842
843 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
844 return 0;
845
846 pc = preempt_count();
847
848 if (unlikely(tracing_selftest_running || tracing_disabled))
849 return 0;
850
851 alloc = sizeof(*entry) + size + 2; /* possible \n added */
852
853 local_save_flags(irq_flags);
854 buffer = global_trace.trace_buffer.buffer;
855 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
856 irq_flags, pc);
857 if (!event)
858 return 0;
859
860 entry = ring_buffer_event_data(event);
861 entry->ip = ip;
862
863 memcpy(&entry->buf, str, size);
864
865 /* Add a newline if necessary */
866 if (entry->buf[size - 1] != '\n') {
867 entry->buf[size] = '\n';
868 entry->buf[size + 1] = '\0';
869 } else
870 entry->buf[size] = '\0';
871
872 __buffer_unlock_commit(buffer, event);
873 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
874
875 return size;
876}
877EXPORT_SYMBOL_GPL(__trace_puts);
878
879/**
880 * __trace_bputs - write the pointer to a constant string into trace buffer
881 * @ip: The address of the caller
882 * @str: The constant string to write to the buffer to
883 */
884int __trace_bputs(unsigned long ip, const char *str)
885{
886 struct ring_buffer_event *event;
887 struct ring_buffer *buffer;
888 struct bputs_entry *entry;
889 unsigned long irq_flags;
890 int size = sizeof(struct bputs_entry);
891 int pc;
892
893 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
894 return 0;
895
896 pc = preempt_count();
897
898 if (unlikely(tracing_selftest_running || tracing_disabled))
899 return 0;
900
901 local_save_flags(irq_flags);
902 buffer = global_trace.trace_buffer.buffer;
903 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
904 irq_flags, pc);
905 if (!event)
906 return 0;
907
908 entry = ring_buffer_event_data(event);
909 entry->ip = ip;
910 entry->str = str;
911
912 __buffer_unlock_commit(buffer, event);
913 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
914
915 return 1;
916}
917EXPORT_SYMBOL_GPL(__trace_bputs);
918
919#ifdef CONFIG_TRACER_SNAPSHOT
David Brazdil0f672f62019-12-10 10:32:29 +0000920void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921{
922 struct tracer *tracer = tr->current_trace;
923 unsigned long flags;
924
925 if (in_nmi()) {
926 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
927 internal_trace_puts("*** snapshot is being ignored ***\n");
928 return;
929 }
930
931 if (!tr->allocated_snapshot) {
932 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
933 internal_trace_puts("*** stopping trace here! ***\n");
934 tracing_off();
935 return;
936 }
937
938 /* Note, snapshot can not be used when the tracer uses it */
939 if (tracer->use_max_tr) {
940 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
941 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
942 return;
943 }
944
945 local_irq_save(flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000946 update_max_tr(tr, current, smp_processor_id(), cond_data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000947 local_irq_restore(flags);
948}
949
David Brazdil0f672f62019-12-10 10:32:29 +0000950void tracing_snapshot_instance(struct trace_array *tr)
951{
952 tracing_snapshot_instance_cond(tr, NULL);
953}
954
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955/**
956 * tracing_snapshot - take a snapshot of the current buffer.
957 *
958 * This causes a swap between the snapshot buffer and the current live
959 * tracing buffer. You can use this to take snapshots of the live
960 * trace when some condition is triggered, but continue to trace.
961 *
962 * Note, make sure to allocate the snapshot with either
963 * a tracing_snapshot_alloc(), or by doing it manually
964 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
965 *
966 * If the snapshot buffer is not allocated, it will stop tracing.
967 * Basically making a permanent snapshot.
968 */
969void tracing_snapshot(void)
970{
971 struct trace_array *tr = &global_trace;
972
973 tracing_snapshot_instance(tr);
974}
975EXPORT_SYMBOL_GPL(tracing_snapshot);
976
David Brazdil0f672f62019-12-10 10:32:29 +0000977/**
978 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
979 * @tr: The tracing instance to snapshot
980 * @cond_data: The data to be tested conditionally, and possibly saved
981 *
982 * This is the same as tracing_snapshot() except that the snapshot is
983 * conditional - the snapshot will only happen if the
984 * cond_snapshot.update() implementation receiving the cond_data
985 * returns true, which means that the trace array's cond_snapshot
986 * update() operation used the cond_data to determine whether the
987 * snapshot should be taken, and if it was, presumably saved it along
988 * with the snapshot.
989 */
990void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
991{
992 tracing_snapshot_instance_cond(tr, cond_data);
993}
994EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
995
996/**
997 * tracing_snapshot_cond_data - get the user data associated with a snapshot
998 * @tr: The tracing instance
999 *
1000 * When the user enables a conditional snapshot using
1001 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1002 * with the snapshot. This accessor is used to retrieve it.
1003 *
1004 * Should not be called from cond_snapshot.update(), since it takes
1005 * the tr->max_lock lock, which the code calling
1006 * cond_snapshot.update() has already done.
1007 *
1008 * Returns the cond_data associated with the trace array's snapshot.
1009 */
1010void *tracing_cond_snapshot_data(struct trace_array *tr)
1011{
1012 void *cond_data = NULL;
1013
1014 arch_spin_lock(&tr->max_lock);
1015
1016 if (tr->cond_snapshot)
1017 cond_data = tr->cond_snapshot->cond_data;
1018
1019 arch_spin_unlock(&tr->max_lock);
1020
1021 return cond_data;
1022}
1023EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1024
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001025static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
1026 struct trace_buffer *size_buf, int cpu_id);
1027static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
1028
1029int tracing_alloc_snapshot_instance(struct trace_array *tr)
1030{
1031 int ret;
1032
1033 if (!tr->allocated_snapshot) {
1034
1035 /* allocate spare buffer */
1036 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1037 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
1038 if (ret < 0)
1039 return ret;
1040
1041 tr->allocated_snapshot = true;
1042 }
1043
1044 return 0;
1045}
1046
1047static void free_snapshot(struct trace_array *tr)
1048{
1049 /*
1050 * We don't free the ring buffer. instead, resize it because
1051 * The max_tr ring buffer has some state (e.g. ring->clock) and
1052 * we want preserve it.
1053 */
1054 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1055 set_buffer_entries(&tr->max_buffer, 1);
1056 tracing_reset_online_cpus(&tr->max_buffer);
1057 tr->allocated_snapshot = false;
1058}
1059
1060/**
1061 * tracing_alloc_snapshot - allocate snapshot buffer.
1062 *
1063 * This only allocates the snapshot buffer if it isn't already
1064 * allocated - it doesn't also take a snapshot.
1065 *
1066 * This is meant to be used in cases where the snapshot buffer needs
1067 * to be set up for events that can't sleep but need to be able to
1068 * trigger a snapshot.
1069 */
1070int tracing_alloc_snapshot(void)
1071{
1072 struct trace_array *tr = &global_trace;
1073 int ret;
1074
1075 ret = tracing_alloc_snapshot_instance(tr);
1076 WARN_ON(ret < 0);
1077
1078 return ret;
1079}
1080EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1081
1082/**
1083 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
1084 *
1085 * This is similar to tracing_snapshot(), but it will allocate the
1086 * snapshot buffer if it isn't already allocated. Use this only
1087 * where it is safe to sleep, as the allocation may sleep.
1088 *
1089 * This causes a swap between the snapshot buffer and the current live
1090 * tracing buffer. You can use this to take snapshots of the live
1091 * trace when some condition is triggered, but continue to trace.
1092 */
1093void tracing_snapshot_alloc(void)
1094{
1095 int ret;
1096
1097 ret = tracing_alloc_snapshot();
1098 if (ret < 0)
1099 return;
1100
1101 tracing_snapshot();
1102}
1103EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
David Brazdil0f672f62019-12-10 10:32:29 +00001104
1105/**
1106 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1107 * @tr: The tracing instance
1108 * @cond_data: User data to associate with the snapshot
1109 * @update: Implementation of the cond_snapshot update function
1110 *
1111 * Check whether the conditional snapshot for the given instance has
1112 * already been enabled, or if the current tracer is already using a
1113 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1114 * save the cond_data and update function inside.
1115 *
1116 * Returns 0 if successful, error otherwise.
1117 */
1118int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1119 cond_update_fn_t update)
1120{
1121 struct cond_snapshot *cond_snapshot;
1122 int ret = 0;
1123
1124 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1125 if (!cond_snapshot)
1126 return -ENOMEM;
1127
1128 cond_snapshot->cond_data = cond_data;
1129 cond_snapshot->update = update;
1130
1131 mutex_lock(&trace_types_lock);
1132
1133 ret = tracing_alloc_snapshot_instance(tr);
1134 if (ret)
1135 goto fail_unlock;
1136
1137 if (tr->current_trace->use_max_tr) {
1138 ret = -EBUSY;
1139 goto fail_unlock;
1140 }
1141
1142 /*
1143 * The cond_snapshot can only change to NULL without the
1144 * trace_types_lock. We don't care if we race with it going
1145 * to NULL, but we want to make sure that it's not set to
1146 * something other than NULL when we get here, which we can
1147 * do safely with only holding the trace_types_lock and not
1148 * having to take the max_lock.
1149 */
1150 if (tr->cond_snapshot) {
1151 ret = -EBUSY;
1152 goto fail_unlock;
1153 }
1154
1155 arch_spin_lock(&tr->max_lock);
1156 tr->cond_snapshot = cond_snapshot;
1157 arch_spin_unlock(&tr->max_lock);
1158
1159 mutex_unlock(&trace_types_lock);
1160
1161 return ret;
1162
1163 fail_unlock:
1164 mutex_unlock(&trace_types_lock);
1165 kfree(cond_snapshot);
1166 return ret;
1167}
1168EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1169
1170/**
1171 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1172 * @tr: The tracing instance
1173 *
1174 * Check whether the conditional snapshot for the given instance is
1175 * enabled; if so, free the cond_snapshot associated with it,
1176 * otherwise return -EINVAL.
1177 *
1178 * Returns 0 if successful, error otherwise.
1179 */
1180int tracing_snapshot_cond_disable(struct trace_array *tr)
1181{
1182 int ret = 0;
1183
1184 arch_spin_lock(&tr->max_lock);
1185
1186 if (!tr->cond_snapshot)
1187 ret = -EINVAL;
1188 else {
1189 kfree(tr->cond_snapshot);
1190 tr->cond_snapshot = NULL;
1191 }
1192
1193 arch_spin_unlock(&tr->max_lock);
1194
1195 return ret;
1196}
1197EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001198#else
1199void tracing_snapshot(void)
1200{
1201 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1202}
1203EXPORT_SYMBOL_GPL(tracing_snapshot);
David Brazdil0f672f62019-12-10 10:32:29 +00001204void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1205{
1206 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1207}
1208EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001209int tracing_alloc_snapshot(void)
1210{
1211 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1212 return -ENODEV;
1213}
1214EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1215void tracing_snapshot_alloc(void)
1216{
1217 /* Give warning */
1218 tracing_snapshot();
1219}
1220EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
David Brazdil0f672f62019-12-10 10:32:29 +00001221void *tracing_cond_snapshot_data(struct trace_array *tr)
1222{
1223 return NULL;
1224}
1225EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1226int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1227{
1228 return -ENODEV;
1229}
1230EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1231int tracing_snapshot_cond_disable(struct trace_array *tr)
1232{
1233 return false;
1234}
1235EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001236#endif /* CONFIG_TRACER_SNAPSHOT */
1237
1238void tracer_tracing_off(struct trace_array *tr)
1239{
1240 if (tr->trace_buffer.buffer)
1241 ring_buffer_record_off(tr->trace_buffer.buffer);
1242 /*
1243 * This flag is looked at when buffers haven't been allocated
1244 * yet, or by some tracers (like irqsoff), that just want to
1245 * know if the ring buffer has been disabled, but it can handle
1246 * races of where it gets disabled but we still do a record.
1247 * As the check is in the fast path of the tracers, it is more
1248 * important to be fast than accurate.
1249 */
1250 tr->buffer_disabled = 1;
1251 /* Make the flag seen by readers */
1252 smp_wmb();
1253}
1254
1255/**
1256 * tracing_off - turn off tracing buffers
1257 *
1258 * This function stops the tracing buffers from recording data.
1259 * It does not disable any overhead the tracers themselves may
1260 * be causing. This function simply causes all recording to
1261 * the ring buffers to fail.
1262 */
1263void tracing_off(void)
1264{
1265 tracer_tracing_off(&global_trace);
1266}
1267EXPORT_SYMBOL_GPL(tracing_off);
1268
1269void disable_trace_on_warning(void)
1270{
1271 if (__disable_trace_on_warning)
1272 tracing_off();
1273}
1274
1275/**
1276 * tracer_tracing_is_on - show real state of ring buffer enabled
1277 * @tr : the trace array to know if ring buffer is enabled
1278 *
1279 * Shows real state of the ring buffer if it is enabled or not.
1280 */
1281bool tracer_tracing_is_on(struct trace_array *tr)
1282{
1283 if (tr->trace_buffer.buffer)
1284 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1285 return !tr->buffer_disabled;
1286}
1287
1288/**
1289 * tracing_is_on - show state of ring buffers enabled
1290 */
1291int tracing_is_on(void)
1292{
1293 return tracer_tracing_is_on(&global_trace);
1294}
1295EXPORT_SYMBOL_GPL(tracing_is_on);
1296
1297static int __init set_buf_size(char *str)
1298{
1299 unsigned long buf_size;
1300
1301 if (!str)
1302 return 0;
1303 buf_size = memparse(str, &str);
1304 /* nr_entries can not be zero */
1305 if (buf_size == 0)
1306 return 0;
1307 trace_buf_size = buf_size;
1308 return 1;
1309}
1310__setup("trace_buf_size=", set_buf_size);
1311
1312static int __init set_tracing_thresh(char *str)
1313{
1314 unsigned long threshold;
1315 int ret;
1316
1317 if (!str)
1318 return 0;
1319 ret = kstrtoul(str, 0, &threshold);
1320 if (ret < 0)
1321 return 0;
1322 tracing_thresh = threshold * 1000;
1323 return 1;
1324}
1325__setup("tracing_thresh=", set_tracing_thresh);
1326
1327unsigned long nsecs_to_usecs(unsigned long nsecs)
1328{
1329 return nsecs / 1000;
1330}
1331
1332/*
1333 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1334 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1335 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1336 * of strings in the order that the evals (enum) were defined.
1337 */
1338#undef C
1339#define C(a, b) b
1340
1341/* These must match the bit postions in trace_iterator_flags */
1342static const char *trace_options[] = {
1343 TRACE_FLAGS
1344 NULL
1345};
1346
1347static struct {
1348 u64 (*func)(void);
1349 const char *name;
1350 int in_ns; /* is this clock in nanoseconds? */
1351} trace_clocks[] = {
1352 { trace_clock_local, "local", 1 },
1353 { trace_clock_global, "global", 1 },
1354 { trace_clock_counter, "counter", 0 },
1355 { trace_clock_jiffies, "uptime", 0 },
1356 { trace_clock, "perf", 1 },
1357 { ktime_get_mono_fast_ns, "mono", 1 },
1358 { ktime_get_raw_fast_ns, "mono_raw", 1 },
1359 { ktime_get_boot_fast_ns, "boot", 1 },
1360 ARCH_TRACE_CLOCKS
1361};
1362
1363bool trace_clock_in_ns(struct trace_array *tr)
1364{
1365 if (trace_clocks[tr->clock_id].in_ns)
1366 return true;
1367
1368 return false;
1369}
1370
1371/*
1372 * trace_parser_get_init - gets the buffer for trace parser
1373 */
1374int trace_parser_get_init(struct trace_parser *parser, int size)
1375{
1376 memset(parser, 0, sizeof(*parser));
1377
1378 parser->buffer = kmalloc(size, GFP_KERNEL);
1379 if (!parser->buffer)
1380 return 1;
1381
1382 parser->size = size;
1383 return 0;
1384}
1385
1386/*
1387 * trace_parser_put - frees the buffer for trace parser
1388 */
1389void trace_parser_put(struct trace_parser *parser)
1390{
1391 kfree(parser->buffer);
1392 parser->buffer = NULL;
1393}
1394
1395/*
1396 * trace_get_user - reads the user input string separated by space
1397 * (matched by isspace(ch))
1398 *
1399 * For each string found the 'struct trace_parser' is updated,
1400 * and the function returns.
1401 *
1402 * Returns number of bytes read.
1403 *
1404 * See kernel/trace/trace.h for 'struct trace_parser' details.
1405 */
1406int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1407 size_t cnt, loff_t *ppos)
1408{
1409 char ch;
1410 size_t read = 0;
1411 ssize_t ret;
1412
1413 if (!*ppos)
1414 trace_parser_clear(parser);
1415
1416 ret = get_user(ch, ubuf++);
1417 if (ret)
1418 goto out;
1419
1420 read++;
1421 cnt--;
1422
1423 /*
1424 * The parser is not finished with the last write,
1425 * continue reading the user input without skipping spaces.
1426 */
1427 if (!parser->cont) {
1428 /* skip white space */
1429 while (cnt && isspace(ch)) {
1430 ret = get_user(ch, ubuf++);
1431 if (ret)
1432 goto out;
1433 read++;
1434 cnt--;
1435 }
1436
1437 parser->idx = 0;
1438
1439 /* only spaces were written */
1440 if (isspace(ch) || !ch) {
1441 *ppos += read;
1442 ret = read;
1443 goto out;
1444 }
1445 }
1446
1447 /* read the non-space input */
1448 while (cnt && !isspace(ch) && ch) {
1449 if (parser->idx < parser->size - 1)
1450 parser->buffer[parser->idx++] = ch;
1451 else {
1452 ret = -EINVAL;
1453 goto out;
1454 }
1455 ret = get_user(ch, ubuf++);
1456 if (ret)
1457 goto out;
1458 read++;
1459 cnt--;
1460 }
1461
1462 /* We either got finished input or we have to wait for another call. */
1463 if (isspace(ch) || !ch) {
1464 parser->buffer[parser->idx] = 0;
1465 parser->cont = false;
1466 } else if (parser->idx < parser->size - 1) {
1467 parser->cont = true;
1468 parser->buffer[parser->idx++] = ch;
1469 /* Make sure the parsed string always terminates with '\0'. */
1470 parser->buffer[parser->idx] = 0;
1471 } else {
1472 ret = -EINVAL;
1473 goto out;
1474 }
1475
1476 *ppos += read;
1477 ret = read;
1478
1479out:
1480 return ret;
1481}
1482
1483/* TODO add a seq_buf_to_buffer() */
1484static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1485{
1486 int len;
1487
1488 if (trace_seq_used(s) <= s->seq.readpos)
1489 return -EBUSY;
1490
1491 len = trace_seq_used(s) - s->seq.readpos;
1492 if (cnt > len)
1493 cnt = len;
1494 memcpy(buf, s->buffer + s->seq.readpos, cnt);
1495
1496 s->seq.readpos += cnt;
1497 return cnt;
1498}
1499
1500unsigned long __read_mostly tracing_thresh;
1501
1502#ifdef CONFIG_TRACER_MAX_TRACE
1503/*
1504 * Copy the new maximum trace into the separate maximum-trace
1505 * structure. (this way the maximum trace is permanently saved,
1506 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
1507 */
1508static void
1509__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1510{
1511 struct trace_buffer *trace_buf = &tr->trace_buffer;
1512 struct trace_buffer *max_buf = &tr->max_buffer;
1513 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1514 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1515
1516 max_buf->cpu = cpu;
1517 max_buf->time_start = data->preempt_timestamp;
1518
1519 max_data->saved_latency = tr->max_latency;
1520 max_data->critical_start = data->critical_start;
1521 max_data->critical_end = data->critical_end;
1522
David Brazdil0f672f62019-12-10 10:32:29 +00001523 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001524 max_data->pid = tsk->pid;
1525 /*
1526 * If tsk == current, then use current_uid(), as that does not use
1527 * RCU. The irq tracer can be called out of RCU scope.
1528 */
1529 if (tsk == current)
1530 max_data->uid = current_uid();
1531 else
1532 max_data->uid = task_uid(tsk);
1533
1534 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1535 max_data->policy = tsk->policy;
1536 max_data->rt_priority = tsk->rt_priority;
1537
1538 /* record this tasks comm */
1539 tracing_record_cmdline(tsk);
1540}
1541
1542/**
1543 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1544 * @tr: tracer
1545 * @tsk: the task with the latency
1546 * @cpu: The cpu that initiated the trace.
David Brazdil0f672f62019-12-10 10:32:29 +00001547 * @cond_data: User data associated with a conditional snapshot
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001548 *
1549 * Flip the buffers between the @tr and the max_tr and record information
1550 * about which task was the cause of this latency.
1551 */
1552void
David Brazdil0f672f62019-12-10 10:32:29 +00001553update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1554 void *cond_data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555{
1556 if (tr->stop_count)
1557 return;
1558
1559 WARN_ON_ONCE(!irqs_disabled());
1560
1561 if (!tr->allocated_snapshot) {
1562 /* Only the nop tracer should hit this when disabling */
1563 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1564 return;
1565 }
1566
1567 arch_spin_lock(&tr->max_lock);
1568
1569 /* Inherit the recordable setting from trace_buffer */
1570 if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1571 ring_buffer_record_on(tr->max_buffer.buffer);
1572 else
1573 ring_buffer_record_off(tr->max_buffer.buffer);
1574
David Brazdil0f672f62019-12-10 10:32:29 +00001575#ifdef CONFIG_TRACER_SNAPSHOT
1576 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1577 goto out_unlock;
1578#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001579 swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
1580
1581 __update_max_tr(tr, tsk, cpu);
David Brazdil0f672f62019-12-10 10:32:29 +00001582
1583 out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001584 arch_spin_unlock(&tr->max_lock);
1585}
1586
1587/**
1588 * update_max_tr_single - only copy one trace over, and reset the rest
David Brazdil0f672f62019-12-10 10:32:29 +00001589 * @tr: tracer
1590 * @tsk: task with the latency
1591 * @cpu: the cpu of the buffer to copy.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592 *
1593 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1594 */
1595void
1596update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1597{
1598 int ret;
1599
1600 if (tr->stop_count)
1601 return;
1602
1603 WARN_ON_ONCE(!irqs_disabled());
1604 if (!tr->allocated_snapshot) {
1605 /* Only the nop tracer should hit this when disabling */
1606 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1607 return;
1608 }
1609
1610 arch_spin_lock(&tr->max_lock);
1611
1612 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1613
1614 if (ret == -EBUSY) {
1615 /*
1616 * We failed to swap the buffer due to a commit taking
1617 * place on this CPU. We fail to record, but we reset
1618 * the max trace buffer (no one writes directly to it)
1619 * and flag that it failed.
1620 */
1621 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1622 "Failed to swap buffers due to commit in progress\n");
1623 }
1624
1625 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1626
1627 __update_max_tr(tr, tsk, cpu);
1628 arch_spin_unlock(&tr->max_lock);
1629}
1630#endif /* CONFIG_TRACER_MAX_TRACE */
1631
David Brazdil0f672f62019-12-10 10:32:29 +00001632static int wait_on_pipe(struct trace_iterator *iter, int full)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001633{
1634 /* Iterators are static, they should be filled or empty */
1635 if (trace_buffer_iter(iter, iter->cpu_file))
1636 return 0;
1637
1638 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1639 full);
1640}
1641
1642#ifdef CONFIG_FTRACE_STARTUP_TEST
1643static bool selftests_can_run;
1644
1645struct trace_selftests {
1646 struct list_head list;
1647 struct tracer *type;
1648};
1649
1650static LIST_HEAD(postponed_selftests);
1651
1652static int save_selftest(struct tracer *type)
1653{
1654 struct trace_selftests *selftest;
1655
1656 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1657 if (!selftest)
1658 return -ENOMEM;
1659
1660 selftest->type = type;
1661 list_add(&selftest->list, &postponed_selftests);
1662 return 0;
1663}
1664
1665static int run_tracer_selftest(struct tracer *type)
1666{
1667 struct trace_array *tr = &global_trace;
1668 struct tracer *saved_tracer = tr->current_trace;
1669 int ret;
1670
1671 if (!type->selftest || tracing_selftest_disabled)
1672 return 0;
1673
1674 /*
1675 * If a tracer registers early in boot up (before scheduling is
1676 * initialized and such), then do not run its selftests yet.
1677 * Instead, run it a little later in the boot process.
1678 */
1679 if (!selftests_can_run)
1680 return save_selftest(type);
1681
1682 /*
1683 * Run a selftest on this tracer.
1684 * Here we reset the trace buffer, and set the current
1685 * tracer to be this tracer. The tracer can then run some
1686 * internal tracing to verify that everything is in order.
1687 * If we fail, we do not register this tracer.
1688 */
1689 tracing_reset_online_cpus(&tr->trace_buffer);
1690
1691 tr->current_trace = type;
1692
1693#ifdef CONFIG_TRACER_MAX_TRACE
1694 if (type->use_max_tr) {
1695 /* If we expanded the buffers, make sure the max is expanded too */
1696 if (ring_buffer_expanded)
1697 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1698 RING_BUFFER_ALL_CPUS);
1699 tr->allocated_snapshot = true;
1700 }
1701#endif
1702
1703 /* the test is responsible for initializing and enabling */
1704 pr_info("Testing tracer %s: ", type->name);
1705 ret = type->selftest(type, tr);
1706 /* the test is responsible for resetting too */
1707 tr->current_trace = saved_tracer;
1708 if (ret) {
1709 printk(KERN_CONT "FAILED!\n");
1710 /* Add the warning after printing 'FAILED' */
1711 WARN_ON(1);
1712 return -1;
1713 }
1714 /* Only reset on passing, to avoid touching corrupted buffers */
1715 tracing_reset_online_cpus(&tr->trace_buffer);
1716
1717#ifdef CONFIG_TRACER_MAX_TRACE
1718 if (type->use_max_tr) {
1719 tr->allocated_snapshot = false;
1720
1721 /* Shrink the max buffer again */
1722 if (ring_buffer_expanded)
1723 ring_buffer_resize(tr->max_buffer.buffer, 1,
1724 RING_BUFFER_ALL_CPUS);
1725 }
1726#endif
1727
1728 printk(KERN_CONT "PASSED\n");
1729 return 0;
1730}
1731
1732static __init int init_trace_selftests(void)
1733{
1734 struct trace_selftests *p, *n;
1735 struct tracer *t, **last;
1736 int ret;
1737
1738 selftests_can_run = true;
1739
1740 mutex_lock(&trace_types_lock);
1741
1742 if (list_empty(&postponed_selftests))
1743 goto out;
1744
1745 pr_info("Running postponed tracer tests:\n");
1746
Olivier Deprez0e641232021-09-23 10:07:05 +02001747 tracing_selftest_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001748 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
David Brazdil0f672f62019-12-10 10:32:29 +00001749 /* This loop can take minutes when sanitizers are enabled, so
1750 * lets make sure we allow RCU processing.
1751 */
1752 cond_resched();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001753 ret = run_tracer_selftest(p->type);
1754 /* If the test fails, then warn and remove from available_tracers */
1755 if (ret < 0) {
1756 WARN(1, "tracer: %s failed selftest, disabling\n",
1757 p->type->name);
1758 last = &trace_types;
1759 for (t = trace_types; t; t = t->next) {
1760 if (t == p->type) {
1761 *last = t->next;
1762 break;
1763 }
1764 last = &t->next;
1765 }
1766 }
1767 list_del(&p->list);
1768 kfree(p);
1769 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001770 tracing_selftest_running = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001771
1772 out:
1773 mutex_unlock(&trace_types_lock);
1774
1775 return 0;
1776}
1777core_initcall(init_trace_selftests);
1778#else
1779static inline int run_tracer_selftest(struct tracer *type)
1780{
1781 return 0;
1782}
1783#endif /* CONFIG_FTRACE_STARTUP_TEST */
1784
1785static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1786
1787static void __init apply_trace_boot_options(void);
1788
1789/**
1790 * register_tracer - register a tracer with the ftrace system.
David Brazdil0f672f62019-12-10 10:32:29 +00001791 * @type: the plugin for the tracer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001792 *
1793 * Register a new plugin tracer.
1794 */
1795int __init register_tracer(struct tracer *type)
1796{
1797 struct tracer *t;
1798 int ret = 0;
1799
1800 if (!type->name) {
1801 pr_info("Tracer must have a name\n");
1802 return -1;
1803 }
1804
1805 if (strlen(type->name) >= MAX_TRACER_SIZE) {
1806 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1807 return -1;
1808 }
1809
Olivier Deprez0e641232021-09-23 10:07:05 +02001810 if (security_locked_down(LOCKDOWN_TRACEFS)) {
1811 pr_warning("Can not register tracer %s due to lockdown\n",
1812 type->name);
1813 return -EPERM;
1814 }
1815
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001816 mutex_lock(&trace_types_lock);
1817
1818 tracing_selftest_running = true;
1819
1820 for (t = trace_types; t; t = t->next) {
1821 if (strcmp(type->name, t->name) == 0) {
1822 /* already found */
1823 pr_info("Tracer %s already registered\n",
1824 type->name);
1825 ret = -1;
1826 goto out;
1827 }
1828 }
1829
1830 if (!type->set_flag)
1831 type->set_flag = &dummy_set_flag;
1832 if (!type->flags) {
1833 /*allocate a dummy tracer_flags*/
1834 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
1835 if (!type->flags) {
1836 ret = -ENOMEM;
1837 goto out;
1838 }
1839 type->flags->val = 0;
1840 type->flags->opts = dummy_tracer_opt;
1841 } else
1842 if (!type->flags->opts)
1843 type->flags->opts = dummy_tracer_opt;
1844
1845 /* store the tracer for __set_tracer_option */
1846 type->flags->trace = type;
1847
1848 ret = run_tracer_selftest(type);
1849 if (ret < 0)
1850 goto out;
1851
1852 type->next = trace_types;
1853 trace_types = type;
1854 add_tracer_options(&global_trace, type);
1855
1856 out:
1857 tracing_selftest_running = false;
1858 mutex_unlock(&trace_types_lock);
1859
1860 if (ret || !default_bootup_tracer)
1861 goto out_unlock;
1862
1863 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1864 goto out_unlock;
1865
1866 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1867 /* Do we want this tracer to start on bootup? */
1868 tracing_set_tracer(&global_trace, type->name);
1869 default_bootup_tracer = NULL;
1870
1871 apply_trace_boot_options();
1872
1873 /* disable other selftests, since this will break it. */
1874 tracing_selftest_disabled = true;
1875#ifdef CONFIG_FTRACE_STARTUP_TEST
1876 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1877 type->name);
1878#endif
1879
1880 out_unlock:
1881 return ret;
1882}
1883
David Brazdil0f672f62019-12-10 10:32:29 +00001884static void tracing_reset_cpu(struct trace_buffer *buf, int cpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001885{
1886 struct ring_buffer *buffer = buf->buffer;
1887
1888 if (!buffer)
1889 return;
1890
1891 ring_buffer_record_disable(buffer);
1892
1893 /* Make sure all commits have finished */
David Brazdil0f672f62019-12-10 10:32:29 +00001894 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001895 ring_buffer_reset_cpu(buffer, cpu);
1896
1897 ring_buffer_record_enable(buffer);
1898}
1899
1900void tracing_reset_online_cpus(struct trace_buffer *buf)
1901{
1902 struct ring_buffer *buffer = buf->buffer;
1903 int cpu;
1904
1905 if (!buffer)
1906 return;
1907
1908 ring_buffer_record_disable(buffer);
1909
1910 /* Make sure all commits have finished */
David Brazdil0f672f62019-12-10 10:32:29 +00001911 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001912
1913 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1914
1915 for_each_online_cpu(cpu)
1916 ring_buffer_reset_cpu(buffer, cpu);
1917
1918 ring_buffer_record_enable(buffer);
1919}
1920
1921/* Must have trace_types_lock held */
1922void tracing_reset_all_online_cpus(void)
1923{
1924 struct trace_array *tr;
1925
1926 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1927 if (!tr->clear_trace)
1928 continue;
1929 tr->clear_trace = false;
1930 tracing_reset_online_cpus(&tr->trace_buffer);
1931#ifdef CONFIG_TRACER_MAX_TRACE
1932 tracing_reset_online_cpus(&tr->max_buffer);
1933#endif
1934 }
1935}
1936
Olivier Deprez0e641232021-09-23 10:07:05 +02001937/*
1938 * The tgid_map array maps from pid to tgid; i.e. the value stored at index i
1939 * is the tgid last observed corresponding to pid=i.
1940 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001941static int *tgid_map;
1942
Olivier Deprez0e641232021-09-23 10:07:05 +02001943/* The maximum valid index into tgid_map. */
1944static size_t tgid_map_max;
1945
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001946#define SAVED_CMDLINES_DEFAULT 128
1947#define NO_CMDLINE_MAP UINT_MAX
1948static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1949struct saved_cmdlines_buffer {
1950 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1951 unsigned *map_cmdline_to_pid;
1952 unsigned cmdline_num;
1953 int cmdline_idx;
1954 char *saved_cmdlines;
1955};
1956static struct saved_cmdlines_buffer *savedcmd;
1957
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001958static inline char *get_saved_cmdlines(int idx)
1959{
1960 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1961}
1962
1963static inline void set_cmdline(int idx, const char *cmdline)
1964{
David Brazdil0f672f62019-12-10 10:32:29 +00001965 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001966}
1967
1968static int allocate_cmdlines_buffer(unsigned int val,
1969 struct saved_cmdlines_buffer *s)
1970{
1971 s->map_cmdline_to_pid = kmalloc_array(val,
1972 sizeof(*s->map_cmdline_to_pid),
1973 GFP_KERNEL);
1974 if (!s->map_cmdline_to_pid)
1975 return -ENOMEM;
1976
1977 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
1978 if (!s->saved_cmdlines) {
1979 kfree(s->map_cmdline_to_pid);
1980 return -ENOMEM;
1981 }
1982
1983 s->cmdline_idx = 0;
1984 s->cmdline_num = val;
1985 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1986 sizeof(s->map_pid_to_cmdline));
1987 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1988 val * sizeof(*s->map_cmdline_to_pid));
1989
1990 return 0;
1991}
1992
1993static int trace_create_savedcmd(void)
1994{
1995 int ret;
1996
1997 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
1998 if (!savedcmd)
1999 return -ENOMEM;
2000
2001 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
2002 if (ret < 0) {
2003 kfree(savedcmd);
2004 savedcmd = NULL;
2005 return -ENOMEM;
2006 }
2007
2008 return 0;
2009}
2010
2011int is_tracing_stopped(void)
2012{
2013 return global_trace.stop_count;
2014}
2015
2016/**
2017 * tracing_start - quick start of the tracer
2018 *
2019 * If tracing is enabled but was stopped by tracing_stop,
2020 * this will start the tracer back up.
2021 */
2022void tracing_start(void)
2023{
2024 struct ring_buffer *buffer;
2025 unsigned long flags;
2026
2027 if (tracing_disabled)
2028 return;
2029
2030 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2031 if (--global_trace.stop_count) {
2032 if (global_trace.stop_count < 0) {
2033 /* Someone screwed up their debugging */
2034 WARN_ON_ONCE(1);
2035 global_trace.stop_count = 0;
2036 }
2037 goto out;
2038 }
2039
2040 /* Prevent the buffers from switching */
2041 arch_spin_lock(&global_trace.max_lock);
2042
2043 buffer = global_trace.trace_buffer.buffer;
2044 if (buffer)
2045 ring_buffer_record_enable(buffer);
2046
2047#ifdef CONFIG_TRACER_MAX_TRACE
2048 buffer = global_trace.max_buffer.buffer;
2049 if (buffer)
2050 ring_buffer_record_enable(buffer);
2051#endif
2052
2053 arch_spin_unlock(&global_trace.max_lock);
2054
2055 out:
2056 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2057}
2058
2059static void tracing_start_tr(struct trace_array *tr)
2060{
2061 struct ring_buffer *buffer;
2062 unsigned long flags;
2063
2064 if (tracing_disabled)
2065 return;
2066
2067 /* If global, we need to also start the max tracer */
2068 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2069 return tracing_start();
2070
2071 raw_spin_lock_irqsave(&tr->start_lock, flags);
2072
2073 if (--tr->stop_count) {
2074 if (tr->stop_count < 0) {
2075 /* Someone screwed up their debugging */
2076 WARN_ON_ONCE(1);
2077 tr->stop_count = 0;
2078 }
2079 goto out;
2080 }
2081
2082 buffer = tr->trace_buffer.buffer;
2083 if (buffer)
2084 ring_buffer_record_enable(buffer);
2085
2086 out:
2087 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2088}
2089
2090/**
2091 * tracing_stop - quick stop of the tracer
2092 *
2093 * Light weight way to stop tracing. Use in conjunction with
2094 * tracing_start.
2095 */
2096void tracing_stop(void)
2097{
2098 struct ring_buffer *buffer;
2099 unsigned long flags;
2100
2101 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2102 if (global_trace.stop_count++)
2103 goto out;
2104
2105 /* Prevent the buffers from switching */
2106 arch_spin_lock(&global_trace.max_lock);
2107
2108 buffer = global_trace.trace_buffer.buffer;
2109 if (buffer)
2110 ring_buffer_record_disable(buffer);
2111
2112#ifdef CONFIG_TRACER_MAX_TRACE
2113 buffer = global_trace.max_buffer.buffer;
2114 if (buffer)
2115 ring_buffer_record_disable(buffer);
2116#endif
2117
2118 arch_spin_unlock(&global_trace.max_lock);
2119
2120 out:
2121 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2122}
2123
2124static void tracing_stop_tr(struct trace_array *tr)
2125{
2126 struct ring_buffer *buffer;
2127 unsigned long flags;
2128
2129 /* If global, we need to also stop the max tracer */
2130 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2131 return tracing_stop();
2132
2133 raw_spin_lock_irqsave(&tr->start_lock, flags);
2134 if (tr->stop_count++)
2135 goto out;
2136
2137 buffer = tr->trace_buffer.buffer;
2138 if (buffer)
2139 ring_buffer_record_disable(buffer);
2140
2141 out:
2142 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2143}
2144
2145static int trace_save_cmdline(struct task_struct *tsk)
2146{
Olivier Deprez0e641232021-09-23 10:07:05 +02002147 unsigned tpid, idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002148
2149 /* treat recording of idle task as a success */
2150 if (!tsk->pid)
2151 return 1;
2152
Olivier Deprez0e641232021-09-23 10:07:05 +02002153 tpid = tsk->pid & (PID_MAX_DEFAULT - 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002154
2155 /*
2156 * It's not the end of the world if we don't get
2157 * the lock, but we also don't want to spin
2158 * nor do we want to disable interrupts,
2159 * so if we miss here, then better luck next time.
2160 */
2161 if (!arch_spin_trylock(&trace_cmdline_lock))
2162 return 0;
2163
Olivier Deprez0e641232021-09-23 10:07:05 +02002164 idx = savedcmd->map_pid_to_cmdline[tpid];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002165 if (idx == NO_CMDLINE_MAP) {
2166 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
2167
Olivier Deprez0e641232021-09-23 10:07:05 +02002168 savedcmd->map_pid_to_cmdline[tpid] = idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002169 savedcmd->cmdline_idx = idx;
2170 }
2171
Olivier Deprez0e641232021-09-23 10:07:05 +02002172 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002173 set_cmdline(idx, tsk->comm);
2174
2175 arch_spin_unlock(&trace_cmdline_lock);
2176
2177 return 1;
2178}
2179
2180static void __trace_find_cmdline(int pid, char comm[])
2181{
2182 unsigned map;
Olivier Deprez0e641232021-09-23 10:07:05 +02002183 int tpid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002184
2185 if (!pid) {
2186 strcpy(comm, "<idle>");
2187 return;
2188 }
2189
2190 if (WARN_ON_ONCE(pid < 0)) {
2191 strcpy(comm, "<XXX>");
2192 return;
2193 }
2194
Olivier Deprez0e641232021-09-23 10:07:05 +02002195 tpid = pid & (PID_MAX_DEFAULT - 1);
2196 map = savedcmd->map_pid_to_cmdline[tpid];
2197 if (map != NO_CMDLINE_MAP) {
2198 tpid = savedcmd->map_cmdline_to_pid[map];
2199 if (tpid == pid) {
2200 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
2201 return;
2202 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002203 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002204 strcpy(comm, "<...>");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002205}
2206
2207void trace_find_cmdline(int pid, char comm[])
2208{
2209 preempt_disable();
2210 arch_spin_lock(&trace_cmdline_lock);
2211
2212 __trace_find_cmdline(pid, comm);
2213
2214 arch_spin_unlock(&trace_cmdline_lock);
2215 preempt_enable();
2216}
2217
Olivier Deprez0e641232021-09-23 10:07:05 +02002218static int *trace_find_tgid_ptr(int pid)
2219{
2220 /*
2221 * Pairs with the smp_store_release in set_tracer_flag() to ensure that
2222 * if we observe a non-NULL tgid_map then we also observe the correct
2223 * tgid_map_max.
2224 */
2225 int *map = smp_load_acquire(&tgid_map);
2226
2227 if (unlikely(!map || pid > tgid_map_max))
2228 return NULL;
2229
2230 return &map[pid];
2231}
2232
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002233int trace_find_tgid(int pid)
2234{
Olivier Deprez0e641232021-09-23 10:07:05 +02002235 int *ptr = trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002236
Olivier Deprez0e641232021-09-23 10:07:05 +02002237 return ptr ? *ptr : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002238}
2239
2240static int trace_save_tgid(struct task_struct *tsk)
2241{
Olivier Deprez0e641232021-09-23 10:07:05 +02002242 int *ptr;
2243
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 /* treat recording of idle task as a success */
2245 if (!tsk->pid)
2246 return 1;
2247
Olivier Deprez0e641232021-09-23 10:07:05 +02002248 ptr = trace_find_tgid_ptr(tsk->pid);
2249 if (!ptr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002250 return 0;
2251
Olivier Deprez0e641232021-09-23 10:07:05 +02002252 *ptr = tsk->tgid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002253 return 1;
2254}
2255
2256static bool tracing_record_taskinfo_skip(int flags)
2257{
2258 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2259 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002260 if (!__this_cpu_read(trace_taskinfo_save))
2261 return true;
2262 return false;
2263}
2264
2265/**
2266 * tracing_record_taskinfo - record the task info of a task
2267 *
David Brazdil0f672f62019-12-10 10:32:29 +00002268 * @task: task to record
2269 * @flags: TRACE_RECORD_CMDLINE for recording comm
2270 * TRACE_RECORD_TGID for recording tgid
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002271 */
2272void tracing_record_taskinfo(struct task_struct *task, int flags)
2273{
2274 bool done;
2275
2276 if (tracing_record_taskinfo_skip(flags))
2277 return;
2278
2279 /*
2280 * Record as much task information as possible. If some fail, continue
2281 * to try to record the others.
2282 */
2283 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2284 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2285
2286 /* If recording any information failed, retry again soon. */
2287 if (!done)
2288 return;
2289
2290 __this_cpu_write(trace_taskinfo_save, false);
2291}
2292
2293/**
2294 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2295 *
David Brazdil0f672f62019-12-10 10:32:29 +00002296 * @prev: previous task during sched_switch
2297 * @next: next task during sched_switch
2298 * @flags: TRACE_RECORD_CMDLINE for recording comm
2299 * TRACE_RECORD_TGID for recording tgid
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002300 */
2301void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2302 struct task_struct *next, int flags)
2303{
2304 bool done;
2305
2306 if (tracing_record_taskinfo_skip(flags))
2307 return;
2308
2309 /*
2310 * Record as much task information as possible. If some fail, continue
2311 * to try to record the others.
2312 */
2313 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2314 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2315 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2316 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
2317
2318 /* If recording any information failed, retry again soon. */
2319 if (!done)
2320 return;
2321
2322 __this_cpu_write(trace_taskinfo_save, false);
2323}
2324
2325/* Helpers to record a specific task information */
2326void tracing_record_cmdline(struct task_struct *task)
2327{
2328 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2329}
2330
2331void tracing_record_tgid(struct task_struct *task)
2332{
2333 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
2334}
2335
2336/*
2337 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2338 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2339 * simplifies those functions and keeps them in sync.
2340 */
2341enum print_line_t trace_handle_return(struct trace_seq *s)
2342{
2343 return trace_seq_has_overflowed(s) ?
2344 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2345}
2346EXPORT_SYMBOL_GPL(trace_handle_return);
2347
2348void
David Brazdil0f672f62019-12-10 10:32:29 +00002349tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2350 unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002351{
2352 struct task_struct *tsk = current;
2353
2354 entry->preempt_count = pc & 0xff;
2355 entry->pid = (tsk) ? tsk->pid : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002356 entry->type = type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002357 entry->flags =
2358#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2359 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
2360#else
2361 TRACE_FLAG_IRQS_NOSUPPORT |
2362#endif
2363 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
2364 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
2365 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
2366 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2367 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
2368}
2369EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
2370
2371struct ring_buffer_event *
2372trace_buffer_lock_reserve(struct ring_buffer *buffer,
2373 int type,
2374 unsigned long len,
2375 unsigned long flags, int pc)
2376{
2377 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
2378}
2379
2380DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2381DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2382static int trace_buffered_event_ref;
2383
2384/**
2385 * trace_buffered_event_enable - enable buffering events
2386 *
2387 * When events are being filtered, it is quicker to use a temporary
2388 * buffer to write the event data into if there's a likely chance
2389 * that it will not be committed. The discard of the ring buffer
2390 * is not as fast as committing, and is much slower than copying
2391 * a commit.
2392 *
2393 * When an event is to be filtered, allocate per cpu buffers to
2394 * write the event data into, and if the event is filtered and discarded
2395 * it is simply dropped, otherwise, the entire data is to be committed
2396 * in one shot.
2397 */
2398void trace_buffered_event_enable(void)
2399{
2400 struct ring_buffer_event *event;
2401 struct page *page;
2402 int cpu;
2403
2404 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2405
2406 if (trace_buffered_event_ref++)
2407 return;
2408
2409 for_each_tracing_cpu(cpu) {
2410 page = alloc_pages_node(cpu_to_node(cpu),
2411 GFP_KERNEL | __GFP_NORETRY, 0);
2412 if (!page)
2413 goto failed;
2414
2415 event = page_address(page);
2416 memset(event, 0, sizeof(*event));
2417
2418 per_cpu(trace_buffered_event, cpu) = event;
2419
2420 preempt_disable();
2421 if (cpu == smp_processor_id() &&
2422 this_cpu_read(trace_buffered_event) !=
2423 per_cpu(trace_buffered_event, cpu))
2424 WARN_ON_ONCE(1);
2425 preempt_enable();
2426 }
2427
2428 return;
2429 failed:
2430 trace_buffered_event_disable();
2431}
2432
2433static void enable_trace_buffered_event(void *data)
2434{
2435 /* Probably not needed, but do it anyway */
2436 smp_rmb();
2437 this_cpu_dec(trace_buffered_event_cnt);
2438}
2439
2440static void disable_trace_buffered_event(void *data)
2441{
2442 this_cpu_inc(trace_buffered_event_cnt);
2443}
2444
2445/**
2446 * trace_buffered_event_disable - disable buffering events
2447 *
2448 * When a filter is removed, it is faster to not use the buffered
2449 * events, and to commit directly into the ring buffer. Free up
2450 * the temp buffers when there are no more users. This requires
2451 * special synchronization with current events.
2452 */
2453void trace_buffered_event_disable(void)
2454{
2455 int cpu;
2456
2457 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2458
2459 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2460 return;
2461
2462 if (--trace_buffered_event_ref)
2463 return;
2464
2465 preempt_disable();
2466 /* For each CPU, set the buffer as used. */
2467 smp_call_function_many(tracing_buffer_mask,
2468 disable_trace_buffered_event, NULL, 1);
2469 preempt_enable();
2470
2471 /* Wait for all current users to finish */
David Brazdil0f672f62019-12-10 10:32:29 +00002472 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002473
2474 for_each_tracing_cpu(cpu) {
2475 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2476 per_cpu(trace_buffered_event, cpu) = NULL;
2477 }
2478 /*
2479 * Make sure trace_buffered_event is NULL before clearing
2480 * trace_buffered_event_cnt.
2481 */
2482 smp_wmb();
2483
2484 preempt_disable();
2485 /* Do the work on each cpu */
2486 smp_call_function_many(tracing_buffer_mask,
2487 enable_trace_buffered_event, NULL, 1);
2488 preempt_enable();
2489}
2490
2491static struct ring_buffer *temp_buffer;
2492
2493struct ring_buffer_event *
2494trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
2495 struct trace_event_file *trace_file,
2496 int type, unsigned long len,
2497 unsigned long flags, int pc)
2498{
2499 struct ring_buffer_event *entry;
2500 int val;
2501
2502 *current_rb = trace_file->tr->trace_buffer.buffer;
2503
2504 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
2505 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2506 (entry = this_cpu_read(trace_buffered_event))) {
2507 /* Try to use the per cpu buffer first */
2508 val = this_cpu_inc_return(trace_buffered_event_cnt);
Olivier Deprez0e641232021-09-23 10:07:05 +02002509 if ((len < (PAGE_SIZE - sizeof(*entry) - sizeof(entry->array[0]))) && val == 1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002510 trace_event_setup(entry, type, flags, pc);
2511 entry->array[0] = len;
2512 return entry;
2513 }
2514 this_cpu_dec(trace_buffered_event_cnt);
2515 }
2516
2517 entry = __trace_buffer_lock_reserve(*current_rb,
2518 type, len, flags, pc);
2519 /*
2520 * If tracing is off, but we have triggers enabled
2521 * we still need to look at the event data. Use the temp_buffer
Olivier Deprez0e641232021-09-23 10:07:05 +02002522 * to store the trace event for the trigger to use. It's recursive
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002523 * safe and will not be recorded anywhere.
2524 */
2525 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2526 *current_rb = temp_buffer;
2527 entry = __trace_buffer_lock_reserve(*current_rb,
2528 type, len, flags, pc);
2529 }
2530 return entry;
2531}
2532EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2533
2534static DEFINE_SPINLOCK(tracepoint_iter_lock);
2535static DEFINE_MUTEX(tracepoint_printk_mutex);
2536
2537static void output_printk(struct trace_event_buffer *fbuffer)
2538{
2539 struct trace_event_call *event_call;
2540 struct trace_event *event;
2541 unsigned long flags;
2542 struct trace_iterator *iter = tracepoint_print_iter;
2543
2544 /* We should never get here if iter is NULL */
2545 if (WARN_ON_ONCE(!iter))
2546 return;
2547
2548 event_call = fbuffer->trace_file->event_call;
2549 if (!event_call || !event_call->event.funcs ||
2550 !event_call->event.funcs->trace)
2551 return;
2552
2553 event = &fbuffer->trace_file->event_call->event;
2554
2555 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2556 trace_seq_init(&iter->seq);
2557 iter->ent = fbuffer->entry;
2558 event_call->event.funcs->trace(iter, 0, event);
2559 trace_seq_putc(&iter->seq, 0);
2560 printk("%s", iter->seq.buffer);
2561
2562 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2563}
2564
2565int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2566 void __user *buffer, size_t *lenp,
2567 loff_t *ppos)
2568{
2569 int save_tracepoint_printk;
2570 int ret;
2571
2572 mutex_lock(&tracepoint_printk_mutex);
2573 save_tracepoint_printk = tracepoint_printk;
2574
2575 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2576
2577 /*
2578 * This will force exiting early, as tracepoint_printk
2579 * is always zero when tracepoint_printk_iter is not allocated
2580 */
2581 if (!tracepoint_print_iter)
2582 tracepoint_printk = 0;
2583
2584 if (save_tracepoint_printk == tracepoint_printk)
2585 goto out;
2586
2587 if (tracepoint_printk)
2588 static_key_enable(&tracepoint_printk_key.key);
2589 else
2590 static_key_disable(&tracepoint_printk_key.key);
2591
2592 out:
2593 mutex_unlock(&tracepoint_printk_mutex);
2594
2595 return ret;
2596}
2597
2598void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2599{
2600 if (static_key_false(&tracepoint_printk_key.key))
2601 output_printk(fbuffer);
2602
2603 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2604 fbuffer->event, fbuffer->entry,
2605 fbuffer->flags, fbuffer->pc);
2606}
2607EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2608
2609/*
2610 * Skip 3:
2611 *
2612 * trace_buffer_unlock_commit_regs()
2613 * trace_event_buffer_commit()
2614 * trace_event_raw_event_xxx()
2615 */
2616# define STACK_SKIP 3
2617
2618void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2619 struct ring_buffer *buffer,
2620 struct ring_buffer_event *event,
2621 unsigned long flags, int pc,
2622 struct pt_regs *regs)
2623{
2624 __buffer_unlock_commit(buffer, event);
2625
2626 /*
2627 * If regs is not set, then skip the necessary functions.
2628 * Note, we can still get here via blktrace, wakeup tracer
2629 * and mmiotrace, but that's ok if they lose a function or
2630 * two. They are not that meaningful.
2631 */
2632 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Olivier Deprez0e641232021-09-23 10:07:05 +02002633 ftrace_trace_userstack(tr, buffer, flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002634}
2635
2636/*
2637 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2638 */
2639void
2640trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2641 struct ring_buffer_event *event)
2642{
2643 __buffer_unlock_commit(buffer, event);
2644}
2645
2646static void
2647trace_process_export(struct trace_export *export,
2648 struct ring_buffer_event *event)
2649{
2650 struct trace_entry *entry;
2651 unsigned int size = 0;
2652
2653 entry = ring_buffer_event_data(event);
2654 size = ring_buffer_event_length(event);
2655 export->write(export, entry, size);
2656}
2657
2658static DEFINE_MUTEX(ftrace_export_lock);
2659
2660static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2661
2662static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2663
2664static inline void ftrace_exports_enable(void)
2665{
2666 static_branch_enable(&ftrace_exports_enabled);
2667}
2668
2669static inline void ftrace_exports_disable(void)
2670{
2671 static_branch_disable(&ftrace_exports_enabled);
2672}
2673
David Brazdil0f672f62019-12-10 10:32:29 +00002674static void ftrace_exports(struct ring_buffer_event *event)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002675{
2676 struct trace_export *export;
2677
2678 preempt_disable_notrace();
2679
David Brazdil0f672f62019-12-10 10:32:29 +00002680 export = rcu_dereference_raw_check(ftrace_exports_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002681 while (export) {
2682 trace_process_export(export, event);
David Brazdil0f672f62019-12-10 10:32:29 +00002683 export = rcu_dereference_raw_check(export->next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002684 }
2685
2686 preempt_enable_notrace();
2687}
2688
2689static inline void
2690add_trace_export(struct trace_export **list, struct trace_export *export)
2691{
2692 rcu_assign_pointer(export->next, *list);
2693 /*
2694 * We are entering export into the list but another
2695 * CPU might be walking that list. We need to make sure
2696 * the export->next pointer is valid before another CPU sees
2697 * the export pointer included into the list.
2698 */
2699 rcu_assign_pointer(*list, export);
2700}
2701
2702static inline int
2703rm_trace_export(struct trace_export **list, struct trace_export *export)
2704{
2705 struct trace_export **p;
2706
2707 for (p = list; *p != NULL; p = &(*p)->next)
2708 if (*p == export)
2709 break;
2710
2711 if (*p != export)
2712 return -1;
2713
2714 rcu_assign_pointer(*p, (*p)->next);
2715
2716 return 0;
2717}
2718
2719static inline void
2720add_ftrace_export(struct trace_export **list, struct trace_export *export)
2721{
2722 if (*list == NULL)
2723 ftrace_exports_enable();
2724
2725 add_trace_export(list, export);
2726}
2727
2728static inline int
2729rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2730{
2731 int ret;
2732
2733 ret = rm_trace_export(list, export);
2734 if (*list == NULL)
2735 ftrace_exports_disable();
2736
2737 return ret;
2738}
2739
2740int register_ftrace_export(struct trace_export *export)
2741{
2742 if (WARN_ON_ONCE(!export->write))
2743 return -1;
2744
2745 mutex_lock(&ftrace_export_lock);
2746
2747 add_ftrace_export(&ftrace_exports_list, export);
2748
2749 mutex_unlock(&ftrace_export_lock);
2750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(register_ftrace_export);
2754
2755int unregister_ftrace_export(struct trace_export *export)
2756{
2757 int ret;
2758
2759 mutex_lock(&ftrace_export_lock);
2760
2761 ret = rm_ftrace_export(&ftrace_exports_list, export);
2762
2763 mutex_unlock(&ftrace_export_lock);
2764
2765 return ret;
2766}
2767EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2768
2769void
2770trace_function(struct trace_array *tr,
2771 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2772 int pc)
2773{
2774 struct trace_event_call *call = &event_function;
2775 struct ring_buffer *buffer = tr->trace_buffer.buffer;
2776 struct ring_buffer_event *event;
2777 struct ftrace_entry *entry;
2778
2779 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2780 flags, pc);
2781 if (!event)
2782 return;
2783 entry = ring_buffer_event_data(event);
2784 entry->ip = ip;
2785 entry->parent_ip = parent_ip;
2786
2787 if (!call_filter_check_discard(call, entry, buffer, event)) {
2788 if (static_branch_unlikely(&ftrace_exports_enabled))
2789 ftrace_exports(event);
2790 __buffer_unlock_commit(buffer, event);
2791 }
2792}
2793
2794#ifdef CONFIG_STACKTRACE
2795
David Brazdil0f672f62019-12-10 10:32:29 +00002796/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2797#define FTRACE_KSTACK_NESTING 4
2798
2799#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002801struct ftrace_stack {
David Brazdil0f672f62019-12-10 10:32:29 +00002802 unsigned long calls[FTRACE_KSTACK_ENTRIES];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002803};
2804
David Brazdil0f672f62019-12-10 10:32:29 +00002805
2806struct ftrace_stacks {
2807 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2808};
2809
2810static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002811static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2812
2813static void __ftrace_trace_stack(struct ring_buffer *buffer,
2814 unsigned long flags,
2815 int skip, int pc, struct pt_regs *regs)
2816{
2817 struct trace_event_call *call = &event_kernel_stack;
2818 struct ring_buffer_event *event;
David Brazdil0f672f62019-12-10 10:32:29 +00002819 unsigned int size, nr_entries;
2820 struct ftrace_stack *fstack;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002821 struct stack_entry *entry;
David Brazdil0f672f62019-12-10 10:32:29 +00002822 int stackidx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002823
2824 /*
2825 * Add one, for this function and the call to save_stack_trace()
2826 * If regs is set, then these functions will not be in the way.
2827 */
2828#ifndef CONFIG_UNWINDER_ORC
2829 if (!regs)
David Brazdil0f672f62019-12-10 10:32:29 +00002830 skip++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002831#endif
2832
2833 /*
2834 * Since events can happen in NMIs there's no safe way to
2835 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2836 * or NMI comes in, it will just have to use the default
2837 * FTRACE_STACK_SIZE.
2838 */
2839 preempt_disable_notrace();
2840
David Brazdil0f672f62019-12-10 10:32:29 +00002841 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2842
2843 /* This should never happen. If it does, yell once and skip */
Olivier Deprez0e641232021-09-23 10:07:05 +02002844 if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
David Brazdil0f672f62019-12-10 10:32:29 +00002845 goto out;
2846
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002847 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002848 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2849 * interrupt will either see the value pre increment or post
2850 * increment. If the interrupt happens pre increment it will have
2851 * restored the counter when it returns. We just need a barrier to
2852 * keep gcc from moving things around.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002853 */
2854 barrier();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002855
David Brazdil0f672f62019-12-10 10:32:29 +00002856 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
2857 size = ARRAY_SIZE(fstack->calls);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002858
David Brazdil0f672f62019-12-10 10:32:29 +00002859 if (regs) {
2860 nr_entries = stack_trace_save_regs(regs, fstack->calls,
2861 size, skip);
2862 } else {
2863 nr_entries = stack_trace_save(fstack->calls, size, skip);
2864 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002865
David Brazdil0f672f62019-12-10 10:32:29 +00002866 size = nr_entries * sizeof(unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002867 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
Olivier Deprez0e641232021-09-23 10:07:05 +02002868 (sizeof(*entry) - sizeof(entry->caller)) + size,
2869 flags, pc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002870 if (!event)
2871 goto out;
2872 entry = ring_buffer_event_data(event);
2873
David Brazdil0f672f62019-12-10 10:32:29 +00002874 memcpy(&entry->caller, fstack->calls, size);
2875 entry->size = nr_entries;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002876
2877 if (!call_filter_check_discard(call, entry, buffer, event))
2878 __buffer_unlock_commit(buffer, event);
2879
2880 out:
2881 /* Again, don't let gcc optimize things here */
2882 barrier();
2883 __this_cpu_dec(ftrace_stack_reserve);
2884 preempt_enable_notrace();
2885
2886}
2887
2888static inline void ftrace_trace_stack(struct trace_array *tr,
2889 struct ring_buffer *buffer,
2890 unsigned long flags,
2891 int skip, int pc, struct pt_regs *regs)
2892{
2893 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
2894 return;
2895
2896 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
2897}
2898
2899void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2900 int pc)
2901{
2902 struct ring_buffer *buffer = tr->trace_buffer.buffer;
2903
2904 if (rcu_is_watching()) {
2905 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2906 return;
2907 }
2908
2909 /*
2910 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2911 * but if the above rcu_is_watching() failed, then the NMI
2912 * triggered someplace critical, and rcu_irq_enter() should
2913 * not be called from NMI.
2914 */
2915 if (unlikely(in_nmi()))
2916 return;
2917
2918 rcu_irq_enter_irqson();
2919 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2920 rcu_irq_exit_irqson();
2921}
2922
2923/**
2924 * trace_dump_stack - record a stack back trace in the trace buffer
2925 * @skip: Number of functions to skip (helper handlers)
2926 */
2927void trace_dump_stack(int skip)
2928{
2929 unsigned long flags;
2930
2931 if (tracing_disabled || tracing_selftest_running)
2932 return;
2933
2934 local_save_flags(flags);
2935
2936#ifndef CONFIG_UNWINDER_ORC
2937 /* Skip 1 to skip this function. */
2938 skip++;
2939#endif
2940 __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2941 flags, skip, preempt_count(), NULL);
2942}
David Brazdil0f672f62019-12-10 10:32:29 +00002943EXPORT_SYMBOL_GPL(trace_dump_stack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002944
David Brazdil0f672f62019-12-10 10:32:29 +00002945#ifdef CONFIG_USER_STACKTRACE_SUPPORT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002946static DEFINE_PER_CPU(int, user_stack_count);
2947
David Brazdil0f672f62019-12-10 10:32:29 +00002948static void
Olivier Deprez0e641232021-09-23 10:07:05 +02002949ftrace_trace_userstack(struct trace_array *tr,
2950 struct ring_buffer *buffer, unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002951{
2952 struct trace_event_call *call = &event_user_stack;
2953 struct ring_buffer_event *event;
2954 struct userstack_entry *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002955
Olivier Deprez0e641232021-09-23 10:07:05 +02002956 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002957 return;
2958
2959 /*
2960 * NMIs can not handle page faults, even with fix ups.
2961 * The save user stack can (and often does) fault.
2962 */
2963 if (unlikely(in_nmi()))
2964 return;
2965
2966 /*
2967 * prevent recursion, since the user stack tracing may
2968 * trigger other kernel events.
2969 */
2970 preempt_disable();
2971 if (__this_cpu_read(user_stack_count))
2972 goto out;
2973
2974 __this_cpu_inc(user_stack_count);
2975
2976 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2977 sizeof(*entry), flags, pc);
2978 if (!event)
2979 goto out_drop_count;
2980 entry = ring_buffer_event_data(event);
2981
2982 entry->tgid = current->tgid;
2983 memset(&entry->caller, 0, sizeof(entry->caller));
2984
David Brazdil0f672f62019-12-10 10:32:29 +00002985 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002986 if (!call_filter_check_discard(call, entry, buffer, event))
2987 __buffer_unlock_commit(buffer, event);
2988
2989 out_drop_count:
2990 __this_cpu_dec(user_stack_count);
2991 out:
2992 preempt_enable();
2993}
David Brazdil0f672f62019-12-10 10:32:29 +00002994#else /* CONFIG_USER_STACKTRACE_SUPPORT */
Olivier Deprez0e641232021-09-23 10:07:05 +02002995static void ftrace_trace_userstack(struct trace_array *tr,
2996 struct ring_buffer *buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00002997 unsigned long flags, int pc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002998{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002999}
David Brazdil0f672f62019-12-10 10:32:29 +00003000#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003001
3002#endif /* CONFIG_STACKTRACE */
3003
3004/* created for use with alloc_percpu */
3005struct trace_buffer_struct {
3006 int nesting;
3007 char buffer[4][TRACE_BUF_SIZE];
3008};
3009
3010static struct trace_buffer_struct *trace_percpu_buffer;
3011
3012/*
3013 * Thise allows for lockless recording. If we're nested too deeply, then
3014 * this returns NULL.
3015 */
3016static char *get_trace_buf(void)
3017{
3018 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
3019
3020 if (!buffer || buffer->nesting >= 4)
3021 return NULL;
3022
3023 buffer->nesting++;
3024
3025 /* Interrupts must see nesting incremented before we use the buffer */
3026 barrier();
Olivier Deprez0e641232021-09-23 10:07:05 +02003027 return &buffer->buffer[buffer->nesting - 1][0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003028}
3029
3030static void put_trace_buf(void)
3031{
3032 /* Don't let the decrement of nesting leak before this */
3033 barrier();
3034 this_cpu_dec(trace_percpu_buffer->nesting);
3035}
3036
3037static int alloc_percpu_trace_buffer(void)
3038{
3039 struct trace_buffer_struct *buffers;
3040
3041 buffers = alloc_percpu(struct trace_buffer_struct);
3042 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3043 return -ENOMEM;
3044
3045 trace_percpu_buffer = buffers;
3046 return 0;
3047}
3048
3049static int buffers_allocated;
3050
3051void trace_printk_init_buffers(void)
3052{
3053 if (buffers_allocated)
3054 return;
3055
3056 if (alloc_percpu_trace_buffer())
3057 return;
3058
3059 /* trace_printk() is for debug use only. Don't use it in production. */
3060
3061 pr_warn("\n");
3062 pr_warn("**********************************************************\n");
3063 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3064 pr_warn("** **\n");
3065 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3066 pr_warn("** **\n");
3067 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3068 pr_warn("** unsafe for production use. **\n");
3069 pr_warn("** **\n");
3070 pr_warn("** If you see this message and you are not debugging **\n");
3071 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3072 pr_warn("** **\n");
3073 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3074 pr_warn("**********************************************************\n");
3075
3076 /* Expand the buffers to set size */
3077 tracing_update_buffers();
3078
3079 buffers_allocated = 1;
3080
3081 /*
3082 * trace_printk_init_buffers() can be called by modules.
3083 * If that happens, then we need to start cmdline recording
3084 * directly here. If the global_trace.buffer is already
3085 * allocated here, then this was called by module code.
3086 */
3087 if (global_trace.trace_buffer.buffer)
3088 tracing_start_cmdline_record();
3089}
David Brazdil0f672f62019-12-10 10:32:29 +00003090EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003091
3092void trace_printk_start_comm(void)
3093{
3094 /* Start tracing comms if trace printk is set */
3095 if (!buffers_allocated)
3096 return;
3097 tracing_start_cmdline_record();
3098}
3099
3100static void trace_printk_start_stop_comm(int enabled)
3101{
3102 if (!buffers_allocated)
3103 return;
3104
3105 if (enabled)
3106 tracing_start_cmdline_record();
3107 else
3108 tracing_stop_cmdline_record();
3109}
3110
3111/**
3112 * trace_vbprintk - write binary msg to tracing buffer
David Brazdil0f672f62019-12-10 10:32:29 +00003113 * @ip: The address of the caller
3114 * @fmt: The string format to write to the buffer
3115 * @args: Arguments for @fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003116 */
3117int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
3118{
3119 struct trace_event_call *call = &event_bprint;
3120 struct ring_buffer_event *event;
3121 struct ring_buffer *buffer;
3122 struct trace_array *tr = &global_trace;
3123 struct bprint_entry *entry;
3124 unsigned long flags;
3125 char *tbuffer;
3126 int len = 0, size, pc;
3127
3128 if (unlikely(tracing_selftest_running || tracing_disabled))
3129 return 0;
3130
3131 /* Don't pollute graph traces with trace_vprintk internals */
3132 pause_graph_tracing();
3133
3134 pc = preempt_count();
3135 preempt_disable_notrace();
3136
3137 tbuffer = get_trace_buf();
3138 if (!tbuffer) {
3139 len = 0;
3140 goto out_nobuffer;
3141 }
3142
3143 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3144
3145 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
3146 goto out;
3147
3148 local_save_flags(flags);
3149 size = sizeof(*entry) + sizeof(u32) * len;
3150 buffer = tr->trace_buffer.buffer;
3151 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3152 flags, pc);
3153 if (!event)
3154 goto out;
3155 entry = ring_buffer_event_data(event);
3156 entry->ip = ip;
3157 entry->fmt = fmt;
3158
3159 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
3160 if (!call_filter_check_discard(call, entry, buffer, event)) {
3161 __buffer_unlock_commit(buffer, event);
3162 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
3163 }
3164
3165out:
3166 put_trace_buf();
3167
3168out_nobuffer:
3169 preempt_enable_notrace();
3170 unpause_graph_tracing();
3171
3172 return len;
3173}
3174EXPORT_SYMBOL_GPL(trace_vbprintk);
3175
3176__printf(3, 0)
3177static int
3178__trace_array_vprintk(struct ring_buffer *buffer,
3179 unsigned long ip, const char *fmt, va_list args)
3180{
3181 struct trace_event_call *call = &event_print;
3182 struct ring_buffer_event *event;
3183 int len = 0, size, pc;
3184 struct print_entry *entry;
3185 unsigned long flags;
3186 char *tbuffer;
3187
3188 if (tracing_disabled || tracing_selftest_running)
3189 return 0;
3190
3191 /* Don't pollute graph traces with trace_vprintk internals */
3192 pause_graph_tracing();
3193
3194 pc = preempt_count();
3195 preempt_disable_notrace();
3196
3197
3198 tbuffer = get_trace_buf();
3199 if (!tbuffer) {
3200 len = 0;
3201 goto out_nobuffer;
3202 }
3203
3204 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
3205
3206 local_save_flags(flags);
3207 size = sizeof(*entry) + len + 1;
3208 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3209 flags, pc);
3210 if (!event)
3211 goto out;
3212 entry = ring_buffer_event_data(event);
3213 entry->ip = ip;
3214
3215 memcpy(&entry->buf, tbuffer, len + 1);
3216 if (!call_filter_check_discard(call, entry, buffer, event)) {
3217 __buffer_unlock_commit(buffer, event);
3218 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
3219 }
3220
3221out:
3222 put_trace_buf();
3223
3224out_nobuffer:
3225 preempt_enable_notrace();
3226 unpause_graph_tracing();
3227
3228 return len;
3229}
3230
3231__printf(3, 0)
3232int trace_array_vprintk(struct trace_array *tr,
3233 unsigned long ip, const char *fmt, va_list args)
3234{
3235 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3236}
3237
3238__printf(3, 0)
3239int trace_array_printk(struct trace_array *tr,
3240 unsigned long ip, const char *fmt, ...)
3241{
3242 int ret;
3243 va_list ap;
3244
3245 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3246 return 0;
3247
Olivier Deprez0e641232021-09-23 10:07:05 +02003248 if (!tr)
3249 return -ENOENT;
3250
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003251 va_start(ap, fmt);
3252 ret = trace_array_vprintk(tr, ip, fmt, ap);
3253 va_end(ap);
3254 return ret;
3255}
David Brazdil0f672f62019-12-10 10:32:29 +00003256EXPORT_SYMBOL_GPL(trace_array_printk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003257
3258__printf(3, 4)
3259int trace_array_printk_buf(struct ring_buffer *buffer,
3260 unsigned long ip, const char *fmt, ...)
3261{
3262 int ret;
3263 va_list ap;
3264
3265 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3266 return 0;
3267
3268 va_start(ap, fmt);
3269 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3270 va_end(ap);
3271 return ret;
3272}
3273
3274__printf(2, 0)
3275int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3276{
3277 return trace_array_vprintk(&global_trace, ip, fmt, args);
3278}
3279EXPORT_SYMBOL_GPL(trace_vprintk);
3280
3281static void trace_iterator_increment(struct trace_iterator *iter)
3282{
3283 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3284
3285 iter->idx++;
3286 if (buf_iter)
3287 ring_buffer_read(buf_iter, NULL);
3288}
3289
3290static struct trace_entry *
3291peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3292 unsigned long *lost_events)
3293{
3294 struct ring_buffer_event *event;
3295 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3296
3297 if (buf_iter)
3298 event = ring_buffer_iter_peek(buf_iter, ts);
3299 else
3300 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
3301 lost_events);
3302
3303 if (event) {
3304 iter->ent_size = ring_buffer_event_length(event);
3305 return ring_buffer_event_data(event);
3306 }
3307 iter->ent_size = 0;
3308 return NULL;
3309}
3310
3311static struct trace_entry *
3312__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3313 unsigned long *missing_events, u64 *ent_ts)
3314{
3315 struct ring_buffer *buffer = iter->trace_buffer->buffer;
3316 struct trace_entry *ent, *next = NULL;
3317 unsigned long lost_events = 0, next_lost = 0;
3318 int cpu_file = iter->cpu_file;
3319 u64 next_ts = 0, ts;
3320 int next_cpu = -1;
3321 int next_size = 0;
3322 int cpu;
3323
3324 /*
3325 * If we are in a per_cpu trace file, don't bother by iterating over
3326 * all cpu and peek directly.
3327 */
3328 if (cpu_file > RING_BUFFER_ALL_CPUS) {
3329 if (ring_buffer_empty_cpu(buffer, cpu_file))
3330 return NULL;
3331 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3332 if (ent_cpu)
3333 *ent_cpu = cpu_file;
3334
3335 return ent;
3336 }
3337
3338 for_each_tracing_cpu(cpu) {
3339
3340 if (ring_buffer_empty_cpu(buffer, cpu))
3341 continue;
3342
3343 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3344
3345 /*
3346 * Pick the entry with the smallest timestamp:
3347 */
3348 if (ent && (!next || ts < next_ts)) {
3349 next = ent;
3350 next_cpu = cpu;
3351 next_ts = ts;
3352 next_lost = lost_events;
3353 next_size = iter->ent_size;
3354 }
3355 }
3356
3357 iter->ent_size = next_size;
3358
3359 if (ent_cpu)
3360 *ent_cpu = next_cpu;
3361
3362 if (ent_ts)
3363 *ent_ts = next_ts;
3364
3365 if (missing_events)
3366 *missing_events = next_lost;
3367
3368 return next;
3369}
3370
3371/* Find the next real entry, without updating the iterator itself */
3372struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3373 int *ent_cpu, u64 *ent_ts)
3374{
3375 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3376}
3377
3378/* Find the next real entry, and increment the iterator to the next entry */
3379void *trace_find_next_entry_inc(struct trace_iterator *iter)
3380{
3381 iter->ent = __find_next_entry(iter, &iter->cpu,
3382 &iter->lost_events, &iter->ts);
3383
3384 if (iter->ent)
3385 trace_iterator_increment(iter);
3386
3387 return iter->ent ? iter : NULL;
3388}
3389
3390static void trace_consume(struct trace_iterator *iter)
3391{
3392 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
3393 &iter->lost_events);
3394}
3395
3396static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3397{
3398 struct trace_iterator *iter = m->private;
3399 int i = (int)*pos;
3400 void *ent;
3401
3402 WARN_ON_ONCE(iter->leftover);
3403
3404 (*pos)++;
3405
3406 /* can't go backwards */
3407 if (iter->idx > i)
3408 return NULL;
3409
3410 if (iter->idx < 0)
3411 ent = trace_find_next_entry_inc(iter);
3412 else
3413 ent = iter;
3414
3415 while (ent && iter->idx < i)
3416 ent = trace_find_next_entry_inc(iter);
3417
3418 iter->pos = *pos;
3419
3420 return ent;
3421}
3422
3423void tracing_iter_reset(struct trace_iterator *iter, int cpu)
3424{
3425 struct ring_buffer_event *event;
3426 struct ring_buffer_iter *buf_iter;
3427 unsigned long entries = 0;
3428 u64 ts;
3429
3430 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
3431
3432 buf_iter = trace_buffer_iter(iter, cpu);
3433 if (!buf_iter)
3434 return;
3435
3436 ring_buffer_iter_reset(buf_iter);
3437
3438 /*
3439 * We could have the case with the max latency tracers
3440 * that a reset never took place on a cpu. This is evident
3441 * by the timestamp being before the start of the buffer.
3442 */
3443 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
3444 if (ts >= iter->trace_buffer->time_start)
3445 break;
3446 entries++;
3447 ring_buffer_read(buf_iter, NULL);
3448 }
3449
3450 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
3451}
3452
3453/*
3454 * The current tracer is copied to avoid a global locking
3455 * all around.
3456 */
3457static void *s_start(struct seq_file *m, loff_t *pos)
3458{
3459 struct trace_iterator *iter = m->private;
3460 struct trace_array *tr = iter->tr;
3461 int cpu_file = iter->cpu_file;
3462 void *p = NULL;
3463 loff_t l = 0;
3464 int cpu;
3465
3466 /*
3467 * copy the tracer to avoid using a global lock all around.
3468 * iter->trace is a copy of current_trace, the pointer to the
3469 * name may be used instead of a strcmp(), as iter->trace->name
3470 * will point to the same string as current_trace->name.
3471 */
3472 mutex_lock(&trace_types_lock);
3473 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3474 *iter->trace = *tr->current_trace;
3475 mutex_unlock(&trace_types_lock);
3476
3477#ifdef CONFIG_TRACER_MAX_TRACE
3478 if (iter->snapshot && iter->trace->use_max_tr)
3479 return ERR_PTR(-EBUSY);
3480#endif
3481
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003482 if (*pos != iter->pos) {
3483 iter->ent = NULL;
3484 iter->cpu = 0;
3485 iter->idx = -1;
3486
3487 if (cpu_file == RING_BUFFER_ALL_CPUS) {
3488 for_each_tracing_cpu(cpu)
3489 tracing_iter_reset(iter, cpu);
3490 } else
3491 tracing_iter_reset(iter, cpu_file);
3492
3493 iter->leftover = 0;
3494 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3495 ;
3496
3497 } else {
3498 /*
3499 * If we overflowed the seq_file before, then we want
3500 * to just reuse the trace_seq buffer again.
3501 */
3502 if (iter->leftover)
3503 p = iter;
3504 else {
3505 l = *pos - 1;
3506 p = s_next(m, p, &l);
3507 }
3508 }
3509
3510 trace_event_read_lock();
3511 trace_access_lock(cpu_file);
3512 return p;
3513}
3514
3515static void s_stop(struct seq_file *m, void *p)
3516{
3517 struct trace_iterator *iter = m->private;
3518
3519#ifdef CONFIG_TRACER_MAX_TRACE
3520 if (iter->snapshot && iter->trace->use_max_tr)
3521 return;
3522#endif
3523
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003524 trace_access_unlock(iter->cpu_file);
3525 trace_event_read_unlock();
3526}
3527
3528static void
David Brazdil0f672f62019-12-10 10:32:29 +00003529get_total_entries_cpu(struct trace_buffer *buf, unsigned long *total,
3530 unsigned long *entries, int cpu)
3531{
3532 unsigned long count;
3533
3534 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3535 /*
3536 * If this buffer has skipped entries, then we hold all
3537 * entries for the trace and we need to ignore the
3538 * ones before the time stamp.
3539 */
3540 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3541 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3542 /* total is the same as the entries */
3543 *total = count;
3544 } else
3545 *total = count +
3546 ring_buffer_overrun_cpu(buf->buffer, cpu);
3547 *entries = count;
3548}
3549
3550static void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003551get_total_entries(struct trace_buffer *buf,
3552 unsigned long *total, unsigned long *entries)
3553{
David Brazdil0f672f62019-12-10 10:32:29 +00003554 unsigned long t, e;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003555 int cpu;
3556
3557 *total = 0;
3558 *entries = 0;
3559
3560 for_each_tracing_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +00003561 get_total_entries_cpu(buf, &t, &e, cpu);
3562 *total += t;
3563 *entries += e;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003564 }
3565}
3566
David Brazdil0f672f62019-12-10 10:32:29 +00003567unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3568{
3569 unsigned long total, entries;
3570
3571 if (!tr)
3572 tr = &global_trace;
3573
3574 get_total_entries_cpu(&tr->trace_buffer, &total, &entries, cpu);
3575
3576 return entries;
3577}
3578
3579unsigned long trace_total_entries(struct trace_array *tr)
3580{
3581 unsigned long total, entries;
3582
3583 if (!tr)
3584 tr = &global_trace;
3585
3586 get_total_entries(&tr->trace_buffer, &total, &entries);
3587
3588 return entries;
3589}
3590
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003591static void print_lat_help_header(struct seq_file *m)
3592{
Olivier Deprez0e641232021-09-23 10:07:05 +02003593 seq_puts(m, "# _------=> CPU# \n"
3594 "# / _-----=> irqs-off \n"
3595 "# | / _----=> need-resched \n"
3596 "# || / _---=> hardirq/softirq \n"
3597 "# ||| / _--=> preempt-depth \n"
3598 "# |||| / delay \n"
3599 "# cmd pid ||||| time | caller \n"
3600 "# \\ / ||||| \\ | / \n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003601}
3602
3603static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
3604{
3605 unsigned long total;
3606 unsigned long entries;
3607
3608 get_total_entries(buf, &total, &entries);
3609 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3610 entries, total, num_online_cpus());
3611 seq_puts(m, "#\n");
3612}
3613
3614static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3615 unsigned int flags)
3616{
3617 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3618
3619 print_event_info(buf, m);
3620
Olivier Deprez0e641232021-09-23 10:07:05 +02003621 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
3622 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003623}
3624
3625static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3626 unsigned int flags)
3627{
3628 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Olivier Deprez0e641232021-09-23 10:07:05 +02003629 const char *space = " ";
3630 int prec = tgid ? 12 : 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003631
David Brazdil0f672f62019-12-10 10:32:29 +00003632 print_event_info(buf, m);
3633
Olivier Deprez0e641232021-09-23 10:07:05 +02003634 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3635 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3636 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3637 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3638 seq_printf(m, "# %.*s||| / delay\n", prec, space);
3639 seq_printf(m, "# TASK-PID %.*s CPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3640 seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003641}
3642
3643void
3644print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3645{
3646 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
3647 struct trace_buffer *buf = iter->trace_buffer;
3648 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
3649 struct tracer *type = iter->trace;
3650 unsigned long entries;
3651 unsigned long total;
3652 const char *name = "preemption";
3653
3654 name = type->name;
3655
3656 get_total_entries(buf, &total, &entries);
3657
3658 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
3659 name, UTS_RELEASE);
3660 seq_puts(m, "# -----------------------------------"
3661 "---------------------------------\n");
3662 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
3663 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
3664 nsecs_to_usecs(data->saved_latency),
3665 entries,
3666 total,
3667 buf->cpu,
3668#if defined(CONFIG_PREEMPT_NONE)
3669 "server",
3670#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3671 "desktop",
3672#elif defined(CONFIG_PREEMPT)
3673 "preempt",
3674#else
3675 "unknown",
3676#endif
3677 /* These are reserved for later use */
3678 0, 0, 0, 0);
3679#ifdef CONFIG_SMP
3680 seq_printf(m, " #P:%d)\n", num_online_cpus());
3681#else
3682 seq_puts(m, ")\n");
3683#endif
3684 seq_puts(m, "# -----------------\n");
3685 seq_printf(m, "# | task: %.16s-%d "
3686 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
3687 data->comm, data->pid,
3688 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
3689 data->policy, data->rt_priority);
3690 seq_puts(m, "# -----------------\n");
3691
3692 if (data->critical_start) {
3693 seq_puts(m, "# => started at: ");
3694 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3695 trace_print_seq(m, &iter->seq);
3696 seq_puts(m, "\n# => ended at: ");
3697 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3698 trace_print_seq(m, &iter->seq);
3699 seq_puts(m, "\n#\n");
3700 }
3701
3702 seq_puts(m, "#\n");
3703}
3704
3705static void test_cpu_buff_start(struct trace_iterator *iter)
3706{
3707 struct trace_seq *s = &iter->seq;
3708 struct trace_array *tr = iter->tr;
3709
3710 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
3711 return;
3712
3713 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3714 return;
3715
3716 if (cpumask_available(iter->started) &&
3717 cpumask_test_cpu(iter->cpu, iter->started))
3718 return;
3719
3720 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
3721 return;
3722
3723 if (cpumask_available(iter->started))
3724 cpumask_set_cpu(iter->cpu, iter->started);
3725
3726 /* Don't print started cpu buffer for the first entry of the trace */
3727 if (iter->idx > 1)
3728 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3729 iter->cpu);
3730}
3731
3732static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
3733{
3734 struct trace_array *tr = iter->tr;
3735 struct trace_seq *s = &iter->seq;
3736 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
3737 struct trace_entry *entry;
3738 struct trace_event *event;
3739
3740 entry = iter->ent;
3741
3742 test_cpu_buff_start(iter);
3743
3744 event = ftrace_find_event(entry->type);
3745
3746 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3747 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3748 trace_print_lat_context(iter);
3749 else
3750 trace_print_context(iter);
3751 }
3752
3753 if (trace_seq_has_overflowed(s))
3754 return TRACE_TYPE_PARTIAL_LINE;
3755
3756 if (event)
3757 return event->funcs->trace(iter, sym_flags, event);
3758
3759 trace_seq_printf(s, "Unknown type %d\n", entry->type);
3760
3761 return trace_handle_return(s);
3762}
3763
3764static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
3765{
3766 struct trace_array *tr = iter->tr;
3767 struct trace_seq *s = &iter->seq;
3768 struct trace_entry *entry;
3769 struct trace_event *event;
3770
3771 entry = iter->ent;
3772
3773 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
3774 trace_seq_printf(s, "%d %d %llu ",
3775 entry->pid, iter->cpu, iter->ts);
3776
3777 if (trace_seq_has_overflowed(s))
3778 return TRACE_TYPE_PARTIAL_LINE;
3779
3780 event = ftrace_find_event(entry->type);
3781 if (event)
3782 return event->funcs->raw(iter, 0, event);
3783
3784 trace_seq_printf(s, "%d ?\n", entry->type);
3785
3786 return trace_handle_return(s);
3787}
3788
3789static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
3790{
3791 struct trace_array *tr = iter->tr;
3792 struct trace_seq *s = &iter->seq;
3793 unsigned char newline = '\n';
3794 struct trace_entry *entry;
3795 struct trace_event *event;
3796
3797 entry = iter->ent;
3798
3799 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3800 SEQ_PUT_HEX_FIELD(s, entry->pid);
3801 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3802 SEQ_PUT_HEX_FIELD(s, iter->ts);
3803 if (trace_seq_has_overflowed(s))
3804 return TRACE_TYPE_PARTIAL_LINE;
3805 }
3806
3807 event = ftrace_find_event(entry->type);
3808 if (event) {
3809 enum print_line_t ret = event->funcs->hex(iter, 0, event);
3810 if (ret != TRACE_TYPE_HANDLED)
3811 return ret;
3812 }
3813
3814 SEQ_PUT_FIELD(s, newline);
3815
3816 return trace_handle_return(s);
3817}
3818
3819static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
3820{
3821 struct trace_array *tr = iter->tr;
3822 struct trace_seq *s = &iter->seq;
3823 struct trace_entry *entry;
3824 struct trace_event *event;
3825
3826 entry = iter->ent;
3827
3828 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3829 SEQ_PUT_FIELD(s, entry->pid);
3830 SEQ_PUT_FIELD(s, iter->cpu);
3831 SEQ_PUT_FIELD(s, iter->ts);
3832 if (trace_seq_has_overflowed(s))
3833 return TRACE_TYPE_PARTIAL_LINE;
3834 }
3835
3836 event = ftrace_find_event(entry->type);
3837 return event ? event->funcs->binary(iter, 0, event) :
3838 TRACE_TYPE_HANDLED;
3839}
3840
3841int trace_empty(struct trace_iterator *iter)
3842{
3843 struct ring_buffer_iter *buf_iter;
3844 int cpu;
3845
3846 /* If we are looking at one CPU buffer, only check that one */
3847 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
3848 cpu = iter->cpu_file;
3849 buf_iter = trace_buffer_iter(iter, cpu);
3850 if (buf_iter) {
3851 if (!ring_buffer_iter_empty(buf_iter))
3852 return 0;
3853 } else {
3854 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3855 return 0;
3856 }
3857 return 1;
3858 }
3859
3860 for_each_tracing_cpu(cpu) {
3861 buf_iter = trace_buffer_iter(iter, cpu);
3862 if (buf_iter) {
3863 if (!ring_buffer_iter_empty(buf_iter))
3864 return 0;
3865 } else {
3866 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3867 return 0;
3868 }
3869 }
3870
3871 return 1;
3872}
3873
3874/* Called with trace_event_read_lock() held. */
3875enum print_line_t print_trace_line(struct trace_iterator *iter)
3876{
3877 struct trace_array *tr = iter->tr;
3878 unsigned long trace_flags = tr->trace_flags;
3879 enum print_line_t ret;
3880
3881 if (iter->lost_events) {
3882 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3883 iter->cpu, iter->lost_events);
3884 if (trace_seq_has_overflowed(&iter->seq))
3885 return TRACE_TYPE_PARTIAL_LINE;
3886 }
3887
3888 if (iter->trace && iter->trace->print_line) {
3889 ret = iter->trace->print_line(iter);
3890 if (ret != TRACE_TYPE_UNHANDLED)
3891 return ret;
3892 }
3893
3894 if (iter->ent->type == TRACE_BPUTS &&
3895 trace_flags & TRACE_ITER_PRINTK &&
3896 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3897 return trace_print_bputs_msg_only(iter);
3898
3899 if (iter->ent->type == TRACE_BPRINT &&
3900 trace_flags & TRACE_ITER_PRINTK &&
3901 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3902 return trace_print_bprintk_msg_only(iter);
3903
3904 if (iter->ent->type == TRACE_PRINT &&
3905 trace_flags & TRACE_ITER_PRINTK &&
3906 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3907 return trace_print_printk_msg_only(iter);
3908
3909 if (trace_flags & TRACE_ITER_BIN)
3910 return print_bin_fmt(iter);
3911
3912 if (trace_flags & TRACE_ITER_HEX)
3913 return print_hex_fmt(iter);
3914
3915 if (trace_flags & TRACE_ITER_RAW)
3916 return print_raw_fmt(iter);
3917
3918 return print_trace_fmt(iter);
3919}
3920
3921void trace_latency_header(struct seq_file *m)
3922{
3923 struct trace_iterator *iter = m->private;
3924 struct trace_array *tr = iter->tr;
3925
3926 /* print nothing if the buffers are empty */
3927 if (trace_empty(iter))
3928 return;
3929
3930 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3931 print_trace_header(m, iter);
3932
3933 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
3934 print_lat_help_header(m);
3935}
3936
3937void trace_default_header(struct seq_file *m)
3938{
3939 struct trace_iterator *iter = m->private;
3940 struct trace_array *tr = iter->tr;
3941 unsigned long trace_flags = tr->trace_flags;
3942
3943 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
3944 return;
3945
3946 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
3947 /* print nothing if the buffers are empty */
3948 if (trace_empty(iter))
3949 return;
3950 print_trace_header(m, iter);
3951 if (!(trace_flags & TRACE_ITER_VERBOSE))
3952 print_lat_help_header(m);
3953 } else {
3954 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
3955 if (trace_flags & TRACE_ITER_IRQ_INFO)
3956 print_func_help_header_irq(iter->trace_buffer,
3957 m, trace_flags);
3958 else
3959 print_func_help_header(iter->trace_buffer, m,
3960 trace_flags);
3961 }
3962 }
3963}
3964
3965static void test_ftrace_alive(struct seq_file *m)
3966{
3967 if (!ftrace_is_dead())
3968 return;
3969 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
3970 "# MAY BE MISSING FUNCTION EVENTS\n");
3971}
3972
3973#ifdef CONFIG_TRACER_MAX_TRACE
3974static void show_snapshot_main_help(struct seq_file *m)
3975{
3976 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
3977 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3978 "# Takes a snapshot of the main buffer.\n"
3979 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
3980 "# (Doesn't have to be '2' works with any number that\n"
3981 "# is not a '0' or '1')\n");
3982}
3983
3984static void show_snapshot_percpu_help(struct seq_file *m)
3985{
3986 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
3987#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3988 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
3989 "# Takes a snapshot of the main buffer for this cpu.\n");
3990#else
3991 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
3992 "# Must use main snapshot file to allocate.\n");
3993#endif
3994 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
3995 "# (Doesn't have to be '2' works with any number that\n"
3996 "# is not a '0' or '1')\n");
3997}
3998
3999static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4000{
4001 if (iter->tr->allocated_snapshot)
4002 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
4003 else
4004 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
4005
4006 seq_puts(m, "# Snapshot commands:\n");
4007 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4008 show_snapshot_main_help(m);
4009 else
4010 show_snapshot_percpu_help(m);
4011}
4012#else
4013/* Should never be called */
4014static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4015#endif
4016
4017static int s_show(struct seq_file *m, void *v)
4018{
4019 struct trace_iterator *iter = v;
4020 int ret;
4021
4022 if (iter->ent == NULL) {
4023 if (iter->tr) {
4024 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4025 seq_puts(m, "#\n");
4026 test_ftrace_alive(m);
4027 }
4028 if (iter->snapshot && trace_empty(iter))
4029 print_snapshot_help(m, iter);
4030 else if (iter->trace && iter->trace->print_header)
4031 iter->trace->print_header(m);
4032 else
4033 trace_default_header(m);
4034
4035 } else if (iter->leftover) {
4036 /*
4037 * If we filled the seq_file buffer earlier, we
4038 * want to just show it now.
4039 */
4040 ret = trace_print_seq(m, &iter->seq);
4041
4042 /* ret should this time be zero, but you never know */
4043 iter->leftover = ret;
4044
4045 } else {
4046 print_trace_line(iter);
4047 ret = trace_print_seq(m, &iter->seq);
4048 /*
4049 * If we overflow the seq_file buffer, then it will
4050 * ask us for this data again at start up.
4051 * Use that instead.
4052 * ret is 0 if seq_file write succeeded.
4053 * -1 otherwise.
4054 */
4055 iter->leftover = ret;
4056 }
4057
4058 return 0;
4059}
4060
4061/*
4062 * Should be used after trace_array_get(), trace_types_lock
4063 * ensures that i_cdev was already initialized.
4064 */
4065static inline int tracing_get_cpu(struct inode *inode)
4066{
4067 if (inode->i_cdev) /* See trace_create_cpu_file() */
4068 return (long)inode->i_cdev - 1;
4069 return RING_BUFFER_ALL_CPUS;
4070}
4071
4072static const struct seq_operations tracer_seq_ops = {
4073 .start = s_start,
4074 .next = s_next,
4075 .stop = s_stop,
4076 .show = s_show,
4077};
4078
4079static struct trace_iterator *
4080__tracing_open(struct inode *inode, struct file *file, bool snapshot)
4081{
4082 struct trace_array *tr = inode->i_private;
4083 struct trace_iterator *iter;
4084 int cpu;
4085
4086 if (tracing_disabled)
4087 return ERR_PTR(-ENODEV);
4088
4089 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
4090 if (!iter)
4091 return ERR_PTR(-ENOMEM);
4092
4093 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
4094 GFP_KERNEL);
4095 if (!iter->buffer_iter)
4096 goto release;
4097
4098 /*
4099 * We make a copy of the current tracer to avoid concurrent
4100 * changes on it while we are reading.
4101 */
4102 mutex_lock(&trace_types_lock);
4103 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
4104 if (!iter->trace)
4105 goto fail;
4106
4107 *iter->trace = *tr->current_trace;
4108
4109 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
4110 goto fail;
4111
4112 iter->tr = tr;
4113
4114#ifdef CONFIG_TRACER_MAX_TRACE
4115 /* Currently only the top directory has a snapshot */
4116 if (tr->current_trace->print_max || snapshot)
4117 iter->trace_buffer = &tr->max_buffer;
4118 else
4119#endif
4120 iter->trace_buffer = &tr->trace_buffer;
4121 iter->snapshot = snapshot;
4122 iter->pos = -1;
4123 iter->cpu_file = tracing_get_cpu(inode);
4124 mutex_init(&iter->mutex);
4125
4126 /* Notify the tracer early; before we stop tracing. */
4127 if (iter->trace && iter->trace->open)
4128 iter->trace->open(iter);
4129
4130 /* Annotate start of buffers if we had overruns */
4131 if (ring_buffer_overruns(iter->trace_buffer->buffer))
4132 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4133
4134 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4135 if (trace_clocks[tr->clock_id].in_ns)
4136 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4137
4138 /* stop the trace while dumping if we are not opening "snapshot" */
4139 if (!iter->snapshot)
4140 tracing_stop_tr(tr);
4141
4142 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
4143 for_each_tracing_cpu(cpu) {
4144 iter->buffer_iter[cpu] =
David Brazdil0f672f62019-12-10 10:32:29 +00004145 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4146 cpu, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004147 }
4148 ring_buffer_read_prepare_sync();
4149 for_each_tracing_cpu(cpu) {
4150 ring_buffer_read_start(iter->buffer_iter[cpu]);
4151 tracing_iter_reset(iter, cpu);
4152 }
4153 } else {
4154 cpu = iter->cpu_file;
4155 iter->buffer_iter[cpu] =
David Brazdil0f672f62019-12-10 10:32:29 +00004156 ring_buffer_read_prepare(iter->trace_buffer->buffer,
4157 cpu, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004158 ring_buffer_read_prepare_sync();
4159 ring_buffer_read_start(iter->buffer_iter[cpu]);
4160 tracing_iter_reset(iter, cpu);
4161 }
4162
4163 mutex_unlock(&trace_types_lock);
4164
4165 return iter;
4166
4167 fail:
4168 mutex_unlock(&trace_types_lock);
4169 kfree(iter->trace);
4170 kfree(iter->buffer_iter);
4171release:
4172 seq_release_private(inode, file);
4173 return ERR_PTR(-ENOMEM);
4174}
4175
4176int tracing_open_generic(struct inode *inode, struct file *filp)
4177{
David Brazdil0f672f62019-12-10 10:32:29 +00004178 int ret;
4179
4180 ret = tracing_check_open_get_tr(NULL);
4181 if (ret)
4182 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004183
4184 filp->private_data = inode->i_private;
4185 return 0;
4186}
4187
4188bool tracing_is_disabled(void)
4189{
4190 return (tracing_disabled) ? true: false;
4191}
4192
4193/*
4194 * Open and update trace_array ref count.
4195 * Must have the current trace_array passed to it.
4196 */
David Brazdil0f672f62019-12-10 10:32:29 +00004197int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004198{
4199 struct trace_array *tr = inode->i_private;
David Brazdil0f672f62019-12-10 10:32:29 +00004200 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004201
David Brazdil0f672f62019-12-10 10:32:29 +00004202 ret = tracing_check_open_get_tr(tr);
4203 if (ret)
4204 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004205
4206 filp->private_data = inode->i_private;
4207
4208 return 0;
4209}
4210
4211static int tracing_release(struct inode *inode, struct file *file)
4212{
4213 struct trace_array *tr = inode->i_private;
4214 struct seq_file *m = file->private_data;
4215 struct trace_iterator *iter;
4216 int cpu;
4217
4218 if (!(file->f_mode & FMODE_READ)) {
4219 trace_array_put(tr);
4220 return 0;
4221 }
4222
4223 /* Writes do not use seq_file */
4224 iter = m->private;
4225 mutex_lock(&trace_types_lock);
4226
4227 for_each_tracing_cpu(cpu) {
4228 if (iter->buffer_iter[cpu])
4229 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4230 }
4231
4232 if (iter->trace && iter->trace->close)
4233 iter->trace->close(iter);
4234
4235 if (!iter->snapshot)
4236 /* reenable tracing if it was previously enabled */
4237 tracing_start_tr(tr);
4238
4239 __trace_array_put(tr);
4240
4241 mutex_unlock(&trace_types_lock);
4242
4243 mutex_destroy(&iter->mutex);
4244 free_cpumask_var(iter->started);
4245 kfree(iter->trace);
4246 kfree(iter->buffer_iter);
4247 seq_release_private(inode, file);
4248
4249 return 0;
4250}
4251
4252static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4253{
4254 struct trace_array *tr = inode->i_private;
4255
4256 trace_array_put(tr);
4257 return 0;
4258}
4259
4260static int tracing_single_release_tr(struct inode *inode, struct file *file)
4261{
4262 struct trace_array *tr = inode->i_private;
4263
4264 trace_array_put(tr);
4265
4266 return single_release(inode, file);
4267}
4268
4269static int tracing_open(struct inode *inode, struct file *file)
4270{
4271 struct trace_array *tr = inode->i_private;
4272 struct trace_iterator *iter;
David Brazdil0f672f62019-12-10 10:32:29 +00004273 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004274
David Brazdil0f672f62019-12-10 10:32:29 +00004275 ret = tracing_check_open_get_tr(tr);
4276 if (ret)
4277 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004278
4279 /* If this file was open for write, then erase contents */
4280 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4281 int cpu = tracing_get_cpu(inode);
4282 struct trace_buffer *trace_buf = &tr->trace_buffer;
4283
4284#ifdef CONFIG_TRACER_MAX_TRACE
4285 if (tr->current_trace->print_max)
4286 trace_buf = &tr->max_buffer;
4287#endif
4288
4289 if (cpu == RING_BUFFER_ALL_CPUS)
4290 tracing_reset_online_cpus(trace_buf);
4291 else
David Brazdil0f672f62019-12-10 10:32:29 +00004292 tracing_reset_cpu(trace_buf, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004293 }
4294
4295 if (file->f_mode & FMODE_READ) {
4296 iter = __tracing_open(inode, file, false);
4297 if (IS_ERR(iter))
4298 ret = PTR_ERR(iter);
4299 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4300 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4301 }
4302
4303 if (ret < 0)
4304 trace_array_put(tr);
4305
4306 return ret;
4307}
4308
4309/*
4310 * Some tracers are not suitable for instance buffers.
4311 * A tracer is always available for the global array (toplevel)
4312 * or if it explicitly states that it is.
4313 */
4314static bool
4315trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4316{
4317 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4318}
4319
4320/* Find the next tracer that this trace array may use */
4321static struct tracer *
4322get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4323{
4324 while (t && !trace_ok_for_array(t, tr))
4325 t = t->next;
4326
4327 return t;
4328}
4329
4330static void *
4331t_next(struct seq_file *m, void *v, loff_t *pos)
4332{
4333 struct trace_array *tr = m->private;
4334 struct tracer *t = v;
4335
4336 (*pos)++;
4337
4338 if (t)
4339 t = get_tracer_for_array(tr, t->next);
4340
4341 return t;
4342}
4343
4344static void *t_start(struct seq_file *m, loff_t *pos)
4345{
4346 struct trace_array *tr = m->private;
4347 struct tracer *t;
4348 loff_t l = 0;
4349
4350 mutex_lock(&trace_types_lock);
4351
4352 t = get_tracer_for_array(tr, trace_types);
4353 for (; t && l < *pos; t = t_next(m, t, &l))
4354 ;
4355
4356 return t;
4357}
4358
4359static void t_stop(struct seq_file *m, void *p)
4360{
4361 mutex_unlock(&trace_types_lock);
4362}
4363
4364static int t_show(struct seq_file *m, void *v)
4365{
4366 struct tracer *t = v;
4367
4368 if (!t)
4369 return 0;
4370
4371 seq_puts(m, t->name);
4372 if (t->next)
4373 seq_putc(m, ' ');
4374 else
4375 seq_putc(m, '\n');
4376
4377 return 0;
4378}
4379
4380static const struct seq_operations show_traces_seq_ops = {
4381 .start = t_start,
4382 .next = t_next,
4383 .stop = t_stop,
4384 .show = t_show,
4385};
4386
4387static int show_traces_open(struct inode *inode, struct file *file)
4388{
4389 struct trace_array *tr = inode->i_private;
4390 struct seq_file *m;
4391 int ret;
4392
David Brazdil0f672f62019-12-10 10:32:29 +00004393 ret = tracing_check_open_get_tr(tr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004394 if (ret)
4395 return ret;
4396
David Brazdil0f672f62019-12-10 10:32:29 +00004397 ret = seq_open(file, &show_traces_seq_ops);
4398 if (ret) {
4399 trace_array_put(tr);
4400 return ret;
4401 }
4402
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004403 m = file->private_data;
4404 m->private = tr;
4405
4406 return 0;
4407}
4408
David Brazdil0f672f62019-12-10 10:32:29 +00004409static int show_traces_release(struct inode *inode, struct file *file)
4410{
4411 struct trace_array *tr = inode->i_private;
4412
4413 trace_array_put(tr);
4414 return seq_release(inode, file);
4415}
4416
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004417static ssize_t
4418tracing_write_stub(struct file *filp, const char __user *ubuf,
4419 size_t count, loff_t *ppos)
4420{
4421 return count;
4422}
4423
4424loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
4425{
4426 int ret;
4427
4428 if (file->f_mode & FMODE_READ)
4429 ret = seq_lseek(file, offset, whence);
4430 else
4431 file->f_pos = ret = 0;
4432
4433 return ret;
4434}
4435
4436static const struct file_operations tracing_fops = {
4437 .open = tracing_open,
4438 .read = seq_read,
4439 .write = tracing_write_stub,
4440 .llseek = tracing_lseek,
4441 .release = tracing_release,
4442};
4443
4444static const struct file_operations show_traces_fops = {
4445 .open = show_traces_open,
4446 .read = seq_read,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004447 .llseek = seq_lseek,
David Brazdil0f672f62019-12-10 10:32:29 +00004448 .release = show_traces_release,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004449};
4450
4451static ssize_t
4452tracing_cpumask_read(struct file *filp, char __user *ubuf,
4453 size_t count, loff_t *ppos)
4454{
4455 struct trace_array *tr = file_inode(filp)->i_private;
4456 char *mask_str;
4457 int len;
4458
4459 len = snprintf(NULL, 0, "%*pb\n",
4460 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4461 mask_str = kmalloc(len, GFP_KERNEL);
4462 if (!mask_str)
4463 return -ENOMEM;
4464
4465 len = snprintf(mask_str, len, "%*pb\n",
4466 cpumask_pr_args(tr->tracing_cpumask));
4467 if (len >= count) {
4468 count = -EINVAL;
4469 goto out_err;
4470 }
4471 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
4472
4473out_err:
4474 kfree(mask_str);
4475
4476 return count;
4477}
4478
4479static ssize_t
4480tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4481 size_t count, loff_t *ppos)
4482{
4483 struct trace_array *tr = file_inode(filp)->i_private;
4484 cpumask_var_t tracing_cpumask_new;
4485 int err, cpu;
4486
4487 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4488 return -ENOMEM;
4489
4490 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4491 if (err)
4492 goto err_unlock;
4493
4494 local_irq_disable();
4495 arch_spin_lock(&tr->max_lock);
4496 for_each_tracing_cpu(cpu) {
4497 /*
4498 * Increase/decrease the disabled counter if we are
4499 * about to flip a bit in the cpumask:
4500 */
4501 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
4502 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4503 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4504 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
4505 }
4506 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
4507 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4508 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4509 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
4510 }
4511 }
4512 arch_spin_unlock(&tr->max_lock);
4513 local_irq_enable();
4514
4515 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
4516 free_cpumask_var(tracing_cpumask_new);
4517
4518 return count;
4519
4520err_unlock:
4521 free_cpumask_var(tracing_cpumask_new);
4522
4523 return err;
4524}
4525
4526static const struct file_operations tracing_cpumask_fops = {
4527 .open = tracing_open_generic_tr,
4528 .read = tracing_cpumask_read,
4529 .write = tracing_cpumask_write,
4530 .release = tracing_release_generic_tr,
4531 .llseek = generic_file_llseek,
4532};
4533
4534static int tracing_trace_options_show(struct seq_file *m, void *v)
4535{
4536 struct tracer_opt *trace_opts;
4537 struct trace_array *tr = m->private;
4538 u32 tracer_flags;
4539 int i;
4540
4541 mutex_lock(&trace_types_lock);
4542 tracer_flags = tr->current_trace->flags->val;
4543 trace_opts = tr->current_trace->flags->opts;
4544
4545 for (i = 0; trace_options[i]; i++) {
4546 if (tr->trace_flags & (1 << i))
4547 seq_printf(m, "%s\n", trace_options[i]);
4548 else
4549 seq_printf(m, "no%s\n", trace_options[i]);
4550 }
4551
4552 for (i = 0; trace_opts[i].name; i++) {
4553 if (tracer_flags & trace_opts[i].bit)
4554 seq_printf(m, "%s\n", trace_opts[i].name);
4555 else
4556 seq_printf(m, "no%s\n", trace_opts[i].name);
4557 }
4558 mutex_unlock(&trace_types_lock);
4559
4560 return 0;
4561}
4562
4563static int __set_tracer_option(struct trace_array *tr,
4564 struct tracer_flags *tracer_flags,
4565 struct tracer_opt *opts, int neg)
4566{
4567 struct tracer *trace = tracer_flags->trace;
4568 int ret;
4569
4570 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
4571 if (ret)
4572 return ret;
4573
4574 if (neg)
4575 tracer_flags->val &= ~opts->bit;
4576 else
4577 tracer_flags->val |= opts->bit;
4578 return 0;
4579}
4580
4581/* Try to assign a tracer specific option */
4582static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
4583{
4584 struct tracer *trace = tr->current_trace;
4585 struct tracer_flags *tracer_flags = trace->flags;
4586 struct tracer_opt *opts = NULL;
4587 int i;
4588
4589 for (i = 0; tracer_flags->opts[i].name; i++) {
4590 opts = &tracer_flags->opts[i];
4591
4592 if (strcmp(cmp, opts->name) == 0)
4593 return __set_tracer_option(tr, trace->flags, opts, neg);
4594 }
4595
4596 return -EINVAL;
4597}
4598
4599/* Some tracers require overwrite to stay enabled */
4600int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4601{
4602 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4603 return -1;
4604
4605 return 0;
4606}
4607
4608int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
4609{
Olivier Deprez0e641232021-09-23 10:07:05 +02004610 int *map;
4611
4612 if ((mask == TRACE_ITER_RECORD_TGID) ||
4613 (mask == TRACE_ITER_RECORD_CMD))
4614 lockdep_assert_held(&event_mutex);
4615
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004616 /* do nothing if flag is already set */
4617 if (!!(tr->trace_flags & mask) == !!enabled)
4618 return 0;
4619
4620 /* Give the tracer a chance to approve the change */
4621 if (tr->current_trace->flag_changed)
4622 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
4623 return -EINVAL;
4624
4625 if (enabled)
4626 tr->trace_flags |= mask;
4627 else
4628 tr->trace_flags &= ~mask;
4629
4630 if (mask == TRACE_ITER_RECORD_CMD)
4631 trace_event_enable_cmd_record(enabled);
4632
4633 if (mask == TRACE_ITER_RECORD_TGID) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004634 if (!tgid_map) {
4635 tgid_map_max = pid_max;
4636 map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
4637 GFP_KERNEL);
4638
4639 /*
4640 * Pairs with smp_load_acquire() in
4641 * trace_find_tgid_ptr() to ensure that if it observes
4642 * the tgid_map we just allocated then it also observes
4643 * the corresponding tgid_map_max value.
4644 */
4645 smp_store_release(&tgid_map, map);
4646 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004647 if (!tgid_map) {
4648 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4649 return -ENOMEM;
4650 }
4651
4652 trace_event_enable_tgid_record(enabled);
4653 }
4654
4655 if (mask == TRACE_ITER_EVENT_FORK)
4656 trace_event_follow_fork(tr, enabled);
4657
4658 if (mask == TRACE_ITER_FUNC_FORK)
4659 ftrace_pid_follow_fork(tr, enabled);
4660
4661 if (mask == TRACE_ITER_OVERWRITE) {
4662 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
4663#ifdef CONFIG_TRACER_MAX_TRACE
4664 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
4665#endif
4666 }
4667
4668 if (mask == TRACE_ITER_PRINTK) {
4669 trace_printk_start_stop_comm(enabled);
4670 trace_printk_control(enabled);
4671 }
4672
4673 return 0;
4674}
4675
4676static int trace_set_options(struct trace_array *tr, char *option)
4677{
4678 char *cmp;
4679 int neg = 0;
4680 int ret;
4681 size_t orig_len = strlen(option);
David Brazdil0f672f62019-12-10 10:32:29 +00004682 int len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004683
4684 cmp = strstrip(option);
4685
David Brazdil0f672f62019-12-10 10:32:29 +00004686 len = str_has_prefix(cmp, "no");
4687 if (len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004688 neg = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00004689
4690 cmp += len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004691
Olivier Deprez0e641232021-09-23 10:07:05 +02004692 mutex_lock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004693 mutex_lock(&trace_types_lock);
4694
4695 ret = match_string(trace_options, -1, cmp);
4696 /* If no option could be set, test the specific tracer options */
4697 if (ret < 0)
4698 ret = set_tracer_option(tr, cmp, neg);
4699 else
4700 ret = set_tracer_flag(tr, 1 << ret, !neg);
4701
4702 mutex_unlock(&trace_types_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02004703 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004704
4705 /*
4706 * If the first trailing whitespace is replaced with '\0' by strstrip,
4707 * turn it back into a space.
4708 */
4709 if (orig_len > strlen(option))
4710 option[strlen(option)] = ' ';
4711
4712 return ret;
4713}
4714
4715static void __init apply_trace_boot_options(void)
4716{
4717 char *buf = trace_boot_options_buf;
4718 char *option;
4719
4720 while (true) {
4721 option = strsep(&buf, ",");
4722
4723 if (!option)
4724 break;
4725
4726 if (*option)
4727 trace_set_options(&global_trace, option);
4728
4729 /* Put back the comma to allow this to be called again */
4730 if (buf)
4731 *(buf - 1) = ',';
4732 }
4733}
4734
4735static ssize_t
4736tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4737 size_t cnt, loff_t *ppos)
4738{
4739 struct seq_file *m = filp->private_data;
4740 struct trace_array *tr = m->private;
4741 char buf[64];
4742 int ret;
4743
4744 if (cnt >= sizeof(buf))
4745 return -EINVAL;
4746
4747 if (copy_from_user(buf, ubuf, cnt))
4748 return -EFAULT;
4749
4750 buf[cnt] = 0;
4751
4752 ret = trace_set_options(tr, buf);
4753 if (ret < 0)
4754 return ret;
4755
4756 *ppos += cnt;
4757
4758 return cnt;
4759}
4760
4761static int tracing_trace_options_open(struct inode *inode, struct file *file)
4762{
4763 struct trace_array *tr = inode->i_private;
4764 int ret;
4765
David Brazdil0f672f62019-12-10 10:32:29 +00004766 ret = tracing_check_open_get_tr(tr);
4767 if (ret)
4768 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004769
4770 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4771 if (ret < 0)
4772 trace_array_put(tr);
4773
4774 return ret;
4775}
4776
4777static const struct file_operations tracing_iter_fops = {
4778 .open = tracing_trace_options_open,
4779 .read = seq_read,
4780 .llseek = seq_lseek,
4781 .release = tracing_single_release_tr,
4782 .write = tracing_trace_options_write,
4783};
4784
4785static const char readme_msg[] =
4786 "tracing mini-HOWTO:\n\n"
4787 "# echo 0 > tracing_on : quick way to disable tracing\n"
4788 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4789 " Important files:\n"
4790 " trace\t\t\t- The static contents of the buffer\n"
4791 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4792 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4793 " current_tracer\t- function and latency tracers\n"
4794 " available_tracers\t- list of configured tracers for current_tracer\n"
David Brazdil0f672f62019-12-10 10:32:29 +00004795 " error_log\t- error log for failed commands (that support it)\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004796 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4797 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4798 " trace_clock\t\t-change the clock used to order events\n"
4799 " local: Per cpu clock but may not be synced across CPUs\n"
4800 " global: Synced across CPUs but slows tracing down.\n"
4801 " counter: Not a clock, but just an increment\n"
4802 " uptime: Jiffy counter from time of boot\n"
4803 " perf: Same clock that perf events use\n"
4804#ifdef CONFIG_X86_64
4805 " x86-tsc: TSC cycle counter\n"
4806#endif
4807 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4808 " delta: Delta difference against a buffer-wide timestamp\n"
4809 " absolute: Absolute (standalone) timestamp\n"
4810 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
4811 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
4812 " tracing_cpumask\t- Limit which CPUs to trace\n"
4813 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4814 "\t\t\t Remove sub-buffer with rmdir\n"
4815 " trace_options\t\t- Set format or modify how tracing happens\n"
David Brazdil0f672f62019-12-10 10:32:29 +00004816 "\t\t\t Disable an option by prefixing 'no' to the\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004817 "\t\t\t option name\n"
4818 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
4819#ifdef CONFIG_DYNAMIC_FTRACE
4820 "\n available_filter_functions - list of functions that can be filtered on\n"
4821 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4822 "\t\t\t functions\n"
4823 "\t accepts: func_full_name or glob-matching-pattern\n"
4824 "\t modules: Can select a group via module\n"
4825 "\t Format: :mod:<module-name>\n"
4826 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4827 "\t triggers: a command to perform when function is hit\n"
4828 "\t Format: <function>:<trigger>[:count]\n"
4829 "\t trigger: traceon, traceoff\n"
4830 "\t\t enable_event:<system>:<event>\n"
4831 "\t\t disable_event:<system>:<event>\n"
4832#ifdef CONFIG_STACKTRACE
4833 "\t\t stacktrace\n"
4834#endif
4835#ifdef CONFIG_TRACER_SNAPSHOT
4836 "\t\t snapshot\n"
4837#endif
4838 "\t\t dump\n"
4839 "\t\t cpudump\n"
4840 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4841 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4842 "\t The first one will disable tracing every time do_fault is hit\n"
4843 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4844 "\t The first time do trap is hit and it disables tracing, the\n"
4845 "\t counter will decrement to 2. If tracing is already disabled,\n"
4846 "\t the counter will not decrement. It only decrements when the\n"
4847 "\t trigger did work\n"
4848 "\t To remove trigger without count:\n"
4849 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4850 "\t To remove trigger with a count:\n"
4851 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
4852 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
4853 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4854 "\t modules: Can select a group via module command :mod:\n"
4855 "\t Does not accept triggers\n"
4856#endif /* CONFIG_DYNAMIC_FTRACE */
4857#ifdef CONFIG_FUNCTION_TRACER
4858 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4859 "\t\t (function)\n"
4860#endif
4861#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4862 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
4863 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
4864 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4865#endif
4866#ifdef CONFIG_TRACER_SNAPSHOT
4867 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4868 "\t\t\t snapshot buffer. Read the contents for more\n"
4869 "\t\t\t information\n"
4870#endif
4871#ifdef CONFIG_STACK_TRACER
4872 " stack_trace\t\t- Shows the max stack trace when active\n"
4873 " stack_max_size\t- Shows current max stack size that was traced\n"
4874 "\t\t\t Write into this file to reset the max size (trigger a\n"
4875 "\t\t\t new trace)\n"
4876#ifdef CONFIG_DYNAMIC_FTRACE
4877 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4878 "\t\t\t traces\n"
4879#endif
4880#endif /* CONFIG_STACK_TRACER */
David Brazdil0f672f62019-12-10 10:32:29 +00004881#ifdef CONFIG_DYNAMIC_EVENTS
4882 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
4883 "\t\t\t Write into this file to define/undefine new trace events.\n"
4884#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004885#ifdef CONFIG_KPROBE_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00004886 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004887 "\t\t\t Write into this file to define/undefine new trace events.\n"
4888#endif
4889#ifdef CONFIG_UPROBE_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00004890 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004891 "\t\t\t Write into this file to define/undefine new trace events.\n"
4892#endif
4893#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
4894 "\t accepts: event-definitions (one definition per line)\n"
4895 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
4896 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
David Brazdil0f672f62019-12-10 10:32:29 +00004897#ifdef CONFIG_HIST_TRIGGERS
4898 "\t s:[synthetic/]<event> <field> [<field>]\n"
4899#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004900 "\t -:[<group>/]<event>\n"
4901#ifdef CONFIG_KPROBE_EVENTS
4902 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4903 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4904#endif
4905#ifdef CONFIG_UPROBE_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00004906 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004907#endif
4908 "\t args: <name>=fetcharg[:type]\n"
4909 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
David Brazdil0f672f62019-12-10 10:32:29 +00004910#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
4911 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
4912#else
4913 "\t $stack<index>, $stack, $retval, $comm,\n"
4914#endif
4915 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
4916 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
4917 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
4918 "\t <type>\\[<array-size>\\]\n"
4919#ifdef CONFIG_HIST_TRIGGERS
4920 "\t field: <stype> <name>;\n"
4921 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
4922 "\t [unsigned] char/int/long\n"
4923#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004924#endif
4925 " events/\t\t- Directory containing all trace event subsystems:\n"
4926 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
4927 " events/<system>/\t- Directory containing all trace events for <system>:\n"
4928 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
4929 "\t\t\t events\n"
4930 " filter\t\t- If set, only events passing filter are traced\n"
4931 " events/<system>/<event>/\t- Directory containing control files for\n"
4932 "\t\t\t <event>:\n"
4933 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
4934 " filter\t\t- If set, only events passing filter are traced\n"
4935 " trigger\t\t- If set, a command to perform when event is hit\n"
4936 "\t Format: <trigger>[:count][if <filter>]\n"
4937 "\t trigger: traceon, traceoff\n"
4938 "\t enable_event:<system>:<event>\n"
4939 "\t disable_event:<system>:<event>\n"
4940#ifdef CONFIG_HIST_TRIGGERS
4941 "\t enable_hist:<system>:<event>\n"
4942 "\t disable_hist:<system>:<event>\n"
4943#endif
4944#ifdef CONFIG_STACKTRACE
4945 "\t\t stacktrace\n"
4946#endif
4947#ifdef CONFIG_TRACER_SNAPSHOT
4948 "\t\t snapshot\n"
4949#endif
4950#ifdef CONFIG_HIST_TRIGGERS
4951 "\t\t hist (see below)\n"
4952#endif
4953 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
4954 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
4955 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
4956 "\t events/block/block_unplug/trigger\n"
4957 "\t The first disables tracing every time block_unplug is hit.\n"
4958 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
4959 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
4960 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
4961 "\t Like function triggers, the counter is only decremented if it\n"
4962 "\t enabled or disabled tracing.\n"
4963 "\t To remove a trigger without a count:\n"
4964 "\t echo '!<trigger> > <system>/<event>/trigger\n"
4965 "\t To remove a trigger with a count:\n"
4966 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
4967 "\t Filters can be ignored when removing a trigger.\n"
4968#ifdef CONFIG_HIST_TRIGGERS
4969 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
4970 "\t Format: hist:keys=<field1[,field2,...]>\n"
4971 "\t [:values=<field1[,field2,...]>]\n"
4972 "\t [:sort=<field1[,field2,...]>]\n"
4973 "\t [:size=#entries]\n"
4974 "\t [:pause][:continue][:clear]\n"
4975 "\t [:name=histname1]\n"
David Brazdil0f672f62019-12-10 10:32:29 +00004976 "\t [:<handler>.<action>]\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004977 "\t [if <filter>]\n\n"
Olivier Deprez0e641232021-09-23 10:07:05 +02004978 "\t Note, special fields can be used as well:\n"
4979 "\t common_timestamp - to record current timestamp\n"
4980 "\t common_cpu - to record the CPU the event happened on\n"
4981 "\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004982 "\t When a matching event is hit, an entry is added to a hash\n"
4983 "\t table using the key(s) and value(s) named, and the value of a\n"
4984 "\t sum called 'hitcount' is incremented. Keys and values\n"
4985 "\t correspond to fields in the event's format description. Keys\n"
4986 "\t can be any field, or the special string 'stacktrace'.\n"
4987 "\t Compound keys consisting of up to two fields can be specified\n"
4988 "\t by the 'keys' keyword. Values must correspond to numeric\n"
4989 "\t fields. Sort keys consisting of up to two fields can be\n"
4990 "\t specified using the 'sort' keyword. The sort direction can\n"
4991 "\t be modified by appending '.descending' or '.ascending' to a\n"
4992 "\t sort field. The 'size' parameter can be used to specify more\n"
4993 "\t or fewer than the default 2048 entries for the hashtable size.\n"
4994 "\t If a hist trigger is given a name using the 'name' parameter,\n"
4995 "\t its histogram data will be shared with other triggers of the\n"
4996 "\t same name, and trigger hits will update this common data.\n\n"
4997 "\t Reading the 'hist' file for the event will dump the hash\n"
4998 "\t table in its entirety to stdout. If there are multiple hist\n"
4999 "\t triggers attached to an event, there will be a table for each\n"
5000 "\t trigger in the output. The table displayed for a named\n"
5001 "\t trigger will be the same as any other instance having the\n"
5002 "\t same name. The default format used to display a given field\n"
5003 "\t can be modified by appending any of the following modifiers\n"
5004 "\t to the field name, as applicable:\n\n"
5005 "\t .hex display a number as a hex value\n"
5006 "\t .sym display an address as a symbol\n"
5007 "\t .sym-offset display an address as a symbol and offset\n"
5008 "\t .execname display a common_pid as a program name\n"
5009 "\t .syscall display a syscall id as a syscall name\n"
5010 "\t .log2 display log2 value rather than raw number\n"
5011 "\t .usecs display a common_timestamp in microseconds\n\n"
5012 "\t The 'pause' parameter can be used to pause an existing hist\n"
5013 "\t trigger or to start a hist trigger but not log any events\n"
5014 "\t until told to do so. 'continue' can be used to start or\n"
5015 "\t restart a paused hist trigger.\n\n"
5016 "\t The 'clear' parameter will clear the contents of a running\n"
5017 "\t hist trigger and leave its current paused/active state\n"
5018 "\t unchanged.\n\n"
5019 "\t The enable_hist and disable_hist triggers can be used to\n"
5020 "\t have one event conditionally start and stop another event's\n"
David Brazdil0f672f62019-12-10 10:32:29 +00005021 "\t already-attached hist trigger. The syntax is analogous to\n"
5022 "\t the enable_event and disable_event triggers.\n\n"
5023 "\t Hist trigger handlers and actions are executed whenever a\n"
5024 "\t a histogram entry is added or updated. They take the form:\n\n"
5025 "\t <handler>.<action>\n\n"
5026 "\t The available handlers are:\n\n"
5027 "\t onmatch(matching.event) - invoke on addition or update\n"
5028 "\t onmax(var) - invoke if var exceeds current max\n"
5029 "\t onchange(var) - invoke action if var changes\n\n"
5030 "\t The available actions are:\n\n"
5031 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5032 "\t save(field,...) - save current event fields\n"
5033#ifdef CONFIG_TRACER_SNAPSHOT
5034 "\t snapshot() - snapshot the trace buffer\n"
5035#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005036#endif
5037;
5038
5039static ssize_t
5040tracing_readme_read(struct file *filp, char __user *ubuf,
5041 size_t cnt, loff_t *ppos)
5042{
5043 return simple_read_from_buffer(ubuf, cnt, ppos,
5044 readme_msg, strlen(readme_msg));
5045}
5046
5047static const struct file_operations tracing_readme_fops = {
5048 .open = tracing_open_generic,
5049 .read = tracing_readme_read,
5050 .llseek = generic_file_llseek,
5051};
5052
5053static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
5054{
Olivier Deprez0e641232021-09-23 10:07:05 +02005055 int pid = ++(*pos);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005056
Olivier Deprez0e641232021-09-23 10:07:05 +02005057 return trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005058}
5059
5060static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
5061{
Olivier Deprez0e641232021-09-23 10:07:05 +02005062 int pid = *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005063
Olivier Deprez0e641232021-09-23 10:07:05 +02005064 return trace_find_tgid_ptr(pid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005065}
5066
5067static void saved_tgids_stop(struct seq_file *m, void *v)
5068{
5069}
5070
5071static int saved_tgids_show(struct seq_file *m, void *v)
5072{
Olivier Deprez0e641232021-09-23 10:07:05 +02005073 int *entry = (int *)v;
5074 int pid = entry - tgid_map;
5075 int tgid = *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005076
Olivier Deprez0e641232021-09-23 10:07:05 +02005077 if (tgid == 0)
5078 return SEQ_SKIP;
5079
5080 seq_printf(m, "%d %d\n", pid, tgid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005081 return 0;
5082}
5083
5084static const struct seq_operations tracing_saved_tgids_seq_ops = {
5085 .start = saved_tgids_start,
5086 .stop = saved_tgids_stop,
5087 .next = saved_tgids_next,
5088 .show = saved_tgids_show,
5089};
5090
5091static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5092{
David Brazdil0f672f62019-12-10 10:32:29 +00005093 int ret;
5094
5095 ret = tracing_check_open_get_tr(NULL);
5096 if (ret)
5097 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005098
5099 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5100}
5101
5102
5103static const struct file_operations tracing_saved_tgids_fops = {
5104 .open = tracing_saved_tgids_open,
5105 .read = seq_read,
5106 .llseek = seq_lseek,
5107 .release = seq_release,
5108};
5109
5110static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
5111{
5112 unsigned int *ptr = v;
5113
5114 if (*pos || m->count)
5115 ptr++;
5116
5117 (*pos)++;
5118
5119 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5120 ptr++) {
5121 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
5122 continue;
5123
5124 return ptr;
5125 }
5126
5127 return NULL;
5128}
5129
5130static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5131{
5132 void *v;
5133 loff_t l = 0;
5134
5135 preempt_disable();
5136 arch_spin_lock(&trace_cmdline_lock);
5137
5138 v = &savedcmd->map_cmdline_to_pid[0];
5139 while (l <= *pos) {
5140 v = saved_cmdlines_next(m, v, &l);
5141 if (!v)
5142 return NULL;
5143 }
5144
5145 return v;
5146}
5147
5148static void saved_cmdlines_stop(struct seq_file *m, void *v)
5149{
5150 arch_spin_unlock(&trace_cmdline_lock);
5151 preempt_enable();
5152}
5153
5154static int saved_cmdlines_show(struct seq_file *m, void *v)
5155{
5156 char buf[TASK_COMM_LEN];
5157 unsigned int *pid = v;
5158
5159 __trace_find_cmdline(*pid, buf);
5160 seq_printf(m, "%d %s\n", *pid, buf);
5161 return 0;
5162}
5163
5164static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5165 .start = saved_cmdlines_start,
5166 .next = saved_cmdlines_next,
5167 .stop = saved_cmdlines_stop,
5168 .show = saved_cmdlines_show,
5169};
5170
5171static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5172{
David Brazdil0f672f62019-12-10 10:32:29 +00005173 int ret;
5174
5175 ret = tracing_check_open_get_tr(NULL);
5176 if (ret)
5177 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005178
5179 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
5180}
5181
5182static const struct file_operations tracing_saved_cmdlines_fops = {
5183 .open = tracing_saved_cmdlines_open,
5184 .read = seq_read,
5185 .llseek = seq_lseek,
5186 .release = seq_release,
5187};
5188
5189static ssize_t
5190tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5191 size_t cnt, loff_t *ppos)
5192{
5193 char buf[64];
5194 int r;
5195
5196 arch_spin_lock(&trace_cmdline_lock);
5197 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
5198 arch_spin_unlock(&trace_cmdline_lock);
5199
5200 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5201}
5202
5203static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5204{
5205 kfree(s->saved_cmdlines);
5206 kfree(s->map_cmdline_to_pid);
5207 kfree(s);
5208}
5209
5210static int tracing_resize_saved_cmdlines(unsigned int val)
5211{
5212 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5213
5214 s = kmalloc(sizeof(*s), GFP_KERNEL);
5215 if (!s)
5216 return -ENOMEM;
5217
5218 if (allocate_cmdlines_buffer(val, s) < 0) {
5219 kfree(s);
5220 return -ENOMEM;
5221 }
5222
5223 arch_spin_lock(&trace_cmdline_lock);
5224 savedcmd_temp = savedcmd;
5225 savedcmd = s;
5226 arch_spin_unlock(&trace_cmdline_lock);
5227 free_saved_cmdlines_buffer(savedcmd_temp);
5228
5229 return 0;
5230}
5231
5232static ssize_t
5233tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5234 size_t cnt, loff_t *ppos)
5235{
5236 unsigned long val;
5237 int ret;
5238
5239 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5240 if (ret)
5241 return ret;
5242
5243 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5244 if (!val || val > PID_MAX_DEFAULT)
5245 return -EINVAL;
5246
5247 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5248 if (ret < 0)
5249 return ret;
5250
5251 *ppos += cnt;
5252
5253 return cnt;
5254}
5255
5256static const struct file_operations tracing_saved_cmdlines_size_fops = {
5257 .open = tracing_open_generic,
5258 .read = tracing_saved_cmdlines_size_read,
5259 .write = tracing_saved_cmdlines_size_write,
5260};
5261
5262#ifdef CONFIG_TRACE_EVAL_MAP_FILE
5263static union trace_eval_map_item *
5264update_eval_map(union trace_eval_map_item *ptr)
5265{
5266 if (!ptr->map.eval_string) {
5267 if (ptr->tail.next) {
5268 ptr = ptr->tail.next;
5269 /* Set ptr to the next real item (skip head) */
5270 ptr++;
5271 } else
5272 return NULL;
5273 }
5274 return ptr;
5275}
5276
5277static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
5278{
5279 union trace_eval_map_item *ptr = v;
5280
5281 /*
5282 * Paranoid! If ptr points to end, we don't want to increment past it.
5283 * This really should never happen.
5284 */
5285 ptr = update_eval_map(ptr);
5286 if (WARN_ON_ONCE(!ptr))
5287 return NULL;
5288
5289 ptr++;
5290
5291 (*pos)++;
5292
5293 ptr = update_eval_map(ptr);
5294
5295 return ptr;
5296}
5297
5298static void *eval_map_start(struct seq_file *m, loff_t *pos)
5299{
5300 union trace_eval_map_item *v;
5301 loff_t l = 0;
5302
5303 mutex_lock(&trace_eval_mutex);
5304
5305 v = trace_eval_maps;
5306 if (v)
5307 v++;
5308
5309 while (v && l < *pos) {
5310 v = eval_map_next(m, v, &l);
5311 }
5312
5313 return v;
5314}
5315
5316static void eval_map_stop(struct seq_file *m, void *v)
5317{
5318 mutex_unlock(&trace_eval_mutex);
5319}
5320
5321static int eval_map_show(struct seq_file *m, void *v)
5322{
5323 union trace_eval_map_item *ptr = v;
5324
5325 seq_printf(m, "%s %ld (%s)\n",
5326 ptr->map.eval_string, ptr->map.eval_value,
5327 ptr->map.system);
5328
5329 return 0;
5330}
5331
5332static const struct seq_operations tracing_eval_map_seq_ops = {
5333 .start = eval_map_start,
5334 .next = eval_map_next,
5335 .stop = eval_map_stop,
5336 .show = eval_map_show,
5337};
5338
5339static int tracing_eval_map_open(struct inode *inode, struct file *filp)
5340{
David Brazdil0f672f62019-12-10 10:32:29 +00005341 int ret;
5342
5343 ret = tracing_check_open_get_tr(NULL);
5344 if (ret)
5345 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005346
5347 return seq_open(filp, &tracing_eval_map_seq_ops);
5348}
5349
5350static const struct file_operations tracing_eval_map_fops = {
5351 .open = tracing_eval_map_open,
5352 .read = seq_read,
5353 .llseek = seq_lseek,
5354 .release = seq_release,
5355};
5356
5357static inline union trace_eval_map_item *
5358trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
5359{
5360 /* Return tail of array given the head */
5361 return ptr + ptr->head.length + 1;
5362}
5363
5364static void
5365trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
5366 int len)
5367{
5368 struct trace_eval_map **stop;
5369 struct trace_eval_map **map;
5370 union trace_eval_map_item *map_array;
5371 union trace_eval_map_item *ptr;
5372
5373 stop = start + len;
5374
5375 /*
5376 * The trace_eval_maps contains the map plus a head and tail item,
5377 * where the head holds the module and length of array, and the
5378 * tail holds a pointer to the next list.
5379 */
5380 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
5381 if (!map_array) {
5382 pr_warn("Unable to allocate trace eval mapping\n");
5383 return;
5384 }
5385
5386 mutex_lock(&trace_eval_mutex);
5387
5388 if (!trace_eval_maps)
5389 trace_eval_maps = map_array;
5390 else {
5391 ptr = trace_eval_maps;
5392 for (;;) {
5393 ptr = trace_eval_jmp_to_tail(ptr);
5394 if (!ptr->tail.next)
5395 break;
5396 ptr = ptr->tail.next;
5397
5398 }
5399 ptr->tail.next = map_array;
5400 }
5401 map_array->head.mod = mod;
5402 map_array->head.length = len;
5403 map_array++;
5404
5405 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5406 map_array->map = **map;
5407 map_array++;
5408 }
5409 memset(map_array, 0, sizeof(*map_array));
5410
5411 mutex_unlock(&trace_eval_mutex);
5412}
5413
5414static void trace_create_eval_file(struct dentry *d_tracer)
5415{
5416 trace_create_file("eval_map", 0444, d_tracer,
5417 NULL, &tracing_eval_map_fops);
5418}
5419
5420#else /* CONFIG_TRACE_EVAL_MAP_FILE */
5421static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5422static inline void trace_insert_eval_map_file(struct module *mod,
5423 struct trace_eval_map **start, int len) { }
5424#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
5425
5426static void trace_insert_eval_map(struct module *mod,
5427 struct trace_eval_map **start, int len)
5428{
5429 struct trace_eval_map **map;
5430
5431 if (len <= 0)
5432 return;
5433
5434 map = start;
5435
5436 trace_event_eval_update(map, len);
5437
5438 trace_insert_eval_map_file(mod, start, len);
5439}
5440
5441static ssize_t
5442tracing_set_trace_read(struct file *filp, char __user *ubuf,
5443 size_t cnt, loff_t *ppos)
5444{
5445 struct trace_array *tr = filp->private_data;
5446 char buf[MAX_TRACER_SIZE+2];
5447 int r;
5448
5449 mutex_lock(&trace_types_lock);
5450 r = sprintf(buf, "%s\n", tr->current_trace->name);
5451 mutex_unlock(&trace_types_lock);
5452
5453 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5454}
5455
5456int tracer_init(struct tracer *t, struct trace_array *tr)
5457{
5458 tracing_reset_online_cpus(&tr->trace_buffer);
5459 return t->init(tr);
5460}
5461
5462static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
5463{
5464 int cpu;
5465
5466 for_each_tracing_cpu(cpu)
5467 per_cpu_ptr(buf->data, cpu)->entries = val;
5468}
5469
5470#ifdef CONFIG_TRACER_MAX_TRACE
5471/* resize @tr's buffer to the size of @size_tr's entries */
5472static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5473 struct trace_buffer *size_buf, int cpu_id)
5474{
5475 int cpu, ret = 0;
5476
5477 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5478 for_each_tracing_cpu(cpu) {
5479 ret = ring_buffer_resize(trace_buf->buffer,
5480 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
5481 if (ret < 0)
5482 break;
5483 per_cpu_ptr(trace_buf->data, cpu)->entries =
5484 per_cpu_ptr(size_buf->data, cpu)->entries;
5485 }
5486 } else {
5487 ret = ring_buffer_resize(trace_buf->buffer,
5488 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
5489 if (ret == 0)
5490 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5491 per_cpu_ptr(size_buf->data, cpu_id)->entries;
5492 }
5493
5494 return ret;
5495}
5496#endif /* CONFIG_TRACER_MAX_TRACE */
5497
5498static int __tracing_resize_ring_buffer(struct trace_array *tr,
5499 unsigned long size, int cpu)
5500{
5501 int ret;
5502
5503 /*
5504 * If kernel or user changes the size of the ring buffer
5505 * we use the size that was given, and we can forget about
5506 * expanding it later.
5507 */
5508 ring_buffer_expanded = true;
5509
5510 /* May be called before buffers are initialized */
5511 if (!tr->trace_buffer.buffer)
5512 return 0;
5513
5514 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
5515 if (ret < 0)
5516 return ret;
5517
5518#ifdef CONFIG_TRACER_MAX_TRACE
5519 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5520 !tr->current_trace->use_max_tr)
5521 goto out;
5522
5523 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
5524 if (ret < 0) {
5525 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5526 &tr->trace_buffer, cpu);
5527 if (r < 0) {
5528 /*
5529 * AARGH! We are left with different
5530 * size max buffer!!!!
5531 * The max buffer is our "snapshot" buffer.
5532 * When a tracer needs a snapshot (one of the
5533 * latency tracers), it swaps the max buffer
5534 * with the saved snap shot. We succeeded to
5535 * update the size of the main buffer, but failed to
5536 * update the size of the max buffer. But when we tried
5537 * to reset the main buffer to the original size, we
5538 * failed there too. This is very unlikely to
5539 * happen, but if it does, warn and kill all
5540 * tracing.
5541 */
5542 WARN_ON(1);
5543 tracing_disabled = 1;
5544 }
5545 return ret;
5546 }
5547
5548 if (cpu == RING_BUFFER_ALL_CPUS)
5549 set_buffer_entries(&tr->max_buffer, size);
5550 else
5551 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
5552
5553 out:
5554#endif /* CONFIG_TRACER_MAX_TRACE */
5555
5556 if (cpu == RING_BUFFER_ALL_CPUS)
5557 set_buffer_entries(&tr->trace_buffer, size);
5558 else
5559 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
5560
5561 return ret;
5562}
5563
5564static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5565 unsigned long size, int cpu_id)
5566{
5567 int ret = size;
5568
5569 mutex_lock(&trace_types_lock);
5570
5571 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5572 /* make sure, this cpu is enabled in the mask */
5573 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5574 ret = -EINVAL;
5575 goto out;
5576 }
5577 }
5578
5579 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
5580 if (ret < 0)
5581 ret = -ENOMEM;
5582
5583out:
5584 mutex_unlock(&trace_types_lock);
5585
5586 return ret;
5587}
5588
5589
5590/**
5591 * tracing_update_buffers - used by tracing facility to expand ring buffers
5592 *
5593 * To save on memory when the tracing is never used on a system with it
5594 * configured in. The ring buffers are set to a minimum size. But once
5595 * a user starts to use the tracing facility, then they need to grow
5596 * to their default size.
5597 *
5598 * This function is to be called when a tracer is about to be used.
5599 */
5600int tracing_update_buffers(void)
5601{
5602 int ret = 0;
5603
5604 mutex_lock(&trace_types_lock);
5605 if (!ring_buffer_expanded)
5606 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
5607 RING_BUFFER_ALL_CPUS);
5608 mutex_unlock(&trace_types_lock);
5609
5610 return ret;
5611}
5612
5613struct trace_option_dentry;
5614
5615static void
5616create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
5617
5618/*
5619 * Used to clear out the tracer before deletion of an instance.
5620 * Must have trace_types_lock held.
5621 */
5622static void tracing_set_nop(struct trace_array *tr)
5623{
5624 if (tr->current_trace == &nop_trace)
5625 return;
5626
5627 tr->current_trace->enabled--;
5628
5629 if (tr->current_trace->reset)
5630 tr->current_trace->reset(tr);
5631
5632 tr->current_trace = &nop_trace;
5633}
5634
5635static void add_tracer_options(struct trace_array *tr, struct tracer *t)
5636{
5637 /* Only enable if the directory has been created already. */
5638 if (!tr->dir)
5639 return;
5640
5641 create_trace_option_files(tr, t);
5642}
5643
5644static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5645{
5646 struct tracer *t;
5647#ifdef CONFIG_TRACER_MAX_TRACE
5648 bool had_max_tr;
5649#endif
5650 int ret = 0;
5651
5652 mutex_lock(&trace_types_lock);
5653
5654 if (!ring_buffer_expanded) {
5655 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
5656 RING_BUFFER_ALL_CPUS);
5657 if (ret < 0)
5658 goto out;
5659 ret = 0;
5660 }
5661
5662 for (t = trace_types; t; t = t->next) {
5663 if (strcmp(t->name, buf) == 0)
5664 break;
5665 }
5666 if (!t) {
5667 ret = -EINVAL;
5668 goto out;
5669 }
5670 if (t == tr->current_trace)
5671 goto out;
5672
David Brazdil0f672f62019-12-10 10:32:29 +00005673#ifdef CONFIG_TRACER_SNAPSHOT
5674 if (t->use_max_tr) {
5675 arch_spin_lock(&tr->max_lock);
5676 if (tr->cond_snapshot)
5677 ret = -EBUSY;
5678 arch_spin_unlock(&tr->max_lock);
5679 if (ret)
5680 goto out;
5681 }
5682#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005683 /* Some tracers won't work on kernel command line */
5684 if (system_state < SYSTEM_RUNNING && t->noboot) {
5685 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5686 t->name);
5687 goto out;
5688 }
5689
5690 /* Some tracers are only allowed for the top level buffer */
5691 if (!trace_ok_for_array(t, tr)) {
5692 ret = -EINVAL;
5693 goto out;
5694 }
5695
5696 /* If trace pipe files are being read, we can't change the tracer */
Olivier Deprez0e641232021-09-23 10:07:05 +02005697 if (tr->trace_ref) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005698 ret = -EBUSY;
5699 goto out;
5700 }
5701
5702 trace_branch_disable();
5703
5704 tr->current_trace->enabled--;
5705
5706 if (tr->current_trace->reset)
5707 tr->current_trace->reset(tr);
5708
David Brazdil0f672f62019-12-10 10:32:29 +00005709 /* Current trace needs to be nop_trace before synchronize_rcu */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005710 tr->current_trace = &nop_trace;
5711
5712#ifdef CONFIG_TRACER_MAX_TRACE
5713 had_max_tr = tr->allocated_snapshot;
5714
5715 if (had_max_tr && !t->use_max_tr) {
5716 /*
5717 * We need to make sure that the update_max_tr sees that
5718 * current_trace changed to nop_trace to keep it from
5719 * swapping the buffers after we resize it.
5720 * The update_max_tr is called from interrupts disabled
5721 * so a synchronized_sched() is sufficient.
5722 */
David Brazdil0f672f62019-12-10 10:32:29 +00005723 synchronize_rcu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005724 free_snapshot(tr);
5725 }
5726#endif
5727
5728#ifdef CONFIG_TRACER_MAX_TRACE
5729 if (t->use_max_tr && !had_max_tr) {
5730 ret = tracing_alloc_snapshot_instance(tr);
5731 if (ret < 0)
5732 goto out;
5733 }
5734#endif
5735
5736 if (t->init) {
5737 ret = tracer_init(t, tr);
5738 if (ret)
5739 goto out;
5740 }
5741
5742 tr->current_trace = t;
5743 tr->current_trace->enabled++;
5744 trace_branch_enable(tr);
5745 out:
5746 mutex_unlock(&trace_types_lock);
5747
5748 return ret;
5749}
5750
5751static ssize_t
5752tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5753 size_t cnt, loff_t *ppos)
5754{
5755 struct trace_array *tr = filp->private_data;
5756 char buf[MAX_TRACER_SIZE+1];
5757 int i;
5758 size_t ret;
5759 int err;
5760
5761 ret = cnt;
5762
5763 if (cnt > MAX_TRACER_SIZE)
5764 cnt = MAX_TRACER_SIZE;
5765
5766 if (copy_from_user(buf, ubuf, cnt))
5767 return -EFAULT;
5768
5769 buf[cnt] = 0;
5770
5771 /* strip ending whitespace. */
5772 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5773 buf[i] = 0;
5774
5775 err = tracing_set_tracer(tr, buf);
5776 if (err)
5777 return err;
5778
5779 *ppos += ret;
5780
5781 return ret;
5782}
5783
5784static ssize_t
5785tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5786 size_t cnt, loff_t *ppos)
5787{
5788 char buf[64];
5789 int r;
5790
5791 r = snprintf(buf, sizeof(buf), "%ld\n",
5792 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
5793 if (r > sizeof(buf))
5794 r = sizeof(buf);
5795 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5796}
5797
5798static ssize_t
5799tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5800 size_t cnt, loff_t *ppos)
5801{
5802 unsigned long val;
5803 int ret;
5804
5805 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5806 if (ret)
5807 return ret;
5808
5809 *ptr = val * 1000;
5810
5811 return cnt;
5812}
5813
5814static ssize_t
5815tracing_thresh_read(struct file *filp, char __user *ubuf,
5816 size_t cnt, loff_t *ppos)
5817{
5818 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5819}
5820
5821static ssize_t
5822tracing_thresh_write(struct file *filp, const char __user *ubuf,
5823 size_t cnt, loff_t *ppos)
5824{
5825 struct trace_array *tr = filp->private_data;
5826 int ret;
5827
5828 mutex_lock(&trace_types_lock);
5829 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5830 if (ret < 0)
5831 goto out;
5832
5833 if (tr->current_trace->update_thresh) {
5834 ret = tr->current_trace->update_thresh(tr);
5835 if (ret < 0)
5836 goto out;
5837 }
5838
5839 ret = cnt;
5840out:
5841 mutex_unlock(&trace_types_lock);
5842
5843 return ret;
5844}
5845
5846#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
5847
5848static ssize_t
5849tracing_max_lat_read(struct file *filp, char __user *ubuf,
5850 size_t cnt, loff_t *ppos)
5851{
5852 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5853}
5854
5855static ssize_t
5856tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5857 size_t cnt, loff_t *ppos)
5858{
5859 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5860}
5861
5862#endif
5863
5864static int tracing_open_pipe(struct inode *inode, struct file *filp)
5865{
5866 struct trace_array *tr = inode->i_private;
5867 struct trace_iterator *iter;
David Brazdil0f672f62019-12-10 10:32:29 +00005868 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005869
David Brazdil0f672f62019-12-10 10:32:29 +00005870 ret = tracing_check_open_get_tr(tr);
5871 if (ret)
5872 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005873
5874 mutex_lock(&trace_types_lock);
5875
5876 /* create a buffer to store the information to pass to userspace */
5877 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
5878 if (!iter) {
5879 ret = -ENOMEM;
5880 __trace_array_put(tr);
5881 goto out;
5882 }
5883
5884 trace_seq_init(&iter->seq);
5885 iter->trace = tr->current_trace;
5886
5887 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
5888 ret = -ENOMEM;
5889 goto fail;
5890 }
5891
5892 /* trace pipe does not show start of buffer */
5893 cpumask_setall(iter->started);
5894
5895 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
5896 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5897
5898 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
5899 if (trace_clocks[tr->clock_id].in_ns)
5900 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5901
5902 iter->tr = tr;
5903 iter->trace_buffer = &tr->trace_buffer;
5904 iter->cpu_file = tracing_get_cpu(inode);
5905 mutex_init(&iter->mutex);
5906 filp->private_data = iter;
5907
5908 if (iter->trace->pipe_open)
5909 iter->trace->pipe_open(iter);
5910
5911 nonseekable_open(inode, filp);
5912
Olivier Deprez0e641232021-09-23 10:07:05 +02005913 tr->trace_ref++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005914out:
5915 mutex_unlock(&trace_types_lock);
5916 return ret;
5917
5918fail:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005919 kfree(iter);
5920 __trace_array_put(tr);
5921 mutex_unlock(&trace_types_lock);
5922 return ret;
5923}
5924
5925static int tracing_release_pipe(struct inode *inode, struct file *file)
5926{
5927 struct trace_iterator *iter = file->private_data;
5928 struct trace_array *tr = inode->i_private;
5929
5930 mutex_lock(&trace_types_lock);
5931
Olivier Deprez0e641232021-09-23 10:07:05 +02005932 tr->trace_ref--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005933
5934 if (iter->trace->pipe_close)
5935 iter->trace->pipe_close(iter);
5936
5937 mutex_unlock(&trace_types_lock);
5938
5939 free_cpumask_var(iter->started);
5940 mutex_destroy(&iter->mutex);
5941 kfree(iter);
5942
5943 trace_array_put(tr);
5944
5945 return 0;
5946}
5947
5948static __poll_t
5949trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
5950{
5951 struct trace_array *tr = iter->tr;
5952
5953 /* Iterators are static, they should be filled or empty */
5954 if (trace_buffer_iter(iter, iter->cpu_file))
5955 return EPOLLIN | EPOLLRDNORM;
5956
5957 if (tr->trace_flags & TRACE_ITER_BLOCK)
5958 /*
5959 * Always select as readable when in blocking mode
5960 */
5961 return EPOLLIN | EPOLLRDNORM;
5962 else
5963 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
5964 filp, poll_table);
5965}
5966
5967static __poll_t
5968tracing_poll_pipe(struct file *filp, poll_table *poll_table)
5969{
5970 struct trace_iterator *iter = filp->private_data;
5971
5972 return trace_poll(iter, filp, poll_table);
5973}
5974
5975/* Must be called with iter->mutex held. */
5976static int tracing_wait_pipe(struct file *filp)
5977{
5978 struct trace_iterator *iter = filp->private_data;
5979 int ret;
5980
5981 while (trace_empty(iter)) {
5982
5983 if ((filp->f_flags & O_NONBLOCK)) {
5984 return -EAGAIN;
5985 }
5986
5987 /*
5988 * We block until we read something and tracing is disabled.
5989 * We still block if tracing is disabled, but we have never
5990 * read anything. This allows a user to cat this file, and
5991 * then enable tracing. But after we have read something,
5992 * we give an EOF when tracing is again disabled.
5993 *
5994 * iter->pos will be 0 if we haven't read anything.
5995 */
5996 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
5997 break;
5998
5999 mutex_unlock(&iter->mutex);
6000
David Brazdil0f672f62019-12-10 10:32:29 +00006001 ret = wait_on_pipe(iter, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006002
6003 mutex_lock(&iter->mutex);
6004
6005 if (ret)
6006 return ret;
6007 }
6008
6009 return 1;
6010}
6011
6012/*
6013 * Consumer reader.
6014 */
6015static ssize_t
6016tracing_read_pipe(struct file *filp, char __user *ubuf,
6017 size_t cnt, loff_t *ppos)
6018{
6019 struct trace_iterator *iter = filp->private_data;
6020 ssize_t sret;
6021
6022 /*
6023 * Avoid more than one consumer on a single file descriptor
6024 * This is just a matter of traces coherency, the ring buffer itself
6025 * is protected.
6026 */
6027 mutex_lock(&iter->mutex);
6028
6029 /* return any leftover data */
6030 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6031 if (sret != -EBUSY)
6032 goto out;
6033
6034 trace_seq_init(&iter->seq);
6035
6036 if (iter->trace->read) {
6037 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6038 if (sret)
6039 goto out;
6040 }
6041
6042waitagain:
6043 sret = tracing_wait_pipe(filp);
6044 if (sret <= 0)
6045 goto out;
6046
6047 /* stop when tracing is finished */
6048 if (trace_empty(iter)) {
6049 sret = 0;
6050 goto out;
6051 }
6052
6053 if (cnt >= PAGE_SIZE)
6054 cnt = PAGE_SIZE - 1;
6055
6056 /* reset all but tr, trace, and overruns */
6057 memset(&iter->seq, 0,
6058 sizeof(struct trace_iterator) -
6059 offsetof(struct trace_iterator, seq));
6060 cpumask_clear(iter->started);
David Brazdil0f672f62019-12-10 10:32:29 +00006061 trace_seq_init(&iter->seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006062 iter->pos = -1;
6063
6064 trace_event_read_lock();
6065 trace_access_lock(iter->cpu_file);
6066 while (trace_find_next_entry_inc(iter) != NULL) {
6067 enum print_line_t ret;
6068 int save_len = iter->seq.seq.len;
6069
6070 ret = print_trace_line(iter);
6071 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6072 /* don't print partial lines */
6073 iter->seq.seq.len = save_len;
6074 break;
6075 }
6076 if (ret != TRACE_TYPE_NO_CONSUME)
6077 trace_consume(iter);
6078
6079 if (trace_seq_used(&iter->seq) >= cnt)
6080 break;
6081
6082 /*
6083 * Setting the full flag means we reached the trace_seq buffer
6084 * size and we should leave by partial output condition above.
6085 * One of the trace_seq_* functions is not used properly.
6086 */
6087 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6088 iter->ent->type);
6089 }
6090 trace_access_unlock(iter->cpu_file);
6091 trace_event_read_unlock();
6092
6093 /* Now copy what we have to the user */
6094 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6095 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
6096 trace_seq_init(&iter->seq);
6097
6098 /*
6099 * If there was nothing to send to user, in spite of consuming trace
6100 * entries, go back to wait for more entries.
6101 */
6102 if (sret == -EBUSY)
6103 goto waitagain;
6104
6105out:
6106 mutex_unlock(&iter->mutex);
6107
6108 return sret;
6109}
6110
6111static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6112 unsigned int idx)
6113{
6114 __free_page(spd->pages[idx]);
6115}
6116
6117static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006118 .confirm = generic_pipe_buf_confirm,
6119 .release = generic_pipe_buf_release,
6120 .steal = generic_pipe_buf_steal,
6121 .get = generic_pipe_buf_get,
6122};
6123
6124static size_t
6125tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
6126{
6127 size_t count;
6128 int save_len;
6129 int ret;
6130
6131 /* Seq buffer is page-sized, exactly what we need. */
6132 for (;;) {
6133 save_len = iter->seq.seq.len;
6134 ret = print_trace_line(iter);
6135
6136 if (trace_seq_has_overflowed(&iter->seq)) {
6137 iter->seq.seq.len = save_len;
6138 break;
6139 }
6140
6141 /*
6142 * This should not be hit, because it should only
6143 * be set if the iter->seq overflowed. But check it
6144 * anyway to be safe.
6145 */
6146 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6147 iter->seq.seq.len = save_len;
6148 break;
6149 }
6150
6151 count = trace_seq_used(&iter->seq) - save_len;
6152 if (rem < count) {
6153 rem = 0;
6154 iter->seq.seq.len = save_len;
6155 break;
6156 }
6157
6158 if (ret != TRACE_TYPE_NO_CONSUME)
6159 trace_consume(iter);
6160 rem -= count;
6161 if (!trace_find_next_entry_inc(iter)) {
6162 rem = 0;
6163 iter->ent = NULL;
6164 break;
6165 }
6166 }
6167
6168 return rem;
6169}
6170
6171static ssize_t tracing_splice_read_pipe(struct file *filp,
6172 loff_t *ppos,
6173 struct pipe_inode_info *pipe,
6174 size_t len,
6175 unsigned int flags)
6176{
6177 struct page *pages_def[PIPE_DEF_BUFFERS];
6178 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6179 struct trace_iterator *iter = filp->private_data;
6180 struct splice_pipe_desc spd = {
6181 .pages = pages_def,
6182 .partial = partial_def,
6183 .nr_pages = 0, /* This gets updated below. */
6184 .nr_pages_max = PIPE_DEF_BUFFERS,
6185 .ops = &tracing_pipe_buf_ops,
6186 .spd_release = tracing_spd_release_pipe,
6187 };
6188 ssize_t ret;
6189 size_t rem;
6190 unsigned int i;
6191
6192 if (splice_grow_spd(pipe, &spd))
6193 return -ENOMEM;
6194
6195 mutex_lock(&iter->mutex);
6196
6197 if (iter->trace->splice_read) {
6198 ret = iter->trace->splice_read(iter, filp,
6199 ppos, pipe, len, flags);
6200 if (ret)
6201 goto out_err;
6202 }
6203
6204 ret = tracing_wait_pipe(filp);
6205 if (ret <= 0)
6206 goto out_err;
6207
6208 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
6209 ret = -EFAULT;
6210 goto out_err;
6211 }
6212
6213 trace_event_read_lock();
6214 trace_access_lock(iter->cpu_file);
6215
6216 /* Fill as many pages as possible. */
6217 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
6218 spd.pages[i] = alloc_page(GFP_KERNEL);
6219 if (!spd.pages[i])
6220 break;
6221
6222 rem = tracing_fill_pipe_page(rem, iter);
6223
6224 /* Copy the data into the page, so we can start over. */
6225 ret = trace_seq_to_buffer(&iter->seq,
6226 page_address(spd.pages[i]),
6227 trace_seq_used(&iter->seq));
6228 if (ret < 0) {
6229 __free_page(spd.pages[i]);
6230 break;
6231 }
6232 spd.partial[i].offset = 0;
6233 spd.partial[i].len = trace_seq_used(&iter->seq);
6234
6235 trace_seq_init(&iter->seq);
6236 }
6237
6238 trace_access_unlock(iter->cpu_file);
6239 trace_event_read_unlock();
6240 mutex_unlock(&iter->mutex);
6241
6242 spd.nr_pages = i;
6243
6244 if (i)
6245 ret = splice_to_pipe(pipe, &spd);
6246 else
6247 ret = 0;
6248out:
6249 splice_shrink_spd(&spd);
6250 return ret;
6251
6252out_err:
6253 mutex_unlock(&iter->mutex);
6254 goto out;
6255}
6256
6257static ssize_t
6258tracing_entries_read(struct file *filp, char __user *ubuf,
6259 size_t cnt, loff_t *ppos)
6260{
6261 struct inode *inode = file_inode(filp);
6262 struct trace_array *tr = inode->i_private;
6263 int cpu = tracing_get_cpu(inode);
6264 char buf[64];
6265 int r = 0;
6266 ssize_t ret;
6267
6268 mutex_lock(&trace_types_lock);
6269
6270 if (cpu == RING_BUFFER_ALL_CPUS) {
6271 int cpu, buf_size_same;
6272 unsigned long size;
6273
6274 size = 0;
6275 buf_size_same = 1;
6276 /* check if all cpu sizes are same */
6277 for_each_tracing_cpu(cpu) {
6278 /* fill in the size from first enabled cpu */
6279 if (size == 0)
6280 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
6281 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
6282 buf_size_same = 0;
6283 break;
6284 }
6285 }
6286
6287 if (buf_size_same) {
6288 if (!ring_buffer_expanded)
6289 r = sprintf(buf, "%lu (expanded: %lu)\n",
6290 size >> 10,
6291 trace_buf_size >> 10);
6292 else
6293 r = sprintf(buf, "%lu\n", size >> 10);
6294 } else
6295 r = sprintf(buf, "X\n");
6296 } else
6297 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
6298
6299 mutex_unlock(&trace_types_lock);
6300
6301 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6302 return ret;
6303}
6304
6305static ssize_t
6306tracing_entries_write(struct file *filp, const char __user *ubuf,
6307 size_t cnt, loff_t *ppos)
6308{
6309 struct inode *inode = file_inode(filp);
6310 struct trace_array *tr = inode->i_private;
6311 unsigned long val;
6312 int ret;
6313
6314 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6315 if (ret)
6316 return ret;
6317
6318 /* must have at least 1 entry */
6319 if (!val)
6320 return -EINVAL;
6321
6322 /* value is in KB */
6323 val <<= 10;
6324 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
6325 if (ret < 0)
6326 return ret;
6327
6328 *ppos += cnt;
6329
6330 return cnt;
6331}
6332
6333static ssize_t
6334tracing_total_entries_read(struct file *filp, char __user *ubuf,
6335 size_t cnt, loff_t *ppos)
6336{
6337 struct trace_array *tr = filp->private_data;
6338 char buf[64];
6339 int r, cpu;
6340 unsigned long size = 0, expanded_size = 0;
6341
6342 mutex_lock(&trace_types_lock);
6343 for_each_tracing_cpu(cpu) {
6344 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
6345 if (!ring_buffer_expanded)
6346 expanded_size += trace_buf_size >> 10;
6347 }
6348 if (ring_buffer_expanded)
6349 r = sprintf(buf, "%lu\n", size);
6350 else
6351 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6352 mutex_unlock(&trace_types_lock);
6353
6354 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6355}
6356
6357static ssize_t
6358tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6359 size_t cnt, loff_t *ppos)
6360{
6361 /*
6362 * There is no need to read what the user has written, this function
6363 * is just to make sure that there is no error when "echo" is used
6364 */
6365
6366 *ppos += cnt;
6367
6368 return cnt;
6369}
6370
6371static int
6372tracing_free_buffer_release(struct inode *inode, struct file *filp)
6373{
6374 struct trace_array *tr = inode->i_private;
6375
6376 /* disable tracing ? */
6377 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
6378 tracer_tracing_off(tr);
6379 /* resize the ring buffer to 0 */
6380 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
6381
6382 trace_array_put(tr);
6383
6384 return 0;
6385}
6386
6387static ssize_t
6388tracing_mark_write(struct file *filp, const char __user *ubuf,
6389 size_t cnt, loff_t *fpos)
6390{
6391 struct trace_array *tr = filp->private_data;
6392 struct ring_buffer_event *event;
6393 enum event_trigger_type tt = ETT_NONE;
6394 struct ring_buffer *buffer;
6395 struct print_entry *entry;
6396 unsigned long irq_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006397 ssize_t written;
6398 int size;
6399 int len;
6400
6401/* Used in tracing_mark_raw_write() as well */
David Brazdil0f672f62019-12-10 10:32:29 +00006402#define FAULTED_STR "<faulted>"
6403#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006404
6405 if (tracing_disabled)
6406 return -EINVAL;
6407
6408 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6409 return -EINVAL;
6410
6411 if (cnt > TRACE_BUF_SIZE)
6412 cnt = TRACE_BUF_SIZE;
6413
6414 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6415
6416 local_save_flags(irq_flags);
6417 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6418
6419 /* If less than "<faulted>", then make sure we can still add that */
6420 if (cnt < FAULTED_SIZE)
6421 size += FAULTED_SIZE - cnt;
6422
6423 buffer = tr->trace_buffer.buffer;
6424 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6425 irq_flags, preempt_count());
6426 if (unlikely(!event))
6427 /* Ring buffer disabled, return as if not open for write */
6428 return -EBADF;
6429
6430 entry = ring_buffer_event_data(event);
6431 entry->ip = _THIS_IP_;
6432
6433 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6434 if (len) {
David Brazdil0f672f62019-12-10 10:32:29 +00006435 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006436 cnt = FAULTED_SIZE;
6437 written = -EFAULT;
6438 } else
6439 written = cnt;
6440 len = cnt;
6441
6442 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6443 /* do not add \n before testing triggers, but add \0 */
6444 entry->buf[cnt] = '\0';
6445 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6446 }
6447
6448 if (entry->buf[cnt - 1] != '\n') {
6449 entry->buf[cnt] = '\n';
6450 entry->buf[cnt + 1] = '\0';
6451 } else
6452 entry->buf[cnt] = '\0';
6453
6454 __buffer_unlock_commit(buffer, event);
6455
6456 if (tt)
6457 event_triggers_post_call(tr->trace_marker_file, tt);
6458
6459 if (written > 0)
6460 *fpos += written;
6461
6462 return written;
6463}
6464
6465/* Limit it for now to 3K (including tag) */
6466#define RAW_DATA_MAX_SIZE (1024*3)
6467
6468static ssize_t
6469tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6470 size_t cnt, loff_t *fpos)
6471{
6472 struct trace_array *tr = filp->private_data;
6473 struct ring_buffer_event *event;
6474 struct ring_buffer *buffer;
6475 struct raw_data_entry *entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006476 unsigned long irq_flags;
6477 ssize_t written;
6478 int size;
6479 int len;
6480
6481#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6482
6483 if (tracing_disabled)
6484 return -EINVAL;
6485
6486 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6487 return -EINVAL;
6488
6489 /* The marker must at least have a tag id */
6490 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6491 return -EINVAL;
6492
6493 if (cnt > TRACE_BUF_SIZE)
6494 cnt = TRACE_BUF_SIZE;
6495
6496 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6497
6498 local_save_flags(irq_flags);
6499 size = sizeof(*entry) + cnt;
6500 if (cnt < FAULT_SIZE_ID)
6501 size += FAULT_SIZE_ID - cnt;
6502
6503 buffer = tr->trace_buffer.buffer;
6504 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6505 irq_flags, preempt_count());
6506 if (!event)
6507 /* Ring buffer disabled, return as if not open for write */
6508 return -EBADF;
6509
6510 entry = ring_buffer_event_data(event);
6511
6512 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6513 if (len) {
6514 entry->id = -1;
David Brazdil0f672f62019-12-10 10:32:29 +00006515 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006516 written = -EFAULT;
6517 } else
6518 written = cnt;
6519
6520 __buffer_unlock_commit(buffer, event);
6521
6522 if (written > 0)
6523 *fpos += written;
6524
6525 return written;
6526}
6527
6528static int tracing_clock_show(struct seq_file *m, void *v)
6529{
6530 struct trace_array *tr = m->private;
6531 int i;
6532
6533 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
6534 seq_printf(m,
6535 "%s%s%s%s", i ? " " : "",
6536 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6537 i == tr->clock_id ? "]" : "");
6538 seq_putc(m, '\n');
6539
6540 return 0;
6541}
6542
6543int tracing_set_clock(struct trace_array *tr, const char *clockstr)
6544{
6545 int i;
6546
6547 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6548 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6549 break;
6550 }
6551 if (i == ARRAY_SIZE(trace_clocks))
6552 return -EINVAL;
6553
6554 mutex_lock(&trace_types_lock);
6555
6556 tr->clock_id = i;
6557
6558 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
6559
6560 /*
6561 * New clock may not be consistent with the previous clock.
6562 * Reset the buffer so that it doesn't have incomparable timestamps.
6563 */
6564 tracing_reset_online_cpus(&tr->trace_buffer);
6565
6566#ifdef CONFIG_TRACER_MAX_TRACE
6567 if (tr->max_buffer.buffer)
6568 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
6569 tracing_reset_online_cpus(&tr->max_buffer);
6570#endif
6571
6572 mutex_unlock(&trace_types_lock);
6573
6574 return 0;
6575}
6576
6577static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6578 size_t cnt, loff_t *fpos)
6579{
6580 struct seq_file *m = filp->private_data;
6581 struct trace_array *tr = m->private;
6582 char buf[64];
6583 const char *clockstr;
6584 int ret;
6585
6586 if (cnt >= sizeof(buf))
6587 return -EINVAL;
6588
6589 if (copy_from_user(buf, ubuf, cnt))
6590 return -EFAULT;
6591
6592 buf[cnt] = 0;
6593
6594 clockstr = strstrip(buf);
6595
6596 ret = tracing_set_clock(tr, clockstr);
6597 if (ret)
6598 return ret;
6599
6600 *fpos += cnt;
6601
6602 return cnt;
6603}
6604
6605static int tracing_clock_open(struct inode *inode, struct file *file)
6606{
6607 struct trace_array *tr = inode->i_private;
6608 int ret;
6609
David Brazdil0f672f62019-12-10 10:32:29 +00006610 ret = tracing_check_open_get_tr(tr);
6611 if (ret)
6612 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006613
6614 ret = single_open(file, tracing_clock_show, inode->i_private);
6615 if (ret < 0)
6616 trace_array_put(tr);
6617
6618 return ret;
6619}
6620
6621static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6622{
6623 struct trace_array *tr = m->private;
6624
6625 mutex_lock(&trace_types_lock);
6626
6627 if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6628 seq_puts(m, "delta [absolute]\n");
6629 else
6630 seq_puts(m, "[delta] absolute\n");
6631
6632 mutex_unlock(&trace_types_lock);
6633
6634 return 0;
6635}
6636
6637static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6638{
6639 struct trace_array *tr = inode->i_private;
6640 int ret;
6641
David Brazdil0f672f62019-12-10 10:32:29 +00006642 ret = tracing_check_open_get_tr(tr);
6643 if (ret)
6644 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006645
6646 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6647 if (ret < 0)
6648 trace_array_put(tr);
6649
6650 return ret;
6651}
6652
6653int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6654{
6655 int ret = 0;
6656
6657 mutex_lock(&trace_types_lock);
6658
6659 if (abs && tr->time_stamp_abs_ref++)
6660 goto out;
6661
6662 if (!abs) {
6663 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6664 ret = -EINVAL;
6665 goto out;
6666 }
6667
6668 if (--tr->time_stamp_abs_ref)
6669 goto out;
6670 }
6671
6672 ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
6673
6674#ifdef CONFIG_TRACER_MAX_TRACE
6675 if (tr->max_buffer.buffer)
6676 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6677#endif
6678 out:
6679 mutex_unlock(&trace_types_lock);
6680
6681 return ret;
6682}
6683
6684struct ftrace_buffer_info {
6685 struct trace_iterator iter;
6686 void *spare;
6687 unsigned int spare_cpu;
6688 unsigned int read;
6689};
6690
6691#ifdef CONFIG_TRACER_SNAPSHOT
6692static int tracing_snapshot_open(struct inode *inode, struct file *file)
6693{
6694 struct trace_array *tr = inode->i_private;
6695 struct trace_iterator *iter;
6696 struct seq_file *m;
David Brazdil0f672f62019-12-10 10:32:29 +00006697 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006698
David Brazdil0f672f62019-12-10 10:32:29 +00006699 ret = tracing_check_open_get_tr(tr);
6700 if (ret)
6701 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006702
6703 if (file->f_mode & FMODE_READ) {
6704 iter = __tracing_open(inode, file, true);
6705 if (IS_ERR(iter))
6706 ret = PTR_ERR(iter);
6707 } else {
6708 /* Writes still need the seq_file to hold the private data */
6709 ret = -ENOMEM;
6710 m = kzalloc(sizeof(*m), GFP_KERNEL);
6711 if (!m)
6712 goto out;
6713 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6714 if (!iter) {
6715 kfree(m);
6716 goto out;
6717 }
6718 ret = 0;
6719
6720 iter->tr = tr;
6721 iter->trace_buffer = &tr->max_buffer;
6722 iter->cpu_file = tracing_get_cpu(inode);
6723 m->private = iter;
6724 file->private_data = m;
6725 }
6726out:
6727 if (ret < 0)
6728 trace_array_put(tr);
6729
6730 return ret;
6731}
6732
6733static ssize_t
6734tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6735 loff_t *ppos)
6736{
6737 struct seq_file *m = filp->private_data;
6738 struct trace_iterator *iter = m->private;
6739 struct trace_array *tr = iter->tr;
6740 unsigned long val;
6741 int ret;
6742
6743 ret = tracing_update_buffers();
6744 if (ret < 0)
6745 return ret;
6746
6747 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6748 if (ret)
6749 return ret;
6750
6751 mutex_lock(&trace_types_lock);
6752
6753 if (tr->current_trace->use_max_tr) {
6754 ret = -EBUSY;
6755 goto out;
6756 }
6757
David Brazdil0f672f62019-12-10 10:32:29 +00006758 arch_spin_lock(&tr->max_lock);
6759 if (tr->cond_snapshot)
6760 ret = -EBUSY;
6761 arch_spin_unlock(&tr->max_lock);
6762 if (ret)
6763 goto out;
6764
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006765 switch (val) {
6766 case 0:
6767 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6768 ret = -EINVAL;
6769 break;
6770 }
6771 if (tr->allocated_snapshot)
6772 free_snapshot(tr);
6773 break;
6774 case 1:
6775/* Only allow per-cpu swap if the ring buffer supports it */
6776#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6777 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6778 ret = -EINVAL;
6779 break;
6780 }
6781#endif
David Brazdil0f672f62019-12-10 10:32:29 +00006782 if (tr->allocated_snapshot)
6783 ret = resize_buffer_duplicate_size(&tr->max_buffer,
6784 &tr->trace_buffer, iter->cpu_file);
6785 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006786 ret = tracing_alloc_snapshot_instance(tr);
David Brazdil0f672f62019-12-10 10:32:29 +00006787 if (ret < 0)
6788 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006789 local_irq_disable();
6790 /* Now, we're going to swap */
6791 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
David Brazdil0f672f62019-12-10 10:32:29 +00006792 update_max_tr(tr, current, smp_processor_id(), NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006793 else
6794 update_max_tr_single(tr, current, iter->cpu_file);
6795 local_irq_enable();
6796 break;
6797 default:
6798 if (tr->allocated_snapshot) {
6799 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6800 tracing_reset_online_cpus(&tr->max_buffer);
6801 else
David Brazdil0f672f62019-12-10 10:32:29 +00006802 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006803 }
6804 break;
6805 }
6806
6807 if (ret >= 0) {
6808 *ppos += cnt;
6809 ret = cnt;
6810 }
6811out:
6812 mutex_unlock(&trace_types_lock);
6813 return ret;
6814}
6815
6816static int tracing_snapshot_release(struct inode *inode, struct file *file)
6817{
6818 struct seq_file *m = file->private_data;
6819 int ret;
6820
6821 ret = tracing_release(inode, file);
6822
6823 if (file->f_mode & FMODE_READ)
6824 return ret;
6825
6826 /* If write only, the seq_file is just a stub */
6827 if (m)
6828 kfree(m->private);
6829 kfree(m);
6830
6831 return 0;
6832}
6833
6834static int tracing_buffers_open(struct inode *inode, struct file *filp);
6835static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6836 size_t count, loff_t *ppos);
6837static int tracing_buffers_release(struct inode *inode, struct file *file);
6838static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6839 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6840
6841static int snapshot_raw_open(struct inode *inode, struct file *filp)
6842{
6843 struct ftrace_buffer_info *info;
6844 int ret;
6845
David Brazdil0f672f62019-12-10 10:32:29 +00006846 /* The following checks for tracefs lockdown */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006847 ret = tracing_buffers_open(inode, filp);
6848 if (ret < 0)
6849 return ret;
6850
6851 info = filp->private_data;
6852
6853 if (info->iter.trace->use_max_tr) {
6854 tracing_buffers_release(inode, filp);
6855 return -EBUSY;
6856 }
6857
6858 info->iter.snapshot = true;
6859 info->iter.trace_buffer = &info->iter.tr->max_buffer;
6860
6861 return ret;
6862}
6863
6864#endif /* CONFIG_TRACER_SNAPSHOT */
6865
6866
6867static const struct file_operations tracing_thresh_fops = {
6868 .open = tracing_open_generic,
6869 .read = tracing_thresh_read,
6870 .write = tracing_thresh_write,
6871 .llseek = generic_file_llseek,
6872};
6873
6874#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
6875static const struct file_operations tracing_max_lat_fops = {
6876 .open = tracing_open_generic,
6877 .read = tracing_max_lat_read,
6878 .write = tracing_max_lat_write,
6879 .llseek = generic_file_llseek,
6880};
6881#endif
6882
6883static const struct file_operations set_tracer_fops = {
6884 .open = tracing_open_generic,
6885 .read = tracing_set_trace_read,
6886 .write = tracing_set_trace_write,
6887 .llseek = generic_file_llseek,
6888};
6889
6890static const struct file_operations tracing_pipe_fops = {
6891 .open = tracing_open_pipe,
6892 .poll = tracing_poll_pipe,
6893 .read = tracing_read_pipe,
6894 .splice_read = tracing_splice_read_pipe,
6895 .release = tracing_release_pipe,
6896 .llseek = no_llseek,
6897};
6898
6899static const struct file_operations tracing_entries_fops = {
6900 .open = tracing_open_generic_tr,
6901 .read = tracing_entries_read,
6902 .write = tracing_entries_write,
6903 .llseek = generic_file_llseek,
6904 .release = tracing_release_generic_tr,
6905};
6906
6907static const struct file_operations tracing_total_entries_fops = {
6908 .open = tracing_open_generic_tr,
6909 .read = tracing_total_entries_read,
6910 .llseek = generic_file_llseek,
6911 .release = tracing_release_generic_tr,
6912};
6913
6914static const struct file_operations tracing_free_buffer_fops = {
6915 .open = tracing_open_generic_tr,
6916 .write = tracing_free_buffer_write,
6917 .release = tracing_free_buffer_release,
6918};
6919
6920static const struct file_operations tracing_mark_fops = {
6921 .open = tracing_open_generic_tr,
6922 .write = tracing_mark_write,
6923 .llseek = generic_file_llseek,
6924 .release = tracing_release_generic_tr,
6925};
6926
6927static const struct file_operations tracing_mark_raw_fops = {
6928 .open = tracing_open_generic_tr,
6929 .write = tracing_mark_raw_write,
6930 .llseek = generic_file_llseek,
6931 .release = tracing_release_generic_tr,
6932};
6933
6934static const struct file_operations trace_clock_fops = {
6935 .open = tracing_clock_open,
6936 .read = seq_read,
6937 .llseek = seq_lseek,
6938 .release = tracing_single_release_tr,
6939 .write = tracing_clock_write,
6940};
6941
6942static const struct file_operations trace_time_stamp_mode_fops = {
6943 .open = tracing_time_stamp_mode_open,
6944 .read = seq_read,
6945 .llseek = seq_lseek,
6946 .release = tracing_single_release_tr,
6947};
6948
6949#ifdef CONFIG_TRACER_SNAPSHOT
6950static const struct file_operations snapshot_fops = {
6951 .open = tracing_snapshot_open,
6952 .read = seq_read,
6953 .write = tracing_snapshot_write,
6954 .llseek = tracing_lseek,
6955 .release = tracing_snapshot_release,
6956};
6957
6958static const struct file_operations snapshot_raw_fops = {
6959 .open = snapshot_raw_open,
6960 .read = tracing_buffers_read,
6961 .release = tracing_buffers_release,
6962 .splice_read = tracing_buffers_splice_read,
6963 .llseek = no_llseek,
6964};
6965
6966#endif /* CONFIG_TRACER_SNAPSHOT */
6967
David Brazdil0f672f62019-12-10 10:32:29 +00006968#define TRACING_LOG_ERRS_MAX 8
6969#define TRACING_LOG_LOC_MAX 128
6970
6971#define CMD_PREFIX " Command: "
6972
6973struct err_info {
6974 const char **errs; /* ptr to loc-specific array of err strings */
6975 u8 type; /* index into errs -> specific err string */
6976 u8 pos; /* MAX_FILTER_STR_VAL = 256 */
6977 u64 ts;
6978};
6979
6980struct tracing_log_err {
6981 struct list_head list;
6982 struct err_info info;
6983 char loc[TRACING_LOG_LOC_MAX]; /* err location */
6984 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
6985};
6986
6987static DEFINE_MUTEX(tracing_err_log_lock);
6988
6989static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
6990{
6991 struct tracing_log_err *err;
6992
6993 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
6994 err = kzalloc(sizeof(*err), GFP_KERNEL);
6995 if (!err)
6996 err = ERR_PTR(-ENOMEM);
6997 tr->n_err_log_entries++;
6998
6999 return err;
7000 }
7001
7002 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
7003 list_del(&err->list);
7004
7005 return err;
7006}
7007
7008/**
7009 * err_pos - find the position of a string within a command for error careting
7010 * @cmd: The tracing command that caused the error
7011 * @str: The string to position the caret at within @cmd
7012 *
7013 * Finds the position of the first occurence of @str within @cmd. The
7014 * return value can be passed to tracing_log_err() for caret placement
7015 * within @cmd.
7016 *
7017 * Returns the index within @cmd of the first occurence of @str or 0
7018 * if @str was not found.
7019 */
7020unsigned int err_pos(char *cmd, const char *str)
7021{
7022 char *found;
7023
7024 if (WARN_ON(!strlen(cmd)))
7025 return 0;
7026
7027 found = strstr(cmd, str);
7028 if (found)
7029 return found - cmd;
7030
7031 return 0;
7032}
7033
7034/**
7035 * tracing_log_err - write an error to the tracing error log
7036 * @tr: The associated trace array for the error (NULL for top level array)
7037 * @loc: A string describing where the error occurred
7038 * @cmd: The tracing command that caused the error
7039 * @errs: The array of loc-specific static error strings
7040 * @type: The index into errs[], which produces the specific static err string
7041 * @pos: The position the caret should be placed in the cmd
7042 *
7043 * Writes an error into tracing/error_log of the form:
7044 *
7045 * <loc>: error: <text>
7046 * Command: <cmd>
7047 * ^
7048 *
7049 * tracing/error_log is a small log file containing the last
7050 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7051 * unless there has been a tracing error, and the error log can be
7052 * cleared and have its memory freed by writing the empty string in
7053 * truncation mode to it i.e. echo > tracing/error_log.
7054 *
7055 * NOTE: the @errs array along with the @type param are used to
7056 * produce a static error string - this string is not copied and saved
7057 * when the error is logged - only a pointer to it is saved. See
7058 * existing callers for examples of how static strings are typically
7059 * defined for use with tracing_log_err().
7060 */
7061void tracing_log_err(struct trace_array *tr,
7062 const char *loc, const char *cmd,
7063 const char **errs, u8 type, u8 pos)
7064{
7065 struct tracing_log_err *err;
7066
7067 if (!tr)
7068 tr = &global_trace;
7069
7070 mutex_lock(&tracing_err_log_lock);
7071 err = get_tracing_log_err(tr);
7072 if (PTR_ERR(err) == -ENOMEM) {
7073 mutex_unlock(&tracing_err_log_lock);
7074 return;
7075 }
7076
7077 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7078 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7079
7080 err->info.errs = errs;
7081 err->info.type = type;
7082 err->info.pos = pos;
7083 err->info.ts = local_clock();
7084
7085 list_add_tail(&err->list, &tr->err_log);
7086 mutex_unlock(&tracing_err_log_lock);
7087}
7088
7089static void clear_tracing_err_log(struct trace_array *tr)
7090{
7091 struct tracing_log_err *err, *next;
7092
7093 mutex_lock(&tracing_err_log_lock);
7094 list_for_each_entry_safe(err, next, &tr->err_log, list) {
7095 list_del(&err->list);
7096 kfree(err);
7097 }
7098
7099 tr->n_err_log_entries = 0;
7100 mutex_unlock(&tracing_err_log_lock);
7101}
7102
7103static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7104{
7105 struct trace_array *tr = m->private;
7106
7107 mutex_lock(&tracing_err_log_lock);
7108
7109 return seq_list_start(&tr->err_log, *pos);
7110}
7111
7112static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7113{
7114 struct trace_array *tr = m->private;
7115
7116 return seq_list_next(v, &tr->err_log, pos);
7117}
7118
7119static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7120{
7121 mutex_unlock(&tracing_err_log_lock);
7122}
7123
7124static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7125{
7126 u8 i;
7127
7128 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7129 seq_putc(m, ' ');
7130 for (i = 0; i < pos; i++)
7131 seq_putc(m, ' ');
7132 seq_puts(m, "^\n");
7133}
7134
7135static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7136{
7137 struct tracing_log_err *err = v;
7138
7139 if (err) {
7140 const char *err_text = err->info.errs[err->info.type];
7141 u64 sec = err->info.ts;
7142 u32 nsec;
7143
7144 nsec = do_div(sec, NSEC_PER_SEC);
7145 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7146 err->loc, err_text);
7147 seq_printf(m, "%s", err->cmd);
7148 tracing_err_log_show_pos(m, err->info.pos);
7149 }
7150
7151 return 0;
7152}
7153
7154static const struct seq_operations tracing_err_log_seq_ops = {
7155 .start = tracing_err_log_seq_start,
7156 .next = tracing_err_log_seq_next,
7157 .stop = tracing_err_log_seq_stop,
7158 .show = tracing_err_log_seq_show
7159};
7160
7161static int tracing_err_log_open(struct inode *inode, struct file *file)
7162{
7163 struct trace_array *tr = inode->i_private;
7164 int ret = 0;
7165
7166 ret = tracing_check_open_get_tr(tr);
7167 if (ret)
7168 return ret;
7169
7170 /* If this file was opened for write, then erase contents */
7171 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
7172 clear_tracing_err_log(tr);
7173
7174 if (file->f_mode & FMODE_READ) {
7175 ret = seq_open(file, &tracing_err_log_seq_ops);
7176 if (!ret) {
7177 struct seq_file *m = file->private_data;
7178 m->private = tr;
7179 } else {
7180 trace_array_put(tr);
7181 }
7182 }
7183 return ret;
7184}
7185
7186static ssize_t tracing_err_log_write(struct file *file,
7187 const char __user *buffer,
7188 size_t count, loff_t *ppos)
7189{
7190 return count;
7191}
7192
7193static int tracing_err_log_release(struct inode *inode, struct file *file)
7194{
7195 struct trace_array *tr = inode->i_private;
7196
7197 trace_array_put(tr);
7198
7199 if (file->f_mode & FMODE_READ)
7200 seq_release(inode, file);
7201
7202 return 0;
7203}
7204
7205static const struct file_operations tracing_err_log_fops = {
7206 .open = tracing_err_log_open,
7207 .write = tracing_err_log_write,
7208 .read = seq_read,
7209 .llseek = seq_lseek,
7210 .release = tracing_err_log_release,
7211};
7212
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007213static int tracing_buffers_open(struct inode *inode, struct file *filp)
7214{
7215 struct trace_array *tr = inode->i_private;
7216 struct ftrace_buffer_info *info;
7217 int ret;
7218
David Brazdil0f672f62019-12-10 10:32:29 +00007219 ret = tracing_check_open_get_tr(tr);
7220 if (ret)
7221 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007222
7223 info = kzalloc(sizeof(*info), GFP_KERNEL);
7224 if (!info) {
7225 trace_array_put(tr);
7226 return -ENOMEM;
7227 }
7228
7229 mutex_lock(&trace_types_lock);
7230
7231 info->iter.tr = tr;
7232 info->iter.cpu_file = tracing_get_cpu(inode);
7233 info->iter.trace = tr->current_trace;
7234 info->iter.trace_buffer = &tr->trace_buffer;
7235 info->spare = NULL;
7236 /* Force reading ring buffer for first read */
7237 info->read = (unsigned int)-1;
7238
7239 filp->private_data = info;
7240
Olivier Deprez0e641232021-09-23 10:07:05 +02007241 tr->trace_ref++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007242
7243 mutex_unlock(&trace_types_lock);
7244
7245 ret = nonseekable_open(inode, filp);
7246 if (ret < 0)
7247 trace_array_put(tr);
7248
7249 return ret;
7250}
7251
7252static __poll_t
7253tracing_buffers_poll(struct file *filp, poll_table *poll_table)
7254{
7255 struct ftrace_buffer_info *info = filp->private_data;
7256 struct trace_iterator *iter = &info->iter;
7257
7258 return trace_poll(iter, filp, poll_table);
7259}
7260
7261static ssize_t
7262tracing_buffers_read(struct file *filp, char __user *ubuf,
7263 size_t count, loff_t *ppos)
7264{
7265 struct ftrace_buffer_info *info = filp->private_data;
7266 struct trace_iterator *iter = &info->iter;
7267 ssize_t ret = 0;
7268 ssize_t size;
7269
7270 if (!count)
7271 return 0;
7272
7273#ifdef CONFIG_TRACER_MAX_TRACE
7274 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7275 return -EBUSY;
7276#endif
7277
7278 if (!info->spare) {
7279 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
7280 iter->cpu_file);
7281 if (IS_ERR(info->spare)) {
7282 ret = PTR_ERR(info->spare);
7283 info->spare = NULL;
7284 } else {
7285 info->spare_cpu = iter->cpu_file;
7286 }
7287 }
7288 if (!info->spare)
7289 return ret;
7290
7291 /* Do we have previous read data to read? */
7292 if (info->read < PAGE_SIZE)
7293 goto read;
7294
7295 again:
7296 trace_access_lock(iter->cpu_file);
7297 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
7298 &info->spare,
7299 count,
7300 iter->cpu_file, 0);
7301 trace_access_unlock(iter->cpu_file);
7302
7303 if (ret < 0) {
7304 if (trace_empty(iter)) {
7305 if ((filp->f_flags & O_NONBLOCK))
7306 return -EAGAIN;
7307
David Brazdil0f672f62019-12-10 10:32:29 +00007308 ret = wait_on_pipe(iter, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007309 if (ret)
7310 return ret;
7311
7312 goto again;
7313 }
7314 return 0;
7315 }
7316
7317 info->read = 0;
7318 read:
7319 size = PAGE_SIZE - info->read;
7320 if (size > count)
7321 size = count;
7322
7323 ret = copy_to_user(ubuf, info->spare + info->read, size);
7324 if (ret == size)
7325 return -EFAULT;
7326
7327 size -= ret;
7328
7329 *ppos += size;
7330 info->read += size;
7331
7332 return size;
7333}
7334
7335static int tracing_buffers_release(struct inode *inode, struct file *file)
7336{
7337 struct ftrace_buffer_info *info = file->private_data;
7338 struct trace_iterator *iter = &info->iter;
7339
7340 mutex_lock(&trace_types_lock);
7341
Olivier Deprez0e641232021-09-23 10:07:05 +02007342 iter->tr->trace_ref--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007343
7344 __trace_array_put(iter->tr);
7345
7346 if (info->spare)
7347 ring_buffer_free_read_page(iter->trace_buffer->buffer,
7348 info->spare_cpu, info->spare);
7349 kfree(info);
7350
7351 mutex_unlock(&trace_types_lock);
7352
7353 return 0;
7354}
7355
7356struct buffer_ref {
7357 struct ring_buffer *buffer;
7358 void *page;
7359 int cpu;
David Brazdil0f672f62019-12-10 10:32:29 +00007360 refcount_t refcount;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007361};
7362
David Brazdil0f672f62019-12-10 10:32:29 +00007363static void buffer_ref_release(struct buffer_ref *ref)
7364{
7365 if (!refcount_dec_and_test(&ref->refcount))
7366 return;
7367 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7368 kfree(ref);
7369}
7370
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007371static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7372 struct pipe_buffer *buf)
7373{
7374 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7375
David Brazdil0f672f62019-12-10 10:32:29 +00007376 buffer_ref_release(ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007377 buf->private = 0;
7378}
7379
David Brazdil0f672f62019-12-10 10:32:29 +00007380static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007381 struct pipe_buffer *buf)
7382{
7383 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7384
David Brazdil0f672f62019-12-10 10:32:29 +00007385 if (refcount_read(&ref->refcount) > INT_MAX/2)
7386 return false;
7387
7388 refcount_inc(&ref->refcount);
7389 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007390}
7391
7392/* Pipe buffer operations for a buffer. */
7393static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007394 .confirm = generic_pipe_buf_confirm,
7395 .release = buffer_pipe_buf_release,
David Brazdil0f672f62019-12-10 10:32:29 +00007396 .steal = generic_pipe_buf_nosteal,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007397 .get = buffer_pipe_buf_get,
7398};
7399
7400/*
7401 * Callback from splice_to_pipe(), if we need to release some pages
7402 * at the end of the spd in case we error'ed out in filling the pipe.
7403 */
7404static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7405{
7406 struct buffer_ref *ref =
7407 (struct buffer_ref *)spd->partial[i].private;
7408
David Brazdil0f672f62019-12-10 10:32:29 +00007409 buffer_ref_release(ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007410 spd->partial[i].private = 0;
7411}
7412
7413static ssize_t
7414tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7415 struct pipe_inode_info *pipe, size_t len,
7416 unsigned int flags)
7417{
7418 struct ftrace_buffer_info *info = file->private_data;
7419 struct trace_iterator *iter = &info->iter;
7420 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7421 struct page *pages_def[PIPE_DEF_BUFFERS];
7422 struct splice_pipe_desc spd = {
7423 .pages = pages_def,
7424 .partial = partial_def,
7425 .nr_pages_max = PIPE_DEF_BUFFERS,
7426 .ops = &buffer_pipe_buf_ops,
7427 .spd_release = buffer_spd_release,
7428 };
7429 struct buffer_ref *ref;
7430 int entries, i;
7431 ssize_t ret = 0;
7432
7433#ifdef CONFIG_TRACER_MAX_TRACE
7434 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7435 return -EBUSY;
7436#endif
7437
7438 if (*ppos & (PAGE_SIZE - 1))
7439 return -EINVAL;
7440
7441 if (len & (PAGE_SIZE - 1)) {
7442 if (len < PAGE_SIZE)
7443 return -EINVAL;
7444 len &= PAGE_MASK;
7445 }
7446
7447 if (splice_grow_spd(pipe, &spd))
7448 return -ENOMEM;
7449
7450 again:
7451 trace_access_lock(iter->cpu_file);
7452 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7453
7454 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
7455 struct page *page;
7456 int r;
7457
7458 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
7459 if (!ref) {
7460 ret = -ENOMEM;
7461 break;
7462 }
7463
David Brazdil0f672f62019-12-10 10:32:29 +00007464 refcount_set(&ref->refcount, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007465 ref->buffer = iter->trace_buffer->buffer;
7466 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
7467 if (IS_ERR(ref->page)) {
7468 ret = PTR_ERR(ref->page);
7469 ref->page = NULL;
7470 kfree(ref);
7471 break;
7472 }
7473 ref->cpu = iter->cpu_file;
7474
7475 r = ring_buffer_read_page(ref->buffer, &ref->page,
7476 len, iter->cpu_file, 1);
7477 if (r < 0) {
7478 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7479 ref->page);
7480 kfree(ref);
7481 break;
7482 }
7483
7484 page = virt_to_page(ref->page);
7485
7486 spd.pages[i] = page;
7487 spd.partial[i].len = PAGE_SIZE;
7488 spd.partial[i].offset = 0;
7489 spd.partial[i].private = (unsigned long)ref;
7490 spd.nr_pages++;
7491 *ppos += PAGE_SIZE;
7492
7493 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7494 }
7495
7496 trace_access_unlock(iter->cpu_file);
7497 spd.nr_pages = i;
7498
7499 /* did we read anything? */
7500 if (!spd.nr_pages) {
7501 if (ret)
7502 goto out;
7503
7504 ret = -EAGAIN;
7505 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
7506 goto out;
7507
David Brazdil0f672f62019-12-10 10:32:29 +00007508 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007509 if (ret)
7510 goto out;
7511
7512 goto again;
7513 }
7514
7515 ret = splice_to_pipe(pipe, &spd);
7516out:
7517 splice_shrink_spd(&spd);
7518
7519 return ret;
7520}
7521
7522static const struct file_operations tracing_buffers_fops = {
7523 .open = tracing_buffers_open,
7524 .read = tracing_buffers_read,
7525 .poll = tracing_buffers_poll,
7526 .release = tracing_buffers_release,
7527 .splice_read = tracing_buffers_splice_read,
7528 .llseek = no_llseek,
7529};
7530
7531static ssize_t
7532tracing_stats_read(struct file *filp, char __user *ubuf,
7533 size_t count, loff_t *ppos)
7534{
7535 struct inode *inode = file_inode(filp);
7536 struct trace_array *tr = inode->i_private;
7537 struct trace_buffer *trace_buf = &tr->trace_buffer;
7538 int cpu = tracing_get_cpu(inode);
7539 struct trace_seq *s;
7540 unsigned long cnt;
7541 unsigned long long t;
7542 unsigned long usec_rem;
7543
7544 s = kmalloc(sizeof(*s), GFP_KERNEL);
7545 if (!s)
7546 return -ENOMEM;
7547
7548 trace_seq_init(s);
7549
7550 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
7551 trace_seq_printf(s, "entries: %ld\n", cnt);
7552
7553 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
7554 trace_seq_printf(s, "overrun: %ld\n", cnt);
7555
7556 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
7557 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7558
7559 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
7560 trace_seq_printf(s, "bytes: %ld\n", cnt);
7561
7562 if (trace_clocks[tr->clock_id].in_ns) {
7563 /* local or global for trace_clock */
7564 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
7565 usec_rem = do_div(t, USEC_PER_SEC);
7566 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7567 t, usec_rem);
7568
7569 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
7570 usec_rem = do_div(t, USEC_PER_SEC);
7571 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7572 } else {
7573 /* counter or tsc mode for trace_clock */
7574 trace_seq_printf(s, "oldest event ts: %llu\n",
7575 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
7576
7577 trace_seq_printf(s, "now ts: %llu\n",
7578 ring_buffer_time_stamp(trace_buf->buffer, cpu));
7579 }
7580
7581 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
7582 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7583
7584 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
7585 trace_seq_printf(s, "read events: %ld\n", cnt);
7586
7587 count = simple_read_from_buffer(ubuf, count, ppos,
7588 s->buffer, trace_seq_used(s));
7589
7590 kfree(s);
7591
7592 return count;
7593}
7594
7595static const struct file_operations tracing_stats_fops = {
7596 .open = tracing_open_generic_tr,
7597 .read = tracing_stats_read,
7598 .llseek = generic_file_llseek,
7599 .release = tracing_release_generic_tr,
7600};
7601
7602#ifdef CONFIG_DYNAMIC_FTRACE
7603
7604static ssize_t
7605tracing_read_dyn_info(struct file *filp, char __user *ubuf,
7606 size_t cnt, loff_t *ppos)
7607{
7608 unsigned long *p = filp->private_data;
7609 char buf[64]; /* Not too big for a shallow stack */
7610 int r;
7611
7612 r = scnprintf(buf, 63, "%ld", *p);
7613 buf[r++] = '\n';
7614
7615 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7616}
7617
7618static const struct file_operations tracing_dyn_info_fops = {
7619 .open = tracing_open_generic,
7620 .read = tracing_read_dyn_info,
7621 .llseek = generic_file_llseek,
7622};
7623#endif /* CONFIG_DYNAMIC_FTRACE */
7624
7625#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7626static void
7627ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
7628 struct trace_array *tr, struct ftrace_probe_ops *ops,
7629 void *data)
7630{
7631 tracing_snapshot_instance(tr);
7632}
7633
7634static void
7635ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
7636 struct trace_array *tr, struct ftrace_probe_ops *ops,
7637 void *data)
7638{
7639 struct ftrace_func_mapper *mapper = data;
7640 long *count = NULL;
7641
7642 if (mapper)
7643 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7644
7645 if (count) {
7646
7647 if (*count <= 0)
7648 return;
7649
7650 (*count)--;
7651 }
7652
7653 tracing_snapshot_instance(tr);
7654}
7655
7656static int
7657ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7658 struct ftrace_probe_ops *ops, void *data)
7659{
7660 struct ftrace_func_mapper *mapper = data;
7661 long *count = NULL;
7662
7663 seq_printf(m, "%ps:", (void *)ip);
7664
7665 seq_puts(m, "snapshot");
7666
7667 if (mapper)
7668 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7669
7670 if (count)
7671 seq_printf(m, ":count=%ld\n", *count);
7672 else
7673 seq_puts(m, ":unlimited\n");
7674
7675 return 0;
7676}
7677
7678static int
7679ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
7680 unsigned long ip, void *init_data, void **data)
7681{
7682 struct ftrace_func_mapper *mapper = *data;
7683
7684 if (!mapper) {
7685 mapper = allocate_ftrace_func_mapper();
7686 if (!mapper)
7687 return -ENOMEM;
7688 *data = mapper;
7689 }
7690
7691 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
7692}
7693
7694static void
7695ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
7696 unsigned long ip, void *data)
7697{
7698 struct ftrace_func_mapper *mapper = data;
7699
7700 if (!ip) {
7701 if (!mapper)
7702 return;
7703 free_ftrace_func_mapper(mapper, NULL);
7704 return;
7705 }
7706
7707 ftrace_func_mapper_remove_ip(mapper, ip);
7708}
7709
7710static struct ftrace_probe_ops snapshot_probe_ops = {
7711 .func = ftrace_snapshot,
7712 .print = ftrace_snapshot_print,
7713};
7714
7715static struct ftrace_probe_ops snapshot_count_probe_ops = {
7716 .func = ftrace_count_snapshot,
7717 .print = ftrace_snapshot_print,
7718 .init = ftrace_snapshot_init,
7719 .free = ftrace_snapshot_free,
7720};
7721
7722static int
7723ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
7724 char *glob, char *cmd, char *param, int enable)
7725{
7726 struct ftrace_probe_ops *ops;
7727 void *count = (void *)-1;
7728 char *number;
7729 int ret;
7730
7731 if (!tr)
7732 return -ENODEV;
7733
7734 /* hash funcs only work with set_ftrace_filter */
7735 if (!enable)
7736 return -EINVAL;
7737
7738 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7739
7740 if (glob[0] == '!')
7741 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
7742
7743 if (!param)
7744 goto out_reg;
7745
7746 number = strsep(&param, ":");
7747
7748 if (!strlen(number))
7749 goto out_reg;
7750
7751 /*
7752 * We use the callback data field (which is a pointer)
7753 * as our counter.
7754 */
7755 ret = kstrtoul(number, 0, (unsigned long *)&count);
7756 if (ret)
7757 return ret;
7758
7759 out_reg:
7760 ret = tracing_alloc_snapshot_instance(tr);
7761 if (ret < 0)
7762 goto out;
7763
7764 ret = register_ftrace_function_probe(glob, tr, ops, count);
7765
7766 out:
7767 return ret < 0 ? ret : 0;
7768}
7769
7770static struct ftrace_func_command ftrace_snapshot_cmd = {
7771 .name = "snapshot",
7772 .func = ftrace_trace_snapshot_callback,
7773};
7774
7775static __init int register_snapshot_cmd(void)
7776{
7777 return register_ftrace_command(&ftrace_snapshot_cmd);
7778}
7779#else
7780static inline __init int register_snapshot_cmd(void) { return 0; }
7781#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
7782
7783static struct dentry *tracing_get_dentry(struct trace_array *tr)
7784{
7785 if (WARN_ON(!tr->dir))
7786 return ERR_PTR(-ENODEV);
7787
7788 /* Top directory uses NULL as the parent */
7789 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7790 return NULL;
7791
7792 /* All sub buffers have a descriptor */
7793 return tr->dir;
7794}
7795
7796static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
7797{
7798 struct dentry *d_tracer;
7799
7800 if (tr->percpu_dir)
7801 return tr->percpu_dir;
7802
7803 d_tracer = tracing_get_dentry(tr);
7804 if (IS_ERR(d_tracer))
7805 return NULL;
7806
7807 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
7808
7809 WARN_ONCE(!tr->percpu_dir,
7810 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
7811
7812 return tr->percpu_dir;
7813}
7814
7815static struct dentry *
7816trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7817 void *data, long cpu, const struct file_operations *fops)
7818{
7819 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7820
7821 if (ret) /* See tracing_get_cpu() */
7822 d_inode(ret)->i_cdev = (void *)(cpu + 1);
7823 return ret;
7824}
7825
7826static void
7827tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
7828{
7829 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
7830 struct dentry *d_cpu;
7831 char cpu_dir[30]; /* 30 characters should be more than enough */
7832
7833 if (!d_percpu)
7834 return;
7835
7836 snprintf(cpu_dir, 30, "cpu%ld", cpu);
7837 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
7838 if (!d_cpu) {
7839 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
7840 return;
7841 }
7842
7843 /* per cpu trace_pipe */
7844 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
7845 tr, cpu, &tracing_pipe_fops);
7846
7847 /* per cpu trace */
7848 trace_create_cpu_file("trace", 0644, d_cpu,
7849 tr, cpu, &tracing_fops);
7850
7851 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
7852 tr, cpu, &tracing_buffers_fops);
7853
7854 trace_create_cpu_file("stats", 0444, d_cpu,
7855 tr, cpu, &tracing_stats_fops);
7856
7857 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
7858 tr, cpu, &tracing_entries_fops);
7859
7860#ifdef CONFIG_TRACER_SNAPSHOT
7861 trace_create_cpu_file("snapshot", 0644, d_cpu,
7862 tr, cpu, &snapshot_fops);
7863
7864 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
7865 tr, cpu, &snapshot_raw_fops);
7866#endif
7867}
7868
7869#ifdef CONFIG_FTRACE_SELFTEST
7870/* Let selftest have access to static functions in this file */
7871#include "trace_selftest.c"
7872#endif
7873
7874static ssize_t
7875trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
7876 loff_t *ppos)
7877{
7878 struct trace_option_dentry *topt = filp->private_data;
7879 char *buf;
7880
7881 if (topt->flags->val & topt->opt->bit)
7882 buf = "1\n";
7883 else
7884 buf = "0\n";
7885
7886 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7887}
7888
7889static ssize_t
7890trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
7891 loff_t *ppos)
7892{
7893 struct trace_option_dentry *topt = filp->private_data;
7894 unsigned long val;
7895 int ret;
7896
7897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7898 if (ret)
7899 return ret;
7900
7901 if (val != 0 && val != 1)
7902 return -EINVAL;
7903
7904 if (!!(topt->flags->val & topt->opt->bit) != val) {
7905 mutex_lock(&trace_types_lock);
7906 ret = __set_tracer_option(topt->tr, topt->flags,
7907 topt->opt, !val);
7908 mutex_unlock(&trace_types_lock);
7909 if (ret)
7910 return ret;
7911 }
7912
7913 *ppos += cnt;
7914
7915 return cnt;
7916}
7917
7918
7919static const struct file_operations trace_options_fops = {
7920 .open = tracing_open_generic,
7921 .read = trace_options_read,
7922 .write = trace_options_write,
7923 .llseek = generic_file_llseek,
7924};
7925
7926/*
7927 * In order to pass in both the trace_array descriptor as well as the index
7928 * to the flag that the trace option file represents, the trace_array
7929 * has a character array of trace_flags_index[], which holds the index
7930 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
7931 * The address of this character array is passed to the flag option file
7932 * read/write callbacks.
7933 *
7934 * In order to extract both the index and the trace_array descriptor,
7935 * get_tr_index() uses the following algorithm.
7936 *
7937 * idx = *ptr;
7938 *
7939 * As the pointer itself contains the address of the index (remember
7940 * index[1] == 1).
7941 *
7942 * Then to get the trace_array descriptor, by subtracting that index
7943 * from the ptr, we get to the start of the index itself.
7944 *
7945 * ptr - idx == &index[0]
7946 *
7947 * Then a simple container_of() from that pointer gets us to the
7948 * trace_array descriptor.
7949 */
7950static void get_tr_index(void *data, struct trace_array **ptr,
7951 unsigned int *pindex)
7952{
7953 *pindex = *(unsigned char *)data;
7954
7955 *ptr = container_of(data - *pindex, struct trace_array,
7956 trace_flags_index);
7957}
7958
7959static ssize_t
7960trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
7961 loff_t *ppos)
7962{
7963 void *tr_index = filp->private_data;
7964 struct trace_array *tr;
7965 unsigned int index;
7966 char *buf;
7967
7968 get_tr_index(tr_index, &tr, &index);
7969
7970 if (tr->trace_flags & (1 << index))
7971 buf = "1\n";
7972 else
7973 buf = "0\n";
7974
7975 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7976}
7977
7978static ssize_t
7979trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
7980 loff_t *ppos)
7981{
7982 void *tr_index = filp->private_data;
7983 struct trace_array *tr;
7984 unsigned int index;
7985 unsigned long val;
7986 int ret;
7987
7988 get_tr_index(tr_index, &tr, &index);
7989
7990 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7991 if (ret)
7992 return ret;
7993
7994 if (val != 0 && val != 1)
7995 return -EINVAL;
7996
Olivier Deprez0e641232021-09-23 10:07:05 +02007997 mutex_lock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007998 mutex_lock(&trace_types_lock);
7999 ret = set_tracer_flag(tr, 1 << index, val);
8000 mutex_unlock(&trace_types_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02008001 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008002
8003 if (ret < 0)
8004 return ret;
8005
8006 *ppos += cnt;
8007
8008 return cnt;
8009}
8010
8011static const struct file_operations trace_options_core_fops = {
8012 .open = tracing_open_generic,
8013 .read = trace_options_core_read,
8014 .write = trace_options_core_write,
8015 .llseek = generic_file_llseek,
8016};
8017
8018struct dentry *trace_create_file(const char *name,
8019 umode_t mode,
8020 struct dentry *parent,
8021 void *data,
8022 const struct file_operations *fops)
8023{
8024 struct dentry *ret;
8025
8026 ret = tracefs_create_file(name, mode, parent, data, fops);
8027 if (!ret)
8028 pr_warn("Could not create tracefs '%s' entry\n", name);
8029
8030 return ret;
8031}
8032
8033
8034static struct dentry *trace_options_init_dentry(struct trace_array *tr)
8035{
8036 struct dentry *d_tracer;
8037
8038 if (tr->options)
8039 return tr->options;
8040
8041 d_tracer = tracing_get_dentry(tr);
8042 if (IS_ERR(d_tracer))
8043 return NULL;
8044
8045 tr->options = tracefs_create_dir("options", d_tracer);
8046 if (!tr->options) {
8047 pr_warn("Could not create tracefs directory 'options'\n");
8048 return NULL;
8049 }
8050
8051 return tr->options;
8052}
8053
8054static void
8055create_trace_option_file(struct trace_array *tr,
8056 struct trace_option_dentry *topt,
8057 struct tracer_flags *flags,
8058 struct tracer_opt *opt)
8059{
8060 struct dentry *t_options;
8061
8062 t_options = trace_options_init_dentry(tr);
8063 if (!t_options)
8064 return;
8065
8066 topt->flags = flags;
8067 topt->opt = opt;
8068 topt->tr = tr;
8069
8070 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
8071 &trace_options_fops);
8072
8073}
8074
8075static void
8076create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
8077{
8078 struct trace_option_dentry *topts;
8079 struct trace_options *tr_topts;
8080 struct tracer_flags *flags;
8081 struct tracer_opt *opts;
8082 int cnt;
8083 int i;
8084
8085 if (!tracer)
8086 return;
8087
8088 flags = tracer->flags;
8089
8090 if (!flags || !flags->opts)
8091 return;
8092
8093 /*
8094 * If this is an instance, only create flags for tracers
8095 * the instance may have.
8096 */
8097 if (!trace_ok_for_array(tracer, tr))
8098 return;
8099
8100 for (i = 0; i < tr->nr_topts; i++) {
8101 /* Make sure there's no duplicate flags. */
8102 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
8103 return;
8104 }
8105
8106 opts = flags->opts;
8107
8108 for (cnt = 0; opts[cnt].name; cnt++)
8109 ;
8110
8111 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
8112 if (!topts)
8113 return;
8114
8115 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
8116 GFP_KERNEL);
8117 if (!tr_topts) {
8118 kfree(topts);
8119 return;
8120 }
8121
8122 tr->topts = tr_topts;
8123 tr->topts[tr->nr_topts].tracer = tracer;
8124 tr->topts[tr->nr_topts].topts = topts;
8125 tr->nr_topts++;
8126
8127 for (cnt = 0; opts[cnt].name; cnt++) {
8128 create_trace_option_file(tr, &topts[cnt], flags,
8129 &opts[cnt]);
8130 WARN_ONCE(topts[cnt].entry == NULL,
8131 "Failed to create trace option: %s",
8132 opts[cnt].name);
8133 }
8134}
8135
8136static struct dentry *
8137create_trace_option_core_file(struct trace_array *tr,
8138 const char *option, long index)
8139{
8140 struct dentry *t_options;
8141
8142 t_options = trace_options_init_dentry(tr);
8143 if (!t_options)
8144 return NULL;
8145
8146 return trace_create_file(option, 0644, t_options,
8147 (void *)&tr->trace_flags_index[index],
8148 &trace_options_core_fops);
8149}
8150
8151static void create_trace_options_dir(struct trace_array *tr)
8152{
8153 struct dentry *t_options;
8154 bool top_level = tr == &global_trace;
8155 int i;
8156
8157 t_options = trace_options_init_dentry(tr);
8158 if (!t_options)
8159 return;
8160
8161 for (i = 0; trace_options[i]; i++) {
8162 if (top_level ||
8163 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
8164 create_trace_option_core_file(tr, trace_options[i], i);
8165 }
8166}
8167
8168static ssize_t
8169rb_simple_read(struct file *filp, char __user *ubuf,
8170 size_t cnt, loff_t *ppos)
8171{
8172 struct trace_array *tr = filp->private_data;
8173 char buf[64];
8174 int r;
8175
8176 r = tracer_tracing_is_on(tr);
8177 r = sprintf(buf, "%d\n", r);
8178
8179 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8180}
8181
8182static ssize_t
8183rb_simple_write(struct file *filp, const char __user *ubuf,
8184 size_t cnt, loff_t *ppos)
8185{
8186 struct trace_array *tr = filp->private_data;
8187 struct ring_buffer *buffer = tr->trace_buffer.buffer;
8188 unsigned long val;
8189 int ret;
8190
8191 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8192 if (ret)
8193 return ret;
8194
8195 if (buffer) {
8196 mutex_lock(&trace_types_lock);
8197 if (!!val == tracer_tracing_is_on(tr)) {
8198 val = 0; /* do nothing */
8199 } else if (val) {
8200 tracer_tracing_on(tr);
8201 if (tr->current_trace->start)
8202 tr->current_trace->start(tr);
8203 } else {
8204 tracer_tracing_off(tr);
8205 if (tr->current_trace->stop)
8206 tr->current_trace->stop(tr);
8207 }
8208 mutex_unlock(&trace_types_lock);
8209 }
8210
8211 (*ppos)++;
8212
8213 return cnt;
8214}
8215
8216static const struct file_operations rb_simple_fops = {
8217 .open = tracing_open_generic_tr,
8218 .read = rb_simple_read,
8219 .write = rb_simple_write,
8220 .release = tracing_release_generic_tr,
8221 .llseek = default_llseek,
8222};
8223
David Brazdil0f672f62019-12-10 10:32:29 +00008224static ssize_t
8225buffer_percent_read(struct file *filp, char __user *ubuf,
8226 size_t cnt, loff_t *ppos)
8227{
8228 struct trace_array *tr = filp->private_data;
8229 char buf[64];
8230 int r;
8231
8232 r = tr->buffer_percent;
8233 r = sprintf(buf, "%d\n", r);
8234
8235 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8236}
8237
8238static ssize_t
8239buffer_percent_write(struct file *filp, const char __user *ubuf,
8240 size_t cnt, loff_t *ppos)
8241{
8242 struct trace_array *tr = filp->private_data;
8243 unsigned long val;
8244 int ret;
8245
8246 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8247 if (ret)
8248 return ret;
8249
8250 if (val > 100)
8251 return -EINVAL;
8252
8253 if (!val)
8254 val = 1;
8255
8256 tr->buffer_percent = val;
8257
8258 (*ppos)++;
8259
8260 return cnt;
8261}
8262
8263static const struct file_operations buffer_percent_fops = {
8264 .open = tracing_open_generic_tr,
8265 .read = buffer_percent_read,
8266 .write = buffer_percent_write,
8267 .release = tracing_release_generic_tr,
8268 .llseek = default_llseek,
8269};
8270
8271static struct dentry *trace_instance_dir;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008272
8273static void
8274init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
8275
8276static int
8277allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
8278{
8279 enum ring_buffer_flags rb_flags;
8280
8281 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
8282
8283 buf->tr = tr;
8284
8285 buf->buffer = ring_buffer_alloc(size, rb_flags);
8286 if (!buf->buffer)
8287 return -ENOMEM;
8288
8289 buf->data = alloc_percpu(struct trace_array_cpu);
8290 if (!buf->data) {
8291 ring_buffer_free(buf->buffer);
8292 buf->buffer = NULL;
8293 return -ENOMEM;
8294 }
8295
8296 /* Allocate the first page for all buffers */
8297 set_buffer_entries(&tr->trace_buffer,
8298 ring_buffer_size(tr->trace_buffer.buffer, 0));
8299
8300 return 0;
8301}
8302
8303static int allocate_trace_buffers(struct trace_array *tr, int size)
8304{
8305 int ret;
8306
8307 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
8308 if (ret)
8309 return ret;
8310
8311#ifdef CONFIG_TRACER_MAX_TRACE
8312 ret = allocate_trace_buffer(tr, &tr->max_buffer,
8313 allocate_snapshot ? size : 1);
8314 if (WARN_ON(ret)) {
8315 ring_buffer_free(tr->trace_buffer.buffer);
8316 tr->trace_buffer.buffer = NULL;
8317 free_percpu(tr->trace_buffer.data);
8318 tr->trace_buffer.data = NULL;
8319 return -ENOMEM;
8320 }
8321 tr->allocated_snapshot = allocate_snapshot;
8322
8323 /*
8324 * Only the top level trace array gets its snapshot allocated
8325 * from the kernel command line.
8326 */
8327 allocate_snapshot = false;
8328#endif
Olivier Deprez0e641232021-09-23 10:07:05 +02008329
8330 /*
8331 * Because of some magic with the way alloc_percpu() works on
8332 * x86_64, we need to synchronize the pgd of all the tables,
8333 * otherwise the trace events that happen in x86_64 page fault
8334 * handlers can't cope with accessing the chance that a
8335 * alloc_percpu()'d memory might be touched in the page fault trace
8336 * event. Oh, and we need to audit all other alloc_percpu() and vmalloc()
8337 * calls in tracing, because something might get triggered within a
8338 * page fault trace event!
8339 */
8340 vmalloc_sync_mappings();
8341
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008342 return 0;
8343}
8344
8345static void free_trace_buffer(struct trace_buffer *buf)
8346{
8347 if (buf->buffer) {
8348 ring_buffer_free(buf->buffer);
8349 buf->buffer = NULL;
8350 free_percpu(buf->data);
8351 buf->data = NULL;
8352 }
8353}
8354
8355static void free_trace_buffers(struct trace_array *tr)
8356{
8357 if (!tr)
8358 return;
8359
8360 free_trace_buffer(&tr->trace_buffer);
8361
8362#ifdef CONFIG_TRACER_MAX_TRACE
8363 free_trace_buffer(&tr->max_buffer);
8364#endif
8365}
8366
8367static void init_trace_flags_index(struct trace_array *tr)
8368{
8369 int i;
8370
8371 /* Used by the trace options files */
8372 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8373 tr->trace_flags_index[i] = i;
8374}
8375
8376static void __update_tracer_options(struct trace_array *tr)
8377{
8378 struct tracer *t;
8379
8380 for (t = trace_types; t; t = t->next)
8381 add_tracer_options(tr, t);
8382}
8383
8384static void update_tracer_options(struct trace_array *tr)
8385{
8386 mutex_lock(&trace_types_lock);
8387 __update_tracer_options(tr);
8388 mutex_unlock(&trace_types_lock);
8389}
8390
David Brazdil0f672f62019-12-10 10:32:29 +00008391struct trace_array *trace_array_create(const char *name)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008392{
8393 struct trace_array *tr;
8394 int ret;
8395
8396 mutex_lock(&event_mutex);
8397 mutex_lock(&trace_types_lock);
8398
8399 ret = -EEXIST;
8400 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8401 if (tr->name && strcmp(tr->name, name) == 0)
8402 goto out_unlock;
8403 }
8404
8405 ret = -ENOMEM;
8406 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8407 if (!tr)
8408 goto out_unlock;
8409
8410 tr->name = kstrdup(name, GFP_KERNEL);
8411 if (!tr->name)
8412 goto out_free_tr;
8413
8414 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8415 goto out_free_tr;
8416
8417 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
8418
8419 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8420
8421 raw_spin_lock_init(&tr->start_lock);
8422
8423 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8424
8425 tr->current_trace = &nop_trace;
8426
8427 INIT_LIST_HEAD(&tr->systems);
8428 INIT_LIST_HEAD(&tr->events);
8429 INIT_LIST_HEAD(&tr->hist_vars);
David Brazdil0f672f62019-12-10 10:32:29 +00008430 INIT_LIST_HEAD(&tr->err_log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008431
8432 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
8433 goto out_free_tr;
8434
8435 tr->dir = tracefs_create_dir(name, trace_instance_dir);
8436 if (!tr->dir)
8437 goto out_free_tr;
8438
8439 ret = event_trace_add_tracer(tr->dir, tr);
8440 if (ret) {
8441 tracefs_remove_recursive(tr->dir);
8442 goto out_free_tr;
8443 }
8444
8445 ftrace_init_trace_array(tr);
8446
8447 init_tracer_tracefs(tr, tr->dir);
8448 init_trace_flags_index(tr);
8449 __update_tracer_options(tr);
8450
8451 list_add(&tr->list, &ftrace_trace_arrays);
8452
8453 mutex_unlock(&trace_types_lock);
8454 mutex_unlock(&event_mutex);
8455
David Brazdil0f672f62019-12-10 10:32:29 +00008456 return tr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008457
8458 out_free_tr:
8459 free_trace_buffers(tr);
8460 free_cpumask_var(tr->tracing_cpumask);
8461 kfree(tr->name);
8462 kfree(tr);
8463
8464 out_unlock:
8465 mutex_unlock(&trace_types_lock);
8466 mutex_unlock(&event_mutex);
8467
David Brazdil0f672f62019-12-10 10:32:29 +00008468 return ERR_PTR(ret);
8469}
8470EXPORT_SYMBOL_GPL(trace_array_create);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008471
David Brazdil0f672f62019-12-10 10:32:29 +00008472static int instance_mkdir(const char *name)
8473{
8474 return PTR_ERR_OR_ZERO(trace_array_create(name));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008475}
8476
David Brazdil0f672f62019-12-10 10:32:29 +00008477static int __remove_instance(struct trace_array *tr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008478{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008479 int i;
8480
Olivier Deprez0e641232021-09-23 10:07:05 +02008481 if (tr->ref || (tr->current_trace && tr->trace_ref))
David Brazdil0f672f62019-12-10 10:32:29 +00008482 return -EBUSY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008483
8484 list_del(&tr->list);
8485
8486 /* Disable all the flags that were enabled coming in */
8487 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8488 if ((1 << i) & ZEROED_TRACE_FLAGS)
8489 set_tracer_flag(tr, 1 << i, 0);
8490 }
8491
8492 tracing_set_nop(tr);
8493 clear_ftrace_function_probes(tr);
8494 event_trace_del_tracer(tr);
8495 ftrace_clear_pids(tr);
8496 ftrace_destroy_function_files(tr);
8497 tracefs_remove_recursive(tr->dir);
8498 free_trace_buffers(tr);
8499
8500 for (i = 0; i < tr->nr_topts; i++) {
8501 kfree(tr->topts[i].topts);
8502 }
8503 kfree(tr->topts);
8504
8505 free_cpumask_var(tr->tracing_cpumask);
8506 kfree(tr->name);
8507 kfree(tr);
David Brazdil0f672f62019-12-10 10:32:29 +00008508 tr = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008509
David Brazdil0f672f62019-12-10 10:32:29 +00008510 return 0;
8511}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008512
Olivier Deprez0e641232021-09-23 10:07:05 +02008513int trace_array_destroy(struct trace_array *this_tr)
David Brazdil0f672f62019-12-10 10:32:29 +00008514{
Olivier Deprez0e641232021-09-23 10:07:05 +02008515 struct trace_array *tr;
David Brazdil0f672f62019-12-10 10:32:29 +00008516 int ret;
8517
Olivier Deprez0e641232021-09-23 10:07:05 +02008518 if (!this_tr)
David Brazdil0f672f62019-12-10 10:32:29 +00008519 return -EINVAL;
8520
8521 mutex_lock(&event_mutex);
8522 mutex_lock(&trace_types_lock);
8523
Olivier Deprez0e641232021-09-23 10:07:05 +02008524 ret = -ENODEV;
8525
8526 /* Making sure trace array exists before destroying it. */
8527 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8528 if (tr == this_tr) {
8529 ret = __remove_instance(tr);
8530 break;
8531 }
8532 }
David Brazdil0f672f62019-12-10 10:32:29 +00008533
8534 mutex_unlock(&trace_types_lock);
8535 mutex_unlock(&event_mutex);
8536
8537 return ret;
8538}
8539EXPORT_SYMBOL_GPL(trace_array_destroy);
8540
8541static int instance_rmdir(const char *name)
8542{
8543 struct trace_array *tr;
8544 int ret;
8545
8546 mutex_lock(&event_mutex);
8547 mutex_lock(&trace_types_lock);
8548
8549 ret = -ENODEV;
8550 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8551 if (tr->name && strcmp(tr->name, name) == 0) {
8552 ret = __remove_instance(tr);
8553 break;
8554 }
8555 }
8556
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008557 mutex_unlock(&trace_types_lock);
8558 mutex_unlock(&event_mutex);
8559
8560 return ret;
8561}
8562
8563static __init void create_trace_instances(struct dentry *d_tracer)
8564{
8565 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8566 instance_mkdir,
8567 instance_rmdir);
8568 if (WARN_ON(!trace_instance_dir))
8569 return;
8570}
8571
8572static void
8573init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
8574{
8575 struct trace_event_file *file;
8576 int cpu;
8577
8578 trace_create_file("available_tracers", 0444, d_tracer,
8579 tr, &show_traces_fops);
8580
8581 trace_create_file("current_tracer", 0644, d_tracer,
8582 tr, &set_tracer_fops);
8583
8584 trace_create_file("tracing_cpumask", 0644, d_tracer,
8585 tr, &tracing_cpumask_fops);
8586
8587 trace_create_file("trace_options", 0644, d_tracer,
8588 tr, &tracing_iter_fops);
8589
8590 trace_create_file("trace", 0644, d_tracer,
8591 tr, &tracing_fops);
8592
8593 trace_create_file("trace_pipe", 0444, d_tracer,
8594 tr, &tracing_pipe_fops);
8595
8596 trace_create_file("buffer_size_kb", 0644, d_tracer,
8597 tr, &tracing_entries_fops);
8598
8599 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8600 tr, &tracing_total_entries_fops);
8601
8602 trace_create_file("free_buffer", 0200, d_tracer,
8603 tr, &tracing_free_buffer_fops);
8604
8605 trace_create_file("trace_marker", 0220, d_tracer,
8606 tr, &tracing_mark_fops);
8607
8608 file = __find_event_file(tr, "ftrace", "print");
8609 if (file && file->dir)
8610 trace_create_file("trigger", 0644, file->dir, file,
8611 &event_trigger_fops);
8612 tr->trace_marker_file = file;
8613
8614 trace_create_file("trace_marker_raw", 0220, d_tracer,
8615 tr, &tracing_mark_raw_fops);
8616
8617 trace_create_file("trace_clock", 0644, d_tracer, tr,
8618 &trace_clock_fops);
8619
8620 trace_create_file("tracing_on", 0644, d_tracer,
8621 tr, &rb_simple_fops);
8622
8623 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8624 &trace_time_stamp_mode_fops);
8625
David Brazdil0f672f62019-12-10 10:32:29 +00008626 tr->buffer_percent = 50;
8627
8628 trace_create_file("buffer_percent", 0444, d_tracer,
8629 tr, &buffer_percent_fops);
8630
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008631 create_trace_options_dir(tr);
8632
8633#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
8634 trace_create_file("tracing_max_latency", 0644, d_tracer,
8635 &tr->max_latency, &tracing_max_lat_fops);
8636#endif
8637
8638 if (ftrace_create_function_files(tr, d_tracer))
8639 WARN(1, "Could not allocate function filter files");
8640
8641#ifdef CONFIG_TRACER_SNAPSHOT
8642 trace_create_file("snapshot", 0644, d_tracer,
8643 tr, &snapshot_fops);
8644#endif
8645
David Brazdil0f672f62019-12-10 10:32:29 +00008646 trace_create_file("error_log", 0644, d_tracer,
8647 tr, &tracing_err_log_fops);
8648
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008649 for_each_tracing_cpu(cpu)
8650 tracing_init_tracefs_percpu(tr, cpu);
8651
8652 ftrace_init_tracefs(tr, d_tracer);
8653}
8654
8655static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
8656{
8657 struct vfsmount *mnt;
8658 struct file_system_type *type;
8659
8660 /*
8661 * To maintain backward compatibility for tools that mount
8662 * debugfs to get to the tracing facility, tracefs is automatically
8663 * mounted to the debugfs/tracing directory.
8664 */
8665 type = get_fs_type("tracefs");
8666 if (!type)
8667 return NULL;
8668 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
8669 put_filesystem(type);
8670 if (IS_ERR(mnt))
8671 return NULL;
8672 mntget(mnt);
8673
8674 return mnt;
8675}
8676
8677/**
8678 * tracing_init_dentry - initialize top level trace array
8679 *
8680 * This is called when creating files or directories in the tracing
8681 * directory. It is called via fs_initcall() by any of the boot up code
8682 * and expects to return the dentry of the top level tracing directory.
8683 */
8684struct dentry *tracing_init_dentry(void)
8685{
8686 struct trace_array *tr = &global_trace;
8687
Olivier Deprez0e641232021-09-23 10:07:05 +02008688 if (security_locked_down(LOCKDOWN_TRACEFS)) {
8689 pr_warning("Tracing disabled due to lockdown\n");
8690 return ERR_PTR(-EPERM);
8691 }
8692
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008693 /* The top level trace array uses NULL as parent */
8694 if (tr->dir)
8695 return NULL;
8696
8697 if (WARN_ON(!tracefs_initialized()) ||
8698 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8699 WARN_ON(!debugfs_initialized())))
8700 return ERR_PTR(-ENODEV);
8701
8702 /*
8703 * As there may still be users that expect the tracing
8704 * files to exist in debugfs/tracing, we must automount
8705 * the tracefs file system there, so older tools still
8706 * work with the newer kerenl.
8707 */
8708 tr->dir = debugfs_create_automount("tracing", NULL,
8709 trace_automount, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008710
8711 return NULL;
8712}
8713
8714extern struct trace_eval_map *__start_ftrace_eval_maps[];
8715extern struct trace_eval_map *__stop_ftrace_eval_maps[];
8716
8717static void __init trace_eval_init(void)
8718{
8719 int len;
8720
8721 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
8722 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
8723}
8724
8725#ifdef CONFIG_MODULES
8726static void trace_module_add_evals(struct module *mod)
8727{
8728 if (!mod->num_trace_evals)
8729 return;
8730
8731 /*
8732 * Modules with bad taint do not have events created, do
8733 * not bother with enums either.
8734 */
8735 if (trace_module_has_bad_taint(mod))
8736 return;
8737
8738 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
8739}
8740
8741#ifdef CONFIG_TRACE_EVAL_MAP_FILE
8742static void trace_module_remove_evals(struct module *mod)
8743{
8744 union trace_eval_map_item *map;
8745 union trace_eval_map_item **last = &trace_eval_maps;
8746
8747 if (!mod->num_trace_evals)
8748 return;
8749
8750 mutex_lock(&trace_eval_mutex);
8751
8752 map = trace_eval_maps;
8753
8754 while (map) {
8755 if (map->head.mod == mod)
8756 break;
8757 map = trace_eval_jmp_to_tail(map);
8758 last = &map->tail.next;
8759 map = map->tail.next;
8760 }
8761 if (!map)
8762 goto out;
8763
8764 *last = trace_eval_jmp_to_tail(map)->tail.next;
8765 kfree(map);
8766 out:
8767 mutex_unlock(&trace_eval_mutex);
8768}
8769#else
8770static inline void trace_module_remove_evals(struct module *mod) { }
8771#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
8772
8773static int trace_module_notify(struct notifier_block *self,
8774 unsigned long val, void *data)
8775{
8776 struct module *mod = data;
8777
8778 switch (val) {
8779 case MODULE_STATE_COMING:
8780 trace_module_add_evals(mod);
8781 break;
8782 case MODULE_STATE_GOING:
8783 trace_module_remove_evals(mod);
8784 break;
8785 }
8786
8787 return 0;
8788}
8789
8790static struct notifier_block trace_module_nb = {
8791 .notifier_call = trace_module_notify,
8792 .priority = 0,
8793};
8794#endif /* CONFIG_MODULES */
8795
8796static __init int tracer_init_tracefs(void)
8797{
8798 struct dentry *d_tracer;
8799
8800 trace_access_lock_init();
8801
8802 d_tracer = tracing_init_dentry();
8803 if (IS_ERR(d_tracer))
8804 return 0;
8805
8806 event_trace_init();
8807
8808 init_tracer_tracefs(&global_trace, d_tracer);
8809 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
8810
8811 trace_create_file("tracing_thresh", 0644, d_tracer,
8812 &global_trace, &tracing_thresh_fops);
8813
8814 trace_create_file("README", 0444, d_tracer,
8815 NULL, &tracing_readme_fops);
8816
8817 trace_create_file("saved_cmdlines", 0444, d_tracer,
8818 NULL, &tracing_saved_cmdlines_fops);
8819
8820 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8821 NULL, &tracing_saved_cmdlines_size_fops);
8822
8823 trace_create_file("saved_tgids", 0444, d_tracer,
8824 NULL, &tracing_saved_tgids_fops);
8825
8826 trace_eval_init();
8827
8828 trace_create_eval_file(d_tracer);
8829
8830#ifdef CONFIG_MODULES
8831 register_module_notifier(&trace_module_nb);
8832#endif
8833
8834#ifdef CONFIG_DYNAMIC_FTRACE
8835 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8836 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
8837#endif
8838
8839 create_trace_instances(d_tracer);
8840
8841 update_tracer_options(&global_trace);
8842
8843 return 0;
8844}
8845
8846static int trace_panic_handler(struct notifier_block *this,
8847 unsigned long event, void *unused)
8848{
8849 if (ftrace_dump_on_oops)
8850 ftrace_dump(ftrace_dump_on_oops);
8851 return NOTIFY_OK;
8852}
8853
8854static struct notifier_block trace_panic_notifier = {
8855 .notifier_call = trace_panic_handler,
8856 .next = NULL,
8857 .priority = 150 /* priority: INT_MAX >= x >= 0 */
8858};
8859
8860static int trace_die_handler(struct notifier_block *self,
8861 unsigned long val,
8862 void *data)
8863{
8864 switch (val) {
8865 case DIE_OOPS:
8866 if (ftrace_dump_on_oops)
8867 ftrace_dump(ftrace_dump_on_oops);
8868 break;
8869 default:
8870 break;
8871 }
8872 return NOTIFY_OK;
8873}
8874
8875static struct notifier_block trace_die_notifier = {
8876 .notifier_call = trace_die_handler,
8877 .priority = 200
8878};
8879
8880/*
8881 * printk is set to max of 1024, we really don't need it that big.
8882 * Nothing should be printing 1000 characters anyway.
8883 */
8884#define TRACE_MAX_PRINT 1000
8885
8886/*
8887 * Define here KERN_TRACE so that we have one place to modify
8888 * it if we decide to change what log level the ftrace dump
8889 * should be at.
8890 */
8891#define KERN_TRACE KERN_EMERG
8892
8893void
8894trace_printk_seq(struct trace_seq *s)
8895{
8896 /* Probably should print a warning here. */
8897 if (s->seq.len >= TRACE_MAX_PRINT)
8898 s->seq.len = TRACE_MAX_PRINT;
8899
8900 /*
8901 * More paranoid code. Although the buffer size is set to
8902 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
8903 * an extra layer of protection.
8904 */
8905 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
8906 s->seq.len = s->seq.size - 1;
8907
8908 /* should be zero ended, but we are paranoid. */
8909 s->buffer[s->seq.len] = 0;
8910
8911 printk(KERN_TRACE "%s", s->buffer);
8912
8913 trace_seq_init(s);
8914}
8915
8916void trace_init_global_iter(struct trace_iterator *iter)
8917{
8918 iter->tr = &global_trace;
8919 iter->trace = iter->tr->current_trace;
8920 iter->cpu_file = RING_BUFFER_ALL_CPUS;
8921 iter->trace_buffer = &global_trace.trace_buffer;
8922
8923 if (iter->trace && iter->trace->open)
8924 iter->trace->open(iter);
8925
8926 /* Annotate start of buffers if we had overruns */
8927 if (ring_buffer_overruns(iter->trace_buffer->buffer))
8928 iter->iter_flags |= TRACE_FILE_ANNOTATE;
8929
8930 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
8931 if (trace_clocks[iter->tr->clock_id].in_ns)
8932 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
8933}
8934
8935void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
8936{
8937 /* use static because iter can be a bit big for the stack */
8938 static struct trace_iterator iter;
8939 static atomic_t dump_running;
8940 struct trace_array *tr = &global_trace;
8941 unsigned int old_userobj;
8942 unsigned long flags;
8943 int cnt = 0, cpu;
8944
8945 /* Only allow one dump user at a time. */
8946 if (atomic_inc_return(&dump_running) != 1) {
8947 atomic_dec(&dump_running);
8948 return;
8949 }
8950
8951 /*
8952 * Always turn off tracing when we dump.
8953 * We don't need to show trace output of what happens
8954 * between multiple crashes.
8955 *
8956 * If the user does a sysrq-z, then they can re-enable
8957 * tracing with echo 1 > tracing_on.
8958 */
8959 tracing_off();
8960
8961 local_irq_save(flags);
8962 printk_nmi_direct_enter();
8963
8964 /* Simulate the iterator */
8965 trace_init_global_iter(&iter);
8966
8967 for_each_tracing_cpu(cpu) {
8968 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
8969 }
8970
8971 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
8972
8973 /* don't look at user memory in panic mode */
8974 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
8975
8976 switch (oops_dump_mode) {
8977 case DUMP_ALL:
8978 iter.cpu_file = RING_BUFFER_ALL_CPUS;
8979 break;
8980 case DUMP_ORIG:
8981 iter.cpu_file = raw_smp_processor_id();
8982 break;
8983 case DUMP_NONE:
8984 goto out_enable;
8985 default:
8986 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
8987 iter.cpu_file = RING_BUFFER_ALL_CPUS;
8988 }
8989
8990 printk(KERN_TRACE "Dumping ftrace buffer:\n");
8991
8992 /* Did function tracer already get disabled? */
8993 if (ftrace_is_dead()) {
8994 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
8995 printk("# MAY BE MISSING FUNCTION EVENTS\n");
8996 }
8997
8998 /*
8999 * We need to stop all tracing on all CPUS to read the
9000 * the next buffer. This is a bit expensive, but is
9001 * not done often. We fill all what we can read,
9002 * and then release the locks again.
9003 */
9004
9005 while (!trace_empty(&iter)) {
9006
9007 if (!cnt)
9008 printk(KERN_TRACE "---------------------------------\n");
9009
9010 cnt++;
9011
David Brazdil0f672f62019-12-10 10:32:29 +00009012 trace_iterator_reset(&iter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009013 iter.iter_flags |= TRACE_FILE_LAT_FMT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009014
9015 if (trace_find_next_entry_inc(&iter) != NULL) {
9016 int ret;
9017
9018 ret = print_trace_line(&iter);
9019 if (ret != TRACE_TYPE_NO_CONSUME)
9020 trace_consume(&iter);
9021 }
9022 touch_nmi_watchdog();
9023
9024 trace_printk_seq(&iter.seq);
9025 }
9026
9027 if (!cnt)
9028 printk(KERN_TRACE " (ftrace buffer empty)\n");
9029 else
9030 printk(KERN_TRACE "---------------------------------\n");
9031
9032 out_enable:
9033 tr->trace_flags |= old_userobj;
9034
9035 for_each_tracing_cpu(cpu) {
9036 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
9037 }
9038 atomic_dec(&dump_running);
9039 printk_nmi_direct_exit();
9040 local_irq_restore(flags);
9041}
9042EXPORT_SYMBOL_GPL(ftrace_dump);
9043
9044int trace_run_command(const char *buf, int (*createfn)(int, char **))
9045{
9046 char **argv;
9047 int argc, ret;
9048
9049 argc = 0;
9050 ret = 0;
9051 argv = argv_split(GFP_KERNEL, buf, &argc);
9052 if (!argv)
9053 return -ENOMEM;
9054
9055 if (argc)
9056 ret = createfn(argc, argv);
9057
9058 argv_free(argv);
9059
9060 return ret;
9061}
9062
9063#define WRITE_BUFSIZE 4096
9064
9065ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
9066 size_t count, loff_t *ppos,
9067 int (*createfn)(int, char **))
9068{
9069 char *kbuf, *buf, *tmp;
9070 int ret = 0;
9071 size_t done = 0;
9072 size_t size;
9073
9074 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
9075 if (!kbuf)
9076 return -ENOMEM;
9077
9078 while (done < count) {
9079 size = count - done;
9080
9081 if (size >= WRITE_BUFSIZE)
9082 size = WRITE_BUFSIZE - 1;
9083
9084 if (copy_from_user(kbuf, buffer + done, size)) {
9085 ret = -EFAULT;
9086 goto out;
9087 }
9088 kbuf[size] = '\0';
9089 buf = kbuf;
9090 do {
9091 tmp = strchr(buf, '\n');
9092 if (tmp) {
9093 *tmp = '\0';
9094 size = tmp - buf + 1;
9095 } else {
9096 size = strlen(buf);
9097 if (done + size < count) {
9098 if (buf != kbuf)
9099 break;
9100 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
9101 pr_warn("Line length is too long: Should be less than %d\n",
9102 WRITE_BUFSIZE - 2);
9103 ret = -EINVAL;
9104 goto out;
9105 }
9106 }
9107 done += size;
9108
9109 /* Remove comments */
9110 tmp = strchr(buf, '#');
9111
9112 if (tmp)
9113 *tmp = '\0';
9114
9115 ret = trace_run_command(buf, createfn);
9116 if (ret)
9117 goto out;
9118 buf += size;
9119
9120 } while (done < count);
9121 }
9122 ret = done;
9123
9124out:
9125 kfree(kbuf);
9126
9127 return ret;
9128}
9129
9130__init static int tracer_alloc_buffers(void)
9131{
9132 int ring_buf_size;
9133 int ret = -ENOMEM;
9134
Olivier Deprez0e641232021-09-23 10:07:05 +02009135
9136 if (security_locked_down(LOCKDOWN_TRACEFS)) {
9137 pr_warning("Tracing disabled due to lockdown\n");
9138 return -EPERM;
9139 }
9140
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009141 /*
9142 * Make sure we don't accidently add more trace options
9143 * than we have bits for.
9144 */
9145 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
9146
9147 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
9148 goto out;
9149
9150 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
9151 goto out_free_buffer_mask;
9152
9153 /* Only allocate trace_printk buffers if a trace_printk exists */
Olivier Deprez0e641232021-09-23 10:07:05 +02009154 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009155 /* Must be called before global_trace.buffer is allocated */
9156 trace_printk_init_buffers();
9157
9158 /* To save memory, keep the ring buffer size to its minimum */
9159 if (ring_buffer_expanded)
9160 ring_buf_size = trace_buf_size;
9161 else
9162 ring_buf_size = 1;
9163
9164 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
9165 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
9166
9167 raw_spin_lock_init(&global_trace.start_lock);
9168
9169 /*
9170 * The prepare callbacks allocates some memory for the ring buffer. We
9171 * don't free the buffer if the if the CPU goes down. If we were to free
9172 * the buffer, then the user would lose any trace that was in the
9173 * buffer. The memory will be removed once the "instance" is removed.
9174 */
9175 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
9176 "trace/RB:preapre", trace_rb_cpu_prepare,
9177 NULL);
9178 if (ret < 0)
9179 goto out_free_cpumask;
9180 /* Used for event triggers */
9181 ret = -ENOMEM;
9182 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
9183 if (!temp_buffer)
9184 goto out_rm_hp_state;
9185
9186 if (trace_create_savedcmd() < 0)
9187 goto out_free_temp_buffer;
9188
9189 /* TODO: make the number of buffers hot pluggable with CPUS */
9190 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
9191 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
9192 WARN_ON(1);
9193 goto out_free_savedcmd;
9194 }
9195
9196 if (global_trace.buffer_disabled)
9197 tracing_off();
9198
9199 if (trace_boot_clock) {
9200 ret = tracing_set_clock(&global_trace, trace_boot_clock);
9201 if (ret < 0)
9202 pr_warn("Trace clock %s not defined, going back to default\n",
9203 trace_boot_clock);
9204 }
9205
9206 /*
9207 * register_tracer() might reference current_trace, so it
9208 * needs to be set before we register anything. This is
9209 * just a bootstrap of current_trace anyway.
9210 */
9211 global_trace.current_trace = &nop_trace;
9212
9213 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9214
9215 ftrace_init_global_array_ops(&global_trace);
9216
9217 init_trace_flags_index(&global_trace);
9218
9219 register_tracer(&nop_trace);
9220
9221 /* Function tracing may start here (via kernel command line) */
9222 init_function_trace();
9223
9224 /* All seems OK, enable tracing */
9225 tracing_disabled = 0;
9226
9227 atomic_notifier_chain_register(&panic_notifier_list,
9228 &trace_panic_notifier);
9229
9230 register_die_notifier(&trace_die_notifier);
9231
9232 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
9233
9234 INIT_LIST_HEAD(&global_trace.systems);
9235 INIT_LIST_HEAD(&global_trace.events);
9236 INIT_LIST_HEAD(&global_trace.hist_vars);
David Brazdil0f672f62019-12-10 10:32:29 +00009237 INIT_LIST_HEAD(&global_trace.err_log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009238 list_add(&global_trace.list, &ftrace_trace_arrays);
9239
9240 apply_trace_boot_options();
9241
9242 register_snapshot_cmd();
9243
9244 return 0;
9245
9246out_free_savedcmd:
9247 free_saved_cmdlines_buffer(savedcmd);
9248out_free_temp_buffer:
9249 ring_buffer_free(temp_buffer);
9250out_rm_hp_state:
9251 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
9252out_free_cpumask:
9253 free_cpumask_var(global_trace.tracing_cpumask);
9254out_free_buffer_mask:
9255 free_cpumask_var(tracing_buffer_mask);
9256out:
9257 return ret;
9258}
9259
9260void __init early_trace_init(void)
9261{
9262 if (tracepoint_printk) {
9263 tracepoint_print_iter =
9264 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
9265 if (WARN_ON(!tracepoint_print_iter))
9266 tracepoint_printk = 0;
9267 else
9268 static_key_enable(&tracepoint_printk_key.key);
9269 }
9270 tracer_alloc_buffers();
9271}
9272
9273void __init trace_init(void)
9274{
9275 trace_event_init();
9276}
9277
9278__init static int clear_boot_tracer(void)
9279{
9280 /*
9281 * The default tracer at boot buffer is an init section.
9282 * This function is called in lateinit. If we did not
9283 * find the boot tracer, then clear it out, to prevent
9284 * later registration from accessing the buffer that is
9285 * about to be freed.
9286 */
9287 if (!default_bootup_tracer)
9288 return 0;
9289
9290 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
9291 default_bootup_tracer);
9292 default_bootup_tracer = NULL;
9293
9294 return 0;
9295}
9296
9297fs_initcall(tracer_init_tracefs);
9298late_initcall_sync(clear_boot_tracer);
9299
9300#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
9301__init static int tracing_set_default_clock(void)
9302{
9303 /* sched_clock_stable() is determined in late_initcall */
9304 if (!trace_boot_clock && !sched_clock_stable()) {
Olivier Deprez0e641232021-09-23 10:07:05 +02009305 if (security_locked_down(LOCKDOWN_TRACEFS)) {
9306 pr_warn("Can not set tracing clock due to lockdown\n");
9307 return -EPERM;
9308 }
9309
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009310 printk(KERN_WARNING
9311 "Unstable clock detected, switching default tracing clock to \"global\"\n"
9312 "If you want to keep using the local clock, then add:\n"
9313 " \"trace_clock=local\"\n"
9314 "on the kernel command line\n");
9315 tracing_set_clock(&global_trace, "global");
9316 }
9317
9318 return 0;
9319}
9320late_initcall_sync(tracing_set_default_clock);
9321#endif