Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1099541lqt; Fri, 7 Jun 2024 08:02:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXjD9IE6G4slgW7Nl3aNpGNdwYN7woPalCRtWILPkIsuzdObCT1OmWrmeEPHgr7oGKKfXDxuwnea4zR2W2WsgICh5CEYp+0UF7pnBXTow== X-Google-Smtp-Source: AGHT+IE1mfdSOlGYS6NlvE+mLaK0bHcSZAKKaoH142aN0hzPh8Huak5jlFo+ZQSBQ1iI/jwZvCCY X-Received: by 2002:a50:99d4:0:b0:57c:61df:fe68 with SMTP id 4fb4d7f45d1cf-57c61dfffafmr425865a12.17.1717772547839; Fri, 07 Jun 2024 08:02:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717772547; cv=pass; d=google.com; s=arc-20160816; b=fFWplQKVl+lBccs9wckAiyfD3lARJJW86re/yxR+Y0RU9yndspcqzWCH1PW4nZ0k6G Q4tV+GqOZGx/PQtfYZuAh9lgwn36PifN016mNq5DqMWm2GSZT+absqJV73fCD/SpruGb vNNzg8kkvOWUBJVIYFESXfwLiL6cN8bp4pUX5bHz+ulGNnn/5i7FB8zXzZsNjadxJ4YZ O2IADRhfY1XhxOubMzwDzHgLmyuntf02GDvDWao3xcJhsb+Nh6VtGZVR9dpcm+cWae4B WDgyuMnV4UL0O3p8lGhFoB4tSe/G4juzKFE8TVvL0bRmZUWZ2X0BLU60Qyztqsv5ZOup coZg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Irw62N1JW6TXxSmdYHpkVdAv1s6RDr+2+xzjFjEPurM=; fh=pqFiGWrwOnMOz7J5JEcls+uL5JrBxW7q4gBZg6HAuoA=; b=i7QPVnvjIqvLn3Hgqd5fuGXI6G+t/kQkGSE2Iq2l9QduHBR6KcwdfCJJBig34scDra pmHMwDpJYecp4HSDFObjf0kaf/KWXtIYBCAk86cApEYPoJMQ9zR+JrKGg9oGCUyMtRW4 Z+xS6wXvKkq1ozgII1lRO04KLHu6J4c2/V0V/VgM9nP3PBy8sygThylZv+gWADukUsu+ qM7mwxys4wuD/YxXu6Z95OXOI0HgoOhAiVK9RZlOrb/GEj+rWCAuVndbs3PL7pdQG00P QBdBNIlctb8FQrSEDt04eEYgrfiOzTuw6NVL3lynVIfVtIau7EBEeMl53a7gN9Yd7R7p 7PMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@pankajraghav.com header.s=MBO0001 header.b=FkxZmTeU; arc=pass (i=1 spf=pass spfdomain=pankajraghav.com dkim=pass dkdomain=pankajraghav.com dmarc=pass fromdomain=pankajraghav.com); spf=pass (google.com: domain of linux-kernel+bounces-206332-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206332-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pankajraghav.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57aae0c65bdsi1824482a12.126.2024.06.07.08.02.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 08:02:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206332-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@pankajraghav.com header.s=MBO0001 header.b=FkxZmTeU; arc=pass (i=1 spf=pass spfdomain=pankajraghav.com dkim=pass dkdomain=pankajraghav.com dmarc=pass fromdomain=pankajraghav.com); spf=pass (google.com: domain of linux-kernel+bounces-206332-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206332-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pankajraghav.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 648241F271B8 for ; Fri, 7 Jun 2024 15:02:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9449119B5B1; Fri, 7 Jun 2024 14:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="FkxZmTeU" Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E219119B3FF; Fri, 7 Jun 2024 14:59:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772374; cv=none; b=mDxE0b59pGAbTKUNg88zAV7Lo13mROhtW43vdyAC8Vn7/7n1MknHAOmI9qwK29czZI8+Ngk/L5pw5cNHhds0dTSxgTX7oU34xeT46KoaE5q/+0BLMar7gAXXxDHLeiM0RN69/5uOpUnRfxXGUYJyS+3FwOCtEIhR8gEUXM1M450= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772374; c=relaxed/simple; bh=lbv0itGOzatp3n/PHe8lv107qcmBTcfYEIFSF2I5aD0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PTPFb205HTDuYMkxK+lbPJM3Q3TpsXUiPCd24Oz01eGN07LmJswkOwnRHk1Kpt6Yhw3A8ln93pbgawEfY3TlgOBe4+jD0HttBBrE2oMM8c+pdESr1ZMq6fcDsDpe4kXSeqxlDkof23NyI3OBsjptAUZaY4OIUNQMiO1MhuBgUcU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pankajraghav.com; spf=pass smtp.mailfrom=pankajraghav.com; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b=FkxZmTeU; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pankajraghav.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pankajraghav.com 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-201.mailbox.org (Postfix) with ESMTPS id 4Vwkqt2MWmz9sjD; Fri, 7 Jun 2024 16:59:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1717772362; 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=Irw62N1JW6TXxSmdYHpkVdAv1s6RDr+2+xzjFjEPurM=; b=FkxZmTeUQodjEwwEb75K+2zFPvV6b3JqGei1ApUQXuLQDxmDPz7MmaHI7SKOBUvLpmf/+6 RIG/chfBNPr6kotV6OPdDvQ2fwPorxEiUAdm4lDe/5yIeIzs6Lg5h1Hw/zyJ3o1NyHKRQq kmed/ezABoj9DNu1Wa4aNFXxRIjx/Qn/Vol75q9M1CNMQMSfGRzNQwUMhcHAe6jVL088In KZPehD/qjeDDVgFzm+4i5Nz9ZXZ+uuuIGrUE9KmBArwJl4NsL/bEQaSQuKxPcLwgZpiX1/ 1jJOTZA699fY5g1ol6vT8/4maWJuppnAYwwt91laAD3wYIiOgszdFZcKO9WSRw== From: "Pankaj Raghav (Samsung)" To: david@fromorbit.com, djwong@kernel.org, chandan.babu@oracle.com, brauner@kernel.org, akpm@linux-foundation.org, willy@infradead.org Cc: mcgrof@kernel.org, linux-mm@kvack.org, hare@suse.de, linux-kernel@vger.kernel.org, yang@os.amperecomputing.com, Zi Yan , linux-xfs@vger.kernel.org, p.raghav@samsung.com, linux-fsdevel@vger.kernel.org, kernel@pankajraghav.com, hch@lst.de, gost.dev@samsung.com, cl@os.amperecomputing.com, john.g.garry@oracle.com Subject: [PATCH v7 03/11] filemap: allocate mapping_min_order folios in the page cache Date: Fri, 7 Jun 2024 14:58:54 +0000 Message-ID: <20240607145902.1137853-4-kernel@pankajraghav.com> In-Reply-To: <20240607145902.1137853-1-kernel@pankajraghav.com> References: <20240607145902.1137853-1-kernel@pankajraghav.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4Vwkqt2MWmz9sjD 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 --- include/linux/pagemap.h | 20 ++++++++++++++++++++ mm/filemap.c | 26 ++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 228275e7049f..899b8d751768 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -439,6 +439,26 @@ unsigned int 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_start_index() - Align starting index based on the min + * folio order of the page cache. + * @mapping: The address_space. + * + * Ensure the index used is aligned to the minimum folio order when adding + * new folios to the page cache by rounding down to the nearest minimum + * folio number of pages. + */ +static inline pgoff_t mapping_align_start_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 46c7a6f59788..8bb0d2bc93c5 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_start_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,16 @@ 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 +2478,8 @@ static int filemap_create_folio(struct file *file, * well to keep locking rules simple. */ filemap_invalidate_lock_shared(mapping); + /* index in PAGE units but aligned to min_order number of pages. */ + 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 +2540,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 +3756,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_start_index(mapping, index); err = filemap_add_folio(mapping, folio, index, gfp); if (unlikely(err)) { folio_put(folio); -- 2.44.1