From patchwork Tue Mar 7 14:34:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13163813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F7B4C678D4 for ; Tue, 7 Mar 2023 14:40:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230486AbjCGOk4 (ORCPT ); Tue, 7 Mar 2023 09:40:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230121AbjCGOid (ORCPT ); Tue, 7 Mar 2023 09:38:33 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20E67911E3; Tue, 7 Mar 2023 06:34:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=J8Wv/S8hZkGarl/vdU2yd/Mb7/z5z/afVPXh/Kxsm98=; b=H2ObeCoc8auj571tf6mE3PvlmW v7uz73zg80LebWXfbeT9jKI8uO4ZQk1c4nZSObeX4GMQXRAW3FIpO72F09zQcUyW9Gu/2P1tdR+93 1ESO4LKLuRhumNB1tdml7A9Nnd6uEgRxdBI/hG8RiWADv0eU5nGp1VbWcLp6oTNnvpbxT4mVH3iFp Vu595d8mCCTWXMGmmbZSMKqXizM5d2t1yV7AbxlbetK8PLWqpk1IXX4tDAaPIaXksi91tbkhJDk2o 37ihKioXLopdZ7z4w4ynbc2Hxi+aX5ZhXuesV+yV9p0Y8XOV7y5NhsvKVCLkkYlfDjbFgNm+L1+eP SLSenIbA==; Received: from [46.183.103.17] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pZYOc-000qBh-D3; Tue, 07 Mar 2023 14:34:31 +0000 From: Christoph Hellwig To: Andrew Morton , Matthew Wilcox , Hugh Dickins Cc: linux-afs@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, cluster-devel@redhat.com, linux-mm@kvack.org, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-nilfs@vger.kernel.org Subject: [PATCH 5/7] shmem: open code the page cache lookup in shmem_get_folio_gfp Date: Tue, 7 Mar 2023 15:34:08 +0100 Message-Id: <20230307143410.28031-6-hch@lst.de> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307143410.28031-1-hch@lst.de> References: <20230307143410.28031-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Use the very low level filemap_get_entry helper to look up the entry in the xarray, and then: - don't bother locking the folio if only doing a userfault notification - open code locking the page and checking for truncation in a related code block This will allow to eventually remove the FGP_ENTRY flag. Signed-off-by: Christoph Hellwig Acked-by: Hugh Dickins Signed-off-by: Hugh Dickins --- mm/shmem.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 3705437c5757ba..714ff3fb02a938 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1856,12 +1856,10 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, sbinfo = SHMEM_SB(inode->i_sb); charge_mm = vma ? vma->vm_mm : NULL; - folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0); + folio = filemap_get_entry(mapping, index); if (folio && vma && userfaultfd_minor(vma)) { - if (!xa_is_value(folio)) { - folio_unlock(folio); + if (!xa_is_value(folio)) folio_put(folio); - } *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); return 0; } @@ -1877,6 +1875,14 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, } if (folio) { + folio_lock(folio); + + /* Has the page been truncated? */ + if (unlikely(folio->mapping != mapping)) { + folio_unlock(folio); + folio_put(folio); + goto repeat; + } if (sgp == SGP_WRITE) folio_mark_accessed(folio); if (folio_test_uptodate(folio))