From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 397F7C4338F for ; Sun, 15 Aug 2021 20:24:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 446B861359 for ; Sun, 15 Aug 2021 20:24:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 446B861359 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 4B6606B0071; Sun, 15 Aug 2021 16:24:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 465C86B0072; Sun, 15 Aug 2021 16:24:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32DC48D0001; Sun, 15 Aug 2021 16:24:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0234.hostedemail.com [216.40.44.234]) by kanga.kvack.org (Postfix) with ESMTP id 1624D6B0071 for ; Sun, 15 Aug 2021 16:24:33 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 9EEEC8249980 for ; Sun, 15 Aug 2021 20:24:32 +0000 (UTC) X-FDA: 78478442784.07.90683C9 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf29.hostedemail.com (Postfix) with ESMTP id 6E06A9013A68 for ; Sun, 15 Aug 2021 20:24:31 +0000 (UTC) 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=3hjLDBVIUuEhPE/QTKjTt6SD+T+B8p+I9/1rjQ1tlWw=; b=JDrV3hdxAMiAk9A1PYJebdSnnM RiAvvc8JofuBVJKF4j5mLzGMSH7jK+Q8rUXkcvhNlmzDfNZUc3o7TGtRIuBR15uXxXt9NwmgT+zLi FJ3YKjYHI/9NEbpLtnqWN4DcDB+2xWe9v0l6MeBYJVzmVr/rCsD8R3WXChBNVxFvmLWizNo9E3AG8 BmvcphhW7VruAqXA9FgoD/mE+M1zFIEbQxTBGcxtXYeE33MrS4bjYZTJPUNR8KrEmO+28/u7Bex8k xu8RymFIgm77ppFY4cpmmwjqA2QtqjTBfOnQwaGJpBWvuFqcgFDB3EOnzt/EERoXRWa1xfujDRVqB O0beq22w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mFMfl-000c6H-SK; Sun, 15 Aug 2021 20:24:07 +0000 Date: Sun, 15 Aug 2021 21:23:57 +0100 From: Matthew Wilcox To: Vlastimil Babka Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v14 078/138] mm/filemap: Add folio_mkwrite_check_truncate() Message-ID: References: <20210715033704.692967-1-willy@infradead.org> <20210715033704.692967-79-willy@infradead.org> <658f52db-47a1-606d-f19a-a666f5817ad9@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <658f52db-47a1-606d-f19a-a666f5817ad9@suse.cz> Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=JDrV3hdx; dmarc=none; spf=none (imf29.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Stat-Signature: x51iceiqfrefd93pw1ospdqxqqbn58xk X-Rspamd-Queue-Id: 6E06A9013A68 X-Rspamd-Server: rspam05 X-HE-Tag: 1629059071-21589 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Aug 12, 2021 at 07:08:33PM +0200, Vlastimil Babka wrote: > > +/** > > + * folio_mkwrite_check_truncate - check if folio was truncated > > + * @folio: the folio to check > > + * @inode: the inode to check the folio against > > + * > > + * Return: the number of bytes in the folio up to EOF, > > + * or -EFAULT if the folio was truncated. > > + */ > > +static inline ssize_t folio_mkwrite_check_truncate(struct folio *folio, > > + struct inode *inode) > > +{ > > + loff_t size = i_size_read(inode); > > + pgoff_t index = size >> PAGE_SHIFT; > > + size_t offset = offset_in_folio(folio, size); > > + > > + if (!folio->mapping) > > The check in the page_ version is > if (page->mapping != inode->i_mapping) > > Why is the one above sufficient? Oh, good question! We know that at some point this page belonged to this file. The caller has a reference on it (and at the time they acquired a refcount on the page, the page was part of the file). The caller also has the page locked, but has not checked that the page is still part of the file. That's where we come in. The truncate path looks up the page, locks it, removes it from i_pages, unmaps it, sets the page->mapping to NULL, unlocks it and puts the page. Because the folio_mkwrite_check_truncate() caller holds a reference on the page, the truncate path will not free the page. So there are only two possibilities for the value of page->mapping; either it's the same as inode->i_mapping, or it's NULL. Now, maybe this is a bit subtle. For robustness, perhaps we should check that it's definitely still part of this file instead of checking whether it is currently part of no file. Perhaps at some point in the future, we might get the reference to the page without checking that it's still part of this file. Opinions?