Introducing trace buffer mapping by user-space
Subsystems (5)
Vincent Donnefort
maintainerv19 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
v18 -> v19:
* Use VM_PFNMAP and vm_insert_pages
* Allocate ring-buffer subbufs with __GFP_COMP
* Pad the meta-page with the zero-page to align on the subbuf_order
* Extend the ring-buffer test with mmap() dedicated suite
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 (5):
ring-buffer: allocate sub-buffers with __GFP_COMP
ring-buffer: Introducing ring-buffer mapping functions
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 | 6 + include/uapi/linux/trace_mmap.h | 48 +++ kernel/trace/ring_buffer.c | 402 +++++++++++++++++- kernel/trace/trace.c | 122 +++++- kernel/trace/trace.h | 1 + tools/testing/selftests/ring-buffer/Makefile | 8 + tools/testing/selftests/ring-buffer/config | 2 + .../testing/selftests/ring-buffer/map_test.c | 302 +++++++++++++ 10 files changed, 987 insertions(+), 11 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: 7604256cecef34a82333d9f78262d3180f4eb525
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
Vincent Donnefort
maintainerPATCH RESEND v19 1/5 ring-buffer: allocate sub-buffers with __GFP_COMP
In preparation for the ring-buffer memory mapping, allocate compound pages for the ring-buffer sub-buffers to enable us to map them to user-space with vm_insert_pages().
kernel/trace/ring_buffer.c +3 -3
@@ -1524,7 +1524,7 @@ 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 | __GFP_ZERO, + mflags | __GFP_COMP | __GFP_ZERO, cpu_buffer->buffer->subbuf_order);if (!page) goto free_pages;
@@ -1609,7 +1609,7 @@ 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 | __GFP_ZERO, + page =alloc_pages_node (cpu_to_node (cpu),GFP_KERNEL | __GFP_COMP | __GFP_ZERO, cpu_buffer->buffer->subbuf_order);if (!page) goto fail_free_reader;
@@ -5579,7 +5579,7 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) goto out; page =alloc_pages_node (cpu_to_node (cpu), -GFP_KERNEL | __GFP_NORETRY | __GFP_ZERO, +GFP_KERNEL | __GFP_NORETRY | __GFP_COMP | __GFP_ZERO, cpu_buffer->buffer->subbuf_order);if (!page) {kfree (bpage);
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
Vincent Donnefort
maintainerPATCH RESEND v19 2/5 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}()
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 +6 -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 ;
@@ -223,4 +225,8 @@ 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, +struct vm_area_struct *vma); +int ring_buffer_unmap (struct trace_buffer *buffer,int cpu); +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 +393 -3
462 lines not on this page · raw message
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
kernel test robot
build botRe: [PATCH v19 RESEND 2/5] ring-buffer: Introducing ring-buffer mapping functions
kernel test robot noticed the following build errors:
Hi Vincent,
kernel test robot noticed the following build errors:
[auto build test ERROR on 7604256cecef34a82333d9f78262d3180f4eb525]
url: https://github.com/intel-lab-lkp/linux/commits/Vincent-Donnefort/ring-buffer-allocate-sub-buffers-with-__GFP_COMP/20240326-181048 base: 7604256cecef34a82333d9f78262d3180f4eb525 patch link: https://lore.kernel.org/r/20240326100830.1326610-3-vdonnefort%40google.com patch subject: [PATCH v19 RESEND 2/5] ring-buffer: Introducing ring-buffer mapping functions config: sh-randconfig-r121-20240327 (https://download.01.org/0day-ci/archive/20240329/[email protected]/config) compiler: sh4-linux-gcc (GCC) 13.2.0 reproduce: (https://download.01.org/0day-ci/archive/20240329/[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 errors (new ones prefixed by >>):
sh4-linux-ld: kernel/trace/ring_buffer.o: in function `__rb_map_vma':
>> ring_buffer.c:(.text+0xffc): undefined reference to `vm_insert_pages'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v19 RESEND 2/5] ring-buffer: Introducing ring-buffer mapping functions
In reply to Vincent Donnefortkernel test robot noticed the following build errors:
Hi Vincent,
kernel test robot noticed the following build errors:
[auto build test ERROR on 7604256cecef34a82333d9f78262d3180f4eb525]
url: https://github.com/intel-lab-lkp/linux/commits/Vincent-Donnefort/ring-buffer-allocate-sub-buffers-with-__GFP_COMP/20240326-181048 base: 7604256cecef34a82333d9f78262d3180f4eb525 patch link: https://lore.kernel.org/r/20240326100830.1326610-3-vdonnefort%40google.com patch subject: [PATCH v19 RESEND 2/5] ring-buffer: Introducing ring-buffer mapping functions config: riscv-randconfig-001-20240328 (https://download.01.org/0day-ci/archive/20240329/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240329/[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 errors (new ones prefixed by >>):
riscv64-linux-ld: kernel/trace/ring_buffer.o: in function `__rb_map_vma':
>> kernel/trace/ring_buffer.c:6295:(.text.__rb_map_vma+0x288): undefined reference to `vm_insert_pages'
vim +6295 kernel/trace/ring_buffer.c
6212
6213 #define subbuf_page(off, start) \
6214 virt_to_page((void *)((start) + ((off) << PAGE_SHIFT)))
6215
6216 #define foreach_subbuf_page(sub_order, start, page) \
6217 page = subbuf_page(0, (start)); \
6218 for (int __off = 0; __off < (1 << (sub_order)); \
6219 __off++, page = subbuf_page(__off, (start)))
6220
6221 /*
6222 * +--------------+ pgoff == 0
6223 * | meta page |
6224 * +--------------+ pgoff == 1
6225 * | 000000000 |
6226 * +--------------+ pgoff == (1 << subbuf_order)
6227 * | subbuffer 0 |
6228 * | |
6229 * +--------------+ pgoff == (2 * (1 << subbuf_order))
6230 * | subbuffer 1 |
6231 * | |
6232 * ...
6233 */
6234 static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
6235 struct vm_area_struct *vma)
6236 {
6237 unsigned long nr_subbufs, nr_pages, vma_pages, pgoff = vma->vm_pgoff;
6238 unsigned int subbuf_pages, subbuf_order;
6239 struct page **pages;
6240 int p = 0, s = 0;
6241 int err;
6242
6243 lockdep_assert_held(&cpu_buffer->mapping_lock);
6244
6245 subbuf_order = cpu_buffer->buffer->subbuf_order;
6246 subbuf_pages = 1 << subbuf_order;
6247
6248 if (subbuf_order && pgoff % subbuf_pages)
6249 return -EINVAL;
6250
6251 nr_subbufs = cpu_buffer->nr_pages + 1;
6252 nr_pages = ((nr_subbufs + 1) << subbuf_order) - pgoff;
6253
6254 vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
6255 if (!vma_pages || vma_pages > nr_pages)
6256 return -EINVAL;
6257
6258 nr_pages = vma_pages;
6259
6260 pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
6261 if (!pages)
6262 return -ENOMEM;
6263
6264 if (!pgoff) {
6265 unsigned long meta_page_padding;
6266
6267 pages[p++] = virt_to_page(cpu_buffer->meta_page);
6268
6269 /*
6270 * Pad with the zero-page to align the meta-page with the
6271 * sub-buffers.
6272 */
6273 meta_page_padding = subbuf_pages - 1;
6274 while (meta_page_padding-- && p < nr_pages)
6275 pages[p++] = ZERO_PAGE(0);
6276 } else {
6277 /* Skip the meta-page */
6278 pgoff -= subbuf_pages;
6279
6280 s += pgoff / subbuf_pages;
6281 }
6282
6283 while (s < nr_subbufs && p < nr_pages) {
6284 struct page *page;
6285
6286 foreach_subbuf_page(subbuf_order, cpu_buffer->subbuf_ids[s], page) {
6287 if (p >= nr_pages)
6288 break;
6289
6290 pages[p++] = page;
6291 }
6292 s++;
6293 }
6294
> 6295 err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages); 6296
6297 kfree(pages);
6298
6299 return err;
6300 }
6301
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Vincent Donnefort
maintainerPATCH RESEND v19 3/5 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 +117 -5
164 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.396.g6e790dbe36-goog
Steven Rostedt
maintainerRe: [PATCH v19 RESEND 3/5] tracing: Allow user-space mapping of the ring-buffer
Quoted 19 lines · On Tue, 26 Mar 2024 10:08:28 +0000 Vincent Donnefort <[email protected]>…
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 7 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.
Thanks for the update Vincent!
Quoted 88 lines
Mapping will prevent snapshot and buffer size modifications.
CC <[email protected]>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 +28
-5@@ -1191,6 +1191,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);@@ -1323,7 +1329,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 ; }@@ -6068,7 +6074,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)@@ -8194,15 +8200,32 @@ 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, +NULL ,NULL ); +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);/* Make sure the waiters see the new wait_index */
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8243
Quoted 7 lines
kernel/trace/trace.c +4
-0@@ -8214,6 +8237,94 @@ 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) +{ +return VM_FAULT_SIGBUS ; +}
If this is all it does, I don't believe it's needed.
Quoted 39 lines
kernel/trace/trace.c +39
-0+ +#ifdef CONFIG_TRACER_MAX_TRACE +static int get_snapshot_map (struct trace_array *tr) +{ +int err =0 ; + +/* +* Called with mmap_lock held. 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 ) + err = -EBUSY ; +else + tr->mapped++; + +spin_unlock (&tr->snapshot_trigger_lock); + +/* Wait for update_max_tr() to observe iter->tr->mapped */ +if (tr->mapped ==1 ) +synchronize_rcu (); + +return err; + +} +static void put_snapshot_map (struct trace_array *tr) +{ +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->mapped)) + tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); +} +#else +static inline int get_snapshot_map (struct trace_array *tr) {return 0 ; } +static inline void put_snapshot_map (struct trace_array *tr) { } +#endif +
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8292
Quoted 10 lines
kernel/trace/trace.c +10
-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; + +WARN_ON (ring_buffer_unmap (iter->array_buffer->buffer, iter->cpu_file)); +put_snapshot_map (iter->tr); +} + +static void tracing_buffers_mmap_open (struct vm_area_struct *vma) { }
Same for the open.
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8298
Quoted 6 lines
kernel/trace/trace.c +6
-0+ +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +};
I replaced this with:
static const struct vm_operations_struct tracing_buffers_vmops = {
.close = tracing_buffers_mmap_close,
};
And it appears to work just fine. The mm code handles the NULL cases for .open and .fault.
Is there any reason to do something different than the mm defaults?
-- Steve
Quoted 52 lines
kernel/trace/trace.c +30
-0+ +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; +int ret =0 ; + +if (vma->vm_flags &VM_WRITE || vma->vm_flags &VM_EXEC || + !(vma->vm_flags &VM_MAYSHARE )) +return -EPERM ; + +vm_flags_mod (vma, +VM_MIXEDMAP |VM_PFNMAP | +VM_DONTCOPY |VM_DONTDUMP |VM_DONTEXPAND |VM_IO , +VM_MAYWRITE ); + + vma->vm_ops = &tracing_buffers_vmops; + + ret =get_snapshot_map (iter->tr); +if (ret) +return ret; + + ret =ring_buffer_map (iter->array_buffer->buffer, iter->cpu_file, vma); +if (ret) +put_snapshot_map (iter->tr); + +return ret; +} +static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read,@@ -8223,6 +8334,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;
Steven Rostedt
maintainerRe: [PATCH v19 RESEND 3/5] tracing: Allow user-space mapping of the ring-buffer
Quoted 69 lines · On Fri, 29 Mar 2024 14:40:55 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8243
Quoted 4 lines
kernel/trace/trace.c +4
-0+static vm_fault_t tracing_buffers_mmap_fault (struct vm_fault *vmf) +{ +return VM_FAULT_SIGBUS ; +}If this is all it does, I don't believe it's needed.
Quoted 39 lines
kernel/trace/trace.c +39
-0+ +#ifdef CONFIG_TRACER_MAX_TRACE +static int get_snapshot_map (struct trace_array *tr) +{ +int err =0 ; + +/* +* Called with mmap_lock held. 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 ) + err = -EBUSY ; +else + tr->mapped++; + +spin_unlock (&tr->snapshot_trigger_lock); + +/* Wait for update_max_tr() to observe iter->tr->mapped */ +if (tr->mapped ==1 ) +synchronize_rcu (); + +return err; + +} +static void put_snapshot_map (struct trace_array *tr) +{ +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->mapped)) + tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); +} +#else +static inline int get_snapshot_map (struct trace_array *tr) {return 0 ; } +static inline void put_snapshot_map (struct trace_array *tr) { } +#endif +kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8292
Quoted 10 lines
kernel/trace/trace.c +10
-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; + +WARN_ON (ring_buffer_unmap (iter->array_buffer->buffer, iter->cpu_file)); +put_snapshot_map (iter->tr); +} + +static void tracing_buffers_mmap_open (struct vm_area_struct *vma) { }Same for the open.
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8298
Quoted 6 lines
kernel/trace/trace.c +6
-0+ +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +};I replaced this with:
Quoted 8 lines
static const struct
vm_operations_structtracing_buffers_vmops= {
.close =tracing_buffers_mmap_close,
};And it appears to work just fine. The mm code handles the NULL cases for .open and .fault.
Is there any reason to do something different than the mm defaults?
Hi Vincent,
Do you plan on sending out a v20 series?
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v19 RESEND 3/5] tracing: Allow user-space mapping of the ring-buffer
Quoted 72 lines · Steven Rostedt
Quoted 69 lines · On Fri, 29 Mar 2024 14:40:55 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8243
Quoted 4 lines
kernel/trace/trace.c +4
-0+static vm_fault_t tracing_buffers_mmap_fault (struct vm_fault *vmf) +{ +return VM_FAULT_SIGBUS ; +}If this is all it does, I don't believe it's needed.
Quoted 39 lines
kernel/trace/trace.c +39
-0+ +#ifdef CONFIG_TRACER_MAX_TRACE +static int get_snapshot_map (struct trace_array *tr) +{ +int err =0 ; + +/* +* Called with mmap_lock held. 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 ) + err = -EBUSY ; +else + tr->mapped++; + +spin_unlock (&tr->snapshot_trigger_lock); + +/* Wait for update_max_tr() to observe iter->tr->mapped */ +if (tr->mapped ==1 ) +synchronize_rcu (); + +return err; + +} +static void put_snapshot_map (struct trace_array *tr) +{ +spin_lock (&tr->snapshot_trigger_lock); +if (!WARN_ON (!tr->mapped)) + tr->mapped--; +spin_unlock (&tr->snapshot_trigger_lock); +} +#else +static inline int get_snapshot_map (struct trace_array *tr) {return 0 ; } +static inline void put_snapshot_map (struct trace_array *tr) { } +#endif +kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8292
Quoted 10 lines
kernel/trace/trace.c +10
-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; + +WARN_ON (ring_buffer_unmap (iter->array_buffer->buffer, iter->cpu_file)); +put_snapshot_map (iter->tr); +} + +static void tracing_buffers_mmap_open (struct vm_area_struct *vma) { }Same for the open.
kernel/trace/trace.c static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned · line 8298
Quoted 6 lines
kernel/trace/trace.c +6
-0+ +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +};I replaced this with:
Quoted 8 lines
Quoted 8 lines
static const struct
vm_operations_structtracing_buffers_vmops= {
.close =tracing_buffers_mmap_close,
};And it appears to work just fine. The mm code handles the NULL cases for .open and .fault.
Is there any reason to do something different than the mm defaults?
No other reason here than my own ignorance. I will remove.
Quoted 4 lines
Hi Vincent,
Do you plan on sending out a v20 series?
Of course, let me spin that this week! Got also few typos to fix in the doc and I believe an include missing for riscv.
Quoted 2 lines
-- Steve
Steven Rostedt
maintainerRe: [PATCH v19 RESEND 3/5] tracing: Allow user-space mapping of the ring-buffer
Quoted 4 lines · On Wed, 3 Apr 2024 15:39:44 +0100 Vincent Donnefort <[email protected]>…
Quoted 1 line
Do you plan on sending out a v20 series?
Of course, let me spin that this week! Got also few typos to fix in the doc and I believe an include missing for riscv.
No rush, I'll be on PTO until next Tuesday, and will not get to it before then.
-- Steve
Vincent Donnefort
maintainerPATCH RESEND v19 4/5 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.396.g6e790dbe36-goog
Randy Dunlap
reviewerRe: [PATCH v19 RESEND 4/5] Documentation: tracing: Add ring-buffer mapping
Hi,
Quoted 22 lines · Vincent Donnefort
It is now possible to
mmap()a ring-buffer to stream its content. Add some documentation and a code example.Signed-off-by Vincent Donnefort <[email protected]>Documentation/trace/ring-buffer-map.rst new file +10
-0@@ -0,0 +1,106 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +Tracefs ring-buffer memory mapping +================================== + +:Author: Vincent Donnefort <[email protected]> + +Overview +========
Documentation/trace/ring-buffer-map.rst line 20
Quoted 10 lines
Documentation/trace/ring-buffer-map.rst new file +10
-0+Tracefs ring-buffer memory map provides an efficient method to stream data +as no memory copy is necessary. The application mapping the ring-buffer becomes +then a consumer for that ring-buffer, in a similar fashion to trace_pipe. + +Memory mapping setup +==================== +The mapping works with a mmap() of the trace_pipe_raw interface. + +The first system page of the mapping contains ring-buffer statistics and +description. It is referred as the meta-page. One of the most important field of
referred to as fields
Documentation/trace/ring-buffer-map.rst line 24
Quoted 4 lines
Documentation/trace/ring-buffer-map.rst new file +4
-0+the meta-page is the reader. It contains the sub-buffer ID which can be safely +read by the mapper (see ring-buffer-design.rst). + +The meta-page is followed by all the sub-buffers, ordered by ascendant ID. It is
ascending
might be better IMO.
Documentation/trace/ring-buffer-map.rst line 34
Quoted 10 lines
Documentation/trace/ring-buffer-map.rst new file +10
-0+therefore effortless to know where the reader starts in the mapping: + +.. code-block:: c + + reader_id = meta->reader->id; + reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size; + +When the application is done with the current reader, it can get a new one using +the trace_pipe_raw ioctl() TRACE_MMAP_IOCTL_GET_READER. This ioctl also updates +the meta-page fields.
[snip]
Thanks.
Signature · 1 line
#Randy
Vincent Donnefort
maintainerPATCH RESEND v19 5/5 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 +302 -0
302 lines not on this page · raw message
Signature · 1 line
2.44.0.396.g6e790dbe36-goog