In __free_pages_prepare(), when FPI_ZEROED is set the page is already
known to be zero. We can skip kernel_init_pages() if page poisoning is
not enabled (because poison would overwrite the zeroes).

This avoids redundant zeroing work when freeing pages that are already
known to contain all zeros.

Signed-off-by: Michael S. Tsirkin <[email protected]>
Assisted-by: Claude:claude-opus-4-6
---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 399e2038dcd9..90a92be96ebf 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1447,7 +1447,14 @@ __always_inline bool __free_pages_prepare(struct page 
*page,
                if (kasan_has_integrated_init())
                        init = false;
        }
-       if (init)
+       /*
+        * Skip redundant zeroing when the page is already known-zero
+        * (FPI_ZEROED) and page poisoning did not overwrite it.
+        * When page_poisoning is enabled, kernel_poison_pages above
+        * wrote PAGE_POISON (0xAA), so we must re-zero.
+        */
+       if (init && !((fpi_flags & FPI_ZEROED) &&
+                     !page_poisoning_enabled_static()))
                kernel_init_pages(page, 1 << order);
 
        /*
-- 
MST


Reply via email to