From patchwork Fri Dec 13 15:55:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13907403 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 6311DE77180 for ; Fri, 13 Dec 2024 15:56:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B91F86B0093; Fri, 13 Dec 2024 10:56:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AA71D6B0096; Fri, 13 Dec 2024 10:56:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9221D6B0098; Fri, 13 Dec 2024 10:56:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6C09F6B0093 for ; Fri, 13 Dec 2024 10:56:15 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 28B8A41B2B for ; Fri, 13 Dec 2024 15:56:15 +0000 (UTC) X-FDA: 82890387216.02.08E16EE Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) by imf05.hostedemail.com (Postfix) with ESMTP id 1B48010001C for ; Fri, 13 Dec 2024 15:55:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=QevZKSsp; spf=pass (imf05.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.177 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=1734105353; 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=S6x6BO6+6Zb8b9h/4S7enLTX5qkkGgQROhVyQCWWxzU=; b=a32l1Pxm/8xN4+xBUjZT9aMfjUxvZEDgEAzEM0sBz/zL8GUbxIDQU2no0PsEFOJgi04UuJ c6jnDwZwpnF1YxltyzqEYJ+llEbYZTe57OBq5sGITe7gaBk//boxCPVR5By34hElZ8mvSi erCEjvKn2mprTH+w2fWC0y/9tHjsd+4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=QevZKSsp; spf=pass (imf05.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.177 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734105353; a=rsa-sha256; cv=none; b=bg5ifqPQojZMCReOZefHR/jF+PfmwLu9XgRc2ErNXo+wV0vUIRP61HkWTuU1XcKNSLaJgD Rr1iacnb8QCDYN9uR31Buk0qbn72mMzQZUecSxQBJuYsb3iUN3Uu8gcghEMiwlr8ZtEwUm I80BOaqE5LFSB630NZToCb6J0Aiwg0c= Received: by mail-il1-f177.google.com with SMTP id e9e14a558f8ab-3a78b39034dso6048295ab.3 for ; Fri, 13 Dec 2024 07:56:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1734105372; x=1734710172; 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=S6x6BO6+6Zb8b9h/4S7enLTX5qkkGgQROhVyQCWWxzU=; b=QevZKSspdL7uoYImfZPT8G1IJ3/mj+DiVIpEPoLIYG6MVT4L9XQ1ssVE92ieY7ZhAv K9r+gSJj+r1zSnK5dJQb9bhw83qfsDDnrSXME2A6rIG4wtP7okQipCiccSSa9CfGaW/Q 5QbEafCjkf1k2yY1x4FiA0UcbPD6Bb9Gdj9dVeSAp2AJ2zffNZPLr11n1hKNqiO7VMpg HRZPd0GQb2UIi2bRIwRhwHTTSa3D9Tyfy5N77B87kGmrSklJN/g1/h5Yo1Oa7X+scoHR ADIsb/d6VSt8KgBjUZKGEqJNt0r62HKgRR/XovXPx2Wd86BmxFvrMSk3zoowmUIQcX1k FJWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734105372; x=1734710172; 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=S6x6BO6+6Zb8b9h/4S7enLTX5qkkGgQROhVyQCWWxzU=; b=VpG8lL09ZNbk+KirD9TUx3hbNhWvaHuVdQ8KY5lSVI0kLoCFA3wAEtJO4zrafkOZv8 GW69tYdpxa/0StMZoPAI06lu0DFNZRxCC/+gY6qEhVT1W3ZFp/jRXznzdc8uPTIfc4va 9dxg/JIxD9FY/9M+0RRbl+CNr8IHz/oVkmmMn04cnEQHwwNyk3i8T0KH0qQJ/VPzrSEJ QUpT6TX5CglNaSIGAEo5f7N4m/JXik63kMdpqnAX109ewie5kKEHtdBY1iRRNDkUYCLp dE00+6OhnoaACDdg1brokEJcEgYdOcxBoGBZAzy59MkVTyBVYgngWsfDy+QpNPPkjZTK fv0w== X-Gm-Message-State: AOJu0YwQ3d3HxWqKyHANpG3YlIRBneNBMhf8cd515HeISrad/osQModF jme2zJFbEQxBqQEMC3qFUY312zBAgiprCAGqSqID+97NNa/wyTKgpIZEX7scyw5sJkISGv0g7LY / X-Gm-Gg: ASbGncv0dg3d6SWmROI18yJzGRzNA6OMujiZb6eTH62keZf2PXNXBwvEdP3Ho1zAFuC 4ZZhV5uPgmUtDN0vljqB+mytG/E7DVNStoqF5Dfwf0BLYHH3x+X1XG/+XCkUIn9+qWszdPRNIh6 o6JXrHWAs+hBaW71NZtknlvFSjy3m1dYlAHzettetprcSdSFQADfjGUGDWO/YB19iBw7plwZqNV B1ZI06hKWIGkRs2DwqvooyG1UTB6Q3x7zP/SUhbnP7b1ESLFrkFwmW1Asih X-Google-Smtp-Source: AGHT+IEynku8b05ugapn+xW5MO4tP+/urn6SKqialSRpaetMXPe/mw9YxvkBEr2Hl0/nNihyBVcVWg== X-Received: by 2002:a05:6e02:164e:b0:3a7:7ded:521b with SMTP id e9e14a558f8ab-3aff800f100mr28298385ab.17.1734105371849; Fri, 13 Dec 2024 07:56:11 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3a9ca03ae11sm35258405ab.41.2024.12.13.07.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Dec 2024 07:56:11 -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/11] mm/truncate: add folio_unmap_invalidate() helper Date: Fri, 13 Dec 2024 08:55:20 -0700 Message-ID: <20241213155557.105419-7-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241213155557.105419-1-axboe@kernel.dk> References: <20241213155557.105419-1-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1B48010001C X-Rspamd-Server: rspam12 X-Stat-Signature: kpoq6466fk9yfbmqmcxfyqio4xgu99id X-Rspam-User: X-HE-Tag: 1734105323-916933 X-HE-Meta: U2FsdGVkX1/5G8vFCU56t+c2/Iy3CM19YBRy+oYESxcAHnj8+n0AWvMB+GgTMeh7aCfSOMJZV3h29p0S6SbNO6n0ofdEGBiLAANZMj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7Yb3AI1U1kLMEPF7+2dZSuy3bQ44MlQfUfUSvAHjaEpbZT8e5IegryWOUxN6o8MKL5ag2E/t4QArYXZ+m86h4jonieZgJNJASt2fPbH3kjKDEsiqQ4MszxG6D3ZPDLBU6DA6qN9d5fwwRkWLVwhqmFgLhPsVR0HbeFuffd 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. 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 | 54 ++++++++++++++++++++++++++------------------------- 2 files changed, 30 insertions(+), 26 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..7b6f4399319f 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; } /** @@ -629,18 +641,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);