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