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