Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages
@ 2026-11-19 12:02 Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 1/7] mm: add PG_zeroed page flag and buddy merge tracking Michael S. Tsirkin
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization

Many hosts immediately discard each free page guest reports.
This means that re-zeroing them in the guest after allocation
is redundant.

This series lets a page reporting device advertise that its completed
reports guarantee zero contents. It tracks that state through the buddy
allocator and uses it to skip clearing freshly allocated user folios.

The series is based on Linux 7.4-rc3, commit 666539eecdf4d.

This is the first part of the project. Further hugepage support, along with
the init_on_free and FPI_ZEROED additions suggested by Matthew Wilcox,
is left for follow-up work if this series is merged.

Patches 1-2 add PG_zeroed tracking and the page reporting device's zeroing
guarantee.

Patches 3-5 skip redundant zeroing in the PMD THP, order-0 (including
x86), and mTHP anonymous allocation paths.

Patch 6 adds VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED to negotiate this
with host.

Patch 7 adds a flush parameter with an approximate reporting budget for
deterministic testing.

For a 512 MiB anonymous allocation using MADV_POPULATE_WRITE in a 2 GiB,
one-vCPU KVM guest, ten samples per mode gave:

  task-clock              feature off          feature on
  PMD THP                 229.0 +/- 20.0 ms     74.7 +/- 35.8 ms
  order-0                 336.1 +/-  6.5 ms     99.2 +/- 19.3 ms

Assisted-by: LLM

Changes since v10:
- Rewritten along the lines suggested by Matthew Wilcox [1].

[1] https://lore.kernel.org/all/aiePVsiV-w-IZ8A4@casper.infradead.org/

Michael S. Tsirkin (7):
  mm: add PG_zeroed page flag and buddy merge tracking
  mm: page_reporting: support host-zeroed reported pages
  mm: skip zeroing in vma_alloc_anon_folio_pmd for pre-zeroed pages
  mm: skip zeroing of pre-zeroed order-0 user pages
  mm: skip zeroing in alloc_anon_folio for pre-zeroed pages
  virtio_balloon: add DEVICE_INIT_REPORTED feature
  mm: page_reporting: add flush parameter with page budget

 drivers/virtio/virtio_balloon.c     | 33 ++++++++++++++++++++--
 include/linux/highmem.h             | 29 +++++++++++++++++--
 include/linux/mm.h                  | 32 +++++++++++++++++----
 include/linux/page-flags.h          | 15 +++++++++-
 include/linux/page_reporting.h      |  8 ++++++
 include/uapi/linux/virtio_balloon.h |  1 +
 mm/huge_memory.c                    | 11 ++------
 mm/memory.c                         | 10 +------
 mm/page_alloc.c                     | 43 ++++++++++++++++++++++++-----
 mm/page_reporting.c                 | 43 +++++++++++++++++++++++++++++
 10 files changed, 189 insertions(+), 36 deletions(-)


base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
-- 
MST


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v11 1/7] mm: add PG_zeroed page flag and buddy merge tracking
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 2/7] mm: page_reporting: support host-zeroed reported pages Michael S. Tsirkin
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Liam R. Howlett, Mike Rapoport, Johannes Weiner,
	Brendan Jackman

Add PG_zeroed (aliased to PG_private_2) to track pages whose contents
are known to be zero.  Propagate it through buddy merges and splits.
Upon folio allocation, PG_zeroed is cleared and its value is transferred
to BIT(2) in lru.next.

This bit, in turn, is tested by the new folio_init_user(). If clear, the
helper zero-initializes the folio. If set, it skips clearing the
contents.

Clear the aliased flag on free so filesystem state cannot be mistaken
for zeroed contents.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 include/linux/highmem.h    | 21 +++++++++++++++++++++
 include/linux/mm.h         | 32 +++++++++++++++++++++++++++-----
 include/linux/page-flags.h | 15 ++++++++++++++-
 mm/page_alloc.c            | 38 ++++++++++++++++++++++++++++++++------
 4 files changed, 94 insertions(+), 12 deletions(-)

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index d7aac9de1c8a4..70bbe93b8d669 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -303,6 +303,27 @@ static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
 #endif
 }
 
