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