blob: d1a67fbb819d3e774be0542faea31292920aa903 [file] [log] [blame]
Jens Axboeb86ff9812006-03-23 19:56:55 +01001/*
2 * Public API and common code for kernel->userspace relay file support.
3 *
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02004 * See Documentation/filesystems/relay.rst for an overview.
Jens Axboeb86ff9812006-03-23 19:56:55 +01005 *
6 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
8 *
9 * Moved to kernel/relay.c by Paul Mundt, 2006.
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080010 * November 2006 - CPU hotplug support by Mathieu Desnoyers
11 * (mathieu.desnoyers@polymtl.ca)
Jens Axboeb86ff9812006-03-23 19:56:55 +010012 *
13 * This file is released under the GPL.
14 */
15#include <linux/errno.h>
16#include <linux/stddef.h>
17#include <linux/slab.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040018#include <linux/export.h>
Jens Axboeb86ff9812006-03-23 19:56:55 +010019#include <linux/string.h>
20#include <linux/relay.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080023#include <linux/cpu.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020024#include <linux/splice.h>
Mathieu Desnoyers23c88752007-02-10 01:45:05 -080025
26/* list of open channels, for cpu hotplug */
27static DEFINE_MUTEX(relay_channels_mutex);
28static LIST_HEAD(relay_channels);
Jens Axboeb86ff9812006-03-23 19:56:55 +010029
30/*
Nick Piggina1e09612008-02-06 01:37:34 -080031 * fault() vm_op implementation for relay file mapping.
Jens Axboeb86ff9812006-03-23 19:56:55 +010032 */
Souptick Joarder3fb38942018-06-14 15:27:31 -070033static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
Jens Axboeb86ff9812006-03-23 19:56:55 +010034{
35 struct page *page;
Dave Jiang11bac802017-02-24 14:56:41 -080036 struct rchan_buf *buf = vmf->vma->vm_private_data;
Nick Piggina1e09612008-02-06 01:37:34 -080037 pgoff_t pgoff = vmf->pgoff;
Jens Axboeb86ff9812006-03-23 19:56:55 +010038
Jens Axboeb86ff9812006-03-23 19:56:55 +010039 if (!buf)
Nick Piggina1e09612008-02-06 01:37:34 -080040 return VM_FAULT_OOM;
Jens Axboeb86ff9812006-03-23 19:56:55 +010041
Nick Piggina1e09612008-02-06 01:37:34 -080042 page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
Jens Axboeb86ff9812006-03-23 19:56:55 +010043 if (!page)
Nick Piggina1e09612008-02-06 01:37:34 -080044 return VM_FAULT_SIGBUS;
Jens Axboeb86ff9812006-03-23 19:56:55 +010045 get_page(page);
Nick Piggina1e09612008-02-06 01:37:34 -080046 vmf->page = page;
Jens Axboeb86ff9812006-03-23 19:56:55 +010047
Nick Piggina1e09612008-02-06 01:37:34 -080048 return 0;
Jens Axboeb86ff9812006-03-23 19:56:55 +010049}
50
51/*
52 * vm_ops for relay file mappings.
53 */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040054static const struct vm_operations_struct relay_file_mmap_ops = {
Nick Piggina1e09612008-02-06 01:37:34 -080055 .fault = relay_buf_fault,
Jens Axboeb86ff9812006-03-23 19:56:55 +010056};
57
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -070058/*
59 * allocate an array of pointers of struct page
60 */
61static struct page **relay_alloc_page_array(unsigned int n_pages)
62{
Jesper Juhl408af872010-11-04 21:44:41 +010063 const size_t pa_size = n_pages * sizeof(struct page *);
64 if (pa_size > PAGE_SIZE)
65 return vzalloc(pa_size);
66 return kzalloc(pa_size, GFP_KERNEL);
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -070067}
68
69/*
70 * free an array of pointers of struct page
71 */
72static void relay_free_page_array(struct page **array)
73{
Pekka Enberg200f1ce2015-06-30 14:59:06 -070074 kvfree(array);
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -070075}
76
Jens Axboeb86ff9812006-03-23 19:56:55 +010077/**
78 * relay_mmap_buf: - mmap channel buffer to process address space
79 * @buf: relay channel buffer
80 * @vma: vm_area_struct describing memory to be mapped
81 *
82 * Returns 0 if ok, negative on error
83 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070084 * Caller should already have grabbed mmap_lock.
Jens Axboeb86ff9812006-03-23 19:56:55 +010085 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -070086static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
Jens Axboeb86ff9812006-03-23 19:56:55 +010087{
88 unsigned long length = vma->vm_end - vma->vm_start;
Jens Axboeb86ff9812006-03-23 19:56:55 +010089
90 if (!buf)
91 return -EBADF;
92
93 if (length != (unsigned long)buf->chan->alloc_size)
94 return -EINVAL;
95
96 vma->vm_ops = &relay_file_mmap_ops;
Nick Piggin2f987352008-02-02 03:08:53 +010097 vma->vm_flags |= VM_DONTEXPAND;
Jens Axboeb86ff9812006-03-23 19:56:55 +010098 vma->vm_private_data = buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +010099
100 return 0;
101}
102
103/**
104 * relay_alloc_buf - allocate a channel buffer
105 * @buf: the buffer struct
106 * @size: total size of the buffer
107 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700108 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
Jens Axboe221415d2006-03-23 19:57:55 +0100109 * passed in size will get page aligned, if it isn't already.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100110 */
Jens Axboe221415d2006-03-23 19:57:55 +0100111static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100112{
113 void *mem;
114 unsigned int i, j, n_pages;
115
Jens Axboe221415d2006-03-23 19:57:55 +0100116 *size = PAGE_ALIGN(*size);
117 n_pages = *size >> PAGE_SHIFT;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100118
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -0700119 buf->page_array = relay_alloc_page_array(n_pages);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100120 if (!buf->page_array)
121 return NULL;
122
123 for (i = 0; i < n_pages; i++) {
124 buf->page_array[i] = alloc_page(GFP_KERNEL);
125 if (unlikely(!buf->page_array[i]))
126 goto depopulate;
Tom Zanussiebf99092007-06-04 09:12:05 +0200127 set_page_private(buf->page_array[i], (unsigned long)buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100128 }
129 mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
130 if (!mem)
131 goto depopulate;
132
Jens Axboe221415d2006-03-23 19:57:55 +0100133 memset(mem, 0, *size);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100134 buf->page_count = n_pages;
135 return mem;
136
137depopulate:
138 for (j = 0; j < i; j++)
139 __free_page(buf->page_array[j]);
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -0700140 relay_free_page_array(buf->page_array);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100141 return NULL;
142}
143
144/**
145 * relay_create_buf - allocate and initialize a channel buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700146 * @chan: the relay channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100147 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700148 * Returns channel buffer if successful, %NULL otherwise.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100149 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700150static struct rchan_buf *relay_create_buf(struct rchan *chan)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100151{
Dan Carpenterf6302f12012-02-10 09:03:58 +0100152 struct rchan_buf *buf;
153
David Rientjes88913bd2018-02-21 14:45:32 -0800154 if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *))
Jens Axboeb86ff9812006-03-23 19:56:55 +0100155 return NULL;
156
Dan Carpenterf6302f12012-02-10 09:03:58 +0100157 buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
158 if (!buf)
159 return NULL;
Kees Cook6da2ec52018-06-12 13:55:00 -0700160 buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *),
161 GFP_KERNEL);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100162 if (!buf->padding)
163 goto free_buf;
164
Jens Axboe221415d2006-03-23 19:57:55 +0100165 buf->start = relay_alloc_buf(buf, &chan->alloc_size);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100166 if (!buf->start)
167 goto free_buf;
168
169 buf->chan = chan;
170 kref_get(&buf->chan->kref);
171 return buf;
172
173free_buf:
174 kfree(buf->padding);
175 kfree(buf);
176 return NULL;
177}
178
179/**
180 * relay_destroy_channel - free the channel struct
Randy Dunlap4c78a662006-09-29 01:59:10 -0700181 * @kref: target kernel reference that contains the relay channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100182 *
183 * Should only be called from kref_put().
184 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700185static void relay_destroy_channel(struct kref *kref)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100186{
187 struct rchan *chan = container_of(kref, struct rchan, kref);
Wei Yongjun71e84322020-08-20 17:42:14 -0700188 free_percpu(chan->buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100189 kfree(chan);
190}
191
192/**
193 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
194 * @buf: the buffer struct
195 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700196static void relay_destroy_buf(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100197{
198 struct rchan *chan = buf->chan;
199 unsigned int i;
200
201 if (likely(buf->start)) {
202 vunmap(buf->start);
203 for (i = 0; i < buf->page_count; i++)
204 __free_page(buf->page_array[i]);
Masami Hiramatsu68ab3d82008-04-29 01:03:46 -0700205 relay_free_page_array(buf->page_array);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100206 }
Akash Goel017c59c2016-09-02 21:47:38 +0200207 *per_cpu_ptr(chan->buf, buf->cpu) = NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100208 kfree(buf->padding);
209 kfree(buf);
210 kref_put(&chan->kref, relay_destroy_channel);
211}
212
213/**
214 * relay_remove_buf - remove a channel buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700215 * @kref: target kernel reference that contains the relay buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100216 *
Masanari Iidae2278672014-02-18 22:54:36 +0900217 * Removes the file from the filesystem, which also frees the
Jens Axboeb86ff9812006-03-23 19:56:55 +0100218 * rchan_buf_struct and the channel buffer. Should only be called from
219 * kref_put().
220 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700221static void relay_remove_buf(struct kref *kref)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100222{
223 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100224 relay_destroy_buf(buf);
225}
226
227/**
228 * relay_buf_empty - boolean, is the channel buffer empty?
229 * @buf: channel buffer
230 *
231 * Returns 1 if the buffer is empty, 0 otherwise.
232 */
Adrian Bunk01c55ed2007-07-19 01:48:32 -0700233static int relay_buf_empty(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100234{
235 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
236}
Jens Axboeb86ff9812006-03-23 19:56:55 +0100237
238/**
239 * relay_buf_full - boolean, is the channel buffer full?
240 * @buf: channel buffer
241 *
242 * Returns 1 if the buffer is full, 0 otherwise.
243 */
244int relay_buf_full(struct rchan_buf *buf)
245{
246 size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
247 return (ready >= buf->chan->n_subbufs) ? 1 : 0;
248}
249EXPORT_SYMBOL_GPL(relay_buf_full);
250
251/*
252 * High-level relay kernel API and associated functions.
253 */
254
Jani Nikula023542f2020-12-15 20:45:57 -0800255static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
256 void *prev_subbuf, size_t prev_padding)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100257{
Jani Nikula023542f2020-12-15 20:45:57 -0800258 if (!buf->chan->cb->subbuf_start)
259 return !relay_buf_full(buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100260
Jani Nikula023542f2020-12-15 20:45:57 -0800261 return buf->chan->cb->subbuf_start(buf, subbuf,
262 prev_subbuf, prev_padding);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100263}
264
Jens Axboeb86ff9812006-03-23 19:56:55 +0100265/**
266 * wakeup_readers - wake up readers waiting on a channel
Peter Zijlstra26b56792016-10-11 13:54:33 -0700267 * @work: contains the channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100268 *
Peter Zijlstra26b56792016-10-11 13:54:33 -0700269 * This is the function used to defer reader waking
Jens Axboeb86ff9812006-03-23 19:56:55 +0100270 */
Peter Zijlstra26b56792016-10-11 13:54:33 -0700271static void wakeup_readers(struct irq_work *work)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100272{
Peter Zijlstra26b56792016-10-11 13:54:33 -0700273 struct rchan_buf *buf;
274
275 buf = container_of(work, struct rchan_buf, wakeup_work);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100276 wake_up_interruptible(&buf->read_wait);
277}
278
279/**
280 * __relay_reset - reset a channel buffer
281 * @buf: the channel buffer
282 * @init: 1 if this is a first-time initialization
283 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800284 * See relay_reset() for description of effect.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100285 */
Andrew Morton192636a2006-12-22 01:11:30 -0800286static void __relay_reset(struct rchan_buf *buf, unsigned int init)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100287{
288 size_t i;
289
290 if (init) {
291 init_waitqueue_head(&buf->read_wait);
292 kref_init(&buf->kref);
Peter Zijlstra26b56792016-10-11 13:54:33 -0700293 init_irq_work(&buf->wakeup_work, wakeup_readers);
294 } else {
295 irq_work_sync(&buf->wakeup_work);
296 }
Jens Axboeb86ff9812006-03-23 19:56:55 +0100297
298 buf->subbufs_produced = 0;
299 buf->subbufs_consumed = 0;
300 buf->bytes_consumed = 0;
301 buf->finalized = 0;
302 buf->data = buf->start;
303 buf->offset = 0;
304
305 for (i = 0; i < buf->chan->n_subbufs; i++)
306 buf->padding[i] = 0;
307
Jani Nikula023542f2020-12-15 20:45:57 -0800308 relay_subbuf_start(buf, buf->data, NULL, 0);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100309}
310
311/**
312 * relay_reset - reset the channel
313 * @chan: the channel
314 *
315 * This has the effect of erasing all data from all channel buffers
316 * and restarting the channel in its initial state. The buffers
317 * are not freed, so any mappings are still in effect.
318 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800319 * NOTE. Care should be taken that the channel isn't actually
Jens Axboeb86ff9812006-03-23 19:56:55 +0100320 * being used by anything when this call is made.
321 */
322void relay_reset(struct rchan *chan)
323{
Akash Goel017c59c2016-09-02 21:47:38 +0200324 struct rchan_buf *buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100325 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100326
327 if (!chan)
328 return;
329
Akash Goel017c59c2016-09-02 21:47:38 +0200330 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
331 __relay_reset(buf, 0);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800332 return;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100333 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800334
335 mutex_lock(&relay_channels_mutex);
Lai Jiangshan98ba4032008-11-14 10:44:59 +0100336 for_each_possible_cpu(i)
Akash Goel017c59c2016-09-02 21:47:38 +0200337 if ((buf = *per_cpu_ptr(chan->buf, i)))
338 __relay_reset(buf, 0);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800339 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100340}
341EXPORT_SYMBOL_GPL(relay_reset);
342
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700343static inline void relay_set_buf_dentry(struct rchan_buf *buf,
344 struct dentry *dentry)
345{
346 buf->dentry = dentry;
David Howells7682c912015-03-17 22:26:16 +0000347 d_inode(buf->dentry)->i_size = buf->early_bytes;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700348}
349
350static struct dentry *relay_create_buf_file(struct rchan *chan,
351 struct rchan_buf *buf,
352 unsigned int cpu)
353{
354 struct dentry *dentry;
355 char *tmpname;
356
357 tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
358 if (!tmpname)
359 return NULL;
360 snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
361
362 /* Create file in fs */
363 dentry = chan->cb->create_buf_file(tmpname, chan->parent,
364 S_IRUSR, buf,
365 &chan->is_global);
Greg Kroah-Hartman2c1cf002019-01-31 13:57:58 +0100366 if (IS_ERR(dentry))
367 dentry = NULL;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700368
369 kfree(tmpname);
370
371 return dentry;
372}
373
Randy Dunlap4c78a662006-09-29 01:59:10 -0700374/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100375 * relay_open_buf - create a new relay channel buffer
376 *
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800377 * used by relay_open() and CPU hotplug.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100378 */
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800379static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100380{
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800381 struct rchan_buf *buf = NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100382 struct dentry *dentry;
383
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800384 if (chan->is_global)
Akash Goel017c59c2016-09-02 21:47:38 +0200385 return *per_cpu_ptr(chan->buf, 0);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100386
387 buf = relay_create_buf(chan);
388 if (!buf)
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700389 return NULL;
390
391 if (chan->has_base_filename) {
392 dentry = relay_create_buf_file(chan, buf, cpu);
393 if (!dentry)
394 goto free_buf;
395 relay_set_buf_dentry(buf, dentry);
Akash Goel59dbb2a2016-08-02 14:07:18 -0700396 } else {
397 /* Only retrieve global info, nothing more, nothing less */
398 dentry = chan->cb->create_buf_file(NULL, NULL,
399 S_IRUSR, buf,
400 &chan->is_global);
Greg Kroah-Hartman2c1cf002019-01-31 13:57:58 +0100401 if (IS_ERR_OR_NULL(dentry))
Akash Goel59dbb2a2016-08-02 14:07:18 -0700402 goto free_buf;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700403 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800404
405 buf->cpu = cpu;
406 __relay_reset(buf, 1);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100407
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800408 if(chan->is_global) {
Akash Goel017c59c2016-09-02 21:47:38 +0200409 *per_cpu_ptr(chan->buf, 0) = buf;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800410 buf->cpu = 0;
411 }
412
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700413 return buf;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800414
415free_buf:
416 relay_destroy_buf(buf);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700417 return NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100418}
419
420/**
421 * relay_close_buf - close a channel buffer
422 * @buf: channel buffer
423 *
424 * Marks the buffer finalized and restores the default callbacks.
425 * The channel buffer and channel buffer data structure are then freed
426 * automatically when the last reference is given up.
427 */
Andrew Morton192636a2006-12-22 01:11:30 -0800428static void relay_close_buf(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100429{
430 buf->finalized = 1;
Peter Zijlstra26b56792016-10-11 13:54:33 -0700431 irq_work_sync(&buf->wakeup_work);
Dmitry Monakhovb8d4a5b2013-04-22 11:41:41 +0400432 buf->chan->cb->remove_buf_file(buf->dentry);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100433 kref_put(&buf->kref, relay_remove_buf);
434}
435
Richard Weinbergere6d49892016-08-18 14:57:17 +0200436int relay_prepare_cpu(unsigned int cpu)
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800437{
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800438 struct rchan *chan;
Akash Goel017c59c2016-09-02 21:47:38 +0200439 struct rchan_buf *buf;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800440
Richard Weinbergere6d49892016-08-18 14:57:17 +0200441 mutex_lock(&relay_channels_mutex);
442 list_for_each_entry(chan, &relay_channels, list) {
443 if ((buf = *per_cpu_ptr(chan->buf, cpu)))
444 continue;
445 buf = relay_open_buf(chan, cpu);
446 if (!buf) {
447 pr_err("relay: cpu %d buffer creation failed\n", cpu);
448 mutex_unlock(&relay_channels_mutex);
449 return -ENOMEM;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800450 }
Richard Weinbergere6d49892016-08-18 14:57:17 +0200451 *per_cpu_ptr(chan->buf, cpu) = buf;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800452 }
Richard Weinbergere6d49892016-08-18 14:57:17 +0200453 mutex_unlock(&relay_channels_mutex);
454 return 0;
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800455}
456
457/**
Jens Axboeb86ff9812006-03-23 19:56:55 +0100458 * relay_open - create a new relay channel
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700459 * @base_filename: base name of files to create, %NULL for buffering only
460 * @parent: dentry of parent directory, %NULL for root directory or buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100461 * @subbuf_size: size of sub-buffers
462 * @n_subbufs: number of sub-buffers
463 * @cb: client callback functions
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800464 * @private_data: user-defined data
Jens Axboeb86ff9812006-03-23 19:56:55 +0100465 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700466 * Returns channel pointer if successful, %NULL otherwise.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100467 *
468 * Creates a channel buffer for each cpu using the sizes and
469 * attributes specified. The created channel buffer files
470 * will be named base_filename0...base_filenameN-1. File
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800471 * permissions will be %S_IRUSR.
Akash Goel59dbb2a2016-08-02 14:07:18 -0700472 *
473 * If opening a buffer (@parent = NULL) that you later wish to register
474 * in a filesystem, call relay_late_setup_files() once the @parent dentry
475 * is available.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100476 */
477struct rchan *relay_open(const char *base_filename,
478 struct dentry *parent,
479 size_t subbuf_size,
480 size_t n_subbufs,
Jani Nikula023542f2020-12-15 20:45:57 -0800481 const struct rchan_callbacks *cb,
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800482 void *private_data)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100483{
484 unsigned int i;
485 struct rchan *chan;
Akash Goel017c59c2016-09-02 21:47:38 +0200486 struct rchan_buf *buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100487
488 if (!(subbuf_size && n_subbufs))
489 return NULL;
Dan Carpenterf6302f12012-02-10 09:03:58 +0100490 if (subbuf_size > UINT_MAX / n_subbufs)
491 return NULL;
Jani Nikula371e0382020-12-15 20:45:53 -0800492 if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
Jani Nikula6f8f2542020-12-15 20:45:50 -0800493 return NULL;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100494
Robert P. J. Daycd861282006-12-13 00:34:52 -0800495 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100496 if (!chan)
497 return NULL;
498
Akash Goel017c59c2016-09-02 21:47:38 +0200499 chan->buf = alloc_percpu(struct rchan_buf *);
Daniel Axtens54e200a2020-06-04 16:51:27 -0700500 if (!chan->buf) {
501 kfree(chan);
502 return NULL;
503 }
504
Jens Axboeb86ff9812006-03-23 19:56:55 +0100505 chan->version = RELAYFS_CHANNEL_VERSION;
506 chan->n_subbufs = n_subbufs;
507 chan->subbuf_size = subbuf_size;
zhangwei(Jovi)a05342cb2013-04-30 15:28:41 -0700508 chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800509 chan->parent = parent;
510 chan->private_data = private_data;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700511 if (base_filename) {
512 chan->has_base_filename = 1;
513 strlcpy(chan->base_filename, base_filename, NAME_MAX);
514 }
Jani Nikula023542f2020-12-15 20:45:57 -0800515 chan->cb = cb;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100516 kref_init(&chan->kref);
517
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800518 mutex_lock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100519 for_each_online_cpu(i) {
Akash Goel017c59c2016-09-02 21:47:38 +0200520 buf = relay_open_buf(chan, i);
521 if (!buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100522 goto free_bufs;
Akash Goel017c59c2016-09-02 21:47:38 +0200523 *per_cpu_ptr(chan->buf, i) = buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100524 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800525 list_add(&chan->list, &relay_channels);
526 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100527
Jens Axboeb86ff9812006-03-23 19:56:55 +0100528 return chan;
529
530free_bufs:
Lai Jiangshan98ba4032008-11-14 10:44:59 +0100531 for_each_possible_cpu(i) {
Akash Goel017c59c2016-09-02 21:47:38 +0200532 if ((buf = *per_cpu_ptr(chan->buf, i)))
533 relay_close_buf(buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100534 }
Jens Axboeb86ff9812006-03-23 19:56:55 +0100535
Jens Axboeb86ff9812006-03-23 19:56:55 +0100536 kref_put(&chan->kref, relay_destroy_channel);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800537 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100538 return NULL;
539}
540EXPORT_SYMBOL_GPL(relay_open);
541
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700542struct rchan_percpu_buf_dispatcher {
543 struct rchan_buf *buf;
544 struct dentry *dentry;
545};
546
547/* Called in atomic context. */
548static void __relay_set_buf_dentry(void *info)
549{
550 struct rchan_percpu_buf_dispatcher *p = info;
551
552 relay_set_buf_dentry(p->buf, p->dentry);
553}
554
555/**
556 * relay_late_setup_files - triggers file creation
557 * @chan: channel to operate on
558 * @base_filename: base name of files to create
559 * @parent: dentry of parent directory, %NULL for root directory
560 *
561 * Returns 0 if successful, non-zero otherwise.
562 *
Akash Goel59dbb2a2016-08-02 14:07:18 -0700563 * Use to setup files for a previously buffer-only channel created
564 * by relay_open() with a NULL parent dentry.
565 *
566 * For example, this is useful for perfomring early tracing in kernel,
567 * before VFS is up and then exposing the early results once the dentry
568 * is available.
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700569 */
570int relay_late_setup_files(struct rchan *chan,
571 const char *base_filename,
572 struct dentry *parent)
573{
574 int err = 0;
575 unsigned int i, curr_cpu;
576 unsigned long flags;
577 struct dentry *dentry;
Akash Goel017c59c2016-09-02 21:47:38 +0200578 struct rchan_buf *buf;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700579 struct rchan_percpu_buf_dispatcher disp;
580
581 if (!chan || !base_filename)
582 return -EINVAL;
583
584 strlcpy(chan->base_filename, base_filename, NAME_MAX);
585
586 mutex_lock(&relay_channels_mutex);
587 /* Is chan already set up? */
Jiri Slabyb786c6a2009-01-17 12:04:36 +0100588 if (unlikely(chan->has_base_filename)) {
589 mutex_unlock(&relay_channels_mutex);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700590 return -EEXIST;
Jiri Slabyb786c6a2009-01-17 12:04:36 +0100591 }
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700592 chan->has_base_filename = 1;
593 chan->parent = parent;
Akash Goel59dbb2a2016-08-02 14:07:18 -0700594
595 if (chan->is_global) {
596 err = -EINVAL;
Akash Goel017c59c2016-09-02 21:47:38 +0200597 buf = *per_cpu_ptr(chan->buf, 0);
598 if (!WARN_ON_ONCE(!buf)) {
599 dentry = relay_create_buf_file(chan, buf, 0);
Akash Goel59dbb2a2016-08-02 14:07:18 -0700600 if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
Akash Goel017c59c2016-09-02 21:47:38 +0200601 relay_set_buf_dentry(buf, dentry);
Akash Goel59dbb2a2016-08-02 14:07:18 -0700602 err = 0;
603 }
604 }
605 mutex_unlock(&relay_channels_mutex);
606 return err;
607 }
608
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700609 curr_cpu = get_cpu();
610 /*
611 * The CPU hotplug notifier ran before us and created buffers with
612 * no files associated. So it's safe to call relay_setup_buf_file()
613 * on all currently online CPUs.
614 */
615 for_each_online_cpu(i) {
Akash Goel017c59c2016-09-02 21:47:38 +0200616 buf = *per_cpu_ptr(chan->buf, i);
617 if (unlikely(!buf)) {
Ingo Molnar7a51cff2008-12-29 16:03:40 +0100618 WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700619 err = -EINVAL;
620 break;
621 }
622
Akash Goel017c59c2016-09-02 21:47:38 +0200623 dentry = relay_create_buf_file(chan, buf, i);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700624 if (unlikely(!dentry)) {
625 err = -EINVAL;
626 break;
627 }
628
629 if (curr_cpu == i) {
630 local_irq_save(flags);
Akash Goel017c59c2016-09-02 21:47:38 +0200631 relay_set_buf_dentry(buf, dentry);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700632 local_irq_restore(flags);
633 } else {
Akash Goel017c59c2016-09-02 21:47:38 +0200634 disp.buf = buf;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700635 disp.dentry = dentry;
636 smp_mb();
637 /* relay_channels_mutex must be held, so wait. */
638 err = smp_call_function_single(i,
639 __relay_set_buf_dentry,
640 &disp, 1);
641 }
642 if (unlikely(err))
643 break;
644 }
645 put_cpu();
646 mutex_unlock(&relay_channels_mutex);
647
648 return err;
649}
Akash Goel59dbb2a2016-08-02 14:07:18 -0700650EXPORT_SYMBOL_GPL(relay_late_setup_files);
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700651
Jens Axboeb86ff9812006-03-23 19:56:55 +0100652/**
653 * relay_switch_subbuf - switch to a new sub-buffer
654 * @buf: channel buffer
655 * @length: size of current event
656 *
657 * Returns either the length passed in or 0 if full.
658 *
659 * Performs sub-buffer-switch tasks such as invoking callbacks,
660 * updating padding counts, waking up readers, etc.
661 */
662size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
663{
664 void *old, *new;
665 size_t old_subbuf, new_subbuf;
666
667 if (unlikely(length > buf->chan->subbuf_size))
668 goto toobig;
669
670 if (buf->offset != buf->chan->subbuf_size + 1) {
671 buf->prev_padding = buf->chan->subbuf_size - buf->offset;
672 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
673 buf->padding[old_subbuf] = buf->prev_padding;
674 buf->subbufs_produced++;
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700675 if (buf->dentry)
David Howells7682c912015-03-17 22:26:16 +0000676 d_inode(buf->dentry)->i_size +=
Eduard - Gabriel Munteanu20d8b672008-07-25 19:45:12 -0700677 buf->chan->subbuf_size -
678 buf->padding[old_subbuf];
679 else
680 buf->early_bytes += buf->chan->subbuf_size -
681 buf->padding[old_subbuf];
Jens Axboe221415d2006-03-23 19:57:55 +0100682 smp_mb();
Peter Zijlstra26b56792016-10-11 13:54:33 -0700683 if (waitqueue_active(&buf->read_wait)) {
Tom Zanussi7c9cb382007-05-09 02:34:01 -0700684 /*
685 * Calling wake_up_interruptible() from here
686 * will deadlock if we happen to be logging
687 * from the scheduler (trying to re-grab
688 * rq->lock), so defer it.
689 */
Peter Zijlstra26b56792016-10-11 13:54:33 -0700690 irq_work_queue(&buf->wakeup_work);
691 }
Jens Axboeb86ff9812006-03-23 19:56:55 +0100692 }
693
694 old = buf->data;
695 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
696 new = buf->start + new_subbuf * buf->chan->subbuf_size;
697 buf->offset = 0;
Jani Nikula023542f2020-12-15 20:45:57 -0800698 if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
Jens Axboeb86ff9812006-03-23 19:56:55 +0100699 buf->offset = buf->chan->subbuf_size + 1;
700 return 0;
701 }
702 buf->data = new;
703 buf->padding[new_subbuf] = 0;
704
705 if (unlikely(length + buf->offset > buf->chan->subbuf_size))
706 goto toobig;
707
708 return length;
709
710toobig:
711 buf->chan->last_toobig = length;
712 return 0;
713}
714EXPORT_SYMBOL_GPL(relay_switch_subbuf);
715
716/**
717 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
718 * @chan: the channel
719 * @cpu: the cpu associated with the channel buffer to update
720 * @subbufs_consumed: number of sub-buffers to add to current buf's count
721 *
722 * Adds to the channel buffer's consumed sub-buffer count.
723 * subbufs_consumed should be the number of sub-buffers newly consumed,
724 * not the total consumed.
725 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800726 * NOTE. Kernel clients don't need to call this function if the channel
Jens Axboeb86ff9812006-03-23 19:56:55 +0100727 * mode is 'overwrite'.
728 */
729void relay_subbufs_consumed(struct rchan *chan,
730 unsigned int cpu,
731 size_t subbufs_consumed)
732{
733 struct rchan_buf *buf;
734
Dan Carpenter9a29d0f2016-12-14 15:05:38 -0800735 if (!chan || cpu >= NR_CPUS)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100736 return;
737
Akash Goel017c59c2016-09-02 21:47:38 +0200738 buf = *per_cpu_ptr(chan->buf, cpu);
Dan Carpenter9a29d0f2016-12-14 15:05:38 -0800739 if (!buf || subbufs_consumed > chan->n_subbufs)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100740 return;
741
Aravind Srinivasan2c53d912009-04-02 16:58:59 -0700742 if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100743 buf->subbufs_consumed = buf->subbufs_produced;
Aravind Srinivasan2c53d912009-04-02 16:58:59 -0700744 else
745 buf->subbufs_consumed += subbufs_consumed;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100746}
747EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
748
749/**
750 * relay_close - close the channel
751 * @chan: the channel
752 *
753 * Closes all channel buffers and frees the channel.
754 */
755void relay_close(struct rchan *chan)
756{
Akash Goel017c59c2016-09-02 21:47:38 +0200757 struct rchan_buf *buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100758 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100759
760 if (!chan)
761 return;
762
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800763 mutex_lock(&relay_channels_mutex);
Akash Goel017c59c2016-09-02 21:47:38 +0200764 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
765 relay_close_buf(buf);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800766 else
767 for_each_possible_cpu(i)
Akash Goel017c59c2016-09-02 21:47:38 +0200768 if ((buf = *per_cpu_ptr(chan->buf, i)))
769 relay_close_buf(buf);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100770
771 if (chan->last_toobig)
772 printk(KERN_WARNING "relay: one or more items not logged "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800773 "[item size (%zd) > sub-buffer size (%zd)]\n",
Jens Axboeb86ff9812006-03-23 19:56:55 +0100774 chan->last_toobig, chan->subbuf_size);
775
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800776 list_del(&chan->list);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100777 kref_put(&chan->kref, relay_destroy_channel);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800778 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100779}
780EXPORT_SYMBOL_GPL(relay_close);
781
782/**
783 * relay_flush - close the channel
784 * @chan: the channel
785 *
Randy Dunlap4c78a662006-09-29 01:59:10 -0700786 * Flushes all channel buffers, i.e. forces buffer switch.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100787 */
788void relay_flush(struct rchan *chan)
789{
Akash Goel017c59c2016-09-02 21:47:38 +0200790 struct rchan_buf *buf;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100791 unsigned int i;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100792
793 if (!chan)
794 return;
795
Akash Goel017c59c2016-09-02 21:47:38 +0200796 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
797 relay_switch_subbuf(buf, 0);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800798 return;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100799 }
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800800
801 mutex_lock(&relay_channels_mutex);
802 for_each_possible_cpu(i)
Akash Goel017c59c2016-09-02 21:47:38 +0200803 if ((buf = *per_cpu_ptr(chan->buf, i)))
804 relay_switch_subbuf(buf, 0);
Mathieu Desnoyers23c88752007-02-10 01:45:05 -0800805 mutex_unlock(&relay_channels_mutex);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100806}
807EXPORT_SYMBOL_GPL(relay_flush);
808
809/**
810 * relay_file_open - open file op for relay files
811 * @inode: the inode
812 * @filp: the file
813 *
814 * Increments the channel buffer refcount.
815 */
816static int relay_file_open(struct inode *inode, struct file *filp)
817{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700818 struct rchan_buf *buf = inode->i_private;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100819 kref_get(&buf->kref);
820 filp->private_data = buf;
821
Lai Jiangshan37529fe2008-03-26 12:01:28 +0100822 return nonseekable_open(inode, filp);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100823}
824
825/**
826 * relay_file_mmap - mmap file op for relay files
827 * @filp: the file
828 * @vma: the vma describing what to map
829 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800830 * Calls upon relay_mmap_buf() to map the file into user space.
Jens Axboeb86ff9812006-03-23 19:56:55 +0100831 */
832static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
833{
834 struct rchan_buf *buf = filp->private_data;
835 return relay_mmap_buf(buf, vma);
836}
837
838/**
839 * relay_file_poll - poll file op for relay files
840 * @filp: the file
841 * @wait: poll table
842 *
843 * Poll implemention.
844 */
Al Viro9dd95742017-07-03 00:42:43 -0400845static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100846{
Al Viro9dd95742017-07-03 00:42:43 -0400847 __poll_t mask = 0;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100848 struct rchan_buf *buf = filp->private_data;
849
850 if (buf->finalized)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800851 return EPOLLERR;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100852
853 if (filp->f_mode & FMODE_READ) {
854 poll_wait(filp, &buf->read_wait, wait);
855 if (!relay_buf_empty(buf))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800856 mask |= EPOLLIN | EPOLLRDNORM;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100857 }
858
859 return mask;
860}
861
862/**
863 * relay_file_release - release file op for relay files
864 * @inode: the inode
865 * @filp: the file
866 *
867 * Decrements the channel refcount, as the filesystem is
868 * no longer using it.
869 */
870static int relay_file_release(struct inode *inode, struct file *filp)
871{
872 struct rchan_buf *buf = filp->private_data;
873 kref_put(&buf->kref, relay_remove_buf);
874
875 return 0;
876}
877
Randy Dunlap4c78a662006-09-29 01:59:10 -0700878/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100879 * relay_file_read_consume - update the consumed count for the buffer
880 */
881static void relay_file_read_consume(struct rchan_buf *buf,
882 size_t read_pos,
883 size_t bytes_consumed)
884{
885 size_t subbuf_size = buf->chan->subbuf_size;
886 size_t n_subbufs = buf->chan->n_subbufs;
887 size_t read_subbuf;
888
Tom Zanussi32194452008-08-05 13:01:10 -0700889 if (buf->subbufs_produced == buf->subbufs_consumed &&
890 buf->offset == buf->bytes_consumed)
891 return;
892
Jens Axboeb86ff9812006-03-23 19:56:55 +0100893 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
894 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
895 buf->bytes_consumed = 0;
896 }
897
898 buf->bytes_consumed += bytes_consumed;
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700899 if (!read_pos)
900 read_subbuf = buf->subbufs_consumed % n_subbufs;
901 else
902 read_subbuf = read_pos / buf->chan->subbuf_size;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100903 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
904 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
905 (buf->offset == subbuf_size))
906 return;
907 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
908 buf->bytes_consumed = 0;
909 }
910}
911
Randy Dunlap4c78a662006-09-29 01:59:10 -0700912/*
Jens Axboeb86ff9812006-03-23 19:56:55 +0100913 * relay_file_read_avail - boolean, are there unconsumed bytes available?
914 */
Pengcheng Yang341a7212020-06-04 16:51:30 -0700915static int relay_file_read_avail(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100916{
Jens Axboeb86ff9812006-03-23 19:56:55 +0100917 size_t subbuf_size = buf->chan->subbuf_size;
918 size_t n_subbufs = buf->chan->n_subbufs;
Jens Axboe221415d2006-03-23 19:57:55 +0100919 size_t produced = buf->subbufs_produced;
Sudip Mukherjeeac05b7a2020-10-15 20:13:25 -0700920 size_t consumed;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100921
Pengcheng Yang341a7212020-06-04 16:51:30 -0700922 relay_file_read_consume(buf, 0, 0);
Jens Axboeb86ff9812006-03-23 19:56:55 +0100923
Tom Zanussi32194452008-08-05 13:01:10 -0700924 consumed = buf->subbufs_consumed;
925
Jens Axboe221415d2006-03-23 19:57:55 +0100926 if (unlikely(buf->offset > subbuf_size)) {
927 if (produced == consumed)
928 return 0;
929 return 1;
930 }
931
932 if (unlikely(produced - consumed >= n_subbufs)) {
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700933 consumed = produced - n_subbufs + 1;
Jens Axboe221415d2006-03-23 19:57:55 +0100934 buf->subbufs_consumed = consumed;
Masami Hiramatsua66e3562007-06-27 14:10:04 -0700935 buf->bytes_consumed = 0;
Jens Axboe221415d2006-03-23 19:57:55 +0100936 }
Daniel Walker1bfbc602007-10-18 03:06:05 -0700937
Jens Axboe221415d2006-03-23 19:57:55 +0100938 produced = (produced % n_subbufs) * subbuf_size + buf->offset;
939 consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
940
941 if (consumed > produced)
942 produced += n_subbufs * subbuf_size;
Daniel Walker1bfbc602007-10-18 03:06:05 -0700943
Tom Zanussi32194452008-08-05 13:01:10 -0700944 if (consumed == produced) {
945 if (buf->offset == subbuf_size &&
946 buf->subbufs_produced > buf->subbufs_consumed)
947 return 1;
Jens Axboe221415d2006-03-23 19:57:55 +0100948 return 0;
Tom Zanussi32194452008-08-05 13:01:10 -0700949 }
Jens Axboe221415d2006-03-23 19:57:55 +0100950
Jens Axboeb86ff9812006-03-23 19:56:55 +0100951 return 1;
952}
953
954/**
955 * relay_file_read_subbuf_avail - return bytes available in sub-buffer
Randy Dunlap4c78a662006-09-29 01:59:10 -0700956 * @read_pos: file read position
957 * @buf: relay channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100958 */
959static size_t relay_file_read_subbuf_avail(size_t read_pos,
960 struct rchan_buf *buf)
961{
962 size_t padding, avail = 0;
963 size_t read_subbuf, read_offset, write_subbuf, write_offset;
964 size_t subbuf_size = buf->chan->subbuf_size;
965
966 write_subbuf = (buf->data - buf->start) / subbuf_size;
967 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
968 read_subbuf = read_pos / subbuf_size;
969 read_offset = read_pos % subbuf_size;
970 padding = buf->padding[read_subbuf];
971
972 if (read_subbuf == write_subbuf) {
973 if (read_offset + padding < write_offset)
974 avail = write_offset - (read_offset + padding);
975 } else
976 avail = (subbuf_size - padding) - read_offset;
977
978 return avail;
979}
980
981/**
982 * relay_file_read_start_pos - find the first available byte to read
Randy Dunlap4c78a662006-09-29 01:59:10 -0700983 * @buf: relay channel buffer
Jens Axboeb86ff9812006-03-23 19:56:55 +0100984 *
Pengcheng Yang341a7212020-06-04 16:51:30 -0700985 * If the read_pos is in the middle of padding, return the
Jens Axboeb86ff9812006-03-23 19:56:55 +0100986 * position of the first actually available byte, otherwise
987 * return the original value.
988 */
Pengcheng Yang341a7212020-06-04 16:51:30 -0700989static size_t relay_file_read_start_pos(struct rchan_buf *buf)
Jens Axboeb86ff9812006-03-23 19:56:55 +0100990{
991 size_t read_subbuf, padding, padding_start, padding_end;
992 size_t subbuf_size = buf->chan->subbuf_size;
993 size_t n_subbufs = buf->chan->n_subbufs;
David Wilder8d62fde2007-06-27 14:10:03 -0700994 size_t consumed = buf->subbufs_consumed % n_subbufs;
Pengcheng Yang341a7212020-06-04 16:51:30 -0700995 size_t read_pos = consumed * subbuf_size + buf->bytes_consumed;
Jens Axboeb86ff9812006-03-23 19:56:55 +0100996
997 read_subbuf = read_pos / subbuf_size;
998 padding = buf->padding[read_subbuf];
999 padding_start = (read_subbuf + 1) * subbuf_size - padding;
1000 padding_end = (read_subbuf + 1) * subbuf_size;
1001 if (read_pos >= padding_start && read_pos < padding_end) {
1002 read_subbuf = (read_subbuf + 1) % n_subbufs;
1003 read_pos = read_subbuf * subbuf_size;
1004 }
1005
1006 return read_pos;
1007}
1008
1009/**
1010 * relay_file_read_end_pos - return the new read position
Randy Dunlap4c78a662006-09-29 01:59:10 -07001011 * @read_pos: file read position
1012 * @buf: relay channel buffer
1013 * @count: number of bytes to be read
Jens Axboeb86ff9812006-03-23 19:56:55 +01001014 */
1015static size_t relay_file_read_end_pos(struct rchan_buf *buf,
1016 size_t read_pos,
1017 size_t count)
1018{
1019 size_t read_subbuf, padding, end_pos;
1020 size_t subbuf_size = buf->chan->subbuf_size;
1021 size_t n_subbufs = buf->chan->n_subbufs;
1022
1023 read_subbuf = read_pos / subbuf_size;
1024 padding = buf->padding[read_subbuf];
1025 if (read_pos % subbuf_size + count + padding == subbuf_size)
1026 end_pos = (read_subbuf + 1) * subbuf_size;
1027 else
1028 end_pos = read_pos + count;
1029 if (end_pos >= subbuf_size * n_subbufs)
1030 end_pos = 0;
1031
1032 return end_pos;
1033}
1034
Al Viroa7c22422016-09-25 22:52:02 -04001035static ssize_t relay_file_read(struct file *filp,
1036 char __user *buffer,
1037 size_t count,
1038 loff_t *ppos)
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001039{
1040 struct rchan_buf *buf = filp->private_data;
1041 size_t read_start, avail;
Al Viroa7c22422016-09-25 22:52:02 -04001042 size_t written = 0;
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001043 int ret;
1044
Al Viroa7c22422016-09-25 22:52:02 -04001045 if (!count)
Jens Axboe221415d2006-03-23 19:57:55 +01001046 return 0;
1047
Al Viro59551022016-01-22 15:40:57 -05001048 inode_lock(file_inode(filp));
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001049 do {
Al Viroa7c22422016-09-25 22:52:02 -04001050 void *from;
1051
Pengcheng Yang341a7212020-06-04 16:51:30 -07001052 if (!relay_file_read_avail(buf))
Jens Axboe221415d2006-03-23 19:57:55 +01001053 break;
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001054
Pengcheng Yang341a7212020-06-04 16:51:30 -07001055 read_start = relay_file_read_start_pos(buf);
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001056 avail = relay_file_read_subbuf_avail(read_start, buf);
1057 if (!avail)
1058 break;
1059
Al Viroa7c22422016-09-25 22:52:02 -04001060 avail = min(count, avail);
1061 from = buf->start + read_start;
1062 ret = avail;
1063 if (copy_to_user(buffer, from, avail))
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001064 break;
1065
Al Viroa7c22422016-09-25 22:52:02 -04001066 buffer += ret;
1067 written += ret;
1068 count -= ret;
1069
1070 relay_file_read_consume(buf, read_start, ret);
1071 *ppos = relay_file_read_end_pos(buf, read_start, ret);
1072 } while (count);
Al Viro59551022016-01-22 15:40:57 -05001073 inode_unlock(file_inode(filp));
Jens Axboe221415d2006-03-23 19:57:55 +01001074
Al Viroa7c22422016-09-25 22:52:02 -04001075 return written;
Tom Zanussi6dac40a2006-03-23 19:58:45 +01001076}
1077
Jens Axboe1db60cf2007-06-12 08:43:46 +02001078static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
1079{
1080 rbuf->bytes_consumed += bytes_consumed;
1081
1082 if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
1083 relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
1084 rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
1085 }
1086}
1087
Tom Zanussiebf99092007-06-04 09:12:05 +02001088static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
1089 struct pipe_buffer *buf)
Jens Axboe221415d2006-03-23 19:57:55 +01001090{
Tom Zanussiebf99092007-06-04 09:12:05 +02001091 struct rchan_buf *rbuf;
1092
1093 rbuf = (struct rchan_buf *)page_private(buf->page);
Jens Axboe1db60cf2007-06-12 08:43:46 +02001094 relay_consume_bytes(rbuf, buf->private);
Tom Zanussiebf99092007-06-04 09:12:05 +02001095}
1096
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08001097static const struct pipe_buf_operations relay_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +02001098 .release = relay_pipe_buf_release,
1099 .try_steal = generic_pipe_buf_try_steal,
1100 .get = generic_pipe_buf_get,
Tom Zanussiebf99092007-06-04 09:12:05 +02001101};
1102
Jens Axboe5eb7f9f2008-03-26 12:04:09 +01001103static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1104{
1105}
1106
Tom Zanussid3f35d982007-07-12 08:12:05 +02001107/*
Tom Zanussiebf99092007-06-04 09:12:05 +02001108 * subbuf_splice_actor - splice up to one subbuf's worth of data
1109 */
Dan Carpenter5f1664f2010-03-05 13:42:51 -08001110static ssize_t subbuf_splice_actor(struct file *in,
Tom Zanussiebf99092007-06-04 09:12:05 +02001111 loff_t *ppos,
1112 struct pipe_inode_info *pipe,
1113 size_t len,
1114 unsigned int flags,
1115 int *nonpad_ret)
1116{
Dan Carpenter5f1664f2010-03-05 13:42:51 -08001117 unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
Tom Zanussiebf99092007-06-04 09:12:05 +02001118 struct rchan_buf *rbuf = in->private_data;
1119 unsigned int subbuf_size = rbuf->chan->subbuf_size;
Tom Zanussi24da24d2007-07-12 08:12:04 +02001120 uint64_t pos = (uint64_t) *ppos;
1121 uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
1122 size_t read_start = (size_t) do_div(pos, alloc_size);
Tom Zanussiebf99092007-06-04 09:12:05 +02001123 size_t read_subbuf = read_start / subbuf_size;
1124 size_t padding = rbuf->padding[read_subbuf];
1125 size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
Jens Axboe35f3d142010-05-20 10:43:18 +02001126 struct page *pages[PIPE_DEF_BUFFERS];
1127 struct partial_page partial[PIPE_DEF_BUFFERS];
Jens Axboe1db60cf2007-06-12 08:43:46 +02001128 struct splice_pipe_desc spd = {
1129 .pages = pages,
1130 .nr_pages = 0,
Eric Dumazet047fe362012-06-12 15:24:40 +02001131 .nr_pages_max = PIPE_DEF_BUFFERS,
Jens Axboe1db60cf2007-06-12 08:43:46 +02001132 .partial = partial,
Jens Axboe1db60cf2007-06-12 08:43:46 +02001133 .ops = &relay_pipe_buf_ops,
Jens Axboe5eb7f9f2008-03-26 12:04:09 +01001134 .spd_release = relay_page_release,
Jens Axboe1db60cf2007-06-12 08:43:46 +02001135 };
Dan Carpenter5f1664f2010-03-05 13:42:51 -08001136 ssize_t ret;
Tom Zanussiebf99092007-06-04 09:12:05 +02001137
1138 if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
1139 return 0;
Jens Axboe35f3d142010-05-20 10:43:18 +02001140 if (splice_grow_spd(pipe, &spd))
1141 return -ENOMEM;
Tom Zanussiebf99092007-06-04 09:12:05 +02001142
Jens Axboe1db60cf2007-06-12 08:43:46 +02001143 /*
1144 * Adjust read len, if longer than what is available
1145 */
1146 if (len > (subbuf_size - read_start % subbuf_size))
1147 len = subbuf_size - read_start % subbuf_size;
Tom Zanussiebf99092007-06-04 09:12:05 +02001148
1149 subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
1150 pidx = (read_start / PAGE_SIZE) % subbuf_pages;
1151 poff = read_start & ~PAGE_MASK;
Al Viroa786c062014-04-11 12:01:03 -04001152 nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max);
Tom Zanussiebf99092007-06-04 09:12:05 +02001153
Jens Axboe16d546692008-03-17 09:04:59 +01001154 for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
Jens Axboe1db60cf2007-06-12 08:43:46 +02001155 unsigned int this_len, this_end, private;
1156 unsigned int cur_pos = read_start + total_len;
Tom Zanussiebf99092007-06-04 09:12:05 +02001157
Jens Axboe1db60cf2007-06-12 08:43:46 +02001158 if (!len)
1159 break;
1160
1161 this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
1162 private = this_len;
1163
1164 spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
1165 spd.partial[spd.nr_pages].offset = poff;
1166
1167 this_end = cur_pos + this_len;
1168 if (this_end >= nonpad_end) {
1169 this_len = nonpad_end - cur_pos;
1170 private = this_len + padding;
1171 }
1172 spd.partial[spd.nr_pages].len = this_len;
1173 spd.partial[spd.nr_pages].private = private;
1174
1175 len -= this_len;
1176 total_len += this_len;
1177 poff = 0;
1178 pidx = (pidx + 1) % subbuf_pages;
1179
1180 if (this_end >= nonpad_end) {
1181 spd.nr_pages++;
Tom Zanussiebf99092007-06-04 09:12:05 +02001182 break;
1183 }
Tom Zanussiebf99092007-06-04 09:12:05 +02001184 }
1185
Jens Axboe35f3d142010-05-20 10:43:18 +02001186 ret = 0;
Jens Axboe1db60cf2007-06-12 08:43:46 +02001187 if (!spd.nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001188 goto out;
Tom Zanussiebf99092007-06-04 09:12:05 +02001189
Jens Axboe1db60cf2007-06-12 08:43:46 +02001190 ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
1191 if (ret < 0 || ret < total_len)
Jens Axboe35f3d142010-05-20 10:43:18 +02001192 goto out;
Tom Zanussiebf99092007-06-04 09:12:05 +02001193
Jens Axboe1db60cf2007-06-12 08:43:46 +02001194 if (read_start + ret == nonpad_end)
1195 ret += padding;
1196
Jens Axboe35f3d142010-05-20 10:43:18 +02001197out:
Eric Dumazet047fe362012-06-12 15:24:40 +02001198 splice_shrink_spd(&spd);
1199 return ret;
Tom Zanussiebf99092007-06-04 09:12:05 +02001200}
1201
1202static ssize_t relay_file_splice_read(struct file *in,
1203 loff_t *ppos,
1204 struct pipe_inode_info *pipe,
1205 size_t len,
1206 unsigned int flags)
1207{
1208 ssize_t spliced;
1209 int ret;
1210 int nonpad_ret = 0;
1211
1212 ret = 0;
1213 spliced = 0;
1214
Tom Zanussia82c53a2008-05-09 13:28:36 +02001215 while (len && !spliced) {
Tom Zanussiebf99092007-06-04 09:12:05 +02001216 ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
1217 if (ret < 0)
1218 break;
1219 else if (!ret) {
Tom Zanussifbb5b7a2008-12-09 13:14:10 -08001220 if (flags & SPLICE_F_NONBLOCK)
Tom Zanussiebf99092007-06-04 09:12:05 +02001221 ret = -EAGAIN;
Tom Zanussifbb5b7a2008-12-09 13:14:10 -08001222 break;
Tom Zanussiebf99092007-06-04 09:12:05 +02001223 }
1224
1225 *ppos += ret;
1226 if (ret > len)
1227 len = 0;
1228 else
1229 len -= ret;
1230 spliced += nonpad_ret;
1231 nonpad_ret = 0;
1232 }
1233
1234 if (spliced)
1235 return spliced;
1236
1237 return ret;
Jens Axboe221415d2006-03-23 19:57:55 +01001238}
1239
Helge Deller15ad7cd2006-12-06 20:40:36 -08001240const struct file_operations relay_file_operations = {
Jens Axboeb86ff9812006-03-23 19:56:55 +01001241 .open = relay_file_open,
1242 .poll = relay_file_poll,
1243 .mmap = relay_file_mmap,
1244 .read = relay_file_read,
1245 .llseek = no_llseek,
1246 .release = relay_file_release,
Tom Zanussiebf99092007-06-04 09:12:05 +02001247 .splice_read = relay_file_splice_read,
Jens Axboeb86ff9812006-03-23 19:56:55 +01001248};
1249EXPORT_SYMBOL_GPL(relay_file_operations);