Received: by 2002:a05:7208:31d3:b0:81:e143:7c29 with SMTP id v19csp2627402rbd; Tue, 9 Apr 2024 12:12:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdj/GMweM/vfc7WP/1V7eWLtAFcHsccLGI4akSeuv0LJUHuQB3pUv5h6jlw7pbjEQvTZ6k/71rjWtUZMp9QQkNfqDwmH8Fs5TplQaZjQ== X-Google-Smtp-Source: AGHT+IF/qn1a8goNxfrtRkvQgFdZIO9AWWohUB/r0AOEvpZj5kcn3exlFllAeWNGMzzYxge3bJv/ X-Received: by 2002:a05:6214:19cf:b0:69b:32af:2ad3 with SMTP id j15-20020a05621419cf00b0069b32af2ad3mr3220816qvc.20.1712689968886; Tue, 09 Apr 2024 12:12:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712689968; cv=pass; d=google.com; s=arc-20160816; b=Ph5T5NGTZ339wRSMNoKIB4qkU7SKtBxEy1JUycRCxqgcwKBFNeINzJUgrj3z5o11XP rDT3oBeK0GvplDfHhvVHPsOEWMCwQsh/j122ug1WNwTNUf6idjilEE72GLOiL3oddmwU sm3lLjKnd7/wyFyO7XaEFo2Ybv+f6tvme+WUiUsR/ciG6SASMkp7kaJmIpXQPfrVQix5 MGZMM4FK4QNft6UWELvhpHfqJbVrSNjttkOrlDBB5aEXkctUM8k8NnBbKA3Wd4b7PhnQ iBSdJ7q6nhlgjY2F9hvoUV1hOwXDWYCpPORdUGNbDAihEAEcfl6RPBUrrSIHt4hBFE9P tD6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date; bh=fdmJqAHD1JV7oqiiOPM+LE41U9zkx3gogJLR9ReqTx0=; fh=D0eMHS9zFNFs4wCkUUso+UlgAN+04alsSSYXXEQDeSA=; b=LBeYmSAjVUeR90SOqSTIbJ4Ke8YbE3mwxEwRjqUmnJemOXgie3jz4oqIN/2ABtDJFx wwUTTTelcIuUCwevW7wg3SE9EnsE5sa2jdyUvECOuskEwXep8AlWH6WjMEe3MAmrEL+o f4pIFmY7M0FUS0q6ag0D49U06nxbZNiYtWpchvqxc22aIvMU0Av2td/LPT4bhIPQYdV6 AQy4MUL6T7QWoEUikOD3O4MZ1hZ6rfC0s/G7spi6AgNqRR1CeyTLa3iuM0NNJywXJ0pV tlFlZCuY0IDv65zVTfy8aH61RdDjAUYHTSwuORkXVZadK6yqjCRq+k4Yx5Qz4KVR+DGf Sabw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-137511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137511-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p5-20020a05621415c500b00696b0e4b287si10667851qvz.477.2024.04.09.12.12.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 12:12:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-137511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137511-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9FC621C20C38 for ; Tue, 9 Apr 2024 19:12:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87A471581EA; Tue, 9 Apr 2024 19:10:37 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3651157E9F; Tue, 9 Apr 2024 19:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712689836; cv=none; b=oc2pJ9WBx9bo83+blcBgxhFfL4eDUiLD8D5+CLRkEB6nDKWP3hC4QqvG9ggwlgeOfKYJgyw45/FgCg1Xhq0aU7YaO9o/V3bkefsY8Tl6c5JW6LxLcPRApgrywNFYYyiLluIB1QTnMxuKBS1XX1faE2FQQW1H+XfJGWvC1InNRIY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712689836; c=relaxed/simple; bh=40pMLAjuY85R1jd9YK3z5hduTrwij5o0IKhxYs6j88A=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=gRLGQn0wrGU8YEcTFXQTV9dOW20uT6SVU6bnqB4GUnR+93/iYs3Hlu/zyLPL0UTcLvep/SxoHT+Osr553RtM8TqaE6yL1adLoGW0kn5NR10jjNXCZv6SqcdfdDziDpoddoyUsXj+OlT420ZNerhQ25fTtYuCIxnmAio9+zhuZ0c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEC73C43390; Tue, 9 Apr 2024 19:10:35 +0000 (UTC) Date: Tue, 9 Apr 2024 15:13:09 -0400 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mathieu Desnoyers , rsavitski@google.com Subject: [PATCH] ring-buffer: Only update pages_touched when a new page is touched Message-ID: <20240409151309.0d0e5056@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit From: "Steven Rostedt (Google)" The "buffer_percent" logic that is used by the ring buffer splice code to only wake up the tasks when there's no data after the buffer is filled to the percentage of the "buffer_percent" file is dependent on three variables that determine the amount of data that is in the ring buffer: 1) pages_read - incremented whenever a new sub-buffer is consumed 2) pages_lost - incremented every time a writer overwrites a sub-buffer 3) pages_touched - incremented when a write goes to a new sub-buffer The percentage is the calculation of: (pages_touched - (pages_lost + pages_read)) / nr_pages Basically, the amount of data is the total number of sub-bufs that have been touched, minus the number of sub-bufs lost and sub-bufs consumed. This is divided by the total count to give the buffer percentage. When the percentage is greater than the value in the "buffer_percent" file, it wakes up splice readers waiting for that amount. It was observed that over time, the amount read from the splice was constantly decreasing the longer the trace was running. That is, if one asked for 60%, it would read over 60% when it first starts tracing, but then it would be woken up at under 60% and would slowly decrease the amount of data read after being woken up, where the amount becomes much less than the buffer percent. This was due to an accounting of the pages_touched incrementation. This value is incremented whenever a writer transfers to a new sub-buffer. But the place where it was incremented was incorrect. If a writer overflowed the current sub-buffer it would go to the next one. If it gets preempted by an interrupt at that time, and the interrupt performs a trace, it too will end up going to the next sub-buffer. But only one should increment the counter. Unfortunately, that was not the case. Change the cmpxchg() that does the real switch of the tail-page into a try_cmpxchg(), and on success, perform the increment of pages_touched. This will only increment the counter once for when the writer moves to a new sub-buffer, and not when there's a race and is incremented for when a writer and its preempting writer both move to the same new sub-buffer. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 25476ead681b..6511dc3a00da 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1393,7 +1393,6 @@ static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); - local_inc(&cpu_buffer->pages_touched); /* * Just make sure we have seen our old_write and synchronize * with any interrupts that come in. @@ -1430,8 +1429,9 @@ static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, */ local_set(&next_page->page->commit, 0); - /* Again, either we update tail_page or an interrupt does */ - (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page); + /* Either we update tail_page or an interrupt does */ + if (try_cmpxchg(&cpu_buffer->tail_page, &tail_page, next_page)) + local_inc(&cpu_buffer->pages_touched); } } -- 2.43.0