Skip to contentSkip to conversation

add static huge zero folio support

PATCH Superseded Pankaj Raghav (Samsung) 5 participants 35 messages Aug 4, 2025 – Aug 6, 2025

Subsystems (6)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover add static huge zero folio support

From: Pankaj Raghav <[email protected]>

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 concern was raised during the review of adding Large Block Size support to XFS[2][3].

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 a single bvec.

Some examples of places in the kernel where this could be useful:
- blkdev_issue_zero_pages()
- iomap_dio_zero()
- vmalloc.c:zero_iter()
- rxperf_process_call()
- fscrypt_zeroout_range_inline_crypt()
- bch2_checksum_update()
...

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 the 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.

I have converted blkdev_issue_zero_pages() as an example as a part of this series. I also noticed close to 4% performance improvement just by replacing ZERO_PAGE with static huge_zero_folio.

I will send patches to individual subsystems using the huge_zero_folio once this gets upstreamed.

Looking forward to some feedback.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-xfs/[email protected]/
[3] https://lore.kernel.org/linux-xfs/[email protected]/

Changes since RFC v2:
- Convert get_huge_zero_page and put_huge_zero_page to *_folio.
- Convert MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO.
- Make the retry for huge_zero_folio from 2 to 1.
- Add an extra sanity check in shrinker scan for static huge_zero_folio case.

Changes since v1:
- Fixed all warnings.
- Added a retry feature after a particular time.
- Added Acked-by and Signed-off-by from David.

Changes since last series[1]:
- Instead of allocating a new page through memblock, use the same infrastructure as huge_zero_folio but raise the reference and never drop it. (David)
- And some minor cleanups based on Lorenzo's feedback.

Pankaj Raghav (5):
mm: rename huge_zero_page to huge_zero_folio
mm: rename MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO
mm: add static huge zero folio
mm: add largest_zero_folio() routine
block: use largest_zero_folio in __blkdev_issue_zero_pages()

 arch/x86/Kconfig         |  1 +
 block/blk-lib.c          | 15 +++----
 include/linux/huge_mm.h  | 35 ++++++++++++++++
 include/linux/mm_types.h |  2 +-
 mm/Kconfig               | 21 ++++++++++
 mm/huge_memory.c         | 86 ++++++++++++++++++++++++++++++----------
 6 files changed, 131 insertions(+), 29 deletions(-)

base-commit: df01d1162a83194a036f0d648ae41e6ad8adbe1a

Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/5 mm: rename huge_zero_page to huge_zero_folio

From: Pankaj Raghav <[email protected]>

As we already moved from exposing huge_zero_page to huge_zero_folio, change the name of the shrinker and the other helper function to reflect that.

No functional changes.

---
 mm/huge_memory.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
mm/huge_memory.c +17 -17
index 2b4ea5a2ce7d..6625514f622b 100644
@@ -207,7 +207,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 	return orders;
 }
 
-static bool get_huge_zero_page(void)
+static bool get_huge_zero_folio(void)
 {
 	struct folio *zero_folio;
 retry:
@@ -237,7 +237,7 @@ static bool get_huge_zero_page(void)
 	return true;
 }
 
-static void put_huge_zero_page(void)
+static void put_huge_zero_folio(void)
 {
 	/*
 	 * Counter should never go to zero here. Only shrinker can put
@@ -251,11 +251,11 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
 		return READ_ONCE(huge_zero_folio);
 
-	if (!get_huge_zero_page())
+	if (!get_huge_zero_folio())
 		return NULL;
 
 	if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
-		put_huge_zero_page();
+		put_huge_zero_folio();
 
 	return READ_ONCE(huge_zero_folio);
 }
@@ -263,18 +263,18 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
 	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
-		put_huge_zero_page();
+		put_huge_zero_folio();
 }
 
-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) == 1 ? HPAGE_PMD_NR : 0;
 }
 
-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) == 1) {
 		struct folio *zero_folio = xchg(&huge_zero_folio, NULL);
@@ -287,7 +287,7 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
 	return 0;
 }
 
-static struct shrinker *huge_zero_page_shrinker;
+static struct shrinker *huge_zero_folio_shrinker;
 
 #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)
 
 static int __init thp_shrinker_init(void)
 {
-	huge_zero_page_shrinker = shrinker_alloc(0, "thp-zero");
-	if (!huge_zero_page_shrinker)
+	huge_zero_folio_shrinker = shrinker_alloc(0, "thp-zero");
+	if (!huge_zero_folio_shrinker)
 		return -ENOMEM;
 
 	deferred_split_shrinker = 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;
 	}
 
-	huge_zero_page_shrinker->count_objects = shrink_huge_zero_page_count;
-	huge_zero_page_shrinker->scan_objects = shrink_huge_zero_page_scan;
-	shrinker_register(huge_zero_page_shrinker);
+	huge_zero_folio_shrinker->count_objects = shrink_huge_zero_folio_count;
+	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
+	shrinker_register(huge_zero_folio_shrinker);
 
 	deferred_split_shrinker->count_objects = deferred_split_count;
 	deferred_split_shrinker->scan_objects = deferred_split_scan;
@@ -875,7 +875,7 @@ static int __init thp_shrinker_init(void)
 
 static void __init thp_shrinker_exit(void)
 {
-	shrinker_free(huge_zero_page_shrinker);
+	shrinker_free(huge_zero_folio_shrinker);
 	shrinker_free(deferred_split_shrinker);
 }
 
Signature · 1 line
2.49.0
#3 lore raw

PATCH 2/5 mm: rename MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO

In reply to Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

As all the helper functions has been renamed from *_page to *_folio, rename the MM flag from MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO.

No functional changes.

---
 include/linux/mm_types.h | 2 +-
 mm/huge_memory.c         | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/mm_types.h +1 -1
index 1ec273b06691..2ad5eaddfcce 100644
@@ -1753,7 +1753,7 @@ enum {
 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
+#define MMF_HUGE_ZERO_FOLIO	23      /* mm has ever used the global huge zero folio */
 #define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
 #define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
mm/huge_memory.c +3 -3
index 6625514f622b..ff06dee213eb 100644
@@ -248,13 +248,13 @@ static void put_huge_zero_folio(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
-	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		return READ_ONCE(huge_zero_folio);
 
 	if (!get_huge_zero_folio())
 		return NULL;
 
-	if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_and_set_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		put_huge_zero_folio();
 
 	return READ_ONCE(huge_zero_folio);
@@ -262,7 +262,7 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
-	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		put_huge_zero_folio();
 }
 
Signature · 1 line
2.49.0
#7 lore raw

PATCH 3/5 mm: add static huge zero folio

In reply to Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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 result in allocating the huge zero folio on first request, if not already allocated, and turn it static such that it can never get freed. 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. Only x86 is enabled as a part of this series. Other architectures shall be enabled as a follow-up to this series.

[1] https://lore.kernel.org/linux-xfs/[email protected]/
[2] https://lore.kernel.org/linux-xfs/[email protected]/

---
 arch/x86/Kconfig        |  1 +
 include/linux/huge_mm.h | 18 ++++++++++++++++
 mm/Kconfig              | 21 +++++++++++++++++++
 mm/huge_memory.c        | 46 ++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
index 0ce86e14ab5e..8e2aa1887309 100644
@@ -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
include/linux/huge_mm.h +18 -0
index 7748489fde1b..78ebceb61d0e 100644
@@ -476,6 +476,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
 
 extern struct folio *huge_zero_folio;
 extern unsigned long huge_zero_pfn;
+extern atomic_t huge_zero_folio_is_static;
 
 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)
 
 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();
