Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp379050ybv; Wed, 19 Feb 2020 01:22:21 -0800 (PST) X-Google-Smtp-Source: APXvYqwuww3v3/49WzhHjW8qkcypsljRkO0xQt35f2abWb1pLTldBY7s5mXFBN1wx21pRUk6Q1r8 X-Received: by 2002:a9d:6b84:: with SMTP id b4mr19124749otq.190.1582104141570; Wed, 19 Feb 2020 01:22:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582104141; cv=none; d=google.com; s=arc-20160816; b=h9jECpmNqVRT2Zl8iW1QDhOpV3vnpO2efMCPzYAQ0qctVi2gGOrBAlBei7i58jOa7+ ei9T/2sCTJdVvDxEXhf517tp0WYG+cLflNAx7joH/web97qjZZwKFGa7L9XcCBFQg03W P6li1Kj3rAaNXWNHV9JjG2NIO4FNaDXv45QAauQMn/fjOBGKCMKmh6VVyMfLOMqAkGIx k9hwGguHsQyPnKNG3mkwLvCWvykrtEhna9CJZDMBUhBT8yqS/bNUviH2lyD1wqY57kH0 QomVNbsVcJGApwsi3GbkYluMgvneYohHLHI03G+cMKbh5IzgHHjG8JnA7LX8vWg6kUgQ MfGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=jnk001kHHnMJZPh5284on/H7ZVuekrqsnplVhw3qKG4=; b=NNsS0ZQM6oRvv//Uszq810w9QMm7Yx6QAElORDT9wbRX4R3SWgSSYMkj9nwEVQI54R xmxj00dIAmtZUYjZdNJW0jacYgMsu9tuFnoANH+fvqM+zn8FsNmev7x9Rs4alySoFvDX Wep3ybO40fii6ihWHhSlxShNdjuPK0Gnqnm+NgQki82P0wDByU0pns85SVKSBJ12jlxB Es31U8qT8YKoVl5Gw6xV3eYp7rdHsk0n9cKKICN41wN7P6BZeEeAVrk9mIKgNsrSjCgM cu4Y4YQ9v8H7IMM3P5scLGx2s0xMgIbPtDaF/Hm2GSSZMFXonybLetisoSbHPHChOtn6 Yj5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=skzdvmlk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f190si9774879oig.229.2020.02.19.01.22.08; Wed, 19 Feb 2020 01:22:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=skzdvmlk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726297AbgBSJWA (ORCPT + 99 others); Wed, 19 Feb 2020 04:22:00 -0500 Received: from mail-ot1-f66.google.com ([209.85.210.66]:44466 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726202AbgBSJV7 (ORCPT ); Wed, 19 Feb 2020 04:21:59 -0500 Received: by mail-ot1-f66.google.com with SMTP id h9so22419176otj.11 for ; Wed, 19 Feb 2020 01:21:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jnk001kHHnMJZPh5284on/H7ZVuekrqsnplVhw3qKG4=; b=skzdvmlkEwjc+AAGfz8cHX6+KV0Ear7q/T2X/aLZyVNB8hYHhuw7952mIi9a+oHsp2 a1qaqaJ4LCcNqSSVeOrMDRc8eCDQ3PQxS8dNBJdz6Jn23OH1JxI0uxCYBe6DfX+c3Wsv BMvlxvoP7fNcpGpjkNUEGOMVNRsOdBPAlrgy+eyisWwxiYxQbtZhI/qyU3ILtNbxWj30 XJ7Y5/V1GTnfsI37Rl1wHgxVw8Ow3geprTuNTc9F7LzKNG4Bq3L0gtcPzyJIV6b75eUg jFoCUoEaq29fBnOKzUjWJKft6VdearndY50Bh6pcEV5S8fsxKTzBy/FpQ9saBVoUf2eO DNNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jnk001kHHnMJZPh5284on/H7ZVuekrqsnplVhw3qKG4=; b=RRYdKl3wmOfG5JIlgtvsFFLdWcDWQtQPSqWF4p6kRErI87tZgpg/ppEb4NOEXTUZF1 CpC01A3P6CacqH2LNFBTk1tQb3cGFNWxcnVNGP6JRzkzRX8BJ75wvrzhDiksrFhaeOp9 heGiRha/jTHfxRWFBrGegdFg4GEycH58tuet3At0gVJTeDY6uAP6gjrjz0moHRhXl/qB E2jCSEPh/DtYNLNYJeFmNDfwnH8OaE2g4kNXmynnqiWSC4Q4HpJ1MBpbhCCzXqszyNDH gg7e+ONCpDch0/0JhcjAMXaP0yNI0pXO2S+fOeJbCt9a/XAxV8ylF96Ht/+Ym/7uQBL+ iBQw== X-Gm-Message-State: APjAAAXc7GfN402otGU9WZihXGqxowdmi2RL0XxlzjZHOEMinNJB8E4i YztAliUZ1ouG222isE8156OhpWc0lfo23pDEKmi5Mg== X-Received: by 2002:a9d:7410:: with SMTP id n16mr19494651otk.23.1582104117580; Wed, 19 Feb 2020 01:21:57 -0800 (PST) MIME-Version: 1.0 References: <20200219045228.GO23230@ZenIV.linux.org.uk> <20200219052329.GP23230@ZenIV.linux.org.uk> In-Reply-To: <20200219052329.GP23230@ZenIV.linux.org.uk> From: Marco Elver Date: Wed, 19 Feb 2020 10:21:46 +0100 Message-ID: Subject: Re: [PATCH] fs: fix a data race in i_size_write/i_size_read To: Al Viro Cc: Qian Cai , Christoph Hellwig , "Darrick J. Wong" , linux-xfs@vger.kernel.org, linux-fsdevel , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 19 Feb 2020 at 06:23, Al Viro wrote: > > On Wed, Feb 19, 2020 at 12:08:40AM -0500, Qian Cai wrote: > > > > > > > On Feb 18, 2020, at 11:52 PM, Al Viro wrote: > > > > > > If aligned 64bit stores on 64bit host (note the BITS_PER_LONG ifdefs) end up > > > being split, the kernel is FUBAR anyway. Details, please - how could that > > > end up happening? > > > > My understanding is the compiler might decide to split the load into saying two 4-byte loads. Then, we might have, > > > > Load1 > > Store > > Load2 > > > > where the load value could be a garbage. Also, Marco (the KCSAN maintainer) who knew more of compiler than me mentioned that there is no guarantee that the store will not be split either. Thus, the WRITE_ONCE(). > > In theory, yes. But by now we're aware of the current convention of assuming plain aligned writes up to word-size are atomic (which KCSAN respects with default Kconfig). > I would suggest > * if some compiler does that, ask the persons responsible for that > "optimization" which flags should be used to disable it. > * if they fail to provide such, educate them regarding the > usefulness of their idea > * if that does not help, don't use the bloody piece of garbage. > > Again, is that pure theory (because I can't come up with > any reason why splitting a 32bit load would be any less legitimate > than doing the same to a 64bit one on a 64bit architecture), > or is there anything that really would pull that off? Right. In reality, for mainstream architectures, it appears quite unlikely. There may be other valid reasons, such as documenting the fact the write can happen concurrently with loads. Let's assume the WRITE_ONCE can be dropped. The load is a different story. While load tearing may not be an issue, it's more likely that other optimizations can break the code. For example load fusing can break code that expects repeated loads in a loop. E.g. I found these uses of i_size_read in loops: git grep -E '(for|while) \(.*i_size_read' fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode)) { fs/ocfs2/dir.c: for (i = 0; i < i_size_read(inode) && i < offset; ) { fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode)) { fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode) fs/squashfs/dir.c: while (length < i_size_read(inode)) { fs/squashfs/namei.c: while (length < i_size_read(dir)) { Can i_size writes happen concurrently, and if so will these break if the compiler decides to just do i_size_read's load once, and keep the result in a register? Thanks, -- Marco