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