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