From patchwork Thu Nov 14 15:25:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13875215 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA224D68B31 for ; Thu, 14 Nov 2024 15:28:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE3F36B009A; Thu, 14 Nov 2024 10:28:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6A986B009B; Thu, 14 Nov 2024 10:28:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBC1E6B009C; Thu, 14 Nov 2024 10:28:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AB6046B009A for ; Thu, 14 Nov 2024 10:28:13 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4E028C0BB8 for ; Thu, 14 Nov 2024 15:28:13 +0000 (UTC) X-FDA: 82785080784.04.B19DFD9 Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) by imf26.hostedemail.com (Postfix) with ESMTP id DDEC6140021 for ; Thu, 14 Nov 2024 15:27:37 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=sM7OhIVC; spf=pass (imf26.hostedemail.com: domain of axboe@kernel.dk designates 209.85.160.41 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731597915; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zZIb5wvnGXtO89gOBSt/Tk4W0KJQpsBnaIBuXsS72Rg=; b=7Qk05XueoGfQxpSKfJOSVdzW6oRqAjAYsF3JtiEb0Dr8vS1n1EdogkmPHzTOpHulo4ghHe sOiQM6IJIYYgr0Cv58RG61cKqmW7L8iytKQZUfOzgmQ82z1acixfb/8AXbjKUIOo9War8p NNb1sQmux4UDUc7C1OkY5HWYJKKkXlw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=sM7OhIVC; spf=pass (imf26.hostedemail.com: domain of axboe@kernel.dk designates 209.85.160.41 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731597915; a=rsa-sha256; cv=none; b=LJUKdVGbK5I+iO+P7wHIM/YHpti6L4lF9kQeVmtKl2fcEiILs2nEe2ItSSRfDhaa5fiepG s4yyNRpErd9VjeWwc/y9lDcRbs04mjtMyRO1gXMecWSNG6PpXciypC6ZzWTqTYBKW0pTXd /y2/WMkBL5dIIoZiqckzuK4LdeEVXlY= Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-2951f3af3ceso447830fac.1 for ; Thu, 14 Nov 2024 07:28:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1731598090; x=1732202890; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zZIb5wvnGXtO89gOBSt/Tk4W0KJQpsBnaIBuXsS72Rg=; b=sM7OhIVC526DIs4la+8Gd2O4X8z3MplH75exncxqvQBBNkequ2/itF0bsingGtl5yD f2oa45xGfnudKeQdMgtsWSf5VC+AR6TJiNXONXs4Wn0tgH5NO5rBmc80cqvy1ohFMDP/ mrWD0Yd94KAXjvArg0curgULaW5T7LlM8ogEpjwkwkzEtAO+Uf7WhQPfab/u4pPLsBsK isBpNG3gjq1QOYKe1EVxrNzoPFdHAhUpNOWC6ymcuRonIV9u88Voog/umnsXfrmJ2quT xVLQPOYCWMEE8r68gu7JRK7WvIzxOqgU2sOtSdtub6JMBpviDhu21U/f5PIrYxFImDaY PAzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731598090; x=1732202890; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zZIb5wvnGXtO89gOBSt/Tk4W0KJQpsBnaIBuXsS72Rg=; b=WZeTVlw3T33Yai83S7sHBg+pKzNN2jNK2MhXNrIktuB8CAvRqSCGnJvHSRgr5qY9pg 0ldHKM4cTaMYh0F3mQLG/HGfI3To0XQXC9Z+Qp8IfoIqPDADPhahIioIoLVdcZgEofEO o7akkDJSjrrmjD1Wr5TQ70KqHnzuXxy5EuZgspKbazQSc65vuc/dhaWolOBo8aeuC08a 0RkUaeHrmJSdtw0Rv1DnRjnBpf3mkjSVx8ztVHGXgqjcoDnyJT/cHKYnSX8P8+i0xfVf JRf+TjiGsvthZ8AZtnwEJllby3inEQ68GtGGLTNUA764244gqcZxAJVGLch407027dZF qL2A== X-Gm-Message-State: AOJu0Yyv7RhM1zTuZoZwX6XgMS93jlG6zZoc6tDFymQceTYCileFyNDG mUw/WuDLcJ+vsAMozOSyZcclb97PwR+1hTNtuTh+7cx/z16wsH87RJfK1JAxk4i5ecYVINU5hgc rc+A= X-Google-Smtp-Source: AGHT+IF0BwKZuS0hD2QtJaajxUzrtQ2SwlZch/PHXd4jMsGrqLjoDD1qWu/LCzOrVtrsjrS2cDAn1Q== X-Received: by 2002:a05:6870:910d:b0:277:c027:1960 with SMTP id 586e51a60fabf-29610376b59mr2544315fac.25.1731598090064; Thu, 14 Nov 2024 07:28:10 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5eea026eb41sm368250eaf.39.2024.11.14.07.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 07:28:09 -0800 (PST) From: Jens Axboe To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: hannes@cmpxchg.org, clm@meta.com, linux-kernel@vger.kernel.org, willy@infradead.org, kirill@shutemov.name, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, bfoster@redhat.com, Jens Axboe Subject: [PATCH 06/17] mm/truncate: add folio_unmap_invalidate() helper Date: Thu, 14 Nov 2024 08:25:10 -0700 Message-ID: <20241114152743.2381672-8-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241114152743.2381672-2-axboe@kernel.dk> References: <20241114152743.2381672-2-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: DDEC6140021 X-Stat-Signature: q1bmf99bqog813mr8q5ay16ocjp116ds X-Rspam-User: X-HE-Tag: 1731598057-496187 X-HE-Meta: U2FsdGVkX18bCI0F5cl2GpNJhPro18czc9V1HGFzlmBgaprBnDhNSiIaHAwSAFzNre7iPNwrZQgdXecr8o9WQSYK8/2Sxh31UiAtBkrajMZPTkDpW1E9riWAqC6z55MqTeQDYwnMOg5OtE+aEU3Umm8CLUmND4vAn+8VN6mQyNWysvk2d8nHM/0k/MDehHWeAoc74YjamdMwx025kQiZCAc5c1uxi1aZw2VjQ4q+ayrqxuwtxk9VfYg8qJeX8gzYNDlyR9wGdyhfhRbazmsMcQ2PdTaPTDihTi1F8JRbsCq2MC9wQO9mdnx0ob9feJXJNaQWL81x0ok2LhGpP0E5hKKeqbt6KEbFuNs/pd13q+AvhIhczcf1JoMaexKKXla8h0E3toGu1xRhQCo6VoEZImKav6Tbi9wRS1MJ66egGET78AgOUI3rVQNrnq/oxvxObmCoCMnbBFZ3MEtuloBeNaYCFmldpJER6KsDV4vYyzQlqsqLgpbQ4fLgSAAfeQzjvVfx2uve00I22BOl0oVXyyB9VGQnbZznpg+riThEMp3/BmO0SMUa+E/DGXknGdw6ChUwIHi8Cmqchx3S5gcLdqewNVnHwy1uSMaSofkeYR482SJCaXmUvJYtqGzL/DlrPA0N5ovyg4sQFTn5M6Nt4UjiRhxEUOpBNW9mah4BNYWa29W2Vj+bkTi3G0NVN6+W++byUTxi/yVFFblJXTRRHCj5R/BnWGnPR3SrAVkKP0uAUd01VRZUKHeRh/EXaEjCONttRWslVS/8dlOd9ERHW83CDiShZUjuv3WgiXToW1p1tV2OjEc/owkiY87z1TBaPhU4YlN5xGZtQsL23hcigqpMyqm8nELdqaN/pgrVo94I3U0vqhsm10Z2wgh5bSvZusIdDTnG1WTxb7l/a54C6QL8TgeaJkwInXdSA74auu9QOMMfWNLmYyQ42szIAcH44v5kjGMrZ6bXMntlkPq SPwRpsqb ZW2b8cY80lSRLtE6aNOGhlT2N53PMETTxyOnAOh6uE+XW/8eoHPWqLugNq4yUxwOG/bnRJvqtczXz8QL/3EOy2GiyynCvJvqe6VdwjnvUSrzPxHC+aU2/ml29Bgti9MF8XWffTKfZFtNYa48UcPZ1uQj8QNUS1nNQsdFk6HyN9qYsx7l2trFIqGM3+BIQOgI8PhOR3Jcpahcnh3ZWJLNh2AcNoeGUwD8P/5Vem1QlvLn+hUb93Q+tGd/JgD97azfctfnwsQQh8BN3K+4HRneauS/O+WXegzQjImRaWWgLYUip2h8Oy8wy9QTnpR7ot95jVLaINC/6k/kyng50KHhD8NjsFqNjLd9fUD2q 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: List-Subscribe: List-Unsubscribe: Add a folio_unmap_invalidate() helper, which unmaps and invalidates a given folio. The caller must already have locked the folio. Use this new helper in invalidate_inode_pages2_range(), rather than duplicate the code there. In preparation for using this elsewhere as well, have it take a gfp_t mask rather than assume GFP_KERNEL is the right choice. This bubbles back to invalidate_complete_folio2() as well. Signed-off-by: Jens Axboe --- include/linux/pagemap.h | 2 ++ mm/truncate.c | 35 ++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 8afacb7520d4..d55bf995bd9e 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -34,6 +34,8 @@ int kiocb_invalidate_pages(struct kiocb *iocb, size_t count); void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count); int filemap_invalidate_pages(struct address_space *mapping, loff_t pos, loff_t end, bool nowait); +int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, + gfp_t gfp); int write_inode_now(struct inode *, int sync); int filemap_fdatawrite(struct address_space *); diff --git a/mm/truncate.c b/mm/truncate.c index 0668cd340a46..6ea16c537534 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -547,12 +547,12 @@ EXPORT_SYMBOL(invalidate_mapping_pages); * sitting in the folio_add_lru() caches. */ static int invalidate_complete_folio2(struct address_space *mapping, - struct folio *folio) + struct folio *folio, gfp_t gfp_mask) { if (folio->mapping != mapping) return 0; - if (!filemap_release_folio(folio, GFP_KERNEL)) + if (!filemap_release_folio(folio, gfp_mask)) return 0; spin_lock(&mapping->host->i_lock); @@ -584,6 +584,25 @@ static int folio_launder(struct address_space *mapping, struct folio *folio) return mapping->a_ops->launder_folio(folio); } +int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, + gfp_t gfp) +{ + int ret; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + + if (folio_test_dirty(folio)) + return 0; + if (folio_mapped(folio)) + unmap_mapping_folio(folio); + BUG_ON(folio_mapped(folio)); + + ret = folio_launder(mapping, folio); + if (!ret && !invalidate_complete_folio2(mapping, folio, gfp)) + return -EBUSY; + return ret; +} + /** * invalidate_inode_pages2_range - remove range of pages from an address_space * @mapping: the address_space @@ -641,18 +660,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping, folio_unlock(folio); continue; } - VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio); folio_wait_writeback(folio); - - if (folio_mapped(folio)) - unmap_mapping_folio(folio); - BUG_ON(folio_mapped(folio)); - - ret2 = folio_launder(mapping, folio); - if (ret2 == 0) { - if (!invalidate_complete_folio2(mapping, folio)) - ret2 = -EBUSY; - } + ret2 = folio_unmap_invalidate(mapping, folio, GFP_KERNEL); if (ret2 < 0) ret = ret2; folio_unlock(folio);