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  */
relay_buf_fault(struct vm_fault * vmf)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  */
relay_alloc_page_array(unsigned int n_pages)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  */
relay_free_page_array(struct page ** array)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  */
relay_mmap_buf(struct rchan_buf * buf,struct vm_area_struct * vma)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  */
relay_alloc_buf(struct rchan_buf * buf,size_t * size)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);
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 	memset(mem, 0, *size);
131 	buf->page_count = n_pages;
132 	return mem;
133 
134 depopulate:
135 	for (j = 0; j < i; j++)
136 		__free_page(buf->page_array[j]);
137 	relay_free_page_array(buf->page_array);
138 	return NULL;
139 }
140 
141 /**
142  *	relay_create_buf - allocate and initialize a channel buffer
143  *	@chan: the relay channel
144  *
145  *	Returns channel buffer if successful, %NULL otherwise.
146  */
relay_create_buf(struct rchan * chan)147 static struct rchan_buf *relay_create_buf(struct rchan *chan)
148 {
149 	struct rchan_buf *buf;
150 
151 	if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t))
152 		return NULL;
153 
154 	buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
155 	if (!buf)
156 		return NULL;
157 	buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t),
158 				     GFP_KERNEL);
159 	if (!buf->padding)
160 		goto free_buf;
161 
162 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
163 	if (!buf->start)
164 		goto free_buf;
165 
166 	buf->chan = chan;
167 	kref_get(&buf->chan->kref);
168 	return buf;
169 
170 free_buf:
171 	kfree(buf->padding);
172 	kfree(buf);
173 	return NULL;
174 }
175 
176 /**
177  *	relay_destroy_channel - free the channel struct
178  *	@kref: target kernel reference that contains the relay channel
179  *
180  *	Should only be called from kref_put().
181  */
relay_destroy_channel(struct kref * kref)182 static void relay_destroy_channel(struct kref *kref)
183 {
184 	struct rchan *chan = container_of(kref, struct rchan, kref);
185 	free_percpu(chan->buf);
186 	kfree(chan);
187 }
188 
189 /**
190  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
191  *	@buf: the buffer struct
192  */
relay_destroy_buf(struct rchan_buf * buf)193 static void relay_destroy_buf(struct rchan_buf *buf)
194 {
195 	struct rchan *chan = buf->chan;
196 	unsigned int i;
197 
198 	if (likely(buf->start)) {
199 		vunmap(buf->start);
200 		for (i = 0; i < buf->page_count; i++)
201 			__free_page(buf->page_array[i]);
202 		relay_free_page_array(buf->page_array);
203 	}
204 	*per_cpu_ptr(chan->buf, buf->cpu) = NULL;
205 	kfree(buf->padding);
206 	kfree(buf);
207 	kref_put(&chan->kref, relay_destroy_channel);
208 }
209 
210 /**
211  *	relay_remove_buf - remove a channel buffer
212  *	@kref: target kernel reference that contains the relay buffer
213  *
214  *	Removes the file from the filesystem, which also frees the
215  *	rchan_buf_struct and the channel buffer.  Should only be called from
216  *	kref_put().
217  */
relay_remove_buf(struct kref * kref)218 static void relay_remove_buf(struct kref *kref)
219 {
220 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
221 	relay_destroy_buf(buf);
222 }
223 
224 /**
225  *	relay_buf_empty - boolean, is the channel buffer empty?
226  *	@buf: channel buffer
227  *
228  *	Returns 1 if the buffer is empty, 0 otherwise.
229  */
relay_buf_empty(struct rchan_buf * buf)230 static int relay_buf_empty(struct rchan_buf *buf)
231 {
232 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
233 }
234 
235 /**
236  *	relay_buf_full - boolean, is the channel buffer full?
237  *	@buf: channel buffer
238  *
239  *	Returns 1 if the buffer is full, 0 otherwise.
240  */
relay_buf_full(struct rchan_buf * buf)241 int relay_buf_full(struct rchan_buf *buf)
242 {
243 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
244 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
245 }
246 EXPORT_SYMBOL_GPL(relay_buf_full);
247 
248 /*
249  * High-level relay kernel API and associated functions.
250  */
251 
relay_subbuf_start(struct rchan_buf * buf,void * subbuf,void * prev_subbuf,size_t prev_padding)252 static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
253 			      void *prev_subbuf, size_t prev_padding)
254 {
255 	if (!buf->chan->cb->subbuf_start)
256 		return !relay_buf_full(buf);
257 
258 	return buf->chan->cb->subbuf_start(buf, subbuf,
259 					   prev_subbuf, prev_padding);
260 }
261 
262 /**
263  *	wakeup_readers - wake up readers waiting on a channel
264  *	@work: contains the channel buffer
265  *
266  *	This is the function used to defer reader waking
267  */
wakeup_readers(struct irq_work * work)268 static void wakeup_readers(struct irq_work *work)
269 {
270 	struct rchan_buf *buf;
271 
272 	buf = container_of(work, struct rchan_buf, wakeup_work);
273 	wake_up_interruptible(&buf->read_wait);
274 }
275 
276 /**
277  *	__relay_reset - reset a channel buffer
278  *	@buf: the channel buffer
279  *	@init: 1 if this is a first-time initialization
280  *
281  *	See relay_reset() for description of effect.
282  */
__relay_reset(struct rchan_buf * buf,unsigned int init)283 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
284 {
285 	size_t i;
286 
287 	if (init) {
288 		init_waitqueue_head(&buf->read_wait);
289 		kref_init(&buf->kref);
290 		init_irq_work(&buf->wakeup_work, wakeup_readers);
291 	} else {
292 		irq_work_sync(&buf->wakeup_work);
293 	}
294 
295 	buf->subbufs_produced = 0;
296 	buf->subbufs_consumed = 0;
297 	buf->bytes_consumed = 0;
298 	buf->finalized = 0;
299 	buf->data = buf->start;
300 	buf->offset = 0;
301 
302 	for (i = 0; i < buf->chan->n_subbufs; i++)
303 		buf->padding[i] = 0;
304 
305 	relay_subbuf_start(buf, buf->data, NULL, 0);
306 }
307 
308 /**
309  *	relay_reset - reset the channel
310  *	@chan: the channel
311  *
312  *	This has the effect of erasing all data from all channel buffers
313  *	and restarting the channel in its initial state.  The buffers
314  *	are not freed, so any mappings are still in effect.
315  *
316  *	NOTE. Care should be taken that the channel isn't actually
317  *	being used by anything when this call is made.
318  */
relay_reset(struct rchan * chan)319 void relay_reset(struct rchan *chan)
320 {
321 	struct rchan_buf *buf;
322 	unsigned int i;
323 
324 	if (!chan)
325 		return;
326 
327 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
328 		__relay_reset(buf, 0);
329 		return;
330 	}
331 
332 	mutex_lock(&relay_channels_mutex);
333 	for_each_possible_cpu(i)
334 		if ((buf = *per_cpu_ptr(chan->buf, i)))
335 			__relay_reset(buf, 0);
336 	mutex_unlock(&relay_channels_mutex);
337 }
338 EXPORT_SYMBOL_GPL(relay_reset);
339 
relay_set_buf_dentry(struct rchan_buf * buf,struct dentry * dentry)340 static inline void relay_set_buf_dentry(struct rchan_buf *buf,
341 					struct dentry *dentry)
342 {
343 	buf->dentry = dentry;
344 	d_inode(buf->dentry)->i_size = buf->early_bytes;
345 }
346 
relay_create_buf_file(struct rchan * chan,struct rchan_buf * buf,unsigned int cpu)347 static struct dentry *relay_create_buf_file(struct rchan *chan,
348 					    struct rchan_buf *buf,
349 					    unsigned int cpu)
350 {
351 	struct dentry *dentry;
352 	char *tmpname;
353 
354 	tmpname = kasprintf(GFP_KERNEL, "%s%d", chan->base_filename, cpu);
355 	if (!tmpname)
356 		return NULL;
357 
358 	/* Create file in fs */
359 	dentry = chan->cb->create_buf_file(tmpname, chan->parent,
360 					   S_IRUSR, buf,
361 					   &chan->is_global);
362 	if (IS_ERR(dentry))
363 		dentry = NULL;
364 
365 	kfree(tmpname);
366 
367 	return dentry;
368 }
369 
370 /*
371  *	relay_open_buf - create a new relay channel buffer
372  *
373  *	used by relay_open() and CPU hotplug.
374  */
relay_open_buf(struct rchan * chan,unsigned int cpu)375 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
376 {
377 	struct rchan_buf *buf;
378 	struct dentry *dentry;
379 
380  	if (chan->is_global)
381 		return *per_cpu_ptr(chan->buf, 0);
382 
383 	buf = relay_create_buf(chan);
384 	if (!buf)
385 		return NULL;
386 
387 	if (chan->has_base_filename) {
388 		dentry = relay_create_buf_file(chan, buf, cpu);
389 		if (!dentry)
390 			goto free_buf;
391 		relay_set_buf_dentry(buf, dentry);
392 	} else {
393 		/* Only retrieve global info, nothing more, nothing less */
394 		dentry = chan->cb->create_buf_file(NULL, NULL,
395 						   S_IRUSR, buf,
396 						   &chan->is_global);
397 		if (IS_ERR_OR_NULL(dentry))
398 			goto free_buf;
399 	}
400 
401  	buf->cpu = cpu;
402  	__relay_reset(buf, 1);
403 
404  	if(chan->is_global) {
405 		*per_cpu_ptr(chan->buf, 0) = buf;
406  		buf->cpu = 0;
407   	}
408 
409 	return buf;
410 
411 free_buf:
412  	relay_destroy_buf(buf);
413 	return NULL;
414 }
415 
416 /**
417  *	relay_close_buf - close a channel buffer
418  *	@buf: channel buffer
419  *
420  *	Marks the buffer finalized and restores the default callbacks.
421  *	The channel buffer and channel buffer data structure are then freed
422  *	automatically when the last reference is given up.
423  */
relay_close_buf(struct rchan_buf * buf)424 static void relay_close_buf(struct rchan_buf *buf)
425 {
426 	buf->finalized = 1;
427 	irq_work_sync(&buf->wakeup_work);
428 	buf->chan->cb->remove_buf_file(buf->dentry);
429 	kref_put(&buf->kref, relay_remove_buf);
430 }
431 
relay_prepare_cpu(unsigned int cpu)432 int relay_prepare_cpu(unsigned int cpu)
433 {
434 	struct rchan *chan;
435 	struct rchan_buf *buf;
436 
437 	mutex_lock(&relay_channels_mutex);
438 	list_for_each_entry(chan, &relay_channels, list) {
439 		if (*per_cpu_ptr(chan->buf, cpu))
440 			continue;
441 		buf = relay_open_buf(chan, cpu);
442 		if (!buf) {
443 			pr_err("relay: cpu %d buffer creation failed\n", cpu);
444 			mutex_unlock(&relay_channels_mutex);
445 			return -ENOMEM;
446 		}
447 		*per_cpu_ptr(chan->buf, cpu) = buf;
448 	}
449 	mutex_unlock(&relay_channels_mutex);
450 	return 0;
451 }
452 
453 /**
454  *	relay_open - create a new relay channel
455  *	@base_filename: base name of files to create, %NULL for buffering only
456  *	@parent: dentry of parent directory, %NULL for root directory or buffer
457  *	@subbuf_size: size of sub-buffers
458  *	@n_subbufs: number of sub-buffers
459  *	@cb: client callback functions
460  *	@private_data: user-defined data
461  *
462  *	Returns channel pointer if successful, %NULL otherwise.
463  *
464  *	Creates a channel buffer for each cpu using the sizes and
465  *	attributes specified.  The created channel buffer files
466  *	will be named base_filename0...base_filenameN-1.  File
467  *	permissions will be %S_IRUSR.
468  *
469  *	If opening a buffer (@parent = NULL) that you later wish to register
470  *	in a filesystem, call relay_late_setup_files() once the @parent dentry
471  *	is available.
472  */
relay_open(const char * base_filename,struct dentry * parent,size_t subbuf_size,size_t n_subbufs,const struct rchan_callbacks * cb,void * private_data)473 struct rchan *relay_open(const char *base_filename,
474 			 struct dentry *parent,
475 			 size_t subbuf_size,
476 			 size_t n_subbufs,
477 			 const struct rchan_callbacks *cb,
478 			 void *private_data)
479 {
480 	unsigned int i;
481 	struct rchan *chan;
482 	struct rchan_buf *buf;
483 
484 	if (!(subbuf_size && n_subbufs))
485 		return NULL;
486 	if (subbuf_size > UINT_MAX / n_subbufs)
487 		return NULL;
488 	if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
489 		return NULL;
490 
491 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
492 	if (!chan)
493 		return NULL;
494 
495 	chan->buf = alloc_percpu(struct rchan_buf *);
496 	if (!chan->buf) {
497 		kfree(chan);
498 		return NULL;
499 	}
500 
501 	chan->version = RELAYFS_CHANNEL_VERSION;
502 	chan->n_subbufs = n_subbufs;
503 	chan->subbuf_size = subbuf_size;
504 	chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
505 	chan->parent = parent;
506 	chan->private_data = private_data;
507 	if (base_filename) {
508 		chan->has_base_filename = 1;
509 		strscpy(chan->base_filename, base_filename, NAME_MAX);
510 	}
511 	chan->cb = cb;
512 	kref_init(&chan->kref);
513 
514 	mutex_lock(&relay_channels_mutex);
515 	for_each_online_cpu(i) {
516 		buf = relay_open_buf(chan, i);
517 		if (!buf)
518 			goto free_bufs;
519 		*per_cpu_ptr(chan->buf, i) = buf;
520 	}
521 	list_add(&chan->list, &relay_channels);
522 	mutex_unlock(&relay_channels_mutex);
523 
524 	return chan;
525 
526 free_bufs:
527 	for_each_possible_cpu(i) {
528 		if ((buf = *per_cpu_ptr(chan->buf, i)))
529 			relay_close_buf(buf);
530 	}
531 
532 	kref_put(&chan->kref, relay_destroy_channel);
533 	mutex_unlock(&relay_channels_mutex);
534 	return NULL;
535 }
536 EXPORT_SYMBOL_GPL(relay_open);
537 
538 struct rchan_percpu_buf_dispatcher {
539 	struct rchan_buf *buf;
540 	struct dentry *dentry;
541 };
542 
543 /* Called in atomic context. */
__relay_set_buf_dentry(void * info)544 static void __relay_set_buf_dentry(void *info)
545 {
546 	struct rchan_percpu_buf_dispatcher *p = info;
547 
548 	relay_set_buf_dentry(p->buf, p->dentry);
549 }
550 
551 /**
552  *	relay_late_setup_files - triggers file creation
553  *	@chan: channel to operate on
554  *	@base_filename: base name of files to create
555  *	@parent: dentry of parent directory, %NULL for root directory
556  *
557  *	Returns 0 if successful, non-zero otherwise.
558  *
559  *	Use to setup files for a previously buffer-only channel created
560  *	by relay_open() with a NULL parent dentry.
561  *
562  *	For example, this is useful for perfomring early tracing in kernel,
563  *	before VFS is up and then exposing the early results once the dentry
564  *	is available.
565  */
relay_late_setup_files(struct rchan * chan,const char * base_filename,struct dentry * parent)566 int relay_late_setup_files(struct rchan *chan,
567 			   const char *base_filename,
568 			   struct dentry *parent)
569 {
570 	int err = 0;
571 	unsigned int i, curr_cpu;
572 	unsigned long flags;
573 	struct dentry *dentry;
574 	struct rchan_buf *buf;
575 	struct rchan_percpu_buf_dispatcher disp;
576 
577 	if (!chan || !base_filename)
578 		return -EINVAL;
579 
580 	strscpy(chan->base_filename, base_filename, NAME_MAX);
581 
582 	mutex_lock(&relay_channels_mutex);
583 	/* Is chan already set up? */
584 	if (unlikely(chan->has_base_filename)) {
585 		mutex_unlock(&relay_channels_mutex);
586 		return -EEXIST;
587 	}
588 	chan->has_base_filename = 1;
589 	chan->parent = parent;
590 
591 	if (chan->is_global) {
592 		err = -EINVAL;
593 		buf = *per_cpu_ptr(chan->buf, 0);
594 		if (!WARN_ON_ONCE(!buf)) {
595 			dentry = relay_create_buf_file(chan, buf, 0);
596 			if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
597 				relay_set_buf_dentry(buf, dentry);
598 				err = 0;
599 			}
600 		}
601 		mutex_unlock(&relay_channels_mutex);
602 		return err;
603 	}
604 
605 	curr_cpu = get_cpu();
606 	/*
607 	 * The CPU hotplug notifier ran before us and created buffers with
608 	 * no files associated. So it's safe to call relay_setup_buf_file()
609 	 * on all currently online CPUs.
610 	 */
611 	for_each_online_cpu(i) {
612 		buf = *per_cpu_ptr(chan->buf, i);
613 		if (unlikely(!buf)) {
614 			WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
615 			err = -EINVAL;
616 			break;
617 		}
618 
619 		dentry = relay_create_buf_file(chan, buf, i);
620 		if (unlikely(!dentry)) {
621 			err = -EINVAL;
622 			break;
623 		}
624 
625 		if (curr_cpu == i) {
626 			local_irq_save(flags);
627 			relay_set_buf_dentry(buf, dentry);
628 			local_irq_restore(flags);
629 		} else {
630 			disp.buf = buf;
631 			disp.dentry = dentry;
632 			smp_mb();
633 			/* relay_channels_mutex must be held, so wait. */
634 			err = smp_call_function_single(i,
635 						       __relay_set_buf_dentry,
636 						       &disp, 1);
637 		}
638 		if (unlikely(err))
639 			break;
640 	}
641 	put_cpu();
642 	mutex_unlock(&relay_channels_mutex);
643 
644 	return err;
645 }
646 EXPORT_SYMBOL_GPL(relay_late_setup_files);
647 
648 /**
649  *	relay_switch_subbuf - switch to a new sub-buffer
650  *	@buf: channel buffer
651  *	@length: size of current event
652  *
653  *	Returns either the length passed in or 0 if full.
654  *
655  *	Performs sub-buffer-switch tasks such as invoking callbacks,
656  *	updating padding counts, waking up readers, etc.
657  */
relay_switch_subbuf(struct rchan_buf * buf,size_t length)658 size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
659 {
660 	void *old, *new;
661 	size_t old_subbuf, new_subbuf;
662 
663 	if (unlikely(length > buf->chan->subbuf_size))
664 		goto toobig;
665 
666 	if (buf->offset != buf->chan->subbuf_size + 1) {
667 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
668 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
669 		buf->padding[old_subbuf] = buf->prev_padding;
670 		buf->subbufs_produced++;
671 		if (buf->dentry)
672 			d_inode(buf->dentry)->i_size +=
673 				buf->chan->subbuf_size -
674 				buf->padding[old_subbuf];
675 		else
676 			buf->early_bytes += buf->chan->subbuf_size -
677 					    buf->padding[old_subbuf];
678 		smp_mb();
679 		if (waitqueue_active(&buf->read_wait)) {
680 			/*
681 			 * Calling wake_up_interruptible() from here
682 			 * will deadlock if we happen to be logging
683 			 * from the scheduler (trying to re-grab
684 			 * rq->lock), so defer it.
685 			 */
686 			irq_work_queue(&buf->wakeup_work);
687 		}
688 	}
689 
690 	old = buf->data;
691 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
692 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
693 	buf->offset = 0;
694 	if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
695 		buf->offset = buf->chan->subbuf_size + 1;
696 		return 0;
697 	}
698 	buf->data = new;
699 	buf->padding[new_subbuf] = 0;
700 
701 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
702 		goto toobig;
703 
704 	return length;
705 
706 toobig:
707 	buf->chan->last_toobig = length;
708 	return 0;
709 }
710 EXPORT_SYMBOL_GPL(relay_switch_subbuf);
711 
712 /**
713  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
714  *	@chan: the channel
715  *	@cpu: the cpu associated with the channel buffer to update
716  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
717  *
718  *	Adds to the channel buffer's consumed sub-buffer count.
719  *	subbufs_consumed should be the number of sub-buffers newly consumed,
720  *	not the total consumed.
721  *
722  *	NOTE. Kernel clients don't need to call this function if the channel
723  *	mode is 'overwrite'.
724  */
relay_subbufs_consumed(struct rchan * chan,unsigned int cpu,size_t subbufs_consumed)725 void relay_subbufs_consumed(struct rchan *chan,
726 			    unsigned int cpu,
727 			    size_t subbufs_consumed)
728 {
729 	struct rchan_buf *buf;
730 
731 	if (!chan || cpu >= NR_CPUS)
732 		return;
733 
734 	buf = *per_cpu_ptr(chan->buf, cpu);
735 	if (!buf || subbufs_consumed > chan->n_subbufs)
736 		return;
737 
738 	if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
739 		buf->subbufs_consumed = buf->subbufs_produced;
740 	else
741 		buf->subbufs_consumed += subbufs_consumed;
742 }
743 EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
744 
745 /**
746  *	relay_close - close the channel
747  *	@chan: the channel
748  *
749  *	Closes all channel buffers and frees the channel.
750  */
relay_close(struct rchan * chan)751 void relay_close(struct rchan *chan)
752 {
753 	struct rchan_buf *buf;
754 	unsigned int i;
755 
756 	if (!chan)
757 		return;
758 
759 	mutex_lock(&relay_channels_mutex);
760 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
761 		relay_close_buf(buf);
762 	else
763 		for_each_possible_cpu(i)
764 			if ((buf = *per_cpu_ptr(chan->buf, i)))
765 				relay_close_buf(buf);
766 
767 	if (chan->last_toobig)
768 		printk(KERN_WARNING "relay: one or more items not logged "
769 		       "[item size (%zd) > sub-buffer size (%zd)]\n",
770 		       chan->last_toobig, chan->subbuf_size);
771 
772 	list_del(&chan->list);
773 	kref_put(&chan->kref, relay_destroy_channel);
774 	mutex_unlock(&relay_channels_mutex);
775 }
776 EXPORT_SYMBOL_GPL(relay_close);
777 
778 /**
779  *	relay_flush - close the channel
780  *	@chan: the channel
781  *
782  *	Flushes all channel buffers, i.e. forces buffer switch.
783  */
relay_flush(struct rchan * chan)784 void relay_flush(struct rchan *chan)
785 {
786 	struct rchan_buf *buf;
787 	unsigned int i;
788 
789 	if (!chan)
790 		return;
791 
792 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
793 		relay_switch_subbuf(buf, 0);
794 		return;
795 	}
796 
797 	mutex_lock(&relay_channels_mutex);
798 	for_each_possible_cpu(i)
799 		if ((buf = *per_cpu_ptr(chan->buf, i)))
800 			relay_switch_subbuf(buf, 0);
801 	mutex_unlock(&relay_channels_mutex);
802 }
803 EXPORT_SYMBOL_GPL(relay_flush);
804 
805 /**
806  *	relay_file_open - open file op for relay files
807  *	@inode: the inode
808  *	@filp: the file
809  *
810  *	Increments the channel buffer refcount.
811  */
relay_file_open(struct inode * inode,struct file * filp)812 static int relay_file_open(struct inode *inode, struct file *filp)
813 {
814 	struct rchan_buf *buf = inode->i_private;
815 	kref_get(&buf->kref);
816 	filp->private_data = buf;
817 
818 	return nonseekable_open(inode, filp);
819 }
820 
821 /**
822  *	relay_file_mmap - mmap file op for relay files
823  *	@filp: the file
824  *	@vma: the vma describing what to map
825  *
826  *	Calls upon relay_mmap_buf() to map the file into user space.
827  */
relay_file_mmap(struct file * filp,struct vm_area_struct * vma)828 static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
829 {
830 	struct rchan_buf *buf = filp->private_data;
831 	return relay_mmap_buf(buf, vma);
832 }
833 
834 /**
835  *	relay_file_poll - poll file op for relay files
836  *	@filp: the file
837  *	@wait: poll table
838  *
839  *	Poll implemention.
840  */
relay_file_poll(struct file * filp,poll_table * wait)841 static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
842 {
843 	__poll_t mask = 0;
844 	struct rchan_buf *buf = filp->private_data;
845 
846 	if (buf->finalized)
847 		return EPOLLERR;
848 
849 	if (filp->f_mode & FMODE_READ) {
850 		poll_wait(filp, &buf->read_wait, wait);
851 		if (!relay_buf_empty(buf))
852 			mask |= EPOLLIN | EPOLLRDNORM;
853 	}
854 
855 	return mask;
856 }
857 
858 /**
859  *	relay_file_release - release file op for relay files
860  *	@inode: the inode
861  *	@filp: the file
862  *
863  *	Decrements the channel refcount, as the filesystem is
864  *	no longer using it.
865  */
relay_file_release(struct inode * inode,struct file * filp)866 static int relay_file_release(struct inode *inode, struct file *filp)
867 {
868 	struct rchan_buf *buf = filp->private_data;
869 	kref_put(&buf->kref, relay_remove_buf);
870 
871 	return 0;
872 }
873 
874 /*
875  *	relay_file_read_consume - update the consumed count for the buffer
876  */
relay_file_read_consume(struct rchan_buf * buf,size_t read_pos,size_t bytes_consumed)877 static void relay_file_read_consume(struct rchan_buf *buf,
878 				    size_t read_pos,
879 				    size_t bytes_consumed)
880 {
881 	size_t subbuf_size = buf->chan->subbuf_size;
882 	size_t n_subbufs = buf->chan->n_subbufs;
883 	size_t read_subbuf;
884 
885 	if (buf->subbufs_produced == buf->subbufs_consumed &&
886 	    buf->offset == buf->bytes_consumed)
887 		return;
888 
889 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
890 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
891 		buf->bytes_consumed = 0;
892 	}
893 
894 	buf->bytes_consumed += bytes_consumed;
895 	if (!read_pos)
896 		read_subbuf = buf->subbufs_consumed % n_subbufs;
897 	else
898 		read_subbuf = read_pos / buf->chan->subbuf_size;
899 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
900 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
901 		    (buf->offset == subbuf_size))
902 			return;
903 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
904 		buf->bytes_consumed = 0;
905 	}
906 }
907 
908 /*
909  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
910  */
relay_file_read_avail(struct rchan_buf * buf)911 static int relay_file_read_avail(struct rchan_buf *buf)
912 {
913 	size_t subbuf_size = buf->chan->subbuf_size;
914 	size_t n_subbufs = buf->chan->n_subbufs;
915 	size_t produced = buf->subbufs_produced;
916 	size_t consumed;
917 
918 	relay_file_read_consume(buf, 0, 0);
919 
920 	consumed = buf->subbufs_consumed;
921 
922 	if (unlikely(buf->offset > subbuf_size)) {
923 		if (produced == consumed)
924 			return 0;
925 		return 1;
926 	}
927 
928 	if (unlikely(produced - consumed >= n_subbufs)) {
929 		consumed = produced - n_subbufs + 1;
930 		buf->subbufs_consumed = consumed;
931 		buf->bytes_consumed = 0;
932 	}
933 
934 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
935 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
936 
937 	if (consumed > produced)
938 		produced += n_subbufs * subbuf_size;
939 
940 	if (consumed == produced) {
941 		if (buf->offset == subbuf_size &&
942 		    buf->subbufs_produced > buf->subbufs_consumed)
943 			return 1;
944 		return 0;
945 	}
946 
947 	return 1;
948 }
949 
950 /**
951  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
952  *	@read_pos: file read position
953  *	@buf: relay channel buffer
954  */
relay_file_read_subbuf_avail(size_t read_pos,struct rchan_buf * buf)955 static size_t relay_file_read_subbuf_avail(size_t read_pos,
956 					   struct rchan_buf *buf)
957 {
958 	size_t padding, avail = 0;
959 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
960 	size_t subbuf_size = buf->chan->subbuf_size;
961 
962 	write_subbuf = (buf->data - buf->start) / subbuf_size;
963 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
964 	read_subbuf = read_pos / subbuf_size;
965 	read_offset = read_pos % subbuf_size;
966 	padding = buf->padding[read_subbuf];
967 
968 	if (read_subbuf == write_subbuf) {
969 		if (read_offset + padding < write_offset)
970 			avail = write_offset - (read_offset + padding);
971 	} else
972 		avail = (subbuf_size - padding) - read_offset;
973 
974 	return avail;
975 }
976 
977 /**
978  *	relay_file_read_start_pos - find the first available byte to read
979  *	@buf: relay channel buffer
980  *
981  *	If the read_pos is in the middle of padding, return the
982  *	position of the first actually available byte, otherwise
983  *	return the original value.
984  */
relay_file_read_start_pos(struct rchan_buf * buf)985 static size_t relay_file_read_start_pos(struct rchan_buf *buf)
986 {
987 	size_t read_subbuf, padding, padding_start, padding_end;
988 	size_t subbuf_size = buf->chan->subbuf_size;
989 	size_t n_subbufs = buf->chan->n_subbufs;
990 	size_t consumed = buf->subbufs_consumed % n_subbufs;
991 	size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed)
992 			% (n_subbufs * subbuf_size);
993 
994 	read_subbuf = read_pos / subbuf_size;
995 	padding = buf->padding[read_subbuf];
996 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
997 	padding_end = (read_subbuf + 1) * subbuf_size;
998 	if (read_pos >= padding_start && read_pos < padding_end) {
999 		read_subbuf = (read_subbuf + 1) % n_subbufs;
1000 		read_pos = read_subbuf * subbuf_size;
1001 	}
1002 
1003 	return read_pos;
1004 }
1005 
1006 /**
1007  *	relay_file_read_end_pos - return the new read position
1008  *	@read_pos: file read position
1009  *	@buf: relay channel buffer
1010  *	@count: number of bytes to be read
1011  */
relay_file_read_end_pos(struct rchan_buf * buf,size_t read_pos,size_t count)1012 static size_t relay_file_read_end_pos(struct rchan_buf *buf,
1013 				      size_t read_pos,
1014 				      size_t count)
1015 {
1016 	size_t read_subbuf, padding, end_pos;
1017 	size_t subbuf_size = buf->chan->subbuf_size;
1018 	size_t n_subbufs = buf->chan->n_subbufs;
1019 
1020 	read_subbuf = read_pos / subbuf_size;
1021 	padding = buf->padding[read_subbuf];
1022 	if (read_pos % subbuf_size + count + padding == subbuf_size)
1023 		end_pos = (read_subbuf + 1) * subbuf_size;
1024 	else
1025 		end_pos = read_pos + count;
1026 	if (end_pos >= subbuf_size * n_subbufs)
1027 		end_pos = 0;
1028 
1029 	return end_pos;
1030 }
1031 
relay_file_read(struct file * filp,char __user * buffer,size_t count,loff_t * ppos)1032 static ssize_t relay_file_read(struct file *filp,
1033 			       char __user *buffer,
1034 			       size_t count,
1035 			       loff_t *ppos)
1036 {
1037 	struct rchan_buf *buf = filp->private_data;
1038 	size_t read_start, avail;
1039 	size_t written = 0;
1040 	int ret;
1041 
1042 	if (!count)
1043 		return 0;
1044 
1045 	inode_lock(file_inode(filp));
1046 	do {
1047 		void *from;
1048 
1049 		if (!relay_file_read_avail(buf))
1050 			break;
1051 
1052 		read_start = relay_file_read_start_pos(buf);
1053 		avail = relay_file_read_subbuf_avail(read_start, buf);
1054 		if (!avail)
1055 			break;
1056 
1057 		avail = min(count, avail);
1058 		from = buf->start + read_start;
1059 		ret = avail;
1060 		if (copy_to_user(buffer, from, avail))
1061 			break;
1062 
1063 		buffer += ret;
1064 		written += ret;
1065 		count -= ret;
1066 
1067 		relay_file_read_consume(buf, read_start, ret);
1068 		*ppos = relay_file_read_end_pos(buf, read_start, ret);
1069 	} while (count);
1070 	inode_unlock(file_inode(filp));
1071 
1072 	return written;
1073 }
1074 
1075 
1076 const struct file_operations relay_file_operations = {
1077 	.open		= relay_file_open,
1078 	.poll		= relay_file_poll,
1079 	.mmap		= relay_file_mmap,
1080 	.read		= relay_file_read,
1081 	.release	= relay_file_release,
1082 };
1083 EXPORT_SYMBOL_GPL(relay_file_operations);
1084