On Mon, 07 Jul 2014 13:00:06 -0700 <gregkh@xxxxxxxxxxxxxxxxxxx> wrote: > > The patch below does not apply to the 3.15-stable tree. > If someone wants it applied there, or to any other stable or longterm > tree, then please email the backport, including the original git commit > id to <stable@xxxxxxxxxxxxxxx>. > > thanks, > > greg k-h > Here's the fix: -- Steve ------------------ original commit in Linus's tree ------------------ >From 8b8b36834d0fff67fc8668093f4312dd04dcf21d Mon Sep 17 00:00:00 2001 From: "Steven Rostedt (Red Hat)" <rostedt@xxxxxxxxxxx> Date: Tue, 10 Jun 2014 09:46:00 -0400 Subject: [PATCH] ring-buffer: Check if buffer exists before polling The per_cpu buffers are created one per possible CPU. But these do not mean that those CPUs are online, nor do they even exist. With the addition of the ring buffer polling, it assumes that the caller polls on an existing buffer. But this is not the case if the user reads trace_pipe from a CPU that does not exist, and this causes the kernel to crash. Simple fix is to check the cpu against buffer bitmask against to see if the buffer was allocated or not and return -ENODEV if it is not. More updates were done to pass the -ENODEV back up to userspace. Link: http://lkml.kernel.org/r/5393DB61.6060707@xxxxxxxxxx Reported-by: Sasha Levin <sasha.levin@xxxxxxxxxx> Cc: stable@xxxxxxxxxxxxxxx # 3.10+ Signed-off-by: Steven Rostedt <rostedt@xxxxxxxxxxx> --- include/linux/ring_buffer.h | 2 +- kernel/trace/ring_buffer.c | 5 ++++- kernel/trace/trace.c | 22 ++++++++++++++++------ kernel/trace/trace.h | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) Index: linux-trace.git/include/linux/ring_buffer.h =================================================================== --- linux-trace.git.orig/include/linux/ring_buffer.h 2014-07-08 12:55:38.008867576 -0400 +++ linux-trace.git/include/linux/ring_buffer.h 2014-07-08 12:55:39.595851653 -0400 @@ -97,7 +97,7 @@ __ring_buffer_alloc((size), (flags), &__key); \ }) -void ring_buffer_wait(struct ring_buffer *buffer, int cpu); +int ring_buffer_wait(struct ring_buffer *buffer, int cpu); int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, struct file *filp, poll_table *poll_table); Index: linux-trace.git/kernel/trace/ring_buffer.c =================================================================== --- linux-trace.git.orig/kernel/trace/ring_buffer.c 2014-07-08 12:55:38.010867556 -0400 +++ linux-trace.git/kernel/trace/ring_buffer.c 2014-07-08 12:55:39.597851633 -0400 @@ -543,7 +543,7 @@ * as data is added to any of the @buffer's cpu buffers. Otherwise * it will wait for data to be added to a specific cpu buffer. */ -void ring_buffer_wait(struct ring_buffer *buffer, int cpu) +int ring_buffer_wait(struct ring_buffer *buffer, int cpu) { struct ring_buffer_per_cpu *cpu_buffer; DEFINE_WAIT(wait); @@ -557,6 +557,8 @@ if (cpu == RING_BUFFER_ALL_CPUS) work = &buffer->irq_work; else { + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return -ENODEV; cpu_buffer = buffer->buffers[cpu]; work = &cpu_buffer->irq_work; } @@ -591,6 +593,7 @@ schedule(); finish_wait(&work->waiters, &wait); + return 0; } /** Index: linux-trace.git/kernel/trace/trace.c =================================================================== --- linux-trace.git.orig/kernel/trace/trace.c 2014-07-08 12:55:38.014867516 -0400 +++ linux-trace.git/kernel/trace/trace.c 2014-07-08 13:04:22.095989414 -0400 @@ -1103,13 +1103,13 @@ } #endif /* CONFIG_TRACER_MAX_TRACE */ -static void default_wait_pipe(struct trace_iterator *iter) +static int default_wait_pipe(struct trace_iterator *iter) { /* Iterators are static, they should be filled or empty */ if (trace_buffer_iter(iter, iter->cpu_file)) - return; + return 0; - ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file); + return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file); } #ifdef CONFIG_FTRACE_STARTUP_TEST @@ -4249,6 +4249,7 @@ static int tracing_wait_pipe(struct file *filp) { struct trace_iterator *iter = filp->private_data; + int ret; while (trace_empty(iter)) { @@ -4258,10 +4259,13 @@ mutex_unlock(&iter->mutex); - iter->trace->wait_pipe(iter); + ret = iter->trace->wait_pipe(iter); mutex_lock(&iter->mutex); + if (ret) + return ret; + if (signal_pending(current)) return -EINTR; @@ -5198,8 +5202,12 @@ goto out_unlock; } mutex_unlock(&trace_types_lock); - iter->trace->wait_pipe(iter); + ret = iter->trace->wait_pipe(iter); mutex_lock(&trace_types_lock); + if (ret) { + size = ret; + goto out_unlock; + } if (signal_pending(current)) { size = -EINTR; goto out_unlock; @@ -5409,8 +5417,10 @@ goto out; } mutex_unlock(&trace_types_lock); - iter->trace->wait_pipe(iter); + ret = iter->trace->wait_pipe(iter); mutex_lock(&trace_types_lock); + if (ret) + goto out; if (signal_pending(current)) { ret = -EINTR; goto out; Index: linux-trace.git/kernel/trace/trace.h =================================================================== --- linux-trace.git.orig/kernel/trace/trace.h 2014-07-08 12:54:11.661733971 -0400 +++ linux-trace.git/kernel/trace/trace.h 2014-07-08 12:58:33.485106872 -0400 @@ -342,7 +342,7 @@ void (*stop)(struct trace_array *tr); void (*open)(struct trace_iterator *iter); void (*pipe_open)(struct trace_iterator *iter); - void (*wait_pipe)(struct trace_iterator *iter); + int (*wait_pipe)(struct trace_iterator *iter); void (*close)(struct trace_iterator *iter); void (*pipe_close)(struct trace_iterator *iter); ssize_t (*read)(struct trace_iterator *iter, -- To unsubscribe from this list: send the line "unsubscribe stable" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html