Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp96254pxk; Wed, 16 Sep 2020 20:09:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxj0xzOcZ2hI/+PgEyNrYvGHw9Pz89IDdDC68DDutTH5kSq86pKc86HOG7JpaSjm/gtM9Nv X-Received: by 2002:a50:88c6:: with SMTP id d64mr6126600edd.141.1600312185866; Wed, 16 Sep 2020 20:09:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600312185; cv=none; d=google.com; s=arc-20160816; b=tob6bB6G+luwJGmAOOfUSl8o/kfNHpDyqzgjBqTEMiX34Hw/ZN2E2xt52gLnn8SX94 7+0HWb7OF4IbR5C88xtI2DgAsNnTG0FNCo4lmo54cWiMX6XswdRCmaz1oxymMmQ1eCOB eqPhzMvJ0MRm5W1lf6D0UHsYMwLKSq0KJ5127p5ZUYjT2vY/NGMCmtjo61bkp3mijkI9 pYe9j1uK6qAexep6l6h310sMwXtgKSMx/iwvrUdmU4/74tEq2mxNmj06py6uA74ig9Tm C+XUtRMC8paItrhgHK46qk/qrQXW3od46xU+bn6dlXLGLaBZFsIOr6B9tCKUwgyUWW8A R/ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=uMnEgq9vaCRYsRSBlvAbbXAo3XKw/rA3rHucz8CqYPs=; b=yMOvw8iu3nyjZ2RHmcIzb3LkxG1DmOXwlFWLgBqp2hN32alsGeuAOR6QT7TIrbLXHW 2XenPnm8xQfCUlpZa6OHb1j3lgLamDHprsgme58UR8St2k0SVL2NPLBY956u8aIP6x8T 2TYIsFKXbffjDMdb1DX8MYAjBi+qAA3xE/4Gln+4RptjxEdPjMfOIhHG8OMn93ekEQlx +D9EmmRnaKqPtHFvUhCsc0xSiJDKvlWcWuty+6iKdMS3wGUZMxcRy4u+oY3+nXnITVq0 eemSl/e4TEzeFpUCGiS2qQWO4KHCuJp/5bNQyNjnp7WNyHcOHAztu0lI6CI6Imiw6yVn YsKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=tsk5XKZM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p8si13410851edi.48.2020.09.16.20.09.21; Wed, 16 Sep 2020 20:09:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=tsk5XKZM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726149AbgIQDIS (ORCPT + 99 others); Wed, 16 Sep 2020 23:08:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726047AbgIQDIL (ORCPT ); Wed, 16 Sep 2020 23:08:11 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3599EC061354; Wed, 16 Sep 2020 20:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=uMnEgq9vaCRYsRSBlvAbbXAo3XKw/rA3rHucz8CqYPs=; b=tsk5XKZMdogbEKddhS+kYKFpSD tILj0/PIxWt27O5LKM5LnkW0dVmd2e+wSOuIwwD/69G1QTC0K9EZq/JtTGUiu5IJQdbq8E5/9vX/5 Z4nAYPlyd3jGV/ixISlEA7eQg04hLVeQo8y8AyhP2/sQczxD/DKca/SQK0h1Xz+cidtDCb3TKeHWI NcAVLTA5afy+MX63+apM1DVpm5l3NmZAKe8s8l+9gymmzr2KzdJ5+DL8szLk64FlPa77tf4peRqyT mFvJ+NQZGfSNvdy7S+pd84wgCtbEXDVhZpnujB3NB666Vc8G8mRJzth09++ywm/yC2Qk8joI2zn7U G9IawJkw==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kIkAY-0006Co-3p; Thu, 17 Sep 2020 03:01:10 +0000 Date: Thu, 17 Sep 2020 04:01:10 +0100 From: Matthew Wilcox To: Dave Chinner Cc: Jan Kara , Amir Goldstein , Andreas Gruenbacher , Theodore Tso , Martin Brandenburg , Mike Marshall , Damien Le Moal , Jaegeuk Kim , Qiuyang Sun , linux-xfs , linux-fsdevel , Linux MM , linux-kernel , Linus Torvalds , "Kirill A. Shutemov" , Andrew Morton , Al Viro , nborisov@suse.de Subject: Re: More filesystem need this fix (xfs: use MMAPLOCK around filemap_map_pages()) Message-ID: <20200917030110.GP5449@casper.infradead.org> References: <20200623052059.1893966-1-david@fromorbit.com> <20200916155851.GA1572@quack2.suse.cz> <20200917014454.GZ12131@dread.disaster.area> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200917014454.GZ12131@dread.disaster.area> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 17, 2020 at 11:44:54AM +1000, Dave Chinner wrote: > So.... > > P0 p1 > > hole punch starts > takes XFS_MMAPLOCK_EXCL > truncate_pagecache_range() ... locks page ... > unmap_mapping_range(start, end) > > > do_fault_around() > ->map_pages > filemap_map_pages() ... trylock page fails ... > page mapping valid, > page is up to date > maps PTEs > > truncate_inode_pages_range() > truncate_cleanup_page(page) > invalidates page > delete_from_page_cache_batch(page) > frees page > > > That doesn't seem good to me. > > Sure, maybe the page hasn't been freed back to the free lists > because of elevated refcounts. But it's been released by the > filesystem and not longer in the page cache so nothing good can come > of this situation... > > AFAICT, this race condition exists for the truncate case as well > as filemap_map_pages() doesn't have a "page beyond inode size" check > in it. However, exposure here is very limited in the truncate case > because truncate_setsize()->truncate_pagecache() zaps the PTEs > again after invalidating the page cache. > > Either way, adding the XFS_MMAPLOCK_SHARED around > filemap_map_pages() avoids the race condition for fallocate and > truncate operations for XFS... > > > As such it is a rather > > different beast compared to the fault handler from fs POV and does not need > > protection from hole punching (current serialization on page lock and > > checking of page->mapping is enough). > > That being said I agree this is subtle and the moment someone adds e.g. a > > readahead call into filemap_map_pages() we have a real problem. I'm not > > sure how to prevent this risk... > > Subtle, yes. So subtle, in fact, I fail to see any reason why the > above race cannot occur as there's no obvious serialisation in the > page cache between PTE zapping and page invalidation to prevent a > fault from coming in an re-establishing the PTEs before invalidation > occurs. > > Cheers, > > Dave. > -- > Dave Chinner > david@fromorbit.com >