+}
 
 static inline bool thp_migration_supported(void)
 {
@@ -685,6 +698,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
 {
 	return 0;
 }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf..366a6d2d771e 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
 
+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
 
mm/huge_memory.c +45 -1

64 lines not on this page · raw message

Signature · 1 line
2.49.0
#24 lore raw

PATCH 4/5 mm: add largest_zero_folio() routine

In reply to Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

---
 include/linux/huge_mm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/linux/huge_mm.h +17 -0
index 78ebceb61d0e..c44a6736704b 100644
@@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *folio, int new_order)
 	return split_folio_to_list_to_order(folio, NULL, new_order);
 }
 
+/*
+ * largest_zero_folio - Get the largest zero size folio available
+ *
+ * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FOLIO
+ * 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 = get_static_huge_zero_folio();
+
+	if (folio)
+		return folio;
+	return page_folio(ZERO_PAGE(0));
+}
 #endif /* _LINUX_HUGE_MM_H */
Signature · 1 line
2.49.0
#30 lore raw

PATCH 5/5 block: use largest_zero_folio in __blkdev_issue_zero_pages()

In reply to Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

---
 block/blk-lib.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
block/blk-lib.c +8 -7
index 4c9f20a689f7..3030a772d3aa 100644
@@ -196,6 +196,8 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev,
 		sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
 		struct bio **biop, unsigned int flags)
 {
+	struct folio *zero_folio = largest_zero_folio();
+
 	while (nr_sects) {
 		unsigned int nr_vecs = __blkdev_sectors_to_bio_pages(nr_sects);
 		struct bio *bio;
@@ -208,15 +210,14 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev,
 			break;
 
 		do {
-			unsigned int len, added;
+			unsigned int len;
 
-			len = min_t(sector_t,
-				PAGE_SIZE, nr_sects << SECTOR_SHIFT);
-			added = bio_add_page(bio, ZERO_PAGE(0), len, 0);
-			if (added < len)
+			len = min_t(sector_t, folio_size(zero_folio),
+				    nr_sects << SECTOR_SHIFT);
+			if (!bio_add_folio(bio, zero_folio, len, 0))
 				break;
-			nr_sects -= added >> SECTOR_SHIFT;
-			sector += added >> SECTOR_SHIFT;
+			nr_sects -= len >> SECTOR_SHIFT;
+			sector += len >> SECTOR_SHIFT;
 		} while (nr_sects);
 
 		*biop = bio_chain_and_submit(*biop, bio);
Signature · 1 line
2.49.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] mm: rename MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO

In reply to Pankaj Raghav (Samsung)
Quoted 9 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

As all the helper functions has been renamed from *_page to *_folio, rename the MM flag from MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO.

No functional changes.

LGTM, so:

Quoted 50 lines
---
 include/linux/mm_types.h | 2 +-
 mm/huge_memory.c         | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/mm_types.h +1 -1
index 1ec273b06691..2ad5eaddfcce 100644
@@ -1753,7 +1753,7 @@ enum {
 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
+#define MMF_HUGE_ZERO_FOLIO	23      /* mm has ever used the global huge zero folio */
 #define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
 #define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
mm/huge_memory.c +3 -4
index 6625514f622b..ff06dee213eb 100644
@@ -248,13 +248,13 @@ static void put_huge_zero_folio(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
-	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		return READ_ONCE(huge_zero_folio);
 
 	if (!get_huge_zero_folio())
 		return NULL;
 
-	if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_and_set_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		put_huge_zero_folio();
 
 	return READ_ONCE(huge_zero_folio);
@@ -262,7 +262,7 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
-	if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
+	if (test_bit(MMF_HUGE_ZERO_FOLIO, &mm->flags))
 		put_huge_zero_folio();
 }
 
--

2.49.0

Acked-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] mm: rename MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO

In reply to Pankaj Raghav (Samsung)
Quoted 10 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

As all the helper functions has been renamed from *_page to *_folio, rename the MM flag from MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO.

No functional changes.

---

Signature · 3 lines
Cheers,

David / dhildenb
#8 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to Pankaj Raghav (Samsung)
Quoted 69 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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 result in allocating the huge zero folio on first request, if not already allocated, and turn it static such that it can never get freed. 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. Only x86 is enabled as a part of this series. Other architectures shall be enabled as a follow-up to this series.

[1] https://lore.kernel.org/linux-xfs/[email protected]/
[2] https://lore.kernel.org/linux-xfs/[email protected]/

---
 arch/x86/Kconfig        |  1 +
 include/linux/huge_mm.h | 18 ++++++++++++++++
 mm/Kconfig              | 21 +++++++++++++++++++
 mm/huge_memory.c        | 46 ++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
index 0ce86e14ab5e..8e2aa1887309 100644
@@ -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
include/linux/huge_mm.h +0 -0
index 7748489fde1b..78ebceb61d0e 100644

include/linux/huge_mm.h vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); · line 479

Quoted 4 lines
include/linux/huge_mm.h +1 -0
@@ -476,6 +476,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
 
 extern struct folio *huge_zero_folio;
 extern unsigned long huge_zero_pfn;
+extern atomic_t huge_zero_folio_is_static;

Really don't love having globals like this, please can we have a helper function that tells you this and not extern it?

Also we're not checking CONFIG_STATIC_HUGE_ZERO_FOLIO but still exposing this value which a helper function would avoid also.

include/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 498

Quoted 8 lines
include/linux/huge_mm.h +1 -0
 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)
 
 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);

Why are we declaring a static inline function prototype that we then implement immediately below?

Quoted 71 lines
include/linux/huge_mm.h +16 -0
+
+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();
+}
 
 static inline bool thp_migration_supported(void)
 {
@@ -685,6 +698,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
 {
 	return 0;
 }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf..366a6d2d771e 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
 
+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
 
mm/huge_memory.c +1 -0
index ff06dee213eb..e117b280b38d 100644
@@ -75,6 +75,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 static bool split_underused_thp = true;
 
 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 = ~0UL;
 unsigned long huge_anon_orders_always __read_mostly;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 271

Quoted 5 lines
mm/huge_memory.c +2 -0
@@ -266,6 +267,45 @@ void mm_put_huge_zero_folio(struct mm_struct *mm)
 		put_huge_zero_folio();
 }
 
+#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO
+

Extremely tiny silly nit - there's a blank line below this, but not under the #endif, let's remove this line.

Quoted 7 lines
mm/huge_memory.c +7 -0
+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;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 288

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	/*
+	 * If we failed to allocate a huge zero folio, just refrain from
+	 * trying for one minute before retrying to get a reference again.
+	 */
+	if (atomic_read(&huge_zero_static_fail_count) > 1) {
+		if (time_before(jiffies, fail_count_clear_timer))
+			return NULL;
+		atomic_set(&huge_zero_static_fail_count, 0);
+	}

