From patchwork Wed Dec 6 15:02:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 174618 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4162578vqy; Wed, 6 Dec 2023 07:02:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEv57a1aQdNPppgoZrKZO2wvj5hRssDaUYH34OKm43xENj0RfLN4mkndRPiQ726RIwAXA35 X-Received: by 2002:a05:6808:118b:b0:3b8:b063:a1c9 with SMTP id j11-20020a056808118b00b003b8b063a1c9mr1156214oil.83.1701874971771; Wed, 06 Dec 2023 07:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701874971; cv=none; d=google.com; s=arc-20160816; b=jAjhElZT6anElR/Hz+ANWlYZnYCpEz3nhQxjDX7AaAbVba6nc1ZtSDt5VyEkuGNA4+ J0DRiJriPQgIsxKCNIqwe4GBS59WxRPmPYz+2Ky657UEbibtAFAcuNIMrkfAIhnM2T/v 3SWW9ldtK1fe91HqxoYJmUO8DDkzCjiyrRL9ABfNg/J2U9NSgI+nukVAGdfGQIYYtGJB sTY1DsbL0GDy58s83dGpjw+lov00UrOMJ2IhZZ9Pn17KRR84HuzYpsRABnT0MSrYom/5 4vw4Y3Dp8T1GS/AkVTUdluAAzQz29Dlcvr9dKllrElpKlZZsubV6MVUCyNnLn1oFr4ZJ XQMg== 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 :message-id:subject:cc:to:from:date; bh=UCC63e8uBHqhOVgyHNrhFYWlq8E6bZu8gOZBRoUSYqg=; fh=R8JZsVKDlS7lXIAvTNV55bsjlDBI0i4sqiOYXO4tZQA=; b=zVFpHctBg4j/fIcWCtO9KE9Xs0V0RqTriV5t120zCpE+9txr1wlh155gXnRSmIwei4 S0fP30z6BjRSim6xBzmlWhB3H+l0MN63biOCP/vwC1SnVZNvdrruG7q+1/GQ96FpUhzk tgrtAGuqF0CrMHkh091ib55MQs6py5WEdEYmUiKtYFApXdQYtuqy54Y7WuC8jEHPgNFE Bsxex6y9Jk+eFK+FvUtHFL1ewKGrRsMpK/eLX54SzTdNdEFFwjTXrnyA/IIWDCsEXgT6 37G6Psi9+Xog2ZZtSwO20edaULS5UK/TAjybEUwAB11bdeGcIvQUlb/29T7q2t4Bl9Vr AZFA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id t15-20020a056808158f00b003b83fc1fdc1si37811oiw.23.2023.12.06.07.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 07:02:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 65671826EE92; Wed, 6 Dec 2023 07:02:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442232AbjLFPCM (ORCPT + 99 others); Wed, 6 Dec 2023 10:02:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379088AbjLFPCL (ORCPT ); Wed, 6 Dec 2023 10:02:11 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 269E0D49 for ; Wed, 6 Dec 2023 07:02:17 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AD1CC433C7; Wed, 6 Dec 2023 15:02:16 +0000 (UTC) Date: Wed, 6 Dec 2023 10:02:44 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers Subject: [PATCH] ring-buffer: Force absolute timestamp on discard of event Message-ID: <20231206100244.5130f9b3@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 06 Dec 2023 07:02:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784545250607292721 X-GMAIL-MSGID: 1784545250607292721 From: "Steven Rostedt (Google)" There's a race where if an event is discarded from the ring buffer and an interrupt were to happen at that time and insert an event, the time stamp is still used from the discarded event as an offset. This can screw up the timings. If the event is going to be discarded, set the "before_stamp" to zero. When a new event comes in, it compares the "before_stamp" with the "write_stamp" and if they are not equal, it will insert an absolute timestamp. This will prevent the timings from getting out of sync due to the discarded event. Cc: stable@vger.kernel.org Fixes: 6f6be606e763f ("ring-buffer: Force before_stamp and write_stamp to be different on discard") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 43cc47d7faaf..a6da2d765c78 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -3030,22 +3030,19 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, local_read(&bpage->write) & ~RB_WRITE_MASK; unsigned long event_length = rb_event_length(event); + /* + * For the before_stamp to be different than the write_stamp + * to make sure that the next event adds an absolute + * value and does not rely on the saved write stamp, which + * is now going to be bogus. + */ + rb_time_set(&cpu_buffer->before_stamp, 0); + /* Something came in, can't discard */ if (!rb_time_cmpxchg(&cpu_buffer->write_stamp, write_stamp, write_stamp - delta)) return false; - /* - * It's possible that the event time delta is zero - * (has the same time stamp as the previous event) - * in which case write_stamp and before_stamp could - * be the same. In such a case, force before_stamp - * to be different than write_stamp. It doesn't - * matter what it is, as long as its different. - */ - if (!delta) - rb_time_set(&cpu_buffer->before_stamp, 0); - /* * If an event were to come in now, it would see that the * write_stamp and the before_stamp are different, and assume