+/**
+ * folio_init_user - Initialize a freshly allocated folio for userspace.
+ * @folio: The folio freshly allocated from the buddy allocator.
+ * @addr_hint: The address accessed by the user or the base address.
+ *
+ * The caller must not have written to the folio or repurposed folio->lru
+ * since allocation: the pre-zeroed marker is only valid until then.
+ * Preserve known-zero contents, but initialize KMSAN metadata that the
+ * allocator may have poisoned. Otherwise, clear the folio if required.
+ */
+static inline void folio_init_user(struct folio *folio, unsigned long addr_hint)
+{
+	if (folio_is_zeroed(folio)) {
+		kmsan_unpoison_memory(folio_address(folio), folio_size(folio));
+		return;
+	}
+
+	if (user_alloc_needs_zeroing())
+		folio_zero_user(folio, addr_hint);
+}
+
 #ifndef vma_alloc_zeroed_movable_folio
 /**
  * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 6af3b7008f1ad..7c9ada8273815 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3325,6 +3325,26 @@ static inline void clear_page_pfmemalloc(struct page *page)
 	page->lru.next = NULL;
 }
 
+static inline bool page_is_zeroed(const struct page *page)
+{
+	return (uintptr_t)page->lru.next & BIT(2);
+}
+
+static inline bool folio_is_zeroed(const struct folio *folio)
+{
+	return (uintptr_t)folio->lru.next & BIT(2);
+}
+
+static inline void set_page_zeroed(struct page *page)
+{
+	page->lru.next = (void *)((uintptr_t)page->lru.next | BIT(2));
+}
+
+static inline void clear_page_zeroed(struct page *page)
+{
+	page->lru.next = (void *)((uintptr_t)page->lru.next & ~BIT(2));
+}
+
 /*
  * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
  */
@@ -5544,8 +5564,9 @@ enum mf_action_page_type {
 	MF_MSG_UNKNOWN,
 };
 
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
 void folio_zero_user(struct folio *folio, unsigned long addr_hint);
+
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
 int copy_user_large_folio(struct folio *dst, struct folio *src,
 			  unsigned long addr_hint,
 			  struct vm_area_struct *vma);
@@ -5693,11 +5714,12 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);
 
 /* Mask used for checking in page_pool_page_is_pp() below. page->pp_magic is
  * OR'ed with PP_SIGNATURE after the allocation in order to preserve bit 0 for
- * the head page of compound page and bit 1 for pfmemalloc page, as well as the
- * bits used for the DMA index. page_is_pfmemalloc() is checked in
- * __page_pool_put_page() to avoid recycling the pfmemalloc page.
+ * the head page of compound page, bit 1 for pfmemalloc page, and bit 2 for
+ * zeroed page, as well as the bits used for the DMA index.
+ * page_is_pfmemalloc() is checked in __page_pool_put_page() to avoid
+ * recycling the pfmemalloc page.
  */