Yeah I really don't like this. This seems overly complicated and too fiddly. Also if I want a static PMD, do I want to wait a minute for next attempt?

Also doing things this way we might end up:

0. Enabling CONFIG_STATIC_HUGE_ZERO_FOLIO
1. Not doing anything that needs a static PMD for a while + get fragmentation.
2. Do something that needs it - oops can't get order-9 page, and waiting 60 seconds between attempts
3. This is silent so you think you have it switched on but are actually getting bad performance.

I appreciate wanting to reuse this code, but we need to find a way to do this really really early, and get rid of this arbitrary time out. It's very aribtrary and we have no easy way of tracing how this might behave under workload.

Also we end up pinning an order-9 page either way, so no harm in getting it first thing?

Quoted 24 lines
mm/huge_memory.c +20 -0
+	/*
+	 * Our raised reference will prevent the shrinker from ever having
+	 * success.
+	 */
+	if (!get_huge_zero_folio()) {
+		int count = atomic_inc_return(&huge_zero_static_fail_count);
+
+		if (count > 1)
+			fail_count_clear_timer = get_jiffies_64() + 60 * HZ;
+
+		return NULL;
+	}
+
+	if (atomic_cmpxchg(&huge_zero_folio_is_static, 0, 1) != 0)
+		put_huge_zero_folio();
+
+	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)
 {

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 323

Quoted 8 lines
mm/huge_memory.c +4 -1
@@ -277,7 +317,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 						 struct shrink_control *sc)
 {
 	if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
-		struct folio *zero_folio = xchg(&huge_zero_folio, NULL);
+		struct folio *zero_folio;
+
+		if (WARN_ON_ONCE(atomic_read(&huge_zero_folio_is_static)))
+			return 0;

I don't hugely love these various sets of static variables, I wonder if we could put them into a helper struct or something, and put these checks into helper functions?

static bool have_static_huge_zero_folio(void)

{
	return ...;
}

etc.

?

Quoted 7 lines
mm/huge_memory.c +1 -1
+		zero_folio = xchg(&huge_zero_folio, NULL);
 		BUG_ON(zero_folio == NULL);
 		WRITE_ONCE(huge_zero_pfn, ~0UL);
 		folio_put(zero_folio);
--

2.49.0

Cheers, Lorenzo

Reviewed-by on v1 4/5
#25 lore raw

Re: [PATCH 4/5] mm: add largest_zero_folio() routine

In reply to Pankaj Raghav (Samsung)
Quoted 10 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

This looks good to me minus nit + comment below, so:

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 719

Quoted 13 lines
---
 include/linux/huge_mm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/linux/huge_mm.h +1 -0
index 78ebceb61d0e..c44a6736704b 100644
@@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *folio, int new_order)
 	return split_folio_to_list_to_order(folio, NULL, new_order);
 }
 
+/*

Maybe let's make this a kernel doc comment?

Quoted 5 lines
include/linux/huge_mm.h +5 -0
+ * largest_zero_folio - Get the largest zero size folio available
+ *
+ * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FOLIO
+ * is enabled. Otherwise, a ZERO_PAGE folio is returned.
+ *

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 734

Quoted 10 lines
include/linux/huge_mm.h +10 -0
+ * 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 = get_static_huge_zero_folio();
+
+	if (folio)
+		return folio;
+	return page_folio(ZERO_PAGE(0));

Feels like we should have this in a wrapper function somewhere. Then again it seems people generally always open-code 'ZERO_PAGE(0)' anyway so maybe this is sort of the 'way things are done'? :P

Quoted 5 lines
include/linux/huge_mm.h +1 -1
+}
 #endif /* _LINUX_HUGE_MM_H */
--

2.49.0

Acked-by on v1 5/5
#31 lore raw

Re: [PATCH 5/5] block: use largest_zero_folio in __blkdev_issue_zero_pages()

In reply to Pankaj Raghav (Samsung)
Quoted 9 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

Nice :)

Quoted 2 lines

This is not my area, but LGTM from mm function invocation point of view so:

We will probably want some input from block people on this too.

Quoted 42 lines
---
 block/blk-lib.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
block/blk-lib.c +8 -8
index 4c9f20a689f7..3030a772d3aa 100644
@@ -196,6 +196,8 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev,
 		sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
 		struct bio **biop, unsigned int flags)
 {
+	struct folio *zero_folio = largest_zero_folio();
+
 	while (nr_sects) {
 		unsigned int nr_vecs = __blkdev_sectors_to_bio_pages(nr_sects);
 		struct bio *bio;
@@ -208,15 +210,14 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev,
 			break;
 
 		do {
-			unsigned int len, added;
+			unsigned int len;
 
-			len = min_t(sector_t,
-				PAGE_SIZE, nr_sects << SECTOR_SHIFT);
-			added = bio_add_page(bio, ZERO_PAGE(0), len, 0);
-			if (added < len)
+			len = min_t(sector_t, folio_size(zero_folio),
+				    nr_sects << SECTOR_SHIFT);
+			if (!bio_add_folio(bio, zero_folio, len, 0))
 				break;
-			nr_sects -= added >> SECTOR_SHIFT;
-			sector += added >> SECTOR_SHIFT;
+			nr_sects -= len >> SECTOR_SHIFT;
+			sector += len >> SECTOR_SHIFT;
 		} while (nr_sects);
 
 		*biop = bio_chain_and_submit(*biop, bio);
--

2.49.0

#9 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to Lorenzo Stoakes
Quoted 204 lines · Lorenzo Stoakes
Quoted 69 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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 result in allocating the huge zero folio on first request, if not already allocated, and turn it static such that it can never get freed. 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. Only x86 is enabled as a part of this series. Other architectures shall be enabled as a follow-up to this series.

[1] https://lore.kernel.org/linux-xfs/[email protected]/
[2] https://lore.kernel.org/linux-xfs/[email protected]/

---
  arch/x86/Kconfig        |  1 +
  include/linux/huge_mm.h | 18 ++++++++++++++++
  mm/Kconfig              | 21 +++++++++++++++++++
  mm/huge_memory.c        | 46 ++++++++++++++++++++++++++++++++++++++++-
  4 files changed, 85 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
index 0ce86e14ab5e..8e2aa1887309 100644
@@ -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
include/linux/huge_mm.h +0 -0
index 7748489fde1b..78ebceb61d0e 100644

include/linux/huge_mm.h vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); · line 479

Quoted 4 lines
include/linux/huge_mm.h +1 -0
@@ -476,6 +476,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
 
  extern struct folio *huge_zero_folio;
  extern unsigned long huge_zero_pfn;
+extern atomic_t huge_zero_folio_is_static;

Really don't love having globals like this, please can we have a helper function that tells you this and not extern it?

Also we're not checking CONFIG_STATIC_HUGE_ZERO_FOLIO but still exposing this value which a helper function would avoid also.

include/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 498

Quoted 8 lines
include/linux/huge_mm.h +1 -0
  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)
 
  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);

