hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
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 /*
....@@ -1426,9 +1883,10 @@
14261883 * place on this CPU. We fail to record, but we reset
14271884 * the max trace buffer (no one writes directly to it)
14281885 * and flag that it failed.
1886
+ * Another reason is resize is in progress.
14291887 */
14301888 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1431
- "Failed to swap buffers due to commit in progress\n");
1889
+ "Failed to swap buffers due to commit or resize in progress\n");
14321890 }
14331891
14341892 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
....@@ -1438,13 +1896,13 @@
14381896 }
14391897 #endif /* CONFIG_TRACER_MAX_TRACE */
14401898
1441
-static int wait_on_pipe(struct trace_iterator *iter, bool full)
1899
+static int wait_on_pipe(struct trace_iterator *iter, int full)
14421900 {
14431901 /* Iterators are static, they should be filled or empty */
14441902 if (trace_buffer_iter(iter, iter->cpu_file))
14451903 return 0;
14461904
1447
- return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1905
+ return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
14481906 full);
14491907 }
14501908
....@@ -1495,7 +1953,7 @@
14951953 * internal tracing to verify that everything is in order.
14961954 * If we fail, we do not register this tracer.
14971955 */
1498
- tracing_reset_online_cpus(&tr->trace_buffer);
1956
+ tracing_reset_online_cpus(&tr->array_buffer);
14991957
15001958 tr->current_trace = type;
15011959
....@@ -1521,7 +1979,7 @@
15211979 return -1;
15221980 }
15231981 /* Only reset on passing, to avoid touching corrupted buffers */
1524
- tracing_reset_online_cpus(&tr->trace_buffer);
1982
+ tracing_reset_online_cpus(&tr->array_buffer);
15251983
15261984 #ifdef CONFIG_TRACER_MAX_TRACE
15271985 if (type->use_max_tr) {
....@@ -1555,6 +2013,10 @@
15552013
15562014 tracing_selftest_running = true;
15572015 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
2016
+ /* This loop can take minutes when sanitizers are enabled, so
2017
+ * lets make sure we allow RCU processing.
2018
+ */
2019
+ cond_resched();
15582020 ret = run_tracer_selftest(p->type);
15592021 /* If the test fails, then warn and remove from available_tracers */
15602022 if (ret < 0) {
....@@ -1593,7 +2055,7 @@
15932055
15942056 /**
15952057 * register_tracer - register a tracer with the ftrace system.
1596
- * @type - the plugin for the tracer
2058
+ * @type: the plugin for the tracer
15972059 *
15982060 * Register a new plugin tracer.
15992061 */
....@@ -1610,6 +2072,12 @@
16102072 if (strlen(type->name) >= MAX_TRACER_SIZE) {
16112073 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
16122074 return -1;
2075
+ }
2076
+
2077
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
2078
+ pr_warn("Can not register tracer %s due to lockdown\n",
2079
+ type->name);
2080
+ return -EPERM;
16132081 }
16142082
16152083 mutex_lock(&trace_types_lock);
....@@ -1670,19 +2138,15 @@
16702138 apply_trace_boot_options();
16712139
16722140 /* 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
2141
+ disable_tracing_selftest("running a tracer");
16782142
16792143 out_unlock:
16802144 return ret;
16812145 }
16822146
1683
-void tracing_reset(struct trace_buffer *buf, int cpu)
2147
+static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
16842148 {
1685
- struct ring_buffer *buffer = buf->buffer;
2149
+ struct trace_buffer *buffer = buf->buffer;
16862150
16872151 if (!buffer)
16882152 return;
....@@ -1690,16 +2154,15 @@
16902154 ring_buffer_record_disable(buffer);
16912155
16922156 /* Make sure all commits have finished */
1693
- synchronize_sched();
2157
+ synchronize_rcu();
16942158 ring_buffer_reset_cpu(buffer, cpu);
16952159
16962160 ring_buffer_record_enable(buffer);
16972161 }
16982162
1699
-void tracing_reset_online_cpus(struct trace_buffer *buf)
2163
+void tracing_reset_online_cpus(struct array_buffer *buf)
17002164 {
1701
- struct ring_buffer *buffer = buf->buffer;
1702
- int cpu;
2165
+ struct trace_buffer *buffer = buf->buffer;
17032166
17042167 if (!buffer)
17052168 return;
....@@ -1707,30 +2170,38 @@
17072170 ring_buffer_record_disable(buffer);
17082171
17092172 /* Make sure all commits have finished */
1710
- synchronize_sched();
2173
+ synchronize_rcu();
17112174
17122175 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
17132176
1714
- for_each_online_cpu(cpu)
1715
- ring_buffer_reset_cpu(buffer, cpu);
2177
+ ring_buffer_reset_online_cpus(buffer);
17162178
17172179 ring_buffer_record_enable(buffer);
17182180 }
17192181
17202182 /* Must have trace_types_lock held */
1721
-void tracing_reset_all_online_cpus(void)
2183
+void tracing_reset_all_online_cpus_unlocked(void)
17222184 {
17232185 struct trace_array *tr;
2186
+
2187
+ lockdep_assert_held(&trace_types_lock);
17242188
17252189 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
17262190 if (!tr->clear_trace)
17272191 continue;
17282192 tr->clear_trace = false;
1729
- tracing_reset_online_cpus(&tr->trace_buffer);
2193
+ tracing_reset_online_cpus(&tr->array_buffer);
17302194 #ifdef CONFIG_TRACER_MAX_TRACE
17312195 tracing_reset_online_cpus(&tr->max_buffer);
17322196 #endif
17332197 }
2198
+}
2199
+
2200
+void tracing_reset_all_online_cpus(void)
2201
+{
2202
+ mutex_lock(&trace_types_lock);
2203
+ tracing_reset_all_online_cpus_unlocked();
2204
+ mutex_unlock(&trace_types_lock);
17342205 }
17352206
17362207 /*
....@@ -1744,6 +2215,11 @@
17442215
17452216 #define SAVED_CMDLINES_DEFAULT 128
17462217 #define NO_CMDLINE_MAP UINT_MAX
2218
+/*
2219
+ * Preemption must be disabled before acquiring trace_cmdline_lock.
2220
+ * The various trace_arrays' max_lock must be acquired in a context
2221
+ * where interrupt is disabled.
2222
+ */
17472223 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
17482224 struct saved_cmdlines_buffer {
17492225 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
....@@ -1761,7 +2237,7 @@
17612237
17622238 static inline void set_cmdline(int idx, const char *cmdline)
17632239 {
1764
- memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
2240
+ strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
17652241 }
17662242
17672243 static int allocate_cmdlines_buffer(unsigned int val,
....@@ -1820,7 +2296,7 @@
18202296 */
18212297 void tracing_start(void)
18222298 {
1823
- struct ring_buffer *buffer;
2299
+ struct trace_buffer *buffer;
18242300 unsigned long flags;
18252301
18262302 if (tracing_disabled)
....@@ -1839,7 +2315,7 @@
18392315 /* Prevent the buffers from switching */
18402316 arch_spin_lock(&global_trace.max_lock);
18412317
1842
- buffer = global_trace.trace_buffer.buffer;
2318
+ buffer = global_trace.array_buffer.buffer;
18432319 if (buffer)
18442320 ring_buffer_record_enable(buffer);
18452321
....@@ -1857,7 +2333,7 @@
18572333
18582334 static void tracing_start_tr(struct trace_array *tr)
18592335 {
1860
- struct ring_buffer *buffer;
2336
+ struct trace_buffer *buffer;
18612337 unsigned long flags;
18622338
18632339 if (tracing_disabled)
....@@ -1878,7 +2354,7 @@
18782354 goto out;
18792355 }
18802356
1881
- buffer = tr->trace_buffer.buffer;
2357
+ buffer = tr->array_buffer.buffer;
18822358 if (buffer)
18832359 ring_buffer_record_enable(buffer);
18842360
....@@ -1894,7 +2370,7 @@
18942370 */
18952371 void tracing_stop(void)
18962372 {
1897
- struct ring_buffer *buffer;
2373
+ struct trace_buffer *buffer;
18982374 unsigned long flags;
18992375
19002376 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
....@@ -1904,7 +2380,7 @@
19042380 /* Prevent the buffers from switching */
19052381 arch_spin_lock(&global_trace.max_lock);
19062382
1907
- buffer = global_trace.trace_buffer.buffer;
2383
+ buffer = global_trace.array_buffer.buffer;
19082384 if (buffer)
19092385 ring_buffer_record_disable(buffer);
19102386
....@@ -1922,7 +2398,7 @@
19222398
19232399 static void tracing_stop_tr(struct trace_array *tr)
19242400 {
1925
- struct ring_buffer *buffer;
2401
+ struct trace_buffer *buffer;
19262402 unsigned long flags;
19272403
19282404 /* If global, we need to also stop the max tracer */
....@@ -1933,7 +2409,7 @@
19332409 if (tr->stop_count++)
19342410 goto out;
19352411
1936
- buffer = tr->trace_buffer.buffer;
2412
+ buffer = tr->array_buffer.buffer;
19372413 if (buffer)
19382414 ring_buffer_record_disable(buffer);
19392415
....@@ -1956,7 +2432,11 @@
19562432 * the lock, but we also don't want to spin
19572433 * nor do we want to disable interrupts,
19582434 * so if we miss here, then better luck next time.
2435
+ *
2436
+ * This is called within the scheduler and wake up, so interrupts
2437
+ * had better been disabled and run queue lock been held.
19592438 */
2439
+ lockdep_assert_preemption_disabled();
19602440 if (!arch_spin_trylock(&trace_cmdline_lock))
19612441 return 0;
19622442
....@@ -2064,9 +2544,9 @@
20642544 /**
20652545 * tracing_record_taskinfo - record the task info of a task
20662546 *
2067
- * @task - task to record
2068
- * @flags - TRACE_RECORD_CMDLINE for recording comm
2069
- * - TRACE_RECORD_TGID for recording tgid
2547
+ * @task: task to record
2548
+ * @flags: TRACE_RECORD_CMDLINE for recording comm
2549
+ * TRACE_RECORD_TGID for recording tgid
20702550 */
20712551 void tracing_record_taskinfo(struct task_struct *task, int flags)
20722552 {
....@@ -2092,10 +2572,10 @@
20922572 /**
20932573 * tracing_record_taskinfo_sched_switch - record task info for sched_switch
20942574 *
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
2575
+ * @prev: previous task during sched_switch
2576
+ * @next: next task during sched_switch
2577
+ * @flags: TRACE_RECORD_CMDLINE for recording comm
2578
+ * TRACE_RECORD_TGID for recording tgid
20992579 */
21002580 void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
21012581 struct task_struct *next, int flags)
....@@ -2145,14 +2625,14 @@
21452625 EXPORT_SYMBOL_GPL(trace_handle_return);
21462626
21472627 void
2148
-tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2149
- int pc)
2628
+tracing_generic_entry_update(struct trace_entry *entry, unsigned short type,
2629
+ unsigned long flags, int pc)
21502630 {
21512631 struct task_struct *tsk = current;
21522632
21532633 entry->preempt_count = pc & 0xff;
2154
- entry->preempt_lazy_count = preempt_lazy_count();
21552634 entry->pid = (tsk) ? tsk->pid : 0;
2635
+ entry->type = type;
21562636 entry->flags =
21572637 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
21582638 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
....@@ -2162,16 +2642,13 @@
21622642 ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
21632643 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
21642644 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
2165
- (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
2166
- (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
2645
+ (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
21672646 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
2168
-
2169
- entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
21702647 }
21712648 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
21722649
21732650 struct ring_buffer_event *
2174
-trace_buffer_lock_reserve(struct ring_buffer *buffer,
2651
+trace_buffer_lock_reserve(struct trace_buffer *buffer,
21752652 int type,
21762653 unsigned long len,
21772654 unsigned long flags, int pc)
....@@ -2221,7 +2698,7 @@
22212698
22222699 preempt_disable();
22232700 if (cpu == smp_processor_id() &&
2224
- this_cpu_read(trace_buffered_event) !=
2701
+ __this_cpu_read(trace_buffered_event) !=
22252702 per_cpu(trace_buffered_event, cpu))
22262703 WARN_ON_ONCE(1);
22272704 preempt_enable();
....@@ -2271,7 +2748,7 @@
22712748 preempt_enable();
22722749
22732750 /* Wait for all current users to finish */
2274
- synchronize_sched();
2751
+ synchronize_rcu();
22752752
22762753 for_each_tracing_cpu(cpu) {
22772754 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
....@@ -2290,10 +2767,10 @@
22902767 preempt_enable();
22912768 }
22922769
2293
-static struct ring_buffer *temp_buffer;
2770
+static struct trace_buffer *temp_buffer;
22942771
22952772 struct ring_buffer_event *
2296
-trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
2773
+trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
22972774 struct trace_event_file *trace_file,
22982775 int type, unsigned long len,
22992776 unsigned long flags, int pc)
....@@ -2301,7 +2778,7 @@
23012778 struct ring_buffer_event *entry;
23022779 int val;
23032780
2304
- *current_rb = trace_file->tr->trace_buffer.buffer;
2781
+ *current_rb = trace_file->tr->array_buffer.buffer;
23052782
23062783 if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags &
23072784 (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
....@@ -2321,7 +2798,7 @@
23212798 /*
23222799 * If tracing is off, but we have triggers enabled
23232800 * we still need to look at the event data. Use the temp_buffer
2324
- * to store the trace event for the tigger to use. It's recusive
2801
+ * to store the trace event for the trigger to use. It's recursive
23252802 * safe and will not be recorded anywhere.
23262803 */
23272804 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
....@@ -2333,12 +2810,13 @@
23332810 }
23342811 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
23352812
2336
-static DEFINE_SPINLOCK(tracepoint_iter_lock);
2813
+static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
23372814 static DEFINE_MUTEX(tracepoint_printk_mutex);
23382815
23392816 static void output_printk(struct trace_event_buffer *fbuffer)
23402817 {
23412818 struct trace_event_call *event_call;
2819
+ struct trace_event_file *file;
23422820 struct trace_event *event;
23432821 unsigned long flags;
23442822 struct trace_iterator *iter = tracepoint_print_iter;
....@@ -2352,20 +2830,26 @@
23522830 !event_call->event.funcs->trace)
23532831 return;
23542832
2833
+ file = fbuffer->trace_file;
2834
+ if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2835
+ (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2836
+ !filter_match_preds(file->filter, fbuffer->entry)))
2837
+ return;
2838
+
23552839 event = &fbuffer->trace_file->event_call->event;
23562840
2357
- spin_lock_irqsave(&tracepoint_iter_lock, flags);
2841
+ raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
23582842 trace_seq_init(&iter->seq);
23592843 iter->ent = fbuffer->entry;
23602844 event_call->event.funcs->trace(iter, 0, event);
23612845 trace_seq_putc(&iter->seq, 0);
23622846 printk("%s", iter->seq.buffer);
23632847
2364
- spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2848
+ raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
23652849 }
23662850
23672851 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2368
- void __user *buffer, size_t *lenp,
2852
+ void *buffer, size_t *lenp,
23692853 loff_t *ppos)
23702854 {
23712855 int save_tracepoint_printk;
....@@ -2402,9 +2886,11 @@
24022886 if (static_key_false(&tracepoint_printk_key.key))
24032887 output_printk(fbuffer);
24042888
2405
- event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2889
+ if (static_branch_unlikely(&trace_event_exports_enabled))
2890
+ ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
2891
+ event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer,
24062892 fbuffer->event, fbuffer->entry,
2407
- fbuffer->flags, fbuffer->pc);
2893
+ fbuffer->flags, fbuffer->pc, fbuffer->regs);
24082894 }
24092895 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
24102896
....@@ -2418,7 +2904,7 @@
24182904 # define STACK_SKIP 3
24192905
24202906 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2421
- struct ring_buffer *buffer,
2907
+ struct trace_buffer *buffer,
24222908 struct ring_buffer_event *event,
24232909 unsigned long flags, int pc,
24242910 struct pt_regs *regs)
....@@ -2439,134 +2925,11 @@
24392925 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
24402926 */
24412927 void
2442
-trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2928
+trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
24432929 struct ring_buffer_event *event)
24442930 {
24452931 __buffer_unlock_commit(buffer, event);
24462932 }
2447
-
2448
-static void
2449
-trace_process_export(struct trace_export *export,
2450
- struct ring_buffer_event *event)
2451
-{
2452
- struct trace_entry *entry;
2453
- unsigned int size = 0;
2454
-
2455
- entry = ring_buffer_event_data(event);
2456
- size = ring_buffer_event_length(event);
2457
- export->write(export, entry, size);
2458
-}
2459
-
2460
-static DEFINE_MUTEX(ftrace_export_lock);
2461
-
2462
-static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2463
-
2464
-static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2465
-
2466
-static inline void ftrace_exports_enable(void)
2467
-{
2468
- static_branch_enable(&ftrace_exports_enabled);
2469
-}
2470
-
2471
-static inline void ftrace_exports_disable(void)
2472
-{
2473
- static_branch_disable(&ftrace_exports_enabled);
2474
-}
2475
-
2476
-void ftrace_exports(struct ring_buffer_event *event)
2477
-{
2478
- struct trace_export *export;
2479
-
2480
- preempt_disable_notrace();
2481
-
2482
- export = rcu_dereference_raw_notrace(ftrace_exports_list);
2483
- while (export) {
2484
- trace_process_export(export, event);
2485
- export = rcu_dereference_raw_notrace(export->next);
2486
- }
2487
-
2488
- preempt_enable_notrace();
2489
-}
2490
-
2491
-static inline void
2492
-add_trace_export(struct trace_export **list, struct trace_export *export)
2493
-{
2494
- rcu_assign_pointer(export->next, *list);
2495
- /*
2496
- * We are entering export into the list but another
2497
- * CPU might be walking that list. We need to make sure
2498
- * the export->next pointer is valid before another CPU sees
2499
- * the export pointer included into the list.
2500
- */
2501
- rcu_assign_pointer(*list, export);
2502
-}
2503
-
2504
-static inline int
2505
-rm_trace_export(struct trace_export **list, struct trace_export *export)
2506
-{
2507
- struct trace_export **p;
2508
-
2509
- for (p = list; *p != NULL; p = &(*p)->next)
2510
- if (*p == export)
2511
- break;
2512
-
2513
- if (*p != export)
2514
- return -1;
2515
-
2516
- rcu_assign_pointer(*p, (*p)->next);
2517
-
2518
- return 0;
2519
-}
2520
-
2521
-static inline void
2522
-add_ftrace_export(struct trace_export **list, struct trace_export *export)
2523
-{
2524
- if (*list == NULL)
2525
- ftrace_exports_enable();
2526
-
2527
- add_trace_export(list, export);
2528
-}
2529
-
2530
-static inline int
2531
-rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2532
-{
2533
- int ret;
2534
-
2535
- ret = rm_trace_export(list, export);
2536
- if (*list == NULL)
2537
- ftrace_exports_disable();
2538
-
2539
- return ret;
2540
-}
2541
-
2542
-int register_ftrace_export(struct trace_export *export)
2543
-{
2544
- if (WARN_ON_ONCE(!export->write))
2545
- return -1;
2546
-
2547
- mutex_lock(&ftrace_export_lock);
2548
-
2549
- add_ftrace_export(&ftrace_exports_list, export);
2550
-
2551
- mutex_unlock(&ftrace_export_lock);
2552
-
2553
- return 0;
2554
-}
2555
-EXPORT_SYMBOL_GPL(register_ftrace_export);
2556
-
2557
-int unregister_ftrace_export(struct trace_export *export)
2558
-{
2559
- int ret;
2560
-
2561
- mutex_lock(&ftrace_export_lock);
2562
-
2563
- ret = rm_ftrace_export(&ftrace_exports_list, export);
2564
-
2565
- mutex_unlock(&ftrace_export_lock);
2566
-
2567
- return ret;
2568
-}
2569
-EXPORT_SYMBOL_GPL(unregister_ftrace_export);
25702933
25712934 void
25722935 trace_function(struct trace_array *tr,
....@@ -2574,7 +2937,7 @@
25742937 int pc)
25752938 {
25762939 struct trace_event_call *call = &event_function;
2577
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
2940
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
25782941 struct ring_buffer_event *event;
25792942 struct ftrace_entry *entry;
25802943
....@@ -2587,35 +2950,41 @@
25872950 entry->parent_ip = parent_ip;
25882951
25892952 if (!call_filter_check_discard(call, entry, buffer, event)) {
2590
- if (static_branch_unlikely(&ftrace_exports_enabled))
2591
- ftrace_exports(event);
2953
+ if (static_branch_unlikely(&trace_function_exports_enabled))
2954
+ ftrace_exports(event, TRACE_EXPORT_FUNCTION);
25922955 __buffer_unlock_commit(buffer, event);
25932956 }
25942957 }
25952958
25962959 #ifdef CONFIG_STACKTRACE
25972960
2598
-#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2961
+/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2962
+#define FTRACE_KSTACK_NESTING 4
2963
+
2964
+#define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
2965
+
25992966 struct ftrace_stack {
2600
- unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
2967
+ unsigned long calls[FTRACE_KSTACK_ENTRIES];
26012968 };
26022969
2603
-static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2970
+
2971
+struct ftrace_stacks {
2972
+ struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
2973
+};
2974
+
2975
+static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
26042976 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
26052977
2606
-static void __ftrace_trace_stack(struct ring_buffer *buffer,
2978
+static void __ftrace_trace_stack(struct trace_buffer *buffer,
26072979 unsigned long flags,
26082980 int skip, int pc, struct pt_regs *regs)
26092981 {
26102982 struct trace_event_call *call = &event_kernel_stack;
26112983 struct ring_buffer_event *event;
2984
+ unsigned int size, nr_entries;
2985
+ struct ftrace_stack *fstack;
26122986 struct stack_entry *entry;
2613
- struct stack_trace trace;
2614
- int use_stack;
2615
- int size = FTRACE_STACK_ENTRIES;
2616
-
2617
- trace.nr_entries = 0;
2618
- trace.skip = skip;
2987
+ int stackidx;
26192988
26202989 /*
26212990 * Add one, for this function and the call to save_stack_trace()
....@@ -2623,43 +2992,37 @@
26232992 */
26242993 #ifndef CONFIG_UNWINDER_ORC
26252994 if (!regs)
2626
- trace.skip++;
2995
+ skip++;
26272996 #endif
26282997
2629
- /*
2630
- * Since events can happen in NMIs there's no safe way to
2631
- * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2632
- * or NMI comes in, it will just have to use the default
2633
- * FTRACE_STACK_SIZE.
2634
- */
26352998 preempt_disable_notrace();
26362999
2637
- use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
3000
+ stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
3001
+
3002
+ /* This should never happen. If it does, yell once and skip */
3003
+ if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
3004
+ goto out;
3005
+
26383006 /*
2639
- * We don't need any atomic variables, just a barrier.
2640
- * If an interrupt comes in, we don't care, because it would
2641
- * have exited and put the counter back to what we want.
2642
- * We just need a barrier to keep gcc from moving things
2643
- * around.
3007
+ * The above __this_cpu_inc_return() is 'atomic' cpu local. An
3008
+ * interrupt will either see the value pre increment or post
3009
+ * increment. If the interrupt happens pre increment it will have
3010
+ * restored the counter when it returns. We just need a barrier to
3011
+ * keep gcc from moving things around.
26443012 */
26453013 barrier();
2646
- if (use_stack == 1) {
2647
- trace.entries = this_cpu_ptr(ftrace_stack.calls);
2648
- trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
26493014
2650
- if (regs)
2651
- save_stack_trace_regs(regs, &trace);
2652
- else
2653
- save_stack_trace(&trace);
3015
+ fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
3016
+ size = ARRAY_SIZE(fstack->calls);
26543017
2655
- if (trace.nr_entries > size)
2656
- size = trace.nr_entries;
2657
- } else
2658
- /* From now on, use_stack is a boolean */
2659
- use_stack = 0;
3018
+ if (regs) {
3019
+ nr_entries = stack_trace_save_regs(regs, fstack->calls,
3020
+ size, skip);
3021
+ } else {
3022
+ nr_entries = stack_trace_save(fstack->calls, size, skip);
3023
+ }
26603024
2661
- size *= sizeof(unsigned long);
2662
-
3025
+ size = nr_entries * sizeof(unsigned long);
26633026 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
26643027 (sizeof(*entry) - sizeof(entry->caller)) + size,
26653028 flags, pc);
....@@ -2667,21 +3030,8 @@
26673030 goto out;
26683031 entry = ring_buffer_event_data(event);
26693032
2670
- memset(&entry->caller, 0, size);
2671
-
2672
- if (use_stack)
2673
- memcpy(&entry->caller, trace.entries,
2674
- trace.nr_entries * sizeof(unsigned long));
2675
- else {
2676
- trace.max_entries = FTRACE_STACK_ENTRIES;
2677
- trace.entries = entry->caller;
2678
- if (regs)
2679
- save_stack_trace_regs(regs, &trace);
2680
- else
2681
- save_stack_trace(&trace);
2682
- }
2683
-
2684
- entry->size = trace.nr_entries;
3033
+ memcpy(&entry->caller, fstack->calls, size);
3034
+ entry->size = nr_entries;
26853035
26863036 if (!call_filter_check_discard(call, entry, buffer, event))
26873037 __buffer_unlock_commit(buffer, event);
....@@ -2695,7 +3045,7 @@
26953045 }
26963046
26973047 static inline void ftrace_trace_stack(struct trace_array *tr,
2698
- struct ring_buffer *buffer,
3048
+ struct trace_buffer *buffer,
26993049 unsigned long flags,
27003050 int skip, int pc, struct pt_regs *regs)
27013051 {
....@@ -2708,7 +3058,7 @@
27083058 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
27093059 int pc)
27103060 {
2711
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
3061
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
27123062
27133063 if (rcu_is_watching()) {
27143064 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
....@@ -2746,20 +3096,21 @@
27463096 /* Skip 1 to skip this function. */
27473097 skip++;
27483098 #endif
2749
- __ftrace_trace_stack(global_trace.trace_buffer.buffer,
3099
+ __ftrace_trace_stack(global_trace.array_buffer.buffer,
27503100 flags, skip, preempt_count(), NULL);
27513101 }
3102
+EXPORT_SYMBOL_GPL(trace_dump_stack);
27523103
3104
+#ifdef CONFIG_USER_STACKTRACE_SUPPORT
27533105 static DEFINE_PER_CPU(int, user_stack_count);
27543106
2755
-void
3107
+static void
27563108 ftrace_trace_userstack(struct trace_array *tr,
2757
- struct ring_buffer *buffer, unsigned long flags, int pc)
3109
+ struct trace_buffer *buffer, unsigned long flags, int pc)
27583110 {
27593111 struct trace_event_call *call = &event_user_stack;
27603112 struct ring_buffer_event *event;
27613113 struct userstack_entry *entry;
2762
- struct stack_trace trace;
27633114
27643115 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
27653116 return;
....@@ -2790,12 +3141,7 @@
27903141 entry->tgid = current->tgid;
27913142 memset(&entry->caller, 0, sizeof(entry->caller));
27923143
2793
- trace.nr_entries = 0;
2794
- trace.max_entries = FTRACE_STACK_ENTRIES;
2795
- trace.skip = 0;
2796
- trace.entries = entry->caller;
2797
-
2798
- save_stack_trace_user(&trace);
3144
+ stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
27993145 if (!call_filter_check_discard(call, entry, buffer, event))
28003146 __buffer_unlock_commit(buffer, event);
28013147
....@@ -2804,13 +3150,13 @@
28043150 out:
28053151 preempt_enable();
28063152 }
2807
-
2808
-#ifdef UNUSED
2809
-static void __trace_userstack(struct trace_array *tr, unsigned long flags)
3153
+#else /* CONFIG_USER_STACKTRACE_SUPPORT */
3154
+static void ftrace_trace_userstack(struct trace_array *tr,
3155
+ struct trace_buffer *buffer,
3156
+ unsigned long flags, int pc)
28103157 {
2811
- ftrace_trace_userstack(tr, flags, preempt_count());
28123158 }
2813
-#endif /* UNUSED */
3159
+#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
28143160
28153161 #endif /* CONFIG_STACKTRACE */
28163162
....@@ -2851,8 +3197,11 @@
28513197 {
28523198 struct trace_buffer_struct __percpu *buffers;
28533199
3200
+ if (trace_percpu_buffer)
3201
+ return 0;
3202
+
28543203 buffers = alloc_percpu(struct trace_buffer_struct);
2855
- if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
3204
+ if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
28563205 return -ENOMEM;
28573206
28583207 trace_percpu_buffer = buffers;
....@@ -2897,9 +3246,10 @@
28973246 * directly here. If the global_trace.buffer is already
28983247 * allocated here, then this was called by module code.
28993248 */
2900
- if (global_trace.trace_buffer.buffer)
3249
+ if (global_trace.array_buffer.buffer)
29013250 tracing_start_cmdline_record();
29023251 }
3252
+EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
29033253
29043254 void trace_printk_start_comm(void)
29053255 {
....@@ -2922,13 +3272,15 @@
29223272
29233273 /**
29243274 * trace_vbprintk - write binary msg to tracing buffer
2925
- *
3275
+ * @ip: The address of the caller
3276
+ * @fmt: The string format to write to the buffer
3277
+ * @args: Arguments for @fmt
29263278 */
29273279 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
29283280 {
29293281 struct trace_event_call *call = &event_bprint;
29303282 struct ring_buffer_event *event;
2931
- struct ring_buffer *buffer;
3283
+ struct trace_buffer *buffer;
29323284 struct trace_array *tr = &global_trace;
29333285 struct bprint_entry *entry;
29343286 unsigned long flags;
....@@ -2953,11 +3305,12 @@
29533305 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
29543306
29553307 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2956
- goto out;
3308
+ goto out_put;
29573309
29583310 local_save_flags(flags);
29593311 size = sizeof(*entry) + sizeof(u32) * len;
2960
- buffer = tr->trace_buffer.buffer;
3312
+ buffer = tr->array_buffer.buffer;
3313
+ ring_buffer_nest_start(buffer);
29613314 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
29623315 flags, pc);
29633316 if (!event)
....@@ -2973,6 +3326,8 @@
29733326 }
29743327
29753328 out:
3329
+ ring_buffer_nest_end(buffer);
3330
+out_put:
29763331 put_trace_buf();
29773332
29783333 out_nobuffer:
....@@ -2985,7 +3340,7 @@
29853340
29863341 __printf(3, 0)
29873342 static int
2988
-__trace_array_vprintk(struct ring_buffer *buffer,
3343
+__trace_array_vprintk(struct trace_buffer *buffer,
29893344 unsigned long ip, const char *fmt, va_list args)
29903345 {
29913346 struct trace_event_call *call = &event_print;
....@@ -3015,6 +3370,7 @@
30153370
30163371 local_save_flags(flags);
30173372 size = sizeof(*entry) + len + 1;
3373
+ ring_buffer_nest_start(buffer);
30183374 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
30193375 flags, pc);
30203376 if (!event)
....@@ -3029,6 +3385,7 @@
30293385 }
30303386
30313387 out:
3388
+ ring_buffer_nest_end(buffer);
30323389 put_trace_buf();
30333390
30343391 out_nobuffer:
....@@ -3042,9 +3399,29 @@
30423399 int trace_array_vprintk(struct trace_array *tr,
30433400 unsigned long ip, const char *fmt, va_list args)
30443401 {
3045
- return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3402
+ return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
30463403 }
30473404
3405
+/**
3406
+ * trace_array_printk - Print a message to a specific instance
3407
+ * @tr: The instance trace_array descriptor
3408
+ * @ip: The instruction pointer that this is called from.
3409
+ * @fmt: The format to print (printf format)
3410
+ *
3411
+ * If a subsystem sets up its own instance, they have the right to
3412
+ * printk strings into their tracing instance buffer using this
3413
+ * function. Note, this function will not write into the top level
3414
+ * buffer (use trace_printk() for that), as writing into the top level
3415
+ * buffer should only have events that can be individually disabled.
3416
+ * trace_printk() is only used for debugging a kernel, and should not
3417
+ * be ever encorporated in normal use.
3418
+ *
3419
+ * trace_array_printk() can be used, as it will not add noise to the
3420
+ * top level tracing buffer.
3421
+ *
3422
+ * Note, trace_array_init_printk() must be called on @tr before this
3423
+ * can be used.
3424
+ */
30483425 __printf(3, 0)
30493426 int trace_array_printk(struct trace_array *tr,
30503427 unsigned long ip, const char *fmt, ...)
....@@ -3052,20 +3429,46 @@
30523429 int ret;
30533430 va_list ap;
30543431
3055
- if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3056
- return 0;
3057
-
30583432 if (!tr)
30593433 return -ENOENT;
3434
+
3435
+ /* This is only allowed for created instances */
3436
+ if (tr == &global_trace)
3437
+ return 0;
3438
+
3439
+ if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3440
+ return 0;
30603441
30613442 va_start(ap, fmt);
30623443 ret = trace_array_vprintk(tr, ip, fmt, ap);
30633444 va_end(ap);
30643445 return ret;
30653446 }
3447
+EXPORT_SYMBOL_GPL(trace_array_printk);
3448
+
3449
+/**
3450
+ * trace_array_init_printk - Initialize buffers for trace_array_printk()
3451
+ * @tr: The trace array to initialize the buffers for
3452
+ *
3453
+ * As trace_array_printk() only writes into instances, they are OK to
3454
+ * have in the kernel (unlike trace_printk()). This needs to be called
3455
+ * before trace_array_printk() can be used on a trace_array.
3456
+ */
3457
+int trace_array_init_printk(struct trace_array *tr)
3458
+{
3459
+ if (!tr)
3460
+ return -ENOENT;
3461
+
3462
+ /* This is only allowed for created instances */
3463
+ if (tr == &global_trace)
3464
+ return -EINVAL;
3465
+
3466
+ return alloc_percpu_trace_buffer();
3467
+}
3468
+EXPORT_SYMBOL_GPL(trace_array_init_printk);
30663469
30673470 __printf(3, 4)
3068
-int trace_array_printk_buf(struct ring_buffer *buffer,
3471
+int trace_array_printk_buf(struct trace_buffer *buffer,
30693472 unsigned long ip, const char *fmt, ...)
30703473 {
30713474 int ret;
....@@ -3093,7 +3496,7 @@
30933496
30943497 iter->idx++;
30953498 if (buf_iter)
3096
- ring_buffer_read(buf_iter, NULL);
3499
+ ring_buffer_iter_advance(buf_iter);
30973500 }
30983501
30993502 static struct trace_entry *
....@@ -3103,11 +3506,15 @@
31033506 struct ring_buffer_event *event;
31043507 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
31053508
3106
- if (buf_iter)
3509
+ if (buf_iter) {
31073510 event = ring_buffer_iter_peek(buf_iter, ts);
3108
- else
3109
- event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
3511
+ if (lost_events)
3512
+ *lost_events = ring_buffer_iter_dropped(buf_iter) ?
3513
+ (unsigned long)-1 : 0;
3514
+ } else {
3515
+ event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
31103516 lost_events);
3517
+ }
31113518
31123519 if (event) {
31133520 iter->ent_size = ring_buffer_event_length(event);
....@@ -3121,7 +3528,7 @@
31213528 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
31223529 unsigned long *missing_events, u64 *ent_ts)
31233530 {
3124
- struct ring_buffer *buffer = iter->trace_buffer->buffer;
3531
+ struct trace_buffer *buffer = iter->array_buffer->buffer;
31253532 struct trace_entry *ent, *next = NULL;
31263533 unsigned long lost_events = 0, next_lost = 0;
31273534 int cpu_file = iter->cpu_file;
....@@ -3177,11 +3584,53 @@
31773584 return next;
31783585 }
31793586
3587
+#define STATIC_TEMP_BUF_SIZE 128
3588
+static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
3589
+
31803590 /* Find the next real entry, without updating the iterator itself */
31813591 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
31823592 int *ent_cpu, u64 *ent_ts)
31833593 {
3184
- return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3594
+ /* __find_next_entry will reset ent_size */
3595
+ int ent_size = iter->ent_size;
3596
+ struct trace_entry *entry;
3597
+
3598
+ /*
3599
+ * If called from ftrace_dump(), then the iter->temp buffer
3600
+ * will be the static_temp_buf and not created from kmalloc.
3601
+ * If the entry size is greater than the buffer, we can
3602
+ * not save it. Just return NULL in that case. This is only
3603
+ * used to add markers when two consecutive events' time
3604
+ * stamps have a large delta. See trace_print_lat_context()
3605
+ */
3606
+ if (iter->temp == static_temp_buf &&
3607
+ STATIC_TEMP_BUF_SIZE < ent_size)
3608
+ return NULL;
3609
+
3610
+ /*
3611
+ * The __find_next_entry() may call peek_next_entry(), which may
3612
+ * call ring_buffer_peek() that may make the contents of iter->ent
3613
+ * undefined. Need to copy iter->ent now.
3614
+ */
3615
+ if (iter->ent && iter->ent != iter->temp) {
3616
+ if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3617
+ !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
3618
+ void *temp;
3619
+ temp = kmalloc(iter->ent_size, GFP_KERNEL);
3620
+ if (!temp)
3621
+ return NULL;
3622
+ kfree(iter->temp);
3623
+ iter->temp = temp;
3624
+ iter->temp_size = iter->ent_size;
3625
+ }
3626
+ memcpy(iter->temp, iter->ent, iter->ent_size);
3627
+ iter->ent = iter->temp;
3628
+ }
3629
+ entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3630
+ /* Put back the original ent_size */
3631
+ iter->ent_size = ent_size;
3632
+
3633
+ return entry;
31853634 }
31863635
31873636 /* Find the next real entry, and increment the iterator to the next entry */
....@@ -3198,7 +3647,7 @@
31983647
31993648 static void trace_consume(struct trace_iterator *iter)
32003649 {
3201
- ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
3650
+ ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
32023651 &iter->lost_events);
32033652 }
32043653
....@@ -3231,12 +3680,11 @@
32313680
32323681 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
32333682 {
3234
- struct ring_buffer_event *event;
32353683 struct ring_buffer_iter *buf_iter;
32363684 unsigned long entries = 0;
32373685 u64 ts;
32383686
3239
- per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
3687
+ per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
32403688
32413689 buf_iter = trace_buffer_iter(iter, cpu);
32423690 if (!buf_iter)
....@@ -3249,14 +3697,14 @@
32493697 * that a reset never took place on a cpu. This is evident
32503698 * by the timestamp being before the start of the buffer.
32513699 */
3252
- while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
3253
- if (ts >= iter->trace_buffer->time_start)
3700
+ while (ring_buffer_iter_peek(buf_iter, &ts)) {
3701
+ if (ts >= iter->array_buffer->time_start)
32543702 break;
32553703 entries++;
3256
- ring_buffer_read(buf_iter, NULL);
3704
+ ring_buffer_iter_advance(buf_iter);
32573705 }
32583706
3259
- per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
3707
+ per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
32603708 }
32613709
32623710 /*
....@@ -3279,8 +3727,15 @@
32793727 * will point to the same string as current_trace->name.
32803728 */
32813729 mutex_lock(&trace_types_lock);
3282
- if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3730
+ if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) {
3731
+ /* Close iter->trace before switching to the new current tracer */
3732
+ if (iter->trace->close)
3733
+ iter->trace->close(iter);
32833734 *iter->trace = *tr->current_trace;
3735
+ /* Reopen the new current tracer */
3736
+ if (iter->trace->open)
3737
+ iter->trace->open(iter);
3738
+ }
32843739 mutex_unlock(&trace_types_lock);
32853740
32863741 #ifdef CONFIG_TRACER_MAX_TRACE
....@@ -3335,49 +3790,81 @@
33353790 }
33363791
33373792 static void
3338
-get_total_entries(struct trace_buffer *buf,
3339
- unsigned long *total, unsigned long *entries)
3793
+get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
3794
+ unsigned long *entries, int cpu)
33403795 {
33413796 unsigned long count;
3797
+
3798
+ count = ring_buffer_entries_cpu(buf->buffer, cpu);
3799
+ /*
3800
+ * If this buffer has skipped entries, then we hold all
3801
+ * entries for the trace and we need to ignore the
3802
+ * ones before the time stamp.
3803
+ */
3804
+ if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3805
+ count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3806
+ /* total is the same as the entries */
3807
+ *total = count;
3808
+ } else
3809
+ *total = count +
3810
+ ring_buffer_overrun_cpu(buf->buffer, cpu);
3811
+ *entries = count;
3812
+}
3813
+
3814
+static void
3815
+get_total_entries(struct array_buffer *buf,
3816
+ unsigned long *total, unsigned long *entries)
3817
+{
3818
+ unsigned long t, e;
33423819 int cpu;
33433820
33443821 *total = 0;
33453822 *entries = 0;
33463823
33473824 for_each_tracing_cpu(cpu) {
3348
- count = ring_buffer_entries_cpu(buf->buffer, cpu);
3349
- /*
3350
- * If this buffer has skipped entries, then we hold all
3351
- * entries for the trace and we need to ignore the
3352
- * ones before the time stamp.
3353
- */
3354
- if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3355
- count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3356
- /* total is the same as the entries */
3357
- *total += count;
3358
- } else
3359
- *total += count +
3360
- ring_buffer_overrun_cpu(buf->buffer, cpu);
3361
- *entries += count;
3825
+ get_total_entries_cpu(buf, &t, &e, cpu);
3826
+ *total += t;
3827
+ *entries += e;
33623828 }
3829
+}
3830
+
3831
+unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
3832
+{
3833
+ unsigned long total, entries;
3834
+
3835
+ if (!tr)
3836
+ tr = &global_trace;
3837
+
3838
+ get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
3839
+
3840
+ return entries;
3841
+}
3842
+
3843
+unsigned long trace_total_entries(struct trace_array *tr)
3844
+{
3845
+ unsigned long total, entries;
3846
+
3847
+ if (!tr)
3848
+ tr = &global_trace;
3849
+
3850
+ get_total_entries(&tr->array_buffer, &total, &entries);
3851
+
3852
+ return entries;
33633853 }
33643854
33653855 static void print_lat_help_header(struct seq_file *m)
33663856 {
3367
- seq_puts(m, "# _--------=> CPU# \n"
3368
- "# / _-------=> irqs-off \n"
3369
- "# | / _------=> need-resched \n"
3370
- "# || / _-----=> need-resched_lazy \n"
3371
- "# ||| / _----=> hardirq/softirq \n"
3372
- "# |||| / _---=> preempt-depth \n"
3373
- "# ||||| / _--=> preempt-lazy-depth\n"
3374
- "# |||||| / _-=> migrate-disable \n"
3375
- "# ||||||| / delay \n"
3376
- "# cmd pid |||||||| time | caller \n"
3377
- "# \\ / |||||||| \\ | / \n");
3857
+ seq_puts(m, "# _------=> CPU# \n"
3858
+ "# / _-----=> irqs-off \n"
3859
+ "# | / _----=> need-resched \n"
3860
+ "# || / _---=> hardirq/softirq \n"
3861
+ "# ||| / _--=> preempt-depth \n"
3862
+ "# |||| / delay \n"
3863
+ "# cmd pid ||||| time | caller \n"
3864
+ "# \\ / ||||| \\ | / \n");
33783865 }
33793866
3380
-static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
3867
+static void print_event_info(struct array_buffer *buf, struct seq_file *m)
33813868 {
33823869 unsigned long total;
33833870 unsigned long entries;
....@@ -3388,49 +3875,40 @@
33883875 seq_puts(m, "#\n");
33893876 }
33903877
3391
-static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3878
+static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
33923879 unsigned int flags)
33933880 {
33943881 bool tgid = flags & TRACE_ITER_RECORD_TGID;
33953882
33963883 print_event_info(buf, m);
33973884
3398
- seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : "");
3399
- seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
3885
+ seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
3886
+ seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
34003887 }
34013888
3402
-static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3889
+static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
34033890 unsigned int flags)
34043891 {
34053892 bool tgid = flags & TRACE_ITER_RECORD_TGID;
3406
- const char tgid_space[] = " ";
3407
- const char space[] = " ";
3893
+ const char *space = " ";
3894
+ int prec = tgid ? 12 : 2;
34083895
34093896 print_event_info(buf, m);
34103897
3411
- seq_printf(m, "# %s _-----=> irqs-off\n",
3412
- tgid ? tgid_space : space);
3413
- seq_printf(m, "# %s / _----=> need-resched\n",
3414
- tgid ? tgid_space : space);
3415
- seq_printf(m, "# %s| / _---=> need-resched_lazy\n",
3416
- tgid ? tgid_space : space);
3417
- seq_printf(m, "# %s|| / _--=> hardirq/softirq\n",
3418
- tgid ? tgid_space : space);
3419
- seq_printf(m, "# %s||| / preempt-depth\n",
3420
- tgid ? tgid_space : space);
3421
- seq_printf(m, "# %s|||| / delay\n",
3422
- tgid ? tgid_space : space);
3423
- seq_printf(m, "# TASK-PID %sCPU# ||||| TIMESTAMP FUNCTION\n",
3424
- tgid ? " TGID " : space);
3425
- seq_printf(m, "# | | %s | ||||| | |\n",
3426
- tgid ? " | " : space);
3898
+ seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space);
3899
+ seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
3900
+ seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
3901
+ seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
3902
+ seq_printf(m, "# %.*s||| / delay\n", prec, space);
3903
+ seq_printf(m, "# TASK-PID %.*s CPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
3904
+ seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
34273905 }
34283906
34293907 void
34303908 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
34313909 {
34323910 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
3433
- struct trace_buffer *buf = iter->trace_buffer;
3911
+ struct array_buffer *buf = iter->array_buffer;
34343912 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
34353913 struct tracer *type = iter->trace;
34363914 unsigned long entries;
....@@ -3457,6 +3935,8 @@
34573935 "desktop",
34583936 #elif defined(CONFIG_PREEMPT)
34593937 "preempt",
3938
+#elif defined(CONFIG_PREEMPT_RT)
3939
+ "preempt_rt",
34603940 #else
34613941 "unknown",
34623942 #endif
....@@ -3503,7 +3983,7 @@
35033983 cpumask_test_cpu(iter->cpu, iter->started))
35043984 return;
35053985
3506
- if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
3986
+ if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
35073987 return;
35083988
35093989 if (cpumask_available(iter->started))
....@@ -3637,7 +4117,7 @@
36374117 if (!ring_buffer_iter_empty(buf_iter))
36384118 return 0;
36394119 } else {
3640
- if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
4120
+ if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
36414121 return 0;
36424122 }
36434123 return 1;
....@@ -3649,7 +4129,7 @@
36494129 if (!ring_buffer_iter_empty(buf_iter))
36504130 return 0;
36514131 } else {
3652
- if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
4132
+ if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
36534133 return 0;
36544134 }
36554135 }
....@@ -3665,8 +4145,12 @@
36654145 enum print_line_t ret;
36664146
36674147 if (iter->lost_events) {
3668
- trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3669
- iter->cpu, iter->lost_events);
4148
+ if (iter->lost_events == (unsigned long)-1)
4149
+ trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4150
+ iter->cpu);
4151
+ else
4152
+ trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4153
+ iter->cpu, iter->lost_events);
36704154 if (trace_seq_has_overflowed(&iter->seq))
36714155 return TRACE_TYPE_PARTIAL_LINE;
36724156 }
....@@ -3739,10 +4223,10 @@
37394223 } else {
37404224 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
37414225 if (trace_flags & TRACE_ITER_IRQ_INFO)
3742
- print_func_help_header_irq(iter->trace_buffer,
4226
+ print_func_help_header_irq(iter->array_buffer,
37434227 m, trace_flags);
37444228 else
3745
- print_func_help_header(iter->trace_buffer, m,
4229
+ print_func_help_header(iter->array_buffer, m,
37464230 trace_flags);
37474231 }
37484232 }
....@@ -3882,6 +4366,18 @@
38824366 goto release;
38834367
38844368 /*
4369
+ * trace_find_next_entry() may need to save off iter->ent.
4370
+ * It will place it into the iter->temp buffer. As most
4371
+ * events are less than 128, allocate a buffer of that size.
4372
+ * If one is greater, then trace_find_next_entry() will
4373
+ * allocate a new buffer to adjust for the bigger iter->ent.
4374
+ * It's not critical if it fails to get allocated here.
4375
+ */
4376
+ iter->temp = kmalloc(128, GFP_KERNEL);
4377
+ if (iter->temp)
4378
+ iter->temp_size = 128;
4379
+
4380
+ /*
38854381 * We make a copy of the current tracer to avoid concurrent
38864382 * changes on it while we are reading.
38874383 */
....@@ -3900,35 +4396,38 @@
39004396 #ifdef CONFIG_TRACER_MAX_TRACE
39014397 /* Currently only the top directory has a snapshot */
39024398 if (tr->current_trace->print_max || snapshot)
3903
- iter->trace_buffer = &tr->max_buffer;
4399
+ iter->array_buffer = &tr->max_buffer;
39044400 else
39054401 #endif
3906
- iter->trace_buffer = &tr->trace_buffer;
4402
+ iter->array_buffer = &tr->array_buffer;
39074403 iter->snapshot = snapshot;
39084404 iter->pos = -1;
39094405 iter->cpu_file = tracing_get_cpu(inode);
39104406 mutex_init(&iter->mutex);
39114407
39124408 /* Notify the tracer early; before we stop tracing. */
3913
- if (iter->trace && iter->trace->open)
4409
+ if (iter->trace->open)
39144410 iter->trace->open(iter);
39154411
39164412 /* Annotate start of buffers if we had overruns */
3917
- if (ring_buffer_overruns(iter->trace_buffer->buffer))
4413
+ if (ring_buffer_overruns(iter->array_buffer->buffer))
39184414 iter->iter_flags |= TRACE_FILE_ANNOTATE;
39194415
39204416 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
39214417 if (trace_clocks[tr->clock_id].in_ns)
39224418 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
39234419
3924
- /* stop the trace while dumping if we are not opening "snapshot" */
3925
- if (!iter->snapshot)
4420
+ /*
4421
+ * If pause-on-trace is enabled, then stop the trace while
4422
+ * dumping, unless this is the "snapshot" file
4423
+ */
4424
+ if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
39264425 tracing_stop_tr(tr);
39274426
39284427 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
39294428 for_each_tracing_cpu(cpu) {
39304429 iter->buffer_iter[cpu] =
3931
- ring_buffer_read_prepare(iter->trace_buffer->buffer,
4430
+ ring_buffer_read_prepare(iter->array_buffer->buffer,
39324431 cpu, GFP_KERNEL);
39334432 }
39344433 ring_buffer_read_prepare_sync();
....@@ -3939,7 +4438,7 @@
39394438 } else {
39404439 cpu = iter->cpu_file;
39414440 iter->buffer_iter[cpu] =
3942
- ring_buffer_read_prepare(iter->trace_buffer->buffer,
4441
+ ring_buffer_read_prepare(iter->array_buffer->buffer,
39434442 cpu, GFP_KERNEL);
39444443 ring_buffer_read_prepare_sync();
39454444 ring_buffer_read_start(iter->buffer_iter[cpu]);
....@@ -3953,6 +4452,7 @@
39534452 fail:
39544453 mutex_unlock(&trace_types_lock);
39554454 kfree(iter->trace);
4455
+ kfree(iter->temp);
39564456 kfree(iter->buffer_iter);
39574457 release:
39584458 seq_release_private(inode, file);
....@@ -3961,8 +4461,11 @@
39614461
39624462 int tracing_open_generic(struct inode *inode, struct file *filp)
39634463 {
3964
- if (tracing_disabled)
3965
- return -ENODEV;
4464
+ int ret;
4465
+
4466
+ ret = tracing_check_open_get_tr(NULL);
4467
+ if (ret)
4468
+ return ret;
39664469
39674470 filp->private_data = inode->i_private;
39684471 return 0;
....@@ -3977,17 +4480,43 @@
39774480 * Open and update trace_array ref count.
39784481 * Must have the current trace_array passed to it.
39794482 */
3980
-static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
4483
+int tracing_open_generic_tr(struct inode *inode, struct file *filp)
39814484 {
39824485 struct trace_array *tr = inode->i_private;
4486
+ int ret;
39834487
3984
- if (tracing_disabled)
3985
- return -ENODEV;
3986
-
3987
- if (trace_array_get(tr) < 0)
3988
- return -ENODEV;
4488
+ ret = tracing_check_open_get_tr(tr);
4489
+ if (ret)
4490
+ return ret;
39894491
39904492 filp->private_data = inode->i_private;
4493
+
4494
+ return 0;
4495
+}
4496
+
4497
+/*
4498
+ * The private pointer of the inode is the trace_event_file.
4499
+ * Update the tr ref count associated to it.
4500
+ */
4501
+int tracing_open_file_tr(struct inode *inode, struct file *filp)
4502
+{
4503
+ struct trace_event_file *file = inode->i_private;
4504
+ int ret;
4505
+
4506
+ ret = tracing_check_open_get_tr(file->tr);
4507
+ if (ret)
4508
+ return ret;
4509
+
4510
+ filp->private_data = inode->i_private;
4511
+
4512
+ return 0;
4513
+}
4514
+
4515
+int tracing_release_file_tr(struct inode *inode, struct file *filp)
4516
+{
4517
+ struct trace_event_file *file = inode->i_private;
4518
+
4519
+ trace_array_put(file->tr);
39914520
39924521 return 0;
39934522 }
....@@ -4016,7 +4545,7 @@
40164545 if (iter->trace && iter->trace->close)
40174546 iter->trace->close(iter);
40184547
4019
- if (!iter->snapshot)
4548
+ if (!iter->snapshot && tr->stop_count)
40204549 /* reenable tracing if it was previously enabled */
40214550 tracing_start_tr(tr);
40224551
....@@ -4026,6 +4555,7 @@
40264555
40274556 mutex_destroy(&iter->mutex);
40284557 free_cpumask_var(iter->started);
4558
+ kfree(iter->temp);
40294559 kfree(iter->trace);
40304560 kfree(iter->buffer_iter);
40314561 seq_release_private(inode, file);
....@@ -4054,15 +4584,16 @@
40544584 {
40554585 struct trace_array *tr = inode->i_private;
40564586 struct trace_iterator *iter;
4057
- int ret = 0;
4587
+ int ret;
40584588
4059
- if (trace_array_get(tr) < 0)
4060
- return -ENODEV;
4589
+ ret = tracing_check_open_get_tr(tr);
4590
+ if (ret)
4591
+ return ret;
40614592
40624593 /* If this file was open for write, then erase contents */
40634594 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
40644595 int cpu = tracing_get_cpu(inode);
4065
- struct trace_buffer *trace_buf = &tr->trace_buffer;
4596
+ struct array_buffer *trace_buf = &tr->array_buffer;
40664597
40674598 #ifdef CONFIG_TRACER_MAX_TRACE
40684599 if (tr->current_trace->print_max)
....@@ -4072,7 +4603,7 @@
40724603 if (cpu == RING_BUFFER_ALL_CPUS)
40734604 tracing_reset_online_cpus(trace_buf);
40744605 else
4075
- tracing_reset(trace_buf, cpu);
4606
+ tracing_reset_cpu(trace_buf, cpu);
40764607 }
40774608
40784609 if (file->f_mode & FMODE_READ) {
....@@ -4173,11 +4704,9 @@
41734704 struct seq_file *m;
41744705 int ret;
41754706
4176
- if (tracing_disabled)
4177
- return -ENODEV;
4178
-
4179
- if (trace_array_get(tr) < 0)
4180
- return -ENODEV;
4707
+ ret = tracing_check_open_get_tr(tr);
4708
+ if (ret)
4709
+ return ret;
41814710
41824711 ret = seq_open(file, &show_traces_seq_ops);
41834712 if (ret) {
....@@ -4221,6 +4750,8 @@
42214750 static const struct file_operations tracing_fops = {
42224751 .open = tracing_open,
42234752 .read = seq_read,
4753
+ .read_iter = seq_read_iter,
4754
+ .splice_read = generic_file_splice_read,
42244755 .write = tracing_write_stub,
42254756 .llseek = tracing_lseek,
42264757 .release = tracing_release,
....@@ -4261,20 +4792,13 @@
42614792 return count;
42624793 }
42634794
4264
-static ssize_t
4265
-tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4266
- size_t count, loff_t *ppos)
4795
+int tracing_set_cpumask(struct trace_array *tr,
4796
+ cpumask_var_t tracing_cpumask_new)
42674797 {
4268
- struct trace_array *tr = file_inode(filp)->i_private;
4269
- cpumask_var_t tracing_cpumask_new;
4270
- int err, cpu;
4798
+ int cpu;
42714799
4272
- if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4273
- return -ENOMEM;
4274
-
4275
- err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4276
- if (err)
4277
- goto err_unlock;
4800
+ if (!tr)
4801
+ return -EINVAL;
42784802
42794803 local_irq_disable();
42804804 arch_spin_lock(&tr->max_lock);
....@@ -4285,24 +4809,53 @@
42854809 */
42864810 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
42874811 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4288
- atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4289
- ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
4812
+ atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4813
+ ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
4814
+#ifdef CONFIG_TRACER_MAX_TRACE
4815
+ ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
4816
+#endif
42904817 }
42914818 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
42924819 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
4293
- atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
4294
- ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
4820
+ atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
4821
+ ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
4822
+#ifdef CONFIG_TRACER_MAX_TRACE
4823
+ ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
4824
+#endif
42954825 }
42964826 }
42974827 arch_spin_unlock(&tr->max_lock);
42984828 local_irq_enable();
42994829
43004830 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
4831
+
4832
+ return 0;
4833
+}
4834
+
4835
+static ssize_t
4836
+tracing_cpumask_write(struct file *filp, const char __user *ubuf,
4837
+ size_t count, loff_t *ppos)
4838
+{
4839
+ struct trace_array *tr = file_inode(filp)->i_private;
4840
+ cpumask_var_t tracing_cpumask_new;
4841
+ int err;
4842
+
4843
+ if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
4844
+ return -ENOMEM;
4845
+
4846
+ err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
4847
+ if (err)
4848
+ goto err_free;
4849
+
4850
+ err = tracing_set_cpumask(tr, tracing_cpumask_new);
4851
+ if (err)
4852
+ goto err_free;
4853
+
43014854 free_cpumask_var(tracing_cpumask_new);
43024855
43034856 return count;
43044857
4305
-err_unlock:
4858
+err_free:
43064859 free_cpumask_var(tracing_cpumask_new);
43074860
43084861 return err;
....@@ -4444,7 +4997,7 @@
44444997 ftrace_pid_follow_fork(tr, enabled);
44454998
44464999 if (mask == TRACE_ITER_OVERWRITE) {
4447
- ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
5000
+ ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
44485001 #ifdef CONFIG_TRACER_MAX_TRACE
44495002 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
44505003 #endif
....@@ -4458,19 +5011,21 @@
44585011 return 0;
44595012 }
44605013
4461
-static int trace_set_options(struct trace_array *tr, char *option)
5014
+int trace_set_options(struct trace_array *tr, char *option)
44625015 {
44635016 char *cmp;
44645017 int neg = 0;
44655018 int ret;
44665019 size_t orig_len = strlen(option);
5020
+ int len;
44675021
44685022 cmp = strstrip(option);
44695023
4470
- if (strncmp(cmp, "no", 2) == 0) {
5024
+ len = str_has_prefix(cmp, "no");
5025
+ if (len)
44715026 neg = 1;
4472
- cmp += 2;
4473
- }
5027
+
5028
+ cmp += len;
44745029
44755030 mutex_lock(&event_mutex);
44765031 mutex_lock(&trace_types_lock);
....@@ -4546,11 +5101,9 @@
45465101 struct trace_array *tr = inode->i_private;
45475102 int ret;
45485103
4549
- if (tracing_disabled)
4550
- return -ENODEV;
4551
-
4552
- if (trace_array_get(tr) < 0)
4553
- return -ENODEV;
5104
+ ret = tracing_check_open_get_tr(tr);
5105
+ if (ret)
5106
+ return ret;
45545107
45555108 ret = single_open(file, tracing_trace_options_show, inode->i_private);
45565109 if (ret < 0)
....@@ -4577,6 +5130,7 @@
45775130 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
45785131 " current_tracer\t- function and latency tracers\n"
45795132 " available_tracers\t- list of configured tracers for current_tracer\n"
5133
+ " error_log\t- error log for failed commands (that support it)\n"
45805134 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
45815135 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
45825136 " trace_clock\t\t-change the clock used to order events\n"
....@@ -4597,7 +5151,7 @@
45975151 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
45985152 "\t\t\t Remove sub-buffer with rmdir\n"
45995153 " trace_options\t\t- Set format or modify how tracing happens\n"
4600
- "\t\t\t Disable an option by adding a suffix 'no' to the\n"
5154
+ "\t\t\t Disable an option by prefixing 'no' to the\n"
46015155 "\t\t\t option name\n"
46025156 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
46035157 #ifdef CONFIG_DYNAMIC_FTRACE
....@@ -4641,6 +5195,8 @@
46415195 #ifdef CONFIG_FUNCTION_TRACER
46425196 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
46435197 "\t\t (function)\n"
5198
+ " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5199
+ "\t\t (function)\n"
46445200 #endif
46455201 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
46465202 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
....@@ -4662,31 +5218,49 @@
46625218 "\t\t\t traces\n"
46635219 #endif
46645220 #endif /* CONFIG_STACK_TRACER */
5221
+#ifdef CONFIG_DYNAMIC_EVENTS
5222
+ " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5223
+ "\t\t\t Write into this file to define/undefine new trace events.\n"
5224
+#endif
46655225 #ifdef CONFIG_KPROBE_EVENTS
4666
- " kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
5226
+ " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
46675227 "\t\t\t Write into this file to define/undefine new trace events.\n"
46685228 #endif
46695229 #ifdef CONFIG_UPROBE_EVENTS
4670
- " uprobe_events\t\t- Add/remove/show the userspace dynamic events\n"
5230
+ " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
46715231 "\t\t\t Write into this file to define/undefine new trace events.\n"
46725232 #endif
46735233 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
46745234 "\t accepts: event-definitions (one definition per line)\n"
46755235 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n"
46765236 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
5237
+#ifdef CONFIG_HIST_TRIGGERS
5238
+ "\t s:[synthetic/]<event> <field> [<field>]\n"
5239
+#endif
46775240 "\t -:[<group>/]<event>\n"
46785241 #ifdef CONFIG_KPROBE_EVENTS
46795242 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4680
- "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
5243
+ "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
46815244 #endif
46825245 #ifdef CONFIG_UPROBE_EVENTS
4683
- "\t place: <path>:<offset>\n"
5246
+ " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
46845247 #endif
46855248 "\t args: <name>=fetcharg[:type]\n"
46865249 "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
4687
- "\t $stack<index>, $stack, $retval, $comm\n"
4688
- "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string,\n"
4689
- "\t b<bit-width>@<bit-offset>/<container-size>\n"
5250
+#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
5251
+ "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
5252
+#else
5253
+ "\t $stack<index>, $stack, $retval, $comm,\n"
5254
+#endif
5255
+ "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5256
+ "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
5257
+ "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5258
+ "\t <type>\\[<array-size>\\]\n"
5259
+#ifdef CONFIG_HIST_TRIGGERS
5260
+ "\t field: <stype> <name>;\n"
5261
+ "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5262
+ "\t [unsigned] char/int/long\n"
5263
+#endif
46905264 #endif
46915265 " events/\t\t- Directory containing all trace event subsystems:\n"
46925266 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
....@@ -4739,6 +5313,7 @@
47395313 "\t [:size=#entries]\n"
47405314 "\t [:pause][:continue][:clear]\n"
47415315 "\t [:name=histname1]\n"
5316
+ "\t [:<handler>.<action>]\n"
47425317 "\t [if <filter>]\n\n"
47435318 "\t Note, special fields can be used as well:\n"
47445319 "\t common_timestamp - to record current timestamp\n"
....@@ -4783,8 +5358,26 @@
47835358 "\t unchanged.\n\n"
47845359 "\t The enable_hist and disable_hist triggers can be used to\n"
47855360 "\t have one event conditionally start and stop another event's\n"
4786
- "\t already-attached hist trigger. The syntax is analagous to\n"
4787
- "\t the enable_event and disable_event triggers.\n"
5361
+ "\t already-attached hist trigger. The syntax is analogous to\n"
5362
+ "\t the enable_event and disable_event triggers.\n\n"
5363
+ "\t Hist trigger handlers and actions are executed whenever a\n"
5364
+ "\t a histogram entry is added or updated. They take the form:\n\n"
5365
+ "\t <handler>.<action>\n\n"
5366
+ "\t The available handlers are:\n\n"
5367
+ "\t onmatch(matching.event) - invoke on addition or update\n"
5368
+ "\t onmax(var) - invoke if var exceeds current max\n"
5369
+ "\t onchange(var) - invoke action if var changes\n\n"
5370
+ "\t The available actions are:\n\n"
5371
+ "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5372
+ "\t save(field,...) - save current event fields\n"
5373
+#ifdef CONFIG_TRACER_SNAPSHOT
5374
+ "\t snapshot() - snapshot the trace buffer\n\n"
5375
+#endif
5376
+#ifdef CONFIG_SYNTH_EVENTS
5377
+ " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5378
+ "\t Write into this file to define/undefine new synthetic events.\n"
5379
+ "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
5380
+#endif
47885381 #endif
47895382 ;
47905383
....@@ -4842,8 +5435,11 @@
48425435
48435436 static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
48445437 {
4845
- if (tracing_disabled)
4846
- return -ENODEV;
5438
+ int ret;
5439
+
5440
+ ret = tracing_check_open_get_tr(NULL);
5441
+ if (ret)
5442
+ return ret;
48475443
48485444 return seq_open(filp, &tracing_saved_tgids_seq_ops);
48495445 }
....@@ -4919,8 +5515,11 @@
49195515
49205516 static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
49215517 {
4922
- if (tracing_disabled)
4923
- return -ENODEV;
5518
+ int ret;
5519
+
5520
+ ret = tracing_check_open_get_tr(NULL);
5521
+ if (ret)
5522
+ return ret;
49245523
49255524 return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
49265525 }
....@@ -4939,9 +5538,11 @@
49395538 char buf[64];
49405539 int r;
49415540
5541
+ preempt_disable();
49425542 arch_spin_lock(&trace_cmdline_lock);
49435543 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
49445544 arch_spin_unlock(&trace_cmdline_lock);
5545
+ preempt_enable();
49455546
49465547 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
49475548 }
....@@ -4966,10 +5567,12 @@
49665567 return -ENOMEM;
49675568 }
49685569
5570
+ preempt_disable();
49695571 arch_spin_lock(&trace_cmdline_lock);
49705572 savedcmd_temp = savedcmd;
49715573 savedcmd = s;
49725574 arch_spin_unlock(&trace_cmdline_lock);
5575
+ preempt_enable();
49735576 free_saved_cmdlines_buffer(savedcmd_temp);
49745577
49755578 return 0;
....@@ -5028,14 +5631,12 @@
50285631 * Paranoid! If ptr points to end, we don't want to increment past it.
50295632 * This really should never happen.
50305633 */
5634
+ (*pos)++;
50315635 ptr = update_eval_map(ptr);
50325636 if (WARN_ON_ONCE(!ptr))
50335637 return NULL;
50345638
50355639 ptr++;
5036
-
5037
- (*pos)++;
5038
-
50395640 ptr = update_eval_map(ptr);
50405641
50415642 return ptr;
....@@ -5084,8 +5685,11 @@
50845685
50855686 static int tracing_eval_map_open(struct inode *inode, struct file *filp)
50865687 {
5087
- if (tracing_disabled)
5088
- return -ENODEV;
5688
+ int ret;
5689
+
5690
+ ret = tracing_check_open_get_tr(NULL);
5691
+ if (ret)
5692
+ return ret;
50895693
50905694 return seq_open(filp, &tracing_eval_map_seq_ops);
50915695 }
....@@ -5198,11 +5802,11 @@
51985802
51995803 int tracer_init(struct tracer *t, struct trace_array *tr)
52005804 {
5201
- tracing_reset_online_cpus(&tr->trace_buffer);
5805
+ tracing_reset_online_cpus(&tr->array_buffer);
52025806 return t->init(tr);
52035807 }
52045808
5205
-static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
5809
+static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
52065810 {
52075811 int cpu;
52085812
....@@ -5212,8 +5816,8 @@
52125816
52135817 #ifdef CONFIG_TRACER_MAX_TRACE
52145818 /* resize @tr's buffer to the size of @size_tr's entries */
5215
-static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
5216
- struct trace_buffer *size_buf, int cpu_id)
5819
+static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5820
+ struct array_buffer *size_buf, int cpu_id)
52175821 {
52185822 int cpu, ret = 0;
52195823
....@@ -5251,10 +5855,10 @@
52515855 ring_buffer_expanded = true;
52525856
52535857 /* May be called before buffers are initialized */
5254
- if (!tr->trace_buffer.buffer)
5858
+ if (!tr->array_buffer.buffer)
52555859 return 0;
52565860
5257
- ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
5861
+ ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
52585862 if (ret < 0)
52595863 return ret;
52605864
....@@ -5265,8 +5869,8 @@
52655869
52665870 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
52675871 if (ret < 0) {
5268
- int r = resize_buffer_duplicate_size(&tr->trace_buffer,
5269
- &tr->trace_buffer, cpu);
5872
+ int r = resize_buffer_duplicate_size(&tr->array_buffer,
5873
+ &tr->array_buffer, cpu);
52705874 if (r < 0) {
52715875 /*
52725876 * AARGH! We are left with different
....@@ -5297,15 +5901,15 @@
52975901 #endif /* CONFIG_TRACER_MAX_TRACE */
52985902
52995903 if (cpu == RING_BUFFER_ALL_CPUS)
5300
- set_buffer_entries(&tr->trace_buffer, size);
5904
+ set_buffer_entries(&tr->array_buffer, size);
53015905 else
5302
- per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
5906
+ per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size;
53035907
53045908 return ret;
53055909 }
53065910
5307
-static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5308
- unsigned long size, int cpu_id)
5911
+ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
5912
+ unsigned long size, int cpu_id)
53095913 {
53105914 int ret = size;
53115915
....@@ -5375,16 +5979,22 @@
53755979 tr->current_trace = &nop_trace;
53765980 }
53775981
5982
+static bool tracer_options_updated;
5983
+
53785984 static void add_tracer_options(struct trace_array *tr, struct tracer *t)
53795985 {
53805986 /* Only enable if the directory has been created already. */
53815987 if (!tr->dir)
53825988 return;
53835989
5990
+ /* Only create trace option files after update_tracer_options finish */
5991
+ if (!tracer_options_updated)
5992
+ return;
5993
+
53845994 create_trace_option_files(tr, t);
53855995 }
53865996
5387
-static int tracing_set_tracer(struct trace_array *tr, const char *buf)
5997
+int tracing_set_tracer(struct trace_array *tr, const char *buf)
53885998 {
53895999 struct tracer *t;
53906000 #ifdef CONFIG_TRACER_MAX_TRACE
....@@ -5413,6 +6023,18 @@
54136023 if (t == tr->current_trace)
54146024 goto out;
54156025
6026
+#ifdef CONFIG_TRACER_SNAPSHOT
6027
+ if (t->use_max_tr) {
6028
+ local_irq_disable();
6029
+ arch_spin_lock(&tr->max_lock);
6030
+ if (tr->cond_snapshot)
6031
+ ret = -EBUSY;
6032
+ arch_spin_unlock(&tr->max_lock);
6033
+ local_irq_enable();
6034
+ if (ret)
6035
+ goto out;
6036
+ }
6037
+#endif
54166038 /* Some tracers won't work on kernel command line */
54176039 if (system_state < SYSTEM_RUNNING && t->noboot) {
54186040 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
....@@ -5427,7 +6049,7 @@
54276049 }
54286050
54296051 /* If trace pipe files are being read, we can't change the tracer */
5430
- if (tr->current_trace->ref) {
6052
+ if (tr->trace_ref) {
54316053 ret = -EBUSY;
54326054 goto out;
54336055 }
....@@ -5439,11 +6061,11 @@
54396061 if (tr->current_trace->reset)
54406062 tr->current_trace->reset(tr);
54416063
5442
- /* Current trace needs to be nop_trace before synchronize_sched */
5443
- tr->current_trace = &nop_trace;
5444
-
54456064 #ifdef CONFIG_TRACER_MAX_TRACE
5446
- had_max_tr = tr->allocated_snapshot;
6065
+ had_max_tr = tr->current_trace->use_max_tr;
6066
+
6067
+ /* Current trace needs to be nop_trace before synchronize_rcu */
6068
+ tr->current_trace = &nop_trace;
54476069
54486070 if (had_max_tr && !t->use_max_tr) {
54496071 /*
....@@ -5453,17 +6075,17 @@
54536075 * The update_max_tr is called from interrupts disabled
54546076 * so a synchronized_sched() is sufficient.
54556077 */
5456
- synchronize_sched();
6078
+ synchronize_rcu();
54576079 free_snapshot(tr);
54586080 }
5459
-#endif
54606081
5461
-#ifdef CONFIG_TRACER_MAX_TRACE
5462
- if (t->use_max_tr && !had_max_tr) {
6082
+ if (t->use_max_tr && !tr->allocated_snapshot) {
54636083 ret = tracing_alloc_snapshot_instance(tr);
54646084 if (ret < 0)
54656085 goto out;
54666086 }
6087
+#else
6088
+ tr->current_trace = &nop_trace;
54676089 #endif
54686090
54696091 if (t->init) {
....@@ -5598,13 +6220,11 @@
55986220 {
55996221 struct trace_array *tr = inode->i_private;
56006222 struct trace_iterator *iter;
5601
- int ret = 0;
6223
+ int ret;
56026224
5603
- if (tracing_disabled)
5604
- return -ENODEV;
5605
-
5606
- if (trace_array_get(tr) < 0)
5607
- return -ENODEV;
6225
+ ret = tracing_check_open_get_tr(tr);
6226
+ if (ret)
6227
+ return ret;
56086228
56096229 mutex_lock(&trace_types_lock);
56106230
....@@ -5635,7 +6255,7 @@
56356255 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
56366256
56376257 iter->tr = tr;
5638
- iter->trace_buffer = &tr->trace_buffer;
6258
+ iter->array_buffer = &tr->array_buffer;
56396259 iter->cpu_file = tracing_get_cpu(inode);
56406260 mutex_init(&iter->mutex);
56416261 filp->private_data = iter;
....@@ -5645,7 +6265,7 @@
56456265
56466266 nonseekable_open(inode, filp);
56476267
5648
- tr->current_trace->ref++;
6268
+ tr->trace_ref++;
56496269 out:
56506270 mutex_unlock(&trace_types_lock);
56516271 return ret;
....@@ -5664,7 +6284,7 @@
56646284
56656285 mutex_lock(&trace_types_lock);
56666286
5667
- tr->current_trace->ref--;
6287
+ tr->trace_ref--;
56686288
56696289 if (iter->trace->pipe_close)
56706290 iter->trace->pipe_close(iter);
....@@ -5672,6 +6292,7 @@
56726292 mutex_unlock(&trace_types_lock);
56736293
56746294 free_cpumask_var(iter->started);
6295
+ kfree(iter->temp);
56756296 mutex_destroy(&iter->mutex);
56766297 kfree(iter);
56776298
....@@ -5695,8 +6316,8 @@
56956316 */
56966317 return EPOLLIN | EPOLLRDNORM;
56976318 else
5698
- return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
5699
- filp, poll_table);
6319
+ return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
6320
+ filp, poll_table, iter->tr->buffer_percent);
57006321 }
57016322
57026323 static __poll_t
....@@ -5733,7 +6354,7 @@
57336354
57346355 mutex_unlock(&iter->mutex);
57356356
5736
- ret = wait_on_pipe(iter, false);
6357
+ ret = wait_on_pipe(iter, 0);
57376358
57386359 mutex_lock(&iter->mutex);
57396360
....@@ -5804,7 +6425,20 @@
58046425
58056426 ret = print_trace_line(iter);
58066427 if (ret == TRACE_TYPE_PARTIAL_LINE) {
5807
- /* don't print partial lines */
6428
+ /*
6429
+ * If one print_trace_line() fills entire trace_seq in one shot,
6430
+ * trace_seq_to_user() will returns -EBUSY because save_len == 0,
6431
+ * In this case, we need to consume it, otherwise, loop will peek
6432
+ * this event next time, resulting in an infinite loop.
6433
+ */
6434
+ if (save_len == 0) {
6435
+ iter->seq.full = 0;
6436
+ trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
6437
+ trace_consume(iter);
6438
+ break;
6439
+ }
6440
+
6441
+ /* In other cases, don't print partial lines */
58086442 iter->seq.seq.len = save_len;
58096443 break;
58106444 }
....@@ -5848,14 +6482,6 @@
58486482 {
58496483 __free_page(spd->pages[idx]);
58506484 }
5851
-
5852
-static const struct pipe_buf_operations tracing_pipe_buf_ops = {
5853
- .can_merge = 0,
5854
- .confirm = generic_pipe_buf_confirm,
5855
- .release = generic_pipe_buf_release,
5856
- .steal = generic_pipe_buf_steal,
5857
- .get = generic_pipe_buf_get,
5858
-};
58596485
58606486 static size_t
58616487 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
....@@ -5918,7 +6544,7 @@
59186544 .partial = partial_def,
59196545 .nr_pages = 0, /* This gets updated below. */
59206546 .nr_pages_max = PIPE_DEF_BUFFERS,
5921
- .ops = &tracing_pipe_buf_ops,
6547
+ .ops = &default_pipe_buf_ops,
59226548 .spd_release = tracing_spd_release_pipe,
59236549 };
59246550 ssize_t ret;
....@@ -6013,8 +6639,8 @@
60136639 for_each_tracing_cpu(cpu) {
60146640 /* fill in the size from first enabled cpu */
60156641 if (size == 0)
6016
- size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
6017
- if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
6642
+ size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6643
+ if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
60186644 buf_size_same = 0;
60196645 break;
60206646 }
....@@ -6030,7 +6656,7 @@
60306656 } else
60316657 r = sprintf(buf, "X\n");
60326658 } else
6033
- r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
6659
+ r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
60346660
60356661 mutex_unlock(&trace_types_lock);
60366662
....@@ -6077,7 +6703,7 @@
60776703
60786704 mutex_lock(&trace_types_lock);
60796705 for_each_tracing_cpu(cpu) {
6080
- size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
6706
+ size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
60816707 if (!ring_buffer_expanded)
60826708 expanded_size += trace_buf_size >> 10;
60836709 }
....@@ -6127,16 +6753,16 @@
61276753 struct trace_array *tr = filp->private_data;
61286754 struct ring_buffer_event *event;
61296755 enum event_trigger_type tt = ETT_NONE;
6130
- struct ring_buffer *buffer;
6756
+ struct trace_buffer *buffer;
61316757 struct print_entry *entry;
61326758 unsigned long irq_flags;
6133
- const char faulted[] = "<faulted>";
61346759 ssize_t written;
61356760 int size;
61366761 int len;
61376762
61386763 /* Used in tracing_mark_raw_write() as well */
6139
-#define FAULTED_SIZE (sizeof(faulted) - 1) /* '\0' is already accounted for */
6764
+#define FAULTED_STR "<faulted>"
6765
+#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
61406766
61416767 if (tracing_disabled)
61426768 return -EINVAL;
....@@ -6156,7 +6782,7 @@
61566782 if (cnt < FAULTED_SIZE)
61576783 size += FAULTED_SIZE - cnt;
61586784
6159
- buffer = tr->trace_buffer.buffer;
6785
+ buffer = tr->array_buffer.buffer;
61606786 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
61616787 irq_flags, preempt_count());
61626788 if (unlikely(!event))
....@@ -6168,12 +6794,11 @@
61686794
61696795 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
61706796 if (len) {
6171
- memcpy(&entry->buf, faulted, FAULTED_SIZE);
6797
+ memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
61726798 cnt = FAULTED_SIZE;
61736799 written = -EFAULT;
61746800 } else
61756801 written = cnt;
6176
- len = cnt;
61776802
61786803 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
61796804 /* do not add \n before testing triggers, but add \0 */
....@@ -6187,6 +6812,8 @@
61876812 } else
61886813 entry->buf[cnt] = '\0';
61896814
6815
+ if (static_branch_unlikely(&trace_marker_exports_enabled))
6816
+ ftrace_exports(event, TRACE_EXPORT_MARKER);
61906817 __buffer_unlock_commit(buffer, event);
61916818
61926819 if (tt)
....@@ -6207,9 +6834,8 @@
62076834 {
62086835 struct trace_array *tr = filp->private_data;
62096836 struct ring_buffer_event *event;
6210
- struct ring_buffer *buffer;
6837
+ struct trace_buffer *buffer;
62116838 struct raw_data_entry *entry;
6212
- const char faulted[] = "<faulted>";
62136839 unsigned long irq_flags;
62146840 ssize_t written;
62156841 int size;
....@@ -6237,7 +6863,7 @@
62376863 if (cnt < FAULT_SIZE_ID)
62386864 size += FAULT_SIZE_ID - cnt;
62396865
6240
- buffer = tr->trace_buffer.buffer;
6866
+ buffer = tr->array_buffer.buffer;
62416867 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
62426868 irq_flags, preempt_count());
62436869 if (!event)
....@@ -6249,7 +6875,7 @@
62496875 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
62506876 if (len) {
62516877 entry->id = -1;
6252
- memcpy(&entry->buf, faulted, FAULTED_SIZE);
6878
+ memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
62536879 written = -EFAULT;
62546880 } else
62556881 written = cnt;
....@@ -6292,13 +6918,13 @@
62926918
62936919 tr->clock_id = i;
62946920
6295
- ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
6921
+ ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
62966922
62976923 /*
62986924 * New clock may not be consistent with the previous clock.
62996925 * Reset the buffer so that it doesn't have incomparable timestamps.
63006926 */
6301
- tracing_reset_online_cpus(&tr->trace_buffer);
6927
+ tracing_reset_online_cpus(&tr->array_buffer);
63026928
63036929 #ifdef CONFIG_TRACER_MAX_TRACE
63046930 if (tr->max_buffer.buffer)
....@@ -6344,11 +6970,9 @@
63446970 struct trace_array *tr = inode->i_private;
63456971 int ret;
63466972
6347
- if (tracing_disabled)
6348
- return -ENODEV;
6349
-
6350
- if (trace_array_get(tr))
6351
- return -ENODEV;
6973
+ ret = tracing_check_open_get_tr(tr);
6974
+ if (ret)
6975
+ return ret;
63526976
63536977 ret = single_open(file, tracing_clock_show, inode->i_private);
63546978 if (ret < 0)
....@@ -6363,7 +6987,7 @@
63636987
63646988 mutex_lock(&trace_types_lock);
63656989
6366
- if (ring_buffer_time_stamp_abs(tr->trace_buffer.buffer))
6990
+ if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
63676991 seq_puts(m, "delta [absolute]\n");
63686992 else
63696993 seq_puts(m, "[delta] absolute\n");
....@@ -6378,11 +7002,9 @@
63787002 struct trace_array *tr = inode->i_private;
63797003 int ret;
63807004
6381
- if (tracing_disabled)
6382
- return -ENODEV;
6383
-
6384
- if (trace_array_get(tr))
6385
- return -ENODEV;
7005
+ ret = tracing_check_open_get_tr(tr);
7006
+ if (ret)
7007
+ return ret;
63867008
63877009 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
63887010 if (ret < 0)
....@@ -6410,7 +7032,7 @@
64107032 goto out;
64117033 }
64127034
6413
- ring_buffer_set_time_stamp_abs(tr->trace_buffer.buffer, abs);
7035
+ ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs);
64147036
64157037 #ifdef CONFIG_TRACER_MAX_TRACE
64167038 if (tr->max_buffer.buffer)
....@@ -6435,10 +7057,11 @@
64357057 struct trace_array *tr = inode->i_private;
64367058 struct trace_iterator *iter;
64377059 struct seq_file *m;
6438
- int ret = 0;
7060
+ int ret;
64397061
6440
- if (trace_array_get(tr) < 0)
6441
- return -ENODEV;
7062
+ ret = tracing_check_open_get_tr(tr);
7063
+ if (ret)
7064
+ return ret;
64427065
64437066 if (file->f_mode & FMODE_READ) {
64447067 iter = __tracing_open(inode, file, true);
....@@ -6458,7 +7081,7 @@
64587081 ret = 0;
64597082
64607083 iter->tr = tr;
6461
- iter->trace_buffer = &tr->max_buffer;
7084
+ iter->array_buffer = &tr->max_buffer;
64627085 iter->cpu_file = tracing_get_cpu(inode);
64637086 m->private = iter;
64647087 file->private_data = m;
....@@ -6468,6 +7091,11 @@
64687091 trace_array_put(tr);
64697092
64707093 return ret;
7094
+}
7095
+
7096
+static void tracing_swap_cpu_buffer(void *tr)
7097
+{
7098
+ update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
64717099 }
64727100
64737101 static ssize_t
....@@ -6495,6 +7123,15 @@
64957123 goto out;
64967124 }
64977125
7126
+ local_irq_disable();
7127
+ arch_spin_lock(&tr->max_lock);
7128
+ if (tr->cond_snapshot)
7129
+ ret = -EBUSY;
7130
+ arch_spin_unlock(&tr->max_lock);
7131
+ local_irq_enable();
7132
+ if (ret)
7133
+ goto out;
7134
+
64987135 switch (val) {
64997136 case 0:
65007137 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
....@@ -6514,25 +7151,27 @@
65147151 #endif
65157152 if (tr->allocated_snapshot)
65167153 ret = resize_buffer_duplicate_size(&tr->max_buffer,
6517
- &tr->trace_buffer, iter->cpu_file);
7154
+ &tr->array_buffer, iter->cpu_file);
65187155 else
65197156 ret = tracing_alloc_snapshot_instance(tr);
65207157 if (ret < 0)
65217158 break;
6522
- local_irq_disable();
65237159 /* Now, we're going to swap */
6524
- if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
6525
- update_max_tr(tr, current, smp_processor_id());
6526
- else
6527
- update_max_tr_single(tr, current, iter->cpu_file);
6528
- local_irq_enable();
7160
+ if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
7161
+ local_irq_disable();
7162
+ update_max_tr(tr, current, smp_processor_id(), NULL);
7163
+ local_irq_enable();
7164
+ } else {
7165
+ smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
7166
+ (void *)tr, 1);
7167
+ }
65297168 break;
65307169 default:
65317170 if (tr->allocated_snapshot) {
65327171 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
65337172 tracing_reset_online_cpus(&tr->max_buffer);
65347173 else
6535
- tracing_reset(&tr->max_buffer, iter->cpu_file);
7174
+ tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
65367175 }
65377176 break;
65387177 }
....@@ -6576,6 +7215,7 @@
65767215 struct ftrace_buffer_info *info;
65777216 int ret;
65787217
7218
+ /* The following checks for tracefs lockdown */
65797219 ret = tracing_buffers_open(inode, filp);
65807220 if (ret < 0)
65817221 return ret;
....@@ -6588,7 +7228,7 @@
65887228 }
65897229
65907230 info->iter.snapshot = true;
6591
- info->iter.trace_buffer = &info->iter.tr->max_buffer;
7231
+ info->iter.array_buffer = &info->iter.tr->max_buffer;
65927232
65937233 return ret;
65947234 }
....@@ -6613,10 +7253,11 @@
66137253 #endif
66147254
66157255 static const struct file_operations set_tracer_fops = {
6616
- .open = tracing_open_generic,
7256
+ .open = tracing_open_generic_tr,
66177257 .read = tracing_set_trace_read,
66187258 .write = tracing_set_trace_write,
66197259 .llseek = generic_file_llseek,
7260
+ .release = tracing_release_generic_tr,
66207261 };
66217262
66227263 static const struct file_operations tracing_pipe_fops = {
....@@ -6697,19 +7338,263 @@
66977338
66987339 #endif /* CONFIG_TRACER_SNAPSHOT */
66997340
7341
+#define TRACING_LOG_ERRS_MAX 8
7342
+#define TRACING_LOG_LOC_MAX 128
7343
+
7344
+#define CMD_PREFIX " Command: "
7345
+
7346
+struct err_info {
7347
+ const char **errs; /* ptr to loc-specific array of err strings */
7348
+ u8 type; /* index into errs -> specific err string */
7349
+ u8 pos; /* MAX_FILTER_STR_VAL = 256 */
7350
+ u64 ts;
7351
+};
7352
+
7353
+struct tracing_log_err {
7354
+ struct list_head list;
7355
+ struct err_info info;
7356
+ char loc[TRACING_LOG_LOC_MAX]; /* err location */
7357
+ char cmd[MAX_FILTER_STR_VAL]; /* what caused err */
7358
+};
7359
+
7360
+static DEFINE_MUTEX(tracing_err_log_lock);
7361
+
7362
+static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr)
7363
+{
7364
+ struct tracing_log_err *err;
7365
+
7366
+ if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
7367
+ err = kzalloc(sizeof(*err), GFP_KERNEL);
7368
+ if (!err)
7369
+ err = ERR_PTR(-ENOMEM);
7370
+ else
7371
+ tr->n_err_log_entries++;
7372
+
7373
+ return err;
7374
+ }
7375
+
7376
+ err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
7377
+ list_del(&err->list);
7378
+
7379
+ return err;
7380
+}
7381
+
7382
+/**
7383
+ * err_pos - find the position of a string within a command for error careting
7384
+ * @cmd: The tracing command that caused the error
7385
+ * @str: The string to position the caret at within @cmd
7386
+ *
7387
+ * Finds the position of the first occurence of @str within @cmd. The
7388
+ * return value can be passed to tracing_log_err() for caret placement
7389
+ * within @cmd.
7390
+ *
7391
+ * Returns the index within @cmd of the first occurence of @str or 0
7392
+ * if @str was not found.
7393
+ */
7394
+unsigned int err_pos(char *cmd, const char *str)
7395
+{
7396
+ char *found;
7397
+
7398
+ if (WARN_ON(!strlen(cmd)))
7399
+ return 0;
7400
+
7401
+ found = strstr(cmd, str);
7402
+ if (found)
7403
+ return found - cmd;
7404
+
7405
+ return 0;
7406
+}
7407
+
7408
+/**
7409
+ * tracing_log_err - write an error to the tracing error log
7410
+ * @tr: The associated trace array for the error (NULL for top level array)
7411
+ * @loc: A string describing where the error occurred
7412
+ * @cmd: The tracing command that caused the error
7413
+ * @errs: The array of loc-specific static error strings
7414
+ * @type: The index into errs[], which produces the specific static err string
7415
+ * @pos: The position the caret should be placed in the cmd
7416
+ *
7417
+ * Writes an error into tracing/error_log of the form:
7418
+ *
7419
+ * <loc>: error: <text>
7420
+ * Command: <cmd>
7421
+ * ^
7422
+ *
7423
+ * tracing/error_log is a small log file containing the last
7424
+ * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
7425
+ * unless there has been a tracing error, and the error log can be
7426
+ * cleared and have its memory freed by writing the empty string in
7427
+ * truncation mode to it i.e. echo > tracing/error_log.
7428
+ *
7429
+ * NOTE: the @errs array along with the @type param are used to
7430
+ * produce a static error string - this string is not copied and saved
7431
+ * when the error is logged - only a pointer to it is saved. See
7432
+ * existing callers for examples of how static strings are typically
7433
+ * defined for use with tracing_log_err().
7434
+ */
7435
+void tracing_log_err(struct trace_array *tr,
7436
+ const char *loc, const char *cmd,
7437
+ const char **errs, u8 type, u8 pos)
7438
+{
7439
+ struct tracing_log_err *err;
7440
+
7441
+ if (!tr)
7442
+ tr = &global_trace;
7443
+
7444
+ mutex_lock(&tracing_err_log_lock);
7445
+ err = get_tracing_log_err(tr);
7446
+ if (PTR_ERR(err) == -ENOMEM) {
7447
+ mutex_unlock(&tracing_err_log_lock);
7448
+ return;
7449
+ }
7450
+
7451
+ snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7452
+ snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd);
7453
+
7454
+ err->info.errs = errs;
7455
+ err->info.type = type;
7456
+ err->info.pos = pos;
7457
+ err->info.ts = local_clock();
7458
+
7459
+ list_add_tail(&err->list, &tr->err_log);
7460
+ mutex_unlock(&tracing_err_log_lock);
7461
+}
7462
+
7463
+static void clear_tracing_err_log(struct trace_array *tr)
7464
+{
7465
+ struct tracing_log_err *err, *next;
7466
+
7467
+ mutex_lock(&tracing_err_log_lock);
7468
+ list_for_each_entry_safe(err, next, &tr->err_log, list) {
7469
+ list_del(&err->list);
7470
+ kfree(err);
7471
+ }
7472
+
7473
+ tr->n_err_log_entries = 0;
7474
+ mutex_unlock(&tracing_err_log_lock);
7475
+}
7476
+
7477
+static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7478
+{
7479
+ struct trace_array *tr = m->private;
7480
+
7481
+ mutex_lock(&tracing_err_log_lock);
7482
+
7483
+ return seq_list_start(&tr->err_log, *pos);
7484
+}
7485
+
7486
+static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7487
+{
7488
+ struct trace_array *tr = m->private;
7489
+
7490
+ return seq_list_next(v, &tr->err_log, pos);
7491
+}
7492
+
7493
+static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7494
+{
7495
+ mutex_unlock(&tracing_err_log_lock);
7496
+}
7497
+
7498
+static void tracing_err_log_show_pos(struct seq_file *m, u8 pos)
7499
+{
7500
+ u8 i;
7501
+
7502
+ for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7503
+ seq_putc(m, ' ');
7504
+ for (i = 0; i < pos; i++)
7505
+ seq_putc(m, ' ');
7506
+ seq_puts(m, "^\n");
7507
+}
7508
+
7509
+static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7510
+{
7511
+ struct tracing_log_err *err = v;
7512
+
7513
+ if (err) {
7514
+ const char *err_text = err->info.errs[err->info.type];
7515
+ u64 sec = err->info.ts;
7516
+ u32 nsec;
7517
+
7518
+ nsec = do_div(sec, NSEC_PER_SEC);
7519
+ seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7520
+ err->loc, err_text);
7521
+ seq_printf(m, "%s", err->cmd);
7522
+ tracing_err_log_show_pos(m, err->info.pos);
7523
+ }
7524
+
7525
+ return 0;
7526
+}
7527
+
7528
+static const struct seq_operations tracing_err_log_seq_ops = {
7529
+ .start = tracing_err_log_seq_start,
7530
+ .next = tracing_err_log_seq_next,
7531
+ .stop = tracing_err_log_seq_stop,
7532
+ .show = tracing_err_log_seq_show
7533
+};
7534
+
7535
+static int tracing_err_log_open(struct inode *inode, struct file *file)
7536
+{
7537
+ struct trace_array *tr = inode->i_private;
7538
+ int ret = 0;
7539
+
7540
+ ret = tracing_check_open_get_tr(tr);
7541
+ if (ret)
7542
+ return ret;
7543
+
7544
+ /* If this file was opened for write, then erase contents */
7545
+ if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
7546
+ clear_tracing_err_log(tr);
7547
+
7548
+ if (file->f_mode & FMODE_READ) {
7549
+ ret = seq_open(file, &tracing_err_log_seq_ops);
7550
+ if (!ret) {
7551
+ struct seq_file *m = file->private_data;
7552
+ m->private = tr;
7553
+ } else {
7554
+ trace_array_put(tr);
7555
+ }
7556
+ }
7557
+ return ret;
7558
+}
7559
+
7560
+static ssize_t tracing_err_log_write(struct file *file,
7561
+ const char __user *buffer,
7562
+ size_t count, loff_t *ppos)
7563
+{
7564
+ return count;
7565
+}
7566
+
7567
+static int tracing_err_log_release(struct inode *inode, struct file *file)
7568
+{
7569
+ struct trace_array *tr = inode->i_private;
7570
+
7571
+ trace_array_put(tr);
7572
+
7573
+ if (file->f_mode & FMODE_READ)
7574
+ seq_release(inode, file);
7575
+
7576
+ return 0;
7577
+}
7578
+
7579
+static const struct file_operations tracing_err_log_fops = {
7580
+ .open = tracing_err_log_open,
7581
+ .write = tracing_err_log_write,
7582
+ .read = seq_read,
7583
+ .llseek = tracing_lseek,
7584
+ .release = tracing_err_log_release,
7585
+};
7586
+
67007587 static int tracing_buffers_open(struct inode *inode, struct file *filp)
67017588 {
67027589 struct trace_array *tr = inode->i_private;
67037590 struct ftrace_buffer_info *info;
67047591 int ret;
67057592
6706
- if (tracing_disabled)
6707
- return -ENODEV;
7593
+ ret = tracing_check_open_get_tr(tr);
7594
+ if (ret)
7595
+ return ret;
67087596
6709
- if (trace_array_get(tr) < 0)
6710
- return -ENODEV;
6711
-
6712
- info = kzalloc(sizeof(*info), GFP_KERNEL);
7597
+ info = kvzalloc(sizeof(*info), GFP_KERNEL);
67137598 if (!info) {
67147599 trace_array_put(tr);
67157600 return -ENOMEM;
....@@ -6720,14 +7605,14 @@
67207605 info->iter.tr = tr;
67217606 info->iter.cpu_file = tracing_get_cpu(inode);
67227607 info->iter.trace = tr->current_trace;
6723
- info->iter.trace_buffer = &tr->trace_buffer;
7608
+ info->iter.array_buffer = &tr->array_buffer;
67247609 info->spare = NULL;
67257610 /* Force reading ring buffer for first read */
67267611 info->read = (unsigned int)-1;
67277612
67287613 filp->private_data = info;
67297614
6730
- tr->current_trace->ref++;
7615
+ tr->trace_ref++;
67317616
67327617 mutex_unlock(&trace_types_lock);
67337618
....@@ -6765,7 +7650,7 @@
67657650 #endif
67667651
67677652 if (!info->spare) {
6768
- info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
7653
+ info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
67697654 iter->cpu_file);
67707655 if (IS_ERR(info->spare)) {
67717656 ret = PTR_ERR(info->spare);
....@@ -6783,7 +7668,7 @@
67837668
67847669 again:
67857670 trace_access_lock(iter->cpu_file);
6786
- ret = ring_buffer_read_page(iter->trace_buffer->buffer,
7671
+ ret = ring_buffer_read_page(iter->array_buffer->buffer,
67877672 &info->spare,
67887673 count,
67897674 iter->cpu_file, 0);
....@@ -6794,7 +7679,7 @@
67947679 if ((filp->f_flags & O_NONBLOCK))
67957680 return -EAGAIN;
67967681
6797
- ret = wait_on_pipe(iter, false);
7682
+ ret = wait_on_pipe(iter, 0);
67987683 if (ret)
67997684 return ret;
68007685
....@@ -6828,14 +7713,14 @@
68287713
68297714 mutex_lock(&trace_types_lock);
68307715
6831
- iter->tr->current_trace->ref--;
7716
+ iter->tr->trace_ref--;
68327717
68337718 __trace_array_put(iter->tr);
68347719
68357720 if (info->spare)
6836
- ring_buffer_free_read_page(iter->trace_buffer->buffer,
7721
+ ring_buffer_free_read_page(iter->array_buffer->buffer,
68377722 info->spare_cpu, info->spare);
6838
- kfree(info);
7723
+ kvfree(info);
68397724
68407725 mutex_unlock(&trace_types_lock);
68417726
....@@ -6843,7 +7728,7 @@
68437728 }
68447729
68457730 struct buffer_ref {
6846
- struct ring_buffer *buffer;
7731
+ struct trace_buffer *buffer;
68477732 void *page;
68487733 int cpu;
68497734 refcount_t refcount;
....@@ -6880,10 +7765,7 @@
68807765
68817766 /* Pipe buffer operations for a buffer. */
68827767 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
6883
- .can_merge = 0,
6884
- .confirm = generic_pipe_buf_confirm,
68857768 .release = buffer_pipe_buf_release,
6886
- .steal = generic_pipe_buf_nosteal,
68877769 .get = buffer_pipe_buf_get,
68887770 };
68897771
....@@ -6939,7 +7821,7 @@
69397821
69407822 again:
69417823 trace_access_lock(iter->cpu_file);
6942
- entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7824
+ entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
69437825
69447826 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
69457827 struct page *page;
....@@ -6952,7 +7834,7 @@
69527834 }
69537835
69547836 refcount_set(&ref->refcount, 1);
6955
- ref->buffer = iter->trace_buffer->buffer;
7837
+ ref->buffer = iter->array_buffer->buffer;
69567838 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
69577839 if (IS_ERR(ref->page)) {
69587840 ret = PTR_ERR(ref->page);
....@@ -6980,7 +7862,7 @@
69807862 spd.nr_pages++;
69817863 *ppos += PAGE_SIZE;
69827864
6983
- entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
7865
+ entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
69847866 }
69857867
69867868 trace_access_unlock(iter->cpu_file);
....@@ -6995,7 +7877,7 @@
69957877 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
69967878 goto out;
69977879
6998
- ret = wait_on_pipe(iter, true);
7880
+ ret = wait_on_pipe(iter, iter->tr->buffer_percent);
69997881 if (ret)
70007882 goto out;
70017883
....@@ -7024,7 +7906,7 @@
70247906 {
70257907 struct inode *inode = file_inode(filp);
70267908 struct trace_array *tr = inode->i_private;
7027
- struct trace_buffer *trace_buf = &tr->trace_buffer;
7909
+ struct array_buffer *trace_buf = &tr->array_buffer;
70287910 int cpu = tracing_get_cpu(inode);
70297911 struct trace_seq *s;
70307912 unsigned long cnt;
....@@ -7095,14 +7977,23 @@
70957977 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
70967978 size_t cnt, loff_t *ppos)
70977979 {
7098
- unsigned long *p = filp->private_data;
7099
- char buf[64]; /* Not too big for a shallow stack */
7980
+ ssize_t ret;
7981
+ char *buf;
71007982 int r;
71017983
7102
- r = scnprintf(buf, 63, "%ld", *p);
7103
- buf[r++] = '\n';
7984
+ /* 256 should be plenty to hold the amount needed */
7985
+ buf = kmalloc(256, GFP_KERNEL);
7986
+ if (!buf)
7987
+ return -ENOMEM;
71047988
7105
- return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7989
+ r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
7990
+ ftrace_update_tot_cnt,
7991
+ ftrace_number_of_pages,
7992
+ ftrace_number_of_groups);
7993
+
7994
+ ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7995
+ kfree(buf);
7996
+ return ret;
71067997 }
71077998
71087999 static const struct file_operations tracing_dyn_info_fops = {
....@@ -7296,7 +8187,7 @@
72968187
72978188 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
72988189
7299
- WARN_ONCE(!tr->percpu_dir,
8190
+ MEM_FAIL(!tr->percpu_dir,
73008191 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
73018192
73028193 return tr->percpu_dir;
....@@ -7405,12 +8296,33 @@
74058296 return cnt;
74068297 }
74078298
8299
+static int tracing_open_options(struct inode *inode, struct file *filp)
8300
+{
8301
+ struct trace_option_dentry *topt = inode->i_private;
8302
+ int ret;
8303
+
8304
+ ret = tracing_check_open_get_tr(topt->tr);
8305
+ if (ret)
8306
+ return ret;
8307
+
8308
+ filp->private_data = inode->i_private;
8309
+ return 0;
8310
+}
8311
+
8312
+static int tracing_release_options(struct inode *inode, struct file *file)
8313
+{
8314
+ struct trace_option_dentry *topt = file->private_data;
8315
+
8316
+ trace_array_put(topt->tr);
8317
+ return 0;
8318
+}
74088319
74098320 static const struct file_operations trace_options_fops = {
7410
- .open = tracing_open_generic,
8321
+ .open = tracing_open_options,
74118322 .read = trace_options_read,
74128323 .write = trace_options_write,
74138324 .llseek = generic_file_llseek,
8325
+ .release = tracing_release_options,
74148326 };
74158327
74168328 /*
....@@ -7617,7 +8529,7 @@
76178529 for (cnt = 0; opts[cnt].name; cnt++) {
76188530 create_trace_option_file(tr, &topts[cnt], flags,
76198531 &opts[cnt]);
7620
- WARN_ONCE(topts[cnt].entry == NULL,
8532
+ MEM_FAIL(topts[cnt].entry == NULL,
76218533 "Failed to create trace option: %s",
76228534 opts[cnt].name);
76238535 }
....@@ -7674,7 +8586,7 @@
76748586 size_t cnt, loff_t *ppos)
76758587 {
76768588 struct trace_array *tr = filp->private_data;
7677
- struct ring_buffer *buffer = tr->trace_buffer.buffer;
8589
+ struct trace_buffer *buffer = tr->array_buffer.buffer;
76788590 unsigned long val;
76798591 int ret;
76808592
....@@ -7711,13 +8623,57 @@
77118623 .llseek = default_llseek,
77128624 };
77138625
7714
-struct dentry *trace_instance_dir;
8626
+static ssize_t
8627
+buffer_percent_read(struct file *filp, char __user *ubuf,
8628
+ size_t cnt, loff_t *ppos)
8629
+{
8630
+ struct trace_array *tr = filp->private_data;
8631
+ char buf[64];
8632
+ int r;
8633
+
8634
+ r = tr->buffer_percent;
8635
+ r = sprintf(buf, "%d\n", r);
8636
+
8637
+ return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8638
+}
8639
+
8640
+static ssize_t
8641
+buffer_percent_write(struct file *filp, const char __user *ubuf,
8642
+ size_t cnt, loff_t *ppos)
8643
+{
8644
+ struct trace_array *tr = filp->private_data;
8645
+ unsigned long val;
8646
+ int ret;
8647
+
8648
+ ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8649
+ if (ret)
8650
+ return ret;
8651
+
8652
+ if (val > 100)
8653
+ return -EINVAL;
8654
+
8655
+ tr->buffer_percent = val;
8656
+
8657
+ (*ppos)++;
8658
+
8659
+ return cnt;
8660
+}
8661
+
8662
+static const struct file_operations buffer_percent_fops = {
8663
+ .open = tracing_open_generic_tr,
8664
+ .read = buffer_percent_read,
8665
+ .write = buffer_percent_write,
8666
+ .release = tracing_release_generic_tr,
8667
+ .llseek = default_llseek,
8668
+};
8669
+
8670
+static struct dentry *trace_instance_dir;
77158671
77168672 static void
77178673 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
77188674
77198675 static int
7720
-allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
8676
+allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
77218677 {
77228678 enum ring_buffer_flags rb_flags;
77238679
....@@ -7737,8 +8693,8 @@
77378693 }
77388694
77398695 /* Allocate the first page for all buffers */
7740
- set_buffer_entries(&tr->trace_buffer,
7741
- ring_buffer_size(tr->trace_buffer.buffer, 0));
8696
+ set_buffer_entries(&tr->array_buffer,
8697
+ ring_buffer_size(tr->array_buffer.buffer, 0));
77428698
77438699 return 0;
77448700 }
....@@ -7747,18 +8703,18 @@
77478703 {
77488704 int ret;
77498705
7750
- ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
8706
+ ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
77518707 if (ret)
77528708 return ret;
77538709
77548710 #ifdef CONFIG_TRACER_MAX_TRACE
77558711 ret = allocate_trace_buffer(tr, &tr->max_buffer,
77568712 allocate_snapshot ? size : 1);
7757
- if (WARN_ON(ret)) {
7758
- ring_buffer_free(tr->trace_buffer.buffer);
7759
- tr->trace_buffer.buffer = NULL;
7760
- free_percpu(tr->trace_buffer.data);
7761
- tr->trace_buffer.data = NULL;
8713
+ if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
8714
+ ring_buffer_free(tr->array_buffer.buffer);
8715
+ tr->array_buffer.buffer = NULL;
8716
+ free_percpu(tr->array_buffer.data);
8717
+ tr->array_buffer.data = NULL;
77628718 return -ENOMEM;
77638719 }
77648720 tr->allocated_snapshot = allocate_snapshot;
....@@ -7770,22 +8726,10 @@
77708726 allocate_snapshot = false;
77718727 #endif
77728728
7773
- /*
7774
- * Because of some magic with the way alloc_percpu() works on
7775
- * x86_64, we need to synchronize the pgd of all the tables,
7776
- * otherwise the trace events that happen in x86_64 page fault
7777
- * handlers can't cope with accessing the chance that a
7778
- * alloc_percpu()'d memory might be touched in the page fault trace
7779
- * event. Oh, and we need to audit all other alloc_percpu() and vmalloc()
7780
- * calls in tracing, because something might get triggered within a
7781
- * page fault trace event!
7782
- */
7783
- vmalloc_sync_mappings();
7784
-
77858729 return 0;
77868730 }
77878731
7788
-static void free_trace_buffer(struct trace_buffer *buf)
8732
+static void free_trace_buffer(struct array_buffer *buf)
77898733 {
77908734 if (buf->buffer) {
77918735 ring_buffer_free(buf->buffer);
....@@ -7800,7 +8744,7 @@
78008744 if (!tr)
78018745 return;
78028746
7803
- free_trace_buffer(&tr->trace_buffer);
8747
+ free_trace_buffer(&tr->array_buffer);
78048748
78058749 #ifdef CONFIG_TRACER_MAX_TRACE
78068750 free_trace_buffer(&tr->max_buffer);
....@@ -7827,28 +8771,68 @@
78278771 static void update_tracer_options(struct trace_array *tr)
78288772 {
78298773 mutex_lock(&trace_types_lock);
8774
+ tracer_options_updated = true;
78308775 __update_tracer_options(tr);
78318776 mutex_unlock(&trace_types_lock);
78328777 }
78338778
7834
-static int instance_mkdir(const char *name)
8779
+/* Must have trace_types_lock held */
8780
+struct trace_array *trace_array_find(const char *instance)
8781
+{
8782
+ struct trace_array *tr, *found = NULL;
8783
+
8784
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8785
+ if (tr->name && strcmp(tr->name, instance) == 0) {
8786
+ found = tr;
8787
+ break;
8788
+ }
8789
+ }
8790
+
8791
+ return found;
8792
+}
8793
+
8794
+struct trace_array *trace_array_find_get(const char *instance)
8795
+{
8796
+ struct trace_array *tr;
8797
+
8798
+ mutex_lock(&trace_types_lock);
8799
+ tr = trace_array_find(instance);
8800
+ if (tr)
8801
+ tr->ref++;
8802
+ mutex_unlock(&trace_types_lock);
8803
+
8804
+ return tr;
8805
+}
8806
+
8807
+static int trace_array_create_dir(struct trace_array *tr)
8808
+{
8809
+ int ret;
8810
+
8811
+ tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
8812
+ if (!tr->dir)
8813
+ return -EINVAL;
8814
+
8815
+ ret = event_trace_add_tracer(tr->dir, tr);
8816
+ if (ret) {
8817
+ tracefs_remove(tr->dir);
8818
+ return ret;
8819
+ }
8820
+
8821
+ init_tracer_tracefs(tr, tr->dir);
8822
+ __update_tracer_options(tr);
8823
+
8824
+ return ret;
8825
+}
8826
+
8827
+static struct trace_array *trace_array_create(const char *name)
78358828 {
78368829 struct trace_array *tr;
78378830 int ret;
78388831
7839
- mutex_lock(&event_mutex);
7840
- mutex_lock(&trace_types_lock);
7841
-
7842
- ret = -EEXIST;
7843
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7844
- if (tr->name && strcmp(tr->name, name) == 0)
7845
- goto out_unlock;
7846
- }
7847
-
78488832 ret = -ENOMEM;
78498833 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
78508834 if (!tr)
7851
- goto out_unlock;
8835
+ return ERR_PTR(ret);
78528836
78538837 tr->name = kstrdup(name, GFP_KERNEL);
78548838 if (!tr->name)
....@@ -7870,70 +8854,112 @@
78708854 INIT_LIST_HEAD(&tr->systems);
78718855 INIT_LIST_HEAD(&tr->events);
78728856 INIT_LIST_HEAD(&tr->hist_vars);
8857
+ INIT_LIST_HEAD(&tr->err_log);
78738858
78748859 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
78758860 goto out_free_tr;
78768861
7877
- tr->dir = tracefs_create_dir(name, trace_instance_dir);
7878
- if (!tr->dir)
8862
+ if (ftrace_allocate_ftrace_ops(tr) < 0)
78798863 goto out_free_tr;
7880
-
7881
- ret = event_trace_add_tracer(tr->dir, tr);
7882
- if (ret) {
7883
- tracefs_remove_recursive(tr->dir);
7884
- goto out_free_tr;
7885
- }
78868864
78878865 ftrace_init_trace_array(tr);
78888866
7889
- init_tracer_tracefs(tr, tr->dir);
78908867 init_trace_flags_index(tr);
7891
- __update_tracer_options(tr);
8868
+
8869
+ if (trace_instance_dir) {
8870
+ ret = trace_array_create_dir(tr);
8871
+ if (ret)
8872
+ goto out_free_tr;
8873
+ } else
8874
+ __trace_early_add_events(tr);
78928875
78938876 list_add(&tr->list, &ftrace_trace_arrays);
78948877
7895
- mutex_unlock(&trace_types_lock);
7896
- mutex_unlock(&event_mutex);
8878
+ tr->ref++;
78978879
7898
- return 0;
8880
+ return tr;
78998881
79008882 out_free_tr:
8883
+ ftrace_free_ftrace_ops(tr);
79018884 free_trace_buffers(tr);
79028885 free_cpumask_var(tr->tracing_cpumask);
79038886 kfree(tr->name);
79048887 kfree(tr);
79058888
7906
- out_unlock:
7907
- mutex_unlock(&trace_types_lock);
7908
- mutex_unlock(&event_mutex);
7909
-
7910
- return ret;
7911
-
8889
+ return ERR_PTR(ret);
79128890 }
79138891
7914
-static int instance_rmdir(const char *name)
8892
+static int instance_mkdir(const char *name)
79158893 {
79168894 struct trace_array *tr;
7917
- int found = 0;
79188895 int ret;
7919
- int i;
79208896
79218897 mutex_lock(&event_mutex);
79228898 mutex_lock(&trace_types_lock);
79238899
7924
- ret = -ENODEV;
7925
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7926
- if (tr->name && strcmp(tr->name, name) == 0) {
7927
- found = 1;
7928
- break;
7929
- }
7930
- }
7931
- if (!found)
8900
+ ret = -EEXIST;
8901
+ if (trace_array_find(name))
79328902 goto out_unlock;
79338903
7934
- ret = -EBUSY;
7935
- if (tr->ref || (tr->current_trace && tr->current_trace->ref))
7936
- goto out_unlock;
8904
+ tr = trace_array_create(name);
8905
+
8906
+ ret = PTR_ERR_OR_ZERO(tr);
8907
+
8908
+out_unlock:
8909
+ mutex_unlock(&trace_types_lock);
8910
+ mutex_unlock(&event_mutex);
8911
+ return ret;
8912
+}
8913
+
8914
+/**
8915
+ * trace_array_get_by_name - Create/Lookup a trace array, given its name.
8916
+ * @name: The name of the trace array to be looked up/created.
8917
+ *
8918
+ * Returns pointer to trace array with given name.
8919
+ * NULL, if it cannot be created.
8920
+ *
8921
+ * NOTE: This function increments the reference counter associated with the
8922
+ * trace array returned. This makes sure it cannot be freed while in use.
8923
+ * Use trace_array_put() once the trace array is no longer needed.
8924
+ * If the trace_array is to be freed, trace_array_destroy() needs to
8925
+ * be called after the trace_array_put(), or simply let user space delete
8926
+ * it from the tracefs instances directory. But until the
8927
+ * trace_array_put() is called, user space can not delete it.
8928
+ *
8929
+ */
8930
+struct trace_array *trace_array_get_by_name(const char *name)
8931
+{
8932
+ struct trace_array *tr;
8933
+
8934
+ mutex_lock(&event_mutex);
8935
+ mutex_lock(&trace_types_lock);
8936
+
8937
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
8938
+ if (tr->name && strcmp(tr->name, name) == 0)
8939
+ goto out_unlock;
8940
+ }
8941
+
8942
+ tr = trace_array_create(name);
8943
+
8944
+ if (IS_ERR(tr))
8945
+ tr = NULL;
8946
+out_unlock:
8947
+ if (tr)
8948
+ tr->ref++;
8949
+
8950
+ mutex_unlock(&trace_types_lock);
8951
+ mutex_unlock(&event_mutex);
8952
+ return tr;
8953
+}
8954
+EXPORT_SYMBOL_GPL(trace_array_get_by_name);
8955
+
8956
+static int __remove_instance(struct trace_array *tr)
8957
+{
8958
+ int i;
8959
+
8960
+ /* Reference counter for a newly created trace array = 1. */
8961
+ if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
8962
+ return -EBUSY;
79378963
79388964 list_del(&tr->list);
79398965
....@@ -7948,8 +8974,9 @@
79488974 event_trace_del_tracer(tr);
79498975 ftrace_clear_pids(tr);
79508976 ftrace_destroy_function_files(tr);
7951
- tracefs_remove_recursive(tr->dir);
8977
+ tracefs_remove(tr->dir);
79528978 free_trace_buffers(tr);
8979
+ clear_tracing_err_log(tr);
79538980
79548981 for (i = 0; i < tr->nr_topts; i++) {
79558982 kfree(tr->topts[i].topts);
....@@ -7960,9 +8987,50 @@
79608987 kfree(tr->name);
79618988 kfree(tr);
79628989
7963
- ret = 0;
8990
+ return 0;
8991
+}
79648992
7965
- out_unlock:
8993
+int trace_array_destroy(struct trace_array *this_tr)
8994
+{
8995
+ struct trace_array *tr;
8996
+ int ret;
8997
+
8998
+ if (!this_tr)
8999
+ return -EINVAL;
9000
+
9001
+ mutex_lock(&event_mutex);
9002
+ mutex_lock(&trace_types_lock);
9003
+
9004
+ ret = -ENODEV;
9005
+
9006
+ /* Making sure trace array exists before destroying it. */
9007
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9008
+ if (tr == this_tr) {
9009
+ ret = __remove_instance(tr);
9010
+ break;
9011
+ }
9012
+ }
9013
+
9014
+ mutex_unlock(&trace_types_lock);
9015
+ mutex_unlock(&event_mutex);
9016
+
9017
+ return ret;
9018
+}
9019
+EXPORT_SYMBOL_GPL(trace_array_destroy);
9020
+
9021
+static int instance_rmdir(const char *name)
9022
+{
9023
+ struct trace_array *tr;
9024
+ int ret;
9025
+
9026
+ mutex_lock(&event_mutex);
9027
+ mutex_lock(&trace_types_lock);
9028
+
9029
+ ret = -ENODEV;
9030
+ tr = trace_array_find(name);
9031
+ if (tr)
9032
+ ret = __remove_instance(tr);
9033
+
79669034 mutex_unlock(&trace_types_lock);
79679035 mutex_unlock(&event_mutex);
79689036
....@@ -7971,11 +9039,27 @@
79719039
79729040 static __init void create_trace_instances(struct dentry *d_tracer)
79739041 {
9042
+ struct trace_array *tr;
9043
+
79749044 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
79759045 instance_mkdir,
79769046 instance_rmdir);
7977
- if (WARN_ON(!trace_instance_dir))
9047
+ if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
79789048 return;
9049
+
9050
+ mutex_lock(&event_mutex);
9051
+ mutex_lock(&trace_types_lock);
9052
+
9053
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9054
+ if (!tr->name)
9055
+ continue;
9056
+ if (MEM_FAIL(trace_array_create_dir(tr) < 0,
9057
+ "Failed to create instance directory\n"))
9058
+ break;
9059
+ }
9060
+
9061
+ mutex_unlock(&trace_types_lock);
9062
+ mutex_unlock(&event_mutex);
79799063 }
79809064
79819065 static void
....@@ -8032,20 +9116,27 @@
80329116 trace_create_file("timestamp_mode", 0444, d_tracer, tr,
80339117 &trace_time_stamp_mode_fops);
80349118
9119
+ tr->buffer_percent = 50;
9120
+
9121
+ trace_create_file("buffer_percent", 0444, d_tracer,
9122
+ tr, &buffer_percent_fops);
9123
+
80359124 create_trace_options_dir(tr);
80369125
80379126 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
8038
- trace_create_file("tracing_max_latency", 0644, d_tracer,
8039
- &tr->max_latency, &tracing_max_lat_fops);
9127
+ trace_create_maxlat_file(tr, d_tracer);
80409128 #endif
80419129
80429130 if (ftrace_create_function_files(tr, d_tracer))
8043
- WARN(1, "Could not allocate function filter files");
9131
+ MEM_FAIL(1, "Could not allocate function filter files");
80449132
80459133 #ifdef CONFIG_TRACER_SNAPSHOT
80469134 trace_create_file("snapshot", 0644, d_tracer,
80479135 tr, &snapshot_fops);
80489136 #endif
9137
+
9138
+ trace_create_file("error_log", 0644, d_tracer,
9139
+ tr, &tracing_err_log_fops);
80499140
80509141 for_each_tracing_cpu(cpu)
80519142 tracing_init_tracefs_percpu(tr, cpu);
....@@ -8053,6 +9144,7 @@
80539144 ftrace_init_tracefs(tr, d_tracer);
80549145 }
80559146
9147
+#ifndef CONFIG_TRACEFS_DISABLE_AUTOMOUNT
80569148 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
80579149 {
80589150 struct vfsmount *mnt;
....@@ -8074,6 +9166,7 @@
80749166
80759167 return mnt;
80769168 }
9169
+#endif
80779170
80789171 /**
80799172 * tracing_init_dentry - initialize top level trace array
....@@ -8082,19 +9175,23 @@
80829175 * directory. It is called via fs_initcall() by any of the boot up code
80839176 * and expects to return the dentry of the top level tracing directory.
80849177 */
8085
-struct dentry *tracing_init_dentry(void)
9178
+int tracing_init_dentry(void)
80869179 {
80879180 struct trace_array *tr = &global_trace;
80889181
9182
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9183
+ pr_warn("Tracing disabled due to lockdown\n");
9184
+ return -EPERM;
9185
+ }
9186
+
80899187 /* The top level trace array uses NULL as parent */
80909188 if (tr->dir)
8091
- return NULL;
9189
+ return 0;
80929190
8093
- if (WARN_ON(!tracefs_initialized()) ||
8094
- (IS_ENABLED(CONFIG_DEBUG_FS) &&
8095
- WARN_ON(!debugfs_initialized())))
8096
- return ERR_PTR(-ENODEV);
9191
+ if (WARN_ON(!tracefs_initialized()))
9192
+ return -ENODEV;
80979193
9194
+#ifndef CONFIG_TRACEFS_DISABLE_AUTOMOUNT
80989195 /*
80999196 * As there may still be users that expect the tracing
81009197 * files to exist in debugfs/tracing, we must automount
....@@ -8103,12 +9200,11 @@
81039200 */
81049201 tr->dir = debugfs_create_automount("tracing", NULL,
81059202 trace_automount, NULL);
8106
- if (!tr->dir) {
8107
- pr_warn_once("Could not create debugfs directory 'tracing'\n");
8108
- return ERR_PTR(-ENOMEM);
8109
- }
9203
+#else
9204
+ tr->dir = ERR_PTR(-ENODEV);
9205
+#endif
81109206
8111
- return NULL;
9207
+ return 0;
81129208 }
81139209
81149210 extern struct trace_eval_map *__start_ftrace_eval_maps[];
....@@ -8184,7 +9280,7 @@
81849280 break;
81859281 }
81869282
8187
- return 0;
9283
+ return NOTIFY_OK;
81889284 }
81899285
81909286 static struct notifier_block trace_module_nb = {
....@@ -8195,48 +9291,48 @@
81959291
81969292 static __init int tracer_init_tracefs(void)
81979293 {
8198
- struct dentry *d_tracer;
9294
+ int ret;
81999295
82009296 trace_access_lock_init();
82019297
8202
- d_tracer = tracing_init_dentry();
8203
- if (IS_ERR(d_tracer))
9298
+ ret = tracing_init_dentry();
9299
+ if (ret)
82049300 return 0;
82059301
82069302 event_trace_init();
82079303
8208
- init_tracer_tracefs(&global_trace, d_tracer);
8209
- ftrace_init_tracefs_toplevel(&global_trace, d_tracer);
9304
+ init_tracer_tracefs(&global_trace, NULL);
9305
+ ftrace_init_tracefs_toplevel(&global_trace, NULL);
82109306
8211
- trace_create_file("tracing_thresh", 0644, d_tracer,
9307
+ trace_create_file("tracing_thresh", 0644, NULL,
82129308 &global_trace, &tracing_thresh_fops);
82139309
8214
- trace_create_file("README", 0444, d_tracer,
9310
+ trace_create_file("README", 0444, NULL,
82159311 NULL, &tracing_readme_fops);
82169312
8217
- trace_create_file("saved_cmdlines", 0444, d_tracer,
9313
+ trace_create_file("saved_cmdlines", 0444, NULL,
82189314 NULL, &tracing_saved_cmdlines_fops);
82199315
8220
- trace_create_file("saved_cmdlines_size", 0644, d_tracer,
9316
+ trace_create_file("saved_cmdlines_size", 0644, NULL,
82219317 NULL, &tracing_saved_cmdlines_size_fops);
82229318
8223
- trace_create_file("saved_tgids", 0444, d_tracer,
9319
+ trace_create_file("saved_tgids", 0444, NULL,
82249320 NULL, &tracing_saved_tgids_fops);
82259321
82269322 trace_eval_init();
82279323
8228
- trace_create_eval_file(d_tracer);
9324
+ trace_create_eval_file(NULL);
82299325
82309326 #ifdef CONFIG_MODULES
82319327 register_module_notifier(&trace_module_nb);
82329328 #endif
82339329
82349330 #ifdef CONFIG_DYNAMIC_FTRACE
8235
- trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
8236
- &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
9331
+ trace_create_file("dyn_ftrace_total_info", 0444, NULL,
9332
+ NULL, &tracing_dyn_info_fops);
82379333 #endif
82389334
8239
- create_trace_instances(d_tracer);
9335
+ create_trace_instances(NULL);
82409336
82419337 update_tracer_options(&global_trace);
82429338
....@@ -8246,8 +9342,17 @@
82469342 static int trace_panic_handler(struct notifier_block *this,
82479343 unsigned long event, void *unused)
82489344 {
9345
+ bool ftrace_check = false;
9346
+
9347
+ trace_android_vh_ftrace_oops_enter(&ftrace_check);
9348
+
9349
+ if (ftrace_check)
9350
+ return NOTIFY_OK;
9351
+
82499352 if (ftrace_dump_on_oops)
82509353 ftrace_dump(ftrace_dump_on_oops);
9354
+
9355
+ trace_android_vh_ftrace_oops_exit(&ftrace_check);
82519356 return NOTIFY_OK;
82529357 }
82539358
....@@ -8261,6 +9366,13 @@
82619366 unsigned long val,
82629367 void *data)
82639368 {
9369
+ bool ftrace_check = false;
9370
+
9371
+ trace_android_vh_ftrace_oops_enter(&ftrace_check);
9372
+
9373
+ if (ftrace_check)
9374
+ return NOTIFY_OK;
9375
+
82649376 switch (val) {
82659377 case DIE_OOPS:
82669378 if (ftrace_dump_on_oops)
....@@ -8269,6 +9381,8 @@
82699381 default:
82709382 break;
82719383 }
9384
+
9385
+ trace_android_vh_ftrace_oops_exit(&ftrace_check);
82729386 return NOTIFY_OK;
82739387 }
82749388
....@@ -8293,6 +9407,8 @@
82939407 void
82949408 trace_printk_seq(struct trace_seq *s)
82959409 {
9410
+ bool dump_printk = true;
9411
+
82969412 /* Probably should print a warning here. */
82979413 if (s->seq.len >= TRACE_MAX_PRINT)
82989414 s->seq.len = TRACE_MAX_PRINT;
....@@ -8308,7 +9424,9 @@
83089424 /* should be zero ended, but we are paranoid. */
83099425 s->buffer[s->seq.len] = 0;
83109426
8311
- printk(KERN_TRACE "%s", s->buffer);
9427
+ trace_android_vh_ftrace_dump_buffer(s, &dump_printk);
9428
+ if (dump_printk)
9429
+ printk(KERN_TRACE "%s", s->buffer);
83129430
83139431 trace_seq_init(s);
83149432 }
....@@ -8318,13 +9436,13 @@
83189436 iter->tr = &global_trace;
83199437 iter->trace = iter->tr->current_trace;
83209438 iter->cpu_file = RING_BUFFER_ALL_CPUS;
8321
- iter->trace_buffer = &global_trace.trace_buffer;
9439
+ iter->array_buffer = &global_trace.array_buffer;
83229440
83239441 if (iter->trace && iter->trace->open)
83249442 iter->trace->open(iter);
83259443
83269444 /* Annotate start of buffers if we had overruns */
8327
- if (ring_buffer_overruns(iter->trace_buffer->buffer))
9445
+ if (ring_buffer_overruns(iter->array_buffer->buffer))
83289446 iter->iter_flags |= TRACE_FILE_ANNOTATE;
83299447
83309448 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
....@@ -8341,6 +9459,8 @@
83419459 unsigned int old_userobj;
83429460 unsigned long flags;
83439461 int cnt = 0, cpu;
9462
+ bool ftrace_check = false;
9463
+ unsigned long size;
83449464
83459465 /* Only allow one dump user at a time. */
83469466 if (atomic_inc_return(&dump_running) != 1) {
....@@ -8363,15 +9483,23 @@
83639483
83649484 /* Simulate the iterator */
83659485 trace_init_global_iter(&iter);
9486
+ /* Can not use kmalloc for iter.temp */
9487
+ iter.temp = static_temp_buf;
9488
+ iter.temp_size = STATIC_TEMP_BUF_SIZE;
83669489
83679490 for_each_tracing_cpu(cpu) {
8368
- atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
9491
+ atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
9492
+ size = ring_buffer_size(iter.array_buffer->buffer, cpu);
9493
+ trace_android_vh_ftrace_size_check(size, &ftrace_check);
83699494 }
83709495
83719496 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
83729497
83739498 /* don't look at user memory in panic mode */
83749499 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
9500
+
9501
+ if (ftrace_check)
9502
+ goto out_enable;
83759503
83769504 switch (oops_dump_mode) {
83779505 case DUMP_ALL:
....@@ -8396,13 +9524,14 @@
83969524 }
83979525
83989526 /*
8399
- * We need to stop all tracing on all CPUS to read the
9527
+ * We need to stop all tracing on all CPUS to read
84009528 * the next buffer. This is a bit expensive, but is
84019529 * not done often. We fill all what we can read,
84029530 * and then release the locks again.
84039531 */
84049532
84059533 while (!trace_empty(&iter)) {
9534
+ ftrace_check = true;
84069535
84079536 if (!cnt)
84089537 printk(KERN_TRACE "---------------------------------\n");
....@@ -8410,7 +9539,9 @@
84109539 cnt++;
84119540
84129541 trace_iterator_reset(&iter);
8413
- iter.iter_flags |= TRACE_FILE_LAT_FMT;
9542
+ trace_android_vh_ftrace_format_check(&ftrace_check);
9543
+ if (ftrace_check)
9544
+ iter.iter_flags |= TRACE_FILE_LAT_FMT;
84149545
84159546 if (trace_find_next_entry_inc(&iter) != NULL) {
84169547 int ret;
....@@ -8433,7 +9564,7 @@
84339564 tr->trace_flags |= old_userobj;
84349565
84359566 for_each_tracing_cpu(cpu) {
8436
- atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
9567
+ atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
84379568 }
84389569 atomic_dec(&dump_running);
84399570 printk_nmi_direct_exit();
....@@ -8532,8 +9663,14 @@
85329663 int ring_buf_size;
85339664 int ret = -ENOMEM;
85349665
9666
+
9667
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9668
+ pr_warn("Tracing disabled due to lockdown\n");
9669
+ return -EPERM;
9670
+ }
9671
+
85359672 /*
8536
- * Make sure we don't accidently add more trace options
9673
+ * Make sure we don't accidentally add more trace options
85379674 * than we have bits for.
85389675 */
85399676 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
....@@ -8562,7 +9699,7 @@
85629699
85639700 /*
85649701 * The prepare callbacks allocates some memory for the ring buffer. We
8565
- * don't free the buffer if the if the CPU goes down. If we were to free
9702
+ * don't free the buffer if the CPU goes down. If we were to free
85669703 * the buffer, then the user would lose any trace that was in the
85679704 * buffer. The memory will be removed once the "instance" is removed.
85689705 */
....@@ -8582,8 +9719,7 @@
85829719
85839720 /* TODO: make the number of buffers hot pluggable with CPUS */
85849721 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
8585
- printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
8586
- WARN_ON(1);
9722
+ MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
85879723 goto out_free_savedcmd;
85889724 }
85899725
....@@ -8628,6 +9764,7 @@
86289764 INIT_LIST_HEAD(&global_trace.systems);
86299765 INIT_LIST_HEAD(&global_trace.events);
86309766 INIT_LIST_HEAD(&global_trace.hist_vars);
9767
+ INIT_LIST_HEAD(&global_trace.err_log);
86319768 list_add(&global_trace.list, &ftrace_trace_arrays);
86329769
86339770 apply_trace_boot_options();
....@@ -8655,12 +9792,15 @@
86559792 if (tracepoint_printk) {
86569793 tracepoint_print_iter =
86579794 kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
8658
- if (WARN_ON(!tracepoint_print_iter))
9795
+ if (MEM_FAIL(!tracepoint_print_iter,
9796
+ "Failed to allocate trace iterator\n"))
86599797 tracepoint_printk = 0;
86609798 else
86619799 static_key_enable(&tracepoint_printk_key.key);
86629800 }
86639801 tracer_alloc_buffers();
9802
+
9803
+ init_events();
86649804 }
86659805
86669806 void __init trace_init(void)
....@@ -8695,6 +9835,11 @@
86959835 {
86969836 /* sched_clock_stable() is determined in late_initcall */
86979837 if (!trace_boot_clock && !sched_clock_stable()) {
9838
+ if (security_locked_down(LOCKDOWN_TRACEFS)) {
9839
+ pr_warn("Can not set tracing clock due to lockdown\n");
9840
+ return -EPERM;
9841
+ }
9842
+
86989843 printk(KERN_WARNING
86999844 "Unstable clock detected, switching default tracing clock to \"global\"\n"
87009845 "If you want to keep using the local clock, then add:\n"