From patchwork Tue Feb 13 09:37:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pankaj Raghav (Samsung)" X-Patchwork-Id: 13554784 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 3EEDAC48260 for ; Tue, 13 Feb 2024 09:37:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C023C6B0081; Tue, 13 Feb 2024 04:37:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB3876B0082; Tue, 13 Feb 2024 04:37:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A2EF86B0085; Tue, 13 Feb 2024 04:37:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8DB536B0081 for ; Tue, 13 Feb 2024 04:37:35 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1FB7E1202B3 for ; Tue, 13 Feb 2024 09:37:35 +0000 (UTC) X-FDA: 81786278070.30.5C3FC95 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by imf03.hostedemail.com (Postfix) with ESMTP id 6032F20003 for ; Tue, 13 Feb 2024 09:37:33 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=MssUC8Ev; spf=pass (imf03.hostedemail.com: domain of me@pankajraghav.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=me@pankajraghav.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707817053; a=rsa-sha256; cv=none; b=PfErESljeV3SHR4Fzb48ZBE0v2vwFv8rawv5DocZgM3Rv0fFT62dr06zoH6OHITh3MMQDB 6kn28alD5IxcfBN65H9s3n9/fF/CZaV/7MFKBgQlQEapAYk8E9b3V/cReXhURJ2QcskKsA p/wCbrXpLi/GJQEs03nJwwgbnlo2vWE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=MssUC8Ev; spf=pass (imf03.hostedemail.com: domain of me@pankajraghav.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=me@pankajraghav.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707817053; 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=w9ziATPUggPlhgislum4xTFLvTMOz0MhHtv1RpfgBms=; b=8CzoIpp42h+RIceH3uJk9x49fhpN7aaS3xOouZ1Jrt1yfvhaKHhKVmt+dMsCSsOjKhd+Fw 0PXlU2Q5t3eakuF6+C8egbjid3l1U9Hymq3stbHOjwp8Dg/98LURWoqragvpeqoOzDLP3Y p9E0bFUjDNatAd4SnOo+9SzmTrNlUH4= Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4TYx7Z18lWz9swL; Tue, 13 Feb 2024 10:37:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1707817050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w9ziATPUggPlhgislum4xTFLvTMOz0MhHtv1RpfgBms=; b=MssUC8EvZXGuZKgwffeP8hZBO3/nI8BUTOn+lQ37ROHXeTh/hED181KRtCHW6PELMLF0UE DEYwufaeT4jV1tuqEVGeSMTaAwMDle5ktiI2EYlKQPjJTQp/vX6/rgh4t8muYPfvrXpcjV hl09EmSpiojQVTrmt2iVgR6ncEJxJK4SW2dbrCqna7p8ayxhpiMmQpfs1UXv1lF/PX5iP4 m8xTmQzMzVSGJz1P+oVrwsTiU4/m/f9+f8Im/vqTieSDlf28XAOIkW0Qd8LCWRGkPeNIvu z48iIrv01XiHn1kxa2oJFgIu9wdVvN1Un6+RzVnIo1BkcnvB0SUBi8zi64m4MA== From: "Pankaj Raghav (Samsung)" To: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: mcgrof@kernel.org, gost.dev@samsung.com, akpm@linux-foundation.org, kbusch@kernel.org, djwong@kernel.org, chandan.babu@oracle.com, p.raghav@samsung.com, linux-kernel@vger.kernel.org, hare@suse.de, willy@infradead.org, linux-mm@kvack.org, david@fromorbit.com Subject: [RFC v2 03/14] filemap: use mapping_min_order while allocating folios Date: Tue, 13 Feb 2024 10:37:02 +0100 Message-ID: <20240213093713.1753368-4-kernel@pankajraghav.com> In-Reply-To: <20240213093713.1753368-1-kernel@pankajraghav.com> References: <20240213093713.1753368-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6032F20003 X-Stat-Signature: 7iorarodmkzo1mb11ixz8ejnc9aysc71 X-Rspam-User: X-HE-Tag: 1707817053-91802 X-HE-Meta: U2FsdGVkX1/rjWOjuVChVlHI+3vwRBgnbB2FGFv61TVmXoyqx6Wi42IUp33XzeE5Uq2aEGq8nj8NqCrOCW6fFZpb/fxuYF14ccO9Wt5sKQlzKF7py0ohLdTGXVe6xy2ep1BhMzNaCHvhbBGl7lxIF5M7SOH586t7VBFSBXVGAQ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uYpFUaSOd7TvceFzZc47jpbs11gVRPnRcb5B2D0YThwIWTS5IcWkYmnvFSS4rwjrrAq3fj+tRgOBnZh5efKyCmv3gqv564naygD40FbDAiPygWHRxZOUcUvINLrbGtYHggzVKKYdcp/mZ62gUWukyN4CN2Y6E650F7ON7SdBjalYi+yVjbCf 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: From: Pankaj Raghav filemap_create_folio() and do_read_cache_folio() were always allocating folio of order 0. __filemap_get_folio was trying to allocate higher order folios when fgp_flags had higher order hint set but it will default to order 0 folio if higher order memory allocation fails. As we bring the notion of mapping_min_order, make sure these functions allocate at least folio of mapping_min_order as we need to guarantee it in the page cache. Add some additional VM_BUG_ON() in page_cache_delete[batch] and __filemap_add_folio to catch errors where we delete or add folios that has order less than min_order. Signed-off-by: Pankaj Raghav Signed-off-by: Luis Chamberlain Reviewed-by: Hannes Reinecke Acked-by: Darrick J. Wong --- mm/filemap.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 323a8e169581..7a6e15c47150 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -127,6 +127,7 @@ static void page_cache_delete(struct address_space *mapping, struct folio *folio, void *shadow) { + unsigned int min_order = mapping_min_folio_order(mapping); XA_STATE(xas, &mapping->i_pages, folio->index); long nr = 1; @@ -135,6 +136,7 @@ static void page_cache_delete(struct address_space *mapping, xas_set_order(&xas, folio->index, folio_order(folio)); nr = folio_nr_pages(folio); + VM_BUG_ON_FOLIO(folio_order(folio) < min_order, folio); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); xas_store(&xas, shadow); @@ -277,6 +279,7 @@ void filemap_remove_folio(struct folio *folio) static void page_cache_delete_batch(struct address_space *mapping, struct folio_batch *fbatch) { + unsigned int min_order = mapping_min_folio_order(mapping); XA_STATE(xas, &mapping->i_pages, fbatch->folios[0]->index); long total_pages = 0; int i = 0; @@ -305,6 +308,7 @@ static void page_cache_delete_batch(struct address_space *mapping, WARN_ON_ONCE(!folio_test_locked(folio)); + VM_BUG_ON_FOLIO(folio_order(folio) < min_order, folio); folio->mapping = NULL; /* Leave folio->index set: truncation lookup relies on it */ @@ -846,6 +850,7 @@ noinline int __filemap_add_folio(struct address_space *mapping, int huge = folio_test_hugetlb(folio); bool charged = false; long nr = 1; + unsigned int min_order = mapping_min_folio_order(mapping); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio); @@ -896,6 +901,7 @@ noinline int __filemap_add_folio(struct address_space *mapping, } } + VM_BUG_ON_FOLIO(folio_order(folio) < min_order, folio); xas_store(&xas, folio); if (xas_error(&xas)) goto unlock; @@ -1847,6 +1853,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, fgf_t fgp_flags, gfp_t gfp) { struct folio *folio; + unsigned int min_order = mapping_min_folio_order(mapping); + unsigned int min_nrpages = mapping_min_folio_nrpages(mapping); + + index = round_down(index, min_nrpages); repeat: folio = filemap_get_entry(mapping, index); @@ -1886,7 +1896,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio_wait_stable(folio); no_page: if (!folio && (fgp_flags & FGP_CREAT)) { - unsigned order = FGF_GET_ORDER(fgp_flags); + unsigned int order = max(min_order, FGF_GET_ORDER(fgp_flags)); int err; if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping)) @@ -1914,8 +1924,13 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, err = -ENOMEM; if (order == 1) order = 0; + if (order < min_order) + order = min_order; if (order > 0) alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN; + + VM_BUG_ON(index & ((1UL << order) - 1)); + folio = filemap_alloc_folio(alloc_gfp, order); if (!folio) continue; @@ -1929,7 +1944,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, break; folio_put(folio); folio = NULL; - } while (order-- > 0); + } while (order-- > min_order); if (err == -EEXIST) goto repeat; @@ -2424,7 +2439,8 @@ static int filemap_create_folio(struct file *file, struct folio *folio; int error; - folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0); + folio = filemap_alloc_folio(mapping_gfp_mask(mapping), + mapping_min_folio_order(mapping)); if (!folio) return -ENOMEM; @@ -3682,7 +3698,8 @@ static struct folio *do_read_cache_folio(struct address_space *mapping, repeat: folio = filemap_get_folio(mapping, index); if (IS_ERR(folio)) { - folio = filemap_alloc_folio(gfp, 0); + folio = filemap_alloc_folio(gfp, + mapping_min_folio_order(mapping)); if (!folio) return ERR_PTR(-ENOMEM); err = filemap_add_folio(mapping, folio, index, gfp);