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