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