Introducing trace buffer mapping by user-space
Subsystems (4)
Vincent Donnefort
maintainerv18 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
v17 -> v18:
* Fix lockdep_assert_held
* Fix spin_lock_init typo
* Fix CONFIG_TRACER_MAX_TRACE typo
v16 -> v17:
* Documentation and comments improvements.
* Create get/put_snapshot_map() for clearer code.
* Replace kzalloc with kcalloc.
* Fix -ENOMEM handling in rb_alloc_meta_page().
* Move flush(cpu_buffer->reader_page) behind the reader lock.
* Move all inc/dec of cpu_buffer->mapped behind reader lock and buffer mutex. (removes READ_ONCE/WRITE_ONCE accesses).
v15 -> v16:
* Add comment for the dcache flush.
* Remove now unnecessary WRITE_ONCE for the meta-page.
v14 -> v15:
* Add meta-page and reader-page flush. Intends to fix the mapping for VIVT and aliasing-VIPT data caches.
* -EPERM on VM_EXEC.
* Fix build warning !CONFIG_TRACER_MAX_TRACE.
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 | 106 +++++ include/linux/ring_buffer.h | 7 + include/uapi/linux/trace_mmap.h | 48 +++ kernel/trace/ring_buffer.c | 385 +++++++++++++++++- kernel/trace/trace.c | 234 ++++++++++- 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, 1085 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: 6efe4d18796934b8ada66c1c446510e7f2d9b972
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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 +373 -3
442 lines not on this page · raw message
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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
@@ -1301,6 +1301,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. *
@@ -1374,10 +1418,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;
@@ -1396,6 +1436,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;
@@ -1440,6 +1484,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);
@@ -1482,6 +1528,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)
@@ -6595,11 +6642,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
@@ -6608,8 +6656,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;
@@ -7711,10 +7764,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 ();
@@ -7724,6 +7778,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) {
@@ -8848,8 +8903,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;
@@ -8868,12 +8928,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 ; }
@@ -9680,7 +9741,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_TRACER_MAX_TRACE +spin_lock_init (&tr->snapshot_trigger_lock); +#endif tr->current_trace = &nop_trace;INIT_LIST_HEAD (&tr->systems);
@@ -10650,7 +10713,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.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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 +132 -5
179 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.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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 +106 -0
106 lines not on this page · raw message
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Vincent Donnefort
maintainerPATCH v18 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.44.0.rc0.258.g7320e95886-goog