History

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.

Qing Wang committed 8 months ago

Commit e39bb9e1 parent a5dd6f53 files · +35 −0Browse files

Unified · Split

3 files
  1. include/linux/ring_buffer.h +1 −0
  2. kernel/trace/ring_buffer.c +21 −0
  3. kernel/trace/trace.c +13 −0
include/linux/ring_buffer.h+1−0
@@ -248,6 +248,7 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node); · 247 unmodified lines
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+21−0
@@ -7310,6 +7310,27 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu, · 7309 unmodified lines
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+13−0
@@ -8213,6 +8213,18 @@ static inline int get_snapshot_map(struct trace_array *tr) { return 0; } · 8212 unmodified lines
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 · 13 unmodified lines
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 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t