ust: fix several segfaults, now seems to trace without errors
[ust.git] / libtracing / relay.c
1 /*
2 * Public API and common code for kernel->userspace relay file support.
3 *
4 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
5 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
6 * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
7 *
8 * Moved to kernel/relay.c by Paul Mundt, 2006.
9 * November 2006 - CPU hotplug support by Mathieu Desnoyers
10 * (mathieu.desnoyers@polymtl.ca)
11 *
12 * This file is released under the GPL.
13 */
14 //ust// #include <linux/errno.h>
15 //ust// #include <linux/stddef.h>
16 //ust// #include <linux/slab.h>
17 //ust// #include <linux/module.h>
18 //ust// #include <linux/string.h>
19 //ust// #include <linux/ltt-relay.h>
20 //ust// #include <linux/vmalloc.h>
21 //ust// #include <linux/mm.h>
22 //ust// #include <linux/cpu.h>
23 //ust// #include <linux/splice.h>
24 //ust// #include <linux/bitops.h>
25 #include <sys/mman.h>
26 #include "kernelcompat.h"
27 #include "list.h"
28 #include "relay.h"
29 #include "channels.h"
30 #include "kref.h"
31 #include "tracer.h"
32 #include "tracercore.h"
33 #include "usterr.h"
34
35 /* list of open channels, for cpu hotplug */
36 static DEFINE_MUTEX(relay_channels_mutex);
37 static LIST_HEAD(relay_channels);
38
39
40 static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf);
41
42 /**
43 * relay_alloc_buf - allocate a channel buffer
44 * @buf: the buffer struct
45 * @size: total size of the buffer
46 */
47 //ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
48 //ust//{
49 //ust// unsigned int i, n_pages;
50 //ust// struct buf_page *buf_page, *n;
51 //ust//
52 //ust// *size = PAGE_ALIGN(*size);
53 //ust// n_pages = *size >> PAGE_SHIFT;
54 //ust//
55 //ust// INIT_LIST_HEAD(&buf->pages);
56 //ust//
57 //ust// for (i = 0; i < n_pages; i++) {
58 //ust// buf_page = kmalloc_node(sizeof(*buf_page), GFP_KERNEL,
59 //ust// cpu_to_node(buf->cpu));
60 //ust// if (unlikely(!buf_page))
61 //ust// goto depopulate;
62 //ust// buf_page->page = alloc_pages_node(cpu_to_node(buf->cpu),
63 //ust// GFP_KERNEL | __GFP_ZERO, 0);
64 //ust// if (unlikely(!buf_page->page)) {
65 //ust// kfree(buf_page);
66 //ust// goto depopulate;
67 //ust// }
68 //ust// list_add_tail(&buf_page->list, &buf->pages);
69 //ust// buf_page->offset = (size_t)i << PAGE_SHIFT;
70 //ust// buf_page->buf = buf;
71 //ust// set_page_private(buf_page->page, (unsigned long)buf_page);
72 //ust// if (i == 0) {
73 //ust// buf->wpage = buf_page;
74 //ust// buf->hpage[0] = buf_page;
75 //ust// buf->hpage[1] = buf_page;
76 //ust// buf->rpage = buf_page;
77 //ust// }
78 //ust// }
79 //ust// buf->page_count = n_pages;
80 //ust// return 0;
81 //ust//
82 //ust//depopulate:
83 //ust// list_for_each_entry_safe(buf_page, n, &buf->pages, list) {
84 //ust// list_del_init(&buf_page->list);
85 //ust// __free_page(buf_page->page);
86 //ust// kfree(buf_page);
87 //ust// }
88 //ust// return -ENOMEM;
89 //ust//}
90
91 static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
92 {
93 unsigned int n_pages;
94 struct buf_page *buf_page, *n;
95
96 void *result;
97
98 *size = PAGE_ALIGN(*size);
99
100 /* Maybe do read-ahead */
101 result = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
102 if(result == MAP_FAILED) {
103 PERROR("mmap");
104 return -1;
105 }
106
107 buf->buf_data = result;
108 buf->buf_size = *size;
109
110 return 0;
111 }
112
113 /**
114 * relay_create_buf - allocate and initialize a channel buffer
115 * @chan: the relay channel
116 * @cpu: cpu the buffer belongs to
117 *
118 * Returns channel buffer if successful, %NULL otherwise.
119 */
120 static struct rchan_buf *relay_create_buf(struct rchan *chan)
121 {
122 int ret;
123 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
124 if (!buf)
125 return NULL;
126
127 // buf->cpu = cpu;
128 ret = relay_alloc_buf(buf, &chan->alloc_size);
129 if (ret)
130 goto free_buf;
131
132 buf->chan = chan;
133 kref_get(&buf->chan->kref);
134 return buf;
135
136 free_buf:
137 kfree(buf);
138 return NULL;
139 }
140
141 /**
142 * relay_destroy_channel - free the channel struct
143 * @kref: target kernel reference that contains the relay channel
144 *
145 * Should only be called from kref_put().
146 */
147 static void relay_destroy_channel(struct kref *kref)
148 {
149 struct rchan *chan = container_of(kref, struct rchan, kref);
150 kfree(chan);
151 }
152
153 /**
154 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
155 * @buf: the buffer struct
156 */
157 static void relay_destroy_buf(struct rchan_buf *buf)
158 {
159 struct rchan *chan = buf->chan;
160 struct buf_page *buf_page, *n;
161 int result;
162
163 result = munmap(buf->buf_data, buf->buf_size);
164 if(result == -1) {
165 PERROR("munmap");
166 }
167
168 //ust// chan->buf[buf->cpu] = NULL;
169 kfree(buf);
170 kref_put(&chan->kref, relay_destroy_channel);
171 }
172
173 /**
174 * relay_remove_buf - remove a channel buffer
175 * @kref: target kernel reference that contains the relay buffer
176 *
177 * Removes the file from the fileystem, which also frees the
178 * rchan_buf_struct and the channel buffer. Should only be called from
179 * kref_put().
180 */
181 static void relay_remove_buf(struct kref *kref)
182 {
183 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
184 buf->chan->cb->remove_buf_file(buf);
185 relay_destroy_buf(buf);
186 }
187
188 /*
189 * High-level relay kernel API and associated functions.
190 */
191
192 /*
193 * rchan_callback implementations defining default channel behavior. Used
194 * in place of corresponding NULL values in client callback struct.
195 */
196
197 /*
198 * create_buf_file_create() default callback. Does nothing.
199 */
200 static struct dentry *create_buf_file_default_callback(const char *filename,
201 struct dentry *parent,
202 int mode,
203 struct rchan_buf *buf)
204 {
205 return NULL;
206 }
207
208 /*
209 * remove_buf_file() default callback. Does nothing.
210 */
211 static int remove_buf_file_default_callback(struct dentry *dentry)
212 {
213 return -EINVAL;
214 }
215
216 /**
217 * wakeup_readers - wake up readers waiting on a channel
218 * @data: contains the channel buffer
219 *
220 * This is the timer function used to defer reader waking.
221 */
222 //ust// static void wakeup_readers(unsigned long data)
223 //ust// {
224 //ust// struct rchan_buf *buf = (struct rchan_buf *)data;
225 //ust// wake_up_interruptible(&buf->read_wait);
226 //ust// }
227
228 /**
229 * __relay_reset - reset a channel buffer
230 * @buf: the channel buffer
231 * @init: 1 if this is a first-time initialization
232 *
233 * See relay_reset() for description of effect.
234 */
235 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
236 {
237 if (init) {
238 //ust// init_waitqueue_head(&buf->read_wait);
239 kref_init(&buf->kref);
240 //ust// setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
241 } else
242 //ust// del_timer_sync(&buf->timer);
243
244 buf->finalized = 0;
245 }
246
247 /*
248 * relay_open_buf - create a new relay channel buffer
249 *
250 * used by relay_open() and CPU hotplug.
251 */
252 static struct rchan_buf *relay_open_buf(struct rchan *chan)
253 {
254 struct rchan_buf *buf = NULL;
255 struct dentry *dentry;
256 //ust// char *tmpname;
257
258 //ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
259 //ust// if (!tmpname)
260 //ust// goto end;
261 //ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
262
263 buf = relay_create_buf(chan);
264 if (!buf)
265 goto free_name;
266
267 __relay_reset(buf, 1);
268
269 /* Create file in fs */
270 //ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
271 //ust// buf);
272
273 ltt_create_buf_file_callback(buf); // ust //
274
275 //ust// if (!dentry)
276 //ust// goto free_buf;
277 //ust//
278 //ust// buf->dentry = dentry;
279
280 goto free_name;
281
282 free_buf:
283 relay_destroy_buf(buf);
284 buf = NULL;
285 free_name:
286 //ust// kfree(tmpname);
287 end:
288 return buf;
289 }
290
291 /**
292 * relay_close_buf - close a channel buffer
293 * @buf: channel buffer
294 *
295 * Marks the buffer finalized and restores the default callbacks.
296 * The channel buffer and channel buffer data structure are then freed
297 * automatically when the last reference is given up.
298 */
299 static void relay_close_buf(struct rchan_buf *buf)
300 {
301 //ust// del_timer_sync(&buf->timer);
302 kref_put(&buf->kref, relay_remove_buf);
303 }
304
305 //ust// static void setup_callbacks(struct rchan *chan,
306 //ust// struct rchan_callbacks *cb)
307 //ust// {
308 //ust// if (!cb) {
309 //ust// chan->cb = &default_channel_callbacks;
310 //ust// return;
311 //ust// }
312 //ust//
313 //ust// if (!cb->create_buf_file)
314 //ust// cb->create_buf_file = create_buf_file_default_callback;
315 //ust// if (!cb->remove_buf_file)
316 //ust// cb->remove_buf_file = remove_buf_file_default_callback;
317 //ust// chan->cb = cb;
318 //ust// }
319
320 /**
321 * relay_hotcpu_callback - CPU hotplug callback
322 * @nb: notifier block
323 * @action: hotplug action to take
324 * @hcpu: CPU number
325 *
326 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
327 */
328 //ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
329 //ust// unsigned long action,
330 //ust// void *hcpu)
331 //ust// {
332 //ust// unsigned int hotcpu = (unsigned long)hcpu;
333 //ust// struct rchan *chan;
334 //ust//
335 //ust// switch (action) {
336 //ust// case CPU_UP_PREPARE:
337 //ust// case CPU_UP_PREPARE_FROZEN:
338 //ust// mutex_lock(&relay_channels_mutex);
339 //ust// list_for_each_entry(chan, &relay_channels, list) {
340 //ust// if (chan->buf[hotcpu])
341 //ust// continue;
342 //ust// chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
343 //ust// if (!chan->buf[hotcpu]) {
344 //ust// printk(KERN_ERR
345 //ust// "relay_hotcpu_callback: cpu %d buffer "
346 //ust// "creation failed\n", hotcpu);
347 //ust// mutex_unlock(&relay_channels_mutex);
348 //ust// return NOTIFY_BAD;
349 //ust// }
350 //ust// }
351 //ust// mutex_unlock(&relay_channels_mutex);
352 //ust// break;
353 //ust// case CPU_DEAD:
354 //ust// case CPU_DEAD_FROZEN:
355 //ust// /* No need to flush the cpu : will be flushed upon
356 //ust// * final relay_flush() call. */
357 //ust// break;
358 //ust// }
359 //ust// return NOTIFY_OK;
360 //ust// }
361
362 /**
363 * ltt_relay_open - create a new relay channel
364 * @base_filename: base name of files to create
365 * @parent: dentry of parent directory, %NULL for root directory
366 * @subbuf_size: size of sub-buffers
367 * @n_subbufs: number of sub-buffers
368 * @cb: client callback functions
369 * @private_data: user-defined data
370 *
371 * Returns channel pointer if successful, %NULL otherwise.
372 *
373 * Creates a channel buffer for each cpu using the sizes and
374 * attributes specified. The created channel buffer files
375 * will be named base_filename0...base_filenameN-1. File
376 * permissions will be %S_IRUSR.
377 */
378 struct rchan *ltt_relay_open(const char *base_filename,
379 struct dentry *parent,
380 size_t subbuf_size,
381 size_t n_subbufs,
382 void *private_data)
383 {
384 unsigned int i;
385 struct rchan *chan;
386 //ust// if (!base_filename)
387 //ust// return NULL;
388
389 if (!(subbuf_size && n_subbufs))
390 return NULL;
391
392 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
393 if (!chan)
394 return NULL;
395
396 chan->version = LTT_RELAY_CHANNEL_VERSION;
397 chan->n_subbufs = n_subbufs;
398 chan->subbuf_size = subbuf_size;
399 chan->subbuf_size_order = get_count_order(subbuf_size);
400 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
401 chan->parent = parent;
402 chan->private_data = private_data;
403 //ust// strlcpy(chan->base_filename, base_filename, NAME_MAX);
404 //ust// setup_callbacks(chan, cb);
405 kref_init(&chan->kref);
406
407 mutex_lock(&relay_channels_mutex);
408 //ust// for_each_online_cpu(i) {
409 chan->buf = relay_open_buf(chan);
410 if (!chan->buf)
411 goto error;
412 //ust// }
413 list_add(&chan->list, &relay_channels);
414 mutex_unlock(&relay_channels_mutex);
415
416 return chan;
417
418 //ust//free_bufs:
419 //ust// for_each_possible_cpu(i) {
420 //ust// if (!chan->buf[i])
421 //ust// break;
422 //ust// relay_close_buf(chan->buf[i]);
423 //ust// }
424
425 error:
426 kref_put(&chan->kref, relay_destroy_channel);
427 mutex_unlock(&relay_channels_mutex);
428 return NULL;
429 }
430 //ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
431
432 /**
433 * ltt_relay_close - close the channel
434 * @chan: the channel
435 *
436 * Closes all channel buffers and frees the channel.
437 */
438 void ltt_relay_close(struct rchan *chan)
439 {
440 unsigned int i;
441
442 if (!chan)
443 return;
444
445 mutex_lock(&relay_channels_mutex);
446 //ust// for_each_possible_cpu(i)
447 if (chan->buf)
448 relay_close_buf(chan->buf);
449
450 list_del(&chan->list);
451 kref_put(&chan->kref, relay_destroy_channel);
452 mutex_unlock(&relay_channels_mutex);
453 }
454 //ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
455
456 /*
457 * Start iteration at the previous element. Skip the real list head.
458 */
459 //ust// struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
460 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
461 //ust// {
462 //ust// struct buf_page *iter;
463 //ust// size_t orig_iter_off;
464 //ust// unsigned int i = 0;
465 //ust//
466 //ust// orig_iter_off = page->offset;
467 //ust// list_for_each_entry_reverse(iter, &page->list, list) {
468 //ust// /*
469 //ust// * Skip the real list head.
470 //ust// */
471 //ust// if (&iter->list == &buf->pages)
472 //ust// continue;
473 //ust// i++;
474 //ust// if (offset >= iter->offset
475 //ust// && offset < iter->offset + PAGE_SIZE) {
476 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
477 //ust// if (i > 1) {
478 //ust// printk(KERN_WARNING
479 //ust// "Backward random access detected in "
480 //ust// "ltt_relay. Iterations %u, "
481 //ust// "offset %zu, orig iter->off %zu, "
482 //ust// "iter->off %zu diff_offset %zd.\n", i,
483 //ust// offset, orig_iter_off, iter->offset,
484 //ust// diff_offset);
485 //ust// WARN_ON(1);
486 //ust// }
487 //ust// #endif
488 //ust// return iter;
489 //ust// }
490 //ust// }
491 //ust// WARN_ON(1);
492 //ust// return NULL;
493 //ust// }
494 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
495
496 /*
497 * Start iteration at the next element. Skip the real list head.
498 */
499 //ust// struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
500 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
501 //ust// {
502 //ust// struct buf_page *iter;
503 //ust// unsigned int i = 0;
504 //ust// size_t orig_iter_off;
505 //ust//
506 //ust// orig_iter_off = page->offset;
507 //ust// list_for_each_entry(iter, &page->list, list) {
508 //ust// /*
509 //ust// * Skip the real list head.
510 //ust// */
511 //ust// if (&iter->list == &buf->pages)
512 //ust// continue;
513 //ust// i++;
514 //ust// if (offset >= iter->offset
515 //ust// && offset < iter->offset + PAGE_SIZE) {
516 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
517 //ust// if (i > 1) {
518 //ust// printk(KERN_WARNING
519 //ust// "Forward random access detected in "
520 //ust// "ltt_relay. Iterations %u, "
521 //ust// "offset %zu, orig iter->off %zu, "
522 //ust// "iter->off %zu diff_offset %zd.\n", i,
523 //ust// offset, orig_iter_off, iter->offset,
524 //ust// diff_offset);
525 //ust// WARN_ON(1);
526 //ust// }
527 //ust// #endif
528 //ust// return iter;
529 //ust// }
530 //ust// }
531 //ust// WARN_ON(1);
532 //ust// return NULL;
533 //ust// }
534 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
535
536 /**
537 * ltt_relay_write - write data to a ltt_relay buffer.
538 * @buf : buffer
539 * @offset : offset within the buffer
540 * @src : source address
541 * @len : length to write
542 * @page : cached buffer page
543 * @pagecpy : page size copied so far
544 */
545 void _ltt_relay_write(struct rchan_buf *buf, size_t offset,
546 const void *src, size_t len, ssize_t cpy)
547 {
548 do {
549 len -= cpy;
550 src += cpy;
551 offset += cpy;
552 /*
553 * Underlying layer should never ask for writes across
554 * subbuffers.
555 */
556 WARN_ON(offset >= buf->buf_size);
557
558 cpy = min_t(size_t, len, buf->buf_size - offset);
559 ltt_relay_do_copy(buf->buf_data + offset, src, cpy);
560 } while (unlikely(len != cpy));
561 }
562 //ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
563
564 /**
565 * ltt_relay_read - read data from ltt_relay_buffer.
566 * @buf : buffer
567 * @offset : offset within the buffer
568 * @dest : destination address
569 * @len : length to write
570 */
571 //ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
572 //ust// void *dest, size_t len)
573 //ust// {
574 //ust// struct buf_page *page;
575 //ust// ssize_t pagecpy, orig_len;
576 //ust//
577 //ust// orig_len = len;
578 //ust// offset &= buf->chan->alloc_size - 1;
579 //ust// page = buf->rpage;
580 //ust// if (unlikely(!len))
581 //ust// return 0;
582 //ust// for (;;) {
583 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
584 //ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
585 //ust// memcpy(dest, page_address(page->page) + (offset & ~PAGE_MASK),
586 //ust// pagecpy);
587 //ust// len -= pagecpy;
588 //ust// if (likely(!len))
589 //ust// break;
590 //ust// dest += pagecpy;
591 //ust// offset += pagecpy;
592 //ust// /*
593 //ust// * Underlying layer should never ask for reads across
594 //ust// * subbuffers.
595 //ust// */
596 //ust// WARN_ON(offset >= buf->chan->alloc_size);
597 //ust// }
598 //ust// return orig_len;
599 //ust// }
600 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
601
602 /**
603 * ltt_relay_read_get_page - Get a whole page to read from
604 * @buf : buffer
605 * @offset : offset within the buffer
606 */
607 //ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
608 //ust// {
609 //ust// struct buf_page *page;
610
611 //ust// offset &= buf->chan->alloc_size - 1;
612 //ust// page = buf->rpage;
613 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
614 //ust// return page;
615 //ust// }
616 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
617
618 /**
619 * ltt_relay_offset_address - get address of a location within the buffer
620 * @buf : buffer
621 * @offset : offset within the buffer.
622 *
623 * Return the address where a given offset is located.
624 * Should be used to get the current subbuffer header pointer. Given we know
625 * it's never on a page boundary, it's safe to write directly to this address,
626 * as long as the write is never bigger than a page size.
627 */
628 void *ltt_relay_offset_address(struct rchan_buf *buf, size_t offset)
629 {
630 //ust// struct buf_page *page;
631 //ust// unsigned int odd;
632 //ust//
633 //ust// offset &= buf->chan->alloc_size - 1;
634 //ust// odd = !!(offset & buf->chan->subbuf_size);
635 //ust// page = buf->hpage[odd];
636 //ust// if (offset < page->offset || offset >= page->offset + PAGE_SIZE)
637 //ust// buf->hpage[odd] = page = buf->wpage;
638 //ust// page = ltt_relay_cache_page(buf, &buf->hpage[odd], page, offset);
639 //ust// return page_address(page->page) + (offset & ~PAGE_MASK);
640 return ((char *)buf->buf_data)+offset;
641 return NULL;
642 }
643 //ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
644
645 /**
646 * relay_file_open - open file op for relay files
647 * @inode: the inode
648 * @filp: the file
649 *
650 * Increments the channel buffer refcount.
651 */
652 //ust// static int relay_file_open(struct inode *inode, struct file *filp)
653 //ust// {
654 //ust// struct rchan_buf *buf = inode->i_private;
655 //ust// kref_get(&buf->kref);
656 //ust// filp->private_data = buf;
657 //ust//
658 //ust// return nonseekable_open(inode, filp);
659 //ust// }
660
661 /**
662 * relay_file_release - release file op for relay files
663 * @inode: the inode
664 * @filp: the file
665 *
666 * Decrements the channel refcount, as the filesystem is
667 * no longer using it.
668 */
669 //ust// static int relay_file_release(struct inode *inode, struct file *filp)
670 //ust// {
671 //ust// struct rchan_buf *buf = filp->private_data;
672 //ust// kref_put(&buf->kref, relay_remove_buf);
673 //ust//
674 //ust// return 0;
675 //ust// }
676
677 //ust// const struct file_operations ltt_relay_file_operations = {
678 //ust// .open = relay_file_open,
679 //ust// .release = relay_file_release,
680 //ust// };
681 //ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
682
683 //ust// static __init int relay_init(void)
684 //ust// {
685 //ust// hotcpu_notifier(relay_hotcpu_callback, 5);
686 //ust// return 0;
687 //ust// }
688
689 //ust// module_init(relay_init);
690 /*
691 * ltt/ltt-relay.c
692 *
693 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
694 *
695 * LTTng lockless buffer space management (reader/writer).
696 *
697 * Author:
698 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
699 *
700 * Inspired from LTT :
701 * Karim Yaghmour (karim@opersys.com)
702 * Tom Zanussi (zanussi@us.ibm.com)
703 * Bob Wisniewski (bob@watson.ibm.com)
704 * And from K42 :
705 * Bob Wisniewski (bob@watson.ibm.com)
706 *
707 * Changelog:
708 * 08/10/08, Cleanup.
709 * 19/10/05, Complete lockless mechanism.
710 * 27/05/05, Modular redesign and rewrite.
711 *
712 * Userspace reader semantic :
713 * while (poll fd != POLLHUP) {
714 * - ioctl RELAY_GET_SUBBUF_SIZE
715 * while (1) {
716 * - ioctl GET_SUBBUF
717 * - splice 1 subbuffer worth of data to a pipe
718 * - splice the data from pipe to disk/network
719 * - ioctl PUT_SUBBUF, check error value
720 * if err val < 0, previous subbuffer was corrupted.
721 * }
722 * }
723 */
724
725 //ust// #include <linux/time.h>
726 //ust// #include <linux/ltt-tracer.h>
727 //ust// #include <linux/ltt-relay.h>
728 //ust// #include <linux/module.h>
729 //ust// #include <linux/string.h>
730 //ust// #include <linux/slab.h>
731 //ust// #include <linux/init.h>
732 //ust// #include <linux/rcupdate.h>
733 //ust// #include <linux/sched.h>
734 //ust// #include <linux/bitops.h>
735 //ust// #include <linux/fs.h>
736 //ust// #include <linux/smp_lock.h>
737 //ust// #include <linux/debugfs.h>
738 //ust// #include <linux/stat.h>
739 //ust// #include <linux/cpu.h>
740 //ust// #include <linux/pipe_fs_i.h>
741 //ust// #include <linux/splice.h>
742 //ust// #include <asm/atomic.h>
743 //ust// #include <asm/local.h>
744
745 #if 0
746 #define printk_dbg(fmt, args...) printk(fmt, args)
747 #else
748 #define printk_dbg(fmt, args...)
749 #endif
750
751 /* LTTng lockless logging buffer info */
752 struct ltt_channel_buf_struct {
753 /* First 32 bytes cache-hot cacheline */
754 local_t offset; /* Current offset in the buffer */
755 local_t *commit_count; /* Commit count per sub-buffer */
756 atomic_long_t consumed; /*
757 * Current offset in the buffer
758 * standard atomic access (shared)
759 */
760 unsigned long last_tsc; /*
761 * Last timestamp written in the buffer.
762 */
763 /* End of first 32 bytes cacheline */
764 atomic_long_t active_readers; /*
765 * Active readers count
766 * standard atomic access (shared)
767 */
768 local_t events_lost;
769 local_t corrupted_subbuffers;
770 spinlock_t full_lock; /*
771 * buffer full condition spinlock, only
772 * for userspace tracing blocking mode
773 * synchronization with reader.
774 */
775 //ust// wait_queue_head_t write_wait; /*
776 //ust// * Wait queue for blocking user space
777 //ust// * writers
778 //ust// */
779 atomic_t wakeup_readers; /* Boolean : wakeup readers waiting ? */
780 } ____cacheline_aligned;
781
782 /*
783 * Last TSC comparison functions. Check if the current TSC overflows
784 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
785 * atomically.
786 */
787
788 #if (BITS_PER_LONG == 32)
789 static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
790 u64 tsc)
791 {
792 ltt_buf->last_tsc = (unsigned long)(tsc >> LTT_TSC_BITS);
793 }
794
795 static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
796 u64 tsc)
797 {
798 unsigned long tsc_shifted = (unsigned long)(tsc >> LTT_TSC_BITS);
799
800 if (unlikely((tsc_shifted - ltt_buf->last_tsc)))
801 return 1;
802 else
803 return 0;
804 }
805 #else
806 static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
807 u64 tsc)
808 {
809 ltt_buf->last_tsc = (unsigned long)tsc;
810 }
811
812 static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
813 u64 tsc)
814 {
815 if (unlikely((tsc - ltt_buf->last_tsc) >> LTT_TSC_BITS))
816 return 1;
817 else
818 return 0;
819 }
820 #endif
821
822 //ust// static struct file_operations ltt_file_operations;
823
824 /*
825 * A switch is done during tracing or as a final flush after tracing (so it
826 * won't write in the new sub-buffer).
827 */
828 enum force_switch_mode { FORCE_ACTIVE, FORCE_FLUSH };
829
830 static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
831 struct ltt_channel_struct *ltt_chan,
832 struct rchan_buf *buf,
833 unsigned int n_subbufs);
834
835 static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan);
836
837 static void ltt_force_switch(struct rchan_buf *buf,
838 enum force_switch_mode mode);
839
840 /*
841 * Trace callbacks
842 */
843 static void ltt_buffer_begin_callback(struct rchan_buf *buf,
844 u64 tsc, unsigned int subbuf_idx)
845 {
846 struct ltt_channel_struct *channel =
847 (struct ltt_channel_struct *)buf->chan->private_data;
848 struct ltt_subbuffer_header *header =
849 (struct ltt_subbuffer_header *)
850 ltt_relay_offset_address(buf,
851 subbuf_idx * buf->chan->subbuf_size);
852
853 header->cycle_count_begin = tsc;
854 header->lost_size = 0xFFFFFFFF; /* for debugging */
855 header->buf_size = buf->chan->subbuf_size;
856 ltt_write_trace_header(channel->trace, header);
857 }
858
859 /*
860 * offset is assumed to never be 0 here : never deliver a completely empty
861 * subbuffer. The lost size is between 0 and subbuf_size-1.
862 */
863 static notrace void ltt_buffer_end_callback(struct rchan_buf *buf,
864 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
865 {
866 struct ltt_channel_struct *channel =
867 (struct ltt_channel_struct *)buf->chan->private_data;
868 struct ltt_channel_buf_struct *ltt_buf = channel->buf;
869 struct ltt_subbuffer_header *header =
870 (struct ltt_subbuffer_header *)
871 ltt_relay_offset_address(buf,
872 subbuf_idx * buf->chan->subbuf_size);
873
874 header->lost_size = SUBBUF_OFFSET((buf->chan->subbuf_size - offset),
875 buf->chan);
876 header->cycle_count_end = tsc;
877 header->events_lost = local_read(&ltt_buf->events_lost);
878 header->subbuf_corrupt = local_read(&ltt_buf->corrupted_subbuffers);
879
880 }
881
882 static notrace void ltt_deliver(struct rchan_buf *buf, unsigned int subbuf_idx,
883 void *subbuf)
884 {
885 struct ltt_channel_struct *channel =
886 (struct ltt_channel_struct *)buf->chan->private_data;
887 struct ltt_channel_buf_struct *ltt_buf = channel->buf;
888
889 atomic_set(&ltt_buf->wakeup_readers, 1);
890 }
891
892 static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf)
893 {
894 struct ltt_channel_struct *ltt_chan;
895 int err;
896 //ust// struct dentry *dentry;
897
898 ltt_chan = buf->chan->private_data;
899 err = ltt_relay_create_buffer(ltt_chan->trace, ltt_chan, buf, buf->chan->n_subbufs);
900 if (err)
901 return ERR_PTR(err);
902
903 //ust// dentry = debugfs_create_file(filename, mode, parent, buf,
904 //ust// &ltt_file_operations);
905 //ust// if (!dentry)
906 //ust// goto error;
907 //ust// return dentry;
908 return NULL; //ust//
909 //ust//error:
910 ltt_relay_destroy_buffer(ltt_chan);
911 return NULL;
912 }
913
914 static int ltt_remove_buf_file_callback(struct rchan_buf *buf)
915 {
916 //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
917 struct ltt_channel_struct *ltt_chan = buf->chan->private_data;
918
919 //ust// debugfs_remove(dentry);
920 ltt_relay_destroy_buffer(ltt_chan);
921
922 return 0;
923 }
924
925 /*
926 * Wake writers :
927 *
928 * This must be done after the trace is removed from the RCU list so that there
929 * are no stalled writers.
930 */
931 //ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
932 //ust// {
933 //ust//
934 //ust// if (waitqueue_active(&ltt_buf->write_wait))
935 //ust// wake_up_interruptible(&ltt_buf->write_wait);
936 //ust// }
937
938 /*
939 * This function should not be called from NMI interrupt context
940 */
941 static notrace void ltt_buf_unfull(struct rchan_buf *buf,
942 unsigned int subbuf_idx,
943 long offset)
944 {
945 //ust// struct ltt_channel_struct *ltt_channel =
946 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
947 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
948 //ust//
949 //ust// ltt_relay_wake_writers(ltt_buf);
950 }
951
952 /**
953 * ltt_open - open file op for ltt files
954 * @inode: opened inode
955 * @file: opened file
956 *
957 * Open implementation. Makes sure only one open instance of a buffer is
958 * done at a given moment.
959 */
960 //ust// static int ltt_open(struct inode *inode, struct file *file)
961 //ust// {
962 //ust// struct rchan_buf *buf = inode->i_private;
963 //ust// struct ltt_channel_struct *ltt_channel =
964 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
965 //ust// struct ltt_channel_buf_struct *ltt_buf =
966 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
967 //ust//
968 //ust// if (!atomic_long_add_unless(&ltt_buf->active_readers, 1, 1))
969 //ust// return -EBUSY;
970 //ust// return ltt_relay_file_operations.open(inode, file);
971 //ust// }
972
973 /**
974 * ltt_release - release file op for ltt files
975 * @inode: opened inode
976 * @file: opened file
977 *
978 * Release implementation.
979 */
980 //ust// static int ltt_release(struct inode *inode, struct file *file)
981 //ust// {
982 //ust// struct rchan_buf *buf = inode->i_private;
983 //ust// struct ltt_channel_struct *ltt_channel =
984 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
985 //ust// struct ltt_channel_buf_struct *ltt_buf =
986 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
987 //ust// int ret;
988 //ust//
989 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
990 //ust// atomic_long_dec(&ltt_buf->active_readers);
991 //ust// ret = ltt_relay_file_operations.release(inode, file);
992 //ust// WARN_ON(ret);
993 //ust// return ret;
994 //ust// }
995
996 /**
997 * ltt_poll - file op for ltt files
998 * @filp: the file
999 * @wait: poll table
1000 *
1001 * Poll implementation.
1002 */
1003 //ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
1004 //ust// {
1005 //ust// unsigned int mask = 0;
1006 //ust// struct inode *inode = filp->f_dentry->d_inode;
1007 //ust// struct rchan_buf *buf = inode->i_private;
1008 //ust// struct ltt_channel_struct *ltt_channel =
1009 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1010 //ust// struct ltt_channel_buf_struct *ltt_buf =
1011 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1012 //ust//
1013 //ust// if (filp->f_mode & FMODE_READ) {
1014 //ust// poll_wait_set_exclusive(wait);
1015 //ust// poll_wait(filp, &buf->read_wait, wait);
1016 //ust//
1017 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1018 //ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1019 //ust// buf->chan)
1020 //ust// - SUBBUF_TRUNC(atomic_long_read(&ltt_buf->consumed),
1021 //ust// buf->chan)
1022 //ust// == 0) {
1023 //ust// if (buf->finalized)
1024 //ust// return POLLHUP;
1025 //ust// else
1026 //ust// return 0;
1027 //ust// } else {
1028 //ust// struct rchan *rchan =
1029 //ust// ltt_channel->trans_channel_data;
1030 //ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1031 //ust// buf->chan)
1032 //ust// - SUBBUF_TRUNC(atomic_long_read(
1033 //ust// &ltt_buf->consumed),
1034 //ust// buf->chan)
1035 //ust// >= rchan->alloc_size)
1036 //ust// return POLLPRI | POLLRDBAND;
1037 //ust// else
1038 //ust// return POLLIN | POLLRDNORM;
1039 //ust// }
1040 //ust// }
1041 //ust// return mask;
1042 //ust// }
1043
1044 static int ltt_do_get_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, long *pconsumed_old)
1045 {
1046 struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)buf->chan->private_data;
1047 long consumed_old, consumed_idx, commit_count, write_offset;
1048 consumed_old = atomic_long_read(&ltt_buf->consumed);
1049 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1050 commit_count = local_read(&ltt_buf->commit_count[consumed_idx]);
1051 /*
1052 * Make sure we read the commit count before reading the buffer
1053 * data and the write offset. Correct consumed offset ordering
1054 * wrt commit count is insured by the use of cmpxchg to update
1055 * the consumed offset.
1056 */
1057 smp_rmb();
1058 write_offset = local_read(&ltt_buf->offset);
1059 /*
1060 * Check that the subbuffer we are trying to consume has been
1061 * already fully committed.
1062 */
1063 if (((commit_count - buf->chan->subbuf_size)
1064 & ltt_channel->commit_count_mask)
1065 - (BUFFER_TRUNC(consumed_old, buf->chan)
1066 >> ltt_channel->n_subbufs_order)
1067 != 0) {
1068 return -EAGAIN;
1069 }
1070 /*
1071 * Check that we are not about to read the same subbuffer in
1072 * which the writer head is.
1073 */
1074 if ((SUBBUF_TRUNC(write_offset, buf->chan)
1075 - SUBBUF_TRUNC(consumed_old, buf->chan))
1076 == 0) {
1077 return -EAGAIN;
1078 }
1079
1080 *pconsumed_old = consumed_old;
1081 return 0;
1082 }
1083
1084 static int ltt_do_put_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, u32 uconsumed_old)
1085 {
1086 long consumed_new, consumed_old;
1087
1088 consumed_old = atomic_long_read(&ltt_buf->consumed);
1089 consumed_old = consumed_old & (~0xFFFFFFFFL);
1090 consumed_old = consumed_old | uconsumed_old;
1091 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1092
1093 spin_lock(&ltt_buf->full_lock);
1094 if (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1095 consumed_new)
1096 != consumed_old) {
1097 /* We have been pushed by the writer : the last
1098 * buffer read _is_ corrupted! It can also
1099 * happen if this is a buffer we never got. */
1100 spin_unlock(&ltt_buf->full_lock);
1101 return -EIO;
1102 } else {
1103 /* tell the client that buffer is now unfull */
1104 int index;
1105 long data;
1106 index = SUBBUF_INDEX(consumed_old, buf->chan);
1107 data = BUFFER_OFFSET(consumed_old, buf->chan);
1108 ltt_buf_unfull(buf, index, data);
1109 spin_unlock(&ltt_buf->full_lock);
1110 }
1111 return 0;
1112 }
1113
1114 /**
1115 * ltt_ioctl - control on the debugfs file
1116 *
1117 * @inode: the inode
1118 * @filp: the file
1119 * @cmd: the command
1120 * @arg: command arg
1121 *
1122 * This ioctl implements three commands necessary for a minimal
1123 * producer/consumer implementation :
1124 * RELAY_GET_SUBBUF
1125 * Get the next sub buffer that can be read. It never blocks.
1126 * RELAY_PUT_SUBBUF
1127 * Release the currently read sub-buffer. Parameter is the last
1128 * put subbuffer (returned by GET_SUBBUF).
1129 * RELAY_GET_N_BUBBUFS
1130 * returns the number of sub buffers in the per cpu channel.
1131 * RELAY_GET_SUBBUF_SIZE
1132 * returns the size of the sub buffers.
1133 */
1134 //ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1135 //ust// unsigned int cmd, unsigned long arg)
1136 //ust// {
1137 //ust// struct rchan_buf *buf = inode->i_private;
1138 //ust// struct ltt_channel_struct *ltt_channel =
1139 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1140 //ust// struct ltt_channel_buf_struct *ltt_buf =
1141 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1142 //ust// u32 __user *argp = (u32 __user *)arg;
1143 //ust//
1144 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1145 //ust// switch (cmd) {
1146 //ust// case RELAY_GET_SUBBUF:
1147 //ust// {
1148 //ust// int ret;
1149 //ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1150 //ust// if(ret < 0)
1151 //ust// return ret;
1152 //ust// return put_user((u32)consumed_old, argp);
1153 //ust// }
1154 //ust// case RELAY_PUT_SUBBUF:
1155 //ust// {
1156 //ust// int ret;
1157 //ust// u32 uconsumed_old;
1158 //ust// ret = get_user(uconsumed_old, argp);
1159 //ust// if (ret)
1160 //ust// return ret; /* will return -EFAULT */
1161 //ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1162 //ust// }
1163 //ust// case RELAY_GET_N_SUBBUFS:
1164 //ust// return put_user((u32)buf->chan->n_subbufs, argp);
1165 //ust// break;
1166 //ust// case RELAY_GET_SUBBUF_SIZE:
1167 //ust// return put_user((u32)buf->chan->subbuf_size, argp);
1168 //ust// break;
1169 //ust// default:
1170 //ust// return -ENOIOCTLCMD;
1171 //ust// }
1172 //ust// return 0;
1173 //ust// }
1174
1175 //ust// #ifdef CONFIG_COMPAT
1176 //ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1177 //ust// unsigned long arg)
1178 //ust// {
1179 //ust// long ret = -ENOIOCTLCMD;
1180 //ust//
1181 //ust// lock_kernel();
1182 //ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1183 //ust// unlock_kernel();
1184 //ust//
1185 //ust// return ret;
1186 //ust// }
1187 //ust// #endif
1188
1189 //ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1190 //ust// struct pipe_buffer *pbuf)
1191 //ust// {
1192 //ust// }
1193 //ust//
1194 //ust// static struct pipe_buf_operations ltt_relay_pipe_buf_ops = {
1195 //ust// .can_merge = 0,
1196 //ust// .map = generic_pipe_buf_map,
1197 //ust// .unmap = generic_pipe_buf_unmap,
1198 //ust// .confirm = generic_pipe_buf_confirm,
1199 //ust// .release = ltt_relay_pipe_buf_release,
1200 //ust// .steal = generic_pipe_buf_steal,
1201 //ust// .get = generic_pipe_buf_get,
1202 //ust// };
1203
1204 //ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1205 //ust// {
1206 //ust// }
1207
1208 /*
1209 * subbuf_splice_actor - splice up to one subbuf's worth of data
1210 */
1211 //ust// static int subbuf_splice_actor(struct file *in,
1212 //ust// loff_t *ppos,
1213 //ust// struct pipe_inode_info *pipe,
1214 //ust// size_t len,
1215 //ust// unsigned int flags)
1216 //ust// {
1217 //ust// struct rchan_buf *buf = in->private_data;
1218 //ust// struct ltt_channel_struct *ltt_channel =
1219 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1220 //ust// struct ltt_channel_buf_struct *ltt_buf =
1221 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1222 //ust// unsigned int poff, subbuf_pages, nr_pages;
1223 //ust// struct page *pages[PIPE_BUFFERS];
1224 //ust// struct partial_page partial[PIPE_BUFFERS];
1225 //ust// struct splice_pipe_desc spd = {
1226 //ust// .pages = pages,
1227 //ust// .nr_pages = 0,
1228 //ust// .partial = partial,
1229 //ust// .flags = flags,
1230 //ust// .ops = &ltt_relay_pipe_buf_ops,
1231 //ust// .spd_release = ltt_relay_page_release,
1232 //ust// };
1233 //ust// long consumed_old, consumed_idx, roffset;
1234 //ust// unsigned long bytes_avail;
1235 //ust//
1236 //ust// /*
1237 //ust// * Check that a GET_SUBBUF ioctl has been done before.
1238 //ust// */
1239 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1240 //ust// consumed_old = atomic_long_read(&ltt_buf->consumed);
1241 //ust// consumed_old += *ppos;
1242 //ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1243 //ust//
1244 //ust// /*
1245 //ust// * Adjust read len, if longer than what is available
1246 //ust// */
1247 //ust// bytes_avail = SUBBUF_TRUNC(local_read(&ltt_buf->offset), buf->chan)
1248 //ust// - consumed_old;
1249 //ust// WARN_ON(bytes_avail > buf->chan->alloc_size);
1250 //ust// len = min_t(size_t, len, bytes_avail);
1251 //ust// subbuf_pages = bytes_avail >> PAGE_SHIFT;
1252 //ust// nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
1253 //ust// roffset = consumed_old & PAGE_MASK;
1254 //ust// poff = consumed_old & ~PAGE_MASK;
1255 //ust// printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
1256 //ust// len, (ssize_t)*ppos, local_read(&ltt_buf->offset));
1257 //ust//
1258 //ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1259 //ust// unsigned int this_len;
1260 //ust// struct buf_page *page;
1261 //ust//
1262 //ust// if (!len)
1263 //ust// break;
1264 //ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1265 //ust// len, roffset);
1266 //ust//
1267 //ust// this_len = PAGE_SIZE - poff;
1268 //ust// page = ltt_relay_read_get_page(buf, roffset);
1269 //ust// spd.pages[spd.nr_pages] = page->page;
1270 //ust// spd.partial[spd.nr_pages].offset = poff;
1271 //ust// spd.partial[spd.nr_pages].len = this_len;
1272 //ust//
1273 //ust// poff = 0;
1274 //ust// roffset += PAGE_SIZE;
1275 //ust// len -= this_len;
1276 //ust// }
1277 //ust//
1278 //ust// if (!spd.nr_pages)
1279 //ust// return 0;
1280 //ust//
1281 //ust// return splice_to_pipe(pipe, &spd);
1282 //ust// }
1283
1284 //ust// static ssize_t ltt_relay_file_splice_read(struct file *in,
1285 //ust// loff_t *ppos,
1286 //ust// struct pipe_inode_info *pipe,
1287 //ust// size_t len,
1288 //ust// unsigned int flags)
1289 //ust// {
1290 //ust// ssize_t spliced;
1291 //ust// int ret;
1292 //ust//
1293 //ust// ret = 0;
1294 //ust// spliced = 0;
1295 //ust//
1296 //ust// printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n",
1297 //ust// len, (ssize_t)*ppos);
1298 //ust// while (len && !spliced) {
1299 //ust// ret = subbuf_splice_actor(in, ppos, pipe, len, flags);
1300 //ust// printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
1301 //ust// if (ret < 0)
1302 //ust// break;
1303 //ust// else if (!ret) {
1304 //ust// if (flags & SPLICE_F_NONBLOCK)
1305 //ust// ret = -EAGAIN;
1306 //ust// break;
1307 //ust// }
1308 //ust//
1309 //ust// *ppos += ret;
1310 //ust// if (ret > len)
1311 //ust// len = 0;
1312 //ust// else
1313 //ust// len -= ret;
1314 //ust// spliced += ret;
1315 //ust// }
1316 //ust//
1317 //ust// if (spliced)
1318 //ust// return spliced;
1319 //ust//
1320 //ust// return ret;
1321 //ust// }
1322
1323 static void ltt_relay_print_subbuffer_errors(
1324 struct ltt_channel_struct *ltt_chan,
1325 long cons_off)
1326 {
1327 struct rchan *rchan = ltt_chan->trans_channel_data;
1328 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1329 long cons_idx, commit_count, write_offset;
1330
1331 cons_idx = SUBBUF_INDEX(cons_off, rchan);
1332 commit_count = local_read(&ltt_buf->commit_count[cons_idx]);
1333 /*
1334 * No need to order commit_count and write_offset reads because we
1335 * execute after trace is stopped when there are no readers left.
1336 */
1337 write_offset = local_read(&ltt_buf->offset);
1338 printk(KERN_WARNING
1339 "LTT : unread channel %s offset is %ld "
1340 "and cons_off : %ld\n",
1341 ltt_chan->channel_name, write_offset, cons_off);
1342 /* Check each sub-buffer for non filled commit count */
1343 if (((commit_count - rchan->subbuf_size) & ltt_chan->commit_count_mask)
1344 - (BUFFER_TRUNC(cons_off, rchan) >> ltt_chan->n_subbufs_order)
1345 != 0)
1346 printk(KERN_ALERT
1347 "LTT : %s : subbuffer %lu has non filled "
1348 "commit count %lu.\n",
1349 ltt_chan->channel_name, cons_idx, commit_count);
1350 printk(KERN_ALERT "LTT : %s : commit count : %lu, subbuf size %zd\n",
1351 ltt_chan->channel_name, commit_count,
1352 rchan->subbuf_size);
1353 }
1354
1355 static void ltt_relay_print_errors(struct ltt_trace_struct *trace,
1356 struct ltt_channel_struct *ltt_chan)
1357 {
1358 struct rchan *rchan = ltt_chan->trans_channel_data;
1359 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1360 long cons_off;
1361
1362 for (cons_off = atomic_long_read(&ltt_buf->consumed);
1363 (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1364 rchan)
1365 - cons_off) > 0;
1366 cons_off = SUBBUF_ALIGN(cons_off, rchan))
1367 ltt_relay_print_subbuffer_errors(ltt_chan, cons_off);
1368 }
1369
1370 static void ltt_relay_print_buffer_errors(struct ltt_channel_struct *ltt_chan)
1371 {
1372 struct ltt_trace_struct *trace = ltt_chan->trace;
1373 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1374
1375 if (local_read(&ltt_buf->events_lost))
1376 printk(KERN_ALERT
1377 "LTT : %s : %ld events lost "
1378 "in %s channel.\n",
1379 ltt_chan->channel_name,
1380 local_read(&ltt_buf->events_lost),
1381 ltt_chan->channel_name);
1382 if (local_read(&ltt_buf->corrupted_subbuffers))
1383 printk(KERN_ALERT
1384 "LTT : %s : %ld corrupted subbuffers "
1385 "in %s channel.\n",
1386 ltt_chan->channel_name,
1387 local_read(&ltt_buf->corrupted_subbuffers),
1388 ltt_chan->channel_name);
1389
1390 ltt_relay_print_errors(trace, ltt_chan);
1391 }
1392
1393 static void ltt_relay_remove_dirs(struct ltt_trace_struct *trace)
1394 {
1395 //ust// debugfs_remove(trace->dentry.trace_root);
1396 }
1397
1398 static void ltt_relay_release_channel(struct kref *kref)
1399 {
1400 struct ltt_channel_struct *ltt_chan = container_of(kref,
1401 struct ltt_channel_struct, kref);
1402 free(ltt_chan->buf);
1403 }
1404
1405 /*
1406 * Create ltt buffer.
1407 */
1408 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1409 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1410 //ust// unsigned int cpu, unsigned int n_subbufs)
1411 //ust// {
1412 //ust// struct ltt_channel_buf_struct *ltt_buf =
1413 //ust// percpu_ptr(ltt_chan->buf, cpu);
1414 //ust// unsigned int j;
1415 //ust//
1416 //ust// ltt_buf->commit_count =
1417 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
1418 //ust// GFP_KERNEL, cpu_to_node(cpu));
1419 //ust// if (!ltt_buf->commit_count)
1420 //ust// return -ENOMEM;
1421 //ust// kref_get(&trace->kref);
1422 //ust// kref_get(&trace->ltt_transport_kref);
1423 //ust// kref_get(&ltt_chan->kref);
1424 //ust// local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
1425 //ust// atomic_long_set(&ltt_buf->consumed, 0);
1426 //ust// atomic_long_set(&ltt_buf->active_readers, 0);
1427 //ust// for (j = 0; j < n_subbufs; j++)
1428 //ust// local_set(&ltt_buf->commit_count[j], 0);
1429 //ust// init_waitqueue_head(&ltt_buf->write_wait);
1430 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1431 //ust// spin_lock_init(&ltt_buf->full_lock);
1432 //ust//
1433 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1434 //ust// /* atomic_add made on local variable on data that belongs to
1435 //ust// * various CPUs : ok because tracing not started (for this cpu). */
1436 //ust// local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
1437 //ust//
1438 //ust// local_set(&ltt_buf->events_lost, 0);
1439 //ust// local_set(&ltt_buf->corrupted_subbuffers, 0);
1440 //ust//
1441 //ust// return 0;
1442 //ust// }
1443
1444 static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1445 struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1446 unsigned int n_subbufs)
1447 {
1448 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1449 unsigned int j;
1450
1451 ltt_buf->commit_count =
1452 zmalloc(sizeof(ltt_buf->commit_count) * n_subbufs);
1453 if (!ltt_buf->commit_count)
1454 return -ENOMEM;
1455 kref_get(&trace->kref);
1456 kref_get(&trace->ltt_transport_kref);
1457 kref_get(&ltt_chan->kref);
1458 local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
1459 atomic_long_set(&ltt_buf->consumed, 0);
1460 atomic_long_set(&ltt_buf->active_readers, 0);
1461 for (j = 0; j < n_subbufs; j++)
1462 local_set(&ltt_buf->commit_count[j], 0);
1463 //ust// init_waitqueue_head(&ltt_buf->write_wait);
1464 atomic_set(&ltt_buf->wakeup_readers, 0);
1465 spin_lock_init(&ltt_buf->full_lock);
1466
1467 ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1468
1469 local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
1470
1471 local_set(&ltt_buf->events_lost, 0);
1472 local_set(&ltt_buf->corrupted_subbuffers, 0);
1473
1474 return 0;
1475 }
1476
1477 static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan)
1478 {
1479 struct ltt_trace_struct *trace = ltt_chan->trace;
1480 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1481
1482 kref_put(&ltt_chan->trace->ltt_transport_kref,
1483 ltt_release_transport);
1484 ltt_relay_print_buffer_errors(ltt_chan);
1485 kfree(ltt_buf->commit_count);
1486 ltt_buf->commit_count = NULL;
1487 kref_put(&ltt_chan->kref, ltt_relay_release_channel);
1488 kref_put(&trace->kref, ltt_release_trace);
1489 //ust// wake_up_interruptible(&trace->kref_wq);
1490 }
1491
1492 /*
1493 * Create channel.
1494 */
1495 static int ltt_relay_create_channel(const char *trace_name,
1496 struct ltt_trace_struct *trace, struct dentry *dir,
1497 const char *channel_name, struct ltt_channel_struct *ltt_chan,
1498 unsigned int subbuf_size, unsigned int n_subbufs,
1499 int overwrite)
1500 {
1501 char *tmpname;
1502 unsigned int tmpname_len;
1503 int err = 0;
1504
1505 tmpname = kmalloc(PATH_MAX, GFP_KERNEL);
1506 if (!tmpname)
1507 return EPERM;
1508 if (overwrite) {
1509 strncpy(tmpname, LTT_FLIGHT_PREFIX, PATH_MAX-1);
1510 strncat(tmpname, channel_name,
1511 PATH_MAX-1-sizeof(LTT_FLIGHT_PREFIX));
1512 } else {
1513 strncpy(tmpname, channel_name, PATH_MAX-1);
1514 }
1515 strncat(tmpname, "_", PATH_MAX-1-strlen(tmpname));
1516
1517 kref_init(&ltt_chan->kref);
1518
1519 ltt_chan->trace = trace;
1520 ltt_chan->buffer_begin = ltt_buffer_begin_callback;
1521 ltt_chan->buffer_end = ltt_buffer_end_callback;
1522 ltt_chan->overwrite = overwrite;
1523 ltt_chan->n_subbufs_order = get_count_order(n_subbufs);
1524 ltt_chan->commit_count_mask = (~0UL >> ltt_chan->n_subbufs_order);
1525 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
1526 ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
1527 if (!ltt_chan->buf)
1528 goto alloc_error;
1529 ltt_chan->trans_channel_data = ltt_relay_open(tmpname,
1530 dir,
1531 subbuf_size,
1532 n_subbufs,
1533 ltt_chan);
1534 tmpname_len = strlen(tmpname);
1535 if (tmpname_len > 0) {
1536 /* Remove final _ for pretty printing */
1537 tmpname[tmpname_len-1] = '\0';
1538 }
1539 if (ltt_chan->trans_channel_data == NULL) {
1540 printk(KERN_ERR "LTT : Can't open %s channel for trace %s\n",
1541 tmpname, trace_name);
1542 goto relay_open_error;
1543 }
1544
1545 err = 0;
1546 goto end;
1547
1548 relay_open_error:
1549 //ust// percpu_free(ltt_chan->buf);
1550 alloc_error:
1551 err = EPERM;
1552 end:
1553 kfree(tmpname);
1554 return err;
1555 }
1556
1557 static int ltt_relay_create_dirs(struct ltt_trace_struct *new_trace)
1558 {
1559 //ust// new_trace->dentry.trace_root = debugfs_create_dir(new_trace->trace_name,
1560 //ust// get_ltt_root());
1561 //ust// if (new_trace->dentry.trace_root == NULL) {
1562 //ust// printk(KERN_ERR "LTT : Trace directory name %s already taken\n",
1563 //ust// new_trace->trace_name);
1564 //ust// return EEXIST;
1565 //ust// }
1566
1567 //ust// new_trace->callbacks.create_buf_file = ltt_create_buf_file_callback;
1568 //ust// new_trace->callbacks.remove_buf_file = ltt_remove_buf_file_callback;
1569
1570 return 0;
1571 }
1572
1573 /*
1574 * LTTng channel flush function.
1575 *
1576 * Must be called when no tracing is active in the channel, because of
1577 * accesses across CPUs.
1578 */
1579 static notrace void ltt_relay_buffer_flush(struct rchan_buf *buf)
1580 {
1581 buf->finalized = 1;
1582 ltt_force_switch(buf, FORCE_FLUSH);
1583 }
1584
1585 static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct *ltt_channel)
1586 {
1587 //ust// unsigned int i;
1588 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1589 //ust//
1590 //ust// for_each_possible_cpu(i) {
1591 //ust// struct ltt_channel_buf_struct *ltt_buf =
1592 //ust// percpu_ptr(ltt_channel->buf, i);
1593 //ust//
1594 //ust// if (atomic_read(&ltt_buf->wakeup_readers) == 1) {
1595 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1596 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1597 //ust// }
1598 //ust// }
1599 }
1600
1601 static void ltt_relay_finish_buffer(struct ltt_channel_struct *ltt_channel)
1602 {
1603 struct rchan *rchan = ltt_channel->trans_channel_data;
1604
1605 if (rchan->buf) {
1606 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
1607 ltt_relay_buffer_flush(rchan->buf);
1608 //ust// ltt_relay_wake_writers(ltt_buf);
1609 }
1610 }
1611
1612
1613 static void ltt_relay_finish_channel(struct ltt_channel_struct *ltt_channel)
1614 {
1615 unsigned int i;
1616
1617 //ust// for_each_possible_cpu(i)
1618 ltt_relay_finish_buffer(ltt_channel);
1619 }
1620
1621 static void ltt_relay_remove_channel(struct ltt_channel_struct *channel)
1622 {
1623 struct rchan *rchan = channel->trans_channel_data;
1624
1625 ltt_relay_close(rchan);
1626 kref_put(&channel->kref, ltt_relay_release_channel);
1627 }
1628
1629 struct ltt_reserve_switch_offsets {
1630 long begin, end, old;
1631 long begin_switch, end_switch_current, end_switch_old;
1632 long commit_count, reserve_commit_diff;
1633 size_t before_hdr_pad, size;
1634 };
1635
1636 /*
1637 * Returns :
1638 * 0 if ok
1639 * !0 if execution must be aborted.
1640 */
1641 static inline int ltt_relay_try_reserve(
1642 struct ltt_channel_struct *ltt_channel,
1643 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1644 struct rchan_buf *buf,
1645 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
1646 u64 *tsc, unsigned int *rflags, int largest_align)
1647 {
1648 offsets->begin = local_read(&ltt_buf->offset);
1649 offsets->old = offsets->begin;
1650 offsets->begin_switch = 0;
1651 offsets->end_switch_current = 0;
1652 offsets->end_switch_old = 0;
1653
1654 *tsc = trace_clock_read64();
1655 if (last_tsc_overflow(ltt_buf, *tsc))
1656 *rflags = LTT_RFLAG_ID_SIZE_TSC;
1657
1658 if (SUBBUF_OFFSET(offsets->begin, buf->chan) == 0) {
1659 offsets->begin_switch = 1; /* For offsets->begin */
1660 } else {
1661 offsets->size = ltt_get_header_size(ltt_channel,
1662 offsets->begin, data_size,
1663 &offsets->before_hdr_pad, *rflags);
1664 offsets->size += ltt_align(offsets->begin + offsets->size,
1665 largest_align)
1666 + data_size;
1667 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
1668 > buf->chan->subbuf_size) {
1669 offsets->end_switch_old = 1; /* For offsets->old */
1670 offsets->begin_switch = 1; /* For offsets->begin */
1671 }
1672 }
1673 if (offsets->begin_switch) {
1674 long subbuf_index;
1675
1676 if (offsets->end_switch_old)
1677 offsets->begin = SUBBUF_ALIGN(offsets->begin,
1678 buf->chan);
1679 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
1680 /* Test new buffer integrity */
1681 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1682 offsets->reserve_commit_diff =
1683 (BUFFER_TRUNC(offsets->begin, buf->chan)
1684 >> ltt_channel->n_subbufs_order)
1685 - (local_read(&ltt_buf->commit_count[subbuf_index])
1686 & ltt_channel->commit_count_mask);
1687 if (offsets->reserve_commit_diff == 0) {
1688 /* Next buffer not corrupted. */
1689 if (!ltt_channel->overwrite &&
1690 (SUBBUF_TRUNC(offsets->begin, buf->chan)
1691 - SUBBUF_TRUNC(atomic_long_read(
1692 &ltt_buf->consumed),
1693 buf->chan))
1694 >= rchan->alloc_size) {
1695 /*
1696 * We do not overwrite non consumed buffers
1697 * and we are full : event is lost.
1698 */
1699 local_inc(&ltt_buf->events_lost);
1700 return -1;
1701 } else {
1702 /*
1703 * next buffer not corrupted, we are either in
1704 * overwrite mode or the buffer is not full.
1705 * It's safe to write in this new subbuffer.
1706 */
1707 }
1708 } else {
1709 /*
1710 * Next subbuffer corrupted. Force pushing reader even
1711 * in normal mode. It's safe to write in this new
1712 * subbuffer.
1713 */
1714 }
1715 offsets->size = ltt_get_header_size(ltt_channel,
1716 offsets->begin, data_size,
1717 &offsets->before_hdr_pad, *rflags);
1718 offsets->size += ltt_align(offsets->begin + offsets->size,
1719 largest_align)
1720 + data_size;
1721 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
1722 > buf->chan->subbuf_size) {
1723 /*
1724 * Event too big for subbuffers, report error, don't
1725 * complete the sub-buffer switch.
1726 */
1727 local_inc(&ltt_buf->events_lost);
1728 return -1;
1729 } else {
1730 /*
1731 * We just made a successful buffer switch and the event
1732 * fits in the new subbuffer. Let's write.
1733 */
1734 }
1735 } else {
1736 /*
1737 * Event fits in the current buffer and we are not on a switch
1738 * boundary. It's safe to write.
1739 */
1740 }
1741 offsets->end = offsets->begin + offsets->size;
1742
1743 if ((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0) {
1744 /*
1745 * The offset_end will fall at the very beginning of the next
1746 * subbuffer.
1747 */
1748 offsets->end_switch_current = 1; /* For offsets->begin */
1749 }
1750 return 0;
1751 }
1752
1753 /*
1754 * Returns :
1755 * 0 if ok
1756 * !0 if execution must be aborted.
1757 */
1758 static inline int ltt_relay_try_switch(
1759 enum force_switch_mode mode,
1760 struct ltt_channel_struct *ltt_channel,
1761 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1762 struct rchan_buf *buf,
1763 struct ltt_reserve_switch_offsets *offsets,
1764 u64 *tsc)
1765 {
1766 long subbuf_index;
1767
1768 offsets->begin = local_read(&ltt_buf->offset);
1769 offsets->old = offsets->begin;
1770 offsets->begin_switch = 0;
1771 offsets->end_switch_old = 0;
1772
1773 *tsc = trace_clock_read64();
1774
1775 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
1776 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
1777 offsets->end_switch_old = 1;
1778 } else {
1779 /* we do not have to switch : buffer is empty */
1780 return -1;
1781 }
1782 if (mode == FORCE_ACTIVE)
1783 offsets->begin += ltt_subbuffer_header_size();
1784 /*
1785 * Always begin_switch in FORCE_ACTIVE mode.
1786 * Test new buffer integrity
1787 */
1788 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1789 offsets->reserve_commit_diff =
1790 (BUFFER_TRUNC(offsets->begin, buf->chan)
1791 >> ltt_channel->n_subbufs_order)
1792 - (local_read(&ltt_buf->commit_count[subbuf_index])
1793 & ltt_channel->commit_count_mask);
1794 if (offsets->reserve_commit_diff == 0) {
1795 /* Next buffer not corrupted. */
1796 if (mode == FORCE_ACTIVE
1797 && !ltt_channel->overwrite
1798 && offsets->begin - atomic_long_read(&ltt_buf->consumed)
1799 >= rchan->alloc_size) {
1800 /*
1801 * We do not overwrite non consumed buffers and we are
1802 * full : ignore switch while tracing is active.
1803 */
1804 return -1;
1805 }
1806 } else {
1807 /*
1808 * Next subbuffer corrupted. Force pushing reader even in normal
1809 * mode
1810 */
1811 }
1812 offsets->end = offsets->begin;
1813 return 0;
1814 }
1815
1816 static inline void ltt_reserve_push_reader(
1817 struct ltt_channel_struct *ltt_channel,
1818 struct ltt_channel_buf_struct *ltt_buf,
1819 struct rchan *rchan,
1820 struct rchan_buf *buf,
1821 struct ltt_reserve_switch_offsets *offsets)
1822 {
1823 long consumed_old, consumed_new;
1824
1825 do {
1826 consumed_old = atomic_long_read(&ltt_buf->consumed);
1827 /*
1828 * If buffer is in overwrite mode, push the reader consumed
1829 * count if the write position has reached it and we are not
1830 * at the first iteration (don't push the reader farther than
1831 * the writer). This operation can be done concurrently by many
1832 * writers in the same buffer, the writer being at the farthest
1833 * write position sub-buffer index in the buffer being the one
1834 * which will win this loop.
1835 * If the buffer is not in overwrite mode, pushing the reader
1836 * only happens if a sub-buffer is corrupted.
1837 */
1838 if ((SUBBUF_TRUNC(offsets->end-1, buf->chan)
1839 - SUBBUF_TRUNC(consumed_old, buf->chan))
1840 >= rchan->alloc_size)
1841 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1842 else {
1843 consumed_new = consumed_old;
1844 break;
1845 }
1846 } while (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1847 consumed_new) != consumed_old);
1848
1849 if (consumed_old != consumed_new) {
1850 /*
1851 * Reader pushed : we are the winner of the push, we can
1852 * therefore reequilibrate reserve and commit. Atomic increment
1853 * of the commit count permits other writers to play around
1854 * with this variable before us. We keep track of
1855 * corrupted_subbuffers even in overwrite mode :
1856 * we never want to write over a non completely committed
1857 * sub-buffer : possible causes : the buffer size is too low
1858 * compared to the unordered data input, or there is a writer
1859 * that died between the reserve and the commit.
1860 */
1861 if (offsets->reserve_commit_diff) {
1862 /*
1863 * We have to alter the sub-buffer commit count.
1864 * We do not deliver the previous subbuffer, given it
1865 * was either corrupted or not consumed (overwrite
1866 * mode).
1867 */
1868 local_add(offsets->reserve_commit_diff,
1869 &ltt_buf->commit_count[
1870 SUBBUF_INDEX(offsets->begin,
1871 buf->chan)]);
1872 if (!ltt_channel->overwrite
1873 || offsets->reserve_commit_diff
1874 != rchan->subbuf_size) {
1875 /*
1876 * The reserve commit diff was not subbuf_size :
1877 * it means the subbuffer was partly written to
1878 * and is therefore corrupted. If it is multiple
1879 * of subbuffer size and we are in flight
1880 * recorder mode, we are skipping over a whole
1881 * subbuffer.
1882 */
1883 local_inc(&ltt_buf->corrupted_subbuffers);
1884 }
1885 }
1886 }
1887 }
1888
1889
1890 /*
1891 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1892 *
1893 * Concurrency safe because we are the last and only thread to alter this
1894 * sub-buffer. As long as it is not delivered and read, no other thread can
1895 * alter the offset, alter the reserve_count or call the
1896 * client_buffer_end_callback on this sub-buffer.
1897 *
1898 * The only remaining threads could be the ones with pending commits. They will
1899 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1900 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1901 * corrupted sub-buffers count and push the readers across these sub-buffers.
1902 *
1903 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1904 * switches in, finding out it's corrupted. The result will be than the old
1905 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1906 * will be declared corrupted too because of the commit count adjustment.
1907 *
1908 * Note : offset_old should never be 0 here.
1909 */
1910 static inline void ltt_reserve_switch_old_subbuf(
1911 struct ltt_channel_struct *ltt_channel,
1912 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1913 struct rchan_buf *buf,
1914 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1915 {
1916 long oldidx = SUBBUF_INDEX(offsets->old - 1, rchan);
1917
1918 ltt_channel->buffer_end(buf, *tsc, offsets->old, oldidx);
1919 /* Must write buffer end before incrementing commit count */
1920 smp_wmb();
1921 offsets->commit_count =
1922 local_add_return(rchan->subbuf_size
1923 - (SUBBUF_OFFSET(offsets->old - 1, rchan)
1924 + 1),
1925 &ltt_buf->commit_count[oldidx]);
1926 if ((BUFFER_TRUNC(offsets->old - 1, rchan)
1927 >> ltt_channel->n_subbufs_order)
1928 - ((offsets->commit_count - rchan->subbuf_size)
1929 & ltt_channel->commit_count_mask) == 0)
1930 ltt_deliver(buf, oldidx, NULL);
1931 }
1932
1933 /*
1934 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
1935 *
1936 * This code can be executed unordered : writers may already have written to the
1937 * sub-buffer before this code gets executed, caution. The commit makes sure
1938 * that this code is executed before the deliver of this sub-buffer.
1939 */
1940 static inline void ltt_reserve_switch_new_subbuf(
1941 struct ltt_channel_struct *ltt_channel,
1942 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1943 struct rchan_buf *buf,
1944 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1945 {
1946 long beginidx = SUBBUF_INDEX(offsets->begin, rchan);
1947
1948 ltt_channel->buffer_begin(buf, *tsc, beginidx);
1949 /* Must write buffer end before incrementing commit count */
1950 smp_wmb();
1951 offsets->commit_count = local_add_return(ltt_subbuffer_header_size(),
1952 &ltt_buf->commit_count[beginidx]);
1953 /* Check if the written buffer has to be delivered */
1954 if ((BUFFER_TRUNC(offsets->begin, rchan)
1955 >> ltt_channel->n_subbufs_order)
1956 - ((offsets->commit_count - rchan->subbuf_size)
1957 & ltt_channel->commit_count_mask) == 0)
1958 ltt_deliver(buf, beginidx, NULL);
1959 }
1960
1961
1962 /*
1963 * ltt_reserve_end_switch_current: finish switching current subbuffer
1964 *
1965 * Concurrency safe because we are the last and only thread to alter this
1966 * sub-buffer. As long as it is not delivered and read, no other thread can
1967 * alter the offset, alter the reserve_count or call the
1968 * client_buffer_end_callback on this sub-buffer.
1969 *
1970 * The only remaining threads could be the ones with pending commits. They will
1971 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1972 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1973 * corrupted sub-buffers count and push the readers across these sub-buffers.
1974 *
1975 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1976 * switches in, finding out it's corrupted. The result will be than the old
1977 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1978 * will be declared corrupted too because of the commit count adjustment.
1979 */
1980 static inline void ltt_reserve_end_switch_current(
1981 struct ltt_channel_struct *ltt_channel,
1982 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1983 struct rchan_buf *buf,
1984 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1985 {
1986 long endidx = SUBBUF_INDEX(offsets->end - 1, rchan);
1987
1988 ltt_channel->buffer_end(buf, *tsc, offsets->end, endidx);
1989 /* Must write buffer begin before incrementing commit count */
1990 smp_wmb();
1991 offsets->commit_count =
1992 local_add_return(rchan->subbuf_size
1993 - (SUBBUF_OFFSET(offsets->end - 1, rchan)
1994 + 1),
1995 &ltt_buf->commit_count[endidx]);
1996 if ((BUFFER_TRUNC(offsets->end - 1, rchan)
1997 >> ltt_channel->n_subbufs_order)
1998 - ((offsets->commit_count - rchan->subbuf_size)
1999 & ltt_channel->commit_count_mask) == 0)
2000 ltt_deliver(buf, endidx, NULL);
2001 }
2002
2003 /**
2004 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
2005 * @trace: the trace structure to log to.
2006 * @ltt_channel: channel structure
2007 * @transport_data: data structure specific to ltt relay
2008 * @data_size: size of the variable length data to log.
2009 * @slot_size: pointer to total size of the slot (out)
2010 * @buf_offset : pointer to reserved buffer offset (out)
2011 * @tsc: pointer to the tsc at the slot reservation (out)
2012 * @cpu: cpuid
2013 *
2014 * Return : -ENOSPC if not enough space, else returns 0.
2015 * It will take care of sub-buffer switching.
2016 */
2017 static notrace int ltt_relay_reserve_slot(struct ltt_trace_struct *trace,
2018 struct ltt_channel_struct *ltt_channel, void **transport_data,
2019 size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc,
2020 unsigned int *rflags, int largest_align)
2021 {
2022 struct rchan *rchan = ltt_channel->trans_channel_data;
2023 struct rchan_buf *buf = *transport_data = rchan->buf;
2024 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
2025 struct ltt_reserve_switch_offsets offsets;
2026
2027 offsets.reserve_commit_diff = 0;
2028 offsets.size = 0;
2029
2030 /*
2031 * Perform retryable operations.
2032 */
2033 if (ltt_nesting > 4) {
2034 local_inc(&ltt_buf->events_lost);
2035 return -EPERM;
2036 }
2037 do {
2038 if (ltt_relay_try_reserve(ltt_channel, ltt_buf,
2039 rchan, buf, &offsets, data_size, tsc, rflags,
2040 largest_align))
2041 return -ENOSPC;
2042 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2043 offsets.end) != offsets.old);
2044
2045 /*
2046 * Atomically update last_tsc. This update races against concurrent
2047 * atomic updates, but the race will always cause supplementary full TSC
2048 * events, never the opposite (missing a full TSC event when it would be
2049 * needed).
2050 */
2051 save_last_tsc(ltt_buf, *tsc);
2052
2053 /*
2054 * Push the reader if necessary
2055 */
2056 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan, buf, &offsets);
2057
2058 /*
2059 * Switch old subbuffer if needed.
2060 */
2061 if (offsets.end_switch_old)
2062 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2063 &offsets, tsc);
2064
2065 /*
2066 * Populate new subbuffer.
2067 */
2068 if (offsets.begin_switch)
2069 ltt_reserve_switch_new_subbuf(ltt_channel, ltt_buf, rchan,
2070 buf, &offsets, tsc);
2071
2072 if (offsets.end_switch_current)
2073 ltt_reserve_end_switch_current(ltt_channel, ltt_buf, rchan,
2074 buf, &offsets, tsc);
2075
2076 *slot_size = offsets.size;
2077 *buf_offset = offsets.begin + offsets.before_hdr_pad;
2078 return 0;
2079 }
2080
2081 /*
2082 * Force a sub-buffer switch for a per-cpu buffer. This operation is
2083 * completely reentrant : can be called while tracing is active with
2084 * absolutely no lock held.
2085 *
2086 * Note, however, that as a local_cmpxchg is used for some atomic
2087 * operations, this function must be called from the CPU which owns the buffer
2088 * for a ACTIVE flush.
2089 */
2090 static notrace void ltt_force_switch(struct rchan_buf *buf,
2091 enum force_switch_mode mode)
2092 {
2093 struct ltt_channel_struct *ltt_channel =
2094 (struct ltt_channel_struct *)buf->chan->private_data;
2095 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
2096 struct rchan *rchan = ltt_channel->trans_channel_data;
2097 struct ltt_reserve_switch_offsets offsets;
2098 u64 tsc;
2099
2100 offsets.reserve_commit_diff = 0;
2101 offsets.size = 0;
2102
2103 /*
2104 * Perform retryable operations.
2105 */
2106 do {
2107 if (ltt_relay_try_switch(mode, ltt_channel, ltt_buf,
2108 rchan, buf, &offsets, &tsc))
2109 return;
2110 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2111 offsets.end) != offsets.old);
2112
2113 /*
2114 * Atomically update last_tsc. This update races against concurrent
2115 * atomic updates, but the race will always cause supplementary full TSC
2116 * events, never the opposite (missing a full TSC event when it would be
2117 * needed).
2118 */
2119 save_last_tsc(ltt_buf, tsc);
2120
2121 /*
2122 * Push the reader if necessary
2123 */
2124 if (mode == FORCE_ACTIVE)
2125 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan,
2126 buf, &offsets);
2127
2128 /*
2129 * Switch old subbuffer if needed.
2130 */
2131 if (offsets.end_switch_old)
2132 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2133 &offsets, &tsc);
2134
2135 /*
2136 * Populate new subbuffer.
2137 */
2138 if (mode == FORCE_ACTIVE)
2139 ltt_reserve_switch_new_subbuf(ltt_channel,
2140 ltt_buf, rchan, buf, &offsets, &tsc);
2141 }
2142
2143 /*
2144 * for flight recording. must be called after relay_commit.
2145 * This function decrements de subbuffer's lost_size each time the commit count
2146 * reaches back the reserve offset (module subbuffer size). It is useful for
2147 * crash dump.
2148 * We use slot_size - 1 to make sure we deal correctly with the case where we
2149 * fill the subbuffer completely (so the subbuf index stays in the previous
2150 * subbuffer).
2151 */
2152 #ifdef CONFIG_LTT_VMCORE
2153 static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2154 long buf_offset, size_t slot_size)
2155 {
2156 struct ltt_channel_struct *ltt_channel =
2157 (struct ltt_channel_struct *)buf->chan->private_data;
2158 struct ltt_channel_buf_struct *ltt_buf =
2159 percpu_ptr(ltt_channel->buf, buf->cpu);
2160 struct ltt_subbuffer_header *header;
2161 long offset, subbuf_idx, commit_count;
2162 uint32_t lost_old, lost_new;
2163
2164 subbuf_idx = SUBBUF_INDEX(buf_offset - 1, buf->chan);
2165 offset = buf_offset + slot_size;
2166 header = (struct ltt_subbuffer_header *)
2167 ltt_relay_offset_address(buf,
2168 subbuf_idx * buf->chan->subbuf_size);
2169 for (;;) {
2170 lost_old = header->lost_size;
2171 commit_count =
2172 local_read(&ltt_buf->commit_count[subbuf_idx]);
2173 /* SUBBUF_OFFSET includes commit_count_mask */
2174 if (!SUBBUF_OFFSET(offset - commit_count, buf->chan)) {
2175 lost_new = (uint32_t)buf->chan->subbuf_size
2176 - SUBBUF_OFFSET(commit_count, buf->chan);
2177 lost_old = cmpxchg_local(&header->lost_size, lost_old,
2178 lost_new);
2179 if (lost_old <= lost_new)
2180 break;
2181 } else {
2182 break;
2183 }
2184 }
2185 }
2186 #else
2187 static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2188 long buf_offset, size_t slot_size)
2189 {
2190 }
2191 #endif
2192
2193 /*
2194 * Atomic unordered slot commit. Increments the commit count in the
2195 * specified sub-buffer, and delivers it if necessary.
2196 *
2197 * Parameters:
2198 *
2199 * @ltt_channel : channel structure
2200 * @transport_data: transport-specific data
2201 * @buf_offset : offset following the event header.
2202 * @slot_size : size of the reserved slot.
2203 */
2204 static notrace void ltt_relay_commit_slot(
2205 struct ltt_channel_struct *ltt_channel,
2206 void **transport_data, long buf_offset, size_t slot_size)
2207 {
2208 struct rchan_buf *buf = *transport_data;
2209 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
2210 struct rchan *rchan = buf->chan;
2211 long offset_end = buf_offset;
2212 long endidx = SUBBUF_INDEX(offset_end - 1, rchan);
2213 long commit_count;
2214
2215 /* Must write slot data before incrementing commit count */
2216 smp_wmb();
2217 commit_count = local_add_return(slot_size,
2218 &ltt_buf->commit_count[endidx]);
2219 /* Check if all commits have been done */
2220 if ((BUFFER_TRUNC(offset_end - 1, rchan)
2221 >> ltt_channel->n_subbufs_order)
2222 - ((commit_count - rchan->subbuf_size)
2223 & ltt_channel->commit_count_mask) == 0)
2224 ltt_deliver(buf, endidx, NULL);
2225 /*
2226 * Update lost_size for each commit. It's needed only for extracting
2227 * ltt buffers from vmcore, after crash.
2228 */
2229 ltt_write_commit_counter(buf, buf_offset, slot_size);
2230 }
2231
2232 /*
2233 * This is called with preemption disabled when user space has requested
2234 * blocking mode. If one of the active traces has free space below a
2235 * specific threshold value, we reenable preemption and block.
2236 */
2237 static int ltt_relay_user_blocking(struct ltt_trace_struct *trace,
2238 unsigned int chan_index, size_t data_size,
2239 struct user_dbg_data *dbg)
2240 {
2241 //ust// struct rchan *rchan;
2242 //ust// struct ltt_channel_buf_struct *ltt_buf;
2243 //ust// struct ltt_channel_struct *channel;
2244 //ust// struct rchan_buf *relay_buf;
2245 //ust// int cpu;
2246 //ust// DECLARE_WAITQUEUE(wait, current);
2247 //ust//
2248 //ust// channel = &trace->channels[chan_index];
2249 //ust// rchan = channel->trans_channel_data;
2250 //ust// cpu = smp_processor_id();
2251 //ust// relay_buf = rchan->buf[cpu];
2252 //ust// ltt_buf = percpu_ptr(channel->buf, cpu);
2253 //ust//
2254 //ust// /*
2255 //ust// * Check if data is too big for the channel : do not
2256 //ust// * block for it.
2257 //ust// */
2258 //ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2259 //ust// return 0;
2260 //ust//
2261 //ust// /*
2262 //ust// * If free space too low, we block. We restart from the
2263 //ust// * beginning after we resume (cpu id may have changed
2264 //ust// * while preemption is active).
2265 //ust// */
2266 //ust// spin_lock(&ltt_buf->full_lock);
2267 //ust// if (!channel->overwrite) {
2268 //ust// dbg->write = local_read(&ltt_buf->offset);
2269 //ust// dbg->read = atomic_long_read(&ltt_buf->consumed);
2270 //ust// dbg->avail_size = dbg->write + LTT_RESERVE_CRITICAL + data_size
2271 //ust// - SUBBUF_TRUNC(dbg->read,
2272 //ust// relay_buf->chan);
2273 //ust// if (dbg->avail_size > rchan->alloc_size) {
2274 //ust// __set_current_state(TASK_INTERRUPTIBLE);
2275 //ust// add_wait_queue(&ltt_buf->write_wait, &wait);
2276 //ust// spin_unlock(&ltt_buf->full_lock);
2277 //ust// preempt_enable();
2278 //ust// schedule();
2279 //ust// __set_current_state(TASK_RUNNING);
2280 //ust// remove_wait_queue(&ltt_buf->write_wait, &wait);
2281 //ust// if (signal_pending(current))
2282 //ust// return -ERESTARTSYS;
2283 //ust// preempt_disable();
2284 //ust// return 1;
2285 //ust// }
2286 //ust// }
2287 //ust// spin_unlock(&ltt_buf->full_lock);
2288 return 0;
2289 }
2290
2291 static void ltt_relay_print_user_errors(struct ltt_trace_struct *trace,
2292 unsigned int chan_index, size_t data_size,
2293 struct user_dbg_data *dbg)
2294 {
2295 struct rchan *rchan;
2296 struct ltt_channel_buf_struct *ltt_buf;
2297 struct ltt_channel_struct *channel;
2298 struct rchan_buf *relay_buf;
2299
2300 channel = &trace->channels[chan_index];
2301 rchan = channel->trans_channel_data;
2302 relay_buf = rchan->buf;
2303 ltt_buf = channel->buf;
2304
2305 printk(KERN_ERR "Error in LTT usertrace : "
2306 "buffer full : event lost in blocking "
2307 "mode. Increase LTT_RESERVE_CRITICAL.\n");
2308 printk(KERN_ERR "LTT nesting level is %u.\n", ltt_nesting);
2309 printk(KERN_ERR "LTT avail size %lu.\n",
2310 dbg->avail_size);
2311 printk(KERN_ERR "avai write : %lu, read : %lu\n",
2312 dbg->write, dbg->read);
2313
2314 dbg->write = local_read(&ltt_buf->offset);
2315 dbg->read = atomic_long_read(&ltt_buf->consumed);
2316
2317 printk(KERN_ERR "LTT cur size %lu.\n",
2318 dbg->write + LTT_RESERVE_CRITICAL + data_size
2319 - SUBBUF_TRUNC(dbg->read, relay_buf->chan));
2320 printk(KERN_ERR "cur write : %lu, read : %lu\n",
2321 dbg->write, dbg->read);
2322 }
2323
2324 //ust// static struct ltt_transport ltt_relay_transport = {
2325 //ust// .name = "relay",
2326 //ust// .owner = THIS_MODULE,
2327 //ust// .ops = {
2328 //ust// .create_dirs = ltt_relay_create_dirs,
2329 //ust// .remove_dirs = ltt_relay_remove_dirs,
2330 //ust// .create_channel = ltt_relay_create_channel,
2331 //ust// .finish_channel = ltt_relay_finish_channel,
2332 //ust// .remove_channel = ltt_relay_remove_channel,
2333 //ust// .wakeup_channel = ltt_relay_async_wakeup_chan,
2334 //ust// .commit_slot = ltt_relay_commit_slot,
2335 //ust// .reserve_slot = ltt_relay_reserve_slot,
2336 //ust// .user_blocking = ltt_relay_user_blocking,
2337 //ust// .user_errors = ltt_relay_print_user_errors,
2338 //ust// },
2339 //ust// };
2340
2341 static struct ltt_transport ust_relay_transport = {
2342 .name = "ustrelay",
2343 //ust// .owner = THIS_MODULE,
2344 .ops = {
2345 .create_dirs = ltt_relay_create_dirs,
2346 .remove_dirs = ltt_relay_remove_dirs,
2347 .create_channel = ltt_relay_create_channel,
2348 .finish_channel = ltt_relay_finish_channel,
2349 .remove_channel = ltt_relay_remove_channel,
2350 .wakeup_channel = ltt_relay_async_wakeup_chan,
2351 .commit_slot = ltt_relay_commit_slot,
2352 .reserve_slot = ltt_relay_reserve_slot,
2353 .user_blocking = ltt_relay_user_blocking,
2354 .user_errors = ltt_relay_print_user_errors,
2355 },
2356 };
2357
2358 //ust// static int __init ltt_relay_init(void)
2359 //ust// {
2360 //ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2361 //ust//
2362 //ust// ltt_file_operations = ltt_relay_file_operations;
2363 //ust// ltt_file_operations.owner = THIS_MODULE;
2364 //ust// ltt_file_operations.open = ltt_open;
2365 //ust// ltt_file_operations.release = ltt_release;
2366 //ust// ltt_file_operations.poll = ltt_poll;
2367 //ust// ltt_file_operations.splice_read = ltt_relay_file_splice_read,
2368 //ust// ltt_file_operations.ioctl = ltt_ioctl;
2369 //ust//#ifdef CONFIG_COMPAT
2370 //ust// ltt_file_operations.compat_ioctl = ltt_compat_ioctl;
2371 //ust//#endif
2372 //ust//
2373 //ust// ltt_transport_register(&ltt_relay_transport);
2374 //ust//
2375 //ust// return 0;
2376 //ust// }
2377
2378 void init_ustrelay_transport(void)
2379 {
2380 ltt_transport_register(&ust_relay_transport);
2381 }
2382
2383 static void __exit ltt_relay_exit(void)
2384 {
2385 //ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
2386
2387 ltt_transport_unregister(&ust_relay_transport);
2388 }
2389
2390 //ust// module_init(ltt_relay_init);
2391 //ust// module_exit(ltt_relay_exit);
2392 //ust//
2393 //ust// MODULE_LICENSE("GPL");
2394 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
2395 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");
This page took 0.084178 seconds and 5 git commands to generate.