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