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