blob: 035b56c3a6c90e40d568187926c19711848cb61d [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/*
25 * A fast way to enable or disable all ring buffers is to
26 * call tracing_on or tracing_off. Turning off the ring buffers
27 * prevents all ring buffers from being recorded to.
28 * Turning this switch on, makes it OK to write to the
29 * ring buffer, if the ring buffer is enabled itself.
30 *
31 * There's three layers that must be on in order to write
32 * to the ring buffer.
33 *
34 * 1) This global flag must be set.
35 * 2) The ring buffer must be enabled for recording.
36 * 3) The per cpu buffer must be enabled for recording.
37 *
38 * In case of an anomaly, this global flag has a bit set that
39 * will permantly disable all ring buffers.
40 */
41
42/*
43 * Global flag to disable all recording to ring buffers
44 * This has two bits: ON, DISABLED
45 *
46 * ON DISABLED
47 * ---- ----------
48 * 0 0 : ring buffers are off
49 * 1 0 : ring buffers are on
50 * X 1 : ring buffers are permanently disabled
51 */
52
53enum {
54 RB_BUFFERS_ON_BIT = 0,
55 RB_BUFFERS_DISABLED_BIT = 1,
56};
57
58enum {
59 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
60 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
61};
62
Hannes Eder5e398412009-02-10 19:44:34 +010063static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -050064
Steven Rostedt474d32b2009-03-03 19:51:40 -050065#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
66
Steven Rostedta3583242008-11-11 15:01:42 -050067/**
68 * tracing_on - enable all tracing buffers
69 *
70 * This function enables all tracing buffers that may have been
71 * disabled with tracing_off.
72 */
73void tracing_on(void)
74{
Steven Rostedt033601a2008-11-21 12:41:55 -050075 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -050076}
Robert Richterc4f50182008-12-11 16:49:22 +010077EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -050078
79/**
80 * tracing_off - turn off all tracing buffers
81 *
82 * This function stops all tracing buffers from recording data.
83 * It does not disable any overhead the tracers themselves may
84 * be causing. This function simply causes all recording to
85 * the ring buffers to fail.
86 */
87void tracing_off(void)
88{
Steven Rostedt033601a2008-11-21 12:41:55 -050089 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
90}
Robert Richterc4f50182008-12-11 16:49:22 +010091EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -050092
93/**
94 * tracing_off_permanent - permanently disable ring buffers
95 *
96 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -050097 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -050098 */
99void tracing_off_permanent(void)
100{
101 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500102}
103
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500104/**
105 * tracing_is_on - show state of ring buffers enabled
106 */
107int tracing_is_on(void)
108{
109 return ring_buffer_flags == RB_BUFFERS_ON;
110}
111EXPORT_SYMBOL_GPL(tracing_is_on);
112
Ingo Molnard06bbd62008-11-12 10:11:37 +0100113#include "trace.h"
114
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400115/* Up this if you want to test the TIME_EXTENTS and normalization */
116#define DEBUG_SHIFT 0
117
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400118u64 ring_buffer_time_stamp(int cpu)
119{
Steven Rostedt47e74f22008-11-12 00:01:27 -0500120 u64 time;
121
122 preempt_disable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400123 /* shift to debug/test normalization and TIME_EXTENTS */
Ingo Molnar14131f22009-02-26 18:47:11 +0100124 time = trace_clock_local() << DEBUG_SHIFT;
Frederic Weisbecker2c2d7322008-12-16 22:08:58 +0100125 preempt_enable_no_resched_notrace();
Steven Rostedt47e74f22008-11-12 00:01:27 -0500126
127 return time;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400128}
Robert Richterc4f50182008-12-11 16:49:22 +0100129EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400130
131void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
132{
133 /* Just stupid testing the normalize function and deltas */
134 *ts >>= DEBUG_SHIFT;
135}
Robert Richterc4f50182008-12-11 16:49:22 +0100136EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400137
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500138#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800139#define RB_ALIGNMENT 4U
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400140#define RB_MAX_SMALL_DATA 28
141
142enum {
143 RB_LEN_TIME_EXTEND = 8,
144 RB_LEN_TIME_STAMP = 16,
145};
146
147/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800148static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400149rb_event_length(struct ring_buffer_event *event)
150{
151 unsigned length;
152
153 switch (event->type) {
154 case RINGBUF_TYPE_PADDING:
155 /* undefined */
156 return -1;
157
158 case RINGBUF_TYPE_TIME_EXTEND:
159 return RB_LEN_TIME_EXTEND;
160
161 case RINGBUF_TYPE_TIME_STAMP:
162 return RB_LEN_TIME_STAMP;
163
164 case RINGBUF_TYPE_DATA:
165 if (event->len)
Andrew Morton67d34722009-01-09 12:27:09 -0800166 length = event->len * RB_ALIGNMENT;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400167 else
168 length = event->array[0];
169 return length + RB_EVNT_HDR_SIZE;
170 default:
171 BUG();
172 }
173 /* not hit */
174 return 0;
175}
176
177/**
178 * ring_buffer_event_length - return the length of the event
179 * @event: the event to get the length of
180 */
181unsigned ring_buffer_event_length(struct ring_buffer_event *event)
182{
Robert Richter465634a2009-01-07 15:32:11 +0100183 unsigned length = rb_event_length(event);
184 if (event->type != RINGBUF_TYPE_DATA)
185 return length;
186 length -= RB_EVNT_HDR_SIZE;
187 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
188 length -= sizeof(event->array[0]);
189 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400190}
Robert Richterc4f50182008-12-11 16:49:22 +0100191EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400192
193/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800194static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400195rb_event_data(struct ring_buffer_event *event)
196{
197 BUG_ON(event->type != RINGBUF_TYPE_DATA);
198 /* If length is in len field, then array[0] has the data */
199 if (event->len)
200 return (void *)&event->array[0];
201 /* Otherwise length is in array[0] and array[1] has the data */
202 return (void *)&event->array[1];
203}
204
205/**
206 * ring_buffer_event_data - return the data of the event
207 * @event: the event to get the data from
208 */
209void *ring_buffer_event_data(struct ring_buffer_event *event)
210{
211 return rb_event_data(event);
212}
Robert Richterc4f50182008-12-11 16:49:22 +0100213EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400214
215#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030216 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400217
218#define TS_SHIFT 27
219#define TS_MASK ((1ULL << TS_SHIFT) - 1)
220#define TS_DELTA_TEST (~TS_MASK)
221
Steven Rostedtabc9b562008-12-02 15:34:06 -0500222struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400223 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500224 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500225 unsigned char data[]; /* data of buffer page */
226};
227
228struct buffer_page {
229 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400230 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400231 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500232 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400233};
234
Steven Rostedt044fa782008-12-02 23:50:03 -0500235static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500236{
Steven Rostedt044fa782008-12-02 23:50:03 -0500237 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500238}
239
Steven Rostedt474d32b2009-03-03 19:51:40 -0500240/**
241 * ring_buffer_page_len - the size of data on the page.
242 * @page: The page to read
243 *
244 * Returns the amount of data on the page, including buffer page header.
245 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500246size_t ring_buffer_page_len(void *page)
247{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500248 return local_read(&((struct buffer_data_page *)page)->commit)
249 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500250}
251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252/*
Steven Rostedted568292008-09-29 23:02:40 -0400253 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
254 * this issue out.
255 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800256static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400257{
Andrew Morton34a148b2009-01-09 12:27:09 -0800258 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400259 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400260}
261
262/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400263 * We need to fit the time_stamp delta into 27 bits.
264 */
265static inline int test_time_stamp(u64 delta)
266{
267 if (delta & TS_DELTA_TEST)
268 return 1;
269 return 0;
270}
271
Steven Rostedt474d32b2009-03-03 19:51:40 -0500272#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400273
274/*
275 * head_page == tail_page && head == tail then buffer is empty.
276 */
277struct ring_buffer_per_cpu {
278 int cpu;
279 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100280 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500281 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282 struct lock_class_key lock_key;
283 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400284 struct buffer_page *head_page; /* read from head */
285 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500286 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400287 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400288 unsigned long overrun;
289 unsigned long entries;
290 u64 write_stamp;
291 u64 read_stamp;
292 atomic_t record_disabled;
293};
294
295struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400296 unsigned pages;
297 unsigned flags;
298 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400299 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200300 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400301
302 struct mutex mutex;
303
304 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400305
Steven Rostedt59222ef2009-03-12 11:46:03 -0400306#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400307 struct notifier_block cpu_notify;
308#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400309};
310
311struct ring_buffer_iter {
312 struct ring_buffer_per_cpu *cpu_buffer;
313 unsigned long head;
314 struct buffer_page *head_page;
315 u64 read_stamp;
316};
317
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500318/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400319#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500320 ({ \
321 int _____ret = unlikely(cond); \
322 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400323 atomic_inc(&buffer->record_disabled); \
324 WARN_ON(1); \
325 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500326 _____ret; \
327 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500328
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400329/**
330 * check_pages - integrity check of buffer pages
331 * @cpu_buffer: CPU buffer with pages to test
332 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500333 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400334 * been corrupted.
335 */
336static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
337{
338 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500339 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400340
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500341 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
342 return -1;
343 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
344 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400345
Steven Rostedt044fa782008-12-02 23:50:03 -0500346 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500347 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500348 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500349 return -1;
350 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500351 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500352 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400353 }
354
355 return 0;
356}
357
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400358static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
359 unsigned nr_pages)
360{
361 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500362 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400363 unsigned long addr;
364 LIST_HEAD(pages);
365 unsigned i;
366
367 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500368 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400369 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500370 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400371 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500372 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400373
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400374 addr = __get_free_page(GFP_KERNEL);
375 if (!addr)
376 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500377 bpage->page = (void *)addr;
378 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400379 }
380
381 list_splice(&pages, head);
382
383 rb_check_pages(cpu_buffer);
384
385 return 0;
386
387 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500388 list_for_each_entry_safe(bpage, tmp, &pages, list) {
389 list_del_init(&bpage->list);
390 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400391 }
392 return -ENOMEM;
393}
394
395static struct ring_buffer_per_cpu *
396rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
397{
398 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500399 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400400 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400401 int ret;
402
403 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
404 GFP_KERNEL, cpu_to_node(cpu));
405 if (!cpu_buffer)
406 return NULL;
407
408 cpu_buffer->cpu = cpu;
409 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100410 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500411 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400412 INIT_LIST_HEAD(&cpu_buffer->pages);
413
Steven Rostedt044fa782008-12-02 23:50:03 -0500414 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400415 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500416 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400417 goto fail_free_buffer;
418
Steven Rostedt044fa782008-12-02 23:50:03 -0500419 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400420 addr = __get_free_page(GFP_KERNEL);
421 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400422 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500423 bpage->page = (void *)addr;
424 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400425
Steven Rostedtd7690412008-10-01 00:29:53 -0400426 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400427
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400428 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
429 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400430 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400431
432 cpu_buffer->head_page
433 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400434 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400435
436 return cpu_buffer;
437
Steven Rostedtd7690412008-10-01 00:29:53 -0400438 fail_free_reader:
439 free_buffer_page(cpu_buffer->reader_page);
440
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400441 fail_free_buffer:
442 kfree(cpu_buffer);
443 return NULL;
444}
445
446static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
447{
448 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500449 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400450
Steven Rostedtd7690412008-10-01 00:29:53 -0400451 list_del_init(&cpu_buffer->reader_page->list);
452 free_buffer_page(cpu_buffer->reader_page);
453
Steven Rostedt044fa782008-12-02 23:50:03 -0500454 list_for_each_entry_safe(bpage, tmp, head, list) {
455 list_del_init(&bpage->list);
456 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400457 }
458 kfree(cpu_buffer);
459}
460
Steven Rostedta7b13742008-09-29 23:02:39 -0400461/*
462 * Causes compile errors if the struct buffer_page gets bigger
463 * than the struct page.
464 */
465extern int ring_buffer_page_too_big(void);
466
Steven Rostedt59222ef2009-03-12 11:46:03 -0400467#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400468static int __cpuinit rb_cpu_notify(struct notifier_block *self,
469 unsigned long action, void *hcpu);
470#endif
471
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472/**
473 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100474 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400475 * @flags: attributes to set for the ring buffer.
476 *
477 * Currently the only flag that is available is the RB_FL_OVERWRITE
478 * flag. This flag means that the buffer will overwrite old data
479 * when the buffer wraps. If this flag is not set, the buffer will
480 * drop data when the tail hits the head.
481 */
482struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
483{
484 struct ring_buffer *buffer;
485 int bsize;
486 int cpu;
487
Steven Rostedta7b13742008-09-29 23:02:39 -0400488 /* Paranoid! Optimizes out when all is well */
489 if (sizeof(struct buffer_page) > sizeof(struct page))
490 ring_buffer_page_too_big();
491
492
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400493 /* keep it in its own cache line */
494 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
495 GFP_KERNEL);
496 if (!buffer)
497 return NULL;
498
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030499 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
500 goto fail_free_buffer;
501
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400502 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
503 buffer->flags = flags;
504
505 /* need at least two pages */
506 if (buffer->pages == 1)
507 buffer->pages++;
508
Steven Rostedt554f7862009-03-11 22:00:13 -0400509 get_online_cpus();
510 cpumask_copy(buffer->cpumask, cpu_online_mask);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400511 buffer->cpus = nr_cpu_ids;
512
513 bsize = sizeof(void *) * nr_cpu_ids;
514 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
515 GFP_KERNEL);
516 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030517 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400518
519 for_each_buffer_cpu(buffer, cpu) {
520 buffer->buffers[cpu] =
521 rb_allocate_cpu_buffer(buffer, cpu);
522 if (!buffer->buffers[cpu])
523 goto fail_free_buffers;
524 }
525
Steven Rostedt59222ef2009-03-12 11:46:03 -0400526#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400527 buffer->cpu_notify.notifier_call = rb_cpu_notify;
528 buffer->cpu_notify.priority = 0;
529 register_cpu_notifier(&buffer->cpu_notify);
530#endif
531
532 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400533 mutex_init(&buffer->mutex);
534
535 return buffer;
536
537 fail_free_buffers:
538 for_each_buffer_cpu(buffer, cpu) {
539 if (buffer->buffers[cpu])
540 rb_free_cpu_buffer(buffer->buffers[cpu]);
541 }
542 kfree(buffer->buffers);
543
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030544 fail_free_cpumask:
545 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400546 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030547
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400548 fail_free_buffer:
549 kfree(buffer);
550 return NULL;
551}
Robert Richterc4f50182008-12-11 16:49:22 +0100552EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400553
554/**
555 * ring_buffer_free - free a ring buffer.
556 * @buffer: the buffer to free.
557 */
558void
559ring_buffer_free(struct ring_buffer *buffer)
560{
561 int cpu;
562
Steven Rostedt554f7862009-03-11 22:00:13 -0400563 get_online_cpus();
564
Steven Rostedt59222ef2009-03-12 11:46:03 -0400565#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400566 unregister_cpu_notifier(&buffer->cpu_notify);
567#endif
568
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400569 for_each_buffer_cpu(buffer, cpu)
570 rb_free_cpu_buffer(buffer->buffers[cpu]);
571
Steven Rostedt554f7862009-03-11 22:00:13 -0400572 put_online_cpus();
573
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030574 free_cpumask_var(buffer->cpumask);
575
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400576 kfree(buffer);
577}
Robert Richterc4f50182008-12-11 16:49:22 +0100578EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400579
580static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
581
582static void
583rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
584{
Steven Rostedt044fa782008-12-02 23:50:03 -0500585 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400586 struct list_head *p;
587 unsigned i;
588
589 atomic_inc(&cpu_buffer->record_disabled);
590 synchronize_sched();
591
592 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500593 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
594 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400595 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500596 bpage = list_entry(p, struct buffer_page, list);
597 list_del_init(&bpage->list);
598 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400599 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500600 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
601 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400602
603 rb_reset_cpu(cpu_buffer);
604
605 rb_check_pages(cpu_buffer);
606
607 atomic_dec(&cpu_buffer->record_disabled);
608
609}
610
611static void
612rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
613 struct list_head *pages, unsigned nr_pages)
614{
Steven Rostedt044fa782008-12-02 23:50:03 -0500615 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400616 struct list_head *p;
617 unsigned i;
618
619 atomic_inc(&cpu_buffer->record_disabled);
620 synchronize_sched();
621
622 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500623 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
624 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400625 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500626 bpage = list_entry(p, struct buffer_page, list);
627 list_del_init(&bpage->list);
628 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400629 }
630 rb_reset_cpu(cpu_buffer);
631
632 rb_check_pages(cpu_buffer);
633
634 atomic_dec(&cpu_buffer->record_disabled);
635}
636
637/**
638 * ring_buffer_resize - resize the ring buffer
639 * @buffer: the buffer to resize.
640 * @size: the new size.
641 *
642 * The tracer is responsible for making sure that the buffer is
643 * not being used while changing the size.
644 * Note: We may be able to change the above requirement by using
645 * RCU synchronizations.
646 *
647 * Minimum size is 2 * BUF_PAGE_SIZE.
648 *
649 * Returns -1 on failure.
650 */
651int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
652{
653 struct ring_buffer_per_cpu *cpu_buffer;
654 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500655 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400656 unsigned long buffer_size;
657 unsigned long addr;
658 LIST_HEAD(pages);
659 int i, cpu;
660
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100661 /*
662 * Always succeed at resizing a non-existent buffer:
663 */
664 if (!buffer)
665 return size;
666
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400667 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
668 size *= BUF_PAGE_SIZE;
669 buffer_size = buffer->pages * BUF_PAGE_SIZE;
670
671 /* we need a minimum of two pages */
672 if (size < BUF_PAGE_SIZE * 2)
673 size = BUF_PAGE_SIZE * 2;
674
675 if (size == buffer_size)
676 return size;
677
678 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400679 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400680
681 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
682
683 if (size < buffer_size) {
684
685 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400686 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
687 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400688
689 rm_pages = buffer->pages - nr_pages;
690
691 for_each_buffer_cpu(buffer, cpu) {
692 cpu_buffer = buffer->buffers[cpu];
693 rb_remove_pages(cpu_buffer, rm_pages);
694 }
695 goto out;
696 }
697
698 /*
699 * This is a bit more difficult. We only want to add pages
700 * when we can allocate enough for all CPUs. We do this
701 * by allocating all the pages and storing them on a local
702 * link list. If we succeed in our allocation, then we
703 * add these pages to the cpu_buffers. Otherwise we just free
704 * them all and return -ENOMEM;
705 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400706 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
707 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500708
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400709 new_pages = nr_pages - buffer->pages;
710
711 for_each_buffer_cpu(buffer, cpu) {
712 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500713 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400714 cache_line_size()),
715 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500716 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400717 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500718 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400719 addr = __get_free_page(GFP_KERNEL);
720 if (!addr)
721 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500722 bpage->page = (void *)addr;
723 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400724 }
725 }
726
727 for_each_buffer_cpu(buffer, cpu) {
728 cpu_buffer = buffer->buffers[cpu];
729 rb_insert_pages(cpu_buffer, &pages, new_pages);
730 }
731
Steven Rostedt554f7862009-03-11 22:00:13 -0400732 if (RB_WARN_ON(buffer, !list_empty(&pages)))
733 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400734
735 out:
736 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400737 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400738 mutex_unlock(&buffer->mutex);
739
740 return size;
741
742 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500743 list_for_each_entry_safe(bpage, tmp, &pages, list) {
744 list_del_init(&bpage->list);
745 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400746 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400747 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100748 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400749 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400750
751 /*
752 * Something went totally wrong, and we are too paranoid
753 * to even clean up the mess.
754 */
755 out_fail:
756 put_online_cpus();
757 mutex_unlock(&buffer->mutex);
758 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400759}
Robert Richterc4f50182008-12-11 16:49:22 +0100760EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400761
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400762static inline int rb_null_event(struct ring_buffer_event *event)
763{
764 return event->type == RINGBUF_TYPE_PADDING;
765}
766
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500767static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500768__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500769{
Steven Rostedt044fa782008-12-02 23:50:03 -0500770 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500771}
772
Steven Rostedt044fa782008-12-02 23:50:03 -0500773static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400774{
Steven Rostedt044fa782008-12-02 23:50:03 -0500775 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400776}
777
778static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400779rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400780{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400781 return __rb_page_index(cpu_buffer->reader_page,
782 cpu_buffer->reader_page->read);
783}
784
785static inline struct ring_buffer_event *
786rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
787{
788 return __rb_page_index(cpu_buffer->head_page,
789 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400790}
791
792static inline struct ring_buffer_event *
793rb_iter_head_event(struct ring_buffer_iter *iter)
794{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400795 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400796}
797
Steven Rostedtbf41a152008-10-04 02:00:59 -0400798static inline unsigned rb_page_write(struct buffer_page *bpage)
799{
800 return local_read(&bpage->write);
801}
802
803static inline unsigned rb_page_commit(struct buffer_page *bpage)
804{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500805 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400806}
807
808/* Size is determined by what has been commited */
809static inline unsigned rb_page_size(struct buffer_page *bpage)
810{
811 return rb_page_commit(bpage);
812}
813
814static inline unsigned
815rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
816{
817 return rb_page_commit(cpu_buffer->commit_page);
818}
819
820static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
821{
822 return rb_page_commit(cpu_buffer->head_page);
823}
824
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400825/*
826 * When the tail hits the head and the buffer is in overwrite mode,
827 * the head jumps to the next page and all content on the previous
828 * page is discarded. But before doing so, we update the overrun
829 * variable of the buffer.
830 */
831static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
832{
833 struct ring_buffer_event *event;
834 unsigned long head;
835
836 for (head = 0; head < rb_head_size(cpu_buffer);
837 head += rb_event_length(event)) {
838
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400839 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500840 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
841 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400842 /* Only count data entries */
843 if (event->type != RINGBUF_TYPE_DATA)
844 continue;
845 cpu_buffer->overrun++;
846 cpu_buffer->entries--;
847 }
848}
849
850static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500851 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400852{
Steven Rostedt044fa782008-12-02 23:50:03 -0500853 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400854
855 if (p == &cpu_buffer->pages)
856 p = p->next;
857
Steven Rostedt044fa782008-12-02 23:50:03 -0500858 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400859}
860
Steven Rostedtbf41a152008-10-04 02:00:59 -0400861static inline unsigned
862rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400863{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400864 unsigned long addr = (unsigned long)event;
865
866 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400867}
868
Andrew Morton34a148b2009-01-09 12:27:09 -0800869static int
Steven Rostedtbf41a152008-10-04 02:00:59 -0400870rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
871 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400872{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400873 unsigned long addr = (unsigned long)event;
874 unsigned long index;
875
876 index = rb_event_index(event);
877 addr &= PAGE_MASK;
878
879 return cpu_buffer->commit_page->page == (void *)addr &&
880 rb_commit_index(cpu_buffer) == index;
881}
882
Andrew Morton34a148b2009-01-09 12:27:09 -0800883static void
Steven Rostedtbf41a152008-10-04 02:00:59 -0400884rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
885 struct ring_buffer_event *event)
886{
887 unsigned long addr = (unsigned long)event;
888 unsigned long index;
889
890 index = rb_event_index(event);
891 addr &= PAGE_MASK;
892
893 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500894 if (RB_WARN_ON(cpu_buffer,
895 cpu_buffer->commit_page == cpu_buffer->tail_page))
896 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -0500897 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400898 cpu_buffer->commit_page->write;
899 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500900 cpu_buffer->write_stamp =
901 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400902 }
903
904 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500905 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400906}
907
Andrew Morton34a148b2009-01-09 12:27:09 -0800908static void
Steven Rostedtbf41a152008-10-04 02:00:59 -0400909rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
910{
911 /*
912 * We only race with interrupts and NMIs on this CPU.
913 * If we own the commit event, then we can commit
914 * all others that interrupted us, since the interruptions
915 * are in stack format (they finish before they come
916 * back to us). This allows us to do a simple loop to
917 * assign the commit to the tail.
918 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -0500919 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -0400920 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500921 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400922 cpu_buffer->commit_page->write;
923 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500924 cpu_buffer->write_stamp =
925 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400926 /* add barrier to keep gcc from optimizing too much */
927 barrier();
928 }
929 while (rb_commit_index(cpu_buffer) !=
930 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500931 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400932 cpu_buffer->commit_page->write;
933 barrier();
934 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -0500935
936 /* again, keep gcc from optimizing */
937 barrier();
938
939 /*
940 * If an interrupt came in just after the first while loop
941 * and pushed the tail page forward, we will be left with
942 * a dangling commit that will never go forward.
943 */
944 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
945 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400946}
947
Steven Rostedtd7690412008-10-01 00:29:53 -0400948static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400949{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500950 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400951 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -0400952}
953
Andrew Morton34a148b2009-01-09 12:27:09 -0800954static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -0400955{
956 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
957
958 /*
959 * The iterator could be on the reader page (it starts there).
960 * But the head could have moved, since the reader was
961 * found. Check for this case and assign the iterator
962 * to the head page instead of next.
963 */
964 if (iter->head_page == cpu_buffer->reader_page)
965 iter->head_page = cpu_buffer->head_page;
966 else
967 rb_inc_page(cpu_buffer, &iter->head_page);
968
Steven Rostedtabc9b562008-12-02 15:34:06 -0500969 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400970 iter->head = 0;
971}
972
973/**
974 * ring_buffer_update_event - update event type and data
975 * @event: the even to update
976 * @type: the type of event
977 * @length: the size of the event field in the ring buffer
978 *
979 * Update the type and data fields of the event. The length
980 * is the actual size that is written to the ring buffer,
981 * and with this, we can determine what to place into the
982 * data field.
983 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800984static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400985rb_update_event(struct ring_buffer_event *event,
986 unsigned type, unsigned length)
987{
988 event->type = type;
989
990 switch (type) {
991
992 case RINGBUF_TYPE_PADDING:
993 break;
994
995 case RINGBUF_TYPE_TIME_EXTEND:
Andrew Morton67d34722009-01-09 12:27:09 -0800996 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997 break;
998
999 case RINGBUF_TYPE_TIME_STAMP:
Andrew Morton67d34722009-01-09 12:27:09 -08001000 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001 break;
1002
1003 case RINGBUF_TYPE_DATA:
1004 length -= RB_EVNT_HDR_SIZE;
1005 if (length > RB_MAX_SMALL_DATA) {
1006 event->len = 0;
1007 event->array[0] = length;
1008 } else
Andrew Morton67d34722009-01-09 12:27:09 -08001009 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001010 break;
1011 default:
1012 BUG();
1013 }
1014}
1015
Andrew Morton34a148b2009-01-09 12:27:09 -08001016static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001017{
1018 struct ring_buffer_event event; /* Used only for sizeof array */
1019
1020 /* zero length can cause confusions */
1021 if (!length)
1022 length = 1;
1023
1024 if (length > RB_MAX_SMALL_DATA)
1025 length += sizeof(event.array[0]);
1026
1027 length += RB_EVNT_HDR_SIZE;
1028 length = ALIGN(length, RB_ALIGNMENT);
1029
1030 return length;
1031}
1032
1033static struct ring_buffer_event *
1034__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1035 unsigned type, unsigned long length, u64 *ts)
1036{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001037 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001038 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001039 struct ring_buffer *buffer = cpu_buffer->buffer;
1040 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001041 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001042 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001043
Steven Rostedt98db8df2008-12-23 11:32:25 -05001044 commit_page = cpu_buffer->commit_page;
1045 /* we just need to protect against interrupts */
1046 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001047 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001048 write = local_add_return(length, &tail_page->write);
1049 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001050
Steven Rostedtbf41a152008-10-04 02:00:59 -04001051 /* See if we shot pass the end of this buffer page */
1052 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053 struct buffer_page *next_page = tail_page;
1054
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001055 local_irq_save(flags);
Steven Rostedt78d904b2009-02-05 18:43:07 -05001056 /*
Steven Rostedta81bd802009-02-06 01:45:16 -05001057 * Since the write to the buffer is still not
1058 * fully lockless, we must be careful with NMIs.
1059 * The locks in the writers are taken when a write
1060 * crosses to a new page. The locks protect against
1061 * races with the readers (this will soon be fixed
1062 * with a lockless solution).
1063 *
1064 * Because we can not protect against NMIs, and we
1065 * want to keep traces reentrant, we need to manage
1066 * what happens when we are in an NMI.
1067 *
Steven Rostedt78d904b2009-02-05 18:43:07 -05001068 * NMIs can happen after we take the lock.
1069 * If we are in an NMI, only take the lock
1070 * if it is not already taken. Otherwise
1071 * simply fail.
1072 */
Steven Rostedta81bd802009-02-06 01:45:16 -05001073 if (unlikely(in_nmi())) {
Steven Rostedt78d904b2009-02-05 18:43:07 -05001074 if (!__raw_spin_trylock(&cpu_buffer->lock))
Steven Rostedt45141d42009-02-12 13:19:48 -05001075 goto out_reset;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001076 } else
1077 __raw_spin_lock(&cpu_buffer->lock);
1078
1079 lock_taken = true;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001081 rb_inc_page(cpu_buffer, &next_page);
1082
Steven Rostedtd7690412008-10-01 00:29:53 -04001083 head_page = cpu_buffer->head_page;
1084 reader_page = cpu_buffer->reader_page;
1085
1086 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001087 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
Steven Rostedt45141d42009-02-12 13:19:48 -05001088 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001089
1090 /*
1091 * If for some reason, we had an interrupt storm that made
1092 * it all the way around the buffer, bail, and warn
1093 * about it.
1094 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001095 if (unlikely(next_page == commit_page)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001096 WARN_ON_ONCE(1);
Steven Rostedt45141d42009-02-12 13:19:48 -05001097 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001098 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001099
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001100 if (next_page == head_page) {
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001101 if (!(buffer->flags & RB_FL_OVERWRITE))
Steven Rostedt45141d42009-02-12 13:19:48 -05001102 goto out_reset;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001103
Steven Rostedtbf41a152008-10-04 02:00:59 -04001104 /* tail_page has not moved yet? */
1105 if (tail_page == cpu_buffer->tail_page) {
1106 /* count overflows */
1107 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108
Steven Rostedtbf41a152008-10-04 02:00:59 -04001109 rb_inc_page(cpu_buffer, &head_page);
1110 cpu_buffer->head_page = head_page;
1111 cpu_buffer->head_page->read = 0;
1112 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001113 }
1114
Steven Rostedtbf41a152008-10-04 02:00:59 -04001115 /*
1116 * If the tail page is still the same as what we think
1117 * it is, then it is up to us to update the tail
1118 * pointer.
1119 */
1120 if (tail_page == cpu_buffer->tail_page) {
1121 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001122 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001123 cpu_buffer->tail_page = next_page;
1124
1125 /* reread the time stamp */
1126 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001127 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001128 }
1129
1130 /*
1131 * The actual tail page has moved forward.
1132 */
1133 if (tail < BUF_PAGE_SIZE) {
1134 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001135 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001136 event->type = RINGBUF_TYPE_PADDING;
1137 }
1138
Steven Rostedtbf41a152008-10-04 02:00:59 -04001139 if (tail <= BUF_PAGE_SIZE)
1140 /* Set the write back to the previous setting */
1141 local_set(&tail_page->write, tail);
1142
1143 /*
1144 * If this was a commit entry that failed,
1145 * increment that too
1146 */
1147 if (tail_page == cpu_buffer->commit_page &&
1148 tail == rb_commit_index(cpu_buffer)) {
1149 rb_set_commit_to_write(cpu_buffer);
1150 }
1151
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001152 __raw_spin_unlock(&cpu_buffer->lock);
1153 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001154
1155 /* fail and let the caller try again */
1156 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001157 }
1158
Steven Rostedtbf41a152008-10-04 02:00:59 -04001159 /* We reserved something on the buffer */
1160
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001161 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1162 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001163
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001164 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001165 rb_update_event(event, type, length);
1166
Steven Rostedtbf41a152008-10-04 02:00:59 -04001167 /*
1168 * If this is a commit and the tail is zero, then update
1169 * this page's time stamp.
1170 */
1171 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001172 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001173
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001175
Steven Rostedt45141d42009-02-12 13:19:48 -05001176 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001177 /* reset write */
1178 if (tail <= BUF_PAGE_SIZE)
1179 local_set(&tail_page->write, tail);
1180
Steven Rostedt78d904b2009-02-05 18:43:07 -05001181 if (likely(lock_taken))
1182 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001183 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001184 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001185}
1186
1187static int
1188rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1189 u64 *ts, u64 *delta)
1190{
1191 struct ring_buffer_event *event;
1192 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001193 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001194
1195 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1196 printk(KERN_WARNING "Delta way too big! %llu"
1197 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001198 (unsigned long long)*delta,
1199 (unsigned long long)*ts,
1200 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001201 WARN_ON(1);
1202 }
1203
1204 /*
1205 * The delta is too big, we to add a
1206 * new timestamp.
1207 */
1208 event = __rb_reserve_next(cpu_buffer,
1209 RINGBUF_TYPE_TIME_EXTEND,
1210 RB_LEN_TIME_EXTEND,
1211 ts);
1212 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001213 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001214
Steven Rostedtbf41a152008-10-04 02:00:59 -04001215 if (PTR_ERR(event) == -EAGAIN)
1216 return -EAGAIN;
1217
1218 /* Only a commited time event can update the write stamp */
1219 if (rb_is_commit(cpu_buffer, event)) {
1220 /*
1221 * If this is the first on the page, then we need to
1222 * update the page itself, and just put in a zero.
1223 */
1224 if (rb_event_index(event)) {
1225 event->time_delta = *delta & TS_MASK;
1226 event->array[0] = *delta >> TS_SHIFT;
1227 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001228 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001229 event->time_delta = 0;
1230 event->array[0] = 0;
1231 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001233 /* let the caller know this was the commit */
1234 ret = 1;
1235 } else {
1236 /* Darn, this is just wasted space */
1237 event->time_delta = 0;
1238 event->array[0] = 0;
1239 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240 }
1241
Steven Rostedtbf41a152008-10-04 02:00:59 -04001242 *delta = 0;
1243
1244 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001245}
1246
1247static struct ring_buffer_event *
1248rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1249 unsigned type, unsigned long length)
1250{
1251 struct ring_buffer_event *event;
1252 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001253 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001254 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255
Steven Rostedtbf41a152008-10-04 02:00:59 -04001256 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001257 /*
1258 * We allow for interrupts to reenter here and do a trace.
1259 * If one does, it will cause this original code to loop
1260 * back here. Even with heavy interrupts happening, this
1261 * should only happen a few times in a row. If this happens
1262 * 1000 times in a row, there must be either an interrupt
1263 * storm or we have something buggy.
1264 * Bail!
1265 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001266 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001267 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001268
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1270
Steven Rostedtbf41a152008-10-04 02:00:59 -04001271 /*
1272 * Only the first commit can update the timestamp.
1273 * Yes there is a race here. If an interrupt comes in
1274 * just after the conditional and it traces too, then it
1275 * will also check the deltas. More than one timestamp may
1276 * also be made. But only the entry that did the actual
1277 * commit will be something other than zero.
1278 */
1279 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1280 rb_page_write(cpu_buffer->tail_page) ==
1281 rb_commit_index(cpu_buffer)) {
1282
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001283 delta = ts - cpu_buffer->write_stamp;
1284
Steven Rostedtbf41a152008-10-04 02:00:59 -04001285 /* make sure this delta is calculated here */
1286 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001287
Steven Rostedtbf41a152008-10-04 02:00:59 -04001288 /* Did the write stamp get updated already? */
1289 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001290 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001291
1292 if (test_time_stamp(delta)) {
1293
1294 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1295
1296 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001297 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001298
1299 if (commit == -EAGAIN)
1300 goto again;
1301
1302 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001303 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001304 } else
1305 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001306 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001307
1308 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001309 if (PTR_ERR(event) == -EAGAIN)
1310 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001311
Steven Rostedtbf41a152008-10-04 02:00:59 -04001312 if (!event) {
1313 if (unlikely(commit))
1314 /*
1315 * Ouch! We needed a timestamp and it was commited. But
1316 * we didn't get our event reserved.
1317 */
1318 rb_set_commit_to_write(cpu_buffer);
1319 return NULL;
1320 }
1321
1322 /*
1323 * If the timestamp was commited, make the commit our entry
1324 * now so that we will update it when needed.
1325 */
1326 if (commit)
1327 rb_set_commit_event(cpu_buffer, event);
1328 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001329 delta = 0;
1330
1331 event->time_delta = delta;
1332
1333 return event;
1334}
1335
Steven Rostedtbf41a152008-10-04 02:00:59 -04001336static DEFINE_PER_CPU(int, rb_need_resched);
1337
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001338/**
1339 * ring_buffer_lock_reserve - reserve a part of the buffer
1340 * @buffer: the ring buffer to reserve from
1341 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001342 *
1343 * Returns a reseverd event on the ring buffer to copy directly to.
1344 * The user of this interface will need to get the body to write into
1345 * and can use the ring_buffer_event_data() interface.
1346 *
1347 * The length is the length of the data needed, not the event length
1348 * which also includes the event header.
1349 *
1350 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1351 * If NULL is returned, then nothing has been allocated or locked.
1352 */
1353struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001354ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001355{
1356 struct ring_buffer_per_cpu *cpu_buffer;
1357 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001358 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001359
Steven Rostedt033601a2008-11-21 12:41:55 -05001360 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001361 return NULL;
1362
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001363 if (atomic_read(&buffer->record_disabled))
1364 return NULL;
1365
Steven Rostedtbf41a152008-10-04 02:00:59 -04001366 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001367 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001368
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001369 cpu = raw_smp_processor_id();
1370
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301371 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001372 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001373
1374 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001375
1376 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001377 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378
1379 length = rb_calculate_event_length(length);
1380 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001381 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001382
1383 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1384 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001385 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001386
Steven Rostedtbf41a152008-10-04 02:00:59 -04001387 /*
1388 * Need to store resched state on this cpu.
1389 * Only the first needs to.
1390 */
1391
1392 if (preempt_count() == 1)
1393 per_cpu(rb_need_resched, cpu) = resched;
1394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001395 return event;
1396
Steven Rostedtd7690412008-10-01 00:29:53 -04001397 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001398 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001399 return NULL;
1400}
Robert Richterc4f50182008-12-11 16:49:22 +01001401EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001402
1403static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1404 struct ring_buffer_event *event)
1405{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001406 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001407
1408 /* Only process further if we own the commit */
1409 if (!rb_is_commit(cpu_buffer, event))
1410 return;
1411
1412 cpu_buffer->write_stamp += event->time_delta;
1413
1414 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001415}
1416
1417/**
1418 * ring_buffer_unlock_commit - commit a reserved
1419 * @buffer: The buffer to commit to
1420 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421 *
1422 * This commits the data to the ring buffer, and releases any locks held.
1423 *
1424 * Must be paired with ring_buffer_lock_reserve.
1425 */
1426int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001427 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001428{
1429 struct ring_buffer_per_cpu *cpu_buffer;
1430 int cpu = raw_smp_processor_id();
1431
1432 cpu_buffer = buffer->buffers[cpu];
1433
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434 rb_commit(cpu_buffer, event);
1435
Steven Rostedtbf41a152008-10-04 02:00:59 -04001436 /*
1437 * Only the last preempt count needs to restore preemption.
1438 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001439 if (preempt_count() == 1)
1440 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1441 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001442 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001443
1444 return 0;
1445}
Robert Richterc4f50182008-12-11 16:49:22 +01001446EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001447
1448/**
1449 * ring_buffer_write - write data to the buffer without reserving
1450 * @buffer: The ring buffer to write to.
1451 * @length: The length of the data being written (excluding the event header)
1452 * @data: The data to write to the buffer.
1453 *
1454 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1455 * one function. If you already have the data to write to the buffer, it
1456 * may be easier to simply call this function.
1457 *
1458 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1459 * and not the length of the event which would hold the header.
1460 */
1461int ring_buffer_write(struct ring_buffer *buffer,
1462 unsigned long length,
1463 void *data)
1464{
1465 struct ring_buffer_per_cpu *cpu_buffer;
1466 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001467 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001468 void *body;
1469 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001471
Steven Rostedt033601a2008-11-21 12:41:55 -05001472 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001473 return -EBUSY;
1474
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001475 if (atomic_read(&buffer->record_disabled))
1476 return -EBUSY;
1477
Steven Rostedt182e9f52008-11-03 23:15:56 -05001478 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001479
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001480 cpu = raw_smp_processor_id();
1481
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301482 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001483 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001484
1485 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001486
1487 if (atomic_read(&cpu_buffer->record_disabled))
1488 goto out;
1489
1490 event_length = rb_calculate_event_length(length);
1491 event = rb_reserve_next_event(cpu_buffer,
1492 RINGBUF_TYPE_DATA, event_length);
1493 if (!event)
1494 goto out;
1495
1496 body = rb_event_data(event);
1497
1498 memcpy(body, data, length);
1499
1500 rb_commit(cpu_buffer, event);
1501
1502 ret = 0;
1503 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001504 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001505
1506 return ret;
1507}
Robert Richterc4f50182008-12-11 16:49:22 +01001508EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001509
Andrew Morton34a148b2009-01-09 12:27:09 -08001510static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001511{
1512 struct buffer_page *reader = cpu_buffer->reader_page;
1513 struct buffer_page *head = cpu_buffer->head_page;
1514 struct buffer_page *commit = cpu_buffer->commit_page;
1515
1516 return reader->read == rb_page_commit(reader) &&
1517 (commit == reader ||
1518 (commit == head &&
1519 head->read == rb_page_commit(commit)));
1520}
1521
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001523 * ring_buffer_record_disable - stop all writes into the buffer
1524 * @buffer: The ring buffer to stop writes to.
1525 *
1526 * This prevents all writes to the buffer. Any attempt to write
1527 * to the buffer after this will fail and return NULL.
1528 *
1529 * The caller should call synchronize_sched() after this.
1530 */
1531void ring_buffer_record_disable(struct ring_buffer *buffer)
1532{
1533 atomic_inc(&buffer->record_disabled);
1534}
Robert Richterc4f50182008-12-11 16:49:22 +01001535EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001536
1537/**
1538 * ring_buffer_record_enable - enable writes to the buffer
1539 * @buffer: The ring buffer to enable writes
1540 *
1541 * Note, multiple disables will need the same number of enables
1542 * to truely enable the writing (much like preempt_disable).
1543 */
1544void ring_buffer_record_enable(struct ring_buffer *buffer)
1545{
1546 atomic_dec(&buffer->record_disabled);
1547}
Robert Richterc4f50182008-12-11 16:49:22 +01001548EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001549
1550/**
1551 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1552 * @buffer: The ring buffer to stop writes to.
1553 * @cpu: The CPU buffer to stop
1554 *
1555 * This prevents all writes to the buffer. Any attempt to write
1556 * to the buffer after this will fail and return NULL.
1557 *
1558 * The caller should call synchronize_sched() after this.
1559 */
1560void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1561{
1562 struct ring_buffer_per_cpu *cpu_buffer;
1563
Steven Rostedt554f7862009-03-11 22:00:13 -04001564 get_online_cpus();
1565
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301566 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04001567 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568
1569 cpu_buffer = buffer->buffers[cpu];
1570 atomic_inc(&cpu_buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001571 out:
1572 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001573}
Robert Richterc4f50182008-12-11 16:49:22 +01001574EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001575
1576/**
1577 * ring_buffer_record_enable_cpu - enable writes to the buffer
1578 * @buffer: The ring buffer to enable writes
1579 * @cpu: The CPU to enable.
1580 *
1581 * Note, multiple disables will need the same number of enables
1582 * to truely enable the writing (much like preempt_disable).
1583 */
1584void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1585{
1586 struct ring_buffer_per_cpu *cpu_buffer;
1587
Steven Rostedt554f7862009-03-11 22:00:13 -04001588 get_online_cpus();
1589
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301590 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04001591 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001592
1593 cpu_buffer = buffer->buffers[cpu];
1594 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001595 out:
1596 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001597}
Robert Richterc4f50182008-12-11 16:49:22 +01001598EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001599
1600/**
1601 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1602 * @buffer: The ring buffer
1603 * @cpu: The per CPU buffer to get the entries from.
1604 */
1605unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1606{
1607 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt554f7862009-03-11 22:00:13 -04001608 unsigned long ret = 0;
1609
1610 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001611
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301612 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04001613 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001614
1615 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001616 ret = cpu_buffer->entries;
1617 out:
1618 put_online_cpus();
1619
1620 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001621}
Robert Richterc4f50182008-12-11 16:49:22 +01001622EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001623
1624/**
1625 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1626 * @buffer: The ring buffer
1627 * @cpu: The per CPU buffer to get the number of overruns from
1628 */
1629unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1630{
1631 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt554f7862009-03-11 22:00:13 -04001632 unsigned long ret = 0;
1633
1634 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001635
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301636 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04001637 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001638
1639 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001640 ret = cpu_buffer->overrun;
1641 out:
1642 put_online_cpus();
1643
1644 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001645}
Robert Richterc4f50182008-12-11 16:49:22 +01001646EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001647
1648/**
1649 * ring_buffer_entries - get the number of entries in a buffer
1650 * @buffer: The ring buffer
1651 *
1652 * Returns the total number of entries in the ring buffer
1653 * (all CPU entries)
1654 */
1655unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1656{
1657 struct ring_buffer_per_cpu *cpu_buffer;
1658 unsigned long entries = 0;
1659 int cpu;
1660
Steven Rostedt554f7862009-03-11 22:00:13 -04001661 get_online_cpus();
1662
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001663 /* if you care about this being correct, lock the buffer */
1664 for_each_buffer_cpu(buffer, cpu) {
1665 cpu_buffer = buffer->buffers[cpu];
1666 entries += cpu_buffer->entries;
1667 }
1668
Steven Rostedt554f7862009-03-11 22:00:13 -04001669 put_online_cpus();
1670
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001671 return entries;
1672}
Robert Richterc4f50182008-12-11 16:49:22 +01001673EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001674
1675/**
1676 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1677 * @buffer: The ring buffer
1678 *
1679 * Returns the total number of overruns in the ring buffer
1680 * (all CPU entries)
1681 */
1682unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1683{
1684 struct ring_buffer_per_cpu *cpu_buffer;
1685 unsigned long overruns = 0;
1686 int cpu;
1687
Steven Rostedt554f7862009-03-11 22:00:13 -04001688 get_online_cpus();
1689
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001690 /* if you care about this being correct, lock the buffer */
1691 for_each_buffer_cpu(buffer, cpu) {
1692 cpu_buffer = buffer->buffers[cpu];
1693 overruns += cpu_buffer->overrun;
1694 }
1695
Steven Rostedt554f7862009-03-11 22:00:13 -04001696 put_online_cpus();
1697
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001698 return overruns;
1699}
Robert Richterc4f50182008-12-11 16:49:22 +01001700EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001701
Steven Rostedt642edba2008-11-12 00:01:26 -05001702static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001703{
1704 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1705
Steven Rostedtd7690412008-10-01 00:29:53 -04001706 /* Iterator usage is expected to have record disabled */
1707 if (list_empty(&cpu_buffer->reader_page->list)) {
1708 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001709 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001710 } else {
1711 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001712 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001713 }
1714 if (iter->head)
1715 iter->read_stamp = cpu_buffer->read_stamp;
1716 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05001717 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001718}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001719
Steven Rostedt642edba2008-11-12 00:01:26 -05001720/**
1721 * ring_buffer_iter_reset - reset an iterator
1722 * @iter: The iterator to reset
1723 *
1724 * Resets the iterator, so that it will start from the beginning
1725 * again.
1726 */
1727void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1728{
Steven Rostedt554f7862009-03-11 22:00:13 -04001729 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05001730 unsigned long flags;
1731
Steven Rostedt554f7862009-03-11 22:00:13 -04001732 if (!iter)
1733 return;
1734
1735 cpu_buffer = iter->cpu_buffer;
1736
Steven Rostedt642edba2008-11-12 00:01:26 -05001737 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1738 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001739 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001740}
Robert Richterc4f50182008-12-11 16:49:22 +01001741EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001742
1743/**
1744 * ring_buffer_iter_empty - check if an iterator has no more to read
1745 * @iter: The iterator to check
1746 */
1747int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1748{
1749 struct ring_buffer_per_cpu *cpu_buffer;
1750
1751 cpu_buffer = iter->cpu_buffer;
1752
Steven Rostedtbf41a152008-10-04 02:00:59 -04001753 return iter->head_page == cpu_buffer->commit_page &&
1754 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001755}
Robert Richterc4f50182008-12-11 16:49:22 +01001756EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001757
1758static void
1759rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1760 struct ring_buffer_event *event)
1761{
1762 u64 delta;
1763
1764 switch (event->type) {
1765 case RINGBUF_TYPE_PADDING:
1766 return;
1767
1768 case RINGBUF_TYPE_TIME_EXTEND:
1769 delta = event->array[0];
1770 delta <<= TS_SHIFT;
1771 delta += event->time_delta;
1772 cpu_buffer->read_stamp += delta;
1773 return;
1774
1775 case RINGBUF_TYPE_TIME_STAMP:
1776 /* FIXME: not implemented */
1777 return;
1778
1779 case RINGBUF_TYPE_DATA:
1780 cpu_buffer->read_stamp += event->time_delta;
1781 return;
1782
1783 default:
1784 BUG();
1785 }
1786 return;
1787}
1788
1789static void
1790rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1791 struct ring_buffer_event *event)
1792{
1793 u64 delta;
1794
1795 switch (event->type) {
1796 case RINGBUF_TYPE_PADDING:
1797 return;
1798
1799 case RINGBUF_TYPE_TIME_EXTEND:
1800 delta = event->array[0];
1801 delta <<= TS_SHIFT;
1802 delta += event->time_delta;
1803 iter->read_stamp += delta;
1804 return;
1805
1806 case RINGBUF_TYPE_TIME_STAMP:
1807 /* FIXME: not implemented */
1808 return;
1809
1810 case RINGBUF_TYPE_DATA:
1811 iter->read_stamp += event->time_delta;
1812 return;
1813
1814 default:
1815 BUG();
1816 }
1817 return;
1818}
1819
Steven Rostedtd7690412008-10-01 00:29:53 -04001820static struct buffer_page *
1821rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001822{
Steven Rostedtd7690412008-10-01 00:29:53 -04001823 struct buffer_page *reader = NULL;
1824 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001825 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001826
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001827 local_irq_save(flags);
1828 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04001829
1830 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001831 /*
1832 * This should normally only loop twice. But because the
1833 * start of the reader inserts an empty page, it causes
1834 * a case where we will loop three times. There should be no
1835 * reason to loop four times (that I know of).
1836 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001837 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001838 reader = NULL;
1839 goto out;
1840 }
1841
Steven Rostedtd7690412008-10-01 00:29:53 -04001842 reader = cpu_buffer->reader_page;
1843
1844 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001845 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04001846 goto out;
1847
1848 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001849 if (RB_WARN_ON(cpu_buffer,
1850 cpu_buffer->reader_page->read > rb_page_size(reader)))
1851 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04001852
1853 /* check if we caught up to the tail */
1854 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001855 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04001856 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001857
1858 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04001859 * Splice the empty reader page into the list around the head.
1860 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001861 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001862
Steven Rostedtd7690412008-10-01 00:29:53 -04001863 reader = cpu_buffer->head_page;
1864 cpu_buffer->reader_page->list.next = reader->list.next;
1865 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001866
1867 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001868 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001869
1870 /* Make the reader page now replace the head */
1871 reader->list.prev->next = &cpu_buffer->reader_page->list;
1872 reader->list.next->prev = &cpu_buffer->reader_page->list;
1873
1874 /*
1875 * If the tail is on the reader, then we must set the head
1876 * to the inserted page, otherwise we set it one before.
1877 */
1878 cpu_buffer->head_page = cpu_buffer->reader_page;
1879
Steven Rostedtbf41a152008-10-04 02:00:59 -04001880 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04001881 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1882
1883 /* Finally update the reader page to the new head */
1884 cpu_buffer->reader_page = reader;
1885 rb_reset_reader_page(cpu_buffer);
1886
1887 goto again;
1888
1889 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001890 __raw_spin_unlock(&cpu_buffer->lock);
1891 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04001892
1893 return reader;
1894}
1895
1896static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1897{
1898 struct ring_buffer_event *event;
1899 struct buffer_page *reader;
1900 unsigned length;
1901
1902 reader = rb_get_reader_page(cpu_buffer);
1903
1904 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001905 if (RB_WARN_ON(cpu_buffer, !reader))
1906 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001907
1908 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001909
1910 if (event->type == RINGBUF_TYPE_DATA)
1911 cpu_buffer->entries--;
1912
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001913 rb_update_read_stamp(cpu_buffer, event);
1914
Steven Rostedtd7690412008-10-01 00:29:53 -04001915 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001916 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001917}
1918
1919static void rb_advance_iter(struct ring_buffer_iter *iter)
1920{
1921 struct ring_buffer *buffer;
1922 struct ring_buffer_per_cpu *cpu_buffer;
1923 struct ring_buffer_event *event;
1924 unsigned length;
1925
1926 cpu_buffer = iter->cpu_buffer;
1927 buffer = cpu_buffer->buffer;
1928
1929 /*
1930 * Check if we are at the end of the buffer.
1931 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001932 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001933 if (RB_WARN_ON(buffer,
1934 iter->head_page == cpu_buffer->commit_page))
1935 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001936 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001937 return;
1938 }
1939
1940 event = rb_iter_head_event(iter);
1941
1942 length = rb_event_length(event);
1943
1944 /*
1945 * This should not be called to advance the header if we are
1946 * at the tail of the buffer.
1947 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001948 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001949 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001950 (iter->head + length > rb_commit_index(cpu_buffer))))
1951 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001952
1953 rb_update_iter_read_stamp(iter, event);
1954
1955 iter->head += length;
1956
1957 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001958 if ((iter->head >= rb_page_size(iter->head_page)) &&
1959 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001960 rb_advance_iter(iter);
1961}
1962
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001963static struct ring_buffer_event *
1964rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001965{
1966 struct ring_buffer_per_cpu *cpu_buffer;
1967 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04001968 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001969 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001970
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001971 cpu_buffer = buffer->buffers[cpu];
1972
1973 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001974 /*
1975 * We repeat when a timestamp is encountered. It is possible
1976 * to get multiple timestamps from an interrupt entering just
1977 * as one timestamp is about to be written. The max times
1978 * that this can happen is the number of nested interrupts we
1979 * can have. Nesting 10 deep of interrupts is clearly
1980 * an anomaly.
1981 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001982 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001983 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001984
Steven Rostedtd7690412008-10-01 00:29:53 -04001985 reader = rb_get_reader_page(cpu_buffer);
1986 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001987 return NULL;
1988
Steven Rostedtd7690412008-10-01 00:29:53 -04001989 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001990
1991 switch (event->type) {
1992 case RINGBUF_TYPE_PADDING:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001993 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedtd7690412008-10-01 00:29:53 -04001994 rb_advance_reader(cpu_buffer);
1995 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001996
1997 case RINGBUF_TYPE_TIME_EXTEND:
1998 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04001999 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002000 goto again;
2001
2002 case RINGBUF_TYPE_TIME_STAMP:
2003 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002004 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002005 goto again;
2006
2007 case RINGBUF_TYPE_DATA:
2008 if (ts) {
2009 *ts = cpu_buffer->read_stamp + event->time_delta;
2010 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2011 }
2012 return event;
2013
2014 default:
2015 BUG();
2016 }
2017
2018 return NULL;
2019}
Robert Richterc4f50182008-12-11 16:49:22 +01002020EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002021
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002022static struct ring_buffer_event *
2023rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002024{
2025 struct ring_buffer *buffer;
2026 struct ring_buffer_per_cpu *cpu_buffer;
2027 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002028 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002029
2030 if (ring_buffer_iter_empty(iter))
2031 return NULL;
2032
2033 cpu_buffer = iter->cpu_buffer;
2034 buffer = cpu_buffer->buffer;
2035
2036 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002037 /*
2038 * We repeat when a timestamp is encountered. It is possible
2039 * to get multiple timestamps from an interrupt entering just
2040 * as one timestamp is about to be written. The max times
2041 * that this can happen is the number of nested interrupts we
2042 * can have. Nesting 10 deep of interrupts is clearly
2043 * an anomaly.
2044 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002045 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002046 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002047
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002048 if (rb_per_cpu_empty(cpu_buffer))
2049 return NULL;
2050
2051 event = rb_iter_head_event(iter);
2052
2053 switch (event->type) {
2054 case RINGBUF_TYPE_PADDING:
Steven Rostedtd7690412008-10-01 00:29:53 -04002055 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002056 goto again;
2057
2058 case RINGBUF_TYPE_TIME_EXTEND:
2059 /* Internal data, OK to advance */
2060 rb_advance_iter(iter);
2061 goto again;
2062
2063 case RINGBUF_TYPE_TIME_STAMP:
2064 /* FIXME: not implemented */
2065 rb_advance_iter(iter);
2066 goto again;
2067
2068 case RINGBUF_TYPE_DATA:
2069 if (ts) {
2070 *ts = iter->read_stamp + event->time_delta;
2071 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2072 }
2073 return event;
2074
2075 default:
2076 BUG();
2077 }
2078
2079 return NULL;
2080}
Robert Richterc4f50182008-12-11 16:49:22 +01002081EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002082
2083/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002084 * ring_buffer_peek - peek at the next event to be read
2085 * @buffer: The ring buffer to read
2086 * @cpu: The cpu to peak at
2087 * @ts: The timestamp counter of this event.
2088 *
2089 * This will return the event that will be read next, but does
2090 * not consume the data.
2091 */
2092struct ring_buffer_event *
2093ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2094{
2095 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002096 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002097 unsigned long flags;
2098
Steven Rostedt554f7862009-03-11 22:00:13 -04002099 get_online_cpus();
2100
2101 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2102 goto out;
2103
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002104 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2105 event = rb_buffer_peek(buffer, cpu, ts);
2106 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2107
Steven Rostedt554f7862009-03-11 22:00:13 -04002108 out:
2109 put_online_cpus();
2110
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002111 return event;
2112}
2113
2114/**
2115 * ring_buffer_iter_peek - peek at the next event to be read
2116 * @iter: The ring buffer iterator
2117 * @ts: The timestamp counter of this event.
2118 *
2119 * This will return the event that will be read next, but does
2120 * not increment the iterator.
2121 */
2122struct ring_buffer_event *
2123ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2124{
2125 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2126 struct ring_buffer_event *event;
2127 unsigned long flags;
2128
2129 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2130 event = rb_iter_peek(iter, ts);
2131 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2132
2133 return event;
2134}
2135
2136/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002137 * ring_buffer_consume - return an event and consume it
2138 * @buffer: The ring buffer to get the next event from
2139 *
2140 * Returns the next event in the ring buffer, and that event is consumed.
2141 * Meaning, that sequential reads will keep returning a different event,
2142 * and eventually empty the ring buffer if the producer is slower.
2143 */
2144struct ring_buffer_event *
2145ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2146{
Steven Rostedt554f7862009-03-11 22:00:13 -04002147 struct ring_buffer_per_cpu *cpu_buffer;
2148 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002149 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002150
Steven Rostedt554f7862009-03-11 22:00:13 -04002151 /* might be called in atomic */
2152 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002153
Steven Rostedt554f7862009-03-11 22:00:13 -04002154 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2155 goto out;
2156
2157 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002158 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002159
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002160 event = rb_buffer_peek(buffer, cpu, ts);
2161 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002162 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002163
Steven Rostedtd7690412008-10-01 00:29:53 -04002164 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002165
Steven Rostedt554f7862009-03-11 22:00:13 -04002166 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002167 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2168
Steven Rostedt554f7862009-03-11 22:00:13 -04002169 out:
2170 preempt_enable();
2171
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002172 return event;
2173}
Robert Richterc4f50182008-12-11 16:49:22 +01002174EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002175
2176/**
2177 * ring_buffer_read_start - start a non consuming read of the buffer
2178 * @buffer: The ring buffer to read from
2179 * @cpu: The cpu buffer to iterate over
2180 *
2181 * This starts up an iteration through the buffer. It also disables
2182 * the recording to the buffer until the reading is finished.
2183 * This prevents the reading from being corrupted. This is not
2184 * a consuming read, so a producer is not expected.
2185 *
2186 * Must be paired with ring_buffer_finish.
2187 */
2188struct ring_buffer_iter *
2189ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2190{
2191 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt554f7862009-03-11 22:00:13 -04002192 struct ring_buffer_iter *iter = NULL;
Steven Rostedtd7690412008-10-01 00:29:53 -04002193 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002194
Steven Rostedt554f7862009-03-11 22:00:13 -04002195 get_online_cpus();
2196
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302197 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002198 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002199
2200 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2201 if (!iter)
Steven Rostedt554f7862009-03-11 22:00:13 -04002202 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002203
2204 cpu_buffer = buffer->buffers[cpu];
2205
2206 iter->cpu_buffer = cpu_buffer;
2207
2208 atomic_inc(&cpu_buffer->record_disabled);
2209 synchronize_sched();
2210
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002211 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002212 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002213 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002214 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002215 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002216
Steven Rostedt554f7862009-03-11 22:00:13 -04002217 out:
2218 put_online_cpus();
2219
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002220 return iter;
2221}
Robert Richterc4f50182008-12-11 16:49:22 +01002222EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002223
2224/**
2225 * ring_buffer_finish - finish reading the iterator of the buffer
2226 * @iter: The iterator retrieved by ring_buffer_start
2227 *
2228 * This re-enables the recording to the buffer, and frees the
2229 * iterator.
2230 */
2231void
2232ring_buffer_read_finish(struct ring_buffer_iter *iter)
2233{
2234 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2235
2236 atomic_dec(&cpu_buffer->record_disabled);
2237 kfree(iter);
2238}
Robert Richterc4f50182008-12-11 16:49:22 +01002239EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002240
2241/**
2242 * ring_buffer_read - read the next item in the ring buffer by the iterator
2243 * @iter: The ring buffer iterator
2244 * @ts: The time stamp of the event read.
2245 *
2246 * This reads the next event in the ring buffer and increments the iterator.
2247 */
2248struct ring_buffer_event *
2249ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2250{
2251 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002252 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2253 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002254
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002255 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2256 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002257 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002258 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
2260 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002261 out:
2262 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002263
2264 return event;
2265}
Robert Richterc4f50182008-12-11 16:49:22 +01002266EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002267
2268/**
2269 * ring_buffer_size - return the size of the ring buffer (in bytes)
2270 * @buffer: The ring buffer.
2271 */
2272unsigned long ring_buffer_size(struct ring_buffer *buffer)
2273{
2274 return BUF_PAGE_SIZE * buffer->pages;
2275}
Robert Richterc4f50182008-12-11 16:49:22 +01002276EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002277
2278static void
2279rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2280{
2281 cpu_buffer->head_page
2282 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002283 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002284 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002285
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002286 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002287
2288 cpu_buffer->tail_page = cpu_buffer->head_page;
2289 cpu_buffer->commit_page = cpu_buffer->head_page;
2290
2291 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2292 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002293 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002294 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002295
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002296 cpu_buffer->overrun = 0;
2297 cpu_buffer->entries = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05002298
2299 cpu_buffer->write_stamp = 0;
2300 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002301}
2302
2303/**
2304 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2305 * @buffer: The ring buffer to reset a per cpu buffer of
2306 * @cpu: The CPU buffer to be reset
2307 */
2308void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2309{
2310 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2311 unsigned long flags;
Steven Rostedt554f7862009-03-11 22:00:13 -04002312 int resched;
2313
2314 /* Can't use get_online_cpus because this can be in atomic */
2315 resched = ftrace_preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002316
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302317 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002318 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002319
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002320 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2321
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002322 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002323
2324 rb_reset_cpu(cpu_buffer);
2325
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002326 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002327
2328 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04002329 out:
2330 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331}
Robert Richterc4f50182008-12-11 16:49:22 +01002332EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002333
2334/**
2335 * ring_buffer_reset - reset a ring buffer
2336 * @buffer: The ring buffer to reset all cpu buffers
2337 */
2338void ring_buffer_reset(struct ring_buffer *buffer)
2339{
Steven Rostedt554f7862009-03-11 22:00:13 -04002340 int resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002341 int cpu;
2342
Steven Rostedt554f7862009-03-11 22:00:13 -04002343 /* Can't use get_online_cpus because this can be in atomic */
2344 resched = ftrace_preempt_disable();
2345
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002346 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002347 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04002348
2349 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002350}
Robert Richterc4f50182008-12-11 16:49:22 +01002351EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002352
2353/**
2354 * rind_buffer_empty - is the ring buffer empty?
2355 * @buffer: The ring buffer to test
2356 */
2357int ring_buffer_empty(struct ring_buffer *buffer)
2358{
2359 struct ring_buffer_per_cpu *cpu_buffer;
2360 int cpu;
2361
Steven Rostedt554f7862009-03-11 22:00:13 -04002362 get_online_cpus();
2363
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002364 /* yes this is racy, but if you don't like the race, lock the buffer */
2365 for_each_buffer_cpu(buffer, cpu) {
2366 cpu_buffer = buffer->buffers[cpu];
2367 if (!rb_per_cpu_empty(cpu_buffer))
2368 return 0;
2369 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002370
2371 put_online_cpus();
2372
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002373 return 1;
2374}
Robert Richterc4f50182008-12-11 16:49:22 +01002375EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002376
2377/**
2378 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2379 * @buffer: The ring buffer
2380 * @cpu: The CPU buffer to test
2381 */
2382int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2383{
2384 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt554f7862009-03-11 22:00:13 -04002385 int ret = 1;
2386
2387 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002388
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302389 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002390 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002391
2392 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002393 ret = rb_per_cpu_empty(cpu_buffer);
2394
2395 out:
2396 put_online_cpus();
2397
2398 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002399}
Robert Richterc4f50182008-12-11 16:49:22 +01002400EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002401
2402/**
2403 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2404 * @buffer_a: One buffer to swap with
2405 * @buffer_b: The other buffer to swap with
2406 *
2407 * This function is useful for tracers that want to take a "snapshot"
2408 * of a CPU buffer and has another back up buffer lying around.
2409 * it is expected that the tracer handles the cpu buffer not being
2410 * used at the moment.
2411 */
2412int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2413 struct ring_buffer *buffer_b, int cpu)
2414{
2415 struct ring_buffer_per_cpu *cpu_buffer_a;
2416 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002417 int ret = -EINVAL;
2418
2419 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002420
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302421 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2422 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002423 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002424
2425 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002426 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002427 goto out;
2428
2429 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002430
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002431 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002432 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002433
2434 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002435 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002436
2437 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002438 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002439
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002440 cpu_buffer_a = buffer_a->buffers[cpu];
2441 cpu_buffer_b = buffer_b->buffers[cpu];
2442
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002443 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002444 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002445
2446 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002447 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002448
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002449 /*
2450 * We can't do a synchronize_sched here because this
2451 * function can be called in atomic context.
2452 * Normally this will be called from the same CPU as cpu.
2453 * If not it's up to the caller to protect this.
2454 */
2455 atomic_inc(&cpu_buffer_a->record_disabled);
2456 atomic_inc(&cpu_buffer_b->record_disabled);
2457
2458 buffer_a->buffers[cpu] = cpu_buffer_b;
2459 buffer_b->buffers[cpu] = cpu_buffer_a;
2460
2461 cpu_buffer_b->buffer = buffer_a;
2462 cpu_buffer_a->buffer = buffer_b;
2463
2464 atomic_dec(&cpu_buffer_a->record_disabled);
2465 atomic_dec(&cpu_buffer_b->record_disabled);
2466
Steven Rostedt554f7862009-03-11 22:00:13 -04002467 ret = 0;
2468out:
2469 put_online_cpus();
2470
2471 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002472}
Robert Richterc4f50182008-12-11 16:49:22 +01002473EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002474
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002475static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002476 struct buffer_data_page *bpage,
2477 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002478{
2479 struct ring_buffer_event *event;
2480 unsigned long head;
2481
2482 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002483 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002484 head += rb_event_length(event)) {
2485
Steven Rostedt044fa782008-12-02 23:50:03 -05002486 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002487 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2488 return;
2489 /* Only count data entries */
2490 if (event->type != RINGBUF_TYPE_DATA)
2491 continue;
2492 cpu_buffer->entries--;
2493 }
2494 __raw_spin_unlock(&cpu_buffer->lock);
2495}
2496
2497/**
2498 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2499 * @buffer: the buffer to allocate for.
2500 *
2501 * This function is used in conjunction with ring_buffer_read_page.
2502 * When reading a full page from the ring buffer, these functions
2503 * can be used to speed up the process. The calling function should
2504 * allocate a few pages first with this function. Then when it
2505 * needs to get pages from the ring buffer, it passes the result
2506 * of this function into ring_buffer_read_page, which will swap
2507 * the page that was allocated, with the read page of the buffer.
2508 *
2509 * Returns:
2510 * The page allocated, or NULL on error.
2511 */
2512void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2513{
Steven Rostedt044fa782008-12-02 23:50:03 -05002514 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002515 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002516
2517 addr = __get_free_page(GFP_KERNEL);
2518 if (!addr)
2519 return NULL;
2520
Steven Rostedt044fa782008-12-02 23:50:03 -05002521 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002522
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002523 rb_init_page(bpage);
2524
Steven Rostedt044fa782008-12-02 23:50:03 -05002525 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002526}
2527
2528/**
2529 * ring_buffer_free_read_page - free an allocated read page
2530 * @buffer: the buffer the page was allocate for
2531 * @data: the page to free
2532 *
2533 * Free a page allocated from ring_buffer_alloc_read_page.
2534 */
2535void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2536{
2537 free_page((unsigned long)data);
2538}
2539
2540/**
2541 * ring_buffer_read_page - extract a page from the ring buffer
2542 * @buffer: buffer to extract from
2543 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002544 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002545 * @cpu: the cpu of the buffer to extract
2546 * @full: should the extraction only happen when the page is full.
2547 *
2548 * This function will pull out a page from the ring buffer and consume it.
2549 * @data_page must be the address of the variable that was returned
2550 * from ring_buffer_alloc_read_page. This is because the page might be used
2551 * to swap with a page in the ring buffer.
2552 *
2553 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002554 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002555 * if (!rpage)
2556 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002557 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002558 * if (ret >= 0)
2559 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002560 *
2561 * When @full is set, the function will not return true unless
2562 * the writer is off the reader page.
2563 *
2564 * Note: it is up to the calling functions to handle sleeps and wakeups.
2565 * The ring buffer can be used anywhere in the kernel and can not
2566 * blindly call wake_up. The layer that uses the ring buffer must be
2567 * responsible for that.
2568 *
2569 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002570 * >=0 if data has been transferred, returns the offset of consumed data.
2571 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002572 */
2573int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002574 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002575{
2576 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2577 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002578 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002579 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002580 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002581 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002582 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002583 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002584 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002585
Steven Rostedt554f7862009-03-11 22:00:13 -04002586 get_online_cpus();
2587
2588 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2589 goto out;
2590
Steven Rostedt474d32b2009-03-03 19:51:40 -05002591 /*
2592 * If len is not big enough to hold the page header, then
2593 * we can not copy anything.
2594 */
2595 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002596 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002597
2598 len -= BUF_PAGE_HDR_SIZE;
2599
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002600 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002601 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002602
Steven Rostedt044fa782008-12-02 23:50:03 -05002603 bpage = *data_page;
2604 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002605 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002606
2607 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2608
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002609 reader = rb_get_reader_page(cpu_buffer);
2610 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002611 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002612
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002613 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002614
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002615 read = reader->read;
2616 commit = rb_page_commit(reader);
2617
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002618 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002619 * If this page has been partially read or
2620 * if len is not big enough to read the rest of the page or
2621 * a writer is still on the page, then
2622 * we must copy the data from the page to the buffer.
2623 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002624 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002625 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002626 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002627 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002628 unsigned int rpos = read;
2629 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002630 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002631
2632 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002633 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002634
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002635 if (len > (commit - read))
2636 len = (commit - read);
2637
2638 size = rb_event_length(event);
2639
2640 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002641 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002642
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002643 /* save the current timestamp, since the user will need it */
2644 save_timestamp = cpu_buffer->read_stamp;
2645
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002646 /* Need to copy one event at a time */
2647 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002648 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002649
2650 len -= size;
2651
2652 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002653 rpos = reader->read;
2654 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002655
2656 event = rb_reader_event(cpu_buffer);
2657 size = rb_event_length(event);
2658 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002659
2660 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002661 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002662 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002663
Steven Rostedt474d32b2009-03-03 19:51:40 -05002664 /* we copied everything to the beginning */
2665 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002666 } else {
2667 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002668 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002669 bpage = reader->page;
2670 reader->page = *data_page;
2671 local_set(&reader->write, 0);
2672 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002673 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002674
2675 /* update the entry counter */
2676 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002677 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002678 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002679
Steven Rostedt554f7862009-03-11 22:00:13 -04002680 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002681 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2682
Steven Rostedt554f7862009-03-11 22:00:13 -04002683 out:
2684 put_online_cpus();
2685
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002686 return ret;
2687}
2688
Steven Rostedta3583242008-11-11 15:01:42 -05002689static ssize_t
2690rb_simple_read(struct file *filp, char __user *ubuf,
2691 size_t cnt, loff_t *ppos)
2692{
Hannes Eder5e398412009-02-10 19:44:34 +01002693 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002694 char buf[64];
2695 int r;
2696
Steven Rostedt033601a2008-11-21 12:41:55 -05002697 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2698 r = sprintf(buf, "permanently disabled\n");
2699 else
2700 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002701
2702 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2703}
2704
2705static ssize_t
2706rb_simple_write(struct file *filp, const char __user *ubuf,
2707 size_t cnt, loff_t *ppos)
2708{
Hannes Eder5e398412009-02-10 19:44:34 +01002709 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002710 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002711 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05002712 int ret;
2713
2714 if (cnt >= sizeof(buf))
2715 return -EINVAL;
2716
2717 if (copy_from_user(&buf, ubuf, cnt))
2718 return -EFAULT;
2719
2720 buf[cnt] = 0;
2721
2722 ret = strict_strtoul(buf, 10, &val);
2723 if (ret < 0)
2724 return ret;
2725
Steven Rostedt033601a2008-11-21 12:41:55 -05002726 if (val)
2727 set_bit(RB_BUFFERS_ON_BIT, p);
2728 else
2729 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05002730
2731 (*ppos)++;
2732
2733 return cnt;
2734}
2735
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002736static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05002737 .open = tracing_open_generic,
2738 .read = rb_simple_read,
2739 .write = rb_simple_write,
2740};
2741
2742
2743static __init int rb_init_debugfs(void)
2744{
2745 struct dentry *d_tracer;
2746 struct dentry *entry;
2747
2748 d_tracer = tracing_init_dentry();
2749
2750 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
Steven Rostedt033601a2008-11-21 12:41:55 -05002751 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05002752 if (!entry)
2753 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2754
2755 return 0;
2756}
2757
2758fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04002759
Steven Rostedt59222ef2009-03-12 11:46:03 -04002760#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04002761static int __cpuinit rb_cpu_notify(struct notifier_block *self,
2762 unsigned long action, void *hcpu)
2763{
2764 struct ring_buffer *buffer =
2765 container_of(self, struct ring_buffer, cpu_notify);
2766 long cpu = (long)hcpu;
2767
2768 switch (action) {
2769 case CPU_UP_PREPARE:
2770 case CPU_UP_PREPARE_FROZEN:
2771 if (cpu_isset(cpu, *buffer->cpumask))
2772 return NOTIFY_OK;
2773
2774 buffer->buffers[cpu] =
2775 rb_allocate_cpu_buffer(buffer, cpu);
2776 if (!buffer->buffers[cpu]) {
2777 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2778 cpu);
2779 return NOTIFY_OK;
2780 }
2781 smp_wmb();
2782 cpu_set(cpu, *buffer->cpumask);
2783 break;
2784 case CPU_DOWN_PREPARE:
2785 case CPU_DOWN_PREPARE_FROZEN:
2786 /*
2787 * Do nothing.
2788 * If we were to free the buffer, then the user would
2789 * lose any trace that was in the buffer.
2790 */
2791 break;
2792 default:
2793 break;
2794 }
2795 return NOTIFY_OK;
2796}
2797#endif