Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp972134ybl; Fri, 24 Jan 2020 12:59:21 -0800 (PST) X-Google-Smtp-Source: APXvYqzCGLDsmYjEWPmEDTn3Jgw8CqllWSrOz6KJJZ2sa+ZlOlFIFbMZAxxvBfpbdjtooxI6aJp/ X-Received: by 2002:a9d:6045:: with SMTP id v5mr3882778otj.252.1579899561218; Fri, 24 Jan 2020 12:59:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579899561; cv=none; d=google.com; s=arc-20160816; b=UZIR6lgiAbH2G76Lu+ql0AsfFGYeVuD1WIC8EqEWWPGRMOcr30V5WAyRrZ07kVCO9C VJc/5VMhFLlhFBt1Qq12CDIbz6Agoeori4yH0cDjh7KZ3055K/k4SDRQSGkblf2ss+DQ boOIw3DOeaWcP65nVauq08GOd0H7WskJFNzdQVQ4MXuyhh5S0WJ/npB4U6/pE2PJG6GM SZ9ybFAgZqqkjom6i4Sr38IFqT9J6oXYQW+8FmZxgKYEC7fzOgta9p+5FAnbuvJ1UhKC oNSqdduMb0jXRPx3aZgu8P0mFLjnS3VU3yIMOqUmOMG5pH3drj2L/2Il3Aup4AM8+htM aGyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=IojNBkNZFfT+FpPnCjJB2Wc8qZH94/k8Mk9CGfKNBbM=; b=QhoittliyO1c7cV0nsfEMOfLcoPQdwM2NZNaXrWJChxxAlI2nX6ki+pCvtpr75QlAa BRZ+A/RjvviXUghjy3HZaVbjb0h0tAjnB7RMIGuJuhhlVZZ8Okn76iCiqYJ84Ai8U8Ht zEhV5ooWHNL7ig9PQlu3C7zIvvcmTSHIbTz12eVSox1WZ/uzjom4pqzFObwrFGIe6k1f Fz7XDtyJDoxWoybCoYfCP3I34mpYELYf7QAUaZ+GWHBUkOQ/w9+wTNqj+RQ/uichHvGq urStjCWjHiVs+FoNkkzKPOmdSVlMns1POcWtOcd0lqWO+hfJ82t3AU8t7l/ixUqM5ASD ayfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iEr5lh3+; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g8si328797oia.262.2020.01.24.12.59.05; Fri, 24 Jan 2020 12:59:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-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=@kernel.org header.s=default header.b=iEr5lh3+; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403859AbgAXSM4 (ORCPT + 99 others); Fri, 24 Jan 2020 13:12:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:50914 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390883AbgAXSM4 (ORCPT ); Fri, 24 Jan 2020 13:12:56 -0500 Received: from gmail.com (unknown [104.132.1.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 90C7E2075D; Fri, 24 Jan 2020 18:12:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579889575; bh=RR2pEgv3AfYWVyNbNfZQfKlvW//2Y1NRPRw8qbA0Nc8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=iEr5lh3+1n6E4bqy9kbISKA2APd/H/Ot2U3wNw1gGv4tc6Z3SKgWHNisVVAb4u2by KOXjFlBBuu7uIWet+S74eqkm/g+GRwFaImTrPKFc1MTlSkw3Rjdc5EeY7AEs8tvCWJ Ra31+bwo2ahiCxnm22pgLTpn+4fR34xcvM3ODq9c= Date: Fri, 24 Jan 2020 10:12:54 -0800 From: Eric Biggers To: Gao Xiang Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Alexander Viro , Daniel Rosenberg , Gabriel Krisman Bertazi Subject: Re: [PATCH] ext4: fix race conditions in ->d_compare() and ->d_hash() Message-ID: <20200124181253.GA41762@gmail.com> References: <20200124041234.159740-1-ebiggers@kernel.org> <20200124050423.GA31271@hsiangkao-HP-ZHAN-66-Pro-G1> <20200124051601.GB832@sol.localdomain> <20200124053415.GC31271@hsiangkao-HP-ZHAN-66-Pro-G1> <20200124054256.GC832@sol.localdomain> <20200124061525.GA2407@hsiangkao-HP-ZHAN-66-Pro-G1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200124061525.GA2407@hsiangkao-HP-ZHAN-66-Pro-G1> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Fri, Jan 24, 2020 at 02:15:31PM +0800, Gao Xiang wrote: > On Thu, Jan 23, 2020 at 09:42:56PM -0800, Eric Biggers wrote: > > On Fri, Jan 24, 2020 at 01:34:23PM +0800, Gao Xiang wrote: > > > On Thu, Jan 23, 2020 at 09:16:01PM -0800, Eric Biggers wrote: > > > > > > [] > > > > > > > So we need READ_ONCE() to ensure that a consistent value is used. > > > > > > By the way, my understanding is all pointer could be accessed > > > atomicly guaranteed by compiler. In my opinion, we generally > > > use READ_ONCE() on pointers for other uses (such as, avoid > > > accessing a variable twice due to compiler optimization and > > > it will break some logic potentially or need some data > > > dependency barrier...) > > > > > > Thanks, > > > Gao Xiang > > > > But that *is* why we need READ_ONCE() here. Without it, there's no guarantee > > that the compiler doesn't load the variable twice. Please read: > > https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE > > After scanning the patch, it seems the parent variable (dentry->d_parent) > only referenced once as below: > > - struct inode *inode = dentry->d_parent->d_inode; > + const struct dentry *parent = READ_ONCE(dentry->d_parent); > + const struct inode *inode = READ_ONCE(parent->d_inode); > > So I think it is enough as > > const struct inode *inode = READ_ONCE(dentry->d_parent->d_inode); > > to access parent inode once to avoid parent inode being accessed > for more time (and all pointers dereference should be in atomic > by compilers) as one reason on > > if (!inode || !IS_CASEFOLDED(inode) || ... > > or etc. > > Thanks for your web reference, I will look into it. I think there > is no worry about dentry->d_parent here because of this only one > dereference on dentry->d_parent. > > You could ignore my words anyway, just my little thought though. > Other part of the patch is ok. > While that does make it really unlikely to cause a real-world problem, it's still undefined behavior to not properly annotate a data race, it would make the code harder to understand as there would be no indication that there's a data race, and it would confuse tools that try to automatically detect data races. So let's keep the READ_ONCE() on d_parent. - Eric