Why are we declaring a static inline function prototype that we then implement immediately below?

Quoted 71 lines
include/linux/huge_mm.h +16 -0
+
+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();
+}
 
  static inline bool thp_migration_supported(void)
  {
@@ -685,6 +698,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
  {
  	return 0;
  }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
  #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
  static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf..366a6d2d771e 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
  config ARCH_WANTS_THP_SWAP
  	def_bool n
 
+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
 
mm/huge_memory.c +1 -0
index ff06dee213eb..e117b280b38d 100644
@@ -75,6 +75,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
  static bool split_underused_thp = true;
 
  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 = ~0UL;
  unsigned long huge_anon_orders_always __read_mostly;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 271

Quoted 5 lines
mm/huge_memory.c +2 -0
@@ -266,6 +267,45 @@ void mm_put_huge_zero_folio(struct mm_struct *mm)
  		put_huge_zero_folio();
  }
 
+#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO
+

Extremely tiny silly nit - there's a blank line below this, but not under the #endif, let's remove this line.

Quoted 7 lines
mm/huge_memory.c +7 -0
+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;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 288

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	/*
+	 * If we failed to allocate a huge zero folio, just refrain from
+	 * trying for one minute before retrying to get a reference again.
+	 */
+	if (atomic_read(&huge_zero_static_fail_count) > 1) {
+		if (time_before(jiffies, fail_count_clear_timer))
+			return NULL;
+		atomic_set(&huge_zero_static_fail_count, 0);
+	}

Yeah I really don't like this. This seems overly complicated and too fiddly. Also if I want a static PMD, do I want to wait a minute for next attempt?

Also doing things this way we might end up:

0. Enabling CONFIG_STATIC_HUGE_ZERO_FOLIO
1. Not doing anything that needs a static PMD for a while + get fragmentation.
2. Do something that needs it - oops can't get order-9 page, and waiting 60 seconds between attempts
3. This is silent so you think you have it switched on but are actually getting bad performance.

Quoted 6 lines

I appreciate wanting to reuse this code, but we need to find a way to do this really really early, and get rid of this arbitrary time out. It's very aribtrary and we have no easy way of tracing how this might behave under workload.

Also we end up pinning an order-9 page either way, so no harm in getting it first thing?

What we could do, to avoid messing with memblock and two ways of initializing a huge zero folio early, and just disable the shrinker.

Downside is that the page is really static (not just when actually used at least once). I like it:

arch/x86/Kconfig +1 -0
index 0ce86e14ab5e1..8e2aa18873098 100644
@@ -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
include/linux/huge_mm.h +16 -0
index 7748489fde1b7..ccfa5c95f14b1 100644
@@ -495,6 +495,17 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
 void mm_put_huge_zero_folio(struct mm_struct *mm);
 
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	if (!IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+		return NULL;
+
+	if (unlikely(!huge_zero_folio))
+		return NULL;
+
+	return huge_zero_folio;
+}
+
 static inline bool thp_migration_supported(void)
 {
 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
@@ -685,6 +696,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
 {
 	return 0;
 }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf2..366a6d2d771e3 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
 
+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 allocated statically and 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
 
mm/huge_memory.c +8 -3
index ff06dee213eb2..f65ba3e6f0824 100644
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
 	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
 	shrinker_register(huge_zero_folio_shrinker);
 
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
 
 	return 0;
 }
Signature · 12 lines
2.50.1


Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but
then have a user-selectable option.

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

-- 
Cheers,

David / dhildenb
#10 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 10 lines
mm/huge_memory.c +0 -1
index ff06dee213eb2..f65ba3e6f0824 100644
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
   	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
   	shrinker_register(huge_zero_folio_shrinker);
 
-	deferred_split_shrinker->count_objects = deferred_split_count;

mm/huge_memory.c static int __init thp_shrinker_init(void) · line 876

Quoted 10 lines
mm/huge_memory.c +8 -2
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}

^ disabled the wrong shrinker, but you should get the idea

Signature · 3 lines
Cheers,

David / dhildenb
#11 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to David Hildenbrand
Quoted 13 lines · David Hildenbrand
Quoted 13 lines

Yeah I really don't like this. This seems overly complicated and too fiddly. Also if I want a static PMD, do I want to wait a minute for next attempt?

Also doing things this way we might end up:

0. Enabling CONFIG_STATIC_HUGE_ZERO_FOLIO
1. Not doing anything that needs a static PMD for a while + get fragmentation.
2. Do something that needs it - oops can't get order-9 page, and waiting 60 seconds between attempts
3. This is silent so you think you have it switched on but are actually getting bad performance.

Quoted 8 lines
Quoted 6 lines

I appreciate wanting to reuse this code, but we need to find a way to do this really really early, and get rid of this arbitrary time out. It's very aribtrary and we have no easy way of tracing how this might behave under workload.

Also we end up pinning an order-9 page either way, so no harm in getting it first thing?

What we could do, to avoid messing with memblock and two ways of initializing a huge zero folio early, and just disable the shrinker.

Nice, I like this approach!

Quoted 2 lines

Downside is that the page is really static (not just when actually used at least once). I like it:

Well I'm not sure this is a downside :P

User is explicitly enabling an option that says 'I'm cool to lose an order-9 page for this'.

Quoted 100 lines
arch/x86/Kconfig +1 -0
index 0ce86e14ab5e1..8e2aa18873098 100644
@@ -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
include/linux/huge_mm.h +16 -0
index 7748489fde1b7..ccfa5c95f14b1 100644
@@ -495,6 +495,17 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
 void mm_put_huge_zero_folio(struct mm_struct *mm);
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	if (!IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+		return NULL;
+
+	if (unlikely(!huge_zero_folio))
+		return NULL;
+
+	return huge_zero_folio;
+}
+
 static inline bool thp_migration_supported(void)
 {
 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
@@ -685,6 +696,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
 {
 	return 0;
 }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf2..366a6d2d771e3 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
+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 allocated statically and 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
mm/huge_memory.c +8 -4
index ff06dee213eb2..f65ba3e6f0824 100644
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
 	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
 	shrinker_register(huge_zero_folio_shrinker);
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
 	return 0;
 }
--

2.50.1

Quoted 4 lines

Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but then have a user-selectable option.

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

Yeah, though I guess we probably need to make it need CONFIG_MMU if so? Probably don't want to provide it if it might somehow break things?

I guess we could keep it as long as CONFIG_STATIC_HUGE_ZERO_FOLIO depend on something sensible like CONFIG_MMU maybe 64-bit too?

Anyway this approach looks generally good!

Quoted 6 lines

--
Cheers,

David / dhildenb

Cheers, Lorenzo

reviewer Reviewed-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] mm: rename MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO

In reply to Pankaj Raghav (Samsung)
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

As all the helper functions has been renamed from *_page to *_folio, rename the MM flag from MMF_HUGE_ZERO_PAGE to MMF_HUGE_ZERO_FOLIO.

Quoted 9 lines

No functional changes.

---
 include/linux/mm_types.h | 2 +-
 mm/huge_memory.c         | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

Best Regards,
Yan, Zi

reviewer Reviewed-by on v1 4/5
#27 lore raw

