Cleanup: move to kernel style SPDX license identifiers
[lttng-modules.git] / lib / ringbuffer / ring_buffer_mmap.c
1 /* SPDX-License-Identifier: GPL-2.0
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 license for this
10 * file.
11 */
12
13 #include <linux/module.h>
14 #include <linux/mm.h>
15
16 #include <wrapper/ringbuffer/backend.h>
17 #include <wrapper/ringbuffer/frontend.h>
18 #include <wrapper/ringbuffer/vfs.h>
19
20 /*
21 * fault() vm_op implementation for ring buffer file mapping.
22 */
23 static int lib_ring_buffer_fault_compat(struct vm_area_struct *vma, struct vm_fault *vmf)
24 {
25 struct lib_ring_buffer *buf = vma->vm_private_data;
26 struct channel *chan = buf->backend.chan;
27 const struct lib_ring_buffer_config *config = &chan->backend.config;
28 pgoff_t pgoff = vmf->pgoff;
29 unsigned long *pfnp;
30 void **virt;
31 unsigned long offset, sb_bindex;
32
33 /*
34 * Verify that faults are only done on the range of pages owned by the
35 * reader.
36 */
37 offset = pgoff << PAGE_SHIFT;
38 sb_bindex = subbuffer_id_get_index(config, buf->backend.buf_rsb.id);
39 if (!(offset >= buf->backend.array[sb_bindex]->mmap_offset
40 && offset < buf->backend.array[sb_bindex]->mmap_offset +
41 buf->backend.chan->backend.subbuf_size))
42 return VM_FAULT_SIGBUS;
43 /*
44 * ring_buffer_read_get_pfn() gets the page frame number for the
45 * current reader's pages.
46 */
47 pfnp = lib_ring_buffer_read_get_pfn(&buf->backend, offset, &virt);
48 if (!*pfnp)
49 return VM_FAULT_SIGBUS;
50 get_page(pfn_to_page(*pfnp));
51 vmf->page = pfn_to_page(*pfnp);
52
53 return 0;
54 }
55
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0))
57 static int lib_ring_buffer_fault(struct vm_fault *vmf)
58 {
59 struct vm_area_struct *vma = vmf->vma;
60 return lib_ring_buffer_fault_compat(vma, vmf);
61 }
62 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)) */
63 static int lib_ring_buffer_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
64 {
65 return lib_ring_buffer_fault_compat(vma, vmf);
66 }
67 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)) */
68
69 /*
70 * vm_ops for ring buffer file mappings.
71 */
72 static const struct vm_operations_struct lib_ring_buffer_mmap_ops = {
73 .fault = lib_ring_buffer_fault,
74 };
75
76 /**
77 * lib_ring_buffer_mmap_buf: - mmap channel buffer to process address space
78 * @buf: ring buffer to map
79 * @vma: vm_area_struct describing memory to be mapped
80 *
81 * Returns 0 if ok, negative on error
82 *
83 * Caller should already have grabbed mmap_sem.
84 */
85 static int lib_ring_buffer_mmap_buf(struct lib_ring_buffer *buf,
86 struct vm_area_struct *vma)
87 {
88 unsigned long length = vma->vm_end - vma->vm_start;
89 struct channel *chan = buf->backend.chan;
90 const struct lib_ring_buffer_config *config = &chan->backend.config;
91 unsigned long mmap_buf_len;
92
93 if (config->output != RING_BUFFER_MMAP)
94 return -EINVAL;
95
96 mmap_buf_len = chan->backend.buf_size;
97 if (chan->backend.extra_reader_sb)
98 mmap_buf_len += chan->backend.subbuf_size;
99
100 if (length != mmap_buf_len)
101 return -EINVAL;
102
103 vma->vm_ops = &lib_ring_buffer_mmap_ops;
104 vma->vm_flags |= VM_DONTEXPAND;
105 vma->vm_private_data = buf;
106
107 return 0;
108 }
109
110 int lib_ring_buffer_mmap(struct file *filp, struct vm_area_struct *vma,
111 struct lib_ring_buffer *buf)
112 {
113 return lib_ring_buffer_mmap_buf(buf, vma);
114 }
115 EXPORT_SYMBOL_GPL(lib_ring_buffer_mmap);
116
117 /**
118 * vfs_lib_ring_buffer_mmap - mmap file op
119 * @filp: the file
120 * @vma: the vma describing what to map
121 *
122 * Calls upon lib_ring_buffer_mmap_buf() to map the file into user space.
123 */
124 int vfs_lib_ring_buffer_mmap(struct file *filp, struct vm_area_struct *vma)
125 {
126 struct lib_ring_buffer *buf = filp->private_data;
127 return lib_ring_buffer_mmap(filp, vma, buf);
128 }
129 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_mmap);
This page took 0.030855 seconds and 4 git commands to generate.