tracing: Updates for 5.13
Steven Rostedt
maintainercover tracing: Updates for 5.13
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
for-next
Head SHA1: 9a6944fee68e25084130386c608c5ac8db487581
Cao jin (1):
bootconfig: Update prototype of setup_boot_config()
Colin Ian King (1):
ftrace: Fix spelling mistake "disabed" -> "disabled"
Steven Rostedt (VMware) (10):
ring-buffer: Separate out internal use of ring_buffer_event_time_stamp()
ring-buffer: Add a event_stamp to cpu_buffer for each level of nesting
tracing: Pass buffer of event to trigger operations
ring-buffer: Allow ring_buffer_event_time_stamp() to return time stamp of all events
tracing: Use a no_filter_buffering_ref to stop using the filter buffer
ring-buffer: Add verifier for using ring_buffer_event_time_stamp()
tracing: Add tracing_event_time_stamp() API
tracing: Add check of trace event print fmts for dereferencing pointers
seq_buf: Add seq_buf_terminate() API
tracing: Add a verifier to check string pointers for trace events
Xu Wang (1):
tools/latency-collector: Remove unneeded semicolon
----
arch/csky/kernel/probes/ftrace.c | 2 +- arch/riscv/kernel/probes/ftrace.c | 2 +- arch/x86/kernel/kprobes/ftrace.c | 2 +- include/linux/ring_buffer.h | 3 +- include/linux/seq_buf.h | 25 ++++ include/linux/trace_events.h | 5 +- init/main.c | 6 +- kernel/trace/ring_buffer.c | 138 ++++++++++++++--- kernel/trace/trace.c | 238 +++++++++++++++++++++++++++--- kernel/trace/trace.h | 12 +- kernel/trace/trace_events.c | 210 ++++++++++++++++++++++++++ kernel/trace/trace_events_hist.c | 100 ++++++++----- kernel/trace/trace_events_trigger.c | 45 +++--- kernel/trace/trace_output.c | 2 +- kernel/trace/trace_printk.c | 11 ++ tools/tracing/latency/latency-collector.c | 4 +- 16 files changed, 697 insertions(+), 108 deletions(-)
Steven Rostedt
maintainerPATCH 1/13 ring-buffer: Separate out internal use of ring_buffer_event_time_stamp()
From: "Steven Rostedt (VMware)" <[email protected]>
The exported use of ring_buffer_event_time_stamp() is going to become different than how it is used internally. Move the internal logic out into a static function called rb_event_time_stamp(), and have the internal callers call that instead.
--- kernel/trace/ring_buffer.c | 41 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-)
kernel/trace/ring_buffer.c +23 -18
@@ -287,6 +287,17 @@ EXPORT_SYMBOL_GPL(ring_buffer_event_data);#define TS_MASK ((1ULL << TS_SHIFT) - 1)#define TS_DELTA_TEST (~TS_MASK) +static u64 rb_event_time_stamp (struct ring_buffer_event *event) +{ +u64 ts; + + ts = event->array[0 ]; + ts <<=TS_SHIFT ; + ts += event->time_delta; + +return ts; +} +/** * ring_buffer_event_time_stamp - return the event's extended timestamp * @event: the event to get the timestamp of
@@ -299,13 +310,7 @@ EXPORT_SYMBOL_GPL(ring_buffer_event_data);*/ u64 ring_buffer_event_time_stamp (struct ring_buffer_event *event) { -u64 ts; - - ts = event->array[0 ]; - ts <<=TS_SHIFT ; - ts += event->time_delta; - -return ts; +return rb_event_time_stamp (event); }/* Flag when events were overwritten */
@@ -2766,7 +2771,7 @@ static u64 rb_time_delta(struct ring_buffer_event *event)return 0 ;case RINGBUF_TYPE_TIME_EXTEND : -return ring_buffer_event_time_stamp (event); +return rb_event_time_stamp (event);case RINGBUF_TYPE_TIME_STAMP :return 0 ;
@@ -3212,13 +3217,13 @@ static void dump_buffer_page(struct buffer_data_page *bpage,switch (event->type_len) {case RINGBUF_TYPE_TIME_EXTEND : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); ts += delta;pr_warn (" [%lld] delta:%lld TIME EXTEND\n" , ts, delta);break ;case RINGBUF_TYPE_TIME_STAMP : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); ts = delta;pr_warn (" [%lld] absolute:%lld TIME STAMP\n" , ts, delta);break ;
@@ -3289,12 +3294,12 @@ static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,switch (event->type_len) {case RINGBUF_TYPE_TIME_EXTEND : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); ts += delta;break ;case RINGBUF_TYPE_TIME_STAMP : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); ts = delta;break ;
@@ -4256,12 +4261,12 @@ rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,return ;case RINGBUF_TYPE_TIME_EXTEND : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); cpu_buffer->read_stamp += delta;return ;case RINGBUF_TYPE_TIME_STAMP : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); cpu_buffer->read_stamp = delta;return ;
@@ -4286,12 +4291,12 @@ rb_update_iter_read_stamp(struct ring_buffer_iter *iter,return ;case RINGBUF_TYPE_TIME_EXTEND : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); iter->read_stamp += delta;return ;case RINGBUF_TYPE_TIME_STAMP : - delta =ring_buffer_event_time_stamp (event); + delta =rb_event_time_stamp (event); iter->read_stamp = delta;return ;
@@ -4544,7 +4549,7 @@ rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,case RINGBUF_TYPE_TIME_STAMP :if (ts) { - *ts =ring_buffer_event_time_stamp (event); + *ts =rb_event_time_stamp (event);ring_buffer_normalize_time_stamp (cpu_buffer->buffer, cpu_buffer->cpu, ts); }
@@ -4635,7 +4640,7 @@ rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)case RINGBUF_TYPE_TIME_STAMP :if (ts) { - *ts =ring_buffer_event_time_stamp (event); + *ts =rb_event_time_stamp (event);ring_buffer_normalize_time_stamp (cpu_buffer->buffer, cpu_buffer->cpu, ts); }
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 2/13 ring-buffer: Add a event_stamp to cpu_buffer for each level of nesting
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add a place to save the current event time stamp for each level of nesting. This will be used to retrieve the time stamp of the current event before it is committed.
--- kernel/trace/ring_buffer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/trace/ring_buffer.c +10 -1
@@ -492,6 +492,8 @@ struct rb_time_struct {#endif typedef struct rb_time_struct rb_time_t ; +#define MAX_NEST 5 +/* * head_page == tail_page && head == tail then buffer is empty. */
@@ -529,6 +531,7 @@ struct ring_buffer_per_cpu {unsigned long read_bytes;rb_time_t write_stamp;rb_time_t before_stamp; +u64 event_stamp[MAX_NEST ];u64 read_stamp;/* ring buffer pages to update, > 0 to add, < 0 to remove */ long nr_pages_to_update;
@@ -2715,6 +2718,10 @@ rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, {unsigned length = info->length;u64 delta = info->delta; +unsigned int nest =local_read (&cpu_buffer->committing) -1 ; + +if (nest <MAX_NEST ) + cpu_buffer->event_stamp[nest] = info->ts;/* * If we need to add a timestamp, then we
@@ -3456,7 +3463,6 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, info->after, ts)) { /* Nothing came after this event between C and E */ info->delta = ts - info->after; - info->ts = ts; } else {/* * Interrupted between C and E:
@@ -3468,6 +3474,7 @@ __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,*/ info->delta =0 ; } + info->ts = ts; info->add_timestamp &= ~RB_ADD_STAMP_FORCE ; }
@@ -5026,6 +5033,8 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)rb_time_set (&cpu_buffer->write_stamp,0 );rb_time_set (&cpu_buffer->before_stamp,0 ); +memset (cpu_buffer->event_stamp,0 ,sizeof (cpu_buffer->event_stamp)); + cpu_buffer->lost_events =0 ; cpu_buffer->last_overrun =0 ;
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 3/13 tracing: Pass buffer of event to trigger operations
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The ring_buffer_event_time_stamp() is going to be updated to extract the time stamp for the event without needing it to be set to have absolute values for all events. But to do so, it needs the buffer that the event is on as the buffer saves information for the event before it is committed to the buffer.
If the trace buffer is disabled, a temporary buffer is used, and there's no access to this buffer from the current histogram triggers, even though it is passed to the trace event code.
Pass the buffer that the event is on all the way down to the histogram triggers.
--- include/linux/trace_events.h | 5 +- kernel/trace/trace.c | 2 +- kernel/trace/trace.h | 4 +- kernel/trace/trace_events_hist.c | 92 +++++++++++++++++++---------- kernel/trace/trace_events_trigger.c | 45 ++++++++------ 5 files changed, 95 insertions(+), 53 deletions(-)
include/linux/trace_events.h +3 -2
@@ -640,7 +640,8 @@ enum event_trigger_type {extern int filter_match_preds (struct event_filter *filter,void *rec);extern enum event_trigger_type -event_triggers_call (struct trace_event_file *file,void *rec, +event_triggers_call (struct trace_event_file *file, +struct trace_buffer *buffer,void *rec,struct ring_buffer_event *event); extern void event_triggers_post_call(struct trace_event_file *file,
@@ -664,7 +665,7 @@ trace_trigger_soft_disabled(struct trace_event_file *file)if (!(eflags &EVENT_FILE_FL_TRIGGER_COND )) {if (eflags &EVENT_FILE_FL_TRIGGER_MODE ) -event_triggers_call (file,NULL ,NULL ); +event_triggers_call (file,NULL ,NULL ,NULL );if (eflags &EVENT_FILE_FL_SOFT_DISABLED )return true;if (eflags &EVENT_FILE_FL_PID_FILTER )
kernel/trace/trace.c +1 -1
@@ -6763,7 +6763,7 @@ tracing_mark_write(struct file *filp, const char __user *ubuf,if (tr->trace_marker_file && !list_empty (&tr->trace_marker_file->triggers)) {/* do not add \n before testing triggers, but add \0 */ entry->buf[cnt] ='\0' ; - tt =event_triggers_call (tr->trace_marker_file, entry, event); + tt =event_triggers_call (tr->trace_marker_file, buffer, entry, event); }if (entry->buf[cnt -1 ] !='\n' ) {
kernel/trace/trace.h +2 -2
@@ -1329,7 +1329,7 @@ __event_trigger_test_discard(struct trace_event_file *file,unsigned long eflags = file->flags;if (eflags &EVENT_FILE_FL_TRIGGER_COND ) - *tt =event_triggers_call (file, entry, event); + *tt =event_triggers_call (file, buffer, entry, event); if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) || (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
@@ -1626,7 +1626,7 @@ extern int register_trigger_hist_enable_disable_cmds(void);*/ struct event_trigger_ops {void (*func)(struct event_trigger_data *data, -void *rec, +struct trace_buffer *buffer,void *rec,struct ring_buffer_event *rbe);int (*init)(struct event_trigger_ops *ops,struct event_trigger_data *data);
kernel/trace/trace_events_hist.c +61 -31
342 lines not on this page · raw message
kernel/trace/trace_events_trigger.c +28 -17
147 lines not on this page · raw message
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 4/13 ring-buffer: Allow ring_buffer_event_time_stamp() to return time stamp of all events
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Currently, ring_buffer_event_time_stamp() only returns an accurate time stamp of the event if it has an absolute extended time stamp attached to it. To make it more robust, use the event_stamp() in case the event does not have an absolute value attached to it.
This will allow ring_buffer_event_time_stamp() to be used in more cases than just histograms, and it will also allow histograms to not require including absolute values all the time.
--- include/linux/ring_buffer.h | 3 +- kernel/trace/ring_buffer.c | 60 ++++++++++++++++++++++++-------- kernel/trace/trace_events_hist.c | 2 +- 3 files changed, 48 insertions(+), 17 deletions(-)
include/linux/ring_buffer.h +2 -1
@@ -61,7 +61,8 @@ enum ring_buffer_type {unsigned ring_buffer_event_length (struct ring_buffer_event *event);void *ring_buffer_event_data (struct ring_buffer_event *event); -u64 ring_buffer_event_time_stamp (struct ring_buffer_event *event); +u64 ring_buffer_event_time_stamp (struct trace_buffer *buffer, +struct ring_buffer_event *event);/* * ring_buffer_discard_commit will remove an event that has not
kernel/trace/ring_buffer.c +45 -15
72 lines not on this page · raw message
kernel/trace/trace_events_hist.c +1 -1
@@ -644,7 +644,7 @@ static u64 hist_field_timestamp(struct hist_field *hist_field,struct hist_trigger_data *hist_data = hist_field->hist_data;struct trace_array *tr = hist_data->event_file->tr; -u64 ts =ring_buffer_event_time_stamp (rbe); +u64 ts =ring_buffer_event_time_stamp (buffer, rbe);if (hist_data->attrs->ts_in_usecs &&trace_clock_in_ns (tr)) ts =ns2usecs (ts);
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 5/13 tracing: Use a no_filter_buffering_ref to stop using the filter buffer
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Currently, the trace histograms relies on it using absolute time stamps to trigger the tracing to not use the temp buffer if filters are set. That's because the histograms need the full timestamp that is saved in the ring buffer. That is no longer the case, as the ring_buffer_event_time_stamp() can now return the time stamp for all events without all triggering a full absolute time stamp.
Now that the absolute time stamp is an unrelated dependency to not using the filters. There's nothing about having absolute timestamps to keep from using the filter buffer. Instead, change the interface to explicitly state to disable filter buffering that the histogram logic can use.
--- kernel/trace/trace.c | 28 ++++++++++++---------------- kernel/trace/trace.h | 4 ++-- kernel/trace/trace_events_hist.c | 6 +++--- 3 files changed, 17 insertions(+), 21 deletions(-)
kernel/trace/trace.c +12 -16
@@ -2737,12 +2737,13 @@ trace_event_buffer_lock_reserve(struct trace_buffer **current_rb, unsigned int trace_ctx) {struct ring_buffer_event *entry; +struct trace_array *tr = trace_file->tr;int val; - *current_rb = trace_file->tr->array_buffer.buffer; + *current_rb = tr->array_buffer.buffer; -if (!ring_buffer_time_stamp_abs (*current_rb) && (trace_file->flags & - (EVENT_FILE_FL_SOFT_DISABLED |EVENT_FILE_FL_FILTERED )) && +if (!tr->no_filter_buffering_ref && + (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED |EVENT_FILE_FL_FILTERED )) && (entry =this_cpu_read (trace_buffered_event))) {/* Try to use the per cpu buffer first */ val =this_cpu_inc_return (trace_buffered_event_cnt);
@@ -6971,31 +6972,26 @@ static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)return ret; } -int tracing_set_time_stamp_abs (struct trace_array *tr,bool abs) +/* +* Set or disable using the per CPU trace_buffer_event when possible. +*/ +int tracing_set_filter_buffering (struct trace_array *tr,bool set) {int ret =0 ;mutex_lock (&trace_types_lock); -if (abs && tr->time_stamp_abs_ref++) +if (set && tr->no_filter_buffering_ref++) goto out; -if (!abs) { -if (WARN_ON_ONCE (!tr->time_stamp_abs_ref)) { +if (!set) { +if (WARN_ON_ONCE (!tr->no_filter_buffering_ref)) { ret = -EINVAL ; goto out; } -if (--tr->time_stamp_abs_ref) - goto out; + --tr->no_filter_buffering_ref; } - -ring_buffer_set_time_stamp_abs (tr->array_buffer.buffer, abs); - -#ifdef CONFIG_TRACER_MAX_TRACE -if (tr->max_buffer.buffer) -ring_buffer_set_time_stamp_abs (tr->max_buffer.buffer, abs); -#endif out:mutex_unlock (&trace_types_lock);
kernel/trace/trace.h +2 -2
@@ -352,7 +352,7 @@ struct trace_array {/* function tracing enabled */ int function_enabled;#endif -int time_stamp_abs_ref; +int no_filter_buffering_ref;struct list_head hist_vars;#ifdef CONFIG_TRACER_SNAPSHOT struct cond_snapshot *cond_snapshot;
@@ -372,7 +372,7 @@ extern int tracing_check_open_get_tr(struct trace_array *tr);extern struct trace_array *trace_array_find (const char *instance);extern struct trace_array *trace_array_find_get (const char *instance); -extern int tracing_set_time_stamp_abs (struct trace_array *tr,bool abs); +extern int tracing_set_filter_buffering (struct trace_array *tr,bool set);extern int tracing_set_clock (struct trace_array *tr,const char *clockstr);extern bool trace_clock_in_ns (struct trace_array *tr);
kernel/trace/trace_events_hist.c +3 -3
@@ -5484,7 +5484,7 @@ static int hist_register_trigger(char *glob, struct event_trigger_ops *ops, goto out; } -tracing_set_time_stamp_abs (file->tr, true); +tracing_set_filter_buffering (file->tr, true); }if (named_data)
@@ -5592,7 +5592,7 @@ static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,if (hist_data->enable_timestamps) {if (!hist_data->remove || unregistered) -tracing_set_time_stamp_abs (file->tr, false); +tracing_set_filter_buffering (file->tr, false); } }
@@ -5639,7 +5639,7 @@ static void hist_unreg_all(struct trace_event_file *file)update_cond_flag (file);if (hist_data->enable_timestamps) -tracing_set_time_stamp_abs (file->tr, false); +tracing_set_filter_buffering (file->tr, false);if (test->ops->free) test->ops->free (test->ops, test); }
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 6/13 ring-buffer: Add verifier for using ring_buffer_event_time_stamp()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The ring_buffer_event_time_stamp() must be only called by an event that has not been committed yet, and is on the buffer that is passed in. This was used to help debug converting the histogram logic over to using the new time stamp code, and was proven to be very useful.
Add a verifier that can check that this is the case, and extra WARN_ONs to catch unexpected use cases.
--- kernel/trace/ring_buffer.c | 56 +++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-)
kernel/trace/ring_buffer.c +52 -4
@@ -742,6 +742,48 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) }#endif +/* +* Enable this to make sure that the event passed to +* ring_buffer_event_time_stamp() is not committed and also +* is on the buffer that it passed in. +*/ +//#define RB_VERIFY_EVENT +#ifdef RB_VERIFY_EVENT +static struct list_head *rb_list_head (struct list_head *list); +static void verify_event (struct ring_buffer_per_cpu *cpu_buffer, +void *event) +{ +struct buffer_page *page = cpu_buffer->commit_page; +struct buffer_page *tail_page =READ_ONCE (cpu_buffer->tail_page); +struct list_head *next; +long commit, write; +unsigned long addr = (unsigned long )event; +bool done = false; +int stop =0 ; + +/* Make sure the event exists and is not committed yet */ +do { +if (page == tail_page ||WARN_ON_ONCE (stop++ >100 )) + done = true; + commit =local_read (&page->page->commit); + write =local_read (&page->write); +if (addr >= (unsigned long )&page->page->data[commit] && + addr < (unsigned long )&page->page->data[write]) +return ; + + next =rb_list_head (page->list.next); + page = list_entry(next, struct buffer_page, list); + }while (!done); +WARN_ON_ONCE (1 ); +} +#else +static inline void verify_event (struct ring_buffer_per_cpu *cpu_buffer, +void *event) +{ +} +#endif + +static inline u64 rb_time_stamp (struct trace_buffer *buffer);/**
@@ -772,13 +814,19 @@ u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,if (event->type_len ==RINGBUF_TYPE_TIME_STAMP )return rb_event_time_stamp (event); + nest =local_read (&cpu_buffer->committing); +verify_event (cpu_buffer, event); +if (WARN_ON_ONCE (!nest)) + goto fail; +/* Read the current saved nesting level time stamp */ - nest =local_read (&cpu_buffer->committing) -1 ; -if (likely (nest <MAX_NEST )) +if (likely (--nest <MAX_NEST ))return cpu_buffer->event_stamp[nest]; -WARN_ON_ONCE (1 ); +/* Shouldn't happen, warn if it does */ +WARN_ONCE (1 ,"nest (%d) greater than max" , nest); + fail:/* Can only fail on 32 bit */ if (!rb_time_read (&cpu_buffer->write_stamp, &ts))/* Screw it, just read the current time */
@@ -2750,7 +2798,7 @@ rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,u64 delta = info->delta;unsigned int nest =local_read (&cpu_buffer->committing) -1 ; -if (nest <MAX_NEST ) +if (!WARN_ON_ONCE (nest >=MAX_NEST )) cpu_buffer->event_stamp[nest] = info->ts;/*
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 7/13 tracing: Add tracing_event_time_stamp() API
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add a tracing_event_time_stamp() API that checks if the event passed in is not on the ring buffer but a pointer to the per CPU trace_buffered_event which does not have its time stamp set yet.
If it is a pointer to the trace_buffered_event, then just return the current time stamp that the ring buffer would produce.
Otherwise, return the time stamp from the event.
--- kernel/trace/trace.c | 8 ++++++++ kernel/trace/trace.h | 1 + 2 files changed, 9 insertions(+)
kernel/trace/trace.c +8 -0
@@ -6972,6 +6972,14 @@ static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)return ret; } +u64 tracing_event_time_stamp (struct trace_buffer *buffer,struct ring_buffer_event *rbe) +{ +if (rbe ==this_cpu_read (trace_buffered_event)) +return ring_buffer_time_stamp (buffer,smp_processor_id ()); + +return ring_buffer_event_time_stamp (buffer, rbe); +} +/* * Set or disable using the per CPU trace_buffer_event when possible. */
kernel/trace/trace.h +1 -0
@@ -372,6 +372,7 @@ extern int tracing_check_open_get_tr(struct trace_array *tr);extern struct trace_array *trace_array_find (const char *instance);extern struct trace_array *trace_array_find_get (const char *instance); +extern u64 tracing_event_time_stamp (struct trace_buffer *buffer,struct ring_buffer_event *rbe);extern int tracing_set_filter_buffering (struct trace_array *tr,bool set);extern int tracing_set_clock (struct trace_array *tr,const char *clockstr);
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 8/13 bootconfig: Update prototype of setup_boot_config()
In reply to Steven RostedtFrom: Cao jin <[email protected]>
Parameter "cmdline" has no use, drop it.
--- init/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
init/main.c +3 -3
@@ -405,7 +405,7 @@ static int __init bootconfig_params(char *param, char *val,return 0 ; } -static void __init setup_boot_config (const char *cmdline) +static void __init setup_boot_config (void ) {static char tmp_cmdline[COMMAND_LINE_SIZE ]__initdata ;const char *msg;
@@ -472,7 +472,7 @@ static void __init setup_boot_config(const char *cmdline)#else -static void __init setup_boot_config (const char *cmdline) +static void __init setup_boot_config (void ) {/* Remove bootconfig data from initrd */ get_boot_config_from_initrd (NULL ,NULL );
@@ -872,7 +872,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)pr_notice ("%s" , linux_banner);early_security_init ();setup_arch (&command_line); -setup_boot_config (command_line); +setup_boot_config ();setup_command_line (command_line);setup_nr_cpu_ids ();setup_per_cpu_areas ();
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 9/13 tools/latency-collector: Remove unneeded semicolon
In reply to Steven RostedtFrom: Xu Wang <[email protected]>
Fix semicolon.cocci warning:
tools/tracing/latency/latency-collector.c:1021:2-3: Unneeded semicolon
--- tools/tracing/latency/latency-collector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/tracing/latency/latency-collector.c +2 -2
@@ -1018,7 +1018,7 @@ static long go_to_sleep(const struct entry *req)cond_timedwait (&printstate.cond, &printstate.mutex, &future);if (time_has_passed (&future))break ; - }; + }if (printstate_has_new_req_arrived (req)) delay =-1 ;
@@ -1941,7 +1941,7 @@ static void scan_arguments(int argc, char *argv[])if (value <0 ) {warnx ("TIME must be >= 0\n" );show_usage (); - ; +exit (0 ); } trace_enable = true; use_random_sleep = true;
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 10/13 ftrace: Fix spelling mistake "disabed" -> "disabled"
In reply to Steven RostedtFrom: Colin Ian King <[email protected]>
There is a spelling mistake in a comment, fix it.
--- arch/csky/kernel/probes/ftrace.c | 2 +- arch/riscv/kernel/probes/ftrace.c | 2 +- arch/x86/kernel/kprobes/ftrace.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
arch/csky/kernel/probes/ftrace.c +1 -1
@@ -9,7 +9,7 @@ int arch_check_ftrace_location(struct kprobe *p)return 0 ; } -/* Ftrace callback handler for kprobes -- called under preepmt disabed */ +/* Ftrace callback handler for kprobes -- called under preepmt disabled */ void kprobe_ftrace_handler (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *ops,struct ftrace_regs *fregs) {
arch/riscv/kernel/probes/ftrace.c +1 -1
@@ -2,7 +2,7 @@#include <linux/kprobes.h> -/* Ftrace callback handler for kprobes -- called under preepmt disabed */ +/* Ftrace callback handler for kprobes -- called under preepmt disabled */ void kprobe_ftrace_handler (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *ops,struct ftrace_regs *regs) {
arch/x86/kernel/kprobes/ftrace.c +1 -1
@@ -12,7 +12,7 @@#include "common.h" -/* Ftrace callback handler for kprobes -- called under preepmt disabed */ +/* Ftrace callback handler for kprobes -- called under preepmt disabled */ void kprobe_ftrace_handler (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *ops,struct ftrace_regs *fregs) {
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 11/13 tracing: Add check of trace event print fmts for dereferencing pointers
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Trace events record data into the ring buffer at the time of the event. The trace event has a printf logic to display the recorded data at a much later time when the user reads the trace file. This makes using dereferencing pointers unsafe if the dereferenced pointer points to the original source. The safe way to handle this is to create an array within the trace event and copy the source into the array. Then the dereference pointer may point to that array.
As this is a easy mistake to make, a check is added to examine all trace event print fmts to make sure that they are safe to use. This only checks the various %p* dereferenced pointers like %pB, %pR, etc. It does not handle dereferencing of strings, as there are some use cases that are OK to dereference the source. That will be dealt with differently.
--- kernel/trace/trace_events.c | 210 ++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+)
kernel/trace/trace_events.c +210 -0
@@ -217,6 +217,214 @@ int trace_event_get_offsets(struct trace_event_call *call)return tail->offset + tail->size; } +/* +* Check if the referenced field is an array and return true, +* as arrays are OK to dereference. +*/ +static bool test_field (const char *fmt,struct trace_event_call *call) +{ +struct trace_event_fields *field = call->class->fields_array; +const char *array_descriptor; +const char *p = fmt; +int len; + +if (!(len =str_has_prefix (fmt,"REC->" ))) +return false; + fmt += len; +for (p = fmt; *p; p++) { +if (!isalnum (*p) && *p !='_' ) +break ; + } + len = p - fmt; + +for (; field->type; field++) { +if (strncmp (field->name, fmt, len) || + field->name[len]) +continue ; + array_descriptor =strchr (field->type,'[' ); +/* This is an array and is OK to dereference. */ +return array_descriptor !=NULL ; + } +return false; +} + +/* +* Examine the print fmt of the event looking for unsafe dereference +* pointers using %p* that could be recorded in the trace event and +* much later referenced after the pointer was freed. Dereferencing +* pointers are OK, if it is dereferenced into the event itself. +*/ +static void test_event_printk (struct trace_event_call *call) +{ +u64 dereference_flags =0 ; +bool first = true; +const char *fmt, *c, *r, *a; +int parens =0 ; +char in_quote =0 ; +int start_arg =0 ; +int arg =0 ; +int i; + + fmt = call->print_fmt; + +if (!fmt) +return ; + +for (i =0 ; fmt[i]; i++) { +switch (fmt[i]) { +case '\\' : + i++; +if (!fmt[i]) +return ; +continue ; +case '"' : +case '\'' : +/* +* The print fmt starts with a string that +* is processed first to find %p* usage, +* then after the first string, the print fmt +* contains arguments that are used to check +* if the dereferenced %p* usage is safe. +*/ +if (first) { +if (fmt[i] =='\'' ) +continue ; +if (in_quote) { + arg =0 ; + first = false; +/* +* If there was no %p* uses +* the fmt is OK. +*/ +if (!dereference_flags) +return ; + } + } +if (in_quote) { +if (in_quote == fmt[i]) + in_quote =0 ; + }else { + in_quote = fmt[i]; + } +continue ; +case '%' : +if (!first || !in_quote) +continue ; + i++; +if (!fmt[i]) +return ; +switch (fmt[i]) { +case '%' : +continue ; +case 'p' : +/* Find dereferencing fields */ +switch (fmt[i +1 ]) { +case 'B' :case 'R' :case 'r' : +case 'b' :case 'M' :case 'm' : +case 'I' :case 'i' :case 'E' : +case 'U' :case 'V' :case 'N' : +case 'a' :case 'd' :case 'D' : +case 'g' :case 't' :case 'C' : +case 'O' :case 'f' : +if (WARN_ONCE (arg ==63 , +"Too many args for event: %s" , +trace_event_name (call))) +return ; + dereference_flags |=1ULL << arg; + } +break ; +default : + { +bool star = false; +int j; + +/* Increment arg if %*s exists. */ +for (j =0 ; fmt[i + j]; j++) { +if (isdigit (fmt[i + j]) || + fmt[i + j] =='.' ) +continue ; +if (fmt[i + j] =='*' ) { + star = true; +continue ; + } +if ((fmt[i + j] =='s' ) && star) + arg++; +break ; + } +break ; + }/* default */ + + }/* switch */ + arg++; +continue ; +case '(' : +if (in_quote) +continue ; + parens++; +continue ; +case ')' : +if (in_quote) +continue ; + parens--; +if (WARN_ONCE (parens <0 , +"Paren mismatch for event: %s\narg='%s'\n%*s" , +trace_event_name (call), + fmt + start_arg, + (i - start_arg) +5 ,"^" )) +return ; +continue ; +case ',' : +if (in_quote || parens) +continue ; + i++; +while (isspace (fmt[i])) + i++; + start_arg = i; +if (!(dereference_flags & (1ULL << arg))) + goto next_arg; + +/* Find the REC-> in the argument */ + c =strchr (fmt + i,',' ); + r =strstr (fmt + i,"REC->" ); +if (r && (!c || r < c)) { +/* +* Addresses of events on the buffer, +* or an array on the buffer is +* OK to dereference. +* There's ways to fool this, but +* this is to catch common mistakes, +* not malicious code. +*/ + a =strchr (fmt + i,'&' ); +if ((a && (a < r)) ||test_field (r, call)) + dereference_flags &= ~(1ULL << arg); + } + next_arg: + i--; + arg++; + } + } + +/* +* If you triggered the below warning, the trace event reported +* uses an unsafe dereference pointer %p*. As the data stored +* at the trace event time may no longer exist when the trace +* event is printed, dereferencing to the original source is +* unsafe. The source of the dereference must be copied into the +* event itself, and the dereference must access the copy instead. +*/ +if (WARN_ON_ONCE (dereference_flags)) { + arg =1 ; +while (!(dereference_flags &1 )) { + dereference_flags >>=1 ; + arg++; + } +pr_warn ("event %s has unsafe dereference of argument %d\n" , +trace_event_name (call), arg); +pr_warn ("print_fmt: %s\n" , fmt); + } +} +int trace_event_raw_init (struct trace_event_call *call) {int id;
@@ -225,6 +433,8 @@ int trace_event_raw_init(struct trace_event_call *call)if (!id)return -ENODEV ; +test_event_printk (call); +return 0 ; }EXPORT_SYMBOL_GPL (trace_event_raw_init);
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 12/13 seq_buf: Add seq_buf_terminate() API
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
In the case that the seq_buf buffer needs to be printed directly, add a way to make sure that the buffer is safe to read by forcing a nul terminating character at the end of the string, or the last byte of the buffer if the string has overflowed.
--- include/linux/seq_buf.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/seq_buf.h +25 -0
@@ -71,6 +71,31 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)return min (s->len, s->size); } +/** +* seq_buf_terminate - Make sure buffer is nul terminated +* @s: the seq_buf descriptor to terminate. +* +* This makes sure that the buffer in @s is nul terminated and +* safe to read as a string. +* +* Note, if this is called when the buffer has overflowed, then +* the last byte of the buffer is zeroed, and the len will still +* point passed it. +* +* After this function is called, s->buffer is safe to use +* in string operations. +*/ +static inline void seq_buf_terminate (struct seq_buf *s) +{ +if (WARN_ON (s->size ==0 )) +return ; + +if (seq_buf_buffer_left (s)) + s->buffer[s->len] =0 ; +else + s->buffer[s->size -1 ] =0 ; +} +/** * seq_buf_get_buf - get buffer to write arbitrary data to * @s: the seq_buf handle
Signature · 1 line
2.30.1
Steven Rostedt
maintainerPATCH 13/13 tracing: Add a verifier to check string pointers for trace events
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
It is a common mistake for someone writing a trace event to save a pointer to a string in the TP_fast_assign() and then display that string pointer in the TP_printk() with %s. The problem is that those two events may happen a long time apart, where the source of the string may no longer exist.
The proper way to handle displaying any string that is not guaranteed to be in the kernel core rodata section, is to copy it into the ring buffer via the __string(), __assign_str() and __get_str() helper macros.
Add a check at run time while displaying the TP_printk() of events to make sure that every %s referenced is safe to dereference, and if it is not, trigger a warning and only show the address of the pointer, and the dereferenced string if it can be safely retrieved with a strncpy_from_kernel_nofault() call.
In order to not have to copy the parsing of vsnprintf() formats, or even exporting its code, the verifier relies on vsnprintf() being able to modify the va_list that is passed to it, and it remains modified after it is called. This is the case for some architectures like x86_64, but other architectures like x86_32 pass the va_list to vsnprintf() as a value not a reference, and the verifier can not use it to parse the non string arguments. Thus, at boot up, it is checked if vsnprintf() modifies the passed in va_list or not, and a static branch will disable the verifier if it's not compatible.
--- kernel/trace/trace.c | 200 ++++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_output.c | 2 +- kernel/trace/trace_printk.c | 11 ++ 4 files changed, 215 insertions(+), 1 deletion(-)
kernel/trace/trace.c +200 -0
@@ -3558,6 +3558,204 @@ static char *trace_iter_expand_format(struct trace_iterator *iter)return tmp; } +/* Returns true if the string is safe to dereference from an event */ +static bool trace_safe_str (struct trace_iterator *iter,const char *str) +{ +unsigned long addr = (unsigned long )str; +struct trace_event *trace_event; +struct trace_event_call *event; + +/* OK if part of the event data */ +if ((addr >= (unsigned long )iter->ent) && + (addr < (unsigned long )iter->ent + iter->ent_size)) +return true; + +/* OK if part of the temp seq buffer */ +if ((addr >= (unsigned long )iter->tmp_seq.buffer) && + (addr < (unsigned long )iter->tmp_seq.buffer +PAGE_SIZE )) +return true; + +/* Core rodata can not be freed */ +if (is_kernel_rodata (addr)) +return true; + +if (trace_is_tracepoint_string (str)) +return true; + +/* +* Now this could be a module event, referencing core module +* data, which is OK. +*/ +if (!iter->ent) +return false; + + trace_event =ftrace_find_event (iter->ent->type); +if (!trace_event) +return false; + + event = container_of(trace_event, struct trace_event_call, event); +if (!event->mod) +return false; + +/* Would rather have rodata, but this will suffice */ +if (within_module_core (addr, event->mod)) +return true; + +return false; +} + +static const char *show_buffer (struct trace_seq *s) +{ +struct seq_buf *seq = &s->seq; + +seq_buf_terminate (seq); + +return seq->buffer; +} + +static DEFINE_STATIC_KEY_FALSE (trace_no_verify); + +static int test_can_verify_check (const char *fmt, ...) +{ +char buf[16 ]; +va_list ap; +int ret; + +/* +* The verifier is dependent on vsnprintf() modifies the va_list +* passed to it, where it is sent as a reference. Some architectures +* (like x86_32) passes it by value, which means that vsnprintf() +* does not modify the va_list passed to it, and the verifier +* would then need to be able to understand all the values that +* vsnprintf can use. If it is passed by value, then the verifier +* is disabled. +*/ +va_start (ap, fmt); +vsnprintf (buf,16 ,"%d" , ap); + ret =va_arg (ap, int); +va_end (ap); + +return ret; +} + +static void test_can_verify (void ) +{ +if (!test_can_verify_check ("%d %d" ,0 ,1 )) { +pr_info ("trace event string verifier disabled\n" ); +static_branch_inc (&trace_no_verify); + } +} + +/** +* trace_check_vprintf - Check dereferenced strings while writing to the seq buffer +* @iter: The iterator that holds the seq buffer and the event being printed +* @fmt: The format used to print the event +* @ap: The va_list holding the data to print from @fmt. +* +* This writes the data into the @iter->seq buffer using the data from +* @fmt and @ap. If the format has a %s, then the source of the string +* is examined to make sure it is safe to print, otherwise it will +* warn and print "[UNSAFE MEMORY]" in place of the dereferenced string +* pointer. +*/ +void trace_check_vprintf (struct trace_iterator *iter,const char *fmt, +va_list ap) +{ +const char *p = fmt; +const char *str; +int i, j; + +if (WARN_ON_ONCE (!fmt)) +return ; + +if (static_branch_unlikely (&trace_no_verify)) + goto print; + +/* Don't bother checking when doing a ftrace_dump() */ +if (iter->fmt == static_fmt_buf) + goto print; + +while (*p) { + j =0 ; + +/* We only care about %s and variants */ +for (i =0 ; p[i]; i++) { +if (i +1 >= iter->fmt_size) { +/* +* If we can't expand the copy buffer, +* just print it. +*/ +if (!trace_iter_expand_format (iter)) + goto print; + } + +if (p[i] =='\\' && p[i+1 ]) { + i++; +continue ; + } +if (p[i] =='%' ) { +/* Need to test cases like %08.*s */ +for (j =1 ; p[i+j]; j++) { +if (isdigit (p[i+j]) || + p[i+j] =='*' || + p[i+j] =='.' ) +continue ; +break ; + } +if (p[i+j] =='s' ) +break ; + } + j =0 ; + } +/* If no %s found then just print normally */ +if (!p[i]) +break ; + +/* Copy up to the %s, and print that */ +strncpy (iter->fmt, p, i); + iter->fmt[i] ='\0' ; +trace_seq_vprintf (&iter->seq, iter->fmt, ap); + +/* The ap now points to the string data of the %s */ + str = va_arg(ap, const char *); + +/* +* If you hit this warning, it is likely that the +* trace event in question used %s on a string that +* was saved at the time of the event, but may not be +* around when the trace is read. Use __string(), +* __assign_str() and __get_str() helpers in the TRACE_EVENT() +* instead. See samples/trace_events/trace-events-sample.h +* for reference. +*/ +if (WARN_ONCE (!trace_safe_str (iter, str), +"fmt: '%s' current_buffer: '%s'" , + fmt,show_buffer (&iter->seq))) { +int ret; + +/* Try to safely read the string */ + ret =strncpy_from_kernel_nofault (iter->fmt, str, + iter->fmt_size); +if (ret <0 ) +trace_seq_printf (&iter->seq,"(0x%px)" , str); +else +trace_seq_printf (&iter->seq,"(0x%px:%s)" , + str, iter->fmt); + str ="[UNSAFE-MEMORY]" ; +strcpy (iter->fmt,"%s" ); + }else { +strncpy (iter->fmt, p + i, j +1 ); + iter->fmt[j+1 ] ='\0' ; + } +trace_seq_printf (&iter->seq, iter->fmt, str); + + p += i + j +1 ; + } + print: +if (*p) +trace_seq_vprintf (&iter->seq, p, ap); +} +const char *trace_event_format (struct trace_iterator *iter,const char *fmt) {const char *p, *new_fmt;
@@ -9675,6 +9873,8 @@ __init static int tracer_alloc_buffers(void)register_snapshot_cmd (); +test_can_verify (); +return 0 ; out_free_savedcmd:
kernel/trace/trace.h +3 -0
@@ -582,7 +582,10 @@ struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,void trace_buffer_unlock_commit_nostack (struct trace_buffer *buffer,struct ring_buffer_event *event); +bool trace_is_tracepoint_string (const char *str);const char *trace_event_format (struct trace_iterator *iter,const char *fmt); +void trace_check_vprintf (struct trace_iterator *iter,const char *fmt, +va_list ap);int trace_empty (struct trace_iterator *iter);
kernel/trace/trace_output.c +1 -1
@@ -317,7 +317,7 @@ void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)va_list ap;va_start (ap, fmt); -trace_seq_vprintf (&iter->seq,trace_event_format (iter, fmt), ap); +trace_check_vprintf (iter,trace_event_format (iter, fmt), ap);va_end (ap); }EXPORT_SYMBOL (trace_event_printf);
kernel/trace/trace_printk.c +11 -0
@@ -251,6 +251,17 @@ int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap) }EXPORT_SYMBOL_GPL (__ftrace_vprintk); +bool trace_is_tracepoint_string (const char *str) +{ +const char **ptr = __start___tracepoint_str; + +for (ptr = __start___tracepoint_str; ptr < __stop___tracepoint_str; ptr++) { +if (str == *ptr) +return true; + } +return false; +} +static const char **find_next (void *v,loff_t *pos) {const char **fmt = v;
Signature · 1 line
2.30.1