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