aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/trace
diff options
context:
space:
mode:
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>2015-05-28 13:14:51 -0400
committerSteven Rostedt <rostedt@goodmis.org>2015-05-28 16:47:01 -0400
commit289a5a25c5d0949e4d9b9ca4f334b6af5a83e090 (patch)
tree52dfb5ac2a8c5fb63a2fa84212e50b423ea04c9b /kernel/trace
parent985e871b28fc3e73ccd21cb999ce61266dc8c941 (diff)
ring-buffer: Give NMIs a chance to lock the reader_lock
Currently, if an NMI does a dump of a ring buffer, it disables all ring buffers from ever doing any writes again. This is because it wont take the locks for the cpu_buffer and this can cause corruption if it preempted a read, or a read happens on another CPU for the current cpu buffer. This is a bit overkill. First, it should at least try to take the lock, and if it fails then disable it. Also, there's no need to disable all ring buffers, even those that are unrelated to what is being read. Only disable the per cpu ring buffer that is being read if it can not get the lock for it. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Diffstat (limited to 'kernel/trace')
-rw-r--r--kernel/trace/ring_buffer.c72
1 files changed, 37 insertions, 35 deletions
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 6d6ebcea3463..e9420fdc7409 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -3859,19 +3859,36 @@ rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3859} 3859}
3860EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); 3860EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
3861 3861
3862static inline int rb_ok_to_lock(void) 3862static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
3863{ 3863{
3864 if (likely(!in_nmi())) {
3865 raw_spin_lock(&cpu_buffer->reader_lock);
3866 return true;
3867 }
3868
3864 /* 3869 /*
3865 * If an NMI die dumps out the content of the ring buffer 3870 * If an NMI die dumps out the content of the ring buffer
3866 * do not grab locks. We also permanently disable the ring 3871 * trylock must be used to prevent a deadlock if the NMI
3867 * buffer too. A one time deal is all you get from reading 3872 * preempted a task that holds the ring buffer locks. If
3868 * the ring buffer from an NMI. 3873 * we get the lock then all is fine, if not, then continue
3874 * to do the read, but this can corrupt the ring buffer,
3875 * so it must be permanently disabled from future writes.
3876 * Reading from NMI is a oneshot deal.
3869 */ 3877 */
3870 if (likely(!in_nmi())) 3878 if (raw_spin_trylock(&cpu_buffer->reader_lock))
3871 return 1; 3879 return true;
3872 3880
3873 tracing_off_permanent(); 3881 /* Continue without locking, but disable the ring buffer */
3874 return 0; 3882 atomic_inc(&cpu_buffer->record_disabled);
3883 return false;
3884}
3885
3886static inline void
3887rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
3888{
3889 if (likely(locked))
3890 raw_spin_unlock(&cpu_buffer->reader_lock);
3891 return;
3875} 3892}
3876 3893
3877/** 3894/**
@@ -3891,21 +3908,18 @@ ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3891 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; 3908 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3892 struct ring_buffer_event *event; 3909 struct ring_buffer_event *event;
3893 unsigned long flags; 3910 unsigned long flags;
3894 int dolock; 3911 bool dolock;
3895 3912
3896 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 3913 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3897 return NULL; 3914 return NULL;
3898 3915
3899 dolock = rb_ok_to_lock();
3900 again: 3916 again:
3901 local_irq_save(flags); 3917 local_irq_save(flags);
3902 if (dolock) 3918 dolock = rb_reader_lock(cpu_buffer);
3903 raw_spin_lock(&cpu_buffer->reader_lock);
3904 event = rb_buffer_peek(cpu_buffer, ts, lost_events); 3919 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3905 if (event && event->type_len == RINGBUF_TYPE_PADDING) 3920 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3906 rb_advance_reader(cpu_buffer); 3921 rb_advance_reader(cpu_buffer);
3907 if (dolock) 3922 rb_reader_unlock(cpu_buffer, dolock);
3908 raw_spin_unlock(&cpu_buffer->reader_lock);
3909 local_irq_restore(flags); 3923 local_irq_restore(flags);
3910 3924
3911 if (event && event->type_len == RINGBUF_TYPE_PADDING) 3925 if (event && event->type_len == RINGBUF_TYPE_PADDING)
@@ -3958,9 +3972,7 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3958 struct ring_buffer_per_cpu *cpu_buffer; 3972 struct ring_buffer_per_cpu *cpu_buffer;
3959 struct ring_buffer_event *event = NULL; 3973 struct ring_buffer_event *event = NULL;
3960 unsigned long flags; 3974 unsigned long flags;
3961 int dolock; 3975 bool dolock;
3962
3963 dolock = rb_ok_to_lock();
3964 3976
3965 again: 3977 again:
3966 /* might be called in atomic */ 3978 /* might be called in atomic */
@@ -3971,8 +3983,7 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3971 3983
3972 cpu_buffer = buffer->buffers[cpu]; 3984 cpu_buffer = buffer->buffers[cpu];
3973 local_irq_save(flags); 3985 local_irq_save(flags);
3974 if (dolock) 3986 dolock = rb_reader_lock(cpu_buffer);
3975 raw_spin_lock(&cpu_buffer->reader_lock);
3976 3987
3977 event = rb_buffer_peek(cpu_buffer, ts, lost_events); 3988 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3978 if (event) { 3989 if (event) {
@@ -3980,8 +3991,7 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3980 rb_advance_reader(cpu_buffer); 3991 rb_advance_reader(cpu_buffer);
3981 } 3992 }
3982 3993
3983 if (dolock) 3994 rb_reader_unlock(cpu_buffer, dolock);
3984 raw_spin_unlock(&cpu_buffer->reader_lock);
3985 local_irq_restore(flags); 3995 local_irq_restore(flags);
3986 3996
3987 out: 3997 out:
@@ -4262,21 +4272,17 @@ int ring_buffer_empty(struct ring_buffer *buffer)
4262{ 4272{
4263 struct ring_buffer_per_cpu *cpu_buffer; 4273 struct ring_buffer_per_cpu *cpu_buffer;
4264 unsigned long flags; 4274 unsigned long flags;
4265 int dolock; 4275 bool dolock;
4266 int cpu; 4276 int cpu;
4267 int ret; 4277 int ret;
4268 4278
4269 dolock = rb_ok_to_lock();
4270
4271 /* yes this is racy, but if you don't like the race, lock the buffer */ 4279 /* yes this is racy, but if you don't like the race, lock the buffer */
4272 for_each_buffer_cpu(buffer, cpu) { 4280 for_each_buffer_cpu(buffer, cpu) {
4273 cpu_buffer = buffer->buffers[cpu]; 4281 cpu_buffer = buffer->buffers[cpu];
4274 local_irq_save(flags); 4282 local_irq_save(flags);
4275 if (dolock) 4283 dolock = rb_reader_lock(cpu_buffer);
4276 raw_spin_lock(&cpu_buffer->reader_lock);
4277 ret = rb_per_cpu_empty(cpu_buffer); 4284 ret = rb_per_cpu_empty(cpu_buffer);
4278 if (dolock) 4285 rb_reader_unlock(cpu_buffer, dolock);
4279 raw_spin_unlock(&cpu_buffer->reader_lock);
4280 local_irq_restore(flags); 4286 local_irq_restore(flags);
4281 4287
4282 if (!ret) 4288 if (!ret)
@@ -4296,21 +4302,17 @@ int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4296{ 4302{
4297 struct ring_buffer_per_cpu *cpu_buffer; 4303 struct ring_buffer_per_cpu *cpu_buffer;
4298 unsigned long flags; 4304 unsigned long flags;
4299 int dolock; 4305 bool dolock;
4300 int ret; 4306 int ret;
4301 4307
4302 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 4308 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4303 return 1; 4309 return 1;
4304 4310
4305 dolock = rb_ok_to_lock();
4306
4307 cpu_buffer = buffer->buffers[cpu]; 4311 cpu_buffer = buffer->buffers[cpu];
4308 local_irq_save(flags); 4312 local_irq_save(flags);
4309 if (dolock) 4313 dolock = rb_reader_lock(cpu_buffer);
4310 raw_spin_lock(&cpu_buffer->reader_lock);
4311 ret = rb_per_cpu_empty(cpu_buffer); 4314 ret = rb_per_cpu_empty(cpu_buffer);
4312 if (dolock) 4315 rb_reader_unlock(cpu_buffer, dolock);
4313 raw_spin_unlock(&cpu_buffer->reader_lock);
4314 local_irq_restore(flags); 4316 local_irq_restore(flags);
4315 4317
4316 return ret; 4318 return ret;