tracing: Fix infinite loops when waiting for full page from buffer
Subsystems (2)
Steven Rostedt
maintainerLinus,
Rabin Vincent found a way that tracing could cause an infinite loop in the kernel. The splice logic wants a full page from the ring buffer but the ring_buffer_wait() returns when there's any data in the ring buffer. The splice code would then continue the loop waiting for a full page. But if a full page never happens, the splice code will never sleep and just continue to loop.
There's another case that Rabin fixed that could loop if there's no memory and kmalloc() constantly returns NULL.
Please pull the latest trace-fixes-v3.18-rc4 tree, which can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
trace-fixes-v3.18-rc4
Tag SHA1: 6c35a7a8ec643089be5869935d87e47a99d36ac1 Head SHA1: 07906da78810dce5fd35b9449358c9208c693dca
Rabin Vincent (2):
tracing: Do not busy wait in buffer splice
tracing: Do not risk busy looping in buffer splice
----
include/linux/ring_buffer.h | 2 +- kernel/trace/ring_buffer.c | 81 ++++++++++++++++++++++++++++++--------------- kernel/trace/trace.c | 33 +++++++++--------- 3 files changed, 70 insertions(+), 46 deletions(-)
---------------------------
include/linux/ring_buffer.h +1 -1
@@ -97,7 +97,7 @@ __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *k__ring_buffer_alloc ((size), (flags), &__key); \ }) -int ring_buffer_wait (struct ring_buffer *buffer,int cpu); +int ring_buffer_wait (struct ring_buffer *buffer,int cpu,bool full);int ring_buffer_poll_wait (struct ring_buffer *buffer,int cpu,struct file *filp,poll_table *poll_table);
kernel/trace/ring_buffer.c +54 -27
106 lines not on this page · raw message
kernel/trace/trace.c +15 -18
@@ -1076,13 +1076,14 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) }#endif /* CONFIG_TRACER_MAX_TRACE */ -static int wait_on_pipe (struct trace_iterator *iter) +static int wait_on_pipe (struct trace_iterator *iter,bool full) {/* Iterators are static, they should be filled or empty */ if (trace_buffer_iter (iter, iter->cpu_file))return 0 ; -return ring_buffer_wait (iter->trace_buffer->buffer, iter->cpu_file); +return ring_buffer_wait (iter->trace_buffer->buffer, iter->cpu_file, + full); }#ifdef CONFIG_FTRACE_STARTUP_TEST
@@ -4434,15 +4435,12 @@ static int tracing_wait_pipe(struct file *filp)mutex_unlock (&iter->mutex); - ret =wait_on_pipe (iter); + ret =wait_on_pipe (iter, false);mutex_lock (&iter->mutex);if (ret)return ret; - -if (signal_pending (current)) -return -EINTR ; }return 1 ;
@@ -5372,16 +5370,12 @@ tracing_buffers_read(struct file *filp, char __user *ubuf, goto out_unlock; }mutex_unlock (&trace_types_lock); - ret =wait_on_pipe (iter); + ret =wait_on_pipe (iter, false);mutex_lock (&trace_types_lock);if (ret) { size = ret; goto out_unlock; } -if (signal_pending (current)) { - size = -EINTR ; - goto out_unlock; - } goto again; } size =0 ;
@@ -5500,7 +5494,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos, };struct buffer_ref *ref;int entries, size, i; -ssize_t ret; +ssize_t ret =0 ;mutex_lock (&trace_types_lock);
@@ -5538,13 +5532,16 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,int r; ref =kzalloc (sizeof (*ref),GFP_KERNEL ); -if (!ref) +if (!ref) { + ret = -ENOMEM ;break ; + } ref->ref =1 ; ref->buffer = iter->trace_buffer->buffer; ref->page =ring_buffer_alloc_read_page (ref->buffer, iter->cpu_file);if (!ref->page) { + ret = -ENOMEM ;kfree (ref);break ; }
@@ -5582,19 +5579,19 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,/* did we read anything? */ if (!spd.nr_pages) { +if (ret) + goto out; +if ((file->f_flags &O_NONBLOCK ) || (flags &SPLICE_F_NONBLOCK )) { ret = -EAGAIN ; goto out; }mutex_unlock (&trace_types_lock); - ret =wait_on_pipe (iter); + ret =wait_on_pipe (iter, true);mutex_lock (&trace_types_lock);if (ret) goto out; -if (signal_pending (current)) { - ret = -EINTR ; - goto out; - } + goto again; }