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