Received: by 2002:a05:7208:13c3:b0:82:bbfa:f723 with SMTP id r3csp1538174rbe; Wed, 15 May 2024 06:22:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUyJjmF7JmaNLF3WjKY1ifnp2CADVWsQori89CfB3mvrkz1YWu26GEBoLtA0TA2Vb6XTdNEwy7e+s3KkIgONEQwRc/FS+/wxyRuuKt+QQ== X-Google-Smtp-Source: AGHT+IFy+AW57fdoB56LmGsuf8plp255wcY96dHPTExAJCGimUEUNBd+R84r3td8keLKm8g74wZx X-Received: by 2002:a50:d685:0:b0:572:9b20:fd with SMTP id 4fb4d7f45d1cf-5734d67af65mr10381257a12.31.1715779322044; Wed, 15 May 2024 06:22:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715779322; cv=pass; d=google.com; s=arc-20160816; b=ogi4EvHdVPjdwNV4eU3U+lf4e/jTYF33p50lWVYa9xVj0p9lnoHNMdnTH68Lj7pLnM CflHcVAcDRlbNNBHf5xxNPnLuxN9/7oKbCavg6mwwQDA58lE8F9oVEQwuiTvcpIUctes DngcipK/Np2UUlaSU7draedHxlgZbH72Cmp1h4fFjxHLxqRDZZqowqZYgWE8yX4HoWR2 xCrBbDNfFXOny/gO7Tk0JDJkHsoUS9diWVOE4bjE+OiPmM1LGVICR02CTvLhFFs1zRdJ z1kU6nN5+OYTd+VhW0guHBE3qUYPhtgfDnrQw3Y2GtrjEvhuODmhj6RTmx6wwmVeoIZR uJTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=aoeP0Ladk/z24yu+FttWXxEkpyrYh4P15if6ZA4rJMM=; fh=Fcb43TjvjEvY1psCC40orAuSNY8pwZnyLCCe/3Plu5U=; b=ApeUJ3QGN0us1Lx411O+5Z1JrM6z9XBJzDkvfT2FUEzF+t2Uht/PJ3DDJzMy14UtTt KwACWnNhvm1E48+N+mCYhTHEeVjnTCsNmxKVDjdpf5d8+iLxzGRdSoU78QlL+/LG/MZF QiwHJ6KI94I3u7NbmMk5LeGN6TJFVgNbmYOQ7BGhPC9sjZy/r08G8Px8cy2nODUgTYFE F8NQpaJ3AoD8Tb80lV0fyYuoJlznrtTlmCQkqpUlxQlQYLGTw0W2YK8j6JDgG28Ika57 x2VQHKj9CCkO1dAJuorpeqwmpT656A2N1CjymHc/C6zhTmBi5W07TEL4aDBNWqiqoOKI SJZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PK5hx74y; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-179909-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179909-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-5733c2d458fsi7402525a12.342.2024.05.15.06.22.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 06:22:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179909-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; dkim=pass header.i=@google.com header.s=20230601 header.b=PK5hx74y; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-179909-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179909-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 884911F279A9 for ; Wed, 15 May 2024 13:22:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6AAC812BEAB; Wed, 15 May 2024 13:21:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PK5hx74y" Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17BE112AAF7 for ; Wed, 15 May 2024 13:21:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715779283; cv=none; b=dNYI2ZsaSe0hK9PdQZ7FLEiAh5Xv6uGXDgdPIYek8bnQvnMzz2F7i6RLx+9Uajus2Phxc20kCtqcSlkBs45nY3GNmaM14Gpbqwc37/SdzyiPzGmwu1O7XC/Swijc1VqD/7TqKyO6c2pmiAGVOI83iiLGK5oeluu832o/0w1H2zY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715779283; c=relaxed/simple; bh=e8/pYDYtbf8OuDE4k++PDPWJCRs31hyPD+r8+dkMjss=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=dlCGG9GY45RDqGnEfb09tALnzdA7Kvv0f+B3C7vsTsV0jer77YB8ZXQ2VRB0GhSzDolOQDrkE/e9NPI5XfQjJ+5/QOnytGg3+ALh1c2t+nevisdRe99woqcNgT4MUINUASXDrvXxlAy6bJ2PrP8tehhYFSXxVmRzNyOOB/4W0Hg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PK5hx74y; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3c9995562a0so3060111b6e.2 for ; Wed, 15 May 2024 06:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715779281; x=1716384081; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=aoeP0Ladk/z24yu+FttWXxEkpyrYh4P15if6ZA4rJMM=; b=PK5hx74yt+hDOAPct5nDFH84vrPlSwwZgUMfffDbk6URMTALoLLiSd75bToIcnnduH jX1RK2rJaPSDqzeXM+gZUJ9EHOe77+Xmr/r3TYRoFIlPlPIQGXapMTagyPBiZ7AzrgEK 28pfrcGikgyXU0Ln4J8JHHKEZCqKEjNAtLAqpYjWiP6o2NchcZmVsJbwOoHifCvixq/N M+jhnYTBJxCV/Dl3TVbNc6oywdll2JFkRbjcAhRy7lOYBCnHCx0Oc3BHRbuigFlg2QUi 7VkYJRYM92cb7Ml3JQZudKx8G31MJI3HArN3BHW5Fp+f7iM6nzsRYdbSXauwXJ8/6uA9 DioQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715779281; x=1716384081; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aoeP0Ladk/z24yu+FttWXxEkpyrYh4P15if6ZA4rJMM=; b=YfDIbyKJ89csgOH7cwDRN8EqBuGTEEIl93VpwsZmo4txeUAjlwe20/Z5ydYwCBw9Wk NcpOf7yiqDpPK1IdJbqRuSBvA/nlgn8PaQcMmgy5W7+ePIo1PyvhKAgAy07jEfSjOpLK JLYiD1PHX+kP8L39vYMloR6XXFFW8RFDB94JKT0tqO8GIdN9foxH+/FHy/6gPbsenfsZ zgDHoP0XxXFH186Lr2n54+Mr+tskNZ0VV9PNuIrGP0cC93GpeuFQkspt7k6T78urYNpy +ZY5AHsBxYdtxyJJi3BHTLdmm4OaD1k2u/EIIo9xqSqRwRIbf8ei9RHogm5hj84zLAuf NjRw== X-Forwarded-Encrypted: i=1; AJvYcCVu5f6y/YXLm7ODqXxULMkHYbuXU2KY6J4rdnZmBZsdvTr9pIiTD92CGH0FVsHNvhML0k//VTcirpwFapjm2XP6vZeqZRlTI++UZUUk X-Gm-Message-State: AOJu0Yyz+3spAWPTgfHePVqkoCVL9hl/2t2MPZk7BKjDBqYZ6nQZ/l7T 8Ru7z4zvDDlWx9gJDfYBKwidjDnSwVKXe00kqKR1SVqFyKRR1k9axgUjg/cC5O7rmBCCgaOfoVD oVrEfPE8psfccxmDSbtFKD1LXfsL2bRsq2peJ X-Received: by 2002:a05:6808:f0c:b0:3c9:bcf1:96a8 with SMTP id 5614622812f47-3c9bcf198b0mr2389080b6e.39.1715779280902; Wed, 15 May 2024 06:21:20 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <4d230bac-bdb0-4a01-8006-e95156965aa8@acm.org> <447ad732-3ff8-40bf-bd82-f7be66899cee@paulmck-laptop> <59ec96c2-52ce-4da1-92c3-9fe38053cd3d@paulmck-laptop> In-Reply-To: From: Marco Elver Date: Wed, 15 May 2024 15:20:41 +0200 Message-ID: Subject: Re: [PATCH] block: Annotate a racy read in blk_do_io_stat() To: Breno Leitao Cc: paulmck@kernel.org, Bart Van Assche , Jens Axboe , "open list:BLOCK LAYER" , open list Content-Type: text/plain; charset="UTF-8" On Wed, 15 May 2024 at 14:48, Breno Leitao wrote: > > On Wed, May 15, 2024 at 09:58:35AM +0200, Marco Elver wrote: > > On Wed, 15 May 2024 at 01:47, Paul E. McKenney wrote: > > > On Mon, May 13, 2024 at 10:13:49AM +0200, Marco Elver wrote: > > > +Use of volatile and __data_racy > > > +------------------------------- > > > + > > > +Adding the volatile keyword to the declaration of a variable causes both > > > +the compiler and KCSAN to treat all reads from that variable as if they > > > +were protected by READ_ONCE() and all writes to that variable as if they > > > +were protected by WRITE_ONCE(). > > > "volatile" isn't something we encourage, right? In which case, I think > > to avoid confusion we should not mention volatile. After all we have > > this: Documentation/process/volatile-considered-harmful.rst > > Since you mentioned this document, the other day I was reading > volatile-considered-harmful.rst document, and I was surprised that there > is no reference for READ_ONCE() primitive at all (same for WRITE_ONCE). > > # grep -c READ_ONCE Documentation/process/volatile-considered-harmful.rst > 0 > > From my perspective, READ_ONCE() is another way of doing real memory > read (volatile) when you really need, at the same time keeping the > compiler free to optimize and reuse the value that was read. The Linux kernel memory model [1] defines the semantics of READ_ONCE() / WRITE_ONCE(). You could say that a READ_ONCE() is something like a "relaxed (but sometimes consume) atomic load" (in C11 lingo), and a WRITE_ONCE() is a "relaxed atomic store". But again, not exactly because the kernel wants to do things that are outside the C standard and no compiler fully supports. This has fun consequences, such as [2]. [1] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0124r6.html [2] https://lpc.events/event/16/contributions/1174/attachments/1108/2121/Status%20Report%20-%20Broken%20Dependency%20Orderings%20in%20the%20Linux%20Kernel.pdf To get what the kernel wants, implementing *ONCE in terms of "volatile" works in all important cases. But we know that it can go wrong - [2] discusses this. The big hope is that one day, the kernel can just switch the *ONCE() implementation to use something better but as a programmer we shouldn't care that there's volatile underneath. > Should volatile-considered-harmful.rst be also expanded to cover > READ_ONCE()? No, on most architectures READ_ONCE/WRITE_ONCE are implemented with volatile, but that's merely an implementation detail. Once upon a time, READ_ONCE on alpha actually implied a real barrier. On arm64 with LTO, READ_ONCE translates into an acquire-load [3] to mitigate the risk of "volatile" actually resulting in "miscompilation" (according to our desired semantics of READ_ONCE). [3] https://elixir.bootlin.com/linux/latest/source/arch/arm64/include/asm/rwonce.h#L26 Thanks, -- Marco