forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/kernel/trace/trace.c
....@@ -17,6 +17,7 @@
1717 #include <linux/stacktrace.h>
1818 #include <linux/writeback.h>
1919 #include <linux/kallsyms.h>
20
+#include <linux/security.h>
2021 #include <linux/seq_file.h>
2122 #include <linux/notifier.h>
2223 #include <linux/irqflags.h>
....@@ -44,6 +45,10 @@
4445 #include <linux/trace.h>
4546 #include <linux/sched/clock.h>
4647 #include <linux/sched/rt.h>
48
+#include <linux/fsnotify.h>
49
+#include <linux/irq_work.h>
50
+#include <linux/workqueue.h>
51
+#include <trace/hooks/ftrace_dump.h>
4752
4853 #include "trace.h"
4954 #include "trace_output.h"
....@@ -64,9 +69,20 @@
6469 static bool __read_mostly tracing_selftest_running;
6570
6671 /*
67
- * If a tracer is running, we do not want to run SELFTEST.
72
+ * If boot-time tracing including tracers/events via kernel cmdline
73
+ * is running, we do not want to run SELFTEST.
6874 */
6975 bool __read_mostly tracing_selftest_disabled;
76
+
77
+#ifdef CONFIG_FTRACE_STARTUP_TEST
78
+void __init disable_tracing_selftest(const char *reason)
79
+{
80
+ if (!tracing_selftest_disabled) {
81
+ tracing_selftest_disabled = true;
82
+ pr_info("Ftrace startup test is disabled due to %s\n", reason);
83
+ }
84
+}
85
+#endif
7086
7187 /* Pipe tracepoints to printk */
7288 struct trace_iterator *tracepoint_print_iter;
....@@ -158,7 +174,10 @@
158174 static union trace_eval_map_item *trace_eval_maps;
159175 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
160176
161
-static int tracing_set_tracer(struct trace_array *tr, const char *buf);
177
+int tracing_set_tracer(struct trace_array *tr, const char *buf);
178
+static void ftrace_trace_userstack(struct trace_array *tr,
179
+ struct trace_buffer *buffer,
180
+ unsigned long flags, int pc);
162181
163182 #define MAX_TRACER_SIZE 100
164183 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
....@@ -215,7 +234,7 @@
215234 static int __init set_trace_boot_options(char *str)
216235 {
217236 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
218
- return 0;
237
+ return 1;
219238 }
220239 __setup("trace_options=", set_trace_boot_options);
221240
....@@ -226,7 +245,7 @@
226245 {
227246 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
228247 trace_boot_clock = trace_boot_clock_buf;
229
- return 0;
248
+ return 1;
230249 }
231250 __setup("trace_clock=", set_trace_boot_clock);
232251
....@@ -248,6 +267,145 @@
248267 do_div(nsec, 1000);
249268 return nsec;
250269 }
270
+
271
+static void
272
+trace_process_export(struct trace_export *export,
273
+ struct ring_buffer_event *event, int flag)
274
+{
275
+ struct trace_entry *entry;
276
+ unsigned int size = 0;
277
+
278
+ if (export->flags & flag) {
279
+ entry = ring_buffer_event_data(event);
280
+ size = ring_buffer_event_length(event);
281
+ export->write(export, entry, size);
282
+ }
283
+}
284
+
285
+static DEFINE_MUTEX(ftrace_export_lock);
286
+
287
+static struct trace_export __rcu *ftrace_exports_list __read_mostly;
288
+
289
+static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
290
+static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
291
+static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
292
+
293
+static inline void ftrace_exports_enable(struct trace_export *export)
294
+{
295
+ if (export->flags & TRACE_EXPORT_FUNCTION)
296
+ static_branch_inc(&trace_function_exports_enabled);
297
+
298
+ if (export->flags & TRACE_EXPORT_EVENT)
299
+ static_branch_inc(&trace_event_exports_enabled);
300
+
301
+ if (export->flags & TRACE_EXPORT_MARKER)
302
+ static_branch_inc(&trace_marker_exports_enabled);
303
+}
304
+
305
+static inline void ftrace_exports_disable(struct trace_export *export)
306
+{
307
+ if (export->flags & TRACE_EXPORT_FUNCTION)
308
+ static_branch_dec(&trace_function_exports_enabled);
309
+
310
+ if (export->flags & TRACE_EXPORT_EVENT)
311
+ static_branch_dec(&trace_event_exports_enabled);
312
+
313
+ if (export->flags & TRACE_EXPORT_MARKER)
314
+ static_branch_dec(&trace_marker_exports_enabled);
315
+}
316
+
317
+static void ftrace_exports(struct ring_buffer_event *event, int flag)
318
+{
319
+ struct trace_export *export;
320
+
321
+ preempt_disable_notrace();
322
+
323
+ export = rcu_dereference_raw_check(ftrace_exports_list);
324
+ while (export) {
325
+ trace_process_export(export, event, flag);
326
+ export = rcu_dereference_raw_check(export->next);
327
+ }
328
+
329
+ preempt_enable_notrace();
330
+}
331
+
332
+static inline void
333
+add_trace_export(struct trace_export **list, struct trace_export *export)
334
+{
335
+ rcu_assign_pointer(export->next, *list);
336
+ /*
337
+ * We are entering export into the list but another
338
+ * CPU might be walking that list. We need to make sure
339
+ * the export->next pointer is valid before another CPU sees
340
+ * the export pointer included into the list.
341
+ */
342
+ rcu_assign_pointer(*list, export);
343
+}
344
+
345
+static inline int
346
+rm_trace_export(struct trace_export **list, struct trace_export *export)
347
+{
348
+ struct trace_export **p;
349
+
350
+ for (p = list; *p != NULL; p = &(*p)->next)
351
+ if (*p == export)
352
+ break;
353
+
354
+ if (*p != export)
355
+ return -1;
356
+
357
+ rcu_assign_pointer(*p, (*p)->next);
358
+
359
+ return 0;
360
+}
361
+
362
+static inline void
363
+add_ftrace_export(struct trace_export **list, struct trace_export *export)
364
+{
365
+ ftrace_exports_enable(export);
366
+
367
+ add_trace_export(list, export);
368
+}
369
+
370
+static inline int
371
+rm_ftrace_export(struct trace_export **list, struct trace_export *export)
372
+{
373
+ int ret;
374
+
375
+ ret = rm_trace_export(list, export);
376
+ ftrace_exports_disable(export);
377
+
378
+ return ret;
379
+}
380
+
381
+int register_ftrace_export(struct trace_export *export)
382
+{
383
+ if (WARN_ON_ONCE(!export->write))
384
+ return -1;
385
+
386
+ mutex_lock(&ftrace_export_lock);
387
+
388
+ add_ftrace_export(&ftrace_exports_list, export);
389
+
390
+ mutex_unlock(&ftrace_export_lock);
391
+
392
+ return 0;
393
+}
394
+EXPORT_SYMBOL_GPL(register_ftrace_export);
395
+
396
+int unregister_ftrace_export(struct trace_export *export)
397
+{
398
+ int ret;
399
+
400
+ mutex_lock(&ftrace_export_lock);
401
+
402
+ ret = rm_ftrace_export(&ftrace_exports_list, export);
403
+
404
+ mutex_unlock(&ftrace_export_lock);
405
+
406
+ return ret;
407
+}
408
+EXPORT_SYMBOL_GPL(unregister_ftrace_export);
251409
252410 /* trace_flags holds trace_options default values */
253411 #define TRACE_DEFAULT_FLAGS \
....@@ -299,15 +457,44 @@
299457 this_tr->ref--;
300458 }
301459
460
+/**
461
+ * trace_array_put - Decrement the reference counter for this trace array.
462
+ *
463
+ * NOTE: Use this when we no longer need the trace array returned by
464
+ * trace_array_get_by_name(). This ensures the trace array can be later
465
+ * destroyed.
466
+ *
467
+ */
302468 void trace_array_put(struct trace_array *this_tr)
303469 {
470
+ if (!this_tr)
471
+ return;
472
+
304473 mutex_lock(&trace_types_lock);
305474 __trace_array_put(this_tr);
306475 mutex_unlock(&trace_types_lock);
307476 }
477
+EXPORT_SYMBOL_GPL(trace_array_put);
478
+
479
+int tracing_check_open_get_tr(struct trace_array *tr)
480
+{
481
+ int ret;
482
+
483
+ ret = security_locked_down(LOCKDOWN_TRACEFS);
484
+ if (ret)
485
+ return ret;
486
+
487
+ if (tracing_disabled)
488
+ return -ENODEV;
489
+
490
+ if (tr && trace_array_get(tr) < 0)
491
+ return -ENODEV;
492
+
493
+ return 0;
494
+}
308495
309496 int call_filter_check_discard(struct trace_event_call *call, void *rec,
310
- struct ring_buffer *buffer,
497
+ struct trace_buffer *buffer,
311498 struct ring_buffer_event *event)
312499 {
313500 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
....@@ -355,20 +542,26 @@
355542 * Returns false if @task should be traced.
356543 */
357544 bool
358
-trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
545
+trace_ignore_this_task(struct trace_pid_list *filtered_pids,
546
+ struct trace_pid_list *filtered_no_pids,
547
+ struct task_struct *task)
359548 {
360549 /*
361
- * Return false, because if filtered_pids does not exist,
362
- * all pids are good to trace.
550
+ * If filterd_no_pids is not empty, and the task's pid is listed
551
+ * in filtered_no_pids, then return true.
552
+ * Otherwise, if filtered_pids is empty, that means we can
553
+ * trace all tasks. If it has content, then only trace pids
554
+ * within filtered_pids.
363555 */
364
- if (!filtered_pids)
365
- return false;
366556
367
- return !trace_find_filtered_pid(filtered_pids, task->pid);
557
+ return (filtered_pids &&
558
+ !trace_find_filtered_pid(filtered_pids, task->pid)) ||
559
+ (filtered_no_pids &&
560
+ trace_find_filtered_pid(filtered_no_pids, task->pid));
368561 }
369562
370563 /**
371
- * trace_pid_filter_add_remove_task - Add or remove a task from a pid_list
564
+ * trace_filter_add_remove_task - Add or remove a task from a pid_list
372565 * @pid_list: The list to modify
373566 * @self: The current task for fork or NULL for exit
374567 * @task: The task to add or remove
....@@ -572,7 +765,7 @@
572765 return read;
573766 }
574767
575
-static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
768
+static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
576769 {
577770 u64 ts;
578771
....@@ -588,7 +781,7 @@
588781
589782 u64 ftrace_now(int cpu)
590783 {
591
- return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
784
+ return buffer_ftrace_now(&global_trace.array_buffer, cpu);
592785 }
593786
594787 /**
....@@ -716,22 +909,22 @@
716909 #endif
717910
718911 #ifdef CONFIG_STACKTRACE
719
-static void __ftrace_trace_stack(struct ring_buffer *buffer,
912
+static void __ftrace_trace_stack(struct trace_buffer *buffer,
720913 unsigned long flags,
721914 int skip, int pc, struct pt_regs *regs);
722915 static inline void ftrace_trace_stack(struct trace_array *tr,
723
- struct ring_buffer *buffer,
916
+ struct trace_buffer *buffer,
724917 unsigned long flags,
725918 int skip, int pc, struct pt_regs *regs);
726919
727920 #else
728
-static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
921
+static inline void __ftrace_trace_stack(struct trace_buffer *buffer,
729922 unsigned long flags,
730923 int skip, int pc, struct pt_regs *regs)
731924 {
732925 }
733926 static inline void ftrace_trace_stack(struct trace_array *tr,
734
- struct ring_buffer *buffer,
927
+ struct trace_buffer *buffer,
735928 unsigned long flags,
736929 int skip, int pc, struct pt_regs *regs)
737930 {
....@@ -745,12 +938,11 @@
745938 {
746939 struct trace_entry *ent = ring_buffer_event_data(event);
747940
748
- tracing_generic_entry_update(ent, flags, pc);
749
- ent->type = type;
941
+ tracing_generic_entry_update(ent, type, flags, pc);
750942 }
751943
752944 static __always_inline struct ring_buffer_event *
753
-__trace_buffer_lock_reserve(struct ring_buffer *buffer,
945
+__trace_buffer_lock_reserve(struct trace_buffer *buffer,
754946 int type,
755947 unsigned long len,
756948 unsigned long flags, int pc)
....@@ -766,8 +958,8 @@
766958
767959 void tracer_tracing_on(struct trace_array *tr)
768960 {
769
- if (tr->trace_buffer.buffer)
770
- ring_buffer_record_on(tr->trace_buffer.buffer);
961
+ if (tr->array_buffer.buffer)
962
+ ring_buffer_record_on(tr->array_buffer.buffer);
771963 /*
772964 * This flag is looked at when buffers haven't been allocated
773965 * yet, or by some tracers (like irqsoff), that just want to
....@@ -795,7 +987,7 @@
795987
796988
797989 static __always_inline void
798
-__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
990
+__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
799991 {
800992 __this_cpu_write(trace_taskinfo_save, true);
801993
....@@ -818,7 +1010,7 @@
8181010 int __trace_puts(unsigned long ip, const char *str, int size)
8191011 {
8201012 struct ring_buffer_event *event;
821
- struct ring_buffer *buffer;
1013
+ struct trace_buffer *buffer;
8221014 struct print_entry *entry;
8231015 unsigned long irq_flags;
8241016 int alloc;
....@@ -835,11 +1027,14 @@
8351027 alloc = sizeof(*entry) + size + 2; /* possible \n added */
8361028
8371029 local_save_flags(irq_flags);
838
- buffer = global_trace.trace_buffer.buffer;
1030
+ buffer = global_trace.array_buffer.buffer;
1031
+ ring_buffer_nest_start(buffer);
8391032 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
8401033 irq_flags, pc);
841
- if (!event)
842
- return 0;
1034
+ if (!event) {
1035
+ size = 0;
1036
+ goto out;
1037
+ }
8431038
8441039 entry = ring_buffer_event_data(event);
8451040 entry->ip = ip;
....@@ -855,7 +1050,8 @@
8551050
8561051 __buffer_unlock_commit(buffer, event);
8571052 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
858
-
1053
+ out:
1054
+ ring_buffer_nest_end(buffer);
8591055 return size;
8601056 }
8611057 EXPORT_SYMBOL_GPL(__trace_puts);
....@@ -868,10 +1064,11 @@
8681064 int __trace_bputs(unsigned long ip, const char *str)
8691065 {
8701066 struct ring_buffer_event *event;
871
- struct ring_buffer *buffer;
1067
+ struct trace_buffer *buffer;
8721068 struct bputs_entry *entry;
8731069 unsigned long irq_flags;
8741070 int size = sizeof(struct bputs_entry);
1071
+ int ret = 0;
8751072 int pc;
8761073
8771074 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
....@@ -883,11 +1080,13 @@
8831080 return 0;
8841081
8851082 local_save_flags(irq_flags);
886
- buffer = global_trace.trace_buffer.buffer;
1083
+ buffer = global_trace.array_buffer.buffer;
1084
+
1085
+ ring_buffer_nest_start(buffer);
8871086 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
8881087 irq_flags, pc);
8891088 if (!event)
890
- return 0;
1089
+ goto out;
8911090
8921091 entry = ring_buffer_event_data(event);
8931092 entry->ip = ip;
....@@ -896,12 +1095,16 @@
8961095 __buffer_unlock_commit(buffer, event);
8971096 ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
8981097
899
- return 1;
1098
+ ret = 1;
1099
+ out:
1100
+ ring_buffer_nest_end(buffer);
1101
+ return ret;
9001102 }
9011103 EXPORT_SYMBOL_GPL(__trace_bputs);
9021104
9031105 #ifdef CONFIG_TRACER_SNAPSHOT
904
-void tracing_snapshot_instance(struct trace_array *tr)
1106
+static void tracing_snapshot_instance_cond(struct trace_array *tr,
1107
+ void *cond_data)
9051108 {
9061109 struct tracer *tracer = tr->current_trace;
9071110 unsigned long flags;
....@@ -927,8 +1130,13 @@
9271130 }
9281131
9291132 local_irq_save(flags);
930
- update_max_tr(tr, current, smp_processor_id());
1133
+ update_max_tr(tr, current, smp_processor_id(), cond_data);
9311134 local_irq_restore(flags);
1135
+}
1136
+
1137
+void tracing_snapshot_instance(struct trace_array *tr)
1138
+{
1139
+ tracing_snapshot_instance_cond(tr, NULL);
9321140 }
9331141
9341142 /**
....@@ -953,9 +1161,59 @@
9531161 }
9541162 EXPORT_SYMBOL_GPL(tracing_snapshot);
9551163
956
-static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
957
- struct trace_buffer *size_buf, int cpu_id);
958
-static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
1164
+/**
1165
+ * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
1166
+ * @tr: The tracing instance to snapshot
1167
+ * @cond_data: The data to be tested conditionally, and possibly saved
1168
+ *
1169
+ * This is the same as tracing_snapshot() except that the snapshot is
1170
+ * conditional - the snapshot will only happen if the
1171
+ * cond_snapshot.update() implementation receiving the cond_data
1172
+ * returns true, which means that the trace array's cond_snapshot
1173
+ * update() operation used the cond_data to determine whether the
1174
+ * snapshot should be taken, and if it was, presumably saved it along
1175
+ * with the snapshot.
1176
+ */
1177
+void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1178
+{
1179
+ tracing_snapshot_instance_cond(tr, cond_data);
1180
+}
1181
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1182
+
1183
+/**
1184
+ * tracing_snapshot_cond_data - get the user data associated with a snapshot
1185
+ * @tr: The tracing instance
1186
+ *
1187
+ * When the user enables a conditional snapshot using
1188
+ * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1189
+ * with the snapshot. This accessor is used to retrieve it.
1190
+ *
1191
+ * Should not be called from cond_snapshot.update(), since it takes
1192
+ * the tr->max_lock lock, which the code calling
1193
+ * cond_snapshot.update() has already done.
1194
+ *
1195
+ * Returns the cond_data associated with the trace array's snapshot.
1196
+ */
1197
+void *tracing_cond_snapshot_data(struct trace_array *tr)
1198
+{
1199
+ void *cond_data = NULL;
1200
+
1201
+ local_irq_disable();
1202
+ arch_spin_lock(&tr->max_lock);
1203
+
1204
+ if (tr->cond_snapshot)
1205
+ cond_data = tr->cond_snapshot->cond_data;
1206
+
1207
+ arch_spin_unlock(&tr->max_lock);
1208
+ local_irq_enable();
1209
+
1210
+ return cond_data;
1211
+}
1212
+EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1213
+
1214
+static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1215
+ struct array_buffer *size_buf, int cpu_id);
1216
+static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
9591217
9601218 int tracing_alloc_snapshot_instance(struct trace_array *tr)
9611219 {
....@@ -965,7 +1223,7 @@
9651223
9661224 /* allocate spare buffer */
9671225 ret = resize_buffer_duplicate_size(&tr->max_buffer,
968
- &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
1226
+ &tr->array_buffer, RING_BUFFER_ALL_CPUS);
9691227 if (ret < 0)
9701228 return ret;
9711229
....@@ -1032,12 +1290,115 @@
10321290 tracing_snapshot();
10331291 }
10341292 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1293
+
1294
+/**
1295
+ * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1296
+ * @tr: The tracing instance
1297
+ * @cond_data: User data to associate with the snapshot
1298
+ * @update: Implementation of the cond_snapshot update function
1299
+ *
1300
+ * Check whether the conditional snapshot for the given instance has
1301
+ * already been enabled, or if the current tracer is already using a
1302
+ * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1303
+ * save the cond_data and update function inside.
1304
+ *
1305
+ * Returns 0 if successful, error otherwise.
1306
+ */
1307
+int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1308
+ cond_update_fn_t update)
1309
+{
1310
+ struct cond_snapshot *cond_snapshot;
1311
+ int ret = 0;
1312
+
1313
+ cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1314
+ if (!cond_snapshot)
1315
+ return -ENOMEM;
1316
+
1317
+ cond_snapshot->cond_data = cond_data;
1318
+ cond_snapshot->update = update;
1319
+
1320
+ mutex_lock(&trace_types_lock);
1321
+
1322
+ ret = tracing_alloc_snapshot_instance(tr);
1323
+ if (ret)
1324
+ goto fail_unlock;
1325
+
1326
+ if (tr->current_trace->use_max_tr) {
1327
+ ret = -EBUSY;
1328
+ goto fail_unlock;
1329
+ }
1330
+
1331
+ /*
1332
+ * The cond_snapshot can only change to NULL without the
1333
+ * trace_types_lock. We don't care if we race with it going
1334
+ * to NULL, but we want to make sure that it's not set to
1335
+ * something other than NULL when we get here, which we can
1336
+ * do safely with only holding the trace_types_lock and not
1337
+ * having to take the max_lock.
1338
+ */
1339
+ if (tr->cond_snapshot) {
1340
+ ret = -EBUSY;
1341
+ goto fail_unlock;
1342
+ }
1343
+
1344
+ local_irq_disable();
1345
+ arch_spin_lock(&tr->max_lock);
1346
+ tr->cond_snapshot = cond_snapshot;
1347
+ arch_spin_unlock(&tr->max_lock);
1348
+ local_irq_enable();
1349
+
1350
+ mutex_unlock(&trace_types_lock);
1351
+
1352
+ return ret;
1353
+
1354
+ fail_unlock:
1355
+ mutex_unlock(&trace_types_lock);
1356
+ kfree(cond_snapshot);
1357
+ return ret;
1358
+}
1359
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1360
+
1361
+/**
1362
+ * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1363
+ * @tr: The tracing instance
1364
+ *
1365
+ * Check whether the conditional snapshot for the given instance is
1366
+ * enabled; if so, free the cond_snapshot associated with it,
1367
+ * otherwise return -EINVAL.
1368
+ *
1369
+ * Returns 0 if successful, error otherwise.
1370
+ */
1371
+int tracing_snapshot_cond_disable(struct trace_array *tr)
1372
+{
1373
+ int ret = 0;
1374
+
1375
+ local_irq_disable();
1376
+ arch_spin_lock(&tr->max_lock);
1377
+
1378
+ if (!tr->cond_snapshot)
1379
+ ret = -EINVAL;
1380
+ else {
1381
+ kfree(tr->cond_snapshot);
1382
+ tr->cond_snapshot = NULL;
1383
+ }
1384
+
1385
+ arch_spin_unlock(&tr->max_lock);
1386
+ local_irq_enable();
1387
+
1388
+ return ret;
1389
+}
1390
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
10351391 #else
10361392 void tracing_snapshot(void)
10371393 {
10381394 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
10391395 }
10401396 EXPORT_SYMBOL_GPL(tracing_snapshot);
1397
+void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1398
+{
1399
+ WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1400
+}
1401
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
10411402 int tracing_alloc_snapshot(void)
10421403 {
10431404 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
....@@ -1050,12 +1411,27 @@
10501411 tracing_snapshot();
10511412 }
10521413 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1414
+void *tracing_cond_snapshot_data(struct trace_array *tr)
1415
+{
1416
+ return NULL;
1417
+}
1418
+EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1419
+int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1420
+{
1421
+ return -ENODEV;
1422
+}
1423
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1424
+int tracing_snapshot_cond_disable(struct trace_array *tr)
1425
+{
1426
+ return false;
1427
+}
1428
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
10531429 #endif /* CONFIG_TRACER_SNAPSHOT */
10541430
10551431 void tracer_tracing_off(struct trace_array *tr)
10561432 {
1057
- if (tr->trace_buffer.buffer)
1058
- ring_buffer_record_off(tr->trace_buffer.buffer);
1433
+ if (tr->array_buffer.buffer)
1434
+ ring_buffer_record_off(tr->array_buffer.buffer);
10591435 /*
10601436 * This flag is looked at when buffers haven't been allocated
10611437 * yet, or by some tracers (like irqsoff), that just want to
....@@ -1085,8 +1461,11 @@
10851461
10861462 void disable_trace_on_warning(void)
10871463 {
1088
- if (__disable_trace_on_warning)
1464
+ if (__disable_trace_on_warning) {
1465
+ trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
1466
+ "Disabling tracing due to warning\n");
10891467 tracing_off();
1468
+ }
10901469 }
10911470
10921471 /**
....@@ -1097,8 +1476,8 @@
10971476 */
10981477 bool tracer_tracing_is_on(struct trace_array *tr)
10991478 {
1100
- if (tr->trace_buffer.buffer)
1101
- return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1479
+ if (tr->array_buffer.buffer)
1480
+ return ring_buffer_record_is_on(tr->array_buffer.buffer);
11021481 return !tr->buffer_disabled;
11031482 }
11041483
....@@ -1118,10 +1497,12 @@
11181497 if (!str)
11191498 return 0;
11201499 buf_size = memparse(str, &str);
1121
- /* nr_entries can not be zero */
1122
- if (buf_size == 0)
1123
- return 0;
1124
- trace_buf_size = buf_size;
1500
+ /*
1501
+ * nr_entries can not be zero and the startup
1502
+ * tests require some buffer space. Therefore
1503
+ * ensure we have at least 4096 bytes of buffer.
1504
+ */
1505
+ trace_buf_size = max(4096UL, buf_size);
11251506 return 1;
11261507 }
11271508 __setup("trace_buf_size=", set_buf_size);
....@@ -1315,6 +1696,73 @@
13151696 }
13161697
13171698 unsigned long __read_mostly tracing_thresh;
1699
+static const struct file_operations tracing_max_lat_fops;
1700
+
1701
+#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1702
+ defined(CONFIG_FSNOTIFY)
1703
+
1704
+static struct workqueue_struct *fsnotify_wq;
1705
+
1706
+static void latency_fsnotify_workfn(struct work_struct *work)
1707
+{
1708
+ struct trace_array *tr = container_of(work, struct trace_array,
1709
+ fsnotify_work);
1710
+ fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
1711
+}
1712
+
1713
+static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1714
+{
1715
+ struct trace_array *tr = container_of(iwork, struct trace_array,
1716
+ fsnotify_irqwork);
1717
+ queue_work(fsnotify_wq, &tr->fsnotify_work);
1718
+}
1719
+
1720
+static void trace_create_maxlat_file(struct trace_array *tr,
1721
+ struct dentry *d_tracer)
1722
+{
1723
+ INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1724
+ init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1725
+ tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
1726
+ d_tracer, &tr->max_latency,
1727
+ &tracing_max_lat_fops);
1728
+}
1729
+
1730
+__init static int latency_fsnotify_init(void)
1731
+{
1732
+ fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1733
+ WQ_UNBOUND | WQ_HIGHPRI, 0);
1734
+ if (!fsnotify_wq) {
1735
+ pr_err("Unable to allocate tr_max_lat_wq\n");
1736
+ return -ENOMEM;
1737
+ }
1738
+ return 0;
1739
+}
1740
+
1741
+late_initcall_sync(latency_fsnotify_init);
1742
+
1743
+void latency_fsnotify(struct trace_array *tr)
1744
+{
1745
+ if (!fsnotify_wq)
1746
+ return;
1747
+ /*
1748
+ * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1749
+ * possible that we are called from __schedule() or do_idle(), which
1750
+ * could cause a deadlock.
1751
+ */
1752
+ irq_work_queue(&tr->fsnotify_irqwork);
1753
+}
1754
+
1755
+/*
1756
+ * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
1757
+ * defined(CONFIG_FSNOTIFY)
1758
+ */
1759
+#else
1760
+
1761
+#define trace_create_maxlat_file(tr, d_tracer) \
1762
+ trace_create_file("tracing_max_latency", 0644, d_tracer, \
1763
+ &tr->max_latency, &tracing_max_lat_fops)
1764
+
1765
+#endif
13181766
13191767 #ifdef CONFIG_TRACER_MAX_TRACE
13201768 /*
....@@ -1325,8 +1773,8 @@
13251773 static void
13261774 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
13271775 {
1328
- struct trace_buffer *trace_buf = &tr->trace_buffer;
1329
- struct trace_buffer *max_buf = &tr->max_buffer;
1776
+ struct array_buffer *trace_buf = &tr->array_buffer;
1777
+ struct array_buffer *max_buf = &tr->max_buffer;
13301778 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
13311779 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
13321780
....@@ -1337,7 +1785,7 @@
13371785 max_data->critical_start = data->critical_start;
13381786 max_data->critical_end = data->critical_end;
13391787
1340
- memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
1788
+ strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
13411789 max_data->pid = tsk->pid;
13421790 /*
13431791 * If tsk == current, then use current_uid(), as that does not use
....@@ -1354,6 +1802,7 @@
13541802
13551803 /* record this tasks comm */
13561804 tracing_record_cmdline(tsk);
1805
+ latency_fsnotify(tr);
13571806 }
13581807
13591808 /**
....@@ -1361,12 +1810,14 @@
13611810 * @tr: tracer
13621811 * @tsk: the task with the latency
13631812 * @cpu: The cpu that initiated the trace.
1813
+ * @cond_data: User data associated with a conditional snapshot
13641814 *
13651815 * Flip the buffers between the @tr and the max_tr and record information
13661816 * about which task was the cause of this latency.
13671817 */
13681818 void
1369
-update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1819
+update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1820
+ void *cond_data)
13701821 {
13711822 if (tr->stop_count)
13721823 return;
....@@ -1381,23 +1832,29 @@
13811832
13821833 arch_spin_lock(&tr->max_lock);
13831834
1384
- /* Inherit the recordable setting from trace_buffer */
1385
- if (ring_buffer_record_is_set_on(tr->trace_buffer.buffer))
1835
+ /* Inherit the recordable setting from array_buffer */
1836
+ if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
13861837 ring_buffer_record_on(tr->max_buffer.buffer);
13871838 else
13881839 ring_buffer_record_off(tr->max_buffer.buffer);
13891840
1390
- swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
1841
+#ifdef CONFIG_TRACER_SNAPSHOT
1842
+ if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
1843
+ goto out_unlock;
1844
+#endif
1845
+ swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
13911846
13921847 __update_max_tr(tr, tsk, cpu);
1848
+
1849
+ out_unlock:
13931850 arch_spin_unlock(&tr->max_lock);
13941851 }
13951852
13961853 /**
13971854 * update_max_tr_single - only copy one trace over, and reset the rest
1398
- * @tr - tracer
1399
- * @tsk - task with the latency
1400
- * @cpu - the cpu of the buffer to copy.
1855
+ * @tr: tracer
1856
+ * @tsk: task with the latency
1857
+ * @cpu: the cpu of the buffer to copy.
14011858 *
14021859 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
14031860 */
....@@ -1418,7 +1875,7 @@
14181875
14191876 arch_spin_lock(&tr->max_lock);
14201877
1421
- ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1878
+ ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
14221879
14231880 if (ret == -EBUSY) {
14241881 /*
....@@ -1438,13 +1895,13 @@
14381895 }
14391896 #endif /* CONFIG_TRACER_MAX_TRACE */
14401897
1441
-static int wait_on_pipe(struct trace_iterator *iter, bool full)
1898
+static int wait_on_pipe(struct trace_iterator *iter, int full)
14421899 {
14431900 /* Iterators are static, they should be filled or empty */
14441901 if (trace_buffer_iter(iter, iter->cpu_file))
14451902 return 0;
14461903
1447
- return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1904
+ return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
14481905 full);
14491906 }
14501907
....@@ -1495,7 +1952,7 @@
14951952 * internal tracing to verify that everything is in order.
14961953 * If we fail, we do not register this tracer.
14971954 */
1498
- tracing_reset_online_cpus(&tr->trace_buffer);
1955
+ tracing_reset_online_cpus(&tr->array_buffer);
14991956
15001957 tr->current_trace = type;
15011958
....@@ -1521,7 +1978,7 @@
15211978 return -1;
15221979 }
15231980 /* Only reset on passing, to avoid touching corrupted buffers */
1524
- tracing_reset_online_cpus(&tr->trace_buffer);
1981
+ tracing_reset_online_cpus(&tr->array_buffer);
15251982
15261983 #ifdef CONFIG_TRACER_MAX_TRACE
15271984 if (type->use_max_tr) {
....@@ -1555,6 +2012,10 @@
15552012
15562013 tracing_selftest_running = true;
15572014 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
2015
+ /* This loop can take minutes when sanitizers are enabled, so
2016
+ * lets make sure we allow RCU processing.
2017
+ */
2018
+ cond_resched();
15582019 ret = run_tracer_selftest(p->type);
15592020 /* If the test fails, then warn and remove from available_tracers */
15602021 if (ret < 0) {
....@@ -1593,7 +2054,7 @@
15932054
15942055 /**
15952056 * register_tracer - register a tracer with the ftrace system.
1596
- * @type - the plugin for the tracer
2057
+ * @type: the plugin for the tracer
15972058 *
15982059 * Register a new plugin tracer.
15992060 */
....@@ -1610,6 +2071,12 @@
16102071 if (strlen(type->name) >= MAX_TRACER_SIZE) {
16112072 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
16122073 return -1;
2074
+ }
2075
+
2076
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
2077
+ pr_warn("Can not register tracer %s due to lockdown\n",
2078
+ type->name);
2079
+ return -EPERM;
16132080 }
16142081
16152082 mutex_lock(&trace_types_lock);
....@@ -1670,19 +2137,15 @@
16702137 apply_trace_boot_options();
16712138
16722139 /* disable other selftests, since this will break it. */
1673
- tracing_selftest_disabled = true;
1674
-#ifdef CONFIG_FTRACE_STARTUP_TEST
1675
- printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1676
- type->name);
1677
-#endif
2140
+ disable_tracing_selftest("running a tracer");
16782141
16792142 out_unlock:
16802143 return ret;
16812144 }
16822145
1683
-void tracing_reset(struct trace_buffer *buf, int cpu)
2146
+static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
16842147 {
1685
- struct ring_buffer *buffer = buf->buffer;
2148
+ struct trace_buffer *buffer = buf->buffer;
16862149
16872150 if (!buffer)
16882151 return;
....@@ -1690,16 +2153,15 @@
16902153 ring_buffer_record_disable(buffer);
16912154
16922155 /* Make sure all commits have finished */
1693
- synchronize_sched();
2156
+ synchronize_rcu();
16942157 ring_buffer_reset_cpu(buffer, cpu);
16952158
16962159 ring_buffer_record_enable(buffer);
16972160 }
16982161
1699
-void tracing_reset_online_cpus(struct trace_buffer *buf)
2162
+void tracing_reset_online_cpus(struct array_buffer *buf)
17002163 {
1701
- struct ring_buffer *buffer = buf->buffer;
1702
- int cpu;
2164
+ struct trace_buffer *buffer = buf->buffer;
17032165
17042166 if (!buffer)
17052167 return;
....@@ -1707,12 +2169,11 @@
17072169 ring_buffer_record_disable(buffer);
17082170
17092171 /* Make sure all commits have finished */
1710
- synchronize_sched();
2172
+ synchronize_rcu();
17112173
17122174 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
17132175
1714
- for_each_online_cpu(cpu)
1715
- ring_buffer_reset_cpu(buffer, cpu);
2176
+ ring_buffer_reset_online_cpus(buffer);
17162177
17172178 ring_buffer_record_enable(buffer);
17182179 }
....@@ -1726,7 +2187,7 @@
17262187 if (!tr->clear_trace)
17272188 continue;
17282189 tr->clear_trace = false;
1729
- tracing_reset_online_cpus(&tr->trace_buffer);
2190
+ tracing_reset_online_cpus(&tr->array_buffer);
17302191 #ifdef CONFIG_TRACER_MAX_TRACE
17312192 tracing_reset_online_cpus(&tr->max_buffer);
17322193 #endif
....@@ -1744,6 +2205,11 @@
17442205
17452206 #define SAVED_CMDLINES_DEFAULT 128
17462207 #define NO_CMDLINE_MAP UINT_MAX
2208
+/*
2209
+ * Preemption must be disabled before acquiring trace_cmdline_lock.
2210
+ * The various trace_arrays' max_lock must be acquired in a context
2211
+ * where interrupt is disabled.
2212
+ */
17472213 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
17482214 struct saved_cmdlines_buffer {
17492215 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
....@@ -1761,7 +2227,7 @@
17612227
17622228 static inline void set_cmdline(int idx, const char *cmdline)
17632229 {
1764
- memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
2230
+ strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
17652231 }
17662232
17672233 static int allocate_cmdlines_buffer(unsigned int val,
....@@ -1820,7 +2286,7 @@
18202286 */
18212287 void tracing_start(void)
18222288 {
1823
- struct ring_buffer *buffer;
2289
+ struct trace_buffer *buffer;
18242290 unsigned long flags;
18252291
18262292 if (tracing_disabled)
....@@ -1839,7 +2305,7 @@
18392305 /* Prevent the buffers from switching */
18402306 arch_spin_lock(&global_trace.max_lock);
18412307
1842
- buffer = global_trace.trace_buffer.buffer;
2308
+ buffer = global_trace.array_buffer.buffer;
18432309 if (buffer)
18442310 ring_buffer_record_enable(buffer);
18452311
....@@ -1857,7 +2323,7 @@
18572323
18582324 static void tracing_start_tr(struct trace_array *tr)
18592325 {
1860
- struct ring_buffer *buffer;
2326
+ struct trace_buffer *buffer;
18612327 unsigned long flags;
18622328
18632329 if (tracing_disabled)
....@@ -1878,7 +2344,7 @@
18782344 goto out;
18792345 }
18802346
1881
- buffer = tr->trace_buffer.buffer;
2347
+ buffer = tr->array_buffer.buffer;
18822348 if (buffer)
18832349 ring_buffer_record_enable(buffer);
18842350
....@@ -1894,7 +2360,7 @@
18942360 */
18952361 void tracing_stop(void)
18962362 {
1897
- struct ring_buffer *buffer;
2363
+ struct trace_buffer *buffer;
18982364 unsigned long flags;
18992365
19002366 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
....@@ -1904,7 +2370,7 @@
19042370 /* Prevent the buffers from switching */
19052371 arch_spin_lock(&global_trace.max_lock);
19062372
1907
- buffer = global_trace.trace_buffer.buffer;
2373
+ buffer = global_trace.array_buffer.buffer;
19082374 if (buffer)
19092375 ring_buffer_record_disable(buffer);
19102376
....@@ -1922,7 +2388,7 @@
19222388
19232389 static void tracing_stop_tr(struct trace_array *tr)
19242390 {
1925
- struct ring_buffer *buffer;
2391
+ struct trace_buffer *buffer;
19262392 unsigned long flags;
19272393
19282394 /* If global, we need to also stop the max tracer */
....@@ -1933,7 +2399,7 @@
19332399 if (tr->stop_count++)
19342400 goto out;
19352401
1936
- buffer = tr->trace_buffer.buffer;
2402
+ buffer = tr->array_buffer.buffer;
19372403 if (buffer)
19382404 ring_buffer_record_disable(buffer);
19392405
....@@ -1956,7 +2422,11 @@
19562422 * the lock, but we also don't want to spin
19572423 * nor do we want to disable interrupts,
19582424 * so if we miss here, then better luck next time.
2425
+ *
2426
+ * This is called within the scheduler and wake up, so interrupts
2427
+ * had better been disabled and run queue lock been held.
19592428 */
2429
+ lockdep_assert_preemption_disabled();
19602430 if (!arch_spin_trylock(&trace_cmdline_lock))
19612431 return 0;
19622432
....@@ -2064,9 +2534,9 @@
20642534 /**
20652535 * tracing_record_taskinfo - record the task info of a task
20662536 *
2067
- * @task - task to record
2068
- * @flags - TRACE_RECORD_CMDLINE for recording comm
2069
- * - TRACE_RECORD_TGID for recording tgid
2537
+ * @task: task to record
2538
+ * @flags: TRACE_RECORD_CMDLINE for recording comm
2539
+ * TRACE_RECORD_TGID for recording tgid
20702540 */
20712541 void tracing_record_taskinfo(struct task_struct *task, int flags)
20722542 {
....@@ -2092,10 +2562,10 @@
20922562 /**
20932563 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
20942564 *
2095
- * @prev - previous task during sched_switch
2096
- * @next - next task during sched_switch
2097
- * @flags - TRACE_RECORD_CMDLINE for recording comm
2098
- * TRACE_RECORD_TGID for recording tgid
2565
+ * @prev: previous task during sched_switch
2566
+ * @next: next task during sched_switch
2567
+ * @flags: TRACE_RECORD_CMDLINE for recording comm
2568
+ * TRACE_RECORD_TGID for recording tgid
20992569 */
21002570 void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
21012571 struct task_struct *next, int flags)
....@@ -2145,13 +2615,14 @@
21452615 EXPORT_SYMBOL_GPL(trace_handle_return);
21462616
21472617 void
2148
-tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2149
- int pc)
2618
+tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2619
+ unsigned long flags, int pc)
21502620 {
21512621 struct task_struct *tsk = current;
21522622
21532623 entry->preempt_count = pc & 0xff;
21542624 entry->pid = (tsk) ? tsk->pid : 0;
2625
+ entry->type = type;
21552626 entry->flags =
21562627 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
21572628 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
....@@ -2167,7 +2638,7 @@
21672638 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
21682639
21692640 struct ring_buffer_event *
2170
-trace_buffer_lock_reserve(struct ring_buffer *buffer,
2641
+trace_buffer_lock_reserve(struct trace_buffer *buffer,
21712642 int type,
21722643 unsigned long len,
21732644 unsigned long flags, int pc)
....@@ -2217,7 +2688,7 @@
22172688
22182689 preempt_disable();
22192690 if (cpu == smp_processor_id() &&
2220
- this_cpu_read(trace_buffered_event) !=
2691
+ __this_cpu_read(trace_buffered_event) !=
22212692 per_cpu(trace_buffered_event, cpu))
22222693 WARN_ON_ONCE(1);
22232694 preempt_enable();
....@@ -2267,7 +2738,7 @@
22672738 preempt_enable();
22682739
22692740 /* Wait for all current users to finish */
2270
- synchronize_sched();
2741
+ synchronize_rcu();
22712742
22722743 for_each_tracing_cpu(cpu) {
22732744 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
....@@ -2286,10 +2757,10 @@
22862757 preempt_enable();
22872758 }
22882759
2289
-static struct ring_buffer *temp_buffer;
2760
+static struct trace_buffer *temp_buffer;
22902761
22912762 struct ring_buffer_event *
2292
-trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
2763
+trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
22932764 struct trace_event_file *trace_file,
22942765 int type, unsigned long len,
22952766 unsigned long flags, int pc)
....@@ -2297,7 +2768,7 @@
22972768 struct ring_buffer_event *entry;
22982769 int val;
22992770
2300
- *current_rb = trace_file->tr->trace_buffer.buffer;
2771
+ *current_rb = trace_file->tr->array_buffer.buffer;
23012772
23022773 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
23032774 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
....@@ -2317,7 +2788,7 @@
23172788 /*
23182789 * If tracing is off, but we have triggers enabled
23192790 * we still need to look at the event data. Use the temp_buffer
2320
- * to store the trace event for the tigger to use. It's recusive
2791
+ * to store the trace event for the trigger to use. It's recursive
23212792 * safe and will not be recorded anywhere.
23222793 */
23232794 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
....@@ -2329,12 +2800,13 @@
23292800 }
23302801 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
23312802
2332
-static DEFINE_SPINLOCK(tracepoint_iter_lock);
2803
+static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
23332804 static DEFINE_MUTEX(tracepoint_printk_mutex);
23342805
23352806 static void output_printk(struct trace_event_buffer *fbuffer)
23362807 {
23372808 struct trace_event_call *event_call;
2809
+ struct trace_event_file *file;
23382810 struct trace_event *event;
23392811 unsigned long flags;
23402812 struct trace_iterator *iter = tracepoint_print_iter;
....@@ -2348,20 +2820,26 @@
23482820 !event_call->event.funcs->trace)
23492821 return;
23502822
2823
+ file = fbuffer->trace_file;
2824
+ if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2825
+ (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2826
+ !filter_match_preds(file->filter, fbuffer->entry)))
2827
+ return;
2828
+
23512829 event = &fbuffer->trace_file->event_call->event;
23522830
2353
- spin_lock_irqsave(&tracepoint_iter_lock, flags);
2831
+ raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
23542832 trace_seq_init(&iter->seq);
23552833 iter->ent = fbuffer->entry;
23562834 event_call->event.funcs->trace(iter, 0, event);
23572835 trace_seq_putc(&iter->seq, 0);
23582836 printk("%s", iter->seq.buffer);
23592837
2360
- spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2838
+ raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
23612839 }
23622840
23632841 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2364
- void __user *buffer, size_t *lenp,
2842
+ void *buffer, size_t *lenp,
23652843 loff_t *ppos)
23662844 {
23672845 int save_tracepoint_printk;
....@@ -2398,9 +2876,11 @@
23982876 if (static_key_false(&tracepoint_printk_key.key))
23992877 output_printk(fbuffer);
24002878
2401
- event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2879
+ if (static_branch_unlikely(&trace_event_exports_enabled))
2880
+ ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
2881
+ event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
24022882 fbuffer->event, fbuffer->entry,
2403
- fbuffer->flags, fbuffer->pc);
2883
+ fbuffer->flags, fbuffer->pc, fbuffer->regs);
24042884 }
24052885 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
24062886
....@@ -2414,7 +2894,7 @@
24142894 # define STACK_SKIP 3
24152895
24162896 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2417
- struct ring_buffer *buffer,
2897
+ struct trace_buffer *buffer,
24182898 struct ring_buffer_event *event,
24192899 unsigned long flags, int pc,
24202900 struct pt_regs *regs)
....@@ -2435,134 +2915,11 @@
24352915 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
24362916 */
24372917 void
2438
-trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2918
+trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
24392919 struct ring_buffer_event *event)
24402920 {
24412921 __buffer_unlock_commit(buffer, event);
24422922 }
2443
-
2444
-static void
2445
-trace_process_export(struct trace_export *export,
2446
- struct ring_buffer_event *event)
2447
-{
2448
- struct trace_entry *entry;
2449
- unsigned int size = 0;
2450
-
2451
- entry = ring_buffer_event_data(event);
2452
- size = ring_buffer_event_length(event);
2453
- export->write(export, entry, size);
2454
-}
2455
-
2456
-static DEFINE_MUTEX(ftrace_export_lock);
2457
-
2458
-static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2459
-
2460
-static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2461
-
2462
-static inline void ftrace_exports_enable(void)
2463
-{
2464
- static_branch_enable(&ftrace_exports_enabled);
2465
-}
2466
-
2467
-static inline void ftrace_exports_disable(void)
2468
-{
2469
- static_branch_disable(&ftrace_exports_enabled);
2470
-}
2471
-
2472
-void ftrace_exports(struct ring_buffer_event *event)
2473
-{
2474
- struct trace_export *export;
2475
-
2476
- preempt_disable_notrace();
2477
-
2478
- export = rcu_dereference_raw_notrace(ftrace_exports_list);
2479
- while (export) {
2480
- trace_process_export(export, event);
2481
- export = rcu_dereference_raw_notrace(export->next);
2482
- }
2483
-
2484
- preempt_enable_notrace();
2485
-}
2486
-
2487
-static inline void
2488
-add_trace_export(struct trace_export **list, struct trace_export *export)
2489
-{
2490
- rcu_assign_pointer(export->next, *list);
2491
- /*
2492
- * We are entering export into the list but another
2493
- * CPU might be walking that list. We need to make sure
2494
- * the export->next pointer is valid before another CPU sees
2495
- * the export pointer included into the list.
2496
- */
2497
- rcu_assign_pointer(*list, export);
2498
-}
2499
-
2500
-static inline int
2501
-rm_trace_export(struct trace_export **list, struct trace_export *export)
2502
-{
2503
- struct trace_export **p;
2504
-
2505
- for (p = list; *p != NULL; p = &(*p)->next)
2506
- if (*p == export)
2507
- break;
2508
-
2509
- if (*p != export)
2510
- return -1;
2511
-
2512
- rcu_assign_pointer(*p, (*p)->next);
2513
-
2514
- return 0;
2515
-}
2516
-
2517
-static inline void
2518
-add_ftrace_export(struct trace_export **list, struct trace_export *export)
2519
-{
2520
- if (*list == NULL)
2521
- ftrace_exports_enable();
2522
-
2523
- add_trace_export(list, export);
2524
-}
2525
-
2526
-static inline int
2527
-rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2528
-{
2529
- int ret;
2530
-
2531
- ret = rm_trace_export(list, export);
2532
- if (*list == NULL)
2533
- ftrace_exports_disable();
2534
-
2535
- return ret;
2536
-}
2537
-
2538
-int register_ftrace_export(struct trace_export *export)
2539
-{
2540
- if (WARN_ON_ONCE(!export->write))
2541
- return -1;
2542
-
2543
- mutex_lock(&ftrace_export_lock);
2544
-
2545
- add_ftrace_export(&ftrace_exports_list, export);
2546
-
2547
- mutex_unlock(&ftrace_export_lock);
2548
-
2549
- return 0;
2550
-}
2551
-EXPORT_SYMBOL_GPL(register_ftrace_export);
2552
-
2553
-int unregister_ftrace_export(struct trace_export *export)
2554
-{
2555
- int ret;
2556
-
2557
- mutex_lock(&ftrace_export_lock);
2558
-
2559
- ret = rm_ftrace_export(&ftrace_exports_list, export);
2560
-
2561
- mutex_unlock(&ftrace_export_lock);
2562
-
2563
- return ret;
2564
-}
2565
-EXPORT_SYMBOL_GPL(unregister_ftrace_export);
25662923
25672924 void
25682925 trace_function(struct trace_array *tr,
....@@ -2570,7 +2927,7 @@
25702927 int pc)
25712928 {
25722929 struct trace_event_call *call = &event_function;
2573
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
2930
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
25742931 struct ring_buffer_event *event;
25752932 struct ftrace_entry *entry;
25762933
....@@ -2583,35 +2940,41 @@
25832940 entry->parent_ip = parent_ip;
25842941
25852942 if (!call_filter_check_discard(call, entry, buffer, event)) {
2586
- if (static_branch_unlikely(&ftrace_exports_enabled))
2587
- ftrace_exports(event);
2943
+ if (static_branch_unlikely(&trace_function_exports_enabled))
2944
+ ftrace_exports(event, TRACE_EXPORT_FUNCTION);
25882945 __buffer_unlock_commit(buffer, event);
25892946 }
25902947 }
25912948
25922949 #ifdef CONFIG_STACKTRACE
25932950
2594
-#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2951
+/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2952
+#define FTRACE_KSTACK_NESTING 4
2953
+
2954
+#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2955
+
25952956 struct ftrace_stack {
2596
- unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2957
+ unsigned long calls[FTRACE_KSTACK_ENTRIES];
25972958 };
25982959
2599
-static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2960
+
2961
+struct ftrace_stacks {
2962
+ struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2963
+};
2964
+
2965
+static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
26002966 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
26012967
2602
-static void __ftrace_trace_stack(struct ring_buffer *buffer,
2968
+static void __ftrace_trace_stack(struct trace_buffer *buffer,
26032969 unsigned long flags,
26042970 int skip, int pc, struct pt_regs *regs)
26052971 {
26062972 struct trace_event_call *call = &event_kernel_stack;
26072973 struct ring_buffer_event *event;
2974
+ unsigned int size, nr_entries;
2975
+ struct ftrace_stack *fstack;
26082976 struct stack_entry *entry;
2609
- struct stack_trace trace;
2610
- int use_stack;
2611
- int size = FTRACE_STACK_ENTRIES;
2612
-
2613
- trace.nr_entries = 0;
2614
- trace.skip = skip;
2977
+ int stackidx;
26152978
26162979 /*
26172980 * Add one, for this function and the call to save_stack_trace()
....@@ -2619,43 +2982,37 @@
26192982 */
26202983 #ifndef CONFIG_UNWINDER_ORC
26212984 if (!regs)
2622
- trace.skip++;
2985
+ skip++;
26232986 #endif
26242987
2625
- /*
2626
- * Since events can happen in NMIs there's no safe way to
2627
- * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2628
- * or NMI comes in, it will just have to use the default
2629
- * FTRACE_STACK_SIZE.
2630
- */
26312988 preempt_disable_notrace();
26322989
2633
- use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
2990
+ stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2991
+
2992
+ /* This should never happen. If it does, yell once and skip */
2993
+ if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
2994
+ goto out;
2995
+
26342996 /*
2635
- * We don't need any atomic variables, just a barrier.
2636
- * If an interrupt comes in, we don't care, because it would
2637
- * have exited and put the counter back to what we want.
2638
- * We just need a barrier to keep gcc from moving things
2639
- * around.
2997
+ * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2998
+ * interrupt will either see the value pre increment or post
2999
+ * increment. If the interrupt happens pre increment it will have
3000
+ * restored the counter when it returns. We just need a barrier to
3001
+ * keep gcc from moving things around.
26403002 */
26413003 barrier();
2642
- if (use_stack == 1) {
2643
- trace.entries = this_cpu_ptr(ftrace_stack.calls);
2644
- trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
26453004
2646
- if (regs)
2647
- save_stack_trace_regs(regs, &trace);
2648
- else
2649
- save_stack_trace(&trace);
3005
+ fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
3006
+ size = ARRAY_SIZE(fstack->calls);
26503007
2651
- if (trace.nr_entries > size)
2652
- size = trace.nr_entries;
2653
- } else
2654
- /* From now on, use_stack is a boolean */
2655
- use_stack = 0;
3008
+ if (regs) {
3009
+ nr_entries = stack_trace_save_regs(regs, fstack->calls,
3010
+ size, skip);
3011
+ } else {
3012
+ nr_entries = stack_trace_save(fstack->calls, size, skip);
3013
+ }
26563014
2657
- size *= sizeof(unsigned long);
2658
-
3015
+ size = nr_entries * sizeof(unsigned long);
26593016 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
26603017 (sizeof(*entry) - sizeof(entry->caller)) + size,
26613018 flags, pc);
....@@ -2663,21 +3020,8 @@
26633020 goto out;
26643021 entry = ring_buffer_event_data(event);
26653022
2666
- memset(&entry->caller, 0, size);
2667
-
2668
- if (use_stack)
2669
- memcpy(&entry->caller, trace.entries,
2670
- trace.nr_entries * sizeof(unsigned long));
2671
- else {
2672
- trace.max_entries = FTRACE_STACK_ENTRIES;
2673
- trace.entries = entry->caller;
2674
- if (regs)
2675
- save_stack_trace_regs(regs, &trace);
2676
- else
2677
- save_stack_trace(&trace);
2678
- }
2679
-
2680
- entry->size = trace.nr_entries;
3023
+ memcpy(&entry->caller, fstack->calls, size);
3024
+ entry->size = nr_entries;
26813025
26823026 if (!call_filter_check_discard(call, entry, buffer, event))
26833027 __buffer_unlock_commit(buffer, event);
....@@ -2691,7 +3035,7 @@
26913035 }
26923036
26933037 static inline void ftrace_trace_stack(struct trace_array *tr,
2694
- struct ring_buffer *buffer,
3038
+ struct trace_buffer *buffer,
26953039 unsigned long flags,
26963040 int skip, int pc, struct pt_regs *regs)
26973041 {
....@@ -2704,7 +3048,7 @@
27043048 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
27053049 int pc)
27063050 {
2707
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
3051
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
27083052
27093053 if (rcu_is_watching()) {
27103054 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
....@@ -2742,20 +3086,21 @@
27423086 /* Skip 1 to skip this function. */
27433087 skip++;
27443088 #endif
2745
- __ftrace_trace_stack(global_trace.trace_buffer.buffer,
3089
+ __ftrace_trace_stack(global_trace.array_buffer.buffer,
27463090 flags, skip, preempt_count(), NULL);
27473091 }
3092
+EXPORT_SYMBOL_GPL(trace_dump_stack);
27483093
3094
+#ifdef CONFIG_USER_STACKTRACE_SUPPORT
27493095 static DEFINE_PER_CPU(int, user_stack_count);
27503096
2751
-void
3097
+static void
27523098 ftrace_trace_userstack(struct trace_array *tr,
2753
- struct ring_buffer *buffer, unsigned long flags, int pc)
3099
+ struct trace_buffer *buffer, unsigned long flags, int pc)
27543100 {
27553101 struct trace_event_call *call = &event_user_stack;
27563102 struct ring_buffer_event *event;
27573103 struct userstack_entry *entry;
2758
- struct stack_trace trace;
27593104
27603105 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
27613106 return;
....@@ -2786,12 +3131,7 @@
27863131 entry->tgid = current->tgid;
27873132 memset(&entry->caller, 0, sizeof(entry->caller));
27883133
2789
- trace.nr_entries = 0;
2790
- trace.max_entries = FTRACE_STACK_ENTRIES;
2791
- trace.skip = 0;
2792
- trace.entries = entry->caller;
2793
-
2794
- save_stack_trace_user(&trace);
3134
+ stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
27953135 if (!call_filter_check_discard(call, entry, buffer, event))
27963136 __buffer_unlock_commit(buffer, event);
27973137
....@@ -2800,13 +3140,13 @@
28003140 out:
28013141 preempt_enable();
28023142 }
2803
-
2804
-#ifdef UNUSED
2805
-static void __trace_userstack(struct trace_array *tr, unsigned long flags)
3143
+#else /* CONFIG_USER_STACKTRACE_SUPPORT */
3144
+static void ftrace_trace_userstack(struct trace_array *tr,
3145
+ struct trace_buffer *buffer,
3146
+ unsigned long flags, int pc)
28063147 {
2807
- ftrace_trace_userstack(tr, flags, preempt_count());
28083148 }
2809
-#endif /* UNUSED */
3149
+#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
28103150
28113151 #endif /* CONFIG_STACKTRACE */
28123152
....@@ -2847,8 +3187,11 @@
28473187 {
28483188 struct trace_buffer_struct __percpu *buffers;
28493189
3190
+ if (trace_percpu_buffer)
3191
+ return 0;
3192
+
28503193 buffers = alloc_percpu(struct trace_buffer_struct);
2851
- if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3194
+ if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
28523195 return -ENOMEM;
28533196
28543197 trace_percpu_buffer = buffers;
....@@ -2893,9 +3236,10 @@
28933236 * directly here. If the global_trace.buffer is already
28943237 * allocated here, then this was called by module code.
28953238 */
2896
- if (global_trace.trace_buffer.buffer)
3239
+ if (global_trace.array_buffer.buffer)
28973240 tracing_start_cmdline_record();
28983241 }
3242
+EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
28993243
29003244 void trace_printk_start_comm(void)
29013245 {
....@@ -2918,13 +3262,15 @@
29183262
29193263 /**
29203264 * trace_vbprintk - write binary msg to tracing buffer
2921
- *
3265
+ * @ip: The address of the caller
3266
+ * @fmt: The string format to write to the buffer
3267
+ * @args: Arguments for @fmt
29223268 */
29233269 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
29243270 {
29253271 struct trace_event_call *call = &event_bprint;
29263272 struct ring_buffer_event *event;
2927
- struct ring_buffer *buffer;
3273
+ struct trace_buffer *buffer;
29283274 struct trace_array *tr = &global_trace;
29293275 struct bprint_entry *entry;
29303276 unsigned long flags;
....@@ -2949,11 +3295,12 @@
29493295 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
29503296
29513297 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2952
- goto out;
3298
+ goto out_put;
29533299
29543300 local_save_flags(flags);
29553301 size = sizeof(*entry) + sizeof(u32) * len;
2956
- buffer = tr->trace_buffer.buffer;
3302
+ buffer = tr->array_buffer.buffer;
3303
+ ring_buffer_nest_start(buffer);
29573304 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
29583305 flags, pc);
29593306 if (!event)
....@@ -2969,6 +3316,8 @@
29693316 }
29703317
29713318 out:
3319
+ ring_buffer_nest_end(buffer);
3320
+out_put:
29723321 put_trace_buf();
29733322
29743323 out_nobuffer:
....@@ -2981,7 +3330,7 @@
29813330
29823331 __printf(3, 0)
29833332 static int
2984
-__trace_array_vprintk(struct ring_buffer *buffer,
3333
+__trace_array_vprintk(struct trace_buffer *buffer,
29853334 unsigned long ip, const char *fmt, va_list args)
29863335 {
29873336 struct trace_event_call *call = &event_print;
....@@ -3011,6 +3360,7 @@
30113360
30123361 local_save_flags(flags);
30133362 size = sizeof(*entry) + len + 1;
3363
+ ring_buffer_nest_start(buffer);
30143364 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
30153365 flags, pc);
30163366 if (!event)
....@@ -3025,6 +3375,7 @@
30253375 }
30263376
30273377 out:
3378
+ ring_buffer_nest_end(buffer);
30283379 put_trace_buf();
30293380
30303381 out_nobuffer:
....@@ -3038,9 +3389,29 @@
30383389 int trace_array_vprintk(struct trace_array *tr,
30393390 unsigned long ip, const char *fmt, va_list args)
30403391 {
3041
- return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3392
+ return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
30423393 }
30433394
3395
+/**
3396
+ * trace_array_printk - Print a message to a specific instance
3397
+ * @tr: The instance trace_array descriptor
3398
+ * @ip: The instruction pointer that this is called from.
3399
+ * @fmt: The format to print (printf format)
3400
+ *
3401
+ * If a subsystem sets up its own instance, they have the right to
3402
+ * printk strings into their tracing instance buffer using this
3403
+ * function. Note, this function will not write into the top level
3404
+ * buffer (use trace_printk() for that), as writing into the top level
3405
+ * buffer should only have events that can be individually disabled.
3406
+ * trace_printk() is only used for debugging a kernel, and should not
3407
+ * be ever encorporated in normal use.
3408
+ *
3409
+ * trace_array_printk() can be used, as it will not add noise to the
3410
+ * top level tracing buffer.
3411
+ *
3412
+ * Note, trace_array_init_printk() must be called on @tr before this
3413
+ * can be used.
3414
+ */
30443415 __printf(3, 0)
30453416 int trace_array_printk(struct trace_array *tr,
30463417 unsigned long ip, const char *fmt, ...)
....@@ -3048,20 +3419,46 @@
30483419 int ret;
30493420 va_list ap;
30503421
3051
- if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3052
- return 0;
3053
-
30543422 if (!tr)
30553423 return -ENOENT;
3424
+
3425
+ /* This is only allowed for created instances */
3426
+ if (tr == &global_trace)
3427
+ return 0;
3428
+
3429
+ if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3430
+ return 0;
30563431
30573432 va_start(ap, fmt);
30583433 ret = trace_array_vprintk(tr, ip, fmt, ap);
30593434 va_end(ap);
30603435 return ret;
30613436 }
3437
+EXPORT_SYMBOL_GPL(trace_array_printk);
3438
+
3439
+/**
3440
+ * trace_array_init_printk - Initialize buffers for trace_array_printk()
3441
+ * @tr: The trace array to initialize the buffers for
3442
+ *
3443
+ * As trace_array_printk() only writes into instances, they are OK to
3444
+ * have in the kernel (unlike trace_printk()). This needs to be called
3445
+ * before trace_array_printk() can be used on a trace_array.
3446
+ */
3447
+int trace_array_init_printk(struct trace_array *tr)
3448
+{
3449
+ if (!tr)
3450
+ return -ENOENT;
3451
+
3452
+ /* This is only allowed for created instances */
3453
+ if (tr == &global_trace)
3454
+ return -EINVAL;
3455
+
3456
+ return alloc_percpu_trace_buffer();
3457
+}
3458
+EXPORT_SYMBOL_GPL(trace_array_init_printk);
30623459
30633460 __printf(3, 4)
3064
-int trace_array_printk_buf(struct ring_buffer *buffer,
3461
+int trace_array_printk_buf(struct trace_buffer *buffer,
30653462 unsigned long ip, const char *fmt, ...)
30663463 {
30673464 int ret;
....@@ -3089,7 +3486,7 @@
30893486
30903487 iter->idx++;
30913488 if (buf_iter)
3092
- ring_buffer_read(buf_iter, NULL);
3489
+ ring_buffer_iter_advance(buf_iter);
30933490 }
30943491
30953492 static struct trace_entry *
....@@ -3099,11 +3496,15 @@
30993496 struct ring_buffer_event *event;
31003497 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
31013498
3102
- if (buf_iter)
3499
+ if (buf_iter) {
31033500 event = ring_buffer_iter_peek(buf_iter, ts);
3104
- else
3105
- event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
3501
+ if (lost_events)
3502
+ *lost_events = ring_buffer_iter_dropped(buf_iter) ?
3503
+ (unsigned long)-1 : 0;
3504
+ } else {
3505
+ event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
31063506 lost_events);
3507
+ }
31073508
31083509 if (event) {
31093510 iter->ent_size = ring_buffer_event_length(event);
....@@ -3117,7 +3518,7 @@
31173518 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
31183519 unsigned long *missing_events, u64 *ent_ts)
31193520 {
3120
- struct ring_buffer *buffer = iter->trace_buffer->buffer;
3521
+ struct trace_buffer *buffer = iter->array_buffer->buffer;
31213522 struct trace_entry *ent, *next = NULL;
31223523 unsigned long lost_events = 0, next_lost = 0;
31233524 int cpu_file = iter->cpu_file;
....@@ -3173,11 +3574,53 @@
31733574 return next;
31743575 }
31753576
3577
+#define STATIC_TEMP_BUF_SIZE 128
3578
+static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
3579
+
31763580 /* Find the next real entry, without updating the iterator itself */
31773581 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
31783582 int *ent_cpu, u64 *ent_ts)
31793583 {
3180
- return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3584
+ /* __find_next_entry will reset ent_size */
3585
+ int ent_size = iter->ent_size;
3586
+ struct trace_entry *entry;
3587
+
3588
+ /*
3589
+ * If called from ftrace_dump(), then the iter->temp buffer
3590
+ * will be the static_temp_buf and not created from kmalloc.
3591
+ * If the entry size is greater than the buffer, we can
3592
+ * not save it. Just return NULL in that case. This is only
3593
+ * used to add markers when two consecutive events' time
3594
+ * stamps have a large delta. See trace_print_lat_context()
3595
+ */
3596
+ if (iter->temp == static_temp_buf &&
3597
+ STATIC_TEMP_BUF_SIZE < ent_size)
3598
+ return NULL;
3599
+
3600
+ /*
3601
+ * The __find_next_entry() may call peek_next_entry(), which may
3602
+ * call ring_buffer_peek() that may make the contents of iter->ent
3603
+ * undefined. Need to copy iter->ent now.
3604
+ */
3605
+ if (iter->ent && iter->ent != iter->temp) {
3606
+ if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3607
+ !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
3608
+ void *temp;
3609
+ temp = kmalloc(iter->ent_size, GFP_KERNEL);
3610
+ if (!temp)
3611
+ return NULL;
3612
+ kfree(iter->temp);
3613
+ iter->temp = temp;
3614
+ iter->temp_size = iter->ent_size;
3615
+ }
3616
+ memcpy(iter->temp, iter->ent, iter->ent_size);
3617
+ iter->ent = iter->temp;
3618
+ }
3619
+ entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3620
+ /* Put back the original ent_size */
3621
+ iter->ent_size = ent_size;
3622
+
3623
+ return entry;
31813624 }
31823625
31833626 /* Find the next real entry, and increment the iterator to the next entry */
....@@ -3194,7 +3637,7 @@
31943637
31953638 static void trace_consume(struct trace_iterator *iter)
31963639 {
3197
- ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
3640
+ ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
31983641 &iter->lost_events);
31993642 }
32003643
....@@ -3227,12 +3670,11 @@
32273670
32283671 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
32293672 {
3230
- struct ring_buffer_event *event;
32313673 struct ring_buffer_iter *buf_iter;
32323674 unsigned long entries = 0;
32333675 u64 ts;
32343676
3235
- per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
3677
+ per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
32363678
32373679 buf_iter = trace_buffer_iter(iter, cpu);
32383680 if (!buf_iter)
....@@ -3245,14 +3687,14 @@
32453687 * that a reset never took place on a cpu. This is evident
32463688 * by the timestamp being before the start of the buffer.
32473689 */
3248
- while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
3249
- if (ts >= iter->trace_buffer->time_start)
3690
+ while (ring_buffer_iter_peek(buf_iter, &ts)) {
3691
+ if (ts >= iter->array_buffer->time_start)
32503692 break;
32513693 entries++;
3252
- ring_buffer_read(buf_iter, NULL);
3694
+ ring_buffer_iter_advance(buf_iter);
32533695 }
32543696
3255
- per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
3697
+ per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
32563698 }
32573699
32583700 /*
....@@ -3331,46 +3773,81 @@
33313773 }
33323774
33333775 static void
3334
-get_total_entries(struct trace_buffer *buf,
3335
- unsigned long *total, unsigned long *entries)
3776
+get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
3777
+ unsigned long *entries, int cpu)
33363778 {
33373779 unsigned long count;
3780
+
3781
+ count = ring_buffer_entries_cpu(buf->buffer, cpu);
3782
+ /*
3783
+ * If this buffer has skipped entries, then we hold all
3784
+ * entries for the trace and we need to ignore the
3785
+ * ones before the time stamp.
3786
+ */
3787
+ if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3788
+ count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3789
+ /* total is the same as the entries */
3790
+ *total = count;
3791
+ } else
3792
+ *total = count +
3793
+ ring_buffer_overrun_cpu(buf->buffer, cpu);
3794
+ *entries = count;
3795
+}
3796
+
3797
+static void
3798
+get_total_entries(struct array_buffer *buf,
3799
+ unsigned long *total, unsigned long *entries)
3800
+{
3801
+ unsigned long t, e;
33383802 int cpu;
33393803
33403804 *total = 0;
33413805 *entries = 0;
33423806
33433807 for_each_tracing_cpu(cpu) {
3344
- count = ring_buffer_entries_cpu(buf->buffer, cpu);
3345
- /*
3346
- * If this buffer has skipped entries, then we hold all
3347
- * entries for the trace and we need to ignore the
3348
- * ones before the time stamp.
3349
- */
3350
- if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3351
- count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3352
- /* total is the same as the entries */
3353
- *total += count;
3354
- } else
3355
- *total += count +
3356
- ring_buffer_overrun_cpu(buf->buffer, cpu);
3357
- *entries += count;
3808
+ get_total_entries_cpu(buf, &t, &e, cpu);
3809
+ *total += t;
3810
+ *entries += e;
33583811 }
3812
+}
3813
+
3814
+unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3815
+{
3816
+ unsigned long total, entries;
3817
+
3818
+ if (!tr)
3819
+ tr = &global_trace;
3820
+
3821
+ get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
3822
+
3823
+ return entries;
3824
+}
3825
+
3826
+unsigned long trace_total_entries(struct trace_array *tr)
3827
+{
3828
+ unsigned long total, entries;
3829
+
3830
+ if (!tr)
3831
+ tr = &global_trace;
3832
+
3833
+ get_total_entries(&tr->array_buffer, &total, &entries);
3834
+
3835
+ return entries;
33593836 }
33603837
33613838 static void print_lat_help_header(struct seq_file *m)
33623839 {
3363
- seq_puts(m, "# _------=> CPU# \n"
3364
- "# / _-----=> irqs-off \n"
3365
- "# | / _----=> need-resched \n"
3366
- "# || / _---=> hardirq/softirq \n"
3367
- "# ||| / _--=> preempt-depth \n"
3368
- "# |||| / delay \n"
3369
- "# cmd pid ||||| time | caller \n"
3370
- "# \\ / ||||| \\ | / \n");
3840
+ seq_puts(m, "# _------=> CPU# \n"
3841
+ "# / _-----=> irqs-off \n"
3842
+ "# | / _----=> need-resched \n"
3843
+ "# || / _---=> hardirq/softirq \n"
3844
+ "# ||| / _--=> preempt-depth \n"
3845
+ "# |||| / delay \n"
3846
+ "# cmd pid ||||| time | caller \n"
3847
+ "# \\ / ||||| \\ | / \n");
33713848 }
33723849
3373
-static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
3850
+static void print_event_info(struct array_buffer *buf, struct seq_file *m)
33743851 {
33753852 unsigned long total;
33763853 unsigned long entries;
....@@ -3381,47 +3858,40 @@
33813858 seq_puts(m, "#\n");
33823859 }
33833860
3384
-static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3861
+static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
33853862 unsigned int flags)
33863863 {
33873864 bool tgid = flags & TRACE_ITER_RECORD_TGID;
33883865
33893866 print_event_info(buf, m);
33903867
3391
- seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3392
- seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
3868
+ seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
3869
+ seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
33933870 }
33943871
3395
-static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3872
+static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
33963873 unsigned int flags)
33973874 {
33983875 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3399
- const char tgid_space[] = " ";
3400
- const char space[] = " ";
3876
+ const char *space = " ";
3877
+ int prec = tgid ? 12 : 2;
34013878
34023879 print_event_info(buf, m);
34033880
3404
- seq_printf(m, "# %s _-----=> irqs-off\n",
3405
- tgid ? tgid_space : space);
3406
- seq_printf(m, "# %s / _----=> need-resched\n",
3407
- tgid ? tgid_space : space);
3408
- seq_printf(m, "# %s| / _---=> hardirq/softirq\n",
3409
- tgid ? tgid_space : space);
3410
- seq_printf(m, "# %s|| / _--=> preempt-depth\n",
3411
- tgid ? tgid_space : space);
3412
- seq_printf(m, "# %s||| / delay\n",
3413
- tgid ? tgid_space : space);
3414
- seq_printf(m, "# TASK-PID %sCPU# |||| TIMESTAMP FUNCTION\n",
3415
- tgid ? " TGID " : space);
3416
- seq_printf(m, "# | | %s | |||| | |\n",
3417
- tgid ? " | " : space);
3881
+ seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3882
+ seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3883
+ seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3884
+ seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3885
+ seq_printf(m, "# %.*s||| / delay\n", prec, space);
3886
+ seq_printf(m, "# TASK-PID %.*s CPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3887
+ seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
34183888 }
34193889
34203890 void
34213891 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
34223892 {
34233893 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
3424
- struct trace_buffer *buf = iter->trace_buffer;
3894
+ struct array_buffer *buf = iter->array_buffer;
34253895 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
34263896 struct tracer *type = iter->trace;
34273897 unsigned long entries;
....@@ -3448,6 +3918,8 @@
34483918 "desktop",
34493919 #elif defined(CONFIG_PREEMPT)
34503920 "preempt",
3921
+#elif defined(CONFIG_PREEMPT_RT)
3922
+ "preempt_rt",
34513923 #else
34523924 "unknown",
34533925 #endif
....@@ -3494,7 +3966,7 @@
34943966 cpumask_test_cpu(iter->cpu, iter->started))
34953967 return;
34963968
3497
- if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
3969
+ if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
34983970 return;
34993971
35003972 if (cpumask_available(iter->started))
....@@ -3628,7 +4100,7 @@
36284100 if (!ring_buffer_iter_empty(buf_iter))
36294101 return 0;
36304102 } else {
3631
- if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
4103
+ if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
36324104 return 0;
36334105 }
36344106 return 1;
....@@ -3640,7 +4112,7 @@
36404112 if (!ring_buffer_iter_empty(buf_iter))
36414113 return 0;
36424114 } else {
3643
- if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
4115
+ if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
36444116 return 0;
36454117 }
36464118 }
....@@ -3656,8 +4128,12 @@
36564128 enum print_line_t ret;
36574129
36584130 if (iter->lost_events) {
3659
- trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3660
- iter->cpu, iter->lost_events);
4131
+ if (iter->lost_events == (unsigned long)-1)
4132
+ trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4133
+ iter->cpu);
4134
+ else
4135
+ trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4136
+ iter->cpu, iter->lost_events);
36614137 if (trace_seq_has_overflowed(&iter->seq))
36624138 return TRACE_TYPE_PARTIAL_LINE;
36634139 }
....@@ -3730,10 +4206,10 @@
37304206 } else {
37314207 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
37324208 if (trace_flags & TRACE_ITER_IRQ_INFO)
3733
- print_func_help_header_irq(iter->trace_buffer,
4209
+ print_func_help_header_irq(iter->array_buffer,
37344210 m, trace_flags);
37354211 else
3736
- print_func_help_header(iter->trace_buffer, m,
4212
+ print_func_help_header(iter->array_buffer, m,
37374213 trace_flags);
37384214 }
37394215 }
....@@ -3873,6 +4349,18 @@
38734349 goto release;
38744350
38754351 /*
4352
+ * trace_find_next_entry() may need to save off iter->ent.
4353
+ * It will place it into the iter->temp buffer. As most
4354
+ * events are less than 128, allocate a buffer of that size.
4355
+ * If one is greater, then trace_find_next_entry() will
4356
+ * allocate a new buffer to adjust for the bigger iter->ent.
4357
+ * It's not critical if it fails to get allocated here.
4358
+ */
4359
+ iter->temp = kmalloc(128, GFP_KERNEL);
4360
+ if (iter->temp)
4361
+ iter->temp_size = 128;
4362
+
4363
+ /*
38764364 * We make a copy of the current tracer to avoid concurrent
38774365 * changes on it while we are reading.
38784366 */
....@@ -3891,35 +4379,38 @@
38914379 #ifdef CONFIG_TRACER_MAX_TRACE
38924380 /* Currently only the top directory has a snapshot */
38934381 if (tr->current_trace->print_max || snapshot)
3894
- iter->trace_buffer = &tr->max_buffer;
4382
+ iter->array_buffer = &tr->max_buffer;
38954383 else
38964384 #endif
3897
- iter->trace_buffer = &tr->trace_buffer;
4385
+ iter->array_buffer = &tr->array_buffer;
38984386 iter->snapshot = snapshot;
38994387 iter->pos = -1;
39004388 iter->cpu_file = tracing_get_cpu(inode);
39014389 mutex_init(&iter->mutex);
39024390
39034391 /* Notify the tracer early; before we stop tracing. */
3904
- if (iter->trace && iter->trace->open)
4392
+ if (iter->trace->open)
39054393 iter->trace->open(iter);
39064394
39074395 /* Annotate start of buffers if we had overruns */
3908
- if (ring_buffer_overruns(iter->trace_buffer->buffer))
4396
+ if (ring_buffer_overruns(iter->array_buffer->buffer))
39094397 iter->iter_flags |= TRACE_FILE_ANNOTATE;
39104398
39114399 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
39124400 if (trace_clocks[tr->clock_id].in_ns)
39134401 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
39144402
3915
- /* stop the trace while dumping if we are not opening "snapshot" */
3916
- if (!iter->snapshot)
4403
+ /*
4404
+ * If pause-on-trace is enabled, then stop the trace while
4405
+ * dumping, unless this is the "snapshot" file
4406
+ */
4407
+ if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
39174408 tracing_stop_tr(tr);
39184409
39194410 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
39204411 for_each_tracing_cpu(cpu) {
39214412 iter->buffer_iter[cpu] =
3922
- ring_buffer_read_prepare(iter->trace_buffer->buffer,
4413
+ ring_buffer_read_prepare(iter->array_buffer->buffer,
39234414 cpu, GFP_KERNEL);
39244415 }
39254416 ring_buffer_read_prepare_sync();
....@@ -3930,7 +4421,7 @@
39304421 } else {
39314422 cpu = iter->cpu_file;
39324423 iter->buffer_iter[cpu] =
3933
- ring_buffer_read_prepare(iter->trace_buffer->buffer,
4424
+ ring_buffer_read_prepare(iter->array_buffer->buffer,
39344425 cpu, GFP_KERNEL);
39354426 ring_buffer_read_prepare_sync();
39364427 ring_buffer_read_start(iter->buffer_iter[cpu]);
....@@ -3944,6 +4435,7 @@
39444435 fail:
39454436 mutex_unlock(&trace_types_lock);
39464437 kfree(iter->trace);
4438
+ kfree(iter->temp);
39474439 kfree(iter->buffer_iter);
39484440 release:
39494441 seq_release_private(inode, file);
....@@ -3952,8 +4444,11 @@
39524444
39534445 int tracing_open_generic(struct inode *inode, struct file *filp)
39544446 {
3955
- if (tracing_disabled)
3956
- return -ENODEV;
4447
+ int ret;
4448
+
4449
+ ret = tracing_check_open_get_tr(NULL);
4450
+ if (ret)
4451
+ return ret;
39574452
39584453 filp->private_data = inode->i_private;
39594454 return 0;
....@@ -3968,15 +4463,14 @@
39684463 * Open and update trace_array ref count.
39694464 * Must have the current trace_array passed to it.
39704465 */
3971
-static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
4466
+int tracing_open_generic_tr(struct inode *inode, struct file *filp)
39724467 {
39734468 struct trace_array *tr = inode->i_private;
4469
+ int ret;
39744470
3975
- if (tracing_disabled)
3976
- return -ENODEV;
3977
-
3978
- if (trace_array_get(tr) < 0)
3979
- return -ENODEV;
4471
+ ret = tracing_check_open_get_tr(tr);
4472
+ if (ret)
4473
+ return ret;
39804474
39814475 filp->private_data = inode->i_private;
39824476
....@@ -4007,7 +4501,7 @@
40074501 if (iter->trace && iter->trace->close)
40084502 iter->trace->close(iter);
40094503
4010
- if (!iter->snapshot)
4504
+ if (!iter->snapshot && tr->stop_count)
40114505 /* reenable tracing if it was previously enabled */
40124506 tracing_start_tr(tr);
40134507
....@@ -4017,6 +4511,7 @@
40174511
40184512 mutex_destroy(&iter->mutex);
40194513 free_cpumask_var(iter->started);
4514
+ kfree(iter->temp);
40204515 kfree(iter->trace);
40214516 kfree(iter->buffer_iter);
40224517 seq_release_private(inode, file);
....@@ -4045,15 +4540,16 @@
40454540 {
40464541 struct trace_array *tr = inode->i_private;
40474542 struct trace_iterator *iter;
4048
- int ret = 0;
4543
+ int ret;
40494544
4050
- if (trace_array_get(tr) < 0)
4051
- return -ENODEV;
4545
+ ret = tracing_check_open_get_tr(tr);
4546
+ if (ret)
4547
+ return ret;
40524548
40534549 /* If this file was open for write, then erase contents */
40544550 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
40554551 int cpu = tracing_get_cpu(inode);
4056
- struct trace_buffer *trace_buf = &tr->trace_buffer;
4552
+ struct array_buffer *trace_buf = &tr->array_buffer;
40574553
40584554 #ifdef CONFIG_TRACER_MAX_TRACE
40594555 if (tr->current_trace->print_max)
....@@ -4063,7 +4559,7 @@
40634559 if (cpu == RING_BUFFER_ALL_CPUS)
40644560 tracing_reset_online_cpus(trace_buf);
40654561 else
4066
- tracing_reset(trace_buf, cpu);
4562
+ tracing_reset_cpu(trace_buf, cpu);
40674563 }
40684564
40694565 if (file->f_mode & FMODE_READ) {
....@@ -4164,11 +4660,9 @@
41644660 struct seq_file *m;
41654661 int ret;
41664662
4167
- if (tracing_disabled)
4168
- return -ENODEV;
4169
-
4170
- if (trace_array_get(tr) < 0)
4171
- return -ENODEV;
4663
+ ret = tracing_check_open_get_tr(tr);
4664
+ if (ret)
4665
+ return ret;
41724666
41734667 ret = seq_open(file, &show_traces_seq_ops);
41744668 if (ret) {
....@@ -4252,20 +4746,13 @@
42524746 return count;
42534747 }
42544748
4255
-static ssize_t
4256
-tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4257
- size_t count, loff_t *ppos)
4749
+int tracing_set_cpumask(struct trace_array *tr,
4750
+ cpumask_var_t tracing_cpumask_new)
42584751 {
4259
- struct trace_array *tr = file_inode(filp)->i_private;
4260
- cpumask_var_t tracing_cpumask_new;
4261
- int err, cpu;
4752
+ int cpu;
42624753
4263
- if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4264
- return -ENOMEM;
4265
-
4266
- err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4267
- if (err)
4268
- goto err_unlock;
4754
+ if (!tr)
4755
+ return -EINVAL;
42694756
42704757 local_irq_disable();
42714758 arch_spin_lock(&tr->max_lock);
....@@ -4276,24 +4763,47 @@
42764763 */
42774764 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
42784765 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4279
- atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4280
- ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
4766
+ atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4767
+ ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
42814768 }
42824769 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
42834770 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4284
- atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4285
- ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
4771
+ atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4772
+ ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
42864773 }
42874774 }
42884775 arch_spin_unlock(&tr->max_lock);
42894776 local_irq_enable();
42904777
42914778 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
4779
+
4780
+ return 0;
4781
+}
4782
+
4783
+static ssize_t
4784
+tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4785
+ size_t count, loff_t *ppos)
4786
+{
4787
+ struct trace_array *tr = file_inode(filp)->i_private;
4788
+ cpumask_var_t tracing_cpumask_new;
4789
+ int err;
4790
+
4791
+ if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4792
+ return -ENOMEM;
4793
+
4794
+ err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4795
+ if (err)
4796
+ goto err_free;
4797
+
4798
+ err = tracing_set_cpumask(tr, tracing_cpumask_new);
4799
+ if (err)
4800
+ goto err_free;
4801
+
42924802 free_cpumask_var(tracing_cpumask_new);
42934803
42944804 return count;
42954805
4296
-err_unlock:
4806
+err_free:
42974807 free_cpumask_var(tracing_cpumask_new);
42984808
42994809 return err;
....@@ -4435,7 +4945,7 @@
44354945 ftrace_pid_follow_fork(tr, enabled);
44364946
44374947 if (mask == TRACE_ITER_OVERWRITE) {
4438
- ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
4948
+ ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
44394949 #ifdef CONFIG_TRACER_MAX_TRACE
44404950 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
44414951 #endif
....@@ -4449,19 +4959,21 @@
44494959 return 0;
44504960 }
44514961
4452
-static int trace_set_options(struct trace_array *tr, char *option)
4962
+int trace_set_options(struct trace_array *tr, char *option)
44534963 {
44544964 char *cmp;
44554965 int neg = 0;
44564966 int ret;
44574967 size_t orig_len = strlen(option);
4968
+ int len;
44584969
44594970 cmp = strstrip(option);
44604971
4461
- if (strncmp(cmp, "no", 2) == 0) {
4972
+ len = str_has_prefix(cmp, "no");
4973
+ if (len)
44624974 neg = 1;
4463
- cmp += 2;
4464
- }
4975
+
4976
+ cmp += len;
44654977
44664978 mutex_lock(&event_mutex);
44674979 mutex_lock(&trace_types_lock);
....@@ -4537,11 +5049,9 @@
45375049 struct trace_array *tr = inode->i_private;
45385050 int ret;
45395051
4540
- if (tracing_disabled)
4541
- return -ENODEV;
4542
-
4543
- if (trace_array_get(tr) < 0)
4544
- return -ENODEV;
5052
+ ret = tracing_check_open_get_tr(tr);
5053
+ if (ret)
5054
+ return ret;
45455055
45465056 ret = single_open(file, tracing_trace_options_show, inode->i_private);
45475057 if (ret < 0)
....@@ -4568,6 +5078,7 @@
45685078 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
45695079 " current_tracer\t- function and latency tracers\n"
45705080 " available_tracers\t- list of configured tracers for current_tracer\n"
5081
+ " error_log\t- error log for failed commands (that support it)\n"
45715082 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
45725083 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
45735084 " trace_clock\t\t-change the clock used to order events\n"
....@@ -4588,7 +5099,7 @@
45885099 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
45895100 "\t\t\t Remove sub-buffer with rmdir\n"
45905101 " trace_options\t\t- Set format or modify how tracing happens\n"
4591
- "\t\t\t Disable an option by adding a suffix 'no' to the\n"
5102
+ "\t\t\t Disable an option by prefixing 'no' to the\n"
45925103 "\t\t\t option name\n"
45935104 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
45945105 #ifdef CONFIG_DYNAMIC_FTRACE
....@@ -4632,6 +5143,8 @@
46325143 #ifdef CONFIG_FUNCTION_TRACER
46335144 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
46345145 "\t\t (function)\n"
5146
+ " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5147
+ "\t\t (function)\n"
46355148 #endif
46365149 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
46375150 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
....@@ -4653,31 +5166,49 @@
46535166 "\t\t\t traces\n"
46545167 #endif
46555168 #endif /* CONFIG_STACK_TRACER */
5169
+#ifdef CONFIG_DYNAMIC_EVENTS
5170
+ " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5171
+ "\t\t\t Write into this file to define/undefine new trace events.\n"
5172
+#endif
46565173 #ifdef CONFIG_KPROBE_EVENTS
4657
- " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
5174
+ " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
46585175 "\t\t\t Write into this file to define/undefine new trace events.\n"
46595176 #endif
46605177 #ifdef CONFIG_UPROBE_EVENTS
4661
- " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
5178
+ " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
46625179 "\t\t\t Write into this file to define/undefine new trace events.\n"
46635180 #endif
46645181 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
46655182 "\t accepts: event-definitions (one definition per line)\n"
46665183 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
46675184 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
5185
+#ifdef CONFIG_HIST_TRIGGERS
5186
+ "\t s:[synthetic/]<event> <field> [<field>]\n"
5187
+#endif
46685188 "\t -:[<group>/]<event>\n"
46695189 #ifdef CONFIG_KPROBE_EVENTS
46705190 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4671
- "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
5191
+ "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
46725192 #endif
46735193 #ifdef CONFIG_UPROBE_EVENTS
4674
- "\t place: <path>:<offset>\n"
5194
+ " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
46755195 #endif
46765196 "\t args: <name>=fetcharg[:type]\n"
46775197 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
4678
- "\t $stack<index>, $stack, $retval, $comm\n"
4679
- "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string,\n"
4680
- "\t b<bit-width>@<bit-offset>/<container-size>\n"
5198
+#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
5199
+ "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
5200
+#else
5201
+ "\t $stack<index>, $stack, $retval, $comm,\n"
5202
+#endif
5203
+ "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5204
+ "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
5205
+ "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5206
+ "\t <type>\\[<array-size>\\]\n"
5207
+#ifdef CONFIG_HIST_TRIGGERS
5208
+ "\t field: <stype> <name>;\n"
5209
+ "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5210
+ "\t [unsigned] char/int/long\n"
5211
+#endif
46815212 #endif
46825213 " events/\t\t- Directory containing all trace event subsystems:\n"
46835214 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
....@@ -4730,6 +5261,7 @@
47305261 "\t [:size=#entries]\n"
47315262 "\t [:pause][:continue][:clear]\n"
47325263 "\t [:name=histname1]\n"
5264
+ "\t [:<handler>.<action>]\n"
47335265 "\t [if <filter>]\n\n"
47345266 "\t Note, special fields can be used as well:\n"
47355267 "\t common_timestamp - to record current timestamp\n"
....@@ -4774,8 +5306,26 @@
47745306 "\t unchanged.\n\n"
47755307 "\t The enable_hist and disable_hist triggers can be used to\n"
47765308 "\t have one event conditionally start and stop another event's\n"
4777
- "\t already-attached hist trigger. The syntax is analagous to\n"
4778
- "\t the enable_event and disable_event triggers.\n"
5309
+ "\t already-attached hist trigger. The syntax is analogous to\n"
5310
+ "\t the enable_event and disable_event triggers.\n\n"
5311
+ "\t Hist trigger handlers and actions are executed whenever a\n"
5312
+ "\t a histogram entry is added or updated. They take the form:\n\n"
5313
+ "\t <handler>.<action>\n\n"
5314
+ "\t The available handlers are:\n\n"
5315
+ "\t onmatch(matching.event) - invoke on addition or update\n"
5316
+ "\t onmax(var) - invoke if var exceeds current max\n"
5317
+ "\t onchange(var) - invoke action if var changes\n\n"
5318
+ "\t The available actions are:\n\n"
5319
+ "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5320
+ "\t save(field,...) - save current event fields\n"
5321
+#ifdef CONFIG_TRACER_SNAPSHOT
5322
+ "\t snapshot() - snapshot the trace buffer\n\n"
5323
+#endif
5324
+#ifdef CONFIG_SYNTH_EVENTS
5325
+ " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5326
+ "\t Write into this file to define/undefine new synthetic events.\n"
5327
+ "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
5328
+#endif
47795329 #endif
47805330 ;
47815331
....@@ -4833,8 +5383,11 @@
48335383
48345384 static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
48355385 {
4836
- if (tracing_disabled)
4837
- return -ENODEV;
5386
+ int ret;
5387
+
5388
+ ret = tracing_check_open_get_tr(NULL);
5389
+ if (ret)
5390
+ return ret;
48385391
48395392 return seq_open(filp, &tracing_saved_tgids_seq_ops);
48405393 }
....@@ -4910,8 +5463,11 @@
49105463
49115464 static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
49125465 {
4913
- if (tracing_disabled)
4914
- return -ENODEV;
5466
+ int ret;
5467
+
5468
+ ret = tracing_check_open_get_tr(NULL);
5469
+ if (ret)
5470
+ return ret;
49155471
49165472 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
49175473 }
....@@ -4930,9 +5486,11 @@
49305486 char buf[64];
49315487 int r;
49325488
5489
+ preempt_disable();
49335490 arch_spin_lock(&trace_cmdline_lock);
49345491 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
49355492 arch_spin_unlock(&trace_cmdline_lock);
5493
+ preempt_enable();
49365494
49375495 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
49385496 }
....@@ -4957,10 +5515,12 @@
49575515 return -ENOMEM;
49585516 }
49595517
5518
+ preempt_disable();
49605519 arch_spin_lock(&trace_cmdline_lock);
49615520 savedcmd_temp = savedcmd;
49625521 savedcmd = s;
49635522 arch_spin_unlock(&trace_cmdline_lock);
5523
+ preempt_enable();
49645524 free_saved_cmdlines_buffer(savedcmd_temp);
49655525
49665526 return 0;
....@@ -5019,14 +5579,12 @@
50195579 * Paranoid! If ptr points to end, we don't want to increment past it.
50205580 * This really should never happen.
50215581 */
5582
+ (*pos)++;
50225583 ptr = update_eval_map(ptr);
50235584 if (WARN_ON_ONCE(!ptr))
50245585 return NULL;
50255586
50265587 ptr++;
5027
-
5028
- (*pos)++;
5029
-
50305588 ptr = update_eval_map(ptr);
50315589
50325590 return ptr;
....@@ -5075,8 +5633,11 @@
50755633
50765634 static int tracing_eval_map_open(struct inode *inode, struct file *filp)
50775635 {
5078
- if (tracing_disabled)
5079
- return -ENODEV;
5636
+ int ret;
5637
+
5638
+ ret = tracing_check_open_get_tr(NULL);
5639
+ if (ret)
5640
+ return ret;
50805641
50815642 return seq_open(filp, &tracing_eval_map_seq_ops);
50825643 }
....@@ -5189,11 +5750,11 @@
51895750
51905751 int tracer_init(struct tracer *t, struct trace_array *tr)
51915752 {
5192
- tracing_reset_online_cpus(&tr->trace_buffer);
5753
+ tracing_reset_online_cpus(&tr->array_buffer);
51935754 return t->init(tr);
51945755 }
51955756
5196
-static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
5757
+static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
51975758 {
51985759 int cpu;
51995760
....@@ -5203,8 +5764,8 @@
52035764
52045765 #ifdef CONFIG_TRACER_MAX_TRACE
52055766 /* resize @tr's buffer to the size of @size_tr's entries */
5206
-static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5207
- struct trace_buffer *size_buf, int cpu_id)
5767
+static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5768
+ struct array_buffer *size_buf, int cpu_id)
52085769 {
52095770 int cpu, ret = 0;
52105771
....@@ -5242,10 +5803,10 @@
52425803 ring_buffer_expanded = true;
52435804
52445805 /* May be called before buffers are initialized */
5245
- if (!tr->trace_buffer.buffer)
5806
+ if (!tr->array_buffer.buffer)
52465807 return 0;
52475808
5248
- ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
5809
+ ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
52495810 if (ret < 0)
52505811 return ret;
52515812
....@@ -5256,8 +5817,8 @@
52565817
52575818 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
52585819 if (ret < 0) {
5259
- int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5260
- &tr->trace_buffer, cpu);
5820
+ int r = resize_buffer_duplicate_size(&tr->array_buffer,
5821
+ &tr->array_buffer, cpu);
52615822 if (r < 0) {
52625823 /*
52635824 * AARGH! We are left with different
....@@ -5288,15 +5849,15 @@
52885849 #endif /* CONFIG_TRACER_MAX_TRACE */
52895850
52905851 if (cpu == RING_BUFFER_ALL_CPUS)
5291
- set_buffer_entries(&tr->trace_buffer, size);
5852
+ set_buffer_entries(&tr->array_buffer, size);
52925853 else
5293
- per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
5854
+ per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size;
52945855
52955856 return ret;
52965857 }
52975858
5298
-static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5299
- unsigned long size, int cpu_id)
5859
+ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5860
+ unsigned long size, int cpu_id)
53005861 {
53015862 int ret = size;
53025863
....@@ -5366,16 +5927,22 @@
53665927 tr->current_trace = &nop_trace;
53675928 }
53685929
5930
+static bool tracer_options_updated;
5931
+
53695932 static void add_tracer_options(struct trace_array *tr, struct tracer *t)
53705933 {
53715934 /* Only enable if the directory has been created already. */
53725935 if (!tr->dir)
53735936 return;
53745937
5938
+ /* Only create trace option files after update_tracer_options finish */
5939
+ if (!tracer_options_updated)
5940
+ return;
5941
+
53755942 create_trace_option_files(tr, t);
53765943 }
53775944
5378
-static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5945
+int tracing_set_tracer(struct trace_array *tr, const char *buf)
53795946 {
53805947 struct tracer *t;
53815948 #ifdef CONFIG_TRACER_MAX_TRACE
....@@ -5404,6 +5971,18 @@
54045971 if (t == tr->current_trace)
54055972 goto out;
54065973
5974
+#ifdef CONFIG_TRACER_SNAPSHOT
5975
+ if (t->use_max_tr) {
5976
+ local_irq_disable();
5977
+ arch_spin_lock(&tr->max_lock);
5978
+ if (tr->cond_snapshot)
5979
+ ret = -EBUSY;
5980
+ arch_spin_unlock(&tr->max_lock);
5981
+ local_irq_enable();
5982
+ if (ret)
5983
+ goto out;
5984
+ }
5985
+#endif
54075986 /* Some tracers won't work on kernel command line */
54085987 if (system_state < SYSTEM_RUNNING && t->noboot) {
54095988 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
....@@ -5418,7 +5997,7 @@
54185997 }
54195998
54205999 /* If trace pipe files are being read, we can't change the tracer */
5421
- if (tr->current_trace->ref) {
6000
+ if (tr->trace_ref) {
54226001 ret = -EBUSY;
54236002 goto out;
54246003 }
....@@ -5430,11 +6009,11 @@
54306009 if (tr->current_trace->reset)
54316010 tr->current_trace->reset(tr);
54326011
5433
- /* Current trace needs to be nop_trace before synchronize_sched */
5434
- tr->current_trace = &nop_trace;
5435
-
54366012 #ifdef CONFIG_TRACER_MAX_TRACE
5437
- had_max_tr = tr->allocated_snapshot;
6013
+ had_max_tr = tr->current_trace->use_max_tr;
6014
+
6015
+ /* Current trace needs to be nop_trace before synchronize_rcu */
6016
+ tr->current_trace = &nop_trace;
54386017
54396018 if (had_max_tr && !t->use_max_tr) {
54406019 /*
....@@ -5444,17 +6023,17 @@
54446023 * The update_max_tr is called from interrupts disabled
54456024 * so a synchronized_sched() is sufficient.
54466025 */
5447
- synchronize_sched();
6026
+ synchronize_rcu();
54486027 free_snapshot(tr);
54496028 }
5450
-#endif
54516029
5452
-#ifdef CONFIG_TRACER_MAX_TRACE
5453
- if (t->use_max_tr && !had_max_tr) {
6030
+ if (t->use_max_tr && !tr->allocated_snapshot) {
54546031 ret = tracing_alloc_snapshot_instance(tr);
54556032 if (ret < 0)
54566033 goto out;
54576034 }
6035
+#else
6036
+ tr->current_trace = &nop_trace;
54586037 #endif
54596038
54606039 if (t->init) {
....@@ -5589,13 +6168,11 @@
55896168 {
55906169 struct trace_array *tr = inode->i_private;
55916170 struct trace_iterator *iter;
5592
- int ret = 0;
6171
+ int ret;
55936172
5594
- if (tracing_disabled)
5595
- return -ENODEV;
5596
-
5597
- if (trace_array_get(tr) < 0)
5598
- return -ENODEV;
6173
+ ret = tracing_check_open_get_tr(tr);
6174
+ if (ret)
6175
+ return ret;
55996176
56006177 mutex_lock(&trace_types_lock);
56016178
....@@ -5626,7 +6203,7 @@
56266203 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
56276204
56286205 iter->tr = tr;
5629
- iter->trace_buffer = &tr->trace_buffer;
6206
+ iter->array_buffer = &tr->array_buffer;
56306207 iter->cpu_file = tracing_get_cpu(inode);
56316208 mutex_init(&iter->mutex);
56326209 filp->private_data = iter;
....@@ -5636,7 +6213,7 @@
56366213
56376214 nonseekable_open(inode, filp);
56386215
5639
- tr->current_trace->ref++;
6216
+ tr->trace_ref++;
56406217 out:
56416218 mutex_unlock(&trace_types_lock);
56426219 return ret;
....@@ -5655,7 +6232,7 @@
56556232
56566233 mutex_lock(&trace_types_lock);
56576234
5658
- tr->current_trace->ref--;
6235
+ tr->trace_ref--;
56596236
56606237 if (iter->trace->pipe_close)
56616238 iter->trace->pipe_close(iter);
....@@ -5686,8 +6263,8 @@
56866263 */
56876264 return EPOLLIN | EPOLLRDNORM;
56886265 else
5689
- return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
5690
- filp, poll_table);
6266
+ return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
6267
+ filp, poll_table, iter->tr->buffer_percent);
56916268 }
56926269
56936270 static __poll_t
....@@ -5724,7 +6301,7 @@
57246301
57256302 mutex_unlock(&iter->mutex);
57266303
5727
- ret = wait_on_pipe(iter, false);
6304
+ ret = wait_on_pipe(iter, 0);
57286305
57296306 mutex_lock(&iter->mutex);
57306307
....@@ -5840,14 +6417,6 @@
58406417 __free_page(spd->pages[idx]);
58416418 }
58426419
5843
-static const struct pipe_buf_operations tracing_pipe_buf_ops = {
5844
- .can_merge = 0,
5845
- .confirm = generic_pipe_buf_confirm,
5846
- .release = generic_pipe_buf_release,
5847
- .steal = generic_pipe_buf_steal,
5848
- .get = generic_pipe_buf_get,
5849
-};
5850
-
58516420 static size_t
58526421 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
58536422 {
....@@ -5909,7 +6478,7 @@
59096478 .partial = partial_def,
59106479 .nr_pages = 0, /* This gets updated below. */
59116480 .nr_pages_max = PIPE_DEF_BUFFERS,
5912
- .ops = &tracing_pipe_buf_ops,
6481
+ .ops = &default_pipe_buf_ops,
59136482 .spd_release = tracing_spd_release_pipe,
59146483 };
59156484 ssize_t ret;
....@@ -6004,8 +6573,8 @@
60046573 for_each_tracing_cpu(cpu) {
60056574 /* fill in the size from first enabled cpu */
60066575 if (size == 0)
6007
- size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
6008
- if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
6576
+ size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6577
+ if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
60096578 buf_size_same = 0;
60106579 break;
60116580 }
....@@ -6021,7 +6590,7 @@
60216590 } else
60226591 r = sprintf(buf, "X\n");
60236592 } else
6024
- r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
6593
+ r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
60256594
60266595 mutex_unlock(&trace_types_lock);
60276596
....@@ -6068,7 +6637,7 @@
60686637
60696638 mutex_lock(&trace_types_lock);
60706639 for_each_tracing_cpu(cpu) {
6071
- size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
6640
+ size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
60726641 if (!ring_buffer_expanded)
60736642 expanded_size += trace_buf_size >> 10;
60746643 }
....@@ -6118,16 +6687,16 @@
61186687 struct trace_array *tr = filp->private_data;
61196688 struct ring_buffer_event *event;
61206689 enum event_trigger_type tt = ETT_NONE;
6121
- struct ring_buffer *buffer;
6690
+ struct trace_buffer *buffer;
61226691 struct print_entry *entry;
61236692 unsigned long irq_flags;
6124
- const char faulted[] = "<faulted>";
61256693 ssize_t written;
61266694 int size;
61276695 int len;
61286696
61296697 /* Used in tracing_mark_raw_write() as well */
6130
-#define FAULTED_SIZE (sizeof(faulted) - 1) /* '\0' is already accounted for */
6698
+#define FAULTED_STR "<faulted>"
6699
+#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
61316700
61326701 if (tracing_disabled)
61336702 return -EINVAL;
....@@ -6147,7 +6716,7 @@
61476716 if (cnt < FAULTED_SIZE)
61486717 size += FAULTED_SIZE - cnt;
61496718
6150
- buffer = tr->trace_buffer.buffer;
6719
+ buffer = tr->array_buffer.buffer;
61516720 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
61526721 irq_flags, preempt_count());
61536722 if (unlikely(!event))
....@@ -6159,12 +6728,11 @@
61596728
61606729 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
61616730 if (len) {
6162
- memcpy(&entry->buf, faulted, FAULTED_SIZE);
6731
+ memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
61636732 cnt = FAULTED_SIZE;
61646733 written = -EFAULT;
61656734 } else
61666735 written = cnt;
6167
- len = cnt;
61686736
61696737 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
61706738 /* do not add \n before testing triggers, but add \0 */
....@@ -6178,6 +6746,8 @@
61786746 } else
61796747 entry->buf[cnt] = '\0';
61806748
6749
+ if (static_branch_unlikely(&trace_marker_exports_enabled))
6750
+ ftrace_exports(event, TRACE_EXPORT_MARKER);
61816751 __buffer_unlock_commit(buffer, event);
61826752
61836753 if (tt)
....@@ -6198,9 +6768,8 @@
61986768 {
61996769 struct trace_array *tr = filp->private_data;
62006770 struct ring_buffer_event *event;
6201
- struct ring_buffer *buffer;
6771
+ struct trace_buffer *buffer;
62026772 struct raw_data_entry *entry;
6203
- const char faulted[] = "<faulted>";
62046773 unsigned long irq_flags;
62056774 ssize_t written;
62066775 int size;
....@@ -6228,7 +6797,7 @@
62286797 if (cnt < FAULT_SIZE_ID)
62296798 size += FAULT_SIZE_ID - cnt;
62306799
6231
- buffer = tr->trace_buffer.buffer;
6800
+ buffer = tr->array_buffer.buffer;
62326801 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
62336802 irq_flags, preempt_count());
62346803 if (!event)
....@@ -6240,7 +6809,7 @@
62406809 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
62416810 if (len) {
62426811 entry->id = -1;
6243
- memcpy(&entry->buf, faulted, FAULTED_SIZE);
6812
+ memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
62446813 written = -EFAULT;
62456814 } else
62466815 written = cnt;
....@@ -6283,13 +6852,13 @@
62836852
62846853 tr->clock_id = i;
62856854
6286
- ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
6855
+ ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
62876856
62886857 /*
62896858 * New clock may not be consistent with the previous clock.
62906859 * Reset the buffer so that it doesn't have incomparable timestamps.
62916860 */
6292
- tracing_reset_online_cpus(&tr->trace_buffer);
6861
+ tracing_reset_online_cpus(&tr->array_buffer);
62936862
62946863 #ifdef CONFIG_TRACER_MAX_TRACE
62956864 if (tr->max_buffer.buffer)
....@@ -6335,11 +6904,9 @@
63356904 struct trace_array *tr = inode->i_private;
63366905 int ret;
63376906
6338
- if (tracing_disabled)
6339
- return -ENODEV;
6340
-
6341
- if (trace_array_get(tr))
6342
- return -ENODEV;
6907
+ ret = tracing_check_open_get_tr(tr);
6908
+ if (ret)
6909
+ return ret;
63436910
63446911 ret = single_open(file, tracing_clock_show, inode->i_private);
63456912 if (ret < 0)
....@@ -6354,7 +6921,7 @@
63546921
63556922 mutex_lock(&trace_types_lock);
63566923
6357
- if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6924
+ if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
63586925 seq_puts(m, "delta [absolute]\n");
63596926 else
63606927 seq_puts(m, "[delta] absolute\n");
....@@ -6369,11 +6936,9 @@
63696936 struct trace_array *tr = inode->i_private;
63706937 int ret;
63716938
6372
- if (tracing_disabled)
6373
- return -ENODEV;
6374
-
6375
- if (trace_array_get(tr))
6376
- return -ENODEV;
6939
+ ret = tracing_check_open_get_tr(tr);
6940
+ if (ret)
6941
+ return ret;
63776942
63786943 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
63796944 if (ret < 0)
....@@ -6401,7 +6966,7 @@
64016966 goto out;
64026967 }
64036968
6404
- ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
6969
+ ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs);
64056970
64066971 #ifdef CONFIG_TRACER_MAX_TRACE
64076972 if (tr->max_buffer.buffer)
....@@ -6426,10 +6991,11 @@
64266991 struct trace_array *tr = inode->i_private;
64276992 struct trace_iterator *iter;
64286993 struct seq_file *m;
6429
- int ret = 0;
6994
+ int ret;
64306995
6431
- if (trace_array_get(tr) < 0)
6432
- return -ENODEV;
6996
+ ret = tracing_check_open_get_tr(tr);
6997
+ if (ret)
6998
+ return ret;
64336999
64347000 if (file->f_mode & FMODE_READ) {
64357001 iter = __tracing_open(inode, file, true);
....@@ -6449,7 +7015,7 @@
64497015 ret = 0;
64507016
64517017 iter->tr = tr;
6452
- iter->trace_buffer = &tr->max_buffer;
7018
+ iter->array_buffer = &tr->max_buffer;
64537019 iter->cpu_file = tracing_get_cpu(inode);
64547020 m->private = iter;
64557021 file->private_data = m;
....@@ -6486,6 +7052,15 @@
64867052 goto out;
64877053 }
64887054
7055
+ local_irq_disable();
7056
+ arch_spin_lock(&tr->max_lock);
7057
+ if (tr->cond_snapshot)
7058
+ ret = -EBUSY;
7059
+ arch_spin_unlock(&tr->max_lock);
7060
+ local_irq_enable();
7061
+ if (ret)
7062
+ goto out;
7063
+
64897064 switch (val) {
64907065 case 0:
64917066 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
....@@ -6505,7 +7080,7 @@
65057080 #endif
65067081 if (tr->allocated_snapshot)
65077082 ret = resize_buffer_duplicate_size(&tr->max_buffer,
6508
- &tr->trace_buffer, iter->cpu_file);
7083
+ &tr->array_buffer, iter->cpu_file);
65097084 else
65107085 ret = tracing_alloc_snapshot_instance(tr);
65117086 if (ret < 0)
....@@ -6513,7 +7088,7 @@
65137088 local_irq_disable();
65147089 /* Now, we're going to swap */
65157090 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6516
- update_max_tr(tr, current, smp_processor_id());
7091
+ update_max_tr(tr, current, smp_processor_id(), NULL);
65177092 else
65187093 update_max_tr_single(tr, current, iter->cpu_file);
65197094 local_irq_enable();
....@@ -6523,7 +7098,7 @@
65237098 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
65247099 tracing_reset_online_cpus(&tr->max_buffer);
65257100 else
6526
- tracing_reset(&tr->max_buffer, iter->cpu_file);
7101
+ tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
65277102 }
65287103 break;
65297104 }
....@@ -6567,6 +7142,7 @@
65677142 struct ftrace_buffer_info *info;
65687143 int ret;
65697144
7145
+ /* The following checks for tracefs lockdown */
65707146 ret = tracing_buffers_open(inode, filp);
65717147 if (ret < 0)
65727148 return ret;
....@@ -6579,7 +7155,7 @@
65797155 }
65807156
65817157 info->iter.snapshot = true;
6582
- info->iter.trace_buffer = &info->iter.tr->max_buffer;
7158
+ info->iter.array_buffer = &info->iter.tr->max_buffer;
65837159
65847160 return ret;
65857161 }
....@@ -6688,19 +7264,263 @@
66887264
66897265 #endif /* CONFIG_TRACER_SNAPSHOT */
66907266
7267
+#define TRACING_LOG_ERRS_MAX 8
7268
+#define TRACING_LOG_LOC_MAX 128
7269
+
7270
+#define CMD_PREFIX " Command: "
7271
+
7272
+struct err_info {
7273
+ const char **errs; /* ptr to loc-specific array of err strings */
7274
+ u8 type; /* index into errs -> specific err string */
7275
+ u8 pos; /* MAX_FILTER_STR_VAL = 256 */
7276
+ u64 ts;
7277
+};
7278
+
7279
+struct tracing_log_err {
7280
+ struct list_head list;
7281
+ struct err_info info;
7282
+ char loc[TRACING_LOG_LOC_MAX]; /* err location */
7283
+ char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
7284
+};
7285
+
7286
+static DEFINE_MUTEX(tracing_err_log_lock);
7287
+
7288
+static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
7289
+{
7290
+ struct tracing_log_err *err;
7291
+
7292
+ if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
7293
+ err = kzalloc(sizeof(*err), GFP_KERNEL);
7294
+ if (!err)
7295
+ err = ERR_PTR(-ENOMEM);
7296
+ else
7297
+ tr->n_err_log_entries++;
7298
+
7299
+ return err;
7300
+ }
7301
+
7302
+ err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
7303
+ list_del(&err->list);
7304
+
7305
+ return err;
7306
+}
7307
+
7308
+/**
7309
+ * err_pos - find the position of a string within a command for error careting
7310
+ * @cmd: The tracing command that caused the error
7311
+ * @str: The string to position the caret at within @cmd
7312
+ *
7313
+ * Finds the position of the first occurence of @str within @cmd. The
7314
+ * return value can be passed to tracing_log_err() for caret placement
7315
+ * within @cmd.
7316
+ *
7317
+ * Returns the index within @cmd of the first occurence of @str or 0
7318
+ * if @str was not found.
7319
+ */
7320
+unsigned int err_pos(char *cmd, const char *str)
7321
+{
7322
+ char *found;
7323
+
7324
+ if (WARN_ON(!strlen(cmd)))
7325
+ return 0;
7326
+
7327
+ found = strstr(cmd, str);
7328
+ if (found)
7329
+ return found - cmd;
7330
+
7331
+ return 0;
7332
+}
7333
+
7334
+/**
7335
+ * tracing_log_err - write an error to the tracing error log
7336
+ * @tr: The associated trace array for the error (NULL for top level array)
7337
+ * @loc: A string describing where the error occurred
7338
+ * @cmd: The tracing command that caused the error
7339
+ * @errs: The array of loc-specific static error strings
7340
+ * @type: The index into errs[], which produces the specific static err string
7341
+ * @pos: The position the caret should be placed in the cmd
7342
+ *
7343
+ * Writes an error into tracing/error_log of the form:
7344
+ *
7345
+ * <loc>: error: <text>
7346
+ * Command: <cmd>
7347
+ * ^
7348
+ *
7349
+ * tracing/error_log is a small log file containing the last
7350
+ * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7351
+ * unless there has been a tracing error, and the error log can be
7352
+ * cleared and have its memory freed by writing the empty string in
7353
+ * truncation mode to it i.e. echo > tracing/error_log.
7354
+ *
7355
+ * NOTE: the @errs array along with the @type param are used to
7356
+ * produce a static error string - this string is not copied and saved
7357
+ * when the error is logged - only a pointer to it is saved. See
7358
+ * existing callers for examples of how static strings are typically
7359
+ * defined for use with tracing_log_err().
7360
+ */
7361
+void tracing_log_err(struct trace_array *tr,
7362
+ const char *loc, const char *cmd,
7363
+ const char **errs, u8 type, u8 pos)
7364
+{
7365
+ struct tracing_log_err *err;
7366
+
7367
+ if (!tr)
7368
+ tr = &global_trace;
7369
+
7370
+ mutex_lock(&tracing_err_log_lock);
7371
+ err = get_tracing_log_err(tr);
7372
+ if (PTR_ERR(err) == -ENOMEM) {
7373
+ mutex_unlock(&tracing_err_log_lock);
7374
+ return;
7375
+ }
7376
+
7377
+ snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7378
+ snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7379
+
7380
+ err->info.errs = errs;
7381
+ err->info.type = type;
7382
+ err->info.pos = pos;
7383
+ err->info.ts = local_clock();
7384
+
7385
+ list_add_tail(&err->list, &tr->err_log);
7386
+ mutex_unlock(&tracing_err_log_lock);
7387
+}
7388
+
7389
+static void clear_tracing_err_log(struct trace_array *tr)
7390
+{
7391
+ struct tracing_log_err *err, *next;
7392
+
7393
+ mutex_lock(&tracing_err_log_lock);
7394
+ list_for_each_entry_safe(err, next, &tr->err_log, list) {
7395
+ list_del(&err->list);
7396
+ kfree(err);
7397
+ }
7398
+
7399
+ tr->n_err_log_entries = 0;
7400
+ mutex_unlock(&tracing_err_log_lock);
7401
+}
7402
+
7403
+static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7404
+{
7405
+ struct trace_array *tr = m->private;
7406
+
7407
+ mutex_lock(&tracing_err_log_lock);
7408
+
7409
+ return seq_list_start(&tr->err_log, *pos);
7410
+}
7411
+
7412
+static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7413
+{
7414
+ struct trace_array *tr = m->private;
7415
+
7416
+ return seq_list_next(v, &tr->err_log, pos);
7417
+}
7418
+
7419
+static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7420
+{
7421
+ mutex_unlock(&tracing_err_log_lock);
7422
+}
7423
+
7424
+static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7425
+{
7426
+ u8 i;
7427
+
7428
+ for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7429
+ seq_putc(m, ' ');
7430
+ for (i = 0; i < pos; i++)
7431
+ seq_putc(m, ' ');
7432
+ seq_puts(m, "^\n");
7433
+}
7434
+
7435
+static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7436
+{
7437
+ struct tracing_log_err *err = v;
7438
+
7439
+ if (err) {
7440
+ const char *err_text = err->info.errs[err->info.type];
7441
+ u64 sec = err->info.ts;
7442
+ u32 nsec;
7443
+
7444
+ nsec = do_div(sec, NSEC_PER_SEC);
7445
+ seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7446
+ err->loc, err_text);
7447
+ seq_printf(m, "%s", err->cmd);
7448
+ tracing_err_log_show_pos(m, err->info.pos);
7449
+ }
7450
+
7451
+ return 0;
7452
+}
7453
+
7454
+static const struct seq_operations tracing_err_log_seq_ops = {
7455
+ .start = tracing_err_log_seq_start,
7456
+ .next = tracing_err_log_seq_next,
7457
+ .stop = tracing_err_log_seq_stop,
7458
+ .show = tracing_err_log_seq_show
7459
+};
7460
+
7461
+static int tracing_err_log_open(struct inode *inode, struct file *file)
7462
+{
7463
+ struct trace_array *tr = inode->i_private;
7464
+ int ret = 0;
7465
+
7466
+ ret = tracing_check_open_get_tr(tr);
7467
+ if (ret)
7468
+ return ret;
7469
+
7470
+ /* If this file was opened for write, then erase contents */
7471
+ if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
7472
+ clear_tracing_err_log(tr);
7473
+
7474
+ if (file->f_mode & FMODE_READ) {
7475
+ ret = seq_open(file, &tracing_err_log_seq_ops);
7476
+ if (!ret) {
7477
+ struct seq_file *m = file->private_data;
7478
+ m->private = tr;
7479
+ } else {
7480
+ trace_array_put(tr);
7481
+ }
7482
+ }
7483
+ return ret;
7484
+}
7485
+
7486
+static ssize_t tracing_err_log_write(struct file *file,
7487
+ const char __user *buffer,
7488
+ size_t count, loff_t *ppos)
7489
+{
7490
+ return count;
7491
+}
7492
+
7493
+static int tracing_err_log_release(struct inode *inode, struct file *file)
7494
+{
7495
+ struct trace_array *tr = inode->i_private;
7496
+
7497
+ trace_array_put(tr);
7498
+
7499
+ if (file->f_mode & FMODE_READ)
7500
+ seq_release(inode, file);
7501
+
7502
+ return 0;
7503
+}
7504
+
7505
+static const struct file_operations tracing_err_log_fops = {
7506
+ .open = tracing_err_log_open,
7507
+ .write = tracing_err_log_write,
7508
+ .read = seq_read,
7509
+ .llseek = seq_lseek,
7510
+ .release = tracing_err_log_release,
7511
+};
7512
+
66917513 static int tracing_buffers_open(struct inode *inode, struct file *filp)
66927514 {
66937515 struct trace_array *tr = inode->i_private;
66947516 struct ftrace_buffer_info *info;
66957517 int ret;
66967518
6697
- if (tracing_disabled)
6698
- return -ENODEV;
7519
+ ret = tracing_check_open_get_tr(tr);
7520
+ if (ret)
7521
+ return ret;
66997522
6700
- if (trace_array_get(tr) < 0)
6701
- return -ENODEV;
6702
-
6703
- info = kzalloc(sizeof(*info), GFP_KERNEL);
7523
+ info = kvzalloc(sizeof(*info), GFP_KERNEL);
67047524 if (!info) {
67057525 trace_array_put(tr);
67067526 return -ENOMEM;
....@@ -6711,14 +7531,14 @@
67117531 info->iter.tr = tr;
67127532 info->iter.cpu_file = tracing_get_cpu(inode);
67137533 info->iter.trace = tr->current_trace;
6714
- info->iter.trace_buffer = &tr->trace_buffer;
7534
+ info->iter.array_buffer = &tr->array_buffer;
67157535 info->spare = NULL;
67167536 /* Force reading ring buffer for first read */
67177537 info->read = (unsigned int)-1;
67187538
67197539 filp->private_data = info;
67207540
6721
- tr->current_trace->ref++;
7541
+ tr->trace_ref++;
67227542
67237543 mutex_unlock(&trace_types_lock);
67247544
....@@ -6756,7 +7576,7 @@
67567576 #endif
67577577
67587578 if (!info->spare) {
6759
- info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
7579
+ info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
67607580 iter->cpu_file);
67617581 if (IS_ERR(info->spare)) {
67627582 ret = PTR_ERR(info->spare);
....@@ -6774,7 +7594,7 @@
67747594
67757595 again:
67767596 trace_access_lock(iter->cpu_file);
6777
- ret = ring_buffer_read_page(iter->trace_buffer->buffer,
7597
+ ret = ring_buffer_read_page(iter->array_buffer->buffer,
67787598 &info->spare,
67797599 count,
67807600 iter->cpu_file, 0);
....@@ -6785,7 +7605,7 @@
67857605 if ((filp->f_flags & O_NONBLOCK))
67867606 return -EAGAIN;
67877607
6788
- ret = wait_on_pipe(iter, false);
7608
+ ret = wait_on_pipe(iter, 0);
67897609 if (ret)
67907610 return ret;
67917611
....@@ -6819,14 +7639,14 @@
68197639
68207640 mutex_lock(&trace_types_lock);
68217641
6822
- iter->tr->current_trace->ref--;
7642
+ iter->tr->trace_ref--;
68237643
68247644 __trace_array_put(iter->tr);
68257645
68267646 if (info->spare)
6827
- ring_buffer_free_read_page(iter->trace_buffer->buffer,
7647
+ ring_buffer_free_read_page(iter->array_buffer->buffer,
68287648 info->spare_cpu, info->spare);
6829
- kfree(info);
7649
+ kvfree(info);
68307650
68317651 mutex_unlock(&trace_types_lock);
68327652
....@@ -6834,7 +7654,7 @@
68347654 }
68357655
68367656 struct buffer_ref {
6837
- struct ring_buffer *buffer;
7657
+ struct trace_buffer *buffer;
68387658 void *page;
68397659 int cpu;
68407660 refcount_t refcount;
....@@ -6871,10 +7691,7 @@
68717691
68727692 /* Pipe buffer operations for a buffer. */
68737693 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
6874
- .can_merge = 0,
6875
- .confirm = generic_pipe_buf_confirm,
68767694 .release = buffer_pipe_buf_release,
6877
- .steal = generic_pipe_buf_nosteal,
68787695 .get = buffer_pipe_buf_get,
68797696 };
68807697
....@@ -6930,7 +7747,7 @@
69307747
69317748 again:
69327749 trace_access_lock(iter->cpu_file);
6933
- entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7750
+ entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
69347751
69357752 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
69367753 struct page *page;
....@@ -6943,7 +7760,7 @@
69437760 }
69447761
69457762 refcount_set(&ref->refcount, 1);
6946
- ref->buffer = iter->trace_buffer->buffer;
7763
+ ref->buffer = iter->array_buffer->buffer;
69477764 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
69487765 if (IS_ERR(ref->page)) {
69497766 ret = PTR_ERR(ref->page);
....@@ -6971,7 +7788,7 @@
69717788 spd.nr_pages++;
69727789 *ppos += PAGE_SIZE;
69737790
6974
- entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7791
+ entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
69757792 }
69767793
69777794 trace_access_unlock(iter->cpu_file);
....@@ -6986,7 +7803,7 @@
69867803 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
69877804 goto out;
69887805
6989
- ret = wait_on_pipe(iter, true);
7806
+ ret = wait_on_pipe(iter, iter->tr->buffer_percent);
69907807 if (ret)
69917808 goto out;
69927809
....@@ -7015,7 +7832,7 @@
70157832 {
70167833 struct inode *inode = file_inode(filp);
70177834 struct trace_array *tr = inode->i_private;
7018
- struct trace_buffer *trace_buf = &tr->trace_buffer;
7835
+ struct array_buffer *trace_buf = &tr->array_buffer;
70197836 int cpu = tracing_get_cpu(inode);
70207837 struct trace_seq *s;
70217838 unsigned long cnt;
....@@ -7086,14 +7903,23 @@
70867903 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
70877904 size_t cnt, loff_t *ppos)
70887905 {
7089
- unsigned long *p = filp->private_data;
7090
- char buf[64]; /* Not too big for a shallow stack */
7906
+ ssize_t ret;
7907
+ char *buf;
70917908 int r;
70927909
7093
- r = scnprintf(buf, 63, "%ld", *p);
7094
- buf[r++] = '\n';
7910
+ /* 256 should be plenty to hold the amount needed */
7911
+ buf = kmalloc(256, GFP_KERNEL);
7912
+ if (!buf)
7913
+ return -ENOMEM;
70957914
7096
- return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7915
+ r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
7916
+ ftrace_update_tot_cnt,
7917
+ ftrace_number_of_pages,
7918
+ ftrace_number_of_groups);
7919
+
7920
+ ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7921
+ kfree(buf);
7922
+ return ret;
70977923 }
70987924
70997925 static const struct file_operations tracing_dyn_info_fops = {
....@@ -7287,7 +8113,7 @@
72878113
72888114 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
72898115
7290
- WARN_ONCE(!tr->percpu_dir,
8116
+ MEM_FAIL(!tr->percpu_dir,
72918117 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
72928118
72938119 return tr->percpu_dir;
....@@ -7608,7 +8434,7 @@
76088434 for (cnt = 0; opts[cnt].name; cnt++) {
76098435 create_trace_option_file(tr, &topts[cnt], flags,
76108436 &opts[cnt]);
7611
- WARN_ONCE(topts[cnt].entry == NULL,
8437
+ MEM_FAIL(topts[cnt].entry == NULL,
76128438 "Failed to create trace option: %s",
76138439 opts[cnt].name);
76148440 }
....@@ -7665,7 +8491,7 @@
76658491 size_t cnt, loff_t *ppos)
76668492 {
76678493 struct trace_array *tr = filp->private_data;
7668
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
8494
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
76698495 unsigned long val;
76708496 int ret;
76718497
....@@ -7702,13 +8528,60 @@
77028528 .llseek = default_llseek,
77038529 };
77048530
7705
-struct dentry *trace_instance_dir;
8531
+static ssize_t
8532
+buffer_percent_read(struct file *filp, char __user *ubuf,
8533
+ size_t cnt, loff_t *ppos)
8534
+{
8535
+ struct trace_array *tr = filp->private_data;
8536
+ char buf[64];
8537
+ int r;
8538
+
8539
+ r = tr->buffer_percent;
8540
+ r = sprintf(buf, "%d\n", r);
8541
+
8542
+ return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8543
+}
8544
+
8545
+static ssize_t
8546
+buffer_percent_write(struct file *filp, const char __user *ubuf,
8547
+ size_t cnt, loff_t *ppos)
8548
+{
8549
+ struct trace_array *tr = filp->private_data;
8550
+ unsigned long val;
8551
+ int ret;
8552
+
8553
+ ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8554
+ if (ret)
8555
+ return ret;
8556
+
8557
+ if (val > 100)
8558
+ return -EINVAL;
8559
+
8560
+ if (!val)
8561
+ val = 1;
8562
+
8563
+ tr->buffer_percent = val;
8564
+
8565
+ (*ppos)++;
8566
+
8567
+ return cnt;
8568
+}
8569
+
8570
+static const struct file_operations buffer_percent_fops = {
8571
+ .open = tracing_open_generic_tr,
8572
+ .read = buffer_percent_read,
8573
+ .write = buffer_percent_write,
8574
+ .release = tracing_release_generic_tr,
8575
+ .llseek = default_llseek,
8576
+};
8577
+
8578
+static struct dentry *trace_instance_dir;
77068579
77078580 static void
77088581 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
77098582
77108583 static int
7711
-allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
8584
+allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
77128585 {
77138586 enum ring_buffer_flags rb_flags;
77148587
....@@ -7728,8 +8601,8 @@
77288601 }
77298602
77308603 /* Allocate the first page for all buffers */
7731
- set_buffer_entries(&tr->trace_buffer,
7732
- ring_buffer_size(tr->trace_buffer.buffer, 0));
8604
+ set_buffer_entries(&tr->array_buffer,
8605
+ ring_buffer_size(tr->array_buffer.buffer, 0));
77338606
77348607 return 0;
77358608 }
....@@ -7738,18 +8611,18 @@
77388611 {
77398612 int ret;
77408613
7741
- ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
8614
+ ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
77428615 if (ret)
77438616 return ret;
77448617
77458618 #ifdef CONFIG_TRACER_MAX_TRACE
77468619 ret = allocate_trace_buffer(tr, &tr->max_buffer,
77478620 allocate_snapshot ? size : 1);
7748
- if (WARN_ON(ret)) {
7749
- ring_buffer_free(tr->trace_buffer.buffer);
7750
- tr->trace_buffer.buffer = NULL;
7751
- free_percpu(tr->trace_buffer.data);
7752
- tr->trace_buffer.data = NULL;
8621
+ if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
8622
+ ring_buffer_free(tr->array_buffer.buffer);
8623
+ tr->array_buffer.buffer = NULL;
8624
+ free_percpu(tr->array_buffer.data);
8625
+ tr->array_buffer.data = NULL;
77538626 return -ENOMEM;
77548627 }
77558628 tr->allocated_snapshot = allocate_snapshot;
....@@ -7761,22 +8634,10 @@
77618634 allocate_snapshot = false;
77628635 #endif
77638636
7764
- /*
7765
- * Because of some magic with the way alloc_percpu() works on
7766
- * x86_64, we need to synchronize the pgd of all the tables,
7767
- * otherwise the trace events that happen in x86_64 page fault
7768
- * handlers can't cope with accessing the chance that a
7769
- * alloc_percpu()'d memory might be touched in the page fault trace
7770
- * event. Oh, and we need to audit all other alloc_percpu() and vmalloc()
7771
- * calls in tracing, because something might get triggered within a
7772
- * page fault trace event!
7773
- */
7774
- vmalloc_sync_mappings();
7775
-
77768637 return 0;
77778638 }
77788639
7779
-static void free_trace_buffer(struct trace_buffer *buf)
8640
+static void free_trace_buffer(struct array_buffer *buf)
77808641 {
77818642 if (buf->buffer) {
77828643 ring_buffer_free(buf->buffer);
....@@ -7791,7 +8652,7 @@
77918652 if (!tr)
77928653 return;
77938654
7794
- free_trace_buffer(&tr->trace_buffer);
8655
+ free_trace_buffer(&tr->array_buffer);
77958656
77968657 #ifdef CONFIG_TRACER_MAX_TRACE
77978658 free_trace_buffer(&tr->max_buffer);
....@@ -7818,28 +8679,68 @@
78188679 static void update_tracer_options(struct trace_array *tr)
78198680 {
78208681 mutex_lock(&trace_types_lock);
8682
+ tracer_options_updated = true;
78218683 __update_tracer_options(tr);
78228684 mutex_unlock(&trace_types_lock);
78238685 }
78248686
7825
-static int instance_mkdir(const char *name)
8687
+/* Must have trace_types_lock held */
8688
+struct trace_array *trace_array_find(const char *instance)
8689
+{
8690
+ struct trace_array *tr, *found = NULL;
8691
+
8692
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8693
+ if (tr->name && strcmp(tr->name, instance) == 0) {
8694
+ found = tr;
8695
+ break;
8696
+ }
8697
+ }
8698
+
8699
+ return found;
8700
+}
8701
+
8702
+struct trace_array *trace_array_find_get(const char *instance)
8703
+{
8704
+ struct trace_array *tr;
8705
+
8706
+ mutex_lock(&trace_types_lock);
8707
+ tr = trace_array_find(instance);
8708
+ if (tr)
8709
+ tr->ref++;
8710
+ mutex_unlock(&trace_types_lock);
8711
+
8712
+ return tr;
8713
+}
8714
+
8715
+static int trace_array_create_dir(struct trace_array *tr)
8716
+{
8717
+ int ret;
8718
+
8719
+ tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
8720
+ if (!tr->dir)
8721
+ return -EINVAL;
8722
+
8723
+ ret = event_trace_add_tracer(tr->dir, tr);
8724
+ if (ret) {
8725
+ tracefs_remove(tr->dir);
8726
+ return ret;
8727
+ }
8728
+
8729
+ init_tracer_tracefs(tr, tr->dir);
8730
+ __update_tracer_options(tr);
8731
+
8732
+ return ret;
8733
+}
8734
+
8735
+static struct trace_array *trace_array_create(const char *name)
78268736 {
78278737 struct trace_array *tr;
78288738 int ret;
78298739
7830
- mutex_lock(&event_mutex);
7831
- mutex_lock(&trace_types_lock);
7832
-
7833
- ret = -EEXIST;
7834
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7835
- if (tr->name && strcmp(tr->name, name) == 0)
7836
- goto out_unlock;
7837
- }
7838
-
78398740 ret = -ENOMEM;
78408741 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
78418742 if (!tr)
7842
- goto out_unlock;
8743
+ return ERR_PTR(ret);
78438744
78448745 tr->name = kstrdup(name, GFP_KERNEL);
78458746 if (!tr->name)
....@@ -7861,70 +8762,112 @@
78618762 INIT_LIST_HEAD(&tr->systems);
78628763 INIT_LIST_HEAD(&tr->events);
78638764 INIT_LIST_HEAD(&tr->hist_vars);
8765
+ INIT_LIST_HEAD(&tr->err_log);
78648766
78658767 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
78668768 goto out_free_tr;
78678769
7868
- tr->dir = tracefs_create_dir(name, trace_instance_dir);
7869
- if (!tr->dir)
8770
+ if (ftrace_allocate_ftrace_ops(tr) < 0)
78708771 goto out_free_tr;
7871
-
7872
- ret = event_trace_add_tracer(tr->dir, tr);
7873
- if (ret) {
7874
- tracefs_remove_recursive(tr->dir);
7875
- goto out_free_tr;
7876
- }
78778772
78788773 ftrace_init_trace_array(tr);
78798774
7880
- init_tracer_tracefs(tr, tr->dir);
78818775 init_trace_flags_index(tr);
7882
- __update_tracer_options(tr);
8776
+
8777
+ if (trace_instance_dir) {
8778
+ ret = trace_array_create_dir(tr);
8779
+ if (ret)
8780
+ goto out_free_tr;
8781
+ } else
8782
+ __trace_early_add_events(tr);
78838783
78848784 list_add(&tr->list, &ftrace_trace_arrays);
78858785
7886
- mutex_unlock(&trace_types_lock);
7887
- mutex_unlock(&event_mutex);
8786
+ tr->ref++;
78888787
7889
- return 0;
8788
+ return tr;
78908789
78918790 out_free_tr:
8791
+ ftrace_free_ftrace_ops(tr);
78928792 free_trace_buffers(tr);
78938793 free_cpumask_var(tr->tracing_cpumask);
78948794 kfree(tr->name);
78958795 kfree(tr);
78968796
7897
- out_unlock:
7898
- mutex_unlock(&trace_types_lock);
7899
- mutex_unlock(&event_mutex);
7900
-
7901
- return ret;
7902
-
8797
+ return ERR_PTR(ret);
79038798 }
79048799
7905
-static int instance_rmdir(const char *name)
8800
+static int instance_mkdir(const char *name)
79068801 {
79078802 struct trace_array *tr;
7908
- int found = 0;
79098803 int ret;
7910
- int i;
79118804
79128805 mutex_lock(&event_mutex);
79138806 mutex_lock(&trace_types_lock);
79148807
7915
- ret = -ENODEV;
7916
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7917
- if (tr->name && strcmp(tr->name, name) == 0) {
7918
- found = 1;
7919
- break;
7920
- }
7921
- }
7922
- if (!found)
8808
+ ret = -EEXIST;
8809
+ if (trace_array_find(name))
79238810 goto out_unlock;
79248811
7925
- ret = -EBUSY;
7926
- if (tr->ref || (tr->current_trace && tr->current_trace->ref))
7927
- goto out_unlock;
8812
+ tr = trace_array_create(name);
8813
+
8814
+ ret = PTR_ERR_OR_ZERO(tr);
8815
+
8816
+out_unlock:
8817
+ mutex_unlock(&trace_types_lock);
8818
+ mutex_unlock(&event_mutex);
8819
+ return ret;
8820
+}
8821
+
8822
+/**
8823
+ * trace_array_get_by_name - Create/Lookup a trace array, given its name.
8824
+ * @name: The name of the trace array to be looked up/created.
8825
+ *
8826
+ * Returns pointer to trace array with given name.
8827
+ * NULL, if it cannot be created.
8828
+ *
8829
+ * NOTE: This function increments the reference counter associated with the
8830
+ * trace array returned. This makes sure it cannot be freed while in use.
8831
+ * Use trace_array_put() once the trace array is no longer needed.
8832
+ * If the trace_array is to be freed, trace_array_destroy() needs to
8833
+ * be called after the trace_array_put(), or simply let user space delete
8834
+ * it from the tracefs instances directory. But until the
8835
+ * trace_array_put() is called, user space can not delete it.
8836
+ *
8837
+ */
8838
+struct trace_array *trace_array_get_by_name(const char *name)
8839
+{
8840
+ struct trace_array *tr;
8841
+
8842
+ mutex_lock(&event_mutex);
8843
+ mutex_lock(&trace_types_lock);
8844
+
8845
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8846
+ if (tr->name && strcmp(tr->name, name) == 0)
8847
+ goto out_unlock;
8848
+ }
8849
+
8850
+ tr = trace_array_create(name);
8851
+
8852
+ if (IS_ERR(tr))
8853
+ tr = NULL;
8854
+out_unlock:
8855
+ if (tr)
8856
+ tr->ref++;
8857
+
8858
+ mutex_unlock(&trace_types_lock);
8859
+ mutex_unlock(&event_mutex);
8860
+ return tr;
8861
+}
8862
+EXPORT_SYMBOL_GPL(trace_array_get_by_name);
8863
+
8864
+static int __remove_instance(struct trace_array *tr)
8865
+{
8866
+ int i;
8867
+
8868
+ /* Reference counter for a newly created trace array = 1. */
8869
+ if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
8870
+ return -EBUSY;
79288871
79298872 list_del(&tr->list);
79308873
....@@ -7939,7 +8882,7 @@
79398882 event_trace_del_tracer(tr);
79408883 ftrace_clear_pids(tr);
79418884 ftrace_destroy_function_files(tr);
7942
- tracefs_remove_recursive(tr->dir);
8885
+ tracefs_remove(tr->dir);
79438886 free_trace_buffers(tr);
79448887
79458888 for (i = 0; i < tr->nr_topts; i++) {
....@@ -7951,9 +8894,50 @@
79518894 kfree(tr->name);
79528895 kfree(tr);
79538896
7954
- ret = 0;
8897
+ return 0;
8898
+}
79558899
7956
- out_unlock:
8900
+int trace_array_destroy(struct trace_array *this_tr)
8901
+{
8902
+ struct trace_array *tr;
8903
+ int ret;
8904
+
8905
+ if (!this_tr)
8906
+ return -EINVAL;
8907
+
8908
+ mutex_lock(&event_mutex);
8909
+ mutex_lock(&trace_types_lock);
8910
+
8911
+ ret = -ENODEV;
8912
+
8913
+ /* Making sure trace array exists before destroying it. */
8914
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8915
+ if (tr == this_tr) {
8916
+ ret = __remove_instance(tr);
8917
+ break;
8918
+ }
8919
+ }
8920
+
8921
+ mutex_unlock(&trace_types_lock);
8922
+ mutex_unlock(&event_mutex);
8923
+
8924
+ return ret;
8925
+}
8926
+EXPORT_SYMBOL_GPL(trace_array_destroy);
8927
+
8928
+static int instance_rmdir(const char *name)
8929
+{
8930
+ struct trace_array *tr;
8931
+ int ret;
8932
+
8933
+ mutex_lock(&event_mutex);
8934
+ mutex_lock(&trace_types_lock);
8935
+
8936
+ ret = -ENODEV;
8937
+ tr = trace_array_find(name);
8938
+ if (tr)
8939
+ ret = __remove_instance(tr);
8940
+
79578941 mutex_unlock(&trace_types_lock);
79588942 mutex_unlock(&event_mutex);
79598943
....@@ -7962,11 +8946,27 @@
79628946
79638947 static __init void create_trace_instances(struct dentry *d_tracer)
79648948 {
8949
+ struct trace_array *tr;
8950
+
79658951 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
79668952 instance_mkdir,
79678953 instance_rmdir);
7968
- if (WARN_ON(!trace_instance_dir))
8954
+ if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
79698955 return;
8956
+
8957
+ mutex_lock(&event_mutex);
8958
+ mutex_lock(&trace_types_lock);
8959
+
8960
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8961
+ if (!tr->name)
8962
+ continue;
8963
+ if (MEM_FAIL(trace_array_create_dir(tr) < 0,
8964
+ "Failed to create instance directory\n"))
8965
+ break;
8966
+ }
8967
+
8968
+ mutex_unlock(&trace_types_lock);
8969
+ mutex_unlock(&event_mutex);
79708970 }
79718971
79728972 static void
....@@ -8023,20 +9023,27 @@
80239023 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
80249024 &trace_time_stamp_mode_fops);
80259025
9026
+ tr->buffer_percent = 50;
9027
+
9028
+ trace_create_file("buffer_percent", 0444, d_tracer,
9029
+ tr, &buffer_percent_fops);
9030
+
80269031 create_trace_options_dir(tr);
80279032
80289033 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
8029
- trace_create_file("tracing_max_latency", 0644, d_tracer,
8030
- &tr->max_latency, &tracing_max_lat_fops);
9034
+ trace_create_maxlat_file(tr, d_tracer);
80319035 #endif
80329036
80339037 if (ftrace_create_function_files(tr, d_tracer))
8034
- WARN(1, "Could not allocate function filter files");
9038
+ MEM_FAIL(1, "Could not allocate function filter files");
80359039
80369040 #ifdef CONFIG_TRACER_SNAPSHOT
80379041 trace_create_file("snapshot", 0644, d_tracer,
80389042 tr, &snapshot_fops);
80399043 #endif
9044
+
9045
+ trace_create_file("error_log", 0644, d_tracer,
9046
+ tr, &tracing_err_log_fops);
80409047
80419048 for_each_tracing_cpu(cpu)
80429049 tracing_init_tracefs_percpu(tr, cpu);
....@@ -8044,6 +9051,7 @@
80449051 ftrace_init_tracefs(tr, d_tracer);
80459052 }
80469053
9054
+#ifndef CONFIG_TRACEFS_DISABLE_AUTOMOUNT
80479055 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
80489056 {
80499057 struct vfsmount *mnt;
....@@ -8065,6 +9073,7 @@
80659073
80669074 return mnt;
80679075 }
9076
+#endif
80689077
80699078 /**
80709079 * tracing_init_dentry - initialize top level trace array
....@@ -8073,19 +9082,23 @@
80739082 * directory. It is called via fs_initcall() by any of the boot up code
80749083 * and expects to return the dentry of the top level tracing directory.
80759084 */
8076
-struct dentry *tracing_init_dentry(void)
9085
+int tracing_init_dentry(void)
80779086 {
80789087 struct trace_array *tr = &global_trace;
80799088
9089
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9090
+ pr_warn("Tracing disabled due to lockdown\n");
9091
+ return -EPERM;
9092
+ }
9093
+
80809094 /* The top level trace array uses NULL as parent */
80819095 if (tr->dir)
8082
- return NULL;
9096
+ return 0;
80839097
8084
- if (WARN_ON(!tracefs_initialized()) ||
8085
- (IS_ENABLED(CONFIG_DEBUG_FS) &&
8086
- WARN_ON(!debugfs_initialized())))
8087
- return ERR_PTR(-ENODEV);
9098
+ if (WARN_ON(!tracefs_initialized()))
9099
+ return -ENODEV;
80889100
9101
+#ifndef CONFIG_TRACEFS_DISABLE_AUTOMOUNT
80899102 /*
80909103 * As there may still be users that expect the tracing
80919104 * files to exist in debugfs/tracing, we must automount
....@@ -8094,12 +9107,11 @@
80949107 */
80959108 tr->dir = debugfs_create_automount("tracing", NULL,
80969109 trace_automount, NULL);
8097
- if (!tr->dir) {
8098
- pr_warn_once("Could not create debugfs directory 'tracing'\n");
8099
- return ERR_PTR(-ENOMEM);
8100
- }
9110
+#else
9111
+ tr->dir = ERR_PTR(-ENODEV);
9112
+#endif
81019113
8102
- return NULL;
9114
+ return 0;
81039115 }
81049116
81059117 extern struct trace_eval_map *__start_ftrace_eval_maps[];
....@@ -8175,7 +9187,7 @@
81759187 break;
81769188 }
81779189
8178
- return 0;
9190
+ return NOTIFY_OK;
81799191 }
81809192
81819193 static struct notifier_block trace_module_nb = {
....@@ -8186,48 +9198,48 @@
81869198
81879199 static __init int tracer_init_tracefs(void)
81889200 {
8189
- struct dentry *d_tracer;
9201
+ int ret;
81909202
81919203 trace_access_lock_init();
81929204
8193
- d_tracer = tracing_init_dentry();
8194
- if (IS_ERR(d_tracer))
9205
+ ret = tracing_init_dentry();
9206
+ if (ret)
81959207 return 0;
81969208
81979209 event_trace_init();
81989210
8199
- init_tracer_tracefs(&global_trace, d_tracer);
8200
- ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
9211
+ init_tracer_tracefs(&global_trace, NULL);
9212
+ ftrace_init_tracefs_toplevel(&global_trace, NULL);
82019213
8202
- trace_create_file("tracing_thresh", 0644, d_tracer,
9214
+ trace_create_file("tracing_thresh", 0644, NULL,
82039215 &global_trace, &tracing_thresh_fops);
82049216
8205
- trace_create_file("README", 0444, d_tracer,
9217
+ trace_create_file("README", 0444, NULL,
82069218 NULL, &tracing_readme_fops);
82079219
8208
- trace_create_file("saved_cmdlines", 0444, d_tracer,
9220
+ trace_create_file("saved_cmdlines", 0444, NULL,
82099221 NULL, &tracing_saved_cmdlines_fops);
82109222
8211
- trace_create_file("saved_cmdlines_size", 0644, d_tracer,
9223
+ trace_create_file("saved_cmdlines_size", 0644, NULL,
82129224 NULL, &tracing_saved_cmdlines_size_fops);
82139225
8214
- trace_create_file("saved_tgids", 0444, d_tracer,
9226
+ trace_create_file("saved_tgids", 0444, NULL,
82159227 NULL, &tracing_saved_tgids_fops);
82169228
82179229 trace_eval_init();
82189230
8219
- trace_create_eval_file(d_tracer);
9231
+ trace_create_eval_file(NULL);
82209232
82219233 #ifdef CONFIG_MODULES
82229234 register_module_notifier(&trace_module_nb);
82239235 #endif
82249236
82259237 #ifdef CONFIG_DYNAMIC_FTRACE
8226
- trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8227
- &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
9238
+ trace_create_file("dyn_ftrace_total_info", 0444, NULL,
9239
+ NULL, &tracing_dyn_info_fops);
82289240 #endif
82299241
8230
- create_trace_instances(d_tracer);
9242
+ create_trace_instances(NULL);
82319243
82329244 update_tracer_options(&global_trace);
82339245
....@@ -8237,8 +9249,17 @@
82379249 static int trace_panic_handler(struct notifier_block *this,
82389250 unsigned long event, void *unused)
82399251 {
9252
+ bool ftrace_check = false;
9253
+
9254
+ trace_android_vh_ftrace_oops_enter(&ftrace_check);
9255
+
9256
+ if (ftrace_check)
9257
+ return NOTIFY_OK;
9258
+
82409259 if (ftrace_dump_on_oops)
82419260 ftrace_dump(ftrace_dump_on_oops);
9261
+
9262
+ trace_android_vh_ftrace_oops_exit(&ftrace_check);
82429263 return NOTIFY_OK;
82439264 }
82449265
....@@ -8252,6 +9273,13 @@
82529273 unsigned long val,
82539274 void *data)
82549275 {
9276
+ bool ftrace_check = false;
9277
+
9278
+ trace_android_vh_ftrace_oops_enter(&ftrace_check);
9279
+
9280
+ if (ftrace_check)
9281
+ return NOTIFY_OK;
9282
+
82559283 switch (val) {
82569284 case DIE_OOPS:
82579285 if (ftrace_dump_on_oops)
....@@ -8260,6 +9288,8 @@
82609288 default:
82619289 break;
82629290 }
9291
+
9292
+ trace_android_vh_ftrace_oops_exit(&ftrace_check);
82639293 return NOTIFY_OK;
82649294 }
82659295
....@@ -8284,6 +9314,8 @@
82849314 void
82859315 trace_printk_seq(struct trace_seq *s)
82869316 {
9317
+ bool dump_printk = true;
9318
+
82879319 /* Probably should print a warning here. */
82889320 if (s->seq.len >= TRACE_MAX_PRINT)
82899321 s->seq.len = TRACE_MAX_PRINT;
....@@ -8299,7 +9331,9 @@
82999331 /* should be zero ended, but we are paranoid. */
83009332 s->buffer[s->seq.len] = 0;
83019333
8302
- printk(KERN_TRACE "%s", s->buffer);
9334
+ trace_android_vh_ftrace_dump_buffer(s, &dump_printk);
9335
+ if (dump_printk)
9336
+ printk(KERN_TRACE "%s", s->buffer);
83039337
83049338 trace_seq_init(s);
83059339 }
....@@ -8309,13 +9343,13 @@
83099343 iter->tr = &global_trace;
83109344 iter->trace = iter->tr->current_trace;
83119345 iter->cpu_file = RING_BUFFER_ALL_CPUS;
8312
- iter->trace_buffer = &global_trace.trace_buffer;
9346
+ iter->array_buffer = &global_trace.array_buffer;
83139347
83149348 if (iter->trace && iter->trace->open)
83159349 iter->trace->open(iter);
83169350
83179351 /* Annotate start of buffers if we had overruns */
8318
- if (ring_buffer_overruns(iter->trace_buffer->buffer))
9352
+ if (ring_buffer_overruns(iter->array_buffer->buffer))
83199353 iter->iter_flags |= TRACE_FILE_ANNOTATE;
83209354
83219355 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
....@@ -8332,6 +9366,8 @@
83329366 unsigned int old_userobj;
83339367 unsigned long flags;
83349368 int cnt = 0, cpu;
9369
+ bool ftrace_check = false;
9370
+ unsigned long size;
83359371
83369372 /* Only allow one dump user at a time. */
83379373 if (atomic_inc_return(&dump_running) != 1) {
....@@ -8354,15 +9390,23 @@
83549390
83559391 /* Simulate the iterator */
83569392 trace_init_global_iter(&iter);
9393
+ /* Can not use kmalloc for iter.temp */
9394
+ iter.temp = static_temp_buf;
9395
+ iter.temp_size = STATIC_TEMP_BUF_SIZE;
83579396
83589397 for_each_tracing_cpu(cpu) {
8359
- atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
9398
+ atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
9399
+ size = ring_buffer_size(iter.array_buffer->buffer, cpu);
9400
+ trace_android_vh_ftrace_size_check(size, &ftrace_check);
83609401 }
83619402
83629403 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
83639404
83649405 /* don't look at user memory in panic mode */
83659406 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
9407
+
9408
+ if (ftrace_check)
9409
+ goto out_enable;
83669410
83679411 switch (oops_dump_mode) {
83689412 case DUMP_ALL:
....@@ -8387,13 +9431,14 @@
83879431 }
83889432
83899433 /*
8390
- * We need to stop all tracing on all CPUS to read the
9434
+ * We need to stop all tracing on all CPUS to read
83919435 * the next buffer. This is a bit expensive, but is
83929436 * not done often. We fill all what we can read,
83939437 * and then release the locks again.
83949438 */
83959439
83969440 while (!trace_empty(&iter)) {
9441
+ ftrace_check = true;
83979442
83989443 if (!cnt)
83999444 printk(KERN_TRACE "---------------------------------\n");
....@@ -8401,7 +9446,9 @@
84019446 cnt++;
84029447
84039448 trace_iterator_reset(&iter);
8404
- iter.iter_flags |= TRACE_FILE_LAT_FMT;
9449
+ trace_android_vh_ftrace_format_check(&ftrace_check);
9450
+ if (ftrace_check)
9451
+ iter.iter_flags |= TRACE_FILE_LAT_FMT;
84059452
84069453 if (trace_find_next_entry_inc(&iter) != NULL) {
84079454 int ret;
....@@ -8424,7 +9471,7 @@
84249471 tr->trace_flags |= old_userobj;
84259472
84269473 for_each_tracing_cpu(cpu) {
8427
- atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
9474
+ atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
84289475 }
84299476 atomic_dec(&dump_running);
84309477 printk_nmi_direct_exit();
....@@ -8523,8 +9570,14 @@
85239570 int ring_buf_size;
85249571 int ret = -ENOMEM;
85259572
9573
+
9574
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9575
+ pr_warn("Tracing disabled due to lockdown\n");
9576
+ return -EPERM;
9577
+ }
9578
+
85269579 /*
8527
- * Make sure we don't accidently add more trace options
9580
+ * Make sure we don't accidentally add more trace options
85289581 * than we have bits for.
85299582 */
85309583 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
....@@ -8553,7 +9606,7 @@
85539606
85549607 /*
85559608 * The prepare callbacks allocates some memory for the ring buffer. We
8556
- * don't free the buffer if the if the CPU goes down. If we were to free
9609
+ * don't free the buffer if the CPU goes down. If we were to free
85579610 * the buffer, then the user would lose any trace that was in the
85589611 * buffer. The memory will be removed once the "instance" is removed.
85599612 */
....@@ -8573,8 +9626,7 @@
85739626
85749627 /* TODO: make the number of buffers hot pluggable with CPUS */
85759628 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
8576
- printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
8577
- WARN_ON(1);
9629
+ MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
85789630 goto out_free_savedcmd;
85799631 }
85809632
....@@ -8619,6 +9671,7 @@
86199671 INIT_LIST_HEAD(&global_trace.systems);
86209672 INIT_LIST_HEAD(&global_trace.events);
86219673 INIT_LIST_HEAD(&global_trace.hist_vars);
9674
+ INIT_LIST_HEAD(&global_trace.err_log);
86229675 list_add(&global_trace.list, &ftrace_trace_arrays);
86239676
86249677 apply_trace_boot_options();
....@@ -8646,7 +9699,8 @@
86469699 if (tracepoint_printk) {
86479700 tracepoint_print_iter =
86489701 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
8649
- if (WARN_ON(!tracepoint_print_iter))
9702
+ if (MEM_FAIL(!tracepoint_print_iter,
9703
+ "Failed to allocate trace iterator\n"))
86509704 tracepoint_printk = 0;
86519705 else
86529706 static_key_enable(&tracepoint_printk_key.key);
....@@ -8686,6 +9740,11 @@
86869740 {
86879741 /* sched_clock_stable() is determined in late_initcall */
86889742 if (!trace_boot_clock && !sched_clock_stable()) {
9743
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9744
+ pr_warn("Can not set tracing clock due to lockdown\n");
9745
+ return -EPERM;
9746
+ }
9747
+
86899748 printk(KERN_WARNING
86909749 "Unstable clock detected, switching default tracing clock to \"global\"\n"
86919750 "If you want to keep using the local clock, then add:\n"