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