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