Re: [PATCH 4/5] mm: add largest_zero_folio() routine

In reply to Pankaj Raghav (Samsung)
Quoted 7 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

Quoted 7 lines
---
 include/linux/huge_mm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Best Regards,
Yan, Zi

reviewer
#2 lore raw

Re: [PATCH 1/5] mm: rename huge_zero_page to huge_zero_folio

In reply to Pankaj Raghav (Samsung)
Quoted 8 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

As we already moved from exposing huge_zero_page to huge_zero_folio, change the name of the shrinker and the other helper function to reflect that.

No functional changes.

Quoted 8 lines
---
 mm/huge_memory.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

#12 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to Lorenzo Stoakes
Quoted 101 lines
Quoted 101 lines
arch/x86/Kconfig +1 -0
index 0ce86e14ab5e1..8e2aa18873098 100644
@@ -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
include/linux/huge_mm.h +16 -0
index 7748489fde1b7..ccfa5c95f14b1 100644
@@ -495,6 +495,17 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)
  struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
  void mm_put_huge_zero_folio(struct mm_struct *mm);
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	if (!IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+		return NULL;
+
+	if (unlikely(!huge_zero_folio))
+		return NULL;
+
+	return huge_zero_folio;
+}
+
  static inline bool thp_migration_supported(void)
  {
  	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
@@ -685,6 +696,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
  {
  	return 0;
  }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
  #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +21 -0
index e443fe8cd6cf2..366a6d2d771e3 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
  config ARCH_WANTS_THP_SWAP
  	def_bool n
+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 allocated statically and 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
mm/huge_memory.c +8 -4
index ff06dee213eb2..f65ba3e6f0824 100644
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
  	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
  	shrinker_register(huge_zero_folio_shrinker);
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
  	return 0;
  }
--

2.50.1

Quoted 7 lines
Quoted 4 lines

Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but then have a user-selectable option.

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

Yeah, though I guess we probably need to make it need CONFIG_MMU if so? Probably don't want to provide it if it might somehow break things?

It would still depend on THP, and THP is !MMU. So that should just work.

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Something like

mm/huge_memory.c +6 -0
index 9c38a95e9f091..9b87884e5f299 100644
@@ -248,6 +248,9 @@ static void put_huge_zero_page(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                return READ_ONCE(huge_zero_folio);
 
@@ -262,6 +265,9 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                put_huge_zero_page();
 }
Signature · 3 lines
Cheers,

David / dhildenb
#26 lore raw

Re: [PATCH 4/5] mm: add largest_zero_folio() routine

In reply to Lorenzo Stoakes
Quoted 8 lines · Lorenzo Stoakes
Quoted 7 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

Quoted 8 lines
Quoted 3 lines

This looks good to me minus nit + comment below, so:

Thanks.

Quoted 12 lines
Quoted 12 lines
---
 include/linux/huge_mm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/linux/huge_mm.h +0 -0
index 78ebceb61d0e..c44a6736704b 100644
@@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *folio, int new_order)
 	return split_folio_to_list_to_order(folio, NULL, new_order);
 }
 
Quoted 3 lines

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 719

Quoted 1 line
include/linux/huge_mm.h +1 -0
+/*

Maybe let's make this a kernel doc comment?

Yes, I will change it in the next version :)

Quoted 26 lines
Quoted 6 lines
include/linux/huge_mm.h +5 -0
+ * largest_zero_folio - Get the largest zero size folio available
+ *
+ * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FOLIO
+ * is enabled. Otherwise, a ZERO_PAGE folio is returned.
+ *

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 734

Quoted 10 lines
include/linux/huge_mm.h +10 -0
+ * 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 = get_static_huge_zero_folio();
+
+	if (folio)
+		return folio;
+	return page_folio(ZERO_PAGE(0));

Feels like we should have this in a wrapper function somewhere. Then again it seems people generally always open-code 'ZERO_PAGE(0)' anyway so maybe this is sort of the 'way things are done'? :P

Quoted 5 lines
include/linux/huge_mm.h +1 -1
+}
 #endif /* _LINUX_HUGE_MM_H */
--

2.49.0

#13 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to David Hildenbrand

Thanks a lot Lorenzo and David for the feedback and quick iteration on the patchset. I really like the number of lines of code has been steadily reducing since the first version :)

I will fold the changes in the next series.

<snip>

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +8 -4
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
  	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
  	shrinker_register(huge_zero_folio_shrinker);
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
  	return 0;
  }
--

2.50.1

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but then have a user-selectable option.

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

One of the early feedbacks from Lorenzo was that there might be some architectures that has PMD size > 2M might enable this by mistake. So the ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO was introduced as an extra precaution apart from user selectable CONFIG_STATIC_HUGE_ZERO_FOLIO.

Isn't it better to have an extra knob per-arch to be on the safer side or you think it is too excessive?

Quoted 34 lines
Quoted 2 lines

Yeah, though I guess we probably need to make it need CONFIG_MMU if so? Probably don't want to provide it if it might somehow break things?

It would still depend on THP, and THP is !MMU. So that should just work.

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Something like

mm/huge_memory.c +6 -0
index 9c38a95e9f091..9b87884e5f299 100644
@@ -248,6 +248,9 @@ static void put_huge_zero_page(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                return READ_ONCE(huge_zero_folio);
 
@@ -262,6 +265,9 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct
 *mm)
 
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                put_huge_zero_page();
Quoted 3 lines

}

--
Pankaj

#14 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 30 lines · Pankaj Raghav (Samsung)

Thanks a lot Lorenzo and David for the feedback and quick iteration on the patchset. I really like the number of lines of code has been steadily reducing since the first version :)

I will fold the changes in the next series.

<snip>

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
(quoted diff) +8 -4
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
   	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
   	shrinker_register(huge_zero_folio_shrinker);
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
   	return 0;
   }
--

2.50.1

Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but then have a user-selectable option.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

One of the early feedbacks from Lorenzo was that there might be some architectures that has PMD size > 2M might enable this by mistake. So the ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO was introduced as an extra precaution apart from user selectable CONFIG_STATIC_HUGE_ZERO_FOLIO.

People will find creative ways to mis-configure their system no matter what you try :)

So I think best we can do is document it carefully.

Signature · 3 lines
Cheers,

David / dhildenb
#15 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 31 lines · David Hildenbrand
Quoted 30 lines · Pankaj Raghav (Samsung)

Thanks a lot Lorenzo and David for the feedback and quick iteration on the patchset. I really like the number of lines of code has been steadily reducing since the first version :)

I will fold the changes in the next series.

<snip>

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
(quoted diff) +8 -4
@@ -866,9 +866,14 @@ static int __init thp_shrinker_init(void)
   	huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
   	shrinker_register(huge_zero_folio_shrinker);
-	deferred_split_shrinker->count_objects = deferred_split_count;
-	deferred_split_shrinker->scan_objects = deferred_split_scan;
-	shrinker_register(deferred_split_shrinker);
+	if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO)) {
+		if (!get_huge_zero_folio())
+			pr_warn("Allocating static huge zero folio failed\n");
+	} else {
+		deferred_split_shrinker->count_objects = deferred_split_count;
+		deferred_split_shrinker->scan_objects = deferred_split_scan;
+		shrinker_register(deferred_split_shrinker);
+	}
   	return 0;
   }
