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