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