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