fix: mm: introduce vma->vm_flags wrapper functions (v6.3)
[lttng-modules.git] / src / lib / ringbuffer / ring_buffer_mmap.c
1 /* SPDX-License-Identifier: GPL-2.0-only
2 *
3 * ring_buffer_mmap.c
4 *
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>
8 *
9 * Re-using code from kernel/relay.c, hence the GPL-2.0-only license for this
10 * file.
11 */
12
13 #include <linux/module.h>
14 #include <linux/mm.h>
15
16 #include <ringbuffer/backend.h>
17 #include <ringbuffer/frontend.h>
18 #include <ringbuffer/vfs.h>
19
20 #include <wrapper/mm.h>
21
22 /*
23 * fault() vm_op implementation for ring buffer file mapping.
24 */
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)
28 #else
29 static int lib_ring_buffer_fault_compat(struct vm_area_struct *vma, struct vm_fault *vmf)
30 #endif
31 {
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;
36 unsigned long *pfnp;
37 void **virt;
38 unsigned long offset, sb_bindex;
39
40 /*
41 * Verify that faults are only done on the range of pages owned by the
42 * reader.
43 */
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;
50 /*
51 * ring_buffer_read_get_pfn() gets the page frame number for the
52 * current reader's pages.
53 */
54 pfnp = lib_ring_buffer_read_get_pfn(&buf->backend, offset, &virt);
55 if (!*pfnp)
56 return VM_FAULT_SIGBUS;
57 get_page(pfn_to_page(*pfnp));
58 vmf->page = pfn_to_page(*pfnp);
59
60 return 0;
61 }
62
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)
66 {
67 struct vm_area_struct *vma = vmf->vma;
68 return lib_ring_buffer_fault_compat(vma, vmf);
69 }
70 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0))
71 static int lib_ring_buffer_fault(struct vm_fault *vmf)
72 {
73 struct vm_area_struct *vma = vmf->vma;
74 return lib_ring_buffer_fault_compat(vma, vmf);
75 }
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)
78 {
79 return lib_ring_buffer_fault_compat(vma, vmf);
80 }
81 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,11,0)) */
82
83 /*
84 * vm_ops for ring buffer file mappings.
85 */
86 static const struct vm_operations_struct lib_ring_buffer_mmap_ops = {
87 .fault = lib_ring_buffer_fault,
88 };
89
90 /**
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
94 *
95 * Returns 0 if ok, negative on error
96 *
97 * Caller should already have grabbed mmap_sem.
98 */
99 static int lib_ring_buffer_mmap_buf(struct lttng_kernel_ring_buffer *buf,
100 struct vm_area_struct *vma)
101 {
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;
106
107 if (config->output != RING_BUFFER_MMAP)
108 return -EINVAL;
109
110 mmap_buf_len = chan->backend.buf_size;
111 if (chan->backend.extra_reader_sb)
112 mmap_buf_len += chan->backend.subbuf_size;
113
114 if (length != mmap_buf_len)
115 return -EINVAL;
116
117 vma->vm_ops = &lib_ring_buffer_mmap_ops;
118 wrapper_vm_flags_set(vma, VM_DONTEXPAND);
119 vma->vm_private_data = buf;
120
121 return 0;
122 }
123
124 int lib_ring_buffer_mmap(struct file *filp, struct vm_area_struct *vma,
125 struct lttng_kernel_ring_buffer *buf)
126 {
127 return lib_ring_buffer_mmap_buf(buf, vma);
128 }
129 EXPORT_SYMBOL_GPL(lib_ring_buffer_mmap);
130
131 /**
132 * vfs_lib_ring_buffer_mmap - mmap file op
133 * @filp: the file
134 * @vma: the vma describing what to map
135 *
136 * Calls upon lib_ring_buffer_mmap_buf() to map the file into user space.
137 */
138 int vfs_lib_ring_buffer_mmap(struct file *filp, struct vm_area_struct *vma)
139 {
140 struct lttng_kernel_ring_buffer *buf = filp->private_data;
141 return lib_ring_buffer_mmap(filp, vma, buf);
142 }
143 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_mmap);
This page took 0.032365 seconds and 4 git commands to generate.