--

2.50.1

Now, one thing I do not like is that we have "ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO" but then have a user-selectable option.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Should we just get rid of ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO?

One of the early feedbacks from Lorenzo was that there might be some architectures that has PMD size > 2M might enable this by mistake. So the ARCH_WANTS_STATIC_HUGE_ZERO_FOLIO was introduced as an extra precaution apart from user selectable CONFIG_STATIC_HUGE_ZERO_FOLIO.

Oh yeah so I did :P forgot I said that.

Quoted 2 lines

People will find creative ways to mis-configure their system no matter what you try :)

I think with an explicit config flag, this won't be _broken_ so much as wasteful, so therefore not really an issue.

Quoted 2 lines

So I think best we can do is document it carefully.

Yup, well always :)

Quoted 6 lines

--
Cheers,

David / dhildenb

Cheers, Lorenzo

#32 lore raw
In reply to Pankaj Raghav (Samsung)
Quoted 2 lines · Pankaj Raghav (Samsung)

Add a config option STATIC_HUGE_ZERO_FOLIO that will always allocate the huge_zero_folio, and it will never drop the reference.

"static" is a really odd naming choice for a dynamically allocated structure. It's one that's never freed, sure, but it's still dynamically allocated in the first place.

#22 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to Pankaj Raghav (Samsung)
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

...

In x86-land, the rules are pretty clear about using imperative voice. There are quite a few "we's" in the changelog and comments in this series.

I do think they're generally good to avoid and do lead to more clarity, but I'm also not sure how important that is in mm-land these days.

include/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 509

Quoted 10 lines
include/linux/huge_mm.h +10 -0
+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();
+}

This seems like an ideal place to use 'struct static_key'.

Quoted 32 lines

static inline bool thp_migration_supported(void)
{

include/linux/huge_mm.h +5 -0
@@ -685,6 +698,11 @@ static inline int change_huge_pud(struct mmu_gather *tlb,
 {
 	return 0;
 }
+
+static inline struct folio *get_static_huge_zero_folio(void)
+{
+	return NULL;
+}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline int split_folio_to_list_to_order(struct folio *folio,
mm/Kconfig +10 -0
index e443fe8cd6cf..366a6d2d771e 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
 
+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

mm/Kconfig config ARCH_WANT_GENERAL_HUGETLB · line 845

Quoted 10 lines
mm/Kconfig +10 -0
+	  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.

IMNHO, this is written like a changelog, not documentation for end users trying to make sense of Kconfig options. I'd suggest keeping it short and sweet:

config PERSISTENT_HUGE_ZERO_FOLIO
bool "Allocate a persistent PMD-sized folio for zeroing"
...
help
Enable this option to reduce the runtime refcounting overhead of the huge zero folio and expand the places in the kernel that can use huge zero folios.

With this option enabled, the huge zero folio is allocated once and never freed. It potentially wastes one huge page worth of memory.

Say Y if your system has lots of memory. Say N if you are memory constrained.

Quoted 28 lines
mm/Kconfig +0 -0
 config MM_ID
 	def_bool n
 
mm/huge_memory.c +10 -0
index ff06dee213eb..e117b280b38d 100644
@@ -75,6 +75,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 static bool split_underused_thp = true;
 
 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 = ~0UL;
 unsigned long huge_anon_orders_always __read_mostly;
@@ -266,6 +267,45 @@ void mm_put_huge_zero_folio(struct mm_struct *mm)
 		put_huge_zero_folio();
 }
 
+#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO
+
+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;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 288

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	/*
+	 * If we failed to allocate a huge zero folio, just refrain from
+	 * trying for one minute before retrying to get a reference again.
+	 */
+	if (atomic_read(&huge_zero_static_fail_count) > 1) {
+		if (time_before(jiffies, fail_count_clear_timer))
+			return NULL;
+		atomic_set(&huge_zero_static_fail_count, 0);
+	}

Any reason that this is an open-coded ratelimit instead of using 'struct ratelimit_state'?

I also find the 'huge_zero_static_fail_count' use pretty unintuitive. This is fundamentally a slow path. Ideally, it's called once. In the pathological case, it's called once a minute.

I'd probably just recommend putting a rate limit on this function, then using a plain old mutex for the actual allocation to keep multiple threads out.

Then the function becomes something like this:

	if (__ratelimit(&huge_zero_alloc_ratelimit))
		return;

	guard(mutex)(&huge_zero_mutex);

	if (!get_huge_zero_folio())
		return NULL;

	static_key_enable(&huge_zero_noref_key);

	return huge_zero_folio;

No atomic, no cmpxchg, no races on allocating.

...

Quoted 6 lines
mm/huge_memory.c +0 -0
 static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,
 						  struct shrink_control *sc)
 {
@@ -277,7 +317,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 						 struct shrink_control *sc)
 {

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 327

Quoted 10 lines
mm/huge_memory.c +5 -1
 	if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
-		struct folio *zero_folio = xchg(&huge_zero_folio, NULL);
+		struct folio *zero_folio;
+
+		if (WARN_ON_ONCE(atomic_read(&huge_zero_folio_is_static)))
+			return 0;
+		zero_folio = xchg(&huge_zero_folio, NULL);
 		BUG_ON(zero_folio == NULL);
 		WRITE_ONCE(huge_zero_pfn, ~0UL);
 		folio_put(zero_folio);

This seems like a hack to me. If you don't want the shrinker to run, then deregister it. Keeping the refcount elevated is fine, but repeatedly calling the shrinker to do atomic_cmpxchg() when you *know* it will do nothing seems silly.

If you can't deregister the shrinker, at least use the static_key approach and check the static key instead of doing futile cmpxchg's forever.

#28 lore raw

Re: [PATCH 4/5] mm: add largest_zero_folio() routine

In reply to Pankaj Raghav (Samsung)
Quoted 6 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

This changelog is telling a lot of the "what" but none of the "why".

The existing huge zero folio API is for users that have an mm. This is *only* for folks that want a huge zero folio but don't have an mm. That's _why_ this function is needed. It's in this series because there was no way to get a huge zero folio before the permanent one was introduced in this series.

Can we please get some of that into the function comment and changelog? It's critical.

Quoted 7 lines
include/linux/huge_mm.h +0 -0
index 78ebceb61d0e..c44a6736704b 100644
@@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *folio, int new_order)
 	return split_folio_to_list_to_order(folio, NULL, new_order);
 }

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 727

Quoted 10 lines
include/linux/huge_mm.h +9 -0
 
+/*
+ * largest_zero_folio - Get the largest zero size folio available
+ *
+ * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FOLIO
+ * is enabled. Otherwise, a ZERO_PAGE folio is returned.
+ *
+ * Deduce the size of the folio with folio_size instead of assuming the
+ * folio size.
+ */

This comment needs to get fleshed out. It at _least_ needs to say something along the lines of:

Use this when a huge zero folio is needed but there is no mm lifetime to tie it to. Basically, use this when you can't use mm_get_huge_zero_folio().

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 733

