1 /* SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2002-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp
6 * Copyright (C) 1999-2005 - Karim Yaghmour <karim@opersys.com>
7 * Copyright (C) 2008-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Re-using code from kernel/relay.c, hence the GPL-2.0-only license for this
13 #include <linux/module.h>
16 #include <ringbuffer/backend.h>
17 #include <ringbuffer/frontend.h>
18 #include <ringbuffer/vfs.h>
21 * fault() vm_op implementation for ring buffer file mapping.
23 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
24 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
25 static vm_fault_t
lib_ring_buffer_fault_compat(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
27 static int lib_ring_buffer_fault_compat(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
30 struct lttng_kernel_ring_buffer
*buf
= vma
->vm_private_data
;
31 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
32 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
33 pgoff_t pgoff
= vmf
->pgoff
;
36 unsigned long offset
, sb_bindex
;
39 * Verify that faults are only done on the range of pages owned by the
42 offset
= pgoff
<< PAGE_SHIFT
;
43 sb_bindex
= subbuffer_id_get_index(config
, buf
->backend
.buf_rsb
.id
);
44 if (!(offset
>= buf
->backend
.array
[sb_bindex
]->mmap_offset
45 && offset
< buf
->backend
.array
[sb_bindex
]->mmap_offset
+
46 buf
->backend
.chan
->backend
.subbuf_size
))
47 return VM_FAULT_SIGBUS
;
49 * ring_buffer_read_get_pfn() gets the page frame number for the
50 * current reader's pages.
52 pfnp
= lib_ring_buffer_read_get_pfn(&buf
->backend
, offset
, &virt
);
54 return VM_FAULT_SIGBUS
;
55 get_page(pfn_to_page(*pfnp
));
56 vmf
->page
= pfn_to_page(*pfnp
);
61 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
62 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
63 static vm_fault_t
lib_ring_buffer_fault(struct vm_fault
*vmf
)
65 struct vm_area_struct
*vma
= vmf
->vma
;
66 return lib_ring_buffer_fault_compat(vma
, vmf
);
68 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0))
69 static int lib_ring_buffer_fault(struct vm_fault
*vmf
)
71 struct vm_area_struct
*vma
= vmf
->vma
;
72 return lib_ring_buffer_fault_compat(vma
, vmf
);
74 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0)) */
75 static int lib_ring_buffer_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
77 return lib_ring_buffer_fault_compat(vma
, vmf
);
79 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0)) */
82 * vm_ops for ring buffer file mappings.
84 static const struct vm_operations_struct lib_ring_buffer_mmap_ops
= {
85 .fault
= lib_ring_buffer_fault
,
89 * lib_ring_buffer_mmap_buf: - mmap channel buffer to process address space
90 * @buf: ring buffer to map
91 * @vma: vm_area_struct describing memory to be mapped
93 * Returns 0 if ok, negative on error
95 * Caller should already have grabbed mmap_sem.
97 static int lib_ring_buffer_mmap_buf(struct lttng_kernel_ring_buffer
*buf
,
98 struct vm_area_struct
*vma
)
100 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
101 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
102 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
103 unsigned long mmap_buf_len
;
105 if (config
->output
!= RING_BUFFER_MMAP
)
108 mmap_buf_len
= chan
->backend
.buf_size
;
109 if (chan
->backend
.extra_reader_sb
)
110 mmap_buf_len
+= chan
->backend
.subbuf_size
;
112 if (length
!= mmap_buf_len
)
115 vma
->vm_ops
= &lib_ring_buffer_mmap_ops
;
116 vma
->vm_flags
|= VM_DONTEXPAND
;
117 vma
->vm_private_data
= buf
;
122 int lib_ring_buffer_mmap(struct file
*filp
, struct vm_area_struct
*vma
,
123 struct lttng_kernel_ring_buffer
*buf
)
125 return lib_ring_buffer_mmap_buf(buf
, vma
);
127 EXPORT_SYMBOL_GPL(lib_ring_buffer_mmap
);
130 * vfs_lib_ring_buffer_mmap - mmap file op
132 * @vma: the vma describing what to map
134 * Calls upon lib_ring_buffer_mmap_buf() to map the file into user space.
136 int vfs_lib_ring_buffer_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
138 struct lttng_kernel_ring_buffer
*buf
= filp
->private_data
;
139 return lib_ring_buffer_mmap(filp
, vma
, buf
);
141 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_mmap
);