Commit | Line | Data |
---|---|---|
b86ff981 JA |
1 | /* |
2 | * Public API and common code for kernel->userspace relay file support. | |
3 | * | |
0c1bc6b8 | 4 | * See Documentation/filesystems/relay.rst for an overview. |
b86ff981 JA |
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. | |
23c88752 MD |
10 | * November 2006 - CPU hotplug support by Mathieu Desnoyers |
11 | * (mathieu.desnoyers@polymtl.ca) | |
b86ff981 JA |
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> | |
9984de1a | 18 | #include <linux/export.h> |
b86ff981 JA |
19 | #include <linux/string.h> |
20 | #include <linux/relay.h> | |
21 | #include <linux/vmalloc.h> | |
22 | #include <linux/mm.h> | |
23c88752 | 23 | #include <linux/cpu.h> |
d6b29d7c | 24 | #include <linux/splice.h> |
23c88752 MD |
25 | |
26 | /* list of open channels, for cpu hotplug */ | |
27 | static DEFINE_MUTEX(relay_channels_mutex); | |
28 | static LIST_HEAD(relay_channels); | |
b86ff981 | 29 | |
b86ff981 | 30 | /* |
a1e09612 | 31 | * fault() vm_op implementation for relay file mapping. |
b86ff981 | 32 | */ |
3fb3894b | 33 | static vm_fault_t relay_buf_fault(struct vm_fault *vmf) |
b86ff981 JA |
34 | { |
35 | struct page *page; | |
11bac800 | 36 | struct rchan_buf *buf = vmf->vma->vm_private_data; |
a1e09612 | 37 | pgoff_t pgoff = vmf->pgoff; |
b86ff981 | 38 | |
b86ff981 | 39 | if (!buf) |
a1e09612 | 40 | return VM_FAULT_OOM; |
b86ff981 | 41 | |
a1e09612 | 42 | page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); |
b86ff981 | 43 | if (!page) |
a1e09612 | 44 | return VM_FAULT_SIGBUS; |
b86ff981 | 45 | get_page(page); |
a1e09612 | 46 | vmf->page = page; |
b86ff981 | 47 | |
a1e09612 | 48 | return 0; |
b86ff981 JA |
49 | } |
50 | ||
51 | /* | |
52 | * vm_ops for relay file mappings. | |
53 | */ | |
f0f37e2f | 54 | static const struct vm_operations_struct relay_file_mmap_ops = { |
a1e09612 | 55 | .fault = relay_buf_fault, |
b86ff981 JA |
56 | }; |
57 | ||
68ab3d88 MH |
58 | /* |
59 | * allocate an array of pointers of struct page | |
60 | */ | |
61 | static struct page **relay_alloc_page_array(unsigned int n_pages) | |
62 | { | |
83d87a4d | 63 | return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL); |
68ab3d88 MH |
64 | } |
65 | ||
66 | /* | |
67 | * free an array of pointers of struct page | |
68 | */ | |
69 | static void relay_free_page_array(struct page **array) | |
70 | { | |
200f1ce3 | 71 | kvfree(array); |
68ab3d88 MH |
72 | } |
73 | ||
b86ff981 JA |
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 | * | |
c1e8d7c6 | 81 | * Caller should already have grabbed mmap_lock. |
b86ff981 | 82 | */ |
01c55ed3 | 83 | static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) |
b86ff981 JA |
84 | { |
85 | unsigned long length = vma->vm_end - vma->vm_start; | |
b86ff981 JA |
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; | |
1c71222e | 94 | vm_flags_set(vma, VM_DONTEXPAND); |
b86ff981 | 95 | vma->vm_private_data = buf; |
b86ff981 JA |
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 | * | |
4c78a663 | 105 | * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The |
221415d7 | 106 | * passed in size will get page aligned, if it isn't already. |
b86ff981 | 107 | */ |
221415d7 | 108 | static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) |
b86ff981 JA |
109 | { |
110 | void *mem; | |
111 | unsigned int i, j, n_pages; | |
112 | ||
221415d7 JA |
113 | *size = PAGE_ALIGN(*size); |
114 | n_pages = *size >> PAGE_SHIFT; | |
b86ff981 | 115 | |
68ab3d88 | 116 | buf->page_array = relay_alloc_page_array(n_pages); |
b86ff981 JA |
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; | |
ebf99093 | 124 | set_page_private(buf->page_array[i], (unsigned long)buf); |
b86ff981 JA |
125 | } |
126 | mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); | |
127 | if (!mem) | |
128 | goto depopulate; | |
129 | ||
221415d7 | 130 | memset(mem, 0, *size); |
b86ff981 JA |
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]); | |
68ab3d88 | 137 | relay_free_page_array(buf->page_array); |
b86ff981 JA |
138 | return NULL; |
139 | } | |
140 | ||
141 | /** | |
142 | * relay_create_buf - allocate and initialize a channel buffer | |
4c78a663 | 143 | * @chan: the relay channel |
b86ff981 | 144 | * |
4c78a663 | 145 | * Returns channel buffer if successful, %NULL otherwise. |
b86ff981 | 146 | */ |
01c55ed3 | 147 | static struct rchan_buf *relay_create_buf(struct rchan *chan) |
b86ff981 | 148 | { |
f6302f1b DC |
149 | struct rchan_buf *buf; |
150 | ||
4d8586e0 | 151 | if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t)) |
b86ff981 JA |
152 | return NULL; |
153 | ||
f6302f1b DC |
154 | buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); |
155 | if (!buf) | |
156 | return NULL; | |
4d8586e0 | 157 | buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t), |
6da2ec56 | 158 | GFP_KERNEL); |
b86ff981 JA |
159 | if (!buf->padding) |
160 | goto free_buf; | |
161 | ||
221415d7 | 162 | buf->start = relay_alloc_buf(buf, &chan->alloc_size); |
b86ff981 JA |
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 | |
4c78a663 | 178 | * @kref: target kernel reference that contains the relay channel |
b86ff981 JA |
179 | * |
180 | * Should only be called from kref_put(). | |
181 | */ | |
01c55ed3 | 182 | static void relay_destroy_channel(struct kref *kref) |
b86ff981 JA |
183 | { |
184 | struct rchan *chan = container_of(kref, struct rchan, kref); | |
71e84329 | 185 | free_percpu(chan->buf); |
b86ff981 JA |
186 | kfree(chan); |
187 | } | |
188 | ||
189 | /** | |
190 | * relay_destroy_buf - destroy an rchan_buf struct and associated buffer | |
191 | * @buf: the buffer struct | |
192 | */ | |
01c55ed3 | 193 | static void relay_destroy_buf(struct rchan_buf *buf) |
b86ff981 JA |
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]); | |
68ab3d88 | 202 | relay_free_page_array(buf->page_array); |
b86ff981 | 203 | } |
017c59c0 | 204 | *per_cpu_ptr(chan->buf, buf->cpu) = NULL; |
b86ff981 JA |
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 | |
4c78a663 | 212 | * @kref: target kernel reference that contains the relay buffer |
b86ff981 | 213 | * |
e227867f | 214 | * Removes the file from the filesystem, which also frees the |
b86ff981 JA |
215 | * rchan_buf_struct and the channel buffer. Should only be called from |
216 | * kref_put(). | |
217 | */ | |
01c55ed3 | 218 | static void relay_remove_buf(struct kref *kref) |
b86ff981 JA |
219 | { |
220 | struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); | |
b86ff981 JA |
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 | */ | |
01c55ed3 | 230 | static int relay_buf_empty(struct rchan_buf *buf) |
b86ff981 JA |
231 | { |
232 | return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; | |
233 | } | |
b86ff981 JA |
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 | */ | |
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 | ||
023542f4 JN |
252 | static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf, |
253 | void *prev_subbuf, size_t prev_padding) | |
b86ff981 | 254 | { |
023542f4 JN |
255 | if (!buf->chan->cb->subbuf_start) |
256 | return !relay_buf_full(buf); | |
b86ff981 | 257 | |
023542f4 JN |
258 | return buf->chan->cb->subbuf_start(buf, subbuf, |
259 | prev_subbuf, prev_padding); | |
b86ff981 JA |
260 | } |
261 | ||
b86ff981 JA |
262 | /** |
263 | * wakeup_readers - wake up readers waiting on a channel | |
26b5679e | 264 | * @work: contains the channel buffer |
b86ff981 | 265 | * |
26b5679e | 266 | * This is the function used to defer reader waking |
b86ff981 | 267 | */ |
26b5679e | 268 | static void wakeup_readers(struct irq_work *work) |
b86ff981 | 269 | { |
26b5679e PZ |
270 | struct rchan_buf *buf; |
271 | ||
272 | buf = container_of(work, struct rchan_buf, wakeup_work); | |
b86ff981 JA |
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 | * | |
72fd4a35 | 281 | * See relay_reset() for description of effect. |
b86ff981 | 282 | */ |
192636ad | 283 | static void __relay_reset(struct rchan_buf *buf, unsigned int init) |
b86ff981 JA |
284 | { |
285 | size_t i; | |
286 | ||
287 | if (init) { | |
288 | init_waitqueue_head(&buf->read_wait); | |
289 | kref_init(&buf->kref); | |
26b5679e PZ |
290 | init_irq_work(&buf->wakeup_work, wakeup_readers); |
291 | } else { | |
292 | irq_work_sync(&buf->wakeup_work); | |
293 | } | |
b86ff981 JA |
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 | ||
023542f4 | 305 | relay_subbuf_start(buf, buf->data, NULL, 0); |
b86ff981 JA |
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 | * | |
72fd4a35 | 316 | * NOTE. Care should be taken that the channel isn't actually |
b86ff981 JA |
317 | * being used by anything when this call is made. |
318 | */ | |
319 | void relay_reset(struct rchan *chan) | |
320 | { | |
017c59c0 | 321 | struct rchan_buf *buf; |
b86ff981 | 322 | unsigned int i; |
b86ff981 JA |
323 | |
324 | if (!chan) | |
325 | return; | |
326 | ||
017c59c0 AG |
327 | if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { |
328 | __relay_reset(buf, 0); | |
23c88752 | 329 | return; |
b86ff981 | 330 | } |
23c88752 MD |
331 | |
332 | mutex_lock(&relay_channels_mutex); | |
98ba4031 | 333 | for_each_possible_cpu(i) |
017c59c0 AG |
334 | if ((buf = *per_cpu_ptr(chan->buf, i))) |
335 | __relay_reset(buf, 0); | |
23c88752 | 336 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
337 | } |
338 | EXPORT_SYMBOL_GPL(relay_reset); | |
339 | ||
20d8b67c EGM |
340 | static inline void relay_set_buf_dentry(struct rchan_buf *buf, |
341 | struct dentry *dentry) | |
342 | { | |
343 | buf->dentry = dentry; | |
7682c918 | 344 | d_inode(buf->dentry)->i_size = buf->early_bytes; |
20d8b67c EGM |
345 | } |
346 | ||
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 | ||
81ca2970 | 354 | tmpname = kasprintf(GFP_KERNEL, "%s%d", chan->base_filename, cpu); |
20d8b67c EGM |
355 | if (!tmpname) |
356 | return NULL; | |
20d8b67c EGM |
357 | |
358 | /* Create file in fs */ | |
359 | dentry = chan->cb->create_buf_file(tmpname, chan->parent, | |
360 | S_IRUSR, buf, | |
361 | &chan->is_global); | |
2c1cf00e GKH |
362 | if (IS_ERR(dentry)) |
363 | dentry = NULL; | |
20d8b67c EGM |
364 | |
365 | kfree(tmpname); | |
366 | ||
367 | return dentry; | |
368 | } | |
369 | ||
4c78a663 | 370 | /* |
b86ff981 JA |
371 | * relay_open_buf - create a new relay channel buffer |
372 | * | |
23c88752 | 373 | * used by relay_open() and CPU hotplug. |
b86ff981 | 374 | */ |
23c88752 | 375 | static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) |
b86ff981 | 376 | { |
598f0046 | 377 | struct rchan_buf *buf; |
b86ff981 JA |
378 | struct dentry *dentry; |
379 | ||
23c88752 | 380 | if (chan->is_global) |
017c59c0 | 381 | return *per_cpu_ptr(chan->buf, 0); |
b86ff981 JA |
382 | |
383 | buf = relay_create_buf(chan); | |
384 | if (!buf) | |
20d8b67c EGM |
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); | |
59dbb2a0 AG |
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); | |
2c1cf00e | 397 | if (IS_ERR_OR_NULL(dentry)) |
59dbb2a0 | 398 | goto free_buf; |
20d8b67c | 399 | } |
23c88752 MD |
400 | |
401 | buf->cpu = cpu; | |
402 | __relay_reset(buf, 1); | |
b86ff981 | 403 | |
23c88752 | 404 | if(chan->is_global) { |
017c59c0 | 405 | *per_cpu_ptr(chan->buf, 0) = buf; |
23c88752 MD |
406 | buf->cpu = 0; |
407 | } | |
408 | ||
20d8b67c | 409 | return buf; |
23c88752 MD |
410 | |
411 | free_buf: | |
412 | relay_destroy_buf(buf); | |
20d8b67c | 413 | return NULL; |
b86ff981 JA |
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 | */ | |
192636ad | 424 | static void relay_close_buf(struct rchan_buf *buf) |
b86ff981 JA |
425 | { |
426 | buf->finalized = 1; | |
26b5679e | 427 | irq_work_sync(&buf->wakeup_work); |
b8d4a5bf | 428 | buf->chan->cb->remove_buf_file(buf->dentry); |
b86ff981 JA |
429 | kref_put(&buf->kref, relay_remove_buf); |
430 | } | |
431 | ||
e6d4989a | 432 | int relay_prepare_cpu(unsigned int cpu) |
23c88752 | 433 | { |
23c88752 | 434 | struct rchan *chan; |
017c59c0 | 435 | struct rchan_buf *buf; |
23c88752 | 436 | |
e6d4989a RW |
437 | mutex_lock(&relay_channels_mutex); |
438 | list_for_each_entry(chan, &relay_channels, list) { | |
47b7eae6 | 439 | if (*per_cpu_ptr(chan->buf, cpu)) |
e6d4989a RW |
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; | |
23c88752 | 446 | } |
e6d4989a | 447 | *per_cpu_ptr(chan->buf, cpu) = buf; |
23c88752 | 448 | } |
e6d4989a RW |
449 | mutex_unlock(&relay_channels_mutex); |
450 | return 0; | |
23c88752 MD |
451 | } |
452 | ||
b86ff981 JA |
453 | /** |
454 | * relay_open - create a new relay channel | |
2a1c6158 | 455 | * @base_filename: base name of files to create |
20d8b67c | 456 | * @parent: dentry of parent directory, %NULL for root directory or buffer |
b86ff981 JA |
457 | * @subbuf_size: size of sub-buffers |
458 | * @n_subbufs: number of sub-buffers | |
459 | * @cb: client callback functions | |
23c88752 | 460 | * @private_data: user-defined data |
b86ff981 | 461 | * |
4c78a663 | 462 | * Returns channel pointer if successful, %NULL otherwise. |
b86ff981 JA |
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 | |
72fd4a35 | 467 | * permissions will be %S_IRUSR. |
b86ff981 JA |
468 | */ |
469 | struct rchan *relay_open(const char *base_filename, | |
470 | struct dentry *parent, | |
471 | size_t subbuf_size, | |
472 | size_t n_subbufs, | |
023542f4 | 473 | const struct rchan_callbacks *cb, |
23c88752 | 474 | void *private_data) |
b86ff981 JA |
475 | { |
476 | unsigned int i; | |
477 | struct rchan *chan; | |
017c59c0 | 478 | struct rchan_buf *buf; |
b86ff981 JA |
479 | |
480 | if (!(subbuf_size && n_subbufs)) | |
481 | return NULL; | |
f6302f1b DC |
482 | if (subbuf_size > UINT_MAX / n_subbufs) |
483 | return NULL; | |
371e0388 | 484 | if (!cb || !cb->create_buf_file || !cb->remove_buf_file) |
6f8f2544 | 485 | return NULL; |
b86ff981 | 486 | |
cd861280 | 487 | chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); |
b86ff981 JA |
488 | if (!chan) |
489 | return NULL; | |
490 | ||
017c59c0 | 491 | chan->buf = alloc_percpu(struct rchan_buf *); |
54e200ab DA |
492 | if (!chan->buf) { |
493 | kfree(chan); | |
494 | return NULL; | |
495 | } | |
496 | ||
b86ff981 JA |
497 | chan->version = RELAYFS_CHANNEL_VERSION; |
498 | chan->n_subbufs = n_subbufs; | |
499 | chan->subbuf_size = subbuf_size; | |
a05342cb | 500 | chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs); |
23c88752 MD |
501 | chan->parent = parent; |
502 | chan->private_data = private_data; | |
20d8b67c EGM |
503 | if (base_filename) { |
504 | chan->has_base_filename = 1; | |
3f0dad01 | 505 | strscpy(chan->base_filename, base_filename, NAME_MAX); |
20d8b67c | 506 | } |
023542f4 | 507 | chan->cb = cb; |
b86ff981 JA |
508 | kref_init(&chan->kref); |
509 | ||
23c88752 | 510 | mutex_lock(&relay_channels_mutex); |
b86ff981 | 511 | for_each_online_cpu(i) { |
017c59c0 AG |
512 | buf = relay_open_buf(chan, i); |
513 | if (!buf) | |
b86ff981 | 514 | goto free_bufs; |
017c59c0 | 515 | *per_cpu_ptr(chan->buf, i) = buf; |
b86ff981 | 516 | } |
23c88752 MD |
517 | list_add(&chan->list, &relay_channels); |
518 | mutex_unlock(&relay_channels_mutex); | |
b86ff981 | 519 | |
b86ff981 JA |
520 | return chan; |
521 | ||
522 | free_bufs: | |
98ba4031 | 523 | for_each_possible_cpu(i) { |
017c59c0 AG |
524 | if ((buf = *per_cpu_ptr(chan->buf, i))) |
525 | relay_close_buf(buf); | |
b86ff981 | 526 | } |
b86ff981 | 527 | |
b86ff981 | 528 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 529 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
530 | return NULL; |
531 | } | |
532 | EXPORT_SYMBOL_GPL(relay_open); | |
533 | ||
20d8b67c EGM |
534 | struct rchan_percpu_buf_dispatcher { |
535 | struct rchan_buf *buf; | |
536 | struct dentry *dentry; | |
537 | }; | |
538 | ||
b86ff981 JA |
539 | /** |
540 | * relay_switch_subbuf - switch to a new sub-buffer | |
541 | * @buf: channel buffer | |
542 | * @length: size of current event | |
543 | * | |
544 | * Returns either the length passed in or 0 if full. | |
545 | * | |
546 | * Performs sub-buffer-switch tasks such as invoking callbacks, | |
547 | * updating padding counts, waking up readers, etc. | |
548 | */ | |
549 | size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) | |
550 | { | |
551 | void *old, *new; | |
552 | size_t old_subbuf, new_subbuf; | |
553 | ||
554 | if (unlikely(length > buf->chan->subbuf_size)) | |
555 | goto toobig; | |
556 | ||
557 | if (buf->offset != buf->chan->subbuf_size + 1) { | |
558 | buf->prev_padding = buf->chan->subbuf_size - buf->offset; | |
559 | old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; | |
560 | buf->padding[old_subbuf] = buf->prev_padding; | |
561 | buf->subbufs_produced++; | |
20d8b67c | 562 | if (buf->dentry) |
7682c918 | 563 | d_inode(buf->dentry)->i_size += |
20d8b67c EGM |
564 | buf->chan->subbuf_size - |
565 | buf->padding[old_subbuf]; | |
566 | else | |
567 | buf->early_bytes += buf->chan->subbuf_size - | |
568 | buf->padding[old_subbuf]; | |
221415d7 | 569 | smp_mb(); |
26b5679e | 570 | if (waitqueue_active(&buf->read_wait)) { |
7c9cb383 TZ |
571 | /* |
572 | * Calling wake_up_interruptible() from here | |
573 | * will deadlock if we happen to be logging | |
574 | * from the scheduler (trying to re-grab | |
575 | * rq->lock), so defer it. | |
576 | */ | |
26b5679e PZ |
577 | irq_work_queue(&buf->wakeup_work); |
578 | } | |
b86ff981 JA |
579 | } |
580 | ||
581 | old = buf->data; | |
582 | new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; | |
583 | new = buf->start + new_subbuf * buf->chan->subbuf_size; | |
584 | buf->offset = 0; | |
023542f4 | 585 | if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) { |
b86ff981 JA |
586 | buf->offset = buf->chan->subbuf_size + 1; |
587 | return 0; | |
588 | } | |
589 | buf->data = new; | |
590 | buf->padding[new_subbuf] = 0; | |
591 | ||
592 | if (unlikely(length + buf->offset > buf->chan->subbuf_size)) | |
593 | goto toobig; | |
594 | ||
595 | return length; | |
596 | ||
597 | toobig: | |
598 | buf->chan->last_toobig = length; | |
599 | return 0; | |
600 | } | |
601 | EXPORT_SYMBOL_GPL(relay_switch_subbuf); | |
602 | ||
603 | /** | |
604 | * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count | |
605 | * @chan: the channel | |
606 | * @cpu: the cpu associated with the channel buffer to update | |
607 | * @subbufs_consumed: number of sub-buffers to add to current buf's count | |
608 | * | |
609 | * Adds to the channel buffer's consumed sub-buffer count. | |
610 | * subbufs_consumed should be the number of sub-buffers newly consumed, | |
611 | * not the total consumed. | |
612 | * | |
72fd4a35 | 613 | * NOTE. Kernel clients don't need to call this function if the channel |
b86ff981 JA |
614 | * mode is 'overwrite'. |
615 | */ | |
616 | void relay_subbufs_consumed(struct rchan *chan, | |
617 | unsigned int cpu, | |
618 | size_t subbufs_consumed) | |
619 | { | |
620 | struct rchan_buf *buf; | |
621 | ||
9a29d0fb | 622 | if (!chan || cpu >= NR_CPUS) |
b86ff981 JA |
623 | return; |
624 | ||
017c59c0 | 625 | buf = *per_cpu_ptr(chan->buf, cpu); |
9a29d0fb | 626 | if (!buf || subbufs_consumed > chan->n_subbufs) |
b86ff981 JA |
627 | return; |
628 | ||
2c53d910 | 629 | if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed) |
b86ff981 | 630 | buf->subbufs_consumed = buf->subbufs_produced; |
2c53d910 AS |
631 | else |
632 | buf->subbufs_consumed += subbufs_consumed; | |
b86ff981 JA |
633 | } |
634 | EXPORT_SYMBOL_GPL(relay_subbufs_consumed); | |
635 | ||
636 | /** | |
637 | * relay_close - close the channel | |
638 | * @chan: the channel | |
639 | * | |
640 | * Closes all channel buffers and frees the channel. | |
641 | */ | |
642 | void relay_close(struct rchan *chan) | |
643 | { | |
017c59c0 | 644 | struct rchan_buf *buf; |
b86ff981 | 645 | unsigned int i; |
b86ff981 JA |
646 | |
647 | if (!chan) | |
648 | return; | |
649 | ||
23c88752 | 650 | mutex_lock(&relay_channels_mutex); |
017c59c0 AG |
651 | if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) |
652 | relay_close_buf(buf); | |
23c88752 MD |
653 | else |
654 | for_each_possible_cpu(i) | |
017c59c0 AG |
655 | if ((buf = *per_cpu_ptr(chan->buf, i))) |
656 | relay_close_buf(buf); | |
b86ff981 JA |
657 | |
658 | if (chan->last_toobig) | |
659 | printk(KERN_WARNING "relay: one or more items not logged " | |
5b5e0928 | 660 | "[item size (%zd) > sub-buffer size (%zd)]\n", |
b86ff981 JA |
661 | chan->last_toobig, chan->subbuf_size); |
662 | ||
23c88752 | 663 | list_del(&chan->list); |
b86ff981 | 664 | kref_put(&chan->kref, relay_destroy_channel); |
23c88752 | 665 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
666 | } |
667 | EXPORT_SYMBOL_GPL(relay_close); | |
668 | ||
669 | /** | |
670 | * relay_flush - close the channel | |
671 | * @chan: the channel | |
672 | * | |
4c78a663 | 673 | * Flushes all channel buffers, i.e. forces buffer switch. |
b86ff981 JA |
674 | */ |
675 | void relay_flush(struct rchan *chan) | |
676 | { | |
017c59c0 | 677 | struct rchan_buf *buf; |
b86ff981 | 678 | unsigned int i; |
b86ff981 JA |
679 | |
680 | if (!chan) | |
681 | return; | |
682 | ||
017c59c0 AG |
683 | if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { |
684 | relay_switch_subbuf(buf, 0); | |
23c88752 | 685 | return; |
b86ff981 | 686 | } |
23c88752 MD |
687 | |
688 | mutex_lock(&relay_channels_mutex); | |
689 | for_each_possible_cpu(i) | |
017c59c0 AG |
690 | if ((buf = *per_cpu_ptr(chan->buf, i))) |
691 | relay_switch_subbuf(buf, 0); | |
23c88752 | 692 | mutex_unlock(&relay_channels_mutex); |
b86ff981 JA |
693 | } |
694 | EXPORT_SYMBOL_GPL(relay_flush); | |
695 | ||
696 | /** | |
697 | * relay_file_open - open file op for relay files | |
698 | * @inode: the inode | |
699 | * @filp: the file | |
700 | * | |
701 | * Increments the channel buffer refcount. | |
702 | */ | |
703 | static int relay_file_open(struct inode *inode, struct file *filp) | |
704 | { | |
8e18e294 | 705 | struct rchan_buf *buf = inode->i_private; |
b86ff981 JA |
706 | kref_get(&buf->kref); |
707 | filp->private_data = buf; | |
708 | ||
37529fe9 | 709 | return nonseekable_open(inode, filp); |
b86ff981 JA |
710 | } |
711 | ||
712 | /** | |
713 | * relay_file_mmap - mmap file op for relay files | |
714 | * @filp: the file | |
715 | * @vma: the vma describing what to map | |
716 | * | |
72fd4a35 | 717 | * Calls upon relay_mmap_buf() to map the file into user space. |
b86ff981 JA |
718 | */ |
719 | static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) | |
720 | { | |
721 | struct rchan_buf *buf = filp->private_data; | |
722 | return relay_mmap_buf(buf, vma); | |
723 | } | |
724 | ||
725 | /** | |
726 | * relay_file_poll - poll file op for relay files | |
727 | * @filp: the file | |
728 | * @wait: poll table | |
729 | * | |
730 | * Poll implemention. | |
731 | */ | |
9dd95748 | 732 | static __poll_t relay_file_poll(struct file *filp, poll_table *wait) |
b86ff981 | 733 | { |
9dd95748 | 734 | __poll_t mask = 0; |
b86ff981 JA |
735 | struct rchan_buf *buf = filp->private_data; |
736 | ||
737 | if (buf->finalized) | |
a9a08845 | 738 | return EPOLLERR; |
b86ff981 JA |
739 | |
740 | if (filp->f_mode & FMODE_READ) { | |
741 | poll_wait(filp, &buf->read_wait, wait); | |
742 | if (!relay_buf_empty(buf)) | |
a9a08845 | 743 | mask |= EPOLLIN | EPOLLRDNORM; |
b86ff981 JA |
744 | } |
745 | ||
746 | return mask; | |
747 | } | |
748 | ||
749 | /** | |
750 | * relay_file_release - release file op for relay files | |
751 | * @inode: the inode | |
752 | * @filp: the file | |
753 | * | |
754 | * Decrements the channel refcount, as the filesystem is | |
755 | * no longer using it. | |
756 | */ | |
757 | static int relay_file_release(struct inode *inode, struct file *filp) | |
758 | { | |
759 | struct rchan_buf *buf = filp->private_data; | |
760 | kref_put(&buf->kref, relay_remove_buf); | |
761 | ||
762 | return 0; | |
763 | } | |
764 | ||
4c78a663 | 765 | /* |
b86ff981 JA |
766 | * relay_file_read_consume - update the consumed count for the buffer |
767 | */ | |
768 | static void relay_file_read_consume(struct rchan_buf *buf, | |
769 | size_t read_pos, | |
770 | size_t bytes_consumed) | |
771 | { | |
772 | size_t subbuf_size = buf->chan->subbuf_size; | |
773 | size_t n_subbufs = buf->chan->n_subbufs; | |
774 | size_t read_subbuf; | |
775 | ||
32194450 TZ |
776 | if (buf->subbufs_produced == buf->subbufs_consumed && |
777 | buf->offset == buf->bytes_consumed) | |
778 | return; | |
779 | ||
b86ff981 JA |
780 | if (buf->bytes_consumed + bytes_consumed > subbuf_size) { |
781 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); | |
782 | buf->bytes_consumed = 0; | |
783 | } | |
784 | ||
785 | buf->bytes_consumed += bytes_consumed; | |
a66e356c MH |
786 | if (!read_pos) |
787 | read_subbuf = buf->subbufs_consumed % n_subbufs; | |
788 | else | |
789 | read_subbuf = read_pos / buf->chan->subbuf_size; | |
b86ff981 JA |
790 | if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { |
791 | if ((read_subbuf == buf->subbufs_produced % n_subbufs) && | |
792 | (buf->offset == subbuf_size)) | |
793 | return; | |
794 | relay_subbufs_consumed(buf->chan, buf->cpu, 1); | |
795 | buf->bytes_consumed = 0; | |
796 | } | |
797 | } | |
798 | ||
4c78a663 | 799 | /* |
b86ff981 JA |
800 | * relay_file_read_avail - boolean, are there unconsumed bytes available? |
801 | */ | |
341a7213 | 802 | static int relay_file_read_avail(struct rchan_buf *buf) |
b86ff981 | 803 | { |
b86ff981 JA |
804 | size_t subbuf_size = buf->chan->subbuf_size; |
805 | size_t n_subbufs = buf->chan->n_subbufs; | |
221415d7 | 806 | size_t produced = buf->subbufs_produced; |
ac05b7a1 | 807 | size_t consumed; |
b86ff981 | 808 | |
341a7213 | 809 | relay_file_read_consume(buf, 0, 0); |
b86ff981 | 810 | |
32194450 TZ |
811 | consumed = buf->subbufs_consumed; |
812 | ||
221415d7 JA |
813 | if (unlikely(buf->offset > subbuf_size)) { |
814 | if (produced == consumed) | |
815 | return 0; | |
816 | return 1; | |
b86ff981 JA |
817 | } |
818 | ||
221415d7 | 819 | if (unlikely(produced - consumed >= n_subbufs)) { |
a66e356c | 820 | consumed = produced - n_subbufs + 1; |
221415d7 | 821 | buf->subbufs_consumed = consumed; |
a66e356c | 822 | buf->bytes_consumed = 0; |
221415d7 | 823 | } |
1bfbc608 | 824 | |
221415d7 JA |
825 | produced = (produced % n_subbufs) * subbuf_size + buf->offset; |
826 | consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; | |
827 | ||
828 | if (consumed > produced) | |
829 | produced += n_subbufs * subbuf_size; | |
1bfbc608 | 830 | |
32194450 TZ |
831 | if (consumed == produced) { |
832 | if (buf->offset == subbuf_size && | |
833 | buf->subbufs_produced > buf->subbufs_consumed) | |
834 | return 1; | |
b86ff981 | 835 | return 0; |
32194450 | 836 | } |
b86ff981 | 837 | |
b86ff981 JA |
838 | return 1; |
839 | } | |
840 | ||
841 | /** | |
842 | * relay_file_read_subbuf_avail - return bytes available in sub-buffer | |
4c78a663 RD |
843 | * @read_pos: file read position |
844 | * @buf: relay channel buffer | |
b86ff981 JA |
845 | */ |
846 | static size_t relay_file_read_subbuf_avail(size_t read_pos, | |
847 | struct rchan_buf *buf) | |
848 | { | |
849 | size_t padding, avail = 0; | |
850 | size_t read_subbuf, read_offset, write_subbuf, write_offset; | |
851 | size_t subbuf_size = buf->chan->subbuf_size; | |
852 | ||
853 | write_subbuf = (buf->data - buf->start) / subbuf_size; | |
854 | write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; | |
855 | read_subbuf = read_pos / subbuf_size; | |
856 | read_offset = read_pos % subbuf_size; | |
857 | padding = buf->padding[read_subbuf]; | |
858 | ||
859 | if (read_subbuf == write_subbuf) { | |
860 | if (read_offset + padding < write_offset) | |
861 | avail = write_offset - (read_offset + padding); | |
862 | } else | |
863 | avail = (subbuf_size - padding) - read_offset; | |
864 | ||
865 | return avail; | |
866 | } | |
867 | ||
868 | /** | |
869 | * relay_file_read_start_pos - find the first available byte to read | |
4c78a663 | 870 | * @buf: relay channel buffer |
b86ff981 | 871 | * |
341a7213 | 872 | * If the read_pos is in the middle of padding, return the |
b86ff981 JA |
873 | * position of the first actually available byte, otherwise |
874 | * return the original value. | |
875 | */ | |
341a7213 | 876 | static size_t relay_file_read_start_pos(struct rchan_buf *buf) |
b86ff981 JA |
877 | { |
878 | size_t read_subbuf, padding, padding_start, padding_end; | |
879 | size_t subbuf_size = buf->chan->subbuf_size; | |
880 | size_t n_subbufs = buf->chan->n_subbufs; | |
8d62fdeb | 881 | size_t consumed = buf->subbufs_consumed % n_subbufs; |
43ec16f1 ZZ |
882 | size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed) |
883 | % (n_subbufs * subbuf_size); | |
b86ff981 JA |
884 | |
885 | read_subbuf = read_pos / subbuf_size; | |
886 | padding = buf->padding[read_subbuf]; | |
887 | padding_start = (read_subbuf + 1) * subbuf_size - padding; | |
888 | padding_end = (read_subbuf + 1) * subbuf_size; | |
889 | if (read_pos >= padding_start && read_pos < padding_end) { | |
890 | read_subbuf = (read_subbuf + 1) % n_subbufs; | |
891 | read_pos = read_subbuf * subbuf_size; | |
892 | } | |
893 | ||
894 | return read_pos; | |
895 | } | |
896 | ||
897 | /** | |
898 | * relay_file_read_end_pos - return the new read position | |
4c78a663 RD |
899 | * @read_pos: file read position |
900 | * @buf: relay channel buffer | |
901 | * @count: number of bytes to be read | |
b86ff981 JA |
902 | */ |
903 | static size_t relay_file_read_end_pos(struct rchan_buf *buf, | |
904 | size_t read_pos, | |
905 | size_t count) | |
906 | { | |
907 | size_t read_subbuf, padding, end_pos; | |
908 | size_t subbuf_size = buf->chan->subbuf_size; | |
909 | size_t n_subbufs = buf->chan->n_subbufs; | |
910 | ||
911 | read_subbuf = read_pos / subbuf_size; | |
912 | padding = buf->padding[read_subbuf]; | |
913 | if (read_pos % subbuf_size + count + padding == subbuf_size) | |
914 | end_pos = (read_subbuf + 1) * subbuf_size; | |
915 | else | |
916 | end_pos = read_pos + count; | |
917 | if (end_pos >= subbuf_size * n_subbufs) | |
918 | end_pos = 0; | |
919 | ||
920 | return end_pos; | |
921 | } | |
922 | ||
a7c22421 AV |
923 | static ssize_t relay_file_read(struct file *filp, |
924 | char __user *buffer, | |
925 | size_t count, | |
926 | loff_t *ppos) | |
221415d7 | 927 | { |
6dac40a7 TZ |
928 | struct rchan_buf *buf = filp->private_data; |
929 | size_t read_start, avail; | |
a7c22421 | 930 | size_t written = 0; |
6dac40a7 | 931 | int ret; |
221415d7 | 932 | |
a7c22421 | 933 | if (!count) |
221415d7 JA |
934 | return 0; |
935 | ||
5955102c | 936 | inode_lock(file_inode(filp)); |
221415d7 | 937 | do { |
a7c22421 AV |
938 | void *from; |
939 | ||
341a7213 | 940 | if (!relay_file_read_avail(buf)) |
6dac40a7 TZ |
941 | break; |
942 | ||
341a7213 | 943 | read_start = relay_file_read_start_pos(buf); |
6dac40a7 TZ |
944 | avail = relay_file_read_subbuf_avail(read_start, buf); |
945 | if (!avail) | |
221415d7 | 946 | break; |
221415d7 | 947 | |
a7c22421 AV |
948 | avail = min(count, avail); |
949 | from = buf->start + read_start; | |
950 | ret = avail; | |
951 | if (copy_to_user(buffer, from, avail)) | |
6dac40a7 TZ |
952 | break; |
953 | ||
a7c22421 AV |
954 | buffer += ret; |
955 | written += ret; | |
956 | count -= ret; | |
6dac40a7 | 957 | |
a7c22421 AV |
958 | relay_file_read_consume(buf, read_start, ret); |
959 | *ppos = relay_file_read_end_pos(buf, read_start, ret); | |
960 | } while (count); | |
961 | inode_unlock(file_inode(filp)); | |
6dac40a7 | 962 | |
a7c22421 | 963 | return written; |
6dac40a7 TZ |
964 | } |
965 | ||
221415d7 | 966 | |
15ad7cdc | 967 | const struct file_operations relay_file_operations = { |
b86ff981 JA |
968 | .open = relay_file_open, |
969 | .poll = relay_file_poll, | |
970 | .mmap = relay_file_mmap, | |
971 | .read = relay_file_read, | |
b86ff981 JA |
972 | .release = relay_file_release, |
973 | }; | |
974 | EXPORT_SYMBOL_GPL(relay_file_operations); |