Quoted 6 lines
include/linux/huge_mm.h +6 -0
+static inline struct folio *largest_zero_folio(void)
+{
+	struct folio *folio = get_static_huge_zero_folio();
+
+	if (folio)
+		return folio;

There needs to be a newline in here.

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 736

Quoted 3 lines
include/linux/huge_mm.h +2 -0
+	return page_folio(ZERO_PAGE(0));
+}
 #endif /* _LINUX_HUGE_MM_H */

The code is fine, but the changelog and comments need quite a bit of work.

#29 lore raw

Re: [PATCH 4/5] mm: add largest_zero_folio() routine

Quoted 10 lines · Dave Hansen
Quoted 6 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

This changelog is telling a lot of the "what" but none of the "why".

Quoted 8 lines

The existing huge zero folio API is for users that have an mm. This is *only* for folks that want a huge zero folio but don't have an mm. That's _why_ this function is needed. It's in this series because there was no way to get a huge zero folio before the permanent one was introduced in this series.

Can we please get some of that into the function comment and changelog? It's critical.

Valid point. I will include that in the next version.

Quoted 22 lines
Quoted 8 lines
include/linux/huge_mm.h +0 -0
index 78ebceb61d0e..c44a6736704b 100644
@@ -716,4 +716,21 @@ static inline int split_folio_to_order(struct folio *folio, int new_order)
 	return split_folio_to_list_to_order(folio, NULL, new_order);
 }

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 727

Quoted 10 lines
include/linux/huge_mm.h +9 -0
 
+/*
+ * largest_zero_folio - Get the largest zero size folio available
+ *
+ * This function will return huge_zero_folio if CONFIG_STATIC_HUGE_ZERO_FOLIO
+ * is enabled. Otherwise, a ZERO_PAGE folio is returned.
+ *
+ * Deduce the size of the folio with folio_size instead of assuming the
+ * folio size.
+ */

This comment needs to get fleshed out. It at _least_ needs to say something along the lines of:

Quoted 4 lines

Use this when a huge zero folio is needed but there is no mm lifetime to tie it to. Basically, use this when you can't use mm_get_huge_zero_folio().

Agreed.

Quoted 9 lines

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 733

Quoted 6 lines
include/linux/huge_mm.h +6 -0
+static inline struct folio *largest_zero_folio(void)
+{
+	struct folio *folio = get_static_huge_zero_folio();
+
+	if (folio)
+		return folio;

There needs to be a newline in here.

Quoted 5 lines

include/linux/huge_mm.h static inline int split_folio_to_order(struct folio *folio, int new_order) · line 736

Quoted 3 lines
include/linux/huge_mm.h +2 -0
+	return page_folio(ZERO_PAGE(0));
+}
 #endif /* _LINUX_HUGE_MM_H */

The code is fine, but the changelog and comments need quite a bit of work.

Sounds good to me. I will make the changes. Thanks Dave.

--
Pankaj

#23 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to Dave Hansen
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

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.

Quoted 7 lines

...

In x86-land, the rules are pretty clear about using imperative voice. There are quite a few "we's" in the changelog and comments in this series.

I do think they're generally good to avoid and do lead to more clarity, but I'm also not sure how important that is in mm-land these days.

Yeah, I will change it to imperative to stay consistent.

<snip>

Quoted 42 lines
Quoted 19 lines

static inline int split_folio_to_list_to_order(struct folio *folio,

mm/Kconfig +10 -0
index e443fe8cd6cf..366a6d2d771e 100644
@@ -823,6 +823,27 @@ config ARCH_WANT_GENERAL_HUGETLB
 config ARCH_WANTS_THP_SWAP
 	def_bool n
 
+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

mm/Kconfig config ARCH_WANT_GENERAL_HUGETLB · line 845

Quoted 10 lines
mm/Kconfig +10 -0
+	  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.

IMNHO, this is written like a changelog, not documentation for end users trying to make sense of Kconfig options. I'd suggest keeping it short and sweet:

config PERSISTENT_HUGE_ZERO_FOLIO
bool "Allocate a persistent PMD-sized folio for zeroing"
...
help
Enable this option to reduce the runtime refcounting overhead of the huge zero folio and expand the places in the kernel that can use huge zero folios.

Quoted 7 lines

With this option enabled, the huge zero folio is allocated once and never freed. It potentially wastes one huge page worth of memory.

Say Y if your system has lots of memory. Say N if you are memory constrained.

This looks short and to the point. I can fold this in the next version. Thanks.

Quoted 64 lines
Quoted 28 lines
mm/Kconfig +0 -0
 config MM_ID
 	def_bool n
 
mm/huge_memory.c +10 -0
index ff06dee213eb..e117b280b38d 100644
@@ -75,6 +75,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 static bool split_underused_thp = true;
 
 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 = ~0UL;
 unsigned long huge_anon_orders_always __read_mostly;
@@ -266,6 +267,45 @@ void mm_put_huge_zero_folio(struct mm_struct *mm)
 		put_huge_zero_folio();
 }
 
+#ifdef CONFIG_STATIC_HUGE_ZERO_FOLIO
+
+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;

mm/huge_memory.c void mm_put_huge_zero_folio(struct mm_struct *mm) · line 288

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	/*
+	 * If we failed to allocate a huge zero folio, just refrain from
+	 * trying for one minute before retrying to get a reference again.
+	 */
+	if (atomic_read(&huge_zero_static_fail_count) > 1) {
+		if (time_before(jiffies, fail_count_clear_timer))
+			return NULL;
+		atomic_set(&huge_zero_static_fail_count, 0);
+	}

Any reason that this is an open-coded ratelimit instead of using 'struct ratelimit_state'?

I also find the 'huge_zero_static_fail_count' use pretty unintuitive. This is fundamentally a slow path. Ideally, it's called once. In the pathological case, it's called once a minute.

I'd probably just recommend putting a rate limit on this function, then using a plain old mutex for the actual allocation to keep multiple threads out.

Then the function becomes something like this:

	if (__ratelimit(&huge_zero_alloc_ratelimit))
		return;

	guard(mutex)(&huge_zero_mutex);

	if (!get_huge_zero_folio())
		return NULL;

	static_key_enable(&huge_zero_noref_key);

	return huge_zero_folio;
Quoted 1 line

No atomic, no cmpxchg, no races on allocating.

David already reworked this part based on Lorenzo's feedback (he also did not like the ratelimiting part like you). The reworked diff is here[1]. No ratelimiting, etc.

Quoted 17 lines

...

Quoted 14 lines

static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,

mm/huge_memory.c +5 -1
 						  struct shrink_control *sc)
 {
@@ -277,7 +317,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 						 struct shrink_control *sc)
 {
 	if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
-		struct folio *zero_folio = xchg(&huge_zero_folio, NULL);
+		struct folio *zero_folio;
+
+		if (WARN_ON_ONCE(atomic_read(&huge_zero_folio_is_static)))
+			return 0;
+		zero_folio = xchg(&huge_zero_folio, NULL);
 		BUG_ON(zero_folio == NULL);
Quoted 9 lines

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 327

Quoted 2 lines
mm/huge_memory.c +0 -0
 		WRITE_ONCE(huge_zero_pfn, ~0UL);
 		folio_put(zero_folio);

This seems like a hack to me. If you don't want the shrinker to run, then deregister it. Keeping the refcount elevated is fine, but repeatedly calling the shrinker to do atomic_cmpxchg() when you *know* it will do nothing seems silly.

The new version[1] deregisters instead of having this condition. :)

