Commit | Line | Data |
---|---|---|
7a8e76a3 SR |
1 | /* |
2 | * Generic ring buffer | |
3 | * | |
4 | * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com> | |
5 | */ | |
0b07436d | 6 | #include <linux/ftrace_event.h> |
7a8e76a3 | 7 | #include <linux/ring_buffer.h> |
14131f2f | 8 | #include <linux/trace_clock.h> |
0b07436d | 9 | #include <linux/trace_seq.h> |
7a8e76a3 | 10 | #include <linux/spinlock.h> |
15693458 | 11 | #include <linux/irq_work.h> |
7a8e76a3 SR |
12 | #include <linux/debugfs.h> |
13 | #include <linux/uaccess.h> | |
a81bd80a | 14 | #include <linux/hardirq.h> |
6c43e554 | 15 | #include <linux/kthread.h> /* for self test */ |
1744a21d | 16 | #include <linux/kmemcheck.h> |
7a8e76a3 SR |
17 | #include <linux/module.h> |
18 | #include <linux/percpu.h> | |
19 | #include <linux/mutex.h> | |
6c43e554 | 20 | #include <linux/delay.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
7a8e76a3 SR |
22 | #include <linux/init.h> |
23 | #include <linux/hash.h> | |
24 | #include <linux/list.h> | |
554f786e | 25 | #include <linux/cpu.h> |
7a8e76a3 SR |
26 | #include <linux/fs.h> |
27 | ||
79615760 | 28 | #include <asm/local.h> |
182e9f5f | 29 | |
83f40318 VN |
30 | static void update_pages_handler(struct work_struct *work); |
31 | ||
d1b182a8 SR |
32 | /* |
33 | * The ring buffer header is special. We must manually up keep it. | |
34 | */ | |
35 | int ring_buffer_print_entry_header(struct trace_seq *s) | |
36 | { | |
37 | int ret; | |
38 | ||
146c3442 J |
39 | ret = trace_seq_puts(s, "# compressed entry header\n"); |
40 | ret = trace_seq_puts(s, "\ttype_len : 5 bits\n"); | |
41 | ret = trace_seq_puts(s, "\ttime_delta : 27 bits\n"); | |
42 | ret = trace_seq_puts(s, "\tarray : 32 bits\n"); | |
43 | ret = trace_seq_putc(s, '\n'); | |
d1b182a8 SR |
44 | ret = trace_seq_printf(s, "\tpadding : type == %d\n", |
45 | RINGBUF_TYPE_PADDING); | |
46 | ret = trace_seq_printf(s, "\ttime_extend : type == %d\n", | |
47 | RINGBUF_TYPE_TIME_EXTEND); | |
334d4169 LJ |
48 | ret = trace_seq_printf(s, "\tdata max type_len == %d\n", |
49 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX); | |
d1b182a8 SR |
50 | |
51 | return ret; | |
52 | } | |
53 | ||
5cc98548 SR |
54 | /* |
55 | * The ring buffer is made up of a list of pages. A separate list of pages is | |
56 | * allocated for each CPU. A writer may only write to a buffer that is | |
57 | * associated with the CPU it is currently executing on. A reader may read | |
58 | * from any per cpu buffer. | |
59 | * | |
60 | * The reader is special. For each per cpu buffer, the reader has its own | |
61 | * reader page. When a reader has read the entire reader page, this reader | |
62 | * page is swapped with another page in the ring buffer. | |
63 | * | |
64 | * Now, as long as the writer is off the reader page, the reader can do what | |
65 | * ever it wants with that page. The writer will never write to that page | |
66 | * again (as long as it is out of the ring buffer). | |
67 | * | |
68 | * Here's some silly ASCII art. | |
69 | * | |
70 | * +------+ | |
71 | * |reader| RING BUFFER | |
72 | * |page | | |
73 | * +------+ +---+ +---+ +---+ | |
74 | * | |-->| |-->| | | |
75 | * +---+ +---+ +---+ | |
76 | * ^ | | |
77 | * | | | |
78 | * +---------------+ | |
79 | * | |
80 | * | |
81 | * +------+ | |
82 | * |reader| RING BUFFER | |
83 | * |page |------------------v | |
84 | * +------+ +---+ +---+ +---+ | |
85 | * | |-->| |-->| | | |
86 | * +---+ +---+ +---+ | |
87 | * ^ | | |
88 | * | | | |
89 | * +---------------+ | |
90 | * | |
91 | * | |
92 | * +------+ | |
93 | * |reader| RING BUFFER | |
94 | * |page |------------------v | |
95 | * +------+ +---+ +---+ +---+ | |
96 | * ^ | |-->| |-->| | | |
97 | * | +---+ +---+ +---+ | |
98 | * | | | |
99 | * | | | |
100 | * +------------------------------+ | |
101 | * | |
102 | * | |
103 | * +------+ | |
104 | * |buffer| RING BUFFER | |
105 | * |page |------------------v | |
106 | * +------+ +---+ +---+ +---+ | |
107 | * ^ | | | |-->| | | |
108 | * | New +---+ +---+ +---+ | |
109 | * | Reader------^ | | |
110 | * | page | | |
111 | * +------------------------------+ | |
112 | * | |
113 | * | |
114 | * After we make this swap, the reader can hand this page off to the splice | |
115 | * code and be done with it. It can even allocate a new page if it needs to | |
116 | * and swap that into the ring buffer. | |
117 | * | |
118 | * We will be using cmpxchg soon to make all this lockless. | |
119 | * | |
120 | */ | |
121 | ||
033601a3 SR |
122 | /* |
123 | * A fast way to enable or disable all ring buffers is to | |
124 | * call tracing_on or tracing_off. Turning off the ring buffers | |
125 | * prevents all ring buffers from being recorded to. | |
126 | * Turning this switch on, makes it OK to write to the | |
127 | * ring buffer, if the ring buffer is enabled itself. | |
128 | * | |
129 | * There's three layers that must be on in order to write | |
130 | * to the ring buffer. | |
131 | * | |
132 | * 1) This global flag must be set. | |
133 | * 2) The ring buffer must be enabled for recording. | |
134 | * 3) The per cpu buffer must be enabled for recording. | |
135 | * | |
136 | * In case of an anomaly, this global flag has a bit set that | |
137 | * will permantly disable all ring buffers. | |
138 | */ | |
139 | ||
140 | /* | |
141 | * Global flag to disable all recording to ring buffers | |
142 | * This has two bits: ON, DISABLED | |
143 | * | |
144 | * ON DISABLED | |
145 | * ---- ---------- | |
146 | * 0 0 : ring buffers are off | |
147 | * 1 0 : ring buffers are on | |
148 | * X 1 : ring buffers are permanently disabled | |
149 | */ | |
150 | ||
151 | enum { | |
152 | RB_BUFFERS_ON_BIT = 0, | |
153 | RB_BUFFERS_DISABLED_BIT = 1, | |
154 | }; | |
155 | ||
156 | enum { | |
157 | RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT, | |
158 | RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT, | |
159 | }; | |
160 | ||
5e39841c | 161 | static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON; |
a3583244 | 162 | |
499e5470 SR |
163 | /* Used for individual buffers (after the counter) */ |
164 | #define RB_BUFFER_OFF (1 << 20) | |
a3583244 | 165 | |
499e5470 | 166 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
033601a3 SR |
167 | |
168 | /** | |
169 | * tracing_off_permanent - permanently disable ring buffers | |
170 | * | |
171 | * This function, once called, will disable all ring buffers | |
c3706f00 | 172 | * permanently. |
033601a3 SR |
173 | */ |
174 | void tracing_off_permanent(void) | |
175 | { | |
176 | set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags); | |
a3583244 SR |
177 | } |
178 | ||
e3d6bf0a | 179 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
67d34724 | 180 | #define RB_ALIGNMENT 4U |
334d4169 | 181 | #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
c7b09308 | 182 | #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */ |
334d4169 | 183 | |
649508f6 | 184 | #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS |
2271048d SR |
185 | # define RB_FORCE_8BYTE_ALIGNMENT 0 |
186 | # define RB_ARCH_ALIGNMENT RB_ALIGNMENT | |
187 | #else | |
188 | # define RB_FORCE_8BYTE_ALIGNMENT 1 | |
189 | # define RB_ARCH_ALIGNMENT 8U | |
190 | #endif | |
191 | ||
649508f6 JH |
192 | #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT) |
193 | ||
334d4169 LJ |
194 | /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ |
195 | #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX | |
7a8e76a3 SR |
196 | |
197 | enum { | |
198 | RB_LEN_TIME_EXTEND = 8, | |
199 | RB_LEN_TIME_STAMP = 16, | |
200 | }; | |
201 | ||
69d1b839 SR |
202 | #define skip_time_extend(event) \ |
203 | ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND)) | |
204 | ||
2d622719 TZ |
205 | static inline int rb_null_event(struct ring_buffer_event *event) |
206 | { | |
a1863c21 | 207 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
2d622719 TZ |
208 | } |
209 | ||
210 | static void rb_event_set_padding(struct ring_buffer_event *event) | |
211 | { | |
a1863c21 | 212 | /* padding has a NULL time_delta */ |
334d4169 | 213 | event->type_len = RINGBUF_TYPE_PADDING; |
2d622719 TZ |
214 | event->time_delta = 0; |
215 | } | |
216 | ||
34a148bf | 217 | static unsigned |
2d622719 | 218 | rb_event_data_length(struct ring_buffer_event *event) |
7a8e76a3 SR |
219 | { |
220 | unsigned length; | |
221 | ||
334d4169 LJ |
222 | if (event->type_len) |
223 | length = event->type_len * RB_ALIGNMENT; | |
2d622719 TZ |
224 | else |
225 | length = event->array[0]; | |
226 | return length + RB_EVNT_HDR_SIZE; | |
227 | } | |
228 | ||
69d1b839 SR |
229 | /* |
230 | * Return the length of the given event. Will return | |
231 | * the length of the time extend if the event is a | |
232 | * time extend. | |
233 | */ | |
234 | static inline unsigned | |
2d622719 TZ |
235 | rb_event_length(struct ring_buffer_event *event) |
236 | { | |
334d4169 | 237 | switch (event->type_len) { |
7a8e76a3 | 238 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
239 | if (rb_null_event(event)) |
240 | /* undefined */ | |
241 | return -1; | |
334d4169 | 242 | return event->array[0] + RB_EVNT_HDR_SIZE; |
7a8e76a3 SR |
243 | |
244 | case RINGBUF_TYPE_TIME_EXTEND: | |
245 | return RB_LEN_TIME_EXTEND; | |
246 | ||
247 | case RINGBUF_TYPE_TIME_STAMP: | |
248 | return RB_LEN_TIME_STAMP; | |
249 | ||
250 | case RINGBUF_TYPE_DATA: | |
2d622719 | 251 | return rb_event_data_length(event); |
7a8e76a3 SR |
252 | default: |
253 | BUG(); | |
254 | } | |
255 | /* not hit */ | |
256 | return 0; | |
257 | } | |
258 | ||
69d1b839 SR |
259 | /* |
260 | * Return total length of time extend and data, | |
261 | * or just the event length for all other events. | |
262 | */ | |
263 | static inline unsigned | |
264 | rb_event_ts_length(struct ring_buffer_event *event) | |
265 | { | |
266 | unsigned len = 0; | |
267 | ||
268 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) { | |
269 | /* time extends include the data event after it */ | |
270 | len = RB_LEN_TIME_EXTEND; | |
271 | event = skip_time_extend(event); | |
272 | } | |
273 | return len + rb_event_length(event); | |
274 | } | |
275 | ||
7a8e76a3 SR |
276 | /** |
277 | * ring_buffer_event_length - return the length of the event | |
278 | * @event: the event to get the length of | |
69d1b839 SR |
279 | * |
280 | * Returns the size of the data load of a data event. | |
281 | * If the event is something other than a data event, it | |
282 | * returns the size of the event itself. With the exception | |
283 | * of a TIME EXTEND, where it still returns the size of the | |
284 | * data load of the data event after it. | |
7a8e76a3 SR |
285 | */ |
286 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) | |
287 | { | |
69d1b839 SR |
288 | unsigned length; |
289 | ||
290 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) | |
291 | event = skip_time_extend(event); | |
292 | ||
293 | length = rb_event_length(event); | |
334d4169 | 294 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
465634ad RR |
295 | return length; |
296 | length -= RB_EVNT_HDR_SIZE; | |
297 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) | |
298 | length -= sizeof(event->array[0]); | |
299 | return length; | |
7a8e76a3 | 300 | } |
c4f50183 | 301 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
7a8e76a3 SR |
302 | |
303 | /* inline for ring buffer fast paths */ | |
34a148bf | 304 | static void * |
7a8e76a3 SR |
305 | rb_event_data(struct ring_buffer_event *event) |
306 | { | |
69d1b839 SR |
307 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) |
308 | event = skip_time_extend(event); | |
334d4169 | 309 | BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
7a8e76a3 | 310 | /* If length is in len field, then array[0] has the data */ |
334d4169 | 311 | if (event->type_len) |
7a8e76a3 SR |
312 | return (void *)&event->array[0]; |
313 | /* Otherwise length is in array[0] and array[1] has the data */ | |
314 | return (void *)&event->array[1]; | |
315 | } | |
316 | ||
317 | /** | |
318 | * ring_buffer_event_data - return the data of the event | |
319 | * @event: the event to get the data from | |
320 | */ | |
321 | void *ring_buffer_event_data(struct ring_buffer_event *event) | |
322 | { | |
323 | return rb_event_data(event); | |
324 | } | |
c4f50183 | 325 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
7a8e76a3 SR |
326 | |
327 | #define for_each_buffer_cpu(buffer, cpu) \ | |
9e01c1b7 | 328 | for_each_cpu(cpu, buffer->cpumask) |
7a8e76a3 SR |
329 | |
330 | #define TS_SHIFT 27 | |
331 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) | |
332 | #define TS_DELTA_TEST (~TS_MASK) | |
333 | ||
66a8cb95 SR |
334 | /* Flag when events were overwritten */ |
335 | #define RB_MISSED_EVENTS (1 << 31) | |
ff0ff84a SR |
336 | /* Missed count stored at end */ |
337 | #define RB_MISSED_STORED (1 << 30) | |
66a8cb95 | 338 | |
abc9b56d | 339 | struct buffer_data_page { |
e4c2ce82 | 340 | u64 time_stamp; /* page time stamp */ |
c3706f00 | 341 | local_t commit; /* write committed index */ |
649508f6 | 342 | unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */ |
abc9b56d SR |
343 | }; |
344 | ||
77ae365e SR |
345 | /* |
346 | * Note, the buffer_page list must be first. The buffer pages | |
347 | * are allocated in cache lines, which means that each buffer | |
348 | * page will be at the beginning of a cache line, and thus | |
349 | * the least significant bits will be zero. We use this to | |
350 | * add flags in the list struct pointers, to make the ring buffer | |
351 | * lockless. | |
352 | */ | |
abc9b56d | 353 | struct buffer_page { |
778c55d4 | 354 | struct list_head list; /* list of buffer pages */ |
abc9b56d | 355 | local_t write; /* index for next write */ |
6f807acd | 356 | unsigned read; /* index for next read */ |
778c55d4 | 357 | local_t entries; /* entries on this page */ |
ff0ff84a | 358 | unsigned long real_end; /* real end of data */ |
abc9b56d | 359 | struct buffer_data_page *page; /* Actual data page */ |
7a8e76a3 SR |
360 | }; |
361 | ||
77ae365e SR |
362 | /* |
363 | * The buffer page counters, write and entries, must be reset | |
364 | * atomically when crossing page boundaries. To synchronize this | |
365 | * update, two counters are inserted into the number. One is | |
366 | * the actual counter for the write position or count on the page. | |
367 | * | |
368 | * The other is a counter of updaters. Before an update happens | |
369 | * the update partition of the counter is incremented. This will | |
370 | * allow the updater to update the counter atomically. | |
371 | * | |
372 | * The counter is 20 bits, and the state data is 12. | |
373 | */ | |
374 | #define RB_WRITE_MASK 0xfffff | |
375 | #define RB_WRITE_INTCNT (1 << 20) | |
376 | ||
044fa782 | 377 | static void rb_init_page(struct buffer_data_page *bpage) |
abc9b56d | 378 | { |
044fa782 | 379 | local_set(&bpage->commit, 0); |
abc9b56d SR |
380 | } |
381 | ||
474d32b6 SR |
382 | /** |
383 | * ring_buffer_page_len - the size of data on the page. | |
384 | * @page: The page to read | |
385 | * | |
386 | * Returns the amount of data on the page, including buffer page header. | |
387 | */ | |
ef7a4a16 SR |
388 | size_t ring_buffer_page_len(void *page) |
389 | { | |
474d32b6 SR |
390 | return local_read(&((struct buffer_data_page *)page)->commit) |
391 | + BUF_PAGE_HDR_SIZE; | |
ef7a4a16 SR |
392 | } |
393 | ||
ed56829c SR |
394 | /* |
395 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing | |
396 | * this issue out. | |
397 | */ | |
34a148bf | 398 | static void free_buffer_page(struct buffer_page *bpage) |
ed56829c | 399 | { |
34a148bf | 400 | free_page((unsigned long)bpage->page); |
e4c2ce82 | 401 | kfree(bpage); |
ed56829c SR |
402 | } |
403 | ||
7a8e76a3 SR |
404 | /* |
405 | * We need to fit the time_stamp delta into 27 bits. | |
406 | */ | |
407 | static inline int test_time_stamp(u64 delta) | |
408 | { | |
409 | if (delta & TS_DELTA_TEST) | |
410 | return 1; | |
411 | return 0; | |
412 | } | |
413 | ||
474d32b6 | 414 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
7a8e76a3 | 415 | |
be957c44 SR |
416 | /* Max payload is BUF_PAGE_SIZE - header (8bytes) */ |
417 | #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2)) | |
418 | ||
d1b182a8 SR |
419 | int ring_buffer_print_page_header(struct trace_seq *s) |
420 | { | |
421 | struct buffer_data_page field; | |
422 | int ret; | |
423 | ||
424 | ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t" | |
26a50744 TZ |
425 | "offset:0;\tsize:%u;\tsigned:%u;\n", |
426 | (unsigned int)sizeof(field.time_stamp), | |
427 | (unsigned int)is_signed_type(u64)); | |
d1b182a8 SR |
428 | |
429 | ret = trace_seq_printf(s, "\tfield: local_t commit;\t" | |
26a50744 | 430 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 431 | (unsigned int)offsetof(typeof(field), commit), |
26a50744 TZ |
432 | (unsigned int)sizeof(field.commit), |
433 | (unsigned int)is_signed_type(long)); | |
d1b182a8 | 434 | |
66a8cb95 SR |
435 | ret = trace_seq_printf(s, "\tfield: int overwrite;\t" |
436 | "offset:%u;\tsize:%u;\tsigned:%u;\n", | |
437 | (unsigned int)offsetof(typeof(field), commit), | |
438 | 1, | |
439 | (unsigned int)is_signed_type(long)); | |
440 | ||
d1b182a8 | 441 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
26a50744 | 442 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
d1b182a8 | 443 | (unsigned int)offsetof(typeof(field), data), |
26a50744 TZ |
444 | (unsigned int)BUF_PAGE_SIZE, |
445 | (unsigned int)is_signed_type(char)); | |
d1b182a8 SR |
446 | |
447 | return ret; | |
448 | } | |
449 | ||
15693458 SRRH |
450 | struct rb_irq_work { |
451 | struct irq_work work; | |
452 | wait_queue_head_t waiters; | |
453 | bool waiters_pending; | |
454 | }; | |
455 | ||
7a8e76a3 SR |
456 | /* |
457 | * head_page == tail_page && head == tail then buffer is empty. | |
458 | */ | |
459 | struct ring_buffer_per_cpu { | |
460 | int cpu; | |
985023de | 461 | atomic_t record_disabled; |
7a8e76a3 | 462 | struct ring_buffer *buffer; |
5389f6fa | 463 | raw_spinlock_t reader_lock; /* serialize readers */ |
445c8951 | 464 | arch_spinlock_t lock; |
7a8e76a3 | 465 | struct lock_class_key lock_key; |
438ced17 | 466 | unsigned int nr_pages; |
3adc54fa | 467 | struct list_head *pages; |
6f807acd SR |
468 | struct buffer_page *head_page; /* read from head */ |
469 | struct buffer_page *tail_page; /* write to tail */ | |
c3706f00 | 470 | struct buffer_page *commit_page; /* committed pages */ |
d769041f | 471 | struct buffer_page *reader_page; |
66a8cb95 SR |
472 | unsigned long lost_events; |
473 | unsigned long last_overrun; | |
c64e148a | 474 | local_t entries_bytes; |
e4906eff | 475 | local_t entries; |
884bfe89 SP |
476 | local_t overrun; |
477 | local_t commit_overrun; | |
478 | local_t dropped_events; | |
fa743953 SR |
479 | local_t committing; |
480 | local_t commits; | |
77ae365e | 481 | unsigned long read; |
c64e148a | 482 | unsigned long read_bytes; |
7a8e76a3 SR |
483 | u64 write_stamp; |
484 | u64 read_stamp; | |
438ced17 VN |
485 | /* ring buffer pages to update, > 0 to add, < 0 to remove */ |
486 | int nr_pages_to_update; | |
487 | struct list_head new_pages; /* new pages to add */ | |
83f40318 | 488 | struct work_struct update_pages_work; |
05fdd70d | 489 | struct completion update_done; |
15693458 SRRH |
490 | |
491 | struct rb_irq_work irq_work; | |
7a8e76a3 SR |
492 | }; |
493 | ||
494 | struct ring_buffer { | |
7a8e76a3 SR |
495 | unsigned flags; |
496 | int cpus; | |
7a8e76a3 | 497 | atomic_t record_disabled; |
83f40318 | 498 | atomic_t resize_disabled; |
00f62f61 | 499 | cpumask_var_t cpumask; |
7a8e76a3 | 500 | |
1f8a6a10 PZ |
501 | struct lock_class_key *reader_lock_key; |
502 | ||
7a8e76a3 SR |
503 | struct mutex mutex; |
504 | ||
505 | struct ring_buffer_per_cpu **buffers; | |
554f786e | 506 | |
59222efe | 507 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
508 | struct notifier_block cpu_notify; |
509 | #endif | |
37886f6a | 510 | u64 (*clock)(void); |
15693458 SRRH |
511 | |
512 | struct rb_irq_work irq_work; | |
7a8e76a3 SR |
513 | }; |
514 | ||
515 | struct ring_buffer_iter { | |
516 | struct ring_buffer_per_cpu *cpu_buffer; | |
517 | unsigned long head; | |
518 | struct buffer_page *head_page; | |
492a74f4 SR |
519 | struct buffer_page *cache_reader_page; |
520 | unsigned long cache_read; | |
7a8e76a3 SR |
521 | u64 read_stamp; |
522 | }; | |
523 | ||
15693458 SRRH |
524 | /* |
525 | * rb_wake_up_waiters - wake up tasks waiting for ring buffer input | |
526 | * | |
527 | * Schedules a delayed work to wake up any task that is blocked on the | |
528 | * ring buffer waiters queue. | |
529 | */ | |
530 | static void rb_wake_up_waiters(struct irq_work *work) | |
531 | { | |
532 | struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work); | |
533 | ||
534 | wake_up_all(&rbwork->waiters); | |
535 | } | |
536 | ||
537 | /** | |
538 | * ring_buffer_wait - wait for input to the ring buffer | |
539 | * @buffer: buffer to wait on | |
540 | * @cpu: the cpu buffer to wait on | |
e30f53aa | 541 | * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS |
15693458 SRRH |
542 | * |
543 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon | |
544 | * as data is added to any of the @buffer's cpu buffers. Otherwise | |
545 | * it will wait for data to be added to a specific cpu buffer. | |
546 | */ | |
e30f53aa | 547 | int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full) |
15693458 | 548 | { |
e30f53aa | 549 | struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer); |
15693458 SRRH |
550 | DEFINE_WAIT(wait); |
551 | struct rb_irq_work *work; | |
e30f53aa | 552 | int ret = 0; |
15693458 SRRH |
553 | |
554 | /* | |
555 | * Depending on what the caller is waiting for, either any | |
556 | * data in any cpu buffer, or a specific buffer, put the | |
557 | * caller on the appropriate wait queue. | |
558 | */ | |
559 | if (cpu == RING_BUFFER_ALL_CPUS) | |
560 | work = &buffer->irq_work; | |
561 | else { | |
8b8b3683 SRRH |
562 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
563 | return -ENODEV; | |
15693458 SRRH |
564 | cpu_buffer = buffer->buffers[cpu]; |
565 | work = &cpu_buffer->irq_work; | |
566 | } | |
567 | ||
568 | ||
e30f53aa RV |
569 | while (true) { |
570 | prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE); | |
571 | ||
572 | /* | |
573 | * The events can happen in critical sections where | |
574 | * checking a work queue can cause deadlocks. | |
575 | * After adding a task to the queue, this flag is set | |
576 | * only to notify events to try to wake up the queue | |
577 | * using irq_work. | |
578 | * | |
579 | * We don't clear it even if the buffer is no longer | |
580 | * empty. The flag only causes the next event to run | |
581 | * irq_work to do the work queue wake up. The worse | |
582 | * that can happen if we race with !trace_empty() is that | |
583 | * an event will cause an irq_work to try to wake up | |
584 | * an empty queue. | |
585 | * | |
586 | * There's no reason to protect this flag either, as | |
587 | * the work queue and irq_work logic will do the necessary | |
588 | * synchronization for the wake ups. The only thing | |
589 | * that is necessary is that the wake up happens after | |
590 | * a task has been queued. It's OK for spurious wake ups. | |
591 | */ | |
592 | work->waiters_pending = true; | |
593 | ||
594 | if (signal_pending(current)) { | |
595 | ret = -EINTR; | |
596 | break; | |
597 | } | |
598 | ||
599 | if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) | |
600 | break; | |
601 | ||
602 | if (cpu != RING_BUFFER_ALL_CPUS && | |
603 | !ring_buffer_empty_cpu(buffer, cpu)) { | |
604 | unsigned long flags; | |
605 | bool pagebusy; | |
606 | ||
607 | if (!full) | |
608 | break; | |
609 | ||
610 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); | |
611 | pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page; | |
612 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | |
613 | ||
614 | if (!pagebusy) | |
615 | break; | |
616 | } | |
15693458 | 617 | |
15693458 | 618 | schedule(); |
e30f53aa | 619 | } |
15693458 SRRH |
620 | |
621 | finish_wait(&work->waiters, &wait); | |
e30f53aa RV |
622 | |
623 | return ret; | |
15693458 SRRH |
624 | } |
625 | ||
626 | /** | |
627 | * ring_buffer_poll_wait - poll on buffer input | |
628 | * @buffer: buffer to wait on | |
629 | * @cpu: the cpu buffer to wait on | |
630 | * @filp: the file descriptor | |
631 | * @poll_table: The poll descriptor | |
632 | * | |
633 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon | |
634 | * as data is added to any of the @buffer's cpu buffers. Otherwise | |
635 | * it will wait for data to be added to a specific cpu buffer. | |
636 | * | |
637 | * Returns POLLIN | POLLRDNORM if data exists in the buffers, | |
638 | * zero otherwise. | |
639 | */ | |
640 | int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, | |
641 | struct file *filp, poll_table *poll_table) | |
642 | { | |
643 | struct ring_buffer_per_cpu *cpu_buffer; | |
644 | struct rb_irq_work *work; | |
645 | ||
15693458 SRRH |
646 | if (cpu == RING_BUFFER_ALL_CPUS) |
647 | work = &buffer->irq_work; | |
648 | else { | |
6721cb60 SRRH |
649 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
650 | return -EINVAL; | |
651 | ||
15693458 SRRH |
652 | cpu_buffer = buffer->buffers[cpu]; |
653 | work = &cpu_buffer->irq_work; | |
654 | } | |
655 | ||
15693458 | 656 | poll_wait(filp, &work->waiters, poll_table); |
4ce97dbf JB |
657 | work->waiters_pending = true; |
658 | /* | |
659 | * There's a tight race between setting the waiters_pending and | |
660 | * checking if the ring buffer is empty. Once the waiters_pending bit | |
661 | * is set, the next event will wake the task up, but we can get stuck | |
662 | * if there's only a single event in. | |
663 | * | |
664 | * FIXME: Ideally, we need a memory barrier on the writer side as well, | |
665 | * but adding a memory barrier to all events will cause too much of a | |
666 | * performance hit in the fast path. We only need a memory barrier when | |
667 | * the buffer goes from empty to having content. But as this race is | |
668 | * extremely small, and it's not a problem if another event comes in, we | |
669 | * will fix it later. | |
670 | */ | |
671 | smp_mb(); | |
15693458 SRRH |
672 | |
673 | if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) || | |
674 | (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu))) | |
675 | return POLLIN | POLLRDNORM; | |
676 | return 0; | |
677 | } | |
678 | ||
f536aafc | 679 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
077c5407 SR |
680 | #define RB_WARN_ON(b, cond) \ |
681 | ({ \ | |
682 | int _____ret = unlikely(cond); \ | |
683 | if (_____ret) { \ | |
684 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ | |
685 | struct ring_buffer_per_cpu *__b = \ | |
686 | (void *)b; \ | |
687 | atomic_inc(&__b->buffer->record_disabled); \ | |
688 | } else \ | |
689 | atomic_inc(&b->record_disabled); \ | |
690 | WARN_ON(1); \ | |
691 | } \ | |
692 | _____ret; \ | |
3e89c7bb | 693 | }) |
f536aafc | 694 | |
37886f6a SR |
695 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
696 | #define DEBUG_SHIFT 0 | |
697 | ||
6d3f1e12 | 698 | static inline u64 rb_time_stamp(struct ring_buffer *buffer) |
88eb0125 SR |
699 | { |
700 | /* shift to debug/test normalization and TIME_EXTENTS */ | |
701 | return buffer->clock() << DEBUG_SHIFT; | |
702 | } | |
703 | ||
37886f6a SR |
704 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu) |
705 | { | |
706 | u64 time; | |
707 | ||
708 | preempt_disable_notrace(); | |
6d3f1e12 | 709 | time = rb_time_stamp(buffer); |
37886f6a SR |
710 | preempt_enable_no_resched_notrace(); |
711 | ||
712 | return time; | |
713 | } | |
714 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); | |
715 | ||
716 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, | |
717 | int cpu, u64 *ts) | |
718 | { | |
719 | /* Just stupid testing the normalize function and deltas */ | |
720 | *ts >>= DEBUG_SHIFT; | |
721 | } | |
722 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); | |
723 | ||
77ae365e SR |
724 | /* |
725 | * Making the ring buffer lockless makes things tricky. | |
726 | * Although writes only happen on the CPU that they are on, | |
727 | * and they only need to worry about interrupts. Reads can | |
728 | * happen on any CPU. | |
729 | * | |
730 | * The reader page is always off the ring buffer, but when the | |
731 | * reader finishes with a page, it needs to swap its page with | |
732 | * a new one from the buffer. The reader needs to take from | |
733 | * the head (writes go to the tail). But if a writer is in overwrite | |
734 | * mode and wraps, it must push the head page forward. | |
735 | * | |
736 | * Here lies the problem. | |
737 | * | |
738 | * The reader must be careful to replace only the head page, and | |
739 | * not another one. As described at the top of the file in the | |
740 | * ASCII art, the reader sets its old page to point to the next | |
741 | * page after head. It then sets the page after head to point to | |
742 | * the old reader page. But if the writer moves the head page | |
743 | * during this operation, the reader could end up with the tail. | |
744 | * | |
745 | * We use cmpxchg to help prevent this race. We also do something | |
746 | * special with the page before head. We set the LSB to 1. | |
747 | * | |
748 | * When the writer must push the page forward, it will clear the | |
749 | * bit that points to the head page, move the head, and then set | |
750 | * the bit that points to the new head page. | |
751 | * | |
752 | * We also don't want an interrupt coming in and moving the head | |
753 | * page on another writer. Thus we use the second LSB to catch | |
754 | * that too. Thus: | |
755 | * | |
756 | * head->list->prev->next bit 1 bit 0 | |
757 | * ------- ------- | |
758 | * Normal page 0 0 | |
759 | * Points to head page 0 1 | |
760 | * New head page 1 0 | |
761 | * | |
762 | * Note we can not trust the prev pointer of the head page, because: | |
763 | * | |
764 | * +----+ +-----+ +-----+ | |
765 | * | |------>| T |---X--->| N | | |
766 | * | |<------| | | | | |
767 | * +----+ +-----+ +-----+ | |
768 | * ^ ^ | | |
769 | * | +-----+ | | | |
770 | * +----------| R |----------+ | | |
771 | * | |<-----------+ | |
772 | * +-----+ | |
773 | * | |
774 | * Key: ---X--> HEAD flag set in pointer | |
775 | * T Tail page | |
776 | * R Reader page | |
777 | * N Next page | |
778 | * | |
779 | * (see __rb_reserve_next() to see where this happens) | |
780 | * | |
781 | * What the above shows is that the reader just swapped out | |
782 | * the reader page with a page in the buffer, but before it | |
783 | * could make the new header point back to the new page added | |
784 | * it was preempted by a writer. The writer moved forward onto | |
785 | * the new page added by the reader and is about to move forward | |
786 | * again. | |
787 | * | |
788 | * You can see, it is legitimate for the previous pointer of | |
789 | * the head (or any page) not to point back to itself. But only | |
790 | * temporarially. | |
791 | */ | |
792 | ||
793 | #define RB_PAGE_NORMAL 0UL | |
794 | #define RB_PAGE_HEAD 1UL | |
795 | #define RB_PAGE_UPDATE 2UL | |
796 | ||
797 | ||
798 | #define RB_FLAG_MASK 3UL | |
799 | ||
800 | /* PAGE_MOVED is not part of the mask */ | |
801 | #define RB_PAGE_MOVED 4UL | |
802 | ||
803 | /* | |
804 | * rb_list_head - remove any bit | |
805 | */ | |
806 | static struct list_head *rb_list_head(struct list_head *list) | |
807 | { | |
808 | unsigned long val = (unsigned long)list; | |
809 | ||
810 | return (struct list_head *)(val & ~RB_FLAG_MASK); | |
811 | } | |
812 | ||
813 | /* | |
6d3f1e12 | 814 | * rb_is_head_page - test if the given page is the head page |
77ae365e SR |
815 | * |
816 | * Because the reader may move the head_page pointer, we can | |
817 | * not trust what the head page is (it may be pointing to | |
818 | * the reader page). But if the next page is a header page, | |
819 | * its flags will be non zero. | |
820 | */ | |
42b16b3f | 821 | static inline int |
77ae365e SR |
822 | rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
823 | struct buffer_page *page, struct list_head *list) | |
824 | { | |
825 | unsigned long val; | |
826 | ||
827 | val = (unsigned long)list->next; | |
828 | ||
829 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) | |
830 | return RB_PAGE_MOVED; | |
831 | ||
832 | return val & RB_FLAG_MASK; | |
833 | } | |
834 | ||
835 | /* | |
836 | * rb_is_reader_page | |
837 | * | |
838 | * The unique thing about the reader page, is that, if the | |
839 | * writer is ever on it, the previous pointer never points | |
840 | * back to the reader page. | |
841 | */ | |
842 | static int rb_is_reader_page(struct buffer_page *page) | |
843 | { | |
844 | struct list_head *list = page->list.prev; | |
845 | ||
846 | return rb_list_head(list->next) != &page->list; | |
847 | } | |
848 | ||
849 | /* | |
850 | * rb_set_list_to_head - set a list_head to be pointing to head. | |
851 | */ | |
852 | static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer, | |
853 | struct list_head *list) | |
854 | { | |
855 | unsigned long *ptr; | |
856 | ||
857 | ptr = (unsigned long *)&list->next; | |
858 | *ptr |= RB_PAGE_HEAD; | |
859 | *ptr &= ~RB_PAGE_UPDATE; | |
860 | } | |
861 | ||
862 | /* | |
863 | * rb_head_page_activate - sets up head page | |
864 | */ | |
865 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) | |
866 | { | |
867 | struct buffer_page *head; | |
868 | ||
869 | head = cpu_buffer->head_page; | |
870 | if (!head) | |
871 | return; | |
872 | ||
873 | /* | |
874 | * Set the previous list pointer to have the HEAD flag. | |
875 | */ | |
876 | rb_set_list_to_head(cpu_buffer, head->list.prev); | |
877 | } | |
878 | ||
879 | static void rb_list_head_clear(struct list_head *list) | |
880 | { | |
881 | unsigned long *ptr = (unsigned long *)&list->next; | |
882 | ||
883 | *ptr &= ~RB_FLAG_MASK; | |
884 | } | |
885 | ||
886 | /* | |
887 | * rb_head_page_dactivate - clears head page ptr (for free list) | |
888 | */ | |
889 | static void | |
890 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) | |
891 | { | |
892 | struct list_head *hd; | |
893 | ||
894 | /* Go through the whole list and clear any pointers found. */ | |
895 | rb_list_head_clear(cpu_buffer->pages); | |
896 | ||
897 | list_for_each(hd, cpu_buffer->pages) | |
898 | rb_list_head_clear(hd); | |
899 | } | |
900 | ||
901 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, | |
902 | struct buffer_page *head, | |
903 | struct buffer_page *prev, | |
904 | int old_flag, int new_flag) | |
905 | { | |
906 | struct list_head *list; | |
907 | unsigned long val = (unsigned long)&head->list; | |
908 | unsigned long ret; | |
909 | ||
910 | list = &prev->list; | |
911 | ||
912 | val &= ~RB_FLAG_MASK; | |
913 | ||
08a40816 SR |
914 | ret = cmpxchg((unsigned long *)&list->next, |
915 | val | old_flag, val | new_flag); | |
77ae365e SR |
916 | |
917 | /* check if the reader took the page */ | |
918 | if ((ret & ~RB_FLAG_MASK) != val) | |
919 | return RB_PAGE_MOVED; | |
920 | ||
921 | return ret & RB_FLAG_MASK; | |
922 | } | |
923 | ||
924 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, | |
925 | struct buffer_page *head, | |
926 | struct buffer_page *prev, | |
927 | int old_flag) | |
928 | { | |
929 | return rb_head_page_set(cpu_buffer, head, prev, | |
930 | old_flag, RB_PAGE_UPDATE); | |
931 | } | |
932 | ||
933 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, | |
934 | struct buffer_page *head, | |
935 | struct buffer_page *prev, | |
936 | int old_flag) | |
937 | { | |
938 | return rb_head_page_set(cpu_buffer, head, prev, | |
939 | old_flag, RB_PAGE_HEAD); | |
940 | } | |
941 | ||
942 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, | |
943 | struct buffer_page *head, | |
944 | struct buffer_page *prev, | |
945 | int old_flag) | |
946 | { | |
947 | return rb_head_page_set(cpu_buffer, head, prev, | |
948 | old_flag, RB_PAGE_NORMAL); | |
949 | } | |
950 | ||
951 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, | |
952 | struct buffer_page **bpage) | |
953 | { | |
954 | struct list_head *p = rb_list_head((*bpage)->list.next); | |
955 | ||
956 | *bpage = list_entry(p, struct buffer_page, list); | |
957 | } | |
958 | ||
959 | static struct buffer_page * | |
960 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) | |
961 | { | |
962 | struct buffer_page *head; | |
963 | struct buffer_page *page; | |
964 | struct list_head *list; | |
965 | int i; | |
966 | ||
967 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) | |
968 | return NULL; | |
969 | ||
970 | /* sanity check */ | |
971 | list = cpu_buffer->pages; | |
972 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) | |
973 | return NULL; | |
974 | ||
975 | page = head = cpu_buffer->head_page; | |
976 | /* | |
977 | * It is possible that the writer moves the header behind | |
978 | * where we started, and we miss in one loop. | |
979 | * A second loop should grab the header, but we'll do | |
980 | * three loops just because I'm paranoid. | |
981 | */ | |
982 | for (i = 0; i < 3; i++) { | |
983 | do { | |
984 | if (rb_is_head_page(cpu_buffer, page, page->list.prev)) { | |
985 | cpu_buffer->head_page = page; | |
986 | return page; | |
987 | } | |
988 | rb_inc_page(cpu_buffer, &page); | |
989 | } while (page != head); | |
990 | } | |
991 | ||
992 | RB_WARN_ON(cpu_buffer, 1); | |
993 | ||
994 | return NULL; | |
995 | } | |
996 | ||
997 | static int rb_head_page_replace(struct buffer_page *old, | |
998 | struct buffer_page *new) | |
999 | { | |
1000 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; | |
1001 | unsigned long val; | |
1002 | unsigned long ret; | |
1003 | ||
1004 | val = *ptr & ~RB_FLAG_MASK; | |
1005 | val |= RB_PAGE_HEAD; | |
1006 | ||
08a40816 | 1007 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
77ae365e SR |
1008 | |
1009 | return ret == val; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * rb_tail_page_update - move the tail page forward | |
1014 | * | |
1015 | * Returns 1 if moved tail page, 0 if someone else did. | |
1016 | */ | |
1017 | static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, | |
1018 | struct buffer_page *tail_page, | |
1019 | struct buffer_page *next_page) | |
1020 | { | |
1021 | struct buffer_page *old_tail; | |
1022 | unsigned long old_entries; | |
1023 | unsigned long old_write; | |
1024 | int ret = 0; | |
1025 | ||
1026 | /* | |
1027 | * The tail page now needs to be moved forward. | |
1028 | * | |
1029 | * We need to reset the tail page, but without messing | |
1030 | * with possible erasing of data brought in by interrupts | |
1031 | * that have moved the tail page and are currently on it. | |
1032 | * | |
1033 | * We add a counter to the write field to denote this. | |
1034 | */ | |
1035 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); | |
1036 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); | |
1037 | ||
1038 | /* | |
1039 | * Just make sure we have seen our old_write and synchronize | |
1040 | * with any interrupts that come in. | |
1041 | */ | |
1042 | barrier(); | |
1043 | ||
1044 | /* | |
1045 | * If the tail page is still the same as what we think | |
1046 | * it is, then it is up to us to update the tail | |
1047 | * pointer. | |
1048 | */ | |
1049 | if (tail_page == cpu_buffer->tail_page) { | |
1050 | /* Zero the write counter */ | |
1051 | unsigned long val = old_write & ~RB_WRITE_MASK; | |
1052 | unsigned long eval = old_entries & ~RB_WRITE_MASK; | |
1053 | ||
1054 | /* | |
1055 | * This will only succeed if an interrupt did | |
1056 | * not come in and change it. In which case, we | |
1057 | * do not want to modify it. | |
da706d8b LJ |
1058 | * |
1059 | * We add (void) to let the compiler know that we do not care | |
1060 | * about the return value of these functions. We use the | |
1061 | * cmpxchg to only update if an interrupt did not already | |
1062 | * do it for us. If the cmpxchg fails, we don't care. | |
77ae365e | 1063 | */ |
da706d8b LJ |
1064 | (void)local_cmpxchg(&next_page->write, old_write, val); |
1065 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); | |
77ae365e SR |
1066 | |
1067 | /* | |
1068 | * No need to worry about races with clearing out the commit. | |
1069 | * it only can increment when a commit takes place. But that | |
1070 | * only happens in the outer most nested commit. | |
1071 | */ | |
1072 | local_set(&next_page->page->commit, 0); | |
1073 | ||
1074 | old_tail = cmpxchg(&cpu_buffer->tail_page, | |
1075 | tail_page, next_page); | |
1076 | ||
1077 | if (old_tail == tail_page) | |
1078 | ret = 1; | |
1079 | } | |
1080 | ||
1081 | return ret; | |
1082 | } | |
1083 | ||
1084 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, | |
1085 | struct buffer_page *bpage) | |
1086 | { | |
1087 | unsigned long val = (unsigned long)bpage; | |
1088 | ||
1089 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) | |
1090 | return 1; | |
1091 | ||
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | /** | |
1096 | * rb_check_list - make sure a pointer to a list has the last bits zero | |
1097 | */ | |
1098 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, | |
1099 | struct list_head *list) | |
1100 | { | |
1101 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) | |
1102 | return 1; | |
1103 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) | |
1104 | return 1; | |
1105 | return 0; | |
1106 | } | |
1107 | ||
7a8e76a3 | 1108 | /** |
d611851b | 1109 | * rb_check_pages - integrity check of buffer pages |
7a8e76a3 SR |
1110 | * @cpu_buffer: CPU buffer with pages to test |
1111 | * | |
c3706f00 | 1112 | * As a safety measure we check to make sure the data pages have not |
7a8e76a3 SR |
1113 | * been corrupted. |
1114 | */ | |
1115 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) | |
1116 | { | |
3adc54fa | 1117 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 1118 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 1119 | |
308f7eeb SR |
1120 | /* Reset the head page if it exists */ |
1121 | if (cpu_buffer->head_page) | |
1122 | rb_set_head_page(cpu_buffer); | |
1123 | ||
77ae365e SR |
1124 | rb_head_page_deactivate(cpu_buffer); |
1125 | ||
3e89c7bb SR |
1126 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
1127 | return -1; | |
1128 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) | |
1129 | return -1; | |
7a8e76a3 | 1130 | |
77ae365e SR |
1131 | if (rb_check_list(cpu_buffer, head)) |
1132 | return -1; | |
1133 | ||
044fa782 | 1134 | list_for_each_entry_safe(bpage, tmp, head, list) { |
3e89c7bb | 1135 | if (RB_WARN_ON(cpu_buffer, |
044fa782 | 1136 | bpage->list.next->prev != &bpage->list)) |
3e89c7bb SR |
1137 | return -1; |
1138 | if (RB_WARN_ON(cpu_buffer, | |
044fa782 | 1139 | bpage->list.prev->next != &bpage->list)) |
3e89c7bb | 1140 | return -1; |
77ae365e SR |
1141 | if (rb_check_list(cpu_buffer, &bpage->list)) |
1142 | return -1; | |
7a8e76a3 SR |
1143 | } |
1144 | ||
77ae365e SR |
1145 | rb_head_page_activate(cpu_buffer); |
1146 | ||
7a8e76a3 SR |
1147 | return 0; |
1148 | } | |
1149 | ||
438ced17 | 1150 | static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu) |
7a8e76a3 | 1151 | { |
438ced17 | 1152 | int i; |
044fa782 | 1153 | struct buffer_page *bpage, *tmp; |
3adc54fa | 1154 | |
7a8e76a3 | 1155 | for (i = 0; i < nr_pages; i++) { |
7ea59064 | 1156 | struct page *page; |
d7ec4bfe VN |
1157 | /* |
1158 | * __GFP_NORETRY flag makes sure that the allocation fails | |
1159 | * gracefully without invoking oom-killer and the system is | |
1160 | * not destabilized. | |
1161 | */ | |
044fa782 | 1162 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
d7ec4bfe | 1163 | GFP_KERNEL | __GFP_NORETRY, |
438ced17 | 1164 | cpu_to_node(cpu)); |
044fa782 | 1165 | if (!bpage) |
e4c2ce82 | 1166 | goto free_pages; |
77ae365e | 1167 | |
438ced17 | 1168 | list_add(&bpage->list, pages); |
77ae365e | 1169 | |
438ced17 | 1170 | page = alloc_pages_node(cpu_to_node(cpu), |
d7ec4bfe | 1171 | GFP_KERNEL | __GFP_NORETRY, 0); |
7ea59064 | 1172 | if (!page) |
7a8e76a3 | 1173 | goto free_pages; |
7ea59064 | 1174 | bpage->page = page_address(page); |
044fa782 | 1175 | rb_init_page(bpage->page); |
7a8e76a3 SR |
1176 | } |
1177 | ||
438ced17 VN |
1178 | return 0; |
1179 | ||
1180 | free_pages: | |
1181 | list_for_each_entry_safe(bpage, tmp, pages, list) { | |
1182 | list_del_init(&bpage->list); | |
1183 | free_buffer_page(bpage); | |
1184 | } | |
1185 | ||
1186 | return -ENOMEM; | |
1187 | } | |
1188 | ||
1189 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, | |
1190 | unsigned nr_pages) | |
1191 | { | |
1192 | LIST_HEAD(pages); | |
1193 | ||
1194 | WARN_ON(!nr_pages); | |
1195 | ||
1196 | if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu)) | |
1197 | return -ENOMEM; | |
1198 | ||
3adc54fa SR |
1199 | /* |
1200 | * The ring buffer page list is a circular list that does not | |
1201 | * start and end with a list head. All page list items point to | |
1202 | * other pages. | |
1203 | */ | |
1204 | cpu_buffer->pages = pages.next; | |
1205 | list_del(&pages); | |
7a8e76a3 | 1206 | |
438ced17 VN |
1207 | cpu_buffer->nr_pages = nr_pages; |
1208 | ||
7a8e76a3 SR |
1209 | rb_check_pages(cpu_buffer); |
1210 | ||
1211 | return 0; | |
7a8e76a3 SR |
1212 | } |
1213 | ||
1214 | static struct ring_buffer_per_cpu * | |
438ced17 | 1215 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu) |
7a8e76a3 SR |
1216 | { |
1217 | struct ring_buffer_per_cpu *cpu_buffer; | |
044fa782 | 1218 | struct buffer_page *bpage; |
7ea59064 | 1219 | struct page *page; |
7a8e76a3 SR |
1220 | int ret; |
1221 | ||
1222 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), | |
1223 | GFP_KERNEL, cpu_to_node(cpu)); | |
1224 | if (!cpu_buffer) | |
1225 | return NULL; | |
1226 | ||
1227 | cpu_buffer->cpu = cpu; | |
1228 | cpu_buffer->buffer = buffer; | |
5389f6fa | 1229 | raw_spin_lock_init(&cpu_buffer->reader_lock); |
1f8a6a10 | 1230 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
edc35bd7 | 1231 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
83f40318 | 1232 | INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler); |
05fdd70d | 1233 | init_completion(&cpu_buffer->update_done); |
15693458 | 1234 | init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters); |
f1dc6725 | 1235 | init_waitqueue_head(&cpu_buffer->irq_work.waiters); |
7a8e76a3 | 1236 | |
044fa782 | 1237 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
e4c2ce82 | 1238 | GFP_KERNEL, cpu_to_node(cpu)); |
044fa782 | 1239 | if (!bpage) |
e4c2ce82 SR |
1240 | goto fail_free_buffer; |
1241 | ||
77ae365e SR |
1242 | rb_check_bpage(cpu_buffer, bpage); |
1243 | ||
044fa782 | 1244 | cpu_buffer->reader_page = bpage; |
7ea59064 VN |
1245 | page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0); |
1246 | if (!page) | |
e4c2ce82 | 1247 | goto fail_free_reader; |
7ea59064 | 1248 | bpage->page = page_address(page); |
044fa782 | 1249 | rb_init_page(bpage->page); |
e4c2ce82 | 1250 | |
d769041f | 1251 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
44b99462 | 1252 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
d769041f | 1253 | |
438ced17 | 1254 | ret = rb_allocate_pages(cpu_buffer, nr_pages); |
7a8e76a3 | 1255 | if (ret < 0) |
d769041f | 1256 | goto fail_free_reader; |
7a8e76a3 SR |
1257 | |
1258 | cpu_buffer->head_page | |
3adc54fa | 1259 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
bf41a158 | 1260 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
7a8e76a3 | 1261 | |
77ae365e SR |
1262 | rb_head_page_activate(cpu_buffer); |
1263 | ||
7a8e76a3 SR |
1264 | return cpu_buffer; |
1265 | ||
d769041f SR |
1266 | fail_free_reader: |
1267 | free_buffer_page(cpu_buffer->reader_page); | |
1268 | ||
7a8e76a3 SR |
1269 | fail_free_buffer: |
1270 | kfree(cpu_buffer); | |
1271 | return NULL; | |
1272 | } | |
1273 | ||
1274 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) | |
1275 | { | |
3adc54fa | 1276 | struct list_head *head = cpu_buffer->pages; |
044fa782 | 1277 | struct buffer_page *bpage, *tmp; |
7a8e76a3 | 1278 | |
d769041f SR |
1279 | free_buffer_page(cpu_buffer->reader_page); |
1280 | ||
77ae365e SR |
1281 | rb_head_page_deactivate(cpu_buffer); |
1282 | ||
3adc54fa SR |
1283 | if (head) { |
1284 | list_for_each_entry_safe(bpage, tmp, head, list) { | |
1285 | list_del_init(&bpage->list); | |
1286 | free_buffer_page(bpage); | |
1287 | } | |
1288 | bpage = list_entry(head, struct buffer_page, list); | |
044fa782 | 1289 | free_buffer_page(bpage); |
7a8e76a3 | 1290 | } |
3adc54fa | 1291 | |
7a8e76a3 SR |
1292 | kfree(cpu_buffer); |
1293 | } | |
1294 | ||
59222efe | 1295 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
1296 | static int rb_cpu_notify(struct notifier_block *self, |
1297 | unsigned long action, void *hcpu); | |
554f786e SR |
1298 | #endif |
1299 | ||
7a8e76a3 | 1300 | /** |
d611851b | 1301 | * __ring_buffer_alloc - allocate a new ring_buffer |
68814b58 | 1302 | * @size: the size in bytes per cpu that is needed. |
7a8e76a3 SR |
1303 | * @flags: attributes to set for the ring buffer. |
1304 | * | |
1305 | * Currently the only flag that is available is the RB_FL_OVERWRITE | |
1306 | * flag. This flag means that the buffer will overwrite old data | |
1307 | * when the buffer wraps. If this flag is not set, the buffer will | |
1308 | * drop data when the tail hits the head. | |
1309 | */ | |
1f8a6a10 PZ |
1310 | struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
1311 | struct lock_class_key *key) | |
7a8e76a3 SR |
1312 | { |
1313 | struct ring_buffer *buffer; | |
1314 | int bsize; | |
438ced17 | 1315 | int cpu, nr_pages; |
7a8e76a3 SR |
1316 | |
1317 | /* keep it in its own cache line */ | |
1318 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), | |
1319 | GFP_KERNEL); | |
1320 | if (!buffer) | |
1321 | return NULL; | |
1322 | ||
9e01c1b7 RR |
1323 | if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
1324 | goto fail_free_buffer; | |
1325 | ||
438ced17 | 1326 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
7a8e76a3 | 1327 | buffer->flags = flags; |
37886f6a | 1328 | buffer->clock = trace_clock_local; |
1f8a6a10 | 1329 | buffer->reader_lock_key = key; |
7a8e76a3 | 1330 | |
15693458 | 1331 | init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters); |
f1dc6725 | 1332 | init_waitqueue_head(&buffer->irq_work.waiters); |
15693458 | 1333 | |
7a8e76a3 | 1334 | /* need at least two pages */ |
438ced17 VN |
1335 | if (nr_pages < 2) |
1336 | nr_pages = 2; | |
7a8e76a3 | 1337 | |
3bf832ce FW |
1338 | /* |
1339 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
1340 | * in early initcall, it will not be notified of secondary cpus. | |
1341 | * In that off case, we need to allocate for all possible cpus. | |
1342 | */ | |
1343 | #ifdef CONFIG_HOTPLUG_CPU | |
d39ad278 | 1344 | cpu_notifier_register_begin(); |
554f786e | 1345 | cpumask_copy(buffer->cpumask, cpu_online_mask); |
3bf832ce FW |
1346 | #else |
1347 | cpumask_copy(buffer->cpumask, cpu_possible_mask); | |
1348 | #endif | |
7a8e76a3 SR |
1349 | buffer->cpus = nr_cpu_ids; |
1350 | ||
1351 | bsize = sizeof(void *) * nr_cpu_ids; | |
1352 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), | |
1353 | GFP_KERNEL); | |
1354 | if (!buffer->buffers) | |
9e01c1b7 | 1355 | goto fail_free_cpumask; |
7a8e76a3 SR |
1356 | |
1357 | for_each_buffer_cpu(buffer, cpu) { | |
1358 | buffer->buffers[cpu] = | |
438ced17 | 1359 | rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
7a8e76a3 SR |
1360 | if (!buffer->buffers[cpu]) |
1361 | goto fail_free_buffers; | |
1362 | } | |
1363 | ||
59222efe | 1364 | #ifdef CONFIG_HOTPLUG_CPU |
554f786e SR |
1365 | buffer->cpu_notify.notifier_call = rb_cpu_notify; |
1366 | buffer->cpu_notify.priority = 0; | |
d39ad278 SB |
1367 | __register_cpu_notifier(&buffer->cpu_notify); |
1368 | cpu_notifier_register_done(); | |
554f786e SR |
1369 | #endif |
1370 | ||
7a8e76a3 SR |
1371 | mutex_init(&buffer->mutex); |
1372 | ||
1373 | return buffer; | |
1374 | ||
1375 | fail_free_buffers: | |
1376 | for_each_buffer_cpu(buffer, cpu) { | |
1377 | if (buffer->buffers[cpu]) | |
1378 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1379 | } | |
1380 | kfree(buffer->buffers); | |
1381 | ||
9e01c1b7 RR |
1382 | fail_free_cpumask: |
1383 | free_cpumask_var(buffer->cpumask); | |
d39ad278 SB |
1384 | #ifdef CONFIG_HOTPLUG_CPU |
1385 | cpu_notifier_register_done(); | |
1386 | #endif | |
9e01c1b7 | 1387 | |
7a8e76a3 SR |
1388 | fail_free_buffer: |
1389 | kfree(buffer); | |
1390 | return NULL; | |
1391 | } | |
1f8a6a10 | 1392 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
7a8e76a3 SR |
1393 | |
1394 | /** | |
1395 | * ring_buffer_free - free a ring buffer. | |
1396 | * @buffer: the buffer to free. | |
1397 | */ | |
1398 | void | |
1399 | ring_buffer_free(struct ring_buffer *buffer) | |
1400 | { | |
1401 | int cpu; | |
1402 | ||
59222efe | 1403 | #ifdef CONFIG_HOTPLUG_CPU |
d39ad278 SB |
1404 | cpu_notifier_register_begin(); |
1405 | __unregister_cpu_notifier(&buffer->cpu_notify); | |
554f786e SR |
1406 | #endif |
1407 | ||
7a8e76a3 SR |
1408 | for_each_buffer_cpu(buffer, cpu) |
1409 | rb_free_cpu_buffer(buffer->buffers[cpu]); | |
1410 | ||
d39ad278 SB |
1411 | #ifdef CONFIG_HOTPLUG_CPU |
1412 | cpu_notifier_register_done(); | |
1413 | #endif | |
554f786e | 1414 | |
bd3f0221 | 1415 | kfree(buffer->buffers); |
9e01c1b7 RR |
1416 | free_cpumask_var(buffer->cpumask); |
1417 | ||
7a8e76a3 SR |
1418 | kfree(buffer); |
1419 | } | |
c4f50183 | 1420 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
7a8e76a3 | 1421 | |
37886f6a SR |
1422 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
1423 | u64 (*clock)(void)) | |
1424 | { | |
1425 | buffer->clock = clock; | |
1426 | } | |
1427 | ||
7a8e76a3 SR |
1428 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
1429 | ||
83f40318 VN |
1430 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
1431 | { | |
1432 | return local_read(&bpage->entries) & RB_WRITE_MASK; | |
1433 | } | |
1434 | ||
1435 | static inline unsigned long rb_page_write(struct buffer_page *bpage) | |
1436 | { | |
1437 | return local_read(&bpage->write) & RB_WRITE_MASK; | |
1438 | } | |
1439 | ||
5040b4b7 | 1440 | static int |
83f40318 | 1441 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages) |
7a8e76a3 | 1442 | { |
83f40318 VN |
1443 | struct list_head *tail_page, *to_remove, *next_page; |
1444 | struct buffer_page *to_remove_page, *tmp_iter_page; | |
1445 | struct buffer_page *last_page, *first_page; | |
1446 | unsigned int nr_removed; | |
1447 | unsigned long head_bit; | |
1448 | int page_entries; | |
1449 | ||
1450 | head_bit = 0; | |
7a8e76a3 | 1451 | |
5389f6fa | 1452 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
83f40318 VN |
1453 | atomic_inc(&cpu_buffer->record_disabled); |
1454 | /* | |
1455 | * We don't race with the readers since we have acquired the reader | |
1456 | * lock. We also don't race with writers after disabling recording. | |
1457 | * This makes it easy to figure out the first and the last page to be | |
1458 | * removed from the list. We unlink all the pages in between including | |
1459 | * the first and last pages. This is done in a busy loop so that we | |
1460 | * lose the least number of traces. | |
1461 | * The pages are freed after we restart recording and unlock readers. | |
1462 | */ | |
1463 | tail_page = &cpu_buffer->tail_page->list; | |
77ae365e | 1464 | |
83f40318 VN |
1465 | /* |
1466 | * tail page might be on reader page, we remove the next page | |
1467 | * from the ring buffer | |
1468 | */ | |
1469 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) | |
1470 | tail_page = rb_list_head(tail_page->next); | |
1471 | to_remove = tail_page; | |
1472 | ||
1473 | /* start of pages to remove */ | |
1474 | first_page = list_entry(rb_list_head(to_remove->next), | |
1475 | struct buffer_page, list); | |
1476 | ||
1477 | for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) { | |
1478 | to_remove = rb_list_head(to_remove)->next; | |
1479 | head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD; | |
7a8e76a3 | 1480 | } |
7a8e76a3 | 1481 | |
83f40318 | 1482 | next_page = rb_list_head(to_remove)->next; |
7a8e76a3 | 1483 | |
83f40318 VN |
1484 | /* |
1485 | * Now we remove all pages between tail_page and next_page. | |
1486 | * Make sure that we have head_bit value preserved for the | |
1487 | * next page | |
1488 | */ | |
1489 | tail_page->next = (struct list_head *)((unsigned long)next_page | | |
1490 | head_bit); | |
1491 | next_page = rb_list_head(next_page); | |
1492 | next_page->prev = tail_page; | |
1493 | ||
1494 | /* make sure pages points to a valid page in the ring buffer */ | |
1495 | cpu_buffer->pages = next_page; | |
1496 | ||
1497 | /* update head page */ | |
1498 | if (head_bit) | |
1499 | cpu_buffer->head_page = list_entry(next_page, | |
1500 | struct buffer_page, list); | |
1501 | ||
1502 | /* | |
1503 | * change read pointer to make sure any read iterators reset | |
1504 | * themselves | |
1505 | */ | |
1506 | cpu_buffer->read = 0; | |
1507 | ||
1508 | /* pages are removed, resume tracing and then free the pages */ | |
1509 | atomic_dec(&cpu_buffer->record_disabled); | |
5389f6fa | 1510 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
83f40318 VN |
1511 | |
1512 | RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)); | |
1513 | ||
1514 | /* last buffer page to remove */ | |
1515 | last_page = list_entry(rb_list_head(to_remove), struct buffer_page, | |
1516 | list); | |
1517 | tmp_iter_page = first_page; | |
1518 | ||
1519 | do { | |
1520 | to_remove_page = tmp_iter_page; | |
1521 | rb_inc_page(cpu_buffer, &tmp_iter_page); | |
1522 | ||
1523 | /* update the counters */ | |
1524 | page_entries = rb_page_entries(to_remove_page); | |
1525 | if (page_entries) { | |
1526 | /* | |
1527 | * If something was added to this page, it was full | |
1528 | * since it is not the tail page. So we deduct the | |
1529 | * bytes consumed in ring buffer from here. | |
48fdc72f | 1530 | * Increment overrun to account for the lost events. |
83f40318 | 1531 | */ |
48fdc72f | 1532 | local_add(page_entries, &cpu_buffer->overrun); |
83f40318 VN |
1533 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
1534 | } | |
1535 | ||
1536 | /* | |
1537 | * We have already removed references to this list item, just | |
1538 | * free up the buffer_page and its page | |
1539 | */ | |
1540 | free_buffer_page(to_remove_page); | |
1541 | nr_removed--; | |
1542 | ||
1543 | } while (to_remove_page != last_page); | |
1544 | ||
1545 | RB_WARN_ON(cpu_buffer, nr_removed); | |
5040b4b7 VN |
1546 | |
1547 | return nr_removed == 0; | |
7a8e76a3 SR |
1548 | } |
1549 | ||
5040b4b7 VN |
1550 | static int |
1551 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) | |
7a8e76a3 | 1552 | { |
5040b4b7 VN |
1553 | struct list_head *pages = &cpu_buffer->new_pages; |
1554 | int retries, success; | |
7a8e76a3 | 1555 | |
5389f6fa | 1556 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
5040b4b7 VN |
1557 | /* |
1558 | * We are holding the reader lock, so the reader page won't be swapped | |
1559 | * in the ring buffer. Now we are racing with the writer trying to | |
1560 | * move head page and the tail page. | |
1561 | * We are going to adapt the reader page update process where: | |
1562 | * 1. We first splice the start and end of list of new pages between | |
1563 | * the head page and its previous page. | |
1564 | * 2. We cmpxchg the prev_page->next to point from head page to the | |
1565 | * start of new pages list. | |
1566 | * 3. Finally, we update the head->prev to the end of new list. | |
1567 | * | |
1568 | * We will try this process 10 times, to make sure that we don't keep | |
1569 | * spinning. | |
1570 | */ | |
1571 | retries = 10; | |
1572 | success = 0; | |
1573 | while (retries--) { | |
1574 | struct list_head *head_page, *prev_page, *r; | |
1575 | struct list_head *last_page, *first_page; | |
1576 | struct list_head *head_page_with_bit; | |
77ae365e | 1577 | |
5040b4b7 | 1578 | head_page = &rb_set_head_page(cpu_buffer)->list; |
54f7be5b SR |
1579 | if (!head_page) |
1580 | break; | |
5040b4b7 VN |
1581 | prev_page = head_page->prev; |
1582 | ||
1583 | first_page = pages->next; | |
1584 | last_page = pages->prev; | |
1585 | ||
1586 | head_page_with_bit = (struct list_head *) | |
1587 | ((unsigned long)head_page | RB_PAGE_HEAD); | |
1588 | ||
1589 | last_page->next = head_page_with_bit; | |
1590 | first_page->prev = prev_page; | |
1591 | ||
1592 | r = cmpxchg(&prev_page->next, head_page_with_bit, first_page); | |
1593 | ||
1594 | if (r == head_page_with_bit) { | |
1595 | /* | |
1596 | * yay, we replaced the page pointer to our new list, | |
1597 | * now, we just have to update to head page's prev | |
1598 | * pointer to point to end of list | |
1599 | */ | |
1600 | head_page->prev = last_page; | |
1601 | success = 1; | |
1602 | break; | |
1603 | } | |
7a8e76a3 | 1604 | } |
7a8e76a3 | 1605 | |
5040b4b7 VN |
1606 | if (success) |
1607 | INIT_LIST_HEAD(pages); | |
1608 | /* | |
1609 | * If we weren't successful in adding in new pages, warn and stop | |
1610 | * tracing | |
1611 | */ | |
1612 | RB_WARN_ON(cpu_buffer, !success); | |
5389f6fa | 1613 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
5040b4b7 VN |
1614 | |
1615 | /* free pages if they weren't inserted */ | |
1616 | if (!success) { | |
1617 | struct buffer_page *bpage, *tmp; | |
1618 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, | |
1619 | list) { | |
1620 | list_del_init(&bpage->list); | |
1621 | free_buffer_page(bpage); | |
1622 | } | |
1623 | } | |
1624 | return success; | |
7a8e76a3 SR |
1625 | } |
1626 | ||
83f40318 | 1627 | static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer) |
438ced17 | 1628 | { |
5040b4b7 VN |
1629 | int success; |
1630 | ||
438ced17 | 1631 | if (cpu_buffer->nr_pages_to_update > 0) |
5040b4b7 | 1632 | success = rb_insert_pages(cpu_buffer); |
438ced17 | 1633 | else |
5040b4b7 VN |
1634 | success = rb_remove_pages(cpu_buffer, |
1635 | -cpu_buffer->nr_pages_to_update); | |
83f40318 | 1636 | |
5040b4b7 VN |
1637 | if (success) |
1638 | cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update; | |
83f40318 VN |
1639 | } |
1640 | ||
1641 | static void update_pages_handler(struct work_struct *work) | |
1642 | { | |
1643 | struct ring_buffer_per_cpu *cpu_buffer = container_of(work, | |
1644 | struct ring_buffer_per_cpu, update_pages_work); | |
1645 | rb_update_pages(cpu_buffer); | |
05fdd70d | 1646 | complete(&cpu_buffer->update_done); |
438ced17 VN |
1647 | } |
1648 | ||
7a8e76a3 SR |
1649 | /** |
1650 | * ring_buffer_resize - resize the ring buffer | |
1651 | * @buffer: the buffer to resize. | |
1652 | * @size: the new size. | |
d611851b | 1653 | * @cpu_id: the cpu buffer to resize |
7a8e76a3 | 1654 | * |
7a8e76a3 SR |
1655 | * Minimum size is 2 * BUF_PAGE_SIZE. |
1656 | * | |
83f40318 | 1657 | * Returns 0 on success and < 0 on failure. |
7a8e76a3 | 1658 | */ |
438ced17 VN |
1659 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, |
1660 | int cpu_id) | |
7a8e76a3 SR |
1661 | { |
1662 | struct ring_buffer_per_cpu *cpu_buffer; | |
438ced17 | 1663 | unsigned nr_pages; |
83f40318 | 1664 | int cpu, err = 0; |
7a8e76a3 | 1665 | |
ee51a1de IM |
1666 | /* |
1667 | * Always succeed at resizing a non-existent buffer: | |
1668 | */ | |
1669 | if (!buffer) | |
1670 | return size; | |
1671 | ||
6a31e1f1 SR |
1672 | /* Make sure the requested buffer exists */ |
1673 | if (cpu_id != RING_BUFFER_ALL_CPUS && | |
1674 | !cpumask_test_cpu(cpu_id, buffer->cpumask)) | |
1675 | return size; | |
1676 | ||
7a8e76a3 SR |
1677 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
1678 | size *= BUF_PAGE_SIZE; | |
7a8e76a3 SR |
1679 | |
1680 | /* we need a minimum of two pages */ | |
1681 | if (size < BUF_PAGE_SIZE * 2) | |
1682 | size = BUF_PAGE_SIZE * 2; | |
1683 | ||
83f40318 | 1684 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
18421015 | 1685 | |
83f40318 VN |
1686 | /* |
1687 | * Don't succeed if resizing is disabled, as a reader might be | |
1688 | * manipulating the ring buffer and is expecting a sane state while | |
1689 | * this is true. | |
1690 | */ | |
1691 | if (atomic_read(&buffer->resize_disabled)) | |
1692 | return -EBUSY; | |
18421015 | 1693 | |
83f40318 | 1694 | /* prevent another thread from changing buffer sizes */ |
7a8e76a3 | 1695 | mutex_lock(&buffer->mutex); |
7a8e76a3 | 1696 | |
438ced17 VN |
1697 | if (cpu_id == RING_BUFFER_ALL_CPUS) { |
1698 | /* calculate the pages to update */ | |
7a8e76a3 SR |
1699 | for_each_buffer_cpu(buffer, cpu) { |
1700 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 | 1701 | |
438ced17 VN |
1702 | cpu_buffer->nr_pages_to_update = nr_pages - |
1703 | cpu_buffer->nr_pages; | |
438ced17 VN |
1704 | /* |
1705 | * nothing more to do for removing pages or no update | |
1706 | */ | |
1707 | if (cpu_buffer->nr_pages_to_update <= 0) | |
1708 | continue; | |
d7ec4bfe | 1709 | /* |
438ced17 VN |
1710 | * to add pages, make sure all new pages can be |
1711 | * allocated without receiving ENOMEM | |
d7ec4bfe | 1712 | */ |
438ced17 VN |
1713 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
1714 | if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update, | |
83f40318 | 1715 | &cpu_buffer->new_pages, cpu)) { |
438ced17 | 1716 | /* not enough memory for new pages */ |
83f40318 VN |
1717 | err = -ENOMEM; |
1718 | goto out_err; | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | get_online_cpus(); | |
1723 | /* | |
1724 | * Fire off all the required work handlers | |
05fdd70d | 1725 | * We can't schedule on offline CPUs, but it's not necessary |
83f40318 VN |
1726 | * since we can change their buffer sizes without any race. |
1727 | */ | |
1728 | for_each_buffer_cpu(buffer, cpu) { | |
1729 | cpu_buffer = buffer->buffers[cpu]; | |
05fdd70d | 1730 | if (!cpu_buffer->nr_pages_to_update) |
83f40318 VN |
1731 | continue; |
1732 | ||
021c5b34 CM |
1733 | /* Can't run something on an offline CPU. */ |
1734 | if (!cpu_online(cpu)) { | |
f5eb5588 SRRH |
1735 | rb_update_pages(cpu_buffer); |
1736 | cpu_buffer->nr_pages_to_update = 0; | |
1737 | } else { | |
05fdd70d VN |
1738 | schedule_work_on(cpu, |
1739 | &cpu_buffer->update_pages_work); | |
f5eb5588 | 1740 | } |
7a8e76a3 | 1741 | } |
7a8e76a3 | 1742 | |
438ced17 VN |
1743 | /* wait for all the updates to complete */ |
1744 | for_each_buffer_cpu(buffer, cpu) { | |
1745 | cpu_buffer = buffer->buffers[cpu]; | |
05fdd70d | 1746 | if (!cpu_buffer->nr_pages_to_update) |
83f40318 VN |
1747 | continue; |
1748 | ||
05fdd70d VN |
1749 | if (cpu_online(cpu)) |
1750 | wait_for_completion(&cpu_buffer->update_done); | |
83f40318 | 1751 | cpu_buffer->nr_pages_to_update = 0; |
438ced17 | 1752 | } |
83f40318 VN |
1753 | |
1754 | put_online_cpus(); | |
438ced17 | 1755 | } else { |
8e49f418 VN |
1756 | /* Make sure this CPU has been intitialized */ |
1757 | if (!cpumask_test_cpu(cpu_id, buffer->cpumask)) | |
1758 | goto out; | |
1759 | ||
438ced17 | 1760 | cpu_buffer = buffer->buffers[cpu_id]; |
83f40318 | 1761 | |
438ced17 VN |
1762 | if (nr_pages == cpu_buffer->nr_pages) |
1763 | goto out; | |
7a8e76a3 | 1764 | |
438ced17 VN |
1765 | cpu_buffer->nr_pages_to_update = nr_pages - |
1766 | cpu_buffer->nr_pages; | |
1767 | ||
1768 | INIT_LIST_HEAD(&cpu_buffer->new_pages); | |
1769 | if (cpu_buffer->nr_pages_to_update > 0 && | |
1770 | __rb_allocate_pages(cpu_buffer->nr_pages_to_update, | |
83f40318 VN |
1771 | &cpu_buffer->new_pages, cpu_id)) { |
1772 | err = -ENOMEM; | |
1773 | goto out_err; | |
1774 | } | |
438ced17 | 1775 | |
83f40318 VN |
1776 | get_online_cpus(); |
1777 | ||
021c5b34 CM |
1778 | /* Can't run something on an offline CPU. */ |
1779 | if (!cpu_online(cpu_id)) | |
f5eb5588 SRRH |
1780 | rb_update_pages(cpu_buffer); |
1781 | else { | |
83f40318 VN |
1782 | schedule_work_on(cpu_id, |
1783 | &cpu_buffer->update_pages_work); | |
05fdd70d | 1784 | wait_for_completion(&cpu_buffer->update_done); |
f5eb5588 | 1785 | } |
83f40318 | 1786 | |
83f40318 | 1787 | cpu_buffer->nr_pages_to_update = 0; |
05fdd70d | 1788 | put_online_cpus(); |
438ced17 | 1789 | } |
7a8e76a3 SR |
1790 | |
1791 | out: | |
659f451f SR |
1792 | /* |
1793 | * The ring buffer resize can happen with the ring buffer | |
1794 | * enabled, so that the update disturbs the tracing as little | |
1795 | * as possible. But if the buffer is disabled, we do not need | |
1796 | * to worry about that, and we can take the time to verify | |
1797 | * that the buffer is not corrupt. | |
1798 | */ | |
1799 | if (atomic_read(&buffer->record_disabled)) { | |
1800 | atomic_inc(&buffer->record_disabled); | |
1801 | /* | |
1802 | * Even though the buffer was disabled, we must make sure | |
1803 | * that it is truly disabled before calling rb_check_pages. | |
1804 | * There could have been a race between checking | |
1805 | * record_disable and incrementing it. | |
1806 | */ | |
1807 | synchronize_sched(); | |
1808 | for_each_buffer_cpu(buffer, cpu) { | |
1809 | cpu_buffer = buffer->buffers[cpu]; | |
1810 | rb_check_pages(cpu_buffer); | |
1811 | } | |
1812 | atomic_dec(&buffer->record_disabled); | |
1813 | } | |
1814 | ||
7a8e76a3 | 1815 | mutex_unlock(&buffer->mutex); |
7a8e76a3 SR |
1816 | return size; |
1817 | ||
83f40318 | 1818 | out_err: |
438ced17 VN |
1819 | for_each_buffer_cpu(buffer, cpu) { |
1820 | struct buffer_page *bpage, *tmp; | |
83f40318 | 1821 | |
438ced17 | 1822 | cpu_buffer = buffer->buffers[cpu]; |
438ced17 | 1823 | cpu_buffer->nr_pages_to_update = 0; |
83f40318 | 1824 | |
438ced17 VN |
1825 | if (list_empty(&cpu_buffer->new_pages)) |
1826 | continue; | |
83f40318 | 1827 | |
438ced17 VN |
1828 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, |
1829 | list) { | |
1830 | list_del_init(&bpage->list); | |
1831 | free_buffer_page(bpage); | |
1832 | } | |
7a8e76a3 | 1833 | } |
641d2f63 | 1834 | mutex_unlock(&buffer->mutex); |
83f40318 | 1835 | return err; |
7a8e76a3 | 1836 | } |
c4f50183 | 1837 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
7a8e76a3 | 1838 | |
750912fa DS |
1839 | void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val) |
1840 | { | |
1841 | mutex_lock(&buffer->mutex); | |
1842 | if (val) | |
1843 | buffer->flags |= RB_FL_OVERWRITE; | |
1844 | else | |
1845 | buffer->flags &= ~RB_FL_OVERWRITE; | |
1846 | mutex_unlock(&buffer->mutex); | |
1847 | } | |
1848 | EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite); | |
1849 | ||
8789a9e7 | 1850 | static inline void * |
044fa782 | 1851 | __rb_data_page_index(struct buffer_data_page *bpage, unsigned index) |
8789a9e7 | 1852 | { |
044fa782 | 1853 | return bpage->data + index; |
8789a9e7 SR |
1854 | } |
1855 | ||
044fa782 | 1856 | static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
7a8e76a3 | 1857 | { |
044fa782 | 1858 | return bpage->page->data + index; |
7a8e76a3 SR |
1859 | } |
1860 | ||
1861 | static inline struct ring_buffer_event * | |
d769041f | 1862 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1863 | { |
6f807acd SR |
1864 | return __rb_page_index(cpu_buffer->reader_page, |
1865 | cpu_buffer->reader_page->read); | |
1866 | } | |
1867 | ||
7a8e76a3 SR |
1868 | static inline struct ring_buffer_event * |
1869 | rb_iter_head_event(struct ring_buffer_iter *iter) | |
1870 | { | |
6f807acd | 1871 | return __rb_page_index(iter->head_page, iter->head); |
7a8e76a3 SR |
1872 | } |
1873 | ||
bf41a158 SR |
1874 | static inline unsigned rb_page_commit(struct buffer_page *bpage) |
1875 | { | |
abc9b56d | 1876 | return local_read(&bpage->page->commit); |
bf41a158 SR |
1877 | } |
1878 | ||
25985edc | 1879 | /* Size is determined by what has been committed */ |
bf41a158 SR |
1880 | static inline unsigned rb_page_size(struct buffer_page *bpage) |
1881 | { | |
1882 | return rb_page_commit(bpage); | |
1883 | } | |
1884 | ||
1885 | static inline unsigned | |
1886 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) | |
1887 | { | |
1888 | return rb_page_commit(cpu_buffer->commit_page); | |
1889 | } | |
1890 | ||
bf41a158 SR |
1891 | static inline unsigned |
1892 | rb_event_index(struct ring_buffer_event *event) | |
1893 | { | |
1894 | unsigned long addr = (unsigned long)event; | |
1895 | ||
22f470f8 | 1896 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
bf41a158 SR |
1897 | } |
1898 | ||
0f0c85fc | 1899 | static inline int |
fa743953 SR |
1900 | rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
1901 | struct ring_buffer_event *event) | |
bf41a158 SR |
1902 | { |
1903 | unsigned long addr = (unsigned long)event; | |
1904 | unsigned long index; | |
1905 | ||
1906 | index = rb_event_index(event); | |
1907 | addr &= PAGE_MASK; | |
1908 | ||
1909 | return cpu_buffer->commit_page->page == (void *)addr && | |
1910 | rb_commit_index(cpu_buffer) == index; | |
1911 | } | |
1912 | ||
34a148bf | 1913 | static void |
bf41a158 | 1914 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1915 | { |
77ae365e SR |
1916 | unsigned long max_count; |
1917 | ||
bf41a158 SR |
1918 | /* |
1919 | * We only race with interrupts and NMIs on this CPU. | |
1920 | * If we own the commit event, then we can commit | |
1921 | * all others that interrupted us, since the interruptions | |
1922 | * are in stack format (they finish before they come | |
1923 | * back to us). This allows us to do a simple loop to | |
1924 | * assign the commit to the tail. | |
1925 | */ | |
a8ccf1d6 | 1926 | again: |
438ced17 | 1927 | max_count = cpu_buffer->nr_pages * 100; |
77ae365e | 1928 | |
bf41a158 | 1929 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
77ae365e SR |
1930 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
1931 | return; | |
1932 | if (RB_WARN_ON(cpu_buffer, | |
1933 | rb_is_reader_page(cpu_buffer->tail_page))) | |
1934 | return; | |
1935 | local_set(&cpu_buffer->commit_page->page->commit, | |
1936 | rb_page_write(cpu_buffer->commit_page)); | |
bf41a158 | 1937 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
abc9b56d SR |
1938 | cpu_buffer->write_stamp = |
1939 | cpu_buffer->commit_page->page->time_stamp; | |
bf41a158 SR |
1940 | /* add barrier to keep gcc from optimizing too much */ |
1941 | barrier(); | |
1942 | } | |
1943 | while (rb_commit_index(cpu_buffer) != | |
1944 | rb_page_write(cpu_buffer->commit_page)) { | |
77ae365e SR |
1945 | |
1946 | local_set(&cpu_buffer->commit_page->page->commit, | |
1947 | rb_page_write(cpu_buffer->commit_page)); | |
1948 | RB_WARN_ON(cpu_buffer, | |
1949 | local_read(&cpu_buffer->commit_page->page->commit) & | |
1950 | ~RB_WRITE_MASK); | |
bf41a158 SR |
1951 | barrier(); |
1952 | } | |
a8ccf1d6 SR |
1953 | |
1954 | /* again, keep gcc from optimizing */ | |
1955 | barrier(); | |
1956 | ||
1957 | /* | |
1958 | * If an interrupt came in just after the first while loop | |
1959 | * and pushed the tail page forward, we will be left with | |
1960 | * a dangling commit that will never go forward. | |
1961 | */ | |
1962 | if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page)) | |
1963 | goto again; | |
7a8e76a3 SR |
1964 | } |
1965 | ||
d769041f | 1966 | static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
7a8e76a3 | 1967 | { |
abc9b56d | 1968 | cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; |
6f807acd | 1969 | cpu_buffer->reader_page->read = 0; |
d769041f SR |
1970 | } |
1971 | ||
34a148bf | 1972 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
d769041f SR |
1973 | { |
1974 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
1975 | ||
1976 | /* | |
1977 | * The iterator could be on the reader page (it starts there). | |
1978 | * But the head could have moved, since the reader was | |
1979 | * found. Check for this case and assign the iterator | |
1980 | * to the head page instead of next. | |
1981 | */ | |
1982 | if (iter->head_page == cpu_buffer->reader_page) | |
77ae365e | 1983 | iter->head_page = rb_set_head_page(cpu_buffer); |
d769041f SR |
1984 | else |
1985 | rb_inc_page(cpu_buffer, &iter->head_page); | |
1986 | ||
abc9b56d | 1987 | iter->read_stamp = iter->head_page->page->time_stamp; |
7a8e76a3 SR |
1988 | iter->head = 0; |
1989 | } | |
1990 | ||
69d1b839 SR |
1991 | /* Slow path, do not inline */ |
1992 | static noinline struct ring_buffer_event * | |
1993 | rb_add_time_stamp(struct ring_buffer_event *event, u64 delta) | |
1994 | { | |
1995 | event->type_len = RINGBUF_TYPE_TIME_EXTEND; | |
1996 | ||
1997 | /* Not the first event on the page? */ | |
1998 | if (rb_event_index(event)) { | |
1999 | event->time_delta = delta & TS_MASK; | |
2000 | event->array[0] = delta >> TS_SHIFT; | |
2001 | } else { | |
2002 | /* nope, just zero it */ | |
2003 | event->time_delta = 0; | |
2004 | event->array[0] = 0; | |
2005 | } | |
2006 | ||
2007 | return skip_time_extend(event); | |
2008 | } | |
2009 | ||
7a8e76a3 | 2010 | /** |
01e3e710 | 2011 | * rb_update_event - update event type and data |
021de3d9 | 2012 | * @event: the event to update |
7a8e76a3 SR |
2013 | * @type: the type of event |
2014 | * @length: the size of the event field in the ring buffer | |
2015 | * | |
2016 | * Update the type and data fields of the event. The length | |
2017 | * is the actual size that is written to the ring buffer, | |
2018 | * and with this, we can determine what to place into the | |
2019 | * data field. | |
2020 | */ | |
34a148bf | 2021 | static void |
69d1b839 SR |
2022 | rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, |
2023 | struct ring_buffer_event *event, unsigned length, | |
2024 | int add_timestamp, u64 delta) | |
7a8e76a3 | 2025 | { |
69d1b839 SR |
2026 | /* Only a commit updates the timestamp */ |
2027 | if (unlikely(!rb_event_is_commit(cpu_buffer, event))) | |
2028 | delta = 0; | |
7a8e76a3 | 2029 | |
69d1b839 SR |
2030 | /* |
2031 | * If we need to add a timestamp, then we | |
2032 | * add it to the start of the resevered space. | |
2033 | */ | |
2034 | if (unlikely(add_timestamp)) { | |
2035 | event = rb_add_time_stamp(event, delta); | |
2036 | length -= RB_LEN_TIME_EXTEND; | |
2037 | delta = 0; | |
7a8e76a3 | 2038 | } |
69d1b839 SR |
2039 | |
2040 | event->time_delta = delta; | |
2041 | length -= RB_EVNT_HDR_SIZE; | |
2042 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) { | |
2043 | event->type_len = 0; | |
2044 | event->array[0] = length; | |
2045 | } else | |
2046 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); | |
7a8e76a3 SR |
2047 | } |
2048 | ||
77ae365e SR |
2049 | /* |
2050 | * rb_handle_head_page - writer hit the head page | |
2051 | * | |
2052 | * Returns: +1 to retry page | |
2053 | * 0 to continue | |
2054 | * -1 on error | |
2055 | */ | |
2056 | static int | |
2057 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, | |
2058 | struct buffer_page *tail_page, | |
2059 | struct buffer_page *next_page) | |
2060 | { | |
2061 | struct buffer_page *new_head; | |
2062 | int entries; | |
2063 | int type; | |
2064 | int ret; | |
2065 | ||
2066 | entries = rb_page_entries(next_page); | |
2067 | ||
2068 | /* | |
2069 | * The hard part is here. We need to move the head | |
2070 | * forward, and protect against both readers on | |
2071 | * other CPUs and writers coming in via interrupts. | |
2072 | */ | |
2073 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, | |
2074 | RB_PAGE_HEAD); | |
2075 | ||
2076 | /* | |
2077 | * type can be one of four: | |
2078 | * NORMAL - an interrupt already moved it for us | |
2079 | * HEAD - we are the first to get here. | |
2080 | * UPDATE - we are the interrupt interrupting | |
2081 | * a current move. | |
2082 | * MOVED - a reader on another CPU moved the next | |
2083 | * pointer to its reader page. Give up | |
2084 | * and try again. | |
2085 | */ | |
2086 | ||
2087 | switch (type) { | |
2088 | case RB_PAGE_HEAD: | |
2089 | /* | |
2090 | * We changed the head to UPDATE, thus | |
2091 | * it is our responsibility to update | |
2092 | * the counters. | |
2093 | */ | |
2094 | local_add(entries, &cpu_buffer->overrun); | |
c64e148a | 2095 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
77ae365e SR |
2096 | |
2097 | /* | |
2098 | * The entries will be zeroed out when we move the | |
2099 | * tail page. | |
2100 | */ | |
2101 | ||
2102 | /* still more to do */ | |
2103 | break; | |
2104 | ||
2105 | case RB_PAGE_UPDATE: | |
2106 | /* | |
2107 | * This is an interrupt that interrupt the | |
2108 | * previous update. Still more to do. | |
2109 | */ | |
2110 | break; | |
2111 | case RB_PAGE_NORMAL: | |
2112 | /* | |
2113 | * An interrupt came in before the update | |
2114 | * and processed this for us. | |
2115 | * Nothing left to do. | |
2116 | */ | |
2117 | return 1; | |
2118 | case RB_PAGE_MOVED: | |
2119 | /* | |
2120 | * The reader is on another CPU and just did | |
2121 | * a swap with our next_page. | |
2122 | * Try again. | |
2123 | */ | |
2124 | return 1; | |
2125 | default: | |
2126 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ | |
2127 | return -1; | |
2128 | } | |
2129 | ||
2130 | /* | |
2131 | * Now that we are here, the old head pointer is | |
2132 | * set to UPDATE. This will keep the reader from | |
2133 | * swapping the head page with the reader page. | |
2134 | * The reader (on another CPU) will spin till | |
2135 | * we are finished. | |
2136 | * | |
2137 | * We just need to protect against interrupts | |
2138 | * doing the job. We will set the next pointer | |
2139 | * to HEAD. After that, we set the old pointer | |
2140 | * to NORMAL, but only if it was HEAD before. | |
2141 | * otherwise we are an interrupt, and only | |
2142 | * want the outer most commit to reset it. | |
2143 | */ | |
2144 | new_head = next_page; | |
2145 | rb_inc_page(cpu_buffer, &new_head); | |
2146 | ||
2147 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, | |
2148 | RB_PAGE_NORMAL); | |
2149 | ||
2150 | /* | |
2151 | * Valid returns are: | |
2152 | * HEAD - an interrupt came in and already set it. | |
2153 | * NORMAL - One of two things: | |
2154 | * 1) We really set it. | |
2155 | * 2) A bunch of interrupts came in and moved | |
2156 | * the page forward again. | |
2157 | */ | |
2158 | switch (ret) { | |
2159 | case RB_PAGE_HEAD: | |
2160 | case RB_PAGE_NORMAL: | |
2161 | /* OK */ | |
2162 | break; | |
2163 | default: | |
2164 | RB_WARN_ON(cpu_buffer, 1); | |
2165 | return -1; | |
2166 | } | |
2167 | ||
2168 | /* | |
2169 | * It is possible that an interrupt came in, | |
2170 | * set the head up, then more interrupts came in | |
2171 | * and moved it again. When we get back here, | |
2172 | * the page would have been set to NORMAL but we | |
2173 | * just set it back to HEAD. | |
2174 | * | |
2175 | * How do you detect this? Well, if that happened | |
2176 | * the tail page would have moved. | |
2177 | */ | |
2178 | if (ret == RB_PAGE_NORMAL) { | |
2179 | /* | |
2180 | * If the tail had moved passed next, then we need | |
2181 | * to reset the pointer. | |
2182 | */ | |
2183 | if (cpu_buffer->tail_page != tail_page && | |
2184 | cpu_buffer->tail_page != next_page) | |
2185 | rb_head_page_set_normal(cpu_buffer, new_head, | |
2186 | next_page, | |
2187 | RB_PAGE_HEAD); | |
2188 | } | |
2189 | ||
2190 | /* | |
2191 | * If this was the outer most commit (the one that | |
2192 | * changed the original pointer from HEAD to UPDATE), | |
2193 | * then it is up to us to reset it to NORMAL. | |
2194 | */ | |
2195 | if (type == RB_PAGE_HEAD) { | |
2196 | ret = rb_head_page_set_normal(cpu_buffer, next_page, | |
2197 | tail_page, | |
2198 | RB_PAGE_UPDATE); | |
2199 | if (RB_WARN_ON(cpu_buffer, | |
2200 | ret != RB_PAGE_UPDATE)) | |
2201 | return -1; | |
2202 | } | |
2203 | ||
2204 | return 0; | |
2205 | } | |
2206 | ||
34a148bf | 2207 | static unsigned rb_calculate_event_length(unsigned length) |
7a8e76a3 SR |
2208 | { |
2209 | struct ring_buffer_event event; /* Used only for sizeof array */ | |
2210 | ||
2211 | /* zero length can cause confusions */ | |
2212 | if (!length) | |
2213 | length = 1; | |
2214 | ||
2271048d | 2215 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
7a8e76a3 SR |
2216 | length += sizeof(event.array[0]); |
2217 | ||
2218 | length += RB_EVNT_HDR_SIZE; | |
2271048d | 2219 | length = ALIGN(length, RB_ARCH_ALIGNMENT); |
7a8e76a3 SR |
2220 | |
2221 | return length; | |
2222 | } | |
2223 | ||
c7b09308 SR |
2224 | static inline void |
2225 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, | |
2226 | struct buffer_page *tail_page, | |
2227 | unsigned long tail, unsigned long length) | |
2228 | { | |
2229 | struct ring_buffer_event *event; | |
2230 | ||
2231 | /* | |
2232 | * Only the event that crossed the page boundary | |
2233 | * must fill the old tail_page with padding. | |
2234 | */ | |
2235 | if (tail >= BUF_PAGE_SIZE) { | |
b3230c8b SR |
2236 | /* |
2237 | * If the page was filled, then we still need | |
2238 | * to update the real_end. Reset it to zero | |
2239 | * and the reader will ignore it. | |
2240 | */ | |
2241 | if (tail == BUF_PAGE_SIZE) | |
2242 | tail_page->real_end = 0; | |
2243 | ||
c7b09308 SR |
2244 | local_sub(length, &tail_page->write); |
2245 | return; | |
2246 | } | |
2247 | ||
2248 | event = __rb_page_index(tail_page, tail); | |
b0b7065b | 2249 | kmemcheck_annotate_bitfield(event, bitfield); |
c7b09308 | 2250 | |
c64e148a VN |
2251 | /* account for padding bytes */ |
2252 | local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes); | |
2253 | ||
ff0ff84a SR |
2254 | /* |
2255 | * Save the original length to the meta data. | |
2256 | * This will be used by the reader to add lost event | |
2257 | * counter. | |
2258 | */ | |
2259 | tail_page->real_end = tail; | |
2260 | ||
c7b09308 SR |
2261 | /* |
2262 | * If this event is bigger than the minimum size, then | |
2263 | * we need to be careful that we don't subtract the | |
2264 | * write counter enough to allow another writer to slip | |
2265 | * in on this page. | |
2266 | * We put in a discarded commit instead, to make sure | |
2267 | * that this space is not used again. | |
2268 | * | |
2269 | * If we are less than the minimum size, we don't need to | |
2270 | * worry about it. | |
2271 | */ | |
2272 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { | |
2273 | /* No room for any events */ | |
2274 | ||
2275 | /* Mark the rest of the page with padding */ | |
2276 | rb_event_set_padding(event); | |
2277 | ||
2278 | /* Set the write back to the previous setting */ | |
2279 | local_sub(length, &tail_page->write); | |
2280 | return; | |
2281 | } | |
2282 | ||
2283 | /* Put in a discarded event */ | |
2284 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; | |
2285 | event->type_len = RINGBUF_TYPE_PADDING; | |
2286 | /* time delta must be non zero */ | |
2287 | event->time_delta = 1; | |
c7b09308 SR |
2288 | |
2289 | /* Set write to end of buffer */ | |
2290 | length = (tail + length) - BUF_PAGE_SIZE; | |
2291 | local_sub(length, &tail_page->write); | |
2292 | } | |
6634ff26 | 2293 | |
747e94ae SR |
2294 | /* |
2295 | * This is the slow path, force gcc not to inline it. | |
2296 | */ | |
2297 | static noinline struct ring_buffer_event * | |
6634ff26 SR |
2298 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
2299 | unsigned long length, unsigned long tail, | |
e8bc43e8 | 2300 | struct buffer_page *tail_page, u64 ts) |
7a8e76a3 | 2301 | { |
5a50e33c | 2302 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
7a8e76a3 | 2303 | struct ring_buffer *buffer = cpu_buffer->buffer; |
77ae365e SR |
2304 | struct buffer_page *next_page; |
2305 | int ret; | |
aa20ae84 SR |
2306 | |
2307 | next_page = tail_page; | |
2308 | ||
aa20ae84 SR |
2309 | rb_inc_page(cpu_buffer, &next_page); |
2310 | ||
aa20ae84 SR |
2311 | /* |
2312 | * If for some reason, we had an interrupt storm that made | |
2313 | * it all the way around the buffer, bail, and warn | |
2314 | * about it. | |
2315 | */ | |
2316 | if (unlikely(next_page == commit_page)) { | |
77ae365e | 2317 | local_inc(&cpu_buffer->commit_overrun); |
aa20ae84 SR |
2318 | goto out_reset; |
2319 | } | |
2320 | ||
77ae365e SR |
2321 | /* |
2322 | * This is where the fun begins! | |
2323 | * | |
2324 | * We are fighting against races between a reader that | |
2325 | * could be on another CPU trying to swap its reader | |
2326 | * page with the buffer head. | |
2327 | * | |
2328 | * We are also fighting against interrupts coming in and | |
2329 | * moving the head or tail on us as well. | |
2330 | * | |
2331 | * If the next page is the head page then we have filled | |
2332 | * the buffer, unless the commit page is still on the | |
2333 | * reader page. | |
2334 | */ | |
2335 | if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) { | |
aa20ae84 | 2336 | |
77ae365e SR |
2337 | /* |
2338 | * If the commit is not on the reader page, then | |
2339 | * move the header page. | |
2340 | */ | |
2341 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { | |
2342 | /* | |
2343 | * If we are not in overwrite mode, | |
2344 | * this is easy, just stop here. | |
2345 | */ | |
884bfe89 SP |
2346 | if (!(buffer->flags & RB_FL_OVERWRITE)) { |
2347 | local_inc(&cpu_buffer->dropped_events); | |
77ae365e | 2348 | goto out_reset; |
884bfe89 | 2349 | } |
77ae365e SR |
2350 | |
2351 | ret = rb_handle_head_page(cpu_buffer, | |
2352 | tail_page, | |
2353 | next_page); | |
2354 | if (ret < 0) | |
2355 | goto out_reset; | |
2356 | if (ret) | |
2357 | goto out_again; | |
2358 | } else { | |
2359 | /* | |
2360 | * We need to be careful here too. The | |
2361 | * commit page could still be on the reader | |
2362 | * page. We could have a small buffer, and | |
2363 | * have filled up the buffer with events | |
2364 | * from interrupts and such, and wrapped. | |
2365 | * | |
2366 | * Note, if the tail page is also the on the | |
2367 | * reader_page, we let it move out. | |
2368 | */ | |
2369 | if (unlikely((cpu_buffer->commit_page != | |
2370 | cpu_buffer->tail_page) && | |
2371 | (cpu_buffer->commit_page == | |
2372 | cpu_buffer->reader_page))) { | |
2373 | local_inc(&cpu_buffer->commit_overrun); | |
2374 | goto out_reset; | |
2375 | } | |
aa20ae84 SR |
2376 | } |
2377 | } | |
2378 | ||
77ae365e SR |
2379 | ret = rb_tail_page_update(cpu_buffer, tail_page, next_page); |
2380 | if (ret) { | |
2381 | /* | |
2382 | * Nested commits always have zero deltas, so | |
2383 | * just reread the time stamp | |
2384 | */ | |
e8bc43e8 SR |
2385 | ts = rb_time_stamp(buffer); |
2386 | next_page->page->time_stamp = ts; | |
aa20ae84 SR |
2387 | } |
2388 | ||
77ae365e | 2389 | out_again: |
aa20ae84 | 2390 | |
77ae365e | 2391 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
aa20ae84 SR |
2392 | |
2393 | /* fail and let the caller try again */ | |
2394 | return ERR_PTR(-EAGAIN); | |
2395 | ||
45141d46 | 2396 | out_reset: |
6f3b3440 | 2397 | /* reset write */ |
c7b09308 | 2398 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
6f3b3440 | 2399 | |
bf41a158 | 2400 | return NULL; |
7a8e76a3 SR |
2401 | } |
2402 | ||
6634ff26 SR |
2403 | static struct ring_buffer_event * |
2404 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, | |
69d1b839 SR |
2405 | unsigned long length, u64 ts, |
2406 | u64 delta, int add_timestamp) | |
6634ff26 | 2407 | { |
5a50e33c | 2408 | struct buffer_page *tail_page; |
6634ff26 SR |
2409 | struct ring_buffer_event *event; |
2410 | unsigned long tail, write; | |
2411 | ||
69d1b839 SR |
2412 | /* |
2413 | * If the time delta since the last event is too big to | |
2414 | * hold in the time field of the event, then we append a | |
2415 | * TIME EXTEND event ahead of the data event. | |
2416 | */ | |
2417 | if (unlikely(add_timestamp)) | |
2418 | length += RB_LEN_TIME_EXTEND; | |
2419 | ||
6634ff26 SR |
2420 | tail_page = cpu_buffer->tail_page; |
2421 | write = local_add_return(length, &tail_page->write); | |
77ae365e SR |
2422 | |
2423 | /* set write to only the index of the write */ | |
2424 | write &= RB_WRITE_MASK; | |
6634ff26 SR |
2425 | tail = write - length; |
2426 | ||
d651aa1d SRRH |
2427 | /* |
2428 | * If this is the first commit on the page, then it has the same | |
2429 | * timestamp as the page itself. | |
2430 | */ | |
2431 | if (!tail) | |
2432 | delta = 0; | |
2433 | ||
6634ff26 | 2434 | /* See if we shot pass the end of this buffer page */ |
747e94ae | 2435 | if (unlikely(write > BUF_PAGE_SIZE)) |
6634ff26 | 2436 | return rb_move_tail(cpu_buffer, length, tail, |
5a50e33c | 2437 | tail_page, ts); |
6634ff26 SR |
2438 | |
2439 | /* We reserved something on the buffer */ | |
2440 | ||
6634ff26 | 2441 | event = __rb_page_index(tail_page, tail); |
1744a21d | 2442 | kmemcheck_annotate_bitfield(event, bitfield); |
69d1b839 | 2443 | rb_update_event(cpu_buffer, event, length, add_timestamp, delta); |
6634ff26 | 2444 | |
69d1b839 | 2445 | local_inc(&tail_page->entries); |
6634ff26 SR |
2446 | |
2447 | /* | |
fa743953 SR |
2448 | * If this is the first commit on the page, then update |
2449 | * its timestamp. | |
6634ff26 | 2450 | */ |
fa743953 | 2451 | if (!tail) |
e8bc43e8 | 2452 | tail_page->page->time_stamp = ts; |
6634ff26 | 2453 | |
c64e148a VN |
2454 | /* account for these added bytes */ |
2455 | local_add(length, &cpu_buffer->entries_bytes); | |
2456 | ||
6634ff26 SR |
2457 | return event; |
2458 | } | |
2459 | ||
edd813bf SR |
2460 | static inline int |
2461 | rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, | |
2462 | struct ring_buffer_event *event) | |
2463 | { | |
2464 | unsigned long new_index, old_index; | |
2465 | struct buffer_page *bpage; | |
2466 | unsigned long index; | |
2467 | unsigned long addr; | |
2468 | ||
2469 | new_index = rb_event_index(event); | |
69d1b839 | 2470 | old_index = new_index + rb_event_ts_length(event); |
edd813bf SR |
2471 | addr = (unsigned long)event; |
2472 | addr &= PAGE_MASK; | |
2473 | ||
2474 | bpage = cpu_buffer->tail_page; | |
2475 | ||
2476 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { | |
77ae365e SR |
2477 | unsigned long write_mask = |
2478 | local_read(&bpage->write) & ~RB_WRITE_MASK; | |
c64e148a | 2479 | unsigned long event_length = rb_event_length(event); |
edd813bf SR |
2480 | /* |
2481 | * This is on the tail page. It is possible that | |
2482 | * a write could come in and move the tail page | |
2483 | * and write to the next page. That is fine | |
2484 | * because we just shorten what is on this page. | |
2485 | */ | |
77ae365e SR |
2486 | old_index += write_mask; |
2487 | new_index += write_mask; | |
edd813bf | 2488 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
c64e148a VN |
2489 | if (index == old_index) { |
2490 | /* update counters */ | |
2491 | local_sub(event_length, &cpu_buffer->entries_bytes); | |
edd813bf | 2492 | return 1; |
c64e148a | 2493 | } |
edd813bf SR |
2494 | } |
2495 | ||
2496 | /* could not discard */ | |
2497 | return 0; | |
2498 | } | |
2499 | ||
fa743953 SR |
2500 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
2501 | { | |
2502 | local_inc(&cpu_buffer->committing); | |
2503 | local_inc(&cpu_buffer->commits); | |
2504 | } | |
2505 | ||
d9abde21 | 2506 | static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) |
fa743953 SR |
2507 | { |
2508 | unsigned long commits; | |
2509 | ||
2510 | if (RB_WARN_ON(cpu_buffer, | |
2511 | !local_read(&cpu_buffer->committing))) | |
2512 | return; | |
2513 | ||
2514 | again: | |
2515 | commits = local_read(&cpu_buffer->commits); | |
2516 | /* synchronize with interrupts */ | |
2517 | barrier(); | |
2518 | if (local_read(&cpu_buffer->committing) == 1) | |
2519 | rb_set_commit_to_write(cpu_buffer); | |
2520 | ||
2521 | local_dec(&cpu_buffer->committing); | |
2522 | ||
2523 | /* synchronize with interrupts */ | |
2524 | barrier(); | |
2525 | ||
2526 | /* | |
2527 | * Need to account for interrupts coming in between the | |
2528 | * updating of the commit page and the clearing of the | |
2529 | * committing counter. | |
2530 | */ | |
2531 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && | |
2532 | !local_read(&cpu_buffer->committing)) { | |
2533 | local_inc(&cpu_buffer->committing); | |
2534 | goto again; | |
2535 | } | |
2536 | } | |
2537 | ||
7a8e76a3 | 2538 | static struct ring_buffer_event * |
62f0b3eb SR |
2539 | rb_reserve_next_event(struct ring_buffer *buffer, |
2540 | struct ring_buffer_per_cpu *cpu_buffer, | |
1cd8d735 | 2541 | unsigned long length) |
7a8e76a3 SR |
2542 | { |
2543 | struct ring_buffer_event *event; | |
69d1b839 | 2544 | u64 ts, delta; |
818e3dd3 | 2545 | int nr_loops = 0; |
69d1b839 | 2546 | int add_timestamp; |
140ff891 | 2547 | u64 diff; |
7a8e76a3 | 2548 | |
fa743953 SR |
2549 | rb_start_commit(cpu_buffer); |
2550 | ||
85bac32c | 2551 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
62f0b3eb SR |
2552 | /* |
2553 | * Due to the ability to swap a cpu buffer from a buffer | |
2554 | * it is possible it was swapped before we committed. | |
2555 | * (committing stops a swap). We check for it here and | |
2556 | * if it happened, we have to fail the write. | |
2557 | */ | |
2558 | barrier(); | |
2559 | if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) { | |
2560 | local_dec(&cpu_buffer->committing); | |
2561 | local_dec(&cpu_buffer->commits); | |
2562 | return NULL; | |
2563 | } | |
85bac32c | 2564 | #endif |
62f0b3eb | 2565 | |
be957c44 | 2566 | length = rb_calculate_event_length(length); |
bf41a158 | 2567 | again: |
69d1b839 SR |
2568 | add_timestamp = 0; |
2569 | delta = 0; | |
2570 | ||
818e3dd3 SR |
2571 | /* |
2572 | * We allow for interrupts to reenter here and do a trace. | |
2573 | * If one does, it will cause this original code to loop | |
2574 | * back here. Even with heavy interrupts happening, this | |
2575 | * should only happen a few times in a row. If this happens | |
2576 | * 1000 times in a row, there must be either an interrupt | |
2577 | * storm or we have something buggy. | |
2578 | * Bail! | |
2579 | */ | |
3e89c7bb | 2580 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
fa743953 | 2581 | goto out_fail; |
818e3dd3 | 2582 | |
6d3f1e12 | 2583 | ts = rb_time_stamp(cpu_buffer->buffer); |
140ff891 | 2584 | diff = ts - cpu_buffer->write_stamp; |
7a8e76a3 | 2585 | |
140ff891 SR |
2586 | /* make sure this diff is calculated here */ |
2587 | barrier(); | |
bf41a158 | 2588 | |
140ff891 SR |
2589 | /* Did the write stamp get updated already? */ |
2590 | if (likely(ts >= cpu_buffer->write_stamp)) { | |
168b6b1d SR |
2591 | delta = diff; |
2592 | if (unlikely(test_time_stamp(delta))) { | |
31274d72 JO |
2593 | int local_clock_stable = 1; |
2594 | #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK | |
35af99e6 | 2595 | local_clock_stable = sched_clock_stable(); |
31274d72 | 2596 | #endif |
69d1b839 | 2597 | WARN_ONCE(delta > (1ULL << 59), |
31274d72 | 2598 | KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s", |
69d1b839 SR |
2599 | (unsigned long long)delta, |
2600 | (unsigned long long)ts, | |
31274d72 JO |
2601 | (unsigned long long)cpu_buffer->write_stamp, |
2602 | local_clock_stable ? "" : | |
2603 | "If you just came from a suspend/resume,\n" | |
2604 | "please switch to the trace global clock:\n" | |
2605 | " echo global > /sys/kernel/debug/tracing/trace_clock\n"); | |
69d1b839 | 2606 | add_timestamp = 1; |
7a8e76a3 | 2607 | } |
168b6b1d | 2608 | } |
7a8e76a3 | 2609 | |
69d1b839 SR |
2610 | event = __rb_reserve_next(cpu_buffer, length, ts, |
2611 | delta, add_timestamp); | |
168b6b1d | 2612 | if (unlikely(PTR_ERR(event) == -EAGAIN)) |
bf41a158 SR |
2613 | goto again; |
2614 | ||
fa743953 SR |
2615 | if (!event) |
2616 | goto out_fail; | |
7a8e76a3 | 2617 | |
7a8e76a3 | 2618 | return event; |
fa743953 SR |
2619 | |
2620 | out_fail: | |
2621 | rb_end_commit(cpu_buffer); | |
2622 | return NULL; | |
7a8e76a3 SR |
2623 | } |
2624 | ||
1155de47 PM |
2625 | #ifdef CONFIG_TRACING |
2626 | ||
567cd4da SR |
2627 | /* |
2628 | * The lock and unlock are done within a preempt disable section. | |
2629 | * The current_context per_cpu variable can only be modified | |
2630 | * by the current task between lock and unlock. But it can | |
2631 | * be modified more than once via an interrupt. To pass this | |
2632 | * information from the lock to the unlock without having to | |
2633 | * access the 'in_interrupt()' functions again (which do show | |
2634 | * a bit of overhead in something as critical as function tracing, | |
2635 | * we use a bitmask trick. | |
2636 | * | |
2637 | * bit 0 = NMI context | |
2638 | * bit 1 = IRQ context | |
2639 | * bit 2 = SoftIRQ context | |
2640 | * bit 3 = normal context. | |
2641 | * | |
2642 | * This works because this is the order of contexts that can | |
2643 | * preempt other contexts. A SoftIRQ never preempts an IRQ | |
2644 | * context. | |
2645 | * | |
2646 | * When the context is determined, the corresponding bit is | |
2647 | * checked and set (if it was set, then a recursion of that context | |
2648 | * happened). | |
2649 | * | |
2650 | * On unlock, we need to clear this bit. To do so, just subtract | |
2651 | * 1 from the current_context and AND it to itself. | |
2652 | * | |
2653 | * (binary) | |
2654 | * 101 - 1 = 100 | |
2655 | * 101 & 100 = 100 (clearing bit zero) | |
2656 | * | |
2657 | * 1010 - 1 = 1001 | |
2658 | * 1010 & 1001 = 1000 (clearing bit 1) | |
2659 | * | |
2660 | * The least significant bit can be cleared this way, and it | |
2661 | * just so happens that it is the same bit corresponding to | |
2662 | * the current context. | |
2663 | */ | |
2664 | static DEFINE_PER_CPU(unsigned int, current_context); | |
261842b7 | 2665 | |
567cd4da | 2666 | static __always_inline int trace_recursive_lock(void) |
261842b7 | 2667 | { |
567cd4da SR |
2668 | unsigned int val = this_cpu_read(current_context); |
2669 | int bit; | |
d9abde21 | 2670 | |
567cd4da SR |
2671 | if (in_interrupt()) { |
2672 | if (in_nmi()) | |
2673 | bit = 0; | |
2674 | else if (in_irq()) | |
2675 | bit = 1; | |
2676 | else | |
2677 | bit = 2; | |
2678 | } else | |
2679 | bit = 3; | |
d9abde21 | 2680 | |
567cd4da SR |
2681 | if (unlikely(val & (1 << bit))) |
2682 | return 1; | |
d9abde21 | 2683 | |
567cd4da SR |
2684 | val |= (1 << bit); |
2685 | this_cpu_write(current_context, val); | |
d9abde21 | 2686 | |
567cd4da | 2687 | return 0; |
261842b7 SR |
2688 | } |
2689 | ||
567cd4da | 2690 | static __always_inline void trace_recursive_unlock(void) |
261842b7 | 2691 | { |
567cd4da | 2692 | unsigned int val = this_cpu_read(current_context); |
261842b7 | 2693 | |
567cd4da SR |
2694 | val--; |
2695 | val &= this_cpu_read(current_context); | |
2696 | this_cpu_write(current_context, val); | |
261842b7 SR |
2697 | } |
2698 | ||
1155de47 PM |
2699 | #else |
2700 | ||
2701 | #define trace_recursive_lock() (0) | |
2702 | #define trace_recursive_unlock() do { } while (0) | |
2703 | ||
2704 | #endif | |
2705 | ||
7a8e76a3 SR |
2706 | /** |
2707 | * ring_buffer_lock_reserve - reserve a part of the buffer | |
2708 | * @buffer: the ring buffer to reserve from | |
2709 | * @length: the length of the data to reserve (excluding event header) | |
7a8e76a3 SR |
2710 | * |
2711 | * Returns a reseverd event on the ring buffer to copy directly to. | |
2712 | * The user of this interface will need to get the body to write into | |
2713 | * and can use the ring_buffer_event_data() interface. | |
2714 | * | |
2715 | * The length is the length of the data needed, not the event length | |
2716 | * which also includes the event header. | |
2717 | * | |
2718 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. | |
2719 | * If NULL is returned, then nothing has been allocated or locked. | |
2720 | */ | |
2721 | struct ring_buffer_event * | |
0a987751 | 2722 | ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) |
7a8e76a3 SR |
2723 | { |
2724 | struct ring_buffer_per_cpu *cpu_buffer; | |
2725 | struct ring_buffer_event *event; | |
5168ae50 | 2726 | int cpu; |
7a8e76a3 | 2727 | |
033601a3 | 2728 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2729 | return NULL; |
2730 | ||
bf41a158 | 2731 | /* If we are tracing schedule, we don't want to recurse */ |
5168ae50 | 2732 | preempt_disable_notrace(); |
bf41a158 | 2733 | |
52fbe9cd LJ |
2734 | if (atomic_read(&buffer->record_disabled)) |
2735 | goto out_nocheck; | |
2736 | ||
261842b7 SR |
2737 | if (trace_recursive_lock()) |
2738 | goto out_nocheck; | |
2739 | ||
7a8e76a3 SR |
2740 | cpu = raw_smp_processor_id(); |
2741 | ||
9e01c1b7 | 2742 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2743 | goto out; |
7a8e76a3 SR |
2744 | |
2745 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2746 | |
2747 | if (atomic_read(&cpu_buffer->record_disabled)) | |
d769041f | 2748 | goto out; |
7a8e76a3 | 2749 | |
be957c44 | 2750 | if (length > BUF_MAX_DATA_SIZE) |
bf41a158 | 2751 | goto out; |
7a8e76a3 | 2752 | |
62f0b3eb | 2753 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 | 2754 | if (!event) |
d769041f | 2755 | goto out; |
7a8e76a3 SR |
2756 | |
2757 | return event; | |
2758 | ||
d769041f | 2759 | out: |
261842b7 SR |
2760 | trace_recursive_unlock(); |
2761 | ||
2762 | out_nocheck: | |
5168ae50 | 2763 | preempt_enable_notrace(); |
7a8e76a3 SR |
2764 | return NULL; |
2765 | } | |
c4f50183 | 2766 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
7a8e76a3 | 2767 | |
a1863c21 SR |
2768 | static void |
2769 | rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
7a8e76a3 SR |
2770 | struct ring_buffer_event *event) |
2771 | { | |
69d1b839 SR |
2772 | u64 delta; |
2773 | ||
fa743953 SR |
2774 | /* |
2775 | * The event first in the commit queue updates the | |
2776 | * time stamp. | |
2777 | */ | |
69d1b839 SR |
2778 | if (rb_event_is_commit(cpu_buffer, event)) { |
2779 | /* | |
2780 | * A commit event that is first on a page | |
2781 | * updates the write timestamp with the page stamp | |
2782 | */ | |
2783 | if (!rb_event_index(event)) | |
2784 | cpu_buffer->write_stamp = | |
2785 | cpu_buffer->commit_page->page->time_stamp; | |
2786 | else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) { | |
2787 | delta = event->array[0]; | |
2788 | delta <<= TS_SHIFT; | |
2789 | delta += event->time_delta; | |
2790 | cpu_buffer->write_stamp += delta; | |
2791 | } else | |
2792 | cpu_buffer->write_stamp += event->time_delta; | |
2793 | } | |
a1863c21 | 2794 | } |
bf41a158 | 2795 | |
a1863c21 SR |
2796 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
2797 | struct ring_buffer_event *event) | |
2798 | { | |
2799 | local_inc(&cpu_buffer->entries); | |
2800 | rb_update_write_stamp(cpu_buffer, event); | |
fa743953 | 2801 | rb_end_commit(cpu_buffer); |
7a8e76a3 SR |
2802 | } |
2803 | ||
15693458 SRRH |
2804 | static __always_inline void |
2805 | rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer) | |
2806 | { | |
2807 | if (buffer->irq_work.waiters_pending) { | |
2808 | buffer->irq_work.waiters_pending = false; | |
2809 | /* irq_work_queue() supplies it's own memory barriers */ | |
2810 | irq_work_queue(&buffer->irq_work.work); | |
2811 | } | |
2812 | ||
2813 | if (cpu_buffer->irq_work.waiters_pending) { | |
2814 | cpu_buffer->irq_work.waiters_pending = false; | |
2815 | /* irq_work_queue() supplies it's own memory barriers */ | |
2816 | irq_work_queue(&cpu_buffer->irq_work.work); | |
2817 | } | |
2818 | } | |
2819 | ||
7a8e76a3 SR |
2820 | /** |
2821 | * ring_buffer_unlock_commit - commit a reserved | |
2822 | * @buffer: The buffer to commit to | |
2823 | * @event: The event pointer to commit. | |
7a8e76a3 SR |
2824 | * |
2825 | * This commits the data to the ring buffer, and releases any locks held. | |
2826 | * | |
2827 | * Must be paired with ring_buffer_lock_reserve. | |
2828 | */ | |
2829 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, | |
0a987751 | 2830 | struct ring_buffer_event *event) |
7a8e76a3 SR |
2831 | { |
2832 | struct ring_buffer_per_cpu *cpu_buffer; | |
2833 | int cpu = raw_smp_processor_id(); | |
2834 | ||
2835 | cpu_buffer = buffer->buffers[cpu]; | |
2836 | ||
7a8e76a3 SR |
2837 | rb_commit(cpu_buffer, event); |
2838 | ||
15693458 SRRH |
2839 | rb_wakeups(buffer, cpu_buffer); |
2840 | ||
261842b7 SR |
2841 | trace_recursive_unlock(); |
2842 | ||
5168ae50 | 2843 | preempt_enable_notrace(); |
7a8e76a3 SR |
2844 | |
2845 | return 0; | |
2846 | } | |
c4f50183 | 2847 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
7a8e76a3 | 2848 | |
f3b9aae1 FW |
2849 | static inline void rb_event_discard(struct ring_buffer_event *event) |
2850 | { | |
69d1b839 SR |
2851 | if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) |
2852 | event = skip_time_extend(event); | |
2853 | ||
334d4169 LJ |
2854 | /* array[0] holds the actual length for the discarded event */ |
2855 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; | |
2856 | event->type_len = RINGBUF_TYPE_PADDING; | |
f3b9aae1 FW |
2857 | /* time delta must be non zero */ |
2858 | if (!event->time_delta) | |
2859 | event->time_delta = 1; | |
2860 | } | |
2861 | ||
a1863c21 SR |
2862 | /* |
2863 | * Decrement the entries to the page that an event is on. | |
2864 | * The event does not even need to exist, only the pointer | |
2865 | * to the page it is on. This may only be called before the commit | |
2866 | * takes place. | |
2867 | */ | |
2868 | static inline void | |
2869 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, | |
2870 | struct ring_buffer_event *event) | |
2871 | { | |
2872 | unsigned long addr = (unsigned long)event; | |
2873 | struct buffer_page *bpage = cpu_buffer->commit_page; | |
2874 | struct buffer_page *start; | |
2875 | ||
2876 | addr &= PAGE_MASK; | |
2877 | ||
2878 | /* Do the likely case first */ | |
2879 | if (likely(bpage->page == (void *)addr)) { | |
2880 | local_dec(&bpage->entries); | |
2881 | return; | |
2882 | } | |
2883 | ||
2884 | /* | |
2885 | * Because the commit page may be on the reader page we | |
2886 | * start with the next page and check the end loop there. | |
2887 | */ | |
2888 | rb_inc_page(cpu_buffer, &bpage); | |
2889 | start = bpage; | |
2890 | do { | |
2891 | if (bpage->page == (void *)addr) { | |
2892 | local_dec(&bpage->entries); | |
2893 | return; | |
2894 | } | |
2895 | rb_inc_page(cpu_buffer, &bpage); | |
2896 | } while (bpage != start); | |
2897 | ||
2898 | /* commit not part of this buffer?? */ | |
2899 | RB_WARN_ON(cpu_buffer, 1); | |
2900 | } | |
2901 | ||
fa1b47dd SR |
2902 | /** |
2903 | * ring_buffer_commit_discard - discard an event that has not been committed | |
2904 | * @buffer: the ring buffer | |
2905 | * @event: non committed event to discard | |
2906 | * | |
dc892f73 SR |
2907 | * Sometimes an event that is in the ring buffer needs to be ignored. |
2908 | * This function lets the user discard an event in the ring buffer | |
2909 | * and then that event will not be read later. | |
2910 | * | |
2911 | * This function only works if it is called before the the item has been | |
2912 | * committed. It will try to free the event from the ring buffer | |
fa1b47dd SR |
2913 | * if another event has not been added behind it. |
2914 | * | |
2915 | * If another event has been added behind it, it will set the event | |
2916 | * up as discarded, and perform the commit. | |
2917 | * | |
2918 | * If this function is called, do not call ring_buffer_unlock_commit on | |
2919 | * the event. | |
2920 | */ | |
2921 | void ring_buffer_discard_commit(struct ring_buffer *buffer, | |
2922 | struct ring_buffer_event *event) | |
2923 | { | |
2924 | struct ring_buffer_per_cpu *cpu_buffer; | |
fa1b47dd SR |
2925 | int cpu; |
2926 | ||
2927 | /* The event is discarded regardless */ | |
f3b9aae1 | 2928 | rb_event_discard(event); |
fa1b47dd | 2929 | |
fa743953 SR |
2930 | cpu = smp_processor_id(); |
2931 | cpu_buffer = buffer->buffers[cpu]; | |
2932 | ||
fa1b47dd SR |
2933 | /* |
2934 | * This must only be called if the event has not been | |
2935 | * committed yet. Thus we can assume that preemption | |
2936 | * is still disabled. | |
2937 | */ | |
fa743953 | 2938 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
fa1b47dd | 2939 | |
a1863c21 | 2940 | rb_decrement_entry(cpu_buffer, event); |
0f2541d2 | 2941 | if (rb_try_to_discard(cpu_buffer, event)) |
edd813bf | 2942 | goto out; |
fa1b47dd SR |
2943 | |
2944 | /* | |
2945 | * The commit is still visible by the reader, so we | |
a1863c21 | 2946 | * must still update the timestamp. |
fa1b47dd | 2947 | */ |
a1863c21 | 2948 | rb_update_write_stamp(cpu_buffer, event); |
fa1b47dd | 2949 | out: |
fa743953 | 2950 | rb_end_commit(cpu_buffer); |
fa1b47dd | 2951 | |
f3b9aae1 FW |
2952 | trace_recursive_unlock(); |
2953 | ||
5168ae50 | 2954 | preempt_enable_notrace(); |
fa1b47dd SR |
2955 | |
2956 | } | |
2957 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); | |
2958 | ||
7a8e76a3 SR |
2959 | /** |
2960 | * ring_buffer_write - write data to the buffer without reserving | |
2961 | * @buffer: The ring buffer to write to. | |
2962 | * @length: The length of the data being written (excluding the event header) | |
2963 | * @data: The data to write to the buffer. | |
2964 | * | |
2965 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as | |
2966 | * one function. If you already have the data to write to the buffer, it | |
2967 | * may be easier to simply call this function. | |
2968 | * | |
2969 | * Note, like ring_buffer_lock_reserve, the length is the length of the data | |
2970 | * and not the length of the event which would hold the header. | |
2971 | */ | |
2972 | int ring_buffer_write(struct ring_buffer *buffer, | |
01e3e710 DS |
2973 | unsigned long length, |
2974 | void *data) | |
7a8e76a3 SR |
2975 | { |
2976 | struct ring_buffer_per_cpu *cpu_buffer; | |
2977 | struct ring_buffer_event *event; | |
7a8e76a3 SR |
2978 | void *body; |
2979 | int ret = -EBUSY; | |
5168ae50 | 2980 | int cpu; |
7a8e76a3 | 2981 | |
033601a3 | 2982 | if (ring_buffer_flags != RB_BUFFERS_ON) |
a3583244 SR |
2983 | return -EBUSY; |
2984 | ||
5168ae50 | 2985 | preempt_disable_notrace(); |
bf41a158 | 2986 | |
52fbe9cd LJ |
2987 | if (atomic_read(&buffer->record_disabled)) |
2988 | goto out; | |
2989 | ||
7a8e76a3 SR |
2990 | cpu = raw_smp_processor_id(); |
2991 | ||
9e01c1b7 | 2992 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
d769041f | 2993 | goto out; |
7a8e76a3 SR |
2994 | |
2995 | cpu_buffer = buffer->buffers[cpu]; | |
7a8e76a3 SR |
2996 | |
2997 | if (atomic_read(&cpu_buffer->record_disabled)) | |
2998 | goto out; | |
2999 | ||
be957c44 SR |
3000 | if (length > BUF_MAX_DATA_SIZE) |
3001 | goto out; | |
3002 | ||
62f0b3eb | 3003 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
7a8e76a3 SR |
3004 | if (!event) |
3005 | goto out; | |
3006 | ||
3007 | body = rb_event_data(event); | |
3008 | ||
3009 | memcpy(body, data, length); | |
3010 | ||
3011 | rb_commit(cpu_buffer, event); | |
3012 | ||
15693458 SRRH |
3013 | rb_wakeups(buffer, cpu_buffer); |
3014 | ||
7a8e76a3 SR |
3015 | ret = 0; |
3016 | out: | |
5168ae50 | 3017 | preempt_enable_notrace(); |
7a8e76a3 SR |
3018 | |
3019 | return ret; | |
3020 | } | |
c4f50183 | 3021 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
7a8e76a3 | 3022 | |
34a148bf | 3023 | static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
bf41a158 SR |
3024 | { |
3025 | struct buffer_page *reader = cpu_buffer->reader_page; | |
77ae365e | 3026 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
bf41a158 SR |
3027 | struct buffer_page *commit = cpu_buffer->commit_page; |
3028 | ||
77ae365e SR |
3029 | /* In case of error, head will be NULL */ |
3030 | if (unlikely(!head)) | |
3031 | return 1; | |
3032 | ||
bf41a158 SR |
3033 | return reader->read == rb_page_commit(reader) && |
3034 | (commit == reader || | |
3035 | (commit == head && | |
3036 | head->read == rb_page_commit(commit))); | |
3037 | } | |
3038 | ||
7a8e76a3 SR |
3039 | /** |
3040 | * ring_buffer_record_disable - stop all writes into the buffer | |
3041 | * @buffer: The ring buffer to stop writes to. | |
3042 | * | |
3043 | * This prevents all writes to the buffer. Any attempt to write | |
3044 | * to the buffer after this will fail and return NULL. | |
3045 | * | |
3046 | * The caller should call synchronize_sched() after this. | |
3047 | */ | |
3048 | void ring_buffer_record_disable(struct ring_buffer *buffer) | |
3049 | { | |
3050 | atomic_inc(&buffer->record_disabled); | |
3051 | } | |
c4f50183 | 3052 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
7a8e76a3 SR |
3053 | |
3054 | /** | |
3055 | * ring_buffer_record_enable - enable writes to the buffer | |
3056 | * @buffer: The ring buffer to enable writes | |
3057 | * | |
3058 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 3059 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
3060 | */ |
3061 | void ring_buffer_record_enable(struct ring_buffer *buffer) | |
3062 | { | |
3063 | atomic_dec(&buffer->record_disabled); | |
3064 | } | |
c4f50183 | 3065 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
7a8e76a3 | 3066 | |
499e5470 SR |
3067 | /** |
3068 | * ring_buffer_record_off - stop all writes into the buffer | |
3069 | * @buffer: The ring buffer to stop writes to. | |
3070 | * | |
3071 | * This prevents all writes to the buffer. Any attempt to write | |
3072 | * to the buffer after this will fail and return NULL. | |
3073 | * | |
3074 | * This is different than ring_buffer_record_disable() as | |
87abb3b1 | 3075 | * it works like an on/off switch, where as the disable() version |
499e5470 SR |
3076 | * must be paired with a enable(). |
3077 | */ | |
3078 | void ring_buffer_record_off(struct ring_buffer *buffer) | |
3079 | { | |
3080 | unsigned int rd; | |
3081 | unsigned int new_rd; | |
3082 | ||
3083 | do { | |
3084 | rd = atomic_read(&buffer->record_disabled); | |
3085 | new_rd = rd | RB_BUFFER_OFF; | |
3086 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); | |
3087 | } | |
3088 | EXPORT_SYMBOL_GPL(ring_buffer_record_off); | |
3089 | ||
3090 | /** | |
3091 | * ring_buffer_record_on - restart writes into the buffer | |
3092 | * @buffer: The ring buffer to start writes to. | |
3093 | * | |
3094 | * This enables all writes to the buffer that was disabled by | |
3095 | * ring_buffer_record_off(). | |
3096 | * | |
3097 | * This is different than ring_buffer_record_enable() as | |
87abb3b1 | 3098 | * it works like an on/off switch, where as the enable() version |
499e5470 SR |
3099 | * must be paired with a disable(). |
3100 | */ | |
3101 | void ring_buffer_record_on(struct ring_buffer *buffer) | |
3102 | { | |
3103 | unsigned int rd; | |
3104 | unsigned int new_rd; | |
3105 | ||
3106 | do { | |
3107 | rd = atomic_read(&buffer->record_disabled); | |
3108 | new_rd = rd & ~RB_BUFFER_OFF; | |
3109 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); | |
3110 | } | |
3111 | EXPORT_SYMBOL_GPL(ring_buffer_record_on); | |
3112 | ||
3113 | /** | |
3114 | * ring_buffer_record_is_on - return true if the ring buffer can write | |
3115 | * @buffer: The ring buffer to see if write is enabled | |
3116 | * | |
3117 | * Returns true if the ring buffer is in a state that it accepts writes. | |
3118 | */ | |
3119 | int ring_buffer_record_is_on(struct ring_buffer *buffer) | |
3120 | { | |
3121 | return !atomic_read(&buffer->record_disabled); | |
3122 | } | |
3123 | ||
7a8e76a3 SR |
3124 | /** |
3125 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer | |
3126 | * @buffer: The ring buffer to stop writes to. | |
3127 | * @cpu: The CPU buffer to stop | |
3128 | * | |
3129 | * This prevents all writes to the buffer. Any attempt to write | |
3130 | * to the buffer after this will fail and return NULL. | |
3131 | * | |
3132 | * The caller should call synchronize_sched() after this. | |
3133 | */ | |
3134 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) | |
3135 | { | |
3136 | struct ring_buffer_per_cpu *cpu_buffer; | |
3137 | ||
9e01c1b7 | 3138 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3139 | return; |
7a8e76a3 SR |
3140 | |
3141 | cpu_buffer = buffer->buffers[cpu]; | |
3142 | atomic_inc(&cpu_buffer->record_disabled); | |
3143 | } | |
c4f50183 | 3144 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
7a8e76a3 SR |
3145 | |
3146 | /** | |
3147 | * ring_buffer_record_enable_cpu - enable writes to the buffer | |
3148 | * @buffer: The ring buffer to enable writes | |
3149 | * @cpu: The CPU to enable. | |
3150 | * | |
3151 | * Note, multiple disables will need the same number of enables | |
c41b20e7 | 3152 | * to truly enable the writing (much like preempt_disable). |
7a8e76a3 SR |
3153 | */ |
3154 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) | |
3155 | { | |
3156 | struct ring_buffer_per_cpu *cpu_buffer; | |
3157 | ||
9e01c1b7 | 3158 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3159 | return; |
7a8e76a3 SR |
3160 | |
3161 | cpu_buffer = buffer->buffers[cpu]; | |
3162 | atomic_dec(&cpu_buffer->record_disabled); | |
3163 | } | |
c4f50183 | 3164 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
7a8e76a3 | 3165 | |
f6195aa0 SR |
3166 | /* |
3167 | * The total entries in the ring buffer is the running counter | |
3168 | * of entries entered into the ring buffer, minus the sum of | |
3169 | * the entries read from the ring buffer and the number of | |
3170 | * entries that were overwritten. | |
3171 | */ | |
3172 | static inline unsigned long | |
3173 | rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) | |
3174 | { | |
3175 | return local_read(&cpu_buffer->entries) - | |
3176 | (local_read(&cpu_buffer->overrun) + cpu_buffer->read); | |
3177 | } | |
3178 | ||
c64e148a VN |
3179 | /** |
3180 | * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer | |
3181 | * @buffer: The ring buffer | |
3182 | * @cpu: The per CPU buffer to read from. | |
3183 | */ | |
50ecf2c3 | 3184 | u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu) |
c64e148a VN |
3185 | { |
3186 | unsigned long flags; | |
3187 | struct ring_buffer_per_cpu *cpu_buffer; | |
3188 | struct buffer_page *bpage; | |
da830e58 | 3189 | u64 ret = 0; |
c64e148a VN |
3190 | |
3191 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
3192 | return 0; | |
3193 | ||
3194 | cpu_buffer = buffer->buffers[cpu]; | |
7115e3fc | 3195 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
c64e148a VN |
3196 | /* |
3197 | * if the tail is on reader_page, oldest time stamp is on the reader | |
3198 | * page | |
3199 | */ | |
3200 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) | |
3201 | bpage = cpu_buffer->reader_page; | |
3202 | else | |
3203 | bpage = rb_set_head_page(cpu_buffer); | |
54f7be5b SR |
3204 | if (bpage) |
3205 | ret = bpage->page->time_stamp; | |
7115e3fc | 3206 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
c64e148a VN |
3207 | |
3208 | return ret; | |
3209 | } | |
3210 | EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts); | |
3211 | ||
3212 | /** | |
3213 | * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer | |
3214 | * @buffer: The ring buffer | |
3215 | * @cpu: The per CPU buffer to read from. | |
3216 | */ | |
3217 | unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu) | |
3218 | { | |
3219 | struct ring_buffer_per_cpu *cpu_buffer; | |
3220 | unsigned long ret; | |
3221 | ||
3222 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
3223 | return 0; | |
3224 | ||
3225 | cpu_buffer = buffer->buffers[cpu]; | |
3226 | ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes; | |
3227 | ||
3228 | return ret; | |
3229 | } | |
3230 | EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu); | |
3231 | ||
7a8e76a3 SR |
3232 | /** |
3233 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer | |
3234 | * @buffer: The ring buffer | |
3235 | * @cpu: The per CPU buffer to get the entries from. | |
3236 | */ | |
3237 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) | |
3238 | { | |
3239 | struct ring_buffer_per_cpu *cpu_buffer; | |
3240 | ||
9e01c1b7 | 3241 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3242 | return 0; |
7a8e76a3 SR |
3243 | |
3244 | cpu_buffer = buffer->buffers[cpu]; | |
554f786e | 3245 | |
f6195aa0 | 3246 | return rb_num_of_entries(cpu_buffer); |
7a8e76a3 | 3247 | } |
c4f50183 | 3248 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
7a8e76a3 SR |
3249 | |
3250 | /** | |
884bfe89 SP |
3251 | * ring_buffer_overrun_cpu - get the number of overruns caused by the ring |
3252 | * buffer wrapping around (only if RB_FL_OVERWRITE is on). | |
7a8e76a3 SR |
3253 | * @buffer: The ring buffer |
3254 | * @cpu: The per CPU buffer to get the number of overruns from | |
3255 | */ | |
3256 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
3257 | { | |
3258 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 3259 | unsigned long ret; |
7a8e76a3 | 3260 | |
9e01c1b7 | 3261 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3262 | return 0; |
7a8e76a3 SR |
3263 | |
3264 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 3265 | ret = local_read(&cpu_buffer->overrun); |
554f786e SR |
3266 | |
3267 | return ret; | |
7a8e76a3 | 3268 | } |
c4f50183 | 3269 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
7a8e76a3 | 3270 | |
f0d2c681 | 3271 | /** |
884bfe89 SP |
3272 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by |
3273 | * commits failing due to the buffer wrapping around while there are uncommitted | |
3274 | * events, such as during an interrupt storm. | |
f0d2c681 SR |
3275 | * @buffer: The ring buffer |
3276 | * @cpu: The per CPU buffer to get the number of overruns from | |
3277 | */ | |
3278 | unsigned long | |
3279 | ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu) | |
3280 | { | |
3281 | struct ring_buffer_per_cpu *cpu_buffer; | |
3282 | unsigned long ret; | |
3283 | ||
3284 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
3285 | return 0; | |
3286 | ||
3287 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 3288 | ret = local_read(&cpu_buffer->commit_overrun); |
f0d2c681 SR |
3289 | |
3290 | return ret; | |
3291 | } | |
3292 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); | |
3293 | ||
884bfe89 SP |
3294 | /** |
3295 | * ring_buffer_dropped_events_cpu - get the number of dropped events caused by | |
3296 | * the ring buffer filling up (only if RB_FL_OVERWRITE is off). | |
3297 | * @buffer: The ring buffer | |
3298 | * @cpu: The per CPU buffer to get the number of overruns from | |
3299 | */ | |
3300 | unsigned long | |
3301 | ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu) | |
3302 | { | |
3303 | struct ring_buffer_per_cpu *cpu_buffer; | |
3304 | unsigned long ret; | |
3305 | ||
3306 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
3307 | return 0; | |
3308 | ||
3309 | cpu_buffer = buffer->buffers[cpu]; | |
3310 | ret = local_read(&cpu_buffer->dropped_events); | |
3311 | ||
3312 | return ret; | |
3313 | } | |
3314 | EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu); | |
3315 | ||
ad964704 SRRH |
3316 | /** |
3317 | * ring_buffer_read_events_cpu - get the number of events successfully read | |
3318 | * @buffer: The ring buffer | |
3319 | * @cpu: The per CPU buffer to get the number of events read | |
3320 | */ | |
3321 | unsigned long | |
3322 | ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu) | |
3323 | { | |
3324 | struct ring_buffer_per_cpu *cpu_buffer; | |
3325 | ||
3326 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
3327 | return 0; | |
3328 | ||
3329 | cpu_buffer = buffer->buffers[cpu]; | |
3330 | return cpu_buffer->read; | |
3331 | } | |
3332 | EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu); | |
3333 | ||
7a8e76a3 SR |
3334 | /** |
3335 | * ring_buffer_entries - get the number of entries in a buffer | |
3336 | * @buffer: The ring buffer | |
3337 | * | |
3338 | * Returns the total number of entries in the ring buffer | |
3339 | * (all CPU entries) | |
3340 | */ | |
3341 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) | |
3342 | { | |
3343 | struct ring_buffer_per_cpu *cpu_buffer; | |
3344 | unsigned long entries = 0; | |
3345 | int cpu; | |
3346 | ||
3347 | /* if you care about this being correct, lock the buffer */ | |
3348 | for_each_buffer_cpu(buffer, cpu) { | |
3349 | cpu_buffer = buffer->buffers[cpu]; | |
f6195aa0 | 3350 | entries += rb_num_of_entries(cpu_buffer); |
7a8e76a3 SR |
3351 | } |
3352 | ||
3353 | return entries; | |
3354 | } | |
c4f50183 | 3355 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
7a8e76a3 SR |
3356 | |
3357 | /** | |
67b394f7 | 3358 | * ring_buffer_overruns - get the number of overruns in buffer |
7a8e76a3 SR |
3359 | * @buffer: The ring buffer |
3360 | * | |
3361 | * Returns the total number of overruns in the ring buffer | |
3362 | * (all CPU entries) | |
3363 | */ | |
3364 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) | |
3365 | { | |
3366 | struct ring_buffer_per_cpu *cpu_buffer; | |
3367 | unsigned long overruns = 0; | |
3368 | int cpu; | |
3369 | ||
3370 | /* if you care about this being correct, lock the buffer */ | |
3371 | for_each_buffer_cpu(buffer, cpu) { | |
3372 | cpu_buffer = buffer->buffers[cpu]; | |
77ae365e | 3373 | overruns += local_read(&cpu_buffer->overrun); |
7a8e76a3 SR |
3374 | } |
3375 | ||
3376 | return overruns; | |
3377 | } | |
c4f50183 | 3378 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
7a8e76a3 | 3379 | |
642edba5 | 3380 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
7a8e76a3 SR |
3381 | { |
3382 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
3383 | ||
d769041f | 3384 | /* Iterator usage is expected to have record disabled */ |
651e22f2 SRRH |
3385 | iter->head_page = cpu_buffer->reader_page; |
3386 | iter->head = cpu_buffer->reader_page->read; | |
3387 | ||
3388 | iter->cache_reader_page = iter->head_page; | |
24607f11 | 3389 | iter->cache_read = cpu_buffer->read; |
651e22f2 | 3390 | |
d769041f SR |
3391 | if (iter->head) |
3392 | iter->read_stamp = cpu_buffer->read_stamp; | |
3393 | else | |
abc9b56d | 3394 | iter->read_stamp = iter->head_page->page->time_stamp; |
642edba5 | 3395 | } |
f83c9d0f | 3396 | |
642edba5 SR |
3397 | /** |
3398 | * ring_buffer_iter_reset - reset an iterator | |
3399 | * @iter: The iterator to reset | |
3400 | * | |
3401 | * Resets the iterator, so that it will start from the beginning | |
3402 | * again. | |
3403 | */ | |
3404 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) | |
3405 | { | |
554f786e | 3406 | struct ring_buffer_per_cpu *cpu_buffer; |
642edba5 SR |
3407 | unsigned long flags; |
3408 | ||
554f786e SR |
3409 | if (!iter) |
3410 | return; | |
3411 | ||
3412 | cpu_buffer = iter->cpu_buffer; | |
3413 | ||
5389f6fa | 3414 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
642edba5 | 3415 | rb_iter_reset(iter); |
5389f6fa | 3416 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 | 3417 | } |
c4f50183 | 3418 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
7a8e76a3 SR |
3419 | |
3420 | /** | |
3421 | * ring_buffer_iter_empty - check if an iterator has no more to read | |
3422 | * @iter: The iterator to check | |
3423 | */ | |
3424 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) | |
3425 | { | |
3426 | struct ring_buffer_per_cpu *cpu_buffer; | |
3427 | ||
3428 | cpu_buffer = iter->cpu_buffer; | |
3429 | ||
bf41a158 SR |
3430 | return iter->head_page == cpu_buffer->commit_page && |
3431 | iter->head == rb_commit_index(cpu_buffer); | |
7a8e76a3 | 3432 | } |
c4f50183 | 3433 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
7a8e76a3 SR |
3434 | |
3435 | static void | |
3436 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, | |
3437 | struct ring_buffer_event *event) | |
3438 | { | |
3439 | u64 delta; | |
3440 | ||
334d4169 | 3441 | switch (event->type_len) { |
7a8e76a3 SR |
3442 | case RINGBUF_TYPE_PADDING: |
3443 | return; | |
3444 | ||
3445 | case RINGBUF_TYPE_TIME_EXTEND: | |
3446 | delta = event->array[0]; | |
3447 | delta <<= TS_SHIFT; | |
3448 | delta += event->time_delta; | |
3449 | cpu_buffer->read_stamp += delta; | |
3450 | return; | |
3451 | ||
3452 | case RINGBUF_TYPE_TIME_STAMP: | |
3453 | /* FIXME: not implemented */ | |
3454 | return; | |
3455 | ||
3456 | case RINGBUF_TYPE_DATA: | |
3457 | cpu_buffer->read_stamp += event->time_delta; | |
3458 | return; | |
3459 | ||
3460 | default: | |
3461 | BUG(); | |
3462 | } | |
3463 | return; | |
3464 | } | |
3465 | ||
3466 | static void | |
3467 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, | |
3468 | struct ring_buffer_event *event) | |
3469 | { | |
3470 | u64 delta; | |
3471 | ||
334d4169 | 3472 | switch (event->type_len) { |
7a8e76a3 SR |
3473 | case RINGBUF_TYPE_PADDING: |
3474 | return; | |
3475 | ||
3476 | case RINGBUF_TYPE_TIME_EXTEND: | |
3477 | delta = event->array[0]; | |
3478 | delta <<= TS_SHIFT; | |
3479 | delta += event->time_delta; | |
3480 | iter->read_stamp += delta; | |
3481 | return; | |
3482 | ||
3483 | case RINGBUF_TYPE_TIME_STAMP: | |
3484 | /* FIXME: not implemented */ | |
3485 | return; | |
3486 | ||
3487 | case RINGBUF_TYPE_DATA: | |
3488 | iter->read_stamp += event->time_delta; | |
3489 | return; | |
3490 | ||
3491 | default: | |
3492 | BUG(); | |
3493 | } | |
3494 | return; | |
3495 | } | |
3496 | ||
d769041f SR |
3497 | static struct buffer_page * |
3498 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) | |
7a8e76a3 | 3499 | { |
d769041f | 3500 | struct buffer_page *reader = NULL; |
66a8cb95 | 3501 | unsigned long overwrite; |
d769041f | 3502 | unsigned long flags; |
818e3dd3 | 3503 | int nr_loops = 0; |
77ae365e | 3504 | int ret; |
d769041f | 3505 | |
3e03fb7f | 3506 | local_irq_save(flags); |
0199c4e6 | 3507 | arch_spin_lock(&cpu_buffer->lock); |
d769041f SR |
3508 | |
3509 | again: | |
818e3dd3 SR |
3510 | /* |
3511 | * This should normally only loop twice. But because the | |
3512 | * start of the reader inserts an empty page, it causes | |
3513 | * a case where we will loop three times. There should be no | |
3514 | * reason to loop four times (that I know of). | |
3515 | */ | |
3e89c7bb | 3516 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
818e3dd3 SR |
3517 | reader = NULL; |
3518 | goto out; | |
3519 | } | |
3520 | ||
d769041f SR |
3521 | reader = cpu_buffer->reader_page; |
3522 | ||
3523 | /* If there's more to read, return this page */ | |
bf41a158 | 3524 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
d769041f SR |
3525 | goto out; |
3526 | ||
3527 | /* Never should we have an index greater than the size */ | |
3e89c7bb SR |
3528 | if (RB_WARN_ON(cpu_buffer, |
3529 | cpu_buffer->reader_page->read > rb_page_size(reader))) | |
3530 | goto out; | |
d769041f SR |
3531 | |
3532 | /* check if we caught up to the tail */ | |
3533 | reader = NULL; | |
bf41a158 | 3534 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
d769041f | 3535 | goto out; |
7a8e76a3 | 3536 | |
a5fb8331 SR |
3537 | /* Don't bother swapping if the ring buffer is empty */ |
3538 | if (rb_num_of_entries(cpu_buffer) == 0) | |
3539 | goto out; | |
3540 | ||
7a8e76a3 | 3541 | /* |
d769041f | 3542 | * Reset the reader page to size zero. |
7a8e76a3 | 3543 | */ |
77ae365e SR |
3544 | local_set(&cpu_buffer->reader_page->write, 0); |
3545 | local_set(&cpu_buffer->reader_page->entries, 0); | |
3546 | local_set(&cpu_buffer->reader_page->page->commit, 0); | |
ff0ff84a | 3547 | cpu_buffer->reader_page->real_end = 0; |
7a8e76a3 | 3548 | |
77ae365e SR |
3549 | spin: |
3550 | /* | |
3551 | * Splice the empty reader page into the list around the head. | |
3552 | */ | |
3553 | reader = rb_set_head_page(cpu_buffer); | |
54f7be5b SR |
3554 | if (!reader) |
3555 | goto out; | |
0e1ff5d7 | 3556 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
d769041f | 3557 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
bf41a158 | 3558 | |
3adc54fa SR |
3559 | /* |
3560 | * cpu_buffer->pages just needs to point to the buffer, it | |
3561 | * has no specific buffer page to point to. Lets move it out | |
25985edc | 3562 | * of our way so we don't accidentally swap it. |
3adc54fa SR |
3563 | */ |
3564 | cpu_buffer->pages = reader->list.prev; | |
3565 | ||
77ae365e SR |
3566 | /* The reader page will be pointing to the new head */ |
3567 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); | |
7a8e76a3 | 3568 | |
66a8cb95 SR |
3569 | /* |
3570 | * We want to make sure we read the overruns after we set up our | |
3571 | * pointers to the next object. The writer side does a | |
3572 | * cmpxchg to cross pages which acts as the mb on the writer | |
3573 | * side. Note, the reader will constantly fail the swap | |
3574 | * while the writer is updating the pointers, so this | |
3575 | * guarantees that the overwrite recorded here is the one we | |
3576 | * want to compare with the last_overrun. | |
3577 | */ | |
3578 | smp_mb(); | |
3579 | overwrite = local_read(&(cpu_buffer->overrun)); | |
3580 | ||
77ae365e SR |
3581 | /* |
3582 | * Here's the tricky part. | |
3583 | * | |
3584 | * We need to move the pointer past the header page. | |
3585 | * But we can only do that if a writer is not currently | |
3586 | * moving it. The page before the header page has the | |
3587 | * flag bit '1' set if it is pointing to the page we want. | |
3588 | * but if the writer is in the process of moving it | |
3589 | * than it will be '2' or already moved '0'. | |
3590 | */ | |
3591 | ||
3592 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); | |
7a8e76a3 SR |
3593 | |
3594 | /* | |
77ae365e | 3595 | * If we did not convert it, then we must try again. |
7a8e76a3 | 3596 | */ |
77ae365e SR |
3597 | if (!ret) |
3598 | goto spin; | |
7a8e76a3 | 3599 | |
77ae365e SR |
3600 | /* |
3601 | * Yeah! We succeeded in replacing the page. | |
3602 | * | |
3603 | * Now make the new head point back to the reader page. | |
3604 | */ | |
5ded3dc6 | 3605 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
77ae365e | 3606 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
d769041f SR |
3607 | |
3608 | /* Finally update the reader page to the new head */ | |
3609 | cpu_buffer->reader_page = reader; | |
3610 | rb_reset_reader_page(cpu_buffer); | |
3611 | ||
66a8cb95 SR |
3612 | if (overwrite != cpu_buffer->last_overrun) { |
3613 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; | |
3614 | cpu_buffer->last_overrun = overwrite; | |
3615 | } | |
3616 | ||
d769041f SR |
3617 | goto again; |
3618 | ||
3619 | out: | |
0199c4e6 | 3620 | arch_spin_unlock(&cpu_buffer->lock); |
3e03fb7f | 3621 | local_irq_restore(flags); |
d769041f SR |
3622 | |
3623 | return reader; | |
3624 | } | |
3625 | ||
3626 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) | |
3627 | { | |
3628 | struct ring_buffer_event *event; | |
3629 | struct buffer_page *reader; | |
3630 | unsigned length; | |
3631 | ||
3632 | reader = rb_get_reader_page(cpu_buffer); | |
7a8e76a3 | 3633 | |
d769041f | 3634 | /* This function should not be called when buffer is empty */ |
3e89c7bb SR |
3635 | if (RB_WARN_ON(cpu_buffer, !reader)) |
3636 | return; | |
7a8e76a3 | 3637 | |
d769041f SR |
3638 | event = rb_reader_event(cpu_buffer); |
3639 | ||
a1863c21 | 3640 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
e4906eff | 3641 | cpu_buffer->read++; |
d769041f SR |
3642 | |
3643 | rb_update_read_stamp(cpu_buffer, event); | |
3644 | ||
3645 | length = rb_event_length(event); | |
6f807acd | 3646 | cpu_buffer->reader_page->read += length; |
7a8e76a3 SR |
3647 | } |
3648 | ||
3649 | static void rb_advance_iter(struct ring_buffer_iter *iter) | |
3650 | { | |
7a8e76a3 SR |
3651 | struct ring_buffer_per_cpu *cpu_buffer; |
3652 | struct ring_buffer_event *event; | |
3653 | unsigned length; | |
3654 | ||
3655 | cpu_buffer = iter->cpu_buffer; | |
7a8e76a3 SR |
3656 | |
3657 | /* | |
3658 | * Check if we are at the end of the buffer. | |
3659 | */ | |
bf41a158 | 3660 | if (iter->head >= rb_page_size(iter->head_page)) { |
ea05b57c SR |
3661 | /* discarded commits can make the page empty */ |
3662 | if (iter->head_page == cpu_buffer->commit_page) | |
3e89c7bb | 3663 | return; |
d769041f | 3664 | rb_inc_iter(iter); |
7a8e76a3 SR |
3665 | return; |
3666 | } | |
3667 | ||
3668 | event = rb_iter_head_event(iter); | |
3669 | ||
3670 | length = rb_event_length(event); | |
3671 | ||
3672 | /* | |
3673 | * This should not be called to advance the header if we are | |
3674 | * at the tail of the buffer. | |
3675 | */ | |
3e89c7bb | 3676 | if (RB_WARN_ON(cpu_buffer, |
f536aafc | 3677 | (iter->head_page == cpu_buffer->commit_page) && |
3e89c7bb SR |
3678 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
3679 | return; | |
7a8e76a3 SR |
3680 | |
3681 | rb_update_iter_read_stamp(iter, event); | |
3682 | ||
3683 | iter->head += length; | |
3684 | ||
3685 | /* check for end of page padding */ | |
bf41a158 SR |
3686 | if ((iter->head >= rb_page_size(iter->head_page)) && |
3687 | (iter->head_page != cpu_buffer->commit_page)) | |
771e0384 | 3688 | rb_inc_iter(iter); |
7a8e76a3 SR |
3689 | } |
3690 | ||
66a8cb95 SR |
3691 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
3692 | { | |
3693 | return cpu_buffer->lost_events; | |
3694 | } | |
3695 | ||
f83c9d0f | 3696 | static struct ring_buffer_event * |
66a8cb95 SR |
3697 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
3698 | unsigned long *lost_events) | |
7a8e76a3 | 3699 | { |
7a8e76a3 | 3700 | struct ring_buffer_event *event; |
d769041f | 3701 | struct buffer_page *reader; |
818e3dd3 | 3702 | int nr_loops = 0; |
7a8e76a3 | 3703 | |
7a8e76a3 | 3704 | again: |
818e3dd3 | 3705 | /* |
69d1b839 SR |
3706 | * We repeat when a time extend is encountered. |
3707 | * Since the time extend is always attached to a data event, | |
3708 | * we should never loop more than once. | |
3709 | * (We never hit the following condition more than twice). | |
818e3dd3 | 3710 | */ |
69d1b839 | 3711 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) |
818e3dd3 | 3712 | return NULL; |
818e3dd3 | 3713 | |
d769041f SR |
3714 | reader = rb_get_reader_page(cpu_buffer); |
3715 | if (!reader) | |
7a8e76a3 SR |
3716 | return NULL; |
3717 | ||
d769041f | 3718 | event = rb_reader_event(cpu_buffer); |
7a8e76a3 | 3719 | |
334d4169 | 3720 | switch (event->type_len) { |
7a8e76a3 | 3721 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
3722 | if (rb_null_event(event)) |
3723 | RB_WARN_ON(cpu_buffer, 1); | |
3724 | /* | |
3725 | * Because the writer could be discarding every | |
3726 | * event it creates (which would probably be bad) | |
3727 | * if we were to go back to "again" then we may never | |
3728 | * catch up, and will trigger the warn on, or lock | |
3729 | * the box. Return the padding, and we will release | |
3730 | * the current locks, and try again. | |
3731 | */ | |
2d622719 | 3732 | return event; |
7a8e76a3 SR |
3733 | |
3734 | case RINGBUF_TYPE_TIME_EXTEND: | |
3735 | /* Internal data, OK to advance */ | |
d769041f | 3736 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3737 | goto again; |
3738 | ||
3739 | case RINGBUF_TYPE_TIME_STAMP: | |
3740 | /* FIXME: not implemented */ | |
d769041f | 3741 | rb_advance_reader(cpu_buffer); |
7a8e76a3 SR |
3742 | goto again; |
3743 | ||
3744 | case RINGBUF_TYPE_DATA: | |
3745 | if (ts) { | |
3746 | *ts = cpu_buffer->read_stamp + event->time_delta; | |
d8eeb2d3 | 3747 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
37886f6a | 3748 | cpu_buffer->cpu, ts); |
7a8e76a3 | 3749 | } |
66a8cb95 SR |
3750 | if (lost_events) |
3751 | *lost_events = rb_lost_events(cpu_buffer); | |
7a8e76a3 SR |
3752 | return event; |
3753 | ||
3754 | default: | |
3755 | BUG(); | |
3756 | } | |
3757 | ||
3758 | return NULL; | |
3759 | } | |
c4f50183 | 3760 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
7a8e76a3 | 3761 | |
f83c9d0f SR |
3762 | static struct ring_buffer_event * |
3763 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |
7a8e76a3 SR |
3764 | { |
3765 | struct ring_buffer *buffer; | |
3766 | struct ring_buffer_per_cpu *cpu_buffer; | |
3767 | struct ring_buffer_event *event; | |
818e3dd3 | 3768 | int nr_loops = 0; |
7a8e76a3 | 3769 | |
7a8e76a3 SR |
3770 | cpu_buffer = iter->cpu_buffer; |
3771 | buffer = cpu_buffer->buffer; | |
3772 | ||
492a74f4 SR |
3773 | /* |
3774 | * Check if someone performed a consuming read to | |
3775 | * the buffer. A consuming read invalidates the iterator | |
3776 | * and we need to reset the iterator in this case. | |
3777 | */ | |
3778 | if (unlikely(iter->cache_read != cpu_buffer->read || | |
3779 | iter->cache_reader_page != cpu_buffer->reader_page)) | |
3780 | rb_iter_reset(iter); | |
3781 | ||
7a8e76a3 | 3782 | again: |
3c05d748 SR |
3783 | if (ring_buffer_iter_empty(iter)) |
3784 | return NULL; | |
3785 | ||
818e3dd3 | 3786 | /* |
021de3d9 SRRH |
3787 | * We repeat when a time extend is encountered or we hit |
3788 | * the end of the page. Since the time extend is always attached | |
3789 | * to a data event, we should never loop more than three times. | |
3790 | * Once for going to next page, once on time extend, and | |
3791 | * finally once to get the event. | |
3792 | * (We never hit the following condition more than thrice). | |
818e3dd3 | 3793 | */ |
021de3d9 | 3794 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) |
818e3dd3 | 3795 | return NULL; |
818e3dd3 | 3796 | |
7a8e76a3 SR |
3797 | if (rb_per_cpu_empty(cpu_buffer)) |
3798 | return NULL; | |
3799 | ||
10e83fd0 | 3800 | if (iter->head >= rb_page_size(iter->head_page)) { |
3c05d748 SR |
3801 | rb_inc_iter(iter); |
3802 | goto again; | |
3803 | } | |
3804 | ||
7a8e76a3 SR |
3805 | event = rb_iter_head_event(iter); |
3806 | ||
334d4169 | 3807 | switch (event->type_len) { |
7a8e76a3 | 3808 | case RINGBUF_TYPE_PADDING: |
2d622719 TZ |
3809 | if (rb_null_event(event)) { |
3810 | rb_inc_iter(iter); | |
3811 | goto again; | |
3812 | } | |
3813 | rb_advance_iter(iter); | |
3814 | return event; | |
7a8e76a3 SR |
3815 | |
3816 | case RINGBUF_TYPE_TIME_EXTEND: | |
3817 | /* Internal data, OK to advance */ | |
3818 | rb_advance_iter(iter); | |
3819 | goto again; | |
3820 | ||
3821 | case RINGBUF_TYPE_TIME_STAMP: | |
3822 | /* FIXME: not implemented */ | |
3823 | rb_advance_iter(iter); | |
3824 | goto again; | |
3825 | ||
3826 | case RINGBUF_TYPE_DATA: | |
3827 | if (ts) { | |
3828 | *ts = iter->read_stamp + event->time_delta; | |
37886f6a SR |
3829 | ring_buffer_normalize_time_stamp(buffer, |
3830 | cpu_buffer->cpu, ts); | |
7a8e76a3 SR |
3831 | } |
3832 | return event; | |
3833 | ||
3834 | default: | |
3835 | BUG(); | |
3836 | } | |
3837 | ||
3838 | return NULL; | |
3839 | } | |
c4f50183 | 3840 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
7a8e76a3 | 3841 | |
8d707e8e SR |
3842 | static inline int rb_ok_to_lock(void) |
3843 | { | |
3844 | /* | |
3845 | * If an NMI die dumps out the content of the ring buffer | |
3846 | * do not grab locks. We also permanently disable the ring | |
3847 | * buffer too. A one time deal is all you get from reading | |
3848 | * the ring buffer from an NMI. | |
3849 | */ | |
464e85eb | 3850 | if (likely(!in_nmi())) |
8d707e8e SR |
3851 | return 1; |
3852 | ||
3853 | tracing_off_permanent(); | |
3854 | return 0; | |
3855 | } | |
3856 | ||
f83c9d0f SR |
3857 | /** |
3858 | * ring_buffer_peek - peek at the next event to be read | |
3859 | * @buffer: The ring buffer to read | |
3860 | * @cpu: The cpu to peak at | |
3861 | * @ts: The timestamp counter of this event. | |
66a8cb95 | 3862 | * @lost_events: a variable to store if events were lost (may be NULL) |
f83c9d0f SR |
3863 | * |
3864 | * This will return the event that will be read next, but does | |
3865 | * not consume the data. | |
3866 | */ | |
3867 | struct ring_buffer_event * | |
66a8cb95 SR |
3868 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, |
3869 | unsigned long *lost_events) | |
f83c9d0f SR |
3870 | { |
3871 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
8aabee57 | 3872 | struct ring_buffer_event *event; |
f83c9d0f | 3873 | unsigned long flags; |
8d707e8e | 3874 | int dolock; |
f83c9d0f | 3875 | |
554f786e | 3876 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 3877 | return NULL; |
554f786e | 3878 | |
8d707e8e | 3879 | dolock = rb_ok_to_lock(); |
2d622719 | 3880 | again: |
8d707e8e SR |
3881 | local_irq_save(flags); |
3882 | if (dolock) | |
5389f6fa | 3883 | raw_spin_lock(&cpu_buffer->reader_lock); |
66a8cb95 | 3884 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
469535a5 RR |
3885 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
3886 | rb_advance_reader(cpu_buffer); | |
8d707e8e | 3887 | if (dolock) |
5389f6fa | 3888 | raw_spin_unlock(&cpu_buffer->reader_lock); |
8d707e8e | 3889 | local_irq_restore(flags); |
f83c9d0f | 3890 | |
1b959e18 | 3891 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3892 | goto again; |
2d622719 | 3893 | |
f83c9d0f SR |
3894 | return event; |
3895 | } | |
3896 | ||
3897 | /** | |
3898 | * ring_buffer_iter_peek - peek at the next event to be read | |
3899 | * @iter: The ring buffer iterator | |
3900 | * @ts: The timestamp counter of this event. | |
3901 | * | |
3902 | * This will return the event that will be read next, but does | |
3903 | * not increment the iterator. | |
3904 | */ | |
3905 | struct ring_buffer_event * | |
3906 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |
3907 | { | |
3908 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
3909 | struct ring_buffer_event *event; | |
3910 | unsigned long flags; | |
3911 | ||
2d622719 | 3912 | again: |
5389f6fa | 3913 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
f83c9d0f | 3914 | event = rb_iter_peek(iter, ts); |
5389f6fa | 3915 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
f83c9d0f | 3916 | |
1b959e18 | 3917 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3918 | goto again; |
2d622719 | 3919 | |
f83c9d0f SR |
3920 | return event; |
3921 | } | |
3922 | ||
7a8e76a3 SR |
3923 | /** |
3924 | * ring_buffer_consume - return an event and consume it | |
3925 | * @buffer: The ring buffer to get the next event from | |
66a8cb95 SR |
3926 | * @cpu: the cpu to read the buffer from |
3927 | * @ts: a variable to store the timestamp (may be NULL) | |
3928 | * @lost_events: a variable to store if events were lost (may be NULL) | |
7a8e76a3 SR |
3929 | * |
3930 | * Returns the next event in the ring buffer, and that event is consumed. | |
3931 | * Meaning, that sequential reads will keep returning a different event, | |
3932 | * and eventually empty the ring buffer if the producer is slower. | |
3933 | */ | |
3934 | struct ring_buffer_event * | |
66a8cb95 SR |
3935 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, |
3936 | unsigned long *lost_events) | |
7a8e76a3 | 3937 | { |
554f786e SR |
3938 | struct ring_buffer_per_cpu *cpu_buffer; |
3939 | struct ring_buffer_event *event = NULL; | |
f83c9d0f | 3940 | unsigned long flags; |
8d707e8e SR |
3941 | int dolock; |
3942 | ||
3943 | dolock = rb_ok_to_lock(); | |
7a8e76a3 | 3944 | |
2d622719 | 3945 | again: |
554f786e SR |
3946 | /* might be called in atomic */ |
3947 | preempt_disable(); | |
3948 | ||
9e01c1b7 | 3949 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
554f786e | 3950 | goto out; |
7a8e76a3 | 3951 | |
554f786e | 3952 | cpu_buffer = buffer->buffers[cpu]; |
8d707e8e SR |
3953 | local_irq_save(flags); |
3954 | if (dolock) | |
5389f6fa | 3955 | raw_spin_lock(&cpu_buffer->reader_lock); |
f83c9d0f | 3956 | |
66a8cb95 SR |
3957 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
3958 | if (event) { | |
3959 | cpu_buffer->lost_events = 0; | |
469535a5 | 3960 | rb_advance_reader(cpu_buffer); |
66a8cb95 | 3961 | } |
7a8e76a3 | 3962 | |
8d707e8e | 3963 | if (dolock) |
5389f6fa | 3964 | raw_spin_unlock(&cpu_buffer->reader_lock); |
8d707e8e | 3965 | local_irq_restore(flags); |
f83c9d0f | 3966 | |
554f786e SR |
3967 | out: |
3968 | preempt_enable(); | |
3969 | ||
1b959e18 | 3970 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
2d622719 | 3971 | goto again; |
2d622719 | 3972 | |
7a8e76a3 SR |
3973 | return event; |
3974 | } | |
c4f50183 | 3975 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
7a8e76a3 SR |
3976 | |
3977 | /** | |
72c9ddfd | 3978 | * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer |
7a8e76a3 SR |
3979 | * @buffer: The ring buffer to read from |
3980 | * @cpu: The cpu buffer to iterate over | |
3981 | * | |
72c9ddfd DM |
3982 | * This performs the initial preparations necessary to iterate |
3983 | * through the buffer. Memory is allocated, buffer recording | |
3984 | * is disabled, and the iterator pointer is returned to the caller. | |
7a8e76a3 | 3985 | * |
72c9ddfd DM |
3986 | * Disabling buffer recordng prevents the reading from being |
3987 | * corrupted. This is not a consuming read, so a producer is not | |
3988 | * expected. | |
3989 | * | |
3990 | * After a sequence of ring_buffer_read_prepare calls, the user is | |
d611851b | 3991 | * expected to make at least one call to ring_buffer_read_prepare_sync. |
72c9ddfd DM |
3992 | * Afterwards, ring_buffer_read_start is invoked to get things going |
3993 | * for real. | |
3994 | * | |
d611851b | 3995 | * This overall must be paired with ring_buffer_read_finish. |
7a8e76a3 SR |
3996 | */ |
3997 | struct ring_buffer_iter * | |
72c9ddfd | 3998 | ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu) |
7a8e76a3 SR |
3999 | { |
4000 | struct ring_buffer_per_cpu *cpu_buffer; | |
8aabee57 | 4001 | struct ring_buffer_iter *iter; |
7a8e76a3 | 4002 | |
9e01c1b7 | 4003 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 4004 | return NULL; |
7a8e76a3 SR |
4005 | |
4006 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); | |
4007 | if (!iter) | |
8aabee57 | 4008 | return NULL; |
7a8e76a3 SR |
4009 | |
4010 | cpu_buffer = buffer->buffers[cpu]; | |
4011 | ||
4012 | iter->cpu_buffer = cpu_buffer; | |
4013 | ||
83f40318 | 4014 | atomic_inc(&buffer->resize_disabled); |
7a8e76a3 | 4015 | atomic_inc(&cpu_buffer->record_disabled); |
72c9ddfd DM |
4016 | |
4017 | return iter; | |
4018 | } | |
4019 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare); | |
4020 | ||
4021 | /** | |
4022 | * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls | |
4023 | * | |
4024 | * All previously invoked ring_buffer_read_prepare calls to prepare | |
4025 | * iterators will be synchronized. Afterwards, read_buffer_read_start | |
4026 | * calls on those iterators are allowed. | |
4027 | */ | |
4028 | void | |
4029 | ring_buffer_read_prepare_sync(void) | |
4030 | { | |
7a8e76a3 | 4031 | synchronize_sched(); |
72c9ddfd DM |
4032 | } |
4033 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync); | |
4034 | ||
4035 | /** | |
4036 | * ring_buffer_read_start - start a non consuming read of the buffer | |
4037 | * @iter: The iterator returned by ring_buffer_read_prepare | |
4038 | * | |
4039 | * This finalizes the startup of an iteration through the buffer. | |
4040 | * The iterator comes from a call to ring_buffer_read_prepare and | |
4041 | * an intervening ring_buffer_read_prepare_sync must have been | |
4042 | * performed. | |
4043 | * | |
d611851b | 4044 | * Must be paired with ring_buffer_read_finish. |
72c9ddfd DM |
4045 | */ |
4046 | void | |
4047 | ring_buffer_read_start(struct ring_buffer_iter *iter) | |
4048 | { | |
4049 | struct ring_buffer_per_cpu *cpu_buffer; | |
4050 | unsigned long flags; | |
4051 | ||
4052 | if (!iter) | |
4053 | return; | |
4054 | ||
4055 | cpu_buffer = iter->cpu_buffer; | |
7a8e76a3 | 4056 | |
5389f6fa | 4057 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
0199c4e6 | 4058 | arch_spin_lock(&cpu_buffer->lock); |
642edba5 | 4059 | rb_iter_reset(iter); |
0199c4e6 | 4060 | arch_spin_unlock(&cpu_buffer->lock); |
5389f6fa | 4061 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 | 4062 | } |
c4f50183 | 4063 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
7a8e76a3 SR |
4064 | |
4065 | /** | |
d611851b | 4066 | * ring_buffer_read_finish - finish reading the iterator of the buffer |
7a8e76a3 SR |
4067 | * @iter: The iterator retrieved by ring_buffer_start |
4068 | * | |
4069 | * This re-enables the recording to the buffer, and frees the | |
4070 | * iterator. | |
4071 | */ | |
4072 | void | |
4073 | ring_buffer_read_finish(struct ring_buffer_iter *iter) | |
4074 | { | |
4075 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | |
9366c1ba | 4076 | unsigned long flags; |
7a8e76a3 | 4077 | |
659f451f SR |
4078 | /* |
4079 | * Ring buffer is disabled from recording, here's a good place | |
9366c1ba SR |
4080 | * to check the integrity of the ring buffer. |
4081 | * Must prevent readers from trying to read, as the check | |
4082 | * clears the HEAD page and readers require it. | |
659f451f | 4083 | */ |
9366c1ba | 4084 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
659f451f | 4085 | rb_check_pages(cpu_buffer); |
9366c1ba | 4086 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
659f451f | 4087 | |
7a8e76a3 | 4088 | atomic_dec(&cpu_buffer->record_disabled); |
83f40318 | 4089 | atomic_dec(&cpu_buffer->buffer->resize_disabled); |
7a8e76a3 SR |
4090 | kfree(iter); |
4091 | } | |
c4f50183 | 4092 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
7a8e76a3 SR |
4093 | |
4094 | /** | |
4095 | * ring_buffer_read - read the next item in the ring buffer by the iterator | |
4096 | * @iter: The ring buffer iterator | |
4097 | * @ts: The time stamp of the event read. | |
4098 | * | |
4099 | * This reads the next event in the ring buffer and increments the iterator. | |
4100 | */ | |
4101 | struct ring_buffer_event * | |
4102 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) | |
4103 | { | |
4104 | struct ring_buffer_event *event; | |
f83c9d0f SR |
4105 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
4106 | unsigned long flags; | |
7a8e76a3 | 4107 | |
5389f6fa | 4108 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
7e9391cf | 4109 | again: |
f83c9d0f | 4110 | event = rb_iter_peek(iter, ts); |
7a8e76a3 | 4111 | if (!event) |
f83c9d0f | 4112 | goto out; |
7a8e76a3 | 4113 | |
7e9391cf SR |
4114 | if (event->type_len == RINGBUF_TYPE_PADDING) |
4115 | goto again; | |
4116 | ||
7a8e76a3 | 4117 | rb_advance_iter(iter); |
f83c9d0f | 4118 | out: |
5389f6fa | 4119 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
7a8e76a3 SR |
4120 | |
4121 | return event; | |
4122 | } | |
c4f50183 | 4123 | EXPORT_SYMBOL_GPL(ring_buffer_read); |
7a8e76a3 SR |
4124 | |
4125 | /** | |
4126 | * ring_buffer_size - return the size of the ring buffer (in bytes) | |
4127 | * @buffer: The ring buffer. | |
4128 | */ | |
438ced17 | 4129 | unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu) |
7a8e76a3 | 4130 | { |
438ced17 VN |
4131 | /* |
4132 | * Earlier, this method returned | |
4133 | * BUF_PAGE_SIZE * buffer->nr_pages | |
4134 | * Since the nr_pages field is now removed, we have converted this to | |
4135 | * return the per cpu buffer value. | |
4136 | */ | |
4137 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
4138 | return 0; | |
4139 | ||
4140 | return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages; | |
7a8e76a3 | 4141 | } |
c4f50183 | 4142 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
7a8e76a3 SR |
4143 | |
4144 | static void | |
4145 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) | |
4146 | { | |
77ae365e SR |
4147 | rb_head_page_deactivate(cpu_buffer); |
4148 | ||
7a8e76a3 | 4149 | cpu_buffer->head_page |
3adc54fa | 4150 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
bf41a158 | 4151 | local_set(&cpu_buffer->head_page->write, 0); |
778c55d4 | 4152 | local_set(&cpu_buffer->head_page->entries, 0); |
abc9b56d | 4153 | local_set(&cpu_buffer->head_page->page->commit, 0); |
d769041f | 4154 | |
6f807acd | 4155 | cpu_buffer->head_page->read = 0; |
bf41a158 SR |
4156 | |
4157 | cpu_buffer->tail_page = cpu_buffer->head_page; | |
4158 | cpu_buffer->commit_page = cpu_buffer->head_page; | |
4159 | ||
4160 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); | |
5040b4b7 | 4161 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
bf41a158 | 4162 | local_set(&cpu_buffer->reader_page->write, 0); |
778c55d4 | 4163 | local_set(&cpu_buffer->reader_page->entries, 0); |
abc9b56d | 4164 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
6f807acd | 4165 | cpu_buffer->reader_page->read = 0; |
7a8e76a3 | 4166 | |
c64e148a | 4167 | local_set(&cpu_buffer->entries_bytes, 0); |
77ae365e | 4168 | local_set(&cpu_buffer->overrun, 0); |
884bfe89 SP |
4169 | local_set(&cpu_buffer->commit_overrun, 0); |
4170 | local_set(&cpu_buffer->dropped_events, 0); | |
e4906eff | 4171 | local_set(&cpu_buffer->entries, 0); |
fa743953 SR |
4172 | local_set(&cpu_buffer->committing, 0); |
4173 | local_set(&cpu_buffer->commits, 0); | |
77ae365e | 4174 | cpu_buffer->read = 0; |
c64e148a | 4175 | cpu_buffer->read_bytes = 0; |
69507c06 SR |
4176 | |
4177 | cpu_buffer->write_stamp = 0; | |
4178 | cpu_buffer->read_stamp = 0; | |
77ae365e | 4179 | |
66a8cb95 SR |
4180 | cpu_buffer->lost_events = 0; |
4181 | cpu_buffer->last_overrun = 0; | |
4182 | ||
77ae365e | 4183 | rb_head_page_activate(cpu_buffer); |
7a8e76a3 SR |
4184 | } |
4185 | ||
4186 | /** | |
4187 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer | |
4188 | * @buffer: The ring buffer to reset a per cpu buffer of | |
4189 | * @cpu: The CPU buffer to be reset | |
4190 | */ | |
4191 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) | |
4192 | { | |
4193 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
4194 | unsigned long flags; | |
4195 | ||
9e01c1b7 | 4196 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 4197 | return; |
7a8e76a3 | 4198 | |
83f40318 | 4199 | atomic_inc(&buffer->resize_disabled); |
41ede23e SR |
4200 | atomic_inc(&cpu_buffer->record_disabled); |
4201 | ||
83f40318 VN |
4202 | /* Make sure all commits have finished */ |
4203 | synchronize_sched(); | |
4204 | ||
5389f6fa | 4205 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
f83c9d0f | 4206 | |
41b6a95d SR |
4207 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) |
4208 | goto out; | |
4209 | ||
0199c4e6 | 4210 | arch_spin_lock(&cpu_buffer->lock); |
7a8e76a3 SR |
4211 | |
4212 | rb_reset_cpu(cpu_buffer); | |
4213 | ||
0199c4e6 | 4214 | arch_spin_unlock(&cpu_buffer->lock); |
f83c9d0f | 4215 | |
41b6a95d | 4216 | out: |
5389f6fa | 4217 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
41ede23e SR |
4218 | |
4219 | atomic_dec(&cpu_buffer->record_disabled); | |
83f40318 | 4220 | atomic_dec(&buffer->resize_disabled); |
7a8e76a3 | 4221 | } |
c4f50183 | 4222 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
7a8e76a3 SR |
4223 | |
4224 | /** | |
4225 | * ring_buffer_reset - reset a ring buffer | |
4226 | * @buffer: The ring buffer to reset all cpu buffers | |
4227 | */ | |
4228 | void ring_buffer_reset(struct ring_buffer *buffer) | |
4229 | { | |
7a8e76a3 SR |
4230 | int cpu; |
4231 | ||
7a8e76a3 | 4232 | for_each_buffer_cpu(buffer, cpu) |
d769041f | 4233 | ring_buffer_reset_cpu(buffer, cpu); |
7a8e76a3 | 4234 | } |
c4f50183 | 4235 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
7a8e76a3 SR |
4236 | |
4237 | /** | |
4238 | * rind_buffer_empty - is the ring buffer empty? | |
4239 | * @buffer: The ring buffer to test | |
4240 | */ | |
4241 | int ring_buffer_empty(struct ring_buffer *buffer) | |
4242 | { | |
4243 | struct ring_buffer_per_cpu *cpu_buffer; | |
d4788207 | 4244 | unsigned long flags; |
8d707e8e | 4245 | int dolock; |
7a8e76a3 | 4246 | int cpu; |
d4788207 | 4247 | int ret; |
7a8e76a3 | 4248 | |
8d707e8e | 4249 | dolock = rb_ok_to_lock(); |
7a8e76a3 SR |
4250 | |
4251 | /* yes this is racy, but if you don't like the race, lock the buffer */ | |
4252 | for_each_buffer_cpu(buffer, cpu) { | |
4253 | cpu_buffer = buffer->buffers[cpu]; | |
8d707e8e SR |
4254 | local_irq_save(flags); |
4255 | if (dolock) | |
5389f6fa | 4256 | raw_spin_lock(&cpu_buffer->reader_lock); |
d4788207 | 4257 | ret = rb_per_cpu_empty(cpu_buffer); |
8d707e8e | 4258 | if (dolock) |
5389f6fa | 4259 | raw_spin_unlock(&cpu_buffer->reader_lock); |
8d707e8e SR |
4260 | local_irq_restore(flags); |
4261 | ||
d4788207 | 4262 | if (!ret) |
7a8e76a3 SR |
4263 | return 0; |
4264 | } | |
554f786e | 4265 | |
7a8e76a3 SR |
4266 | return 1; |
4267 | } | |
c4f50183 | 4268 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
7a8e76a3 SR |
4269 | |
4270 | /** | |
4271 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? | |
4272 | * @buffer: The ring buffer | |
4273 | * @cpu: The CPU buffer to test | |
4274 | */ | |
4275 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) | |
4276 | { | |
4277 | struct ring_buffer_per_cpu *cpu_buffer; | |
d4788207 | 4278 | unsigned long flags; |
8d707e8e | 4279 | int dolock; |
8aabee57 | 4280 | int ret; |
7a8e76a3 | 4281 | |
9e01c1b7 | 4282 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
8aabee57 | 4283 | return 1; |
7a8e76a3 | 4284 | |
8d707e8e SR |
4285 | dolock = rb_ok_to_lock(); |
4286 | ||
7a8e76a3 | 4287 | cpu_buffer = buffer->buffers[cpu]; |
8d707e8e SR |
4288 | local_irq_save(flags); |
4289 | if (dolock) | |
5389f6fa | 4290 | raw_spin_lock(&cpu_buffer->reader_lock); |
554f786e | 4291 | ret = rb_per_cpu_empty(cpu_buffer); |
8d707e8e | 4292 | if (dolock) |
5389f6fa | 4293 | raw_spin_unlock(&cpu_buffer->reader_lock); |
8d707e8e | 4294 | local_irq_restore(flags); |
554f786e SR |
4295 | |
4296 | return ret; | |
7a8e76a3 | 4297 | } |
c4f50183 | 4298 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
7a8e76a3 | 4299 | |
85bac32c | 4300 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
7a8e76a3 SR |
4301 | /** |
4302 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers | |
4303 | * @buffer_a: One buffer to swap with | |
4304 | * @buffer_b: The other buffer to swap with | |
4305 | * | |
4306 | * This function is useful for tracers that want to take a "snapshot" | |
4307 | * of a CPU buffer and has another back up buffer lying around. | |
4308 | * it is expected that the tracer handles the cpu buffer not being | |
4309 | * used at the moment. | |
4310 | */ | |
4311 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, | |
4312 | struct ring_buffer *buffer_b, int cpu) | |
4313 | { | |
4314 | struct ring_buffer_per_cpu *cpu_buffer_a; | |
4315 | struct ring_buffer_per_cpu *cpu_buffer_b; | |
554f786e SR |
4316 | int ret = -EINVAL; |
4317 | ||
9e01c1b7 RR |
4318 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
4319 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) | |
554f786e | 4320 | goto out; |
7a8e76a3 | 4321 | |
438ced17 VN |
4322 | cpu_buffer_a = buffer_a->buffers[cpu]; |
4323 | cpu_buffer_b = buffer_b->buffers[cpu]; | |
4324 | ||
7a8e76a3 | 4325 | /* At least make sure the two buffers are somewhat the same */ |
438ced17 | 4326 | if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) |
554f786e SR |
4327 | goto out; |
4328 | ||
4329 | ret = -EAGAIN; | |
7a8e76a3 | 4330 | |
97b17efe | 4331 | if (ring_buffer_flags != RB_BUFFERS_ON) |
554f786e | 4332 | goto out; |
97b17efe SR |
4333 | |
4334 | if (atomic_read(&buffer_a->record_disabled)) | |
554f786e | 4335 | goto out; |
97b17efe SR |
4336 | |
4337 | if (atomic_read(&buffer_b->record_disabled)) | |
554f786e | 4338 | goto out; |
97b17efe | 4339 | |
97b17efe | 4340 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
554f786e | 4341 | goto out; |
97b17efe SR |
4342 | |
4343 | if (atomic_read(&cpu_buffer_b->record_disabled)) | |
554f786e | 4344 | goto out; |
97b17efe | 4345 | |
7a8e76a3 SR |
4346 | /* |
4347 | * We can't do a synchronize_sched here because this | |
4348 | * function can be called in atomic context. | |
4349 | * Normally this will be called from the same CPU as cpu. | |
4350 | * If not it's up to the caller to protect this. | |
4351 | */ | |
4352 | atomic_inc(&cpu_buffer_a->record_disabled); | |
4353 | atomic_inc(&cpu_buffer_b->record_disabled); | |
4354 | ||
98277991 SR |
4355 | ret = -EBUSY; |
4356 | if (local_read(&cpu_buffer_a->committing)) | |
4357 | goto out_dec; | |
4358 | if (local_read(&cpu_buffer_b->committing)) | |
4359 | goto out_dec; | |
4360 | ||
7a8e76a3 SR |
4361 | buffer_a->buffers[cpu] = cpu_buffer_b; |
4362 | buffer_b->buffers[cpu] = cpu_buffer_a; | |
4363 | ||
4364 | cpu_buffer_b->buffer = buffer_a; | |
4365 | cpu_buffer_a->buffer = buffer_b; | |
4366 | ||
98277991 SR |
4367 | ret = 0; |
4368 | ||
4369 | out_dec: | |
7a8e76a3 SR |
4370 | atomic_dec(&cpu_buffer_a->record_disabled); |
4371 | atomic_dec(&cpu_buffer_b->record_disabled); | |
554f786e | 4372 | out: |
554f786e | 4373 | return ret; |
7a8e76a3 | 4374 | } |
c4f50183 | 4375 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
85bac32c | 4376 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ |
7a8e76a3 | 4377 | |
8789a9e7 SR |
4378 | /** |
4379 | * ring_buffer_alloc_read_page - allocate a page to read from buffer | |
4380 | * @buffer: the buffer to allocate for. | |
d611851b | 4381 | * @cpu: the cpu buffer to allocate. |
8789a9e7 SR |
4382 | * |
4383 | * This function is used in conjunction with ring_buffer_read_page. | |
4384 | * When reading a full page from the ring buffer, these functions | |
4385 | * can be used to speed up the process. The calling function should | |
4386 | * allocate a few pages first with this function. Then when it | |
4387 | * needs to get pages from the ring buffer, it passes the result | |
4388 | * of this function into ring_buffer_read_page, which will swap | |
4389 | * the page that was allocated, with the read page of the buffer. | |
4390 | * | |
4391 | * Returns: | |
4392 | * The page allocated, or NULL on error. | |
4393 | */ | |
7ea59064 | 4394 | void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu) |
8789a9e7 | 4395 | { |
044fa782 | 4396 | struct buffer_data_page *bpage; |
7ea59064 | 4397 | struct page *page; |
8789a9e7 | 4398 | |
d7ec4bfe VN |
4399 | page = alloc_pages_node(cpu_to_node(cpu), |
4400 | GFP_KERNEL | __GFP_NORETRY, 0); | |
7ea59064 | 4401 | if (!page) |
8789a9e7 SR |
4402 | return NULL; |
4403 | ||
7ea59064 | 4404 | bpage = page_address(page); |
8789a9e7 | 4405 | |
ef7a4a16 SR |
4406 | rb_init_page(bpage); |
4407 | ||
044fa782 | 4408 | return bpage; |
8789a9e7 | 4409 | } |
d6ce96da | 4410 | EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); |
8789a9e7 SR |
4411 | |
4412 | /** | |
4413 | * ring_buffer_free_read_page - free an allocated read page | |
4414 | * @buffer: the buffer the page was allocate for | |
4415 | * @data: the page to free | |
4416 | * | |
4417 | * Free a page allocated from ring_buffer_alloc_read_page. | |
4418 | */ | |
4419 | void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data) | |
4420 | { | |
4421 | free_page((unsigned long)data); | |
4422 | } | |
d6ce96da | 4423 | EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); |
8789a9e7 SR |
4424 | |
4425 | /** | |
4426 | * ring_buffer_read_page - extract a page from the ring buffer | |
4427 | * @buffer: buffer to extract from | |
4428 | * @data_page: the page to use allocated from ring_buffer_alloc_read_page | |
ef7a4a16 | 4429 | * @len: amount to extract |
8789a9e7 SR |
4430 | * @cpu: the cpu of the buffer to extract |
4431 | * @full: should the extraction only happen when the page is full. | |
4432 | * | |
4433 | * This function will pull out a page from the ring buffer and consume it. | |
4434 | * @data_page must be the address of the variable that was returned | |
4435 | * from ring_buffer_alloc_read_page. This is because the page might be used | |
4436 | * to swap with a page in the ring buffer. | |
4437 | * | |
4438 | * for example: | |
d611851b | 4439 | * rpage = ring_buffer_alloc_read_page(buffer, cpu); |
8789a9e7 SR |
4440 | * if (!rpage) |
4441 | * return error; | |
ef7a4a16 | 4442 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
667d2412 LJ |
4443 | * if (ret >= 0) |
4444 | * process_page(rpage, ret); | |
8789a9e7 SR |
4445 | * |
4446 | * When @full is set, the function will not return true unless | |
4447 | * the writer is off the reader page. | |
4448 | * | |
4449 | * Note: it is up to the calling functions to handle sleeps and wakeups. | |
4450 | * The ring buffer can be used anywhere in the kernel and can not | |
4451 | * blindly call wake_up. The layer that uses the ring buffer must be | |
4452 | * responsible for that. | |
4453 | * | |
4454 | * Returns: | |
667d2412 LJ |
4455 | * >=0 if data has been transferred, returns the offset of consumed data. |
4456 | * <0 if no data has been transferred. | |
8789a9e7 SR |
4457 | */ |
4458 | int ring_buffer_read_page(struct ring_buffer *buffer, | |
ef7a4a16 | 4459 | void **data_page, size_t len, int cpu, int full) |
8789a9e7 SR |
4460 | { |
4461 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; | |
4462 | struct ring_buffer_event *event; | |
044fa782 | 4463 | struct buffer_data_page *bpage; |
ef7a4a16 | 4464 | struct buffer_page *reader; |
ff0ff84a | 4465 | unsigned long missed_events; |
8789a9e7 | 4466 | unsigned long flags; |
ef7a4a16 | 4467 | unsigned int commit; |
667d2412 | 4468 | unsigned int read; |
4f3640f8 | 4469 | u64 save_timestamp; |
667d2412 | 4470 | int ret = -1; |
8789a9e7 | 4471 | |
554f786e SR |
4472 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
4473 | goto out; | |
4474 | ||
474d32b6 SR |
4475 | /* |
4476 | * If len is not big enough to hold the page header, then | |
4477 | * we can not copy anything. | |
4478 | */ | |
4479 | if (len <= BUF_PAGE_HDR_SIZE) | |
554f786e | 4480 | goto out; |
474d32b6 SR |
4481 | |
4482 | len -= BUF_PAGE_HDR_SIZE; | |
4483 | ||
8789a9e7 | 4484 | if (!data_page) |
554f786e | 4485 | goto out; |
8789a9e7 | 4486 | |
044fa782 SR |
4487 | bpage = *data_page; |
4488 | if (!bpage) | |
554f786e | 4489 | goto out; |
8789a9e7 | 4490 | |
5389f6fa | 4491 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
8789a9e7 | 4492 | |
ef7a4a16 SR |
4493 | reader = rb_get_reader_page(cpu_buffer); |
4494 | if (!reader) | |
554f786e | 4495 | goto out_unlock; |
8789a9e7 | 4496 | |
ef7a4a16 SR |
4497 | event = rb_reader_event(cpu_buffer); |
4498 | ||
4499 | read = reader->read; | |
4500 | commit = rb_page_commit(reader); | |
667d2412 | 4501 | |
66a8cb95 | 4502 | /* Check if any events were dropped */ |
ff0ff84a | 4503 | missed_events = cpu_buffer->lost_events; |
66a8cb95 | 4504 | |
8789a9e7 | 4505 | /* |
474d32b6 SR |
4506 | * If this page has been partially read or |
4507 | * if len is not big enough to read the rest of the page or | |
4508 | * a writer is still on the page, then | |
4509 | * we must copy the data from the page to the buffer. | |
4510 | * Otherwise, we can simply swap the page with the one passed in. | |
8789a9e7 | 4511 | */ |
474d32b6 | 4512 | if (read || (len < (commit - read)) || |
ef7a4a16 | 4513 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
667d2412 | 4514 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
474d32b6 SR |
4515 | unsigned int rpos = read; |
4516 | unsigned int pos = 0; | |
ef7a4a16 | 4517 | unsigned int size; |
8789a9e7 SR |
4518 | |
4519 | if (full) | |
554f786e | 4520 | goto out_unlock; |
8789a9e7 | 4521 | |
ef7a4a16 SR |
4522 | if (len > (commit - read)) |
4523 | len = (commit - read); | |
4524 | ||
69d1b839 SR |
4525 | /* Always keep the time extend and data together */ |
4526 | size = rb_event_ts_length(event); | |
ef7a4a16 SR |
4527 | |
4528 | if (len < size) | |
554f786e | 4529 | goto out_unlock; |
ef7a4a16 | 4530 | |
4f3640f8 SR |
4531 | /* save the current timestamp, since the user will need it */ |
4532 | save_timestamp = cpu_buffer->read_stamp; | |
4533 | ||
ef7a4a16 SR |
4534 | /* Need to copy one event at a time */ |
4535 | do { | |
e1e35927 DS |
4536 | /* We need the size of one event, because |
4537 | * rb_advance_reader only advances by one event, | |
4538 | * whereas rb_event_ts_length may include the size of | |
4539 | * one or two events. | |
4540 | * We have already ensured there's enough space if this | |
4541 | * is a time extend. */ | |
4542 | size = rb_event_length(event); | |
474d32b6 | 4543 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
ef7a4a16 SR |
4544 | |
4545 | len -= size; | |
4546 | ||
4547 | rb_advance_reader(cpu_buffer); | |
474d32b6 SR |
4548 | rpos = reader->read; |
4549 | pos += size; | |
ef7a4a16 | 4550 | |
18fab912 HY |
4551 | if (rpos >= commit) |
4552 | break; | |
4553 | ||
ef7a4a16 | 4554 | event = rb_reader_event(cpu_buffer); |
69d1b839 SR |
4555 | /* Always keep the time extend and data together */ |
4556 | size = rb_event_ts_length(event); | |
e1e35927 | 4557 | } while (len >= size); |
667d2412 LJ |
4558 | |
4559 | /* update bpage */ | |
ef7a4a16 | 4560 | local_set(&bpage->commit, pos); |
4f3640f8 | 4561 | bpage->time_stamp = save_timestamp; |
ef7a4a16 | 4562 | |
474d32b6 SR |
4563 | /* we copied everything to the beginning */ |
4564 | read = 0; | |
8789a9e7 | 4565 | } else { |
afbab76a | 4566 | /* update the entry counter */ |
77ae365e | 4567 | cpu_buffer->read += rb_page_entries(reader); |
c64e148a | 4568 | cpu_buffer->read_bytes += BUF_PAGE_SIZE; |
afbab76a | 4569 | |
8789a9e7 | 4570 | /* swap the pages */ |
044fa782 | 4571 | rb_init_page(bpage); |
ef7a4a16 SR |
4572 | bpage = reader->page; |
4573 | reader->page = *data_page; | |
4574 | local_set(&reader->write, 0); | |
778c55d4 | 4575 | local_set(&reader->entries, 0); |
ef7a4a16 | 4576 | reader->read = 0; |
044fa782 | 4577 | *data_page = bpage; |
ff0ff84a SR |
4578 | |
4579 | /* | |
4580 | * Use the real_end for the data size, | |
4581 | * This gives us a chance to store the lost events | |
4582 | * on the page. | |
4583 | */ | |
4584 | if (reader->real_end) | |
4585 | local_set(&bpage->commit, reader->real_end); | |
8789a9e7 | 4586 | } |
667d2412 | 4587 | ret = read; |
8789a9e7 | 4588 | |
66a8cb95 | 4589 | cpu_buffer->lost_events = 0; |
2711ca23 SR |
4590 | |
4591 | commit = local_read(&bpage->commit); | |
66a8cb95 SR |
4592 | /* |
4593 | * Set a flag in the commit field if we lost events | |
4594 | */ | |
ff0ff84a | 4595 | if (missed_events) { |
ff0ff84a SR |
4596 | /* If there is room at the end of the page to save the |
4597 | * missed events, then record it there. | |
4598 | */ | |
4599 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { | |
4600 | memcpy(&bpage->data[commit], &missed_events, | |
4601 | sizeof(missed_events)); | |
4602 | local_add(RB_MISSED_STORED, &bpage->commit); | |
2711ca23 | 4603 | commit += sizeof(missed_events); |
ff0ff84a | 4604 | } |
66a8cb95 | 4605 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
ff0ff84a | 4606 | } |
66a8cb95 | 4607 | |
2711ca23 SR |
4608 | /* |
4609 | * This page may be off to user land. Zero it out here. | |
4610 | */ | |
4611 | if (commit < BUF_PAGE_SIZE) | |
4612 | memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit); | |
4613 | ||
554f786e | 4614 | out_unlock: |
5389f6fa | 4615 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
8789a9e7 | 4616 | |
554f786e | 4617 | out: |
8789a9e7 SR |
4618 | return ret; |
4619 | } | |
d6ce96da | 4620 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
8789a9e7 | 4621 | |
59222efe | 4622 | #ifdef CONFIG_HOTPLUG_CPU |
09c9e84d FW |
4623 | static int rb_cpu_notify(struct notifier_block *self, |
4624 | unsigned long action, void *hcpu) | |
554f786e SR |
4625 | { |
4626 | struct ring_buffer *buffer = | |
4627 | container_of(self, struct ring_buffer, cpu_notify); | |
4628 | long cpu = (long)hcpu; | |
438ced17 VN |
4629 | int cpu_i, nr_pages_same; |
4630 | unsigned int nr_pages; | |
554f786e SR |
4631 | |
4632 | switch (action) { | |
4633 | case CPU_UP_PREPARE: | |
4634 | case CPU_UP_PREPARE_FROZEN: | |
3f237a79 | 4635 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
554f786e SR |
4636 | return NOTIFY_OK; |
4637 | ||
438ced17 VN |
4638 | nr_pages = 0; |
4639 | nr_pages_same = 1; | |
4640 | /* check if all cpu sizes are same */ | |
4641 | for_each_buffer_cpu(buffer, cpu_i) { | |
4642 | /* fill in the size from first enabled cpu */ | |
4643 | if (nr_pages == 0) | |
4644 | nr_pages = buffer->buffers[cpu_i]->nr_pages; | |
4645 | if (nr_pages != buffer->buffers[cpu_i]->nr_pages) { | |
4646 | nr_pages_same = 0; | |
4647 | break; | |
4648 | } | |
4649 | } | |
4650 | /* allocate minimum pages, user can later expand it */ | |
4651 | if (!nr_pages_same) | |
4652 | nr_pages = 2; | |
554f786e | 4653 | buffer->buffers[cpu] = |
438ced17 | 4654 | rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
554f786e SR |
4655 | if (!buffer->buffers[cpu]) { |
4656 | WARN(1, "failed to allocate ring buffer on CPU %ld\n", | |
4657 | cpu); | |
4658 | return NOTIFY_OK; | |
4659 | } | |
4660 | smp_wmb(); | |
3f237a79 | 4661 | cpumask_set_cpu(cpu, buffer->cpumask); |
554f786e SR |
4662 | break; |
4663 | case CPU_DOWN_PREPARE: | |
4664 | case CPU_DOWN_PREPARE_FROZEN: | |
4665 | /* | |
4666 | * Do nothing. | |
4667 | * If we were to free the buffer, then the user would | |
4668 | * lose any trace that was in the buffer. | |
4669 | */ | |
4670 | break; | |
4671 | default: | |
4672 | break; | |
4673 | } | |
4674 | return NOTIFY_OK; | |
4675 | } | |
4676 | #endif | |
6c43e554 SRRH |
4677 | |
4678 | #ifdef CONFIG_RING_BUFFER_STARTUP_TEST | |
4679 | /* | |
4680 | * This is a basic integrity check of the ring buffer. | |
4681 | * Late in the boot cycle this test will run when configured in. | |
4682 | * It will kick off a thread per CPU that will go into a loop | |
4683 | * writing to the per cpu ring buffer various sizes of data. | |
4684 | * Some of the data will be large items, some small. | |
4685 | * | |
4686 | * Another thread is created that goes into a spin, sending out | |
4687 | * IPIs to the other CPUs to also write into the ring buffer. | |
4688 | * this is to test the nesting ability of the buffer. | |
4689 | * | |
4690 | * Basic stats are recorded and reported. If something in the | |
4691 | * ring buffer should happen that's not expected, a big warning | |
4692 | * is displayed and all ring buffers are disabled. | |
4693 | */ | |
4694 | static struct task_struct *rb_threads[NR_CPUS] __initdata; | |
4695 | ||
4696 | struct rb_test_data { | |
4697 | struct ring_buffer *buffer; | |
4698 | unsigned long events; | |
4699 | unsigned long bytes_written; | |
4700 | unsigned long bytes_alloc; | |
4701 | unsigned long bytes_dropped; | |
4702 | unsigned long events_nested; | |
4703 | unsigned long bytes_written_nested; | |
4704 | unsigned long bytes_alloc_nested; | |
4705 | unsigned long bytes_dropped_nested; | |
4706 | int min_size_nested; | |
4707 | int max_size_nested; | |
4708 | int max_size; | |
4709 | int min_size; | |
4710 | int cpu; | |
4711 | int cnt; | |
4712 | }; | |
4713 | ||
4714 | static struct rb_test_data rb_data[NR_CPUS] __initdata; | |
4715 | ||
4716 | /* 1 meg per cpu */ | |
4717 | #define RB_TEST_BUFFER_SIZE 1048576 | |
4718 | ||
4719 | static char rb_string[] __initdata = | |
4720 | "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\" | |
4721 | "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890" | |
4722 | "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv"; | |
4723 | ||
4724 | static bool rb_test_started __initdata; | |
4725 | ||
4726 | struct rb_item { | |
4727 | int size; | |
4728 | char str[]; | |
4729 | }; | |
4730 | ||
4731 | static __init int rb_write_something(struct rb_test_data *data, bool nested) | |
4732 | { | |
4733 | struct ring_buffer_event *event; | |
4734 | struct rb_item *item; | |
4735 | bool started; | |
4736 | int event_len; | |
4737 | int size; | |
4738 | int len; | |
4739 | int cnt; | |
4740 | ||
4741 | /* Have nested writes different that what is written */ | |
4742 | cnt = data->cnt + (nested ? 27 : 0); | |
4743 | ||
4744 | /* Multiply cnt by ~e, to make some unique increment */ | |
4745 | size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1); | |
4746 | ||
4747 | len = size + sizeof(struct rb_item); | |
4748 | ||
4749 | started = rb_test_started; | |
4750 | /* read rb_test_started before checking buffer enabled */ | |
4751 | smp_rmb(); | |
4752 | ||
4753 | event = ring_buffer_lock_reserve(data->buffer, len); | |
4754 | if (!event) { | |
4755 | /* Ignore dropped events before test starts. */ | |
4756 | if (started) { | |
4757 | if (nested) | |
4758 | data->bytes_dropped += len; | |
4759 | else | |
4760 | data->bytes_dropped_nested += len; | |
4761 | } | |
4762 | return len; | |
4763 | } | |
4764 | ||
4765 | event_len = ring_buffer_event_length(event); | |
4766 | ||
4767 | if (RB_WARN_ON(data->buffer, event_len < len)) | |
4768 | goto out; | |
4769 | ||
4770 | item = ring_buffer_event_data(event); | |
4771 | item->size = size; | |
4772 | memcpy(item->str, rb_string, size); | |
4773 | ||
4774 | if (nested) { | |
4775 | data->bytes_alloc_nested += event_len; | |
4776 | data->bytes_written_nested += len; | |
4777 | data->events_nested++; | |
4778 | if (!data->min_size_nested || len < data->min_size_nested) | |
4779 | data->min_size_nested = len; | |
4780 | if (len > data->max_size_nested) | |
4781 | data->max_size_nested = len; | |
4782 | } else { | |
4783 | data->bytes_alloc += event_len; | |
4784 | data->bytes_written += len; | |
4785 | data->events++; | |
4786 | if (!data->min_size || len < data->min_size) | |
4787 | data->max_size = len; | |
4788 | if (len > data->max_size) | |
4789 | data->max_size = len; | |
4790 | } | |
4791 | ||
4792 | out: | |
4793 | ring_buffer_unlock_commit(data->buffer, event); | |
4794 | ||
4795 | return 0; | |
4796 | } | |
4797 | ||
4798 | static __init int rb_test(void *arg) | |
4799 | { | |
4800 | struct rb_test_data *data = arg; | |
4801 | ||
4802 | while (!kthread_should_stop()) { | |
4803 | rb_write_something(data, false); | |
4804 | data->cnt++; | |
4805 | ||
4806 | set_current_state(TASK_INTERRUPTIBLE); | |
4807 | /* Now sleep between a min of 100-300us and a max of 1ms */ | |
4808 | usleep_range(((data->cnt % 3) + 1) * 100, 1000); | |
4809 | } | |
4810 | ||
4811 | return 0; | |
4812 | } | |
4813 | ||
4814 | static __init void rb_ipi(void *ignore) | |
4815 | { | |
4816 | struct rb_test_data *data; | |
4817 | int cpu = smp_processor_id(); | |
4818 | ||
4819 | data = &rb_data[cpu]; | |
4820 | rb_write_something(data, true); | |
4821 | } | |
4822 | ||
4823 | static __init int rb_hammer_test(void *arg) | |
4824 | { | |
4825 | while (!kthread_should_stop()) { | |
4826 | ||
4827 | /* Send an IPI to all cpus to write data! */ | |
4828 | smp_call_function(rb_ipi, NULL, 1); | |
4829 | /* No sleep, but for non preempt, let others run */ | |
4830 | schedule(); | |
4831 | } | |
4832 | ||
4833 | return 0; | |
4834 | } | |
4835 | ||
4836 | static __init int test_ringbuffer(void) | |
4837 | { | |
4838 | struct task_struct *rb_hammer; | |
4839 | struct ring_buffer *buffer; | |
4840 | int cpu; | |
4841 | int ret = 0; | |
4842 | ||
4843 | pr_info("Running ring buffer tests...\n"); | |
4844 | ||
4845 | buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE); | |
4846 | if (WARN_ON(!buffer)) | |
4847 | return 0; | |
4848 | ||
4849 | /* Disable buffer so that threads can't write to it yet */ | |
4850 | ring_buffer_record_off(buffer); | |
4851 | ||
4852 | for_each_online_cpu(cpu) { | |
4853 | rb_data[cpu].buffer = buffer; | |
4854 | rb_data[cpu].cpu = cpu; | |
4855 | rb_data[cpu].cnt = cpu; | |
4856 | rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu], | |
4857 | "rbtester/%d", cpu); | |
4858 | if (WARN_ON(!rb_threads[cpu])) { | |
4859 | pr_cont("FAILED\n"); | |
4860 | ret = -1; | |
4861 | goto out_free; | |
4862 | } | |
4863 | ||
4864 | kthread_bind(rb_threads[cpu], cpu); | |
4865 | wake_up_process(rb_threads[cpu]); | |
4866 | } | |
4867 | ||
4868 | /* Now create the rb hammer! */ | |
4869 | rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer"); | |
4870 | if (WARN_ON(!rb_hammer)) { | |
4871 | pr_cont("FAILED\n"); | |
4872 | ret = -1; | |
4873 | goto out_free; | |
4874 | } | |
4875 | ||
4876 | ring_buffer_record_on(buffer); | |
4877 | /* | |
4878 | * Show buffer is enabled before setting rb_test_started. | |
4879 | * Yes there's a small race window where events could be | |
4880 | * dropped and the thread wont catch it. But when a ring | |
4881 | * buffer gets enabled, there will always be some kind of | |
4882 | * delay before other CPUs see it. Thus, we don't care about | |
4883 | * those dropped events. We care about events dropped after | |
4884 | * the threads see that the buffer is active. | |
4885 | */ | |
4886 | smp_wmb(); | |
4887 | rb_test_started = true; | |
4888 | ||
4889 | set_current_state(TASK_INTERRUPTIBLE); | |
4890 | /* Just run for 10 seconds */; | |
4891 | schedule_timeout(10 * HZ); | |
4892 | ||
4893 | kthread_stop(rb_hammer); | |
4894 | ||
4895 | out_free: | |
4896 | for_each_online_cpu(cpu) { | |
4897 | if (!rb_threads[cpu]) | |
4898 | break; | |
4899 | kthread_stop(rb_threads[cpu]); | |
4900 | } | |
4901 | if (ret) { | |
4902 | ring_buffer_free(buffer); | |
4903 | return ret; | |
4904 | } | |
4905 | ||
4906 | /* Report! */ | |
4907 | pr_info("finished\n"); | |
4908 | for_each_online_cpu(cpu) { | |
4909 | struct ring_buffer_event *event; | |
4910 | struct rb_test_data *data = &rb_data[cpu]; | |
4911 | struct rb_item *item; | |
4912 | unsigned long total_events; | |
4913 | unsigned long total_dropped; | |
4914 | unsigned long total_written; | |
4915 | unsigned long total_alloc; | |
4916 | unsigned long total_read = 0; | |
4917 | unsigned long total_size = 0; | |
4918 | unsigned long total_len = 0; | |
4919 | unsigned long total_lost = 0; | |
4920 | unsigned long lost; | |
4921 | int big_event_size; | |
4922 | int small_event_size; | |
4923 | ||
4924 | ret = -1; | |
4925 | ||
4926 | total_events = data->events + data->events_nested; | |
4927 | total_written = data->bytes_written + data->bytes_written_nested; | |
4928 | total_alloc = data->bytes_alloc + data->bytes_alloc_nested; | |
4929 | total_dropped = data->bytes_dropped + data->bytes_dropped_nested; | |
4930 | ||
4931 | big_event_size = data->max_size + data->max_size_nested; | |
4932 | small_event_size = data->min_size + data->min_size_nested; | |
4933 | ||
4934 | pr_info("CPU %d:\n", cpu); | |
4935 | pr_info(" events: %ld\n", total_events); | |
4936 | pr_info(" dropped bytes: %ld\n", total_dropped); | |
4937 | pr_info(" alloced bytes: %ld\n", total_alloc); | |
4938 | pr_info(" written bytes: %ld\n", total_written); | |
4939 | pr_info(" biggest event: %d\n", big_event_size); | |
4940 | pr_info(" smallest event: %d\n", small_event_size); | |
4941 | ||
4942 | if (RB_WARN_ON(buffer, total_dropped)) | |
4943 | break; | |
4944 | ||
4945 | ret = 0; | |
4946 | ||
4947 | while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) { | |
4948 | total_lost += lost; | |
4949 | item = ring_buffer_event_data(event); | |
4950 | total_len += ring_buffer_event_length(event); | |
4951 | total_size += item->size + sizeof(struct rb_item); | |
4952 | if (memcmp(&item->str[0], rb_string, item->size) != 0) { | |
4953 | pr_info("FAILED!\n"); | |
4954 | pr_info("buffer had: %.*s\n", item->size, item->str); | |
4955 | pr_info("expected: %.*s\n", item->size, rb_string); | |
4956 | RB_WARN_ON(buffer, 1); | |
4957 | ret = -1; | |
4958 | break; | |
4959 | } | |
4960 | total_read++; | |
4961 | } | |
4962 | if (ret) | |
4963 | break; | |
4964 | ||
4965 | ret = -1; | |
4966 | ||
4967 | pr_info(" read events: %ld\n", total_read); | |
4968 | pr_info(" lost events: %ld\n", total_lost); | |
4969 | pr_info(" total events: %ld\n", total_lost + total_read); | |
4970 | pr_info(" recorded len bytes: %ld\n", total_len); | |
4971 | pr_info(" recorded size bytes: %ld\n", total_size); | |
4972 | if (total_lost) | |
4973 | pr_info(" With dropped events, record len and size may not match\n" | |
4974 | " alloced and written from above\n"); | |
4975 | if (!total_lost) { | |
4976 | if (RB_WARN_ON(buffer, total_len != total_alloc || | |
4977 | total_size != total_written)) | |
4978 | break; | |
4979 | } | |
4980 | if (RB_WARN_ON(buffer, total_lost + total_read != total_events)) | |
4981 | break; | |
4982 | ||
4983 | ret = 0; | |
4984 | } | |
4985 | if (!ret) | |
4986 | pr_info("Ring buffer PASSED!\n"); | |
4987 | ||
4988 | ring_buffer_free(buffer); | |
4989 | return 0; | |
4990 | } | |
4991 | ||
4992 | late_initcall(test_ringbuffer); | |
4993 | #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */ |