Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: GPL-2.0-only |
9f36eaed | 2 | * |
f3bc08c5 MD |
3 | * ring_buffer_splice.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> | |
886d51a3 | 7 | * Copyright (C) 2008-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc08c5 | 8 | * |
886d51a3 | 9 | * Re-using code from kernel/relay.c, which is why it is licensed under |
9f36eaed | 10 | * the GPL-2.0. |
f3bc08c5 MD |
11 | */ |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/fs.h> | |
989f58e8 | 15 | #include <linux/version.h> |
f3bc08c5 | 16 | |
c075712b | 17 | #include <wrapper/splice.h> |
24591303 MD |
18 | #include <ringbuffer/backend.h> |
19 | #include <ringbuffer/frontend.h> | |
20 | #include <ringbuffer/vfs.h> | |
f3bc08c5 MD |
21 | |
22 | #if 0 | |
23 | #define printk_dbg(fmt, args...) printk(fmt, args) | |
24 | #else | |
25 | #define printk_dbg(fmt, args...) | |
26 | #endif | |
27 | ||
d83004aa JD |
28 | loff_t vfs_lib_ring_buffer_no_llseek(struct file *file, loff_t offset, |
29 | int origin) | |
f3bc08c5 MD |
30 | { |
31 | return -ESPIPE; | |
32 | } | |
d83004aa | 33 | EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_no_llseek); |
f3bc08c5 MD |
34 | |
35 | /* | |
36 | * Release pages from the buffer so splice pipe_to_file can move them. | |
37 | * Called after the pipe has been populated with buffer pages. | |
38 | */ | |
39 | static void lib_ring_buffer_pipe_buf_release(struct pipe_inode_info *pipe, | |
40 | struct pipe_buffer *pbuf) | |
41 | { | |
42 | __free_page(pbuf->page); | |
43 | } | |
44 | ||
4352d508 MJ |
45 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0)) |
46 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { | |
47 | .release = lib_ring_buffer_pipe_buf_release, | |
48 | .try_steal = generic_pipe_buf_try_steal, | |
49 | .get = generic_pipe_buf_get | |
50 | }; | |
51 | #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)) | |
52 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { | |
53 | .confirm = generic_pipe_buf_confirm, | |
54 | .release = lib_ring_buffer_pipe_buf_release, | |
55 | .steal = generic_pipe_buf_steal, | |
56 | .get = generic_pipe_buf_get | |
57 | }; | |
58 | #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3,15,0)) | |
59 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { | |
60 | .can_merge = 0, | |
61 | .confirm = generic_pipe_buf_confirm, | |
62 | .release = lib_ring_buffer_pipe_buf_release, | |
63 | .steal = generic_pipe_buf_steal, | |
64 | .get = generic_pipe_buf_get | |
65 | }; | |
66 | #else | |
f3bc08c5 MD |
67 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { |
68 | .can_merge = 0, | |
69 | .map = generic_pipe_buf_map, | |
70 | .unmap = generic_pipe_buf_unmap, | |
71 | .confirm = generic_pipe_buf_confirm, | |
72 | .release = lib_ring_buffer_pipe_buf_release, | |
73 | .steal = generic_pipe_buf_steal, | |
4352d508 | 74 | .get = generic_pipe_buf_get |
f3bc08c5 | 75 | }; |
4352d508 | 76 | #endif |
f3bc08c5 MD |
77 | |
78 | /* | |
79 | * Page release operation after splice pipe_to_file ends. | |
80 | */ | |
81 | static void lib_ring_buffer_page_release(struct splice_pipe_desc *spd, | |
82 | unsigned int i) | |
83 | { | |
84 | __free_page(spd->pages[i]); | |
85 | } | |
86 | ||
87 | /* | |
88 | * subbuf_splice_actor - splice up to one subbuf's worth of data | |
89 | */ | |
90 | static int subbuf_splice_actor(struct file *in, | |
91 | loff_t *ppos, | |
92 | struct pipe_inode_info *pipe, | |
93 | size_t len, | |
d83004aa JD |
94 | unsigned int flags, |
95 | struct lib_ring_buffer *buf) | |
f3bc08c5 | 96 | { |
f3bc08c5 | 97 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 98 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
99 | unsigned int poff, subbuf_pages, nr_pages; |
100 | struct page *pages[PIPE_DEF_BUFFERS]; | |
101 | struct partial_page partial[PIPE_DEF_BUFFERS]; | |
102 | struct splice_pipe_desc spd = { | |
103 | .pages = pages, | |
104 | .nr_pages = 0, | |
105 | .partial = partial, | |
e2833bed | 106 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)) |
f3bc08c5 | 107 | .flags = flags, |
e2833bed | 108 | #endif |
f3bc08c5 MD |
109 | .ops = &ring_buffer_pipe_buf_ops, |
110 | .spd_release = lib_ring_buffer_page_release, | |
111 | }; | |
88dfd899 | 112 | unsigned long consumed_old, roffset; |
f3bc08c5 MD |
113 | unsigned long bytes_avail; |
114 | ||
115 | /* | |
116 | * Check that a GET_SUBBUF ioctl has been done before. | |
117 | */ | |
118 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
119 | consumed_old = lib_ring_buffer_get_consumed(config, buf); | |
120 | consumed_old += *ppos; | |
f3bc08c5 MD |
121 | |
122 | /* | |
123 | * Adjust read len, if longer than what is available. | |
124 | * Max read size is 1 subbuffer due to get_subbuf/put_subbuf for | |
125 | * protection. | |
126 | */ | |
127 | bytes_avail = chan->backend.subbuf_size; | |
128 | WARN_ON(bytes_avail > chan->backend.buf_size); | |
129 | len = min_t(size_t, len, bytes_avail); | |
130 | subbuf_pages = bytes_avail >> PAGE_SHIFT; | |
131 | nr_pages = min_t(unsigned int, subbuf_pages, PIPE_DEF_BUFFERS); | |
132 | roffset = consumed_old & PAGE_MASK; | |
133 | poff = consumed_old & ~PAGE_MASK; | |
134 | printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n", | |
135 | len, (ssize_t)*ppos, lib_ring_buffer_get_offset(config, buf)); | |
136 | ||
137 | for (; spd.nr_pages < nr_pages; spd.nr_pages++) { | |
138 | unsigned int this_len; | |
0112cb7b MD |
139 | unsigned long *pfnp, new_pfn; |
140 | struct page *new_page; | |
f3bc08c5 MD |
141 | void **virt; |
142 | ||
143 | if (!len) | |
144 | break; | |
145 | printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n", | |
146 | len, roffset); | |
147 | ||
148 | /* | |
149 | * We have to replace the page we are moving into the splice | |
150 | * pipe. | |
151 | */ | |
152 | new_page = alloc_pages_node(cpu_to_node(max(buf->backend.cpu, | |
153 | 0)), | |
154 | GFP_KERNEL | __GFP_ZERO, 0); | |
155 | if (!new_page) | |
156 | break; | |
0112cb7b | 157 | new_pfn = page_to_pfn(new_page); |
f3bc08c5 | 158 | this_len = PAGE_SIZE - poff; |
0112cb7b MD |
159 | pfnp = lib_ring_buffer_read_get_pfn(&buf->backend, roffset, &virt); |
160 | spd.pages[spd.nr_pages] = pfn_to_page(*pfnp); | |
161 | *pfnp = new_pfn; | |
f3bc08c5 MD |
162 | *virt = page_address(new_page); |
163 | spd.partial[spd.nr_pages].offset = poff; | |
164 | spd.partial[spd.nr_pages].len = this_len; | |
165 | ||
166 | poff = 0; | |
167 | roffset += PAGE_SIZE; | |
168 | len -= this_len; | |
169 | } | |
170 | ||
171 | if (!spd.nr_pages) | |
172 | return 0; | |
173 | ||
90225db5 | 174 | return wrapper_splice_to_pipe(pipe, &spd); |
f3bc08c5 MD |
175 | } |
176 | ||
177 | ssize_t lib_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
178 | struct pipe_inode_info *pipe, size_t len, | |
d83004aa JD |
179 | unsigned int flags, |
180 | struct lib_ring_buffer *buf) | |
f3bc08c5 | 181 | { |
f3bc08c5 | 182 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 183 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
184 | ssize_t spliced; |
185 | int ret; | |
186 | ||
187 | if (config->output != RING_BUFFER_SPLICE) | |
188 | return -EINVAL; | |
189 | ||
190 | /* | |
191 | * We require ppos and length to be page-aligned for performance reasons | |
192 | * (no page copy). Size is known using the ioctl | |
193 | * RING_BUFFER_GET_PADDED_SUBBUF_SIZE, which is page-size padded. | |
194 | * We fail when the ppos or len passed is not page-sized, because splice | |
195 | * is not allowed to copy more than the length passed as parameter (so | |
196 | * the ABI does not let us silently copy more than requested to include | |
197 | * padding). | |
198 | */ | |
199 | if (*ppos != PAGE_ALIGN(*ppos) || len != PAGE_ALIGN(len)) | |
200 | return -EINVAL; | |
201 | ||
202 | ret = 0; | |
203 | spliced = 0; | |
204 | ||
205 | printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n", len, | |
206 | (ssize_t)*ppos); | |
207 | while (len && !spliced) { | |
d83004aa | 208 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags, buf); |
f3bc08c5 MD |
209 | printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret); |
210 | if (ret < 0) | |
211 | break; | |
212 | else if (!ret) { | |
213 | if (flags & SPLICE_F_NONBLOCK) | |
214 | ret = -EAGAIN; | |
215 | break; | |
216 | } | |
217 | ||
218 | *ppos += ret; | |
219 | if (ret > len) | |
220 | len = 0; | |
221 | else | |
222 | len -= ret; | |
223 | spliced += ret; | |
224 | } | |
225 | ||
226 | if (spliced) | |
227 | return spliced; | |
228 | ||
229 | return ret; | |
230 | } | |
231 | EXPORT_SYMBOL_GPL(lib_ring_buffer_splice_read); | |
d83004aa JD |
232 | |
233 | ssize_t vfs_lib_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
234 | struct pipe_inode_info *pipe, size_t len, | |
235 | unsigned int flags) | |
236 | { | |
237 | struct lib_ring_buffer *buf = in->private_data; | |
238 | ||
239 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, flags, buf); | |
240 | } | |
241 | EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_splice_read); |