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