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