Skip to content

Commit e39bb9e

Browse files
Qing Wangrostedt
authored andcommitted
tracing: Fix WARN_ON in tracing_buffers_mmap_close
When a process forks, the child process copies the parent's VMAs but the user_mapped reference count is not incremented. As a result, when both the parent and child processes exit, tracing_buffers_mmap_close() is called twice. On the second call, user_mapped is already 0, causing the function to return -ENODEV and triggering a WARN_ON. Normally, this isn't an issue as the memory is mapped with VM_DONTCOPY set. But this is only a hint, and the application can call madvise(MADVISE_DOFORK) which resets the VM_DONTCOPY flag. When the application does that, it can trigger this issue on fork. Fix it by incrementing the user_mapped reference count without re-mapping the pages in the VMA's open callback. Cc: stable@vger.kernel.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Vincent Donnefort <vdonnefort@google.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Link: https://patch.msgid.link/20260227025842.1085206-1-wangqing7171@gmail.com Fixes: cf9f0f7 ("tracing: Allow user-space mapping of the ring-buffer") Reported-by: syzbot+3b5dd2030fe08afdf65d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3b5dd2030fe08afdf65d Tested-by: syzbot+3b5dd2030fe08afdf65d@syzkaller.appspotmail.com Signed-off-by: Qing Wang <wangqing7171@gmail.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent a5dd6f5 commit e39bb9e

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

include/linux/ring_buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node);
248248

249249
int ring_buffer_map(struct trace_buffer *buffer, int cpu,
250250
struct vm_area_struct *vma);
251+
void ring_buffer_map_dup(struct trace_buffer *buffer, int cpu);
251252
int ring_buffer_unmap(struct trace_buffer *buffer, int cpu);
252253
int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu);
253254
#endif /* _LINUX_RING_BUFFER_H */

kernel/trace/ring_buffer.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7310,6 +7310,27 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu,
73107310
return err;
73117311
}
73127312

7313+
/*
7314+
* This is called when a VMA is duplicated (e.g., on fork()) to increment
7315+
* the user_mapped counter without remapping pages.
7316+
*/
7317+
void ring_buffer_map_dup(struct trace_buffer *buffer, int cpu)
7318+
{
7319+
struct ring_buffer_per_cpu *cpu_buffer;
7320+
7321+
if (WARN_ON(!cpumask_test_cpu(cpu, buffer->cpumask)))
7322+
return;
7323+
7324+
cpu_buffer = buffer->buffers[cpu];
7325+
7326+
guard(mutex)(&cpu_buffer->mapping_lock);
7327+
7328+
if (cpu_buffer->user_mapped)
7329+
__rb_inc_dec_mapped(cpu_buffer, true);
7330+
else
7331+
WARN(1, "Unexpected buffer stat, it should be mapped");
7332+
}
7333+
73137334
int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
73147335
{
73157336
struct ring_buffer_per_cpu *cpu_buffer;

kernel/trace/trace.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8213,6 +8213,18 @@ static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
82138213
static inline void put_snapshot_map(struct trace_array *tr) { }
82148214
#endif
82158215

8216+
/*
8217+
* This is called when a VMA is duplicated (e.g., on fork()) to increment
8218+
* the user_mapped counter without remapping pages.
8219+
*/
8220+
static void tracing_buffers_mmap_open(struct vm_area_struct *vma)
8221+
{
8222+
struct ftrace_buffer_info *info = vma->vm_file->private_data;
8223+
struct trace_iterator *iter = &info->iter;
8224+
8225+
ring_buffer_map_dup(iter->array_buffer->buffer, iter->cpu_file);
8226+
}
8227+
82168228
static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
82178229
{
82188230
struct ftrace_buffer_info *info = vma->vm_file->private_data;
@@ -8232,6 +8244,7 @@ static int tracing_buffers_may_split(struct vm_area_struct *vma, unsigned long a
82328244
}
82338245

82348246
static const struct vm_operations_struct tracing_buffers_vmops = {
8247+
.open = tracing_buffers_mmap_open,
82358248
.close = tracing_buffers_mmap_close,
82368249
.may_split = tracing_buffers_may_split,
82378250
};

0 commit comments

Comments
 (0)