Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp408568lqg; Fri, 1 Mar 2024 08:42:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWuY7kGsFWbv+0cLv3FkUTPIFgD1Lo+8LeIhA55HsU2uiPICgyi7BdpI7y3NwoHXEiN9MCBqp9YlPsdzo1DqqLmKUg+S44B6YOTLZC6mQ== X-Google-Smtp-Source: AGHT+IF8INVmpyONu4OWYLKCZSS4mts9QweBDMu2M54+OMuZFFHbrbuwC6qAluPE81uqwzhVzL8G X-Received: by 2002:a05:6a20:8e1b:b0:1a0:e59d:1dc4 with SMTP id y27-20020a056a208e1b00b001a0e59d1dc4mr2418016pzj.11.1709311357594; Fri, 01 Mar 2024 08:42:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709311357; cv=pass; d=google.com; s=arc-20160816; b=m+RFppGNtu+aaC8YaA9hxuJGeoALYaOnQ2U1171mrQNIM+KwOHXh7o4FTbxFjHifgQ aCXNfS4drp1dC+TlNQkaQiZd0CtIO1vBSZS6ikSL3df+QWBb0C5kTcMQzBB4lnecbmCU FKfuTqUbXpuWtnvZY/dRrF7Atup7aOt6X3MmKBmcGdKthLPDLwOIHm99NH5LUIklX2Y4 K70D7wA0uF4rVdLCWyeNqRrARQT5FyxBeNfR2S5FsVYu7jV5YVqoRUy60RfI9nEZu+Bz 1BupB+2EmGv/2R/aVkPrVd+V8gi0x8M/eWCQOAMxtCGu48ABvhgN47kl6ihRpKaxWPfQ eqWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=i0qNWm8bAzu7WzBIesexLN8pDHUSjszkVS/BxFtxLGs=; fh=xVfLYtvmyzgVuZW71VzHCHubklsgRH2qFwnZuQj9byo=; b=x5yUzf1mS8vRo6CPbLDcQ3R7DMuJS3TKYl7DuUk616s1AUkfs/eT3a39L0x/szLL4B /hhnXE2okXGItHJZ3OmTJ6/SOmU5MzGxh17by/7D+G36pSSxMCj7R8JC9G5twvQbjwf1 OVTqDyG2sPBAnP7l6/EhcCzaPos+iXzVcqTargekTtXlVrFDF+ORzBgttESzNrfXh13w e2KOJKuqx6i2rZhd7TdiXSubuZBmnMctFWbuHH5hW+FeJav2QJLoJsNyE39Q7A86Ulf/ zVJz0Zo9/M8PcFsCv0gKV/sgeCFM3sbQteJYqhHxLjucY/6HOXo5LZoFj+IW8i70hP4g gVJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=vzvcqDYD; arc=pass (i=1 spf=pass spfdomain=efficios.com dkim=pass dkdomain=efficios.com dmarc=pass fromdomain=efficios.com); spf=pass (google.com: domain of linux-kernel+bounces-88714-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 2-20020a630002000000b005b91536981csi3959357pga.11.2024.03.01.08.42.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 08:42:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88714-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=vzvcqDYD; arc=pass (i=1 spf=pass spfdomain=efficios.com dkim=pass dkdomain=efficios.com dmarc=pass fromdomain=efficios.com); spf=pass (google.com: domain of linux-kernel+bounces-88714-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 895FDB24D8F for ; Fri, 1 Mar 2024 16:38:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 064D946BB; Fri, 1 Mar 2024 16:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b="vzvcqDYD" Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) (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 DC57F3C29; Fri, 1 Mar 2024 16:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=167.114.26.122 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709311085; cv=none; b=SlMIskWxEGRp+b00h26eUX5PG7ZuF1HAHHBdV5i5pvI4IFrizxtNHnDJZ1vLCTrTVPiZLvE5IT0/tbX0i2/dJW4a02przXENTvRPBIw9o9YfnBGkXTvbgOr5QrgJoro9nDEaKXh45C1QcsQlFY9EBvTs781fwxyFpTOugpb5p0U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709311085; c=relaxed/simple; bh=kxcPnu0fhPYzuZ42n+29yXL+hWBsAeXa8e2UmWZhueY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=jRHHtdbskcyLu6xuJyPDXs/Rj3rU0O7TyiPS0QuXBHGt6id+Hbs4XpdkoZOVZ8RNbdOuTtXEe4w3iZPebSts96ZO8C999QQp25lWH9byn9zQWF+XWPJKQHmXIrn4SX2buM3KrA6iTcGzDAQFskVdb1N3f6NlNeYXVzdM2nHGavo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b=vzvcqDYD; arc=none smtp.client-ip=167.114.26.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1709311072; bh=kxcPnu0fhPYzuZ42n+29yXL+hWBsAeXa8e2UmWZhueY=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=vzvcqDYDL5ec91kEut4gMp+A26q5HCk735VqZCH6l5aHgIyxvX/j1uPHFrbja2QZ+ WFPNeClSJA2a8wVnusBc0HxX4vbPlgW4YuBrIbnvdyKvEbsJ3LokAJtDRyhCpqV1XR ZVB67trnYLjI7SZp/AkuF6m+if/BZUMrK7Vx2gTyqAQx0SuwsEuaiI6feMlRkH9nFC Gd4UOl+G7OO63E0PXyRJnQSBgqleDQMYleYBsLqtHJtA1z7253r3GQ6QJ6KNUwDS9n zs5yR9XE/g1ws2k51aGpeG8PSRTXryYZfv6a4+6mcL31myd1v3gMWTL702fZpbont1 m1w2EQOW2NHXQ== Received: from [172.16.0.134] (192-222-143-198.qc.cable.ebox.net [192.222.143.198]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4TmYfl6TXMzfPb; Fri, 1 Mar 2024 11:37:51 -0500 (EST) Message-ID: Date: Fri, 1 Mar 2024 11:37:54 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] ring-buffer: use READ_ONCE() to read cpu_buffer->commit_page in concurrent environment Content-Language: en-US To: Steven Rostedt , linke , paulmck Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mhiramat@kernel.org, Peter Zijlstra , Josh Triplett , Linus Torvalds , Andrew Morton , Ingo Molnar References: <20240301104945.43119349@gandalf.local.home> From: Mathieu Desnoyers In-Reply-To: <20240301104945.43119349@gandalf.local.home> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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. > > -- Steve -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com