Quoted 2 lines

If you can't deregister the shrinker, at least use the static_key approach and check the static key instead of doing futile cmpxchg's forever.

--
Pankaj

[1] https://lore.kernel.org/linux-mm/[email protected]/

#33 lore raw
In reply to Dave Hansen
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Pankaj Raghav (Samsung)

Add a config option STATIC_HUGE_ZERO_FOLIO that will always allocate the huge_zero_folio, and it will never drop the reference.

"static" is a really odd naming choice for a dynamically allocated structure. It's one that's never freed, sure, but it's still dynamically allocated in the first place.

That is a fair point.

I like the rename you did to PERSISTENT_HUGE_ZERO_FOLIO instead of STATIC_HUGE_ZERO_FOLIO as we still allocate it dynamically.

@David, @Lorenzo and @Zi: Does the rename sound good to you?

--
Pankaj

#34 lore raw
Quoted 9 lines · Pankaj Raghav (Samsung)
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Pankaj Raghav (Samsung)

Add a config option STATIC_HUGE_ZERO_FOLIO that will always allocate the huge_zero_folio, and it will never drop the reference.

"static" is a really odd naming choice for a dynamically allocated structure. It's one that's never freed, sure, but it's still dynamically allocated in the first place.

Quoted 6 lines

That is a fair point.

I like the rename you did to PERSISTENT_HUGE_ZERO_FOLIO instead of STATIC_HUGE_ZERO_FOLIO as we still allocate it dynamically.

@David, @Lorenzo and @Zi: Does the rename sound good to you?

Yes, sounds good.

Signature · 3 lines
Cheers,

David / dhildenb
#16 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

In reply to David Hildenbrand
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

2 options:
- we could do:
if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO) && huge_zero_folio)
return huge_zero_folio;

or
- As dave suggested, we could do a static key and enable it when the option is enabled and the allocation succeeded. The same static key can also be used in get_static_huge_zero_folio().

I think the latter option will look more clean and slightly more optimal?

Quoted 26 lines

Something like

mm/huge_memory.c +6 -0
index 9c38a95e9f091..9b87884e5f299 100644
@@ -248,6 +248,9 @@ static void put_huge_zero_page(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                return READ_ONCE(huge_zero_folio);
 
@@ -262,6 +265,9 @@ struct folio *mm_get_huge_zero_folio(struct mm_struct
 *mm)
 
 void mm_put_huge_zero_folio(struct mm_struct *mm)
 {
+       if (IS_ENABLED(CONFIG_STATIC_HUGE_ZERO_FOLIO))
+               return huge_zero_folio;
+
        if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
                put_huge_zero_page();
Quoted 2 lines

}

--
Pankaj

#17 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 6 lines · Pankaj Raghav (Samsung)
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

Then we return huge_zero_folio, which is NULL?

Or what are you concerned about?

Signature · 3 lines
Cheers,

David / dhildenb
#18 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 11 lines · David Hildenbrand
Quoted 6 lines · Pankaj Raghav (Samsung)
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

Then we return huge_zero_folio, which is NULL?

Or what are you concerned about?

But don't we want to keep the "dynamic" allocation part be present even though we failed to allocate it statically in the shrinker_init?

Mainly so that the existing users of mm_get_huge_zero_folio() are not affected by these changes.

Quoted 6 lines
Signature · 4 lines
-- 
Cheers,

David / dhildenb
#19 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 11 lines · Pankaj Raghav (Samsung)
Quoted 10 lines · David Hildenbrand
Quoted 6 lines · Pankaj Raghav (Samsung)
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

Then we return huge_zero_folio, which is NULL?

Quoted 7 lines
Quoted 1 line

Or what are you concerned about?

But don't we want to keep the "dynamic" allocation part be present even though we failed to allocate it statically in the shrinker_init?

Mainly so that the existing users of mm_get_huge_zero_folio() are not affected by these changes.

I would just keep it simple and say that if we fail the early allocation (which will be extremely unlikely that early during boot!), just don't ever try to reallocate, even not when we could through mm_get_huge_zero_folio().

That sounds as simple as it gets. Again, failing to allocate that early and then succeeding to allocate later is a fairly unlikely scenario.

Signature · 3 lines
Cheers,

David / dhildenb
#20 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 20 lines · David Hildenbrand
Quoted 11 lines · Pankaj Raghav (Samsung)
Quoted 10 lines · David Hildenbrand
Quoted 6 lines · Pankaj Raghav (Samsung)
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

Then we return huge_zero_folio, which is NULL?

Quoted 7 lines
Quoted 1 line

Or what are you concerned about?

But don't we want to keep the "dynamic" allocation part be present even though we failed to allocate it statically in the shrinker_init?

Mainly so that the existing users of mm_get_huge_zero_folio() are not affected by these changes.

Quoted 6 lines

I would just keep it simple and say that if we fail the early allocation (which will be extremely unlikely that early during boot!), just don't ever try to reallocate, even not when we could through mm_get_huge_zero_folio().

That sounds as simple as it gets. Again, failing to allocate that early and then succeeding to allocate later is a fairly unlikely scenario.

Ok. I will also document this as a comment just so that people are aware of this behaviour.

Thanks a lot David for the comments and feedback!

Signature · 1 line
Pankaj Raghav
#21 lore raw

Re: [PATCH 3/5] mm: add static huge zero folio

Quoted 25 lines · Pankaj Raghav (Samsung)
Quoted 24 lines · David Hildenbrand
Quoted 11 lines · Pankaj Raghav (Samsung)
Quoted 10 lines · David Hildenbrand
Quoted 6 lines · Pankaj Raghav (Samsung)
Quoted 3 lines

We could go one step further and special case in mm_get_huge_zero_folio() + mm_put_huge_zero_folio() on CONFIG_STATIC_HUGE_ZERO_FOLIO.

Hmm, but we could have also failed to allocate even though the option was enabled.

Then we return huge_zero_folio, which is NULL?

Quoted 7 lines
Quoted 1 line

Or what are you concerned about?

But don't we want to keep the "dynamic" allocation part be present even though we failed to allocate it statically in the shrinker_init?

Mainly so that the existing users of mm_get_huge_zero_folio() are not affected by these changes.

I would just keep it simple and say that if we fail the early allocation (which will be extremely unlikely that early during boot!), just don't ever try to reallocate, even not when we could through mm_get_huge_zero_folio().

Quoted 7 lines
Quoted 2 lines

That sounds as simple as it gets. Again, failing to allocate that early and then succeeding to allocate later is a fairly unlikely scenario.

Ok. I will also document this as a comment just so that people are aware of this behaviour.

Thanks a lot David for the comments and feedback!

Sure, as always, feel free to object if you think I am talking nonsense :)

Signature · 3 lines
Cheers,

David / dhildenb