From patchwork Thu Aug 15 09:08:41 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: 13764595 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 46A35C3DA7F for ; Thu, 15 Aug 2024 09:09:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A1296B00A4; Thu, 15 Aug 2024 05:09:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 951396B00A5; Thu, 15 Aug 2024 05:09:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81B5F6B00A6; Thu, 15 Aug 2024 05:09:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 62B1B6B00A4 for ; Thu, 15 Aug 2024 05:09:17 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D97A6C13A5 for ; Thu, 15 Aug 2024 09:09:16 +0000 (UTC) X-FDA: 82453905912.02.E5884FA Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by imf23.hostedemail.com (Postfix) with ESMTP id 1AE54140012 for ; Thu, 15 Aug 2024 09:09:14 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=UW42QgFo; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf23.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.171 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723712942; a=rsa-sha256; cv=none; b=xbnQKV/9G2UqovJ8GhONJFgWwxHyQRzQoPuH4NdoByBXtbHc9I1cPZD1Gncpwjha5665yc dTsUEd5rZk4Bqc1dO0Q9uk66qK+HEsXkkJ23dNgZG3TVq78xZo3Oho0Wnn4rQd8GOy1j+f CXr3Xc0hPpv5EHUGfHoyVnwlM8ViIJ4= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=UW42QgFo; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf23.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.171 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723712942; 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=HuI7fVPMlfsEpgPxKValH+cep7FGGOgEqF+LLd6h+EY=; b=Zl5sZDF/OgO+3TyvCtijZkCSp2fQFAhamFr+yKT/WyAVDbsLx8fKuBm8NIf8DUBtesjhQ6 eEgURBVWaraDtm/0iVNc2Uw8ezdcbBt+6zI8mYZwYyJLap2DDLNgceVPPdEcLq8t3d7z8s vPcbL520qu2YKLHam/i0DtmSLBOeQTU= Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::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-201.mailbox.org (Postfix) with ESMTPS id 4Wkznz6MBnz9sSJ; Thu, 15 Aug 2024 11:09:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1723712951; 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=HuI7fVPMlfsEpgPxKValH+cep7FGGOgEqF+LLd6h+EY=; b=UW42QgFoOK84Vd0PpRAiSW72YyDcY6HrA3zsBOCM2B5h890yjK3m1ac1P8wXsG/VrXiz+/ OAe+LynBFrdhdmX/PiWHMFSbkTK94soDSHdrs/zWsJ0nrgtlGN+rBTtD3gcAFk0CYKrhHr wyaf4O7IpLOaoQ9RWqsjn0boxcNDHet5v9yMUYbpWjkQ/OJZLq2ShNygzy3iT+70CJ32Rl NkIEwkiqDTGqfJ8zbZwEJ0tZTia2TCn5ZttkKSiTea/vS59m1hfJFljwDHrJ4tjHuYslng j4lAzWPJT2C0zUspOGe6/ZVamBXeBhG3SUngSWxNL77wV/H1sBlltqRX/dEkcw== From: "Pankaj Raghav (Samsung)" To: brauner@kernel.org, akpm@linux-foundation.org Cc: chandan.babu@oracle.com, linux-fsdevel@vger.kernel.org, djwong@kernel.org, hare@suse.de, gost.dev@samsung.com, linux-xfs@vger.kernel.org, kernel@pankajraghav.com, hch@lst.de, david@fromorbit.com, Zi Yan , yang@os.amperecomputing.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, willy@infradead.org, john.g.garry@oracle.com, cl@os.amperecomputing.com, p.raghav@samsung.com, mcgrof@kernel.org, ryan.roberts@arm.com Subject: [PATCH v12 02/10] filemap: allocate mapping_min_order folios in the page cache Date: Thu, 15 Aug 2024 11:08:41 +0200 Message-ID: <20240815090849.972355-3-kernel@pankajraghav.com> In-Reply-To: <20240815090849.972355-1-kernel@pankajraghav.com> References: <20240815090849.972355-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 1AE54140012 X-Rspamd-Server: rspam01 X-Stat-Signature: 63s4y7epstmox8gq9dsz6q4biniagcuy X-HE-Tag: 1723712954-942127 X-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YUqM+2ntc0b77Sr7V9sB214xcHbdUhPMm3oaSi1+kjpArtn3HDD0mFE4ruBBTXc9BMxA2FW1brEs3GmOpFkrYLCG7y2KMu3nlVFaM/eRMgAevg5C3Ra886/lLgUxmx+4Bq7WadhbcP+XJ9Eu9fl2BhJwik4V5a/cHbDuyH75LUD4BcJHUmb8BMS/Wd+E+gkkeN2imYvg/+U5lpVzlyOI1WGj1xg0InJcQUQSRsDMy2d58Gr39iHuUluZjUH5kpRt44XaxvdzW3OaF66KKHwXdbY8K7LevX55jVi/a/Lrj98Avd0n4L6hnYS5rx9o8PbmnoNpMWIs+SCkhUR9LMdNFIEu7eiA== 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 6c4489ada3ecc..623c0f988da79 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);