Skip to content

Commit b0f269b

Browse files
Qing WangSasha Levin
authored andcommitted
tracing: Fix WARN_ON in tracing_buffers_mmap_close
commit e39bb9e upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a3f88e3 commit b0f269b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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
@@ -7292,6 +7292,27 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu,
72927292
return err;
72937293
}
72947294

7295+
/*
7296+
* This is called when a VMA is duplicated (e.g., on fork()) to increment
7297+
* the user_mapped counter without remapping pages.
7298+
*/
7299+
void ring_buffer_map_dup(struct trace_buffer *buffer, int cpu)
7300+
{
7301+
struct ring_buffer_per_cpu *cpu_buffer;
7302+
7303+
if (WARN_ON(!cpumask_test_cpu(cpu, buffer->cpumask)))
7304+
return;
7305+
7306+
cpu_buffer = buffer->buffers[cpu];
7307+
7308+
guard(mutex)(&cpu_buffer->mapping_lock);
7309+
7310+
if (cpu_buffer->user_mapped)
7311+
__rb_inc_dec_mapped(cpu_buffer, true);
7312+
else
7313+
WARN(1, "Unexpected buffer stat, it should be mapped");
7314+
}
7315+
72957316
int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
72967317
{
72977318
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
@@ -8999,6 +8999,18 @@ static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
89998999
static inline void put_snapshot_map(struct trace_array *tr) { }
90009000
#endif
90019001

9002+
/*
9003+
* This is called when a VMA is duplicated (e.g., on fork()) to increment
9004+
* the user_mapped counter without remapping pages.
9005+
*/
9006+
static void tracing_buffers_mmap_open(struct vm_area_struct *vma)
9007+
{
9008+
struct ftrace_buffer_info *info = vma->vm_file->private_data;
9009+
struct trace_iterator *iter = &info->iter;
9010+
9011+
ring_buffer_map_dup(iter->array_buffer->buffer, iter->cpu_file);
9012+
}
9013+
90029014
static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
90039015
{
90049016
struct ftrace_buffer_info *info = vma->vm_file->private_data;
@@ -9018,6 +9030,7 @@ static int tracing_buffers_may_split(struct vm_area_struct *vma, unsigned long a
90189030
}
90199031

90209032
static const struct vm_operations_struct tracing_buffers_vmops = {
9033+
.open = tracing_buffers_mmap_open,
90219034
.close = tracing_buffers_mmap_close,
90229035
.may_split = tracing_buffers_may_split,
90239036
};

0 commit comments

Comments
 (0)