From patchwork Fri Jul 26 11:59:48 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: 13742701 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 A5380C3DA7F for ; Fri, 26 Jul 2024 12:00:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 011876B00A9; Fri, 26 Jul 2024 08:00:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F03B26B00AA; Fri, 26 Jul 2024 08:00:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7F696B00AB; Fri, 26 Jul 2024 08:00:22 -0400 (EDT) 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 ADB746B00A9 for ; Fri, 26 Jul 2024 08:00:22 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1274A1617FF for ; Fri, 26 Jul 2024 12:00:22 +0000 (UTC) X-FDA: 82381761084.24.5FD85AD Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by imf01.hostedemail.com (Postfix) with ESMTP id 9C0154000D for ; Fri, 26 Jul 2024 12:00:19 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=FT5iL9QN; spf=pass (imf01.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721995195; 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=deQPnnwd6TDzRj/A268eQqMo7O4raj6JQhQivX0nI/U=; b=heW3sHI89UE7Ez/Uw8s9L0jVBXywl49o4608vp4I7lW+xfpHQWTTGfbkIaa16/AACzRBrf ZpDCmishH0tiFY4BE+cQdOUKyv3OR1Xumts7jiEi0FouDueEcYKpu/ka+c0jZbU5VLagEv 33jUK+mHbKB1hyUOAR4vDaKeOZOY1II= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=FT5iL9QN; spf=pass (imf01.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721995195; a=rsa-sha256; cv=none; b=Xb0GM9Ax0XcLTGm697Te74nTBi5Ii/ZNRIWjJlqfQI/3Ou78jOJTkShTbyiw5EX172PDBT NucHcen6QFqhbL4EiHsSCgkV1p0okpGWJw7d7Qg7PMIP6G67rhOjGsQy9ZMuqiJ3wvA5dC FmWOO2SbsY/r7yz6ZIhvrTxovcokmTc= Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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-202.mailbox.org (Postfix) with ESMTPS id 4WVmXZ2yfvz9t5k; Fri, 26 Jul 2024 14:00:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1721995214; 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=deQPnnwd6TDzRj/A268eQqMo7O4raj6JQhQivX0nI/U=; b=FT5iL9QN/v9EXF+rksj3tSAbWcJwXkYgSvV3GM6fcW2n6dMSgjEZ9H+ImoP+KySY4TgPeg MRNyoYgh6AYlpch7HLKG3z9v2XzT059u7m/7wqC/kwv6YdlguxYP0JnSRaRgkiHaUenVuc +jxDZWzzMk3qTottcCJt8V6Q82cY+mNzI9rPD6GiOLoPGUesqbDoXN3yUvV+EcuQR0DMPk UdDflMdJyTXG+Ba5cm6R+0ZahTzUb9FVikHzHIAB48qyHIW9TIZ4kOfBLgyRVM++USEAgd 72JYL6K7ER8VlQzoFhpIQmnXoZDcbm0Ux+AjLem8yMHxBZzJbJJqxrlHMTn2Nw== From: "Pankaj Raghav (Samsung)" To: david@fromorbit.com, willy@infradead.org, chandan.babu@oracle.com, djwong@kernel.org, brauner@kernel.org, akpm@linux-foundation.org Cc: yang@os.amperecomputing.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, john.g.garry@oracle.com, linux-fsdevel@vger.kernel.org, hare@suse.de, p.raghav@samsung.com, mcgrof@kernel.org, gost.dev@samsung.com, cl@os.amperecomputing.com, linux-xfs@vger.kernel.org, kernel@pankajraghav.com, ryan.roberts@arm.com, hch@lst.de, Zi Yan Subject: [PATCH v11 02/10] filemap: allocate mapping_min_order folios in the page cache Date: Fri, 26 Jul 2024 13:59:48 +0200 Message-ID: <20240726115956.643538-3-kernel@pankajraghav.com> In-Reply-To: <20240726115956.643538-1-kernel@pankajraghav.com> References: <20240726115956.643538-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 9C0154000D X-Stat-Signature: 1unsjzbft8ojbn9dtsp14qnf7pdz1gba X-HE-Tag: 1721995219-767844 X-HE-Meta: U2FsdG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8qK0WTnl5BVDAaSBeFdgeBTlLyb+9MGzo4fKni4Q9TxMDM/XCVdvku3l89bZ/s0/4VlinQ8iglAv3JnUQI/iCuPK1wN2loVW3827Ml+9+pTM/W4bEa17uGLpi6LfgLQAhNp9CYZ2P0AxIhOsjqiYsiLWkdThjYufrUyFdy1/h4u694cDDp9QbhibqEhkFrbEq6bjVla+aWaYGSC/rDU9jMrs6Zm+ZBbHF+kMRYKJiQUtNKnPCyyBiMyaK6afQJhlPB816+wMFGPWlLAS6x0qlYXHqCtKywkjrDDAi3/8rI/P76+Xo1CBeG/B6QhGZzSYffjxJIQca3z0gy6miEC+urCpBdcaDDF1GLc5I6L0Eu2UoCQmD8gt7nlf01z4rh96L7tgvv/Xh4G5CpDjT864nvPLZl73lM1U1NLG2KB9KsQSU8QeaVN 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. Supporting mapping_min_order implies that we guarantee each folio in the page cache has at least an order of mapping_min_order. When adding new folios to the page cache we must also ensure the index used is aligned to the mapping_min_order as the page cache requires the index to be aligned to the order of the folio. Co-developed-by: Luis Chamberlain Signed-off-by: Luis Chamberlain Signed-off-by: Pankaj Raghav Reviewed-by: Hannes Reinecke Reviewed-by: Darrick J. Wong Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/pagemap.h | 20 ++++++++++++++++++++ mm/filemap.c | 24 ++++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 75bbe88b89904..3a876d6801a90 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -447,6 +447,26 @@ mapping_min_folio_order(const struct address_space *mapping) return (mapping->flags & AS_FOLIO_ORDER_MIN_MASK) >> AS_FOLIO_ORDER_MIN; } +static inline unsigned long +mapping_min_folio_nrpages(struct address_space *mapping) +{ + return 1UL << mapping_min_folio_order(mapping); +} + +/** + * mapping_align_index() - Align index for this mapping. + * @mapping: The address_space. + * + * The index of a folio must be naturally aligned. If you are adding a + * new folio to the page cache and need to know what index to give it, + * call this function. + */ +static inline pgoff_t mapping_align_index(struct address_space *mapping, + pgoff_t index) +{ + return round_down(index, mapping_min_folio_nrpages(mapping)); +} + /* * Large folio support currently depends on THP. These dependencies are * being worked on but are not yet fixed. diff --git a/mm/filemap.c b/mm/filemap.c index ad5e4a848070e..d27e9ac54309d 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -859,6 +859,8 @@ noinline int __filemap_add_folio(struct address_space *mapping, VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio); + VM_BUG_ON_FOLIO(folio_order(folio) < mapping_min_folio_order(mapping), + folio); mapping_set_update(&xas, mapping); VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio); @@ -1919,8 +1921,10 @@ 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 min_order = mapping_min_folio_order(mapping); + unsigned int order = max(min_order, FGF_GET_ORDER(fgp_flags)); int err; + index = mapping_align_index(mapping, index); if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping)) gfp |= __GFP_WRITE; @@ -1943,7 +1947,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, gfp_t alloc_gfp = gfp; err = -ENOMEM; - if (order > 0) + if (order > min_order) alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN; folio = filemap_alloc_folio(alloc_gfp, order); if (!folio) @@ -1958,7 +1962,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; @@ -2447,13 +2451,15 @@ static int filemap_update_page(struct kiocb *iocb, } static int filemap_create_folio(struct file *file, - struct address_space *mapping, pgoff_t index, + struct address_space *mapping, loff_t pos, struct folio_batch *fbatch) { struct folio *folio; int error; + unsigned int min_order = mapping_min_folio_order(mapping); + pgoff_t index; - folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0); + folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order); if (!folio) return -ENOMEM; @@ -2471,6 +2477,7 @@ static int filemap_create_folio(struct file *file, * well to keep locking rules simple. */ filemap_invalidate_lock_shared(mapping); + index = (pos >> (PAGE_SHIFT + min_order)) << min_order; error = filemap_add_folio(mapping, folio, index, mapping_gfp_constraint(mapping, GFP_KERNEL)); if (error == -EEXIST) @@ -2531,8 +2538,7 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count, if (!folio_batch_count(fbatch)) { if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ)) return -EAGAIN; - err = filemap_create_folio(filp, mapping, - iocb->ki_pos >> PAGE_SHIFT, fbatch); + err = filemap_create_folio(filp, mapping, iocb->ki_pos, fbatch); if (err == AOP_TRUNCATED_PAGE) goto retry; return err; @@ -3748,9 +3754,11 @@ 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); + index = mapping_align_index(mapping, index); err = filemap_add_folio(mapping, folio, index, gfp); if (unlikely(err)) { folio_put(folio);