blob: cb850d2c4bfac9c6a54fce02dbd0c59640ed9914 [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
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -0500165static int 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 (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500869 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
870 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500871 if (!event)
872 return 0;
873
874 entry = ring_buffer_event_data(event);
875 entry->ip = ip;
876
877 memcpy(&entry->buf, str, size);
878
879 /* Add a newline if necessary */
880 if (entry->buf[size - 1] != '\n') {
881 entry->buf[size] = '\n';
882 entry->buf[size + 1] = '\0';
883 } else
884 entry->buf[size] = '\0';
885
886 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400887 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500888
889 return size;
890}
891EXPORT_SYMBOL_GPL(__trace_puts);
892
893/**
894 * __trace_bputs - write the pointer to a constant string into trace buffer
895 * @ip: The address of the caller
896 * @str: The constant string to write to the buffer to
897 */
898int __trace_bputs(unsigned long ip, const char *str)
899{
900 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500901 struct trace_buffer *buffer;
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500902 struct bputs_entry *entry;
903 unsigned long irq_flags;
904 int size = sizeof(struct bputs_entry);
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800905 int pc;
906
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400907 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
zhangwei(Jovi)f0160a52013-07-18 16:31:18 +0800908 return 0;
909
zhangwei(Jovi)8abfb872013-07-18 16:31:05 +0800910 pc = preempt_count();
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500911
Steven Rostedt (Red Hat)3132e102014-01-23 12:27:59 -0500912 if (unlikely(tracing_selftest_running || tracing_disabled))
913 return 0;
914
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500915 local_save_flags(irq_flags);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500916 buffer = global_trace.array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -0500917 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
918 irq_flags, pc);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500919 if (!event)
920 return 0;
921
922 entry = ring_buffer_event_data(event);
923 entry->ip = ip;
924 entry->str = str;
925
926 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -0400927 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -0500928
929 return 1;
930}
931EXPORT_SYMBOL_GPL(__trace_bputs);
932
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500933#ifdef CONFIG_TRACER_SNAPSHOT
Tom Zanussia35873a2019-02-13 17:42:45 -0600934void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500935{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500936 struct tracer *tracer = tr->current_trace;
937 unsigned long flags;
938
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500939 if (in_nmi()) {
940 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
941 internal_trace_puts("*** snapshot is being ignored ***\n");
942 return;
943 }
944
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500945 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500946 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
947 internal_trace_puts("*** stopping trace here! ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500948 tracing_off();
949 return;
950 }
951
952 /* Note, snapshot can not be used when the tracer uses it */
953 if (tracer->use_max_tr) {
Steven Rostedt (Red Hat)ca268da2013-03-09 00:40:58 -0500954 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
955 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500956 return;
957 }
958
959 local_irq_save(flags);
Tom Zanussia35873a2019-02-13 17:42:45 -0600960 update_max_tr(tr, current, smp_processor_id(), cond_data);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500961 local_irq_restore(flags);
962}
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400963
Tom Zanussia35873a2019-02-13 17:42:45 -0600964void tracing_snapshot_instance(struct trace_array *tr)
965{
966 tracing_snapshot_instance_cond(tr, NULL);
967}
968
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400969/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +0800970 * tracing_snapshot - take a snapshot of the current buffer.
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -0400971 *
972 * This causes a swap between the snapshot buffer and the current live
973 * tracing buffer. You can use this to take snapshots of the live
974 * trace when some condition is triggered, but continue to trace.
975 *
976 * Note, make sure to allocate the snapshot with either
977 * a tracing_snapshot_alloc(), or by doing it manually
978 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
979 *
980 * If the snapshot buffer is not allocated, it will stop tracing.
981 * Basically making a permanent snapshot.
982 */
983void tracing_snapshot(void)
984{
985 struct trace_array *tr = &global_trace;
986
987 tracing_snapshot_instance(tr);
988}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -0500989EXPORT_SYMBOL_GPL(tracing_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -0500990
Tom Zanussia35873a2019-02-13 17:42:45 -0600991/**
992 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
993 * @tr: The tracing instance to snapshot
994 * @cond_data: The data to be tested conditionally, and possibly saved
995 *
996 * This is the same as tracing_snapshot() except that the snapshot is
997 * conditional - the snapshot will only happen if the
998 * cond_snapshot.update() implementation receiving the cond_data
999 * returns true, which means that the trace array's cond_snapshot
1000 * update() operation used the cond_data to determine whether the
1001 * snapshot should be taken, and if it was, presumably saved it along
1002 * with the snapshot.
1003 */
1004void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1005{
1006 tracing_snapshot_instance_cond(tr, cond_data);
1007}
1008EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1009
1010/**
1011 * tracing_snapshot_cond_data - get the user data associated with a snapshot
1012 * @tr: The tracing instance
1013 *
1014 * When the user enables a conditional snapshot using
1015 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1016 * with the snapshot. This accessor is used to retrieve it.
1017 *
1018 * Should not be called from cond_snapshot.update(), since it takes
1019 * the tr->max_lock lock, which the code calling
1020 * cond_snapshot.update() has already done.
1021 *
1022 * Returns the cond_data associated with the trace array's snapshot.
1023 */
1024void *tracing_cond_snapshot_data(struct trace_array *tr)
1025{
1026 void *cond_data = NULL;
1027
1028 arch_spin_lock(&tr->max_lock);
1029
1030 if (tr->cond_snapshot)
1031 cond_data = tr->cond_snapshot->cond_data;
1032
1033 arch_spin_unlock(&tr->max_lock);
1034
1035 return cond_data;
1036}
1037EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1038
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001039static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1040 struct array_buffer *size_buf, int cpu_id);
1041static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001042
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001043int tracing_alloc_snapshot_instance(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001044{
1045 int ret;
1046
1047 if (!tr->allocated_snapshot) {
1048
1049 /* allocate spare buffer */
1050 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001051 &tr->array_buffer, RING_BUFFER_ALL_CPUS);
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001052 if (ret < 0)
1053 return ret;
1054
1055 tr->allocated_snapshot = true;
1056 }
1057
1058 return 0;
1059}
1060
Fabian Frederickad1438a2014-04-17 21:44:42 +02001061static void free_snapshot(struct trace_array *tr)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001062{
1063 /*
1064 * We don't free the ring buffer. instead, resize it because
1065 * The max_tr ring buffer has some state (e.g. ring->clock) and
1066 * we want preserve it.
1067 */
1068 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1069 set_buffer_entries(&tr->max_buffer, 1);
1070 tracing_reset_online_cpus(&tr->max_buffer);
1071 tr->allocated_snapshot = false;
1072}
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001073
1074/**
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001075 * tracing_alloc_snapshot - allocate snapshot buffer.
1076 *
1077 * This only allocates the snapshot buffer if it isn't already
1078 * allocated - it doesn't also take a snapshot.
1079 *
1080 * This is meant to be used in cases where the snapshot buffer needs
1081 * to be set up for events that can't sleep but need to be able to
1082 * trigger a snapshot.
1083 */
1084int tracing_alloc_snapshot(void)
1085{
1086 struct trace_array *tr = &global_trace;
1087 int ret;
1088
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001089 ret = tracing_alloc_snapshot_instance(tr);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001090 WARN_ON(ret < 0);
1091
1092 return ret;
1093}
1094EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1095
1096/**
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001097 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001098 *
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001099 * This is similar to tracing_snapshot(), but it will allocate the
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001100 * snapshot buffer if it isn't already allocated. Use this only
1101 * where it is safe to sleep, as the allocation may sleep.
1102 *
1103 * This causes a swap between the snapshot buffer and the current live
1104 * tracing buffer. You can use this to take snapshots of the live
1105 * trace when some condition is triggered, but continue to trace.
1106 */
1107void tracing_snapshot_alloc(void)
1108{
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001109 int ret;
1110
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001111 ret = tracing_alloc_snapshot();
1112 if (ret < 0)
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04001113 return;
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001114
1115 tracing_snapshot();
1116}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001117EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001118
1119/**
1120 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1121 * @tr: The tracing instance
1122 * @cond_data: User data to associate with the snapshot
1123 * @update: Implementation of the cond_snapshot update function
1124 *
1125 * Check whether the conditional snapshot for the given instance has
1126 * already been enabled, or if the current tracer is already using a
1127 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1128 * save the cond_data and update function inside.
1129 *
1130 * Returns 0 if successful, error otherwise.
1131 */
1132int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1133 cond_update_fn_t update)
1134{
1135 struct cond_snapshot *cond_snapshot;
1136 int ret = 0;
1137
1138 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1139 if (!cond_snapshot)
1140 return -ENOMEM;
1141
1142 cond_snapshot->cond_data = cond_data;
1143 cond_snapshot->update = update;
1144
1145 mutex_lock(&trace_types_lock);
1146
1147 ret = tracing_alloc_snapshot_instance(tr);
1148 if (ret)
1149 goto fail_unlock;
1150
1151 if (tr->current_trace->use_max_tr) {
1152 ret = -EBUSY;
1153 goto fail_unlock;
1154 }
1155
Steven Rostedt (VMware)1c347a92019-02-14 18:45:21 -05001156 /*
1157 * The cond_snapshot can only change to NULL without the
1158 * trace_types_lock. We don't care if we race with it going
1159 * to NULL, but we want to make sure that it's not set to
1160 * something other than NULL when we get here, which we can
1161 * do safely with only holding the trace_types_lock and not
1162 * having to take the max_lock.
1163 */
Tom Zanussia35873a2019-02-13 17:42:45 -06001164 if (tr->cond_snapshot) {
1165 ret = -EBUSY;
1166 goto fail_unlock;
1167 }
1168
1169 arch_spin_lock(&tr->max_lock);
1170 tr->cond_snapshot = cond_snapshot;
1171 arch_spin_unlock(&tr->max_lock);
1172
1173 mutex_unlock(&trace_types_lock);
1174
1175 return ret;
1176
1177 fail_unlock:
1178 mutex_unlock(&trace_types_lock);
1179 kfree(cond_snapshot);
1180 return ret;
1181}
1182EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1183
1184/**
1185 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1186 * @tr: The tracing instance
1187 *
1188 * Check whether the conditional snapshot for the given instance is
1189 * enabled; if so, free the cond_snapshot associated with it,
1190 * otherwise return -EINVAL.
1191 *
1192 * Returns 0 if successful, error otherwise.
1193 */
1194int tracing_snapshot_cond_disable(struct trace_array *tr)
1195{
1196 int ret = 0;
1197
1198 arch_spin_lock(&tr->max_lock);
1199
1200 if (!tr->cond_snapshot)
1201 ret = -EINVAL;
1202 else {
1203 kfree(tr->cond_snapshot);
1204 tr->cond_snapshot = NULL;
1205 }
1206
1207 arch_spin_unlock(&tr->max_lock);
1208
1209 return ret;
1210}
1211EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001212#else
1213void tracing_snapshot(void)
1214{
1215 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1216}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001217EXPORT_SYMBOL_GPL(tracing_snapshot);
Tom Zanussia35873a2019-02-13 17:42:45 -06001218void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1219{
1220 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1221}
1222EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001223int tracing_alloc_snapshot(void)
1224{
1225 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1226 return -ENODEV;
1227}
1228EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001229void tracing_snapshot_alloc(void)
1230{
1231 /* Give warning */
1232 tracing_snapshot();
1233}
Steven Rostedt (Red Hat)1b22e382013-03-09 00:56:08 -05001234EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
Tom Zanussia35873a2019-02-13 17:42:45 -06001235void *tracing_cond_snapshot_data(struct trace_array *tr)
1236{
1237 return NULL;
1238}
1239EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1240int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1241{
1242 return -ENODEV;
1243}
1244EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1245int tracing_snapshot_cond_disable(struct trace_array *tr)
1246{
1247 return false;
1248}
1249EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
Steven Rostedt (Red Hat)ad909e22013-03-06 21:45:37 -05001250#endif /* CONFIG_TRACER_SNAPSHOT */
1251
Steven Rostedt (VMware)2290f2c2017-04-20 11:46:03 -04001252void tracer_tracing_off(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001253{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001254 if (tr->array_buffer.buffer)
1255 ring_buffer_record_off(tr->array_buffer.buffer);
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001256 /*
1257 * This flag is looked at when buffers haven't been allocated
1258 * yet, or by some tracers (like irqsoff), that just want to
1259 * know if the ring buffer has been disabled, but it can handle
1260 * races of where it gets disabled but we still do a record.
1261 * As the check is in the fast path of the tracers, it is more
1262 * important to be fast than accurate.
1263 */
1264 tr->buffer_disabled = 1;
1265 /* Make the flag seen by readers */
1266 smp_wmb();
1267}
1268
Steven Rostedt499e5472012-02-22 15:50:28 -05001269/**
1270 * tracing_off - turn off tracing buffers
1271 *
1272 * This function stops the tracing buffers from recording data.
1273 * It does not disable any overhead the tracers themselves may
1274 * be causing. This function simply causes all recording to
1275 * the ring buffers to fail.
1276 */
1277void tracing_off(void)
1278{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001279 tracer_tracing_off(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001280}
1281EXPORT_SYMBOL_GPL(tracing_off);
1282
Steven Rostedt (Red Hat)de7edd32013-06-14 16:21:43 -04001283void disable_trace_on_warning(void)
1284{
1285 if (__disable_trace_on_warning)
1286 tracing_off();
1287}
1288
Steven Rostedt499e5472012-02-22 15:50:28 -05001289/**
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001290 * tracer_tracing_is_on - show real state of ring buffer enabled
1291 * @tr : the trace array to know if ring buffer is enabled
1292 *
1293 * Shows real state of the ring buffer if it is enabled or not.
1294 */
Steven Rostedt (VMware)ec573502018-08-01 16:08:57 -04001295bool tracer_tracing_is_on(struct trace_array *tr)
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001296{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001297 if (tr->array_buffer.buffer)
1298 return ring_buffer_record_is_on(tr->array_buffer.buffer);
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001299 return !tr->buffer_disabled;
1300}
1301
Steven Rostedt499e5472012-02-22 15:50:28 -05001302/**
1303 * tracing_is_on - show state of ring buffers enabled
1304 */
1305int tracing_is_on(void)
1306{
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04001307 return tracer_tracing_is_on(&global_trace);
Steven Rostedt499e5472012-02-22 15:50:28 -05001308}
1309EXPORT_SYMBOL_GPL(tracing_is_on);
1310
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001311static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001312{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001313 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001314
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001315 if (!str)
1316 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +08001317 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001318 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +08001319 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02001320 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001321 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322 return 1;
1323}
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001324__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325
Tim Bird0e950172010-02-25 15:36:43 -08001326static int __init set_tracing_thresh(char *str)
1327{
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001328 unsigned long threshold;
Tim Bird0e950172010-02-25 15:36:43 -08001329 int ret;
1330
1331 if (!str)
1332 return 0;
Daniel Walterbcd83ea2012-09-26 22:08:38 +02001333 ret = kstrtoul(str, 0, &threshold);
Tim Bird0e950172010-02-25 15:36:43 -08001334 if (ret < 0)
1335 return 0;
Wang Tianhong87abb3b2012-08-02 14:02:00 +08001336 tracing_thresh = threshold * 1000;
Tim Bird0e950172010-02-25 15:36:43 -08001337 return 1;
1338}
1339__setup("tracing_thresh=", set_tracing_thresh);
1340
Steven Rostedt57f50be2008-05-12 21:20:44 +02001341unsigned long nsecs_to_usecs(unsigned long nsecs)
1342{
1343 return nsecs / 1000;
1344}
1345
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001346/*
1347 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001348 * 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 -04001349 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
Jeremy Lintonf57a4142017-05-31 16:56:48 -05001350 * of strings in the order that the evals (enum) were defined.
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001351 */
1352#undef C
1353#define C(a, b) b
1354
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001355/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001356static const char *trace_options[] = {
Steven Rostedt (Red Hat)a3418a32015-09-29 09:43:30 -04001357 TRACE_FLAGS
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358 NULL
1359};
1360
Zhaolei5079f322009-08-25 16:12:56 +08001361static struct {
1362 u64 (*func)(void);
1363 const char *name;
David Sharp8be07092012-11-13 12:18:22 -08001364 int in_ns; /* is this clock in nanoseconds? */
Zhaolei5079f322009-08-25 16:12:56 +08001365} trace_clocks[] = {
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001366 { trace_clock_local, "local", 1 },
1367 { trace_clock_global, "global", 1 },
1368 { trace_clock_counter, "counter", 0 },
Linus Torvaldse7fda6c2014-08-05 17:46:42 -07001369 { trace_clock_jiffies, "uptime", 0 },
Thomas Gleixner1b3e5c02014-07-16 21:05:25 +00001370 { trace_clock, "perf", 1 },
1371 { ktime_get_mono_fast_ns, "mono", 1 },
Drew Richardsonaabfa5f2015-05-08 07:30:39 -07001372 { ktime_get_raw_fast_ns, "mono_raw", 1 },
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001373 { ktime_get_boot_fast_ns, "boot", 1 },
David Sharp8cbd9cc2012-11-13 12:18:21 -08001374 ARCH_TRACE_CLOCKS
Zhaolei5079f322009-08-25 16:12:56 +08001375};
1376
Tom Zanussi860f9f62018-01-15 20:51:48 -06001377bool trace_clock_in_ns(struct trace_array *tr)
1378{
1379 if (trace_clocks[tr->clock_id].in_ns)
1380 return true;
1381
1382 return false;
1383}
1384
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001385/*
1386 * trace_parser_get_init - gets the buffer for trace parser
1387 */
1388int trace_parser_get_init(struct trace_parser *parser, int size)
1389{
1390 memset(parser, 0, sizeof(*parser));
1391
1392 parser->buffer = kmalloc(size, GFP_KERNEL);
1393 if (!parser->buffer)
1394 return 1;
1395
1396 parser->size = size;
1397 return 0;
1398}
1399
1400/*
1401 * trace_parser_put - frees the buffer for trace parser
1402 */
1403void trace_parser_put(struct trace_parser *parser)
1404{
1405 kfree(parser->buffer);
Steven Rostedt (VMware)0e684b62017-02-02 17:58:18 -05001406 parser->buffer = NULL;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001407}
1408
1409/*
1410 * trace_get_user - reads the user input string separated by space
1411 * (matched by isspace(ch))
1412 *
1413 * For each string found the 'struct trace_parser' is updated,
1414 * and the function returns.
1415 *
1416 * Returns number of bytes read.
1417 *
1418 * See kernel/trace/trace.h for 'struct trace_parser' details.
1419 */
1420int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1421 size_t cnt, loff_t *ppos)
1422{
1423 char ch;
1424 size_t read = 0;
1425 ssize_t ret;
1426
1427 if (!*ppos)
1428 trace_parser_clear(parser);
1429
1430 ret = get_user(ch, ubuf++);
1431 if (ret)
1432 goto out;
1433
1434 read++;
1435 cnt--;
1436
1437 /*
1438 * The parser is not finished with the last write,
1439 * continue reading the user input without skipping spaces.
1440 */
1441 if (!parser->cont) {
1442 /* skip white space */
1443 while (cnt && isspace(ch)) {
1444 ret = get_user(ch, ubuf++);
1445 if (ret)
1446 goto out;
1447 read++;
1448 cnt--;
1449 }
1450
Changbin Du76638d92018-01-16 17:02:29 +08001451 parser->idx = 0;
1452
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001453 /* only spaces were written */
Changbin Du921a7ac2018-01-16 17:02:28 +08001454 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001455 *ppos += read;
1456 ret = read;
1457 goto out;
1458 }
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001459 }
1460
1461 /* read the non-space input */
Changbin Du921a7ac2018-01-16 17:02:28 +08001462 while (cnt && !isspace(ch) && ch) {
Li Zefan3c235a32009-09-22 13:51:54 +08001463 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001464 parser->buffer[parser->idx++] = ch;
1465 else {
1466 ret = -EINVAL;
1467 goto out;
1468 }
1469 ret = get_user(ch, ubuf++);
1470 if (ret)
1471 goto out;
1472 read++;
1473 cnt--;
1474 }
1475
1476 /* We either got finished input or we have to wait for another call. */
Changbin Du921a7ac2018-01-16 17:02:28 +08001477 if (isspace(ch) || !ch) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001478 parser->buffer[parser->idx] = 0;
1479 parser->cont = false;
Steven Rostedt057db842013-10-09 22:23:23 -04001480 } else if (parser->idx < parser->size - 1) {
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001481 parser->cont = true;
1482 parser->buffer[parser->idx++] = ch;
Changbin Duf4d07062018-01-16 17:02:30 +08001483 /* Make sure the parsed string always terminates with '\0'. */
1484 parser->buffer[parser->idx] = 0;
Steven Rostedt057db842013-10-09 22:23:23 -04001485 } else {
1486 ret = -EINVAL;
1487 goto out;
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +02001488 }
1489
1490 *ppos += read;
1491 ret = read;
1492
1493out:
1494 return ret;
1495}
1496
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001497/* TODO add a seq_buf_to_buffer() */
Dmitri Vorobievb8b94262009-03-22 19:11:11 +02001498static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001499{
1500 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001501
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001502 if (trace_seq_used(s) <= s->seq.readpos)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001503 return -EBUSY;
1504
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001505 len = trace_seq_used(s) - s->seq.readpos;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001506 if (cnt > len)
1507 cnt = len;
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001508 memcpy(buf, s->buffer + s->seq.readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001509
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001510 s->seq.readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02001511 return cnt;
1512}
1513
Tim Bird0e950172010-02-25 15:36:43 -08001514unsigned long __read_mostly tracing_thresh;
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02001515static const struct file_operations tracing_max_lat_fops;
1516
1517#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1518 defined(CONFIG_FSNOTIFY)
1519
1520static struct workqueue_struct *fsnotify_wq;
1521
1522static void latency_fsnotify_workfn(struct work_struct *work)
1523{
1524 struct trace_array *tr = container_of(work, struct trace_array,
1525 fsnotify_work);
1526 fsnotify(tr->d_max_latency->d_inode, FS_MODIFY,
1527 tr->d_max_latency->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
1528}
1529
1530static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1531{
1532 struct trace_array *tr = container_of(iwork, struct trace_array,
1533 fsnotify_irqwork);
1534 queue_work(fsnotify_wq, &tr->fsnotify_work);
1535}
1536
1537static void trace_create_maxlat_file(struct trace_array *tr,
1538 struct dentry *d_tracer)
1539{
1540 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1541 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1542 tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
1543 d_tracer, &tr->max_latency,
1544 &tracing_max_lat_fops);
1545}
1546
1547__init static int latency_fsnotify_init(void)
1548{
1549 fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1550 WQ_UNBOUND | WQ_HIGHPRI, 0);
1551 if (!fsnotify_wq) {
1552 pr_err("Unable to allocate tr_max_lat_wq\n");
1553 return -ENOMEM;
1554 }
1555 return 0;
1556}
1557
1558late_initcall_sync(latency_fsnotify_init);
1559
1560void latency_fsnotify(struct trace_array *tr)
1561{
1562 if (!fsnotify_wq)
1563 return;
1564 /*
1565 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1566 * possible that we are called from __schedule() or do_idle(), which
1567 * could cause a deadlock.
1568 */
1569 irq_work_queue(&tr->fsnotify_irqwork);
1570}
1571
1572/*
1573 * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1574 * defined(CONFIG_FSNOTIFY)
1575 */
1576#else
1577
1578#define trace_create_maxlat_file(tr, d_tracer) \
1579 trace_create_file("tracing_max_latency", 0644, d_tracer, \
1580 &tr->max_latency, &tracing_max_lat_fops)
1581
1582#endif
Tim Bird0e950172010-02-25 15:36:43 -08001583
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001584#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001585/*
1586 * Copy the new maximum trace into the separate maximum-trace
1587 * structure. (this way the maximum trace is permanently saved,
Chunyu Hu5a93bae2017-10-19 14:32:33 +08001588 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001589 */
1590static void
1591__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1592{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001593 struct array_buffer *trace_buf = &tr->array_buffer;
1594 struct array_buffer *max_buf = &tr->max_buffer;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001595 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1596 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001597
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001598 max_buf->cpu = cpu;
1599 max_buf->time_start = data->preempt_timestamp;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001600
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001601 max_data->saved_latency = tr->max_latency;
Steven Rostedt8248ac02009-09-02 12:27:41 -04001602 max_data->critical_start = data->critical_start;
1603 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001604
Tom Zanussi85f726a2019-03-05 10:12:00 -06001605 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001606 max_data->pid = tsk->pid;
Steven Rostedt (Red Hat)f17a5192013-05-30 21:10:37 -04001607 /*
1608 * If tsk == current, then use current_uid(), as that does not use
1609 * RCU. The irq tracer can be called out of RCU scope.
1610 */
1611 if (tsk == current)
1612 max_data->uid = current_uid();
1613 else
1614 max_data->uid = task_uid(tsk);
1615
Steven Rostedt8248ac02009-09-02 12:27:41 -04001616 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1617 max_data->policy = tsk->policy;
1618 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001619
1620 /* record this tasks comm */
1621 tracing_record_cmdline(tsk);
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02001622 latency_fsnotify(tr);
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001623}
1624
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001625/**
1626 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1627 * @tr: tracer
1628 * @tsk: the task with the latency
1629 * @cpu: The cpu that initiated the trace.
Tom Zanussia35873a2019-02-13 17:42:45 -06001630 * @cond_data: User data associated with a conditional snapshot
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001631 *
1632 * Flip the buffers between the @tr and the max_tr and record information
1633 * about which task was the cause of this latency.
1634 */
Ingo Molnare309b412008-05-12 21:20:51 +02001635void
Tom Zanussia35873a2019-02-13 17:42:45 -06001636update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1637 void *cond_data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001638{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001639 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001640 return;
1641
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001642 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt34600f02013-01-22 13:35:11 -05001643
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05001644 if (!tr->allocated_snapshot) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001645 /* Only the nop tracer should hit this when disabling */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001646 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001647 return;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09001648 }
Steven Rostedt34600f02013-01-22 13:35:11 -05001649
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001650 arch_spin_lock(&tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001651
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001652 /* Inherit the recordable setting from array_buffer */
1653 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
Masami Hiramatsu73c8d892018-07-14 01:28:15 +09001654 ring_buffer_record_on(tr->max_buffer.buffer);
1655 else
1656 ring_buffer_record_off(tr->max_buffer.buffer);
1657
Tom Zanussia35873a2019-02-13 17:42:45 -06001658#ifdef CONFIG_TRACER_SNAPSHOT
1659 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1660 goto out_unlock;
1661#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001662 swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001663
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001664 __update_max_tr(tr, tsk, cpu);
Tom Zanussia35873a2019-02-13 17:42:45 -06001665
1666 out_unlock:
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001667 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001668}
1669
1670/**
1671 * update_max_tr_single - only copy one trace over, and reset the rest
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07001672 * @tr: tracer
1673 * @tsk: task with the latency
1674 * @cpu: the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001675 *
1676 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001677 */
Ingo Molnare309b412008-05-12 21:20:51 +02001678void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001679update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1680{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001681 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001682
Steven Rostedt2b6080f2012-05-11 13:29:49 -04001683 if (tr->stop_count)
Steven Rostedtb8de7bd12009-08-31 22:32:27 -04001684 return;
1685
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001686 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt6c244992013-04-29 20:08:14 -04001687 if (!tr->allocated_snapshot) {
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001688 /* Only the nop tracer should hit this when disabling */
Linus Torvalds9e8529a2013-04-29 13:55:38 -07001689 WARN_ON_ONCE(tr->current_trace != &nop_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001690 return;
Steven Rostedt (Red Hat)2930e042013-03-26 17:33:00 -04001691 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09001692
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001693 arch_spin_lock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001694
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001695 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001696
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001697 if (ret == -EBUSY) {
1698 /*
1699 * We failed to swap the buffer due to a commit taking
1700 * place on this CPU. We fail to record, but we reset
1701 * the max trace buffer (no one writes directly to it)
1702 * and flag that it failed.
1703 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001704 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001705 "Failed to swap buffers due to commit in progress\n");
1706 }
1707
Steven Rostedte8165dbb2009-09-03 19:13:05 -04001708 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001709
1710 __update_max_tr(tr, tsk, cpu);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05001711 arch_spin_unlock(&tr->max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001712}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04001713#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001714
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05001715static int wait_on_pipe(struct trace_iterator *iter, int full)
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001716{
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05001717 /* Iterators are static, they should be filled or empty */
1718 if (trace_buffer_iter(iter, iter->cpu_file))
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04001719 return 0;
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001720
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001721 return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
Rabin Vincente30f53a2014-11-10 19:46:34 +01001722 full);
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04001723}
1724
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001725#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001726static bool selftests_can_run;
1727
1728struct trace_selftests {
1729 struct list_head list;
1730 struct tracer *type;
1731};
1732
1733static LIST_HEAD(postponed_selftests);
1734
1735static int save_selftest(struct tracer *type)
1736{
1737 struct trace_selftests *selftest;
1738
1739 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1740 if (!selftest)
1741 return -ENOMEM;
1742
1743 selftest->type = type;
1744 list_add(&selftest->list, &postponed_selftests);
1745 return 0;
1746}
1747
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001748static int run_tracer_selftest(struct tracer *type)
1749{
1750 struct trace_array *tr = &global_trace;
1751 struct tracer *saved_tracer = tr->current_trace;
1752 int ret;
1753
1754 if (!type->selftest || tracing_selftest_disabled)
1755 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001756
1757 /*
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001758 * If a tracer registers early in boot up (before scheduling is
1759 * initialized and such), then do not run its selftests yet.
1760 * Instead, run it a little later in the boot process.
1761 */
1762 if (!selftests_can_run)
1763 return save_selftest(type);
1764
1765 /*
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001766 * Run a selftest on this tracer.
1767 * Here we reset the trace buffer, and set the current
1768 * tracer to be this tracer. The tracer can then run some
1769 * internal tracing to verify that everything is in order.
1770 * If we fail, we do not register this tracer.
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001771 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001772 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001773
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001774 tr->current_trace = type;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001776#ifdef CONFIG_TRACER_MAX_TRACE
1777 if (type->use_max_tr) {
1778 /* If we expanded the buffers, make sure the max is expanded too */
1779 if (ring_buffer_expanded)
1780 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1781 RING_BUFFER_ALL_CPUS);
1782 tr->allocated_snapshot = true;
1783 }
1784#endif
1785
1786 /* the test is responsible for initializing and enabling */
1787 pr_info("Testing tracer %s: ", type->name);
1788 ret = type->selftest(type, tr);
1789 /* the test is responsible for resetting too */
1790 tr->current_trace = saved_tracer;
1791 if (ret) {
1792 printk(KERN_CONT "FAILED!\n");
1793 /* Add the warning after printing 'FAILED' */
1794 WARN_ON(1);
1795 return -1;
1796 }
1797 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001798 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001799
1800#ifdef CONFIG_TRACER_MAX_TRACE
1801 if (type->use_max_tr) {
1802 tr->allocated_snapshot = false;
1803
1804 /* Shrink the max buffer again */
1805 if (ring_buffer_expanded)
1806 ring_buffer_resize(tr->max_buffer.buffer, 1,
1807 RING_BUFFER_ALL_CPUS);
1808 }
1809#endif
1810
1811 printk(KERN_CONT "PASSED\n");
1812 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001813}
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001814
1815static __init int init_trace_selftests(void)
1816{
1817 struct trace_selftests *p, *n;
1818 struct tracer *t, **last;
1819 int ret;
1820
1821 selftests_can_run = true;
1822
1823 mutex_lock(&trace_types_lock);
1824
1825 if (list_empty(&postponed_selftests))
1826 goto out;
1827
1828 pr_info("Running postponed tracer tests:\n");
1829
1830 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
Anders Roxell6fc21712018-11-30 15:56:22 +01001831 /* This loop can take minutes when sanitizers are enabled, so
1832 * lets make sure we allow RCU processing.
1833 */
1834 cond_resched();
Steven Rostedt (VMware)9afecfb2017-03-24 17:59:10 -04001835 ret = run_tracer_selftest(p->type);
1836 /* If the test fails, then warn and remove from available_tracers */
1837 if (ret < 0) {
1838 WARN(1, "tracer: %s failed selftest, disabling\n",
1839 p->type->name);
1840 last = &trace_types;
1841 for (t = trace_types; t; t = t->next) {
1842 if (t == p->type) {
1843 *last = t->next;
1844 break;
1845 }
1846 last = &t->next;
1847 }
1848 }
1849 list_del(&p->list);
1850 kfree(p);
1851 }
1852
1853 out:
1854 mutex_unlock(&trace_types_lock);
1855
1856 return 0;
1857}
Steven Rostedtb9ef0322017-05-17 11:14:35 -04001858core_initcall(init_trace_selftests);
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001859#else
1860static inline int run_tracer_selftest(struct tracer *type)
1861{
1862 return 0;
1863}
1864#endif /* CONFIG_FTRACE_STARTUP_TEST */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001865
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001866static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1867
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001868static void __init apply_trace_boot_options(void);
1869
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001870/**
1871 * register_tracer - register a tracer with the ftrace system.
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07001872 * @type: the plugin for the tracer
Steven Rostedt4fcdae82008-05-12 21:21:00 +02001873 *
1874 * Register a new plugin tracer.
1875 */
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001876int __init register_tracer(struct tracer *type)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877{
1878 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001879 int ret = 0;
1880
1881 if (!type->name) {
1882 pr_info("Tracer must have a name\n");
1883 return -1;
1884 }
1885
Dan Carpenter24a461d2010-07-10 12:06:44 +02001886 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +08001887 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1888 return -1;
1889 }
1890
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05001891 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11001892 pr_warn("Can not register tracer %s due to lockdown\n",
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05001893 type->name);
1894 return -EPERM;
1895 }
1896
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001897 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +01001898
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001899 tracing_selftest_running = true;
1900
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001901 for (t = trace_types; t; t = t->next) {
1902 if (strcmp(type->name, t->name) == 0) {
1903 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +08001904 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001905 type->name);
1906 ret = -1;
1907 goto out;
1908 }
1909 }
1910
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001911 if (!type->set_flag)
1912 type->set_flag = &dummy_set_flag;
Chunyu Hud39cdd22016-03-08 21:37:01 +08001913 if (!type->flags) {
1914 /*allocate a dummy tracer_flags*/
1915 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
Chunyu Huc8ca0032016-03-14 20:35:41 +08001916 if (!type->flags) {
1917 ret = -ENOMEM;
1918 goto out;
1919 }
Chunyu Hud39cdd22016-03-08 21:37:01 +08001920 type->flags->val = 0;
1921 type->flags->opts = dummy_tracer_opt;
1922 } else
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001923 if (!type->flags->opts)
1924 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001925
Chunyu Hud39cdd22016-03-08 21:37:01 +08001926 /* store the tracer for __set_tracer_option */
1927 type->flags->trace = type;
1928
Steven Rostedt (Red Hat)f4e781c2013-03-07 11:10:56 -05001929 ret = run_tracer_selftest(type);
1930 if (ret < 0)
1931 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +02001932
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001933 type->next = trace_types;
1934 trace_types = type;
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04001935 add_tracer_options(&global_trace, type);
Steven Rostedt60a11772008-05-12 21:20:44 +02001936
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001937 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01001938 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001939 mutex_unlock(&trace_types_lock);
1940
Steven Rostedtdac74942009-02-05 01:13:38 -05001941 if (ret || !default_bootup_tracer)
1942 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -05001943
Li Zefanee6c2c12009-09-18 14:06:47 +08001944 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -05001945 goto out_unlock;
1946
1947 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1948 /* Do we want this tracer to start on bootup? */
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05001949 tracing_set_tracer(&global_trace, type->name);
Steven Rostedtdac74942009-02-05 01:13:38 -05001950 default_bootup_tracer = NULL;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08001951
1952 apply_trace_boot_options();
1953
Steven Rostedtdac74942009-02-05 01:13:38 -05001954 /* disable other selftests, since this will break it. */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05001955 tracing_selftest_disabled = true;
Steven Rostedtdac74942009-02-05 01:13:38 -05001956#ifdef CONFIG_FTRACE_STARTUP_TEST
1957 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1958 type->name);
1959#endif
1960
1961 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001962 return ret;
1963}
1964
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001965static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
Steven Rostedtf6339032009-09-04 12:35:16 -04001966{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001967 struct trace_buffer *buffer = buf->buffer;
Steven Rostedtf6339032009-09-04 12:35:16 -04001968
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001969 if (!buffer)
1970 return;
1971
Steven Rostedtf6339032009-09-04 12:35:16 -04001972 ring_buffer_record_disable(buffer);
1973
1974 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08001975 synchronize_rcu();
Steven Rostedt68179682012-05-08 20:57:53 -04001976 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -04001977
1978 ring_buffer_record_enable(buffer);
1979}
1980
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05001981void tracing_reset_online_cpus(struct array_buffer *buf)
Pekka J Enberg213cc062008-12-19 12:08:39 +02001982{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05001983 struct trace_buffer *buffer = buf->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +02001984 int cpu;
1985
Hiraku Toyookaa5416412012-12-19 16:02:34 +09001986 if (!buffer)
1987 return;
1988
Steven Rostedt621968c2009-09-04 12:02:35 -04001989 ring_buffer_record_disable(buffer);
1990
1991 /* Make sure all commits have finished */
Paul E. McKenney74401722018-11-06 18:44:52 -08001992 synchronize_rcu();
Steven Rostedt621968c2009-09-04 12:02:35 -04001993
Alexander Z Lam94571582013-08-02 18:36:16 -07001994 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
Pekka J Enberg213cc062008-12-19 12:08:39 +02001995
1996 for_each_online_cpu(cpu)
Steven Rostedt68179682012-05-08 20:57:53 -04001997 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -04001998
1999 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +02002000}
2001
Steven Rostedt (Red Hat)09d80912013-07-23 22:21:59 -04002002/* Must have trace_types_lock held */
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002003void tracing_reset_all_online_cpus(void)
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002004{
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002005 struct trace_array *tr;
2006
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002007 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04002008 if (!tr->clear_trace)
2009 continue;
2010 tr->clear_trace = false;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002011 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002012#ifdef CONFIG_TRACER_MAX_TRACE
2013 tracing_reset_online_cpus(&tr->max_buffer);
2014#endif
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002015 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002016}
2017
Joel Fernandesd914ba32017-06-26 19:01:55 -07002018static int *tgid_map;
2019
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002020#define SAVED_CMDLINES_DEFAULT 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01002021#define NO_CMDLINE_MAP UINT_MAX
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01002022static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002023struct saved_cmdlines_buffer {
2024 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
2025 unsigned *map_cmdline_to_pid;
2026 unsigned cmdline_num;
2027 int cmdline_idx;
2028 char *saved_cmdlines;
2029};
2030static struct saved_cmdlines_buffer *savedcmd;
Steven Rostedt25b0b442008-05-12 21:21:00 +02002031
Steven Rostedt25b0b442008-05-12 21:21:00 +02002032/* temporary disable recording */
Joel Fernandesd914ba32017-06-26 19:01:55 -07002033static atomic_t trace_record_taskinfo_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002034
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002035static inline char *get_saved_cmdlines(int idx)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002036{
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002037 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
2038}
2039
2040static inline void set_cmdline(int idx, const char *cmdline)
2041{
Tom Zanussi85f726a2019-03-05 10:12:00 -06002042 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002043}
2044
2045static int allocate_cmdlines_buffer(unsigned int val,
2046 struct saved_cmdlines_buffer *s)
2047{
Kees Cook6da2ec52018-06-12 13:55:00 -07002048 s->map_cmdline_to_pid = kmalloc_array(val,
2049 sizeof(*s->map_cmdline_to_pid),
2050 GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002051 if (!s->map_cmdline_to_pid)
2052 return -ENOMEM;
2053
Kees Cook6da2ec52018-06-12 13:55:00 -07002054 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002055 if (!s->saved_cmdlines) {
2056 kfree(s->map_cmdline_to_pid);
2057 return -ENOMEM;
2058 }
2059
2060 s->cmdline_idx = 0;
2061 s->cmdline_num = val;
2062 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
2063 sizeof(s->map_pid_to_cmdline));
2064 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
2065 val * sizeof(*s->map_cmdline_to_pid));
2066
2067 return 0;
2068}
2069
2070static int trace_create_savedcmd(void)
2071{
2072 int ret;
2073
Namhyung Kima6af8fb2014-06-10 16:11:35 +09002074 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002075 if (!savedcmd)
2076 return -ENOMEM;
2077
2078 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
2079 if (ret < 0) {
2080 kfree(savedcmd);
2081 savedcmd = NULL;
2082 return -ENOMEM;
2083 }
2084
2085 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002086}
2087
Carsten Emdeb5130b12009-09-13 01:43:07 +02002088int is_tracing_stopped(void)
2089{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002090 return global_trace.stop_count;
Carsten Emdeb5130b12009-09-13 01:43:07 +02002091}
2092
Steven Rostedt0f048702008-11-05 16:05:44 -05002093/**
2094 * tracing_start - quick start of the tracer
2095 *
2096 * If tracing is enabled but was stopped by tracing_stop,
2097 * this will start the tracer back up.
2098 */
2099void tracing_start(void)
2100{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002101 struct trace_buffer *buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002102 unsigned long flags;
2103
2104 if (tracing_disabled)
2105 return;
2106
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002107 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2108 if (--global_trace.stop_count) {
2109 if (global_trace.stop_count < 0) {
Steven Rostedtb06a8302009-01-22 14:26:15 -05002110 /* Someone screwed up their debugging */
2111 WARN_ON_ONCE(1);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002112 global_trace.stop_count = 0;
Steven Rostedtb06a8302009-01-22 14:26:15 -05002113 }
Steven Rostedt0f048702008-11-05 16:05:44 -05002114 goto out;
2115 }
2116
Steven Rostedta2f80712010-03-12 19:56:00 -05002117 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002118 arch_spin_lock(&global_trace.max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05002119
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002120 buffer = global_trace.array_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002121 if (buffer)
2122 ring_buffer_record_enable(buffer);
2123
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002124#ifdef CONFIG_TRACER_MAX_TRACE
2125 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002126 if (buffer)
2127 ring_buffer_record_enable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002128#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002129
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002130 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002131
Steven Rostedt0f048702008-11-05 16:05:44 -05002132 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002133 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2134}
2135
2136static void tracing_start_tr(struct trace_array *tr)
2137{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002138 struct trace_buffer *buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002139 unsigned long flags;
2140
2141 if (tracing_disabled)
2142 return;
2143
2144 /* If global, we need to also start the max tracer */
2145 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2146 return tracing_start();
2147
2148 raw_spin_lock_irqsave(&tr->start_lock, flags);
2149
2150 if (--tr->stop_count) {
2151 if (tr->stop_count < 0) {
2152 /* Someone screwed up their debugging */
2153 WARN_ON_ONCE(1);
2154 tr->stop_count = 0;
2155 }
2156 goto out;
2157 }
2158
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002159 buffer = tr->array_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002160 if (buffer)
2161 ring_buffer_record_enable(buffer);
2162
2163 out:
2164 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002165}
2166
2167/**
2168 * tracing_stop - quick stop of the tracer
2169 *
2170 * Light weight way to stop tracing. Use in conjunction with
2171 * tracing_start.
2172 */
2173void tracing_stop(void)
2174{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002175 struct trace_buffer *buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002176 unsigned long flags;
2177
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002178 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
2179 if (global_trace.stop_count++)
Steven Rostedt0f048702008-11-05 16:05:44 -05002180 goto out;
2181
Steven Rostedta2f80712010-03-12 19:56:00 -05002182 /* Prevent the buffers from switching */
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002183 arch_spin_lock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002184
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002185 buffer = global_trace.array_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002186 if (buffer)
2187 ring_buffer_record_disable(buffer);
2188
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002189#ifdef CONFIG_TRACER_MAX_TRACE
2190 buffer = global_trace.max_buffer.buffer;
Steven Rostedt0f048702008-11-05 16:05:44 -05002191 if (buffer)
2192 ring_buffer_record_disable(buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05002193#endif
Steven Rostedt0f048702008-11-05 16:05:44 -05002194
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05002195 arch_spin_unlock(&global_trace.max_lock);
Steven Rostedta2f80712010-03-12 19:56:00 -05002196
Steven Rostedt0f048702008-11-05 16:05:44 -05002197 out:
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002198 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
2199}
2200
2201static void tracing_stop_tr(struct trace_array *tr)
2202{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002203 struct trace_buffer *buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002204 unsigned long flags;
2205
2206 /* If global, we need to also stop the max tracer */
2207 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
2208 return tracing_stop();
2209
2210 raw_spin_lock_irqsave(&tr->start_lock, flags);
2211 if (tr->stop_count++)
2212 goto out;
2213
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002214 buffer = tr->array_buffer.buffer;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04002215 if (buffer)
2216 ring_buffer_record_disable(buffer);
2217
2218 out:
2219 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05002220}
2221
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002222static int trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002223{
Carsten Emdea635cf02009-03-18 09:00:41 +01002224 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002225
Joel Fernandeseaf260a2017-07-06 16:00:21 -07002226 /* treat recording of idle task as a success */
2227 if (!tsk->pid)
2228 return 1;
2229
2230 if (unlikely(tsk->pid > PID_MAX_DEFAULT))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002231 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002232
2233 /*
2234 * It's not the end of the world if we don't get
2235 * the lock, but we also don't want to spin
2236 * nor do we want to disable interrupts,
2237 * so if we miss here, then better luck next time.
2238 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002239 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002240 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002241
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002242 idx = savedcmd->map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01002243 if (idx == NO_CMDLINE_MAP) {
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002244 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245
Carsten Emdea635cf02009-03-18 09:00:41 +01002246 /*
2247 * Check whether the cmdline buffer at idx has a pid
2248 * mapped. We are going to overwrite that entry so we
2249 * need to clear the map_pid_to_cmdline. Otherwise we
2250 * would read the new comm for the old pid.
2251 */
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002252 pid = savedcmd->map_cmdline_to_pid[idx];
Carsten Emdea635cf02009-03-18 09:00:41 +01002253 if (pid != NO_CMDLINE_MAP)
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002254 savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002256 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
2257 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002258
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002259 savedcmd->cmdline_idx = idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002260 }
2261
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002262 set_cmdline(idx, tsk->comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002263
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002264 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedt (Red Hat)379cfda2014-05-30 09:42:39 -04002265
2266 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267}
2268
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002269static void __trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002271 unsigned map;
2272
Steven Rostedt4ca530852009-03-16 19:20:15 -04002273 if (!pid) {
2274 strcpy(comm, "<idle>");
2275 return;
2276 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002277
Steven Rostedt74bf4072010-01-25 15:11:53 -05002278 if (WARN_ON_ONCE(pid < 0)) {
2279 strcpy(comm, "<XXX>");
2280 return;
2281 }
2282
Steven Rostedt4ca530852009-03-16 19:20:15 -04002283 if (pid > PID_MAX_DEFAULT) {
2284 strcpy(comm, "<...>");
2285 return;
2286 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002287
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09002288 map = savedcmd->map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01002289 if (map != NO_CMDLINE_MAP)
Amey Telawanee09e2862017-05-03 15:41:14 +05302290 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
Thomas Gleixner50d88752009-03-18 08:58:44 +01002291 else
2292 strcpy(comm, "<...>");
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04002293}
2294
2295void trace_find_cmdline(int pid, char comm[])
2296{
2297 preempt_disable();
2298 arch_spin_lock(&trace_cmdline_lock);
2299
2300 __trace_find_cmdline(pid, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002301
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002302 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02002303 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002304}
2305
Joel Fernandesd914ba32017-06-26 19:01:55 -07002306int trace_find_tgid(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307{
Joel Fernandesd914ba32017-06-26 19:01:55 -07002308 if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2309 return 0;
2310
2311 return tgid_map[pid];
2312}
2313
2314static int trace_save_tgid(struct task_struct *tsk)
2315{
Joel Fernandesbd45d342017-07-06 16:00:22 -07002316 /* treat recording of idle task as a success */
2317 if (!tsk->pid)
2318 return 1;
2319
2320 if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
Joel Fernandesd914ba32017-06-26 19:01:55 -07002321 return 0;
2322
2323 tgid_map[tsk->pid] = tsk->tgid;
2324 return 1;
2325}
2326
2327static bool tracing_record_taskinfo_skip(int flags)
2328{
2329 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2330 return true;
2331 if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2332 return true;
2333 if (!__this_cpu_read(trace_taskinfo_save))
2334 return true;
2335 return false;
2336}
2337
2338/**
2339 * tracing_record_taskinfo - record the task info of a task
2340 *
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07002341 * @task: task to record
2342 * @flags: TRACE_RECORD_CMDLINE for recording comm
2343 * TRACE_RECORD_TGID for recording tgid
Joel Fernandesd914ba32017-06-26 19:01:55 -07002344 */
2345void tracing_record_taskinfo(struct task_struct *task, int flags)
2346{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002347 bool done;
2348
Joel Fernandesd914ba32017-06-26 19:01:55 -07002349 if (tracing_record_taskinfo_skip(flags))
2350 return;
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002351
2352 /*
2353 * Record as much task information as possible. If some fail, continue
2354 * to try to record the others.
2355 */
2356 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2357 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2358
2359 /* If recording any information failed, retry again soon. */
2360 if (!done)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002361 return;
2362
Joel Fernandesd914ba32017-06-26 19:01:55 -07002363 __this_cpu_write(trace_taskinfo_save, false);
2364}
2365
2366/**
2367 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2368 *
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07002369 * @prev: previous task during sched_switch
2370 * @next: next task during sched_switch
2371 * @flags: TRACE_RECORD_CMDLINE for recording comm
2372 * TRACE_RECORD_TGID for recording tgid
Joel Fernandesd914ba32017-06-26 19:01:55 -07002373 */
2374void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2375 struct task_struct *next, int flags)
2376{
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002377 bool done;
2378
Joel Fernandesd914ba32017-06-26 19:01:55 -07002379 if (tracing_record_taskinfo_skip(flags))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002380 return;
2381
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002382 /*
2383 * Record as much task information as possible. If some fail, continue
2384 * to try to record the others.
2385 */
2386 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2387 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2388 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2389 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
Joel Fernandesd914ba32017-06-26 19:01:55 -07002390
Joel Fernandes29b1a8a2017-07-06 16:00:23 -07002391 /* If recording any information failed, retry again soon. */
2392 if (!done)
Joel Fernandesd914ba32017-06-26 19:01:55 -07002393 return;
2394
2395 __this_cpu_write(trace_taskinfo_save, false);
2396}
2397
2398/* Helpers to record a specific task information */
2399void tracing_record_cmdline(struct task_struct *task)
2400{
2401 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2402}
2403
2404void tracing_record_tgid(struct task_struct *task)
2405{
2406 tracing_record_taskinfo(task, TRACE_RECORD_TGID);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002407}
2408
Steven Rostedt (VMware)af0009f2017-03-16 11:01:06 -04002409/*
2410 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2411 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2412 * simplifies those functions and keeps them in sync.
2413 */
2414enum print_line_t trace_handle_return(struct trace_seq *s)
2415{
2416 return trace_seq_has_overflowed(s) ?
2417 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2418}
2419EXPORT_SYMBOL_GPL(trace_handle_return);
2420
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03002421void
Cong Wang46710f32019-05-25 09:57:59 -07002422tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2423 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002424{
2425 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002426
Steven Rostedt777e2082008-09-29 23:02:42 -04002427 entry->preempt_count = pc & 0xff;
2428 entry->pid = (tsk) ? tsk->pid : 0;
Cong Wang46710f32019-05-25 09:57:59 -07002429 entry->type = type;
Steven Rostedt777e2082008-09-29 23:02:42 -04002430 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04002431#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04002432 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04002433#else
2434 TRACE_FLAG_IRQS_NOSUPPORT |
2435#endif
Peter Zijlstra7e6867b2016-03-18 16:28:04 +01002436 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
Pavankumar Kondetic59f29c2016-12-09 21:50:17 +05302438 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
Peter Zijlstrae5137b52013-10-04 17:28:26 +02002439 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2440 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002441}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002442EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002443
Steven Rostedte77405a2009-09-02 14:17:06 -04002444struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002445trace_buffer_lock_reserve(struct trace_buffer *buffer,
Steven Rostedte77405a2009-09-02 14:17:06 -04002446 int type,
2447 unsigned long len,
2448 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002449{
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002450 return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002451}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02002452
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002453DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2454DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2455static int trace_buffered_event_ref;
2456
2457/**
2458 * trace_buffered_event_enable - enable buffering events
2459 *
2460 * When events are being filtered, it is quicker to use a temporary
2461 * buffer to write the event data into if there's a likely chance
2462 * that it will not be committed. The discard of the ring buffer
2463 * is not as fast as committing, and is much slower than copying
2464 * a commit.
2465 *
2466 * When an event is to be filtered, allocate per cpu buffers to
2467 * write the event data into, and if the event is filtered and discarded
2468 * it is simply dropped, otherwise, the entire data is to be committed
2469 * in one shot.
2470 */
2471void trace_buffered_event_enable(void)
2472{
2473 struct ring_buffer_event *event;
2474 struct page *page;
2475 int cpu;
2476
2477 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2478
2479 if (trace_buffered_event_ref++)
2480 return;
2481
2482 for_each_tracing_cpu(cpu) {
2483 page = alloc_pages_node(cpu_to_node(cpu),
2484 GFP_KERNEL | __GFP_NORETRY, 0);
2485 if (!page)
2486 goto failed;
2487
2488 event = page_address(page);
2489 memset(event, 0, sizeof(*event));
2490
2491 per_cpu(trace_buffered_event, cpu) = event;
2492
2493 preempt_disable();
2494 if (cpu == smp_processor_id() &&
2495 this_cpu_read(trace_buffered_event) !=
2496 per_cpu(trace_buffered_event, cpu))
2497 WARN_ON_ONCE(1);
2498 preempt_enable();
2499 }
2500
2501 return;
2502 failed:
2503 trace_buffered_event_disable();
2504}
2505
2506static void enable_trace_buffered_event(void *data)
2507{
2508 /* Probably not needed, but do it anyway */
2509 smp_rmb();
2510 this_cpu_dec(trace_buffered_event_cnt);
2511}
2512
2513static void disable_trace_buffered_event(void *data)
2514{
2515 this_cpu_inc(trace_buffered_event_cnt);
2516}
2517
2518/**
2519 * trace_buffered_event_disable - disable buffering events
2520 *
2521 * When a filter is removed, it is faster to not use the buffered
2522 * events, and to commit directly into the ring buffer. Free up
2523 * the temp buffers when there are no more users. This requires
2524 * special synchronization with current events.
2525 */
2526void trace_buffered_event_disable(void)
2527{
2528 int cpu;
2529
2530 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2531
2532 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2533 return;
2534
2535 if (--trace_buffered_event_ref)
2536 return;
2537
2538 preempt_disable();
2539 /* For each CPU, set the buffer as used. */
2540 smp_call_function_many(tracing_buffer_mask,
2541 disable_trace_buffered_event, NULL, 1);
2542 preempt_enable();
2543
2544 /* Wait for all current users to finish */
Paul E. McKenney74401722018-11-06 18:44:52 -08002545 synchronize_rcu();
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002546
2547 for_each_tracing_cpu(cpu) {
2548 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2549 per_cpu(trace_buffered_event, cpu) = NULL;
2550 }
2551 /*
2552 * Make sure trace_buffered_event is NULL before clearing
2553 * trace_buffered_event_cnt.
2554 */
2555 smp_wmb();
2556
2557 preempt_disable();
2558 /* Do the work on each cpu */
2559 smp_call_function_many(tracing_buffer_mask,
2560 enable_trace_buffered_event, NULL, 1);
2561 preempt_enable();
2562}
2563
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002564static struct trace_buffer *temp_buffer;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002565
Steven Rostedtef5580d2009-02-27 19:38:04 -05002566struct ring_buffer_event *
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002567trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002568 struct trace_event_file *trace_file,
Steven Rostedtccb469a2012-08-02 10:32:10 -04002569 int type, unsigned long len,
2570 unsigned long flags, int pc)
2571{
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002572 struct ring_buffer_event *entry;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002573 int val;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002574
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05002575 *current_rb = trace_file->tr->array_buffer.buffer;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002576
Tom Zanussi00b41452018-01-15 20:51:39 -06002577 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -04002578 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2579 (entry = this_cpu_read(trace_buffered_event))) {
2580 /* Try to use the per cpu buffer first */
2581 val = this_cpu_inc_return(trace_buffered_event_cnt);
2582 if (val == 1) {
2583 trace_event_setup(entry, type, flags, pc);
2584 entry->array[0] = len;
2585 return entry;
2586 }
2587 this_cpu_dec(trace_buffered_event_cnt);
2588 }
2589
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002590 entry = __trace_buffer_lock_reserve(*current_rb,
2591 type, len, flags, pc);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002592 /*
2593 * If tracing is off, but we have triggers enabled
2594 * we still need to look at the event data. Use the temp_buffer
2595 * to store the trace event for the tigger to use. It's recusive
2596 * safe and will not be recorded anywhere.
2597 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002598 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04002599 *current_rb = temp_buffer;
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 return entry;
Steven Rostedtccb469a2012-08-02 10:32:10 -04002604}
2605EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2606
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002607static DEFINE_SPINLOCK(tracepoint_iter_lock);
2608static DEFINE_MUTEX(tracepoint_printk_mutex);
2609
2610static void output_printk(struct trace_event_buffer *fbuffer)
2611{
2612 struct trace_event_call *event_call;
Masami Hiramatsud8d0c242020-01-11 01:05:18 +09002613 struct trace_event_file *file;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002614 struct trace_event *event;
2615 unsigned long flags;
2616 struct trace_iterator *iter = tracepoint_print_iter;
2617
2618 /* We should never get here if iter is NULL */
2619 if (WARN_ON_ONCE(!iter))
2620 return;
2621
2622 event_call = fbuffer->trace_file->event_call;
2623 if (!event_call || !event_call->event.funcs ||
2624 !event_call->event.funcs->trace)
2625 return;
2626
Masami Hiramatsud8d0c242020-01-11 01:05:18 +09002627 file = fbuffer->trace_file;
2628 if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2629 (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2630 !filter_match_preds(file->filter, fbuffer->entry)))
2631 return;
2632
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002633 event = &fbuffer->trace_file->event_call->event;
2634
2635 spin_lock_irqsave(&tracepoint_iter_lock, flags);
2636 trace_seq_init(&iter->seq);
2637 iter->ent = fbuffer->entry;
2638 event_call->event.funcs->trace(iter, 0, event);
2639 trace_seq_putc(&iter->seq, 0);
2640 printk("%s", iter->seq.buffer);
2641
2642 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2643}
2644
2645int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2646 void __user *buffer, size_t *lenp,
2647 loff_t *ppos)
2648{
2649 int save_tracepoint_printk;
2650 int ret;
2651
2652 mutex_lock(&tracepoint_printk_mutex);
2653 save_tracepoint_printk = tracepoint_printk;
2654
2655 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2656
2657 /*
2658 * This will force exiting early, as tracepoint_printk
2659 * is always zero when tracepoint_printk_iter is not allocated
2660 */
2661 if (!tracepoint_print_iter)
2662 tracepoint_printk = 0;
2663
2664 if (save_tracepoint_printk == tracepoint_printk)
2665 goto out;
2666
2667 if (tracepoint_printk)
2668 static_key_enable(&tracepoint_printk_key.key);
2669 else
2670 static_key_disable(&tracepoint_printk_key.key);
2671
2672 out:
2673 mutex_unlock(&tracepoint_printk_mutex);
2674
2675 return ret;
2676}
2677
2678void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2679{
2680 if (static_key_false(&tracepoint_printk_key.key))
2681 output_printk(fbuffer);
2682
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09002683 event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002684 fbuffer->event, fbuffer->entry,
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09002685 fbuffer->flags, fbuffer->pc, fbuffer->regs);
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05002686}
2687EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2688
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002689/*
2690 * Skip 3:
2691 *
2692 * trace_buffer_unlock_commit_regs()
2693 * trace_event_buffer_commit()
2694 * trace_event_raw_event_xxx()
Rohit Visavalia13cf9122018-01-29 15:11:26 +05302695 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002696# define STACK_SKIP 3
2697
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04002698void trace_buffer_unlock_commit_regs(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002699 struct trace_buffer *buffer,
Steven Rostedt0d5c6e12012-11-01 20:54:21 -04002700 struct ring_buffer_event *event,
2701 unsigned long flags, int pc,
2702 struct pt_regs *regs)
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002703{
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002704 __buffer_unlock_commit(buffer, event);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002705
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002706 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002707 * If regs is not set, then skip the necessary functions.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002708 * Note, we can still get here via blktrace, wakeup tracer
2709 * and mmiotrace, but that's ok if they lose a function or
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002710 * two. They are not that meaningful.
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002711 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002712 ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002713 ftrace_trace_userstack(buffer, flags, pc);
2714}
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002715
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002716/*
2717 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2718 */
2719void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002720trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -05002721 struct ring_buffer_event *event)
2722{
2723 __buffer_unlock_commit(buffer, event);
2724}
2725
Chunyan Zhang478409d2016-11-21 15:57:18 +08002726static void
2727trace_process_export(struct trace_export *export,
2728 struct ring_buffer_event *event)
2729{
2730 struct trace_entry *entry;
2731 unsigned int size = 0;
2732
2733 entry = ring_buffer_event_data(event);
2734 size = ring_buffer_event_length(event);
Felipe Balbia773d412017-06-02 13:20:25 +03002735 export->write(export, entry, size);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002736}
2737
2738static DEFINE_MUTEX(ftrace_export_lock);
2739
2740static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2741
2742static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2743
2744static inline void ftrace_exports_enable(void)
2745{
2746 static_branch_enable(&ftrace_exports_enabled);
2747}
2748
2749static inline void ftrace_exports_disable(void)
2750{
2751 static_branch_disable(&ftrace_exports_enabled);
2752}
2753
Mathieu Malaterre1cce3772018-05-16 21:30:12 +02002754static void ftrace_exports(struct ring_buffer_event *event)
Chunyan Zhang478409d2016-11-21 15:57:18 +08002755{
2756 struct trace_export *export;
2757
2758 preempt_disable_notrace();
2759
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -04002760 export = rcu_dereference_raw_check(ftrace_exports_list);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002761 while (export) {
2762 trace_process_export(export, event);
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -04002763 export = rcu_dereference_raw_check(export->next);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002764 }
2765
2766 preempt_enable_notrace();
2767}
2768
2769static inline void
2770add_trace_export(struct trace_export **list, struct trace_export *export)
2771{
2772 rcu_assign_pointer(export->next, *list);
2773 /*
2774 * We are entering export into the list but another
2775 * CPU might be walking that list. We need to make sure
2776 * the export->next pointer is valid before another CPU sees
2777 * the export pointer included into the list.
2778 */
2779 rcu_assign_pointer(*list, export);
2780}
2781
2782static inline int
2783rm_trace_export(struct trace_export **list, struct trace_export *export)
2784{
2785 struct trace_export **p;
2786
2787 for (p = list; *p != NULL; p = &(*p)->next)
2788 if (*p == export)
2789 break;
2790
2791 if (*p != export)
2792 return -1;
2793
2794 rcu_assign_pointer(*p, (*p)->next);
2795
2796 return 0;
2797}
2798
2799static inline void
2800add_ftrace_export(struct trace_export **list, struct trace_export *export)
2801{
2802 if (*list == NULL)
2803 ftrace_exports_enable();
2804
2805 add_trace_export(list, export);
2806}
2807
2808static inline int
2809rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2810{
2811 int ret;
2812
2813 ret = rm_trace_export(list, export);
2814 if (*list == NULL)
2815 ftrace_exports_disable();
2816
2817 return ret;
2818}
2819
2820int register_ftrace_export(struct trace_export *export)
2821{
2822 if (WARN_ON_ONCE(!export->write))
2823 return -1;
2824
2825 mutex_lock(&ftrace_export_lock);
2826
2827 add_ftrace_export(&ftrace_exports_list, export);
2828
2829 mutex_unlock(&ftrace_export_lock);
2830
2831 return 0;
2832}
2833EXPORT_SYMBOL_GPL(register_ftrace_export);
2834
2835int unregister_ftrace_export(struct trace_export *export)
2836{
2837 int ret;
2838
2839 mutex_lock(&ftrace_export_lock);
2840
2841 ret = rm_ftrace_export(&ftrace_exports_list, export);
2842
2843 mutex_unlock(&ftrace_export_lock);
2844
2845 return ret;
2846}
2847EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2848
Ingo Molnare309b412008-05-12 21:20:51 +02002849void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05002850trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04002851 unsigned long ip, unsigned long parent_ip, unsigned long flags,
2852 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002853{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002854 struct trace_event_call *call = &event_function;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002855 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002856 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04002857 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002858
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002859 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2860 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002861 if (!event)
2862 return;
2863 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04002864 entry->ip = ip;
2865 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05002866
Chunyan Zhang478409d2016-11-21 15:57:18 +08002867 if (!call_filter_check_discard(call, entry, buffer, event)) {
2868 if (static_branch_unlikely(&ftrace_exports_enabled))
2869 ftrace_exports(event);
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002870 __buffer_unlock_commit(buffer, event);
Chunyan Zhang478409d2016-11-21 15:57:18 +08002871 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002872}
2873
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002874#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002875
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002876/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2877#define FTRACE_KSTACK_NESTING 4
2878
2879#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2880
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002881struct ftrace_stack {
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002882 unsigned long calls[FTRACE_KSTACK_ENTRIES];
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002883};
2884
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002885
2886struct ftrace_stacks {
2887 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2888};
2889
2890static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002891static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2892
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002893static void __ftrace_trace_stack(struct trace_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05002894 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09002895 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02002896{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002897 struct trace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002898 struct ring_buffer_event *event;
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002899 unsigned int size, nr_entries;
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002900 struct ftrace_stack *fstack;
Steven Rostedt777e2082008-09-29 23:02:42 -04002901 struct stack_entry *entry;
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002902 int stackidx;
Ingo Molnar86387f72008-05-12 21:20:51 +02002903
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002904 /*
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002905 * Add one, for this function and the call to save_stack_trace()
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002906 * If regs is set, then these functions will not be in the way.
2907 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002908#ifndef CONFIG_UNWINDER_ORC
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002909 if (!regs)
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002910 skip++;
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05002911#endif
Steven Rostedt (Red Hat)be54f692016-06-23 14:03:47 -04002912
2913 /*
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002914 * Since events can happen in NMIs there's no safe way to
2915 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2916 * or NMI comes in, it will just have to use the default
2917 * FTRACE_STACK_SIZE.
2918 */
2919 preempt_disable_notrace();
2920
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002921 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2922
2923 /* This should never happen. If it does, yell once and skip */
2924 if (WARN_ON_ONCE(stackidx > FTRACE_KSTACK_NESTING))
2925 goto out;
2926
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002927 /*
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002928 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2929 * interrupt will either see the value pre increment or post
2930 * increment. If the interrupt happens pre increment it will have
2931 * restored the counter when it returns. We just need a barrier to
2932 * keep gcc from moving things around.
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002933 */
2934 barrier();
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002935
Thomas Gleixner2a820bf2019-04-25 11:45:14 +02002936 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002937 size = ARRAY_SIZE(fstack->calls);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002938
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002939 if (regs) {
2940 nr_entries = stack_trace_save_regs(regs, fstack->calls,
2941 size, skip);
2942 } else {
2943 nr_entries = stack_trace_save(fstack->calls, size, skip);
2944 }
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002945
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002946 size = nr_entries * sizeof(unsigned long);
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05002947 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2948 sizeof(*entry) + size, flags, pc);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002949 if (!event)
2950 goto out;
2951 entry = ring_buffer_event_data(event);
2952
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02002953 memcpy(&entry->caller, fstack->calls, size);
2954 entry->size = nr_entries;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002955
Tom Zanussif306cc82013-10-24 08:34:17 -05002956 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04002957 __buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002958
2959 out:
2960 /* Again, don't let gcc optimize things here */
2961 barrier();
Shan Wei82146522012-11-19 13:21:01 +08002962 __this_cpu_dec(ftrace_stack_reserve);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04002963 preempt_enable_notrace();
2964
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002965}
2966
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002967static inline void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002968 struct trace_buffer *buffer,
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002969 unsigned long flags,
2970 int skip, int pc, struct pt_regs *regs)
Steven Rostedt53614992009-01-15 19:12:40 -05002971{
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04002972 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
Steven Rostedt53614992009-01-15 19:12:40 -05002973 return;
2974
Steven Rostedt (Red Hat)73dddbb2015-09-29 15:38:55 -04002975 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
Steven Rostedt53614992009-01-15 19:12:40 -05002976}
2977
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02002978void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2979 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04002980{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05002981 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002982
2983 if (rcu_is_watching()) {
2984 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2985 return;
2986 }
2987
2988 /*
2989 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2990 * but if the above rcu_is_watching() failed, then the NMI
2991 * triggered someplace critical, and rcu_irq_enter() should
2992 * not be called from NMI.
2993 */
2994 if (unlikely(in_nmi()))
2995 return;
2996
Steven Rostedt (VMware)a33d7d92017-05-12 13:15:45 -04002997 rcu_irq_enter_irqson();
2998 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2999 rcu_irq_exit_irqson();
Steven Rostedt38697052008-10-01 13:14:09 -04003000}
3001
Steven Rostedt03889382009-12-11 09:48:22 -05003002/**
3003 * trace_dump_stack - record a stack back trace in the trace buffer
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003004 * @skip: Number of functions to skip (helper handlers)
Steven Rostedt03889382009-12-11 09:48:22 -05003005 */
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003006void trace_dump_stack(int skip)
Steven Rostedt03889382009-12-11 09:48:22 -05003007{
3008 unsigned long flags;
3009
3010 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05003011 return;
Steven Rostedt03889382009-12-11 09:48:22 -05003012
3013 local_save_flags(flags);
3014
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05003015#ifndef CONFIG_UNWINDER_ORC
3016 /* Skip 1 to skip this function. */
3017 skip++;
3018#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003019 __ftrace_trace_stack(global_trace.array_buffer.buffer,
Steven Rostedt (Red Hat)c142be82013-03-13 09:55:57 -04003020 flags, skip, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05003021}
Nikolay Borisovda387e52018-10-17 09:51:43 +03003022EXPORT_SYMBOL_GPL(trace_dump_stack);
Steven Rostedt03889382009-12-11 09:48:22 -05003023
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003024#ifdef CONFIG_USER_STACKTRACE_SUPPORT
Steven Rostedt91e86e52010-11-10 12:56:12 +01003025static DEFINE_PER_CPU(int, user_stack_count);
3026
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003027static void
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003028ftrace_trace_userstack(struct trace_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02003029{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003030 struct trace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02003031 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02003032 struct userstack_entry *entry;
Török Edwin02b67512008-11-22 13:28:47 +02003033
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003034 if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
Török Edwin02b67512008-11-22 13:28:47 +02003035 return;
3036
Steven Rostedtb6345872010-03-12 20:03:30 -05003037 /*
3038 * NMIs can not handle page faults, even with fix ups.
3039 * The save user stack can (and often does) fault.
3040 */
3041 if (unlikely(in_nmi()))
3042 return;
3043
Steven Rostedt91e86e52010-11-10 12:56:12 +01003044 /*
3045 * prevent recursion, since the user stack tracing may
3046 * trigger other kernel events.
3047 */
3048 preempt_disable();
3049 if (__this_cpu_read(user_stack_count))
3050 goto out;
3051
3052 __this_cpu_inc(user_stack_count);
3053
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003054 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
3055 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02003056 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08003057 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02003058 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02003059
Steven Rostedt48659d32009-09-11 11:36:23 -04003060 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02003061 memset(&entry->caller, 0, sizeof(entry->caller));
3062
Thomas Gleixneree6dd0d2019-04-25 11:45:16 +02003063 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
Tom Zanussif306cc82013-10-24 08:34:17 -05003064 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003065 __buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01003066
Li Zefan1dbd1952010-12-09 15:47:56 +08003067 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01003068 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01003069 out:
3070 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02003071}
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003072#else /* CONFIG_USER_STACKTRACE_SUPPORT */
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003073static void ftrace_trace_userstack(struct trace_buffer *buffer,
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003074 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02003075{
Török Edwin02b67512008-11-22 13:28:47 +02003076}
Thomas Gleixnerc438f142019-04-25 11:45:15 +02003077#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
Török Edwin02b67512008-11-22 13:28:47 +02003078
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02003079#endif /* CONFIG_STACKTRACE */
3080
Steven Rostedt07d777f2011-09-22 14:01:55 -04003081/* created for use with alloc_percpu */
3082struct trace_buffer_struct {
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003083 int nesting;
3084 char buffer[4][TRACE_BUF_SIZE];
Steven Rostedt07d777f2011-09-22 14:01:55 -04003085};
3086
3087static struct trace_buffer_struct *trace_percpu_buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003088
3089/*
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003090 * Thise allows for lockless recording. If we're nested too deeply, then
3091 * this returns NULL.
Steven Rostedt07d777f2011-09-22 14:01:55 -04003092 */
3093static char *get_trace_buf(void)
3094{
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003095 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
Steven Rostedt07d777f2011-09-22 14:01:55 -04003096
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003097 if (!buffer || buffer->nesting >= 4)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003098 return NULL;
3099
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04003100 buffer->nesting++;
3101
3102 /* Interrupts must see nesting incremented before we use the buffer */
3103 barrier();
3104 return &buffer->buffer[buffer->nesting][0];
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003105}
3106
3107static void put_trace_buf(void)
3108{
Steven Rostedt (VMware)3d9622c2017-09-05 11:32:01 -04003109 /* Don't let the decrement of nesting leak before this */
3110 barrier();
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003111 this_cpu_dec(trace_percpu_buffer->nesting);
Steven Rostedt07d777f2011-09-22 14:01:55 -04003112}
3113
3114static int alloc_percpu_trace_buffer(void)
3115{
3116 struct trace_buffer_struct *buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003117
3118 buffers = alloc_percpu(struct trace_buffer_struct);
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003119 if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3120 return -ENOMEM;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003121
3122 trace_percpu_buffer = buffers;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003123 return 0;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003124}
3125
Steven Rostedt81698832012-10-11 10:15:05 -04003126static int buffers_allocated;
3127
Steven Rostedt07d777f2011-09-22 14:01:55 -04003128void trace_printk_init_buffers(void)
3129{
Steven Rostedt07d777f2011-09-22 14:01:55 -04003130 if (buffers_allocated)
3131 return;
3132
3133 if (alloc_percpu_trace_buffer())
3134 return;
3135
Steven Rostedt2184db42014-05-28 13:14:40 -04003136 /* trace_printk() is for debug use only. Don't use it in production. */
3137
Joe Perchesa395d6a2016-03-22 14:28:09 -07003138 pr_warn("\n");
3139 pr_warn("**********************************************************\n");
3140 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3141 pr_warn("** **\n");
3142 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3143 pr_warn("** **\n");
3144 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3145 pr_warn("** unsafe for production use. **\n");
3146 pr_warn("** **\n");
3147 pr_warn("** If you see this message and you are not debugging **\n");
3148 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3149 pr_warn("** **\n");
3150 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3151 pr_warn("**********************************************************\n");
Steven Rostedt07d777f2011-09-22 14:01:55 -04003152
Steven Rostedtb382ede62012-10-10 21:44:34 -04003153 /* Expand the buffers to set size */
3154 tracing_update_buffers();
3155
Steven Rostedt07d777f2011-09-22 14:01:55 -04003156 buffers_allocated = 1;
Steven Rostedt81698832012-10-11 10:15:05 -04003157
3158 /*
3159 * trace_printk_init_buffers() can be called by modules.
3160 * If that happens, then we need to start cmdline recording
3161 * directly here. If the global_trace.buffer is already
3162 * allocated here, then this was called by module code.
3163 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003164 if (global_trace.array_buffer.buffer)
Steven Rostedt81698832012-10-11 10:15:05 -04003165 tracing_start_cmdline_record();
3166}
Divya Indif45d1222019-03-20 11:28:51 -07003167EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
Steven Rostedt81698832012-10-11 10:15:05 -04003168
3169void trace_printk_start_comm(void)
3170{
3171 /* Start tracing comms if trace printk is set */
3172 if (!buffers_allocated)
3173 return;
3174 tracing_start_cmdline_record();
3175}
3176
3177static void trace_printk_start_stop_comm(int enabled)
3178{
3179 if (!buffers_allocated)
3180 return;
3181
3182 if (enabled)
3183 tracing_start_cmdline_record();
3184 else
3185 tracing_stop_cmdline_record();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003186}
3187
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003188/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003189 * trace_vbprintk - write binary msg to tracing buffer
Jakub Kicinskic68c9ec2019-08-27 22:25:47 -07003190 * @ip: The address of the caller
3191 * @fmt: The string format to write to the buffer
3192 * @args: Arguments for @fmt
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003193 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003194int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003195{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003196 struct trace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003197 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003198 struct trace_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003199 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003200 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003201 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003202 char *tbuffer;
3203 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003204
3205 if (unlikely(tracing_selftest_running || tracing_disabled))
3206 return 0;
3207
3208 /* Don't pollute graph traces with trace_vprintk internals */
3209 pause_graph_tracing();
3210
3211 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003212 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003213
Steven Rostedt07d777f2011-09-22 14:01:55 -04003214 tbuffer = get_trace_buf();
3215 if (!tbuffer) {
3216 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003217 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003218 }
3219
3220 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3221
3222 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003223 goto out;
3224
Steven Rostedt07d777f2011-09-22 14:01:55 -04003225 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003226 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003227 buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003228 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3229 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003230 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003231 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003232 entry = ring_buffer_event_data(event);
3233 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003234 entry->fmt = fmt;
3235
Steven Rostedt07d777f2011-09-22 14:01:55 -04003236 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Tom Zanussif306cc82013-10-24 08:34:17 -05003237 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003238 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003239 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003240 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003241
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003242out:
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003243 put_trace_buf();
3244
3245out_nobuffer:
Steven Rostedt5168ae52010-06-03 09:36:50 -04003246 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003247 unpause_graph_tracing();
3248
3249 return len;
3250}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003251EXPORT_SYMBOL_GPL(trace_vbprintk);
3252
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003253__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003254static int
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003255__trace_array_vprintk(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003256 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003257{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003258 struct trace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003259 struct ring_buffer_event *event;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003260 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003261 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003262 unsigned long flags;
3263 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003264
3265 if (tracing_disabled || tracing_selftest_running)
3266 return 0;
3267
Steven Rostedt07d777f2011-09-22 14:01:55 -04003268 /* Don't pollute graph traces with trace_vprintk internals */
3269 pause_graph_tracing();
3270
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003271 pc = preempt_count();
3272 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003273
Steven Rostedt07d777f2011-09-22 14:01:55 -04003274
3275 tbuffer = get_trace_buf();
3276 if (!tbuffer) {
3277 len = 0;
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003278 goto out_nobuffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04003279 }
3280
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003281 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003282
Steven Rostedt07d777f2011-09-22 14:01:55 -04003283 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003284 size = sizeof(*entry) + len + 1;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05003285 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3286 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003287 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04003288 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003289 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01003290 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003291
Dan Carpenter3558a5a2014-11-27 18:57:52 +03003292 memcpy(&entry->buf, tbuffer, len + 1);
Tom Zanussif306cc82013-10-24 08:34:17 -05003293 if (!call_filter_check_discard(call, entry, buffer, event)) {
Steven Rostedt7ffbd482012-10-11 12:14:25 -04003294 __buffer_unlock_commit(buffer, event);
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003295 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
Steven Rostedtd9313692010-01-06 17:27:11 -05003296 }
Andy Lutomirskie2ace002016-05-26 12:00:33 -07003297
3298out:
3299 put_trace_buf();
3300
3301out_nobuffer:
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003302 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04003303 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003304
3305 return len;
3306}
Steven Rostedt659372d2009-09-03 19:11:07 -04003307
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003308__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003309int trace_array_vprintk(struct trace_array *tr,
3310 unsigned long ip, const char *fmt, va_list args)
3311{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003312 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003313}
3314
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003315__printf(3, 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003316int trace_array_printk(struct trace_array *tr,
3317 unsigned long ip, const char *fmt, ...)
3318{
3319 int ret;
3320 va_list ap;
3321
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003322 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003323 return 0;
3324
Divya Indi953ae452019-08-14 10:55:25 -07003325 if (!tr)
3326 return -ENOENT;
3327
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003328 va_start(ap, fmt);
3329 ret = trace_array_vprintk(tr, ip, fmt, ap);
3330 va_end(ap);
3331 return ret;
3332}
Divya Indif45d1222019-03-20 11:28:51 -07003333EXPORT_SYMBOL_GPL(trace_array_printk);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003334
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003335__printf(3, 4)
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003336int trace_array_printk_buf(struct trace_buffer *buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003337 unsigned long ip, const char *fmt, ...)
3338{
3339 int ret;
3340 va_list ap;
3341
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003342 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003343 return 0;
3344
3345 va_start(ap, fmt);
3346 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3347 va_end(ap);
3348 return ret;
3349}
3350
Mathieu Malaterre26b68dd2018-03-08 21:58:43 +01003351__printf(2, 0)
Steven Rostedt659372d2009-09-03 19:11:07 -04003352int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3353{
Steven Rostedta813a152009-10-09 01:41:35 -04003354 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04003355}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01003356EXPORT_SYMBOL_GPL(trace_vprintk);
3357
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003358static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04003359{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003360 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3361
Steven Rostedt5a90f572008-09-03 17:42:51 -04003362 iter->idx++;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003363 if (buf_iter)
3364 ring_buffer_read(buf_iter, NULL);
Steven Rostedt5a90f572008-09-03 17:42:51 -04003365}
3366
Ingo Molnare309b412008-05-12 21:20:51 +02003367static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003368peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3369 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003370{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003371 struct ring_buffer_event *event;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003372 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003373
Steven Rostedtd7690412008-10-01 00:29:53 -04003374 if (buf_iter)
3375 event = ring_buffer_iter_peek(buf_iter, ts);
3376 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003377 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003378 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04003379
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04003380 if (event) {
3381 iter->ent_size = ring_buffer_event_length(event);
3382 return ring_buffer_event_data(event);
3383 }
3384 iter->ent_size = 0;
3385 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003386}
Steven Rostedtd7690412008-10-01 00:29:53 -04003387
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003388static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04003389__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3390 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003391{
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05003392 struct trace_buffer *buffer = iter->array_buffer->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003393 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08003394 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003395 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003396 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003397 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003398 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003399 int cpu;
3400
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003401 /*
3402 * If we are in a per_cpu trace file, don't bother by iterating over
3403 * all cpu and peek directly.
3404 */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003405 if (cpu_file > RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003406 if (ring_buffer_empty_cpu(buffer, cpu_file))
3407 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003408 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003409 if (ent_cpu)
3410 *ent_cpu = cpu_file;
3411
3412 return ent;
3413 }
3414
Steven Rostedtab464282008-05-12 21:21:00 +02003415 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003416
3417 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003418 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003419
Steven Rostedtbc21b472010-03-31 19:49:26 -04003420 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003421
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02003422 /*
3423 * Pick the entry with the smallest timestamp:
3424 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003425 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003426 next = ent;
3427 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003428 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04003429 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04003430 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003431 }
3432 }
3433
Steven Rostedt12b5da32012-03-27 10:43:28 -04003434 iter->ent_size = next_size;
3435
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003436 if (ent_cpu)
3437 *ent_cpu = next_cpu;
3438
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003439 if (ent_ts)
3440 *ent_ts = next_ts;
3441
Steven Rostedtbc21b472010-03-31 19:49:26 -04003442 if (missing_events)
3443 *missing_events = next_lost;
3444
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003445 return next;
3446}
3447
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003448/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003449struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3450 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003451{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003452 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003453}
Ingo Molnar8c523a92008-05-12 21:20:46 +02003454
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003455/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05003456void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003457{
Steven Rostedtbc21b472010-03-31 19:49:26 -04003458 iter->ent = __find_next_entry(iter, &iter->cpu,
3459 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003460
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003461 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01003462 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003463
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003464 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003465}
3466
Ingo Molnare309b412008-05-12 21:20:51 +02003467static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02003468{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003469 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
Steven Rostedtbc21b472010-03-31 19:49:26 -04003470 &iter->lost_events);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003471}
3472
Ingo Molnare309b412008-05-12 21:20:51 +02003473static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003474{
3475 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003476 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003477 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003478
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003479 WARN_ON_ONCE(iter->leftover);
3480
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003481 (*pos)++;
3482
3483 /* can't go backwards */
3484 if (iter->idx > i)
3485 return NULL;
3486
3487 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05003488 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003489 else
3490 ent = iter;
3491
3492 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05003493 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003494
3495 iter->pos = *pos;
3496
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003497 return ent;
3498}
3499
Jason Wessel955b61e2010-08-05 09:22:23 -05003500void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003501{
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003502 struct ring_buffer_event *event;
3503 struct ring_buffer_iter *buf_iter;
3504 unsigned long entries = 0;
3505 u64 ts;
3506
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003507 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003508
Steven Rostedt6d158a82012-06-27 20:46:14 -04003509 buf_iter = trace_buffer_iter(iter, cpu);
3510 if (!buf_iter)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003511 return;
3512
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003513 ring_buffer_iter_reset(buf_iter);
3514
3515 /*
3516 * We could have the case with the max latency tracers
3517 * that a reset never took place on a cpu. This is evident
3518 * by the timestamp being before the start of the buffer.
3519 */
3520 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003521 if (ts >= iter->array_buffer->time_start)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003522 break;
3523 entries++;
3524 ring_buffer_read(buf_iter, NULL);
3525 }
3526
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003527 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003528}
3529
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003530/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003531 * The current tracer is copied to avoid a global locking
3532 * all around.
3533 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003534static void *s_start(struct seq_file *m, loff_t *pos)
3535{
3536 struct trace_iterator *iter = m->private;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003537 struct trace_array *tr = iter->tr;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003538 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003539 void *p = NULL;
3540 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003541 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003542
Hiraku Toyooka2fd196e2012-12-26 11:52:52 +09003543 /*
3544 * copy the tracer to avoid using a global lock all around.
3545 * iter->trace is a copy of current_trace, the pointer to the
3546 * name may be used instead of a strcmp(), as iter->trace->name
3547 * will point to the same string as current_trace->name.
3548 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003549 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003550 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3551 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003552 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003553
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003554#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003555 if (iter->snapshot && iter->trace->use_max_tr)
3556 return ERR_PTR(-EBUSY);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003557#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003558
3559 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003560 atomic_inc(&trace_record_taskinfo_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003561
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003562 if (*pos != iter->pos) {
3563 iter->ent = NULL;
3564 iter->cpu = 0;
3565 iter->idx = -1;
3566
Steven Rostedtae3b5092013-01-23 15:22:59 -05003567 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003568 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003569 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003570 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003571 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003572
Lai Jiangshanac91d852010-03-02 17:54:50 +08003573 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003574 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3575 ;
3576
3577 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05003578 /*
3579 * If we overflowed the seq_file before, then we want
3580 * to just reuse the trace_seq buffer again.
3581 */
3582 if (iter->leftover)
3583 p = iter;
3584 else {
3585 l = *pos - 1;
3586 p = s_next(m, p, &l);
3587 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003588 }
3589
Lai Jiangshan4f535962009-05-18 19:35:34 +08003590 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003591 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003592 return p;
3593}
3594
3595static void s_stop(struct seq_file *m, void *p)
3596{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003597 struct trace_iterator *iter = m->private;
3598
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003599#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003600 if (iter->snapshot && iter->trace->use_max_tr)
3601 return;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003602#endif
Hiraku Toyookadebdd572012-12-26 11:53:00 +09003603
3604 if (!iter->snapshot)
Joel Fernandesd914ba32017-06-26 19:01:55 -07003605 atomic_dec(&trace_record_taskinfo_disabled);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003606
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003607 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003608 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003609}
3610
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003611static void
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003612get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003613 unsigned long *entries, int cpu)
3614{
3615 unsigned long count;
3616
3617 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3618 /*
3619 * If this buffer has skipped entries, then we hold all
3620 * entries for the trace and we need to ignore the
3621 * ones before the time stamp.
3622 */
3623 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3624 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3625 /* total is the same as the entries */
3626 *total = count;
3627 } else
3628 *total = count +
3629 ring_buffer_overrun_cpu(buf->buffer, cpu);
3630 *entries = count;
3631}
3632
3633static void
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003634get_total_entries(struct array_buffer *buf,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003635 unsigned long *total, unsigned long *entries)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003636{
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003637 unsigned long t, e;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003638 int cpu;
3639
3640 *total = 0;
3641 *entries = 0;
3642
3643 for_each_tracing_cpu(cpu) {
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003644 get_total_entries_cpu(buf, &t, &e, cpu);
3645 *total += t;
3646 *entries += e;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003647 }
3648}
3649
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003650unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3651{
3652 unsigned long total, entries;
3653
3654 if (!tr)
3655 tr = &global_trace;
3656
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003657 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003658
3659 return entries;
3660}
3661
3662unsigned long trace_total_entries(struct trace_array *tr)
3663{
3664 unsigned long total, entries;
3665
3666 if (!tr)
3667 tr = &global_trace;
3668
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003669 get_total_entries(&tr->array_buffer, &total, &entries);
Douglas Andersonecffc8a2019-03-19 10:12:05 -07003670
3671 return entries;
3672}
3673
Ingo Molnare309b412008-05-12 21:20:51 +02003674static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003675{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01003676 seq_puts(m, "# _------=> CPU# \n"
3677 "# / _-----=> irqs-off \n"
3678 "# | / _----=> need-resched \n"
3679 "# || / _---=> hardirq/softirq \n"
3680 "# ||| / _--=> preempt-depth \n"
3681 "# |||| / delay \n"
3682 "# cmd pid ||||| time | caller \n"
3683 "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003684}
3685
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003686static void print_event_info(struct array_buffer *buf, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003687{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003688 unsigned long total;
3689 unsigned long entries;
3690
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003691 get_total_entries(buf, &total, &entries);
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003692 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
3693 entries, total, num_online_cpus());
3694 seq_puts(m, "#\n");
3695}
3696
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003697static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
Joel Fernandes441dae82017-06-25 22:38:43 -07003698 unsigned int flags)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003699{
Joel Fernandes441dae82017-06-25 22:38:43 -07003700 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3701
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003702 print_event_info(buf, m);
Joel Fernandes441dae82017-06-25 22:38:43 -07003703
Joel Fernandes (Google)f8494fa2018-06-25 17:08:22 -07003704 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3705 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003706}
3707
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003708static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
Joel Fernandes441dae82017-06-25 22:38:43 -07003709 unsigned int flags)
Steven Rostedt77271ce2011-11-17 09:34:33 -05003710{
Joel Fernandes441dae82017-06-25 22:38:43 -07003711 bool tgid = flags & TRACE_ITER_RECORD_TGID;
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003712 const char *space = " ";
3713 int prec = tgid ? 10 : 2;
Joel Fernandes441dae82017-06-25 22:38:43 -07003714
Quentin Perret9e738212019-02-14 15:29:50 +00003715 print_event_info(buf, m);
3716
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003717 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3718 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3719 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3720 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3721 seq_printf(m, "# %.*s||| / delay\n", prec, space);
3722 seq_printf(m, "# TASK-PID %.*sCPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3723 seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
Steven Rostedt77271ce2011-11-17 09:34:33 -05003724}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003725
Jiri Olsa62b915f2010-04-02 19:01:22 +02003726void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003727print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3728{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003729 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003730 struct array_buffer *buf = iter->array_buffer;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003731 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04003732 struct tracer *type = iter->trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05003733 unsigned long entries;
3734 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003735 const char *name = "preemption";
3736
Steven Rostedt (Red Hat)d840f712013-02-01 18:38:47 -05003737 name = type->name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003738
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003739 get_total_entries(buf, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003740
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003741 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003742 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003743 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003744 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003745 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003746 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02003747 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003748 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003749 total,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05003750 buf->cpu,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003751#if defined(CONFIG_PREEMPT_NONE)
3752 "server",
3753#elif defined(CONFIG_PREEMPT_VOLUNTARY)
3754 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04003755#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003756 "preempt",
Sebastian Andrzej Siewior9c34fc42019-10-15 21:18:20 +02003757#elif defined(CONFIG_PREEMPT_RT)
3758 "preempt_rt",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003759#else
3760 "unknown",
3761#endif
3762 /* These are reserved for later use */
3763 0, 0, 0, 0);
3764#ifdef CONFIG_SMP
3765 seq_printf(m, " #P:%d)\n", num_online_cpus());
3766#else
3767 seq_puts(m, ")\n");
3768#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003769 seq_puts(m, "# -----------------\n");
3770 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003771 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
Eric W. Biedermand20b92a2012-03-13 16:02:19 -07003772 data->comm, data->pid,
3773 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003774 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003775 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003776
3777 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003778 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003779 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3780 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003781 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02003782 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3783 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04003784 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003785 }
3786
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09003787 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003788}
3789
Steven Rostedta3097202008-11-07 22:36:02 -05003790static void test_cpu_buff_start(struct trace_iterator *iter)
3791{
3792 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003793 struct trace_array *tr = iter->tr;
Steven Rostedta3097202008-11-07 22:36:02 -05003794
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003795 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05003796 return;
3797
3798 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3799 return;
3800
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003801 if (cpumask_available(iter->started) &&
3802 cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05003803 return;
3804
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003805 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04003806 return;
3807
Matthias Kaehlcke4dbbe2d2017-04-21 16:41:10 -07003808 if (cpumask_available(iter->started))
Sasha Levin919cd972015-09-04 12:45:56 -04003809 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02003810
3811 /* Don't print started cpu buffer for the first entry of the trace */
3812 if (iter->idx > 1)
3813 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3814 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05003815}
3816
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003817static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003818{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003819 struct trace_array *tr = iter->tr;
Steven Rostedt214023c2008-05-12 21:20:46 +02003820 struct trace_seq *s = &iter->seq;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003821 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003822 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003823 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003824
Ingo Molnar4e3c3332008-05-12 21:20:45 +02003825 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003826
Steven Rostedta3097202008-11-07 22:36:02 -05003827 test_cpu_buff_start(iter);
3828
Steven Rostedtf633cef2008-12-23 23:24:13 -05003829 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003830
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003831 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003832 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3833 trace_print_lat_context(iter);
3834 else
3835 trace_print_context(iter);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003836 }
3837
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003838 if (trace_seq_has_overflowed(s))
3839 return TRACE_TYPE_PARTIAL_LINE;
3840
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003841 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003842 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003843
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003844 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003845
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003846 return trace_handle_return(s);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003847}
3848
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003849static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003850{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003851 struct trace_array *tr = iter->tr;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003852 struct trace_seq *s = &iter->seq;
3853 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003854 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003855
3856 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003857
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003858 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003859 trace_seq_printf(s, "%d %d %llu ",
3860 entry->pid, iter->cpu, iter->ts);
3861
3862 if (trace_seq_has_overflowed(s))
3863 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003864
Steven Rostedtf633cef2008-12-23 23:24:13 -05003865 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003866 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04003867 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003868
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003869 trace_seq_printf(s, "%d ?\n", entry->type);
Steven Rostedt7104f302008-10-01 10:52:51 -04003870
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003871 return trace_handle_return(s);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003872}
3873
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003874static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003875{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003876 struct trace_array *tr = iter->tr;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003877 struct trace_seq *s = &iter->seq;
3878 unsigned char newline = '\n';
3879 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003880 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003881
3882 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003883
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003884 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003885 SEQ_PUT_HEX_FIELD(s, entry->pid);
3886 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3887 SEQ_PUT_HEX_FIELD(s, iter->ts);
3888 if (trace_seq_has_overflowed(s))
3889 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003890 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003891
Steven Rostedtf633cef2008-12-23 23:24:13 -05003892 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02003893 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04003894 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02003895 if (ret != TRACE_TYPE_HANDLED)
3896 return ret;
3897 }
Steven Rostedt7104f302008-10-01 10:52:51 -04003898
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003899 SEQ_PUT_FIELD(s, newline);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003900
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003901 return trace_handle_return(s);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003902}
3903
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003904static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003905{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003906 struct trace_array *tr = iter->tr;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003907 struct trace_seq *s = &iter->seq;
3908 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05003909 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003910
3911 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003912
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003913 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003914 SEQ_PUT_FIELD(s, entry->pid);
3915 SEQ_PUT_FIELD(s, iter->cpu);
3916 SEQ_PUT_FIELD(s, iter->ts);
3917 if (trace_seq_has_overflowed(s))
3918 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02003919 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003920
Steven Rostedtf633cef2008-12-23 23:24:13 -05003921 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04003922 return event ? event->funcs->binary(iter, 0, event) :
3923 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003924}
3925
Jiri Olsa62b915f2010-04-02 19:01:22 +02003926int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003927{
Steven Rostedt6d158a82012-06-27 20:46:14 -04003928 struct ring_buffer_iter *buf_iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003929 int cpu;
3930
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003931 /* If we are looking at one CPU buffer, only check that one */
Steven Rostedtae3b5092013-01-23 15:22:59 -05003932 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003933 cpu = iter->cpu_file;
Steven Rostedt6d158a82012-06-27 20:46:14 -04003934 buf_iter = trace_buffer_iter(iter, cpu);
3935 if (buf_iter) {
3936 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003937 return 0;
3938 } else {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003939 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Steven Rostedt9aba60f2009-03-11 19:52:30 -04003940 return 0;
3941 }
3942 return 1;
3943 }
3944
Steven Rostedtab464282008-05-12 21:21:00 +02003945 for_each_tracing_cpu(cpu) {
Steven Rostedt6d158a82012-06-27 20:46:14 -04003946 buf_iter = trace_buffer_iter(iter, cpu);
3947 if (buf_iter) {
3948 if (!ring_buffer_iter_empty(buf_iter))
Steven Rostedtd7690412008-10-01 00:29:53 -04003949 return 0;
3950 } else {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05003951 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
Steven Rostedtd7690412008-10-01 00:29:53 -04003952 return 0;
3953 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003954 }
Steven Rostedtd7690412008-10-01 00:29:53 -04003955
Frederic Weisbecker797d3712008-09-30 18:13:45 +02003956 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003957}
3958
Lai Jiangshan4f535962009-05-18 19:35:34 +08003959/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05003960enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003961{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04003962 struct trace_array *tr = iter->tr;
3963 unsigned long trace_flags = tr->trace_flags;
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003964 enum print_line_t ret;
3965
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -05003966 if (iter->lost_events) {
3967 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3968 iter->cpu, iter->lost_events);
3969 if (trace_seq_has_overflowed(&iter->seq))
3970 return TRACE_TYPE_PARTIAL_LINE;
3971 }
Steven Rostedtbc21b472010-03-31 19:49:26 -04003972
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003973 if (iter->trace && iter->trace->print_line) {
3974 ret = iter->trace->print_line(iter);
3975 if (ret != TRACE_TYPE_UNHANDLED)
3976 return ret;
3977 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02003978
Steven Rostedt (Red Hat)09ae7232013-03-08 21:02:34 -05003979 if (iter->ent->type == TRACE_BPUTS &&
3980 trace_flags & TRACE_ITER_PRINTK &&
3981 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3982 return trace_print_bputs_msg_only(iter);
3983
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003984 if (iter->ent->type == TRACE_BPRINT &&
3985 trace_flags & TRACE_ITER_PRINTK &&
3986 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003987 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01003988
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003989 if (iter->ent->type == TRACE_PRINT &&
3990 trace_flags & TRACE_ITER_PRINTK &&
3991 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04003992 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01003993
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02003994 if (trace_flags & TRACE_ITER_BIN)
3995 return print_bin_fmt(iter);
3996
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02003997 if (trace_flags & TRACE_ITER_HEX)
3998 return print_hex_fmt(iter);
3999
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004000 if (trace_flags & TRACE_ITER_RAW)
4001 return print_raw_fmt(iter);
4002
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004003 return print_trace_fmt(iter);
4004}
4005
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004006void trace_latency_header(struct seq_file *m)
4007{
4008 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004009 struct trace_array *tr = iter->tr;
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004010
4011 /* print nothing if the buffers are empty */
4012 if (trace_empty(iter))
4013 return;
4014
4015 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4016 print_trace_header(m, iter);
4017
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004018 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01004019 print_lat_help_header(m);
4020}
4021
Jiri Olsa62b915f2010-04-02 19:01:22 +02004022void trace_default_header(struct seq_file *m)
4023{
4024 struct trace_iterator *iter = m->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004025 struct trace_array *tr = iter->tr;
4026 unsigned long trace_flags = tr->trace_flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +02004027
Jiri Olsaf56e7f82011-06-03 16:58:49 +02004028 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4029 return;
4030
Jiri Olsa62b915f2010-04-02 19:01:22 +02004031 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4032 /* print nothing if the buffers are empty */
4033 if (trace_empty(iter))
4034 return;
4035 print_trace_header(m, iter);
4036 if (!(trace_flags & TRACE_ITER_VERBOSE))
4037 print_lat_help_header(m);
4038 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05004039 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4040 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004041 print_func_help_header_irq(iter->array_buffer,
Joel Fernandes441dae82017-06-25 22:38:43 -07004042 m, trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05004043 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004044 print_func_help_header(iter->array_buffer, m,
Joel Fernandes441dae82017-06-25 22:38:43 -07004045 trace_flags);
Steven Rostedt77271ce2011-11-17 09:34:33 -05004046 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02004047 }
4048}
4049
Steven Rostedte0a413f2011-09-29 21:26:16 -04004050static void test_ftrace_alive(struct seq_file *m)
4051{
4052 if (!ftrace_is_dead())
4053 return;
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004054 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4055 "# MAY BE MISSING FUNCTION EVENTS\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04004056}
4057
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004058#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004059static void show_snapshot_main_help(struct seq_file *m)
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004060{
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004061 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
4062 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4063 "# Takes a snapshot of the main buffer.\n"
4064 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
4065 "# (Doesn't have to be '2' works with any number that\n"
4066 "# is not a '0' or '1')\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004067}
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004068
4069static void show_snapshot_percpu_help(struct seq_file *m)
4070{
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004071 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004072#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004073 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4074 "# Takes a snapshot of the main buffer for this cpu.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004075#else
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004076 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4077 "# Must use main snapshot file to allocate.\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004078#endif
Rasmus Villemoesd79ac282014-11-08 21:42:11 +01004079 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\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)f1affca2013-03-05 14:35:11 -05004082}
4083
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004084static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4085{
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05004086 if (iter->tr->allocated_snapshot)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004087 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004088 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004089 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004090
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004091 seq_puts(m, "# Snapshot commands:\n");
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05004092 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4093 show_snapshot_main_help(m);
4094 else
4095 show_snapshot_percpu_help(m);
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004096}
4097#else
4098/* Should never be called */
4099static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4100#endif
4101
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004102static int s_show(struct seq_file *m, void *v)
4103{
4104 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004105 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004106
4107 if (iter->ent == NULL) {
4108 if (iter->tr) {
4109 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4110 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04004111 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004112 }
Steven Rostedt (Red Hat)d8741e22013-03-05 10:25:16 -05004113 if (iter->snapshot && trace_empty(iter))
4114 print_snapshot_help(m, iter);
4115 else if (iter->trace && iter->trace->print_header)
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004116 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02004117 else
4118 trace_default_header(m);
4119
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004120 } else if (iter->leftover) {
4121 /*
4122 * If we filled the seq_file buffer earlier, we
4123 * want to just show it now.
4124 */
4125 ret = trace_print_seq(m, &iter->seq);
4126
4127 /* ret should this time be zero, but you never know */
4128 iter->leftover = ret;
4129
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004130 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02004131 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05004132 ret = trace_print_seq(m, &iter->seq);
4133 /*
4134 * If we overflow the seq_file buffer, then it will
4135 * ask us for this data again at start up.
4136 * Use that instead.
4137 * ret is 0 if seq_file write succeeded.
4138 * -1 otherwise.
4139 */
4140 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004141 }
4142
4143 return 0;
4144}
4145
Oleg Nesterov649e9c702013-07-23 17:25:54 +02004146/*
4147 * Should be used after trace_array_get(), trace_types_lock
4148 * ensures that i_cdev was already initialized.
4149 */
4150static inline int tracing_get_cpu(struct inode *inode)
4151{
4152 if (inode->i_cdev) /* See trace_create_cpu_file() */
4153 return (long)inode->i_cdev - 1;
4154 return RING_BUFFER_ALL_CPUS;
4155}
4156
James Morris88e9d342009-09-22 16:43:43 -07004157static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004158 .start = s_start,
4159 .next = s_next,
4160 .stop = s_stop,
4161 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004162};
4163
Ingo Molnare309b412008-05-12 21:20:51 +02004164static struct trace_iterator *
Oleg Nesterov6484c712013-07-23 17:26:10 +02004165__tracing_open(struct inode *inode, struct file *file, bool snapshot)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004166{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004167 struct trace_array *tr = inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004168 struct trace_iterator *iter;
Jiri Olsa50e18b92012-04-25 10:23:39 +02004169 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004170
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004171 if (tracing_disabled)
4172 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02004173
Jiri Olsa50e18b92012-04-25 10:23:39 +02004174 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004175 if (!iter)
4176 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004177
Gil Fruchter72917232015-06-09 10:32:35 +03004178 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
Steven Rostedt6d158a82012-06-27 20:46:14 -04004179 GFP_KERNEL);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004180 if (!iter->buffer_iter)
4181 goto release;
4182
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004183 /*
4184 * We make a copy of the current tracer to avoid concurrent
4185 * changes on it while we are reading.
4186 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004187 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004188 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004189 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004190 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004191
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004192 *iter->trace = *tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004193
Li Zefan79f55992009-06-15 14:58:26 +08004194 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004195 goto fail;
4196
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004197 iter->tr = tr;
4198
4199#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004200 /* Currently only the top directory has a snapshot */
4201 if (tr->current_trace->print_max || snapshot)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004202 iter->array_buffer = &tr->max_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004203 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004204#endif
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004205 iter->array_buffer = &tr->array_buffer;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004206 iter->snapshot = snapshot;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004207 iter->pos = -1;
Oleg Nesterov6484c712013-07-23 17:26:10 +02004208 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004209 mutex_init(&iter->mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004210
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004211 /* Notify the tracer early; before we stop tracing. */
4212 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01004213 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01004214
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004215 /* Annotate start of buffers if we had overruns */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004216 if (ring_buffer_overruns(iter->array_buffer->buffer))
Steven Rostedt12ef7d42008-11-12 17:52:38 -05004217 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4218
David Sharp8be07092012-11-13 12:18:22 -08004219 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09004220 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08004221 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4222
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004223 /* stop the trace while dumping if we are not opening "snapshot" */
4224 if (!iter->snapshot)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004225 tracing_stop_tr(tr);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004226
Steven Rostedtae3b5092013-01-23 15:22:59 -05004227 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004228 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004229 iter->buffer_iter[cpu] =
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004230 ring_buffer_read_prepare(iter->array_buffer->buffer,
Douglas Anderson31b265b2019-03-08 11:32:04 -08004231 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004232 }
4233 ring_buffer_read_prepare_sync();
4234 for_each_tracing_cpu(cpu) {
4235 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004236 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004237 }
4238 } else {
4239 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004240 iter->buffer_iter[cpu] =
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004241 ring_buffer_read_prepare(iter->array_buffer->buffer,
Douglas Anderson31b265b2019-03-08 11:32:04 -08004242 cpu, GFP_KERNEL);
David Miller72c9ddf2010-04-20 15:47:11 -07004243 ring_buffer_read_prepare_sync();
4244 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04004245 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004246 }
4247
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004248 mutex_unlock(&trace_types_lock);
4249
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004250 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004251
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004252 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004253 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004254 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004255 kfree(iter->buffer_iter);
Dan Carpenter93574fc2012-07-11 09:35:08 +03004256release:
Jiri Olsa50e18b92012-04-25 10:23:39 +02004257 seq_release_private(inode, file);
4258 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004259}
4260
4261int tracing_open_generic(struct inode *inode, struct file *filp)
4262{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004263 int ret;
4264
4265 ret = tracing_check_open_get_tr(NULL);
4266 if (ret)
4267 return ret;
Steven Rostedt60a11772008-05-12 21:20:44 +02004268
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004269 filp->private_data = inode->i_private;
4270 return 0;
4271}
4272
Geyslan G. Bem2e864212013-10-18 21:15:54 -03004273bool tracing_is_disabled(void)
4274{
4275 return (tracing_disabled) ? true: false;
4276}
4277
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004278/*
4279 * Open and update trace_array ref count.
4280 * Must have the current trace_array passed to it.
4281 */
Steven Rostedt (VMware)aa07d712019-10-11 19:12:21 -04004282int tracing_open_generic_tr(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004283{
4284 struct trace_array *tr = inode->i_private;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004285 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004286
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004287 ret = tracing_check_open_get_tr(tr);
4288 if (ret)
4289 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004290
4291 filp->private_data = inode->i_private;
4292
4293 return 0;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004294}
4295
Hannes Eder4fd27352009-02-10 19:44:12 +01004296static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004297{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004298 struct trace_array *tr = inode->i_private;
matt mooney907f2782010-09-27 19:04:53 -07004299 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004300 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004301 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004302
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004303 if (!(file->f_mode & FMODE_READ)) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004304 trace_array_put(tr);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004305 return 0;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004306 }
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004307
Oleg Nesterov6484c712013-07-23 17:26:10 +02004308 /* Writes do not use seq_file */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004309 iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004310 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05004311
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004312 for_each_tracing_cpu(cpu) {
4313 if (iter->buffer_iter[cpu])
4314 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4315 }
4316
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004317 if (iter->trace && iter->trace->close)
4318 iter->trace->close(iter);
4319
Hiraku Toyookadebdd572012-12-26 11:53:00 +09004320 if (!iter->snapshot)
4321 /* reenable tracing if it was previously enabled */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004322 tracing_start_tr(tr);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004323
4324 __trace_array_put(tr);
4325
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004326 mutex_unlock(&trace_types_lock);
4327
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004328 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02004329 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01004330 kfree(iter->trace);
Steven Rostedt6d158a82012-06-27 20:46:14 -04004331 kfree(iter->buffer_iter);
Jiri Olsa50e18b92012-04-25 10:23:39 +02004332 seq_release_private(inode, file);
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004333
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004334 return 0;
4335}
4336
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004337static int tracing_release_generic_tr(struct inode *inode, struct file *file)
4338{
4339 struct trace_array *tr = inode->i_private;
4340
4341 trace_array_put(tr);
4342 return 0;
4343}
4344
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004345static int tracing_single_release_tr(struct inode *inode, struct file *file)
4346{
4347 struct trace_array *tr = inode->i_private;
4348
4349 trace_array_put(tr);
4350
4351 return single_release(inode, file);
4352}
4353
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004354static int tracing_open(struct inode *inode, struct file *file)
4355{
Oleg Nesterov6484c712013-07-23 17:26:10 +02004356 struct trace_array *tr = inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05004357 struct trace_iterator *iter;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004358 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004359
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004360 ret = tracing_check_open_get_tr(tr);
4361 if (ret)
4362 return ret;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004363
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004364 /* If this file was open for write, then erase contents */
Oleg Nesterov6484c712013-07-23 17:26:10 +02004365 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4366 int cpu = tracing_get_cpu(inode);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004367 struct array_buffer *trace_buf = &tr->array_buffer;
Bo Yan8dd33bc2017-09-18 10:03:35 -07004368
4369#ifdef CONFIG_TRACER_MAX_TRACE
4370 if (tr->current_trace->print_max)
4371 trace_buf = &tr->max_buffer;
4372#endif
Oleg Nesterov6484c712013-07-23 17:26:10 +02004373
4374 if (cpu == RING_BUFFER_ALL_CPUS)
Bo Yan8dd33bc2017-09-18 10:03:35 -07004375 tracing_reset_online_cpus(trace_buf);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004376 else
Steven Rostedt (VMware)a47b53e2019-08-13 12:14:35 -04004377 tracing_reset_cpu(trace_buf, cpu);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004378 }
4379
4380 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02004381 iter = __tracing_open(inode, file, false);
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004382 if (IS_ERR(iter))
4383 ret = PTR_ERR(iter);
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004384 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004385 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4386 }
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04004387
4388 if (ret < 0)
4389 trace_array_put(tr);
4390
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004391 return ret;
4392}
4393
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004394/*
4395 * Some tracers are not suitable for instance buffers.
4396 * A tracer is always available for the global array (toplevel)
4397 * or if it explicitly states that it is.
4398 */
4399static bool
4400trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4401{
4402 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4403}
4404
4405/* Find the next tracer that this trace array may use */
4406static struct tracer *
4407get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4408{
4409 while (t && !trace_ok_for_array(t, tr))
4410 t = t->next;
4411
4412 return t;
4413}
4414
Ingo Molnare309b412008-05-12 21:20:51 +02004415static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004416t_next(struct seq_file *m, void *v, loff_t *pos)
4417{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004418 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004419 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004420
4421 (*pos)++;
4422
4423 if (t)
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004424 t = get_tracer_for_array(tr, t->next);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004425
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004426 return t;
4427}
4428
4429static void *t_start(struct seq_file *m, loff_t *pos)
4430{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004431 struct trace_array *tr = m->private;
Li Zefanf129e962009-06-24 09:53:44 +08004432 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004433 loff_t l = 0;
4434
4435 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004436
4437 t = get_tracer_for_array(tr, trace_types);
4438 for (; t && l < *pos; t = t_next(m, t, &l))
4439 ;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004440
4441 return t;
4442}
4443
4444static void t_stop(struct seq_file *m, void *p)
4445{
4446 mutex_unlock(&trace_types_lock);
4447}
4448
4449static int t_show(struct seq_file *m, void *v)
4450{
4451 struct tracer *t = v;
4452
4453 if (!t)
4454 return 0;
4455
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004456 seq_puts(m, t->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004457 if (t->next)
4458 seq_putc(m, ' ');
4459 else
4460 seq_putc(m, '\n');
4461
4462 return 0;
4463}
4464
James Morris88e9d342009-09-22 16:43:43 -07004465static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004466 .start = t_start,
4467 .next = t_next,
4468 .stop = t_stop,
4469 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004470};
4471
4472static int show_traces_open(struct inode *inode, struct file *file)
4473{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004474 struct trace_array *tr = inode->i_private;
4475 struct seq_file *m;
4476 int ret;
4477
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004478 ret = tracing_check_open_get_tr(tr);
4479 if (ret)
4480 return ret;
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004481
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004482 ret = seq_open(file, &show_traces_seq_ops);
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004483 if (ret) {
4484 trace_array_put(tr);
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004485 return ret;
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004486 }
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05004487
4488 m = file->private_data;
4489 m->private = tr;
4490
4491 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004492}
4493
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004494static int show_traces_release(struct inode *inode, struct file *file)
4495{
4496 struct trace_array *tr = inode->i_private;
4497
4498 trace_array_put(tr);
4499 return seq_release(inode, file);
4500}
4501
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004502static ssize_t
4503tracing_write_stub(struct file *filp, const char __user *ubuf,
4504 size_t count, loff_t *ppos)
4505{
4506 return count;
4507}
4508
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004509loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
Slava Pestov364829b2010-11-24 15:13:16 -08004510{
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004511 int ret;
4512
Slava Pestov364829b2010-11-24 15:13:16 -08004513 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004514 ret = seq_lseek(file, offset, whence);
Slava Pestov364829b2010-11-24 15:13:16 -08004515 else
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004516 file->f_pos = ret = 0;
4517
4518 return ret;
Slava Pestov364829b2010-11-24 15:13:16 -08004519}
4520
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004521static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004522 .open = tracing_open,
4523 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04004524 .write = tracing_write_stub,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004525 .llseek = tracing_lseek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004526 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004527};
4528
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004529static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02004530 .open = show_traces_open,
4531 .read = seq_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004532 .llseek = seq_lseek,
Steven Rostedt (VMware)194c2c72019-10-11 18:19:17 -04004533 .release = show_traces_release,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004534};
4535
4536static ssize_t
4537tracing_cpumask_read(struct file *filp, char __user *ubuf,
4538 size_t count, loff_t *ppos)
4539{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004540 struct trace_array *tr = file_inode(filp)->i_private;
Changbin Du90e406f2017-11-30 11:39:43 +08004541 char *mask_str;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004542 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004543
Changbin Du90e406f2017-11-30 11:39:43 +08004544 len = snprintf(NULL, 0, "%*pb\n",
4545 cpumask_pr_args(tr->tracing_cpumask)) + 1;
4546 mask_str = kmalloc(len, GFP_KERNEL);
4547 if (!mask_str)
4548 return -ENOMEM;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004549
Changbin Du90e406f2017-11-30 11:39:43 +08004550 len = snprintf(mask_str, len, "%*pb\n",
Tejun Heo1a402432015-02-13 14:37:39 -08004551 cpumask_pr_args(tr->tracing_cpumask));
4552 if (len >= count) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004553 count = -EINVAL;
4554 goto out_err;
4555 }
Changbin Du90e406f2017-11-30 11:39:43 +08004556 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004557
4558out_err:
Changbin Du90e406f2017-11-30 11:39:43 +08004559 kfree(mask_str);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004560
4561 return count;
4562}
4563
4564static ssize_t
4565tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4566 size_t count, loff_t *ppos)
4567{
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004568 struct trace_array *tr = file_inode(filp)->i_private;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304569 cpumask_var_t tracing_cpumask_new;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004570 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304571
4572 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4573 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004574
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304575 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004576 if (err)
4577 goto err_unlock;
4578
Steven Rostedta5e25882008-12-02 15:34:05 -05004579 local_irq_disable();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004580 arch_spin_lock(&tr->max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02004581 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02004582 /*
4583 * Increase/decrease the disabled counter if we are
4584 * about to flip a bit in the cpumask:
4585 */
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004586 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304587 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004588 atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4589 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004590 }
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004591 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304592 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004593 atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4594 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004595 }
4596 }
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05004597 arch_spin_unlock(&tr->max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05004598 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02004599
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004600 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304601 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004602
Ingo Molnarc7078de2008-05-12 21:20:52 +02004603 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02004604
4605err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08004606 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02004607
4608 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02004609}
4610
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004611static const struct file_operations tracing_cpumask_fops = {
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004612 .open = tracing_open_generic_tr,
Ingo Molnarc7078de2008-05-12 21:20:52 +02004613 .read = tracing_cpumask_read,
4614 .write = tracing_cpumask_write,
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07004615 .release = tracing_release_generic_tr,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004616 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004617};
4618
Li Zefanfdb372e2009-12-08 11:15:59 +08004619static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004620{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004621 struct tracer_opt *trace_opts;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004622 struct trace_array *tr = m->private;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004623 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004624 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004625
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004626 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004627 tracer_flags = tr->current_trace->flags->val;
4628 trace_opts = tr->current_trace->flags->opts;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004629
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004630 for (i = 0; trace_options[i]; i++) {
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004631 if (tr->trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08004632 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004633 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004634 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004635 }
4636
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004637 for (i = 0; trace_opts[i].name; i++) {
4638 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08004639 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004640 else
Li Zefanfdb372e2009-12-08 11:15:59 +08004641 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004642 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05004643 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004644
Li Zefanfdb372e2009-12-08 11:15:59 +08004645 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004646}
4647
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004648static int __set_tracer_option(struct trace_array *tr,
Li Zefan8d18eaa2009-12-08 11:17:06 +08004649 struct tracer_flags *tracer_flags,
4650 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004651{
Chunyu Hud39cdd22016-03-08 21:37:01 +08004652 struct tracer *trace = tracer_flags->trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004653 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004654
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004655 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004656 if (ret)
4657 return ret;
4658
4659 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08004660 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004661 else
Zhaolei77708412009-08-07 18:53:21 +08004662 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004663 return 0;
4664}
4665
Li Zefan8d18eaa2009-12-08 11:17:06 +08004666/* Try to assign a tracer specific option */
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004667static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
Li Zefan8d18eaa2009-12-08 11:17:06 +08004668{
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004669 struct tracer *trace = tr->current_trace;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004670 struct tracer_flags *tracer_flags = trace->flags;
4671 struct tracer_opt *opts = NULL;
4672 int i;
4673
4674 for (i = 0; tracer_flags->opts[i].name; i++) {
4675 opts = &tracer_flags->opts[i];
4676
4677 if (strcmp(cmp, opts->name) == 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004678 return __set_tracer_option(tr, trace->flags, opts, neg);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004679 }
4680
4681 return -EINVAL;
4682}
4683
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004684/* Some tracers require overwrite to stay enabled */
4685int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
4686{
4687 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
4688 return -1;
4689
4690 return 0;
4691}
4692
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004693int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004694{
Prateek Sood3a53acf2019-12-10 09:15:16 +00004695 if ((mask == TRACE_ITER_RECORD_TGID) ||
4696 (mask == TRACE_ITER_RECORD_CMD))
4697 lockdep_assert_held(&event_mutex);
4698
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004699 /* do nothing if flag is already set */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004700 if (!!(tr->trace_flags & mask) == !!enabled)
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004701 return 0;
4702
4703 /* Give the tracer a chance to approve the change */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004704 if (tr->current_trace->flag_changed)
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -05004705 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004706 return -EINVAL;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004707
4708 if (enabled)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004709 tr->trace_flags |= mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004710 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04004711 tr->trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08004712
4713 if (mask == TRACE_ITER_RECORD_CMD)
4714 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08004715
Joel Fernandesd914ba32017-06-26 19:01:55 -07004716 if (mask == TRACE_ITER_RECORD_TGID) {
4717 if (!tgid_map)
Yuming Han6ee40512019-10-24 11:34:30 +08004718 tgid_map = kvcalloc(PID_MAX_DEFAULT + 1,
Kees Cook6396bb22018-06-12 14:03:40 -07004719 sizeof(*tgid_map),
Joel Fernandesd914ba32017-06-26 19:01:55 -07004720 GFP_KERNEL);
4721 if (!tgid_map) {
4722 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
4723 return -ENOMEM;
4724 }
4725
4726 trace_event_enable_tgid_record(enabled);
4727 }
4728
Steven Rostedtc37775d2016-04-13 16:59:18 -04004729 if (mask == TRACE_ITER_EVENT_FORK)
4730 trace_event_follow_fork(tr, enabled);
4731
Namhyung Kim1e104862017-04-17 11:44:28 +09004732 if (mask == TRACE_ITER_FUNC_FORK)
4733 ftrace_pid_follow_fork(tr, enabled);
4734
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004735 if (mask == TRACE_ITER_OVERWRITE) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05004736 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004737#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05004738 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
Steven Rostedt (Red Hat)80902822013-03-14 14:20:54 -04004739#endif
4740 }
Steven Rostedt81698832012-10-11 10:15:05 -04004741
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004742 if (mask == TRACE_ITER_PRINTK) {
Steven Rostedt81698832012-10-11 10:15:05 -04004743 trace_printk_start_stop_comm(enabled);
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -04004744 trace_printk_control(enabled);
4745 }
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004746
4747 return 0;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04004748}
4749
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004750static int trace_set_options(struct trace_array *tr, char *option)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004751{
Li Zefan8d18eaa2009-12-08 11:17:06 +08004752 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004753 int neg = 0;
Yisheng Xie591a0332018-05-17 16:36:03 +08004754 int ret;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004755 size_t orig_len = strlen(option);
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004756 int len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004757
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004758 cmp = strstrip(option);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004759
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004760 len = str_has_prefix(cmp, "no");
4761 if (len)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004762 neg = 1;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -05004763
4764 cmp += len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004765
Prateek Sood3a53acf2019-12-10 09:15:16 +00004766 mutex_lock(&event_mutex);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004767 mutex_lock(&trace_types_lock);
4768
Yisheng Xie591a0332018-05-17 16:36:03 +08004769 ret = match_string(trace_options, -1, cmp);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01004770 /* If no option could be set, test the specific tracer options */
Yisheng Xie591a0332018-05-17 16:36:03 +08004771 if (ret < 0)
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05004772 ret = set_tracer_option(tr, cmp, neg);
Yisheng Xie591a0332018-05-17 16:36:03 +08004773 else
4774 ret = set_tracer_flag(tr, 1 << ret, !neg);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04004775
4776 mutex_unlock(&trace_types_lock);
Prateek Sood3a53acf2019-12-10 09:15:16 +00004777 mutex_unlock(&event_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004778
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004779 /*
4780 * If the first trailing whitespace is replaced with '\0' by strstrip,
4781 * turn it back into a space.
4782 */
4783 if (orig_len > strlen(option))
4784 option[strlen(option)] = ' ';
4785
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004786 return ret;
4787}
4788
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004789static void __init apply_trace_boot_options(void)
4790{
4791 char *buf = trace_boot_options_buf;
4792 char *option;
4793
4794 while (true) {
4795 option = strsep(&buf, ",");
4796
4797 if (!option)
4798 break;
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004799
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05004800 if (*option)
4801 trace_set_options(&global_trace, option);
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08004802
4803 /* Put back the comma to allow this to be called again */
4804 if (buf)
4805 *(buf - 1) = ',';
4806 }
4807}
4808
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004809static ssize_t
4810tracing_trace_options_write(struct file *filp, const char __user *ubuf,
4811 size_t cnt, loff_t *ppos)
4812{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004813 struct seq_file *m = filp->private_data;
4814 struct trace_array *tr = m->private;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004815 char buf[64];
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004816 int ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004817
4818 if (cnt >= sizeof(buf))
4819 return -EINVAL;
4820
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08004821 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004822 return -EFAULT;
4823
Steven Rostedta8dd2172013-01-09 20:54:17 -05004824 buf[cnt] = 0;
4825
Steven Rostedt2b6080f2012-05-11 13:29:49 -04004826 ret = trace_set_options(tr, buf);
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04004827 if (ret < 0)
4828 return ret;
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04004829
Jiri Olsacf8517c2009-10-23 19:36:16 -04004830 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004831
4832 return cnt;
4833}
4834
Li Zefanfdb372e2009-12-08 11:15:59 +08004835static int tracing_trace_options_open(struct inode *inode, struct file *file)
4836{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004837 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004838 int ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004839
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04004840 ret = tracing_check_open_get_tr(tr);
4841 if (ret)
4842 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004843
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07004844 ret = single_open(file, tracing_trace_options_show, inode->i_private);
4845 if (ret < 0)
4846 trace_array_put(tr);
4847
4848 return ret;
Li Zefanfdb372e2009-12-08 11:15:59 +08004849}
4850
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004851static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08004852 .open = tracing_trace_options_open,
4853 .read = seq_read,
4854 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04004855 .release = tracing_single_release_tr,
Steven Rostedtee6bce52008-11-12 17:52:37 -05004856 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004857};
4858
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004859static const char readme_msg[] =
4860 "tracing mini-HOWTO:\n\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004861 "# echo 0 > tracing_on : quick way to disable tracing\n"
4862 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
4863 " Important files:\n"
4864 " trace\t\t\t- The static contents of the buffer\n"
4865 "\t\t\t To clear the buffer write into this file: echo > trace\n"
4866 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
4867 " current_tracer\t- function and latency tracers\n"
4868 " available_tracers\t- list of configured tracers for current_tracer\n"
Tom Zanussia8d65572019-03-31 18:48:25 -05004869 " error_log\t- error log for failed commands (that support it)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004870 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
4871 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
4872 " trace_clock\t\t-change the clock used to order events\n"
4873 " local: Per cpu clock but may not be synced across CPUs\n"
4874 " global: Synced across CPUs but slows tracing down.\n"
4875 " counter: Not a clock, but just an increment\n"
4876 " uptime: Jiffy counter from time of boot\n"
4877 " perf: Same clock that perf events use\n"
4878#ifdef CONFIG_X86_64
4879 " x86-tsc: TSC cycle counter\n"
4880#endif
Tom Zanussi2c1ea602018-01-15 20:51:41 -06004881 "\n timestamp_mode\t-view the mode used to timestamp events\n"
4882 " delta: Delta difference against a buffer-wide timestamp\n"
4883 " absolute: Absolute (standalone) timestamp\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004884 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
Steven Rostedtfa32e852016-07-06 15:25:08 -04004885 "\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 -04004886 " tracing_cpumask\t- Limit which CPUs to trace\n"
4887 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
4888 "\t\t\t Remove sub-buffer with rmdir\n"
4889 " trace_options\t\t- Set format or modify how tracing happens\n"
Srivatsa S. Bhat (VMware)b9416992019-01-28 17:55:53 -08004890 "\t\t\t Disable an option by prefixing 'no' to the\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004891 "\t\t\t option name\n"
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09004892 " 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 -04004893#ifdef CONFIG_DYNAMIC_FTRACE
4894 "\n available_filter_functions - list of functions that can be filtered on\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004895 " set_ftrace_filter\t- echo function name in here to only trace these\n"
4896 "\t\t\t functions\n"
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09004897 "\t accepts: func_full_name or glob-matching-pattern\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004898 "\t modules: Can select a group via module\n"
4899 "\t Format: :mod:<module-name>\n"
4900 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
4901 "\t triggers: a command to perform when function is hit\n"
4902 "\t Format: <function>:<trigger>[:count]\n"
4903 "\t trigger: traceon, traceoff\n"
4904 "\t\t enable_event:<system>:<event>\n"
4905 "\t\t disable_event:<system>:<event>\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004906#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004907 "\t\t stacktrace\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004908#endif
4909#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004910 "\t\t snapshot\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004911#endif
Steven Rostedt (Red Hat)17a280e2014-04-10 22:43:37 -04004912 "\t\t dump\n"
4913 "\t\t cpudump\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004914 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
4915 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
4916 "\t The first one will disable tracing every time do_fault is hit\n"
4917 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
4918 "\t The first time do trap is hit and it disables tracing, the\n"
4919 "\t counter will decrement to 2. If tracing is already disabled,\n"
4920 "\t the counter will not decrement. It only decrements when the\n"
4921 "\t trigger did work\n"
4922 "\t To remove trigger without count:\n"
4923 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
4924 "\t To remove trigger with a count:\n"
4925 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004926 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004927 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
4928 "\t modules: Can select a group via module command :mod:\n"
4929 "\t Does not accept triggers\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004930#endif /* CONFIG_DYNAMIC_FTRACE */
4931#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004932 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
4933 "\t\t (function)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004934#endif
4935#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4936 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
Namhyung Kimd048a8c72014-06-13 01:23:53 +09004937 " 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 -04004938 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
4939#endif
4940#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004941 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
4942 "\t\t\t snapshot buffer. Read the contents for more\n"
4943 "\t\t\t information\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004944#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004945#ifdef CONFIG_STACK_TRACER
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004946 " stack_trace\t\t- Shows the max stack trace when active\n"
4947 " stack_max_size\t- Shows current max stack size that was traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004948 "\t\t\t Write into this file to reset the max size (trigger a\n"
4949 "\t\t\t new trace)\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004950#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05004951 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
4952 "\t\t\t traces\n"
Steven Rostedt (Red Hat)22f45642013-03-15 17:23:20 -04004953#endif
zhangwei(Jovi)991821c2013-07-15 16:32:34 +08004954#endif /* CONFIG_STACK_TRACER */
Masami Hiramatsu5448d442018-11-05 18:02:08 +09004955#ifdef CONFIG_DYNAMIC_EVENTS
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09004956 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
Masami Hiramatsu5448d442018-11-05 18:02:08 +09004957 "\t\t\t Write into this file to define/undefine new trace events.\n"
4958#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004959#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09004960 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004961 "\t\t\t Write into this file to define/undefine new trace events.\n"
4962#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004963#ifdef CONFIG_UPROBE_EVENTS
Masami Hiramatsu41af3cf2019-06-20 00:07:58 +09004964 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004965 "\t\t\t Write into this file to define/undefine new trace events.\n"
4966#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004967#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Masami Hiramatsu86425622016-08-18 17:58:15 +09004968 "\t accepts: event-definitions (one definition per line)\n"
Masami Hiramatsuc3ca46e2017-05-23 15:05:50 +09004969 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
4970 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09004971#ifdef CONFIG_HIST_TRIGGERS
4972 "\t s:[synthetic/]<event> <field> [<field>]\n"
4973#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09004974 "\t -:[<group>/]<event>\n"
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004975#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu86425622016-08-18 17:58:15 +09004976 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Naveen N. Rao35b6f552017-02-22 19:23:39 +05304977 "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004978#endif
Anton Blanchard6b0b7552017-02-16 17:00:50 +11004979#ifdef CONFIG_UPROBE_EVENTS
Ravi Bangoria1cc33162018-08-20 10:12:47 +05304980 " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
Masami Hiramatsu86425622016-08-18 17:58:15 +09004981#endif
4982 "\t args: <name>=fetcharg[:type]\n"
4983 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004984#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09004985 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004986#else
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09004987 "\t $stack<index>, $stack, $retval, $comm,\n"
Masami Hiramatsua1303af2018-04-25 21:21:26 +09004988#endif
Masami Hiramatsua42e3c42019-06-20 00:08:37 +09004989 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +09004990 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
Masami Hiramatsu88903c42019-05-15 14:38:30 +09004991 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
Masami Hiramatsu40b53b72018-04-25 21:21:55 +09004992 "\t <type>\\[<array-size>\\]\n"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09004993#ifdef CONFIG_HIST_TRIGGERS
4994 "\t field: <stype> <name>;\n"
4995 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
4996 "\t [unsigned] char/int/long\n"
4997#endif
Masami Hiramatsu86425622016-08-18 17:58:15 +09004998#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06004999 " events/\t\t- Directory containing all trace event subsystems:\n"
5000 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
5001 " events/<system>/\t- Directory containing all trace events for <system>:\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005002 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5003 "\t\t\t events\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005004 " filter\t\t- If set, only events passing filter are traced\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005005 " events/<system>/<event>/\t- Directory containing control files for\n"
5006 "\t\t\t <event>:\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005007 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
5008 " filter\t\t- If set, only events passing filter are traced\n"
5009 " trigger\t\t- If set, a command to perform when event is hit\n"
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005010 "\t Format: <trigger>[:count][if <filter>]\n"
5011 "\t trigger: traceon, traceoff\n"
5012 "\t enable_event:<system>:<event>\n"
5013 "\t disable_event:<system>:<event>\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06005014#ifdef CONFIG_HIST_TRIGGERS
5015 "\t enable_hist:<system>:<event>\n"
5016 "\t disable_hist:<system>:<event>\n"
5017#endif
Tom Zanussi26f25562014-01-17 15:11:44 -06005018#ifdef CONFIG_STACKTRACE
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005019 "\t\t stacktrace\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005020#endif
5021#ifdef CONFIG_TRACER_SNAPSHOT
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005022 "\t\t snapshot\n"
Tom Zanussi26f25562014-01-17 15:11:44 -06005023#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005024#ifdef CONFIG_HIST_TRIGGERS
5025 "\t\t hist (see below)\n"
5026#endif
Steven Rostedt (Red Hat)71485c42014-01-23 00:10:04 -05005027 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
5028 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
5029 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
5030 "\t events/block/block_unplug/trigger\n"
5031 "\t The first disables tracing every time block_unplug is hit.\n"
5032 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
5033 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
5034 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
5035 "\t Like function triggers, the counter is only decremented if it\n"
5036 "\t enabled or disabled tracing.\n"
5037 "\t To remove a trigger without a count:\n"
5038 "\t echo '!<trigger> > <system>/<event>/trigger\n"
5039 "\t To remove a trigger with a count:\n"
5040 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
5041 "\t Filters can be ignored when removing a trigger.\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005042#ifdef CONFIG_HIST_TRIGGERS
5043 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005044 "\t Format: hist:keys=<field1[,field2,...]>\n"
Tom Zanussif2606832016-03-03 12:54:43 -06005045 "\t [:values=<field1[,field2,...]>]\n"
Tom Zanussie62347d2016-03-03 12:54:45 -06005046 "\t [:sort=<field1[,field2,...]>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005047 "\t [:size=#entries]\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005048 "\t [:pause][:continue][:clear]\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005049 "\t [:name=histname1]\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005050 "\t [:<handler>.<action>]\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005051 "\t [if <filter>]\n\n"
5052 "\t When a matching event is hit, an entry is added to a hash\n"
Tom Zanussif2606832016-03-03 12:54:43 -06005053 "\t table using the key(s) and value(s) named, and the value of a\n"
5054 "\t sum called 'hitcount' is incremented. Keys and values\n"
5055 "\t correspond to fields in the event's format description. Keys\n"
Tom Zanussi69a02002016-03-03 12:54:52 -06005056 "\t can be any field, or the special string 'stacktrace'.\n"
5057 "\t Compound keys consisting of up to two fields can be specified\n"
5058 "\t by the 'keys' keyword. Values must correspond to numeric\n"
5059 "\t fields. Sort keys consisting of up to two fields can be\n"
5060 "\t specified using the 'sort' keyword. The sort direction can\n"
5061 "\t be modified by appending '.descending' or '.ascending' to a\n"
5062 "\t sort field. The 'size' parameter can be used to specify more\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005063 "\t or fewer than the default 2048 entries for the hashtable size.\n"
5064 "\t If a hist trigger is given a name using the 'name' parameter,\n"
5065 "\t its histogram data will be shared with other triggers of the\n"
5066 "\t same name, and trigger hits will update this common data.\n\n"
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005067 "\t Reading the 'hist' file for the event will dump the hash\n"
Tom Zanussi52a7f162016-03-03 12:54:57 -06005068 "\t table in its entirety to stdout. If there are multiple hist\n"
5069 "\t triggers attached to an event, there will be a table for each\n"
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005070 "\t trigger in the output. The table displayed for a named\n"
5071 "\t trigger will be the same as any other instance having the\n"
5072 "\t same name. The default format used to display a given field\n"
5073 "\t can be modified by appending any of the following modifiers\n"
5074 "\t to the field name, as applicable:\n\n"
Tom Zanussic6afad42016-03-03 12:54:49 -06005075 "\t .hex display a number as a hex value\n"
5076 "\t .sym display an address as a symbol\n"
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005077 "\t .sym-offset display an address as a symbol and offset\n"
Tom Zanussi31696192016-03-03 12:54:51 -06005078 "\t .execname display a common_pid as a program name\n"
Tom Zanussi860f9f62018-01-15 20:51:48 -06005079 "\t .syscall display a syscall id as a syscall name\n"
5080 "\t .log2 display log2 value rather than raw number\n"
5081 "\t .usecs display a common_timestamp in microseconds\n\n"
Tom Zanussi83e99912016-03-03 12:54:46 -06005082 "\t The 'pause' parameter can be used to pause an existing hist\n"
5083 "\t trigger or to start a hist trigger but not log any events\n"
5084 "\t until told to do so. 'continue' can be used to start or\n"
5085 "\t restart a paused hist trigger.\n\n"
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005086 "\t The 'clear' parameter will clear the contents of a running\n"
5087 "\t hist trigger and leave its current paused/active state\n"
5088 "\t unchanged.\n\n"
Tom Zanussid0bad492016-03-03 12:54:55 -06005089 "\t The enable_hist and disable_hist triggers can be used to\n"
5090 "\t have one event conditionally start and stop another event's\n"
Colin Ian King9e5a36a2019-02-17 22:32:22 +00005091 "\t already-attached hist trigger. The syntax is analogous to\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005092 "\t the enable_event and disable_event triggers.\n\n"
5093 "\t Hist trigger handlers and actions are executed whenever a\n"
5094 "\t a histogram entry is added or updated. They take the form:\n\n"
5095 "\t <handler>.<action>\n\n"
5096 "\t The available handlers are:\n\n"
5097 "\t onmatch(matching.event) - invoke on addition or update\n"
Tom Zanussidff81f52019-02-13 17:42:48 -06005098 "\t onmax(var) - invoke if var exceeds current max\n"
5099 "\t onchange(var) - invoke action if var changes\n\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005100 "\t The available actions are:\n\n"
Tom Zanussie91eefd72019-02-13 17:42:50 -06005101 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
Tom Zanussic3e49502019-02-13 17:42:43 -06005102 "\t save(field,...) - save current event fields\n"
Tom Zanussia3785b72019-02-13 17:42:46 -06005103#ifdef CONFIG_TRACER_SNAPSHOT
5104 "\t snapshot() - snapshot the trace buffer\n"
5105#endif
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005106#endif
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005107;
5108
5109static ssize_t
5110tracing_readme_read(struct file *filp, char __user *ubuf,
5111 size_t cnt, loff_t *ppos)
5112{
5113 return simple_read_from_buffer(ubuf, cnt, ppos,
5114 readme_msg, strlen(readme_msg));
5115}
5116
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005117static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02005118 .open = tracing_open_generic,
5119 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02005120 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02005121};
5122
Michael Sartain99c621d2017-07-05 22:07:15 -06005123static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
5124{
5125 int *ptr = v;
5126
5127 if (*pos || m->count)
5128 ptr++;
5129
5130 (*pos)++;
5131
5132 for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) {
5133 if (trace_find_tgid(*ptr))
5134 return ptr;
5135 }
5136
5137 return NULL;
5138}
5139
5140static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
5141{
5142 void *v;
5143 loff_t l = 0;
5144
5145 if (!tgid_map)
5146 return NULL;
5147
5148 v = &tgid_map[0];
5149 while (l <= *pos) {
5150 v = saved_tgids_next(m, v, &l);
5151 if (!v)
5152 return NULL;
5153 }
5154
5155 return v;
5156}
5157
5158static void saved_tgids_stop(struct seq_file *m, void *v)
5159{
5160}
5161
5162static int saved_tgids_show(struct seq_file *m, void *v)
5163{
5164 int pid = (int *)v - tgid_map;
5165
5166 seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid));
5167 return 0;
5168}
5169
5170static const struct seq_operations tracing_saved_tgids_seq_ops = {
5171 .start = saved_tgids_start,
5172 .stop = saved_tgids_stop,
5173 .next = saved_tgids_next,
5174 .show = saved_tgids_show,
5175};
5176
5177static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
5178{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005179 int ret;
5180
5181 ret = tracing_check_open_get_tr(NULL);
5182 if (ret)
5183 return ret;
Michael Sartain99c621d2017-07-05 22:07:15 -06005184
5185 return seq_open(filp, &tracing_saved_tgids_seq_ops);
5186}
5187
5188
5189static const struct file_operations tracing_saved_tgids_fops = {
5190 .open = tracing_saved_tgids_open,
5191 .read = seq_read,
5192 .llseek = seq_lseek,
5193 .release = seq_release,
5194};
5195
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005196static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005197{
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005198 unsigned int *ptr = v;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005199
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005200 if (*pos || m->count)
5201 ptr++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005202
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005203 (*pos)++;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005204
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005205 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
5206 ptr++) {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005207 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
Avadh Patel69abe6a2009-04-10 16:04:48 -04005208 continue;
5209
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005210 return ptr;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005211 }
5212
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005213 return NULL;
5214}
Avadh Patel69abe6a2009-04-10 16:04:48 -04005215
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005216static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
5217{
5218 void *v;
5219 loff_t l = 0;
Avadh Patel69abe6a2009-04-10 16:04:48 -04005220
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005221 preempt_disable();
5222 arch_spin_lock(&trace_cmdline_lock);
5223
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005224 v = &savedcmd->map_cmdline_to_pid[0];
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005225 while (l <= *pos) {
5226 v = saved_cmdlines_next(m, v, &l);
5227 if (!v)
5228 return NULL;
5229 }
5230
5231 return v;
5232}
5233
5234static void saved_cmdlines_stop(struct seq_file *m, void *v)
5235{
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005236 arch_spin_unlock(&trace_cmdline_lock);
5237 preempt_enable();
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005238}
5239
5240static int saved_cmdlines_show(struct seq_file *m, void *v)
5241{
5242 char buf[TASK_COMM_LEN];
5243 unsigned int *pid = v;
5244
Steven Rostedt (Red Hat)4c27e752014-05-30 10:49:46 -04005245 __trace_find_cmdline(*pid, buf);
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005246 seq_printf(m, "%d %s\n", *pid, buf);
5247 return 0;
5248}
5249
5250static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
5251 .start = saved_cmdlines_start,
5252 .next = saved_cmdlines_next,
5253 .stop = saved_cmdlines_stop,
5254 .show = saved_cmdlines_show,
5255};
5256
5257static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
5258{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005259 int ret;
5260
5261 ret = tracing_check_open_get_tr(NULL);
5262 if (ret)
5263 return ret;
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005264
5265 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
Avadh Patel69abe6a2009-04-10 16:04:48 -04005266}
5267
5268static const struct file_operations tracing_saved_cmdlines_fops = {
Yoshihiro YUNOMAE42584c82014-02-20 17:44:31 +09005269 .open = tracing_saved_cmdlines_open,
5270 .read = seq_read,
5271 .llseek = seq_lseek,
5272 .release = seq_release,
Avadh Patel69abe6a2009-04-10 16:04:48 -04005273};
5274
5275static ssize_t
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005276tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
5277 size_t cnt, loff_t *ppos)
5278{
5279 char buf[64];
5280 int r;
5281
5282 arch_spin_lock(&trace_cmdline_lock);
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005283 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005284 arch_spin_unlock(&trace_cmdline_lock);
5285
5286 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5287}
5288
5289static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
5290{
5291 kfree(s->saved_cmdlines);
5292 kfree(s->map_cmdline_to_pid);
5293 kfree(s);
5294}
5295
5296static int tracing_resize_saved_cmdlines(unsigned int val)
5297{
5298 struct saved_cmdlines_buffer *s, *savedcmd_temp;
5299
Namhyung Kima6af8fb2014-06-10 16:11:35 +09005300 s = kmalloc(sizeof(*s), GFP_KERNEL);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005301 if (!s)
5302 return -ENOMEM;
5303
5304 if (allocate_cmdlines_buffer(val, s) < 0) {
5305 kfree(s);
5306 return -ENOMEM;
5307 }
5308
5309 arch_spin_lock(&trace_cmdline_lock);
5310 savedcmd_temp = savedcmd;
5311 savedcmd = s;
5312 arch_spin_unlock(&trace_cmdline_lock);
5313 free_saved_cmdlines_buffer(savedcmd_temp);
5314
5315 return 0;
5316}
5317
5318static ssize_t
5319tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
5320 size_t cnt, loff_t *ppos)
5321{
5322 unsigned long val;
5323 int ret;
5324
5325 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5326 if (ret)
5327 return ret;
5328
5329 /* must have at least 1 entry or less than PID_MAX_DEFAULT */
5330 if (!val || val > PID_MAX_DEFAULT)
5331 return -EINVAL;
5332
5333 ret = tracing_resize_saved_cmdlines((unsigned int)val);
5334 if (ret < 0)
5335 return ret;
5336
5337 *ppos += cnt;
5338
5339 return cnt;
5340}
5341
5342static const struct file_operations tracing_saved_cmdlines_size_fops = {
5343 .open = tracing_open_generic,
5344 .read = tracing_saved_cmdlines_size_read,
5345 .write = tracing_saved_cmdlines_size_write,
5346};
5347
Jeremy Linton681bec02017-05-31 16:56:53 -05005348#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005349static union trace_eval_map_item *
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005350update_eval_map(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005351{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005352 if (!ptr->map.eval_string) {
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005353 if (ptr->tail.next) {
5354 ptr = ptr->tail.next;
5355 /* Set ptr to the next real item (skip head) */
5356 ptr++;
5357 } else
5358 return NULL;
5359 }
5360 return ptr;
5361}
5362
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005363static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005364{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005365 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005366
5367 /*
5368 * Paranoid! If ptr points to end, we don't want to increment past it.
5369 * This really should never happen.
5370 */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005371 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005372 if (WARN_ON_ONCE(!ptr))
5373 return NULL;
5374
5375 ptr++;
5376
5377 (*pos)++;
5378
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005379 ptr = update_eval_map(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005380
5381 return ptr;
5382}
5383
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005384static void *eval_map_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005385{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005386 union trace_eval_map_item *v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005387 loff_t l = 0;
5388
Jeremy Linton1793ed92017-05-31 16:56:46 -05005389 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005390
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005391 v = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005392 if (v)
5393 v++;
5394
5395 while (v && l < *pos) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005396 v = eval_map_next(m, v, &l);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005397 }
5398
5399 return v;
5400}
5401
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005402static void eval_map_stop(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005403{
Jeremy Linton1793ed92017-05-31 16:56:46 -05005404 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005405}
5406
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005407static int eval_map_show(struct seq_file *m, void *v)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005408{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005409 union trace_eval_map_item *ptr = v;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005410
5411 seq_printf(m, "%s %ld (%s)\n",
Jeremy Linton00f4b652017-05-31 16:56:43 -05005412 ptr->map.eval_string, ptr->map.eval_value,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005413 ptr->map.system);
5414
5415 return 0;
5416}
5417
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005418static const struct seq_operations tracing_eval_map_seq_ops = {
5419 .start = eval_map_start,
5420 .next = eval_map_next,
5421 .stop = eval_map_stop,
5422 .show = eval_map_show,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005423};
5424
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005425static int tracing_eval_map_open(struct inode *inode, struct file *filp)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005426{
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005427 int ret;
5428
5429 ret = tracing_check_open_get_tr(NULL);
5430 if (ret)
5431 return ret;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005432
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005433 return seq_open(filp, &tracing_eval_map_seq_ops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005434}
5435
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005436static const struct file_operations tracing_eval_map_fops = {
5437 .open = tracing_eval_map_open,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005438 .read = seq_read,
5439 .llseek = seq_lseek,
5440 .release = seq_release,
5441};
5442
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005443static inline union trace_eval_map_item *
Jeremy Linton5f60b352017-05-31 16:56:47 -05005444trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005445{
5446 /* Return tail of array given the head */
5447 return ptr + ptr->head.length + 1;
5448}
5449
5450static void
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005451trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005452 int len)
5453{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005454 struct trace_eval_map **stop;
5455 struct trace_eval_map **map;
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005456 union trace_eval_map_item *map_array;
5457 union trace_eval_map_item *ptr;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005458
5459 stop = start + len;
5460
5461 /*
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005462 * The trace_eval_maps contains the map plus a head and tail item,
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005463 * where the head holds the module and length of array, and the
5464 * tail holds a pointer to the next list.
5465 */
Kees Cook6da2ec52018-06-12 13:55:00 -07005466 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005467 if (!map_array) {
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005468 pr_warn("Unable to allocate trace eval mapping\n");
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005469 return;
5470 }
5471
Jeremy Linton1793ed92017-05-31 16:56:46 -05005472 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005473
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005474 if (!trace_eval_maps)
5475 trace_eval_maps = map_array;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005476 else {
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05005477 ptr = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005478 for (;;) {
Jeremy Linton5f60b352017-05-31 16:56:47 -05005479 ptr = trace_eval_jmp_to_tail(ptr);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005480 if (!ptr->tail.next)
5481 break;
5482 ptr = ptr->tail.next;
5483
5484 }
5485 ptr->tail.next = map_array;
5486 }
5487 map_array->head.mod = mod;
5488 map_array->head.length = len;
5489 map_array++;
5490
5491 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5492 map_array->map = **map;
5493 map_array++;
5494 }
5495 memset(map_array, 0, sizeof(*map_array));
5496
Jeremy Linton1793ed92017-05-31 16:56:46 -05005497 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005498}
5499
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005500static void trace_create_eval_file(struct dentry *d_tracer)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005501{
Jeremy Linton681bec02017-05-31 16:56:53 -05005502 trace_create_file("eval_map", 0444, d_tracer,
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005503 NULL, &tracing_eval_map_fops);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005504}
5505
Jeremy Linton681bec02017-05-31 16:56:53 -05005506#else /* CONFIG_TRACE_EVAL_MAP_FILE */
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005507static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5508static inline void trace_insert_eval_map_file(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005509 struct trace_eval_map **start, int len) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05005510#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005511
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005512static void trace_insert_eval_map(struct module *mod,
Jeremy Linton00f4b652017-05-31 16:56:43 -05005513 struct trace_eval_map **start, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005514{
Jeremy Linton00f4b652017-05-31 16:56:43 -05005515 struct trace_eval_map **map;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005516
5517 if (len <= 0)
5518 return;
5519
5520 map = start;
5521
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005522 trace_event_eval_update(map, len);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04005523
Jeremy Lintonf57a4142017-05-31 16:56:48 -05005524 trace_insert_eval_map_file(mod, start, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04005525}
5526
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09005527static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005528tracing_set_trace_read(struct file *filp, char __user *ubuf,
5529 size_t cnt, loff_t *ppos)
5530{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005531 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005532 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005533 int r;
5534
5535 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005536 r = sprintf(buf, "%s\n", tr->current_trace->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005537 mutex_unlock(&trace_types_lock);
5538
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005539 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005540}
5541
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005542int tracer_init(struct tracer *t, struct trace_array *tr)
5543{
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005544 tracing_reset_online_cpus(&tr->array_buffer);
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005545 return t->init(tr);
5546}
5547
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005548static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005549{
5550 int cpu;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05005551
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005552 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005553 per_cpu_ptr(buf->data, cpu)->entries = val;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005554}
5555
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005556#ifdef CONFIG_TRACER_MAX_TRACE
Hiraku Toyookad60da502012-10-17 11:56:16 +09005557/* resize @tr's buffer to the size of @size_tr's entries */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005558static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5559 struct array_buffer *size_buf, int cpu_id)
Hiraku Toyookad60da502012-10-17 11:56:16 +09005560{
5561 int cpu, ret = 0;
5562
5563 if (cpu_id == RING_BUFFER_ALL_CPUS) {
5564 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005565 ret = ring_buffer_resize(trace_buf->buffer,
5566 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005567 if (ret < 0)
5568 break;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005569 per_cpu_ptr(trace_buf->data, cpu)->entries =
5570 per_cpu_ptr(size_buf->data, cpu)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005571 }
5572 } else {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005573 ret = ring_buffer_resize(trace_buf->buffer,
5574 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005575 if (ret == 0)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005576 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
5577 per_cpu_ptr(size_buf->data, cpu_id)->entries;
Hiraku Toyookad60da502012-10-17 11:56:16 +09005578 }
5579
5580 return ret;
5581}
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005582#endif /* CONFIG_TRACER_MAX_TRACE */
Hiraku Toyookad60da502012-10-17 11:56:16 +09005583
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005584static int __tracing_resize_ring_buffer(struct trace_array *tr,
5585 unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04005586{
5587 int ret;
5588
5589 /*
5590 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04005591 * we use the size that was given, and we can forget about
5592 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04005593 */
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05005594 ring_buffer_expanded = true;
Steven Rostedt73c51622009-03-11 13:42:01 -04005595
Steven Rostedtb382ede62012-10-10 21:44:34 -04005596 /* May be called before buffers are initialized */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005597 if (!tr->array_buffer.buffer)
Steven Rostedtb382ede62012-10-10 21:44:34 -04005598 return 0;
5599
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005600 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005601 if (ret < 0)
5602 return ret;
5603
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005604#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005605 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
5606 !tr->current_trace->use_max_tr)
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005607 goto out;
5608
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005609 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005610 if (ret < 0) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005611 int r = resize_buffer_duplicate_size(&tr->array_buffer,
5612 &tr->array_buffer, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04005613 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04005614 /*
5615 * AARGH! We are left with different
5616 * size max buffer!!!!
5617 * The max buffer is our "snapshot" buffer.
5618 * When a tracer needs a snapshot (one of the
5619 * latency tracers), it swaps the max buffer
5620 * with the saved snap shot. We succeeded to
5621 * update the size of the main buffer, but failed to
5622 * update the size of the max buffer. But when we tried
5623 * to reset the main buffer to the original size, we
5624 * failed there too. This is very unlikely to
5625 * happen, but if it does, warn and kill all
5626 * tracing.
5627 */
Steven Rostedt73c51622009-03-11 13:42:01 -04005628 WARN_ON(1);
5629 tracing_disabled = 1;
5630 }
5631 return ret;
5632 }
5633
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005634 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005635 set_buffer_entries(&tr->max_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005636 else
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005637 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005638
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005639 out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005640#endif /* CONFIG_TRACER_MAX_TRACE */
5641
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005642 if (cpu == RING_BUFFER_ALL_CPUS)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005643 set_buffer_entries(&tr->array_buffer, size);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005644 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005645 per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04005646
5647 return ret;
5648}
5649
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005650static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5651 unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005652{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07005653 int ret = size;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005654
5655 mutex_lock(&trace_types_lock);
5656
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005657 if (cpu_id != RING_BUFFER_ALL_CPUS) {
5658 /* make sure, this cpu is enabled in the mask */
5659 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
5660 ret = -EINVAL;
5661 goto out;
5662 }
5663 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005664
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005665 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005666 if (ret < 0)
5667 ret = -ENOMEM;
5668
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005669out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07005670 mutex_unlock(&trace_types_lock);
5671
5672 return ret;
5673}
5674
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005675
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005676/**
5677 * tracing_update_buffers - used by tracing facility to expand ring buffers
5678 *
5679 * To save on memory when the tracing is never used on a system with it
5680 * configured in. The ring buffers are set to a minimum size. But once
5681 * a user starts to use the tracing facility, then they need to grow
5682 * to their default size.
5683 *
5684 * This function is to be called when a tracer is about to be used.
5685 */
5686int tracing_update_buffers(void)
5687{
5688 int ret = 0;
5689
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005690 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005691 if (!ring_buffer_expanded)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005692 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005693 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005694 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04005695
5696 return ret;
5697}
5698
Steven Rostedt577b7852009-02-26 23:43:05 -05005699struct trace_option_dentry;
5700
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005701static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005702create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
Steven Rostedt577b7852009-02-26 23:43:05 -05005703
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005704/*
5705 * Used to clear out the tracer before deletion of an instance.
5706 * Must have trace_types_lock held.
5707 */
5708static void tracing_set_nop(struct trace_array *tr)
5709{
5710 if (tr->current_trace == &nop_trace)
5711 return;
5712
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005713 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05005714
5715 if (tr->current_trace->reset)
5716 tr->current_trace->reset(tr);
5717
5718 tr->current_trace = &nop_trace;
5719}
5720
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04005721static void add_tracer_options(struct trace_array *tr, struct tracer *t)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005722{
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005723 /* Only enable if the directory has been created already. */
5724 if (!tr->dir)
5725 return;
5726
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04005727 create_trace_option_files(tr, t);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05005728}
5729
5730static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5731{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005732 struct tracer *t;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005733#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005734 bool had_max_tr;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005735#endif
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005736 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005737
Steven Rostedt1027fcb2009-03-12 11:33:20 -04005738 mutex_lock(&trace_types_lock);
5739
Steven Rostedt73c51622009-03-11 13:42:01 -04005740 if (!ring_buffer_expanded) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005741 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005742 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04005743 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01005744 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04005745 ret = 0;
5746 }
5747
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005748 for (t = trace_types; t; t = t->next) {
5749 if (strcmp(t->name, buf) == 0)
5750 break;
5751 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005752 if (!t) {
5753 ret = -EINVAL;
5754 goto out;
5755 }
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005756 if (t == tr->current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005757 goto out;
5758
Tom Zanussia35873a2019-02-13 17:42:45 -06005759#ifdef CONFIG_TRACER_SNAPSHOT
5760 if (t->use_max_tr) {
5761 arch_spin_lock(&tr->max_lock);
5762 if (tr->cond_snapshot)
5763 ret = -EBUSY;
5764 arch_spin_unlock(&tr->max_lock);
5765 if (ret)
5766 goto out;
5767 }
5768#endif
Ziqian SUN (Zamir)c7b3ae02017-09-11 14:26:35 +08005769 /* Some tracers won't work on kernel command line */
5770 if (system_state < SYSTEM_RUNNING && t->noboot) {
5771 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
5772 t->name);
5773 goto out;
5774 }
5775
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005776 /* Some tracers are only allowed for the top level buffer */
5777 if (!trace_ok_for_array(t, tr)) {
5778 ret = -EINVAL;
5779 goto out;
5780 }
5781
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005782 /* If trace pipe files are being read, we can't change the tracer */
5783 if (tr->current_trace->ref) {
5784 ret = -EBUSY;
5785 goto out;
5786 }
5787
Steven Rostedt9f029e82008-11-12 15:24:24 -05005788 trace_branch_disable();
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005789
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005790 tr->current_trace->enabled--;
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04005791
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005792 if (tr->current_trace->reset)
5793 tr->current_trace->reset(tr);
Steven Rostedt34600f02013-01-22 13:35:11 -05005794
Paul E. McKenney74401722018-11-06 18:44:52 -08005795 /* Current trace needs to be nop_trace before synchronize_rcu */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005796 tr->current_trace = &nop_trace;
Steven Rostedt34600f02013-01-22 13:35:11 -05005797
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05005798#ifdef CONFIG_TRACER_MAX_TRACE
5799 had_max_tr = tr->allocated_snapshot;
Steven Rostedt34600f02013-01-22 13:35:11 -05005800
5801 if (had_max_tr && !t->use_max_tr) {
5802 /*
5803 * We need to make sure that the update_max_tr sees that
5804 * current_trace changed to nop_trace to keep it from
5805 * swapping the buffers after we resize it.
5806 * The update_max_tr is called from interrupts disabled
5807 * so a synchronized_sched() is sufficient.
5808 */
Paul E. McKenney74401722018-11-06 18:44:52 -08005809 synchronize_rcu();
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04005810 free_snapshot(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005811 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005812#endif
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005813
5814#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt34600f02013-01-22 13:35:11 -05005815 if (t->use_max_tr && !had_max_tr) {
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04005816 ret = tracing_alloc_snapshot_instance(tr);
Hiraku Toyookad60da502012-10-17 11:56:16 +09005817 if (ret < 0)
5818 goto out;
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09005819 }
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05005820#endif
Steven Rostedt577b7852009-02-26 23:43:05 -05005821
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005822 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02005823 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01005824 if (ret)
5825 goto out;
5826 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005827
Steven Rostedt2b6080f2012-05-11 13:29:49 -04005828 tr->current_trace = t;
Steven Rostedt (Red Hat)50512ab2014-01-14 08:52:35 -05005829 tr->current_trace->enabled++;
Steven Rostedt9f029e82008-11-12 15:24:24 -05005830 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005831 out:
5832 mutex_unlock(&trace_types_lock);
5833
Peter Zijlstrad9e54072008-11-01 19:57:37 +01005834 return ret;
5835}
5836
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005837static ssize_t
5838tracing_set_trace_write(struct file *filp, const char __user *ubuf,
5839 size_t cnt, loff_t *ppos)
5840{
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005841 struct trace_array *tr = filp->private_data;
Li Zefanee6c2c12009-09-18 14:06:47 +08005842 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005843 int i;
5844 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005845 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005846
Steven Rostedt60063a62008-10-28 10:44:24 -04005847 ret = cnt;
5848
Li Zefanee6c2c12009-09-18 14:06:47 +08005849 if (cnt > MAX_TRACER_SIZE)
5850 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005851
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08005852 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005853 return -EFAULT;
5854
5855 buf[cnt] = 0;
5856
5857 /* strip ending whitespace. */
5858 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
5859 buf[i] = 0;
5860
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05005861 err = tracing_set_tracer(tr, buf);
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01005862 if (err)
5863 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005864
Jiri Olsacf8517c2009-10-23 19:36:16 -04005865 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005866
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02005867 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005868}
5869
5870static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005871tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
5872 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005873{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005874 char buf[64];
5875 int r;
5876
Steven Rostedtcffae432008-05-12 21:21:00 +02005877 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005878 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02005879 if (r > sizeof(buf))
5880 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02005881 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005882}
5883
5884static ssize_t
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005885tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
5886 size_t cnt, loff_t *ppos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005887{
Hannes Eder5e398412009-02-10 19:44:34 +01005888 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005889 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005890
Peter Huewe22fe9b52011-06-07 21:58:27 +02005891 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5892 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02005893 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005894
5895 *ptr = val * 1000;
5896
5897 return cnt;
5898}
5899
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005900static ssize_t
5901tracing_thresh_read(struct file *filp, char __user *ubuf,
5902 size_t cnt, loff_t *ppos)
5903{
5904 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
5905}
5906
5907static ssize_t
5908tracing_thresh_write(struct file *filp, const char __user *ubuf,
5909 size_t cnt, loff_t *ppos)
5910{
5911 struct trace_array *tr = filp->private_data;
5912 int ret;
5913
5914 mutex_lock(&trace_types_lock);
5915 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
5916 if (ret < 0)
5917 goto out;
5918
5919 if (tr->current_trace->update_thresh) {
5920 ret = tr->current_trace->update_thresh(tr);
5921 if (ret < 0)
5922 goto out;
5923 }
5924
5925 ret = cnt;
5926out:
5927 mutex_unlock(&trace_types_lock);
5928
5929 return ret;
5930}
5931
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04005932#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Chen Gange428abb2015-11-10 05:15:15 +08005933
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04005934static ssize_t
5935tracing_max_lat_read(struct file *filp, char __user *ubuf,
5936 size_t cnt, loff_t *ppos)
5937{
5938 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos);
5939}
5940
5941static ssize_t
5942tracing_max_lat_write(struct file *filp, const char __user *ubuf,
5943 size_t cnt, loff_t *ppos)
5944{
5945 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos);
5946}
5947
Chen Gange428abb2015-11-10 05:15:15 +08005948#endif
5949
Steven Rostedtb3806b42008-05-12 21:20:46 +02005950static int tracing_open_pipe(struct inode *inode, struct file *filp)
5951{
Oleg Nesterov15544202013-07-23 17:25:57 +02005952 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005953 struct trace_iterator *iter;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005954 int ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02005955
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04005956 ret = tracing_check_open_get_tr(tr);
5957 if (ret)
5958 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04005959
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005960 mutex_lock(&trace_types_lock);
5961
Steven Rostedtb3806b42008-05-12 21:20:46 +02005962 /* create a buffer to store the information to pass to userspace */
5963 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005964 if (!iter) {
5965 ret = -ENOMEM;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07005966 __trace_array_put(tr);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01005967 goto out;
5968 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02005969
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005970 trace_seq_init(&iter->seq);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05005971 iter->trace = tr->current_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005972
5973 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
5974 ret = -ENOMEM;
5975 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10305976 }
5977
Steven Rostedta3097202008-11-07 22:36:02 -05005978 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10305979 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05005980
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04005981 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt112f38a72009-06-01 15:16:05 -04005982 iter->iter_flags |= TRACE_FILE_LAT_FMT;
5983
David Sharp8be07092012-11-13 12:18:22 -08005984 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09005985 if (trace_clocks[tr->clock_id].in_ns)
David Sharp8be07092012-11-13 12:18:22 -08005986 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
5987
Oleg Nesterov15544202013-07-23 17:25:57 +02005988 iter->tr = tr;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05005989 iter->array_buffer = &tr->array_buffer;
Oleg Nesterov15544202013-07-23 17:25:57 +02005990 iter->cpu_file = tracing_get_cpu(inode);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01005991 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02005992 filp->private_data = iter;
5993
Steven Rostedt107bad82008-05-12 21:21:01 +02005994 if (iter->trace->pipe_open)
5995 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02005996
Arnd Bergmannb4447862010-07-07 23:40:11 +02005997 nonseekable_open(inode, filp);
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05005998
5999 tr->current_trace->ref++;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006000out:
6001 mutex_unlock(&trace_types_lock);
6002 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006003
6004fail:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006005 kfree(iter);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006006 __trace_array_put(tr);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006007 mutex_unlock(&trace_types_lock);
6008 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006009}
6010
6011static int tracing_release_pipe(struct inode *inode, struct file *file)
6012{
6013 struct trace_iterator *iter = file->private_data;
Oleg Nesterov15544202013-07-23 17:25:57 +02006014 struct trace_array *tr = inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006015
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006016 mutex_lock(&trace_types_lock);
6017
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05006018 tr->current_trace->ref--;
6019
Steven Rostedt29bf4a52009-12-09 12:37:43 -05006020 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05006021 iter->trace->pipe_close(iter);
6022
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01006023 mutex_unlock(&trace_types_lock);
6024
Rusty Russell44623442009-01-01 10:12:23 +10306025 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006026 mutex_destroy(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006027 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006028
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006029 trace_array_put(tr);
6030
Steven Rostedtb3806b42008-05-12 21:20:46 +02006031 return 0;
6032}
6033
Al Viro9dd95742017-07-03 00:42:43 -04006034static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006035trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006036{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006037 struct trace_array *tr = iter->tr;
6038
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006039 /* Iterators are static, they should be filled or empty */
6040 if (trace_buffer_iter(iter, iter->cpu_file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08006041 return EPOLLIN | EPOLLRDNORM;
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006042
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006043 if (tr->trace_flags & TRACE_ITER_BLOCK)
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006044 /*
6045 * Always select as readable when in blocking mode
6046 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08006047 return EPOLLIN | EPOLLRDNORM;
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006048 else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006049 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
Steven Rostedt (Red Hat)15693452013-02-28 19:59:17 -05006050 filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006051}
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006052
Al Viro9dd95742017-07-03 00:42:43 -04006053static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05006054tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6055{
6056 struct trace_iterator *iter = filp->private_data;
6057
6058 return trace_poll(iter, filp, poll_table);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006059}
6060
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05006061/* Must be called with iter->mutex held. */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006062static int tracing_wait_pipe(struct file *filp)
6063{
6064 struct trace_iterator *iter = filp->private_data;
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006065 int ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006066
6067 while (trace_empty(iter)) {
6068
6069 if ((filp->f_flags & O_NONBLOCK)) {
6070 return -EAGAIN;
6071 }
6072
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006073 /*
Liu Bo250bfd32013-01-14 10:54:11 +08006074 * We block until we read something and tracing is disabled.
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006075 * We still block if tracing is disabled, but we have never
6076 * read anything. This allows a user to cat this file, and
6077 * then enable tracing. But after we have read something,
6078 * we give an EOF when tracing is again disabled.
6079 *
6080 * iter->pos will be 0 if we haven't read anything.
6081 */
Tahsin Erdogan75df6e62017-09-17 03:23:48 -07006082 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006083 break;
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04006084
6085 mutex_unlock(&iter->mutex);
6086
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05006087 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)f4874262014-04-29 16:07:28 -04006088
6089 mutex_lock(&iter->mutex);
6090
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04006091 if (ret)
6092 return ret;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006093 }
6094
6095 return 1;
6096}
6097
Steven Rostedtb3806b42008-05-12 21:20:46 +02006098/*
6099 * Consumer reader.
6100 */
6101static ssize_t
6102tracing_read_pipe(struct file *filp, char __user *ubuf,
6103 size_t cnt, loff_t *ppos)
6104{
6105 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006106 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006107
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006108 /*
6109 * Avoid more than one consumer on a single file descriptor
6110 * This is just a matter of traces coherency, the ring buffer itself
6111 * is protected.
6112 */
6113 mutex_lock(&iter->mutex);
Steven Rostedt (Red Hat)12458002016-09-23 22:57:13 -04006114
6115 /* return any leftover data */
6116 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6117 if (sret != -EBUSY)
6118 goto out;
6119
6120 trace_seq_init(&iter->seq);
6121
Steven Rostedt107bad82008-05-12 21:21:01 +02006122 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006123 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6124 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02006125 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02006126 }
6127
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006128waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006129 sret = tracing_wait_pipe(filp);
6130 if (sret <= 0)
6131 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006132
6133 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006134 if (trace_empty(iter)) {
6135 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02006136 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02006137 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02006138
6139 if (cnt >= PAGE_SIZE)
6140 cnt = PAGE_SIZE - 1;
6141
Steven Rostedt53d0aa72008-05-12 21:21:01 +02006142 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02006143 memset(&iter->seq, 0,
6144 sizeof(struct trace_iterator) -
6145 offsetof(struct trace_iterator, seq));
Andrew Vagined5467d2013-08-02 21:16:43 +04006146 cpumask_clear(iter->started);
Petr Mladekd303de12019-10-11 16:21:34 +02006147 trace_seq_init(&iter->seq);
Steven Rostedt4823ed72008-05-12 21:21:01 +02006148 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006149
Lai Jiangshan4f535962009-05-18 19:35:34 +08006150 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006151 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05006152 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02006153 enum print_line_t ret;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006154 int save_len = iter->seq.seq.len;
Steven Rostedt088b1e422008-05-12 21:20:48 +02006155
Ingo Molnarf9896bf2008-05-12 21:20:47 +02006156 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02006157 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02006158 /* don't print partial lines */
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006159 iter->seq.seq.len = save_len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006160 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02006161 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01006162 if (ret != TRACE_TYPE_NO_CONSUME)
6163 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006164
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006165 if (trace_seq_used(&iter->seq) >= cnt)
Steven Rostedtb3806b42008-05-12 21:20:46 +02006166 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01006167
6168 /*
6169 * Setting the full flag means we reached the trace_seq buffer
6170 * size and we should leave by partial output condition above.
6171 * One of the trace_seq_* functions is not used properly.
6172 */
6173 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6174 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02006175 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006176 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006177 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02006178
Steven Rostedtb3806b42008-05-12 21:20:46 +02006179 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006180 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006181 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
Steven Rostedtf9520752009-03-02 14:04:40 -05006182 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006183
6184 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006185 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006186 * entries, go back to wait for more entries.
6187 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006188 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02006189 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006190
Steven Rostedt107bad82008-05-12 21:21:01 +02006191out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006192 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02006193
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02006194 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02006195}
6196
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006197static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6198 unsigned int idx)
6199{
6200 __free_page(spd->pages[idx]);
6201}
6202
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08006203static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006204 .confirm = generic_pipe_buf_confirm,
Al Viro92fdd982014-01-17 07:53:39 -05006205 .release = generic_pipe_buf_release,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006206 .steal = generic_pipe_buf_steal,
6207 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006208};
6209
Steven Rostedt34cd4992009-02-09 12:06:29 -05006210static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006211tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006212{
6213 size_t count;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006214 int save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006215 int ret;
6216
6217 /* Seq buffer is page-sized, exactly what we need. */
6218 for (;;) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006219 save_len = iter->seq.seq.len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006220 ret = print_trace_line(iter);
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006221
6222 if (trace_seq_has_overflowed(&iter->seq)) {
6223 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006224 break;
6225 }
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006226
6227 /*
6228 * This should not be hit, because it should only
6229 * be set if the iter->seq overflowed. But check it
6230 * anyway to be safe.
6231 */
Steven Rostedt34cd4992009-02-09 12:06:29 -05006232 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006233 iter->seq.seq.len = save_len;
6234 break;
6235 }
6236
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006237 count = trace_seq_used(&iter->seq) - save_len;
Steven Rostedt (Red Hat)74f06bb2014-11-17 13:12:22 -05006238 if (rem < count) {
6239 rem = 0;
6240 iter->seq.seq.len = save_len;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006241 break;
6242 }
6243
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08006244 if (ret != TRACE_TYPE_NO_CONSUME)
6245 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05006246 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05006247 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05006248 rem = 0;
6249 iter->ent = NULL;
6250 break;
6251 }
6252 }
6253
6254 return rem;
6255}
6256
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006257static ssize_t tracing_splice_read_pipe(struct file *filp,
6258 loff_t *ppos,
6259 struct pipe_inode_info *pipe,
6260 size_t len,
6261 unsigned int flags)
6262{
Jens Axboe35f3d142010-05-20 10:43:18 +02006263 struct page *pages_def[PIPE_DEF_BUFFERS];
6264 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006265 struct trace_iterator *iter = filp->private_data;
6266 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02006267 .pages = pages_def,
6268 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006269 .nr_pages = 0, /* This gets updated below. */
Eric Dumazet047fe362012-06-12 15:24:40 +02006270 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt34cd4992009-02-09 12:06:29 -05006271 .ops = &tracing_pipe_buf_ops,
6272 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006273 };
6274 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006275 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006276 unsigned int i;
6277
Jens Axboe35f3d142010-05-20 10:43:18 +02006278 if (splice_grow_spd(pipe, &spd))
6279 return -ENOMEM;
6280
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006281 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006282
6283 if (iter->trace->splice_read) {
6284 ret = iter->trace->splice_read(iter, filp,
6285 ppos, pipe, len, flags);
6286 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006287 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006288 }
6289
6290 ret = tracing_wait_pipe(filp);
6291 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05006292 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006293
Jason Wessel955b61e2010-08-05 09:22:23 -05006294 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006295 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05006296 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006297 }
6298
Lai Jiangshan4f535962009-05-18 19:35:34 +08006299 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006300 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006301
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006302 /* Fill as many pages as possible. */
Al Viroa786c062014-04-11 12:01:03 -04006303 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006304 spd.pages[i] = alloc_page(GFP_KERNEL);
6305 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05006306 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006307
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01006308 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006309
6310 /* Copy the data into the page, so we can start over. */
6311 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02006312 page_address(spd.pages[i]),
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006313 trace_seq_used(&iter->seq));
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006314 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02006315 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006316 break;
6317 }
Jens Axboe35f3d142010-05-20 10:43:18 +02006318 spd.partial[i].offset = 0;
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05006319 spd.partial[i].len = trace_seq_used(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006320
Steven Rostedtf9520752009-03-02 14:04:40 -05006321 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006322 }
6323
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08006324 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08006325 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006326 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006327
6328 spd.nr_pages = i;
6329
Steven Rostedt (Red Hat)a29054d92016-03-18 15:46:48 -04006330 if (i)
6331 ret = splice_to_pipe(pipe, &spd);
6332 else
6333 ret = 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02006334out:
Eric Dumazet047fe362012-06-12 15:24:40 +02006335 splice_shrink_spd(&spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02006336 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006337
Steven Rostedt34cd4992009-02-09 12:06:29 -05006338out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01006339 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02006340 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006341}
6342
Steven Rostedta98a3c32008-05-12 21:20:59 +02006343static ssize_t
6344tracing_entries_read(struct file *filp, char __user *ubuf,
6345 size_t cnt, loff_t *ppos)
6346{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006347 struct inode *inode = file_inode(filp);
6348 struct trace_array *tr = inode->i_private;
6349 int cpu = tracing_get_cpu(inode);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006350 char buf[64];
6351 int r = 0;
6352 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006353
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006354 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006355
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006356 if (cpu == RING_BUFFER_ALL_CPUS) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006357 int cpu, buf_size_same;
6358 unsigned long size;
6359
6360 size = 0;
6361 buf_size_same = 1;
6362 /* check if all cpu sizes are same */
6363 for_each_tracing_cpu(cpu) {
6364 /* fill in the size from first enabled cpu */
6365 if (size == 0)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006366 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6367 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006368 buf_size_same = 0;
6369 break;
6370 }
6371 }
6372
6373 if (buf_size_same) {
6374 if (!ring_buffer_expanded)
6375 r = sprintf(buf, "%lu (expanded: %lu)\n",
6376 size >> 10,
6377 trace_buf_size >> 10);
6378 else
6379 r = sprintf(buf, "%lu\n", size >> 10);
6380 } else
6381 r = sprintf(buf, "X\n");
6382 } else
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006383 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006384
Steven Rostedtdb526ca2009-03-12 13:53:25 -04006385 mutex_unlock(&trace_types_lock);
6386
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08006387 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6388 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006389}
6390
6391static ssize_t
6392tracing_entries_write(struct file *filp, const char __user *ubuf,
6393 size_t cnt, loff_t *ppos)
6394{
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006395 struct inode *inode = file_inode(filp);
6396 struct trace_array *tr = inode->i_private;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006397 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006398 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006399
Peter Huewe22fe9b52011-06-07 21:58:27 +02006400 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6401 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02006402 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006403
6404 /* must have at least 1 entry */
6405 if (!val)
6406 return -EINVAL;
6407
Steven Rostedt1696b2b2008-11-13 00:09:35 -05006408 /* value is in KB */
6409 val <<= 10;
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006410 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006411 if (ret < 0)
6412 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006413
Jiri Olsacf8517c2009-10-23 19:36:16 -04006414 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006415
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006416 return cnt;
6417}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05006418
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006419static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006420tracing_total_entries_read(struct file *filp, char __user *ubuf,
6421 size_t cnt, loff_t *ppos)
6422{
6423 struct trace_array *tr = filp->private_data;
6424 char buf[64];
6425 int r, cpu;
6426 unsigned long size = 0, expanded_size = 0;
6427
6428 mutex_lock(&trace_types_lock);
6429 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006430 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006431 if (!ring_buffer_expanded)
6432 expanded_size += trace_buf_size >> 10;
6433 }
6434 if (ring_buffer_expanded)
6435 r = sprintf(buf, "%lu\n", size);
6436 else
6437 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6438 mutex_unlock(&trace_types_lock);
6439
6440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6441}
6442
6443static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006444tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
6445 size_t cnt, loff_t *ppos)
6446{
6447 /*
6448 * There is no need to read what the user has written, this function
6449 * is just to make sure that there is no error when "echo" is used
6450 */
6451
6452 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02006453
6454 return cnt;
6455}
6456
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006457static int
6458tracing_free_buffer_release(struct inode *inode, struct file *filp)
6459{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006460 struct trace_array *tr = inode->i_private;
6461
Steven Rostedtcf30cf62011-06-14 22:44:07 -04006462 /* disable tracing ? */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006463 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
Alexander Z Lam711e1242013-08-02 18:36:15 -07006464 tracer_tracing_off(tr);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006465 /* resize the ring buffer to 0 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006466 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006467
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006468 trace_array_put(tr);
6469
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07006470 return 0;
6471}
6472
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006473static ssize_t
6474tracing_mark_write(struct file *filp, const char __user *ubuf,
6475 size_t cnt, loff_t *fpos)
6476{
Alexander Z Lam2d716192013-07-01 15:31:24 -07006477 struct trace_array *tr = filp->private_data;
Steven Rostedtd696b582011-09-22 11:50:27 -04006478 struct ring_buffer_event *event;
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006479 enum event_trigger_type tt = ETT_NONE;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05006480 struct trace_buffer *buffer;
Steven Rostedtd696b582011-09-22 11:50:27 -04006481 struct print_entry *entry;
6482 unsigned long irq_flags;
Steven Rostedtd696b582011-09-22 11:50:27 -04006483 ssize_t written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006484 int size;
6485 int len;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006486
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006487/* Used in tracing_mark_raw_write() as well */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006488#define FAULTED_STR "<faulted>"
6489#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006490
Steven Rostedtc76f0692008-11-07 22:36:02 -05006491 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006492 return -EINVAL;
6493
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04006494 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
Mandeep Singh Baines5224c3a2012-09-07 18:12:19 -07006495 return -EINVAL;
6496
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006497 if (cnt > TRACE_BUF_SIZE)
6498 cnt = TRACE_BUF_SIZE;
6499
Steven Rostedtd696b582011-09-22 11:50:27 -04006500 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006501
Steven Rostedtd696b582011-09-22 11:50:27 -04006502 local_save_flags(irq_flags);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006503 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
6504
6505 /* If less than "<faulted>", then make sure we can still add that */
6506 if (cnt < FAULTED_SIZE)
6507 size += FAULTED_SIZE - cnt;
6508
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006509 buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006510 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
6511 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006512 if (unlikely(!event))
Steven Rostedtd696b582011-09-22 11:50:27 -04006513 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006514 return -EBADF;
Steven Rostedtd696b582011-09-22 11:50:27 -04006515
6516 entry = ring_buffer_event_data(event);
6517 entry->ip = _THIS_IP_;
6518
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006519 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
6520 if (len) {
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006521 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006522 cnt = FAULTED_SIZE;
6523 written = -EFAULT;
Steven Rostedtd696b582011-09-22 11:50:27 -04006524 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006525 written = cnt;
6526 len = cnt;
Steven Rostedtd696b582011-09-22 11:50:27 -04006527
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006528 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
6529 /* do not add \n before testing triggers, but add \0 */
6530 entry->buf[cnt] = '\0';
6531 tt = event_triggers_call(tr->trace_marker_file, entry, event);
6532 }
6533
Steven Rostedtd696b582011-09-22 11:50:27 -04006534 if (entry->buf[cnt - 1] != '\n') {
6535 entry->buf[cnt] = '\n';
6536 entry->buf[cnt + 1] = '\0';
6537 } else
6538 entry->buf[cnt] = '\0';
6539
Steven Rostedt7ffbd482012-10-11 12:14:25 -04006540 __buffer_unlock_commit(buffer, event);
Steven Rostedtd696b582011-09-22 11:50:27 -04006541
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04006542 if (tt)
6543 event_triggers_post_call(tr->trace_marker_file, tt);
6544
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006545 if (written > 0)
6546 *fpos += written;
Steven Rostedtd696b582011-09-22 11:50:27 -04006547
Steven Rostedtfa32e852016-07-06 15:25:08 -04006548 return written;
6549}
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006550
Steven Rostedtfa32e852016-07-06 15:25:08 -04006551/* Limit it for now to 3K (including tag) */
6552#define RAW_DATA_MAX_SIZE (1024*3)
6553
6554static ssize_t
6555tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
6556 size_t cnt, loff_t *fpos)
6557{
6558 struct trace_array *tr = filp->private_data;
6559 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05006560 struct trace_buffer *buffer;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006561 struct raw_data_entry *entry;
6562 unsigned long irq_flags;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006563 ssize_t written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006564 int size;
6565 int len;
6566
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006567#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
6568
Steven Rostedtfa32e852016-07-06 15:25:08 -04006569 if (tracing_disabled)
6570 return -EINVAL;
6571
6572 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
6573 return -EINVAL;
6574
6575 /* The marker must at least have a tag id */
6576 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
6577 return -EINVAL;
6578
6579 if (cnt > TRACE_BUF_SIZE)
6580 cnt = TRACE_BUF_SIZE;
6581
6582 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
6583
Steven Rostedtfa32e852016-07-06 15:25:08 -04006584 local_save_flags(irq_flags);
6585 size = sizeof(*entry) + cnt;
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006586 if (cnt < FAULT_SIZE_ID)
6587 size += FAULT_SIZE_ID - cnt;
6588
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006589 buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)3e9a8aa2016-11-23 11:29:58 -05006590 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
6591 irq_flags, preempt_count());
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006592 if (!event)
Steven Rostedtfa32e852016-07-06 15:25:08 -04006593 /* Ring buffer disabled, return as if not open for write */
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006594 return -EBADF;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006595
6596 entry = ring_buffer_event_data(event);
6597
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006598 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
6599 if (len) {
6600 entry->id = -1;
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01006601 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006602 written = -EFAULT;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006603 } else
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006604 written = cnt;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006605
6606 __buffer_unlock_commit(buffer, event);
6607
Steven Rostedt (Red Hat)656c7f02016-12-08 12:40:18 -05006608 if (written > 0)
6609 *fpos += written;
Steven Rostedtfa32e852016-07-06 15:25:08 -04006610
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02006611 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03006612}
6613
Li Zefan13f16d22009-12-08 11:16:11 +08006614static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08006615{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006616 struct trace_array *tr = m->private;
Zhaolei5079f322009-08-25 16:12:56 +08006617 int i;
6618
6619 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08006620 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08006621 "%s%s%s%s", i ? " " : "",
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006622 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
6623 i == tr->clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08006624 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08006625
Li Zefan13f16d22009-12-08 11:16:11 +08006626 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08006627}
6628
Tom Zanussid71bd342018-01-15 20:52:07 -06006629int tracing_set_clock(struct trace_array *tr, const char *clockstr)
Zhaolei5079f322009-08-25 16:12:56 +08006630{
Zhaolei5079f322009-08-25 16:12:56 +08006631 int i;
6632
Zhaolei5079f322009-08-25 16:12:56 +08006633 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
6634 if (strcmp(trace_clocks[i].name, clockstr) == 0)
6635 break;
6636 }
6637 if (i == ARRAY_SIZE(trace_clocks))
6638 return -EINVAL;
6639
Zhaolei5079f322009-08-25 16:12:56 +08006640 mutex_lock(&trace_types_lock);
6641
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006642 tr->clock_id = i;
6643
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006644 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
Zhaolei5079f322009-08-25 16:12:56 +08006645
David Sharp60303ed2012-10-11 16:27:52 -07006646 /*
6647 * New clock may not be consistent with the previous clock.
6648 * Reset the buffer so that it doesn't have incomparable timestamps.
6649 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006650 tracing_reset_online_cpus(&tr->array_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006651
6652#ifdef CONFIG_TRACER_MAX_TRACE
Baohong Liu170b3b12017-09-05 16:57:19 -05006653 if (tr->max_buffer.buffer)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006654 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
Alexander Z Lam94571582013-08-02 18:36:16 -07006655 tracing_reset_online_cpus(&tr->max_buffer);
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05006656#endif
David Sharp60303ed2012-10-11 16:27:52 -07006657
Zhaolei5079f322009-08-25 16:12:56 +08006658 mutex_unlock(&trace_types_lock);
6659
Steven Rostedte1e232c2014-02-10 23:38:46 -05006660 return 0;
6661}
6662
6663static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
6664 size_t cnt, loff_t *fpos)
6665{
6666 struct seq_file *m = filp->private_data;
6667 struct trace_array *tr = m->private;
6668 char buf[64];
6669 const char *clockstr;
6670 int ret;
6671
6672 if (cnt >= sizeof(buf))
6673 return -EINVAL;
6674
Wang Xiaoqiang4afe6492016-04-18 15:23:29 +08006675 if (copy_from_user(buf, ubuf, cnt))
Steven Rostedte1e232c2014-02-10 23:38:46 -05006676 return -EFAULT;
6677
6678 buf[cnt] = 0;
6679
6680 clockstr = strstrip(buf);
6681
6682 ret = tracing_set_clock(tr, clockstr);
6683 if (ret)
6684 return ret;
6685
Zhaolei5079f322009-08-25 16:12:56 +08006686 *fpos += cnt;
6687
6688 return cnt;
6689}
6690
Li Zefan13f16d22009-12-08 11:16:11 +08006691static int tracing_clock_open(struct inode *inode, struct file *file)
6692{
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006693 struct trace_array *tr = inode->i_private;
6694 int ret;
6695
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006696 ret = tracing_check_open_get_tr(tr);
6697 if (ret)
6698 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006699
6700 ret = single_open(file, tracing_clock_show, inode->i_private);
6701 if (ret < 0)
6702 trace_array_put(tr);
6703
6704 return ret;
Li Zefan13f16d22009-12-08 11:16:11 +08006705}
6706
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006707static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
6708{
6709 struct trace_array *tr = m->private;
6710
6711 mutex_lock(&trace_types_lock);
6712
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006713 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006714 seq_puts(m, "delta [absolute]\n");
6715 else
6716 seq_puts(m, "[delta] absolute\n");
6717
6718 mutex_unlock(&trace_types_lock);
6719
6720 return 0;
6721}
6722
6723static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
6724{
6725 struct trace_array *tr = inode->i_private;
6726 int ret;
6727
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006728 ret = tracing_check_open_get_tr(tr);
6729 if (ret)
6730 return ret;
Tom Zanussi2c1ea602018-01-15 20:51:41 -06006731
6732 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
6733 if (ret < 0)
6734 trace_array_put(tr);
6735
6736 return ret;
6737}
6738
Tom Zanussi00b41452018-01-15 20:51:39 -06006739int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs)
6740{
6741 int ret = 0;
6742
6743 mutex_lock(&trace_types_lock);
6744
6745 if (abs && tr->time_stamp_abs_ref++)
6746 goto out;
6747
6748 if (!abs) {
6749 if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) {
6750 ret = -EINVAL;
6751 goto out;
6752 }
6753
6754 if (--tr->time_stamp_abs_ref)
6755 goto out;
6756 }
6757
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006758 ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs);
Tom Zanussi00b41452018-01-15 20:51:39 -06006759
6760#ifdef CONFIG_TRACER_MAX_TRACE
6761 if (tr->max_buffer.buffer)
6762 ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs);
6763#endif
6764 out:
6765 mutex_unlock(&trace_types_lock);
6766
6767 return ret;
6768}
6769
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006770struct ftrace_buffer_info {
6771 struct trace_iterator iter;
6772 void *spare;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04006773 unsigned int spare_cpu;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006774 unsigned int read;
6775};
6776
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006777#ifdef CONFIG_TRACER_SNAPSHOT
6778static int tracing_snapshot_open(struct inode *inode, struct file *file)
6779{
Oleg Nesterov6484c712013-07-23 17:26:10 +02006780 struct trace_array *tr = inode->i_private;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006781 struct trace_iterator *iter;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006782 struct seq_file *m;
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006783 int ret;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006784
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006785 ret = tracing_check_open_get_tr(tr);
6786 if (ret)
6787 return ret;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006788
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006789 if (file->f_mode & FMODE_READ) {
Oleg Nesterov6484c712013-07-23 17:26:10 +02006790 iter = __tracing_open(inode, file, true);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006791 if (IS_ERR(iter))
6792 ret = PTR_ERR(iter);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006793 } else {
6794 /* Writes still need the seq_file to hold the private data */
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006795 ret = -ENOMEM;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006796 m = kzalloc(sizeof(*m), GFP_KERNEL);
6797 if (!m)
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006798 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006799 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6800 if (!iter) {
6801 kfree(m);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006802 goto out;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006803 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006804 ret = 0;
6805
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006806 iter->tr = tr;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006807 iter->array_buffer = &tr->max_buffer;
Oleg Nesterov6484c712013-07-23 17:26:10 +02006808 iter->cpu_file = tracing_get_cpu(inode);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006809 m->private = iter;
6810 file->private_data = m;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006811 }
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07006812out:
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006813 if (ret < 0)
6814 trace_array_put(tr);
6815
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006816 return ret;
6817}
6818
6819static ssize_t
6820tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
6821 loff_t *ppos)
6822{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006823 struct seq_file *m = filp->private_data;
6824 struct trace_iterator *iter = m->private;
6825 struct trace_array *tr = iter->tr;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006826 unsigned long val;
6827 int ret;
6828
6829 ret = tracing_update_buffers();
6830 if (ret < 0)
6831 return ret;
6832
6833 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6834 if (ret)
6835 return ret;
6836
6837 mutex_lock(&trace_types_lock);
6838
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006839 if (tr->current_trace->use_max_tr) {
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006840 ret = -EBUSY;
6841 goto out;
6842 }
6843
Tom Zanussia35873a2019-02-13 17:42:45 -06006844 arch_spin_lock(&tr->max_lock);
6845 if (tr->cond_snapshot)
6846 ret = -EBUSY;
6847 arch_spin_unlock(&tr->max_lock);
6848 if (ret)
6849 goto out;
6850
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006851 switch (val) {
6852 case 0:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006853 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6854 ret = -EINVAL;
6855 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006856 }
Steven Rostedt (Red Hat)3209cff2013-03-12 11:17:54 -04006857 if (tr->allocated_snapshot)
6858 free_snapshot(tr);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006859 break;
6860 case 1:
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006861/* Only allow per-cpu swap if the ring buffer supports it */
6862#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
6863 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
6864 ret = -EINVAL;
6865 break;
6866 }
6867#endif
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006868 if (tr->allocated_snapshot)
6869 ret = resize_buffer_duplicate_size(&tr->max_buffer,
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006870 &tr->array_buffer, iter->cpu_file);
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006871 else
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04006872 ret = tracing_alloc_snapshot_instance(tr);
Eiichi Tsukata46cc0b42019-06-25 10:29:10 +09006873 if (ret < 0)
6874 break;
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006875 local_irq_disable();
6876 /* Now, we're going to swap */
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006877 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
Tom Zanussia35873a2019-02-13 17:42:45 -06006878 update_max_tr(tr, current, smp_processor_id(), NULL);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006879 else
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05006880 update_max_tr_single(tr, current, iter->cpu_file);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006881 local_irq_enable();
6882 break;
6883 default:
Steven Rostedt (Red Hat)45ad21c2013-03-05 18:25:02 -05006884 if (tr->allocated_snapshot) {
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006885 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6886 tracing_reset_online_cpus(&tr->max_buffer);
6887 else
Steven Rostedt (VMware)a47b53e2019-08-13 12:14:35 -04006888 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05006889 }
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006890 break;
6891 }
6892
6893 if (ret >= 0) {
6894 *ppos += cnt;
6895 ret = cnt;
6896 }
6897out:
6898 mutex_unlock(&trace_types_lock);
6899 return ret;
6900}
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006901
6902static int tracing_snapshot_release(struct inode *inode, struct file *file)
6903{
6904 struct seq_file *m = file->private_data;
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006905 int ret;
6906
6907 ret = tracing_release(inode, file);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006908
6909 if (file->f_mode & FMODE_READ)
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04006910 return ret;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04006911
6912 /* If write only, the seq_file is just a stub */
6913 if (m)
6914 kfree(m->private);
6915 kfree(m);
6916
6917 return 0;
6918}
6919
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006920static int tracing_buffers_open(struct inode *inode, struct file *filp);
6921static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
6922 size_t count, loff_t *ppos);
6923static int tracing_buffers_release(struct inode *inode, struct file *file);
6924static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
6925 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
6926
6927static int snapshot_raw_open(struct inode *inode, struct file *filp)
6928{
6929 struct ftrace_buffer_info *info;
6930 int ret;
6931
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04006932 /* The following checks for tracefs lockdown */
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006933 ret = tracing_buffers_open(inode, filp);
6934 if (ret < 0)
6935 return ret;
6936
6937 info = filp->private_data;
6938
6939 if (info->iter.trace->use_max_tr) {
6940 tracing_buffers_release(inode, filp);
6941 return -EBUSY;
6942 }
6943
6944 info->iter.snapshot = true;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006945 info->iter.array_buffer = &info->iter.tr->max_buffer;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05006946
6947 return ret;
6948}
6949
Hiraku Toyookadebdd572012-12-26 11:53:00 +09006950#endif /* CONFIG_TRACER_SNAPSHOT */
6951
6952
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04006953static const struct file_operations tracing_thresh_fops = {
6954 .open = tracing_open_generic,
6955 .read = tracing_thresh_read,
6956 .write = tracing_thresh_write,
6957 .llseek = generic_file_llseek,
6958};
6959
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04006960#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006961static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006962 .open = tracing_open_generic,
6963 .read = tracing_max_lat_read,
6964 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006965 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006966};
Chen Gange428abb2015-11-10 05:15:15 +08006967#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006968
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006969static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006970 .open = tracing_open_generic,
6971 .read = tracing_set_trace_read,
6972 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006973 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02006974};
6975
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006976static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006977 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02006978 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006979 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02006980 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02006981 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006982 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02006983};
6984
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006985static const struct file_operations tracing_entries_fops = {
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006986 .open = tracing_open_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006987 .read = tracing_entries_read,
6988 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02006989 .llseek = generic_file_llseek,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02006990 .release = tracing_release_generic_tr,
Steven Rostedta98a3c32008-05-12 21:20:59 +02006991};
6992
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006993static const struct file_operations tracing_total_entries_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006994 .open = tracing_open_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006995 .read = tracing_total_entries_read,
6996 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04006997 .release = tracing_release_generic_tr,
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07006998};
6999
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07007000static const struct file_operations tracing_free_buffer_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007001 .open = tracing_open_generic_tr,
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07007002 .write = tracing_free_buffer_write,
7003 .release = tracing_free_buffer_release,
7004};
7005
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007006static const struct file_operations tracing_mark_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007007 .open = tracing_open_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007008 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007009 .llseek = generic_file_llseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007010 .release = tracing_release_generic_tr,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03007011};
7012
Steven Rostedtfa32e852016-07-06 15:25:08 -04007013static const struct file_operations tracing_mark_raw_fops = {
7014 .open = tracing_open_generic_tr,
7015 .write = tracing_mark_raw_write,
7016 .llseek = generic_file_llseek,
7017 .release = tracing_release_generic_tr,
7018};
7019
Zhaolei5079f322009-08-25 16:12:56 +08007020static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08007021 .open = tracing_clock_open,
7022 .read = seq_read,
7023 .llseek = seq_lseek,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007024 .release = tracing_single_release_tr,
Zhaolei5079f322009-08-25 16:12:56 +08007025 .write = tracing_clock_write,
7026};
7027
Tom Zanussi2c1ea602018-01-15 20:51:41 -06007028static const struct file_operations trace_time_stamp_mode_fops = {
7029 .open = tracing_time_stamp_mode_open,
7030 .read = seq_read,
7031 .llseek = seq_lseek,
7032 .release = tracing_single_release_tr,
7033};
7034
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007035#ifdef CONFIG_TRACER_SNAPSHOT
7036static const struct file_operations snapshot_fops = {
7037 .open = tracing_snapshot_open,
7038 .read = seq_read,
7039 .write = tracing_snapshot_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05007040 .llseek = tracing_lseek,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007041 .release = tracing_snapshot_release,
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007042};
Hiraku Toyookadebdd572012-12-26 11:53:00 +09007043
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007044static const struct file_operations snapshot_raw_fops = {
7045 .open = snapshot_raw_open,
7046 .read = tracing_buffers_read,
7047 .release = tracing_buffers_release,
7048 .splice_read = tracing_buffers_splice_read,
7049 .llseek = no_llseek,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007050};
7051
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007052#endif /* CONFIG_TRACER_SNAPSHOT */
7053
Tom Zanussi8a062902019-03-31 18:48:15 -05007054#define TRACING_LOG_ERRS_MAX 8
7055#define TRACING_LOG_LOC_MAX 128
7056
7057#define CMD_PREFIX " Command: "
7058
7059struct err_info {
7060 const char **errs; /* ptr to loc-specific array of err strings */
7061 u8 type; /* index into errs -> specific err string */
7062 u8 pos; /* MAX_FILTER_STR_VAL = 256 */
7063 u64 ts;
7064};
7065
7066struct tracing_log_err {
7067 struct list_head list;
7068 struct err_info info;
7069 char loc[TRACING_LOG_LOC_MAX]; /* err location */
7070 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
7071};
7072
Tom Zanussi8a062902019-03-31 18:48:15 -05007073static DEFINE_MUTEX(tracing_err_log_lock);
7074
YueHaibingff585c52019-06-14 23:32:10 +08007075static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
Tom Zanussi8a062902019-03-31 18:48:15 -05007076{
7077 struct tracing_log_err *err;
7078
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007079 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007080 err = kzalloc(sizeof(*err), GFP_KERNEL);
7081 if (!err)
7082 err = ERR_PTR(-ENOMEM);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007083 tr->n_err_log_entries++;
Tom Zanussi8a062902019-03-31 18:48:15 -05007084
7085 return err;
7086 }
7087
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007088 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
Tom Zanussi8a062902019-03-31 18:48:15 -05007089 list_del(&err->list);
7090
7091 return err;
7092}
7093
7094/**
7095 * err_pos - find the position of a string within a command for error careting
7096 * @cmd: The tracing command that caused the error
7097 * @str: The string to position the caret at within @cmd
7098 *
7099 * Finds the position of the first occurence of @str within @cmd. The
7100 * return value can be passed to tracing_log_err() for caret placement
7101 * within @cmd.
7102 *
7103 * Returns the index within @cmd of the first occurence of @str or 0
7104 * if @str was not found.
7105 */
7106unsigned int err_pos(char *cmd, const char *str)
7107{
7108 char *found;
7109
7110 if (WARN_ON(!strlen(cmd)))
7111 return 0;
7112
7113 found = strstr(cmd, str);
7114 if (found)
7115 return found - cmd;
7116
7117 return 0;
7118}
7119
7120/**
7121 * tracing_log_err - write an error to the tracing error log
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007122 * @tr: The associated trace array for the error (NULL for top level array)
Tom Zanussi8a062902019-03-31 18:48:15 -05007123 * @loc: A string describing where the error occurred
7124 * @cmd: The tracing command that caused the error
7125 * @errs: The array of loc-specific static error strings
7126 * @type: The index into errs[], which produces the specific static err string
7127 * @pos: The position the caret should be placed in the cmd
7128 *
7129 * Writes an error into tracing/error_log of the form:
7130 *
7131 * <loc>: error: <text>
7132 * Command: <cmd>
7133 * ^
7134 *
7135 * tracing/error_log is a small log file containing the last
7136 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7137 * unless there has been a tracing error, and the error log can be
7138 * cleared and have its memory freed by writing the empty string in
7139 * truncation mode to it i.e. echo > tracing/error_log.
7140 *
7141 * NOTE: the @errs array along with the @type param are used to
7142 * produce a static error string - this string is not copied and saved
7143 * when the error is logged - only a pointer to it is saved. See
7144 * existing callers for examples of how static strings are typically
7145 * defined for use with tracing_log_err().
7146 */
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007147void tracing_log_err(struct trace_array *tr,
7148 const char *loc, const char *cmd,
Tom Zanussi8a062902019-03-31 18:48:15 -05007149 const char **errs, u8 type, u8 pos)
7150{
7151 struct tracing_log_err *err;
7152
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007153 if (!tr)
7154 tr = &global_trace;
7155
Tom Zanussi8a062902019-03-31 18:48:15 -05007156 mutex_lock(&tracing_err_log_lock);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007157 err = get_tracing_log_err(tr);
Tom Zanussi8a062902019-03-31 18:48:15 -05007158 if (PTR_ERR(err) == -ENOMEM) {
7159 mutex_unlock(&tracing_err_log_lock);
7160 return;
7161 }
7162
7163 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7164 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7165
7166 err->info.errs = errs;
7167 err->info.type = type;
7168 err->info.pos = pos;
7169 err->info.ts = local_clock();
7170
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007171 list_add_tail(&err->list, &tr->err_log);
Tom Zanussi8a062902019-03-31 18:48:15 -05007172 mutex_unlock(&tracing_err_log_lock);
7173}
7174
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007175static void clear_tracing_err_log(struct trace_array *tr)
Tom Zanussi8a062902019-03-31 18:48:15 -05007176{
7177 struct tracing_log_err *err, *next;
7178
7179 mutex_lock(&tracing_err_log_lock);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007180 list_for_each_entry_safe(err, next, &tr->err_log, list) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007181 list_del(&err->list);
7182 kfree(err);
7183 }
7184
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007185 tr->n_err_log_entries = 0;
Tom Zanussi8a062902019-03-31 18:48:15 -05007186 mutex_unlock(&tracing_err_log_lock);
7187}
7188
7189static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7190{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007191 struct trace_array *tr = m->private;
7192
Tom Zanussi8a062902019-03-31 18:48:15 -05007193 mutex_lock(&tracing_err_log_lock);
7194
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007195 return seq_list_start(&tr->err_log, *pos);
Tom Zanussi8a062902019-03-31 18:48:15 -05007196}
7197
7198static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7199{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007200 struct trace_array *tr = m->private;
7201
7202 return seq_list_next(v, &tr->err_log, pos);
Tom Zanussi8a062902019-03-31 18:48:15 -05007203}
7204
7205static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7206{
7207 mutex_unlock(&tracing_err_log_lock);
7208}
7209
7210static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7211{
7212 u8 i;
7213
7214 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7215 seq_putc(m, ' ');
7216 for (i = 0; i < pos; i++)
7217 seq_putc(m, ' ');
7218 seq_puts(m, "^\n");
7219}
7220
7221static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7222{
7223 struct tracing_log_err *err = v;
7224
7225 if (err) {
7226 const char *err_text = err->info.errs[err->info.type];
7227 u64 sec = err->info.ts;
7228 u32 nsec;
7229
7230 nsec = do_div(sec, NSEC_PER_SEC);
7231 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7232 err->loc, err_text);
7233 seq_printf(m, "%s", err->cmd);
7234 tracing_err_log_show_pos(m, err->info.pos);
7235 }
7236
7237 return 0;
7238}
7239
7240static const struct seq_operations tracing_err_log_seq_ops = {
7241 .start = tracing_err_log_seq_start,
7242 .next = tracing_err_log_seq_next,
7243 .stop = tracing_err_log_seq_stop,
7244 .show = tracing_err_log_seq_show
7245};
7246
7247static int tracing_err_log_open(struct inode *inode, struct file *file)
7248{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007249 struct trace_array *tr = inode->i_private;
Tom Zanussi8a062902019-03-31 18:48:15 -05007250 int ret = 0;
7251
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007252 ret = tracing_check_open_get_tr(tr);
7253 if (ret)
7254 return ret;
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007255
Tom Zanussi8a062902019-03-31 18:48:15 -05007256 /* If this file was opened for write, then erase contents */
7257 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007258 clear_tracing_err_log(tr);
Tom Zanussi8a062902019-03-31 18:48:15 -05007259
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007260 if (file->f_mode & FMODE_READ) {
Tom Zanussi8a062902019-03-31 18:48:15 -05007261 ret = seq_open(file, &tracing_err_log_seq_ops);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04007262 if (!ret) {
7263 struct seq_file *m = file->private_data;
7264 m->private = tr;
7265 } else {
7266 trace_array_put(tr);
7267 }
7268 }
Tom Zanussi8a062902019-03-31 18:48:15 -05007269 return ret;
7270}
7271
7272static ssize_t tracing_err_log_write(struct file *file,
7273 const char __user *buffer,
7274 size_t count, loff_t *ppos)
7275{
7276 return count;
7277}
7278
Takeshi Misawad122ed62019-06-28 19:56:40 +09007279static int tracing_err_log_release(struct inode *inode, struct file *file)
7280{
7281 struct trace_array *tr = inode->i_private;
7282
7283 trace_array_put(tr);
7284
7285 if (file->f_mode & FMODE_READ)
7286 seq_release(inode, file);
7287
7288 return 0;
7289}
7290
Tom Zanussi8a062902019-03-31 18:48:15 -05007291static const struct file_operations tracing_err_log_fops = {
7292 .open = tracing_err_log_open,
7293 .write = tracing_err_log_write,
7294 .read = seq_read,
7295 .llseek = seq_lseek,
Takeshi Misawad122ed62019-06-28 19:56:40 +09007296 .release = tracing_err_log_release,
Tom Zanussi8a062902019-03-31 18:48:15 -05007297};
7298
Steven Rostedt2cadf912008-12-01 22:20:19 -05007299static int tracing_buffers_open(struct inode *inode, struct file *filp)
7300{
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007301 struct trace_array *tr = inode->i_private;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007302 struct ftrace_buffer_info *info;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007303 int ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007304
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007305 ret = tracing_check_open_get_tr(tr);
7306 if (ret)
7307 return ret;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007308
Steven Rostedt2cadf912008-12-01 22:20:19 -05007309 info = kzalloc(sizeof(*info), GFP_KERNEL);
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007310 if (!info) {
7311 trace_array_put(tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007312 return -ENOMEM;
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007313 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007314
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007315 mutex_lock(&trace_types_lock);
7316
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007317 info->iter.tr = tr;
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007318 info->iter.cpu_file = tracing_get_cpu(inode);
Steven Rostedtb6273442013-02-28 13:44:11 -05007319 info->iter.trace = tr->current_trace;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007320 info->iter.array_buffer = &tr->array_buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007321 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007322 /* Force reading ring buffer for first read */
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007323 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007324
7325 filp->private_data = info;
7326
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007327 tr->current_trace->ref++;
7328
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007329 mutex_unlock(&trace_types_lock);
7330
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04007331 ret = nonseekable_open(inode, filp);
7332 if (ret < 0)
7333 trace_array_put(tr);
7334
7335 return ret;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007336}
7337
Al Viro9dd95742017-07-03 00:42:43 -04007338static __poll_t
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007339tracing_buffers_poll(struct file *filp, poll_table *poll_table)
7340{
7341 struct ftrace_buffer_info *info = filp->private_data;
7342 struct trace_iterator *iter = &info->iter;
7343
7344 return trace_poll(iter, filp, poll_table);
7345}
7346
Steven Rostedt2cadf912008-12-01 22:20:19 -05007347static ssize_t
7348tracing_buffers_read(struct file *filp, char __user *ubuf,
7349 size_t count, loff_t *ppos)
7350{
7351 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007352 struct trace_iterator *iter = &info->iter;
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007353 ssize_t ret = 0;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007354 ssize_t size;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007355
Steven Rostedt2dc5d122009-03-04 19:10:05 -05007356 if (!count)
7357 return 0;
7358
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007359#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007360 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7361 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007362#endif
7363
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007364 if (!info->spare) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007365 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007366 iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007367 if (IS_ERR(info->spare)) {
7368 ret = PTR_ERR(info->spare);
7369 info->spare = NULL;
7370 } else {
7371 info->spare_cpu = iter->cpu_file;
7372 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007373 }
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007374 if (!info->spare)
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007375 return ret;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007376
Steven Rostedt2cadf912008-12-01 22:20:19 -05007377 /* Do we have previous read data to read? */
7378 if (info->read < PAGE_SIZE)
7379 goto read;
7380
Steven Rostedtb6273442013-02-28 13:44:11 -05007381 again:
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007382 trace_access_lock(iter->cpu_file);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007383 ret = ring_buffer_read_page(iter->array_buffer->buffer,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007384 &info->spare,
7385 count,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007386 iter->cpu_file, 0);
7387 trace_access_unlock(iter->cpu_file);
Steven Rostedtb6273442013-02-28 13:44:11 -05007388
7389 if (ret < 0) {
7390 if (trace_empty(iter)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007391 if ((filp->f_flags & O_NONBLOCK))
7392 return -EAGAIN;
7393
Steven Rostedt (VMware)2c2b0a72018-11-29 20:32:26 -05007394 ret = wait_on_pipe(iter, 0);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007395 if (ret)
7396 return ret;
7397
Steven Rostedtb6273442013-02-28 13:44:11 -05007398 goto again;
7399 }
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007400 return 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05007401 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007402
Steven Rostedt436fc282011-10-14 10:44:25 -04007403 info->read = 0;
Steven Rostedtb6273442013-02-28 13:44:11 -05007404 read:
Steven Rostedt2cadf912008-12-01 22:20:19 -05007405 size = PAGE_SIZE - info->read;
7406 if (size > count)
7407 size = count;
7408
7409 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007410 if (ret == size)
7411 return -EFAULT;
7412
Steven Rostedt2dc5d122009-03-04 19:10:05 -05007413 size -= ret;
7414
Steven Rostedt2cadf912008-12-01 22:20:19 -05007415 *ppos += size;
7416 info->read += size;
7417
7418 return size;
7419}
7420
7421static int tracing_buffers_release(struct inode *inode, struct file *file)
7422{
7423 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007424 struct trace_iterator *iter = &info->iter;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007425
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007426 mutex_lock(&trace_types_lock);
7427
Steven Rostedt (Red Hat)cf6ab6d2014-12-15 20:13:31 -05007428 iter->tr->current_trace->ref--;
7429
Steven Rostedt (Red Hat)ff451962013-07-01 22:50:29 -04007430 __trace_array_put(iter->tr);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007431
Lai Jiangshanddd538f2009-04-02 15:16:59 +08007432 if (info->spare)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007433 ring_buffer_free_read_page(iter->array_buffer->buffer,
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007434 info->spare_cpu, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007435 kfree(info);
7436
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05007437 mutex_unlock(&trace_types_lock);
7438
Steven Rostedt2cadf912008-12-01 22:20:19 -05007439 return 0;
7440}
7441
7442struct buffer_ref {
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05007443 struct trace_buffer *buffer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007444 void *page;
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007445 int cpu;
Jann Hornb9872222019-04-04 23:59:25 +02007446 refcount_t refcount;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007447};
7448
Jann Hornb9872222019-04-04 23:59:25 +02007449static void buffer_ref_release(struct buffer_ref *ref)
7450{
7451 if (!refcount_dec_and_test(&ref->refcount))
7452 return;
7453 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
7454 kfree(ref);
7455}
7456
Steven Rostedt2cadf912008-12-01 22:20:19 -05007457static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
7458 struct pipe_buffer *buf)
7459{
7460 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7461
Jann Hornb9872222019-04-04 23:59:25 +02007462 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007463 buf->private = 0;
7464}
7465
Matthew Wilcox15fab632019-04-05 14:02:10 -07007466static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007467 struct pipe_buffer *buf)
7468{
7469 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
7470
Linus Torvaldse9e1a2e2019-04-26 11:09:55 -07007471 if (refcount_read(&ref->refcount) > INT_MAX/2)
Matthew Wilcox15fab632019-04-05 14:02:10 -07007472 return false;
7473
Jann Hornb9872222019-04-04 23:59:25 +02007474 refcount_inc(&ref->refcount);
Matthew Wilcox15fab632019-04-05 14:02:10 -07007475 return true;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007476}
7477
7478/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08007479static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007480 .confirm = generic_pipe_buf_confirm,
7481 .release = buffer_pipe_buf_release,
Jann Hornb9872222019-04-04 23:59:25 +02007482 .steal = generic_pipe_buf_nosteal,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007483 .get = buffer_pipe_buf_get,
7484};
7485
7486/*
7487 * Callback from splice_to_pipe(), if we need to release some pages
7488 * at the end of the spd in case we error'ed out in filling the pipe.
7489 */
7490static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
7491{
7492 struct buffer_ref *ref =
7493 (struct buffer_ref *)spd->partial[i].private;
7494
Jann Hornb9872222019-04-04 23:59:25 +02007495 buffer_ref_release(ref);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007496 spd->partial[i].private = 0;
7497}
7498
7499static ssize_t
7500tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7501 struct pipe_inode_info *pipe, size_t len,
7502 unsigned int flags)
7503{
7504 struct ftrace_buffer_info *info = file->private_data;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007505 struct trace_iterator *iter = &info->iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02007506 struct partial_page partial_def[PIPE_DEF_BUFFERS];
7507 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05007508 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02007509 .pages = pages_def,
7510 .partial = partial_def,
Eric Dumazet047fe362012-06-12 15:24:40 +02007511 .nr_pages_max = PIPE_DEF_BUFFERS,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007512 .ops = &buffer_pipe_buf_ops,
7513 .spd_release = buffer_spd_release,
7514 };
7515 struct buffer_ref *ref;
Steven Rostedt (VMware)6b7e6332017-12-22 20:38:57 -05007516 int entries, i;
Rabin Vincent07906da2014-11-06 22:26:07 +01007517 ssize_t ret = 0;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007518
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007519#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007520 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
7521 return -EBUSY;
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007522#endif
7523
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007524 if (*ppos & (PAGE_SIZE - 1))
7525 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007526
7527 if (len & (PAGE_SIZE - 1)) {
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007528 if (len < PAGE_SIZE)
7529 return -EINVAL;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007530 len &= PAGE_MASK;
7531 }
7532
Al Viro1ae22932016-09-17 18:31:46 -04007533 if (splice_grow_spd(pipe, &spd))
7534 return -ENOMEM;
7535
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007536 again:
7537 trace_access_lock(iter->cpu_file);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007538 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Steven Rostedt93459c62009-04-29 00:23:13 -04007539
Al Viroa786c062014-04-11 12:01:03 -04007540 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05007541 struct page *page;
7542 int r;
7543
7544 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
Rabin Vincent07906da2014-11-06 22:26:07 +01007545 if (!ref) {
7546 ret = -ENOMEM;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007547 break;
Rabin Vincent07906da2014-11-06 22:26:07 +01007548 }
Steven Rostedt2cadf912008-12-01 22:20:19 -05007549
Jann Hornb9872222019-04-04 23:59:25 +02007550 refcount_set(&ref->refcount, 1);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007551 ref->buffer = iter->array_buffer->buffer;
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007552 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
Steven Rostedt (VMware)a7e52ad2017-08-02 14:20:54 -04007553 if (IS_ERR(ref->page)) {
7554 ret = PTR_ERR(ref->page);
7555 ref->page = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007556 kfree(ref);
7557 break;
7558 }
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007559 ref->cpu = iter->cpu_file;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007560
7561 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007562 len, iter->cpu_file, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007563 if (r < 0) {
Steven Rostedt (VMware)73a757e2017-05-01 09:35:09 -04007564 ring_buffer_free_read_page(ref->buffer, ref->cpu,
7565 ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007566 kfree(ref);
7567 break;
7568 }
7569
Steven Rostedt2cadf912008-12-01 22:20:19 -05007570 page = virt_to_page(ref->page);
7571
7572 spd.pages[i] = page;
7573 spd.partial[i].len = PAGE_SIZE;
7574 spd.partial[i].offset = 0;
7575 spd.partial[i].private = (unsigned long)ref;
7576 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08007577 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04007578
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007579 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007580 }
7581
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007582 trace_access_unlock(iter->cpu_file);
Steven Rostedt2cadf912008-12-01 22:20:19 -05007583 spd.nr_pages = i;
7584
7585 /* did we read anything? */
7586 if (!spd.nr_pages) {
Rabin Vincent07906da2014-11-06 22:26:07 +01007587 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007588 goto out;
Rabin Vincent07906da2014-11-06 22:26:07 +01007589
Al Viro1ae22932016-09-17 18:31:46 -04007590 ret = -EAGAIN;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007591 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
Al Viro1ae22932016-09-17 18:31:46 -04007592 goto out;
Steven Rostedt (Red Hat)d716ff72014-12-15 22:31:07 -05007593
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05007594 ret = wait_on_pipe(iter, iter->tr->buffer_percent);
Steven Rostedt (Red Hat)8b8b3682014-06-10 09:46:00 -04007595 if (ret)
Al Viro1ae22932016-09-17 18:31:46 -04007596 goto out;
Rabin Vincente30f53a2014-11-10 19:46:34 +01007597
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007598 goto again;
Steven Rostedt2cadf912008-12-01 22:20:19 -05007599 }
7600
7601 ret = splice_to_pipe(pipe, &spd);
Al Viro1ae22932016-09-17 18:31:46 -04007602out:
Eric Dumazet047fe362012-06-12 15:24:40 +02007603 splice_shrink_spd(&spd);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007604
Steven Rostedt2cadf912008-12-01 22:20:19 -05007605 return ret;
7606}
7607
7608static const struct file_operations tracing_buffers_fops = {
7609 .open = tracing_buffers_open,
7610 .read = tracing_buffers_read,
Steven Rostedtcc60cdc2013-02-28 09:17:16 -05007611 .poll = tracing_buffers_poll,
Steven Rostedt2cadf912008-12-01 22:20:19 -05007612 .release = tracing_buffers_release,
7613 .splice_read = tracing_buffers_splice_read,
7614 .llseek = no_llseek,
7615};
7616
Steven Rostedtc8d77182009-04-29 18:03:45 -04007617static ssize_t
7618tracing_stats_read(struct file *filp, char __user *ubuf,
7619 size_t count, loff_t *ppos)
7620{
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007621 struct inode *inode = file_inode(filp);
7622 struct trace_array *tr = inode->i_private;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007623 struct array_buffer *trace_buf = &tr->array_buffer;
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007624 int cpu = tracing_get_cpu(inode);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007625 struct trace_seq *s;
7626 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007627 unsigned long long t;
7628 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007629
Li Zefane4f2d102009-06-15 10:57:28 +08007630 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007631 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01007632 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04007633
7634 trace_seq_init(s);
7635
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007636 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007637 trace_seq_printf(s, "entries: %ld\n", cnt);
7638
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007639 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007640 trace_seq_printf(s, "overrun: %ld\n", cnt);
7641
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007642 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007643 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
7644
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007645 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007646 trace_seq_printf(s, "bytes: %ld\n", cnt);
7647
Yoshihiro YUNOMAE58e8eed2013-04-23 10:32:39 +09007648 if (trace_clocks[tr->clock_id].in_ns) {
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007649 /* local or global for trace_clock */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007650 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007651 usec_rem = do_div(t, USEC_PER_SEC);
7652 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
7653 t, usec_rem);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007654
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007655 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007656 usec_rem = do_div(t, USEC_PER_SEC);
7657 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
7658 } else {
7659 /* counter or tsc mode for trace_clock */
7660 trace_seq_printf(s, "oldest event ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007661 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007662
7663 trace_seq_printf(s, "now ts: %llu\n",
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007664 ring_buffer_time_stamp(trace_buf->buffer, cpu));
Yoshihiro YUNOMAE11043d8b2012-11-13 12:18:23 -08007665 }
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07007666
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007667 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
Slava Pestov884bfe82011-07-15 14:23:58 -07007668 trace_seq_printf(s, "dropped events: %ld\n", cnt);
7669
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05007670 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05007671 trace_seq_printf(s, "read events: %ld\n", cnt);
7672
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05007673 count = simple_read_from_buffer(ubuf, count, ppos,
7674 s->buffer, trace_seq_used(s));
Steven Rostedtc8d77182009-04-29 18:03:45 -04007675
7676 kfree(s);
7677
7678 return count;
7679}
7680
7681static const struct file_operations tracing_stats_fops = {
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007682 .open = tracing_open_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007683 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007684 .llseek = generic_file_llseek,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007685 .release = tracing_release_generic_tr,
Steven Rostedtc8d77182009-04-29 18:03:45 -04007686};
7687
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007688#ifdef CONFIG_DYNAMIC_FTRACE
7689
7690static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007691tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007692 size_t cnt, loff_t *ppos)
7693{
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007694 ssize_t ret;
7695 char *buf;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007696 int r;
7697
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007698 /* 256 should be plenty to hold the amount needed */
7699 buf = kmalloc(256, GFP_KERNEL);
7700 if (!buf)
7701 return -ENOMEM;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007702
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007703 r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
7704 ftrace_update_tot_cnt,
7705 ftrace_number_of_pages,
7706 ftrace_number_of_groups);
7707
7708 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7709 kfree(buf);
7710 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007711}
7712
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007713static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02007714 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04007715 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02007716 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007717};
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007718#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007719
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007720#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
7721static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007722ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007723 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007724 void *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007725{
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007726 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007727}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007728
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007729static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04007730ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007731 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007732 void *data)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007733{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007734 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007735 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007736
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007737 if (mapper)
7738 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007739
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007740 if (count) {
7741
7742 if (*count <= 0)
7743 return;
7744
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007745 (*count)--;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007746 }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007747
Steven Rostedt (VMware)cab50372017-04-20 11:34:06 -04007748 tracing_snapshot_instance(tr);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007749}
7750
7751static int
7752ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
7753 struct ftrace_probe_ops *ops, void *data)
7754{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007755 struct ftrace_func_mapper *mapper = data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007756 long *count = NULL;
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007757
7758 seq_printf(m, "%ps:", (void *)ip);
7759
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007760 seq_puts(m, "snapshot");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007761
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007762 if (mapper)
7763 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
7764
7765 if (count)
7766 seq_printf(m, ":count=%ld\n", *count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007767 else
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007768 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007769
7770 return 0;
7771}
7772
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007773static int
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007774ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007775 unsigned long ip, void *init_data, void **data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007776{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007777 struct ftrace_func_mapper *mapper = *data;
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007778
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007779 if (!mapper) {
7780 mapper = allocate_ftrace_func_mapper();
7781 if (!mapper)
7782 return -ENOMEM;
7783 *data = mapper;
7784 }
7785
7786 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007787}
7788
7789static void
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04007790ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007791 unsigned long ip, void *data)
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007792{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04007793 struct ftrace_func_mapper *mapper = data;
7794
7795 if (!ip) {
7796 if (!mapper)
7797 return;
7798 free_ftrace_func_mapper(mapper, NULL);
7799 return;
7800 }
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007801
7802 ftrace_func_mapper_remove_ip(mapper, ip);
7803}
7804
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007805static struct ftrace_probe_ops snapshot_probe_ops = {
7806 .func = ftrace_snapshot,
7807 .print = ftrace_snapshot_print,
7808};
7809
7810static struct ftrace_probe_ops snapshot_count_probe_ops = {
7811 .func = ftrace_count_snapshot,
7812 .print = ftrace_snapshot_print,
Steven Rostedt (VMware)1a93f8b2017-04-03 22:09:43 -04007813 .init = ftrace_snapshot_init,
7814 .free = ftrace_snapshot_free,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007815};
7816
7817static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007818ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007819 char *glob, char *cmd, char *param, int enable)
7820{
7821 struct ftrace_probe_ops *ops;
7822 void *count = (void *)-1;
7823 char *number;
7824 int ret;
7825
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -04007826 if (!tr)
7827 return -ENODEV;
7828
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007829 /* hash funcs only work with set_ftrace_filter */
7830 if (!enable)
7831 return -EINVAL;
7832
7833 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
7834
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04007835 if (glob[0] == '!')
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04007836 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007837
7838 if (!param)
7839 goto out_reg;
7840
7841 number = strsep(&param, ":");
7842
7843 if (!strlen(number))
7844 goto out_reg;
7845
7846 /*
7847 * We use the callback data field (which is a pointer)
7848 * as our counter.
7849 */
7850 ret = kstrtoul(number, 0, (unsigned long *)&count);
7851 if (ret)
7852 return ret;
7853
7854 out_reg:
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04007855 ret = tracing_alloc_snapshot_instance(tr);
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007856 if (ret < 0)
7857 goto out;
7858
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007859 ret = register_ftrace_function_probe(glob, tr, ops, count);
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007860
Steven Rostedt (VMware)df62db52017-04-19 12:07:08 -04007861 out:
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007862 return ret < 0 ? ret : 0;
7863}
7864
7865static struct ftrace_func_command ftrace_snapshot_cmd = {
7866 .name = "snapshot",
7867 .func = ftrace_trace_snapshot_callback,
7868};
7869
Tom Zanussi38de93a2013-10-24 08:34:18 -05007870static __init int register_snapshot_cmd(void)
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007871{
7872 return register_ftrace_command(&ftrace_snapshot_cmd);
7873}
7874#else
Tom Zanussi38de93a2013-10-24 08:34:18 -05007875static inline __init int register_snapshot_cmd(void) { return 0; }
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04007876#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007877
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007878static struct dentry *tracing_get_dentry(struct trace_array *tr)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007879{
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007880 if (WARN_ON(!tr->dir))
7881 return ERR_PTR(-ENODEV);
7882
7883 /* Top directory uses NULL as the parent */
7884 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
7885 return NULL;
7886
7887 /* All sub buffers have a descriptor */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007888 return tr->dir;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02007889}
7890
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007891static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
7892{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007893 struct dentry *d_tracer;
7894
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007895 if (tr->percpu_dir)
7896 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007897
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05007898 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05007899 if (IS_ERR(d_tracer))
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007900 return NULL;
7901
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007902 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007903
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007904 WARN_ONCE(!tr->percpu_dir,
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007905 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007906
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007907 return tr->percpu_dir;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007908}
7909
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007910static struct dentry *
7911trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
7912 void *data, long cpu, const struct file_operations *fops)
7913{
7914 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
7915
7916 if (ret) /* See tracing_get_cpu() */
David Howells7682c912015-03-17 22:26:16 +00007917 d_inode(ret)->i_cdev = (void *)(cpu + 1);
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007918 return ret;
7919}
7920
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007921static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007922tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007923{
Steven Rostedt2b6080f2012-05-11 13:29:49 -04007924 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007925 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04007926 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007927
Namhyung Kim0a3d7ce2012-04-23 10:11:57 +09007928 if (!d_percpu)
7929 return;
7930
Steven Rostedtdd49a382010-10-20 21:51:26 -04007931 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007932 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007933 if (!d_cpu) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07007934 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007935 return;
7936 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007937
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01007938 /* per cpu trace_pipe */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007939 trace_create_cpu_file("trace_pipe", 0444, d_cpu,
Oleg Nesterov15544202013-07-23 17:25:57 +02007940 tr, cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007941
7942 /* per cpu trace */
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007943 trace_create_cpu_file("trace", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007944 tr, cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04007945
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007946 trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007947 tr, cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04007948
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007949 trace_create_cpu_file("stats", 0444, d_cpu,
Oleg Nesterov4d3435b2013-07-23 17:26:03 +02007950 tr, cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08007951
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007952 trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02007953 tr, cpu, &tracing_entries_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007954
7955#ifdef CONFIG_TRACER_SNAPSHOT
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007956 trace_create_cpu_file("snapshot", 0644, d_cpu,
Oleg Nesterov6484c712013-07-23 17:26:10 +02007957 tr, cpu, &snapshot_fops);
Steven Rostedt (Red Hat)6de58e62013-03-05 16:18:16 -05007958
Oleg Nesterov649e9c702013-07-23 17:25:54 +02007959 trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
Oleg Nesterov46ef2be2013-07-23 17:26:00 +02007960 tr, cpu, &snapshot_raw_fops);
Steven Rostedt (Red Hat)f1affca2013-03-05 14:35:11 -05007961#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01007962}
7963
Steven Rostedt60a11772008-05-12 21:20:44 +02007964#ifdef CONFIG_FTRACE_SELFTEST
7965/* Let selftest have access to static functions in this file */
7966#include "trace_selftest.c"
7967#endif
7968
Steven Rostedt577b7852009-02-26 23:43:05 -05007969static ssize_t
7970trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
7971 loff_t *ppos)
7972{
7973 struct trace_option_dentry *topt = filp->private_data;
7974 char *buf;
7975
7976 if (topt->flags->val & topt->opt->bit)
7977 buf = "1\n";
7978 else
7979 buf = "0\n";
7980
7981 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
7982}
7983
7984static ssize_t
7985trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
7986 loff_t *ppos)
7987{
7988 struct trace_option_dentry *topt = filp->private_data;
7989 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05007990 int ret;
7991
Peter Huewe22fe9b52011-06-07 21:58:27 +02007992 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7993 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05007994 return ret;
7995
Li Zefan8d18eaa2009-12-08 11:17:06 +08007996 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05007997 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08007998
7999 if (!!(topt->flags->val & topt->opt->bit) != val) {
8000 mutex_lock(&trace_types_lock);
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05008001 ret = __set_tracer_option(topt->tr, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05008002 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08008003 mutex_unlock(&trace_types_lock);
8004 if (ret)
8005 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05008006 }
8007
8008 *ppos += cnt;
8009
8010 return cnt;
8011}
8012
8013
8014static const struct file_operations trace_options_fops = {
8015 .open = tracing_open_generic,
8016 .read = trace_options_read,
8017 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02008018 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05008019};
8020
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008021/*
8022 * In order to pass in both the trace_array descriptor as well as the index
8023 * to the flag that the trace option file represents, the trace_array
8024 * has a character array of trace_flags_index[], which holds the index
8025 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
8026 * The address of this character array is passed to the flag option file
8027 * read/write callbacks.
8028 *
8029 * In order to extract both the index and the trace_array descriptor,
8030 * get_tr_index() uses the following algorithm.
8031 *
8032 * idx = *ptr;
8033 *
8034 * As the pointer itself contains the address of the index (remember
8035 * index[1] == 1).
8036 *
8037 * Then to get the trace_array descriptor, by subtracting that index
8038 * from the ptr, we get to the start of the index itself.
8039 *
8040 * ptr - idx == &index[0]
8041 *
8042 * Then a simple container_of() from that pointer gets us to the
8043 * trace_array descriptor.
8044 */
8045static void get_tr_index(void *data, struct trace_array **ptr,
8046 unsigned int *pindex)
8047{
8048 *pindex = *(unsigned char *)data;
8049
8050 *ptr = container_of(data - *pindex, struct trace_array,
8051 trace_flags_index);
8052}
8053
Steven Rostedta8259072009-02-26 22:19:12 -05008054static ssize_t
8055trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
8056 loff_t *ppos)
8057{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008058 void *tr_index = filp->private_data;
8059 struct trace_array *tr;
8060 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05008061 char *buf;
8062
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008063 get_tr_index(tr_index, &tr, &index);
8064
8065 if (tr->trace_flags & (1 << index))
Steven Rostedta8259072009-02-26 22:19:12 -05008066 buf = "1\n";
8067 else
8068 buf = "0\n";
8069
8070 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8071}
8072
8073static ssize_t
8074trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
8075 loff_t *ppos)
8076{
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008077 void *tr_index = filp->private_data;
8078 struct trace_array *tr;
8079 unsigned int index;
Steven Rostedta8259072009-02-26 22:19:12 -05008080 unsigned long val;
8081 int ret;
8082
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008083 get_tr_index(tr_index, &tr, &index);
8084
Peter Huewe22fe9b52011-06-07 21:58:27 +02008085 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8086 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05008087 return ret;
8088
Zhaoleif2d84b62009-08-07 18:55:48 +08008089 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05008090 return -EINVAL;
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008091
Prateek Sood3a53acf2019-12-10 09:15:16 +00008092 mutex_lock(&event_mutex);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008093 mutex_lock(&trace_types_lock);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008094 ret = set_tracer_flag(tr, 1 << index, val);
Steven Rostedt (Red Hat)69d34da2013-03-14 13:50:56 -04008095 mutex_unlock(&trace_types_lock);
Prateek Sood3a53acf2019-12-10 09:15:16 +00008096 mutex_unlock(&event_mutex);
Steven Rostedta8259072009-02-26 22:19:12 -05008097
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -04008098 if (ret < 0)
8099 return ret;
8100
Steven Rostedta8259072009-02-26 22:19:12 -05008101 *ppos += cnt;
8102
8103 return cnt;
8104}
8105
Steven Rostedta8259072009-02-26 22:19:12 -05008106static const struct file_operations trace_options_core_fops = {
8107 .open = tracing_open_generic,
8108 .read = trace_options_core_read,
8109 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02008110 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05008111};
8112
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008113struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04008114 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008115 struct dentry *parent,
8116 void *data,
8117 const struct file_operations *fops)
8118{
8119 struct dentry *ret;
8120
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008121 ret = tracefs_create_file(name, mode, parent, data, fops);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008122 if (!ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -07008123 pr_warn("Could not create tracefs '%s' entry\n", name);
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008124
8125 return ret;
8126}
8127
8128
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008129static struct dentry *trace_options_init_dentry(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05008130{
8131 struct dentry *d_tracer;
Steven Rostedta8259072009-02-26 22:19:12 -05008132
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008133 if (tr->options)
8134 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05008135
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008136 d_tracer = tracing_get_dentry(tr);
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008137 if (IS_ERR(d_tracer))
Steven Rostedta8259072009-02-26 22:19:12 -05008138 return NULL;
8139
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008140 tr->options = tracefs_create_dir("options", d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008141 if (!tr->options) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07008142 pr_warn("Could not create tracefs directory 'options'\n");
Steven Rostedta8259072009-02-26 22:19:12 -05008143 return NULL;
8144 }
8145
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008146 return tr->options;
Steven Rostedta8259072009-02-26 22:19:12 -05008147}
8148
Steven Rostedt577b7852009-02-26 23:43:05 -05008149static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008150create_trace_option_file(struct trace_array *tr,
8151 struct trace_option_dentry *topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05008152 struct tracer_flags *flags,
8153 struct tracer_opt *opt)
8154{
8155 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05008156
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008157 t_options = trace_options_init_dentry(tr);
Steven Rostedt577b7852009-02-26 23:43:05 -05008158 if (!t_options)
8159 return;
8160
8161 topt->flags = flags;
8162 topt->opt = opt;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008163 topt->tr = tr;
Steven Rostedt577b7852009-02-26 23:43:05 -05008164
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008165 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05008166 &trace_options_fops);
8167
Steven Rostedt577b7852009-02-26 23:43:05 -05008168}
8169
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008170static void
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008171create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
Steven Rostedt577b7852009-02-26 23:43:05 -05008172{
8173 struct trace_option_dentry *topts;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008174 struct trace_options *tr_topts;
Steven Rostedt577b7852009-02-26 23:43:05 -05008175 struct tracer_flags *flags;
8176 struct tracer_opt *opts;
8177 int cnt;
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008178 int i;
Steven Rostedt577b7852009-02-26 23:43:05 -05008179
8180 if (!tracer)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008181 return;
Steven Rostedt577b7852009-02-26 23:43:05 -05008182
8183 flags = tracer->flags;
8184
8185 if (!flags || !flags->opts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008186 return;
8187
8188 /*
8189 * If this is an instance, only create flags for tracers
8190 * the instance may have.
8191 */
8192 if (!trace_ok_for_array(tracer, tr))
8193 return;
8194
8195 for (i = 0; i < tr->nr_topts; i++) {
Chunyu Hud39cdd22016-03-08 21:37:01 +08008196 /* Make sure there's no duplicate flags. */
8197 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008198 return;
8199 }
Steven Rostedt577b7852009-02-26 23:43:05 -05008200
8201 opts = flags->opts;
8202
8203 for (cnt = 0; opts[cnt].name; cnt++)
8204 ;
8205
Steven Rostedt0cfe8242009-02-27 10:51:10 -05008206 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05008207 if (!topts)
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008208 return;
8209
8210 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
8211 GFP_KERNEL);
8212 if (!tr_topts) {
8213 kfree(topts);
8214 return;
8215 }
8216
8217 tr->topts = tr_topts;
8218 tr->topts[tr->nr_topts].tracer = tracer;
8219 tr->topts[tr->nr_topts].topts = topts;
8220 tr->nr_topts++;
Steven Rostedt577b7852009-02-26 23:43:05 -05008221
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04008222 for (cnt = 0; opts[cnt].name; cnt++) {
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008223 create_trace_option_file(tr, &topts[cnt], flags,
Steven Rostedt577b7852009-02-26 23:43:05 -05008224 &opts[cnt]);
Steven Rostedt (Red Hat)41d9c0b2015-09-29 17:31:55 -04008225 WARN_ONCE(topts[cnt].entry == NULL,
8226 "Failed to create trace option: %s",
8227 opts[cnt].name);
8228 }
Steven Rostedt577b7852009-02-26 23:43:05 -05008229}
8230
Steven Rostedta8259072009-02-26 22:19:12 -05008231static struct dentry *
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008232create_trace_option_core_file(struct trace_array *tr,
8233 const char *option, long index)
Steven Rostedta8259072009-02-26 22:19:12 -05008234{
8235 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05008236
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008237 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05008238 if (!t_options)
8239 return NULL;
8240
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008241 return trace_create_file(option, 0644, t_options,
8242 (void *)&tr->trace_flags_index[index],
8243 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05008244}
8245
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008246static void create_trace_options_dir(struct trace_array *tr)
Steven Rostedta8259072009-02-26 22:19:12 -05008247{
8248 struct dentry *t_options;
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008249 bool top_level = tr == &global_trace;
Steven Rostedta8259072009-02-26 22:19:12 -05008250 int i;
8251
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008252 t_options = trace_options_init_dentry(tr);
Steven Rostedta8259072009-02-26 22:19:12 -05008253 if (!t_options)
8254 return;
8255
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008256 for (i = 0; trace_options[i]; i++) {
8257 if (top_level ||
8258 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
8259 create_trace_option_core_file(tr, trace_options[i], i);
8260 }
Steven Rostedta8259072009-02-26 22:19:12 -05008261}
8262
Steven Rostedt499e5472012-02-22 15:50:28 -05008263static ssize_t
8264rb_simple_read(struct file *filp, char __user *ubuf,
8265 size_t cnt, loff_t *ppos)
8266{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04008267 struct trace_array *tr = filp->private_data;
Steven Rostedt499e5472012-02-22 15:50:28 -05008268 char buf[64];
8269 int r;
8270
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008271 r = tracer_tracing_is_on(tr);
Steven Rostedt499e5472012-02-22 15:50:28 -05008272 r = sprintf(buf, "%d\n", r);
8273
8274 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8275}
8276
8277static ssize_t
8278rb_simple_write(struct file *filp, const char __user *ubuf,
8279 size_t cnt, loff_t *ppos)
8280{
Steven Rostedt348f0fc2012-04-16 15:41:28 -04008281 struct trace_array *tr = filp->private_data;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -05008282 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt499e5472012-02-22 15:50:28 -05008283 unsigned long val;
8284 int ret;
8285
8286 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8287 if (ret)
8288 return ret;
8289
8290 if (buffer) {
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008291 mutex_lock(&trace_types_lock);
Steven Rostedt (VMware)f1436412018-08-01 15:40:57 -04008292 if (!!val == tracer_tracing_is_on(tr)) {
8293 val = 0; /* do nothing */
8294 } else if (val) {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008295 tracer_tracing_on(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008296 if (tr->current_trace->start)
8297 tr->current_trace->start(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008298 } else {
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -04008299 tracer_tracing_off(tr);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008300 if (tr->current_trace->stop)
8301 tr->current_trace->stop(tr);
Steven Rostedt2df8f8a2013-01-11 16:14:10 -05008302 }
8303 mutex_unlock(&trace_types_lock);
Steven Rostedt499e5472012-02-22 15:50:28 -05008304 }
8305
8306 (*ppos)++;
8307
8308 return cnt;
8309}
8310
8311static const struct file_operations rb_simple_fops = {
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04008312 .open = tracing_open_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05008313 .read = rb_simple_read,
8314 .write = rb_simple_write,
Steven Rostedt (Red Hat)7b85af62013-07-01 23:34:22 -04008315 .release = tracing_release_generic_tr,
Steven Rostedt499e5472012-02-22 15:50:28 -05008316 .llseek = default_llseek,
8317};
8318
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008319static ssize_t
8320buffer_percent_read(struct file *filp, char __user *ubuf,
8321 size_t cnt, loff_t *ppos)
8322{
8323 struct trace_array *tr = filp->private_data;
8324 char buf[64];
8325 int r;
8326
8327 r = tr->buffer_percent;
8328 r = sprintf(buf, "%d\n", r);
8329
8330 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8331}
8332
8333static ssize_t
8334buffer_percent_write(struct file *filp, const char __user *ubuf,
8335 size_t cnt, loff_t *ppos)
8336{
8337 struct trace_array *tr = filp->private_data;
8338 unsigned long val;
8339 int ret;
8340
8341 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8342 if (ret)
8343 return ret;
8344
8345 if (val > 100)
8346 return -EINVAL;
8347
8348 if (!val)
8349 val = 1;
8350
8351 tr->buffer_percent = val;
8352
8353 (*ppos)++;
8354
8355 return cnt;
8356}
8357
8358static const struct file_operations buffer_percent_fops = {
8359 .open = tracing_open_generic_tr,
8360 .read = buffer_percent_read,
8361 .write = buffer_percent_write,
8362 .release = tracing_release_generic_tr,
8363 .llseek = default_llseek,
8364};
8365
YueHaibingff585c52019-06-14 23:32:10 +08008366static struct dentry *trace_instance_dir;
Steven Rostedt277ba042012-08-03 16:10:49 -04008367
8368static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008369init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
Steven Rostedt277ba042012-08-03 16:10:49 -04008370
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008371static int
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008372allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
Steven Rostedt277ba042012-08-03 16:10:49 -04008373{
8374 enum ring_buffer_flags rb_flags;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008375
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008376 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008377
Steven Rostedt (Red Hat)dced3412014-01-14 10:19:46 -05008378 buf->tr = tr;
8379
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008380 buf->buffer = ring_buffer_alloc(size, rb_flags);
8381 if (!buf->buffer)
8382 return -ENOMEM;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008383
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008384 buf->data = alloc_percpu(struct trace_array_cpu);
8385 if (!buf->data) {
8386 ring_buffer_free(buf->buffer);
Steven Rostedt (VMware)4397f042017-12-26 20:07:34 -05008387 buf->buffer = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008388 return -ENOMEM;
8389 }
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008390
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008391 /* Allocate the first page for all buffers */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008392 set_buffer_entries(&tr->array_buffer,
8393 ring_buffer_size(tr->array_buffer.buffer, 0));
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008394
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008395 return 0;
8396}
8397
8398static int allocate_trace_buffers(struct trace_array *tr, int size)
8399{
8400 int ret;
8401
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008402 ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008403 if (ret)
8404 return ret;
8405
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008406#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008407 ret = allocate_trace_buffer(tr, &tr->max_buffer,
8408 allocate_snapshot ? size : 1);
8409 if (WARN_ON(ret)) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008410 ring_buffer_free(tr->array_buffer.buffer);
8411 tr->array_buffer.buffer = NULL;
8412 free_percpu(tr->array_buffer.data);
8413 tr->array_buffer.data = NULL;
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008414 return -ENOMEM;
8415 }
8416 tr->allocated_snapshot = allocate_snapshot;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008417
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05008418 /*
8419 * Only the top level trace array gets its snapshot allocated
8420 * from the kernel command line.
8421 */
8422 allocate_snapshot = false;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008423#endif
8424 return 0;
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008425}
8426
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008427static void free_trace_buffer(struct array_buffer *buf)
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008428{
8429 if (buf->buffer) {
8430 ring_buffer_free(buf->buffer);
8431 buf->buffer = NULL;
8432 free_percpu(buf->data);
8433 buf->data = NULL;
8434 }
8435}
8436
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008437static void free_trace_buffers(struct trace_array *tr)
8438{
8439 if (!tr)
8440 return;
8441
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05008442 free_trace_buffer(&tr->array_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008443
8444#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt (Red Hat)f0b70cc2014-06-10 12:06:30 -04008445 free_trace_buffer(&tr->max_buffer);
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008446#endif
8447}
8448
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008449static void init_trace_flags_index(struct trace_array *tr)
8450{
8451 int i;
8452
8453 /* Used by the trace options files */
8454 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
8455 tr->trace_flags_index[i] = i;
8456}
8457
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008458static void __update_tracer_options(struct trace_array *tr)
8459{
8460 struct tracer *t;
8461
8462 for (t = trace_types; t; t = t->next)
8463 add_tracer_options(tr, t);
8464}
8465
8466static void update_tracer_options(struct trace_array *tr)
8467{
8468 mutex_lock(&trace_types_lock);
8469 __update_tracer_options(tr);
8470 mutex_unlock(&trace_types_lock);
8471}
8472
Divya Indi28879782019-11-20 11:08:38 -08008473static struct trace_array *trace_array_create(const char *name)
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008474{
Steven Rostedt277ba042012-08-03 16:10:49 -04008475 struct trace_array *tr;
8476 int ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04008477
Steven Rostedt277ba042012-08-03 16:10:49 -04008478 ret = -ENOMEM;
8479 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
8480 if (!tr)
Divya Indi28879782019-11-20 11:08:38 -08008481 return ERR_PTR(ret);
Steven Rostedt277ba042012-08-03 16:10:49 -04008482
8483 tr->name = kstrdup(name, GFP_KERNEL);
8484 if (!tr->name)
8485 goto out_free_tr;
8486
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008487 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
8488 goto out_free_tr;
8489
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008490 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04008491
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008492 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
8493
Steven Rostedt277ba042012-08-03 16:10:49 -04008494 raw_spin_lock_init(&tr->start_lock);
8495
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05008496 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
8497
Steven Rostedt277ba042012-08-03 16:10:49 -04008498 tr->current_trace = &nop_trace;
8499
8500 INIT_LIST_HEAD(&tr->systems);
8501 INIT_LIST_HEAD(&tr->events);
Tom Zanussi067fe032018-01-15 20:51:56 -06008502 INIT_LIST_HEAD(&tr->hist_vars);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04008503 INIT_LIST_HEAD(&tr->err_log);
Steven Rostedt277ba042012-08-03 16:10:49 -04008504
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05008505 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
Steven Rostedt277ba042012-08-03 16:10:49 -04008506 goto out_free_tr;
8507
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008508 tr->dir = tracefs_create_dir(name, trace_instance_dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008509 if (!tr->dir)
8510 goto out_free_tr;
8511
8512 ret = event_trace_add_tracer(tr->dir, tr);
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008513 if (ret) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008514 tracefs_remove_recursive(tr->dir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008515 goto out_free_tr;
Alexander Z Lam609e85a2013-07-10 17:34:34 -07008516 }
Steven Rostedt277ba042012-08-03 16:10:49 -04008517
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04008518 ftrace_init_trace_array(tr);
8519
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008520 init_tracer_tracefs(tr, tr->dir);
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04008521 init_trace_flags_index(tr);
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008522 __update_tracer_options(tr);
Steven Rostedt277ba042012-08-03 16:10:49 -04008523
8524 list_add(&tr->list, &ftrace_trace_arrays);
8525
Divya Indi28879782019-11-20 11:08:38 -08008526 tr->ref++;
8527
Steven Rostedt277ba042012-08-03 16:10:49 -04008528
Divya Indif45d1222019-03-20 11:28:51 -07008529 return tr;
Steven Rostedt277ba042012-08-03 16:10:49 -04008530
8531 out_free_tr:
Steven Rostedt (Red Hat)23aaa3c2014-06-06 00:01:46 -04008532 free_trace_buffers(tr);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008533 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt277ba042012-08-03 16:10:49 -04008534 kfree(tr->name);
8535 kfree(tr);
8536
Divya Indif45d1222019-03-20 11:28:51 -07008537 return ERR_PTR(ret);
8538}
Steven Rostedt277ba042012-08-03 16:10:49 -04008539
Divya Indif45d1222019-03-20 11:28:51 -07008540static int instance_mkdir(const char *name)
8541{
Divya Indi28879782019-11-20 11:08:38 -08008542 struct trace_array *tr;
8543 int ret;
8544
8545 mutex_lock(&event_mutex);
8546 mutex_lock(&trace_types_lock);
8547
8548 ret = -EEXIST;
8549 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8550 if (tr->name && strcmp(tr->name, name) == 0)
8551 goto out_unlock;
8552 }
8553
8554 tr = trace_array_create(name);
8555
8556 ret = PTR_ERR_OR_ZERO(tr);
8557
8558out_unlock:
8559 mutex_unlock(&trace_types_lock);
8560 mutex_unlock(&event_mutex);
8561 return ret;
Steven Rostedt277ba042012-08-03 16:10:49 -04008562}
8563
Divya Indi28879782019-11-20 11:08:38 -08008564/**
8565 * trace_array_get_by_name - Create/Lookup a trace array, given its name.
8566 * @name: The name of the trace array to be looked up/created.
8567 *
8568 * Returns pointer to trace array with given name.
8569 * NULL, if it cannot be created.
8570 *
8571 * NOTE: This function increments the reference counter associated with the
8572 * trace array returned. This makes sure it cannot be freed while in use.
8573 * Use trace_array_put() once the trace array is no longer needed.
8574 *
8575 */
8576struct trace_array *trace_array_get_by_name(const char *name)
8577{
8578 struct trace_array *tr;
8579
8580 mutex_lock(&event_mutex);
8581 mutex_lock(&trace_types_lock);
8582
8583 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8584 if (tr->name && strcmp(tr->name, name) == 0)
8585 goto out_unlock;
8586 }
8587
8588 tr = trace_array_create(name);
8589
8590 if (IS_ERR(tr))
8591 tr = NULL;
8592out_unlock:
8593 if (tr)
8594 tr->ref++;
8595
8596 mutex_unlock(&trace_types_lock);
8597 mutex_unlock(&event_mutex);
8598 return tr;
8599}
8600EXPORT_SYMBOL_GPL(trace_array_get_by_name);
8601
Divya Indif45d1222019-03-20 11:28:51 -07008602static int __remove_instance(struct trace_array *tr)
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008603{
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008604 int i;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008605
Divya Indi28879782019-11-20 11:08:38 -08008606 /* Reference counter for a newly created trace array = 1. */
8607 if (tr->ref > 1 || (tr->current_trace && tr->current_trace->ref))
Divya Indif45d1222019-03-20 11:28:51 -07008608 return -EBUSY;
Steven Rostedt (Red Hat)a695cb52013-03-06 15:27:24 -05008609
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008610 list_del(&tr->list);
8611
Steven Rostedt (Red Hat)20550622016-04-25 22:40:12 -04008612 /* Disable all the flags that were enabled coming in */
8613 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
8614 if ((1 << i) & ZEROED_TRACE_FLAGS)
8615 set_tracer_flag(tr, 1 << i, 0);
8616 }
8617
Steven Rostedt (Red Hat)6b450d22014-01-14 08:43:01 -05008618 tracing_set_nop(tr);
Naveen N. Raoa0e63692017-05-16 23:21:26 +05308619 clear_ftrace_function_probes(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008620 event_trace_del_tracer(tr);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09008621 ftrace_clear_pids(tr);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008622 ftrace_destroy_function_files(tr);
Jiaxing Wang681a4a22015-10-18 19:58:08 +08008623 tracefs_remove_recursive(tr->dir);
Steven Rostedt (Red Hat)a9fcaaa2014-06-06 23:17:28 -04008624 free_trace_buffers(tr);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008625
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008626 for (i = 0; i < tr->nr_topts; i++) {
8627 kfree(tr->topts[i].topts);
8628 }
8629 kfree(tr->topts);
8630
Chunyu Hudb9108e02017-07-20 18:36:09 +08008631 free_cpumask_var(tr->tracing_cpumask);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008632 kfree(tr->name);
8633 kfree(tr);
Divya Indif45d1222019-03-20 11:28:51 -07008634 tr = NULL;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008635
Divya Indif45d1222019-03-20 11:28:51 -07008636 return 0;
8637}
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008638
Divya Indie585e642019-08-14 10:55:24 -07008639int trace_array_destroy(struct trace_array *this_tr)
Divya Indif45d1222019-03-20 11:28:51 -07008640{
Divya Indie585e642019-08-14 10:55:24 -07008641 struct trace_array *tr;
Divya Indif45d1222019-03-20 11:28:51 -07008642 int ret;
8643
Divya Indie585e642019-08-14 10:55:24 -07008644 if (!this_tr)
Divya Indif45d1222019-03-20 11:28:51 -07008645 return -EINVAL;
8646
8647 mutex_lock(&event_mutex);
8648 mutex_lock(&trace_types_lock);
8649
Divya Indie585e642019-08-14 10:55:24 -07008650 ret = -ENODEV;
8651
8652 /* Making sure trace array exists before destroying it. */
8653 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8654 if (tr == this_tr) {
8655 ret = __remove_instance(tr);
8656 break;
8657 }
8658 }
Divya Indif45d1222019-03-20 11:28:51 -07008659
8660 mutex_unlock(&trace_types_lock);
8661 mutex_unlock(&event_mutex);
8662
8663 return ret;
8664}
8665EXPORT_SYMBOL_GPL(trace_array_destroy);
8666
8667static int instance_rmdir(const char *name)
8668{
8669 struct trace_array *tr;
8670 int ret;
8671
8672 mutex_lock(&event_mutex);
8673 mutex_lock(&trace_types_lock);
8674
8675 ret = -ENODEV;
8676 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8677 if (tr->name && strcmp(tr->name, name) == 0) {
8678 ret = __remove_instance(tr);
8679 break;
8680 }
8681 }
8682
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008683 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04008684 mutex_unlock(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04008685
8686 return ret;
8687}
8688
Steven Rostedt277ba042012-08-03 16:10:49 -04008689static __init void create_trace_instances(struct dentry *d_tracer)
8690{
Steven Rostedt (Red Hat)eae47352015-01-21 10:01:39 -05008691 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
8692 instance_mkdir,
8693 instance_rmdir);
Steven Rostedt277ba042012-08-03 16:10:49 -04008694 if (WARN_ON(!trace_instance_dir))
8695 return;
Steven Rostedt277ba042012-08-03 16:10:49 -04008696}
8697
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008698static void
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008699init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008700{
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008701 struct trace_event_file *file;
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008702 int cpu;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008703
Steven Rostedt (Red Hat)607e2ea2013-11-06 22:42:48 -05008704 trace_create_file("available_tracers", 0444, d_tracer,
8705 tr, &show_traces_fops);
8706
8707 trace_create_file("current_tracer", 0644, d_tracer,
8708 tr, &set_tracer_fops);
8709
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07008710 trace_create_file("tracing_cpumask", 0644, d_tracer,
8711 tr, &tracing_cpumask_fops);
8712
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008713 trace_create_file("trace_options", 0644, d_tracer,
8714 tr, &tracing_iter_fops);
8715
8716 trace_create_file("trace", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008717 tr, &tracing_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008718
8719 trace_create_file("trace_pipe", 0444, d_tracer,
Oleg Nesterov15544202013-07-23 17:25:57 +02008720 tr, &tracing_pipe_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008721
8722 trace_create_file("buffer_size_kb", 0644, d_tracer,
Oleg Nesterov0bc392e2013-07-23 17:26:06 +02008723 tr, &tracing_entries_fops);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008724
8725 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
8726 tr, &tracing_total_entries_fops);
8727
Wang YanQing238ae932013-05-26 16:52:01 +08008728 trace_create_file("free_buffer", 0200, d_tracer,
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008729 tr, &tracing_free_buffer_fops);
8730
8731 trace_create_file("trace_marker", 0220, d_tracer,
8732 tr, &tracing_mark_fops);
8733
Steven Rostedt (VMware)3dd80952018-05-09 14:17:48 -04008734 file = __find_event_file(tr, "ftrace", "print");
8735 if (file && file->dir)
8736 trace_create_file("trigger", 0644, file->dir, file,
8737 &event_trigger_fops);
8738 tr->trace_marker_file = file;
8739
Steven Rostedtfa32e852016-07-06 15:25:08 -04008740 trace_create_file("trace_marker_raw", 0220, d_tracer,
8741 tr, &tracing_mark_raw_fops);
8742
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008743 trace_create_file("trace_clock", 0644, d_tracer, tr,
8744 &trace_clock_fops);
8745
8746 trace_create_file("tracing_on", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008747 tr, &rb_simple_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008748
Tom Zanussi2c1ea602018-01-15 20:51:41 -06008749 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
8750 &trace_time_stamp_mode_fops);
8751
Steven Rostedt (VMware)a7b1d742018-11-29 22:36:47 -05008752 tr->buffer_percent = 50;
Steven Rostedt (VMware)03329f92018-11-29 21:38:42 -05008753
8754 trace_create_file("buffer_percent", 0444, d_tracer,
8755 tr, &buffer_percent_fops);
8756
Steven Rostedt (Red Hat)16270142015-09-30 12:30:06 -04008757 create_trace_options_dir(tr);
8758
Steven Rostedt (Red Hat)f971cc92016-09-07 12:45:09 -04008759#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +02008760 trace_create_maxlat_file(tr, d_tracer);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05008761#endif
8762
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05008763 if (ftrace_create_function_files(tr, d_tracer))
8764 WARN(1, "Could not allocate function filter files");
8765
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008766#ifdef CONFIG_TRACER_SNAPSHOT
8767 trace_create_file("snapshot", 0644, d_tracer,
Oleg Nesterov6484c712013-07-23 17:26:10 +02008768 tr, &snapshot_fops);
Steven Rostedt (Red Hat)ce9bae52013-03-05 21:23:55 -05008769#endif
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008770
Tom Zanussi8a062902019-03-31 18:48:15 -05008771 trace_create_file("error_log", 0644, d_tracer,
8772 tr, &tracing_err_log_fops);
8773
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008774 for_each_tracing_cpu(cpu)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008775 tracing_init_tracefs_percpu(tr, cpu);
Steven Rostedt (Red Hat)121aaee2013-03-05 21:52:25 -05008776
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04008777 ftrace_init_tracefs(tr, d_tracer);
Steven Rostedt2b6080f2012-05-11 13:29:49 -04008778}
8779
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008780static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008781{
8782 struct vfsmount *mnt;
8783 struct file_system_type *type;
8784
8785 /*
8786 * To maintain backward compatibility for tools that mount
8787 * debugfs to get to the tracing facility, tracefs is automatically
8788 * mounted to the debugfs/tracing directory.
8789 */
8790 type = get_fs_type("tracefs");
8791 if (!type)
8792 return NULL;
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13008793 mnt = vfs_submount(mntpt, type, "tracefs", NULL);
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008794 put_filesystem(type);
8795 if (IS_ERR(mnt))
8796 return NULL;
8797 mntget(mnt);
8798
8799 return mnt;
8800}
8801
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008802/**
8803 * tracing_init_dentry - initialize top level trace array
8804 *
8805 * This is called when creating files or directories in the tracing
8806 * directory. It is called via fs_initcall() by any of the boot up code
8807 * and expects to return the dentry of the top level tracing directory.
8808 */
8809struct dentry *tracing_init_dentry(void)
8810{
8811 struct trace_array *tr = &global_trace;
8812
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05008813 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11008814 pr_warn("Tracing disabled due to lockdown\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05008815 return ERR_PTR(-EPERM);
8816 }
8817
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008818 /* The top level trace array uses NULL as parent */
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008819 if (tr->dir)
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008820 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008821
Jiaxing Wang8b129192015-11-06 16:04:16 +08008822 if (WARN_ON(!tracefs_initialized()) ||
8823 (IS_ENABLED(CONFIG_DEBUG_FS) &&
8824 WARN_ON(!debugfs_initialized())))
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008825 return ERR_PTR(-ENODEV);
8826
Steven Rostedt (Red Hat)f76180b2015-01-20 15:48:46 -05008827 /*
8828 * As there may still be users that expect the tracing
8829 * files to exist in debugfs/tracing, we must automount
8830 * the tracefs file system there, so older tools still
8831 * work with the newer kerenl.
8832 */
8833 tr->dir = debugfs_create_automount("tracing", NULL,
8834 trace_automount, NULL);
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008835
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008836 return NULL;
Steven Rostedt (Red Hat)7eeafbc2015-01-26 21:00:48 -05008837}
8838
Jeremy Linton00f4b652017-05-31 16:56:43 -05008839extern struct trace_eval_map *__start_ftrace_eval_maps[];
8840extern struct trace_eval_map *__stop_ftrace_eval_maps[];
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008841
Jeremy Linton5f60b352017-05-31 16:56:47 -05008842static void __init trace_eval_init(void)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008843{
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008844 int len;
8845
Jeremy Linton02fd7f62017-05-31 16:56:42 -05008846 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008847 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008848}
8849
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008850#ifdef CONFIG_MODULES
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008851static void trace_module_add_evals(struct module *mod)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008852{
Jeremy Linton99be6472017-05-31 16:56:44 -05008853 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008854 return;
8855
8856 /*
8857 * Modules with bad taint do not have events created, do
8858 * not bother with enums either.
8859 */
8860 if (trace_module_has_bad_taint(mod))
8861 return;
8862
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008863 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008864}
8865
Jeremy Linton681bec02017-05-31 16:56:53 -05008866#ifdef CONFIG_TRACE_EVAL_MAP_FILE
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008867static void trace_module_remove_evals(struct module *mod)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008868{
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008869 union trace_eval_map_item *map;
8870 union trace_eval_map_item **last = &trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008871
Jeremy Linton99be6472017-05-31 16:56:44 -05008872 if (!mod->num_trace_evals)
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008873 return;
8874
Jeremy Linton1793ed92017-05-31 16:56:46 -05008875 mutex_lock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008876
Jeremy Linton23bf8cb2017-05-31 16:56:45 -05008877 map = trace_eval_maps;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008878
8879 while (map) {
8880 if (map->head.mod == mod)
8881 break;
Jeremy Linton5f60b352017-05-31 16:56:47 -05008882 map = trace_eval_jmp_to_tail(map);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008883 last = &map->tail.next;
8884 map = map->tail.next;
8885 }
8886 if (!map)
8887 goto out;
8888
Jeremy Linton5f60b352017-05-31 16:56:47 -05008889 *last = trace_eval_jmp_to_tail(map)->tail.next;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008890 kfree(map);
8891 out:
Jeremy Linton1793ed92017-05-31 16:56:46 -05008892 mutex_unlock(&trace_eval_mutex);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008893}
8894#else
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008895static inline void trace_module_remove_evals(struct module *mod) { }
Jeremy Linton681bec02017-05-31 16:56:53 -05008896#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008897
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008898static int trace_module_notify(struct notifier_block *self,
8899 unsigned long val, void *data)
8900{
8901 struct module *mod = data;
8902
8903 switch (val) {
8904 case MODULE_STATE_COMING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008905 trace_module_add_evals(mod);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008906 break;
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008907 case MODULE_STATE_GOING:
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008908 trace_module_remove_evals(mod);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008909 break;
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008910 }
8911
8912 return 0;
8913}
8914
8915static struct notifier_block trace_module_nb = {
8916 .notifier_call = trace_module_notify,
8917 .priority = 0,
8918};
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008919#endif /* CONFIG_MODULES */
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008920
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008921static __init int tracer_init_tracefs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008922{
8923 struct dentry *d_tracer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008924
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08008925 trace_access_lock_init();
8926
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008927 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05008928 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +09008929 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008930
Steven Rostedt (VMware)58b92542018-05-08 15:09:27 -04008931 event_trace_init();
8932
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05008933 init_tracer_tracefs(&global_trace, d_tracer);
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04008934 ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008935
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008936 trace_create_file("tracing_thresh", 0644, d_tracer,
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04008937 &global_trace, &tracing_thresh_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008938
Li Zefan339ae5d2009-04-17 10:34:30 +08008939 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008940 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02008941
Avadh Patel69abe6a2009-04-10 16:04:48 -04008942 trace_create_file("saved_cmdlines", 0444, d_tracer,
8943 NULL, &tracing_saved_cmdlines_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03008944
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09008945 trace_create_file("saved_cmdlines_size", 0644, d_tracer,
8946 NULL, &tracing_saved_cmdlines_size_fops);
8947
Michael Sartain99c621d2017-07-05 22:07:15 -06008948 trace_create_file("saved_tgids", 0444, d_tracer,
8949 NULL, &tracing_saved_tgids_fops);
8950
Jeremy Linton5f60b352017-05-31 16:56:47 -05008951 trace_eval_init();
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04008952
Jeremy Lintonf57a4142017-05-31 16:56:48 -05008953 trace_create_eval_file(d_tracer);
Steven Rostedt (Red Hat)98284132015-03-31 17:23:45 -04008954
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04008955#ifdef CONFIG_MODULES
8956 register_module_notifier(&trace_module_nb);
8957#endif
8958
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008959#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01008960 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04008961 NULL, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008962#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01008963
Steven Rostedt277ba042012-08-03 16:10:49 -04008964 create_trace_instances(d_tracer);
Hiraku Toyookadebdd572012-12-26 11:53:00 +09008965
Steven Rostedt (Red Hat)37aea982015-09-30 14:27:31 -04008966 update_tracer_options(&global_trace);
Steven Rostedt (Red Hat)09d23a12015-02-03 12:45:53 -05008967
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01008968 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02008969}
8970
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008971static int trace_panic_handler(struct notifier_block *this,
8972 unsigned long event, void *unused)
8973{
Steven Rostedt944ac422008-10-23 19:26:08 -04008974 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008975 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008976 return NOTIFY_OK;
8977}
8978
8979static struct notifier_block trace_panic_notifier = {
8980 .notifier_call = trace_panic_handler,
8981 .next = NULL,
8982 .priority = 150 /* priority: INT_MAX >= x >= 0 */
8983};
8984
8985static int trace_die_handler(struct notifier_block *self,
8986 unsigned long val,
8987 void *data)
8988{
8989 switch (val) {
8990 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04008991 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02008992 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04008993 break;
8994 default:
8995 break;
8996 }
8997 return NOTIFY_OK;
8998}
8999
9000static struct notifier_block trace_die_notifier = {
9001 .notifier_call = trace_die_handler,
9002 .priority = 200
9003};
9004
9005/*
9006 * printk is set to max of 1024, we really don't need it that big.
9007 * Nothing should be printing 1000 characters anyway.
9008 */
9009#define TRACE_MAX_PRINT 1000
9010
9011/*
9012 * Define here KERN_TRACE so that we have one place to modify
9013 * it if we decide to change what log level the ftrace dump
9014 * should be at.
9015 */
Steven Rostedt428aee12009-01-14 12:24:42 -05009016#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009017
Jason Wessel955b61e2010-08-05 09:22:23 -05009018void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009019trace_printk_seq(struct trace_seq *s)
9020{
9021 /* Probably should print a warning here. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04009022 if (s->seq.len >= TRACE_MAX_PRINT)
9023 s->seq.len = TRACE_MAX_PRINT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009024
Steven Rostedt (Red Hat)820b75f2014-11-19 10:56:41 -05009025 /*
9026 * More paranoid code. Although the buffer size is set to
9027 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
9028 * an extra layer of protection.
9029 */
9030 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
9031 s->seq.len = s->seq.size - 1;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009032
9033 /* should be zero ended, but we are paranoid. */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04009034 s->buffer[s->seq.len] = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009035
9036 printk(KERN_TRACE "%s", s->buffer);
9037
Steven Rostedtf9520752009-03-02 14:04:40 -05009038 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009039}
9040
Jason Wessel955b61e2010-08-05 09:22:23 -05009041void trace_init_global_iter(struct trace_iterator *iter)
9042{
9043 iter->tr = &global_trace;
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009044 iter->trace = iter->tr->current_trace;
Steven Rostedtae3b5092013-01-23 15:22:59 -05009045 iter->cpu_file = RING_BUFFER_ALL_CPUS;
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009046 iter->array_buffer = &global_trace.array_buffer;
Cody P Schaferb2f974d2013-10-23 11:49:57 -07009047
9048 if (iter->trace && iter->trace->open)
9049 iter->trace->open(iter);
9050
9051 /* Annotate start of buffers if we had overruns */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009052 if (ring_buffer_overruns(iter->array_buffer->buffer))
Cody P Schaferb2f974d2013-10-23 11:49:57 -07009053 iter->iter_flags |= TRACE_FILE_ANNOTATE;
9054
9055 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
9056 if (trace_clocks[iter->tr->clock_id].in_ns)
9057 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
Jason Wessel955b61e2010-08-05 09:22:23 -05009058}
9059
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009060void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009061{
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009062 /* use static because iter can be a bit big for the stack */
9063 static struct trace_iterator iter;
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009064 static atomic_t dump_running;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009065 struct trace_array *tr = &global_trace;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009066 unsigned int old_userobj;
Steven Rostedtd7690412008-10-01 00:29:53 -04009067 unsigned long flags;
9068 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009069
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009070 /* Only allow one dump user at a time. */
9071 if (atomic_inc_return(&dump_running) != 1) {
9072 atomic_dec(&dump_running);
9073 return;
Steven Rostedte0a413f2011-09-29 21:26:16 -04009074 }
9075
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009076 /*
9077 * Always turn off tracing when we dump.
9078 * We don't need to show trace output of what happens
9079 * between multiple crashes.
9080 *
9081 * If the user does a sysrq-z, then they can re-enable
9082 * tracing with echo 1 > tracing_on.
9083 */
9084 tracing_off();
9085
9086 local_irq_save(flags);
Petr Mladek03fc7f92018-06-27 16:20:28 +02009087 printk_nmi_direct_enter();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009088
Jovi Zhang38dbe0b2013-01-25 18:03:07 +08009089 /* Simulate the iterator */
Jason Wessel955b61e2010-08-05 09:22:23 -05009090 trace_init_global_iter(&iter);
9091
Steven Rostedtd7690412008-10-01 00:29:53 -04009092 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009093 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04009094 }
9095
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009096 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009097
Török Edwinb54d3de2008-11-22 13:28:48 +02009098 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009099 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Török Edwinb54d3de2008-11-22 13:28:48 +02009100
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009101 switch (oops_dump_mode) {
9102 case DUMP_ALL:
Steven Rostedtae3b5092013-01-23 15:22:59 -05009103 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009104 break;
9105 case DUMP_ORIG:
9106 iter.cpu_file = raw_smp_processor_id();
9107 break;
9108 case DUMP_NONE:
9109 goto out_enable;
9110 default:
9111 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
Steven Rostedtae3b5092013-01-23 15:22:59 -05009112 iter.cpu_file = RING_BUFFER_ALL_CPUS;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009113 }
9114
9115 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009116
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009117 /* Did function tracer already get disabled? */
9118 if (ftrace_is_dead()) {
9119 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
9120 printk("# MAY BE MISSING FUNCTION EVENTS\n");
9121 }
9122
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009123 /*
9124 * We need to stop all tracing on all CPUS to read the
9125 * the next buffer. This is a bit expensive, but is
9126 * not done often. We fill all what we can read,
9127 * and then release the locks again.
9128 */
9129
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009130 while (!trace_empty(&iter)) {
9131
9132 if (!cnt)
9133 printk(KERN_TRACE "---------------------------------\n");
9134
9135 cnt++;
9136
Miguel Ojeda0c97bf82019-05-23 14:45:35 +02009137 trace_iterator_reset(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009138 iter.iter_flags |= TRACE_FILE_LAT_FMT;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009139
Jason Wessel955b61e2010-08-05 09:22:23 -05009140 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08009141 int ret;
9142
9143 ret = print_trace_line(&iter);
9144 if (ret != TRACE_TYPE_NO_CONSUME)
9145 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009146 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05009147 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009148
9149 trace_printk_seq(&iter.seq);
9150 }
9151
9152 if (!cnt)
9153 printk(KERN_TRACE " (ftrace buffer empty)\n");
9154 else
9155 printk(KERN_TRACE "---------------------------------\n");
9156
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02009157 out_enable:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04009158 tr->trace_flags |= old_userobj;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009159
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -04009160 for_each_tracing_cpu(cpu) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05009161 atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009162 }
Petr Mladek03fc7f92018-06-27 16:20:28 +02009163 atomic_dec(&dump_running);
9164 printk_nmi_direct_exit();
Steven Rostedtcd891ae2009-04-28 11:39:34 -04009165 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009166}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07009167EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01009168
Tom Zanussi7e465ba2017-09-22 14:58:20 -05009169int trace_run_command(const char *buf, int (*createfn)(int, char **))
9170{
9171 char **argv;
9172 int argc, ret;
9173
9174 argc = 0;
9175 ret = 0;
9176 argv = argv_split(GFP_KERNEL, buf, &argc);
9177 if (!argv)
9178 return -ENOMEM;
9179
9180 if (argc)
9181 ret = createfn(argc, argv);
9182
9183 argv_free(argv);
9184
9185 return ret;
9186}
9187
9188#define WRITE_BUFSIZE 4096
9189
9190ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
9191 size_t count, loff_t *ppos,
9192 int (*createfn)(int, char **))
9193{
9194 char *kbuf, *buf, *tmp;
9195 int ret = 0;
9196 size_t done = 0;
9197 size_t size;
9198
9199 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
9200 if (!kbuf)
9201 return -ENOMEM;
9202
9203 while (done < count) {
9204 size = count - done;
9205
9206 if (size >= WRITE_BUFSIZE)
9207 size = WRITE_BUFSIZE - 1;
9208
9209 if (copy_from_user(kbuf, buffer + done, size)) {
9210 ret = -EFAULT;
9211 goto out;
9212 }
9213 kbuf[size] = '\0';
9214 buf = kbuf;
9215 do {
9216 tmp = strchr(buf, '\n');
9217 if (tmp) {
9218 *tmp = '\0';
9219 size = tmp - buf + 1;
9220 } else {
9221 size = strlen(buf);
9222 if (done + size < count) {
9223 if (buf != kbuf)
9224 break;
9225 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
9226 pr_warn("Line length is too long: Should be less than %d\n",
9227 WRITE_BUFSIZE - 2);
9228 ret = -EINVAL;
9229 goto out;
9230 }
9231 }
9232 done += size;
9233
9234 /* Remove comments */
9235 tmp = strchr(buf, '#');
9236
9237 if (tmp)
9238 *tmp = '\0';
9239
9240 ret = trace_run_command(buf, createfn);
9241 if (ret)
9242 goto out;
9243 buf += size;
9244
9245 } while (done < count);
9246 }
9247 ret = done;
9248
9249out:
9250 kfree(kbuf);
9251
9252 return ret;
9253}
9254
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009255__init static int tracer_alloc_buffers(void)
9256{
Steven Rostedt73c51622009-03-11 13:42:01 -04009257 int ring_buf_size;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309258 int ret = -ENOMEM;
9259
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05009260
9261 if (security_locked_down(LOCKDOWN_TRACEFS)) {
Stephen Rothwellee195452019-12-06 09:25:03 +11009262 pr_warn("Tracing disabled due to lockdown\n");
Steven Rostedt (VMware)a3566462019-12-02 16:25:27 -05009263 return -EPERM;
9264 }
9265
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04009266 /*
9267 * Make sure we don't accidently add more trace options
9268 * than we have bits for.
9269 */
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04009270 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
Steven Rostedt (Red Hat)b5e87c02015-09-29 18:13:33 -04009271
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309272 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
9273 goto out;
9274
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009275 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309276 goto out_free_buffer_mask;
9277
Steven Rostedt07d777f2011-09-22 14:01:55 -04009278 /* Only allocate trace_printk buffers if a trace_printk exists */
9279 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
Steven Rostedt81698832012-10-11 10:15:05 -04009280 /* Must be called before global_trace.buffer is allocated */
Steven Rostedt07d777f2011-09-22 14:01:55 -04009281 trace_printk_init_buffers();
9282
Steven Rostedt73c51622009-03-11 13:42:01 -04009283 /* To save memory, keep the ring buffer size to its minimum */
9284 if (ring_buffer_expanded)
9285 ring_buf_size = trace_buf_size;
9286 else
9287 ring_buf_size = 1;
9288
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309289 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009290 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009291
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009292 raw_spin_lock_init(&global_trace.start_lock);
9293
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009294 /*
9295 * The prepare callbacks allocates some memory for the ring buffer. We
9296 * don't free the buffer if the if the CPU goes down. If we were to free
9297 * the buffer, then the user would lose any trace that was in the
9298 * buffer. The memory will be removed once the "instance" is removed.
9299 */
9300 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
9301 "trace/RB:preapre", trace_rb_cpu_prepare,
9302 NULL);
9303 if (ret < 0)
9304 goto out_free_cpumask;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009305 /* Used for event triggers */
Dan Carpenter147d88e02017-08-01 14:02:01 +03009306 ret = -ENOMEM;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009307 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
9308 if (!temp_buffer)
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009309 goto out_rm_hp_state;
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009310
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009311 if (trace_create_savedcmd() < 0)
9312 goto out_free_temp_buffer;
9313
Steven Rostedtab464282008-05-12 21:21:00 +02009314 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt (Red Hat)737223f2013-03-05 21:13:47 -05009315 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009316 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
9317 WARN_ON(1);
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009318 goto out_free_savedcmd;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009319 }
Steven Rostedta7603ff2012-08-06 16:24:11 -04009320
Steven Rostedt499e5472012-02-22 15:50:28 -05009321 if (global_trace.buffer_disabled)
9322 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009323
Steven Rostedte1e232c2014-02-10 23:38:46 -05009324 if (trace_boot_clock) {
9325 ret = tracing_set_clock(&global_trace, trace_boot_clock);
9326 if (ret < 0)
Joe Perchesa395d6a2016-03-22 14:28:09 -07009327 pr_warn("Trace clock %s not defined, going back to default\n",
9328 trace_boot_clock);
Steven Rostedte1e232c2014-02-10 23:38:46 -05009329 }
9330
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04009331 /*
9332 * register_tracer() might reference current_trace, so it
9333 * needs to be set before we register anything. This is
9334 * just a bootstrap of current_trace anyway.
9335 */
Steven Rostedt2b6080f2012-05-11 13:29:49 -04009336 global_trace.current_trace = &nop_trace;
9337
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -05009338 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9339
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05009340 ftrace_init_global_array_ops(&global_trace);
9341
Steven Rostedt (Red Hat)9a38a882015-09-30 11:11:15 -04009342 init_trace_flags_index(&global_trace);
9343
Steven Rostedt (Red Hat)ca164312013-05-23 11:51:10 -04009344 register_tracer(&nop_trace);
9345
Steven Rostedt (VMware)dbeafd02017-03-03 13:48:42 -05009346 /* Function tracing may start here (via kernel command line) */
9347 init_function_trace();
9348
Steven Rostedt60a11772008-05-12 21:20:44 +02009349 /* All seems OK, enable tracing */
9350 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04009351
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009352 atomic_notifier_chain_register(&panic_notifier_list,
9353 &trace_panic_notifier);
9354
9355 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01009356
Steven Rostedtae63b31e2012-05-03 23:09:03 -04009357 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
9358
9359 INIT_LIST_HEAD(&global_trace.systems);
9360 INIT_LIST_HEAD(&global_trace.events);
Tom Zanussi067fe032018-01-15 20:51:56 -06009361 INIT_LIST_HEAD(&global_trace.hist_vars);
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -04009362 INIT_LIST_HEAD(&global_trace.err_log);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04009363 list_add(&global_trace.list, &ftrace_trace_arrays);
9364
Jiaxing Wanga4d1e682015-11-04 09:14:29 +08009365 apply_trace_boot_options();
Steven Rostedt7bcfaf52012-11-01 22:56:07 -04009366
Steven Rostedt (Red Hat)77fd5c12013-03-12 11:49:18 -04009367 register_snapshot_cmd();
9368
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01009369 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04009370
Yoshihiro YUNOMAE939c7a42014-06-05 10:24:27 +09009371out_free_savedcmd:
9372 free_saved_cmdlines_buffer(savedcmd);
Steven Rostedt (Red Hat)2c4a33a2014-03-25 23:39:41 -04009373out_free_temp_buffer:
9374 ring_buffer_free(temp_buffer);
Sebastian Andrzej Siewiorb32614c2016-11-27 00:13:34 +01009375out_rm_hp_state:
9376 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309377out_free_cpumask:
Alexander Z Lamccfe9e42013-08-08 09:47:45 -07009378 free_cpumask_var(global_trace.tracing_cpumask);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10309379out_free_buffer_mask:
9380 free_cpumask_var(tracing_buffer_mask);
9381out:
9382 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02009383}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05009384
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05009385void __init early_trace_init(void)
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009386{
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05009387 if (tracepoint_printk) {
9388 tracepoint_print_iter =
9389 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
9390 if (WARN_ON(!tracepoint_print_iter))
9391 tracepoint_printk = 0;
Steven Rostedt (Red Hat)423917452016-11-23 15:52:45 -05009392 else
9393 static_key_enable(&tracepoint_printk_key.key);
Steven Rostedt (Red Hat)0daa23022014-12-12 22:27:10 -05009394 }
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009395 tracer_alloc_buffers();
Steven Rostedt (VMware)e725c732017-03-03 13:37:33 -05009396}
9397
9398void __init trace_init(void)
9399{
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04009400 trace_event_init();
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05009401}
9402
Steven Rostedtb2821ae2009-02-02 21:38:32 -05009403__init static int clear_boot_tracer(void)
9404{
9405 /*
9406 * The default tracer at boot buffer is an init section.
9407 * This function is called in lateinit. If we did not
9408 * find the boot tracer, then clear it out, to prevent
9409 * later registration from accessing the buffer that is
9410 * about to be freed.
9411 */
9412 if (!default_bootup_tracer)
9413 return 0;
9414
9415 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
9416 default_bootup_tracer);
9417 default_bootup_tracer = NULL;
9418
9419 return 0;
9420}
9421
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05009422fs_initcall(tracer_init_tracefs);
Steven Rostedt (VMware)4bb0f0e2017-08-01 12:01:52 -04009423late_initcall_sync(clear_boot_tracer);
Chris Wilson3fd49c92018-03-30 16:01:31 +01009424
9425#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
9426__init static int tracing_set_default_clock(void)
9427{
9428 /* sched_clock_stable() is determined in late_initcall */
Chris Wilson5125eee2018-04-04 22:24:50 +01009429 if (!trace_boot_clock && !sched_clock_stable()) {
Chris Wilson3fd49c92018-03-30 16:01:31 +01009430 printk(KERN_WARNING
9431 "Unstable clock detected, switching default tracing clock to \"global\"\n"
9432 "If you want to keep using the local clock, then add:\n"
9433 " \"trace_clock=local\"\n"
9434 "on the kernel command line\n");
9435 tracing_set_clock(&global_trace, "global");
9436 }
9437
9438 return 0;
9439}
9440late_initcall_sync(tracing_set_default_clock);
9441#endif