1b86ff981SJens Axboe /* 2b86ff981SJens Axboe * Public API and common code for kernel->userspace relay file support. 3b86ff981SJens Axboe * 40c1bc6b8SMauro Carvalho Chehab * See Documentation/filesystems/relay.rst for an overview. 5b86ff981SJens Axboe * 6b86ff981SJens Axboe * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp 7b86ff981SJens Axboe * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com) 8b86ff981SJens Axboe * 9b86ff981SJens Axboe * Moved to kernel/relay.c by Paul Mundt, 2006. 1023c88752SMathieu Desnoyers * November 2006 - CPU hotplug support by Mathieu Desnoyers 1123c88752SMathieu Desnoyers * (mathieu.desnoyers@polymtl.ca) 12b86ff981SJens Axboe * 13b86ff981SJens Axboe * This file is released under the GPL. 14b86ff981SJens Axboe */ 15b86ff981SJens Axboe #include <linux/errno.h> 16b86ff981SJens Axboe #include <linux/stddef.h> 17b86ff981SJens Axboe #include <linux/slab.h> 189984de1aSPaul Gortmaker #include <linux/export.h> 19b86ff981SJens Axboe #include <linux/string.h> 20b86ff981SJens Axboe #include <linux/relay.h> 21b86ff981SJens Axboe #include <linux/vmalloc.h> 22b86ff981SJens Axboe #include <linux/mm.h> 2323c88752SMathieu Desnoyers #include <linux/cpu.h> 24d6b29d7cSJens Axboe #include <linux/splice.h> 2523c88752SMathieu Desnoyers 2623c88752SMathieu Desnoyers /* list of open channels, for cpu hotplug */ 2723c88752SMathieu Desnoyers static DEFINE_MUTEX(relay_channels_mutex); 2823c88752SMathieu Desnoyers static LIST_HEAD(relay_channels); 29b86ff981SJens Axboe 30b86ff981SJens Axboe /* 31a1e09612SNick Piggin * fault() vm_op implementation for relay file mapping. 32b86ff981SJens Axboe */ 333fb3894bSSouptick Joarder static vm_fault_t relay_buf_fault(struct vm_fault *vmf) 34b86ff981SJens Axboe { 35b86ff981SJens Axboe struct page *page; 3611bac800SDave Jiang struct rchan_buf *buf = vmf->vma->vm_private_data; 37a1e09612SNick Piggin pgoff_t pgoff = vmf->pgoff; 38b86ff981SJens Axboe 39b86ff981SJens Axboe if (!buf) 40a1e09612SNick Piggin return VM_FAULT_OOM; 41b86ff981SJens Axboe 42a1e09612SNick Piggin page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); 43b86ff981SJens Axboe if (!page) 44a1e09612SNick Piggin return VM_FAULT_SIGBUS; 45b86ff981SJens Axboe get_page(page); 46a1e09612SNick Piggin vmf->page = page; 47b86ff981SJens Axboe 48a1e09612SNick Piggin return 0; 49b86ff981SJens Axboe } 50b86ff981SJens Axboe 51b86ff981SJens Axboe /* 52b86ff981SJens Axboe * vm_ops for relay file mappings. 53b86ff981SJens Axboe */ 54f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct relay_file_mmap_ops = { 55a1e09612SNick Piggin .fault = relay_buf_fault, 56b86ff981SJens Axboe }; 57b86ff981SJens Axboe 5868ab3d88SMasami Hiramatsu /* 5968ab3d88SMasami Hiramatsu * allocate an array of pointers of struct page 6068ab3d88SMasami Hiramatsu */ 6168ab3d88SMasami Hiramatsu static struct page **relay_alloc_page_array(unsigned int n_pages) 6268ab3d88SMasami Hiramatsu { 63408af87aSJesper Juhl const size_t pa_size = n_pages * sizeof(struct page *); 64408af87aSJesper Juhl if (pa_size > PAGE_SIZE) 65408af87aSJesper Juhl return vzalloc(pa_size); 66408af87aSJesper Juhl return kzalloc(pa_size, GFP_KERNEL); 6768ab3d88SMasami Hiramatsu } 6868ab3d88SMasami Hiramatsu 6968ab3d88SMasami Hiramatsu /* 7068ab3d88SMasami Hiramatsu * free an array of pointers of struct page 7168ab3d88SMasami Hiramatsu */ 7268ab3d88SMasami Hiramatsu static void relay_free_page_array(struct page **array) 7368ab3d88SMasami Hiramatsu { 74200f1ce3SPekka Enberg kvfree(array); 7568ab3d88SMasami Hiramatsu } 7668ab3d88SMasami Hiramatsu 77b86ff981SJens Axboe /** 78b86ff981SJens Axboe * relay_mmap_buf: - mmap channel buffer to process address space 79b86ff981SJens Axboe * @buf: relay channel buffer 80b86ff981SJens Axboe * @vma: vm_area_struct describing memory to be mapped 81b86ff981SJens Axboe * 82b86ff981SJens Axboe * Returns 0 if ok, negative on error 83b86ff981SJens Axboe * 84c1e8d7c6SMichel Lespinasse * Caller should already have grabbed mmap_lock. 85b86ff981SJens Axboe */ 8601c55ed3SAdrian Bunk static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) 87b86ff981SJens Axboe { 88b86ff981SJens Axboe unsigned long length = vma->vm_end - vma->vm_start; 89b86ff981SJens Axboe 90b86ff981SJens Axboe if (!buf) 91b86ff981SJens Axboe return -EBADF; 92b86ff981SJens Axboe 93b86ff981SJens Axboe if (length != (unsigned long)buf->chan->alloc_size) 94b86ff981SJens Axboe return -EINVAL; 95b86ff981SJens Axboe 96b86ff981SJens Axboe vma->vm_ops = &relay_file_mmap_ops; 972f98735cSNick Piggin vma->vm_flags |= VM_DONTEXPAND; 98b86ff981SJens Axboe vma->vm_private_data = buf; 99b86ff981SJens Axboe 100b86ff981SJens Axboe return 0; 101b86ff981SJens Axboe } 102b86ff981SJens Axboe 103b86ff981SJens Axboe /** 104b86ff981SJens Axboe * relay_alloc_buf - allocate a channel buffer 105b86ff981SJens Axboe * @buf: the buffer struct 106b86ff981SJens Axboe * @size: total size of the buffer 107b86ff981SJens Axboe * 1084c78a663SRandy Dunlap * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The 109221415d7SJens Axboe * passed in size will get page aligned, if it isn't already. 110b86ff981SJens Axboe */ 111221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) 112b86ff981SJens Axboe { 113b86ff981SJens Axboe void *mem; 114b86ff981SJens Axboe unsigned int i, j, n_pages; 115b86ff981SJens Axboe 116221415d7SJens Axboe *size = PAGE_ALIGN(*size); 117221415d7SJens Axboe n_pages = *size >> PAGE_SHIFT; 118b86ff981SJens Axboe 11968ab3d88SMasami Hiramatsu buf->page_array = relay_alloc_page_array(n_pages); 120b86ff981SJens Axboe if (!buf->page_array) 121b86ff981SJens Axboe return NULL; 122b86ff981SJens Axboe 123b86ff981SJens Axboe for (i = 0; i < n_pages; i++) { 124b86ff981SJens Axboe buf->page_array[i] = alloc_page(GFP_KERNEL); 125b86ff981SJens Axboe if (unlikely(!buf->page_array[i])) 126b86ff981SJens Axboe goto depopulate; 127ebf99093STom Zanussi set_page_private(buf->page_array[i], (unsigned long)buf); 128b86ff981SJens Axboe } 129b86ff981SJens Axboe mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); 130b86ff981SJens Axboe if (!mem) 131b86ff981SJens Axboe goto depopulate; 132b86ff981SJens Axboe 133221415d7SJens Axboe memset(mem, 0, *size); 134b86ff981SJens Axboe buf->page_count = n_pages; 135b86ff981SJens Axboe return mem; 136b86ff981SJens Axboe 137b86ff981SJens Axboe depopulate: 138b86ff981SJens Axboe for (j = 0; j < i; j++) 139b86ff981SJens Axboe __free_page(buf->page_array[j]); 14068ab3d88SMasami Hiramatsu relay_free_page_array(buf->page_array); 141b86ff981SJens Axboe return NULL; 142b86ff981SJens Axboe } 143b86ff981SJens Axboe 144b86ff981SJens Axboe /** 145b86ff981SJens Axboe * relay_create_buf - allocate and initialize a channel buffer 1464c78a663SRandy Dunlap * @chan: the relay channel 147b86ff981SJens Axboe * 1484c78a663SRandy Dunlap * Returns channel buffer if successful, %NULL otherwise. 149b86ff981SJens Axboe */ 15001c55ed3SAdrian Bunk static struct rchan_buf *relay_create_buf(struct rchan *chan) 151b86ff981SJens Axboe { 152f6302f1bSDan Carpenter struct rchan_buf *buf; 153f6302f1bSDan Carpenter 15488913bd8SDavid Rientjes if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *)) 155b86ff981SJens Axboe return NULL; 156b86ff981SJens Axboe 157f6302f1bSDan Carpenter buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); 158f6302f1bSDan Carpenter if (!buf) 159f6302f1bSDan Carpenter return NULL; 1606da2ec56SKees Cook buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *), 1616da2ec56SKees Cook GFP_KERNEL); 162b86ff981SJens Axboe if (!buf->padding) 163b86ff981SJens Axboe goto free_buf; 164b86ff981SJens Axboe 165221415d7SJens Axboe buf->start = relay_alloc_buf(buf, &chan->alloc_size); 166b86ff981SJens Axboe if (!buf->start) 167b86ff981SJens Axboe goto free_buf; 168b86ff981SJens Axboe 169b86ff981SJens Axboe buf->chan = chan; 170b86ff981SJens Axboe kref_get(&buf->chan->kref); 171b86ff981SJens Axboe return buf; 172b86ff981SJens Axboe 173b86ff981SJens Axboe free_buf: 174b86ff981SJens Axboe kfree(buf->padding); 175b86ff981SJens Axboe kfree(buf); 176b86ff981SJens Axboe return NULL; 177b86ff981SJens Axboe } 178b86ff981SJens Axboe 179b86ff981SJens Axboe /** 180b86ff981SJens Axboe * relay_destroy_channel - free the channel struct 1814c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay channel 182b86ff981SJens Axboe * 183b86ff981SJens Axboe * Should only be called from kref_put(). 184b86ff981SJens Axboe */ 18501c55ed3SAdrian Bunk static void relay_destroy_channel(struct kref *kref) 186b86ff981SJens Axboe { 187b86ff981SJens Axboe struct rchan *chan = container_of(kref, struct rchan, kref); 18871e84329SWei Yongjun free_percpu(chan->buf); 189b86ff981SJens Axboe kfree(chan); 190b86ff981SJens Axboe } 191b86ff981SJens Axboe 192b86ff981SJens Axboe /** 193b86ff981SJens Axboe * relay_destroy_buf - destroy an rchan_buf struct and associated buffer 194b86ff981SJens Axboe * @buf: the buffer struct 195b86ff981SJens Axboe */ 19601c55ed3SAdrian Bunk static void relay_destroy_buf(struct rchan_buf *buf) 197b86ff981SJens Axboe { 198b86ff981SJens Axboe struct rchan *chan = buf->chan; 199b86ff981SJens Axboe unsigned int i; 200b86ff981SJens Axboe 201b86ff981SJens Axboe if (likely(buf->start)) { 202b86ff981SJens Axboe vunmap(buf->start); 203b86ff981SJens Axboe for (i = 0; i < buf->page_count; i++) 204b86ff981SJens Axboe __free_page(buf->page_array[i]); 20568ab3d88SMasami Hiramatsu relay_free_page_array(buf->page_array); 206b86ff981SJens Axboe } 207017c59c0SAkash Goel *per_cpu_ptr(chan->buf, buf->cpu) = NULL; 208b86ff981SJens Axboe kfree(buf->padding); 209b86ff981SJens Axboe kfree(buf); 210b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 211b86ff981SJens Axboe } 212b86ff981SJens Axboe 213b86ff981SJens Axboe /** 214b86ff981SJens Axboe * relay_remove_buf - remove a channel buffer 2154c78a663SRandy Dunlap * @kref: target kernel reference that contains the relay buffer 216b86ff981SJens Axboe * 217e227867fSMasanari Iida * Removes the file from the filesystem, which also frees the 218b86ff981SJens Axboe * rchan_buf_struct and the channel buffer. Should only be called from 219b86ff981SJens Axboe * kref_put(). 220b86ff981SJens Axboe */ 22101c55ed3SAdrian Bunk static void relay_remove_buf(struct kref *kref) 222b86ff981SJens Axboe { 223b86ff981SJens Axboe struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); 224b86ff981SJens Axboe relay_destroy_buf(buf); 225b86ff981SJens Axboe } 226b86ff981SJens Axboe 227b86ff981SJens Axboe /** 228b86ff981SJens Axboe * relay_buf_empty - boolean, is the channel buffer empty? 229b86ff981SJens Axboe * @buf: channel buffer 230b86ff981SJens Axboe * 231b86ff981SJens Axboe * Returns 1 if the buffer is empty, 0 otherwise. 232b86ff981SJens Axboe */ 23301c55ed3SAdrian Bunk static int relay_buf_empty(struct rchan_buf *buf) 234b86ff981SJens Axboe { 235b86ff981SJens Axboe return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; 236b86ff981SJens Axboe } 237b86ff981SJens Axboe 238b86ff981SJens Axboe /** 239b86ff981SJens Axboe * relay_buf_full - boolean, is the channel buffer full? 240b86ff981SJens Axboe * @buf: channel buffer 241b86ff981SJens Axboe * 242b86ff981SJens Axboe * Returns 1 if the buffer is full, 0 otherwise. 243b86ff981SJens Axboe */ 244b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf) 245b86ff981SJens Axboe { 246b86ff981SJens Axboe size_t ready = buf->subbufs_produced - buf->subbufs_consumed; 247b86ff981SJens Axboe return (ready >= buf->chan->n_subbufs) ? 1 : 0; 248b86ff981SJens Axboe } 249b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full); 250b86ff981SJens Axboe 251b86ff981SJens Axboe /* 252b86ff981SJens Axboe * High-level relay kernel API and associated functions. 253b86ff981SJens Axboe */ 254b86ff981SJens Axboe 255b86ff981SJens Axboe /* 256b86ff981SJens Axboe * rchan_callback implementations defining default channel behavior. Used 257b86ff981SJens Axboe * in place of corresponding NULL values in client callback struct. 258b86ff981SJens Axboe */ 259b86ff981SJens Axboe 260b86ff981SJens Axboe /* 261b86ff981SJens Axboe * subbuf_start() default callback. Does nothing. 262b86ff981SJens Axboe */ 263b86ff981SJens Axboe static int subbuf_start_default_callback (struct rchan_buf *buf, 264b86ff981SJens Axboe void *subbuf, 265b86ff981SJens Axboe void *prev_subbuf, 266b86ff981SJens Axboe size_t prev_padding) 267b86ff981SJens Axboe { 268b86ff981SJens Axboe if (relay_buf_full(buf)) 269b86ff981SJens Axboe return 0; 270b86ff981SJens Axboe 271b86ff981SJens Axboe return 1; 272b86ff981SJens Axboe } 273b86ff981SJens Axboe 274b86ff981SJens Axboe /* 275b86ff981SJens Axboe * create_buf_file_create() default callback. Does nothing. 276b86ff981SJens Axboe */ 277b86ff981SJens Axboe static struct dentry *create_buf_file_default_callback(const char *filename, 278b86ff981SJens Axboe struct dentry *parent, 279f4ae40a6SAl Viro umode_t mode, 280b86ff981SJens Axboe struct rchan_buf *buf, 281b86ff981SJens Axboe int *is_global) 282b86ff981SJens Axboe { 283b86ff981SJens Axboe return NULL; 284b86ff981SJens Axboe } 285b86ff981SJens Axboe 286b86ff981SJens Axboe /* 287b86ff981SJens Axboe * remove_buf_file() default callback. Does nothing. 288b86ff981SJens Axboe */ 289b86ff981SJens Axboe static int remove_buf_file_default_callback(struct dentry *dentry) 290b86ff981SJens Axboe { 291b86ff981SJens Axboe return -EINVAL; 292b86ff981SJens Axboe } 293b86ff981SJens Axboe 294b86ff981SJens Axboe /** 295b86ff981SJens Axboe * wakeup_readers - wake up readers waiting on a channel 29626b5679eSPeter Zijlstra * @work: contains the channel buffer 297b86ff981SJens Axboe * 29826b5679eSPeter Zijlstra * This is the function used to defer reader waking 299b86ff981SJens Axboe */ 30026b5679eSPeter Zijlstra static void wakeup_readers(struct irq_work *work) 301b86ff981SJens Axboe { 30226b5679eSPeter Zijlstra struct rchan_buf *buf; 30326b5679eSPeter Zijlstra 30426b5679eSPeter Zijlstra buf = container_of(work, struct rchan_buf, wakeup_work); 305b86ff981SJens Axboe wake_up_interruptible(&buf->read_wait); 306b86ff981SJens Axboe } 307b86ff981SJens Axboe 308b86ff981SJens Axboe /** 309b86ff981SJens Axboe * __relay_reset - reset a channel buffer 310b86ff981SJens Axboe * @buf: the channel buffer 311b86ff981SJens Axboe * @init: 1 if this is a first-time initialization 312b86ff981SJens Axboe * 31372fd4a35SRobert P. J. Day * See relay_reset() for description of effect. 314b86ff981SJens Axboe */ 315192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init) 316b86ff981SJens Axboe { 317b86ff981SJens Axboe size_t i; 318b86ff981SJens Axboe 319b86ff981SJens Axboe if (init) { 320b86ff981SJens Axboe init_waitqueue_head(&buf->read_wait); 321b86ff981SJens Axboe kref_init(&buf->kref); 32226b5679eSPeter Zijlstra init_irq_work(&buf->wakeup_work, wakeup_readers); 32326b5679eSPeter Zijlstra } else { 32426b5679eSPeter Zijlstra irq_work_sync(&buf->wakeup_work); 32526b5679eSPeter Zijlstra } 326b86ff981SJens Axboe 327b86ff981SJens Axboe buf->subbufs_produced = 0; 328b86ff981SJens Axboe buf->subbufs_consumed = 0; 329b86ff981SJens Axboe buf->bytes_consumed = 0; 330b86ff981SJens Axboe buf->finalized = 0; 331b86ff981SJens Axboe buf->data = buf->start; 332b86ff981SJens Axboe buf->offset = 0; 333b86ff981SJens Axboe 334b86ff981SJens Axboe for (i = 0; i < buf->chan->n_subbufs; i++) 335b86ff981SJens Axboe buf->padding[i] = 0; 336b86ff981SJens Axboe 337b86ff981SJens Axboe buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); 338b86ff981SJens Axboe } 339b86ff981SJens Axboe 340b86ff981SJens Axboe /** 341b86ff981SJens Axboe * relay_reset - reset the channel 342b86ff981SJens Axboe * @chan: the channel 343b86ff981SJens Axboe * 344b86ff981SJens Axboe * This has the effect of erasing all data from all channel buffers 345b86ff981SJens Axboe * and restarting the channel in its initial state. The buffers 346b86ff981SJens Axboe * are not freed, so any mappings are still in effect. 347b86ff981SJens Axboe * 34872fd4a35SRobert P. J. Day * NOTE. Care should be taken that the channel isn't actually 349b86ff981SJens Axboe * being used by anything when this call is made. 350b86ff981SJens Axboe */ 351b86ff981SJens Axboe void relay_reset(struct rchan *chan) 352b86ff981SJens Axboe { 353017c59c0SAkash Goel struct rchan_buf *buf; 354b86ff981SJens Axboe unsigned int i; 355b86ff981SJens Axboe 356b86ff981SJens Axboe if (!chan) 357b86ff981SJens Axboe return; 358b86ff981SJens Axboe 359017c59c0SAkash Goel if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { 360017c59c0SAkash Goel __relay_reset(buf, 0); 36123c88752SMathieu Desnoyers return; 362b86ff981SJens Axboe } 36323c88752SMathieu Desnoyers 36423c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 36598ba4031SLai Jiangshan for_each_possible_cpu(i) 366017c59c0SAkash Goel if ((buf = *per_cpu_ptr(chan->buf, i))) 367017c59c0SAkash Goel __relay_reset(buf, 0); 36823c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 369b86ff981SJens Axboe } 370b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset); 371b86ff981SJens Axboe 37220d8b67cSEduard - Gabriel Munteanu static inline void relay_set_buf_dentry(struct rchan_buf *buf, 37320d8b67cSEduard - Gabriel Munteanu struct dentry *dentry) 37420d8b67cSEduard - Gabriel Munteanu { 37520d8b67cSEduard - Gabriel Munteanu buf->dentry = dentry; 3767682c918SDavid Howells d_inode(buf->dentry)->i_size = buf->early_bytes; 37720d8b67cSEduard - Gabriel Munteanu } 37820d8b67cSEduard - Gabriel Munteanu 37920d8b67cSEduard - Gabriel Munteanu static struct dentry *relay_create_buf_file(struct rchan *chan, 38020d8b67cSEduard - Gabriel Munteanu struct rchan_buf *buf, 38120d8b67cSEduard - Gabriel Munteanu unsigned int cpu) 38220d8b67cSEduard - Gabriel Munteanu { 38320d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 38420d8b67cSEduard - Gabriel Munteanu char *tmpname; 38520d8b67cSEduard - Gabriel Munteanu 38620d8b67cSEduard - Gabriel Munteanu tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); 38720d8b67cSEduard - Gabriel Munteanu if (!tmpname) 38820d8b67cSEduard - Gabriel Munteanu return NULL; 38920d8b67cSEduard - Gabriel Munteanu snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); 39020d8b67cSEduard - Gabriel Munteanu 39120d8b67cSEduard - Gabriel Munteanu /* Create file in fs */ 39220d8b67cSEduard - Gabriel Munteanu dentry = chan->cb->create_buf_file(tmpname, chan->parent, 39320d8b67cSEduard - Gabriel Munteanu S_IRUSR, buf, 39420d8b67cSEduard - Gabriel Munteanu &chan->is_global); 3952c1cf00eSGreg Kroah-Hartman if (IS_ERR(dentry)) 3962c1cf00eSGreg Kroah-Hartman dentry = NULL; 39720d8b67cSEduard - Gabriel Munteanu 39820d8b67cSEduard - Gabriel Munteanu kfree(tmpname); 39920d8b67cSEduard - Gabriel Munteanu 40020d8b67cSEduard - Gabriel Munteanu return dentry; 40120d8b67cSEduard - Gabriel Munteanu } 40220d8b67cSEduard - Gabriel Munteanu 4034c78a663SRandy Dunlap /* 404b86ff981SJens Axboe * relay_open_buf - create a new relay channel buffer 405b86ff981SJens Axboe * 40623c88752SMathieu Desnoyers * used by relay_open() and CPU hotplug. 407b86ff981SJens Axboe */ 40823c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) 409b86ff981SJens Axboe { 41023c88752SMathieu Desnoyers struct rchan_buf *buf = NULL; 411b86ff981SJens Axboe struct dentry *dentry; 412b86ff981SJens Axboe 41323c88752SMathieu Desnoyers if (chan->is_global) 414017c59c0SAkash Goel return *per_cpu_ptr(chan->buf, 0); 415b86ff981SJens Axboe 416b86ff981SJens Axboe buf = relay_create_buf(chan); 417b86ff981SJens Axboe if (!buf) 41820d8b67cSEduard - Gabriel Munteanu return NULL; 41920d8b67cSEduard - Gabriel Munteanu 42020d8b67cSEduard - Gabriel Munteanu if (chan->has_base_filename) { 42120d8b67cSEduard - Gabriel Munteanu dentry = relay_create_buf_file(chan, buf, cpu); 42220d8b67cSEduard - Gabriel Munteanu if (!dentry) 42320d8b67cSEduard - Gabriel Munteanu goto free_buf; 42420d8b67cSEduard - Gabriel Munteanu relay_set_buf_dentry(buf, dentry); 42559dbb2a0SAkash Goel } else { 42659dbb2a0SAkash Goel /* Only retrieve global info, nothing more, nothing less */ 42759dbb2a0SAkash Goel dentry = chan->cb->create_buf_file(NULL, NULL, 42859dbb2a0SAkash Goel S_IRUSR, buf, 42959dbb2a0SAkash Goel &chan->is_global); 4302c1cf00eSGreg Kroah-Hartman if (IS_ERR_OR_NULL(dentry)) 43159dbb2a0SAkash Goel goto free_buf; 43220d8b67cSEduard - Gabriel Munteanu } 433b86ff981SJens Axboe 43423c88752SMathieu Desnoyers buf->cpu = cpu; 435b86ff981SJens Axboe __relay_reset(buf, 1); 436b86ff981SJens Axboe 43723c88752SMathieu Desnoyers if(chan->is_global) { 438017c59c0SAkash Goel *per_cpu_ptr(chan->buf, 0) = buf; 43923c88752SMathieu Desnoyers buf->cpu = 0; 44023c88752SMathieu Desnoyers } 44123c88752SMathieu Desnoyers 44220d8b67cSEduard - Gabriel Munteanu return buf; 44323c88752SMathieu Desnoyers 44423c88752SMathieu Desnoyers free_buf: 44523c88752SMathieu Desnoyers relay_destroy_buf(buf); 44620d8b67cSEduard - Gabriel Munteanu return NULL; 447b86ff981SJens Axboe } 448b86ff981SJens Axboe 449b86ff981SJens Axboe /** 450b86ff981SJens Axboe * relay_close_buf - close a channel buffer 451b86ff981SJens Axboe * @buf: channel buffer 452b86ff981SJens Axboe * 453b86ff981SJens Axboe * Marks the buffer finalized and restores the default callbacks. 454b86ff981SJens Axboe * The channel buffer and channel buffer data structure are then freed 455b86ff981SJens Axboe * automatically when the last reference is given up. 456b86ff981SJens Axboe */ 457192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf) 458b86ff981SJens Axboe { 459b86ff981SJens Axboe buf->finalized = 1; 46026b5679eSPeter Zijlstra irq_work_sync(&buf->wakeup_work); 461b8d4a5bfSDmitry Monakhov buf->chan->cb->remove_buf_file(buf->dentry); 462b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 463b86ff981SJens Axboe } 464b86ff981SJens Axboe 465192636adSAndrew Morton static void setup_callbacks(struct rchan *chan, 466b86ff981SJens Axboe struct rchan_callbacks *cb) 467b86ff981SJens Axboe { 468b86ff981SJens Axboe if (!cb->subbuf_start) 469b86ff981SJens Axboe cb->subbuf_start = subbuf_start_default_callback; 470b86ff981SJens Axboe if (!cb->create_buf_file) 471b86ff981SJens Axboe cb->create_buf_file = create_buf_file_default_callback; 472b86ff981SJens Axboe if (!cb->remove_buf_file) 473b86ff981SJens Axboe cb->remove_buf_file = remove_buf_file_default_callback; 474b86ff981SJens Axboe chan->cb = cb; 475b86ff981SJens Axboe } 476b86ff981SJens Axboe 477e6d4989aSRichard Weinberger int relay_prepare_cpu(unsigned int cpu) 47823c88752SMathieu Desnoyers { 47923c88752SMathieu Desnoyers struct rchan *chan; 480017c59c0SAkash Goel struct rchan_buf *buf; 48123c88752SMathieu Desnoyers 48223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 48323c88752SMathieu Desnoyers list_for_each_entry(chan, &relay_channels, list) { 484e6d4989aSRichard Weinberger if ((buf = *per_cpu_ptr(chan->buf, cpu))) 48523c88752SMathieu Desnoyers continue; 486e6d4989aSRichard Weinberger buf = relay_open_buf(chan, cpu); 487017c59c0SAkash Goel if (!buf) { 488e6d4989aSRichard Weinberger pr_err("relay: cpu %d buffer creation failed\n", cpu); 48923c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 490e6d4989aSRichard Weinberger return -ENOMEM; 49123c88752SMathieu Desnoyers } 492e6d4989aSRichard Weinberger *per_cpu_ptr(chan->buf, cpu) = buf; 49323c88752SMathieu Desnoyers } 49423c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 495e6d4989aSRichard Weinberger return 0; 49623c88752SMathieu Desnoyers } 49723c88752SMathieu Desnoyers 49823c88752SMathieu Desnoyers /** 499b86ff981SJens Axboe * relay_open - create a new relay channel 50020d8b67cSEduard - Gabriel Munteanu * @base_filename: base name of files to create, %NULL for buffering only 50120d8b67cSEduard - Gabriel Munteanu * @parent: dentry of parent directory, %NULL for root directory or buffer 502b86ff981SJens Axboe * @subbuf_size: size of sub-buffers 503b86ff981SJens Axboe * @n_subbufs: number of sub-buffers 504b86ff981SJens Axboe * @cb: client callback functions 50523c88752SMathieu Desnoyers * @private_data: user-defined data 506b86ff981SJens Axboe * 5074c78a663SRandy Dunlap * Returns channel pointer if successful, %NULL otherwise. 508b86ff981SJens Axboe * 509b86ff981SJens Axboe * Creates a channel buffer for each cpu using the sizes and 510b86ff981SJens Axboe * attributes specified. The created channel buffer files 511b86ff981SJens Axboe * will be named base_filename0...base_filenameN-1. File 51272fd4a35SRobert P. J. Day * permissions will be %S_IRUSR. 51359dbb2a0SAkash Goel * 51459dbb2a0SAkash Goel * If opening a buffer (@parent = NULL) that you later wish to register 51559dbb2a0SAkash Goel * in a filesystem, call relay_late_setup_files() once the @parent dentry 51659dbb2a0SAkash Goel * is available. 517b86ff981SJens Axboe */ 518b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename, 519b86ff981SJens Axboe struct dentry *parent, 520b86ff981SJens Axboe size_t subbuf_size, 521b86ff981SJens Axboe size_t n_subbufs, 52223c88752SMathieu Desnoyers struct rchan_callbacks *cb, 52323c88752SMathieu Desnoyers void *private_data) 524b86ff981SJens Axboe { 525b86ff981SJens Axboe unsigned int i; 526b86ff981SJens Axboe struct rchan *chan; 527017c59c0SAkash Goel struct rchan_buf *buf; 528b86ff981SJens Axboe 529b86ff981SJens Axboe if (!(subbuf_size && n_subbufs)) 530b86ff981SJens Axboe return NULL; 531f6302f1bSDan Carpenter if (subbuf_size > UINT_MAX / n_subbufs) 532f6302f1bSDan Carpenter return NULL; 533*6f8f2544SJani Nikula if (!cb) 534*6f8f2544SJani Nikula return NULL; 535b86ff981SJens Axboe 536cd861280SRobert P. J. Day chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); 537b86ff981SJens Axboe if (!chan) 538b86ff981SJens Axboe return NULL; 539b86ff981SJens Axboe 540017c59c0SAkash Goel chan->buf = alloc_percpu(struct rchan_buf *); 54154e200abSDaniel Axtens if (!chan->buf) { 54254e200abSDaniel Axtens kfree(chan); 54354e200abSDaniel Axtens return NULL; 54454e200abSDaniel Axtens } 54554e200abSDaniel Axtens 546b86ff981SJens Axboe chan->version = RELAYFS_CHANNEL_VERSION; 547b86ff981SJens Axboe chan->n_subbufs = n_subbufs; 548b86ff981SJens Axboe chan->subbuf_size = subbuf_size; 549a05342cbSzhangwei(Jovi) chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs); 55023c88752SMathieu Desnoyers chan->parent = parent; 55123c88752SMathieu Desnoyers chan->private_data = private_data; 55220d8b67cSEduard - Gabriel Munteanu if (base_filename) { 55320d8b67cSEduard - Gabriel Munteanu chan->has_base_filename = 1; 55423c88752SMathieu Desnoyers strlcpy(chan->base_filename, base_filename, NAME_MAX); 55520d8b67cSEduard - Gabriel Munteanu } 556b86ff981SJens Axboe setup_callbacks(chan, cb); 557b86ff981SJens Axboe kref_init(&chan->kref); 558b86ff981SJens Axboe 55923c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 560b86ff981SJens Axboe for_each_online_cpu(i) { 561017c59c0SAkash Goel buf = relay_open_buf(chan, i); 562017c59c0SAkash Goel if (!buf) 563b86ff981SJens Axboe goto free_bufs; 564017c59c0SAkash Goel *per_cpu_ptr(chan->buf, i) = buf; 565b86ff981SJens Axboe } 56623c88752SMathieu Desnoyers list_add(&chan->list, &relay_channels); 56723c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 568b86ff981SJens Axboe 569b86ff981SJens Axboe return chan; 570b86ff981SJens Axboe 571b86ff981SJens Axboe free_bufs: 57298ba4031SLai Jiangshan for_each_possible_cpu(i) { 573017c59c0SAkash Goel if ((buf = *per_cpu_ptr(chan->buf, i))) 574017c59c0SAkash Goel relay_close_buf(buf); 575b86ff981SJens Axboe } 576b86ff981SJens Axboe 577b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 57823c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 579b86ff981SJens Axboe return NULL; 580b86ff981SJens Axboe } 581b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open); 582b86ff981SJens Axboe 58320d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher { 58420d8b67cSEduard - Gabriel Munteanu struct rchan_buf *buf; 58520d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 58620d8b67cSEduard - Gabriel Munteanu }; 58720d8b67cSEduard - Gabriel Munteanu 58820d8b67cSEduard - Gabriel Munteanu /* Called in atomic context. */ 58920d8b67cSEduard - Gabriel Munteanu static void __relay_set_buf_dentry(void *info) 59020d8b67cSEduard - Gabriel Munteanu { 59120d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher *p = info; 59220d8b67cSEduard - Gabriel Munteanu 59320d8b67cSEduard - Gabriel Munteanu relay_set_buf_dentry(p->buf, p->dentry); 59420d8b67cSEduard - Gabriel Munteanu } 59520d8b67cSEduard - Gabriel Munteanu 59620d8b67cSEduard - Gabriel Munteanu /** 59720d8b67cSEduard - Gabriel Munteanu * relay_late_setup_files - triggers file creation 59820d8b67cSEduard - Gabriel Munteanu * @chan: channel to operate on 59920d8b67cSEduard - Gabriel Munteanu * @base_filename: base name of files to create 60020d8b67cSEduard - Gabriel Munteanu * @parent: dentry of parent directory, %NULL for root directory 60120d8b67cSEduard - Gabriel Munteanu * 60220d8b67cSEduard - Gabriel Munteanu * Returns 0 if successful, non-zero otherwise. 60320d8b67cSEduard - Gabriel Munteanu * 60459dbb2a0SAkash Goel * Use to setup files for a previously buffer-only channel created 60559dbb2a0SAkash Goel * by relay_open() with a NULL parent dentry. 60659dbb2a0SAkash Goel * 60759dbb2a0SAkash Goel * For example, this is useful for perfomring early tracing in kernel, 60859dbb2a0SAkash Goel * before VFS is up and then exposing the early results once the dentry 60959dbb2a0SAkash Goel * is available. 61020d8b67cSEduard - Gabriel Munteanu */ 61120d8b67cSEduard - Gabriel Munteanu int relay_late_setup_files(struct rchan *chan, 61220d8b67cSEduard - Gabriel Munteanu const char *base_filename, 61320d8b67cSEduard - Gabriel Munteanu struct dentry *parent) 61420d8b67cSEduard - Gabriel Munteanu { 61520d8b67cSEduard - Gabriel Munteanu int err = 0; 61620d8b67cSEduard - Gabriel Munteanu unsigned int i, curr_cpu; 61720d8b67cSEduard - Gabriel Munteanu unsigned long flags; 61820d8b67cSEduard - Gabriel Munteanu struct dentry *dentry; 619017c59c0SAkash Goel struct rchan_buf *buf; 62020d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher disp; 62120d8b67cSEduard - Gabriel Munteanu 62220d8b67cSEduard - Gabriel Munteanu if (!chan || !base_filename) 62320d8b67cSEduard - Gabriel Munteanu return -EINVAL; 62420d8b67cSEduard - Gabriel Munteanu 62520d8b67cSEduard - Gabriel Munteanu strlcpy(chan->base_filename, base_filename, NAME_MAX); 62620d8b67cSEduard - Gabriel Munteanu 62720d8b67cSEduard - Gabriel Munteanu mutex_lock(&relay_channels_mutex); 62820d8b67cSEduard - Gabriel Munteanu /* Is chan already set up? */ 629b786c6a9SJiri Slaby if (unlikely(chan->has_base_filename)) { 630b786c6a9SJiri Slaby mutex_unlock(&relay_channels_mutex); 63120d8b67cSEduard - Gabriel Munteanu return -EEXIST; 632b786c6a9SJiri Slaby } 63320d8b67cSEduard - Gabriel Munteanu chan->has_base_filename = 1; 63420d8b67cSEduard - Gabriel Munteanu chan->parent = parent; 63559dbb2a0SAkash Goel 63659dbb2a0SAkash Goel if (chan->is_global) { 63759dbb2a0SAkash Goel err = -EINVAL; 638017c59c0SAkash Goel buf = *per_cpu_ptr(chan->buf, 0); 639017c59c0SAkash Goel if (!WARN_ON_ONCE(!buf)) { 640017c59c0SAkash Goel dentry = relay_create_buf_file(chan, buf, 0); 64159dbb2a0SAkash Goel if (dentry && !WARN_ON_ONCE(!chan->is_global)) { 642017c59c0SAkash Goel relay_set_buf_dentry(buf, dentry); 64359dbb2a0SAkash Goel err = 0; 64459dbb2a0SAkash Goel } 64559dbb2a0SAkash Goel } 64659dbb2a0SAkash Goel mutex_unlock(&relay_channels_mutex); 64759dbb2a0SAkash Goel return err; 64859dbb2a0SAkash Goel } 64959dbb2a0SAkash Goel 65020d8b67cSEduard - Gabriel Munteanu curr_cpu = get_cpu(); 65120d8b67cSEduard - Gabriel Munteanu /* 65220d8b67cSEduard - Gabriel Munteanu * The CPU hotplug notifier ran before us and created buffers with 65320d8b67cSEduard - Gabriel Munteanu * no files associated. So it's safe to call relay_setup_buf_file() 65420d8b67cSEduard - Gabriel Munteanu * on all currently online CPUs. 65520d8b67cSEduard - Gabriel Munteanu */ 65620d8b67cSEduard - Gabriel Munteanu for_each_online_cpu(i) { 657017c59c0SAkash Goel buf = *per_cpu_ptr(chan->buf, i); 658017c59c0SAkash Goel if (unlikely(!buf)) { 6597a51cffbSIngo Molnar WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n"); 66020d8b67cSEduard - Gabriel Munteanu err = -EINVAL; 66120d8b67cSEduard - Gabriel Munteanu break; 66220d8b67cSEduard - Gabriel Munteanu } 66320d8b67cSEduard - Gabriel Munteanu 664017c59c0SAkash Goel dentry = relay_create_buf_file(chan, buf, i); 66520d8b67cSEduard - Gabriel Munteanu if (unlikely(!dentry)) { 66620d8b67cSEduard - Gabriel Munteanu err = -EINVAL; 66720d8b67cSEduard - Gabriel Munteanu break; 66820d8b67cSEduard - Gabriel Munteanu } 66920d8b67cSEduard - Gabriel Munteanu 67020d8b67cSEduard - Gabriel Munteanu if (curr_cpu == i) { 67120d8b67cSEduard - Gabriel Munteanu local_irq_save(flags); 672017c59c0SAkash Goel relay_set_buf_dentry(buf, dentry); 67320d8b67cSEduard - Gabriel Munteanu local_irq_restore(flags); 67420d8b67cSEduard - Gabriel Munteanu } else { 675017c59c0SAkash Goel disp.buf = buf; 67620d8b67cSEduard - Gabriel Munteanu disp.dentry = dentry; 67720d8b67cSEduard - Gabriel Munteanu smp_mb(); 67820d8b67cSEduard - Gabriel Munteanu /* relay_channels_mutex must be held, so wait. */ 67920d8b67cSEduard - Gabriel Munteanu err = smp_call_function_single(i, 68020d8b67cSEduard - Gabriel Munteanu __relay_set_buf_dentry, 68120d8b67cSEduard - Gabriel Munteanu &disp, 1); 68220d8b67cSEduard - Gabriel Munteanu } 68320d8b67cSEduard - Gabriel Munteanu if (unlikely(err)) 68420d8b67cSEduard - Gabriel Munteanu break; 68520d8b67cSEduard - Gabriel Munteanu } 68620d8b67cSEduard - Gabriel Munteanu put_cpu(); 68720d8b67cSEduard - Gabriel Munteanu mutex_unlock(&relay_channels_mutex); 68820d8b67cSEduard - Gabriel Munteanu 68920d8b67cSEduard - Gabriel Munteanu return err; 69020d8b67cSEduard - Gabriel Munteanu } 69159dbb2a0SAkash Goel EXPORT_SYMBOL_GPL(relay_late_setup_files); 69220d8b67cSEduard - Gabriel Munteanu 693b86ff981SJens Axboe /** 694b86ff981SJens Axboe * relay_switch_subbuf - switch to a new sub-buffer 695b86ff981SJens Axboe * @buf: channel buffer 696b86ff981SJens Axboe * @length: size of current event 697b86ff981SJens Axboe * 698b86ff981SJens Axboe * Returns either the length passed in or 0 if full. 699b86ff981SJens Axboe * 700b86ff981SJens Axboe * Performs sub-buffer-switch tasks such as invoking callbacks, 701b86ff981SJens Axboe * updating padding counts, waking up readers, etc. 702b86ff981SJens Axboe */ 703b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) 704b86ff981SJens Axboe { 705b86ff981SJens Axboe void *old, *new; 706b86ff981SJens Axboe size_t old_subbuf, new_subbuf; 707b86ff981SJens Axboe 708b86ff981SJens Axboe if (unlikely(length > buf->chan->subbuf_size)) 709b86ff981SJens Axboe goto toobig; 710b86ff981SJens Axboe 711b86ff981SJens Axboe if (buf->offset != buf->chan->subbuf_size + 1) { 712b86ff981SJens Axboe buf->prev_padding = buf->chan->subbuf_size - buf->offset; 713b86ff981SJens Axboe old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 714b86ff981SJens Axboe buf->padding[old_subbuf] = buf->prev_padding; 715b86ff981SJens Axboe buf->subbufs_produced++; 71620d8b67cSEduard - Gabriel Munteanu if (buf->dentry) 7177682c918SDavid Howells d_inode(buf->dentry)->i_size += 71820d8b67cSEduard - Gabriel Munteanu buf->chan->subbuf_size - 71920d8b67cSEduard - Gabriel Munteanu buf->padding[old_subbuf]; 72020d8b67cSEduard - Gabriel Munteanu else 72120d8b67cSEduard - Gabriel Munteanu buf->early_bytes += buf->chan->subbuf_size - 722221415d7SJens Axboe buf->padding[old_subbuf]; 723221415d7SJens Axboe smp_mb(); 72426b5679eSPeter Zijlstra if (waitqueue_active(&buf->read_wait)) { 7257c9cb383STom Zanussi /* 7267c9cb383STom Zanussi * Calling wake_up_interruptible() from here 7277c9cb383STom Zanussi * will deadlock if we happen to be logging 7287c9cb383STom Zanussi * from the scheduler (trying to re-grab 7297c9cb383STom Zanussi * rq->lock), so defer it. 7307c9cb383STom Zanussi */ 73126b5679eSPeter Zijlstra irq_work_queue(&buf->wakeup_work); 73226b5679eSPeter Zijlstra } 733b86ff981SJens Axboe } 734b86ff981SJens Axboe 735b86ff981SJens Axboe old = buf->data; 736b86ff981SJens Axboe new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 737b86ff981SJens Axboe new = buf->start + new_subbuf * buf->chan->subbuf_size; 738b86ff981SJens Axboe buf->offset = 0; 739b86ff981SJens Axboe if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { 740b86ff981SJens Axboe buf->offset = buf->chan->subbuf_size + 1; 741b86ff981SJens Axboe return 0; 742b86ff981SJens Axboe } 743b86ff981SJens Axboe buf->data = new; 744b86ff981SJens Axboe buf->padding[new_subbuf] = 0; 745b86ff981SJens Axboe 746b86ff981SJens Axboe if (unlikely(length + buf->offset > buf->chan->subbuf_size)) 747b86ff981SJens Axboe goto toobig; 748b86ff981SJens Axboe 749b86ff981SJens Axboe return length; 750b86ff981SJens Axboe 751b86ff981SJens Axboe toobig: 752b86ff981SJens Axboe buf->chan->last_toobig = length; 753b86ff981SJens Axboe return 0; 754b86ff981SJens Axboe } 755b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf); 756b86ff981SJens Axboe 757b86ff981SJens Axboe /** 758b86ff981SJens Axboe * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count 759b86ff981SJens Axboe * @chan: the channel 760b86ff981SJens Axboe * @cpu: the cpu associated with the channel buffer to update 761b86ff981SJens Axboe * @subbufs_consumed: number of sub-buffers to add to current buf's count 762b86ff981SJens Axboe * 763b86ff981SJens Axboe * Adds to the channel buffer's consumed sub-buffer count. 764b86ff981SJens Axboe * subbufs_consumed should be the number of sub-buffers newly consumed, 765b86ff981SJens Axboe * not the total consumed. 766b86ff981SJens Axboe * 76772fd4a35SRobert P. J. Day * NOTE. Kernel clients don't need to call this function if the channel 768b86ff981SJens Axboe * mode is 'overwrite'. 769b86ff981SJens Axboe */ 770b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan, 771b86ff981SJens Axboe unsigned int cpu, 772b86ff981SJens Axboe size_t subbufs_consumed) 773b86ff981SJens Axboe { 774b86ff981SJens Axboe struct rchan_buf *buf; 775b86ff981SJens Axboe 7769a29d0fbSDan Carpenter if (!chan || cpu >= NR_CPUS) 777b86ff981SJens Axboe return; 778b86ff981SJens Axboe 779017c59c0SAkash Goel buf = *per_cpu_ptr(chan->buf, cpu); 7809a29d0fbSDan Carpenter if (!buf || subbufs_consumed > chan->n_subbufs) 781b86ff981SJens Axboe return; 782b86ff981SJens Axboe 7832c53d910SAravind Srinivasan if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed) 784b86ff981SJens Axboe buf->subbufs_consumed = buf->subbufs_produced; 7852c53d910SAravind Srinivasan else 7862c53d910SAravind Srinivasan buf->subbufs_consumed += subbufs_consumed; 787b86ff981SJens Axboe } 788b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed); 789b86ff981SJens Axboe 790b86ff981SJens Axboe /** 791b86ff981SJens Axboe * relay_close - close the channel 792b86ff981SJens Axboe * @chan: the channel 793b86ff981SJens Axboe * 794b86ff981SJens Axboe * Closes all channel buffers and frees the channel. 795b86ff981SJens Axboe */ 796b86ff981SJens Axboe void relay_close(struct rchan *chan) 797b86ff981SJens Axboe { 798017c59c0SAkash Goel struct rchan_buf *buf; 799b86ff981SJens Axboe unsigned int i; 800b86ff981SJens Axboe 801b86ff981SJens Axboe if (!chan) 802b86ff981SJens Axboe return; 803b86ff981SJens Axboe 80423c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 805017c59c0SAkash Goel if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) 806017c59c0SAkash Goel relay_close_buf(buf); 80723c88752SMathieu Desnoyers else 80823c88752SMathieu Desnoyers for_each_possible_cpu(i) 809017c59c0SAkash Goel if ((buf = *per_cpu_ptr(chan->buf, i))) 810017c59c0SAkash Goel relay_close_buf(buf); 811b86ff981SJens Axboe 812b86ff981SJens Axboe if (chan->last_toobig) 813b86ff981SJens Axboe printk(KERN_WARNING "relay: one or more items not logged " 8145b5e0928SAlexey Dobriyan "[item size (%zd) > sub-buffer size (%zd)]\n", 815b86ff981SJens Axboe chan->last_toobig, chan->subbuf_size); 816b86ff981SJens Axboe 81723c88752SMathieu Desnoyers list_del(&chan->list); 818b86ff981SJens Axboe kref_put(&chan->kref, relay_destroy_channel); 81923c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 820b86ff981SJens Axboe } 821b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close); 822b86ff981SJens Axboe 823b86ff981SJens Axboe /** 824b86ff981SJens Axboe * relay_flush - close the channel 825b86ff981SJens Axboe * @chan: the channel 826b86ff981SJens Axboe * 8274c78a663SRandy Dunlap * Flushes all channel buffers, i.e. forces buffer switch. 828b86ff981SJens Axboe */ 829b86ff981SJens Axboe void relay_flush(struct rchan *chan) 830b86ff981SJens Axboe { 831017c59c0SAkash Goel struct rchan_buf *buf; 832b86ff981SJens Axboe unsigned int i; 833b86ff981SJens Axboe 834b86ff981SJens Axboe if (!chan) 835b86ff981SJens Axboe return; 836b86ff981SJens Axboe 837017c59c0SAkash Goel if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { 838017c59c0SAkash Goel relay_switch_subbuf(buf, 0); 83923c88752SMathieu Desnoyers return; 840b86ff981SJens Axboe } 84123c88752SMathieu Desnoyers 84223c88752SMathieu Desnoyers mutex_lock(&relay_channels_mutex); 84323c88752SMathieu Desnoyers for_each_possible_cpu(i) 844017c59c0SAkash Goel if ((buf = *per_cpu_ptr(chan->buf, i))) 845017c59c0SAkash Goel relay_switch_subbuf(buf, 0); 84623c88752SMathieu Desnoyers mutex_unlock(&relay_channels_mutex); 847b86ff981SJens Axboe } 848b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush); 849b86ff981SJens Axboe 850b86ff981SJens Axboe /** 851b86ff981SJens Axboe * relay_file_open - open file op for relay files 852b86ff981SJens Axboe * @inode: the inode 853b86ff981SJens Axboe * @filp: the file 854b86ff981SJens Axboe * 855b86ff981SJens Axboe * Increments the channel buffer refcount. 856b86ff981SJens Axboe */ 857b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp) 858b86ff981SJens Axboe { 8598e18e294STheodore Ts'o struct rchan_buf *buf = inode->i_private; 860b86ff981SJens Axboe kref_get(&buf->kref); 861b86ff981SJens Axboe filp->private_data = buf; 862b86ff981SJens Axboe 86337529fe9SLai Jiangshan return nonseekable_open(inode, filp); 864b86ff981SJens Axboe } 865b86ff981SJens Axboe 866b86ff981SJens Axboe /** 867b86ff981SJens Axboe * relay_file_mmap - mmap file op for relay files 868b86ff981SJens Axboe * @filp: the file 869b86ff981SJens Axboe * @vma: the vma describing what to map 870b86ff981SJens Axboe * 87172fd4a35SRobert P. J. Day * Calls upon relay_mmap_buf() to map the file into user space. 872b86ff981SJens Axboe */ 873b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) 874b86ff981SJens Axboe { 875b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 876b86ff981SJens Axboe return relay_mmap_buf(buf, vma); 877b86ff981SJens Axboe } 878b86ff981SJens Axboe 879b86ff981SJens Axboe /** 880b86ff981SJens Axboe * relay_file_poll - poll file op for relay files 881b86ff981SJens Axboe * @filp: the file 882b86ff981SJens Axboe * @wait: poll table 883b86ff981SJens Axboe * 884b86ff981SJens Axboe * Poll implemention. 885b86ff981SJens Axboe */ 8869dd95748SAl Viro static __poll_t relay_file_poll(struct file *filp, poll_table *wait) 887b86ff981SJens Axboe { 8889dd95748SAl Viro __poll_t mask = 0; 889b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 890b86ff981SJens Axboe 891b86ff981SJens Axboe if (buf->finalized) 892a9a08845SLinus Torvalds return EPOLLERR; 893b86ff981SJens Axboe 894b86ff981SJens Axboe if (filp->f_mode & FMODE_READ) { 895b86ff981SJens Axboe poll_wait(filp, &buf->read_wait, wait); 896b86ff981SJens Axboe if (!relay_buf_empty(buf)) 897a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 898b86ff981SJens Axboe } 899b86ff981SJens Axboe 900b86ff981SJens Axboe return mask; 901b86ff981SJens Axboe } 902b86ff981SJens Axboe 903b86ff981SJens Axboe /** 904b86ff981SJens Axboe * relay_file_release - release file op for relay files 905b86ff981SJens Axboe * @inode: the inode 906b86ff981SJens Axboe * @filp: the file 907b86ff981SJens Axboe * 908b86ff981SJens Axboe * Decrements the channel refcount, as the filesystem is 909b86ff981SJens Axboe * no longer using it. 910b86ff981SJens Axboe */ 911b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp) 912b86ff981SJens Axboe { 913b86ff981SJens Axboe struct rchan_buf *buf = filp->private_data; 914b86ff981SJens Axboe kref_put(&buf->kref, relay_remove_buf); 915b86ff981SJens Axboe 916b86ff981SJens Axboe return 0; 917b86ff981SJens Axboe } 918b86ff981SJens Axboe 9194c78a663SRandy Dunlap /* 920b86ff981SJens Axboe * relay_file_read_consume - update the consumed count for the buffer 921b86ff981SJens Axboe */ 922b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf, 923b86ff981SJens Axboe size_t read_pos, 924b86ff981SJens Axboe size_t bytes_consumed) 925b86ff981SJens Axboe { 926b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 927b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 928b86ff981SJens Axboe size_t read_subbuf; 929b86ff981SJens Axboe 93032194450STom Zanussi if (buf->subbufs_produced == buf->subbufs_consumed && 93132194450STom Zanussi buf->offset == buf->bytes_consumed) 93232194450STom Zanussi return; 93332194450STom Zanussi 934b86ff981SJens Axboe if (buf->bytes_consumed + bytes_consumed > subbuf_size) { 935b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 936b86ff981SJens Axboe buf->bytes_consumed = 0; 937b86ff981SJens Axboe } 938b86ff981SJens Axboe 939b86ff981SJens Axboe buf->bytes_consumed += bytes_consumed; 940a66e356cSMasami Hiramatsu if (!read_pos) 941a66e356cSMasami Hiramatsu read_subbuf = buf->subbufs_consumed % n_subbufs; 942a66e356cSMasami Hiramatsu else 943b86ff981SJens Axboe read_subbuf = read_pos / buf->chan->subbuf_size; 944b86ff981SJens Axboe if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { 945b86ff981SJens Axboe if ((read_subbuf == buf->subbufs_produced % n_subbufs) && 946b86ff981SJens Axboe (buf->offset == subbuf_size)) 947b86ff981SJens Axboe return; 948b86ff981SJens Axboe relay_subbufs_consumed(buf->chan, buf->cpu, 1); 949b86ff981SJens Axboe buf->bytes_consumed = 0; 950b86ff981SJens Axboe } 951b86ff981SJens Axboe } 952b86ff981SJens Axboe 9534c78a663SRandy Dunlap /* 954b86ff981SJens Axboe * relay_file_read_avail - boolean, are there unconsumed bytes available? 955b86ff981SJens Axboe */ 956341a7213SPengcheng Yang static int relay_file_read_avail(struct rchan_buf *buf) 957b86ff981SJens Axboe { 958b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 959b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 960221415d7SJens Axboe size_t produced = buf->subbufs_produced; 961ac05b7a1SSudip Mukherjee size_t consumed; 962b86ff981SJens Axboe 963341a7213SPengcheng Yang relay_file_read_consume(buf, 0, 0); 964b86ff981SJens Axboe 96532194450STom Zanussi consumed = buf->subbufs_consumed; 96632194450STom Zanussi 967221415d7SJens Axboe if (unlikely(buf->offset > subbuf_size)) { 968221415d7SJens Axboe if (produced == consumed) 969221415d7SJens Axboe return 0; 970221415d7SJens Axboe return 1; 971221415d7SJens Axboe } 972221415d7SJens Axboe 973221415d7SJens Axboe if (unlikely(produced - consumed >= n_subbufs)) { 974a66e356cSMasami Hiramatsu consumed = produced - n_subbufs + 1; 975221415d7SJens Axboe buf->subbufs_consumed = consumed; 976a66e356cSMasami Hiramatsu buf->bytes_consumed = 0; 977221415d7SJens Axboe } 978221415d7SJens Axboe 979221415d7SJens Axboe produced = (produced % n_subbufs) * subbuf_size + buf->offset; 980221415d7SJens Axboe consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; 981221415d7SJens Axboe 982221415d7SJens Axboe if (consumed > produced) 983221415d7SJens Axboe produced += n_subbufs * subbuf_size; 984221415d7SJens Axboe 98532194450STom Zanussi if (consumed == produced) { 98632194450STom Zanussi if (buf->offset == subbuf_size && 98732194450STom Zanussi buf->subbufs_produced > buf->subbufs_consumed) 98832194450STom Zanussi return 1; 989221415d7SJens Axboe return 0; 99032194450STom Zanussi } 991221415d7SJens Axboe 992b86ff981SJens Axboe return 1; 993b86ff981SJens Axboe } 994b86ff981SJens Axboe 995b86ff981SJens Axboe /** 996b86ff981SJens Axboe * relay_file_read_subbuf_avail - return bytes available in sub-buffer 9974c78a663SRandy Dunlap * @read_pos: file read position 9984c78a663SRandy Dunlap * @buf: relay channel buffer 999b86ff981SJens Axboe */ 1000b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos, 1001b86ff981SJens Axboe struct rchan_buf *buf) 1002b86ff981SJens Axboe { 1003b86ff981SJens Axboe size_t padding, avail = 0; 1004b86ff981SJens Axboe size_t read_subbuf, read_offset, write_subbuf, write_offset; 1005b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1006b86ff981SJens Axboe 1007b86ff981SJens Axboe write_subbuf = (buf->data - buf->start) / subbuf_size; 1008b86ff981SJens Axboe write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; 1009b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1010b86ff981SJens Axboe read_offset = read_pos % subbuf_size; 1011b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1012b86ff981SJens Axboe 1013b86ff981SJens Axboe if (read_subbuf == write_subbuf) { 1014b86ff981SJens Axboe if (read_offset + padding < write_offset) 1015b86ff981SJens Axboe avail = write_offset - (read_offset + padding); 1016b86ff981SJens Axboe } else 1017b86ff981SJens Axboe avail = (subbuf_size - padding) - read_offset; 1018b86ff981SJens Axboe 1019b86ff981SJens Axboe return avail; 1020b86ff981SJens Axboe } 1021b86ff981SJens Axboe 1022b86ff981SJens Axboe /** 1023b86ff981SJens Axboe * relay_file_read_start_pos - find the first available byte to read 10244c78a663SRandy Dunlap * @buf: relay channel buffer 1025b86ff981SJens Axboe * 1026341a7213SPengcheng Yang * If the read_pos is in the middle of padding, return the 1027b86ff981SJens Axboe * position of the first actually available byte, otherwise 1028b86ff981SJens Axboe * return the original value. 1029b86ff981SJens Axboe */ 1030341a7213SPengcheng Yang static size_t relay_file_read_start_pos(struct rchan_buf *buf) 1031b86ff981SJens Axboe { 1032b86ff981SJens Axboe size_t read_subbuf, padding, padding_start, padding_end; 1033b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1034b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 10358d62fdebSDavid Wilder size_t consumed = buf->subbufs_consumed % n_subbufs; 1036341a7213SPengcheng Yang size_t read_pos = consumed * subbuf_size + buf->bytes_consumed; 1037b86ff981SJens Axboe 1038b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1039b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1040b86ff981SJens Axboe padding_start = (read_subbuf + 1) * subbuf_size - padding; 1041b86ff981SJens Axboe padding_end = (read_subbuf + 1) * subbuf_size; 1042b86ff981SJens Axboe if (read_pos >= padding_start && read_pos < padding_end) { 1043b86ff981SJens Axboe read_subbuf = (read_subbuf + 1) % n_subbufs; 1044b86ff981SJens Axboe read_pos = read_subbuf * subbuf_size; 1045b86ff981SJens Axboe } 1046b86ff981SJens Axboe 1047b86ff981SJens Axboe return read_pos; 1048b86ff981SJens Axboe } 1049b86ff981SJens Axboe 1050b86ff981SJens Axboe /** 1051b86ff981SJens Axboe * relay_file_read_end_pos - return the new read position 10524c78a663SRandy Dunlap * @read_pos: file read position 10534c78a663SRandy Dunlap * @buf: relay channel buffer 10544c78a663SRandy Dunlap * @count: number of bytes to be read 1055b86ff981SJens Axboe */ 1056b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf, 1057b86ff981SJens Axboe size_t read_pos, 1058b86ff981SJens Axboe size_t count) 1059b86ff981SJens Axboe { 1060b86ff981SJens Axboe size_t read_subbuf, padding, end_pos; 1061b86ff981SJens Axboe size_t subbuf_size = buf->chan->subbuf_size; 1062b86ff981SJens Axboe size_t n_subbufs = buf->chan->n_subbufs; 1063b86ff981SJens Axboe 1064b86ff981SJens Axboe read_subbuf = read_pos / subbuf_size; 1065b86ff981SJens Axboe padding = buf->padding[read_subbuf]; 1066b86ff981SJens Axboe if (read_pos % subbuf_size + count + padding == subbuf_size) 1067b86ff981SJens Axboe end_pos = (read_subbuf + 1) * subbuf_size; 1068b86ff981SJens Axboe else 1069b86ff981SJens Axboe end_pos = read_pos + count; 1070b86ff981SJens Axboe if (end_pos >= subbuf_size * n_subbufs) 1071b86ff981SJens Axboe end_pos = 0; 1072b86ff981SJens Axboe 1073b86ff981SJens Axboe return end_pos; 1074b86ff981SJens Axboe } 1075b86ff981SJens Axboe 1076a7c22421SAl Viro static ssize_t relay_file_read(struct file *filp, 1077a7c22421SAl Viro char __user *buffer, 1078a7c22421SAl Viro size_t count, 1079a7c22421SAl Viro loff_t *ppos) 1080221415d7SJens Axboe { 1081221415d7SJens Axboe struct rchan_buf *buf = filp->private_data; 1082221415d7SJens Axboe size_t read_start, avail; 1083a7c22421SAl Viro size_t written = 0; 10846dac40a7STom Zanussi int ret; 1085221415d7SJens Axboe 1086a7c22421SAl Viro if (!count) 1087221415d7SJens Axboe return 0; 1088221415d7SJens Axboe 10895955102cSAl Viro inode_lock(file_inode(filp)); 10906dac40a7STom Zanussi do { 1091a7c22421SAl Viro void *from; 1092a7c22421SAl Viro 1093341a7213SPengcheng Yang if (!relay_file_read_avail(buf)) 1094221415d7SJens Axboe break; 1095221415d7SJens Axboe 1096341a7213SPengcheng Yang read_start = relay_file_read_start_pos(buf); 10976dac40a7STom Zanussi avail = relay_file_read_subbuf_avail(read_start, buf); 10986dac40a7STom Zanussi if (!avail) 10996dac40a7STom Zanussi break; 1100221415d7SJens Axboe 1101a7c22421SAl Viro avail = min(count, avail); 1102a7c22421SAl Viro from = buf->start + read_start; 1103a7c22421SAl Viro ret = avail; 1104a7c22421SAl Viro if (copy_to_user(buffer, from, avail)) 11056dac40a7STom Zanussi break; 11066dac40a7STom Zanussi 1107a7c22421SAl Viro buffer += ret; 1108a7c22421SAl Viro written += ret; 1109a7c22421SAl Viro count -= ret; 1110a7c22421SAl Viro 1111221415d7SJens Axboe relay_file_read_consume(buf, read_start, ret); 1112221415d7SJens Axboe *ppos = relay_file_read_end_pos(buf, read_start, ret); 1113a7c22421SAl Viro } while (count); 11145955102cSAl Viro inode_unlock(file_inode(filp)); 1115221415d7SJens Axboe 1116a7c22421SAl Viro return written; 11176dac40a7STom Zanussi } 11186dac40a7STom Zanussi 11191db60cf2SJens Axboe static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) 11201db60cf2SJens Axboe { 11211db60cf2SJens Axboe rbuf->bytes_consumed += bytes_consumed; 11221db60cf2SJens Axboe 11231db60cf2SJens Axboe if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { 11241db60cf2SJens Axboe relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); 11251db60cf2SJens Axboe rbuf->bytes_consumed %= rbuf->chan->subbuf_size; 11261db60cf2SJens Axboe } 11271db60cf2SJens Axboe } 11281db60cf2SJens Axboe 1129ebf99093STom Zanussi static void relay_pipe_buf_release(struct pipe_inode_info *pipe, 1130ebf99093STom Zanussi struct pipe_buffer *buf) 1131221415d7SJens Axboe { 1132ebf99093STom Zanussi struct rchan_buf *rbuf; 1133ebf99093STom Zanussi 1134ebf99093STom Zanussi rbuf = (struct rchan_buf *)page_private(buf->page); 11351db60cf2SJens Axboe relay_consume_bytes(rbuf, buf->private); 1136ebf99093STom Zanussi } 1137ebf99093STom Zanussi 113828dfef8fSAlexey Dobriyan static const struct pipe_buf_operations relay_pipe_buf_ops = { 1139ebf99093STom Zanussi .release = relay_pipe_buf_release, 1140c928f642SChristoph Hellwig .try_steal = generic_pipe_buf_try_steal, 1141ebf99093STom Zanussi .get = generic_pipe_buf_get, 1142ebf99093STom Zanussi }; 1143ebf99093STom Zanussi 11445eb7f9faSJens Axboe static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i) 11455eb7f9faSJens Axboe { 11465eb7f9faSJens Axboe } 11475eb7f9faSJens Axboe 1148d3f35d98STom Zanussi /* 1149ebf99093STom Zanussi * subbuf_splice_actor - splice up to one subbuf's worth of data 1150ebf99093STom Zanussi */ 11515f1664f9SDan Carpenter static ssize_t subbuf_splice_actor(struct file *in, 1152ebf99093STom Zanussi loff_t *ppos, 1153ebf99093STom Zanussi struct pipe_inode_info *pipe, 1154ebf99093STom Zanussi size_t len, 1155ebf99093STom Zanussi unsigned int flags, 1156ebf99093STom Zanussi int *nonpad_ret) 1157ebf99093STom Zanussi { 11585f1664f9SDan Carpenter unsigned int pidx, poff, total_len, subbuf_pages, nr_pages; 1159ebf99093STom Zanussi struct rchan_buf *rbuf = in->private_data; 1160ebf99093STom Zanussi unsigned int subbuf_size = rbuf->chan->subbuf_size; 116124da24deSTom Zanussi uint64_t pos = (uint64_t) *ppos; 116224da24deSTom Zanussi uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; 116324da24deSTom Zanussi size_t read_start = (size_t) do_div(pos, alloc_size); 1164ebf99093STom Zanussi size_t read_subbuf = read_start / subbuf_size; 1165ebf99093STom Zanussi size_t padding = rbuf->padding[read_subbuf]; 1166ebf99093STom Zanussi size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; 116735f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 116835f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 11691db60cf2SJens Axboe struct splice_pipe_desc spd = { 11701db60cf2SJens Axboe .pages = pages, 11711db60cf2SJens Axboe .nr_pages = 0, 1172047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 11731db60cf2SJens Axboe .partial = partial, 11741db60cf2SJens Axboe .ops = &relay_pipe_buf_ops, 11755eb7f9faSJens Axboe .spd_release = relay_page_release, 11761db60cf2SJens Axboe }; 11775f1664f9SDan Carpenter ssize_t ret; 1178ebf99093STom Zanussi 1179ebf99093STom Zanussi if (rbuf->subbufs_produced == rbuf->subbufs_consumed) 1180ebf99093STom Zanussi return 0; 118135f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 118235f3d14dSJens Axboe return -ENOMEM; 1183ebf99093STom Zanussi 11841db60cf2SJens Axboe /* 11851db60cf2SJens Axboe * Adjust read len, if longer than what is available 11861db60cf2SJens Axboe */ 11871db60cf2SJens Axboe if (len > (subbuf_size - read_start % subbuf_size)) 11881db60cf2SJens Axboe len = subbuf_size - read_start % subbuf_size; 1189ebf99093STom Zanussi 1190ebf99093STom Zanussi subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; 1191ebf99093STom Zanussi pidx = (read_start / PAGE_SIZE) % subbuf_pages; 1192ebf99093STom Zanussi poff = read_start & ~PAGE_MASK; 1193a786c06dSAl Viro nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max); 1194ebf99093STom Zanussi 119516d54669SJens Axboe for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) { 11961db60cf2SJens Axboe unsigned int this_len, this_end, private; 11971db60cf2SJens Axboe unsigned int cur_pos = read_start + total_len; 1198ebf99093STom Zanussi 11991db60cf2SJens Axboe if (!len) 1200ebf99093STom Zanussi break; 12011db60cf2SJens Axboe 12021db60cf2SJens Axboe this_len = min_t(unsigned long, len, PAGE_SIZE - poff); 12031db60cf2SJens Axboe private = this_len; 12041db60cf2SJens Axboe 12051db60cf2SJens Axboe spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; 12061db60cf2SJens Axboe spd.partial[spd.nr_pages].offset = poff; 12071db60cf2SJens Axboe 12081db60cf2SJens Axboe this_end = cur_pos + this_len; 12091db60cf2SJens Axboe if (this_end >= nonpad_end) { 12101db60cf2SJens Axboe this_len = nonpad_end - cur_pos; 12111db60cf2SJens Axboe private = this_len + padding; 1212ebf99093STom Zanussi } 12131db60cf2SJens Axboe spd.partial[spd.nr_pages].len = this_len; 12141db60cf2SJens Axboe spd.partial[spd.nr_pages].private = private; 1215ebf99093STom Zanussi 12161db60cf2SJens Axboe len -= this_len; 12171db60cf2SJens Axboe total_len += this_len; 1218ebf99093STom Zanussi poff = 0; 1219ebf99093STom Zanussi pidx = (pidx + 1) % subbuf_pages; 1220ebf99093STom Zanussi 12211db60cf2SJens Axboe if (this_end >= nonpad_end) { 12221db60cf2SJens Axboe spd.nr_pages++; 1223ebf99093STom Zanussi break; 1224ebf99093STom Zanussi } 1225ebf99093STom Zanussi } 1226ebf99093STom Zanussi 122735f3d14dSJens Axboe ret = 0; 12281db60cf2SJens Axboe if (!spd.nr_pages) 122935f3d14dSJens Axboe goto out; 1230ebf99093STom Zanussi 12311db60cf2SJens Axboe ret = *nonpad_ret = splice_to_pipe(pipe, &spd); 12321db60cf2SJens Axboe if (ret < 0 || ret < total_len) 123335f3d14dSJens Axboe goto out; 1234ebf99093STom Zanussi 12351db60cf2SJens Axboe if (read_start + ret == nonpad_end) 12361db60cf2SJens Axboe ret += padding; 1237ebf99093STom Zanussi 123835f3d14dSJens Axboe out: 1239047fe360SEric Dumazet splice_shrink_spd(&spd); 1240ebf99093STom Zanussi return ret; 1241ebf99093STom Zanussi } 1242ebf99093STom Zanussi 1243ebf99093STom Zanussi static ssize_t relay_file_splice_read(struct file *in, 1244ebf99093STom Zanussi loff_t *ppos, 1245ebf99093STom Zanussi struct pipe_inode_info *pipe, 1246ebf99093STom Zanussi size_t len, 1247ebf99093STom Zanussi unsigned int flags) 1248ebf99093STom Zanussi { 1249ebf99093STom Zanussi ssize_t spliced; 1250ebf99093STom Zanussi int ret; 1251ebf99093STom Zanussi int nonpad_ret = 0; 1252ebf99093STom Zanussi 1253ebf99093STom Zanussi ret = 0; 1254ebf99093STom Zanussi spliced = 0; 1255ebf99093STom Zanussi 1256a82c53a0STom Zanussi while (len && !spliced) { 1257ebf99093STom Zanussi ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); 1258ebf99093STom Zanussi if (ret < 0) 1259ebf99093STom Zanussi break; 1260ebf99093STom Zanussi else if (!ret) { 1261fbb5b7aeSTom Zanussi if (flags & SPLICE_F_NONBLOCK) 1262ebf99093STom Zanussi ret = -EAGAIN; 1263ebf99093STom Zanussi break; 1264ebf99093STom Zanussi } 1265ebf99093STom Zanussi 1266ebf99093STom Zanussi *ppos += ret; 1267ebf99093STom Zanussi if (ret > len) 1268ebf99093STom Zanussi len = 0; 1269ebf99093STom Zanussi else 1270ebf99093STom Zanussi len -= ret; 1271ebf99093STom Zanussi spliced += nonpad_ret; 1272ebf99093STom Zanussi nonpad_ret = 0; 1273ebf99093STom Zanussi } 1274ebf99093STom Zanussi 1275ebf99093STom Zanussi if (spliced) 1276ebf99093STom Zanussi return spliced; 1277ebf99093STom Zanussi 1278ebf99093STom Zanussi return ret; 1279221415d7SJens Axboe } 1280221415d7SJens Axboe 128115ad7cdcSHelge Deller const struct file_operations relay_file_operations = { 1282b86ff981SJens Axboe .open = relay_file_open, 1283b86ff981SJens Axboe .poll = relay_file_poll, 1284b86ff981SJens Axboe .mmap = relay_file_mmap, 1285b86ff981SJens Axboe .read = relay_file_read, 1286b86ff981SJens Axboe .llseek = no_llseek, 1287b86ff981SJens Axboe .release = relay_file_release, 1288ebf99093STom Zanussi .splice_read = relay_file_splice_read, 1289b86ff981SJens Axboe }; 1290b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations); 1291