Re: [PATCH] tracing: Mitigate possible softlockup in __tracing_open()

From: Zheng Yejian
Date: Mon Aug 26 2024 - 21:30:07 EST


On 2024/8/26 22:35, Steven Rostedt wrote:
On Sat, 24 Aug 2024 11:03:43 +0800
Zheng Yejian <zhengyejian@xxxxxxxxxxxxxxx> wrote:

In __tracing_open(), when max latency tracers took place on the cpu,
the time start of its buffer would be updated, then event entries with
timestamps being earlier than start of the buffer would be skipped
(see tracing_iter_reset()).

Softlockup will occur if the kernel is non-preemptible and too many
entries were skipped in the loop that reset every cpu buffer, so add
cond_resched() to avoid it.

Signed-off-by: Zheng Yejian <zhengyejian@xxxxxxxxxxxxxxx>
---
kernel/trace/trace.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index ebe7ce2f5f4a..88faa95b457b 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -4706,6 +4706,15 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)
for_each_tracing_cpu(cpu) {
ring_buffer_read_start(iter->buffer_iter[cpu]);
tracing_iter_reset(iter, cpu);
+ /*
+ * When max latency tracers took place on the cpu, the time start
+ * of its buffer would be updated, then event entries with timestamps
+ * being earlier than start of the buffer would be skipped
+ * (see tracing_iter_reset()). Softlockup will occur if the kernel
+ * is non-preemptible and too many entries were skipped in the loop,
+ * so add cond_resched() to mitigate it.
+ */
+ cond_resched();

This is the wrong place to put this. If the problem is with
tracing_iter_reset(), then add it there.

while (ring_buffer_iter_peek(buf_iter, &ts)) {
if (ts >= iter->array_buffer->time_start)
break;
entries++;
ring_buffer_iter_advance(buf_iter);
/* This could be a big loop */
cond_resched();

Do resched check every event peek, I'm afraid there will
be too many 're-schedule'.

}

-- Steve



}
} else {
cpu = iter->cpu_file;


--
Thanks,
Zheng Yejian