xref: /linux/kernel/relay.c (revision 52c996d3f40b40f87ef9dc80596903309682acc3)
1b86ff981SJens Axboe /*
2b86ff981SJens Axboe  * Public API and common code for kernel->userspace relay file support.
3b86ff981SJens Axboe  *
40c1bc6b8SMauro Carvalho Chehab  * See Documentation/filesystems/relay.rst for an overview.
5b86ff981SJens Axboe  *
6b86ff981SJens Axboe  * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
7b86ff981SJens Axboe  * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
8b86ff981SJens Axboe  *
9b86ff981SJens Axboe  * Moved to kernel/relay.c by Paul Mundt, 2006.
1023c88752SMathieu Desnoyers  * November 2006 - CPU hotplug support by Mathieu Desnoyers
1123c88752SMathieu Desnoyers  * 	(mathieu.desnoyers@polymtl.ca)
12b86ff981SJens Axboe  *
13b86ff981SJens Axboe  * This file is released under the GPL.
14b86ff981SJens Axboe  */
15b86ff981SJens Axboe #include <linux/errno.h>
16b86ff981SJens Axboe #include <linux/stddef.h>
17b86ff981SJens Axboe #include <linux/slab.h>
189984de1aSPaul Gortmaker #include <linux/export.h>
19b86ff981SJens Axboe #include <linux/string.h>
20b86ff981SJens Axboe #include <linux/relay.h>
21b86ff981SJens Axboe #include <linux/vmalloc.h>
22b86ff981SJens Axboe #include <linux/mm.h>
2323c88752SMathieu Desnoyers #include <linux/cpu.h>
24d6b29d7cSJens Axboe #include <linux/splice.h>
2523c88752SMathieu Desnoyers 
2623c88752SMathieu Desnoyers /* list of open channels, for cpu hotplug */
2723c88752SMathieu Desnoyers static DEFINE_MUTEX(relay_channels_mutex);
2823c88752SMathieu Desnoyers static LIST_HEAD(relay_channels);
29b86ff981SJens Axboe 
30b86ff981SJens Axboe /*
31a1e09612SNick Piggin  * fault() vm_op implementation for relay file mapping.
32b86ff981SJens Axboe  */
333fb3894bSSouptick Joarder static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
34b86ff981SJens Axboe {
35b86ff981SJens Axboe 	struct page *page;
3611bac800SDave Jiang 	struct rchan_buf *buf = vmf->vma->vm_private_data;
37a1e09612SNick Piggin 	pgoff_t pgoff = vmf->pgoff;
38b86ff981SJens Axboe 
39b86ff981SJens Axboe 	if (!buf)
40a1e09612SNick Piggin 		return VM_FAULT_OOM;
41b86ff981SJens Axboe 
42a1e09612SNick Piggin 	page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
43b86ff981SJens Axboe 	if (!page)
44a1e09612SNick Piggin 		return VM_FAULT_SIGBUS;
45b86ff981SJens Axboe 	get_page(page);
46a1e09612SNick Piggin 	vmf->page = page;
47b86ff981SJens Axboe 
48a1e09612SNick Piggin 	return 0;
49b86ff981SJens Axboe }
50b86ff981SJens Axboe 
51b86ff981SJens Axboe /*
52b86ff981SJens Axboe  * vm_ops for relay file mappings.
53b86ff981SJens Axboe  */
54f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct relay_file_mmap_ops = {
55a1e09612SNick Piggin 	.fault = relay_buf_fault,
56b86ff981SJens Axboe };
57b86ff981SJens Axboe 
5868ab3d88SMasami Hiramatsu /*
5968ab3d88SMasami Hiramatsu  * allocate an array of pointers of struct page
6068ab3d88SMasami Hiramatsu  */
6168ab3d88SMasami Hiramatsu static struct page **relay_alloc_page_array(unsigned int n_pages)
6268ab3d88SMasami Hiramatsu {
6383d87a4dSwuchi 	return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
6468ab3d88SMasami Hiramatsu }
6568ab3d88SMasami Hiramatsu 
6668ab3d88SMasami Hiramatsu /*
6768ab3d88SMasami Hiramatsu  * free an array of pointers of struct page
6868ab3d88SMasami Hiramatsu  */
6968ab3d88SMasami Hiramatsu static void relay_free_page_array(struct page **array)
7068ab3d88SMasami Hiramatsu {
71200f1ce3SPekka Enberg 	kvfree(array);
7268ab3d88SMasami Hiramatsu }
7368ab3d88SMasami Hiramatsu 
74b86ff981SJens Axboe /**
75b86ff981SJens Axboe  *	relay_mmap_buf: - mmap channel buffer to process address space
76b86ff981SJens Axboe  *	@buf: relay channel buffer
77b86ff981SJens Axboe  *	@vma: vm_area_struct describing memory to be mapped
78b86ff981SJens Axboe  *
79b86ff981SJens Axboe  *	Returns 0 if ok, negative on error
80b86ff981SJens Axboe  *
81c1e8d7c6SMichel Lespinasse  *	Caller should already have grabbed mmap_lock.
82b86ff981SJens Axboe  */
8301c55ed3SAdrian Bunk static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
84b86ff981SJens Axboe {
85b86ff981SJens Axboe 	unsigned long length = vma->vm_end - vma->vm_start;
86b86ff981SJens Axboe 
87b86ff981SJens Axboe 	if (!buf)
88b86ff981SJens Axboe 		return -EBADF;
89b86ff981SJens Axboe 
90b86ff981SJens Axboe 	if (length != (unsigned long)buf->chan->alloc_size)
91b86ff981SJens Axboe 		return -EINVAL;
92b86ff981SJens Axboe 
93b86ff981SJens Axboe 	vma->vm_ops = &relay_file_mmap_ops;
941c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_DONTEXPAND);
95b86ff981SJens Axboe 	vma->vm_private_data = buf;
96b86ff981SJens Axboe 
97b86ff981SJens Axboe 	return 0;
98b86ff981SJens Axboe }
99b86ff981SJens Axboe 
100b86ff981SJens Axboe /**
101b86ff981SJens Axboe  *	relay_alloc_buf - allocate a channel buffer
102b86ff981SJens Axboe  *	@buf: the buffer struct
103b86ff981SJens Axboe  *	@size: total size of the buffer
104b86ff981SJens Axboe  *
1054c78a663SRandy Dunlap  *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
106221415d7SJens Axboe  *	passed in size will get page aligned, if it isn't already.
107b86ff981SJens Axboe  */
108221415d7SJens Axboe static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
109b86ff981SJens Axboe {
110b86ff981SJens Axboe 	void *mem;
111b86ff981SJens Axboe 	unsigned int i, j, n_pages;
112b86ff981SJens Axboe 
113221415d7SJens Axboe 	*size = PAGE_ALIGN(*size);
114221415d7SJens Axboe 	n_pages = *size >> PAGE_SHIFT;
115b86ff981SJens Axboe 
11668ab3d88SMasami Hiramatsu 	buf->page_array = relay_alloc_page_array(n_pages);
117b86ff981SJens Axboe 	if (!buf->page_array)
118b86ff981SJens Axboe 		return NULL;
119b86ff981SJens Axboe 
120b86ff981SJens Axboe 	for (i = 0; i < n_pages; i++) {
121b86ff981SJens Axboe 		buf->page_array[i] = alloc_page(GFP_KERNEL);
122b86ff981SJens Axboe 		if (unlikely(!buf->page_array[i]))
123b86ff981SJens Axboe 			goto depopulate;
124ebf99093STom Zanussi 		set_page_private(buf->page_array[i], (unsigned long)buf);
125b86ff981SJens Axboe 	}
126b86ff981SJens Axboe 	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
127b86ff981SJens Axboe 	if (!mem)
128b86ff981SJens Axboe 		goto depopulate;
129b86ff981SJens Axboe 
130221415d7SJens Axboe 	memset(mem, 0, *size);
131b86ff981SJens Axboe 	buf->page_count = n_pages;
132b86ff981SJens Axboe 	return mem;
133b86ff981SJens Axboe 
134b86ff981SJens Axboe depopulate:
135b86ff981SJens Axboe 	for (j = 0; j < i; j++)
136b86ff981SJens Axboe 		__free_page(buf->page_array[j]);
13768ab3d88SMasami Hiramatsu 	relay_free_page_array(buf->page_array);
138b86ff981SJens Axboe 	return NULL;
139b86ff981SJens Axboe }
140b86ff981SJens Axboe 
141b86ff981SJens Axboe /**
142b86ff981SJens Axboe  *	relay_create_buf - allocate and initialize a channel buffer
1434c78a663SRandy Dunlap  *	@chan: the relay channel
144b86ff981SJens Axboe  *
1454c78a663SRandy Dunlap  *	Returns channel buffer if successful, %NULL otherwise.
146b86ff981SJens Axboe  */
14701c55ed3SAdrian Bunk static struct rchan_buf *relay_create_buf(struct rchan *chan)
148b86ff981SJens Axboe {
149f6302f1bSDan Carpenter 	struct rchan_buf *buf;
150f6302f1bSDan Carpenter 
1514d8586e0SGavrilov Ilia 	if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t))
152b86ff981SJens Axboe 		return NULL;
153b86ff981SJens Axboe 
154f6302f1bSDan Carpenter 	buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
155f6302f1bSDan Carpenter 	if (!buf)
156f6302f1bSDan Carpenter 		return NULL;
1574d8586e0SGavrilov Ilia 	buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t),
1586da2ec56SKees Cook 				     GFP_KERNEL);
159b86ff981SJens Axboe 	if (!buf->padding)
160b86ff981SJens Axboe 		goto free_buf;
161b86ff981SJens Axboe 
162221415d7SJens Axboe 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
163b86ff981SJens Axboe 	if (!buf->start)
164b86ff981SJens Axboe 		goto free_buf;
165b86ff981SJens Axboe 
166b86ff981SJens Axboe 	buf->chan = chan;
167b86ff981SJens Axboe 	kref_get(&buf->chan->kref);
168b86ff981SJens Axboe 	return buf;
169b86ff981SJens Axboe 
170b86ff981SJens Axboe free_buf:
171b86ff981SJens Axboe 	kfree(buf->padding);
172b86ff981SJens Axboe 	kfree(buf);
173b86ff981SJens Axboe 	return NULL;
174b86ff981SJens Axboe }
175b86ff981SJens Axboe 
176b86ff981SJens Axboe /**
177b86ff981SJens Axboe  *	relay_destroy_channel - free the channel struct
1784c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay channel
179b86ff981SJens Axboe  *
180b86ff981SJens Axboe  *	Should only be called from kref_put().
181b86ff981SJens Axboe  */
18201c55ed3SAdrian Bunk static void relay_destroy_channel(struct kref *kref)
183b86ff981SJens Axboe {
184b86ff981SJens Axboe 	struct rchan *chan = container_of(kref, struct rchan, kref);
18571e84329SWei Yongjun 	free_percpu(chan->buf);
186b86ff981SJens Axboe 	kfree(chan);
187b86ff981SJens Axboe }
188b86ff981SJens Axboe 
189b86ff981SJens Axboe /**
190b86ff981SJens Axboe  *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
191b86ff981SJens Axboe  *	@buf: the buffer struct
192b86ff981SJens Axboe  */
19301c55ed3SAdrian Bunk static void relay_destroy_buf(struct rchan_buf *buf)
194b86ff981SJens Axboe {
195b86ff981SJens Axboe 	struct rchan *chan = buf->chan;
196b86ff981SJens Axboe 	unsigned int i;
197b86ff981SJens Axboe 
198b86ff981SJens Axboe 	if (likely(buf->start)) {
199b86ff981SJens Axboe 		vunmap(buf->start);
200b86ff981SJens Axboe 		for (i = 0; i < buf->page_count; i++)
201b86ff981SJens Axboe 			__free_page(buf->page_array[i]);
20268ab3d88SMasami Hiramatsu 		relay_free_page_array(buf->page_array);
203b86ff981SJens Axboe 	}
204017c59c0SAkash Goel 	*per_cpu_ptr(chan->buf, buf->cpu) = NULL;
205b86ff981SJens Axboe 	kfree(buf->padding);
206b86ff981SJens Axboe 	kfree(buf);
207b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
208b86ff981SJens Axboe }
209b86ff981SJens Axboe 
210b86ff981SJens Axboe /**
211b86ff981SJens Axboe  *	relay_remove_buf - remove a channel buffer
2124c78a663SRandy Dunlap  *	@kref: target kernel reference that contains the relay buffer
213b86ff981SJens Axboe  *
214e227867fSMasanari Iida  *	Removes the file from the filesystem, which also frees the
215b86ff981SJens Axboe  *	rchan_buf_struct and the channel buffer.  Should only be called from
216b86ff981SJens Axboe  *	kref_put().
217b86ff981SJens Axboe  */
21801c55ed3SAdrian Bunk static void relay_remove_buf(struct kref *kref)
219b86ff981SJens Axboe {
220b86ff981SJens Axboe 	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
221b86ff981SJens Axboe 	relay_destroy_buf(buf);
222b86ff981SJens Axboe }
223b86ff981SJens Axboe 
224b86ff981SJens Axboe /**
225b86ff981SJens Axboe  *	relay_buf_empty - boolean, is the channel buffer empty?
226b86ff981SJens Axboe  *	@buf: channel buffer
227b86ff981SJens Axboe  *
228b86ff981SJens Axboe  *	Returns 1 if the buffer is empty, 0 otherwise.
229b86ff981SJens Axboe  */
23001c55ed3SAdrian Bunk static int relay_buf_empty(struct rchan_buf *buf)
231b86ff981SJens Axboe {
232b86ff981SJens Axboe 	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
233b86ff981SJens Axboe }
234b86ff981SJens Axboe 
235b86ff981SJens Axboe /**
236b86ff981SJens Axboe  *	relay_buf_full - boolean, is the channel buffer full?
237b86ff981SJens Axboe  *	@buf: channel buffer
238b86ff981SJens Axboe  *
239b86ff981SJens Axboe  *	Returns 1 if the buffer is full, 0 otherwise.
240b86ff981SJens Axboe  */
241b86ff981SJens Axboe int relay_buf_full(struct rchan_buf *buf)
242b86ff981SJens Axboe {
243b86ff981SJens Axboe 	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
244b86ff981SJens Axboe 	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
245b86ff981SJens Axboe }
246b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_buf_full);
247b86ff981SJens Axboe 
248b86ff981SJens Axboe /*
249b86ff981SJens Axboe  * High-level relay kernel API and associated functions.
250b86ff981SJens Axboe  */
251b86ff981SJens Axboe 
252023542f4SJani Nikula static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
253023542f4SJani Nikula 			      void *prev_subbuf, size_t prev_padding)
254b86ff981SJens Axboe {
255023542f4SJani Nikula 	if (!buf->chan->cb->subbuf_start)
256023542f4SJani Nikula 		return !relay_buf_full(buf);
257b86ff981SJens Axboe 
258023542f4SJani Nikula 	return buf->chan->cb->subbuf_start(buf, subbuf,
259023542f4SJani Nikula 					   prev_subbuf, prev_padding);
260b86ff981SJens Axboe }
261b86ff981SJens Axboe 
262b86ff981SJens Axboe /**
263b86ff981SJens Axboe  *	wakeup_readers - wake up readers waiting on a channel
26426b5679eSPeter Zijlstra  *	@work: contains the channel buffer
265b86ff981SJens Axboe  *
26626b5679eSPeter Zijlstra  *	This is the function used to defer reader waking
267b86ff981SJens Axboe  */
26826b5679eSPeter Zijlstra static void wakeup_readers(struct irq_work *work)
269b86ff981SJens Axboe {
27026b5679eSPeter Zijlstra 	struct rchan_buf *buf;
27126b5679eSPeter Zijlstra 
27226b5679eSPeter Zijlstra 	buf = container_of(work, struct rchan_buf, wakeup_work);
273b86ff981SJens Axboe 	wake_up_interruptible(&buf->read_wait);
274b86ff981SJens Axboe }
275b86ff981SJens Axboe 
276b86ff981SJens Axboe /**
277b86ff981SJens Axboe  *	__relay_reset - reset a channel buffer
278b86ff981SJens Axboe  *	@buf: the channel buffer
279b86ff981SJens Axboe  *	@init: 1 if this is a first-time initialization
280b86ff981SJens Axboe  *
28172fd4a35SRobert P. J. Day  *	See relay_reset() for description of effect.
282b86ff981SJens Axboe  */
283192636adSAndrew Morton static void __relay_reset(struct rchan_buf *buf, unsigned int init)
284b86ff981SJens Axboe {
285b86ff981SJens Axboe 	size_t i;
286b86ff981SJens Axboe 
287b86ff981SJens Axboe 	if (init) {
288b86ff981SJens Axboe 		init_waitqueue_head(&buf->read_wait);
289b86ff981SJens Axboe 		kref_init(&buf->kref);
29026b5679eSPeter Zijlstra 		init_irq_work(&buf->wakeup_work, wakeup_readers);
29126b5679eSPeter Zijlstra 	} else {
29226b5679eSPeter Zijlstra 		irq_work_sync(&buf->wakeup_work);
29326b5679eSPeter Zijlstra 	}
294b86ff981SJens Axboe 
295b86ff981SJens Axboe 	buf->subbufs_produced = 0;
296b86ff981SJens Axboe 	buf->subbufs_consumed = 0;
297b86ff981SJens Axboe 	buf->bytes_consumed = 0;
298b86ff981SJens Axboe 	buf->finalized = 0;
299b86ff981SJens Axboe 	buf->data = buf->start;
300b86ff981SJens Axboe 	buf->offset = 0;
301b86ff981SJens Axboe 
302b86ff981SJens Axboe 	for (i = 0; i < buf->chan->n_subbufs; i++)
303b86ff981SJens Axboe 		buf->padding[i] = 0;
304b86ff981SJens Axboe 
305023542f4SJani Nikula 	relay_subbuf_start(buf, buf->data, NULL, 0);
306b86ff981SJens Axboe }
307b86ff981SJens Axboe 
308b86ff981SJens Axboe /**
309b86ff981SJens Axboe  *	relay_reset - reset the channel
310b86ff981SJens Axboe  *	@chan: the channel
311b86ff981SJens Axboe  *
312b86ff981SJens Axboe  *	This has the effect of erasing all data from all channel buffers
313b86ff981SJens Axboe  *	and restarting the channel in its initial state.  The buffers
314b86ff981SJens Axboe  *	are not freed, so any mappings are still in effect.
315b86ff981SJens Axboe  *
31672fd4a35SRobert P. J. Day  *	NOTE. Care should be taken that the channel isn't actually
317b86ff981SJens Axboe  *	being used by anything when this call is made.
318b86ff981SJens Axboe  */
319b86ff981SJens Axboe void relay_reset(struct rchan *chan)
320b86ff981SJens Axboe {
321017c59c0SAkash Goel 	struct rchan_buf *buf;
322b86ff981SJens Axboe 	unsigned int i;
323b86ff981SJens Axboe 
324b86ff981SJens Axboe 	if (!chan)
325b86ff981SJens Axboe 		return;
326b86ff981SJens Axboe 
327017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
328017c59c0SAkash Goel 		__relay_reset(buf, 0);
32923c88752SMathieu Desnoyers 		return;
330b86ff981SJens Axboe 	}
33123c88752SMathieu Desnoyers 
33223c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
33398ba4031SLai Jiangshan 	for_each_possible_cpu(i)
334017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
335017c59c0SAkash Goel 			__relay_reset(buf, 0);
33623c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
337b86ff981SJens Axboe }
338b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_reset);
339b86ff981SJens Axboe 
34020d8b67cSEduard - Gabriel Munteanu static inline void relay_set_buf_dentry(struct rchan_buf *buf,
34120d8b67cSEduard - Gabriel Munteanu 					struct dentry *dentry)
34220d8b67cSEduard - Gabriel Munteanu {
34320d8b67cSEduard - Gabriel Munteanu 	buf->dentry = dentry;
3447682c918SDavid Howells 	d_inode(buf->dentry)->i_size = buf->early_bytes;
34520d8b67cSEduard - Gabriel Munteanu }
34620d8b67cSEduard - Gabriel Munteanu 
34720d8b67cSEduard - Gabriel Munteanu static struct dentry *relay_create_buf_file(struct rchan *chan,
34820d8b67cSEduard - Gabriel Munteanu 					    struct rchan_buf *buf,
34920d8b67cSEduard - Gabriel Munteanu 					    unsigned int cpu)
35020d8b67cSEduard - Gabriel Munteanu {
35120d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
35220d8b67cSEduard - Gabriel Munteanu 	char *tmpname;
35320d8b67cSEduard - Gabriel Munteanu 
35420d8b67cSEduard - Gabriel Munteanu 	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
35520d8b67cSEduard - Gabriel Munteanu 	if (!tmpname)
35620d8b67cSEduard - Gabriel Munteanu 		return NULL;
35720d8b67cSEduard - Gabriel Munteanu 	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
35820d8b67cSEduard - Gabriel Munteanu 
35920d8b67cSEduard - Gabriel Munteanu 	/* Create file in fs */
36020d8b67cSEduard - Gabriel Munteanu 	dentry = chan->cb->create_buf_file(tmpname, chan->parent,
36120d8b67cSEduard - Gabriel Munteanu 					   S_IRUSR, buf,
36220d8b67cSEduard - Gabriel Munteanu 					   &chan->is_global);
3632c1cf00eSGreg Kroah-Hartman 	if (IS_ERR(dentry))
3642c1cf00eSGreg Kroah-Hartman 		dentry = NULL;
36520d8b67cSEduard - Gabriel Munteanu 
36620d8b67cSEduard - Gabriel Munteanu 	kfree(tmpname);
36720d8b67cSEduard - Gabriel Munteanu 
36820d8b67cSEduard - Gabriel Munteanu 	return dentry;
36920d8b67cSEduard - Gabriel Munteanu }
37020d8b67cSEduard - Gabriel Munteanu 
3714c78a663SRandy Dunlap /*
372b86ff981SJens Axboe  *	relay_open_buf - create a new relay channel buffer
373b86ff981SJens Axboe  *
37423c88752SMathieu Desnoyers  *	used by relay_open() and CPU hotplug.
375b86ff981SJens Axboe  */
37623c88752SMathieu Desnoyers static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
377b86ff981SJens Axboe {
378*598f0046SLi kunyu 	struct rchan_buf *buf;
379b86ff981SJens Axboe 	struct dentry *dentry;
380b86ff981SJens Axboe 
38123c88752SMathieu Desnoyers  	if (chan->is_global)
382017c59c0SAkash Goel 		return *per_cpu_ptr(chan->buf, 0);
383b86ff981SJens Axboe 
384b86ff981SJens Axboe 	buf = relay_create_buf(chan);
385b86ff981SJens Axboe 	if (!buf)
38620d8b67cSEduard - Gabriel Munteanu 		return NULL;
38720d8b67cSEduard - Gabriel Munteanu 
38820d8b67cSEduard - Gabriel Munteanu 	if (chan->has_base_filename) {
38920d8b67cSEduard - Gabriel Munteanu 		dentry = relay_create_buf_file(chan, buf, cpu);
39020d8b67cSEduard - Gabriel Munteanu 		if (!dentry)
39120d8b67cSEduard - Gabriel Munteanu 			goto free_buf;
39220d8b67cSEduard - Gabriel Munteanu 		relay_set_buf_dentry(buf, dentry);
39359dbb2a0SAkash Goel 	} else {
39459dbb2a0SAkash Goel 		/* Only retrieve global info, nothing more, nothing less */
39559dbb2a0SAkash Goel 		dentry = chan->cb->create_buf_file(NULL, NULL,
39659dbb2a0SAkash Goel 						   S_IRUSR, buf,
39759dbb2a0SAkash Goel 						   &chan->is_global);
3982c1cf00eSGreg Kroah-Hartman 		if (IS_ERR_OR_NULL(dentry))
39959dbb2a0SAkash Goel 			goto free_buf;
40020d8b67cSEduard - Gabriel Munteanu 	}
401b86ff981SJens Axboe 
40223c88752SMathieu Desnoyers  	buf->cpu = cpu;
403b86ff981SJens Axboe  	__relay_reset(buf, 1);
404b86ff981SJens Axboe 
40523c88752SMathieu Desnoyers  	if(chan->is_global) {
406017c59c0SAkash Goel 		*per_cpu_ptr(chan->buf, 0) = buf;
40723c88752SMathieu Desnoyers  		buf->cpu = 0;
40823c88752SMathieu Desnoyers   	}
40923c88752SMathieu Desnoyers 
41020d8b67cSEduard - Gabriel Munteanu 	return buf;
41123c88752SMathieu Desnoyers 
41223c88752SMathieu Desnoyers free_buf:
41323c88752SMathieu Desnoyers  	relay_destroy_buf(buf);
41420d8b67cSEduard - Gabriel Munteanu 	return NULL;
415b86ff981SJens Axboe }
416b86ff981SJens Axboe 
417b86ff981SJens Axboe /**
418b86ff981SJens Axboe  *	relay_close_buf - close a channel buffer
419b86ff981SJens Axboe  *	@buf: channel buffer
420b86ff981SJens Axboe  *
421b86ff981SJens Axboe  *	Marks the buffer finalized and restores the default callbacks.
422b86ff981SJens Axboe  *	The channel buffer and channel buffer data structure are then freed
423b86ff981SJens Axboe  *	automatically when the last reference is given up.
424b86ff981SJens Axboe  */
425192636adSAndrew Morton static void relay_close_buf(struct rchan_buf *buf)
426b86ff981SJens Axboe {
427b86ff981SJens Axboe 	buf->finalized = 1;
42826b5679eSPeter Zijlstra 	irq_work_sync(&buf->wakeup_work);
429b8d4a5bfSDmitry Monakhov 	buf->chan->cb->remove_buf_file(buf->dentry);
430b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
431b86ff981SJens Axboe }
432b86ff981SJens Axboe 
433e6d4989aSRichard Weinberger int relay_prepare_cpu(unsigned int cpu)
43423c88752SMathieu Desnoyers {
43523c88752SMathieu Desnoyers 	struct rchan *chan;
436017c59c0SAkash Goel 	struct rchan_buf *buf;
43723c88752SMathieu Desnoyers 
43823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
43923c88752SMathieu Desnoyers 	list_for_each_entry(chan, &relay_channels, list) {
44047b7eae6SColin Ian King 		if (*per_cpu_ptr(chan->buf, cpu))
44123c88752SMathieu Desnoyers 			continue;
442e6d4989aSRichard Weinberger 		buf = relay_open_buf(chan, cpu);
443017c59c0SAkash Goel 		if (!buf) {
444e6d4989aSRichard Weinberger 			pr_err("relay: cpu %d buffer creation failed\n", cpu);
44523c88752SMathieu Desnoyers 			mutex_unlock(&relay_channels_mutex);
446e6d4989aSRichard Weinberger 			return -ENOMEM;
44723c88752SMathieu Desnoyers 		}
448e6d4989aSRichard Weinberger 		*per_cpu_ptr(chan->buf, cpu) = buf;
44923c88752SMathieu Desnoyers 	}
45023c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
451e6d4989aSRichard Weinberger 	return 0;
45223c88752SMathieu Desnoyers }
45323c88752SMathieu Desnoyers 
45423c88752SMathieu Desnoyers /**
455b86ff981SJens Axboe  *	relay_open - create a new relay channel
45620d8b67cSEduard - Gabriel Munteanu  *	@base_filename: base name of files to create, %NULL for buffering only
45720d8b67cSEduard - Gabriel Munteanu  *	@parent: dentry of parent directory, %NULL for root directory or buffer
458b86ff981SJens Axboe  *	@subbuf_size: size of sub-buffers
459b86ff981SJens Axboe  *	@n_subbufs: number of sub-buffers
460b86ff981SJens Axboe  *	@cb: client callback functions
46123c88752SMathieu Desnoyers  *	@private_data: user-defined data
462b86ff981SJens Axboe  *
4634c78a663SRandy Dunlap  *	Returns channel pointer if successful, %NULL otherwise.
464b86ff981SJens Axboe  *
465b86ff981SJens Axboe  *	Creates a channel buffer for each cpu using the sizes and
466b86ff981SJens Axboe  *	attributes specified.  The created channel buffer files
467b86ff981SJens Axboe  *	will be named base_filename0...base_filenameN-1.  File
46872fd4a35SRobert P. J. Day  *	permissions will be %S_IRUSR.
46959dbb2a0SAkash Goel  *
47059dbb2a0SAkash Goel  *	If opening a buffer (@parent = NULL) that you later wish to register
47159dbb2a0SAkash Goel  *	in a filesystem, call relay_late_setup_files() once the @parent dentry
47259dbb2a0SAkash Goel  *	is available.
473b86ff981SJens Axboe  */
474b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
475b86ff981SJens Axboe 			 struct dentry *parent,
476b86ff981SJens Axboe 			 size_t subbuf_size,
477b86ff981SJens Axboe 			 size_t n_subbufs,
478023542f4SJani Nikula 			 const struct rchan_callbacks *cb,
47923c88752SMathieu Desnoyers 			 void *private_data)
480b86ff981SJens Axboe {
481b86ff981SJens Axboe 	unsigned int i;
482b86ff981SJens Axboe 	struct rchan *chan;
483017c59c0SAkash Goel 	struct rchan_buf *buf;
484b86ff981SJens Axboe 
485b86ff981SJens Axboe 	if (!(subbuf_size && n_subbufs))
486b86ff981SJens Axboe 		return NULL;
487f6302f1bSDan Carpenter 	if (subbuf_size > UINT_MAX / n_subbufs)
488f6302f1bSDan Carpenter 		return NULL;
489371e0388SJani Nikula 	if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
4906f8f2544SJani Nikula 		return NULL;
491b86ff981SJens Axboe 
492cd861280SRobert P. J. Day 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
493b86ff981SJens Axboe 	if (!chan)
494b86ff981SJens Axboe 		return NULL;
495b86ff981SJens Axboe 
496017c59c0SAkash Goel 	chan->buf = alloc_percpu(struct rchan_buf *);
49754e200abSDaniel Axtens 	if (!chan->buf) {
49854e200abSDaniel Axtens 		kfree(chan);
49954e200abSDaniel Axtens 		return NULL;
50054e200abSDaniel Axtens 	}
50154e200abSDaniel Axtens 
502b86ff981SJens Axboe 	chan->version = RELAYFS_CHANNEL_VERSION;
503b86ff981SJens Axboe 	chan->n_subbufs = n_subbufs;
504b86ff981SJens Axboe 	chan->subbuf_size = subbuf_size;
505a05342cbSzhangwei(Jovi) 	chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
50623c88752SMathieu Desnoyers 	chan->parent = parent;
50723c88752SMathieu Desnoyers 	chan->private_data = private_data;
50820d8b67cSEduard - Gabriel Munteanu 	if (base_filename) {
50920d8b67cSEduard - Gabriel Munteanu 		chan->has_base_filename = 1;
5103f0dad01SXu Panda 		strscpy(chan->base_filename, base_filename, NAME_MAX);
51120d8b67cSEduard - Gabriel Munteanu 	}
512023542f4SJani Nikula 	chan->cb = cb;
513b86ff981SJens Axboe 	kref_init(&chan->kref);
514b86ff981SJens Axboe 
51523c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
516b86ff981SJens Axboe 	for_each_online_cpu(i) {
517017c59c0SAkash Goel 		buf = relay_open_buf(chan, i);
518017c59c0SAkash Goel 		if (!buf)
519b86ff981SJens Axboe 			goto free_bufs;
520017c59c0SAkash Goel 		*per_cpu_ptr(chan->buf, i) = buf;
521b86ff981SJens Axboe 	}
52223c88752SMathieu Desnoyers 	list_add(&chan->list, &relay_channels);
52323c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
524b86ff981SJens Axboe 
525b86ff981SJens Axboe 	return chan;
526b86ff981SJens Axboe 
527b86ff981SJens Axboe free_bufs:
52898ba4031SLai Jiangshan 	for_each_possible_cpu(i) {
529017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
530017c59c0SAkash Goel 			relay_close_buf(buf);
531b86ff981SJens Axboe 	}
532b86ff981SJens Axboe 
533b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
53423c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
535b86ff981SJens Axboe 	return NULL;
536b86ff981SJens Axboe }
537b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_open);
538b86ff981SJens Axboe 
53920d8b67cSEduard - Gabriel Munteanu struct rchan_percpu_buf_dispatcher {
54020d8b67cSEduard - Gabriel Munteanu 	struct rchan_buf *buf;
54120d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
54220d8b67cSEduard - Gabriel Munteanu };
54320d8b67cSEduard - Gabriel Munteanu 
54420d8b67cSEduard - Gabriel Munteanu /* Called in atomic context. */
54520d8b67cSEduard - Gabriel Munteanu static void __relay_set_buf_dentry(void *info)
54620d8b67cSEduard - Gabriel Munteanu {
54720d8b67cSEduard - Gabriel Munteanu 	struct rchan_percpu_buf_dispatcher *p = info;
54820d8b67cSEduard - Gabriel Munteanu 
54920d8b67cSEduard - Gabriel Munteanu 	relay_set_buf_dentry(p->buf, p->dentry);
55020d8b67cSEduard - Gabriel Munteanu }
55120d8b67cSEduard - Gabriel Munteanu 
55220d8b67cSEduard - Gabriel Munteanu /**
55320d8b67cSEduard - Gabriel Munteanu  *	relay_late_setup_files - triggers file creation
55420d8b67cSEduard - Gabriel Munteanu  *	@chan: channel to operate on
55520d8b67cSEduard - Gabriel Munteanu  *	@base_filename: base name of files to create
55620d8b67cSEduard - Gabriel Munteanu  *	@parent: dentry of parent directory, %NULL for root directory
55720d8b67cSEduard - Gabriel Munteanu  *
55820d8b67cSEduard - Gabriel Munteanu  *	Returns 0 if successful, non-zero otherwise.
55920d8b67cSEduard - Gabriel Munteanu  *
56059dbb2a0SAkash Goel  *	Use to setup files for a previously buffer-only channel created
56159dbb2a0SAkash Goel  *	by relay_open() with a NULL parent dentry.
56259dbb2a0SAkash Goel  *
56359dbb2a0SAkash Goel  *	For example, this is useful for perfomring early tracing in kernel,
56459dbb2a0SAkash Goel  *	before VFS is up and then exposing the early results once the dentry
56559dbb2a0SAkash Goel  *	is available.
56620d8b67cSEduard - Gabriel Munteanu  */
56720d8b67cSEduard - Gabriel Munteanu int relay_late_setup_files(struct rchan *chan,
56820d8b67cSEduard - Gabriel Munteanu 			   const char *base_filename,
56920d8b67cSEduard - Gabriel Munteanu 			   struct dentry *parent)
57020d8b67cSEduard - Gabriel Munteanu {
57120d8b67cSEduard - Gabriel Munteanu 	int err = 0;
57220d8b67cSEduard - Gabriel Munteanu 	unsigned int i, curr_cpu;
57320d8b67cSEduard - Gabriel Munteanu 	unsigned long flags;
57420d8b67cSEduard - Gabriel Munteanu 	struct dentry *dentry;
575017c59c0SAkash Goel 	struct rchan_buf *buf;
57620d8b67cSEduard - Gabriel Munteanu 	struct rchan_percpu_buf_dispatcher disp;
57720d8b67cSEduard - Gabriel Munteanu 
57820d8b67cSEduard - Gabriel Munteanu 	if (!chan || !base_filename)
57920d8b67cSEduard - Gabriel Munteanu 		return -EINVAL;
58020d8b67cSEduard - Gabriel Munteanu 
5813f0dad01SXu Panda 	strscpy(chan->base_filename, base_filename, NAME_MAX);
58220d8b67cSEduard - Gabriel Munteanu 
58320d8b67cSEduard - Gabriel Munteanu 	mutex_lock(&relay_channels_mutex);
58420d8b67cSEduard - Gabriel Munteanu 	/* Is chan already set up? */
585b786c6a9SJiri Slaby 	if (unlikely(chan->has_base_filename)) {
586b786c6a9SJiri Slaby 		mutex_unlock(&relay_channels_mutex);
58720d8b67cSEduard - Gabriel Munteanu 		return -EEXIST;
588b786c6a9SJiri Slaby 	}
58920d8b67cSEduard - Gabriel Munteanu 	chan->has_base_filename = 1;
59020d8b67cSEduard - Gabriel Munteanu 	chan->parent = parent;
59159dbb2a0SAkash Goel 
59259dbb2a0SAkash Goel 	if (chan->is_global) {
59359dbb2a0SAkash Goel 		err = -EINVAL;
594017c59c0SAkash Goel 		buf = *per_cpu_ptr(chan->buf, 0);
595017c59c0SAkash Goel 		if (!WARN_ON_ONCE(!buf)) {
596017c59c0SAkash Goel 			dentry = relay_create_buf_file(chan, buf, 0);
59759dbb2a0SAkash Goel 			if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
598017c59c0SAkash Goel 				relay_set_buf_dentry(buf, dentry);
59959dbb2a0SAkash Goel 				err = 0;
60059dbb2a0SAkash Goel 			}
60159dbb2a0SAkash Goel 		}
60259dbb2a0SAkash Goel 		mutex_unlock(&relay_channels_mutex);
60359dbb2a0SAkash Goel 		return err;
60459dbb2a0SAkash Goel 	}
60559dbb2a0SAkash Goel 
60620d8b67cSEduard - Gabriel Munteanu 	curr_cpu = get_cpu();
60720d8b67cSEduard - Gabriel Munteanu 	/*
60820d8b67cSEduard - Gabriel Munteanu 	 * The CPU hotplug notifier ran before us and created buffers with
60920d8b67cSEduard - Gabriel Munteanu 	 * no files associated. So it's safe to call relay_setup_buf_file()
61020d8b67cSEduard - Gabriel Munteanu 	 * on all currently online CPUs.
61120d8b67cSEduard - Gabriel Munteanu 	 */
61220d8b67cSEduard - Gabriel Munteanu 	for_each_online_cpu(i) {
613017c59c0SAkash Goel 		buf = *per_cpu_ptr(chan->buf, i);
614017c59c0SAkash Goel 		if (unlikely(!buf)) {
6157a51cffbSIngo Molnar 			WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
61620d8b67cSEduard - Gabriel Munteanu 			err = -EINVAL;
61720d8b67cSEduard - Gabriel Munteanu 			break;
61820d8b67cSEduard - Gabriel Munteanu 		}
61920d8b67cSEduard - Gabriel Munteanu 
620017c59c0SAkash Goel 		dentry = relay_create_buf_file(chan, buf, i);
62120d8b67cSEduard - Gabriel Munteanu 		if (unlikely(!dentry)) {
62220d8b67cSEduard - Gabriel Munteanu 			err = -EINVAL;
62320d8b67cSEduard - Gabriel Munteanu 			break;
62420d8b67cSEduard - Gabriel Munteanu 		}
62520d8b67cSEduard - Gabriel Munteanu 
62620d8b67cSEduard - Gabriel Munteanu 		if (curr_cpu == i) {
62720d8b67cSEduard - Gabriel Munteanu 			local_irq_save(flags);
628017c59c0SAkash Goel 			relay_set_buf_dentry(buf, dentry);
62920d8b67cSEduard - Gabriel Munteanu 			local_irq_restore(flags);
63020d8b67cSEduard - Gabriel Munteanu 		} else {
631017c59c0SAkash Goel 			disp.buf = buf;
63220d8b67cSEduard - Gabriel Munteanu 			disp.dentry = dentry;
63320d8b67cSEduard - Gabriel Munteanu 			smp_mb();
63420d8b67cSEduard - Gabriel Munteanu 			/* relay_channels_mutex must be held, so wait. */
63520d8b67cSEduard - Gabriel Munteanu 			err = smp_call_function_single(i,
63620d8b67cSEduard - Gabriel Munteanu 						       __relay_set_buf_dentry,
63720d8b67cSEduard - Gabriel Munteanu 						       &disp, 1);
63820d8b67cSEduard - Gabriel Munteanu 		}
63920d8b67cSEduard - Gabriel Munteanu 		if (unlikely(err))
64020d8b67cSEduard - Gabriel Munteanu 			break;
64120d8b67cSEduard - Gabriel Munteanu 	}
64220d8b67cSEduard - Gabriel Munteanu 	put_cpu();
64320d8b67cSEduard - Gabriel Munteanu 	mutex_unlock(&relay_channels_mutex);
64420d8b67cSEduard - Gabriel Munteanu 
64520d8b67cSEduard - Gabriel Munteanu 	return err;
64620d8b67cSEduard - Gabriel Munteanu }
64759dbb2a0SAkash Goel EXPORT_SYMBOL_GPL(relay_late_setup_files);
64820d8b67cSEduard - Gabriel Munteanu 
649b86ff981SJens Axboe /**
650b86ff981SJens Axboe  *	relay_switch_subbuf - switch to a new sub-buffer
651b86ff981SJens Axboe  *	@buf: channel buffer
652b86ff981SJens Axboe  *	@length: size of current event
653b86ff981SJens Axboe  *
654b86ff981SJens Axboe  *	Returns either the length passed in or 0 if full.
655b86ff981SJens Axboe  *
656b86ff981SJens Axboe  *	Performs sub-buffer-switch tasks such as invoking callbacks,
657b86ff981SJens Axboe  *	updating padding counts, waking up readers, etc.
658b86ff981SJens Axboe  */
659b86ff981SJens Axboe size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
660b86ff981SJens Axboe {
661b86ff981SJens Axboe 	void *old, *new;
662b86ff981SJens Axboe 	size_t old_subbuf, new_subbuf;
663b86ff981SJens Axboe 
664b86ff981SJens Axboe 	if (unlikely(length > buf->chan->subbuf_size))
665b86ff981SJens Axboe 		goto toobig;
666b86ff981SJens Axboe 
667b86ff981SJens Axboe 	if (buf->offset != buf->chan->subbuf_size + 1) {
668b86ff981SJens Axboe 		buf->prev_padding = buf->chan->subbuf_size - buf->offset;
669b86ff981SJens Axboe 		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
670b86ff981SJens Axboe 		buf->padding[old_subbuf] = buf->prev_padding;
671b86ff981SJens Axboe 		buf->subbufs_produced++;
67220d8b67cSEduard - Gabriel Munteanu 		if (buf->dentry)
6737682c918SDavid Howells 			d_inode(buf->dentry)->i_size +=
67420d8b67cSEduard - Gabriel Munteanu 				buf->chan->subbuf_size -
67520d8b67cSEduard - Gabriel Munteanu 				buf->padding[old_subbuf];
67620d8b67cSEduard - Gabriel Munteanu 		else
67720d8b67cSEduard - Gabriel Munteanu 			buf->early_bytes += buf->chan->subbuf_size -
678221415d7SJens Axboe 					    buf->padding[old_subbuf];
679221415d7SJens Axboe 		smp_mb();
68026b5679eSPeter Zijlstra 		if (waitqueue_active(&buf->read_wait)) {
6817c9cb383STom Zanussi 			/*
6827c9cb383STom Zanussi 			 * Calling wake_up_interruptible() from here
6837c9cb383STom Zanussi 			 * will deadlock if we happen to be logging
6847c9cb383STom Zanussi 			 * from the scheduler (trying to re-grab
6857c9cb383STom Zanussi 			 * rq->lock), so defer it.
6867c9cb383STom Zanussi 			 */
68726b5679eSPeter Zijlstra 			irq_work_queue(&buf->wakeup_work);
68826b5679eSPeter Zijlstra 		}
689b86ff981SJens Axboe 	}
690b86ff981SJens Axboe 
691b86ff981SJens Axboe 	old = buf->data;
692b86ff981SJens Axboe 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
693b86ff981SJens Axboe 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
694b86ff981SJens Axboe 	buf->offset = 0;
695023542f4SJani Nikula 	if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
696b86ff981SJens Axboe 		buf->offset = buf->chan->subbuf_size + 1;
697b86ff981SJens Axboe 		return 0;
698b86ff981SJens Axboe 	}
699b86ff981SJens Axboe 	buf->data = new;
700b86ff981SJens Axboe 	buf->padding[new_subbuf] = 0;
701b86ff981SJens Axboe 
702b86ff981SJens Axboe 	if (unlikely(length + buf->offset > buf->chan->subbuf_size))
703b86ff981SJens Axboe 		goto toobig;
704b86ff981SJens Axboe 
705b86ff981SJens Axboe 	return length;
706b86ff981SJens Axboe 
707b86ff981SJens Axboe toobig:
708b86ff981SJens Axboe 	buf->chan->last_toobig = length;
709b86ff981SJens Axboe 	return 0;
710b86ff981SJens Axboe }
711b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_switch_subbuf);
712b86ff981SJens Axboe 
713b86ff981SJens Axboe /**
714b86ff981SJens Axboe  *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
715b86ff981SJens Axboe  *	@chan: the channel
716b86ff981SJens Axboe  *	@cpu: the cpu associated with the channel buffer to update
717b86ff981SJens Axboe  *	@subbufs_consumed: number of sub-buffers to add to current buf's count
718b86ff981SJens Axboe  *
719b86ff981SJens Axboe  *	Adds to the channel buffer's consumed sub-buffer count.
720b86ff981SJens Axboe  *	subbufs_consumed should be the number of sub-buffers newly consumed,
721b86ff981SJens Axboe  *	not the total consumed.
722b86ff981SJens Axboe  *
72372fd4a35SRobert P. J. Day  *	NOTE. Kernel clients don't need to call this function if the channel
724b86ff981SJens Axboe  *	mode is 'overwrite'.
725b86ff981SJens Axboe  */
726b86ff981SJens Axboe void relay_subbufs_consumed(struct rchan *chan,
727b86ff981SJens Axboe 			    unsigned int cpu,
728b86ff981SJens Axboe 			    size_t subbufs_consumed)
729b86ff981SJens Axboe {
730b86ff981SJens Axboe 	struct rchan_buf *buf;
731b86ff981SJens Axboe 
7329a29d0fbSDan Carpenter 	if (!chan || cpu >= NR_CPUS)
733b86ff981SJens Axboe 		return;
734b86ff981SJens Axboe 
735017c59c0SAkash Goel 	buf = *per_cpu_ptr(chan->buf, cpu);
7369a29d0fbSDan Carpenter 	if (!buf || subbufs_consumed > chan->n_subbufs)
737b86ff981SJens Axboe 		return;
738b86ff981SJens Axboe 
7392c53d910SAravind Srinivasan 	if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
740b86ff981SJens Axboe 		buf->subbufs_consumed = buf->subbufs_produced;
7412c53d910SAravind Srinivasan 	else
7422c53d910SAravind Srinivasan 		buf->subbufs_consumed += subbufs_consumed;
743b86ff981SJens Axboe }
744b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
745b86ff981SJens Axboe 
746b86ff981SJens Axboe /**
747b86ff981SJens Axboe  *	relay_close - close the channel
748b86ff981SJens Axboe  *	@chan: the channel
749b86ff981SJens Axboe  *
750b86ff981SJens Axboe  *	Closes all channel buffers and frees the channel.
751b86ff981SJens Axboe  */
752b86ff981SJens Axboe void relay_close(struct rchan *chan)
753b86ff981SJens Axboe {
754017c59c0SAkash Goel 	struct rchan_buf *buf;
755b86ff981SJens Axboe 	unsigned int i;
756b86ff981SJens Axboe 
757b86ff981SJens Axboe 	if (!chan)
758b86ff981SJens Axboe 		return;
759b86ff981SJens Axboe 
76023c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
761017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
762017c59c0SAkash Goel 		relay_close_buf(buf);
76323c88752SMathieu Desnoyers 	else
76423c88752SMathieu Desnoyers 		for_each_possible_cpu(i)
765017c59c0SAkash Goel 			if ((buf = *per_cpu_ptr(chan->buf, i)))
766017c59c0SAkash Goel 				relay_close_buf(buf);
767b86ff981SJens Axboe 
768b86ff981SJens Axboe 	if (chan->last_toobig)
769b86ff981SJens Axboe 		printk(KERN_WARNING "relay: one or more items not logged "
7705b5e0928SAlexey Dobriyan 		       "[item size (%zd) > sub-buffer size (%zd)]\n",
771b86ff981SJens Axboe 		       chan->last_toobig, chan->subbuf_size);
772b86ff981SJens Axboe 
77323c88752SMathieu Desnoyers 	list_del(&chan->list);
774b86ff981SJens Axboe 	kref_put(&chan->kref, relay_destroy_channel);
77523c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
776b86ff981SJens Axboe }
777b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_close);
778b86ff981SJens Axboe 
779b86ff981SJens Axboe /**
780b86ff981SJens Axboe  *	relay_flush - close the channel
781b86ff981SJens Axboe  *	@chan: the channel
782b86ff981SJens Axboe  *
7834c78a663SRandy Dunlap  *	Flushes all channel buffers, i.e. forces buffer switch.
784b86ff981SJens Axboe  */
785b86ff981SJens Axboe void relay_flush(struct rchan *chan)
786b86ff981SJens Axboe {
787017c59c0SAkash Goel 	struct rchan_buf *buf;
788b86ff981SJens Axboe 	unsigned int i;
789b86ff981SJens Axboe 
790b86ff981SJens Axboe 	if (!chan)
791b86ff981SJens Axboe 		return;
792b86ff981SJens Axboe 
793017c59c0SAkash Goel 	if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
794017c59c0SAkash Goel 		relay_switch_subbuf(buf, 0);
79523c88752SMathieu Desnoyers 		return;
796b86ff981SJens Axboe 	}
79723c88752SMathieu Desnoyers 
79823c88752SMathieu Desnoyers 	mutex_lock(&relay_channels_mutex);
79923c88752SMathieu Desnoyers 	for_each_possible_cpu(i)
800017c59c0SAkash Goel 		if ((buf = *per_cpu_ptr(chan->buf, i)))
801017c59c0SAkash Goel 			relay_switch_subbuf(buf, 0);
80223c88752SMathieu Desnoyers 	mutex_unlock(&relay_channels_mutex);
803b86ff981SJens Axboe }
804b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_flush);
805b86ff981SJens Axboe 
806b86ff981SJens Axboe /**
807b86ff981SJens Axboe  *	relay_file_open - open file op for relay files
808b86ff981SJens Axboe  *	@inode: the inode
809b86ff981SJens Axboe  *	@filp: the file
810b86ff981SJens Axboe  *
811b86ff981SJens Axboe  *	Increments the channel buffer refcount.
812b86ff981SJens Axboe  */
813b86ff981SJens Axboe static int relay_file_open(struct inode *inode, struct file *filp)
814b86ff981SJens Axboe {
8158e18e294STheodore Ts'o 	struct rchan_buf *buf = inode->i_private;
816b86ff981SJens Axboe 	kref_get(&buf->kref);
817b86ff981SJens Axboe 	filp->private_data = buf;
818b86ff981SJens Axboe 
81937529fe9SLai Jiangshan 	return nonseekable_open(inode, filp);
820b86ff981SJens Axboe }
821b86ff981SJens Axboe 
822b86ff981SJens Axboe /**
823b86ff981SJens Axboe  *	relay_file_mmap - mmap file op for relay files
824b86ff981SJens Axboe  *	@filp: the file
825b86ff981SJens Axboe  *	@vma: the vma describing what to map
826b86ff981SJens Axboe  *
82772fd4a35SRobert P. J. Day  *	Calls upon relay_mmap_buf() to map the file into user space.
828b86ff981SJens Axboe  */
829b86ff981SJens Axboe static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
830b86ff981SJens Axboe {
831b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
832b86ff981SJens Axboe 	return relay_mmap_buf(buf, vma);
833b86ff981SJens Axboe }
834b86ff981SJens Axboe 
835b86ff981SJens Axboe /**
836b86ff981SJens Axboe  *	relay_file_poll - poll file op for relay files
837b86ff981SJens Axboe  *	@filp: the file
838b86ff981SJens Axboe  *	@wait: poll table
839b86ff981SJens Axboe  *
840b86ff981SJens Axboe  *	Poll implemention.
841b86ff981SJens Axboe  */
8429dd95748SAl Viro static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
843b86ff981SJens Axboe {
8449dd95748SAl Viro 	__poll_t mask = 0;
845b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
846b86ff981SJens Axboe 
847b86ff981SJens Axboe 	if (buf->finalized)
848a9a08845SLinus Torvalds 		return EPOLLERR;
849b86ff981SJens Axboe 
850b86ff981SJens Axboe 	if (filp->f_mode & FMODE_READ) {
851b86ff981SJens Axboe 		poll_wait(filp, &buf->read_wait, wait);
852b86ff981SJens Axboe 		if (!relay_buf_empty(buf))
853a9a08845SLinus Torvalds 			mask |= EPOLLIN | EPOLLRDNORM;
854b86ff981SJens Axboe 	}
855b86ff981SJens Axboe 
856b86ff981SJens Axboe 	return mask;
857b86ff981SJens Axboe }
858b86ff981SJens Axboe 
859b86ff981SJens Axboe /**
860b86ff981SJens Axboe  *	relay_file_release - release file op for relay files
861b86ff981SJens Axboe  *	@inode: the inode
862b86ff981SJens Axboe  *	@filp: the file
863b86ff981SJens Axboe  *
864b86ff981SJens Axboe  *	Decrements the channel refcount, as the filesystem is
865b86ff981SJens Axboe  *	no longer using it.
866b86ff981SJens Axboe  */
867b86ff981SJens Axboe static int relay_file_release(struct inode *inode, struct file *filp)
868b86ff981SJens Axboe {
869b86ff981SJens Axboe 	struct rchan_buf *buf = filp->private_data;
870b86ff981SJens Axboe 	kref_put(&buf->kref, relay_remove_buf);
871b86ff981SJens Axboe 
872b86ff981SJens Axboe 	return 0;
873b86ff981SJens Axboe }
874b86ff981SJens Axboe 
8754c78a663SRandy Dunlap /*
876b86ff981SJens Axboe  *	relay_file_read_consume - update the consumed count for the buffer
877b86ff981SJens Axboe  */
878b86ff981SJens Axboe static void relay_file_read_consume(struct rchan_buf *buf,
879b86ff981SJens Axboe 				    size_t read_pos,
880b86ff981SJens Axboe 				    size_t bytes_consumed)
881b86ff981SJens Axboe {
882b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
883b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
884b86ff981SJens Axboe 	size_t read_subbuf;
885b86ff981SJens Axboe 
88632194450STom Zanussi 	if (buf->subbufs_produced == buf->subbufs_consumed &&
88732194450STom Zanussi 	    buf->offset == buf->bytes_consumed)
88832194450STom Zanussi 		return;
88932194450STom Zanussi 
890b86ff981SJens Axboe 	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
891b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
892b86ff981SJens Axboe 		buf->bytes_consumed = 0;
893b86ff981SJens Axboe 	}
894b86ff981SJens Axboe 
895b86ff981SJens Axboe 	buf->bytes_consumed += bytes_consumed;
896a66e356cSMasami Hiramatsu 	if (!read_pos)
897a66e356cSMasami Hiramatsu 		read_subbuf = buf->subbufs_consumed % n_subbufs;
898a66e356cSMasami Hiramatsu 	else
899b86ff981SJens Axboe 		read_subbuf = read_pos / buf->chan->subbuf_size;
900b86ff981SJens Axboe 	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
901b86ff981SJens Axboe 		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
902b86ff981SJens Axboe 		    (buf->offset == subbuf_size))
903b86ff981SJens Axboe 			return;
904b86ff981SJens Axboe 		relay_subbufs_consumed(buf->chan, buf->cpu, 1);
905b86ff981SJens Axboe 		buf->bytes_consumed = 0;
906b86ff981SJens Axboe 	}
907b86ff981SJens Axboe }
908b86ff981SJens Axboe 
9094c78a663SRandy Dunlap /*
910b86ff981SJens Axboe  *	relay_file_read_avail - boolean, are there unconsumed bytes available?
911b86ff981SJens Axboe  */
912341a7213SPengcheng Yang static int relay_file_read_avail(struct rchan_buf *buf)
913b86ff981SJens Axboe {
914b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
915b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
916221415d7SJens Axboe 	size_t produced = buf->subbufs_produced;
917ac05b7a1SSudip Mukherjee 	size_t consumed;
918b86ff981SJens Axboe 
919341a7213SPengcheng Yang 	relay_file_read_consume(buf, 0, 0);
920b86ff981SJens Axboe 
92132194450STom Zanussi 	consumed = buf->subbufs_consumed;
92232194450STom Zanussi 
923221415d7SJens Axboe 	if (unlikely(buf->offset > subbuf_size)) {
924221415d7SJens Axboe 		if (produced == consumed)
925221415d7SJens Axboe 			return 0;
926221415d7SJens Axboe 		return 1;
927221415d7SJens Axboe 	}
928221415d7SJens Axboe 
929221415d7SJens Axboe 	if (unlikely(produced - consumed >= n_subbufs)) {
930a66e356cSMasami Hiramatsu 		consumed = produced - n_subbufs + 1;
931221415d7SJens Axboe 		buf->subbufs_consumed = consumed;
932a66e356cSMasami Hiramatsu 		buf->bytes_consumed = 0;
933221415d7SJens Axboe 	}
934221415d7SJens Axboe 
935221415d7SJens Axboe 	produced = (produced % n_subbufs) * subbuf_size + buf->offset;
936221415d7SJens Axboe 	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
937221415d7SJens Axboe 
938221415d7SJens Axboe 	if (consumed > produced)
939221415d7SJens Axboe 		produced += n_subbufs * subbuf_size;
940221415d7SJens Axboe 
94132194450STom Zanussi 	if (consumed == produced) {
94232194450STom Zanussi 		if (buf->offset == subbuf_size &&
94332194450STom Zanussi 		    buf->subbufs_produced > buf->subbufs_consumed)
94432194450STom Zanussi 			return 1;
945221415d7SJens Axboe 		return 0;
94632194450STom Zanussi 	}
947221415d7SJens Axboe 
948b86ff981SJens Axboe 	return 1;
949b86ff981SJens Axboe }
950b86ff981SJens Axboe 
951b86ff981SJens Axboe /**
952b86ff981SJens Axboe  *	relay_file_read_subbuf_avail - return bytes available in sub-buffer
9534c78a663SRandy Dunlap  *	@read_pos: file read position
9544c78a663SRandy Dunlap  *	@buf: relay channel buffer
955b86ff981SJens Axboe  */
956b86ff981SJens Axboe static size_t relay_file_read_subbuf_avail(size_t read_pos,
957b86ff981SJens Axboe 					   struct rchan_buf *buf)
958b86ff981SJens Axboe {
959b86ff981SJens Axboe 	size_t padding, avail = 0;
960b86ff981SJens Axboe 	size_t read_subbuf, read_offset, write_subbuf, write_offset;
961b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
962b86ff981SJens Axboe 
963b86ff981SJens Axboe 	write_subbuf = (buf->data - buf->start) / subbuf_size;
964b86ff981SJens Axboe 	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
965b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
966b86ff981SJens Axboe 	read_offset = read_pos % subbuf_size;
967b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
968b86ff981SJens Axboe 
969b86ff981SJens Axboe 	if (read_subbuf == write_subbuf) {
970b86ff981SJens Axboe 		if (read_offset + padding < write_offset)
971b86ff981SJens Axboe 			avail = write_offset - (read_offset + padding);
972b86ff981SJens Axboe 	} else
973b86ff981SJens Axboe 		avail = (subbuf_size - padding) - read_offset;
974b86ff981SJens Axboe 
975b86ff981SJens Axboe 	return avail;
976b86ff981SJens Axboe }
977b86ff981SJens Axboe 
978b86ff981SJens Axboe /**
979b86ff981SJens Axboe  *	relay_file_read_start_pos - find the first available byte to read
9804c78a663SRandy Dunlap  *	@buf: relay channel buffer
981b86ff981SJens Axboe  *
982341a7213SPengcheng Yang  *	If the read_pos is in the middle of padding, return the
983b86ff981SJens Axboe  *	position of the first actually available byte, otherwise
984b86ff981SJens Axboe  *	return the original value.
985b86ff981SJens Axboe  */
986341a7213SPengcheng Yang static size_t relay_file_read_start_pos(struct rchan_buf *buf)
987b86ff981SJens Axboe {
988b86ff981SJens Axboe 	size_t read_subbuf, padding, padding_start, padding_end;
989b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
990b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
9918d62fdebSDavid Wilder 	size_t consumed = buf->subbufs_consumed % n_subbufs;
99243ec16f1SZhang Zhengming 	size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed)
99343ec16f1SZhang Zhengming 			% (n_subbufs * subbuf_size);
994b86ff981SJens Axboe 
995b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
996b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
997b86ff981SJens Axboe 	padding_start = (read_subbuf + 1) * subbuf_size - padding;
998b86ff981SJens Axboe 	padding_end = (read_subbuf + 1) * subbuf_size;
999b86ff981SJens Axboe 	if (read_pos >= padding_start && read_pos < padding_end) {
1000b86ff981SJens Axboe 		read_subbuf = (read_subbuf + 1) % n_subbufs;
1001b86ff981SJens Axboe 		read_pos = read_subbuf * subbuf_size;
1002b86ff981SJens Axboe 	}
1003b86ff981SJens Axboe 
1004b86ff981SJens Axboe 	return read_pos;
1005b86ff981SJens Axboe }
1006b86ff981SJens Axboe 
1007b86ff981SJens Axboe /**
1008b86ff981SJens Axboe  *	relay_file_read_end_pos - return the new read position
10094c78a663SRandy Dunlap  *	@read_pos: file read position
10104c78a663SRandy Dunlap  *	@buf: relay channel buffer
10114c78a663SRandy Dunlap  *	@count: number of bytes to be read
1012b86ff981SJens Axboe  */
1013b86ff981SJens Axboe static size_t relay_file_read_end_pos(struct rchan_buf *buf,
1014b86ff981SJens Axboe 				      size_t read_pos,
1015b86ff981SJens Axboe 				      size_t count)
1016b86ff981SJens Axboe {
1017b86ff981SJens Axboe 	size_t read_subbuf, padding, end_pos;
1018b86ff981SJens Axboe 	size_t subbuf_size = buf->chan->subbuf_size;
1019b86ff981SJens Axboe 	size_t n_subbufs = buf->chan->n_subbufs;
1020b86ff981SJens Axboe 
1021b86ff981SJens Axboe 	read_subbuf = read_pos / subbuf_size;
1022b86ff981SJens Axboe 	padding = buf->padding[read_subbuf];
1023b86ff981SJens Axboe 	if (read_pos % subbuf_size + count + padding == subbuf_size)
1024b86ff981SJens Axboe 		end_pos = (read_subbuf + 1) * subbuf_size;
1025b86ff981SJens Axboe 	else
1026b86ff981SJens Axboe 		end_pos = read_pos + count;
1027b86ff981SJens Axboe 	if (end_pos >= subbuf_size * n_subbufs)
1028b86ff981SJens Axboe 		end_pos = 0;
1029b86ff981SJens Axboe 
1030b86ff981SJens Axboe 	return end_pos;
1031b86ff981SJens Axboe }
1032b86ff981SJens Axboe 
1033a7c22421SAl Viro static ssize_t relay_file_read(struct file *filp,
1034a7c22421SAl Viro 			       char __user *buffer,
1035a7c22421SAl Viro 			       size_t count,
1036a7c22421SAl Viro 			       loff_t *ppos)
1037221415d7SJens Axboe {
1038221415d7SJens Axboe 	struct rchan_buf *buf = filp->private_data;
1039221415d7SJens Axboe 	size_t read_start, avail;
1040a7c22421SAl Viro 	size_t written = 0;
10416dac40a7STom Zanussi 	int ret;
1042221415d7SJens Axboe 
1043a7c22421SAl Viro 	if (!count)
1044221415d7SJens Axboe 		return 0;
1045221415d7SJens Axboe 
10465955102cSAl Viro 	inode_lock(file_inode(filp));
10476dac40a7STom Zanussi 	do {
1048a7c22421SAl Viro 		void *from;
1049a7c22421SAl Viro 
1050341a7213SPengcheng Yang 		if (!relay_file_read_avail(buf))
1051221415d7SJens Axboe 			break;
1052221415d7SJens Axboe 
1053341a7213SPengcheng Yang 		read_start = relay_file_read_start_pos(buf);
10546dac40a7STom Zanussi 		avail = relay_file_read_subbuf_avail(read_start, buf);
10556dac40a7STom Zanussi 		if (!avail)
10566dac40a7STom Zanussi 			break;
1057221415d7SJens Axboe 
1058a7c22421SAl Viro 		avail = min(count, avail);
1059a7c22421SAl Viro 		from = buf->start + read_start;
1060a7c22421SAl Viro 		ret = avail;
1061a7c22421SAl Viro 		if (copy_to_user(buffer, from, avail))
10626dac40a7STom Zanussi 			break;
10636dac40a7STom Zanussi 
1064a7c22421SAl Viro 		buffer += ret;
1065a7c22421SAl Viro 		written += ret;
1066a7c22421SAl Viro 		count -= ret;
1067a7c22421SAl Viro 
1068221415d7SJens Axboe 		relay_file_read_consume(buf, read_start, ret);
1069221415d7SJens Axboe 		*ppos = relay_file_read_end_pos(buf, read_start, ret);
1070a7c22421SAl Viro 	} while (count);
10715955102cSAl Viro 	inode_unlock(file_inode(filp));
1072221415d7SJens Axboe 
1073a7c22421SAl Viro 	return written;
10746dac40a7STom Zanussi }
10756dac40a7STom Zanussi 
1076221415d7SJens Axboe 
107715ad7cdcSHelge Deller const struct file_operations relay_file_operations = {
1078b86ff981SJens Axboe 	.open		= relay_file_open,
1079b86ff981SJens Axboe 	.poll		= relay_file_poll,
1080b86ff981SJens Axboe 	.mmap		= relay_file_mmap,
1081b86ff981SJens Axboe 	.read		= relay_file_read,
1082b86ff981SJens Axboe 	.release	= relay_file_release,
1083b86ff981SJens Axboe };
1084b86ff981SJens Axboe EXPORT_SYMBOL_GPL(relay_file_operations);
1085