-#define PP_MAGIC_MASK ~(PP_DMA_INDEX_MASK | 0x3UL)
+#define PP_MAGIC_MASK ~(PP_DMA_INDEX_MASK | 0x7UL)
 
 #ifdef CONFIG_PAGE_POOL
 static inline bool page_pool_page_is_pp(const struct page *page)
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index b0ddc652e76cc..39cd5911a2188 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -153,6 +153,9 @@ enum pageflags {
 	 */
 	PG_fscache = PG_private_2,	/* page backed by cache */
 
+	/* Page contents are known to be zero (host-zeroed or balloon) */
+	PG_zeroed = PG_private_2,
+
 	/* XEN */
 	/* Pinned in Xen as a read-only pagetable page. */
 	PG_pinned = PG_owner_priv_1,
@@ -686,6 +689,16 @@ FOLIO_FLAG_FALSE(idle)
  */
 __PAGEFLAG(Reported, reported, PF_NO_COMPOUND)
 
+/*
+ * PageZeroed() tracks pages whose contents are known to be zero.
+ * Set on free-list pages by the balloon driver or page reporting.
+ * The allocator uses this to skip redundant zeroing.
+ */
+__PAGEFLAG(Zeroed, zeroed, PF_HEAD)
+SETPAGEFLAG(Zeroed, zeroed, PF_NO_COMPOUND)
+CLEARPAGEFLAG(Zeroed, zeroed, PF_NO_COMPOUND)
+#define __PG_ZEROED (1UL << PG_zeroed)
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 PAGEFLAG(VmemmapSelfHosted, vmemmap_self_hosted, PF_ANY)
 #else
@@ -1189,7 +1202,7 @@ static __always_inline void __ClearPageAnonExclusive(struct page *page)
  * alloc-free cycle to prevent from reusing the page.
  */
 #define PAGE_FLAGS_CHECK_AT_PREP	\
-	((PAGEFLAGS_MASK & ~__PG_HWPOISON) | LRU_GEN_MASK | LRU_REFS_MASK)
+	((PAGEFLAGS_MASK & ~(__PG_HWPOISON | __PG_ZEROED)) | LRU_GEN_MASK | LRU_REFS_MASK)
 
 /*
  * Flags stored in the second page of a compound page.  They may overlap
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 6624293e4d787..927a288eb419b 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -986,10 +986,21 @@ static inline void __free_one_page(struct page *page,
 			change_pageblock_range(buddy, order, migratetype);
 		}
 
-		combined_pfn = buddy_pfn & pfn;
-		page = page + (combined_pfn - pfn);
-		pfn = combined_pfn;
-		order++;
+		{
+			bool page_zeroed = PageZeroed(page);
+			bool buddy_zeroed = PageZeroed(buddy);
+
+			__ClearPageZeroed(page);
+			__ClearPageZeroed(buddy);
+
+			combined_pfn = buddy_pfn & pfn;
+			page = page + (combined_pfn - pfn);
+			pfn = combined_pfn;
+			order++;
+
+			if (page_zeroed && buddy_zeroed)
+				__SetPageZeroed(page);
+		}
 	}
 
 done_merging:
@@ -1378,7 +1389,7 @@ static __always_inline bool __free_pages_prepare(struct page *page,
 					continue;
 				}
 			}
-			tail_page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
+			tail_page->flags.f &= ~(PAGE_FLAGS_CHECK_AT_PREP | __PG_ZEROED);
 		}
 	}
 	if (folio_test_anon(folio)) {
@@ -1397,7 +1408,8 @@ static __always_inline bool __free_pages_prepare(struct page *page,
 	}
 
 	page_cpupid_reset_last(page);
-	page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
+	/* Clear the aliased PG_private_2 bit when freeing allocated pages. */
+	page->flags.f &= ~(PAGE_FLAGS_CHECK_AT_PREP | __PG_ZEROED);
 	page->private = 0;
 	reset_page_owner(page, order);
 	page_table_check_free(page, order);
@@ -1507,6 +1519,7 @@ static void split_large_buddy(struct zone *zone, struct page *page,
 			      unsigned long pfn, int order, fpi_t fpi)
 {
 	unsigned long end = pfn + (1 << order);
+	bool zeroed = PageZeroed(page);
 
 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn, 1 << order));
 	/* Caller removed page from freelist, buddy info cleared! */
