1 /* 2 * Public API and common code for kernel->userspace relay file support. 3 * 4 * See Documentation/filesystems/relay.rst for an overview. 5 * 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. 10 * November 2006 - CPU hotplug support by Mathieu Desnoyers 11 * (mathieu.desnoyers@polymtl.ca) 12 * 13 * This file is released under the GPL. 14 */ 15 #include <linux/errno.h> 16 #include <linux/stddef.h> 17 #include <linux/slab.h> 18 #include <linux/export.h> 19 #include <linux/string.h> 20 #include <linux/relay.h> 21 #include <linux/vmalloc.h> 22 #include <linux/mm.h> 23 #include <linux/cpu.h> 24 #include <linux/splice.h> 25 26 /* list of open channels, for cpu hotplug */ 27 static DEFINE_MUTEX(relay_channels_mutex); 28 static LIST_HEAD(relay_channels); 29 30 /* 31 * fault() vm_op implementation for relay file mapping. 32 */ 33 static vm_fault_t relay_buf_fault(struct vm_fault *vmf) 34 { 35 struct page *page; 36 struct rchan_buf *buf = vmf->vma->vm_private_data; 37 pgoff_t pgoff = vmf->pgoff; 38 39 if (!buf) 40 return VM_FAULT_OOM; 41 42 page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); 43 if (!page) 44 return VM_FAULT_SIGBUS; 45 get_page(page); 46 vmf->page = page; 47 48 return 0; 49 } 50 51 /* 52 * vm_ops for relay file mappings. 53 */ 54 static const struct vm_operations_struct relay_file_mmap_ops = { 55 .fault = relay_buf_fault, 56 }; 57 58 /* 59 * allocate an array of pointers of struct page 60 */ 61 static struct page **relay_alloc_page_array(unsigned int n_pages) 62 { 63 return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); 64 } 65 66 /* 67 * free an array of pointers of struct page 68 */ 69 static void relay_free_page_array(struct page **array) 70 { 71 kvfree(array); 72 } 73 74 /** 75 * relay_mmap_buf: - mmap channel buffer to process address space 76 * @buf: relay channel buffer 77 * @vma: vm_area_struct describing memory to be mapped 78 * 79 * Returns 0 if ok, negative on error 80 * 81 * Caller should already have grabbed mmap_lock. 82 */ 83 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) 84 { 85 unsigned long length = vma->vm_end - vma->vm_start; 86 87 if (!buf) 88 return -EBADF; 89 90 if (length != (unsigned long)buf->chan->alloc_size) 91 return -EINVAL; 92 93 vma->vm_ops = &relay_file_mmap_ops; 94 vm_flags_set(vma, VM_DONTEXPAND); 95 vma->vm_private_data = buf; 96 97 return 0; 98 } 99 100 /** 101 * relay_alloc_buf - allocate a channel buffer 102 * @buf: the buffer struct 103 * @size: total size of the buffer 104 * 105 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The 106 * passed in size will get page aligned, if it isn't already. 107 */ 108 static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) 109 { 110 void *mem; 111 unsigned int i, j, n_pages; 112 113 *size = PAGE_ALIGN(*size); 114 n_pages = *size >> PAGE_SHIFT; 115 116 buf->page_array = relay_alloc_page_array(n_pages); 117 if (!buf->page_array) 118 return NULL; 119 120 for (i = 0; i < n_pages; i++) { 121 buf->page_array[i] = alloc_page(GFP_KERNEL | __GFP_ZERO); 122 if (unlikely(!buf->page_array[i])) 123 goto depopulate; 124 set_page_private(buf->page_array[i], (unsigned long)buf); 125 } 126 mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); 127 if (!mem) 128 goto depopulate; 129 130 buf->page_count = n_pages; 131 return mem; 132 133 depopulate: 134 for (j = 0; j < i; j++) 135 __free_page(buf->page_array[j]); 136 relay_free_page_array(buf->page_array); 137 return NULL; 138 } 139 140 /** 141 * relay_create_buf - allocate and initialize a channel buffer 142 * @chan: the relay channel 143 * 144 * Returns channel buffer if successful, %NULL otherwise. 145 */ 146 static struct rchan_buf *relay_create_buf(struct rchan *chan) 147 { 148 struct rchan_buf *buf; 149 150 if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t)) 151 return NULL; 152 153 buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); 154 if (!buf) 155 return NULL; 156 buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t), 157 GFP_KERNEL); 158 if (!buf->padding) 159 goto free_buf; 160 161 buf->start = relay_alloc_buf(buf, &chan->alloc_size); 162 if (!buf->start) 163 goto free_buf; 164 165 buf->chan = chan; 166 kref_get(&buf->chan->kref); 167 return buf; 168 169 free_buf: 170 kfree(buf->padding); 171 kfree(buf); 172 return NULL; 173 } 174 175 /** 176 * relay_destroy_channel - free the channel struct 177 * @kref: target kernel reference that contains the relay channel 178 * 179 * Should only be called from kref_put(). 180 */ 181 static void relay_destroy_channel(struct kref *kref) 182 { 183 struct rchan *chan = container_of(kref, struct rchan, kref); 184 free_percpu(chan->buf); 185 kfree(chan); 186 } 187 188 /** 189 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer 190 * @buf: the buffer struct 191 */ 192 static void relay_destroy_buf(struct rchan_buf *buf) 193 { 194 struct rchan *chan = buf->chan; 195 unsigned int i; 196 197 if (likely(buf->start)) { 198 vunmap(buf->start); 199 for (i = 0; i < buf->page_count; i++) 200 __free_page(buf->page_array[i]); 201 relay_free_page_array(buf->page_array); 202 } 203 *per_cpu_ptr(chan->buf, buf->cpu) = NULL; 204 kfree(buf->padding); 205 kfree(buf); 206 kref_put(&chan->kref, relay_destroy_channel); 207 } 208 209 /** 210 * relay_remove_buf - remove a channel buffer 211 * @kref: target kernel reference that contains the relay buffer 212 * 213 * Removes the file from the filesystem, which also frees the 214 * rchan_buf_struct and the channel buffer. Should only be called from 215 * kref_put(). 216 */ 217 static void relay_remove_buf(struct kref *kref) 218 { 219 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); 220 relay_destroy_buf(buf); 221 } 222 223 /** 224 * relay_buf_empty - boolean, is the channel buffer empty? 225 * @buf: channel buffer 226 * 227 * Returns 1 if the buffer is empty, 0 otherwise. 228 */ 229 static int relay_buf_empty(struct rchan_buf *buf) 230 { 231 return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; 232 } 233 234 /** 235 * relay_buf_full - boolean, is the channel buffer full? 236 * @buf: channel buffer 237 * 238 * Returns 1 if the buffer is full, 0 otherwise. 239 */ 240 int relay_buf_full(struct rchan_buf *buf) 241 { 242 size_t ready = buf->subbufs_produced - buf->subbufs_consumed; 243 return (ready >= buf->chan->n_subbufs) ? 1 : 0; 244 } 245 EXPORT_SYMBOL_GPL(relay_buf_full); 246 247 /* 248 * High-level relay kernel API and associated functions. 249 */ 250 251 static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf, 252 void *prev_subbuf) 253 { 254 int full = relay_buf_full(buf); 255 256 if (full) 257 buf->stats.full_count++; 258 259 if (!buf->chan->cb->subbuf_start) 260 return !full; 261 262 return buf->chan->cb->subbuf_start(buf, subbuf, 263 prev_subbuf); 264 } 265 266 /** 267 * wakeup_readers - wake up readers waiting on a channel 268 * @work: contains the channel buffer 269 * 270 * This is the function used to defer reader waking 271 */ 272 static void wakeup_readers(struct irq_work *work) 273 { 274 struct rchan_buf *buf; 275 276 buf = container_of(work, struct rchan_buf, wakeup_work); 277 wake_up_interruptible(&buf->read_wait); 278 } 279 280 /** 281 * __relay_reset - reset a channel buffer 282 * @buf: the channel buffer 283 * @init: 1 if this is a first-time initialization 284 * 285 * See relay_reset() for description of effect. 286 */ 287 static void __relay_reset(struct rchan_buf *buf, unsigned int init) 288 { 289 size_t i; 290 291 if (init) { 292 init_waitqueue_head(&buf->read_wait); 293 kref_init(&buf->kref); 294 init_irq_work(&buf->wakeup_work, wakeup_readers); 295 } else { 296 irq_work_sync(&buf->wakeup_work); 297 } 298 299 buf->subbufs_produced = 0; 300 buf->subbufs_consumed = 0; 301 buf->bytes_consumed = 0; 302 buf->finalized = 0; 303 buf->data = buf->start; 304 buf->offset = 0; 305 buf->stats.full_count = 0; 306 buf->stats.big_count = 0; 307 308 for (i = 0; i < buf->chan->n_subbufs; i++) 309 buf->padding[i] = 0; 310 311 relay_subbuf_start(buf, buf->data, NULL); 312 } 313 314 /** 315 * relay_reset - reset the channel 316 * @chan: the channel 317 * 318 * This has the effect of erasing all data from all channel buffers 319 * and restarting the channel in its initial state. The buffers 320 * are not freed, so any mappings are still in effect. 321 * 322 * NOTE. Care should be taken that the channel isn't actually 323 * being used by anything when this call is made. 324 */ 325 void relay_reset(struct rchan *chan) 326 { 327 struct rchan_buf *buf; 328 unsigned int i; 329 330 if (!chan) 331 return; 332 333 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { 334 __relay_reset(buf, 0); 335 return; 336 } 337 338 mutex_lock(&relay_channels_mutex); 339 for_each_possible_cpu(i) 340 if ((buf = *per_cpu_ptr(chan->buf, i))) 341 __relay_reset(buf, 0); 342 mutex_unlock(&relay_channels_mutex); 343 } 344 EXPORT_SYMBOL_GPL(relay_reset); 345 346 static inline void relay_set_buf_dentry(struct rchan_buf *buf, 347 struct dentry *dentry) 348 { 349 buf->dentry = dentry; 350 d_inode(buf->dentry)->i_size = buf->early_bytes; 351 } 352 353 static struct dentry *relay_create_buf_file(struct rchan *chan, 354 struct rchan_buf *buf, 355 unsigned int cpu) 356 { 357 struct dentry *dentry; 358 char *tmpname; 359 360 tmpname = kasprintf(GFP_KERNEL, "%s%d", chan->base_filename, cpu); 361 if (!tmpname) 362 return NULL; 363 364 /* Create file in fs */ 365 dentry = chan->cb->create_buf_file(tmpname, chan->parent, 366 S_IRUSR, buf, 367 &chan->is_global); 368 if (IS_ERR(dentry)) 369 dentry = NULL; 370 371 kfree(tmpname); 372 373 return dentry; 374 } 375 376 /* 377 * relay_open_buf - create a new relay channel buffer 378 * 379 * used by relay_open() and CPU hotplug. 380 */ 381 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) 382 { 383 struct rchan_buf *buf; 384 struct dentry *dentry; 385 386 if (chan->is_global) 387 return *per_cpu_ptr(chan->buf, 0); 388 389 buf = relay_create_buf(chan); 390 if (!buf) 391 return NULL; 392 393 if (chan->has_base_filename) { 394 dentry = relay_create_buf_file(chan, buf, cpu); 395 if (!dentry) 396 goto free_buf; 397 relay_set_buf_dentry(buf, dentry); 398 } else { 399 /* Only retrieve global info, nothing more, nothing less */ 400 dentry = chan->cb->create_buf_file(NULL, NULL, 401 S_IRUSR, buf, 402 &chan->is_global); 403 if (IS_ERR_OR_NULL(dentry)) 404 goto free_buf; 405 } 406 407 buf->cpu = cpu; 408 __relay_reset(buf, 1); 409 410 if(chan->is_global) { 411 *per_cpu_ptr(chan->buf, 0) = buf; 412 buf->cpu = 0; 413 } 414 415 return buf; 416 417 free_buf: 418 relay_destroy_buf(buf); 419 return NULL; 420 } 421 422 /** 423 * relay_close_buf - close a channel buffer 424 * @buf: channel buffer 425 * 426 * Marks the buffer finalized and restores the default callbacks. 427 * The channel buffer and channel buffer data structure are then freed 428 * automatically when the last reference is given up. 429 */ 430 static void relay_close_buf(struct rchan_buf *buf) 431 { 432 buf->finalized = 1; 433 irq_work_sync(&buf->wakeup_work); 434 buf->chan->cb->remove_buf_file(buf->dentry); 435 kref_put(&buf->kref, relay_remove_buf); 436 } 437 438 int relay_prepare_cpu(unsigned int cpu) 439 { 440 struct rchan *chan; 441 struct rchan_buf *buf; 442 443 mutex_lock(&relay_channels_mutex); 444 list_for_each_entry(chan, &relay_channels, list) { 445 if (*per_cpu_ptr(chan->buf, cpu)) 446 continue; 447 buf = relay_open_buf(chan, cpu); 448 if (!buf) { 449 pr_err("relay: cpu %d buffer creation failed\n", cpu); 450 mutex_unlock(&relay_channels_mutex); 451 return -ENOMEM; 452 } 453 *per_cpu_ptr(chan->buf, cpu) = buf; 454 } 455 mutex_unlock(&relay_channels_mutex); 456 return 0; 457 } 458 459 /** 460 * relay_open - create a new relay channel 461 * @base_filename: base name of files to create 462 * @parent: dentry of parent directory, %NULL for root directory or buffer 463 * @subbuf_size: size of sub-buffers 464 * @n_subbufs: number of sub-buffers 465 * @cb: client callback functions 466 * @private_data: user-defined data 467 * 468 * Returns channel pointer if successful, %NULL otherwise. 469 * 470 * Creates a channel buffer for each cpu using the sizes and 471 * attributes specified. The created channel buffer files 472 * will be named base_filename0...base_filenameN-1. File 473 * permissions will be %S_IRUSR. 474 */ 475 struct rchan *relay_open(const char *base_filename, 476 struct dentry *parent, 477 size_t subbuf_size, 478 size_t n_subbufs, 479 const struct rchan_callbacks *cb, 480 void *private_data) 481 { 482 unsigned int i; 483 struct rchan *chan; 484 struct rchan_buf *buf; 485 486 if (!(subbuf_size && n_subbufs)) 487 return NULL; 488 if (subbuf_size > UINT_MAX / n_subbufs) 489 return NULL; 490 if (!cb || !cb->create_buf_file || !cb->remove_buf_file) 491 return NULL; 492 493 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); 494 if (!chan) 495 return NULL; 496 497 chan->buf = alloc_percpu(struct rchan_buf *); 498 if (!chan->buf) { 499 kfree(chan); 500 return NULL; 501 } 502 503 chan->version = RELAYFS_CHANNEL_VERSION; 504 chan->n_subbufs = n_subbufs; 505 chan->subbuf_size = subbuf_size; 506 chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs); 507 chan->parent = parent; 508 chan->private_data = private_data; 509 if (base_filename) { 510 chan->has_base_filename = 1; 511 strscpy(chan->base_filename, base_filename, NAME_MAX); 512 } 513 chan->cb = cb; 514 kref_init(&chan->kref); 515 516 mutex_lock(&relay_channels_mutex); 517 for_each_online_cpu(i) { 518 buf = relay_open_buf(chan, i); 519 if (!buf) 520 goto free_bufs; 521 *per_cpu_ptr(chan->buf, i) = buf; 522 } 523 list_add(&chan->list, &relay_channels); 524 mutex_unlock(&relay_channels_mutex); 525 526 return chan; 527 528 free_bufs: 529 for_each_possible_cpu(i) { 530 if ((buf = *per_cpu_ptr(chan->buf, i))) 531 relay_close_buf(buf); 532 } 533 534 kref_put(&chan->kref, relay_destroy_channel); 535 mutex_unlock(&relay_channels_mutex); 536 return NULL; 537 } 538 EXPORT_SYMBOL_GPL(relay_open); 539 540 struct rchan_percpu_buf_dispatcher { 541 struct rchan_buf *buf; 542 struct dentry *dentry; 543 }; 544 545 /** 546 * relay_switch_subbuf - switch to a new sub-buffer 547 * @buf: channel buffer 548 * @length: size of current event 549 * 550 * Returns either the length passed in or 0 if full. 551 * 552 * Performs sub-buffer-switch tasks such as invoking callbacks, 553 * updating padding counts, waking up readers, etc. 554 */ 555 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) 556 { 557 void *old, *new; 558 size_t old_subbuf, new_subbuf; 559 560 if (unlikely(length > buf->chan->subbuf_size)) 561 goto toobig; 562 563 if (buf->offset != buf->chan->subbuf_size + 1) { 564 size_t prev_padding; 565 566 prev_padding = buf->chan->subbuf_size - buf->offset; 567 old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 568 buf->padding[old_subbuf] = prev_padding; 569 buf->subbufs_produced++; 570 if (buf->dentry) 571 d_inode(buf->dentry)->i_size += 572 buf->chan->subbuf_size - 573 buf->padding[old_subbuf]; 574 else 575 buf->early_bytes += buf->chan->subbuf_size - 576 buf->padding[old_subbuf]; 577 smp_mb(); 578 if (waitqueue_active(&buf->read_wait)) { 579 /* 580 * Calling wake_up_interruptible() from here 581 * will deadlock if we happen to be logging 582 * from the scheduler (trying to re-grab 583 * rq->lock), so defer it. 584 */ 585 irq_work_queue(&buf->wakeup_work); 586 } 587 } 588 589 old = buf->data; 590 new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; 591 new = buf->start + new_subbuf * buf->chan->subbuf_size; 592 buf->offset = 0; 593 if (!relay_subbuf_start(buf, new, old)) { 594 buf->offset = buf->chan->subbuf_size + 1; 595 return 0; 596 } 597 buf->data = new; 598 buf->padding[new_subbuf] = 0; 599 600 if (unlikely(length + buf->offset > buf->chan->subbuf_size)) 601 goto toobig; 602 603 return length; 604 605 toobig: 606 buf->stats.big_count++; 607 return 0; 608 } 609 EXPORT_SYMBOL_GPL(relay_switch_subbuf); 610 611 /** 612 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count 613 * @chan: the channel 614 * @cpu: the cpu associated with the channel buffer to update 615 * @subbufs_consumed: number of sub-buffers to add to current buf's count 616 * 617 * Adds to the channel buffer's consumed sub-buffer count. 618 * subbufs_consumed should be the number of sub-buffers newly consumed, 619 * not the total consumed. 620 * 621 * NOTE. Kernel clients don't need to call this function if the channel 622 * mode is 'overwrite'. 623 */ 624 void relay_subbufs_consumed(struct rchan *chan, 625 unsigned int cpu, 626 size_t subbufs_consumed) 627 { 628 struct rchan_buf *buf; 629 630 if (!chan || cpu >= NR_CPUS) 631 return; 632 633 buf = *per_cpu_ptr(chan->buf, cpu); 634 if (!buf || subbufs_consumed > chan->n_subbufs) 635 return; 636 637 if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed) 638 buf->subbufs_consumed = buf->subbufs_produced; 639 else 640 buf->subbufs_consumed += subbufs_consumed; 641 } 642 EXPORT_SYMBOL_GPL(relay_subbufs_consumed); 643 644 /** 645 * relay_close - close the channel 646 * @chan: the channel 647 * 648 * Closes all channel buffers and frees the channel. 649 */ 650 void relay_close(struct rchan *chan) 651 { 652 struct rchan_buf *buf; 653 unsigned int i; 654 655 if (!chan) 656 return; 657 658 mutex_lock(&relay_channels_mutex); 659 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) 660 relay_close_buf(buf); 661 else 662 for_each_possible_cpu(i) 663 if ((buf = *per_cpu_ptr(chan->buf, i))) 664 relay_close_buf(buf); 665 666 list_del(&chan->list); 667 kref_put(&chan->kref, relay_destroy_channel); 668 mutex_unlock(&relay_channels_mutex); 669 } 670 EXPORT_SYMBOL_GPL(relay_close); 671 672 /** 673 * relay_flush - close the channel 674 * @chan: the channel 675 * 676 * Flushes all channel buffers, i.e. forces buffer switch. 677 */ 678 void relay_flush(struct rchan *chan) 679 { 680 struct rchan_buf *buf; 681 unsigned int i; 682 683 if (!chan) 684 return; 685 686 if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { 687 relay_switch_subbuf(buf, 0); 688 return; 689 } 690 691 mutex_lock(&relay_channels_mutex); 692 for_each_possible_cpu(i) 693 if ((buf = *per_cpu_ptr(chan->buf, i))) 694 relay_switch_subbuf(buf, 0); 695 mutex_unlock(&relay_channels_mutex); 696 } 697 EXPORT_SYMBOL_GPL(relay_flush); 698 699 /** 700 * relay_stats - get channel buffer statistics 701 * @chan: the channel 702 * @flags: select particular information to get 703 * 704 * Returns the count of certain field that caller specifies. 705 */ 706 size_t relay_stats(struct rchan *chan, int flags) 707 { 708 unsigned int i, count = 0; 709 struct rchan_buf *rbuf; 710 711 if (!chan || flags > RELAY_STATS_LAST) 712 return 0; 713 714 if (chan->is_global) { 715 rbuf = *per_cpu_ptr(chan->buf, 0); 716 if (flags & RELAY_STATS_BUF_FULL) 717 count = rbuf->stats.full_count; 718 else if (flags & RELAY_STATS_WRT_BIG) 719 count = rbuf->stats.big_count; 720 } else { 721 for_each_online_cpu(i) { 722 rbuf = *per_cpu_ptr(chan->buf, i); 723 if (rbuf) { 724 if (flags & RELAY_STATS_BUF_FULL) 725 count += rbuf->stats.full_count; 726 else if (flags & RELAY_STATS_WRT_BIG) 727 count += rbuf->stats.big_count; 728 } 729 } 730 } 731 732 return count; 733 } 734 735 /** 736 * relay_file_open - open file op for relay files 737 * @inode: the inode 738 * @filp: the file 739 * 740 * Increments the channel buffer refcount. 741 */ 742 static int relay_file_open(struct inode *inode, struct file *filp) 743 { 744 struct rchan_buf *buf = inode->i_private; 745 kref_get(&buf->kref); 746 filp->private_data = buf; 747 748 return nonseekable_open(inode, filp); 749 } 750 751 /** 752 * relay_file_mmap - mmap file op for relay files 753 * @filp: the file 754 * @vma: the vma describing what to map 755 * 756 * Calls upon relay_mmap_buf() to map the file into user space. 757 */ 758 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) 759 { 760 struct rchan_buf *buf = filp->private_data; 761 return relay_mmap_buf(buf, vma); 762 } 763 764 /** 765 * relay_file_poll - poll file op for relay files 766 * @filp: the file 767 * @wait: poll table 768 * 769 * Poll implemention. 770 */ 771 static __poll_t relay_file_poll(struct file *filp, poll_table *wait) 772 { 773 __poll_t mask = 0; 774 struct rchan_buf *buf = filp->private_data; 775 776 if (buf->finalized) 777 return EPOLLERR; 778 779 if (filp->f_mode & FMODE_READ) { 780 poll_wait(filp, &buf->read_wait, wait); 781 if (!relay_buf_empty(buf)) 782 mask |= EPOLLIN | EPOLLRDNORM; 783 } 784 785 return mask; 786 } 787 788 /** 789 * relay_file_release - release file op for relay files 790 * @inode: the inode 791 * @filp: the file 792 * 793 * Decrements the channel refcount, as the filesystem is 794 * no longer using it. 795 */ 796 static int relay_file_release(struct inode *inode, struct file *filp) 797 { 798 struct rchan_buf *buf = filp->private_data; 799 kref_put(&buf->kref, relay_remove_buf); 800 801 return 0; 802 } 803 804 /* 805 * relay_file_read_consume - update the consumed count for the buffer 806 */ 807 static void relay_file_read_consume(struct rchan_buf *buf, 808 size_t read_pos, 809 size_t bytes_consumed) 810 { 811 size_t subbuf_size = buf->chan->subbuf_size; 812 size_t n_subbufs = buf->chan->n_subbufs; 813 size_t read_subbuf; 814 815 if (buf->subbufs_produced == buf->subbufs_consumed && 816 buf->offset == buf->bytes_consumed) 817 return; 818 819 if (buf->bytes_consumed + bytes_consumed > subbuf_size) { 820 relay_subbufs_consumed(buf->chan, buf->cpu, 1); 821 buf->bytes_consumed = 0; 822 } 823 824 buf->bytes_consumed += bytes_consumed; 825 if (!read_pos) 826 read_subbuf = buf->subbufs_consumed % n_subbufs; 827 else 828 read_subbuf = read_pos / buf->chan->subbuf_size; 829 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { 830 if ((read_subbuf == buf->subbufs_produced % n_subbufs) && 831 (buf->offset == subbuf_size)) 832 return; 833 relay_subbufs_consumed(buf->chan, buf->cpu, 1); 834 buf->bytes_consumed = 0; 835 } 836 } 837 838 /* 839 * relay_file_read_avail - boolean, are there unconsumed bytes available? 840 */ 841 static int relay_file_read_avail(struct rchan_buf *buf) 842 { 843 size_t subbuf_size = buf->chan->subbuf_size; 844 size_t n_subbufs = buf->chan->n_subbufs; 845 size_t produced = buf->subbufs_produced; 846 size_t consumed; 847 848 relay_file_read_consume(buf, 0, 0); 849 850 consumed = buf->subbufs_consumed; 851 852 if (unlikely(buf->offset > subbuf_size)) { 853 if (produced == consumed) 854 return 0; 855 return 1; 856 } 857 858 if (unlikely(produced - consumed >= n_subbufs)) { 859 consumed = produced - n_subbufs + 1; 860 buf->subbufs_consumed = consumed; 861 buf->bytes_consumed = 0; 862 } 863 864 produced = (produced % n_subbufs) * subbuf_size + buf->offset; 865 consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; 866 867 if (consumed > produced) 868 produced += n_subbufs * subbuf_size; 869 870 if (consumed == produced) { 871 if (buf->offset == subbuf_size && 872 buf->subbufs_produced > buf->subbufs_consumed) 873 return 1; 874 return 0; 875 } 876 877 return 1; 878 } 879 880 /** 881 * relay_file_read_subbuf_avail - return bytes available in sub-buffer 882 * @read_pos: file read position 883 * @buf: relay channel buffer 884 */ 885 static size_t relay_file_read_subbuf_avail(size_t read_pos, 886 struct rchan_buf *buf) 887 { 888 size_t padding, avail = 0; 889 size_t read_subbuf, read_offset, write_subbuf, write_offset; 890 size_t subbuf_size = buf->chan->subbuf_size; 891 892 write_subbuf = (buf->data - buf->start) / subbuf_size; 893 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; 894 read_subbuf = read_pos / subbuf_size; 895 read_offset = read_pos % subbuf_size; 896 padding = buf->padding[read_subbuf]; 897 898 if (read_subbuf == write_subbuf) { 899 if (read_offset + padding < write_offset) 900 avail = write_offset - (read_offset + padding); 901 } else 902 avail = (subbuf_size - padding) - read_offset; 903 904 return avail; 905 } 906 907 /** 908 * relay_file_read_start_pos - find the first available byte to read 909 * @buf: relay channel buffer 910 * 911 * If the read_pos is in the middle of padding, return the 912 * position of the first actually available byte, otherwise 913 * return the original value. 914 */ 915 static size_t relay_file_read_start_pos(struct rchan_buf *buf) 916 { 917 size_t read_subbuf, padding, padding_start, padding_end; 918 size_t subbuf_size = buf->chan->subbuf_size; 919 size_t n_subbufs = buf->chan->n_subbufs; 920 size_t consumed = buf->subbufs_consumed % n_subbufs; 921 size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed) 922 % (n_subbufs * subbuf_size); 923 924 read_subbuf = read_pos / subbuf_size; 925 padding = buf->padding[read_subbuf]; 926 padding_start = (read_subbuf + 1) * subbuf_size - padding; 927 padding_end = (read_subbuf + 1) * subbuf_size; 928 if (read_pos >= padding_start && read_pos < padding_end) { 929 read_subbuf = (read_subbuf + 1) % n_subbufs; 930 read_pos = read_subbuf * subbuf_size; 931 } 932 933 return read_pos; 934 } 935 936 /** 937 * relay_file_read_end_pos - return the new read position 938 * @read_pos: file read position 939 * @buf: relay channel buffer 940 * @count: number of bytes to be read 941 */ 942 static size_t relay_file_read_end_pos(struct rchan_buf *buf, 943 size_t read_pos, 944 size_t count) 945 { 946 size_t read_subbuf, padding, end_pos; 947 size_t subbuf_size = buf->chan->subbuf_size; 948 size_t n_subbufs = buf->chan->n_subbufs; 949 950 read_subbuf = read_pos / subbuf_size; 951 padding = buf->padding[read_subbuf]; 952 if (read_pos % subbuf_size + count + padding == subbuf_size) 953 end_pos = (read_subbuf + 1) * subbuf_size; 954 else 955 end_pos = read_pos + count; 956 if (end_pos >= subbuf_size * n_subbufs) 957 end_pos = 0; 958 959 return end_pos; 960 } 961 962 static ssize_t relay_file_read(struct file *filp, 963 char __user *buffer, 964 size_t count, 965 loff_t *ppos) 966 { 967 struct rchan_buf *buf = filp->private_data; 968 size_t read_start, avail; 969 size_t written = 0; 970 int ret; 971 972 if (!count) 973 return 0; 974 975 inode_lock(file_inode(filp)); 976 do { 977 void *from; 978 979 if (!relay_file_read_avail(buf)) 980 break; 981 982 read_start = relay_file_read_start_pos(buf); 983 avail = relay_file_read_subbuf_avail(read_start, buf); 984 if (!avail) 985 break; 986 987 avail = min(count, avail); 988 from = buf->start + read_start; 989 ret = avail; 990 if (copy_to_user(buffer, from, avail)) 991 break; 992 993 buffer += ret; 994 written += ret; 995 count -= ret; 996 997 relay_file_read_consume(buf, read_start, ret); 998 *ppos = relay_file_read_end_pos(buf, read_start, ret); 999 } while (count); 1000 inode_unlock(file_inode(filp)); 1001 1002 return written; 1003 } 1004 1005 1006 const struct file_operations relay_file_operations = { 1007 .open = relay_file_open, 1008 .poll = relay_file_poll, 1009 .mmap = relay_file_mmap, 1010 .read = relay_file_read, 1011 .release = relay_file_release, 1012 }; 1013 EXPORT_SYMBOL_GPL(relay_file_operations); 1014