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