@@ -1518,6 +1531,8 @@ static void split_large_buddy(struct zone *zone, struct page *page,
 	do {
 		int mt = get_pfnblock_migratetype(page, pfn);
 
+		if (zeroed)
+			__SetPageZeroed(page);
 		__free_one_page(page, pfn, zone, order, mt, fpi);
 		pfn += 1 << order;
 		if (pfn == end)
@@ -1705,6 +1720,7 @@ static inline unsigned int expand(struct zone *zone, struct page *page, int low,
 {
 	unsigned int size = 1 << high;
 	unsigned int nr_added = 0;
+	bool zeroed = PageZeroed(page);
 
 	while (high > low) {
 		high--;
@@ -1722,6 +1738,8 @@ static inline unsigned int expand(struct zone *zone, struct page *page, int low,
 
 		__add_to_free_list(&page[size], zone, high, migratetype, false);
 		set_buddy_order(&page[size], high);
+		if (zeroed)
+			__SetPageZeroed(&page[size]);
 		nr_added += size;
 	}
 
@@ -1880,6 +1898,13 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags
 		set_page_pfmemalloc(page);
 	else
 		clear_page_pfmemalloc(page);
+
+	if (PageZeroed(page)) {
+		__ClearPageZeroed(page);
+		set_page_zeroed(page);
+	} else {
+		clear_page_zeroed(page);
+	}
 }
 
 /*
@@ -3144,6 +3169,7 @@ int __isolate_free_page(struct page *page, unsigned int order)
 	}
 
 	del_page_from_free_list(page, zone, order, mt);
+	__ClearPageZeroed(page);
 
 	/*
 	 * Set the pageblock if the isolated page is at least half of a
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 2/7] mm: page_reporting: support host-zeroed reported pages
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 1/7] mm: add PG_zeroed page flag and buddy merge tracking Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 3/7] mm: skip zeroing in vma_alloc_anon_folio_pmd for pre-zeroed pages Michael S. Tsirkin
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Liam R. Howlett, Mike Rapoport, Johannes Weiner,
	Brendan Jackman

Add a host_zeroes_pages flag to page_reporting_dev_info, allowing
page reporting devices to declare that their host zeros reported pages
on reclaim.

On successful reporting by such a device that guarantees zero contents, set
PG_zeroed.

Note, that hosts zero the pages for guests, safely, even on
architectures with aliasing data caches, and even without knowing the
guest user virtual addresses.

Specifically, ARM64 data caches behave as PIPT; KVM handles cache maintenance
around stage-2 mappings and invalidates the whole I-cache when it aliases.
On aliasing MIPS, host unmapping flushes the whole D-cache, and slow GUP
flushes the host aliases of replacement backing pages before KVM maps
them.

Any host that can't handle this, will simply not set the flag in its
page reporting device.

No driver sets host_zeroes_pages yet.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 include/linux/page_reporting.h | 8 ++++++++
 mm/page_reporting.c            | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/include/linux/page_reporting.h b/include/linux/page_reporting.h
index 272b1274efdc1..49adfc5d45804 100644
--- a/include/linux/page_reporting.h
+++ b/include/linux/page_reporting.h
@@ -13,6 +13,14 @@ struct page_reporting_dev_info {
 	int (*report)(struct page_reporting_dev_info *prdev,
 		      struct scatterlist *sg, unsigned int nents);
 
+	/*
+	 * If true, the host zeros reported pages on reclaim.
+	 * This includes any architecture-required cache flushing or
+	 * invalidation, especially on architectures with aliasing data
+	 * caches, so guest accesses observe the zeroed contents.
+	 */
+	bool host_zeroes_pages;
+
 	/* work struct for processing reports */
 	struct delayed_work work;
 
diff --git a/mm/page_reporting.c b/mm/page_reporting.c
index de587be178010..841dcd64777d1 100644
--- a/mm/page_reporting.c
+++ b/mm/page_reporting.c
@@ -127,6 +127,9 @@ page_reporting_drain(struct page_reporting_dev_info *prdev,
 		int mt = get_pageblock_migratetype(page);
 		unsigned int order = get_order(sg->length);
 
+		if (reported && prdev->host_zeroes_pages)
+			__SetPageZeroed(page);
+
 		__putback_isolated_page(page, order, mt);
 
 		/* If the pages were not reported due to error skip flagging */
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 3/7] mm: skip zeroing in vma_alloc_anon_folio_pmd for pre-zeroed pages
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 1/7] mm: add PG_zeroed page flag and buddy merge tracking Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 2/7] mm: page_reporting: support host-zeroed reported pages Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 4/7] mm: skip zeroing of pre-zeroed order-0 user pages Michael S. Tsirkin
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Baolin Wang, Liam R. Howlett, Nico Pache,
	Ryan Roberts, Barry Song, Usama Arif, Kiryl Shutsemau

Initialize newly allocated anonymous PMD folios through folio_init_user(),
to skip initialization of PG_zeroed pages.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 mm/huge_memory.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index eb913acb03b74..80bd9b54eff2a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1375,17 +1375,10 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma,
 
 	folio_throttle_swaprate(folio, gfp);
 
-       /*
-	* When a folio is not zeroed during allocation (__GFP_ZERO not used)
-	* or user folios require special handling, folio_zero_user() is used to
-	* make sure that the page corresponding to the faulting address will be
-	* hot in the cache after zeroing.
-	*/
-	if (user_alloc_needs_zeroing())
-		folio_zero_user(folio, addr);
+	folio_init_user(folio, addr);
 	/*
 	 * The memory barrier inside __folio_mark_uptodate makes sure that
-	 * folio_zero_user writes become visible before the set_pmd_at()
+	 * folio initialization becomes visible before the set_pmd_at()
 	 * write.
 	 */
 	__folio_mark_uptodate(folio);
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 4/7] mm: skip zeroing of pre-zeroed order-0 user pages
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
                   ` (2 preceding siblings ...)
  2026-11-19 12:02 ` [PATCH v11 3/7] mm: skip zeroing in vma_alloc_anon_folio_pmd for pre-zeroed pages Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 5/7] mm: skip zeroing in alloc_anon_folio for pre-zeroed pages Michael S. Tsirkin
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Liam R. Howlett, Mike Rapoport, Johannes Weiner,
	Brendan Jackman

