Introducing trace buffer mapping by user-space
Subsystems (4)
Vincent Donnefort
maintainerv14 cover Introducing trace buffer mapping by user-space
The tracing ring-buffers can be stored on disk or sent to network without any copy via splice. However the later doesn't allow real time processing of the traces. A solution is to give userspace direct access to the ring-buffer pages via a mapping. An application can now become a consumer of the ring-buffer, in a similar fashion to what trace_pipe offers.
Support for this new feature can already be found in libtracefs from version 1.8, when built with EXTRA_CFLAGS=-DFORCE_MMAP_ENABLE.
Vincent
v13 -> v14:
* All cpu_buffer->mapped readers use READ_ONCE (except for swap_cpu)
* on unmap, sync meta-page teardown with the reader_lock instead of the synchronize_rcu.
* Add a dedicated spinlock for trace_array ->snapshot and ->mapped. (intends to fix a lockdep issue)
* Add kerneldoc for flags and Reserved fields.
* Add kselftest for snapshot/map mutual exclusion.
v12 -> v13:
* Swap subbufs_{touched,lost} for Reserved fields.
* Add a flag field in the meta-page.
* Fix CONFIG_TRACER_MAX_TRACE.
* Rebase on top of trace/urgent.
* Add a comment for try_unregister_trigger()
v11 -> v12:
* Fix code sample mmap bug.
* Add logging in sample code.
* Reset tracer in selftest.
* Add a refcount for the snapshot users.
* Prevent mapping when there are snapshot users and vice versa.
* Refine the meta-page.
* Fix types in the meta-page.
* Collect Reviewed-by.
v10 -> v11:
* Add Documentation and code sample.
* Add a selftest.
* Move all the update to the meta-page into a single
rb_update_meta_page().
* rb_update_meta_page() is now called from ring_buffer_map_get_reader() to fix NOBLOCK callers.
* kerneldoc for struct trace_meta_page.
* Add a patch to zero all the ring-buffer allocations.
v9 -> v10:
* Refactor rb_update_meta_page()
* In-loop declaration for foreach_subbuf_page()
* Check for cpu_buffer->mapped overflow
v8 -> v9:
* Fix the unlock path in ring_buffer_map()
* Fix cpu_buffer cast with rb_work_rq->is_cpu_buffer
* Rebase on linux-trace/for-next (3cb3091138ca0921c4569bcf7ffa062519639b6a)
v7 -> v8:
* Drop the subbufs renaming into bpages
* Use subbuf as a name when relevant
v6 -> v7:
* Rebase onto lore.kernel.org/lkml/[email protected]/
* Support for subbufs
* Rename subbufs into bpages
v5 -> v6:
* Rebase on next-20230802.
* (unsigned long) -> (void *) cast for virt_to_page().
* Add a wait for the GET_READER_PAGE ioctl.
* Move writer fields update (overrun/pages_lost/entries/pages_touched) in the irq_work.
* Rearrange id in struct buffer_page.
* Rearrange the meta-page.
* ring_buffer_meta_page -> trace_buffer_meta_page.
* Add meta_struct_len into the meta-page.
v4 -> v5:
* Trivial rebase onto 6.5-rc3 (previously 6.4-rc3)
v3 -> v4:
* Add to the meta-page:
- pages_lost / pages_read (allow to compute how full is the ring-buffer)
- read (allow to compute how many entries can be read)
- A reader_page struct.
* Rename ring_buffer_meta_header -> ring_buffer_meta
* Rename ring_buffer_get_reader_page -> ring_buffer_map_get_reader_page
* Properly consume events on ring_buffer_map_get_reader_page() with rb_advance_reader().
v2 -> v3:
* Remove data page list (for non-consuming read)
** Implies removing order > 0 meta-page
* Add a new meta page field ->read
* Rename ring_buffer_meta_page_header into ring_buffer_meta_header
v1 -> v2:
* Hide data_pages from the userspace struct
* Fix META_PAGE_MAX_PAGES
* Support for order > 0 meta-page
* Add missing page->mapping.
Vincent Donnefort (6):
ring-buffer: Zero ring-buffer sub-buffers
ring-buffer: Introducing ring-buffer mapping functions
tracing: Add snapshot refcount
tracing: Allow user-space mapping of the ring-buffer
Documentation: tracing: Add ring-buffer mapping ring-buffer/selftest: Add ring-buffer mapping test
Documentation/trace/index.rst | 1 + Documentation/trace/ring-buffer-map.rst | 104 ++++++ include/linux/ring_buffer.h | 7 + include/uapi/linux/trace_mmap.h | 48 +++ kernel/trace/ring_buffer.c | 344 +++++++++++++++++- kernel/trace/trace.c | 221 +++++++++-- kernel/trace/trace.h | 9 +- kernel/trace/trace_events_trigger.c | 58 ++- tools/testing/selftests/ring-buffer/Makefile | 8 + tools/testing/selftests/ring-buffer/config | 2 + .../testing/selftests/ring-buffer/map_test.c | 273 ++++++++++++++ 11 files changed, 1029 insertions(+), 46 deletions(-) create mode 100644 Documentation/trace/ring-buffer-map.rst create mode 100644 include/uapi/linux/trace_mmap.h create mode 100644 tools/testing/selftests/ring-buffer/Makefile create mode 100644 tools/testing/selftests/ring-buffer/config create mode 100644 tools/testing/selftests/ring-buffer/map_test.c
base-commit: e2412e51fdea837b50ce31fea8e5dfc885237f3a
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Vincent Donnefort
maintainerPATCH v14 1/6 ring-buffer: Zero ring-buffer sub-buffers
In preparation for the ring-buffer memory mapping where each subbuf will be accessible to user-space, zero all the page allocations.
kernel/trace/ring_buffer.c +6 -3
@@ -1472,7 +1472,8 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,list_add (&bpage->list, pages); - page =alloc_pages_node (cpu_to_node (cpu_buffer->cpu), mflags, + page =alloc_pages_node (cpu_to_node (cpu_buffer->cpu), + mflags | __GFP_ZERO, cpu_buffer->buffer->subbuf_order);if (!page) goto free_pages;
@@ -1557,7 +1558,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) cpu_buffer->reader_page = bpage; - page =alloc_pages_node (cpu_to_node (cpu),GFP_KERNEL , cpu_buffer->buffer->subbuf_order); + page =alloc_pages_node (cpu_to_node (cpu),GFP_KERNEL | __GFP_ZERO, + cpu_buffer->buffer->subbuf_order);if (!page) goto fail_free_reader; bpage->page =page_address (page);
@@ -5525,7 +5527,8 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)if (bpage->data) goto out; - page =alloc_pages_node (cpu_to_node (cpu),GFP_KERNEL | __GFP_NORETRY, + page =alloc_pages_node (cpu_to_node (cpu), +GFP_KERNEL | __GFP_NORETRY | __GFP_ZERO, cpu_buffer->buffer->subbuf_order);if (!page) {kfree (bpage);
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Vincent Donnefort
maintainerPATCH v14 2/6 ring-buffer: Introducing ring-buffer mapping functions
In reply to Vincent DonnefortIn preparation for allowing the user-space to map a ring-buffer, add a set of mapping functions:
ring_buffer_{map,unmap}()
ring_buffer_map_fault()
And controls on the ring-buffer:
ring_buffer_map_get_reader() /* swap reader and head */
Mapping the ring-buffer also involves:
A unique ID for each subbuf of the ring-buffer, currently they are only identified through their in-kernel VA.
A meta-page, where are stored ring-buffer statistics and a description for the current reader
The linear mapping exposes the meta-page, and each subbuf of the ring-buffer, ordered following their unique ID, assigned during the first mapping.
Once mapped, no subbuf can get in or out of the ring-buffer: the buffer size will remain unmodified and the splice enabling functions will in reality simply memcpy the data instead of swapping subbufs.
include/linux/ring_buffer.h +7 -0
@@ -6,6 +6,8 @@#include <linux/seq_file.h> #include <linux/poll.h> +#include <uapi/linux/trace_mmap.h> +struct trace_buffer ;struct ring_buffer_iter ;
@@ -221,4 +223,9 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node);#define trace_rb_cpu_prepare NULL#endif +int ring_buffer_map (struct trace_buffer *buffer,int cpu); +int ring_buffer_unmap (struct trace_buffer *buffer,int cpu); +struct page *ring_buffer_map_fault (struct trace_buffer *buffer,int cpu, +unsigned long pgoff); +int ring_buffer_map_get_reader (struct trace_buffer *buffer,int cpu);#endif /* _LINUX_RING_BUFFER_H */
include/uapi/linux/trace_mmap.h new file +46 -0
46 lines not on this page · raw message
kernel/trace/ring_buffer.c +332 -3
395 lines not on this page · raw message
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Vincent Donnefort
maintainerPATCH v14 3/6 tracing: Add snapshot refcount
In reply to Vincent DonnefortWhen a ring-buffer is memory mapped by user-space, no trace or ring-buffer swap is possible. This means the snapshot feature is mutually exclusive with the memory mapping. Having a refcount on snapshot users will help to know if a mapping is possible or not.
Instead of relying on the global trace_types_lock, a new spinlock is introduced to serialize accesses to trace_array->snapshot. This intends to allow access to that variable in a context where the mmap lock is already held.
kernel/trace/trace.c +82 -17
@@ -1300,6 +1300,50 @@ static void free_snapshot(struct trace_array *tr) tr->allocated_snapshot = false; } +static int tracing_arm_snapshot_locked (struct trace_array *tr) +{ +int ret; + +lockdep_assert_held (&trace_types_lock); + +spin_lock (&tr->snapshot_trigger_lock); +if (tr->snapshot ==UINT_MAX ) { +spin_unlock (&tr->snapshot_trigger_lock); +return -EBUSY ; + } + + tr->snapshot++; +spin_unlock (&tr->snapshot_trigger_lock); + + ret =tracing_alloc_snapshot_instance (tr); +if (ret) { +spin_lock (&tr->snapshot_trigger_lock); + tr->snapshot--; +spin_unlock (&tr->snapshot_trigger_lock); + } + +return ret; +} + +int tracing_arm_snapshot (struct trace_array *tr) +{ +int ret; + +mutex_lock (&trace_types_lock); + ret =tracing_arm_snapshot_locked (tr); +mutex_unlock (&trace_types_lock); + +return ret; +} + +void tracing_disarm_snapshot (struct trace_array *tr) +{ +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->snapshot)) + tr->snapshot--; +spin_unlock (&tr->snapshot_trigger_lock); +} +/** * tracing_alloc_snapshot - allocate snapshot buffer. *
@@ -1373,10 +1417,6 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,mutex_lock (&trace_types_lock); - ret =tracing_alloc_snapshot_instance (tr); -if (ret) - goto fail_unlock; -if (tr->current_trace->use_max_tr) { ret = -EBUSY ; goto fail_unlock;
@@ -1395,6 +1435,10 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, goto fail_unlock; } + ret =tracing_arm_snapshot_locked (tr); +if (ret) + goto fail_unlock; +local_irq_disable ();arch_spin_lock (&tr->max_lock); tr->cond_snapshot = cond_snapshot;
@@ -1439,6 +1483,8 @@ int tracing_snapshot_cond_disable(struct trace_array *tr)arch_spin_unlock (&tr->max_lock);local_irq_enable (); +tracing_disarm_snapshot (tr); +return ret; }EXPORT_SYMBOL_GPL (tracing_snapshot_cond_disable);
@@ -1481,6 +1527,7 @@ int tracing_snapshot_cond_disable(struct trace_array *tr) }EXPORT_SYMBOL_GPL (tracing_snapshot_cond_disable);#define free_snapshot (tr) do { } while (0) +#define tracing_arm_snapshot_locked (tr) ({ -EBUSY; })#endif /* CONFIG_TRACER_SNAPSHOT */ void tracer_tracing_off (struct trace_array *tr)
@@ -6593,11 +6640,12 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf)*/ synchronize_rcu ();free_snapshot (tr); + tracing_disarm_snapshot(tr); } -if (t->use_max_tr && !tr->allocated_snapshot) { - ret =tracing_alloc_snapshot_instance (tr); -if (ret <0 ) +if (t->use_max_tr) { + ret =tracing_arm_snapshot_locked (tr); +if (ret) goto out; }#else
@@ -6606,8 +6654,13 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf)if (t->init) { ret =tracer_init (t, tr); -if (ret) +if (ret) { +#ifdef CONFIG_TRACER_MAX_TRACE +if (t->use_max_tr) +tracing_disarm_snapshot (tr); +#endif goto out; + } } tr->current_trace = t;
@@ -7709,10 +7762,11 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,if (tr->allocated_snapshot) ret =resize_buffer_duplicate_size (&tr->max_buffer, &tr->array_buffer, iter->cpu_file); -else - ret =tracing_alloc_snapshot_instance (tr); -if (ret <0 ) + + ret =tracing_arm_snapshot_locked (tr); +if (ret)break ; +/* Now, we're going to swap */ if (iter->cpu_file ==RING_BUFFER_ALL_CPUS ) {local_irq_disable ();
@@ -7722,6 +7776,7 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,smp_call_function_single (iter->cpu_file, tracing_swap_cpu_buffer, (void *)tr,1 ); } +tracing_disarm_snapshot (tr);break ;default :if (tr->allocated_snapshot) {
@@ -8846,8 +8901,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; -if (glob[0 ] =='!' ) -return unregister_ftrace_function_probe_func (glob+1 , tr, ops); +if (glob[0 ] =='!' ) { + ret =unregister_ftrace_function_probe_func (glob+1 , tr, ops); +if (!ret) +tracing_disarm_snapshot (tr); + +return ret; + }if (!param) goto out_reg;
@@ -8866,12 +8926,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,return ret; out_reg: - ret =tracing_alloc_snapshot_instance (tr); + ret =tracing_arm_snapshot (tr);if (ret <0 ) goto out; ret =register_ftrace_function_probe (glob, tr, ops, count); - +if (ret <0 ) +tracing_disarm_snapshot (tr); out:return ret <0 ? ret :0 ; }
@@ -9678,7 +9739,9 @@ trace_array_create_systems(const char *name, const char *systems)raw_spin_lock_init (&tr->start_lock); tr->max_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; - +#ifdef CONFIG_TRCER_MAX_TRACE +spinlock_init (&tr->snapshot_trigger_lock); +#endif tr->current_trace = &nop_trace;INIT_LIST_HEAD (&tr->systems);
@@ -10648,7 +10711,9 @@ __init static int tracer_alloc_buffers(void) global_trace.current_trace = &nop_trace; global_trace.max_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; - +#ifdef CONFIG_TRACER_MAX_TRACE +spin_lock_init (&global_trace.snapshot_trigger_lock); +#endif ftrace_init_global_array_ops (&global_trace);init_trace_flags_index (&global_trace);
kernel/trace/trace.h +6 -2
@@ -334,8 +334,8 @@ struct trace_array {*/ struct array_buffer max_buffer;bool allocated_snapshot; -#endif -#ifdef CONFIG_TRACER_MAX_TRACE +spinlock_t snapshot_trigger_lock; +unsigned int snapshot;unsigned long max_latency;#ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency;
@@ -1973,12 +1973,16 @@ static inline void trace_event_eval_update(struct trace_eval_map **map, int len)#ifdef CONFIG_TRACER_SNAPSHOT void tracing_snapshot_instance (struct trace_array *tr);int tracing_alloc_snapshot_instance (struct trace_array *tr); +int tracing_arm_snapshot (struct trace_array *tr); +void tracing_disarm_snapshot (struct trace_array *tr);#else static inline void tracing_snapshot_instance (struct trace_array *tr) { }static inline int tracing_alloc_snapshot_instance (struct trace_array *tr) {return 0 ; } +static inline int tracing_arm_snapshot (struct trace_array *tr) {return 0 ; } +static inline void tracing_disarm_snapshot (struct trace_array *tr) { }#endif #ifdef CONFIG_PREEMPT_TRACER
kernel/trace/trace_events_trigger.c +41 -17
89 lines not on this page · raw message
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Vincent Donnefort
maintainerPATCH v14 4/6 tracing: Allow user-space mapping of the ring-buffer
In reply to Vincent DonnefortCurrently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy.
A solution for that problem is to let the user-space map the ring-buffer directly.
The mapping is exposed via the per-CPU file trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each subbuffer constituting the ring-buffer, ordered by their unique page ID:
* Meta-page -- include/uapi/linux/trace_mmap.h for a description
* Subbuf ID 0
* Subbuf ID 1
...
It is therefore easy to translate a subbuf ID into an offset in the mapping:
reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size;
When new data is available, the mapper must call a newly introduced ioctl:TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data.
Mapping will prevent snapshot and buffer size modifications.
include/uapi/linux/trace_mmap.h +2 -0
@@ -43,4 +43,6 @@ struct trace_buffer_meta { __u64 Reserved2; }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) +#endif /* _TRACE_MMAP_H_ */
kernel/trace/trace.c +119 -5
166 lines not on this page · raw message
kernel/trace/trace.h +1 -0
@@ -336,6 +336,7 @@ struct trace_array {bool allocated_snapshot;spinlock_t snapshot_trigger_lock;unsigned int snapshot; +unsigned int mapped;unsigned long max_latency;#ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency;
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Mathieu Desnoyers
reviewerRe: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer
Quoted 19 lines · Vincent Donnefort
Currently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy.
A solution for that problem is to let the user-space map the ring-buffer directly.
The mapping is exposed via the per-CPU file
trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each subbuffer constituting the ring-buffer, ordered by their unique page ID:* Meta-page --
include/uapi/linux/trace_mmap.hfor a description
* Subbuf ID 0
* Subbuf ID 1...It is therefore easy to translate a subbuf ID into an offset in the mapping:
Quoted 8 lines
reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size;When new data is available, the mapper must call a newly introduced ioctl:
TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data.Mapping will prevent snapshot and buffer size modifications.
How are the kernel linear mapping and the userspace mapping made coherent on architectures with virtually aliasing data caches ?
Ref. https://lore.kernel.org/lkml/[email protected]/T/#t
Thanks,
Mathieu
Quoted 202 lines
Signed-off-by Vincent Donnefort <[email protected]>include/uapi/linux/trace_mmap.h +2
-0@@ -43,4 +43,6 @@ struct trace_buffer_meta { __u64 Reserved2; }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) +#endif /* _TRACE_MMAP_H_ */ kernel/trace/trace.c +119
-5@@ -1175,6 +1175,12 @@ static void tracing_snapshot_instance_cond(struct trace_array *tr,return ; } +if (tr->mapped) { +trace_array_puts (tr,"*** BUFFER MEMORY MAPPED ***\n" ); +trace_array_puts (tr,"*** Can not use snapshot (sorry) ***\n" ); +return ; + } +local_irq_save (flags);update_max_tr (tr, current,smp_processor_id (), cond_data);local_irq_restore (flags);@@ -1307,7 +1313,7 @@ static int tracing_arm_snapshot_locked(struct trace_array *tr)lockdep_assert_held (&trace_types_lock);spin_lock (&tr->snapshot_trigger_lock); -if (tr->snapshot ==UINT_MAX ) { +if (tr->snapshot ==UINT_MAX || tr->mapped) {spin_unlock (&tr->snapshot_trigger_lock);return -EBUSY ; }@@ -6533,7 +6539,7 @@ static void tracing_set_nop(struct trace_array *tr) {if (tr->current_trace == &nop_trace)return ; - + tr->current_trace->enabled--;if (tr->current_trace->reset)@@ -8652,15 +8658,31 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,return ret; } -/* An ioctl call with cmd 0 to the ring buffer file will wake up all waiters */ static long tracing_buffers_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {struct ftrace_buffer_info *info = file->private_data;struct trace_iterator *iter = &info->iter; +int err; + +if (cmd ==TRACE_MMAP_IOCTL_GET_READER ) { +if (!(file->f_flags &O_NONBLOCK )) { + err =ring_buffer_wait (iter->array_buffer->buffer, + iter->cpu_file, + iter->tr->buffer_percent); +if (err) +return err; + } -if (cmd) -return -ENOIOCTLCMD ; +return ring_buffer_map_get_reader (iter->array_buffer->buffer, + iter->cpu_file); + }else if (cmd) { +return -ENOTTY ; + } +/* +* An ioctl call with cmd 0 to the ring buffer file will wake up all +* waiters +*/ mutex_lock (&trace_types_lock); iter->wait_index++;@@ -8673,6 +8695,97 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsignedreturn 0 ; } +static vm_fault_t tracing_buffers_mmap_fault (struct vm_fault *vmf) +{ +struct ftrace_buffer_info *info = vmf->vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; +vm_fault_t ret =VM_FAULT_SIGBUS ; +struct page *page; + + page =ring_buffer_map_fault (iter->array_buffer->buffer, iter->cpu_file, + vmf->pgoff); +if (!page) +return ret; + +get_page (page); + vmf->page = page; + vmf->page->mapping = vmf->vma->vm_file->f_mapping; + vmf->page->index = vmf->pgoff; + +return 0 ; +} + +static void tracing_buffers_mmap_close (struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; +struct trace_array *tr = iter->tr; + +ring_buffer_unmap (iter->array_buffer->buffer, iter->cpu_file); + +#ifdef CONFIG_TRACER_MAX_TRACE +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->mapped)) + tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); +#endif +} + +static void tracing_buffers_mmap_open (struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; + +WARN_ON (ring_buffer_map (iter->array_buffer->buffer, iter->cpu_file)); +} + +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +}; + +static int tracing_buffers_mmap (struct file *filp,struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = filp->private_data; +struct trace_iterator *iter = &info->iter; +struct trace_array *tr = iter->tr; +int ret =0 ; + +if (vma->vm_flags &VM_WRITE ) +return -EPERM ; + +vm_flags_mod (vma,VM_DONTCOPY |VM_DONTDUMP ,VM_MAYWRITE ); + vma->vm_ops = &tracing_buffers_vmops; + +#ifdef CONFIG_TRACER_MAX_TRACE +/* +* We hold mmap_lock here. lockdep would be unhappy if we would now take +* trace_types_lock. Instead use the specific snapshot_trigger_lock. +*/ +spin_lock (&tr->snapshot_trigger_lock); +if (tr->snapshot || tr->mapped ==UINT_MAX ) { +spin_unlock (&tr->snapshot_trigger_lock); +return -EBUSY ; + } + tr->mapped++; +spin_unlock (&tr->snapshot_trigger_lock); + +/* Wait for update_max_tr() to observe iter->tr->mapped */ +if (tr->mapped ==1 ) +synchronize_rcu (); +#endif + ret =ring_buffer_map (iter->array_buffer->buffer, iter->cpu_file); +#ifdef CONFIG_TRACER_MAX_TRACE +if (ret) { +spin_lock (&tr->snapshot_trigger_lock); + iter->tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); + } +#endif +return ret; +} +static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read,@@ -8681,6 +8794,7 @@ static const struct file_operations tracing_buffers_fops = { .splice_read = tracing_buffers_splice_read, .unlocked_ioctl = tracing_buffers_ioctl, .llseek = no_llseek, + .mmap = tracing_buffers_mmap, }; static ssize_tkernel/trace/trace.h +1
-0@@ -336,6 +336,7 @@ struct trace_array {bool allocated_snapshot;spinlock_t snapshot_trigger_lock;unsigned int snapshot; +unsigned int mapped;unsigned long max_latency;#ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency;
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Vincent Donnefort
maintainerRe: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer
Quoted 27 lines · Mathieu Desnoyers
Quoted 26 lines · Vincent Donnefort
Currently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy.
A solution for that problem is to let the user-space map the ring-buffer directly.
The mapping is exposed via the per-CPU file
trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each subbuffer constituting the ring-buffer, ordered by their unique page ID:* Meta-page --
include/uapi/linux/trace_mmap.hfor a description
* Subbuf ID 0
* Subbuf ID 1...It is therefore easy to translate a subbuf ID into an offset in the mapping:
reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size;When new data is available, the mapper must call a newly introduced ioctl:
TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data.
Quoted 6 lines
Quoted 1 line
Mapping will prevent snapshot and buffer size modifications.
How are the kernel linear mapping and the userspace mapping made coherent on architectures with virtually aliasing data caches ?
Hi Mathieu,
Thanks for the pointer.
We are in the exact same problem as DAX. We do modify the data through the kernel linear mapping while user-space can read it through its own. I should probably return an error when used with any of the arch ARM || SPARC || MIPS, until cpu_dcache_is_aliasing() introduces a fine-grain differentiation.
Quoted 203 lines
Thanks,
Mathieu
Quoted 192 lines
Signed-off-by Vincent Donnefort <[email protected]>include/uapi/linux/trace_mmap.h +2
-0@@ -43,4 +43,6 @@ struct trace_buffer_meta { __u64 Reserved2; }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) +#endif /* _TRACE_MMAP_H_ */ kernel/trace/trace.c +119
-5@@ -1175,6 +1175,12 @@ static void tracing_snapshot_instance_cond(struct trace_array *tr,return ; } +if (tr->mapped) { +trace_array_puts (tr,"*** BUFFER MEMORY MAPPED ***\n" ); +trace_array_puts (tr,"*** Can not use snapshot (sorry) ***\n" ); +return ; + } +local_irq_save (flags);update_max_tr (tr, current,smp_processor_id (), cond_data);local_irq_restore (flags);@@ -1307,7 +1313,7 @@ static int tracing_arm_snapshot_locked(struct trace_array *tr)lockdep_assert_held (&trace_types_lock);spin_lock (&tr->snapshot_trigger_lock); -if (tr->snapshot ==UINT_MAX ) { +if (tr->snapshot ==UINT_MAX || tr->mapped) {spin_unlock (&tr->snapshot_trigger_lock);return -EBUSY ; }@@ -6533,7 +6539,7 @@ static void tracing_set_nop(struct trace_array *tr) {if (tr->current_trace == &nop_trace)return ; - + tr->current_trace->enabled--;if (tr->current_trace->reset)@@ -8652,15 +8658,31 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,return ret; } -/* An ioctl call with cmd 0 to the ring buffer file will wake up all waiters */ static long tracing_buffers_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {struct ftrace_buffer_info *info = file->private_data;struct trace_iterator *iter = &info->iter; +int err; + +if (cmd ==TRACE_MMAP_IOCTL_GET_READER ) { +if (!(file->f_flags &O_NONBLOCK )) { + err =ring_buffer_wait (iter->array_buffer->buffer, + iter->cpu_file, + iter->tr->buffer_percent); +if (err) +return err; + } -if (cmd) -return -ENOIOCTLCMD ; +return ring_buffer_map_get_reader (iter->array_buffer->buffer, + iter->cpu_file); + }else if (cmd) { +return -ENOTTY ; + } +/* +* An ioctl call with cmd 0 to the ring buffer file will wake up all +* waiters +*/ mutex_lock (&trace_types_lock); iter->wait_index++;@@ -8673,6 +8695,97 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsignedreturn 0 ; } +static vm_fault_t tracing_buffers_mmap_fault (struct vm_fault *vmf) +{ +struct ftrace_buffer_info *info = vmf->vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; +vm_fault_t ret =VM_FAULT_SIGBUS ; +struct page *page; + + page =ring_buffer_map_fault (iter->array_buffer->buffer, iter->cpu_file, + vmf->pgoff); +if (!page) +return ret; + +get_page (page); + vmf->page = page; + vmf->page->mapping = vmf->vma->vm_file->f_mapping; + vmf->page->index = vmf->pgoff; + +return 0 ; +} + +static void tracing_buffers_mmap_close (struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; +struct trace_array *tr = iter->tr; + +ring_buffer_unmap (iter->array_buffer->buffer, iter->cpu_file); + +#ifdef CONFIG_TRACER_MAX_TRACE +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->mapped)) + tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); +#endif +} + +static void tracing_buffers_mmap_open (struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = vma->vm_file->private_data; +struct trace_iterator *iter = &info->iter; + +WARN_ON (ring_buffer_map (iter->array_buffer->buffer, iter->cpu_file)); +} + +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +}; + +static int tracing_buffers_mmap (struct file *filp,struct vm_area_struct *vma) +{ +struct ftrace_buffer_info *info = filp->private_data; +struct trace_iterator *iter = &info->iter; +struct trace_array *tr = iter->tr; +int ret =0 ; + +if (vma->vm_flags &VM_WRITE ) +return -EPERM ; + +vm_flags_mod (vma,VM_DONTCOPY |VM_DONTDUMP ,VM_MAYWRITE ); + vma->vm_ops = &tracing_buffers_vmops; + +#ifdef CONFIG_TRACER_MAX_TRACE +/* +* We hold mmap_lock here. lockdep would be unhappy if we would now take +* trace_types_lock. Instead use the specific snapshot_trigger_lock. +*/ +spin_lock (&tr->snapshot_trigger_lock); +if (tr->snapshot || tr->mapped ==UINT_MAX ) { +spin_unlock (&tr->snapshot_trigger_lock); +return -EBUSY ; + } + tr->mapped++; +spin_unlock (&tr->snapshot_trigger_lock); + +/* Wait for update_max_tr() to observe iter->tr->mapped */ +if (tr->mapped ==1 ) +synchronize_rcu (); +#endif + ret =ring_buffer_map (iter->array_buffer->buffer, iter->cpu_file); +#ifdef CONFIG_TRACER_MAX_TRACE +if (ret) { +spin_lock (&tr->snapshot_trigger_lock); + iter->tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); + } +#endif +return ret; +} +static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read,@@ -8681,6 +8794,7 @@ static const struct file_operations tracing_buffers_fops = { .splice_read = tracing_buffers_splice_read, .unlocked_ioctl = tracing_buffers_ioctl, .llseek = no_llseek, + .mmap = tracing_buffers_mmap, }; static ssize_tkernel/trace/trace.h +1
-0@@ -336,6 +336,7 @@ struct trace_array {bool allocated_snapshot;spinlock_t snapshot_trigger_lock;unsigned int snapshot; +unsigned int mapped;unsigned long max_latency;#ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency;Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Mathieu Desnoyers
reviewerRe: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer
Quoted 1 line · Vincent Donnefort
On Mon, Feb 05, 2024 at 11:55:08AM -0500, Mathieu Desnoyers wrote:
[...]
Quoted 6 lines
Quoted 5 lines
How are the kernel linear mapping and the userspace mapping made coherent on architectures with virtually aliasing data caches ?
Quoted 8 lines
Hi Mathieu,
Thanks for the pointer.
We are in the exact same problem as DAX. We do modify the data through the kernel linear mapping while user-space can read it through its own. I should probably return an error when used with any of the arch ARM || SPARC || MIPS, until
cpu_dcache_is_aliasing()introduces a fine-grain differentiation.
You might want to use LTTng's ring buffer approach instead. See
https://github.com/lttng/lttng-modules/blob/master/src/lib/ringbuffer/ring_buffer_frontend.c#L1202
lib_ring_buffer_flush_read_subbuf_dcache()
Basically, whenever user-space grabs a sub-buffer for reading (through lttng-modules's LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF ioctl), lttng calls flush_dcache_page() on all pages of this subbuffer (I should really change this for a call to flush_dcache_folio() which would be more efficient).
Note that doing this is not very efficient on architectures which have coherent data caches and incoherent dcache vs icache: in that case, we issue the flush_dcache_page uselessly. I plan on using the new cpu_dcache_is_aliasing() check once/if it makes it way upstream to remove those useless flushes on architectures which define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, but do not virtually alias the data cache.
The equivalent of LTTng's "get subbuf" operation would be the new TRACE_MMAP_IOCTL_GET_READER ioctl in ftrace AFAIU.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Vincent Donnefort
maintainerRe: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer
Quoted 19 lines · Mathieu Desnoyers
Quoted 1 line · Vincent Donnefort
On Mon, Feb 05, 2024 at 11:55:08AM -0500, Mathieu Desnoyers wrote:
[...]
Quoted 6 lines
Quoted 5 lines
How are the kernel linear mapping and the userspace mapping made coherent on architectures with virtually aliasing data caches ?
Quoted 8 lines
Hi Mathieu,
Thanks for the pointer.
We are in the exact same problem as DAX. We do modify the data through the kernel linear mapping while user-space can read it through its own. I should probably return an error when used with any of the arch ARM || SPARC || MIPS, until
cpu_dcache_is_aliasing()introduces a fine-grain differentiation.
Quoted 5 lines
You might want to use LTTng's ring buffer approach instead. See
https://github.com/lttng/lttng-modules/blob/master/src/lib/ringbuffer/ring_buffer_frontend.c#L1202
lib_ring_buffer_flush_read_subbuf_dcache()
Thanks!
Quoted 7 lines
Basically, whenever user-space grabs a sub-buffer for reading (through lttng-modules's
LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUFioctl), lttng callsflush_dcache_page()on all pages of this subbuffer (I should really change this for a call toflush_dcache_folio()which would be more efficient).
Quoted 7 lines
Note that doing this is not very efficient on architectures which have coherent data caches and incoherent dcache vs icache: in that case, we issue the
flush_dcache_pageuselessly. I plan on using the newcpu_dcache_is_aliasing()check once/if it makes it way upstream to remove those useless flushes on architectures which defineARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, but do not virtually alias the data cache.
I believe the aim is to use the mapping by default in libtracefs and fallback to splice whenever not available... But for those arch, I guess that might be a mistake. Wonder if then it isn't just better to return ENOTSUPP?
Quoted 3 lines
The equivalent of LTTng's "get subbuf" operation would be the new
TRACE_MMAP_IOCTL_GET_READERioctl in ftrace AFAIU.
That is correct!
Quoted 10 lines
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
kernel test robot
build botRe: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer
In reply to Vincent Donnefortkernel test robot noticed the following build warnings:
Hi Vincent,
kernel test robot noticed the following build warnings:
[auto build test WARNING on e2412e51fdea837b50ce31fea8e5dfc885237f3a]
url: https://github.com/intel-lab-lkp/linux/commits/Vincent-Donnefort/ring-buffer-Zero-ring-buffer-sub-buffers/20240206-004413 base: e2412e51fdea837b50ce31fea8e5dfc885237f3a patch link: https://lore.kernel.org/r/20240205163410.2296552-5-vdonnefort%40google.com patch subject: [PATCH v14 4/6] tracing: Allow user-space mapping of the ring-buffer config: arc-defconfig (https://download.01.org/0day-ci/archive/20240206/[email protected]/config) compiler: arc-elf-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240206/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
kernel/trace/trace.c: In function 'tracing_buffers_mmap_close':
>> kernel/trace/trace.c:8722:29: warning: unused variable 'tr' [-Wunused-variable]
8722 | struct trace_array *tr = iter->tr;
| ^~
kernel/trace/trace.c: In function 'tracing_buffers_mmap':
kernel/trace/trace.c:8752:29: warning: unused variable 'tr' [-Wunused-variable]
8752 | struct trace_array *tr = iter->tr;
| ^~vim +/tr +8722 kernel/trace/trace.c
8717
8718 static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
8719 {
8720 struct ftrace_buffer_info *info = vma->vm_file->private_data;
8721 struct trace_iterator *iter = &info->iter;
> 8722 struct trace_array *tr = iter->tr;
8723
8724 ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file);
8725
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Vincent Donnefort
maintainerPATCH v14 5/6 Documentation: tracing: Add ring-buffer mapping
In reply to Vincent DonnefortIt is now possible to mmap() a ring-buffer to stream its content. Add some documentation and a code example.
Documentation/trace/index.rst +1 -0
@@ -29,6 +29,7 @@ Linux Tracing Technologies timerlat-tracer intel_th ring-buffer-design + ring-buffer-map stm sys-t coresight/index
Documentation/trace/ring-buffer-map.rst new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Vincent Donnefort
maintainerPATCH v14 6/6 ring-buffer/selftest: Add ring-buffer mapping test
In reply to Vincent DonnefortThis test maps a ring-buffer and validate the meta-page after reset and after emitting few events.
tools/testing/selftests/ring-buffer/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wl,-no-as-needed -Wall +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -D_GNU_SOURCE + +TEST_GEN_PROGS = map_test + +include ../lib.mk
tools/testing/selftests/ring-buffer/config new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_FTRACE=y +CONFIG_TRACER_SNAPSHOT=y
tools/testing/selftests/ring-buffer/map_test.c new file +273 -0
273 lines not on this page · raw message
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog