1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Generic ring buffer 4 * 5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com> 6 */ 7 #include <linux/ring_buffer_types.h> 8 #include <linux/sched/isolation.h> 9 #include <linux/trace_recursion.h> 10 #include <linux/trace_events.h> 11 #include <linux/ring_buffer.h> 12 #include <linux/trace_clock.h> 13 #include <linux/sched/clock.h> 14 #include <linux/cacheflush.h> 15 #include <linux/trace_seq.h> 16 #include <linux/spinlock.h> 17 #include <linux/irq_work.h> 18 #include <linux/security.h> 19 #include <linux/uaccess.h> 20 #include <linux/hardirq.h> 21 #include <linux/kthread.h> /* for self test */ 22 #include <linux/module.h> 23 #include <linux/percpu.h> 24 #include <linux/mutex.h> 25 #include <linux/delay.h> 26 #include <linux/slab.h> 27 #include <linux/init.h> 28 #include <linux/hash.h> 29 #include <linux/list.h> 30 #include <linux/cpu.h> 31 #include <linux/oom.h> 32 #include <linux/mm.h> 33 34 #include <asm/local64.h> 35 #include <asm/local.h> 36 #include <asm/setup.h> 37 38 #include "trace.h" 39 40 /* 41 * The "absolute" timestamp in the buffer is only 59 bits. 42 * If a clock has the 5 MSBs set, it needs to be saved and 43 * reinserted. 44 */ 45 #define TS_MSB (0xf8ULL << 56) 46 #define ABS_TS_MASK (~TS_MSB) 47 48 static void update_pages_handler(struct work_struct *work); 49 50 #define RING_BUFFER_META_MAGIC 0xBADFEED 51 52 struct ring_buffer_meta { 53 int magic; 54 int struct_sizes; 55 unsigned long total_size; 56 unsigned long buffers_offset; 57 }; 58 59 struct ring_buffer_cpu_meta { 60 unsigned long first_buffer; 61 unsigned long head_buffer; 62 unsigned long commit_buffer; 63 __u32 subbuf_size; 64 __u32 nr_subbufs; 65 int buffers[]; 66 }; 67 68 /* 69 * The ring buffer header is special. We must manually up keep it. 70 */ 71 int ring_buffer_print_entry_header(struct trace_seq *s) 72 { 73 trace_seq_puts(s, "# compressed entry header\n"); 74 trace_seq_puts(s, "\ttype_len : 5 bits\n"); 75 trace_seq_puts(s, "\ttime_delta : 27 bits\n"); 76 trace_seq_puts(s, "\tarray : 32 bits\n"); 77 trace_seq_putc(s, '\n'); 78 trace_seq_printf(s, "\tpadding : type == %d\n", 79 RINGBUF_TYPE_PADDING); 80 trace_seq_printf(s, "\ttime_extend : type == %d\n", 81 RINGBUF_TYPE_TIME_EXTEND); 82 trace_seq_printf(s, "\ttime_stamp : type == %d\n", 83 RINGBUF_TYPE_TIME_STAMP); 84 trace_seq_printf(s, "\tdata max type_len == %d\n", 85 RINGBUF_TYPE_DATA_TYPE_LEN_MAX); 86 87 return !trace_seq_has_overflowed(s); 88 } 89 90 /* 91 * The ring buffer is made up of a list of pages. A separate list of pages is 92 * allocated for each CPU. A writer may only write to a buffer that is 93 * associated with the CPU it is currently executing on. A reader may read 94 * from any per cpu buffer. 95 * 96 * The reader is special. For each per cpu buffer, the reader has its own 97 * reader page. When a reader has read the entire reader page, this reader 98 * page is swapped with another page in the ring buffer. 99 * 100 * Now, as long as the writer is off the reader page, the reader can do what 101 * ever it wants with that page. The writer will never write to that page 102 * again (as long as it is out of the ring buffer). 103 * 104 * Here's some silly ASCII art. 105 * 106 * +------+ 107 * |reader| RING BUFFER 108 * |page | 109 * +------+ +---+ +---+ +---+ 110 * | |-->| |-->| | 111 * +---+ +---+ +---+ 112 * ^ | 113 * | | 114 * +---------------+ 115 * 116 * 117 * +------+ 118 * |reader| RING BUFFER 119 * |page |------------------v 120 * +------+ +---+ +---+ +---+ 121 * | |-->| |-->| | 122 * +---+ +---+ +---+ 123 * ^ | 124 * | | 125 * +---------------+ 126 * 127 * 128 * +------+ 129 * |reader| RING BUFFER 130 * |page |------------------v 131 * +------+ +---+ +---+ +---+ 132 * ^ | |-->| |-->| | 133 * | +---+ +---+ +---+ 134 * | | 135 * | | 136 * +------------------------------+ 137 * 138 * 139 * +------+ 140 * |buffer| RING BUFFER 141 * |page |------------------v 142 * +------+ +---+ +---+ +---+ 143 * ^ | | | |-->| | 144 * | New +---+ +---+ +---+ 145 * | Reader------^ | 146 * | page | 147 * +------------------------------+ 148 * 149 * 150 * After we make this swap, the reader can hand this page off to the splice 151 * code and be done with it. It can even allocate a new page if it needs to 152 * and swap that into the ring buffer. 153 * 154 * We will be using cmpxchg soon to make all this lockless. 155 * 156 */ 157 158 /* Used for individual buffers (after the counter) */ 159 #define RB_BUFFER_OFF (1 << 20) 160 161 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ 162 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX 163 164 enum { 165 RB_LEN_TIME_EXTEND = 8, 166 RB_LEN_TIME_STAMP = 8, 167 }; 168 169 #define skip_time_extend(event) \ 170 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND)) 171 172 #define extended_time(event) \ 173 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND) 174 175 static inline bool rb_null_event(struct ring_buffer_event *event) 176 { 177 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; 178 } 179 180 static void rb_event_set_padding(struct ring_buffer_event *event) 181 { 182 /* padding has a NULL time_delta */ 183 event->type_len = RINGBUF_TYPE_PADDING; 184 event->time_delta = 0; 185 } 186 187 static unsigned 188 rb_event_data_length(struct ring_buffer_event *event) 189 { 190 unsigned length; 191 192 if (event->type_len) 193 length = event->type_len * RB_ALIGNMENT; 194 else 195 length = event->array[0]; 196 return length + RB_EVNT_HDR_SIZE; 197 } 198 199 /* 200 * Return the length of the given event. Will return 201 * the length of the time extend if the event is a 202 * time extend. 203 */ 204 static inline unsigned 205 rb_event_length(struct ring_buffer_event *event) 206 { 207 switch (event->type_len) { 208 case RINGBUF_TYPE_PADDING: 209 if (rb_null_event(event)) 210 /* undefined */ 211 return -1; 212 return event->array[0] + RB_EVNT_HDR_SIZE; 213 214 case RINGBUF_TYPE_TIME_EXTEND: 215 return RB_LEN_TIME_EXTEND; 216 217 case RINGBUF_TYPE_TIME_STAMP: 218 return RB_LEN_TIME_STAMP; 219 220 case RINGBUF_TYPE_DATA: 221 return rb_event_data_length(event); 222 default: 223 WARN_ON_ONCE(1); 224 } 225 /* not hit */ 226 return 0; 227 } 228 229 /* 230 * Return total length of time extend and data, 231 * or just the event length for all other events. 232 */ 233 static inline unsigned 234 rb_event_ts_length(struct ring_buffer_event *event) 235 { 236 unsigned len = 0; 237 238 if (extended_time(event)) { 239 /* time extends include the data event after it */ 240 len = RB_LEN_TIME_EXTEND; 241 event = skip_time_extend(event); 242 } 243 return len + rb_event_length(event); 244 } 245 246 /** 247 * ring_buffer_event_length - return the length of the event 248 * @event: the event to get the length of 249 * 250 * Returns the size of the data load of a data event. 251 * If the event is something other than a data event, it 252 * returns the size of the event itself. With the exception 253 * of a TIME EXTEND, where it still returns the size of the 254 * data load of the data event after it. 255 */ 256 unsigned ring_buffer_event_length(struct ring_buffer_event *event) 257 { 258 unsigned length; 259 260 if (extended_time(event)) 261 event = skip_time_extend(event); 262 263 length = rb_event_length(event); 264 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) 265 return length; 266 length -= RB_EVNT_HDR_SIZE; 267 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) 268 length -= sizeof(event->array[0]); 269 return length; 270 } 271 EXPORT_SYMBOL_GPL(ring_buffer_event_length); 272 273 /* inline for ring buffer fast paths */ 274 static __always_inline void * 275 rb_event_data(struct ring_buffer_event *event) 276 { 277 if (extended_time(event)) 278 event = skip_time_extend(event); 279 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); 280 /* If length is in len field, then array[0] has the data */ 281 if (event->type_len) 282 return (void *)&event->array[0]; 283 /* Otherwise length is in array[0] and array[1] has the data */ 284 return (void *)&event->array[1]; 285 } 286 287 /** 288 * ring_buffer_event_data - return the data of the event 289 * @event: the event to get the data from 290 */ 291 void *ring_buffer_event_data(struct ring_buffer_event *event) 292 { 293 return rb_event_data(event); 294 } 295 EXPORT_SYMBOL_GPL(ring_buffer_event_data); 296 297 #define for_each_buffer_cpu(buffer, cpu) \ 298 for_each_cpu(cpu, buffer->cpumask) 299 300 #define for_each_online_buffer_cpu(buffer, cpu) \ 301 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask) 302 303 static u64 rb_event_time_stamp(struct ring_buffer_event *event) 304 { 305 u64 ts; 306 307 ts = event->array[0]; 308 ts <<= TS_SHIFT; 309 ts += event->time_delta; 310 311 return ts; 312 } 313 314 /* Flag when events were overwritten */ 315 #define RB_MISSED_EVENTS (1 << 31) 316 /* Missed count stored at end */ 317 #define RB_MISSED_STORED (1 << 30) 318 319 #define RB_MISSED_MASK (3 << 30) 320 321 struct buffer_data_read_page { 322 unsigned order; /* order of the page */ 323 struct buffer_data_page *data; /* actual data, stored in this page */ 324 }; 325 326 /* 327 * Note, the buffer_page list must be first. The buffer pages 328 * are allocated in cache lines, which means that each buffer 329 * page will be at the beginning of a cache line, and thus 330 * the least significant bits will be zero. We use this to 331 * add flags in the list struct pointers, to make the ring buffer 332 * lockless. 333 */ 334 struct buffer_page { 335 struct list_head list; /* list of buffer pages */ 336 local_t write; /* index for next write */ 337 unsigned read; /* index for next read */ 338 local_t entries; /* entries on this page */ 339 unsigned long real_end; /* real end of data */ 340 unsigned order; /* order of the page */ 341 u32 id:30; /* ID for external mapping */ 342 u32 range:1; /* Mapped via a range */ 343 struct buffer_data_page *page; /* Actual data page */ 344 }; 345 346 /* 347 * The buffer page counters, write and entries, must be reset 348 * atomically when crossing page boundaries. To synchronize this 349 * update, two counters are inserted into the number. One is 350 * the actual counter for the write position or count on the page. 351 * 352 * The other is a counter of updaters. Before an update happens 353 * the update partition of the counter is incremented. This will 354 * allow the updater to update the counter atomically. 355 * 356 * The counter is 20 bits, and the state data is 12. 357 */ 358 #define RB_WRITE_MASK 0xfffff 359 #define RB_WRITE_INTCNT (1 << 20) 360 361 static void rb_init_page(struct buffer_data_page *bpage) 362 { 363 local_set(&bpage->commit, 0); 364 } 365 366 static __always_inline unsigned int rb_page_commit(struct buffer_page *bpage) 367 { 368 return local_read(&bpage->page->commit); 369 } 370 371 static void free_buffer_page(struct buffer_page *bpage) 372 { 373 /* Range pages are not to be freed */ 374 if (!bpage->range) 375 free_pages((unsigned long)bpage->page, bpage->order); 376 kfree(bpage); 377 } 378 379 /* 380 * For best performance, allocate cpu buffer data cache line sized 381 * and per CPU. 382 */ 383 #define alloc_cpu_buffer(cpu) (struct ring_buffer_per_cpu *) \ 384 kzalloc_node(ALIGN(sizeof(struct ring_buffer_per_cpu), \ 385 cache_line_size()), GFP_KERNEL, cpu_to_node(cpu)); 386 387 #define alloc_cpu_page(cpu) (struct buffer_page *) \ 388 kzalloc_node(ALIGN(sizeof(struct buffer_page), \ 389 cache_line_size()), GFP_KERNEL, cpu_to_node(cpu)); 390 391 static struct buffer_data_page *alloc_cpu_data(int cpu, int order) 392 { 393 struct buffer_data_page *dpage; 394 struct page *page; 395 gfp_t mflags; 396 397 /* 398 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails 399 * gracefully without invoking oom-killer and the system is not 400 * destabilized. 401 */ 402 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO; 403 404 page = alloc_pages_node(cpu_to_node(cpu), mflags, order); 405 if (!page) 406 return NULL; 407 408 dpage = page_address(page); 409 rb_init_page(dpage); 410 411 return dpage; 412 } 413 414 struct rb_irq_work { 415 struct irq_work work; 416 wait_queue_head_t waiters; 417 wait_queue_head_t full_waiters; 418 atomic_t seq; 419 bool waiters_pending; 420 bool full_waiters_pending; 421 bool wakeup_full; 422 }; 423 424 /* 425 * Structure to hold event state and handle nested events. 426 */ 427 struct rb_event_info { 428 u64 ts; 429 u64 delta; 430 u64 before; 431 u64 after; 432 unsigned long length; 433 struct buffer_page *tail_page; 434 int add_timestamp; 435 }; 436 437 /* 438 * Used for the add_timestamp 439 * NONE 440 * EXTEND - wants a time extend 441 * ABSOLUTE - the buffer requests all events to have absolute time stamps 442 * FORCE - force a full time stamp. 443 */ 444 enum { 445 RB_ADD_STAMP_NONE = 0, 446 RB_ADD_STAMP_EXTEND = BIT(1), 447 RB_ADD_STAMP_ABSOLUTE = BIT(2), 448 RB_ADD_STAMP_FORCE = BIT(3) 449 }; 450 /* 451 * Used for which event context the event is in. 452 * TRANSITION = 0 453 * NMI = 1 454 * IRQ = 2 455 * SOFTIRQ = 3 456 * NORMAL = 4 457 * 458 * See trace_recursive_lock() comment below for more details. 459 */ 460 enum { 461 RB_CTX_TRANSITION, 462 RB_CTX_NMI, 463 RB_CTX_IRQ, 464 RB_CTX_SOFTIRQ, 465 RB_CTX_NORMAL, 466 RB_CTX_MAX 467 }; 468 469 struct rb_time_struct { 470 local64_t time; 471 }; 472 typedef struct rb_time_struct rb_time_t; 473 474 #define MAX_NEST 5 475 476 /* 477 * head_page == tail_page && head == tail then buffer is empty. 478 */ 479 struct ring_buffer_per_cpu { 480 int cpu; 481 atomic_t record_disabled; 482 atomic_t resize_disabled; 483 struct trace_buffer *buffer; 484 raw_spinlock_t reader_lock; /* serialize readers */ 485 arch_spinlock_t lock; 486 struct lock_class_key lock_key; 487 struct buffer_data_page *free_page; 488 unsigned long nr_pages; 489 unsigned int current_context; 490 struct list_head *pages; 491 /* pages generation counter, incremented when the list changes */ 492 unsigned long cnt; 493 struct buffer_page *head_page; /* read from head */ 494 struct buffer_page *tail_page; /* write to tail */ 495 struct buffer_page *commit_page; /* committed pages */ 496 struct buffer_page *reader_page; 497 unsigned long lost_events; 498 unsigned long last_overrun; 499 unsigned long nest; 500 local_t entries_bytes; 501 local_t entries; 502 local_t overrun; 503 local_t commit_overrun; 504 local_t dropped_events; 505 local_t committing; 506 local_t commits; 507 local_t pages_touched; 508 local_t pages_lost; 509 local_t pages_read; 510 long last_pages_touch; 511 size_t shortest_full; 512 unsigned long read; 513 unsigned long read_bytes; 514 rb_time_t write_stamp; 515 rb_time_t before_stamp; 516 u64 event_stamp[MAX_NEST]; 517 u64 read_stamp; 518 /* pages removed since last reset */ 519 unsigned long pages_removed; 520 521 unsigned int mapped; 522 unsigned int user_mapped; /* user space mapping */ 523 struct mutex mapping_lock; 524 struct buffer_page **subbuf_ids; /* ID to subbuf VA */ 525 struct trace_buffer_meta *meta_page; 526 struct ring_buffer_cpu_meta *ring_meta; 527 528 struct ring_buffer_remote *remote; 529 530 /* ring buffer pages to update, > 0 to add, < 0 to remove */ 531 long nr_pages_to_update; 532 struct list_head new_pages; /* new pages to add */ 533 struct work_struct update_pages_work; 534 struct completion update_done; 535 536 struct rb_irq_work irq_work; 537 }; 538 539 struct trace_buffer { 540 unsigned flags; 541 int cpus; 542 atomic_t record_disabled; 543 atomic_t resizing; 544 cpumask_var_t cpumask; 545 546 struct lock_class_key *reader_lock_key; 547 548 struct mutex mutex; 549 550 struct ring_buffer_per_cpu **buffers; 551 552 struct ring_buffer_remote *remote; 553 554 struct hlist_node node; 555 u64 (*clock)(void); 556 557 struct rb_irq_work irq_work; 558 bool time_stamp_abs; 559 560 unsigned long range_addr_start; 561 unsigned long range_addr_end; 562 563 struct ring_buffer_meta *meta; 564 565 unsigned int subbuf_size; 566 unsigned int subbuf_order; 567 unsigned int max_data_size; 568 }; 569 570 struct ring_buffer_iter { 571 struct ring_buffer_per_cpu *cpu_buffer; 572 unsigned long head; 573 unsigned long next_event; 574 struct buffer_page *head_page; 575 struct buffer_page *cache_reader_page; 576 unsigned long cache_read; 577 unsigned long cache_pages_removed; 578 u64 read_stamp; 579 u64 page_stamp; 580 struct ring_buffer_event *event; 581 size_t event_size; 582 int missed_events; 583 }; 584 585 int ring_buffer_print_page_header(struct trace_buffer *buffer, struct trace_seq *s) 586 { 587 struct buffer_data_page field; 588 589 trace_seq_printf(s, "\tfield: u64 timestamp;\t" 590 "offset:0;\tsize:%u;\tsigned:%u;\n", 591 (unsigned int)sizeof(field.time_stamp), 592 (unsigned int)is_signed_type(u64)); 593 594 trace_seq_printf(s, "\tfield: local_t commit;\t" 595 "offset:%u;\tsize:%u;\tsigned:%u;\n", 596 (unsigned int)offsetof(typeof(field), commit), 597 (unsigned int)sizeof(field.commit), 598 (unsigned int)is_signed_type(long)); 599 600 trace_seq_printf(s, "\tfield: char overwrite;\t" 601 "offset:%u;\tsize:%u;\tsigned:%u;\n", 602 (unsigned int)offsetof(typeof(field), commit), 603 1, 604 (unsigned int)is_signed_type(char)); 605 606 trace_seq_printf(s, "\tfield: char data;\t" 607 "offset:%u;\tsize:%u;\tsigned:%u;\n", 608 (unsigned int)offsetof(typeof(field), data), 609 (unsigned int)(buffer ? buffer->subbuf_size : 610 PAGE_SIZE - BUF_PAGE_HDR_SIZE), 611 (unsigned int)is_signed_type(char)); 612 613 return !trace_seq_has_overflowed(s); 614 } 615 616 static inline void rb_time_read(rb_time_t *t, u64 *ret) 617 { 618 *ret = local64_read(&t->time); 619 } 620 static void rb_time_set(rb_time_t *t, u64 val) 621 { 622 local64_set(&t->time, val); 623 } 624 625 /* 626 * Enable this to make sure that the event passed to 627 * ring_buffer_event_time_stamp() is not committed and also 628 * is on the buffer that it passed in. 629 */ 630 //#define RB_VERIFY_EVENT 631 #ifdef RB_VERIFY_EVENT 632 static struct list_head *rb_list_head(struct list_head *list); 633 static void verify_event(struct ring_buffer_per_cpu *cpu_buffer, 634 void *event) 635 { 636 struct buffer_page *page = cpu_buffer->commit_page; 637 struct buffer_page *tail_page = READ_ONCE(cpu_buffer->tail_page); 638 struct list_head *next; 639 long commit, write; 640 unsigned long addr = (unsigned long)event; 641 bool done = false; 642 int stop = 0; 643 644 /* Make sure the event exists and is not committed yet */ 645 do { 646 if (page == tail_page || WARN_ON_ONCE(stop++ > 100)) 647 done = true; 648 commit = local_read(&page->page->commit); 649 write = local_read(&page->write); 650 if (addr >= (unsigned long)&page->page->data[commit] && 651 addr < (unsigned long)&page->page->data[write]) 652 return; 653 654 next = rb_list_head(page->list.next); 655 page = list_entry(next, struct buffer_page, list); 656 } while (!done); 657 WARN_ON_ONCE(1); 658 } 659 #else 660 static inline void verify_event(struct ring_buffer_per_cpu *cpu_buffer, 661 void *event) 662 { 663 } 664 #endif 665 666 /* 667 * The absolute time stamp drops the 5 MSBs and some clocks may 668 * require them. The rb_fix_abs_ts() will take a previous full 669 * time stamp, and add the 5 MSB of that time stamp on to the 670 * saved absolute time stamp. Then they are compared in case of 671 * the unlikely event that the latest time stamp incremented 672 * the 5 MSB. 673 */ 674 static inline u64 rb_fix_abs_ts(u64 abs, u64 save_ts) 675 { 676 if (save_ts & TS_MSB) { 677 abs |= save_ts & TS_MSB; 678 /* Check for overflow */ 679 if (unlikely(abs < save_ts)) 680 abs += 1ULL << 59; 681 } 682 return abs; 683 } 684 685 static inline u64 rb_time_stamp(struct trace_buffer *buffer); 686 687 /** 688 * ring_buffer_event_time_stamp - return the event's current time stamp 689 * @buffer: The buffer that the event is on 690 * @event: the event to get the time stamp of 691 * 692 * Note, this must be called after @event is reserved, and before it is 693 * committed to the ring buffer. And must be called from the same 694 * context where the event was reserved (normal, softirq, irq, etc). 695 * 696 * Returns the time stamp associated with the current event. 697 * If the event has an extended time stamp, then that is used as 698 * the time stamp to return. 699 * In the highly unlikely case that the event was nested more than 700 * the max nesting, then the write_stamp of the buffer is returned, 701 * otherwise current time is returned, but that really neither of 702 * the last two cases should ever happen. 703 */ 704 u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer, 705 struct ring_buffer_event *event) 706 { 707 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()]; 708 unsigned int nest; 709 u64 ts; 710 711 /* If the event includes an absolute time, then just use that */ 712 if (event->type_len == RINGBUF_TYPE_TIME_STAMP) { 713 ts = rb_event_time_stamp(event); 714 return rb_fix_abs_ts(ts, cpu_buffer->tail_page->page->time_stamp); 715 } 716 717 nest = local_read(&cpu_buffer->committing); 718 verify_event(cpu_buffer, event); 719 if (WARN_ON_ONCE(!nest)) 720 goto fail; 721 722 /* Read the current saved nesting level time stamp */ 723 if (likely(--nest < MAX_NEST)) 724 return cpu_buffer->event_stamp[nest]; 725 726 /* Shouldn't happen, warn if it does */ 727 WARN_ONCE(1, "nest (%d) greater than max", nest); 728 729 fail: 730 rb_time_read(&cpu_buffer->write_stamp, &ts); 731 732 return ts; 733 } 734 735 /** 736 * ring_buffer_nr_dirty_pages - get the number of used pages in the ring buffer 737 * @buffer: The ring_buffer to get the number of pages from 738 * @cpu: The cpu of the ring_buffer to get the number of pages from 739 * 740 * Returns the number of pages that have content in the ring buffer. 741 */ 742 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu) 743 { 744 size_t read; 745 size_t lost; 746 size_t cnt; 747 748 read = local_read(&buffer->buffers[cpu]->pages_read); 749 lost = local_read(&buffer->buffers[cpu]->pages_lost); 750 cnt = local_read(&buffer->buffers[cpu]->pages_touched); 751 752 if (WARN_ON_ONCE(cnt < lost)) 753 return 0; 754 755 cnt -= lost; 756 757 /* The reader can read an empty page, but not more than that */ 758 if (cnt < read) { 759 WARN_ON_ONCE(read > cnt + 1); 760 return 0; 761 } 762 763 return cnt - read; 764 } 765 766 static __always_inline bool full_hit(struct trace_buffer *buffer, int cpu, int full) 767 { 768 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; 769 size_t nr_pages; 770 size_t dirty; 771 772 nr_pages = cpu_buffer->nr_pages; 773 if (!nr_pages || !full) 774 return true; 775 776 /* 777 * Add one as dirty will never equal nr_pages, as the sub-buffer 778 * that the writer is on is not counted as dirty. 779 * This is needed if "buffer_percent" is set to 100. 780 */ 781 dirty = ring_buffer_nr_dirty_pages(buffer, cpu) + 1; 782 783 return (dirty * 100) >= (full * nr_pages); 784 } 785 786 /* 787 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input 788 * 789 * Schedules a delayed work to wake up any task that is blocked on the 790 * ring buffer waiters queue. 791 */ 792 static void rb_wake_up_waiters(struct irq_work *work) 793 { 794 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work); 795 796 /* For waiters waiting for the first wake up */ 797 (void)atomic_fetch_inc_release(&rbwork->seq); 798 799 wake_up_all(&rbwork->waiters); 800 if (rbwork->full_waiters_pending || rbwork->wakeup_full) { 801 /* Only cpu_buffer sets the above flags */ 802 struct ring_buffer_per_cpu *cpu_buffer = 803 container_of(rbwork, struct ring_buffer_per_cpu, irq_work); 804 805 /* Called from interrupt context */ 806 raw_spin_lock(&cpu_buffer->reader_lock); 807 rbwork->wakeup_full = false; 808 rbwork->full_waiters_pending = false; 809 810 /* Waking up all waiters, they will reset the shortest full */ 811 cpu_buffer->shortest_full = 0; 812 raw_spin_unlock(&cpu_buffer->reader_lock); 813 814 wake_up_all(&rbwork->full_waiters); 815 } 816 } 817 818 /** 819 * ring_buffer_wake_waiters - wake up any waiters on this ring buffer 820 * @buffer: The ring buffer to wake waiters on 821 * @cpu: The CPU buffer to wake waiters on 822 * 823 * In the case of a file that represents a ring buffer is closing, 824 * it is prudent to wake up any waiters that are on this. 825 */ 826 void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu) 827 { 828 struct ring_buffer_per_cpu *cpu_buffer; 829 struct rb_irq_work *rbwork; 830 831 if (!buffer) 832 return; 833 834 if (cpu == RING_BUFFER_ALL_CPUS) { 835 836 /* Wake up individual ones too. One level recursion */ 837 for_each_buffer_cpu(buffer, cpu) 838 ring_buffer_wake_waiters(buffer, cpu); 839 840 rbwork = &buffer->irq_work; 841 } else { 842 if (WARN_ON_ONCE(!buffer->buffers)) 843 return; 844 if (WARN_ON_ONCE(cpu >= nr_cpu_ids)) 845 return; 846 847 cpu_buffer = buffer->buffers[cpu]; 848 /* The CPU buffer may not have been initialized yet */ 849 if (!cpu_buffer) 850 return; 851 rbwork = &cpu_buffer->irq_work; 852 } 853 854 /* This can be called in any context */ 855 irq_work_queue(&rbwork->work); 856 } 857 858 static bool rb_watermark_hit(struct trace_buffer *buffer, int cpu, int full) 859 { 860 struct ring_buffer_per_cpu *cpu_buffer; 861 bool ret = false; 862 863 /* Reads of all CPUs always waits for any data */ 864 if (cpu == RING_BUFFER_ALL_CPUS) 865 return !ring_buffer_empty(buffer); 866 867 cpu_buffer = buffer->buffers[cpu]; 868 869 if (!ring_buffer_empty_cpu(buffer, cpu)) { 870 unsigned long flags; 871 bool pagebusy; 872 873 if (!full) 874 return true; 875 876 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 877 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page; 878 ret = !pagebusy && full_hit(buffer, cpu, full); 879 880 if (!ret && (!cpu_buffer->shortest_full || 881 cpu_buffer->shortest_full > full)) { 882 cpu_buffer->shortest_full = full; 883 } 884 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 885 } 886 return ret; 887 } 888 889 static inline bool 890 rb_wait_cond(struct rb_irq_work *rbwork, struct trace_buffer *buffer, 891 int cpu, int full, ring_buffer_cond_fn cond, void *data) 892 { 893 if (rb_watermark_hit(buffer, cpu, full)) 894 return true; 895 896 if (cond(data)) 897 return true; 898 899 /* 900 * The events can happen in critical sections where 901 * checking a work queue can cause deadlocks. 902 * After adding a task to the queue, this flag is set 903 * only to notify events to try to wake up the queue 904 * using irq_work. 905 * 906 * We don't clear it even if the buffer is no longer 907 * empty. The flag only causes the next event to run 908 * irq_work to do the work queue wake up. The worse 909 * that can happen if we race with !trace_empty() is that 910 * an event will cause an irq_work to try to wake up 911 * an empty queue. 912 * 913 * There's no reason to protect this flag either, as 914 * the work queue and irq_work logic will do the necessary 915 * synchronization for the wake ups. The only thing 916 * that is necessary is that the wake up happens after 917 * a task has been queued. It's OK for spurious wake ups. 918 */ 919 if (full) 920 rbwork->full_waiters_pending = true; 921 else 922 rbwork->waiters_pending = true; 923 924 return false; 925 } 926 927 struct rb_wait_data { 928 struct rb_irq_work *irq_work; 929 int seq; 930 }; 931 932 /* 933 * The default wait condition for ring_buffer_wait() is to just to exit the 934 * wait loop the first time it is woken up. 935 */ 936 static bool rb_wait_once(void *data) 937 { 938 struct rb_wait_data *rdata = data; 939 struct rb_irq_work *rbwork = rdata->irq_work; 940 941 return atomic_read_acquire(&rbwork->seq) != rdata->seq; 942 } 943 944 /** 945 * ring_buffer_wait - wait for input to the ring buffer 946 * @buffer: buffer to wait on 947 * @cpu: the cpu buffer to wait on 948 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS 949 * @cond: condition function to break out of wait (NULL to run once) 950 * @data: the data to pass to @cond. 951 * 952 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon 953 * as data is added to any of the @buffer's cpu buffers. Otherwise 954 * it will wait for data to be added to a specific cpu buffer. 955 */ 956 int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full, 957 ring_buffer_cond_fn cond, void *data) 958 { 959 struct ring_buffer_per_cpu *cpu_buffer; 960 struct wait_queue_head *waitq; 961 struct rb_irq_work *rbwork; 962 struct rb_wait_data rdata; 963 int ret = 0; 964 965 /* 966 * Depending on what the caller is waiting for, either any 967 * data in any cpu buffer, or a specific buffer, put the 968 * caller on the appropriate wait queue. 969 */ 970 if (cpu == RING_BUFFER_ALL_CPUS) { 971 rbwork = &buffer->irq_work; 972 /* Full only makes sense on per cpu reads */ 973 full = 0; 974 } else { 975 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 976 return -ENODEV; 977 cpu_buffer = buffer->buffers[cpu]; 978 rbwork = &cpu_buffer->irq_work; 979 } 980 981 if (full) 982 waitq = &rbwork->full_waiters; 983 else 984 waitq = &rbwork->waiters; 985 986 /* Set up to exit loop as soon as it is woken */ 987 if (!cond) { 988 cond = rb_wait_once; 989 rdata.irq_work = rbwork; 990 rdata.seq = atomic_read_acquire(&rbwork->seq); 991 data = &rdata; 992 } 993 994 ret = wait_event_interruptible((*waitq), 995 rb_wait_cond(rbwork, buffer, cpu, full, cond, data)); 996 997 return ret; 998 } 999 1000 /** 1001 * ring_buffer_poll_wait - poll on buffer input 1002 * @buffer: buffer to wait on 1003 * @cpu: the cpu buffer to wait on 1004 * @filp: the file descriptor 1005 * @poll_table: The poll descriptor 1006 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS 1007 * 1008 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon 1009 * as data is added to any of the @buffer's cpu buffers. Otherwise 1010 * it will wait for data to be added to a specific cpu buffer. 1011 * 1012 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers, 1013 * zero otherwise. 1014 */ 1015 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu, 1016 struct file *filp, poll_table *poll_table, int full) 1017 { 1018 struct ring_buffer_per_cpu *cpu_buffer; 1019 struct rb_irq_work *rbwork; 1020 1021 if (cpu == RING_BUFFER_ALL_CPUS) { 1022 rbwork = &buffer->irq_work; 1023 full = 0; 1024 } else { 1025 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 1026 return EPOLLERR; 1027 1028 cpu_buffer = buffer->buffers[cpu]; 1029 rbwork = &cpu_buffer->irq_work; 1030 } 1031 1032 if (full) { 1033 poll_wait(filp, &rbwork->full_waiters, poll_table); 1034 1035 if (rb_watermark_hit(buffer, cpu, full)) 1036 return EPOLLIN | EPOLLRDNORM; 1037 /* 1038 * Only allow full_waiters_pending update to be seen after 1039 * the shortest_full is set (in rb_watermark_hit). If the 1040 * writer sees the full_waiters_pending flag set, it will 1041 * compare the amount in the ring buffer to shortest_full. 1042 * If the amount in the ring buffer is greater than the 1043 * shortest_full percent, it will call the irq_work handler 1044 * to wake up this list. The irq_handler will reset shortest_full 1045 * back to zero. That's done under the reader_lock, but 1046 * the below smp_mb() makes sure that the update to 1047 * full_waiters_pending doesn't leak up into the above. 1048 */ 1049 smp_mb(); 1050 rbwork->full_waiters_pending = true; 1051 return 0; 1052 } 1053 1054 poll_wait(filp, &rbwork->waiters, poll_table); 1055 rbwork->waiters_pending = true; 1056 1057 /* 1058 * There's a tight race between setting the waiters_pending and 1059 * checking if the ring buffer is empty. Once the waiters_pending bit 1060 * is set, the next event will wake the task up, but we can get stuck 1061 * if there's only a single event in. 1062 * 1063 * FIXME: Ideally, we need a memory barrier on the writer side as well, 1064 * but adding a memory barrier to all events will cause too much of a 1065 * performance hit in the fast path. We only need a memory barrier when 1066 * the buffer goes from empty to having content. But as this race is 1067 * extremely small, and it's not a problem if another event comes in, we 1068 * will fix it later. 1069 */ 1070 smp_mb(); 1071 1072 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) || 1073 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu))) 1074 return EPOLLIN | EPOLLRDNORM; 1075 return 0; 1076 } 1077 1078 /* buffer may be either ring_buffer or ring_buffer_per_cpu */ 1079 #define RB_WARN_ON(b, cond) \ 1080 ({ \ 1081 int _____ret = unlikely(cond); \ 1082 if (_____ret) { \ 1083 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ 1084 struct ring_buffer_per_cpu *__b = \ 1085 (void *)b; \ 1086 atomic_inc(&__b->buffer->record_disabled); \ 1087 } else \ 1088 atomic_inc(&b->record_disabled); \ 1089 WARN_ON(1); \ 1090 } \ 1091 _____ret; \ 1092 }) 1093 1094 /* Up this if you want to test the TIME_EXTENTS and normalization */ 1095 #define DEBUG_SHIFT 0 1096 1097 static inline u64 rb_time_stamp(struct trace_buffer *buffer) 1098 { 1099 u64 ts; 1100 1101 /* Skip retpolines :-( */ 1102 if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && likely(buffer->clock == trace_clock_local)) 1103 ts = trace_clock_local(); 1104 else 1105 ts = buffer->clock(); 1106 1107 /* shift to debug/test normalization and TIME_EXTENTS */ 1108 return ts << DEBUG_SHIFT; 1109 } 1110 1111 u64 ring_buffer_time_stamp(struct trace_buffer *buffer) 1112 { 1113 u64 time; 1114 1115 preempt_disable_notrace(); 1116 time = rb_time_stamp(buffer); 1117 preempt_enable_notrace(); 1118 1119 return time; 1120 } 1121 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); 1122 1123 void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer, 1124 int cpu, u64 *ts) 1125 { 1126 /* Just stupid testing the normalize function and deltas */ 1127 *ts >>= DEBUG_SHIFT; 1128 } 1129 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); 1130 1131 /* 1132 * Making the ring buffer lockless makes things tricky. 1133 * Although writes only happen on the CPU that they are on, 1134 * and they only need to worry about interrupts. Reads can 1135 * happen on any CPU. 1136 * 1137 * The reader page is always off the ring buffer, but when the 1138 * reader finishes with a page, it needs to swap its page with 1139 * a new one from the buffer. The reader needs to take from 1140 * the head (writes go to the tail). But if a writer is in overwrite 1141 * mode and wraps, it must push the head page forward. 1142 * 1143 * Here lies the problem. 1144 * 1145 * The reader must be careful to replace only the head page, and 1146 * not another one. As described at the top of the file in the 1147 * ASCII art, the reader sets its old page to point to the next 1148 * page after head. It then sets the page after head to point to 1149 * the old reader page. But if the writer moves the head page 1150 * during this operation, the reader could end up with the tail. 1151 * 1152 * We use cmpxchg to help prevent this race. We also do something 1153 * special with the page before head. We set the LSB to 1. 1154 * 1155 * When the writer must push the page forward, it will clear the 1156 * bit that points to the head page, move the head, and then set 1157 * the bit that points to the new head page. 1158 * 1159 * We also don't want an interrupt coming in and moving the head 1160 * page on another writer. Thus we use the second LSB to catch 1161 * that too. Thus: 1162 * 1163 * head->list->prev->next bit 1 bit 0 1164 * ------- ------- 1165 * Normal page 0 0 1166 * Points to head page 0 1 1167 * New head page 1 0 1168 * 1169 * Note we can not trust the prev pointer of the head page, because: 1170 * 1171 * +----+ +-----+ +-----+ 1172 * | |------>| T |---X--->| N | 1173 * | |<------| | | | 1174 * +----+ +-----+ +-----+ 1175 * ^ ^ | 1176 * | +-----+ | | 1177 * +----------| R |----------+ | 1178 * | |<-----------+ 1179 * +-----+ 1180 * 1181 * Key: ---X--> HEAD flag set in pointer 1182 * T Tail page 1183 * R Reader page 1184 * N Next page 1185 * 1186 * (see __rb_reserve_next() to see where this happens) 1187 * 1188 * What the above shows is that the reader just swapped out 1189 * the reader page with a page in the buffer, but before it 1190 * could make the new header point back to the new page added 1191 * it was preempted by a writer. The writer moved forward onto 1192 * the new page added by the reader and is about to move forward 1193 * again. 1194 * 1195 * You can see, it is legitimate for the previous pointer of 1196 * the head (or any page) not to point back to itself. But only 1197 * temporarily. 1198 */ 1199 1200 #define RB_PAGE_NORMAL 0UL 1201 #define RB_PAGE_HEAD 1UL 1202 #define RB_PAGE_UPDATE 2UL 1203 1204 1205 #define RB_FLAG_MASK 3UL 1206 1207 /* PAGE_MOVED is not part of the mask */ 1208 #define RB_PAGE_MOVED 4UL 1209 1210 /* 1211 * rb_list_head - remove any bit 1212 */ 1213 static struct list_head *rb_list_head(struct list_head *list) 1214 { 1215 unsigned long val = (unsigned long)list; 1216 1217 return (struct list_head *)(val & ~RB_FLAG_MASK); 1218 } 1219 1220 /* 1221 * rb_is_head_page - test if the given page is the head page 1222 * 1223 * Because the reader may move the head_page pointer, we can 1224 * not trust what the head page is (it may be pointing to 1225 * the reader page). But if the next page is a header page, 1226 * its flags will be non zero. 1227 */ 1228 static inline int 1229 rb_is_head_page(struct buffer_page *page, struct list_head *list) 1230 { 1231 unsigned long val; 1232 1233 val = (unsigned long)list->next; 1234 1235 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) 1236 return RB_PAGE_MOVED; 1237 1238 return val & RB_FLAG_MASK; 1239 } 1240 1241 /* 1242 * rb_is_reader_page 1243 * 1244 * The unique thing about the reader page, is that, if the 1245 * writer is ever on it, the previous pointer never points 1246 * back to the reader page. 1247 */ 1248 static bool rb_is_reader_page(struct buffer_page *page) 1249 { 1250 struct list_head *list = page->list.prev; 1251 1252 return rb_list_head(list->next) != &page->list; 1253 } 1254 1255 /* 1256 * rb_set_list_to_head - set a list_head to be pointing to head. 1257 */ 1258 static void rb_set_list_to_head(struct list_head *list) 1259 { 1260 unsigned long *ptr; 1261 1262 ptr = (unsigned long *)&list->next; 1263 *ptr |= RB_PAGE_HEAD; 1264 *ptr &= ~RB_PAGE_UPDATE; 1265 } 1266 1267 /* 1268 * rb_head_page_activate - sets up head page 1269 */ 1270 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) 1271 { 1272 struct buffer_page *head; 1273 1274 head = cpu_buffer->head_page; 1275 if (!head) 1276 return; 1277 1278 /* 1279 * Set the previous list pointer to have the HEAD flag. 1280 */ 1281 rb_set_list_to_head(head->list.prev); 1282 1283 if (cpu_buffer->ring_meta) { 1284 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 1285 meta->head_buffer = (unsigned long)head->page; 1286 } 1287 } 1288 1289 static void rb_list_head_clear(struct list_head *list) 1290 { 1291 unsigned long *ptr = (unsigned long *)&list->next; 1292 1293 *ptr &= ~RB_FLAG_MASK; 1294 } 1295 1296 /* 1297 * rb_head_page_deactivate - clears head page ptr (for free list) 1298 */ 1299 static void 1300 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) 1301 { 1302 struct list_head *hd; 1303 1304 /* Go through the whole list and clear any pointers found. */ 1305 rb_list_head_clear(cpu_buffer->pages); 1306 1307 list_for_each(hd, cpu_buffer->pages) 1308 rb_list_head_clear(hd); 1309 } 1310 1311 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, 1312 struct buffer_page *head, 1313 struct buffer_page *prev, 1314 int old_flag, int new_flag) 1315 { 1316 struct list_head *list; 1317 unsigned long val = (unsigned long)&head->list; 1318 unsigned long ret; 1319 1320 list = &prev->list; 1321 1322 val &= ~RB_FLAG_MASK; 1323 1324 ret = cmpxchg((unsigned long *)&list->next, 1325 val | old_flag, val | new_flag); 1326 1327 /* check if the reader took the page */ 1328 if ((ret & ~RB_FLAG_MASK) != val) 1329 return RB_PAGE_MOVED; 1330 1331 return ret & RB_FLAG_MASK; 1332 } 1333 1334 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, 1335 struct buffer_page *head, 1336 struct buffer_page *prev, 1337 int old_flag) 1338 { 1339 return rb_head_page_set(cpu_buffer, head, prev, 1340 old_flag, RB_PAGE_UPDATE); 1341 } 1342 1343 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, 1344 struct buffer_page *head, 1345 struct buffer_page *prev, 1346 int old_flag) 1347 { 1348 return rb_head_page_set(cpu_buffer, head, prev, 1349 old_flag, RB_PAGE_HEAD); 1350 } 1351 1352 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, 1353 struct buffer_page *head, 1354 struct buffer_page *prev, 1355 int old_flag) 1356 { 1357 return rb_head_page_set(cpu_buffer, head, prev, 1358 old_flag, RB_PAGE_NORMAL); 1359 } 1360 1361 static inline void rb_inc_page(struct buffer_page **bpage) 1362 { 1363 struct list_head *p = rb_list_head((*bpage)->list.next); 1364 1365 *bpage = list_entry(p, struct buffer_page, list); 1366 } 1367 1368 static inline void rb_dec_page(struct buffer_page **bpage) 1369 { 1370 struct list_head *p = rb_list_head((*bpage)->list.prev); 1371 1372 *bpage = list_entry(p, struct buffer_page, list); 1373 } 1374 1375 static struct buffer_page * 1376 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) 1377 { 1378 struct buffer_page *head; 1379 struct buffer_page *page; 1380 struct list_head *list; 1381 int i; 1382 1383 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) 1384 return NULL; 1385 1386 /* sanity check */ 1387 list = cpu_buffer->pages; 1388 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) 1389 return NULL; 1390 1391 page = head = cpu_buffer->head_page; 1392 /* 1393 * It is possible that the writer moves the header behind 1394 * where we started, and we miss in one loop. 1395 * A second loop should grab the header, but we'll do 1396 * three loops just because I'm paranoid. 1397 */ 1398 for (i = 0; i < 3; i++) { 1399 do { 1400 if (rb_is_head_page(page, page->list.prev)) { 1401 cpu_buffer->head_page = page; 1402 return page; 1403 } 1404 rb_inc_page(&page); 1405 } while (page != head); 1406 } 1407 1408 RB_WARN_ON(cpu_buffer, 1); 1409 1410 return NULL; 1411 } 1412 1413 static bool rb_head_page_replace(struct buffer_page *old, 1414 struct buffer_page *new) 1415 { 1416 unsigned long *ptr = (unsigned long *)&old->list.prev->next; 1417 unsigned long val; 1418 1419 val = *ptr & ~RB_FLAG_MASK; 1420 val |= RB_PAGE_HEAD; 1421 1422 return try_cmpxchg(ptr, &val, (unsigned long)&new->list); 1423 } 1424 1425 /* 1426 * rb_tail_page_update - move the tail page forward 1427 */ 1428 static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, 1429 struct buffer_page *tail_page, 1430 struct buffer_page *next_page) 1431 { 1432 unsigned long old_entries; 1433 unsigned long old_write; 1434 1435 /* 1436 * The tail page now needs to be moved forward. 1437 * 1438 * We need to reset the tail page, but without messing 1439 * with possible erasing of data brought in by interrupts 1440 * that have moved the tail page and are currently on it. 1441 * 1442 * We add a counter to the write field to denote this. 1443 */ 1444 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); 1445 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); 1446 1447 /* 1448 * Just make sure we have seen our old_write and synchronize 1449 * with any interrupts that come in. 1450 */ 1451 barrier(); 1452 1453 /* 1454 * If the tail page is still the same as what we think 1455 * it is, then it is up to us to update the tail 1456 * pointer. 1457 */ 1458 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) { 1459 /* Zero the write counter */ 1460 unsigned long val = old_write & ~RB_WRITE_MASK; 1461 unsigned long eval = old_entries & ~RB_WRITE_MASK; 1462 1463 /* 1464 * This will only succeed if an interrupt did 1465 * not come in and change it. In which case, we 1466 * do not want to modify it. 1467 * 1468 * We add (void) to let the compiler know that we do not care 1469 * about the return value of these functions. We use the 1470 * cmpxchg to only update if an interrupt did not already 1471 * do it for us. If the cmpxchg fails, we don't care. 1472 */ 1473 (void)local_cmpxchg(&next_page->write, old_write, val); 1474 (void)local_cmpxchg(&next_page->entries, old_entries, eval); 1475 1476 /* 1477 * No need to worry about races with clearing out the commit. 1478 * it only can increment when a commit takes place. But that 1479 * only happens in the outer most nested commit. 1480 */ 1481 local_set(&next_page->page->commit, 0); 1482 1483 /* Either we update tail_page or an interrupt does */ 1484 if (try_cmpxchg(&cpu_buffer->tail_page, &tail_page, next_page)) 1485 local_inc(&cpu_buffer->pages_touched); 1486 } 1487 } 1488 1489 static void rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, 1490 struct buffer_page *bpage) 1491 { 1492 unsigned long val = (unsigned long)bpage; 1493 1494 RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK); 1495 } 1496 1497 static bool rb_check_links(struct ring_buffer_per_cpu *cpu_buffer, 1498 struct list_head *list) 1499 { 1500 if (RB_WARN_ON(cpu_buffer, 1501 rb_list_head(rb_list_head(list->next)->prev) != list)) 1502 return false; 1503 1504 if (RB_WARN_ON(cpu_buffer, 1505 rb_list_head(rb_list_head(list->prev)->next) != list)) 1506 return false; 1507 1508 return true; 1509 } 1510 1511 /** 1512 * rb_check_pages - integrity check of buffer pages 1513 * @cpu_buffer: CPU buffer with pages to test 1514 * 1515 * As a safety measure we check to make sure the data pages have not 1516 * been corrupted. 1517 */ 1518 static void rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) 1519 { 1520 struct list_head *head, *tmp; 1521 unsigned long buffer_cnt; 1522 unsigned long flags; 1523 int nr_loops = 0; 1524 1525 /* 1526 * Walk the linked list underpinning the ring buffer and validate all 1527 * its next and prev links. 1528 * 1529 * The check acquires the reader_lock to avoid concurrent processing 1530 * with code that could be modifying the list. However, the lock cannot 1531 * be held for the entire duration of the walk, as this would make the 1532 * time when interrupts are disabled non-deterministic, dependent on the 1533 * ring buffer size. Therefore, the code releases and re-acquires the 1534 * lock after checking each page. The ring_buffer_per_cpu.cnt variable 1535 * is then used to detect if the list was modified while the lock was 1536 * not held, in which case the check needs to be restarted. 1537 * 1538 * The code attempts to perform the check at most three times before 1539 * giving up. This is acceptable because this is only a self-validation 1540 * to detect problems early on. In practice, the list modification 1541 * operations are fairly spaced, and so this check typically succeeds at 1542 * most on the second try. 1543 */ 1544 again: 1545 if (++nr_loops > 3) 1546 return; 1547 1548 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 1549 head = rb_list_head(cpu_buffer->pages); 1550 if (!rb_check_links(cpu_buffer, head)) 1551 goto out_locked; 1552 buffer_cnt = cpu_buffer->cnt; 1553 tmp = head; 1554 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 1555 1556 while (true) { 1557 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 1558 1559 if (buffer_cnt != cpu_buffer->cnt) { 1560 /* The list was updated, try again. */ 1561 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 1562 goto again; 1563 } 1564 1565 tmp = rb_list_head(tmp->next); 1566 if (tmp == head) 1567 /* The iteration circled back, all is done. */ 1568 goto out_locked; 1569 1570 if (!rb_check_links(cpu_buffer, tmp)) 1571 goto out_locked; 1572 1573 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 1574 } 1575 1576 out_locked: 1577 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 1578 } 1579 1580 /* 1581 * Take an address, add the meta data size as well as the array of 1582 * array subbuffer indexes, then align it to a subbuffer size. 1583 * 1584 * This is used to help find the next per cpu subbuffer within a mapped range. 1585 */ 1586 static unsigned long 1587 rb_range_align_subbuf(unsigned long addr, int subbuf_size, int nr_subbufs) 1588 { 1589 addr += sizeof(struct ring_buffer_cpu_meta) + 1590 sizeof(int) * nr_subbufs; 1591 return ALIGN(addr, subbuf_size); 1592 } 1593 1594 /* 1595 * Return the ring_buffer_meta for a given @cpu. 1596 */ 1597 static void *rb_range_meta(struct trace_buffer *buffer, int nr_pages, int cpu) 1598 { 1599 int subbuf_size = buffer->subbuf_size + BUF_PAGE_HDR_SIZE; 1600 struct ring_buffer_cpu_meta *meta; 1601 struct ring_buffer_meta *bmeta; 1602 unsigned long ptr; 1603 int nr_subbufs; 1604 1605 bmeta = buffer->meta; 1606 if (!bmeta) 1607 return NULL; 1608 1609 ptr = (unsigned long)bmeta + bmeta->buffers_offset; 1610 meta = (struct ring_buffer_cpu_meta *)ptr; 1611 1612 /* When nr_pages passed in is zero, the first meta has already been initialized */ 1613 if (!nr_pages) { 1614 nr_subbufs = meta->nr_subbufs; 1615 } else { 1616 /* Include the reader page */ 1617 nr_subbufs = nr_pages + 1; 1618 } 1619 1620 /* 1621 * The first chunk may not be subbuffer aligned, where as 1622 * the rest of the chunks are. 1623 */ 1624 if (cpu) { 1625 ptr = rb_range_align_subbuf(ptr, subbuf_size, nr_subbufs); 1626 ptr += subbuf_size * nr_subbufs; 1627 1628 /* We can use multiplication to find chunks greater than 1 */ 1629 if (cpu > 1) { 1630 unsigned long size; 1631 unsigned long p; 1632 1633 /* Save the beginning of this CPU chunk */ 1634 p = ptr; 1635 ptr = rb_range_align_subbuf(ptr, subbuf_size, nr_subbufs); 1636 ptr += subbuf_size * nr_subbufs; 1637 1638 /* Now all chunks after this are the same size */ 1639 size = ptr - p; 1640 ptr += size * (cpu - 2); 1641 } 1642 } 1643 return (void *)ptr; 1644 } 1645 1646 /* Return the start of subbufs given the meta pointer */ 1647 static void *rb_subbufs_from_meta(struct ring_buffer_cpu_meta *meta) 1648 { 1649 int subbuf_size = meta->subbuf_size; 1650 unsigned long ptr; 1651 1652 ptr = (unsigned long)meta; 1653 ptr = rb_range_align_subbuf(ptr, subbuf_size, meta->nr_subbufs); 1654 1655 return (void *)ptr; 1656 } 1657 1658 /* 1659 * Return a specific sub-buffer for a given @cpu defined by @idx. 1660 */ 1661 static void *rb_range_buffer(struct ring_buffer_per_cpu *cpu_buffer, int idx) 1662 { 1663 struct ring_buffer_cpu_meta *meta; 1664 unsigned long ptr; 1665 int subbuf_size; 1666 1667 meta = rb_range_meta(cpu_buffer->buffer, 0, cpu_buffer->cpu); 1668 if (!meta) 1669 return NULL; 1670 1671 if (WARN_ON_ONCE(idx >= meta->nr_subbufs)) 1672 return NULL; 1673 1674 subbuf_size = meta->subbuf_size; 1675 1676 /* Map this buffer to the order that's in meta->buffers[] */ 1677 idx = meta->buffers[idx]; 1678 1679 ptr = (unsigned long)rb_subbufs_from_meta(meta); 1680 1681 ptr += subbuf_size * idx; 1682 if (ptr + subbuf_size > cpu_buffer->buffer->range_addr_end) 1683 return NULL; 1684 1685 return (void *)ptr; 1686 } 1687 1688 /* 1689 * See if the existing memory contains a valid meta section. 1690 * if so, use that, otherwise initialize it. 1691 */ 1692 static bool rb_meta_init(struct trace_buffer *buffer, int scratch_size) 1693 { 1694 unsigned long ptr = buffer->range_addr_start; 1695 struct ring_buffer_meta *bmeta; 1696 unsigned long total_size; 1697 int struct_sizes; 1698 1699 bmeta = (struct ring_buffer_meta *)ptr; 1700 buffer->meta = bmeta; 1701 1702 total_size = buffer->range_addr_end - buffer->range_addr_start; 1703 1704 struct_sizes = sizeof(struct ring_buffer_cpu_meta); 1705 struct_sizes |= sizeof(*bmeta) << 16; 1706 1707 /* The first buffer will start word size after the meta page */ 1708 ptr += sizeof(*bmeta); 1709 ptr = ALIGN(ptr, sizeof(long)); 1710 ptr += scratch_size; 1711 1712 if (bmeta->magic != RING_BUFFER_META_MAGIC) { 1713 pr_info("Ring buffer boot meta mismatch of magic\n"); 1714 goto init; 1715 } 1716 1717 if (bmeta->struct_sizes != struct_sizes) { 1718 pr_info("Ring buffer boot meta mismatch of struct size\n"); 1719 goto init; 1720 } 1721 1722 if (bmeta->total_size != total_size) { 1723 pr_info("Ring buffer boot meta mismatch of total size\n"); 1724 goto init; 1725 } 1726 1727 if (bmeta->buffers_offset > bmeta->total_size) { 1728 pr_info("Ring buffer boot meta mismatch of offset outside of total size\n"); 1729 goto init; 1730 } 1731 1732 if (bmeta->buffers_offset != (void *)ptr - (void *)bmeta) { 1733 pr_info("Ring buffer boot meta mismatch of first buffer offset\n"); 1734 goto init; 1735 } 1736 1737 return true; 1738 1739 init: 1740 bmeta->magic = RING_BUFFER_META_MAGIC; 1741 bmeta->struct_sizes = struct_sizes; 1742 bmeta->total_size = total_size; 1743 bmeta->buffers_offset = (void *)ptr - (void *)bmeta; 1744 1745 /* Zero out the scratch pad */ 1746 memset((void *)bmeta + sizeof(*bmeta), 0, bmeta->buffers_offset - sizeof(*bmeta)); 1747 1748 return false; 1749 } 1750 1751 /* 1752 * See if the existing memory contains valid ring buffer data. 1753 * As the previous kernel must be the same as this kernel, all 1754 * the calculations (size of buffers and number of buffers) 1755 * must be the same. 1756 */ 1757 static bool rb_cpu_meta_valid(struct ring_buffer_cpu_meta *meta, int cpu, 1758 struct trace_buffer *buffer, int nr_pages, 1759 unsigned long *subbuf_mask) 1760 { 1761 int subbuf_size = PAGE_SIZE; 1762 struct buffer_data_page *subbuf; 1763 unsigned long buffers_start; 1764 unsigned long buffers_end; 1765 int i; 1766 1767 if (!subbuf_mask) 1768 return false; 1769 1770 buffers_start = meta->first_buffer; 1771 buffers_end = meta->first_buffer + (subbuf_size * meta->nr_subbufs); 1772 1773 /* Is the head and commit buffers within the range of buffers? */ 1774 if (meta->head_buffer < buffers_start || 1775 meta->head_buffer >= buffers_end) { 1776 pr_info("Ring buffer boot meta [%d] head buffer out of range\n", cpu); 1777 return false; 1778 } 1779 1780 if (meta->commit_buffer < buffers_start || 1781 meta->commit_buffer >= buffers_end) { 1782 pr_info("Ring buffer boot meta [%d] commit buffer out of range\n", cpu); 1783 return false; 1784 } 1785 1786 subbuf = rb_subbufs_from_meta(meta); 1787 1788 bitmap_clear(subbuf_mask, 0, meta->nr_subbufs); 1789 1790 /* Is the meta buffers and the subbufs themselves have correct data? */ 1791 for (i = 0; i < meta->nr_subbufs; i++) { 1792 if (meta->buffers[i] < 0 || 1793 meta->buffers[i] >= meta->nr_subbufs) { 1794 pr_info("Ring buffer boot meta [%d] array out of range\n", cpu); 1795 return false; 1796 } 1797 1798 if ((unsigned)local_read(&subbuf->commit) > subbuf_size) { 1799 pr_info("Ring buffer boot meta [%d] buffer invalid commit\n", cpu); 1800 return false; 1801 } 1802 1803 if (test_bit(meta->buffers[i], subbuf_mask)) { 1804 pr_info("Ring buffer boot meta [%d] array has duplicates\n", cpu); 1805 return false; 1806 } 1807 1808 set_bit(meta->buffers[i], subbuf_mask); 1809 subbuf = (void *)subbuf + subbuf_size; 1810 } 1811 1812 return true; 1813 } 1814 1815 static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf); 1816 1817 static int rb_read_data_buffer(struct buffer_data_page *dpage, int tail, int cpu, 1818 unsigned long long *timestamp, u64 *delta_ptr) 1819 { 1820 struct ring_buffer_event *event; 1821 u64 ts, delta; 1822 int events = 0; 1823 int len; 1824 int e; 1825 1826 *delta_ptr = 0; 1827 *timestamp = 0; 1828 1829 ts = dpage->time_stamp; 1830 1831 for (e = 0; e < tail; e += len) { 1832 1833 event = (struct ring_buffer_event *)(dpage->data + e); 1834 len = rb_event_length(event); 1835 if (len <= 0 || len > tail - e) 1836 return -1; 1837 1838 switch (event->type_len) { 1839 1840 case RINGBUF_TYPE_TIME_EXTEND: 1841 delta = rb_event_time_stamp(event); 1842 ts += delta; 1843 break; 1844 1845 case RINGBUF_TYPE_TIME_STAMP: 1846 delta = rb_event_time_stamp(event); 1847 delta = rb_fix_abs_ts(delta, ts); 1848 if (delta < ts) { 1849 *delta_ptr = delta; 1850 *timestamp = ts; 1851 return -1; 1852 } 1853 ts = delta; 1854 break; 1855 1856 case RINGBUF_TYPE_PADDING: 1857 if (event->time_delta == 1) 1858 break; 1859 fallthrough; 1860 case RINGBUF_TYPE_DATA: 1861 events++; 1862 ts += event->time_delta; 1863 break; 1864 1865 default: 1866 return -1; 1867 } 1868 } 1869 *timestamp = ts; 1870 return events; 1871 } 1872 1873 static int rb_validate_buffer(struct buffer_data_page *dpage, int cpu) 1874 { 1875 unsigned long long ts; 1876 u64 delta; 1877 int tail; 1878 1879 tail = local_read(&dpage->commit); 1880 return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); 1881 } 1882 1883 /* If the meta data has been validated, now validate the events */ 1884 static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer) 1885 { 1886 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 1887 struct buffer_page *head_page, *orig_head, *orig_reader; 1888 unsigned long entry_bytes = 0; 1889 unsigned long entries = 0; 1890 int ret; 1891 u64 ts; 1892 int i; 1893 1894 if (!meta || !meta->head_buffer) 1895 return; 1896 1897 orig_head = head_page = cpu_buffer->head_page; 1898 orig_reader = cpu_buffer->reader_page; 1899 1900 /* Do the reader page first */ 1901 ret = rb_validate_buffer(orig_reader->page, cpu_buffer->cpu); 1902 if (ret < 0) { 1903 pr_info("Ring buffer reader page is invalid\n"); 1904 goto invalid; 1905 } 1906 entries += ret; 1907 entry_bytes += local_read(&orig_reader->page->commit); 1908 local_set(&orig_reader->entries, ret); 1909 1910 ts = head_page->page->time_stamp; 1911 1912 /* 1913 * Try to rewind the head so that we can read the pages which already 1914 * read in the previous boot. 1915 */ 1916 if (head_page == cpu_buffer->tail_page) 1917 goto skip_rewind; 1918 1919 rb_dec_page(&head_page); 1920 for (i = 0; i < meta->nr_subbufs + 1; i++, rb_dec_page(&head_page)) { 1921 1922 /* Rewind until tail (writer) page. */ 1923 if (head_page == cpu_buffer->tail_page) 1924 break; 1925 1926 /* Ensure the page has older data than head. */ 1927 if (ts < head_page->page->time_stamp) 1928 break; 1929 1930 ts = head_page->page->time_stamp; 1931 /* Ensure the page has correct timestamp and some data. */ 1932 if (!ts || rb_page_commit(head_page) == 0) 1933 break; 1934 1935 /* Stop rewind if the page is invalid. */ 1936 ret = rb_validate_buffer(head_page->page, cpu_buffer->cpu); 1937 if (ret < 0) 1938 break; 1939 1940 /* Recover the number of entries and update stats. */ 1941 local_set(&head_page->entries, ret); 1942 if (ret) 1943 local_inc(&cpu_buffer->pages_touched); 1944 entries += ret; 1945 entry_bytes += rb_page_commit(head_page); 1946 } 1947 if (i) 1948 pr_info("Ring buffer [%d] rewound %d pages\n", cpu_buffer->cpu, i); 1949 1950 /* The last rewound page must be skipped. */ 1951 if (head_page != orig_head) 1952 rb_inc_page(&head_page); 1953 1954 /* 1955 * If the ring buffer was rewound, then inject the reader page 1956 * into the location just before the original head page. 1957 */ 1958 if (head_page != orig_head) { 1959 struct buffer_page *bpage = orig_head; 1960 1961 rb_dec_page(&bpage); 1962 /* 1963 * Insert the reader_page before the original head page. 1964 * Since the list encode RB_PAGE flags, general list 1965 * operations should be avoided. 1966 */ 1967 cpu_buffer->reader_page->list.next = &orig_head->list; 1968 cpu_buffer->reader_page->list.prev = orig_head->list.prev; 1969 orig_head->list.prev = &cpu_buffer->reader_page->list; 1970 bpage->list.next = &cpu_buffer->reader_page->list; 1971 1972 /* Make the head_page the reader page */ 1973 cpu_buffer->reader_page = head_page; 1974 bpage = head_page; 1975 rb_inc_page(&head_page); 1976 head_page->list.prev = bpage->list.prev; 1977 rb_dec_page(&bpage); 1978 bpage->list.next = &head_page->list; 1979 rb_set_list_to_head(&bpage->list); 1980 cpu_buffer->pages = &head_page->list; 1981 1982 cpu_buffer->head_page = head_page; 1983 meta->head_buffer = (unsigned long)head_page->page; 1984 1985 /* Reset all the indexes */ 1986 bpage = cpu_buffer->reader_page; 1987 meta->buffers[0] = rb_meta_subbuf_idx(meta, bpage->page); 1988 bpage->id = 0; 1989 1990 for (i = 1, bpage = head_page; i < meta->nr_subbufs; 1991 i++, rb_inc_page(&bpage)) { 1992 meta->buffers[i] = rb_meta_subbuf_idx(meta, bpage->page); 1993 bpage->id = i; 1994 } 1995 1996 /* We'll restart verifying from orig_head */ 1997 head_page = orig_head; 1998 } 1999 2000 skip_rewind: 2001 /* If the commit_buffer is the reader page, update the commit page */ 2002 if (meta->commit_buffer == (unsigned long)cpu_buffer->reader_page->page) { 2003 cpu_buffer->commit_page = cpu_buffer->reader_page; 2004 /* Nothing more to do, the only page is the reader page */ 2005 goto done; 2006 } 2007 2008 /* Iterate until finding the commit page */ 2009 for (i = 0; i < meta->nr_subbufs + 1; i++, rb_inc_page(&head_page)) { 2010 2011 /* The original reader page has already been checked/counted. */ 2012 if (head_page == orig_reader) 2013 continue; 2014 2015 ret = rb_validate_buffer(head_page->page, cpu_buffer->cpu); 2016 if (ret < 0) { 2017 pr_info("Ring buffer meta [%d] invalid buffer page\n", 2018 cpu_buffer->cpu); 2019 goto invalid; 2020 } 2021 2022 /* If the buffer has content, update pages_touched */ 2023 if (ret) 2024 local_inc(&cpu_buffer->pages_touched); 2025 2026 entries += ret; 2027 entry_bytes += local_read(&head_page->page->commit); 2028 local_set(&head_page->entries, ret); 2029 2030 if (head_page == cpu_buffer->commit_page) 2031 break; 2032 } 2033 2034 if (head_page != cpu_buffer->commit_page) { 2035 pr_info("Ring buffer meta [%d] commit page not found\n", 2036 cpu_buffer->cpu); 2037 goto invalid; 2038 } 2039 done: 2040 local_set(&cpu_buffer->entries, entries); 2041 local_set(&cpu_buffer->entries_bytes, entry_bytes); 2042 2043 pr_info("Ring buffer meta [%d] is from previous boot!\n", cpu_buffer->cpu); 2044 return; 2045 2046 invalid: 2047 /* The content of the buffers are invalid, reset the meta data */ 2048 meta->head_buffer = 0; 2049 meta->commit_buffer = 0; 2050 2051 /* Reset the reader page */ 2052 local_set(&cpu_buffer->reader_page->entries, 0); 2053 local_set(&cpu_buffer->reader_page->page->commit, 0); 2054 2055 /* Reset all the subbuffers */ 2056 for (i = 0; i < meta->nr_subbufs - 1; i++, rb_inc_page(&head_page)) { 2057 local_set(&head_page->entries, 0); 2058 local_set(&head_page->page->commit, 0); 2059 } 2060 } 2061 2062 static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int scratch_size) 2063 { 2064 struct ring_buffer_cpu_meta *meta; 2065 unsigned long *subbuf_mask; 2066 unsigned long delta; 2067 void *subbuf; 2068 bool valid = false; 2069 int cpu; 2070 int i; 2071 2072 /* Create a mask to test the subbuf array */ 2073 subbuf_mask = bitmap_alloc(nr_pages + 1, GFP_KERNEL); 2074 /* If subbuf_mask fails to allocate, then rb_meta_valid() will return false */ 2075 2076 if (rb_meta_init(buffer, scratch_size)) 2077 valid = true; 2078 2079 for (cpu = 0; cpu < nr_cpu_ids; cpu++) { 2080 void *next_meta; 2081 2082 meta = rb_range_meta(buffer, nr_pages, cpu); 2083 2084 if (valid && rb_cpu_meta_valid(meta, cpu, buffer, nr_pages, subbuf_mask)) { 2085 /* Make the mappings match the current address */ 2086 subbuf = rb_subbufs_from_meta(meta); 2087 delta = (unsigned long)subbuf - meta->first_buffer; 2088 meta->first_buffer += delta; 2089 meta->head_buffer += delta; 2090 meta->commit_buffer += delta; 2091 continue; 2092 } 2093 2094 if (cpu < nr_cpu_ids - 1) 2095 next_meta = rb_range_meta(buffer, nr_pages, cpu + 1); 2096 else 2097 next_meta = (void *)buffer->range_addr_end; 2098 2099 memset(meta, 0, next_meta - (void *)meta); 2100 2101 meta->nr_subbufs = nr_pages + 1; 2102 meta->subbuf_size = PAGE_SIZE; 2103 2104 subbuf = rb_subbufs_from_meta(meta); 2105 2106 meta->first_buffer = (unsigned long)subbuf; 2107 2108 /* 2109 * The buffers[] array holds the order of the sub-buffers 2110 * that are after the meta data. The sub-buffers may 2111 * be swapped out when read and inserted into a different 2112 * location of the ring buffer. Although their addresses 2113 * remain the same, the buffers[] array contains the 2114 * index into the sub-buffers holding their actual order. 2115 */ 2116 for (i = 0; i < meta->nr_subbufs; i++) { 2117 meta->buffers[i] = i; 2118 rb_init_page(subbuf); 2119 subbuf += meta->subbuf_size; 2120 } 2121 } 2122 bitmap_free(subbuf_mask); 2123 } 2124 2125 static void *rbm_start(struct seq_file *m, loff_t *pos) 2126 { 2127 struct ring_buffer_per_cpu *cpu_buffer = m->private; 2128 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 2129 unsigned long val; 2130 2131 if (!meta) 2132 return NULL; 2133 2134 if (*pos > meta->nr_subbufs) 2135 return NULL; 2136 2137 val = *pos; 2138 val++; 2139 2140 return (void *)val; 2141 } 2142 2143 static void *rbm_next(struct seq_file *m, void *v, loff_t *pos) 2144 { 2145 (*pos)++; 2146 2147 return rbm_start(m, pos); 2148 } 2149 2150 static int rbm_show(struct seq_file *m, void *v) 2151 { 2152 struct ring_buffer_per_cpu *cpu_buffer = m->private; 2153 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 2154 unsigned long val = (unsigned long)v; 2155 2156 if (val == 1) { 2157 seq_printf(m, "head_buffer: %d\n", 2158 rb_meta_subbuf_idx(meta, (void *)meta->head_buffer)); 2159 seq_printf(m, "commit_buffer: %d\n", 2160 rb_meta_subbuf_idx(meta, (void *)meta->commit_buffer)); 2161 seq_printf(m, "subbuf_size: %d\n", meta->subbuf_size); 2162 seq_printf(m, "nr_subbufs: %d\n", meta->nr_subbufs); 2163 return 0; 2164 } 2165 2166 val -= 2; 2167 seq_printf(m, "buffer[%ld]: %d\n", val, meta->buffers[val]); 2168 2169 return 0; 2170 } 2171 2172 static void rbm_stop(struct seq_file *m, void *p) 2173 { 2174 } 2175 2176 static const struct seq_operations rb_meta_seq_ops = { 2177 .start = rbm_start, 2178 .next = rbm_next, 2179 .show = rbm_show, 2180 .stop = rbm_stop, 2181 }; 2182 2183 int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, int cpu) 2184 { 2185 struct seq_file *m; 2186 int ret; 2187 2188 ret = seq_open(file, &rb_meta_seq_ops); 2189 if (ret) 2190 return ret; 2191 2192 m = file->private_data; 2193 m->private = buffer->buffers[cpu]; 2194 2195 return 0; 2196 } 2197 2198 /* Map the buffer_pages to the previous head and commit pages */ 2199 static void rb_meta_buffer_update(struct ring_buffer_per_cpu *cpu_buffer, 2200 struct buffer_page *bpage) 2201 { 2202 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 2203 2204 if (meta->head_buffer == (unsigned long)bpage->page) 2205 cpu_buffer->head_page = bpage; 2206 2207 if (meta->commit_buffer == (unsigned long)bpage->page) { 2208 cpu_buffer->commit_page = bpage; 2209 cpu_buffer->tail_page = bpage; 2210 } 2211 } 2212 2213 static struct ring_buffer_desc *ring_buffer_desc(struct trace_buffer_desc *trace_desc, int cpu) 2214 { 2215 struct ring_buffer_desc *desc, *end; 2216 size_t len; 2217 int i; 2218 2219 if (!trace_desc) 2220 return NULL; 2221 2222 if (cpu >= trace_desc->nr_cpus) 2223 return NULL; 2224 2225 end = (struct ring_buffer_desc *)((void *)trace_desc + trace_desc->struct_len); 2226 desc = __first_ring_buffer_desc(trace_desc); 2227 len = struct_size(desc, page_va, desc->nr_page_va); 2228 desc = (struct ring_buffer_desc *)((void *)desc + (len * cpu)); 2229 2230 if (desc < end && desc->cpu == cpu) 2231 return desc; 2232 2233 /* Missing CPUs, need to linear search */ 2234 for_each_ring_buffer_desc(desc, i, trace_desc) { 2235 if (desc->cpu == cpu) 2236 return desc; 2237 } 2238 2239 return NULL; 2240 } 2241 2242 static void *ring_buffer_desc_page(struct ring_buffer_desc *desc, unsigned int page_id) 2243 { 2244 return page_id >= desc->nr_page_va ? NULL : (void *)desc->page_va[page_id]; 2245 } 2246 2247 static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, 2248 long nr_pages, struct list_head *pages) 2249 { 2250 struct trace_buffer *buffer = cpu_buffer->buffer; 2251 struct ring_buffer_cpu_meta *meta = NULL; 2252 struct buffer_page *bpage, *tmp; 2253 bool user_thread = current->mm != NULL; 2254 struct ring_buffer_desc *desc = NULL; 2255 long i; 2256 2257 /* 2258 * Check if the available memory is there first. 2259 * Note, si_mem_available() only gives us a rough estimate of available 2260 * memory. It may not be accurate. But we don't care, we just want 2261 * to prevent doing any allocation when it is obvious that it is 2262 * not going to succeed. 2263 */ 2264 i = si_mem_available(); 2265 if (i < nr_pages) 2266 return -ENOMEM; 2267 2268 /* 2269 * If a user thread allocates too much, and si_mem_available() 2270 * reports there's enough memory, even though there is not. 2271 * Make sure the OOM killer kills this thread. This can happen 2272 * even with RETRY_MAYFAIL because another task may be doing 2273 * an allocation after this task has taken all memory. 2274 * This is the task the OOM killer needs to take out during this 2275 * loop, even if it was triggered by an allocation somewhere else. 2276 */ 2277 if (user_thread) 2278 set_current_oom_origin(); 2279 2280 if (buffer->range_addr_start) 2281 meta = rb_range_meta(buffer, nr_pages, cpu_buffer->cpu); 2282 2283 if (buffer->remote) { 2284 desc = ring_buffer_desc(buffer->remote->desc, cpu_buffer->cpu); 2285 if (!desc || WARN_ON(desc->nr_page_va != (nr_pages + 1))) 2286 return -EINVAL; 2287 } 2288 2289 for (i = 0; i < nr_pages; i++) { 2290 2291 bpage = alloc_cpu_page(cpu_buffer->cpu); 2292 if (!bpage) 2293 goto free_pages; 2294 2295 rb_check_bpage(cpu_buffer, bpage); 2296 2297 /* 2298 * Append the pages as for mapped buffers we want to keep 2299 * the order 2300 */ 2301 list_add_tail(&bpage->list, pages); 2302 2303 if (meta) { 2304 /* A range was given. Use that for the buffer page */ 2305 bpage->page = rb_range_buffer(cpu_buffer, i + 1); 2306 if (!bpage->page) 2307 goto free_pages; 2308 /* If this is valid from a previous boot */ 2309 if (meta->head_buffer) 2310 rb_meta_buffer_update(cpu_buffer, bpage); 2311 bpage->range = 1; 2312 bpage->id = i + 1; 2313 } else if (desc) { 2314 void *p = ring_buffer_desc_page(desc, i + 1); 2315 2316 if (WARN_ON(!p)) 2317 goto free_pages; 2318 2319 bpage->page = p; 2320 bpage->range = 1; /* bpage->page can't be freed */ 2321 bpage->id = i + 1; 2322 cpu_buffer->subbuf_ids[i + 1] = bpage; 2323 } else { 2324 int order = cpu_buffer->buffer->subbuf_order; 2325 bpage->page = alloc_cpu_data(cpu_buffer->cpu, order); 2326 if (!bpage->page) 2327 goto free_pages; 2328 } 2329 bpage->order = cpu_buffer->buffer->subbuf_order; 2330 2331 if (user_thread && fatal_signal_pending(current)) 2332 goto free_pages; 2333 } 2334 if (user_thread) 2335 clear_current_oom_origin(); 2336 2337 return 0; 2338 2339 free_pages: 2340 list_for_each_entry_safe(bpage, tmp, pages, list) { 2341 list_del_init(&bpage->list); 2342 free_buffer_page(bpage); 2343 } 2344 if (user_thread) 2345 clear_current_oom_origin(); 2346 2347 return -ENOMEM; 2348 } 2349 2350 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, 2351 unsigned long nr_pages) 2352 { 2353 LIST_HEAD(pages); 2354 2355 WARN_ON(!nr_pages); 2356 2357 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages)) 2358 return -ENOMEM; 2359 2360 /* 2361 * The ring buffer page list is a circular list that does not 2362 * start and end with a list head. All page list items point to 2363 * other pages. 2364 */ 2365 cpu_buffer->pages = pages.next; 2366 list_del(&pages); 2367 2368 cpu_buffer->nr_pages = nr_pages; 2369 2370 rb_check_pages(cpu_buffer); 2371 2372 return 0; 2373 } 2374 2375 static struct ring_buffer_per_cpu * 2376 rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) 2377 { 2378 struct ring_buffer_per_cpu *cpu_buffer __free(kfree) = 2379 alloc_cpu_buffer(cpu); 2380 struct ring_buffer_cpu_meta *meta; 2381 struct buffer_page *bpage; 2382 int ret; 2383 2384 if (!cpu_buffer) 2385 return NULL; 2386 2387 cpu_buffer->cpu = cpu; 2388 cpu_buffer->buffer = buffer; 2389 raw_spin_lock_init(&cpu_buffer->reader_lock); 2390 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); 2391 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 2392 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler); 2393 init_completion(&cpu_buffer->update_done); 2394 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters); 2395 init_waitqueue_head(&cpu_buffer->irq_work.waiters); 2396 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); 2397 mutex_init(&cpu_buffer->mapping_lock); 2398 2399 bpage = alloc_cpu_page(cpu); 2400 if (!bpage) 2401 return NULL; 2402 2403 rb_check_bpage(cpu_buffer, bpage); 2404 2405 cpu_buffer->reader_page = bpage; 2406 2407 if (buffer->range_addr_start) { 2408 /* 2409 * Range mapped buffers have the same restrictions as memory 2410 * mapped ones do. 2411 */ 2412 cpu_buffer->mapped = 1; 2413 cpu_buffer->ring_meta = rb_range_meta(buffer, nr_pages, cpu); 2414 bpage->page = rb_range_buffer(cpu_buffer, 0); 2415 if (!bpage->page) 2416 goto fail_free_reader; 2417 if (cpu_buffer->ring_meta->head_buffer) 2418 rb_meta_buffer_update(cpu_buffer, bpage); 2419 bpage->range = 1; 2420 } else if (buffer->remote) { 2421 struct ring_buffer_desc *desc = ring_buffer_desc(buffer->remote->desc, cpu); 2422 2423 if (!desc) 2424 goto fail_free_reader; 2425 2426 cpu_buffer->remote = buffer->remote; 2427 cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va; 2428 cpu_buffer->nr_pages = nr_pages; 2429 cpu_buffer->subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1, 2430 sizeof(*cpu_buffer->subbuf_ids), GFP_KERNEL); 2431 if (!cpu_buffer->subbuf_ids) 2432 goto fail_free_reader; 2433 2434 /* Remote buffers are read-only and immutable */ 2435 atomic_inc(&cpu_buffer->record_disabled); 2436 atomic_inc(&cpu_buffer->resize_disabled); 2437 2438 bpage->page = ring_buffer_desc_page(desc, cpu_buffer->meta_page->reader.id); 2439 if (!bpage->page) 2440 goto fail_free_reader; 2441 2442 bpage->range = 1; 2443 cpu_buffer->subbuf_ids[0] = bpage; 2444 } else { 2445 int order = cpu_buffer->buffer->subbuf_order; 2446 bpage->page = alloc_cpu_data(cpu, order); 2447 if (!bpage->page) 2448 goto fail_free_reader; 2449 } 2450 2451 INIT_LIST_HEAD(&cpu_buffer->reader_page->list); 2452 INIT_LIST_HEAD(&cpu_buffer->new_pages); 2453 2454 ret = rb_allocate_pages(cpu_buffer, nr_pages); 2455 if (ret < 0) 2456 goto fail_free_reader; 2457 2458 rb_meta_validate_events(cpu_buffer); 2459 2460 /* If the boot meta was valid then this has already been updated */ 2461 meta = cpu_buffer->ring_meta; 2462 if (!meta || !meta->head_buffer || 2463 !cpu_buffer->head_page || !cpu_buffer->commit_page || !cpu_buffer->tail_page) { 2464 if (meta && meta->head_buffer && 2465 (cpu_buffer->head_page || cpu_buffer->commit_page || cpu_buffer->tail_page)) { 2466 pr_warn("Ring buffer meta buffers not all mapped\n"); 2467 if (!cpu_buffer->head_page) 2468 pr_warn(" Missing head_page\n"); 2469 if (!cpu_buffer->commit_page) 2470 pr_warn(" Missing commit_page\n"); 2471 if (!cpu_buffer->tail_page) 2472 pr_warn(" Missing tail_page\n"); 2473 } 2474 2475 cpu_buffer->head_page 2476 = list_entry(cpu_buffer->pages, struct buffer_page, list); 2477 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; 2478 2479 rb_head_page_activate(cpu_buffer); 2480 2481 if (cpu_buffer->ring_meta) 2482 meta->commit_buffer = meta->head_buffer; 2483 } else { 2484 /* The valid meta buffer still needs to activate the head page */ 2485 rb_head_page_activate(cpu_buffer); 2486 } 2487 2488 return_ptr(cpu_buffer); 2489 2490 fail_free_reader: 2491 free_buffer_page(cpu_buffer->reader_page); 2492 2493 return NULL; 2494 } 2495 2496 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) 2497 { 2498 struct list_head *head = cpu_buffer->pages; 2499 struct buffer_page *bpage, *tmp; 2500 2501 irq_work_sync(&cpu_buffer->irq_work.work); 2502 2503 if (cpu_buffer->remote) 2504 kfree(cpu_buffer->subbuf_ids); 2505 2506 free_buffer_page(cpu_buffer->reader_page); 2507 2508 if (head) { 2509 rb_head_page_deactivate(cpu_buffer); 2510 2511 list_for_each_entry_safe(bpage, tmp, head, list) { 2512 list_del_init(&bpage->list); 2513 free_buffer_page(bpage); 2514 } 2515 bpage = list_entry(head, struct buffer_page, list); 2516 free_buffer_page(bpage); 2517 } 2518 2519 free_page((unsigned long)cpu_buffer->free_page); 2520 2521 kfree(cpu_buffer); 2522 } 2523 2524 static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags, 2525 int order, unsigned long start, 2526 unsigned long end, 2527 unsigned long scratch_size, 2528 struct lock_class_key *key, 2529 struct ring_buffer_remote *remote) 2530 { 2531 struct trace_buffer *buffer __free(kfree) = NULL; 2532 long nr_pages; 2533 int subbuf_size; 2534 int bsize; 2535 int cpu; 2536 int ret; 2537 2538 /* keep it in its own cache line */ 2539 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), 2540 GFP_KERNEL); 2541 if (!buffer) 2542 return NULL; 2543 2544 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) 2545 return NULL; 2546 2547 buffer->subbuf_order = order; 2548 subbuf_size = (PAGE_SIZE << order); 2549 buffer->subbuf_size = subbuf_size - BUF_PAGE_HDR_SIZE; 2550 2551 /* Max payload is buffer page size - header (8bytes) */ 2552 buffer->max_data_size = buffer->subbuf_size - (sizeof(u32) * 2); 2553 2554 buffer->flags = flags; 2555 buffer->clock = trace_clock_local; 2556 buffer->reader_lock_key = key; 2557 2558 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters); 2559 init_waitqueue_head(&buffer->irq_work.waiters); 2560 2561 buffer->cpus = nr_cpu_ids; 2562 2563 bsize = sizeof(void *) * nr_cpu_ids; 2564 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), 2565 GFP_KERNEL); 2566 if (!buffer->buffers) 2567 goto fail_free_cpumask; 2568 2569 cpu = raw_smp_processor_id(); 2570 2571 /* If start/end are specified, then that overrides size */ 2572 if (start && end) { 2573 unsigned long buffers_start; 2574 unsigned long ptr; 2575 int n; 2576 2577 /* Make sure that start is word aligned */ 2578 start = ALIGN(start, sizeof(long)); 2579 2580 /* scratch_size needs to be aligned too */ 2581 scratch_size = ALIGN(scratch_size, sizeof(long)); 2582 2583 /* Subtract the buffer meta data and word aligned */ 2584 buffers_start = start + sizeof(struct ring_buffer_cpu_meta); 2585 buffers_start = ALIGN(buffers_start, sizeof(long)); 2586 buffers_start += scratch_size; 2587 2588 /* Calculate the size for the per CPU data */ 2589 size = end - buffers_start; 2590 size = size / nr_cpu_ids; 2591 2592 /* 2593 * The number of sub-buffers (nr_pages) is determined by the 2594 * total size allocated minus the meta data size. 2595 * Then that is divided by the number of per CPU buffers 2596 * needed, plus account for the integer array index that 2597 * will be appended to the meta data. 2598 */ 2599 nr_pages = (size - sizeof(struct ring_buffer_cpu_meta)) / 2600 (subbuf_size + sizeof(int)); 2601 /* Need at least two pages plus the reader page */ 2602 if (nr_pages < 3) 2603 goto fail_free_buffers; 2604 2605 again: 2606 /* Make sure that the size fits aligned */ 2607 for (n = 0, ptr = buffers_start; n < nr_cpu_ids; n++) { 2608 ptr += sizeof(struct ring_buffer_cpu_meta) + 2609 sizeof(int) * nr_pages; 2610 ptr = ALIGN(ptr, subbuf_size); 2611 ptr += subbuf_size * nr_pages; 2612 } 2613 if (ptr > end) { 2614 if (nr_pages <= 3) 2615 goto fail_free_buffers; 2616 nr_pages--; 2617 goto again; 2618 } 2619 2620 /* nr_pages should not count the reader page */ 2621 nr_pages--; 2622 buffer->range_addr_start = start; 2623 buffer->range_addr_end = end; 2624 2625 rb_range_meta_init(buffer, nr_pages, scratch_size); 2626 } else if (remote) { 2627 struct ring_buffer_desc *desc = ring_buffer_desc(remote->desc, cpu); 2628 2629 buffer->remote = remote; 2630 /* The writer is remote. This ring-buffer is read-only */ 2631 atomic_inc(&buffer->record_disabled); 2632 nr_pages = desc->nr_page_va - 1; 2633 if (nr_pages < 2) 2634 goto fail_free_buffers; 2635 } else { 2636 2637 /* need at least two pages */ 2638 nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size); 2639 if (nr_pages < 2) 2640 nr_pages = 2; 2641 } 2642 2643 cpumask_set_cpu(cpu, buffer->cpumask); 2644 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); 2645 if (!buffer->buffers[cpu]) 2646 goto fail_free_buffers; 2647 2648 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); 2649 if (ret < 0) 2650 goto fail_free_buffers; 2651 2652 mutex_init(&buffer->mutex); 2653 2654 return_ptr(buffer); 2655 2656 fail_free_buffers: 2657 for_each_buffer_cpu(buffer, cpu) { 2658 if (buffer->buffers[cpu]) 2659 rb_free_cpu_buffer(buffer->buffers[cpu]); 2660 } 2661 kfree(buffer->buffers); 2662 2663 fail_free_cpumask: 2664 free_cpumask_var(buffer->cpumask); 2665 2666 return NULL; 2667 } 2668 2669 /** 2670 * __ring_buffer_alloc - allocate a new ring_buffer 2671 * @size: the size in bytes per cpu that is needed. 2672 * @flags: attributes to set for the ring buffer. 2673 * @key: ring buffer reader_lock_key. 2674 * 2675 * Currently the only flag that is available is the RB_FL_OVERWRITE 2676 * flag. This flag means that the buffer will overwrite old data 2677 * when the buffer wraps. If this flag is not set, the buffer will 2678 * drop data when the tail hits the head. 2679 */ 2680 struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, 2681 struct lock_class_key *key) 2682 { 2683 /* Default buffer page size - one system page */ 2684 return alloc_buffer(size, flags, 0, 0, 0, 0, key, NULL); 2685 2686 } 2687 EXPORT_SYMBOL_GPL(__ring_buffer_alloc); 2688 2689 /** 2690 * __ring_buffer_alloc_range - allocate a new ring_buffer from existing memory 2691 * @size: the size in bytes per cpu that is needed. 2692 * @flags: attributes to set for the ring buffer. 2693 * @order: sub-buffer order 2694 * @start: start of allocated range 2695 * @range_size: size of allocated range 2696 * @scratch_size: size of scratch area (for preallocated memory buffers) 2697 * @key: ring buffer reader_lock_key. 2698 * 2699 * Currently the only flag that is available is the RB_FL_OVERWRITE 2700 * flag. This flag means that the buffer will overwrite old data 2701 * when the buffer wraps. If this flag is not set, the buffer will 2702 * drop data when the tail hits the head. 2703 */ 2704 struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flags, 2705 int order, unsigned long start, 2706 unsigned long range_size, 2707 unsigned long scratch_size, 2708 struct lock_class_key *key) 2709 { 2710 return alloc_buffer(size, flags, order, start, start + range_size, 2711 scratch_size, key, NULL); 2712 } 2713 2714 /** 2715 * __ring_buffer_alloc_remote - allocate a new ring_buffer from a remote 2716 * @remote: Contains a description of the ring-buffer pages and remote callbacks. 2717 * @key: ring buffer reader_lock_key. 2718 */ 2719 struct trace_buffer *__ring_buffer_alloc_remote(struct ring_buffer_remote *remote, 2720 struct lock_class_key *key) 2721 { 2722 return alloc_buffer(0, 0, 0, 0, 0, 0, key, remote); 2723 } 2724 2725 void *ring_buffer_meta_scratch(struct trace_buffer *buffer, unsigned int *size) 2726 { 2727 struct ring_buffer_meta *meta; 2728 void *ptr; 2729 2730 if (!buffer || !buffer->meta) 2731 return NULL; 2732 2733 meta = buffer->meta; 2734 2735 ptr = (void *)ALIGN((unsigned long)meta + sizeof(*meta), sizeof(long)); 2736 2737 if (size) 2738 *size = (void *)meta + meta->buffers_offset - ptr; 2739 2740 return ptr; 2741 } 2742 2743 /** 2744 * ring_buffer_free - free a ring buffer. 2745 * @buffer: the buffer to free. 2746 */ 2747 void 2748 ring_buffer_free(struct trace_buffer *buffer) 2749 { 2750 int cpu; 2751 2752 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); 2753 2754 irq_work_sync(&buffer->irq_work.work); 2755 2756 for_each_buffer_cpu(buffer, cpu) 2757 rb_free_cpu_buffer(buffer->buffers[cpu]); 2758 2759 kfree(buffer->buffers); 2760 free_cpumask_var(buffer->cpumask); 2761 2762 kfree(buffer); 2763 } 2764 EXPORT_SYMBOL_GPL(ring_buffer_free); 2765 2766 void ring_buffer_set_clock(struct trace_buffer *buffer, 2767 u64 (*clock)(void)) 2768 { 2769 buffer->clock = clock; 2770 } 2771 2772 void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs) 2773 { 2774 buffer->time_stamp_abs = abs; 2775 } 2776 2777 bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer) 2778 { 2779 return buffer->time_stamp_abs; 2780 } 2781 2782 static inline unsigned long rb_page_entries(struct buffer_page *bpage) 2783 { 2784 return local_read(&bpage->entries) & RB_WRITE_MASK; 2785 } 2786 2787 static inline unsigned long rb_page_write(struct buffer_page *bpage) 2788 { 2789 return local_read(&bpage->write) & RB_WRITE_MASK; 2790 } 2791 2792 static bool 2793 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages) 2794 { 2795 struct list_head *tail_page, *to_remove, *next_page; 2796 struct buffer_page *to_remove_page, *tmp_iter_page; 2797 struct buffer_page *last_page, *first_page; 2798 unsigned long nr_removed; 2799 unsigned long head_bit; 2800 int page_entries; 2801 2802 head_bit = 0; 2803 2804 raw_spin_lock_irq(&cpu_buffer->reader_lock); 2805 atomic_inc(&cpu_buffer->record_disabled); 2806 /* 2807 * We don't race with the readers since we have acquired the reader 2808 * lock. We also don't race with writers after disabling recording. 2809 * This makes it easy to figure out the first and the last page to be 2810 * removed from the list. We unlink all the pages in between including 2811 * the first and last pages. This is done in a busy loop so that we 2812 * lose the least number of traces. 2813 * The pages are freed after we restart recording and unlock readers. 2814 */ 2815 tail_page = &cpu_buffer->tail_page->list; 2816 2817 /* 2818 * tail page might be on reader page, we remove the next page 2819 * from the ring buffer 2820 */ 2821 if (cpu_buffer->tail_page == cpu_buffer->reader_page) 2822 tail_page = rb_list_head(tail_page->next); 2823 to_remove = tail_page; 2824 2825 /* start of pages to remove */ 2826 first_page = list_entry(rb_list_head(to_remove->next), 2827 struct buffer_page, list); 2828 2829 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) { 2830 to_remove = rb_list_head(to_remove)->next; 2831 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD; 2832 } 2833 /* Read iterators need to reset themselves when some pages removed */ 2834 cpu_buffer->pages_removed += nr_removed; 2835 2836 next_page = rb_list_head(to_remove)->next; 2837 2838 /* 2839 * Now we remove all pages between tail_page and next_page. 2840 * Make sure that we have head_bit value preserved for the 2841 * next page 2842 */ 2843 tail_page->next = (struct list_head *)((unsigned long)next_page | 2844 head_bit); 2845 next_page = rb_list_head(next_page); 2846 next_page->prev = tail_page; 2847 2848 /* make sure pages points to a valid page in the ring buffer */ 2849 cpu_buffer->pages = next_page; 2850 cpu_buffer->cnt++; 2851 2852 /* update head page */ 2853 if (head_bit) 2854 cpu_buffer->head_page = list_entry(next_page, 2855 struct buffer_page, list); 2856 2857 /* pages are removed, resume tracing and then free the pages */ 2858 atomic_dec(&cpu_buffer->record_disabled); 2859 raw_spin_unlock_irq(&cpu_buffer->reader_lock); 2860 2861 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)); 2862 2863 /* last buffer page to remove */ 2864 last_page = list_entry(rb_list_head(to_remove), struct buffer_page, 2865 list); 2866 tmp_iter_page = first_page; 2867 2868 do { 2869 cond_resched(); 2870 2871 to_remove_page = tmp_iter_page; 2872 rb_inc_page(&tmp_iter_page); 2873 2874 /* update the counters */ 2875 page_entries = rb_page_entries(to_remove_page); 2876 if (page_entries) { 2877 /* 2878 * If something was added to this page, it was full 2879 * since it is not the tail page. So we deduct the 2880 * bytes consumed in ring buffer from here. 2881 * Increment overrun to account for the lost events. 2882 */ 2883 local_add(page_entries, &cpu_buffer->overrun); 2884 local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes); 2885 local_inc(&cpu_buffer->pages_lost); 2886 } 2887 2888 /* 2889 * We have already removed references to this list item, just 2890 * free up the buffer_page and its page 2891 */ 2892 free_buffer_page(to_remove_page); 2893 nr_removed--; 2894 2895 } while (to_remove_page != last_page); 2896 2897 RB_WARN_ON(cpu_buffer, nr_removed); 2898 2899 return nr_removed == 0; 2900 } 2901 2902 static bool 2903 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) 2904 { 2905 struct list_head *pages = &cpu_buffer->new_pages; 2906 unsigned long flags; 2907 bool success; 2908 int retries; 2909 2910 /* Can be called at early boot up, where interrupts must not been enabled */ 2911 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 2912 /* 2913 * We are holding the reader lock, so the reader page won't be swapped 2914 * in the ring buffer. Now we are racing with the writer trying to 2915 * move head page and the tail page. 2916 * We are going to adapt the reader page update process where: 2917 * 1. We first splice the start and end of list of new pages between 2918 * the head page and its previous page. 2919 * 2. We cmpxchg the prev_page->next to point from head page to the 2920 * start of new pages list. 2921 * 3. Finally, we update the head->prev to the end of new list. 2922 * 2923 * We will try this process 10 times, to make sure that we don't keep 2924 * spinning. 2925 */ 2926 retries = 10; 2927 success = false; 2928 while (retries--) { 2929 struct list_head *head_page, *prev_page; 2930 struct list_head *last_page, *first_page; 2931 struct list_head *head_page_with_bit; 2932 struct buffer_page *hpage = rb_set_head_page(cpu_buffer); 2933 2934 if (!hpage) 2935 break; 2936 head_page = &hpage->list; 2937 prev_page = head_page->prev; 2938 2939 first_page = pages->next; 2940 last_page = pages->prev; 2941 2942 head_page_with_bit = (struct list_head *) 2943 ((unsigned long)head_page | RB_PAGE_HEAD); 2944 2945 last_page->next = head_page_with_bit; 2946 first_page->prev = prev_page; 2947 2948 /* caution: head_page_with_bit gets updated on cmpxchg failure */ 2949 if (try_cmpxchg(&prev_page->next, 2950 &head_page_with_bit, first_page)) { 2951 /* 2952 * yay, we replaced the page pointer to our new list, 2953 * now, we just have to update to head page's prev 2954 * pointer to point to end of list 2955 */ 2956 head_page->prev = last_page; 2957 cpu_buffer->cnt++; 2958 success = true; 2959 break; 2960 } 2961 } 2962 2963 if (success) 2964 INIT_LIST_HEAD(pages); 2965 /* 2966 * If we weren't successful in adding in new pages, warn and stop 2967 * tracing 2968 */ 2969 RB_WARN_ON(cpu_buffer, !success); 2970 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 2971 2972 /* free pages if they weren't inserted */ 2973 if (!success) { 2974 struct buffer_page *bpage, *tmp; 2975 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, 2976 list) { 2977 list_del_init(&bpage->list); 2978 free_buffer_page(bpage); 2979 } 2980 } 2981 return success; 2982 } 2983 2984 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer) 2985 { 2986 bool success; 2987 2988 if (cpu_buffer->nr_pages_to_update > 0) 2989 success = rb_insert_pages(cpu_buffer); 2990 else 2991 success = rb_remove_pages(cpu_buffer, 2992 -cpu_buffer->nr_pages_to_update); 2993 2994 if (success) 2995 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update; 2996 } 2997 2998 static void update_pages_handler(struct work_struct *work) 2999 { 3000 struct ring_buffer_per_cpu *cpu_buffer = container_of(work, 3001 struct ring_buffer_per_cpu, update_pages_work); 3002 rb_update_pages(cpu_buffer); 3003 complete(&cpu_buffer->update_done); 3004 } 3005 3006 /** 3007 * ring_buffer_resize - resize the ring buffer 3008 * @buffer: the buffer to resize. 3009 * @size: the new size. 3010 * @cpu_id: the cpu buffer to resize 3011 * 3012 * Minimum size is 2 * buffer->subbuf_size. 3013 * 3014 * Returns 0 on success and < 0 on failure. 3015 */ 3016 int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, 3017 int cpu_id) 3018 { 3019 struct ring_buffer_per_cpu *cpu_buffer; 3020 unsigned long nr_pages; 3021 int cpu, err; 3022 3023 /* 3024 * Always succeed at resizing a non-existent buffer: 3025 */ 3026 if (!buffer) 3027 return 0; 3028 3029 /* Make sure the requested buffer exists */ 3030 if (cpu_id != RING_BUFFER_ALL_CPUS && 3031 !cpumask_test_cpu(cpu_id, buffer->cpumask)) 3032 return 0; 3033 3034 nr_pages = DIV_ROUND_UP(size, buffer->subbuf_size); 3035 3036 /* we need a minimum of two pages */ 3037 if (nr_pages < 2) 3038 nr_pages = 2; 3039 3040 /* 3041 * Keep CPUs from coming online while resizing to synchronize 3042 * with new per CPU buffers being created. 3043 */ 3044 guard(cpus_read_lock)(); 3045 3046 /* prevent another thread from changing buffer sizes */ 3047 mutex_lock(&buffer->mutex); 3048 atomic_inc(&buffer->resizing); 3049 3050 if (cpu_id == RING_BUFFER_ALL_CPUS) { 3051 /* 3052 * Don't succeed if resizing is disabled, as a reader might be 3053 * manipulating the ring buffer and is expecting a sane state while 3054 * this is true. 3055 */ 3056 for_each_buffer_cpu(buffer, cpu) { 3057 cpu_buffer = buffer->buffers[cpu]; 3058 if (atomic_read(&cpu_buffer->resize_disabled)) { 3059 err = -EBUSY; 3060 goto out_err_unlock; 3061 } 3062 } 3063 3064 /* calculate the pages to update */ 3065 for_each_buffer_cpu(buffer, cpu) { 3066 cpu_buffer = buffer->buffers[cpu]; 3067 3068 cpu_buffer->nr_pages_to_update = nr_pages - 3069 cpu_buffer->nr_pages; 3070 /* 3071 * nothing more to do for removing pages or no update 3072 */ 3073 if (cpu_buffer->nr_pages_to_update <= 0) 3074 continue; 3075 /* 3076 * to add pages, make sure all new pages can be 3077 * allocated without receiving ENOMEM 3078 */ 3079 INIT_LIST_HEAD(&cpu_buffer->new_pages); 3080 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update, 3081 &cpu_buffer->new_pages)) { 3082 /* not enough memory for new pages */ 3083 err = -ENOMEM; 3084 goto out_err; 3085 } 3086 3087 cond_resched(); 3088 } 3089 3090 /* 3091 * Fire off all the required work handlers 3092 * We can't schedule on offline CPUs, but it's not necessary 3093 * since we can change their buffer sizes without any race. 3094 */ 3095 for_each_buffer_cpu(buffer, cpu) { 3096 cpu_buffer = buffer->buffers[cpu]; 3097 if (!cpu_buffer->nr_pages_to_update) 3098 continue; 3099 3100 /* Can't run something on an offline CPU. */ 3101 if (!cpu_online(cpu)) { 3102 rb_update_pages(cpu_buffer); 3103 cpu_buffer->nr_pages_to_update = 0; 3104 } else { 3105 /* Run directly if possible. */ 3106 migrate_disable(); 3107 if (cpu != smp_processor_id()) { 3108 migrate_enable(); 3109 schedule_work_on(cpu, 3110 &cpu_buffer->update_pages_work); 3111 } else { 3112 update_pages_handler(&cpu_buffer->update_pages_work); 3113 migrate_enable(); 3114 } 3115 } 3116 } 3117 3118 /* wait for all the updates to complete */ 3119 for_each_buffer_cpu(buffer, cpu) { 3120 cpu_buffer = buffer->buffers[cpu]; 3121 if (!cpu_buffer->nr_pages_to_update) 3122 continue; 3123 3124 if (cpu_online(cpu)) 3125 wait_for_completion(&cpu_buffer->update_done); 3126 cpu_buffer->nr_pages_to_update = 0; 3127 } 3128 3129 } else { 3130 cpu_buffer = buffer->buffers[cpu_id]; 3131 3132 if (nr_pages == cpu_buffer->nr_pages) 3133 goto out; 3134 3135 /* 3136 * Don't succeed if resizing is disabled, as a reader might be 3137 * manipulating the ring buffer and is expecting a sane state while 3138 * this is true. 3139 */ 3140 if (atomic_read(&cpu_buffer->resize_disabled)) { 3141 err = -EBUSY; 3142 goto out_err_unlock; 3143 } 3144 3145 cpu_buffer->nr_pages_to_update = nr_pages - 3146 cpu_buffer->nr_pages; 3147 3148 INIT_LIST_HEAD(&cpu_buffer->new_pages); 3149 if (cpu_buffer->nr_pages_to_update > 0 && 3150 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update, 3151 &cpu_buffer->new_pages)) { 3152 err = -ENOMEM; 3153 goto out_err; 3154 } 3155 3156 /* Can't run something on an offline CPU. */ 3157 if (!cpu_online(cpu_id)) 3158 rb_update_pages(cpu_buffer); 3159 else { 3160 /* Run directly if possible. */ 3161 migrate_disable(); 3162 if (cpu_id == smp_processor_id()) { 3163 rb_update_pages(cpu_buffer); 3164 migrate_enable(); 3165 } else { 3166 migrate_enable(); 3167 schedule_work_on(cpu_id, 3168 &cpu_buffer->update_pages_work); 3169 wait_for_completion(&cpu_buffer->update_done); 3170 } 3171 } 3172 3173 cpu_buffer->nr_pages_to_update = 0; 3174 } 3175 3176 out: 3177 /* 3178 * The ring buffer resize can happen with the ring buffer 3179 * enabled, so that the update disturbs the tracing as little 3180 * as possible. But if the buffer is disabled, we do not need 3181 * to worry about that, and we can take the time to verify 3182 * that the buffer is not corrupt. 3183 */ 3184 if (atomic_read(&buffer->record_disabled)) { 3185 atomic_inc(&buffer->record_disabled); 3186 /* 3187 * Even though the buffer was disabled, we must make sure 3188 * that it is truly disabled before calling rb_check_pages. 3189 * There could have been a race between checking 3190 * record_disable and incrementing it. 3191 */ 3192 synchronize_rcu(); 3193 for_each_buffer_cpu(buffer, cpu) { 3194 cpu_buffer = buffer->buffers[cpu]; 3195 rb_check_pages(cpu_buffer); 3196 } 3197 atomic_dec(&buffer->record_disabled); 3198 } 3199 3200 atomic_dec(&buffer->resizing); 3201 mutex_unlock(&buffer->mutex); 3202 return 0; 3203 3204 out_err: 3205 for_each_buffer_cpu(buffer, cpu) { 3206 struct buffer_page *bpage, *tmp; 3207 3208 cpu_buffer = buffer->buffers[cpu]; 3209 cpu_buffer->nr_pages_to_update = 0; 3210 3211 if (list_empty(&cpu_buffer->new_pages)) 3212 continue; 3213 3214 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, 3215 list) { 3216 list_del_init(&bpage->list); 3217 free_buffer_page(bpage); 3218 3219 cond_resched(); 3220 } 3221 } 3222 out_err_unlock: 3223 atomic_dec(&buffer->resizing); 3224 mutex_unlock(&buffer->mutex); 3225 return err; 3226 } 3227 EXPORT_SYMBOL_GPL(ring_buffer_resize); 3228 3229 void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val) 3230 { 3231 mutex_lock(&buffer->mutex); 3232 if (val) 3233 buffer->flags |= RB_FL_OVERWRITE; 3234 else 3235 buffer->flags &= ~RB_FL_OVERWRITE; 3236 mutex_unlock(&buffer->mutex); 3237 } 3238 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite); 3239 3240 static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) 3241 { 3242 return bpage->page->data + index; 3243 } 3244 3245 static __always_inline struct ring_buffer_event * 3246 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) 3247 { 3248 return __rb_page_index(cpu_buffer->reader_page, 3249 cpu_buffer->reader_page->read); 3250 } 3251 3252 static struct ring_buffer_event * 3253 rb_iter_head_event(struct ring_buffer_iter *iter) 3254 { 3255 struct ring_buffer_event *event; 3256 struct buffer_page *iter_head_page = iter->head_page; 3257 unsigned long commit; 3258 unsigned length; 3259 3260 if (iter->head != iter->next_event) 3261 return iter->event; 3262 3263 /* 3264 * When the writer goes across pages, it issues a cmpxchg which 3265 * is a mb(), which will synchronize with the rmb here. 3266 * (see rb_tail_page_update() and __rb_reserve_next()) 3267 */ 3268 commit = rb_page_commit(iter_head_page); 3269 smp_rmb(); 3270 3271 /* An event needs to be at least 8 bytes in size */ 3272 if (iter->head > commit - 8) 3273 goto reset; 3274 3275 event = __rb_page_index(iter_head_page, iter->head); 3276 length = rb_event_length(event); 3277 3278 /* 3279 * READ_ONCE() doesn't work on functions and we don't want the 3280 * compiler doing any crazy optimizations with length. 3281 */ 3282 barrier(); 3283 3284 if ((iter->head + length) > commit || length > iter->event_size) 3285 /* Writer corrupted the read? */ 3286 goto reset; 3287 3288 memcpy(iter->event, event, length); 3289 /* 3290 * If the page stamp is still the same after this rmb() then the 3291 * event was safely copied without the writer entering the page. 3292 */ 3293 smp_rmb(); 3294 3295 /* Make sure the page didn't change since we read this */ 3296 if (iter->page_stamp != iter_head_page->page->time_stamp || 3297 commit > rb_page_commit(iter_head_page)) 3298 goto reset; 3299 3300 iter->next_event = iter->head + length; 3301 return iter->event; 3302 reset: 3303 /* Reset to the beginning */ 3304 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; 3305 iter->head = 0; 3306 iter->next_event = 0; 3307 iter->missed_events = 1; 3308 return NULL; 3309 } 3310 3311 /* Size is determined by what has been committed */ 3312 static __always_inline unsigned rb_page_size(struct buffer_page *bpage) 3313 { 3314 return rb_page_commit(bpage) & ~RB_MISSED_MASK; 3315 } 3316 3317 static __always_inline unsigned 3318 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) 3319 { 3320 return rb_page_commit(cpu_buffer->commit_page); 3321 } 3322 3323 static __always_inline unsigned 3324 rb_event_index(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event *event) 3325 { 3326 unsigned long addr = (unsigned long)event; 3327 3328 addr &= (PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1; 3329 3330 return addr - BUF_PAGE_HDR_SIZE; 3331 } 3332 3333 static void rb_inc_iter(struct ring_buffer_iter *iter) 3334 { 3335 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 3336 3337 /* 3338 * The iterator could be on the reader page (it starts there). 3339 * But the head could have moved, since the reader was 3340 * found. Check for this case and assign the iterator 3341 * to the head page instead of next. 3342 */ 3343 if (iter->head_page == cpu_buffer->reader_page) 3344 iter->head_page = rb_set_head_page(cpu_buffer); 3345 else 3346 rb_inc_page(&iter->head_page); 3347 3348 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; 3349 iter->head = 0; 3350 iter->next_event = 0; 3351 } 3352 3353 /* Return the index into the sub-buffers for a given sub-buffer */ 3354 static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf) 3355 { 3356 void *subbuf_array; 3357 3358 subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs; 3359 subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size); 3360 return (subbuf - subbuf_array) / meta->subbuf_size; 3361 } 3362 3363 static void rb_update_meta_head(struct ring_buffer_per_cpu *cpu_buffer, 3364 struct buffer_page *next_page) 3365 { 3366 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 3367 unsigned long old_head = (unsigned long)next_page->page; 3368 unsigned long new_head; 3369 3370 rb_inc_page(&next_page); 3371 new_head = (unsigned long)next_page->page; 3372 3373 /* 3374 * Only move it forward once, if something else came in and 3375 * moved it forward, then we don't want to touch it. 3376 */ 3377 (void)cmpxchg(&meta->head_buffer, old_head, new_head); 3378 } 3379 3380 static void rb_update_meta_reader(struct ring_buffer_per_cpu *cpu_buffer, 3381 struct buffer_page *reader) 3382 { 3383 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 3384 void *old_reader = cpu_buffer->reader_page->page; 3385 void *new_reader = reader->page; 3386 int id; 3387 3388 id = reader->id; 3389 cpu_buffer->reader_page->id = id; 3390 reader->id = 0; 3391 3392 meta->buffers[0] = rb_meta_subbuf_idx(meta, new_reader); 3393 meta->buffers[id] = rb_meta_subbuf_idx(meta, old_reader); 3394 3395 /* The head pointer is the one after the reader */ 3396 rb_update_meta_head(cpu_buffer, reader); 3397 } 3398 3399 /* 3400 * rb_handle_head_page - writer hit the head page 3401 * 3402 * Returns: +1 to retry page 3403 * 0 to continue 3404 * -1 on error 3405 */ 3406 static int 3407 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, 3408 struct buffer_page *tail_page, 3409 struct buffer_page *next_page) 3410 { 3411 struct buffer_page *new_head; 3412 int entries; 3413 int type; 3414 int ret; 3415 3416 entries = rb_page_entries(next_page); 3417 3418 /* 3419 * The hard part is here. We need to move the head 3420 * forward, and protect against both readers on 3421 * other CPUs and writers coming in via interrupts. 3422 */ 3423 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, 3424 RB_PAGE_HEAD); 3425 3426 /* 3427 * type can be one of four: 3428 * NORMAL - an interrupt already moved it for us 3429 * HEAD - we are the first to get here. 3430 * UPDATE - we are the interrupt interrupting 3431 * a current move. 3432 * MOVED - a reader on another CPU moved the next 3433 * pointer to its reader page. Give up 3434 * and try again. 3435 */ 3436 3437 switch (type) { 3438 case RB_PAGE_HEAD: 3439 /* 3440 * We changed the head to UPDATE, thus 3441 * it is our responsibility to update 3442 * the counters. 3443 */ 3444 local_add(entries, &cpu_buffer->overrun); 3445 local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes); 3446 local_inc(&cpu_buffer->pages_lost); 3447 3448 if (cpu_buffer->ring_meta) 3449 rb_update_meta_head(cpu_buffer, next_page); 3450 /* 3451 * The entries will be zeroed out when we move the 3452 * tail page. 3453 */ 3454 3455 /* still more to do */ 3456 break; 3457 3458 case RB_PAGE_UPDATE: 3459 /* 3460 * This is an interrupt that interrupt the 3461 * previous update. Still more to do. 3462 */ 3463 break; 3464 case RB_PAGE_NORMAL: 3465 /* 3466 * An interrupt came in before the update 3467 * and processed this for us. 3468 * Nothing left to do. 3469 */ 3470 return 1; 3471 case RB_PAGE_MOVED: 3472 /* 3473 * The reader is on another CPU and just did 3474 * a swap with our next_page. 3475 * Try again. 3476 */ 3477 return 1; 3478 default: 3479 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ 3480 return -1; 3481 } 3482 3483 /* 3484 * Now that we are here, the old head pointer is 3485 * set to UPDATE. This will keep the reader from 3486 * swapping the head page with the reader page. 3487 * The reader (on another CPU) will spin till 3488 * we are finished. 3489 * 3490 * We just need to protect against interrupts 3491 * doing the job. We will set the next pointer 3492 * to HEAD. After that, we set the old pointer 3493 * to NORMAL, but only if it was HEAD before. 3494 * otherwise we are an interrupt, and only 3495 * want the outer most commit to reset it. 3496 */ 3497 new_head = next_page; 3498 rb_inc_page(&new_head); 3499 3500 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, 3501 RB_PAGE_NORMAL); 3502 3503 /* 3504 * Valid returns are: 3505 * HEAD - an interrupt came in and already set it. 3506 * NORMAL - One of two things: 3507 * 1) We really set it. 3508 * 2) A bunch of interrupts came in and moved 3509 * the page forward again. 3510 */ 3511 switch (ret) { 3512 case RB_PAGE_HEAD: 3513 case RB_PAGE_NORMAL: 3514 /* OK */ 3515 break; 3516 default: 3517 RB_WARN_ON(cpu_buffer, 1); 3518 return -1; 3519 } 3520 3521 /* 3522 * It is possible that an interrupt came in, 3523 * set the head up, then more interrupts came in 3524 * and moved it again. When we get back here, 3525 * the page would have been set to NORMAL but we 3526 * just set it back to HEAD. 3527 * 3528 * How do you detect this? Well, if that happened 3529 * the tail page would have moved. 3530 */ 3531 if (ret == RB_PAGE_NORMAL) { 3532 struct buffer_page *buffer_tail_page; 3533 3534 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page); 3535 /* 3536 * If the tail had moved passed next, then we need 3537 * to reset the pointer. 3538 */ 3539 if (buffer_tail_page != tail_page && 3540 buffer_tail_page != next_page) 3541 rb_head_page_set_normal(cpu_buffer, new_head, 3542 next_page, 3543 RB_PAGE_HEAD); 3544 } 3545 3546 /* 3547 * If this was the outer most commit (the one that 3548 * changed the original pointer from HEAD to UPDATE), 3549 * then it is up to us to reset it to NORMAL. 3550 */ 3551 if (type == RB_PAGE_HEAD) { 3552 ret = rb_head_page_set_normal(cpu_buffer, next_page, 3553 tail_page, 3554 RB_PAGE_UPDATE); 3555 if (RB_WARN_ON(cpu_buffer, 3556 ret != RB_PAGE_UPDATE)) 3557 return -1; 3558 } 3559 3560 return 0; 3561 } 3562 3563 static inline void 3564 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, 3565 unsigned long tail, struct rb_event_info *info) 3566 { 3567 unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size); 3568 struct buffer_page *tail_page = info->tail_page; 3569 struct ring_buffer_event *event; 3570 unsigned long length = info->length; 3571 3572 /* 3573 * Only the event that crossed the page boundary 3574 * must fill the old tail_page with padding. 3575 */ 3576 if (tail >= bsize) { 3577 /* 3578 * If the page was filled, then we still need 3579 * to update the real_end. Reset it to zero 3580 * and the reader will ignore it. 3581 */ 3582 if (tail == bsize) 3583 tail_page->real_end = 0; 3584 3585 local_sub(length, &tail_page->write); 3586 return; 3587 } 3588 3589 event = __rb_page_index(tail_page, tail); 3590 3591 /* 3592 * Save the original length to the meta data. 3593 * This will be used by the reader to add lost event 3594 * counter. 3595 */ 3596 tail_page->real_end = tail; 3597 3598 /* 3599 * If this event is bigger than the minimum size, then 3600 * we need to be careful that we don't subtract the 3601 * write counter enough to allow another writer to slip 3602 * in on this page. 3603 * We put in a discarded commit instead, to make sure 3604 * that this space is not used again, and this space will 3605 * not be accounted into 'entries_bytes'. 3606 * 3607 * If we are less than the minimum size, we don't need to 3608 * worry about it. 3609 */ 3610 if (tail > (bsize - RB_EVNT_MIN_SIZE)) { 3611 /* No room for any events */ 3612 3613 /* Mark the rest of the page with padding */ 3614 rb_event_set_padding(event); 3615 3616 /* Make sure the padding is visible before the write update */ 3617 smp_wmb(); 3618 3619 /* Set the write back to the previous setting */ 3620 local_sub(length, &tail_page->write); 3621 return; 3622 } 3623 3624 /* Put in a discarded event */ 3625 event->array[0] = (bsize - tail) - RB_EVNT_HDR_SIZE; 3626 event->type_len = RINGBUF_TYPE_PADDING; 3627 /* time delta must be non zero */ 3628 event->time_delta = 1; 3629 3630 /* account for padding bytes */ 3631 local_add(bsize - tail, &cpu_buffer->entries_bytes); 3632 3633 /* Make sure the padding is visible before the tail_page->write update */ 3634 smp_wmb(); 3635 3636 /* Set write to end of buffer */ 3637 length = (tail + length) - bsize; 3638 local_sub(length, &tail_page->write); 3639 } 3640 3641 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer); 3642 3643 /* 3644 * This is the slow path, force gcc not to inline it. 3645 */ 3646 static noinline struct ring_buffer_event * 3647 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, 3648 unsigned long tail, struct rb_event_info *info) 3649 { 3650 struct buffer_page *tail_page = info->tail_page; 3651 struct buffer_page *commit_page = cpu_buffer->commit_page; 3652 struct trace_buffer *buffer = cpu_buffer->buffer; 3653 struct buffer_page *next_page; 3654 int ret; 3655 3656 next_page = tail_page; 3657 3658 rb_inc_page(&next_page); 3659 3660 /* 3661 * If for some reason, we had an interrupt storm that made 3662 * it all the way around the buffer, bail, and warn 3663 * about it. 3664 */ 3665 if (unlikely(next_page == commit_page)) { 3666 local_inc(&cpu_buffer->commit_overrun); 3667 goto out_reset; 3668 } 3669 3670 /* 3671 * This is where the fun begins! 3672 * 3673 * We are fighting against races between a reader that 3674 * could be on another CPU trying to swap its reader 3675 * page with the buffer head. 3676 * 3677 * We are also fighting against interrupts coming in and 3678 * moving the head or tail on us as well. 3679 * 3680 * If the next page is the head page then we have filled 3681 * the buffer, unless the commit page is still on the 3682 * reader page. 3683 */ 3684 if (rb_is_head_page(next_page, &tail_page->list)) { 3685 3686 /* 3687 * If the commit is not on the reader page, then 3688 * move the header page. 3689 */ 3690 if (!rb_is_reader_page(cpu_buffer->commit_page)) { 3691 /* 3692 * If we are not in overwrite mode, 3693 * this is easy, just stop here. 3694 */ 3695 if (!(buffer->flags & RB_FL_OVERWRITE)) { 3696 local_inc(&cpu_buffer->dropped_events); 3697 goto out_reset; 3698 } 3699 3700 ret = rb_handle_head_page(cpu_buffer, 3701 tail_page, 3702 next_page); 3703 if (ret < 0) 3704 goto out_reset; 3705 if (ret) 3706 goto out_again; 3707 } else { 3708 /* 3709 * We need to be careful here too. The 3710 * commit page could still be on the reader 3711 * page. We could have a small buffer, and 3712 * have filled up the buffer with events 3713 * from interrupts and such, and wrapped. 3714 * 3715 * Note, if the tail page is also on the 3716 * reader_page, we let it move out. 3717 */ 3718 if (unlikely((cpu_buffer->commit_page != 3719 cpu_buffer->tail_page) && 3720 (cpu_buffer->commit_page == 3721 cpu_buffer->reader_page))) { 3722 local_inc(&cpu_buffer->commit_overrun); 3723 goto out_reset; 3724 } 3725 } 3726 } 3727 3728 rb_tail_page_update(cpu_buffer, tail_page, next_page); 3729 3730 out_again: 3731 3732 rb_reset_tail(cpu_buffer, tail, info); 3733 3734 /* Commit what we have for now. */ 3735 rb_end_commit(cpu_buffer); 3736 /* rb_end_commit() decs committing */ 3737 local_inc(&cpu_buffer->committing); 3738 3739 /* fail and let the caller try again */ 3740 return ERR_PTR(-EAGAIN); 3741 3742 out_reset: 3743 /* reset write */ 3744 rb_reset_tail(cpu_buffer, tail, info); 3745 3746 return NULL; 3747 } 3748 3749 /* Slow path */ 3750 static struct ring_buffer_event * 3751 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer, 3752 struct ring_buffer_event *event, u64 delta, bool abs) 3753 { 3754 if (abs) 3755 event->type_len = RINGBUF_TYPE_TIME_STAMP; 3756 else 3757 event->type_len = RINGBUF_TYPE_TIME_EXTEND; 3758 3759 /* Not the first event on the page, or not delta? */ 3760 if (abs || rb_event_index(cpu_buffer, event)) { 3761 event->time_delta = delta & TS_MASK; 3762 event->array[0] = delta >> TS_SHIFT; 3763 } else { 3764 /* nope, just zero it */ 3765 event->time_delta = 0; 3766 event->array[0] = 0; 3767 } 3768 3769 return skip_time_extend(event); 3770 } 3771 3772 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK 3773 static inline bool sched_clock_stable(void) 3774 { 3775 return true; 3776 } 3777 #endif 3778 3779 static void 3780 rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer, 3781 struct rb_event_info *info) 3782 { 3783 u64 write_stamp; 3784 3785 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s", 3786 (unsigned long long)info->delta, 3787 (unsigned long long)info->ts, 3788 (unsigned long long)info->before, 3789 (unsigned long long)info->after, 3790 (unsigned long long)({rb_time_read(&cpu_buffer->write_stamp, &write_stamp); write_stamp;}), 3791 sched_clock_stable() ? "" : 3792 "If you just came from a suspend/resume,\n" 3793 "please switch to the trace global clock:\n" 3794 " echo global > /sys/kernel/tracing/trace_clock\n" 3795 "or add trace_clock=global to the kernel command line\n"); 3796 } 3797 3798 static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer, 3799 struct ring_buffer_event **event, 3800 struct rb_event_info *info, 3801 u64 *delta, 3802 unsigned int *length) 3803 { 3804 bool abs = info->add_timestamp & 3805 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE); 3806 3807 if (unlikely(info->delta > (1ULL << 59))) { 3808 /* 3809 * Some timers can use more than 59 bits, and when a timestamp 3810 * is added to the buffer, it will lose those bits. 3811 */ 3812 if (abs && (info->ts & TS_MSB)) { 3813 info->delta &= ABS_TS_MASK; 3814 3815 /* did the clock go backwards */ 3816 } else if (info->before == info->after && info->before > info->ts) { 3817 /* not interrupted */ 3818 static int once; 3819 3820 /* 3821 * This is possible with a recalibrating of the TSC. 3822 * Do not produce a call stack, but just report it. 3823 */ 3824 if (!once) { 3825 once++; 3826 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n", 3827 info->before, info->ts); 3828 } 3829 } else 3830 rb_check_timestamp(cpu_buffer, info); 3831 if (!abs) 3832 info->delta = 0; 3833 } 3834 *event = rb_add_time_stamp(cpu_buffer, *event, info->delta, abs); 3835 *length -= RB_LEN_TIME_EXTEND; 3836 *delta = 0; 3837 } 3838 3839 /** 3840 * rb_update_event - update event type and data 3841 * @cpu_buffer: The per cpu buffer of the @event 3842 * @event: the event to update 3843 * @info: The info to update the @event with (contains length and delta) 3844 * 3845 * Update the type and data fields of the @event. The length 3846 * is the actual size that is written to the ring buffer, 3847 * and with this, we can determine what to place into the 3848 * data field. 3849 */ 3850 static void 3851 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, 3852 struct ring_buffer_event *event, 3853 struct rb_event_info *info) 3854 { 3855 unsigned length = info->length; 3856 u64 delta = info->delta; 3857 unsigned int nest = local_read(&cpu_buffer->committing) - 1; 3858 3859 if (!WARN_ON_ONCE(nest >= MAX_NEST)) 3860 cpu_buffer->event_stamp[nest] = info->ts; 3861 3862 /* 3863 * If we need to add a timestamp, then we 3864 * add it to the start of the reserved space. 3865 */ 3866 if (unlikely(info->add_timestamp)) 3867 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length); 3868 3869 event->time_delta = delta; 3870 length -= RB_EVNT_HDR_SIZE; 3871 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) { 3872 event->type_len = 0; 3873 event->array[0] = length; 3874 } else 3875 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); 3876 } 3877 3878 static unsigned rb_calculate_event_length(unsigned length) 3879 { 3880 struct ring_buffer_event event; /* Used only for sizeof array */ 3881 3882 /* zero length can cause confusions */ 3883 if (!length) 3884 length++; 3885 3886 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) 3887 length += sizeof(event.array[0]); 3888 3889 length += RB_EVNT_HDR_SIZE; 3890 length = ALIGN(length, RB_ARCH_ALIGNMENT); 3891 3892 /* 3893 * In case the time delta is larger than the 27 bits for it 3894 * in the header, we need to add a timestamp. If another 3895 * event comes in when trying to discard this one to increase 3896 * the length, then the timestamp will be added in the allocated 3897 * space of this event. If length is bigger than the size needed 3898 * for the TIME_EXTEND, then padding has to be used. The events 3899 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal 3900 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding. 3901 * As length is a multiple of 4, we only need to worry if it 3902 * is 12 (RB_LEN_TIME_EXTEND + 4). 3903 */ 3904 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT) 3905 length += RB_ALIGNMENT; 3906 3907 return length; 3908 } 3909 3910 static inline bool 3911 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, 3912 struct ring_buffer_event *event) 3913 { 3914 unsigned long new_index, old_index; 3915 struct buffer_page *bpage; 3916 unsigned long addr; 3917 3918 new_index = rb_event_index(cpu_buffer, event); 3919 old_index = new_index + rb_event_ts_length(event); 3920 addr = (unsigned long)event; 3921 addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1); 3922 3923 bpage = READ_ONCE(cpu_buffer->tail_page); 3924 3925 /* 3926 * Make sure the tail_page is still the same and 3927 * the next write location is the end of this event 3928 */ 3929 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { 3930 unsigned long write_mask = 3931 local_read(&bpage->write) & ~RB_WRITE_MASK; 3932 unsigned long event_length = rb_event_length(event); 3933 3934 /* 3935 * For the before_stamp to be different than the write_stamp 3936 * to make sure that the next event adds an absolute 3937 * value and does not rely on the saved write stamp, which 3938 * is now going to be bogus. 3939 * 3940 * By setting the before_stamp to zero, the next event 3941 * is not going to use the write_stamp and will instead 3942 * create an absolute timestamp. This means there's no 3943 * reason to update the wirte_stamp! 3944 */ 3945 rb_time_set(&cpu_buffer->before_stamp, 0); 3946 3947 /* 3948 * If an event were to come in now, it would see that the 3949 * write_stamp and the before_stamp are different, and assume 3950 * that this event just added itself before updating 3951 * the write stamp. The interrupting event will fix the 3952 * write stamp for us, and use an absolute timestamp. 3953 */ 3954 3955 /* 3956 * This is on the tail page. It is possible that 3957 * a write could come in and move the tail page 3958 * and write to the next page. That is fine 3959 * because we just shorten what is on this page. 3960 */ 3961 old_index += write_mask; 3962 new_index += write_mask; 3963 3964 /* caution: old_index gets updated on cmpxchg failure */ 3965 if (local_try_cmpxchg(&bpage->write, &old_index, new_index)) { 3966 /* update counters */ 3967 local_sub(event_length, &cpu_buffer->entries_bytes); 3968 return true; 3969 } 3970 } 3971 3972 /* could not discard */ 3973 return false; 3974 } 3975 3976 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) 3977 { 3978 local_inc(&cpu_buffer->committing); 3979 local_inc(&cpu_buffer->commits); 3980 } 3981 3982 static __always_inline void 3983 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) 3984 { 3985 unsigned long max_count; 3986 3987 /* 3988 * We only race with interrupts and NMIs on this CPU. 3989 * If we own the commit event, then we can commit 3990 * all others that interrupted us, since the interruptions 3991 * are in stack format (they finish before they come 3992 * back to us). This allows us to do a simple loop to 3993 * assign the commit to the tail. 3994 */ 3995 again: 3996 max_count = cpu_buffer->nr_pages * 100; 3997 3998 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) { 3999 if (RB_WARN_ON(cpu_buffer, !(--max_count))) 4000 return; 4001 if (RB_WARN_ON(cpu_buffer, 4002 rb_is_reader_page(cpu_buffer->tail_page))) 4003 return; 4004 /* 4005 * No need for a memory barrier here, as the update 4006 * of the tail_page did it for this page. 4007 */ 4008 local_set(&cpu_buffer->commit_page->page->commit, 4009 rb_page_write(cpu_buffer->commit_page)); 4010 rb_inc_page(&cpu_buffer->commit_page); 4011 if (cpu_buffer->ring_meta) { 4012 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 4013 meta->commit_buffer = (unsigned long)cpu_buffer->commit_page->page; 4014 } 4015 /* add barrier to keep gcc from optimizing too much */ 4016 barrier(); 4017 } 4018 while (rb_commit_index(cpu_buffer) != 4019 rb_page_write(cpu_buffer->commit_page)) { 4020 4021 /* Make sure the readers see the content of what is committed. */ 4022 smp_wmb(); 4023 local_set(&cpu_buffer->commit_page->page->commit, 4024 rb_page_write(cpu_buffer->commit_page)); 4025 RB_WARN_ON(cpu_buffer, 4026 local_read(&cpu_buffer->commit_page->page->commit) & 4027 ~RB_WRITE_MASK); 4028 barrier(); 4029 } 4030 4031 /* again, keep gcc from optimizing */ 4032 barrier(); 4033 4034 /* 4035 * If an interrupt came in just after the first while loop 4036 * and pushed the tail page forward, we will be left with 4037 * a dangling commit that will never go forward. 4038 */ 4039 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page))) 4040 goto again; 4041 } 4042 4043 static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) 4044 { 4045 unsigned long commits; 4046 4047 if (RB_WARN_ON(cpu_buffer, 4048 !local_read(&cpu_buffer->committing))) 4049 return; 4050 4051 again: 4052 commits = local_read(&cpu_buffer->commits); 4053 /* synchronize with interrupts */ 4054 barrier(); 4055 if (local_read(&cpu_buffer->committing) == 1) 4056 rb_set_commit_to_write(cpu_buffer); 4057 4058 local_dec(&cpu_buffer->committing); 4059 4060 /* synchronize with interrupts */ 4061 barrier(); 4062 4063 /* 4064 * Need to account for interrupts coming in between the 4065 * updating of the commit page and the clearing of the 4066 * committing counter. 4067 */ 4068 if (unlikely(local_read(&cpu_buffer->commits) != commits) && 4069 !local_read(&cpu_buffer->committing)) { 4070 local_inc(&cpu_buffer->committing); 4071 goto again; 4072 } 4073 } 4074 4075 static inline void rb_event_discard(struct ring_buffer_event *event) 4076 { 4077 if (extended_time(event)) 4078 event = skip_time_extend(event); 4079 4080 /* array[0] holds the actual length for the discarded event */ 4081 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; 4082 event->type_len = RINGBUF_TYPE_PADDING; 4083 /* time delta must be non zero */ 4084 if (!event->time_delta) 4085 event->time_delta = 1; 4086 } 4087 4088 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer) 4089 { 4090 local_inc(&cpu_buffer->entries); 4091 rb_end_commit(cpu_buffer); 4092 } 4093 4094 static bool 4095 rb_irq_work_queue(struct rb_irq_work *irq_work) 4096 { 4097 int cpu; 4098 4099 /* irq_work_queue_on() is not NMI-safe */ 4100 if (unlikely(in_nmi())) 4101 return irq_work_queue(&irq_work->work); 4102 4103 /* 4104 * If CPU isolation is not active, cpu is always the current 4105 * CPU, and the following is equivallent to irq_work_queue(). 4106 */ 4107 cpu = housekeeping_any_cpu(HK_TYPE_KERNEL_NOISE); 4108 return irq_work_queue_on(&irq_work->work, cpu); 4109 } 4110 4111 static __always_inline void 4112 rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer) 4113 { 4114 if (buffer->irq_work.waiters_pending) { 4115 buffer->irq_work.waiters_pending = false; 4116 /* irq_work_queue() supplies it's own memory barriers */ 4117 rb_irq_work_queue(&buffer->irq_work); 4118 } 4119 4120 if (cpu_buffer->irq_work.waiters_pending) { 4121 cpu_buffer->irq_work.waiters_pending = false; 4122 /* irq_work_queue() supplies it's own memory barriers */ 4123 rb_irq_work_queue(&cpu_buffer->irq_work); 4124 } 4125 4126 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched)) 4127 return; 4128 4129 if (cpu_buffer->reader_page == cpu_buffer->commit_page) 4130 return; 4131 4132 if (!cpu_buffer->irq_work.full_waiters_pending) 4133 return; 4134 4135 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched); 4136 4137 if (!full_hit(buffer, cpu_buffer->cpu, cpu_buffer->shortest_full)) 4138 return; 4139 4140 cpu_buffer->irq_work.wakeup_full = true; 4141 cpu_buffer->irq_work.full_waiters_pending = false; 4142 /* irq_work_queue() supplies it's own memory barriers */ 4143 rb_irq_work_queue(&cpu_buffer->irq_work); 4144 } 4145 4146 #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION 4147 # define do_ring_buffer_record_recursion() \ 4148 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_) 4149 #else 4150 # define do_ring_buffer_record_recursion() do { } while (0) 4151 #endif 4152 4153 /* 4154 * The lock and unlock are done within a preempt disable section. 4155 * The current_context per_cpu variable can only be modified 4156 * by the current task between lock and unlock. But it can 4157 * be modified more than once via an interrupt. To pass this 4158 * information from the lock to the unlock without having to 4159 * access the 'in_interrupt()' functions again (which do show 4160 * a bit of overhead in something as critical as function tracing, 4161 * we use a bitmask trick. 4162 * 4163 * bit 1 = NMI context 4164 * bit 2 = IRQ context 4165 * bit 3 = SoftIRQ context 4166 * bit 4 = normal context. 4167 * 4168 * This works because this is the order of contexts that can 4169 * preempt other contexts. A SoftIRQ never preempts an IRQ 4170 * context. 4171 * 4172 * When the context is determined, the corresponding bit is 4173 * checked and set (if it was set, then a recursion of that context 4174 * happened). 4175 * 4176 * On unlock, we need to clear this bit. To do so, just subtract 4177 * 1 from the current_context and AND it to itself. 4178 * 4179 * (binary) 4180 * 101 - 1 = 100 4181 * 101 & 100 = 100 (clearing bit zero) 4182 * 4183 * 1010 - 1 = 1001 4184 * 1010 & 1001 = 1000 (clearing bit 1) 4185 * 4186 * The least significant bit can be cleared this way, and it 4187 * just so happens that it is the same bit corresponding to 4188 * the current context. 4189 * 4190 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit 4191 * is set when a recursion is detected at the current context, and if 4192 * the TRANSITION bit is already set, it will fail the recursion. 4193 * This is needed because there's a lag between the changing of 4194 * interrupt context and updating the preempt count. In this case, 4195 * a false positive will be found. To handle this, one extra recursion 4196 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION 4197 * bit is already set, then it is considered a recursion and the function 4198 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned. 4199 * 4200 * On the trace_recursive_unlock(), the TRANSITION bit will be the first 4201 * to be cleared. Even if it wasn't the context that set it. That is, 4202 * if an interrupt comes in while NORMAL bit is set and the ring buffer 4203 * is called before preempt_count() is updated, since the check will 4204 * be on the NORMAL bit, the TRANSITION bit will then be set. If an 4205 * NMI then comes in, it will set the NMI bit, but when the NMI code 4206 * does the trace_recursive_unlock() it will clear the TRANSITION bit 4207 * and leave the NMI bit set. But this is fine, because the interrupt 4208 * code that set the TRANSITION bit will then clear the NMI bit when it 4209 * calls trace_recursive_unlock(). If another NMI comes in, it will 4210 * set the TRANSITION bit and continue. 4211 * 4212 * Note: The TRANSITION bit only handles a single transition between context. 4213 */ 4214 4215 static __always_inline bool 4216 trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer) 4217 { 4218 unsigned int val = cpu_buffer->current_context; 4219 int bit = interrupt_context_level(); 4220 4221 bit = RB_CTX_NORMAL - bit; 4222 4223 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) { 4224 /* 4225 * It is possible that this was called by transitioning 4226 * between interrupt context, and preempt_count() has not 4227 * been updated yet. In this case, use the TRANSITION bit. 4228 */ 4229 bit = RB_CTX_TRANSITION; 4230 if (val & (1 << (bit + cpu_buffer->nest))) { 4231 do_ring_buffer_record_recursion(); 4232 return true; 4233 } 4234 } 4235 4236 val |= (1 << (bit + cpu_buffer->nest)); 4237 cpu_buffer->current_context = val; 4238 4239 return false; 4240 } 4241 4242 static __always_inline void 4243 trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer) 4244 { 4245 cpu_buffer->current_context &= 4246 cpu_buffer->current_context - (1 << cpu_buffer->nest); 4247 } 4248 4249 /* The recursive locking above uses 5 bits */ 4250 #define NESTED_BITS 5 4251 4252 /** 4253 * ring_buffer_nest_start - Allow to trace while nested 4254 * @buffer: The ring buffer to modify 4255 * 4256 * The ring buffer has a safety mechanism to prevent recursion. 4257 * But there may be a case where a trace needs to be done while 4258 * tracing something else. In this case, calling this function 4259 * will allow this function to nest within a currently active 4260 * ring_buffer_lock_reserve(). 4261 * 4262 * Call this function before calling another ring_buffer_lock_reserve() and 4263 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit(). 4264 */ 4265 void ring_buffer_nest_start(struct trace_buffer *buffer) 4266 { 4267 struct ring_buffer_per_cpu *cpu_buffer; 4268 int cpu; 4269 4270 /* Enabled by ring_buffer_nest_end() */ 4271 preempt_disable_notrace(); 4272 cpu = raw_smp_processor_id(); 4273 cpu_buffer = buffer->buffers[cpu]; 4274 /* This is the shift value for the above recursive locking */ 4275 cpu_buffer->nest += NESTED_BITS; 4276 } 4277 4278 /** 4279 * ring_buffer_nest_end - Allow to trace while nested 4280 * @buffer: The ring buffer to modify 4281 * 4282 * Must be called after ring_buffer_nest_start() and after the 4283 * ring_buffer_unlock_commit(). 4284 */ 4285 void ring_buffer_nest_end(struct trace_buffer *buffer) 4286 { 4287 struct ring_buffer_per_cpu *cpu_buffer; 4288 int cpu; 4289 4290 /* disabled by ring_buffer_nest_start() */ 4291 cpu = raw_smp_processor_id(); 4292 cpu_buffer = buffer->buffers[cpu]; 4293 /* This is the shift value for the above recursive locking */ 4294 cpu_buffer->nest -= NESTED_BITS; 4295 preempt_enable_notrace(); 4296 } 4297 4298 /** 4299 * ring_buffer_unlock_commit - commit a reserved 4300 * @buffer: The buffer to commit to 4301 * 4302 * This commits the data to the ring buffer, and releases any locks held. 4303 * 4304 * Must be paired with ring_buffer_lock_reserve. 4305 */ 4306 int ring_buffer_unlock_commit(struct trace_buffer *buffer) 4307 { 4308 struct ring_buffer_per_cpu *cpu_buffer; 4309 int cpu = raw_smp_processor_id(); 4310 4311 cpu_buffer = buffer->buffers[cpu]; 4312 4313 rb_commit(cpu_buffer); 4314 4315 rb_wakeups(buffer, cpu_buffer); 4316 4317 trace_recursive_unlock(cpu_buffer); 4318 4319 preempt_enable_notrace(); 4320 4321 return 0; 4322 } 4323 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); 4324 4325 /* Special value to validate all deltas on a page. */ 4326 #define CHECK_FULL_PAGE 1L 4327 4328 #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS 4329 4330 static const char *show_irq_str(int bits) 4331 { 4332 static const char * type[] = { 4333 ".", // 0 4334 "s", // 1 4335 "h", // 2 4336 "Hs", // 3 4337 "n", // 4 4338 "Ns", // 5 4339 "Nh", // 6 4340 "NHs", // 7 4341 }; 4342 4343 return type[bits]; 4344 } 4345 4346 /* Assume this is a trace event */ 4347 static const char *show_flags(struct ring_buffer_event *event) 4348 { 4349 struct trace_entry *entry; 4350 int bits = 0; 4351 4352 if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry)) 4353 return "X"; 4354 4355 entry = ring_buffer_event_data(event); 4356 4357 if (entry->flags & TRACE_FLAG_SOFTIRQ) 4358 bits |= 1; 4359 4360 if (entry->flags & TRACE_FLAG_HARDIRQ) 4361 bits |= 2; 4362 4363 if (entry->flags & TRACE_FLAG_NMI) 4364 bits |= 4; 4365 4366 return show_irq_str(bits); 4367 } 4368 4369 static const char *show_irq(struct ring_buffer_event *event) 4370 { 4371 struct trace_entry *entry; 4372 4373 if (rb_event_data_length(event) - RB_EVNT_HDR_SIZE < sizeof(*entry)) 4374 return ""; 4375 4376 entry = ring_buffer_event_data(event); 4377 if (entry->flags & TRACE_FLAG_IRQS_OFF) 4378 return "d"; 4379 return ""; 4380 } 4381 4382 static const char *show_interrupt_level(void) 4383 { 4384 unsigned long pc = preempt_count(); 4385 unsigned char level = 0; 4386 4387 if (pc & SOFTIRQ_OFFSET) 4388 level |= 1; 4389 4390 if (pc & HARDIRQ_MASK) 4391 level |= 2; 4392 4393 if (pc & NMI_MASK) 4394 level |= 4; 4395 4396 return show_irq_str(level); 4397 } 4398 4399 static void dump_buffer_page(struct buffer_data_page *bpage, 4400 struct rb_event_info *info, 4401 unsigned long tail) 4402 { 4403 struct ring_buffer_event *event; 4404 u64 ts, delta; 4405 int e; 4406 4407 ts = bpage->time_stamp; 4408 pr_warn(" [%lld] PAGE TIME STAMP\n", ts); 4409 4410 for (e = 0; e < tail; e += rb_event_length(event)) { 4411 4412 event = (struct ring_buffer_event *)(bpage->data + e); 4413 4414 switch (event->type_len) { 4415 4416 case RINGBUF_TYPE_TIME_EXTEND: 4417 delta = rb_event_time_stamp(event); 4418 ts += delta; 4419 pr_warn(" 0x%x: [%lld] delta:%lld TIME EXTEND\n", 4420 e, ts, delta); 4421 break; 4422 4423 case RINGBUF_TYPE_TIME_STAMP: 4424 delta = rb_event_time_stamp(event); 4425 ts = rb_fix_abs_ts(delta, ts); 4426 pr_warn(" 0x%x: [%lld] absolute:%lld TIME STAMP\n", 4427 e, ts, delta); 4428 break; 4429 4430 case RINGBUF_TYPE_PADDING: 4431 ts += event->time_delta; 4432 pr_warn(" 0x%x: [%lld] delta:%d PADDING\n", 4433 e, ts, event->time_delta); 4434 break; 4435 4436 case RINGBUF_TYPE_DATA: 4437 ts += event->time_delta; 4438 pr_warn(" 0x%x: [%lld] delta:%d %s%s\n", 4439 e, ts, event->time_delta, 4440 show_flags(event), show_irq(event)); 4441 break; 4442 4443 default: 4444 break; 4445 } 4446 } 4447 pr_warn("expected end:0x%lx last event actually ended at:0x%x\n", tail, e); 4448 } 4449 4450 static DEFINE_PER_CPU(atomic_t, checking); 4451 static atomic_t ts_dump; 4452 4453 #define buffer_warn_return(fmt, ...) \ 4454 do { \ 4455 /* If another report is happening, ignore this one */ \ 4456 if (atomic_inc_return(&ts_dump) != 1) { \ 4457 atomic_dec(&ts_dump); \ 4458 goto out; \ 4459 } \ 4460 atomic_inc(&cpu_buffer->record_disabled); \ 4461 pr_warn(fmt, ##__VA_ARGS__); \ 4462 dump_buffer_page(bpage, info, tail); \ 4463 atomic_dec(&ts_dump); \ 4464 /* There's some cases in boot up that this can happen */ \ 4465 if (WARN_ON_ONCE(system_state != SYSTEM_BOOTING)) \ 4466 /* Do not re-enable checking */ \ 4467 return; \ 4468 } while (0) 4469 4470 /* 4471 * Check if the current event time stamp matches the deltas on 4472 * the buffer page. 4473 */ 4474 static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer, 4475 struct rb_event_info *info, 4476 unsigned long tail) 4477 { 4478 struct buffer_data_page *bpage; 4479 u64 ts, delta; 4480 bool full = false; 4481 int ret; 4482 4483 bpage = info->tail_page->page; 4484 4485 if (tail == CHECK_FULL_PAGE) { 4486 full = true; 4487 tail = local_read(&bpage->commit); 4488 } else if (info->add_timestamp & 4489 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) { 4490 /* Ignore events with absolute time stamps */ 4491 return; 4492 } 4493 4494 /* 4495 * Do not check the first event (skip possible extends too). 4496 * Also do not check if previous events have not been committed. 4497 */ 4498 if (tail <= 8 || tail > local_read(&bpage->commit)) 4499 return; 4500 4501 /* 4502 * If this interrupted another event, 4503 */ 4504 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1) 4505 goto out; 4506 4507 ret = rb_read_data_buffer(bpage, tail, cpu_buffer->cpu, &ts, &delta); 4508 if (ret < 0) { 4509 if (delta < ts) { 4510 buffer_warn_return("[CPU: %d]ABSOLUTE TIME WENT BACKWARDS: last ts: %lld absolute ts: %lld clock:%pS\n", 4511 cpu_buffer->cpu, ts, delta, 4512 cpu_buffer->buffer->clock); 4513 goto out; 4514 } 4515 } 4516 if ((full && ts > info->ts) || 4517 (!full && ts + info->delta != info->ts)) { 4518 buffer_warn_return("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s context:%s\ntrace clock:%pS", 4519 cpu_buffer->cpu, 4520 ts + info->delta, info->ts, info->delta, 4521 info->before, info->after, 4522 full ? " (full)" : "", show_interrupt_level(), 4523 cpu_buffer->buffer->clock); 4524 } 4525 out: 4526 atomic_dec(this_cpu_ptr(&checking)); 4527 } 4528 #else 4529 static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer, 4530 struct rb_event_info *info, 4531 unsigned long tail) 4532 { 4533 } 4534 #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */ 4535 4536 static struct ring_buffer_event * 4537 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, 4538 struct rb_event_info *info) 4539 { 4540 struct ring_buffer_event *event; 4541 struct buffer_page *tail_page; 4542 unsigned long tail, write, w; 4543 4544 /* Don't let the compiler play games with cpu_buffer->tail_page */ 4545 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page); 4546 4547 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK; 4548 barrier(); 4549 rb_time_read(&cpu_buffer->before_stamp, &info->before); 4550 rb_time_read(&cpu_buffer->write_stamp, &info->after); 4551 barrier(); 4552 info->ts = rb_time_stamp(cpu_buffer->buffer); 4553 4554 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) { 4555 info->delta = info->ts; 4556 } else { 4557 /* 4558 * If interrupting an event time update, we may need an 4559 * absolute timestamp. 4560 * Don't bother if this is the start of a new page (w == 0). 4561 */ 4562 if (!w) { 4563 /* Use the sub-buffer timestamp */ 4564 info->delta = 0; 4565 } else if (unlikely(info->before != info->after)) { 4566 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND; 4567 info->length += RB_LEN_TIME_EXTEND; 4568 } else { 4569 info->delta = info->ts - info->after; 4570 if (unlikely(test_time_stamp(info->delta))) { 4571 info->add_timestamp |= RB_ADD_STAMP_EXTEND; 4572 info->length += RB_LEN_TIME_EXTEND; 4573 } 4574 } 4575 } 4576 4577 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts); 4578 4579 /*C*/ write = local_add_return(info->length, &tail_page->write); 4580 4581 /* set write to only the index of the write */ 4582 write &= RB_WRITE_MASK; 4583 4584 tail = write - info->length; 4585 4586 /* See if we shot pass the end of this buffer page */ 4587 if (unlikely(write > cpu_buffer->buffer->subbuf_size)) { 4588 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE); 4589 return rb_move_tail(cpu_buffer, tail, info); 4590 } 4591 4592 if (likely(tail == w)) { 4593 /* Nothing interrupted us between A and C */ 4594 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts); 4595 /* 4596 * If something came in between C and D, the write stamp 4597 * may now not be in sync. But that's fine as the before_stamp 4598 * will be different and then next event will just be forced 4599 * to use an absolute timestamp. 4600 */ 4601 if (likely(!(info->add_timestamp & 4602 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) 4603 /* This did not interrupt any time update */ 4604 info->delta = info->ts - info->after; 4605 else 4606 /* Just use full timestamp for interrupting event */ 4607 info->delta = info->ts; 4608 check_buffer(cpu_buffer, info, tail); 4609 } else { 4610 u64 ts; 4611 /* SLOW PATH - Interrupted between A and C */ 4612 4613 /* Save the old before_stamp */ 4614 rb_time_read(&cpu_buffer->before_stamp, &info->before); 4615 4616 /* 4617 * Read a new timestamp and update the before_stamp to make 4618 * the next event after this one force using an absolute 4619 * timestamp. This is in case an interrupt were to come in 4620 * between E and F. 4621 */ 4622 ts = rb_time_stamp(cpu_buffer->buffer); 4623 rb_time_set(&cpu_buffer->before_stamp, ts); 4624 4625 barrier(); 4626 /*E*/ rb_time_read(&cpu_buffer->write_stamp, &info->after); 4627 barrier(); 4628 /*F*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) && 4629 info->after == info->before && info->after < ts) { 4630 /* 4631 * Nothing came after this event between C and F, it is 4632 * safe to use info->after for the delta as it 4633 * matched info->before and is still valid. 4634 */ 4635 info->delta = ts - info->after; 4636 } else { 4637 /* 4638 * Interrupted between C and F: 4639 * Lost the previous events time stamp. Just set the 4640 * delta to zero, and this will be the same time as 4641 * the event this event interrupted. And the events that 4642 * came after this will still be correct (as they would 4643 * have built their delta on the previous event. 4644 */ 4645 info->delta = 0; 4646 } 4647 info->ts = ts; 4648 info->add_timestamp &= ~RB_ADD_STAMP_FORCE; 4649 } 4650 4651 /* 4652 * If this is the first commit on the page, then it has the same 4653 * timestamp as the page itself. 4654 */ 4655 if (unlikely(!tail && !(info->add_timestamp & 4656 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) 4657 info->delta = 0; 4658 4659 /* We reserved something on the buffer */ 4660 4661 event = __rb_page_index(tail_page, tail); 4662 rb_update_event(cpu_buffer, event, info); 4663 4664 local_inc(&tail_page->entries); 4665 4666 /* 4667 * If this is the first commit on the page, then update 4668 * its timestamp. 4669 */ 4670 if (unlikely(!tail)) 4671 tail_page->page->time_stamp = info->ts; 4672 4673 /* account for these added bytes */ 4674 local_add(info->length, &cpu_buffer->entries_bytes); 4675 4676 return event; 4677 } 4678 4679 static __always_inline struct ring_buffer_event * 4680 rb_reserve_next_event(struct trace_buffer *buffer, 4681 struct ring_buffer_per_cpu *cpu_buffer, 4682 unsigned long length) 4683 { 4684 struct ring_buffer_event *event; 4685 struct rb_event_info info; 4686 int nr_loops = 0; 4687 int add_ts_default; 4688 4689 /* 4690 * ring buffer does cmpxchg as well as atomic64 operations 4691 * (which some archs use locking for atomic64), make sure this 4692 * is safe in NMI context 4693 */ 4694 if ((!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) || 4695 IS_ENABLED(CONFIG_GENERIC_ATOMIC64)) && 4696 (unlikely(in_nmi()))) { 4697 return NULL; 4698 } 4699 4700 rb_start_commit(cpu_buffer); 4701 /* The commit page can not change after this */ 4702 4703 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP 4704 /* 4705 * Due to the ability to swap a cpu buffer from a buffer 4706 * it is possible it was swapped before we committed. 4707 * (committing stops a swap). We check for it here and 4708 * if it happened, we have to fail the write. 4709 */ 4710 barrier(); 4711 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) { 4712 local_dec(&cpu_buffer->committing); 4713 local_dec(&cpu_buffer->commits); 4714 return NULL; 4715 } 4716 #endif 4717 4718 info.length = rb_calculate_event_length(length); 4719 4720 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) { 4721 add_ts_default = RB_ADD_STAMP_ABSOLUTE; 4722 info.length += RB_LEN_TIME_EXTEND; 4723 if (info.length > cpu_buffer->buffer->max_data_size) 4724 goto out_fail; 4725 } else { 4726 add_ts_default = RB_ADD_STAMP_NONE; 4727 } 4728 4729 again: 4730 info.add_timestamp = add_ts_default; 4731 info.delta = 0; 4732 4733 /* 4734 * We allow for interrupts to reenter here and do a trace. 4735 * If one does, it will cause this original code to loop 4736 * back here. Even with heavy interrupts happening, this 4737 * should only happen a few times in a row. If this happens 4738 * 1000 times in a row, there must be either an interrupt 4739 * storm or we have something buggy. 4740 * Bail! 4741 */ 4742 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) 4743 goto out_fail; 4744 4745 event = __rb_reserve_next(cpu_buffer, &info); 4746 4747 if (unlikely(PTR_ERR(event) == -EAGAIN)) { 4748 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND)) 4749 info.length -= RB_LEN_TIME_EXTEND; 4750 goto again; 4751 } 4752 4753 if (likely(event)) 4754 return event; 4755 out_fail: 4756 rb_end_commit(cpu_buffer); 4757 return NULL; 4758 } 4759 4760 /** 4761 * ring_buffer_lock_reserve - reserve a part of the buffer 4762 * @buffer: the ring buffer to reserve from 4763 * @length: the length of the data to reserve (excluding event header) 4764 * 4765 * Returns a reserved event on the ring buffer to copy directly to. 4766 * The user of this interface will need to get the body to write into 4767 * and can use the ring_buffer_event_data() interface. 4768 * 4769 * The length is the length of the data needed, not the event length 4770 * which also includes the event header. 4771 * 4772 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. 4773 * If NULL is returned, then nothing has been allocated or locked. 4774 */ 4775 struct ring_buffer_event * 4776 ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length) 4777 { 4778 struct ring_buffer_per_cpu *cpu_buffer; 4779 struct ring_buffer_event *event; 4780 int cpu; 4781 4782 /* If we are tracing schedule, we don't want to recurse */ 4783 preempt_disable_notrace(); 4784 4785 if (unlikely(atomic_read(&buffer->record_disabled))) 4786 goto out; 4787 4788 cpu = raw_smp_processor_id(); 4789 4790 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask))) 4791 goto out; 4792 4793 cpu_buffer = buffer->buffers[cpu]; 4794 4795 if (unlikely(atomic_read(&cpu_buffer->record_disabled))) 4796 goto out; 4797 4798 if (unlikely(length > buffer->max_data_size)) 4799 goto out; 4800 4801 if (unlikely(trace_recursive_lock(cpu_buffer))) 4802 goto out; 4803 4804 event = rb_reserve_next_event(buffer, cpu_buffer, length); 4805 if (!event) 4806 goto out_unlock; 4807 4808 return event; 4809 4810 out_unlock: 4811 trace_recursive_unlock(cpu_buffer); 4812 out: 4813 preempt_enable_notrace(); 4814 return NULL; 4815 } 4816 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); 4817 4818 /* 4819 * Decrement the entries to the page that an event is on. 4820 * The event does not even need to exist, only the pointer 4821 * to the page it is on. This may only be called before the commit 4822 * takes place. 4823 */ 4824 static inline void 4825 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, 4826 struct ring_buffer_event *event) 4827 { 4828 unsigned long addr = (unsigned long)event; 4829 struct buffer_page *bpage = cpu_buffer->commit_page; 4830 struct buffer_page *start; 4831 4832 addr &= ~((PAGE_SIZE << cpu_buffer->buffer->subbuf_order) - 1); 4833 4834 /* Do the likely case first */ 4835 if (likely(bpage->page == (void *)addr)) { 4836 local_dec(&bpage->entries); 4837 return; 4838 } 4839 4840 /* 4841 * Because the commit page may be on the reader page we 4842 * start with the next page and check the end loop there. 4843 */ 4844 rb_inc_page(&bpage); 4845 start = bpage; 4846 do { 4847 if (bpage->page == (void *)addr) { 4848 local_dec(&bpage->entries); 4849 return; 4850 } 4851 rb_inc_page(&bpage); 4852 } while (bpage != start); 4853 4854 /* commit not part of this buffer?? */ 4855 RB_WARN_ON(cpu_buffer, 1); 4856 } 4857 4858 /** 4859 * ring_buffer_discard_commit - discard an event that has not been committed 4860 * @buffer: the ring buffer 4861 * @event: non committed event to discard 4862 * 4863 * Sometimes an event that is in the ring buffer needs to be ignored. 4864 * This function lets the user discard an event in the ring buffer 4865 * and then that event will not be read later. 4866 * 4867 * This function only works if it is called before the item has been 4868 * committed. It will try to free the event from the ring buffer 4869 * if another event has not been added behind it. 4870 * 4871 * If another event has been added behind it, it will set the event 4872 * up as discarded, and perform the commit. 4873 * 4874 * If this function is called, do not call ring_buffer_unlock_commit on 4875 * the event. 4876 */ 4877 void ring_buffer_discard_commit(struct trace_buffer *buffer, 4878 struct ring_buffer_event *event) 4879 { 4880 struct ring_buffer_per_cpu *cpu_buffer; 4881 int cpu; 4882 4883 /* The event is discarded regardless */ 4884 rb_event_discard(event); 4885 4886 cpu = smp_processor_id(); 4887 cpu_buffer = buffer->buffers[cpu]; 4888 4889 /* 4890 * This must only be called if the event has not been 4891 * committed yet. Thus we can assume that preemption 4892 * is still disabled. 4893 */ 4894 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); 4895 4896 rb_decrement_entry(cpu_buffer, event); 4897 rb_try_to_discard(cpu_buffer, event); 4898 rb_end_commit(cpu_buffer); 4899 4900 trace_recursive_unlock(cpu_buffer); 4901 4902 preempt_enable_notrace(); 4903 4904 } 4905 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); 4906 4907 /** 4908 * ring_buffer_write - write data to the buffer without reserving 4909 * @buffer: The ring buffer to write to. 4910 * @length: The length of the data being written (excluding the event header) 4911 * @data: The data to write to the buffer. 4912 * 4913 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as 4914 * one function. If you already have the data to write to the buffer, it 4915 * may be easier to simply call this function. 4916 * 4917 * Note, like ring_buffer_lock_reserve, the length is the length of the data 4918 * and not the length of the event which would hold the header. 4919 */ 4920 int ring_buffer_write(struct trace_buffer *buffer, 4921 unsigned long length, 4922 void *data) 4923 { 4924 struct ring_buffer_per_cpu *cpu_buffer; 4925 struct ring_buffer_event *event; 4926 void *body; 4927 int ret = -EBUSY; 4928 int cpu; 4929 4930 guard(preempt_notrace)(); 4931 4932 if (atomic_read(&buffer->record_disabled)) 4933 return -EBUSY; 4934 4935 cpu = raw_smp_processor_id(); 4936 4937 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 4938 return -EBUSY; 4939 4940 cpu_buffer = buffer->buffers[cpu]; 4941 4942 if (atomic_read(&cpu_buffer->record_disabled)) 4943 return -EBUSY; 4944 4945 if (length > buffer->max_data_size) 4946 return -EBUSY; 4947 4948 if (unlikely(trace_recursive_lock(cpu_buffer))) 4949 return -EBUSY; 4950 4951 event = rb_reserve_next_event(buffer, cpu_buffer, length); 4952 if (!event) 4953 goto out_unlock; 4954 4955 body = rb_event_data(event); 4956 4957 memcpy(body, data, length); 4958 4959 rb_commit(cpu_buffer); 4960 4961 rb_wakeups(buffer, cpu_buffer); 4962 4963 ret = 0; 4964 4965 out_unlock: 4966 trace_recursive_unlock(cpu_buffer); 4967 return ret; 4968 } 4969 EXPORT_SYMBOL_GPL(ring_buffer_write); 4970 4971 /* 4972 * The total entries in the ring buffer is the running counter 4973 * of entries entered into the ring buffer, minus the sum of 4974 * the entries read from the ring buffer and the number of 4975 * entries that were overwritten. 4976 */ 4977 static inline unsigned long 4978 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) 4979 { 4980 return local_read(&cpu_buffer->entries) - 4981 (local_read(&cpu_buffer->overrun) + cpu_buffer->read); 4982 } 4983 4984 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) 4985 { 4986 return !rb_num_of_entries(cpu_buffer); 4987 } 4988 4989 /** 4990 * ring_buffer_record_disable - stop all writes into the buffer 4991 * @buffer: The ring buffer to stop writes to. 4992 * 4993 * This prevents all writes to the buffer. Any attempt to write 4994 * to the buffer after this will fail and return NULL. 4995 * 4996 * The caller should call synchronize_rcu() after this. 4997 */ 4998 void ring_buffer_record_disable(struct trace_buffer *buffer) 4999 { 5000 atomic_inc(&buffer->record_disabled); 5001 } 5002 EXPORT_SYMBOL_GPL(ring_buffer_record_disable); 5003 5004 /** 5005 * ring_buffer_record_enable - enable writes to the buffer 5006 * @buffer: The ring buffer to enable writes 5007 * 5008 * Note, multiple disables will need the same number of enables 5009 * to truly enable the writing (much like preempt_disable). 5010 */ 5011 void ring_buffer_record_enable(struct trace_buffer *buffer) 5012 { 5013 atomic_dec(&buffer->record_disabled); 5014 } 5015 EXPORT_SYMBOL_GPL(ring_buffer_record_enable); 5016 5017 /** 5018 * ring_buffer_record_off - stop all writes into the buffer 5019 * @buffer: The ring buffer to stop writes to. 5020 * 5021 * This prevents all writes to the buffer. Any attempt to write 5022 * to the buffer after this will fail and return NULL. 5023 * 5024 * This is different than ring_buffer_record_disable() as 5025 * it works like an on/off switch, where as the disable() version 5026 * must be paired with a enable(). 5027 */ 5028 void ring_buffer_record_off(struct trace_buffer *buffer) 5029 { 5030 unsigned int rd; 5031 unsigned int new_rd; 5032 5033 rd = atomic_read(&buffer->record_disabled); 5034 do { 5035 new_rd = rd | RB_BUFFER_OFF; 5036 } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd)); 5037 } 5038 EXPORT_SYMBOL_GPL(ring_buffer_record_off); 5039 5040 /** 5041 * ring_buffer_record_on - restart writes into the buffer 5042 * @buffer: The ring buffer to start writes to. 5043 * 5044 * This enables all writes to the buffer that was disabled by 5045 * ring_buffer_record_off(). 5046 * 5047 * This is different than ring_buffer_record_enable() as 5048 * it works like an on/off switch, where as the enable() version 5049 * must be paired with a disable(). 5050 */ 5051 void ring_buffer_record_on(struct trace_buffer *buffer) 5052 { 5053 unsigned int rd; 5054 unsigned int new_rd; 5055 5056 rd = atomic_read(&buffer->record_disabled); 5057 do { 5058 new_rd = rd & ~RB_BUFFER_OFF; 5059 } while (!atomic_try_cmpxchg(&buffer->record_disabled, &rd, new_rd)); 5060 } 5061 EXPORT_SYMBOL_GPL(ring_buffer_record_on); 5062 5063 /** 5064 * ring_buffer_record_is_on - return true if the ring buffer can write 5065 * @buffer: The ring buffer to see if write is enabled 5066 * 5067 * Returns true if the ring buffer is in a state that it accepts writes. 5068 */ 5069 bool ring_buffer_record_is_on(struct trace_buffer *buffer) 5070 { 5071 return !atomic_read(&buffer->record_disabled); 5072 } 5073 5074 /** 5075 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable 5076 * @buffer: The ring buffer to see if write is set enabled 5077 * 5078 * Returns true if the ring buffer is set writable by ring_buffer_record_on(). 5079 * Note that this does NOT mean it is in a writable state. 5080 * 5081 * It may return true when the ring buffer has been disabled by 5082 * ring_buffer_record_disable(), as that is a temporary disabling of 5083 * the ring buffer. 5084 */ 5085 bool ring_buffer_record_is_set_on(struct trace_buffer *buffer) 5086 { 5087 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF); 5088 } 5089 5090 /** 5091 * ring_buffer_record_is_on_cpu - return true if the ring buffer can write 5092 * @buffer: The ring buffer to see if write is enabled 5093 * @cpu: The CPU to test if the ring buffer can write too 5094 * 5095 * Returns true if the ring buffer is in a state that it accepts writes 5096 * for a particular CPU. 5097 */ 5098 bool ring_buffer_record_is_on_cpu(struct trace_buffer *buffer, int cpu) 5099 { 5100 struct ring_buffer_per_cpu *cpu_buffer; 5101 5102 cpu_buffer = buffer->buffers[cpu]; 5103 5104 return ring_buffer_record_is_set_on(buffer) && 5105 !atomic_read(&cpu_buffer->record_disabled); 5106 } 5107 5108 /** 5109 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer 5110 * @buffer: The ring buffer to stop writes to. 5111 * @cpu: The CPU buffer to stop 5112 * 5113 * This prevents all writes to the buffer. Any attempt to write 5114 * to the buffer after this will fail and return NULL. 5115 * 5116 * The caller should call synchronize_rcu() after this. 5117 */ 5118 void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu) 5119 { 5120 struct ring_buffer_per_cpu *cpu_buffer; 5121 5122 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5123 return; 5124 5125 cpu_buffer = buffer->buffers[cpu]; 5126 atomic_inc(&cpu_buffer->record_disabled); 5127 } 5128 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); 5129 5130 /** 5131 * ring_buffer_record_enable_cpu - enable writes to the buffer 5132 * @buffer: The ring buffer to enable writes 5133 * @cpu: The CPU to enable. 5134 * 5135 * Note, multiple disables will need the same number of enables 5136 * to truly enable the writing (much like preempt_disable). 5137 */ 5138 void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu) 5139 { 5140 struct ring_buffer_per_cpu *cpu_buffer; 5141 5142 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5143 return; 5144 5145 cpu_buffer = buffer->buffers[cpu]; 5146 atomic_dec(&cpu_buffer->record_disabled); 5147 } 5148 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); 5149 5150 /** 5151 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer 5152 * @buffer: The ring buffer 5153 * @cpu: The per CPU buffer to read from. 5154 */ 5155 u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu) 5156 { 5157 unsigned long flags; 5158 struct ring_buffer_per_cpu *cpu_buffer; 5159 struct buffer_page *bpage; 5160 u64 ret = 0; 5161 5162 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5163 return 0; 5164 5165 cpu_buffer = buffer->buffers[cpu]; 5166 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 5167 /* 5168 * if the tail is on reader_page, oldest time stamp is on the reader 5169 * page 5170 */ 5171 if (cpu_buffer->tail_page == cpu_buffer->reader_page) 5172 bpage = cpu_buffer->reader_page; 5173 else 5174 bpage = rb_set_head_page(cpu_buffer); 5175 if (bpage) 5176 ret = bpage->page->time_stamp; 5177 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 5178 5179 return ret; 5180 } 5181 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts); 5182 5183 /** 5184 * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer 5185 * @buffer: The ring buffer 5186 * @cpu: The per CPU buffer to read from. 5187 */ 5188 unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu) 5189 { 5190 struct ring_buffer_per_cpu *cpu_buffer; 5191 unsigned long ret; 5192 5193 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5194 return 0; 5195 5196 cpu_buffer = buffer->buffers[cpu]; 5197 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes; 5198 5199 return ret; 5200 } 5201 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu); 5202 5203 /** 5204 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer 5205 * @buffer: The ring buffer 5206 * @cpu: The per CPU buffer to get the entries from. 5207 */ 5208 unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu) 5209 { 5210 struct ring_buffer_per_cpu *cpu_buffer; 5211 5212 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5213 return 0; 5214 5215 cpu_buffer = buffer->buffers[cpu]; 5216 5217 return rb_num_of_entries(cpu_buffer); 5218 } 5219 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); 5220 5221 /** 5222 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring 5223 * buffer wrapping around (only if RB_FL_OVERWRITE is on). 5224 * @buffer: The ring buffer 5225 * @cpu: The per CPU buffer to get the number of overruns from 5226 */ 5227 unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu) 5228 { 5229 struct ring_buffer_per_cpu *cpu_buffer; 5230 unsigned long ret; 5231 5232 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5233 return 0; 5234 5235 cpu_buffer = buffer->buffers[cpu]; 5236 ret = local_read(&cpu_buffer->overrun); 5237 5238 return ret; 5239 } 5240 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); 5241 5242 /** 5243 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by 5244 * commits failing due to the buffer wrapping around while there are uncommitted 5245 * events, such as during an interrupt storm. 5246 * @buffer: The ring buffer 5247 * @cpu: The per CPU buffer to get the number of overruns from 5248 */ 5249 unsigned long 5250 ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu) 5251 { 5252 struct ring_buffer_per_cpu *cpu_buffer; 5253 unsigned long ret; 5254 5255 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5256 return 0; 5257 5258 cpu_buffer = buffer->buffers[cpu]; 5259 ret = local_read(&cpu_buffer->commit_overrun); 5260 5261 return ret; 5262 } 5263 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); 5264 5265 /** 5266 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by 5267 * the ring buffer filling up (only if RB_FL_OVERWRITE is off). 5268 * @buffer: The ring buffer 5269 * @cpu: The per CPU buffer to get the number of overruns from 5270 */ 5271 unsigned long 5272 ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu) 5273 { 5274 struct ring_buffer_per_cpu *cpu_buffer; 5275 unsigned long ret; 5276 5277 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5278 return 0; 5279 5280 cpu_buffer = buffer->buffers[cpu]; 5281 ret = local_read(&cpu_buffer->dropped_events); 5282 5283 return ret; 5284 } 5285 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu); 5286 5287 /** 5288 * ring_buffer_read_events_cpu - get the number of events successfully read 5289 * @buffer: The ring buffer 5290 * @cpu: The per CPU buffer to get the number of events read 5291 */ 5292 unsigned long 5293 ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu) 5294 { 5295 struct ring_buffer_per_cpu *cpu_buffer; 5296 5297 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 5298 return 0; 5299 5300 cpu_buffer = buffer->buffers[cpu]; 5301 return cpu_buffer->read; 5302 } 5303 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu); 5304 5305 /** 5306 * ring_buffer_entries - get the number of entries in a buffer 5307 * @buffer: The ring buffer 5308 * 5309 * Returns the total number of entries in the ring buffer 5310 * (all CPU entries) 5311 */ 5312 unsigned long ring_buffer_entries(struct trace_buffer *buffer) 5313 { 5314 struct ring_buffer_per_cpu *cpu_buffer; 5315 unsigned long entries = 0; 5316 int cpu; 5317 5318 /* if you care about this being correct, lock the buffer */ 5319 for_each_buffer_cpu(buffer, cpu) { 5320 cpu_buffer = buffer->buffers[cpu]; 5321 entries += rb_num_of_entries(cpu_buffer); 5322 } 5323 5324 return entries; 5325 } 5326 EXPORT_SYMBOL_GPL(ring_buffer_entries); 5327 5328 /** 5329 * ring_buffer_overruns - get the number of overruns in buffer 5330 * @buffer: The ring buffer 5331 * 5332 * Returns the total number of overruns in the ring buffer 5333 * (all CPU entries) 5334 */ 5335 unsigned long ring_buffer_overruns(struct trace_buffer *buffer) 5336 { 5337 struct ring_buffer_per_cpu *cpu_buffer; 5338 unsigned long overruns = 0; 5339 int cpu; 5340 5341 /* if you care about this being correct, lock the buffer */ 5342 for_each_buffer_cpu(buffer, cpu) { 5343 cpu_buffer = buffer->buffers[cpu]; 5344 overruns += local_read(&cpu_buffer->overrun); 5345 } 5346 5347 return overruns; 5348 } 5349 EXPORT_SYMBOL_GPL(ring_buffer_overruns); 5350 5351 static bool rb_read_remote_meta_page(struct ring_buffer_per_cpu *cpu_buffer) 5352 { 5353 local_set(&cpu_buffer->entries, READ_ONCE(cpu_buffer->meta_page->entries)); 5354 local_set(&cpu_buffer->overrun, READ_ONCE(cpu_buffer->meta_page->overrun)); 5355 local_set(&cpu_buffer->pages_touched, READ_ONCE(cpu_buffer->meta_page->pages_touched)); 5356 local_set(&cpu_buffer->pages_lost, READ_ONCE(cpu_buffer->meta_page->pages_lost)); 5357 5358 return rb_num_of_entries(cpu_buffer); 5359 } 5360 5361 static void rb_update_remote_head(struct ring_buffer_per_cpu *cpu_buffer) 5362 { 5363 struct buffer_page *next, *orig; 5364 int retry = 3; 5365 5366 orig = next = cpu_buffer->head_page; 5367 rb_inc_page(&next); 5368 5369 /* Run after the writer */ 5370 while (cpu_buffer->head_page->page->time_stamp > next->page->time_stamp) { 5371 rb_inc_page(&next); 5372 5373 rb_list_head_clear(cpu_buffer->head_page->list.prev); 5374 rb_inc_page(&cpu_buffer->head_page); 5375 rb_set_list_to_head(cpu_buffer->head_page->list.prev); 5376 5377 if (cpu_buffer->head_page == orig) { 5378 if (WARN_ON_ONCE(!(--retry))) 5379 return; 5380 } 5381 } 5382 5383 orig = cpu_buffer->commit_page = cpu_buffer->head_page; 5384 retry = 3; 5385 5386 while (cpu_buffer->commit_page->page->time_stamp < next->page->time_stamp) { 5387 rb_inc_page(&next); 5388 rb_inc_page(&cpu_buffer->commit_page); 5389 5390 if (cpu_buffer->commit_page == orig) { 5391 if (WARN_ON_ONCE(!(--retry))) 5392 return; 5393 } 5394 } 5395 } 5396 5397 static void rb_iter_reset(struct ring_buffer_iter *iter) 5398 { 5399 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 5400 5401 if (cpu_buffer->remote) { 5402 rb_read_remote_meta_page(cpu_buffer); 5403 rb_update_remote_head(cpu_buffer); 5404 } 5405 5406 /* Iterator usage is expected to have record disabled */ 5407 iter->head_page = cpu_buffer->reader_page; 5408 iter->head = cpu_buffer->reader_page->read; 5409 iter->next_event = iter->head; 5410 5411 iter->cache_reader_page = iter->head_page; 5412 iter->cache_read = cpu_buffer->read; 5413 iter->cache_pages_removed = cpu_buffer->pages_removed; 5414 5415 if (iter->head) { 5416 iter->read_stamp = cpu_buffer->read_stamp; 5417 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp; 5418 } else { 5419 iter->read_stamp = iter->head_page->page->time_stamp; 5420 iter->page_stamp = iter->read_stamp; 5421 } 5422 } 5423 5424 /** 5425 * ring_buffer_iter_reset - reset an iterator 5426 * @iter: The iterator to reset 5427 * 5428 * Resets the iterator, so that it will start from the beginning 5429 * again. 5430 */ 5431 void ring_buffer_iter_reset(struct ring_buffer_iter *iter) 5432 { 5433 struct ring_buffer_per_cpu *cpu_buffer; 5434 unsigned long flags; 5435 5436 if (!iter) 5437 return; 5438 5439 cpu_buffer = iter->cpu_buffer; 5440 5441 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 5442 rb_iter_reset(iter); 5443 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 5444 } 5445 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); 5446 5447 /** 5448 * ring_buffer_iter_empty - check if an iterator has no more to read 5449 * @iter: The iterator to check 5450 */ 5451 int ring_buffer_iter_empty(struct ring_buffer_iter *iter) 5452 { 5453 struct ring_buffer_per_cpu *cpu_buffer; 5454 struct buffer_page *reader; 5455 struct buffer_page *head_page; 5456 struct buffer_page *commit_page; 5457 struct buffer_page *curr_commit_page; 5458 unsigned commit; 5459 u64 curr_commit_ts; 5460 u64 commit_ts; 5461 5462 cpu_buffer = iter->cpu_buffer; 5463 reader = cpu_buffer->reader_page; 5464 head_page = cpu_buffer->head_page; 5465 commit_page = READ_ONCE(cpu_buffer->commit_page); 5466 commit_ts = commit_page->page->time_stamp; 5467 5468 /* 5469 * When the writer goes across pages, it issues a cmpxchg which 5470 * is a mb(), which will synchronize with the rmb here. 5471 * (see rb_tail_page_update()) 5472 */ 5473 smp_rmb(); 5474 commit = rb_page_commit(commit_page); 5475 /* We want to make sure that the commit page doesn't change */ 5476 smp_rmb(); 5477 5478 /* Make sure commit page didn't change */ 5479 curr_commit_page = READ_ONCE(cpu_buffer->commit_page); 5480 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp); 5481 5482 /* If the commit page changed, then there's more data */ 5483 if (curr_commit_page != commit_page || 5484 curr_commit_ts != commit_ts) 5485 return 0; 5486 5487 /* Still racy, as it may return a false positive, but that's OK */ 5488 return ((iter->head_page == commit_page && iter->head >= commit) || 5489 (iter->head_page == reader && commit_page == head_page && 5490 head_page->read == commit && 5491 iter->head == rb_page_size(cpu_buffer->reader_page))); 5492 } 5493 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); 5494 5495 static void 5496 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, 5497 struct ring_buffer_event *event) 5498 { 5499 u64 delta; 5500 5501 switch (event->type_len) { 5502 case RINGBUF_TYPE_PADDING: 5503 return; 5504 5505 case RINGBUF_TYPE_TIME_EXTEND: 5506 delta = rb_event_time_stamp(event); 5507 cpu_buffer->read_stamp += delta; 5508 return; 5509 5510 case RINGBUF_TYPE_TIME_STAMP: 5511 delta = rb_event_time_stamp(event); 5512 delta = rb_fix_abs_ts(delta, cpu_buffer->read_stamp); 5513 cpu_buffer->read_stamp = delta; 5514 return; 5515 5516 case RINGBUF_TYPE_DATA: 5517 cpu_buffer->read_stamp += event->time_delta; 5518 return; 5519 5520 default: 5521 RB_WARN_ON(cpu_buffer, 1); 5522 } 5523 } 5524 5525 static void 5526 rb_update_iter_read_stamp(struct ring_buffer_iter *iter, 5527 struct ring_buffer_event *event) 5528 { 5529 u64 delta; 5530 5531 switch (event->type_len) { 5532 case RINGBUF_TYPE_PADDING: 5533 return; 5534 5535 case RINGBUF_TYPE_TIME_EXTEND: 5536 delta = rb_event_time_stamp(event); 5537 iter->read_stamp += delta; 5538 return; 5539 5540 case RINGBUF_TYPE_TIME_STAMP: 5541 delta = rb_event_time_stamp(event); 5542 delta = rb_fix_abs_ts(delta, iter->read_stamp); 5543 iter->read_stamp = delta; 5544 return; 5545 5546 case RINGBUF_TYPE_DATA: 5547 iter->read_stamp += event->time_delta; 5548 return; 5549 5550 default: 5551 RB_WARN_ON(iter->cpu_buffer, 1); 5552 } 5553 } 5554 5555 static struct buffer_page * 5556 __rb_get_reader_page_from_remote(struct ring_buffer_per_cpu *cpu_buffer) 5557 { 5558 struct buffer_page *new_reader, *prev_reader, *prev_head, *new_head, *last; 5559 5560 if (!rb_read_remote_meta_page(cpu_buffer)) 5561 return NULL; 5562 5563 /* More to read on the reader page */ 5564 if (cpu_buffer->reader_page->read < rb_page_size(cpu_buffer->reader_page)) { 5565 if (!cpu_buffer->reader_page->read) 5566 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; 5567 return cpu_buffer->reader_page; 5568 } 5569 5570 prev_reader = cpu_buffer->subbuf_ids[cpu_buffer->meta_page->reader.id]; 5571 5572 WARN_ON_ONCE(cpu_buffer->remote->swap_reader_page(cpu_buffer->cpu, 5573 cpu_buffer->remote->priv)); 5574 /* nr_pages doesn't include the reader page */ 5575 if (WARN_ON_ONCE(cpu_buffer->meta_page->reader.id > cpu_buffer->nr_pages)) 5576 return NULL; 5577 5578 new_reader = cpu_buffer->subbuf_ids[cpu_buffer->meta_page->reader.id]; 5579 5580 WARN_ON_ONCE(prev_reader == new_reader); 5581 5582 prev_head = new_reader; /* New reader was also the previous head */ 5583 new_head = prev_head; 5584 rb_inc_page(&new_head); 5585 last = prev_head; 5586 rb_dec_page(&last); 5587 5588 /* Clear the old HEAD flag */ 5589 rb_list_head_clear(cpu_buffer->head_page->list.prev); 5590 5591 prev_reader->list.next = prev_head->list.next; 5592 prev_reader->list.prev = prev_head->list.prev; 5593 5594 /* Swap prev_reader with new_reader */ 5595 last->list.next = &prev_reader->list; 5596 new_head->list.prev = &prev_reader->list; 5597 5598 new_reader->list.prev = &new_reader->list; 5599 new_reader->list.next = &new_head->list; 5600 5601 /* Reactivate the HEAD flag */ 5602 rb_set_list_to_head(&last->list); 5603 5604 cpu_buffer->head_page = new_head; 5605 cpu_buffer->reader_page = new_reader; 5606 cpu_buffer->pages = &new_head->list; 5607 cpu_buffer->read_stamp = new_reader->page->time_stamp; 5608 cpu_buffer->lost_events = cpu_buffer->meta_page->reader.lost_events; 5609 5610 return rb_page_size(cpu_buffer->reader_page) ? cpu_buffer->reader_page : NULL; 5611 } 5612 5613 static struct buffer_page * 5614 __rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) 5615 { 5616 struct buffer_page *reader = NULL; 5617 unsigned long bsize = READ_ONCE(cpu_buffer->buffer->subbuf_size); 5618 unsigned long overwrite; 5619 unsigned long flags; 5620 int nr_loops = 0; 5621 bool ret; 5622 5623 local_irq_save(flags); 5624 arch_spin_lock(&cpu_buffer->lock); 5625 5626 again: 5627 /* 5628 * This should normally only loop twice. But because the 5629 * start of the reader inserts an empty page, it causes 5630 * a case where we will loop three times. There should be no 5631 * reason to loop four times (that I know of). 5632 */ 5633 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { 5634 reader = NULL; 5635 goto out; 5636 } 5637 5638 reader = cpu_buffer->reader_page; 5639 5640 /* If there's more to read, return this page */ 5641 if (cpu_buffer->reader_page->read < rb_page_size(reader)) 5642 goto out; 5643 5644 /* Never should we have an index greater than the size */ 5645 if (RB_WARN_ON(cpu_buffer, 5646 cpu_buffer->reader_page->read > rb_page_size(reader))) 5647 goto out; 5648 5649 /* check if we caught up to the tail */ 5650 reader = NULL; 5651 if (cpu_buffer->commit_page == cpu_buffer->reader_page) 5652 goto out; 5653 5654 /* Don't bother swapping if the ring buffer is empty */ 5655 if (rb_num_of_entries(cpu_buffer) == 0) 5656 goto out; 5657 5658 /* 5659 * Reset the reader page to size zero. 5660 */ 5661 local_set(&cpu_buffer->reader_page->write, 0); 5662 local_set(&cpu_buffer->reader_page->entries, 0); 5663 cpu_buffer->reader_page->real_end = 0; 5664 5665 spin: 5666 /* 5667 * Splice the empty reader page into the list around the head. 5668 */ 5669 reader = rb_set_head_page(cpu_buffer); 5670 if (!reader) 5671 goto out; 5672 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); 5673 cpu_buffer->reader_page->list.prev = reader->list.prev; 5674 5675 /* 5676 * cpu_buffer->pages just needs to point to the buffer, it 5677 * has no specific buffer page to point to. Lets move it out 5678 * of our way so we don't accidentally swap it. 5679 */ 5680 cpu_buffer->pages = reader->list.prev; 5681 5682 /* The reader page will be pointing to the new head */ 5683 rb_set_list_to_head(&cpu_buffer->reader_page->list); 5684 5685 /* 5686 * We want to make sure we read the overruns after we set up our 5687 * pointers to the next object. The writer side does a 5688 * cmpxchg to cross pages which acts as the mb on the writer 5689 * side. Note, the reader will constantly fail the swap 5690 * while the writer is updating the pointers, so this 5691 * guarantees that the overwrite recorded here is the one we 5692 * want to compare with the last_overrun. 5693 */ 5694 smp_mb(); 5695 overwrite = local_read(&(cpu_buffer->overrun)); 5696 5697 /* 5698 * Here's the tricky part. 5699 * 5700 * We need to move the pointer past the header page. 5701 * But we can only do that if a writer is not currently 5702 * moving it. The page before the header page has the 5703 * flag bit '1' set if it is pointing to the page we want. 5704 * but if the writer is in the process of moving it 5705 * then it will be '2' or already moved '0'. 5706 */ 5707 5708 ret = rb_head_page_replace(reader, cpu_buffer->reader_page); 5709 5710 /* 5711 * If we did not convert it, then we must try again. 5712 */ 5713 if (!ret) 5714 goto spin; 5715 5716 if (cpu_buffer->ring_meta) 5717 rb_update_meta_reader(cpu_buffer, reader); 5718 5719 /* 5720 * Yay! We succeeded in replacing the page. 5721 * 5722 * Now make the new head point back to the reader page. 5723 */ 5724 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; 5725 rb_inc_page(&cpu_buffer->head_page); 5726 5727 cpu_buffer->cnt++; 5728 local_inc(&cpu_buffer->pages_read); 5729 5730 /* Finally update the reader page to the new head */ 5731 cpu_buffer->reader_page = reader; 5732 cpu_buffer->reader_page->read = 0; 5733 5734 if (overwrite != cpu_buffer->last_overrun) { 5735 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; 5736 cpu_buffer->last_overrun = overwrite; 5737 } 5738 5739 goto again; 5740 5741 out: 5742 /* Update the read_stamp on the first event */ 5743 if (reader && reader->read == 0) 5744 cpu_buffer->read_stamp = reader->page->time_stamp; 5745 5746 arch_spin_unlock(&cpu_buffer->lock); 5747 local_irq_restore(flags); 5748 5749 /* 5750 * The writer has preempt disable, wait for it. But not forever 5751 * Although, 1 second is pretty much "forever" 5752 */ 5753 #define USECS_WAIT 1000000 5754 for (nr_loops = 0; nr_loops < USECS_WAIT; nr_loops++) { 5755 /* If the write is past the end of page, a writer is still updating it */ 5756 if (likely(!reader || rb_page_write(reader) <= bsize)) 5757 break; 5758 5759 udelay(1); 5760 5761 /* Get the latest version of the reader write value */ 5762 smp_rmb(); 5763 } 5764 5765 /* The writer is not moving forward? Something is wrong */ 5766 if (RB_WARN_ON(cpu_buffer, nr_loops == USECS_WAIT)) 5767 reader = NULL; 5768 5769 /* 5770 * Make sure we see any padding after the write update 5771 * (see rb_reset_tail()). 5772 * 5773 * In addition, a writer may be writing on the reader page 5774 * if the page has not been fully filled, so the read barrier 5775 * is also needed to make sure we see the content of what is 5776 * committed by the writer (see rb_set_commit_to_write()). 5777 */ 5778 smp_rmb(); 5779 5780 5781 return reader; 5782 } 5783 5784 static struct buffer_page * 5785 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) 5786 { 5787 return cpu_buffer->remote ? __rb_get_reader_page_from_remote(cpu_buffer) : 5788 __rb_get_reader_page(cpu_buffer); 5789 } 5790 5791 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) 5792 { 5793 struct ring_buffer_event *event; 5794 struct buffer_page *reader; 5795 unsigned length; 5796 5797 reader = rb_get_reader_page(cpu_buffer); 5798 5799 /* This function should not be called when buffer is empty */ 5800 if (RB_WARN_ON(cpu_buffer, !reader)) 5801 return; 5802 5803 event = rb_reader_event(cpu_buffer); 5804 5805 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) 5806 cpu_buffer->read++; 5807 5808 rb_update_read_stamp(cpu_buffer, event); 5809 5810 length = rb_event_length(event); 5811 cpu_buffer->reader_page->read += length; 5812 cpu_buffer->read_bytes += length; 5813 } 5814 5815 static void rb_advance_iter(struct ring_buffer_iter *iter) 5816 { 5817 struct ring_buffer_per_cpu *cpu_buffer; 5818 5819 cpu_buffer = iter->cpu_buffer; 5820 5821 /* If head == next_event then we need to jump to the next event */ 5822 if (iter->head == iter->next_event) { 5823 /* If the event gets overwritten again, there's nothing to do */ 5824 if (rb_iter_head_event(iter) == NULL) 5825 return; 5826 } 5827 5828 iter->head = iter->next_event; 5829 5830 /* 5831 * Check if we are at the end of the buffer. 5832 */ 5833 if (iter->next_event >= rb_page_size(iter->head_page)) { 5834 /* discarded commits can make the page empty */ 5835 if (iter->head_page == cpu_buffer->commit_page) 5836 return; 5837 rb_inc_iter(iter); 5838 return; 5839 } 5840 5841 rb_update_iter_read_stamp(iter, iter->event); 5842 } 5843 5844 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) 5845 { 5846 return cpu_buffer->lost_events; 5847 } 5848 5849 static struct ring_buffer_event * 5850 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, 5851 unsigned long *lost_events) 5852 { 5853 struct ring_buffer_event *event; 5854 struct buffer_page *reader; 5855 int nr_loops = 0; 5856 5857 if (ts) 5858 *ts = 0; 5859 again: 5860 /* 5861 * We repeat when a time extend is encountered. 5862 * Since the time extend is always attached to a data event, 5863 * we should never loop more than once. 5864 * (We never hit the following condition more than twice). 5865 */ 5866 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) 5867 return NULL; 5868 5869 reader = rb_get_reader_page(cpu_buffer); 5870 if (!reader) 5871 return NULL; 5872 5873 event = rb_reader_event(cpu_buffer); 5874 5875 switch (event->type_len) { 5876 case RINGBUF_TYPE_PADDING: 5877 if (rb_null_event(event)) 5878 RB_WARN_ON(cpu_buffer, 1); 5879 /* 5880 * Because the writer could be discarding every 5881 * event it creates (which would probably be bad) 5882 * if we were to go back to "again" then we may never 5883 * catch up, and will trigger the warn on, or lock 5884 * the box. Return the padding, and we will release 5885 * the current locks, and try again. 5886 */ 5887 return event; 5888 5889 case RINGBUF_TYPE_TIME_EXTEND: 5890 /* Internal data, OK to advance */ 5891 rb_advance_reader(cpu_buffer); 5892 goto again; 5893 5894 case RINGBUF_TYPE_TIME_STAMP: 5895 if (ts) { 5896 *ts = rb_event_time_stamp(event); 5897 *ts = rb_fix_abs_ts(*ts, reader->page->time_stamp); 5898 ring_buffer_normalize_time_stamp(cpu_buffer->buffer, 5899 cpu_buffer->cpu, ts); 5900 } 5901 /* Internal data, OK to advance */ 5902 rb_advance_reader(cpu_buffer); 5903 goto again; 5904 5905 case RINGBUF_TYPE_DATA: 5906 if (ts && !(*ts)) { 5907 *ts = cpu_buffer->read_stamp + event->time_delta; 5908 ring_buffer_normalize_time_stamp(cpu_buffer->buffer, 5909 cpu_buffer->cpu, ts); 5910 } 5911 if (lost_events) 5912 *lost_events = rb_lost_events(cpu_buffer); 5913 return event; 5914 5915 default: 5916 RB_WARN_ON(cpu_buffer, 1); 5917 } 5918 5919 return NULL; 5920 } 5921 EXPORT_SYMBOL_GPL(ring_buffer_peek); 5922 5923 static struct ring_buffer_event * 5924 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) 5925 { 5926 struct trace_buffer *buffer; 5927 struct ring_buffer_per_cpu *cpu_buffer; 5928 struct ring_buffer_event *event; 5929 int nr_loops = 0; 5930 5931 if (ts) 5932 *ts = 0; 5933 5934 cpu_buffer = iter->cpu_buffer; 5935 buffer = cpu_buffer->buffer; 5936 5937 /* 5938 * Check if someone performed a consuming read to the buffer 5939 * or removed some pages from the buffer. In these cases, 5940 * iterator was invalidated and we need to reset it. 5941 */ 5942 if (unlikely(iter->cache_read != cpu_buffer->read || 5943 iter->cache_reader_page != cpu_buffer->reader_page || 5944 iter->cache_pages_removed != cpu_buffer->pages_removed)) 5945 rb_iter_reset(iter); 5946 5947 again: 5948 if (ring_buffer_iter_empty(iter)) 5949 return NULL; 5950 5951 /* 5952 * As the writer can mess with what the iterator is trying 5953 * to read, just give up if we fail to get an event after 5954 * three tries. The iterator is not as reliable when reading 5955 * the ring buffer with an active write as the consumer is. 5956 * Do not warn if the three failures is reached. 5957 */ 5958 if (++nr_loops > 3) 5959 return NULL; 5960 5961 if (rb_per_cpu_empty(cpu_buffer)) 5962 return NULL; 5963 5964 if (iter->head >= rb_page_size(iter->head_page)) { 5965 rb_inc_iter(iter); 5966 goto again; 5967 } 5968 5969 event = rb_iter_head_event(iter); 5970 if (!event) 5971 goto again; 5972 5973 switch (event->type_len) { 5974 case RINGBUF_TYPE_PADDING: 5975 if (rb_null_event(event)) { 5976 rb_inc_iter(iter); 5977 goto again; 5978 } 5979 rb_advance_iter(iter); 5980 return event; 5981 5982 case RINGBUF_TYPE_TIME_EXTEND: 5983 /* Internal data, OK to advance */ 5984 rb_advance_iter(iter); 5985 goto again; 5986 5987 case RINGBUF_TYPE_TIME_STAMP: 5988 if (ts) { 5989 *ts = rb_event_time_stamp(event); 5990 *ts = rb_fix_abs_ts(*ts, iter->head_page->page->time_stamp); 5991 ring_buffer_normalize_time_stamp(cpu_buffer->buffer, 5992 cpu_buffer->cpu, ts); 5993 } 5994 /* Internal data, OK to advance */ 5995 rb_advance_iter(iter); 5996 goto again; 5997 5998 case RINGBUF_TYPE_DATA: 5999 if (ts && !(*ts)) { 6000 *ts = iter->read_stamp + event->time_delta; 6001 ring_buffer_normalize_time_stamp(buffer, 6002 cpu_buffer->cpu, ts); 6003 } 6004 return event; 6005 6006 default: 6007 RB_WARN_ON(cpu_buffer, 1); 6008 } 6009 6010 return NULL; 6011 } 6012 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); 6013 6014 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer) 6015 { 6016 if (likely(!in_nmi())) { 6017 raw_spin_lock(&cpu_buffer->reader_lock); 6018 return true; 6019 } 6020 6021 /* 6022 * If an NMI die dumps out the content of the ring buffer 6023 * trylock must be used to prevent a deadlock if the NMI 6024 * preempted a task that holds the ring buffer locks. If 6025 * we get the lock then all is fine, if not, then continue 6026 * to do the read, but this can corrupt the ring buffer, 6027 * so it must be permanently disabled from future writes. 6028 * Reading from NMI is a oneshot deal. 6029 */ 6030 if (raw_spin_trylock(&cpu_buffer->reader_lock)) 6031 return true; 6032 6033 /* Continue without locking, but disable the ring buffer */ 6034 atomic_inc(&cpu_buffer->record_disabled); 6035 return false; 6036 } 6037 6038 static inline void 6039 rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked) 6040 { 6041 if (likely(locked)) 6042 raw_spin_unlock(&cpu_buffer->reader_lock); 6043 } 6044 6045 /** 6046 * ring_buffer_peek - peek at the next event to be read 6047 * @buffer: The ring buffer to read 6048 * @cpu: The cpu to peak at 6049 * @ts: The timestamp counter of this event. 6050 * @lost_events: a variable to store if events were lost (may be NULL) 6051 * 6052 * This will return the event that will be read next, but does 6053 * not consume the data. 6054 */ 6055 struct ring_buffer_event * 6056 ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts, 6057 unsigned long *lost_events) 6058 { 6059 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; 6060 struct ring_buffer_event *event; 6061 unsigned long flags; 6062 bool dolock; 6063 6064 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6065 return NULL; 6066 6067 again: 6068 local_irq_save(flags); 6069 dolock = rb_reader_lock(cpu_buffer); 6070 event = rb_buffer_peek(cpu_buffer, ts, lost_events); 6071 if (event && event->type_len == RINGBUF_TYPE_PADDING) 6072 rb_advance_reader(cpu_buffer); 6073 rb_reader_unlock(cpu_buffer, dolock); 6074 local_irq_restore(flags); 6075 6076 if (event && event->type_len == RINGBUF_TYPE_PADDING) 6077 goto again; 6078 6079 return event; 6080 } 6081 6082 /** ring_buffer_iter_dropped - report if there are dropped events 6083 * @iter: The ring buffer iterator 6084 * 6085 * Returns true if there was dropped events since the last peek. 6086 */ 6087 bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter) 6088 { 6089 bool ret = iter->missed_events != 0; 6090 6091 iter->missed_events = 0; 6092 return ret; 6093 } 6094 EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped); 6095 6096 /** 6097 * ring_buffer_iter_peek - peek at the next event to be read 6098 * @iter: The ring buffer iterator 6099 * @ts: The timestamp counter of this event. 6100 * 6101 * This will return the event that will be read next, but does 6102 * not increment the iterator. 6103 */ 6104 struct ring_buffer_event * 6105 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) 6106 { 6107 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 6108 struct ring_buffer_event *event; 6109 unsigned long flags; 6110 6111 again: 6112 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 6113 event = rb_iter_peek(iter, ts); 6114 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 6115 6116 if (event && event->type_len == RINGBUF_TYPE_PADDING) 6117 goto again; 6118 6119 return event; 6120 } 6121 6122 /** 6123 * ring_buffer_consume - return an event and consume it 6124 * @buffer: The ring buffer to get the next event from 6125 * @cpu: the cpu to read the buffer from 6126 * @ts: a variable to store the timestamp (may be NULL) 6127 * @lost_events: a variable to store if events were lost (may be NULL) 6128 * 6129 * Returns the next event in the ring buffer, and that event is consumed. 6130 * Meaning, that sequential reads will keep returning a different event, 6131 * and eventually empty the ring buffer if the producer is slower. 6132 */ 6133 struct ring_buffer_event * 6134 ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts, 6135 unsigned long *lost_events) 6136 { 6137 struct ring_buffer_per_cpu *cpu_buffer; 6138 struct ring_buffer_event *event = NULL; 6139 unsigned long flags; 6140 bool dolock; 6141 6142 again: 6143 /* might be called in atomic */ 6144 preempt_disable(); 6145 6146 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6147 goto out; 6148 6149 cpu_buffer = buffer->buffers[cpu]; 6150 local_irq_save(flags); 6151 dolock = rb_reader_lock(cpu_buffer); 6152 6153 event = rb_buffer_peek(cpu_buffer, ts, lost_events); 6154 if (event) { 6155 cpu_buffer->lost_events = 0; 6156 rb_advance_reader(cpu_buffer); 6157 } 6158 6159 rb_reader_unlock(cpu_buffer, dolock); 6160 local_irq_restore(flags); 6161 6162 out: 6163 preempt_enable(); 6164 6165 if (event && event->type_len == RINGBUF_TYPE_PADDING) 6166 goto again; 6167 6168 return event; 6169 } 6170 EXPORT_SYMBOL_GPL(ring_buffer_consume); 6171 6172 /** 6173 * ring_buffer_read_start - start a non consuming read of the buffer 6174 * @buffer: The ring buffer to read from 6175 * @cpu: The cpu buffer to iterate over 6176 * @flags: gfp flags to use for memory allocation 6177 * 6178 * This creates an iterator to allow non-consuming iteration through 6179 * the buffer. If the buffer is disabled for writing, it will produce 6180 * the same information each time, but if the buffer is still writing 6181 * then the first hit of a write will cause the iteration to stop. 6182 * 6183 * Must be paired with ring_buffer_read_finish. 6184 */ 6185 struct ring_buffer_iter * 6186 ring_buffer_read_start(struct trace_buffer *buffer, int cpu, gfp_t flags) 6187 { 6188 struct ring_buffer_per_cpu *cpu_buffer; 6189 struct ring_buffer_iter *iter; 6190 6191 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6192 return NULL; 6193 6194 iter = kzalloc_obj(*iter, flags); 6195 if (!iter) 6196 return NULL; 6197 6198 /* Holds the entire event: data and meta data */ 6199 iter->event_size = buffer->subbuf_size; 6200 iter->event = kmalloc(iter->event_size, flags); 6201 if (!iter->event) { 6202 kfree(iter); 6203 return NULL; 6204 } 6205 6206 cpu_buffer = buffer->buffers[cpu]; 6207 6208 iter->cpu_buffer = cpu_buffer; 6209 6210 atomic_inc(&cpu_buffer->resize_disabled); 6211 6212 guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock); 6213 arch_spin_lock(&cpu_buffer->lock); 6214 rb_iter_reset(iter); 6215 arch_spin_unlock(&cpu_buffer->lock); 6216 6217 return iter; 6218 } 6219 EXPORT_SYMBOL_GPL(ring_buffer_read_start); 6220 6221 /** 6222 * ring_buffer_read_finish - finish reading the iterator of the buffer 6223 * @iter: The iterator retrieved by ring_buffer_start 6224 * 6225 * This re-enables resizing of the buffer, and frees the iterator. 6226 */ 6227 void 6228 ring_buffer_read_finish(struct ring_buffer_iter *iter) 6229 { 6230 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 6231 6232 /* Use this opportunity to check the integrity of the ring buffer. */ 6233 rb_check_pages(cpu_buffer); 6234 6235 atomic_dec(&cpu_buffer->resize_disabled); 6236 kfree(iter->event); 6237 kfree(iter); 6238 } 6239 EXPORT_SYMBOL_GPL(ring_buffer_read_finish); 6240 6241 /** 6242 * ring_buffer_iter_advance - advance the iterator to the next location 6243 * @iter: The ring buffer iterator 6244 * 6245 * Move the location of the iterator such that the next read will 6246 * be the next location of the iterator. 6247 */ 6248 void ring_buffer_iter_advance(struct ring_buffer_iter *iter) 6249 { 6250 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 6251 unsigned long flags; 6252 6253 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 6254 6255 rb_advance_iter(iter); 6256 6257 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 6258 } 6259 EXPORT_SYMBOL_GPL(ring_buffer_iter_advance); 6260 6261 /** 6262 * ring_buffer_size - return the size of the ring buffer (in bytes) 6263 * @buffer: The ring buffer. 6264 * @cpu: The CPU to get ring buffer size from. 6265 */ 6266 unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu) 6267 { 6268 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6269 return 0; 6270 6271 return buffer->subbuf_size * buffer->buffers[cpu]->nr_pages; 6272 } 6273 EXPORT_SYMBOL_GPL(ring_buffer_size); 6274 6275 /** 6276 * ring_buffer_max_event_size - return the max data size of an event 6277 * @buffer: The ring buffer. 6278 * 6279 * Returns the maximum size an event can be. 6280 */ 6281 unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer) 6282 { 6283 /* If abs timestamp is requested, events have a timestamp too */ 6284 if (ring_buffer_time_stamp_abs(buffer)) 6285 return buffer->max_data_size - RB_LEN_TIME_EXTEND; 6286 return buffer->max_data_size; 6287 } 6288 EXPORT_SYMBOL_GPL(ring_buffer_max_event_size); 6289 6290 static void rb_clear_buffer_page(struct buffer_page *page) 6291 { 6292 local_set(&page->write, 0); 6293 local_set(&page->entries, 0); 6294 rb_init_page(page->page); 6295 page->read = 0; 6296 } 6297 6298 /* 6299 * When the buffer is memory mapped to user space, each sub buffer 6300 * has a unique id that is used by the meta data to tell the user 6301 * where the current reader page is. 6302 * 6303 * For a normal allocated ring buffer, the id is saved in the buffer page 6304 * id field, and updated via this function. 6305 * 6306 * But for a fixed memory mapped buffer, the id is already assigned for 6307 * fixed memory ordering in the memory layout and can not be used. Instead 6308 * the index of where the page lies in the memory layout is used. 6309 * 6310 * For the normal pages, set the buffer page id with the passed in @id 6311 * value and return that. 6312 * 6313 * For fixed memory mapped pages, get the page index in the memory layout 6314 * and return that as the id. 6315 */ 6316 static int rb_page_id(struct ring_buffer_per_cpu *cpu_buffer, 6317 struct buffer_page *bpage, int id) 6318 { 6319 /* 6320 * For boot buffers, the id is the index, 6321 * otherwise, set the buffer page with this id 6322 */ 6323 if (cpu_buffer->ring_meta) 6324 id = rb_meta_subbuf_idx(cpu_buffer->ring_meta, bpage->page); 6325 else 6326 bpage->id = id; 6327 6328 return id; 6329 } 6330 6331 static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer) 6332 { 6333 struct trace_buffer_meta *meta = cpu_buffer->meta_page; 6334 6335 if (!meta) 6336 return; 6337 6338 meta->reader.read = cpu_buffer->reader_page->read; 6339 meta->reader.id = rb_page_id(cpu_buffer, cpu_buffer->reader_page, 6340 cpu_buffer->reader_page->id); 6341 6342 meta->reader.lost_events = cpu_buffer->lost_events; 6343 6344 meta->entries = local_read(&cpu_buffer->entries); 6345 meta->overrun = local_read(&cpu_buffer->overrun); 6346 meta->read = cpu_buffer->read; 6347 meta->pages_lost = local_read(&cpu_buffer->pages_lost); 6348 meta->pages_touched = local_read(&cpu_buffer->pages_touched); 6349 6350 /* Some archs do not have data cache coherency between kernel and user-space */ 6351 flush_kernel_vmap_range(cpu_buffer->meta_page, PAGE_SIZE); 6352 } 6353 6354 static void 6355 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) 6356 { 6357 struct buffer_page *page; 6358 6359 if (cpu_buffer->remote) { 6360 if (!cpu_buffer->remote->reset) 6361 return; 6362 6363 cpu_buffer->remote->reset(cpu_buffer->cpu, cpu_buffer->remote->priv); 6364 rb_read_remote_meta_page(cpu_buffer); 6365 6366 /* Read related values, not covered by the meta-page */ 6367 local_set(&cpu_buffer->pages_read, 0); 6368 cpu_buffer->read = 0; 6369 cpu_buffer->read_bytes = 0; 6370 cpu_buffer->last_overrun = 0; 6371 cpu_buffer->reader_page->read = 0; 6372 6373 return; 6374 } 6375 6376 rb_head_page_deactivate(cpu_buffer); 6377 6378 cpu_buffer->head_page 6379 = list_entry(cpu_buffer->pages, struct buffer_page, list); 6380 rb_clear_buffer_page(cpu_buffer->head_page); 6381 list_for_each_entry(page, cpu_buffer->pages, list) { 6382 rb_clear_buffer_page(page); 6383 } 6384 6385 cpu_buffer->tail_page = cpu_buffer->head_page; 6386 cpu_buffer->commit_page = cpu_buffer->head_page; 6387 6388 INIT_LIST_HEAD(&cpu_buffer->reader_page->list); 6389 INIT_LIST_HEAD(&cpu_buffer->new_pages); 6390 rb_clear_buffer_page(cpu_buffer->reader_page); 6391 6392 local_set(&cpu_buffer->entries_bytes, 0); 6393 local_set(&cpu_buffer->overrun, 0); 6394 local_set(&cpu_buffer->commit_overrun, 0); 6395 local_set(&cpu_buffer->dropped_events, 0); 6396 local_set(&cpu_buffer->entries, 0); 6397 local_set(&cpu_buffer->committing, 0); 6398 local_set(&cpu_buffer->commits, 0); 6399 local_set(&cpu_buffer->pages_touched, 0); 6400 local_set(&cpu_buffer->pages_lost, 0); 6401 local_set(&cpu_buffer->pages_read, 0); 6402 cpu_buffer->last_pages_touch = 0; 6403 cpu_buffer->shortest_full = 0; 6404 cpu_buffer->read = 0; 6405 cpu_buffer->read_bytes = 0; 6406 6407 rb_time_set(&cpu_buffer->write_stamp, 0); 6408 rb_time_set(&cpu_buffer->before_stamp, 0); 6409 6410 memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp)); 6411 6412 cpu_buffer->lost_events = 0; 6413 cpu_buffer->last_overrun = 0; 6414 6415 rb_head_page_activate(cpu_buffer); 6416 cpu_buffer->pages_removed = 0; 6417 6418 if (cpu_buffer->mapped) { 6419 rb_update_meta_page(cpu_buffer); 6420 if (cpu_buffer->ring_meta) { 6421 struct ring_buffer_cpu_meta *meta = cpu_buffer->ring_meta; 6422 meta->commit_buffer = meta->head_buffer; 6423 } 6424 } 6425 } 6426 6427 /* Must have disabled the cpu buffer then done a synchronize_rcu */ 6428 static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) 6429 { 6430 guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock); 6431 6432 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) 6433 return; 6434 6435 arch_spin_lock(&cpu_buffer->lock); 6436 6437 rb_reset_cpu(cpu_buffer); 6438 6439 arch_spin_unlock(&cpu_buffer->lock); 6440 } 6441 6442 /** 6443 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer 6444 * @buffer: The ring buffer to reset a per cpu buffer of 6445 * @cpu: The CPU buffer to be reset 6446 */ 6447 void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu) 6448 { 6449 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; 6450 6451 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6452 return; 6453 6454 /* prevent another thread from changing buffer sizes */ 6455 mutex_lock(&buffer->mutex); 6456 6457 atomic_inc(&cpu_buffer->resize_disabled); 6458 atomic_inc(&cpu_buffer->record_disabled); 6459 6460 /* Make sure all commits have finished */ 6461 synchronize_rcu(); 6462 6463 reset_disabled_cpu_buffer(cpu_buffer); 6464 6465 atomic_dec(&cpu_buffer->record_disabled); 6466 atomic_dec(&cpu_buffer->resize_disabled); 6467 6468 mutex_unlock(&buffer->mutex); 6469 } 6470 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); 6471 6472 /* Flag to ensure proper resetting of atomic variables */ 6473 #define RESET_BIT (1 << 30) 6474 6475 /** 6476 * ring_buffer_reset_online_cpus - reset a ring buffer per CPU buffer 6477 * @buffer: The ring buffer to reset a per cpu buffer of 6478 */ 6479 void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) 6480 { 6481 struct ring_buffer_per_cpu *cpu_buffer; 6482 int cpu; 6483 6484 /* prevent another thread from changing buffer sizes */ 6485 mutex_lock(&buffer->mutex); 6486 6487 for_each_online_buffer_cpu(buffer, cpu) { 6488 cpu_buffer = buffer->buffers[cpu]; 6489 6490 atomic_add(RESET_BIT, &cpu_buffer->resize_disabled); 6491 atomic_inc(&cpu_buffer->record_disabled); 6492 } 6493 6494 /* Make sure all commits have finished */ 6495 synchronize_rcu(); 6496 6497 for_each_buffer_cpu(buffer, cpu) { 6498 cpu_buffer = buffer->buffers[cpu]; 6499 6500 /* 6501 * If a CPU came online during the synchronize_rcu(), then 6502 * ignore it. 6503 */ 6504 if (!(atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT)) 6505 continue; 6506 6507 reset_disabled_cpu_buffer(cpu_buffer); 6508 6509 atomic_dec(&cpu_buffer->record_disabled); 6510 atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled); 6511 } 6512 6513 mutex_unlock(&buffer->mutex); 6514 } 6515 6516 /** 6517 * ring_buffer_reset - reset a ring buffer 6518 * @buffer: The ring buffer to reset all cpu buffers 6519 */ 6520 void ring_buffer_reset(struct trace_buffer *buffer) 6521 { 6522 struct ring_buffer_per_cpu *cpu_buffer; 6523 int cpu; 6524 6525 /* prevent another thread from changing buffer sizes */ 6526 mutex_lock(&buffer->mutex); 6527 6528 for_each_buffer_cpu(buffer, cpu) { 6529 cpu_buffer = buffer->buffers[cpu]; 6530 6531 atomic_inc(&cpu_buffer->resize_disabled); 6532 atomic_inc(&cpu_buffer->record_disabled); 6533 } 6534 6535 /* Make sure all commits have finished */ 6536 synchronize_rcu(); 6537 6538 for_each_buffer_cpu(buffer, cpu) { 6539 cpu_buffer = buffer->buffers[cpu]; 6540 6541 reset_disabled_cpu_buffer(cpu_buffer); 6542 6543 atomic_dec(&cpu_buffer->record_disabled); 6544 atomic_dec(&cpu_buffer->resize_disabled); 6545 } 6546 6547 mutex_unlock(&buffer->mutex); 6548 } 6549 EXPORT_SYMBOL_GPL(ring_buffer_reset); 6550 6551 /** 6552 * ring_buffer_empty - is the ring buffer empty? 6553 * @buffer: The ring buffer to test 6554 */ 6555 bool ring_buffer_empty(struct trace_buffer *buffer) 6556 { 6557 struct ring_buffer_per_cpu *cpu_buffer; 6558 unsigned long flags; 6559 bool dolock; 6560 bool ret; 6561 int cpu; 6562 6563 /* yes this is racy, but if you don't like the race, lock the buffer */ 6564 for_each_buffer_cpu(buffer, cpu) { 6565 cpu_buffer = buffer->buffers[cpu]; 6566 local_irq_save(flags); 6567 dolock = rb_reader_lock(cpu_buffer); 6568 ret = rb_per_cpu_empty(cpu_buffer); 6569 rb_reader_unlock(cpu_buffer, dolock); 6570 local_irq_restore(flags); 6571 6572 if (!ret) 6573 return false; 6574 } 6575 6576 return true; 6577 } 6578 EXPORT_SYMBOL_GPL(ring_buffer_empty); 6579 6580 /** 6581 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? 6582 * @buffer: The ring buffer 6583 * @cpu: The CPU buffer to test 6584 */ 6585 bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu) 6586 { 6587 struct ring_buffer_per_cpu *cpu_buffer; 6588 unsigned long flags; 6589 bool dolock; 6590 bool ret; 6591 6592 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6593 return true; 6594 6595 cpu_buffer = buffer->buffers[cpu]; 6596 local_irq_save(flags); 6597 dolock = rb_reader_lock(cpu_buffer); 6598 ret = rb_per_cpu_empty(cpu_buffer); 6599 rb_reader_unlock(cpu_buffer, dolock); 6600 local_irq_restore(flags); 6601 6602 return ret; 6603 } 6604 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); 6605 6606 int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu) 6607 { 6608 struct ring_buffer_per_cpu *cpu_buffer; 6609 6610 if (cpu != RING_BUFFER_ALL_CPUS) { 6611 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6612 return -EINVAL; 6613 6614 cpu_buffer = buffer->buffers[cpu]; 6615 6616 guard(raw_spinlock)(&cpu_buffer->reader_lock); 6617 if (rb_read_remote_meta_page(cpu_buffer)) 6618 rb_wakeups(buffer, cpu_buffer); 6619 6620 return 0; 6621 } 6622 6623 guard(cpus_read_lock)(); 6624 6625 /* 6626 * Make sure all the ring buffers are up to date before we start reading 6627 * them. 6628 */ 6629 for_each_buffer_cpu(buffer, cpu) { 6630 cpu_buffer = buffer->buffers[cpu]; 6631 6632 guard(raw_spinlock)(&cpu_buffer->reader_lock); 6633 rb_read_remote_meta_page(cpu_buffer); 6634 } 6635 6636 for_each_buffer_cpu(buffer, cpu) { 6637 cpu_buffer = buffer->buffers[cpu]; 6638 6639 if (rb_num_of_entries(cpu_buffer)) 6640 rb_wakeups(buffer, cpu_buffer); 6641 } 6642 6643 return 0; 6644 } 6645 6646 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP 6647 /** 6648 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers 6649 * @buffer_a: One buffer to swap with 6650 * @buffer_b: The other buffer to swap with 6651 * @cpu: the CPU of the buffers to swap 6652 * 6653 * This function is useful for tracers that want to take a "snapshot" 6654 * of a CPU buffer and has another back up buffer lying around. 6655 * it is expected that the tracer handles the cpu buffer not being 6656 * used at the moment. 6657 */ 6658 int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, 6659 struct trace_buffer *buffer_b, int cpu) 6660 { 6661 struct ring_buffer_per_cpu *cpu_buffer_a; 6662 struct ring_buffer_per_cpu *cpu_buffer_b; 6663 int ret = -EINVAL; 6664 6665 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || 6666 !cpumask_test_cpu(cpu, buffer_b->cpumask)) 6667 return -EINVAL; 6668 6669 cpu_buffer_a = buffer_a->buffers[cpu]; 6670 cpu_buffer_b = buffer_b->buffers[cpu]; 6671 6672 /* It's up to the callers to not try to swap mapped buffers */ 6673 if (WARN_ON_ONCE(cpu_buffer_a->mapped || cpu_buffer_b->mapped)) 6674 return -EBUSY; 6675 6676 /* At least make sure the two buffers are somewhat the same */ 6677 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) 6678 return -EINVAL; 6679 6680 if (buffer_a->subbuf_order != buffer_b->subbuf_order) 6681 return -EINVAL; 6682 6683 if (atomic_read(&buffer_a->record_disabled)) 6684 return -EAGAIN; 6685 6686 if (atomic_read(&buffer_b->record_disabled)) 6687 return -EAGAIN; 6688 6689 if (atomic_read(&cpu_buffer_a->record_disabled)) 6690 return -EAGAIN; 6691 6692 if (atomic_read(&cpu_buffer_b->record_disabled)) 6693 return -EAGAIN; 6694 6695 /* 6696 * We can't do a synchronize_rcu here because this 6697 * function can be called in atomic context. 6698 * Normally this will be called from the same CPU as cpu. 6699 * If not it's up to the caller to protect this. 6700 */ 6701 atomic_inc(&cpu_buffer_a->record_disabled); 6702 atomic_inc(&cpu_buffer_b->record_disabled); 6703 6704 ret = -EBUSY; 6705 if (local_read(&cpu_buffer_a->committing)) 6706 goto out_dec; 6707 if (local_read(&cpu_buffer_b->committing)) 6708 goto out_dec; 6709 6710 /* 6711 * When resize is in progress, we cannot swap it because 6712 * it will mess the state of the cpu buffer. 6713 */ 6714 if (atomic_read(&buffer_a->resizing)) 6715 goto out_dec; 6716 if (atomic_read(&buffer_b->resizing)) 6717 goto out_dec; 6718 6719 buffer_a->buffers[cpu] = cpu_buffer_b; 6720 buffer_b->buffers[cpu] = cpu_buffer_a; 6721 6722 cpu_buffer_b->buffer = buffer_a; 6723 cpu_buffer_a->buffer = buffer_b; 6724 6725 ret = 0; 6726 6727 out_dec: 6728 atomic_dec(&cpu_buffer_a->record_disabled); 6729 atomic_dec(&cpu_buffer_b->record_disabled); 6730 return ret; 6731 } 6732 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); 6733 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ 6734 6735 /** 6736 * ring_buffer_alloc_read_page - allocate a page to read from buffer 6737 * @buffer: the buffer to allocate for. 6738 * @cpu: the cpu buffer to allocate. 6739 * 6740 * This function is used in conjunction with ring_buffer_read_page. 6741 * When reading a full page from the ring buffer, these functions 6742 * can be used to speed up the process. The calling function should 6743 * allocate a few pages first with this function. Then when it 6744 * needs to get pages from the ring buffer, it passes the result 6745 * of this function into ring_buffer_read_page, which will swap 6746 * the page that was allocated, with the read page of the buffer. 6747 * 6748 * Returns: 6749 * The page allocated, or ERR_PTR 6750 */ 6751 struct buffer_data_read_page * 6752 ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) 6753 { 6754 struct ring_buffer_per_cpu *cpu_buffer; 6755 struct buffer_data_read_page *bpage = NULL; 6756 unsigned long flags; 6757 6758 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6759 return ERR_PTR(-ENODEV); 6760 6761 bpage = kzalloc_obj(*bpage); 6762 if (!bpage) 6763 return ERR_PTR(-ENOMEM); 6764 6765 bpage->order = buffer->subbuf_order; 6766 cpu_buffer = buffer->buffers[cpu]; 6767 local_irq_save(flags); 6768 arch_spin_lock(&cpu_buffer->lock); 6769 6770 if (cpu_buffer->free_page) { 6771 bpage->data = cpu_buffer->free_page; 6772 cpu_buffer->free_page = NULL; 6773 } 6774 6775 arch_spin_unlock(&cpu_buffer->lock); 6776 local_irq_restore(flags); 6777 6778 if (bpage->data) { 6779 rb_init_page(bpage->data); 6780 } else { 6781 bpage->data = alloc_cpu_data(cpu, cpu_buffer->buffer->subbuf_order); 6782 if (!bpage->data) { 6783 kfree(bpage); 6784 return ERR_PTR(-ENOMEM); 6785 } 6786 } 6787 6788 return bpage; 6789 } 6790 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); 6791 6792 /** 6793 * ring_buffer_free_read_page - free an allocated read page 6794 * @buffer: the buffer the page was allocate for 6795 * @cpu: the cpu buffer the page came from 6796 * @data_page: the page to free 6797 * 6798 * Free a page allocated from ring_buffer_alloc_read_page. 6799 */ 6800 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, 6801 struct buffer_data_read_page *data_page) 6802 { 6803 struct ring_buffer_per_cpu *cpu_buffer; 6804 struct buffer_data_page *bpage = data_page->data; 6805 struct page *page = virt_to_page(bpage); 6806 unsigned long flags; 6807 6808 if (!buffer || !buffer->buffers || !buffer->buffers[cpu]) 6809 return; 6810 6811 cpu_buffer = buffer->buffers[cpu]; 6812 6813 /* 6814 * If the page is still in use someplace else, or order of the page 6815 * is different from the subbuffer order of the buffer - 6816 * we can't reuse it 6817 */ 6818 if (page_ref_count(page) > 1 || data_page->order != buffer->subbuf_order) 6819 goto out; 6820 6821 local_irq_save(flags); 6822 arch_spin_lock(&cpu_buffer->lock); 6823 6824 if (!cpu_buffer->free_page) { 6825 cpu_buffer->free_page = bpage; 6826 bpage = NULL; 6827 } 6828 6829 arch_spin_unlock(&cpu_buffer->lock); 6830 local_irq_restore(flags); 6831 6832 out: 6833 free_pages((unsigned long)bpage, data_page->order); 6834 kfree(data_page); 6835 } 6836 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); 6837 6838 /** 6839 * ring_buffer_read_page - extract a page from the ring buffer 6840 * @buffer: buffer to extract from 6841 * @data_page: the page to use allocated from ring_buffer_alloc_read_page 6842 * @len: amount to extract 6843 * @cpu: the cpu of the buffer to extract 6844 * @full: should the extraction only happen when the page is full. 6845 * 6846 * This function will pull out a page from the ring buffer and consume it. 6847 * @data_page must be the address of the variable that was returned 6848 * from ring_buffer_alloc_read_page. This is because the page might be used 6849 * to swap with a page in the ring buffer. 6850 * 6851 * for example: 6852 * rpage = ring_buffer_alloc_read_page(buffer, cpu); 6853 * if (IS_ERR(rpage)) 6854 * return PTR_ERR(rpage); 6855 * ret = ring_buffer_read_page(buffer, rpage, len, cpu, 0); 6856 * if (ret >= 0) 6857 * process_page(ring_buffer_read_page_data(rpage), ret); 6858 * ring_buffer_free_read_page(buffer, cpu, rpage); 6859 * 6860 * When @full is set, the function will not return true unless 6861 * the writer is off the reader page. 6862 * 6863 * Note: it is up to the calling functions to handle sleeps and wakeups. 6864 * The ring buffer can be used anywhere in the kernel and can not 6865 * blindly call wake_up. The layer that uses the ring buffer must be 6866 * responsible for that. 6867 * 6868 * Returns: 6869 * >=0 if data has been transferred, returns the offset of consumed data. 6870 * <0 if no data has been transferred. 6871 */ 6872 int ring_buffer_read_page(struct trace_buffer *buffer, 6873 struct buffer_data_read_page *data_page, 6874 size_t len, int cpu, int full) 6875 { 6876 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; 6877 struct ring_buffer_event *event; 6878 struct buffer_data_page *bpage; 6879 struct buffer_page *reader; 6880 unsigned long missed_events; 6881 unsigned int commit; 6882 unsigned int read; 6883 u64 save_timestamp; 6884 bool force_memcpy; 6885 6886 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 6887 return -1; 6888 6889 /* 6890 * If len is not big enough to hold the page header, then 6891 * we can not copy anything. 6892 */ 6893 if (len <= BUF_PAGE_HDR_SIZE) 6894 return -1; 6895 6896 len -= BUF_PAGE_HDR_SIZE; 6897 6898 if (!data_page || !data_page->data) 6899 return -1; 6900 6901 if (data_page->order != buffer->subbuf_order) 6902 return -1; 6903 6904 bpage = data_page->data; 6905 if (!bpage) 6906 return -1; 6907 6908 guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock); 6909 6910 reader = rb_get_reader_page(cpu_buffer); 6911 if (!reader) 6912 return -1; 6913 6914 event = rb_reader_event(cpu_buffer); 6915 6916 read = reader->read; 6917 commit = rb_page_size(reader); 6918 6919 /* Check if any events were dropped */ 6920 missed_events = cpu_buffer->lost_events; 6921 6922 force_memcpy = cpu_buffer->mapped || cpu_buffer->remote; 6923 6924 /* 6925 * If this page has been partially read or 6926 * if len is not big enough to read the rest of the page or 6927 * a writer is still on the page, then 6928 * we must copy the data from the page to the buffer. 6929 * Otherwise, we can simply swap the page with the one passed in. 6930 */ 6931 if (read || (len < (commit - read)) || 6932 cpu_buffer->reader_page == cpu_buffer->commit_page || 6933 force_memcpy) { 6934 struct buffer_data_page *rpage = cpu_buffer->reader_page->page; 6935 unsigned int rpos = read; 6936 unsigned int pos = 0; 6937 unsigned int size; 6938 6939 /* 6940 * If a full page is expected, this can still be returned 6941 * if there's been a previous partial read and the 6942 * rest of the page can be read and the commit page is off 6943 * the reader page. 6944 */ 6945 if (full && 6946 (!read || (len < (commit - read)) || 6947 cpu_buffer->reader_page == cpu_buffer->commit_page)) 6948 return -1; 6949 6950 if (len > (commit - read)) 6951 len = (commit - read); 6952 6953 /* Always keep the time extend and data together */ 6954 size = rb_event_ts_length(event); 6955 6956 if (len < size) 6957 return -1; 6958 6959 /* save the current timestamp, since the user will need it */ 6960 save_timestamp = cpu_buffer->read_stamp; 6961 6962 /* Need to copy one event at a time */ 6963 do { 6964 /* We need the size of one event, because 6965 * rb_advance_reader only advances by one event, 6966 * whereas rb_event_ts_length may include the size of 6967 * one or two events. 6968 * We have already ensured there's enough space if this 6969 * is a time extend. */ 6970 size = rb_event_length(event); 6971 memcpy(bpage->data + pos, rpage->data + rpos, size); 6972 6973 len -= size; 6974 6975 rb_advance_reader(cpu_buffer); 6976 rpos = reader->read; 6977 pos += size; 6978 6979 if (rpos >= commit) 6980 break; 6981 6982 event = rb_reader_event(cpu_buffer); 6983 /* Always keep the time extend and data together */ 6984 size = rb_event_ts_length(event); 6985 } while (len >= size); 6986 6987 /* update bpage */ 6988 local_set(&bpage->commit, pos); 6989 bpage->time_stamp = save_timestamp; 6990 6991 /* we copied everything to the beginning */ 6992 read = 0; 6993 } else { 6994 /* update the entry counter */ 6995 cpu_buffer->read += rb_page_entries(reader); 6996 cpu_buffer->read_bytes += rb_page_size(reader); 6997 6998 /* swap the pages */ 6999 rb_init_page(bpage); 7000 bpage = reader->page; 7001 reader->page = data_page->data; 7002 local_set(&reader->write, 0); 7003 local_set(&reader->entries, 0); 7004 reader->read = 0; 7005 data_page->data = bpage; 7006 7007 /* 7008 * Use the real_end for the data size, 7009 * This gives us a chance to store the lost events 7010 * on the page. 7011 */ 7012 if (reader->real_end) 7013 local_set(&bpage->commit, reader->real_end); 7014 } 7015 7016 cpu_buffer->lost_events = 0; 7017 7018 commit = local_read(&bpage->commit); 7019 /* 7020 * Set a flag in the commit field if we lost events 7021 */ 7022 if (missed_events) { 7023 /* If there is room at the end of the page to save the 7024 * missed events, then record it there. 7025 */ 7026 if (buffer->subbuf_size - commit >= sizeof(missed_events)) { 7027 memcpy(&bpage->data[commit], &missed_events, 7028 sizeof(missed_events)); 7029 local_add(RB_MISSED_STORED, &bpage->commit); 7030 commit += sizeof(missed_events); 7031 } 7032 local_add(RB_MISSED_EVENTS, &bpage->commit); 7033 } 7034 7035 /* 7036 * This page may be off to user land. Zero it out here. 7037 */ 7038 if (commit < buffer->subbuf_size) 7039 memset(&bpage->data[commit], 0, buffer->subbuf_size - commit); 7040 7041 return read; 7042 } 7043 EXPORT_SYMBOL_GPL(ring_buffer_read_page); 7044 7045 /** 7046 * ring_buffer_read_page_data - get pointer to the data in the page. 7047 * @page: the page to get the data from 7048 * 7049 * Returns pointer to the actual data in this page. 7050 */ 7051 void *ring_buffer_read_page_data(struct buffer_data_read_page *page) 7052 { 7053 return page->data; 7054 } 7055 EXPORT_SYMBOL_GPL(ring_buffer_read_page_data); 7056 7057 /** 7058 * ring_buffer_subbuf_size_get - get size of the sub buffer. 7059 * @buffer: the buffer to get the sub buffer size from 7060 * 7061 * Returns size of the sub buffer, in bytes. 7062 */ 7063 int ring_buffer_subbuf_size_get(struct trace_buffer *buffer) 7064 { 7065 return buffer->subbuf_size + BUF_PAGE_HDR_SIZE; 7066 } 7067 EXPORT_SYMBOL_GPL(ring_buffer_subbuf_size_get); 7068 7069 /** 7070 * ring_buffer_subbuf_order_get - get order of system sub pages in one buffer page. 7071 * @buffer: The ring_buffer to get the system sub page order from 7072 * 7073 * By default, one ring buffer sub page equals to one system page. This parameter 7074 * is configurable, per ring buffer. The size of the ring buffer sub page can be 7075 * extended, but must be an order of system page size. 7076 * 7077 * Returns the order of buffer sub page size, in system pages: 7078 * 0 means the sub buffer size is 1 system page and so forth. 7079 * In case of an error < 0 is returned. 7080 */ 7081 int ring_buffer_subbuf_order_get(struct trace_buffer *buffer) 7082 { 7083 if (!buffer) 7084 return -EINVAL; 7085 7086 return buffer->subbuf_order; 7087 } 7088 EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_get); 7089 7090 /** 7091 * ring_buffer_subbuf_order_set - set the size of ring buffer sub page. 7092 * @buffer: The ring_buffer to set the new page size. 7093 * @order: Order of the system pages in one sub buffer page 7094 * 7095 * By default, one ring buffer pages equals to one system page. This API can be 7096 * used to set new size of the ring buffer page. The size must be order of 7097 * system page size, that's why the input parameter @order is the order of 7098 * system pages that are allocated for one ring buffer page: 7099 * 0 - 1 system page 7100 * 1 - 2 system pages 7101 * 3 - 4 system pages 7102 * ... 7103 * 7104 * Returns 0 on success or < 0 in case of an error. 7105 */ 7106 int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) 7107 { 7108 struct ring_buffer_per_cpu *cpu_buffer; 7109 struct buffer_page *bpage, *tmp; 7110 int old_order, old_size; 7111 int nr_pages; 7112 int psize; 7113 int err; 7114 int cpu; 7115 7116 if (!buffer || order < 0) 7117 return -EINVAL; 7118 7119 if (buffer->subbuf_order == order) 7120 return 0; 7121 7122 psize = (1 << order) * PAGE_SIZE; 7123 if (psize <= BUF_PAGE_HDR_SIZE) 7124 return -EINVAL; 7125 7126 /* Size of a subbuf cannot be greater than the write counter */ 7127 if (psize > RB_WRITE_MASK + 1) 7128 return -EINVAL; 7129 7130 old_order = buffer->subbuf_order; 7131 old_size = buffer->subbuf_size; 7132 7133 /* prevent another thread from changing buffer sizes */ 7134 guard(mutex)(&buffer->mutex); 7135 atomic_inc(&buffer->record_disabled); 7136 7137 /* Make sure all commits have finished */ 7138 synchronize_rcu(); 7139 7140 buffer->subbuf_order = order; 7141 buffer->subbuf_size = psize - BUF_PAGE_HDR_SIZE; 7142 7143 /* Make sure all new buffers are allocated, before deleting the old ones */ 7144 for_each_buffer_cpu(buffer, cpu) { 7145 7146 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 7147 continue; 7148 7149 cpu_buffer = buffer->buffers[cpu]; 7150 7151 if (cpu_buffer->mapped) { 7152 err = -EBUSY; 7153 goto error; 7154 } 7155 7156 /* Update the number of pages to match the new size */ 7157 nr_pages = old_size * buffer->buffers[cpu]->nr_pages; 7158 nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size); 7159 7160 /* we need a minimum of two pages */ 7161 if (nr_pages < 2) 7162 nr_pages = 2; 7163 7164 cpu_buffer->nr_pages_to_update = nr_pages; 7165 7166 /* Include the reader page */ 7167 nr_pages++; 7168 7169 /* Allocate the new size buffer */ 7170 INIT_LIST_HEAD(&cpu_buffer->new_pages); 7171 if (__rb_allocate_pages(cpu_buffer, nr_pages, 7172 &cpu_buffer->new_pages)) { 7173 /* not enough memory for new pages */ 7174 err = -ENOMEM; 7175 goto error; 7176 } 7177 } 7178 7179 for_each_buffer_cpu(buffer, cpu) { 7180 struct buffer_data_page *old_free_data_page; 7181 struct list_head old_pages; 7182 unsigned long flags; 7183 7184 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 7185 continue; 7186 7187 cpu_buffer = buffer->buffers[cpu]; 7188 7189 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7190 7191 /* Clear the head bit to make the link list normal to read */ 7192 rb_head_page_deactivate(cpu_buffer); 7193 7194 /* 7195 * Collect buffers from the cpu_buffer pages list and the 7196 * reader_page on old_pages, so they can be freed later when not 7197 * under a spinlock. The pages list is a linked list with no 7198 * head, adding old_pages turns it into a regular list with 7199 * old_pages being the head. 7200 */ 7201 list_add(&old_pages, cpu_buffer->pages); 7202 list_add(&cpu_buffer->reader_page->list, &old_pages); 7203 7204 /* One page was allocated for the reader page */ 7205 cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next, 7206 struct buffer_page, list); 7207 list_del_init(&cpu_buffer->reader_page->list); 7208 7209 /* Install the new pages, remove the head from the list */ 7210 cpu_buffer->pages = cpu_buffer->new_pages.next; 7211 list_del_init(&cpu_buffer->new_pages); 7212 cpu_buffer->cnt++; 7213 7214 cpu_buffer->head_page 7215 = list_entry(cpu_buffer->pages, struct buffer_page, list); 7216 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; 7217 7218 cpu_buffer->nr_pages = cpu_buffer->nr_pages_to_update; 7219 cpu_buffer->nr_pages_to_update = 0; 7220 7221 old_free_data_page = cpu_buffer->free_page; 7222 cpu_buffer->free_page = NULL; 7223 7224 rb_head_page_activate(cpu_buffer); 7225 7226 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7227 7228 /* Free old sub buffers */ 7229 list_for_each_entry_safe(bpage, tmp, &old_pages, list) { 7230 list_del_init(&bpage->list); 7231 free_buffer_page(bpage); 7232 } 7233 free_pages((unsigned long)old_free_data_page, old_order); 7234 7235 rb_check_pages(cpu_buffer); 7236 } 7237 7238 atomic_dec(&buffer->record_disabled); 7239 7240 return 0; 7241 7242 error: 7243 buffer->subbuf_order = old_order; 7244 buffer->subbuf_size = old_size; 7245 7246 atomic_dec(&buffer->record_disabled); 7247 7248 for_each_buffer_cpu(buffer, cpu) { 7249 cpu_buffer = buffer->buffers[cpu]; 7250 7251 if (!cpu_buffer->nr_pages_to_update) 7252 continue; 7253 7254 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, list) { 7255 list_del_init(&bpage->list); 7256 free_buffer_page(bpage); 7257 } 7258 } 7259 7260 return err; 7261 } 7262 EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set); 7263 7264 static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer) 7265 { 7266 struct page *page; 7267 7268 if (cpu_buffer->meta_page) 7269 return 0; 7270 7271 page = alloc_page(GFP_USER | __GFP_ZERO); 7272 if (!page) 7273 return -ENOMEM; 7274 7275 cpu_buffer->meta_page = page_to_virt(page); 7276 7277 return 0; 7278 } 7279 7280 static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer) 7281 { 7282 unsigned long addr = (unsigned long)cpu_buffer->meta_page; 7283 7284 free_page(addr); 7285 cpu_buffer->meta_page = NULL; 7286 } 7287 7288 static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer, 7289 struct buffer_page **subbuf_ids) 7290 { 7291 struct trace_buffer_meta *meta = cpu_buffer->meta_page; 7292 unsigned int nr_subbufs = cpu_buffer->nr_pages + 1; 7293 struct buffer_page *first_subbuf, *subbuf; 7294 int cnt = 0; 7295 int id = 0; 7296 7297 id = rb_page_id(cpu_buffer, cpu_buffer->reader_page, id); 7298 subbuf_ids[id++] = cpu_buffer->reader_page; 7299 cnt++; 7300 7301 first_subbuf = subbuf = rb_set_head_page(cpu_buffer); 7302 do { 7303 id = rb_page_id(cpu_buffer, subbuf, id); 7304 7305 if (WARN_ON(id >= nr_subbufs)) 7306 break; 7307 7308 subbuf_ids[id] = subbuf; 7309 7310 rb_inc_page(&subbuf); 7311 id++; 7312 cnt++; 7313 } while (subbuf != first_subbuf); 7314 7315 WARN_ON(cnt != nr_subbufs); 7316 7317 /* install subbuf ID to bpage translation */ 7318 cpu_buffer->subbuf_ids = subbuf_ids; 7319 7320 meta->meta_struct_len = sizeof(*meta); 7321 meta->nr_subbufs = nr_subbufs; 7322 meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE; 7323 meta->meta_page_size = meta->subbuf_size; 7324 7325 rb_update_meta_page(cpu_buffer); 7326 } 7327 7328 static struct ring_buffer_per_cpu * 7329 rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu) 7330 { 7331 struct ring_buffer_per_cpu *cpu_buffer; 7332 7333 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 7334 return ERR_PTR(-EINVAL); 7335 7336 cpu_buffer = buffer->buffers[cpu]; 7337 7338 mutex_lock(&cpu_buffer->mapping_lock); 7339 7340 if (!cpu_buffer->user_mapped) { 7341 mutex_unlock(&cpu_buffer->mapping_lock); 7342 return ERR_PTR(-ENODEV); 7343 } 7344 7345 return cpu_buffer; 7346 } 7347 7348 static void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer) 7349 { 7350 mutex_unlock(&cpu_buffer->mapping_lock); 7351 } 7352 7353 /* 7354 * Fast-path for rb_buffer_(un)map(). Called whenever the meta-page doesn't need 7355 * to be set-up or torn-down. 7356 */ 7357 static int __rb_inc_dec_mapped(struct ring_buffer_per_cpu *cpu_buffer, 7358 bool inc) 7359 { 7360 unsigned long flags; 7361 7362 lockdep_assert_held(&cpu_buffer->mapping_lock); 7363 7364 /* mapped is always greater or equal to user_mapped */ 7365 if (WARN_ON(cpu_buffer->mapped < cpu_buffer->user_mapped)) 7366 return -EINVAL; 7367 7368 if (inc && cpu_buffer->mapped == UINT_MAX) 7369 return -EBUSY; 7370 7371 if (WARN_ON(!inc && cpu_buffer->user_mapped == 0)) 7372 return -EINVAL; 7373 7374 mutex_lock(&cpu_buffer->buffer->mutex); 7375 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7376 7377 if (inc) { 7378 cpu_buffer->user_mapped++; 7379 cpu_buffer->mapped++; 7380 } else { 7381 cpu_buffer->user_mapped--; 7382 cpu_buffer->mapped--; 7383 } 7384 7385 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7386 mutex_unlock(&cpu_buffer->buffer->mutex); 7387 7388 return 0; 7389 } 7390 7391 /* 7392 * +--------------+ pgoff == 0 7393 * | meta page | 7394 * +--------------+ pgoff == 1 7395 * | subbuffer 0 | 7396 * | | 7397 * +--------------+ pgoff == (1 + (1 << subbuf_order)) 7398 * | subbuffer 1 | 7399 * | | 7400 * ... 7401 */ 7402 #ifdef CONFIG_MMU 7403 static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer, 7404 struct vm_area_struct *vma) 7405 { 7406 unsigned long nr_subbufs, nr_pages, nr_vma_pages, pgoff = vma->vm_pgoff; 7407 unsigned int subbuf_pages, subbuf_order; 7408 struct page **pages __free(kfree) = NULL; 7409 int p = 0, s = 0; 7410 int err; 7411 7412 /* Refuse MP_PRIVATE or writable mappings */ 7413 if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC || 7414 !(vma->vm_flags & VM_MAYSHARE)) 7415 return -EPERM; 7416 7417 subbuf_order = cpu_buffer->buffer->subbuf_order; 7418 subbuf_pages = 1 << subbuf_order; 7419 7420 if (subbuf_order && pgoff % subbuf_pages) 7421 return -EINVAL; 7422 7423 /* 7424 * Make sure the mapping cannot become writable later. Also tell the VM 7425 * to not touch these pages (VM_DONTCOPY | VM_DONTEXPAND). 7426 */ 7427 vm_flags_mod(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP, 7428 VM_MAYWRITE); 7429 7430 lockdep_assert_held(&cpu_buffer->mapping_lock); 7431 7432 nr_subbufs = cpu_buffer->nr_pages + 1; /* + reader-subbuf */ 7433 nr_pages = ((nr_subbufs + 1) << subbuf_order); /* + meta-page */ 7434 if (nr_pages <= pgoff) 7435 return -EINVAL; 7436 7437 nr_pages -= pgoff; 7438 7439 nr_vma_pages = vma_pages(vma); 7440 if (!nr_vma_pages || nr_vma_pages > nr_pages) 7441 return -EINVAL; 7442 7443 nr_pages = nr_vma_pages; 7444 7445 pages = kzalloc_objs(*pages, nr_pages); 7446 if (!pages) 7447 return -ENOMEM; 7448 7449 if (!pgoff) { 7450 unsigned long meta_page_padding; 7451 7452 pages[p++] = virt_to_page(cpu_buffer->meta_page); 7453 7454 /* 7455 * Pad with the zero-page to align the meta-page with the 7456 * sub-buffers. 7457 */ 7458 meta_page_padding = subbuf_pages - 1; 7459 while (meta_page_padding-- && p < nr_pages) { 7460 unsigned long __maybe_unused zero_addr = 7461 vma->vm_start + (PAGE_SIZE * p); 7462 7463 pages[p++] = ZERO_PAGE(zero_addr); 7464 } 7465 } else { 7466 /* Skip the meta-page */ 7467 pgoff -= subbuf_pages; 7468 7469 s += pgoff / subbuf_pages; 7470 } 7471 7472 while (p < nr_pages) { 7473 struct buffer_page *subbuf; 7474 struct page *page; 7475 int off = 0; 7476 7477 if (WARN_ON_ONCE(s >= nr_subbufs)) 7478 return -EINVAL; 7479 7480 subbuf = cpu_buffer->subbuf_ids[s]; 7481 page = virt_to_page((void *)subbuf->page); 7482 7483 for (; off < (1 << (subbuf_order)); off++, page++) { 7484 if (p >= nr_pages) 7485 break; 7486 7487 pages[p++] = page; 7488 } 7489 s++; 7490 } 7491 7492 err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages); 7493 7494 return err; 7495 } 7496 #else 7497 static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer, 7498 struct vm_area_struct *vma) 7499 { 7500 return -EOPNOTSUPP; 7501 } 7502 #endif 7503 7504 int ring_buffer_map(struct trace_buffer *buffer, int cpu, 7505 struct vm_area_struct *vma) 7506 { 7507 struct ring_buffer_per_cpu *cpu_buffer; 7508 struct buffer_page **subbuf_ids; 7509 unsigned long flags; 7510 int err; 7511 7512 if (!cpumask_test_cpu(cpu, buffer->cpumask) || buffer->remote) 7513 return -EINVAL; 7514 7515 cpu_buffer = buffer->buffers[cpu]; 7516 7517 guard(mutex)(&cpu_buffer->mapping_lock); 7518 7519 if (cpu_buffer->user_mapped) { 7520 err = __rb_map_vma(cpu_buffer, vma); 7521 if (!err) 7522 err = __rb_inc_dec_mapped(cpu_buffer, true); 7523 return err; 7524 } 7525 7526 /* prevent another thread from changing buffer/sub-buffer sizes */ 7527 guard(mutex)(&buffer->mutex); 7528 7529 err = rb_alloc_meta_page(cpu_buffer); 7530 if (err) 7531 return err; 7532 7533 /* subbuf_ids includes the reader while nr_pages does not */ 7534 subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1, sizeof(*subbuf_ids), GFP_KERNEL); 7535 if (!subbuf_ids) { 7536 rb_free_meta_page(cpu_buffer); 7537 return -ENOMEM; 7538 } 7539 7540 atomic_inc(&cpu_buffer->resize_disabled); 7541 7542 /* 7543 * Lock all readers to block any subbuf swap until the subbuf IDs are 7544 * assigned. 7545 */ 7546 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7547 rb_setup_ids_meta_page(cpu_buffer, subbuf_ids); 7548 7549 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7550 7551 err = __rb_map_vma(cpu_buffer, vma); 7552 if (!err) { 7553 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7554 /* This is the first time it is mapped by user */ 7555 cpu_buffer->mapped++; 7556 cpu_buffer->user_mapped = 1; 7557 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7558 } else { 7559 kfree(cpu_buffer->subbuf_ids); 7560 cpu_buffer->subbuf_ids = NULL; 7561 rb_free_meta_page(cpu_buffer); 7562 atomic_dec(&cpu_buffer->resize_disabled); 7563 } 7564 7565 return err; 7566 } 7567 7568 /* 7569 * This is called when a VMA is duplicated (e.g., on fork()) to increment 7570 * the user_mapped counter without remapping pages. 7571 */ 7572 void ring_buffer_map_dup(struct trace_buffer *buffer, int cpu) 7573 { 7574 struct ring_buffer_per_cpu *cpu_buffer; 7575 7576 if (WARN_ON(!cpumask_test_cpu(cpu, buffer->cpumask))) 7577 return; 7578 7579 cpu_buffer = buffer->buffers[cpu]; 7580 7581 guard(mutex)(&cpu_buffer->mapping_lock); 7582 7583 if (cpu_buffer->user_mapped) 7584 __rb_inc_dec_mapped(cpu_buffer, true); 7585 else 7586 WARN(1, "Unexpected buffer stat, it should be mapped"); 7587 } 7588 7589 int ring_buffer_unmap(struct trace_buffer *buffer, int cpu) 7590 { 7591 struct ring_buffer_per_cpu *cpu_buffer; 7592 unsigned long flags; 7593 7594 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 7595 return -EINVAL; 7596 7597 cpu_buffer = buffer->buffers[cpu]; 7598 7599 guard(mutex)(&cpu_buffer->mapping_lock); 7600 7601 if (!cpu_buffer->user_mapped) { 7602 return -ENODEV; 7603 } else if (cpu_buffer->user_mapped > 1) { 7604 __rb_inc_dec_mapped(cpu_buffer, false); 7605 return 0; 7606 } 7607 7608 guard(mutex)(&buffer->mutex); 7609 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7610 7611 /* This is the last user space mapping */ 7612 if (!WARN_ON_ONCE(cpu_buffer->mapped < cpu_buffer->user_mapped)) 7613 cpu_buffer->mapped--; 7614 cpu_buffer->user_mapped = 0; 7615 7616 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7617 7618 kfree(cpu_buffer->subbuf_ids); 7619 cpu_buffer->subbuf_ids = NULL; 7620 rb_free_meta_page(cpu_buffer); 7621 atomic_dec(&cpu_buffer->resize_disabled); 7622 7623 return 0; 7624 } 7625 7626 int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu) 7627 { 7628 struct ring_buffer_per_cpu *cpu_buffer; 7629 struct buffer_page *reader; 7630 unsigned long missed_events; 7631 unsigned long reader_size; 7632 unsigned long flags; 7633 7634 cpu_buffer = rb_get_mapped_buffer(buffer, cpu); 7635 if (IS_ERR(cpu_buffer)) 7636 return (int)PTR_ERR(cpu_buffer); 7637 7638 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 7639 7640 consume: 7641 if (rb_per_cpu_empty(cpu_buffer)) 7642 goto out; 7643 7644 reader_size = rb_page_size(cpu_buffer->reader_page); 7645 7646 /* 7647 * There are data to be read on the current reader page, we can 7648 * return to the caller. But before that, we assume the latter will read 7649 * everything. Let's update the kernel reader accordingly. 7650 */ 7651 if (cpu_buffer->reader_page->read < reader_size) { 7652 while (cpu_buffer->reader_page->read < reader_size) 7653 rb_advance_reader(cpu_buffer); 7654 goto out; 7655 } 7656 7657 /* Did the reader catch up with the writer? */ 7658 if (cpu_buffer->reader_page == cpu_buffer->commit_page) 7659 goto out; 7660 7661 reader = rb_get_reader_page(cpu_buffer); 7662 if (WARN_ON(!reader)) 7663 goto out; 7664 7665 /* Check if any events were dropped */ 7666 missed_events = cpu_buffer->lost_events; 7667 7668 if (missed_events) { 7669 if (cpu_buffer->reader_page != cpu_buffer->commit_page) { 7670 struct buffer_data_page *bpage = reader->page; 7671 unsigned int commit; 7672 /* 7673 * Use the real_end for the data size, 7674 * This gives us a chance to store the lost events 7675 * on the page. 7676 */ 7677 if (reader->real_end) 7678 local_set(&bpage->commit, reader->real_end); 7679 /* 7680 * If there is room at the end of the page to save the 7681 * missed events, then record it there. 7682 */ 7683 commit = rb_page_size(reader); 7684 if (buffer->subbuf_size - commit >= sizeof(missed_events)) { 7685 memcpy(&bpage->data[commit], &missed_events, 7686 sizeof(missed_events)); 7687 local_add(RB_MISSED_STORED, &bpage->commit); 7688 } 7689 local_add(RB_MISSED_EVENTS, &bpage->commit); 7690 } else if (!WARN_ONCE(cpu_buffer->reader_page == cpu_buffer->tail_page, 7691 "Reader on commit with %ld missed events", 7692 missed_events)) { 7693 /* 7694 * There shouldn't be any missed events if the tail_page 7695 * is on the reader page. But if the tail page is not on the 7696 * reader page and the commit_page is, that would mean that 7697 * there's a commit_overrun (an interrupt preempted an 7698 * addition of an event and then filled the buffer 7699 * with new events). In this case it's not an 7700 * error, but it should still be reported. 7701 * 7702 * TODO: Add missed events to the page for user space to know. 7703 */ 7704 pr_info("Ring buffer [%d] commit overrun lost %ld events at timestamp:%lld\n", 7705 cpu, missed_events, cpu_buffer->reader_page->page->time_stamp); 7706 } 7707 } 7708 7709 cpu_buffer->lost_events = 0; 7710 7711 goto consume; 7712 7713 out: 7714 /* Some archs do not have data cache coherency between kernel and user-space */ 7715 flush_kernel_vmap_range(cpu_buffer->reader_page->page, 7716 buffer->subbuf_size + BUF_PAGE_HDR_SIZE); 7717 7718 rb_update_meta_page(cpu_buffer); 7719 7720 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 7721 rb_put_mapped_buffer(cpu_buffer); 7722 7723 return 0; 7724 } 7725 7726 static void rb_cpu_sync(void *data) 7727 { 7728 /* Not really needed, but documents what is happening */ 7729 smp_rmb(); 7730 } 7731 7732 /* 7733 * We only allocate new buffers, never free them if the CPU goes down. 7734 * If we were to free the buffer, then the user would lose any trace that was in 7735 * the buffer. 7736 */ 7737 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) 7738 { 7739 struct trace_buffer *buffer; 7740 long nr_pages_same; 7741 int cpu_i; 7742 unsigned long nr_pages; 7743 7744 buffer = container_of(node, struct trace_buffer, node); 7745 if (cpumask_test_cpu(cpu, buffer->cpumask)) 7746 return 0; 7747 7748 nr_pages = 0; 7749 nr_pages_same = 1; 7750 /* check if all cpu sizes are same */ 7751 for_each_buffer_cpu(buffer, cpu_i) { 7752 /* fill in the size from first enabled cpu */ 7753 if (nr_pages == 0) 7754 nr_pages = buffer->buffers[cpu_i]->nr_pages; 7755 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) { 7756 nr_pages_same = 0; 7757 break; 7758 } 7759 } 7760 /* allocate minimum pages, user can later expand it */ 7761 if (!nr_pages_same) 7762 nr_pages = 2; 7763 buffer->buffers[cpu] = 7764 rb_allocate_cpu_buffer(buffer, nr_pages, cpu); 7765 if (!buffer->buffers[cpu]) { 7766 WARN(1, "failed to allocate ring buffer on CPU %u\n", 7767 cpu); 7768 return -ENOMEM; 7769 } 7770 7771 /* 7772 * Ensure trace_buffer readers observe the newly allocated 7773 * ring_buffer_per_cpu before they check the cpumask. Instead of using a 7774 * read barrier for all readers, send an IPI. 7775 */ 7776 if (unlikely(system_state == SYSTEM_RUNNING)) { 7777 on_each_cpu(rb_cpu_sync, NULL, 1); 7778 /* Not really needed, but documents what is happening */ 7779 smp_wmb(); 7780 } 7781 7782 cpumask_set_cpu(cpu, buffer->cpumask); 7783 return 0; 7784 } 7785 7786 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST 7787 /* 7788 * This is a basic integrity check of the ring buffer. 7789 * Late in the boot cycle this test will run when configured in. 7790 * It will kick off a thread per CPU that will go into a loop 7791 * writing to the per cpu ring buffer various sizes of data. 7792 * Some of the data will be large items, some small. 7793 * 7794 * Another thread is created that goes into a spin, sending out 7795 * IPIs to the other CPUs to also write into the ring buffer. 7796 * this is to test the nesting ability of the buffer. 7797 * 7798 * Basic stats are recorded and reported. If something in the 7799 * ring buffer should happen that's not expected, a big warning 7800 * is displayed and all ring buffers are disabled. 7801 */ 7802 static struct task_struct *rb_threads[NR_CPUS] __initdata; 7803 7804 struct rb_test_data { 7805 struct trace_buffer *buffer; 7806 unsigned long events; 7807 unsigned long bytes_written; 7808 unsigned long bytes_alloc; 7809 unsigned long bytes_dropped; 7810 unsigned long events_nested; 7811 unsigned long bytes_written_nested; 7812 unsigned long bytes_alloc_nested; 7813 unsigned long bytes_dropped_nested; 7814 int min_size_nested; 7815 int max_size_nested; 7816 int max_size; 7817 int min_size; 7818 int cpu; 7819 int cnt; 7820 }; 7821 7822 static struct rb_test_data rb_data[NR_CPUS] __initdata; 7823 7824 /* 1 meg per cpu */ 7825 #define RB_TEST_BUFFER_SIZE 1048576 7826 7827 static char rb_string[] __initdata = 7828 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\" 7829 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890" 7830 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv"; 7831 7832 static bool rb_test_started __initdata; 7833 7834 struct rb_item { 7835 int size; 7836 char str[]; 7837 }; 7838 7839 static __init int rb_write_something(struct rb_test_data *data, bool nested) 7840 { 7841 struct ring_buffer_event *event; 7842 struct rb_item *item; 7843 bool started; 7844 int event_len; 7845 int size; 7846 int len; 7847 int cnt; 7848 7849 /* Have nested writes different that what is written */ 7850 cnt = data->cnt + (nested ? 27 : 0); 7851 7852 /* Multiply cnt by ~e, to make some unique increment */ 7853 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1); 7854 7855 len = size + sizeof(struct rb_item); 7856 7857 started = rb_test_started; 7858 /* read rb_test_started before checking buffer enabled */ 7859 smp_rmb(); 7860 7861 event = ring_buffer_lock_reserve(data->buffer, len); 7862 if (!event) { 7863 /* Ignore dropped events before test starts. */ 7864 if (started) { 7865 if (nested) 7866 data->bytes_dropped_nested += len; 7867 else 7868 data->bytes_dropped += len; 7869 } 7870 return len; 7871 } 7872 7873 event_len = ring_buffer_event_length(event); 7874 7875 if (RB_WARN_ON(data->buffer, event_len < len)) 7876 goto out; 7877 7878 item = ring_buffer_event_data(event); 7879 item->size = size; 7880 memcpy(item->str, rb_string, size); 7881 7882 if (nested) { 7883 data->bytes_alloc_nested += event_len; 7884 data->bytes_written_nested += len; 7885 data->events_nested++; 7886 if (!data->min_size_nested || len < data->min_size_nested) 7887 data->min_size_nested = len; 7888 if (len > data->max_size_nested) 7889 data->max_size_nested = len; 7890 } else { 7891 data->bytes_alloc += event_len; 7892 data->bytes_written += len; 7893 data->events++; 7894 if (!data->min_size || len < data->min_size) 7895 data->max_size = len; 7896 if (len > data->max_size) 7897 data->max_size = len; 7898 } 7899 7900 out: 7901 ring_buffer_unlock_commit(data->buffer); 7902 7903 return 0; 7904 } 7905 7906 static __init int rb_test(void *arg) 7907 { 7908 struct rb_test_data *data = arg; 7909 7910 while (!kthread_should_stop()) { 7911 rb_write_something(data, false); 7912 data->cnt++; 7913 7914 set_current_state(TASK_INTERRUPTIBLE); 7915 /* Now sleep between a min of 100-300us and a max of 1ms */ 7916 usleep_range(((data->cnt % 3) + 1) * 100, 1000); 7917 } 7918 7919 return 0; 7920 } 7921 7922 static __init void rb_ipi(void *ignore) 7923 { 7924 struct rb_test_data *data; 7925 int cpu = smp_processor_id(); 7926 7927 data = &rb_data[cpu]; 7928 rb_write_something(data, true); 7929 } 7930 7931 static __init int rb_hammer_test(void *arg) 7932 { 7933 while (!kthread_should_stop()) { 7934 7935 /* Send an IPI to all cpus to write data! */ 7936 smp_call_function(rb_ipi, NULL, 1); 7937 /* No sleep, but for non preempt, let others run */ 7938 schedule(); 7939 } 7940 7941 return 0; 7942 } 7943 7944 static __init int test_ringbuffer(void) 7945 { 7946 struct task_struct *rb_hammer; 7947 struct trace_buffer *buffer; 7948 int cpu; 7949 int ret = 0; 7950 7951 if (security_locked_down(LOCKDOWN_TRACEFS)) { 7952 pr_warn("Lockdown is enabled, skipping ring buffer tests\n"); 7953 return 0; 7954 } 7955 7956 pr_info("Running ring buffer tests...\n"); 7957 7958 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE); 7959 if (WARN_ON(!buffer)) 7960 return 0; 7961 7962 /* Disable buffer so that threads can't write to it yet */ 7963 ring_buffer_record_off(buffer); 7964 7965 for_each_online_cpu(cpu) { 7966 rb_data[cpu].buffer = buffer; 7967 rb_data[cpu].cpu = cpu; 7968 rb_data[cpu].cnt = cpu; 7969 rb_threads[cpu] = kthread_run_on_cpu(rb_test, &rb_data[cpu], 7970 cpu, "rbtester/%u"); 7971 if (WARN_ON(IS_ERR(rb_threads[cpu]))) { 7972 pr_cont("FAILED\n"); 7973 ret = PTR_ERR(rb_threads[cpu]); 7974 goto out_free; 7975 } 7976 } 7977 7978 /* Now create the rb hammer! */ 7979 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer"); 7980 if (WARN_ON(IS_ERR(rb_hammer))) { 7981 pr_cont("FAILED\n"); 7982 ret = PTR_ERR(rb_hammer); 7983 goto out_free; 7984 } 7985 7986 ring_buffer_record_on(buffer); 7987 /* 7988 * Show buffer is enabled before setting rb_test_started. 7989 * Yes there's a small race window where events could be 7990 * dropped and the thread won't catch it. But when a ring 7991 * buffer gets enabled, there will always be some kind of 7992 * delay before other CPUs see it. Thus, we don't care about 7993 * those dropped events. We care about events dropped after 7994 * the threads see that the buffer is active. 7995 */ 7996 smp_wmb(); 7997 rb_test_started = true; 7998 7999 set_current_state(TASK_INTERRUPTIBLE); 8000 /* Just run for 10 seconds */ 8001 schedule_timeout(10 * HZ); 8002 8003 kthread_stop(rb_hammer); 8004 8005 out_free: 8006 for_each_online_cpu(cpu) { 8007 if (!rb_threads[cpu]) 8008 break; 8009 kthread_stop(rb_threads[cpu]); 8010 } 8011 if (ret) { 8012 ring_buffer_free(buffer); 8013 return ret; 8014 } 8015 8016 /* Report! */ 8017 pr_info("finished\n"); 8018 for_each_online_cpu(cpu) { 8019 struct ring_buffer_event *event; 8020 struct rb_test_data *data = &rb_data[cpu]; 8021 struct rb_item *item; 8022 unsigned long total_events; 8023 unsigned long total_dropped; 8024 unsigned long total_written; 8025 unsigned long total_alloc; 8026 unsigned long total_read = 0; 8027 unsigned long total_size = 0; 8028 unsigned long total_len = 0; 8029 unsigned long total_lost = 0; 8030 unsigned long lost; 8031 int big_event_size; 8032 int small_event_size; 8033 8034 ret = -1; 8035 8036 total_events = data->events + data->events_nested; 8037 total_written = data->bytes_written + data->bytes_written_nested; 8038 total_alloc = data->bytes_alloc + data->bytes_alloc_nested; 8039 total_dropped = data->bytes_dropped + data->bytes_dropped_nested; 8040 8041 big_event_size = data->max_size + data->max_size_nested; 8042 small_event_size = data->min_size + data->min_size_nested; 8043 8044 pr_info("CPU %d:\n", cpu); 8045 pr_info(" events: %ld\n", total_events); 8046 pr_info(" dropped bytes: %ld\n", total_dropped); 8047 pr_info(" alloced bytes: %ld\n", total_alloc); 8048 pr_info(" written bytes: %ld\n", total_written); 8049 pr_info(" biggest event: %d\n", big_event_size); 8050 pr_info(" smallest event: %d\n", small_event_size); 8051 8052 if (RB_WARN_ON(buffer, total_dropped)) 8053 break; 8054 8055 ret = 0; 8056 8057 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) { 8058 total_lost += lost; 8059 item = ring_buffer_event_data(event); 8060 total_len += ring_buffer_event_length(event); 8061 total_size += item->size + sizeof(struct rb_item); 8062 if (memcmp(&item->str[0], rb_string, item->size) != 0) { 8063 pr_info("FAILED!\n"); 8064 pr_info("buffer had: %.*s\n", item->size, item->str); 8065 pr_info("expected: %.*s\n", item->size, rb_string); 8066 RB_WARN_ON(buffer, 1); 8067 ret = -1; 8068 break; 8069 } 8070 total_read++; 8071 } 8072 if (ret) 8073 break; 8074 8075 ret = -1; 8076 8077 pr_info(" read events: %ld\n", total_read); 8078 pr_info(" lost events: %ld\n", total_lost); 8079 pr_info(" total events: %ld\n", total_lost + total_read); 8080 pr_info(" recorded len bytes: %ld\n", total_len); 8081 pr_info(" recorded size bytes: %ld\n", total_size); 8082 if (total_lost) { 8083 pr_info(" With dropped events, record len and size may not match\n" 8084 " alloced and written from above\n"); 8085 } else { 8086 if (RB_WARN_ON(buffer, total_len != total_alloc || 8087 total_size != total_written)) 8088 break; 8089 } 8090 if (RB_WARN_ON(buffer, total_lost + total_read != total_events)) 8091 break; 8092 8093 ret = 0; 8094 } 8095 if (!ret) 8096 pr_info("Ring buffer PASSED!\n"); 8097 8098 ring_buffer_free(buffer); 8099 return 0; 8100 } 8101 8102 late_initcall(test_ringbuffer); 8103 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */ 8104