tracing: Fixes for v5.4-rc2
Subsystems (4)
Steven Rostedt
maintainerPATCH 7/11 tracing: Do not create tracefs files if tracefs lockdown is in effect
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
If on boot up, lockdown is activated for tracefs, don't even bother creating the files. This can also prevent instances from being created if lockdown is in effect.
--- fs/tracefs/inode.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/tracefs/inode.c +4 -0
@@ -16,6 +16,7 @@#include <linux/namei.h> #include <linux/tracefs.h> #include <linux/fsnotify.h> +#include <linux/security.h> #include <linux/seq_file.h> #include <linux/parser.h> #include <linux/magic.h>
@@ -390,6 +391,9 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,struct dentry *dentry;struct inode *inode; +if (security_locked_down (LOCKDOWN_TRACEFS )) +return NULL ; +if (!(mode &S_IFMT )) mode |=S_IFREG ;BUG_ON (!S_ISREG (mode));
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 2/11 ftrace: Get a reference counter for the trace_array on filter files
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The ftrace set_ftrace_filter and set_ftrace_notrace files are specific for an instance now. They need to take a reference to the instance otherwise there could be a race between accessing the files and deleting the instance.
It wasn't until the :mod: caching where these file operations started referencing the trace_array directly.
--- kernel/trace/ftrace.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)
kernel/trace/ftrace.c +18 -9
@@ -3540,21 +3540,22 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,struct ftrace_hash *hash;struct list_head *mod_head;struct trace_array *tr = ops->private; -int ret =0 ; +int ret = -ENOMEM ;ftrace_ops_init (ops);if (unlikely (ftrace_disabled))return -ENODEV ; +if (tr &&trace_array_get (tr) <0 ) +return -ENODEV ; + iter =kzalloc (sizeof (*iter),GFP_KERNEL );if (!iter) -return -ENOMEM ; + goto out; -if (trace_parser_get_init (&iter->parser,FTRACE_BUFF_MAX )) { -kfree (iter); -return -ENOMEM ; - } +if (trace_parser_get_init (&iter->parser,FTRACE_BUFF_MAX )) + goto out; iter->ops = ops; iter->flags = flag;
@@ -3584,13 +3585,13 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,if (!iter->hash) {trace_parser_put (&iter->parser); -kfree (iter); - ret = -ENOMEM ; goto out_unlock; } }else iter->hash = hash; + ret =0 ; +if (file->f_mode &FMODE_READ ) { iter->pg = ftrace_pages_start;
@@ -3602,7 +3603,6 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,/* Failed */ free_ftrace_hash (iter->hash);trace_parser_put (&iter->parser); -kfree (iter); } }else file->private_data = iter;
@@ -3610,6 +3610,13 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag, out_unlock:mutex_unlock (&ops->func_hash->regex_lock); + out: +if (ret) { +kfree (iter); +if (tr) +trace_array_put (tr); + } +return ret; }
@@ -5037,6 +5044,8 @@ int ftrace_regex_release(struct inode *inode, struct file *file)mutex_unlock (&iter->ops->func_hash->regex_lock);free_ftrace_hash (iter->hash); +if (iter->tr) +trace_array_put (iter->tr);kfree (iter);return 0 ;
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 4/11 tracing: Have trace events system open call tracing_open_generic_tr()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Instead of having the trace events system open call open code the taking of the trace_array descriptor (with trace_array_get()) and then calling trace_open_generic(), have it use the tracing_open_generic_tr() that does the combination of the two. This requires making tracing_open_generic_tr() global.
--- kernel/trace/trace.c | 2 +- kernel/trace/trace.h | 1 + kernel/trace/trace_events.c | 17 +++-------------- 3 files changed, 5 insertions(+), 15 deletions(-)
kernel/trace/trace.c +1 -1
@@ -4156,7 +4156,7 @@ bool tracing_is_disabled(void)* Open and update trace_array ref count. * Must have the current trace_array passed to it. */ -static int tracing_open_generic_tr (struct inode *inode,struct file *filp) +int tracing_open_generic_tr (struct inode *inode,struct file *filp) {struct trace_array *tr = inode->i_private;
kernel/trace/trace.h +1 -0
@@ -681,6 +681,7 @@ void tracing_reset_online_cpus(struct trace_buffer *buf);void tracing_reset_current (int cpu);void tracing_reset_all_online_cpus (void );int tracing_open_generic (struct inode *inode,struct file *filp); +int tracing_open_generic_tr (struct inode *inode,struct file *filp);bool tracing_is_disabled (void );bool tracer_tracing_is_on (struct trace_array *tr);void tracer_tracing_on (struct trace_array *tr);
kernel/trace/trace_events.c +3 -14
@@ -1440,28 +1440,17 @@ static int system_tr_open(struct inode *inode, struct file *filp)struct trace_array *tr = inode->i_private;int ret; -if (tracing_is_disabled ()) -return -ENODEV ; - -if (trace_array_get (tr) <0 ) -return -ENODEV ; -/* Make a temporary dir that has no system but points to tr */ dir =kzalloc (sizeof (*dir),GFP_KERNEL ); -if (!dir) { -trace_array_put (tr); +if (!dir)return -ENOMEM ; - } - - dir->tr = tr; - ret =tracing_open_generic (inode, filp); + ret =tracing_open_generic_tr (inode, filp);if (ret <0 ) { -trace_array_put (tr);kfree (dir);return ret; } - + dir->tr = tr; filp->private_data = dir;return 0 ;
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 11/11 tracing: Initialize iter->seq after zeroing in tracing_read_pipe()
In reply to Steven RostedtFrom: Petr Mladek <[email protected]>
A customer reported the following softlockup:
[899688.160002] NMI watchdog: BUG: soft lockup - CPU#0 stuck for 22s! [test.sh:16464] [899688.160002] CPU: 0 PID: 16464 Comm: test.sh Not tainted 4.12.14-6.23-azure #1 SLE12-SP4 [899688.160002] RIP: 0010:up_write+0x1a/0x30 [899688.160002] Kernel panic - not syncing: softlockup: hung tasks [899688.160002] RIP: 0010:up_write+0x1a/0x30 [899688.160002] RSP: 0018:ffffa86784d4fde8 EFLAGS: 00000257 ORIG_RAX: ffffffffffffff12 [899688.160002] RAX: ffffffff970fea00 RBX: 0000000000000001 RCX: 0000000000000000 [899688.160002] RDX: ffffffff00000001 RSI: 0000000000000080 RDI: ffffffff970fea00 [899688.160002] RBP: ffffffffffffffff R08: ffffffffffffffff R09: 0000000000000000 [899688.160002] R10: 0000000000000000 R11: 0000000000000000 R12: ffff8b59014720d8 [899688.160002] R13: ffff8b59014720c0 R14: ffff8b5901471090 R15: ffff8b5901470000 [899688.160002] tracing_read_pipe+0x336/0x3c0 [899688.160002] __vfs_read+0x26/0x140 [899688.160002] vfs_read+0x87/0x130 [899688.160002] SyS_read+0x42/0x90 [899688.160002] do_syscall_64+0x74/0x160
It caught the process in the middle of trace_access_unlock(). There is no loop. So, it must be looping in the caller tracing_read_pipe() via the "waitagain" label.
Crashdump analyze uncovered that iter->seq was completely zeroed at this point, including iter->seq.seq.size. It means that print_trace_line() was never able to print anything and there was no forward progress.
The culprit seems to be in the code:
/* reset all but tr, trace, and overruns */ memset(&iter->seq, 0, sizeof(struct trace_iterator) - offsetof(struct trace_iterator, seq));
It was added by the commit 53d0aa773053ab182877 ("ftrace:
add logic to record overruns"). It was v2.6.27-rc1. It was the time when iter->seq looked like:
struct trace_seq {
unsigned char buffer[PAGE_SIZE];
unsigned int len;
};There was no "size" variable and zeroing was perfectly fine.
The solution is to reinitialize the structure after or without zeroing.
--- kernel/trace/trace.c | 1 + 1 file changed, 1 insertion(+)
kernel/trace/trace.c +1 -0
@@ -6036,6 +6036,7 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,sizeof (struct trace_iterator ) - offsetof(struct trace_iterator, seq));cpumask_clear (iter->started); +trace_seq_init (&iter->seq); iter->pos =-1 ;trace_event_read_lock ();
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 6/11 tracing: Add locked_down checks to the open calls of files created for tracefs
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Added various checks on open tracefs calls to see if tracefs is in lockdown mode, and if so, to return -EPERM.
Note, the event format files (which are basically standard on all machines) as well as the enabled_functions file (which shows what is currently being traced) are not lockde down. Perhaps they should be, but it seems counter intuitive to lockdown information to help you know if the system has been modified.
--- kernel/trace/ftrace.c | 23 ++++++++++++++++++++++- kernel/trace/trace.c | 8 ++++++++ kernel/trace/trace_events.c | 8 ++++++++ kernel/trace/trace_events_hist.c | 11 +++++++++++ kernel/trace/trace_events_trigger.c | 8 +++++++- kernel/trace/trace_kprobe.c | 12 +++++++++++- kernel/trace/trace_printk.c | 7 +++++++ kernel/trace/trace_stack.c | 8 ++++++++ kernel/trace/trace_stat.c | 6 +++++- kernel/trace/trace_uprobe.c | 11 +++++++++++ 10 files changed, 98 insertions(+), 4 deletions(-)
kernel/trace/ftrace.c +22 -1
@@ -18,6 +18,7 @@#include <linux/clocksource.h> #include <linux/sched/task.h> #include <linux/kallsyms.h> +#include <linux/security.h> #include <linux/seq_file.h> #include <linux/tracefs.h> #include <linux/hardirq.h>
@@ -3486,6 +3487,11 @@ static int ftrace_avail_open(struct inode *inode, struct file *file) { struct ftrace_iterator *iter; +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret;if (unlikely (ftrace_disabled))return -ENODEV ;
@@ -3505,6 +3511,15 @@ ftrace_enabled_open(struct inode *inode, struct file *file) {struct ftrace_iterator *iter; +/* +* This shows us what functions are currently being +* traced and by what. Not sure if we want lockdown +* to hide such critical information for an admin. +* Although, perhaps it can show information we don't +* want people to see, but if something is tracing +* something, we probably want to know about it. +*/ + iter =__seq_open_private (file, &show_ftrace_seq_ops,sizeof (*iter));if (!iter)return -ENOMEM ;
@@ -3625,6 +3640,7 @@ ftrace_filter_open(struct inode *inode, struct file *file) {struct ftrace_ops *ops = inode->i_private; +/* Checks for tracefs lockdown */ return ftrace_regex_open (ops,FTRACE_ITER_FILTER |FTRACE_ITER_DO_PROBES , inode, file);
@@ -3635,6 +3651,7 @@ ftrace_notrace_open(struct inode *inode, struct file *file) {struct ftrace_ops *ops = inode->i_private; +/* Checks for tracefs lockdown */ return ftrace_regex_open (ops,FTRACE_ITER_NOTRACE , inode, file); }
@@ -5203,9 +5220,13 @@ static int __ftrace_graph_open(struct inode *inode, struct file *file, struct ftrace_graph_data *fgd) { - int ret = 0; + int ret;struct ftrace_hash *new_hash =NULL ; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +if (file->f_mode &FMODE_WRITE ) {const int size_bits =FTRACE_HASH_DEFAULT_BITS ;
kernel/trace/trace.c +8 -0
@@ -17,6 +17,7 @@#include <linux/stacktrace.h> #include <linux/writeback.h> #include <linux/kallsyms.h> +#include <linux/security.h> #include <linux/seq_file.h> #include <linux/notifier.h> #include <linux/irqflags.h>
@@ -306,6 +307,12 @@ void trace_array_put(struct trace_array *this_tr)int tracing_check_open_get_tr (struct trace_array *tr) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +if (tracing_disabled)return -ENODEV ;
@@ -6813,6 +6820,7 @@ static int snapshot_raw_open(struct inode *inode, struct file *filp)struct ftrace_buffer_info *info;int ret; +/* The following checks for tracefs lockdown */ ret =tracing_buffers_open (inode, filp);if (ret <0 )return ret;
kernel/trace/trace_events.c +8 -0
@@ -12,6 +12,7 @@#define pr_fmt (fmt) fmt#include <linux/workqueue.h> +#include <linux/security.h> #include <linux/spinlock.h> #include <linux/kthread.h> #include <linux/tracefs.h>
@@ -1294,6 +1295,8 @@ static int trace_format_open(struct inode *inode, struct file *file)struct seq_file *m;int ret; +/* Do we want to hide event format files on tracefs lockdown? */ + ret =seq_open (file, &trace_format_seq_ops);if (ret <0 )return ret;
@@ -1760,6 +1763,10 @@ ftrace_event_open(struct inode *inode, struct file *file,struct seq_file *m;int ret; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; + ret =seq_open (file, seq_ops);if (ret <0 )return ret;
@@ -1784,6 +1791,7 @@ ftrace_event_avail_open(struct inode *inode, struct file *file) {const struct seq_operations *seq_ops = &show_event_seq_ops; +/* Checks for tracefs lockdown */ return ftrace_event_open (inode, file, seq_ops); }
kernel/trace/trace_events_hist.c +11 -0
@@ -7,6 +7,7 @@#include <linux/module.h> #include <linux/kallsyms.h> +#include <linux/security.h> #include <linux/mutex.h> #include <linux/slab.h> #include <linux/stacktrace.h>
@@ -1448,6 +1449,10 @@ static int synth_events_open(struct inode *inode, struct file *file) {int ret; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { ret =dyn_events_release_all (&synth_event_ops);if (ret <0 )
@@ -5515,6 +5520,12 @@ static int hist_show(struct seq_file *m, void *v)static int event_hist_open (struct inode *inode,struct file *file) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +return single_open (file, hist_show, file); }
kernel/trace/trace_events_trigger.c +7 -1
@@ -5,6 +5,7 @@* Copyright (C) 2013 Tom Zanussi <[email protected]> */ +#include <linux/security.h> #include <linux/module.h> #include <linux/ctype.h> #include <linux/mutex.h>
@@ -173,7 +174,11 @@ static const struct seq_operations event_triggers_seq_ops = {static int event_trigger_regex_open (struct inode *inode,struct file *file) { -int ret =0 ; +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret;mutex_lock (&event_mutex);
@@ -292,6 +297,7 @@ event_trigger_write(struct file *filp, const char __user *ubuf,static int event_trigger_open (struct inode *inode,struct file *filp) { +/* Checks for tracefs lockdown */ return event_trigger_regex_open (inode, filp); }
kernel/trace/trace_kprobe.c +11 -1
@@ -7,11 +7,11 @@*/ #define pr_fmt (fmt) "trace_kprobe: " fmt +#include <linux/security.h> #include <linux/module.h> #include <linux/uaccess.h> #include <linux/rculist.h> #include <linux/error-injection.h> -#include <linux/security.h> #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
@@ -936,6 +936,10 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { ret =dyn_events_release_all (&trace_kprobe_ops);if (ret <0 )
@@ -988,6 +992,12 @@ static const struct seq_operations profile_seq_op = {static int profile_open (struct inode *inode,struct file *file) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +return seq_open (file, &profile_seq_op); }
kernel/trace/trace_printk.c +7 -0
@@ -6,6 +6,7 @@* */ #include <linux/seq_file.h> +#include <linux/security.h> #include <linux/uaccess.h> #include <linux/kernel.h> #include <linux/ftrace.h>
@@ -348,6 +349,12 @@ static const struct seq_operations show_format_seq_ops = {static int ftrace_formats_open (struct inode *inode,struct file *file) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +return seq_open (file, &show_format_seq_ops); }
kernel/trace/trace_stack.c +8 -0
@@ -5,6 +5,7 @@*/ #include <linux/sched/task_stack.h> #include <linux/stacktrace.h> +#include <linux/security.h> #include <linux/kallsyms.h> #include <linux/seq_file.h> #include <linux/spinlock.h>
@@ -470,6 +471,12 @@ static const struct seq_operations stack_trace_seq_ops = {static int stack_trace_open (struct inode *inode,struct file *file) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +return seq_open (file, &stack_trace_seq_ops); }
@@ -487,6 +494,7 @@ stack_trace_filter_open(struct inode *inode, struct file *file) {struct ftrace_ops *ops = inode->i_private; +/* Checks for tracefs lockdown */ return ftrace_regex_open (ops,FTRACE_ITER_FILTER , inode, file); }
kernel/trace/trace_stat.c +5 -1
@@ -9,7 +9,7 @@* */ - +#include <linux/security.h> #include <linux/list.h> #include <linux/slab.h> #include <linux/rbtree.h>
@@ -238,6 +238,10 @@ static int tracing_stat_open(struct inode *inode, struct file *file)struct seq_file *m;struct stat_session *session = inode->i_private; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; + ret =stat_seq_init (session);if (ret)return ret;
kernel/trace/trace_uprobe.c +11 -0
@@ -7,6 +7,7 @@*/ #define pr_fmt (fmt) "trace_uprobe: " fmt +#include <linux/security.h> #include <linux/ctype.h> #include <linux/module.h> #include <linux/uaccess.h>
@@ -769,6 +770,10 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { ret =dyn_events_release_all (&trace_uprobe_ops);if (ret)
@@ -818,6 +823,12 @@ static const struct seq_operations profile_seq_op = {static int profile_open (struct inode *inode,struct file *file) { +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; +return seq_open (file, &profile_seq_op); }
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 10/11 tracing/hwlat: Dont ignore outer-loop duration when calculating max_latency
In reply to Steven RostedtFrom: "Srivatsa S. Bhat (VMware)" <[email protected]>
max_latency is intended to record the maximum ever observed hardware latency, which may occur in either part of the loop (inner/outer). So we need to also consider the outer-loop sample when updating max_latency.
--- kernel/trace/trace_hwlat.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/trace/trace_hwlat.c +2 -0
@@ -256,6 +256,8 @@ static int get_sample(void)/* Keep a running maximum ever recorded hardware latency */ if (sample > tr->max_latency) tr->max_latency = sample; +if (outer_sample > tr->max_latency) + tr->max_latency = outer_sample; } out:
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 9/11 tracing/hwlat: Report total time spent in all NMIs during the sample
In reply to Steven RostedtFrom: "Srivatsa S. Bhat (VMware)" <[email protected]>
nmi_total_ts is supposed to record the total time spent in *all* NMIs that occur on the given CPU during the (active portion of the) sampling window. However, the code seems to be overwriting this variable for each NMI, thereby only recording the time spent in the most recent NMI. Fix it by accumulating the duration instead.
--- kernel/trace/trace_hwlat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_hwlat.c +1 -1
@@ -150,7 +150,7 @@ void trace_hwlat_callback(bool enter)if (enter) nmi_ts_start =time_get ();else - nmi_total_ts =time_get () - nmi_ts_start; + nmi_total_ts +=time_get () - nmi_ts_start; }if (enter)
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 5/11 tracing: Add tracing_check_open_get_tr()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Currently, most files in the tracefs directory test if tracing_disabled is set. If so, it should return -ENODEV. The tracing_disabled is called when tracing is found to be broken. Originally it was done in case the ring buffer was found to be corrupted, and we wanted to prevent reading it from crashing the kernel. But it's also called if a tracing selftest fails on boot. It's a one way switch. That is, once it is triggered, tracing is disabled until reboot.
As most tracefs files can also be used by instances in the tracefs directory, they need to be carefully done. Each instance has a trace_array associated to it, and when the instance is removed, the trace_array is freed. But if an instance is opened with a reference to the trace_array, then it requires looking up the trace_array to get its ref counter (as there could be a race with it being deleted and the open itself). Once it is found, a reference is added to prevent the instance from being removed (and the trace_array associated with it freed).
Combine the two checks (tracing_disabled and trace_array_get()) into a single helper function. This will also make it easier to add lockdown to tracefs later.
--- kernel/trace/ftrace.c | 7 +- kernel/trace/trace.c | 117 +++++++++++++++++-------------- kernel/trace/trace.h | 1 + kernel/trace/trace_dynevent.c | 4 ++ kernel/trace/trace_events.c | 10 +-- kernel/trace/trace_events_hist.c | 2 +- 6 files changed, 81 insertions(+), 60 deletions(-)
kernel/trace/ftrace.c +4 -3
@@ -3547,7 +3547,7 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,if (unlikely (ftrace_disabled))return -ENODEV ; -if (tr &&trace_array_get (tr) <0 ) +if (tracing_check_open_get_tr (tr))return -ENODEV ; iter =kzalloc (sizeof (*iter),GFP_KERNEL );
@@ -6546,8 +6546,9 @@ ftrace_pid_open(struct inode *inode, struct file *file)struct seq_file *m;int ret =0 ; -if (trace_array_get (tr) <0 ) -return -ENODEV ; + ret =tracing_check_open_get_tr (tr); +if (ret) +return ret;if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC ))
kernel/trace/trace.c +65 -52
211 lines not on this page · raw message
kernel/trace/trace.h +1 -0
@@ -338,6 +338,7 @@ extern struct mutex trace_types_lock;extern int trace_array_get (struct trace_array *tr);extern void trace_array_put (struct trace_array *tr); +extern int tracing_check_open_get_tr (struct trace_array *tr);extern int tracing_set_time_stamp_abs (struct trace_array *tr,bool abs);extern int tracing_set_clock (struct trace_array *tr,const char *clockstr);
kernel/trace/trace_dynevent.c +4 -0
@@ -174,6 +174,10 @@ static int dyn_event_open(struct inode *inode, struct file *file) {int ret; + ret =tracing_check_open_get_tr (NULL ); +if (ret) +return ret; +if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC )) { ret =dyn_events_release_all (NULL );if (ret <0 )
kernel/trace/trace_events.c +6 -4
@@ -1794,8 +1794,9 @@ ftrace_event_set_open(struct inode *inode, struct file *file)struct trace_array *tr = inode->i_private;int ret; -if (trace_array_get (tr) <0 ) -return -ENODEV ; + ret =tracing_check_open_get_tr (tr); +if (ret) +return ret;if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC ))
@@ -1814,8 +1815,9 @@ ftrace_event_set_pid_open(struct inode *inode, struct file *file)struct trace_array *tr = inode->i_private;int ret; -if (trace_array_get (tr) <0 ) -return -ENODEV ; + ret =tracing_check_open_get_tr (tr); +if (ret) +return ret;if ((file->f_mode &FMODE_WRITE ) && (file->f_flags &O_TRUNC ))
kernel/trace/trace_events_hist.c +1 -1
@@ -1680,7 +1680,7 @@ static int save_hist_vars(struct hist_trigger_data *hist_data)if (var_data)return 0 ; -if (trace_array_get (tr) <0 ) +if (tracing_check_open_get_tr (tr))return -ENODEV ; var_data =kzalloc (sizeof (*var_data),GFP_KERNEL );
Signature · 1 line
2.23.0
Steven Rostedt
maintainerPATCH 1/11 tracefs: Revert ccbd54ff54e8 ("tracefs: Restrict tracefs when the kernel is locked down")
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Running the latest kernel through my "make instances" stress tests, I triggered the following bug (with KASAN and kmemleak enabled):
mkdir invoked oom-killer:
gfp_mask=0x40cd0(GFP_KERNEL|__GFP_COMP|__GFP_RECLAIMABLE), order=0, oom_score_adj=0
CPU: 1 PID: 2229 Comm: mkdir Not tainted 5.4.0-rc2-test #325 Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014 Call Trace: dump_stack+0x64/0x8c dump_header+0x43/0x3b7 ? trace_hardirqs_on+0x48/0x4a oom_kill_process+0x68/0x2d5 out_of_memory+0x2aa/0x2d0 __alloc_pages_nodemask+0x96d/0xb67 __alloc_pages_node+0x19/0x1e alloc_slab_page+0x17/0x45 new_slab+0xd0/0x234 ___slab_alloc.constprop.86+0x18f/0x336 ? alloc_inode+0x2c/0x74 ? irq_trace+0x12/0x1e ? tracer_hardirqs_off+0x1d/0xd7 ? __slab_alloc.constprop.85+0x21/0x53 __slab_alloc.constprop.85+0x31/0x53 ? __slab_alloc.constprop.85+0x31/0x53 ? alloc_inode+0x2c/0x74 kmem_cache_alloc+0x50/0x179 ? alloc_inode+0x2c/0x74 alloc_inode+0x2c/0x74 new_inode_pseudo+0xf/0x48 new_inode+0x15/0x25 tracefs_get_inode+0x23/0x7c ? lookup_one_len+0x54/0x6c tracefs_create_file+0x53/0x11d trace_create_file+0x15/0x33 event_create_dir+0x2a3/0x34b __trace_add_new_event+0x1c/0x26 event_trace_add_tracer+0x56/0x86 trace_array_create+0x13e/0x1e1 instance_mkdir+0x8/0x17 tracefs_syscall_mkdir+0x39/0x50 ? get_dname+0x31/0x31 vfs_mkdir+0x78/0xa3 do_mkdirat+0x71/0xb0 sys_mkdir+0x19/0x1b do_fast_syscall_32+0xb0/0xed
I bisected this down to the addition of the proxy_ops into tracefs for lockdown. It appears that the allocation of the proxy_ops and then freeing it in the destroy_inode callback, is causing havoc with the memory system. Reading the documentation about destroy_inode and talking with Linus about this, this is buggy and wrong. When defining the destroy_inode() method, it is expected that the destroy_inode() will also free the inode, and not just the extra allocations done in the creation of the inode. The faulty commit causes a memory leak of the inode data structure when they are deleted.
Instead of allocating the proxy_ops (and then having to free it) the checks should be done by the open functions themselves, and not hack into the tracefs directory. First revert the tracefs updates for locked_down and then later we can add the locked_down checks in the kernel/trace files.
--- fs/tracefs/inode.c | 42 +----------------------------------------- 1 file changed, 1 insertion(+), 41 deletions(-)
fs/tracefs/inode.c +1 -41
@@ -20,7 +20,6 @@#include <linux/parser.h> #include <linux/magic.h> #include <linux/slab.h> -#include <linux/security.h> #define TRACEFS_DEFAULT_MODE 0700
@@ -28,25 +27,6 @@ static struct vfsmount *tracefs_mount;static int tracefs_mount_count;static bool tracefs_registered; -static int default_open_file (struct inode *inode,struct file *filp) -{ -struct dentry *dentry = filp->f_path.dentry; -struct file_operations *real_fops; -int ret; - -if (!dentry) -return -EINVAL ; - - ret =security_locked_down (LOCKDOWN_TRACEFS ); -if (ret) -return ret; - - real_fops = dentry->d_fsdata; -if (!real_fops->open) -return 0 ; -return real_fops->open (inode, filp); -} -static ssize_t default_read_file (struct file *file,char __user *buf,size_t count,loff_t *ppos) {
@@ -241,12 +221,6 @@ static int tracefs_apply_options(struct super_block *sb)return 0 ; } -static void tracefs_destroy_inode (struct inode *inode) -{ -if (S_ISREG (inode->i_mode)) -kfree (inode->i_fop); -} -static int tracefs_remount (struct super_block *sb,int *flags,char *data) {int err;
@@ -283,7 +257,6 @@ static int tracefs_show_options(struct seq_file *m, struct dentry *root)static const struct super_operations tracefs_super_operations = { .statfs = simple_statfs, .remount_fs = tracefs_remount, - .destroy_inode = tracefs_destroy_inode, .show_options = tracefs_show_options, };
@@ -414,7 +387,6 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops) { - struct file_operations *proxy_fops; struct dentry *dentry;struct inode *inode;
@@ -430,20 +402,8 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,if (unlikely (!inode))return failed_creating (dentry); - proxy_fops =kzalloc (sizeof (struct file_operations ),GFP_KERNEL ); -if (unlikely (!proxy_fops)) { -iput (inode); -return failed_creating (dentry); - } - -if (!fops) - fops = &tracefs_file_operations; - - dentry->d_fsdata = (void *)fops; -memcpy (proxy_fops, fops,sizeof (*proxy_fops)); - proxy_fops->open = default_open_file; inode->i_mode = mode; - inode->i_fop = proxy_fops; + inode->i_fop = fops ? fops : &tracefs_file_operations; inode->i_private = data;d_instantiate (dentry, inode);fsnotify_create (dentry->d_parent->d_inode, dentry);
Signature · 1 line
2.23.0
Steven Rostedt
maintainercover tracing: Fixes for v5.4-rc2
Petr Mladek (1):
tracing: Initialize iter->seq after zeroing in tracing_read_pipe()
Srivatsa S. Bhat (VMware) (2):
tracing/hwlat: Report total time spent in all NMIs during the sample tracing/hwlat: Don't ignore outer-loop duration when calculating max_latency
Steven Rostedt (VMware) (8):
tracefs: Revert ccbd54ff54e8 ("tracefs: Restrict tracefs when the kernel is locked down")
ftrace: Get a reference counter for the trace_array on filter files
tracing: Get trace_array reference for available_tracers files
tracing: Have trace events system open call tracing_open_generic_tr()
tracing: Add tracing_check_open_get_tr()
tracing: Add locked_down checks to the open calls of files created for tracefs
tracing: Do not create tracefs files if tracefs lockdown is in effect
recordmcount: Fix nop_mcount() function
----
fs/tracefs/inode.c | 46 ++---------- kernel/trace/ftrace.c | 55 ++++++++++---- kernel/trace/trace.c | 139 ++++++++++++++++++++++-------------- kernel/trace/trace.h | 2 + kernel/trace/trace_dynevent.c | 4 ++ kernel/trace/trace_events.c | 35 +++++---- kernel/trace/trace_events_hist.c | 13 +++- kernel/trace/trace_events_trigger.c | 8 ++- kernel/trace/trace_hwlat.c | 4 +- kernel/trace/trace_kprobe.c | 12 +++- kernel/trace/trace_printk.c | 7 ++ kernel/trace/trace_stack.c | 8 +++ kernel/trace/trace_stat.c | 6 +- kernel/trace/trace_uprobe.c | 11 +++ scripts/recordmcount.h | 5 +- 15 files changed, 223 insertions(+), 132 deletions(-)
Steven Rostedt
maintainerPATCH 3/11 tracing: Get trace_array reference for available_tracers files
From: "Steven Rostedt (VMware)" <[email protected]>
As instances may have different tracers available, we need to look at the trace_array descriptor that shows the list of the available tracers for the instance. But there's a race between opening the file and an admin deleting the instance. The trace_array_get() needs to be called before accessing the trace_array.
--- kernel/trace/trace.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
kernel/trace/trace.c +15 -2
@@ -4355,9 +4355,14 @@ static int show_traces_open(struct inode *inode, struct file *file)if (tracing_disabled)return -ENODEV ; +if (trace_array_get (tr) <0 ) +return -ENODEV ; + ret =seq_open (file, &show_traces_seq_ops); -if (ret) +if (ret) { +trace_array_put (tr);return ret; + } m = file->private_data; m->private = tr;
@@ -4365,6 +4370,14 @@ static int show_traces_open(struct inode *inode, struct file *file)return 0 ; } +static int show_traces_release (struct inode *inode,struct file *file) +{ +struct trace_array *tr = inode->i_private; + +trace_array_put (tr); +return seq_release (inode, file); +} +static ssize_t tracing_write_stub (struct file *filp,const char __user *ubuf,size_t count,loff_t *ppos)
@@ -4395,8 +4408,8 @@ static const struct file_operations tracing_fops = {static const struct file_operations show_traces_fops = { .open = show_traces_open, .read = seq_read, - .release = seq_release, .llseek = seq_lseek, + .release = show_traces_release, }; static ssize_t
Signature · 1 line
2.23.0
Re: [for-linus][PATCH 08/11] recordmcount: Fix nop_mcount() function
In reply to a message not in the archiveOne problem with quilt, is that it doesn't like non-ASCII characters (I'm looking at you Kleine-König!)
From: "Steven Rostedt (VMware)" <[email protected]>
The removal of the longjmp code in recordmcount.c mistakenly made the return of make_nop() being negative an exit of nop_mcount(). It should not exit the routine, but instead just not process that part of the code. By exiting with an error code, it would cause the update of recordmcount to fail some files which would fail the build if ftrace function tracing was enabled.
--- scripts/recordmcount.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
scripts/recordmcount.h +1 -4
@@ -389,11 +389,8 @@ static int nop_mcount(Elf_Shdr const *const relhdr, mcountsym =get_mcountsym (sym0, relp, str0);if (mcountsym ==Elf_r_sym (relp) && !is_fake_mcount (relp)) { -if (make_nop) { +if (make_nop) ret =make_nop ((void *)ehdr,_w (shdr->sh_offset) +_w (relp->r_offset)); -if (ret <0 ) -return -1 ; - }if (warn_on_notrace_sect && !once) {printf ("Section %s has mcount callers being ignored\n" , txtname);
Signature · 1 line
2.23.0
Re: [for-linus][PATCH 08/11] recordmcount: Fix nop_mcount() function
Quoted 12 lines · Steven Rostedt
One problem with quilt, is that it doesn't like non-ASCII characters (I'm looking at you Kleine-König!)
From: "Steven Rostedt (VMware)" <[email protected]>
The removal of the longjmp code in
recordmcount.cmistakenly made the return ofmake_nop()being negative an exit ofnop_mcount(). It should not exit the routine, but instead just not process that part of the code. By exiting with an error code, it would cause the update of recordmcount to fail some files which would fail the build if ftrace function tracing was enabled.
Quoted 4 lines
Reported-by Uwe Kleine-önig <[email protected]>Tested-by Uwe Kleine-König <[email protected]>
And now it seems to have problems with plain ASCII-K, but only once out of two :-|
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | http://www.pengutronix.de/ |
Re: [for-linus][PATCH 08/11] recordmcount: Fix nop_mcount() function
Quoted 5 lines · On Sun, 13 Oct 2019 21:14:07 +0200 Uwe Kleine-König…
Quoted 2 lines
Reported-by Uwe Kleine-önig <[email protected]>Tested-by Uwe Kleine-König <[email protected]>And now it seems to have problems with plain ASCII-K, but only once out of two :-|
That was me manually fixing the email.
The original looked like this:
-- Steve
Steven Rostedt
maintainerRe: [for-linus][PATCH 08/11] recordmcount: Fix nop_mcount() function
Quoted 5 lines · On Sun, 13 Oct 2019 15:53:37 -0400 Steven Rostedt <[email protected]> wrote:
The original looked like this:
Reported-by Uwe Kleine-König <[email protected]>Tested-by Uwe Kleine-König <[email protected]>
And if you are wondering what the git history has, you can see it here:
-- Steve