blob: 7c0168ad6d51559f039248e9066ae6f3bd397f12 [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>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Steven Rostedt182e9f52008-11-03 23:15:56 -050023#include "trace.h"
24
Steven Rostedt033601a2008-11-21 12:41:55 -050025/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040026 * The ring buffer header is special. We must manually up keep it.
27 */
28int ring_buffer_print_entry_header(struct trace_seq *s)
29{
30 int ret;
31
Lai Jiangshan334d4162009-04-24 11:27:05 +080032 ret = trace_seq_printf(s, "# compressed entry header\n");
33 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040034 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
35 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
36 ret = trace_seq_printf(s, "\n");
37 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
38 RINGBUF_TYPE_PADDING);
39 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
40 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080041 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
42 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040043
44 return ret;
45}
46
47/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040048 * The ring buffer is made up of a list of pages. A separate list of pages is
49 * allocated for each CPU. A writer may only write to a buffer that is
50 * associated with the CPU it is currently executing on. A reader may read
51 * from any per cpu buffer.
52 *
53 * The reader is special. For each per cpu buffer, the reader has its own
54 * reader page. When a reader has read the entire reader page, this reader
55 * page is swapped with another page in the ring buffer.
56 *
57 * Now, as long as the writer is off the reader page, the reader can do what
58 * ever it wants with that page. The writer will never write to that page
59 * again (as long as it is out of the ring buffer).
60 *
61 * Here's some silly ASCII art.
62 *
63 * +------+
64 * |reader| RING BUFFER
65 * |page |
66 * +------+ +---+ +---+ +---+
67 * | |-->| |-->| |
68 * +---+ +---+ +---+
69 * ^ |
70 * | |
71 * +---------------+
72 *
73 *
74 * +------+
75 * |reader| RING BUFFER
76 * |page |------------------v
77 * +------+ +---+ +---+ +---+
78 * | |-->| |-->| |
79 * +---+ +---+ +---+
80 * ^ |
81 * | |
82 * +---------------+
83 *
84 *
85 * +------+
86 * |reader| RING BUFFER
87 * |page |------------------v
88 * +------+ +---+ +---+ +---+
89 * ^ | |-->| |-->| |
90 * | +---+ +---+ +---+
91 * | |
92 * | |
93 * +------------------------------+
94 *
95 *
96 * +------+
97 * |buffer| RING BUFFER
98 * |page |------------------v
99 * +------+ +---+ +---+ +---+
100 * ^ | | | |-->| |
101 * | New +---+ +---+ +---+
102 * | Reader------^ |
103 * | page |
104 * +------------------------------+
105 *
106 *
107 * After we make this swap, the reader can hand this page off to the splice
108 * code and be done with it. It can even allocate a new page if it needs to
109 * and swap that into the ring buffer.
110 *
111 * We will be using cmpxchg soon to make all this lockless.
112 *
113 */
114
115/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500116 * A fast way to enable or disable all ring buffers is to
117 * call tracing_on or tracing_off. Turning off the ring buffers
118 * prevents all ring buffers from being recorded to.
119 * Turning this switch on, makes it OK to write to the
120 * ring buffer, if the ring buffer is enabled itself.
121 *
122 * There's three layers that must be on in order to write
123 * to the ring buffer.
124 *
125 * 1) This global flag must be set.
126 * 2) The ring buffer must be enabled for recording.
127 * 3) The per cpu buffer must be enabled for recording.
128 *
129 * In case of an anomaly, this global flag has a bit set that
130 * will permantly disable all ring buffers.
131 */
132
133/*
134 * Global flag to disable all recording to ring buffers
135 * This has two bits: ON, DISABLED
136 *
137 * ON DISABLED
138 * ---- ----------
139 * 0 0 : ring buffers are off
140 * 1 0 : ring buffers are on
141 * X 1 : ring buffers are permanently disabled
142 */
143
144enum {
145 RB_BUFFERS_ON_BIT = 0,
146 RB_BUFFERS_DISABLED_BIT = 1,
147};
148
149enum {
150 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
151 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
152};
153
Hannes Eder5e398412009-02-10 19:44:34 +0100154static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500155
Steven Rostedt474d32b2009-03-03 19:51:40 -0500156#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
157
Steven Rostedta3583242008-11-11 15:01:42 -0500158/**
159 * tracing_on - enable all tracing buffers
160 *
161 * This function enables all tracing buffers that may have been
162 * disabled with tracing_off.
163 */
164void tracing_on(void)
165{
Steven Rostedt033601a2008-11-21 12:41:55 -0500166 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500167}
Robert Richterc4f50182008-12-11 16:49:22 +0100168EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500169
170/**
171 * tracing_off - turn off all tracing buffers
172 *
173 * This function stops all tracing buffers from recording data.
174 * It does not disable any overhead the tracers themselves may
175 * be causing. This function simply causes all recording to
176 * the ring buffers to fail.
177 */
178void tracing_off(void)
179{
Steven Rostedt033601a2008-11-21 12:41:55 -0500180 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
181}
Robert Richterc4f50182008-12-11 16:49:22 +0100182EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500183
184/**
185 * tracing_off_permanent - permanently disable ring buffers
186 *
187 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500188 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500189 */
190void tracing_off_permanent(void)
191{
192 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500193}
194
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500195/**
196 * tracing_is_on - show state of ring buffers enabled
197 */
198int tracing_is_on(void)
199{
200 return ring_buffer_flags == RB_BUFFERS_ON;
201}
202EXPORT_SYMBOL_GPL(tracing_is_on);
203
Ingo Molnard06bbd62008-11-12 10:11:37 +0100204#include "trace.h"
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
211/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
212#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400213
214enum {
215 RB_LEN_TIME_EXTEND = 8,
216 RB_LEN_TIME_STAMP = 16,
217};
218
Tom Zanussi2d622712009-03-22 03:30:49 -0500219static inline int rb_null_event(struct ring_buffer_event *event)
220{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800221 return event->type_len == RINGBUF_TYPE_PADDING
222 && event->time_delta == 0;
Tom Zanussi2d622712009-03-22 03:30:49 -0500223}
224
225static inline int rb_discarded_event(struct ring_buffer_event *event)
226{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800227 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500228}
229
230static void rb_event_set_padding(struct ring_buffer_event *event)
231{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800232 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500233 event->time_delta = 0;
234}
235
Tom Zanussi2d622712009-03-22 03:30:49 -0500236static unsigned
237rb_event_data_length(struct ring_buffer_event *event)
238{
239 unsigned length;
240
Lai Jiangshan334d4162009-04-24 11:27:05 +0800241 if (event->type_len)
242 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500243 else
244 length = event->array[0];
245 return length + RB_EVNT_HDR_SIZE;
246}
247
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800249static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250rb_event_length(struct ring_buffer_event *event)
251{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800252 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500254 if (rb_null_event(event))
255 /* undefined */
256 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800257 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400258
259 case RINGBUF_TYPE_TIME_EXTEND:
260 return RB_LEN_TIME_EXTEND;
261
262 case RINGBUF_TYPE_TIME_STAMP:
263 return RB_LEN_TIME_STAMP;
264
265 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500266 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400267 default:
268 BUG();
269 }
270 /* not hit */
271 return 0;
272}
273
274/**
275 * ring_buffer_event_length - return the length of the event
276 * @event: the event to get the length of
277 */
278unsigned ring_buffer_event_length(struct ring_buffer_event *event)
279{
Robert Richter465634a2009-01-07 15:32:11 +0100280 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800281 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100282 return length;
283 length -= RB_EVNT_HDR_SIZE;
284 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
285 length -= sizeof(event->array[0]);
286 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400287}
Robert Richterc4f50182008-12-11 16:49:22 +0100288EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289
290/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800291static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400292rb_event_data(struct ring_buffer_event *event)
293{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800294 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400295 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800296 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297 return (void *)&event->array[0];
298 /* Otherwise length is in array[0] and array[1] has the data */
299 return (void *)&event->array[1];
300}
301
302/**
303 * ring_buffer_event_data - return the data of the event
304 * @event: the event to get the data from
305 */
306void *ring_buffer_event_data(struct ring_buffer_event *event)
307{
308 return rb_event_data(event);
309}
Robert Richterc4f50182008-12-11 16:49:22 +0100310EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400311
312#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030313 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400314
315#define TS_SHIFT 27
316#define TS_MASK ((1ULL << TS_SHIFT) - 1)
317#define TS_DELTA_TEST (~TS_MASK)
318
Steven Rostedtabc9b562008-12-02 15:34:06 -0500319struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400320 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500321 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500322 unsigned char data[]; /* data of buffer page */
323};
324
325struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400326 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400328 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400329 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500330 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400331};
332
Steven Rostedt044fa782008-12-02 23:50:03 -0500333static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500334{
Steven Rostedt044fa782008-12-02 23:50:03 -0500335 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500336}
337
Steven Rostedt474d32b2009-03-03 19:51:40 -0500338/**
339 * ring_buffer_page_len - the size of data on the page.
340 * @page: The page to read
341 *
342 * Returns the amount of data on the page, including buffer page header.
343 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500344size_t ring_buffer_page_len(void *page)
345{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500346 return local_read(&((struct buffer_data_page *)page)->commit)
347 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500348}
349
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400350/*
Steven Rostedted568292008-09-29 23:02:40 -0400351 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
352 * this issue out.
353 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800354static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400355{
Andrew Morton34a148b2009-01-09 12:27:09 -0800356 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400357 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400358}
359
360/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400361 * We need to fit the time_stamp delta into 27 bits.
362 */
363static inline int test_time_stamp(u64 delta)
364{
365 if (delta & TS_DELTA_TEST)
366 return 1;
367 return 0;
368}
369
Steven Rostedt474d32b2009-03-03 19:51:40 -0500370#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400371
Steven Rostedtbe957c42009-05-11 14:42:53 -0400372/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
373#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
374
Steven Rostedtea05b572009-06-03 09:30:10 -0400375/* Max number of timestamps that can fit on a page */
376#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
377
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400378int ring_buffer_print_page_header(struct trace_seq *s)
379{
380 struct buffer_data_page field;
381 int ret;
382
383 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
384 "offset:0;\tsize:%u;\n",
385 (unsigned int)sizeof(field.time_stamp));
386
387 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
388 "offset:%u;\tsize:%u;\n",
389 (unsigned int)offsetof(typeof(field), commit),
390 (unsigned int)sizeof(field.commit));
391
392 ret = trace_seq_printf(s, "\tfield: char data;\t"
393 "offset:%u;\tsize:%u;\n",
394 (unsigned int)offsetof(typeof(field), data),
395 (unsigned int)BUF_PAGE_SIZE);
396
397 return ret;
398}
399
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400400/*
401 * head_page == tail_page && head == tail then buffer is empty.
402 */
403struct ring_buffer_per_cpu {
404 int cpu;
405 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100406 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500407 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400408 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400409 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400410 struct buffer_page *head_page; /* read from head */
411 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500412 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400413 struct buffer_page *reader_page;
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400414 unsigned long nmi_dropped;
415 unsigned long commit_overrun;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400416 unsigned long overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400417 unsigned long read;
418 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400419 local_t committing;
420 local_t commits;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400421 u64 write_stamp;
422 u64 read_stamp;
423 atomic_t record_disabled;
424};
425
426struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400427 unsigned pages;
428 unsigned flags;
429 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400430 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200431 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400432
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200433 struct lock_class_key *reader_lock_key;
434
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400435 struct mutex mutex;
436
437 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400438
Steven Rostedt59222ef2009-03-12 11:46:03 -0400439#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400440 struct notifier_block cpu_notify;
441#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400442 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400443};
444
445struct ring_buffer_iter {
446 struct ring_buffer_per_cpu *cpu_buffer;
447 unsigned long head;
448 struct buffer_page *head_page;
449 u64 read_stamp;
450};
451
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500452/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400453#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500454 ({ \
455 int _____ret = unlikely(cond); \
456 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400457 atomic_inc(&buffer->record_disabled); \
458 WARN_ON(1); \
459 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500460 _____ret; \
461 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500462
Steven Rostedt37886f62009-03-17 17:22:06 -0400463/* Up this if you want to test the TIME_EXTENTS and normalization */
464#define DEBUG_SHIFT 0
465
Steven Rostedt88eb0122009-05-11 16:28:23 -0400466static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
467{
468 /* shift to debug/test normalization and TIME_EXTENTS */
469 return buffer->clock() << DEBUG_SHIFT;
470}
471
Steven Rostedt37886f62009-03-17 17:22:06 -0400472u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
473{
474 u64 time;
475
476 preempt_disable_notrace();
Steven Rostedt88eb0122009-05-11 16:28:23 -0400477 time = rb_time_stamp(buffer, cpu);
Steven Rostedt37886f62009-03-17 17:22:06 -0400478 preempt_enable_no_resched_notrace();
479
480 return time;
481}
482EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
483
484void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
485 int cpu, u64 *ts)
486{
487 /* Just stupid testing the normalize function and deltas */
488 *ts >>= DEBUG_SHIFT;
489}
490EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
491
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492/**
493 * check_pages - integrity check of buffer pages
494 * @cpu_buffer: CPU buffer with pages to test
495 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500496 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400497 * been corrupted.
498 */
499static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
500{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400501 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500502 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500504 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
505 return -1;
506 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
507 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400508
Steven Rostedt044fa782008-12-02 23:50:03 -0500509 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500510 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500511 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500512 return -1;
513 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500514 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500515 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400516 }
517
518 return 0;
519}
520
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400521static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
522 unsigned nr_pages)
523{
Steven Rostedt044fa782008-12-02 23:50:03 -0500524 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400525 unsigned long addr;
526 LIST_HEAD(pages);
527 unsigned i;
528
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400529 WARN_ON(!nr_pages);
530
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400531 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500532 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400533 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500534 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400535 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500536 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400537
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400538 addr = __get_free_page(GFP_KERNEL);
539 if (!addr)
540 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500541 bpage->page = (void *)addr;
542 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400543 }
544
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400545 /*
546 * The ring buffer page list is a circular list that does not
547 * start and end with a list head. All page list items point to
548 * other pages.
549 */
550 cpu_buffer->pages = pages.next;
551 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400552
553 rb_check_pages(cpu_buffer);
554
555 return 0;
556
557 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500558 list_for_each_entry_safe(bpage, tmp, &pages, list) {
559 list_del_init(&bpage->list);
560 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400561 }
562 return -ENOMEM;
563}
564
565static struct ring_buffer_per_cpu *
566rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
567{
568 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500569 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400570 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400571 int ret;
572
573 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
574 GFP_KERNEL, cpu_to_node(cpu));
575 if (!cpu_buffer)
576 return NULL;
577
578 cpu_buffer->cpu = cpu;
579 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100580 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200581 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500582 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400583
Steven Rostedt044fa782008-12-02 23:50:03 -0500584 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400585 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500586 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400587 goto fail_free_buffer;
588
Steven Rostedt044fa782008-12-02 23:50:03 -0500589 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400590 addr = __get_free_page(GFP_KERNEL);
591 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400592 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500593 bpage->page = (void *)addr;
594 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400595
Steven Rostedtd7690412008-10-01 00:29:53 -0400596 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400597
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400598 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
599 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400600 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400601
602 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400603 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400604 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400605
606 return cpu_buffer;
607
Steven Rostedtd7690412008-10-01 00:29:53 -0400608 fail_free_reader:
609 free_buffer_page(cpu_buffer->reader_page);
610
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400611 fail_free_buffer:
612 kfree(cpu_buffer);
613 return NULL;
614}
615
616static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
617{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400618 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500619 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400620
Steven Rostedtd7690412008-10-01 00:29:53 -0400621 free_buffer_page(cpu_buffer->reader_page);
622
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400623 if (head) {
624 list_for_each_entry_safe(bpage, tmp, head, list) {
625 list_del_init(&bpage->list);
626 free_buffer_page(bpage);
627 }
628 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -0500629 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400630 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400631
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400632 kfree(cpu_buffer);
633}
634
Steven Rostedt59222ef2009-03-12 11:46:03 -0400635#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100636static int rb_cpu_notify(struct notifier_block *self,
637 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400638#endif
639
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400640/**
641 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100642 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400643 * @flags: attributes to set for the ring buffer.
644 *
645 * Currently the only flag that is available is the RB_FL_OVERWRITE
646 * flag. This flag means that the buffer will overwrite old data
647 * when the buffer wraps. If this flag is not set, the buffer will
648 * drop data when the tail hits the head.
649 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200650struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
651 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400652{
653 struct ring_buffer *buffer;
654 int bsize;
655 int cpu;
656
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400657 /* keep it in its own cache line */
658 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
659 GFP_KERNEL);
660 if (!buffer)
661 return NULL;
662
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030663 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
664 goto fail_free_buffer;
665
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400666 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
667 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400668 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200669 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400670
671 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -0400672 if (buffer->pages < 2)
673 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400674
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100675 /*
676 * In case of non-hotplug cpu, if the ring-buffer is allocated
677 * in early initcall, it will not be notified of secondary cpus.
678 * In that off case, we need to allocate for all possible cpus.
679 */
680#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400681 get_online_cpus();
682 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100683#else
684 cpumask_copy(buffer->cpumask, cpu_possible_mask);
685#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400686 buffer->cpus = nr_cpu_ids;
687
688 bsize = sizeof(void *) * nr_cpu_ids;
689 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
690 GFP_KERNEL);
691 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030692 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400693
694 for_each_buffer_cpu(buffer, cpu) {
695 buffer->buffers[cpu] =
696 rb_allocate_cpu_buffer(buffer, cpu);
697 if (!buffer->buffers[cpu])
698 goto fail_free_buffers;
699 }
700
Steven Rostedt59222ef2009-03-12 11:46:03 -0400701#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400702 buffer->cpu_notify.notifier_call = rb_cpu_notify;
703 buffer->cpu_notify.priority = 0;
704 register_cpu_notifier(&buffer->cpu_notify);
705#endif
706
707 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400708 mutex_init(&buffer->mutex);
709
710 return buffer;
711
712 fail_free_buffers:
713 for_each_buffer_cpu(buffer, cpu) {
714 if (buffer->buffers[cpu])
715 rb_free_cpu_buffer(buffer->buffers[cpu]);
716 }
717 kfree(buffer->buffers);
718
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030719 fail_free_cpumask:
720 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400721 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030722
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400723 fail_free_buffer:
724 kfree(buffer);
725 return NULL;
726}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200727EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400728
729/**
730 * ring_buffer_free - free a ring buffer.
731 * @buffer: the buffer to free.
732 */
733void
734ring_buffer_free(struct ring_buffer *buffer)
735{
736 int cpu;
737
Steven Rostedt554f7862009-03-11 22:00:13 -0400738 get_online_cpus();
739
Steven Rostedt59222ef2009-03-12 11:46:03 -0400740#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400741 unregister_cpu_notifier(&buffer->cpu_notify);
742#endif
743
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400744 for_each_buffer_cpu(buffer, cpu)
745 rb_free_cpu_buffer(buffer->buffers[cpu]);
746
Steven Rostedt554f7862009-03-11 22:00:13 -0400747 put_online_cpus();
748
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030749 free_cpumask_var(buffer->cpumask);
750
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400751 kfree(buffer);
752}
Robert Richterc4f50182008-12-11 16:49:22 +0100753EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400754
Steven Rostedt37886f62009-03-17 17:22:06 -0400755void ring_buffer_set_clock(struct ring_buffer *buffer,
756 u64 (*clock)(void))
757{
758 buffer->clock = clock;
759}
760
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400761static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
762
763static void
764rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
765{
Steven Rostedt044fa782008-12-02 23:50:03 -0500766 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400767 struct list_head *p;
768 unsigned i;
769
770 atomic_inc(&cpu_buffer->record_disabled);
771 synchronize_sched();
772
773 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400774 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500775 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400776 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500777 bpage = list_entry(p, struct buffer_page, list);
778 list_del_init(&bpage->list);
779 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400780 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400781 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500782 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400783
784 rb_reset_cpu(cpu_buffer);
785
786 rb_check_pages(cpu_buffer);
787
788 atomic_dec(&cpu_buffer->record_disabled);
789
790}
791
792static void
793rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
794 struct list_head *pages, unsigned nr_pages)
795{
Steven Rostedt044fa782008-12-02 23:50:03 -0500796 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400797 struct list_head *p;
798 unsigned i;
799
800 atomic_inc(&cpu_buffer->record_disabled);
801 synchronize_sched();
802
803 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500804 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
805 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400806 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500807 bpage = list_entry(p, struct buffer_page, list);
808 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400809 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400810 }
811 rb_reset_cpu(cpu_buffer);
812
813 rb_check_pages(cpu_buffer);
814
815 atomic_dec(&cpu_buffer->record_disabled);
816}
817
818/**
819 * ring_buffer_resize - resize the ring buffer
820 * @buffer: the buffer to resize.
821 * @size: the new size.
822 *
823 * The tracer is responsible for making sure that the buffer is
824 * not being used while changing the size.
825 * Note: We may be able to change the above requirement by using
826 * RCU synchronizations.
827 *
828 * Minimum size is 2 * BUF_PAGE_SIZE.
829 *
830 * Returns -1 on failure.
831 */
832int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
833{
834 struct ring_buffer_per_cpu *cpu_buffer;
835 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500836 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400837 unsigned long buffer_size;
838 unsigned long addr;
839 LIST_HEAD(pages);
840 int i, cpu;
841
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100842 /*
843 * Always succeed at resizing a non-existent buffer:
844 */
845 if (!buffer)
846 return size;
847
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400848 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
849 size *= BUF_PAGE_SIZE;
850 buffer_size = buffer->pages * BUF_PAGE_SIZE;
851
852 /* we need a minimum of two pages */
853 if (size < BUF_PAGE_SIZE * 2)
854 size = BUF_PAGE_SIZE * 2;
855
856 if (size == buffer_size)
857 return size;
858
859 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400860 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400861
862 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
863
864 if (size < buffer_size) {
865
866 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400867 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
868 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400869
870 rm_pages = buffer->pages - nr_pages;
871
872 for_each_buffer_cpu(buffer, cpu) {
873 cpu_buffer = buffer->buffers[cpu];
874 rb_remove_pages(cpu_buffer, rm_pages);
875 }
876 goto out;
877 }
878
879 /*
880 * This is a bit more difficult. We only want to add pages
881 * when we can allocate enough for all CPUs. We do this
882 * by allocating all the pages and storing them on a local
883 * link list. If we succeed in our allocation, then we
884 * add these pages to the cpu_buffers. Otherwise we just free
885 * them all and return -ENOMEM;
886 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400887 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
888 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500889
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400890 new_pages = nr_pages - buffer->pages;
891
892 for_each_buffer_cpu(buffer, cpu) {
893 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500894 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400895 cache_line_size()),
896 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500897 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400898 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500899 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400900 addr = __get_free_page(GFP_KERNEL);
901 if (!addr)
902 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500903 bpage->page = (void *)addr;
904 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400905 }
906 }
907
908 for_each_buffer_cpu(buffer, cpu) {
909 cpu_buffer = buffer->buffers[cpu];
910 rb_insert_pages(cpu_buffer, &pages, new_pages);
911 }
912
Steven Rostedt554f7862009-03-11 22:00:13 -0400913 if (RB_WARN_ON(buffer, !list_empty(&pages)))
914 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400915
916 out:
917 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400918 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400919 mutex_unlock(&buffer->mutex);
920
921 return size;
922
923 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500924 list_for_each_entry_safe(bpage, tmp, &pages, list) {
925 list_del_init(&bpage->list);
926 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400927 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400928 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100929 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400931
932 /*
933 * Something went totally wrong, and we are too paranoid
934 * to even clean up the mess.
935 */
936 out_fail:
937 put_online_cpus();
938 mutex_unlock(&buffer->mutex);
939 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940}
Robert Richterc4f50182008-12-11 16:49:22 +0100941EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400942
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500943static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500944__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500945{
Steven Rostedt044fa782008-12-02 23:50:03 -0500946 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500947}
948
Steven Rostedt044fa782008-12-02 23:50:03 -0500949static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400950{
Steven Rostedt044fa782008-12-02 23:50:03 -0500951 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400952}
953
954static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400955rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400957 return __rb_page_index(cpu_buffer->reader_page,
958 cpu_buffer->reader_page->read);
959}
960
961static inline struct ring_buffer_event *
962rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
963{
964 return __rb_page_index(cpu_buffer->head_page,
965 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400966}
967
968static inline struct ring_buffer_event *
969rb_iter_head_event(struct ring_buffer_iter *iter)
970{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400971 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400972}
973
Steven Rostedtbf41a152008-10-04 02:00:59 -0400974static inline unsigned rb_page_write(struct buffer_page *bpage)
975{
976 return local_read(&bpage->write);
977}
978
979static inline unsigned rb_page_commit(struct buffer_page *bpage)
980{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500981 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400982}
983
984/* Size is determined by what has been commited */
985static inline unsigned rb_page_size(struct buffer_page *bpage)
986{
987 return rb_page_commit(bpage);
988}
989
990static inline unsigned
991rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
992{
993 return rb_page_commit(cpu_buffer->commit_page);
994}
995
996static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
997{
998 return rb_page_commit(cpu_buffer->head_page);
999}
1000
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001001static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05001002 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001003{
Steven Rostedt044fa782008-12-02 23:50:03 -05001004 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001005
Steven Rostedt044fa782008-12-02 23:50:03 -05001006 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001007}
1008
Steven Rostedtbf41a152008-10-04 02:00:59 -04001009static inline unsigned
1010rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001011{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001012 unsigned long addr = (unsigned long)event;
1013
Steven Rostedt22f470f2009-06-11 09:29:58 -04001014 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001015}
1016
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001017static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001018rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1023
1024 index = rb_event_index(event);
1025 addr &= PAGE_MASK;
1026
1027 return cpu_buffer->commit_page->page == (void *)addr &&
1028 rb_commit_index(cpu_buffer) == index;
1029}
1030
Andrew Morton34a148b2009-01-09 12:27:09 -08001031static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001032rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1033{
1034 /*
1035 * We only race with interrupts and NMIs on this CPU.
1036 * If we own the commit event, then we can commit
1037 * all others that interrupted us, since the interruptions
1038 * are in stack format (they finish before they come
1039 * back to us). This allows us to do a simple loop to
1040 * assign the commit to the tail.
1041 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001042 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001043 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001044 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001045 cpu_buffer->commit_page->write;
1046 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001047 cpu_buffer->write_stamp =
1048 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001049 /* add barrier to keep gcc from optimizing too much */
1050 barrier();
1051 }
1052 while (rb_commit_index(cpu_buffer) !=
1053 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001054 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001055 cpu_buffer->commit_page->write;
1056 barrier();
1057 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001058
1059 /* again, keep gcc from optimizing */
1060 barrier();
1061
1062 /*
1063 * If an interrupt came in just after the first while loop
1064 * and pushed the tail page forward, we will be left with
1065 * a dangling commit that will never go forward.
1066 */
1067 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1068 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001069}
1070
Steven Rostedtd7690412008-10-01 00:29:53 -04001071static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001072{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001073 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001074 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001075}
1076
Andrew Morton34a148b2009-01-09 12:27:09 -08001077static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001078{
1079 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1080
1081 /*
1082 * The iterator could be on the reader page (it starts there).
1083 * But the head could have moved, since the reader was
1084 * found. Check for this case and assign the iterator
1085 * to the head page instead of next.
1086 */
1087 if (iter->head_page == cpu_buffer->reader_page)
1088 iter->head_page = cpu_buffer->head_page;
1089 else
1090 rb_inc_page(cpu_buffer, &iter->head_page);
1091
Steven Rostedtabc9b562008-12-02 15:34:06 -05001092 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001093 iter->head = 0;
1094}
1095
1096/**
1097 * ring_buffer_update_event - update event type and data
1098 * @event: the even to update
1099 * @type: the type of event
1100 * @length: the size of the event field in the ring buffer
1101 *
1102 * Update the type and data fields of the event. The length
1103 * is the actual size that is written to the ring buffer,
1104 * and with this, we can determine what to place into the
1105 * data field.
1106 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001107static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108rb_update_event(struct ring_buffer_event *event,
1109 unsigned type, unsigned length)
1110{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001111 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001112
1113 switch (type) {
1114
1115 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001116 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 break;
1119
Lai Jiangshan334d4162009-04-24 11:27:05 +08001120 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121 length -= RB_EVNT_HDR_SIZE;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001122 if (length > RB_MAX_SMALL_DATA)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001123 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001124 else
1125 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001126 break;
1127 default:
1128 BUG();
1129 }
1130}
1131
Andrew Morton34a148b2009-01-09 12:27:09 -08001132static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133{
1134 struct ring_buffer_event event; /* Used only for sizeof array */
1135
1136 /* zero length can cause confusions */
1137 if (!length)
1138 length = 1;
1139
1140 if (length > RB_MAX_SMALL_DATA)
1141 length += sizeof(event.array[0]);
1142
1143 length += RB_EVNT_HDR_SIZE;
1144 length = ALIGN(length, RB_ALIGNMENT);
1145
1146 return length;
1147}
1148
Steven Rostedtc7b09302009-06-11 11:12:00 -04001149static inline void
1150rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1151 struct buffer_page *tail_page,
1152 unsigned long tail, unsigned long length)
1153{
1154 struct ring_buffer_event *event;
1155
1156 /*
1157 * Only the event that crossed the page boundary
1158 * must fill the old tail_page with padding.
1159 */
1160 if (tail >= BUF_PAGE_SIZE) {
1161 local_sub(length, &tail_page->write);
1162 return;
1163 }
1164
1165 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001166 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001167
1168 /*
1169 * If this event is bigger than the minimum size, then
1170 * we need to be careful that we don't subtract the
1171 * write counter enough to allow another writer to slip
1172 * in on this page.
1173 * We put in a discarded commit instead, to make sure
1174 * that this space is not used again.
1175 *
1176 * If we are less than the minimum size, we don't need to
1177 * worry about it.
1178 */
1179 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1180 /* No room for any events */
1181
1182 /* Mark the rest of the page with padding */
1183 rb_event_set_padding(event);
1184
1185 /* Set the write back to the previous setting */
1186 local_sub(length, &tail_page->write);
1187 return;
1188 }
1189
1190 /* Put in a discarded event */
1191 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1192 event->type_len = RINGBUF_TYPE_PADDING;
1193 /* time delta must be non zero */
1194 event->time_delta = 1;
1195 /* Account for this as an entry */
1196 local_inc(&tail_page->entries);
1197 local_inc(&cpu_buffer->entries);
1198
1199 /* Set write to end of buffer */
1200 length = (tail + length) - BUF_PAGE_SIZE;
1201 local_sub(length, &tail_page->write);
1202}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001203
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001204static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001205rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1206 unsigned long length, unsigned long tail,
1207 struct buffer_page *commit_page,
1208 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001209{
Steven Rostedt6634ff22009-05-06 15:30:07 -04001210 struct buffer_page *next_page, *head_page, *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001211 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001212 bool lock_taken = false;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001213 unsigned long flags;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001214
1215 next_page = tail_page;
1216
1217 local_irq_save(flags);
1218 /*
1219 * Since the write to the buffer is still not
1220 * fully lockless, we must be careful with NMIs.
1221 * The locks in the writers are taken when a write
1222 * crosses to a new page. The locks protect against
1223 * races with the readers (this will soon be fixed
1224 * with a lockless solution).
1225 *
1226 * Because we can not protect against NMIs, and we
1227 * want to keep traces reentrant, we need to manage
1228 * what happens when we are in an NMI.
1229 *
1230 * NMIs can happen after we take the lock.
1231 * If we are in an NMI, only take the lock
1232 * if it is not already taken. Otherwise
1233 * simply fail.
1234 */
1235 if (unlikely(in_nmi())) {
1236 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1237 cpu_buffer->nmi_dropped++;
1238 goto out_reset;
1239 }
1240 } else
1241 __raw_spin_lock(&cpu_buffer->lock);
1242
1243 lock_taken = true;
1244
1245 rb_inc_page(cpu_buffer, &next_page);
1246
1247 head_page = cpu_buffer->head_page;
1248 reader_page = cpu_buffer->reader_page;
1249
1250 /* we grabbed the lock before incrementing */
1251 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1252 goto out_reset;
1253
1254 /*
1255 * If for some reason, we had an interrupt storm that made
1256 * it all the way around the buffer, bail, and warn
1257 * about it.
1258 */
1259 if (unlikely(next_page == commit_page)) {
1260 cpu_buffer->commit_overrun++;
1261 goto out_reset;
1262 }
1263
1264 if (next_page == head_page) {
1265 if (!(buffer->flags & RB_FL_OVERWRITE))
1266 goto out_reset;
1267
1268 /* tail_page has not moved yet? */
1269 if (tail_page == cpu_buffer->tail_page) {
1270 /* count overflows */
1271 cpu_buffer->overrun +=
1272 local_read(&head_page->entries);
1273
1274 rb_inc_page(cpu_buffer, &head_page);
1275 cpu_buffer->head_page = head_page;
1276 cpu_buffer->head_page->read = 0;
1277 }
1278 }
1279
1280 /*
1281 * If the tail page is still the same as what we think
1282 * it is, then it is up to us to update the tail
1283 * pointer.
1284 */
1285 if (tail_page == cpu_buffer->tail_page) {
1286 local_set(&next_page->write, 0);
1287 local_set(&next_page->entries, 0);
1288 local_set(&next_page->page->commit, 0);
1289 cpu_buffer->tail_page = next_page;
1290
1291 /* reread the time stamp */
Steven Rostedt88eb0122009-05-11 16:28:23 -04001292 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001293 cpu_buffer->tail_page->page->time_stamp = *ts;
1294 }
1295
Steven Rostedtc7b09302009-06-11 11:12:00 -04001296 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001297
1298 __raw_spin_unlock(&cpu_buffer->lock);
1299 local_irq_restore(flags);
1300
1301 /* fail and let the caller try again */
1302 return ERR_PTR(-EAGAIN);
1303
Steven Rostedt45141d42009-02-12 13:19:48 -05001304 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001305 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001306 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001307
Steven Rostedt78d904b2009-02-05 18:43:07 -05001308 if (likely(lock_taken))
1309 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001310 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001311 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001312}
1313
Steven Rostedt6634ff22009-05-06 15:30:07 -04001314static struct ring_buffer_event *
1315__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1316 unsigned type, unsigned long length, u64 *ts)
1317{
1318 struct buffer_page *tail_page, *commit_page;
1319 struct ring_buffer_event *event;
1320 unsigned long tail, write;
1321
1322 commit_page = cpu_buffer->commit_page;
1323 /* we just need to protect against interrupts */
1324 barrier();
1325 tail_page = cpu_buffer->tail_page;
1326 write = local_add_return(length, &tail_page->write);
1327 tail = write - length;
1328
1329 /* See if we shot pass the end of this buffer page */
1330 if (write > BUF_PAGE_SIZE)
1331 return rb_move_tail(cpu_buffer, length, tail,
1332 commit_page, tail_page, ts);
1333
1334 /* We reserved something on the buffer */
1335
Steven Rostedt6634ff22009-05-06 15:30:07 -04001336 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001337 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001338 rb_update_event(event, type, length);
1339
1340 /* The passed in type is zero for DATA */
1341 if (likely(!type))
1342 local_inc(&tail_page->entries);
1343
1344 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001345 * If this is the first commit on the page, then update
1346 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001347 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001348 if (!tail)
1349 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001350
1351 return event;
1352}
1353
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001354static inline int
1355rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1356 struct ring_buffer_event *event)
1357{
1358 unsigned long new_index, old_index;
1359 struct buffer_page *bpage;
1360 unsigned long index;
1361 unsigned long addr;
1362
1363 new_index = rb_event_index(event);
1364 old_index = new_index + rb_event_length(event);
1365 addr = (unsigned long)event;
1366 addr &= PAGE_MASK;
1367
1368 bpage = cpu_buffer->tail_page;
1369
1370 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1371 /*
1372 * This is on the tail page. It is possible that
1373 * a write could come in and move the tail page
1374 * and write to the next page. That is fine
1375 * because we just shorten what is on this page.
1376 */
1377 index = local_cmpxchg(&bpage->write, old_index, new_index);
1378 if (index == old_index)
1379 return 1;
1380 }
1381
1382 /* could not discard */
1383 return 0;
1384}
1385
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001386static int
1387rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1388 u64 *ts, u64 *delta)
1389{
1390 struct ring_buffer_event *event;
1391 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001392 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393
1394 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1395 printk(KERN_WARNING "Delta way too big! %llu"
1396 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001397 (unsigned long long)*delta,
1398 (unsigned long long)*ts,
1399 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001400 WARN_ON(1);
1401 }
1402
1403 /*
1404 * The delta is too big, we to add a
1405 * new timestamp.
1406 */
1407 event = __rb_reserve_next(cpu_buffer,
1408 RINGBUF_TYPE_TIME_EXTEND,
1409 RB_LEN_TIME_EXTEND,
1410 ts);
1411 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001412 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001413
Steven Rostedtbf41a152008-10-04 02:00:59 -04001414 if (PTR_ERR(event) == -EAGAIN)
1415 return -EAGAIN;
1416
1417 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04001418 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001419 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001420 * If this is the first on the page, then it was
1421 * updated with the page itself. Try to discard it
1422 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04001423 */
1424 if (rb_event_index(event)) {
1425 event->time_delta = *delta & TS_MASK;
1426 event->array[0] = *delta >> TS_SHIFT;
1427 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04001428 /* try to discard, since we do not need this */
1429 if (!rb_try_to_discard(cpu_buffer, event)) {
1430 /* nope, just zero it */
1431 event->time_delta = 0;
1432 event->array[0] = 0;
1433 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001434 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001435 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001436 /* let the caller know this was the commit */
1437 ret = 1;
1438 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001439 /* Try to discard the event */
1440 if (!rb_try_to_discard(cpu_buffer, event)) {
1441 /* Darn, this is just wasted space */
1442 event->time_delta = 0;
1443 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001444 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04001445 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001446 }
1447
Steven Rostedtbf41a152008-10-04 02:00:59 -04001448 *delta = 0;
1449
1450 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451}
1452
Steven Rostedtfa743952009-06-16 12:37:57 -04001453static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
1454{
1455 local_inc(&cpu_buffer->committing);
1456 local_inc(&cpu_buffer->commits);
1457}
1458
1459static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
1460{
1461 unsigned long commits;
1462
1463 if (RB_WARN_ON(cpu_buffer,
1464 !local_read(&cpu_buffer->committing)))
1465 return;
1466
1467 again:
1468 commits = local_read(&cpu_buffer->commits);
1469 /* synchronize with interrupts */
1470 barrier();
1471 if (local_read(&cpu_buffer->committing) == 1)
1472 rb_set_commit_to_write(cpu_buffer);
1473
1474 local_dec(&cpu_buffer->committing);
1475
1476 /* synchronize with interrupts */
1477 barrier();
1478
1479 /*
1480 * Need to account for interrupts coming in between the
1481 * updating of the commit page and the clearing of the
1482 * committing counter.
1483 */
1484 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
1485 !local_read(&cpu_buffer->committing)) {
1486 local_inc(&cpu_buffer->committing);
1487 goto again;
1488 }
1489}
1490
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001491static struct ring_buffer_event *
1492rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001493 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001494{
1495 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04001496 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001497 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001498 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001499
Steven Rostedtfa743952009-06-16 12:37:57 -04001500 rb_start_commit(cpu_buffer);
1501
Steven Rostedtbe957c42009-05-11 14:42:53 -04001502 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001503 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001504 /*
1505 * We allow for interrupts to reenter here and do a trace.
1506 * If one does, it will cause this original code to loop
1507 * back here. Even with heavy interrupts happening, this
1508 * should only happen a few times in a row. If this happens
1509 * 1000 times in a row, there must be either an interrupt
1510 * storm or we have something buggy.
1511 * Bail!
1512 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001513 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04001514 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001515
Steven Rostedt88eb0122009-05-11 16:28:23 -04001516 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001517
Steven Rostedtbf41a152008-10-04 02:00:59 -04001518 /*
1519 * Only the first commit can update the timestamp.
1520 * Yes there is a race here. If an interrupt comes in
1521 * just after the conditional and it traces too, then it
1522 * will also check the deltas. More than one timestamp may
1523 * also be made. But only the entry that did the actual
1524 * commit will be something other than zero.
1525 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001526 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1527 rb_page_write(cpu_buffer->tail_page) ==
1528 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04001529 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001530
Steven Rostedt168b6b12009-05-11 22:11:05 -04001531 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001532
Steven Rostedt168b6b12009-05-11 22:11:05 -04001533 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001534 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001535
Steven Rostedtbf41a152008-10-04 02:00:59 -04001536 /* Did the write stamp get updated already? */
1537 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04001538 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001539
Steven Rostedt168b6b12009-05-11 22:11:05 -04001540 delta = diff;
1541 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001542
1543 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001544 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04001545 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001546
1547 if (commit == -EAGAIN)
1548 goto again;
1549
1550 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001551 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04001552 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001553
Steven Rostedt168b6b12009-05-11 22:11:05 -04001554 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001555 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04001556 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04001557 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001558
Steven Rostedtfa743952009-06-16 12:37:57 -04001559 if (!event)
1560 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001561
Steven Rostedtfa743952009-06-16 12:37:57 -04001562 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001563 delta = 0;
1564
1565 event->time_delta = delta;
1566
1567 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04001568
1569 out_fail:
1570 rb_end_commit(cpu_buffer);
1571 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001572}
1573
Paul Mundt1155de42009-06-25 14:30:12 +09001574#ifdef CONFIG_TRACING
1575
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001576#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001577
1578static int trace_recursive_lock(void)
1579{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001580 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001581
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001582 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1583 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001584
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001585 /* Disable all tracing before we do anything else */
1586 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001587
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04001588 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001589 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1590 current->trace_recursion,
1591 hardirq_count() >> HARDIRQ_SHIFT,
1592 softirq_count() >> SOFTIRQ_SHIFT,
1593 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001594
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001595 WARN_ON_ONCE(1);
1596 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001597}
1598
1599static void trace_recursive_unlock(void)
1600{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001601 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001602
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001603 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001604}
1605
Paul Mundt1155de42009-06-25 14:30:12 +09001606#else
1607
1608#define trace_recursive_lock() (0)
1609#define trace_recursive_unlock() do { } while (0)
1610
1611#endif
1612
Steven Rostedtbf41a152008-10-04 02:00:59 -04001613static DEFINE_PER_CPU(int, rb_need_resched);
1614
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001615/**
1616 * ring_buffer_lock_reserve - reserve a part of the buffer
1617 * @buffer: the ring buffer to reserve from
1618 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001619 *
1620 * Returns a reseverd event on the ring buffer to copy directly to.
1621 * The user of this interface will need to get the body to write into
1622 * and can use the ring_buffer_event_data() interface.
1623 *
1624 * The length is the length of the data needed, not the event length
1625 * which also includes the event header.
1626 *
1627 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1628 * If NULL is returned, then nothing has been allocated or locked.
1629 */
1630struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001631ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001632{
1633 struct ring_buffer_per_cpu *cpu_buffer;
1634 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001635 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001636
Steven Rostedt033601a2008-11-21 12:41:55 -05001637 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001638 return NULL;
1639
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001640 if (atomic_read(&buffer->record_disabled))
1641 return NULL;
1642
Steven Rostedtbf41a152008-10-04 02:00:59 -04001643 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001644 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001645
Steven Rostedt261842b2009-04-16 21:41:52 -04001646 if (trace_recursive_lock())
1647 goto out_nocheck;
1648
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001649 cpu = raw_smp_processor_id();
1650
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301651 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001652 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001653
1654 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001655
1656 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001657 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001658
Steven Rostedtbe957c42009-05-11 14:42:53 -04001659 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001660 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001661
Steven Rostedt1cd8d732009-05-11 14:08:09 -04001662 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001663 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001664 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001665
Steven Rostedtbf41a152008-10-04 02:00:59 -04001666 /*
1667 * Need to store resched state on this cpu.
1668 * Only the first needs to.
1669 */
1670
1671 if (preempt_count() == 1)
1672 per_cpu(rb_need_resched, cpu) = resched;
1673
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001674 return event;
1675
Steven Rostedtd7690412008-10-01 00:29:53 -04001676 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001677 trace_recursive_unlock();
1678
1679 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001680 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001681 return NULL;
1682}
Robert Richterc4f50182008-12-11 16:49:22 +01001683EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001684
1685static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1686 struct ring_buffer_event *event)
1687{
Steven Rostedte4906ef2009-04-30 20:49:44 -04001688 local_inc(&cpu_buffer->entries);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001689
Steven Rostedtfa743952009-06-16 12:37:57 -04001690 /*
1691 * The event first in the commit queue updates the
1692 * time stamp.
1693 */
1694 if (rb_event_is_commit(cpu_buffer, event))
1695 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001696
Steven Rostedtfa743952009-06-16 12:37:57 -04001697 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001698}
1699
1700/**
1701 * ring_buffer_unlock_commit - commit a reserved
1702 * @buffer: The buffer to commit to
1703 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001704 *
1705 * This commits the data to the ring buffer, and releases any locks held.
1706 *
1707 * Must be paired with ring_buffer_lock_reserve.
1708 */
1709int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001710 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001711{
1712 struct ring_buffer_per_cpu *cpu_buffer;
1713 int cpu = raw_smp_processor_id();
1714
1715 cpu_buffer = buffer->buffers[cpu];
1716
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001717 rb_commit(cpu_buffer, event);
1718
Steven Rostedt261842b2009-04-16 21:41:52 -04001719 trace_recursive_unlock();
1720
Steven Rostedtbf41a152008-10-04 02:00:59 -04001721 /*
1722 * Only the last preempt count needs to restore preemption.
1723 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001724 if (preempt_count() == 1)
1725 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1726 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001727 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001728
1729 return 0;
1730}
Robert Richterc4f50182008-12-11 16:49:22 +01001731EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001732
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001733static inline void rb_event_discard(struct ring_buffer_event *event)
1734{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001735 /* array[0] holds the actual length for the discarded event */
1736 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1737 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001738 /* time delta must be non zero */
1739 if (!event->time_delta)
1740 event->time_delta = 1;
1741}
1742
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001743/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001744 * ring_buffer_event_discard - discard any event in the ring buffer
1745 * @event: the event to discard
1746 *
1747 * Sometimes a event that is in the ring buffer needs to be ignored.
1748 * This function lets the user discard an event in the ring buffer
1749 * and then that event will not be read later.
1750 *
1751 * Note, it is up to the user to be careful with this, and protect
1752 * against races. If the user discards an event that has been consumed
1753 * it is possible that it could corrupt the ring buffer.
1754 */
1755void ring_buffer_event_discard(struct ring_buffer_event *event)
1756{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001757 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001758}
1759EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1760
1761/**
1762 * ring_buffer_commit_discard - discard an event that has not been committed
1763 * @buffer: the ring buffer
1764 * @event: non committed event to discard
1765 *
1766 * This is similar to ring_buffer_event_discard but must only be
1767 * performed on an event that has not been committed yet. The difference
1768 * is that this will also try to free the event from the ring buffer
1769 * if another event has not been added behind it.
1770 *
1771 * If another event has been added behind it, it will set the event
1772 * up as discarded, and perform the commit.
1773 *
1774 * If this function is called, do not call ring_buffer_unlock_commit on
1775 * the event.
1776 */
1777void ring_buffer_discard_commit(struct ring_buffer *buffer,
1778 struct ring_buffer_event *event)
1779{
1780 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001781 int cpu;
1782
1783 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001784 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001785
Steven Rostedtfa743952009-06-16 12:37:57 -04001786 cpu = smp_processor_id();
1787 cpu_buffer = buffer->buffers[cpu];
1788
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001789 /*
1790 * This must only be called if the event has not been
1791 * committed yet. Thus we can assume that preemption
1792 * is still disabled.
1793 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001794 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001795
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001796 if (!rb_try_to_discard(cpu_buffer, event))
1797 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001798
1799 /*
1800 * The commit is still visible by the reader, so we
1801 * must increment entries.
1802 */
Steven Rostedte4906ef2009-04-30 20:49:44 -04001803 local_inc(&cpu_buffer->entries);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001804 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04001805 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001806
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001807 trace_recursive_unlock();
1808
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001809 /*
1810 * Only the last preempt count needs to restore preemption.
1811 */
1812 if (preempt_count() == 1)
1813 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1814 else
1815 preempt_enable_no_resched_notrace();
1816
1817}
1818EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1819
1820/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001821 * ring_buffer_write - write data to the buffer without reserving
1822 * @buffer: The ring buffer to write to.
1823 * @length: The length of the data being written (excluding the event header)
1824 * @data: The data to write to the buffer.
1825 *
1826 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1827 * one function. If you already have the data to write to the buffer, it
1828 * may be easier to simply call this function.
1829 *
1830 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1831 * and not the length of the event which would hold the header.
1832 */
1833int ring_buffer_write(struct ring_buffer *buffer,
1834 unsigned long length,
1835 void *data)
1836{
1837 struct ring_buffer_per_cpu *cpu_buffer;
1838 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001839 void *body;
1840 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001841 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001842
Steven Rostedt033601a2008-11-21 12:41:55 -05001843 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001844 return -EBUSY;
1845
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001846 if (atomic_read(&buffer->record_disabled))
1847 return -EBUSY;
1848
Steven Rostedt182e9f52008-11-03 23:15:56 -05001849 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001850
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001851 cpu = raw_smp_processor_id();
1852
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301853 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001854 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001855
1856 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001857
1858 if (atomic_read(&cpu_buffer->record_disabled))
1859 goto out;
1860
Steven Rostedtbe957c42009-05-11 14:42:53 -04001861 if (length > BUF_MAX_DATA_SIZE)
1862 goto out;
1863
1864 event = rb_reserve_next_event(cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001865 if (!event)
1866 goto out;
1867
1868 body = rb_event_data(event);
1869
1870 memcpy(body, data, length);
1871
1872 rb_commit(cpu_buffer, event);
1873
1874 ret = 0;
1875 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001876 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001877
1878 return ret;
1879}
Robert Richterc4f50182008-12-11 16:49:22 +01001880EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001881
Andrew Morton34a148b2009-01-09 12:27:09 -08001882static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001883{
1884 struct buffer_page *reader = cpu_buffer->reader_page;
1885 struct buffer_page *head = cpu_buffer->head_page;
1886 struct buffer_page *commit = cpu_buffer->commit_page;
1887
1888 return reader->read == rb_page_commit(reader) &&
1889 (commit == reader ||
1890 (commit == head &&
1891 head->read == rb_page_commit(commit)));
1892}
1893
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001894/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001895 * ring_buffer_record_disable - stop all writes into the buffer
1896 * @buffer: The ring buffer to stop writes to.
1897 *
1898 * This prevents all writes to the buffer. Any attempt to write
1899 * to the buffer after this will fail and return NULL.
1900 *
1901 * The caller should call synchronize_sched() after this.
1902 */
1903void ring_buffer_record_disable(struct ring_buffer *buffer)
1904{
1905 atomic_inc(&buffer->record_disabled);
1906}
Robert Richterc4f50182008-12-11 16:49:22 +01001907EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001908
1909/**
1910 * ring_buffer_record_enable - enable writes to the buffer
1911 * @buffer: The ring buffer to enable writes
1912 *
1913 * Note, multiple disables will need the same number of enables
1914 * to truely enable the writing (much like preempt_disable).
1915 */
1916void ring_buffer_record_enable(struct ring_buffer *buffer)
1917{
1918 atomic_dec(&buffer->record_disabled);
1919}
Robert Richterc4f50182008-12-11 16:49:22 +01001920EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001921
1922/**
1923 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1924 * @buffer: The ring buffer to stop writes to.
1925 * @cpu: The CPU buffer to stop
1926 *
1927 * This prevents all writes to the buffer. Any attempt to write
1928 * to the buffer after this will fail and return NULL.
1929 *
1930 * The caller should call synchronize_sched() after this.
1931 */
1932void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1933{
1934 struct ring_buffer_per_cpu *cpu_buffer;
1935
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301936 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001937 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001938
1939 cpu_buffer = buffer->buffers[cpu];
1940 atomic_inc(&cpu_buffer->record_disabled);
1941}
Robert Richterc4f50182008-12-11 16:49:22 +01001942EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001943
1944/**
1945 * ring_buffer_record_enable_cpu - enable writes to the buffer
1946 * @buffer: The ring buffer to enable writes
1947 * @cpu: The CPU to enable.
1948 *
1949 * Note, multiple disables will need the same number of enables
1950 * to truely enable the writing (much like preempt_disable).
1951 */
1952void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1953{
1954 struct ring_buffer_per_cpu *cpu_buffer;
1955
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301956 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001957 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001958
1959 cpu_buffer = buffer->buffers[cpu];
1960 atomic_dec(&cpu_buffer->record_disabled);
1961}
Robert Richterc4f50182008-12-11 16:49:22 +01001962EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001963
1964/**
1965 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1966 * @buffer: The ring buffer
1967 * @cpu: The per CPU buffer to get the entries from.
1968 */
1969unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1970{
1971 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001972 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001973
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301974 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001975 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001976
1977 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04001978 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1979 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04001980
1981 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001982}
Robert Richterc4f50182008-12-11 16:49:22 +01001983EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001984
1985/**
1986 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1987 * @buffer: The ring buffer
1988 * @cpu: The per CPU buffer to get the number of overruns from
1989 */
1990unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1991{
1992 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001993 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001994
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301995 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001996 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001997
1998 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001999 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04002000
2001 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002002}
Robert Richterc4f50182008-12-11 16:49:22 +01002003EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004
2005/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002006 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
2007 * @buffer: The ring buffer
2008 * @cpu: The per CPU buffer to get the number of overruns from
2009 */
2010unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
2011{
2012 struct ring_buffer_per_cpu *cpu_buffer;
2013 unsigned long ret;
2014
2015 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2016 return 0;
2017
2018 cpu_buffer = buffer->buffers[cpu];
2019 ret = cpu_buffer->nmi_dropped;
2020
2021 return ret;
2022}
2023EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
2024
2025/**
2026 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2027 * @buffer: The ring buffer
2028 * @cpu: The per CPU buffer to get the number of overruns from
2029 */
2030unsigned long
2031ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2032{
2033 struct ring_buffer_per_cpu *cpu_buffer;
2034 unsigned long ret;
2035
2036 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2037 return 0;
2038
2039 cpu_buffer = buffer->buffers[cpu];
2040 ret = cpu_buffer->commit_overrun;
2041
2042 return ret;
2043}
2044EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2045
2046/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002047 * ring_buffer_entries - get the number of entries in a buffer
2048 * @buffer: The ring buffer
2049 *
2050 * Returns the total number of entries in the ring buffer
2051 * (all CPU entries)
2052 */
2053unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2054{
2055 struct ring_buffer_per_cpu *cpu_buffer;
2056 unsigned long entries = 0;
2057 int cpu;
2058
2059 /* if you care about this being correct, lock the buffer */
2060 for_each_buffer_cpu(buffer, cpu) {
2061 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002062 entries += (local_read(&cpu_buffer->entries) -
2063 cpu_buffer->overrun) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002064 }
2065
2066 return entries;
2067}
Robert Richterc4f50182008-12-11 16:49:22 +01002068EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002069
2070/**
2071 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2072 * @buffer: The ring buffer
2073 *
2074 * Returns the total number of overruns in the ring buffer
2075 * (all CPU entries)
2076 */
2077unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2078{
2079 struct ring_buffer_per_cpu *cpu_buffer;
2080 unsigned long overruns = 0;
2081 int cpu;
2082
2083 /* if you care about this being correct, lock the buffer */
2084 for_each_buffer_cpu(buffer, cpu) {
2085 cpu_buffer = buffer->buffers[cpu];
2086 overruns += cpu_buffer->overrun;
2087 }
2088
2089 return overruns;
2090}
Robert Richterc4f50182008-12-11 16:49:22 +01002091EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002092
Steven Rostedt642edba2008-11-12 00:01:26 -05002093static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002094{
2095 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2096
Steven Rostedtd7690412008-10-01 00:29:53 -04002097 /* Iterator usage is expected to have record disabled */
2098 if (list_empty(&cpu_buffer->reader_page->list)) {
2099 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002100 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002101 } else {
2102 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002103 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002104 }
2105 if (iter->head)
2106 iter->read_stamp = cpu_buffer->read_stamp;
2107 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002108 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05002109}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002110
Steven Rostedt642edba2008-11-12 00:01:26 -05002111/**
2112 * ring_buffer_iter_reset - reset an iterator
2113 * @iter: The iterator to reset
2114 *
2115 * Resets the iterator, so that it will start from the beginning
2116 * again.
2117 */
2118void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2119{
Steven Rostedt554f7862009-03-11 22:00:13 -04002120 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002121 unsigned long flags;
2122
Steven Rostedt554f7862009-03-11 22:00:13 -04002123 if (!iter)
2124 return;
2125
2126 cpu_buffer = iter->cpu_buffer;
2127
Steven Rostedt642edba2008-11-12 00:01:26 -05002128 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2129 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002130 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002131}
Robert Richterc4f50182008-12-11 16:49:22 +01002132EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002133
2134/**
2135 * ring_buffer_iter_empty - check if an iterator has no more to read
2136 * @iter: The iterator to check
2137 */
2138int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2139{
2140 struct ring_buffer_per_cpu *cpu_buffer;
2141
2142 cpu_buffer = iter->cpu_buffer;
2143
Steven Rostedtbf41a152008-10-04 02:00:59 -04002144 return iter->head_page == cpu_buffer->commit_page &&
2145 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002146}
Robert Richterc4f50182008-12-11 16:49:22 +01002147EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002148
2149static void
2150rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2151 struct ring_buffer_event *event)
2152{
2153 u64 delta;
2154
Lai Jiangshan334d4162009-04-24 11:27:05 +08002155 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002156 case RINGBUF_TYPE_PADDING:
2157 return;
2158
2159 case RINGBUF_TYPE_TIME_EXTEND:
2160 delta = event->array[0];
2161 delta <<= TS_SHIFT;
2162 delta += event->time_delta;
2163 cpu_buffer->read_stamp += delta;
2164 return;
2165
2166 case RINGBUF_TYPE_TIME_STAMP:
2167 /* FIXME: not implemented */
2168 return;
2169
2170 case RINGBUF_TYPE_DATA:
2171 cpu_buffer->read_stamp += event->time_delta;
2172 return;
2173
2174 default:
2175 BUG();
2176 }
2177 return;
2178}
2179
2180static void
2181rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2182 struct ring_buffer_event *event)
2183{
2184 u64 delta;
2185
Lai Jiangshan334d4162009-04-24 11:27:05 +08002186 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187 case RINGBUF_TYPE_PADDING:
2188 return;
2189
2190 case RINGBUF_TYPE_TIME_EXTEND:
2191 delta = event->array[0];
2192 delta <<= TS_SHIFT;
2193 delta += event->time_delta;
2194 iter->read_stamp += delta;
2195 return;
2196
2197 case RINGBUF_TYPE_TIME_STAMP:
2198 /* FIXME: not implemented */
2199 return;
2200
2201 case RINGBUF_TYPE_DATA:
2202 iter->read_stamp += event->time_delta;
2203 return;
2204
2205 default:
2206 BUG();
2207 }
2208 return;
2209}
2210
Steven Rostedtd7690412008-10-01 00:29:53 -04002211static struct buffer_page *
2212rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002213{
Steven Rostedtd7690412008-10-01 00:29:53 -04002214 struct buffer_page *reader = NULL;
2215 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002216 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002217
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002218 local_irq_save(flags);
2219 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002220
2221 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002222 /*
2223 * This should normally only loop twice. But because the
2224 * start of the reader inserts an empty page, it causes
2225 * a case where we will loop three times. There should be no
2226 * reason to loop four times (that I know of).
2227 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002228 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002229 reader = NULL;
2230 goto out;
2231 }
2232
Steven Rostedtd7690412008-10-01 00:29:53 -04002233 reader = cpu_buffer->reader_page;
2234
2235 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002236 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002237 goto out;
2238
2239 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002240 if (RB_WARN_ON(cpu_buffer,
2241 cpu_buffer->reader_page->read > rb_page_size(reader)))
2242 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002243
2244 /* check if we caught up to the tail */
2245 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002246 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002247 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002248
2249 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002250 * Splice the empty reader page into the list around the head.
2251 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002252 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002253
Steven Rostedtd7690412008-10-01 00:29:53 -04002254 reader = cpu_buffer->head_page;
2255 cpu_buffer->reader_page->list.next = reader->list.next;
2256 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002257
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002258 /*
2259 * cpu_buffer->pages just needs to point to the buffer, it
2260 * has no specific buffer page to point to. Lets move it out
2261 * of our way so we don't accidently swap it.
2262 */
2263 cpu_buffer->pages = reader->list.prev;
2264
Steven Rostedtbf41a152008-10-04 02:00:59 -04002265 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002266 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002267 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002268
2269 /* Make the reader page now replace the head */
2270 reader->list.prev->next = &cpu_buffer->reader_page->list;
2271 reader->list.next->prev = &cpu_buffer->reader_page->list;
2272
2273 /*
2274 * If the tail is on the reader, then we must set the head
2275 * to the inserted page, otherwise we set it one before.
2276 */
2277 cpu_buffer->head_page = cpu_buffer->reader_page;
2278
Steven Rostedtbf41a152008-10-04 02:00:59 -04002279 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002280 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2281
2282 /* Finally update the reader page to the new head */
2283 cpu_buffer->reader_page = reader;
2284 rb_reset_reader_page(cpu_buffer);
2285
2286 goto again;
2287
2288 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002289 __raw_spin_unlock(&cpu_buffer->lock);
2290 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002291
2292 return reader;
2293}
2294
2295static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2296{
2297 struct ring_buffer_event *event;
2298 struct buffer_page *reader;
2299 unsigned length;
2300
2301 reader = rb_get_reader_page(cpu_buffer);
2302
2303 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002304 if (RB_WARN_ON(cpu_buffer, !reader))
2305 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002306
2307 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002308
Lai Jiangshan334d4162009-04-24 11:27:05 +08002309 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2310 || rb_discarded_event(event))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002311 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002313 rb_update_read_stamp(cpu_buffer, event);
2314
Steven Rostedtd7690412008-10-01 00:29:53 -04002315 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002316 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002317}
2318
2319static void rb_advance_iter(struct ring_buffer_iter *iter)
2320{
2321 struct ring_buffer *buffer;
2322 struct ring_buffer_per_cpu *cpu_buffer;
2323 struct ring_buffer_event *event;
2324 unsigned length;
2325
2326 cpu_buffer = iter->cpu_buffer;
2327 buffer = cpu_buffer->buffer;
2328
2329 /*
2330 * Check if we are at the end of the buffer.
2331 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002332 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04002333 /* discarded commits can make the page empty */
2334 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002335 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002336 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002337 return;
2338 }
2339
2340 event = rb_iter_head_event(iter);
2341
2342 length = rb_event_length(event);
2343
2344 /*
2345 * This should not be called to advance the header if we are
2346 * at the tail of the buffer.
2347 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002348 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002349 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002350 (iter->head + length > rb_commit_index(cpu_buffer))))
2351 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002352
2353 rb_update_iter_read_stamp(iter, event);
2354
2355 iter->head += length;
2356
2357 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002358 if ((iter->head >= rb_page_size(iter->head_page)) &&
2359 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002360 rb_advance_iter(iter);
2361}
2362
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002363static struct ring_buffer_event *
2364rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002365{
2366 struct ring_buffer_per_cpu *cpu_buffer;
2367 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002368 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002369 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002370
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002371 cpu_buffer = buffer->buffers[cpu];
2372
2373 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002374 /*
2375 * We repeat when a timestamp is encountered. It is possible
2376 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04002377 * as one timestamp is about to be written, or from discarded
2378 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002379 */
Steven Rostedtea05b572009-06-03 09:30:10 -04002380 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002381 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002382
Steven Rostedtd7690412008-10-01 00:29:53 -04002383 reader = rb_get_reader_page(cpu_buffer);
2384 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002385 return NULL;
2386
Steven Rostedtd7690412008-10-01 00:29:53 -04002387 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002388
Lai Jiangshan334d4162009-04-24 11:27:05 +08002389 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002390 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002391 if (rb_null_event(event))
2392 RB_WARN_ON(cpu_buffer, 1);
2393 /*
2394 * Because the writer could be discarding every
2395 * event it creates (which would probably be bad)
2396 * if we were to go back to "again" then we may never
2397 * catch up, and will trigger the warn on, or lock
2398 * the box. Return the padding, and we will release
2399 * the current locks, and try again.
2400 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002401 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002402 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002403
2404 case RINGBUF_TYPE_TIME_EXTEND:
2405 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002406 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002407 goto again;
2408
2409 case RINGBUF_TYPE_TIME_STAMP:
2410 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002411 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002412 goto again;
2413
2414 case RINGBUF_TYPE_DATA:
2415 if (ts) {
2416 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002417 ring_buffer_normalize_time_stamp(buffer,
2418 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002419 }
2420 return event;
2421
2422 default:
2423 BUG();
2424 }
2425
2426 return NULL;
2427}
Robert Richterc4f50182008-12-11 16:49:22 +01002428EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002429
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002430static struct ring_buffer_event *
2431rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432{
2433 struct ring_buffer *buffer;
2434 struct ring_buffer_per_cpu *cpu_buffer;
2435 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002436 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002437
2438 if (ring_buffer_iter_empty(iter))
2439 return NULL;
2440
2441 cpu_buffer = iter->cpu_buffer;
2442 buffer = cpu_buffer->buffer;
2443
2444 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002445 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04002446 * We repeat when a timestamp is encountered.
2447 * We can get multiple timestamps by nested interrupts or also
2448 * if filtering is on (discarding commits). Since discarding
2449 * commits can be frequent we can get a lot of timestamps.
2450 * But we limit them by not adding timestamps if they begin
2451 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002452 */
Steven Rostedtea05b572009-06-03 09:30:10 -04002453 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002454 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002455
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002456 if (rb_per_cpu_empty(cpu_buffer))
2457 return NULL;
2458
2459 event = rb_iter_head_event(iter);
2460
Lai Jiangshan334d4162009-04-24 11:27:05 +08002461 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002462 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002463 if (rb_null_event(event)) {
2464 rb_inc_iter(iter);
2465 goto again;
2466 }
2467 rb_advance_iter(iter);
2468 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002469
2470 case RINGBUF_TYPE_TIME_EXTEND:
2471 /* Internal data, OK to advance */
2472 rb_advance_iter(iter);
2473 goto again;
2474
2475 case RINGBUF_TYPE_TIME_STAMP:
2476 /* FIXME: not implemented */
2477 rb_advance_iter(iter);
2478 goto again;
2479
2480 case RINGBUF_TYPE_DATA:
2481 if (ts) {
2482 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002483 ring_buffer_normalize_time_stamp(buffer,
2484 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002485 }
2486 return event;
2487
2488 default:
2489 BUG();
2490 }
2491
2492 return NULL;
2493}
Robert Richterc4f50182008-12-11 16:49:22 +01002494EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495
Steven Rostedt8d707e82009-06-16 21:22:48 -04002496static inline int rb_ok_to_lock(void)
2497{
2498 /*
2499 * If an NMI die dumps out the content of the ring buffer
2500 * do not grab locks. We also permanently disable the ring
2501 * buffer too. A one time deal is all you get from reading
2502 * the ring buffer from an NMI.
2503 */
2504 if (likely(!in_nmi() && !oops_in_progress))
2505 return 1;
2506
2507 tracing_off_permanent();
2508 return 0;
2509}
2510
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002512 * ring_buffer_peek - peek at the next event to be read
2513 * @buffer: The ring buffer to read
2514 * @cpu: The cpu to peak at
2515 * @ts: The timestamp counter of this event.
2516 *
2517 * This will return the event that will be read next, but does
2518 * not consume the data.
2519 */
2520struct ring_buffer_event *
2521ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2522{
2523 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002524 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002525 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04002526 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002527
Steven Rostedt554f7862009-03-11 22:00:13 -04002528 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002529 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002530
Steven Rostedt8d707e82009-06-16 21:22:48 -04002531 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05002532 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04002533 local_irq_save(flags);
2534 if (dolock)
2535 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002536 event = rb_buffer_peek(buffer, cpu, ts);
Steven Rostedt8d707e82009-06-16 21:22:48 -04002537 if (dolock)
2538 spin_unlock(&cpu_buffer->reader_lock);
2539 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002540
Lai Jiangshan334d4162009-04-24 11:27:05 +08002541 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002542 cpu_relax();
2543 goto again;
2544 }
2545
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002546 return event;
2547}
2548
2549/**
2550 * ring_buffer_iter_peek - peek at the next event to be read
2551 * @iter: The ring buffer iterator
2552 * @ts: The timestamp counter of this event.
2553 *
2554 * This will return the event that will be read next, but does
2555 * not increment the iterator.
2556 */
2557struct ring_buffer_event *
2558ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2559{
2560 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2561 struct ring_buffer_event *event;
2562 unsigned long flags;
2563
Tom Zanussi2d622712009-03-22 03:30:49 -05002564 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002565 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2566 event = rb_iter_peek(iter, ts);
2567 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2568
Lai Jiangshan334d4162009-04-24 11:27:05 +08002569 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002570 cpu_relax();
2571 goto again;
2572 }
2573
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002574 return event;
2575}
2576
2577/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002578 * ring_buffer_consume - return an event and consume it
2579 * @buffer: The ring buffer to get the next event from
2580 *
2581 * Returns the next event in the ring buffer, and that event is consumed.
2582 * Meaning, that sequential reads will keep returning a different event,
2583 * and eventually empty the ring buffer if the producer is slower.
2584 */
2585struct ring_buffer_event *
2586ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2587{
Steven Rostedt554f7862009-03-11 22:00:13 -04002588 struct ring_buffer_per_cpu *cpu_buffer;
2589 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002590 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04002591 int dolock;
2592
2593 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002594
Tom Zanussi2d622712009-03-22 03:30:49 -05002595 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002596 /* might be called in atomic */
2597 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598
Steven Rostedt554f7862009-03-11 22:00:13 -04002599 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2600 goto out;
2601
2602 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04002603 local_irq_save(flags);
2604 if (dolock)
2605 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002606
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002607 event = rb_buffer_peek(buffer, cpu, ts);
2608 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002609 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002610
Steven Rostedtd7690412008-10-01 00:29:53 -04002611 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002612
Steven Rostedt554f7862009-03-11 22:00:13 -04002613 out_unlock:
Steven Rostedt8d707e82009-06-16 21:22:48 -04002614 if (dolock)
2615 spin_unlock(&cpu_buffer->reader_lock);
2616 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002617
Steven Rostedt554f7862009-03-11 22:00:13 -04002618 out:
2619 preempt_enable();
2620
Lai Jiangshan334d4162009-04-24 11:27:05 +08002621 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002622 cpu_relax();
2623 goto again;
2624 }
2625
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002626 return event;
2627}
Robert Richterc4f50182008-12-11 16:49:22 +01002628EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002629
2630/**
2631 * ring_buffer_read_start - start a non consuming read of the buffer
2632 * @buffer: The ring buffer to read from
2633 * @cpu: The cpu buffer to iterate over
2634 *
2635 * This starts up an iteration through the buffer. It also disables
2636 * the recording to the buffer until the reading is finished.
2637 * This prevents the reading from being corrupted. This is not
2638 * a consuming read, so a producer is not expected.
2639 *
2640 * Must be paired with ring_buffer_finish.
2641 */
2642struct ring_buffer_iter *
2643ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2644{
2645 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002646 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002647 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002648
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302649 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002650 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002651
2652 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2653 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002654 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002655
2656 cpu_buffer = buffer->buffers[cpu];
2657
2658 iter->cpu_buffer = cpu_buffer;
2659
2660 atomic_inc(&cpu_buffer->record_disabled);
2661 synchronize_sched();
2662
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002663 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002664 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002665 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002666 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002667 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002668
2669 return iter;
2670}
Robert Richterc4f50182008-12-11 16:49:22 +01002671EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002672
2673/**
2674 * ring_buffer_finish - finish reading the iterator of the buffer
2675 * @iter: The iterator retrieved by ring_buffer_start
2676 *
2677 * This re-enables the recording to the buffer, and frees the
2678 * iterator.
2679 */
2680void
2681ring_buffer_read_finish(struct ring_buffer_iter *iter)
2682{
2683 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2684
2685 atomic_dec(&cpu_buffer->record_disabled);
2686 kfree(iter);
2687}
Robert Richterc4f50182008-12-11 16:49:22 +01002688EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002689
2690/**
2691 * ring_buffer_read - read the next item in the ring buffer by the iterator
2692 * @iter: The ring buffer iterator
2693 * @ts: The time stamp of the event read.
2694 *
2695 * This reads the next event in the ring buffer and increments the iterator.
2696 */
2697struct ring_buffer_event *
2698ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2699{
2700 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002701 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2702 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002703
Tom Zanussi2d622712009-03-22 03:30:49 -05002704 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002705 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2706 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002707 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002708 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002709
2710 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002711 out:
2712 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002713
Lai Jiangshan334d4162009-04-24 11:27:05 +08002714 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
Tom Zanussi2d622712009-03-22 03:30:49 -05002715 cpu_relax();
2716 goto again;
2717 }
2718
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002719 return event;
2720}
Robert Richterc4f50182008-12-11 16:49:22 +01002721EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002722
2723/**
2724 * ring_buffer_size - return the size of the ring buffer (in bytes)
2725 * @buffer: The ring buffer.
2726 */
2727unsigned long ring_buffer_size(struct ring_buffer *buffer)
2728{
2729 return BUF_PAGE_SIZE * buffer->pages;
2730}
Robert Richterc4f50182008-12-11 16:49:22 +01002731EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002732
2733static void
2734rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2735{
2736 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002737 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002738 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002739 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002740 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002741
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002742 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002743
2744 cpu_buffer->tail_page = cpu_buffer->head_page;
2745 cpu_buffer->commit_page = cpu_buffer->head_page;
2746
2747 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2748 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04002749 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002750 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002751 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002752
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002753 cpu_buffer->nmi_dropped = 0;
2754 cpu_buffer->commit_overrun = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002755 cpu_buffer->overrun = 0;
Steven Rostedte4906ef2009-04-30 20:49:44 -04002756 cpu_buffer->read = 0;
2757 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04002758 local_set(&cpu_buffer->committing, 0);
2759 local_set(&cpu_buffer->commits, 0);
Steven Rostedt69507c02009-01-21 18:45:57 -05002760
2761 cpu_buffer->write_stamp = 0;
2762 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002763}
2764
2765/**
2766 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2767 * @buffer: The ring buffer to reset a per cpu buffer of
2768 * @cpu: The CPU buffer to be reset
2769 */
2770void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2771{
2772 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2773 unsigned long flags;
2774
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302775 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002776 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002777
Steven Rostedt41ede232009-05-01 20:26:54 -04002778 atomic_inc(&cpu_buffer->record_disabled);
2779
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002780 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2781
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002782 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002783
2784 rb_reset_cpu(cpu_buffer);
2785
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002786 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002787
2788 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04002789
2790 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002791}
Robert Richterc4f50182008-12-11 16:49:22 +01002792EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002793
2794/**
2795 * ring_buffer_reset - reset a ring buffer
2796 * @buffer: The ring buffer to reset all cpu buffers
2797 */
2798void ring_buffer_reset(struct ring_buffer *buffer)
2799{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002800 int cpu;
2801
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002802 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002803 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002804}
Robert Richterc4f50182008-12-11 16:49:22 +01002805EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002806
2807/**
2808 * rind_buffer_empty - is the ring buffer empty?
2809 * @buffer: The ring buffer to test
2810 */
2811int ring_buffer_empty(struct ring_buffer *buffer)
2812{
2813 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04002814 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04002815 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002816 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04002817 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002818
Steven Rostedt8d707e82009-06-16 21:22:48 -04002819 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002820
2821 /* yes this is racy, but if you don't like the race, lock the buffer */
2822 for_each_buffer_cpu(buffer, cpu) {
2823 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04002824 local_irq_save(flags);
2825 if (dolock)
2826 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04002827 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04002828 if (dolock)
2829 spin_unlock(&cpu_buffer->reader_lock);
2830 local_irq_restore(flags);
2831
Steven Rostedtd4788202009-06-17 00:39:43 -04002832 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002833 return 0;
2834 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002835
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002836 return 1;
2837}
Robert Richterc4f50182008-12-11 16:49:22 +01002838EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002839
2840/**
2841 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2842 * @buffer: The ring buffer
2843 * @cpu: The CPU buffer to test
2844 */
2845int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2846{
2847 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04002848 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04002849 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002850 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002851
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302852 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002853 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002854
Steven Rostedt8d707e82009-06-16 21:22:48 -04002855 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04002856
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002857 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04002858 local_irq_save(flags);
2859 if (dolock)
2860 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04002861 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04002862 if (dolock)
2863 spin_unlock(&cpu_buffer->reader_lock);
2864 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04002865
2866 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002867}
Robert Richterc4f50182008-12-11 16:49:22 +01002868EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002869
2870/**
2871 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2872 * @buffer_a: One buffer to swap with
2873 * @buffer_b: The other buffer to swap with
2874 *
2875 * This function is useful for tracers that want to take a "snapshot"
2876 * of a CPU buffer and has another back up buffer lying around.
2877 * it is expected that the tracer handles the cpu buffer not being
2878 * used at the moment.
2879 */
2880int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2881 struct ring_buffer *buffer_b, int cpu)
2882{
2883 struct ring_buffer_per_cpu *cpu_buffer_a;
2884 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002885 int ret = -EINVAL;
2886
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302887 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2888 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002889 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002890
2891 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002892 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002893 goto out;
2894
2895 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002896
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002897 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002898 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002899
2900 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002901 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002902
2903 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002904 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002905
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002906 cpu_buffer_a = buffer_a->buffers[cpu];
2907 cpu_buffer_b = buffer_b->buffers[cpu];
2908
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002909 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002910 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002911
2912 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002913 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002914
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002915 /*
2916 * We can't do a synchronize_sched here because this
2917 * function can be called in atomic context.
2918 * Normally this will be called from the same CPU as cpu.
2919 * If not it's up to the caller to protect this.
2920 */
2921 atomic_inc(&cpu_buffer_a->record_disabled);
2922 atomic_inc(&cpu_buffer_b->record_disabled);
2923
2924 buffer_a->buffers[cpu] = cpu_buffer_b;
2925 buffer_b->buffers[cpu] = cpu_buffer_a;
2926
2927 cpu_buffer_b->buffer = buffer_a;
2928 cpu_buffer_a->buffer = buffer_b;
2929
2930 atomic_dec(&cpu_buffer_a->record_disabled);
2931 atomic_dec(&cpu_buffer_b->record_disabled);
2932
Steven Rostedt554f7862009-03-11 22:00:13 -04002933 ret = 0;
2934out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002935 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002936}
Robert Richterc4f50182008-12-11 16:49:22 +01002937EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002938
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002939/**
2940 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2941 * @buffer: the buffer to allocate for.
2942 *
2943 * This function is used in conjunction with ring_buffer_read_page.
2944 * When reading a full page from the ring buffer, these functions
2945 * can be used to speed up the process. The calling function should
2946 * allocate a few pages first with this function. Then when it
2947 * needs to get pages from the ring buffer, it passes the result
2948 * of this function into ring_buffer_read_page, which will swap
2949 * the page that was allocated, with the read page of the buffer.
2950 *
2951 * Returns:
2952 * The page allocated, or NULL on error.
2953 */
2954void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2955{
Steven Rostedt044fa782008-12-02 23:50:03 -05002956 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002957 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002958
2959 addr = __get_free_page(GFP_KERNEL);
2960 if (!addr)
2961 return NULL;
2962
Steven Rostedt044fa782008-12-02 23:50:03 -05002963 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002964
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002965 rb_init_page(bpage);
2966
Steven Rostedt044fa782008-12-02 23:50:03 -05002967 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002968}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002969EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002970
2971/**
2972 * ring_buffer_free_read_page - free an allocated read page
2973 * @buffer: the buffer the page was allocate for
2974 * @data: the page to free
2975 *
2976 * Free a page allocated from ring_buffer_alloc_read_page.
2977 */
2978void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2979{
2980 free_page((unsigned long)data);
2981}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04002982EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002983
2984/**
2985 * ring_buffer_read_page - extract a page from the ring buffer
2986 * @buffer: buffer to extract from
2987 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002988 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002989 * @cpu: the cpu of the buffer to extract
2990 * @full: should the extraction only happen when the page is full.
2991 *
2992 * This function will pull out a page from the ring buffer and consume it.
2993 * @data_page must be the address of the variable that was returned
2994 * from ring_buffer_alloc_read_page. This is because the page might be used
2995 * to swap with a page in the ring buffer.
2996 *
2997 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002998 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002999 * if (!rpage)
3000 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003001 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003002 * if (ret >= 0)
3003 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003004 *
3005 * When @full is set, the function will not return true unless
3006 * the writer is off the reader page.
3007 *
3008 * Note: it is up to the calling functions to handle sleeps and wakeups.
3009 * The ring buffer can be used anywhere in the kernel and can not
3010 * blindly call wake_up. The layer that uses the ring buffer must be
3011 * responsible for that.
3012 *
3013 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003014 * >=0 if data has been transferred, returns the offset of consumed data.
3015 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003016 */
3017int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003018 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003019{
3020 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3021 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003022 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003023 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003024 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003025 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003026 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003027 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003028 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003029
Steven Rostedt554f7862009-03-11 22:00:13 -04003030 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3031 goto out;
3032
Steven Rostedt474d32b2009-03-03 19:51:40 -05003033 /*
3034 * If len is not big enough to hold the page header, then
3035 * we can not copy anything.
3036 */
3037 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003038 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003039
3040 len -= BUF_PAGE_HDR_SIZE;
3041
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003042 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003043 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003044
Steven Rostedt044fa782008-12-02 23:50:03 -05003045 bpage = *data_page;
3046 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003047 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003048
3049 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3050
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003051 reader = rb_get_reader_page(cpu_buffer);
3052 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003053 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003054
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003055 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003056
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003057 read = reader->read;
3058 commit = rb_page_commit(reader);
3059
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003060 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003061 * If this page has been partially read or
3062 * if len is not big enough to read the rest of the page or
3063 * a writer is still on the page, then
3064 * we must copy the data from the page to the buffer.
3065 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003066 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003067 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003068 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003069 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003070 unsigned int rpos = read;
3071 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003072 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003073
3074 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003075 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003076
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003077 if (len > (commit - read))
3078 len = (commit - read);
3079
3080 size = rb_event_length(event);
3081
3082 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003083 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003084
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003085 /* save the current timestamp, since the user will need it */
3086 save_timestamp = cpu_buffer->read_stamp;
3087
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003088 /* Need to copy one event at a time */
3089 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003090 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003091
3092 len -= size;
3093
3094 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003095 rpos = reader->read;
3096 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003097
3098 event = rb_reader_event(cpu_buffer);
3099 size = rb_event_length(event);
3100 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003101
3102 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003103 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003104 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003105
Steven Rostedt474d32b2009-03-03 19:51:40 -05003106 /* we copied everything to the beginning */
3107 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003108 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003109 /* update the entry counter */
3110 cpu_buffer->read += local_read(&reader->entries);
3111
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003112 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003113 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003114 bpage = reader->page;
3115 reader->page = *data_page;
3116 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003117 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003118 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003119 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003120 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003121 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003122
Steven Rostedt554f7862009-03-11 22:00:13 -04003123 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003124 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3125
Steven Rostedt554f7862009-03-11 22:00:13 -04003126 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003127 return ret;
3128}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003129EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003130
Paul Mundt1155de42009-06-25 14:30:12 +09003131#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003132static ssize_t
3133rb_simple_read(struct file *filp, char __user *ubuf,
3134 size_t cnt, loff_t *ppos)
3135{
Hannes Eder5e398412009-02-10 19:44:34 +01003136 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003137 char buf[64];
3138 int r;
3139
Steven Rostedt033601a2008-11-21 12:41:55 -05003140 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3141 r = sprintf(buf, "permanently disabled\n");
3142 else
3143 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003144
3145 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3146}
3147
3148static ssize_t
3149rb_simple_write(struct file *filp, const char __user *ubuf,
3150 size_t cnt, loff_t *ppos)
3151{
Hannes Eder5e398412009-02-10 19:44:34 +01003152 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003153 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003154 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003155 int ret;
3156
3157 if (cnt >= sizeof(buf))
3158 return -EINVAL;
3159
3160 if (copy_from_user(&buf, ubuf, cnt))
3161 return -EFAULT;
3162
3163 buf[cnt] = 0;
3164
3165 ret = strict_strtoul(buf, 10, &val);
3166 if (ret < 0)
3167 return ret;
3168
Steven Rostedt033601a2008-11-21 12:41:55 -05003169 if (val)
3170 set_bit(RB_BUFFERS_ON_BIT, p);
3171 else
3172 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003173
3174 (*ppos)++;
3175
3176 return cnt;
3177}
3178
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003179static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003180 .open = tracing_open_generic,
3181 .read = rb_simple_read,
3182 .write = rb_simple_write,
3183};
3184
3185
3186static __init int rb_init_debugfs(void)
3187{
3188 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003189
3190 d_tracer = tracing_init_dentry();
3191
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003192 trace_create_file("tracing_on", 0644, d_tracer,
3193 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003194
3195 return 0;
3196}
3197
3198fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003199#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003200
Steven Rostedt59222ef2009-03-12 11:46:03 -04003201#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003202static int rb_cpu_notify(struct notifier_block *self,
3203 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003204{
3205 struct ring_buffer *buffer =
3206 container_of(self, struct ring_buffer, cpu_notify);
3207 long cpu = (long)hcpu;
3208
3209 switch (action) {
3210 case CPU_UP_PREPARE:
3211 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303212 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003213 return NOTIFY_OK;
3214
3215 buffer->buffers[cpu] =
3216 rb_allocate_cpu_buffer(buffer, cpu);
3217 if (!buffer->buffers[cpu]) {
3218 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3219 cpu);
3220 return NOTIFY_OK;
3221 }
3222 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303223 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003224 break;
3225 case CPU_DOWN_PREPARE:
3226 case CPU_DOWN_PREPARE_FROZEN:
3227 /*
3228 * Do nothing.
3229 * If we were to free the buffer, then the user would
3230 * lose any trace that was in the buffer.
3231 */
3232 break;
3233 default:
3234 break;
3235 }
3236 return NOTIFY_OK;
3237}
3238#endif