From patchwork Tue Dec 3 15:31:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13892623 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 3DEC2E69EAF for ; Tue, 3 Dec 2024 15:33:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1ADA56B0095; Tue, 3 Dec 2024 10:32:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 161486B0098; Tue, 3 Dec 2024 10:32:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF0846B0099; Tue, 3 Dec 2024 10:32:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CFF156B0095 for ; Tue, 3 Dec 2024 10:32:57 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 870BDAE665 for ; Tue, 3 Dec 2024 15:32:57 +0000 (UTC) X-FDA: 82854040416.24.E8FE128 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by imf13.hostedemail.com (Postfix) with ESMTP id 0A4A720020 for ; Tue, 3 Dec 2024 15:32:40 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=UVyAIebX; spf=pass (imf13.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.171 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=1733239969; 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=DRb+N8QQBu8dVr+Sad3apJL8CLp+dfOfMlp0JKFijps=; b=YdUGpW63q30m8jDl7RwSvRYZVtF5fCbeUTvRZ+L8qqM9h5PA3osRhBPRCuyg/OAHoJpyKd cvYQ9jCzCoy5lYMsy2S0e6BnB/Z58tNl7zNe9rvGg4eHMSXRNr02XlFM4CWO+66xKm/Onz Bk2r3+BKiG8fUh6trEQHq71t9vsWTIY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=UVyAIebX; spf=pass (imf13.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.171 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733239969; a=rsa-sha256; cv=none; b=aO1UXutoxRMY4XL8+trGhDNEXgQn+s0z63gJr1KLj32zfHD40HsFxmGParb+K7sfvzaqH7 PtyDaMYxDFq1XZFFAepCJERwk/LQwblkEpaRrgPSB3CCG3zV2UapMasJAVL4cwHMWqrqji LRnx/+zlEY5J1zVBzteXn2VBWAunhSI= Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3ea4c550a3fso2766295b6e.2 for ; Tue, 03 Dec 2024 07:32:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1733239974; x=1733844774; 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=DRb+N8QQBu8dVr+Sad3apJL8CLp+dfOfMlp0JKFijps=; b=UVyAIebXLvrJq6nq92bE9/Xk1Q9FydCpQ0r9Lf284i6C80hI2ssrMUEflitf5zWqSh +M3sewANNc+j+nnfWXutjV+AzK36zwkeEtt63g9TGvSJqqQJTf/ZjIMWE+KyBkhZPF1e sw4Uroa2qtNUfzpqYitZPuCo43dVR75yDClzIdjCxvxWsgWnoc6KdkmBB3+Gs5n6D5If O87E4mJZ32CvgVnbvD+nImo+LXE35vWI7x4+Eux/sn7KsEKMsPw++py3a7Zv+8PV+4ku xKL4bTo3NDTh48kqjw7BeaqFDKn934YWhhiv7SnRWqfPEn11QDSrQMG9Zo4DzYMruaux mkjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733239974; x=1733844774; 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=DRb+N8QQBu8dVr+Sad3apJL8CLp+dfOfMlp0JKFijps=; b=iHjFH5jbiGD0LFKkVr18BmcnQ0zf1mAYvvxTHwWUg3f51paTjxVIoqWosSihY9Qx+2 Q9nG5Ay2WdVkiCRir/PG8UFPFWfaAKEfVAy3Lvhge3LDGKnO9Qhze89hgbugjOvBPbHW 8Nu0JeTDg/ocPcOtMVgdGl8Tr6BEfJTTOJZczntnCIKiXp0gMEyRRKlP+Z3CtlCbbFsD pTqSgSgROKCklFXIeUNC4cHc13/l733EjlB2Z3bNy2m4phY8TQMO4JPnPRyqnihZYuPU uSXHDGYpxX4J1FN4qburK3oVoP6TzFnULigyRdsdDy2/ajTTr4FJa/38nmSqCf/61uVs 7YYA== X-Gm-Message-State: AOJu0Yy2NXRI7v8Hjz2A37xjuRjOkZK8+SwxTHiaOHHsK00MJdqBIJQr +85Uf9+/jXT7viWEpNV9/HWtkqKFZ+bMl0ewvZqS6Vl5Ze/wytTZA1dqRzIG80R5VRaFlrnL2K4 e X-Gm-Gg: ASbGncsH43dmyJCL910BNpTmcztOme1nJ7DEg+8ESrcSC+cT5EAw5nRSGLj0sEZnYKV ukOAK6zA8qxkUItzZV5lYwZUkqfxRMtPct0QBnqLbmPEtgt0GX2p27yRyUd1IMSlHOnKG5/AsNh iV/pggEcxi58+i6XL83KnmLP1pfKCdZKuyYhWUqXcyQJE2xCLbzhFuG+Yxm739yfw//I6XyDjrg qkMg8MUQpT35Nuc93r9y3qrgASlP5czDaAZbnOKuAzl33L+kFUpHldkdY0= X-Google-Smtp-Source: AGHT+IGHTQ5LeGksRhzqMID0Kqh5YSoxN8Gylam5Ojf1AQcflOZOTzp2gKozYR7Jx08uJ7hl5NcXoA== X-Received: by 2002:a05:6808:3843:b0:3e8:1f5d:b7f8 with SMTP id 5614622812f47-3eae4edadc4mr3375768b6e.1.1733239974385; Tue, 03 Dec 2024 07:32:54 -0800 (PST) Received: from localhost.localdomain ([130.250.255.163]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3ea86036cbbsm2891878b6e.8.2024.12.03.07.32.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 07:32:53 -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: Tue, 3 Dec 2024 08:31:42 -0700 Message-ID: <20241203153232.92224-8-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241203153232.92224-2-axboe@kernel.dk> References: <20241203153232.92224-2-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0A4A720020 X-Stat-Signature: 6fuun7if97d6ji44qots39h5uji78w4q X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733239960-505125 X-HE-Meta: U2FsdGVkX1+oxr2xOPifHpd16hWOQOyHRLhV5PpdDNsU+kOOWdk/uMrUjxsIVqh7cKJ70GfkbXzyZ1OFVs994RVPYr21tOUL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jVUMMsRau05IECk9l2nJxgBt7z/yvNf2NER7bqcDW9+WokAkEjURqMz7XrGsBQ7tIhe2l63f89qblQ6LZW3ERs/qWW7sL3VcCYC7d5IxTGctR9upZaxonaoDKWLDt19/RBJwo3OKhIwUgT/Y60JVwiv6z/iAvtNIjgCXIu7fLT 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 72b03b37c265..f2d49dccb7c1 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 7c304d2f0052..c1dfddb1122a 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -533,12 +533,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); @@ -570,6 +570,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 @@ -629,18 +648,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);