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