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