]>
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 | ||
69d1b839 SR |
227 | #define skip_time_extend(event) \ |
228 | ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND)) | |
229 | ||
2d622719 TZ |
230 | static inline int rb_null_event(struct ring_buffer_event *event) |
231 | { | |
a1863c21 | 232 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
2d622719 TZ |
233 | } |
234 | ||
235 | static void rb_event_set_padding(struct ring_buffer_event *event) | |
236 | { | |
a1863c21 | 237 | /* padding has a NULL time_delta */ |
334d4169 | 238 | event->type_len = RINGBUF_TYPE_PADDING; |
2d622719 TZ |
239 | event->time_delta = 0; |
240 | } | |
241 | ||
34a148bf | 242 | static unsigned |
2d622719 | 243 | rb_event_data_length(struct ring_buffer_event *event) |
7a8e76a3 SR |
244 | { |
245 | unsigned length; | |
246 | ||
334d4169 LJ |
247 | if (event->type_len) |
248 | length = event->type_len * RB_ALIGNMENT; | |
2d622719 TZ |
249 | else |
250 | length = event->array[0]; | |
251 | return length + RB_EVNT_HDR_SIZE; | |
252 | } | |
253 | ||
69d1b839 SR |
254 | /* |
255 | * Return the length of the given event. Will return | |
256 | * the length of the time extend if the event is a | |
257 | * time extend. | |
258 | */ | |
259 | static inline unsigned | |
2d622719 TZ |
260 | rb_event_length(struct ring_buffer_event *event) |
261 | { | |
334d4169 | 262 | switch (event->type_len) { |
7a8e76a3 | 263 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
264 | if (rb_null_event(event)) |
265 | /* undefined */ | |
266 | return -1; | |
334d4169 | 267 | return event->array[0] + RB_EVNT_HDR_SIZE; |
7a8e76a3 SR |
268 | |
269 | case RINGBUF_TYPE_TIME_EXTEND: | |
270 | return RB_LEN_TIME_EXTEND; | |
271 | ||
272 | case RINGBUF_TYPE_TIME_STAMP: | |
273 | return RB_LEN_TIME_STAMP; | |
274 | ||
275 | case RINGBUF_TYPE_DATA: | |
2d622719 | 276 | return rb_event_data_length(event); |
7a8e76a3 SR |
277 | default: |
278 | BUG(); | |
279 | } | |
280 | /* not hit */ | |
281 | return 0; | |
282 | } | |
283 | ||
69d1b839 SR |
284 | /* |
285 | * Return total length of time extend and data, | |
286 | * or just the event length for all other events. | |
287 | */ | |
288 | static inline unsigned | |
289 | rb_event_ts_length(struct ring_buffer_event *event) | |
290 | { | |
291 | unsigned len = 0; | |
292 | ||
293 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) { | |
294 | /* time extends include the data event after it */ | |
295 | len = RB_LEN_TIME_EXTEND; | |
296 | event = skip_time_extend(event); | |
297 | } | |
298 | return len + rb_event_length(event); | |
299 | } | |
300 | ||
7a8e76a3 SR |
301 | /** |
302 | * ring_buffer_event_length - return the length of the event | |
303 | * @event: the event to get the length of | |
69d1b839 SR |
304 | * |
305 | * Returns the size of the data load of a data event. | |
306 | * If the event is something other than a data event, it | |
307 | * returns the size of the event itself. With the exception | |
308 | * of a TIME EXTEND, where it still returns the size of the | |
309 | * data load of the data event after it. | |
7a8e76a3 SR |
310 | */ |
311 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) | |
312 | { | |
69d1b839 SR |
313 | unsigned length; |
314 | ||
315 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) | |
316 | event = skip_time_extend(event); | |
317 | ||
318 | length = rb_event_length(event); | |
334d4169 | 319 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
465634ad RR |
320 | return length; |
321 | length -= RB_EVNT_HDR_SIZE; | |
322 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) | |
323 | length -= sizeof(event->array[0]); | |
324 | return length; | |
7a8e76a3 | 325 | } |
c4f50183 | 326 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
7a8e76a3 SR |
327 | |
328 | /* inline for ring buffer fast paths */ | |
34a148bf | 329 | static void * |
7a8e76a3 SR |
330 | rb_event_data(struct ring_buffer_event *event) |
331 | { | |
69d1b839 SR |
332 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) |
333 | event = skip_time_extend(event); | |
334d4169 | 334 | BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
7a8e76a3 | 335 | /* If length is in len field, then array[0] has the data */ |
334d4169 | 336 | if (event->type_len) |
7a8e76a3 SR |
337 | return (void *)&event->array[0]; |
338 | /* Otherwise length is in array[0] and array[1] has the data */ | |
339 | return (void *)&event->array[1]; | |
340 | } | |
341 | ||
342 | /** | |
343 | * ring_buffer_event_data - return the data of the event | |
344 | * @event: the event to get the data from | |
345 | */ | |
346 | void *ring_buffer_event_data(struct ring_buffer_event *event) | |
347 | { | |
348 | return rb_event_data(event); | |
349 | } | |
c4f50183 | 350 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
7a8e76a3 SR |
351 | |
352 | #define for_each_buffer_cpu(buffer, cpu) \ | |
9e01c1b7 | 353 | for_each_cpu(cpu, buffer->cpumask) |
7a8e76a3 SR |
354 | |
355 | #define TS_SHIFT 27 | |
356 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) | |
357 | #define TS_DELTA_TEST (~TS_MASK) | |
358 | ||
66a8cb95 SR |
359 | /* Flag when events were overwritten */ |
360 | #define RB_MISSED_EVENTS (1 << 31) | |
ff0ff84a SR |
361 | /* Missed count stored at end */ |
362 | #define RB_MISSED_STORED (1 << 30) | |
66a8cb95 | 363 | |
abc9b56d | 364 | struct buffer_data_page { |
e4c2ce82 | 365 | u64 time_stamp; /* page time stamp */ |
c3706f00 | 366 | local_t commit; /* write committed index */ |
abc9b56d SR |
367 | unsigned char data[]; /* data of buffer page */ |
368 | }; | |
369 | ||
77ae365e SR |
370 | /* |
371 | * Note, the buffer_page list must be first. The buffer pages | |
372 | * are allocated in cache lines, which means that each buffer | |
373 | * page will be at the beginning of a cache line, and thus | |
374 | * the least significant bits will be zero. We use this to | |
375 | * add flags in the list struct pointers, to make the ring buffer | |
376 | * lockless. | |
377 | */ | |
abc9b56d | 378 | struct buffer_page { |
778c55d4 | 379 | struct list_head list; /* list of buffer pages */ |
abc9b56d | 380 | local_t write; /* index for next write */ |
6f807acd | 381 | unsigned read; /* index for next read */ |
778c55d4 | 382 | local_t entries; /* entries on this page */ |
ff0ff84a | 383 | unsigned long real_end; /* real end of data */ |
abc9b56d | 384 | struct buffer_data_page *page; /* Actual data page */ |
7a8e76a3 SR |
385 | }; |
386 | ||
77ae365e SR |
387 | /* |
388 | * The buffer page counters, write and entries, must be reset | |
389 | * atomically when crossing page boundaries. To synchronize this | |
390 | * update, two counters are inserted into the number. One is | |
391 | * the actual counter for the write position or count on the page. | |
392 | * | |
393 | * The other is a counter of updaters. Before an update happens | |
394 | * the update partition of the counter is incremented. This will | |
395 | * allow the updater to update the counter atomically. | |
396 | * | |
397 | * The counter is 20 bits, and the state data is 12. | |
398 | */ | |
399 | #define RB_WRITE_MASK 0xfffff | |
400 | #define RB_WRITE_INTCNT (1 << 20) | |
401 | ||
044fa782 | 402 | static void rb_init_page(struct buffer_data_page *bpage) |
abc9b56d | 403 | { |
044fa782 | 404 | local_set(&bpage->commit, 0); |
abc9b56d SR |
405 | } |
406 | ||
474d32b6 SR |
407 | /** |
408 | * ring_buffer_page_len - the size of data on the page. | |
409 | * @page: The page to read | |
410 | * | |
411 | * Returns the amount of data on the page, including buffer page header. | |
412 | */ | |
ef7a4a16 SR |
413 | size_t ring_buffer_page_len(void *page) |
414 | { | |
474d32b6 SR |
415 | return local_read(&((struct buffer_data_page *)page)->commit) |
416 | + BUF_PAGE_HDR_SIZE; | |
ef7a4a16 SR |
417 | } |
418 | ||
ed56829c SR |
419 | /* |
420 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing | |
421 | * this issue out. | |
422 | */ | |
34a148bf | 423 | static void free_buffer_page(struct buffer_page *bpage) |
ed56829c | 424 | { |
34a148bf | 425 | free_page((unsigned long)bpage->page); |
e4c2ce82 | 426 | kfree(bpage); |
ed56829c SR |
427 | } |
428 | ||
7a8e76a3 SR |
429 | /* |
430 | * We need to fit the time_stamp delta into 27 bits. | |
431 | */ | |
432 | static inline int test_time_stamp(u64 delta) | |
433 | { | |
434 | if (delta & TS_DELTA_TEST) | |
435 | return 1; | |
436 | return 0; | |
437 | } | |
438 | ||
474d32b6 | 439 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
7a8e76a3 | 440 | |
be957c44 SR |
441 | /* Max payload is BUF_PAGE_SIZE - header (8bytes) */ |
442 | #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2)) | |
443 | ||
ea05b57c | 444 | /* Max number of timestamps that can fit on a page */ |
d0134324 | 445 | #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_EXTEND) |
ea05b57c | 446 | |
d1b182a8 SR |
447 | int ring_buffer_print_page_header(struct trace_seq *s) |
448 | { | |
449 | struct buffer_data_page field; | |
450 | int ret; | |
451 | ||
452 | ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t" | |
26a50744 TZ |
453 | "offset:0;\tsize:%u;\tsigned:%u;\n", |
454 | (unsigned int)sizeof(field.time_stamp), | |
455 | (unsigned int)is_signed_type(u64)); | |
d1b182a8 SR |
456 | |
457 | ret = trace_seq_printf(s, "\tfield: local_t commit;\t" | |
26a50744 | 458 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 459 | (unsigned int)offsetof(typeof(field), commit), |
26a50744 TZ |
460 | (unsigned int)sizeof(field.commit), |
461 | (unsigned int)is_signed_type(long)); | |
d1b182a8 | 462 | |
66a8cb95 SR |
463 | ret = trace_seq_printf(s, "\tfield: int overwrite;\t" |
464 | "offset:%u;\tsize:%u;\tsigned:%u;\n", | |
465 | (unsigned int)offsetof(typeof(field), commit), | |
466 | 1, | |
467 | (unsigned int)is_signed_type(long)); | |
468 | ||
d1b182a8 | 469 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
26a50744 | 470 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 471 | (unsigned int)offsetof(typeof(field), data), |
26a50744 TZ |
472 | (unsigned int)BUF_PAGE_SIZE, |
473 | (unsigned int)is_signed_type(char)); | |
d1b182a8 SR |
474 | |
475 | return ret; | |
476 | } | |
477 | ||
7a8e76a3 SR |
478 | /* |
479 | * head_page == tail_page && head == tail then buffer is empty. | |
480 | */ | |
481 | struct ring_buffer_per_cpu { | |
482 | int cpu; | |
985023de | 483 | atomic_t record_disabled; |
7a8e76a3 | 484 | struct ring_buffer *buffer; |
77ae365e | 485 | spinlock_t reader_lock; /* serialize readers */ |
445c8951 | 486 | arch_spinlock_t lock; |
7a8e76a3 | 487 | struct lock_class_key lock_key; |
3adc54fa | 488 | struct list_head *pages; |
6f807acd SR |
489 | struct buffer_page *head_page; /* read from head */ |
490 | struct buffer_page *tail_page; /* write to tail */ | |
c3706f00 | 491 | struct buffer_page *commit_page; /* committed pages */ |
d769041f | 492 | struct buffer_page *reader_page; |
66a8cb95 SR |
493 | unsigned long lost_events; |
494 | unsigned long last_overrun; | |
77ae365e SR |
495 | local_t commit_overrun; |
496 | local_t overrun; | |
e4906eff | 497 | local_t entries; |
fa743953 SR |
498 | local_t committing; |
499 | local_t commits; | |
77ae365e | 500 | unsigned long read; |
7a8e76a3 SR |
501 | u64 write_stamp; |
502 | u64 read_stamp; | |
7a8e76a3 SR |
503 | }; |
504 | ||
505 | struct ring_buffer { | |
7a8e76a3 SR |
506 | unsigned pages; |
507 | unsigned flags; | |
508 | int cpus; | |
7a8e76a3 | 509 | atomic_t record_disabled; |
00f62f61 | 510 | cpumask_var_t cpumask; |
7a8e76a3 | 511 | |
1f8a6a10 PZ |
512 | struct lock_class_key *reader_lock_key; |
513 | ||
7a8e76a3 SR |
514 | struct mutex mutex; |
515 | ||
516 | struct ring_buffer_per_cpu **buffers; | |
554f786e | 517 | |
59222efe | 518 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
519 | struct notifier_block cpu_notify; |
520 | #endif | |
37886f6a | 521 | u64 (*clock)(void); |
7a8e76a3 SR |
522 | }; |
523 | ||
524 | struct ring_buffer_iter { | |
525 | struct ring_buffer_per_cpu *cpu_buffer; | |
526 | unsigned long head; | |
527 | struct buffer_page *head_page; | |
492a74f4 SR |
528 | struct buffer_page *cache_reader_page; |
529 | unsigned long cache_read; | |
7a8e76a3 SR |
530 | u64 read_stamp; |
531 | }; | |
532 | ||
f536aafc | 533 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
077c5407 SR |
534 | #define RB_WARN_ON(b, cond) \ |
535 | ({ \ | |
536 | int _____ret = unlikely(cond); \ | |
537 | if (_____ret) { \ | |
538 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ | |
539 | struct ring_buffer_per_cpu *__b = \ | |
540 | (void *)b; \ | |
541 | atomic_inc(&__b->buffer->record_disabled); \ | |
542 | } else \ | |
543 | atomic_inc(&b->record_disabled); \ | |
544 | WARN_ON(1); \ | |
545 | } \ | |
546 | _____ret; \ | |
3e89c7bb | 547 | }) |
f536aafc | 548 | |
37886f6a SR |
549 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
550 | #define DEBUG_SHIFT 0 | |
551 | ||
6d3f1e12 | 552 | static inline u64 rb_time_stamp(struct ring_buffer *buffer) |
88eb0125 SR |
553 | { |
554 | /* shift to debug/test normalization and TIME_EXTENTS */ | |
555 | return buffer->clock() << DEBUG_SHIFT; | |
556 | } | |
557 | ||
37886f6a SR |
558 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu) |
559 | { | |
560 | u64 time; | |
561 | ||
562 | preempt_disable_notrace(); | |
6d3f1e12 | 563 | time = rb_time_stamp(buffer); |
37886f6a SR |
564 | preempt_enable_no_resched_notrace(); |
565 | ||
566 | return time; | |
567 | } | |
568 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); | |
569 | ||
570 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, | |
571 | int cpu, u64 *ts) | |
572 | { | |
573 | /* Just stupid testing the normalize function and deltas */ | |
574 | *ts >>= DEBUG_SHIFT; | |
575 | } | |
576 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); | |
577 | ||
77ae365e SR |
578 | /* |
579 | * Making the ring buffer lockless makes things tricky. | |
580 | * Although writes only happen on the CPU that they are on, | |
581 | * and they only need to worry about interrupts. Reads can | |
582 | * happen on any CPU. | |
583 | * | |
584 | * The reader page is always off the ring buffer, but when the | |
585 | * reader finishes with a page, it needs to swap its page with | |
586 | * a new one from the buffer. The reader needs to take from | |
587 | * the head (writes go to the tail). But if a writer is in overwrite | |
588 | * mode and wraps, it must push the head page forward. | |
589 | * | |
590 | * Here lies the problem. | |
591 | * | |
592 | * The reader must be careful to replace only the head page, and | |
593 | * not another one. As described at the top of the file in the | |
594 | * ASCII art, the reader sets its old page to point to the next | |
595 | * page after head. It then sets the page after head to point to | |
596 | * the old reader page. But if the writer moves the head page | |
597 | * during this operation, the reader could end up with the tail. | |
598 | * | |
599 | * We use cmpxchg to help prevent this race. We also do something | |
600 | * special with the page before head. We set the LSB to 1. | |
601 | * | |
602 | * When the writer must push the page forward, it will clear the | |
603 | * bit that points to the head page, move the head, and then set | |
604 | * the bit that points to the new head page. | |
605 | * | |
606 | * We also don't want an interrupt coming in and moving the head | |
607 | * page on another writer. Thus we use the second LSB to catch | |
608 | * that too. Thus: | |
609 | * | |
610 | * head->list->prev->next bit 1 bit 0 | |
611 | * ------- ------- | |
612 | * Normal page 0 0 | |
613 | * Points to head page 0 1 | |
614 | * New head page 1 0 | |
615 | * | |
616 | * Note we can not trust the prev pointer of the head page, because: | |
617 | * | |
618 | * +----+ +-----+ +-----+ | |
619 | * | |------>| T |---X--->| N | | |
620 | * | |<------| | | | | |
621 | * +----+ +-----+ +-----+ | |
622 | * ^ ^ | | |
623 | * | +-----+ | | | |
624 | * +----------| R |----------+ | | |
625 | * | |<-----------+ | |
626 | * +-----+ | |
627 | * | |
628 | * Key: ---X--> HEAD flag set in pointer | |
629 | * T Tail page | |
630 | * R Reader page | |
631 | * N Next page | |
632 | * | |
633 | * (see __rb_reserve_next() to see where this happens) | |
634 | * | |
635 | * What the above shows is that the reader just swapped out | |
636 | * the reader page with a page in the buffer, but before it | |
637 | * could make the new header point back to the new page added | |
638 | * it was preempted by a writer. The writer moved forward onto | |
639 | * the new page added by the reader and is about to move forward | |
640 | * again. | |
641 | * | |
642 | * You can see, it is legitimate for the previous pointer of | |
643 | * the head (or any page) not to point back to itself. But only | |
644 | * temporarially. | |
645 | */ | |
646 | ||
647 | #define RB_PAGE_NORMAL 0UL | |
648 | #define RB_PAGE_HEAD 1UL | |
649 | #define RB_PAGE_UPDATE 2UL | |
650 | ||
651 | ||
652 | #define RB_FLAG_MASK 3UL | |
653 | ||
654 | /* PAGE_MOVED is not part of the mask */ | |
655 | #define RB_PAGE_MOVED 4UL | |
656 | ||
657 | /* | |
658 | * rb_list_head - remove any bit | |
659 | */ | |
660 | static struct list_head *rb_list_head(struct list_head *list) | |
661 | { | |
662 | unsigned long val = (unsigned long)list; | |
663 | ||
664 | return (struct list_head *)(val & ~RB_FLAG_MASK); | |
665 | } | |
666 | ||
667 | /* | |
6d3f1e12 | 668 | * rb_is_head_page - test if the given page is the head page |
77ae365e SR |
669 | * |
670 | * Because the reader may move the head_page pointer, we can | |
671 | * not trust what the head page is (it may be pointing to | |
672 | * the reader page). But if the next page is a header page, | |
673 | * its flags will be non zero. | |
674 | */ | |
675 | static int inline | |
676 | rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer, | |
677 | struct buffer_page *page, struct list_head *list) | |
678 | { | |
679 | unsigned long val; | |
680 | ||
681 | val = (unsigned long)list->next; | |
682 | ||
683 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) | |
684 | return RB_PAGE_MOVED; | |
685 | ||
686 | return val & RB_FLAG_MASK; | |
687 | } | |
688 | ||
689 | /* | |
690 | * rb_is_reader_page | |
691 | * | |
692 | * The unique thing about the reader page, is that, if the | |
693 | * writer is ever on it, the previous pointer never points | |
694 | * back to the reader page. | |
695 | */ | |
696 | static int rb_is_reader_page(struct buffer_page *page) | |
697 | { | |
698 | struct list_head *list = page->list.prev; | |
699 | ||
700 | return rb_list_head(list->next) != &page->list; | |
701 | } | |
702 | ||
703 | /* | |
704 | * rb_set_list_to_head - set a list_head to be pointing to head. | |
705 | */ | |
706 | static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer, | |
707 | struct list_head *list) | |
708 | { | |
709 | unsigned long *ptr; | |
710 | ||
711 | ptr = (unsigned long *)&list->next; | |
712 | *ptr |= RB_PAGE_HEAD; | |
713 | *ptr &= ~RB_PAGE_UPDATE; | |
714 | } | |
715 | ||
716 | /* | |
717 | * rb_head_page_activate - sets up head page | |
718 | */ | |
719 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) | |
720 | { | |
721 | struct buffer_page *head; | |
722 | ||
723 | head = cpu_buffer->head_page; | |
724 | if (!head) | |
725 | return; | |
726 | ||
727 | /* | |
728 | * Set the previous list pointer to have the HEAD flag. | |
729 | */ | |
730 | rb_set_list_to_head(cpu_buffer, head->list.prev); | |
731 | } | |
732 | ||
733 | static void rb_list_head_clear(struct list_head *list) | |
734 | { | |
735 | unsigned long *ptr = (unsigned long *)&list->next; | |
736 | ||
737 | *ptr &= ~RB_FLAG_MASK; | |
738 | } | |
739 | ||
740 | /* | |
741 | * rb_head_page_dactivate - clears head page ptr (for free list) | |
742 | */ | |
743 | static void | |
744 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) | |
745 | { | |
746 | struct list_head *hd; | |
747 | ||
748 | /* Go through the whole list and clear any pointers found. */ | |
749 | rb_list_head_clear(cpu_buffer->pages); | |
750 | ||
751 | list_for_each(hd, cpu_buffer->pages) | |
752 | rb_list_head_clear(hd); | |
753 | } | |
754 | ||
755 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, | |
756 | struct buffer_page *head, | |
757 | struct buffer_page *prev, | |
758 | int old_flag, int new_flag) | |
759 | { | |
760 | struct list_head *list; | |
761 | unsigned long val = (unsigned long)&head->list; | |
762 | unsigned long ret; | |
763 | ||
764 | list = &prev->list; | |
765 | ||
766 | val &= ~RB_FLAG_MASK; | |
767 | ||
08a40816 SR |
768 | ret = cmpxchg((unsigned long *)&list->next, |
769 | val | old_flag, val | new_flag); | |
77ae365e SR |
770 | |
771 | /* check if the reader took the page */ | |
772 | if ((ret & ~RB_FLAG_MASK) != val) | |
773 | return RB_PAGE_MOVED; | |
774 | ||
775 | return ret & RB_FLAG_MASK; | |
776 | } | |
777 | ||
778 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, | |
779 | struct buffer_page *head, | |
780 | struct buffer_page *prev, | |
781 | int old_flag) | |
782 | { | |
783 | return rb_head_page_set(cpu_buffer, head, prev, | |
784 | old_flag, RB_PAGE_UPDATE); | |
785 | } | |
786 | ||
787 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, | |
788 | struct buffer_page *head, | |
789 | struct buffer_page *prev, | |
790 | int old_flag) | |
791 | { | |
792 | return rb_head_page_set(cpu_buffer, head, prev, | |
793 | old_flag, RB_PAGE_HEAD); | |
794 | } | |
795 | ||
796 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, | |
797 | struct buffer_page *head, | |
798 | struct buffer_page *prev, | |
799 | int old_flag) | |
800 | { | |
801 | return rb_head_page_set(cpu_buffer, head, prev, | |
802 | old_flag, RB_PAGE_NORMAL); | |
803 | } | |
804 | ||
805 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, | |
806 | struct buffer_page **bpage) | |
807 | { | |
808 | struct list_head *p = rb_list_head((*bpage)->list.next); | |
809 | ||
810 | *bpage = list_entry(p, struct buffer_page, list); | |
811 | } | |
812 | ||
813 | static struct buffer_page * | |
814 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) | |
815 | { | |
816 | struct buffer_page *head; | |
817 | struct buffer_page *page; | |
818 | struct list_head *list; | |
819 | int i; | |
820 | ||
821 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) | |
822 | return NULL; | |
823 | ||
824 | /* sanity check */ | |
825 | list = cpu_buffer->pages; | |
826 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) | |
827 | return NULL; | |
828 | ||
829 | page = head = cpu_buffer->head_page; | |
830 | /* | |
831 | * It is possible that the writer moves the header behind | |
832 | * where we started, and we miss in one loop. | |
833 | * A second loop should grab the header, but we'll do | |
834 | * three loops just because I'm paranoid. | |
835 | */ | |
836 | for (i = 0; i < 3; i++) { | |
837 | do { | |
838 | if (rb_is_head_page(cpu_buffer, page, page->list.prev)) { | |
839 | cpu_buffer->head_page = page; | |
840 | return page; | |
841 | } | |
842 | rb_inc_page(cpu_buffer, &page); | |
843 | } while (page != head); | |
844 | } | |
845 | ||
846 | RB_WARN_ON(cpu_buffer, 1); | |
847 | ||
848 | return NULL; | |
849 | } | |
850 | ||
851 | static int rb_head_page_replace(struct buffer_page *old, | |
852 | struct buffer_page *new) | |
853 | { | |
854 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; | |
855 | unsigned long val; | |
856 | unsigned long ret; | |
857 | ||
858 | val = *ptr & ~RB_FLAG_MASK; | |
859 | val |= RB_PAGE_HEAD; | |
860 | ||
08a40816 | 861 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
77ae365e SR |
862 | |
863 | return ret == val; | |
864 | } | |
865 | ||
866 | /* | |
867 | * rb_tail_page_update - move the tail page forward | |
868 | * | |
869 | * Returns 1 if moved tail page, 0 if someone else did. | |
870 | */ | |
871 | static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, | |
872 | struct buffer_page *tail_page, | |
873 | struct buffer_page *next_page) | |
874 | { | |
875 | struct buffer_page *old_tail; | |
876 | unsigned long old_entries; | |
877 | unsigned long old_write; | |
878 | int ret = 0; | |
879 | ||
880 | /* | |
881 | * The tail page now needs to be moved forward. | |
882 | * | |
883 | * We need to reset the tail page, but without messing | |
884 | * with possible erasing of data brought in by interrupts | |
885 | * that have moved the tail page and are currently on it. | |
886 | * | |
887 | * We add a counter to the write field to denote this. | |
888 | */ | |
889 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); | |
890 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); | |
891 | ||
892 | /* | |
893 | * Just make sure we have seen our old_write and synchronize | |
894 | * with any interrupts that come in. | |
895 | */ | |
896 | barrier(); | |
897 | ||
898 | /* | |
899 | * If the tail page is still the same as what we think | |
900 | * it is, then it is up to us to update the tail | |
901 | * pointer. | |
902 | */ | |
903 | if (tail_page == cpu_buffer->tail_page) { | |
904 | /* Zero the write counter */ | |
905 | unsigned long val = old_write & ~RB_WRITE_MASK; | |
906 | unsigned long eval = old_entries & ~RB_WRITE_MASK; | |
907 | ||
908 | /* | |
909 | * This will only succeed if an interrupt did | |
910 | * not come in and change it. In which case, we | |
911 | * do not want to modify it. | |
da706d8b LJ |
912 | * |
913 | * We add (void) to let the compiler know that we do not care | |
914 | * about the return value of these functions. We use the | |
915 | * cmpxchg to only update if an interrupt did not already | |
916 | * do it for us. If the cmpxchg fails, we don't care. | |
77ae365e | 917 | */ |
da706d8b LJ |
918 | (void)local_cmpxchg(&next_page->write, old_write, val); |
919 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); | |
77ae365e SR |
920 | |
921 | /* | |
922 | * No need to worry about races with clearing out the commit. | |
923 | * it only can increment when a commit takes place. But that | |
924 | * only happens in the outer most nested commit. | |
925 | */ | |
926 | local_set(&next_page->page->commit, 0); | |
927 | ||
928 | old_tail = cmpxchg(&cpu_buffer->tail_page, | |
929 | tail_page, next_page); | |
930 | ||
931 | if (old_tail == tail_page) | |
932 | ret = 1; | |
933 | } | |
934 | ||
935 | return ret; | |
936 | } | |
937 | ||
938 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, | |
939 | struct buffer_page *bpage) | |
940 | { | |
941 | unsigned long val = (unsigned long)bpage; | |
942 | ||
943 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) | |
944 | return 1; | |
945 | ||
946 | return 0; | |
947 | } | |
948 | ||
949 | /** | |
950 | * rb_check_list - make sure a pointer to a list has the last bits zero | |
951 | */ | |
952 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, | |
953 | struct list_head *list) | |
954 | { | |
955 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) | |
956 | return 1; | |
957 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) | |
958 | return 1; | |
959 | return 0; | |
960 | } | |
961 | ||
7a8e76a3 SR |
962 | /** |
963 | * check_pages - integrity check of buffer pages | |
964 | * @cpu_buffer: CPU buffer with pages to test | |
965 | * | |
c3706f00 | 966 | * As a safety measure we check to make sure the data pages have not |
7a8e76a3 SR |
967 | * been corrupted. |
968 | */ | |
969 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) | |
970 | { | |
3adc54fa | 971 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 972 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 973 | |
77ae365e SR |
974 | rb_head_page_deactivate(cpu_buffer); |
975 | ||
3e89c7bb SR |
976 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
977 | return -1; | |
978 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) | |
979 | return -1; | |
7a8e76a3 | 980 | |
77ae365e SR |
981 | if (rb_check_list(cpu_buffer, head)) |
982 | return -1; | |
983 | ||
044fa782 | 984 | list_for_each_entry_safe(bpage, tmp, head, list) { |
3e89c7bb | 985 | if (RB_WARN_ON(cpu_buffer, |
044fa782 | 986 | bpage->list.next->prev != &bpage->list)) |
3e89c7bb SR |
987 | return -1; |
988 | if (RB_WARN_ON(cpu_buffer, | |
044fa782 | 989 | bpage->list.prev->next != &bpage->list)) |
3e89c7bb | 990 | return -1; |
77ae365e SR |
991 | if (rb_check_list(cpu_buffer, &bpage->list)) |
992 | return -1; | |
7a8e76a3 SR |
993 | } |
994 | ||
77ae365e SR |
995 | rb_head_page_activate(cpu_buffer); |
996 | ||
7a8e76a3 SR |
997 | return 0; |
998 | } | |
999 | ||
7a8e76a3 SR |
1000 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
1001 | unsigned nr_pages) | |
1002 | { | |
044fa782 | 1003 | struct buffer_page *bpage, *tmp; |
7a8e76a3 SR |
1004 | unsigned long addr; |
1005 | LIST_HEAD(pages); | |
1006 | unsigned i; | |
1007 | ||
3adc54fa SR |
1008 | WARN_ON(!nr_pages); |
1009 | ||
7a8e76a3 | 1010 | for (i = 0; i < nr_pages; i++) { |
044fa782 | 1011 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
aa1e0e3b | 1012 | GFP_KERNEL, cpu_to_node(cpu_buffer->cpu)); |
044fa782 | 1013 | if (!bpage) |
e4c2ce82 | 1014 | goto free_pages; |
77ae365e SR |
1015 | |
1016 | rb_check_bpage(cpu_buffer, bpage); | |
1017 | ||
044fa782 | 1018 | list_add(&bpage->list, &pages); |
e4c2ce82 | 1019 | |
7a8e76a3 SR |
1020 | addr = __get_free_page(GFP_KERNEL); |
1021 | if (!addr) | |
1022 | goto free_pages; | |
044fa782 SR |
1023 | bpage->page = (void *)addr; |
1024 | rb_init_page(bpage->page); | |
7a8e76a3 SR |
1025 | } |
1026 | ||
3adc54fa SR |
1027 | /* |
1028 | * The ring buffer page list is a circular list that does not | |
1029 | * start and end with a list head. All page list items point to | |
1030 | * other pages. | |
1031 | */ | |
1032 | cpu_buffer->pages = pages.next; | |
1033 | list_del(&pages); | |
7a8e76a3 SR |
1034 | |
1035 | rb_check_pages(cpu_buffer); | |
1036 | ||
1037 | return 0; | |
1038 | ||
1039 | free_pages: | |
044fa782 SR |
1040 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
1041 | list_del_init(&bpage->list); | |
1042 | free_buffer_page(bpage); | |
7a8e76a3 SR |
1043 | } |
1044 | return -ENOMEM; | |
1045 | } | |
1046 | ||
1047 | static struct ring_buffer_per_cpu * | |
1048 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu) | |
1049 | { | |
1050 | struct ring_buffer_per_cpu *cpu_buffer; | |
044fa782 | 1051 | struct buffer_page *bpage; |
d769041f | 1052 | unsigned long addr; |
7a8e76a3 SR |
1053 | int ret; |
1054 | ||
1055 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), | |
1056 | GFP_KERNEL, cpu_to_node(cpu)); | |
1057 | if (!cpu_buffer) | |
1058 | return NULL; | |
1059 | ||
1060 | cpu_buffer->cpu = cpu; | |
1061 | cpu_buffer->buffer = buffer; | |
f83c9d0f | 1062 | spin_lock_init(&cpu_buffer->reader_lock); |
1f8a6a10 | 1063 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
edc35bd7 | 1064 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
7a8e76a3 | 1065 | |
044fa782 | 1066 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
e4c2ce82 | 1067 | GFP_KERNEL, cpu_to_node(cpu)); |
044fa782 | 1068 | if (!bpage) |
e4c2ce82 SR |
1069 | goto fail_free_buffer; |
1070 | ||
77ae365e SR |
1071 | rb_check_bpage(cpu_buffer, bpage); |
1072 | ||
044fa782 | 1073 | cpu_buffer->reader_page = bpage; |
d769041f SR |
1074 | addr = __get_free_page(GFP_KERNEL); |
1075 | if (!addr) | |
e4c2ce82 | 1076 | goto fail_free_reader; |
044fa782 SR |
1077 | bpage->page = (void *)addr; |
1078 | rb_init_page(bpage->page); | |
e4c2ce82 | 1079 | |
d769041f | 1080 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
d769041f | 1081 | |
7a8e76a3 SR |
1082 | ret = rb_allocate_pages(cpu_buffer, buffer->pages); |
1083 | if (ret < 0) | |
d769041f | 1084 | goto fail_free_reader; |
7a8e76a3 SR |
1085 | |
1086 | cpu_buffer->head_page | |
3adc54fa | 1087 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
bf41a158 | 1088 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
7a8e76a3 | 1089 | |
77ae365e SR |
1090 | rb_head_page_activate(cpu_buffer); |
1091 | ||
7a8e76a3 SR |
1092 | return cpu_buffer; |
1093 | ||
d769041f SR |
1094 | fail_free_reader: |
1095 | free_buffer_page(cpu_buffer->reader_page); | |
1096 | ||
7a8e76a3 SR |
1097 | fail_free_buffer: |
1098 | kfree(cpu_buffer); | |
1099 | return NULL; | |
1100 | } | |
1101 | ||
1102 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) | |
1103 | { | |
3adc54fa | 1104 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 1105 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 1106 | |
d769041f SR |
1107 | free_buffer_page(cpu_buffer->reader_page); |
1108 | ||
77ae365e SR |
1109 | rb_head_page_deactivate(cpu_buffer); |
1110 | ||
3adc54fa SR |
1111 | if (head) { |
1112 | list_for_each_entry_safe(bpage, tmp, head, list) { | |
1113 | list_del_init(&bpage->list); | |
1114 | free_buffer_page(bpage); | |
1115 | } | |
1116 | bpage = list_entry(head, struct buffer_page, list); | |
044fa782 | 1117 | free_buffer_page(bpage); |
7a8e76a3 | 1118 | } |
3adc54fa | 1119 | |
7a8e76a3 SR |
1120 | kfree(cpu_buffer); |
1121 | } | |
1122 | ||
59222efe | 1123 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
1124 | static int rb_cpu_notify(struct notifier_block *self, |
1125 | unsigned long action, void *hcpu); | |
554f786e SR |
1126 | #endif |
1127 | ||
7a8e76a3 SR |
1128 | /** |
1129 | * ring_buffer_alloc - allocate a new ring_buffer | |
68814b58 | 1130 | * @size: the size in bytes per cpu that is needed. |
7a8e76a3 SR |
1131 | * @flags: attributes to set for the ring buffer. |
1132 | * | |
1133 | * Currently the only flag that is available is the RB_FL_OVERWRITE | |
1134 | * flag. This flag means that the buffer will overwrite old data | |
1135 | * when the buffer wraps. If this flag is not set, the buffer will | |
1136 | * drop data when the tail hits the head. | |
1137 | */ | |
1f8a6a10 PZ |
1138 | struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
1139 | struct lock_class_key *key) | |
7a8e76a3 SR |
1140 | { |
1141 | struct ring_buffer *buffer; | |
1142 | int bsize; | |
1143 | int cpu; | |
1144 | ||
1145 | /* keep it in its own cache line */ | |
1146 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), | |
1147 | GFP_KERNEL); | |
1148 | if (!buffer) | |
1149 | return NULL; | |
1150 | ||
9e01c1b7 RR |
1151 | if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
1152 | goto fail_free_buffer; | |
1153 | ||
7a8e76a3 SR |
1154 | buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
1155 | buffer->flags = flags; | |
37886f6a | 1156 | buffer->clock = trace_clock_local; |
1f8a6a10 | 1157 | buffer->reader_lock_key = key; |
7a8e76a3 SR |
1158 | |
1159 | /* need at least two pages */ | |
5f78abee SR |
1160 | if (buffer->pages < 2) |
1161 | buffer->pages = 2; | |
7a8e76a3 | 1162 | |
3bf832ce FW |
1163 | /* |
1164 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
1165 | * in early initcall, it will not be notified of secondary cpus. | |
1166 | * In that off case, we need to allocate for all possible cpus. | |
1167 | */ | |
1168 | #ifdef CONFIG_HOTPLUG_CPU | |
554f786e SR |
1169 | get_online_cpus(); |
1170 | cpumask_copy(buffer->cpumask, cpu_online_mask); | |
3bf832ce FW |
1171 | #else |
1172 | cpumask_copy(buffer->cpumask, cpu_possible_mask); | |
1173 | #endif | |
7a8e76a3 SR |
1174 | buffer->cpus = nr_cpu_ids; |
1175 | ||
1176 | bsize = sizeof(void *) * nr_cpu_ids; | |
1177 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), | |
1178 | GFP_KERNEL); | |
1179 | if (!buffer->buffers) | |
9e01c1b7 | 1180 | goto fail_free_cpumask; |
7a8e76a3 SR |
1181 | |
1182 | for_each_buffer_cpu(buffer, cpu) { | |
1183 | buffer->buffers[cpu] = | |
1184 | rb_allocate_cpu_buffer(buffer, cpu); | |
1185 | if (!buffer->buffers[cpu]) | |
1186 | goto fail_free_buffers; | |
1187 | } | |
1188 | ||
59222efe | 1189 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
1190 | buffer->cpu_notify.notifier_call = rb_cpu_notify; |
1191 | buffer->cpu_notify.priority = 0; | |
1192 | register_cpu_notifier(&buffer->cpu_notify); | |
1193 | #endif | |
1194 | ||
1195 | put_online_cpus(); | |
7a8e76a3 SR |
1196 | mutex_init(&buffer->mutex); |
1197 | ||
1198 | return buffer; | |
1199 | ||
1200 | fail_free_buffers: | |
1201 | for_each_buffer_cpu(buffer, cpu) { | |
1202 | if (buffer->buffers[cpu]) | |
1203 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1204 | } | |
1205 | kfree(buffer->buffers); | |
1206 | ||
9e01c1b7 RR |
1207 | fail_free_cpumask: |
1208 | free_cpumask_var(buffer->cpumask); | |
554f786e | 1209 | put_online_cpus(); |
9e01c1b7 | 1210 | |
7a8e76a3 SR |
1211 | fail_free_buffer: |
1212 | kfree(buffer); | |
1213 | return NULL; | |
1214 | } | |
1f8a6a10 | 1215 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
7a8e76a3 SR |
1216 | |
1217 | /** | |
1218 | * ring_buffer_free - free a ring buffer. | |
1219 | * @buffer: the buffer to free. | |
1220 | */ | |
1221 | void | |
1222 | ring_buffer_free(struct ring_buffer *buffer) | |
1223 | { | |
1224 | int cpu; | |
1225 | ||
554f786e SR |
1226 | get_online_cpus(); |
1227 | ||
59222efe | 1228 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
1229 | unregister_cpu_notifier(&buffer->cpu_notify); |
1230 | #endif | |
1231 | ||
7a8e76a3 SR |
1232 | for_each_buffer_cpu(buffer, cpu) |
1233 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1234 | ||
554f786e SR |
1235 | put_online_cpus(); |
1236 | ||
bd3f0221 | 1237 | kfree(buffer->buffers); |
9e01c1b7 RR |
1238 | free_cpumask_var(buffer->cpumask); |
1239 | ||
7a8e76a3 SR |
1240 | kfree(buffer); |
1241 | } | |
c4f50183 | 1242 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
7a8e76a3 | 1243 | |
37886f6a SR |
1244 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
1245 | u64 (*clock)(void)) | |
1246 | { | |
1247 | buffer->clock = clock; | |
1248 | } | |
1249 | ||
7a8e76a3 SR |
1250 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
1251 | ||
1252 | static void | |
1253 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages) | |
1254 | { | |
044fa782 | 1255 | struct buffer_page *bpage; |
7a8e76a3 SR |
1256 | struct list_head *p; |
1257 | unsigned i; | |
1258 | ||
f7112949 | 1259 | spin_lock_irq(&cpu_buffer->reader_lock); |
77ae365e SR |
1260 | rb_head_page_deactivate(cpu_buffer); |
1261 | ||
7a8e76a3 | 1262 | for (i = 0; i < nr_pages; i++) { |
3adc54fa | 1263 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
292f60c0 | 1264 | goto out; |
3adc54fa | 1265 | p = cpu_buffer->pages->next; |
044fa782 SR |
1266 | bpage = list_entry(p, struct buffer_page, list); |
1267 | list_del_init(&bpage->list); | |
1268 | free_buffer_page(bpage); | |
7a8e76a3 | 1269 | } |
3adc54fa | 1270 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
292f60c0 | 1271 | goto out; |
7a8e76a3 SR |
1272 | |
1273 | rb_reset_cpu(cpu_buffer); | |
7a8e76a3 SR |
1274 | rb_check_pages(cpu_buffer); |
1275 | ||
292f60c0 | 1276 | out: |
dd7f5943 | 1277 | spin_unlock_irq(&cpu_buffer->reader_lock); |
7a8e76a3 SR |
1278 | } |
1279 | ||
1280 | static void | |
1281 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer, | |
1282 | struct list_head *pages, unsigned nr_pages) | |
1283 | { | |
044fa782 | 1284 | struct buffer_page *bpage; |
7a8e76a3 SR |
1285 | struct list_head *p; |
1286 | unsigned i; | |
1287 | ||
77ae365e SR |
1288 | spin_lock_irq(&cpu_buffer->reader_lock); |
1289 | rb_head_page_deactivate(cpu_buffer); | |
1290 | ||
7a8e76a3 | 1291 | for (i = 0; i < nr_pages; i++) { |
3e89c7bb | 1292 | if (RB_WARN_ON(cpu_buffer, list_empty(pages))) |
292f60c0 | 1293 | goto out; |
7a8e76a3 | 1294 | p = pages->next; |
044fa782 SR |
1295 | bpage = list_entry(p, struct buffer_page, list); |
1296 | list_del_init(&bpage->list); | |
3adc54fa | 1297 | list_add_tail(&bpage->list, cpu_buffer->pages); |
7a8e76a3 SR |
1298 | } |
1299 | rb_reset_cpu(cpu_buffer); | |
7a8e76a3 SR |
1300 | rb_check_pages(cpu_buffer); |
1301 | ||
292f60c0 | 1302 | out: |
dd7f5943 | 1303 | spin_unlock_irq(&cpu_buffer->reader_lock); |
7a8e76a3 SR |
1304 | } |
1305 | ||
1306 | /** | |
1307 | * ring_buffer_resize - resize the ring buffer | |
1308 | * @buffer: the buffer to resize. | |
1309 | * @size: the new size. | |
1310 | * | |
7a8e76a3 SR |
1311 | * Minimum size is 2 * BUF_PAGE_SIZE. |
1312 | * | |
1313 | * Returns -1 on failure. | |
1314 | */ | |
1315 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size) | |
1316 | { | |
1317 | struct ring_buffer_per_cpu *cpu_buffer; | |
1318 | unsigned nr_pages, rm_pages, new_pages; | |
044fa782 | 1319 | struct buffer_page *bpage, *tmp; |
7a8e76a3 SR |
1320 | unsigned long buffer_size; |
1321 | unsigned long addr; | |
1322 | LIST_HEAD(pages); | |
1323 | int i, cpu; | |
1324 | ||
ee51a1de IM |
1325 | /* |
1326 | * Always succeed at resizing a non-existent buffer: | |
1327 | */ | |
1328 | if (!buffer) | |
1329 | return size; | |
1330 | ||
7a8e76a3 SR |
1331 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
1332 | size *= BUF_PAGE_SIZE; | |
1333 | buffer_size = buffer->pages * BUF_PAGE_SIZE; | |
1334 | ||
1335 | /* we need a minimum of two pages */ | |
1336 | if (size < BUF_PAGE_SIZE * 2) | |
1337 | size = BUF_PAGE_SIZE * 2; | |
1338 | ||
1339 | if (size == buffer_size) | |
1340 | return size; | |
1341 | ||
18421015 SR |
1342 | atomic_inc(&buffer->record_disabled); |
1343 | ||
1344 | /* Make sure all writers are done with this buffer. */ | |
1345 | synchronize_sched(); | |
1346 | ||
7a8e76a3 | 1347 | mutex_lock(&buffer->mutex); |
554f786e | 1348 | get_online_cpus(); |
7a8e76a3 SR |
1349 | |
1350 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); | |
1351 | ||
1352 | if (size < buffer_size) { | |
1353 | ||
1354 | /* easy case, just free pages */ | |
554f786e SR |
1355 | if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) |
1356 | goto out_fail; | |
7a8e76a3 SR |
1357 | |
1358 | rm_pages = buffer->pages - nr_pages; | |
1359 | ||
1360 | for_each_buffer_cpu(buffer, cpu) { | |
1361 | cpu_buffer = buffer->buffers[cpu]; | |
1362 | rb_remove_pages(cpu_buffer, rm_pages); | |
1363 | } | |
1364 | goto out; | |
1365 | } | |
1366 | ||
1367 | /* | |
1368 | * This is a bit more difficult. We only want to add pages | |
1369 | * when we can allocate enough for all CPUs. We do this | |
1370 | * by allocating all the pages and storing them on a local | |
1371 | * link list. If we succeed in our allocation, then we | |
1372 | * add these pages to the cpu_buffers. Otherwise we just free | |
1373 | * them all and return -ENOMEM; | |
1374 | */ | |
554f786e SR |
1375 | if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) |
1376 | goto out_fail; | |
f536aafc | 1377 | |
7a8e76a3 SR |
1378 | new_pages = nr_pages - buffer->pages; |
1379 | ||
1380 | for_each_buffer_cpu(buffer, cpu) { | |
1381 | for (i = 0; i < new_pages; i++) { | |
044fa782 | 1382 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), |
e4c2ce82 SR |
1383 | cache_line_size()), |
1384 | GFP_KERNEL, cpu_to_node(cpu)); | |
044fa782 | 1385 | if (!bpage) |
e4c2ce82 | 1386 | goto free_pages; |
044fa782 | 1387 | list_add(&bpage->list, &pages); |
7a8e76a3 SR |
1388 | addr = __get_free_page(GFP_KERNEL); |
1389 | if (!addr) | |
1390 | goto free_pages; | |
044fa782 SR |
1391 | bpage->page = (void *)addr; |
1392 | rb_init_page(bpage->page); | |
7a8e76a3 SR |
1393 | } |
1394 | } | |
1395 | ||
1396 | for_each_buffer_cpu(buffer, cpu) { | |
1397 | cpu_buffer = buffer->buffers[cpu]; | |
1398 | rb_insert_pages(cpu_buffer, &pages, new_pages); | |
1399 | } | |
1400 | ||
554f786e SR |
1401 | if (RB_WARN_ON(buffer, !list_empty(&pages))) |
1402 | goto out_fail; | |
7a8e76a3 SR |
1403 | |
1404 | out: | |
1405 | buffer->pages = nr_pages; | |
554f786e | 1406 | put_online_cpus(); |
7a8e76a3 SR |
1407 | mutex_unlock(&buffer->mutex); |
1408 | ||
18421015 SR |
1409 | atomic_dec(&buffer->record_disabled); |
1410 | ||
7a8e76a3 SR |
1411 | return size; |
1412 | ||
1413 | free_pages: | |
044fa782 SR |
1414 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
1415 | list_del_init(&bpage->list); | |
1416 | free_buffer_page(bpage); | |
7a8e76a3 | 1417 | } |
554f786e | 1418 | put_online_cpus(); |
641d2f63 | 1419 | mutex_unlock(&buffer->mutex); |
18421015 | 1420 | atomic_dec(&buffer->record_disabled); |
7a8e76a3 | 1421 | return -ENOMEM; |
554f786e SR |
1422 | |
1423 | /* | |
1424 | * Something went totally wrong, and we are too paranoid | |
1425 | * to even clean up the mess. | |
1426 | */ | |
1427 | out_fail: | |
1428 | put_online_cpus(); | |
1429 | mutex_unlock(&buffer->mutex); | |
18421015 | 1430 | atomic_dec(&buffer->record_disabled); |
554f786e | 1431 | return -1; |
7a8e76a3 | 1432 | } |
c4f50183 | 1433 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
7a8e76a3 | 1434 | |
8789a9e7 | 1435 | static inline void * |
044fa782 | 1436 | __rb_data_page_index(struct buffer_data_page *bpage, unsigned index) |
8789a9e7 | 1437 | { |
044fa782 | 1438 | return bpage->data + index; |
8789a9e7 SR |
1439 | } |
1440 | ||
044fa782 | 1441 | static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
7a8e76a3 | 1442 | { |
044fa782 | 1443 | return bpage->page->data + index; |
7a8e76a3 SR |
1444 | } |
1445 | ||
1446 | static inline struct ring_buffer_event * | |
d769041f | 1447 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1448 | { |
6f807acd SR |
1449 | return __rb_page_index(cpu_buffer->reader_page, |
1450 | cpu_buffer->reader_page->read); | |
1451 | } | |
1452 | ||
7a8e76a3 SR |
1453 | static inline struct ring_buffer_event * |
1454 | rb_iter_head_event(struct ring_buffer_iter *iter) | |
1455 | { | |
6f807acd | 1456 | return __rb_page_index(iter->head_page, iter->head); |
7a8e76a3 SR |
1457 | } |
1458 | ||
77ae365e | 1459 | static inline unsigned long rb_page_write(struct buffer_page *bpage) |
bf41a158 | 1460 | { |
77ae365e | 1461 | return local_read(&bpage->write) & RB_WRITE_MASK; |
bf41a158 SR |
1462 | } |
1463 | ||
1464 | static inline unsigned rb_page_commit(struct buffer_page *bpage) | |
1465 | { | |
abc9b56d | 1466 | return local_read(&bpage->page->commit); |
bf41a158 SR |
1467 | } |
1468 | ||
77ae365e SR |
1469 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
1470 | { | |
1471 | return local_read(&bpage->entries) & RB_WRITE_MASK; | |
1472 | } | |
1473 | ||
bf41a158 SR |
1474 | /* Size is determined by what has been commited */ |
1475 | static inline unsigned rb_page_size(struct buffer_page *bpage) | |
1476 | { | |
1477 | return rb_page_commit(bpage); | |
1478 | } | |
1479 | ||
1480 | static inline unsigned | |
1481 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) | |
1482 | { | |
1483 | return rb_page_commit(cpu_buffer->commit_page); | |
1484 | } | |
1485 | ||
bf41a158 SR |
1486 | static inline unsigned |
1487 | rb_event_index(struct ring_buffer_event *event) | |
1488 | { | |
1489 | unsigned long addr = (unsigned long)event; | |
1490 | ||
22f470f8 | 1491 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
bf41a158 SR |
1492 | } |
1493 | ||
0f0c85fc | 1494 | static inline int |
fa743953 SR |
1495 | rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
1496 | struct ring_buffer_event *event) | |
bf41a158 SR |
1497 | { |
1498 | unsigned long addr = (unsigned long)event; | |
1499 | unsigned long index; | |
1500 | ||
1501 | index = rb_event_index(event); | |
1502 | addr &= PAGE_MASK; | |
1503 | ||
1504 | return cpu_buffer->commit_page->page == (void *)addr && | |
1505 | rb_commit_index(cpu_buffer) == index; | |
1506 | } | |
1507 | ||
34a148bf | 1508 | static void |
bf41a158 | 1509 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1510 | { |
77ae365e SR |
1511 | unsigned long max_count; |
1512 | ||
bf41a158 SR |
1513 | /* |
1514 | * We only race with interrupts and NMIs on this CPU. | |
1515 | * If we own the commit event, then we can commit | |
1516 | * all others that interrupted us, since the interruptions | |
1517 | * are in stack format (they finish before they come | |
1518 | * back to us). This allows us to do a simple loop to | |
1519 | * assign the commit to the tail. | |
1520 | */ | |
a8ccf1d6 | 1521 | again: |
77ae365e SR |
1522 | max_count = cpu_buffer->buffer->pages * 100; |
1523 | ||
bf41a158 | 1524 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
77ae365e SR |
1525 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
1526 | return; | |
1527 | if (RB_WARN_ON(cpu_buffer, | |
1528 | rb_is_reader_page(cpu_buffer->tail_page))) | |
1529 | return; | |
1530 | local_set(&cpu_buffer->commit_page->page->commit, | |
1531 | rb_page_write(cpu_buffer->commit_page)); | |
bf41a158 | 1532 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
abc9b56d SR |
1533 | cpu_buffer->write_stamp = |
1534 | cpu_buffer->commit_page->page->time_stamp; | |
bf41a158 SR |
1535 | /* add barrier to keep gcc from optimizing too much */ |
1536 | barrier(); | |
1537 | } | |
1538 | while (rb_commit_index(cpu_buffer) != | |
1539 | rb_page_write(cpu_buffer->commit_page)) { | |
77ae365e SR |
1540 | |
1541 | local_set(&cpu_buffer->commit_page->page->commit, | |
1542 | rb_page_write(cpu_buffer->commit_page)); | |
1543 | RB_WARN_ON(cpu_buffer, | |
1544 | local_read(&cpu_buffer->commit_page->page->commit) & | |
1545 | ~RB_WRITE_MASK); | |
bf41a158 SR |
1546 | barrier(); |
1547 | } | |
a8ccf1d6 SR |
1548 | |
1549 | /* again, keep gcc from optimizing */ | |
1550 | barrier(); | |
1551 | ||
1552 | /* | |
1553 | * If an interrupt came in just after the first while loop | |
1554 | * and pushed the tail page forward, we will be left with | |
1555 | * a dangling commit that will never go forward. | |
1556 | */ | |
1557 | if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page)) | |
1558 | goto again; | |
7a8e76a3 SR |
1559 | } |
1560 | ||
d769041f | 1561 | static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1562 | { |
abc9b56d | 1563 | cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; |
6f807acd | 1564 | cpu_buffer->reader_page->read = 0; |
d769041f SR |
1565 | } |
1566 | ||
34a148bf | 1567 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
d769041f SR |
1568 | { |
1569 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
1570 | ||
1571 | /* | |
1572 | * The iterator could be on the reader page (it starts there). | |
1573 | * But the head could have moved, since the reader was | |
1574 | * found. Check for this case and assign the iterator | |
1575 | * to the head page instead of next. | |
1576 | */ | |
1577 | if (iter->head_page == cpu_buffer->reader_page) | |
77ae365e | 1578 | iter->head_page = rb_set_head_page(cpu_buffer); |
d769041f SR |
1579 | else |
1580 | rb_inc_page(cpu_buffer, &iter->head_page); | |
1581 | ||
abc9b56d | 1582 | iter->read_stamp = iter->head_page->page->time_stamp; |
7a8e76a3 SR |
1583 | iter->head = 0; |
1584 | } | |
1585 | ||
69d1b839 SR |
1586 | /* Slow path, do not inline */ |
1587 | static noinline struct ring_buffer_event * | |
1588 | rb_add_time_stamp(struct ring_buffer_event *event, u64 delta) | |
1589 | { | |
1590 | event->type_len = RINGBUF_TYPE_TIME_EXTEND; | |
1591 | ||
1592 | /* Not the first event on the page? */ | |
1593 | if (rb_event_index(event)) { | |
1594 | event->time_delta = delta & TS_MASK; | |
1595 | event->array[0] = delta >> TS_SHIFT; | |
1596 | } else { | |
1597 | /* nope, just zero it */ | |
1598 | event->time_delta = 0; | |
1599 | event->array[0] = 0; | |
1600 | } | |
1601 | ||
1602 | return skip_time_extend(event); | |
1603 | } | |
1604 | ||
7a8e76a3 SR |
1605 | /** |
1606 | * ring_buffer_update_event - update event type and data | |
1607 | * @event: the even to update | |
1608 | * @type: the type of event | |
1609 | * @length: the size of the event field in the ring buffer | |
1610 | * | |
1611 | * Update the type and data fields of the event. The length | |
1612 | * is the actual size that is written to the ring buffer, | |
1613 | * and with this, we can determine what to place into the | |
1614 | * data field. | |
1615 | */ | |
34a148bf | 1616 | static void |
69d1b839 SR |
1617 | rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, |
1618 | struct ring_buffer_event *event, unsigned length, | |
1619 | int add_timestamp, u64 delta) | |
7a8e76a3 | 1620 | { |
69d1b839 SR |
1621 | /* Only a commit updates the timestamp */ |
1622 | if (unlikely(!rb_event_is_commit(cpu_buffer, event))) | |
1623 | delta = 0; | |
7a8e76a3 | 1624 | |
69d1b839 SR |
1625 | /* |
1626 | * If we need to add a timestamp, then we | |
1627 | * add it to the start of the resevered space. | |
1628 | */ | |
1629 | if (unlikely(add_timestamp)) { | |
1630 | event = rb_add_time_stamp(event, delta); | |
1631 | length -= RB_LEN_TIME_EXTEND; | |
1632 | delta = 0; | |
7a8e76a3 | 1633 | } |
69d1b839 SR |
1634 | |
1635 | event->time_delta = delta; | |
1636 | length -= RB_EVNT_HDR_SIZE; | |
1637 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) { | |
1638 | event->type_len = 0; | |
1639 | event->array[0] = length; | |
1640 | } else | |
1641 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); | |
7a8e76a3 SR |
1642 | } |
1643 | ||
77ae365e SR |
1644 | /* |
1645 | * rb_handle_head_page - writer hit the head page | |
1646 | * | |
1647 | * Returns: +1 to retry page | |
1648 | * 0 to continue | |
1649 | * -1 on error | |
1650 | */ | |
1651 | static int | |
1652 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, | |
1653 | struct buffer_page *tail_page, | |
1654 | struct buffer_page *next_page) | |
1655 | { | |
1656 | struct buffer_page *new_head; | |
1657 | int entries; | |
1658 | int type; | |
1659 | int ret; | |
1660 | ||
1661 | entries = rb_page_entries(next_page); | |
1662 | ||
1663 | /* | |
1664 | * The hard part is here. We need to move the head | |
1665 | * forward, and protect against both readers on | |
1666 | * other CPUs and writers coming in via interrupts. | |
1667 | */ | |
1668 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, | |
1669 | RB_PAGE_HEAD); | |
1670 | ||
1671 | /* | |
1672 | * type can be one of four: | |
1673 | * NORMAL - an interrupt already moved it for us | |
1674 | * HEAD - we are the first to get here. | |
1675 | * UPDATE - we are the interrupt interrupting | |
1676 | * a current move. | |
1677 | * MOVED - a reader on another CPU moved the next | |
1678 | * pointer to its reader page. Give up | |
1679 | * and try again. | |
1680 | */ | |
1681 | ||
1682 | switch (type) { | |
1683 | case RB_PAGE_HEAD: | |
1684 | /* | |
1685 | * We changed the head to UPDATE, thus | |
1686 | * it is our responsibility to update | |
1687 | * the counters. | |
1688 | */ | |
1689 | local_add(entries, &cpu_buffer->overrun); | |
1690 | ||
1691 | /* | |
1692 | * The entries will be zeroed out when we move the | |
1693 | * tail page. | |
1694 | */ | |
1695 | ||
1696 | /* still more to do */ | |
1697 | break; | |
1698 | ||
1699 | case RB_PAGE_UPDATE: | |
1700 | /* | |
1701 | * This is an interrupt that interrupt the | |
1702 | * previous update. Still more to do. | |
1703 | */ | |
1704 | break; | |
1705 | case RB_PAGE_NORMAL: | |
1706 | /* | |
1707 | * An interrupt came in before the update | |
1708 | * and processed this for us. | |
1709 | * Nothing left to do. | |
1710 | */ | |
1711 | return 1; | |
1712 | case RB_PAGE_MOVED: | |
1713 | /* | |
1714 | * The reader is on another CPU and just did | |
1715 | * a swap with our next_page. | |
1716 | * Try again. | |
1717 | */ | |
1718 | return 1; | |
1719 | default: | |
1720 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ | |
1721 | return -1; | |
1722 | } | |
1723 | ||
1724 | /* | |
1725 | * Now that we are here, the old head pointer is | |
1726 | * set to UPDATE. This will keep the reader from | |
1727 | * swapping the head page with the reader page. | |
1728 | * The reader (on another CPU) will spin till | |
1729 | * we are finished. | |
1730 | * | |
1731 | * We just need to protect against interrupts | |
1732 | * doing the job. We will set the next pointer | |
1733 | * to HEAD. After that, we set the old pointer | |
1734 | * to NORMAL, but only if it was HEAD before. | |
1735 | * otherwise we are an interrupt, and only | |
1736 | * want the outer most commit to reset it. | |
1737 | */ | |
1738 | new_head = next_page; | |
1739 | rb_inc_page(cpu_buffer, &new_head); | |
1740 | ||
1741 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, | |
1742 | RB_PAGE_NORMAL); | |
1743 | ||
1744 | /* | |
1745 | * Valid returns are: | |
1746 | * HEAD - an interrupt came in and already set it. | |
1747 | * NORMAL - One of two things: | |
1748 | * 1) We really set it. | |
1749 | * 2) A bunch of interrupts came in and moved | |
1750 | * the page forward again. | |
1751 | */ | |
1752 | switch (ret) { | |
1753 | case RB_PAGE_HEAD: | |
1754 | case RB_PAGE_NORMAL: | |
1755 | /* OK */ | |
1756 | break; | |
1757 | default: | |
1758 | RB_WARN_ON(cpu_buffer, 1); | |
1759 | return -1; | |
1760 | } | |
1761 | ||
1762 | /* | |
1763 | * It is possible that an interrupt came in, | |
1764 | * set the head up, then more interrupts came in | |
1765 | * and moved it again. When we get back here, | |
1766 | * the page would have been set to NORMAL but we | |
1767 | * just set it back to HEAD. | |
1768 | * | |
1769 | * How do you detect this? Well, if that happened | |
1770 | * the tail page would have moved. | |
1771 | */ | |
1772 | if (ret == RB_PAGE_NORMAL) { | |
1773 | /* | |
1774 | * If the tail had moved passed next, then we need | |
1775 | * to reset the pointer. | |
1776 | */ | |
1777 | if (cpu_buffer->tail_page != tail_page && | |
1778 | cpu_buffer->tail_page != next_page) | |
1779 | rb_head_page_set_normal(cpu_buffer, new_head, | |
1780 | next_page, | |
1781 | RB_PAGE_HEAD); | |
1782 | } | |
1783 | ||
1784 | /* | |
1785 | * If this was the outer most commit (the one that | |
1786 | * changed the original pointer from HEAD to UPDATE), | |
1787 | * then it is up to us to reset it to NORMAL. | |
1788 | */ | |
1789 | if (type == RB_PAGE_HEAD) { | |
1790 | ret = rb_head_page_set_normal(cpu_buffer, next_page, | |
1791 | tail_page, | |
1792 | RB_PAGE_UPDATE); | |
1793 | if (RB_WARN_ON(cpu_buffer, | |
1794 | ret != RB_PAGE_UPDATE)) | |
1795 | return -1; | |
1796 | } | |
1797 | ||
1798 | return 0; | |
1799 | } | |
1800 | ||
34a148bf | 1801 | static unsigned rb_calculate_event_length(unsigned length) |
7a8e76a3 SR |
1802 | { |
1803 | struct ring_buffer_event event; /* Used only for sizeof array */ | |
1804 | ||
1805 | /* zero length can cause confusions */ | |
1806 | if (!length) | |
1807 | length = 1; | |
1808 | ||
2271048d | 1809 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
7a8e76a3 SR |
1810 | length += sizeof(event.array[0]); |
1811 | ||
1812 | length += RB_EVNT_HDR_SIZE; | |
2271048d | 1813 | length = ALIGN(length, RB_ARCH_ALIGNMENT); |
7a8e76a3 SR |
1814 | |
1815 | return length; | |
1816 | } | |
1817 | ||
c7b09308 SR |
1818 | static inline void |
1819 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, | |
1820 | struct buffer_page *tail_page, | |
1821 | unsigned long tail, unsigned long length) | |
1822 | { | |
1823 | struct ring_buffer_event *event; | |
1824 | ||
1825 | /* | |
1826 | * Only the event that crossed the page boundary | |
1827 | * must fill the old tail_page with padding. | |
1828 | */ | |
1829 | if (tail >= BUF_PAGE_SIZE) { | |
b3230c8b SR |
1830 | /* |
1831 | * If the page was filled, then we still need | |
1832 | * to update the real_end. Reset it to zero | |
1833 | * and the reader will ignore it. | |
1834 | */ | |
1835 | if (tail == BUF_PAGE_SIZE) | |
1836 | tail_page->real_end = 0; | |
1837 | ||
c7b09308 SR |
1838 | local_sub(length, &tail_page->write); |
1839 | return; | |
1840 | } | |
1841 | ||
1842 | event = __rb_page_index(tail_page, tail); | |
b0b7065b | 1843 | kmemcheck_annotate_bitfield(event, bitfield); |
c7b09308 | 1844 | |
ff0ff84a SR |
1845 | /* |
1846 | * Save the original length to the meta data. | |
1847 | * This will be used by the reader to add lost event | |
1848 | * counter. | |
1849 | */ | |
1850 | tail_page->real_end = tail; | |
1851 | ||
c7b09308 SR |
1852 | /* |
1853 | * If this event is bigger than the minimum size, then | |
1854 | * we need to be careful that we don't subtract the | |
1855 | * write counter enough to allow another writer to slip | |
1856 | * in on this page. | |
1857 | * We put in a discarded commit instead, to make sure | |
1858 | * that this space is not used again. | |
1859 | * | |
1860 | * If we are less than the minimum size, we don't need to | |
1861 | * worry about it. | |
1862 | */ | |
1863 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { | |
1864 | /* No room for any events */ | |
1865 | ||
1866 | /* Mark the rest of the page with padding */ | |
1867 | rb_event_set_padding(event); | |
1868 | ||
1869 | /* Set the write back to the previous setting */ | |
1870 | local_sub(length, &tail_page->write); | |
1871 | return; | |
1872 | } | |
1873 | ||
1874 | /* Put in a discarded event */ | |
1875 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; | |
1876 | event->type_len = RINGBUF_TYPE_PADDING; | |
1877 | /* time delta must be non zero */ | |
1878 | event->time_delta = 1; | |
c7b09308 SR |
1879 | |
1880 | /* Set write to end of buffer */ | |
1881 | length = (tail + length) - BUF_PAGE_SIZE; | |
1882 | local_sub(length, &tail_page->write); | |
1883 | } | |
6634ff26 | 1884 | |
747e94ae SR |
1885 | /* |
1886 | * This is the slow path, force gcc not to inline it. | |
1887 | */ | |
1888 | static noinline struct ring_buffer_event * | |
6634ff26 SR |
1889 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
1890 | unsigned long length, unsigned long tail, | |
e8bc43e8 | 1891 | struct buffer_page *tail_page, u64 ts) |
7a8e76a3 | 1892 | { |
5a50e33c | 1893 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
7a8e76a3 | 1894 | struct ring_buffer *buffer = cpu_buffer->buffer; |
77ae365e SR |
1895 | struct buffer_page *next_page; |
1896 | int ret; | |
aa20ae84 SR |
1897 | |
1898 | next_page = tail_page; | |
1899 | ||
aa20ae84 SR |
1900 | rb_inc_page(cpu_buffer, &next_page); |
1901 | ||
aa20ae84 SR |
1902 | /* |
1903 | * If for some reason, we had an interrupt storm that made | |
1904 | * it all the way around the buffer, bail, and warn | |
1905 | * about it. | |
1906 | */ | |
1907 | if (unlikely(next_page == commit_page)) { | |
77ae365e | 1908 | local_inc(&cpu_buffer->commit_overrun); |
aa20ae84 SR |
1909 | goto out_reset; |
1910 | } | |
1911 | ||
77ae365e SR |
1912 | /* |
1913 | * This is where the fun begins! | |
1914 | * | |
1915 | * We are fighting against races between a reader that | |
1916 | * could be on another CPU trying to swap its reader | |
1917 | * page with the buffer head. | |
1918 | * | |
1919 | * We are also fighting against interrupts coming in and | |
1920 | * moving the head or tail on us as well. | |
1921 | * | |
1922 | * If the next page is the head page then we have filled | |
1923 | * the buffer, unless the commit page is still on the | |
1924 | * reader page. | |
1925 | */ | |
1926 | if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) { | |
aa20ae84 | 1927 | |
77ae365e SR |
1928 | /* |
1929 | * If the commit is not on the reader page, then | |
1930 | * move the header page. | |
1931 | */ | |
1932 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { | |
1933 | /* | |
1934 | * If we are not in overwrite mode, | |
1935 | * this is easy, just stop here. | |
1936 | */ | |
1937 | if (!(buffer->flags & RB_FL_OVERWRITE)) | |
1938 | goto out_reset; | |
1939 | ||
1940 | ret = rb_handle_head_page(cpu_buffer, | |
1941 | tail_page, | |
1942 | next_page); | |
1943 | if (ret < 0) | |
1944 | goto out_reset; | |
1945 | if (ret) | |
1946 | goto out_again; | |
1947 | } else { | |
1948 | /* | |
1949 | * We need to be careful here too. The | |
1950 | * commit page could still be on the reader | |
1951 | * page. We could have a small buffer, and | |
1952 | * have filled up the buffer with events | |
1953 | * from interrupts and such, and wrapped. | |
1954 | * | |
1955 | * Note, if the tail page is also the on the | |
1956 | * reader_page, we let it move out. | |
1957 | */ | |
1958 | if (unlikely((cpu_buffer->commit_page != | |
1959 | cpu_buffer->tail_page) && | |
1960 | (cpu_buffer->commit_page == | |
1961 | cpu_buffer->reader_page))) { | |
1962 | local_inc(&cpu_buffer->commit_overrun); | |
1963 | goto out_reset; | |
1964 | } | |
aa20ae84 SR |
1965 | } |
1966 | } | |
1967 | ||
77ae365e SR |
1968 | ret = rb_tail_page_update(cpu_buffer, tail_page, next_page); |
1969 | if (ret) { | |
1970 | /* | |
1971 | * Nested commits always have zero deltas, so | |
1972 | * just reread the time stamp | |
1973 | */ | |
e8bc43e8 SR |
1974 | ts = rb_time_stamp(buffer); |
1975 | next_page->page->time_stamp = ts; | |
aa20ae84 SR |
1976 | } |
1977 | ||
77ae365e | 1978 | out_again: |
aa20ae84 | 1979 | |
77ae365e | 1980 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
aa20ae84 SR |
1981 | |
1982 | /* fail and let the caller try again */ | |
1983 | return ERR_PTR(-EAGAIN); | |
1984 | ||
45141d46 | 1985 | out_reset: |
6f3b3440 | 1986 | /* reset write */ |
c7b09308 | 1987 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
6f3b3440 | 1988 | |
bf41a158 | 1989 | return NULL; |
7a8e76a3 SR |
1990 | } |
1991 | ||
6634ff26 SR |
1992 | static struct ring_buffer_event * |
1993 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, | |
69d1b839 SR |
1994 | unsigned long length, u64 ts, |
1995 | u64 delta, int add_timestamp) | |
6634ff26 | 1996 | { |
5a50e33c | 1997 | struct buffer_page *tail_page; |
6634ff26 SR |
1998 | struct ring_buffer_event *event; |
1999 | unsigned long tail, write; | |
2000 | ||
69d1b839 SR |
2001 | /* |
2002 | * If the time delta since the last event is too big to | |
2003 | * hold in the time field of the event, then we append a | |
2004 | * TIME EXTEND event ahead of the data event. | |
2005 | */ | |
2006 | if (unlikely(add_timestamp)) | |
2007 | length += RB_LEN_TIME_EXTEND; | |
2008 | ||
6634ff26 SR |
2009 | tail_page = cpu_buffer->tail_page; |
2010 | write = local_add_return(length, &tail_page->write); | |
77ae365e SR |
2011 | |
2012 | /* set write to only the index of the write */ | |
2013 | write &= RB_WRITE_MASK; | |
6634ff26 SR |
2014 | tail = write - length; |
2015 | ||
2016 | /* See if we shot pass the end of this buffer page */ | |
747e94ae | 2017 | if (unlikely(write > BUF_PAGE_SIZE)) |
6634ff26 | 2018 | return rb_move_tail(cpu_buffer, length, tail, |
5a50e33c | 2019 | tail_page, ts); |
6634ff26 SR |
2020 | |
2021 | /* We reserved something on the buffer */ | |
2022 | ||
6634ff26 | 2023 | event = __rb_page_index(tail_page, tail); |
1744a21d | 2024 | kmemcheck_annotate_bitfield(event, bitfield); |
69d1b839 | 2025 | rb_update_event(cpu_buffer, event, length, add_timestamp, delta); |
6634ff26 | 2026 | |
69d1b839 | 2027 | local_inc(&tail_page->entries); |
6634ff26 SR |
2028 | |
2029 | /* | |
fa743953 SR |
2030 | * If this is the first commit on the page, then update |
2031 | * its timestamp. | |
6634ff26 | 2032 | */ |
fa743953 | 2033 | if (!tail) |
e8bc43e8 | 2034 | tail_page->page->time_stamp = ts; |
6634ff26 SR |
2035 | |
2036 | return event; | |
2037 | } | |
2038 | ||
edd813bf SR |
2039 | static inline int |
2040 | rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, | |
2041 | struct ring_buffer_event *event) | |
2042 | { | |
2043 | unsigned long new_index, old_index; | |
2044 | struct buffer_page *bpage; | |
2045 | unsigned long index; | |
2046 | unsigned long addr; | |
2047 | ||
2048 | new_index = rb_event_index(event); | |
69d1b839 | 2049 | old_index = new_index + rb_event_ts_length(event); |
edd813bf SR |
2050 | addr = (unsigned long)event; |
2051 | addr &= PAGE_MASK; | |
2052 | ||
2053 | bpage = cpu_buffer->tail_page; | |
2054 | ||
2055 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { | |
77ae365e SR |
2056 | unsigned long write_mask = |
2057 | local_read(&bpage->write) & ~RB_WRITE_MASK; | |
edd813bf SR |
2058 | /* |
2059 | * This is on the tail page. It is possible that | |
2060 | * a write could come in and move the tail page | |
2061 | * and write to the next page. That is fine | |
2062 | * because we just shorten what is on this page. | |
2063 | */ | |
77ae365e SR |
2064 | old_index += write_mask; |
2065 | new_index += write_mask; | |
edd813bf SR |
2066 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
2067 | if (index == old_index) | |
2068 | return 1; | |
2069 | } | |
2070 | ||
2071 | /* could not discard */ | |
2072 | return 0; | |
2073 | } | |
2074 | ||
fa743953 SR |
2075 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
2076 | { | |
2077 | local_inc(&cpu_buffer->committing); | |
2078 | local_inc(&cpu_buffer->commits); | |
2079 | } | |
2080 | ||
2081 | static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) | |
2082 | { | |
2083 | unsigned long commits; | |
2084 | ||
2085 | if (RB_WARN_ON(cpu_buffer, | |
2086 | !local_read(&cpu_buffer->committing))) | |
2087 | return; | |
2088 | ||
2089 | again: | |
2090 | commits = local_read(&cpu_buffer->commits); | |
2091 | /* synchronize with interrupts */ | |
2092 | barrier(); | |
2093 | if (local_read(&cpu_buffer->committing) == 1) | |
2094 | rb_set_commit_to_write(cpu_buffer); | |
2095 | ||
2096 | local_dec(&cpu_buffer->committing); | |
2097 | ||
2098 | /* synchronize with interrupts */ | |
2099 | barrier(); | |
2100 | ||
2101 | /* | |
2102 | * Need to account for interrupts coming in between the | |
2103 | * updating of the commit page and the clearing of the | |
2104 | * committing counter. | |
2105 | */ | |
2106 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && | |
2107 | !local_read(&cpu_buffer->committing)) { | |
2108 | local_inc(&cpu_buffer->committing); | |
2109 | goto again; | |
2110 | } | |
2111 | } | |
2112 | ||
7a8e76a3 | 2113 | static struct ring_buffer_event * |
62f0b3eb SR |
2114 | rb_reserve_next_event(struct ring_buffer *buffer, |
2115 | struct ring_buffer_per_cpu *cpu_buffer, | |
1cd8d735 | 2116 | unsigned long length) |
7a8e76a3 SR |
2117 | { |
2118 | struct ring_buffer_event *event; | |
69d1b839 | 2119 | u64 ts, delta; |
818e3dd3 | 2120 | int nr_loops = 0; |
69d1b839 | 2121 | int add_timestamp; |
140ff891 | 2122 | u64 diff; |
7a8e76a3 | 2123 | |
fa743953 SR |
2124 | rb_start_commit(cpu_buffer); |
2125 | ||
85bac32c | 2126 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
62f0b3eb SR |
2127 | /* |
2128 | * Due to the ability to swap a cpu buffer from a buffer | |
2129 | * it is possible it was swapped before we committed. | |
2130 | * (committing stops a swap). We check for it here and | |
2131 | * if it happened, we have to fail the write. | |
2132 | */ | |
2133 | barrier(); | |
2134 | if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) { | |
2135 | local_dec(&cpu_buffer->committing); | |
2136 | local_dec(&cpu_buffer->commits); | |
2137 | return NULL; | |
2138 | } | |
85bac32c | 2139 | #endif |
62f0b3eb | 2140 | |
be957c44 | 2141 | length = rb_calculate_event_length(length); |
bf41a158 | 2142 | again: |
69d1b839 SR |
2143 | add_timestamp = 0; |
2144 | delta = 0; | |
2145 | ||
818e3dd3 SR |
2146 | /* |
2147 | * We allow for interrupts to reenter here and do a trace. | |
2148 | * If one does, it will cause this original code to loop | |
2149 | * back here. Even with heavy interrupts happening, this | |
2150 | * should only happen a few times in a row. If this happens | |
2151 | * 1000 times in a row, there must be either an interrupt | |
2152 | * storm or we have something buggy. | |
2153 | * Bail! | |
2154 | */ | |
3e89c7bb | 2155 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
fa743953 | 2156 | goto out_fail; |
818e3dd3 | 2157 | |
6d3f1e12 | 2158 | ts = rb_time_stamp(cpu_buffer->buffer); |
140ff891 | 2159 | diff = ts - cpu_buffer->write_stamp; |
7a8e76a3 | 2160 | |
140ff891 SR |
2161 | /* make sure this diff is calculated here */ |
2162 | barrier(); | |
bf41a158 | 2163 | |
140ff891 SR |
2164 | /* Did the write stamp get updated already? */ |
2165 | if (likely(ts >= cpu_buffer->write_stamp)) { | |
168b6b1d SR |
2166 | delta = diff; |
2167 | if (unlikely(test_time_stamp(delta))) { | |
69d1b839 SR |
2168 | WARN_ONCE(delta > (1ULL << 59), |
2169 | KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n", | |
2170 | (unsigned long long)delta, | |
2171 | (unsigned long long)ts, | |
2172 | (unsigned long long)cpu_buffer->write_stamp); | |
2173 | add_timestamp = 1; | |
7a8e76a3 | 2174 | } |
168b6b1d | 2175 | } |
7a8e76a3 | 2176 | |
69d1b839 SR |
2177 | event = __rb_reserve_next(cpu_buffer, length, ts, |
2178 | delta, add_timestamp); | |
168b6b1d | 2179 | if (unlikely(PTR_ERR(event) == -EAGAIN)) |
bf41a158 SR |
2180 | goto again; |
2181 | ||
fa743953 SR |
2182 | if (!event) |
2183 | goto out_fail; | |
7a8e76a3 | 2184 | |
7a8e76a3 | 2185 | return event; |
fa743953 SR |
2186 | |
2187 | out_fail: | |
2188 | rb_end_commit(cpu_buffer); | |
2189 | return NULL; | |
7a8e76a3 SR |
2190 | } |
2191 | ||
1155de47 PM |
2192 | #ifdef CONFIG_TRACING |
2193 | ||
aa18efb2 | 2194 | #define TRACE_RECURSIVE_DEPTH 16 |
261842b7 SR |
2195 | |
2196 | static int trace_recursive_lock(void) | |
2197 | { | |
aa18efb2 | 2198 | current->trace_recursion++; |
261842b7 | 2199 | |
aa18efb2 SR |
2200 | if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH)) |
2201 | return 0; | |
e057a5e5 | 2202 | |
aa18efb2 SR |
2203 | /* Disable all tracing before we do anything else */ |
2204 | tracing_off_permanent(); | |
261842b7 | 2205 | |
7d7d2b80 | 2206 | printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:" |
aa18efb2 SR |
2207 | "HC[%lu]:SC[%lu]:NMI[%lu]\n", |
2208 | current->trace_recursion, | |
2209 | hardirq_count() >> HARDIRQ_SHIFT, | |
2210 | softirq_count() >> SOFTIRQ_SHIFT, | |
2211 | in_nmi()); | |
261842b7 | 2212 | |
aa18efb2 SR |
2213 | WARN_ON_ONCE(1); |
2214 | return -1; | |
261842b7 SR |
2215 | } |
2216 | ||
2217 | static void trace_recursive_unlock(void) | |
2218 | { | |
aa18efb2 | 2219 | WARN_ON_ONCE(!current->trace_recursion); |
261842b7 | 2220 | |
aa18efb2 | 2221 | current->trace_recursion--; |
261842b7 SR |
2222 | } |
2223 | ||
1155de47 PM |
2224 | #else |
2225 | ||
2226 | #define trace_recursive_lock() (0) | |
2227 | #define trace_recursive_unlock() do { } while (0) | |
2228 | ||
2229 | #endif | |
2230 | ||
7a8e76a3 SR |
2231 | /** |
2232 | * ring_buffer_lock_reserve - reserve a part of the buffer | |
2233 | * @buffer: the ring buffer to reserve from | |
2234 | * @length: the length of the data to reserve (excluding event header) | |
7a8e76a3 SR |
2235 | * |
2236 | * Returns a reseverd event on the ring buffer to copy directly to. | |
2237 | * The user of this interface will need to get the body to write into | |
2238 | * and can use the ring_buffer_event_data() interface. | |
2239 | * | |
2240 | * The length is the length of the data needed, not the event length | |
2241 | * which also includes the event header. | |
2242 | * | |
2243 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. | |
2244 | * If NULL is returned, then nothing has been allocated or locked. | |
2245 | */ | |
2246 | struct ring_buffer_event * | |
0a987751 | 2247 | ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) |
7a8e76a3 SR |
2248 | { |
2249 | struct ring_buffer_per_cpu *cpu_buffer; | |
2250 | struct ring_buffer_event *event; | |
5168ae50 | 2251 | int cpu; |
7a8e76a3 | 2252 | |
033601a3 | 2253 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2254 | return NULL; |
2255 | ||
bf41a158 | 2256 | /* If we are tracing schedule, we don't want to recurse */ |
5168ae50 | 2257 | preempt_disable_notrace(); |
bf41a158 | 2258 | |
52fbe9cd LJ |
2259 | if (atomic_read(&buffer->record_disabled)) |
2260 | goto out_nocheck; | |
2261 | ||
261842b7 SR |
2262 | if (trace_recursive_lock()) |
2263 | goto out_nocheck; | |
2264 | ||
7a8e76a3 SR |
2265 | cpu = raw_smp_processor_id(); |
2266 | ||
9e01c1b7 | 2267 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2268 | goto out; |
7a8e76a3 SR |
2269 | |
2270 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2271 | |
2272 | if (atomic_read(&cpu_buffer->record_disabled)) | |
d769041f | 2273 | goto out; |
7a8e76a3 | 2274 | |
be957c44 | 2275 | if (length > BUF_MAX_DATA_SIZE) |
bf41a158 | 2276 | goto out; |
7a8e76a3 | 2277 | |
62f0b3eb | 2278 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 | 2279 | if (!event) |
d769041f | 2280 | goto out; |
7a8e76a3 SR |
2281 | |
2282 | return event; | |
2283 | ||
d769041f | 2284 | out: |
261842b7 SR |
2285 | trace_recursive_unlock(); |
2286 | ||
2287 | out_nocheck: | |
5168ae50 | 2288 | preempt_enable_notrace(); |
7a8e76a3 SR |
2289 | return NULL; |
2290 | } | |
c4f50183 | 2291 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
7a8e76a3 | 2292 | |
a1863c21 SR |
2293 | static void |
2294 | rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
7a8e76a3 SR |
2295 | struct ring_buffer_event *event) |
2296 | { | |
69d1b839 SR |
2297 | u64 delta; |
2298 | ||
fa743953 SR |
2299 | /* |
2300 | * The event first in the commit queue updates the | |
2301 | * time stamp. | |
2302 | */ | |
69d1b839 SR |
2303 | if (rb_event_is_commit(cpu_buffer, event)) { |
2304 | /* | |
2305 | * A commit event that is first on a page | |
2306 | * updates the write timestamp with the page stamp | |
2307 | */ | |
2308 | if (!rb_event_index(event)) | |
2309 | cpu_buffer->write_stamp = | |
2310 | cpu_buffer->commit_page->page->time_stamp; | |
2311 | else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) { | |
2312 | delta = event->array[0]; | |
2313 | delta <<= TS_SHIFT; | |
2314 | delta += event->time_delta; | |
2315 | cpu_buffer->write_stamp += delta; | |
2316 | } else | |
2317 | cpu_buffer->write_stamp += event->time_delta; | |
2318 | } | |
a1863c21 | 2319 | } |
bf41a158 | 2320 | |
a1863c21 SR |
2321 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
2322 | struct ring_buffer_event *event) | |
2323 | { | |
2324 | local_inc(&cpu_buffer->entries); | |
2325 | rb_update_write_stamp(cpu_buffer, event); | |
fa743953 | 2326 | rb_end_commit(cpu_buffer); |
7a8e76a3 SR |
2327 | } |
2328 | ||
2329 | /** | |
2330 | * ring_buffer_unlock_commit - commit a reserved | |
2331 | * @buffer: The buffer to commit to | |
2332 | * @event: The event pointer to commit. | |
7a8e76a3 SR |
2333 | * |
2334 | * This commits the data to the ring buffer, and releases any locks held. | |
2335 | * | |
2336 | * Must be paired with ring_buffer_lock_reserve. | |
2337 | */ | |
2338 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, | |
0a987751 | 2339 | struct ring_buffer_event *event) |
7a8e76a3 SR |
2340 | { |
2341 | struct ring_buffer_per_cpu *cpu_buffer; | |
2342 | int cpu = raw_smp_processor_id(); | |
2343 | ||
2344 | cpu_buffer = buffer->buffers[cpu]; | |
2345 | ||
7a8e76a3 SR |
2346 | rb_commit(cpu_buffer, event); |
2347 | ||
261842b7 SR |
2348 | trace_recursive_unlock(); |
2349 | ||
5168ae50 | 2350 | preempt_enable_notrace(); |
7a8e76a3 SR |
2351 | |
2352 | return 0; | |
2353 | } | |
c4f50183 | 2354 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
7a8e76a3 | 2355 | |
f3b9aae1 FW |
2356 | static inline void rb_event_discard(struct ring_buffer_event *event) |
2357 | { | |
69d1b839 SR |
2358 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) |
2359 | event = skip_time_extend(event); | |
2360 | ||
334d4169 LJ |
2361 | /* array[0] holds the actual length for the discarded event */ |
2362 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; | |
2363 | event->type_len = RINGBUF_TYPE_PADDING; | |
f3b9aae1 FW |
2364 | /* time delta must be non zero */ |
2365 | if (!event->time_delta) | |
2366 | event->time_delta = 1; | |
2367 | } | |
2368 | ||
a1863c21 SR |
2369 | /* |
2370 | * Decrement the entries to the page that an event is on. | |
2371 | * The event does not even need to exist, only the pointer | |
2372 | * to the page it is on. This may only be called before the commit | |
2373 | * takes place. | |
2374 | */ | |
2375 | static inline void | |
2376 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, | |
2377 | struct ring_buffer_event *event) | |
2378 | { | |
2379 | unsigned long addr = (unsigned long)event; | |
2380 | struct buffer_page *bpage = cpu_buffer->commit_page; | |
2381 | struct buffer_page *start; | |
2382 | ||
2383 | addr &= PAGE_MASK; | |
2384 | ||
2385 | /* Do the likely case first */ | |
2386 | if (likely(bpage->page == (void *)addr)) { | |
2387 | local_dec(&bpage->entries); | |
2388 | return; | |
2389 | } | |
2390 | ||
2391 | /* | |
2392 | * Because the commit page may be on the reader page we | |
2393 | * start with the next page and check the end loop there. | |
2394 | */ | |
2395 | rb_inc_page(cpu_buffer, &bpage); | |
2396 | start = bpage; | |
2397 | do { | |
2398 | if (bpage->page == (void *)addr) { | |
2399 | local_dec(&bpage->entries); | |
2400 | return; | |
2401 | } | |
2402 | rb_inc_page(cpu_buffer, &bpage); | |
2403 | } while (bpage != start); | |
2404 | ||
2405 | /* commit not part of this buffer?? */ | |
2406 | RB_WARN_ON(cpu_buffer, 1); | |
2407 | } | |
2408 | ||
fa1b47dd SR |
2409 | /** |
2410 | * ring_buffer_commit_discard - discard an event that has not been committed | |
2411 | * @buffer: the ring buffer | |
2412 | * @event: non committed event to discard | |
2413 | * | |
dc892f73 SR |
2414 | * Sometimes an event that is in the ring buffer needs to be ignored. |
2415 | * This function lets the user discard an event in the ring buffer | |
2416 | * and then that event will not be read later. | |
2417 | * | |
2418 | * This function only works if it is called before the the item has been | |
2419 | * committed. It will try to free the event from the ring buffer | |
fa1b47dd SR |
2420 | * if another event has not been added behind it. |
2421 | * | |
2422 | * If another event has been added behind it, it will set the event | |
2423 | * up as discarded, and perform the commit. | |
2424 | * | |
2425 | * If this function is called, do not call ring_buffer_unlock_commit on | |
2426 | * the event. | |
2427 | */ | |
2428 | void ring_buffer_discard_commit(struct ring_buffer *buffer, | |
2429 | struct ring_buffer_event *event) | |
2430 | { | |
2431 | struct ring_buffer_per_cpu *cpu_buffer; | |
fa1b47dd SR |
2432 | int cpu; |
2433 | ||
2434 | /* The event is discarded regardless */ | |
f3b9aae1 | 2435 | rb_event_discard(event); |
fa1b47dd | 2436 | |
fa743953 SR |
2437 | cpu = smp_processor_id(); |
2438 | cpu_buffer = buffer->buffers[cpu]; | |
2439 | ||
fa1b47dd SR |
2440 | /* |
2441 | * This must only be called if the event has not been | |
2442 | * committed yet. Thus we can assume that preemption | |
2443 | * is still disabled. | |
2444 | */ | |
fa743953 | 2445 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
fa1b47dd | 2446 | |
a1863c21 | 2447 | rb_decrement_entry(cpu_buffer, event); |
0f2541d2 | 2448 | if (rb_try_to_discard(cpu_buffer, event)) |
edd813bf | 2449 | goto out; |
fa1b47dd SR |
2450 | |
2451 | /* | |
2452 | * The commit is still visible by the reader, so we | |
a1863c21 | 2453 | * must still update the timestamp. |
fa1b47dd | 2454 | */ |
a1863c21 | 2455 | rb_update_write_stamp(cpu_buffer, event); |
fa1b47dd | 2456 | out: |
fa743953 | 2457 | rb_end_commit(cpu_buffer); |
fa1b47dd | 2458 | |
f3b9aae1 FW |
2459 | trace_recursive_unlock(); |
2460 | ||
5168ae50 | 2461 | preempt_enable_notrace(); |
fa1b47dd SR |
2462 | |
2463 | } | |
2464 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); | |
2465 | ||
7a8e76a3 SR |
2466 | /** |
2467 | * ring_buffer_write - write data to the buffer without reserving | |
2468 | * @buffer: The ring buffer to write to. | |
2469 | * @length: The length of the data being written (excluding the event header) | |
2470 | * @data: The data to write to the buffer. | |
2471 | * | |
2472 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as | |
2473 | * one function. If you already have the data to write to the buffer, it | |
2474 | * may be easier to simply call this function. | |
2475 | * | |
2476 | * Note, like ring_buffer_lock_reserve, the length is the length of the data | |
2477 | * and not the length of the event which would hold the header. | |
2478 | */ | |
2479 | int ring_buffer_write(struct ring_buffer *buffer, | |
2480 | unsigned long length, | |
2481 | void *data) | |
2482 | { | |
2483 | struct ring_buffer_per_cpu *cpu_buffer; | |
2484 | struct ring_buffer_event *event; | |
7a8e76a3 SR |
2485 | void *body; |
2486 | int ret = -EBUSY; | |
5168ae50 | 2487 | int cpu; |
7a8e76a3 | 2488 | |
033601a3 | 2489 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2490 | return -EBUSY; |
2491 | ||
5168ae50 | 2492 | preempt_disable_notrace(); |
bf41a158 | 2493 | |
52fbe9cd LJ |
2494 | if (atomic_read(&buffer->record_disabled)) |
2495 | goto out; | |
2496 | ||
7a8e76a3 SR |
2497 | cpu = raw_smp_processor_id(); |
2498 | ||
9e01c1b7 | 2499 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2500 | goto out; |
7a8e76a3 SR |
2501 | |
2502 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2503 | |
2504 | if (atomic_read(&cpu_buffer->record_disabled)) | |
2505 | goto out; | |
2506 | ||
be957c44 SR |
2507 | if (length > BUF_MAX_DATA_SIZE) |
2508 | goto out; | |
2509 | ||
62f0b3eb | 2510 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 SR |
2511 | if (!event) |
2512 | goto out; | |
2513 | ||
2514 | body = rb_event_data(event); | |
2515 | ||
2516 | memcpy(body, data, length); | |
2517 | ||
2518 | rb_commit(cpu_buffer, event); | |
2519 | ||
2520 | ret = 0; | |
2521 | out: | |
5168ae50 | 2522 | preempt_enable_notrace(); |
7a8e76a3 SR |
2523 | |
2524 | return ret; | |
2525 | } | |
c4f50183 | 2526 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
7a8e76a3 | 2527 | |
34a148bf | 2528 | static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
bf41a158 SR |
2529 | { |
2530 | struct buffer_page *reader = cpu_buffer->reader_page; | |
77ae365e | 2531 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
bf41a158 SR |
2532 | struct buffer_page *commit = cpu_buffer->commit_page; |
2533 | ||
77ae365e SR |
2534 | /* In case of error, head will be NULL */ |
2535 | if (unlikely(!head)) | |
2536 | return 1; | |
2537 | ||
bf41a158 SR |
2538 | return reader->read == rb_page_commit(reader) && |
2539 | (commit == reader || | |
2540 | (commit == head && | |
2541 | head->read == rb_page_commit(commit))); | |
2542 | } | |
2543 | ||
7a8e76a3 SR |
2544 | /** |
2545 | * ring_buffer_record_disable - stop all writes into the buffer | |
2546 | * @buffer: The ring buffer to stop writes to. | |
2547 | * | |
2548 | * This prevents all writes to the buffer. Any attempt to write | |
2549 | * to the buffer after this will fail and return NULL. | |
2550 | * | |
2551 | * The caller should call synchronize_sched() after this. | |
2552 | */ | |
2553 | void ring_buffer_record_disable(struct ring_buffer *buffer) | |
2554 | { | |
2555 | atomic_inc(&buffer->record_disabled); | |
2556 | } | |
c4f50183 | 2557 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
7a8e76a3 SR |
2558 | |
2559 | /** | |
2560 | * ring_buffer_record_enable - enable writes to the buffer | |
2561 | * @buffer: The ring buffer to enable writes | |
2562 | * | |
2563 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 2564 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
2565 | */ |
2566 | void ring_buffer_record_enable(struct ring_buffer *buffer) | |
2567 | { | |
2568 | atomic_dec(&buffer->record_disabled); | |
2569 | } | |
c4f50183 | 2570 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
7a8e76a3 SR |
2571 | |
2572 | /** | |
2573 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer | |
2574 | * @buffer: The ring buffer to stop writes to. | |
2575 | * @cpu: The CPU buffer to stop | |
2576 | * | |
2577 | * This prevents all writes to the buffer. Any attempt to write | |
2578 | * to the buffer after this will fail and return NULL. | |
2579 | * | |
2580 | * The caller should call synchronize_sched() after this. | |
2581 | */ | |
2582 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) | |
2583 | { | |
2584 | struct ring_buffer_per_cpu *cpu_buffer; | |
2585 | ||
9e01c1b7 | 2586 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2587 | return; |
7a8e76a3 SR |
2588 | |
2589 | cpu_buffer = buffer->buffers[cpu]; | |
2590 | atomic_inc(&cpu_buffer->record_disabled); | |
2591 | } | |
c4f50183 | 2592 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
7a8e76a3 SR |
2593 | |
2594 | /** | |
2595 | * ring_buffer_record_enable_cpu - enable writes to the buffer | |
2596 | * @buffer: The ring buffer to enable writes | |
2597 | * @cpu: The CPU to enable. | |
2598 | * | |
2599 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 2600 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
2601 | */ |
2602 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) | |
2603 | { | |
2604 | struct ring_buffer_per_cpu *cpu_buffer; | |
2605 | ||
9e01c1b7 | 2606 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2607 | return; |
7a8e76a3 SR |
2608 | |
2609 | cpu_buffer = buffer->buffers[cpu]; | |
2610 | atomic_dec(&cpu_buffer->record_disabled); | |
2611 | } | |
c4f50183 | 2612 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
7a8e76a3 SR |
2613 | |
2614 | /** | |
2615 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer | |
2616 | * @buffer: The ring buffer | |
2617 | * @cpu: The per CPU buffer to get the entries from. | |
2618 | */ | |
2619 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) | |
2620 | { | |
2621 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 2622 | unsigned long ret; |
7a8e76a3 | 2623 | |
9e01c1b7 | 2624 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2625 | return 0; |
7a8e76a3 SR |
2626 | |
2627 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2628 | ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun)) |
e4906eff | 2629 | - cpu_buffer->read; |
554f786e SR |
2630 | |
2631 | return ret; | |
7a8e76a3 | 2632 | } |
c4f50183 | 2633 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
7a8e76a3 SR |
2634 | |
2635 | /** | |
2636 | * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer | |
2637 | * @buffer: The ring buffer | |
2638 | * @cpu: The per CPU buffer to get the number of overruns from | |
2639 | */ | |
2640 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
2641 | { | |
2642 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 2643 | unsigned long ret; |
7a8e76a3 | 2644 | |
9e01c1b7 | 2645 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 2646 | return 0; |
7a8e76a3 SR |
2647 | |
2648 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2649 | ret = local_read(&cpu_buffer->overrun); |
554f786e SR |
2650 | |
2651 | return ret; | |
7a8e76a3 | 2652 | } |
c4f50183 | 2653 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
7a8e76a3 | 2654 | |
f0d2c681 SR |
2655 | /** |
2656 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits | |
2657 | * @buffer: The ring buffer | |
2658 | * @cpu: The per CPU buffer to get the number of overruns from | |
2659 | */ | |
2660 | unsigned long | |
2661 | ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
2662 | { | |
2663 | struct ring_buffer_per_cpu *cpu_buffer; | |
2664 | unsigned long ret; | |
2665 | ||
2666 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
2667 | return 0; | |
2668 | ||
2669 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2670 | ret = local_read(&cpu_buffer->commit_overrun); |
f0d2c681 SR |
2671 | |
2672 | return ret; | |
2673 | } | |
2674 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); | |
2675 | ||
7a8e76a3 SR |
2676 | /** |
2677 | * ring_buffer_entries - get the number of entries in a buffer | |
2678 | * @buffer: The ring buffer | |
2679 | * | |
2680 | * Returns the total number of entries in the ring buffer | |
2681 | * (all CPU entries) | |
2682 | */ | |
2683 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) | |
2684 | { | |
2685 | struct ring_buffer_per_cpu *cpu_buffer; | |
2686 | unsigned long entries = 0; | |
2687 | int cpu; | |
2688 | ||
2689 | /* if you care about this being correct, lock the buffer */ | |
2690 | for_each_buffer_cpu(buffer, cpu) { | |
2691 | cpu_buffer = buffer->buffers[cpu]; | |
e4906eff | 2692 | entries += (local_read(&cpu_buffer->entries) - |
77ae365e | 2693 | local_read(&cpu_buffer->overrun)) - cpu_buffer->read; |
7a8e76a3 SR |
2694 | } |
2695 | ||
2696 | return entries; | |
2697 | } | |
c4f50183 | 2698 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
7a8e76a3 SR |
2699 | |
2700 | /** | |
67b394f7 | 2701 | * ring_buffer_overruns - get the number of overruns in buffer |
7a8e76a3 SR |
2702 | * @buffer: The ring buffer |
2703 | * | |
2704 | * Returns the total number of overruns in the ring buffer | |
2705 | * (all CPU entries) | |
2706 | */ | |
2707 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) | |
2708 | { | |
2709 | struct ring_buffer_per_cpu *cpu_buffer; | |
2710 | unsigned long overruns = 0; | |
2711 | int cpu; | |
2712 | ||
2713 | /* if you care about this being correct, lock the buffer */ | |
2714 | for_each_buffer_cpu(buffer, cpu) { | |
2715 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 2716 | overruns += local_read(&cpu_buffer->overrun); |
7a8e76a3 SR |
2717 | } |
2718 | ||
2719 | return overruns; | |
2720 | } | |
c4f50183 | 2721 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
7a8e76a3 | 2722 | |
642edba5 | 2723 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
7a8e76a3 SR |
2724 | { |
2725 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
2726 | ||
d769041f SR |
2727 | /* Iterator usage is expected to have record disabled */ |
2728 | if (list_empty(&cpu_buffer->reader_page->list)) { | |
77ae365e SR |
2729 | iter->head_page = rb_set_head_page(cpu_buffer); |
2730 | if (unlikely(!iter->head_page)) | |
2731 | return; | |
2732 | iter->head = iter->head_page->read; | |
d769041f SR |
2733 | } else { |
2734 | iter->head_page = cpu_buffer->reader_page; | |
6f807acd | 2735 | iter->head = cpu_buffer->reader_page->read; |
d769041f SR |
2736 | } |
2737 | if (iter->head) | |
2738 | iter->read_stamp = cpu_buffer->read_stamp; | |
2739 | else | |
abc9b56d | 2740 | iter->read_stamp = iter->head_page->page->time_stamp; |
492a74f4 SR |
2741 | iter->cache_reader_page = cpu_buffer->reader_page; |
2742 | iter->cache_read = cpu_buffer->read; | |
642edba5 | 2743 | } |
f83c9d0f | 2744 | |
642edba5 SR |
2745 | /** |
2746 | * ring_buffer_iter_reset - reset an iterator | |
2747 | * @iter: The iterator to reset | |
2748 | * | |
2749 | * Resets the iterator, so that it will start from the beginning | |
2750 | * again. | |
2751 | */ | |
2752 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) | |
2753 | { | |
554f786e | 2754 | struct ring_buffer_per_cpu *cpu_buffer; |
642edba5 SR |
2755 | unsigned long flags; |
2756 | ||
554f786e SR |
2757 | if (!iter) |
2758 | return; | |
2759 | ||
2760 | cpu_buffer = iter->cpu_buffer; | |
2761 | ||
642edba5 SR |
2762 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
2763 | rb_iter_reset(iter); | |
f83c9d0f | 2764 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 | 2765 | } |
c4f50183 | 2766 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
7a8e76a3 SR |
2767 | |
2768 | /** | |
2769 | * ring_buffer_iter_empty - check if an iterator has no more to read | |
2770 | * @iter: The iterator to check | |
2771 | */ | |
2772 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) | |
2773 | { | |
2774 | struct ring_buffer_per_cpu *cpu_buffer; | |
2775 | ||
2776 | cpu_buffer = iter->cpu_buffer; | |
2777 | ||
bf41a158 SR |
2778 | return iter->head_page == cpu_buffer->commit_page && |
2779 | iter->head == rb_commit_index(cpu_buffer); | |
7a8e76a3 | 2780 | } |
c4f50183 | 2781 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
7a8e76a3 SR |
2782 | |
2783 | static void | |
2784 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
2785 | struct ring_buffer_event *event) | |
2786 | { | |
2787 | u64 delta; | |
2788 | ||
334d4169 | 2789 | switch (event->type_len) { |
7a8e76a3 SR |
2790 | case RINGBUF_TYPE_PADDING: |
2791 | return; | |
2792 | ||
2793 | case RINGBUF_TYPE_TIME_EXTEND: | |
2794 | delta = event->array[0]; | |
2795 | delta <<= TS_SHIFT; | |
2796 | delta += event->time_delta; | |
2797 | cpu_buffer->read_stamp += delta; | |
2798 | return; | |
2799 | ||
2800 | case RINGBUF_TYPE_TIME_STAMP: | |
2801 | /* FIXME: not implemented */ | |
2802 | return; | |
2803 | ||
2804 | case RINGBUF_TYPE_DATA: | |
2805 | cpu_buffer->read_stamp += event->time_delta; | |
2806 | return; | |
2807 | ||
2808 | default: | |
2809 | BUG(); | |
2810 | } | |
2811 | return; | |
2812 | } | |
2813 | ||
2814 | static void | |
2815 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, | |
2816 | struct ring_buffer_event *event) | |
2817 | { | |
2818 | u64 delta; | |
2819 | ||
334d4169 | 2820 | switch (event->type_len) { |
7a8e76a3 SR |
2821 | case RINGBUF_TYPE_PADDING: |
2822 | return; | |
2823 | ||
2824 | case RINGBUF_TYPE_TIME_EXTEND: | |
2825 | delta = event->array[0]; | |
2826 | delta <<= TS_SHIFT; | |
2827 | delta += event->time_delta; | |
2828 | iter->read_stamp += delta; | |
2829 | return; | |
2830 | ||
2831 | case RINGBUF_TYPE_TIME_STAMP: | |
2832 | /* FIXME: not implemented */ | |
2833 | return; | |
2834 | ||
2835 | case RINGBUF_TYPE_DATA: | |
2836 | iter->read_stamp += event->time_delta; | |
2837 | return; | |
2838 | ||
2839 | default: | |
2840 | BUG(); | |
2841 | } | |
2842 | return; | |
2843 | } | |
2844 | ||
d769041f SR |
2845 | static struct buffer_page * |
2846 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) | |
7a8e76a3 | 2847 | { |
d769041f | 2848 | struct buffer_page *reader = NULL; |
66a8cb95 | 2849 | unsigned long overwrite; |
d769041f | 2850 | unsigned long flags; |
818e3dd3 | 2851 | int nr_loops = 0; |
77ae365e | 2852 | int ret; |
d769041f | 2853 | |
3e03fb7f | 2854 | local_irq_save(flags); |
0199c4e6 | 2855 | arch_spin_lock(&cpu_buffer->lock); |
d769041f SR |
2856 | |
2857 | again: | |
818e3dd3 SR |
2858 | /* |
2859 | * This should normally only loop twice. But because the | |
2860 | * start of the reader inserts an empty page, it causes | |
2861 | * a case where we will loop three times. There should be no | |
2862 | * reason to loop four times (that I know of). | |
2863 | */ | |
3e89c7bb | 2864 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
818e3dd3 SR |
2865 | reader = NULL; |
2866 | goto out; | |
2867 | } | |
2868 | ||
d769041f SR |
2869 | reader = cpu_buffer->reader_page; |
2870 | ||
2871 | /* If there's more to read, return this page */ | |
bf41a158 | 2872 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
d769041f SR |
2873 | goto out; |
2874 | ||
2875 | /* Never should we have an index greater than the size */ | |
3e89c7bb SR |
2876 | if (RB_WARN_ON(cpu_buffer, |
2877 | cpu_buffer->reader_page->read > rb_page_size(reader))) | |
2878 | goto out; | |
d769041f SR |
2879 | |
2880 | /* check if we caught up to the tail */ | |
2881 | reader = NULL; | |
bf41a158 | 2882 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
d769041f | 2883 | goto out; |
7a8e76a3 SR |
2884 | |
2885 | /* | |
d769041f | 2886 | * Reset the reader page to size zero. |
7a8e76a3 | 2887 | */ |
77ae365e SR |
2888 | local_set(&cpu_buffer->reader_page->write, 0); |
2889 | local_set(&cpu_buffer->reader_page->entries, 0); | |
2890 | local_set(&cpu_buffer->reader_page->page->commit, 0); | |
ff0ff84a | 2891 | cpu_buffer->reader_page->real_end = 0; |
7a8e76a3 | 2892 | |
77ae365e SR |
2893 | spin: |
2894 | /* | |
2895 | * Splice the empty reader page into the list around the head. | |
2896 | */ | |
2897 | reader = rb_set_head_page(cpu_buffer); | |
0e1ff5d7 | 2898 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
d769041f | 2899 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
bf41a158 | 2900 | |
3adc54fa SR |
2901 | /* |
2902 | * cpu_buffer->pages just needs to point to the buffer, it | |
2903 | * has no specific buffer page to point to. Lets move it out | |
2904 | * of our way so we don't accidently swap it. | |
2905 | */ | |
2906 | cpu_buffer->pages = reader->list.prev; | |
2907 | ||
77ae365e SR |
2908 | /* The reader page will be pointing to the new head */ |
2909 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); | |
7a8e76a3 | 2910 | |
66a8cb95 SR |
2911 | /* |
2912 | * We want to make sure we read the overruns after we set up our | |
2913 | * pointers to the next object. The writer side does a | |
2914 | * cmpxchg to cross pages which acts as the mb on the writer | |
2915 | * side. Note, the reader will constantly fail the swap | |
2916 | * while the writer is updating the pointers, so this | |
2917 | * guarantees that the overwrite recorded here is the one we | |
2918 | * want to compare with the last_overrun. | |
2919 | */ | |
2920 | smp_mb(); | |
2921 | overwrite = local_read(&(cpu_buffer->overrun)); | |
2922 | ||
77ae365e SR |
2923 | /* |
2924 | * Here's the tricky part. | |
2925 | * | |
2926 | * We need to move the pointer past the header page. | |
2927 | * But we can only do that if a writer is not currently | |
2928 | * moving it. The page before the header page has the | |
2929 | * flag bit '1' set if it is pointing to the page we want. | |
2930 | * but if the writer is in the process of moving it | |
2931 | * than it will be '2' or already moved '0'. | |
2932 | */ | |
2933 | ||
2934 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); | |
7a8e76a3 SR |
2935 | |
2936 | /* | |
77ae365e | 2937 | * If we did not convert it, then we must try again. |
7a8e76a3 | 2938 | */ |
77ae365e SR |
2939 | if (!ret) |
2940 | goto spin; | |
7a8e76a3 | 2941 | |
77ae365e SR |
2942 | /* |
2943 | * Yeah! We succeeded in replacing the page. | |
2944 | * | |
2945 | * Now make the new head point back to the reader page. | |
2946 | */ | |
5ded3dc6 | 2947 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
77ae365e | 2948 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
d769041f SR |
2949 | |
2950 | /* Finally update the reader page to the new head */ | |
2951 | cpu_buffer->reader_page = reader; | |
2952 | rb_reset_reader_page(cpu_buffer); | |
2953 | ||
66a8cb95 SR |
2954 | if (overwrite != cpu_buffer->last_overrun) { |
2955 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; | |
2956 | cpu_buffer->last_overrun = overwrite; | |
2957 | } | |
2958 | ||
d769041f SR |
2959 | goto again; |
2960 | ||
2961 | out: | |
0199c4e6 | 2962 | arch_spin_unlock(&cpu_buffer->lock); |
3e03fb7f | 2963 | local_irq_restore(flags); |
d769041f SR |
2964 | |
2965 | return reader; | |
2966 | } | |
2967 | ||
2968 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) | |
2969 | { | |
2970 | struct ring_buffer_event *event; | |
2971 | struct buffer_page *reader; | |
2972 | unsigned length; | |
2973 | ||
2974 | reader = rb_get_reader_page(cpu_buffer); | |
7a8e76a3 | 2975 | |
d769041f | 2976 | /* This function should not be called when buffer is empty */ |
3e89c7bb SR |
2977 | if (RB_WARN_ON(cpu_buffer, !reader)) |
2978 | return; | |
7a8e76a3 | 2979 | |
d769041f SR |
2980 | event = rb_reader_event(cpu_buffer); |
2981 | ||
a1863c21 | 2982 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
e4906eff | 2983 | cpu_buffer->read++; |
d769041f SR |
2984 | |
2985 | rb_update_read_stamp(cpu_buffer, event); | |
2986 | ||
2987 | length = rb_event_length(event); | |
6f807acd | 2988 | cpu_buffer->reader_page->read += length; |
7a8e76a3 SR |
2989 | } |
2990 | ||
2991 | static void rb_advance_iter(struct ring_buffer_iter *iter) | |
2992 | { | |
7a8e76a3 SR |
2993 | struct ring_buffer_per_cpu *cpu_buffer; |
2994 | struct ring_buffer_event *event; | |
2995 | unsigned length; | |
2996 | ||
2997 | cpu_buffer = iter->cpu_buffer; | |
7a8e76a3 SR |
2998 | |
2999 | /* | |
3000 | * Check if we are at the end of the buffer. | |
3001 | */ | |
bf41a158 | 3002 | if (iter->head >= rb_page_size(iter->head_page)) { |
ea05b57c SR |
3003 | /* discarded commits can make the page empty */ |
3004 | if (iter->head_page == cpu_buffer->commit_page) | |
3e89c7bb | 3005 | return; |
d769041f | 3006 | rb_inc_iter(iter); |
7a8e76a3 SR |
3007 | return; |
3008 | } | |
3009 | ||
3010 | event = rb_iter_head_event(iter); | |
3011 | ||
3012 | length = rb_event_length(event); | |
3013 | ||
3014 | /* | |
3015 | * This should not be called to advance the header if we are | |
3016 | * at the tail of the buffer. | |
3017 | */ | |
3e89c7bb | 3018 | if (RB_WARN_ON(cpu_buffer, |
f536aafc | 3019 | (iter->head_page == cpu_buffer->commit_page) && |
3e89c7bb SR |
3020 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
3021 | return; | |
7a8e76a3 SR |
3022 | |
3023 | rb_update_iter_read_stamp(iter, event); | |
3024 | ||
3025 | iter->head += length; | |
3026 | ||
3027 | /* check for end of page padding */ | |
bf41a158 SR |
3028 | if ((iter->head >= rb_page_size(iter->head_page)) && |
3029 | (iter->head_page != cpu_buffer->commit_page)) | |
7a8e76a3 SR |
3030 | rb_advance_iter(iter); |
3031 | } | |
3032 | ||
66a8cb95 SR |
3033 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
3034 | { | |
3035 | return cpu_buffer->lost_events; | |
3036 | } | |
3037 | ||
f83c9d0f | 3038 | static struct ring_buffer_event * |
66a8cb95 SR |
3039 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
3040 | unsigned long *lost_events) | |
7a8e76a3 | 3041 | { |
7a8e76a3 | 3042 | struct ring_buffer_event *event; |
d769041f | 3043 | struct buffer_page *reader; |
818e3dd3 | 3044 | int nr_loops = 0; |
7a8e76a3 | 3045 | |
7a8e76a3 | 3046 | again: |
818e3dd3 | 3047 | /* |
69d1b839 SR |
3048 | * We repeat when a time extend is encountered. |
3049 | * Since the time extend is always attached to a data event, | |
3050 | * we should never loop more than once. | |
3051 | * (We never hit the following condition more than twice). | |
818e3dd3 | 3052 | */ |
69d1b839 | 3053 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) |
818e3dd3 | 3054 | return NULL; |
818e3dd3 | 3055 | |
d769041f SR |
3056 | reader = rb_get_reader_page(cpu_buffer); |
3057 | if (!reader) | |
7a8e76a3 SR |
3058 | return NULL; |
3059 | ||
d769041f | 3060 | event = rb_reader_event(cpu_buffer); |
7a8e76a3 | 3061 | |
334d4169 | 3062 | switch (event->type_len) { |
7a8e76a3 | 3063 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
3064 | if (rb_null_event(event)) |
3065 | RB_WARN_ON(cpu_buffer, 1); | |
3066 | /* | |
3067 | * Because the writer could be discarding every | |
3068 | * event it creates (which would probably be bad) | |
3069 | * if we were to go back to "again" then we may never | |
3070 | * catch up, and will trigger the warn on, or lock | |
3071 | * the box. Return the padding, and we will release | |
3072 | * the current locks, and try again. | |
3073 | */ | |
2d622719 | 3074 | return event; |
7a8e76a3 SR |
3075 | |
3076 | case RINGBUF_TYPE_TIME_EXTEND: | |
3077 | /* Internal data, OK to advance */ | |
d769041f | 3078 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3079 | goto again; |
3080 | ||
3081 | case RINGBUF_TYPE_TIME_STAMP: | |
3082 | /* FIXME: not implemented */ | |
d769041f | 3083 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3084 | goto again; |
3085 | ||
3086 | case RINGBUF_TYPE_DATA: | |
3087 | if (ts) { | |
3088 | *ts = cpu_buffer->read_stamp + event->time_delta; | |
d8eeb2d3 | 3089 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
37886f6a | 3090 | cpu_buffer->cpu, ts); |
7a8e76a3 | 3091 | } |
66a8cb95 SR |
3092 | if (lost_events) |
3093 | *lost_events = rb_lost_events(cpu_buffer); | |
7a8e76a3 SR |
3094 | return event; |
3095 | ||
3096 | default: | |
3097 | BUG(); | |
3098 | } | |
3099 | ||
3100 | return NULL; | |
3101 | } | |
c4f50183 | 3102 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
7a8e76a3 | 3103 | |
f83c9d0f SR |
3104 | static struct ring_buffer_event * |
3105 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |
7a8e76a3 SR |
3106 | { |
3107 | struct ring_buffer *buffer; | |
3108 | struct ring_buffer_per_cpu *cpu_buffer; | |
3109 | struct ring_buffer_event *event; | |
818e3dd3 | 3110 | int nr_loops = 0; |
7a8e76a3 | 3111 | |
7a8e76a3 SR |
3112 | cpu_buffer = iter->cpu_buffer; |
3113 | buffer = cpu_buffer->buffer; | |
3114 | ||
492a74f4 SR |
3115 | /* |
3116 | * Check if someone performed a consuming read to | |
3117 | * the buffer. A consuming read invalidates the iterator | |
3118 | * and we need to reset the iterator in this case. | |
3119 | */ | |
3120 | if (unlikely(iter->cache_read != cpu_buffer->read || | |
3121 | iter->cache_reader_page != cpu_buffer->reader_page)) | |
3122 | rb_iter_reset(iter); | |
3123 | ||
7a8e76a3 | 3124 | again: |
3c05d748 SR |
3125 | if (ring_buffer_iter_empty(iter)) |
3126 | return NULL; | |
3127 | ||
818e3dd3 | 3128 | /* |
69d1b839 SR |
3129 | * We repeat when a time extend is encountered. |
3130 | * Since the time extend is always attached to a data event, | |
3131 | * we should never loop more than once. | |
3132 | * (We never hit the following condition more than twice). | |
818e3dd3 | 3133 | */ |
69d1b839 | 3134 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) |
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 | ||
69d1b839 SR |
3832 | /* Always keep the time extend and data together */ |
3833 | size = rb_event_ts_length(event); | |
ef7a4a16 SR |
3834 | |
3835 | if (len < size) | |
554f786e | 3836 | goto out_unlock; |
ef7a4a16 | 3837 | |
4f3640f8 SR |
3838 | /* save the current timestamp, since the user will need it */ |
3839 | save_timestamp = cpu_buffer->read_stamp; | |
3840 | ||
ef7a4a16 SR |
3841 | /* Need to copy one event at a time */ |
3842 | do { | |
474d32b6 | 3843 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
ef7a4a16 SR |
3844 | |
3845 | len -= size; | |
3846 | ||
3847 | rb_advance_reader(cpu_buffer); | |
474d32b6 SR |
3848 | rpos = reader->read; |
3849 | pos += size; | |
ef7a4a16 | 3850 | |
18fab912 HY |
3851 | if (rpos >= commit) |
3852 | break; | |
3853 | ||
ef7a4a16 | 3854 | event = rb_reader_event(cpu_buffer); |
69d1b839 SR |
3855 | /* Always keep the time extend and data together */ |
3856 | size = rb_event_ts_length(event); | |
ef7a4a16 | 3857 | } while (len > size); |
667d2412 LJ |
3858 | |
3859 | /* update bpage */ | |
ef7a4a16 | 3860 | local_set(&bpage->commit, pos); |
4f3640f8 | 3861 | bpage->time_stamp = save_timestamp; |
ef7a4a16 | 3862 | |
474d32b6 SR |
3863 | /* we copied everything to the beginning */ |
3864 | read = 0; | |
8789a9e7 | 3865 | } else { |
afbab76a | 3866 | /* update the entry counter */ |
77ae365e | 3867 | cpu_buffer->read += rb_page_entries(reader); |
afbab76a | 3868 | |
8789a9e7 | 3869 | /* swap the pages */ |
044fa782 | 3870 | rb_init_page(bpage); |
ef7a4a16 SR |
3871 | bpage = reader->page; |
3872 | reader->page = *data_page; | |
3873 | local_set(&reader->write, 0); | |
778c55d4 | 3874 | local_set(&reader->entries, 0); |
ef7a4a16 | 3875 | reader->read = 0; |
044fa782 | 3876 | *data_page = bpage; |
ff0ff84a SR |
3877 | |
3878 | /* | |
3879 | * Use the real_end for the data size, | |
3880 | * This gives us a chance to store the lost events | |
3881 | * on the page. | |
3882 | */ | |
3883 | if (reader->real_end) | |
3884 | local_set(&bpage->commit, reader->real_end); | |
8789a9e7 | 3885 | } |
667d2412 | 3886 | ret = read; |
8789a9e7 | 3887 | |
66a8cb95 | 3888 | cpu_buffer->lost_events = 0; |
2711ca23 SR |
3889 | |
3890 | commit = local_read(&bpage->commit); | |
66a8cb95 SR |
3891 | /* |
3892 | * Set a flag in the commit field if we lost events | |
3893 | */ | |
ff0ff84a | 3894 | if (missed_events) { |
ff0ff84a SR |
3895 | /* If there is room at the end of the page to save the |
3896 | * missed events, then record it there. | |
3897 | */ | |
3898 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { | |
3899 | memcpy(&bpage->data[commit], &missed_events, | |
3900 | sizeof(missed_events)); | |
3901 | local_add(RB_MISSED_STORED, &bpage->commit); | |
2711ca23 | 3902 | commit += sizeof(missed_events); |
ff0ff84a | 3903 | } |
66a8cb95 | 3904 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
ff0ff84a | 3905 | } |
66a8cb95 | 3906 | |
2711ca23 SR |
3907 | /* |
3908 | * This page may be off to user land. Zero it out here. | |
3909 | */ | |
3910 | if (commit < BUF_PAGE_SIZE) | |
3911 | memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit); | |
3912 | ||
554f786e | 3913 | out_unlock: |
8789a9e7 SR |
3914 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
3915 | ||
554f786e | 3916 | out: |
8789a9e7 SR |
3917 | return ret; |
3918 | } | |
d6ce96da | 3919 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
8789a9e7 | 3920 | |
1155de47 | 3921 | #ifdef CONFIG_TRACING |
a3583244 SR |
3922 | static ssize_t |
3923 | rb_simple_read(struct file *filp, char __user *ubuf, | |
3924 | size_t cnt, loff_t *ppos) | |
3925 | { | |
5e39841c | 3926 | unsigned long *p = filp->private_data; |
a3583244 SR |
3927 | char buf[64]; |
3928 | int r; | |
3929 | ||
033601a3 SR |
3930 | if (test_bit(RB_BUFFERS_DISABLED_BIT, p)) |
3931 | r = sprintf(buf, "permanently disabled\n"); | |
3932 | else | |
3933 | r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p)); | |
a3583244 SR |
3934 | |
3935 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
3936 | } | |
3937 | ||
3938 | static ssize_t | |
3939 | rb_simple_write(struct file *filp, const char __user *ubuf, | |
3940 | size_t cnt, loff_t *ppos) | |
3941 | { | |
5e39841c | 3942 | unsigned long *p = filp->private_data; |
a3583244 | 3943 | char buf[64]; |
5e39841c | 3944 | unsigned long val; |
a3583244 SR |
3945 | int ret; |
3946 | ||
3947 | if (cnt >= sizeof(buf)) | |
3948 | return -EINVAL; | |
3949 | ||
3950 | if (copy_from_user(&buf, ubuf, cnt)) | |
3951 | return -EFAULT; | |
3952 | ||
3953 | buf[cnt] = 0; | |
3954 | ||
3955 | ret = strict_strtoul(buf, 10, &val); | |
3956 | if (ret < 0) | |
3957 | return ret; | |
3958 | ||
033601a3 SR |
3959 | if (val) |
3960 | set_bit(RB_BUFFERS_ON_BIT, p); | |
3961 | else | |
3962 | clear_bit(RB_BUFFERS_ON_BIT, p); | |
a3583244 SR |
3963 | |
3964 | (*ppos)++; | |
3965 | ||
3966 | return cnt; | |
3967 | } | |
3968 | ||
5e2336a0 | 3969 | static const struct file_operations rb_simple_fops = { |
a3583244 SR |
3970 | .open = tracing_open_generic, |
3971 | .read = rb_simple_read, | |
3972 | .write = rb_simple_write, | |
3973 | }; | |
3974 | ||
3975 | ||
3976 | static __init int rb_init_debugfs(void) | |
3977 | { | |
3978 | struct dentry *d_tracer; | |
a3583244 SR |
3979 | |
3980 | d_tracer = tracing_init_dentry(); | |
3981 | ||
5452af66 FW |
3982 | trace_create_file("tracing_on", 0644, d_tracer, |
3983 | &ring_buffer_flags, &rb_simple_fops); | |
a3583244 SR |
3984 | |
3985 | return 0; | |
3986 | } | |
3987 | ||
3988 | fs_initcall(rb_init_debugfs); | |
1155de47 | 3989 | #endif |
554f786e | 3990 | |
59222efe | 3991 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
3992 | static int rb_cpu_notify(struct notifier_block *self, |
3993 | unsigned long action, void *hcpu) | |
554f786e SR |
3994 | { |
3995 | struct ring_buffer *buffer = | |
3996 | container_of(self, struct ring_buffer, cpu_notify); | |
3997 | long cpu = (long)hcpu; | |
3998 | ||
3999 | switch (action) { | |
4000 | case CPU_UP_PREPARE: | |
4001 | case CPU_UP_PREPARE_FROZEN: | |
3f237a79 | 4002 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
554f786e SR |
4003 | return NOTIFY_OK; |
4004 | ||
4005 | buffer->buffers[cpu] = | |
4006 | rb_allocate_cpu_buffer(buffer, cpu); | |
4007 | if (!buffer->buffers[cpu]) { | |
4008 | WARN(1, "failed to allocate ring buffer on CPU %ld\n", | |
4009 | cpu); | |
4010 | return NOTIFY_OK; | |
4011 | } | |
4012 | smp_wmb(); | |
3f237a79 | 4013 | cpumask_set_cpu(cpu, buffer->cpumask); |
554f786e SR |
4014 | break; |
4015 | case CPU_DOWN_PREPARE: | |
4016 | case CPU_DOWN_PREPARE_FROZEN: | |
4017 | /* | |
4018 | * Do nothing. | |
4019 | * If we were to free the buffer, then the user would | |
4020 | * lose any trace that was in the buffer. | |
4021 | */ | |
4022 | break; | |
4023 | default: | |
4024 | break; | |
4025 | } | |
4026 | return NOTIFY_OK; | |
4027 | } | |
4028 | #endif |