4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
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>
25 * The ring buffer is made up of a list of pages. A separate list of pages is
26 * allocated for each CPU. A writer may only write to a buffer that is
27 * associated with the CPU it is currently executing on. A reader may read
28 * from any per cpu buffer.
30 * The reader is special. For each per cpu buffer, the reader has its own
31 * reader page. When a reader has read the entire reader page, this reader
32 * page is swapped with another page in the ring buffer.
34 * Now, as long as the writer is off the reader page, the reader can do what
35 * ever it wants with that page. The writer will never write to that page
36 * again (as long as it is out of the ring buffer).
38 * Here's some silly ASCII art.
41 * |reader| RING BUFFER
43 * +------+ +---+ +---+ +---+
52 * |reader| RING BUFFER
53 * |page |------------------v
54 * +------+ +---+ +---+ +---+
63 * |reader| RING BUFFER
64 * |page |------------------v
65 * +------+ +---+ +---+ +---+
70 * +------------------------------+
74 * |buffer| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
78 * | New +---+ +---+ +---+
81 * +------------------------------+
84 * After we make this swap, the reader can hand this page off to the splice
85 * code and be done with it. It can even allocate a new page if it needs to
86 * and swap that into the ring buffer.
88 * We will be using cmpxchg soon to make all this lockless.
93 * A fast way to enable or disable all ring buffers is to
94 * call tracing_on or tracing_off. Turning off the ring buffers
95 * prevents all ring buffers from being recorded to.
96 * Turning this switch on, makes it OK to write to the
97 * ring buffer, if the ring buffer is enabled itself.
99 * There's three layers that must be on in order to write
100 * to the ring buffer.
102 * 1) This global flag must be set.
103 * 2) The ring buffer must be enabled for recording.
104 * 3) The per cpu buffer must be enabled for recording.
106 * In case of an anomaly, this global flag has a bit set that
107 * will permantly disable all ring buffers.
111 * Global flag to disable all recording to ring buffers
112 * This has two bits: ON, DISABLED
116 * 0 0 : ring buffers are off
117 * 1 0 : ring buffers are on
118 * X 1 : ring buffers are permanently disabled
122 RB_BUFFERS_ON_BIT = 0,
123 RB_BUFFERS_DISABLED_BIT = 1,
127 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
128 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
131 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
133 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
136 * tracing_on - enable all tracing buffers
138 * This function enables all tracing buffers that may have been
139 * disabled with tracing_off.
141 void tracing_on(void)
143 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
145 EXPORT_SYMBOL_GPL(tracing_on);
148 * tracing_off - turn off all tracing buffers
150 * This function stops all tracing buffers from recording data.
151 * It does not disable any overhead the tracers themselves may
152 * be causing. This function simply causes all recording to
153 * the ring buffers to fail.
155 void tracing_off(void)
157 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
159 EXPORT_SYMBOL_GPL(tracing_off);
162 * tracing_off_permanent - permanently disable ring buffers
164 * This function, once called, will disable all ring buffers
167 void tracing_off_permanent(void)
169 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
173 * tracing_is_on - show state of ring buffers enabled
175 int tracing_is_on(void)
177 return ring_buffer_flags == RB_BUFFERS_ON;
179 EXPORT_SYMBOL_GPL(tracing_is_on);
183 /* Up this if you want to test the TIME_EXTENTS and normalization */
184 #define DEBUG_SHIFT 0
186 u64 ring_buffer_time_stamp(int cpu)
190 preempt_disable_notrace();
191 /* shift to debug/test normalization and TIME_EXTENTS */
192 time = trace_clock_local() << DEBUG_SHIFT;
193 preempt_enable_no_resched_notrace();
197 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
199 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
201 /* Just stupid testing the normalize function and deltas */
204 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
206 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
207 #define RB_ALIGNMENT 4U
208 #define RB_MAX_SMALL_DATA 28
211 RB_LEN_TIME_EXTEND = 8,
212 RB_LEN_TIME_STAMP = 16,
215 /* inline for ring buffer fast paths */
217 rb_event_length(struct ring_buffer_event *event)
221 switch (event->type) {
222 case RINGBUF_TYPE_PADDING:
226 case RINGBUF_TYPE_TIME_EXTEND:
227 return RB_LEN_TIME_EXTEND;
229 case RINGBUF_TYPE_TIME_STAMP:
230 return RB_LEN_TIME_STAMP;
232 case RINGBUF_TYPE_DATA:
234 length = event->len * RB_ALIGNMENT;
236 length = event->array[0];
237 return length + RB_EVNT_HDR_SIZE;
246 * ring_buffer_event_length - return the length of the event
247 * @event: the event to get the length of
249 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
251 unsigned length = rb_event_length(event);
252 if (event->type != RINGBUF_TYPE_DATA)
254 length -= RB_EVNT_HDR_SIZE;
255 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
256 length -= sizeof(event->array[0]);
259 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
261 /* inline for ring buffer fast paths */
263 rb_event_data(struct ring_buffer_event *event)
265 BUG_ON(event->type != RINGBUF_TYPE_DATA);
266 /* If length is in len field, then array[0] has the data */
268 return (void *)&event->array[0];
269 /* Otherwise length is in array[0] and array[1] has the data */
270 return (void *)&event->array[1];
274 * ring_buffer_event_data - return the data of the event
275 * @event: the event to get the data from
277 void *ring_buffer_event_data(struct ring_buffer_event *event)
279 return rb_event_data(event);
281 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
283 #define for_each_buffer_cpu(buffer, cpu) \
284 for_each_cpu(cpu, buffer->cpumask)
287 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
288 #define TS_DELTA_TEST (~TS_MASK)
290 struct buffer_data_page {
291 u64 time_stamp; /* page time stamp */
292 local_t commit; /* write committed index */
293 unsigned char data[]; /* data of buffer page */
297 local_t write; /* index for next write */
298 unsigned read; /* index for next read */
299 struct list_head list; /* list of free pages */
300 struct buffer_data_page *page; /* Actual data page */
303 static void rb_init_page(struct buffer_data_page *bpage)
305 local_set(&bpage->commit, 0);
309 * ring_buffer_page_len - the size of data on the page.
310 * @page: The page to read
312 * Returns the amount of data on the page, including buffer page header.
314 size_t ring_buffer_page_len(void *page)
316 return local_read(&((struct buffer_data_page *)page)->commit)
321 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
324 static void free_buffer_page(struct buffer_page *bpage)
326 free_page((unsigned long)bpage->page);
331 * We need to fit the time_stamp delta into 27 bits.
333 static inline int test_time_stamp(u64 delta)
335 if (delta & TS_DELTA_TEST)
340 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
343 * head_page == tail_page && head == tail then buffer is empty.
345 struct ring_buffer_per_cpu {
347 struct ring_buffer *buffer;
348 spinlock_t reader_lock; /* serialize readers */
350 struct lock_class_key lock_key;
351 struct list_head pages;
352 struct buffer_page *head_page; /* read from head */
353 struct buffer_page *tail_page; /* write to tail */
354 struct buffer_page *commit_page; /* committed pages */
355 struct buffer_page *reader_page;
356 unsigned long overrun;
357 unsigned long entries;
360 atomic_t record_disabled;
367 atomic_t record_disabled;
368 cpumask_var_t cpumask;
372 struct ring_buffer_per_cpu **buffers;
374 #ifdef CONFIG_HOTPLUG_CPU
375 struct notifier_block cpu_notify;
379 struct ring_buffer_iter {
380 struct ring_buffer_per_cpu *cpu_buffer;
382 struct buffer_page *head_page;
386 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
387 #define RB_WARN_ON(buffer, cond) \
389 int _____ret = unlikely(cond); \
391 atomic_inc(&buffer->record_disabled); \
398 * check_pages - integrity check of buffer pages
399 * @cpu_buffer: CPU buffer with pages to test
401 * As a safety measure we check to make sure the data pages have not
404 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
406 struct list_head *head = &cpu_buffer->pages;
407 struct buffer_page *bpage, *tmp;
409 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
411 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
414 list_for_each_entry_safe(bpage, tmp, head, list) {
415 if (RB_WARN_ON(cpu_buffer,
416 bpage->list.next->prev != &bpage->list))
418 if (RB_WARN_ON(cpu_buffer,
419 bpage->list.prev->next != &bpage->list))
426 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
429 struct list_head *head = &cpu_buffer->pages;
430 struct buffer_page *bpage, *tmp;
435 for (i = 0; i < nr_pages; i++) {
436 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
437 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
440 list_add(&bpage->list, &pages);
442 addr = __get_free_page(GFP_KERNEL);
445 bpage->page = (void *)addr;
446 rb_init_page(bpage->page);
449 list_splice(&pages, head);
451 rb_check_pages(cpu_buffer);
456 list_for_each_entry_safe(bpage, tmp, &pages, list) {
457 list_del_init(&bpage->list);
458 free_buffer_page(bpage);
463 static struct ring_buffer_per_cpu *
464 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
466 struct ring_buffer_per_cpu *cpu_buffer;
467 struct buffer_page *bpage;
471 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
472 GFP_KERNEL, cpu_to_node(cpu));
476 cpu_buffer->cpu = cpu;
477 cpu_buffer->buffer = buffer;
478 spin_lock_init(&cpu_buffer->reader_lock);
479 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
480 INIT_LIST_HEAD(&cpu_buffer->pages);
482 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
483 GFP_KERNEL, cpu_to_node(cpu));
485 goto fail_free_buffer;
487 cpu_buffer->reader_page = bpage;
488 addr = __get_free_page(GFP_KERNEL);
490 goto fail_free_reader;
491 bpage->page = (void *)addr;
492 rb_init_page(bpage->page);
494 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
496 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
498 goto fail_free_reader;
500 cpu_buffer->head_page
501 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
502 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
507 free_buffer_page(cpu_buffer->reader_page);
514 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
516 struct list_head *head = &cpu_buffer->pages;
517 struct buffer_page *bpage, *tmp;
519 list_del_init(&cpu_buffer->reader_page->list);
520 free_buffer_page(cpu_buffer->reader_page);
522 list_for_each_entry_safe(bpage, tmp, head, list) {
523 list_del_init(&bpage->list);
524 free_buffer_page(bpage);
530 * Causes compile errors if the struct buffer_page gets bigger
531 * than the struct page.
533 extern int ring_buffer_page_too_big(void);
535 #ifdef CONFIG_HOTPLUG_CPU
536 static int __cpuinit rb_cpu_notify(struct notifier_block *self,
537 unsigned long action, void *hcpu);
541 * ring_buffer_alloc - allocate a new ring_buffer
542 * @size: the size in bytes per cpu that is needed.
543 * @flags: attributes to set for the ring buffer.
545 * Currently the only flag that is available is the RB_FL_OVERWRITE
546 * flag. This flag means that the buffer will overwrite old data
547 * when the buffer wraps. If this flag is not set, the buffer will
548 * drop data when the tail hits the head.
550 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
552 struct ring_buffer *buffer;
556 /* Paranoid! Optimizes out when all is well */
557 if (sizeof(struct buffer_page) > sizeof(struct page))
558 ring_buffer_page_too_big();
561 /* keep it in its own cache line */
562 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
567 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
568 goto fail_free_buffer;
570 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
571 buffer->flags = flags;
573 /* need at least two pages */
574 if (buffer->pages == 1)
578 cpumask_copy(buffer->cpumask, cpu_online_mask);
579 buffer->cpus = nr_cpu_ids;
581 bsize = sizeof(void *) * nr_cpu_ids;
582 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
584 if (!buffer->buffers)
585 goto fail_free_cpumask;
587 for_each_buffer_cpu(buffer, cpu) {
588 buffer->buffers[cpu] =
589 rb_allocate_cpu_buffer(buffer, cpu);
590 if (!buffer->buffers[cpu])
591 goto fail_free_buffers;
594 #ifdef CONFIG_HOTPLUG_CPU
595 buffer->cpu_notify.notifier_call = rb_cpu_notify;
596 buffer->cpu_notify.priority = 0;
597 register_cpu_notifier(&buffer->cpu_notify);
601 mutex_init(&buffer->mutex);
606 for_each_buffer_cpu(buffer, cpu) {
607 if (buffer->buffers[cpu])
608 rb_free_cpu_buffer(buffer->buffers[cpu]);
610 kfree(buffer->buffers);
613 free_cpumask_var(buffer->cpumask);
620 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
623 * ring_buffer_free - free a ring buffer.
624 * @buffer: the buffer to free.
627 ring_buffer_free(struct ring_buffer *buffer)
633 #ifdef CONFIG_HOTPLUG_CPU
634 unregister_cpu_notifier(&buffer->cpu_notify);
637 for_each_buffer_cpu(buffer, cpu)
638 rb_free_cpu_buffer(buffer->buffers[cpu]);
642 free_cpumask_var(buffer->cpumask);
646 EXPORT_SYMBOL_GPL(ring_buffer_free);
648 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
651 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
653 struct buffer_page *bpage;
657 atomic_inc(&cpu_buffer->record_disabled);
660 for (i = 0; i < nr_pages; i++) {
661 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
663 p = cpu_buffer->pages.next;
664 bpage = list_entry(p, struct buffer_page, list);
665 list_del_init(&bpage->list);
666 free_buffer_page(bpage);
668 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
671 rb_reset_cpu(cpu_buffer);
673 rb_check_pages(cpu_buffer);
675 atomic_dec(&cpu_buffer->record_disabled);
680 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
681 struct list_head *pages, unsigned nr_pages)
683 struct buffer_page *bpage;
687 atomic_inc(&cpu_buffer->record_disabled);
690 for (i = 0; i < nr_pages; i++) {
691 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
694 bpage = list_entry(p, struct buffer_page, list);
695 list_del_init(&bpage->list);
696 list_add_tail(&bpage->list, &cpu_buffer->pages);
698 rb_reset_cpu(cpu_buffer);
700 rb_check_pages(cpu_buffer);
702 atomic_dec(&cpu_buffer->record_disabled);
706 * ring_buffer_resize - resize the ring buffer
707 * @buffer: the buffer to resize.
708 * @size: the new size.
710 * The tracer is responsible for making sure that the buffer is
711 * not being used while changing the size.
712 * Note: We may be able to change the above requirement by using
713 * RCU synchronizations.
715 * Minimum size is 2 * BUF_PAGE_SIZE.
717 * Returns -1 on failure.
719 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
721 struct ring_buffer_per_cpu *cpu_buffer;
722 unsigned nr_pages, rm_pages, new_pages;
723 struct buffer_page *bpage, *tmp;
724 unsigned long buffer_size;
730 * Always succeed at resizing a non-existent buffer:
735 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
736 size *= BUF_PAGE_SIZE;
737 buffer_size = buffer->pages * BUF_PAGE_SIZE;
739 /* we need a minimum of two pages */
740 if (size < BUF_PAGE_SIZE * 2)
741 size = BUF_PAGE_SIZE * 2;
743 if (size == buffer_size)
746 mutex_lock(&buffer->mutex);
749 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
751 if (size < buffer_size) {
753 /* easy case, just free pages */
754 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
757 rm_pages = buffer->pages - nr_pages;
759 for_each_buffer_cpu(buffer, cpu) {
760 cpu_buffer = buffer->buffers[cpu];
761 rb_remove_pages(cpu_buffer, rm_pages);
767 * This is a bit more difficult. We only want to add pages
768 * when we can allocate enough for all CPUs. We do this
769 * by allocating all the pages and storing them on a local
770 * link list. If we succeed in our allocation, then we
771 * add these pages to the cpu_buffers. Otherwise we just free
772 * them all and return -ENOMEM;
774 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
777 new_pages = nr_pages - buffer->pages;
779 for_each_buffer_cpu(buffer, cpu) {
780 for (i = 0; i < new_pages; i++) {
781 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
783 GFP_KERNEL, cpu_to_node(cpu));
786 list_add(&bpage->list, &pages);
787 addr = __get_free_page(GFP_KERNEL);
790 bpage->page = (void *)addr;
791 rb_init_page(bpage->page);
795 for_each_buffer_cpu(buffer, cpu) {
796 cpu_buffer = buffer->buffers[cpu];
797 rb_insert_pages(cpu_buffer, &pages, new_pages);
800 if (RB_WARN_ON(buffer, !list_empty(&pages)))
804 buffer->pages = nr_pages;
806 mutex_unlock(&buffer->mutex);
811 list_for_each_entry_safe(bpage, tmp, &pages, list) {
812 list_del_init(&bpage->list);
813 free_buffer_page(bpage);
816 mutex_unlock(&buffer->mutex);
820 * Something went totally wrong, and we are too paranoid
821 * to even clean up the mess.
825 mutex_unlock(&buffer->mutex);
828 EXPORT_SYMBOL_GPL(ring_buffer_resize);
830 static inline int rb_null_event(struct ring_buffer_event *event)
832 return event->type == RINGBUF_TYPE_PADDING;
836 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
838 return bpage->data + index;
841 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
843 return bpage->page->data + index;
846 static inline struct ring_buffer_event *
847 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
849 return __rb_page_index(cpu_buffer->reader_page,
850 cpu_buffer->reader_page->read);
853 static inline struct ring_buffer_event *
854 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
856 return __rb_page_index(cpu_buffer->head_page,
857 cpu_buffer->head_page->read);
860 static inline struct ring_buffer_event *
861 rb_iter_head_event(struct ring_buffer_iter *iter)
863 return __rb_page_index(iter->head_page, iter->head);
866 static inline unsigned rb_page_write(struct buffer_page *bpage)
868 return local_read(&bpage->write);
871 static inline unsigned rb_page_commit(struct buffer_page *bpage)
873 return local_read(&bpage->page->commit);
876 /* Size is determined by what has been commited */
877 static inline unsigned rb_page_size(struct buffer_page *bpage)
879 return rb_page_commit(bpage);
882 static inline unsigned
883 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
885 return rb_page_commit(cpu_buffer->commit_page);
888 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
890 return rb_page_commit(cpu_buffer->head_page);
894 * When the tail hits the head and the buffer is in overwrite mode,
895 * the head jumps to the next page and all content on the previous
896 * page is discarded. But before doing so, we update the overrun
897 * variable of the buffer.
899 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
901 struct ring_buffer_event *event;
904 for (head = 0; head < rb_head_size(cpu_buffer);
905 head += rb_event_length(event)) {
907 event = __rb_page_index(cpu_buffer->head_page, head);
908 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
910 /* Only count data entries */
911 if (event->type != RINGBUF_TYPE_DATA)
913 cpu_buffer->overrun++;
914 cpu_buffer->entries--;
918 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
919 struct buffer_page **bpage)
921 struct list_head *p = (*bpage)->list.next;
923 if (p == &cpu_buffer->pages)
926 *bpage = list_entry(p, struct buffer_page, list);
929 static inline unsigned
930 rb_event_index(struct ring_buffer_event *event)
932 unsigned long addr = (unsigned long)event;
934 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
938 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
939 struct ring_buffer_event *event)
941 unsigned long addr = (unsigned long)event;
944 index = rb_event_index(event);
947 return cpu_buffer->commit_page->page == (void *)addr &&
948 rb_commit_index(cpu_buffer) == index;
952 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
953 struct ring_buffer_event *event)
955 unsigned long addr = (unsigned long)event;
958 index = rb_event_index(event);
961 while (cpu_buffer->commit_page->page != (void *)addr) {
962 if (RB_WARN_ON(cpu_buffer,
963 cpu_buffer->commit_page == cpu_buffer->tail_page))
965 cpu_buffer->commit_page->page->commit =
966 cpu_buffer->commit_page->write;
967 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
968 cpu_buffer->write_stamp =
969 cpu_buffer->commit_page->page->time_stamp;
972 /* Now set the commit to the event's index */
973 local_set(&cpu_buffer->commit_page->page->commit, index);
977 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
980 * We only race with interrupts and NMIs on this CPU.
981 * If we own the commit event, then we can commit
982 * all others that interrupted us, since the interruptions
983 * are in stack format (they finish before they come
984 * back to us). This allows us to do a simple loop to
985 * assign the commit to the tail.
988 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
989 cpu_buffer->commit_page->page->commit =
990 cpu_buffer->commit_page->write;
991 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
992 cpu_buffer->write_stamp =
993 cpu_buffer->commit_page->page->time_stamp;
994 /* add barrier to keep gcc from optimizing too much */
997 while (rb_commit_index(cpu_buffer) !=
998 rb_page_write(cpu_buffer->commit_page)) {
999 cpu_buffer->commit_page->page->commit =
1000 cpu_buffer->commit_page->write;
1004 /* again, keep gcc from optimizing */
1008 * If an interrupt came in just after the first while loop
1009 * and pushed the tail page forward, we will be left with
1010 * a dangling commit that will never go forward.
1012 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1016 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1018 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1019 cpu_buffer->reader_page->read = 0;
1022 static void rb_inc_iter(struct ring_buffer_iter *iter)
1024 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1027 * The iterator could be on the reader page (it starts there).
1028 * But the head could have moved, since the reader was
1029 * found. Check for this case and assign the iterator
1030 * to the head page instead of next.
1032 if (iter->head_page == cpu_buffer->reader_page)
1033 iter->head_page = cpu_buffer->head_page;
1035 rb_inc_page(cpu_buffer, &iter->head_page);
1037 iter->read_stamp = iter->head_page->page->time_stamp;
1042 * ring_buffer_update_event - update event type and data
1043 * @event: the even to update
1044 * @type: the type of event
1045 * @length: the size of the event field in the ring buffer
1047 * Update the type and data fields of the event. The length
1048 * is the actual size that is written to the ring buffer,
1049 * and with this, we can determine what to place into the
1053 rb_update_event(struct ring_buffer_event *event,
1054 unsigned type, unsigned length)
1060 case RINGBUF_TYPE_PADDING:
1063 case RINGBUF_TYPE_TIME_EXTEND:
1064 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
1067 case RINGBUF_TYPE_TIME_STAMP:
1068 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
1071 case RINGBUF_TYPE_DATA:
1072 length -= RB_EVNT_HDR_SIZE;
1073 if (length > RB_MAX_SMALL_DATA) {
1075 event->array[0] = length;
1077 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1084 static unsigned rb_calculate_event_length(unsigned length)
1086 struct ring_buffer_event event; /* Used only for sizeof array */
1088 /* zero length can cause confusions */
1092 if (length > RB_MAX_SMALL_DATA)
1093 length += sizeof(event.array[0]);
1095 length += RB_EVNT_HDR_SIZE;
1096 length = ALIGN(length, RB_ALIGNMENT);
1101 static struct ring_buffer_event *
1102 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1103 unsigned type, unsigned long length, u64 *ts)
1105 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1106 unsigned long tail, write;
1107 struct ring_buffer *buffer = cpu_buffer->buffer;
1108 struct ring_buffer_event *event;
1109 unsigned long flags;
1110 bool lock_taken = false;
1112 commit_page = cpu_buffer->commit_page;
1113 /* we just need to protect against interrupts */
1115 tail_page = cpu_buffer->tail_page;
1116 write = local_add_return(length, &tail_page->write);
1117 tail = write - length;
1119 /* See if we shot pass the end of this buffer page */
1120 if (write > BUF_PAGE_SIZE) {
1121 struct buffer_page *next_page = tail_page;
1123 local_irq_save(flags);
1125 * Since the write to the buffer is still not
1126 * fully lockless, we must be careful with NMIs.
1127 * The locks in the writers are taken when a write
1128 * crosses to a new page. The locks protect against
1129 * races with the readers (this will soon be fixed
1130 * with a lockless solution).
1132 * Because we can not protect against NMIs, and we
1133 * want to keep traces reentrant, we need to manage
1134 * what happens when we are in an NMI.
1136 * NMIs can happen after we take the lock.
1137 * If we are in an NMI, only take the lock
1138 * if it is not already taken. Otherwise
1141 if (unlikely(in_nmi())) {
1142 if (!__raw_spin_trylock(&cpu_buffer->lock))
1145 __raw_spin_lock(&cpu_buffer->lock);
1149 rb_inc_page(cpu_buffer, &next_page);
1151 head_page = cpu_buffer->head_page;
1152 reader_page = cpu_buffer->reader_page;
1154 /* we grabbed the lock before incrementing */
1155 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1159 * If for some reason, we had an interrupt storm that made
1160 * it all the way around the buffer, bail, and warn
1163 if (unlikely(next_page == commit_page)) {
1168 if (next_page == head_page) {
1169 if (!(buffer->flags & RB_FL_OVERWRITE))
1172 /* tail_page has not moved yet? */
1173 if (tail_page == cpu_buffer->tail_page) {
1174 /* count overflows */
1175 rb_update_overflow(cpu_buffer);
1177 rb_inc_page(cpu_buffer, &head_page);
1178 cpu_buffer->head_page = head_page;
1179 cpu_buffer->head_page->read = 0;
1184 * If the tail page is still the same as what we think
1185 * it is, then it is up to us to update the tail
1188 if (tail_page == cpu_buffer->tail_page) {
1189 local_set(&next_page->write, 0);
1190 local_set(&next_page->page->commit, 0);
1191 cpu_buffer->tail_page = next_page;
1193 /* reread the time stamp */
1194 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1195 cpu_buffer->tail_page->page->time_stamp = *ts;
1199 * The actual tail page has moved forward.
1201 if (tail < BUF_PAGE_SIZE) {
1202 /* Mark the rest of the page with padding */
1203 event = __rb_page_index(tail_page, tail);
1204 event->type = RINGBUF_TYPE_PADDING;
1207 if (tail <= BUF_PAGE_SIZE)
1208 /* Set the write back to the previous setting */
1209 local_set(&tail_page->write, tail);
1212 * If this was a commit entry that failed,
1213 * increment that too
1215 if (tail_page == cpu_buffer->commit_page &&
1216 tail == rb_commit_index(cpu_buffer)) {
1217 rb_set_commit_to_write(cpu_buffer);
1220 __raw_spin_unlock(&cpu_buffer->lock);
1221 local_irq_restore(flags);
1223 /* fail and let the caller try again */
1224 return ERR_PTR(-EAGAIN);
1227 /* We reserved something on the buffer */
1229 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1232 event = __rb_page_index(tail_page, tail);
1233 rb_update_event(event, type, length);
1236 * If this is a commit and the tail is zero, then update
1237 * this page's time stamp.
1239 if (!tail && rb_is_commit(cpu_buffer, event))
1240 cpu_buffer->commit_page->page->time_stamp = *ts;
1246 if (tail <= BUF_PAGE_SIZE)
1247 local_set(&tail_page->write, tail);
1249 if (likely(lock_taken))
1250 __raw_spin_unlock(&cpu_buffer->lock);
1251 local_irq_restore(flags);
1256 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1257 u64 *ts, u64 *delta)
1259 struct ring_buffer_event *event;
1263 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1264 printk(KERN_WARNING "Delta way too big! %llu"
1265 " ts=%llu write stamp = %llu\n",
1266 (unsigned long long)*delta,
1267 (unsigned long long)*ts,
1268 (unsigned long long)cpu_buffer->write_stamp);
1273 * The delta is too big, we to add a
1276 event = __rb_reserve_next(cpu_buffer,
1277 RINGBUF_TYPE_TIME_EXTEND,
1283 if (PTR_ERR(event) == -EAGAIN)
1286 /* Only a commited time event can update the write stamp */
1287 if (rb_is_commit(cpu_buffer, event)) {
1289 * If this is the first on the page, then we need to
1290 * update the page itself, and just put in a zero.
1292 if (rb_event_index(event)) {
1293 event->time_delta = *delta & TS_MASK;
1294 event->array[0] = *delta >> TS_SHIFT;
1296 cpu_buffer->commit_page->page->time_stamp = *ts;
1297 event->time_delta = 0;
1298 event->array[0] = 0;
1300 cpu_buffer->write_stamp = *ts;
1301 /* let the caller know this was the commit */
1304 /* Darn, this is just wasted space */
1305 event->time_delta = 0;
1306 event->array[0] = 0;
1315 static struct ring_buffer_event *
1316 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1317 unsigned type, unsigned long length)
1319 struct ring_buffer_event *event;
1326 * We allow for interrupts to reenter here and do a trace.
1327 * If one does, it will cause this original code to loop
1328 * back here. Even with heavy interrupts happening, this
1329 * should only happen a few times in a row. If this happens
1330 * 1000 times in a row, there must be either an interrupt
1331 * storm or we have something buggy.
1334 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1337 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1340 * Only the first commit can update the timestamp.
1341 * Yes there is a race here. If an interrupt comes in
1342 * just after the conditional and it traces too, then it
1343 * will also check the deltas. More than one timestamp may
1344 * also be made. But only the entry that did the actual
1345 * commit will be something other than zero.
1347 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1348 rb_page_write(cpu_buffer->tail_page) ==
1349 rb_commit_index(cpu_buffer)) {
1351 delta = ts - cpu_buffer->write_stamp;
1353 /* make sure this delta is calculated here */
1356 /* Did the write stamp get updated already? */
1357 if (unlikely(ts < cpu_buffer->write_stamp))
1360 if (test_time_stamp(delta)) {
1362 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1364 if (commit == -EBUSY)
1367 if (commit == -EAGAIN)
1370 RB_WARN_ON(cpu_buffer, commit < 0);
1373 /* Non commits have zero deltas */
1376 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1377 if (PTR_ERR(event) == -EAGAIN)
1381 if (unlikely(commit))
1383 * Ouch! We needed a timestamp and it was commited. But
1384 * we didn't get our event reserved.
1386 rb_set_commit_to_write(cpu_buffer);
1391 * If the timestamp was commited, make the commit our entry
1392 * now so that we will update it when needed.
1395 rb_set_commit_event(cpu_buffer, event);
1396 else if (!rb_is_commit(cpu_buffer, event))
1399 event->time_delta = delta;
1404 static DEFINE_PER_CPU(int, rb_need_resched);
1407 * ring_buffer_lock_reserve - reserve a part of the buffer
1408 * @buffer: the ring buffer to reserve from
1409 * @length: the length of the data to reserve (excluding event header)
1411 * Returns a reseverd event on the ring buffer to copy directly to.
1412 * The user of this interface will need to get the body to write into
1413 * and can use the ring_buffer_event_data() interface.
1415 * The length is the length of the data needed, not the event length
1416 * which also includes the event header.
1418 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1419 * If NULL is returned, then nothing has been allocated or locked.
1421 struct ring_buffer_event *
1422 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1424 struct ring_buffer_per_cpu *cpu_buffer;
1425 struct ring_buffer_event *event;
1428 if (ring_buffer_flags != RB_BUFFERS_ON)
1431 if (atomic_read(&buffer->record_disabled))
1434 /* If we are tracing schedule, we don't want to recurse */
1435 resched = ftrace_preempt_disable();
1437 cpu = raw_smp_processor_id();
1439 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1442 cpu_buffer = buffer->buffers[cpu];
1444 if (atomic_read(&cpu_buffer->record_disabled))
1447 length = rb_calculate_event_length(length);
1448 if (length > BUF_PAGE_SIZE)
1451 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1456 * Need to store resched state on this cpu.
1457 * Only the first needs to.
1460 if (preempt_count() == 1)
1461 per_cpu(rb_need_resched, cpu) = resched;
1466 ftrace_preempt_enable(resched);
1469 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1471 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1472 struct ring_buffer_event *event)
1474 cpu_buffer->entries++;
1476 /* Only process further if we own the commit */
1477 if (!rb_is_commit(cpu_buffer, event))
1480 cpu_buffer->write_stamp += event->time_delta;
1482 rb_set_commit_to_write(cpu_buffer);
1486 * ring_buffer_unlock_commit - commit a reserved
1487 * @buffer: The buffer to commit to
1488 * @event: The event pointer to commit.
1490 * This commits the data to the ring buffer, and releases any locks held.
1492 * Must be paired with ring_buffer_lock_reserve.
1494 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1495 struct ring_buffer_event *event)
1497 struct ring_buffer_per_cpu *cpu_buffer;
1498 int cpu = raw_smp_processor_id();
1500 cpu_buffer = buffer->buffers[cpu];
1502 rb_commit(cpu_buffer, event);
1505 * Only the last preempt count needs to restore preemption.
1507 if (preempt_count() == 1)
1508 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1510 preempt_enable_no_resched_notrace();
1514 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1517 * ring_buffer_write - write data to the buffer without reserving
1518 * @buffer: The ring buffer to write to.
1519 * @length: The length of the data being written (excluding the event header)
1520 * @data: The data to write to the buffer.
1522 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1523 * one function. If you already have the data to write to the buffer, it
1524 * may be easier to simply call this function.
1526 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1527 * and not the length of the event which would hold the header.
1529 int ring_buffer_write(struct ring_buffer *buffer,
1530 unsigned long length,
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 struct ring_buffer_event *event;
1535 unsigned long event_length;
1540 if (ring_buffer_flags != RB_BUFFERS_ON)
1543 if (atomic_read(&buffer->record_disabled))
1546 resched = ftrace_preempt_disable();
1548 cpu = raw_smp_processor_id();
1550 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1553 cpu_buffer = buffer->buffers[cpu];
1555 if (atomic_read(&cpu_buffer->record_disabled))
1558 event_length = rb_calculate_event_length(length);
1559 event = rb_reserve_next_event(cpu_buffer,
1560 RINGBUF_TYPE_DATA, event_length);
1564 body = rb_event_data(event);
1566 memcpy(body, data, length);
1568 rb_commit(cpu_buffer, event);
1572 ftrace_preempt_enable(resched);
1576 EXPORT_SYMBOL_GPL(ring_buffer_write);
1578 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1580 struct buffer_page *reader = cpu_buffer->reader_page;
1581 struct buffer_page *head = cpu_buffer->head_page;
1582 struct buffer_page *commit = cpu_buffer->commit_page;
1584 return reader->read == rb_page_commit(reader) &&
1585 (commit == reader ||
1587 head->read == rb_page_commit(commit)));
1591 * ring_buffer_record_disable - stop all writes into the buffer
1592 * @buffer: The ring buffer to stop writes to.
1594 * This prevents all writes to the buffer. Any attempt to write
1595 * to the buffer after this will fail and return NULL.
1597 * The caller should call synchronize_sched() after this.
1599 void ring_buffer_record_disable(struct ring_buffer *buffer)
1601 atomic_inc(&buffer->record_disabled);
1603 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1606 * ring_buffer_record_enable - enable writes to the buffer
1607 * @buffer: The ring buffer to enable writes
1609 * Note, multiple disables will need the same number of enables
1610 * to truely enable the writing (much like preempt_disable).
1612 void ring_buffer_record_enable(struct ring_buffer *buffer)
1614 atomic_dec(&buffer->record_disabled);
1616 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1619 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1620 * @buffer: The ring buffer to stop writes to.
1621 * @cpu: The CPU buffer to stop
1623 * This prevents all writes to the buffer. Any attempt to write
1624 * to the buffer after this will fail and return NULL.
1626 * The caller should call synchronize_sched() after this.
1628 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1630 struct ring_buffer_per_cpu *cpu_buffer;
1632 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1635 cpu_buffer = buffer->buffers[cpu];
1636 atomic_inc(&cpu_buffer->record_disabled);
1638 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1641 * ring_buffer_record_enable_cpu - enable writes to the buffer
1642 * @buffer: The ring buffer to enable writes
1643 * @cpu: The CPU to enable.
1645 * Note, multiple disables will need the same number of enables
1646 * to truely enable the writing (much like preempt_disable).
1648 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1650 struct ring_buffer_per_cpu *cpu_buffer;
1652 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1655 cpu_buffer = buffer->buffers[cpu];
1656 atomic_dec(&cpu_buffer->record_disabled);
1658 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1661 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1662 * @buffer: The ring buffer
1663 * @cpu: The per CPU buffer to get the entries from.
1665 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1667 struct ring_buffer_per_cpu *cpu_buffer;
1670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1673 cpu_buffer = buffer->buffers[cpu];
1674 ret = cpu_buffer->entries;
1678 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1681 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1682 * @buffer: The ring buffer
1683 * @cpu: The per CPU buffer to get the number of overruns from
1685 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1687 struct ring_buffer_per_cpu *cpu_buffer;
1690 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1693 cpu_buffer = buffer->buffers[cpu];
1694 ret = cpu_buffer->overrun;
1698 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1701 * ring_buffer_entries - get the number of entries in a buffer
1702 * @buffer: The ring buffer
1704 * Returns the total number of entries in the ring buffer
1707 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1709 struct ring_buffer_per_cpu *cpu_buffer;
1710 unsigned long entries = 0;
1713 /* if you care about this being correct, lock the buffer */
1714 for_each_buffer_cpu(buffer, cpu) {
1715 cpu_buffer = buffer->buffers[cpu];
1716 entries += cpu_buffer->entries;
1721 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1724 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1725 * @buffer: The ring buffer
1727 * Returns the total number of overruns in the ring buffer
1730 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1732 struct ring_buffer_per_cpu *cpu_buffer;
1733 unsigned long overruns = 0;
1736 /* if you care about this being correct, lock the buffer */
1737 for_each_buffer_cpu(buffer, cpu) {
1738 cpu_buffer = buffer->buffers[cpu];
1739 overruns += cpu_buffer->overrun;
1744 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1746 static void rb_iter_reset(struct ring_buffer_iter *iter)
1748 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1750 /* Iterator usage is expected to have record disabled */
1751 if (list_empty(&cpu_buffer->reader_page->list)) {
1752 iter->head_page = cpu_buffer->head_page;
1753 iter->head = cpu_buffer->head_page->read;
1755 iter->head_page = cpu_buffer->reader_page;
1756 iter->head = cpu_buffer->reader_page->read;
1759 iter->read_stamp = cpu_buffer->read_stamp;
1761 iter->read_stamp = iter->head_page->page->time_stamp;
1765 * ring_buffer_iter_reset - reset an iterator
1766 * @iter: The iterator to reset
1768 * Resets the iterator, so that it will start from the beginning
1771 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1773 struct ring_buffer_per_cpu *cpu_buffer;
1774 unsigned long flags;
1779 cpu_buffer = iter->cpu_buffer;
1781 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1782 rb_iter_reset(iter);
1783 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1785 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1788 * ring_buffer_iter_empty - check if an iterator has no more to read
1789 * @iter: The iterator to check
1791 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1793 struct ring_buffer_per_cpu *cpu_buffer;
1795 cpu_buffer = iter->cpu_buffer;
1797 return iter->head_page == cpu_buffer->commit_page &&
1798 iter->head == rb_commit_index(cpu_buffer);
1800 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1803 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1804 struct ring_buffer_event *event)
1808 switch (event->type) {
1809 case RINGBUF_TYPE_PADDING:
1812 case RINGBUF_TYPE_TIME_EXTEND:
1813 delta = event->array[0];
1815 delta += event->time_delta;
1816 cpu_buffer->read_stamp += delta;
1819 case RINGBUF_TYPE_TIME_STAMP:
1820 /* FIXME: not implemented */
1823 case RINGBUF_TYPE_DATA:
1824 cpu_buffer->read_stamp += event->time_delta;
1834 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1835 struct ring_buffer_event *event)
1839 switch (event->type) {
1840 case RINGBUF_TYPE_PADDING:
1843 case RINGBUF_TYPE_TIME_EXTEND:
1844 delta = event->array[0];
1846 delta += event->time_delta;
1847 iter->read_stamp += delta;
1850 case RINGBUF_TYPE_TIME_STAMP:
1851 /* FIXME: not implemented */
1854 case RINGBUF_TYPE_DATA:
1855 iter->read_stamp += event->time_delta;
1864 static struct buffer_page *
1865 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1867 struct buffer_page *reader = NULL;
1868 unsigned long flags;
1871 local_irq_save(flags);
1872 __raw_spin_lock(&cpu_buffer->lock);
1876 * This should normally only loop twice. But because the
1877 * start of the reader inserts an empty page, it causes
1878 * a case where we will loop three times. There should be no
1879 * reason to loop four times (that I know of).
1881 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1886 reader = cpu_buffer->reader_page;
1888 /* If there's more to read, return this page */
1889 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1892 /* Never should we have an index greater than the size */
1893 if (RB_WARN_ON(cpu_buffer,
1894 cpu_buffer->reader_page->read > rb_page_size(reader)))
1897 /* check if we caught up to the tail */
1899 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1903 * Splice the empty reader page into the list around the head.
1904 * Reset the reader page to size zero.
1907 reader = cpu_buffer->head_page;
1908 cpu_buffer->reader_page->list.next = reader->list.next;
1909 cpu_buffer->reader_page->list.prev = reader->list.prev;
1911 local_set(&cpu_buffer->reader_page->write, 0);
1912 local_set(&cpu_buffer->reader_page->page->commit, 0);
1914 /* Make the reader page now replace the head */
1915 reader->list.prev->next = &cpu_buffer->reader_page->list;
1916 reader->list.next->prev = &cpu_buffer->reader_page->list;
1919 * If the tail is on the reader, then we must set the head
1920 * to the inserted page, otherwise we set it one before.
1922 cpu_buffer->head_page = cpu_buffer->reader_page;
1924 if (cpu_buffer->commit_page != reader)
1925 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1927 /* Finally update the reader page to the new head */
1928 cpu_buffer->reader_page = reader;
1929 rb_reset_reader_page(cpu_buffer);
1934 __raw_spin_unlock(&cpu_buffer->lock);
1935 local_irq_restore(flags);
1940 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1942 struct ring_buffer_event *event;
1943 struct buffer_page *reader;
1946 reader = rb_get_reader_page(cpu_buffer);
1948 /* This function should not be called when buffer is empty */
1949 if (RB_WARN_ON(cpu_buffer, !reader))
1952 event = rb_reader_event(cpu_buffer);
1954 if (event->type == RINGBUF_TYPE_DATA)
1955 cpu_buffer->entries--;
1957 rb_update_read_stamp(cpu_buffer, event);
1959 length = rb_event_length(event);
1960 cpu_buffer->reader_page->read += length;
1963 static void rb_advance_iter(struct ring_buffer_iter *iter)
1965 struct ring_buffer *buffer;
1966 struct ring_buffer_per_cpu *cpu_buffer;
1967 struct ring_buffer_event *event;
1970 cpu_buffer = iter->cpu_buffer;
1971 buffer = cpu_buffer->buffer;
1974 * Check if we are at the end of the buffer.
1976 if (iter->head >= rb_page_size(iter->head_page)) {
1977 if (RB_WARN_ON(buffer,
1978 iter->head_page == cpu_buffer->commit_page))
1984 event = rb_iter_head_event(iter);
1986 length = rb_event_length(event);
1989 * This should not be called to advance the header if we are
1990 * at the tail of the buffer.
1992 if (RB_WARN_ON(cpu_buffer,
1993 (iter->head_page == cpu_buffer->commit_page) &&
1994 (iter->head + length > rb_commit_index(cpu_buffer))))
1997 rb_update_iter_read_stamp(iter, event);
1999 iter->head += length;
2001 /* check for end of page padding */
2002 if ((iter->head >= rb_page_size(iter->head_page)) &&
2003 (iter->head_page != cpu_buffer->commit_page))
2004 rb_advance_iter(iter);
2007 static struct ring_buffer_event *
2008 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2010 struct ring_buffer_per_cpu *cpu_buffer;
2011 struct ring_buffer_event *event;
2012 struct buffer_page *reader;
2015 cpu_buffer = buffer->buffers[cpu];
2019 * We repeat when a timestamp is encountered. It is possible
2020 * to get multiple timestamps from an interrupt entering just
2021 * as one timestamp is about to be written. The max times
2022 * that this can happen is the number of nested interrupts we
2023 * can have. Nesting 10 deep of interrupts is clearly
2026 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2029 reader = rb_get_reader_page(cpu_buffer);
2033 event = rb_reader_event(cpu_buffer);
2035 switch (event->type) {
2036 case RINGBUF_TYPE_PADDING:
2037 RB_WARN_ON(cpu_buffer, 1);
2038 rb_advance_reader(cpu_buffer);
2041 case RINGBUF_TYPE_TIME_EXTEND:
2042 /* Internal data, OK to advance */
2043 rb_advance_reader(cpu_buffer);
2046 case RINGBUF_TYPE_TIME_STAMP:
2047 /* FIXME: not implemented */
2048 rb_advance_reader(cpu_buffer);
2051 case RINGBUF_TYPE_DATA:
2053 *ts = cpu_buffer->read_stamp + event->time_delta;
2054 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2064 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2066 static struct ring_buffer_event *
2067 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2069 struct ring_buffer *buffer;
2070 struct ring_buffer_per_cpu *cpu_buffer;
2071 struct ring_buffer_event *event;
2074 if (ring_buffer_iter_empty(iter))
2077 cpu_buffer = iter->cpu_buffer;
2078 buffer = cpu_buffer->buffer;
2082 * We repeat when a timestamp is encountered. It is possible
2083 * to get multiple timestamps from an interrupt entering just
2084 * as one timestamp is about to be written. The max times
2085 * that this can happen is the number of nested interrupts we
2086 * can have. Nesting 10 deep of interrupts is clearly
2089 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2092 if (rb_per_cpu_empty(cpu_buffer))
2095 event = rb_iter_head_event(iter);
2097 switch (event->type) {
2098 case RINGBUF_TYPE_PADDING:
2102 case RINGBUF_TYPE_TIME_EXTEND:
2103 /* Internal data, OK to advance */
2104 rb_advance_iter(iter);
2107 case RINGBUF_TYPE_TIME_STAMP:
2108 /* FIXME: not implemented */
2109 rb_advance_iter(iter);
2112 case RINGBUF_TYPE_DATA:
2114 *ts = iter->read_stamp + event->time_delta;
2115 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2125 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2128 * ring_buffer_peek - peek at the next event to be read
2129 * @buffer: The ring buffer to read
2130 * @cpu: The cpu to peak at
2131 * @ts: The timestamp counter of this event.
2133 * This will return the event that will be read next, but does
2134 * not consume the data.
2136 struct ring_buffer_event *
2137 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2139 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2140 struct ring_buffer_event *event;
2141 unsigned long flags;
2143 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2146 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2147 event = rb_buffer_peek(buffer, cpu, ts);
2148 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2154 * ring_buffer_iter_peek - peek at the next event to be read
2155 * @iter: The ring buffer iterator
2156 * @ts: The timestamp counter of this event.
2158 * This will return the event that will be read next, but does
2159 * not increment the iterator.
2161 struct ring_buffer_event *
2162 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2164 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2165 struct ring_buffer_event *event;
2166 unsigned long flags;
2168 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2169 event = rb_iter_peek(iter, ts);
2170 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2176 * ring_buffer_consume - return an event and consume it
2177 * @buffer: The ring buffer to get the next event from
2179 * Returns the next event in the ring buffer, and that event is consumed.
2180 * Meaning, that sequential reads will keep returning a different event,
2181 * and eventually empty the ring buffer if the producer is slower.
2183 struct ring_buffer_event *
2184 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2186 struct ring_buffer_per_cpu *cpu_buffer;
2187 struct ring_buffer_event *event = NULL;
2188 unsigned long flags;
2190 /* might be called in atomic */
2193 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2196 cpu_buffer = buffer->buffers[cpu];
2197 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2199 event = rb_buffer_peek(buffer, cpu, ts);
2203 rb_advance_reader(cpu_buffer);
2206 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2213 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2216 * ring_buffer_read_start - start a non consuming read of the buffer
2217 * @buffer: The ring buffer to read from
2218 * @cpu: The cpu buffer to iterate over
2220 * This starts up an iteration through the buffer. It also disables
2221 * the recording to the buffer until the reading is finished.
2222 * This prevents the reading from being corrupted. This is not
2223 * a consuming read, so a producer is not expected.
2225 * Must be paired with ring_buffer_finish.
2227 struct ring_buffer_iter *
2228 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2230 struct ring_buffer_per_cpu *cpu_buffer;
2231 struct ring_buffer_iter *iter;
2232 unsigned long flags;
2234 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2237 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2241 cpu_buffer = buffer->buffers[cpu];
2243 iter->cpu_buffer = cpu_buffer;
2245 atomic_inc(&cpu_buffer->record_disabled);
2246 synchronize_sched();
2248 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2249 __raw_spin_lock(&cpu_buffer->lock);
2250 rb_iter_reset(iter);
2251 __raw_spin_unlock(&cpu_buffer->lock);
2252 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2256 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2259 * ring_buffer_finish - finish reading the iterator of the buffer
2260 * @iter: The iterator retrieved by ring_buffer_start
2262 * This re-enables the recording to the buffer, and frees the
2266 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2268 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2270 atomic_dec(&cpu_buffer->record_disabled);
2273 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2276 * ring_buffer_read - read the next item in the ring buffer by the iterator
2277 * @iter: The ring buffer iterator
2278 * @ts: The time stamp of the event read.
2280 * This reads the next event in the ring buffer and increments the iterator.
2282 struct ring_buffer_event *
2283 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2285 struct ring_buffer_event *event;
2286 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2287 unsigned long flags;
2289 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2290 event = rb_iter_peek(iter, ts);
2294 rb_advance_iter(iter);
2296 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2300 EXPORT_SYMBOL_GPL(ring_buffer_read);
2303 * ring_buffer_size - return the size of the ring buffer (in bytes)
2304 * @buffer: The ring buffer.
2306 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2308 return BUF_PAGE_SIZE * buffer->pages;
2310 EXPORT_SYMBOL_GPL(ring_buffer_size);
2313 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2315 cpu_buffer->head_page
2316 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2317 local_set(&cpu_buffer->head_page->write, 0);
2318 local_set(&cpu_buffer->head_page->page->commit, 0);
2320 cpu_buffer->head_page->read = 0;
2322 cpu_buffer->tail_page = cpu_buffer->head_page;
2323 cpu_buffer->commit_page = cpu_buffer->head_page;
2325 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2326 local_set(&cpu_buffer->reader_page->write, 0);
2327 local_set(&cpu_buffer->reader_page->page->commit, 0);
2328 cpu_buffer->reader_page->read = 0;
2330 cpu_buffer->overrun = 0;
2331 cpu_buffer->entries = 0;
2333 cpu_buffer->write_stamp = 0;
2334 cpu_buffer->read_stamp = 0;
2338 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2339 * @buffer: The ring buffer to reset a per cpu buffer of
2340 * @cpu: The CPU buffer to be reset
2342 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2344 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2345 unsigned long flags;
2347 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2350 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2352 __raw_spin_lock(&cpu_buffer->lock);
2354 rb_reset_cpu(cpu_buffer);
2356 __raw_spin_unlock(&cpu_buffer->lock);
2358 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2360 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2363 * ring_buffer_reset - reset a ring buffer
2364 * @buffer: The ring buffer to reset all cpu buffers
2366 void ring_buffer_reset(struct ring_buffer *buffer)
2370 for_each_buffer_cpu(buffer, cpu)
2371 ring_buffer_reset_cpu(buffer, cpu);
2373 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2376 * rind_buffer_empty - is the ring buffer empty?
2377 * @buffer: The ring buffer to test
2379 int ring_buffer_empty(struct ring_buffer *buffer)
2381 struct ring_buffer_per_cpu *cpu_buffer;
2384 /* yes this is racy, but if you don't like the race, lock the buffer */
2385 for_each_buffer_cpu(buffer, cpu) {
2386 cpu_buffer = buffer->buffers[cpu];
2387 if (!rb_per_cpu_empty(cpu_buffer))
2393 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2396 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2397 * @buffer: The ring buffer
2398 * @cpu: The CPU buffer to test
2400 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2402 struct ring_buffer_per_cpu *cpu_buffer;
2405 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2408 cpu_buffer = buffer->buffers[cpu];
2409 ret = rb_per_cpu_empty(cpu_buffer);
2414 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2417 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2418 * @buffer_a: One buffer to swap with
2419 * @buffer_b: The other buffer to swap with
2421 * This function is useful for tracers that want to take a "snapshot"
2422 * of a CPU buffer and has another back up buffer lying around.
2423 * it is expected that the tracer handles the cpu buffer not being
2424 * used at the moment.
2426 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2427 struct ring_buffer *buffer_b, int cpu)
2429 struct ring_buffer_per_cpu *cpu_buffer_a;
2430 struct ring_buffer_per_cpu *cpu_buffer_b;
2433 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2434 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2437 /* At least make sure the two buffers are somewhat the same */
2438 if (buffer_a->pages != buffer_b->pages)
2443 if (ring_buffer_flags != RB_BUFFERS_ON)
2446 if (atomic_read(&buffer_a->record_disabled))
2449 if (atomic_read(&buffer_b->record_disabled))
2452 cpu_buffer_a = buffer_a->buffers[cpu];
2453 cpu_buffer_b = buffer_b->buffers[cpu];
2455 if (atomic_read(&cpu_buffer_a->record_disabled))
2458 if (atomic_read(&cpu_buffer_b->record_disabled))
2462 * We can't do a synchronize_sched here because this
2463 * function can be called in atomic context.
2464 * Normally this will be called from the same CPU as cpu.
2465 * If not it's up to the caller to protect this.
2467 atomic_inc(&cpu_buffer_a->record_disabled);
2468 atomic_inc(&cpu_buffer_b->record_disabled);
2470 buffer_a->buffers[cpu] = cpu_buffer_b;
2471 buffer_b->buffers[cpu] = cpu_buffer_a;
2473 cpu_buffer_b->buffer = buffer_a;
2474 cpu_buffer_a->buffer = buffer_b;
2476 atomic_dec(&cpu_buffer_a->record_disabled);
2477 atomic_dec(&cpu_buffer_b->record_disabled);
2483 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2485 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2486 struct buffer_data_page *bpage,
2487 unsigned int offset)
2489 struct ring_buffer_event *event;
2492 __raw_spin_lock(&cpu_buffer->lock);
2493 for (head = offset; head < local_read(&bpage->commit);
2494 head += rb_event_length(event)) {
2496 event = __rb_data_page_index(bpage, head);
2497 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2499 /* Only count data entries */
2500 if (event->type != RINGBUF_TYPE_DATA)
2502 cpu_buffer->entries--;
2504 __raw_spin_unlock(&cpu_buffer->lock);
2508 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2509 * @buffer: the buffer to allocate for.
2511 * This function is used in conjunction with ring_buffer_read_page.
2512 * When reading a full page from the ring buffer, these functions
2513 * can be used to speed up the process. The calling function should
2514 * allocate a few pages first with this function. Then when it
2515 * needs to get pages from the ring buffer, it passes the result
2516 * of this function into ring_buffer_read_page, which will swap
2517 * the page that was allocated, with the read page of the buffer.
2520 * The page allocated, or NULL on error.
2522 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2524 struct buffer_data_page *bpage;
2527 addr = __get_free_page(GFP_KERNEL);
2531 bpage = (void *)addr;
2533 rb_init_page(bpage);
2539 * ring_buffer_free_read_page - free an allocated read page
2540 * @buffer: the buffer the page was allocate for
2541 * @data: the page to free
2543 * Free a page allocated from ring_buffer_alloc_read_page.
2545 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2547 free_page((unsigned long)data);
2551 * ring_buffer_read_page - extract a page from the ring buffer
2552 * @buffer: buffer to extract from
2553 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2554 * @len: amount to extract
2555 * @cpu: the cpu of the buffer to extract
2556 * @full: should the extraction only happen when the page is full.
2558 * This function will pull out a page from the ring buffer and consume it.
2559 * @data_page must be the address of the variable that was returned
2560 * from ring_buffer_alloc_read_page. This is because the page might be used
2561 * to swap with a page in the ring buffer.
2564 * rpage = ring_buffer_alloc_read_page(buffer);
2567 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2569 * process_page(rpage, ret);
2571 * When @full is set, the function will not return true unless
2572 * the writer is off the reader page.
2574 * Note: it is up to the calling functions to handle sleeps and wakeups.
2575 * The ring buffer can be used anywhere in the kernel and can not
2576 * blindly call wake_up. The layer that uses the ring buffer must be
2577 * responsible for that.
2580 * >=0 if data has been transferred, returns the offset of consumed data.
2581 * <0 if no data has been transferred.
2583 int ring_buffer_read_page(struct ring_buffer *buffer,
2584 void **data_page, size_t len, int cpu, int full)
2586 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2587 struct ring_buffer_event *event;
2588 struct buffer_data_page *bpage;
2589 struct buffer_page *reader;
2590 unsigned long flags;
2591 unsigned int commit;
2596 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2600 * If len is not big enough to hold the page header, then
2601 * we can not copy anything.
2603 if (len <= BUF_PAGE_HDR_SIZE)
2606 len -= BUF_PAGE_HDR_SIZE;
2615 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2617 reader = rb_get_reader_page(cpu_buffer);
2621 event = rb_reader_event(cpu_buffer);
2623 read = reader->read;
2624 commit = rb_page_commit(reader);
2627 * If this page has been partially read or
2628 * if len is not big enough to read the rest of the page or
2629 * a writer is still on the page, then
2630 * we must copy the data from the page to the buffer.
2631 * Otherwise, we can simply swap the page with the one passed in.
2633 if (read || (len < (commit - read)) ||
2634 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2635 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2636 unsigned int rpos = read;
2637 unsigned int pos = 0;
2643 if (len > (commit - read))
2644 len = (commit - read);
2646 size = rb_event_length(event);
2651 /* save the current timestamp, since the user will need it */
2652 save_timestamp = cpu_buffer->read_stamp;
2654 /* Need to copy one event at a time */
2656 memcpy(bpage->data + pos, rpage->data + rpos, size);
2660 rb_advance_reader(cpu_buffer);
2661 rpos = reader->read;
2664 event = rb_reader_event(cpu_buffer);
2665 size = rb_event_length(event);
2666 } while (len > size);
2669 local_set(&bpage->commit, pos);
2670 bpage->time_stamp = save_timestamp;
2672 /* we copied everything to the beginning */
2675 /* swap the pages */
2676 rb_init_page(bpage);
2677 bpage = reader->page;
2678 reader->page = *data_page;
2679 local_set(&reader->write, 0);
2683 /* update the entry counter */
2684 rb_remove_entries(cpu_buffer, bpage, read);
2689 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2696 rb_simple_read(struct file *filp, char __user *ubuf,
2697 size_t cnt, loff_t *ppos)
2699 unsigned long *p = filp->private_data;
2703 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2704 r = sprintf(buf, "permanently disabled\n");
2706 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2708 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2712 rb_simple_write(struct file *filp, const char __user *ubuf,
2713 size_t cnt, loff_t *ppos)
2715 unsigned long *p = filp->private_data;
2720 if (cnt >= sizeof(buf))
2723 if (copy_from_user(&buf, ubuf, cnt))
2728 ret = strict_strtoul(buf, 10, &val);
2733 set_bit(RB_BUFFERS_ON_BIT, p);
2735 clear_bit(RB_BUFFERS_ON_BIT, p);
2742 static const struct file_operations rb_simple_fops = {
2743 .open = tracing_open_generic,
2744 .read = rb_simple_read,
2745 .write = rb_simple_write,
2749 static __init int rb_init_debugfs(void)
2751 struct dentry *d_tracer;
2752 struct dentry *entry;
2754 d_tracer = tracing_init_dentry();
2756 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2757 &ring_buffer_flags, &rb_simple_fops);
2759 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2764 fs_initcall(rb_init_debugfs);
2766 #ifdef CONFIG_HOTPLUG_CPU
2767 static int __cpuinit rb_cpu_notify(struct notifier_block *self,
2768 unsigned long action, void *hcpu)
2770 struct ring_buffer *buffer =
2771 container_of(self, struct ring_buffer, cpu_notify);
2772 long cpu = (long)hcpu;
2775 case CPU_UP_PREPARE:
2776 case CPU_UP_PREPARE_FROZEN:
2777 if (cpu_isset(cpu, *buffer->cpumask))
2780 buffer->buffers[cpu] =
2781 rb_allocate_cpu_buffer(buffer, cpu);
2782 if (!buffer->buffers[cpu]) {
2783 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2788 cpu_set(cpu, *buffer->cpumask);
2790 case CPU_DOWN_PREPARE:
2791 case CPU_DOWN_PREPARE_FROZEN:
2794 * If we were to free the buffer, then the user would
2795 * lose any trace that was in the buffer.