blob: f935bd5ec3e839e9735e4efbc4072fa93348ac88 [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/init.h>
17#include <linux/hash.h>
18#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040019#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040020#include <linux/fs.h>
21
Steven Rostedt182e9f52008-11-03 23:15:56 -050022#include "trace.h"
23
Steven Rostedt033601a2008-11-21 12:41:55 -050024/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040025 * The ring buffer is made up of a list of pages. A separate list of pages is
26 * allocated for each CPU. A writer may only write to a buffer that is
27 * associated with the CPU it is currently executing on. A reader may read
28 * from any per cpu buffer.
29 *
30 * The reader is special. For each per cpu buffer, the reader has its own
31 * reader page. When a reader has read the entire reader page, this reader
32 * page is swapped with another page in the ring buffer.
33 *
34 * Now, as long as the writer is off the reader page, the reader can do what
35 * ever it wants with that page. The writer will never write to that page
36 * again (as long as it is out of the ring buffer).
37 *
38 * Here's some silly ASCII art.
39 *
40 * +------+
41 * |reader| RING BUFFER
42 * |page |
43 * +------+ +---+ +---+ +---+
44 * | |-->| |-->| |
45 * +---+ +---+ +---+
46 * ^ |
47 * | |
48 * +---------------+
49 *
50 *
51 * +------+
52 * |reader| RING BUFFER
53 * |page |------------------v
54 * +------+ +---+ +---+ +---+
55 * | |-->| |-->| |
56 * +---+ +---+ +---+
57 * ^ |
58 * | |
59 * +---------------+
60 *
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |------------------v
65 * +------+ +---+ +---+ +---+
66 * ^ | |-->| |-->| |
67 * | +---+ +---+ +---+
68 * | |
69 * | |
70 * +------------------------------+
71 *
72 *
73 * +------+
74 * |buffer| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * ^ | | | |-->| |
78 * | New +---+ +---+ +---+
79 * | Reader------^ |
80 * | page |
81 * +------------------------------+
82 *
83 *
84 * After we make this swap, the reader can hand this page off to the splice
85 * code and be done with it. It can even allocate a new page if it needs to
86 * and swap that into the ring buffer.
87 *
88 * We will be using cmpxchg soon to make all this lockless.
89 *
90 */
91
92/*
Steven Rostedt033601a2008-11-21 12:41:55 -050093 * A fast way to enable or disable all ring buffers is to
94 * call tracing_on or tracing_off. Turning off the ring buffers
95 * prevents all ring buffers from being recorded to.
96 * Turning this switch on, makes it OK to write to the
97 * ring buffer, if the ring buffer is enabled itself.
98 *
99 * There's three layers that must be on in order to write
100 * to the ring buffer.
101 *
102 * 1) This global flag must be set.
103 * 2) The ring buffer must be enabled for recording.
104 * 3) The per cpu buffer must be enabled for recording.
105 *
106 * In case of an anomaly, this global flag has a bit set that
107 * will permantly disable all ring buffers.
108 */
109
110/*
111 * Global flag to disable all recording to ring buffers
112 * This has two bits: ON, DISABLED
113 *
114 * ON DISABLED
115 * ---- ----------
116 * 0 0 : ring buffers are off
117 * 1 0 : ring buffers are on
118 * X 1 : ring buffers are permanently disabled
119 */
120
121enum {
122 RB_BUFFERS_ON_BIT = 0,
123 RB_BUFFERS_DISABLED_BIT = 1,
124};
125
126enum {
127 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
128 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
129};
130
Hannes Eder5e398412009-02-10 19:44:34 +0100131static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500132
Steven Rostedt474d32b2009-03-03 19:51:40 -0500133#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
134
Steven Rostedta3583242008-11-11 15:01:42 -0500135/**
136 * tracing_on - enable all tracing buffers
137 *
138 * This function enables all tracing buffers that may have been
139 * disabled with tracing_off.
140 */
141void tracing_on(void)
142{
Steven Rostedt033601a2008-11-21 12:41:55 -0500143 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500144}
Robert Richterc4f50182008-12-11 16:49:22 +0100145EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500146
147/**
148 * tracing_off - turn off all tracing buffers
149 *
150 * This function stops all tracing buffers from recording data.
151 * It does not disable any overhead the tracers themselves may
152 * be causing. This function simply causes all recording to
153 * the ring buffers to fail.
154 */
155void tracing_off(void)
156{
Steven Rostedt033601a2008-11-21 12:41:55 -0500157 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
158}
Robert Richterc4f50182008-12-11 16:49:22 +0100159EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500160
161/**
162 * tracing_off_permanent - permanently disable ring buffers
163 *
164 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500165 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500166 */
167void tracing_off_permanent(void)
168{
169 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500170}
171
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500172/**
173 * tracing_is_on - show state of ring buffers enabled
174 */
175int tracing_is_on(void)
176{
177 return ring_buffer_flags == RB_BUFFERS_ON;
178}
179EXPORT_SYMBOL_GPL(tracing_is_on);
180
Ingo Molnard06bbd62008-11-12 10:11:37 +0100181#include "trace.h"
182
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500183#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800184#define RB_ALIGNMENT 4U
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400185#define RB_MAX_SMALL_DATA 28
186
187enum {
188 RB_LEN_TIME_EXTEND = 8,
189 RB_LEN_TIME_STAMP = 16,
190};
191
Tom Zanussi2d622712009-03-22 03:30:49 -0500192static inline int rb_null_event(struct ring_buffer_event *event)
193{
194 return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
195}
196
197static inline int rb_discarded_event(struct ring_buffer_event *event)
198{
199 return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
200}
201
202static void rb_event_set_padding(struct ring_buffer_event *event)
203{
204 event->type = RINGBUF_TYPE_PADDING;
205 event->time_delta = 0;
206}
207
Tom Zanussi2d622712009-03-22 03:30:49 -0500208static unsigned
209rb_event_data_length(struct ring_buffer_event *event)
210{
211 unsigned length;
212
213 if (event->len)
214 length = event->len * RB_ALIGNMENT;
215 else
216 length = event->array[0];
217 return length + RB_EVNT_HDR_SIZE;
218}
219
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400220/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800221static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400222rb_event_length(struct ring_buffer_event *event)
223{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400224 switch (event->type) {
225 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500226 if (rb_null_event(event))
227 /* undefined */
228 return -1;
229 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400230
231 case RINGBUF_TYPE_TIME_EXTEND:
232 return RB_LEN_TIME_EXTEND;
233
234 case RINGBUF_TYPE_TIME_STAMP:
235 return RB_LEN_TIME_STAMP;
236
237 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500238 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400239 default:
240 BUG();
241 }
242 /* not hit */
243 return 0;
244}
245
246/**
247 * ring_buffer_event_length - return the length of the event
248 * @event: the event to get the length of
249 */
250unsigned ring_buffer_event_length(struct ring_buffer_event *event)
251{
Robert Richter465634a2009-01-07 15:32:11 +0100252 unsigned length = rb_event_length(event);
253 if (event->type != RINGBUF_TYPE_DATA)
254 return length;
255 length -= RB_EVNT_HDR_SIZE;
256 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
257 length -= sizeof(event->array[0]);
258 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400259}
Robert Richterc4f50182008-12-11 16:49:22 +0100260EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400261
262/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800263static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400264rb_event_data(struct ring_buffer_event *event)
265{
266 BUG_ON(event->type != RINGBUF_TYPE_DATA);
267 /* If length is in len field, then array[0] has the data */
268 if (event->len)
269 return (void *)&event->array[0];
270 /* Otherwise length is in array[0] and array[1] has the data */
271 return (void *)&event->array[1];
272}
273
274/**
275 * ring_buffer_event_data - return the data of the event
276 * @event: the event to get the data from
277 */
278void *ring_buffer_event_data(struct ring_buffer_event *event)
279{
280 return rb_event_data(event);
281}
Robert Richterc4f50182008-12-11 16:49:22 +0100282EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400283
284#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030285 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400286
287#define TS_SHIFT 27
288#define TS_MASK ((1ULL << TS_SHIFT) - 1)
289#define TS_DELTA_TEST (~TS_MASK)
290
Steven Rostedtabc9b562008-12-02 15:34:06 -0500291struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400292 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500293 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500294 unsigned char data[]; /* data of buffer page */
295};
296
297struct buffer_page {
298 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400299 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400300 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500301 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400302};
303
Steven Rostedt044fa782008-12-02 23:50:03 -0500304static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500305{
Steven Rostedt044fa782008-12-02 23:50:03 -0500306 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500307}
308
Steven Rostedt474d32b2009-03-03 19:51:40 -0500309/**
310 * ring_buffer_page_len - the size of data on the page.
311 * @page: The page to read
312 *
313 * Returns the amount of data on the page, including buffer page header.
314 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500315size_t ring_buffer_page_len(void *page)
316{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500317 return local_read(&((struct buffer_data_page *)page)->commit)
318 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500319}
320
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400321/*
Steven Rostedted568292008-09-29 23:02:40 -0400322 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
323 * this issue out.
324 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800325static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400326{
Andrew Morton34a148b2009-01-09 12:27:09 -0800327 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400328 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400329}
330
331/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400332 * We need to fit the time_stamp delta into 27 bits.
333 */
334static inline int test_time_stamp(u64 delta)
335{
336 if (delta & TS_DELTA_TEST)
337 return 1;
338 return 0;
339}
340
Steven Rostedt474d32b2009-03-03 19:51:40 -0500341#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400342
343/*
344 * head_page == tail_page && head == tail then buffer is empty.
345 */
346struct ring_buffer_per_cpu {
347 int cpu;
348 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100349 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500350 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400351 struct lock_class_key lock_key;
352 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400353 struct buffer_page *head_page; /* read from head */
354 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500355 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400356 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400357 unsigned long overrun;
358 unsigned long entries;
359 u64 write_stamp;
360 u64 read_stamp;
361 atomic_t record_disabled;
362};
363
364struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400365 unsigned pages;
366 unsigned flags;
367 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400368 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200369 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400370
371 struct mutex mutex;
372
373 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400374
Steven Rostedt59222ef2009-03-12 11:46:03 -0400375#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400376 struct notifier_block cpu_notify;
377#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400378 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400379};
380
381struct ring_buffer_iter {
382 struct ring_buffer_per_cpu *cpu_buffer;
383 unsigned long head;
384 struct buffer_page *head_page;
385 u64 read_stamp;
386};
387
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500388/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400389#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500390 ({ \
391 int _____ret = unlikely(cond); \
392 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400393 atomic_inc(&buffer->record_disabled); \
394 WARN_ON(1); \
395 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500396 _____ret; \
397 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500398
Steven Rostedt37886f62009-03-17 17:22:06 -0400399/* Up this if you want to test the TIME_EXTENTS and normalization */
400#define DEBUG_SHIFT 0
401
402u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
403{
404 u64 time;
405
406 preempt_disable_notrace();
407 /* shift to debug/test normalization and TIME_EXTENTS */
408 time = buffer->clock() << DEBUG_SHIFT;
409 preempt_enable_no_resched_notrace();
410
411 return time;
412}
413EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
414
415void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
416 int cpu, u64 *ts)
417{
418 /* Just stupid testing the normalize function and deltas */
419 *ts >>= DEBUG_SHIFT;
420}
421EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
422
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400423/**
424 * check_pages - integrity check of buffer pages
425 * @cpu_buffer: CPU buffer with pages to test
426 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500427 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400428 * been corrupted.
429 */
430static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
431{
432 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500433 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400434
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500435 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
436 return -1;
437 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
438 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400439
Steven Rostedt044fa782008-12-02 23:50:03 -0500440 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500441 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500442 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500443 return -1;
444 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500445 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500446 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400447 }
448
449 return 0;
450}
451
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400452static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
453 unsigned nr_pages)
454{
455 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500456 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400457 unsigned long addr;
458 LIST_HEAD(pages);
459 unsigned i;
460
461 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500462 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400463 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500464 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400465 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500466 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400467
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400468 addr = __get_free_page(GFP_KERNEL);
469 if (!addr)
470 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500471 bpage->page = (void *)addr;
472 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400473 }
474
475 list_splice(&pages, head);
476
477 rb_check_pages(cpu_buffer);
478
479 return 0;
480
481 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500482 list_for_each_entry_safe(bpage, tmp, &pages, list) {
483 list_del_init(&bpage->list);
484 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400485 }
486 return -ENOMEM;
487}
488
489static struct ring_buffer_per_cpu *
490rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
491{
492 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500493 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400494 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400495 int ret;
496
497 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
498 GFP_KERNEL, cpu_to_node(cpu));
499 if (!cpu_buffer)
500 return NULL;
501
502 cpu_buffer->cpu = cpu;
503 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100504 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500505 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400506 INIT_LIST_HEAD(&cpu_buffer->pages);
507
Steven Rostedt044fa782008-12-02 23:50:03 -0500508 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400509 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500510 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400511 goto fail_free_buffer;
512
Steven Rostedt044fa782008-12-02 23:50:03 -0500513 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400514 addr = __get_free_page(GFP_KERNEL);
515 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400516 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500517 bpage->page = (void *)addr;
518 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400519
Steven Rostedtd7690412008-10-01 00:29:53 -0400520 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400521
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400522 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
523 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400524 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400525
526 cpu_buffer->head_page
527 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400528 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400529
530 return cpu_buffer;
531
Steven Rostedtd7690412008-10-01 00:29:53 -0400532 fail_free_reader:
533 free_buffer_page(cpu_buffer->reader_page);
534
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400535 fail_free_buffer:
536 kfree(cpu_buffer);
537 return NULL;
538}
539
540static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
541{
542 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500543 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400544
Steven Rostedtd7690412008-10-01 00:29:53 -0400545 free_buffer_page(cpu_buffer->reader_page);
546
Steven Rostedt044fa782008-12-02 23:50:03 -0500547 list_for_each_entry_safe(bpage, tmp, head, list) {
548 list_del_init(&bpage->list);
549 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400550 }
551 kfree(cpu_buffer);
552}
553
Steven Rostedta7b13742008-09-29 23:02:39 -0400554/*
555 * Causes compile errors if the struct buffer_page gets bigger
556 * than the struct page.
557 */
558extern int ring_buffer_page_too_big(void);
559
Steven Rostedt59222ef2009-03-12 11:46:03 -0400560#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100561static int rb_cpu_notify(struct notifier_block *self,
562 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400563#endif
564
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400565/**
566 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100567 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400568 * @flags: attributes to set for the ring buffer.
569 *
570 * Currently the only flag that is available is the RB_FL_OVERWRITE
571 * flag. This flag means that the buffer will overwrite old data
572 * when the buffer wraps. If this flag is not set, the buffer will
573 * drop data when the tail hits the head.
574 */
575struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
576{
577 struct ring_buffer *buffer;
578 int bsize;
579 int cpu;
580
Steven Rostedta7b13742008-09-29 23:02:39 -0400581 /* Paranoid! Optimizes out when all is well */
582 if (sizeof(struct buffer_page) > sizeof(struct page))
583 ring_buffer_page_too_big();
584
585
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400586 /* keep it in its own cache line */
587 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
588 GFP_KERNEL);
589 if (!buffer)
590 return NULL;
591
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030592 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
593 goto fail_free_buffer;
594
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400595 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
596 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400597 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400598
599 /* need at least two pages */
600 if (buffer->pages == 1)
601 buffer->pages++;
602
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100603 /*
604 * In case of non-hotplug cpu, if the ring-buffer is allocated
605 * in early initcall, it will not be notified of secondary cpus.
606 * In that off case, we need to allocate for all possible cpus.
607 */
608#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400609 get_online_cpus();
610 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100611#else
612 cpumask_copy(buffer->cpumask, cpu_possible_mask);
613#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400614 buffer->cpus = nr_cpu_ids;
615
616 bsize = sizeof(void *) * nr_cpu_ids;
617 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
618 GFP_KERNEL);
619 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030620 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400621
622 for_each_buffer_cpu(buffer, cpu) {
623 buffer->buffers[cpu] =
624 rb_allocate_cpu_buffer(buffer, cpu);
625 if (!buffer->buffers[cpu])
626 goto fail_free_buffers;
627 }
628
Steven Rostedt59222ef2009-03-12 11:46:03 -0400629#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400630 buffer->cpu_notify.notifier_call = rb_cpu_notify;
631 buffer->cpu_notify.priority = 0;
632 register_cpu_notifier(&buffer->cpu_notify);
633#endif
634
635 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400636 mutex_init(&buffer->mutex);
637
638 return buffer;
639
640 fail_free_buffers:
641 for_each_buffer_cpu(buffer, cpu) {
642 if (buffer->buffers[cpu])
643 rb_free_cpu_buffer(buffer->buffers[cpu]);
644 }
645 kfree(buffer->buffers);
646
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030647 fail_free_cpumask:
648 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400649 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030650
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400651 fail_free_buffer:
652 kfree(buffer);
653 return NULL;
654}
Robert Richterc4f50182008-12-11 16:49:22 +0100655EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400656
657/**
658 * ring_buffer_free - free a ring buffer.
659 * @buffer: the buffer to free.
660 */
661void
662ring_buffer_free(struct ring_buffer *buffer)
663{
664 int cpu;
665
Steven Rostedt554f7862009-03-11 22:00:13 -0400666 get_online_cpus();
667
Steven Rostedt59222ef2009-03-12 11:46:03 -0400668#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400669 unregister_cpu_notifier(&buffer->cpu_notify);
670#endif
671
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400672 for_each_buffer_cpu(buffer, cpu)
673 rb_free_cpu_buffer(buffer->buffers[cpu]);
674
Steven Rostedt554f7862009-03-11 22:00:13 -0400675 put_online_cpus();
676
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030677 free_cpumask_var(buffer->cpumask);
678
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400679 kfree(buffer);
680}
Robert Richterc4f50182008-12-11 16:49:22 +0100681EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400682
Steven Rostedt37886f62009-03-17 17:22:06 -0400683void ring_buffer_set_clock(struct ring_buffer *buffer,
684 u64 (*clock)(void))
685{
686 buffer->clock = clock;
687}
688
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400689static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
690
691static void
692rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
693{
Steven Rostedt044fa782008-12-02 23:50:03 -0500694 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400695 struct list_head *p;
696 unsigned i;
697
698 atomic_inc(&cpu_buffer->record_disabled);
699 synchronize_sched();
700
701 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500702 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
703 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400704 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500705 bpage = list_entry(p, struct buffer_page, list);
706 list_del_init(&bpage->list);
707 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400708 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500709 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
710 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400711
712 rb_reset_cpu(cpu_buffer);
713
714 rb_check_pages(cpu_buffer);
715
716 atomic_dec(&cpu_buffer->record_disabled);
717
718}
719
720static void
721rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
722 struct list_head *pages, unsigned nr_pages)
723{
Steven Rostedt044fa782008-12-02 23:50:03 -0500724 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400725 struct list_head *p;
726 unsigned i;
727
728 atomic_inc(&cpu_buffer->record_disabled);
729 synchronize_sched();
730
731 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500732 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
733 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400734 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500735 bpage = list_entry(p, struct buffer_page, list);
736 list_del_init(&bpage->list);
737 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400738 }
739 rb_reset_cpu(cpu_buffer);
740
741 rb_check_pages(cpu_buffer);
742
743 atomic_dec(&cpu_buffer->record_disabled);
744}
745
746/**
747 * ring_buffer_resize - resize the ring buffer
748 * @buffer: the buffer to resize.
749 * @size: the new size.
750 *
751 * The tracer is responsible for making sure that the buffer is
752 * not being used while changing the size.
753 * Note: We may be able to change the above requirement by using
754 * RCU synchronizations.
755 *
756 * Minimum size is 2 * BUF_PAGE_SIZE.
757 *
758 * Returns -1 on failure.
759 */
760int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
761{
762 struct ring_buffer_per_cpu *cpu_buffer;
763 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500764 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400765 unsigned long buffer_size;
766 unsigned long addr;
767 LIST_HEAD(pages);
768 int i, cpu;
769
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100770 /*
771 * Always succeed at resizing a non-existent buffer:
772 */
773 if (!buffer)
774 return size;
775
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400776 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
777 size *= BUF_PAGE_SIZE;
778 buffer_size = buffer->pages * BUF_PAGE_SIZE;
779
780 /* we need a minimum of two pages */
781 if (size < BUF_PAGE_SIZE * 2)
782 size = BUF_PAGE_SIZE * 2;
783
784 if (size == buffer_size)
785 return size;
786
787 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400788 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400789
790 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
791
792 if (size < buffer_size) {
793
794 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400795 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
796 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400797
798 rm_pages = buffer->pages - nr_pages;
799
800 for_each_buffer_cpu(buffer, cpu) {
801 cpu_buffer = buffer->buffers[cpu];
802 rb_remove_pages(cpu_buffer, rm_pages);
803 }
804 goto out;
805 }
806
807 /*
808 * This is a bit more difficult. We only want to add pages
809 * when we can allocate enough for all CPUs. We do this
810 * by allocating all the pages and storing them on a local
811 * link list. If we succeed in our allocation, then we
812 * add these pages to the cpu_buffers. Otherwise we just free
813 * them all and return -ENOMEM;
814 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400815 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
816 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500817
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400818 new_pages = nr_pages - buffer->pages;
819
820 for_each_buffer_cpu(buffer, cpu) {
821 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500822 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400823 cache_line_size()),
824 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500825 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400826 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500827 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400828 addr = __get_free_page(GFP_KERNEL);
829 if (!addr)
830 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500831 bpage->page = (void *)addr;
832 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400833 }
834 }
835
836 for_each_buffer_cpu(buffer, cpu) {
837 cpu_buffer = buffer->buffers[cpu];
838 rb_insert_pages(cpu_buffer, &pages, new_pages);
839 }
840
Steven Rostedt554f7862009-03-11 22:00:13 -0400841 if (RB_WARN_ON(buffer, !list_empty(&pages)))
842 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400843
844 out:
845 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400846 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400847 mutex_unlock(&buffer->mutex);
848
849 return size;
850
851 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500852 list_for_each_entry_safe(bpage, tmp, &pages, list) {
853 list_del_init(&bpage->list);
854 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400855 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400856 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100857 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400858 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400859
860 /*
861 * Something went totally wrong, and we are too paranoid
862 * to even clean up the mess.
863 */
864 out_fail:
865 put_online_cpus();
866 mutex_unlock(&buffer->mutex);
867 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400868}
Robert Richterc4f50182008-12-11 16:49:22 +0100869EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400870
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500871static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500872__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500873{
Steven Rostedt044fa782008-12-02 23:50:03 -0500874 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500875}
876
Steven Rostedt044fa782008-12-02 23:50:03 -0500877static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400878{
Steven Rostedt044fa782008-12-02 23:50:03 -0500879 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400880}
881
882static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400883rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400884{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400885 return __rb_page_index(cpu_buffer->reader_page,
886 cpu_buffer->reader_page->read);
887}
888
889static inline struct ring_buffer_event *
890rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
891{
892 return __rb_page_index(cpu_buffer->head_page,
893 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400894}
895
896static inline struct ring_buffer_event *
897rb_iter_head_event(struct ring_buffer_iter *iter)
898{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400899 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400900}
901
Steven Rostedtbf41a152008-10-04 02:00:59 -0400902static inline unsigned rb_page_write(struct buffer_page *bpage)
903{
904 return local_read(&bpage->write);
905}
906
907static inline unsigned rb_page_commit(struct buffer_page *bpage)
908{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500909 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400910}
911
912/* Size is determined by what has been commited */
913static inline unsigned rb_page_size(struct buffer_page *bpage)
914{
915 return rb_page_commit(bpage);
916}
917
918static inline unsigned
919rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
920{
921 return rb_page_commit(cpu_buffer->commit_page);
922}
923
924static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
925{
926 return rb_page_commit(cpu_buffer->head_page);
927}
928
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400929/*
930 * When the tail hits the head and the buffer is in overwrite mode,
931 * the head jumps to the next page and all content on the previous
932 * page is discarded. But before doing so, we update the overrun
933 * variable of the buffer.
934 */
935static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
936{
937 struct ring_buffer_event *event;
938 unsigned long head;
939
940 for (head = 0; head < rb_head_size(cpu_buffer);
941 head += rb_event_length(event)) {
942
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400943 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500944 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
945 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946 /* Only count data entries */
947 if (event->type != RINGBUF_TYPE_DATA)
948 continue;
949 cpu_buffer->overrun++;
950 cpu_buffer->entries--;
951 }
952}
953
954static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500955 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956{
Steven Rostedt044fa782008-12-02 23:50:03 -0500957 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400958
959 if (p == &cpu_buffer->pages)
960 p = p->next;
961
Steven Rostedt044fa782008-12-02 23:50:03 -0500962 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400963}
964
Steven Rostedtbf41a152008-10-04 02:00:59 -0400965static inline unsigned
966rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400967{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400968 unsigned long addr = (unsigned long)event;
969
970 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400971}
972
Andrew Morton34a148b2009-01-09 12:27:09 -0800973static int
Steven Rostedtbf41a152008-10-04 02:00:59 -0400974rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
975 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400976{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400977 unsigned long addr = (unsigned long)event;
978 unsigned long index;
979
980 index = rb_event_index(event);
981 addr &= PAGE_MASK;
982
983 return cpu_buffer->commit_page->page == (void *)addr &&
984 rb_commit_index(cpu_buffer) == index;
985}
986
Andrew Morton34a148b2009-01-09 12:27:09 -0800987static void
Steven Rostedtbf41a152008-10-04 02:00:59 -0400988rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
989 struct ring_buffer_event *event)
990{
991 unsigned long addr = (unsigned long)event;
992 unsigned long index;
993
994 index = rb_event_index(event);
995 addr &= PAGE_MASK;
996
997 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500998 if (RB_WARN_ON(cpu_buffer,
999 cpu_buffer->commit_page == cpu_buffer->tail_page))
1000 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001001 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001002 cpu_buffer->commit_page->write;
1003 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001004 cpu_buffer->write_stamp =
1005 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001006 }
1007
1008 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001009 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001010}
1011
Andrew Morton34a148b2009-01-09 12:27:09 -08001012static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001013rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1014{
1015 /*
1016 * We only race with interrupts and NMIs on this CPU.
1017 * If we own the commit event, then we can commit
1018 * all others that interrupted us, since the interruptions
1019 * are in stack format (they finish before they come
1020 * back to us). This allows us to do a simple loop to
1021 * assign the commit to the tail.
1022 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001023 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001024 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001025 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001026 cpu_buffer->commit_page->write;
1027 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001028 cpu_buffer->write_stamp =
1029 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001030 /* add barrier to keep gcc from optimizing too much */
1031 barrier();
1032 }
1033 while (rb_commit_index(cpu_buffer) !=
1034 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001035 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001036 cpu_buffer->commit_page->write;
1037 barrier();
1038 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001039
1040 /* again, keep gcc from optimizing */
1041 barrier();
1042
1043 /*
1044 * If an interrupt came in just after the first while loop
1045 * and pushed the tail page forward, we will be left with
1046 * a dangling commit that will never go forward.
1047 */
1048 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1049 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001050}
1051
Steven Rostedtd7690412008-10-01 00:29:53 -04001052static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001054 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001055 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001056}
1057
Andrew Morton34a148b2009-01-09 12:27:09 -08001058static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001059{
1060 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1061
1062 /*
1063 * The iterator could be on the reader page (it starts there).
1064 * But the head could have moved, since the reader was
1065 * found. Check for this case and assign the iterator
1066 * to the head page instead of next.
1067 */
1068 if (iter->head_page == cpu_buffer->reader_page)
1069 iter->head_page = cpu_buffer->head_page;
1070 else
1071 rb_inc_page(cpu_buffer, &iter->head_page);
1072
Steven Rostedtabc9b562008-12-02 15:34:06 -05001073 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001074 iter->head = 0;
1075}
1076
1077/**
1078 * ring_buffer_update_event - update event type and data
1079 * @event: the even to update
1080 * @type: the type of event
1081 * @length: the size of the event field in the ring buffer
1082 *
1083 * Update the type and data fields of the event. The length
1084 * is the actual size that is written to the ring buffer,
1085 * and with this, we can determine what to place into the
1086 * data field.
1087 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001088static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089rb_update_event(struct ring_buffer_event *event,
1090 unsigned type, unsigned length)
1091{
1092 event->type = type;
1093
1094 switch (type) {
1095
1096 case RINGBUF_TYPE_PADDING:
1097 break;
1098
1099 case RINGBUF_TYPE_TIME_EXTEND:
Andrew Morton67d34722009-01-09 12:27:09 -08001100 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001101 break;
1102
1103 case RINGBUF_TYPE_TIME_STAMP:
Andrew Morton67d34722009-01-09 12:27:09 -08001104 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001105 break;
1106
1107 case RINGBUF_TYPE_DATA:
1108 length -= RB_EVNT_HDR_SIZE;
1109 if (length > RB_MAX_SMALL_DATA) {
1110 event->len = 0;
1111 event->array[0] = length;
1112 } else
Andrew Morton67d34722009-01-09 12:27:09 -08001113 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001114 break;
1115 default:
1116 BUG();
1117 }
1118}
1119
Andrew Morton34a148b2009-01-09 12:27:09 -08001120static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121{
1122 struct ring_buffer_event event; /* Used only for sizeof array */
1123
1124 /* zero length can cause confusions */
1125 if (!length)
1126 length = 1;
1127
1128 if (length > RB_MAX_SMALL_DATA)
1129 length += sizeof(event.array[0]);
1130
1131 length += RB_EVNT_HDR_SIZE;
1132 length = ALIGN(length, RB_ALIGNMENT);
1133
1134 return length;
1135}
1136
1137static struct ring_buffer_event *
1138__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1139 unsigned type, unsigned long length, u64 *ts)
1140{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001141 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001142 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001143 struct ring_buffer *buffer = cpu_buffer->buffer;
1144 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001145 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001146 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147
Steven Rostedt98db8df2008-12-23 11:32:25 -05001148 commit_page = cpu_buffer->commit_page;
1149 /* we just need to protect against interrupts */
1150 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001151 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001152 write = local_add_return(length, &tail_page->write);
1153 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154
Steven Rostedtbf41a152008-10-04 02:00:59 -04001155 /* See if we shot pass the end of this buffer page */
1156 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001157 struct buffer_page *next_page = tail_page;
1158
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001159 local_irq_save(flags);
Steven Rostedt78d904b2009-02-05 18:43:07 -05001160 /*
Steven Rostedta81bd802009-02-06 01:45:16 -05001161 * Since the write to the buffer is still not
1162 * fully lockless, we must be careful with NMIs.
1163 * The locks in the writers are taken when a write
1164 * crosses to a new page. The locks protect against
1165 * races with the readers (this will soon be fixed
1166 * with a lockless solution).
1167 *
1168 * Because we can not protect against NMIs, and we
1169 * want to keep traces reentrant, we need to manage
1170 * what happens when we are in an NMI.
1171 *
Steven Rostedt78d904b2009-02-05 18:43:07 -05001172 * NMIs can happen after we take the lock.
1173 * If we are in an NMI, only take the lock
1174 * if it is not already taken. Otherwise
1175 * simply fail.
1176 */
Steven Rostedta81bd802009-02-06 01:45:16 -05001177 if (unlikely(in_nmi())) {
Steven Rostedt78d904b2009-02-05 18:43:07 -05001178 if (!__raw_spin_trylock(&cpu_buffer->lock))
Steven Rostedt45141d42009-02-12 13:19:48 -05001179 goto out_reset;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001180 } else
1181 __raw_spin_lock(&cpu_buffer->lock);
1182
1183 lock_taken = true;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001184
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001185 rb_inc_page(cpu_buffer, &next_page);
1186
Steven Rostedtd7690412008-10-01 00:29:53 -04001187 head_page = cpu_buffer->head_page;
1188 reader_page = cpu_buffer->reader_page;
1189
1190 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001191 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
Steven Rostedt45141d42009-02-12 13:19:48 -05001192 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001193
1194 /*
1195 * If for some reason, we had an interrupt storm that made
1196 * it all the way around the buffer, bail, and warn
1197 * about it.
1198 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001199 if (unlikely(next_page == commit_page)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001200 WARN_ON_ONCE(1);
Steven Rostedt45141d42009-02-12 13:19:48 -05001201 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001202 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001203
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001204 if (next_page == head_page) {
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001205 if (!(buffer->flags & RB_FL_OVERWRITE))
Steven Rostedt45141d42009-02-12 13:19:48 -05001206 goto out_reset;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001207
Steven Rostedtbf41a152008-10-04 02:00:59 -04001208 /* tail_page has not moved yet? */
1209 if (tail_page == cpu_buffer->tail_page) {
1210 /* count overflows */
1211 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001212
Steven Rostedtbf41a152008-10-04 02:00:59 -04001213 rb_inc_page(cpu_buffer, &head_page);
1214 cpu_buffer->head_page = head_page;
1215 cpu_buffer->head_page->read = 0;
1216 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001217 }
1218
Steven Rostedtbf41a152008-10-04 02:00:59 -04001219 /*
1220 * If the tail page is still the same as what we think
1221 * it is, then it is up to us to update the tail
1222 * pointer.
1223 */
1224 if (tail_page == cpu_buffer->tail_page) {
1225 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001226 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001227 cpu_buffer->tail_page = next_page;
1228
1229 /* reread the time stamp */
Steven Rostedt37886f62009-03-17 17:22:06 -04001230 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001231 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001232 }
1233
1234 /*
1235 * The actual tail page has moved forward.
1236 */
1237 if (tail < BUF_PAGE_SIZE) {
1238 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001239 event = __rb_page_index(tail_page, tail);
Tom Zanussi2d622712009-03-22 03:30:49 -05001240 rb_event_set_padding(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241 }
1242
Steven Rostedtbf41a152008-10-04 02:00:59 -04001243 if (tail <= BUF_PAGE_SIZE)
1244 /* Set the write back to the previous setting */
1245 local_set(&tail_page->write, tail);
1246
1247 /*
1248 * If this was a commit entry that failed,
1249 * increment that too
1250 */
1251 if (tail_page == cpu_buffer->commit_page &&
1252 tail == rb_commit_index(cpu_buffer)) {
1253 rb_set_commit_to_write(cpu_buffer);
1254 }
1255
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001256 __raw_spin_unlock(&cpu_buffer->lock);
1257 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001258
1259 /* fail and let the caller try again */
1260 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001261 }
1262
Steven Rostedtbf41a152008-10-04 02:00:59 -04001263 /* We reserved something on the buffer */
1264
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001265 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1266 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001267
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001268 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269 rb_update_event(event, type, length);
1270
Steven Rostedtbf41a152008-10-04 02:00:59 -04001271 /*
1272 * If this is a commit and the tail is zero, then update
1273 * this page's time stamp.
1274 */
1275 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001276 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001279
Steven Rostedt45141d42009-02-12 13:19:48 -05001280 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001281 /* reset write */
1282 if (tail <= BUF_PAGE_SIZE)
1283 local_set(&tail_page->write, tail);
1284
Steven Rostedt78d904b2009-02-05 18:43:07 -05001285 if (likely(lock_taken))
1286 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001287 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001288 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001289}
1290
1291static int
1292rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1293 u64 *ts, u64 *delta)
1294{
1295 struct ring_buffer_event *event;
1296 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001297 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001298
1299 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1300 printk(KERN_WARNING "Delta way too big! %llu"
1301 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001302 (unsigned long long)*delta,
1303 (unsigned long long)*ts,
1304 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001305 WARN_ON(1);
1306 }
1307
1308 /*
1309 * The delta is too big, we to add a
1310 * new timestamp.
1311 */
1312 event = __rb_reserve_next(cpu_buffer,
1313 RINGBUF_TYPE_TIME_EXTEND,
1314 RB_LEN_TIME_EXTEND,
1315 ts);
1316 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001317 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001318
Steven Rostedtbf41a152008-10-04 02:00:59 -04001319 if (PTR_ERR(event) == -EAGAIN)
1320 return -EAGAIN;
1321
1322 /* Only a commited time event can update the write stamp */
1323 if (rb_is_commit(cpu_buffer, event)) {
1324 /*
1325 * If this is the first on the page, then we need to
1326 * update the page itself, and just put in a zero.
1327 */
1328 if (rb_event_index(event)) {
1329 event->time_delta = *delta & TS_MASK;
1330 event->array[0] = *delta >> TS_SHIFT;
1331 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001332 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001333 event->time_delta = 0;
1334 event->array[0] = 0;
1335 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001336 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001337 /* let the caller know this was the commit */
1338 ret = 1;
1339 } else {
1340 /* Darn, this is just wasted space */
1341 event->time_delta = 0;
1342 event->array[0] = 0;
1343 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001344 }
1345
Steven Rostedtbf41a152008-10-04 02:00:59 -04001346 *delta = 0;
1347
1348 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349}
1350
1351static struct ring_buffer_event *
1352rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1353 unsigned type, unsigned long length)
1354{
1355 struct ring_buffer_event *event;
1356 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001357 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001358 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001359
Steven Rostedtbf41a152008-10-04 02:00:59 -04001360 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001361 /*
1362 * We allow for interrupts to reenter here and do a trace.
1363 * If one does, it will cause this original code to loop
1364 * back here. Even with heavy interrupts happening, this
1365 * should only happen a few times in a row. If this happens
1366 * 1000 times in a row, there must be either an interrupt
1367 * storm or we have something buggy.
1368 * Bail!
1369 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001370 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001371 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001372
Steven Rostedt37886f62009-03-17 17:22:06 -04001373 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374
Steven Rostedtbf41a152008-10-04 02:00:59 -04001375 /*
1376 * Only the first commit can update the timestamp.
1377 * Yes there is a race here. If an interrupt comes in
1378 * just after the conditional and it traces too, then it
1379 * will also check the deltas. More than one timestamp may
1380 * also be made. But only the entry that did the actual
1381 * commit will be something other than zero.
1382 */
1383 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1384 rb_page_write(cpu_buffer->tail_page) ==
1385 rb_commit_index(cpu_buffer)) {
1386
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001387 delta = ts - cpu_buffer->write_stamp;
1388
Steven Rostedtbf41a152008-10-04 02:00:59 -04001389 /* make sure this delta is calculated here */
1390 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391
Steven Rostedtbf41a152008-10-04 02:00:59 -04001392 /* Did the write stamp get updated already? */
1393 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001394 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001395
1396 if (test_time_stamp(delta)) {
1397
1398 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1399
1400 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001402
1403 if (commit == -EAGAIN)
1404 goto again;
1405
1406 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001407 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001408 } else
1409 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001410 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411
1412 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001413 if (PTR_ERR(event) == -EAGAIN)
1414 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001415
Steven Rostedtbf41a152008-10-04 02:00:59 -04001416 if (!event) {
1417 if (unlikely(commit))
1418 /*
1419 * Ouch! We needed a timestamp and it was commited. But
1420 * we didn't get our event reserved.
1421 */
1422 rb_set_commit_to_write(cpu_buffer);
1423 return NULL;
1424 }
1425
1426 /*
1427 * If the timestamp was commited, make the commit our entry
1428 * now so that we will update it when needed.
1429 */
1430 if (commit)
1431 rb_set_commit_event(cpu_buffer, event);
1432 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001433 delta = 0;
1434
1435 event->time_delta = delta;
1436
1437 return event;
1438}
1439
Steven Rostedtbf41a152008-10-04 02:00:59 -04001440static DEFINE_PER_CPU(int, rb_need_resched);
1441
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001442/**
1443 * ring_buffer_lock_reserve - reserve a part of the buffer
1444 * @buffer: the ring buffer to reserve from
1445 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001446 *
1447 * Returns a reseverd event on the ring buffer to copy directly to.
1448 * The user of this interface will need to get the body to write into
1449 * and can use the ring_buffer_event_data() interface.
1450 *
1451 * The length is the length of the data needed, not the event length
1452 * which also includes the event header.
1453 *
1454 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1455 * If NULL is returned, then nothing has been allocated or locked.
1456 */
1457struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001458ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001459{
1460 struct ring_buffer_per_cpu *cpu_buffer;
1461 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001462 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001463
Steven Rostedt033601a2008-11-21 12:41:55 -05001464 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001465 return NULL;
1466
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467 if (atomic_read(&buffer->record_disabled))
1468 return NULL;
1469
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001471 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001472
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001473 cpu = raw_smp_processor_id();
1474
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301475 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001476 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001477
1478 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001479
1480 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001481 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001482
1483 length = rb_calculate_event_length(length);
1484 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001485 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001486
1487 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1488 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001489 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001490
Steven Rostedtbf41a152008-10-04 02:00:59 -04001491 /*
1492 * Need to store resched state on this cpu.
1493 * Only the first needs to.
1494 */
1495
1496 if (preempt_count() == 1)
1497 per_cpu(rb_need_resched, cpu) = resched;
1498
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001499 return event;
1500
Steven Rostedtd7690412008-10-01 00:29:53 -04001501 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001502 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001503 return NULL;
1504}
Robert Richterc4f50182008-12-11 16:49:22 +01001505EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001506
1507static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1508 struct ring_buffer_event *event)
1509{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001510 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001511
1512 /* Only process further if we own the commit */
1513 if (!rb_is_commit(cpu_buffer, event))
1514 return;
1515
1516 cpu_buffer->write_stamp += event->time_delta;
1517
1518 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519}
1520
1521/**
1522 * ring_buffer_unlock_commit - commit a reserved
1523 * @buffer: The buffer to commit to
1524 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525 *
1526 * This commits the data to the ring buffer, and releases any locks held.
1527 *
1528 * Must be paired with ring_buffer_lock_reserve.
1529 */
1530int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001531 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001532{
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 int cpu = raw_smp_processor_id();
1535
1536 cpu_buffer = buffer->buffers[cpu];
1537
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001538 rb_commit(cpu_buffer, event);
1539
Steven Rostedtbf41a152008-10-04 02:00:59 -04001540 /*
1541 * Only the last preempt count needs to restore preemption.
1542 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001543 if (preempt_count() == 1)
1544 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1545 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001546 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001547
1548 return 0;
1549}
Robert Richterc4f50182008-12-11 16:49:22 +01001550EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551
1552/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001553 * ring_buffer_event_discard - discard any event in the ring buffer
1554 * @event: the event to discard
1555 *
1556 * Sometimes a event that is in the ring buffer needs to be ignored.
1557 * This function lets the user discard an event in the ring buffer
1558 * and then that event will not be read later.
1559 *
1560 * Note, it is up to the user to be careful with this, and protect
1561 * against races. If the user discards an event that has been consumed
1562 * it is possible that it could corrupt the ring buffer.
1563 */
1564void ring_buffer_event_discard(struct ring_buffer_event *event)
1565{
1566 event->type = RINGBUF_TYPE_PADDING;
1567 /* time delta must be non zero */
1568 if (!event->time_delta)
1569 event->time_delta = 1;
1570}
1571EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1572
1573/**
1574 * ring_buffer_commit_discard - discard an event that has not been committed
1575 * @buffer: the ring buffer
1576 * @event: non committed event to discard
1577 *
1578 * This is similar to ring_buffer_event_discard but must only be
1579 * performed on an event that has not been committed yet. The difference
1580 * is that this will also try to free the event from the ring buffer
1581 * if another event has not been added behind it.
1582 *
1583 * If another event has been added behind it, it will set the event
1584 * up as discarded, and perform the commit.
1585 *
1586 * If this function is called, do not call ring_buffer_unlock_commit on
1587 * the event.
1588 */
1589void ring_buffer_discard_commit(struct ring_buffer *buffer,
1590 struct ring_buffer_event *event)
1591{
1592 struct ring_buffer_per_cpu *cpu_buffer;
1593 unsigned long new_index, old_index;
1594 struct buffer_page *bpage;
1595 unsigned long index;
1596 unsigned long addr;
1597 int cpu;
1598
1599 /* The event is discarded regardless */
1600 ring_buffer_event_discard(event);
1601
1602 /*
1603 * This must only be called if the event has not been
1604 * committed yet. Thus we can assume that preemption
1605 * is still disabled.
1606 */
1607 RB_WARN_ON(buffer, !preempt_count());
1608
1609 cpu = smp_processor_id();
1610 cpu_buffer = buffer->buffers[cpu];
1611
1612 new_index = rb_event_index(event);
1613 old_index = new_index + rb_event_length(event);
1614 addr = (unsigned long)event;
1615 addr &= PAGE_MASK;
1616
1617 bpage = cpu_buffer->tail_page;
1618
1619 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1620 /*
1621 * This is on the tail page. It is possible that
1622 * a write could come in and move the tail page
1623 * and write to the next page. That is fine
1624 * because we just shorten what is on this page.
1625 */
1626 index = local_cmpxchg(&bpage->write, old_index, new_index);
1627 if (index == old_index)
1628 goto out;
1629 }
1630
1631 /*
1632 * The commit is still visible by the reader, so we
1633 * must increment entries.
1634 */
1635 cpu_buffer->entries++;
1636 out:
1637 /*
1638 * If a write came in and pushed the tail page
1639 * we still need to update the commit pointer
1640 * if we were the commit.
1641 */
1642 if (rb_is_commit(cpu_buffer, event))
1643 rb_set_commit_to_write(cpu_buffer);
1644
1645 /*
1646 * Only the last preempt count needs to restore preemption.
1647 */
1648 if (preempt_count() == 1)
1649 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1650 else
1651 preempt_enable_no_resched_notrace();
1652
1653}
1654EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1655
1656/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001657 * ring_buffer_write - write data to the buffer without reserving
1658 * @buffer: The ring buffer to write to.
1659 * @length: The length of the data being written (excluding the event header)
1660 * @data: The data to write to the buffer.
1661 *
1662 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1663 * one function. If you already have the data to write to the buffer, it
1664 * may be easier to simply call this function.
1665 *
1666 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1667 * and not the length of the event which would hold the header.
1668 */
1669int ring_buffer_write(struct ring_buffer *buffer,
1670 unsigned long length,
1671 void *data)
1672{
1673 struct ring_buffer_per_cpu *cpu_buffer;
1674 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001675 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001676 void *body;
1677 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001678 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001679
Steven Rostedt033601a2008-11-21 12:41:55 -05001680 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001681 return -EBUSY;
1682
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001683 if (atomic_read(&buffer->record_disabled))
1684 return -EBUSY;
1685
Steven Rostedt182e9f52008-11-03 23:15:56 -05001686 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001687
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001688 cpu = raw_smp_processor_id();
1689
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301690 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001691 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001692
1693 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001694
1695 if (atomic_read(&cpu_buffer->record_disabled))
1696 goto out;
1697
1698 event_length = rb_calculate_event_length(length);
1699 event = rb_reserve_next_event(cpu_buffer,
1700 RINGBUF_TYPE_DATA, event_length);
1701 if (!event)
1702 goto out;
1703
1704 body = rb_event_data(event);
1705
1706 memcpy(body, data, length);
1707
1708 rb_commit(cpu_buffer, event);
1709
1710 ret = 0;
1711 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001712 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001713
1714 return ret;
1715}
Robert Richterc4f50182008-12-11 16:49:22 +01001716EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001717
Andrew Morton34a148b2009-01-09 12:27:09 -08001718static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001719{
1720 struct buffer_page *reader = cpu_buffer->reader_page;
1721 struct buffer_page *head = cpu_buffer->head_page;
1722 struct buffer_page *commit = cpu_buffer->commit_page;
1723
1724 return reader->read == rb_page_commit(reader) &&
1725 (commit == reader ||
1726 (commit == head &&
1727 head->read == rb_page_commit(commit)));
1728}
1729
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001730/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001731 * ring_buffer_record_disable - stop all writes into the buffer
1732 * @buffer: The ring buffer to stop writes to.
1733 *
1734 * This prevents all writes to the buffer. Any attempt to write
1735 * to the buffer after this will fail and return NULL.
1736 *
1737 * The caller should call synchronize_sched() after this.
1738 */
1739void ring_buffer_record_disable(struct ring_buffer *buffer)
1740{
1741 atomic_inc(&buffer->record_disabled);
1742}
Robert Richterc4f50182008-12-11 16:49:22 +01001743EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001744
1745/**
1746 * ring_buffer_record_enable - enable writes to the buffer
1747 * @buffer: The ring buffer to enable writes
1748 *
1749 * Note, multiple disables will need the same number of enables
1750 * to truely enable the writing (much like preempt_disable).
1751 */
1752void ring_buffer_record_enable(struct ring_buffer *buffer)
1753{
1754 atomic_dec(&buffer->record_disabled);
1755}
Robert Richterc4f50182008-12-11 16:49:22 +01001756EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001757
1758/**
1759 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1760 * @buffer: The ring buffer to stop writes to.
1761 * @cpu: The CPU buffer to stop
1762 *
1763 * This prevents all writes to the buffer. Any attempt to write
1764 * to the buffer after this will fail and return NULL.
1765 *
1766 * The caller should call synchronize_sched() after this.
1767 */
1768void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1769{
1770 struct ring_buffer_per_cpu *cpu_buffer;
1771
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301772 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001773 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001774
1775 cpu_buffer = buffer->buffers[cpu];
1776 atomic_inc(&cpu_buffer->record_disabled);
1777}
Robert Richterc4f50182008-12-11 16:49:22 +01001778EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001779
1780/**
1781 * ring_buffer_record_enable_cpu - enable writes to the buffer
1782 * @buffer: The ring buffer to enable writes
1783 * @cpu: The CPU to enable.
1784 *
1785 * Note, multiple disables will need the same number of enables
1786 * to truely enable the writing (much like preempt_disable).
1787 */
1788void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1789{
1790 struct ring_buffer_per_cpu *cpu_buffer;
1791
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301792 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001793 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001794
1795 cpu_buffer = buffer->buffers[cpu];
1796 atomic_dec(&cpu_buffer->record_disabled);
1797}
Robert Richterc4f50182008-12-11 16:49:22 +01001798EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001799
1800/**
1801 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1802 * @buffer: The ring buffer
1803 * @cpu: The per CPU buffer to get the entries from.
1804 */
1805unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1806{
1807 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001808 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001809
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301810 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001811 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001812
1813 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001814 ret = cpu_buffer->entries;
Steven Rostedt554f7862009-03-11 22:00:13 -04001815
1816 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001817}
Robert Richterc4f50182008-12-11 16:49:22 +01001818EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819
1820/**
1821 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1822 * @buffer: The ring buffer
1823 * @cpu: The per CPU buffer to get the number of overruns from
1824 */
1825unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1826{
1827 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001828 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001829
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301830 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001831 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001832
1833 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001834 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001835
1836 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001837}
Robert Richterc4f50182008-12-11 16:49:22 +01001838EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001839
1840/**
1841 * ring_buffer_entries - get the number of entries in a buffer
1842 * @buffer: The ring buffer
1843 *
1844 * Returns the total number of entries in the ring buffer
1845 * (all CPU entries)
1846 */
1847unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1848{
1849 struct ring_buffer_per_cpu *cpu_buffer;
1850 unsigned long entries = 0;
1851 int cpu;
1852
1853 /* if you care about this being correct, lock the buffer */
1854 for_each_buffer_cpu(buffer, cpu) {
1855 cpu_buffer = buffer->buffers[cpu];
1856 entries += cpu_buffer->entries;
1857 }
1858
1859 return entries;
1860}
Robert Richterc4f50182008-12-11 16:49:22 +01001861EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001862
1863/**
1864 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1865 * @buffer: The ring buffer
1866 *
1867 * Returns the total number of overruns in the ring buffer
1868 * (all CPU entries)
1869 */
1870unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1871{
1872 struct ring_buffer_per_cpu *cpu_buffer;
1873 unsigned long overruns = 0;
1874 int cpu;
1875
1876 /* if you care about this being correct, lock the buffer */
1877 for_each_buffer_cpu(buffer, cpu) {
1878 cpu_buffer = buffer->buffers[cpu];
1879 overruns += cpu_buffer->overrun;
1880 }
1881
1882 return overruns;
1883}
Robert Richterc4f50182008-12-11 16:49:22 +01001884EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885
Steven Rostedt642edba2008-11-12 00:01:26 -05001886static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001887{
1888 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1889
Steven Rostedtd7690412008-10-01 00:29:53 -04001890 /* Iterator usage is expected to have record disabled */
1891 if (list_empty(&cpu_buffer->reader_page->list)) {
1892 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001893 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001894 } else {
1895 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001896 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001897 }
1898 if (iter->head)
1899 iter->read_stamp = cpu_buffer->read_stamp;
1900 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05001901 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001902}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001903
Steven Rostedt642edba2008-11-12 00:01:26 -05001904/**
1905 * ring_buffer_iter_reset - reset an iterator
1906 * @iter: The iterator to reset
1907 *
1908 * Resets the iterator, so that it will start from the beginning
1909 * again.
1910 */
1911void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1912{
Steven Rostedt554f7862009-03-11 22:00:13 -04001913 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05001914 unsigned long flags;
1915
Steven Rostedt554f7862009-03-11 22:00:13 -04001916 if (!iter)
1917 return;
1918
1919 cpu_buffer = iter->cpu_buffer;
1920
Steven Rostedt642edba2008-11-12 00:01:26 -05001921 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1922 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001923 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001924}
Robert Richterc4f50182008-12-11 16:49:22 +01001925EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001926
1927/**
1928 * ring_buffer_iter_empty - check if an iterator has no more to read
1929 * @iter: The iterator to check
1930 */
1931int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1932{
1933 struct ring_buffer_per_cpu *cpu_buffer;
1934
1935 cpu_buffer = iter->cpu_buffer;
1936
Steven Rostedtbf41a152008-10-04 02:00:59 -04001937 return iter->head_page == cpu_buffer->commit_page &&
1938 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001939}
Robert Richterc4f50182008-12-11 16:49:22 +01001940EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001941
1942static void
1943rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1944 struct ring_buffer_event *event)
1945{
1946 u64 delta;
1947
1948 switch (event->type) {
1949 case RINGBUF_TYPE_PADDING:
1950 return;
1951
1952 case RINGBUF_TYPE_TIME_EXTEND:
1953 delta = event->array[0];
1954 delta <<= TS_SHIFT;
1955 delta += event->time_delta;
1956 cpu_buffer->read_stamp += delta;
1957 return;
1958
1959 case RINGBUF_TYPE_TIME_STAMP:
1960 /* FIXME: not implemented */
1961 return;
1962
1963 case RINGBUF_TYPE_DATA:
1964 cpu_buffer->read_stamp += event->time_delta;
1965 return;
1966
1967 default:
1968 BUG();
1969 }
1970 return;
1971}
1972
1973static void
1974rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1975 struct ring_buffer_event *event)
1976{
1977 u64 delta;
1978
1979 switch (event->type) {
1980 case RINGBUF_TYPE_PADDING:
1981 return;
1982
1983 case RINGBUF_TYPE_TIME_EXTEND:
1984 delta = event->array[0];
1985 delta <<= TS_SHIFT;
1986 delta += event->time_delta;
1987 iter->read_stamp += delta;
1988 return;
1989
1990 case RINGBUF_TYPE_TIME_STAMP:
1991 /* FIXME: not implemented */
1992 return;
1993
1994 case RINGBUF_TYPE_DATA:
1995 iter->read_stamp += event->time_delta;
1996 return;
1997
1998 default:
1999 BUG();
2000 }
2001 return;
2002}
2003
Steven Rostedtd7690412008-10-01 00:29:53 -04002004static struct buffer_page *
2005rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002006{
Steven Rostedtd7690412008-10-01 00:29:53 -04002007 struct buffer_page *reader = NULL;
2008 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002009 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002010
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002011 local_irq_save(flags);
2012 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002013
2014 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002015 /*
2016 * This should normally only loop twice. But because the
2017 * start of the reader inserts an empty page, it causes
2018 * a case where we will loop three times. There should be no
2019 * reason to loop four times (that I know of).
2020 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002021 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002022 reader = NULL;
2023 goto out;
2024 }
2025
Steven Rostedtd7690412008-10-01 00:29:53 -04002026 reader = cpu_buffer->reader_page;
2027
2028 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002029 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002030 goto out;
2031
2032 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002033 if (RB_WARN_ON(cpu_buffer,
2034 cpu_buffer->reader_page->read > rb_page_size(reader)))
2035 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002036
2037 /* check if we caught up to the tail */
2038 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002039 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002040 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002041
2042 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002043 * Splice the empty reader page into the list around the head.
2044 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002045 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002046
Steven Rostedtd7690412008-10-01 00:29:53 -04002047 reader = cpu_buffer->head_page;
2048 cpu_buffer->reader_page->list.next = reader->list.next;
2049 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002050
2051 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002052 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002053
2054 /* Make the reader page now replace the head */
2055 reader->list.prev->next = &cpu_buffer->reader_page->list;
2056 reader->list.next->prev = &cpu_buffer->reader_page->list;
2057
2058 /*
2059 * If the tail is on the reader, then we must set the head
2060 * to the inserted page, otherwise we set it one before.
2061 */
2062 cpu_buffer->head_page = cpu_buffer->reader_page;
2063
Steven Rostedtbf41a152008-10-04 02:00:59 -04002064 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002065 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2066
2067 /* Finally update the reader page to the new head */
2068 cpu_buffer->reader_page = reader;
2069 rb_reset_reader_page(cpu_buffer);
2070
2071 goto again;
2072
2073 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002074 __raw_spin_unlock(&cpu_buffer->lock);
2075 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002076
2077 return reader;
2078}
2079
2080static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2081{
2082 struct ring_buffer_event *event;
2083 struct buffer_page *reader;
2084 unsigned length;
2085
2086 reader = rb_get_reader_page(cpu_buffer);
2087
2088 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002089 if (RB_WARN_ON(cpu_buffer, !reader))
2090 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002091
2092 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002093
Tom Zanussi2d622712009-03-22 03:30:49 -05002094 if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002095 cpu_buffer->entries--;
2096
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002097 rb_update_read_stamp(cpu_buffer, event);
2098
Steven Rostedtd7690412008-10-01 00:29:53 -04002099 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002100 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002101}
2102
2103static void rb_advance_iter(struct ring_buffer_iter *iter)
2104{
2105 struct ring_buffer *buffer;
2106 struct ring_buffer_per_cpu *cpu_buffer;
2107 struct ring_buffer_event *event;
2108 unsigned length;
2109
2110 cpu_buffer = iter->cpu_buffer;
2111 buffer = cpu_buffer->buffer;
2112
2113 /*
2114 * Check if we are at the end of the buffer.
2115 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002116 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002117 if (RB_WARN_ON(buffer,
2118 iter->head_page == cpu_buffer->commit_page))
2119 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002120 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002121 return;
2122 }
2123
2124 event = rb_iter_head_event(iter);
2125
2126 length = rb_event_length(event);
2127
2128 /*
2129 * This should not be called to advance the header if we are
2130 * at the tail of the buffer.
2131 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002132 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002133 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002134 (iter->head + length > rb_commit_index(cpu_buffer))))
2135 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002136
2137 rb_update_iter_read_stamp(iter, event);
2138
2139 iter->head += length;
2140
2141 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142 if ((iter->head >= rb_page_size(iter->head_page)) &&
2143 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002144 rb_advance_iter(iter);
2145}
2146
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002147static struct ring_buffer_event *
2148rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002149{
2150 struct ring_buffer_per_cpu *cpu_buffer;
2151 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002152 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002153 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002154
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002155 cpu_buffer = buffer->buffers[cpu];
2156
2157 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002158 /*
2159 * We repeat when a timestamp is encountered. It is possible
2160 * to get multiple timestamps from an interrupt entering just
2161 * as one timestamp is about to be written. The max times
2162 * that this can happen is the number of nested interrupts we
2163 * can have. Nesting 10 deep of interrupts is clearly
2164 * an anomaly.
2165 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002166 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002167 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002168
Steven Rostedtd7690412008-10-01 00:29:53 -04002169 reader = rb_get_reader_page(cpu_buffer);
2170 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002171 return NULL;
2172
Steven Rostedtd7690412008-10-01 00:29:53 -04002173 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002174
2175 switch (event->type) {
2176 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002177 if (rb_null_event(event))
2178 RB_WARN_ON(cpu_buffer, 1);
2179 /*
2180 * Because the writer could be discarding every
2181 * event it creates (which would probably be bad)
2182 * if we were to go back to "again" then we may never
2183 * catch up, and will trigger the warn on, or lock
2184 * the box. Return the padding, and we will release
2185 * the current locks, and try again.
2186 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002187 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002188 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189
2190 case RINGBUF_TYPE_TIME_EXTEND:
2191 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002192 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002193 goto again;
2194
2195 case RINGBUF_TYPE_TIME_STAMP:
2196 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002197 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198 goto again;
2199
2200 case RINGBUF_TYPE_DATA:
2201 if (ts) {
2202 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002203 ring_buffer_normalize_time_stamp(buffer,
2204 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002205 }
2206 return event;
2207
2208 default:
2209 BUG();
2210 }
2211
2212 return NULL;
2213}
Robert Richterc4f50182008-12-11 16:49:22 +01002214EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002215
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002216static struct ring_buffer_event *
2217rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002218{
2219 struct ring_buffer *buffer;
2220 struct ring_buffer_per_cpu *cpu_buffer;
2221 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002222 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002223
2224 if (ring_buffer_iter_empty(iter))
2225 return NULL;
2226
2227 cpu_buffer = iter->cpu_buffer;
2228 buffer = cpu_buffer->buffer;
2229
2230 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002231 /*
2232 * We repeat when a timestamp is encountered. It is possible
2233 * to get multiple timestamps from an interrupt entering just
2234 * as one timestamp is about to be written. The max times
2235 * that this can happen is the number of nested interrupts we
2236 * can have. Nesting 10 deep of interrupts is clearly
2237 * an anomaly.
2238 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002239 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002240 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002241
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242 if (rb_per_cpu_empty(cpu_buffer))
2243 return NULL;
2244
2245 event = rb_iter_head_event(iter);
2246
2247 switch (event->type) {
2248 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002249 if (rb_null_event(event)) {
2250 rb_inc_iter(iter);
2251 goto again;
2252 }
2253 rb_advance_iter(iter);
2254 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255
2256 case RINGBUF_TYPE_TIME_EXTEND:
2257 /* Internal data, OK to advance */
2258 rb_advance_iter(iter);
2259 goto again;
2260
2261 case RINGBUF_TYPE_TIME_STAMP:
2262 /* FIXME: not implemented */
2263 rb_advance_iter(iter);
2264 goto again;
2265
2266 case RINGBUF_TYPE_DATA:
2267 if (ts) {
2268 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002269 ring_buffer_normalize_time_stamp(buffer,
2270 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002271 }
2272 return event;
2273
2274 default:
2275 BUG();
2276 }
2277
2278 return NULL;
2279}
Robert Richterc4f50182008-12-11 16:49:22 +01002280EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002281
2282/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002283 * ring_buffer_peek - peek at the next event to be read
2284 * @buffer: The ring buffer to read
2285 * @cpu: The cpu to peak at
2286 * @ts: The timestamp counter of this event.
2287 *
2288 * This will return the event that will be read next, but does
2289 * not consume the data.
2290 */
2291struct ring_buffer_event *
2292ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2293{
2294 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002295 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002296 unsigned long flags;
2297
Steven Rostedt554f7862009-03-11 22:00:13 -04002298 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002299 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002300
Tom Zanussi2d622712009-03-22 03:30:49 -05002301 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002302 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2303 event = rb_buffer_peek(buffer, cpu, ts);
2304 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2305
Tom Zanussi2d622712009-03-22 03:30:49 -05002306 if (event && event->type == RINGBUF_TYPE_PADDING) {
2307 cpu_relax();
2308 goto again;
2309 }
2310
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002311 return event;
2312}
2313
2314/**
2315 * ring_buffer_iter_peek - peek at the next event to be read
2316 * @iter: The ring buffer iterator
2317 * @ts: The timestamp counter of this event.
2318 *
2319 * This will return the event that will be read next, but does
2320 * not increment the iterator.
2321 */
2322struct ring_buffer_event *
2323ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2324{
2325 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2326 struct ring_buffer_event *event;
2327 unsigned long flags;
2328
Tom Zanussi2d622712009-03-22 03:30:49 -05002329 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002330 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2331 event = rb_iter_peek(iter, ts);
2332 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2333
Tom Zanussi2d622712009-03-22 03:30:49 -05002334 if (event && event->type == RINGBUF_TYPE_PADDING) {
2335 cpu_relax();
2336 goto again;
2337 }
2338
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002339 return event;
2340}
2341
2342/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002343 * ring_buffer_consume - return an event and consume it
2344 * @buffer: The ring buffer to get the next event from
2345 *
2346 * Returns the next event in the ring buffer, and that event is consumed.
2347 * Meaning, that sequential reads will keep returning a different event,
2348 * and eventually empty the ring buffer if the producer is slower.
2349 */
2350struct ring_buffer_event *
2351ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2352{
Steven Rostedt554f7862009-03-11 22:00:13 -04002353 struct ring_buffer_per_cpu *cpu_buffer;
2354 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002355 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002356
Tom Zanussi2d622712009-03-22 03:30:49 -05002357 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002358 /* might be called in atomic */
2359 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002360
Steven Rostedt554f7862009-03-11 22:00:13 -04002361 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2362 goto out;
2363
2364 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002365 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002366
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002367 event = rb_buffer_peek(buffer, cpu, ts);
2368 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002369 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002370
Steven Rostedtd7690412008-10-01 00:29:53 -04002371 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002372
Steven Rostedt554f7862009-03-11 22:00:13 -04002373 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002374 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2375
Steven Rostedt554f7862009-03-11 22:00:13 -04002376 out:
2377 preempt_enable();
2378
Tom Zanussi2d622712009-03-22 03:30:49 -05002379 if (event && event->type == RINGBUF_TYPE_PADDING) {
2380 cpu_relax();
2381 goto again;
2382 }
2383
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002384 return event;
2385}
Robert Richterc4f50182008-12-11 16:49:22 +01002386EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002387
2388/**
2389 * ring_buffer_read_start - start a non consuming read of the buffer
2390 * @buffer: The ring buffer to read from
2391 * @cpu: The cpu buffer to iterate over
2392 *
2393 * This starts up an iteration through the buffer. It also disables
2394 * the recording to the buffer until the reading is finished.
2395 * This prevents the reading from being corrupted. This is not
2396 * a consuming read, so a producer is not expected.
2397 *
2398 * Must be paired with ring_buffer_finish.
2399 */
2400struct ring_buffer_iter *
2401ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2402{
2403 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002404 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002405 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002406
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302407 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002408 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002409
2410 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2411 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002412 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002413
2414 cpu_buffer = buffer->buffers[cpu];
2415
2416 iter->cpu_buffer = cpu_buffer;
2417
2418 atomic_inc(&cpu_buffer->record_disabled);
2419 synchronize_sched();
2420
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002421 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002422 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002423 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002424 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002425 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002426
2427 return iter;
2428}
Robert Richterc4f50182008-12-11 16:49:22 +01002429EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002430
2431/**
2432 * ring_buffer_finish - finish reading the iterator of the buffer
2433 * @iter: The iterator retrieved by ring_buffer_start
2434 *
2435 * This re-enables the recording to the buffer, and frees the
2436 * iterator.
2437 */
2438void
2439ring_buffer_read_finish(struct ring_buffer_iter *iter)
2440{
2441 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2442
2443 atomic_dec(&cpu_buffer->record_disabled);
2444 kfree(iter);
2445}
Robert Richterc4f50182008-12-11 16:49:22 +01002446EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002447
2448/**
2449 * ring_buffer_read - read the next item in the ring buffer by the iterator
2450 * @iter: The ring buffer iterator
2451 * @ts: The time stamp of the event read.
2452 *
2453 * This reads the next event in the ring buffer and increments the iterator.
2454 */
2455struct ring_buffer_event *
2456ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2457{
2458 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002459 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2460 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002461
Tom Zanussi2d622712009-03-22 03:30:49 -05002462 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002463 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2464 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002465 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002466 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002467
2468 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002469 out:
2470 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002471
Tom Zanussi2d622712009-03-22 03:30:49 -05002472 if (event && event->type == RINGBUF_TYPE_PADDING) {
2473 cpu_relax();
2474 goto again;
2475 }
2476
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002477 return event;
2478}
Robert Richterc4f50182008-12-11 16:49:22 +01002479EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002480
2481/**
2482 * ring_buffer_size - return the size of the ring buffer (in bytes)
2483 * @buffer: The ring buffer.
2484 */
2485unsigned long ring_buffer_size(struct ring_buffer *buffer)
2486{
2487 return BUF_PAGE_SIZE * buffer->pages;
2488}
Robert Richterc4f50182008-12-11 16:49:22 +01002489EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002490
2491static void
2492rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2493{
2494 cpu_buffer->head_page
2495 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002496 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002497 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002498
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002499 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002500
2501 cpu_buffer->tail_page = cpu_buffer->head_page;
2502 cpu_buffer->commit_page = cpu_buffer->head_page;
2503
2504 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2505 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002506 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002507 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002508
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002509 cpu_buffer->overrun = 0;
2510 cpu_buffer->entries = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05002511
2512 cpu_buffer->write_stamp = 0;
2513 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002514}
2515
2516/**
2517 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2518 * @buffer: The ring buffer to reset a per cpu buffer of
2519 * @cpu: The CPU buffer to be reset
2520 */
2521void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2522{
2523 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2524 unsigned long flags;
2525
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302526 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002527 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002528
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002529 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2530
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002531 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002532
2533 rb_reset_cpu(cpu_buffer);
2534
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002535 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002536
2537 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002538}
Robert Richterc4f50182008-12-11 16:49:22 +01002539EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002540
2541/**
2542 * ring_buffer_reset - reset a ring buffer
2543 * @buffer: The ring buffer to reset all cpu buffers
2544 */
2545void ring_buffer_reset(struct ring_buffer *buffer)
2546{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002547 int cpu;
2548
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002549 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002550 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551}
Robert Richterc4f50182008-12-11 16:49:22 +01002552EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002553
2554/**
2555 * rind_buffer_empty - is the ring buffer empty?
2556 * @buffer: The ring buffer to test
2557 */
2558int ring_buffer_empty(struct ring_buffer *buffer)
2559{
2560 struct ring_buffer_per_cpu *cpu_buffer;
2561 int cpu;
2562
2563 /* yes this is racy, but if you don't like the race, lock the buffer */
2564 for_each_buffer_cpu(buffer, cpu) {
2565 cpu_buffer = buffer->buffers[cpu];
2566 if (!rb_per_cpu_empty(cpu_buffer))
2567 return 0;
2568 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002569
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002570 return 1;
2571}
Robert Richterc4f50182008-12-11 16:49:22 +01002572EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002573
2574/**
2575 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2576 * @buffer: The ring buffer
2577 * @cpu: The CPU buffer to test
2578 */
2579int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2580{
2581 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002582 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002583
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302584 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002585 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002586
2587 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002588 ret = rb_per_cpu_empty(cpu_buffer);
2589
Steven Rostedt554f7862009-03-11 22:00:13 -04002590
2591 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002592}
Robert Richterc4f50182008-12-11 16:49:22 +01002593EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002594
2595/**
2596 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2597 * @buffer_a: One buffer to swap with
2598 * @buffer_b: The other buffer to swap with
2599 *
2600 * This function is useful for tracers that want to take a "snapshot"
2601 * of a CPU buffer and has another back up buffer lying around.
2602 * it is expected that the tracer handles the cpu buffer not being
2603 * used at the moment.
2604 */
2605int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2606 struct ring_buffer *buffer_b, int cpu)
2607{
2608 struct ring_buffer_per_cpu *cpu_buffer_a;
2609 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002610 int ret = -EINVAL;
2611
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302612 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2613 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002614 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615
2616 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002617 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002618 goto out;
2619
2620 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002621
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002622 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002623 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002624
2625 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002626 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002627
2628 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002629 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002630
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002631 cpu_buffer_a = buffer_a->buffers[cpu];
2632 cpu_buffer_b = buffer_b->buffers[cpu];
2633
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002634 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002635 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002636
2637 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002638 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002639
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002640 /*
2641 * We can't do a synchronize_sched here because this
2642 * function can be called in atomic context.
2643 * Normally this will be called from the same CPU as cpu.
2644 * If not it's up to the caller to protect this.
2645 */
2646 atomic_inc(&cpu_buffer_a->record_disabled);
2647 atomic_inc(&cpu_buffer_b->record_disabled);
2648
2649 buffer_a->buffers[cpu] = cpu_buffer_b;
2650 buffer_b->buffers[cpu] = cpu_buffer_a;
2651
2652 cpu_buffer_b->buffer = buffer_a;
2653 cpu_buffer_a->buffer = buffer_b;
2654
2655 atomic_dec(&cpu_buffer_a->record_disabled);
2656 atomic_dec(&cpu_buffer_b->record_disabled);
2657
Steven Rostedt554f7862009-03-11 22:00:13 -04002658 ret = 0;
2659out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002660 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002661}
Robert Richterc4f50182008-12-11 16:49:22 +01002662EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002663
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002664static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002665 struct buffer_data_page *bpage,
2666 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002667{
2668 struct ring_buffer_event *event;
2669 unsigned long head;
2670
2671 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002672 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002673 head += rb_event_length(event)) {
2674
Steven Rostedt044fa782008-12-02 23:50:03 -05002675 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002676 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2677 return;
2678 /* Only count data entries */
2679 if (event->type != RINGBUF_TYPE_DATA)
2680 continue;
2681 cpu_buffer->entries--;
2682 }
2683 __raw_spin_unlock(&cpu_buffer->lock);
2684}
2685
2686/**
2687 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2688 * @buffer: the buffer to allocate for.
2689 *
2690 * This function is used in conjunction with ring_buffer_read_page.
2691 * When reading a full page from the ring buffer, these functions
2692 * can be used to speed up the process. The calling function should
2693 * allocate a few pages first with this function. Then when it
2694 * needs to get pages from the ring buffer, it passes the result
2695 * of this function into ring_buffer_read_page, which will swap
2696 * the page that was allocated, with the read page of the buffer.
2697 *
2698 * Returns:
2699 * The page allocated, or NULL on error.
2700 */
2701void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2702{
Steven Rostedt044fa782008-12-02 23:50:03 -05002703 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002704 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002705
2706 addr = __get_free_page(GFP_KERNEL);
2707 if (!addr)
2708 return NULL;
2709
Steven Rostedt044fa782008-12-02 23:50:03 -05002710 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002711
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002712 rb_init_page(bpage);
2713
Steven Rostedt044fa782008-12-02 23:50:03 -05002714 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002715}
2716
2717/**
2718 * ring_buffer_free_read_page - free an allocated read page
2719 * @buffer: the buffer the page was allocate for
2720 * @data: the page to free
2721 *
2722 * Free a page allocated from ring_buffer_alloc_read_page.
2723 */
2724void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2725{
2726 free_page((unsigned long)data);
2727}
2728
2729/**
2730 * ring_buffer_read_page - extract a page from the ring buffer
2731 * @buffer: buffer to extract from
2732 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002733 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002734 * @cpu: the cpu of the buffer to extract
2735 * @full: should the extraction only happen when the page is full.
2736 *
2737 * This function will pull out a page from the ring buffer and consume it.
2738 * @data_page must be the address of the variable that was returned
2739 * from ring_buffer_alloc_read_page. This is because the page might be used
2740 * to swap with a page in the ring buffer.
2741 *
2742 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002743 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002744 * if (!rpage)
2745 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002746 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002747 * if (ret >= 0)
2748 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002749 *
2750 * When @full is set, the function will not return true unless
2751 * the writer is off the reader page.
2752 *
2753 * Note: it is up to the calling functions to handle sleeps and wakeups.
2754 * The ring buffer can be used anywhere in the kernel and can not
2755 * blindly call wake_up. The layer that uses the ring buffer must be
2756 * responsible for that.
2757 *
2758 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002759 * >=0 if data has been transferred, returns the offset of consumed data.
2760 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002761 */
2762int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002763 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002764{
2765 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2766 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002767 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002768 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002769 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002770 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002771 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002772 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002773 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002774
Steven Rostedt554f7862009-03-11 22:00:13 -04002775 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2776 goto out;
2777
Steven Rostedt474d32b2009-03-03 19:51:40 -05002778 /*
2779 * If len is not big enough to hold the page header, then
2780 * we can not copy anything.
2781 */
2782 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002783 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002784
2785 len -= BUF_PAGE_HDR_SIZE;
2786
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002787 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002788 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002789
Steven Rostedt044fa782008-12-02 23:50:03 -05002790 bpage = *data_page;
2791 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002792 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002793
2794 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2795
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002796 reader = rb_get_reader_page(cpu_buffer);
2797 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002798 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002799
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002800 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002801
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002802 read = reader->read;
2803 commit = rb_page_commit(reader);
2804
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002805 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002806 * If this page has been partially read or
2807 * if len is not big enough to read the rest of the page or
2808 * a writer is still on the page, then
2809 * we must copy the data from the page to the buffer.
2810 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002811 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002812 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002813 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002814 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002815 unsigned int rpos = read;
2816 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002817 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002818
2819 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002820 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002821
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002822 if (len > (commit - read))
2823 len = (commit - read);
2824
2825 size = rb_event_length(event);
2826
2827 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002828 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002829
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002830 /* save the current timestamp, since the user will need it */
2831 save_timestamp = cpu_buffer->read_stamp;
2832
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002833 /* Need to copy one event at a time */
2834 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002835 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002836
2837 len -= size;
2838
2839 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002840 rpos = reader->read;
2841 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002842
2843 event = rb_reader_event(cpu_buffer);
2844 size = rb_event_length(event);
2845 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002846
2847 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002848 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002849 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002850
Steven Rostedt474d32b2009-03-03 19:51:40 -05002851 /* we copied everything to the beginning */
2852 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002853 } else {
2854 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002855 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002856 bpage = reader->page;
2857 reader->page = *data_page;
2858 local_set(&reader->write, 0);
2859 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002860 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002861
2862 /* update the entry counter */
2863 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002864 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002865 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002866
Steven Rostedt554f7862009-03-11 22:00:13 -04002867 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002868 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2869
Steven Rostedt554f7862009-03-11 22:00:13 -04002870 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002871 return ret;
2872}
2873
Steven Rostedta3583242008-11-11 15:01:42 -05002874static ssize_t
2875rb_simple_read(struct file *filp, char __user *ubuf,
2876 size_t cnt, loff_t *ppos)
2877{
Hannes Eder5e398412009-02-10 19:44:34 +01002878 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002879 char buf[64];
2880 int r;
2881
Steven Rostedt033601a2008-11-21 12:41:55 -05002882 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2883 r = sprintf(buf, "permanently disabled\n");
2884 else
2885 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002886
2887 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2888}
2889
2890static ssize_t
2891rb_simple_write(struct file *filp, const char __user *ubuf,
2892 size_t cnt, loff_t *ppos)
2893{
Hannes Eder5e398412009-02-10 19:44:34 +01002894 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002895 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002896 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05002897 int ret;
2898
2899 if (cnt >= sizeof(buf))
2900 return -EINVAL;
2901
2902 if (copy_from_user(&buf, ubuf, cnt))
2903 return -EFAULT;
2904
2905 buf[cnt] = 0;
2906
2907 ret = strict_strtoul(buf, 10, &val);
2908 if (ret < 0)
2909 return ret;
2910
Steven Rostedt033601a2008-11-21 12:41:55 -05002911 if (val)
2912 set_bit(RB_BUFFERS_ON_BIT, p);
2913 else
2914 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05002915
2916 (*ppos)++;
2917
2918 return cnt;
2919}
2920
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002921static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05002922 .open = tracing_open_generic,
2923 .read = rb_simple_read,
2924 .write = rb_simple_write,
2925};
2926
2927
2928static __init int rb_init_debugfs(void)
2929{
2930 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05002931
2932 d_tracer = tracing_init_dentry();
2933
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002934 trace_create_file("tracing_on", 0644, d_tracer,
2935 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05002936
2937 return 0;
2938}
2939
2940fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04002941
Steven Rostedt59222ef2009-03-12 11:46:03 -04002942#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01002943static int rb_cpu_notify(struct notifier_block *self,
2944 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04002945{
2946 struct ring_buffer *buffer =
2947 container_of(self, struct ring_buffer, cpu_notify);
2948 long cpu = (long)hcpu;
2949
2950 switch (action) {
2951 case CPU_UP_PREPARE:
2952 case CPU_UP_PREPARE_FROZEN:
2953 if (cpu_isset(cpu, *buffer->cpumask))
2954 return NOTIFY_OK;
2955
2956 buffer->buffers[cpu] =
2957 rb_allocate_cpu_buffer(buffer, cpu);
2958 if (!buffer->buffers[cpu]) {
2959 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2960 cpu);
2961 return NOTIFY_OK;
2962 }
2963 smp_wmb();
2964 cpu_set(cpu, *buffer->cpumask);
2965 break;
2966 case CPU_DOWN_PREPARE:
2967 case CPU_DOWN_PREPARE_FROZEN:
2968 /*
2969 * Do nothing.
2970 * If we were to free the buffer, then the user would
2971 * lose any trace that was in the buffer.
2972 */
2973 break;
2974 default:
2975 break;
2976 }
2977 return NOTIFY_OK;
2978}
2979#endif