From patchwork Fri Dec 20 15:47:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13916937 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 51A7AE7718C for ; Fri, 20 Dec 2024 15:48:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB65F6B0095; Fri, 20 Dec 2024 10:48:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA0E86B0096; Fri, 20 Dec 2024 10:48:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF2846B0098; Fri, 20 Dec 2024 10:48:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9F4396B0095 for ; Fri, 20 Dec 2024 10:48:46 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4B91844BA6 for ; Fri, 20 Dec 2024 15:48:46 +0000 (UTC) X-FDA: 82915768908.25.90AD4F3 Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) by imf01.hostedemail.com (Postfix) with ESMTP id 4628D40011 for ; Fri, 20 Dec 2024 15:48:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=2KNrBsFN; dmarc=none; spf=pass (imf01.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.52 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734709708; a=rsa-sha256; cv=none; b=vg9YEVsvXZzMuv3OE6i9LJ3vW8gdUNHSiGLt/Lhw+y5UOYQ5/1TiS0h8xLK3FRnNS+dIvC lMt6QYbehpNikZe4SeAkJE114pB8CuS/zMJFacGG7S9T00quLjMgmnQxFCrmjFLSfD+O6A n0VTCLH/25dXch9PNGdF0A+77i1UeRE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=2KNrBsFN; dmarc=none; spf=pass (imf01.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.52 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734709708; 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=hj114LsQAeZlZx1JIhp9yNhhSBSN5VfiZ1DE7mn9M1E=; b=w4XPYYH1X81BuBtRyu9gnUdoCvkYK+wvq9Q6ThITh/H6Hv6MxSfN0vjzMM+3YAe4s7Al6o Qptgb6ZY1rSi0mR4xfryOIemFkeuQH90EgVII+9dQgBoP1GGjVIOXUwHfA0F04gBgskfBA inHjz1UGUpnk+91fXKkFK9k210o2jV8= Received: by mail-io1-f52.google.com with SMTP id ca18e2360f4ac-844ce213af6so72926639f.1 for ; Fri, 20 Dec 2024 07:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1734709723; x=1735314523; 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=hj114LsQAeZlZx1JIhp9yNhhSBSN5VfiZ1DE7mn9M1E=; b=2KNrBsFNL2EjEhRyP5iXp+7ZKHUhnwlSxvCeY/Ij1XPqjqNAiMS6y5WRHhKnUpHZgH gksDqeIt5UZ4yaTnxImykHPO+IK0zo3vX29w559ghX2LTRNyE48tOKGI/hwYaASCwWNb hQNDtDzVVlwPMRGuAW72tdtrSB01L/7//CKgnasf0GfP9Ka2BFaUuz1OE6gcj5fmyMTs fe9hy+jIoNwfD4ZR//DK60tmwjhly2BqjYVkAaMht6MPHypRhTk2jI5W2JZFsZ6N8vw0 wFidwEF47kXZFaXskYTsK1n1n5s4WoVA+pzVdkk1WRmtvDrqZNracWsUV+VzzlGMa/m5 tY6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734709723; x=1735314523; 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=hj114LsQAeZlZx1JIhp9yNhhSBSN5VfiZ1DE7mn9M1E=; b=WpxIdQ+Vmzq492qSJK7Imh6Fs/+Wd57uz7znJSkXRmJsktPLTi5MgXZPWC2dDdh4Ih dwwsrlsHveeUCyYbpo+RpkFDiAcF7VLQfMHFOhd0a7M5zfk3jSmijdcURHXtuKfS2P25 KwXkmtmd/ZCaP4QsNkGaSyNqMxnj9PjYP2lB0nwwThZanDTwK8JHKRcluaPMLoy7o2iR 8oxbZiXTvID67hCamehVdIh7MiXIhuQQVxXTpGvXYGGY5t/ucsC/P+9OYqrMZNg235tL MRYuhamBQpoRGFXmYike+bvBFOmMaa/i2yILinueUC1BMH4mpAA1MDXlpkgWTUMYmlc5 KbjQ== X-Gm-Message-State: AOJu0YxARO39RW+wH5uZ8FqqW/2OCGewtgeq5oLJA5h++pEUpbbuN7FX sxuZBshNL9EHAjsQVFqpZjeQ1lQCiVdY+6+OnnZG7rrhzuZwMozr6O203EcIhTzhMBFTjI3/JnC x X-Gm-Gg: ASbGncuy2M1SrEw3FWQ6CBJItLHerzF5Kf08GsSiFu1/USGYJkm4QkxlDKJch4c4/r5 dgdvAfRlAfUhGvwbqlRYcuGP8pDOdhIBmn5tYduXtWFAkPmZxkon5TL0PYBHdVu0Vfh4OdSZKqP njr0mdbgNBYyLTqxwO5wYJd6YDLlhnhO83GB7x4bH1OweBiTKMUFQK6ojAfzY7AS5njDyyvieQ1 saojheaTWyrFvTnYqDJsWI0QcVci7wzkVrMb4Vh/Idh1bScy1GrQ0AQxYWP X-Google-Smtp-Source: AGHT+IECOzR43hLv/D5wLt0YmDzKJdUpeT0f1YGhsBnnrmOZn/oym4rW0bRMY26iQGlNbtfBjUSBgg== X-Received: by 2002:a05:6e02:3683:b0:3a7:8720:9de5 with SMTP id e9e14a558f8ab-3c2d1aa27e8mr34276355ab.1.1734709723099; Fri, 20 Dec 2024 07:48:43 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4e68bf66ed9sm837821173.45.2024.12.20.07.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2024 07:48:42 -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, bfoster@redhat.com, Jens Axboe Subject: [PATCH 06/12] mm/truncate: add folio_unmap_invalidate() helper Date: Fri, 20 Dec 2024 08:47:44 -0700 Message-ID: <20241220154831.1086649-7-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241220154831.1086649-1-axboe@kernel.dk> References: <20241220154831.1086649-1-axboe@kernel.dk> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4628D40011 X-Stat-Signature: wg4a8j1b11rbpso159aj6aa3x1zyg4kx X-HE-Tag: 1734709698-82185 X-HE-Meta: U2FsdGVkX198n24IYjFcJRs9XUTO95iaG3jQb3VoDyh0zdc7lDvgAtg+BchfHhy+3NNBph3jJX5AY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wYMVznqoBp4G4P7OSF4+IIgc1NHD5jl8cTeTo1aU4r0xW1TBCfFn3w3qC8kt4QV3cGuu7xWKuKcnGJ4KqeyBFN5cD/uZXcBXUqVTjfPqEVRx2gbqiZk1D9gGHvgKkU6gqGmHfB23uZN2KgSqKXNOg+JF7C0P6hrsRASdSo/sssD4o2xUABT51dlobJV4cyWRU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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. Embed the old invalidate_complete_folio2() helper in there as well, as nobody else calls it. 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 --- mm/internal.h | 2 ++ mm/truncate.c | 53 +++++++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index cb8d8e8e3ffa..ed3c3690eb03 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -392,6 +392,8 @@ void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long addr, unsigned long end, struct zap_details *details); +int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, + gfp_t gfp); void page_cache_ra_order(struct readahead_control *, struct file_ra_state *, unsigned int order); diff --git a/mm/truncate.c b/mm/truncate.c index 7c304d2f0052..e2e115adfbc5 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -525,6 +525,15 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping, } EXPORT_SYMBOL(invalidate_mapping_pages); +static int folio_launder(struct address_space *mapping, struct folio *folio) +{ + if (!folio_test_dirty(folio)) + return 0; + if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL) + return 0; + return mapping->a_ops->launder_folio(folio); +} + /* * This is like mapping_evict_folio(), except it ignores the folio's * refcount. We do this because invalidate_inode_pages2() needs stronger @@ -532,14 +541,26 @@ EXPORT_SYMBOL(invalidate_mapping_pages); * shrink_folio_list() has a temp ref on them, or because they're transiently * sitting in the folio_add_lru() caches. */ -static int invalidate_complete_folio2(struct address_space *mapping, - struct folio *folio) +int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, + gfp_t gfp) { - if (folio->mapping != mapping) - return 0; + int ret; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); - if (!filemap_release_folio(folio, GFP_KERNEL)) + 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) + return ret; + if (folio->mapping != mapping) + return -EBUSY; + if (!filemap_release_folio(folio, gfp)) + return -EBUSY; spin_lock(&mapping->host->i_lock); xa_lock_irq(&mapping->i_pages); @@ -558,16 +579,7 @@ static int invalidate_complete_folio2(struct address_space *mapping, failed: xa_unlock_irq(&mapping->i_pages); spin_unlock(&mapping->host->i_lock); - return 0; -} - -static int folio_launder(struct address_space *mapping, struct folio *folio) -{ - if (!folio_test_dirty(folio)) - return 0; - if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL) - return 0; - return mapping->a_ops->launder_folio(folio); + return -EBUSY; } /** @@ -631,16 +643,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping, } 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);