Received: by 2002:ab2:2997:0:b0:1ec:cbc4:63fb with SMTP id n23csp509824lqb; Thu, 29 Feb 2024 07:19:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUMOg/H+PKUI945xVk7NLKLwnZJbN9K3OBSMv6uQRBnLby0R1WN2pAhGzf4+fQIWpV97M4BLsECMi2ZlCUjbpwwaz2daqwgMiKIY1EBuQ== X-Google-Smtp-Source: AGHT+IFfE7iVOiBKfjcYdUvNOs/zn7NjcgqgryAku+yuiwSI8DODzebPrnC+MbFiMfk64kbk7YsX X-Received: by 2002:a05:622a:174f:b0:42e:1f86:3910 with SMTP id l15-20020a05622a174f00b0042e1f863910mr2787257qtk.62.1709219957934; Thu, 29 Feb 2024 07:19:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709219957; cv=pass; d=google.com; s=arc-20160816; b=QeX5dVEAh3iN4KeMDtIveffYuS8BCKYuhBolLocUSqG78WSBoOynT3EgDBKtOTFha3 LqxZjFLDvE4MVtLH0wBm8zGq24RDEcez0b1pN6LFGn7hJ8BDKiUneL5YqfBPXzUfOygp 7sBpXXg2oKkqc416bmk3BTa7mMSSLUzwpMQz1Pc3EQlxQ/iJe2tz0tzyC5YY7ZrDZ2oz 22drF1BJ8atO9xXEsskyZ9lsLqIbA81mFjVBDbaoKR1I5ojXwPXA5frz9NIDAK6YU2UQ E0gXm0tdJYr9GSGmHYgVWT4XUxB7Z1aQmsY+9xgrM2kmUiBoxHXy5dupMsktfnYQwAVZ qEOw== 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:references:in-reply-to:message-id :subject:cc:to:from:date; bh=OZdk1YDebDMIp0ZNRfujAkzLDHHblJtaoTuHYwotulk=; fh=lGwW5lXhJ3xWkdGi5w6Msrr4cx4mWwhJgi8T+AHOup0=; b=EUeW8aPb8OA+0ra84lBmvb73qkma8p2Dgm+CKIL3zun2d9JNc0j6hgGCxNhd3nUFnU TLGKuy7ryrxARmSNCcwJJD6/mJNfaUHHUME/MVvcwHxwWca3SnoEIYWQy5vSdCSeERkR NTR1fmqWjxD2K5tCRVJvWSx3fIzV7mhNHHQ+Bc5B9gbtcd1hDsrWOphvAbV6nkf0wpYY //fJcb6/YExPnUM4PR8UJ7AXtaYOgz9nXj5NQtxVet7vFI1Tbm4QWpq6ioRvq3R121Z+ 23kKIemcN9ttIrCvPlgl+lLFkq4FN13nO3yuIku9CgwyGxedRNvYqYAO+fwuL6mrC2bm nBAg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-86913-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86913-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id k10-20020ac85fca000000b0042ec2c2cd0csi610502qta.281.2024.02.29.07.19.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 07:19:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86913-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-86913-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86913-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 751CF1C20F56 for ; Thu, 29 Feb 2024 15:19:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 10C7B13DBA5; Thu, 29 Feb 2024 15:19:15 +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 8604312F362; Thu, 29 Feb 2024 15:19:14 +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=1709219954; cv=none; b=NANCQa2OCtBA9qRs+j35c1XzmXMdsjy6tnkJgkZHQCFqroB+MDzw+FVzS0xvGeY/cp5rsa1zvsvuO1N83EE8dRbyVo1enN9pUrw2FPfO85DUo6n1Cxhn4nu24ByshSMyWi/pXC6jgm/rcl/dtj2yfwAoabr7hq92439Gr0VCKVg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709219954; c=relaxed/simple; bh=AkQ/AaKsEPuD8c6bNddvXHBp1aND4HVn4Dv1rsPMEkg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=balmccoC4WHyMnSdqlaXyIkZcrRINYj+Z+YBMaKzT7QRG8i3/nkFYdIWPGrbhrZZ1zlHOL9zwNSgBPo+VMNO3TCezEDpoRgPrR5PbGQwbdFHMhwZPSQVQEgJKeUsF12M0h5y3o+fpXJZP4DoMjGwMxT3p4+Jq1nW8tDlHA+SvZE= 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 2A192C433C7; Thu, 29 Feb 2024 15:19:13 +0000 (UTC) Date: Thu, 29 Feb 2024 10:21:19 -0500 From: Steven Rostedt To: linke Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com, mhiramat@kernel.org Subject: Re: [PATCH] ring-buffer: use READ_ONCE() to read cpu_buffer->commit_page in concurrent environment Message-ID: <20240229102119.7c475dee@gandalf.local.home> In-Reply-To: References: 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 On Thu, 29 Feb 2024 20:32:26 +0800 linke wrote: > Hi Steven, sorry for the late reply. > > > > > Now the reason for the above READ_ONCE() is because the variables *are* > > going to be used again. We do *not* want the compiler to play any games > > with that. > > > > I don't think it is because the variables are going to be used again. > Compiler optimizations barely do bad things in single thread programs. It > is because cpu_buffer->commit_page may change concurrently and should be > accessed atomically. So basically you are worried about read-tearing? That wasn't mentioned in the change log. > > /* Make sure commit page didn't change */ > curr_commit_page = READ_ONCE(cpu_buffer->commit_page); > curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp); > > /* If the commit page changed, then there's more data */ > if (curr_commit_page != commit_page || > curr_commit_ts != commit_ts) > return 0; > > This code read cpu_buffer->commit_page and time_stamp again to check > whether commit page changed. It shows that cpu_buffer->commit_page and > time_stamp may be changed by other threads. > > commit_page = cpu_buffer->commit_page; > commit_ts = commit_page->page->time_stamp; > > So the commit_page and time_stamp above is read while other threads may > change it. I think it is a data race if it is not atomic. Thus it is > necessary to use READ_ONCE() here. Funny part is, if the above timestamp read did a tear, then this would definitely not match, and would return the correct value. That is, the buffer is not empty because the only way for this to get corrupted is if something is in the process of writing to it. Now we could add a comment stating this. So, I don't even think the reading of the commit_page is needed (it's long size so it should not tear, and if it does, I consider that more a bug in the compiler). Please explain why READ_ONCE() is needed, and what exactly is it "fixing". That is, what breaks if it's not there? -- Steve