Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp1277023rbe; Fri, 1 Mar 2024 09:10:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVzNoyoPYZX/JZyhMoayjH3AaDmGUWvScSQJJKRhhcYw2ihKWyMQhOjdOvVf7Ul+LsPZsjSOw72q0NgUvnYukzlSfk/v8EZAg2Gld50UQ== X-Google-Smtp-Source: AGHT+IHYvs7JXNurHj6yc9e2SdAlg3QF0ZztJN4oTfzeaHmd8s0PZ9BvI+ey+SbAM7xTDezGRUsE X-Received: by 2002:aa7:de03:0:b0:566:def4:fe80 with SMTP id h3-20020aa7de03000000b00566def4fe80mr1439790edv.36.1709313021862; Fri, 01 Mar 2024 09:10:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709313021; cv=pass; d=google.com; s=arc-20160816; b=O0FFUBSdJoSq0YYxwNbDw5PBIaOTeoPxdtE3wvZ+D1UKkefW1Um1JPaQCKP1WUVWLX vv2dbdFGxq/OVpXpW4zW/z/OO90RG8T2oWa8+OxX92kgNK26qEVo1nRRqJ2AF264Uey0 ZSjqb0aDObmHICR8u5XAsVtKCr0zlGHQjhQZsoXF+gAHDcqXhH4fQ3eV35UVWyo936YD fdO220a07TOu+dzQHEMzkXRiSlSgs0/AD+oHJ/BOc5xI/rISPs86BIYHk7ADAh+grTgx RMlXy2n7iKxBEpMx9rBXvnp7n+74w+VzgTYUv8rDsK05tiMVyke6VTstejspWHwHXNhl a3vQ== 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=hdM7gjK514nCa/teKoBi0qPPt8zXicarTt/rfDtCkrU=; fh=ptYQGT8lIqM09DxdY7RJFw2dOE+IQbSPgs5eSSo4G5k=; b=XYJKB2lWFKTMD8We+sZFMO8DyVSupkUODjF/+AbJhj+lszbMDeHwBFGQQkJSJ98M08 hP258YqPASxSNjqnDRRAGR7x696y05sAx5W3j6meKcHyqVoPi3qoOiWAO4sgztd9w+LC 2rE6fsq3fBi19Ysf8/fH5Fexi+twIxThSzfLGzAq7p7LECH3TGFUkEErrnZIKZPCWfez SAP1yzM7UmoIuE3pZXLLiCtwKEF+QiPpPi1JwJnbACsktFuK8lT+9Qc1qday65AB6pHR d43b/OY/GCUq/aSZF2nZBj8jHd0O4F8/u4PePRqcdTOtteYAyCUFTGseiiF06dDCUrTV 82jQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-88807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88807-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id x15-20020a05640226cf00b00566c42ecfeesi913534edd.111.2024.03.01.09.10.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 09:10:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-88807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88807-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9F2151F21A87 for ; Fri, 1 Mar 2024 17:10:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 62C928494; Fri, 1 Mar 2024 17:09:14 +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 DDC926FC9; Fri, 1 Mar 2024 17:09:13 +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=1709312953; cv=none; b=Ll7XEc9hJ1ZFBhsk7S4dhqziXnwdx56JxfUV2ZHlr/Mb/GTrmD3UIFa+SNp4Z3TNVcZuJsqmY9kHsJ21yQCX4NiEABs11pofoFOCqXfpzuDQL9DJqULMh2CCW7PXSoikzpbE/NXJ98aipYUvOF2Tqc4BFOe6ln2FItUuKcXDqSs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709312953; c=relaxed/simple; bh=XDqoJAXdIq1KRDq0R/8lbUoPXXIchNJTUrkc2hFa5YQ=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LBnN0q60oJGdA4C+pCOSBSj7DWXuNtcV1tR8Hid9Kh+l7zAEZr+DD6lPzCEimq9kSS/I3+KIoJsN8wvf19716VechrFhYIr/2kBBE5+6RXS1KuxCSzuLmR8wcMmV6fk7li+LG5HdWsaldcWYl16UdzTBzfhPRUjFlXjaXfnSI2A= 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 26EAAC433C7; Fri, 1 Mar 2024 17:09:12 +0000 (UTC) Date: Fri, 1 Mar 2024 12:11:21 -0500 From: Steven Rostedt To: Mathieu Desnoyers Cc: linke , paulmck , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mhiramat@kernel.org, Peter Zijlstra , Josh Triplett , Linus Torvalds , Andrew Morton , Ingo Molnar Subject: Re: [PATCH] ring-buffer: use READ_ONCE() to read cpu_buffer->commit_page in concurrent environment Message-ID: <20240301121121.47b4fbdd@gandalf.local.home> In-Reply-To: References: <20240301104945.43119349@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 On Fri, 1 Mar 2024 11:37:54 -0500 Mathieu Desnoyers wrote: > On 2024-03-01 10:49, Steven Rostedt wrote: > > On Fri, 1 Mar 2024 13:37:18 +0800 > > linke wrote: > > > >>> So basically you are worried about read-tearing? > >>> > >>> That wasn't mentioned in the change log. > >> > >> Yes. Sorry for making this confused, I am not very familiar with this and > >> still learning. > > > > No problem. We all have to learn this anyway. > > > >> > >>> 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. > >> > >> I agree with you here. > >> > >> commit = rb_page_commit(commit_page); > >> > >> But if commit_page above is the result of a torn read, the commit field > >> read by rb_page_commit() may not represent a valid value. > > > > But commit_page is a word length, and I will argue that any compiler that > > tears "long" words is broken. ;-) > > [ For those tuning in, we are discussing ring_buffer_iter_empty() > "commit_page = cpu_buffer->commit_page;" racy load. ] > > I counter-argue that real-world compilers *are* broken based on your > personal definition, but we have to deal with them, as documented > in Documentation/memory-barriers.txt (see below). > > What is the added overhead of using a READ_ONCE() there ? Why are > we wasting effort trying to guess the compiler behavior if the > real-world performance impact is insignificant ? > > Quote from memory-barrier.txt explaining the purpose of {READ,WRITE}_ONCE(): > > "(*) For aligned memory locations whose size allows them to be accessed > with a single memory-reference instruction, prevents "load tearing" > and "store tearing," in which a single large access is replaced by > multiple smaller accesses." > > I agree that {READ,WRITE}_ONCE() are really not needed at initialization, > when there are demonstrably no concurrent accesses to the data > > But trying to eliminate {READ,WRITE}_ONCE() on concurrently accessed fields > just adds complexity, prevents static analyzers to properly understand the > code and report issues, and just obfuscates the code. > > Thanks, > > Mathieu > > > > >> > >> In this case, READ_ONCE() is only needed for the commit_page. > > > > But we can at least keep the READ_ONCE() on the commit_page just because it > > is used in the next instruction. > And here I did state that READ_ONCE() does have another use case. So there's no argument about adding it here. -- Steve