Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4500837imm; Fri, 18 May 2018 06:13:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrvs+qvPZgYpLTgSj7whPlQOgzo6UfqjNlj8EQjU5LC7VPvzdO4lxW/xUArPoTkhxprOIiO X-Received: by 2002:a17:902:8d8c:: with SMTP id v12-v6mr9437874plo.366.1526649215312; Fri, 18 May 2018 06:13:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526649215; cv=none; d=google.com; s=arc-20160816; b=JYfmpYcpYDHRq7gzrDi8L1qBr0Un0ifjvTJjczcYE6Lj7hvMp9mOiryoMnlcFf0E01 r2dc3lsdn6LF4mWOc9tmtQsZHeVeNbdrUG1gmuUseU4+K5+naoqA02j1WZyBuQIqQsfm VVcBHLqyHZXoF7uxvYE4ZkXfm9e0xWfjFaCl0x0qF2vJkRoY4roy40KW0QDkUSYrbJWu PTK4/bmmkE93+903KBiX8z0mg442vCiV4zAOx1u4WOrfmi6pLJDHQ2EKnVG1pO4ylfXS gPgMLwvtR0wUIP/FZBI11YTBVnNhGaQWjH62ELK9bxqFIAS3xA07ttbosKPCq1O0Z45Q vI5g== 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:arc-authentication-results; bh=NXPnn7Sc6CYpVJ12GCvfNWacOYWr1NGl0CLTGcPWxdw=; b=ODb3jUi2/mJMLPfZhBk6qnrdsDPGjny5avRomVAozJE7u7znWISLx7Rnbl6IvOUNGZ sZKETPE04bQkWUTi7j897R10DTwhy4VjFgJUEnFoXBcvTA8CTe5jp//6aRuX9gyqnKGA PlARRO5vPt23mgsdI7R7EJGyVDKucx7tIlBKB9UxpE4getBSGLAcvdV/aI5CdX/RXwia nPlpl4cdPBrcKLl7L8HuRl+hYPRmFrY6lMyyv7F50ec04wPKOvT+/J29wb0iHhynFv01 2Ss28AeBoujAtYqgfJu5dbfZhS4zLyrhuG4aeCiOGCS5l91q6eCeB2tJ3k+2tdFVe3fh Lm1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ZGomeoDb; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h5-v6si1007093pgs.201.2018.05.18.06.13.20; Fri, 18 May 2018 06:13:35 -0700 (PDT) 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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ZGomeoDb; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752060AbeERNNK (ORCPT + 99 others); Fri, 18 May 2018 09:13:10 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:45698 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750957AbeERNNI (ORCPT ); Fri, 18 May 2018 09:13:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.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:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=NXPnn7Sc6CYpVJ12GCvfNWacOYWr1NGl0CLTGcPWxdw=; b=ZGomeoDbV2iZ7eisIdIyYh6kD dDHsNMor0YtbPW8eXRT2EHDW79dfKUA6y2hw3xXxbU0iU39fmLbOBLQ2SFgInyEWBsMgH3dbfujh6 i13nkx3qDZj0lgen/X2LFX/9gIdNsd+L94Ncb/K/4++doiCXldKLZNikfKE80Q4hHl2YDhprHPzEn it4ow9LYkiVgS+xg+ICvSFwdHGKMKN/yjVpTIH50CDw7JF8FhxcX3MqE8FYA51NYDo/HJx1PELnnL dBYoiRp9KGT5L8xIEgYz1qUA7AVm2Uk9bdwTCof8aNwY4/IB0lQl3mVS6yR02Qn47bEA9xQNGr5AZ 6L6aKhOiA==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fJfBy-0006Yp-98; Fri, 18 May 2018 13:13:06 +0000 Date: Fri, 18 May 2018 06:13:06 -0700 From: Matthew Wilcox To: Kent Overstreet Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrew Morton , Dave Chinner , darrick.wong@oracle.com, tytso@mit.edu, linux-btrfs@vger.kernel.org, clm@fb.com, jbacik@fb.com, viro@zeniv.linux.org.uk, peterz@infradead.org Subject: Re: [PATCH 01/10] mm: pagecache add lock Message-ID: <20180518131305.GA6361@bombadil.infradead.org> References: <20180518074918.13816-1-kent.overstreet@gmail.com> <20180518074918.13816-3-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180518074918.13816-3-kent.overstreet@gmail.com> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 18, 2018 at 03:49:00AM -0400, Kent Overstreet wrote: > Add a per address space lock around adding pages to the pagecache - making it > possible for fallocate INSERT_RANGE/COLLAPSE_RANGE to work correctly, and also > hopefully making truncate and dio a bit saner. (moving this section here from the overall description so I can reply to it in one place) > * pagecache add lock > > This is the only one that touches existing code in nontrivial ways. > The problem it's solving is that there is no existing general mechanism > for shooting down pages in the page and keeping them removed, which is a > real problem if you're doing anything that modifies file data and isn't > buffered writes. > > Historically, the only problematic case has been direct IO, and people > have been willing to say "well, if you mix buffered and direct IO you > get what you deserve", and that's probably not unreasonable. But now we > have fallocate insert range and collapse range, and those are broken in > ways I frankly don't want to think about if they can't ensure consistency > with the page cache. ext4 manages collapse-vs-pagefault with the ext4-specific i_mmap_sem. You may get pushback on the grounds that this ought to be a filesystem-specific lock rather than one embedded in the generic inode. > Also, the mechanism truncate uses (i_size and sacrificing a goat) has > historically been rather fragile, IMO it might be a good think if we > switched it to a more general rigorous mechanism. > > I need this solved for bcachefs because without this mechanism, the page > cache inconsistencies lead to various assertions popping (primarily when > we didn't think we need to get a disk reservation going by page cache > state, but then do the actual write and disk space accounting says oops, > we did need one). And having to reason about what can happen without > a locking mechanism for this is not something I care to spend brain > cycles on. > > That said, my patch is kind of ugly, and it requires filesystem changes > for other filesystems to take advantage of it. And unfortunately, since > one of the code paths that needs locking is readahead, I don't see any > realistic way of implementing the locking within just bcachefs code. > > So I'm hoping someone has an idea for something cleaner (I think I recall > Matthew Wilcox saying he had an idea for how to use xarray to solve this), > but if not I'll polish up my pagecache add lock patch and see what I can > do to make it less ugly, and hopefully other people find it palatable > or at least useful. My idea with the XArray is that we have a number of reserved entries which we can use as blocking entries. I was originally planning on making this an XArray feature, but I now believe it's a page-cache-special feature. We can always revisit that decision if it turns out to be useful to another user. API: int filemap_block_range(struct address_space *mapping, loff_t start, loff_t end); void filemap_remove_block(struct address_space *mapping, loff_t start, loff_t end); - After removing a block, the pagecache is empty between [start, end]. - You have to treat the block as a single entity; don't unblock only a subrange of the range you originally blocked. - Lookups of a page within a blocked range return NULL. - Attempts to add a page to a blocked range sleep on one of the page_wait_table queues. - Attempts to block a blocked range will also sleep on one of the page_wait_table queues. Is this restriction acceptable for your use case? It's clearly not a problem for fallocate insert/collapse. It would only be a problem for Direct I/O if people are doing subpage directio from within the same page. I think that's rare enough to not be a problem (but please tell me if I'm wrong!)