blob: 8d144fd94aa88f51bf0d9e1c8972f2b7f1326a76 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002/*
3 * ring buffer based function tracer
4 *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006 * 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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020014 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050015#include <linux/ring_buffer.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020016#include <generated/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050017#include <linux/stacktrace.h>
18#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#include <linux/kallsyms.h>
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -040020#include <linux/security.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020021#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040022#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050023#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/debugfs.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050025#include <linux/tracefs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020026#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020027#include <linux/hardirq.h>
28#include <linux/linkage.h>
29#include <linux/uaccess.h>
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -040030#include <linux/vmalloc.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020031#include <linux/ftrace.h>
32#include <linux/module.h>
33#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050034#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040035#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010036#include <linux/string.h>
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -050037#include <linux/mount.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080038#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020040#include <linux/ctype.h>
41#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020042#include <linux/poll.h>
Steven Rostedtb892e5c2012-03-01 22:06:48 -050043#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020044#include <linux/fs.h>
Chunyan Zhang478409d2016-11-21 15:57:18 +080045#include <linux/trace.h>
Chris Wilson3fd49c92018-03-30 16:01:31 +010046#include <linux/sched/clock.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060047#include <linux/sched/rt.h>
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +020048#include <linux/fsnotify.h>
49#include <linux/irq_work.h>
50#include <linux/workqueue.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020051
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020052#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050053#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020054
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010055/*
Steven Rostedt73c51622009-03-11 13:42:01 -040056 * On boot up, the ring buffer is set to the minimum size, so that
57 * we do not waste memory on systems that are not using tracing.
58 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -050059bool ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040060
61/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010062 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010063 * A selftest will lurk into the ring-buffer to count the
64 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010065 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010066 * at the same time, giving false positive or negative results.
67 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010068static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010069
Steven Rostedtb2821ae2009-02-02 21:38:32 -050070/*
71 * If a tracer is running, we do not want to run SELFTEST.
72 */
Li Zefan020e5f82009-07-01 10:47:05 +080073bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050074
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050075/* Pipe tracepoints to printk */
76struct trace_iterator *tracepoint_print_iter;
77int tracepoint_printk;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -050078static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -050079
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010080/* For tracers that don't implement custom flags */
81static struct tracer_opt dummy_tracer_opt[] = {
82 { }
83};
84
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -050085static int
86dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010087{
88 return 0;
89}
Steven Rostedt0f048702008-11-05 16:05:44 -050090
91/*
Steven Rostedt7ffbd482012-10-11 12:14:25 -040092 * To prevent the comm cache from being overwritten when no
93 * tracing is active, only save the comm when a trace event
94 * occurred.
95 */
Joel Fernandesd914ba32017-06-26 19:01:55 -070096static DEFINE_PER_CPU(bool, trace_taskinfo_save);
Steven Rostedt7ffbd482012-10-11 12:14:25 -040097
98/*
Steven Rostedt0f048702008-11-05 16:05:44 -050099 * Kill all tracing for good (never come back).
100 * It is initialized to 1 but will turn to zero if the initialization
101 * of the tracer is successful. But that is the only place that sets
102 * this back to zero.
103 */
Hannes Eder4fd27352009-02-10 19:44:12 +0100104static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -0500105
Jason Wessel955b61e2010-08-05 09:22:23 -0500106cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200107
Steven Rostedt944ac422008-10-23 19:26:08 -0400108/*
109 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
110 *
111 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
112 * is set, then ftrace_dump is called. This will output the contents
113 * of the ftrace buffers to the console. This is very useful for
114 * capturing traces that lead to crashes and outputing it to a
115 * serial console.
116 *
117 * It is default off, but you can enable it with either specifying
118 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200119 * /proc/sys/kernel/ftrace_dump_on_oops
120 * Set 1 if you want to dump buffers of all CPUs
121 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400122 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200123
124enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400125
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400126/* When set, tracing will stop when a WARN*() is hit */
127int __disable_trace_on_warning;
128
Jeremy Linton681bec02017-05-31 16:56:53 -0500129#ifdef CONFIG_TRACE_EVAL_MAP_FILE
130/* Map of enums to their values, for "eval_map" file */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500131struct trace_eval_map_head {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400132 struct module *mod;
133 unsigned long length;
134};
135
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500136union trace_eval_map_item;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400137
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500138struct trace_eval_map_tail {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400139 /*
140 * "end" is first and points to NULL as it must be different
Jeremy Linton00f4b652017-05-31 16:56:43 -0500141 * than "mod" or "eval_string"
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400142 */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500143 union trace_eval_map_item *next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400144 const char *end; /* points to NULL */
145};
146
Jeremy Linton1793ed92017-05-31 16:56:46 -0500147static DEFINE_MUTEX(trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400148
149/*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500150 * The trace_eval_maps are saved in an array with two extra elements,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400151 * one at the beginning, and one at the end. The beginning item contains
152 * the count of the saved maps (head.length), and the module they
153 * belong to if not built in (head.mod). The ending item contains a
Jeremy Linton681bec02017-05-31 16:56:53 -0500154 * pointer to the next array of saved eval_map items.
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400155 */
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500156union trace_eval_map_item {
Jeremy Linton00f4b652017-05-31 16:56:43 -0500157 struct trace_eval_map map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500158 struct trace_eval_map_head head;
159 struct trace_eval_map_tail tail;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400160};
161
Jeremy Linton23bf8cb2017-05-31 16:56:45 -0500162static union trace_eval_map_item *trace_eval_maps;
Jeremy Linton681bec02017-05-31 16:56:53 -0500163#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -0400164
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900165int tracing_set_tracer(struct trace_array *tr, const char *buf);
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500166static void ftrace_trace_userstack(struct trace_buffer *buffer,
Thomas Gleixnerc438f142019-04-25 11:45:15 +0200167 unsigned long flags, int pc);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500168
Li Zefanee6c2c12009-09-18 14:06:47 +0800169#define MAX_TRACER_SIZE 100
170static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500171static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100172
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500173static bool allocate_snapshot;
174
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200175static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100176{
Chen Gang67012ab2013-04-08 12:06:44 +0800177 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500178 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400179 /* We are using ftrace early, expand it */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500180 ring_buffer_expanded = true;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100181 return 1;
182}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200183__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100184
Steven Rostedt944ac422008-10-23 19:26:08 -0400185static int __init set_ftrace_dump_on_oops(char *str)
186{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200187 if (*str++ != '=' || !*str) {
188 ftrace_dump_on_oops = DUMP_ALL;
189 return 1;
190 }
191
192 if (!strcmp("orig_cpu", str)) {
193 ftrace_dump_on_oops = DUMP_ORIG;
194 return 1;
195 }
196
197 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400198}
199__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200200
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400201static int __init stop_trace_on_warning(char *str)
202{
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200203 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
204 __disable_trace_on_warning = 1;
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400205 return 1;
206}
Luis Claudio R. Goncalves933ff9f2014-11-12 21:14:00 -0200207__setup("traceoff_on_warning", stop_trace_on_warning);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400208
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400209static int __init boot_alloc_snapshot(char *str)
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500210{
211 allocate_snapshot = true;
212 /* We also need the main ring buffer expanded */
213 ring_buffer_expanded = true;
214 return 1;
215}
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -0400216__setup("alloc_snapshot", boot_alloc_snapshot);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -0500217
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400218
219static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400220
221static int __init set_trace_boot_options(char *str)
222{
Chen Gang67012ab2013-04-08 12:06:44 +0800223 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
Steven Rostedt7bcfaf52012-11-01 22:56:07 -0400224 return 0;
225}
226__setup("trace_options=", set_trace_boot_options);
227
Steven Rostedte1e232c2014-02-10 23:38:46 -0500228static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
229static char *trace_boot_clock __initdata;
230
231static int __init set_trace_boot_clock(char *str)
232{
233 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
234 trace_boot_clock = trace_boot_clock_buf;
235 return 0;
236}
237__setup("trace_clock=", set_trace_boot_clock);
238
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -0500239static int __init set_tracepoint_printk(char *str)
240{
241 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
242 tracepoint_printk = 1;
243 return 1;
244}
245__setup("tp_printk", set_tracepoint_printk);
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -0400246
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100247unsigned long long ns2usecs(u64 nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200248{
249 nsec += 500;
250 do_div(nsec, 1000);
251 return nsec;
252}
253
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400254/* trace_flags holds trace_options default values */
255#define TRACE_DEFAULT_FLAGS \
256 (FUNCTION_DEFAULT_FLAGS | \
257 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
259 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
260 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
261
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400262/* trace_options that are only supported by global_trace */
263#define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
264 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
265
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -0400266/* trace_flags that are default zero for instances */
267#define ZEROED_TRACE_FLAGS \
Namhyung Kim1e104862017-04-17 11:44:28 +0900268 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -0400269
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200270/*
Joel Fernandes67d04bb2017-02-16 20:10:58 -0800271 * The global_trace is the descriptor that holds the top-level tracing
272 * buffers for the live tracing.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200273 */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400274static struct trace_array global_trace = {
275 .trace_flags = TRACE_DEFAULT_FLAGS,
276};
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200277
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400278LIST_HEAD(ftrace_trace_arrays);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200279
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400280int trace_array_get(struct trace_array *this_tr)
281{
282 struct trace_array *tr;
283 int ret = -ENODEV;
284
285 mutex_lock(&trace_types_lock);
286 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
287 if (tr == this_tr) {
288 tr->ref++;
289 ret = 0;
290 break;
291 }
292 }
293 mutex_unlock(&trace_types_lock);
294
295 return ret;
296}
297
298static void __trace_array_put(struct trace_array *this_tr)
299{
300 WARN_ON(!this_tr->ref);
301 this_tr->ref--;
302}
303
Divya Indi28879782019-11-20 11:08:38 -0800304/**
305 * trace_array_put - Decrement the reference counter for this trace array.
306 *
307 * NOTE: Use this when we no longer need the trace array returned by
308 * trace_array_get_by_name(). This ensures the trace array can be later
309 * destroyed.
310 *
311 */
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400312void trace_array_put(struct trace_array *this_tr)
313{
Divya Indi28879782019-11-20 11:08:38 -0800314 if (!this_tr)
315 return;
316
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400317 mutex_lock(&trace_types_lock);
318 __trace_array_put(this_tr);
319 mutex_unlock(&trace_types_lock);
320}
Divya Indi28879782019-11-20 11:08:38 -0800321EXPORT_SYMBOL_GPL(trace_array_put);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -0400322
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -0400323int tracing_check_open_get_tr(struct trace_array *tr)
324{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -0400325 int ret;
326
327 ret = security_locked_down(LOCKDOWN_TRACEFS);
328 if (ret)
329 return ret;
330
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -0400331 if (tracing_disabled)
332 return -ENODEV;
333
334 if (tr && trace_array_get(tr) < 0)
335 return -ENODEV;
336
337 return 0;
338}
339
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400340int call_filter_check_discard(struct trace_event_call *call, void *rec,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500341 struct trace_buffer *buffer,
Tom Zanussif306cc82013-10-24 08:34:17 -0500342 struct ring_buffer_event *event)
343{
344 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
345 !filter_match_preds(call->filter, rec)) {
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400346 __trace_event_discard_commit(buffer, event);
Tom Zanussif306cc82013-10-24 08:34:17 -0500347 return 1;
348 }
349
350 return 0;
351}
Tom Zanussieb02ce02009-04-08 03:15:54 -0500352
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400353void trace_free_pid_list(struct trace_pid_list *pid_list)
354{
355 vfree(pid_list->pids);
356 kfree(pid_list);
357}
358
Steven Rostedtd8275c42016-04-14 12:15:22 -0400359/**
360 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
361 * @filtered_pids: The list of pids to check
362 * @search_pid: The PID to find in @filtered_pids
363 *
364 * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
365 */
366bool
367trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
368{
369 /*
370 * If pid_max changed after filtered_pids was created, we
371 * by default ignore all pids greater than the previous pid_max.
372 */
373 if (search_pid >= filtered_pids->pid_max)
374 return false;
375
376 return test_bit(search_pid, filtered_pids->pids);
377}
378
379/**
380 * trace_ignore_this_task - should a task be ignored for tracing
381 * @filtered_pids: The list of pids to check
382 * @task: The task that should be ignored if not filtered
383 *
384 * Checks if @task should be traced or not from @filtered_pids.
385 * Returns true if @task should *NOT* be traced.
386 * Returns false if @task should be traced.
387 */
388bool
389trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
390{
391 /*
392 * Return false, because if filtered_pids does not exist,
393 * all pids are good to trace.
394 */
395 if (!filtered_pids)
396 return false;
397
398 return !trace_find_filtered_pid(filtered_pids, task->pid);
399}
400
401/**
Matthias Kaehlckef08367b2019-05-23 12:26:28 -0700402 * trace_filter_add_remove_task - Add or remove a task from a pid_list
Steven Rostedtd8275c42016-04-14 12:15:22 -0400403 * @pid_list: The list to modify
404 * @self: The current task for fork or NULL for exit
405 * @task: The task to add or remove
406 *
407 * If adding a task, if @self is defined, the task is only added if @self
408 * is also included in @pid_list. This happens on fork and tasks should
409 * only be added when the parent is listed. If @self is NULL, then the
410 * @task pid will be removed from the list, which would happen on exit
411 * of a task.
412 */
413void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
414 struct task_struct *self,
415 struct task_struct *task)
416{
417 if (!pid_list)
418 return;
419
420 /* For forks, we only add if the forking task is listed */
421 if (self) {
422 if (!trace_find_filtered_pid(pid_list, self->pid))
423 return;
424 }
425
426 /* Sorry, but we don't support pid_max changing after setting */
427 if (task->pid >= pid_list->pid_max)
428 return;
429
430 /* "self" is set for forks, and NULL for exits */
431 if (self)
432 set_bit(task->pid, pid_list->pids);
433 else
434 clear_bit(task->pid, pid_list->pids);
435}
436
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -0400437/**
438 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
439 * @pid_list: The pid list to show
440 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
441 * @pos: The position of the file
442 *
443 * This is used by the seq_file "next" operation to iterate the pids
444 * listed in a trace_pid_list structure.
445 *
446 * Returns the pid+1 as we want to display pid of zero, but NULL would
447 * stop the iteration.
448 */
449void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
450{
451 unsigned long pid = (unsigned long)v;
452
453 (*pos)++;
454
455 /* pid already is +1 of the actual prevous bit */
456 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
457
458 /* Return pid + 1 to allow zero to be represented */
459 if (pid < pid_list->pid_max)
460 return (void *)(pid + 1);
461
462 return NULL;
463}
464
465/**
466 * trace_pid_start - Used for seq_file to start reading pid lists
467 * @pid_list: The pid list to show
468 * @pos: The position of the file
469 *
470 * This is used by seq_file "start" operation to start the iteration
471 * of listing pids.
472 *
473 * Returns the pid+1 as we want to display pid of zero, but NULL would
474 * stop the iteration.
475 */
476void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
477{
478 unsigned long pid;
479 loff_t l = 0;
480
481 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
482 if (pid >= pid_list->pid_max)
483 return NULL;
484
485 /* Return pid + 1 so that zero can be the exit value */
486 for (pid++; pid && l < *pos;
487 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
488 ;
489 return (void *)pid;
490}
491
492/**
493 * trace_pid_show - show the current pid in seq_file processing
494 * @m: The seq_file structure to write into
495 * @v: A void pointer of the pid (+1) value to display
496 *
497 * Can be directly used by seq_file operations to display the current
498 * pid value.
499 */
500int trace_pid_show(struct seq_file *m, void *v)
501{
502 unsigned long pid = (unsigned long)v - 1;
503
504 seq_printf(m, "%lu\n", pid);
505 return 0;
506}
507
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400508/* 128 should be much more than enough */
509#define PID_BUF_SIZE 127
510
511int trace_pid_write(struct trace_pid_list *filtered_pids,
512 struct trace_pid_list **new_pid_list,
513 const char __user *ubuf, size_t cnt)
514{
515 struct trace_pid_list *pid_list;
516 struct trace_parser parser;
517 unsigned long val;
518 int nr_pids = 0;
519 ssize_t read = 0;
520 ssize_t ret = 0;
521 loff_t pos;
522 pid_t pid;
523
524 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
525 return -ENOMEM;
526
527 /*
528 * Always recreate a new array. The write is an all or nothing
529 * operation. Always create a new array when adding new pids by
530 * the user. If the operation fails, then the current list is
531 * not modified.
532 */
533 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
Wenwen Wang91862cc2019-04-19 21:22:59 -0500534 if (!pid_list) {
535 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400536 return -ENOMEM;
Wenwen Wang91862cc2019-04-19 21:22:59 -0500537 }
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400538
539 pid_list->pid_max = READ_ONCE(pid_max);
540
541 /* Only truncating will shrink pid_max */
542 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
543 pid_list->pid_max = filtered_pids->pid_max;
544
545 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
546 if (!pid_list->pids) {
Wenwen Wang91862cc2019-04-19 21:22:59 -0500547 trace_parser_put(&parser);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400548 kfree(pid_list);
549 return -ENOMEM;
550 }
551
552 if (filtered_pids) {
553 /* copy the current bits to the new max */
Wei Yongjun67f20b02016-07-04 15:10:04 +0000554 for_each_set_bit(pid, filtered_pids->pids,
555 filtered_pids->pid_max) {
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400556 set_bit(pid, pid_list->pids);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400557 nr_pids++;
558 }
559 }
560
561 while (cnt > 0) {
562
563 pos = 0;
564
565 ret = trace_get_user(&parser, ubuf, cnt, &pos);
566 if (ret < 0 || !trace_parser_loaded(&parser))
567 break;
568
569 read += ret;
570 ubuf += ret;
571 cnt -= ret;
572
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400573 ret = -EINVAL;
574 if (kstrtoul(parser.buffer, 0, &val))
575 break;
576 if (val >= pid_list->pid_max)
577 break;
578
579 pid = (pid_t)val;
580
581 set_bit(pid, pid_list->pids);
582 nr_pids++;
583
584 trace_parser_clear(&parser);
585 ret = 0;
586 }
587 trace_parser_put(&parser);
588
589 if (ret < 0) {
590 trace_free_pid_list(pid_list);
591 return ret;
592 }
593
594 if (!nr_pids) {
595 /* Cleared the list of pids */
596 trace_free_pid_list(pid_list);
597 read = ret;
598 pid_list = NULL;
599 }
600
601 *new_pid_list = pid_list;
602
603 return read;
604}
605
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500606static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
Steven Rostedt37886f62009-03-17 17:22:06 -0400607{
608 u64 ts;
609
610 /* Early boot up does not have a buffer yet */
Alexander Z Lam94571582013-08-02 18:36:16 -0700611 if (!buf->buffer)
Steven Rostedt37886f62009-03-17 17:22:06 -0400612 return trace_clock_local();
613
Alexander Z Lam94571582013-08-02 18:36:16 -0700614 ts = ring_buffer_time_stamp(buf->buffer, cpu);
615 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
Steven Rostedt37886f62009-03-17 17:22:06 -0400616
617 return ts;
618}
619
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100620u64 ftrace_now(int cpu)
Alexander Z Lam94571582013-08-02 18:36:16 -0700621{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500622 return buffer_ftrace_now(&global_trace.array_buffer, cpu);
Alexander Z Lam94571582013-08-02 18:36:16 -0700623}
624
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400625/**
626 * tracing_is_enabled - Show if global_trace has been disabled
627 *
628 * Shows if the global trace has been enabled or not. It uses the
629 * mirror flag "buffer_disabled" to be used in fast paths such as for
630 * the irqsoff tracer. But it may be inaccurate due to races. If you
631 * need to know the accurate state, use tracing_is_on() which is a little
632 * slower, but accurate.
633 */
Steven Rostedt90369902008-11-05 16:05:44 -0500634int tracing_is_enabled(void)
635{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400636 /*
637 * For quick access (irqsoff uses this in fast path), just
638 * return the mirror variable of the state of the ring buffer.
639 * It's a little racy, but we don't really care.
640 */
641 smp_rmb();
642 return !global_trace.buffer_disabled;
Steven Rostedt90369902008-11-05 16:05:44 -0500643}
644
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200645/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400646 * trace_buf_size is the size in bytes that is allocated
647 * for a buffer. Note, the number of bytes is always rounded
648 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400649 *
650 * This number is purposely set to a low number of 16384.
651 * If the dump on oops happens, it will be much appreciated
652 * to not have to wait for all that output. Anyway this can be
653 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200654 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400655#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400656
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400657static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200658
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200659/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200661
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200662/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200663 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200664 */
Alexander Z Lama8227412013-07-01 19:37:54 -0700665DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200666
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800667/*
668 * serialize the access of the ring buffer
669 *
670 * ring buffer serializes readers, but it is low level protection.
671 * The validity of the events (which returns by ring_buffer_peek() ..etc)
672 * are not protected by ring buffer.
673 *
674 * The content of events may become garbage if we allow other process consumes
675 * these events concurrently:
676 * A) the page of the consumed events may become a normal page
677 * (not reader page) in ring buffer, and this page will be rewrited
678 * by events producer.
679 * B) The page of the consumed events may become a page for splice_read,
680 * and this page will be returned to system.
681 *
682 * These primitives allow multi process access to different cpu ring buffer
683 * concurrently.
684 *
685 * These primitives don't distinguish read-only and read-consume access.
686 * Multi read-only access are also serialized.
687 */
688
689#ifdef CONFIG_SMP
690static DECLARE_RWSEM(all_cpu_access_lock);
691static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
692
693static inline void trace_access_lock(int cpu)
694{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500695 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800696 /* gain it for accessing the whole ring buffer. */
697 down_write(&all_cpu_access_lock);
698 } else {
699 /* gain it for accessing a cpu ring buffer. */
700
Steven Rostedtae3b5092013-01-23 15:22:59 -0500701 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800702 down_read(&all_cpu_access_lock);
703
704 /* Secondly block other access to this @cpu ring buffer. */
705 mutex_lock(&per_cpu(cpu_access_lock, cpu));
706 }
707}
708
709static inline void trace_access_unlock(int cpu)
710{
Steven Rostedtae3b5092013-01-23 15:22:59 -0500711 if (cpu == RING_BUFFER_ALL_CPUS) {
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800712 up_write(&all_cpu_access_lock);
713 } else {
714 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
715 up_read(&all_cpu_access_lock);
716 }
717}
718
719static inline void trace_access_lock_init(void)
720{
721 int cpu;
722
723 for_each_possible_cpu(cpu)
724 mutex_init(&per_cpu(cpu_access_lock, cpu));
725}
726
727#else
728
729static DEFINE_MUTEX(access_lock);
730
731static inline void trace_access_lock(int cpu)
732{
733 (void)cpu;
734 mutex_lock(&access_lock);
735}
736
737static inline void trace_access_unlock(int cpu)
738{
739 (void)cpu;
740 mutex_unlock(&access_lock);
741}
742
743static inline void trace_access_lock_init(void)
744{
745}
746
747#endif
748
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400749#ifdef CONFIG_STACKTRACE
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500750static void __ftrace_trace_stack(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400751 unsigned long flags,
752 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400753static inline void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500754 struct trace_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400755 unsigned long flags,
756 int skip, int pc, struct pt_regs *regs);
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400757
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400758#else
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500759static inline void __ftrace_trace_stack(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400760 unsigned long flags,
761 int skip, int pc, struct pt_regs *regs)
762{
763}
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400764static inline void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500765 struct trace_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -0400766 unsigned long flags,
767 int skip, int pc, struct pt_regs *regs)
Steven Rostedt (Red Hat)ca475e82015-09-28 09:41:11 -0400768{
769}
770
Steven Rostedt (Red Hat)d78a4612015-09-25 13:30:47 -0400771#endif
772
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500773static __always_inline void
774trace_event_setup(struct ring_buffer_event *event,
775 int type, unsigned long flags, int pc)
776{
777 struct trace_entry *ent = ring_buffer_event_data(event);
778
Cong Wang46710f32019-05-25 09:57:59 -0700779 tracing_generic_entry_update(ent, type, flags, pc);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500780}
781
782static __always_inline struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500783__trace_buffer_lock_reserve(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500784 int type,
785 unsigned long len,
786 unsigned long flags, int pc)
787{
788 struct ring_buffer_event *event;
789
790 event = ring_buffer_lock_reserve(buffer, len);
791 if (event != NULL)
792 trace_event_setup(event, type, flags, pc);
793
794 return event;
795}
796
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -0400797void tracer_tracing_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400798{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500799 if (tr->array_buffer.buffer)
800 ring_buffer_record_on(tr->array_buffer.buffer);
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400801 /*
802 * This flag is looked at when buffers haven't been allocated
803 * yet, or by some tracers (like irqsoff), that just want to
804 * know if the ring buffer has been disabled, but it can handle
805 * races of where it gets disabled but we still do a record.
806 * As the check is in the fast path of the tracers, it is more
807 * important to be fast than accurate.
808 */
809 tr->buffer_disabled = 0;
810 /* Make the flag seen by readers */
811 smp_wmb();
812}
813
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200814/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500815 * tracing_on - enable tracing buffers
816 *
817 * This function enables tracing buffers that may have been
818 * disabled with tracing_off.
819 */
820void tracing_on(void)
821{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400822 tracer_tracing_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -0500823}
824EXPORT_SYMBOL_GPL(tracing_on);
825
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500826
827static __always_inline void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500828__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500829{
Joel Fernandesd914ba32017-06-26 19:01:55 -0700830 __this_cpu_write(trace_taskinfo_save, true);
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500831
832 /* If this is the temp buffer, we need to commit fully */
833 if (this_cpu_read(trace_buffered_event) == event) {
834 /* Length is in event->array[0] */
835 ring_buffer_write(buffer, event->array[0], &event->array[1]);
836 /* Release the temp buffer */
837 this_cpu_dec(trace_buffered_event_cnt);
838 } else
839 ring_buffer_unlock_commit(buffer, event);
840}
841
Steven Rostedt499e5472012-02-22 15:50:28 -0500842/**
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500843 * __trace_puts - write a constant string into the trace buffer.
844 * @ip: The address of the caller
845 * @str: The constant string to write
846 * @size: The size of the string.
847 */
848int __trace_puts(unsigned long ip, const char *str, int size)
849{
850 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500851 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500852 struct print_entry *entry;
853 unsigned long irq_flags;
854 int alloc;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800855 int pc;
856
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400857 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800858 return 0;
859
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800860 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500861
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500862 if (unlikely(tracing_selftest_running || tracing_disabled))
863 return 0;
864
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500865 alloc = sizeof(*entry) + size + 2; /* possible \n added */
866
867 local_save_flags(irq_flags);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500868 buffer = global_trace.array_buffer.buffer;
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500869 ring_buffer_nest_start(buffer);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500870 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
871 irq_flags, pc);
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500872 if (!event) {
873 size = 0;
874 goto out;
875 }
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500876
877 entry = ring_buffer_event_data(event);
878 entry->ip = ip;
879
880 memcpy(&entry->buf, str, size);
881
882 /* Add a newline if necessary */
883 if (entry->buf[size - 1] != '\n') {
884 entry->buf[size] = '\n';
885 entry->buf[size + 1] = '\0';
886 } else
887 entry->buf[size] = '\0';
888
889 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400890 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500891 out:
892 ring_buffer_nest_end(buffer);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500893 return size;
894}
895EXPORT_SYMBOL_GPL(__trace_puts);
896
897/**
898 * __trace_bputs - write the pointer to a constant string into trace buffer
899 * @ip: The address of the caller
900 * @str: The constant string to write to the buffer to
901 */
902int __trace_bputs(unsigned long ip, const char *str)
903{
904 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500905 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500906 struct bputs_entry *entry;
907 unsigned long irq_flags;
908 int size = sizeof(struct bputs_entry);
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500909 int ret = 0;
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800910 int pc;
911
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400912 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800913 return 0;
914
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800915 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500916
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500917 if (unlikely(tracing_selftest_running || tracing_disabled))
918 return 0;
919
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500920 local_save_flags(irq_flags);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500921 buffer = global_trace.array_buffer.buffer;
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500922
923 ring_buffer_nest_start(buffer);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500924 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
925 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500926 if (!event)
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500927 goto out;
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500928
929 entry = ring_buffer_event_data(event);
930 entry->ip = ip;
931 entry->str = str;
932
933 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400934 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500935
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -0500936 ret = 1;
937 out:
938 ring_buffer_nest_end(buffer);
939 return ret;
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500940}
941EXPORT_SYMBOL_GPL(__trace_bputs);
942
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500943#ifdef CONFIG_TRACER_SNAPSHOT
Tom Zanussia35873a2019-02-13 17:42:45 -0600944void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500945{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500946 struct tracer *tracer = tr->current_trace;
947 unsigned long flags;
948
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500949 if (in_nmi()) {
950 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
951 internal_trace_puts("*** snapshot is being ignored ***\n");
952 return;
953 }
954
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500955 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500956 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
957 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500958 tracing_off();
959 return;
960 }
961
962 /* Note, snapshot can not be used when the tracer uses it */
963 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500964 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
965 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500966 return;
967 }
968
969 local_irq_save(flags);
Tom Zanussia35873a2019-02-13 17:42:45 -0600970 update_max_tr(tr, current, smp_processor_id(), cond_data);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500971 local_irq_restore(flags);
972}
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400973
Tom Zanussia35873a2019-02-13 17:42:45 -0600974void tracing_snapshot_instance(struct trace_array *tr)
975{
976 tracing_snapshot_instance_cond(tr, NULL);
977}
978
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400979/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +0800980 * tracing_snapshot - take a snapshot of the current buffer.
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400981 *
982 * This causes a swap between the snapshot buffer and the current live
983 * tracing buffer. You can use this to take snapshots of the live
984 * trace when some condition is triggered, but continue to trace.
985 *
986 * Note, make sure to allocate the snapshot with either
987 * a tracing_snapshot_alloc(), or by doing it manually
988 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
989 *
990 * If the snapshot buffer is not allocated, it will stop tracing.
991 * Basically making a permanent snapshot.
992 */
993void tracing_snapshot(void)
994{
995 struct trace_array *tr = &global_trace;
996
997 tracing_snapshot_instance(tr);
998}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500999EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001000
Tom Zanussia35873a2019-02-13 17:42:45 -06001001/**
1002 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
1003 * @tr: The tracing instance to snapshot
1004 * @cond_data: The data to be tested conditionally, and possibly saved
1005 *
1006 * This is the same as tracing_snapshot() except that the snapshot is
1007 * conditional - the snapshot will only happen if the
1008 * cond_snapshot.update() implementation receiving the cond_data
1009 * returns true, which means that the trace array's cond_snapshot
1010 * update() operation used the cond_data to determine whether the
1011 * snapshot should be taken, and if it was, presumably saved it along
1012 * with the snapshot.
1013 */
1014void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1015{
1016 tracing_snapshot_instance_cond(tr, cond_data);
1017}
1018EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1019
1020/**
1021 * tracing_snapshot_cond_data - get the user data associated with a snapshot
1022 * @tr: The tracing instance
1023 *
1024 * When the user enables a conditional snapshot using
1025 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1026 * with the snapshot. This accessor is used to retrieve it.
1027 *
1028 * Should not be called from cond_snapshot.update(), since it takes
1029 * the tr->max_lock lock, which the code calling
1030 * cond_snapshot.update() has already done.
1031 *
1032 * Returns the cond_data associated with the trace array's snapshot.
1033 */
1034void *tracing_cond_snapshot_data(struct trace_array *tr)
1035{
1036 void *cond_data = NULL;
1037
1038 arch_spin_lock(&tr->max_lock);
1039
1040 if (tr->cond_snapshot)
1041 cond_data = tr->cond_snapshot->cond_data;
1042
1043 arch_spin_unlock(&tr->max_lock);
1044
1045 return cond_data;
1046}
1047EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1048
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001049static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1050 struct array_buffer *size_buf, int cpu_id);
1051static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001052
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001053int tracing_alloc_snapshot_instance(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001054{
1055 int ret;
1056
1057 if (!tr->allocated_snapshot) {
1058
1059 /* allocate spare buffer */
1060 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001061 &tr->array_buffer, RING_BUFFER_ALL_CPUS);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001062 if (ret < 0)
1063 return ret;
1064
1065 tr->allocated_snapshot = true;
1066 }
1067
1068 return 0;
1069}
1070
Fabian Frederickad1438a2014-04-17 21:44:42 +02001071static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001072{
1073 /*
1074 * We don't free the ring buffer. instead, resize it because
1075 * The max_tr ring buffer has some state (e.g. ring->clock) and
1076 * we want preserve it.
1077 */
1078 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1079 set_buffer_entries(&tr->max_buffer, 1);
1080 tracing_reset_online_cpus(&tr->max_buffer);
1081 tr->allocated_snapshot = false;
1082}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001083
1084/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001085 * tracing_alloc_snapshot - allocate snapshot buffer.
1086 *
1087 * This only allocates the snapshot buffer if it isn't already
1088 * allocated - it doesn't also take a snapshot.
1089 *
1090 * This is meant to be used in cases where the snapshot buffer needs
1091 * to be set up for events that can't sleep but need to be able to
1092 * trigger a snapshot.
1093 */
1094int tracing_alloc_snapshot(void)
1095{
1096 struct trace_array *tr = &global_trace;
1097 int ret;
1098
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001099 ret = tracing_alloc_snapshot_instance(tr);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001100 WARN_ON(ret < 0);
1101
1102 return ret;
1103}
1104EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1105
1106/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001107 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001108 *
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001109 * This is similar to tracing_snapshot(), but it will allocate the
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001110 * snapshot buffer if it isn't already allocated. Use this only
1111 * where it is safe to sleep, as the allocation may sleep.
1112 *
1113 * This causes a swap between the snapshot buffer and the current live
1114 * tracing buffer. You can use this to take snapshots of the live
1115 * trace when some condition is triggered, but continue to trace.
1116 */
1117void tracing_snapshot_alloc(void)
1118{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001119 int ret;
1120
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001121 ret = tracing_alloc_snapshot();
1122 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001123 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001124
1125 tracing_snapshot();
1126}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001127EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001128
1129/**
1130 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1131 * @tr: The tracing instance
1132 * @cond_data: User data to associate with the snapshot
1133 * @update: Implementation of the cond_snapshot update function
1134 *
1135 * Check whether the conditional snapshot for the given instance has
1136 * already been enabled, or if the current tracer is already using a
1137 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1138 * save the cond_data and update function inside.
1139 *
1140 * Returns 0 if successful, error otherwise.
1141 */
1142int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1143 cond_update_fn_t update)
1144{
1145 struct cond_snapshot *cond_snapshot;
1146 int ret = 0;
1147
1148 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1149 if (!cond_snapshot)
1150 return -ENOMEM;
1151
1152 cond_snapshot->cond_data = cond_data;
1153 cond_snapshot->update = update;
1154
1155 mutex_lock(&trace_types_lock);
1156
1157 ret = tracing_alloc_snapshot_instance(tr);
1158 if (ret)
1159 goto fail_unlock;
1160
1161 if (tr->current_trace->use_max_tr) {
1162 ret = -EBUSY;
1163 goto fail_unlock;
1164 }
1165
Steven Rostedt (VMware)1c347a92019-02-14 18:45:21 -05001166 /*
1167 * The cond_snapshot can only change to NULL without the
1168 * trace_types_lock. We don't care if we race with it going
1169 * to NULL, but we want to make sure that it's not set to
1170 * something other than NULL when we get here, which we can
1171 * do safely with only holding the trace_types_lock and not
1172 * having to take the max_lock.
1173 */
Tom Zanussia35873a2019-02-13 17:42:45 -06001174 if (tr->cond_snapshot) {
1175 ret = -EBUSY;
1176 goto fail_unlock;
1177 }
1178
1179 arch_spin_lock(&tr->max_lock);
1180 tr->cond_snapshot = cond_snapshot;
1181 arch_spin_unlock(&tr->max_lock);
1182
1183 mutex_unlock(&trace_types_lock);
1184
1185 return ret;
1186
1187 fail_unlock:
1188 mutex_unlock(&trace_types_lock);
1189 kfree(cond_snapshot);
1190 return ret;
1191}
1192EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1193
1194/**
1195 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1196 * @tr: The tracing instance
1197 *
1198 * Check whether the conditional snapshot for the given instance is
1199 * enabled; if so, free the cond_snapshot associated with it,
1200 * otherwise return -EINVAL.
1201 *
1202 * Returns 0 if successful, error otherwise.
1203 */
1204int tracing_snapshot_cond_disable(struct trace_array *tr)
1205{
1206 int ret = 0;
1207
1208 arch_spin_lock(&tr->max_lock);
1209
1210 if (!tr->cond_snapshot)
1211 ret = -EINVAL;
1212 else {
1213 kfree(tr->cond_snapshot);
1214 tr->cond_snapshot = NULL;
1215 }
1216
1217 arch_spin_unlock(&tr->max_lock);
1218
1219 return ret;
1220}
1221EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001222#else
1223void tracing_snapshot(void)
1224{
1225 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1226}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001227EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussia35873a2019-02-13 17:42:45 -06001228void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1229{
1230 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1231}
1232EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001233int tracing_alloc_snapshot(void)
1234{
1235 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1236 return -ENODEV;
1237}
1238EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001239void tracing_snapshot_alloc(void)
1240{
1241 /* Give warning */
1242 tracing_snapshot();
1243}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001244EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001245void *tracing_cond_snapshot_data(struct trace_array *tr)
1246{
1247 return NULL;
1248}
1249EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1250int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1251{
1252 return -ENODEV;
1253}
1254EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1255int tracing_snapshot_cond_disable(struct trace_array *tr)
1256{
1257 return false;
1258}
1259EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001260#endif /* CONFIG_TRACER_SNAPSHOT */
1261
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -04001262void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001263{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001264 if (tr->array_buffer.buffer)
1265 ring_buffer_record_off(tr->array_buffer.buffer);
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001266 /*
1267 * This flag is looked at when buffers haven't been allocated
1268 * yet, or by some tracers (like irqsoff), that just want to
1269 * know if the ring buffer has been disabled, but it can handle
1270 * races of where it gets disabled but we still do a record.
1271 * As the check is in the fast path of the tracers, it is more
1272 * important to be fast than accurate.
1273 */
1274 tr->buffer_disabled = 1;
1275 /* Make the flag seen by readers */
1276 smp_wmb();
1277}
1278
Steven Rostedt499e5472012-02-22 15:50:28 -05001279/**
1280 * tracing_off - turn off tracing buffers
1281 *
1282 * This function stops the tracing buffers from recording data.
1283 * It does not disable any overhead the tracers themselves may
1284 * be causing. This function simply causes all recording to
1285 * the ring buffers to fail.
1286 */
1287void tracing_off(void)
1288{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001289 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001290}
1291EXPORT_SYMBOL_GPL(tracing_off);
1292
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001293void disable_trace_on_warning(void)
1294{
1295 if (__disable_trace_on_warning)
1296 tracing_off();
1297}
1298
Steven Rostedt499e5472012-02-22 15:50:28 -05001299/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001300 * tracer_tracing_is_on - show real state of ring buffer enabled
1301 * @tr : the trace array to know if ring buffer is enabled
1302 *
1303 * Shows real state of the ring buffer if it is enabled or not.
1304 */
Steven Rostedt (VMware)ec573502018-08-01 16:08:57 -04001305bool tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001306{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001307 if (tr->array_buffer.buffer)
1308 return ring_buffer_record_is_on(tr->array_buffer.buffer);
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001309 return !tr->buffer_disabled;
1310}
1311
Steven Rostedt499e5472012-02-22 15:50:28 -05001312/**
1313 * tracing_is_on - show state of ring buffers enabled
1314 */
1315int tracing_is_on(void)
1316{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001317 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001318}
1319EXPORT_SYMBOL_GPL(tracing_is_on);
1320
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001321static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001323 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001324
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325 if (!str)
1326 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001327 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001328 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001329 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001330 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001331 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332 return 1;
1333}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001334__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001335
Tim Bird0e950172010-02-25 15:36:43 -08001336static int __init set_tracing_thresh(char *str)
1337{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001338 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001339 int ret;
1340
1341 if (!str)
1342 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001343 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001344 if (ret < 0)
1345 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001346 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001347 return 1;
1348}
1349__setup("tracing_thresh=", set_tracing_thresh);
1350
Steven Rostedt57f50be2008-05-12 21:20:44 +02001351unsigned long nsecs_to_usecs(unsigned long nsecs)
1352{
1353 return nsecs / 1000;
1354}
1355
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001356/*
1357 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001358 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001359 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001360 * of strings in the order that the evals (enum) were defined.
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001361 */
1362#undef C
1363#define C(a, b) b
1364
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001365/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001366static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001367 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001368 NULL
1369};
1370
Zhaolei5079f322009-08-25 16:12:56 +08001371static struct {
1372 u64 (*func)(void);
1373 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001374 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001375} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001376 { trace_clock_local, "local", 1 },
1377 { trace_clock_global, "global", 1 },
1378 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001379 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001380 { trace_clock, "perf", 1 },
1381 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001382 { ktime_get_raw_fast_ns, "mono_raw", 1 },
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001383 { ktime_get_boot_fast_ns, "boot", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001384 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001385};
1386
Tom Zanussi860f9f62018-01-15 20:51:48 -06001387bool trace_clock_in_ns(struct trace_array *tr)
1388{
1389 if (trace_clocks[tr->clock_id].in_ns)
1390 return true;
1391
1392 return false;
1393}
1394
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001395/*
1396 * trace_parser_get_init - gets the buffer for trace parser
1397 */
1398int trace_parser_get_init(struct trace_parser *parser, int size)
1399{
1400 memset(parser, 0, sizeof(*parser));
1401
1402 parser->buffer = kmalloc(size, GFP_KERNEL);
1403 if (!parser->buffer)
1404 return 1;
1405
1406 parser->size = size;
1407 return 0;
1408}
1409
1410/*
1411 * trace_parser_put - frees the buffer for trace parser
1412 */
1413void trace_parser_put(struct trace_parser *parser)
1414{
1415 kfree(parser->buffer);
Steven Rostedt (VMware)0e684b62017-02-02 17:58:18 -05001416 parser->buffer = NULL;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001417}
1418
1419/*
1420 * trace_get_user - reads the user input string separated by space
1421 * (matched by isspace(ch))
1422 *
1423 * For each string found the 'struct trace_parser' is updated,
1424 * and the function returns.
1425 *
1426 * Returns number of bytes read.
1427 *
1428 * See kernel/trace/trace.h for 'struct trace_parser' details.
1429 */
1430int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1431 size_t cnt, loff_t *ppos)
1432{
1433 char ch;
1434 size_t read = 0;
1435 ssize_t ret;
1436
1437 if (!*ppos)
1438 trace_parser_clear(parser);
1439
1440 ret = get_user(ch, ubuf++);
1441 if (ret)
1442 goto out;
1443
1444 read++;
1445 cnt--;
1446
1447 /*
1448 * The parser is not finished with the last write,
1449 * continue reading the user input without skipping spaces.
1450 */
1451 if (!parser->cont) {
1452 /* skip white space */
1453 while (cnt && isspace(ch)) {
1454 ret = get_user(ch, ubuf++);
1455 if (ret)
1456 goto out;
1457 read++;
1458 cnt--;
1459 }
1460
Changbin Du76638d92018-01-16 17:02:29 +08001461 parser->idx = 0;
1462
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001463 /* only spaces were written */
Changbin Du921a7ac2018-01-16 17:02:28 +08001464 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001465 *ppos += read;
1466 ret = read;
1467 goto out;
1468 }
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001469 }
1470
1471 /* read the non-space input */
Changbin Du921a7ac2018-01-16 17:02:28 +08001472 while (cnt && !isspace(ch) && ch) {
Li Zefan3c235a32009-09-22 13:51:54 +08001473 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001474 parser->buffer[parser->idx++] = ch;
1475 else {
1476 ret = -EINVAL;
1477 goto out;
1478 }
1479 ret = get_user(ch, ubuf++);
1480 if (ret)
1481 goto out;
1482 read++;
1483 cnt--;
1484 }
1485
1486 /* We either got finished input or we have to wait for another call. */
Changbin Du921a7ac2018-01-16 17:02:28 +08001487 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001488 parser->buffer[parser->idx] = 0;
1489 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001490 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001491 parser->cont = true;
1492 parser->buffer[parser->idx++] = ch;
Changbin Duf4d07062018-01-16 17:02:30 +08001493 /* Make sure the parsed string always terminates with '\0'. */
1494 parser->buffer[parser->idx] = 0;
Steven Rostedt057db842013-10-09 22:23:23 -04001495 } else {
1496 ret = -EINVAL;
1497 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001498 }
1499
1500 *ppos += read;
1501 ret = read;
1502
1503out:
1504 return ret;
1505}
1506
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001507/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001508static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001509{
1510 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001511
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001512 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001513 return -EBUSY;
1514
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001515 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001516 if (cnt > len)
1517 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001518 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001519
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001520 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001521 return cnt;
1522}
1523
Tim Bird0e950172010-02-25 15:36:43 -08001524unsigned long __read_mostly tracing_thresh;
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02001525static const struct file_operations tracing_max_lat_fops;
1526
1527#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1528 defined(CONFIG_FSNOTIFY)
1529
1530static struct workqueue_struct *fsnotify_wq;
1531
1532static void latency_fsnotify_workfn(struct work_struct *work)
1533{
1534 struct trace_array *tr = container_of(work, struct trace_array,
1535 fsnotify_work);
1536 fsnotify(tr->d_max_latency->d_inode, FS_MODIFY,
1537 tr->d_max_latency->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
1538}
1539
1540static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1541{
1542 struct trace_array *tr = container_of(iwork, struct trace_array,
1543 fsnotify_irqwork);
1544 queue_work(fsnotify_wq, &tr->fsnotify_work);
1545}
1546
1547static void trace_create_maxlat_file(struct trace_array *tr,
1548 struct dentry *d_tracer)
1549{
1550 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1551 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1552 tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
1553 d_tracer, &tr->max_latency,
1554 &tracing_max_lat_fops);
1555}
1556
1557__init static int latency_fsnotify_init(void)
1558{
1559 fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1560 WQ_UNBOUND | WQ_HIGHPRI, 0);
1561 if (!fsnotify_wq) {
1562 pr_err("Unable to allocate tr_max_lat_wq\n");
1563 return -ENOMEM;
1564 }
1565 return 0;
1566}
1567
1568late_initcall_sync(latency_fsnotify_init);
1569
1570void latency_fsnotify(struct trace_array *tr)
1571{
1572 if (!fsnotify_wq)
1573 return;
1574 /*
1575 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1576 * possible that we are called from __schedule() or do_idle(), which
1577 * could cause a deadlock.
1578 */
1579 irq_work_queue(&tr->fsnotify_irqwork);
1580}
1581
1582/*
1583 * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1584 * defined(CONFIG_FSNOTIFY)
1585 */
1586#else
1587
1588#define trace_create_maxlat_file(tr, d_tracer) \
1589 trace_create_file("tracing_max_latency", 0644, d_tracer, \
1590 &tr->max_latency, &tracing_max_lat_fops)
1591
1592#endif
Tim Bird0e950172010-02-25 15:36:43 -08001593
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001594#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001595/*
1596 * Copy the new maximum trace into the separate maximum-trace
1597 * structure. (this way the maximum trace is permanently saved,
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001598 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001599 */
1600static void
1601__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1602{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001603 struct array_buffer *trace_buf = &tr->array_buffer;
1604 struct array_buffer *max_buf = &tr->max_buffer;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001605 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1606 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001607
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001608 max_buf->cpu = cpu;
1609 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001610
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001611 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001612 max_data->critical_start = data->critical_start;
1613 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001614
Tom Zanussi85f726a2019-03-05 10:12:00 -06001615 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001616 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001617 /*
1618 * If tsk == current, then use current_uid(), as that does not use
1619 * RCU. The irq tracer can be called out of RCU scope.
1620 */
1621 if (tsk == current)
1622 max_data->uid = current_uid();
1623 else
1624 max_data->uid = task_uid(tsk);
1625
Steven Rostedt8248ac02009-09-02 12:27:41 -04001626 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1627 max_data->policy = tsk->policy;
1628 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001629
1630 /* record this tasks comm */
1631 tracing_record_cmdline(tsk);
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02001632 latency_fsnotify(tr);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001633}
1634
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001635/**
1636 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1637 * @tr: tracer
1638 * @tsk: the task with the latency
1639 * @cpu: The cpu that initiated the trace.
Tom Zanussia35873a2019-02-13 17:42:45 -06001640 * @cond_data: User data associated with a conditional snapshot
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001641 *
1642 * Flip the buffers between the @tr and the max_tr and record information
1643 * about which task was the cause of this latency.
1644 */
Ingo Molnare309b412008-05-12 21:20:51 +02001645void
Tom Zanussia35873a2019-02-13 17:42:45 -06001646update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1647 void *cond_data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001648{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001649 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001650 return;
1651
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001652 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001653
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001654 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001655 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001656 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001657 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001658 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001659
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001660 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001661
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001662 /* Inherit the recordable setting from array_buffer */
1663 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09001664 ring_buffer_record_on(tr->max_buffer.buffer);
1665 else
1666 ring_buffer_record_off(tr->max_buffer.buffer);
1667
Tom Zanussia35873a2019-02-13 17:42:45 -06001668#ifdef CONFIG_TRACER_SNAPSHOT
1669 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1670 goto out_unlock;
1671#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001672 swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001673
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001674 __update_max_tr(tr, tsk, cpu);
Tom Zanussia35873a2019-02-13 17:42:45 -06001675
1676 out_unlock:
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001677 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001678}
1679
1680/**
1681 * update_max_tr_single - only copy one trace over, and reset the rest
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07001682 * @tr: tracer
1683 * @tsk: task with the latency
1684 * @cpu: the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001685 *
1686 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001687 */
Ingo Molnare309b412008-05-12 21:20:51 +02001688void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001689update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1690{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001691 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001692
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001693 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001694 return;
1695
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001696 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001697 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001698 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001699 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001700 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001701 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001702
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001703 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001704
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001705 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001706
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001707 if (ret == -EBUSY) {
1708 /*
1709 * We failed to swap the buffer due to a commit taking
1710 * place on this CPU. We fail to record, but we reset
1711 * the max trace buffer (no one writes directly to it)
1712 * and flag that it failed.
1713 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001714 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001715 "Failed to swap buffers due to commit in progress\n");
1716 }
1717
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001718 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001719
1720 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001721 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001722}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001723#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001724
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001725static int wait_on_pipe(struct trace_iterator *iter, int full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001726{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001727 /* Iterators are static, they should be filled or empty */
1728 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001729 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001730
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001731 return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
Rabin Vincente30f53a2014-11-10 19:46:34 +01001732 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001733}
1734
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001735#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001736static bool selftests_can_run;
1737
1738struct trace_selftests {
1739 struct list_head list;
1740 struct tracer *type;
1741};
1742
1743static LIST_HEAD(postponed_selftests);
1744
1745static int save_selftest(struct tracer *type)
1746{
1747 struct trace_selftests *selftest;
1748
1749 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1750 if (!selftest)
1751 return -ENOMEM;
1752
1753 selftest->type = type;
1754 list_add(&selftest->list, &postponed_selftests);
1755 return 0;
1756}
1757
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001758static int run_tracer_selftest(struct tracer *type)
1759{
1760 struct trace_array *tr = &global_trace;
1761 struct tracer *saved_tracer = tr->current_trace;
1762 int ret;
1763
1764 if (!type->selftest || tracing_selftest_disabled)
1765 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766
1767 /*
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001768 * If a tracer registers early in boot up (before scheduling is
1769 * initialized and such), then do not run its selftests yet.
1770 * Instead, run it a little later in the boot process.
1771 */
1772 if (!selftests_can_run)
1773 return save_selftest(type);
1774
1775 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001776 * Run a selftest on this tracer.
1777 * Here we reset the trace buffer, and set the current
1778 * tracer to be this tracer. The tracer can then run some
1779 * internal tracing to verify that everything is in order.
1780 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001781 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001782 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001783
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001784 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001785
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001786#ifdef CONFIG_TRACER_MAX_TRACE
1787 if (type->use_max_tr) {
1788 /* If we expanded the buffers, make sure the max is expanded too */
1789 if (ring_buffer_expanded)
1790 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1791 RING_BUFFER_ALL_CPUS);
1792 tr->allocated_snapshot = true;
1793 }
1794#endif
1795
1796 /* the test is responsible for initializing and enabling */
1797 pr_info("Testing tracer %s: ", type->name);
1798 ret = type->selftest(type, tr);
1799 /* the test is responsible for resetting too */
1800 tr->current_trace = saved_tracer;
1801 if (ret) {
1802 printk(KERN_CONT "FAILED!\n");
1803 /* Add the warning after printing 'FAILED' */
1804 WARN_ON(1);
1805 return -1;
1806 }
1807 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001808 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001809
1810#ifdef CONFIG_TRACER_MAX_TRACE
1811 if (type->use_max_tr) {
1812 tr->allocated_snapshot = false;
1813
1814 /* Shrink the max buffer again */
1815 if (ring_buffer_expanded)
1816 ring_buffer_resize(tr->max_buffer.buffer, 1,
1817 RING_BUFFER_ALL_CPUS);
1818 }
1819#endif
1820
1821 printk(KERN_CONT "PASSED\n");
1822 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823}
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001824
1825static __init int init_trace_selftests(void)
1826{
1827 struct trace_selftests *p, *n;
1828 struct tracer *t, **last;
1829 int ret;
1830
1831 selftests_can_run = true;
1832
1833 mutex_lock(&trace_types_lock);
1834
1835 if (list_empty(&postponed_selftests))
1836 goto out;
1837
1838 pr_info("Running postponed tracer tests:\n");
1839
1840 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
Anders Roxell6fc21712018-11-30 15:56:22 +01001841 /* This loop can take minutes when sanitizers are enabled, so
1842 * lets make sure we allow RCU processing.
1843 */
1844 cond_resched();
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001845 ret = run_tracer_selftest(p->type);
1846 /* If the test fails, then warn and remove from available_tracers */
1847 if (ret < 0) {
1848 WARN(1, "tracer: %s failed selftest, disabling\n",
1849 p->type->name);
1850 last = &trace_types;
1851 for (t = trace_types; t; t = t->next) {
1852 if (t == p->type) {
1853 *last = t->next;
1854 break;
1855 }
1856 last = &t->next;
1857 }
1858 }
1859 list_del(&p->list);
1860 kfree(p);
1861 }
1862
1863 out:
1864 mutex_unlock(&trace_types_lock);
1865
1866 return 0;
1867}
Steven Rostedtb9ef0322017-05-17 11:14:35 -04001868core_initcall(init_trace_selftests);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001869#else
1870static inline int run_tracer_selftest(struct tracer *type)
1871{
1872 return 0;
1873}
1874#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001875
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001876static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1877
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001878static void __init apply_trace_boot_options(void);
1879
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001880/**
1881 * register_tracer - register a tracer with the ftrace system.
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07001882 * @type: the plugin for the tracer
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001883 *
1884 * Register a new plugin tracer.
1885 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001886int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001887{
1888 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001889 int ret = 0;
1890
1891 if (!type->name) {
1892 pr_info("Tracer must have a name\n");
1893 return -1;
1894 }
1895
Dan Carpenter24a461d2010-07-10 12:06:44 +02001896 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001897 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1898 return -1;
1899 }
1900
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05001901 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11001902 pr_warn("Can not register tracer %s due to lockdown\n",
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05001903 type->name);
1904 return -EPERM;
1905 }
1906
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001908
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001909 tracing_selftest_running = true;
1910
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001911 for (t = trace_types; t; t = t->next) {
1912 if (strcmp(type->name, t->name) == 0) {
1913 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001914 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001915 type->name);
1916 ret = -1;
1917 goto out;
1918 }
1919 }
1920
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001921 if (!type->set_flag)
1922 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001923 if (!type->flags) {
1924 /*allocate a dummy tracer_flags*/
1925 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001926 if (!type->flags) {
1927 ret = -ENOMEM;
1928 goto out;
1929 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001930 type->flags->val = 0;
1931 type->flags->opts = dummy_tracer_opt;
1932 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001933 if (!type->flags->opts)
1934 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001935
Chunyu Hud39cdd22016-03-08 21:37:01 +08001936 /* store the tracer for __set_tracer_option */
1937 type->flags->trace = type;
1938
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001939 ret = run_tracer_selftest(type);
1940 if (ret < 0)
1941 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001942
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001943 type->next = trace_types;
1944 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001945 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001946
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001947 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001948 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001949 mutex_unlock(&trace_types_lock);
1950
Steven Rostedtdac74942009-02-05 01:13:38 -05001951 if (ret || !default_bootup_tracer)
1952 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001953
Li Zefanee6c2c12009-09-18 14:06:47 +08001954 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001955 goto out_unlock;
1956
1957 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1958 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001959 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001960 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001961
1962 apply_trace_boot_options();
1963
Steven Rostedtdac74942009-02-05 01:13:38 -05001964 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001965 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001966#ifdef CONFIG_FTRACE_STARTUP_TEST
1967 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1968 type->name);
1969#endif
1970
1971 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001972 return ret;
1973}
1974
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001975static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001976{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001977 struct trace_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001978
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001979 if (!buffer)
1980 return;
1981
Steven Rostedtf6339032009-09-04 12:35:16 -04001982 ring_buffer_record_disable(buffer);
1983
1984 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08001985 synchronize_rcu();
Steven Rostedt68179682012-05-08 20:57:53 -04001986 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001987
1988 ring_buffer_record_enable(buffer);
1989}
1990
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001991void tracing_reset_online_cpus(struct array_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001992{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001993 struct trace_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001994 int cpu;
1995
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001996 if (!buffer)
1997 return;
1998
Steven Rostedt621968c2009-09-04 12:02:35 -04001999 ring_buffer_record_disable(buffer);
2000
2001 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08002002 synchronize_rcu();
Steven Rostedt621968c2009-09-04 12:02:35 -04002003
Alexander Z Lam94571582013-08-02 18:36:16 -07002004 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02002005
2006 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04002007 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04002008
2009 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02002010}
2011
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04002012/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002013void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002014{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002015 struct trace_array *tr;
2016
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002017 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04002018 if (!tr->clear_trace)
2019 continue;
2020 tr->clear_trace = false;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002021 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002022#ifdef CONFIG_TRACER_MAX_TRACE
2023 tracing_reset_online_cpus(&tr->max_buffer);
2024#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002025 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002026}
2027
Joel Fernandesd914ba32017-06-26 19:01:55 -07002028static int *tgid_map;
2029
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002030#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01002031#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01002032static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002033struct saved_cmdlines_buffer {
2034 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
2035 unsigned *map_cmdline_to_pid;
2036 unsigned cmdline_num;
2037 int cmdline_idx;
2038 char *saved_cmdlines;
2039};
2040static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02002041
Steven Rostedt25b0b442008-05-12 21:21:00 +02002042/* temporary disable recording */
Joel Fernandesd914ba32017-06-26 19:01:55 -07002043static atomic_t trace_record_taskinfo_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002044
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002045static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002046{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002047 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
2048}
2049
2050static inline void set_cmdline(int idx, const char *cmdline)
2051{
Tom Zanussi85f726a2019-03-05 10:12:00 -06002052 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002053}
2054
2055static int allocate_cmdlines_buffer(unsigned int val,
2056 struct saved_cmdlines_buffer *s)
2057{
Kees Cook6da2ec52018-06-12 13:55:00 -07002058 s->map_cmdline_to_pid = kmalloc_array(val,
2059 sizeof(*s->map_cmdline_to_pid),
2060 GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002061 if (!s->map_cmdline_to_pid)
2062 return -ENOMEM;
2063
Kees Cook6da2ec52018-06-12 13:55:00 -07002064 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002065 if (!s->saved_cmdlines) {
2066 kfree(s->map_cmdline_to_pid);
2067 return -ENOMEM;
2068 }
2069
2070 s->cmdline_idx = 0;
2071 s->cmdline_num = val;
2072 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
2073 sizeof(s->map_pid_to_cmdline));
2074 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
2075 val * sizeof(*s->map_cmdline_to_pid));
2076
2077 return 0;
2078}
2079
2080static int trace_create_savedcmd(void)
2081{
2082 int ret;
2083
Namhyung Kima6af8fb2014-06-10 16:11:35 +09002084 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002085 if (!savedcmd)
2086 return -ENOMEM;
2087
2088 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
2089 if (ret < 0) {
2090 kfree(savedcmd);
2091 savedcmd = NULL;
2092 return -ENOMEM;
2093 }
2094
2095 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002096}
2097
Carsten Emdeb5130b12009-09-13 01:43:07 +02002098int is_tracing_stopped(void)
2099{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002100 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02002101}
2102
Steven Rostedt0f048702008-11-05 16:05:44 -05002103/**
2104 * tracing_start - quick start of the tracer
2105 *
2106 * If tracing is enabled but was stopped by tracing_stop,
2107 * this will start the tracer back up.
2108 */
2109void tracing_start(void)
2110{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002111 struct trace_buffer *buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002112 unsigned long flags;
2113
2114 if (tracing_disabled)
2115 return;
2116
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002117 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2118 if (--global_trace.stop_count) {
2119 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05002120 /* Someone screwed up their debugging */
2121 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002122 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05002123 }
Steven Rostedt0f048702008-11-05 16:05:44 -05002124 goto out;
2125 }
2126
Steven Rostedta2f80712010-03-12 19:56:00 -05002127 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002128 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05002129
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002130 buffer = global_trace.array_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002131 if (buffer)
2132 ring_buffer_record_enable(buffer);
2133
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002134#ifdef CONFIG_TRACER_MAX_TRACE
2135 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002136 if (buffer)
2137 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002138#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002139
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002140 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002141
Steven Rostedt0f048702008-11-05 16:05:44 -05002142 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002143 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2144}
2145
2146static void tracing_start_tr(struct trace_array *tr)
2147{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002148 struct trace_buffer *buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002149 unsigned long flags;
2150
2151 if (tracing_disabled)
2152 return;
2153
2154 /* If global, we need to also start the max tracer */
2155 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2156 return tracing_start();
2157
2158 raw_spin_lock_irqsave(&tr->start_lock, flags);
2159
2160 if (--tr->stop_count) {
2161 if (tr->stop_count < 0) {
2162 /* Someone screwed up their debugging */
2163 WARN_ON_ONCE(1);
2164 tr->stop_count = 0;
2165 }
2166 goto out;
2167 }
2168
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002169 buffer = tr->array_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002170 if (buffer)
2171 ring_buffer_record_enable(buffer);
2172
2173 out:
2174 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002175}
2176
2177/**
2178 * tracing_stop - quick stop of the tracer
2179 *
2180 * Light weight way to stop tracing. Use in conjunction with
2181 * tracing_start.
2182 */
2183void tracing_stop(void)
2184{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002185 struct trace_buffer *buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002186 unsigned long flags;
2187
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002188 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2189 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05002190 goto out;
2191
Steven Rostedta2f80712010-03-12 19:56:00 -05002192 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002193 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002194
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002195 buffer = global_trace.array_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002196 if (buffer)
2197 ring_buffer_record_disable(buffer);
2198
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002199#ifdef CONFIG_TRACER_MAX_TRACE
2200 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002201 if (buffer)
2202 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002203#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002204
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002205 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002206
Steven Rostedt0f048702008-11-05 16:05:44 -05002207 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002208 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2209}
2210
2211static void tracing_stop_tr(struct trace_array *tr)
2212{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002213 struct trace_buffer *buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002214 unsigned long flags;
2215
2216 /* If global, we need to also stop the max tracer */
2217 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2218 return tracing_stop();
2219
2220 raw_spin_lock_irqsave(&tr->start_lock, flags);
2221 if (tr->stop_count++)
2222 goto out;
2223
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002224 buffer = tr->array_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002225 if (buffer)
2226 ring_buffer_record_disable(buffer);
2227
2228 out:
2229 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002230}
2231
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002232static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002233{
Carsten Emdea635cf02009-03-18 09:00:41 +01002234 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002235
Joel Fernandeseaf260a2017-07-06 16:00:21 -07002236 /* treat recording of idle task as a success */
2237 if (!tsk->pid)
2238 return 1;
2239
2240 if (unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002241 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002242
2243 /*
2244 * It's not the end of the world if we don't get
2245 * the lock, but we also don't want to spin
2246 * nor do we want to disable interrupts,
2247 * so if we miss here, then better luck next time.
2248 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002249 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002250 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002252 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01002253 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002254 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255
Carsten Emdea635cf02009-03-18 09:00:41 +01002256 /*
2257 * Check whether the cmdline buffer at idx has a pid
2258 * mapped. We are going to overwrite that entry so we
2259 * need to clear the map_pid_to_cmdline. Otherwise we
2260 * would read the new comm for the old pid.
2261 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002262 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01002263 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002264 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002265
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002266 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
2267 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002268
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002269 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270 }
2271
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002272 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002274 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002275
2276 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002277}
2278
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002279static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002280{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002281 unsigned map;
2282
Steven Rostedt4ca530852009-03-16 19:20:15 -04002283 if (!pid) {
2284 strcpy(comm, "<idle>");
2285 return;
2286 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002287
Steven Rostedt74bf4072010-01-25 15:11:53 -05002288 if (WARN_ON_ONCE(pid < 0)) {
2289 strcpy(comm, "<XXX>");
2290 return;
2291 }
2292
Steven Rostedt4ca530852009-03-16 19:20:15 -04002293 if (pid > PID_MAX_DEFAULT) {
2294 strcpy(comm, "<...>");
2295 return;
2296 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002297
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002298 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01002299 if (map != NO_CMDLINE_MAP)
Amey Telawanee09e2862017-05-03 15:41:14 +05302300 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
Thomas Gleixner50d88752009-03-18 08:58:44 +01002301 else
2302 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002303}
2304
2305void trace_find_cmdline(int pid, char comm[])
2306{
2307 preempt_disable();
2308 arch_spin_lock(&trace_cmdline_lock);
2309
2310 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002311
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002312 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02002313 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002314}
2315
Joel Fernandesd914ba32017-06-26 19:01:55 -07002316int trace_find_tgid(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002317{
Joel Fernandesd914ba32017-06-26 19:01:55 -07002318 if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2319 return 0;
2320
2321 return tgid_map[pid];
2322}
2323
2324static int trace_save_tgid(struct task_struct *tsk)
2325{
Joel Fernandesbd45d342017-07-06 16:00:22 -07002326 /* treat recording of idle task as a success */
2327 if (!tsk->pid)
2328 return 1;
2329
2330 if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
Joel Fernandesd914ba32017-06-26 19:01:55 -07002331 return 0;
2332
2333 tgid_map[tsk->pid] = tsk->tgid;
2334 return 1;
2335}
2336
2337static bool tracing_record_taskinfo_skip(int flags)
2338{
2339 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2340 return true;
2341 if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2342 return true;
2343 if (!__this_cpu_read(trace_taskinfo_save))
2344 return true;
2345 return false;
2346}
2347
2348/**
2349 * tracing_record_taskinfo - record the task info of a task
2350 *
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07002351 * @task: task to record
2352 * @flags: TRACE_RECORD_CMDLINE for recording comm
2353 * TRACE_RECORD_TGID for recording tgid
Joel Fernandesd914ba32017-06-26 19:01:55 -07002354 */
2355void tracing_record_taskinfo(struct task_struct *task, int flags)
2356{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002357 bool done;
2358
Joel Fernandesd914ba32017-06-26 19:01:55 -07002359 if (tracing_record_taskinfo_skip(flags))
2360 return;
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002361
2362 /*
2363 * Record as much task information as possible. If some fail, continue
2364 * to try to record the others.
2365 */
2366 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2367 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2368
2369 /* If recording any information failed, retry again soon. */
2370 if (!done)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002371 return;
2372
Joel Fernandesd914ba32017-06-26 19:01:55 -07002373 __this_cpu_write(trace_taskinfo_save, false);
2374}
2375
2376/**
2377 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2378 *
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07002379 * @prev: previous task during sched_switch
2380 * @next: next task during sched_switch
2381 * @flags: TRACE_RECORD_CMDLINE for recording comm
2382 * TRACE_RECORD_TGID for recording tgid
Joel Fernandesd914ba32017-06-26 19:01:55 -07002383 */
2384void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2385 struct task_struct *next, int flags)
2386{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002387 bool done;
2388
Joel Fernandesd914ba32017-06-26 19:01:55 -07002389 if (tracing_record_taskinfo_skip(flags))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002390 return;
2391
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002392 /*
2393 * Record as much task information as possible. If some fail, continue
2394 * to try to record the others.
2395 */
2396 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2397 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2398 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2399 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
Joel Fernandesd914ba32017-06-26 19:01:55 -07002400
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002401 /* If recording any information failed, retry again soon. */
2402 if (!done)
Joel Fernandesd914ba32017-06-26 19:01:55 -07002403 return;
2404
2405 __this_cpu_write(trace_taskinfo_save, false);
2406}
2407
2408/* Helpers to record a specific task information */
2409void tracing_record_cmdline(struct task_struct *task)
2410{
2411 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2412}
2413
2414void tracing_record_tgid(struct task_struct *task)
2415{
2416 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002417}
2418
Steven Rostedt (VMware)af0009f2017-03-16 11:01:06 -04002419/*
2420 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2421 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2422 * simplifies those functions and keeps them in sync.
2423 */
2424enum print_line_t trace_handle_return(struct trace_seq *s)
2425{
2426 return trace_seq_has_overflowed(s) ?
2427 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2428}
2429EXPORT_SYMBOL_GPL(trace_handle_return);
2430
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03002431void
Cong Wang46710f32019-05-25 09:57:59 -07002432tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2433 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002434{
2435 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002436
Steven Rostedt777e2082008-09-29 23:02:42 -04002437 entry->preempt_count = pc & 0xff;
2438 entry->pid = (tsk) ? tsk->pid : 0;
Cong Wang46710f32019-05-25 09:57:59 -07002439 entry->type = type;
Steven Rostedt777e2082008-09-29 23:02:42 -04002440 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04002441#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04002442 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04002443#else
2444 TRACE_FLAG_IRQS_NOSUPPORT |
2445#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01002446 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002447 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
Pavankumar Kondetic59f29c2016-12-09 21:50:17 +05302448 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02002449 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2450 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002451}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002452EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002453
Steven Rostedte77405a2009-09-02 14:17:06 -04002454struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002455trace_buffer_lock_reserve(struct trace_buffer *buffer,
Steven Rostedte77405a2009-09-02 14:17:06 -04002456 int type,
2457 unsigned long len,
2458 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002459{
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002460 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002461}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002462
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002463DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2464DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2465static int trace_buffered_event_ref;
2466
2467/**
2468 * trace_buffered_event_enable - enable buffering events
2469 *
2470 * When events are being filtered, it is quicker to use a temporary
2471 * buffer to write the event data into if there's a likely chance
2472 * that it will not be committed. The discard of the ring buffer
2473 * is not as fast as committing, and is much slower than copying
2474 * a commit.
2475 *
2476 * When an event is to be filtered, allocate per cpu buffers to
2477 * write the event data into, and if the event is filtered and discarded
2478 * it is simply dropped, otherwise, the entire data is to be committed
2479 * in one shot.
2480 */
2481void trace_buffered_event_enable(void)
2482{
2483 struct ring_buffer_event *event;
2484 struct page *page;
2485 int cpu;
2486
2487 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2488
2489 if (trace_buffered_event_ref++)
2490 return;
2491
2492 for_each_tracing_cpu(cpu) {
2493 page = alloc_pages_node(cpu_to_node(cpu),
2494 GFP_KERNEL | __GFP_NORETRY, 0);
2495 if (!page)
2496 goto failed;
2497
2498 event = page_address(page);
2499 memset(event, 0, sizeof(*event));
2500
2501 per_cpu(trace_buffered_event, cpu) = event;
2502
2503 preempt_disable();
2504 if (cpu == smp_processor_id() &&
2505 this_cpu_read(trace_buffered_event) !=
2506 per_cpu(trace_buffered_event, cpu))
2507 WARN_ON_ONCE(1);
2508 preempt_enable();
2509 }
2510
2511 return;
2512 failed:
2513 trace_buffered_event_disable();
2514}
2515
2516static void enable_trace_buffered_event(void *data)
2517{
2518 /* Probably not needed, but do it anyway */
2519 smp_rmb();
2520 this_cpu_dec(trace_buffered_event_cnt);
2521}
2522
2523static void disable_trace_buffered_event(void *data)
2524{
2525 this_cpu_inc(trace_buffered_event_cnt);
2526}
2527
2528/**
2529 * trace_buffered_event_disable - disable buffering events
2530 *
2531 * When a filter is removed, it is faster to not use the buffered
2532 * events, and to commit directly into the ring buffer. Free up
2533 * the temp buffers when there are no more users. This requires
2534 * special synchronization with current events.
2535 */
2536void trace_buffered_event_disable(void)
2537{
2538 int cpu;
2539
2540 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2541
2542 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2543 return;
2544
2545 if (--trace_buffered_event_ref)
2546 return;
2547
2548 preempt_disable();
2549 /* For each CPU, set the buffer as used. */
2550 smp_call_function_many(tracing_buffer_mask,
2551 disable_trace_buffered_event, NULL, 1);
2552 preempt_enable();
2553
2554 /* Wait for all current users to finish */
Paul E. McKenney74401722018-11-06 18:44:52 -08002555 synchronize_rcu();
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002556
2557 for_each_tracing_cpu(cpu) {
2558 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2559 per_cpu(trace_buffered_event, cpu) = NULL;
2560 }
2561 /*
2562 * Make sure trace_buffered_event is NULL before clearing
2563 * trace_buffered_event_cnt.
2564 */
2565 smp_wmb();
2566
2567 preempt_disable();
2568 /* Do the work on each cpu */
2569 smp_call_function_many(tracing_buffer_mask,
2570 enable_trace_buffered_event, NULL, 1);
2571 preempt_enable();
2572}
2573
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002574static struct trace_buffer *temp_buffer;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002575
Steven Rostedtef5580d2009-02-27 19:38:04 -05002576struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002577trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002578 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002579 int type, unsigned long len,
2580 unsigned long flags, int pc)
2581{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002582 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002583 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002584
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002585 *current_rb = trace_file->tr->array_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002586
Tom Zanussi00b41452018-01-15 20:51:39 -06002587 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002588 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2589 (entry = this_cpu_read(trace_buffered_event))) {
2590 /* Try to use the per cpu buffer first */
2591 val = this_cpu_inc_return(trace_buffered_event_cnt);
2592 if (val == 1) {
2593 trace_event_setup(entry, type, flags, pc);
2594 entry->array[0] = len;
2595 return entry;
2596 }
2597 this_cpu_dec(trace_buffered_event_cnt);
2598 }
2599
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002600 entry = __trace_buffer_lock_reserve(*current_rb,
2601 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002602 /*
2603 * If tracing is off, but we have triggers enabled
2604 * we still need to look at the event data. Use the temp_buffer
2605 * to store the trace event for the tigger to use. It's recusive
2606 * safe and will not be recorded anywhere.
2607 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002608 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002609 *current_rb = temp_buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002610 entry = __trace_buffer_lock_reserve(*current_rb,
2611 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002612 }
2613 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002614}
2615EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2616
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002617static DEFINE_SPINLOCK(tracepoint_iter_lock);
2618static DEFINE_MUTEX(tracepoint_printk_mutex);
2619
2620static void output_printk(struct trace_event_buffer *fbuffer)
2621{
2622 struct trace_event_call *event_call;
Masami Hiramatsud8d0c242020-01-11 01:05:18 +09002623 struct trace_event_file *file;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002624 struct trace_event *event;
2625 unsigned long flags;
2626 struct trace_iterator *iter = tracepoint_print_iter;
2627
2628 /* We should never get here if iter is NULL */
2629 if (WARN_ON_ONCE(!iter))
2630 return;
2631
2632 event_call = fbuffer->trace_file->event_call;
2633 if (!event_call || !event_call->event.funcs ||
2634 !event_call->event.funcs->trace)
2635 return;
2636
Masami Hiramatsud8d0c242020-01-11 01:05:18 +09002637 file = fbuffer->trace_file;
2638 if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2639 (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2640 !filter_match_preds(file->filter, fbuffer->entry)))
2641 return;
2642
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002643 event = &fbuffer->trace_file->event_call->event;
2644
2645 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2646 trace_seq_init(&iter->seq);
2647 iter->ent = fbuffer->entry;
2648 event_call->event.funcs->trace(iter, 0, event);
2649 trace_seq_putc(&iter->seq, 0);
2650 printk("%s", iter->seq.buffer);
2651
2652 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2653}
2654
2655int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2656 void __user *buffer, size_t *lenp,
2657 loff_t *ppos)
2658{
2659 int save_tracepoint_printk;
2660 int ret;
2661
2662 mutex_lock(&tracepoint_printk_mutex);
2663 save_tracepoint_printk = tracepoint_printk;
2664
2665 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2666
2667 /*
2668 * This will force exiting early, as tracepoint_printk
2669 * is always zero when tracepoint_printk_iter is not allocated
2670 */
2671 if (!tracepoint_print_iter)
2672 tracepoint_printk = 0;
2673
2674 if (save_tracepoint_printk == tracepoint_printk)
2675 goto out;
2676
2677 if (tracepoint_printk)
2678 static_key_enable(&tracepoint_printk_key.key);
2679 else
2680 static_key_disable(&tracepoint_printk_key.key);
2681
2682 out:
2683 mutex_unlock(&tracepoint_printk_mutex);
2684
2685 return ret;
2686}
2687
2688void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2689{
2690 if (static_key_false(&tracepoint_printk_key.key))
2691 output_printk(fbuffer);
2692
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09002693 event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002694 fbuffer->event, fbuffer->entry,
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09002695 fbuffer->flags, fbuffer->pc, fbuffer->regs);
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002696}
2697EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2698
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002699/*
2700 * Skip 3:
2701 *
2702 * trace_buffer_unlock_commit_regs()
2703 * trace_event_buffer_commit()
2704 * trace_event_raw_event_xxx()
Rohit Visavalia13cf9122018-01-29 15:11:26 +05302705 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002706# define STACK_SKIP 3
2707
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002708void trace_buffer_unlock_commit_regs(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002709 struct trace_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002710 struct ring_buffer_event *event,
2711 unsigned long flags, int pc,
2712 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002713{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002714 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002715
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002716 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002717 * If regs is not set, then skip the necessary functions.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002718 * Note, we can still get here via blktrace, wakeup tracer
2719 * and mmiotrace, but that's ok if they lose a function or
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002720 * two. They are not that meaningful.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002721 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002722 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002723 ftrace_trace_userstack(buffer, flags, pc);
2724}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002725
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002726/*
2727 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2728 */
2729void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002730trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002731 struct ring_buffer_event *event)
2732{
2733 __buffer_unlock_commit(buffer, event);
2734}
2735
Chunyan Zhang478409d2016-11-21 15:57:18 +08002736static void
2737trace_process_export(struct trace_export *export,
2738 struct ring_buffer_event *event)
2739{
2740 struct trace_entry *entry;
2741 unsigned int size = 0;
2742
2743 entry = ring_buffer_event_data(event);
2744 size = ring_buffer_event_length(event);
Felipe Balbia773d412017-06-02 13:20:25 +03002745 export->write(export, entry, size);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002746}
2747
2748static DEFINE_MUTEX(ftrace_export_lock);
2749
2750static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2751
2752static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2753
2754static inline void ftrace_exports_enable(void)
2755{
2756 static_branch_enable(&ftrace_exports_enabled);
2757}
2758
2759static inline void ftrace_exports_disable(void)
2760{
2761 static_branch_disable(&ftrace_exports_enabled);
2762}
2763
Mathieu Malaterre1cce3772018-05-16 21:30:12 +02002764static void ftrace_exports(struct ring_buffer_event *event)
Chunyan Zhang478409d2016-11-21 15:57:18 +08002765{
2766 struct trace_export *export;
2767
2768 preempt_disable_notrace();
2769
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -04002770 export = rcu_dereference_raw_check(ftrace_exports_list);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002771 while (export) {
2772 trace_process_export(export, event);
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -04002773 export = rcu_dereference_raw_check(export->next);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002774 }
2775
2776 preempt_enable_notrace();
2777}
2778
2779static inline void
2780add_trace_export(struct trace_export **list, struct trace_export *export)
2781{
2782 rcu_assign_pointer(export->next, *list);
2783 /*
2784 * We are entering export into the list but another
2785 * CPU might be walking that list. We need to make sure
2786 * the export->next pointer is valid before another CPU sees
2787 * the export pointer included into the list.
2788 */
2789 rcu_assign_pointer(*list, export);
2790}
2791
2792static inline int
2793rm_trace_export(struct trace_export **list, struct trace_export *export)
2794{
2795 struct trace_export **p;
2796
2797 for (p = list; *p != NULL; p = &(*p)->next)
2798 if (*p == export)
2799 break;
2800
2801 if (*p != export)
2802 return -1;
2803
2804 rcu_assign_pointer(*p, (*p)->next);
2805
2806 return 0;
2807}
2808
2809static inline void
2810add_ftrace_export(struct trace_export **list, struct trace_export *export)
2811{
2812 if (*list == NULL)
2813 ftrace_exports_enable();
2814
2815 add_trace_export(list, export);
2816}
2817
2818static inline int
2819rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2820{
2821 int ret;
2822
2823 ret = rm_trace_export(list, export);
2824 if (*list == NULL)
2825 ftrace_exports_disable();
2826
2827 return ret;
2828}
2829
2830int register_ftrace_export(struct trace_export *export)
2831{
2832 if (WARN_ON_ONCE(!export->write))
2833 return -1;
2834
2835 mutex_lock(&ftrace_export_lock);
2836
2837 add_ftrace_export(&ftrace_exports_list, export);
2838
2839 mutex_unlock(&ftrace_export_lock);
2840
2841 return 0;
2842}
2843EXPORT_SYMBOL_GPL(register_ftrace_export);
2844
2845int unregister_ftrace_export(struct trace_export *export)
2846{
2847 int ret;
2848
2849 mutex_lock(&ftrace_export_lock);
2850
2851 ret = rm_ftrace_export(&ftrace_exports_list, export);
2852
2853 mutex_unlock(&ftrace_export_lock);
2854
2855 return ret;
2856}
2857EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2858
Ingo Molnare309b412008-05-12 21:20:51 +02002859void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002860trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002861 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2862 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002863{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002864 struct trace_event_call *call = &event_function;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002865 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002866 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002867 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002868
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002869 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2870 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002871 if (!event)
2872 return;
2873 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002874 entry->ip = ip;
2875 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002876
Chunyan Zhang478409d2016-11-21 15:57:18 +08002877 if (!call_filter_check_discard(call, entry, buffer, event)) {
2878 if (static_branch_unlikely(&ftrace_exports_enabled))
2879 ftrace_exports(event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002880 __buffer_unlock_commit(buffer, event);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002881 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002882}
2883
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002884#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002885
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002886/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2887#define FTRACE_KSTACK_NESTING 4
2888
2889#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2890
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002891struct ftrace_stack {
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002892 unsigned long calls[FTRACE_KSTACK_ENTRIES];
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002893};
2894
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002895
2896struct ftrace_stacks {
2897 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2898};
2899
2900static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002901static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2902
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002903static void __ftrace_trace_stack(struct trace_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002904 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002905 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002906{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002907 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002908 struct ring_buffer_event *event;
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002909 unsigned int size, nr_entries;
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002910 struct ftrace_stack *fstack;
Steven Rostedt777e2082008-09-29 23:02:42 -04002911 struct stack_entry *entry;
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002912 int stackidx;
Ingo Molnar86387f72008-05-12 21:20:51 +02002913
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002914 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002915 * Add one, for this function and the call to save_stack_trace()
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002916 * If regs is set, then these functions will not be in the way.
2917 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002918#ifndef CONFIG_UNWINDER_ORC
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002919 if (!regs)
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002920 skip++;
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002921#endif
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002922
2923 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002924 * Since events can happen in NMIs there's no safe way to
2925 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2926 * or NMI comes in, it will just have to use the default
2927 * FTRACE_STACK_SIZE.
2928 */
2929 preempt_disable_notrace();
2930
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002931 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2932
2933 /* This should never happen. If it does, yell once and skip */
2934 if (WARN_ON_ONCE(stackidx > FTRACE_KSTACK_NESTING))
2935 goto out;
2936
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002937 /*
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002938 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2939 * interrupt will either see the value pre increment or post
2940 * increment. If the interrupt happens pre increment it will have
2941 * restored the counter when it returns. We just need a barrier to
2942 * keep gcc from moving things around.
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002943 */
2944 barrier();
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002945
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002946 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002947 size = ARRAY_SIZE(fstack->calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002948
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002949 if (regs) {
2950 nr_entries = stack_trace_save_regs(regs, fstack->calls,
2951 size, skip);
2952 } else {
2953 nr_entries = stack_trace_save(fstack->calls, size, skip);
2954 }
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002955
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002956 size = nr_entries * sizeof(unsigned long);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002957 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2958 sizeof(*entry) + size, flags, pc);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002959 if (!event)
2960 goto out;
2961 entry = ring_buffer_event_data(event);
2962
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002963 memcpy(&entry->caller, fstack->calls, size);
2964 entry->size = nr_entries;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002965
Tom Zanussif306cc82013-10-24 08:34:17 -05002966 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002967 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002968
2969 out:
2970 /* Again, don't let gcc optimize things here */
2971 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002972 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002973 preempt_enable_notrace();
2974
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002975}
2976
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002977static inline void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002978 struct trace_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002979 unsigned long flags,
2980 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002981{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002982 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002983 return;
2984
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002985 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002986}
2987
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002988void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2989 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002990{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002991 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002992
2993 if (rcu_is_watching()) {
2994 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2995 return;
2996 }
2997
2998 /*
2999 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
3000 * but if the above rcu_is_watching() failed, then the NMI
3001 * triggered someplace critical, and rcu_irq_enter() should
3002 * not be called from NMI.
3003 */
3004 if (unlikely(in_nmi()))
3005 return;
3006
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04003007 rcu_irq_enter_irqson();
3008 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
3009 rcu_irq_exit_irqson();
Steven Rostedt38697052008-10-01 13:14:09 -04003010}
3011
Steven Rostedt03889382009-12-11 09:48:22 -05003012/**
3013 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003014 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05003015 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003016void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05003017{
3018 unsigned long flags;
3019
3020 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05003021 return;
Steven Rostedt03889382009-12-11 09:48:22 -05003022
3023 local_save_flags(flags);
3024
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05003025#ifndef CONFIG_UNWINDER_ORC
3026 /* Skip 1 to skip this function. */
3027 skip++;
3028#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003029 __ftrace_trace_stack(global_trace.array_buffer.buffer,
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003030 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05003031}
Nikolay Borisovda387e52018-10-17 09:51:43 +03003032EXPORT_SYMBOL_GPL(trace_dump_stack);
Steven Rostedt03889382009-12-11 09:48:22 -05003033
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003034#ifdef CONFIG_USER_STACKTRACE_SUPPORT
Steven Rostedt91e86e52010-11-10 12:56:12 +01003035static DEFINE_PER_CPU(int, user_stack_count);
3036
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003037static void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003038ftrace_trace_userstack(struct trace_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02003039{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003040 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02003041 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02003042 struct userstack_entry *entry;
Török Edwin02b67512008-11-22 13:28:47 +02003043
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003044 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02003045 return;
3046
Steven Rostedtb6345872010-03-12 20:03:30 -05003047 /*
3048 * NMIs can not handle page faults, even with fix ups.
3049 * The save user stack can (and often does) fault.
3050 */
3051 if (unlikely(in_nmi()))
3052 return;
3053
Steven Rostedt91e86e52010-11-10 12:56:12 +01003054 /*
3055 * prevent recursion, since the user stack tracing may
3056 * trigger other kernel events.
3057 */
3058 preempt_disable();
3059 if (__this_cpu_read(user_stack_count))
3060 goto out;
3061
3062 __this_cpu_inc(user_stack_count);
3063
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003064 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
3065 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02003066 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08003067 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02003068 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02003069
Steven Rostedt48659d32009-09-11 11:36:23 -04003070 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02003071 memset(&entry->caller, 0, sizeof(entry->caller));
3072
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02003073 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
Tom Zanussif306cc82013-10-24 08:34:17 -05003074 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003075 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01003076
Li Zefan1dbd1952010-12-09 15:47:56 +08003077 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01003078 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01003079 out:
3080 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02003081}
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003082#else /* CONFIG_USER_STACKTRACE_SUPPORT */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003083static void ftrace_trace_userstack(struct trace_buffer *buffer,
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003084 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02003085{
Török Edwin02b67512008-11-22 13:28:47 +02003086}
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003087#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
Török Edwin02b67512008-11-22 13:28:47 +02003088
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02003089#endif /* CONFIG_STACKTRACE */
3090
Steven Rostedt07d777f2011-09-22 14:01:55 -04003091/* created for use with alloc_percpu */
3092struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003093 int nesting;
3094 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04003095};
3096
3097static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003098
3099/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003100 * Thise allows for lockless recording. If we're nested too deeply, then
3101 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04003102 */
3103static char *get_trace_buf(void)
3104{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003105 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04003106
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003107 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003108 return NULL;
3109
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04003110 buffer->nesting++;
3111
3112 /* Interrupts must see nesting incremented before we use the buffer */
3113 barrier();
3114 return &buffer->buffer[buffer->nesting][0];
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003115}
3116
3117static void put_trace_buf(void)
3118{
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04003119 /* Don't let the decrement of nesting leak before this */
3120 barrier();
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003121 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04003122}
3123
3124static int alloc_percpu_trace_buffer(void)
3125{
3126 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003127
3128 buffers = alloc_percpu(struct trace_buffer_struct);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05003129 if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003130 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003131
3132 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003133 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003134}
3135
Steven Rostedt81698832012-10-11 10:15:05 -04003136static int buffers_allocated;
3137
Steven Rostedt07d777f2011-09-22 14:01:55 -04003138void trace_printk_init_buffers(void)
3139{
Steven Rostedt07d777f2011-09-22 14:01:55 -04003140 if (buffers_allocated)
3141 return;
3142
3143 if (alloc_percpu_trace_buffer())
3144 return;
3145
Steven Rostedt2184db42014-05-28 13:14:40 -04003146 /* trace_printk() is for debug use only. Don't use it in production. */
3147
Joe Perchesa395d6a2016-03-22 14:28:09 -07003148 pr_warn("\n");
3149 pr_warn("**********************************************************\n");
3150 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3151 pr_warn("** **\n");
3152 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3153 pr_warn("** **\n");
3154 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3155 pr_warn("** unsafe for production use. **\n");
3156 pr_warn("** **\n");
3157 pr_warn("** If you see this message and you are not debugging **\n");
3158 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3159 pr_warn("** **\n");
3160 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3161 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04003162
Steven Rostedtb382ede62012-10-10 21:44:34 -04003163 /* Expand the buffers to set size */
3164 tracing_update_buffers();
3165
Steven Rostedt07d777f2011-09-22 14:01:55 -04003166 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04003167
3168 /*
3169 * trace_printk_init_buffers() can be called by modules.
3170 * If that happens, then we need to start cmdline recording
3171 * directly here. If the global_trace.buffer is already
3172 * allocated here, then this was called by module code.
3173 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003174 if (global_trace.array_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04003175 tracing_start_cmdline_record();
3176}
Divya Indif45d1222019-03-20 11:28:51 -07003177EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
Steven Rostedt81698832012-10-11 10:15:05 -04003178
3179void trace_printk_start_comm(void)
3180{
3181 /* Start tracing comms if trace printk is set */
3182 if (!buffers_allocated)
3183 return;
3184 tracing_start_cmdline_record();
3185}
3186
3187static void trace_printk_start_stop_comm(int enabled)
3188{
3189 if (!buffers_allocated)
3190 return;
3191
3192 if (enabled)
3193 tracing_start_cmdline_record();
3194 else
3195 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003196}
3197
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003198/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003199 * trace_vbprintk - write binary msg to tracing buffer
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07003200 * @ip: The address of the caller
3201 * @fmt: The string format to write to the buffer
3202 * @args: Arguments for @fmt
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003203 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003204int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003205{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003206 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003207 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003208 struct trace_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003209 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003210 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003211 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003212 char *tbuffer;
3213 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003214
3215 if (unlikely(tracing_selftest_running || tracing_disabled))
3216 return 0;
3217
3218 /* Don't pollute graph traces with trace_vprintk internals */
3219 pause_graph_tracing();
3220
3221 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003222 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003223
Steven Rostedt07d777f2011-09-22 14:01:55 -04003224 tbuffer = get_trace_buf();
3225 if (!tbuffer) {
3226 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003227 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003228 }
3229
3230 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3231
3232 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Steven Rostedt (VMware)34423f22020-01-22 06:44:50 -05003233 goto out_put;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003234
Steven Rostedt07d777f2011-09-22 14:01:55 -04003235 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003236 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003237 buffer = tr->array_buffer.buffer;
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -05003238 ring_buffer_nest_start(buffer);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003239 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3240 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003241 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003242 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003243 entry = ring_buffer_event_data(event);
3244 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003245 entry->fmt = fmt;
3246
Steven Rostedt07d777f2011-09-22 14:01:55 -04003247 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05003248 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003249 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003250 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003251 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003252
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003253out:
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -05003254 ring_buffer_nest_end(buffer);
Steven Rostedt (VMware)34423f22020-01-22 06:44:50 -05003255out_put:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003256 put_trace_buf();
3257
3258out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003259 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003260 unpause_graph_tracing();
3261
3262 return len;
3263}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003264EXPORT_SYMBOL_GPL(trace_vbprintk);
3265
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003266__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003267static int
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003268__trace_array_vprintk(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003269 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003270{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003271 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003272 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003273 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003274 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003275 unsigned long flags;
3276 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003277
3278 if (tracing_disabled || tracing_selftest_running)
3279 return 0;
3280
Steven Rostedt07d777f2011-09-22 14:01:55 -04003281 /* Don't pollute graph traces with trace_vprintk internals */
3282 pause_graph_tracing();
3283
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003284 pc = preempt_count();
3285 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003286
Steven Rostedt07d777f2011-09-22 14:01:55 -04003287
3288 tbuffer = get_trace_buf();
3289 if (!tbuffer) {
3290 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003291 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003292 }
3293
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003294 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003295
Steven Rostedt07d777f2011-09-22 14:01:55 -04003296 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003297 size = sizeof(*entry) + len + 1;
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -05003298 ring_buffer_nest_start(buffer);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003299 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3300 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003301 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003302 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003303 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01003304 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003305
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003306 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05003307 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003308 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003309 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003310 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003311
3312out:
Steven Rostedt (VMware)82d1b812020-01-16 08:20:18 -05003313 ring_buffer_nest_end(buffer);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003314 put_trace_buf();
3315
3316out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003317 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003318 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003319
3320 return len;
3321}
Steven Rostedt659372d2009-09-03 19:11:07 -04003322
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003323__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003324int trace_array_vprintk(struct trace_array *tr,
3325 unsigned long ip, const char *fmt, va_list args)
3326{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003327 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003328}
3329
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003330__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003331int trace_array_printk(struct trace_array *tr,
3332 unsigned long ip, const char *fmt, ...)
3333{
3334 int ret;
3335 va_list ap;
3336
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003337 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003338 return 0;
3339
Divya Indi953ae452019-08-14 10:55:25 -07003340 if (!tr)
3341 return -ENOENT;
3342
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003343 va_start(ap, fmt);
3344 ret = trace_array_vprintk(tr, ip, fmt, ap);
3345 va_end(ap);
3346 return ret;
3347}
Divya Indif45d1222019-03-20 11:28:51 -07003348EXPORT_SYMBOL_GPL(trace_array_printk);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003349
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003350__printf(3, 4)
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003351int trace_array_printk_buf(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003352 unsigned long ip, const char *fmt, ...)
3353{
3354 int ret;
3355 va_list ap;
3356
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003357 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003358 return 0;
3359
3360 va_start(ap, fmt);
3361 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3362 va_end(ap);
3363 return ret;
3364}
3365
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003366__printf(2, 0)
Steven Rostedt659372d2009-09-03 19:11:07 -04003367int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3368{
Steven Rostedta813a152009-10-09 01:41:35 -04003369 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04003370}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003371EXPORT_SYMBOL_GPL(trace_vprintk);
3372
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003373static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04003374{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003375 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3376
Steven Rostedt5a90f572008-09-03 17:42:51 -04003377 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003378 if (buf_iter)
3379 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04003380}
3381
Ingo Molnare309b412008-05-12 21:20:51 +02003382static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003383peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3384 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003385{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003386 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003387 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003388
Steven Rostedtd7690412008-10-01 00:29:53 -04003389 if (buf_iter)
3390 event = ring_buffer_iter_peek(buf_iter, ts);
3391 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003392 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003393 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04003394
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04003395 if (event) {
3396 iter->ent_size = ring_buffer_event_length(event);
3397 return ring_buffer_event_data(event);
3398 }
3399 iter->ent_size = 0;
3400 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003401}
Steven Rostedtd7690412008-10-01 00:29:53 -04003402
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003403static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003404__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3405 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003406{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003407 struct trace_buffer *buffer = iter->array_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003408 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08003409 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003410 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003411 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003412 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003413 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003414 int cpu;
3415
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003416 /*
3417 * If we are in a per_cpu trace file, don't bother by iterating over
3418 * all cpu and peek directly.
3419 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003420 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003421 if (ring_buffer_empty_cpu(buffer, cpu_file))
3422 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003423 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003424 if (ent_cpu)
3425 *ent_cpu = cpu_file;
3426
3427 return ent;
3428 }
3429
Steven Rostedtab464282008-05-12 21:21:00 +02003430 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003431
3432 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003433 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003434
Steven Rostedtbc21b472010-03-31 19:49:26 -04003435 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003436
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02003437 /*
3438 * Pick the entry with the smallest timestamp:
3439 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003440 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003441 next = ent;
3442 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003443 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003444 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003445 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446 }
3447 }
3448
Steven Rostedt12b5da32012-03-27 10:43:28 -04003449 iter->ent_size = next_size;
3450
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003451 if (ent_cpu)
3452 *ent_cpu = next_cpu;
3453
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003454 if (ent_ts)
3455 *ent_ts = next_ts;
3456
Steven Rostedtbc21b472010-03-31 19:49:26 -04003457 if (missing_events)
3458 *missing_events = next_lost;
3459
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003460 return next;
3461}
3462
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003463/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003464struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3465 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003466{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003467 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003468}
Ingo Molnar8c523a92008-05-12 21:20:46 +02003469
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003470/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05003471void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003472{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003473 iter->ent = __find_next_entry(iter, &iter->cpu,
3474 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003475
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003476 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003477 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003478
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003479 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003480}
3481
Ingo Molnare309b412008-05-12 21:20:51 +02003482static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003483{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003484 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003485 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003486}
3487
Ingo Molnare309b412008-05-12 21:20:51 +02003488static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003489{
3490 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003491 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003492 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003493
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003494 WARN_ON_ONCE(iter->leftover);
3495
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003496 (*pos)++;
3497
3498 /* can't go backwards */
3499 if (iter->idx > i)
3500 return NULL;
3501
3502 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05003503 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003504 else
3505 ent = iter;
3506
3507 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05003508 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003509
3510 iter->pos = *pos;
3511
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003512 return ent;
3513}
3514
Jason Wessel955b61e2010-08-05 09:22:23 -05003515void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003516{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003517 struct ring_buffer_event *event;
3518 struct ring_buffer_iter *buf_iter;
3519 unsigned long entries = 0;
3520 u64 ts;
3521
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003522 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003523
Steven Rostedt6d158a82012-06-27 20:46:14 -04003524 buf_iter = trace_buffer_iter(iter, cpu);
3525 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003526 return;
3527
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003528 ring_buffer_iter_reset(buf_iter);
3529
3530 /*
3531 * We could have the case with the max latency tracers
3532 * that a reset never took place on a cpu. This is evident
3533 * by the timestamp being before the start of the buffer.
3534 */
3535 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003536 if (ts >= iter->array_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003537 break;
3538 entries++;
3539 ring_buffer_read(buf_iter, NULL);
3540 }
3541
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003542 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003543}
3544
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003545/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003546 * The current tracer is copied to avoid a global locking
3547 * all around.
3548 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003549static void *s_start(struct seq_file *m, loff_t *pos)
3550{
3551 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003552 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003553 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003554 void *p = NULL;
3555 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003556 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003557
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09003558 /*
3559 * copy the tracer to avoid using a global lock all around.
3560 * iter->trace is a copy of current_trace, the pointer to the
3561 * name may be used instead of a strcmp(), as iter->trace->name
3562 * will point to the same string as current_trace->name.
3563 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003564 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003565 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3566 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003567 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003568
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003569#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003570 if (iter->snapshot && iter->trace->use_max_tr)
3571 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003572#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003573
3574 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003575 atomic_inc(&trace_record_taskinfo_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003576
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003577 if (*pos != iter->pos) {
3578 iter->ent = NULL;
3579 iter->cpu = 0;
3580 iter->idx = -1;
3581
Steven Rostedtae3b5092013-01-23 15:22:59 -05003582 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003583 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003584 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003585 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003586 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003587
Lai Jiangshanac91d852010-03-02 17:54:50 +08003588 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003589 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3590 ;
3591
3592 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003593 /*
3594 * If we overflowed the seq_file before, then we want
3595 * to just reuse the trace_seq buffer again.
3596 */
3597 if (iter->leftover)
3598 p = iter;
3599 else {
3600 l = *pos - 1;
3601 p = s_next(m, p, &l);
3602 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003603 }
3604
Lai Jiangshan4f535962009-05-18 19:35:34 +08003605 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003606 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003607 return p;
3608}
3609
3610static void s_stop(struct seq_file *m, void *p)
3611{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003612 struct trace_iterator *iter = m->private;
3613
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003614#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003615 if (iter->snapshot && iter->trace->use_max_tr)
3616 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003617#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003618
3619 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003620 atomic_dec(&trace_record_taskinfo_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003621
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003622 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003623 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003624}
3625
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003626static void
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003627get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003628 unsigned long *entries, int cpu)
3629{
3630 unsigned long count;
3631
3632 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3633 /*
3634 * If this buffer has skipped entries, then we hold all
3635 * entries for the trace and we need to ignore the
3636 * ones before the time stamp.
3637 */
3638 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3639 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3640 /* total is the same as the entries */
3641 *total = count;
3642 } else
3643 *total = count +
3644 ring_buffer_overrun_cpu(buf->buffer, cpu);
3645 *entries = count;
3646}
3647
3648static void
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003649get_total_entries(struct array_buffer *buf,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003650 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003651{
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003652 unsigned long t, e;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003653 int cpu;
3654
3655 *total = 0;
3656 *entries = 0;
3657
3658 for_each_tracing_cpu(cpu) {
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003659 get_total_entries_cpu(buf, &t, &e, cpu);
3660 *total += t;
3661 *entries += e;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003662 }
3663}
3664
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003665unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3666{
3667 unsigned long total, entries;
3668
3669 if (!tr)
3670 tr = &global_trace;
3671
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003672 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003673
3674 return entries;
3675}
3676
3677unsigned long trace_total_entries(struct trace_array *tr)
3678{
3679 unsigned long total, entries;
3680
3681 if (!tr)
3682 tr = &global_trace;
3683
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003684 get_total_entries(&tr->array_buffer, &total, &entries);
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003685
3686 return entries;
3687}
3688
Ingo Molnare309b412008-05-12 21:20:51 +02003689static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003690{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003691 seq_puts(m, "# _------=> CPU# \n"
3692 "# / _-----=> irqs-off \n"
3693 "# | / _----=> need-resched \n"
3694 "# || / _---=> hardirq/softirq \n"
3695 "# ||| / _--=> preempt-depth \n"
3696 "# |||| / delay \n"
3697 "# cmd pid ||||| time | caller \n"
3698 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003699}
3700
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003701static void print_event_info(struct array_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003702{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003703 unsigned long total;
3704 unsigned long entries;
3705
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003706 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003707 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3708 entries, total, num_online_cpus());
3709 seq_puts(m, "#\n");
3710}
3711
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003712static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
Joel Fernandes441dae82017-06-25 22:38:43 -07003713 unsigned int flags)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003714{
Joel Fernandes441dae82017-06-25 22:38:43 -07003715 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3716
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003717 print_event_info(buf, m);
Joel Fernandes441dae82017-06-25 22:38:43 -07003718
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003719 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3720 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003721}
3722
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003723static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
Joel Fernandes441dae82017-06-25 22:38:43 -07003724 unsigned int flags)
Steven Rostedt77271ce2011-11-17 09:34:33 -05003725{
Joel Fernandes441dae82017-06-25 22:38:43 -07003726 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003727 const char *space = " ";
3728 int prec = tgid ? 10 : 2;
Joel Fernandes441dae82017-06-25 22:38:43 -07003729
Quentin Perret9e738212019-02-14 15:29:50 +00003730 print_event_info(buf, m);
3731
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003732 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3733 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3734 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3735 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3736 seq_printf(m, "# %.*s||| / delay\n", prec, space);
3737 seq_printf(m, "# TASK-PID %.*sCPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3738 seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
Steven Rostedt77271ce2011-11-17 09:34:33 -05003739}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003740
Jiri Olsa62b915f2010-04-02 19:01:22 +02003741void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003742print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3743{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003744 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003745 struct array_buffer *buf = iter->array_buffer;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003746 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003747 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003748 unsigned long entries;
3749 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003750 const char *name = "preemption";
3751
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05003752 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003753
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003754 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003755
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003756 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003757 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003758 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003759 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003760 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003761 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02003762 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003763 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003764 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003765 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003766#if defined(CONFIG_PREEMPT_NONE)
3767 "server",
3768#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3769 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04003770#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003771 "preempt",
Sebastian Andrzej Siewior9c34fc42019-10-15 21:18:20 +02003772#elif defined(CONFIG_PREEMPT_RT)
3773 "preempt_rt",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003774#else
3775 "unknown",
3776#endif
3777 /* These are reserved for later use */
3778 0, 0, 0, 0);
3779#ifdef CONFIG_SMP
3780 seq_printf(m, " #P:%d)\n", num_online_cpus());
3781#else
3782 seq_puts(m, ")\n");
3783#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003784 seq_puts(m, "# -----------------\n");
3785 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003786 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07003787 data->comm, data->pid,
3788 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003789 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003790 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003791
3792 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003793 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003794 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3795 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003796 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003797 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3798 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04003799 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003800 }
3801
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003802 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003803}
3804
Steven Rostedta3097202008-11-07 22:36:02 -05003805static void test_cpu_buff_start(struct trace_iterator *iter)
3806{
3807 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003808 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003809
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003810 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003811 return;
3812
3813 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3814 return;
3815
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003816 if (cpumask_available(iter->started) &&
3817 cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003818 return;
3819
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003820 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003821 return;
3822
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003823 if (cpumask_available(iter->started))
Sasha Levin919cd972015-09-04 12:45:56 -04003824 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003825
3826 /* Don't print started cpu buffer for the first entry of the trace */
3827 if (iter->idx > 1)
3828 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3829 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003830}
3831
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003832static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003833{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003834 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003835 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003836 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003837 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003838 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003839
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003840 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003841
Steven Rostedta3097202008-11-07 22:36:02 -05003842 test_cpu_buff_start(iter);
3843
Steven Rostedtf633cef2008-12-23 23:24:13 -05003844 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003845
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003846 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003847 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3848 trace_print_lat_context(iter);
3849 else
3850 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003851 }
3852
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003853 if (trace_seq_has_overflowed(s))
3854 return TRACE_TYPE_PARTIAL_LINE;
3855
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003856 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003857 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003858
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003859 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003860
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003861 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003862}
3863
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003864static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003865{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003866 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003867 struct trace_seq *s = &iter->seq;
3868 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003869 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003870
3871 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003872
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003873 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003874 trace_seq_printf(s, "%d %d %llu ",
3875 entry->pid, iter->cpu, iter->ts);
3876
3877 if (trace_seq_has_overflowed(s))
3878 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003879
Steven Rostedtf633cef2008-12-23 23:24:13 -05003880 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003881 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003882 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003883
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003884 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003885
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003886 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003887}
3888
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003889static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003890{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003891 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003892 struct trace_seq *s = &iter->seq;
3893 unsigned char newline = '\n';
3894 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003895 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003896
3897 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003898
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003899 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003900 SEQ_PUT_HEX_FIELD(s, entry->pid);
3901 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3902 SEQ_PUT_HEX_FIELD(s, iter->ts);
3903 if (trace_seq_has_overflowed(s))
3904 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003905 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003906
Steven Rostedtf633cef2008-12-23 23:24:13 -05003907 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003908 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003909 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003910 if (ret != TRACE_TYPE_HANDLED)
3911 return ret;
3912 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003913
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003914 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003915
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003916 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003917}
3918
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003919static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003920{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003921 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003922 struct trace_seq *s = &iter->seq;
3923 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003924 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003925
3926 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003927
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003928 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003929 SEQ_PUT_FIELD(s, entry->pid);
3930 SEQ_PUT_FIELD(s, iter->cpu);
3931 SEQ_PUT_FIELD(s, iter->ts);
3932 if (trace_seq_has_overflowed(s))
3933 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003934 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003935
Steven Rostedtf633cef2008-12-23 23:24:13 -05003936 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003937 return event ? event->funcs->binary(iter, 0, event) :
3938 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003939}
3940
Jiri Olsa62b915f2010-04-02 19:01:22 +02003941int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003942{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003943 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003944 int cpu;
3945
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003946 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003947 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003948 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003949 buf_iter = trace_buffer_iter(iter, cpu);
3950 if (buf_iter) {
3951 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003952 return 0;
3953 } else {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003954 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003955 return 0;
3956 }
3957 return 1;
3958 }
3959
Steven Rostedtab464282008-05-12 21:21:00 +02003960 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003961 buf_iter = trace_buffer_iter(iter, cpu);
3962 if (buf_iter) {
3963 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003964 return 0;
3965 } else {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003966 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003967 return 0;
3968 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003969 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003970
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003971 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003972}
3973
Lai Jiangshan4f535962009-05-18 19:35:34 +08003974/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003975enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003976{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003977 struct trace_array *tr = iter->tr;
3978 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003979 enum print_line_t ret;
3980
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003981 if (iter->lost_events) {
3982 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3983 iter->cpu, iter->lost_events);
3984 if (trace_seq_has_overflowed(&iter->seq))
3985 return TRACE_TYPE_PARTIAL_LINE;
3986 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003987
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003988 if (iter->trace && iter->trace->print_line) {
3989 ret = iter->trace->print_line(iter);
3990 if (ret != TRACE_TYPE_UNHANDLED)
3991 return ret;
3992 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003993
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003994 if (iter->ent->type == TRACE_BPUTS &&
3995 trace_flags & TRACE_ITER_PRINTK &&
3996 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3997 return trace_print_bputs_msg_only(iter);
3998
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003999 if (iter->ent->type == TRACE_BPRINT &&
4000 trace_flags & TRACE_ITER_PRINTK &&
4001 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04004002 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01004003
Frederic Weisbecker66896a82008-12-13 20:18:13 +01004004 if (iter->ent->type == TRACE_PRINT &&
4005 trace_flags & TRACE_ITER_PRINTK &&
4006 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04004007 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01004008
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02004009 if (trace_flags & TRACE_ITER_BIN)
4010 return print_bin_fmt(iter);
4011
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02004012 if (trace_flags & TRACE_ITER_HEX)
4013 return print_hex_fmt(iter);
4014
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004015 if (trace_flags & TRACE_ITER_RAW)
4016 return print_raw_fmt(iter);
4017
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004018 return print_trace_fmt(iter);
4019}
4020
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004021void trace_latency_header(struct seq_file *m)
4022{
4023 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004024 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004025
4026 /* print nothing if the buffers are empty */
4027 if (trace_empty(iter))
4028 return;
4029
4030 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4031 print_trace_header(m, iter);
4032
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004033 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004034 print_lat_help_header(m);
4035}
4036
Jiri Olsa62b915f2010-04-02 19:01:22 +02004037void trace_default_header(struct seq_file *m)
4038{
4039 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004040 struct trace_array *tr = iter->tr;
4041 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02004042
Jiri Olsaf56e7f82011-06-03 16:58:49 +02004043 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4044 return;
4045
Jiri Olsa62b915f2010-04-02 19:01:22 +02004046 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4047 /* print nothing if the buffers are empty */
4048 if (trace_empty(iter))
4049 return;
4050 print_trace_header(m, iter);
4051 if (!(trace_flags & TRACE_ITER_VERBOSE))
4052 print_lat_help_header(m);
4053 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05004054 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4055 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004056 print_func_help_header_irq(iter->array_buffer,
Joel Fernandes441dae82017-06-25 22:38:43 -07004057 m, trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05004058 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004059 print_func_help_header(iter->array_buffer, m,
Joel Fernandes441dae82017-06-25 22:38:43 -07004060 trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05004061 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02004062 }
4063}
4064
Steven Rostedte0a413f2011-09-29 21:26:16 -04004065static void test_ftrace_alive(struct seq_file *m)
4066{
4067 if (!ftrace_is_dead())
4068 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004069 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4070 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04004071}
4072
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004073#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004074static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004075{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004076 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
4077 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4078 "# Takes a snapshot of the main buffer.\n"
4079 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
4080 "# (Doesn't have to be '2' works with any number that\n"
4081 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004082}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004083
4084static void show_snapshot_percpu_help(struct seq_file *m)
4085{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004086 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004087#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004088 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4089 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004090#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004091 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4092 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004093#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004094 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
4095 "# (Doesn't have to be '2' works with any number that\n"
4096 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004097}
4098
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004099static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4100{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004101 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004102 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004103 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004104 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004105
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004106 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004107 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4108 show_snapshot_main_help(m);
4109 else
4110 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004111}
4112#else
4113/* Should never be called */
4114static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4115#endif
4116
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004117static int s_show(struct seq_file *m, void *v)
4118{
4119 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004120 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004121
4122 if (iter->ent == NULL) {
4123 if (iter->tr) {
4124 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4125 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04004126 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004127 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004128 if (iter->snapshot && trace_empty(iter))
4129 print_snapshot_help(m, iter);
4130 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004131 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02004132 else
4133 trace_default_header(m);
4134
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004135 } else if (iter->leftover) {
4136 /*
4137 * If we filled the seq_file buffer earlier, we
4138 * want to just show it now.
4139 */
4140 ret = trace_print_seq(m, &iter->seq);
4141
4142 /* ret should this time be zero, but you never know */
4143 iter->leftover = ret;
4144
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004145 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004146 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004147 ret = trace_print_seq(m, &iter->seq);
4148 /*
4149 * If we overflow the seq_file buffer, then it will
4150 * ask us for this data again at start up.
4151 * Use that instead.
4152 * ret is 0 if seq_file write succeeded.
4153 * -1 otherwise.
4154 */
4155 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004156 }
4157
4158 return 0;
4159}
4160
Oleg Nesterov649e9c702013-07-23 17:25:54 +02004161/*
4162 * Should be used after trace_array_get(), trace_types_lock
4163 * ensures that i_cdev was already initialized.
4164 */
4165static inline int tracing_get_cpu(struct inode *inode)
4166{
4167 if (inode->i_cdev) /* See trace_create_cpu_file() */
4168 return (long)inode->i_cdev - 1;
4169 return RING_BUFFER_ALL_CPUS;
4170}
4171
James Morris88e9d342009-09-22 16:43:43 -07004172static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004173 .start = s_start,
4174 .next = s_next,
4175 .stop = s_stop,
4176 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004177};
4178
Ingo Molnare309b412008-05-12 21:20:51 +02004179static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02004180__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004181{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004182 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004183 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02004184 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004185
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004186 if (tracing_disabled)
4187 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02004188
Jiri Olsa50e18b92012-04-25 10:23:39 +02004189 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004190 if (!iter)
4191 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004192
Gil Fruchter72917232015-06-09 10:32:35 +03004193 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04004194 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004195 if (!iter->buffer_iter)
4196 goto release;
4197
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004198 /*
4199 * We make a copy of the current tracer to avoid concurrent
4200 * changes on it while we are reading.
4201 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004202 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004203 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004204 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004205 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004206
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004207 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004208
Li Zefan79f55992009-06-15 14:58:26 +08004209 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004210 goto fail;
4211
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004212 iter->tr = tr;
4213
4214#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004215 /* Currently only the top directory has a snapshot */
4216 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004217 iter->array_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004218 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004219#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004220 iter->array_buffer = &tr->array_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004221 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004222 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02004223 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004224 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004225
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004226 /* Notify the tracer early; before we stop tracing. */
Dan Carpenterb3f7a6c2014-11-22 21:30:12 +03004227 if (iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01004228 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004229
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004230 /* Annotate start of buffers if we had overruns */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004231 if (ring_buffer_overruns(iter->array_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004232 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4233
David Sharp8be07092012-11-13 12:18:22 -08004234 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09004235 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08004236 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4237
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004238 /* stop the trace while dumping if we are not opening "snapshot" */
4239 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004240 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004241
Steven Rostedtae3b5092013-01-23 15:22:59 -05004242 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004243 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004244 iter->buffer_iter[cpu] =
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004245 ring_buffer_read_prepare(iter->array_buffer->buffer,
Douglas Anderson31b265b2019-03-08 11:32:04 -08004246 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004247 }
4248 ring_buffer_read_prepare_sync();
4249 for_each_tracing_cpu(cpu) {
4250 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004251 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004252 }
4253 } else {
4254 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004255 iter->buffer_iter[cpu] =
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004256 ring_buffer_read_prepare(iter->array_buffer->buffer,
Douglas Anderson31b265b2019-03-08 11:32:04 -08004257 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004258 ring_buffer_read_prepare_sync();
4259 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004260 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004261 }
4262
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004263 mutex_unlock(&trace_types_lock);
4264
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004265 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004266
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004267 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004268 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004269 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004270 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004271release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02004272 seq_release_private(inode, file);
4273 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004274}
4275
4276int tracing_open_generic(struct inode *inode, struct file *filp)
4277{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004278 int ret;
4279
4280 ret = tracing_check_open_get_tr(NULL);
4281 if (ret)
4282 return ret;
Steven Rostedt60a11772008-05-12 21:20:44 +02004283
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004284 filp->private_data = inode->i_private;
4285 return 0;
4286}
4287
Geyslan G. Bem2e864212013-10-18 21:15:54 -03004288bool tracing_is_disabled(void)
4289{
4290 return (tracing_disabled) ? true: false;
4291}
4292
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004293/*
4294 * Open and update trace_array ref count.
4295 * Must have the current trace_array passed to it.
4296 */
Steven Rostedt (VMware)aa07d712019-10-11 19:12:21 -04004297int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004298{
4299 struct trace_array *tr = inode->i_private;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004300 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004301
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004302 ret = tracing_check_open_get_tr(tr);
4303 if (ret)
4304 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004305
4306 filp->private_data = inode->i_private;
4307
4308 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004309}
4310
Hannes Eder4fd27352009-02-10 19:44:12 +01004311static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004312{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004313 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07004314 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004315 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004316 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004317
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004318 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004319 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004320 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004321 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004322
Oleg Nesterov6484c712013-07-23 17:26:10 +02004323 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004324 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004325 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004326
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004327 for_each_tracing_cpu(cpu) {
4328 if (iter->buffer_iter[cpu])
4329 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4330 }
4331
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004332 if (iter->trace && iter->trace->close)
4333 iter->trace->close(iter);
4334
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004335 if (!iter->snapshot)
4336 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004337 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004338
4339 __trace_array_put(tr);
4340
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004341 mutex_unlock(&trace_types_lock);
4342
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004343 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004344 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004345 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004346 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02004347 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004348
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004349 return 0;
4350}
4351
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004352static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4353{
4354 struct trace_array *tr = inode->i_private;
4355
4356 trace_array_put(tr);
4357 return 0;
4358}
4359
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004360static int tracing_single_release_tr(struct inode *inode, struct file *file)
4361{
4362 struct trace_array *tr = inode->i_private;
4363
4364 trace_array_put(tr);
4365
4366 return single_release(inode, file);
4367}
4368
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004369static int tracing_open(struct inode *inode, struct file *file)
4370{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004371 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004372 struct trace_iterator *iter;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004373 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004374
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004375 ret = tracing_check_open_get_tr(tr);
4376 if (ret)
4377 return ret;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004378
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004379 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02004380 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4381 int cpu = tracing_get_cpu(inode);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004382 struct array_buffer *trace_buf = &tr->array_buffer;
Bo Yan8dd33bc2017-09-18 10:03:35 -07004383
4384#ifdef CONFIG_TRACER_MAX_TRACE
4385 if (tr->current_trace->print_max)
4386 trace_buf = &tr->max_buffer;
4387#endif
Oleg Nesterov6484c712013-07-23 17:26:10 +02004388
4389 if (cpu == RING_BUFFER_ALL_CPUS)
Bo Yan8dd33bc2017-09-18 10:03:35 -07004390 tracing_reset_online_cpus(trace_buf);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004391 else
Steven Rostedt (VMware)a47b53e2019-08-13 12:14:35 -04004392 tracing_reset_cpu(trace_buf, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004393 }
4394
4395 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004396 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004397 if (IS_ERR(iter))
4398 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004399 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004400 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4401 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004402
4403 if (ret < 0)
4404 trace_array_put(tr);
4405
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004406 return ret;
4407}
4408
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004409/*
4410 * Some tracers are not suitable for instance buffers.
4411 * A tracer is always available for the global array (toplevel)
4412 * or if it explicitly states that it is.
4413 */
4414static bool
4415trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4416{
4417 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4418}
4419
4420/* Find the next tracer that this trace array may use */
4421static struct tracer *
4422get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4423{
4424 while (t && !trace_ok_for_array(t, tr))
4425 t = t->next;
4426
4427 return t;
4428}
4429
Ingo Molnare309b412008-05-12 21:20:51 +02004430static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004431t_next(struct seq_file *m, void *v, loff_t *pos)
4432{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004433 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004434 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004435
4436 (*pos)++;
4437
4438 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004439 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004440
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004441 return t;
4442}
4443
4444static void *t_start(struct seq_file *m, loff_t *pos)
4445{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004446 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004447 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004448 loff_t l = 0;
4449
4450 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004451
4452 t = get_tracer_for_array(tr, trace_types);
4453 for (; t && l < *pos; t = t_next(m, t, &l))
4454 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004455
4456 return t;
4457}
4458
4459static void t_stop(struct seq_file *m, void *p)
4460{
4461 mutex_unlock(&trace_types_lock);
4462}
4463
4464static int t_show(struct seq_file *m, void *v)
4465{
4466 struct tracer *t = v;
4467
4468 if (!t)
4469 return 0;
4470
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004471 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004472 if (t->next)
4473 seq_putc(m, ' ');
4474 else
4475 seq_putc(m, '\n');
4476
4477 return 0;
4478}
4479
James Morris88e9d342009-09-22 16:43:43 -07004480static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004481 .start = t_start,
4482 .next = t_next,
4483 .stop = t_stop,
4484 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004485};
4486
4487static int show_traces_open(struct inode *inode, struct file *file)
4488{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004489 struct trace_array *tr = inode->i_private;
4490 struct seq_file *m;
4491 int ret;
4492
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004493 ret = tracing_check_open_get_tr(tr);
4494 if (ret)
4495 return ret;
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004496
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004497 ret = seq_open(file, &show_traces_seq_ops);
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004498 if (ret) {
4499 trace_array_put(tr);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004500 return ret;
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004501 }
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004502
4503 m = file->private_data;
4504 m->private = tr;
4505
4506 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004507}
4508
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004509static int show_traces_release(struct inode *inode, struct file *file)
4510{
4511 struct trace_array *tr = inode->i_private;
4512
4513 trace_array_put(tr);
4514 return seq_release(inode, file);
4515}
4516
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004517static ssize_t
4518tracing_write_stub(struct file *filp, const char __user *ubuf,
4519 size_t count, loff_t *ppos)
4520{
4521 return count;
4522}
4523
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004524loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08004525{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004526 int ret;
4527
Slava Pestov364829b2010-11-24 15:13:16 -08004528 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004529 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08004530 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004531 file->f_pos = ret = 0;
4532
4533 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08004534}
4535
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004536static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004537 .open = tracing_open,
4538 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004539 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004540 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004541 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004542};
4543
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004544static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004545 .open = show_traces_open,
4546 .read = seq_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004547 .llseek = seq_lseek,
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004548 .release = show_traces_release,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004549};
4550
4551static ssize_t
4552tracing_cpumask_read(struct file *filp, char __user *ubuf,
4553 size_t count, loff_t *ppos)
4554{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004555 struct trace_array *tr = file_inode(filp)->i_private;
Changbin Du90e406f2017-11-30 11:39:43 +08004556 char *mask_str;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004557 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004558
Changbin Du90e406f2017-11-30 11:39:43 +08004559 len = snprintf(NULL, 0, "%*pb\n",
4560 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4561 mask_str = kmalloc(len, GFP_KERNEL);
4562 if (!mask_str)
4563 return -ENOMEM;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004564
Changbin Du90e406f2017-11-30 11:39:43 +08004565 len = snprintf(mask_str, len, "%*pb\n",
Tejun Heo1a402432015-02-13 14:37:39 -08004566 cpumask_pr_args(tr->tracing_cpumask));
4567 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004568 count = -EINVAL;
4569 goto out_err;
4570 }
Changbin Du90e406f2017-11-30 11:39:43 +08004571 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004572
4573out_err:
Changbin Du90e406f2017-11-30 11:39:43 +08004574 kfree(mask_str);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004575
4576 return count;
4577}
4578
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +09004579int tracing_set_cpumask(struct trace_array *tr,
4580 cpumask_var_t tracing_cpumask_new)
Ingo Molnarc7078de2008-05-12 21:20:52 +02004581{
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +09004582 int cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304583
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +09004584 if (!tr)
4585 return -EINVAL;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004586
Steven Rostedta5e25882008-12-02 15:34:05 -05004587 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004588 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02004589 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004590 /*
4591 * Increase/decrease the disabled counter if we are
4592 * about to flip a bit in the cpumask:
4593 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004594 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304595 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004596 atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4597 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004598 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004599 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304600 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004601 atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4602 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004603 }
4604 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004605 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05004606 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02004607
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004608 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +09004609
4610 return 0;
4611}
4612
4613static ssize_t
4614tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4615 size_t count, loff_t *ppos)
4616{
4617 struct trace_array *tr = file_inode(filp)->i_private;
4618 cpumask_var_t tracing_cpumask_new;
4619 int err;
4620
4621 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4622 return -ENOMEM;
4623
4624 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4625 if (err)
4626 goto err_free;
4627
4628 err = tracing_set_cpumask(tr, tracing_cpumask_new);
4629 if (err)
4630 goto err_free;
4631
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304632 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004633
Ingo Molnarc7078de2008-05-12 21:20:52 +02004634 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004635
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +09004636err_free:
Li Zefan215368e2009-06-15 10:56:42 +08004637 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004638
4639 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004640}
4641
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004642static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004643 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004644 .read = tracing_cpumask_read,
4645 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004646 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004647 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004648};
4649
Li Zefanfdb372e2009-12-08 11:15:59 +08004650static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004651{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004652 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004653 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004654 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004655 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004656
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004657 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004658 tracer_flags = tr->current_trace->flags->val;
4659 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004660
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004661 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004662 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08004663 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004664 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004665 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004666 }
4667
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004668 for (i = 0; trace_opts[i].name; i++) {
4669 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08004670 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004671 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004672 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004673 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004674 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004675
Li Zefanfdb372e2009-12-08 11:15:59 +08004676 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004677}
4678
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004679static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08004680 struct tracer_flags *tracer_flags,
4681 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004682{
Chunyu Hud39cdd22016-03-08 21:37:01 +08004683 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004684 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004685
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004686 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004687 if (ret)
4688 return ret;
4689
4690 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08004691 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004692 else
Zhaolei77708412009-08-07 18:53:21 +08004693 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004694 return 0;
4695}
4696
Li Zefan8d18eaa2009-12-08 11:17:06 +08004697/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004698static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08004699{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004700 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004701 struct tracer_flags *tracer_flags = trace->flags;
4702 struct tracer_opt *opts = NULL;
4703 int i;
4704
4705 for (i = 0; tracer_flags->opts[i].name; i++) {
4706 opts = &tracer_flags->opts[i];
4707
4708 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004709 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004710 }
4711
4712 return -EINVAL;
4713}
4714
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004715/* Some tracers require overwrite to stay enabled */
4716int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4717{
4718 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4719 return -1;
4720
4721 return 0;
4722}
4723
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004724int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004725{
Prateek Sood3a53acf2019-12-10 09:15:16 +00004726 if ((mask == TRACE_ITER_RECORD_TGID) ||
4727 (mask == TRACE_ITER_RECORD_CMD))
4728 lockdep_assert_held(&event_mutex);
4729
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004730 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004731 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004732 return 0;
4733
4734 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004735 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05004736 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004737 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004738
4739 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004740 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004741 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004742 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08004743
4744 if (mask == TRACE_ITER_RECORD_CMD)
4745 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08004746
Joel Fernandesd914ba32017-06-26 19:01:55 -07004747 if (mask == TRACE_ITER_RECORD_TGID) {
4748 if (!tgid_map)
Yuming Han6ee40512019-10-24 11:34:30 +08004749 tgid_map = kvcalloc(PID_MAX_DEFAULT + 1,
Kees Cook6396bb22018-06-12 14:03:40 -07004750 sizeof(*tgid_map),
Joel Fernandesd914ba32017-06-26 19:01:55 -07004751 GFP_KERNEL);
4752 if (!tgid_map) {
4753 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4754 return -ENOMEM;
4755 }
4756
4757 trace_event_enable_tgid_record(enabled);
4758 }
4759
Steven Rostedtc37775d2016-04-13 16:59:18 -04004760 if (mask == TRACE_ITER_EVENT_FORK)
4761 trace_event_follow_fork(tr, enabled);
4762
Namhyung Kim1e104862017-04-17 11:44:28 +09004763 if (mask == TRACE_ITER_FUNC_FORK)
4764 ftrace_pid_follow_fork(tr, enabled);
4765
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004766 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004767 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004768#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004769 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004770#endif
4771 }
Steven Rostedt81698832012-10-11 10:15:05 -04004772
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004773 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04004774 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004775 trace_printk_control(enabled);
4776 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004777
4778 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004779}
4780
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +09004781int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004782{
Li Zefan8d18eaa2009-12-08 11:17:06 +08004783 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004784 int neg = 0;
Yisheng Xie591a0332018-05-17 16:36:03 +08004785 int ret;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004786 size_t orig_len = strlen(option);
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004787 int len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004788
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004789 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004790
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004791 len = str_has_prefix(cmp, "no");
4792 if (len)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004793 neg = 1;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004794
4795 cmp += len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004796
Prateek Sood3a53acf2019-12-10 09:15:16 +00004797 mutex_lock(&event_mutex);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004798 mutex_lock(&trace_types_lock);
4799
Yisheng Xie591a0332018-05-17 16:36:03 +08004800 ret = match_string(trace_options, -1, cmp);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004801 /* If no option could be set, test the specific tracer options */
Yisheng Xie591a0332018-05-17 16:36:03 +08004802 if (ret < 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004803 ret = set_tracer_option(tr, cmp, neg);
Yisheng Xie591a0332018-05-17 16:36:03 +08004804 else
4805 ret = set_tracer_flag(tr, 1 << ret, !neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004806
4807 mutex_unlock(&trace_types_lock);
Prateek Sood3a53acf2019-12-10 09:15:16 +00004808 mutex_unlock(&event_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004809
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004810 /*
4811 * If the first trailing whitespace is replaced with '\0' by strstrip,
4812 * turn it back into a space.
4813 */
4814 if (orig_len > strlen(option))
4815 option[strlen(option)] = ' ';
4816
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004817 return ret;
4818}
4819
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004820static void __init apply_trace_boot_options(void)
4821{
4822 char *buf = trace_boot_options_buf;
4823 char *option;
4824
4825 while (true) {
4826 option = strsep(&buf, ",");
4827
4828 if (!option)
4829 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004830
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05004831 if (*option)
4832 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004833
4834 /* Put back the comma to allow this to be called again */
4835 if (buf)
4836 *(buf - 1) = ',';
4837 }
4838}
4839
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004840static ssize_t
4841tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4842 size_t cnt, loff_t *ppos)
4843{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004844 struct seq_file *m = filp->private_data;
4845 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004846 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004847 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004848
4849 if (cnt >= sizeof(buf))
4850 return -EINVAL;
4851
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004852 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004853 return -EFAULT;
4854
Steven Rostedta8dd2172013-01-09 20:54:17 -05004855 buf[cnt] = 0;
4856
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004857 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004858 if (ret < 0)
4859 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004860
Jiri Olsacf8517c2009-10-23 19:36:16 -04004861 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004862
4863 return cnt;
4864}
4865
Li Zefanfdb372e2009-12-08 11:15:59 +08004866static int tracing_trace_options_open(struct inode *inode, struct file *file)
4867{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004868 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004869 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004870
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004871 ret = tracing_check_open_get_tr(tr);
4872 if (ret)
4873 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004874
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004875 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4876 if (ret < 0)
4877 trace_array_put(tr);
4878
4879 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004880}
4881
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004882static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004883 .open = tracing_trace_options_open,
4884 .read = seq_read,
4885 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004886 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004887 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004888};
4889
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004890static const char readme_msg[] =
4891 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004892 "# echo 0 > tracing_on : quick way to disable tracing\n"
4893 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4894 " Important files:\n"
4895 " trace\t\t\t- The static contents of the buffer\n"
4896 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4897 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4898 " current_tracer\t- function and latency tracers\n"
4899 " available_tracers\t- list of configured tracers for current_tracer\n"
Tom Zanussia8d65572019-03-31 18:48:25 -05004900 " error_log\t- error log for failed commands (that support it)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004901 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4902 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4903 " trace_clock\t\t-change the clock used to order events\n"
4904 " local: Per cpu clock but may not be synced across CPUs\n"
4905 " global: Synced across CPUs but slows tracing down.\n"
4906 " counter: Not a clock, but just an increment\n"
4907 " uptime: Jiffy counter from time of boot\n"
4908 " perf: Same clock that perf events use\n"
4909#ifdef CONFIG_X86_64
4910 " x86-tsc: TSC cycle counter\n"
4911#endif
Tom Zanussi2c1ea602018-01-15 20:51:41 -06004912 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4913 " delta: Delta difference against a buffer-wide timestamp\n"
4914 " absolute: Absolute (standalone) timestamp\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004915 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
Steven Rostedtfa32e852016-07-06 15:25:08 -04004916 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004917 " tracing_cpumask\t- Limit which CPUs to trace\n"
4918 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4919 "\t\t\t Remove sub-buffer with rmdir\n"
4920 " trace_options\t\t- Set format or modify how tracing happens\n"
Srivatsa S. Bhat (VMware)b9416992019-01-28 17:55:53 -08004921 "\t\t\t Disable an option by prefixing 'no' to the\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004922 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004923 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004924#ifdef CONFIG_DYNAMIC_FTRACE
4925 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004926 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4927 "\t\t\t functions\n"
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09004928 "\t accepts: func_full_name or glob-matching-pattern\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004929 "\t modules: Can select a group via module\n"
4930 "\t Format: :mod:<module-name>\n"
4931 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4932 "\t triggers: a command to perform when function is hit\n"
4933 "\t Format: <function>:<trigger>[:count]\n"
4934 "\t trigger: traceon, traceoff\n"
4935 "\t\t enable_event:<system>:<event>\n"
4936 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004937#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004938 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004939#endif
4940#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004941 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004942#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004943 "\t\t dump\n"
4944 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004945 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4946 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4947 "\t The first one will disable tracing every time do_fault is hit\n"
4948 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4949 "\t The first time do trap is hit and it disables tracing, the\n"
4950 "\t counter will decrement to 2. If tracing is already disabled,\n"
4951 "\t the counter will not decrement. It only decrements when the\n"
4952 "\t trigger did work\n"
4953 "\t To remove trigger without count:\n"
4954 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4955 "\t To remove trigger with a count:\n"
4956 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004957 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004958 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4959 "\t modules: Can select a group via module command :mod:\n"
4960 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004961#endif /* CONFIG_DYNAMIC_FTRACE */
4962#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004963 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4964 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004965#endif
4966#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4967 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004968 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004969 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4970#endif
4971#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004972 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4973 "\t\t\t snapshot buffer. Read the contents for more\n"
4974 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004975#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004976#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004977 " stack_trace\t\t- Shows the max stack trace when active\n"
4978 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004979 "\t\t\t Write into this file to reset the max size (trigger a\n"
4980 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004981#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004982 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4983 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004984#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004985#endif /* CONFIG_STACK_TRACER */
Masami Hiramatsu5448d442018-11-05 18:02:08 +09004986#ifdef CONFIG_DYNAMIC_EVENTS
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09004987 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
Masami Hiramatsu5448d442018-11-05 18:02:08 +09004988 "\t\t\t Write into this file to define/undefine new trace events.\n"
4989#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004990#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09004991 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004992 "\t\t\t Write into this file to define/undefine new trace events.\n"
4993#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004994#ifdef CONFIG_UPROBE_EVENTS
Masami Hiramatsu41af3cf2019-06-20 00:07:58 +09004995 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004996 "\t\t\t Write into this file to define/undefine new trace events.\n"
4997#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004998#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Masami Hiramatsu86425622016-08-18 17:58:15 +09004999 "\t accepts: event-definitions (one definition per line)\n"
Masami Hiramatsuc3ca46e2017-05-23 15:05:50 +09005000 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
5001 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09005002#ifdef CONFIG_HIST_TRIGGERS
5003 "\t s:[synthetic/]<event> <field> [<field>]\n"
5004#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09005005 "\t -:[<group>/]<event>\n"
Anton Blanchard6b0b7552017-02-16 17:00:50 +11005006#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09005007 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Naveen N. Rao35b6f552017-02-22 19:23:39 +05305008 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09005009#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11005010#ifdef CONFIG_UPROBE_EVENTS
Ravi Bangoria1cc33162018-08-20 10:12:47 +05305011 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09005012#endif
5013 "\t args: <name>=fetcharg[:type]\n"
5014 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09005015#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09005016 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09005017#else
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09005018 "\t $stack<index>, $stack, $retval, $comm,\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09005019#endif
Masami Hiramatsua42e3c42019-06-20 00:08:37 +09005020 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +09005021 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
Masami Hiramatsu88903c42019-05-15 14:38:30 +09005022 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
Masami Hiramatsu40b53b72018-04-25 21:21:55 +09005023 "\t <type>\\[<array-size>\\]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09005024#ifdef CONFIG_HIST_TRIGGERS
5025 "\t field: <stype> <name>;\n"
5026 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5027 "\t [unsigned] char/int/long\n"
5028#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09005029#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06005030 " events/\t\t- Directory containing all trace event subsystems:\n"
5031 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
5032 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005033 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5034 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005035 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005036 " events/<system>/<event>/\t- Directory containing control files for\n"
5037 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005038 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
5039 " filter\t\t- If set, only events passing filter are traced\n"
5040 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005041 "\t Format: <trigger>[:count][if <filter>]\n"
5042 "\t trigger: traceon, traceoff\n"
5043 "\t enable_event:<system>:<event>\n"
5044 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06005045#ifdef CONFIG_HIST_TRIGGERS
5046 "\t enable_hist:<system>:<event>\n"
5047 "\t disable_hist:<system>:<event>\n"
5048#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06005049#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005050 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005051#endif
5052#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005053 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005054#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005055#ifdef CONFIG_HIST_TRIGGERS
5056 "\t\t hist (see below)\n"
5057#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005058 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
5059 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
5060 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
5061 "\t events/block/block_unplug/trigger\n"
5062 "\t The first disables tracing every time block_unplug is hit.\n"
5063 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
5064 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
5065 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
5066 "\t Like function triggers, the counter is only decremented if it\n"
5067 "\t enabled or disabled tracing.\n"
5068 "\t To remove a trigger without a count:\n"
5069 "\t echo '!<trigger> > <system>/<event>/trigger\n"
5070 "\t To remove a trigger with a count:\n"
5071 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
5072 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005073#ifdef CONFIG_HIST_TRIGGERS
5074 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005075 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06005076 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06005077 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005078 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005079 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005080 "\t [:name=histname1]\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005081 "\t [:<handler>.<action>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005082 "\t [if <filter>]\n\n"
5083 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06005084 "\t table using the key(s) and value(s) named, and the value of a\n"
5085 "\t sum called 'hitcount' is incremented. Keys and values\n"
5086 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06005087 "\t can be any field, or the special string 'stacktrace'.\n"
5088 "\t Compound keys consisting of up to two fields can be specified\n"
5089 "\t by the 'keys' keyword. Values must correspond to numeric\n"
5090 "\t fields. Sort keys consisting of up to two fields can be\n"
5091 "\t specified using the 'sort' keyword. The sort direction can\n"
5092 "\t be modified by appending '.descending' or '.ascending' to a\n"
5093 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005094 "\t or fewer than the default 2048 entries for the hashtable size.\n"
5095 "\t If a hist trigger is given a name using the 'name' parameter,\n"
5096 "\t its histogram data will be shared with other triggers of the\n"
5097 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005098 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06005099 "\t table in its entirety to stdout. If there are multiple hist\n"
5100 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005101 "\t trigger in the output. The table displayed for a named\n"
5102 "\t trigger will be the same as any other instance having the\n"
5103 "\t same name. The default format used to display a given field\n"
5104 "\t can be modified by appending any of the following modifiers\n"
5105 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06005106 "\t .hex display a number as a hex value\n"
5107 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005108 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06005109 "\t .execname display a common_pid as a program name\n"
Tom Zanussi860f9f62018-01-15 20:51:48 -06005110 "\t .syscall display a syscall id as a syscall name\n"
5111 "\t .log2 display log2 value rather than raw number\n"
5112 "\t .usecs display a common_timestamp in microseconds\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06005113 "\t The 'pause' parameter can be used to pause an existing hist\n"
5114 "\t trigger or to start a hist trigger but not log any events\n"
5115 "\t until told to do so. 'continue' can be used to start or\n"
5116 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005117 "\t The 'clear' parameter will clear the contents of a running\n"
5118 "\t hist trigger and leave its current paused/active state\n"
5119 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06005120 "\t The enable_hist and disable_hist triggers can be used to\n"
5121 "\t have one event conditionally start and stop another event's\n"
Colin Ian King9e5a36a2019-02-17 22:32:22 +00005122 "\t already-attached hist trigger. The syntax is analogous to\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005123 "\t the enable_event and disable_event triggers.\n\n"
5124 "\t Hist trigger handlers and actions are executed whenever a\n"
5125 "\t a histogram entry is added or updated. They take the form:\n\n"
5126 "\t <handler>.<action>\n\n"
5127 "\t The available handlers are:\n\n"
5128 "\t onmatch(matching.event) - invoke on addition or update\n"
Tom Zanussidff81f52019-02-13 17:42:48 -06005129 "\t onmax(var) - invoke if var exceeds current max\n"
5130 "\t onchange(var) - invoke action if var changes\n\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005131 "\t The available actions are:\n\n"
Tom Zanussie91eefd72019-02-13 17:42:50 -06005132 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005133 "\t save(field,...) - save current event fields\n"
Tom Zanussia3785b72019-02-13 17:42:46 -06005134#ifdef CONFIG_TRACER_SNAPSHOT
5135 "\t snapshot() - snapshot the trace buffer\n"
5136#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005137#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005138;
5139
5140static ssize_t
5141tracing_readme_read(struct file *filp, char __user *ubuf,
5142 size_t cnt, loff_t *ppos)
5143{
5144 return simple_read_from_buffer(ubuf, cnt, ppos,
5145 readme_msg, strlen(readme_msg));
5146}
5147
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005148static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02005149 .open = tracing_open_generic,
5150 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005151 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005152};
5153
Michael Sartain99c621d2017-07-05 22:07:15 -06005154static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
5155{
5156 int *ptr = v;
5157
5158 if (*pos || m->count)
5159 ptr++;
5160
5161 (*pos)++;
5162
5163 for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) {
5164 if (trace_find_tgid(*ptr))
5165 return ptr;
5166 }
5167
5168 return NULL;
5169}
5170
5171static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
5172{
5173 void *v;
5174 loff_t l = 0;
5175
5176 if (!tgid_map)
5177 return NULL;
5178
5179 v = &tgid_map[0];
5180 while (l <= *pos) {
5181 v = saved_tgids_next(m, v, &l);
5182 if (!v)
5183 return NULL;
5184 }
5185
5186 return v;
5187}
5188
5189static void saved_tgids_stop(struct seq_file *m, void *v)
5190{
5191}
5192
5193static int saved_tgids_show(struct seq_file *m, void *v)
5194{
5195 int pid = (int *)v - tgid_map;
5196
5197 seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid));
5198 return 0;
5199}
5200
5201static const struct seq_operations tracing_saved_tgids_seq_ops = {
5202 .start = saved_tgids_start,
5203 .stop = saved_tgids_stop,
5204 .next = saved_tgids_next,
5205 .show = saved_tgids_show,
5206};
5207
5208static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5209{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005210 int ret;
5211
5212 ret = tracing_check_open_get_tr(NULL);
5213 if (ret)
5214 return ret;
Michael Sartain99c621d2017-07-05 22:07:15 -06005215
5216 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5217}
5218
5219
5220static const struct file_operations tracing_saved_tgids_fops = {
5221 .open = tracing_saved_tgids_open,
5222 .read = seq_read,
5223 .llseek = seq_lseek,
5224 .release = seq_release,
5225};
5226
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005227static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005228{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005229 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005230
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005231 if (*pos || m->count)
5232 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005233
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005234 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005235
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005236 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5237 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005238 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005239 continue;
5240
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005241 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005242 }
5243
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005244 return NULL;
5245}
Avadh Patel69abe6a2009-04-10 16:04:48 -04005246
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005247static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5248{
5249 void *v;
5250 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005251
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005252 preempt_disable();
5253 arch_spin_lock(&trace_cmdline_lock);
5254
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005255 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005256 while (l <= *pos) {
5257 v = saved_cmdlines_next(m, v, &l);
5258 if (!v)
5259 return NULL;
5260 }
5261
5262 return v;
5263}
5264
5265static void saved_cmdlines_stop(struct seq_file *m, void *v)
5266{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005267 arch_spin_unlock(&trace_cmdline_lock);
5268 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005269}
5270
5271static int saved_cmdlines_show(struct seq_file *m, void *v)
5272{
5273 char buf[TASK_COMM_LEN];
5274 unsigned int *pid = v;
5275
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005276 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005277 seq_printf(m, "%d %s\n", *pid, buf);
5278 return 0;
5279}
5280
5281static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5282 .start = saved_cmdlines_start,
5283 .next = saved_cmdlines_next,
5284 .stop = saved_cmdlines_stop,
5285 .show = saved_cmdlines_show,
5286};
5287
5288static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5289{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005290 int ret;
5291
5292 ret = tracing_check_open_get_tr(NULL);
5293 if (ret)
5294 return ret;
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005295
5296 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04005297}
5298
5299static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005300 .open = tracing_saved_cmdlines_open,
5301 .read = seq_read,
5302 .llseek = seq_lseek,
5303 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04005304};
5305
5306static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005307tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5308 size_t cnt, loff_t *ppos)
5309{
5310 char buf[64];
5311 int r;
5312
5313 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005314 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005315 arch_spin_unlock(&trace_cmdline_lock);
5316
5317 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5318}
5319
5320static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5321{
5322 kfree(s->saved_cmdlines);
5323 kfree(s->map_cmdline_to_pid);
5324 kfree(s);
5325}
5326
5327static int tracing_resize_saved_cmdlines(unsigned int val)
5328{
5329 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5330
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005331 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005332 if (!s)
5333 return -ENOMEM;
5334
5335 if (allocate_cmdlines_buffer(val, s) < 0) {
5336 kfree(s);
5337 return -ENOMEM;
5338 }
5339
5340 arch_spin_lock(&trace_cmdline_lock);
5341 savedcmd_temp = savedcmd;
5342 savedcmd = s;
5343 arch_spin_unlock(&trace_cmdline_lock);
5344 free_saved_cmdlines_buffer(savedcmd_temp);
5345
5346 return 0;
5347}
5348
5349static ssize_t
5350tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5351 size_t cnt, loff_t *ppos)
5352{
5353 unsigned long val;
5354 int ret;
5355
5356 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5357 if (ret)
5358 return ret;
5359
5360 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5361 if (!val || val > PID_MAX_DEFAULT)
5362 return -EINVAL;
5363
5364 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5365 if (ret < 0)
5366 return ret;
5367
5368 *ppos += cnt;
5369
5370 return cnt;
5371}
5372
5373static const struct file_operations tracing_saved_cmdlines_size_fops = {
5374 .open = tracing_open_generic,
5375 .read = tracing_saved_cmdlines_size_read,
5376 .write = tracing_saved_cmdlines_size_write,
5377};
5378
Jeremy Linton681bec02017-05-31 16:56:53 -05005379#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005380static union trace_eval_map_item *
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005381update_eval_map(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005382{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005383 if (!ptr->map.eval_string) {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005384 if (ptr->tail.next) {
5385 ptr = ptr->tail.next;
5386 /* Set ptr to the next real item (skip head) */
5387 ptr++;
5388 } else
5389 return NULL;
5390 }
5391 return ptr;
5392}
5393
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005394static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005395{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005396 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005397
5398 /*
5399 * Paranoid! If ptr points to end, we don't want to increment past it.
5400 * This really should never happen.
5401 */
Vasily Averin039958a2020-01-24 10:03:01 +03005402 (*pos)++;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005403 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005404 if (WARN_ON_ONCE(!ptr))
5405 return NULL;
5406
5407 ptr++;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005408 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005409
5410 return ptr;
5411}
5412
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005413static void *eval_map_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005414{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005415 union trace_eval_map_item *v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005416 loff_t l = 0;
5417
Jeremy Linton1793ed92017-05-31 16:56:46 -05005418 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005419
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005420 v = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005421 if (v)
5422 v++;
5423
5424 while (v && l < *pos) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005425 v = eval_map_next(m, v, &l);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005426 }
5427
5428 return v;
5429}
5430
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005431static void eval_map_stop(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005432{
Jeremy Linton1793ed92017-05-31 16:56:46 -05005433 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005434}
5435
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005436static int eval_map_show(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005437{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005438 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005439
5440 seq_printf(m, "%s %ld (%s)\n",
Jeremy Linton00f4b652017-05-31 16:56:43 -05005441 ptr->map.eval_string, ptr->map.eval_value,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005442 ptr->map.system);
5443
5444 return 0;
5445}
5446
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005447static const struct seq_operations tracing_eval_map_seq_ops = {
5448 .start = eval_map_start,
5449 .next = eval_map_next,
5450 .stop = eval_map_stop,
5451 .show = eval_map_show,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005452};
5453
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005454static int tracing_eval_map_open(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005455{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005456 int ret;
5457
5458 ret = tracing_check_open_get_tr(NULL);
5459 if (ret)
5460 return ret;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005461
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005462 return seq_open(filp, &tracing_eval_map_seq_ops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005463}
5464
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005465static const struct file_operations tracing_eval_map_fops = {
5466 .open = tracing_eval_map_open,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005467 .read = seq_read,
5468 .llseek = seq_lseek,
5469 .release = seq_release,
5470};
5471
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005472static inline union trace_eval_map_item *
Jeremy Linton5f60b352017-05-31 16:56:47 -05005473trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005474{
5475 /* Return tail of array given the head */
5476 return ptr + ptr->head.length + 1;
5477}
5478
5479static void
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005480trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005481 int len)
5482{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005483 struct trace_eval_map **stop;
5484 struct trace_eval_map **map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005485 union trace_eval_map_item *map_array;
5486 union trace_eval_map_item *ptr;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005487
5488 stop = start + len;
5489
5490 /*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005491 * The trace_eval_maps contains the map plus a head and tail item,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005492 * where the head holds the module and length of array, and the
5493 * tail holds a pointer to the next list.
5494 */
Kees Cook6da2ec52018-06-12 13:55:00 -07005495 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005496 if (!map_array) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005497 pr_warn("Unable to allocate trace eval mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005498 return;
5499 }
5500
Jeremy Linton1793ed92017-05-31 16:56:46 -05005501 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005502
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005503 if (!trace_eval_maps)
5504 trace_eval_maps = map_array;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005505 else {
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005506 ptr = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005507 for (;;) {
Jeremy Linton5f60b352017-05-31 16:56:47 -05005508 ptr = trace_eval_jmp_to_tail(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005509 if (!ptr->tail.next)
5510 break;
5511 ptr = ptr->tail.next;
5512
5513 }
5514 ptr->tail.next = map_array;
5515 }
5516 map_array->head.mod = mod;
5517 map_array->head.length = len;
5518 map_array++;
5519
5520 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5521 map_array->map = **map;
5522 map_array++;
5523 }
5524 memset(map_array, 0, sizeof(*map_array));
5525
Jeremy Linton1793ed92017-05-31 16:56:46 -05005526 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005527}
5528
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005529static void trace_create_eval_file(struct dentry *d_tracer)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005530{
Jeremy Linton681bec02017-05-31 16:56:53 -05005531 trace_create_file("eval_map", 0444, d_tracer,
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005532 NULL, &tracing_eval_map_fops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005533}
5534
Jeremy Linton681bec02017-05-31 16:56:53 -05005535#else /* CONFIG_TRACE_EVAL_MAP_FILE */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005536static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5537static inline void trace_insert_eval_map_file(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005538 struct trace_eval_map **start, int len) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05005539#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005540
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005541static void trace_insert_eval_map(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005542 struct trace_eval_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005543{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005544 struct trace_eval_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005545
5546 if (len <= 0)
5547 return;
5548
5549 map = start;
5550
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005551 trace_event_eval_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005552
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005553 trace_insert_eval_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005554}
5555
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005556static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005557tracing_set_trace_read(struct file *filp, char __user *ubuf,
5558 size_t cnt, loff_t *ppos)
5559{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005560 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005561 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005562 int r;
5563
5564 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005565 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005566 mutex_unlock(&trace_types_lock);
5567
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005568 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005569}
5570
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005571int tracer_init(struct tracer *t, struct trace_array *tr)
5572{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005573 tracing_reset_online_cpus(&tr->array_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005574 return t->init(tr);
5575}
5576
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005577static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005578{
5579 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005580
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005581 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005582 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005583}
5584
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005585#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09005586/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005587static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5588 struct array_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09005589{
5590 int cpu, ret = 0;
5591
5592 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5593 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005594 ret = ring_buffer_resize(trace_buf->buffer,
5595 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005596 if (ret < 0)
5597 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005598 per_cpu_ptr(trace_buf->data, cpu)->entries =
5599 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005600 }
5601 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005602 ret = ring_buffer_resize(trace_buf->buffer,
5603 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005604 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005605 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5606 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005607 }
5608
5609 return ret;
5610}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005611#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09005612
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005613static int __tracing_resize_ring_buffer(struct trace_array *tr,
5614 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04005615{
5616 int ret;
5617
5618 /*
5619 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04005620 * we use the size that was given, and we can forget about
5621 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04005622 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005623 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04005624
Steven Rostedtb382ede62012-10-10 21:44:34 -04005625 /* May be called before buffers are initialized */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005626 if (!tr->array_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04005627 return 0;
5628
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005629 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005630 if (ret < 0)
5631 return ret;
5632
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005633#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005634 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5635 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005636 goto out;
5637
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005638 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005639 if (ret < 0) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005640 int r = resize_buffer_duplicate_size(&tr->array_buffer,
5641 &tr->array_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005642 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04005643 /*
5644 * AARGH! We are left with different
5645 * size max buffer!!!!
5646 * The max buffer is our "snapshot" buffer.
5647 * When a tracer needs a snapshot (one of the
5648 * latency tracers), it swaps the max buffer
5649 * with the saved snap shot. We succeeded to
5650 * update the size of the main buffer, but failed to
5651 * update the size of the max buffer. But when we tried
5652 * to reset the main buffer to the original size, we
5653 * failed there too. This is very unlikely to
5654 * happen, but if it does, warn and kill all
5655 * tracing.
5656 */
Steven Rostedt73c51622009-03-11 13:42:01 -04005657 WARN_ON(1);
5658 tracing_disabled = 1;
5659 }
5660 return ret;
5661 }
5662
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005663 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005664 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005665 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005666 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005667
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005668 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005669#endif /* CONFIG_TRACER_MAX_TRACE */
5670
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005671 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005672 set_buffer_entries(&tr->array_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005673 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005674 per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04005675
5676 return ret;
5677}
5678
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +09005679ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5680 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005681{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07005682 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005683
5684 mutex_lock(&trace_types_lock);
5685
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005686 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5687 /* make sure, this cpu is enabled in the mask */
5688 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5689 ret = -EINVAL;
5690 goto out;
5691 }
5692 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005693
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005694 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005695 if (ret < 0)
5696 ret = -ENOMEM;
5697
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005698out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005699 mutex_unlock(&trace_types_lock);
5700
5701 return ret;
5702}
5703
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005704
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005705/**
5706 * tracing_update_buffers - used by tracing facility to expand ring buffers
5707 *
5708 * To save on memory when the tracing is never used on a system with it
5709 * configured in. The ring buffers are set to a minimum size. But once
5710 * a user starts to use the tracing facility, then they need to grow
5711 * to their default size.
5712 *
5713 * This function is to be called when a tracer is about to be used.
5714 */
5715int tracing_update_buffers(void)
5716{
5717 int ret = 0;
5718
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005719 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005720 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005721 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005722 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005723 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005724
5725 return ret;
5726}
5727
Steven Rostedt577b7852009-02-26 23:43:05 -05005728struct trace_option_dentry;
5729
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005730static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005731create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05005732
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005733/*
5734 * Used to clear out the tracer before deletion of an instance.
5735 * Must have trace_types_lock held.
5736 */
5737static void tracing_set_nop(struct trace_array *tr)
5738{
5739 if (tr->current_trace == &nop_trace)
5740 return;
5741
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005742 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005743
5744 if (tr->current_trace->reset)
5745 tr->current_trace->reset(tr);
5746
5747 tr->current_trace = &nop_trace;
5748}
5749
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04005750static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005751{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005752 /* Only enable if the directory has been created already. */
5753 if (!tr->dir)
5754 return;
5755
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005756 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005757}
5758
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +09005759int tracing_set_tracer(struct trace_array *tr, const char *buf)
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005760{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005761 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005762#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005763 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005764#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005765 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005766
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005767 mutex_lock(&trace_types_lock);
5768
Steven Rostedt73c51622009-03-11 13:42:01 -04005769 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005770 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005771 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04005772 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01005773 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04005774 ret = 0;
5775 }
5776
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005777 for (t = trace_types; t; t = t->next) {
5778 if (strcmp(t->name, buf) == 0)
5779 break;
5780 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005781 if (!t) {
5782 ret = -EINVAL;
5783 goto out;
5784 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005785 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005786 goto out;
5787
Tom Zanussia35873a2019-02-13 17:42:45 -06005788#ifdef CONFIG_TRACER_SNAPSHOT
5789 if (t->use_max_tr) {
5790 arch_spin_lock(&tr->max_lock);
5791 if (tr->cond_snapshot)
5792 ret = -EBUSY;
5793 arch_spin_unlock(&tr->max_lock);
5794 if (ret)
5795 goto out;
5796 }
5797#endif
Ziqian SUN (Zamir)c7b3ae02017-09-11 14:26:35 +08005798 /* Some tracers won't work on kernel command line */
5799 if (system_state < SYSTEM_RUNNING && t->noboot) {
5800 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5801 t->name);
5802 goto out;
5803 }
5804
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005805 /* Some tracers are only allowed for the top level buffer */
5806 if (!trace_ok_for_array(t, tr)) {
5807 ret = -EINVAL;
5808 goto out;
5809 }
5810
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005811 /* If trace pipe files are being read, we can't change the tracer */
5812 if (tr->current_trace->ref) {
5813 ret = -EBUSY;
5814 goto out;
5815 }
5816
Steven Rostedt9f029e82008-11-12 15:24:24 -05005817 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005818
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005819 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005820
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005821 if (tr->current_trace->reset)
5822 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05005823
Paul E. McKenney74401722018-11-06 18:44:52 -08005824 /* Current trace needs to be nop_trace before synchronize_rcu */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005825 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05005826
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005827#ifdef CONFIG_TRACER_MAX_TRACE
5828 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05005829
5830 if (had_max_tr && !t->use_max_tr) {
5831 /*
5832 * We need to make sure that the update_max_tr sees that
5833 * current_trace changed to nop_trace to keep it from
5834 * swapping the buffers after we resize it.
5835 * The update_max_tr is called from interrupts disabled
5836 * so a synchronized_sched() is sufficient.
5837 */
Paul E. McKenney74401722018-11-06 18:44:52 -08005838 synchronize_rcu();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005839 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005840 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005841#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005842
5843#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005844 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04005845 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005846 if (ret < 0)
5847 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005848 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005849#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05005850
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005851 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005852 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005853 if (ret)
5854 goto out;
5855 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005856
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005857 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005858 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05005859 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005860 out:
5861 mutex_unlock(&trace_types_lock);
5862
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005863 return ret;
5864}
5865
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005866static ssize_t
5867tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5868 size_t cnt, loff_t *ppos)
5869{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005870 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005871 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005872 int i;
5873 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005874 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005875
Steven Rostedt60063a62008-10-28 10:44:24 -04005876 ret = cnt;
5877
Li Zefanee6c2c12009-09-18 14:06:47 +08005878 if (cnt > MAX_TRACER_SIZE)
5879 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005880
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005881 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005882 return -EFAULT;
5883
5884 buf[cnt] = 0;
5885
5886 /* strip ending whitespace. */
5887 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5888 buf[i] = 0;
5889
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005890 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005891 if (err)
5892 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005893
Jiri Olsacf8517c2009-10-23 19:36:16 -04005894 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005895
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005896 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005897}
5898
5899static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005900tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5901 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005902{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005903 char buf[64];
5904 int r;
5905
Steven Rostedtcffae432008-05-12 21:21:00 +02005906 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005907 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02005908 if (r > sizeof(buf))
5909 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005910 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005911}
5912
5913static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005914tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5915 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005916{
Hannes Eder5e398412009-02-10 19:44:34 +01005917 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005918 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005919
Peter Huewe22fe9b52011-06-07 21:58:27 +02005920 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5921 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005922 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005923
5924 *ptr = val * 1000;
5925
5926 return cnt;
5927}
5928
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005929static ssize_t
5930tracing_thresh_read(struct file *filp, char __user *ubuf,
5931 size_t cnt, loff_t *ppos)
5932{
5933 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5934}
5935
5936static ssize_t
5937tracing_thresh_write(struct file *filp, const char __user *ubuf,
5938 size_t cnt, loff_t *ppos)
5939{
5940 struct trace_array *tr = filp->private_data;
5941 int ret;
5942
5943 mutex_lock(&trace_types_lock);
5944 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5945 if (ret < 0)
5946 goto out;
5947
5948 if (tr->current_trace->update_thresh) {
5949 ret = tr->current_trace->update_thresh(tr);
5950 if (ret < 0)
5951 goto out;
5952 }
5953
5954 ret = cnt;
5955out:
5956 mutex_unlock(&trace_types_lock);
5957
5958 return ret;
5959}
5960
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04005961#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Chen Gange428abb2015-11-10 05:15:15 +08005962
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005963static ssize_t
5964tracing_max_lat_read(struct file *filp, char __user *ubuf,
5965 size_t cnt, loff_t *ppos)
5966{
5967 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5968}
5969
5970static ssize_t
5971tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5972 size_t cnt, loff_t *ppos)
5973{
5974 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5975}
5976
Chen Gange428abb2015-11-10 05:15:15 +08005977#endif
5978
Steven Rostedtb3806b42008-05-12 21:20:46 +02005979static int tracing_open_pipe(struct inode *inode, struct file *filp)
5980{
Oleg Nesterov15544202013-07-23 17:25:57 +02005981 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005982 struct trace_iterator *iter;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005983 int ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005984
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005985 ret = tracing_check_open_get_tr(tr);
5986 if (ret)
5987 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005988
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005989 mutex_lock(&trace_types_lock);
5990
Steven Rostedtb3806b42008-05-12 21:20:46 +02005991 /* create a buffer to store the information to pass to userspace */
5992 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005993 if (!iter) {
5994 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005995 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005996 goto out;
5997 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005998
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005999 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006000 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006001
6002 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
6003 ret = -ENOMEM;
6004 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10306005 }
6006
Steven Rostedta3097202008-11-07 22:36:02 -05006007 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10306008 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05006009
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006010 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04006011 iter->iter_flags |= TRACE_FILE_LAT_FMT;
6012
David Sharp8be07092012-11-13 12:18:22 -08006013 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09006014 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08006015 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6016
Oleg Nesterov15544202013-07-23 17:25:57 +02006017 iter->tr = tr;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006018 iter->array_buffer = &tr->array_buffer;
Oleg Nesterov15544202013-07-23 17:25:57 +02006019 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006020 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006021 filp->private_data = iter;
6022
Steven Rostedt107bad82008-05-12 21:21:01 +02006023 if (iter->trace->pipe_open)
6024 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02006025
Arnd Bergmannb4447862010-07-07 23:40:11 +02006026 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006027
6028 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006029out:
6030 mutex_unlock(&trace_types_lock);
6031 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006032
6033fail:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006034 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006035 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006036 mutex_unlock(&trace_types_lock);
6037 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006038}
6039
6040static int tracing_release_pipe(struct inode *inode, struct file *file)
6041{
6042 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02006043 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006044
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006045 mutex_lock(&trace_types_lock);
6046
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006047 tr->current_trace->ref--;
6048
Steven Rostedt29bf4a52009-12-09 12:37:43 -05006049 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05006050 iter->trace->pipe_close(iter);
6051
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006052 mutex_unlock(&trace_types_lock);
6053
Rusty Russell44623442009-01-01 10:12:23 +10306054 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006055 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006056 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006057
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006058 trace_array_put(tr);
6059
Steven Rostedtb3806b42008-05-12 21:20:46 +02006060 return 0;
6061}
6062
Al Viro9dd95742017-07-03 00:42:43 -04006063static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006064trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006065{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006066 struct trace_array *tr = iter->tr;
6067
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006068 /* Iterators are static, they should be filled or empty */
6069 if (trace_buffer_iter(iter, iter->cpu_file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08006070 return EPOLLIN | EPOLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006071
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006072 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006073 /*
6074 * Always select as readable when in blocking mode
6075 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08006076 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006077 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006078 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006079 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006080}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006081
Al Viro9dd95742017-07-03 00:42:43 -04006082static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006083tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6084{
6085 struct trace_iterator *iter = filp->private_data;
6086
6087 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006088}
6089
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006090/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006091static int tracing_wait_pipe(struct file *filp)
6092{
6093 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006094 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006095
6096 while (trace_empty(iter)) {
6097
6098 if ((filp->f_flags & O_NONBLOCK)) {
6099 return -EAGAIN;
6100 }
6101
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006102 /*
Liu Bo250bfd32013-01-14 10:54:11 +08006103 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006104 * We still block if tracing is disabled, but we have never
6105 * read anything. This allows a user to cat this file, and
6106 * then enable tracing. But after we have read something,
6107 * we give an EOF when tracing is again disabled.
6108 *
6109 * iter->pos will be 0 if we haven't read anything.
6110 */
Tahsin Erdogan75df6e62017-09-17 03:23:48 -07006111 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006112 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04006113
6114 mutex_unlock(&iter->mutex);
6115
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05006116 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04006117
6118 mutex_lock(&iter->mutex);
6119
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006120 if (ret)
6121 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006122 }
6123
6124 return 1;
6125}
6126
Steven Rostedtb3806b42008-05-12 21:20:46 +02006127/*
6128 * Consumer reader.
6129 */
6130static ssize_t
6131tracing_read_pipe(struct file *filp, char __user *ubuf,
6132 size_t cnt, loff_t *ppos)
6133{
6134 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006135 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006136
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006137 /*
6138 * Avoid more than one consumer on a single file descriptor
6139 * This is just a matter of traces coherency, the ring buffer itself
6140 * is protected.
6141 */
6142 mutex_lock(&iter->mutex);
Steven Rostedt (Red Hat)12458002016-09-23 22:57:13 -04006143
6144 /* return any leftover data */
6145 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6146 if (sret != -EBUSY)
6147 goto out;
6148
6149 trace_seq_init(&iter->seq);
6150
Steven Rostedt107bad82008-05-12 21:21:01 +02006151 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006152 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6153 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02006154 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02006155 }
6156
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006157waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006158 sret = tracing_wait_pipe(filp);
6159 if (sret <= 0)
6160 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006161
6162 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006163 if (trace_empty(iter)) {
6164 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02006165 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006166 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02006167
6168 if (cnt >= PAGE_SIZE)
6169 cnt = PAGE_SIZE - 1;
6170
Steven Rostedt53d0aa72008-05-12 21:21:01 +02006171 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02006172 memset(&iter->seq, 0,
6173 sizeof(struct trace_iterator) -
6174 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04006175 cpumask_clear(iter->started);
Petr Mladekd303de12019-10-11 16:21:34 +02006176 trace_seq_init(&iter->seq);
Steven Rostedt4823ed72008-05-12 21:21:01 +02006177 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006178
Lai Jiangshan4f535962009-05-18 19:35:34 +08006179 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006180 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05006181 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02006182 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006183 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02006184
Ingo Molnarf9896bf2008-05-12 21:20:47 +02006185 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02006186 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02006187 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006188 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006189 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02006190 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01006191 if (ret != TRACE_TYPE_NO_CONSUME)
6192 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006193
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006194 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02006195 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01006196
6197 /*
6198 * Setting the full flag means we reached the trace_seq buffer
6199 * size and we should leave by partial output condition above.
6200 * One of the trace_seq_* functions is not used properly.
6201 */
6202 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6203 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006204 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006205 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006206 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02006207
Steven Rostedtb3806b42008-05-12 21:20:46 +02006208 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006209 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006210 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05006211 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006212
6213 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006214 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006215 * entries, go back to wait for more entries.
6216 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006217 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006218 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006219
Steven Rostedt107bad82008-05-12 21:21:01 +02006220out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006221 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02006222
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006223 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006224}
6225
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006226static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6227 unsigned int idx)
6228{
6229 __free_page(spd->pages[idx]);
6230}
6231
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006232static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006233 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05006234 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006235 .steal = generic_pipe_buf_steal,
6236 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006237};
6238
Steven Rostedt34cd4992009-02-09 12:06:29 -05006239static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006240tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006241{
6242 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006243 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006244 int ret;
6245
6246 /* Seq buffer is page-sized, exactly what we need. */
6247 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006248 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006249 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006250
6251 if (trace_seq_has_overflowed(&iter->seq)) {
6252 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006253 break;
6254 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006255
6256 /*
6257 * This should not be hit, because it should only
6258 * be set if the iter->seq overflowed. But check it
6259 * anyway to be safe.
6260 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05006261 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006262 iter->seq.seq.len = save_len;
6263 break;
6264 }
6265
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006266 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006267 if (rem < count) {
6268 rem = 0;
6269 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006270 break;
6271 }
6272
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08006273 if (ret != TRACE_TYPE_NO_CONSUME)
6274 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05006275 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05006276 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006277 rem = 0;
6278 iter->ent = NULL;
6279 break;
6280 }
6281 }
6282
6283 return rem;
6284}
6285
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006286static ssize_t tracing_splice_read_pipe(struct file *filp,
6287 loff_t *ppos,
6288 struct pipe_inode_info *pipe,
6289 size_t len,
6290 unsigned int flags)
6291{
Jens Axboe35f3d142010-05-20 10:43:18 +02006292 struct page *pages_def[PIPE_DEF_BUFFERS];
6293 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006294 struct trace_iterator *iter = filp->private_data;
6295 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006296 .pages = pages_def,
6297 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006298 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02006299 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006300 .ops = &tracing_pipe_buf_ops,
6301 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006302 };
6303 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006304 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006305 unsigned int i;
6306
Jens Axboe35f3d142010-05-20 10:43:18 +02006307 if (splice_grow_spd(pipe, &spd))
6308 return -ENOMEM;
6309
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006310 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006311
6312 if (iter->trace->splice_read) {
6313 ret = iter->trace->splice_read(iter, filp,
6314 ppos, pipe, len, flags);
6315 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006316 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006317 }
6318
6319 ret = tracing_wait_pipe(filp);
6320 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006321 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006322
Jason Wessel955b61e2010-08-05 09:22:23 -05006323 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006324 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006325 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006326 }
6327
Lai Jiangshan4f535962009-05-18 19:35:34 +08006328 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006329 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006330
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006331 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04006332 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006333 spd.pages[i] = alloc_page(GFP_KERNEL);
6334 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05006335 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006336
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006337 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006338
6339 /* Copy the data into the page, so we can start over. */
6340 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02006341 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006342 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006343 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006344 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006345 break;
6346 }
Jens Axboe35f3d142010-05-20 10:43:18 +02006347 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006348 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006349
Steven Rostedtf9520752009-03-02 14:04:40 -05006350 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006351 }
6352
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006353 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006354 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006355 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006356
6357 spd.nr_pages = i;
6358
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04006359 if (i)
6360 ret = splice_to_pipe(pipe, &spd);
6361 else
6362 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02006363out:
Eric Dumazet047fe362012-06-12 15:24:40 +02006364 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02006365 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006366
Steven Rostedt34cd4992009-02-09 12:06:29 -05006367out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006368 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02006369 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006370}
6371
Steven Rostedta98a3c32008-05-12 21:20:59 +02006372static ssize_t
6373tracing_entries_read(struct file *filp, char __user *ubuf,
6374 size_t cnt, loff_t *ppos)
6375{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006376 struct inode *inode = file_inode(filp);
6377 struct trace_array *tr = inode->i_private;
6378 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006379 char buf[64];
6380 int r = 0;
6381 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006382
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006383 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006384
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006385 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006386 int cpu, buf_size_same;
6387 unsigned long size;
6388
6389 size = 0;
6390 buf_size_same = 1;
6391 /* check if all cpu sizes are same */
6392 for_each_tracing_cpu(cpu) {
6393 /* fill in the size from first enabled cpu */
6394 if (size == 0)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006395 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6396 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006397 buf_size_same = 0;
6398 break;
6399 }
6400 }
6401
6402 if (buf_size_same) {
6403 if (!ring_buffer_expanded)
6404 r = sprintf(buf, "%lu (expanded: %lu)\n",
6405 size >> 10,
6406 trace_buf_size >> 10);
6407 else
6408 r = sprintf(buf, "%lu\n", size >> 10);
6409 } else
6410 r = sprintf(buf, "X\n");
6411 } else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006412 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006413
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006414 mutex_unlock(&trace_types_lock);
6415
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006416 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6417 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006418}
6419
6420static ssize_t
6421tracing_entries_write(struct file *filp, const char __user *ubuf,
6422 size_t cnt, loff_t *ppos)
6423{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006424 struct inode *inode = file_inode(filp);
6425 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006426 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006427 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006428
Peter Huewe22fe9b52011-06-07 21:58:27 +02006429 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6430 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02006431 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006432
6433 /* must have at least 1 entry */
6434 if (!val)
6435 return -EINVAL;
6436
Steven Rostedt1696b2b2008-11-13 00:09:35 -05006437 /* value is in KB */
6438 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006439 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006440 if (ret < 0)
6441 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006442
Jiri Olsacf8517c2009-10-23 19:36:16 -04006443 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006444
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006445 return cnt;
6446}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05006447
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006448static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006449tracing_total_entries_read(struct file *filp, char __user *ubuf,
6450 size_t cnt, loff_t *ppos)
6451{
6452 struct trace_array *tr = filp->private_data;
6453 char buf[64];
6454 int r, cpu;
6455 unsigned long size = 0, expanded_size = 0;
6456
6457 mutex_lock(&trace_types_lock);
6458 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006459 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006460 if (!ring_buffer_expanded)
6461 expanded_size += trace_buf_size >> 10;
6462 }
6463 if (ring_buffer_expanded)
6464 r = sprintf(buf, "%lu\n", size);
6465 else
6466 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6467 mutex_unlock(&trace_types_lock);
6468
6469 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6470}
6471
6472static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006473tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6474 size_t cnt, loff_t *ppos)
6475{
6476 /*
6477 * There is no need to read what the user has written, this function
6478 * is just to make sure that there is no error when "echo" is used
6479 */
6480
6481 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006482
6483 return cnt;
6484}
6485
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006486static int
6487tracing_free_buffer_release(struct inode *inode, struct file *filp)
6488{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006489 struct trace_array *tr = inode->i_private;
6490
Steven Rostedtcf30cf62011-06-14 22:44:07 -04006491 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006492 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07006493 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006494 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006495 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006496
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006497 trace_array_put(tr);
6498
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006499 return 0;
6500}
6501
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006502static ssize_t
6503tracing_mark_write(struct file *filp, const char __user *ubuf,
6504 size_t cnt, loff_t *fpos)
6505{
Alexander Z Lam2d716192013-07-01 15:31:24 -07006506 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04006507 struct ring_buffer_event *event;
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006508 enum event_trigger_type tt = ETT_NONE;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05006509 struct trace_buffer *buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04006510 struct print_entry *entry;
6511 unsigned long irq_flags;
Steven Rostedtd696b582011-09-22 11:50:27 -04006512 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006513 int size;
6514 int len;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006515
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006516/* Used in tracing_mark_raw_write() as well */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006517#define FAULTED_STR "<faulted>"
6518#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006519
Steven Rostedtc76f0692008-11-07 22:36:02 -05006520 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006521 return -EINVAL;
6522
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006523 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07006524 return -EINVAL;
6525
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006526 if (cnt > TRACE_BUF_SIZE)
6527 cnt = TRACE_BUF_SIZE;
6528
Steven Rostedtd696b582011-09-22 11:50:27 -04006529 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006530
Steven Rostedtd696b582011-09-22 11:50:27 -04006531 local_save_flags(irq_flags);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006532 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6533
6534 /* If less than "<faulted>", then make sure we can still add that */
6535 if (cnt < FAULTED_SIZE)
6536 size += FAULTED_SIZE - cnt;
6537
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006538 buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006539 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6540 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006541 if (unlikely(!event))
Steven Rostedtd696b582011-09-22 11:50:27 -04006542 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006543 return -EBADF;
Steven Rostedtd696b582011-09-22 11:50:27 -04006544
6545 entry = ring_buffer_event_data(event);
6546 entry->ip = _THIS_IP_;
6547
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006548 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6549 if (len) {
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006550 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006551 cnt = FAULTED_SIZE;
6552 written = -EFAULT;
Steven Rostedtd696b582011-09-22 11:50:27 -04006553 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006554 written = cnt;
6555 len = cnt;
Steven Rostedtd696b582011-09-22 11:50:27 -04006556
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006557 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6558 /* do not add \n before testing triggers, but add \0 */
6559 entry->buf[cnt] = '\0';
6560 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6561 }
6562
Steven Rostedtd696b582011-09-22 11:50:27 -04006563 if (entry->buf[cnt - 1] != '\n') {
6564 entry->buf[cnt] = '\n';
6565 entry->buf[cnt + 1] = '\0';
6566 } else
6567 entry->buf[cnt] = '\0';
6568
Steven Rostedt7ffbd482012-10-11 12:14:25 -04006569 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04006570
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006571 if (tt)
6572 event_triggers_post_call(tr->trace_marker_file, tt);
6573
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006574 if (written > 0)
6575 *fpos += written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006576
Steven Rostedtfa32e852016-07-06 15:25:08 -04006577 return written;
6578}
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006579
Steven Rostedtfa32e852016-07-06 15:25:08 -04006580/* Limit it for now to 3K (including tag) */
6581#define RAW_DATA_MAX_SIZE (1024*3)
6582
6583static ssize_t
6584tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6585 size_t cnt, loff_t *fpos)
6586{
6587 struct trace_array *tr = filp->private_data;
6588 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05006589 struct trace_buffer *buffer;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006590 struct raw_data_entry *entry;
6591 unsigned long irq_flags;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006592 ssize_t written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006593 int size;
6594 int len;
6595
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006596#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6597
Steven Rostedtfa32e852016-07-06 15:25:08 -04006598 if (tracing_disabled)
6599 return -EINVAL;
6600
6601 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6602 return -EINVAL;
6603
6604 /* The marker must at least have a tag id */
6605 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6606 return -EINVAL;
6607
6608 if (cnt > TRACE_BUF_SIZE)
6609 cnt = TRACE_BUF_SIZE;
6610
6611 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6612
Steven Rostedtfa32e852016-07-06 15:25:08 -04006613 local_save_flags(irq_flags);
6614 size = sizeof(*entry) + cnt;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006615 if (cnt < FAULT_SIZE_ID)
6616 size += FAULT_SIZE_ID - cnt;
6617
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006618 buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006619 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6620 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006621 if (!event)
Steven Rostedtfa32e852016-07-06 15:25:08 -04006622 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006623 return -EBADF;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006624
6625 entry = ring_buffer_event_data(event);
6626
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006627 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6628 if (len) {
6629 entry->id = -1;
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006630 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006631 written = -EFAULT;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006632 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006633 written = cnt;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006634
6635 __buffer_unlock_commit(buffer, event);
6636
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006637 if (written > 0)
6638 *fpos += written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006639
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02006640 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006641}
6642
Li Zefan13f16d22009-12-08 11:16:11 +08006643static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08006644{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006645 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08006646 int i;
6647
6648 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08006649 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08006650 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006651 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6652 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08006653 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08006654
Li Zefan13f16d22009-12-08 11:16:11 +08006655 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08006656}
6657
Tom Zanussid71bd342018-01-15 20:52:07 -06006658int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08006659{
Zhaolei5079f322009-08-25 16:12:56 +08006660 int i;
6661
Zhaolei5079f322009-08-25 16:12:56 +08006662 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6663 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6664 break;
6665 }
6666 if (i == ARRAY_SIZE(trace_clocks))
6667 return -EINVAL;
6668
Zhaolei5079f322009-08-25 16:12:56 +08006669 mutex_lock(&trace_types_lock);
6670
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006671 tr->clock_id = i;
6672
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006673 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08006674
David Sharp60303ed2012-10-11 16:27:52 -07006675 /*
6676 * New clock may not be consistent with the previous clock.
6677 * Reset the buffer so that it doesn't have incomparable timestamps.
6678 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006679 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006680
6681#ifdef CONFIG_TRACER_MAX_TRACE
Baohong Liu170b3b12017-09-05 16:57:19 -05006682 if (tr->max_buffer.buffer)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006683 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07006684 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006685#endif
David Sharp60303ed2012-10-11 16:27:52 -07006686
Zhaolei5079f322009-08-25 16:12:56 +08006687 mutex_unlock(&trace_types_lock);
6688
Steven Rostedte1e232c2014-02-10 23:38:46 -05006689 return 0;
6690}
6691
6692static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6693 size_t cnt, loff_t *fpos)
6694{
6695 struct seq_file *m = filp->private_data;
6696 struct trace_array *tr = m->private;
6697 char buf[64];
6698 const char *clockstr;
6699 int ret;
6700
6701 if (cnt >= sizeof(buf))
6702 return -EINVAL;
6703
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08006704 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05006705 return -EFAULT;
6706
6707 buf[cnt] = 0;
6708
6709 clockstr = strstrip(buf);
6710
6711 ret = tracing_set_clock(tr, clockstr);
6712 if (ret)
6713 return ret;
6714
Zhaolei5079f322009-08-25 16:12:56 +08006715 *fpos += cnt;
6716
6717 return cnt;
6718}
6719
Li Zefan13f16d22009-12-08 11:16:11 +08006720static int tracing_clock_open(struct inode *inode, struct file *file)
6721{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006722 struct trace_array *tr = inode->i_private;
6723 int ret;
6724
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006725 ret = tracing_check_open_get_tr(tr);
6726 if (ret)
6727 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006728
6729 ret = single_open(file, tracing_clock_show, inode->i_private);
6730 if (ret < 0)
6731 trace_array_put(tr);
6732
6733 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08006734}
6735
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006736static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6737{
6738 struct trace_array *tr = m->private;
6739
6740 mutex_lock(&trace_types_lock);
6741
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006742 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006743 seq_puts(m, "delta [absolute]\n");
6744 else
6745 seq_puts(m, "[delta] absolute\n");
6746
6747 mutex_unlock(&trace_types_lock);
6748
6749 return 0;
6750}
6751
6752static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6753{
6754 struct trace_array *tr = inode->i_private;
6755 int ret;
6756
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006757 ret = tracing_check_open_get_tr(tr);
6758 if (ret)
6759 return ret;
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006760
6761 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6762 if (ret < 0)
6763 trace_array_put(tr);
6764
6765 return ret;
6766}
6767
Tom Zanussi00b41452018-01-15 20:51:39 -06006768int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6769{
6770 int ret = 0;
6771
6772 mutex_lock(&trace_types_lock);
6773
6774 if (abs && tr->time_stamp_abs_ref++)
6775 goto out;
6776
6777 if (!abs) {
6778 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6779 ret = -EINVAL;
6780 goto out;
6781 }
6782
6783 if (--tr->time_stamp_abs_ref)
6784 goto out;
6785 }
6786
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006787 ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs);
Tom Zanussi00b41452018-01-15 20:51:39 -06006788
6789#ifdef CONFIG_TRACER_MAX_TRACE
6790 if (tr->max_buffer.buffer)
6791 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6792#endif
6793 out:
6794 mutex_unlock(&trace_types_lock);
6795
6796 return ret;
6797}
6798
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006799struct ftrace_buffer_info {
6800 struct trace_iterator iter;
6801 void *spare;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006802 unsigned int spare_cpu;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006803 unsigned int read;
6804};
6805
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006806#ifdef CONFIG_TRACER_SNAPSHOT
6807static int tracing_snapshot_open(struct inode *inode, struct file *file)
6808{
Oleg Nesterov6484c712013-07-23 17:26:10 +02006809 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006810 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006811 struct seq_file *m;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006812 int ret;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006813
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006814 ret = tracing_check_open_get_tr(tr);
6815 if (ret)
6816 return ret;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006817
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006818 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02006819 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006820 if (IS_ERR(iter))
6821 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006822 } else {
6823 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006824 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006825 m = kzalloc(sizeof(*m), GFP_KERNEL);
6826 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006827 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006828 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6829 if (!iter) {
6830 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006831 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006832 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006833 ret = 0;
6834
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006835 iter->tr = tr;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006836 iter->array_buffer = &tr->max_buffer;
Oleg Nesterov6484c712013-07-23 17:26:10 +02006837 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006838 m->private = iter;
6839 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006840 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006841out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006842 if (ret < 0)
6843 trace_array_put(tr);
6844
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006845 return ret;
6846}
6847
6848static ssize_t
6849tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6850 loff_t *ppos)
6851{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006852 struct seq_file *m = filp->private_data;
6853 struct trace_iterator *iter = m->private;
6854 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006855 unsigned long val;
6856 int ret;
6857
6858 ret = tracing_update_buffers();
6859 if (ret < 0)
6860 return ret;
6861
6862 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6863 if (ret)
6864 return ret;
6865
6866 mutex_lock(&trace_types_lock);
6867
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006868 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006869 ret = -EBUSY;
6870 goto out;
6871 }
6872
Tom Zanussia35873a2019-02-13 17:42:45 -06006873 arch_spin_lock(&tr->max_lock);
6874 if (tr->cond_snapshot)
6875 ret = -EBUSY;
6876 arch_spin_unlock(&tr->max_lock);
6877 if (ret)
6878 goto out;
6879
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006880 switch (val) {
6881 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006882 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6883 ret = -EINVAL;
6884 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006885 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04006886 if (tr->allocated_snapshot)
6887 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006888 break;
6889 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006890/* Only allow per-cpu swap if the ring buffer supports it */
6891#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6892 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6893 ret = -EINVAL;
6894 break;
6895 }
6896#endif
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006897 if (tr->allocated_snapshot)
6898 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006899 &tr->array_buffer, iter->cpu_file);
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006900 else
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04006901 ret = tracing_alloc_snapshot_instance(tr);
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006902 if (ret < 0)
6903 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006904 local_irq_disable();
6905 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006906 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Tom Zanussia35873a2019-02-13 17:42:45 -06006907 update_max_tr(tr, current, smp_processor_id(), NULL);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006908 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006909 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006910 local_irq_enable();
6911 break;
6912 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006913 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006914 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6915 tracing_reset_online_cpus(&tr->max_buffer);
6916 else
Steven Rostedt (VMware)a47b53e2019-08-13 12:14:35 -04006917 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006918 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006919 break;
6920 }
6921
6922 if (ret >= 0) {
6923 *ppos += cnt;
6924 ret = cnt;
6925 }
6926out:
6927 mutex_unlock(&trace_types_lock);
6928 return ret;
6929}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006930
6931static int tracing_snapshot_release(struct inode *inode, struct file *file)
6932{
6933 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006934 int ret;
6935
6936 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006937
6938 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006939 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006940
6941 /* If write only, the seq_file is just a stub */
6942 if (m)
6943 kfree(m->private);
6944 kfree(m);
6945
6946 return 0;
6947}
6948
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006949static int tracing_buffers_open(struct inode *inode, struct file *filp);
6950static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6951 size_t count, loff_t *ppos);
6952static int tracing_buffers_release(struct inode *inode, struct file *file);
6953static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6954 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6955
6956static int snapshot_raw_open(struct inode *inode, struct file *filp)
6957{
6958 struct ftrace_buffer_info *info;
6959 int ret;
6960
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04006961 /* The following checks for tracefs lockdown */
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006962 ret = tracing_buffers_open(inode, filp);
6963 if (ret < 0)
6964 return ret;
6965
6966 info = filp->private_data;
6967
6968 if (info->iter.trace->use_max_tr) {
6969 tracing_buffers_release(inode, filp);
6970 return -EBUSY;
6971 }
6972
6973 info->iter.snapshot = true;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006974 info->iter.array_buffer = &info->iter.tr->max_buffer;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006975
6976 return ret;
6977}
6978
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006979#endif /* CONFIG_TRACER_SNAPSHOT */
6980
6981
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006982static const struct file_operations tracing_thresh_fops = {
6983 .open = tracing_open_generic,
6984 .read = tracing_thresh_read,
6985 .write = tracing_thresh_write,
6986 .llseek = generic_file_llseek,
6987};
6988
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04006989#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006990static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006991 .open = tracing_open_generic,
6992 .read = tracing_max_lat_read,
6993 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006994 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006995};
Chen Gange428abb2015-11-10 05:15:15 +08006996#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006997
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006998static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006999 .open = tracing_open_generic,
7000 .read = tracing_set_trace_read,
7001 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007002 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007003};
7004
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007005static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007006 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02007007 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007008 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02007009 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007010 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007011 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02007012};
7013
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007014static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007015 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02007016 .read = tracing_entries_read,
7017 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007018 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007019 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02007020};
7021
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07007022static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007023 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07007024 .read = tracing_total_entries_read,
7025 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007026 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07007027};
7028
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07007029static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007030 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07007031 .write = tracing_free_buffer_write,
7032 .release = tracing_free_buffer_release,
7033};
7034
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007035static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007036 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007037 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007038 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007039 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007040};
7041
Steven Rostedtfa32e852016-07-06 15:25:08 -04007042static const struct file_operations tracing_mark_raw_fops = {
7043 .open = tracing_open_generic_tr,
7044 .write = tracing_mark_raw_write,
7045 .llseek = generic_file_llseek,
7046 .release = tracing_release_generic_tr,
7047};
7048
Zhaolei5079f322009-08-25 16:12:56 +08007049static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08007050 .open = tracing_clock_open,
7051 .read = seq_read,
7052 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007053 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08007054 .write = tracing_clock_write,
7055};
7056
Tom Zanussi2c1ea602018-01-15 20:51:41 -06007057static const struct file_operations trace_time_stamp_mode_fops = {
7058 .open = tracing_time_stamp_mode_open,
7059 .read = seq_read,
7060 .llseek = seq_lseek,
7061 .release = tracing_single_release_tr,
7062};
7063
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007064#ifdef CONFIG_TRACER_SNAPSHOT
7065static const struct file_operations snapshot_fops = {
7066 .open = tracing_snapshot_open,
7067 .read = seq_read,
7068 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05007069 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007070 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007071};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007072
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007073static const struct file_operations snapshot_raw_fops = {
7074 .open = snapshot_raw_open,
7075 .read = tracing_buffers_read,
7076 .release = tracing_buffers_release,
7077 .splice_read = tracing_buffers_splice_read,
7078 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007079};
7080
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007081#endif /* CONFIG_TRACER_SNAPSHOT */
7082
Tom Zanussi8a062902019-03-31 18:48:15 -05007083#define TRACING_LOG_ERRS_MAX 8
7084#define TRACING_LOG_LOC_MAX 128
7085
7086#define CMD_PREFIX " Command: "
7087
7088struct err_info {
7089 const char **errs; /* ptr to loc-specific array of err strings */
7090 u8 type; /* index into errs -> specific err string */
7091 u8 pos; /* MAX_FILTER_STR_VAL = 256 */
7092 u64 ts;
7093};
7094
7095struct tracing_log_err {
7096 struct list_head list;
7097 struct err_info info;
7098 char loc[TRACING_LOG_LOC_MAX]; /* err location */
7099 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
7100};
7101
Tom Zanussi8a062902019-03-31 18:48:15 -05007102static DEFINE_MUTEX(tracing_err_log_lock);
7103
YueHaibingff585c52019-06-14 23:32:10 +08007104static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
Tom Zanussi8a062902019-03-31 18:48:15 -05007105{
7106 struct tracing_log_err *err;
7107
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007108 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007109 err = kzalloc(sizeof(*err), GFP_KERNEL);
7110 if (!err)
7111 err = ERR_PTR(-ENOMEM);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007112 tr->n_err_log_entries++;
Tom Zanussi8a062902019-03-31 18:48:15 -05007113
7114 return err;
7115 }
7116
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007117 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
Tom Zanussi8a062902019-03-31 18:48:15 -05007118 list_del(&err->list);
7119
7120 return err;
7121}
7122
7123/**
7124 * err_pos - find the position of a string within a command for error careting
7125 * @cmd: The tracing command that caused the error
7126 * @str: The string to position the caret at within @cmd
7127 *
7128 * Finds the position of the first occurence of @str within @cmd. The
7129 * return value can be passed to tracing_log_err() for caret placement
7130 * within @cmd.
7131 *
7132 * Returns the index within @cmd of the first occurence of @str or 0
7133 * if @str was not found.
7134 */
7135unsigned int err_pos(char *cmd, const char *str)
7136{
7137 char *found;
7138
7139 if (WARN_ON(!strlen(cmd)))
7140 return 0;
7141
7142 found = strstr(cmd, str);
7143 if (found)
7144 return found - cmd;
7145
7146 return 0;
7147}
7148
7149/**
7150 * tracing_log_err - write an error to the tracing error log
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007151 * @tr: The associated trace array for the error (NULL for top level array)
Tom Zanussi8a062902019-03-31 18:48:15 -05007152 * @loc: A string describing where the error occurred
7153 * @cmd: The tracing command that caused the error
7154 * @errs: The array of loc-specific static error strings
7155 * @type: The index into errs[], which produces the specific static err string
7156 * @pos: The position the caret should be placed in the cmd
7157 *
7158 * Writes an error into tracing/error_log of the form:
7159 *
7160 * <loc>: error: <text>
7161 * Command: <cmd>
7162 * ^
7163 *
7164 * tracing/error_log is a small log file containing the last
7165 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7166 * unless there has been a tracing error, and the error log can be
7167 * cleared and have its memory freed by writing the empty string in
7168 * truncation mode to it i.e. echo > tracing/error_log.
7169 *
7170 * NOTE: the @errs array along with the @type param are used to
7171 * produce a static error string - this string is not copied and saved
7172 * when the error is logged - only a pointer to it is saved. See
7173 * existing callers for examples of how static strings are typically
7174 * defined for use with tracing_log_err().
7175 */
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007176void tracing_log_err(struct trace_array *tr,
7177 const char *loc, const char *cmd,
Tom Zanussi8a062902019-03-31 18:48:15 -05007178 const char **errs, u8 type, u8 pos)
7179{
7180 struct tracing_log_err *err;
7181
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007182 if (!tr)
7183 tr = &global_trace;
7184
Tom Zanussi8a062902019-03-31 18:48:15 -05007185 mutex_lock(&tracing_err_log_lock);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007186 err = get_tracing_log_err(tr);
Tom Zanussi8a062902019-03-31 18:48:15 -05007187 if (PTR_ERR(err) == -ENOMEM) {
7188 mutex_unlock(&tracing_err_log_lock);
7189 return;
7190 }
7191
7192 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7193 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7194
7195 err->info.errs = errs;
7196 err->info.type = type;
7197 err->info.pos = pos;
7198 err->info.ts = local_clock();
7199
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007200 list_add_tail(&err->list, &tr->err_log);
Tom Zanussi8a062902019-03-31 18:48:15 -05007201 mutex_unlock(&tracing_err_log_lock);
7202}
7203
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007204static void clear_tracing_err_log(struct trace_array *tr)
Tom Zanussi8a062902019-03-31 18:48:15 -05007205{
7206 struct tracing_log_err *err, *next;
7207
7208 mutex_lock(&tracing_err_log_lock);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007209 list_for_each_entry_safe(err, next, &tr->err_log, list) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007210 list_del(&err->list);
7211 kfree(err);
7212 }
7213
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007214 tr->n_err_log_entries = 0;
Tom Zanussi8a062902019-03-31 18:48:15 -05007215 mutex_unlock(&tracing_err_log_lock);
7216}
7217
7218static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7219{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007220 struct trace_array *tr = m->private;
7221
Tom Zanussi8a062902019-03-31 18:48:15 -05007222 mutex_lock(&tracing_err_log_lock);
7223
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007224 return seq_list_start(&tr->err_log, *pos);
Tom Zanussi8a062902019-03-31 18:48:15 -05007225}
7226
7227static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7228{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007229 struct trace_array *tr = m->private;
7230
7231 return seq_list_next(v, &tr->err_log, pos);
Tom Zanussi8a062902019-03-31 18:48:15 -05007232}
7233
7234static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7235{
7236 mutex_unlock(&tracing_err_log_lock);
7237}
7238
7239static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7240{
7241 u8 i;
7242
7243 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7244 seq_putc(m, ' ');
7245 for (i = 0; i < pos; i++)
7246 seq_putc(m, ' ');
7247 seq_puts(m, "^\n");
7248}
7249
7250static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7251{
7252 struct tracing_log_err *err = v;
7253
7254 if (err) {
7255 const char *err_text = err->info.errs[err->info.type];
7256 u64 sec = err->info.ts;
7257 u32 nsec;
7258
7259 nsec = do_div(sec, NSEC_PER_SEC);
7260 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7261 err->loc, err_text);
7262 seq_printf(m, "%s", err->cmd);
7263 tracing_err_log_show_pos(m, err->info.pos);
7264 }
7265
7266 return 0;
7267}
7268
7269static const struct seq_operations tracing_err_log_seq_ops = {
7270 .start = tracing_err_log_seq_start,
7271 .next = tracing_err_log_seq_next,
7272 .stop = tracing_err_log_seq_stop,
7273 .show = tracing_err_log_seq_show
7274};
7275
7276static int tracing_err_log_open(struct inode *inode, struct file *file)
7277{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007278 struct trace_array *tr = inode->i_private;
Tom Zanussi8a062902019-03-31 18:48:15 -05007279 int ret = 0;
7280
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007281 ret = tracing_check_open_get_tr(tr);
7282 if (ret)
7283 return ret;
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007284
Tom Zanussi8a062902019-03-31 18:48:15 -05007285 /* If this file was opened for write, then erase contents */
7286 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007287 clear_tracing_err_log(tr);
Tom Zanussi8a062902019-03-31 18:48:15 -05007288
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007289 if (file->f_mode & FMODE_READ) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007290 ret = seq_open(file, &tracing_err_log_seq_ops);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007291 if (!ret) {
7292 struct seq_file *m = file->private_data;
7293 m->private = tr;
7294 } else {
7295 trace_array_put(tr);
7296 }
7297 }
Tom Zanussi8a062902019-03-31 18:48:15 -05007298 return ret;
7299}
7300
7301static ssize_t tracing_err_log_write(struct file *file,
7302 const char __user *buffer,
7303 size_t count, loff_t *ppos)
7304{
7305 return count;
7306}
7307
Takeshi Misawad122ed62019-06-28 19:56:40 +09007308static int tracing_err_log_release(struct inode *inode, struct file *file)
7309{
7310 struct trace_array *tr = inode->i_private;
7311
7312 trace_array_put(tr);
7313
7314 if (file->f_mode & FMODE_READ)
7315 seq_release(inode, file);
7316
7317 return 0;
7318}
7319
Tom Zanussi8a062902019-03-31 18:48:15 -05007320static const struct file_operations tracing_err_log_fops = {
7321 .open = tracing_err_log_open,
7322 .write = tracing_err_log_write,
7323 .read = seq_read,
7324 .llseek = seq_lseek,
Takeshi Misawad122ed62019-06-28 19:56:40 +09007325 .release = tracing_err_log_release,
Tom Zanussi8a062902019-03-31 18:48:15 -05007326};
7327
Steven Rostedt2cadf912008-12-01 22:20:19 -05007328static int tracing_buffers_open(struct inode *inode, struct file *filp)
7329{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007330 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007331 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007332 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007333
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007334 ret = tracing_check_open_get_tr(tr);
7335 if (ret)
7336 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007337
Steven Rostedt2cadf912008-12-01 22:20:19 -05007338 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007339 if (!info) {
7340 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007341 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007342 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007343
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007344 mutex_lock(&trace_types_lock);
7345
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007346 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007347 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05007348 info->iter.trace = tr->current_trace;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007349 info->iter.array_buffer = &tr->array_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007350 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007351 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007352 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007353
7354 filp->private_data = info;
7355
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007356 tr->current_trace->ref++;
7357
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007358 mutex_unlock(&trace_types_lock);
7359
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007360 ret = nonseekable_open(inode, filp);
7361 if (ret < 0)
7362 trace_array_put(tr);
7363
7364 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007365}
7366
Al Viro9dd95742017-07-03 00:42:43 -04007367static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007368tracing_buffers_poll(struct file *filp, poll_table *poll_table)
7369{
7370 struct ftrace_buffer_info *info = filp->private_data;
7371 struct trace_iterator *iter = &info->iter;
7372
7373 return trace_poll(iter, filp, poll_table);
7374}
7375
Steven Rostedt2cadf912008-12-01 22:20:19 -05007376static ssize_t
7377tracing_buffers_read(struct file *filp, char __user *ubuf,
7378 size_t count, loff_t *ppos)
7379{
7380 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007381 struct trace_iterator *iter = &info->iter;
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007382 ssize_t ret = 0;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007383 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007384
Steven Rostedt2dc5d122009-03-04 19:10:05 -05007385 if (!count)
7386 return 0;
7387
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007388#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007389 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7390 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007391#endif
7392
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007393 if (!info->spare) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007394 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007395 iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007396 if (IS_ERR(info->spare)) {
7397 ret = PTR_ERR(info->spare);
7398 info->spare = NULL;
7399 } else {
7400 info->spare_cpu = iter->cpu_file;
7401 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007402 }
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007403 if (!info->spare)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007404 return ret;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007405
Steven Rostedt2cadf912008-12-01 22:20:19 -05007406 /* Do we have previous read data to read? */
7407 if (info->read < PAGE_SIZE)
7408 goto read;
7409
Steven Rostedtb6273442013-02-28 13:44:11 -05007410 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007411 trace_access_lock(iter->cpu_file);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007412 ret = ring_buffer_read_page(iter->array_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007413 &info->spare,
7414 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007415 iter->cpu_file, 0);
7416 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05007417
7418 if (ret < 0) {
7419 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007420 if ((filp->f_flags & O_NONBLOCK))
7421 return -EAGAIN;
7422
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05007423 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007424 if (ret)
7425 return ret;
7426
Steven Rostedtb6273442013-02-28 13:44:11 -05007427 goto again;
7428 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007429 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05007430 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007431
Steven Rostedt436fc282011-10-14 10:44:25 -04007432 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05007433 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05007434 size = PAGE_SIZE - info->read;
7435 if (size > count)
7436 size = count;
7437
7438 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007439 if (ret == size)
7440 return -EFAULT;
7441
Steven Rostedt2dc5d122009-03-04 19:10:05 -05007442 size -= ret;
7443
Steven Rostedt2cadf912008-12-01 22:20:19 -05007444 *ppos += size;
7445 info->read += size;
7446
7447 return size;
7448}
7449
7450static int tracing_buffers_release(struct inode *inode, struct file *file)
7451{
7452 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007453 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007454
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007455 mutex_lock(&trace_types_lock);
7456
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007457 iter->tr->current_trace->ref--;
7458
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04007459 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007460
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007461 if (info->spare)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007462 ring_buffer_free_read_page(iter->array_buffer->buffer,
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007463 info->spare_cpu, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007464 kfree(info);
7465
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007466 mutex_unlock(&trace_types_lock);
7467
Steven Rostedt2cadf912008-12-01 22:20:19 -05007468 return 0;
7469}
7470
7471struct buffer_ref {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05007472 struct trace_buffer *buffer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007473 void *page;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007474 int cpu;
Jann Hornb9872222019-04-04 23:59:25 +02007475 refcount_t refcount;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007476};
7477
Jann Hornb9872222019-04-04 23:59:25 +02007478static void buffer_ref_release(struct buffer_ref *ref)
7479{
7480 if (!refcount_dec_and_test(&ref->refcount))
7481 return;
7482 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7483 kfree(ref);
7484}
7485
Steven Rostedt2cadf912008-12-01 22:20:19 -05007486static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7487 struct pipe_buffer *buf)
7488{
7489 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7490
Jann Hornb9872222019-04-04 23:59:25 +02007491 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007492 buf->private = 0;
7493}
7494
Matthew Wilcox15fab632019-04-05 14:02:10 -07007495static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007496 struct pipe_buffer *buf)
7497{
7498 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7499
Linus Torvaldse9e1a2e2019-04-26 11:09:55 -07007500 if (refcount_read(&ref->refcount) > INT_MAX/2)
Matthew Wilcox15fab632019-04-05 14:02:10 -07007501 return false;
7502
Jann Hornb9872222019-04-04 23:59:25 +02007503 refcount_inc(&ref->refcount);
Matthew Wilcox15fab632019-04-05 14:02:10 -07007504 return true;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007505}
7506
7507/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08007508static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007509 .confirm = generic_pipe_buf_confirm,
7510 .release = buffer_pipe_buf_release,
Jann Hornb9872222019-04-04 23:59:25 +02007511 .steal = generic_pipe_buf_nosteal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007512 .get = buffer_pipe_buf_get,
7513};
7514
7515/*
7516 * Callback from splice_to_pipe(), if we need to release some pages
7517 * at the end of the spd in case we error'ed out in filling the pipe.
7518 */
7519static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7520{
7521 struct buffer_ref *ref =
7522 (struct buffer_ref *)spd->partial[i].private;
7523
Jann Hornb9872222019-04-04 23:59:25 +02007524 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007525 spd->partial[i].private = 0;
7526}
7527
7528static ssize_t
7529tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7530 struct pipe_inode_info *pipe, size_t len,
7531 unsigned int flags)
7532{
7533 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007534 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02007535 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7536 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05007537 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02007538 .pages = pages_def,
7539 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02007540 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007541 .ops = &buffer_pipe_buf_ops,
7542 .spd_release = buffer_spd_release,
7543 };
7544 struct buffer_ref *ref;
Steven Rostedt (VMware)6b7e6332017-12-22 20:38:57 -05007545 int entries, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01007546 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007547
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007548#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007549 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7550 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007551#endif
7552
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007553 if (*ppos & (PAGE_SIZE - 1))
7554 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007555
7556 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007557 if (len < PAGE_SIZE)
7558 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007559 len &= PAGE_MASK;
7560 }
7561
Al Viro1ae22932016-09-17 18:31:46 -04007562 if (splice_grow_spd(pipe, &spd))
7563 return -ENOMEM;
7564
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007565 again:
7566 trace_access_lock(iter->cpu_file);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007567 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04007568
Al Viroa786c062014-04-11 12:01:03 -04007569 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007570 struct page *page;
7571 int r;
7572
7573 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01007574 if (!ref) {
7575 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007576 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01007577 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007578
Jann Hornb9872222019-04-04 23:59:25 +02007579 refcount_set(&ref->refcount, 1);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007580 ref->buffer = iter->array_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007581 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007582 if (IS_ERR(ref->page)) {
7583 ret = PTR_ERR(ref->page);
7584 ref->page = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007585 kfree(ref);
7586 break;
7587 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007588 ref->cpu = iter->cpu_file;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007589
7590 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007591 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007592 if (r < 0) {
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007593 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7594 ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007595 kfree(ref);
7596 break;
7597 }
7598
Steven Rostedt2cadf912008-12-01 22:20:19 -05007599 page = virt_to_page(ref->page);
7600
7601 spd.pages[i] = page;
7602 spd.partial[i].len = PAGE_SIZE;
7603 spd.partial[i].offset = 0;
7604 spd.partial[i].private = (unsigned long)ref;
7605 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007606 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04007607
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007608 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007609 }
7610
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007611 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007612 spd.nr_pages = i;
7613
7614 /* did we read anything? */
7615 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01007616 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007617 goto out;
Rabin Vincent07906da2014-11-06 22:26:07 +01007618
Al Viro1ae22932016-09-17 18:31:46 -04007619 ret = -EAGAIN;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007620 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
Al Viro1ae22932016-09-17 18:31:46 -04007621 goto out;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007622
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05007623 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04007624 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007625 goto out;
Rabin Vincente30f53a2014-11-10 19:46:34 +01007626
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007627 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007628 }
7629
7630 ret = splice_to_pipe(pipe, &spd);
Al Viro1ae22932016-09-17 18:31:46 -04007631out:
Eric Dumazet047fe362012-06-12 15:24:40 +02007632 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007633
Steven Rostedt2cadf912008-12-01 22:20:19 -05007634 return ret;
7635}
7636
7637static const struct file_operations tracing_buffers_fops = {
7638 .open = tracing_buffers_open,
7639 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007640 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007641 .release = tracing_buffers_release,
7642 .splice_read = tracing_buffers_splice_read,
7643 .llseek = no_llseek,
7644};
7645
Steven Rostedtc8d77182009-04-29 18:03:45 -04007646static ssize_t
7647tracing_stats_read(struct file *filp, char __user *ubuf,
7648 size_t count, loff_t *ppos)
7649{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007650 struct inode *inode = file_inode(filp);
7651 struct trace_array *tr = inode->i_private;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007652 struct array_buffer *trace_buf = &tr->array_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007653 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007654 struct trace_seq *s;
7655 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007656 unsigned long long t;
7657 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007658
Li Zefane4f2d102009-06-15 10:57:28 +08007659 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007660 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01007661 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007662
7663 trace_seq_init(s);
7664
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007665 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007666 trace_seq_printf(s, "entries: %ld\n", cnt);
7667
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007668 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007669 trace_seq_printf(s, "overrun: %ld\n", cnt);
7670
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007671 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007672 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7673
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007674 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007675 trace_seq_printf(s, "bytes: %ld\n", cnt);
7676
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09007677 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007678 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007679 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007680 usec_rem = do_div(t, USEC_PER_SEC);
7681 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7682 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007683
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007684 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007685 usec_rem = do_div(t, USEC_PER_SEC);
7686 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7687 } else {
7688 /* counter or tsc mode for trace_clock */
7689 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007690 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007691
7692 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007693 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007694 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007695
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007696 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07007697 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7698
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007699 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05007700 trace_seq_printf(s, "read events: %ld\n", cnt);
7701
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05007702 count = simple_read_from_buffer(ubuf, count, ppos,
7703 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04007704
7705 kfree(s);
7706
7707 return count;
7708}
7709
7710static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007711 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007712 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007713 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007714 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007715};
7716
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007717#ifdef CONFIG_DYNAMIC_FTRACE
7718
7719static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007720tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007721 size_t cnt, loff_t *ppos)
7722{
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007723 ssize_t ret;
7724 char *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007725 int r;
7726
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007727 /* 256 should be plenty to hold the amount needed */
7728 buf = kmalloc(256, GFP_KERNEL);
7729 if (!buf)
7730 return -ENOMEM;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007731
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007732 r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
7733 ftrace_update_tot_cnt,
7734 ftrace_number_of_pages,
7735 ftrace_number_of_groups);
7736
7737 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7738 kfree(buf);
7739 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007740}
7741
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007742static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007743 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007744 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007745 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007746};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007747#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007748
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007749#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7750static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007751ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007752 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007753 void *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007754{
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007755 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007756}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007757
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007758static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007759ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007760 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007761 void *data)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007762{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007763 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007764 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007765
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007766 if (mapper)
7767 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007768
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007769 if (count) {
7770
7771 if (*count <= 0)
7772 return;
7773
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007774 (*count)--;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007775 }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007776
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007777 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007778}
7779
7780static int
7781ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7782 struct ftrace_probe_ops *ops, void *data)
7783{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007784 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007785 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007786
7787 seq_printf(m, "%ps:", (void *)ip);
7788
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007789 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007790
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007791 if (mapper)
7792 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7793
7794 if (count)
7795 seq_printf(m, ":count=%ld\n", *count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007796 else
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007797 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007798
7799 return 0;
7800}
7801
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007802static int
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007803ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007804 unsigned long ip, void *init_data, void **data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007805{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007806 struct ftrace_func_mapper *mapper = *data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007807
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007808 if (!mapper) {
7809 mapper = allocate_ftrace_func_mapper();
7810 if (!mapper)
7811 return -ENOMEM;
7812 *data = mapper;
7813 }
7814
7815 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007816}
7817
7818static void
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007819ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007820 unsigned long ip, void *data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007821{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007822 struct ftrace_func_mapper *mapper = data;
7823
7824 if (!ip) {
7825 if (!mapper)
7826 return;
7827 free_ftrace_func_mapper(mapper, NULL);
7828 return;
7829 }
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007830
7831 ftrace_func_mapper_remove_ip(mapper, ip);
7832}
7833
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007834static struct ftrace_probe_ops snapshot_probe_ops = {
7835 .func = ftrace_snapshot,
7836 .print = ftrace_snapshot_print,
7837};
7838
7839static struct ftrace_probe_ops snapshot_count_probe_ops = {
7840 .func = ftrace_count_snapshot,
7841 .print = ftrace_snapshot_print,
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007842 .init = ftrace_snapshot_init,
7843 .free = ftrace_snapshot_free,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007844};
7845
7846static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007847ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007848 char *glob, char *cmd, char *param, int enable)
7849{
7850 struct ftrace_probe_ops *ops;
7851 void *count = (void *)-1;
7852 char *number;
7853 int ret;
7854
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -04007855 if (!tr)
7856 return -ENODEV;
7857
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007858 /* hash funcs only work with set_ftrace_filter */
7859 if (!enable)
7860 return -EINVAL;
7861
7862 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7863
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04007864 if (glob[0] == '!')
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04007865 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007866
7867 if (!param)
7868 goto out_reg;
7869
7870 number = strsep(&param, ":");
7871
7872 if (!strlen(number))
7873 goto out_reg;
7874
7875 /*
7876 * We use the callback data field (which is a pointer)
7877 * as our counter.
7878 */
7879 ret = kstrtoul(number, 0, (unsigned long *)&count);
7880 if (ret)
7881 return ret;
7882
7883 out_reg:
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04007884 ret = tracing_alloc_snapshot_instance(tr);
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007885 if (ret < 0)
7886 goto out;
7887
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007888 ret = register_ftrace_function_probe(glob, tr, ops, count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007889
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007890 out:
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007891 return ret < 0 ? ret : 0;
7892}
7893
7894static struct ftrace_func_command ftrace_snapshot_cmd = {
7895 .name = "snapshot",
7896 .func = ftrace_trace_snapshot_callback,
7897};
7898
Tom Zanussi38de93a2013-10-24 08:34:18 -05007899static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007900{
7901 return register_ftrace_command(&ftrace_snapshot_cmd);
7902}
7903#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05007904static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007905#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007906
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007907static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007908{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007909 if (WARN_ON(!tr->dir))
7910 return ERR_PTR(-ENODEV);
7911
7912 /* Top directory uses NULL as the parent */
7913 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7914 return NULL;
7915
7916 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007917 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007918}
7919
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007920static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
7921{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007922 struct dentry *d_tracer;
7923
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007924 if (tr->percpu_dir)
7925 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007926
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007927 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007928 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007929 return NULL;
7930
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007931 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007932
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05007933 MEM_FAIL(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007934 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007935
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007936 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007937}
7938
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007939static struct dentry *
7940trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7941 void *data, long cpu, const struct file_operations *fops)
7942{
7943 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7944
7945 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00007946 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007947 return ret;
7948}
7949
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007950static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007951tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007952{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007953 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007954 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04007955 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007956
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09007957 if (!d_percpu)
7958 return;
7959
Steven Rostedtdd49a382010-10-20 21:51:26 -04007960 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007961 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007962 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007963 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007964 return;
7965 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007966
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007967 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007968 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02007969 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007970
7971 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007972 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007973 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04007974
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007975 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007976 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007977
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007978 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007979 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08007980
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007981 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007982 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007983
7984#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007985 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007986 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007987
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007988 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007989 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007990#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007991}
7992
Steven Rostedt60a11772008-05-12 21:20:44 +02007993#ifdef CONFIG_FTRACE_SELFTEST
7994/* Let selftest have access to static functions in this file */
7995#include "trace_selftest.c"
7996#endif
7997
Steven Rostedt577b7852009-02-26 23:43:05 -05007998static ssize_t
7999trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
8000 loff_t *ppos)
8001{
8002 struct trace_option_dentry *topt = filp->private_data;
8003 char *buf;
8004
8005 if (topt->flags->val & topt->opt->bit)
8006 buf = "1\n";
8007 else
8008 buf = "0\n";
8009
8010 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8011}
8012
8013static ssize_t
8014trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
8015 loff_t *ppos)
8016{
8017 struct trace_option_dentry *topt = filp->private_data;
8018 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05008019 int ret;
8020
Peter Huewe22fe9b52011-06-07 21:58:27 +02008021 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8022 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05008023 return ret;
8024
Li Zefan8d18eaa2009-12-08 11:17:06 +08008025 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05008026 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08008027
8028 if (!!(topt->flags->val & topt->opt->bit) != val) {
8029 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05008030 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05008031 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08008032 mutex_unlock(&trace_types_lock);
8033 if (ret)
8034 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05008035 }
8036
8037 *ppos += cnt;
8038
8039 return cnt;
8040}
8041
8042
8043static const struct file_operations trace_options_fops = {
8044 .open = tracing_open_generic,
8045 .read = trace_options_read,
8046 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02008047 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05008048};
8049
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008050/*
8051 * In order to pass in both the trace_array descriptor as well as the index
8052 * to the flag that the trace option file represents, the trace_array
8053 * has a character array of trace_flags_index[], which holds the index
8054 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
8055 * The address of this character array is passed to the flag option file
8056 * read/write callbacks.
8057 *
8058 * In order to extract both the index and the trace_array descriptor,
8059 * get_tr_index() uses the following algorithm.
8060 *
8061 * idx = *ptr;
8062 *
8063 * As the pointer itself contains the address of the index (remember
8064 * index[1] == 1).
8065 *
8066 * Then to get the trace_array descriptor, by subtracting that index
8067 * from the ptr, we get to the start of the index itself.
8068 *
8069 * ptr - idx == &index[0]
8070 *
8071 * Then a simple container_of() from that pointer gets us to the
8072 * trace_array descriptor.
8073 */
8074static void get_tr_index(void *data, struct trace_array **ptr,
8075 unsigned int *pindex)
8076{
8077 *pindex = *(unsigned char *)data;
8078
8079 *ptr = container_of(data - *pindex, struct trace_array,
8080 trace_flags_index);
8081}
8082
Steven Rostedta8259072009-02-26 22:19:12 -05008083static ssize_t
8084trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
8085 loff_t *ppos)
8086{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008087 void *tr_index = filp->private_data;
8088 struct trace_array *tr;
8089 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05008090 char *buf;
8091
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008092 get_tr_index(tr_index, &tr, &index);
8093
8094 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05008095 buf = "1\n";
8096 else
8097 buf = "0\n";
8098
8099 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8100}
8101
8102static ssize_t
8103trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
8104 loff_t *ppos)
8105{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008106 void *tr_index = filp->private_data;
8107 struct trace_array *tr;
8108 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05008109 unsigned long val;
8110 int ret;
8111
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008112 get_tr_index(tr_index, &tr, &index);
8113
Peter Huewe22fe9b52011-06-07 21:58:27 +02008114 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8115 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05008116 return ret;
8117
Zhaoleif2d84b62009-08-07 18:55:48 +08008118 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05008119 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008120
Prateek Sood3a53acf2019-12-10 09:15:16 +00008121 mutex_lock(&event_mutex);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008122 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008123 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008124 mutex_unlock(&trace_types_lock);
Prateek Sood3a53acf2019-12-10 09:15:16 +00008125 mutex_unlock(&event_mutex);
Steven Rostedta8259072009-02-26 22:19:12 -05008126
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04008127 if (ret < 0)
8128 return ret;
8129
Steven Rostedta8259072009-02-26 22:19:12 -05008130 *ppos += cnt;
8131
8132 return cnt;
8133}
8134
Steven Rostedta8259072009-02-26 22:19:12 -05008135static const struct file_operations trace_options_core_fops = {
8136 .open = tracing_open_generic,
8137 .read = trace_options_core_read,
8138 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02008139 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05008140};
8141
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008142struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04008143 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008144 struct dentry *parent,
8145 void *data,
8146 const struct file_operations *fops)
8147{
8148 struct dentry *ret;
8149
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008150 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008151 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07008152 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008153
8154 return ret;
8155}
8156
8157
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008158static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05008159{
8160 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05008161
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008162 if (tr->options)
8163 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05008164
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008165 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008166 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05008167 return NULL;
8168
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008169 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008170 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07008171 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05008172 return NULL;
8173 }
8174
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008175 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05008176}
8177
Steven Rostedt577b7852009-02-26 23:43:05 -05008178static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008179create_trace_option_file(struct trace_array *tr,
8180 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05008181 struct tracer_flags *flags,
8182 struct tracer_opt *opt)
8183{
8184 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05008185
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008186 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05008187 if (!t_options)
8188 return;
8189
8190 topt->flags = flags;
8191 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008192 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05008193
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008194 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05008195 &trace_options_fops);
8196
Steven Rostedt577b7852009-02-26 23:43:05 -05008197}
8198
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008199static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008200create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05008201{
8202 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008203 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05008204 struct tracer_flags *flags;
8205 struct tracer_opt *opts;
8206 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008207 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05008208
8209 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008210 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05008211
8212 flags = tracer->flags;
8213
8214 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008215 return;
8216
8217 /*
8218 * If this is an instance, only create flags for tracers
8219 * the instance may have.
8220 */
8221 if (!trace_ok_for_array(tracer, tr))
8222 return;
8223
8224 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08008225 /* Make sure there's no duplicate flags. */
8226 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008227 return;
8228 }
Steven Rostedt577b7852009-02-26 23:43:05 -05008229
8230 opts = flags->opts;
8231
8232 for (cnt = 0; opts[cnt].name; cnt++)
8233 ;
8234
Steven Rostedt0cfe8242009-02-27 10:51:10 -05008235 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05008236 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008237 return;
8238
8239 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
8240 GFP_KERNEL);
8241 if (!tr_topts) {
8242 kfree(topts);
8243 return;
8244 }
8245
8246 tr->topts = tr_topts;
8247 tr->topts[tr->nr_topts].tracer = tracer;
8248 tr->topts[tr->nr_topts].topts = topts;
8249 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05008250
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04008251 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008252 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05008253 &opts[cnt]);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05008254 MEM_FAIL(topts[cnt].entry == NULL,
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04008255 "Failed to create trace option: %s",
8256 opts[cnt].name);
8257 }
Steven Rostedt577b7852009-02-26 23:43:05 -05008258}
8259
Steven Rostedta8259072009-02-26 22:19:12 -05008260static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008261create_trace_option_core_file(struct trace_array *tr,
8262 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05008263{
8264 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05008265
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008266 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05008267 if (!t_options)
8268 return NULL;
8269
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008270 return trace_create_file(option, 0644, t_options,
8271 (void *)&tr->trace_flags_index[index],
8272 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05008273}
8274
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008275static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05008276{
8277 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008278 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05008279 int i;
8280
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008281 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05008282 if (!t_options)
8283 return;
8284
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008285 for (i = 0; trace_options[i]; i++) {
8286 if (top_level ||
8287 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
8288 create_trace_option_core_file(tr, trace_options[i], i);
8289 }
Steven Rostedta8259072009-02-26 22:19:12 -05008290}
8291
Steven Rostedt499e5472012-02-22 15:50:28 -05008292static ssize_t
8293rb_simple_read(struct file *filp, char __user *ubuf,
8294 size_t cnt, loff_t *ppos)
8295{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04008296 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05008297 char buf[64];
8298 int r;
8299
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008300 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05008301 r = sprintf(buf, "%d\n", r);
8302
8303 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8304}
8305
8306static ssize_t
8307rb_simple_write(struct file *filp, const char __user *ubuf,
8308 size_t cnt, loff_t *ppos)
8309{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04008310 struct trace_array *tr = filp->private_data;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05008311 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05008312 unsigned long val;
8313 int ret;
8314
8315 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8316 if (ret)
8317 return ret;
8318
8319 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008320 mutex_lock(&trace_types_lock);
Steven Rostedt (VMware)f1436412018-08-01 15:40:57 -04008321 if (!!val == tracer_tracing_is_on(tr)) {
8322 val = 0; /* do nothing */
8323 } else if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008324 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008325 if (tr->current_trace->start)
8326 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008327 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008328 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008329 if (tr->current_trace->stop)
8330 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008331 }
8332 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05008333 }
8334
8335 (*ppos)++;
8336
8337 return cnt;
8338}
8339
8340static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04008341 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05008342 .read = rb_simple_read,
8343 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04008344 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05008345 .llseek = default_llseek,
8346};
8347
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008348static ssize_t
8349buffer_percent_read(struct file *filp, char __user *ubuf,
8350 size_t cnt, loff_t *ppos)
8351{
8352 struct trace_array *tr = filp->private_data;
8353 char buf[64];
8354 int r;
8355
8356 r = tr->buffer_percent;
8357 r = sprintf(buf, "%d\n", r);
8358
8359 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8360}
8361
8362static ssize_t
8363buffer_percent_write(struct file *filp, const char __user *ubuf,
8364 size_t cnt, loff_t *ppos)
8365{
8366 struct trace_array *tr = filp->private_data;
8367 unsigned long val;
8368 int ret;
8369
8370 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8371 if (ret)
8372 return ret;
8373
8374 if (val > 100)
8375 return -EINVAL;
8376
8377 if (!val)
8378 val = 1;
8379
8380 tr->buffer_percent = val;
8381
8382 (*ppos)++;
8383
8384 return cnt;
8385}
8386
8387static const struct file_operations buffer_percent_fops = {
8388 .open = tracing_open_generic_tr,
8389 .read = buffer_percent_read,
8390 .write = buffer_percent_write,
8391 .release = tracing_release_generic_tr,
8392 .llseek = default_llseek,
8393};
8394
YueHaibingff585c52019-06-14 23:32:10 +08008395static struct dentry *trace_instance_dir;
Steven Rostedt277ba042012-08-03 16:10:49 -04008396
8397static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008398init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04008399
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008400static int
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008401allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04008402{
8403 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008404
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008405 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008406
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05008407 buf->tr = tr;
8408
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008409 buf->buffer = ring_buffer_alloc(size, rb_flags);
8410 if (!buf->buffer)
8411 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008412
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008413 buf->data = alloc_percpu(struct trace_array_cpu);
8414 if (!buf->data) {
8415 ring_buffer_free(buf->buffer);
Steven Rostedt (VMware)4397f042017-12-26 20:07:34 -05008416 buf->buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008417 return -ENOMEM;
8418 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008419
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008420 /* Allocate the first page for all buffers */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008421 set_buffer_entries(&tr->array_buffer,
8422 ring_buffer_size(tr->array_buffer.buffer, 0));
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008423
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008424 return 0;
8425}
8426
8427static int allocate_trace_buffers(struct trace_array *tr, int size)
8428{
8429 int ret;
8430
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008431 ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008432 if (ret)
8433 return ret;
8434
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008435#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008436 ret = allocate_trace_buffer(tr, &tr->max_buffer,
8437 allocate_snapshot ? size : 1);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05008438 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008439 ring_buffer_free(tr->array_buffer.buffer);
8440 tr->array_buffer.buffer = NULL;
8441 free_percpu(tr->array_buffer.data);
8442 tr->array_buffer.data = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008443 return -ENOMEM;
8444 }
8445 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008446
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008447 /*
8448 * Only the top level trace array gets its snapshot allocated
8449 * from the kernel command line.
8450 */
8451 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008452#endif
8453 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008454}
8455
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008456static void free_trace_buffer(struct array_buffer *buf)
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008457{
8458 if (buf->buffer) {
8459 ring_buffer_free(buf->buffer);
8460 buf->buffer = NULL;
8461 free_percpu(buf->data);
8462 buf->data = NULL;
8463 }
8464}
8465
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008466static void free_trace_buffers(struct trace_array *tr)
8467{
8468 if (!tr)
8469 return;
8470
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008471 free_trace_buffer(&tr->array_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008472
8473#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008474 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008475#endif
8476}
8477
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008478static void init_trace_flags_index(struct trace_array *tr)
8479{
8480 int i;
8481
8482 /* Used by the trace options files */
8483 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8484 tr->trace_flags_index[i] = i;
8485}
8486
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008487static void __update_tracer_options(struct trace_array *tr)
8488{
8489 struct tracer *t;
8490
8491 for (t = trace_types; t; t = t->next)
8492 add_tracer_options(tr, t);
8493}
8494
8495static void update_tracer_options(struct trace_array *tr)
8496{
8497 mutex_lock(&trace_types_lock);
8498 __update_tracer_options(tr);
8499 mutex_unlock(&trace_types_lock);
8500}
8501
Divya Indi28879782019-11-20 11:08:38 -08008502static struct trace_array *trace_array_create(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008503{
Steven Rostedt277ba042012-08-03 16:10:49 -04008504 struct trace_array *tr;
8505 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04008506
Steven Rostedt277ba042012-08-03 16:10:49 -04008507 ret = -ENOMEM;
8508 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8509 if (!tr)
Divya Indi28879782019-11-20 11:08:38 -08008510 return ERR_PTR(ret);
Steven Rostedt277ba042012-08-03 16:10:49 -04008511
8512 tr->name = kstrdup(name, GFP_KERNEL);
8513 if (!tr->name)
8514 goto out_free_tr;
8515
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008516 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8517 goto out_free_tr;
8518
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008519 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008520
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008521 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8522
Steven Rostedt277ba042012-08-03 16:10:49 -04008523 raw_spin_lock_init(&tr->start_lock);
8524
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05008525 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8526
Steven Rostedt277ba042012-08-03 16:10:49 -04008527 tr->current_trace = &nop_trace;
8528
8529 INIT_LIST_HEAD(&tr->systems);
8530 INIT_LIST_HEAD(&tr->events);
Tom Zanussi067fe032018-01-15 20:51:56 -06008531 INIT_LIST_HEAD(&tr->hist_vars);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04008532 INIT_LIST_HEAD(&tr->err_log);
Steven Rostedt277ba042012-08-03 16:10:49 -04008533
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008534 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04008535 goto out_free_tr;
8536
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008537 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008538 if (!tr->dir)
8539 goto out_free_tr;
8540
8541 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008542 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008543 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008544 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008545 }
Steven Rostedt277ba042012-08-03 16:10:49 -04008546
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04008547 ftrace_init_trace_array(tr);
8548
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008549 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008550 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008551 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04008552
8553 list_add(&tr->list, &ftrace_trace_arrays);
8554
Divya Indi28879782019-11-20 11:08:38 -08008555 tr->ref++;
8556
Steven Rostedt277ba042012-08-03 16:10:49 -04008557
Divya Indif45d1222019-03-20 11:28:51 -07008558 return tr;
Steven Rostedt277ba042012-08-03 16:10:49 -04008559
8560 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008561 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008562 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04008563 kfree(tr->name);
8564 kfree(tr);
8565
Divya Indif45d1222019-03-20 11:28:51 -07008566 return ERR_PTR(ret);
8567}
Steven Rostedt277ba042012-08-03 16:10:49 -04008568
Divya Indif45d1222019-03-20 11:28:51 -07008569static int instance_mkdir(const char *name)
8570{
Divya Indi28879782019-11-20 11:08:38 -08008571 struct trace_array *tr;
8572 int ret;
8573
8574 mutex_lock(&event_mutex);
8575 mutex_lock(&trace_types_lock);
8576
8577 ret = -EEXIST;
8578 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8579 if (tr->name && strcmp(tr->name, name) == 0)
8580 goto out_unlock;
8581 }
8582
8583 tr = trace_array_create(name);
8584
8585 ret = PTR_ERR_OR_ZERO(tr);
8586
8587out_unlock:
8588 mutex_unlock(&trace_types_lock);
8589 mutex_unlock(&event_mutex);
8590 return ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04008591}
8592
Divya Indi28879782019-11-20 11:08:38 -08008593/**
8594 * trace_array_get_by_name - Create/Lookup a trace array, given its name.
8595 * @name: The name of the trace array to be looked up/created.
8596 *
8597 * Returns pointer to trace array with given name.
8598 * NULL, if it cannot be created.
8599 *
8600 * NOTE: This function increments the reference counter associated with the
8601 * trace array returned. This makes sure it cannot be freed while in use.
8602 * Use trace_array_put() once the trace array is no longer needed.
Steven Rostedt (VMware)28394da2020-01-24 20:47:46 -05008603 * If the trace_array is to be freed, trace_array_destroy() needs to
8604 * be called after the trace_array_put(), or simply let user space delete
8605 * it from the tracefs instances directory. But until the
8606 * trace_array_put() is called, user space can not delete it.
Divya Indi28879782019-11-20 11:08:38 -08008607 *
8608 */
8609struct trace_array *trace_array_get_by_name(const char *name)
8610{
8611 struct trace_array *tr;
8612
8613 mutex_lock(&event_mutex);
8614 mutex_lock(&trace_types_lock);
8615
8616 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8617 if (tr->name && strcmp(tr->name, name) == 0)
8618 goto out_unlock;
8619 }
8620
8621 tr = trace_array_create(name);
8622
8623 if (IS_ERR(tr))
8624 tr = NULL;
8625out_unlock:
8626 if (tr)
8627 tr->ref++;
8628
8629 mutex_unlock(&trace_types_lock);
8630 mutex_unlock(&event_mutex);
8631 return tr;
8632}
8633EXPORT_SYMBOL_GPL(trace_array_get_by_name);
8634
Divya Indif45d1222019-03-20 11:28:51 -07008635static int __remove_instance(struct trace_array *tr)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008636{
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008637 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008638
Divya Indi28879782019-11-20 11:08:38 -08008639 /* Reference counter for a newly created trace array = 1. */
8640 if (tr->ref > 1 || (tr->current_trace && tr->current_trace->ref))
Divya Indif45d1222019-03-20 11:28:51 -07008641 return -EBUSY;
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05008642
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008643 list_del(&tr->list);
8644
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008645 /* Disable all the flags that were enabled coming in */
8646 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8647 if ((1 << i) & ZEROED_TRACE_FLAGS)
8648 set_tracer_flag(tr, 1 << i, 0);
8649 }
8650
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05008651 tracing_set_nop(tr);
Naveen N. Raoa0e63692017-05-16 23:21:26 +05308652 clear_ftrace_function_probes(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008653 event_trace_del_tracer(tr);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09008654 ftrace_clear_pids(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008655 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08008656 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04008657 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008658
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008659 for (i = 0; i < tr->nr_topts; i++) {
8660 kfree(tr->topts[i].topts);
8661 }
8662 kfree(tr->topts);
8663
Chunyu Hudb9108e02017-07-20 18:36:09 +08008664 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008665 kfree(tr->name);
8666 kfree(tr);
Divya Indif45d1222019-03-20 11:28:51 -07008667 tr = NULL;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008668
Divya Indif45d1222019-03-20 11:28:51 -07008669 return 0;
8670}
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008671
Divya Indie585e642019-08-14 10:55:24 -07008672int trace_array_destroy(struct trace_array *this_tr)
Divya Indif45d1222019-03-20 11:28:51 -07008673{
Divya Indie585e642019-08-14 10:55:24 -07008674 struct trace_array *tr;
Divya Indif45d1222019-03-20 11:28:51 -07008675 int ret;
8676
Divya Indie585e642019-08-14 10:55:24 -07008677 if (!this_tr)
Divya Indif45d1222019-03-20 11:28:51 -07008678 return -EINVAL;
8679
8680 mutex_lock(&event_mutex);
8681 mutex_lock(&trace_types_lock);
8682
Divya Indie585e642019-08-14 10:55:24 -07008683 ret = -ENODEV;
8684
8685 /* Making sure trace array exists before destroying it. */
8686 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8687 if (tr == this_tr) {
8688 ret = __remove_instance(tr);
8689 break;
8690 }
8691 }
Divya Indif45d1222019-03-20 11:28:51 -07008692
8693 mutex_unlock(&trace_types_lock);
8694 mutex_unlock(&event_mutex);
8695
8696 return ret;
8697}
8698EXPORT_SYMBOL_GPL(trace_array_destroy);
8699
8700static int instance_rmdir(const char *name)
8701{
8702 struct trace_array *tr;
8703 int ret;
8704
8705 mutex_lock(&event_mutex);
8706 mutex_lock(&trace_types_lock);
8707
8708 ret = -ENODEV;
8709 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8710 if (tr->name && strcmp(tr->name, name) == 0) {
8711 ret = __remove_instance(tr);
8712 break;
8713 }
8714 }
8715
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008716 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008717 mutex_unlock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008718
8719 return ret;
8720}
8721
Steven Rostedt277ba042012-08-03 16:10:49 -04008722static __init void create_trace_instances(struct dentry *d_tracer)
8723{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05008724 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8725 instance_mkdir,
8726 instance_rmdir);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05008727 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
Steven Rostedt277ba042012-08-03 16:10:49 -04008728 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04008729}
8730
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008731static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008732init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008733{
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008734 struct trace_event_file *file;
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008735 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008736
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05008737 trace_create_file("available_tracers", 0444, d_tracer,
8738 tr, &show_traces_fops);
8739
8740 trace_create_file("current_tracer", 0644, d_tracer,
8741 tr, &set_tracer_fops);
8742
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008743 trace_create_file("tracing_cpumask", 0644, d_tracer,
8744 tr, &tracing_cpumask_fops);
8745
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008746 trace_create_file("trace_options", 0644, d_tracer,
8747 tr, &tracing_iter_fops);
8748
8749 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008750 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008751
8752 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02008753 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008754
8755 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02008756 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008757
8758 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8759 tr, &tracing_total_entries_fops);
8760
Wang YanQing238ae932013-05-26 16:52:01 +08008761 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008762 tr, &tracing_free_buffer_fops);
8763
8764 trace_create_file("trace_marker", 0220, d_tracer,
8765 tr, &tracing_mark_fops);
8766
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008767 file = __find_event_file(tr, "ftrace", "print");
8768 if (file && file->dir)
8769 trace_create_file("trigger", 0644, file->dir, file,
8770 &event_trigger_fops);
8771 tr->trace_marker_file = file;
8772
Steven Rostedtfa32e852016-07-06 15:25:08 -04008773 trace_create_file("trace_marker_raw", 0220, d_tracer,
8774 tr, &tracing_mark_raw_fops);
8775
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008776 trace_create_file("trace_clock", 0644, d_tracer, tr,
8777 &trace_clock_fops);
8778
8779 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008780 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008781
Tom Zanussi2c1ea602018-01-15 20:51:41 -06008782 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8783 &trace_time_stamp_mode_fops);
8784
Steven Rostedt (VMware)a7b1d742018-11-29 22:36:47 -05008785 tr->buffer_percent = 50;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008786
8787 trace_create_file("buffer_percent", 0444, d_tracer,
8788 tr, &buffer_percent_fops);
8789
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008790 create_trace_options_dir(tr);
8791
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04008792#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02008793 trace_create_maxlat_file(tr, d_tracer);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05008794#endif
8795
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008796 if (ftrace_create_function_files(tr, d_tracer))
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05008797 MEM_FAIL(1, "Could not allocate function filter files");
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008798
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008799#ifdef CONFIG_TRACER_SNAPSHOT
8800 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008801 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008802#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008803
Tom Zanussi8a062902019-03-31 18:48:15 -05008804 trace_create_file("error_log", 0644, d_tracer,
8805 tr, &tracing_err_log_fops);
8806
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008807 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008808 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008809
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04008810 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008811}
8812
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008813static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008814{
8815 struct vfsmount *mnt;
8816 struct file_system_type *type;
8817
8818 /*
8819 * To maintain backward compatibility for tools that mount
8820 * debugfs to get to the tracing facility, tracefs is automatically
8821 * mounted to the debugfs/tracing directory.
8822 */
8823 type = get_fs_type("tracefs");
8824 if (!type)
8825 return NULL;
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008826 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008827 put_filesystem(type);
8828 if (IS_ERR(mnt))
8829 return NULL;
8830 mntget(mnt);
8831
8832 return mnt;
8833}
8834
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008835/**
8836 * tracing_init_dentry - initialize top level trace array
8837 *
8838 * This is called when creating files or directories in the tracing
8839 * directory. It is called via fs_initcall() by any of the boot up code
8840 * and expects to return the dentry of the top level tracing directory.
8841 */
8842struct dentry *tracing_init_dentry(void)
8843{
8844 struct trace_array *tr = &global_trace;
8845
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05008846 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11008847 pr_warn("Tracing disabled due to lockdown\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05008848 return ERR_PTR(-EPERM);
8849 }
8850
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008851 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008852 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008853 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008854
Jiaxing Wang8b129192015-11-06 16:04:16 +08008855 if (WARN_ON(!tracefs_initialized()) ||
8856 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8857 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008858 return ERR_PTR(-ENODEV);
8859
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008860 /*
8861 * As there may still be users that expect the tracing
8862 * files to exist in debugfs/tracing, we must automount
8863 * the tracefs file system there, so older tools still
8864 * work with the newer kerenl.
8865 */
8866 tr->dir = debugfs_create_automount("tracing", NULL,
8867 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008868
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008869 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008870}
8871
Jeremy Linton00f4b652017-05-31 16:56:43 -05008872extern struct trace_eval_map *__start_ftrace_eval_maps[];
8873extern struct trace_eval_map *__stop_ftrace_eval_maps[];
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008874
Jeremy Linton5f60b352017-05-31 16:56:47 -05008875static void __init trace_eval_init(void)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008876{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008877 int len;
8878
Jeremy Linton02fd7f62017-05-31 16:56:42 -05008879 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008880 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008881}
8882
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008883#ifdef CONFIG_MODULES
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008884static void trace_module_add_evals(struct module *mod)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008885{
Jeremy Linton99be6472017-05-31 16:56:44 -05008886 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008887 return;
8888
8889 /*
8890 * Modules with bad taint do not have events created, do
8891 * not bother with enums either.
8892 */
8893 if (trace_module_has_bad_taint(mod))
8894 return;
8895
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008896 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008897}
8898
Jeremy Linton681bec02017-05-31 16:56:53 -05008899#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008900static void trace_module_remove_evals(struct module *mod)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008901{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008902 union trace_eval_map_item *map;
8903 union trace_eval_map_item **last = &trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008904
Jeremy Linton99be6472017-05-31 16:56:44 -05008905 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008906 return;
8907
Jeremy Linton1793ed92017-05-31 16:56:46 -05008908 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008909
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008910 map = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008911
8912 while (map) {
8913 if (map->head.mod == mod)
8914 break;
Jeremy Linton5f60b352017-05-31 16:56:47 -05008915 map = trace_eval_jmp_to_tail(map);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008916 last = &map->tail.next;
8917 map = map->tail.next;
8918 }
8919 if (!map)
8920 goto out;
8921
Jeremy Linton5f60b352017-05-31 16:56:47 -05008922 *last = trace_eval_jmp_to_tail(map)->tail.next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008923 kfree(map);
8924 out:
Jeremy Linton1793ed92017-05-31 16:56:46 -05008925 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008926}
8927#else
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008928static inline void trace_module_remove_evals(struct module *mod) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05008929#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008930
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008931static int trace_module_notify(struct notifier_block *self,
8932 unsigned long val, void *data)
8933{
8934 struct module *mod = data;
8935
8936 switch (val) {
8937 case MODULE_STATE_COMING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008938 trace_module_add_evals(mod);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008939 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008940 case MODULE_STATE_GOING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008941 trace_module_remove_evals(mod);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008942 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008943 }
8944
8945 return 0;
8946}
8947
8948static struct notifier_block trace_module_nb = {
8949 .notifier_call = trace_module_notify,
8950 .priority = 0,
8951};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008952#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008953
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008954static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008955{
8956 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008957
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08008958 trace_access_lock_init();
8959
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008960 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008961 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09008962 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008963
Steven Rostedt (VMware)58b92542018-05-08 15:09:27 -04008964 event_trace_init();
8965
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008966 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04008967 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008968
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008969 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04008970 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008971
Li Zefan339ae5d2009-04-17 10:34:30 +08008972 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008973 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02008974
Avadh Patel69abe6a2009-04-10 16:04:48 -04008975 trace_create_file("saved_cmdlines", 0444, d_tracer,
8976 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03008977
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008978 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8979 NULL, &tracing_saved_cmdlines_size_fops);
8980
Michael Sartain99c621d2017-07-05 22:07:15 -06008981 trace_create_file("saved_tgids", 0444, d_tracer,
8982 NULL, &tracing_saved_tgids_fops);
8983
Jeremy Linton5f60b352017-05-31 16:56:47 -05008984 trace_eval_init();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008985
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008986 trace_create_eval_file(d_tracer);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008987
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008988#ifdef CONFIG_MODULES
8989 register_module_notifier(&trace_module_nb);
8990#endif
8991
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008992#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008993 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04008994 NULL, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008995#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01008996
Steven Rostedt277ba042012-08-03 16:10:49 -04008997 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09008998
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008999 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05009000
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01009001 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009002}
9003
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009004static int trace_panic_handler(struct notifier_block *this,
9005 unsigned long event, void *unused)
9006{
Steven Rostedt944ac422008-10-23 19:26:08 -04009007 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009008 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009009 return NOTIFY_OK;
9010}
9011
9012static struct notifier_block trace_panic_notifier = {
9013 .notifier_call = trace_panic_handler,
9014 .next = NULL,
9015 .priority = 150 /* priority: INT_MAX >= x >= 0 */
9016};
9017
9018static int trace_die_handler(struct notifier_block *self,
9019 unsigned long val,
9020 void *data)
9021{
9022 switch (val) {
9023 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04009024 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009025 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009026 break;
9027 default:
9028 break;
9029 }
9030 return NOTIFY_OK;
9031}
9032
9033static struct notifier_block trace_die_notifier = {
9034 .notifier_call = trace_die_handler,
9035 .priority = 200
9036};
9037
9038/*
9039 * printk is set to max of 1024, we really don't need it that big.
9040 * Nothing should be printing 1000 characters anyway.
9041 */
9042#define TRACE_MAX_PRINT 1000
9043
9044/*
9045 * Define here KERN_TRACE so that we have one place to modify
9046 * it if we decide to change what log level the ftrace dump
9047 * should be at.
9048 */
Steven Rostedt428aee12009-01-14 12:24:42 -05009049#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009050
Jason Wessel955b61e2010-08-05 09:22:23 -05009051void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009052trace_printk_seq(struct trace_seq *s)
9053{
9054 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04009055 if (s->seq.len >= TRACE_MAX_PRINT)
9056 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009057
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05009058 /*
9059 * More paranoid code. Although the buffer size is set to
9060 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
9061 * an extra layer of protection.
9062 */
9063 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
9064 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009065
9066 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04009067 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009068
9069 printk(KERN_TRACE "%s", s->buffer);
9070
Steven Rostedtf9520752009-03-02 14:04:40 -05009071 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009072}
9073
Jason Wessel955b61e2010-08-05 09:22:23 -05009074void trace_init_global_iter(struct trace_iterator *iter)
9075{
9076 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009077 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05009078 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009079 iter->array_buffer = &global_trace.array_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07009080
9081 if (iter->trace && iter->trace->open)
9082 iter->trace->open(iter);
9083
9084 /* Annotate start of buffers if we had overruns */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009085 if (ring_buffer_overruns(iter->array_buffer->buffer))
Cody P Schaferb2f974d2013-10-23 11:49:57 -07009086 iter->iter_flags |= TRACE_FILE_ANNOTATE;
9087
9088 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
9089 if (trace_clocks[iter->tr->clock_id].in_ns)
9090 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05009091}
9092
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009093void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009094{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009095 /* use static because iter can be a bit big for the stack */
9096 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009097 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009098 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009099 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04009100 unsigned long flags;
9101 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009102
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009103 /* Only allow one dump user at a time. */
9104 if (atomic_inc_return(&dump_running) != 1) {
9105 atomic_dec(&dump_running);
9106 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04009107 }
9108
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009109 /*
9110 * Always turn off tracing when we dump.
9111 * We don't need to show trace output of what happens
9112 * between multiple crashes.
9113 *
9114 * If the user does a sysrq-z, then they can re-enable
9115 * tracing with echo 1 > tracing_on.
9116 */
9117 tracing_off();
9118
9119 local_irq_save(flags);
Petr Mladek03fc7f92018-06-27 16:20:28 +02009120 printk_nmi_direct_enter();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009121
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08009122 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05009123 trace_init_global_iter(&iter);
9124
Steven Rostedtd7690412008-10-01 00:29:53 -04009125 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009126 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04009127 }
9128
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009129 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009130
Török Edwinb54d3de2008-11-22 13:28:48 +02009131 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009132 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02009133
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009134 switch (oops_dump_mode) {
9135 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05009136 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009137 break;
9138 case DUMP_ORIG:
9139 iter.cpu_file = raw_smp_processor_id();
9140 break;
9141 case DUMP_NONE:
9142 goto out_enable;
9143 default:
9144 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05009145 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009146 }
9147
9148 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009149
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009150 /* Did function tracer already get disabled? */
9151 if (ftrace_is_dead()) {
9152 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
9153 printk("# MAY BE MISSING FUNCTION EVENTS\n");
9154 }
9155
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009156 /*
9157 * We need to stop all tracing on all CPUS to read the
9158 * the next buffer. This is a bit expensive, but is
9159 * not done often. We fill all what we can read,
9160 * and then release the locks again.
9161 */
9162
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009163 while (!trace_empty(&iter)) {
9164
9165 if (!cnt)
9166 printk(KERN_TRACE "---------------------------------\n");
9167
9168 cnt++;
9169
Miguel Ojeda0c97bf82019-05-23 14:45:35 +02009170 trace_iterator_reset(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009171 iter.iter_flags |= TRACE_FILE_LAT_FMT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009172
Jason Wessel955b61e2010-08-05 09:22:23 -05009173 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08009174 int ret;
9175
9176 ret = print_trace_line(&iter);
9177 if (ret != TRACE_TYPE_NO_CONSUME)
9178 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009179 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05009180 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009181
9182 trace_printk_seq(&iter.seq);
9183 }
9184
9185 if (!cnt)
9186 printk(KERN_TRACE " (ftrace buffer empty)\n");
9187 else
9188 printk(KERN_TRACE "---------------------------------\n");
9189
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009190 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009191 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009192
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009193 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009194 atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009195 }
Petr Mladek03fc7f92018-06-27 16:20:28 +02009196 atomic_dec(&dump_running);
9197 printk_nmi_direct_exit();
Steven Rostedtcd891ae2009-04-28 11:39:34 -04009198 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009199}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07009200EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009201
Tom Zanussi7e465ba2017-09-22 14:58:20 -05009202int trace_run_command(const char *buf, int (*createfn)(int, char **))
9203{
9204 char **argv;
9205 int argc, ret;
9206
9207 argc = 0;
9208 ret = 0;
9209 argv = argv_split(GFP_KERNEL, buf, &argc);
9210 if (!argv)
9211 return -ENOMEM;
9212
9213 if (argc)
9214 ret = createfn(argc, argv);
9215
9216 argv_free(argv);
9217
9218 return ret;
9219}
9220
9221#define WRITE_BUFSIZE 4096
9222
9223ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
9224 size_t count, loff_t *ppos,
9225 int (*createfn)(int, char **))
9226{
9227 char *kbuf, *buf, *tmp;
9228 int ret = 0;
9229 size_t done = 0;
9230 size_t size;
9231
9232 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
9233 if (!kbuf)
9234 return -ENOMEM;
9235
9236 while (done < count) {
9237 size = count - done;
9238
9239 if (size >= WRITE_BUFSIZE)
9240 size = WRITE_BUFSIZE - 1;
9241
9242 if (copy_from_user(kbuf, buffer + done, size)) {
9243 ret = -EFAULT;
9244 goto out;
9245 }
9246 kbuf[size] = '\0';
9247 buf = kbuf;
9248 do {
9249 tmp = strchr(buf, '\n');
9250 if (tmp) {
9251 *tmp = '\0';
9252 size = tmp - buf + 1;
9253 } else {
9254 size = strlen(buf);
9255 if (done + size < count) {
9256 if (buf != kbuf)
9257 break;
9258 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
9259 pr_warn("Line length is too long: Should be less than %d\n",
9260 WRITE_BUFSIZE - 2);
9261 ret = -EINVAL;
9262 goto out;
9263 }
9264 }
9265 done += size;
9266
9267 /* Remove comments */
9268 tmp = strchr(buf, '#');
9269
9270 if (tmp)
9271 *tmp = '\0';
9272
9273 ret = trace_run_command(buf, createfn);
9274 if (ret)
9275 goto out;
9276 buf += size;
9277
9278 } while (done < count);
9279 }
9280 ret = done;
9281
9282out:
9283 kfree(kbuf);
9284
9285 return ret;
9286}
9287
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009288__init static int tracer_alloc_buffers(void)
9289{
Steven Rostedt73c51622009-03-11 13:42:01 -04009290 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309291 int ret = -ENOMEM;
9292
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05009293
9294 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11009295 pr_warn("Tracing disabled due to lockdown\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05009296 return -EPERM;
9297 }
9298
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04009299 /*
9300 * Make sure we don't accidently add more trace options
9301 * than we have bits for.
9302 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04009303 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04009304
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309305 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
9306 goto out;
9307
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009308 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309309 goto out_free_buffer_mask;
9310
Steven Rostedt07d777f2011-09-22 14:01:55 -04009311 /* Only allocate trace_printk buffers if a trace_printk exists */
9312 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04009313 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04009314 trace_printk_init_buffers();
9315
Steven Rostedt73c51622009-03-11 13:42:01 -04009316 /* To save memory, keep the ring buffer size to its minimum */
9317 if (ring_buffer_expanded)
9318 ring_buf_size = trace_buf_size;
9319 else
9320 ring_buf_size = 1;
9321
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309322 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009323 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009324
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009325 raw_spin_lock_init(&global_trace.start_lock);
9326
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009327 /*
9328 * The prepare callbacks allocates some memory for the ring buffer. We
9329 * don't free the buffer if the if the CPU goes down. If we were to free
9330 * the buffer, then the user would lose any trace that was in the
9331 * buffer. The memory will be removed once the "instance" is removed.
9332 */
9333 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
9334 "trace/RB:preapre", trace_rb_cpu_prepare,
9335 NULL);
9336 if (ret < 0)
9337 goto out_free_cpumask;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009338 /* Used for event triggers */
Dan Carpenter147d88e02017-08-01 14:02:01 +03009339 ret = -ENOMEM;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009340 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
9341 if (!temp_buffer)
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009342 goto out_rm_hp_state;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009343
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009344 if (trace_create_savedcmd() < 0)
9345 goto out_free_temp_buffer;
9346
Steven Rostedtab464282008-05-12 21:21:00 +02009347 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05009348 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05009349 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009350 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009351 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04009352
Steven Rostedt499e5472012-02-22 15:50:28 -05009353 if (global_trace.buffer_disabled)
9354 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009355
Steven Rostedte1e232c2014-02-10 23:38:46 -05009356 if (trace_boot_clock) {
9357 ret = tracing_set_clock(&global_trace, trace_boot_clock);
9358 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07009359 pr_warn("Trace clock %s not defined, going back to default\n",
9360 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05009361 }
9362
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04009363 /*
9364 * register_tracer() might reference current_trace, so it
9365 * needs to be set before we register anything. This is
9366 * just a bootstrap of current_trace anyway.
9367 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009368 global_trace.current_trace = &nop_trace;
9369
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05009370 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9371
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05009372 ftrace_init_global_array_ops(&global_trace);
9373
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04009374 init_trace_flags_index(&global_trace);
9375
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04009376 register_tracer(&nop_trace);
9377
Steven Rostedt (VMware)dbeafd02017-03-03 13:48:42 -05009378 /* Function tracing may start here (via kernel command line) */
9379 init_function_trace();
9380
Steven Rostedt60a11772008-05-12 21:20:44 +02009381 /* All seems OK, enable tracing */
9382 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009383
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009384 atomic_notifier_chain_register(&panic_notifier_list,
9385 &trace_panic_notifier);
9386
9387 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01009388
Steven Rostedtae63b31e2012-05-03 23:09:03 -04009389 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
9390
9391 INIT_LIST_HEAD(&global_trace.systems);
9392 INIT_LIST_HEAD(&global_trace.events);
Tom Zanussi067fe032018-01-15 20:51:56 -06009393 INIT_LIST_HEAD(&global_trace.hist_vars);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04009394 INIT_LIST_HEAD(&global_trace.err_log);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04009395 list_add(&global_trace.list, &ftrace_trace_arrays);
9396
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08009397 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04009398
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04009399 register_snapshot_cmd();
9400
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01009401 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009402
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009403out_free_savedcmd:
9404 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009405out_free_temp_buffer:
9406 ring_buffer_free(temp_buffer);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009407out_rm_hp_state:
9408 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309409out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009410 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309411out_free_buffer_mask:
9412 free_cpumask_var(tracing_buffer_mask);
9413out:
9414 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009415}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05009416
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05009417void __init early_trace_init(void)
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009418{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05009419 if (tracepoint_printk) {
9420 tracepoint_print_iter =
9421 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05009422 if (MEM_FAIL(!tracepoint_print_iter,
9423 "Failed to allocate trace iterator\n"))
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05009424 tracepoint_printk = 0;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05009425 else
9426 static_key_enable(&tracepoint_printk_key.key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05009427 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009428 tracer_alloc_buffers();
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05009429}
9430
9431void __init trace_init(void)
9432{
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04009433 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009434}
9435
Steven Rostedtb2821ae2009-02-02 21:38:32 -05009436__init static int clear_boot_tracer(void)
9437{
9438 /*
9439 * The default tracer at boot buffer is an init section.
9440 * This function is called in lateinit. If we did not
9441 * find the boot tracer, then clear it out, to prevent
9442 * later registration from accessing the buffer that is
9443 * about to be freed.
9444 */
9445 if (!default_bootup_tracer)
9446 return 0;
9447
9448 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
9449 default_bootup_tracer);
9450 default_bootup_tracer = NULL;
9451
9452 return 0;
9453}
9454
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05009455fs_initcall(tracer_init_tracefs);
Steven Rostedt (VMware)4bb0f0e2017-08-01 12:01:52 -04009456late_initcall_sync(clear_boot_tracer);
Chris Wilson3fd49c92018-03-30 16:01:31 +01009457
9458#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
9459__init static int tracing_set_default_clock(void)
9460{
9461 /* sched_clock_stable() is determined in late_initcall */
Chris Wilson5125eee2018-04-04 22:24:50 +01009462 if (!trace_boot_clock && !sched_clock_stable()) {
Chris Wilson3fd49c92018-03-30 16:01:31 +01009463 printk(KERN_WARNING
9464 "Unstable clock detected, switching default tracing clock to \"global\"\n"
9465 "If you want to keep using the local clock, then add:\n"
9466 " \"trace_clock=local\"\n"
9467 "on the kernel command line\n");
9468 tracing_set_clock(&global_trace, "global");
9469 }
9470
9471 return 0;
9472}
9473late_initcall_sync(tracing_set_default_clock);
9474#endif