Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / lttng / 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
29 #include "../../wrapper/splice.h"
30 #include "../../wrapper/ringbuffer/backend.h"
31 #include "../../wrapper/ringbuffer/frontend.h"
32 #include "../../wrapper/ringbuffer/vfs.h"
33
34 #if 0
35 #define printk_dbg(fmt, args...) printk(fmt, args)
36 #else
37 #define printk_dbg(fmt, args...)
38 #endif
39
40 loff_t vfs_lib_ring_buffer_no_llseek(struct file *file, loff_t offset,
41                 int origin)
42 {
43         return -ESPIPE;
44 }
45 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_no_llseek);
46
47 /*
48  * Release pages from the buffer so splice pipe_to_file can move them.
49  * Called after the pipe has been populated with buffer pages.
50  */
51 static void lib_ring_buffer_pipe_buf_release(struct pipe_inode_info *pipe,
52                                              struct pipe_buffer *pbuf)
53 {
54         __free_page(pbuf->page);
55 }
56
57 static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = {
58         .can_merge = 0,
59         .map = generic_pipe_buf_map,
60         .unmap = generic_pipe_buf_unmap,
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
67 /*
68  * Page release operation after splice pipe_to_file ends.
69  */
70 static void lib_ring_buffer_page_release(struct splice_pipe_desc *spd,
71                                          unsigned int i)
72 {
73         __free_page(spd->pages[i]);
74 }
75
76 /*
77  *      subbuf_splice_actor - splice up to one subbuf's worth of data
78  */
79 static int subbuf_splice_actor(struct file *in,
80                                loff_t *ppos,
81                                struct pipe_inode_info *pipe,
82                                size_t len,
83                                unsigned int flags,
84                                struct lib_ring_buffer *buf)
85 {
86         struct channel *chan = buf->backend.chan;
87         const struct lib_ring_buffer_config *config = &chan->backend.config;
88         unsigned int poff, subbuf_pages, nr_pages;
89         struct page *pages[PIPE_DEF_BUFFERS];
90         struct partial_page partial[PIPE_DEF_BUFFERS];
91         struct splice_pipe_desc spd = {
92                 .pages = pages,
93                 .nr_pages = 0,
94                 .partial = partial,
95                 .flags = flags,
96                 .ops = &ring_buffer_pipe_buf_ops,
97                 .spd_release = lib_ring_buffer_page_release,
98         };
99         unsigned long consumed_old, roffset;
100         unsigned long bytes_avail;
101
102         /*
103          * Check that a GET_SUBBUF ioctl has been done before.
104          */
105         WARN_ON(atomic_long_read(&buf->active_readers) != 1);
106         consumed_old = lib_ring_buffer_get_consumed(config, buf);
107         consumed_old += *ppos;
108
109         /*
110          * Adjust read len, if longer than what is available.
111          * Max read size is 1 subbuffer due to get_subbuf/put_subbuf for
112          * protection.
113          */
114         bytes_avail = chan->backend.subbuf_size;
115         WARN_ON(bytes_avail > chan->backend.buf_size);
116         len = min_t(size_t, len, bytes_avail);
117         subbuf_pages = bytes_avail >> PAGE_SHIFT;
118         nr_pages = min_t(unsigned int, subbuf_pages, PIPE_DEF_BUFFERS);
119         roffset = consumed_old & PAGE_MASK;
120         poff = consumed_old & ~PAGE_MASK;
121         printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
122                    len, (ssize_t)*ppos, lib_ring_buffer_get_offset(config, buf));
123
124         for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
125                 unsigned int this_len;
126                 struct page **page, *new_page;
127                 void **virt;
128
129                 if (!len)
130                         break;
131                 printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
132                            len, roffset);
133
134                 /*
135                  * We have to replace the page we are moving into the splice
136                  * pipe.
137                  */
138                 new_page = alloc_pages_node(cpu_to_node(max(buf->backend.cpu,
139                                                             0)),
140                                             GFP_KERNEL | __GFP_ZERO, 0);
141                 if (!new_page)
142                         break;
143
144                 this_len = PAGE_SIZE - poff;
145                 page = lib_ring_buffer_read_get_page(&buf->backend, roffset, &virt);
146                 spd.pages[spd.nr_pages] = *page;
147                 *page = new_page;
148                 *virt = page_address(new_page);
149                 spd.partial[spd.nr_pages].offset = poff;
150                 spd.partial[spd.nr_pages].len = this_len;
151
152                 poff = 0;
153                 roffset += PAGE_SIZE;
154                 len -= this_len;
155         }
156
157         if (!spd.nr_pages)
158                 return 0;
159
160         return wrapper_splice_to_pipe(pipe, &spd);
161 }
162
163 ssize_t lib_ring_buffer_splice_read(struct file *in, loff_t *ppos,
164                                     struct pipe_inode_info *pipe, size_t len,
165                                     unsigned int flags,
166                                     struct lib_ring_buffer *buf)
167 {
168         struct channel *chan = buf->backend.chan;
169         const struct lib_ring_buffer_config *config = &chan->backend.config;
170         ssize_t spliced;
171         int ret;
172
173         if (config->output != RING_BUFFER_SPLICE)
174                 return -EINVAL;
175
176         /*
177          * We require ppos and length to be page-aligned for performance reasons
178          * (no page copy). Size is known using the ioctl
179          * RING_BUFFER_GET_PADDED_SUBBUF_SIZE, which is page-size padded.
180          * We fail when the ppos or len passed is not page-sized, because splice
181          * is not allowed to copy more than the length passed as parameter (so
182          * the ABI does not let us silently copy more than requested to include
183          * padding).
184          */
185         if (*ppos != PAGE_ALIGN(*ppos) || len != PAGE_ALIGN(len))
186                 return -EINVAL;
187
188         ret = 0;
189         spliced = 0;
190
191         printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n", len,
192                    (ssize_t)*ppos);
193         while (len && !spliced) {
194                 ret = subbuf_splice_actor(in, ppos, pipe, len, flags, buf);
195                 printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
196                 if (ret < 0)
197                         break;
198                 else if (!ret) {
199                         if (flags & SPLICE_F_NONBLOCK)
200                                 ret = -EAGAIN;
201                         break;
202                 }
203
204                 *ppos += ret;
205                 if (ret > len)
206                         len = 0;
207                 else
208                         len -= ret;
209                 spliced += ret;
210         }
211
212         if (spliced)
213                 return spliced;
214
215         return ret;
216 }
217 EXPORT_SYMBOL_GPL(lib_ring_buffer_splice_read);
218
219 ssize_t vfs_lib_ring_buffer_splice_read(struct file *in, loff_t *ppos,
220                                     struct pipe_inode_info *pipe, size_t len,
221                                     unsigned int flags)
222 {
223         struct lib_ring_buffer *buf = in->private_data;
224
225         return lib_ring_buffer_splice_read(in, ppos, pipe, len, flags, buf);
226 }
227 EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_splice_read);