blob: 58128ad2fde03af48f525f605d8682208c29ad57 [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 Rostedt7a8e76a2008-09-29 23:02:38 -0400183/* Up this if you want to test the TIME_EXTENTS and normalization */
184#define DEBUG_SHIFT 0
185
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400186u64 ring_buffer_time_stamp(int cpu)
187{
Steven Rostedt47e74f22008-11-12 00:01:27 -0500188 u64 time;
189
190 preempt_disable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400191 /* shift to debug/test normalization and TIME_EXTENTS */
Ingo Molnar14131f22009-02-26 18:47:11 +0100192 time = trace_clock_local() << DEBUG_SHIFT;
Frederic Weisbecker2c2d7322008-12-16 22:08:58 +0100193 preempt_enable_no_resched_notrace();
Steven Rostedt47e74f22008-11-12 00:01:27 -0500194
195 return time;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400196}
Robert Richterc4f50182008-12-11 16:49:22 +0100197EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400198
199void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
200{
201 /* Just stupid testing the normalize function and deltas */
202 *ts >>= DEBUG_SHIFT;
203}
Robert Richterc4f50182008-12-11 16:49:22 +0100204EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400208#define RB_MAX_SMALL_DATA 28
209
210enum {
211 RB_LEN_TIME_EXTEND = 8,
212 RB_LEN_TIME_STAMP = 16,
213};
214
215/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800216static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400217rb_event_length(struct ring_buffer_event *event)
218{
219 unsigned length;
220
221 switch (event->type) {
222 case RINGBUF_TYPE_PADDING:
223 /* undefined */
224 return -1;
225
226 case RINGBUF_TYPE_TIME_EXTEND:
227 return RB_LEN_TIME_EXTEND;
228
229 case RINGBUF_TYPE_TIME_STAMP:
230 return RB_LEN_TIME_STAMP;
231
232 case RINGBUF_TYPE_DATA:
233 if (event->len)
Andrew Morton67d34722009-01-09 12:27:09 -0800234 length = event->len * RB_ALIGNMENT;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400235 else
236 length = event->array[0];
237 return length + RB_EVNT_HDR_SIZE;
238 default:
239 BUG();
240 }
241 /* not hit */
242 return 0;
243}
244
245/**
246 * ring_buffer_event_length - return the length of the event
247 * @event: the event to get the length of
248 */
249unsigned ring_buffer_event_length(struct ring_buffer_event *event)
250{
Robert Richter465634a2009-01-07 15:32:11 +0100251 unsigned length = rb_event_length(event);
252 if (event->type != RINGBUF_TYPE_DATA)
253 return length;
254 length -= RB_EVNT_HDR_SIZE;
255 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
256 length -= sizeof(event->array[0]);
257 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400258}
Robert Richterc4f50182008-12-11 16:49:22 +0100259EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260
261/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800262static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400263rb_event_data(struct ring_buffer_event *event)
264{
265 BUG_ON(event->type != RINGBUF_TYPE_DATA);
266 /* If length is in len field, then array[0] has the data */
267 if (event->len)
268 return (void *)&event->array[0];
269 /* Otherwise length is in array[0] and array[1] has the data */
270 return (void *)&event->array[1];
271}
272
273/**
274 * ring_buffer_event_data - return the data of the event
275 * @event: the event to get the data from
276 */
277void *ring_buffer_event_data(struct ring_buffer_event *event)
278{
279 return rb_event_data(event);
280}
Robert Richterc4f50182008-12-11 16:49:22 +0100281EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282
283#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030284 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400285
286#define TS_SHIFT 27
287#define TS_MASK ((1ULL << TS_SHIFT) - 1)
288#define TS_DELTA_TEST (~TS_MASK)
289
Steven Rostedtabc9b562008-12-02 15:34:06 -0500290struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400291 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500292 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500293 unsigned char data[]; /* data of buffer page */
294};
295
296struct buffer_page {
297 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400298 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400299 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500300 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400301};
302
Steven Rostedt044fa782008-12-02 23:50:03 -0500303static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500304{
Steven Rostedt044fa782008-12-02 23:50:03 -0500305 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500306}
307
Steven Rostedt474d32b2009-03-03 19:51:40 -0500308/**
309 * ring_buffer_page_len - the size of data on the page.
310 * @page: The page to read
311 *
312 * Returns the amount of data on the page, including buffer page header.
313 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500314size_t ring_buffer_page_len(void *page)
315{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500316 return local_read(&((struct buffer_data_page *)page)->commit)
317 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500318}
319
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400320/*
Steven Rostedted568292008-09-29 23:02:40 -0400321 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
322 * this issue out.
323 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800324static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400325{
Andrew Morton34a148b2009-01-09 12:27:09 -0800326 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400327 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400328}
329
330/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400331 * We need to fit the time_stamp delta into 27 bits.
332 */
333static inline int test_time_stamp(u64 delta)
334{
335 if (delta & TS_DELTA_TEST)
336 return 1;
337 return 0;
338}
339
Steven Rostedt474d32b2009-03-03 19:51:40 -0500340#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400341
342/*
343 * head_page == tail_page && head == tail then buffer is empty.
344 */
345struct ring_buffer_per_cpu {
346 int cpu;
347 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100348 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500349 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400350 struct lock_class_key lock_key;
351 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400352 struct buffer_page *head_page; /* read from head */
353 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500354 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400355 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400356 unsigned long overrun;
357 unsigned long entries;
358 u64 write_stamp;
359 u64 read_stamp;
360 atomic_t record_disabled;
361};
362
363struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400364 unsigned pages;
365 unsigned flags;
366 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400367 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200368 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400369
370 struct mutex mutex;
371
372 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400373
Steven Rostedt59222ef2009-03-12 11:46:03 -0400374#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400375 struct notifier_block cpu_notify;
376#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400377};
378
379struct ring_buffer_iter {
380 struct ring_buffer_per_cpu *cpu_buffer;
381 unsigned long head;
382 struct buffer_page *head_page;
383 u64 read_stamp;
384};
385
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500386/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400387#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500388 ({ \
389 int _____ret = unlikely(cond); \
390 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400391 atomic_inc(&buffer->record_disabled); \
392 WARN_ON(1); \
393 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500394 _____ret; \
395 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500396
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400397/**
398 * check_pages - integrity check of buffer pages
399 * @cpu_buffer: CPU buffer with pages to test
400 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500401 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400402 * been corrupted.
403 */
404static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
405{
406 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500407 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400408
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500409 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
410 return -1;
411 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
412 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400413
Steven Rostedt044fa782008-12-02 23:50:03 -0500414 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500415 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500416 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500417 return -1;
418 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500419 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500420 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400421 }
422
423 return 0;
424}
425
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400426static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
427 unsigned nr_pages)
428{
429 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500430 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400431 unsigned long addr;
432 LIST_HEAD(pages);
433 unsigned i;
434
435 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500436 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400437 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500438 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400439 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500440 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400441
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400442 addr = __get_free_page(GFP_KERNEL);
443 if (!addr)
444 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500445 bpage->page = (void *)addr;
446 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400447 }
448
449 list_splice(&pages, head);
450
451 rb_check_pages(cpu_buffer);
452
453 return 0;
454
455 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500456 list_for_each_entry_safe(bpage, tmp, &pages, list) {
457 list_del_init(&bpage->list);
458 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400459 }
460 return -ENOMEM;
461}
462
463static struct ring_buffer_per_cpu *
464rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
465{
466 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500467 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400468 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400469 int ret;
470
471 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
472 GFP_KERNEL, cpu_to_node(cpu));
473 if (!cpu_buffer)
474 return NULL;
475
476 cpu_buffer->cpu = cpu;
477 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100478 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500479 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400480 INIT_LIST_HEAD(&cpu_buffer->pages);
481
Steven Rostedt044fa782008-12-02 23:50:03 -0500482 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400483 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500484 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400485 goto fail_free_buffer;
486
Steven Rostedt044fa782008-12-02 23:50:03 -0500487 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400488 addr = __get_free_page(GFP_KERNEL);
489 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400490 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500491 bpage->page = (void *)addr;
492 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400493
Steven Rostedtd7690412008-10-01 00:29:53 -0400494 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400495
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400496 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
497 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400498 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400499
500 cpu_buffer->head_page
501 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400502 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503
504 return cpu_buffer;
505
Steven Rostedtd7690412008-10-01 00:29:53 -0400506 fail_free_reader:
507 free_buffer_page(cpu_buffer->reader_page);
508
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400509 fail_free_buffer:
510 kfree(cpu_buffer);
511 return NULL;
512}
513
514static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
515{
516 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500517 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400518
Steven Rostedtd7690412008-10-01 00:29:53 -0400519 list_del_init(&cpu_buffer->reader_page->list);
520 free_buffer_page(cpu_buffer->reader_page);
521
Steven Rostedt044fa782008-12-02 23:50:03 -0500522 list_for_each_entry_safe(bpage, tmp, head, list) {
523 list_del_init(&bpage->list);
524 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400525 }
526 kfree(cpu_buffer);
527}
528
Steven Rostedta7b13742008-09-29 23:02:39 -0400529/*
530 * Causes compile errors if the struct buffer_page gets bigger
531 * than the struct page.
532 */
533extern int ring_buffer_page_too_big(void);
534
Steven Rostedt59222ef2009-03-12 11:46:03 -0400535#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400536static int __cpuinit rb_cpu_notify(struct notifier_block *self,
537 unsigned long action, void *hcpu);
538#endif
539
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400540/**
541 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100542 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400543 * @flags: attributes to set for the ring buffer.
544 *
545 * Currently the only flag that is available is the RB_FL_OVERWRITE
546 * flag. This flag means that the buffer will overwrite old data
547 * when the buffer wraps. If this flag is not set, the buffer will
548 * drop data when the tail hits the head.
549 */
550struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
551{
552 struct ring_buffer *buffer;
553 int bsize;
554 int cpu;
555
Steven Rostedta7b13742008-09-29 23:02:39 -0400556 /* Paranoid! Optimizes out when all is well */
557 if (sizeof(struct buffer_page) > sizeof(struct page))
558 ring_buffer_page_too_big();
559
560
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400561 /* keep it in its own cache line */
562 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
563 GFP_KERNEL);
564 if (!buffer)
565 return NULL;
566
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030567 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
568 goto fail_free_buffer;
569
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400570 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
571 buffer->flags = flags;
572
573 /* need at least two pages */
574 if (buffer->pages == 1)
575 buffer->pages++;
576
Steven Rostedt554f7862009-03-11 22:00:13 -0400577 get_online_cpus();
578 cpumask_copy(buffer->cpumask, cpu_online_mask);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400579 buffer->cpus = nr_cpu_ids;
580
581 bsize = sizeof(void *) * nr_cpu_ids;
582 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
583 GFP_KERNEL);
584 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030585 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400586
587 for_each_buffer_cpu(buffer, cpu) {
588 buffer->buffers[cpu] =
589 rb_allocate_cpu_buffer(buffer, cpu);
590 if (!buffer->buffers[cpu])
591 goto fail_free_buffers;
592 }
593
Steven Rostedt59222ef2009-03-12 11:46:03 -0400594#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400595 buffer->cpu_notify.notifier_call = rb_cpu_notify;
596 buffer->cpu_notify.priority = 0;
597 register_cpu_notifier(&buffer->cpu_notify);
598#endif
599
600 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400601 mutex_init(&buffer->mutex);
602
603 return buffer;
604
605 fail_free_buffers:
606 for_each_buffer_cpu(buffer, cpu) {
607 if (buffer->buffers[cpu])
608 rb_free_cpu_buffer(buffer->buffers[cpu]);
609 }
610 kfree(buffer->buffers);
611
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030612 fail_free_cpumask:
613 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400614 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400616 fail_free_buffer:
617 kfree(buffer);
618 return NULL;
619}
Robert Richterc4f50182008-12-11 16:49:22 +0100620EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400621
622/**
623 * ring_buffer_free - free a ring buffer.
624 * @buffer: the buffer to free.
625 */
626void
627ring_buffer_free(struct ring_buffer *buffer)
628{
629 int cpu;
630
Steven Rostedt554f7862009-03-11 22:00:13 -0400631 get_online_cpus();
632
Steven Rostedt59222ef2009-03-12 11:46:03 -0400633#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400634 unregister_cpu_notifier(&buffer->cpu_notify);
635#endif
636
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400637 for_each_buffer_cpu(buffer, cpu)
638 rb_free_cpu_buffer(buffer->buffers[cpu]);
639
Steven Rostedt554f7862009-03-11 22:00:13 -0400640 put_online_cpus();
641
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030642 free_cpumask_var(buffer->cpumask);
643
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400644 kfree(buffer);
645}
Robert Richterc4f50182008-12-11 16:49:22 +0100646EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400647
648static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
649
650static void
651rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
652{
Steven Rostedt044fa782008-12-02 23:50:03 -0500653 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400654 struct list_head *p;
655 unsigned i;
656
657 atomic_inc(&cpu_buffer->record_disabled);
658 synchronize_sched();
659
660 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500661 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
662 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400663 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500664 bpage = list_entry(p, struct buffer_page, list);
665 list_del_init(&bpage->list);
666 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400667 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500668 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
669 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400670
671 rb_reset_cpu(cpu_buffer);
672
673 rb_check_pages(cpu_buffer);
674
675 atomic_dec(&cpu_buffer->record_disabled);
676
677}
678
679static void
680rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
681 struct list_head *pages, unsigned nr_pages)
682{
Steven Rostedt044fa782008-12-02 23:50:03 -0500683 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400684 struct list_head *p;
685 unsigned i;
686
687 atomic_inc(&cpu_buffer->record_disabled);
688 synchronize_sched();
689
690 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500691 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
692 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400693 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500694 bpage = list_entry(p, struct buffer_page, list);
695 list_del_init(&bpage->list);
696 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400697 }
698 rb_reset_cpu(cpu_buffer);
699
700 rb_check_pages(cpu_buffer);
701
702 atomic_dec(&cpu_buffer->record_disabled);
703}
704
705/**
706 * ring_buffer_resize - resize the ring buffer
707 * @buffer: the buffer to resize.
708 * @size: the new size.
709 *
710 * The tracer is responsible for making sure that the buffer is
711 * not being used while changing the size.
712 * Note: We may be able to change the above requirement by using
713 * RCU synchronizations.
714 *
715 * Minimum size is 2 * BUF_PAGE_SIZE.
716 *
717 * Returns -1 on failure.
718 */
719int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
720{
721 struct ring_buffer_per_cpu *cpu_buffer;
722 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500723 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400724 unsigned long buffer_size;
725 unsigned long addr;
726 LIST_HEAD(pages);
727 int i, cpu;
728
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100729 /*
730 * Always succeed at resizing a non-existent buffer:
731 */
732 if (!buffer)
733 return size;
734
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400735 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
736 size *= BUF_PAGE_SIZE;
737 buffer_size = buffer->pages * BUF_PAGE_SIZE;
738
739 /* we need a minimum of two pages */
740 if (size < BUF_PAGE_SIZE * 2)
741 size = BUF_PAGE_SIZE * 2;
742
743 if (size == buffer_size)
744 return size;
745
746 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400747 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400748
749 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
750
751 if (size < buffer_size) {
752
753 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400754 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
755 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400756
757 rm_pages = buffer->pages - nr_pages;
758
759 for_each_buffer_cpu(buffer, cpu) {
760 cpu_buffer = buffer->buffers[cpu];
761 rb_remove_pages(cpu_buffer, rm_pages);
762 }
763 goto out;
764 }
765
766 /*
767 * This is a bit more difficult. We only want to add pages
768 * when we can allocate enough for all CPUs. We do this
769 * by allocating all the pages and storing them on a local
770 * link list. If we succeed in our allocation, then we
771 * add these pages to the cpu_buffers. Otherwise we just free
772 * them all and return -ENOMEM;
773 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400774 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
775 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500776
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400777 new_pages = nr_pages - buffer->pages;
778
779 for_each_buffer_cpu(buffer, cpu) {
780 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500781 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400782 cache_line_size()),
783 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500784 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400785 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500786 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400787 addr = __get_free_page(GFP_KERNEL);
788 if (!addr)
789 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500790 bpage->page = (void *)addr;
791 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400792 }
793 }
794
795 for_each_buffer_cpu(buffer, cpu) {
796 cpu_buffer = buffer->buffers[cpu];
797 rb_insert_pages(cpu_buffer, &pages, new_pages);
798 }
799
Steven Rostedt554f7862009-03-11 22:00:13 -0400800 if (RB_WARN_ON(buffer, !list_empty(&pages)))
801 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400802
803 out:
804 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400805 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400806 mutex_unlock(&buffer->mutex);
807
808 return size;
809
810 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500811 list_for_each_entry_safe(bpage, tmp, &pages, list) {
812 list_del_init(&bpage->list);
813 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400814 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400815 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100816 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400817 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400818
819 /*
820 * Something went totally wrong, and we are too paranoid
821 * to even clean up the mess.
822 */
823 out_fail:
824 put_online_cpus();
825 mutex_unlock(&buffer->mutex);
826 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400827}
Robert Richterc4f50182008-12-11 16:49:22 +0100828EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400829
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400830static inline int rb_null_event(struct ring_buffer_event *event)
831{
832 return event->type == RINGBUF_TYPE_PADDING;
833}
834
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500835static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500836__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500837{
Steven Rostedt044fa782008-12-02 23:50:03 -0500838 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500839}
840
Steven Rostedt044fa782008-12-02 23:50:03 -0500841static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400842{
Steven Rostedt044fa782008-12-02 23:50:03 -0500843 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400844}
845
846static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400847rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400848{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400849 return __rb_page_index(cpu_buffer->reader_page,
850 cpu_buffer->reader_page->read);
851}
852
853static inline struct ring_buffer_event *
854rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
855{
856 return __rb_page_index(cpu_buffer->head_page,
857 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400858}
859
860static inline struct ring_buffer_event *
861rb_iter_head_event(struct ring_buffer_iter *iter)
862{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400863 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400864}
865
Steven Rostedtbf41a152008-10-04 02:00:59 -0400866static inline unsigned rb_page_write(struct buffer_page *bpage)
867{
868 return local_read(&bpage->write);
869}
870
871static inline unsigned rb_page_commit(struct buffer_page *bpage)
872{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500873 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400874}
875
876/* Size is determined by what has been commited */
877static inline unsigned rb_page_size(struct buffer_page *bpage)
878{
879 return rb_page_commit(bpage);
880}
881
882static inline unsigned
883rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
884{
885 return rb_page_commit(cpu_buffer->commit_page);
886}
887
888static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
889{
890 return rb_page_commit(cpu_buffer->head_page);
891}
892
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400893/*
894 * When the tail hits the head and the buffer is in overwrite mode,
895 * the head jumps to the next page and all content on the previous
896 * page is discarded. But before doing so, we update the overrun
897 * variable of the buffer.
898 */
899static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
900{
901 struct ring_buffer_event *event;
902 unsigned long head;
903
904 for (head = 0; head < rb_head_size(cpu_buffer);
905 head += rb_event_length(event)) {
906
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400907 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500908 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
909 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400910 /* Only count data entries */
911 if (event->type != RINGBUF_TYPE_DATA)
912 continue;
913 cpu_buffer->overrun++;
914 cpu_buffer->entries--;
915 }
916}
917
918static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500919 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400920{
Steven Rostedt044fa782008-12-02 23:50:03 -0500921 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400922
923 if (p == &cpu_buffer->pages)
924 p = p->next;
925
Steven Rostedt044fa782008-12-02 23:50:03 -0500926 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400927}
928
Steven Rostedtbf41a152008-10-04 02:00:59 -0400929static inline unsigned
930rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400931{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400932 unsigned long addr = (unsigned long)event;
933
934 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400935}
936
Andrew Morton34a148b2009-01-09 12:27:09 -0800937static int
Steven Rostedtbf41a152008-10-04 02:00:59 -0400938rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
939 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400941 unsigned long addr = (unsigned long)event;
942 unsigned long index;
943
944 index = rb_event_index(event);
945 addr &= PAGE_MASK;
946
947 return cpu_buffer->commit_page->page == (void *)addr &&
948 rb_commit_index(cpu_buffer) == index;
949}
950
Andrew Morton34a148b2009-01-09 12:27:09 -0800951static void
Steven Rostedtbf41a152008-10-04 02:00:59 -0400952rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
953 struct ring_buffer_event *event)
954{
955 unsigned long addr = (unsigned long)event;
956 unsigned long index;
957
958 index = rb_event_index(event);
959 addr &= PAGE_MASK;
960
961 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500962 if (RB_WARN_ON(cpu_buffer,
963 cpu_buffer->commit_page == cpu_buffer->tail_page))
964 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -0500965 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400966 cpu_buffer->commit_page->write;
967 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500968 cpu_buffer->write_stamp =
969 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400970 }
971
972 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500973 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400974}
975
Andrew Morton34a148b2009-01-09 12:27:09 -0800976static void
Steven Rostedtbf41a152008-10-04 02:00:59 -0400977rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
978{
979 /*
980 * We only race with interrupts and NMIs on this CPU.
981 * If we own the commit event, then we can commit
982 * all others that interrupted us, since the interruptions
983 * are in stack format (they finish before they come
984 * back to us). This allows us to do a simple loop to
985 * assign the commit to the tail.
986 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -0500987 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -0400988 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500989 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400990 cpu_buffer->commit_page->write;
991 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500992 cpu_buffer->write_stamp =
993 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400994 /* add barrier to keep gcc from optimizing too much */
995 barrier();
996 }
997 while (rb_commit_index(cpu_buffer) !=
998 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500999 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001000 cpu_buffer->commit_page->write;
1001 barrier();
1002 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001003
1004 /* again, keep gcc from optimizing */
1005 barrier();
1006
1007 /*
1008 * If an interrupt came in just after the first while loop
1009 * and pushed the tail page forward, we will be left with
1010 * a dangling commit that will never go forward.
1011 */
1012 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1013 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001014}
1015
Steven Rostedtd7690412008-10-01 00:29:53 -04001016static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001017{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001018 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001019 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001020}
1021
Andrew Morton34a148b2009-01-09 12:27:09 -08001022static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001023{
1024 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1025
1026 /*
1027 * The iterator could be on the reader page (it starts there).
1028 * But the head could have moved, since the reader was
1029 * found. Check for this case and assign the iterator
1030 * to the head page instead of next.
1031 */
1032 if (iter->head_page == cpu_buffer->reader_page)
1033 iter->head_page = cpu_buffer->head_page;
1034 else
1035 rb_inc_page(cpu_buffer, &iter->head_page);
1036
Steven Rostedtabc9b562008-12-02 15:34:06 -05001037 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001038 iter->head = 0;
1039}
1040
1041/**
1042 * ring_buffer_update_event - update event type and data
1043 * @event: the even to update
1044 * @type: the type of event
1045 * @length: the size of the event field in the ring buffer
1046 *
1047 * Update the type and data fields of the event. The length
1048 * is the actual size that is written to the ring buffer,
1049 * and with this, we can determine what to place into the
1050 * data field.
1051 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001052static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053rb_update_event(struct ring_buffer_event *event,
1054 unsigned type, unsigned length)
1055{
1056 event->type = type;
1057
1058 switch (type) {
1059
1060 case RINGBUF_TYPE_PADDING:
1061 break;
1062
1063 case RINGBUF_TYPE_TIME_EXTEND:
Andrew Morton67d34722009-01-09 12:27:09 -08001064 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065 break;
1066
1067 case RINGBUF_TYPE_TIME_STAMP:
Andrew Morton67d34722009-01-09 12:27:09 -08001068 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001069 break;
1070
1071 case RINGBUF_TYPE_DATA:
1072 length -= RB_EVNT_HDR_SIZE;
1073 if (length > RB_MAX_SMALL_DATA) {
1074 event->len = 0;
1075 event->array[0] = length;
1076 } else
Andrew Morton67d34722009-01-09 12:27:09 -08001077 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001078 break;
1079 default:
1080 BUG();
1081 }
1082}
1083
Andrew Morton34a148b2009-01-09 12:27:09 -08001084static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085{
1086 struct ring_buffer_event event; /* Used only for sizeof array */
1087
1088 /* zero length can cause confusions */
1089 if (!length)
1090 length = 1;
1091
1092 if (length > RB_MAX_SMALL_DATA)
1093 length += sizeof(event.array[0]);
1094
1095 length += RB_EVNT_HDR_SIZE;
1096 length = ALIGN(length, RB_ALIGNMENT);
1097
1098 return length;
1099}
1100
1101static struct ring_buffer_event *
1102__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1103 unsigned type, unsigned long length, u64 *ts)
1104{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001105 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001106 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001107 struct ring_buffer *buffer = cpu_buffer->buffer;
1108 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001109 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001110 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001111
Steven Rostedt98db8df2008-12-23 11:32:25 -05001112 commit_page = cpu_buffer->commit_page;
1113 /* we just need to protect against interrupts */
1114 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001115 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001116 write = local_add_return(length, &tail_page->write);
1117 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118
Steven Rostedtbf41a152008-10-04 02:00:59 -04001119 /* See if we shot pass the end of this buffer page */
1120 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121 struct buffer_page *next_page = tail_page;
1122
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001123 local_irq_save(flags);
Steven Rostedt78d904b2009-02-05 18:43:07 -05001124 /*
Steven Rostedta81bd802009-02-06 01:45:16 -05001125 * Since the write to the buffer is still not
1126 * fully lockless, we must be careful with NMIs.
1127 * The locks in the writers are taken when a write
1128 * crosses to a new page. The locks protect against
1129 * races with the readers (this will soon be fixed
1130 * with a lockless solution).
1131 *
1132 * Because we can not protect against NMIs, and we
1133 * want to keep traces reentrant, we need to manage
1134 * what happens when we are in an NMI.
1135 *
Steven Rostedt78d904b2009-02-05 18:43:07 -05001136 * NMIs can happen after we take the lock.
1137 * If we are in an NMI, only take the lock
1138 * if it is not already taken. Otherwise
1139 * simply fail.
1140 */
Steven Rostedta81bd802009-02-06 01:45:16 -05001141 if (unlikely(in_nmi())) {
Steven Rostedt78d904b2009-02-05 18:43:07 -05001142 if (!__raw_spin_trylock(&cpu_buffer->lock))
Steven Rostedt45141d42009-02-12 13:19:48 -05001143 goto out_reset;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001144 } else
1145 __raw_spin_lock(&cpu_buffer->lock);
1146
1147 lock_taken = true;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001148
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001149 rb_inc_page(cpu_buffer, &next_page);
1150
Steven Rostedtd7690412008-10-01 00:29:53 -04001151 head_page = cpu_buffer->head_page;
1152 reader_page = cpu_buffer->reader_page;
1153
1154 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001155 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
Steven Rostedt45141d42009-02-12 13:19:48 -05001156 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001157
1158 /*
1159 * If for some reason, we had an interrupt storm that made
1160 * it all the way around the buffer, bail, and warn
1161 * about it.
1162 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001163 if (unlikely(next_page == commit_page)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001164 WARN_ON_ONCE(1);
Steven Rostedt45141d42009-02-12 13:19:48 -05001165 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001166 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001167
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001168 if (next_page == head_page) {
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001169 if (!(buffer->flags & RB_FL_OVERWRITE))
Steven Rostedt45141d42009-02-12 13:19:48 -05001170 goto out_reset;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001171
Steven Rostedtbf41a152008-10-04 02:00:59 -04001172 /* tail_page has not moved yet? */
1173 if (tail_page == cpu_buffer->tail_page) {
1174 /* count overflows */
1175 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001176
Steven Rostedtbf41a152008-10-04 02:00:59 -04001177 rb_inc_page(cpu_buffer, &head_page);
1178 cpu_buffer->head_page = head_page;
1179 cpu_buffer->head_page->read = 0;
1180 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181 }
1182
Steven Rostedtbf41a152008-10-04 02:00:59 -04001183 /*
1184 * If the tail page is still the same as what we think
1185 * it is, then it is up to us to update the tail
1186 * pointer.
1187 */
1188 if (tail_page == cpu_buffer->tail_page) {
1189 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001190 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001191 cpu_buffer->tail_page = next_page;
1192
1193 /* reread the time stamp */
1194 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001195 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001196 }
1197
1198 /*
1199 * The actual tail page has moved forward.
1200 */
1201 if (tail < BUF_PAGE_SIZE) {
1202 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001203 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001204 event->type = RINGBUF_TYPE_PADDING;
1205 }
1206
Steven Rostedtbf41a152008-10-04 02:00:59 -04001207 if (tail <= BUF_PAGE_SIZE)
1208 /* Set the write back to the previous setting */
1209 local_set(&tail_page->write, tail);
1210
1211 /*
1212 * If this was a commit entry that failed,
1213 * increment that too
1214 */
1215 if (tail_page == cpu_buffer->commit_page &&
1216 tail == rb_commit_index(cpu_buffer)) {
1217 rb_set_commit_to_write(cpu_buffer);
1218 }
1219
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001220 __raw_spin_unlock(&cpu_buffer->lock);
1221 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001222
1223 /* fail and let the caller try again */
1224 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001225 }
1226
Steven Rostedtbf41a152008-10-04 02:00:59 -04001227 /* We reserved something on the buffer */
1228
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001229 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1230 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001232 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001233 rb_update_event(event, type, length);
1234
Steven Rostedtbf41a152008-10-04 02:00:59 -04001235 /*
1236 * If this is a commit and the tail is zero, then update
1237 * this page's time stamp.
1238 */
1239 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001240 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001241
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001243
Steven Rostedt45141d42009-02-12 13:19:48 -05001244 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001245 /* reset write */
1246 if (tail <= BUF_PAGE_SIZE)
1247 local_set(&tail_page->write, tail);
1248
Steven Rostedt78d904b2009-02-05 18:43:07 -05001249 if (likely(lock_taken))
1250 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001251 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001252 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001253}
1254
1255static int
1256rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1257 u64 *ts, u64 *delta)
1258{
1259 struct ring_buffer_event *event;
1260 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001261 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262
1263 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1264 printk(KERN_WARNING "Delta way too big! %llu"
1265 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001266 (unsigned long long)*delta,
1267 (unsigned long long)*ts,
1268 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269 WARN_ON(1);
1270 }
1271
1272 /*
1273 * The delta is too big, we to add a
1274 * new timestamp.
1275 */
1276 event = __rb_reserve_next(cpu_buffer,
1277 RINGBUF_TYPE_TIME_EXTEND,
1278 RB_LEN_TIME_EXTEND,
1279 ts);
1280 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001281 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001282
Steven Rostedtbf41a152008-10-04 02:00:59 -04001283 if (PTR_ERR(event) == -EAGAIN)
1284 return -EAGAIN;
1285
1286 /* Only a commited time event can update the write stamp */
1287 if (rb_is_commit(cpu_buffer, event)) {
1288 /*
1289 * If this is the first on the page, then we need to
1290 * update the page itself, and just put in a zero.
1291 */
1292 if (rb_event_index(event)) {
1293 event->time_delta = *delta & TS_MASK;
1294 event->array[0] = *delta >> TS_SHIFT;
1295 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001296 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001297 event->time_delta = 0;
1298 event->array[0] = 0;
1299 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001300 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001301 /* let the caller know this was the commit */
1302 ret = 1;
1303 } else {
1304 /* Darn, this is just wasted space */
1305 event->time_delta = 0;
1306 event->array[0] = 0;
1307 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001308 }
1309
Steven Rostedtbf41a152008-10-04 02:00:59 -04001310 *delta = 0;
1311
1312 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001313}
1314
1315static struct ring_buffer_event *
1316rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1317 unsigned type, unsigned long length)
1318{
1319 struct ring_buffer_event *event;
1320 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001321 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001322 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001323
Steven Rostedtbf41a152008-10-04 02:00:59 -04001324 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001325 /*
1326 * We allow for interrupts to reenter here and do a trace.
1327 * If one does, it will cause this original code to loop
1328 * back here. Even with heavy interrupts happening, this
1329 * should only happen a few times in a row. If this happens
1330 * 1000 times in a row, there must be either an interrupt
1331 * storm or we have something buggy.
1332 * Bail!
1333 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001334 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001335 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001336
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001337 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1338
Steven Rostedtbf41a152008-10-04 02:00:59 -04001339 /*
1340 * Only the first commit can update the timestamp.
1341 * Yes there is a race here. If an interrupt comes in
1342 * just after the conditional and it traces too, then it
1343 * will also check the deltas. More than one timestamp may
1344 * also be made. But only the entry that did the actual
1345 * commit will be something other than zero.
1346 */
1347 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1348 rb_page_write(cpu_buffer->tail_page) ==
1349 rb_commit_index(cpu_buffer)) {
1350
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001351 delta = ts - cpu_buffer->write_stamp;
1352
Steven Rostedtbf41a152008-10-04 02:00:59 -04001353 /* make sure this delta is calculated here */
1354 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001355
Steven Rostedtbf41a152008-10-04 02:00:59 -04001356 /* Did the write stamp get updated already? */
1357 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001358 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001359
1360 if (test_time_stamp(delta)) {
1361
1362 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1363
1364 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001365 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001366
1367 if (commit == -EAGAIN)
1368 goto again;
1369
1370 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001371 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001372 } else
1373 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375
1376 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001377 if (PTR_ERR(event) == -EAGAIN)
1378 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379
Steven Rostedtbf41a152008-10-04 02:00:59 -04001380 if (!event) {
1381 if (unlikely(commit))
1382 /*
1383 * Ouch! We needed a timestamp and it was commited. But
1384 * we didn't get our event reserved.
1385 */
1386 rb_set_commit_to_write(cpu_buffer);
1387 return NULL;
1388 }
1389
1390 /*
1391 * If the timestamp was commited, make the commit our entry
1392 * now so that we will update it when needed.
1393 */
1394 if (commit)
1395 rb_set_commit_event(cpu_buffer, event);
1396 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001397 delta = 0;
1398
1399 event->time_delta = delta;
1400
1401 return event;
1402}
1403
Steven Rostedtbf41a152008-10-04 02:00:59 -04001404static DEFINE_PER_CPU(int, rb_need_resched);
1405
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001406/**
1407 * ring_buffer_lock_reserve - reserve a part of the buffer
1408 * @buffer: the ring buffer to reserve from
1409 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001410 *
1411 * Returns a reseverd event on the ring buffer to copy directly to.
1412 * The user of this interface will need to get the body to write into
1413 * and can use the ring_buffer_event_data() interface.
1414 *
1415 * The length is the length of the data needed, not the event length
1416 * which also includes the event header.
1417 *
1418 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1419 * If NULL is returned, then nothing has been allocated or locked.
1420 */
1421struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001422ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001423{
1424 struct ring_buffer_per_cpu *cpu_buffer;
1425 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001426 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001427
Steven Rostedt033601a2008-11-21 12:41:55 -05001428 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001429 return NULL;
1430
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431 if (atomic_read(&buffer->record_disabled))
1432 return NULL;
1433
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001435 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001436
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001437 cpu = raw_smp_processor_id();
1438
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301439 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001440 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001441
1442 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001443
1444 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001445 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001446
1447 length = rb_calculate_event_length(length);
1448 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001449 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001450
1451 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1452 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001453 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454
Steven Rostedtbf41a152008-10-04 02:00:59 -04001455 /*
1456 * Need to store resched state on this cpu.
1457 * Only the first needs to.
1458 */
1459
1460 if (preempt_count() == 1)
1461 per_cpu(rb_need_resched, cpu) = resched;
1462
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001463 return event;
1464
Steven Rostedtd7690412008-10-01 00:29:53 -04001465 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001466 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001467 return NULL;
1468}
Robert Richterc4f50182008-12-11 16:49:22 +01001469EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001470
1471static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1472 struct ring_buffer_event *event)
1473{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001474 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001475
1476 /* Only process further if we own the commit */
1477 if (!rb_is_commit(cpu_buffer, event))
1478 return;
1479
1480 cpu_buffer->write_stamp += event->time_delta;
1481
1482 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001483}
1484
1485/**
1486 * ring_buffer_unlock_commit - commit a reserved
1487 * @buffer: The buffer to commit to
1488 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001489 *
1490 * This commits the data to the ring buffer, and releases any locks held.
1491 *
1492 * Must be paired with ring_buffer_lock_reserve.
1493 */
1494int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001495 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001496{
1497 struct ring_buffer_per_cpu *cpu_buffer;
1498 int cpu = raw_smp_processor_id();
1499
1500 cpu_buffer = buffer->buffers[cpu];
1501
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001502 rb_commit(cpu_buffer, event);
1503
Steven Rostedtbf41a152008-10-04 02:00:59 -04001504 /*
1505 * Only the last preempt count needs to restore preemption.
1506 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001507 if (preempt_count() == 1)
1508 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1509 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001510 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001511
1512 return 0;
1513}
Robert Richterc4f50182008-12-11 16:49:22 +01001514EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001515
1516/**
1517 * ring_buffer_write - write data to the buffer without reserving
1518 * @buffer: The ring buffer to write to.
1519 * @length: The length of the data being written (excluding the event header)
1520 * @data: The data to write to the buffer.
1521 *
1522 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1523 * one function. If you already have the data to write to the buffer, it
1524 * may be easier to simply call this function.
1525 *
1526 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1527 * and not the length of the event which would hold the header.
1528 */
1529int ring_buffer_write(struct ring_buffer *buffer,
1530 unsigned long length,
1531 void *data)
1532{
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001535 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536 void *body;
1537 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001538 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001539
Steven Rostedt033601a2008-11-21 12:41:55 -05001540 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001541 return -EBUSY;
1542
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001543 if (atomic_read(&buffer->record_disabled))
1544 return -EBUSY;
1545
Steven Rostedt182e9f52008-11-03 23:15:56 -05001546 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001547
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001548 cpu = raw_smp_processor_id();
1549
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301550 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001551 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001552
1553 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554
1555 if (atomic_read(&cpu_buffer->record_disabled))
1556 goto out;
1557
1558 event_length = rb_calculate_event_length(length);
1559 event = rb_reserve_next_event(cpu_buffer,
1560 RINGBUF_TYPE_DATA, event_length);
1561 if (!event)
1562 goto out;
1563
1564 body = rb_event_data(event);
1565
1566 memcpy(body, data, length);
1567
1568 rb_commit(cpu_buffer, event);
1569
1570 ret = 0;
1571 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001572 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001573
1574 return ret;
1575}
Robert Richterc4f50182008-12-11 16:49:22 +01001576EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001577
Andrew Morton34a148b2009-01-09 12:27:09 -08001578static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001579{
1580 struct buffer_page *reader = cpu_buffer->reader_page;
1581 struct buffer_page *head = cpu_buffer->head_page;
1582 struct buffer_page *commit = cpu_buffer->commit_page;
1583
1584 return reader->read == rb_page_commit(reader) &&
1585 (commit == reader ||
1586 (commit == head &&
1587 head->read == rb_page_commit(commit)));
1588}
1589
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001590/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591 * ring_buffer_record_disable - stop all writes into the buffer
1592 * @buffer: The ring buffer to stop writes to.
1593 *
1594 * This prevents all writes to the buffer. Any attempt to write
1595 * to the buffer after this will fail and return NULL.
1596 *
1597 * The caller should call synchronize_sched() after this.
1598 */
1599void ring_buffer_record_disable(struct ring_buffer *buffer)
1600{
1601 atomic_inc(&buffer->record_disabled);
1602}
Robert Richterc4f50182008-12-11 16:49:22 +01001603EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001604
1605/**
1606 * ring_buffer_record_enable - enable writes to the buffer
1607 * @buffer: The ring buffer to enable writes
1608 *
1609 * Note, multiple disables will need the same number of enables
1610 * to truely enable the writing (much like preempt_disable).
1611 */
1612void ring_buffer_record_enable(struct ring_buffer *buffer)
1613{
1614 atomic_dec(&buffer->record_disabled);
1615}
Robert Richterc4f50182008-12-11 16:49:22 +01001616EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001617
1618/**
1619 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1620 * @buffer: The ring buffer to stop writes to.
1621 * @cpu: The CPU buffer to stop
1622 *
1623 * This prevents all writes to the buffer. Any attempt to write
1624 * to the buffer after this will fail and return NULL.
1625 *
1626 * The caller should call synchronize_sched() after this.
1627 */
1628void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1629{
1630 struct ring_buffer_per_cpu *cpu_buffer;
1631
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301632 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001633 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001634
1635 cpu_buffer = buffer->buffers[cpu];
1636 atomic_inc(&cpu_buffer->record_disabled);
1637}
Robert Richterc4f50182008-12-11 16:49:22 +01001638EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001639
1640/**
1641 * ring_buffer_record_enable_cpu - enable writes to the buffer
1642 * @buffer: The ring buffer to enable writes
1643 * @cpu: The CPU to enable.
1644 *
1645 * Note, multiple disables will need the same number of enables
1646 * to truely enable the writing (much like preempt_disable).
1647 */
1648void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1649{
1650 struct ring_buffer_per_cpu *cpu_buffer;
1651
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301652 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001653 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654
1655 cpu_buffer = buffer->buffers[cpu];
1656 atomic_dec(&cpu_buffer->record_disabled);
1657}
Robert Richterc4f50182008-12-11 16:49:22 +01001658EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001659
1660/**
1661 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1662 * @buffer: The ring buffer
1663 * @cpu: The per CPU buffer to get the entries from.
1664 */
1665unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1666{
1667 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001668 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001669
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001671 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001672
1673 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001674 ret = cpu_buffer->entries;
Steven Rostedt554f7862009-03-11 22:00:13 -04001675
1676 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001677}
Robert Richterc4f50182008-12-11 16:49:22 +01001678EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001679
1680/**
1681 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1682 * @buffer: The ring buffer
1683 * @cpu: The per CPU buffer to get the number of overruns from
1684 */
1685unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1686{
1687 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001688 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001689
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301690 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001691 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001692
1693 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001694 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001695
1696 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001697}
Robert Richterc4f50182008-12-11 16:49:22 +01001698EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001699
1700/**
1701 * ring_buffer_entries - get the number of entries in a buffer
1702 * @buffer: The ring buffer
1703 *
1704 * Returns the total number of entries in the ring buffer
1705 * (all CPU entries)
1706 */
1707unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1708{
1709 struct ring_buffer_per_cpu *cpu_buffer;
1710 unsigned long entries = 0;
1711 int cpu;
1712
1713 /* if you care about this being correct, lock the buffer */
1714 for_each_buffer_cpu(buffer, cpu) {
1715 cpu_buffer = buffer->buffers[cpu];
1716 entries += cpu_buffer->entries;
1717 }
1718
1719 return entries;
1720}
Robert Richterc4f50182008-12-11 16:49:22 +01001721EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001722
1723/**
1724 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1725 * @buffer: The ring buffer
1726 *
1727 * Returns the total number of overruns in the ring buffer
1728 * (all CPU entries)
1729 */
1730unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1731{
1732 struct ring_buffer_per_cpu *cpu_buffer;
1733 unsigned long overruns = 0;
1734 int cpu;
1735
1736 /* if you care about this being correct, lock the buffer */
1737 for_each_buffer_cpu(buffer, cpu) {
1738 cpu_buffer = buffer->buffers[cpu];
1739 overruns += cpu_buffer->overrun;
1740 }
1741
1742 return overruns;
1743}
Robert Richterc4f50182008-12-11 16:49:22 +01001744EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001745
Steven Rostedt642edba2008-11-12 00:01:26 -05001746static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001747{
1748 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1749
Steven Rostedtd7690412008-10-01 00:29:53 -04001750 /* Iterator usage is expected to have record disabled */
1751 if (list_empty(&cpu_buffer->reader_page->list)) {
1752 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001753 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001754 } else {
1755 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001756 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001757 }
1758 if (iter->head)
1759 iter->read_stamp = cpu_buffer->read_stamp;
1760 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05001761 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001762}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001763
Steven Rostedt642edba2008-11-12 00:01:26 -05001764/**
1765 * ring_buffer_iter_reset - reset an iterator
1766 * @iter: The iterator to reset
1767 *
1768 * Resets the iterator, so that it will start from the beginning
1769 * again.
1770 */
1771void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1772{
Steven Rostedt554f7862009-03-11 22:00:13 -04001773 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05001774 unsigned long flags;
1775
Steven Rostedt554f7862009-03-11 22:00:13 -04001776 if (!iter)
1777 return;
1778
1779 cpu_buffer = iter->cpu_buffer;
1780
Steven Rostedt642edba2008-11-12 00:01:26 -05001781 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1782 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001783 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001784}
Robert Richterc4f50182008-12-11 16:49:22 +01001785EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001786
1787/**
1788 * ring_buffer_iter_empty - check if an iterator has no more to read
1789 * @iter: The iterator to check
1790 */
1791int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1792{
1793 struct ring_buffer_per_cpu *cpu_buffer;
1794
1795 cpu_buffer = iter->cpu_buffer;
1796
Steven Rostedtbf41a152008-10-04 02:00:59 -04001797 return iter->head_page == cpu_buffer->commit_page &&
1798 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001799}
Robert Richterc4f50182008-12-11 16:49:22 +01001800EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001801
1802static void
1803rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1804 struct ring_buffer_event *event)
1805{
1806 u64 delta;
1807
1808 switch (event->type) {
1809 case RINGBUF_TYPE_PADDING:
1810 return;
1811
1812 case RINGBUF_TYPE_TIME_EXTEND:
1813 delta = event->array[0];
1814 delta <<= TS_SHIFT;
1815 delta += event->time_delta;
1816 cpu_buffer->read_stamp += delta;
1817 return;
1818
1819 case RINGBUF_TYPE_TIME_STAMP:
1820 /* FIXME: not implemented */
1821 return;
1822
1823 case RINGBUF_TYPE_DATA:
1824 cpu_buffer->read_stamp += event->time_delta;
1825 return;
1826
1827 default:
1828 BUG();
1829 }
1830 return;
1831}
1832
1833static void
1834rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1835 struct ring_buffer_event *event)
1836{
1837 u64 delta;
1838
1839 switch (event->type) {
1840 case RINGBUF_TYPE_PADDING:
1841 return;
1842
1843 case RINGBUF_TYPE_TIME_EXTEND:
1844 delta = event->array[0];
1845 delta <<= TS_SHIFT;
1846 delta += event->time_delta;
1847 iter->read_stamp += delta;
1848 return;
1849
1850 case RINGBUF_TYPE_TIME_STAMP:
1851 /* FIXME: not implemented */
1852 return;
1853
1854 case RINGBUF_TYPE_DATA:
1855 iter->read_stamp += event->time_delta;
1856 return;
1857
1858 default:
1859 BUG();
1860 }
1861 return;
1862}
1863
Steven Rostedtd7690412008-10-01 00:29:53 -04001864static struct buffer_page *
1865rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001866{
Steven Rostedtd7690412008-10-01 00:29:53 -04001867 struct buffer_page *reader = NULL;
1868 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001869 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001870
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001871 local_irq_save(flags);
1872 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04001873
1874 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001875 /*
1876 * This should normally only loop twice. But because the
1877 * start of the reader inserts an empty page, it causes
1878 * a case where we will loop three times. There should be no
1879 * reason to loop four times (that I know of).
1880 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001881 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001882 reader = NULL;
1883 goto out;
1884 }
1885
Steven Rostedtd7690412008-10-01 00:29:53 -04001886 reader = cpu_buffer->reader_page;
1887
1888 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001889 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04001890 goto out;
1891
1892 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001893 if (RB_WARN_ON(cpu_buffer,
1894 cpu_buffer->reader_page->read > rb_page_size(reader)))
1895 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04001896
1897 /* check if we caught up to the tail */
1898 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001899 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04001900 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001901
1902 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04001903 * Splice the empty reader page into the list around the head.
1904 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001905 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906
Steven Rostedtd7690412008-10-01 00:29:53 -04001907 reader = cpu_buffer->head_page;
1908 cpu_buffer->reader_page->list.next = reader->list.next;
1909 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001910
1911 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001912 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001913
1914 /* Make the reader page now replace the head */
1915 reader->list.prev->next = &cpu_buffer->reader_page->list;
1916 reader->list.next->prev = &cpu_buffer->reader_page->list;
1917
1918 /*
1919 * If the tail is on the reader, then we must set the head
1920 * to the inserted page, otherwise we set it one before.
1921 */
1922 cpu_buffer->head_page = cpu_buffer->reader_page;
1923
Steven Rostedtbf41a152008-10-04 02:00:59 -04001924 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04001925 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1926
1927 /* Finally update the reader page to the new head */
1928 cpu_buffer->reader_page = reader;
1929 rb_reset_reader_page(cpu_buffer);
1930
1931 goto again;
1932
1933 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001934 __raw_spin_unlock(&cpu_buffer->lock);
1935 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04001936
1937 return reader;
1938}
1939
1940static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1941{
1942 struct ring_buffer_event *event;
1943 struct buffer_page *reader;
1944 unsigned length;
1945
1946 reader = rb_get_reader_page(cpu_buffer);
1947
1948 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001949 if (RB_WARN_ON(cpu_buffer, !reader))
1950 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001951
1952 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001953
1954 if (event->type == RINGBUF_TYPE_DATA)
1955 cpu_buffer->entries--;
1956
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001957 rb_update_read_stamp(cpu_buffer, event);
1958
Steven Rostedtd7690412008-10-01 00:29:53 -04001959 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001960 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001961}
1962
1963static void rb_advance_iter(struct ring_buffer_iter *iter)
1964{
1965 struct ring_buffer *buffer;
1966 struct ring_buffer_per_cpu *cpu_buffer;
1967 struct ring_buffer_event *event;
1968 unsigned length;
1969
1970 cpu_buffer = iter->cpu_buffer;
1971 buffer = cpu_buffer->buffer;
1972
1973 /*
1974 * Check if we are at the end of the buffer.
1975 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001976 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001977 if (RB_WARN_ON(buffer,
1978 iter->head_page == cpu_buffer->commit_page))
1979 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001980 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001981 return;
1982 }
1983
1984 event = rb_iter_head_event(iter);
1985
1986 length = rb_event_length(event);
1987
1988 /*
1989 * This should not be called to advance the header if we are
1990 * at the tail of the buffer.
1991 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001992 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001993 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001994 (iter->head + length > rb_commit_index(cpu_buffer))))
1995 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001996
1997 rb_update_iter_read_stamp(iter, event);
1998
1999 iter->head += length;
2000
2001 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002002 if ((iter->head >= rb_page_size(iter->head_page)) &&
2003 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004 rb_advance_iter(iter);
2005}
2006
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002007static struct ring_buffer_event *
2008rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002009{
2010 struct ring_buffer_per_cpu *cpu_buffer;
2011 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002012 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002013 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002014
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002015 cpu_buffer = buffer->buffers[cpu];
2016
2017 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002018 /*
2019 * We repeat when a timestamp is encountered. It is possible
2020 * to get multiple timestamps from an interrupt entering just
2021 * as one timestamp is about to be written. The max times
2022 * that this can happen is the number of nested interrupts we
2023 * can have. Nesting 10 deep of interrupts is clearly
2024 * an anomaly.
2025 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002026 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002027 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002028
Steven Rostedtd7690412008-10-01 00:29:53 -04002029 reader = rb_get_reader_page(cpu_buffer);
2030 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002031 return NULL;
2032
Steven Rostedtd7690412008-10-01 00:29:53 -04002033 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002034
2035 switch (event->type) {
2036 case RINGBUF_TYPE_PADDING:
Steven Rostedtbf41a152008-10-04 02:00:59 -04002037 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedtd7690412008-10-01 00:29:53 -04002038 rb_advance_reader(cpu_buffer);
2039 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002040
2041 case RINGBUF_TYPE_TIME_EXTEND:
2042 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002043 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002044 goto again;
2045
2046 case RINGBUF_TYPE_TIME_STAMP:
2047 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002048 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002049 goto again;
2050
2051 case RINGBUF_TYPE_DATA:
2052 if (ts) {
2053 *ts = cpu_buffer->read_stamp + event->time_delta;
2054 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2055 }
2056 return event;
2057
2058 default:
2059 BUG();
2060 }
2061
2062 return NULL;
2063}
Robert Richterc4f50182008-12-11 16:49:22 +01002064EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002065
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002066static struct ring_buffer_event *
2067rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002068{
2069 struct ring_buffer *buffer;
2070 struct ring_buffer_per_cpu *cpu_buffer;
2071 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002072 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002073
2074 if (ring_buffer_iter_empty(iter))
2075 return NULL;
2076
2077 cpu_buffer = iter->cpu_buffer;
2078 buffer = cpu_buffer->buffer;
2079
2080 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002081 /*
2082 * We repeat when a timestamp is encountered. It is possible
2083 * to get multiple timestamps from an interrupt entering just
2084 * as one timestamp is about to be written. The max times
2085 * that this can happen is the number of nested interrupts we
2086 * can have. Nesting 10 deep of interrupts is clearly
2087 * an anomaly.
2088 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002089 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002090 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002091
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002092 if (rb_per_cpu_empty(cpu_buffer))
2093 return NULL;
2094
2095 event = rb_iter_head_event(iter);
2096
2097 switch (event->type) {
2098 case RINGBUF_TYPE_PADDING:
Steven Rostedtd7690412008-10-01 00:29:53 -04002099 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002100 goto again;
2101
2102 case RINGBUF_TYPE_TIME_EXTEND:
2103 /* Internal data, OK to advance */
2104 rb_advance_iter(iter);
2105 goto again;
2106
2107 case RINGBUF_TYPE_TIME_STAMP:
2108 /* FIXME: not implemented */
2109 rb_advance_iter(iter);
2110 goto again;
2111
2112 case RINGBUF_TYPE_DATA:
2113 if (ts) {
2114 *ts = iter->read_stamp + event->time_delta;
2115 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2116 }
2117 return event;
2118
2119 default:
2120 BUG();
2121 }
2122
2123 return NULL;
2124}
Robert Richterc4f50182008-12-11 16:49:22 +01002125EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002126
2127/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002128 * ring_buffer_peek - peek at the next event to be read
2129 * @buffer: The ring buffer to read
2130 * @cpu: The cpu to peak at
2131 * @ts: The timestamp counter of this event.
2132 *
2133 * This will return the event that will be read next, but does
2134 * not consume the data.
2135 */
2136struct ring_buffer_event *
2137ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2138{
2139 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002140 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002141 unsigned long flags;
2142
Steven Rostedt554f7862009-03-11 22:00:13 -04002143 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002144 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002145
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002146 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2147 event = rb_buffer_peek(buffer, cpu, ts);
2148 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2149
2150 return event;
2151}
2152
2153/**
2154 * ring_buffer_iter_peek - peek at the next event to be read
2155 * @iter: The ring buffer iterator
2156 * @ts: The timestamp counter of this event.
2157 *
2158 * This will return the event that will be read next, but does
2159 * not increment the iterator.
2160 */
2161struct ring_buffer_event *
2162ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2163{
2164 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2165 struct ring_buffer_event *event;
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2169 event = rb_iter_peek(iter, ts);
2170 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2171
2172 return event;
2173}
2174
2175/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002176 * ring_buffer_consume - return an event and consume it
2177 * @buffer: The ring buffer to get the next event from
2178 *
2179 * Returns the next event in the ring buffer, and that event is consumed.
2180 * Meaning, that sequential reads will keep returning a different event,
2181 * and eventually empty the ring buffer if the producer is slower.
2182 */
2183struct ring_buffer_event *
2184ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2185{
Steven Rostedt554f7862009-03-11 22:00:13 -04002186 struct ring_buffer_per_cpu *cpu_buffer;
2187 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002188 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189
Steven Rostedt554f7862009-03-11 22:00:13 -04002190 /* might be called in atomic */
2191 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002192
Steven Rostedt554f7862009-03-11 22:00:13 -04002193 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2194 goto out;
2195
2196 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002197 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002199 event = rb_buffer_peek(buffer, cpu, ts);
2200 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002201 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002202
Steven Rostedtd7690412008-10-01 00:29:53 -04002203 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002204
Steven Rostedt554f7862009-03-11 22:00:13 -04002205 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002206 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2207
Steven Rostedt554f7862009-03-11 22:00:13 -04002208 out:
2209 preempt_enable();
2210
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002211 return event;
2212}
Robert Richterc4f50182008-12-11 16:49:22 +01002213EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002214
2215/**
2216 * ring_buffer_read_start - start a non consuming read of the buffer
2217 * @buffer: The ring buffer to read from
2218 * @cpu: The cpu buffer to iterate over
2219 *
2220 * This starts up an iteration through the buffer. It also disables
2221 * the recording to the buffer until the reading is finished.
2222 * This prevents the reading from being corrupted. This is not
2223 * a consuming read, so a producer is not expected.
2224 *
2225 * Must be paired with ring_buffer_finish.
2226 */
2227struct ring_buffer_iter *
2228ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2229{
2230 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002231 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002232 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002233
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302234 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002235 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002236
2237 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2238 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002239 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240
2241 cpu_buffer = buffer->buffers[cpu];
2242
2243 iter->cpu_buffer = cpu_buffer;
2244
2245 atomic_inc(&cpu_buffer->record_disabled);
2246 synchronize_sched();
2247
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002248 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002249 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002250 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002251 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002252 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253
2254 return iter;
2255}
Robert Richterc4f50182008-12-11 16:49:22 +01002256EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257
2258/**
2259 * ring_buffer_finish - finish reading the iterator of the buffer
2260 * @iter: The iterator retrieved by ring_buffer_start
2261 *
2262 * This re-enables the recording to the buffer, and frees the
2263 * iterator.
2264 */
2265void
2266ring_buffer_read_finish(struct ring_buffer_iter *iter)
2267{
2268 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2269
2270 atomic_dec(&cpu_buffer->record_disabled);
2271 kfree(iter);
2272}
Robert Richterc4f50182008-12-11 16:49:22 +01002273EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002274
2275/**
2276 * ring_buffer_read - read the next item in the ring buffer by the iterator
2277 * @iter: The ring buffer iterator
2278 * @ts: The time stamp of the event read.
2279 *
2280 * This reads the next event in the ring buffer and increments the iterator.
2281 */
2282struct ring_buffer_event *
2283ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2284{
2285 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002286 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2287 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002289 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2290 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002291 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002292 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002293
2294 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002295 out:
2296 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002297
2298 return event;
2299}
Robert Richterc4f50182008-12-11 16:49:22 +01002300EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002301
2302/**
2303 * ring_buffer_size - return the size of the ring buffer (in bytes)
2304 * @buffer: The ring buffer.
2305 */
2306unsigned long ring_buffer_size(struct ring_buffer *buffer)
2307{
2308 return BUF_PAGE_SIZE * buffer->pages;
2309}
Robert Richterc4f50182008-12-11 16:49:22 +01002310EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002311
2312static void
2313rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2314{
2315 cpu_buffer->head_page
2316 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002317 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002318 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002319
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002320 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002321
2322 cpu_buffer->tail_page = cpu_buffer->head_page;
2323 cpu_buffer->commit_page = cpu_buffer->head_page;
2324
2325 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2326 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002327 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002328 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002329
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002330 cpu_buffer->overrun = 0;
2331 cpu_buffer->entries = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05002332
2333 cpu_buffer->write_stamp = 0;
2334 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002335}
2336
2337/**
2338 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2339 * @buffer: The ring buffer to reset a per cpu buffer of
2340 * @cpu: The CPU buffer to be reset
2341 */
2342void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2343{
2344 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2345 unsigned long flags;
2346
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302347 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002348 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002349
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002350 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2351
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002352 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002353
2354 rb_reset_cpu(cpu_buffer);
2355
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002356 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002357
2358 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002359}
Robert Richterc4f50182008-12-11 16:49:22 +01002360EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002361
2362/**
2363 * ring_buffer_reset - reset a ring buffer
2364 * @buffer: The ring buffer to reset all cpu buffers
2365 */
2366void ring_buffer_reset(struct ring_buffer *buffer)
2367{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002368 int cpu;
2369
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002370 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002371 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002372}
Robert Richterc4f50182008-12-11 16:49:22 +01002373EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002374
2375/**
2376 * rind_buffer_empty - is the ring buffer empty?
2377 * @buffer: The ring buffer to test
2378 */
2379int ring_buffer_empty(struct ring_buffer *buffer)
2380{
2381 struct ring_buffer_per_cpu *cpu_buffer;
2382 int cpu;
2383
2384 /* yes this is racy, but if you don't like the race, lock the buffer */
2385 for_each_buffer_cpu(buffer, cpu) {
2386 cpu_buffer = buffer->buffers[cpu];
2387 if (!rb_per_cpu_empty(cpu_buffer))
2388 return 0;
2389 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002391 return 1;
2392}
Robert Richterc4f50182008-12-11 16:49:22 +01002393EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002394
2395/**
2396 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2397 * @buffer: The ring buffer
2398 * @cpu: The CPU buffer to test
2399 */
2400int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2401{
2402 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002403 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002404
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302405 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002406 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002407
2408 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002409 ret = rb_per_cpu_empty(cpu_buffer);
2410
Steven Rostedt554f7862009-03-11 22:00:13 -04002411
2412 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002413}
Robert Richterc4f50182008-12-11 16:49:22 +01002414EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002415
2416/**
2417 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2418 * @buffer_a: One buffer to swap with
2419 * @buffer_b: The other buffer to swap with
2420 *
2421 * This function is useful for tracers that want to take a "snapshot"
2422 * of a CPU buffer and has another back up buffer lying around.
2423 * it is expected that the tracer handles the cpu buffer not being
2424 * used at the moment.
2425 */
2426int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2427 struct ring_buffer *buffer_b, int cpu)
2428{
2429 struct ring_buffer_per_cpu *cpu_buffer_a;
2430 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002431 int ret = -EINVAL;
2432
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302433 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2434 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002435 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002436
2437 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002438 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002439 goto out;
2440
2441 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002442
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002443 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002444 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002445
2446 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002447 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002448
2449 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002450 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002451
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002452 cpu_buffer_a = buffer_a->buffers[cpu];
2453 cpu_buffer_b = buffer_b->buffers[cpu];
2454
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002455 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002456 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002457
2458 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002459 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002460
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002461 /*
2462 * We can't do a synchronize_sched here because this
2463 * function can be called in atomic context.
2464 * Normally this will be called from the same CPU as cpu.
2465 * If not it's up to the caller to protect this.
2466 */
2467 atomic_inc(&cpu_buffer_a->record_disabled);
2468 atomic_inc(&cpu_buffer_b->record_disabled);
2469
2470 buffer_a->buffers[cpu] = cpu_buffer_b;
2471 buffer_b->buffers[cpu] = cpu_buffer_a;
2472
2473 cpu_buffer_b->buffer = buffer_a;
2474 cpu_buffer_a->buffer = buffer_b;
2475
2476 atomic_dec(&cpu_buffer_a->record_disabled);
2477 atomic_dec(&cpu_buffer_b->record_disabled);
2478
Steven Rostedt554f7862009-03-11 22:00:13 -04002479 ret = 0;
2480out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002481 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002482}
Robert Richterc4f50182008-12-11 16:49:22 +01002483EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002484
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002485static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002486 struct buffer_data_page *bpage,
2487 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002488{
2489 struct ring_buffer_event *event;
2490 unsigned long head;
2491
2492 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002493 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002494 head += rb_event_length(event)) {
2495
Steven Rostedt044fa782008-12-02 23:50:03 -05002496 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002497 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2498 return;
2499 /* Only count data entries */
2500 if (event->type != RINGBUF_TYPE_DATA)
2501 continue;
2502 cpu_buffer->entries--;
2503 }
2504 __raw_spin_unlock(&cpu_buffer->lock);
2505}
2506
2507/**
2508 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2509 * @buffer: the buffer to allocate for.
2510 *
2511 * This function is used in conjunction with ring_buffer_read_page.
2512 * When reading a full page from the ring buffer, these functions
2513 * can be used to speed up the process. The calling function should
2514 * allocate a few pages first with this function. Then when it
2515 * needs to get pages from the ring buffer, it passes the result
2516 * of this function into ring_buffer_read_page, which will swap
2517 * the page that was allocated, with the read page of the buffer.
2518 *
2519 * Returns:
2520 * The page allocated, or NULL on error.
2521 */
2522void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2523{
Steven Rostedt044fa782008-12-02 23:50:03 -05002524 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002525 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002526
2527 addr = __get_free_page(GFP_KERNEL);
2528 if (!addr)
2529 return NULL;
2530
Steven Rostedt044fa782008-12-02 23:50:03 -05002531 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002532
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002533 rb_init_page(bpage);
2534
Steven Rostedt044fa782008-12-02 23:50:03 -05002535 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002536}
2537
2538/**
2539 * ring_buffer_free_read_page - free an allocated read page
2540 * @buffer: the buffer the page was allocate for
2541 * @data: the page to free
2542 *
2543 * Free a page allocated from ring_buffer_alloc_read_page.
2544 */
2545void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2546{
2547 free_page((unsigned long)data);
2548}
2549
2550/**
2551 * ring_buffer_read_page - extract a page from the ring buffer
2552 * @buffer: buffer to extract from
2553 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002554 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002555 * @cpu: the cpu of the buffer to extract
2556 * @full: should the extraction only happen when the page is full.
2557 *
2558 * This function will pull out a page from the ring buffer and consume it.
2559 * @data_page must be the address of the variable that was returned
2560 * from ring_buffer_alloc_read_page. This is because the page might be used
2561 * to swap with a page in the ring buffer.
2562 *
2563 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002564 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002565 * if (!rpage)
2566 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002567 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002568 * if (ret >= 0)
2569 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002570 *
2571 * When @full is set, the function will not return true unless
2572 * the writer is off the reader page.
2573 *
2574 * Note: it is up to the calling functions to handle sleeps and wakeups.
2575 * The ring buffer can be used anywhere in the kernel and can not
2576 * blindly call wake_up. The layer that uses the ring buffer must be
2577 * responsible for that.
2578 *
2579 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002580 * >=0 if data has been transferred, returns the offset of consumed data.
2581 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002582 */
2583int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002584 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002585{
2586 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2587 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002588 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002589 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002590 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002591 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002592 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002593 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002594 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002595
Steven Rostedt554f7862009-03-11 22:00:13 -04002596 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2597 goto out;
2598
Steven Rostedt474d32b2009-03-03 19:51:40 -05002599 /*
2600 * If len is not big enough to hold the page header, then
2601 * we can not copy anything.
2602 */
2603 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002604 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002605
2606 len -= BUF_PAGE_HDR_SIZE;
2607
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002608 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002609 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002610
Steven Rostedt044fa782008-12-02 23:50:03 -05002611 bpage = *data_page;
2612 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002613 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002614
2615 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2616
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002617 reader = rb_get_reader_page(cpu_buffer);
2618 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002619 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002620
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002621 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002622
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002623 read = reader->read;
2624 commit = rb_page_commit(reader);
2625
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002626 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002627 * If this page has been partially read or
2628 * if len is not big enough to read the rest of the page or
2629 * a writer is still on the page, then
2630 * we must copy the data from the page to the buffer.
2631 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002632 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002633 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002634 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002635 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002636 unsigned int rpos = read;
2637 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002638 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002639
2640 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002641 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002642
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002643 if (len > (commit - read))
2644 len = (commit - read);
2645
2646 size = rb_event_length(event);
2647
2648 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002649 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002650
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002651 /* save the current timestamp, since the user will need it */
2652 save_timestamp = cpu_buffer->read_stamp;
2653
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002654 /* Need to copy one event at a time */
2655 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002656 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002657
2658 len -= size;
2659
2660 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002661 rpos = reader->read;
2662 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002663
2664 event = rb_reader_event(cpu_buffer);
2665 size = rb_event_length(event);
2666 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002667
2668 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002669 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002670 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002671
Steven Rostedt474d32b2009-03-03 19:51:40 -05002672 /* we copied everything to the beginning */
2673 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002674 } else {
2675 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002676 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002677 bpage = reader->page;
2678 reader->page = *data_page;
2679 local_set(&reader->write, 0);
2680 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002681 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002682
2683 /* update the entry counter */
2684 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002685 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002686 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002687
Steven Rostedt554f7862009-03-11 22:00:13 -04002688 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002689 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2690
Steven Rostedt554f7862009-03-11 22:00:13 -04002691 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002692 return ret;
2693}
2694
Steven Rostedta3583242008-11-11 15:01:42 -05002695static ssize_t
2696rb_simple_read(struct file *filp, char __user *ubuf,
2697 size_t cnt, loff_t *ppos)
2698{
Hannes Eder5e398412009-02-10 19:44:34 +01002699 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002700 char buf[64];
2701 int r;
2702
Steven Rostedt033601a2008-11-21 12:41:55 -05002703 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2704 r = sprintf(buf, "permanently disabled\n");
2705 else
2706 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002707
2708 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2709}
2710
2711static ssize_t
2712rb_simple_write(struct file *filp, const char __user *ubuf,
2713 size_t cnt, loff_t *ppos)
2714{
Hannes Eder5e398412009-02-10 19:44:34 +01002715 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002716 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002717 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05002718 int ret;
2719
2720 if (cnt >= sizeof(buf))
2721 return -EINVAL;
2722
2723 if (copy_from_user(&buf, ubuf, cnt))
2724 return -EFAULT;
2725
2726 buf[cnt] = 0;
2727
2728 ret = strict_strtoul(buf, 10, &val);
2729 if (ret < 0)
2730 return ret;
2731
Steven Rostedt033601a2008-11-21 12:41:55 -05002732 if (val)
2733 set_bit(RB_BUFFERS_ON_BIT, p);
2734 else
2735 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05002736
2737 (*ppos)++;
2738
2739 return cnt;
2740}
2741
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002742static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05002743 .open = tracing_open_generic,
2744 .read = rb_simple_read,
2745 .write = rb_simple_write,
2746};
2747
2748
2749static __init int rb_init_debugfs(void)
2750{
2751 struct dentry *d_tracer;
2752 struct dentry *entry;
2753
2754 d_tracer = tracing_init_dentry();
2755
2756 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
Steven Rostedt033601a2008-11-21 12:41:55 -05002757 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05002758 if (!entry)
2759 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2760
2761 return 0;
2762}
2763
2764fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04002765
Steven Rostedt59222ef2009-03-12 11:46:03 -04002766#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04002767static int __cpuinit rb_cpu_notify(struct notifier_block *self,
2768 unsigned long action, void *hcpu)
2769{
2770 struct ring_buffer *buffer =
2771 container_of(self, struct ring_buffer, cpu_notify);
2772 long cpu = (long)hcpu;
2773
2774 switch (action) {
2775 case CPU_UP_PREPARE:
2776 case CPU_UP_PREPARE_FROZEN:
2777 if (cpu_isset(cpu, *buffer->cpumask))
2778 return NOTIFY_OK;
2779
2780 buffer->buffers[cpu] =
2781 rb_allocate_cpu_buffer(buffer, cpu);
2782 if (!buffer->buffers[cpu]) {
2783 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2784 cpu);
2785 return NOTIFY_OK;
2786 }
2787 smp_wmb();
2788 cpu_set(cpu, *buffer->cpumask);
2789 break;
2790 case CPU_DOWN_PREPARE:
2791 case CPU_DOWN_PREPARE_FROZEN:
2792 /*
2793 * Do nothing.
2794 * If we were to free the buffer, then the user would
2795 * lose any trace that was in the buffer.
2796 */
2797 break;
2798 default:
2799 break;
2800 }
2801 return NOTIFY_OK;
2802}
2803#endif