Skip initialization for order-0 PG_zeroed folios.

For the generic vma_alloc_zeroed_movable_folio(), skip clearing known-zero
contents and unpoison KMSAN metadata.

On x86, vma_alloc_zeroed_movable_folio() requests __GFP_ZERO, so check
PageZeroed() in post_alloc_hook() to cover that.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 include/linux/highmem.h | 8 ++++++--
 mm/page_alloc.c         | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 70bbe93b8d669..4abd2a0fd82f7 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -344,8 +344,12 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 	struct folio *folio;
 
 	folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vaddr);
-	if (folio && user_alloc_needs_zeroing())
-		clear_user_highpage(&folio->page, vaddr);
+	if (folio) {
+		if (folio_is_zeroed(folio))
+			kmsan_unpoison_memory(folio_address(folio), PAGE_SIZE);
+		else if (user_alloc_needs_zeroing())
+			clear_user_highpage(&folio->page, vaddr);
+	}
 
 	return folio;
 }
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 927a288eb419b..c2cb2f66c1736 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1829,8 +1829,11 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 				gfp_t gfp_flags, unsigned int alloc_flags)
 {
 	const bool zero_tags = gfp_flags & __GFP_ZEROTAGS;
+	/*
+	 * Skip zeroing if already done. Sanitizer handling still needs to happen.
+	 */
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
-			!should_skip_init(gfp_flags);
+			!should_skip_init(gfp_flags) && !PageZeroed(page);
 	int i;
 
 	set_page_private(page, 0);
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 5/7] mm: skip zeroing in alloc_anon_folio for pre-zeroed pages
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
                   ` (3 preceding siblings ...)
  2026-11-19 12:02 ` [PATCH v11 4/7] mm: skip zeroing of pre-zeroed order-0 user pages Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:02 ` [PATCH v11 6/7] virtio_balloon: add DEVICE_INIT_REPORTED feature Michael S. Tsirkin
  2026-11-19 12:03 ` [PATCH v11 7/7] mm: page_reporting: add flush parameter with page budget Michael S. Tsirkin
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Liam R. Howlett, Mike Rapoport

Initialize newly allocated anonymous multi-size THP folios through
folio_init_user().

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 mm/memory.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index ec3d9d99ccee2..020ae881b226e 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5479,15 +5479,7 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			goto fallback;
 		}
 		folio_throttle_swaprate(folio, gfp);
