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> |
1744a21d | 13 | #include <linux/kmemcheck.h> |
7a8e76a3 SR |
14 | #include <linux/module.h> |
15 | #include <linux/percpu.h> | |
16 | #include <linux/mutex.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
7a8e76a3 SR |
18 | #include <linux/init.h> |
19 | #include <linux/hash.h> | |
20 | #include <linux/list.h> | |
554f786e | 21 | #include <linux/cpu.h> |
7a8e76a3 SR |
22 | #include <linux/fs.h> |
23 | ||
79615760 | 24 | #include <asm/local.h> |
182e9f5f SR |
25 | #include "trace.h" |
26 | ||
d1b182a8 SR |
27 | /* |
28 | * The ring buffer header is special. We must manually up keep it. | |
29 | */ | |
30 | int ring_buffer_print_entry_header(struct trace_seq *s) | |
31 | { | |
32 | int ret; | |
33 | ||
334d4169 LJ |
34 | ret = trace_seq_printf(s, "# compressed entry header\n"); |
35 | ret = trace_seq_printf(s, "\ttype_len : 5 bits\n"); | |
d1b182a8 SR |
36 | ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n"); |
37 | ret = trace_seq_printf(s, "\tarray : 32 bits\n"); | |
38 | ret = trace_seq_printf(s, "\n"); | |
39 | ret = trace_seq_printf(s, "\tpadding : type == %d\n", | |
40 | RINGBUF_TYPE_PADDING); | |
41 | ret = trace_seq_printf(s, "\ttime_extend : type == %d\n", | |
42 | RINGBUF_TYPE_TIME_EXTEND); | |
334d4169 LJ |
43 | ret = trace_seq_printf(s, "\tdata max type_len == %d\n", |
44 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX); | |
d1b182a8 SR |
45 | |
46 | return ret; | |
47 | } | |
48 | ||
5cc98548 SR |
49 | /* |
50 | * The ring buffer is made up of a list of pages. A separate list of pages is | |
51 | * allocated for each CPU. A writer may only write to a buffer that is | |
52 | * associated with the CPU it is currently executing on. A reader may read | |
53 | * from any per cpu buffer. | |
54 | * | |
55 | * The reader is special. For each per cpu buffer, the reader has its own | |
56 | * reader page. When a reader has read the entire reader page, this reader | |
57 | * page is swapped with another page in the ring buffer. | |
58 | * | |
59 | * Now, as long as the writer is off the reader page, the reader can do what | |
60 | * ever it wants with that page. The writer will never write to that page | |
61 | * again (as long as it is out of the ring buffer). | |
62 | * | |
63 | * Here's some silly ASCII art. | |
64 | * | |
65 | * +------+ | |
66 | * |reader| RING BUFFER | |
67 | * |page | | |
68 | * +------+ +---+ +---+ +---+ | |
69 | * | |-->| |-->| | | |
70 | * +---+ +---+ +---+ | |
71 | * ^ | | |
72 | * | | | |
73 | * +---------------+ | |
74 | * | |
75 | * | |
76 | * +------+ | |
77 | * |reader| RING BUFFER | |
78 | * |page |------------------v | |
79 | * +------+ +---+ +---+ +---+ | |
80 | * | |-->| |-->| | | |
81 | * +---+ +---+ +---+ | |
82 | * ^ | | |
83 | * | | | |
84 | * +---------------+ | |
85 | * | |
86 | * | |
87 | * +------+ | |
88 | * |reader| RING BUFFER | |
89 | * |page |------------------v | |
90 | * +------+ +---+ +---+ +---+ | |
91 | * ^ | |-->| |-->| | | |
92 | * | +---+ +---+ +---+ | |
93 | * | | | |
94 | * | | | |
95 | * +------------------------------+ | |
96 | * | |
97 | * | |
98 | * +------+ | |
99 | * |buffer| RING BUFFER | |
100 | * |page |------------------v | |
101 | * +------+ +---+ +---+ +---+ | |
102 | * ^ | | | |-->| | | |
103 | * | New +---+ +---+ +---+ | |
104 | * | Reader------^ | | |
105 | * | page | | |
106 | * +------------------------------+ | |
107 | * | |
108 | * | |
109 | * After we make this swap, the reader can hand this page off to the splice | |
110 | * code and be done with it. It can even allocate a new page if it needs to | |
111 | * and swap that into the ring buffer. | |
112 | * | |
113 | * We will be using cmpxchg soon to make all this lockless. | |
114 | * | |
115 | */ | |
116 | ||
033601a3 SR |
117 | /* |
118 | * A fast way to enable or disable all ring buffers is to | |
119 | * call tracing_on or tracing_off. Turning off the ring buffers | |
120 | * prevents all ring buffers from being recorded to. | |
121 | * Turning this switch on, makes it OK to write to the | |
122 | * ring buffer, if the ring buffer is enabled itself. | |
123 | * | |
124 | * There's three layers that must be on in order to write | |
125 | * to the ring buffer. | |
126 | * | |
127 | * 1) This global flag must be set. | |
128 | * 2) The ring buffer must be enabled for recording. | |
129 | * 3) The per cpu buffer must be enabled for recording. | |
130 | * | |
131 | * In case of an anomaly, this global flag has a bit set that | |
132 | * will permantly disable all ring buffers. | |
133 | */ | |
134 | ||
135 | /* | |
136 | * Global flag to disable all recording to ring buffers | |
137 | * This has two bits: ON, DISABLED | |
138 | * | |
139 | * ON DISABLED | |
140 | * ---- ---------- | |
141 | * 0 0 : ring buffers are off | |
142 | * 1 0 : ring buffers are on | |
143 | * X 1 : ring buffers are permanently disabled | |
144 | */ | |
145 | ||
146 | enum { | |
147 | RB_BUFFERS_ON_BIT = 0, | |
148 | RB_BUFFERS_DISABLED_BIT = 1, | |
149 | }; | |
150 | ||
151 | enum { | |
152 | RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT, | |
153 | RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT, | |
154 | }; | |
155 | ||
5e39841c | 156 | static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON; |
a3583244 | 157 | |
474d32b6 SR |
158 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
159 | ||
a3583244 SR |
160 | /** |
161 | * tracing_on - enable all tracing buffers | |
162 | * | |
163 | * This function enables all tracing buffers that may have been | |
164 | * disabled with tracing_off. | |
165 | */ | |
166 | void tracing_on(void) | |
167 | { | |
033601a3 | 168 | set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
a3583244 | 169 | } |
c4f50183 | 170 | EXPORT_SYMBOL_GPL(tracing_on); |
a3583244 SR |
171 | |
172 | /** | |
173 | * tracing_off - turn off all tracing buffers | |
174 | * | |
175 | * This function stops all tracing buffers from recording data. | |
176 | * It does not disable any overhead the tracers themselves may | |
177 | * be causing. This function simply causes all recording to | |
178 | * the ring buffers to fail. | |
179 | */ | |
180 | void tracing_off(void) | |
181 | { | |
033601a3 SR |
182 | clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
183 | } | |
c4f50183 | 184 | EXPORT_SYMBOL_GPL(tracing_off); |
033601a3 SR |
185 | |
186 | /** | |
187 | * tracing_off_permanent - permanently disable ring buffers | |
188 | * | |
189 | * This function, once called, will disable all ring buffers | |
c3706f00 | 190 | * permanently. |
033601a3 SR |
191 | */ |
192 | void tracing_off_permanent(void) | |
193 | { | |
194 | set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags); | |
a3583244 SR |
195 | } |
196 | ||
988ae9d6 SR |
197 | /** |
198 | * tracing_is_on - show state of ring buffers enabled | |
199 | */ | |
200 | int tracing_is_on(void) | |
201 | { | |
202 | return ring_buffer_flags == RB_BUFFERS_ON; | |
203 | } | |
204 | EXPORT_SYMBOL_GPL(tracing_is_on); | |
205 | ||
e3d6bf0a | 206 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
67d34724 | 207 | #define RB_ALIGNMENT 4U |
334d4169 | 208 | #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
c7b09308 | 209 | #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */ |
334d4169 | 210 | |
2271048d SR |
211 | #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
212 | # define RB_FORCE_8BYTE_ALIGNMENT 0 | |
213 | # define RB_ARCH_ALIGNMENT RB_ALIGNMENT | |
214 | #else | |
215 | # define RB_FORCE_8BYTE_ALIGNMENT 1 | |
216 | # define RB_ARCH_ALIGNMENT 8U | |
217 | #endif | |
218 | ||
334d4169 LJ |
219 | /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ |
220 | #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX | |
7a8e76a3 SR |
221 | |
222 | enum { | |
223 | RB_LEN_TIME_EXTEND = 8, | |
224 | RB_LEN_TIME_STAMP = 16, | |
225 | }; | |
226 | ||
2d622719 TZ |
227 | static inline int rb_null_event(struct ring_buffer_event *event) |
228 | { | |
a1863c21 | 229 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
2d622719 TZ |
230 | } |
231 | ||
232 | static void rb_event_set_padding(struct ring_buffer_event *event) | |
233 | { | |
a1863c21 | 234 | /* padding has a NULL time_delta */ |
334d4169 | 235 | event->type_len = RINGBUF_TYPE_PADDING; |
2d622719 TZ |
236 | event->time_delta = 0; |
237 | } | |
238 | ||
34a148bf | 239 | static unsigned |
2d622719 | 240 | rb_event_data_length(struct ring_buffer_event *event) |
7a8e76a3 SR |
241 | { |
242 | unsigned length; | |
243 | ||
334d4169 LJ |
244 | if (event->type_len) |
245 | length = event->type_len * RB_ALIGNMENT; | |
2d622719 TZ |
246 | else |
247 | length = event->array[0]; | |
248 | return length + RB_EVNT_HDR_SIZE; | |
249 | } | |
250 | ||
251 | /* inline for ring buffer fast paths */ | |
252 | static unsigned | |
253 | rb_event_length(struct ring_buffer_event *event) | |
254 | { | |
334d4169 | 255 | switch (event->type_len) { |
7a8e76a3 | 256 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
257 | if (rb_null_event(event)) |
258 | /* undefined */ | |
259 | return -1; | |
334d4169 | 260 | return event->array[0] + RB_EVNT_HDR_SIZE; |
7a8e76a3 SR |
261 | |
262 | case RINGBUF_TYPE_TIME_EXTEND: | |
263 | return RB_LEN_TIME_EXTEND; | |
264 | ||
265 | case RINGBUF_TYPE_TIME_STAMP: | |
266 | return RB_LEN_TIME_STAMP; | |
267 | ||
268 | case RINGBUF_TYPE_DATA: | |
2d622719 | 269 | return rb_event_data_length(event); |
7a8e76a3 SR |
270 | default: |
271 | BUG(); | |
272 | } | |
273 | /* not hit */ | |
274 | return 0; | |
275 | } | |
276 | ||
277 | /** | |
278 | * ring_buffer_event_length - return the length of the event | |
279 | * @event: the event to get the length of | |
280 | */ | |
281 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) | |
282 | { | |
465634ad | 283 | unsigned length = rb_event_length(event); |
334d4169 | 284 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
465634ad RR |
285 | return length; |
286 | length -= RB_EVNT_HDR_SIZE; | |
287 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) | |
288 | length -= sizeof(event->array[0]); | |
289 | return length; | |
7a8e76a3 | 290 | } |
c4f50183 | 291 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
7a8e76a3 SR |
292 | |
293 | /* inline for ring buffer fast paths */ | |
34a148bf | 294 | static void * |
7a8e76a3 SR |
295 | rb_event_data(struct ring_buffer_event *event) |
296 | { | |
334d4169 | 297 | BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
7a8e76a3 | 298 | /* If length is in len field, then array[0] has the data */ |
334d4169 | 299 | if (event->type_len) |
7a8e76a3 SR |
300 | return (void *)&event->array[0]; |
301 | /* Otherwise length is in array[0] and array[1] has the data */ | |
302 | return (void *)&event->array[1]; | |
303 | } | |
304 | ||
305 | /** | |
306 | * ring_buffer_event_data - return the data of the event | |
307 | * @event: the event to get the data from | |
308 | */ | |
309 | void *ring_buffer_event_data(struct ring_buffer_event *event) | |
310 | { | |
311 | return rb_event_data(event); | |
312 | } | |
c4f50183 | 313 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
7a8e76a3 SR |
314 | |
315 | #define for_each_buffer_cpu(buffer, cpu) \ | |
9e01c1b7 | 316 | for_each_cpu(cpu, buffer->cpumask) |
7a8e76a3 SR |
317 | |
318 | #define TS_SHIFT 27 | |
319 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) | |
320 | #define TS_DELTA_TEST (~TS_MASK) | |
321 | ||
66a8cb95 SR |
322 | /* Flag when events were overwritten */ |
323 | #define RB_MISSED_EVENTS (1 << 31) | |
ff0ff84a SR |
324 | /* Missed count stored at end */ |
325 | #define RB_MISSED_STORED (1 << 30) | |
66a8cb95 | 326 | |
abc9b56d | 327 | struct buffer_data_page { |
e4c2ce82 | 328 | u64 time_stamp; /* page time stamp */ |
c3706f00 | 329 | local_t commit; /* write committed index */ |
abc9b56d SR |
330 | unsigned char data[]; /* data of buffer page */ |
331 | }; | |
332 | ||
77ae365e SR |
333 | /* |
334 | * Note, the buffer_page list must be first. The buffer pages | |
335 | * are allocated in cache lines, which means that each buffer | |
336 | * page will be at the beginning of a cache line, and thus | |
337 | * the least significant bits will be zero. We use this to | |
338 | * add flags in the list struct pointers, to make the ring buffer | |
339 | * lockless. | |
340 | */ | |
abc9b56d | 341 | struct buffer_page { |
778c55d4 | 342 | struct list_head list; /* list of buffer pages */ |
abc9b56d | 343 | local_t write; /* index for next write */ |
6f807acd | 344 | unsigned read; /* index for next read */ |
778c55d4 | 345 | local_t entries; /* entries on this page */ |
ff0ff84a | 346 | unsigned long real_end; /* real end of data */ |
abc9b56d | 347 | struct buffer_data_page *page; /* Actual data page */ |
7a8e76a3 SR |
348 | }; |
349 | ||
77ae365e SR |
350 | /* |
351 | * The buffer page counters, write and entries, must be reset | |
352 | * atomically when crossing page boundaries. To synchronize this | |
353 | * update, two counters are inserted into the number. One is | |
354 | * the actual counter for the write position or count on the page. | |
355 | * | |
356 | * The other is a counter of updaters. Before an update happens | |
357 | * the update partition of the counter is incremented. This will | |
358 | * allow the updater to update the counter atomically. | |
359 | * | |
360 | * The counter is 20 bits, and the state data is 12. | |
361 | */ | |
362 | #define RB_WRITE_MASK 0xfffff | |
363 | #define RB_WRITE_INTCNT (1 << 20) | |
364 | ||
044fa782 | 365 | static void rb_init_page(struct buffer_data_page *bpage) |
abc9b56d | 366 | { |
044fa782 | 367 | local_set(&bpage->commit, 0); |
abc9b56d SR |
368 | } |
369 | ||
474d32b6 SR |
370 | /** |
371 | * ring_buffer_page_len - the size of data on the page. | |
372 | * @page: The page to read | |
373 | * | |
374 | * Returns the amount of data on the page, including buffer page header. | |
375 | */ | |
ef7a4a16 SR |
376 | size_t ring_buffer_page_len(void *page) |
377 | { | |
474d32b6 SR |
378 | return local_read(&((struct buffer_data_page *)page)->commit) |
379 | + BUF_PAGE_HDR_SIZE; | |
ef7a4a16 SR |
380 | } |
381 | ||
ed56829c SR |
382 | /* |
383 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing | |
384 | * this issue out. | |
385 | */ | |
34a148bf | 386 | static void free_buffer_page(struct buffer_page *bpage) |
ed56829c | 387 | { |
34a148bf | 388 | free_page((unsigned long)bpage->page); |
e4c2ce82 | 389 | kfree(bpage); |
ed56829c SR |
390 | } |
391 | ||
7a8e76a3 SR |
392 | /* |
393 | * We need to fit the time_stamp delta into 27 bits. | |
394 | */ | |
395 | static inline int test_time_stamp(u64 delta) | |
396 | { | |
397 | if (delta & TS_DELTA_TEST) | |
398 | return 1; | |
399 | return 0; | |
400 | } | |
401 | ||
474d32b6 | 402 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
7a8e76a3 | 403 | |
be957c44 SR |
404 | /* Max payload is BUF_PAGE_SIZE - header (8bytes) */ |
405 | #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2)) | |
406 | ||
ea05b57c | 407 | /* Max number of timestamps that can fit on a page */ |
d0134324 | 408 | #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_EXTEND) |
ea05b57c | 409 | |
d1b182a8 SR |
410 | int ring_buffer_print_page_header(struct trace_seq *s) |
411 | { | |
412 | struct buffer_data_page field; | |
413 | int ret; | |
414 | ||
415 | ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t" | |
26a50744 TZ |
416 | "offset:0;\tsize:%u;\tsigned:%u;\n", |
417 | (unsigned int)sizeof(field.time_stamp), | |
418 | (unsigned int)is_signed_type(u64)); | |
d1b182a8 SR |
419 | |
420 | ret = trace_seq_printf(s, "\tfield: local_t commit;\t" | |
26a50744 | 421 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 422 | (unsigned int)offsetof(typeof(field), commit), |
26a50744 TZ |
423 | (unsigned int)sizeof(field.commit), |
424 | (unsigned int)is_signed_type(long)); | |
d1b182a8 | 425 | |
66a8cb95 SR |
426 | ret = trace_seq_printf(s, "\tfield: int overwrite;\t" |
427 | "offset:%u;\tsize:%u;\tsigned:%u;\n", | |
428 | (unsigned int)offsetof(typeof(field), commit), | |
429 | 1, | |
430 | (unsigned int)is_signed_type(long)); | |
431 | ||
d1b182a8 | 432 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
26a50744 | 433 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 434 | (unsigned int)offsetof(typeof(field), data), |
26a50744 TZ |
435 | (unsigned int)BUF_PAGE_SIZE, |
436 | (unsigned int)is_signed_type(char)); | |
d1b182a8 SR |
437 | |
438 | return ret; | |
439 | } | |
440 | ||
7a8e76a3 SR |
441 | /* |
442 | * head_page == tail_page && head == tail then buffer is empty. | |
443 | */ | |
444 | struct ring_buffer_per_cpu { | |
445 | int cpu; | |
985023de | 446 | atomic_t record_disabled; |
7a8e76a3 | 447 | struct ring_buffer *buffer; |
77ae365e | 448 | spinlock_t reader_lock; /* serialize readers */ |
445c8951 | 449 | arch_spinlock_t lock; |
7a8e76a3 | 450 | struct lock_class_key lock_key; |
3adc54fa | 451 | struct list_head *pages; |
6f807acd SR |
452 | struct buffer_page *head_page; /* read from head */ |
453 | struct buffer_page *tail_page; /* write to tail */ | |
c3706f00 | 454 | struct buffer_page *commit_page; /* committed pages */ |
d769041f | 455 | struct buffer_page *reader_page; |
66a8cb95 SR |
456 | unsigned long lost_events; |
457 | unsigned long last_overrun; | |
77ae365e SR |
458 | local_t commit_overrun; |
459 | local_t overrun; | |
e4906eff | 460 | local_t entries; |
fa743953 SR |
461 | local_t committing; |
462 | local_t commits; | |
77ae365e | 463 | unsigned long read; |
7a8e76a3 SR |
464 | u64 write_stamp; |
465 | u64 read_stamp; | |
7a8e76a3 SR |
466 | }; |
467 | ||
468 | struct ring_buffer { | |
7a8e76a3 SR |
469 | unsigned pages; |
470 | unsigned flags; | |
471 | int cpus; | |
7a8e76a3 | 472 | atomic_t record_disabled; |
00f62f61 | 473 | cpumask_var_t cpumask; |
7a8e76a3 | 474 | |
1f8a6a10 PZ |
475 | struct lock_class_key *reader_lock_key; |
476 | ||
7a8e76a3 SR |
477 | struct mutex mutex; |
478 | ||
479 | struct ring_buffer_per_cpu **buffers; | |
554f786e | 480 | |
59222efe | 481 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
482 | struct notifier_block cpu_notify; |
483 | #endif | |
37886f6a | 484 | u64 (*clock)(void); |
7a8e76a3 SR |
485 | }; |
486 | ||
487 | struct ring_buffer_iter { | |
488 | struct ring_buffer_per_cpu *cpu_buffer; | |
489 | unsigned long head; | |
490 | struct buffer_page *head_page; | |
492a74f4 SR |
491 | struct buffer_page *cache_reader_page; |
492 | unsigned long cache_read; | |
7a8e76a3 SR |
493 | u64 read_stamp; |
494 | }; | |
495 | ||
f536aafc | 496 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
077c5407 SR |
497 | #define RB_WARN_ON(b, cond) \ |
498 | ({ \ | |
499 | int _____ret = unlikely(cond); \ | |
500 | if (_____ret) { \ | |
501 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ | |
502 | struct ring_buffer_per_cpu *__b = \ | |
503 | (void *)b; \ | |
504 | atomic_inc(&__b->buffer->record_disabled); \ | |
505 | } else \ | |
506 | atomic_inc(&b->record_disabled); \ | |
507 | WARN_ON(1); \ | |
508 | } \ | |
509 | _____ret; \ | |
3e89c7bb | 510 | }) |
f536aafc | 511 | |
37886f6a SR |
512 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
513 | #define DEBUG_SHIFT 0 | |
514 | ||
6d3f1e12 | 515 | static inline u64 rb_time_stamp(struct ring_buffer *buffer) |
88eb0125 SR |
516 | { |
517 | /* shift to debug/test normalization and TIME_EXTENTS */ | |
518 | return buffer->clock() << DEBUG_SHIFT; | |
519 | } | |
520 | ||
37886f6a SR |
521 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu) |
522 | { | |
523 | u64 time; | |
524 | ||
525 | preempt_disable_notrace(); | |
6d3f1e12 | 526 | time = rb_time_stamp(buffer); |
37886f6a SR |
527 | preempt_enable_no_resched_notrace(); |
528 | ||
529 | return time; | |
530 | } | |
531 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); | |
532 | ||
533 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, | |
534 | int cpu, u64 *ts) | |
535 | { | |
536 | /* Just stupid testing the normalize function and deltas */ | |
537 | *ts >>= DEBUG_SHIFT; | |
538 | } | |
539 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); | |
540 | ||
77ae365e SR |
541 | /* |
542 | * Making the ring buffer lockless makes things tricky. | |
543 | * Although writes only happen on the CPU that they are on, | |
544 | * and they only need to worry about interrupts. Reads can | |
545 | * happen on any CPU. | |
546 | * | |
547 | * The reader page is always off the ring buffer, but when the | |
548 | * reader finishes with a page, it needs to swap its page with | |
549 | * a new one from the buffer. The reader needs to take from | |
550 | * the head (writes go to the tail). But if a writer is in overwrite | |
551 | * mode and wraps, it must push the head page forward. | |
552 | * | |
553 | * Here lies the problem. | |
554 | * | |
555 | * The reader must be careful to replace only the head page, and | |
556 | * not another one. As described at the top of the file in the | |
557 | * ASCII art, the reader sets its old page to point to the next | |
558 | * page after head. It then sets the page after head to point to | |
559 | * the old reader page. But if the writer moves the head page | |
560 | * during this operation, the reader could end up with the tail. | |
561 | * | |
562 | * We use cmpxchg to help prevent this race. We also do something | |
563 | * special with the page before head. We set the LSB to 1. | |
564 | * | |
565 | * When the writer must push the page forward, it will clear the | |
566 | * bit that points to the head page, move the head, and then set | |
567 | * the bit that points to the new head page. | |
568 | * | |
569 | * We also don't want an interrupt coming in and moving the head | |
570 | * page on another writer. Thus we use the second LSB to catch | |
571 | * that too. Thus: | |
572 | * | |
573 | * head->list->prev->next bit 1 bit 0 | |
574 | * ------- ------- | |
575 | * Normal page 0 0 | |
576 | * Points to head page 0 1 | |
577 | * New head page 1 0 | |
578 | * | |
579 | * Note we can not trust the prev pointer of the head page, because: | |
580 | * | |
581 | * +----+ +-----+ +-----+ | |
582 | * | |------>| T |---X--->| N | | |
583 | * | |<------| | | | | |
584 | * +----+ +-----+ +-----+ | |
585 | * ^ ^ | | |
586 | * | +-----+ | | | |
587 | * +----------| R |----------+ | | |
588 | * | |<-----------+ | |
589 | * +-----+ | |
590 | * | |
591 | * Key: ---X--> HEAD flag set in pointer | |
592 | * T Tail page | |
593 | * R Reader page | |
594 | * N Next page | |
595 | * | |
596 | * (see __rb_reserve_next() to see where this happens) | |
597 | * | |
598 | * What the above shows is that the reader just swapped out | |
599 | * the reader page with a page in the buffer, but before it | |
600 | * could make the new header point back to the new page added | |
601 | * it was preempted by a writer. The writer moved forward onto | |
602 | * the new page added by the reader and is about to move forward | |
603 | * again. | |
604 | * | |
605 | * You can see, it is legitimate for the previous pointer of | |
606 | * the head (or any page) not to point back to itself. But only | |
607 | * temporarially. | |
608 | */ | |
609 | ||
610 | #define RB_PAGE_NORMAL 0UL | |
611 | #define RB_PAGE_HEAD 1UL | |
612 | #define RB_PAGE_UPDATE 2UL | |
613 | ||
614 | ||
615 | #define RB_FLAG_MASK 3UL | |
616 | ||
617 | /* PAGE_MOVED is not part of the mask */ | |
618 | #define RB_PAGE_MOVED 4UL | |
619 | ||
620 | /* | |
621 | * rb_list_head - remove any bit | |
622 | */ | |
623 | static struct list_head *rb_list_head(struct list_head *list) | |
624 | { | |
625 | unsigned long val = (unsigned long)list; | |
626 | ||
627 | return (struct list_head *)(val & ~RB_FLAG_MASK); | |
628 | } | |
629 | ||
630 | /* | |
6d3f1e12 | 631 | * rb_is_head_page - test if the given page is the head page |
77ae365e SR |
632 | * |
633 | * Because the reader may move the head_page pointer, we can | |
634 | * not trust what the head page is (it may be pointing to | |
635 | * the reader page). But if the next page is a header page, | |
636 | * its flags will be non zero. | |
637 | */ | |
638 | static int inline | |
639 | rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer, | |
640 | struct buffer_page *page, struct list_head *list) | |
641 | { | |
642 | unsigned long val; | |
643 | ||
644 | val = (unsigned long)list->next; | |
645 | ||
646 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) | |
647 | return RB_PAGE_MOVED; | |
648 | ||
649 | return val & RB_FLAG_MASK; | |
650 | } | |
651 | ||
652 | /* | |
653 | * rb_is_reader_page | |
654 | * | |
655 | * The unique thing about the reader page, is that, if the | |
656 | * writer is ever on it, the previous pointer never points | |
657 | * back to the reader page. | |
658 | */ | |
659 | static int rb_is_reader_page(struct buffer_page *page) | |
660 | { | |
661 | struct list_head *list = page->list.prev; | |
662 | ||
663 | return rb_list_head(list->next) != &page->list; | |
664 | } | |
665 | ||
666 | /* | |
667 | * rb_set_list_to_head - set a list_head to be pointing to head. | |
668 | */ | |
669 | static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer, | |
670 | struct list_head *list) | |
671 | { | |
672 | unsigned long *ptr; | |
673 | ||
674 | ptr = (unsigned long *)&list->next; | |
675 | *ptr |= RB_PAGE_HEAD; | |
676 | *ptr &= ~RB_PAGE_UPDATE; | |
677 | } | |
678 | ||
679 | /* | |
680 | * rb_head_page_activate - sets up head page | |
681 | */ | |
682 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) | |
683 | { | |
684 | struct buffer_page *head; | |
685 | ||
686 | head = cpu_buffer->head_page; | |
687 | if (!head) | |
688 | return; | |
689 | ||
690 | /* | |
691 | * Set the previous list pointer to have the HEAD flag. | |
692 | */ | |
693 | rb_set_list_to_head(cpu_buffer, head->list.prev); | |
694 | } | |
695 | ||
696 | static void rb_list_head_clear(struct list_head *list) | |
697 | { | |
698 | unsigned long *ptr = (unsigned long *)&list->next; | |
699 | ||
700 | *ptr &= ~RB_FLAG_MASK; | |
701 | } | |
702 | ||
703 | /* | |
704 | * rb_head_page_dactivate - clears head page ptr (for free list) | |
705 | */ | |
706 | static void | |
707 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) | |
708 | { | |
709 | struct list_head *hd; | |
710 | ||
711 | /* Go through the whole list and clear any pointers found. */ | |
712 | rb_list_head_clear(cpu_buffer->pages); | |
713 | ||
714 | list_for_each(hd, cpu_buffer->pages) | |
715 | rb_list_head_clear(hd); | |
716 | } | |
717 | ||
718 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, | |
719 | struct buffer_page *head, | |
720 | struct buffer_page *prev, | |
721 | int old_flag, int new_flag) | |
722 | { | |
723 | struct list_head *list; | |
724 | unsigned long val = (unsigned long)&head->list; | |
725 | unsigned long ret; | |
726 | ||
727 | list = &prev->list; | |
728 | ||
729 | val &= ~RB_FLAG_MASK; | |
730 | ||
08a40816 SR |
731 | ret = cmpxchg((unsigned long *)&list->next, |
732 | val | old_flag, val | new_flag); | |
77ae365e SR |
733 | |
734 | /* check if the reader took the page */ | |
735 | if ((ret & ~RB_FLAG_MASK) != val) | |
736 | return RB_PAGE_MOVED; | |
737 | ||
738 | return ret & RB_FLAG_MASK; | |
739 | } | |
740 | ||
741 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, | |
742 | struct buffer_page *head, | |
743 | struct buffer_page *prev, | |
744 | int old_flag) | |
745 | { | |
746 | return rb_head_page_set(cpu_buffer, head, prev, | |
747 | old_flag, RB_PAGE_UPDATE); | |
748 | } | |
749 | ||
750 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, | |
751 | struct buffer_page *head, | |
752 | struct buffer_page *prev, | |
753 | int old_flag) | |
754 | { | |
755 | return rb_head_page_set(cpu_buffer, head, prev, | |
756 | old_flag, RB_PAGE_HEAD); | |
757 | } | |
758 | ||
759 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, | |
760 | struct buffer_page *head, | |
761 | struct buffer_page *prev, | |
762 | int old_flag) | |
763 | { | |
764 | return rb_head_page_set(cpu_buffer, head, prev, | |
765 | old_flag, RB_PAGE_NORMAL); | |
766 | } | |
767 | ||
768 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, | |
769 | struct buffer_page **bpage) | |
770 | { | |
771 | struct list_head *p = rb_list_head((*bpage)->list.next); | |
772 | ||
773 | *bpage = list_entry(p, struct buffer_page, list); | |
774 | } | |
775 | ||
776 | static struct buffer_page * | |
777 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) | |
778 | { | |
779 | struct buffer_page *head; | |
780 | struct buffer_page *page; | |
781 | struct list_head *list; | |
782 | int i; | |
783 | ||
784 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) | |
785 | return NULL; | |
786 | ||
787 | /* sanity check */ | |
788 | list = cpu_buffer->pages; | |
789 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) | |
790 | return NULL; | |
791 | ||
792 | page = head = cpu_buffer->head_page; | |
793 | /* | |
794 | * It is possible that the writer moves the header behind | |
795 | * where we started, and we miss in one loop. | |
796 | * A second loop should grab the header, but we'll do | |
797 | * three loops just because I'm paranoid. | |
798 | */ | |
799 | for (i = 0; i < 3; i++) { | |
800 | do { | |
801 | if (rb_is_head_page(cpu_buffer, page, page->list.prev)) { | |
802 | cpu_buffer->head_page = page; | |
803 | return page; | |
804 | } | |
805 | rb_inc_page(cpu_buffer, &page); | |
806 | } while (page != head); | |
807 | } | |
808 | ||
809 | RB_WARN_ON(cpu_buffer, 1); | |
810 | ||
811 | return NULL; | |
812 | } | |
813 | ||
814 | static int rb_head_page_replace(struct buffer_page *old, | |
815 | struct buffer_page *new) | |
816 | { | |
817 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; | |
818 | unsigned long val; | |
819 | unsigned long ret; | |
820 | ||
821 | val = *ptr & ~RB_FLAG_MASK; | |
822 | val |= RB_PAGE_HEAD; | |
823 | ||
08a40816 | 824 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
77ae365e SR |
825 | |
826 | return ret == val; | |
827 | } | |
828 | ||
829 | /* | |
830 | * rb_tail_page_update - move the tail page forward | |
831 | * | |
832 | * Returns 1 if moved tail page, 0 if someone else did. | |
833 | */ | |
834 | static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, | |
835 | struct buffer_page *tail_page, | |
836 | struct buffer_page *next_page) | |
837 | { | |
838 | struct buffer_page *old_tail; | |
839 | unsigned long old_entries; | |
840 | unsigned long old_write; | |
841 | int ret = 0; | |
842 | ||
843 | /* | |
844 | * The tail page now needs to be moved forward. | |
845 | * | |
846 | * We need to reset the tail page, but without messing | |
847 | * with possible erasing of data brought in by interrupts | |
848 | * that have moved the tail page and are currently on it. | |
849 | * | |
850 | * We add a counter to the write field to denote this. | |
851 | */ | |
852 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); | |
853 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); | |
854 | ||
855 | /* | |
856 | * Just make sure we have seen our old_write and synchronize | |
857 | * with any interrupts that come in. | |
858 | */ | |
859 | barrier(); | |
860 | ||
861 | /* | |
862 | * If the tail page is still the same as what we think | |
863 | * it is, then it is up to us to update the tail | |
864 | * pointer. | |
865 | */ | |
866 | if (tail_page == cpu_buffer->tail_page) { | |
867 | /* Zero the write counter */ | |
868 | unsigned long val = old_write & ~RB_WRITE_MASK; | |
869 | unsigned long eval = old_entries & ~RB_WRITE_MASK; | |
870 | ||
871 | /* | |
872 | * This will only succeed if an interrupt did | |
873 | * not come in and change it. In which case, we | |
874 | * do not want to modify it. | |
da706d8b LJ |
875 | * |
876 | * We add (void) to let the compiler know that we do not care | |
877 | * about the return value of these functions. We use the | |
878 | * cmpxchg to only update if an interrupt did not already | |
879 | * do it for us. If the cmpxchg fails, we don't care. | |
77ae365e | 880 | */ |
da706d8b LJ |
881 | (void)local_cmpxchg(&next_page->write, old_write, val); |
882 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); | |
77ae365e SR |
883 | |
884 | /* | |
885 | * No need to worry about races with clearing out the commit. | |
886 | * it only can increment when a commit takes place. But that | |
887 | * only happens in the outer most nested commit. | |
888 | */ | |
889 | local_set(&next_page->page->commit, 0); | |
890 | ||
891 | old_tail = cmpxchg(&cpu_buffer->tail_page, | |
892 | tail_page, next_page); | |
893 | ||
894 | if (old_tail == tail_page) | |
895 | ret = 1; | |
896 | } | |
897 | ||
898 | return ret; | |
899 | } | |
900 | ||
901 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, | |
902 | struct buffer_page *bpage) | |
903 | { | |
904 | unsigned long val = (unsigned long)bpage; | |
905 | ||
906 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) | |
907 | return 1; | |
908 | ||
909 | return 0; | |
910 | } | |
911 | ||
912 | /** | |
913 | * rb_check_list - make sure a pointer to a list has the last bits zero | |
914 | */ | |
915 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, | |
916 | struct list_head *list) | |
917 | { | |
918 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) | |
919 | return 1; | |
920 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) | |
921 | return 1; | |
922 | return 0; | |
923 | } | |
924 | ||
7a8e76a3 SR |
925 | /** |
926 | * check_pages - integrity check of buffer pages | |
927 | * @cpu_buffer: CPU buffer with pages to test | |
928 | * | |
c3706f00 | 929 | * As a safety measure we check to make sure the data pages have not |
7a8e76a3 SR |
930 | * been corrupted. |
931 | */ | |
932 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) | |
933 | { | |
3adc54fa | 934 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 935 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 936 | |
77ae365e SR |
937 | rb_head_page_deactivate(cpu_buffer); |
938 | ||
3e89c7bb SR |
939 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
940 | return -1; | |
941 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) | |
942 | return -1; | |
7a8e76a3 | 943 | |
77ae365e SR |
944 | if (rb_check_list(cpu_buffer, head)) |
945 | return -1; | |
946 | ||
044fa782 | 947 | list_for_each_entry_safe(bpage, tmp, head, list) { |
3e89c7bb | 948 | if (RB_WARN_ON(cpu_buffer, |
044fa782 | 949 | bpage->list.next->prev != &bpage->list)) |
3e89c7bb SR |
950 | return -1; |
951 | if (RB_WARN_ON(cpu_buffer, | |
044fa782 | 952 | bpage->list.prev->next != &bpage->list)) |
3e89c7bb | 953 | return -1; |
77ae365e SR |
954 | if (rb_check_list(cpu_buffer, &bpage->list)) |
955 | return -1; | |
7a8e76a3 SR |
956 | } |
957 | ||
77ae365e SR |
958 | rb_head_page_activate(cpu_buffer); |
959 | ||
7a8e76a3 SR |
960 | return 0; |
961 | } | |
962 | ||
7a8e76a3 SR |
963 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
964 | unsigned nr_pages) | |
965 | { | |
044fa782 | 966 | struct buffer_page *bpage, *tmp; |
7a8e76a3 SR |
967 | unsigned long addr; |
968 | LIST_HEAD(pages); | |
969 | unsigned i; | |
970 | ||
3adc54fa SR |
971 | WARN_ON(!nr_pages); |
972 | ||
7a8e76a3 | 973 | for (i = 0; i < nr_pages; i++) { |
044fa782 | 974 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
aa1e0e3b | 975 | GFP_KERNEL, cpu_to_node(cpu_buffer->cpu)); |
044fa782 | 976 | if (!bpage) |
e4c2ce82 | 977 | goto free_pages; |
77ae365e SR |
978 | |
979 | rb_check_bpage(cpu_buffer, bpage); | |
980 | ||
044fa782 | 981 | list_add(&bpage->list, &pages); |
e4c2ce82 | 982 | |
7a8e76a3 SR |
983 | addr = __get_free_page(GFP_KERNEL); |
984 | if (!addr) | |
985 | goto free_pages; | |
044fa782 SR |
986 | bpage->page = (void *)addr; |
987 | rb_init_page(bpage->page); | |
7a8e76a3 SR |
988 | } |
989 | ||
3adc54fa SR |
990 | /* |
991 | * The ring buffer page list is a circular list that does not | |
992 | * start and end with a list head. All page list items point to | |
993 | * other pages. | |
994 | */ | |
995 | cpu_buffer->pages = pages.next; | |
996 | list_del(&pages); | |
7a8e76a3 SR |
997 | |
998 | rb_check_pages(cpu_buffer); | |
999 | ||
1000 | return 0; | |
1001 | ||
1002 | free_pages: | |
044fa782 SR |
1003 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
1004 | list_del_init(&bpage->list); | |
1005 | free_buffer_page(bpage); | |
7a8e76a3 SR |
1006 | } |
1007 | return -ENOMEM; | |
1008 | } | |
1009 | ||
1010 | static struct ring_buffer_per_cpu * | |
1011 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu) | |
1012 | { | |
1013 | struct ring_buffer_per_cpu *cpu_buffer; | |
044fa782 | 1014 | struct buffer_page *bpage; |
d769041f | 1015 | unsigned long addr; |
7a8e76a3 SR |
1016 | int ret; |
1017 | ||
1018 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), | |
1019 | GFP_KERNEL, cpu_to_node(cpu)); | |
1020 | if (!cpu_buffer) | |
1021 | return NULL; | |
1022 | ||
1023 | cpu_buffer->cpu = cpu; | |
1024 | cpu_buffer->buffer = buffer; | |
f83c9d0f | 1025 | spin_lock_init(&cpu_buffer->reader_lock); |
1f8a6a10 | 1026 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
edc35bd7 | 1027 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
7a8e76a3 | 1028 | |
044fa782 | 1029 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
e4c2ce82 | 1030 | GFP_KERNEL, cpu_to_node(cpu)); |
044fa782 | 1031 | if (!bpage) |
e4c2ce82 SR |
1032 | goto fail_free_buffer; |
1033 | ||
77ae365e SR |
1034 | rb_check_bpage(cpu_buffer, bpage); |
1035 | ||
044fa782 | 1036 | cpu_buffer->reader_page = bpage; |
d769041f SR |
1037 | addr = __get_free_page(GFP_KERNEL); |
1038 | if (!addr) | |
e4c2ce82 | 1039 | goto fail_free_reader; |
044fa782 SR |
1040 | bpage->page = (void *)addr; |
1041 | rb_init_page(bpage->page); | |
e4c2ce82 | 1042 | |
d769041f | 1043 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
d769041f | 1044 | |
7a8e76a3 SR |
1045 | ret = rb_allocate_pages(cpu_buffer, buffer->pages); |
1046 | if (ret < 0) | |
d769041f | 1047 | goto fail_free_reader; |
7a8e76a3 SR |
1048 | |
1049 | cpu_buffer->head_page | |
3adc54fa | 1050 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
bf41a158 | 1051 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
7a8e76a3 | 1052 | |
77ae365e SR |
1053 | rb_head_page_activate(cpu_buffer); |
1054 | ||
7a8e76a3 SR |
1055 | return cpu_buffer; |
1056 | ||
d769041f SR |
1057 | fail_free_reader: |
1058 | free_buffer_page(cpu_buffer->reader_page); | |
1059 | ||
7a8e76a3 SR |
1060 | fail_free_buffer: |
1061 | kfree(cpu_buffer); | |
1062 | return NULL; | |
1063 | } | |
1064 | ||
1065 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) | |
1066 | { | |
3adc54fa | 1067 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 1068 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 1069 | |
d769041f SR |
1070 | free_buffer_page(cpu_buffer->reader_page); |
1071 | ||
77ae365e SR |
1072 | rb_head_page_deactivate(cpu_buffer); |
1073 | ||
3adc54fa SR |
1074 | if (head) { |
1075 | list_for_each_entry_safe(bpage, tmp, head, list) { | |
1076 | list_del_init(&bpage->list); | |
1077 | free_buffer_page(bpage); | |
1078 | } | |
1079 | bpage = list_entry(head, struct buffer_page, list); | |
044fa782 | 1080 | free_buffer_page(bpage); |
7a8e76a3 | 1081 | } |
3adc54fa | 1082 | |
7a8e76a3 SR |
1083 | kfree(cpu_buffer); |
1084 | } | |
1085 | ||
59222efe | 1086 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
1087 | static int rb_cpu_notify(struct notifier_block *self, |
1088 | unsigned long action, void *hcpu); | |
554f786e SR |
1089 | #endif |
1090 | ||
7a8e76a3 SR |
1091 | /** |
1092 | * ring_buffer_alloc - allocate a new ring_buffer | |
68814b58 | 1093 | * @size: the size in bytes per cpu that is needed. |
7a8e76a3 SR |
1094 | * @flags: attributes to set for the ring buffer. |
1095 | * | |
1096 | * Currently the only flag that is available is the RB_FL_OVERWRITE | |
1097 | * flag. This flag means that the buffer will overwrite old data | |
1098 | * when the buffer wraps. If this flag is not set, the buffer will | |
1099 | * drop data when the tail hits the head. | |
1100 | */ | |
1f8a6a10 PZ |
1101 | struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
1102 | struct lock_class_key *key) | |
7a8e76a3 SR |
1103 | { |
1104 | struct ring_buffer *buffer; | |
1105 | int bsize; | |
1106 | int cpu; | |
1107 | ||
1108 | /* keep it in its own cache line */ | |
1109 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), | |
1110 | GFP_KERNEL); | |
1111 | if (!buffer) | |
1112 | return NULL; | |
1113 | ||
9e01c1b7 RR |
1114 | if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
1115 | goto fail_free_buffer; | |
1116 | ||
7a8e76a3 SR |
1117 | buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
1118 | buffer->flags = flags; | |
37886f6a | 1119 | buffer->clock = trace_clock_local; |
1f8a6a10 | 1120 | buffer->reader_lock_key = key; |
7a8e76a3 SR |
1121 | |
1122 | /* need at least two pages */ | |
5f78abee SR |
1123 | if (buffer->pages < 2) |
1124 | buffer->pages = 2; | |
7a8e76a3 | 1125 | |
3bf832ce FW |
1126 | /* |
1127 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
1128 | * in early initcall, it will not be notified of secondary cpus. | |
1129 | * In that off case, we need to allocate for all possible cpus. | |
1130 | */ | |
1131 | #ifdef CONFIG_HOTPLUG_CPU | |
554f786e SR |
1132 | get_online_cpus(); |
1133 | cpumask_copy(buffer->cpumask, cpu_online_mask); | |
3bf832ce FW |
1134 | #else |
1135 | cpumask_copy(buffer->cpumask, cpu_possible_mask); | |
1136 | #endif | |
7a8e76a3 SR |
1137 | buffer->cpus = nr_cpu_ids; |
1138 | ||
1139 | bsize = sizeof(void *) * nr_cpu_ids; | |
1140 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), | |
1141 | GFP_KERNEL); | |
1142 | if (!buffer->buffers) | |
9e01c1b7 | 1143 | goto fail_free_cpumask; |
7a8e76a3 SR |
1144 | |
1145 | for_each_buffer_cpu(buffer, cpu) { | |
1146 | buffer->buffers[cpu] = | |
1147 | rb_allocate_cpu_buffer(buffer, cpu); | |
1148 | if (!buffer->buffers[cpu]) | |
1149 | goto fail_free_buffers; | |
1150 | } | |
1151 | ||
59222efe | 1152 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
1153 | buffer->cpu_notify.notifier_call = rb_cpu_notify; |
1154 | buffer->cpu_notify.priority = 0; | |
1155 | register_cpu_notifier(&buffer->cpu_notify); | |
1156 | #endif | |
1157 | ||
1158 | put_online_cpus(); | |
7a8e76a3 SR |
1159 | mutex_init(&buffer->mutex); |
1160 | ||
1161 | return buffer; | |
1162 | ||
1163 | fail_free_buffers: | |
1164 | for_each_buffer_cpu(buffer, cpu) { | |
1165 | if (buffer->buffers[cpu]) | |
1166 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1167 | } | |
1168 | kfree(buffer->buffers); | |
1169 | ||
9e01c1b7 RR |
1170 | fail_free_cpumask: |
1171 | free_cpumask_var(buffer->cpumask); | |
554f786e | 1172 | put_online_cpus(); |
9e01c1b7 | 1173 | |
7a8e76a3 SR |
1174 | fail_free_buffer: |
1175 | kfree(buffer); | |
1176 | return NULL; | |
1177 | } | |
1f8a6a10 | 1178 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
7a8e76a3 SR |
1179 | |
1180 | /** | |
1181 | * ring_buffer_free - free a ring buffer. | |
1182 | * @buffer: the buffer to free. | |
1183 | */ | |
1184 | void | |
1185 | ring_buffer_free(struct ring_buffer *buffer) | |
1186 | { | |
1187 | int cpu; | |
1188 | ||
554f786e SR |
1189 | get_online_cpus(); |
1190 | ||
59222efe | 1191 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
1192 | unregister_cpu_notifier(&buffer->cpu_notify); |
1193 | #endif | |
1194 | ||
7a8e76a3 SR |
1195 | for_each_buffer_cpu(buffer, cpu) |
1196 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1197 | ||
554f786e SR |
1198 | put_online_cpus(); |
1199 | ||
bd3f0221 | 1200 | kfree(buffer->buffers); |
9e01c1b7 RR |
1201 | free_cpumask_var(buffer->cpumask); |
1202 | ||
7a8e76a3 SR |
1203 | kfree(buffer); |
1204 | } | |
c4f50183 | 1205 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
7a8e76a3 | 1206 | |
37886f6a SR |
1207 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
1208 | u64 (*clock)(void)) | |
1209 | { | |
1210 | buffer->clock = clock; | |
1211 | } | |
1212 | ||
7a8e76a3 SR |
1213 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
1214 | ||
1215 | static void | |
1216 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages) | |
1217 | { | |
044fa782 | 1218 | struct buffer_page *bpage; |
7a8e76a3 SR |
1219 | struct list_head *p; |
1220 | unsigned i; | |
1221 | ||
f7112949 | 1222 | spin_lock_irq(&cpu_buffer->reader_lock); |
77ae365e SR |
1223 | rb_head_page_deactivate(cpu_buffer); |
1224 | ||
7a8e76a3 | 1225 | for (i = 0; i < nr_pages; i++) { |
3adc54fa | 1226 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
292f60c0 | 1227 | goto out; |
3adc54fa | 1228 | p = cpu_buffer->pages->next; |
044fa782 SR |
1229 | bpage = list_entry(p, struct buffer_page, list); |
1230 | list_del_init(&bpage->list); | |
1231 | free_buffer_page(bpage); | |
7a8e76a3 | 1232 | } |
3adc54fa | 1233 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
292f60c0 | 1234 | goto out; |
7a8e76a3 SR |
1235 | |
1236 | rb_reset_cpu(cpu_buffer); | |
7a8e76a3 SR |
1237 | rb_check_pages(cpu_buffer); |
1238 | ||
292f60c0 | 1239 | out: |
dd7f5943 | 1240 | spin_unlock_irq(&cpu_buffer->reader_lock); |
7a8e76a3 SR |
1241 | } |
1242 | ||
1243 | static void | |
1244 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer, | |
1245 | struct list_head *pages, unsigned nr_pages) | |
1246 | { | |
044fa782 | 1247 | struct buffer_page *bpage; |
7a8e76a3 SR |
1248 | struct list_head *p; |
1249 | unsigned i; | |
1250 | ||
77ae365e SR |
1251 | spin_lock_irq(&cpu_buffer->reader_lock); |
1252 | rb_head_page_deactivate(cpu_buffer); | |
1253 | ||
7a8e76a3 | 1254 | for (i = 0; i < nr_pages; i++) { |
3e89c7bb | 1255 | if (RB_WARN_ON(cpu_buffer, list_empty(pages))) |
292f60c0 | 1256 | goto out; |
7a8e76a3 | 1257 | p = pages->next; |
044fa782 SR |
1258 | bpage = list_entry(p, struct buffer_page, list); |
1259 | list_del_init(&bpage->list); | |
3adc54fa | 1260 | list_add_tail(&bpage->list, cpu_buffer->pages); |
7a8e76a3 SR |
1261 | } |
1262 | rb_reset_cpu(cpu_buffer); | |
7a8e76a3 SR |
1263 | rb_check_pages(cpu_buffer); |
1264 | ||
292f60c0 | 1265 | out: |
dd7f5943 | 1266 | spin_unlock_irq(&cpu_buffer->reader_lock); |
7a8e76a3 SR |
1267 | } |
1268 | ||
1269 | /** | |
1270 | * ring_buffer_resize - resize the ring buffer | |
1271 | * @buffer: the buffer to resize. | |
1272 | * @size: the new size. | |
1273 | * | |
7a8e76a3 SR |
1274 | * Minimum size is 2 * BUF_PAGE_SIZE. |
1275 | * | |
1276 | * Returns -1 on failure. | |
1277 | */ | |
1278 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size) | |
1279 | { | |
1280 | struct ring_buffer_per_cpu *cpu_buffer; | |
1281 | unsigned nr_pages, rm_pages, new_pages; | |
044fa782 | 1282 | struct buffer_page *bpage, *tmp; |
7a8e76a3 SR |
1283 | unsigned long buffer_size; |
1284 | unsigned long addr; | |
1285 | LIST_HEAD(pages); | |
1286 | int i, cpu; | |
1287 | ||
ee51a1de IM |
1288 | /* |
1289 | * Always succeed at resizing a non-existent buffer: | |
1290 | */ | |
1291 | if (!buffer) | |
1292 | return size; | |
1293 | ||
7a8e76a3 SR |
1294 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
1295 | size *= BUF_PAGE_SIZE; | |
1296 | buffer_size = buffer->pages * BUF_PAGE_SIZE; | |
1297 | ||
1298 | /* we need a minimum of two pages */ | |
1299 | if (size < BUF_PAGE_SIZE * 2) | |
1300 | size = BUF_PAGE_SIZE * 2; | |
1301 | ||
1302 | if (size == buffer_size) | |
1303 | return size; | |
1304 | ||
18421015 SR |
1305 | atomic_inc(&buffer->record_disabled); |
1306 | ||
1307 | /* Make sure all writers are done with this buffer. */ | |
1308 | synchronize_sched(); | |
1309 | ||
7a8e76a3 | 1310 | mutex_lock(&buffer->mutex); |
554f786e | 1311 | get_online_cpus(); |
7a8e76a3 SR |
1312 | |
1313 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); | |
1314 | ||
1315 | if (size < buffer_size) { | |
1316 | ||
1317 | /* easy case, just free pages */ | |
554f786e SR |
1318 | if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) |
1319 | goto out_fail; | |
7a8e76a3 SR |
1320 | |
1321 | rm_pages = buffer->pages - nr_pages; | |
1322 | ||
1323 | for_each_buffer_cpu(buffer, cpu) { | |
1324 | cpu_buffer = buffer->buffers[cpu]; | |
1325 | rb_remove_pages(cpu_buffer, rm_pages); | |
1326 | } | |
1327 | goto out; | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * This is a bit more difficult. We only want to add pages | |
1332 | * when we can allocate enough for all CPUs. We do this | |
1333 | * by allocating all the pages and storing them on a local | |
1334 | * link list. If we succeed in our allocation, then we | |
1335 | * add these pages to the cpu_buffers. Otherwise we just free | |
1336 | * them all and return -ENOMEM; | |
1337 | */ | |
554f786e SR |
1338 | if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) |
1339 | goto out_fail; | |
f536aafc | 1340 | |
7a8e76a3 SR |
1341 | new_pages = nr_pages - buffer->pages; |
1342 | ||
1343 | for_each_buffer_cpu(buffer, cpu) { | |
1344 | for (i = 0; i < new_pages; i++) { | |
044fa782 | 1345 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), |
e4c2ce82 SR |
1346 | cache_line_size()), |
1347 | GFP_KERNEL, cpu_to_node(cpu)); | |
044fa782 | 1348 | if (!bpage) |
e4c2ce82 | 1349 | goto free_pages; |
044fa782 | 1350 | list_add(&bpage->list, &pages); |
7a8e76a3 SR |
1351 | addr = __get_free_page(GFP_KERNEL); |
1352 | if (!addr) | |
1353 | goto free_pages; | |
044fa782 SR |
1354 | bpage->page = (void *)addr; |
1355 | rb_init_page(bpage->page); | |
7a8e76a3 SR |
1356 | } |
1357 | } | |
1358 | ||
1359 | for_each_buffer_cpu(buffer, cpu) { | |
1360 | cpu_buffer = buffer->buffers[cpu]; | |
1361 | rb_insert_pages(cpu_buffer, &pages, new_pages); | |
1362 | } | |
1363 | ||
554f786e SR |
1364 | if (RB_WARN_ON(buffer, !list_empty(&pages))) |
1365 | goto out_fail; | |
7a8e76a3 SR |
1366 | |
1367 | out: | |
1368 | buffer->pages = nr_pages; | |
554f786e | 1369 | put_online_cpus(); |
7a8e76a3 SR |
1370 | mutex_unlock(&buffer->mutex); |
1371 | ||
18421015 SR |
1372 | atomic_dec(&buffer->record_disabled); |
1373 | ||
7a8e76a3 SR |
1374 | return size; |
1375 | ||
1376 | free_pages: | |
044fa782 SR |
1377 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
1378 | list_del_init(&bpage->list); | |
1379 | free_buffer_page(bpage); | |
7a8e76a3 | 1380 | } |
554f786e | 1381 | put_online_cpus(); |
641d2f63 | 1382 | mutex_unlock(&buffer->mutex); |
18421015 | 1383 | atomic_dec(&buffer->record_disabled); |
7a8e76a3 | 1384 | return -ENOMEM; |
554f786e SR |
1385 | |
1386 | /* | |
1387 | * Something went totally wrong, and we are too paranoid | |
1388 | * to even clean up the mess. | |
1389 | */ | |
1390 | out_fail: | |
1391 | put_online_cpus(); | |
1392 | mutex_unlock(&buffer->mutex); | |
18421015 | 1393 | atomic_dec(&buffer->record_disabled); |
554f786e | 1394 | return -1; |
7a8e76a3 | 1395 | } |
c4f50183 | 1396 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
7a8e76a3 | 1397 | |
8789a9e7 | 1398 | static inline void * |
044fa782 | 1399 | __rb_data_page_index(struct buffer_data_page *bpage, unsigned index) |
8789a9e7 | 1400 | { |
044fa782 | 1401 | return bpage->data + index; |
8789a9e7 SR |
1402 | } |
1403 | ||
044fa782 | 1404 | static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
7a8e76a3 | 1405 | { |
044fa782 | 1406 | return bpage->page->data + index; |
7a8e76a3 SR |
1407 | } |
1408 | ||
1409 | static inline struct ring_buffer_event * | |
d769041f | 1410 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1411 | { |
6f807acd SR |
1412 | return __rb_page_index(cpu_buffer->reader_page, |
1413 | cpu_buffer->reader_page->read); | |
1414 | } | |
1415 | ||
7a8e76a3 SR |
1416 | static inline struct ring_buffer_event * |
1417 | rb_iter_head_event(struct ring_buffer_iter *iter) | |
1418 | { | |
6f807acd | 1419 | return __rb_page_index(iter->head_page, iter->head); |
7a8e76a3 SR |
1420 | } |
1421 | ||
77ae365e | 1422 | static inline unsigned long rb_page_write(struct buffer_page *bpage) |
bf41a158 | 1423 | { |
77ae365e | 1424 | return local_read(&bpage->write) & RB_WRITE_MASK; |
bf41a158 SR |
1425 | } |
1426 | ||
1427 | static inline unsigned rb_page_commit(struct buffer_page *bpage) | |
1428 | { | |
abc9b56d | 1429 | return local_read(&bpage->page->commit); |
bf41a158 SR |
1430 | } |
1431 | ||
77ae365e SR |
1432 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
1433 | { | |
1434 | return local_read(&bpage->entries) & RB_WRITE_MASK; | |
1435 | } | |
1436 | ||
bf41a158 SR |
1437 | /* Size is determined by what has been commited */ |
1438 | static inline unsigned rb_page_size(struct buffer_page *bpage) | |
1439 | { | |
1440 | return rb_page_commit(bpage); | |
1441 | } | |
1442 | ||
1443 | static inline unsigned | |
1444 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) | |
1445 | { | |
1446 | return rb_page_commit(cpu_buffer->commit_page); | |
1447 | } | |
1448 | ||
bf41a158 SR |
1449 | static inline unsigned |
1450 | rb_event_index(struct ring_buffer_event *event) | |
1451 | { | |
1452 | unsigned long addr = (unsigned long)event; | |
1453 | ||
22f470f8 | 1454 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
bf41a158 SR |
1455 | } |
1456 | ||
0f0c85fc | 1457 | static inline int |
fa743953 SR |
1458 | rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
1459 | struct ring_buffer_event *event) | |
bf41a158 SR |
1460 | { |
1461 | unsigned long addr = (unsigned long)event; | |
1462 | unsigned long index; | |
1463 | ||
1464 | index = rb_event_index(event); | |
1465 | addr &= PAGE_MASK; | |
1466 | ||
1467 | return cpu_buffer->commit_page->page == (void *)addr && | |
1468 | rb_commit_index(cpu_buffer) == index; | |
1469 | } | |
1470 | ||
34a148bf | 1471 | static void |
bf41a158 | 1472 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1473 | { |
77ae365e SR |
1474 | unsigned long max_count; |
1475 | ||
bf41a158 SR |
1476 | /* |
1477 | * We only race with interrupts and NMIs on this CPU. | |
1478 | * If we own the commit event, then we can commit | |
1479 | * all others that interrupted us, since the interruptions | |
1480 | * are in stack format (they finish before they come | |
1481 | * back to us). This allows us to do a simple loop to | |
1482 | * assign the commit to the tail. | |
1483 | */ | |
a8ccf1d6 | 1484 | again: |
77ae365e SR |
1485 | max_count = cpu_buffer->buffer->pages * 100; |
1486 | ||
bf41a158 | 1487 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
77ae365e SR |
1488 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
1489 | return; | |
1490 | if (RB_WARN_ON(cpu_buffer, | |
1491 | rb_is_reader_page(cpu_buffer->tail_page))) | |
1492 | return; | |
1493 | local_set(&cpu_buffer->commit_page->page->commit, | |
1494 | rb_page_write(cpu_buffer->commit_page)); | |
bf41a158 | 1495 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
abc9b56d SR |
1496 | cpu_buffer->write_stamp = |
1497 | cpu_buffer->commit_page->page->time_stamp; | |
bf41a158 SR |
1498 | /* add barrier to keep gcc from optimizing too much */ |
1499 | barrier(); | |
1500 | } | |
1501 | while (rb_commit_index(cpu_buffer) != | |
1502 | rb_page_write(cpu_buffer->commit_page)) { | |
77ae365e SR |
1503 | |
1504 | local_set(&cpu_buffer->commit_page->page->commit, | |
1505 | rb_page_write(cpu_buffer->commit_page)); | |
1506 | RB_WARN_ON(cpu_buffer, | |
1507 | local_read(&cpu_buffer->commit_page->page->commit) & | |
1508 | ~RB_WRITE_MASK); | |
bf41a158 SR |
1509 | barrier(); |
1510 | } | |
a8ccf1d6 SR |
1511 | |
1512 | /* again, keep gcc from optimizing */ | |
1513 | barrier(); | |
1514 | ||
1515 | /* | |
1516 | * If an interrupt came in just after the first while loop | |
1517 | * and pushed the tail page forward, we will be left with | |
1518 | * a dangling commit that will never go forward. | |
1519 | */ | |
1520 | if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page)) | |
1521 | goto again; | |
7a8e76a3 SR |
1522 | } |
1523 | ||
d769041f | 1524 | static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1525 | { |
abc9b56d | 1526 | cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; |
6f807acd | 1527 | cpu_buffer->reader_page->read = 0; |
d769041f SR |
1528 | } |
1529 | ||
34a148bf | 1530 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
d769041f SR |
1531 | { |
1532 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
1533 | ||
1534 | /* | |
1535 | * The iterator could be on the reader page (it starts there). | |
1536 | * But the head could have moved, since the reader was | |
1537 | * found. Check for this case and assign the iterator | |
1538 | * to the head page instead of next. | |
1539 | */ | |
1540 | if (iter->head_page == cpu_buffer->reader_page) | |
77ae365e | 1541 | iter->head_page = rb_set_head_page(cpu_buffer); |
d769041f SR |
1542 | else |
1543 | rb_inc_page(cpu_buffer, &iter->head_page); | |
1544 | ||
abc9b56d | 1545 | iter->read_stamp = iter->head_page->page->time_stamp; |
7a8e76a3 SR |
1546 | iter->head = 0; |
1547 | } | |
1548 | ||
1549 | /** | |
1550 | * ring_buffer_update_event - update event type and data | |
1551 | * @event: the even to update | |
1552 | * @type: the type of event | |
1553 | * @length: the size of the event field in the ring buffer | |
1554 | * | |
1555 | * Update the type and data fields of the event. The length | |
1556 | * is the actual size that is written to the ring buffer, | |
1557 | * and with this, we can determine what to place into the | |
1558 | * data field. | |
1559 | */ | |
34a148bf | 1560 | static void |
7a8e76a3 SR |
1561 | rb_update_event(struct ring_buffer_event *event, |
1562 | unsigned type, unsigned length) | |
1563 | { | |
334d4169 | 1564 | event->type_len = type; |
7a8e76a3 SR |
1565 | |
1566 | switch (type) { | |
1567 | ||
1568 | case RINGBUF_TYPE_PADDING: | |
7a8e76a3 | 1569 | case RINGBUF_TYPE_TIME_EXTEND: |
7a8e76a3 | 1570 | case RINGBUF_TYPE_TIME_STAMP: |
7a8e76a3 SR |
1571 | break; |
1572 | ||
334d4169 | 1573 | case 0: |
7a8e76a3 | 1574 | length -= RB_EVNT_HDR_SIZE; |
2271048d | 1575 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
7a8e76a3 | 1576 | event->array[0] = length; |
334d4169 LJ |
1577 | else |
1578 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); | |
7a8e76a3 SR |
1579 | break; |
1580 | default: | |
1581 | BUG(); | |
1582 | } | |
1583 | } | |
1584 | ||
77ae365e SR |
1585 | /* |
1586 | * rb_handle_head_page - writer hit the head page | |
1587 | * | |
1588 | * Returns: +1 to retry page | |
1589 | * 0 to continue | |
1590 | * -1 on error | |
1591 | */ | |
1592 | static int | |
1593 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, | |
1594 | struct buffer_page *tail_page, | |
1595 | struct buffer_page *next_page) | |
1596 | { | |
1597 | struct buffer_page *new_head; | |
1598 | int entries; | |
1599 | int type; | |
1600 | int ret; | |
1601 | ||
1602 | entries = rb_page_entries(next_page); | |
1603 | ||
1604 | /* | |
1605 | * The hard part is here. We need to move the head | |
1606 | * forward, and protect against both readers on | |
1607 | * other CPUs and writers coming in via interrupts. | |
1608 | */ | |
1609 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, | |
1610 | RB_PAGE_HEAD); | |
1611 | ||
1612 | /* | |
1613 | * type can be one of four: | |
1614 | * NORMAL - an interrupt already moved it for us | |
1615 | * HEAD - we are the first to get here. | |
1616 | * UPDATE - we are the interrupt interrupting | |
1617 | * a current move. | |
1618 | * MOVED - a reader on another CPU moved the next | |
1619 | * pointer to its reader page. Give up | |
1620 | * and try again. | |
1621 | */ | |
1622 | ||
1623 | switch (type) { | |
1624 | case RB_PAGE_HEAD: | |
1625 | /* | |
1626 | * We changed the head to UPDATE, thus | |
1627 | * it is our responsibility to update | |
1628 | * the counters. | |
1629 | */ | |
1630 | local_add(entries, &cpu_buffer->overrun); | |
1631 | ||
1632 | /* | |
1633 | * The entries will be zeroed out when we move the | |
1634 | * tail page. | |
1635 | */ | |
1636 | ||
1637 | /* still more to do */ | |
1638 | break; | |
1639 | ||
1640 | case RB_PAGE_UPDATE: | |
1641 | /* | |
1642 | * This is an interrupt that interrupt the | |
1643 | * previous update. Still more to do. | |
1644 | */ | |
1645 | break; | |
1646 | case RB_PAGE_NORMAL: | |
1647 | /* | |
1648 | * An interrupt came in before the update | |
1649 | * and processed this for us. | |
1650 | * Nothing left to do. | |
1651 | */ | |
1652 | return 1; | |
1653 | case RB_PAGE_MOVED: | |
1654 | /* | |
1655 | * The reader is on another CPU and just did | |
1656 | * a swap with our next_page. | |
1657 | * Try again. | |
1658 | */ | |
1659 | return 1; | |
1660 | default: | |
1661 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ | |
1662 | return -1; | |
1663 | } | |
1664 | ||
1665 | /* | |
1666 | * Now that we are here, the old head pointer is | |
1667 | * set to UPDATE. This will keep the reader from | |
1668 | * swapping the head page with the reader page. | |
1669 | * The reader (on another CPU) will spin till | |
1670 | * we are finished. | |
1671 | * | |
1672 | * We just need to protect against interrupts | |
1673 | * doing the job. We will set the next pointer | |
1674 | * to HEAD. After that, we set the old pointer | |
1675 | * to NORMAL, but only if it was HEAD before. | |
1676 | * otherwise we are an interrupt, and only | |
1677 | * want the outer most commit to reset it. | |
1678 | */ | |
1679 | new_head = next_page; | |
1680 | rb_inc_page(cpu_buffer, &new_head); | |
1681 | ||
1682 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, | |
1683 | RB_PAGE_NORMAL); | |
1684 | ||
1685 | /* | |
1686 | * Valid returns are: | |
1687 | * HEAD - an interrupt came in and already set it. | |
1688 | * NORMAL - One of two things: | |
1689 | * 1) We really set it. | |
1690 | * 2) A bunch of interrupts came in and moved | |
1691 | * the page forward again. | |
1692 | */ | |
1693 | switch (ret) { | |
1694 | case RB_PAGE_HEAD: | |
1695 | case RB_PAGE_NORMAL: | |
1696 | /* OK */ | |
1697 | break; | |
1698 | default: | |
1699 | RB_WARN_ON(cpu_buffer, 1); | |
1700 | return -1; | |
1701 | } | |
1702 | ||
1703 | /* | |
1704 | * It is possible that an interrupt came in, | |
1705 | * set the head up, then more interrupts came in | |
1706 | * and moved it again. When we get back here, | |
1707 | * the page would have been set to NORMAL but we | |
1708 | * just set it back to HEAD. | |
1709 | * | |
1710 | * How do you detect this? Well, if that happened | |
1711 | * the tail page would have moved. | |
1712 | */ | |
1713 | if (ret == RB_PAGE_NORMAL) { | |
1714 | /* | |
1715 | * If the tail had moved passed next, then we need | |
1716 | * to reset the pointer. | |
1717 | */ | |
1718 | if (cpu_buffer->tail_page != tail_page && | |
1719 | cpu_buffer->tail_page != next_page) | |
1720 | rb_head_page_set_normal(cpu_buffer, new_head, | |
1721 | next_page, | |
1722 | RB_PAGE_HEAD); | |
1723 | } | |
1724 | ||
1725 | /* | |
1726 | * If this was the outer most commit (the one that | |
1727 | * changed the original pointer from HEAD to UPDATE), | |
1728 | * then it is up to us to reset it to NORMAL. | |
1729 | */ | |
1730 | if (type == RB_PAGE_HEAD) { | |
1731 | ret = rb_head_page_set_normal(cpu_buffer, next_page, | |
1732 | tail_page, | |
1733 | RB_PAGE_UPDATE); | |
1734 | if (RB_WARN_ON(cpu_buffer, | |
1735 | ret != RB_PAGE_UPDATE)) | |
1736 | return -1; | |
1737 | } | |
1738 | ||
1739 | return 0; | |
1740 | } | |
1741 | ||
34a148bf | 1742 | static unsigned rb_calculate_event_length(unsigned length) |
7a8e76a3 SR |
1743 | { |
1744 | struct ring_buffer_event event; /* Used only for sizeof array */ | |
1745 | ||
1746 | /* zero length can cause confusions */ | |
1747 | if (!length) | |
1748 | length = 1; | |
1749 | ||
2271048d | 1750 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
7a8e76a3 SR |
1751 | length += sizeof(event.array[0]); |
1752 | ||
1753 | length += RB_EVNT_HDR_SIZE; | |
2271048d | 1754 | length = ALIGN(length, RB_ARCH_ALIGNMENT); |
7a8e76a3 SR |
1755 | |
1756 | return length; | |
1757 | } | |
1758 | ||
c7b09308 SR |
1759 | static inline void |
1760 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, | |
1761 | struct buffer_page *tail_page, | |
1762 | unsigned long tail, unsigned long length) | |
1763 | { | |
1764 | struct ring_buffer_event *event; | |
1765 | ||
1766 | /* | |
1767 | * Only the event that crossed the page boundary | |
1768 | * must fill the old tail_page with padding. | |
1769 | */ | |
1770 | if (tail >= BUF_PAGE_SIZE) { | |
b3230c8b SR |
1771 | /* |
1772 | * If the page was filled, then we still need | |
1773 | * to update the real_end. Reset it to zero | |
1774 | * and the reader will ignore it. | |
1775 | */ | |
1776 | if (tail == BUF_PAGE_SIZE) | |
1777 | tail_page->real_end = 0; | |
1778 | ||
c7b09308 SR |
1779 | local_sub(length, &tail_page->write); |
1780 | return; | |
1781 | } | |
1782 | ||
1783 | event = __rb_page_index(tail_page, tail); | |
b0b7065b | 1784 | kmemcheck_annotate_bitfield(event, bitfield); |
c7b09308 | 1785 | |
ff0ff84a SR |
1786 | /* |
1787 | * Save the original length to the meta data. | |
1788 | * This will be used by the reader to add lost event | |
1789 | * counter. | |
1790 | */ | |
1791 | tail_page->real_end = tail; | |
1792 | ||
c7b09308 SR |
1793 | /* |
1794 | * If this event is bigger than the minimum size, then | |
1795 | * we need to be careful that we don't subtract the | |
1796 | * write counter enough to allow another writer to slip | |
1797 | * in on this page. | |
1798 | * We put in a discarded commit instead, to make sure | |
1799 | * that this space is not used again. | |
1800 | * | |
1801 | * If we are less than the minimum size, we don't need to | |
1802 | * worry about it. | |
1803 | */ | |
1804 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { | |
1805 | /* No room for any events */ | |
1806 | ||
1807 | /* Mark the rest of the page with padding */ | |
1808 | rb_event_set_padding(event); | |
1809 | ||
1810 | /* Set the write back to the previous setting */ | |
1811 | local_sub(length, &tail_page->write); | |
1812 | return; | |
1813 | } | |
1814 | ||
1815 | /* Put in a discarded event */ | |
1816 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; | |
1817 | event->type_len = RINGBUF_TYPE_PADDING; | |
1818 | /* time delta must be non zero */ | |
1819 | event->time_delta = 1; | |
c7b09308 SR |
1820 | |
1821 | /* Set write to end of buffer */ | |
1822 | length = (tail + length) - BUF_PAGE_SIZE; | |
1823 | local_sub(length, &tail_page->write); | |
1824 | } | |
6634ff26 | 1825 | |
747e94ae SR |
1826 | /* |
1827 | * This is the slow path, force gcc not to inline it. | |
1828 | */ | |
1829 | static noinline struct ring_buffer_event * | |
6634ff26 SR |
1830 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
1831 | unsigned long length, unsigned long tail, | |
e8bc43e8 | 1832 | struct buffer_page *tail_page, u64 ts) |
7a8e76a3 | 1833 | { |
5a50e33c | 1834 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
7a8e76a3 | 1835 | struct ring_buffer *buffer = cpu_buffer->buffer; |
77ae365e SR |
1836 | struct buffer_page *next_page; |
1837 | int ret; | |
aa20ae84 SR |
1838 | |
1839 | next_page = tail_page; | |
1840 | ||
aa20ae84 SR |
1841 | rb_inc_page(cpu_buffer, &next_page); |
1842 | ||
aa20ae84 SR |
1843 | /* |
1844 | * If for some reason, we had an interrupt storm that made | |
1845 | * it all the way around the buffer, bail, and warn | |
1846 | * about it. | |
1847 | */ | |
1848 | if (unlikely(next_page == commit_page)) { | |
77ae365e | 1849 | local_inc(&cpu_buffer->commit_overrun); |
aa20ae84 SR |
1850 | goto out_reset; |
1851 | } | |
1852 | ||
77ae365e SR |
1853 | /* |
1854 | * This is where the fun begins! | |
1855 | * | |
1856 | * We are fighting against races between a reader that | |
1857 | * could be on another CPU trying to swap its reader | |
1858 | * page with the buffer head. | |
1859 | * | |
1860 | * We are also fighting against interrupts coming in and | |
1861 | * moving the head or tail on us as well. | |
1862 | * | |
1863 | * If the next page is the head page then we have filled | |
1864 | * the buffer, unless the commit page is still on the | |
1865 | * reader page. | |
1866 | */ | |
1867 | if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) { | |
aa20ae84 | 1868 | |
77ae365e SR |
1869 | /* |
1870 | * If the commit is not on the reader page, then | |
1871 | * move the header page. | |
1872 | */ | |
1873 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { | |
1874 | /* | |
1875 | * If we are not in overwrite mode, | |
1876 | * this is easy, just stop here. | |
1877 | */ | |
1878 | if (!(buffer->flags & RB_FL_OVERWRITE)) | |
1879 | goto out_reset; | |
1880 | ||
1881 | ret = rb_handle_head_page(cpu_buffer, | |
1882 | tail_page, | |
1883 | next_page); | |
1884 | if (ret < 0) | |
1885 | goto out_reset; | |
1886 | if (ret) | |
1887 | goto out_again; | |
1888 | } else { | |
1889 | /* | |
1890 | * We need to be careful here too. The | |
1891 | * commit page could still be on the reader | |
1892 | * page. We could have a small buffer, and | |
1893 | * have filled up the buffer with events | |
1894 | * from interrupts and such, and wrapped. | |
1895 | * | |
1896 | * Note, if the tail page is also the on the | |
1897 | * reader_page, we let it move out. | |
1898 | */ | |
1899 | if (unlikely((cpu_buffer->commit_page != | |
1900 | cpu_buffer->tail_page) && | |
1901 | (cpu_buffer->commit_page == | |
1902 | cpu_buffer->reader_page))) { | |
1903 | local_inc(&cpu_buffer->commit_overrun); | |
1904 | goto out_reset; | |
1905 | } | |
aa20ae84 SR |
1906 | } |
1907 | } | |
1908 | ||
77ae365e SR |
1909 | ret = rb_tail_page_update(cpu_buffer, tail_page, next_page); |
1910 | if (ret) { | |
1911 | /* | |
1912 | * Nested commits always have zero deltas, so | |
1913 | * just reread the time stamp | |
1914 | */ | |
e8bc43e8 SR |
1915 | ts = rb_time_stamp(buffer); |
1916 | next_page->page->time_stamp = ts; | |
aa20ae84 SR |
1917 | } |
1918 | ||
77ae365e | 1919 | out_again: |
aa20ae84 | 1920 | |
77ae365e | 1921 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
aa20ae84 SR |
1922 | |
1923 | /* fail and let the caller try again */ | |
1924 | return ERR_PTR(-EAGAIN); | |
1925 | ||
45141d46 | 1926 | out_reset: |
6f3b3440 | 1927 | /* reset write */ |
c7b09308 | 1928 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
6f3b3440 | 1929 | |
bf41a158 | 1930 | return NULL; |
7a8e76a3 SR |
1931 | } |
1932 | ||
6634ff26 SR |
1933 | static struct ring_buffer_event * |
1934 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, | |
e8bc43e8 | 1935 | unsigned type, unsigned long length, u64 ts) |
6634ff26 | 1936 | { |
5a50e33c | 1937 | struct buffer_page *tail_page; |
6634ff26 SR |
1938 | struct ring_buffer_event *event; |
1939 | unsigned long tail, write; | |
1940 | ||
6634ff26 SR |
1941 | tail_page = cpu_buffer->tail_page; |
1942 | write = local_add_return(length, &tail_page->write); | |
77ae365e SR |
1943 | |
1944 | /* set write to only the index of the write */ | |
1945 | write &= RB_WRITE_MASK; | |
6634ff26 SR |
1946 | tail = write - length; |
1947 | ||
1948 | /* See if we shot pass the end of this buffer page */ | |
747e94ae | 1949 | if (unlikely(write > BUF_PAGE_SIZE)) |
6634ff26 | 1950 | return rb_move_tail(cpu_buffer, length, tail, |
5a50e33c | 1951 | tail_page, ts); |
6634ff26 SR |
1952 | |
1953 | /* We reserved something on the buffer */ | |
1954 | ||
6634ff26 | 1955 | event = __rb_page_index(tail_page, tail); |
1744a21d | 1956 | kmemcheck_annotate_bitfield(event, bitfield); |
6634ff26 SR |
1957 | rb_update_event(event, type, length); |
1958 | ||
1959 | /* The passed in type is zero for DATA */ | |
1960 | if (likely(!type)) | |
1961 | local_inc(&tail_page->entries); | |
1962 | ||
1963 | /* | |
fa743953 SR |
1964 | * If this is the first commit on the page, then update |
1965 | * its timestamp. | |
6634ff26 | 1966 | */ |
fa743953 | 1967 | if (!tail) |
e8bc43e8 | 1968 | tail_page->page->time_stamp = ts; |
6634ff26 SR |
1969 | |
1970 | return event; | |
1971 | } | |
1972 | ||
edd813bf SR |
1973 | static inline int |
1974 | rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, | |
1975 | struct ring_buffer_event *event) | |
1976 | { | |
1977 | unsigned long new_index, old_index; | |
1978 | struct buffer_page *bpage; | |
1979 | unsigned long index; | |
1980 | unsigned long addr; | |
1981 | ||
1982 | new_index = rb_event_index(event); | |
1983 | old_index = new_index + rb_event_length(event); | |
1984 | addr = (unsigned long)event; | |
1985 | addr &= PAGE_MASK; | |
1986 | ||
1987 | bpage = cpu_buffer->tail_page; | |
1988 | ||
1989 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { | |
77ae365e SR |
1990 | unsigned long write_mask = |
1991 | local_read(&bpage->write) & ~RB_WRITE_MASK; | |
edd813bf SR |
1992 | /* |
1993 | * This is on the tail page. It is possible that | |
1994 | * a write could come in and move the tail page | |
1995 | * and write to the next page. That is fine | |
1996 | * because we just shorten what is on this page. | |
1997 | */ | |
77ae365e SR |
1998 | old_index += write_mask; |
1999 | new_index += write_mask; | |
edd813bf SR |
2000 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
2001 | if (index == old_index) | |
2002 | return 1; | |
2003 | } | |
2004 | ||
2005 | /* could not discard */ | |
2006 | return 0; | |
2007 | } | |
2008 | ||
7a8e76a3 SR |
2009 | static int |
2010 | rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
e8bc43e8 | 2011 | u64 ts, u64 *delta) |
7a8e76a3 SR |
2012 | { |
2013 | struct ring_buffer_event *event; | |
bf41a158 | 2014 | int ret; |
7a8e76a3 | 2015 | |
95609791 BP |
2016 | WARN_ONCE(*delta > (1ULL << 59), |
2017 | KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n", | |
2018 | (unsigned long long)*delta, | |
e8bc43e8 | 2019 | (unsigned long long)ts, |
95609791 | 2020 | (unsigned long long)cpu_buffer->write_stamp); |
7a8e76a3 SR |
2021 | |
2022 | /* | |
2023 | * The delta is too big, we to add a | |
2024 | * new timestamp. | |
2025 | */ | |
2026 | event = __rb_reserve_next(cpu_buffer, | |
2027 | RINGBUF_TYPE_TIME_EXTEND, | |
2028 | RB_LEN_TIME_EXTEND, | |
2029 | ts); | |
2030 | if (!event) | |
bf41a158 | 2031 | return -EBUSY; |
7a8e76a3 | 2032 | |
bf41a158 SR |
2033 | if (PTR_ERR(event) == -EAGAIN) |
2034 | return -EAGAIN; | |
2035 | ||
2036 | /* Only a commited time event can update the write stamp */ | |
fa743953 | 2037 | if (rb_event_is_commit(cpu_buffer, event)) { |
bf41a158 | 2038 | /* |
fa743953 SR |
2039 | * If this is the first on the page, then it was |
2040 | * updated with the page itself. Try to discard it | |
2041 | * and if we can't just make it zero. | |
bf41a158 SR |
2042 | */ |
2043 | if (rb_event_index(event)) { | |
2044 | event->time_delta = *delta & TS_MASK; | |
2045 | event->array[0] = *delta >> TS_SHIFT; | |
2046 | } else { | |
ea05b57c SR |
2047 | /* try to discard, since we do not need this */ |
2048 | if (!rb_try_to_discard(cpu_buffer, event)) { | |
2049 | /* nope, just zero it */ | |
2050 | event->time_delta = 0; | |
2051 | event->array[0] = 0; | |
2052 | } | |
bf41a158 | 2053 | } |
e8bc43e8 | 2054 | cpu_buffer->write_stamp = ts; |
bf41a158 SR |
2055 | /* let the caller know this was the commit */ |
2056 | ret = 1; | |
2057 | } else { | |
edd813bf SR |
2058 | /* Try to discard the event */ |
2059 | if (!rb_try_to_discard(cpu_buffer, event)) { | |
2060 | /* Darn, this is just wasted space */ | |
2061 | event->time_delta = 0; | |
2062 | event->array[0] = 0; | |
edd813bf | 2063 | } |
f57a8a19 | 2064 | ret = 0; |
7a8e76a3 SR |
2065 | } |
2066 | ||
bf41a158 SR |
2067 | *delta = 0; |
2068 | ||
2069 | return ret; | |
7a8e76a3 SR |
2070 | } |
2071 | ||
fa743953 SR |
2072 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
2073 | { | |
2074 | local_inc(&cpu_buffer->committing); | |
2075 | local_inc(&cpu_buffer->commits); | |
2076 | } | |
2077 | ||
2078 | static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) | |
2079 | { | |
2080 | unsigned long commits; | |
2081 | ||
2082 | if (RB_WARN_ON(cpu_buffer, | |
2083 | !local_read(&cpu_buffer->committing))) | |
2084 | return; | |
2085 | ||
2086 | again: | |
2087 | commits = local_read(&cpu_buffer->commits); | |
2088 | /* synchronize with interrupts */ | |
2089 | barrier(); | |
2090 | if (local_read(&cpu_buffer->committing) == 1) | |
2091 | rb_set_commit_to_write(cpu_buffer); | |
2092 | ||
2093 | local_dec(&cpu_buffer->committing); | |
2094 | ||
2095 | /* synchronize with interrupts */ | |
2096 | barrier(); | |
2097 | ||
2098 | /* | |
2099 | * Need to account for interrupts coming in between the | |
2100 | * updating of the commit page and the clearing of the | |
2101 | * committing counter. | |
2102 | */ | |
2103 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && | |
2104 | !local_read(&cpu_buffer->committing)) { | |
2105 | local_inc(&cpu_buffer->committing); | |
2106 | goto again; | |
2107 | } | |
2108 | } | |
2109 | ||
7a8e76a3 | 2110 | static struct ring_buffer_event * |
62f0b3eb SR |
2111 | rb_reserve_next_event(struct ring_buffer *buffer, |
2112 | struct ring_buffer_per_cpu *cpu_buffer, | |
1cd8d735 | 2113 | unsigned long length) |
7a8e76a3 SR |
2114 | { |
2115 | struct ring_buffer_event *event; | |
168b6b1d | 2116 | u64 ts, delta = 0; |
bf41a158 | 2117 | int commit = 0; |
818e3dd3 | 2118 | int nr_loops = 0; |
7a8e76a3 | 2119 | |
fa743953 SR |
2120 | rb_start_commit(cpu_buffer); |
2121 | ||
85bac32c | 2122 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
62f0b3eb SR |
2123 | /* |
2124 | * Due to the ability to swap a cpu buffer from a buffer | |
2125 | * it is possible it was swapped before we committed. | |
2126 | * (committing stops a swap). We check for it here and | |
2127 | * if it happened, we have to fail the write. | |
2128 | */ | |
2129 | barrier(); | |
2130 | if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) { | |
2131 | local_dec(&cpu_buffer->committing); | |
2132 | local_dec(&cpu_buffer->commits); | |
2133 | return NULL; | |
2134 | } | |
85bac32c | 2135 | #endif |
62f0b3eb | 2136 | |
be957c44 | 2137 | length = rb_calculate_event_length(length); |
bf41a158 | 2138 | again: |
818e3dd3 SR |
2139 | /* |
2140 | * We allow for interrupts to reenter here and do a trace. | |
2141 | * If one does, it will cause this original code to loop | |
2142 | * back here. Even with heavy interrupts happening, this | |
2143 | * should only happen a few times in a row. If this happens | |
2144 | * 1000 times in a row, there must be either an interrupt | |
2145 | * storm or we have something buggy. | |
2146 | * Bail! | |
2147 | */ | |
3e89c7bb | 2148 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
fa743953 | 2149 | goto out_fail; |
818e3dd3 | 2150 | |
6d3f1e12 | 2151 | ts = rb_time_stamp(cpu_buffer->buffer); |
7a8e76a3 | 2152 | |
bf41a158 SR |
2153 | /* |
2154 | * Only the first commit can update the timestamp. | |
2155 | * Yes there is a race here. If an interrupt comes in | |
2156 | * just after the conditional and it traces too, then it | |
2157 | * will also check the deltas. More than one timestamp may | |
2158 | * also be made. But only the entry that did the actual | |
2159 | * commit will be something other than zero. | |
2160 | */ | |
0f0c85fc SR |
2161 | if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page && |
2162 | rb_page_write(cpu_buffer->tail_page) == | |
2163 | rb_commit_index(cpu_buffer))) { | |
168b6b1d | 2164 | u64 diff; |
bf41a158 | 2165 | |
168b6b1d | 2166 | diff = ts - cpu_buffer->write_stamp; |
7a8e76a3 | 2167 | |
168b6b1d | 2168 | /* make sure this diff is calculated here */ |
bf41a158 SR |
2169 | barrier(); |
2170 | ||
2171 | /* Did the write stamp get updated already? */ | |
2172 | if (unlikely(ts < cpu_buffer->write_stamp)) | |
168b6b1d | 2173 | goto get_event; |
bf41a158 | 2174 | |
168b6b1d SR |
2175 | delta = diff; |
2176 | if (unlikely(test_time_stamp(delta))) { | |
7a8e76a3 | 2177 | |
e8bc43e8 | 2178 | commit = rb_add_time_stamp(cpu_buffer, ts, &delta); |
bf41a158 | 2179 | if (commit == -EBUSY) |
fa743953 | 2180 | goto out_fail; |
bf41a158 SR |
2181 | |
2182 | if (commit == -EAGAIN) | |
2183 | goto again; | |
2184 | ||
2185 | RB_WARN_ON(cpu_buffer, commit < 0); | |
7a8e76a3 | 2186 | } |
168b6b1d | 2187 | } |
7a8e76a3 | 2188 | |
168b6b1d | 2189 | get_event: |
e8bc43e8 | 2190 | event = __rb_reserve_next(cpu_buffer, 0, length, ts); |
168b6b1d | 2191 | if (unlikely(PTR_ERR(event) == -EAGAIN)) |
bf41a158 SR |
2192 | goto again; |
2193 | ||
fa743953 SR |
2194 | if (!event) |
2195 | goto out_fail; | |
7a8e76a3 | 2196 | |
fa743953 | 2197 | if (!rb_event_is_commit(cpu_buffer, event)) |
7a8e76a3 SR |
2198 | delta = 0; |
2199 | ||
2200 | event->time_delta = delta; | |
2201 | ||
2202 | return event; | |
fa743953 SR |
2203 | |
2204 | out_fail: | |
2205 | rb_end_commit(cpu_buffer); | |
2206 | return NULL; | |
7a8e76a3 SR |
2207 | } |
2208 | ||
1155de47 PM |
2209 | #ifdef CONFIG_TRACING |
2210 | ||
aa18efb2 | 2211 | #define TRACE_RECURSIVE_DEPTH 16 |
261842b7 SR |
2212 | |
2213 | static int trace_recursive_lock(void) | |
2214 | { | |
aa18efb2 | 2215 | current->trace_recursion++; |
261842b7 | 2216 | |
aa18efb2 SR |
2217 | if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH)) |
2218 | return 0; | |
e057a5e5 | 2219 | |
aa18efb2 SR |
2220 | /* Disable all tracing before we do anything else */ |
2221 | tracing_off_permanent(); | |
261842b7 | 2222 | |
7d7d2b80 | 2223 | printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:" |
aa18efb2 SR |
2224 | "HC[%lu]:SC[%lu]:NMI[%lu]\n", |
2225 | current->trace_recursion, | |
2226 | hardirq_count() >> HARDIRQ_SHIFT, | |
2227 | softirq_count() >> SOFTIRQ_SHIFT, | |
2228 | in_nmi()); | |
261842b7 | 2229 | |
aa18efb2 SR |
2230 | WARN_ON_ONCE(1); |
2231 | return -1; | |
261842b7 SR |
2232 | } |
2233 | ||
2234 | static void trace_recursive_unlock(void) | |
2235 | { | |
aa18efb2 | 2236 | WARN_ON_ONCE(!current->trace_recursion); |
261842b7 | 2237 | |
aa18efb2 | 2238 | current->trace_recursion--; |
261842b7 SR |
2239 | } |
2240 | ||
1155de47 PM |
2241 | #else |
2242 | ||
2243 | #define trace_recursive_lock() (0) | |
2244 | #define trace_recursive_unlock() do { } while (0) | |
2245 | ||
2246 | #endif | |
2247 | ||
7a8e76a3 SR |
2248 | /** |
2249 | * ring_buffer_lock_reserve - reserve a part of the buffer | |
2250 | * @buffer: the ring buffer to reserve from | |
2251 | * @length: the length of the data to reserve (excluding event header) | |
7a8e76a3 SR |
2252 | * |
2253 | * Returns a reseverd event on the ring buffer to copy directly to. | |
2254 | * The user of this interface will need to get the body to write into | |
2255 | * and can use the ring_buffer_event_data() interface. | |
2256 | * | |
2257 | * The length is the length of the data needed, not the event length | |
2258 | * which also includes the event header. | |
2259 | * | |
2260 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. | |
2261 | * If NULL is returned, then nothing has been allocated or locked. | |
2262 | */ | |
2263 | struct ring_buffer_event * | |
0a987751 | 2264 | ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) |
7a8e76a3 SR |
2265 | { |
2266 | struct ring_buffer_per_cpu *cpu_buffer; | |
2267 | struct ring_buffer_event *event; | |
5168ae50 | 2268 | int cpu; |
7a8e76a3 | 2269 | |
033601a3 | 2270 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2271 | return NULL; |
2272 | ||
bf41a158 | 2273 | /* If we are tracing schedule, we don't want to recurse */ |
5168ae50 | 2274 | preempt_disable_notrace(); |
bf41a158 | 2275 | |
52fbe9cd LJ |
2276 | if (atomic_read(&buffer->record_disabled)) |
2277 | goto out_nocheck; | |
2278 | ||
261842b7 SR |
2279 | if (trace_recursive_lock()) |
2280 | goto out_nocheck; | |
2281 | ||
7a8e76a3 SR |
2282 | cpu = raw_smp_processor_id(); |
2283 | ||
9e01c1b7 | 2284 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2285 | goto out; |
7a8e76a3 SR |
2286 | |
2287 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2288 | |
2289 | if (atomic_read(&cpu_buffer->record_disabled)) | |
d769041f | 2290 | goto out; |
7a8e76a3 | 2291 | |
be957c44 | 2292 | if (length > BUF_MAX_DATA_SIZE) |
bf41a158 | 2293 | goto out; |
7a8e76a3 | 2294 | |
62f0b3eb | 2295 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 | 2296 | if (!event) |
d769041f | 2297 | goto out; |
7a8e76a3 SR |
2298 | |
2299 | return event; | |
2300 | ||
d769041f | 2301 | out: |
261842b7 SR |
2302 | trace_recursive_unlock(); |
2303 | ||
2304 | out_nocheck: | |
5168ae50 | 2305 | preempt_enable_notrace(); |
7a8e76a3 SR |
2306 | return NULL; |
2307 | } | |
c4f50183 | 2308 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
7a8e76a3 | 2309 | |
a1863c21 SR |
2310 | static void |
2311 | rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
7a8e76a3 SR |
2312 | struct ring_buffer_event *event) |
2313 | { | |
fa743953 SR |
2314 | /* |
2315 | * The event first in the commit queue updates the | |
2316 | * time stamp. | |
2317 | */ | |
2318 | if (rb_event_is_commit(cpu_buffer, event)) | |
2319 | cpu_buffer->write_stamp += event->time_delta; | |
a1863c21 | 2320 | } |
bf41a158 | 2321 | |
a1863c21 SR |
2322 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
2323 | struct ring_buffer_event *event) | |
2324 | { | |
2325 | local_inc(&cpu_buffer->entries); | |
2326 | rb_update_write_stamp(cpu_buffer, event); | |
fa743953 | 2327 | rb_end_commit(cpu_buffer); |
7a8e76a3 SR |
2328 | } |
2329 | ||
2330 | /** | |
2331 | * ring_buffer_unlock_commit - commit a reserved | |
2332 | * @buffer: The buffer to commit to | |
2333 | * @event: The event pointer to commit. | |
7a8e76a3 SR |
2334 | * |
2335 | * This commits the data to the ring buffer, and releases any locks held. | |
2336 | * | |
2337 | * Must be paired with ring_buffer_lock_reserve. | |
2338 | */ | |
2339 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, | |
0a987751 | 2340 | struct ring_buffer_event *event) |
7a8e76a3 SR |
2341 | { |
2342 | struct ring_buffer_per_cpu *cpu_buffer; | |
2343 | int cpu = raw_smp_processor_id(); | |
2344 | ||
2345 | cpu_buffer = buffer->buffers[cpu]; | |
2346 | ||
7a8e76a3 SR |
2347 | rb_commit(cpu_buffer, event); |
2348 | ||
261842b7 SR |
2349 | trace_recursive_unlock(); |
2350 | ||
5168ae50 | 2351 | preempt_enable_notrace(); |
7a8e76a3 SR |
2352 | |
2353 | return 0; | |
2354 | } | |
c4f50183 | 2355 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
7a8e76a3 | 2356 | |
f3b9aae1 FW |
2357 | static inline void rb_event_discard(struct ring_buffer_event *event) |
2358 | { | |
334d4169 LJ |
2359 | /* array[0] holds the actual length for the discarded event */ |
2360 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; | |
2361 | event->type_len = RINGBUF_TYPE_PADDING; | |
f3b9aae1 FW |
2362 | /* time delta must be non zero */ |
2363 | if (!event->time_delta) | |
2364 | event->time_delta = 1; | |
2365 | } | |
2366 | ||
a1863c21 SR |
2367 | /* |
2368 | * Decrement the entries to the page that an event is on. | |
2369 | * The event does not even need to exist, only the pointer | |
2370 | * to the page it is on. This may only be called before the commit | |
2371 | * takes place. | |
2372 | */ | |
2373 | static inline void | |
2374 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, | |
2375 | struct ring_buffer_event *event) | |
2376 | { | |
2377 | unsigned long addr = (unsigned long)event; | |
2378 | struct buffer_page *bpage = cpu_buffer->commit_page; | |
2379 | struct buffer_page *start; | |
2380 | ||
2381 | addr &= PAGE_MASK; | |
2382 | ||
2383 | /* Do the likely case first */ | |
2384 | if (likely(bpage->page == (void *)addr)) { | |
2385 | local_dec(&bpage->entries); | |
2386 | return; | |
2387 | } | |
2388 | ||
2389 | /* | |
2390 | * Because the commit page may be on the reader page we | |
2391 | * start with the next page and check the end loop there. | |
2392 | */ | |
2393 | rb_inc_page(cpu_buffer, &bpage); | |
2394 | start = bpage; | |
2395 | do { | |
2396 | if (bpage->page == (void *)addr) { | |
2397 | local_dec(&bpage->entries); | |
2398 | return; | |
2399 | } | |
2400 | rb_inc_page(cpu_buffer, &bpage); | |
2401 | } while (bpage != start); | |
2402 | ||
2403 | /* commit not part of this buffer?? */ | |
2404 | RB_WARN_ON(cpu_buffer, 1); | |
2405 | } | |
2406 | ||
fa1b47dd SR |
2407 | /** |
2408 | * ring_buffer_commit_discard - discard an event that has not been committed | |
2409 | * @buffer: the ring buffer | |
2410 | * @event: non committed event to discard | |
2411 | * | |
dc892f73 SR |
2412 | * Sometimes an event that is in the ring buffer needs to be ignored. |
2413 | * This function lets the user discard an event in the ring buffer | |
2414 | * and then that event will not be read later. | |
2415 | * | |
2416 | * This function only works if it is called before the the item has been | |
2417 | * committed. It will try to free the event from the ring buffer | |
fa1b47dd SR |
2418 | * if another event has not been added behind it. |
2419 | * | |
2420 | * If another event has been added behind it, it will set the event | |
2421 | * up as discarded, and perform the commit. | |
2422 | * | |
2423 | * If this function is called, do not call ring_buffer_unlock_commit on | |
2424 | * the event. | |
2425 | */ | |
2426 | void ring_buffer_discard_commit(struct ring_buffer *buffer, | |
2427 | struct ring_buffer_event *event) | |
2428 | { | |
2429 | struct ring_buffer_per_cpu *cpu_buffer; | |
fa1b47dd SR |
2430 | int cpu; |
2431 | ||
2432 | /* The event is discarded regardless */ | |
f3b9aae1 | 2433 | rb_event_discard(event); |
fa1b47dd | 2434 | |
fa743953 SR |
2435 | cpu = smp_processor_id(); |
2436 | cpu_buffer = buffer->buffers[cpu]; | |
2437 | ||
fa1b47dd SR |
2438 | /* |
2439 | * This must only be called if the event has not been | |
2440 | * committed yet. Thus we can assume that preemption | |
2441 | * is still disabled. | |
2442 | */ | |
fa743953 | 2443 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
fa1b47dd | 2444 | |
a1863c21 | 2445 | rb_decrement_entry(cpu_buffer, event); |
0f2541d2 | 2446 | if (rb_try_to_discard(cpu_buffer, event)) |
edd813bf | 2447 | goto out; |
fa1b47dd SR |
2448 | |
2449 | /* | |
2450 | * The commit is still visible by the reader, so we | |
a1863c21 | 2451 | * must still update the timestamp. |
fa1b47dd | 2452 | */ |
a1863c21 | 2453 | rb_update_write_stamp(cpu_buffer, event); |
fa1b47dd | 2454 | out: |
fa743953 | 2455 | rb_end_commit(cpu_buffer); |
fa1b47dd | 2456 | |
f3b9aae1 FW |
2457 | trace_recursive_unlock(); |
2458 | ||
5168ae50 | 2459 | preempt_enable_notrace(); |
fa1b47dd SR |
2460 | |
2461 | } | |
2462 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); | |
2463 | ||
7a8e76a3 SR |
2464 | /** |
2465 | * ring_buffer_write - write data to the buffer without reserving | |
2466 | * @buffer: The ring buffer to write to. | |
2467 | * @length: The length of the data being written (excluding the event header) | |
2468 | * @data: The data to write to the buffer. | |
2469 | * | |
2470 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as | |
2471 | * one function. If you already have the data to write to the buffer, it | |
2472 | * may be easier to simply call this function. | |
2473 | * | |
2474 | * Note, like ring_buffer_lock_reserve, the length is the length of the data | |
2475 | * and not the length of the event which would hold the header. | |
2476 | */ | |
2477 | int ring_buffer_write(struct ring_buffer *buffer, | |
2478 | unsigned long length, | |
2479 | void *data) | |
2480 | { | |
2481 | struct ring_buffer_per_cpu *cpu_buffer; | |
2482 | struct ring_buffer_event *event; | |
7a8e76a3 SR |
2483 | void *body; |
2484 | int ret = -EBUSY; | |
5168ae50 | 2485 | int cpu; |
7a8e76a3 | 2486 | |
033601a3 | 2487 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2488 | return -EBUSY; |
2489 | ||
5168ae50 | 2490 | preempt_disable_notrace(); |
bf41a158 | 2491 | |
52fbe9cd LJ |
2492 | if (atomic_read(&buffer->record_disabled)) |
2493 | goto out; | |
2494 | ||
7a8e76a3 SR |
2495 | cpu = raw_smp_processor_id(); |
2496 | ||
9e01c1b7 | 2497 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2498 | goto out; |
7a8e76a3 SR |
2499 | |
2500 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2501 | |
2502 | if (atomic_read(&cpu_buffer->record_disabled)) | |
2503 | goto out; | |
2504 | ||
be957c44 SR |
2505 | if (length > BUF_MAX_DATA_SIZE) |
2506 | goto out; | |
2507 | ||
62f0b3eb | 2508 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 SR |
2509 | if (!event) |
2510 | goto out; | |
2511 | ||
2512 | body = rb_event_data(event); | |
2513 | ||
2514 | memcpy(body, data, length); | |
2515 | ||
2516 | rb_commit(cpu_buffer, event); | |
2517 | ||
2518 | ret = 0; | |
2519 | out: | |
5168ae50 | 2520 | preempt_enable_notrace(); |
7a8e76a3 SR |
2521 | |
2522 | return ret; | |
2523 | } | |
c4f50183 | 2524 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
7a8e76a3 | 2525 | |
34a148bf | 2526 | static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
bf41a158 SR |
2527 | { |
2528 | struct buffer_page *reader = cpu_buffer->reader_page; | |
77ae365e | 2529 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
bf41a158 SR |
2530 | struct buffer_page *commit = cpu_buffer->commit_page; |
2531 | ||
77ae365e SR |
2532 | /* In case of error, head will be NULL */ |
2533 | if (unlikely(!head)) | |
2534 | return 1; | |
2535 | ||
bf41a158 SR |
2536 | return reader->read == rb_page_commit(reader) && |
2537 | (commit == reader || | |
2538 | (commit == head && | |
2539 | head->read == rb_page_commit(commit))); | |
2540 | } | |
2541 | ||
7a8e76a3 SR |
2542 | /** |
2543 | * ring_buffer_record_disable - stop all writes into the buffer | |
2544 | * @buffer: The ring buffer to stop writes to. | |
2545 | * | |
2546 | * This prevents all writes to the buffer. Any attempt to write | |
2547 | * to the buffer after this will fail and return NULL. | |
2548 | * | |
2549 | * The caller should call synchronize_sched() after this. | |
2550 | */ | |
2551 | void ring_buffer_record_disable(struct ring_buffer *buffer) | |
2552 | { | |
2553 | atomic_inc(&buffer->record_disabled); | |
2554 | } | |
c4f50183 | 2555 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
7a8e76a3 SR |
2556 | |
2557 | /** | |
2558 | * ring_buffer_record_enable - enable writes to the buffer | |
2559 | * @buffer: The ring buffer to enable writes | |
2560 | * | |
2561 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 2562 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
2563 | */ |
2564 | void ring_buffer_record_enable(struct ring_buffer *buffer) | |
2565 | { | |
2566 | atomic_dec(&buffer->record_disabled); | |
2567 | } | |
c4f50183 | 2568 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
7a8e76a3 SR |
2569 | |
2570 | /** | |
2571 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer | |
2572 | * @buffer: The ring buffer to stop writes to. | |
2573 | * @cpu: The CPU buffer to stop | |
2574 | * | |
2575 | * This prevents all writes to the buffer. Any attempt to write | |
2576 | * to the buffer after this will fail and return NULL. | |
2577 | * | |
2578 | * The caller should call synchronize_sched() after this. | |
2579 | */ | |
2580 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) | |
2581 | { | |
2582 | struct ring_buffer_per_cpu *cpu_buffer; | |
2583 | ||
9e01c1b7 | 2584 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2585 | return; |
7a8e76a3 SR |
2586 | |
2587 | cpu_buffer = buffer->buffers[cpu]; | |
2588 | atomic_inc(&cpu_buffer->record_disabled); | |
2589 | } | |
c4f50183 | 2590 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
7a8e76a3 SR |
2591 | |
2592 | /** | |
2593 | * ring_buffer_record_enable_cpu - enable writes to the buffer | |
2594 | * @buffer: The ring buffer to enable writes | |
2595 | * @cpu: The CPU to enable. | |
2596 | * | |
2597 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 2598 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
2599 | */ |
2600 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) | |
2601 | { | |
2602 | struct ring_buffer_per_cpu *cpu_buffer; | |
2603 | ||
9e01c1b7 | 2604 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2605 | return; |
7a8e76a3 SR |
2606 | |
2607 | cpu_buffer = buffer->buffers[cpu]; | |
2608 | atomic_dec(&cpu_buffer->record_disabled); | |
2609 | } | |
c4f50183 | 2610 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
7a8e76a3 SR |
2611 | |
2612 | /** | |
2613 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer | |
2614 | * @buffer: The ring buffer | |
2615 | * @cpu: The per CPU buffer to get the entries from. | |
2616 | */ | |
2617 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) | |
2618 | { | |
2619 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 2620 | unsigned long ret; |
7a8e76a3 | 2621 | |
9e01c1b7 | 2622 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2623 | return 0; |
7a8e76a3 SR |
2624 | |
2625 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2626 | ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun)) |
e4906eff | 2627 | - cpu_buffer->read; |
554f786e SR |
2628 | |
2629 | return ret; | |
7a8e76a3 | 2630 | } |
c4f50183 | 2631 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
7a8e76a3 SR |
2632 | |
2633 | /** | |
2634 | * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer | |
2635 | * @buffer: The ring buffer | |
2636 | * @cpu: The per CPU buffer to get the number of overruns from | |
2637 | */ | |
2638 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
2639 | { | |
2640 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 2641 | unsigned long ret; |
7a8e76a3 | 2642 | |
9e01c1b7 | 2643 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2644 | return 0; |
7a8e76a3 SR |
2645 | |
2646 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2647 | ret = local_read(&cpu_buffer->overrun); |
554f786e SR |
2648 | |
2649 | return ret; | |
7a8e76a3 | 2650 | } |
c4f50183 | 2651 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
7a8e76a3 | 2652 | |
f0d2c681 SR |
2653 | /** |
2654 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits | |
2655 | * @buffer: The ring buffer | |
2656 | * @cpu: The per CPU buffer to get the number of overruns from | |
2657 | */ | |
2658 | unsigned long | |
2659 | ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
2660 | { | |
2661 | struct ring_buffer_per_cpu *cpu_buffer; | |
2662 | unsigned long ret; | |
2663 | ||
2664 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
2665 | return 0; | |
2666 | ||
2667 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2668 | ret = local_read(&cpu_buffer->commit_overrun); |
f0d2c681 SR |
2669 | |
2670 | return ret; | |
2671 | } | |
2672 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); | |
2673 | ||
7a8e76a3 SR |
2674 | /** |
2675 | * ring_buffer_entries - get the number of entries in a buffer | |
2676 | * @buffer: The ring buffer | |
2677 | * | |
2678 | * Returns the total number of entries in the ring buffer | |
2679 | * (all CPU entries) | |
2680 | */ | |
2681 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) | |
2682 | { | |
2683 | struct ring_buffer_per_cpu *cpu_buffer; | |
2684 | unsigned long entries = 0; | |
2685 | int cpu; | |
2686 | ||
2687 | /* if you care about this being correct, lock the buffer */ | |
2688 | for_each_buffer_cpu(buffer, cpu) { | |
2689 | cpu_buffer = buffer->buffers[cpu]; | |
e4906eff | 2690 | entries += (local_read(&cpu_buffer->entries) - |
77ae365e | 2691 | local_read(&cpu_buffer->overrun)) - cpu_buffer->read; |
7a8e76a3 SR |
2692 | } |
2693 | ||
2694 | return entries; | |
2695 | } | |
c4f50183 | 2696 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
7a8e76a3 SR |
2697 | |
2698 | /** | |
67b394f7 | 2699 | * ring_buffer_overruns - get the number of overruns in buffer |
7a8e76a3 SR |
2700 | * @buffer: The ring buffer |
2701 | * | |
2702 | * Returns the total number of overruns in the ring buffer | |
2703 | * (all CPU entries) | |
2704 | */ | |
2705 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) | |
2706 | { | |
2707 | struct ring_buffer_per_cpu *cpu_buffer; | |
2708 | unsigned long overruns = 0; | |
2709 | int cpu; | |
2710 | ||
2711 | /* if you care about this being correct, lock the buffer */ | |
2712 | for_each_buffer_cpu(buffer, cpu) { | |
2713 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2714 | overruns += local_read(&cpu_buffer->overrun); |
7a8e76a3 SR |
2715 | } |
2716 | ||
2717 | return overruns; | |
2718 | } | |
c4f50183 | 2719 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
7a8e76a3 | 2720 | |
642edba5 | 2721 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
7a8e76a3 SR |
2722 | { |
2723 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
2724 | ||
d769041f SR |
2725 | /* Iterator usage is expected to have record disabled */ |
2726 | if (list_empty(&cpu_buffer->reader_page->list)) { | |
77ae365e SR |
2727 | iter->head_page = rb_set_head_page(cpu_buffer); |
2728 | if (unlikely(!iter->head_page)) | |
2729 | return; | |
2730 | iter->head = iter->head_page->read; | |
d769041f SR |
2731 | } else { |
2732 | iter->head_page = cpu_buffer->reader_page; | |
6f807acd | 2733 | iter->head = cpu_buffer->reader_page->read; |
d769041f SR |
2734 | } |
2735 | if (iter->head) | |
2736 | iter->read_stamp = cpu_buffer->read_stamp; | |
2737 | else | |
abc9b56d | 2738 | iter->read_stamp = iter->head_page->page->time_stamp; |
492a74f4 SR |
2739 | iter->cache_reader_page = cpu_buffer->reader_page; |
2740 | iter->cache_read = cpu_buffer->read; | |
642edba5 | 2741 | } |
f83c9d0f | 2742 | |
642edba5 SR |
2743 | /** |
2744 | * ring_buffer_iter_reset - reset an iterator | |
2745 | * @iter: The iterator to reset | |
2746 | * | |
2747 | * Resets the iterator, so that it will start from the beginning | |
2748 | * again. | |
2749 | */ | |
2750 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) | |
2751 | { | |
554f786e | 2752 | struct ring_buffer_per_cpu *cpu_buffer; |
642edba5 SR |
2753 | unsigned long flags; |
2754 | ||
554f786e SR |
2755 | if (!iter) |
2756 | return; | |
2757 | ||
2758 | cpu_buffer = iter->cpu_buffer; | |
2759 | ||
642edba5 SR |
2760 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
2761 | rb_iter_reset(iter); | |
f83c9d0f | 2762 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 | 2763 | } |
c4f50183 | 2764 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
7a8e76a3 SR |
2765 | |
2766 | /** | |
2767 | * ring_buffer_iter_empty - check if an iterator has no more to read | |
2768 | * @iter: The iterator to check | |
2769 | */ | |
2770 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) | |
2771 | { | |
2772 | struct ring_buffer_per_cpu *cpu_buffer; | |
2773 | ||
2774 | cpu_buffer = iter->cpu_buffer; | |
2775 | ||
bf41a158 SR |
2776 | return iter->head_page == cpu_buffer->commit_page && |
2777 | iter->head == rb_commit_index(cpu_buffer); | |
7a8e76a3 | 2778 | } |
c4f50183 | 2779 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
7a8e76a3 SR |
2780 | |
2781 | static void | |
2782 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
2783 | struct ring_buffer_event *event) | |
2784 | { | |
2785 | u64 delta; | |
2786 | ||
334d4169 | 2787 | switch (event->type_len) { |
7a8e76a3 SR |
2788 | case RINGBUF_TYPE_PADDING: |
2789 | return; | |
2790 | ||
2791 | case RINGBUF_TYPE_TIME_EXTEND: | |
2792 | delta = event->array[0]; | |
2793 | delta <<= TS_SHIFT; | |
2794 | delta += event->time_delta; | |
2795 | cpu_buffer->read_stamp += delta; | |
2796 | return; | |
2797 | ||
2798 | case RINGBUF_TYPE_TIME_STAMP: | |
2799 | /* FIXME: not implemented */ | |
2800 | return; | |
2801 | ||
2802 | case RINGBUF_TYPE_DATA: | |
2803 | cpu_buffer->read_stamp += event->time_delta; | |
2804 | return; | |
2805 | ||
2806 | default: | |
2807 | BUG(); | |
2808 | } | |
2809 | return; | |
2810 | } | |
2811 | ||
2812 | static void | |
2813 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, | |
2814 | struct ring_buffer_event *event) | |
2815 | { | |
2816 | u64 delta; | |
2817 | ||
334d4169 | 2818 | switch (event->type_len) { |
7a8e76a3 SR |
2819 | case RINGBUF_TYPE_PADDING: |
2820 | return; | |
2821 | ||
2822 | case RINGBUF_TYPE_TIME_EXTEND: | |
2823 | delta = event->array[0]; | |
2824 | delta <<= TS_SHIFT; | |
2825 | delta += event->time_delta; | |
2826 | iter->read_stamp += delta; | |
2827 | return; | |
2828 | ||
2829 | case RINGBUF_TYPE_TIME_STAMP: | |
2830 | /* FIXME: not implemented */ | |
2831 | return; | |
2832 | ||
2833 | case RINGBUF_TYPE_DATA: | |
2834 | iter->read_stamp += event->time_delta; | |
2835 | return; | |
2836 | ||
2837 | default: | |
2838 | BUG(); | |
2839 | } | |
2840 | return; | |
2841 | } | |
2842 | ||
d769041f SR |
2843 | static struct buffer_page * |
2844 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) | |
7a8e76a3 | 2845 | { |
d769041f | 2846 | struct buffer_page *reader = NULL; |
66a8cb95 | 2847 | unsigned long overwrite; |
d769041f | 2848 | unsigned long flags; |
818e3dd3 | 2849 | int nr_loops = 0; |
77ae365e | 2850 | int ret; |
d769041f | 2851 | |
3e03fb7f | 2852 | local_irq_save(flags); |
0199c4e6 | 2853 | arch_spin_lock(&cpu_buffer->lock); |
d769041f SR |
2854 | |
2855 | again: | |
818e3dd3 SR |
2856 | /* |
2857 | * This should normally only loop twice. But because the | |
2858 | * start of the reader inserts an empty page, it causes | |
2859 | * a case where we will loop three times. There should be no | |
2860 | * reason to loop four times (that I know of). | |
2861 | */ | |
3e89c7bb | 2862 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
818e3dd3 SR |
2863 | reader = NULL; |
2864 | goto out; | |
2865 | } | |
2866 | ||
d769041f SR |
2867 | reader = cpu_buffer->reader_page; |
2868 | ||
2869 | /* If there's more to read, return this page */ | |
bf41a158 | 2870 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
d769041f SR |
2871 | goto out; |
2872 | ||
2873 | /* Never should we have an index greater than the size */ | |
3e89c7bb SR |
2874 | if (RB_WARN_ON(cpu_buffer, |
2875 | cpu_buffer->reader_page->read > rb_page_size(reader))) | |
2876 | goto out; | |
d769041f SR |
2877 | |
2878 | /* check if we caught up to the tail */ | |
2879 | reader = NULL; | |
bf41a158 | 2880 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
d769041f | 2881 | goto out; |
7a8e76a3 SR |
2882 | |
2883 | /* | |
d769041f | 2884 | * Reset the reader page to size zero. |
7a8e76a3 | 2885 | */ |
77ae365e SR |
2886 | local_set(&cpu_buffer->reader_page->write, 0); |
2887 | local_set(&cpu_buffer->reader_page->entries, 0); | |
2888 | local_set(&cpu_buffer->reader_page->page->commit, 0); | |
ff0ff84a | 2889 | cpu_buffer->reader_page->real_end = 0; |
7a8e76a3 | 2890 | |
77ae365e SR |
2891 | spin: |
2892 | /* | |
2893 | * Splice the empty reader page into the list around the head. | |
2894 | */ | |
2895 | reader = rb_set_head_page(cpu_buffer); | |
0e1ff5d7 | 2896 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
d769041f | 2897 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
bf41a158 | 2898 | |
3adc54fa SR |
2899 | /* |
2900 | * cpu_buffer->pages just needs to point to the buffer, it | |
2901 | * has no specific buffer page to point to. Lets move it out | |
2902 | * of our way so we don't accidently swap it. | |
2903 | */ | |
2904 | cpu_buffer->pages = reader->list.prev; | |
2905 | ||
77ae365e SR |
2906 | /* The reader page will be pointing to the new head */ |
2907 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); | |
7a8e76a3 | 2908 | |
66a8cb95 SR |
2909 | /* |
2910 | * We want to make sure we read the overruns after we set up our | |
2911 | * pointers to the next object. The writer side does a | |
2912 | * cmpxchg to cross pages which acts as the mb on the writer | |
2913 | * side. Note, the reader will constantly fail the swap | |
2914 | * while the writer is updating the pointers, so this | |
2915 | * guarantees that the overwrite recorded here is the one we | |
2916 | * want to compare with the last_overrun. | |
2917 | */ | |
2918 | smp_mb(); | |
2919 | overwrite = local_read(&(cpu_buffer->overrun)); | |
2920 | ||
77ae365e SR |
2921 | /* |
2922 | * Here's the tricky part. | |
2923 | * | |
2924 | * We need to move the pointer past the header page. | |
2925 | * But we can only do that if a writer is not currently | |
2926 | * moving it. The page before the header page has the | |
2927 | * flag bit '1' set if it is pointing to the page we want. | |
2928 | * but if the writer is in the process of moving it | |
2929 | * than it will be '2' or already moved '0'. | |
2930 | */ | |
2931 | ||
2932 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); | |
7a8e76a3 SR |
2933 | |
2934 | /* | |
77ae365e | 2935 | * If we did not convert it, then we must try again. |
7a8e76a3 | 2936 | */ |
77ae365e SR |
2937 | if (!ret) |
2938 | goto spin; | |
7a8e76a3 | 2939 | |
77ae365e SR |
2940 | /* |
2941 | * Yeah! We succeeded in replacing the page. | |
2942 | * | |
2943 | * Now make the new head point back to the reader page. | |
2944 | */ | |
5ded3dc6 | 2945 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
77ae365e | 2946 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
d769041f SR |
2947 | |
2948 | /* Finally update the reader page to the new head */ | |
2949 | cpu_buffer->reader_page = reader; | |
2950 | rb_reset_reader_page(cpu_buffer); | |
2951 | ||
66a8cb95 SR |
2952 | if (overwrite != cpu_buffer->last_overrun) { |
2953 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; | |
2954 | cpu_buffer->last_overrun = overwrite; | |
2955 | } | |
2956 | ||
d769041f SR |
2957 | goto again; |
2958 | ||
2959 | out: | |
0199c4e6 | 2960 | arch_spin_unlock(&cpu_buffer->lock); |
3e03fb7f | 2961 | local_irq_restore(flags); |
d769041f SR |
2962 | |
2963 | return reader; | |
2964 | } | |
2965 | ||
2966 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) | |
2967 | { | |
2968 | struct ring_buffer_event *event; | |
2969 | struct buffer_page *reader; | |
2970 | unsigned length; | |
2971 | ||
2972 | reader = rb_get_reader_page(cpu_buffer); | |
7a8e76a3 | 2973 | |
d769041f | 2974 | /* This function should not be called when buffer is empty */ |
3e89c7bb SR |
2975 | if (RB_WARN_ON(cpu_buffer, !reader)) |
2976 | return; | |
7a8e76a3 | 2977 | |
d769041f SR |
2978 | event = rb_reader_event(cpu_buffer); |
2979 | ||
a1863c21 | 2980 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
e4906eff | 2981 | cpu_buffer->read++; |
d769041f SR |
2982 | |
2983 | rb_update_read_stamp(cpu_buffer, event); | |
2984 | ||
2985 | length = rb_event_length(event); | |
6f807acd | 2986 | cpu_buffer->reader_page->read += length; |
7a8e76a3 SR |
2987 | } |
2988 | ||
2989 | static void rb_advance_iter(struct ring_buffer_iter *iter) | |
2990 | { | |
7a8e76a3 SR |
2991 | struct ring_buffer_per_cpu *cpu_buffer; |
2992 | struct ring_buffer_event *event; | |
2993 | unsigned length; | |
2994 | ||
2995 | cpu_buffer = iter->cpu_buffer; | |
7a8e76a3 SR |
2996 | |
2997 | /* | |
2998 | * Check if we are at the end of the buffer. | |
2999 | */ | |
bf41a158 | 3000 | if (iter->head >= rb_page_size(iter->head_page)) { |
ea05b57c SR |
3001 | /* discarded commits can make the page empty */ |
3002 | if (iter->head_page == cpu_buffer->commit_page) | |
3e89c7bb | 3003 | return; |
d769041f | 3004 | rb_inc_iter(iter); |
7a8e76a3 SR |
3005 | return; |
3006 | } | |
3007 | ||
3008 | event = rb_iter_head_event(iter); | |
3009 | ||
3010 | length = rb_event_length(event); | |
3011 | ||
3012 | /* | |
3013 | * This should not be called to advance the header if we are | |
3014 | * at the tail of the buffer. | |
3015 | */ | |
3e89c7bb | 3016 | if (RB_WARN_ON(cpu_buffer, |
f536aafc | 3017 | (iter->head_page == cpu_buffer->commit_page) && |
3e89c7bb SR |
3018 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
3019 | return; | |
7a8e76a3 SR |
3020 | |
3021 | rb_update_iter_read_stamp(iter, event); | |
3022 | ||
3023 | iter->head += length; | |
3024 | ||
3025 | /* check for end of page padding */ | |
bf41a158 SR |
3026 | if ((iter->head >= rb_page_size(iter->head_page)) && |
3027 | (iter->head_page != cpu_buffer->commit_page)) | |
7a8e76a3 SR |
3028 | rb_advance_iter(iter); |
3029 | } | |
3030 | ||
66a8cb95 SR |
3031 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
3032 | { | |
3033 | return cpu_buffer->lost_events; | |
3034 | } | |
3035 | ||
f83c9d0f | 3036 | static struct ring_buffer_event * |
66a8cb95 SR |
3037 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
3038 | unsigned long *lost_events) | |
7a8e76a3 | 3039 | { |
7a8e76a3 | 3040 | struct ring_buffer_event *event; |
d769041f | 3041 | struct buffer_page *reader; |
818e3dd3 | 3042 | int nr_loops = 0; |
7a8e76a3 | 3043 | |
7a8e76a3 | 3044 | again: |
818e3dd3 SR |
3045 | /* |
3046 | * We repeat when a timestamp is encountered. It is possible | |
3047 | * to get multiple timestamps from an interrupt entering just | |
ea05b57c SR |
3048 | * as one timestamp is about to be written, or from discarded |
3049 | * commits. The most that we can have is the number on a single page. | |
818e3dd3 | 3050 | */ |
ea05b57c | 3051 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE)) |
818e3dd3 | 3052 | return NULL; |
818e3dd3 | 3053 | |
d769041f SR |
3054 | reader = rb_get_reader_page(cpu_buffer); |
3055 | if (!reader) | |
7a8e76a3 SR |
3056 | return NULL; |
3057 | ||
d769041f | 3058 | event = rb_reader_event(cpu_buffer); |
7a8e76a3 | 3059 | |
334d4169 | 3060 | switch (event->type_len) { |
7a8e76a3 | 3061 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
3062 | if (rb_null_event(event)) |
3063 | RB_WARN_ON(cpu_buffer, 1); | |
3064 | /* | |
3065 | * Because the writer could be discarding every | |
3066 | * event it creates (which would probably be bad) | |
3067 | * if we were to go back to "again" then we may never | |
3068 | * catch up, and will trigger the warn on, or lock | |
3069 | * the box. Return the padding, and we will release | |
3070 | * the current locks, and try again. | |
3071 | */ | |
2d622719 | 3072 | return event; |
7a8e76a3 SR |
3073 | |
3074 | case RINGBUF_TYPE_TIME_EXTEND: | |
3075 | /* Internal data, OK to advance */ | |
d769041f | 3076 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3077 | goto again; |
3078 | ||
3079 | case RINGBUF_TYPE_TIME_STAMP: | |
3080 | /* FIXME: not implemented */ | |
d769041f | 3081 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3082 | goto again; |
3083 | ||
3084 | case RINGBUF_TYPE_DATA: | |
3085 | if (ts) { | |
3086 | *ts = cpu_buffer->read_stamp + event->time_delta; | |
d8eeb2d3 | 3087 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
37886f6a | 3088 | cpu_buffer->cpu, ts); |
7a8e76a3 | 3089 | } |
66a8cb95 SR |
3090 | if (lost_events) |
3091 | *lost_events = rb_lost_events(cpu_buffer); | |
7a8e76a3 SR |
3092 | return event; |
3093 | ||
3094 | default: | |
3095 | BUG(); | |
3096 | } | |
3097 | ||
3098 | return NULL; | |
3099 | } | |
c4f50183 | 3100 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
7a8e76a3 | 3101 | |
f83c9d0f SR |
3102 | static struct ring_buffer_event * |
3103 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |
7a8e76a3 SR |
3104 | { |
3105 | struct ring_buffer *buffer; | |
3106 | struct ring_buffer_per_cpu *cpu_buffer; | |
3107 | struct ring_buffer_event *event; | |
818e3dd3 | 3108 | int nr_loops = 0; |
7a8e76a3 | 3109 | |
7a8e76a3 SR |
3110 | cpu_buffer = iter->cpu_buffer; |
3111 | buffer = cpu_buffer->buffer; | |
3112 | ||
492a74f4 SR |
3113 | /* |
3114 | * Check if someone performed a consuming read to | |
3115 | * the buffer. A consuming read invalidates the iterator | |
3116 | * and we need to reset the iterator in this case. | |
3117 | */ | |
3118 | if (unlikely(iter->cache_read != cpu_buffer->read || | |
3119 | iter->cache_reader_page != cpu_buffer->reader_page)) | |
3120 | rb_iter_reset(iter); | |
3121 | ||
7a8e76a3 | 3122 | again: |
3c05d748 SR |
3123 | if (ring_buffer_iter_empty(iter)) |
3124 | return NULL; | |
3125 | ||
818e3dd3 | 3126 | /* |
ea05b57c SR |
3127 | * We repeat when a timestamp is encountered. |
3128 | * We can get multiple timestamps by nested interrupts or also | |
3129 | * if filtering is on (discarding commits). Since discarding | |
3130 | * commits can be frequent we can get a lot of timestamps. | |
3131 | * But we limit them by not adding timestamps if they begin | |
3132 | * at the start of a page. | |
818e3dd3 | 3133 | */ |
ea05b57c | 3134 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE)) |
818e3dd3 | 3135 | return NULL; |
818e3dd3 | 3136 | |
7a8e76a3 SR |
3137 | if (rb_per_cpu_empty(cpu_buffer)) |
3138 | return NULL; | |
3139 | ||
3c05d748 SR |
3140 | if (iter->head >= local_read(&iter->head_page->page->commit)) { |
3141 | rb_inc_iter(iter); | |
3142 | goto again; | |
3143 | } | |
3144 | ||
7a8e76a3 SR |
3145 | event = rb_iter_head_event(iter); |
3146 | ||
334d4169 | 3147 | switch (event->type_len) { |
7a8e76a3 | 3148 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
3149 | if (rb_null_event(event)) { |
3150 | rb_inc_iter(iter); | |
3151 | goto again; | |
3152 | } | |
3153 | rb_advance_iter(iter); | |
3154 | return event; | |
7a8e76a3 SR |
3155 | |
3156 | case RINGBUF_TYPE_TIME_EXTEND: | |
3157 | /* Internal data, OK to advance */ | |
3158 | rb_advance_iter(iter); | |
3159 | goto again; | |
3160 | ||
3161 | case RINGBUF_TYPE_TIME_STAMP: | |
3162 | /* FIXME: not implemented */ | |
3163 | rb_advance_iter(iter); | |
3164 | goto again; | |
3165 | ||
3166 | case RINGBUF_TYPE_DATA: | |
3167 | if (ts) { | |
3168 | *ts = iter->read_stamp + event->time_delta; | |
37886f6a SR |
3169 | ring_buffer_normalize_time_stamp(buffer, |
3170 | cpu_buffer->cpu, ts); | |
7a8e76a3 SR |
3171 | } |
3172 | return event; | |
3173 | ||
3174 | default: | |
3175 | BUG(); | |
3176 | } | |
3177 | ||
3178 | return NULL; | |
3179 | } | |
c4f50183 | 3180 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
7a8e76a3 | 3181 | |
8d707e8e SR |
3182 | static inline int rb_ok_to_lock(void) |
3183 | { | |
3184 | /* | |
3185 | * If an NMI die dumps out the content of the ring buffer | |
3186 | * do not grab locks. We also permanently disable the ring | |
3187 | * buffer too. A one time deal is all you get from reading | |
3188 | * the ring buffer from an NMI. | |
3189 | */ | |
464e85eb | 3190 | if (likely(!in_nmi())) |
8d707e8e SR |
3191 | return 1; |
3192 | ||
3193 | tracing_off_permanent(); | |
3194 | return 0; | |
3195 | } | |
3196 | ||
f83c9d0f SR |
3197 | /** |
3198 | * ring_buffer_peek - peek at the next event to be read | |
3199 | * @buffer: The ring buffer to read | |
3200 | * @cpu: The cpu to peak at | |
3201 | * @ts: The timestamp counter of this event. | |
66a8cb95 | 3202 | * @lost_events: a variable to store if events were lost (may be NULL) |
f83c9d0f SR |
3203 | * |
3204 | * This will return the event that will be read next, but does | |
3205 | * not consume the data. | |
3206 | */ | |
3207 | struct ring_buffer_event * | |
66a8cb95 SR |
3208 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, |
3209 | unsigned long *lost_events) | |
f83c9d0f SR |
3210 | { |
3211 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
8aabee57 | 3212 | struct ring_buffer_event *event; |
f83c9d0f | 3213 | unsigned long flags; |
8d707e8e | 3214 | int dolock; |
f83c9d0f | 3215 | |
554f786e | 3216 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3217 | return NULL; |
554f786e | 3218 | |
8d707e8e | 3219 | dolock = rb_ok_to_lock(); |
2d622719 | 3220 | again: |
8d707e8e SR |
3221 | local_irq_save(flags); |
3222 | if (dolock) | |
3223 | spin_lock(&cpu_buffer->reader_lock); | |
66a8cb95 | 3224 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
469535a5 RR |
3225 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
3226 | rb_advance_reader(cpu_buffer); | |
8d707e8e SR |
3227 | if (dolock) |
3228 | spin_unlock(&cpu_buffer->reader_lock); | |
3229 | local_irq_restore(flags); | |
f83c9d0f | 3230 | |
1b959e18 | 3231 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3232 | goto again; |
2d622719 | 3233 | |
f83c9d0f SR |
3234 | return event; |
3235 | } | |
3236 | ||
3237 | /** | |
3238 | * ring_buffer_iter_peek - peek at the next event to be read | |
3239 | * @iter: The ring buffer iterator | |
3240 | * @ts: The timestamp counter of this event. | |
3241 | * | |
3242 | * This will return the event that will be read next, but does | |
3243 | * not increment the iterator. | |
3244 | */ | |
3245 | struct ring_buffer_event * | |
3246 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |
3247 | { | |
3248 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
3249 | struct ring_buffer_event *event; | |
3250 | unsigned long flags; | |
3251 | ||
2d622719 | 3252 | again: |
f83c9d0f SR |
3253 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
3254 | event = rb_iter_peek(iter, ts); | |
3255 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | |
3256 | ||
1b959e18 | 3257 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3258 | goto again; |
2d622719 | 3259 | |
f83c9d0f SR |
3260 | return event; |
3261 | } | |
3262 | ||
7a8e76a3 SR |
3263 | /** |
3264 | * ring_buffer_consume - return an event and consume it | |
3265 | * @buffer: The ring buffer to get the next event from | |
66a8cb95 SR |
3266 | * @cpu: the cpu to read the buffer from |
3267 | * @ts: a variable to store the timestamp (may be NULL) | |
3268 | * @lost_events: a variable to store if events were lost (may be NULL) | |
7a8e76a3 SR |
3269 | * |
3270 | * Returns the next event in the ring buffer, and that event is consumed. | |
3271 | * Meaning, that sequential reads will keep returning a different event, | |
3272 | * and eventually empty the ring buffer if the producer is slower. | |
3273 | */ | |
3274 | struct ring_buffer_event * | |
66a8cb95 SR |
3275 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, |
3276 | unsigned long *lost_events) | |
7a8e76a3 | 3277 | { |
554f786e SR |
3278 | struct ring_buffer_per_cpu *cpu_buffer; |
3279 | struct ring_buffer_event *event = NULL; | |
f83c9d0f | 3280 | unsigned long flags; |
8d707e8e SR |
3281 | int dolock; |
3282 | ||
3283 | dolock = rb_ok_to_lock(); | |
7a8e76a3 | 3284 | |
2d622719 | 3285 | again: |
554f786e SR |
3286 | /* might be called in atomic */ |
3287 | preempt_disable(); | |
3288 | ||
9e01c1b7 | 3289 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
554f786e | 3290 | goto out; |
7a8e76a3 | 3291 | |
554f786e | 3292 | cpu_buffer = buffer->buffers[cpu]; |
8d707e8e SR |
3293 | local_irq_save(flags); |
3294 | if (dolock) | |
3295 | spin_lock(&cpu_buffer->reader_lock); | |
f83c9d0f | 3296 | |
66a8cb95 SR |
3297 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
3298 | if (event) { | |
3299 | cpu_buffer->lost_events = 0; | |
469535a5 | 3300 | rb_advance_reader(cpu_buffer); |
66a8cb95 | 3301 | } |
7a8e76a3 | 3302 | |
8d707e8e SR |
3303 | if (dolock) |
3304 | spin_unlock(&cpu_buffer->reader_lock); | |
3305 | local_irq_restore(flags); | |
f83c9d0f | 3306 | |
554f786e SR |
3307 | out: |
3308 | preempt_enable(); | |
3309 | ||
1b959e18 | 3310 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3311 | goto again; |
2d622719 | 3312 | |
7a8e76a3 SR |
3313 | return event; |
3314 | } | |
c4f50183 | 3315 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
7a8e76a3 SR |
3316 | |
3317 | /** | |
72c9ddfd | 3318 | * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer |
7a8e76a3 SR |
3319 | * @buffer: The ring buffer to read from |
3320 | * @cpu: The cpu buffer to iterate over | |
3321 | * | |
72c9ddfd DM |
3322 | * This performs the initial preparations necessary to iterate |
3323 | * through the buffer. Memory is allocated, buffer recording | |
3324 | * is disabled, and the iterator pointer is returned to the caller. | |
7a8e76a3 | 3325 | * |
72c9ddfd DM |
3326 | * Disabling buffer recordng prevents the reading from being |
3327 | * corrupted. This is not a consuming read, so a producer is not | |
3328 | * expected. | |
3329 | * | |
3330 | * After a sequence of ring_buffer_read_prepare calls, the user is | |
3331 | * expected to make at least one call to ring_buffer_prepare_sync. | |
3332 | * Afterwards, ring_buffer_read_start is invoked to get things going | |
3333 | * for real. | |
3334 | * | |
3335 | * This overall must be paired with ring_buffer_finish. | |
7a8e76a3 SR |
3336 | */ |
3337 | struct ring_buffer_iter * | |
72c9ddfd | 3338 | ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu) |
7a8e76a3 SR |
3339 | { |
3340 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 3341 | struct ring_buffer_iter *iter; |
7a8e76a3 | 3342 | |
9e01c1b7 | 3343 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3344 | return NULL; |
7a8e76a3 SR |
3345 | |
3346 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); | |
3347 | if (!iter) | |
8aabee57 | 3348 | return NULL; |
7a8e76a3 SR |
3349 | |
3350 | cpu_buffer = buffer->buffers[cpu]; | |
3351 | ||
3352 | iter->cpu_buffer = cpu_buffer; | |
3353 | ||
3354 | atomic_inc(&cpu_buffer->record_disabled); | |
72c9ddfd DM |
3355 | |
3356 | return iter; | |
3357 | } | |
3358 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare); | |
3359 | ||
3360 | /** | |
3361 | * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls | |
3362 | * | |
3363 | * All previously invoked ring_buffer_read_prepare calls to prepare | |
3364 | * iterators will be synchronized. Afterwards, read_buffer_read_start | |
3365 | * calls on those iterators are allowed. | |
3366 | */ | |
3367 | void | |
3368 | ring_buffer_read_prepare_sync(void) | |
3369 | { | |
7a8e76a3 | 3370 | synchronize_sched(); |
72c9ddfd DM |
3371 | } |
3372 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync); | |
3373 | ||
3374 | /** | |
3375 | * ring_buffer_read_start - start a non consuming read of the buffer | |
3376 | * @iter: The iterator returned by ring_buffer_read_prepare | |
3377 | * | |
3378 | * This finalizes the startup of an iteration through the buffer. | |
3379 | * The iterator comes from a call to ring_buffer_read_prepare and | |
3380 | * an intervening ring_buffer_read_prepare_sync must have been | |
3381 | * performed. | |
3382 | * | |
3383 | * Must be paired with ring_buffer_finish. | |
3384 | */ | |
3385 | void | |
3386 | ring_buffer_read_start(struct ring_buffer_iter *iter) | |
3387 | { | |
3388 | struct ring_buffer_per_cpu *cpu_buffer; | |
3389 | unsigned long flags; | |
3390 | ||
3391 | if (!iter) | |
3392 | return; | |
3393 | ||
3394 | cpu_buffer = iter->cpu_buffer; | |
7a8e76a3 | 3395 | |
f83c9d0f | 3396 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
0199c4e6 | 3397 | arch_spin_lock(&cpu_buffer->lock); |
642edba5 | 3398 | rb_iter_reset(iter); |
0199c4e6 | 3399 | arch_spin_unlock(&cpu_buffer->lock); |
f83c9d0f | 3400 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 | 3401 | } |
c4f50183 | 3402 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
7a8e76a3 SR |
3403 | |
3404 | /** | |
3405 | * ring_buffer_finish - finish reading the iterator of the buffer | |
3406 | * @iter: The iterator retrieved by ring_buffer_start | |
3407 | * | |
3408 | * This re-enables the recording to the buffer, and frees the | |
3409 | * iterator. | |
3410 | */ | |
3411 | void | |
3412 | ring_buffer_read_finish(struct ring_buffer_iter *iter) | |
3413 | { | |
3414 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
3415 | ||
3416 | atomic_dec(&cpu_buffer->record_disabled); | |
3417 | kfree(iter); | |
3418 | } | |
c4f50183 | 3419 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
7a8e76a3 SR |
3420 | |
3421 | /** | |
3422 | * ring_buffer_read - read the next item in the ring buffer by the iterator | |
3423 | * @iter: The ring buffer iterator | |
3424 | * @ts: The time stamp of the event read. | |
3425 | * | |
3426 | * This reads the next event in the ring buffer and increments the iterator. | |
3427 | */ | |
3428 | struct ring_buffer_event * | |
3429 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) | |
3430 | { | |
3431 | struct ring_buffer_event *event; | |
f83c9d0f SR |
3432 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
3433 | unsigned long flags; | |
7a8e76a3 | 3434 | |
f83c9d0f | 3435 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
7e9391cf | 3436 | again: |
f83c9d0f | 3437 | event = rb_iter_peek(iter, ts); |
7a8e76a3 | 3438 | if (!event) |
f83c9d0f | 3439 | goto out; |
7a8e76a3 | 3440 | |
7e9391cf SR |
3441 | if (event->type_len == RINGBUF_TYPE_PADDING) |
3442 | goto again; | |
3443 | ||
7a8e76a3 | 3444 | rb_advance_iter(iter); |
f83c9d0f SR |
3445 | out: |
3446 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | |
7a8e76a3 SR |
3447 | |
3448 | return event; | |
3449 | } | |
c4f50183 | 3450 | EXPORT_SYMBOL_GPL(ring_buffer_read); |
7a8e76a3 SR |
3451 | |
3452 | /** | |
3453 | * ring_buffer_size - return the size of the ring buffer (in bytes) | |
3454 | * @buffer: The ring buffer. | |
3455 | */ | |
3456 | unsigned long ring_buffer_size(struct ring_buffer *buffer) | |
3457 | { | |
3458 | return BUF_PAGE_SIZE * buffer->pages; | |
3459 | } | |
c4f50183 | 3460 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
7a8e76a3 SR |
3461 | |
3462 | static void | |
3463 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) | |
3464 | { | |
77ae365e SR |
3465 | rb_head_page_deactivate(cpu_buffer); |
3466 | ||
7a8e76a3 | 3467 | cpu_buffer->head_page |
3adc54fa | 3468 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
bf41a158 | 3469 | local_set(&cpu_buffer->head_page->write, 0); |
778c55d4 | 3470 | local_set(&cpu_buffer->head_page->entries, 0); |
abc9b56d | 3471 | local_set(&cpu_buffer->head_page->page->commit, 0); |
d769041f | 3472 | |
6f807acd | 3473 | cpu_buffer->head_page->read = 0; |
bf41a158 SR |
3474 | |
3475 | cpu_buffer->tail_page = cpu_buffer->head_page; | |
3476 | cpu_buffer->commit_page = cpu_buffer->head_page; | |
3477 | ||
3478 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); | |
3479 | local_set(&cpu_buffer->reader_page->write, 0); | |
778c55d4 | 3480 | local_set(&cpu_buffer->reader_page->entries, 0); |
abc9b56d | 3481 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
6f807acd | 3482 | cpu_buffer->reader_page->read = 0; |
7a8e76a3 | 3483 | |
77ae365e SR |
3484 | local_set(&cpu_buffer->commit_overrun, 0); |
3485 | local_set(&cpu_buffer->overrun, 0); | |
e4906eff | 3486 | local_set(&cpu_buffer->entries, 0); |
fa743953 SR |
3487 | local_set(&cpu_buffer->committing, 0); |
3488 | local_set(&cpu_buffer->commits, 0); | |
77ae365e | 3489 | cpu_buffer->read = 0; |
69507c06 SR |
3490 | |
3491 | cpu_buffer->write_stamp = 0; | |
3492 | cpu_buffer->read_stamp = 0; | |
77ae365e | 3493 | |
66a8cb95 SR |
3494 | cpu_buffer->lost_events = 0; |
3495 | cpu_buffer->last_overrun = 0; | |
3496 | ||
77ae365e | 3497 | rb_head_page_activate(cpu_buffer); |
7a8e76a3 SR |
3498 | } |
3499 | ||
3500 | /** | |
3501 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer | |
3502 | * @buffer: The ring buffer to reset a per cpu buffer of | |
3503 | * @cpu: The CPU buffer to be reset | |
3504 | */ | |
3505 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) | |
3506 | { | |
3507 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
3508 | unsigned long flags; | |
3509 | ||
9e01c1b7 | 3510 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3511 | return; |
7a8e76a3 | 3512 | |
41ede23e SR |
3513 | atomic_inc(&cpu_buffer->record_disabled); |
3514 | ||
f83c9d0f SR |
3515 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
3516 | ||
41b6a95d SR |
3517 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) |
3518 | goto out; | |
3519 | ||
0199c4e6 | 3520 | arch_spin_lock(&cpu_buffer->lock); |
7a8e76a3 SR |
3521 | |
3522 | rb_reset_cpu(cpu_buffer); | |
3523 | ||
0199c4e6 | 3524 | arch_spin_unlock(&cpu_buffer->lock); |
f83c9d0f | 3525 | |
41b6a95d | 3526 | out: |
f83c9d0f | 3527 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
41ede23e SR |
3528 | |
3529 | atomic_dec(&cpu_buffer->record_disabled); | |
7a8e76a3 | 3530 | } |
c4f50183 | 3531 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
7a8e76a3 SR |
3532 | |
3533 | /** | |
3534 | * ring_buffer_reset - reset a ring buffer | |
3535 | * @buffer: The ring buffer to reset all cpu buffers | |
3536 | */ | |
3537 | void ring_buffer_reset(struct ring_buffer *buffer) | |
3538 | { | |
7a8e76a3 SR |
3539 | int cpu; |
3540 | ||
7a8e76a3 | 3541 | for_each_buffer_cpu(buffer, cpu) |
d769041f | 3542 | ring_buffer_reset_cpu(buffer, cpu); |
7a8e76a3 | 3543 | } |
c4f50183 | 3544 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
7a8e76a3 SR |
3545 | |
3546 | /** | |
3547 | * rind_buffer_empty - is the ring buffer empty? | |
3548 | * @buffer: The ring buffer to test | |
3549 | */ | |
3550 | int ring_buffer_empty(struct ring_buffer *buffer) | |
3551 | { | |
3552 | struct ring_buffer_per_cpu *cpu_buffer; | |
d4788207 | 3553 | unsigned long flags; |
8d707e8e | 3554 | int dolock; |
7a8e76a3 | 3555 | int cpu; |
d4788207 | 3556 | int ret; |
7a8e76a3 | 3557 | |
8d707e8e | 3558 | dolock = rb_ok_to_lock(); |
7a8e76a3 SR |
3559 | |
3560 | /* yes this is racy, but if you don't like the race, lock the buffer */ | |
3561 | for_each_buffer_cpu(buffer, cpu) { | |
3562 | cpu_buffer = buffer->buffers[cpu]; | |
8d707e8e SR |
3563 | local_irq_save(flags); |
3564 | if (dolock) | |
3565 | spin_lock(&cpu_buffer->reader_lock); | |
d4788207 | 3566 | ret = rb_per_cpu_empty(cpu_buffer); |
8d707e8e SR |
3567 | if (dolock) |
3568 | spin_unlock(&cpu_buffer->reader_lock); | |
3569 | local_irq_restore(flags); | |
3570 | ||
d4788207 | 3571 | if (!ret) |
7a8e76a3 SR |
3572 | return 0; |
3573 | } | |
554f786e | 3574 | |
7a8e76a3 SR |
3575 | return 1; |
3576 | } | |
c4f50183 | 3577 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
7a8e76a3 SR |
3578 | |
3579 | /** | |
3580 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? | |
3581 | * @buffer: The ring buffer | |
3582 | * @cpu: The CPU buffer to test | |
3583 | */ | |
3584 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) | |
3585 | { | |
3586 | struct ring_buffer_per_cpu *cpu_buffer; | |
d4788207 | 3587 | unsigned long flags; |
8d707e8e | 3588 | int dolock; |
8aabee57 | 3589 | int ret; |
7a8e76a3 | 3590 | |
9e01c1b7 | 3591 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3592 | return 1; |
7a8e76a3 | 3593 | |
8d707e8e SR |
3594 | dolock = rb_ok_to_lock(); |
3595 | ||
7a8e76a3 | 3596 | cpu_buffer = buffer->buffers[cpu]; |
8d707e8e SR |
3597 | local_irq_save(flags); |
3598 | if (dolock) | |
3599 | spin_lock(&cpu_buffer->reader_lock); | |
554f786e | 3600 | ret = rb_per_cpu_empty(cpu_buffer); |
8d707e8e SR |
3601 | if (dolock) |
3602 | spin_unlock(&cpu_buffer->reader_lock); | |
3603 | local_irq_restore(flags); | |
554f786e SR |
3604 | |
3605 | return ret; | |
7a8e76a3 | 3606 | } |
c4f50183 | 3607 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
7a8e76a3 | 3608 | |
85bac32c | 3609 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
7a8e76a3 SR |
3610 | /** |
3611 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers | |
3612 | * @buffer_a: One buffer to swap with | |
3613 | * @buffer_b: The other buffer to swap with | |
3614 | * | |
3615 | * This function is useful for tracers that want to take a "snapshot" | |
3616 | * of a CPU buffer and has another back up buffer lying around. | |
3617 | * it is expected that the tracer handles the cpu buffer not being | |
3618 | * used at the moment. | |
3619 | */ | |
3620 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, | |
3621 | struct ring_buffer *buffer_b, int cpu) | |
3622 | { | |
3623 | struct ring_buffer_per_cpu *cpu_buffer_a; | |
3624 | struct ring_buffer_per_cpu *cpu_buffer_b; | |
554f786e SR |
3625 | int ret = -EINVAL; |
3626 | ||
9e01c1b7 RR |
3627 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
3628 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) | |
554f786e | 3629 | goto out; |
7a8e76a3 SR |
3630 | |
3631 | /* At least make sure the two buffers are somewhat the same */ | |
6d102bc6 | 3632 | if (buffer_a->pages != buffer_b->pages) |
554f786e SR |
3633 | goto out; |
3634 | ||
3635 | ret = -EAGAIN; | |
7a8e76a3 | 3636 | |
97b17efe | 3637 | if (ring_buffer_flags != RB_BUFFERS_ON) |
554f786e | 3638 | goto out; |
97b17efe SR |
3639 | |
3640 | if (atomic_read(&buffer_a->record_disabled)) | |
554f786e | 3641 | goto out; |
97b17efe SR |
3642 | |
3643 | if (atomic_read(&buffer_b->record_disabled)) | |
554f786e | 3644 | goto out; |
97b17efe | 3645 | |
7a8e76a3 SR |
3646 | cpu_buffer_a = buffer_a->buffers[cpu]; |
3647 | cpu_buffer_b = buffer_b->buffers[cpu]; | |
3648 | ||
97b17efe | 3649 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
554f786e | 3650 | goto out; |
97b17efe SR |
3651 | |
3652 | if (atomic_read(&cpu_buffer_b->record_disabled)) | |
554f786e | 3653 | goto out; |
97b17efe | 3654 | |
7a8e76a3 SR |
3655 | /* |
3656 | * We can't do a synchronize_sched here because this | |
3657 | * function can be called in atomic context. | |
3658 | * Normally this will be called from the same CPU as cpu. | |
3659 | * If not it's up to the caller to protect this. | |
3660 | */ | |
3661 | atomic_inc(&cpu_buffer_a->record_disabled); | |
3662 | atomic_inc(&cpu_buffer_b->record_disabled); | |
3663 | ||
98277991 SR |
3664 | ret = -EBUSY; |
3665 | if (local_read(&cpu_buffer_a->committing)) | |
3666 | goto out_dec; | |
3667 | if (local_read(&cpu_buffer_b->committing)) | |
3668 | goto out_dec; | |
3669 | ||
7a8e76a3 SR |
3670 | buffer_a->buffers[cpu] = cpu_buffer_b; |
3671 | buffer_b->buffers[cpu] = cpu_buffer_a; | |
3672 | ||
3673 | cpu_buffer_b->buffer = buffer_a; | |
3674 | cpu_buffer_a->buffer = buffer_b; | |
3675 | ||
98277991 SR |
3676 | ret = 0; |
3677 | ||
3678 | out_dec: | |
7a8e76a3 SR |
3679 | atomic_dec(&cpu_buffer_a->record_disabled); |
3680 | atomic_dec(&cpu_buffer_b->record_disabled); | |
554f786e | 3681 | out: |
554f786e | 3682 | return ret; |
7a8e76a3 | 3683 | } |
c4f50183 | 3684 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
85bac32c | 3685 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ |
7a8e76a3 | 3686 | |
8789a9e7 SR |
3687 | /** |
3688 | * ring_buffer_alloc_read_page - allocate a page to read from buffer | |
3689 | * @buffer: the buffer to allocate for. | |
3690 | * | |
3691 | * This function is used in conjunction with ring_buffer_read_page. | |
3692 | * When reading a full page from the ring buffer, these functions | |
3693 | * can be used to speed up the process. The calling function should | |
3694 | * allocate a few pages first with this function. Then when it | |
3695 | * needs to get pages from the ring buffer, it passes the result | |
3696 | * of this function into ring_buffer_read_page, which will swap | |
3697 | * the page that was allocated, with the read page of the buffer. | |
3698 | * | |
3699 | * Returns: | |
3700 | * The page allocated, or NULL on error. | |
3701 | */ | |
3702 | void *ring_buffer_alloc_read_page(struct ring_buffer *buffer) | |
3703 | { | |
044fa782 | 3704 | struct buffer_data_page *bpage; |
ef7a4a16 | 3705 | unsigned long addr; |
8789a9e7 SR |
3706 | |
3707 | addr = __get_free_page(GFP_KERNEL); | |
3708 | if (!addr) | |
3709 | return NULL; | |
3710 | ||
044fa782 | 3711 | bpage = (void *)addr; |
8789a9e7 | 3712 | |
ef7a4a16 SR |
3713 | rb_init_page(bpage); |
3714 | ||
044fa782 | 3715 | return bpage; |
8789a9e7 | 3716 | } |
d6ce96da | 3717 | EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); |
8789a9e7 SR |
3718 | |
3719 | /** | |
3720 | * ring_buffer_free_read_page - free an allocated read page | |
3721 | * @buffer: the buffer the page was allocate for | |
3722 | * @data: the page to free | |
3723 | * | |
3724 | * Free a page allocated from ring_buffer_alloc_read_page. | |
3725 | */ | |
3726 | void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data) | |
3727 | { | |
3728 | free_page((unsigned long)data); | |
3729 | } | |
d6ce96da | 3730 | EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); |
8789a9e7 SR |
3731 | |
3732 | /** | |
3733 | * ring_buffer_read_page - extract a page from the ring buffer | |
3734 | * @buffer: buffer to extract from | |
3735 | * @data_page: the page to use allocated from ring_buffer_alloc_read_page | |
ef7a4a16 | 3736 | * @len: amount to extract |
8789a9e7 SR |
3737 | * @cpu: the cpu of the buffer to extract |
3738 | * @full: should the extraction only happen when the page is full. | |
3739 | * | |
3740 | * This function will pull out a page from the ring buffer and consume it. | |
3741 | * @data_page must be the address of the variable that was returned | |
3742 | * from ring_buffer_alloc_read_page. This is because the page might be used | |
3743 | * to swap with a page in the ring buffer. | |
3744 | * | |
3745 | * for example: | |
b85fa01e | 3746 | * rpage = ring_buffer_alloc_read_page(buffer); |
8789a9e7 SR |
3747 | * if (!rpage) |
3748 | * return error; | |
ef7a4a16 | 3749 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
667d2412 LJ |
3750 | * if (ret >= 0) |
3751 | * process_page(rpage, ret); | |
8789a9e7 SR |
3752 | * |
3753 | * When @full is set, the function will not return true unless | |
3754 | * the writer is off the reader page. | |
3755 | * | |
3756 | * Note: it is up to the calling functions to handle sleeps and wakeups. | |
3757 | * The ring buffer can be used anywhere in the kernel and can not | |
3758 | * blindly call wake_up. The layer that uses the ring buffer must be | |
3759 | * responsible for that. | |
3760 | * | |
3761 | * Returns: | |
667d2412 LJ |
3762 | * >=0 if data has been transferred, returns the offset of consumed data. |
3763 | * <0 if no data has been transferred. | |
8789a9e7 SR |
3764 | */ |
3765 | int ring_buffer_read_page(struct ring_buffer *buffer, | |
ef7a4a16 | 3766 | void **data_page, size_t len, int cpu, int full) |
8789a9e7 SR |
3767 | { |
3768 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
3769 | struct ring_buffer_event *event; | |
044fa782 | 3770 | struct buffer_data_page *bpage; |
ef7a4a16 | 3771 | struct buffer_page *reader; |
ff0ff84a | 3772 | unsigned long missed_events; |
8789a9e7 | 3773 | unsigned long flags; |
ef7a4a16 | 3774 | unsigned int commit; |
667d2412 | 3775 | unsigned int read; |
4f3640f8 | 3776 | u64 save_timestamp; |
667d2412 | 3777 | int ret = -1; |
8789a9e7 | 3778 | |
554f786e SR |
3779 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
3780 | goto out; | |
3781 | ||
474d32b6 SR |
3782 | /* |
3783 | * If len is not big enough to hold the page header, then | |
3784 | * we can not copy anything. | |
3785 | */ | |
3786 | if (len <= BUF_PAGE_HDR_SIZE) | |
554f786e | 3787 | goto out; |
474d32b6 SR |
3788 | |
3789 | len -= BUF_PAGE_HDR_SIZE; | |
3790 | ||
8789a9e7 | 3791 | if (!data_page) |
554f786e | 3792 | goto out; |
8789a9e7 | 3793 | |
044fa782 SR |
3794 | bpage = *data_page; |
3795 | if (!bpage) | |
554f786e | 3796 | goto out; |
8789a9e7 SR |
3797 | |
3798 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); | |
3799 | ||
ef7a4a16 SR |
3800 | reader = rb_get_reader_page(cpu_buffer); |
3801 | if (!reader) | |
554f786e | 3802 | goto out_unlock; |
8789a9e7 | 3803 | |
ef7a4a16 SR |
3804 | event = rb_reader_event(cpu_buffer); |
3805 | ||
3806 | read = reader->read; | |
3807 | commit = rb_page_commit(reader); | |
667d2412 | 3808 | |
66a8cb95 | 3809 | /* Check if any events were dropped */ |
ff0ff84a | 3810 | missed_events = cpu_buffer->lost_events; |
66a8cb95 | 3811 | |
8789a9e7 | 3812 | /* |
474d32b6 SR |
3813 | * If this page has been partially read or |
3814 | * if len is not big enough to read the rest of the page or | |
3815 | * a writer is still on the page, then | |
3816 | * we must copy the data from the page to the buffer. | |
3817 | * Otherwise, we can simply swap the page with the one passed in. | |
8789a9e7 | 3818 | */ |
474d32b6 | 3819 | if (read || (len < (commit - read)) || |
ef7a4a16 | 3820 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
667d2412 | 3821 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
474d32b6 SR |
3822 | unsigned int rpos = read; |
3823 | unsigned int pos = 0; | |
ef7a4a16 | 3824 | unsigned int size; |
8789a9e7 SR |
3825 | |
3826 | if (full) | |
554f786e | 3827 | goto out_unlock; |
8789a9e7 | 3828 | |
ef7a4a16 SR |
3829 | if (len > (commit - read)) |
3830 | len = (commit - read); | |
3831 | ||
3832 | size = rb_event_length(event); | |
3833 | ||
3834 | if (len < size) | |
554f786e | 3835 | goto out_unlock; |
ef7a4a16 | 3836 | |
4f3640f8 SR |
3837 | /* save the current timestamp, since the user will need it */ |
3838 | save_timestamp = cpu_buffer->read_stamp; | |
3839 | ||
ef7a4a16 SR |
3840 | /* Need to copy one event at a time */ |
3841 | do { | |
474d32b6 | 3842 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
ef7a4a16 SR |
3843 | |
3844 | len -= size; | |
3845 | ||
3846 | rb_advance_reader(cpu_buffer); | |
474d32b6 SR |
3847 | rpos = reader->read; |
3848 | pos += size; | |
ef7a4a16 | 3849 | |
18fab912 HY |
3850 | if (rpos >= commit) |
3851 | break; | |
3852 | ||
ef7a4a16 SR |
3853 | event = rb_reader_event(cpu_buffer); |
3854 | size = rb_event_length(event); | |
3855 | } while (len > size); | |
667d2412 LJ |
3856 | |
3857 | /* update bpage */ | |
ef7a4a16 | 3858 | local_set(&bpage->commit, pos); |
4f3640f8 | 3859 | bpage->time_stamp = save_timestamp; |
ef7a4a16 | 3860 | |
474d32b6 SR |
3861 | /* we copied everything to the beginning */ |
3862 | read = 0; | |
8789a9e7 | 3863 | } else { |
afbab76a | 3864 | /* update the entry counter */ |
77ae365e | 3865 | cpu_buffer->read += rb_page_entries(reader); |
afbab76a | 3866 | |
8789a9e7 | 3867 | /* swap the pages */ |
044fa782 | 3868 | rb_init_page(bpage); |
ef7a4a16 SR |
3869 | bpage = reader->page; |
3870 | reader->page = *data_page; | |
3871 | local_set(&reader->write, 0); | |
778c55d4 | 3872 | local_set(&reader->entries, 0); |
ef7a4a16 | 3873 | reader->read = 0; |
044fa782 | 3874 | *data_page = bpage; |
ff0ff84a SR |
3875 | |
3876 | /* | |
3877 | * Use the real_end for the data size, | |
3878 | * This gives us a chance to store the lost events | |
3879 | * on the page. | |
3880 | */ | |
3881 | if (reader->real_end) | |
3882 | local_set(&bpage->commit, reader->real_end); | |
8789a9e7 | 3883 | } |
667d2412 | 3884 | ret = read; |
8789a9e7 | 3885 | |
66a8cb95 | 3886 | cpu_buffer->lost_events = 0; |
2711ca23 SR |
3887 | |
3888 | commit = local_read(&bpage->commit); | |
66a8cb95 SR |
3889 | /* |
3890 | * Set a flag in the commit field if we lost events | |
3891 | */ | |
ff0ff84a | 3892 | if (missed_events) { |
ff0ff84a SR |
3893 | /* If there is room at the end of the page to save the |
3894 | * missed events, then record it there. | |
3895 | */ | |
3896 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { | |
3897 | memcpy(&bpage->data[commit], &missed_events, | |
3898 | sizeof(missed_events)); | |
3899 | local_add(RB_MISSED_STORED, &bpage->commit); | |
2711ca23 | 3900 | commit += sizeof(missed_events); |
ff0ff84a | 3901 | } |
66a8cb95 | 3902 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
ff0ff84a | 3903 | } |
66a8cb95 | 3904 | |
2711ca23 SR |
3905 | /* |
3906 | * This page may be off to user land. Zero it out here. | |
3907 | */ | |
3908 | if (commit < BUF_PAGE_SIZE) | |
3909 | memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit); | |
3910 | ||
554f786e | 3911 | out_unlock: |
8789a9e7 SR |
3912 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
3913 | ||
554f786e | 3914 | out: |
8789a9e7 SR |
3915 | return ret; |
3916 | } | |
d6ce96da | 3917 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
8789a9e7 | 3918 | |
1155de47 | 3919 | #ifdef CONFIG_TRACING |
a3583244 SR |
3920 | static ssize_t |
3921 | rb_simple_read(struct file *filp, char __user *ubuf, | |
3922 | size_t cnt, loff_t *ppos) | |
3923 | { | |
5e39841c | 3924 | unsigned long *p = filp->private_data; |
a3583244 SR |
3925 | char buf[64]; |
3926 | int r; | |
3927 | ||
033601a3 SR |
3928 | if (test_bit(RB_BUFFERS_DISABLED_BIT, p)) |
3929 | r = sprintf(buf, "permanently disabled\n"); | |
3930 | else | |
3931 | r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p)); | |
a3583244 SR |
3932 | |
3933 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
3934 | } | |
3935 | ||
3936 | static ssize_t | |
3937 | rb_simple_write(struct file *filp, const char __user *ubuf, | |
3938 | size_t cnt, loff_t *ppos) | |
3939 | { | |
5e39841c | 3940 | unsigned long *p = filp->private_data; |
a3583244 | 3941 | char buf[64]; |
5e39841c | 3942 | unsigned long val; |
a3583244 SR |
3943 | int ret; |
3944 | ||
3945 | if (cnt >= sizeof(buf)) | |
3946 | return -EINVAL; | |
3947 | ||
3948 | if (copy_from_user(&buf, ubuf, cnt)) | |
3949 | return -EFAULT; | |
3950 | ||
3951 | buf[cnt] = 0; | |
3952 | ||
3953 | ret = strict_strtoul(buf, 10, &val); | |
3954 | if (ret < 0) | |
3955 | return ret; | |
3956 | ||
033601a3 SR |
3957 | if (val) |
3958 | set_bit(RB_BUFFERS_ON_BIT, p); | |
3959 | else | |
3960 | clear_bit(RB_BUFFERS_ON_BIT, p); | |
a3583244 SR |
3961 | |
3962 | (*ppos)++; | |
3963 | ||
3964 | return cnt; | |
3965 | } | |
3966 | ||
5e2336a0 | 3967 | static const struct file_operations rb_simple_fops = { |
a3583244 SR |
3968 | .open = tracing_open_generic, |
3969 | .read = rb_simple_read, | |
3970 | .write = rb_simple_write, | |
3971 | }; | |
3972 | ||
3973 | ||
3974 | static __init int rb_init_debugfs(void) | |
3975 | { | |
3976 | struct dentry *d_tracer; | |
a3583244 SR |
3977 | |
3978 | d_tracer = tracing_init_dentry(); | |
3979 | ||
5452af66 FW |
3980 | trace_create_file("tracing_on", 0644, d_tracer, |
3981 | &ring_buffer_flags, &rb_simple_fops); | |
a3583244 SR |
3982 | |
3983 | return 0; | |
3984 | } | |
3985 | ||
3986 | fs_initcall(rb_init_debugfs); | |
1155de47 | 3987 | #endif |
554f786e | 3988 | |
59222efe | 3989 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
3990 | static int rb_cpu_notify(struct notifier_block *self, |
3991 | unsigned long action, void *hcpu) | |
554f786e SR |
3992 | { |
3993 | struct ring_buffer *buffer = | |
3994 | container_of(self, struct ring_buffer, cpu_notify); | |
3995 | long cpu = (long)hcpu; | |
3996 | ||
3997 | switch (action) { | |
3998 | case CPU_UP_PREPARE: | |
3999 | case CPU_UP_PREPARE_FROZEN: | |
3f237a79 | 4000 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
554f786e SR |
4001 | return NOTIFY_OK; |
4002 | ||
4003 | buffer->buffers[cpu] = | |
4004 | rb_allocate_cpu_buffer(buffer, cpu); | |
4005 | if (!buffer->buffers[cpu]) { | |
4006 | WARN(1, "failed to allocate ring buffer on CPU %ld\n", | |
4007 | cpu); | |
4008 | return NOTIFY_OK; | |
4009 | } | |
4010 | smp_wmb(); | |
3f237a79 | 4011 | cpumask_set_cpu(cpu, buffer->cpumask); |
554f786e SR |
4012 | break; |
4013 | case CPU_DOWN_PREPARE: | |
4014 | case CPU_DOWN_PREPARE_FROZEN: | |
4015 | /* | |
4016 | * Do nothing. | |
4017 | * If we were to free the buffer, then the user would | |
4018 | * lose any trace that was in the buffer. | |
4019 | */ | |
4020 | break; | |
4021 | default: | |
4022 | break; | |
4023 | } | |
4024 | return NOTIFY_OK; | |
4025 | } | |
4026 | #endif |