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