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