Blame view
kernel/relay.c
32.2 KB
b86ff981a [PATCH] relay: mi... |
1 2 3 |
/* * Public API and common code for kernel->userspace relay file support. * |
c9b3febc5 Fix a use after f... |
4 |
* See Documentation/filesystems/relay.txt for an overview. |
b86ff981a [PATCH] relay: mi... |
5 6 7 8 9 |
* * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com) * * Moved to kernel/relay.c by Paul Mundt, 2006. |
23c887522 [PATCH] Relay: ad... |
10 11 |
* November 2006 - CPU hotplug support by Mathieu Desnoyers * (mathieu.desnoyers@polymtl.ca) |
b86ff981a [PATCH] relay: mi... |
12 13 14 15 16 17 |
* * This file is released under the GPL. */ #include <linux/errno.h> #include <linux/stddef.h> #include <linux/slab.h> |
9984de1a5 kernel: Map most ... |
18 |
#include <linux/export.h> |
b86ff981a [PATCH] relay: mi... |
19 20 21 22 |
#include <linux/string.h> #include <linux/relay.h> #include <linux/vmalloc.h> #include <linux/mm.h> |
23c887522 [PATCH] Relay: ad... |
23 |
#include <linux/cpu.h> |
d6b29d7ce splice: divorce t... |
24 |
#include <linux/splice.h> |
23c887522 [PATCH] Relay: ad... |
25 26 27 28 |
/* list of open channels, for cpu hotplug */ static DEFINE_MUTEX(relay_channels_mutex); static LIST_HEAD(relay_channels); |
b86ff981a [PATCH] relay: mi... |
29 30 31 32 33 34 35 36 37 38 39 |
/* * close() vm_op implementation for relay file mapping. */ static void relay_file_mmap_close(struct vm_area_struct *vma) { struct rchan_buf *buf = vma->vm_private_data; buf->chan->cb->buf_unmapped(buf, vma->vm_file); } /* |
a1e096129 relay: nopage |
40 |
* fault() vm_op implementation for relay file mapping. |
b86ff981a [PATCH] relay: mi... |
41 |
*/ |
3fb3894b8 kernel/relay.c: c... |
42 |
static vm_fault_t relay_buf_fault(struct vm_fault *vmf) |
b86ff981a [PATCH] relay: mi... |
43 44 |
{ struct page *page; |
11bac8000 mm, fs: reduce fa... |
45 |
struct rchan_buf *buf = vmf->vma->vm_private_data; |
a1e096129 relay: nopage |
46 |
pgoff_t pgoff = vmf->pgoff; |
b86ff981a [PATCH] relay: mi... |
47 |
|
b86ff981a [PATCH] relay: mi... |
48 |
if (!buf) |
a1e096129 relay: nopage |
49 |
return VM_FAULT_OOM; |
b86ff981a [PATCH] relay: mi... |
50 |
|
a1e096129 relay: nopage |
51 |
page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT)); |
b86ff981a [PATCH] relay: mi... |
52 |
if (!page) |
a1e096129 relay: nopage |
53 |
return VM_FAULT_SIGBUS; |
b86ff981a [PATCH] relay: mi... |
54 |
get_page(page); |
a1e096129 relay: nopage |
55 |
vmf->page = page; |
b86ff981a [PATCH] relay: mi... |
56 |
|
a1e096129 relay: nopage |
57 |
return 0; |
b86ff981a [PATCH] relay: mi... |
58 59 60 61 62 |
} /* * vm_ops for relay file mappings. */ |
f0f37e2f7 const: mark struc... |
63 |
static const struct vm_operations_struct relay_file_mmap_ops = { |
a1e096129 relay: nopage |
64 |
.fault = relay_buf_fault, |
b86ff981a [PATCH] relay: mi... |
65 66 |
.close = relay_file_mmap_close, }; |
68ab3d883 relayfs: support ... |
67 68 69 70 71 |
/* * allocate an array of pointers of struct page */ static struct page **relay_alloc_page_array(unsigned int n_pages) { |
408af87a3 Clean up relay_al... |
72 73 74 75 |
const size_t pa_size = n_pages * sizeof(struct page *); if (pa_size > PAGE_SIZE) return vzalloc(pa_size); return kzalloc(pa_size, GFP_KERNEL); |
68ab3d883 relayfs: support ... |
76 77 78 79 80 81 82 |
} /* * free an array of pointers of struct page */ static void relay_free_page_array(struct page **array) { |
200f1ce36 kernel/relay.c: u... |
83 |
kvfree(array); |
68ab3d883 relayfs: support ... |
84 |
} |
b86ff981a [PATCH] relay: mi... |
85 86 87 88 89 90 91 92 93 |
/** * relay_mmap_buf: - mmap channel buffer to process address space * @buf: relay channel buffer * @vma: vm_area_struct describing memory to be mapped * * Returns 0 if ok, negative on error * * Caller should already have grabbed mmap_sem. */ |
01c55ed32 kernel/relay.c: m... |
94 |
static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma) |
b86ff981a [PATCH] relay: mi... |
95 96 97 98 99 100 101 102 103 104 105 |
{ unsigned long length = vma->vm_end - vma->vm_start; struct file *filp = vma->vm_file; if (!buf) return -EBADF; if (length != (unsigned long)buf->chan->alloc_size) return -EINVAL; vma->vm_ops = &relay_file_mmap_ops; |
2f98735c9 vm audit: add VM_... |
106 |
vma->vm_flags |= VM_DONTEXPAND; |
b86ff981a [PATCH] relay: mi... |
107 108 109 110 111 112 113 114 115 116 117 |
vma->vm_private_data = buf; buf->chan->cb->buf_mapped(buf, filp); return 0; } /** * relay_alloc_buf - allocate a channel buffer * @buf: the buffer struct * @size: total size of the buffer * |
4c78a6639 [PATCH] kernel-do... |
118 |
* Returns a pointer to the resulting buffer, %NULL if unsuccessful. The |
221415d76 [PATCH] relay: ad... |
119 |
* passed in size will get page aligned, if it isn't already. |
b86ff981a [PATCH] relay: mi... |
120 |
*/ |
221415d76 [PATCH] relay: ad... |
121 |
static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size) |
b86ff981a [PATCH] relay: mi... |
122 123 124 |
{ void *mem; unsigned int i, j, n_pages; |
221415d76 [PATCH] relay: ad... |
125 126 |
*size = PAGE_ALIGN(*size); n_pages = *size >> PAGE_SHIFT; |
b86ff981a [PATCH] relay: mi... |
127 |
|
68ab3d883 relayfs: support ... |
128 |
buf->page_array = relay_alloc_page_array(n_pages); |
b86ff981a [PATCH] relay: mi... |
129 130 131 132 133 134 135 |
if (!buf->page_array) return NULL; for (i = 0; i < n_pages; i++) { buf->page_array[i] = alloc_page(GFP_KERNEL); if (unlikely(!buf->page_array[i])) goto depopulate; |
ebf990934 splice: relay sup... |
136 |
set_page_private(buf->page_array[i], (unsigned long)buf); |
b86ff981a [PATCH] relay: mi... |
137 138 139 140 |
} mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL); if (!mem) goto depopulate; |
221415d76 [PATCH] relay: ad... |
141 |
memset(mem, 0, *size); |
b86ff981a [PATCH] relay: mi... |
142 143 144 145 146 147 |
buf->page_count = n_pages; return mem; depopulate: for (j = 0; j < i; j++) __free_page(buf->page_array[j]); |
68ab3d883 relayfs: support ... |
148 |
relay_free_page_array(buf->page_array); |
b86ff981a [PATCH] relay: mi... |
149 150 151 152 153 |
return NULL; } /** * relay_create_buf - allocate and initialize a channel buffer |
4c78a6639 [PATCH] kernel-do... |
154 |
* @chan: the relay channel |
b86ff981a [PATCH] relay: mi... |
155 |
* |
4c78a6639 [PATCH] kernel-do... |
156 |
* Returns channel buffer if successful, %NULL otherwise. |
b86ff981a [PATCH] relay: mi... |
157 |
*/ |
01c55ed32 kernel/relay.c: m... |
158 |
static struct rchan_buf *relay_create_buf(struct rchan *chan) |
b86ff981a [PATCH] relay: mi... |
159 |
{ |
f6302f1bc relay: prevent in... |
160 |
struct rchan_buf *buf; |
88913bd8e kernel/relay.c: l... |
161 |
if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *)) |
b86ff981a [PATCH] relay: mi... |
162 |
return NULL; |
f6302f1bc relay: prevent in... |
163 164 165 |
buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL); if (!buf) return NULL; |
6da2ec560 treewide: kmalloc... |
166 167 |
buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *), GFP_KERNEL); |
b86ff981a [PATCH] relay: mi... |
168 169 |
if (!buf->padding) goto free_buf; |
221415d76 [PATCH] relay: ad... |
170 |
buf->start = relay_alloc_buf(buf, &chan->alloc_size); |
b86ff981a [PATCH] relay: mi... |
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
if (!buf->start) goto free_buf; buf->chan = chan; kref_get(&buf->chan->kref); return buf; free_buf: kfree(buf->padding); kfree(buf); return NULL; } /** * relay_destroy_channel - free the channel struct |
4c78a6639 [PATCH] kernel-do... |
186 |
* @kref: target kernel reference that contains the relay channel |
b86ff981a [PATCH] relay: mi... |
187 188 189 |
* * Should only be called from kref_put(). */ |
01c55ed32 kernel/relay.c: m... |
190 |
static void relay_destroy_channel(struct kref *kref) |
b86ff981a [PATCH] relay: mi... |
191 192 |
{ struct rchan *chan = container_of(kref, struct rchan, kref); |
fa2e0d4e4 kernel/relay.c: f... |
193 |
free_percpu(chan->buf); |
b86ff981a [PATCH] relay: mi... |
194 195 196 197 198 199 200 |
kfree(chan); } /** * relay_destroy_buf - destroy an rchan_buf struct and associated buffer * @buf: the buffer struct */ |
01c55ed32 kernel/relay.c: m... |
201 |
static void relay_destroy_buf(struct rchan_buf *buf) |
b86ff981a [PATCH] relay: mi... |
202 203 204 205 206 207 208 209 |
{ struct rchan *chan = buf->chan; unsigned int i; if (likely(buf->start)) { vunmap(buf->start); for (i = 0; i < buf->page_count; i++) __free_page(buf->page_array[i]); |
68ab3d883 relayfs: support ... |
210 |
relay_free_page_array(buf->page_array); |
b86ff981a [PATCH] relay: mi... |
211 |
} |
017c59c04 relay: Use per CP... |
212 |
*per_cpu_ptr(chan->buf, buf->cpu) = NULL; |
b86ff981a [PATCH] relay: mi... |
213 214 215 216 217 218 219 |
kfree(buf->padding); kfree(buf); kref_put(&chan->kref, relay_destroy_channel); } /** * relay_remove_buf - remove a channel buffer |
4c78a6639 [PATCH] kernel-do... |
220 |
* @kref: target kernel reference that contains the relay buffer |
b86ff981a [PATCH] relay: mi... |
221 |
* |
e227867f1 treewide: Fix typ... |
222 |
* Removes the file from the filesystem, which also frees the |
b86ff981a [PATCH] relay: mi... |
223 224 225 |
* rchan_buf_struct and the channel buffer. Should only be called from * kref_put(). */ |
01c55ed32 kernel/relay.c: m... |
226 |
static void relay_remove_buf(struct kref *kref) |
b86ff981a [PATCH] relay: mi... |
227 228 |
{ struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref); |
b86ff981a [PATCH] relay: mi... |
229 230 231 232 233 234 235 236 237 |
relay_destroy_buf(buf); } /** * relay_buf_empty - boolean, is the channel buffer empty? * @buf: channel buffer * * Returns 1 if the buffer is empty, 0 otherwise. */ |
01c55ed32 kernel/relay.c: m... |
238 |
static int relay_buf_empty(struct rchan_buf *buf) |
b86ff981a [PATCH] relay: mi... |
239 240 241 |
{ return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1; } |
b86ff981a [PATCH] relay: mi... |
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
/** * relay_buf_full - boolean, is the channel buffer full? * @buf: channel buffer * * Returns 1 if the buffer is full, 0 otherwise. */ int relay_buf_full(struct rchan_buf *buf) { size_t ready = buf->subbufs_produced - buf->subbufs_consumed; return (ready >= buf->chan->n_subbufs) ? 1 : 0; } EXPORT_SYMBOL_GPL(relay_buf_full); /* * High-level relay kernel API and associated functions. */ /* * rchan_callback implementations defining default channel behavior. Used * in place of corresponding NULL values in client callback struct. */ /* * subbuf_start() default callback. Does nothing. */ static int subbuf_start_default_callback (struct rchan_buf *buf, void *subbuf, void *prev_subbuf, size_t prev_padding) { if (relay_buf_full(buf)) return 0; return 1; } /* * buf_mapped() default callback. Does nothing. */ static void buf_mapped_default_callback(struct rchan_buf *buf, struct file *filp) { } /* * buf_unmapped() default callback. Does nothing. */ static void buf_unmapped_default_callback(struct rchan_buf *buf, struct file *filp) { } /* * create_buf_file_create() default callback. Does nothing. */ static struct dentry *create_buf_file_default_callback(const char *filename, struct dentry *parent, |
f4ae40a6a switch debugfs to... |
300 |
umode_t mode, |
b86ff981a [PATCH] relay: mi... |
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
struct rchan_buf *buf, int *is_global) { return NULL; } /* * remove_buf_file() default callback. Does nothing. */ static int remove_buf_file_default_callback(struct dentry *dentry) { return -EINVAL; } /* relay channel default callbacks */ static struct rchan_callbacks default_channel_callbacks = { .subbuf_start = subbuf_start_default_callback, .buf_mapped = buf_mapped_default_callback, .buf_unmapped = buf_unmapped_default_callback, .create_buf_file = create_buf_file_default_callback, .remove_buf_file = remove_buf_file_default_callback, }; /** * wakeup_readers - wake up readers waiting on a channel |
26b5679e4 relay: Use irq_wo... |
326 |
* @work: contains the channel buffer |
b86ff981a [PATCH] relay: mi... |
327 |
* |
26b5679e4 relay: Use irq_wo... |
328 |
* This is the function used to defer reader waking |
b86ff981a [PATCH] relay: mi... |
329 |
*/ |
26b5679e4 relay: Use irq_wo... |
330 |
static void wakeup_readers(struct irq_work *work) |
b86ff981a [PATCH] relay: mi... |
331 |
{ |
26b5679e4 relay: Use irq_wo... |
332 333 334 |
struct rchan_buf *buf; buf = container_of(work, struct rchan_buf, wakeup_work); |
b86ff981a [PATCH] relay: mi... |
335 336 337 338 339 340 341 342 |
wake_up_interruptible(&buf->read_wait); } /** * __relay_reset - reset a channel buffer * @buf: the channel buffer * @init: 1 if this is a first-time initialization * |
72fd4a35a [PATCH] Numerous ... |
343 |
* See relay_reset() for description of effect. |
b86ff981a [PATCH] relay: mi... |
344 |
*/ |
192636ad9 [PATCH] relay: re... |
345 |
static void __relay_reset(struct rchan_buf *buf, unsigned int init) |
b86ff981a [PATCH] relay: mi... |
346 347 348 349 350 351 |
{ size_t i; if (init) { init_waitqueue_head(&buf->read_wait); kref_init(&buf->kref); |
26b5679e4 relay: Use irq_wo... |
352 353 354 355 |
init_irq_work(&buf->wakeup_work, wakeup_readers); } else { irq_work_sync(&buf->wakeup_work); } |
b86ff981a [PATCH] relay: mi... |
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
buf->subbufs_produced = 0; buf->subbufs_consumed = 0; buf->bytes_consumed = 0; buf->finalized = 0; buf->data = buf->start; buf->offset = 0; for (i = 0; i < buf->chan->n_subbufs; i++) buf->padding[i] = 0; buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0); } /** * relay_reset - reset the channel * @chan: the channel * * This has the effect of erasing all data from all channel buffers * and restarting the channel in its initial state. The buffers * are not freed, so any mappings are still in effect. * |
72fd4a35a [PATCH] Numerous ... |
378 |
* NOTE. Care should be taken that the channel isn't actually |
b86ff981a [PATCH] relay: mi... |
379 380 381 382 |
* being used by anything when this call is made. */ void relay_reset(struct rchan *chan) { |
017c59c04 relay: Use per CP... |
383 |
struct rchan_buf *buf; |
b86ff981a [PATCH] relay: mi... |
384 |
unsigned int i; |
b86ff981a [PATCH] relay: mi... |
385 386 387 |
if (!chan) return; |
017c59c04 relay: Use per CP... |
388 389 |
if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { __relay_reset(buf, 0); |
23c887522 [PATCH] Relay: ad... |
390 |
return; |
b86ff981a [PATCH] relay: mi... |
391 |
} |
23c887522 [PATCH] Relay: ad... |
392 393 |
mutex_lock(&relay_channels_mutex); |
98ba4031a relay: fix cpu of... |
394 |
for_each_possible_cpu(i) |
017c59c04 relay: Use per CP... |
395 396 |
if ((buf = *per_cpu_ptr(chan->buf, i))) __relay_reset(buf, 0); |
23c887522 [PATCH] Relay: ad... |
397 |
mutex_unlock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
398 399 |
} EXPORT_SYMBOL_GPL(relay_reset); |
20d8b67c0 relay: add buffer... |
400 401 402 403 |
static inline void relay_set_buf_dentry(struct rchan_buf *buf, struct dentry *dentry) { buf->dentry = dentry; |
7682c9184 VFS: kernel/: d_i... |
404 |
d_inode(buf->dentry)->i_size = buf->early_bytes; |
20d8b67c0 relay: add buffer... |
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
} static struct dentry *relay_create_buf_file(struct rchan *chan, struct rchan_buf *buf, unsigned int cpu) { struct dentry *dentry; char *tmpname; tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); if (!tmpname) return NULL; snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu); /* Create file in fs */ dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, buf, &chan->is_global); |
2c1cf00ee relay: check retu... |
423 424 |
if (IS_ERR(dentry)) dentry = NULL; |
20d8b67c0 relay: add buffer... |
425 426 427 428 429 |
kfree(tmpname); return dentry; } |
4c78a6639 [PATCH] kernel-do... |
430 |
/* |
b86ff981a [PATCH] relay: mi... |
431 432 |
* relay_open_buf - create a new relay channel buffer * |
23c887522 [PATCH] Relay: ad... |
433 |
* used by relay_open() and CPU hotplug. |
b86ff981a [PATCH] relay: mi... |
434 |
*/ |
23c887522 [PATCH] Relay: ad... |
435 |
static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu) |
b86ff981a [PATCH] relay: mi... |
436 |
{ |
23c887522 [PATCH] Relay: ad... |
437 |
struct rchan_buf *buf = NULL; |
b86ff981a [PATCH] relay: mi... |
438 |
struct dentry *dentry; |
23c887522 [PATCH] Relay: ad... |
439 |
if (chan->is_global) |
017c59c04 relay: Use per CP... |
440 |
return *per_cpu_ptr(chan->buf, 0); |
b86ff981a [PATCH] relay: mi... |
441 442 443 |
buf = relay_create_buf(chan); if (!buf) |
20d8b67c0 relay: add buffer... |
444 445 446 447 448 449 450 |
return NULL; if (chan->has_base_filename) { dentry = relay_create_buf_file(chan, buf, cpu); if (!dentry) goto free_buf; relay_set_buf_dentry(buf, dentry); |
59dbb2a06 relay: add global... |
451 452 453 454 455 |
} else { /* Only retrieve global info, nothing more, nothing less */ dentry = chan->cb->create_buf_file(NULL, NULL, S_IRUSR, buf, &chan->is_global); |
2c1cf00ee relay: check retu... |
456 |
if (IS_ERR_OR_NULL(dentry)) |
59dbb2a06 relay: add global... |
457 |
goto free_buf; |
20d8b67c0 relay: add buffer... |
458 |
} |
23c887522 [PATCH] Relay: ad... |
459 460 461 |
buf->cpu = cpu; __relay_reset(buf, 1); |
b86ff981a [PATCH] relay: mi... |
462 |
|
23c887522 [PATCH] Relay: ad... |
463 |
if(chan->is_global) { |
017c59c04 relay: Use per CP... |
464 |
*per_cpu_ptr(chan->buf, 0) = buf; |
23c887522 [PATCH] Relay: ad... |
465 466 |
buf->cpu = 0; } |
20d8b67c0 relay: add buffer... |
467 |
return buf; |
23c887522 [PATCH] Relay: ad... |
468 469 470 |
free_buf: relay_destroy_buf(buf); |
20d8b67c0 relay: add buffer... |
471 |
return NULL; |
b86ff981a [PATCH] relay: mi... |
472 473 474 475 476 477 478 479 480 481 |
} /** * relay_close_buf - close a channel buffer * @buf: channel buffer * * Marks the buffer finalized and restores the default callbacks. * The channel buffer and channel buffer data structure are then freed * automatically when the last reference is given up. */ |
192636ad9 [PATCH] relay: re... |
482 |
static void relay_close_buf(struct rchan_buf *buf) |
b86ff981a [PATCH] relay: mi... |
483 484 |
{ buf->finalized = 1; |
26b5679e4 relay: Use irq_wo... |
485 |
irq_work_sync(&buf->wakeup_work); |
b8d4a5bf6 relay: move remov... |
486 |
buf->chan->cb->remove_buf_file(buf->dentry); |
b86ff981a [PATCH] relay: mi... |
487 488 |
kref_put(&buf->kref, relay_remove_buf); } |
192636ad9 [PATCH] relay: re... |
489 |
static void setup_callbacks(struct rchan *chan, |
b86ff981a [PATCH] relay: mi... |
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
struct rchan_callbacks *cb) { if (!cb) { chan->cb = &default_channel_callbacks; return; } if (!cb->subbuf_start) cb->subbuf_start = subbuf_start_default_callback; if (!cb->buf_mapped) cb->buf_mapped = buf_mapped_default_callback; if (!cb->buf_unmapped) cb->buf_unmapped = buf_unmapped_default_callback; if (!cb->create_buf_file) cb->create_buf_file = create_buf_file_default_callback; if (!cb->remove_buf_file) cb->remove_buf_file = remove_buf_file_default_callback; chan->cb = cb; } |
e6d4989a9 relayfs: Convert ... |
509 |
int relay_prepare_cpu(unsigned int cpu) |
23c887522 [PATCH] Relay: ad... |
510 |
{ |
23c887522 [PATCH] Relay: ad... |
511 |
struct rchan *chan; |
017c59c04 relay: Use per CP... |
512 |
struct rchan_buf *buf; |
23c887522 [PATCH] Relay: ad... |
513 |
|
e6d4989a9 relayfs: Convert ... |
514 515 516 517 518 519 520 521 522 523 |
mutex_lock(&relay_channels_mutex); list_for_each_entry(chan, &relay_channels, list) { if ((buf = *per_cpu_ptr(chan->buf, cpu))) continue; buf = relay_open_buf(chan, cpu); if (!buf) { pr_err("relay: cpu %d buffer creation failed ", cpu); mutex_unlock(&relay_channels_mutex); return -ENOMEM; |
23c887522 [PATCH] Relay: ad... |
524 |
} |
e6d4989a9 relayfs: Convert ... |
525 |
*per_cpu_ptr(chan->buf, cpu) = buf; |
23c887522 [PATCH] Relay: ad... |
526 |
} |
e6d4989a9 relayfs: Convert ... |
527 528 |
mutex_unlock(&relay_channels_mutex); return 0; |
23c887522 [PATCH] Relay: ad... |
529 530 531 |
} /** |
b86ff981a [PATCH] relay: mi... |
532 |
* relay_open - create a new relay channel |
20d8b67c0 relay: add buffer... |
533 534 |
* @base_filename: base name of files to create, %NULL for buffering only * @parent: dentry of parent directory, %NULL for root directory or buffer |
b86ff981a [PATCH] relay: mi... |
535 536 537 |
* @subbuf_size: size of sub-buffers * @n_subbufs: number of sub-buffers * @cb: client callback functions |
23c887522 [PATCH] Relay: ad... |
538 |
* @private_data: user-defined data |
b86ff981a [PATCH] relay: mi... |
539 |
* |
4c78a6639 [PATCH] kernel-do... |
540 |
* Returns channel pointer if successful, %NULL otherwise. |
b86ff981a [PATCH] relay: mi... |
541 542 543 544 |
* * Creates a channel buffer for each cpu using the sizes and * attributes specified. The created channel buffer files * will be named base_filename0...base_filenameN-1. File |
72fd4a35a [PATCH] Numerous ... |
545 |
* permissions will be %S_IRUSR. |
59dbb2a06 relay: add global... |
546 547 548 549 |
* * If opening a buffer (@parent = NULL) that you later wish to register * in a filesystem, call relay_late_setup_files() once the @parent dentry * is available. |
b86ff981a [PATCH] relay: mi... |
550 551 552 553 554 |
*/ struct rchan *relay_open(const char *base_filename, struct dentry *parent, size_t subbuf_size, size_t n_subbufs, |
23c887522 [PATCH] Relay: ad... |
555 556 |
struct rchan_callbacks *cb, void *private_data) |
b86ff981a [PATCH] relay: mi... |
557 558 559 |
{ unsigned int i; struct rchan *chan; |
017c59c04 relay: Use per CP... |
560 |
struct rchan_buf *buf; |
b86ff981a [PATCH] relay: mi... |
561 562 563 |
if (!(subbuf_size && n_subbufs)) return NULL; |
f6302f1bc relay: prevent in... |
564 565 |
if (subbuf_size > UINT_MAX / n_subbufs) return NULL; |
b86ff981a [PATCH] relay: mi... |
566 |
|
cd8612808 [PATCH] Fix numer... |
567 |
chan = kzalloc(sizeof(struct rchan), GFP_KERNEL); |
b86ff981a [PATCH] relay: mi... |
568 569 |
if (!chan) return NULL; |
017c59c04 relay: Use per CP... |
570 |
chan->buf = alloc_percpu(struct rchan_buf *); |
1c44e6e09 kernel/relay.c: h... |
571 572 573 574 |
if (!chan->buf) { kfree(chan); return NULL; } |
b86ff981a [PATCH] relay: mi... |
575 576 577 |
chan->version = RELAYFS_CHANNEL_VERSION; chan->n_subbufs = n_subbufs; chan->subbuf_size = subbuf_size; |
a05342cbd relay: use macro ... |
578 |
chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs); |
23c887522 [PATCH] Relay: ad... |
579 580 |
chan->parent = parent; chan->private_data = private_data; |
20d8b67c0 relay: add buffer... |
581 582 583 584 |
if (base_filename) { chan->has_base_filename = 1; strlcpy(chan->base_filename, base_filename, NAME_MAX); } |
b86ff981a [PATCH] relay: mi... |
585 586 |
setup_callbacks(chan, cb); kref_init(&chan->kref); |
23c887522 [PATCH] Relay: ad... |
587 |
mutex_lock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
588 |
for_each_online_cpu(i) { |
017c59c04 relay: Use per CP... |
589 590 |
buf = relay_open_buf(chan, i); if (!buf) |
b86ff981a [PATCH] relay: mi... |
591 |
goto free_bufs; |
017c59c04 relay: Use per CP... |
592 |
*per_cpu_ptr(chan->buf, i) = buf; |
b86ff981a [PATCH] relay: mi... |
593 |
} |
23c887522 [PATCH] Relay: ad... |
594 595 |
list_add(&chan->list, &relay_channels); mutex_unlock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
596 |
|
b86ff981a [PATCH] relay: mi... |
597 598 599 |
return chan; free_bufs: |
98ba4031a relay: fix cpu of... |
600 |
for_each_possible_cpu(i) { |
017c59c04 relay: Use per CP... |
601 602 |
if ((buf = *per_cpu_ptr(chan->buf, i))) relay_close_buf(buf); |
b86ff981a [PATCH] relay: mi... |
603 |
} |
b86ff981a [PATCH] relay: mi... |
604 |
|
b86ff981a [PATCH] relay: mi... |
605 |
kref_put(&chan->kref, relay_destroy_channel); |
23c887522 [PATCH] Relay: ad... |
606 |
mutex_unlock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
607 608 609 |
return NULL; } EXPORT_SYMBOL_GPL(relay_open); |
20d8b67c0 relay: add buffer... |
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
struct rchan_percpu_buf_dispatcher { struct rchan_buf *buf; struct dentry *dentry; }; /* Called in atomic context. */ static void __relay_set_buf_dentry(void *info) { struct rchan_percpu_buf_dispatcher *p = info; relay_set_buf_dentry(p->buf, p->dentry); } /** * relay_late_setup_files - triggers file creation * @chan: channel to operate on * @base_filename: base name of files to create * @parent: dentry of parent directory, %NULL for root directory * * Returns 0 if successful, non-zero otherwise. * |
59dbb2a06 relay: add global... |
631 632 633 634 635 636 |
* Use to setup files for a previously buffer-only channel created * by relay_open() with a NULL parent dentry. * * For example, this is useful for perfomring early tracing in kernel, * before VFS is up and then exposing the early results once the dentry * is available. |
20d8b67c0 relay: add buffer... |
637 638 639 640 641 642 643 644 645 |
*/ int relay_late_setup_files(struct rchan *chan, const char *base_filename, struct dentry *parent) { int err = 0; unsigned int i, curr_cpu; unsigned long flags; struct dentry *dentry; |
017c59c04 relay: Use per CP... |
646 |
struct rchan_buf *buf; |
20d8b67c0 relay: add buffer... |
647 648 649 650 651 652 653 654 655 |
struct rchan_percpu_buf_dispatcher disp; if (!chan || !base_filename) return -EINVAL; strlcpy(chan->base_filename, base_filename, NAME_MAX); mutex_lock(&relay_channels_mutex); /* Is chan already set up? */ |
b786c6a98 relay: fix lock i... |
656 657 |
if (unlikely(chan->has_base_filename)) { mutex_unlock(&relay_channels_mutex); |
20d8b67c0 relay: add buffer... |
658 |
return -EEXIST; |
b786c6a98 relay: fix lock i... |
659 |
} |
20d8b67c0 relay: add buffer... |
660 661 |
chan->has_base_filename = 1; chan->parent = parent; |
59dbb2a06 relay: add global... |
662 663 664 |
if (chan->is_global) { err = -EINVAL; |
017c59c04 relay: Use per CP... |
665 666 667 |
buf = *per_cpu_ptr(chan->buf, 0); if (!WARN_ON_ONCE(!buf)) { dentry = relay_create_buf_file(chan, buf, 0); |
59dbb2a06 relay: add global... |
668 |
if (dentry && !WARN_ON_ONCE(!chan->is_global)) { |
017c59c04 relay: Use per CP... |
669 |
relay_set_buf_dentry(buf, dentry); |
59dbb2a06 relay: add global... |
670 671 672 673 674 675 |
err = 0; } } mutex_unlock(&relay_channels_mutex); return err; } |
20d8b67c0 relay: add buffer... |
676 677 678 679 680 681 682 |
curr_cpu = get_cpu(); /* * The CPU hotplug notifier ran before us and created buffers with * no files associated. So it's safe to call relay_setup_buf_file() * on all currently online CPUs. */ for_each_online_cpu(i) { |
017c59c04 relay: Use per CP... |
683 684 |
buf = *per_cpu_ptr(chan->buf, i); if (unlikely(!buf)) { |
7a51cffbd relayfs: replace ... |
685 686 |
WARN_ONCE(1, KERN_ERR "CPU has no buffer! "); |
20d8b67c0 relay: add buffer... |
687 688 689 |
err = -EINVAL; break; } |
017c59c04 relay: Use per CP... |
690 |
dentry = relay_create_buf_file(chan, buf, i); |
20d8b67c0 relay: add buffer... |
691 692 693 694 695 696 697 |
if (unlikely(!dentry)) { err = -EINVAL; break; } if (curr_cpu == i) { local_irq_save(flags); |
017c59c04 relay: Use per CP... |
698 |
relay_set_buf_dentry(buf, dentry); |
20d8b67c0 relay: add buffer... |
699 700 |
local_irq_restore(flags); } else { |
017c59c04 relay: Use per CP... |
701 |
disp.buf = buf; |
20d8b67c0 relay: add buffer... |
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
disp.dentry = dentry; smp_mb(); /* relay_channels_mutex must be held, so wait. */ err = smp_call_function_single(i, __relay_set_buf_dentry, &disp, 1); } if (unlikely(err)) break; } put_cpu(); mutex_unlock(&relay_channels_mutex); return err; } |
59dbb2a06 relay: add global... |
717 |
EXPORT_SYMBOL_GPL(relay_late_setup_files); |
20d8b67c0 relay: add buffer... |
718 |
|
b86ff981a [PATCH] relay: mi... |
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
/** * relay_switch_subbuf - switch to a new sub-buffer * @buf: channel buffer * @length: size of current event * * Returns either the length passed in or 0 if full. * * Performs sub-buffer-switch tasks such as invoking callbacks, * updating padding counts, waking up readers, etc. */ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length) { void *old, *new; size_t old_subbuf, new_subbuf; if (unlikely(length > buf->chan->subbuf_size)) goto toobig; if (buf->offset != buf->chan->subbuf_size + 1) { buf->prev_padding = buf->chan->subbuf_size - buf->offset; old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; buf->padding[old_subbuf] = buf->prev_padding; buf->subbufs_produced++; |
20d8b67c0 relay: add buffer... |
742 |
if (buf->dentry) |
7682c9184 VFS: kernel/: d_i... |
743 |
d_inode(buf->dentry)->i_size += |
20d8b67c0 relay: add buffer... |
744 745 746 747 748 |
buf->chan->subbuf_size - buf->padding[old_subbuf]; else buf->early_bytes += buf->chan->subbuf_size - buf->padding[old_subbuf]; |
221415d76 [PATCH] relay: ad... |
749 |
smp_mb(); |
26b5679e4 relay: Use irq_wo... |
750 |
if (waitqueue_active(&buf->read_wait)) { |
7c9cb3830 relay: use plain ... |
751 752 753 754 755 756 |
/* * Calling wake_up_interruptible() from here * will deadlock if we happen to be logging * from the scheduler (trying to re-grab * rq->lock), so defer it. */ |
26b5679e4 relay: Use irq_wo... |
757 758 |
irq_work_queue(&buf->wakeup_work); } |
b86ff981a [PATCH] relay: mi... |
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 |
} old = buf->data; new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs; new = buf->start + new_subbuf * buf->chan->subbuf_size; buf->offset = 0; if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) { buf->offset = buf->chan->subbuf_size + 1; return 0; } buf->data = new; buf->padding[new_subbuf] = 0; if (unlikely(length + buf->offset > buf->chan->subbuf_size)) goto toobig; return length; toobig: buf->chan->last_toobig = length; return 0; } EXPORT_SYMBOL_GPL(relay_switch_subbuf); /** * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count * @chan: the channel * @cpu: the cpu associated with the channel buffer to update * @subbufs_consumed: number of sub-buffers to add to current buf's count * * Adds to the channel buffer's consumed sub-buffer count. * subbufs_consumed should be the number of sub-buffers newly consumed, * not the total consumed. * |
72fd4a35a [PATCH] Numerous ... |
793 |
* NOTE. Kernel clients don't need to call this function if the channel |
b86ff981a [PATCH] relay: mi... |
794 795 796 797 798 799 800 |
* mode is 'overwrite'. */ void relay_subbufs_consumed(struct rchan *chan, unsigned int cpu, size_t subbufs_consumed) { struct rchan_buf *buf; |
9a29d0fbc relay: check arra... |
801 |
if (!chan || cpu >= NR_CPUS) |
b86ff981a [PATCH] relay: mi... |
802 |
return; |
017c59c04 relay: Use per CP... |
803 |
buf = *per_cpu_ptr(chan->buf, cpu); |
9a29d0fbc relay: check arra... |
804 |
if (!buf || subbufs_consumed > chan->n_subbufs) |
b86ff981a [PATCH] relay: mi... |
805 |
return; |
2c53d9109 relay: fix for po... |
806 |
if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed) |
b86ff981a [PATCH] relay: mi... |
807 |
buf->subbufs_consumed = buf->subbufs_produced; |
2c53d9109 relay: fix for po... |
808 809 |
else buf->subbufs_consumed += subbufs_consumed; |
b86ff981a [PATCH] relay: mi... |
810 811 812 813 814 815 816 817 818 819 820 |
} EXPORT_SYMBOL_GPL(relay_subbufs_consumed); /** * relay_close - close the channel * @chan: the channel * * Closes all channel buffers and frees the channel. */ void relay_close(struct rchan *chan) { |
017c59c04 relay: Use per CP... |
821 |
struct rchan_buf *buf; |
b86ff981a [PATCH] relay: mi... |
822 |
unsigned int i; |
b86ff981a [PATCH] relay: mi... |
823 824 825 |
if (!chan) return; |
23c887522 [PATCH] Relay: ad... |
826 |
mutex_lock(&relay_channels_mutex); |
017c59c04 relay: Use per CP... |
827 828 |
if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) relay_close_buf(buf); |
23c887522 [PATCH] Relay: ad... |
829 830 |
else for_each_possible_cpu(i) |
017c59c04 relay: Use per CP... |
831 832 |
if ((buf = *per_cpu_ptr(chan->buf, i))) relay_close_buf(buf); |
b86ff981a [PATCH] relay: mi... |
833 834 835 |
if (chan->last_toobig) printk(KERN_WARNING "relay: one or more items not logged " |
5b5e0928f lib/vsprintf.c: r... |
836 837 |
"[item size (%zd) > sub-buffer size (%zd)] ", |
b86ff981a [PATCH] relay: mi... |
838 |
chan->last_toobig, chan->subbuf_size); |
23c887522 [PATCH] Relay: ad... |
839 |
list_del(&chan->list); |
b86ff981a [PATCH] relay: mi... |
840 |
kref_put(&chan->kref, relay_destroy_channel); |
23c887522 [PATCH] Relay: ad... |
841 |
mutex_unlock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
842 843 844 845 846 847 848 |
} EXPORT_SYMBOL_GPL(relay_close); /** * relay_flush - close the channel * @chan: the channel * |
4c78a6639 [PATCH] kernel-do... |
849 |
* Flushes all channel buffers, i.e. forces buffer switch. |
b86ff981a [PATCH] relay: mi... |
850 851 852 |
*/ void relay_flush(struct rchan *chan) { |
017c59c04 relay: Use per CP... |
853 |
struct rchan_buf *buf; |
b86ff981a [PATCH] relay: mi... |
854 |
unsigned int i; |
b86ff981a [PATCH] relay: mi... |
855 856 857 |
if (!chan) return; |
017c59c04 relay: Use per CP... |
858 859 |
if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) { relay_switch_subbuf(buf, 0); |
23c887522 [PATCH] Relay: ad... |
860 |
return; |
b86ff981a [PATCH] relay: mi... |
861 |
} |
23c887522 [PATCH] Relay: ad... |
862 863 864 |
mutex_lock(&relay_channels_mutex); for_each_possible_cpu(i) |
017c59c04 relay: Use per CP... |
865 866 |
if ((buf = *per_cpu_ptr(chan->buf, i))) relay_switch_subbuf(buf, 0); |
23c887522 [PATCH] Relay: ad... |
867 |
mutex_unlock(&relay_channels_mutex); |
b86ff981a [PATCH] relay: mi... |
868 869 870 871 872 873 874 875 876 877 878 879 |
} EXPORT_SYMBOL_GPL(relay_flush); /** * relay_file_open - open file op for relay files * @inode: the inode * @filp: the file * * Increments the channel buffer refcount. */ static int relay_file_open(struct inode *inode, struct file *filp) { |
8e18e2941 [PATCH] inode_die... |
880 |
struct rchan_buf *buf = inode->i_private; |
b86ff981a [PATCH] relay: mi... |
881 882 |
kref_get(&buf->kref); filp->private_data = buf; |
37529fe9f set relay file ca... |
883 |
return nonseekable_open(inode, filp); |
b86ff981a [PATCH] relay: mi... |
884 885 886 887 888 889 890 |
} /** * relay_file_mmap - mmap file op for relay files * @filp: the file * @vma: the vma describing what to map * |
72fd4a35a [PATCH] Numerous ... |
891 |
* Calls upon relay_mmap_buf() to map the file into user space. |
b86ff981a [PATCH] relay: mi... |
892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
*/ static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma) { struct rchan_buf *buf = filp->private_data; return relay_mmap_buf(buf, vma); } /** * relay_file_poll - poll file op for relay files * @filp: the file * @wait: poll table * * Poll implemention. */ |
9dd957485 ipc, kernel, mm: ... |
906 |
static __poll_t relay_file_poll(struct file *filp, poll_table *wait) |
b86ff981a [PATCH] relay: mi... |
907 |
{ |
9dd957485 ipc, kernel, mm: ... |
908 |
__poll_t mask = 0; |
b86ff981a [PATCH] relay: mi... |
909 910 911 |
struct rchan_buf *buf = filp->private_data; if (buf->finalized) |
a9a08845e vfs: do bulk POLL... |
912 |
return EPOLLERR; |
b86ff981a [PATCH] relay: mi... |
913 914 915 916 |
if (filp->f_mode & FMODE_READ) { poll_wait(filp, &buf->read_wait, wait); if (!relay_buf_empty(buf)) |
a9a08845e vfs: do bulk POLL... |
917 |
mask |= EPOLLIN | EPOLLRDNORM; |
b86ff981a [PATCH] relay: mi... |
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
} return mask; } /** * relay_file_release - release file op for relay files * @inode: the inode * @filp: the file * * Decrements the channel refcount, as the filesystem is * no longer using it. */ static int relay_file_release(struct inode *inode, struct file *filp) { struct rchan_buf *buf = filp->private_data; kref_put(&buf->kref, relay_remove_buf); return 0; } |
4c78a6639 [PATCH] kernel-do... |
938 |
/* |
b86ff981a [PATCH] relay: mi... |
939 940 941 942 943 944 945 946 947 |
* relay_file_read_consume - update the consumed count for the buffer */ static void relay_file_read_consume(struct rchan_buf *buf, size_t read_pos, size_t bytes_consumed) { size_t subbuf_size = buf->chan->subbuf_size; size_t n_subbufs = buf->chan->n_subbufs; size_t read_subbuf; |
321944503 relay: fix "full ... |
948 949 950 |
if (buf->subbufs_produced == buf->subbufs_consumed && buf->offset == buf->bytes_consumed) return; |
b86ff981a [PATCH] relay: mi... |
951 952 953 954 955 956 |
if (buf->bytes_consumed + bytes_consumed > subbuf_size) { relay_subbufs_consumed(buf->chan, buf->cpu, 1); buf->bytes_consumed = 0; } buf->bytes_consumed += bytes_consumed; |
a66e356c0 relayfs: fix over... |
957 958 959 960 |
if (!read_pos) read_subbuf = buf->subbufs_consumed % n_subbufs; else read_subbuf = read_pos / buf->chan->subbuf_size; |
b86ff981a [PATCH] relay: mi... |
961 962 963 964 965 966 967 968 |
if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) { if ((read_subbuf == buf->subbufs_produced % n_subbufs) && (buf->offset == subbuf_size)) return; relay_subbufs_consumed(buf->chan, buf->cpu, 1); buf->bytes_consumed = 0; } } |
4c78a6639 [PATCH] kernel-do... |
969 |
/* |
b86ff981a [PATCH] relay: mi... |
970 971 972 973 |
* relay_file_read_avail - boolean, are there unconsumed bytes available? */ static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos) { |
b86ff981a [PATCH] relay: mi... |
974 975 |
size_t subbuf_size = buf->chan->subbuf_size; size_t n_subbufs = buf->chan->n_subbufs; |
221415d76 [PATCH] relay: ad... |
976 977 |
size_t produced = buf->subbufs_produced; size_t consumed = buf->subbufs_consumed; |
b86ff981a [PATCH] relay: mi... |
978 |
|
221415d76 [PATCH] relay: ad... |
979 |
relay_file_read_consume(buf, read_pos, 0); |
b86ff981a [PATCH] relay: mi... |
980 |
|
321944503 relay: fix "full ... |
981 |
consumed = buf->subbufs_consumed; |
221415d76 [PATCH] relay: ad... |
982 983 984 985 |
if (unlikely(buf->offset > subbuf_size)) { if (produced == consumed) return 0; return 1; |
b86ff981a [PATCH] relay: mi... |
986 |
} |
221415d76 [PATCH] relay: ad... |
987 |
if (unlikely(produced - consumed >= n_subbufs)) { |
a66e356c0 relayfs: fix over... |
988 |
consumed = produced - n_subbufs + 1; |
221415d76 [PATCH] relay: ad... |
989 |
buf->subbufs_consumed = consumed; |
a66e356c0 relayfs: fix over... |
990 |
buf->bytes_consumed = 0; |
221415d76 [PATCH] relay: ad... |
991 |
} |
1bfbc608b whitespace fixes:... |
992 |
|
221415d76 [PATCH] relay: ad... |
993 994 995 996 997 |
produced = (produced % n_subbufs) * subbuf_size + buf->offset; consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed; if (consumed > produced) produced += n_subbufs * subbuf_size; |
1bfbc608b whitespace fixes:... |
998 |
|
321944503 relay: fix "full ... |
999 1000 1001 1002 |
if (consumed == produced) { if (buf->offset == subbuf_size && buf->subbufs_produced > buf->subbufs_consumed) return 1; |
b86ff981a [PATCH] relay: mi... |
1003 |
return 0; |
321944503 relay: fix "full ... |
1004 |
} |
b86ff981a [PATCH] relay: mi... |
1005 |
|
b86ff981a [PATCH] relay: mi... |
1006 1007 1008 1009 1010 |
return 1; } /** * relay_file_read_subbuf_avail - return bytes available in sub-buffer |
4c78a6639 [PATCH] kernel-do... |
1011 1012 |
* @read_pos: file read position * @buf: relay channel buffer |
b86ff981a [PATCH] relay: mi... |
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
*/ static size_t relay_file_read_subbuf_avail(size_t read_pos, struct rchan_buf *buf) { size_t padding, avail = 0; size_t read_subbuf, read_offset, write_subbuf, write_offset; size_t subbuf_size = buf->chan->subbuf_size; write_subbuf = (buf->data - buf->start) / subbuf_size; write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; read_subbuf = read_pos / subbuf_size; read_offset = read_pos % subbuf_size; padding = buf->padding[read_subbuf]; if (read_subbuf == write_subbuf) { if (read_offset + padding < write_offset) avail = write_offset - (read_offset + padding); } else avail = (subbuf_size - padding) - read_offset; return avail; } /** * relay_file_read_start_pos - find the first available byte to read |
4c78a6639 [PATCH] kernel-do... |
1038 1039 |
* @read_pos: file read position * @buf: relay channel buffer |
b86ff981a [PATCH] relay: mi... |
1040 |
* |
72fd4a35a [PATCH] Numerous ... |
1041 |
* If the @read_pos is in the middle of padding, return the |
b86ff981a [PATCH] relay: mi... |
1042 1043 1044 1045 1046 1047 1048 1049 1050 |
* position of the first actually available byte, otherwise * return the original value. */ static size_t relay_file_read_start_pos(size_t read_pos, struct rchan_buf *buf) { size_t read_subbuf, padding, padding_start, padding_end; size_t subbuf_size = buf->chan->subbuf_size; size_t n_subbufs = buf->chan->n_subbufs; |
8d62fdebd relay file read: ... |
1051 |
size_t consumed = buf->subbufs_consumed % n_subbufs; |
b86ff981a [PATCH] relay: mi... |
1052 |
|
8d62fdebd relay file read: ... |
1053 1054 |
if (!read_pos) read_pos = consumed * subbuf_size + buf->bytes_consumed; |
b86ff981a [PATCH] relay: mi... |
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
read_subbuf = read_pos / subbuf_size; padding = buf->padding[read_subbuf]; padding_start = (read_subbuf + 1) * subbuf_size - padding; padding_end = (read_subbuf + 1) * subbuf_size; if (read_pos >= padding_start && read_pos < padding_end) { read_subbuf = (read_subbuf + 1) % n_subbufs; read_pos = read_subbuf * subbuf_size; } return read_pos; } /** * relay_file_read_end_pos - return the new read position |
4c78a6639 [PATCH] kernel-do... |
1069 1070 1071 |
* @read_pos: file read position * @buf: relay channel buffer * @count: number of bytes to be read |
b86ff981a [PATCH] relay: mi... |
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
*/ static size_t relay_file_read_end_pos(struct rchan_buf *buf, size_t read_pos, size_t count) { size_t read_subbuf, padding, end_pos; size_t subbuf_size = buf->chan->subbuf_size; size_t n_subbufs = buf->chan->n_subbufs; read_subbuf = read_pos / subbuf_size; padding = buf->padding[read_subbuf]; if (read_pos % subbuf_size + count + padding == subbuf_size) end_pos = (read_subbuf + 1) * subbuf_size; else end_pos = read_pos + count; if (end_pos >= subbuf_size * n_subbufs) end_pos = 0; return end_pos; } |
a7c224216 relay: simplify r... |
1092 1093 1094 1095 |
static ssize_t relay_file_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos) |
221415d76 [PATCH] relay: ad... |
1096 |
{ |
6dac40a7c [PATCH] relay: co... |
1097 1098 |
struct rchan_buf *buf = filp->private_data; size_t read_start, avail; |
a7c224216 relay: simplify r... |
1099 |
size_t written = 0; |
6dac40a7c [PATCH] relay: co... |
1100 |
int ret; |
221415d76 [PATCH] relay: ad... |
1101 |
|
a7c224216 relay: simplify r... |
1102 |
if (!count) |
221415d76 [PATCH] relay: ad... |
1103 |
return 0; |
5955102c9 wrappers for ->i_... |
1104 |
inode_lock(file_inode(filp)); |
221415d76 [PATCH] relay: ad... |
1105 |
do { |
a7c224216 relay: simplify r... |
1106 |
void *from; |
6dac40a7c [PATCH] relay: co... |
1107 1108 1109 1110 1111 1112 |
if (!relay_file_read_avail(buf, *ppos)) break; read_start = relay_file_read_start_pos(*ppos, buf); avail = relay_file_read_subbuf_avail(read_start, buf); if (!avail) |
221415d76 [PATCH] relay: ad... |
1113 |
break; |
221415d76 [PATCH] relay: ad... |
1114 |
|
a7c224216 relay: simplify r... |
1115 1116 1117 1118 |
avail = min(count, avail); from = buf->start + read_start; ret = avail; if (copy_to_user(buffer, from, avail)) |
6dac40a7c [PATCH] relay: co... |
1119 |
break; |
a7c224216 relay: simplify r... |
1120 1121 1122 |
buffer += ret; written += ret; count -= ret; |
6dac40a7c [PATCH] relay: co... |
1123 |
|
a7c224216 relay: simplify r... |
1124 1125 1126 1127 |
relay_file_read_consume(buf, read_start, ret); *ppos = relay_file_read_end_pos(buf, read_start, ret); } while (count); inode_unlock(file_inode(filp)); |
6dac40a7c [PATCH] relay: co... |
1128 |
|
a7c224216 relay: simplify r... |
1129 |
return written; |
6dac40a7c [PATCH] relay: co... |
1130 |
} |
1db60cf20 relay: use splice... |
1131 1132 1133 1134 1135 1136 1137 1138 1139 |
static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed) { rbuf->bytes_consumed += bytes_consumed; if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) { relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1); rbuf->bytes_consumed %= rbuf->chan->subbuf_size; } } |
ebf990934 splice: relay sup... |
1140 1141 |
static void relay_pipe_buf_release(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
6dac40a7c [PATCH] relay: co... |
1142 |
{ |
ebf990934 splice: relay sup... |
1143 1144 1145 |
struct rchan_buf *rbuf; rbuf = (struct rchan_buf *)page_private(buf->page); |
1db60cf20 relay: use splice... |
1146 |
relay_consume_bytes(rbuf, buf->private); |
ebf990934 splice: relay sup... |
1147 |
} |
28dfef8fe const: constify r... |
1148 |
static const struct pipe_buf_operations relay_pipe_buf_ops = { |
cac36bb06 pipe: change the ... |
1149 |
.confirm = generic_pipe_buf_confirm, |
ebf990934 splice: relay sup... |
1150 1151 1152 1153 |
.release = relay_pipe_buf_release, .steal = generic_pipe_buf_steal, .get = generic_pipe_buf_get, }; |
5eb7f9fa8 relay: set an spd... |
1154 1155 1156 |
static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i) { } |
d3f35d98b relay: fixup kern... |
1157 |
/* |
ebf990934 splice: relay sup... |
1158 1159 |
* subbuf_splice_actor - splice up to one subbuf's worth of data */ |
5f1664f92 splice: comparing... |
1160 |
static ssize_t subbuf_splice_actor(struct file *in, |
ebf990934 splice: relay sup... |
1161 1162 1163 1164 1165 1166 |
loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags, int *nonpad_ret) { |
5f1664f92 splice: comparing... |
1167 |
unsigned int pidx, poff, total_len, subbuf_pages, nr_pages; |
ebf990934 splice: relay sup... |
1168 1169 |
struct rchan_buf *rbuf = in->private_data; unsigned int subbuf_size = rbuf->chan->subbuf_size; |
24da24de2 relay: fix bogus ... |
1170 1171 1172 |
uint64_t pos = (uint64_t) *ppos; uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size; size_t read_start = (size_t) do_div(pos, alloc_size); |
ebf990934 splice: relay sup... |
1173 1174 1175 |
size_t read_subbuf = read_start / subbuf_size; size_t padding = rbuf->padding[read_subbuf]; size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding; |
35f3d14db pipe: add support... |
1176 1177 |
struct page *pages[PIPE_DEF_BUFFERS]; struct partial_page partial[PIPE_DEF_BUFFERS]; |
1db60cf20 relay: use splice... |
1178 1179 1180 |
struct splice_pipe_desc spd = { .pages = pages, .nr_pages = 0, |
047fe3605 splice: fix racy ... |
1181 |
.nr_pages_max = PIPE_DEF_BUFFERS, |
1db60cf20 relay: use splice... |
1182 |
.partial = partial, |
1db60cf20 relay: use splice... |
1183 |
.ops = &relay_pipe_buf_ops, |
5eb7f9fa8 relay: set an spd... |
1184 |
.spd_release = relay_page_release, |
1db60cf20 relay: use splice... |
1185 |
}; |
5f1664f92 splice: comparing... |
1186 |
ssize_t ret; |
ebf990934 splice: relay sup... |
1187 1188 1189 |
if (rbuf->subbufs_produced == rbuf->subbufs_consumed) return 0; |
35f3d14db pipe: add support... |
1190 1191 |
if (splice_grow_spd(pipe, &spd)) return -ENOMEM; |
ebf990934 splice: relay sup... |
1192 |
|
1db60cf20 relay: use splice... |
1193 1194 1195 1196 1197 |
/* * Adjust read len, if longer than what is available */ if (len > (subbuf_size - read_start % subbuf_size)) len = subbuf_size - read_start % subbuf_size; |
ebf990934 splice: relay sup... |
1198 1199 1200 1201 |
subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT; pidx = (read_start / PAGE_SIZE) % subbuf_pages; poff = read_start & ~PAGE_MASK; |
a786c06d9 missing bits of "... |
1202 |
nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max); |
ebf990934 splice: relay sup... |
1203 |
|
16d546694 relay: fix subbuf... |
1204 |
for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) { |
1db60cf20 relay: use splice... |
1205 1206 |
unsigned int this_len, this_end, private; unsigned int cur_pos = read_start + total_len; |
ebf990934 splice: relay sup... |
1207 |
|
1db60cf20 relay: use splice... |
1208 |
if (!len) |
ebf990934 splice: relay sup... |
1209 |
break; |
ebf990934 splice: relay sup... |
1210 |
|
1db60cf20 relay: use splice... |
1211 1212 |
this_len = min_t(unsigned long, len, PAGE_SIZE - poff); private = this_len; |
ebf990934 splice: relay sup... |
1213 |
|
1db60cf20 relay: use splice... |
1214 1215 |
spd.pages[spd.nr_pages] = rbuf->page_array[pidx]; spd.partial[spd.nr_pages].offset = poff; |
ebf990934 splice: relay sup... |
1216 |
|
1db60cf20 relay: use splice... |
1217 1218 1219 1220 |
this_end = cur_pos + this_len; if (this_end >= nonpad_end) { this_len = nonpad_end - cur_pos; private = this_len + padding; |
ebf990934 splice: relay sup... |
1221 |
} |
1db60cf20 relay: use splice... |
1222 1223 |
spd.partial[spd.nr_pages].len = this_len; spd.partial[spd.nr_pages].private = private; |
ebf990934 splice: relay sup... |
1224 |
|
1db60cf20 relay: use splice... |
1225 1226 1227 1228 |
len -= this_len; total_len += this_len; poff = 0; pidx = (pidx + 1) % subbuf_pages; |
ebf990934 splice: relay sup... |
1229 |
|
1db60cf20 relay: use splice... |
1230 1231 |
if (this_end >= nonpad_end) { spd.nr_pages++; |
ebf990934 splice: relay sup... |
1232 1233 |
break; } |
ebf990934 splice: relay sup... |
1234 |
} |
35f3d14db pipe: add support... |
1235 |
ret = 0; |
1db60cf20 relay: use splice... |
1236 |
if (!spd.nr_pages) |
35f3d14db pipe: add support... |
1237 |
goto out; |
ebf990934 splice: relay sup... |
1238 |
|
1db60cf20 relay: use splice... |
1239 1240 |
ret = *nonpad_ret = splice_to_pipe(pipe, &spd); if (ret < 0 || ret < total_len) |
35f3d14db pipe: add support... |
1241 |
goto out; |
ebf990934 splice: relay sup... |
1242 |
|
1db60cf20 relay: use splice... |
1243 1244 |
if (read_start + ret == nonpad_end) ret += padding; |
35f3d14db pipe: add support... |
1245 |
out: |
047fe3605 splice: fix racy ... |
1246 1247 |
splice_shrink_spd(&spd); return ret; |
ebf990934 splice: relay sup... |
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 |
} static ssize_t relay_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { ssize_t spliced; int ret; int nonpad_ret = 0; ret = 0; spliced = 0; |
a82c53a0e splice: fix sendf... |
1262 |
while (len && !spliced) { |
ebf990934 splice: relay sup... |
1263 1264 1265 1266 |
ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret); if (ret < 0) break; else if (!ret) { |
fbb5b7ae4 relayfs: fix infi... |
1267 |
if (flags & SPLICE_F_NONBLOCK) |
ebf990934 splice: relay sup... |
1268 |
ret = -EAGAIN; |
fbb5b7ae4 relayfs: fix infi... |
1269 |
break; |
ebf990934 splice: relay sup... |
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 |
} *ppos += ret; if (ret > len) len = 0; else len -= ret; spliced += nonpad_ret; nonpad_ret = 0; } if (spliced) return spliced; return ret; |
221415d76 [PATCH] relay: ad... |
1285 |
} |
15ad7cdcf [PATCH] struct se... |
1286 |
const struct file_operations relay_file_operations = { |
b86ff981a [PATCH] relay: mi... |
1287 1288 1289 1290 1291 1292 |
.open = relay_file_open, .poll = relay_file_poll, .mmap = relay_file_mmap, .read = relay_file_read, .llseek = no_llseek, .release = relay_file_release, |
ebf990934 splice: relay sup... |
1293 |
.splice_read = relay_file_splice_read, |
b86ff981a [PATCH] relay: mi... |
1294 1295 |
}; EXPORT_SYMBOL_GPL(relay_file_operations); |