-		/*
-		 * When a folio is not zeroed during allocation
-		 * (__GFP_ZERO not used) or user folios require special
-		 * handling, folio_zero_user() is used to make sure
-		 * that the page corresponding to the faulting address
-		 * will be hot in the cache after zeroing.
-		 */
-		if (user_alloc_needs_zeroing())
-			folio_zero_user(folio, vmf->address);
+		folio_init_user(folio, vmf->address);
 		return folio;
 next:
 		count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 6/7] virtio_balloon: add DEVICE_INIT_REPORTED feature
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
                   ` (4 preceding siblings ...)
  2026-11-19 12:02 ` [PATCH v11 5/7] mm: skip zeroing in alloc_anon_folio for pre-zeroed pages Michael S. Tsirkin
@ 2026-11-19 12:02 ` Michael S. Tsirkin
  2026-11-19 12:03 ` [PATCH v11 7/7] mm: page_reporting: add flush parameter with page budget Michael S. Tsirkin
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Jason Wang, Eugenio Pérez, Xuan Zhuo

Add VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED (bit 6) device feature.
When negotiated, the host zeros reported pages on reclaim.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 drivers/virtio/virtio_balloon.c     | 33 ++++++++++++++++++++++++++---
 include/uapi/linux/virtio_balloon.h |  1 +
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 8c1b47ec17a29..4c6630c5f9599 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -19,6 +19,7 @@
 #include <linux/wait.h>
 #include <linux/mm.h>
 #include <linux/page_reporting.h>
+#include <linux/cc_platform.h>
 
 /*
  * Balloon device works in 4K page units.  So each page is pointed to by
@@ -205,7 +206,9 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i
 	struct virtio_balloon *vb =
 		container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
 	struct virtqueue *vq = vb->reporting_vq;
-	unsigned int unused, err;
+	struct scatterlist *entry;
+	unsigned int len, i;
+	int err;
 
 	/* We should always be able to add these buffers to an empty queue. */
 	err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT);
@@ -221,9 +224,24 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i
 	virtqueue_kick(vq);
 
 	/* When host has read buffer, this completes via balloon_ack */
-	wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
+	wait_event(vb->acked, virtqueue_get_buf(vq, &len));
 
-	return 0;
+	if (!pr_dev_info->host_zeroes_pages)
+		return 0;
+
+	/*
+	 * Only a fully initialized batch may be marked reported when reported
+	 * pages are treated as zeroed. A short completion does not identify
+	 * which pages were initialized, so reject the entire batch.
+	 * Subtract each entry to avoid overflowing a total-length accumulator.
+	 */
+	for_each_sg(sg, entry, nents, i) {
+		if (len < entry->length)
+			return -EIO;
+		len -= entry->length;
+	}
+
+	return len ? -EIO : 0;
 }
 
 static void set_page_pfns(struct virtio_balloon *vb,
@@ -1043,6 +1061,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
 #if defined(CONFIG_ARM64) && defined(CONFIG_ARM64_64K_PAGES)
 		vb->pr_dev_info.order = 5;
 #endif
+		if (virtio_has_feature(vb->vdev,
+				      VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED))
+			vb->pr_dev_info.host_zeroes_pages = true;
 
 		vb->pr_dev_info.capacity = capacity;
 		err = page_reporting_register(&vb->pr_dev_info);
@@ -1189,6 +1210,11 @@ static int virtballoon_validate(struct virtio_device *vdev)
 	else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
 		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
 
+	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) ||
+	    page_poisoning_enabled_static())
+		__virtio_clear_bit(vdev,
+				   VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED);
+
 	/*
 	 * Disable indirect descriptors to avoid memory allocation in
 	 * virtqueue_add during page reporting.
@@ -1205,6 +1231,7 @@ static unsigned int features[] = {
 	VIRTIO_BALLOON_F_FREE_PAGE_HINT,
 	VIRTIO_BALLOON_F_PAGE_POISON,
 	VIRTIO_BALLOON_F_REPORTING,
+	VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED,
 };
 
 static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
index ee35a372805d4..17de46c629c2e 100644
--- a/include/uapi/linux/virtio_balloon.h
+++ b/include/uapi/linux/virtio_balloon.h
@@ -37,6 +37,7 @@
 #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
 #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
 #define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
+#define VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED	6 /* Host zeroes reported pages */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v11 7/7] mm: page_reporting: add flush parameter with page budget
  2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
                   ` (5 preceding siblings ...)
  2026-11-19 12:02 ` [PATCH v11 6/7] virtio_balloon: add DEVICE_INIT_REPORTED feature Michael S. Tsirkin
