ring-buffer: Fix bytes info in per_cpu buffer stats

The 'bytes' info in file 'per_cpu/cpu<X>/stats' means the number of
bytes in cpu buffer that have not been consumed. However, currently
after consuming data by reading file 'trace_pipe', the 'bytes' info
was not changed as expected.

# cat per_cpu/cpu0/stats
entries: 0
overrun: 0
commit overrun: 0
bytes: 568 <--- 'bytes' is problematical !!!
oldest event ts: 8651.371479
now ts: 8653.912224
dropped events: 0
read events: 8

The root cause is incorrect stat on cpu_buffer->read_bytes. To fix it:
1. When stat 'read_bytes', account consumed event in rb_advance_reader();
2. When stat 'entries_bytes', exclude the discarded padding event which
is smaller than minimum size because it is invisible to reader. Then
use rb_page_commit() instead of BUF_PAGE_SIZE at where accounting for
page-based read/remove/overrun.

Also correct the comments of ring_buffer_bytes_cpu() in this patch.

Link: https://lore.kernel.org/linux-trace-kernel/20230921125425.1708423-1-zhengyejian1@huawei.com

Cc: stable@vger.kernel.org
Fixes: c64e148a3be3 ("trace: Add ring buffer stats to measure rate of events")
Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by Zheng Yejian and committed by Steven Rostedt (Google) 45d99ea4 ce9ecca0

Changed files
+15 -13
kernel
+15 -13
kernel/trace/ring_buffer.c
··· 354 354 local_set(&bpage->commit, 0); 355 355 } 356 356 357 + static __always_inline unsigned int rb_page_commit(struct buffer_page *bpage) 358 + { 359 + return local_read(&bpage->page->commit); 360 + } 361 + 357 362 static void free_buffer_page(struct buffer_page *bpage) 358 363 { 359 364 free_page((unsigned long)bpage->page); ··· 2008 2003 * Increment overrun to account for the lost events. 2009 2004 */ 2010 2005 local_add(page_entries, &cpu_buffer->overrun); 2011 - local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); 2006 + local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes); 2012 2007 local_inc(&cpu_buffer->pages_lost); 2013 2008 } 2014 2009 ··· 2372 2367 cpu_buffer->reader_page->read); 2373 2368 } 2374 2369 2375 - static __always_inline unsigned rb_page_commit(struct buffer_page *bpage) 2376 - { 2377 - return local_read(&bpage->page->commit); 2378 - } 2379 - 2380 2370 static struct ring_buffer_event * 2381 2371 rb_iter_head_event(struct ring_buffer_iter *iter) 2382 2372 { ··· 2517 2517 * the counters. 2518 2518 */ 2519 2519 local_add(entries, &cpu_buffer->overrun); 2520 - local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); 2520 + local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes); 2521 2521 local_inc(&cpu_buffer->pages_lost); 2522 2522 2523 2523 /* ··· 2660 2660 2661 2661 event = __rb_page_index(tail_page, tail); 2662 2662 2663 - /* account for padding bytes */ 2664 - local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes); 2665 - 2666 2663 /* 2667 2664 * Save the original length to the meta data. 2668 2665 * This will be used by the reader to add lost event ··· 2673 2676 * write counter enough to allow another writer to slip 2674 2677 * in on this page. 2675 2678 * We put in a discarded commit instead, to make sure 2676 - * that this space is not used again. 2679 + * that this space is not used again, and this space will 2680 + * not be accounted into 'entries_bytes'. 2677 2681 * 2678 2682 * If we are less than the minimum size, we don't need to 2679 2683 * worry about it. ··· 2698 2700 event->type_len = RINGBUF_TYPE_PADDING; 2699 2701 /* time delta must be non zero */ 2700 2702 event->time_delta = 1; 2703 + 2704 + /* account for padding bytes */ 2705 + local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes); 2701 2706 2702 2707 /* Make sure the padding is visible before the tail_page->write update */ 2703 2708 smp_wmb(); ··· 4216 4215 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts); 4217 4216 4218 4217 /** 4219 - * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer 4218 + * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer 4220 4219 * @buffer: The ring buffer 4221 4220 * @cpu: The per CPU buffer to read from. 4222 4221 */ ··· 4724 4723 4725 4724 length = rb_event_length(event); 4726 4725 cpu_buffer->reader_page->read += length; 4726 + cpu_buffer->read_bytes += length; 4727 4727 } 4728 4728 4729 4729 static void rb_advance_iter(struct ring_buffer_iter *iter) ··· 5818 5816 } else { 5819 5817 /* update the entry counter */ 5820 5818 cpu_buffer->read += rb_page_entries(reader); 5821 - cpu_buffer->read_bytes += BUF_PAGE_SIZE; 5819 + cpu_buffer->read_bytes += rb_page_commit(reader); 5822 5820 5823 5821 /* swap the pages */ 5824 5822 rb_init_page(bpage);