Skip to content

Commit ca846a7

Browse files
rostedtJiri Slaby
authored andcommitted
ring-buffer: Check if buffer exists before polling
commit 8b8b368 upstream. 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@oracle.com Reported-by: Sasha Levin <sasha.levin@oracle.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 378005b commit ca846a7

4 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎include/linux/ring_buffer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *k
9797
__ring_buffer_alloc((size), (flags), &__key); \
9898
})
9999

100-
void ring_buffer_wait(struct ring_buffer *buffer, int cpu);
100+
int ring_buffer_wait(struct ring_buffer *buffer, int cpu);
101101
int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
102102
struct file *filp, poll_table *poll_table);
103103

‎kernel/trace/ring_buffer.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ static void rb_wake_up_waiters(struct irq_work *work)
543543
* as data is added to any of the @buffer's cpu buffers. Otherwise
544544
* it will wait for data to be added to a specific cpu buffer.
545545
*/
546-
void ring_buffer_wait(struct ring_buffer *buffer, int cpu)
546+
int ring_buffer_wait(struct ring_buffer *buffer, int cpu)
547547
{
548548
struct ring_buffer_per_cpu *cpu_buffer;
549549
DEFINE_WAIT(wait);
@@ -557,6 +557,8 @@ void ring_buffer_wait(struct ring_buffer *buffer, int cpu)
557557
if (cpu == RING_BUFFER_ALL_CPUS)
558558
work = &buffer->irq_work;
559559
else {
560+
if (!cpumask_test_cpu(cpu, buffer->cpumask))
561+
return -ENODEV;
560562
cpu_buffer = buffer->buffers[cpu];
561563
work = &cpu_buffer->irq_work;
562564
}
@@ -591,6 +593,7 @@ void ring_buffer_wait(struct ring_buffer *buffer, int cpu)
591593
schedule();
592594

593595
finish_wait(&work->waiters, &wait);
596+
return 0;
594597
}
595598

596599
/**

‎kernel/trace/trace.c‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,13 +1044,13 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
10441044
}
10451045
#endif /* CONFIG_TRACER_MAX_TRACE */
10461046

1047-
static void default_wait_pipe(struct trace_iterator *iter)
1047+
static int default_wait_pipe(struct trace_iterator *iter)
10481048
{
10491049
/* Iterators are static, they should be filled or empty */
10501050
if (trace_buffer_iter(iter, iter->cpu_file))
1051-
return;
1051+
return 0;
10521052

1053-
ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1053+
return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
10541054
}
10551055

10561056
#ifdef CONFIG_FTRACE_STARTUP_TEST
@@ -4059,17 +4059,19 @@ tracing_poll_pipe(struct file *filp, poll_table *poll_table)
40594059
*
40604060
* Anyway, this is really very primitive wakeup.
40614061
*/
4062-
void poll_wait_pipe(struct trace_iterator *iter)
4062+
int poll_wait_pipe(struct trace_iterator *iter)
40634063
{
40644064
set_current_state(TASK_INTERRUPTIBLE);
40654065
/* sleep for 100 msecs, and try again. */
40664066
schedule_timeout(HZ / 10);
4067+
return 0;
40674068
}
40684069

40694070
/* Must be called with trace_types_lock mutex held. */
40704071
static int tracing_wait_pipe(struct file *filp)
40714072
{
40724073
struct trace_iterator *iter = filp->private_data;
4074+
int ret;
40734075

40744076
while (trace_empty(iter)) {
40754077

@@ -4079,10 +4081,13 @@ static int tracing_wait_pipe(struct file *filp)
40794081

40804082
mutex_unlock(&iter->mutex);
40814083

4082-
iter->trace->wait_pipe(iter);
4084+
ret = iter->trace->wait_pipe(iter);
40834085

40844086
mutex_lock(&iter->mutex);
40854087

4088+
if (ret)
4089+
return ret;
4090+
40864091
if (signal_pending(current))
40874092
return -EINTR;
40884093

@@ -5016,8 +5021,12 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,
50165021
goto out_unlock;
50175022
}
50185023
mutex_unlock(&trace_types_lock);
5019-
iter->trace->wait_pipe(iter);
5024+
ret = iter->trace->wait_pipe(iter);
50205025
mutex_lock(&trace_types_lock);
5026+
if (ret) {
5027+
size = ret;
5028+
goto out_unlock;
5029+
}
50215030
if (signal_pending(current)) {
50225031
size = -EINTR;
50235032
goto out_unlock;
@@ -5229,8 +5238,10 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
52295238
goto out;
52305239
}
52315240
mutex_unlock(&trace_types_lock);
5232-
iter->trace->wait_pipe(iter);
5241+
ret = iter->trace->wait_pipe(iter);
52335242
mutex_lock(&trace_types_lock);
5243+
if (ret)
5244+
goto out;
52345245
if (signal_pending(current)) {
52355246
ret = -EINTR;
52365247
goto out;

‎kernel/trace/trace.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ struct tracer {
334334
void (*stop)(struct trace_array *tr);
335335
void (*open)(struct trace_iterator *iter);
336336
void (*pipe_open)(struct trace_iterator *iter);
337-
void (*wait_pipe)(struct trace_iterator *iter);
337+
int (*wait_pipe)(struct trace_iterator *iter);
338338
void (*close)(struct trace_iterator *iter);
339339
void (*pipe_close)(struct trace_iterator *iter);
340340
ssize_t (*read)(struct trace_iterator *iter,
@@ -549,7 +549,7 @@ void trace_init_global_iter(struct trace_iterator *iter);
549549

550550
void tracing_iter_reset(struct trace_iterator *iter, int cpu);
551551

552-
void poll_wait_pipe(struct trace_iterator *iter);
552+
int poll_wait_pipe(struct trace_iterator *iter);
553553

554554
void tracing_sched_switch_trace(struct trace_array *tr,
555555
struct task_struct *prev,

0 commit comments

Comments
 (0)