Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp2879804rwo; Mon, 24 Jul 2023 02:47:23 -0700 (PDT) X-Google-Smtp-Source: APBJJlHvAWKmsBBkQA7SJsXVNgw8dJuyAP21EqiCvNEa78f/ZydccMDr7VxgU85FoFvrUJsmcrMR X-Received: by 2002:a17:907:a043:b0:994:4f4a:218b with SMTP id gz3-20020a170907a04300b009944f4a218bmr9875460ejc.6.1690192043343; Mon, 24 Jul 2023 02:47:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690192043; cv=none; d=google.com; s=arc-20160816; b=abi2qqESwivZ1tnQTk1VL7FgjswRxdTgXcpLefxZyjV6BjUY9ug5g0dz1DC7h+mnzp SmPsko2zkMIckoBfkAWpOoS7q4TteVy8POwz2M6u9jDUBpY+EdE1gUKlThfEkDFwJ/pE mYUmuO2P+3tqaMMCMYyV1x+cg7ZMyf/UmbbnkLSXm3w6DEMp3zz5nrH+rdaODuRtrkPX 9e4oBdN66o7e0JVYgUMYSoaESvYvE9afzsfY1jJhCizOWbFqkr3mTPXZQRMKNy5LeT+P BWODcT1J2YH7elriIjf75KiTZCcyDt7Qq6q3hgtuIZAHRV/wIiSVgy2q5odg4KqwawXv 6CbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=LsRrJJ2c91JRs5Jd+wZCJ2I+Qi32JQEdyhVfjgFkAMg=; fh=VuYSgIFCSC3IusgM+kf+sxudREcQiFQCKM3osuNh6dI=; b=DhzIDOzAL4IshP3KSbX0ge/M2Z03jsiuqoAwaZ+/nwG1ZmXzuW4NKIbHnmMI+sgQ/n BrKSj2AFBpH2sFkhbOTv+i/mIo46xjVgmqXjJ9bO2ClsGN5JoSI2HwPBmT70/YEM5bxI Xu1Te7/L3d26/n8lZotXWHSJ/rsbX0Zh+lSWmVOPnNBZKENfsYiG+YS4zVhpY5JKpEBB 0Uq/CGb1DCrpx2yQaIinKxqyoezFsgiv+M5udmKGtRexPuht8NUVm3UB+8U6uMm3xb8i +ck73z7tLPKIb5uZ4g9QDJs25kn5SMCcRP2S4wOYKevo7HSIhjcJmdFB0W7Fni7I27pN dYAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p6-20020a170906140600b00992bf74c31csi5831314ejc.1005.2023.07.24.02.46.58; Mon, 24 Jul 2023 02:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231778AbjGXInj (ORCPT + 99 others); Mon, 24 Jul 2023 04:43:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231765AbjGXIn0 (ORCPT ); Mon, 24 Jul 2023 04:43:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90C3F1B4; Mon, 24 Jul 2023 01:43:08 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2EC4C60FD3; Mon, 24 Jul 2023 08:43:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4C9AC433C7; Mon, 24 Jul 2023 08:43:06 +0000 (UTC) Date: Mon, 24 Jul 2023 04:43:04 -0400 From: Steven Rostedt To: Zheng Yejian Cc: , , , Subject: Re: [PATCH] ring-buffer: Fix wrong stat of cpu_buffer->read Message-ID: <20230724044304.5a93a266@rorschach.local.home> In-Reply-To: <20230724054040.3489499-1-zhengyejian1@huawei.com> References: <20230724054040.3489499-1-zhengyejian1@huawei.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Do you folks find it fun to post a fix right after I send Linus a pull request? ;-) Thanks, I'll queue it up (but I will wait a bit before posting in case more fixes trickle in) -- Steve On Mon, 24 Jul 2023 13:40:40 +0800 Zheng Yejian wrote: > When pages are removed in rb_remove_pages(), 'cpu_buffer->read' is set > to 0 in order to make sure any read iterators reset themselves. However, > this will mess 'entries' stating, see following steps: > > # cd /sys/kernel/tracing/ > # 1. Enlarge ring buffer prepare for later reducing: > # echo 20 > per_cpu/cpu0/buffer_size_kb > # 2. Write a log into ring buffer of cpu0: > # taskset -c 0 echo "hello1" > trace_marker > # 3. Read the log: > # cat per_cpu/cpu0/trace_pipe > <...>-332 [000] ..... 62.406844: tracing_mark_write: hello1 > # 4. Stop reading and see the stats, now 0 entries, and 1 event readed: > # cat per_cpu/cpu0/stats > entries: 0 > [...] > read events: 1 > # 5. Reduce the ring buffer > # echo 7 > per_cpu/cpu0/buffer_size_kb > # 6. Now entries became unexpected 1 because actually no entries!!! > # cat per_cpu/cpu0/stats > entries: 1 > [...] > read events: 0 > > To fix it, introduce 'page_removed' field to count total removed pages > since last reset, then use it to let read iterators reset themselves > instead of changing the 'read' pointer. > > Fixes: 83f40318dab0 ("ring-buffer: Make removal of ring buffer pages atomic") > Signed-off-by: Zheng Yejian > --- > kernel/trace/ring_buffer.c | 22 ++++++++++++---------- > 1 file changed, 12 insertions(+), 10 deletions(-) > > diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c > index de061dd47313..46b4a3c7c3bf 100644 > --- a/kernel/trace/ring_buffer.c > +++ b/kernel/trace/ring_buffer.c > @@ -523,6 +523,8 @@ struct ring_buffer_per_cpu { > rb_time_t before_stamp; > u64 event_stamp[MAX_NEST]; > u64 read_stamp; > + /* pages removed since last reset */ > + unsigned long pages_removed; > /* ring buffer pages to update, > 0 to add, < 0 to remove */ > long nr_pages_to_update; > struct list_head new_pages; /* new pages to add */ > @@ -559,6 +561,7 @@ struct ring_buffer_iter { > struct buffer_page *head_page; > struct buffer_page *cache_reader_page; > unsigned long cache_read; > + unsigned long cache_pages_removed; > u64 read_stamp; > u64 page_stamp; > struct ring_buffer_event *event; > @@ -1957,6 +1960,8 @@ rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages) > to_remove = rb_list_head(to_remove)->next; > head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD; > } > + /* Read iterators need to reset themselves when some pages removed */ > + cpu_buffer->pages_removed += nr_removed; > > next_page = rb_list_head(to_remove)->next; > > @@ -1978,12 +1983,6 @@ rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages) > cpu_buffer->head_page = list_entry(next_page, > struct buffer_page, list); > > - /* > - * change read pointer to make sure any read iterators reset > - * themselves > - */ > - cpu_buffer->read = 0; > - > /* pages are removed, resume tracing and then free the pages */ > atomic_dec(&cpu_buffer->record_disabled); > raw_spin_unlock_irq(&cpu_buffer->reader_lock); > @@ -4395,6 +4394,7 @@ static void rb_iter_reset(struct ring_buffer_iter *iter) > > iter->cache_reader_page = iter->head_page; > iter->cache_read = cpu_buffer->read; > + iter->cache_pages_removed = cpu_buffer->pages_removed; > > if (iter->head) { > iter->read_stamp = cpu_buffer->read_stamp; > @@ -4849,12 +4849,13 @@ rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) > buffer = cpu_buffer->buffer; > > /* > - * Check if someone performed a consuming read to > - * the buffer. A consuming read invalidates the iterator > - * and we need to reset the iterator in this case. > + * Check if someone performed a consuming read to the buffer > + * or removed some pages from the buffer. In these cases, > + * iterator was invalidated and we need to reset it. > */ > if (unlikely(iter->cache_read != cpu_buffer->read || > - iter->cache_reader_page != cpu_buffer->reader_page)) > + iter->cache_reader_page != cpu_buffer->reader_page || > + iter->cache_pages_removed != cpu_buffer->pages_removed)) > rb_iter_reset(iter); > > again: > @@ -5298,6 +5299,7 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) > cpu_buffer->last_overrun = 0; > > rb_head_page_activate(cpu_buffer); > + cpu_buffer->pages_removed = 0; > } > > /* Must have disabled the cpu buffer then done a synchronize_rcu */