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