diff options
Diffstat (limited to 'kernel/trace/ring_buffer.c')
-rw-r--r-- | kernel/trace/ring_buffer.c | 172 |
1 files changed, 110 insertions, 62 deletions
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index da2c59d8f486..454e74e718cf 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c | |||
@@ -218,17 +218,12 @@ enum { | |||
218 | 218 | ||
219 | static inline int rb_null_event(struct ring_buffer_event *event) | 219 | static inline int rb_null_event(struct ring_buffer_event *event) |
220 | { | 220 | { |
221 | return event->type_len == RINGBUF_TYPE_PADDING | 221 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
222 | && event->time_delta == 0; | ||
223 | } | ||
224 | |||
225 | static inline int rb_discarded_event(struct ring_buffer_event *event) | ||
226 | { | ||
227 | return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta; | ||
228 | } | 222 | } |
229 | 223 | ||
230 | static void rb_event_set_padding(struct ring_buffer_event *event) | 224 | static void rb_event_set_padding(struct ring_buffer_event *event) |
231 | { | 225 | { |
226 | /* padding has a NULL time_delta */ | ||
232 | event->type_len = RINGBUF_TYPE_PADDING; | 227 | event->type_len = RINGBUF_TYPE_PADDING; |
233 | event->time_delta = 0; | 228 | event->time_delta = 0; |
234 | } | 229 | } |
@@ -472,14 +467,19 @@ struct ring_buffer_iter { | |||
472 | }; | 467 | }; |
473 | 468 | ||
474 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ | 469 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
475 | #define RB_WARN_ON(buffer, cond) \ | 470 | #define RB_WARN_ON(b, cond) \ |
476 | ({ \ | 471 | ({ \ |
477 | int _____ret = unlikely(cond); \ | 472 | int _____ret = unlikely(cond); \ |
478 | if (_____ret) { \ | 473 | if (_____ret) { \ |
479 | atomic_inc(&buffer->record_disabled); \ | 474 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ |
480 | WARN_ON(1); \ | 475 | struct ring_buffer_per_cpu *__b = \ |
481 | } \ | 476 | (void *)b; \ |
482 | _____ret; \ | 477 | atomic_inc(&__b->buffer->record_disabled); \ |
478 | } else \ | ||
479 | atomic_inc(&b->record_disabled); \ | ||
480 | WARN_ON(1); \ | ||
481 | } \ | ||
482 | _____ret; \ | ||
483 | }) | 483 | }) |
484 | 484 | ||
485 | /* Up this if you want to test the TIME_EXTENTS and normalization */ | 485 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
@@ -1778,9 +1778,6 @@ rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, | |||
1778 | event->type_len = RINGBUF_TYPE_PADDING; | 1778 | event->type_len = RINGBUF_TYPE_PADDING; |
1779 | /* time delta must be non zero */ | 1779 | /* time delta must be non zero */ |
1780 | event->time_delta = 1; | 1780 | event->time_delta = 1; |
1781 | /* Account for this as an entry */ | ||
1782 | local_inc(&tail_page->entries); | ||
1783 | local_inc(&cpu_buffer->entries); | ||
1784 | 1781 | ||
1785 | /* Set write to end of buffer */ | 1782 | /* Set write to end of buffer */ |
1786 | length = (tail + length) - BUF_PAGE_SIZE; | 1783 | length = (tail + length) - BUF_PAGE_SIZE; |
@@ -2076,7 +2073,8 @@ static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) | |||
2076 | } | 2073 | } |
2077 | 2074 | ||
2078 | static struct ring_buffer_event * | 2075 | static struct ring_buffer_event * |
2079 | rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer, | 2076 | rb_reserve_next_event(struct ring_buffer *buffer, |
2077 | struct ring_buffer_per_cpu *cpu_buffer, | ||
2080 | unsigned long length) | 2078 | unsigned long length) |
2081 | { | 2079 | { |
2082 | struct ring_buffer_event *event; | 2080 | struct ring_buffer_event *event; |
@@ -2086,6 +2084,21 @@ rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer, | |||
2086 | 2084 | ||
2087 | rb_start_commit(cpu_buffer); | 2085 | rb_start_commit(cpu_buffer); |
2088 | 2086 | ||
2087 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP | ||
2088 | /* | ||
2089 | * Due to the ability to swap a cpu buffer from a buffer | ||
2090 | * it is possible it was swapped before we committed. | ||
2091 | * (committing stops a swap). We check for it here and | ||
2092 | * if it happened, we have to fail the write. | ||
2093 | */ | ||
2094 | barrier(); | ||
2095 | if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) { | ||
2096 | local_dec(&cpu_buffer->committing); | ||
2097 | local_dec(&cpu_buffer->commits); | ||
2098 | return NULL; | ||
2099 | } | ||
2100 | #endif | ||
2101 | |||
2089 | length = rb_calculate_event_length(length); | 2102 | length = rb_calculate_event_length(length); |
2090 | again: | 2103 | again: |
2091 | /* | 2104 | /* |
@@ -2246,7 +2259,7 @@ ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) | |||
2246 | if (length > BUF_MAX_DATA_SIZE) | 2259 | if (length > BUF_MAX_DATA_SIZE) |
2247 | goto out; | 2260 | goto out; |
2248 | 2261 | ||
2249 | event = rb_reserve_next_event(cpu_buffer, length); | 2262 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
2250 | if (!event) | 2263 | if (!event) |
2251 | goto out; | 2264 | goto out; |
2252 | 2265 | ||
@@ -2269,18 +2282,23 @@ ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) | |||
2269 | } | 2282 | } |
2270 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); | 2283 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
2271 | 2284 | ||
2272 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, | 2285 | static void |
2286 | rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer, | ||
2273 | struct ring_buffer_event *event) | 2287 | struct ring_buffer_event *event) |
2274 | { | 2288 | { |
2275 | local_inc(&cpu_buffer->entries); | ||
2276 | |||
2277 | /* | 2289 | /* |
2278 | * The event first in the commit queue updates the | 2290 | * The event first in the commit queue updates the |
2279 | * time stamp. | 2291 | * time stamp. |
2280 | */ | 2292 | */ |
2281 | if (rb_event_is_commit(cpu_buffer, event)) | 2293 | if (rb_event_is_commit(cpu_buffer, event)) |
2282 | cpu_buffer->write_stamp += event->time_delta; | 2294 | cpu_buffer->write_stamp += event->time_delta; |
2295 | } | ||
2283 | 2296 | ||
2297 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, | ||
2298 | struct ring_buffer_event *event) | ||
2299 | { | ||
2300 | local_inc(&cpu_buffer->entries); | ||
2301 | rb_update_write_stamp(cpu_buffer, event); | ||
2284 | rb_end_commit(cpu_buffer); | 2302 | rb_end_commit(cpu_buffer); |
2285 | } | 2303 | } |
2286 | 2304 | ||
@@ -2327,32 +2345,57 @@ static inline void rb_event_discard(struct ring_buffer_event *event) | |||
2327 | event->time_delta = 1; | 2345 | event->time_delta = 1; |
2328 | } | 2346 | } |
2329 | 2347 | ||
2330 | /** | 2348 | /* |
2331 | * ring_buffer_event_discard - discard any event in the ring buffer | 2349 | * Decrement the entries to the page that an event is on. |
2332 | * @event: the event to discard | 2350 | * The event does not even need to exist, only the pointer |
2333 | * | 2351 | * to the page it is on. This may only be called before the commit |
2334 | * Sometimes a event that is in the ring buffer needs to be ignored. | 2352 | * takes place. |
2335 | * This function lets the user discard an event in the ring buffer | ||
2336 | * and then that event will not be read later. | ||
2337 | * | ||
2338 | * Note, it is up to the user to be careful with this, and protect | ||
2339 | * against races. If the user discards an event that has been consumed | ||
2340 | * it is possible that it could corrupt the ring buffer. | ||
2341 | */ | 2353 | */ |
2342 | void ring_buffer_event_discard(struct ring_buffer_event *event) | 2354 | static inline void |
2355 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, | ||
2356 | struct ring_buffer_event *event) | ||
2343 | { | 2357 | { |
2344 | rb_event_discard(event); | 2358 | unsigned long addr = (unsigned long)event; |
2359 | struct buffer_page *bpage = cpu_buffer->commit_page; | ||
2360 | struct buffer_page *start; | ||
2361 | |||
2362 | addr &= PAGE_MASK; | ||
2363 | |||
2364 | /* Do the likely case first */ | ||
2365 | if (likely(bpage->page == (void *)addr)) { | ||
2366 | local_dec(&bpage->entries); | ||
2367 | return; | ||
2368 | } | ||
2369 | |||
2370 | /* | ||
2371 | * Because the commit page may be on the reader page we | ||
2372 | * start with the next page and check the end loop there. | ||
2373 | */ | ||
2374 | rb_inc_page(cpu_buffer, &bpage); | ||
2375 | start = bpage; | ||
2376 | do { | ||
2377 | if (bpage->page == (void *)addr) { | ||
2378 | local_dec(&bpage->entries); | ||
2379 | return; | ||
2380 | } | ||
2381 | rb_inc_page(cpu_buffer, &bpage); | ||
2382 | } while (bpage != start); | ||
2383 | |||
2384 | /* commit not part of this buffer?? */ | ||
2385 | RB_WARN_ON(cpu_buffer, 1); | ||
2345 | } | 2386 | } |
2346 | EXPORT_SYMBOL_GPL(ring_buffer_event_discard); | ||
2347 | 2387 | ||
2348 | /** | 2388 | /** |
2349 | * ring_buffer_commit_discard - discard an event that has not been committed | 2389 | * ring_buffer_commit_discard - discard an event that has not been committed |
2350 | * @buffer: the ring buffer | 2390 | * @buffer: the ring buffer |
2351 | * @event: non committed event to discard | 2391 | * @event: non committed event to discard |
2352 | * | 2392 | * |
2353 | * This is similar to ring_buffer_event_discard but must only be | 2393 | * Sometimes an event that is in the ring buffer needs to be ignored. |
2354 | * performed on an event that has not been committed yet. The difference | 2394 | * This function lets the user discard an event in the ring buffer |
2355 | * is that this will also try to free the event from the ring buffer | 2395 | * and then that event will not be read later. |
2396 | * | ||
2397 | * This function only works if it is called before the the item has been | ||
2398 | * committed. It will try to free the event from the ring buffer | ||
2356 | * if another event has not been added behind it. | 2399 | * if another event has not been added behind it. |
2357 | * | 2400 | * |
2358 | * If another event has been added behind it, it will set the event | 2401 | * If another event has been added behind it, it will set the event |
@@ -2380,14 +2423,15 @@ void ring_buffer_discard_commit(struct ring_buffer *buffer, | |||
2380 | */ | 2423 | */ |
2381 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); | 2424 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
2382 | 2425 | ||
2426 | rb_decrement_entry(cpu_buffer, event); | ||
2383 | if (rb_try_to_discard(cpu_buffer, event)) | 2427 | if (rb_try_to_discard(cpu_buffer, event)) |
2384 | goto out; | 2428 | goto out; |
2385 | 2429 | ||
2386 | /* | 2430 | /* |
2387 | * The commit is still visible by the reader, so we | 2431 | * The commit is still visible by the reader, so we |
2388 | * must increment entries. | 2432 | * must still update the timestamp. |
2389 | */ | 2433 | */ |
2390 | local_inc(&cpu_buffer->entries); | 2434 | rb_update_write_stamp(cpu_buffer, event); |
2391 | out: | 2435 | out: |
2392 | rb_end_commit(cpu_buffer); | 2436 | rb_end_commit(cpu_buffer); |
2393 | 2437 | ||
@@ -2448,7 +2492,7 @@ int ring_buffer_write(struct ring_buffer *buffer, | |||
2448 | if (length > BUF_MAX_DATA_SIZE) | 2492 | if (length > BUF_MAX_DATA_SIZE) |
2449 | goto out; | 2493 | goto out; |
2450 | 2494 | ||
2451 | event = rb_reserve_next_event(cpu_buffer, length); | 2495 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
2452 | if (!event) | 2496 | if (!event) |
2453 | goto out; | 2497 | goto out; |
2454 | 2498 | ||
@@ -2899,8 +2943,7 @@ static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) | |||
2899 | 2943 | ||
2900 | event = rb_reader_event(cpu_buffer); | 2944 | event = rb_reader_event(cpu_buffer); |
2901 | 2945 | ||
2902 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX | 2946 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
2903 | || rb_discarded_event(event)) | ||
2904 | cpu_buffer->read++; | 2947 | cpu_buffer->read++; |
2905 | 2948 | ||
2906 | rb_update_read_stamp(cpu_buffer, event); | 2949 | rb_update_read_stamp(cpu_buffer, event); |
@@ -3132,10 +3175,8 @@ ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) | |||
3132 | spin_unlock(&cpu_buffer->reader_lock); | 3175 | spin_unlock(&cpu_buffer->reader_lock); |
3133 | local_irq_restore(flags); | 3176 | local_irq_restore(flags); |
3134 | 3177 | ||
3135 | if (event && event->type_len == RINGBUF_TYPE_PADDING) { | 3178 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
3136 | cpu_relax(); | ||
3137 | goto again; | 3179 | goto again; |
3138 | } | ||
3139 | 3180 | ||
3140 | return event; | 3181 | return event; |
3141 | } | 3182 | } |
@@ -3160,10 +3201,8 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) | |||
3160 | event = rb_iter_peek(iter, ts); | 3201 | event = rb_iter_peek(iter, ts); |
3161 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | 3202 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
3162 | 3203 | ||
3163 | if (event && event->type_len == RINGBUF_TYPE_PADDING) { | 3204 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
3164 | cpu_relax(); | ||
3165 | goto again; | 3205 | goto again; |
3166 | } | ||
3167 | 3206 | ||
3168 | return event; | 3207 | return event; |
3169 | } | 3208 | } |
@@ -3209,10 +3248,8 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts) | |||
3209 | out: | 3248 | out: |
3210 | preempt_enable(); | 3249 | preempt_enable(); |
3211 | 3250 | ||
3212 | if (event && event->type_len == RINGBUF_TYPE_PADDING) { | 3251 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
3213 | cpu_relax(); | ||
3214 | goto again; | 3252 | goto again; |
3215 | } | ||
3216 | 3253 | ||
3217 | return event; | 3254 | return event; |
3218 | } | 3255 | } |
@@ -3292,21 +3329,19 @@ ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) | |||
3292 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; | 3329 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
3293 | unsigned long flags; | 3330 | unsigned long flags; |
3294 | 3331 | ||
3295 | again: | ||
3296 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); | 3332 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
3333 | again: | ||
3297 | event = rb_iter_peek(iter, ts); | 3334 | event = rb_iter_peek(iter, ts); |
3298 | if (!event) | 3335 | if (!event) |
3299 | goto out; | 3336 | goto out; |
3300 | 3337 | ||
3338 | if (event->type_len == RINGBUF_TYPE_PADDING) | ||
3339 | goto again; | ||
3340 | |||
3301 | rb_advance_iter(iter); | 3341 | rb_advance_iter(iter); |
3302 | out: | 3342 | out: |
3303 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | 3343 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
3304 | 3344 | ||
3305 | if (event && event->type_len == RINGBUF_TYPE_PADDING) { | ||
3306 | cpu_relax(); | ||
3307 | goto again; | ||
3308 | } | ||
3309 | |||
3310 | return event; | 3345 | return event; |
3311 | } | 3346 | } |
3312 | EXPORT_SYMBOL_GPL(ring_buffer_read); | 3347 | EXPORT_SYMBOL_GPL(ring_buffer_read); |
@@ -3373,12 +3408,16 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) | |||
3373 | 3408 | ||
3374 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); | 3409 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
3375 | 3410 | ||
3411 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) | ||
3412 | goto out; | ||
3413 | |||
3376 | __raw_spin_lock(&cpu_buffer->lock); | 3414 | __raw_spin_lock(&cpu_buffer->lock); |
3377 | 3415 | ||
3378 | rb_reset_cpu(cpu_buffer); | 3416 | rb_reset_cpu(cpu_buffer); |
3379 | 3417 | ||
3380 | __raw_spin_unlock(&cpu_buffer->lock); | 3418 | __raw_spin_unlock(&cpu_buffer->lock); |
3381 | 3419 | ||
3420 | out: | ||
3382 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); | 3421 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
3383 | 3422 | ||
3384 | atomic_dec(&cpu_buffer->record_disabled); | 3423 | atomic_dec(&cpu_buffer->record_disabled); |
@@ -3461,6 +3500,7 @@ int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) | |||
3461 | } | 3500 | } |
3462 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); | 3501 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
3463 | 3502 | ||
3503 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP | ||
3464 | /** | 3504 | /** |
3465 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers | 3505 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
3466 | * @buffer_a: One buffer to swap with | 3506 | * @buffer_a: One buffer to swap with |
@@ -3515,20 +3555,28 @@ int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, | |||
3515 | atomic_inc(&cpu_buffer_a->record_disabled); | 3555 | atomic_inc(&cpu_buffer_a->record_disabled); |
3516 | atomic_inc(&cpu_buffer_b->record_disabled); | 3556 | atomic_inc(&cpu_buffer_b->record_disabled); |
3517 | 3557 | ||
3558 | ret = -EBUSY; | ||
3559 | if (local_read(&cpu_buffer_a->committing)) | ||
3560 | goto out_dec; | ||
3561 | if (local_read(&cpu_buffer_b->committing)) | ||
3562 | goto out_dec; | ||
3563 | |||
3518 | buffer_a->buffers[cpu] = cpu_buffer_b; | 3564 | buffer_a->buffers[cpu] = cpu_buffer_b; |
3519 | buffer_b->buffers[cpu] = cpu_buffer_a; | 3565 | buffer_b->buffers[cpu] = cpu_buffer_a; |
3520 | 3566 | ||
3521 | cpu_buffer_b->buffer = buffer_a; | 3567 | cpu_buffer_b->buffer = buffer_a; |
3522 | cpu_buffer_a->buffer = buffer_b; | 3568 | cpu_buffer_a->buffer = buffer_b; |
3523 | 3569 | ||
3570 | ret = 0; | ||
3571 | |||
3572 | out_dec: | ||
3524 | atomic_dec(&cpu_buffer_a->record_disabled); | 3573 | atomic_dec(&cpu_buffer_a->record_disabled); |
3525 | atomic_dec(&cpu_buffer_b->record_disabled); | 3574 | atomic_dec(&cpu_buffer_b->record_disabled); |
3526 | |||
3527 | ret = 0; | ||
3528 | out: | 3575 | out: |
3529 | return ret; | 3576 | return ret; |
3530 | } | 3577 | } |
3531 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); | 3578 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
3579 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ | ||
3532 | 3580 | ||
3533 | /** | 3581 | /** |
3534 | * ring_buffer_alloc_read_page - allocate a page to read from buffer | 3582 | * ring_buffer_alloc_read_page - allocate a page to read from buffer |