| 1 | /* SPDX-License-Identifier: GPL-2.0 |
| 2 | * |
| 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> |
| 7 | * Copyright (C) 2008-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Re-using code from kernel/relay.c, which is why it is licensed under |
| 10 | * the GPL-2.0. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <lttng-kernel-version.h> |
| 16 | |
| 17 | #include <wrapper/splice.h> |
| 18 | #include <wrapper/ringbuffer/backend.h> |
| 19 | #include <wrapper/ringbuffer/frontend.h> |
| 20 | #include <wrapper/ringbuffer/vfs.h> |
| 21 | |
| 22 | #if 0 |
| 23 | #define printk_dbg(fmt, args...) printk(fmt, args) |
| 24 | #else |
| 25 | #define printk_dbg(fmt, args...) |
| 26 | #endif |
| 27 | |
| 28 | loff_t vfs_lib_ring_buffer_no_llseek(struct file *file, loff_t offset, |
| 29 | int origin) |
| 30 | { |
| 31 | return -ESPIPE; |
| 32 | } |
| 33 | EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_no_llseek); |
| 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 | |
| 45 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_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 (LTTNG_LINUX_VERSION_CODE >= LTTNG_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 (LTTNG_LINUX_VERSION_CODE >= LTTNG_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 |
| 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, |
| 74 | .get = generic_pipe_buf_get |
| 75 | }; |
| 76 | #endif |
| 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, |
| 94 | unsigned int flags, |
| 95 | struct lib_ring_buffer *buf) |
| 96 | { |
| 97 | struct channel *chan = buf->backend.chan; |
| 98 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 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, |
| 106 | #if (LTTNG_LINUX_VERSION_CODE < LTTNG_KERNEL_VERSION(4,12,0)) |
| 107 | .flags = flags, |
| 108 | #endif |
| 109 | .ops = &ring_buffer_pipe_buf_ops, |
| 110 | .spd_release = lib_ring_buffer_page_release, |
| 111 | }; |
| 112 | unsigned long consumed_old, roffset; |
| 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; |
| 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; |
| 139 | unsigned long *pfnp, new_pfn; |
| 140 | struct page *new_page; |
| 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; |
| 157 | new_pfn = page_to_pfn(new_page); |
| 158 | this_len = PAGE_SIZE - poff; |
| 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; |
| 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 | |
| 174 | return wrapper_splice_to_pipe(pipe, &spd); |
| 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, |
| 179 | unsigned int flags, |
| 180 | struct lib_ring_buffer *buf) |
| 181 | { |
| 182 | struct channel *chan = buf->backend.chan; |
| 183 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 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) { |
| 208 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags, buf); |
| 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); |
| 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); |