xref: /linux/kernel/relay.c (revision 2489e958129ff7cbf26a34ee33cdc9ccbd68fe3c)
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 	if (!buf->chan->cb->subbuf_start)
255 		return !relay_buf_full(buf);
256 
257 	return buf->chan->cb->subbuf_start(buf, subbuf,
258 					   prev_subbuf);
259 }
260 
261 /**
262  *	wakeup_readers - wake up readers waiting on a channel
263  *	@work: contains the channel buffer
264  *
265  *	This is the function used to defer reader waking
266  */
267 static void wakeup_readers(struct irq_work *work)
268 {
269 	struct rchan_buf *buf;
270 
271 	buf = container_of(work, struct rchan_buf, wakeup_work);
272 	wake_up_interruptible(&buf->read_wait);
273 }
274 
275 /**
276  *	__relay_reset - reset a channel buffer
277  *	@buf: the channel buffer
278  *	@init: 1 if this is a first-time initialization
279  *
280  *	See relay_reset() for description of effect.
281  */
282 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
283 {
284 	size_t i;
285 
286 	if (init) {
287 		init_waitqueue_head(&buf->read_wait);
288 		kref_init(&buf->kref);
289 		init_irq_work(&buf->wakeup_work, wakeup_readers);
290 	} else {
291 		irq_work_sync(&buf->wakeup_work);
292 	}
293 
294 	buf->subbufs_produced = 0;
295 	buf->subbufs_consumed = 0;
296 	buf->bytes_consumed = 0;
297 	buf->finalized = 0;
298 	buf->data = buf->start;
299 	buf->offset = 0;
300 
301 	for (i = 0; i < buf->chan->n_subbufs; i++)
302 		buf->padding[i] = 0;
303 
304 	relay_subbuf_start(buf, buf->data, NULL);
305 }
306 
307 /**
308  *	relay_reset - reset the channel
309  *	@chan: the channel
310  *
311  *	This has the effect of erasing all data from all channel buffers
312  *	and restarting the channel in its initial state.  The buffers
313  *	are not freed, so any mappings are still in effect.
314  *
315  *	NOTE. Care should be taken that the channel isn't actually
316  *	being used by anything when this call is made.
317  */
318 void relay_reset(struct rchan *chan)
319 {
320 	struct rchan_buf *buf;
321 	unsigned int i;
322 
323 	if (!chan)
324 		return;
325 
326 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
327 		__relay_reset(buf, 0);
328 		return;
329 	}
330 
331 	mutex_lock(&relay_channels_mutex);
332 	for_each_possible_cpu(i)
333 		if ((buf = *per_cpu_ptr(chan->buf, i)))
334 			__relay_reset(buf, 0);
335 	mutex_unlock(&relay_channels_mutex);
336 }
337 EXPORT_SYMBOL_GPL(relay_reset);
338 
339 static inline void relay_set_buf_dentry(struct rchan_buf *buf,
340 					struct dentry *dentry)
341 {
342 	buf->dentry = dentry;
343 	d_inode(buf->dentry)->i_size = buf->early_bytes;
344 }
345 
346 static struct dentry *relay_create_buf_file(struct rchan *chan,
347 					    struct rchan_buf *buf,
348 					    unsigned int cpu)
349 {
350 	struct dentry *dentry;
351 	char *tmpname;
352 
353 	tmpname = kasprintf(GFP_KERNEL, "%s%d", chan->base_filename, cpu);
354 	if (!tmpname)
355 		return NULL;
356 
357 	/* Create file in fs */
358 	dentry = chan->cb->create_buf_file(tmpname, chan->parent,
359 					   S_IRUSR, buf,
360 					   &chan->is_global);
361 	if (IS_ERR(dentry))
362 		dentry = NULL;
363 
364 	kfree(tmpname);
365 
366 	return dentry;
367 }
368 
369 /*
370  *	relay_open_buf - create a new relay channel buffer
371  *
372  *	used by relay_open() and CPU hotplug.
373  */
374 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
375 {
376 	struct rchan_buf *buf;
377 	struct dentry *dentry;
378 
379  	if (chan->is_global)
380 		return *per_cpu_ptr(chan->buf, 0);
381 
382 	buf = relay_create_buf(chan);
383 	if (!buf)
384 		return NULL;
385 
386 	if (chan->has_base_filename) {
387 		dentry = relay_create_buf_file(chan, buf, cpu);
388 		if (!dentry)
389 			goto free_buf;
390 		relay_set_buf_dentry(buf, dentry);
391 	} else {
392 		/* Only retrieve global info, nothing more, nothing less */
393 		dentry = chan->cb->create_buf_file(NULL, NULL,
394 						   S_IRUSR, buf,
395 						   &chan->is_global);
396 		if (IS_ERR_OR_NULL(dentry))
397 			goto free_buf;
398 	}
399 
400  	buf->cpu = cpu;
401  	__relay_reset(buf, 1);
402 
403  	if(chan->is_global) {
404 		*per_cpu_ptr(chan->buf, 0) = buf;
405  		buf->cpu = 0;
406   	}
407 
408 	return buf;
409 
410 free_buf:
411  	relay_destroy_buf(buf);
412 	return NULL;
413 }
414 
415 /**
416  *	relay_close_buf - close a channel buffer
417  *	@buf: channel buffer
418  *
419  *	Marks the buffer finalized and restores the default callbacks.
420  *	The channel buffer and channel buffer data structure are then freed
421  *	automatically when the last reference is given up.
422  */
423 static void relay_close_buf(struct rchan_buf *buf)
424 {
425 	buf->finalized = 1;
426 	irq_work_sync(&buf->wakeup_work);
427 	buf->chan->cb->remove_buf_file(buf->dentry);
428 	kref_put(&buf->kref, relay_remove_buf);
429 }
430 
431 int relay_prepare_cpu(unsigned int cpu)
432 {
433 	struct rchan *chan;
434 	struct rchan_buf *buf;
435 
436 	mutex_lock(&relay_channels_mutex);
437 	list_for_each_entry(chan, &relay_channels, list) {
438 		if (*per_cpu_ptr(chan->buf, cpu))
439 			continue;
440 		buf = relay_open_buf(chan, cpu);
441 		if (!buf) {
442 			pr_err("relay: cpu %d buffer creation failed\n", cpu);
443 			mutex_unlock(&relay_channels_mutex);
444 			return -ENOMEM;
445 		}
446 		*per_cpu_ptr(chan->buf, cpu) = buf;
447 	}
448 	mutex_unlock(&relay_channels_mutex);
449 	return 0;
450 }
451 
452 /**
453  *	relay_open - create a new relay channel
454  *	@base_filename: base name of files to create
455  *	@parent: dentry of parent directory, %NULL for root directory or buffer
456  *	@subbuf_size: size of sub-buffers
457  *	@n_subbufs: number of sub-buffers
458  *	@cb: client callback functions
459  *	@private_data: user-defined data
460  *
461  *	Returns channel pointer if successful, %NULL otherwise.
462  *
463  *	Creates a channel buffer for each cpu using the sizes and
464  *	attributes specified.  The created channel buffer files
465  *	will be named base_filename0...base_filenameN-1.  File
466  *	permissions will be %S_IRUSR.
467  */
468 struct rchan *relay_open(const char *base_filename,
469 			 struct dentry *parent,
470 			 size_t subbuf_size,
471 			 size_t n_subbufs,
472 			 const struct rchan_callbacks *cb,
473 			 void *private_data)
474 {
475 	unsigned int i;
476 	struct rchan *chan;
477 	struct rchan_buf *buf;
478 
479 	if (!(subbuf_size && n_subbufs))
480 		return NULL;
481 	if (subbuf_size > UINT_MAX / n_subbufs)
482 		return NULL;
483 	if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
484 		return NULL;
485 
486 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
487 	if (!chan)
488 		return NULL;
489 
490 	chan->buf = alloc_percpu(struct rchan_buf *);
491 	if (!chan->buf) {
492 		kfree(chan);
493 		return NULL;
494 	}
495 
496 	chan->version = RELAYFS_CHANNEL_VERSION;
497 	chan->n_subbufs = n_subbufs;
498 	chan->subbuf_size = subbuf_size;
499 	chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
500 	chan->parent = parent;
501 	chan->private_data = private_data;
502 	if (base_filename) {
503 		chan->has_base_filename = 1;
504 		strscpy(chan->base_filename, base_filename, NAME_MAX);
505 	}
506 	chan->cb = cb;
507 	kref_init(&chan->kref);
508 
509 	mutex_lock(&relay_channels_mutex);
510 	for_each_online_cpu(i) {
511 		buf = relay_open_buf(chan, i);
512 		if (!buf)
513 			goto free_bufs;
514 		*per_cpu_ptr(chan->buf, i) = buf;
515 	}
516 	list_add(&chan->list, &relay_channels);
517 	mutex_unlock(&relay_channels_mutex);
518 
519 	return chan;
520 
521 free_bufs:
522 	for_each_possible_cpu(i) {
523 		if ((buf = *per_cpu_ptr(chan->buf, i)))
524 			relay_close_buf(buf);
525 	}
526 
527 	kref_put(&chan->kref, relay_destroy_channel);
528 	mutex_unlock(&relay_channels_mutex);
529 	return NULL;
530 }
531 EXPORT_SYMBOL_GPL(relay_open);
532 
533 struct rchan_percpu_buf_dispatcher {
534 	struct rchan_buf *buf;
535 	struct dentry *dentry;
536 };
537 
538 /**
539  *	relay_switch_subbuf - switch to a new sub-buffer
540  *	@buf: channel buffer
541  *	@length: size of current event
542  *
543  *	Returns either the length passed in or 0 if full.
544  *
545  *	Performs sub-buffer-switch tasks such as invoking callbacks,
546  *	updating padding counts, waking up readers, etc.
547  */
548 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
549 {
550 	void *old, *new;
551 	size_t old_subbuf, new_subbuf;
552 
553 	if (unlikely(length > buf->chan->subbuf_size))
554 		goto toobig;
555 
556 	if (buf->offset != buf->chan->subbuf_size + 1) {
557 		size_t prev_padding;
558 
559 		prev_padding = buf->chan->subbuf_size - buf->offset;
560 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
561 		buf->padding[old_subbuf] = prev_padding;
562 		buf->subbufs_produced++;
563 		if (buf->dentry)
564 			d_inode(buf->dentry)->i_size +=
565 				buf->chan->subbuf_size -
566 				buf->padding[old_subbuf];
567 		else
568 			buf->early_bytes += buf->chan->subbuf_size -
569 					    buf->padding[old_subbuf];
570 		smp_mb();
571 		if (waitqueue_active(&buf->read_wait)) {
572 			/*
573 			 * Calling wake_up_interruptible() from here
574 			 * will deadlock if we happen to be logging
575 			 * from the scheduler (trying to re-grab
576 			 * rq->lock), so defer it.
577 			 */
578 			irq_work_queue(&buf->wakeup_work);
579 		}
580 	}
581 
582 	old = buf->data;
583 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
584 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
585 	buf->offset = 0;
586 	if (!relay_subbuf_start(buf, new, old)) {
587 		buf->offset = buf->chan->subbuf_size + 1;
588 		return 0;
589 	}
590 	buf->data = new;
591 	buf->padding[new_subbuf] = 0;
592 
593 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
594 		goto toobig;
595 
596 	return length;
597 
598 toobig:
599 	buf->chan->last_toobig = length;
600 	return 0;
601 }
602 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
603 
604 /**
605  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
606  *	@chan: the channel
607  *	@cpu: the cpu associated with the channel buffer to update
608  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
609  *
610  *	Adds to the channel buffer's consumed sub-buffer count.
611  *	subbufs_consumed should be the number of sub-buffers newly consumed,
612  *	not the total consumed.
613  *
614  *	NOTE. Kernel clients don't need to call this function if the channel
615  *	mode is 'overwrite'.
616  */
617 void relay_subbufs_consumed(struct rchan *chan,
618 			    unsigned int cpu,
619 			    size_t subbufs_consumed)
620 {
621 	struct rchan_buf *buf;
622 
623 	if (!chan || cpu >= NR_CPUS)
624 		return;
625 
626 	buf = *per_cpu_ptr(chan->buf, cpu);
627 	if (!buf || subbufs_consumed > chan->n_subbufs)
628 		return;
629 
630 	if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
631 		buf->subbufs_consumed = buf->subbufs_produced;
632 	else
633 		buf->subbufs_consumed += subbufs_consumed;
634 }
635 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
636 
637 /**
638  *	relay_close - close the channel
639  *	@chan: the channel
640  *
641  *	Closes all channel buffers and frees the channel.
642  */
643 void relay_close(struct rchan *chan)
644 {
645 	struct rchan_buf *buf;
646 	unsigned int i;
647 
648 	if (!chan)
649 		return;
650 
651 	mutex_lock(&relay_channels_mutex);
652 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
653 		relay_close_buf(buf);
654 	else
655 		for_each_possible_cpu(i)
656 			if ((buf = *per_cpu_ptr(chan->buf, i)))
657 				relay_close_buf(buf);
658 
659 	if (chan->last_toobig)
660 		printk(KERN_WARNING "relay: one or more items not logged "
661 		       "[item size (%zd) > sub-buffer size (%zd)]\n",
662 		       chan->last_toobig, chan->subbuf_size);
663 
664 	list_del(&chan->list);
665 	kref_put(&chan->kref, relay_destroy_channel);
666 	mutex_unlock(&relay_channels_mutex);
667 }
668 EXPORT_SYMBOL_GPL(relay_close);
669 
670 /**
671  *	relay_flush - close the channel
672  *	@chan: the channel
673  *
674  *	Flushes all channel buffers, i.e. forces buffer switch.
675  */
676 void relay_flush(struct rchan *chan)
677 {
678 	struct rchan_buf *buf;
679 	unsigned int i;
680 
681 	if (!chan)
682 		return;
683 
684 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
685 		relay_switch_subbuf(buf, 0);
686 		return;
687 	}
688 
689 	mutex_lock(&relay_channels_mutex);
690 	for_each_possible_cpu(i)
691 		if ((buf = *per_cpu_ptr(chan->buf, i)))
692 			relay_switch_subbuf(buf, 0);
693 	mutex_unlock(&relay_channels_mutex);
694 }
695 EXPORT_SYMBOL_GPL(relay_flush);
696 
697 /**
698  *	relay_file_open - open file op for relay files
699  *	@inode: the inode
700  *	@filp: the file
701  *
702  *	Increments the channel buffer refcount.
703  */
704 static int relay_file_open(struct inode *inode, struct file *filp)
705 {
706 	struct rchan_buf *buf = inode->i_private;
707 	kref_get(&buf->kref);
708 	filp->private_data = buf;
709 
710 	return nonseekable_open(inode, filp);
711 }
712 
713 /**
714  *	relay_file_mmap - mmap file op for relay files
715  *	@filp: the file
716  *	@vma: the vma describing what to map
717  *
718  *	Calls upon relay_mmap_buf() to map the file into user space.
719  */
720 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
721 {
722 	struct rchan_buf *buf = filp->private_data;
723 	return relay_mmap_buf(buf, vma);
724 }
725 
726 /**
727  *	relay_file_poll - poll file op for relay files
728  *	@filp: the file
729  *	@wait: poll table
730  *
731  *	Poll implemention.
732  */
733 static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
734 {
735 	__poll_t mask = 0;
736 	struct rchan_buf *buf = filp->private_data;
737 
738 	if (buf->finalized)
739 		return EPOLLERR;
740 
741 	if (filp->f_mode & FMODE_READ) {
742 		poll_wait(filp, &buf->read_wait, wait);
743 		if (!relay_buf_empty(buf))
744 			mask |= EPOLLIN | EPOLLRDNORM;
745 	}
746 
747 	return mask;
748 }
749 
750 /**
751  *	relay_file_release - release file op for relay files
752  *	@inode: the inode
753  *	@filp: the file
754  *
755  *	Decrements the channel refcount, as the filesystem is
756  *	no longer using it.
757  */
758 static int relay_file_release(struct inode *inode, struct file *filp)
759 {
760 	struct rchan_buf *buf = filp->private_data;
761 	kref_put(&buf->kref, relay_remove_buf);
762 
763 	return 0;
764 }
765 
766 /*
767  *	relay_file_read_consume - update the consumed count for the buffer
768  */
769 static void relay_file_read_consume(struct rchan_buf *buf,
770 				    size_t read_pos,
771 				    size_t bytes_consumed)
772 {
773 	size_t subbuf_size = buf->chan->subbuf_size;
774 	size_t n_subbufs = buf->chan->n_subbufs;
775 	size_t read_subbuf;
776 
777 	if (buf->subbufs_produced == buf->subbufs_consumed &&
778 	    buf->offset == buf->bytes_consumed)
779 		return;
780 
781 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
782 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
783 		buf->bytes_consumed = 0;
784 	}
785 
786 	buf->bytes_consumed += bytes_consumed;
787 	if (!read_pos)
788 		read_subbuf = buf->subbufs_consumed % n_subbufs;
789 	else
790 		read_subbuf = read_pos / buf->chan->subbuf_size;
791 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
792 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
793 		    (buf->offset == subbuf_size))
794 			return;
795 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
796 		buf->bytes_consumed = 0;
797 	}
798 }
799 
800 /*
801  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
802  */
803 static int relay_file_read_avail(struct rchan_buf *buf)
804 {
805 	size_t subbuf_size = buf->chan->subbuf_size;
806 	size_t n_subbufs = buf->chan->n_subbufs;
807 	size_t produced = buf->subbufs_produced;
808 	size_t consumed;
809 
810 	relay_file_read_consume(buf, 0, 0);
811 
812 	consumed = buf->subbufs_consumed;
813 
814 	if (unlikely(buf->offset > subbuf_size)) {
815 		if (produced == consumed)
816 			return 0;
817 		return 1;
818 	}
819 
820 	if (unlikely(produced - consumed >= n_subbufs)) {
821 		consumed = produced - n_subbufs + 1;
822 		buf->subbufs_consumed = consumed;
823 		buf->bytes_consumed = 0;
824 	}
825 
826 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
827 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
828 
829 	if (consumed > produced)
830 		produced += n_subbufs * subbuf_size;
831 
832 	if (consumed == produced) {
833 		if (buf->offset == subbuf_size &&
834 		    buf->subbufs_produced > buf->subbufs_consumed)
835 			return 1;
836 		return 0;
837 	}
838 
839 	return 1;
840 }
841 
842 /**
843  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
844  *	@read_pos: file read position
845  *	@buf: relay channel buffer
846  */
847 static size_t relay_file_read_subbuf_avail(size_t read_pos,
848 					   struct rchan_buf *buf)
849 {
850 	size_t padding, avail = 0;
851 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
852 	size_t subbuf_size = buf->chan->subbuf_size;
853 
854 	write_subbuf = (buf->data - buf->start) / subbuf_size;
855 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
856 	read_subbuf = read_pos / subbuf_size;
857 	read_offset = read_pos % subbuf_size;
858 	padding = buf->padding[read_subbuf];
859 
860 	if (read_subbuf == write_subbuf) {
861 		if (read_offset + padding < write_offset)
862 			avail = write_offset - (read_offset + padding);
863 	} else
864 		avail = (subbuf_size - padding) - read_offset;
865 
866 	return avail;
867 }
868 
869 /**
870  *	relay_file_read_start_pos - find the first available byte to read
871  *	@buf: relay channel buffer
872  *
873  *	If the read_pos is in the middle of padding, return the
874  *	position of the first actually available byte, otherwise
875  *	return the original value.
876  */
877 static size_t relay_file_read_start_pos(struct rchan_buf *buf)
878 {
879 	size_t read_subbuf, padding, padding_start, padding_end;
880 	size_t subbuf_size = buf->chan->subbuf_size;
881 	size_t n_subbufs = buf->chan->n_subbufs;
882 	size_t consumed = buf->subbufs_consumed % n_subbufs;
883 	size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed)
884 			% (n_subbufs * subbuf_size);
885 
886 	read_subbuf = read_pos / subbuf_size;
887 	padding = buf->padding[read_subbuf];
888 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
889 	padding_end = (read_subbuf + 1) * subbuf_size;
890 	if (read_pos >= padding_start && read_pos < padding_end) {
891 		read_subbuf = (read_subbuf + 1) % n_subbufs;
892 		read_pos = read_subbuf * subbuf_size;
893 	}
894 
895 	return read_pos;
896 }
897 
898 /**
899  *	relay_file_read_end_pos - return the new read position
900  *	@read_pos: file read position
901  *	@buf: relay channel buffer
902  *	@count: number of bytes to be read
903  */
904 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
905 				      size_t read_pos,
906 				      size_t count)
907 {
908 	size_t read_subbuf, padding, end_pos;
909 	size_t subbuf_size = buf->chan->subbuf_size;
910 	size_t n_subbufs = buf->chan->n_subbufs;
911 
912 	read_subbuf = read_pos / subbuf_size;
913 	padding = buf->padding[read_subbuf];
914 	if (read_pos % subbuf_size + count + padding == subbuf_size)
915 		end_pos = (read_subbuf + 1) * subbuf_size;
916 	else
917 		end_pos = read_pos + count;
918 	if (end_pos >= subbuf_size * n_subbufs)
919 		end_pos = 0;
920 
921 	return end_pos;
922 }
923 
924 static ssize_t relay_file_read(struct file *filp,
925 			       char __user *buffer,
926 			       size_t count,
927 			       loff_t *ppos)
928 {
929 	struct rchan_buf *buf = filp->private_data;
930 	size_t read_start, avail;
931 	size_t written = 0;
932 	int ret;
933 
934 	if (!count)
935 		return 0;
936 
937 	inode_lock(file_inode(filp));
938 	do {
939 		void *from;
940 
941 		if (!relay_file_read_avail(buf))
942 			break;
943 
944 		read_start = relay_file_read_start_pos(buf);
945 		avail = relay_file_read_subbuf_avail(read_start, buf);
946 		if (!avail)
947 			break;
948 
949 		avail = min(count, avail);
950 		from = buf->start + read_start;
951 		ret = avail;
952 		if (copy_to_user(buffer, from, avail))
953 			break;
954 
955 		buffer += ret;
956 		written += ret;
957 		count -= ret;
958 
959 		relay_file_read_consume(buf, read_start, ret);
960 		*ppos = relay_file_read_end_pos(buf, read_start, ret);
961 	} while (count);
962 	inode_unlock(file_inode(filp));
963 
964 	return written;
965 }
966 
967 
968 const struct file_operations relay_file_operations = {
969 	.open		= relay_file_open,
970 	.poll		= relay_file_poll,
971 	.mmap		= relay_file_mmap,
972 	.read		= relay_file_read,
973 	.release	= relay_file_release,
974 };
975 EXPORT_SYMBOL_GPL(relay_file_operations);
976