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