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