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>
20 #include <wrapper/mm.h>
23 * fault() vm_op implementation for ring buffer file mapping.
25 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
26 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
27 static vm_fault_t
lib_ring_buffer_fault_compat(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
29 static int lib_ring_buffer_fault_compat(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
32 struct lttng_kernel_ring_buffer
*buf
= vma
->vm_private_data
;
33 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
34 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
35 pgoff_t pgoff
= vmf
->pgoff
;
38 unsigned long offset
, sb_bindex
;
41 * Verify that faults are only done on the range of pages owned by the
44 offset
= pgoff
<< PAGE_SHIFT
;
45 sb_bindex
= subbuffer_id_get_index(config
, buf
->backend
.buf_rsb
.id
);
46 if (!(offset
>= buf
->backend
.array
[sb_bindex
]->mmap_offset
47 && offset
< buf
->backend
.array
[sb_bindex
]->mmap_offset
+
48 buf
->backend
.chan
->backend
.subbuf_size
))
49 return VM_FAULT_SIGBUS
;
51 * ring_buffer_read_get_pfn() gets the page frame number for the
52 * current reader's pages.
54 pfnp
= lib_ring_buffer_read_get_pfn(&buf
->backend
, offset
, &virt
);
56 return VM_FAULT_SIGBUS
;
57 get_page(pfn_to_page(*pfnp
));
58 vmf
->page
= pfn_to_page(*pfnp
);
63 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,1,0) || \
64 LTTNG_RHEL_KERNEL_RANGE(4,18,0,193,0,0, 4,19,0,0,0,0))
65 static vm_fault_t
lib_ring_buffer_fault(struct vm_fault
*vmf
)
67 struct vm_area_struct
*vma
= vmf
->vma
;
68 return lib_ring_buffer_fault_compat(vma
, vmf
);
70 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0))
71 static int lib_ring_buffer_fault(struct vm_fault
*vmf
)
73 struct vm_area_struct
*vma
= vmf
->vma
;
74 return lib_ring_buffer_fault_compat(vma
, vmf
);
76 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0)) */
77 static int lib_ring_buffer_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
79 return lib_ring_buffer_fault_compat(vma
, vmf
);
81 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0)) */
84 * vm_ops for ring buffer file mappings.
86 static const struct vm_operations_struct lib_ring_buffer_mmap_ops
= {
87 .fault
= lib_ring_buffer_fault
,
91 * lib_ring_buffer_mmap_buf: - mmap channel buffer to process address space
92 * @buf: ring buffer to map
93 * @vma: vm_area_struct describing memory to be mapped
95 * Returns 0 if ok, negative on error
97 * Caller should already have grabbed mmap_sem.
99 static int lib_ring_buffer_mmap_buf(struct lttng_kernel_ring_buffer
*buf
,
100 struct vm_area_struct
*vma
)
102 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
103 struct lttng_kernel_ring_buffer_channel
*chan
= buf
->backend
.chan
;
104 const struct lttng_kernel_ring_buffer_config
*config
= &chan
->backend
.config
;
105 unsigned long mmap_buf_len
;
107 if (config
->output
!= RING_BUFFER_MMAP
)
110 mmap_buf_len
= chan
->backend
.buf_size
;
111 if (chan
->backend
.extra_reader_sb
)
112 mmap_buf_len
+= chan
->backend
.subbuf_size
;
114 if (length
!= mmap_buf_len
)
117 vma
->vm_ops
= &lib_ring_buffer_mmap_ops
;
118 wrapper_vm_flags_set(vma
, VM_DONTEXPAND
);
119 vma
->vm_private_data
= buf
;
124 int lib_ring_buffer_mmap(struct file
*filp
, struct vm_area_struct
*vma
,
125 struct lttng_kernel_ring_buffer
*buf
)
127 return lib_ring_buffer_mmap_buf(buf
, vma
);
129 EXPORT_SYMBOL_GPL(lib_ring_buffer_mmap
);
132 * vfs_lib_ring_buffer_mmap - mmap file op
134 * @vma: the vma describing what to map
136 * Calls upon lib_ring_buffer_mmap_buf() to map the file into user space.
138 int vfs_lib_ring_buffer_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
140 struct lttng_kernel_ring_buffer
*buf
= filp
->private_data
;
141 return lib_ring_buffer_mmap(filp
, vma
, buf
);
143 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_mmap
);