From nobody Sun Feb 8 01:07:21 2026 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 776322E8DF2; Thu, 24 Jul 2025 14:50:26 +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=1753368628; cv=none; b=AHUEdaey6AVZdkzajChvm0zDUuVgpYhyxVhySrD4jcgjDr6vqU4aCaJcNR0eDRuCk9G1giRt/e2uJMxeI6u71AvRVTeGgfmeVgzy9TF2fac6nyNRPnmPSKSPyFhh2GUEjLC/9+dbnWjbQcGlJQYUGhYJHlpdGfVXmFJZfCpttQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753368628; c=relaxed/simple; bh=7+0BZ+Y9aBy5gxJfmvebis2hZGKosloX75tcKikiPpI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nxEBelaTQ+YJ9ZHYzS38+ZbONW1BnMfUNVvmaQBdGsqPYAGi+Z0ZCBUyjWmXrXbpIri/NrC3CFXl0pON+DAFuF8tg6zRxSzR/y9umg2fwVxnGTjHU9VH5cSXx6jtxk1OXYIJ8fN5dPJuFjjVZbyVlRrDbw5yw8Z44VY9a1tKAIQ= 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=fGW1OltX; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="fGW1OltX" 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 4bnv7L5sBNz9tV9; Thu, 24 Jul 2025 16:50:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1753368622; 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=FOp/GZbIELNxUFAy4Lvk7xSAmNGCaCvGZ6cxvtiBNgE=; b=fGW1OltXhLG1qiHyJSscjoHS8RAejuySa0WcLuJd2nbCWCzpDlr2zcCXbGwexZt9B5CUCI FgFH5JmvrYdCQZ0E3o2yfc02Ndr6hONgtkOBoKajck9dEl/yL9eewMOY7V5GWVOfneM9SM oapFzHxro2NG2ZomEFCXnc4Z89zLdTy51XADLaAknjCA4FDQuRmgYCn9hPv3f//+yT7UtC E6yhUE1mP8yKAkoT+GKp062MZXLTuHDMiA8dDKvLe2DSxSu790SXQm04rnrUbIi2yphu1i KmD6XqflycMsCjHi4mAPKdwuVa5SPuYW/0xXxf2wwIL3dic33soKYhgNCvmkXQ== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of kernel@pankajraghav.com designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com From: "Pankaj Raghav (Samsung)" To: Suren Baghdasaryan , Ryan Roberts , Baolin Wang , Borislav Petkov , Ingo Molnar , "H . Peter Anvin" , Vlastimil Babka , Zi Yan , Mike Rapoport , Dave Hansen , Michal Hocko , David Hildenbrand , Lorenzo Stoakes , Andrew Morton , Thomas Gleixner , Nico Pache , Dev Jain , "Liam R . Howlett" , Jens Axboe Cc: linux-kernel@vger.kernel.org, willy@infradead.org, linux-mm@kvack.org, x86@kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Darrick J . Wong" , mcgrof@kernel.org, gost.dev@samsung.com, kernel@pankajraghav.com, hch@lst.de, Pankaj Raghav Subject: [RFC v2 1/4] mm: rename huge_zero_page_shrinker to huge_zero_folio_shrinker Date: Thu, 24 Jul 2025 16:49:58 +0200 Message-ID: <20250724145001.487878-2-kernel@pankajraghav.com> In-Reply-To: <20250724145001.487878-1-kernel@pankajraghav.com> References: <20250724145001.487878-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: quoted-printable X-Rspamd-Queue-Id: 4bnv7L5sBNz9tV9 Content-Type: text/plain; charset="utf-8" From: Pankaj Raghav As we already moved from exposing huge_zero_page to huge_zero_folio, change the name of the shrinker to reflect that. No functional changes. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Pankaj Raghav Reviewed-by: Lorenzo Stoakes --- mm/huge_memory.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2b4ea5a2ce7d..5d8365d1d3e9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -266,15 +266,15 @@ void mm_put_huge_zero_folio(struct mm_struct *mm) put_huge_zero_page(); } =20 -static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink, - struct shrink_control *sc) +static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink, + struct shrink_control *sc) { /* we can free zero page only if last reference remains */ return atomic_read(&huge_zero_refcount) =3D=3D 1 ? HPAGE_PMD_NR : 0; } =20 -static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, - struct shrink_control *sc) +static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, + struct shrink_control *sc) { if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) =3D=3D 1) { struct folio *zero_folio =3D xchg(&huge_zero_folio, NULL); @@ -287,7 +287,7 @@ static unsigned long shrink_huge_zero_page_scan(struct = shrinker *shrink, return 0; } =20 -static struct shrinker *huge_zero_page_shrinker; +static struct shrinker *huge_zero_folio_shrinker; =20 #ifdef CONFIG_SYSFS static ssize_t enabled_show(struct kobject *kobj, @@ -849,8 +849,8 @@ static inline void hugepage_exit_sysfs(struct kobject *= hugepage_kobj) =20 static int __init thp_shrinker_init(void) { - huge_zero_page_shrinker =3D shrinker_alloc(0, "thp-zero"); - if (!huge_zero_page_shrinker) + huge_zero_folio_shrinker =3D shrinker_alloc(0, "thp-zero"); + if (!huge_zero_folio_shrinker) return -ENOMEM; =20 deferred_split_shrinker =3D shrinker_alloc(SHRINKER_NUMA_AWARE | @@ -858,13 +858,13 @@ static int __init thp_shrinker_init(void) SHRINKER_NONSLAB, "thp-deferred_split"); if (!deferred_split_shrinker) { - shrinker_free(huge_zero_page_shrinker); + shrinker_free(huge_zero_folio_shrinker); return -ENOMEM; } =20 - huge_zero_page_shrinker->count_objects =3D shrink_huge_zero_page_count; - huge_zero_page_shrinker->scan_objects =3D shrink_huge_zero_page_scan; - shrinker_register(huge_zero_page_shrinker); + huge_zero_folio_shrinker->count_objects =3D shrink_huge_zero_folio_count; + huge_zero_folio_shrinker->scan_objects =3D shrink_huge_zero_folio_scan; + shrinker_register(huge_zero_folio_shrinker); =20 deferred_split_shrinker->count_objects =3D deferred_split_count; deferred_split_shrinker->scan_objects =3D deferred_split_scan; @@ -875,7 +875,7 @@ static int __init thp_shrinker_init(void) =20 static void __init thp_shrinker_exit(void) { - shrinker_free(huge_zero_page_shrinker); + shrinker_free(huge_zero_folio_shrinker); shrinker_free(deferred_split_shrinker); } =20 --=20 2.49.0 From nobody Sun Feb 8 01:07:21 2026 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 BB9D628D849; Thu, 24 Jul 2025 14:50:33 +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=1753368635; cv=none; b=G5O0N/p+bLLs989SzC7GiClSAh4d2l02NU1ndA9Y+2nM06Iw7nyftPp4Dyn1sA8nqe1xRz5Gx9UpzvyRTCMCTySNhBQE8WDvzyM8vwMSLhKey9vrpuR+Nvvo/r+zj1vPMOonnq25q/NPFKV7Omep5bECgjxYgTx/SiufqXAz1jQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753368635; c=relaxed/simple; bh=U+XrJ7qPmXckpgma7xjywbKnSlZ/lISxgwb9DgiEZdA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S+AEskJtFHNy1LGBTSU5l2LjN3zQ0YbMaHyqGsxK1iroSIwyOf7DpOEBM2MXilIM3NtBhaRpDGCHkMp5IFlMDCqNbVEt+pUckKJChEUFbJMEjmQPYBlq/rIr76MB0J+j5i7v+LHdzWxBCK1s1KksqPo0bZ43PKITsTOISyUFxIw= 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=aGH8S43j; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="aGH8S43j" 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 4bnv7T6G6Zz9sts; Thu, 24 Jul 2025 16:50:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1753368629; 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=L5l/HIngC1kXD7T+ooDKjA2O1qsfDTkVYtcVPghF1zI=; b=aGH8S43jvliLtI9JLC7gBR7ZmPCNPo5wcNcyvxKVz0IM0/XqSy8QwkIqO6a50QlUPIXpkr w807Dk+viwStdR/npoCHpSA5zfnhHIb72oHI6PiRwfqtHM9JVF99mj/eki1GoaOkXlvT9o D/mfyjTLI45zpliMVMCu9O3aRhI2lNHTSO+sZGy/f3id+zsC2gxfO8Oq9iaKwxQkFR4epW pogJ1GjOn9d3SoCIvD/5AnfVGNMTsjs5A0bZizbJh8lXtWL6uWQpQfl6RQz4sjUWlNoihE EVopdEgme7f1JUspdKXZ2HVL2NJMnx28FlHHsQG3oTbmsDBzuY5cdKA4o3Q/qw== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of kernel@pankajraghav.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com From: "Pankaj Raghav (Samsung)" To: Suren Baghdasaryan , Ryan Roberts , Baolin Wang , Borislav Petkov , Ingo Molnar , "H . Peter Anvin" , Vlastimil Babka , Zi Yan , Mike Rapoport , Dave Hansen , Michal Hocko , David Hildenbrand , Lorenzo Stoakes , Andrew Morton , Thomas Gleixner , Nico Pache , Dev Jain , "Liam R . Howlett" , Jens Axboe Cc: linux-kernel@vger.kernel.org, willy@infradead.org, linux-mm@kvack.org, x86@kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Darrick J . Wong" , mcgrof@kernel.org, gost.dev@samsung.com, kernel@pankajraghav.com, hch@lst.de, Pankaj Raghav Subject: [RFC v2 2/4] mm: add static huge zero folio Date: Thu, 24 Jul 2025 16:49:59 +0200 Message-ID: <20250724145001.487878-3-kernel@pankajraghav.com> In-Reply-To: <20250724145001.487878-1-kernel@pankajraghav.com> References: <20250724145001.487878-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: quoted-printable X-Rspamd-Queue-Id: 4bnv7T6G6Zz9sts Content-Type: text/plain; charset="utf-8" From: Pankaj Raghav There are many places in the kernel where we need to zeroout larger chunks but the maximum segment we can zeroout at a time by ZERO_PAGE is limited by PAGE_SIZE. This is especially annoying in block devices and filesystems where we attach multiple ZERO_PAGEs to the bio in different bvecs. With multipage bvec support in block layer, it is much more efficient to send out larger zero pages as a part of single bvec. This concern was raised during the review of adding LBS support to XFS[1][2]. Usually huge_zero_folio is allocated on demand, and it will be deallocated by the shrinker if there are no users of it left. At moment, huge_zero_folio infrastructure refcount is tied to the process lifetime that created it. This might not work for bio layer as the completions can be async and the process that created the huge_zero_folio might no longer be alive. And, one of the main point that came during discussion is to have something bigger than zero page as a drop-in replacement. Add a config option STATIC_HUGE_ZERO_FOLIO that will always allocate the huge_zero_folio, and it will never drop the reference. This makes using the huge_zero_folio without having to pass any mm struct and does not tie the lifetime of the zero folio to anything, making it a drop-in replacement for ZERO_PAGE. If STATIC_HUGE_ZERO_FOLIO config option is enabled, then mm_get_huge_zero_folio() will simply return this page instead of dynamically allocating a new PMD page. This option can waste memory in small systems or systems with 64k base page size. So make it an opt-in and also add an option from individual architecture so that we don't enable this feature for larger base page size systems. [1] https://lore.kernel.org/linux-xfs/20231027051847.GA7885@lst.de/ [2] https://lore.kernel.org/linux-xfs/ZitIK5OnR7ZNY0IG@infradead.org/ Co-developed-by: David Hildenbrand Signed-off-by: David Hildenbrand Signed-off-by: Pankaj Raghav --- arch/x86/Kconfig | 1 + include/linux/huge_mm.h | 18 ++++++++++++++++++ mm/Kconfig | 21 +++++++++++++++++++++ mm/huge_memory.c | 42 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0ce86e14ab5e..8e2aa1887309 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -153,6 +153,7 @@ config X86 select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64 select ARCH_WANT_HUGETLB_VMEMMAP_PREINIT if X86_64 select ARCH_WANTS_THP_SWAP if X86_64 + select ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO if X86_64 select ARCH_HAS_PARANOID_L1D_FLUSH select ARCH_WANT_IRQS_OFF_ACTIVATE_MM select BUILDTIME_TABLE_SORT diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 7748489fde1b..78ebceb61d0e 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -476,6 +476,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); =20 extern struct folio *huge_zero_folio; extern unsigned long huge_zero_pfn; +extern atomic_t huge_zero_folio_is_static; =20 static inline bool is_huge_zero_folio(const struct folio *folio) { @@ -494,6 +495,18 @@ static inline bool is_huge_zero_pmd(pmd_t pmd) =20 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm); void mm_put_huge_zero_folio(struct mm_struct *mm); +struct folio *__get_static_huge_zero_folio(void); + +static inline struct folio *get_static_huge_zero_folio(void) +{ + if (!IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) + return NULL; + + if (likely(atomic_read(&huge_zero_folio_is_static))) + return huge_zero_folio; + + return __get_static_huge_zero_folio(); +} =20 static inline bool thp_migration_supported(void) { @@ -685,6 +698,11 @@ static inline int change_huge_pud(struct mmu_gather *t= lb, { return 0; } + +static inline struct folio *get_static_huge_zero_folio(void) +{ + return NULL; +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 static inline int split_folio_to_list_to_order(struct folio *folio, diff --git a/mm/Kconfig b/mm/Kconfig index 0287e8d94aea..e2132fcf2ccb 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -835,6 +835,27 @@ config ARCH_WANT_GENERAL_HUGETLB config ARCH_WANTS_THP_SWAP def_bool n =20 +config ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO + def_bool n + +config STATIC_HUGE_ZERO_FOLIO + bool "Allocate a PMD sized folio for zeroing" + depends on ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO && TRANSPARENT_HUGEPAGE + help + Without this config enabled, the huge zero folio is allocated on + demand and freed under memory pressure once no longer in use. + To detect remaining users reliably, references to the huge zero folio + must be tracked precisely, so it is commonly only available for mapping + it into user page tables. + + With this config enabled, the huge zero folio can also be used + for other purposes that do not implement precise reference counting: + it is still allocated on demand, but never freed, allowing for more + wide-spread use, for example, when performing I/O similar to the + traditional shared zeropage. + + Not suitable for memory constrained systems. + config MM_ID def_bool n =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 5d8365d1d3e9..c160c37f4d31 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -75,6 +75,7 @@ static unsigned long deferred_split_scan(struct shrinker = *shrink, static bool split_underused_thp =3D true; =20 static atomic_t huge_zero_refcount; +atomic_t huge_zero_folio_is_static __read_mostly; struct folio *huge_zero_folio __read_mostly; unsigned long huge_zero_pfn __read_mostly =3D ~0UL; unsigned long huge_anon_orders_always __read_mostly; @@ -266,6 +267,47 @@ void mm_put_huge_zero_folio(struct mm_struct *mm) put_huge_zero_page(); } =20 +#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO +#define FAIL_COUNT_LIMIT 2 + +struct folio *__get_static_huge_zero_folio(void) +{ + static unsigned long fail_count_clear_timer; + static atomic_t huge_zero_static_fail_count __read_mostly; + + if (unlikely(!slab_is_available())) + return NULL; + + /* + * If we failed to allocate a huge zero folio multiple times, + * just refrain from trying for one minute before retrying to get + * a reference again. + */ + if (atomic_read(&huge_zero_static_fail_count) > FAIL_COUNT_LIMIT) { + if (time_before(jiffies, fail_count_clear_timer)) + return NULL; + atomic_set(&huge_zero_static_fail_count, 0); + } + /* + * Our raised reference will prevent the shrinker from ever having + * success. + */ + if (!get_huge_zero_page()) { + int count =3D atomic_inc_return(&huge_zero_static_fail_count); + + if (count > FAIL_COUNT_LIMIT) + fail_count_clear_timer =3D get_jiffies_64() + 60 * HZ; + + return NULL; + } + + if (atomic_cmpxchg(&huge_zero_folio_is_static, 0, 1) !=3D 0) + put_huge_zero_page(); + + return huge_zero_folio; +} +#endif /* CONFIG_STATIC_HUGE_ZERO_FOLIO */ + static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink, struct shrink_control *sc) { --=20 2.49.0 From nobody Sun Feb 8 01:07:21 2026 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 DDDD22E8E00; Thu, 24 Jul 2025 14:50:41 +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=1753368643; cv=none; b=Xwcpj/XamDFit4sJlyBSDVrm0FQaWDIZzmSTqrO84436gRqih4myWYvxqrl77UC87Gzdtcf0K4FSTNMi+ZmmG4mDnwf/lJrRWaBcf4fnZxE1gbQvG08VNOs+HMKka61pIhmDp0Rq6fsstjPavD6kAGJgunsVvbfDdIsvtekFy9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753368643; c=relaxed/simple; bh=3yvTRW5FhiAKs+vOBuG4HDRUaaXxURgqxp/7gqTmM9g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u99tfd1Qf/aYV/qi6ZfDisaoN9/l4AwyVFOjTroTthc/B+6W4t8snS/yIjBv8Ey0kfng4jw09oPd2uI7QcdXTNqcQXRjMT+gkUnBuC+gjgWrj/PDaLwfxspXEqcbo8wn8qE7/hWtPkbRdx8M+YQxLimJ0M/YDi9oiHLmN905DuA= 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=bmODlJne; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="bmODlJne" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (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 4bnv7f3dscz9stm; Thu, 24 Jul 2025 16:50:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1753368638; 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=kgGhybuB+tAZvqsghR5tZCuaExpLC7iQdGM6Ue4YApc=; b=bmODlJnet//564H9GeC9UKYZRU0b9X8jENtT/ImDqDE/BSUhU8rDT7sLaQXjai9cqvVPzC XRTh5RmBQ1OSw7iUsTEHZpj07zzID1a8hden/+Ko29ENEAmGxnE9WXBP/ZCpE6TdMcVWFY ziPtCk32ubRHQP9Re7bDQ5LcoGHYZ1HRESsfNTQRdlI7cFMrm5aCsx6seEQ8RRP0sUdfvl sEIV5w7JLjAF4BSoGp8kJuKfvhw5slZnmYfIQJcNvK1EQx3HCXMNi0n23ze48W2IQslE6a asi3QmBg3iL+GjW4H1HswQbOAienOX5tDKRH0fN+Qa6qnvMZ7lHZs/hA3JtlHw== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of kernel@pankajraghav.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com From: "Pankaj Raghav (Samsung)" To: Suren Baghdasaryan , Ryan Roberts , Baolin Wang , Borislav Petkov , Ingo Molnar , "H . Peter Anvin" , Vlastimil Babka , Zi Yan , Mike Rapoport , Dave Hansen , Michal Hocko , David Hildenbrand , Lorenzo Stoakes , Andrew Morton , Thomas Gleixner , Nico Pache , Dev Jain , "Liam R . Howlett" , Jens Axboe Cc: linux-kernel@vger.kernel.org, willy@infradead.org, linux-mm@kvack.org, x86@kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Darrick J . Wong" , mcgrof@kernel.org, gost.dev@samsung.com, kernel@pankajraghav.com, hch@lst.de, Pankaj Raghav Subject: [RFC v2 3/4] mm: add largest_zero_folio() routine Date: Thu, 24 Jul 2025 16:50:00 +0200 Message-ID: <20250724145001.487878-4-kernel@pankajraghav.com> In-Reply-To: <20250724145001.487878-1-kernel@pankajraghav.com> References: <20250724145001.487878-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: quoted-printable X-Rspamd-Queue-Id: 4bnv7f3dscz9stm Content-Type: text/plain; charset="utf-8" From: Pankaj Raghav Add largest_zero_folio() routine so that huge_zero_folio can be used directly when CONFIG_STATIC_HUGE_ZERO_FOLIO is enabled. This will return ZERO_PAGE folio if CONFIG_STATIC_HUGE_ZERO_FOLIO is disabled or if we failed to allocate a huge_zero_folio. Co-developed-by: David Hildenbrand Signed-off-by: David Hildenbrand Signed-off-by: Pankaj Raghav --- include/linux/huge_mm.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 78ebceb61d0e..c44a6736704b 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *f= olio, int new_order) return split_folio_to_list_to_order(folio, NULL, new_order); } =20 +/* + * largest_zero_folio - Get the largest zero size folio available + * + * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FO= LIO + * is enabled. Otherwise, a ZERO_PAGE folio is returned. + * + * Deduce the size of the folio with folio_size instead of assuming the + * folio size. + */ +static inline struct folio *largest_zero_folio(void) +{ + struct folio *folio =3D get_static_huge_zero_folio(); + + if (folio) + return folio; + return page_folio(ZERO_PAGE(0)); +} #endif /* _LINUX_HUGE_MM_H */ --=20 2.49.0 From nobody Sun Feb 8 01:07:21 2026 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (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 BAE382E92BB; Thu, 24 Jul 2025 14:50:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753368656; cv=none; b=brgYq/pRFdKd6yYSbpmSydMPjGaCPKYLsnRfVOqC5nuGVoMaLEBBh0q0fQ8xfPv/tTtHhgE7MLP47DzA8V004TSRficAlm3bxvBK07gpRd/v1TJk39wNFRWSKXugDWkQRFcYQyWhUu1Kh2a0kma3SpZWXiqb+DZ5tLsaap/snGg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753368656; c=relaxed/simple; bh=cE5HF3Q/pCpPUC8URCS1OOHU8K26yfnjKDLE9CXvFJg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bua8T3mDm8Jv51fN9IoBA0EhxQ9I/qxWOG5OeqO8u2jNHVmDJ4CQk/wY2tlVnga+sccm1cgaD0BzNkcy/RpZZDpPERwDij2oihHwKZbvgiIMqGD/s3WYHWVwr6z4m0u5Rrv2WS90dNKxOk4nByRjOC5p47cK422aDwtM12bzIbE= 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=x76vEytQ; arc=none smtp.client-ip=80.241.56.152 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="x76vEytQ" Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.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-102.mailbox.org (Postfix) with ESMTPS id 4bnv7n1wFlz9t5m; Thu, 24 Jul 2025 16:50:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1753368645; 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=aUm65lZP53PD6UFdCNM0FQrwo2MXNsuP3026Ad+nKaE=; b=x76vEytQYOiiVL0uyLac3ejDCr+U69TsJRegMRGlNKbpDMYYg4GpZMLFlbbQ03qKsHkyGT 3/he+Tjf5ia6zlRTkkeVh4HHNZfWPxH9XuB4olqTnZU33wEJLQ8Zqzs5TDhsLqpw/kpshB kgQACvOmj7yTzNqKfD4d/vQO30NUw1ysp57qkBzVeUA8V7H189iNVqBzwIwABxFkzTmqox m2BtWlsv9PfGbJIsZCwN/mHrKUJK+tazSBRReLrAQO308vnl0RQfQlRSpEgIDTXEowkh5U cxhDKCzlU88kiEbrA9wTAWTntaXCqvfUaabmIGGhFTAM7GNB75fIRFxIHBzqkA== From: "Pankaj Raghav (Samsung)" To: Suren Baghdasaryan , Ryan Roberts , Baolin Wang , Borislav Petkov , Ingo Molnar , "H . Peter Anvin" , Vlastimil Babka , Zi Yan , Mike Rapoport , Dave Hansen , Michal Hocko , David Hildenbrand , Lorenzo Stoakes , Andrew Morton , Thomas Gleixner , Nico Pache , Dev Jain , "Liam R . Howlett" , Jens Axboe Cc: linux-kernel@vger.kernel.org, willy@infradead.org, linux-mm@kvack.org, x86@kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Darrick J . Wong" , mcgrof@kernel.org, gost.dev@samsung.com, kernel@pankajraghav.com, hch@lst.de, Pankaj Raghav Subject: [RFC v2 4/4] block: use largest_zero_folio in __blkdev_issue_zero_pages() Date: Thu, 24 Jul 2025 16:50:01 +0200 Message-ID: <20250724145001.487878-5-kernel@pankajraghav.com> In-Reply-To: <20250724145001.487878-1-kernel@pankajraghav.com> References: <20250724145001.487878-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Pankaj Raghav Use largest_zero_folio() in __blkdev_issue_zero_pages(). On systems with CONFIG_STATIC_HUGE_ZERO_FOLIO enabled, we will end up sending larger bvecs instead of multiple small ones. Noticed a 4% increase in performance on a commercial NVMe SSD which does not support OP_WRITE_ZEROES. The device's MDTS was 128K. The performance gains might be bigger if the device supports bigger MDTS. Signed-off-by: Pankaj Raghav --- block/blk-lib.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 4c9f20a689f7..3030a772d3aa 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -196,6 +196,8 @@ static void __blkdev_issue_zero_pages(struct block_devi= ce *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned int flags) { + struct folio *zero_folio =3D largest_zero_folio(); + while (nr_sects) { unsigned int nr_vecs =3D __blkdev_sectors_to_bio_pages(nr_sects); struct bio *bio; @@ -208,15 +210,14 @@ static void __blkdev_issue_zero_pages(struct block_de= vice *bdev, break; =20 do { - unsigned int len, added; + unsigned int len; =20 - len =3D min_t(sector_t, - PAGE_SIZE, nr_sects << SECTOR_SHIFT); - added =3D bio_add_page(bio, ZERO_PAGE(0), len, 0); - if (added < len) + len =3D min_t(sector_t, folio_size(zero_folio), + nr_sects << SECTOR_SHIFT); + if (!bio_add_folio(bio, zero_folio, len, 0)) break; - nr_sects -=3D added >> SECTOR_SHIFT; - sector +=3D added >> SECTOR_SHIFT; + nr_sects -=3D len >> SECTOR_SHIFT; + sector +=3D len >> SECTOR_SHIFT; } while (nr_sects); =20 *biop =3D bio_chain_and_submit(*biop, bio); --=20 2.49.0