2 * Public API and common code for kernel->userspace relay file support.
4 * See Documentation/filesystems/relay.txt for an overview.
6 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
9 * Moved to kernel/relay.c by Paul Mundt, 2006.
10 * November 2006 - CPU hotplug support by Mathieu Desnoyers
11 * (mathieu.desnoyers@polymtl.ca)
13 * This file is released under the GPL.
15 #include <linux/errno.h>
16 #include <linux/stddef.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <linux/relay.h>
21 #include <linux/vmalloc.h>
23 #include <linux/cpu.h>
24 #include <linux/splice.h>
26 /* list of open channels, for cpu hotplug */
27 static DEFINE_MUTEX(relay_channels_mutex
);
28 static LIST_HEAD(relay_channels
);
31 * close() vm_op implementation for relay file mapping.
33 static void relay_file_mmap_close(struct vm_area_struct
*vma
)
35 struct rchan_buf
*buf
= vma
->vm_private_data
;
36 buf
->chan
->cb
->buf_unmapped(buf
, vma
->vm_file
);
40 * nopage() vm_op implementation for relay file mapping.
42 static struct page
*relay_buf_nopage(struct vm_area_struct
*vma
,
43 unsigned long address
,
47 struct rchan_buf
*buf
= vma
->vm_private_data
;
48 unsigned long offset
= address
- vma
->vm_start
;
50 if (address
> vma
->vm_end
)
51 return NOPAGE_SIGBUS
; /* Disallow mremap */
55 page
= vmalloc_to_page(buf
->start
+ offset
);
61 *type
= VM_FAULT_MINOR
;
67 * vm_ops for relay file mappings.
69 static struct vm_operations_struct relay_file_mmap_ops
= {
70 .nopage
= relay_buf_nopage
,
71 .close
= relay_file_mmap_close
,
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
79 * Returns 0 if ok, negative on error
81 * Caller should already have grabbed mmap_sem.
83 static int relay_mmap_buf(struct rchan_buf
*buf
, struct vm_area_struct
*vma
)
85 unsigned long length
= vma
->vm_end
- vma
->vm_start
;
86 struct file
*filp
= vma
->vm_file
;
91 if (length
!= (unsigned long)buf
->chan
->alloc_size
)
94 vma
->vm_ops
= &relay_file_mmap_ops
;
95 vma
->vm_flags
|= VM_DONTEXPAND
;
96 vma
->vm_private_data
= buf
;
97 buf
->chan
->cb
->buf_mapped(buf
, filp
);
103 * relay_alloc_buf - allocate a channel buffer
104 * @buf: the buffer struct
105 * @size: total size of the buffer
107 * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
108 * passed in size will get page aligned, if it isn't already.
110 static void *relay_alloc_buf(struct rchan_buf
*buf
, size_t *size
)
113 unsigned int i
, j
, n_pages
;
115 *size
= PAGE_ALIGN(*size
);
116 n_pages
= *size
>> PAGE_SHIFT
;
118 buf
->page_array
= kcalloc(n_pages
, sizeof(struct page
*), GFP_KERNEL
);
119 if (!buf
->page_array
)
122 for (i
= 0; i
< n_pages
; i
++) {
123 buf
->page_array
[i
] = alloc_page(GFP_KERNEL
);
124 if (unlikely(!buf
->page_array
[i
]))
126 set_page_private(buf
->page_array
[i
], (unsigned long)buf
);
128 mem
= vmap(buf
->page_array
, n_pages
, VM_MAP
, PAGE_KERNEL
);
132 memset(mem
, 0, *size
);
133 buf
->page_count
= n_pages
;
137 for (j
= 0; j
< i
; j
++)
138 __free_page(buf
->page_array
[j
]);
139 kfree(buf
->page_array
);
144 * relay_create_buf - allocate and initialize a channel buffer
145 * @chan: the relay channel
147 * Returns channel buffer if successful, %NULL otherwise.
149 static struct rchan_buf
*relay_create_buf(struct rchan
*chan
)
151 struct rchan_buf
*buf
= kzalloc(sizeof(struct rchan_buf
), GFP_KERNEL
);
155 buf
->padding
= kmalloc(chan
->n_subbufs
* sizeof(size_t *), GFP_KERNEL
);
159 buf
->start
= relay_alloc_buf(buf
, &chan
->alloc_size
);
164 kref_get(&buf
->chan
->kref
);
174 * relay_destroy_channel - free the channel struct
175 * @kref: target kernel reference that contains the relay channel
177 * Should only be called from kref_put().
179 static void relay_destroy_channel(struct kref
*kref
)
181 struct rchan
*chan
= container_of(kref
, struct rchan
, kref
);
186 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
187 * @buf: the buffer struct
189 static void relay_destroy_buf(struct rchan_buf
*buf
)
191 struct rchan
*chan
= buf
->chan
;
194 if (likely(buf
->start
)) {
196 for (i
= 0; i
< buf
->page_count
; i
++)
197 __free_page(buf
->page_array
[i
]);
198 kfree(buf
->page_array
);
200 chan
->buf
[buf
->cpu
] = NULL
;
203 kref_put(&chan
->kref
, relay_destroy_channel
);
207 * relay_remove_buf - remove a channel buffer
208 * @kref: target kernel reference that contains the relay buffer
210 * Removes the file from the fileystem, which also frees the
211 * rchan_buf_struct and the channel buffer. Should only be called from
214 static void relay_remove_buf(struct kref
*kref
)
216 struct rchan_buf
*buf
= container_of(kref
, struct rchan_buf
, kref
);
217 buf
->chan
->cb
->remove_buf_file(buf
->dentry
);
218 relay_destroy_buf(buf
);
222 * relay_buf_empty - boolean, is the channel buffer empty?
223 * @buf: channel buffer
225 * Returns 1 if the buffer is empty, 0 otherwise.
227 static int relay_buf_empty(struct rchan_buf
*buf
)
229 return (buf
->subbufs_produced
- buf
->subbufs_consumed
) ? 0 : 1;
233 * relay_buf_full - boolean, is the channel buffer full?
234 * @buf: channel buffer
236 * Returns 1 if the buffer is full, 0 otherwise.
238 int relay_buf_full(struct rchan_buf
*buf
)
240 size_t ready
= buf
->subbufs_produced
- buf
->subbufs_consumed
;
241 return (ready
>= buf
->chan
->n_subbufs
) ? 1 : 0;
243 EXPORT_SYMBOL_GPL(relay_buf_full
);
246 * High-level relay kernel API and associated functions.
250 * rchan_callback implementations defining default channel behavior. Used
251 * in place of corresponding NULL values in client callback struct.
255 * subbuf_start() default callback. Does nothing.
257 static int subbuf_start_default_callback (struct rchan_buf
*buf
,
262 if (relay_buf_full(buf
))
269 * buf_mapped() default callback. Does nothing.
271 static void buf_mapped_default_callback(struct rchan_buf
*buf
,
277 * buf_unmapped() default callback. Does nothing.
279 static void buf_unmapped_default_callback(struct rchan_buf
*buf
,
285 * create_buf_file_create() default callback. Does nothing.
287 static struct dentry
*create_buf_file_default_callback(const char *filename
,
288 struct dentry
*parent
,
290 struct rchan_buf
*buf
,
297 * remove_buf_file() default callback. Does nothing.
299 static int remove_buf_file_default_callback(struct dentry
*dentry
)
304 /* relay channel default callbacks */
305 static struct rchan_callbacks default_channel_callbacks
= {
306 .subbuf_start
= subbuf_start_default_callback
,
307 .buf_mapped
= buf_mapped_default_callback
,
308 .buf_unmapped
= buf_unmapped_default_callback
,
309 .create_buf_file
= create_buf_file_default_callback
,
310 .remove_buf_file
= remove_buf_file_default_callback
,
314 * wakeup_readers - wake up readers waiting on a channel
315 * @data: contains the channel buffer
317 * This is the timer function used to defer reader waking.
319 static void wakeup_readers(unsigned long data
)
321 struct rchan_buf
*buf
= (struct rchan_buf
*)data
;
322 wake_up_interruptible(&buf
->read_wait
);
326 * __relay_reset - reset a channel buffer
327 * @buf: the channel buffer
328 * @init: 1 if this is a first-time initialization
330 * See relay_reset() for description of effect.
332 static void __relay_reset(struct rchan_buf
*buf
, unsigned int init
)
337 init_waitqueue_head(&buf
->read_wait
);
338 kref_init(&buf
->kref
);
339 setup_timer(&buf
->timer
, wakeup_readers
, (unsigned long)buf
);
341 del_timer_sync(&buf
->timer
);
343 buf
->subbufs_produced
= 0;
344 buf
->subbufs_consumed
= 0;
345 buf
->bytes_consumed
= 0;
347 buf
->data
= buf
->start
;
350 for (i
= 0; i
< buf
->chan
->n_subbufs
; i
++)
353 buf
->chan
->cb
->subbuf_start(buf
, buf
->data
, NULL
, 0);
357 * relay_reset - reset the channel
360 * This has the effect of erasing all data from all channel buffers
361 * and restarting the channel in its initial state. The buffers
362 * are not freed, so any mappings are still in effect.
364 * NOTE. Care should be taken that the channel isn't actually
365 * being used by anything when this call is made.
367 void relay_reset(struct rchan
*chan
)
374 if (chan
->is_global
&& chan
->buf
[0]) {
375 __relay_reset(chan
->buf
[0], 0);
379 mutex_lock(&relay_channels_mutex
);
380 for_each_online_cpu(i
)
382 __relay_reset(chan
->buf
[i
], 0);
383 mutex_unlock(&relay_channels_mutex
);
385 EXPORT_SYMBOL_GPL(relay_reset
);
388 * relay_open_buf - create a new relay channel buffer
390 * used by relay_open() and CPU hotplug.
392 static struct rchan_buf
*relay_open_buf(struct rchan
*chan
, unsigned int cpu
)
394 struct rchan_buf
*buf
= NULL
;
395 struct dentry
*dentry
;
401 tmpname
= kzalloc(NAME_MAX
+ 1, GFP_KERNEL
);
404 snprintf(tmpname
, NAME_MAX
, "%s%d", chan
->base_filename
, cpu
);
406 buf
= relay_create_buf(chan
);
411 __relay_reset(buf
, 1);
413 /* Create file in fs */
414 dentry
= chan
->cb
->create_buf_file(tmpname
, chan
->parent
, S_IRUSR
,
415 buf
, &chan
->is_global
);
419 buf
->dentry
= dentry
;
421 if(chan
->is_global
) {
429 relay_destroy_buf(buf
);
438 * relay_close_buf - close a channel buffer
439 * @buf: channel buffer
441 * Marks the buffer finalized and restores the default callbacks.
442 * The channel buffer and channel buffer data structure are then freed
443 * automatically when the last reference is given up.
445 static void relay_close_buf(struct rchan_buf
*buf
)
448 del_timer_sync(&buf
->timer
);
449 kref_put(&buf
->kref
, relay_remove_buf
);
452 static void setup_callbacks(struct rchan
*chan
,
453 struct rchan_callbacks
*cb
)
456 chan
->cb
= &default_channel_callbacks
;
460 if (!cb
->subbuf_start
)
461 cb
->subbuf_start
= subbuf_start_default_callback
;
463 cb
->buf_mapped
= buf_mapped_default_callback
;
464 if (!cb
->buf_unmapped
)
465 cb
->buf_unmapped
= buf_unmapped_default_callback
;
466 if (!cb
->create_buf_file
)
467 cb
->create_buf_file
= create_buf_file_default_callback
;
468 if (!cb
->remove_buf_file
)
469 cb
->remove_buf_file
= remove_buf_file_default_callback
;
474 * relay_hotcpu_callback - CPU hotplug callback
475 * @nb: notifier block
476 * @action: hotplug action to take
479 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
481 static int __cpuinit
relay_hotcpu_callback(struct notifier_block
*nb
,
482 unsigned long action
,
485 unsigned int hotcpu
= (unsigned long)hcpu
;
490 case CPU_UP_PREPARE_FROZEN
:
491 mutex_lock(&relay_channels_mutex
);
492 list_for_each_entry(chan
, &relay_channels
, list
) {
493 if (chan
->buf
[hotcpu
])
495 chan
->buf
[hotcpu
] = relay_open_buf(chan
, hotcpu
);
496 if(!chan
->buf
[hotcpu
]) {
498 "relay_hotcpu_callback: cpu %d buffer "
499 "creation failed\n", hotcpu
);
500 mutex_unlock(&relay_channels_mutex
);
504 mutex_unlock(&relay_channels_mutex
);
507 case CPU_DEAD_FROZEN
:
508 /* No need to flush the cpu : will be flushed upon
509 * final relay_flush() call. */
516 * relay_open - create a new relay channel
517 * @base_filename: base name of files to create
518 * @parent: dentry of parent directory, %NULL for root directory
519 * @subbuf_size: size of sub-buffers
520 * @n_subbufs: number of sub-buffers
521 * @cb: client callback functions
522 * @private_data: user-defined data
524 * Returns channel pointer if successful, %NULL otherwise.
526 * Creates a channel buffer for each cpu using the sizes and
527 * attributes specified. The created channel buffer files
528 * will be named base_filename0...base_filenameN-1. File
529 * permissions will be %S_IRUSR.
531 struct rchan
*relay_open(const char *base_filename
,
532 struct dentry
*parent
,
535 struct rchan_callbacks
*cb
,
543 if (!(subbuf_size
&& n_subbufs
))
546 chan
= kzalloc(sizeof(struct rchan
), GFP_KERNEL
);
550 chan
->version
= RELAYFS_CHANNEL_VERSION
;
551 chan
->n_subbufs
= n_subbufs
;
552 chan
->subbuf_size
= subbuf_size
;
553 chan
->alloc_size
= FIX_SIZE(subbuf_size
* n_subbufs
);
554 chan
->parent
= parent
;
555 chan
->private_data
= private_data
;
556 strlcpy(chan
->base_filename
, base_filename
, NAME_MAX
);
557 setup_callbacks(chan
, cb
);
558 kref_init(&chan
->kref
);
560 mutex_lock(&relay_channels_mutex
);
561 for_each_online_cpu(i
) {
562 chan
->buf
[i
] = relay_open_buf(chan
, i
);
566 list_add(&chan
->list
, &relay_channels
);
567 mutex_unlock(&relay_channels_mutex
);
572 for_each_online_cpu(i
) {
575 relay_close_buf(chan
->buf
[i
]);
578 kref_put(&chan
->kref
, relay_destroy_channel
);
579 mutex_unlock(&relay_channels_mutex
);
582 EXPORT_SYMBOL_GPL(relay_open
);
585 * relay_switch_subbuf - switch to a new sub-buffer
586 * @buf: channel buffer
587 * @length: size of current event
589 * Returns either the length passed in or 0 if full.
591 * Performs sub-buffer-switch tasks such as invoking callbacks,
592 * updating padding counts, waking up readers, etc.
594 size_t relay_switch_subbuf(struct rchan_buf
*buf
, size_t length
)
597 size_t old_subbuf
, new_subbuf
;
599 if (unlikely(length
> buf
->chan
->subbuf_size
))
602 if (buf
->offset
!= buf
->chan
->subbuf_size
+ 1) {
603 buf
->prev_padding
= buf
->chan
->subbuf_size
- buf
->offset
;
604 old_subbuf
= buf
->subbufs_produced
% buf
->chan
->n_subbufs
;
605 buf
->padding
[old_subbuf
] = buf
->prev_padding
;
606 buf
->subbufs_produced
++;
607 buf
->dentry
->d_inode
->i_size
+= buf
->chan
->subbuf_size
-
608 buf
->padding
[old_subbuf
];
610 if (waitqueue_active(&buf
->read_wait
))
612 * Calling wake_up_interruptible() from here
613 * will deadlock if we happen to be logging
614 * from the scheduler (trying to re-grab
615 * rq->lock), so defer it.
617 __mod_timer(&buf
->timer
, jiffies
+ 1);
621 new_subbuf
= buf
->subbufs_produced
% buf
->chan
->n_subbufs
;
622 new = buf
->start
+ new_subbuf
* buf
->chan
->subbuf_size
;
624 if (!buf
->chan
->cb
->subbuf_start(buf
, new, old
, buf
->prev_padding
)) {
625 buf
->offset
= buf
->chan
->subbuf_size
+ 1;
629 buf
->padding
[new_subbuf
] = 0;
631 if (unlikely(length
+ buf
->offset
> buf
->chan
->subbuf_size
))
637 buf
->chan
->last_toobig
= length
;
640 EXPORT_SYMBOL_GPL(relay_switch_subbuf
);
643 * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
645 * @cpu: the cpu associated with the channel buffer to update
646 * @subbufs_consumed: number of sub-buffers to add to current buf's count
648 * Adds to the channel buffer's consumed sub-buffer count.
649 * subbufs_consumed should be the number of sub-buffers newly consumed,
650 * not the total consumed.
652 * NOTE. Kernel clients don't need to call this function if the channel
653 * mode is 'overwrite'.
655 void relay_subbufs_consumed(struct rchan
*chan
,
657 size_t subbufs_consumed
)
659 struct rchan_buf
*buf
;
664 if (cpu
>= NR_CPUS
|| !chan
->buf
[cpu
])
667 buf
= chan
->buf
[cpu
];
668 buf
->subbufs_consumed
+= subbufs_consumed
;
669 if (buf
->subbufs_consumed
> buf
->subbufs_produced
)
670 buf
->subbufs_consumed
= buf
->subbufs_produced
;
672 EXPORT_SYMBOL_GPL(relay_subbufs_consumed
);
675 * relay_close - close the channel
678 * Closes all channel buffers and frees the channel.
680 void relay_close(struct rchan
*chan
)
687 mutex_lock(&relay_channels_mutex
);
688 if (chan
->is_global
&& chan
->buf
[0])
689 relay_close_buf(chan
->buf
[0]);
691 for_each_possible_cpu(i
)
693 relay_close_buf(chan
->buf
[i
]);
695 if (chan
->last_toobig
)
696 printk(KERN_WARNING
"relay: one or more items not logged "
697 "[item size (%Zd) > sub-buffer size (%Zd)]\n",
698 chan
->last_toobig
, chan
->subbuf_size
);
700 list_del(&chan
->list
);
701 kref_put(&chan
->kref
, relay_destroy_channel
);
702 mutex_unlock(&relay_channels_mutex
);
704 EXPORT_SYMBOL_GPL(relay_close
);
707 * relay_flush - close the channel
710 * Flushes all channel buffers, i.e. forces buffer switch.
712 void relay_flush(struct rchan
*chan
)
719 if (chan
->is_global
&& chan
->buf
[0]) {
720 relay_switch_subbuf(chan
->buf
[0], 0);
724 mutex_lock(&relay_channels_mutex
);
725 for_each_possible_cpu(i
)
727 relay_switch_subbuf(chan
->buf
[i
], 0);
728 mutex_unlock(&relay_channels_mutex
);
730 EXPORT_SYMBOL_GPL(relay_flush
);
733 * relay_file_open - open file op for relay files
737 * Increments the channel buffer refcount.
739 static int relay_file_open(struct inode
*inode
, struct file
*filp
)
741 struct rchan_buf
*buf
= inode
->i_private
;
742 kref_get(&buf
->kref
);
743 filp
->private_data
= buf
;
749 * relay_file_mmap - mmap file op for relay files
751 * @vma: the vma describing what to map
753 * Calls upon relay_mmap_buf() to map the file into user space.
755 static int relay_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
757 struct rchan_buf
*buf
= filp
->private_data
;
758 return relay_mmap_buf(buf
, vma
);
762 * relay_file_poll - poll file op for relay files
768 static unsigned int relay_file_poll(struct file
*filp
, poll_table
*wait
)
770 unsigned int mask
= 0;
771 struct rchan_buf
*buf
= filp
->private_data
;
776 if (filp
->f_mode
& FMODE_READ
) {
777 poll_wait(filp
, &buf
->read_wait
, wait
);
778 if (!relay_buf_empty(buf
))
779 mask
|= POLLIN
| POLLRDNORM
;
786 * relay_file_release - release file op for relay files
790 * Decrements the channel refcount, as the filesystem is
791 * no longer using it.
793 static int relay_file_release(struct inode
*inode
, struct file
*filp
)
795 struct rchan_buf
*buf
= filp
->private_data
;
796 kref_put(&buf
->kref
, relay_remove_buf
);
802 * relay_file_read_consume - update the consumed count for the buffer
804 static void relay_file_read_consume(struct rchan_buf
*buf
,
806 size_t bytes_consumed
)
808 size_t subbuf_size
= buf
->chan
->subbuf_size
;
809 size_t n_subbufs
= buf
->chan
->n_subbufs
;
812 if (buf
->bytes_consumed
+ bytes_consumed
> subbuf_size
) {
813 relay_subbufs_consumed(buf
->chan
, buf
->cpu
, 1);
814 buf
->bytes_consumed
= 0;
817 buf
->bytes_consumed
+= bytes_consumed
;
819 read_subbuf
= buf
->subbufs_consumed
% n_subbufs
;
821 read_subbuf
= read_pos
/ buf
->chan
->subbuf_size
;
822 if (buf
->bytes_consumed
+ buf
->padding
[read_subbuf
] == subbuf_size
) {
823 if ((read_subbuf
== buf
->subbufs_produced
% n_subbufs
) &&
824 (buf
->offset
== subbuf_size
))
826 relay_subbufs_consumed(buf
->chan
, buf
->cpu
, 1);
827 buf
->bytes_consumed
= 0;
832 * relay_file_read_avail - boolean, are there unconsumed bytes available?
834 static int relay_file_read_avail(struct rchan_buf
*buf
, size_t read_pos
)
836 size_t subbuf_size
= buf
->chan
->subbuf_size
;
837 size_t n_subbufs
= buf
->chan
->n_subbufs
;
838 size_t produced
= buf
->subbufs_produced
;
839 size_t consumed
= buf
->subbufs_consumed
;
841 relay_file_read_consume(buf
, read_pos
, 0);
843 if (unlikely(buf
->offset
> subbuf_size
)) {
844 if (produced
== consumed
)
849 if (unlikely(produced
- consumed
>= n_subbufs
)) {
850 consumed
= produced
- n_subbufs
+ 1;
851 buf
->subbufs_consumed
= consumed
;
852 buf
->bytes_consumed
= 0;
855 produced
= (produced
% n_subbufs
) * subbuf_size
+ buf
->offset
;
856 consumed
= (consumed
% n_subbufs
) * subbuf_size
+ buf
->bytes_consumed
;
858 if (consumed
> produced
)
859 produced
+= n_subbufs
* subbuf_size
;
861 if (consumed
== produced
)
868 * relay_file_read_subbuf_avail - return bytes available in sub-buffer
869 * @read_pos: file read position
870 * @buf: relay channel buffer
872 static size_t relay_file_read_subbuf_avail(size_t read_pos
,
873 struct rchan_buf
*buf
)
875 size_t padding
, avail
= 0;
876 size_t read_subbuf
, read_offset
, write_subbuf
, write_offset
;
877 size_t subbuf_size
= buf
->chan
->subbuf_size
;
879 write_subbuf
= (buf
->data
- buf
->start
) / subbuf_size
;
880 write_offset
= buf
->offset
> subbuf_size
? subbuf_size
: buf
->offset
;
881 read_subbuf
= read_pos
/ subbuf_size
;
882 read_offset
= read_pos
% subbuf_size
;
883 padding
= buf
->padding
[read_subbuf
];
885 if (read_subbuf
== write_subbuf
) {
886 if (read_offset
+ padding
< write_offset
)
887 avail
= write_offset
- (read_offset
+ padding
);
889 avail
= (subbuf_size
- padding
) - read_offset
;
895 * relay_file_read_start_pos - find the first available byte to read
896 * @read_pos: file read position
897 * @buf: relay channel buffer
899 * If the @read_pos is in the middle of padding, return the
900 * position of the first actually available byte, otherwise
901 * return the original value.
903 static size_t relay_file_read_start_pos(size_t read_pos
,
904 struct rchan_buf
*buf
)
906 size_t read_subbuf
, padding
, padding_start
, padding_end
;
907 size_t subbuf_size
= buf
->chan
->subbuf_size
;
908 size_t n_subbufs
= buf
->chan
->n_subbufs
;
909 size_t consumed
= buf
->subbufs_consumed
% n_subbufs
;
912 read_pos
= consumed
* subbuf_size
+ buf
->bytes_consumed
;
913 read_subbuf
= read_pos
/ subbuf_size
;
914 padding
= buf
->padding
[read_subbuf
];
915 padding_start
= (read_subbuf
+ 1) * subbuf_size
- padding
;
916 padding_end
= (read_subbuf
+ 1) * subbuf_size
;
917 if (read_pos
>= padding_start
&& read_pos
< padding_end
) {
918 read_subbuf
= (read_subbuf
+ 1) % n_subbufs
;
919 read_pos
= read_subbuf
* subbuf_size
;
926 * relay_file_read_end_pos - return the new read position
927 * @read_pos: file read position
928 * @buf: relay channel buffer
929 * @count: number of bytes to be read
931 static size_t relay_file_read_end_pos(struct rchan_buf
*buf
,
935 size_t read_subbuf
, padding
, end_pos
;
936 size_t subbuf_size
= buf
->chan
->subbuf_size
;
937 size_t n_subbufs
= buf
->chan
->n_subbufs
;
939 read_subbuf
= read_pos
/ subbuf_size
;
940 padding
= buf
->padding
[read_subbuf
];
941 if (read_pos
% subbuf_size
+ count
+ padding
== subbuf_size
)
942 end_pos
= (read_subbuf
+ 1) * subbuf_size
;
944 end_pos
= read_pos
+ count
;
945 if (end_pos
>= subbuf_size
* n_subbufs
)
952 * subbuf_read_actor - read up to one subbuf's worth of data
954 static int subbuf_read_actor(size_t read_start
,
955 struct rchan_buf
*buf
,
957 read_descriptor_t
*desc
,
963 from
= buf
->start
+ read_start
;
965 if (copy_to_user(desc
->arg
.buf
, from
, avail
)) {
966 desc
->error
= -EFAULT
;
969 desc
->arg
.data
+= ret
;
970 desc
->written
+= ret
;
976 typedef int (*subbuf_actor_t
) (size_t read_start
,
977 struct rchan_buf
*buf
,
979 read_descriptor_t
*desc
,
983 * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
985 static ssize_t
relay_file_read_subbufs(struct file
*filp
, loff_t
*ppos
,
986 subbuf_actor_t subbuf_actor
,
988 read_descriptor_t
*desc
)
990 struct rchan_buf
*buf
= filp
->private_data
;
991 size_t read_start
, avail
;
997 mutex_lock(&filp
->f_path
.dentry
->d_inode
->i_mutex
);
999 if (!relay_file_read_avail(buf
, *ppos
))
1002 read_start
= relay_file_read_start_pos(*ppos
, buf
);
1003 avail
= relay_file_read_subbuf_avail(read_start
, buf
);
1007 avail
= min(desc
->count
, avail
);
1008 ret
= subbuf_actor(read_start
, buf
, avail
, desc
, actor
);
1009 if (desc
->error
< 0)
1013 relay_file_read_consume(buf
, read_start
, ret
);
1014 *ppos
= relay_file_read_end_pos(buf
, read_start
, ret
);
1016 } while (desc
->count
&& ret
);
1017 mutex_unlock(&filp
->f_path
.dentry
->d_inode
->i_mutex
);
1019 return desc
->written
;
1022 static ssize_t
relay_file_read(struct file
*filp
,
1023 char __user
*buffer
,
1027 read_descriptor_t desc
;
1030 desc
.arg
.buf
= buffer
;
1032 return relay_file_read_subbufs(filp
, ppos
, subbuf_read_actor
,
1036 static void relay_consume_bytes(struct rchan_buf
*rbuf
, int bytes_consumed
)
1038 rbuf
->bytes_consumed
+= bytes_consumed
;
1040 if (rbuf
->bytes_consumed
>= rbuf
->chan
->subbuf_size
) {
1041 relay_subbufs_consumed(rbuf
->chan
, rbuf
->cpu
, 1);
1042 rbuf
->bytes_consumed
%= rbuf
->chan
->subbuf_size
;
1046 static void relay_pipe_buf_release(struct pipe_inode_info
*pipe
,
1047 struct pipe_buffer
*buf
)
1049 struct rchan_buf
*rbuf
;
1051 rbuf
= (struct rchan_buf
*)page_private(buf
->page
);
1052 relay_consume_bytes(rbuf
, buf
->private);
1055 static struct pipe_buf_operations relay_pipe_buf_ops
= {
1057 .map
= generic_pipe_buf_map
,
1058 .unmap
= generic_pipe_buf_unmap
,
1059 .confirm
= generic_pipe_buf_confirm
,
1060 .release
= relay_pipe_buf_release
,
1061 .steal
= generic_pipe_buf_steal
,
1062 .get
= generic_pipe_buf_get
,
1066 * subbuf_splice_actor - splice up to one subbuf's worth of data
1068 static int subbuf_splice_actor(struct file
*in
,
1070 struct pipe_inode_info
*pipe
,
1075 unsigned int pidx
, poff
, total_len
, subbuf_pages
, ret
;
1076 struct rchan_buf
*rbuf
= in
->private_data
;
1077 unsigned int subbuf_size
= rbuf
->chan
->subbuf_size
;
1078 uint64_t pos
= (uint64_t) *ppos
;
1079 uint32_t alloc_size
= (uint32_t) rbuf
->chan
->alloc_size
;
1080 size_t read_start
= (size_t) do_div(pos
, alloc_size
);
1081 size_t read_subbuf
= read_start
/ subbuf_size
;
1082 size_t padding
= rbuf
->padding
[read_subbuf
];
1083 size_t nonpad_end
= read_subbuf
* subbuf_size
+ subbuf_size
- padding
;
1084 struct page
*pages
[PIPE_BUFFERS
];
1085 struct partial_page partial
[PIPE_BUFFERS
];
1086 struct splice_pipe_desc spd
= {
1091 .ops
= &relay_pipe_buf_ops
,
1094 if (rbuf
->subbufs_produced
== rbuf
->subbufs_consumed
)
1098 * Adjust read len, if longer than what is available
1100 if (len
> (subbuf_size
- read_start
% subbuf_size
))
1101 len
= subbuf_size
- read_start
% subbuf_size
;
1103 subbuf_pages
= rbuf
->chan
->alloc_size
>> PAGE_SHIFT
;
1104 pidx
= (read_start
/ PAGE_SIZE
) % subbuf_pages
;
1105 poff
= read_start
& ~PAGE_MASK
;
1107 for (total_len
= 0; spd
.nr_pages
< subbuf_pages
; spd
.nr_pages
++) {
1108 unsigned int this_len
, this_end
, private;
1109 unsigned int cur_pos
= read_start
+ total_len
;
1114 this_len
= min_t(unsigned long, len
, PAGE_SIZE
- poff
);
1117 spd
.pages
[spd
.nr_pages
] = rbuf
->page_array
[pidx
];
1118 spd
.partial
[spd
.nr_pages
].offset
= poff
;
1120 this_end
= cur_pos
+ this_len
;
1121 if (this_end
>= nonpad_end
) {
1122 this_len
= nonpad_end
- cur_pos
;
1123 private = this_len
+ padding
;
1125 spd
.partial
[spd
.nr_pages
].len
= this_len
;
1126 spd
.partial
[spd
.nr_pages
].private = private;
1129 total_len
+= this_len
;
1131 pidx
= (pidx
+ 1) % subbuf_pages
;
1133 if (this_end
>= nonpad_end
) {
1142 ret
= *nonpad_ret
= splice_to_pipe(pipe
, &spd
);
1143 if (ret
< 0 || ret
< total_len
)
1146 if (read_start
+ ret
== nonpad_end
)
1152 static ssize_t
relay_file_splice_read(struct file
*in
,
1154 struct pipe_inode_info
*pipe
,
1166 ret
= subbuf_splice_actor(in
, ppos
, pipe
, len
, flags
, &nonpad_ret
);
1172 if (flags
& SPLICE_F_NONBLOCK
) {
1183 spliced
+= nonpad_ret
;
1193 const struct file_operations relay_file_operations
= {
1194 .open
= relay_file_open
,
1195 .poll
= relay_file_poll
,
1196 .mmap
= relay_file_mmap
,
1197 .read
= relay_file_read
,
1198 .llseek
= no_llseek
,
1199 .release
= relay_file_release
,
1200 .splice_read
= relay_file_splice_read
,
1202 EXPORT_SYMBOL_GPL(relay_file_operations
);
1204 static __init
int relay_init(void)
1207 hotcpu_notifier(relay_hotcpu_callback
, 0);
1211 module_init(relay_init
);