@ 2026-11-19 12:03 ` Michael S. Tsirkin
  6 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 12:03 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, David Hildenbrand, Vlastimil Babka,
	Brendan Jackman, Michal Hocko, Suren Baghdasaryan, Jason Wang,
	Andrea Arcangeli, Dev Jain, Geert Uytterhoeven, Greg Ungerer,
	Gregory Price, Huang, Ying, Lance Yang, Lorenzo Stoakes,
	Magnus Lindholm, Matthew Wilcox, Miaohe Lin, Zi Yan, linux-mm,
	virtualization, Johannes Weiner, Brendan Jackman

Add a write-only module parameter "flush" that triggers immediate
page reporting.  The value specifies approximately how many pages
(at page_reporting_order) to report.  And I mean, very approximately.
The flush loops through reporting cycles until the budget is exhausted,
all pages are reported, or a signal is pending.

  echo 512 > /sys/module/page_reporting/parameters/flush

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Assisted-by: LLM
---
 mm/page_reporting.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/mm/page_reporting.c b/mm/page_reporting.c
index 841dcd64777d1..74c03844fc3e9 100644
--- a/mm/page_reporting.c
+++ b/mm/page_reporting.c
@@ -360,6 +360,46 @@ static void page_reporting_process(struct work_struct *work)
 static DEFINE_MUTEX(page_reporting_mutex);
 DEFINE_STATIC_KEY_FALSE(page_reporting_enabled);
 
+static int page_reporting_flush_set(const char *val,
+				    const struct kernel_param *kp)
+{
+	struct page_reporting_dev_info *prdev;
+	unsigned int budget;
+	int err;
+
+	err = kstrtouint(val, 0, &budget);
+	if (err)
+		return err;
+	if (!budget)
+		return 0;
+
+	mutex_lock(&page_reporting_mutex);
+	prdev = rcu_dereference_protected(pr_dev_info,
+				lockdep_is_held(&page_reporting_mutex));
+	if (prdev) {
+		while (budget) {
+			budget -= min(budget, PAGE_REPORTING_CAPACITY);
+			flush_delayed_work(&prdev->work);
+			__page_reporting_request(prdev);
+			flush_delayed_work(&prdev->work);
+			if (atomic_read(&prdev->state) == PAGE_REPORTING_IDLE)
+				break;
+			if (signal_pending(current))
+				break;
+		}
+	}
+	mutex_unlock(&page_reporting_mutex);
+	return 0;
+}
+
+static const struct kernel_param_ops flush_ops = {
+	.set = page_reporting_flush_set,
+	.get = param_get_uint,
+};
+static unsigned int page_reporting_flush;
+module_param_cb(flush, &flush_ops, &page_reporting_flush, 0200);
+MODULE_PARM_DESC(flush, "Report up to N pages at page_reporting_order");
+
 int page_reporting_register(struct page_reporting_dev_info *prdev)
 {
 	int err = 0;
-- 
MST


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-19 12:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 12:02 [PATCH v11 0/7] mm/virtio: skip redundant zeroing of host-zeroed reported pages Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 1/7] mm: add PG_zeroed page flag and buddy merge tracking Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 2/7] mm: page_reporting: support host-zeroed reported pages Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 3/7] mm: skip zeroing in vma_alloc_anon_folio_pmd for pre-zeroed pages Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 4/7] mm: skip zeroing of pre-zeroed order-0 user pages Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 5/7] mm: skip zeroing in alloc_anon_folio for pre-zeroed pages Michael S. Tsirkin
2026-11-19 12:02 ` [PATCH v11 6/7] virtio_balloon: add DEVICE_INIT_REPORTED feature Michael S. Tsirkin
2026-11-19 12:03 ` [PATCH v11 7/7] mm: page_reporting: add flush parameter with page budget Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox