]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/trace/ring_buffer.c
ring-buffer: add locks around rb_per_cpu_empty
[mirror_ubuntu-bionic-kernel.git] / kernel / trace / ring_buffer.c
1 /*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_clock.h>
8 #include <linux/ftrace_irq.h>
9 #include <linux/spinlock.h>
10 #include <linux/debugfs.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <linux/mutex.h>
16 #include <linux/init.h>
17 #include <linux/hash.h>
18 #include <linux/list.h>
19 #include <linux/cpu.h>
20 #include <linux/fs.h>
21
22 #include "trace.h"
23
24 /*
25 * The ring buffer header is special. We must manually up keep it.
26 */
27 int ring_buffer_print_entry_header(struct trace_seq *s)
28 {
29 int ret;
30
31 ret = trace_seq_printf(s, "# compressed entry header\n");
32 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
33 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
40 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
41 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
42
43 return ret;
44 }
45
46 /*
47 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
51 *
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
55 *
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
59 *
60 * Here's some silly ASCII art.
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |
65 * +------+ +---+ +---+ +---+
66 * | |-->| |-->| |
67 * +---+ +---+ +---+
68 * ^ |
69 * | |
70 * +---------------+
71 *
72 *
73 * +------+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * | |-->| |-->| |
78 * +---+ +---+ +---+
79 * ^ |
80 * | |
81 * +---------------+
82 *
83 *
84 * +------+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
88 * ^ | |-->| |-->| |
89 * | +---+ +---+ +---+
90 * | |
91 * | |
92 * +------------------------------+
93 *
94 *
95 * +------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
99 * ^ | | | |-->| |
100 * | New +---+ +---+ +---+
101 * | Reader------^ |
102 * | page |
103 * +------------------------------+
104 *
105 *
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
109 *
110 * We will be using cmpxchg soon to make all this lockless.
111 *
112 */
113
114 /*
115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
120 *
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
123 *
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
127 *
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
130 */
131
132 /*
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
135 *
136 * ON DISABLED
137 * ---- ----------
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
141 */
142
143 enum {
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
146 };
147
148 enum {
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
151 };
152
153 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
154
155 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
156
157 /**
158 * tracing_on - enable all tracing buffers
159 *
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
162 */
163 void tracing_on(void)
164 {
165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
166 }
167 EXPORT_SYMBOL_GPL(tracing_on);
168
169 /**
170 * tracing_off - turn off all tracing buffers
171 *
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
176 */
177 void tracing_off(void)
178 {
179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
180 }
181 EXPORT_SYMBOL_GPL(tracing_off);
182
183 /**
184 * tracing_off_permanent - permanently disable ring buffers
185 *
186 * This function, once called, will disable all ring buffers
187 * permanently.
188 */
189 void tracing_off_permanent(void)
190 {
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
192 }
193
194 /**
195 * tracing_is_on - show state of ring buffers enabled
196 */
197 int tracing_is_on(void)
198 {
199 return ring_buffer_flags == RB_BUFFERS_ON;
200 }
201 EXPORT_SYMBOL_GPL(tracing_is_on);
202
203 #include "trace.h"
204
205 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
206 #define RB_ALIGNMENT 4U
207 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
208 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
209
210 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
211 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
212
213 enum {
214 RB_LEN_TIME_EXTEND = 8,
215 RB_LEN_TIME_STAMP = 16,
216 };
217
218 static inline int rb_null_event(struct ring_buffer_event *event)
219 {
220 return event->type_len == RINGBUF_TYPE_PADDING
221 && event->time_delta == 0;
222 }
223
224 static inline int rb_discarded_event(struct ring_buffer_event *event)
225 {
226 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
227 }
228
229 static void rb_event_set_padding(struct ring_buffer_event *event)
230 {
231 event->type_len = RINGBUF_TYPE_PADDING;
232 event->time_delta = 0;
233 }
234
235 static unsigned
236 rb_event_data_length(struct ring_buffer_event *event)
237 {
238 unsigned length;
239
240 if (event->type_len)
241 length = event->type_len * RB_ALIGNMENT;
242 else
243 length = event->array[0];
244 return length + RB_EVNT_HDR_SIZE;
245 }
246
247 /* inline for ring buffer fast paths */
248 static unsigned
249 rb_event_length(struct ring_buffer_event *event)
250 {
251 switch (event->type_len) {
252 case RINGBUF_TYPE_PADDING:
253 if (rb_null_event(event))
254 /* undefined */
255 return -1;
256 return event->array[0] + RB_EVNT_HDR_SIZE;
257
258 case RINGBUF_TYPE_TIME_EXTEND:
259 return RB_LEN_TIME_EXTEND;
260
261 case RINGBUF_TYPE_TIME_STAMP:
262 return RB_LEN_TIME_STAMP;
263
264 case RINGBUF_TYPE_DATA:
265 return rb_event_data_length(event);
266 default:
267 BUG();
268 }
269 /* not hit */
270 return 0;
271 }
272
273 /**
274 * ring_buffer_event_length - return the length of the event
275 * @event: the event to get the length of
276 */
277 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
278 {
279 unsigned length = rb_event_length(event);
280 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
281 return length;
282 length -= RB_EVNT_HDR_SIZE;
283 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
284 length -= sizeof(event->array[0]);
285 return length;
286 }
287 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
288
289 /* inline for ring buffer fast paths */
290 static void *
291 rb_event_data(struct ring_buffer_event *event)
292 {
293 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
294 /* If length is in len field, then array[0] has the data */
295 if (event->type_len)
296 return (void *)&event->array[0];
297 /* Otherwise length is in array[0] and array[1] has the data */
298 return (void *)&event->array[1];
299 }
300
301 /**
302 * ring_buffer_event_data - return the data of the event
303 * @event: the event to get the data from
304 */
305 void *ring_buffer_event_data(struct ring_buffer_event *event)
306 {
307 return rb_event_data(event);
308 }
309 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
310
311 #define for_each_buffer_cpu(buffer, cpu) \
312 for_each_cpu(cpu, buffer->cpumask)
313
314 #define TS_SHIFT 27
315 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
316 #define TS_DELTA_TEST (~TS_MASK)
317
318 struct buffer_data_page {
319 u64 time_stamp; /* page time stamp */
320 local_t commit; /* write committed index */
321 unsigned char data[]; /* data of buffer page */
322 };
323
324 struct buffer_page {
325 struct list_head list; /* list of buffer pages */
326 local_t write; /* index for next write */
327 unsigned read; /* index for next read */
328 local_t entries; /* entries on this page */
329 struct buffer_data_page *page; /* Actual data page */
330 };
331
332 static void rb_init_page(struct buffer_data_page *bpage)
333 {
334 local_set(&bpage->commit, 0);
335 }
336
337 /**
338 * ring_buffer_page_len - the size of data on the page.
339 * @page: The page to read
340 *
341 * Returns the amount of data on the page, including buffer page header.
342 */
343 size_t ring_buffer_page_len(void *page)
344 {
345 return local_read(&((struct buffer_data_page *)page)->commit)
346 + BUF_PAGE_HDR_SIZE;
347 }
348
349 /*
350 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
351 * this issue out.
352 */
353 static void free_buffer_page(struct buffer_page *bpage)
354 {
355 free_page((unsigned long)bpage->page);
356 kfree(bpage);
357 }
358
359 /*
360 * We need to fit the time_stamp delta into 27 bits.
361 */
362 static inline int test_time_stamp(u64 delta)
363 {
364 if (delta & TS_DELTA_TEST)
365 return 1;
366 return 0;
367 }
368
369 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
370
371 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
372 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
373
374 /* Max number of timestamps that can fit on a page */
375 #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
376
377 int ring_buffer_print_page_header(struct trace_seq *s)
378 {
379 struct buffer_data_page field;
380 int ret;
381
382 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
383 "offset:0;\tsize:%u;\n",
384 (unsigned int)sizeof(field.time_stamp));
385
386 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
387 "offset:%u;\tsize:%u;\n",
388 (unsigned int)offsetof(typeof(field), commit),
389 (unsigned int)sizeof(field.commit));
390
391 ret = trace_seq_printf(s, "\tfield: char data;\t"
392 "offset:%u;\tsize:%u;\n",
393 (unsigned int)offsetof(typeof(field), data),
394 (unsigned int)BUF_PAGE_SIZE);
395
396 return ret;
397 }
398
399 /*
400 * head_page == tail_page && head == tail then buffer is empty.
401 */
402 struct ring_buffer_per_cpu {
403 int cpu;
404 struct ring_buffer *buffer;
405 spinlock_t reader_lock; /* serialize readers */
406 raw_spinlock_t lock;
407 struct lock_class_key lock_key;
408 struct list_head pages;
409 struct buffer_page *head_page; /* read from head */
410 struct buffer_page *tail_page; /* write to tail */
411 struct buffer_page *commit_page; /* committed pages */
412 struct buffer_page *reader_page;
413 unsigned long nmi_dropped;
414 unsigned long commit_overrun;
415 unsigned long overrun;
416 unsigned long read;
417 local_t entries;
418 local_t committing;
419 local_t commits;
420 u64 write_stamp;
421 u64 read_stamp;
422 atomic_t record_disabled;
423 };
424
425 struct ring_buffer {
426 unsigned pages;
427 unsigned flags;
428 int cpus;
429 atomic_t record_disabled;
430 cpumask_var_t cpumask;
431
432 struct lock_class_key *reader_lock_key;
433
434 struct mutex mutex;
435
436 struct ring_buffer_per_cpu **buffers;
437
438 #ifdef CONFIG_HOTPLUG_CPU
439 struct notifier_block cpu_notify;
440 #endif
441 u64 (*clock)(void);
442 };
443
444 struct ring_buffer_iter {
445 struct ring_buffer_per_cpu *cpu_buffer;
446 unsigned long head;
447 struct buffer_page *head_page;
448 u64 read_stamp;
449 };
450
451 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
452 #define RB_WARN_ON(buffer, cond) \
453 ({ \
454 int _____ret = unlikely(cond); \
455 if (_____ret) { \
456 atomic_inc(&buffer->record_disabled); \
457 WARN_ON(1); \
458 } \
459 _____ret; \
460 })
461
462 /* Up this if you want to test the TIME_EXTENTS and normalization */
463 #define DEBUG_SHIFT 0
464
465 static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
466 {
467 /* shift to debug/test normalization and TIME_EXTENTS */
468 return buffer->clock() << DEBUG_SHIFT;
469 }
470
471 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
472 {
473 u64 time;
474
475 preempt_disable_notrace();
476 time = rb_time_stamp(buffer, cpu);
477 preempt_enable_no_resched_notrace();
478
479 return time;
480 }
481 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
482
483 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
484 int cpu, u64 *ts)
485 {
486 /* Just stupid testing the normalize function and deltas */
487 *ts >>= DEBUG_SHIFT;
488 }
489 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
490
491 /**
492 * check_pages - integrity check of buffer pages
493 * @cpu_buffer: CPU buffer with pages to test
494 *
495 * As a safety measure we check to make sure the data pages have not
496 * been corrupted.
497 */
498 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
499 {
500 struct list_head *head = &cpu_buffer->pages;
501 struct buffer_page *bpage, *tmp;
502
503 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
504 return -1;
505 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
506 return -1;
507
508 list_for_each_entry_safe(bpage, tmp, head, list) {
509 if (RB_WARN_ON(cpu_buffer,
510 bpage->list.next->prev != &bpage->list))
511 return -1;
512 if (RB_WARN_ON(cpu_buffer,
513 bpage->list.prev->next != &bpage->list))
514 return -1;
515 }
516
517 return 0;
518 }
519
520 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
521 unsigned nr_pages)
522 {
523 struct list_head *head = &cpu_buffer->pages;
524 struct buffer_page *bpage, *tmp;
525 unsigned long addr;
526 LIST_HEAD(pages);
527 unsigned i;
528
529 for (i = 0; i < nr_pages; i++) {
530 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
531 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
532 if (!bpage)
533 goto free_pages;
534 list_add(&bpage->list, &pages);
535
536 addr = __get_free_page(GFP_KERNEL);
537 if (!addr)
538 goto free_pages;
539 bpage->page = (void *)addr;
540 rb_init_page(bpage->page);
541 }
542
543 list_splice(&pages, head);
544
545 rb_check_pages(cpu_buffer);
546
547 return 0;
548
549 free_pages:
550 list_for_each_entry_safe(bpage, tmp, &pages, list) {
551 list_del_init(&bpage->list);
552 free_buffer_page(bpage);
553 }
554 return -ENOMEM;
555 }
556
557 static struct ring_buffer_per_cpu *
558 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
559 {
560 struct ring_buffer_per_cpu *cpu_buffer;
561 struct buffer_page *bpage;
562 unsigned long addr;
563 int ret;
564
565 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
566 GFP_KERNEL, cpu_to_node(cpu));
567 if (!cpu_buffer)
568 return NULL;
569
570 cpu_buffer->cpu = cpu;
571 cpu_buffer->buffer = buffer;
572 spin_lock_init(&cpu_buffer->reader_lock);
573 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
574 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
575 INIT_LIST_HEAD(&cpu_buffer->pages);
576
577 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
578 GFP_KERNEL, cpu_to_node(cpu));
579 if (!bpage)
580 goto fail_free_buffer;
581
582 cpu_buffer->reader_page = bpage;
583 addr = __get_free_page(GFP_KERNEL);
584 if (!addr)
585 goto fail_free_reader;
586 bpage->page = (void *)addr;
587 rb_init_page(bpage->page);
588
589 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
590
591 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
592 if (ret < 0)
593 goto fail_free_reader;
594
595 cpu_buffer->head_page
596 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
597 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
598
599 return cpu_buffer;
600
601 fail_free_reader:
602 free_buffer_page(cpu_buffer->reader_page);
603
604 fail_free_buffer:
605 kfree(cpu_buffer);
606 return NULL;
607 }
608
609 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
610 {
611 struct list_head *head = &cpu_buffer->pages;
612 struct buffer_page *bpage, *tmp;
613
614 free_buffer_page(cpu_buffer->reader_page);
615
616 list_for_each_entry_safe(bpage, tmp, head, list) {
617 list_del_init(&bpage->list);
618 free_buffer_page(bpage);
619 }
620 kfree(cpu_buffer);
621 }
622
623 #ifdef CONFIG_HOTPLUG_CPU
624 static int rb_cpu_notify(struct notifier_block *self,
625 unsigned long action, void *hcpu);
626 #endif
627
628 /**
629 * ring_buffer_alloc - allocate a new ring_buffer
630 * @size: the size in bytes per cpu that is needed.
631 * @flags: attributes to set for the ring buffer.
632 *
633 * Currently the only flag that is available is the RB_FL_OVERWRITE
634 * flag. This flag means that the buffer will overwrite old data
635 * when the buffer wraps. If this flag is not set, the buffer will
636 * drop data when the tail hits the head.
637 */
638 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
639 struct lock_class_key *key)
640 {
641 struct ring_buffer *buffer;
642 int bsize;
643 int cpu;
644
645 /* keep it in its own cache line */
646 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
647 GFP_KERNEL);
648 if (!buffer)
649 return NULL;
650
651 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
652 goto fail_free_buffer;
653
654 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
655 buffer->flags = flags;
656 buffer->clock = trace_clock_local;
657 buffer->reader_lock_key = key;
658
659 /* need at least two pages */
660 if (buffer->pages < 2)
661 buffer->pages = 2;
662
663 /*
664 * In case of non-hotplug cpu, if the ring-buffer is allocated
665 * in early initcall, it will not be notified of secondary cpus.
666 * In that off case, we need to allocate for all possible cpus.
667 */
668 #ifdef CONFIG_HOTPLUG_CPU
669 get_online_cpus();
670 cpumask_copy(buffer->cpumask, cpu_online_mask);
671 #else
672 cpumask_copy(buffer->cpumask, cpu_possible_mask);
673 #endif
674 buffer->cpus = nr_cpu_ids;
675
676 bsize = sizeof(void *) * nr_cpu_ids;
677 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
678 GFP_KERNEL);
679 if (!buffer->buffers)
680 goto fail_free_cpumask;
681
682 for_each_buffer_cpu(buffer, cpu) {
683 buffer->buffers[cpu] =
684 rb_allocate_cpu_buffer(buffer, cpu);
685 if (!buffer->buffers[cpu])
686 goto fail_free_buffers;
687 }
688
689 #ifdef CONFIG_HOTPLUG_CPU
690 buffer->cpu_notify.notifier_call = rb_cpu_notify;
691 buffer->cpu_notify.priority = 0;
692 register_cpu_notifier(&buffer->cpu_notify);
693 #endif
694
695 put_online_cpus();
696 mutex_init(&buffer->mutex);
697
698 return buffer;
699
700 fail_free_buffers:
701 for_each_buffer_cpu(buffer, cpu) {
702 if (buffer->buffers[cpu])
703 rb_free_cpu_buffer(buffer->buffers[cpu]);
704 }
705 kfree(buffer->buffers);
706
707 fail_free_cpumask:
708 free_cpumask_var(buffer->cpumask);
709 put_online_cpus();
710
711 fail_free_buffer:
712 kfree(buffer);
713 return NULL;
714 }
715 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
716
717 /**
718 * ring_buffer_free - free a ring buffer.
719 * @buffer: the buffer to free.
720 */
721 void
722 ring_buffer_free(struct ring_buffer *buffer)
723 {
724 int cpu;
725
726 get_online_cpus();
727
728 #ifdef CONFIG_HOTPLUG_CPU
729 unregister_cpu_notifier(&buffer->cpu_notify);
730 #endif
731
732 for_each_buffer_cpu(buffer, cpu)
733 rb_free_cpu_buffer(buffer->buffers[cpu]);
734
735 put_online_cpus();
736
737 free_cpumask_var(buffer->cpumask);
738
739 kfree(buffer);
740 }
741 EXPORT_SYMBOL_GPL(ring_buffer_free);
742
743 void ring_buffer_set_clock(struct ring_buffer *buffer,
744 u64 (*clock)(void))
745 {
746 buffer->clock = clock;
747 }
748
749 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
750
751 static void
752 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
753 {
754 struct buffer_page *bpage;
755 struct list_head *p;
756 unsigned i;
757
758 atomic_inc(&cpu_buffer->record_disabled);
759 synchronize_sched();
760
761 for (i = 0; i < nr_pages; i++) {
762 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
763 return;
764 p = cpu_buffer->pages.next;
765 bpage = list_entry(p, struct buffer_page, list);
766 list_del_init(&bpage->list);
767 free_buffer_page(bpage);
768 }
769 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
770 return;
771
772 rb_reset_cpu(cpu_buffer);
773
774 rb_check_pages(cpu_buffer);
775
776 atomic_dec(&cpu_buffer->record_disabled);
777
778 }
779
780 static void
781 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
782 struct list_head *pages, unsigned nr_pages)
783 {
784 struct buffer_page *bpage;
785 struct list_head *p;
786 unsigned i;
787
788 atomic_inc(&cpu_buffer->record_disabled);
789 synchronize_sched();
790
791 for (i = 0; i < nr_pages; i++) {
792 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
793 return;
794 p = pages->next;
795 bpage = list_entry(p, struct buffer_page, list);
796 list_del_init(&bpage->list);
797 list_add_tail(&bpage->list, &cpu_buffer->pages);
798 }
799 rb_reset_cpu(cpu_buffer);
800
801 rb_check_pages(cpu_buffer);
802
803 atomic_dec(&cpu_buffer->record_disabled);
804 }
805
806 /**
807 * ring_buffer_resize - resize the ring buffer
808 * @buffer: the buffer to resize.
809 * @size: the new size.
810 *
811 * The tracer is responsible for making sure that the buffer is
812 * not being used while changing the size.
813 * Note: We may be able to change the above requirement by using
814 * RCU synchronizations.
815 *
816 * Minimum size is 2 * BUF_PAGE_SIZE.
817 *
818 * Returns -1 on failure.
819 */
820 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
821 {
822 struct ring_buffer_per_cpu *cpu_buffer;
823 unsigned nr_pages, rm_pages, new_pages;
824 struct buffer_page *bpage, *tmp;
825 unsigned long buffer_size;
826 unsigned long addr;
827 LIST_HEAD(pages);
828 int i, cpu;
829
830 /*
831 * Always succeed at resizing a non-existent buffer:
832 */
833 if (!buffer)
834 return size;
835
836 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
837 size *= BUF_PAGE_SIZE;
838 buffer_size = buffer->pages * BUF_PAGE_SIZE;
839
840 /* we need a minimum of two pages */
841 if (size < BUF_PAGE_SIZE * 2)
842 size = BUF_PAGE_SIZE * 2;
843
844 if (size == buffer_size)
845 return size;
846
847 mutex_lock(&buffer->mutex);
848 get_online_cpus();
849
850 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
851
852 if (size < buffer_size) {
853
854 /* easy case, just free pages */
855 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
856 goto out_fail;
857
858 rm_pages = buffer->pages - nr_pages;
859
860 for_each_buffer_cpu(buffer, cpu) {
861 cpu_buffer = buffer->buffers[cpu];
862 rb_remove_pages(cpu_buffer, rm_pages);
863 }
864 goto out;
865 }
866
867 /*
868 * This is a bit more difficult. We only want to add pages
869 * when we can allocate enough for all CPUs. We do this
870 * by allocating all the pages and storing them on a local
871 * link list. If we succeed in our allocation, then we
872 * add these pages to the cpu_buffers. Otherwise we just free
873 * them all and return -ENOMEM;
874 */
875 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
876 goto out_fail;
877
878 new_pages = nr_pages - buffer->pages;
879
880 for_each_buffer_cpu(buffer, cpu) {
881 for (i = 0; i < new_pages; i++) {
882 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
883 cache_line_size()),
884 GFP_KERNEL, cpu_to_node(cpu));
885 if (!bpage)
886 goto free_pages;
887 list_add(&bpage->list, &pages);
888 addr = __get_free_page(GFP_KERNEL);
889 if (!addr)
890 goto free_pages;
891 bpage->page = (void *)addr;
892 rb_init_page(bpage->page);
893 }
894 }
895
896 for_each_buffer_cpu(buffer, cpu) {
897 cpu_buffer = buffer->buffers[cpu];
898 rb_insert_pages(cpu_buffer, &pages, new_pages);
899 }
900
901 if (RB_WARN_ON(buffer, !list_empty(&pages)))
902 goto out_fail;
903
904 out:
905 buffer->pages = nr_pages;
906 put_online_cpus();
907 mutex_unlock(&buffer->mutex);
908
909 return size;
910
911 free_pages:
912 list_for_each_entry_safe(bpage, tmp, &pages, list) {
913 list_del_init(&bpage->list);
914 free_buffer_page(bpage);
915 }
916 put_online_cpus();
917 mutex_unlock(&buffer->mutex);
918 return -ENOMEM;
919
920 /*
921 * Something went totally wrong, and we are too paranoid
922 * to even clean up the mess.
923 */
924 out_fail:
925 put_online_cpus();
926 mutex_unlock(&buffer->mutex);
927 return -1;
928 }
929 EXPORT_SYMBOL_GPL(ring_buffer_resize);
930
931 static inline void *
932 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
933 {
934 return bpage->data + index;
935 }
936
937 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
938 {
939 return bpage->page->data + index;
940 }
941
942 static inline struct ring_buffer_event *
943 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
944 {
945 return __rb_page_index(cpu_buffer->reader_page,
946 cpu_buffer->reader_page->read);
947 }
948
949 static inline struct ring_buffer_event *
950 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
951 {
952 return __rb_page_index(cpu_buffer->head_page,
953 cpu_buffer->head_page->read);
954 }
955
956 static inline struct ring_buffer_event *
957 rb_iter_head_event(struct ring_buffer_iter *iter)
958 {
959 return __rb_page_index(iter->head_page, iter->head);
960 }
961
962 static inline unsigned rb_page_write(struct buffer_page *bpage)
963 {
964 return local_read(&bpage->write);
965 }
966
967 static inline unsigned rb_page_commit(struct buffer_page *bpage)
968 {
969 return local_read(&bpage->page->commit);
970 }
971
972 /* Size is determined by what has been commited */
973 static inline unsigned rb_page_size(struct buffer_page *bpage)
974 {
975 return rb_page_commit(bpage);
976 }
977
978 static inline unsigned
979 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
980 {
981 return rb_page_commit(cpu_buffer->commit_page);
982 }
983
984 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
985 {
986 return rb_page_commit(cpu_buffer->head_page);
987 }
988
989 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
990 struct buffer_page **bpage)
991 {
992 struct list_head *p = (*bpage)->list.next;
993
994 if (p == &cpu_buffer->pages)
995 p = p->next;
996
997 *bpage = list_entry(p, struct buffer_page, list);
998 }
999
1000 static inline unsigned
1001 rb_event_index(struct ring_buffer_event *event)
1002 {
1003 unsigned long addr = (unsigned long)event;
1004
1005 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1006 }
1007
1008 static inline int
1009 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1010 struct ring_buffer_event *event)
1011 {
1012 unsigned long addr = (unsigned long)event;
1013 unsigned long index;
1014
1015 index = rb_event_index(event);
1016 addr &= PAGE_MASK;
1017
1018 return cpu_buffer->commit_page->page == (void *)addr &&
1019 rb_commit_index(cpu_buffer) == index;
1020 }
1021
1022 static void
1023 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1024 {
1025 /*
1026 * We only race with interrupts and NMIs on this CPU.
1027 * If we own the commit event, then we can commit
1028 * all others that interrupted us, since the interruptions
1029 * are in stack format (they finish before they come
1030 * back to us). This allows us to do a simple loop to
1031 * assign the commit to the tail.
1032 */
1033 again:
1034 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1035 cpu_buffer->commit_page->page->commit =
1036 cpu_buffer->commit_page->write;
1037 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1038 cpu_buffer->write_stamp =
1039 cpu_buffer->commit_page->page->time_stamp;
1040 /* add barrier to keep gcc from optimizing too much */
1041 barrier();
1042 }
1043 while (rb_commit_index(cpu_buffer) !=
1044 rb_page_write(cpu_buffer->commit_page)) {
1045 cpu_buffer->commit_page->page->commit =
1046 cpu_buffer->commit_page->write;
1047 barrier();
1048 }
1049
1050 /* again, keep gcc from optimizing */
1051 barrier();
1052
1053 /*
1054 * If an interrupt came in just after the first while loop
1055 * and pushed the tail page forward, we will be left with
1056 * a dangling commit that will never go forward.
1057 */
1058 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1059 goto again;
1060 }
1061
1062 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1063 {
1064 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1065 cpu_buffer->reader_page->read = 0;
1066 }
1067
1068 static void rb_inc_iter(struct ring_buffer_iter *iter)
1069 {
1070 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1071
1072 /*
1073 * The iterator could be on the reader page (it starts there).
1074 * But the head could have moved, since the reader was
1075 * found. Check for this case and assign the iterator
1076 * to the head page instead of next.
1077 */
1078 if (iter->head_page == cpu_buffer->reader_page)
1079 iter->head_page = cpu_buffer->head_page;
1080 else
1081 rb_inc_page(cpu_buffer, &iter->head_page);
1082
1083 iter->read_stamp = iter->head_page->page->time_stamp;
1084 iter->head = 0;
1085 }
1086
1087 /**
1088 * ring_buffer_update_event - update event type and data
1089 * @event: the even to update
1090 * @type: the type of event
1091 * @length: the size of the event field in the ring buffer
1092 *
1093 * Update the type and data fields of the event. The length
1094 * is the actual size that is written to the ring buffer,
1095 * and with this, we can determine what to place into the
1096 * data field.
1097 */
1098 static void
1099 rb_update_event(struct ring_buffer_event *event,
1100 unsigned type, unsigned length)
1101 {
1102 event->type_len = type;
1103
1104 switch (type) {
1105
1106 case RINGBUF_TYPE_PADDING:
1107 case RINGBUF_TYPE_TIME_EXTEND:
1108 case RINGBUF_TYPE_TIME_STAMP:
1109 break;
1110
1111 case 0:
1112 length -= RB_EVNT_HDR_SIZE;
1113 if (length > RB_MAX_SMALL_DATA)
1114 event->array[0] = length;
1115 else
1116 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1117 break;
1118 default:
1119 BUG();
1120 }
1121 }
1122
1123 static unsigned rb_calculate_event_length(unsigned length)
1124 {
1125 struct ring_buffer_event event; /* Used only for sizeof array */
1126
1127 /* zero length can cause confusions */
1128 if (!length)
1129 length = 1;
1130
1131 if (length > RB_MAX_SMALL_DATA)
1132 length += sizeof(event.array[0]);
1133
1134 length += RB_EVNT_HDR_SIZE;
1135 length = ALIGN(length, RB_ALIGNMENT);
1136
1137 return length;
1138 }
1139
1140 static inline void
1141 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1142 struct buffer_page *tail_page,
1143 unsigned long tail, unsigned long length)
1144 {
1145 struct ring_buffer_event *event;
1146
1147 /*
1148 * Only the event that crossed the page boundary
1149 * must fill the old tail_page with padding.
1150 */
1151 if (tail >= BUF_PAGE_SIZE) {
1152 local_sub(length, &tail_page->write);
1153 return;
1154 }
1155
1156 event = __rb_page_index(tail_page, tail);
1157
1158 /*
1159 * If this event is bigger than the minimum size, then
1160 * we need to be careful that we don't subtract the
1161 * write counter enough to allow another writer to slip
1162 * in on this page.
1163 * We put in a discarded commit instead, to make sure
1164 * that this space is not used again.
1165 *
1166 * If we are less than the minimum size, we don't need to
1167 * worry about it.
1168 */
1169 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1170 /* No room for any events */
1171
1172 /* Mark the rest of the page with padding */
1173 rb_event_set_padding(event);
1174
1175 /* Set the write back to the previous setting */
1176 local_sub(length, &tail_page->write);
1177 return;
1178 }
1179
1180 /* Put in a discarded event */
1181 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1182 event->type_len = RINGBUF_TYPE_PADDING;
1183 /* time delta must be non zero */
1184 event->time_delta = 1;
1185 /* Account for this as an entry */
1186 local_inc(&tail_page->entries);
1187 local_inc(&cpu_buffer->entries);
1188
1189 /* Set write to end of buffer */
1190 length = (tail + length) - BUF_PAGE_SIZE;
1191 local_sub(length, &tail_page->write);
1192 }
1193
1194 static struct ring_buffer_event *
1195 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1196 unsigned long length, unsigned long tail,
1197 struct buffer_page *commit_page,
1198 struct buffer_page *tail_page, u64 *ts)
1199 {
1200 struct buffer_page *next_page, *head_page, *reader_page;
1201 struct ring_buffer *buffer = cpu_buffer->buffer;
1202 bool lock_taken = false;
1203 unsigned long flags;
1204
1205 next_page = tail_page;
1206
1207 local_irq_save(flags);
1208 /*
1209 * Since the write to the buffer is still not
1210 * fully lockless, we must be careful with NMIs.
1211 * The locks in the writers are taken when a write
1212 * crosses to a new page. The locks protect against
1213 * races with the readers (this will soon be fixed
1214 * with a lockless solution).
1215 *
1216 * Because we can not protect against NMIs, and we
1217 * want to keep traces reentrant, we need to manage
1218 * what happens when we are in an NMI.
1219 *
1220 * NMIs can happen after we take the lock.
1221 * If we are in an NMI, only take the lock
1222 * if it is not already taken. Otherwise
1223 * simply fail.
1224 */
1225 if (unlikely(in_nmi())) {
1226 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1227 cpu_buffer->nmi_dropped++;
1228 goto out_reset;
1229 }
1230 } else
1231 __raw_spin_lock(&cpu_buffer->lock);
1232
1233 lock_taken = true;
1234
1235 rb_inc_page(cpu_buffer, &next_page);
1236
1237 head_page = cpu_buffer->head_page;
1238 reader_page = cpu_buffer->reader_page;
1239
1240 /* we grabbed the lock before incrementing */
1241 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1242 goto out_reset;
1243
1244 /*
1245 * If for some reason, we had an interrupt storm that made
1246 * it all the way around the buffer, bail, and warn
1247 * about it.
1248 */
1249 if (unlikely(next_page == commit_page)) {
1250 cpu_buffer->commit_overrun++;
1251 goto out_reset;
1252 }
1253
1254 if (next_page == head_page) {
1255 if (!(buffer->flags & RB_FL_OVERWRITE))
1256 goto out_reset;
1257
1258 /* tail_page has not moved yet? */
1259 if (tail_page == cpu_buffer->tail_page) {
1260 /* count overflows */
1261 cpu_buffer->overrun +=
1262 local_read(&head_page->entries);
1263
1264 rb_inc_page(cpu_buffer, &head_page);
1265 cpu_buffer->head_page = head_page;
1266 cpu_buffer->head_page->read = 0;
1267 }
1268 }
1269
1270 /*
1271 * If the tail page is still the same as what we think
1272 * it is, then it is up to us to update the tail
1273 * pointer.
1274 */
1275 if (tail_page == cpu_buffer->tail_page) {
1276 local_set(&next_page->write, 0);
1277 local_set(&next_page->entries, 0);
1278 local_set(&next_page->page->commit, 0);
1279 cpu_buffer->tail_page = next_page;
1280
1281 /* reread the time stamp */
1282 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
1283 cpu_buffer->tail_page->page->time_stamp = *ts;
1284 }
1285
1286 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1287
1288 __raw_spin_unlock(&cpu_buffer->lock);
1289 local_irq_restore(flags);
1290
1291 /* fail and let the caller try again */
1292 return ERR_PTR(-EAGAIN);
1293
1294 out_reset:
1295 /* reset write */
1296 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1297
1298 if (likely(lock_taken))
1299 __raw_spin_unlock(&cpu_buffer->lock);
1300 local_irq_restore(flags);
1301 return NULL;
1302 }
1303
1304 static struct ring_buffer_event *
1305 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1306 unsigned type, unsigned long length, u64 *ts)
1307 {
1308 struct buffer_page *tail_page, *commit_page;
1309 struct ring_buffer_event *event;
1310 unsigned long tail, write;
1311
1312 commit_page = cpu_buffer->commit_page;
1313 /* we just need to protect against interrupts */
1314 barrier();
1315 tail_page = cpu_buffer->tail_page;
1316 write = local_add_return(length, &tail_page->write);
1317 tail = write - length;
1318
1319 /* See if we shot pass the end of this buffer page */
1320 if (write > BUF_PAGE_SIZE)
1321 return rb_move_tail(cpu_buffer, length, tail,
1322 commit_page, tail_page, ts);
1323
1324 /* We reserved something on the buffer */
1325
1326 event = __rb_page_index(tail_page, tail);
1327 rb_update_event(event, type, length);
1328
1329 /* The passed in type is zero for DATA */
1330 if (likely(!type))
1331 local_inc(&tail_page->entries);
1332
1333 /*
1334 * If this is the first commit on the page, then update
1335 * its timestamp.
1336 */
1337 if (!tail)
1338 tail_page->page->time_stamp = *ts;
1339
1340 return event;
1341 }
1342
1343 static inline int
1344 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1345 struct ring_buffer_event *event)
1346 {
1347 unsigned long new_index, old_index;
1348 struct buffer_page *bpage;
1349 unsigned long index;
1350 unsigned long addr;
1351
1352 new_index = rb_event_index(event);
1353 old_index = new_index + rb_event_length(event);
1354 addr = (unsigned long)event;
1355 addr &= PAGE_MASK;
1356
1357 bpage = cpu_buffer->tail_page;
1358
1359 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1360 /*
1361 * This is on the tail page. It is possible that
1362 * a write could come in and move the tail page
1363 * and write to the next page. That is fine
1364 * because we just shorten what is on this page.
1365 */
1366 index = local_cmpxchg(&bpage->write, old_index, new_index);
1367 if (index == old_index)
1368 return 1;
1369 }
1370
1371 /* could not discard */
1372 return 0;
1373 }
1374
1375 static int
1376 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1377 u64 *ts, u64 *delta)
1378 {
1379 struct ring_buffer_event *event;
1380 static int once;
1381 int ret;
1382
1383 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1384 printk(KERN_WARNING "Delta way too big! %llu"
1385 " ts=%llu write stamp = %llu\n",
1386 (unsigned long long)*delta,
1387 (unsigned long long)*ts,
1388 (unsigned long long)cpu_buffer->write_stamp);
1389 WARN_ON(1);
1390 }
1391
1392 /*
1393 * The delta is too big, we to add a
1394 * new timestamp.
1395 */
1396 event = __rb_reserve_next(cpu_buffer,
1397 RINGBUF_TYPE_TIME_EXTEND,
1398 RB_LEN_TIME_EXTEND,
1399 ts);
1400 if (!event)
1401 return -EBUSY;
1402
1403 if (PTR_ERR(event) == -EAGAIN)
1404 return -EAGAIN;
1405
1406 /* Only a commited time event can update the write stamp */
1407 if (rb_event_is_commit(cpu_buffer, event)) {
1408 /*
1409 * If this is the first on the page, then it was
1410 * updated with the page itself. Try to discard it
1411 * and if we can't just make it zero.
1412 */
1413 if (rb_event_index(event)) {
1414 event->time_delta = *delta & TS_MASK;
1415 event->array[0] = *delta >> TS_SHIFT;
1416 } else {
1417 /* try to discard, since we do not need this */
1418 if (!rb_try_to_discard(cpu_buffer, event)) {
1419 /* nope, just zero it */
1420 event->time_delta = 0;
1421 event->array[0] = 0;
1422 }
1423 }
1424 cpu_buffer->write_stamp = *ts;
1425 /* let the caller know this was the commit */
1426 ret = 1;
1427 } else {
1428 /* Try to discard the event */
1429 if (!rb_try_to_discard(cpu_buffer, event)) {
1430 /* Darn, this is just wasted space */
1431 event->time_delta = 0;
1432 event->array[0] = 0;
1433 }
1434 ret = 0;
1435 }
1436
1437 *delta = 0;
1438
1439 return ret;
1440 }
1441
1442 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
1443 {
1444 local_inc(&cpu_buffer->committing);
1445 local_inc(&cpu_buffer->commits);
1446 }
1447
1448 static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
1449 {
1450 unsigned long commits;
1451
1452 if (RB_WARN_ON(cpu_buffer,
1453 !local_read(&cpu_buffer->committing)))
1454 return;
1455
1456 again:
1457 commits = local_read(&cpu_buffer->commits);
1458 /* synchronize with interrupts */
1459 barrier();
1460 if (local_read(&cpu_buffer->committing) == 1)
1461 rb_set_commit_to_write(cpu_buffer);
1462
1463 local_dec(&cpu_buffer->committing);
1464
1465 /* synchronize with interrupts */
1466 barrier();
1467
1468 /*
1469 * Need to account for interrupts coming in between the
1470 * updating of the commit page and the clearing of the
1471 * committing counter.
1472 */
1473 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
1474 !local_read(&cpu_buffer->committing)) {
1475 local_inc(&cpu_buffer->committing);
1476 goto again;
1477 }
1478 }
1479
1480 static struct ring_buffer_event *
1481 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1482 unsigned long length)
1483 {
1484 struct ring_buffer_event *event;
1485 u64 ts, delta = 0;
1486 int commit = 0;
1487 int nr_loops = 0;
1488
1489 rb_start_commit(cpu_buffer);
1490
1491 length = rb_calculate_event_length(length);
1492 again:
1493 /*
1494 * We allow for interrupts to reenter here and do a trace.
1495 * If one does, it will cause this original code to loop
1496 * back here. Even with heavy interrupts happening, this
1497 * should only happen a few times in a row. If this happens
1498 * 1000 times in a row, there must be either an interrupt
1499 * storm or we have something buggy.
1500 * Bail!
1501 */
1502 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1503 goto out_fail;
1504
1505 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1506
1507 /*
1508 * Only the first commit can update the timestamp.
1509 * Yes there is a race here. If an interrupt comes in
1510 * just after the conditional and it traces too, then it
1511 * will also check the deltas. More than one timestamp may
1512 * also be made. But only the entry that did the actual
1513 * commit will be something other than zero.
1514 */
1515 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1516 rb_page_write(cpu_buffer->tail_page) ==
1517 rb_commit_index(cpu_buffer))) {
1518 u64 diff;
1519
1520 diff = ts - cpu_buffer->write_stamp;
1521
1522 /* make sure this diff is calculated here */
1523 barrier();
1524
1525 /* Did the write stamp get updated already? */
1526 if (unlikely(ts < cpu_buffer->write_stamp))
1527 goto get_event;
1528
1529 delta = diff;
1530 if (unlikely(test_time_stamp(delta))) {
1531
1532 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1533 if (commit == -EBUSY)
1534 goto out_fail;
1535
1536 if (commit == -EAGAIN)
1537 goto again;
1538
1539 RB_WARN_ON(cpu_buffer, commit < 0);
1540 }
1541 }
1542
1543 get_event:
1544 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
1545 if (unlikely(PTR_ERR(event) == -EAGAIN))
1546 goto again;
1547
1548 if (!event)
1549 goto out_fail;
1550
1551 if (!rb_event_is_commit(cpu_buffer, event))
1552 delta = 0;
1553
1554 event->time_delta = delta;
1555
1556 return event;
1557
1558 out_fail:
1559 rb_end_commit(cpu_buffer);
1560 return NULL;
1561 }
1562
1563 #define TRACE_RECURSIVE_DEPTH 16
1564
1565 static int trace_recursive_lock(void)
1566 {
1567 current->trace_recursion++;
1568
1569 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1570 return 0;
1571
1572 /* Disable all tracing before we do anything else */
1573 tracing_off_permanent();
1574
1575 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1576 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1577 current->trace_recursion,
1578 hardirq_count() >> HARDIRQ_SHIFT,
1579 softirq_count() >> SOFTIRQ_SHIFT,
1580 in_nmi());
1581
1582 WARN_ON_ONCE(1);
1583 return -1;
1584 }
1585
1586 static void trace_recursive_unlock(void)
1587 {
1588 WARN_ON_ONCE(!current->trace_recursion);
1589
1590 current->trace_recursion--;
1591 }
1592
1593 static DEFINE_PER_CPU(int, rb_need_resched);
1594
1595 /**
1596 * ring_buffer_lock_reserve - reserve a part of the buffer
1597 * @buffer: the ring buffer to reserve from
1598 * @length: the length of the data to reserve (excluding event header)
1599 *
1600 * Returns a reseverd event on the ring buffer to copy directly to.
1601 * The user of this interface will need to get the body to write into
1602 * and can use the ring_buffer_event_data() interface.
1603 *
1604 * The length is the length of the data needed, not the event length
1605 * which also includes the event header.
1606 *
1607 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1608 * If NULL is returned, then nothing has been allocated or locked.
1609 */
1610 struct ring_buffer_event *
1611 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1612 {
1613 struct ring_buffer_per_cpu *cpu_buffer;
1614 struct ring_buffer_event *event;
1615 int cpu, resched;
1616
1617 if (ring_buffer_flags != RB_BUFFERS_ON)
1618 return NULL;
1619
1620 if (atomic_read(&buffer->record_disabled))
1621 return NULL;
1622
1623 /* If we are tracing schedule, we don't want to recurse */
1624 resched = ftrace_preempt_disable();
1625
1626 if (trace_recursive_lock())
1627 goto out_nocheck;
1628
1629 cpu = raw_smp_processor_id();
1630
1631 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1632 goto out;
1633
1634 cpu_buffer = buffer->buffers[cpu];
1635
1636 if (atomic_read(&cpu_buffer->record_disabled))
1637 goto out;
1638
1639 if (length > BUF_MAX_DATA_SIZE)
1640 goto out;
1641
1642 event = rb_reserve_next_event(cpu_buffer, length);
1643 if (!event)
1644 goto out;
1645
1646 /*
1647 * Need to store resched state on this cpu.
1648 * Only the first needs to.
1649 */
1650
1651 if (preempt_count() == 1)
1652 per_cpu(rb_need_resched, cpu) = resched;
1653
1654 return event;
1655
1656 out:
1657 trace_recursive_unlock();
1658
1659 out_nocheck:
1660 ftrace_preempt_enable(resched);
1661 return NULL;
1662 }
1663 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1664
1665 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1666 struct ring_buffer_event *event)
1667 {
1668 local_inc(&cpu_buffer->entries);
1669
1670 /*
1671 * The event first in the commit queue updates the
1672 * time stamp.
1673 */
1674 if (rb_event_is_commit(cpu_buffer, event))
1675 cpu_buffer->write_stamp += event->time_delta;
1676
1677 rb_end_commit(cpu_buffer);
1678 }
1679
1680 /**
1681 * ring_buffer_unlock_commit - commit a reserved
1682 * @buffer: The buffer to commit to
1683 * @event: The event pointer to commit.
1684 *
1685 * This commits the data to the ring buffer, and releases any locks held.
1686 *
1687 * Must be paired with ring_buffer_lock_reserve.
1688 */
1689 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1690 struct ring_buffer_event *event)
1691 {
1692 struct ring_buffer_per_cpu *cpu_buffer;
1693 int cpu = raw_smp_processor_id();
1694
1695 cpu_buffer = buffer->buffers[cpu];
1696
1697 rb_commit(cpu_buffer, event);
1698
1699 trace_recursive_unlock();
1700
1701 /*
1702 * Only the last preempt count needs to restore preemption.
1703 */
1704 if (preempt_count() == 1)
1705 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1706 else
1707 preempt_enable_no_resched_notrace();
1708
1709 return 0;
1710 }
1711 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1712
1713 static inline void rb_event_discard(struct ring_buffer_event *event)
1714 {
1715 /* array[0] holds the actual length for the discarded event */
1716 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1717 event->type_len = RINGBUF_TYPE_PADDING;
1718 /* time delta must be non zero */
1719 if (!event->time_delta)
1720 event->time_delta = 1;
1721 }
1722
1723 /**
1724 * ring_buffer_event_discard - discard any event in the ring buffer
1725 * @event: the event to discard
1726 *
1727 * Sometimes a event that is in the ring buffer needs to be ignored.
1728 * This function lets the user discard an event in the ring buffer
1729 * and then that event will not be read later.
1730 *
1731 * Note, it is up to the user to be careful with this, and protect
1732 * against races. If the user discards an event that has been consumed
1733 * it is possible that it could corrupt the ring buffer.
1734 */
1735 void ring_buffer_event_discard(struct ring_buffer_event *event)
1736 {
1737 rb_event_discard(event);
1738 }
1739 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1740
1741 /**
1742 * ring_buffer_commit_discard - discard an event that has not been committed
1743 * @buffer: the ring buffer
1744 * @event: non committed event to discard
1745 *
1746 * This is similar to ring_buffer_event_discard but must only be
1747 * performed on an event that has not been committed yet. The difference
1748 * is that this will also try to free the event from the ring buffer
1749 * if another event has not been added behind it.
1750 *
1751 * If another event has been added behind it, it will set the event
1752 * up as discarded, and perform the commit.
1753 *
1754 * If this function is called, do not call ring_buffer_unlock_commit on
1755 * the event.
1756 */
1757 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1758 struct ring_buffer_event *event)
1759 {
1760 struct ring_buffer_per_cpu *cpu_buffer;
1761 int cpu;
1762
1763 /* The event is discarded regardless */
1764 rb_event_discard(event);
1765
1766 cpu = smp_processor_id();
1767 cpu_buffer = buffer->buffers[cpu];
1768
1769 /*
1770 * This must only be called if the event has not been
1771 * committed yet. Thus we can assume that preemption
1772 * is still disabled.
1773 */
1774 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
1775
1776 if (!rb_try_to_discard(cpu_buffer, event))
1777 goto out;
1778
1779 /*
1780 * The commit is still visible by the reader, so we
1781 * must increment entries.
1782 */
1783 local_inc(&cpu_buffer->entries);
1784 out:
1785 rb_end_commit(cpu_buffer);
1786
1787 trace_recursive_unlock();
1788
1789 /*
1790 * Only the last preempt count needs to restore preemption.
1791 */
1792 if (preempt_count() == 1)
1793 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1794 else
1795 preempt_enable_no_resched_notrace();
1796
1797 }
1798 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1799
1800 /**
1801 * ring_buffer_write - write data to the buffer without reserving
1802 * @buffer: The ring buffer to write to.
1803 * @length: The length of the data being written (excluding the event header)
1804 * @data: The data to write to the buffer.
1805 *
1806 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1807 * one function. If you already have the data to write to the buffer, it
1808 * may be easier to simply call this function.
1809 *
1810 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1811 * and not the length of the event which would hold the header.
1812 */
1813 int ring_buffer_write(struct ring_buffer *buffer,
1814 unsigned long length,
1815 void *data)
1816 {
1817 struct ring_buffer_per_cpu *cpu_buffer;
1818 struct ring_buffer_event *event;
1819 void *body;
1820 int ret = -EBUSY;
1821 int cpu, resched;
1822
1823 if (ring_buffer_flags != RB_BUFFERS_ON)
1824 return -EBUSY;
1825
1826 if (atomic_read(&buffer->record_disabled))
1827 return -EBUSY;
1828
1829 resched = ftrace_preempt_disable();
1830
1831 cpu = raw_smp_processor_id();
1832
1833 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1834 goto out;
1835
1836 cpu_buffer = buffer->buffers[cpu];
1837
1838 if (atomic_read(&cpu_buffer->record_disabled))
1839 goto out;
1840
1841 if (length > BUF_MAX_DATA_SIZE)
1842 goto out;
1843
1844 event = rb_reserve_next_event(cpu_buffer, length);
1845 if (!event)
1846 goto out;
1847
1848 body = rb_event_data(event);
1849
1850 memcpy(body, data, length);
1851
1852 rb_commit(cpu_buffer, event);
1853
1854 ret = 0;
1855 out:
1856 ftrace_preempt_enable(resched);
1857
1858 return ret;
1859 }
1860 EXPORT_SYMBOL_GPL(ring_buffer_write);
1861
1862 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1863 {
1864 struct buffer_page *reader = cpu_buffer->reader_page;
1865 struct buffer_page *head = cpu_buffer->head_page;
1866 struct buffer_page *commit = cpu_buffer->commit_page;
1867
1868 return reader->read == rb_page_commit(reader) &&
1869 (commit == reader ||
1870 (commit == head &&
1871 head->read == rb_page_commit(commit)));
1872 }
1873
1874 /**
1875 * ring_buffer_record_disable - stop all writes into the buffer
1876 * @buffer: The ring buffer to stop writes to.
1877 *
1878 * This prevents all writes to the buffer. Any attempt to write
1879 * to the buffer after this will fail and return NULL.
1880 *
1881 * The caller should call synchronize_sched() after this.
1882 */
1883 void ring_buffer_record_disable(struct ring_buffer *buffer)
1884 {
1885 atomic_inc(&buffer->record_disabled);
1886 }
1887 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1888
1889 /**
1890 * ring_buffer_record_enable - enable writes to the buffer
1891 * @buffer: The ring buffer to enable writes
1892 *
1893 * Note, multiple disables will need the same number of enables
1894 * to truely enable the writing (much like preempt_disable).
1895 */
1896 void ring_buffer_record_enable(struct ring_buffer *buffer)
1897 {
1898 atomic_dec(&buffer->record_disabled);
1899 }
1900 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1901
1902 /**
1903 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1904 * @buffer: The ring buffer to stop writes to.
1905 * @cpu: The CPU buffer to stop
1906 *
1907 * This prevents all writes to the buffer. Any attempt to write
1908 * to the buffer after this will fail and return NULL.
1909 *
1910 * The caller should call synchronize_sched() after this.
1911 */
1912 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1913 {
1914 struct ring_buffer_per_cpu *cpu_buffer;
1915
1916 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1917 return;
1918
1919 cpu_buffer = buffer->buffers[cpu];
1920 atomic_inc(&cpu_buffer->record_disabled);
1921 }
1922 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1923
1924 /**
1925 * ring_buffer_record_enable_cpu - enable writes to the buffer
1926 * @buffer: The ring buffer to enable writes
1927 * @cpu: The CPU to enable.
1928 *
1929 * Note, multiple disables will need the same number of enables
1930 * to truely enable the writing (much like preempt_disable).
1931 */
1932 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1933 {
1934 struct ring_buffer_per_cpu *cpu_buffer;
1935
1936 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1937 return;
1938
1939 cpu_buffer = buffer->buffers[cpu];
1940 atomic_dec(&cpu_buffer->record_disabled);
1941 }
1942 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1943
1944 /**
1945 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1946 * @buffer: The ring buffer
1947 * @cpu: The per CPU buffer to get the entries from.
1948 */
1949 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1950 {
1951 struct ring_buffer_per_cpu *cpu_buffer;
1952 unsigned long ret;
1953
1954 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1955 return 0;
1956
1957 cpu_buffer = buffer->buffers[cpu];
1958 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1959 - cpu_buffer->read;
1960
1961 return ret;
1962 }
1963 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1964
1965 /**
1966 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1967 * @buffer: The ring buffer
1968 * @cpu: The per CPU buffer to get the number of overruns from
1969 */
1970 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1971 {
1972 struct ring_buffer_per_cpu *cpu_buffer;
1973 unsigned long ret;
1974
1975 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1976 return 0;
1977
1978 cpu_buffer = buffer->buffers[cpu];
1979 ret = cpu_buffer->overrun;
1980
1981 return ret;
1982 }
1983 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1984
1985 /**
1986 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1987 * @buffer: The ring buffer
1988 * @cpu: The per CPU buffer to get the number of overruns from
1989 */
1990 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1991 {
1992 struct ring_buffer_per_cpu *cpu_buffer;
1993 unsigned long ret;
1994
1995 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1996 return 0;
1997
1998 cpu_buffer = buffer->buffers[cpu];
1999 ret = cpu_buffer->nmi_dropped;
2000
2001 return ret;
2002 }
2003 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
2004
2005 /**
2006 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2007 * @buffer: The ring buffer
2008 * @cpu: The per CPU buffer to get the number of overruns from
2009 */
2010 unsigned long
2011 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2012 {
2013 struct ring_buffer_per_cpu *cpu_buffer;
2014 unsigned long ret;
2015
2016 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2017 return 0;
2018
2019 cpu_buffer = buffer->buffers[cpu];
2020 ret = cpu_buffer->commit_overrun;
2021
2022 return ret;
2023 }
2024 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2025
2026 /**
2027 * ring_buffer_entries - get the number of entries in a buffer
2028 * @buffer: The ring buffer
2029 *
2030 * Returns the total number of entries in the ring buffer
2031 * (all CPU entries)
2032 */
2033 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2034 {
2035 struct ring_buffer_per_cpu *cpu_buffer;
2036 unsigned long entries = 0;
2037 int cpu;
2038
2039 /* if you care about this being correct, lock the buffer */
2040 for_each_buffer_cpu(buffer, cpu) {
2041 cpu_buffer = buffer->buffers[cpu];
2042 entries += (local_read(&cpu_buffer->entries) -
2043 cpu_buffer->overrun) - cpu_buffer->read;
2044 }
2045
2046 return entries;
2047 }
2048 EXPORT_SYMBOL_GPL(ring_buffer_entries);
2049
2050 /**
2051 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2052 * @buffer: The ring buffer
2053 *
2054 * Returns the total number of overruns in the ring buffer
2055 * (all CPU entries)
2056 */
2057 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2058 {
2059 struct ring_buffer_per_cpu *cpu_buffer;
2060 unsigned long overruns = 0;
2061 int cpu;
2062
2063 /* if you care about this being correct, lock the buffer */
2064 for_each_buffer_cpu(buffer, cpu) {
2065 cpu_buffer = buffer->buffers[cpu];
2066 overruns += cpu_buffer->overrun;
2067 }
2068
2069 return overruns;
2070 }
2071 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2072
2073 static void rb_iter_reset(struct ring_buffer_iter *iter)
2074 {
2075 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2076
2077 /* Iterator usage is expected to have record disabled */
2078 if (list_empty(&cpu_buffer->reader_page->list)) {
2079 iter->head_page = cpu_buffer->head_page;
2080 iter->head = cpu_buffer->head_page->read;
2081 } else {
2082 iter->head_page = cpu_buffer->reader_page;
2083 iter->head = cpu_buffer->reader_page->read;
2084 }
2085 if (iter->head)
2086 iter->read_stamp = cpu_buffer->read_stamp;
2087 else
2088 iter->read_stamp = iter->head_page->page->time_stamp;
2089 }
2090
2091 /**
2092 * ring_buffer_iter_reset - reset an iterator
2093 * @iter: The iterator to reset
2094 *
2095 * Resets the iterator, so that it will start from the beginning
2096 * again.
2097 */
2098 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2099 {
2100 struct ring_buffer_per_cpu *cpu_buffer;
2101 unsigned long flags;
2102
2103 if (!iter)
2104 return;
2105
2106 cpu_buffer = iter->cpu_buffer;
2107
2108 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2109 rb_iter_reset(iter);
2110 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2111 }
2112 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2113
2114 /**
2115 * ring_buffer_iter_empty - check if an iterator has no more to read
2116 * @iter: The iterator to check
2117 */
2118 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2119 {
2120 struct ring_buffer_per_cpu *cpu_buffer;
2121
2122 cpu_buffer = iter->cpu_buffer;
2123
2124 return iter->head_page == cpu_buffer->commit_page &&
2125 iter->head == rb_commit_index(cpu_buffer);
2126 }
2127 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2128
2129 static void
2130 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2131 struct ring_buffer_event *event)
2132 {
2133 u64 delta;
2134
2135 switch (event->type_len) {
2136 case RINGBUF_TYPE_PADDING:
2137 return;
2138
2139 case RINGBUF_TYPE_TIME_EXTEND:
2140 delta = event->array[0];
2141 delta <<= TS_SHIFT;
2142 delta += event->time_delta;
2143 cpu_buffer->read_stamp += delta;
2144 return;
2145
2146 case RINGBUF_TYPE_TIME_STAMP:
2147 /* FIXME: not implemented */
2148 return;
2149
2150 case RINGBUF_TYPE_DATA:
2151 cpu_buffer->read_stamp += event->time_delta;
2152 return;
2153
2154 default:
2155 BUG();
2156 }
2157 return;
2158 }
2159
2160 static void
2161 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2162 struct ring_buffer_event *event)
2163 {
2164 u64 delta;
2165
2166 switch (event->type_len) {
2167 case RINGBUF_TYPE_PADDING:
2168 return;
2169
2170 case RINGBUF_TYPE_TIME_EXTEND:
2171 delta = event->array[0];
2172 delta <<= TS_SHIFT;
2173 delta += event->time_delta;
2174 iter->read_stamp += delta;
2175 return;
2176
2177 case RINGBUF_TYPE_TIME_STAMP:
2178 /* FIXME: not implemented */
2179 return;
2180
2181 case RINGBUF_TYPE_DATA:
2182 iter->read_stamp += event->time_delta;
2183 return;
2184
2185 default:
2186 BUG();
2187 }
2188 return;
2189 }
2190
2191 static struct buffer_page *
2192 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2193 {
2194 struct buffer_page *reader = NULL;
2195 unsigned long flags;
2196 int nr_loops = 0;
2197
2198 local_irq_save(flags);
2199 __raw_spin_lock(&cpu_buffer->lock);
2200
2201 again:
2202 /*
2203 * This should normally only loop twice. But because the
2204 * start of the reader inserts an empty page, it causes
2205 * a case where we will loop three times. There should be no
2206 * reason to loop four times (that I know of).
2207 */
2208 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2209 reader = NULL;
2210 goto out;
2211 }
2212
2213 reader = cpu_buffer->reader_page;
2214
2215 /* If there's more to read, return this page */
2216 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2217 goto out;
2218
2219 /* Never should we have an index greater than the size */
2220 if (RB_WARN_ON(cpu_buffer,
2221 cpu_buffer->reader_page->read > rb_page_size(reader)))
2222 goto out;
2223
2224 /* check if we caught up to the tail */
2225 reader = NULL;
2226 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2227 goto out;
2228
2229 /*
2230 * Splice the empty reader page into the list around the head.
2231 * Reset the reader page to size zero.
2232 */
2233
2234 reader = cpu_buffer->head_page;
2235 cpu_buffer->reader_page->list.next = reader->list.next;
2236 cpu_buffer->reader_page->list.prev = reader->list.prev;
2237
2238 local_set(&cpu_buffer->reader_page->write, 0);
2239 local_set(&cpu_buffer->reader_page->entries, 0);
2240 local_set(&cpu_buffer->reader_page->page->commit, 0);
2241
2242 /* Make the reader page now replace the head */
2243 reader->list.prev->next = &cpu_buffer->reader_page->list;
2244 reader->list.next->prev = &cpu_buffer->reader_page->list;
2245
2246 /*
2247 * If the tail is on the reader, then we must set the head
2248 * to the inserted page, otherwise we set it one before.
2249 */
2250 cpu_buffer->head_page = cpu_buffer->reader_page;
2251
2252 if (cpu_buffer->commit_page != reader)
2253 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2254
2255 /* Finally update the reader page to the new head */
2256 cpu_buffer->reader_page = reader;
2257 rb_reset_reader_page(cpu_buffer);
2258
2259 goto again;
2260
2261 out:
2262 __raw_spin_unlock(&cpu_buffer->lock);
2263 local_irq_restore(flags);
2264
2265 return reader;
2266 }
2267
2268 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2269 {
2270 struct ring_buffer_event *event;
2271 struct buffer_page *reader;
2272 unsigned length;
2273
2274 reader = rb_get_reader_page(cpu_buffer);
2275
2276 /* This function should not be called when buffer is empty */
2277 if (RB_WARN_ON(cpu_buffer, !reader))
2278 return;
2279
2280 event = rb_reader_event(cpu_buffer);
2281
2282 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2283 || rb_discarded_event(event))
2284 cpu_buffer->read++;
2285
2286 rb_update_read_stamp(cpu_buffer, event);
2287
2288 length = rb_event_length(event);
2289 cpu_buffer->reader_page->read += length;
2290 }
2291
2292 static void rb_advance_iter(struct ring_buffer_iter *iter)
2293 {
2294 struct ring_buffer *buffer;
2295 struct ring_buffer_per_cpu *cpu_buffer;
2296 struct ring_buffer_event *event;
2297 unsigned length;
2298
2299 cpu_buffer = iter->cpu_buffer;
2300 buffer = cpu_buffer->buffer;
2301
2302 /*
2303 * Check if we are at the end of the buffer.
2304 */
2305 if (iter->head >= rb_page_size(iter->head_page)) {
2306 /* discarded commits can make the page empty */
2307 if (iter->head_page == cpu_buffer->commit_page)
2308 return;
2309 rb_inc_iter(iter);
2310 return;
2311 }
2312
2313 event = rb_iter_head_event(iter);
2314
2315 length = rb_event_length(event);
2316
2317 /*
2318 * This should not be called to advance the header if we are
2319 * at the tail of the buffer.
2320 */
2321 if (RB_WARN_ON(cpu_buffer,
2322 (iter->head_page == cpu_buffer->commit_page) &&
2323 (iter->head + length > rb_commit_index(cpu_buffer))))
2324 return;
2325
2326 rb_update_iter_read_stamp(iter, event);
2327
2328 iter->head += length;
2329
2330 /* check for end of page padding */
2331 if ((iter->head >= rb_page_size(iter->head_page)) &&
2332 (iter->head_page != cpu_buffer->commit_page))
2333 rb_advance_iter(iter);
2334 }
2335
2336 static struct ring_buffer_event *
2337 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2338 {
2339 struct ring_buffer_per_cpu *cpu_buffer;
2340 struct ring_buffer_event *event;
2341 struct buffer_page *reader;
2342 int nr_loops = 0;
2343
2344 cpu_buffer = buffer->buffers[cpu];
2345
2346 again:
2347 /*
2348 * We repeat when a timestamp is encountered. It is possible
2349 * to get multiple timestamps from an interrupt entering just
2350 * as one timestamp is about to be written, or from discarded
2351 * commits. The most that we can have is the number on a single page.
2352 */
2353 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
2354 return NULL;
2355
2356 reader = rb_get_reader_page(cpu_buffer);
2357 if (!reader)
2358 return NULL;
2359
2360 event = rb_reader_event(cpu_buffer);
2361
2362 switch (event->type_len) {
2363 case RINGBUF_TYPE_PADDING:
2364 if (rb_null_event(event))
2365 RB_WARN_ON(cpu_buffer, 1);
2366 /*
2367 * Because the writer could be discarding every
2368 * event it creates (which would probably be bad)
2369 * if we were to go back to "again" then we may never
2370 * catch up, and will trigger the warn on, or lock
2371 * the box. Return the padding, and we will release
2372 * the current locks, and try again.
2373 */
2374 rb_advance_reader(cpu_buffer);
2375 return event;
2376
2377 case RINGBUF_TYPE_TIME_EXTEND:
2378 /* Internal data, OK to advance */
2379 rb_advance_reader(cpu_buffer);
2380 goto again;
2381
2382 case RINGBUF_TYPE_TIME_STAMP:
2383 /* FIXME: not implemented */
2384 rb_advance_reader(cpu_buffer);
2385 goto again;
2386
2387 case RINGBUF_TYPE_DATA:
2388 if (ts) {
2389 *ts = cpu_buffer->read_stamp + event->time_delta;
2390 ring_buffer_normalize_time_stamp(buffer,
2391 cpu_buffer->cpu, ts);
2392 }
2393 return event;
2394
2395 default:
2396 BUG();
2397 }
2398
2399 return NULL;
2400 }
2401 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2402
2403 static struct ring_buffer_event *
2404 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2405 {
2406 struct ring_buffer *buffer;
2407 struct ring_buffer_per_cpu *cpu_buffer;
2408 struct ring_buffer_event *event;
2409 int nr_loops = 0;
2410
2411 if (ring_buffer_iter_empty(iter))
2412 return NULL;
2413
2414 cpu_buffer = iter->cpu_buffer;
2415 buffer = cpu_buffer->buffer;
2416
2417 again:
2418 /*
2419 * We repeat when a timestamp is encountered.
2420 * We can get multiple timestamps by nested interrupts or also
2421 * if filtering is on (discarding commits). Since discarding
2422 * commits can be frequent we can get a lot of timestamps.
2423 * But we limit them by not adding timestamps if they begin
2424 * at the start of a page.
2425 */
2426 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
2427 return NULL;
2428
2429 if (rb_per_cpu_empty(cpu_buffer))
2430 return NULL;
2431
2432 event = rb_iter_head_event(iter);
2433
2434 switch (event->type_len) {
2435 case RINGBUF_TYPE_PADDING:
2436 if (rb_null_event(event)) {
2437 rb_inc_iter(iter);
2438 goto again;
2439 }
2440 rb_advance_iter(iter);
2441 return event;
2442
2443 case RINGBUF_TYPE_TIME_EXTEND:
2444 /* Internal data, OK to advance */
2445 rb_advance_iter(iter);
2446 goto again;
2447
2448 case RINGBUF_TYPE_TIME_STAMP:
2449 /* FIXME: not implemented */
2450 rb_advance_iter(iter);
2451 goto again;
2452
2453 case RINGBUF_TYPE_DATA:
2454 if (ts) {
2455 *ts = iter->read_stamp + event->time_delta;
2456 ring_buffer_normalize_time_stamp(buffer,
2457 cpu_buffer->cpu, ts);
2458 }
2459 return event;
2460
2461 default:
2462 BUG();
2463 }
2464
2465 return NULL;
2466 }
2467 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2468
2469 /**
2470 * ring_buffer_peek - peek at the next event to be read
2471 * @buffer: The ring buffer to read
2472 * @cpu: The cpu to peak at
2473 * @ts: The timestamp counter of this event.
2474 *
2475 * This will return the event that will be read next, but does
2476 * not consume the data.
2477 */
2478 struct ring_buffer_event *
2479 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2480 {
2481 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2482 struct ring_buffer_event *event;
2483 unsigned long flags;
2484
2485 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2486 return NULL;
2487
2488 again:
2489 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2490 event = rb_buffer_peek(buffer, cpu, ts);
2491 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2492
2493 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2494 cpu_relax();
2495 goto again;
2496 }
2497
2498 return event;
2499 }
2500
2501 /**
2502 * ring_buffer_iter_peek - peek at the next event to be read
2503 * @iter: The ring buffer iterator
2504 * @ts: The timestamp counter of this event.
2505 *
2506 * This will return the event that will be read next, but does
2507 * not increment the iterator.
2508 */
2509 struct ring_buffer_event *
2510 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2511 {
2512 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2513 struct ring_buffer_event *event;
2514 unsigned long flags;
2515
2516 again:
2517 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2518 event = rb_iter_peek(iter, ts);
2519 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2520
2521 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2522 cpu_relax();
2523 goto again;
2524 }
2525
2526 return event;
2527 }
2528
2529 /**
2530 * ring_buffer_consume - return an event and consume it
2531 * @buffer: The ring buffer to get the next event from
2532 *
2533 * Returns the next event in the ring buffer, and that event is consumed.
2534 * Meaning, that sequential reads will keep returning a different event,
2535 * and eventually empty the ring buffer if the producer is slower.
2536 */
2537 struct ring_buffer_event *
2538 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2539 {
2540 struct ring_buffer_per_cpu *cpu_buffer;
2541 struct ring_buffer_event *event = NULL;
2542 unsigned long flags;
2543
2544 again:
2545 /* might be called in atomic */
2546 preempt_disable();
2547
2548 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2549 goto out;
2550
2551 cpu_buffer = buffer->buffers[cpu];
2552 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2553
2554 event = rb_buffer_peek(buffer, cpu, ts);
2555 if (!event)
2556 goto out_unlock;
2557
2558 rb_advance_reader(cpu_buffer);
2559
2560 out_unlock:
2561 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2562
2563 out:
2564 preempt_enable();
2565
2566 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2567 cpu_relax();
2568 goto again;
2569 }
2570
2571 return event;
2572 }
2573 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2574
2575 /**
2576 * ring_buffer_read_start - start a non consuming read of the buffer
2577 * @buffer: The ring buffer to read from
2578 * @cpu: The cpu buffer to iterate over
2579 *
2580 * This starts up an iteration through the buffer. It also disables
2581 * the recording to the buffer until the reading is finished.
2582 * This prevents the reading from being corrupted. This is not
2583 * a consuming read, so a producer is not expected.
2584 *
2585 * Must be paired with ring_buffer_finish.
2586 */
2587 struct ring_buffer_iter *
2588 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2589 {
2590 struct ring_buffer_per_cpu *cpu_buffer;
2591 struct ring_buffer_iter *iter;
2592 unsigned long flags;
2593
2594 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2595 return NULL;
2596
2597 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2598 if (!iter)
2599 return NULL;
2600
2601 cpu_buffer = buffer->buffers[cpu];
2602
2603 iter->cpu_buffer = cpu_buffer;
2604
2605 atomic_inc(&cpu_buffer->record_disabled);
2606 synchronize_sched();
2607
2608 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2609 __raw_spin_lock(&cpu_buffer->lock);
2610 rb_iter_reset(iter);
2611 __raw_spin_unlock(&cpu_buffer->lock);
2612 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2613
2614 return iter;
2615 }
2616 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2617
2618 /**
2619 * ring_buffer_finish - finish reading the iterator of the buffer
2620 * @iter: The iterator retrieved by ring_buffer_start
2621 *
2622 * This re-enables the recording to the buffer, and frees the
2623 * iterator.
2624 */
2625 void
2626 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2627 {
2628 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2629
2630 atomic_dec(&cpu_buffer->record_disabled);
2631 kfree(iter);
2632 }
2633 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2634
2635 /**
2636 * ring_buffer_read - read the next item in the ring buffer by the iterator
2637 * @iter: The ring buffer iterator
2638 * @ts: The time stamp of the event read.
2639 *
2640 * This reads the next event in the ring buffer and increments the iterator.
2641 */
2642 struct ring_buffer_event *
2643 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2644 {
2645 struct ring_buffer_event *event;
2646 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2647 unsigned long flags;
2648
2649 again:
2650 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2651 event = rb_iter_peek(iter, ts);
2652 if (!event)
2653 goto out;
2654
2655 rb_advance_iter(iter);
2656 out:
2657 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2658
2659 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2660 cpu_relax();
2661 goto again;
2662 }
2663
2664 return event;
2665 }
2666 EXPORT_SYMBOL_GPL(ring_buffer_read);
2667
2668 /**
2669 * ring_buffer_size - return the size of the ring buffer (in bytes)
2670 * @buffer: The ring buffer.
2671 */
2672 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2673 {
2674 return BUF_PAGE_SIZE * buffer->pages;
2675 }
2676 EXPORT_SYMBOL_GPL(ring_buffer_size);
2677
2678 static void
2679 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2680 {
2681 cpu_buffer->head_page
2682 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2683 local_set(&cpu_buffer->head_page->write, 0);
2684 local_set(&cpu_buffer->head_page->entries, 0);
2685 local_set(&cpu_buffer->head_page->page->commit, 0);
2686
2687 cpu_buffer->head_page->read = 0;
2688
2689 cpu_buffer->tail_page = cpu_buffer->head_page;
2690 cpu_buffer->commit_page = cpu_buffer->head_page;
2691
2692 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2693 local_set(&cpu_buffer->reader_page->write, 0);
2694 local_set(&cpu_buffer->reader_page->entries, 0);
2695 local_set(&cpu_buffer->reader_page->page->commit, 0);
2696 cpu_buffer->reader_page->read = 0;
2697
2698 cpu_buffer->nmi_dropped = 0;
2699 cpu_buffer->commit_overrun = 0;
2700 cpu_buffer->overrun = 0;
2701 cpu_buffer->read = 0;
2702 local_set(&cpu_buffer->entries, 0);
2703 local_set(&cpu_buffer->committing, 0);
2704 local_set(&cpu_buffer->commits, 0);
2705
2706 cpu_buffer->write_stamp = 0;
2707 cpu_buffer->read_stamp = 0;
2708 }
2709
2710 /**
2711 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2712 * @buffer: The ring buffer to reset a per cpu buffer of
2713 * @cpu: The CPU buffer to be reset
2714 */
2715 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2716 {
2717 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2718 unsigned long flags;
2719
2720 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2721 return;
2722
2723 atomic_inc(&cpu_buffer->record_disabled);
2724
2725 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2726
2727 __raw_spin_lock(&cpu_buffer->lock);
2728
2729 rb_reset_cpu(cpu_buffer);
2730
2731 __raw_spin_unlock(&cpu_buffer->lock);
2732
2733 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2734
2735 atomic_dec(&cpu_buffer->record_disabled);
2736 }
2737 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2738
2739 /**
2740 * ring_buffer_reset - reset a ring buffer
2741 * @buffer: The ring buffer to reset all cpu buffers
2742 */
2743 void ring_buffer_reset(struct ring_buffer *buffer)
2744 {
2745 int cpu;
2746
2747 for_each_buffer_cpu(buffer, cpu)
2748 ring_buffer_reset_cpu(buffer, cpu);
2749 }
2750 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2751
2752 /**
2753 * rind_buffer_empty - is the ring buffer empty?
2754 * @buffer: The ring buffer to test
2755 */
2756 int ring_buffer_empty(struct ring_buffer *buffer)
2757 {
2758 struct ring_buffer_per_cpu *cpu_buffer;
2759 unsigned long flags;
2760 int cpu;
2761 int ret;
2762
2763 /* yes this is racy, but if you don't like the race, lock the buffer */
2764 for_each_buffer_cpu(buffer, cpu) {
2765 cpu_buffer = buffer->buffers[cpu];
2766 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2767 ret = rb_per_cpu_empty(cpu_buffer);
2768 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2769 if (!ret)
2770 return 0;
2771 }
2772
2773 return 1;
2774 }
2775 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2776
2777 /**
2778 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2779 * @buffer: The ring buffer
2780 * @cpu: The CPU buffer to test
2781 */
2782 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2783 {
2784 struct ring_buffer_per_cpu *cpu_buffer;
2785 unsigned long flags;
2786 int ret;
2787
2788 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2789 return 1;
2790
2791 cpu_buffer = buffer->buffers[cpu];
2792 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2793 ret = rb_per_cpu_empty(cpu_buffer);
2794 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2795
2796 return ret;
2797 }
2798 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2799
2800 /**
2801 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2802 * @buffer_a: One buffer to swap with
2803 * @buffer_b: The other buffer to swap with
2804 *
2805 * This function is useful for tracers that want to take a "snapshot"
2806 * of a CPU buffer and has another back up buffer lying around.
2807 * it is expected that the tracer handles the cpu buffer not being
2808 * used at the moment.
2809 */
2810 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2811 struct ring_buffer *buffer_b, int cpu)
2812 {
2813 struct ring_buffer_per_cpu *cpu_buffer_a;
2814 struct ring_buffer_per_cpu *cpu_buffer_b;
2815 int ret = -EINVAL;
2816
2817 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2818 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2819 goto out;
2820
2821 /* At least make sure the two buffers are somewhat the same */
2822 if (buffer_a->pages != buffer_b->pages)
2823 goto out;
2824
2825 ret = -EAGAIN;
2826
2827 if (ring_buffer_flags != RB_BUFFERS_ON)
2828 goto out;
2829
2830 if (atomic_read(&buffer_a->record_disabled))
2831 goto out;
2832
2833 if (atomic_read(&buffer_b->record_disabled))
2834 goto out;
2835
2836 cpu_buffer_a = buffer_a->buffers[cpu];
2837 cpu_buffer_b = buffer_b->buffers[cpu];
2838
2839 if (atomic_read(&cpu_buffer_a->record_disabled))
2840 goto out;
2841
2842 if (atomic_read(&cpu_buffer_b->record_disabled))
2843 goto out;
2844
2845 /*
2846 * We can't do a synchronize_sched here because this
2847 * function can be called in atomic context.
2848 * Normally this will be called from the same CPU as cpu.
2849 * If not it's up to the caller to protect this.
2850 */
2851 atomic_inc(&cpu_buffer_a->record_disabled);
2852 atomic_inc(&cpu_buffer_b->record_disabled);
2853
2854 buffer_a->buffers[cpu] = cpu_buffer_b;
2855 buffer_b->buffers[cpu] = cpu_buffer_a;
2856
2857 cpu_buffer_b->buffer = buffer_a;
2858 cpu_buffer_a->buffer = buffer_b;
2859
2860 atomic_dec(&cpu_buffer_a->record_disabled);
2861 atomic_dec(&cpu_buffer_b->record_disabled);
2862
2863 ret = 0;
2864 out:
2865 return ret;
2866 }
2867 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2868
2869 /**
2870 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2871 * @buffer: the buffer to allocate for.
2872 *
2873 * This function is used in conjunction with ring_buffer_read_page.
2874 * When reading a full page from the ring buffer, these functions
2875 * can be used to speed up the process. The calling function should
2876 * allocate a few pages first with this function. Then when it
2877 * needs to get pages from the ring buffer, it passes the result
2878 * of this function into ring_buffer_read_page, which will swap
2879 * the page that was allocated, with the read page of the buffer.
2880 *
2881 * Returns:
2882 * The page allocated, or NULL on error.
2883 */
2884 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2885 {
2886 struct buffer_data_page *bpage;
2887 unsigned long addr;
2888
2889 addr = __get_free_page(GFP_KERNEL);
2890 if (!addr)
2891 return NULL;
2892
2893 bpage = (void *)addr;
2894
2895 rb_init_page(bpage);
2896
2897 return bpage;
2898 }
2899 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2900
2901 /**
2902 * ring_buffer_free_read_page - free an allocated read page
2903 * @buffer: the buffer the page was allocate for
2904 * @data: the page to free
2905 *
2906 * Free a page allocated from ring_buffer_alloc_read_page.
2907 */
2908 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2909 {
2910 free_page((unsigned long)data);
2911 }
2912 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2913
2914 /**
2915 * ring_buffer_read_page - extract a page from the ring buffer
2916 * @buffer: buffer to extract from
2917 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2918 * @len: amount to extract
2919 * @cpu: the cpu of the buffer to extract
2920 * @full: should the extraction only happen when the page is full.
2921 *
2922 * This function will pull out a page from the ring buffer and consume it.
2923 * @data_page must be the address of the variable that was returned
2924 * from ring_buffer_alloc_read_page. This is because the page might be used
2925 * to swap with a page in the ring buffer.
2926 *
2927 * for example:
2928 * rpage = ring_buffer_alloc_read_page(buffer);
2929 * if (!rpage)
2930 * return error;
2931 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2932 * if (ret >= 0)
2933 * process_page(rpage, ret);
2934 *
2935 * When @full is set, the function will not return true unless
2936 * the writer is off the reader page.
2937 *
2938 * Note: it is up to the calling functions to handle sleeps and wakeups.
2939 * The ring buffer can be used anywhere in the kernel and can not
2940 * blindly call wake_up. The layer that uses the ring buffer must be
2941 * responsible for that.
2942 *
2943 * Returns:
2944 * >=0 if data has been transferred, returns the offset of consumed data.
2945 * <0 if no data has been transferred.
2946 */
2947 int ring_buffer_read_page(struct ring_buffer *buffer,
2948 void **data_page, size_t len, int cpu, int full)
2949 {
2950 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2951 struct ring_buffer_event *event;
2952 struct buffer_data_page *bpage;
2953 struct buffer_page *reader;
2954 unsigned long flags;
2955 unsigned int commit;
2956 unsigned int read;
2957 u64 save_timestamp;
2958 int ret = -1;
2959
2960 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2961 goto out;
2962
2963 /*
2964 * If len is not big enough to hold the page header, then
2965 * we can not copy anything.
2966 */
2967 if (len <= BUF_PAGE_HDR_SIZE)
2968 goto out;
2969
2970 len -= BUF_PAGE_HDR_SIZE;
2971
2972 if (!data_page)
2973 goto out;
2974
2975 bpage = *data_page;
2976 if (!bpage)
2977 goto out;
2978
2979 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2980
2981 reader = rb_get_reader_page(cpu_buffer);
2982 if (!reader)
2983 goto out_unlock;
2984
2985 event = rb_reader_event(cpu_buffer);
2986
2987 read = reader->read;
2988 commit = rb_page_commit(reader);
2989
2990 /*
2991 * If this page has been partially read or
2992 * if len is not big enough to read the rest of the page or
2993 * a writer is still on the page, then
2994 * we must copy the data from the page to the buffer.
2995 * Otherwise, we can simply swap the page with the one passed in.
2996 */
2997 if (read || (len < (commit - read)) ||
2998 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2999 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
3000 unsigned int rpos = read;
3001 unsigned int pos = 0;
3002 unsigned int size;
3003
3004 if (full)
3005 goto out_unlock;
3006
3007 if (len > (commit - read))
3008 len = (commit - read);
3009
3010 size = rb_event_length(event);
3011
3012 if (len < size)
3013 goto out_unlock;
3014
3015 /* save the current timestamp, since the user will need it */
3016 save_timestamp = cpu_buffer->read_stamp;
3017
3018 /* Need to copy one event at a time */
3019 do {
3020 memcpy(bpage->data + pos, rpage->data + rpos, size);
3021
3022 len -= size;
3023
3024 rb_advance_reader(cpu_buffer);
3025 rpos = reader->read;
3026 pos += size;
3027
3028 event = rb_reader_event(cpu_buffer);
3029 size = rb_event_length(event);
3030 } while (len > size);
3031
3032 /* update bpage */
3033 local_set(&bpage->commit, pos);
3034 bpage->time_stamp = save_timestamp;
3035
3036 /* we copied everything to the beginning */
3037 read = 0;
3038 } else {
3039 /* update the entry counter */
3040 cpu_buffer->read += local_read(&reader->entries);
3041
3042 /* swap the pages */
3043 rb_init_page(bpage);
3044 bpage = reader->page;
3045 reader->page = *data_page;
3046 local_set(&reader->write, 0);
3047 local_set(&reader->entries, 0);
3048 reader->read = 0;
3049 *data_page = bpage;
3050 }
3051 ret = read;
3052
3053 out_unlock:
3054 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3055
3056 out:
3057 return ret;
3058 }
3059 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3060
3061 static ssize_t
3062 rb_simple_read(struct file *filp, char __user *ubuf,
3063 size_t cnt, loff_t *ppos)
3064 {
3065 unsigned long *p = filp->private_data;
3066 char buf[64];
3067 int r;
3068
3069 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3070 r = sprintf(buf, "permanently disabled\n");
3071 else
3072 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3073
3074 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3075 }
3076
3077 static ssize_t
3078 rb_simple_write(struct file *filp, const char __user *ubuf,
3079 size_t cnt, loff_t *ppos)
3080 {
3081 unsigned long *p = filp->private_data;
3082 char buf[64];
3083 unsigned long val;
3084 int ret;
3085
3086 if (cnt >= sizeof(buf))
3087 return -EINVAL;
3088
3089 if (copy_from_user(&buf, ubuf, cnt))
3090 return -EFAULT;
3091
3092 buf[cnt] = 0;
3093
3094 ret = strict_strtoul(buf, 10, &val);
3095 if (ret < 0)
3096 return ret;
3097
3098 if (val)
3099 set_bit(RB_BUFFERS_ON_BIT, p);
3100 else
3101 clear_bit(RB_BUFFERS_ON_BIT, p);
3102
3103 (*ppos)++;
3104
3105 return cnt;
3106 }
3107
3108 static const struct file_operations rb_simple_fops = {
3109 .open = tracing_open_generic,
3110 .read = rb_simple_read,
3111 .write = rb_simple_write,
3112 };
3113
3114
3115 static __init int rb_init_debugfs(void)
3116 {
3117 struct dentry *d_tracer;
3118
3119 d_tracer = tracing_init_dentry();
3120
3121 trace_create_file("tracing_on", 0644, d_tracer,
3122 &ring_buffer_flags, &rb_simple_fops);
3123
3124 return 0;
3125 }
3126
3127 fs_initcall(rb_init_debugfs);
3128
3129 #ifdef CONFIG_HOTPLUG_CPU
3130 static int rb_cpu_notify(struct notifier_block *self,
3131 unsigned long action, void *hcpu)
3132 {
3133 struct ring_buffer *buffer =
3134 container_of(self, struct ring_buffer, cpu_notify);
3135 long cpu = (long)hcpu;
3136
3137 switch (action) {
3138 case CPU_UP_PREPARE:
3139 case CPU_UP_PREPARE_FROZEN:
3140 if (cpumask_test_cpu(cpu, buffer->cpumask))
3141 return NOTIFY_OK;
3142
3143 buffer->buffers[cpu] =
3144 rb_allocate_cpu_buffer(buffer, cpu);
3145 if (!buffer->buffers[cpu]) {
3146 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3147 cpu);
3148 return NOTIFY_OK;
3149 }
3150 smp_wmb();
3151 cpumask_set_cpu(cpu, buffer->cpumask);
3152 break;
3153 case CPU_DOWN_PREPARE:
3154 case CPU_DOWN_PREPARE_FROZEN:
3155 /*
3156 * Do nothing.
3157 * If we were to free the buffer, then the user would
3158 * lose any trace that was in the buffer.
3159 */
3160 break;
3161 default:
3162 break;
3163 }
3164 return NOTIFY_OK;
3165 }
3166 #endif