Skip to contentSkip to conversation

cleanup page poisoning

PATCH Superseded Vlastimil Babka 3 participants 16 messages Nov 3, 2020 – Nov 12, 2020

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover cleanup page poisoning

The first version was called "optimize handling of memory debugging parameters" [1], changes are:

- apply review feedback
- drop former Patch 3
- add new patches 3-5, change name and cover letter of series

I have identified a number of issues and opportunities for cleanup with CONFIG_PAGE_POISON and friends:
- interaction with init_on_alloc and init_on_free parameters depends on the order of parameters (Patch 1)
- the boot time enabling uses static key, but inefficienty (Patch 2)
- sanity checking is incompatible with hibernation (Patch 3)
- CONFIG_PAGE_POISONING_NO_SANITY can be removed now that we have init_on_free (Patch 4)
- CONFIG_PAGE_POISONING_ZERO can be most likely removed now that we have init_on_free (Patch 5)

[1] https://lore.kernel.org/r/[email protected]

Vlastimil Babka (5):
mm, page_alloc: do not rely on the order of page_poison and
init_on_alloc/free parameters
mm, page_poison: use static key more efficiently kernel/power: allow hibernation with page_poison sanity checking mm, page_poison: remove CONFIG_PAGE_POISONING_NO_SANITY mm, page_poison: remove CONFIG_PAGE_POISONING_ZERO

 drivers/virtio/virtio_balloon.c |   4 +-
 include/linux/mm.h              |  43 ++++++------
 include/linux/poison.h          |   4 --
 init/main.c                     |   2 +-
 kernel/power/hibernate.c        |   2 +-
 kernel/power/power.h            |   2 +-
 kernel/power/snapshot.c         |  14 ++--
 mm/Kconfig.debug                |  28 ++------
 mm/page_alloc.c                 | 113 ++++++++++++++++----------------
 mm/page_poison.c                |  56 ++--------------
 tools/include/linux/poison.h    |   6 +-
 11 files changed, 106 insertions(+), 168 deletions(-)
Signature · 1 line
2.29.1
#1 lore raw

PATCH v2 1/5 mm, page_alloc: do not rely on the order of page_poison and init_on_alloc/free parameters

Enabling page_poison=1 together with init_on_alloc=1 or init_on_free=1 produces a warning in dmesg that page_poison takes precedence. However, as these warnings are printed in early_param handlers for init_on_alloc/free, they are not printed if page_poison is enabled later on the command line (handlers are called in the order of their parameters), or when init_on_alloc/free is always enabled by the respective config option - before the page_poison early param handler is called, it is not considered to be enabled. This is inconsistent.

We can remove the dependency on order by making the init_on_* parameters only set a boolean variable, and postponing the evaluation after all early params have been processed. Introduce a new init_mem_debugging_and_hardening() function for that, and move the related debug_pagealloc processing there as well.

As a result init_mem_debugging_and_hardening() knows always accurately if init_on_* and/or page_poison options were enabled. Thus we can also optimize want_init_on_alloc() and want_init_on_free(). We don't need to check page_poisoning_enabled() there, we can instead not enable the init_on_* static keys at all, if page poisoning is enabled. This results in a simpler and more effective code.

---
 include/linux/mm.h | 20 ++---------
 init/main.c        |  2 +-
 mm/page_alloc.c    | 88 ++++++++++++++++++++++------------------------
 3 files changed, 46 insertions(+), 64 deletions(-)
include/linux/mm.h +3 -17
index ef360fe70aaf..e0bc1c8aa2a6 100644
@@ -2865,6 +2865,7 @@ extern int apply_to_existing_page_range(struct mm_struct *mm,
 				   unsigned long address, unsigned long size,
 				   pte_fn_t fn, void *data);
 
+extern void init_mem_debugging_and_hardening(void);
 #ifdef CONFIG_PAGE_POISONING
 extern bool page_poisoning_enabled(void);
 extern void kernel_poison_pages(struct page *page, int numpages, int enable);
@@ -2874,35 +2875,20 @@ static inline void kernel_poison_pages(struct page *page, int numpages,
 					int enable) { }
 #endif
 
-#ifdef CONFIG_INIT_ON_ALLOC_DEFAULT_ON
-DECLARE_STATIC_KEY_TRUE(init_on_alloc);
-#else
 DECLARE_STATIC_KEY_FALSE(init_on_alloc);
-#endif
 static inline bool want_init_on_alloc(gfp_t flags)
 {
-	if (static_branch_unlikely(&init_on_alloc) &&
-	    !page_poisoning_enabled())
+	if (static_branch_unlikely(&init_on_alloc))
 		return true;
 	return flags & __GFP_ZERO;
 }
 
-#ifdef CONFIG_INIT_ON_FREE_DEFAULT_ON
-DECLARE_STATIC_KEY_TRUE(init_on_free);
-#else
 DECLARE_STATIC_KEY_FALSE(init_on_free);
-#endif
 static inline bool want_init_on_free(void)
 {
-	return static_branch_unlikely(&init_on_free) &&
-	       !page_poisoning_enabled();
+	return static_branch_unlikely(&init_on_free);
 }
 
-#ifdef CONFIG_DEBUG_PAGEALLOC
-extern void init_debug_pagealloc(void);
-#else
-static inline void init_debug_pagealloc(void) {}
-#endif
 extern bool _debug_pagealloc_enabled_early;
 DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
 
init/main.c +1 -1
index 130376ec10ba..14da5458baf6 100644
@@ -815,7 +815,7 @@ static void __init mm_init(void)
 	 * bigger than MAX_ORDER unless SPARSEMEM.
 	 */
 	page_ext_init_flatmem();
-	init_debug_pagealloc();
+	init_mem_debugging_and_hardening();
 	report_meminit();
 	mem_init();
 	kmem_cache_init();
mm/page_alloc.c +42 -46

120 lines not on this page · raw message

Signature · 1 line
2.29.1
#2 lore raw

PATCH v2 2/5 mm, page_poison: use static key more efficiently

In reply to Vlastimil Babka

Commit 11c9c7edae06 ("mm/page_poison.c: replace bool variable with static key") changed page_poisoning_enabled() to a static key check. However, the function is not inlined, so each check still involves a function call with overhead not eliminated when page poisoning is disabled.

Analogically to how debug_pagealloc is handled, this patch converts page_poisoning_enabled() back to boolean check, and introduces page_poisoning_enabled_static() for fast paths. Both functions are inlined.

The function kernel_poison_pages() is also called unconditionally and does the static key check inside. Remove it from there and put it to callers. Also split it to two functions kernel_poison_pages() and kernel_unpoison_pages() instead of the confusing bool parameter.

Also optimize the check that enables page poisoning instead of debug_pagealloc for architectures without proper debug_pagealloc support. Move the check to init_mem_debugging_and_hardening() to enable a single static key instead of having two static branches in page_poisoning_enabled_static().

---
 drivers/virtio/virtio_balloon.c |  2 +-
 include/linux/mm.h              | 23 +++++++++++---
 mm/page_alloc.c                 | 19 ++++++++++--
 mm/page_poison.c                | 53 +++++----------------------------
 4 files changed, 43 insertions(+), 54 deletions(-)
drivers/virtio/virtio_balloon.c +1 -1
index 481611c09dae..e53faed6ba93 100644
@@ -1116,7 +1116,7 @@ static int virtballoon_validate(struct virtio_device *vdev)
 	 */
 	if (!want_init_on_free() &&
 	    (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY) ||
-	     !page_poisoning_enabled()))
+	     !page_poisoning_enabled_static()))
 		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
 	else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
 		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
include/linux/mm.h +19 -4
index e0bc1c8aa2a6..4d6dd9f44571 100644
@@ -2867,12 +2867,27 @@ extern int apply_to_existing_page_range(struct mm_struct *mm,
 
 extern void init_mem_debugging_and_hardening(void);
 #ifdef CONFIG_PAGE_POISONING
-extern bool page_poisoning_enabled(void);
-extern void kernel_poison_pages(struct page *page, int numpages, int enable);
+extern void kernel_poison_pages(struct page *page, int numpages);
+extern void kernel_unpoison_pages(struct page *page, int numpages);
+extern bool _page_poisoning_enabled_early;
+DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
+static inline bool page_poisoning_enabled(void)
+{
+	return _page_poisoning_enabled_early;
+}
+/*
+ * For use in fast paths after init_mem_debugging() has run, or when a
+ * false negative result is not harmful when called too early.
+ */
+static inline bool page_poisoning_enabled_static(void)
+{
+	return (static_branch_unlikely(&_page_poisoning_enabled));
+}
 #else
 static inline bool page_poisoning_enabled(void) { return false; }
-static inline void kernel_poison_pages(struct page *page, int numpages,
-					int enable) { }
+static inline bool page_poisoning_enabled_static(void) { return false; }
+static inline void kernel_poison_pages(struct page *page, int numpages) { }
+static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
 #endif
 
 DECLARE_STATIC_KEY_FALSE(init_on_alloc);
mm/page_alloc.c +16 -3
index 44d596c9c764..fd7f9345adc0 100644
@@ -775,6 +775,17 @@ void init_mem_debugging_and_hardening(void)
 			static_branch_enable(&init_on_free);
 	}
 
+#ifdef CONFIG_PAGE_POISONING
+	/*
+	 * Page poisoning is debug page alloc for some arches. If
+	 * either of those options are enabled, enable poisoning.
+	 */
+	if (page_poisoning_enabled() ||
+	     (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
+	      debug_pagealloc_enabled()))
+		static_branch_enable(&_page_poisoning_enabled);
+#endif
+
 #ifdef CONFIG_DEBUG_PAGEALLOC
 	if (!debug_pagealloc_enabled())
 		return;
@@ -1260,7 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
 	if (want_init_on_free())
 		kernel_init_free_pages(page, 1 << order);
 
-	kernel_poison_pages(page, 1 << order, 0);
+	if (page_poisoning_enabled_static())
+		kernel_poison_pages(page, 1 << order);
 	/*
 	 * arch_free_page() can make the page's contents inaccessible.  s390
 	 * does this.  So nothing which can access the page's contents should
@@ -2206,7 +2218,7 @@ static inline int check_new_page(struct page *page)
 static inline bool free_pages_prezeroed(void)
 {
 	return (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
-		page_poisoning_enabled()) || want_init_on_free();
+		page_poisoning_enabled_static()) || want_init_on_free();
 }
 
 #ifdef CONFIG_DEBUG_VM
@@ -2269,7 +2281,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	if (debug_pagealloc_enabled_static())
 		kernel_map_pages(page, 1 << order, 1);
 	kasan_alloc_pages(page, order);
-	kernel_poison_pages(page, 1 << order, 1);
+	if (page_poisoning_enabled_static())
+		kernel_unpoison_pages(page, 1 << order);
 	set_page_owner(page, order, gfp_flags);
 }
 
mm/page_poison.c +7 -46

83 lines not on this page · raw message

Signature · 1 line
2.29.1
#3 lore raw

Re: [PATCH v2 2/5] mm, page_poison: use static key more efficiently

Quoted 18 lines · Vlastimil Babka

Commit 11c9c7edae06 ("mm/page_poison.c: replace bool variable with static key") changed page_poisoning_enabled() to a static key check. However, the function is not inlined, so each check still involves a function call with overhead not eliminated when page poisoning is disabled.

Analogically to how debug_pagealloc is handled, this patch converts page_poisoning_enabled() back to boolean check, and introduces page_poisoning_enabled_static() for fast paths. Both functions are inlined.

The function kernel_poison_pages() is also called unconditionally and does the static key check inside. Remove it from there and put it to callers. Also split it to two functions kernel_poison_pages() and kernel_unpoison_pages() instead of the confusing bool parameter.

Also optimize the check that enables page poisoning instead of debug_pagealloc for architectures without proper debug_pagealloc support. Move the check to init_mem_debugging_and_hardening() to enable a single static key instead of having two static branches in page_poisoning_enabled_static().

[...]

include/linux/mm.h extern int apply_to_existing_page_range(struct mm_struct *mm, · line 2884

Quoted 6 lines

+ * For use in fast paths after init_mem_debugging() has run, or when a
+ * false negative result is not harmful when called too early.
+ */

include/linux/mm.h +3 -0
+static inline bool page_poisoning_enabled_static(void)
+{
+	return (static_branch_unlikely(&_page_poisoning_enabled));

As already mentioned IIRC:

return static_branch_unlikely(&_page_poisoning_enabled);

Quoted 34 lines
include/linux/mm.h +4 -2
+}
  #else
  static inline bool page_poisoning_enabled(void) { return false; }
-static inline void kernel_poison_pages(struct page *page, int numpages,
-					int enable) { }
+static inline bool page_poisoning_enabled_static(void) { return false; }
+static inline void kernel_poison_pages(struct page *page, int numpages) { }
+static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
  #endif
 
  DECLARE_STATIC_KEY_FALSE(init_on_alloc);
mm/page_alloc.c +11 -0
index 44d596c9c764..fd7f9345adc0 100644
@@ -775,6 +775,17 @@ void init_mem_debugging_and_hardening(void)
  			static_branch_enable(&init_on_free);
  	}
 
+#ifdef CONFIG_PAGE_POISONING
+	/*
+	 * Page poisoning is debug page alloc for some arches. If
+	 * either of those options are enabled, enable poisoning.
+	 */
+	if (page_poisoning_enabled() ||
+	     (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
+	      debug_pagealloc_enabled()))
+		static_branch_enable(&_page_poisoning_enabled);
+#endif
+
  #ifdef CONFIG_DEBUG_PAGEALLOC
  	if (!debug_pagealloc_enabled())
  		return;

mm/page_alloc.c static __always_inline bool free_pages_prepare(struct page *page, · line 1275

Quoted 6 lines
mm/page_alloc.c +2 -1
@@ -1260,7 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
  	if (want_init_on_free())
  		kernel_init_free_pages(page, 1 << order);
 
-	kernel_poison_pages(page, 1 << order, 0);
+	if (page_poisoning_enabled_static())
+		kernel_poison_pages(page, 1 << order);

This would look much better by having kernel_poison_pages() simply be implemented in a header, where the static check is performed.

Take a look at how it's handled in mm/shuffle.h

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw

Re: [PATCH v2 2/5] mm, page_poison: use static key more efficiently

Quoted 22 lines · David Hildenbrand
Quoted 18 lines · Vlastimil Babka

Commit 11c9c7edae06 ("mm/page_poison.c: replace bool variable with static key") changed page_poisoning_enabled() to a static key check. However, the function is not inlined, so each check still involves a function call with overhead not eliminated when page poisoning is disabled.

Analogically to how debug_pagealloc is handled, this patch converts page_poisoning_enabled() back to boolean check, and introduces page_poisoning_enabled_static() for fast paths. Both functions are inlined.

The function kernel_poison_pages() is also called unconditionally and does the static key check inside. Remove it from there and put it to callers. Also split it to two functions kernel_poison_pages() and kernel_unpoison_pages() instead of the confusing bool parameter.

Also optimize the check that enables page poisoning instead of debug_pagealloc for architectures without proper debug_pagealloc support. Move the check to init_mem_debugging_and_hardening() to enable a single static key instead of having two static branches in page_poisoning_enabled_static().

[...]

Quoted 8 lines

Review

Quoted 6 lines

+ * For use in fast paths after init_mem_debugging() has run, or when a
+ * false negative result is not harmful when called too early.
+ */

(quoted diff) +3 -0
+static inline bool page_poisoning_enabled_static(void)
+{
+	return (static_branch_unlikely(&_page_poisoning_enabled));

As already mentioned IIRC:

Yes, it was, and I thought I fixed it. Guess not.

Quoted 8 lines

return static_branch_unlikely(&_page_poisoning_enabled);

Quoted 6 lines
(quoted diff) +1 -0
+}
@@ -1260,7 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
  	if (want_init_on_free())
  		kernel_init_free_pages(page, 1 << order);
 
Quoted 8 lines

Review static __always_inline bool free_pages_prepare(struct page *page, · line 1275

Quoted 3 lines
(quoted diff) +2 -1
-	kernel_poison_pages(page, 1 << order, 0);
+	if (page_poisoning_enabled_static())
+		kernel_poison_pages(page, 1 << order);

This would look much better by having kernel_poison_pages() simply be implemented in a header, where the static check is performed.

Take a look at how it's handled in mm/shuffle.h

Ok. Fixup below.

----8<----

From 7ce26ba61296f583f0f9089e7887f07424f25d2c Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Nov 2020 15:20:58 +0100
Subject: [PATCH] mm, page_poison: use static key more efficiently-fix

Non-functional cleanups, per David Hildenbrand.

---
 include/linux/mm.h | 16 +++++++++++++---
 mm/page_alloc.c    |  7 +++----
 mm/page_poison.c   |  4 ++--
 3 files changed, 18 insertions(+), 9 deletions(-)
include/linux/mm.h +13 -3
index 4d6dd9f44571..861b9392b5dc 100644
@@ -2867,8 +2867,8 @@ extern int apply_to_existing_page_range(struct mm_struct *mm,
 
 extern void init_mem_debugging_and_hardening(void);
 #ifdef CONFIG_PAGE_POISONING
-extern void kernel_poison_pages(struct page *page, int numpages);
-extern void kernel_unpoison_pages(struct page *page, int numpages);
+extern void __kernel_poison_pages(struct page *page, int numpages);
+extern void __kernel_unpoison_pages(struct page *page, int numpages);
 extern bool _page_poisoning_enabled_early;
 DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
 static inline bool page_poisoning_enabled(void)
@@ -2881,7 +2881,17 @@ static inline bool page_poisoning_enabled(void)
  */
 static inline bool page_poisoning_enabled_static(void)
 {
-	return (static_branch_unlikely(&_page_poisoning_enabled));
+	return static_branch_unlikely(&_page_poisoning_enabled);
+}
+static inline void kernel_poison_pages(struct page *page, int numpages)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_poison_pages(page, numpages);
+}
+static inline void kernel_unpoison_pages(struct page *page, int numpages)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_unpoison_pages(page, numpages);
 }
 #else
 static inline bool page_poisoning_enabled(void) { return false; }
mm/page_alloc.c +3 -4
index fd7f9345adc0..1388b5939551 100644
@@ -1271,8 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
 	if (want_init_on_free())
 		kernel_init_free_pages(page, 1 << order);
 
-	if (page_poisoning_enabled_static())
-		kernel_poison_pages(page, 1 << order);
+	kernel_poison_pages(page, 1 << order);
+
 	/*
 	 * arch_free_page() can make the page's contents inaccessible.  s390
 	 * does this.  So nothing which can access the page's contents should
@@ -2281,8 +2281,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	if (debug_pagealloc_enabled_static())
 		kernel_map_pages(page, 1 << order, 1);
 	kasan_alloc_pages(page, order);
-	if (page_poisoning_enabled_static())
-		kernel_unpoison_pages(page, 1 << order);
+	kernel_unpoison_pages(page, 1 << order);
 	set_page_owner(page, order, gfp_flags);
 }
 
mm/page_poison.c +2 -2
index dd7aeada036f..4d75fc9ccc7a 100644
@@ -30,7 +30,7 @@ static void poison_page(struct page *page)
 	kunmap_atomic(addr);
 }
 
-void kernel_poison_pages(struct page *page, int n)
+void __kernel_poison_pages(struct page *page, int n)
 {
 	int i;
 
@@ -89,7 +89,7 @@ static void unpoison_page(struct page *page)
 	kunmap_atomic(addr);
 }
 
-void kernel_unpoison_pages(struct page *page, int n)
+void __kernel_unpoison_pages(struct page *page, int n)
 {
 	int i;
 
Signature · 1 line
2.29.1
#5 lore raw

Re: [PATCH v2 2/5] mm, page_poison: use static key more efficiently

Quoted 143 lines · Vlastimil Babka
Quoted 22 lines · David Hildenbrand
Quoted 18 lines · Vlastimil Babka

Commit 11c9c7edae06 ("mm/page_poison.c: replace bool variable with static key") changed page_poisoning_enabled() to a static key check. However, the function is not inlined, so each check still involves a function call with overhead not eliminated when page poisoning is disabled.

Analogically to how debug_pagealloc is handled, this patch converts page_poisoning_enabled() back to boolean check, and introduces page_poisoning_enabled_static() for fast paths. Both functions are inlined.

The function kernel_poison_pages() is also called unconditionally and does the static key check inside. Remove it from there and put it to callers. Also split it to two functions kernel_poison_pages() and kernel_unpoison_pages() instead of the confusing bool parameter.

Also optimize the check that enables page poisoning instead of debug_pagealloc for architectures without proper debug_pagealloc support. Move the check to init_mem_debugging_and_hardening() to enable a single static key instead of having two static branches in page_poisoning_enabled_static().

[...]

Quoted 8 lines

Review

Quoted 6 lines

+ * For use in fast paths after init_mem_debugging() has run, or when a
+ * false negative result is not harmful when called too early.
+ */

(quoted diff) +3 -0
+static inline bool page_poisoning_enabled_static(void)
+{
+	return (static_branch_unlikely(&_page_poisoning_enabled));

As already mentioned IIRC:

Yes, it was, and I thought I fixed it. Guess not.

Quoted 8 lines

return static_branch_unlikely(&_page_poisoning_enabled);

Quoted 6 lines
mm/page_alloc.c +1 -0
+}
@@ -1260,7 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
   	if (want_init_on_free())
   		kernel_init_free_pages(page, 1 << order);
 
Quoted 8 lines

Review static __always_inline bool free_pages_prepare(struct page *page, · line 1275

Quoted 3 lines
(quoted diff) +2 -1
-	kernel_poison_pages(page, 1 << order, 0);
+	if (page_poisoning_enabled_static())
+		kernel_poison_pages(page, 1 << order);

This would look much better by having kernel_poison_pages() simply be implemented in a header, where the static check is performed.

Take a look at how it's handled in mm/shuffle.h

Ok. Fixup below.

----8<----

From 7ce26ba61296f583f0f9089e7887f07424f25d2c Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Nov 2020 15:20:58 +0100
Subject: [PATCH] mm, page_poison: use static key more efficiently-fix

Non-functional cleanups, per David Hildenbrand.

---
   include/linux/mm.h | 16 +++++++++++++---
   mm/page_alloc.c    |  7 +++----
   mm/page_poison.c   |  4 ++--
   3 files changed, 18 insertions(+), 9 deletions(-)
include/linux/mm.h +13 -3
index 4d6dd9f44571..861b9392b5dc 100644
@@ -2867,8 +2867,8 @@ extern int apply_to_existing_page_range(struct mm_struct *mm,
 
   extern void init_mem_debugging_and_hardening(void);
   #ifdef CONFIG_PAGE_POISONING
-extern void kernel_poison_pages(struct page *page, int numpages);
-extern void kernel_unpoison_pages(struct page *page, int numpages);
+extern void __kernel_poison_pages(struct page *page, int numpages);
+extern void __kernel_unpoison_pages(struct page *page, int numpages);
   extern bool _page_poisoning_enabled_early;
   DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
   static inline bool page_poisoning_enabled(void)
@@ -2881,7 +2881,17 @@ static inline bool page_poisoning_enabled(void)
    */
   static inline bool page_poisoning_enabled_static(void)
   {
-	return (static_branch_unlikely(&_page_poisoning_enabled));
+	return static_branch_unlikely(&_page_poisoning_enabled);
+}
+static inline void kernel_poison_pages(struct page *page, int numpages)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_poison_pages(page, numpages);
+}
+static inline void kernel_unpoison_pages(struct page *page, int numpages)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_unpoison_pages(page, numpages);
   }
   #else
   static inline bool page_poisoning_enabled(void) { return false; }
mm/page_alloc.c +3 -4
index fd7f9345adc0..1388b5939551 100644
@@ -1271,8 +1271,8 @@ static __always_inline bool free_pages_prepare(struct page *page,
   	if (want_init_on_free())
   		kernel_init_free_pages(page, 1 << order);
 
-	if (page_poisoning_enabled_static())
-		kernel_poison_pages(page, 1 << order);
+	kernel_poison_pages(page, 1 << order);
+
   	/*
   	 * arch_free_page() can make the page's contents inaccessible.  s390
   	 * does this.  So nothing which can access the page's contents should
@@ -2281,8 +2281,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
   	if (debug_pagealloc_enabled_static())
   		kernel_map_pages(page, 1 << order, 1);
   	kasan_alloc_pages(page, order);
-	if (page_poisoning_enabled_static())
-		kernel_unpoison_pages(page, 1 << order);
+	kernel_unpoison_pages(page, 1 << order);
   	set_page_owner(page, order, gfp_flags);
   }
 
mm/page_poison.c +1 -1
index dd7aeada036f..4d75fc9ccc7a 100644
@@ -30,7 +30,7 @@ static void poison_page(struct page *page)
   	kunmap_atomic(addr);
   }
 
-void kernel_poison_pages(struct page *page, int n)
+void __kernel_poison_pages(struct page *page, int n)
   {
   	int i;
 

mm/page_poison.c static void unpoison_page(struct page *page) · line 95

Quoted 8 lines
mm/page_poison.c +1 -1
@@ -89,7 +89,7 @@ static void unpoison_page(struct page *page)
   	kunmap_atomic(addr);
   }
 
-void kernel_unpoison_pages(struct page *page, int n)
+void __kernel_unpoison_pages(struct page *page, int n)
   {
   	int i;
 

LGTM.

Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw

PATCH v2 3/5 kernel/power: allow hibernation with page_poison sanity checking

In reply to Vlastimil Babka

Page poisoning used to be incompatible with hibernation, as the state of poisoned pages was lost after resume, thus enabling CONFIG_HIBERNATION forces CONFIG_PAGE_POISONING_NO_SANITY. For the same reason, the poisoning with zeroes variant CONFIG_PAGE_POISONING_ZERO used to disable hibernation. The latter restriction was removed by commit 1ad1410f632d ("PM / Hibernate: allow hibernation with PAGE_POISONING_ZERO") and similarly for init_on_free by commit 18451f9f9e58 ("PM: hibernate: fix crashes with init_on_free=1") by making sure free pages are cleared after resume.

We can use the same mechanism to instead poison free pages with PAGE_POISON after resume. This covers both zero and 0xAA patterns. Thus we can remove the Kconfig restriction that disables page poison sanity checking when hibernation is enabled.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Pavel Machek <[email protected]>
---
 kernel/power/hibernate.c |  2 +-
 kernel/power/power.h     |  2 +-
 kernel/power/snapshot.c  | 14 ++++++++++----
 mm/Kconfig.debug         |  1 -
 4 files changed, 12 insertions(+), 7 deletions(-)
kernel/power/hibernate.c +1 -1
index 2fc7d509a34f..da0b41914177 100644
@@ -326,7 +326,7 @@ static int create_image(int platform_mode)
 
 	if (!in_suspend) {
 		events_check_enabled = false;
-		clear_free_pages();
+		clear_or_poison_free_pages();
 	}
 
 	platform_leave(platform_mode);
kernel/power/power.h +1 -1
index 24f12d534515..778bf431ec02 100644
@@ -106,7 +106,7 @@ extern int create_basic_memory_bitmaps(void);
 extern void free_basic_memory_bitmaps(void);
 extern int hibernate_preallocate_memory(void);
 
-extern void clear_free_pages(void);
+extern void clear_or_poison_free_pages(void);
 
 /**
  *	Auxiliary structure used for reading the snapshot image data and
kernel/power/snapshot.c +10 -4
index 46b1804c1ddf..6b1c84afa891 100644
@@ -1144,7 +1144,7 @@ void free_basic_memory_bitmaps(void)
 	pr_debug("Basic memory bitmaps freed\n");
 }
 
-void clear_free_pages(void)
+void clear_or_poison_free_pages(void)
 {
 	struct memory_bitmap *bm = free_pages_map;
 	unsigned long pfn;
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
 	if (WARN_ON(!(free_pages_map)))
 		return;
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+	if (page_poisoning_enabled() || want_init_on_free()) {
 		memory_bm_position_reset(bm);
 		pfn = memory_bm_next_pfn(bm);
 		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn))
-				clear_highpage(pfn_to_page(pfn));
+			if (pfn_valid(pfn)) {
+				struct page *page = pfn_to_page(pfn);
+				if (page_poisoning_enabled_static())
+					kernel_poison_pages(page, 1);
+				else if (want_init_on_free())
+					clear_highpage(page);
+
+			}
 
 			pfn = memory_bm_next_pfn(bm);
 		}
mm/Kconfig.debug +0 -1
index 864f129f1937..c57786ad5be9 100644
@@ -64,7 +64,6 @@ config PAGE_OWNER
 
 config PAGE_POISONING
 	bool "Poison pages after freeing"
-	select PAGE_POISONING_NO_SANITY if HIBERNATION
 	help
 	  Fill the pages with poison patterns after free_pages() and verify
 	  the patterns before alloc_pages. The filling of the memory helps
Signature · 1 line
2.29.1
maintainer Acked-by on v2 3/5
#7 lore raw

Re: [PATCH v2 3/5] kernel/power: allow hibernation with page_poison sanity checking

Quoted 15 lines · Vlastimil Babka

Page poisoning used to be incompatible with hibernation, as the state of poisoned pages was lost after resume, thus enabling CONFIG_HIBERNATION forces CONFIG_PAGE_POISONING_NO_SANITY. For the same reason, the poisoning with zeroes variant CONFIG_PAGE_POISONING_ZERO used to disable hibernation. The latter restriction was removed by commit 1ad1410f632d ("PM / Hibernate: allow hibernation with PAGE_POISONING_ZERO") and similarly for init_on_free by commit 18451f9f9e58 ("PM: hibernate: fix crashes with init_on_free=1") by making sure free pages are cleared after resume.

We can use the same mechanism to instead poison free pages with PAGE_POISON after resume. This covers both zero and 0xAA patterns. Thus we can remove the Kconfig restriction that disables page poison sanity checking when hibernation is enabled.

Quoted 5 lines
Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Pavel Machek <[email protected]>

for the hibernation part and I'm assuming that this patch will be routed through the mm tree.

Quoted 83 lines
---
 kernel/power/hibernate.c |  2 +-
 kernel/power/power.h     |  2 +-
 kernel/power/snapshot.c  | 14 ++++++++++----
 mm/Kconfig.debug         |  1 -
 4 files changed, 12 insertions(+), 7 deletions(-)
kernel/power/hibernate.c +1 -1
index 2fc7d509a34f..da0b41914177 100644
@@ -326,7 +326,7 @@ static int create_image(int platform_mode)
 
        if (!in_suspend) {
                events_check_enabled = false;
-               clear_free_pages();
+               clear_or_poison_free_pages();
        }
 
        platform_leave(platform_mode);
kernel/power/power.h +1 -1
index 24f12d534515..778bf431ec02 100644
@@ -106,7 +106,7 @@ extern int create_basic_memory_bitmaps(void);
 extern void free_basic_memory_bitmaps(void);
 extern int hibernate_preallocate_memory(void);
 
-extern void clear_free_pages(void);
+extern void clear_or_poison_free_pages(void);
 
 /**
  *     Auxiliary structure used for reading the snapshot image data and
kernel/power/snapshot.c +10 -4
index 46b1804c1ddf..6b1c84afa891 100644
@@ -1144,7 +1144,7 @@ void free_basic_memory_bitmaps(void)
        pr_debug("Basic memory bitmaps freed\n");
 }
 
-void clear_free_pages(void)
+void clear_or_poison_free_pages(void)
 {
        struct memory_bitmap *bm = free_pages_map;
        unsigned long pfn;
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
        if (WARN_ON(!(free_pages_map)))
                return;
 
-       if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+       if (page_poisoning_enabled() || want_init_on_free()) {
                memory_bm_position_reset(bm);
                pfn = memory_bm_next_pfn(bm);
                while (pfn != BM_END_OF_MAP) {
-                       if (pfn_valid(pfn))
-                               clear_highpage(pfn_to_page(pfn));
+                       if (pfn_valid(pfn)) {
+                               struct page *page = pfn_to_page(pfn);
+                               if (page_poisoning_enabled_static())
+                                       kernel_poison_pages(page, 1);
+                               else if (want_init_on_free())
+                                       clear_highpage(page);
+
+                       }
 
                        pfn = memory_bm_next_pfn(bm);
                }
mm/Kconfig.debug +0 -2
index 864f129f1937..c57786ad5be9 100644
@@ -64,7 +64,6 @@ config PAGE_OWNER
 
 config PAGE_POISONING
        bool "Poison pages after freeing"
-       select PAGE_POISONING_NO_SANITY if HIBERNATION
        help
          Fill the pages with poison patterns after free_pages() and verify
          the patterns before alloc_pages. The filling of the memory helps
--

2.29.1

#8 lore raw

Re: [PATCH v2 3/5] kernel/power: allow hibernation with page_poison sanity checking

In reply to Vlastimil Babka
Quoted 68 lines · Vlastimil Babka

Page poisoning used to be incompatible with hibernation, as the state of poisoned pages was lost after resume, thus enabling CONFIG_HIBERNATION forces CONFIG_PAGE_POISONING_NO_SANITY. For the same reason, the poisoning with zeroes variant CONFIG_PAGE_POISONING_ZERO used to disable hibernation. The latter restriction was removed by commit 1ad1410f632d ("PM / Hibernate: allow hibernation with PAGE_POISONING_ZERO") and similarly for init_on_free by commit 18451f9f9e58 ("PM: hibernate: fix crashes with init_on_free=1") by making sure free pages are cleared after resume.

We can use the same mechanism to instead poison free pages with PAGE_POISON after resume. This covers both zero and 0xAA patterns. Thus we can remove the Kconfig restriction that disables page poison sanity checking when hibernation is enabled.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Pavel Machek <[email protected]>
---
  kernel/power/hibernate.c |  2 +-
  kernel/power/power.h     |  2 +-
  kernel/power/snapshot.c  | 14 ++++++++++----
  mm/Kconfig.debug         |  1 -
  4 files changed, 12 insertions(+), 7 deletions(-)
kernel/power/hibernate.c +1 -1
index 2fc7d509a34f..da0b41914177 100644
@@ -326,7 +326,7 @@ static int create_image(int platform_mode)
 
  	if (!in_suspend) {
  		events_check_enabled = false;
-		clear_free_pages();
+		clear_or_poison_free_pages();
  	}
 
  	platform_leave(platform_mode);
kernel/power/power.h +1 -1
index 24f12d534515..778bf431ec02 100644
@@ -106,7 +106,7 @@ extern int create_basic_memory_bitmaps(void);
  extern void free_basic_memory_bitmaps(void);
  extern int hibernate_preallocate_memory(void);
 
-extern void clear_free_pages(void);
+extern void clear_or_poison_free_pages(void);
 
  /**
   *	Auxiliary structure used for reading the snapshot image data and
kernel/power/snapshot.c +1 -1
index 46b1804c1ddf..6b1c84afa891 100644
@@ -1144,7 +1144,7 @@ void free_basic_memory_bitmaps(void)
  	pr_debug("Basic memory bitmaps freed\n");
  }
 
-void clear_free_pages(void)
+void clear_or_poison_free_pages(void)
  {
  	struct memory_bitmap *bm = free_pages_map;
  	unsigned long pfn;
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
  	if (WARN_ON(!(free_pages_map)))
  		return;

kernel/power/snapshot.c void clear_free_pages(void) · line 1160

Quoted 10 lines
kernel/power/snapshot.c +3 -3
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+	if (page_poisoning_enabled() || want_init_on_free()) {
  		memory_bm_position_reset(bm);
  		pfn = memory_bm_next_pfn(bm);
  		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn))
-				clear_highpage(pfn_to_page(pfn));
+			if (pfn_valid(pfn)) {
+				struct page *page = pfn_to_page(pfn);

^ empty line missing. And at least I prefer to declare all variables in the function header.

I'd even suggest to move this into a separate function like

clear_or_poison_free_page(struct page *page)

Quoted 15 lines
kernel/power/snapshot.c +6 -0
+				if (page_poisoning_enabled_static())
+					kernel_poison_pages(page, 1);
+				else if (want_init_on_free())
+					clear_highpage(page);
+
+			}
 
  			pfn = memory_bm_next_pfn(bm);
  		}
mm/Kconfig.debug +0 -0
index 864f129f1937..c57786ad5be9 100644

mm/Kconfig.debug config PAGE_OWNER · line 69

Quoted 7 lines
mm/Kconfig.debug +0 -1
@@ -64,7 +64,6 @@ config PAGE_OWNER
 
  config PAGE_POISONING
  	bool "Poison pages after freeing"
-	select PAGE_POISONING_NO_SANITY if HIBERNATION
  	help
  	  Fill the pages with poison patterns after free_pages() and verify
  	  the patterns before alloc_pages. The filling of the memory helps

Unless I am missing something important, this should work! Thanks!

Signature · 3 lines
Thanks,

David / dhildenb
#9 lore raw

Re: [PATCH v2 3/5] kernel/power: allow hibernation with page_poison sanity checking

On 11/11/20 4:42 PM, David Hildenbrand wrote:
...

Quoted 17 lines

Review void clear_free_pages(void) · line 1160

Quoted 13 lines
(quoted diff) +3 -3
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
  	if (WARN_ON(!(free_pages_map)))
  		return;
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+	if (page_poisoning_enabled() || want_init_on_free()) {
  		memory_bm_position_reset(bm);
  		pfn = memory_bm_next_pfn(bm);
  		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn))
-				clear_highpage(pfn_to_page(pfn));
+			if (pfn_valid(pfn)) {
+				struct page *page = pfn_to_page(pfn);

^ empty line missing. And at least I prefer to declare all variables in the function header.

Quoted 5 lines

I'd even suggest to move this into a separate function like

clear_or_poison_free_page(struct page *page)

Ok, fixup below.

----8<----
From cae1e8ccfa57c28ed1b2f5f8a47319b86cbdcfbf Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Nov 2020 15:33:07 +0100
Subject: [PATCH] kernel/power: allow hibernation with page_poison sanity
checking-fix

Adapt to __kernel_unpoison_pages fixup. Split out clear_or_poison_free_page() per David Hildenbrand.

---
 include/linux/mm.h      |  1 +
 kernel/power/snapshot.c | 18 ++++++++++--------
 2 files changed, 11 insertions(+), 8 deletions(-)
include/linux/mm.h +1 -0
index 861b9392b5dc..d4cfb06a611e 100644
@@ -2896,6 +2896,7 @@ static inline void kernel_unpoison_pages(struct page *page, int numpages)
 #else
 static inline bool page_poisoning_enabled(void) { return false; }
 static inline bool page_poisoning_enabled_static(void) { return false; }
+static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
 static inline void kernel_poison_pages(struct page *page, int numpages) { }
 static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
 #endif
kernel/power/snapshot.c +10 -8
index 6b1c84afa891..a3491b29c5cc 100644
@@ -1144,6 +1144,14 @@ void free_basic_memory_bitmaps(void)
 	pr_debug("Basic memory bitmaps freed\n");
 }
 
+static void clear_or_poison_free_page(struct page *page)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_poison_pages(page, 1);
+	else if (want_init_on_free())
+		clear_highpage(page);
+}
+
 void clear_or_poison_free_pages(void)
 {
 	struct memory_bitmap *bm = free_pages_map;
@@ -1156,14 +1164,8 @@ void clear_or_poison_free_pages(void)
 		memory_bm_position_reset(bm);
 		pfn = memory_bm_next_pfn(bm);
 		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn)) {
-				struct page *page = pfn_to_page(pfn);
-				if (page_poisoning_enabled_static())
-					kernel_poison_pages(page, 1);
-				else if (want_init_on_free())
-					clear_highpage(page);
-
-			}
+			if (pfn_valid(pfn))
+				clear_or_poison_free_page(pfn_to_page(pfn));
 
 			pfn = memory_bm_next_pfn(bm);
 		}
Signature · 1 line
2.29.1
maintainer Acked-by
#10 lore raw

Re: [PATCH v2 3/5] kernel/power: allow hibernation with page_poison sanity checking

Quoted 35 lines · Vlastimil Babka

On 11/11/20 4:42 PM, David Hildenbrand wrote:
...

Quoted 17 lines

Review void clear_free_pages(void) · line 1160

Quoted 13 lines
(quoted diff) +3 -3
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
     if (WARN_ON(!(free_pages_map)))
             return;
 
-    if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+    if (page_poisoning_enabled() || want_init_on_free()) {
             memory_bm_position_reset(bm);
             pfn = memory_bm_next_pfn(bm);
             while (pfn != BM_END_OF_MAP) {
-                    if (pfn_valid(pfn))
-                            clear_highpage(pfn_to_page(pfn));
+                    if (pfn_valid(pfn)) {
+                            struct page *page = pfn_to_page(pfn);

^ empty line missing. And at least I prefer to declare all variables in the function header.

Quoted 5 lines

I'd even suggest to move this into a separate function like

clear_or_poison_free_page(struct page *page)

Ok, fixup below.

----8<----
From cae1e8ccfa57c28ed1b2f5f8a47319b86cbdcfbf Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Nov 2020 15:33:07 +0100
Subject: [PATCH] kernel/power: allow hibernation with page_poison sanity
checking-fix

Quoted 4 lines

Adapt to __kernel_unpoison_pages fixup. Split out clear_or_poison_free_page() per David Hildenbrand.

Still

Quoted 57 lines
---
  include/linux/mm.h      |  1 +
  kernel/power/snapshot.c | 18 ++++++++++--------
  2 files changed, 11 insertions(+), 8 deletions(-)
include/linux/mm.h +1 -0
index 861b9392b5dc..d4cfb06a611e 100644
@@ -2896,6 +2896,7 @@ static inline void kernel_unpoison_pages(struct page *page, int numpages)
  #else
  static inline bool page_poisoning_enabled(void) { return false; }
  static inline bool page_poisoning_enabled_static(void) { return false; }
+static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
  static inline void kernel_poison_pages(struct page *page, int numpages) { }
  static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
  #endif
kernel/power/snapshot.c +10 -9
index 6b1c84afa891..a3491b29c5cc 100644
@@ -1144,6 +1144,14 @@ void free_basic_memory_bitmaps(void)
        pr_debug("Basic memory bitmaps freed\n");
  }
 
+static void clear_or_poison_free_page(struct page *page)
+{
+       if (page_poisoning_enabled_static())
+               __kernel_poison_pages(page, 1);
+       else if (want_init_on_free())
+               clear_highpage(page);
+}
+
  void clear_or_poison_free_pages(void)
  {
        struct memory_bitmap *bm = free_pages_map;
@@ -1156,14 +1164,8 @@ void clear_or_poison_free_pages(void)
                memory_bm_position_reset(bm);
                pfn = memory_bm_next_pfn(bm);
                while (pfn != BM_END_OF_MAP) {
-                       if (pfn_valid(pfn)) {
-                               struct page *page = pfn_to_page(pfn);
-                               if (page_poisoning_enabled_static())
-                                       kernel_poison_pages(page, 1);
-                               else if (want_init_on_free())
-                                       clear_highpage(page);
-
-                       }
+                       if (pfn_valid(pfn))
+                               clear_or_poison_free_page(pfn_to_page(pfn));
 
                        pfn = memory_bm_next_pfn(bm);
                }
--

2.29.1

#11 lore raw

Re: [PATCH v2 3/5] kernel/power: allow hibernation with page_poison sanity checking

In reply to Vlastimil Babka
Quoted 82 lines · Vlastimil Babka

On 11/11/20 4:42 PM, David Hildenbrand wrote:
...

Quoted 17 lines

Review void clear_free_pages(void) · line 1160

Quoted 13 lines
(quoted diff) +3 -3
@@ -1152,12 +1152,18 @@ void clear_free_pages(void)
   	if (WARN_ON(!(free_pages_map)))
   		return;
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) {
+	if (page_poisoning_enabled() || want_init_on_free()) {
   		memory_bm_position_reset(bm);
   		pfn = memory_bm_next_pfn(bm);
   		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn))
-				clear_highpage(pfn_to_page(pfn));
+			if (pfn_valid(pfn)) {
+				struct page *page = pfn_to_page(pfn);

^ empty line missing. And at least I prefer to declare all variables in the function header.

Quoted 5 lines

I'd even suggest to move this into a separate function like

clear_or_poison_free_page(struct page *page)

Ok, fixup below.

----8<----
From cae1e8ccfa57c28ed1b2f5f8a47319b86cbdcfbf Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Nov 2020 15:33:07 +0100
Subject: [PATCH] kernel/power: allow hibernation with page_poison sanity
checking-fix

Adapt to __kernel_unpoison_pages fixup. Split out clear_or_poison_free_page() per David Hildenbrand.

---
   include/linux/mm.h      |  1 +
   kernel/power/snapshot.c | 18 ++++++++++--------
   2 files changed, 11 insertions(+), 8 deletions(-)
include/linux/mm.h +1 -0
index 861b9392b5dc..d4cfb06a611e 100644
@@ -2896,6 +2896,7 @@ static inline void kernel_unpoison_pages(struct page *page, int numpages)
   #else
   static inline bool page_poisoning_enabled(void) { return false; }
   static inline bool page_poisoning_enabled_static(void) { return false; }
+static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
   static inline void kernel_poison_pages(struct page *page, int numpages) { }
   static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
   #endif
kernel/power/snapshot.c +8 -3
index 6b1c84afa891..a3491b29c5cc 100644
@@ -1144,6 +1144,14 @@ void free_basic_memory_bitmaps(void)
   	pr_debug("Basic memory bitmaps freed\n");
   }
 
+static void clear_or_poison_free_page(struct page *page)
+{
+	if (page_poisoning_enabled_static())
+		__kernel_poison_pages(page, 1);
+	else if (want_init_on_free())
+		clear_highpage(page);
+}
+
   void clear_or_poison_free_pages(void)
   {
   	struct memory_bitmap *bm = free_pages_map;
@@ -1156,14 +1164,8 @@ void clear_or_poison_free_pages(void)
   		memory_bm_position_reset(bm);
   		pfn = memory_bm_next_pfn(bm);
   		while (pfn != BM_END_OF_MAP) {
-			if (pfn_valid(pfn)) {
-				struct page *page = pfn_to_page(pfn);
-				if (page_poisoning_enabled_static())

kernel/power/snapshot.c void clear_or_poison_free_pages(void) · line 1171

Quoted 10 lines
kernel/power/snapshot.c +2 -5
-					kernel_poison_pages(page, 1);
-				else if (want_init_on_free())
-					clear_highpage(page);
-
-			}
+			if (pfn_valid(pfn))
+				clear_or_poison_free_page(pfn_to_page(pfn));
 
   			pfn = memory_bm_next_pfn(bm);
   		}

LGTM, thanks!

Signature · 3 lines
Thanks,

David / dhildenb
#12 lore raw

PATCH v2 4/5 mm, page_poison: remove CONFIG_PAGE_POISONING_NO_SANITY

In reply to Vlastimil Babka

CONFIG_PAGE_POISONING_NO_SANITY skips the check on page alloc whether the poison pattern was corrupted, suggesting a use-after-free. The motivation to introduce it in commit 8823b1dbc05f ("mm/page_poison.c: enable PAGE_POISONING as a separate option") was to simply sanitize freed pages, optimally together with CONFIG_PAGE_POISONING_ZERO.

These days we have an init_on_free=1 boot option, which makes this use case of page poisoning redundant. For sanitizing, writing zeroes is sufficient, there is pretty much no benefit from writing the 0xAA poison pattern to freed pages, without checking it back on alloc. Thus, remove this option and suggest init_on_free instead in the main config's help.

---
 drivers/virtio/virtio_balloon.c |  4 +---
 mm/Kconfig.debug                | 15 ++++-----------
 mm/page_poison.c                |  3 ---
 3 files changed, 5 insertions(+), 17 deletions(-)
drivers/virtio/virtio_balloon.c +1 -3
index e53faed6ba93..8985fc2cea86 100644
@@ -1114,9 +1114,7 @@ static int virtballoon_validate(struct virtio_device *vdev)
 	 * page reporting as it could potentially change the contents
 	 * of our free pages.
 	 */
-	if (!want_init_on_free() &&
-	    (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY) ||
-	     !page_poisoning_enabled_static()))
+	if (!want_init_on_free() && !page_poisoning_enabled_static())
 		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
 	else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
 		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
mm/Kconfig.debug +4 -11
index c57786ad5be9..14e29fe5bfa6 100644
@@ -74,18 +74,11 @@ config PAGE_POISONING
 	  Note that "poison" here is not the same thing as the "HWPoison"
 	  for CONFIG_MEMORY_FAILURE. This is software poisoning only.
 
-	  If unsure, say N
+	  If you are only interested in sanitization of freed pages without
+	  checking the poison pattern on alloc, you can boot the kernel with
+	  "init_on_free=1" instead of enabling this.
 
-config PAGE_POISONING_NO_SANITY
-	depends on PAGE_POISONING
-	bool "Only poison, don't sanity check"
-	help
-	   Skip the sanity checking on alloc, only fill the pages with
-	   poison on free. This reduces some of the overhead of the
-	   poisoning feature.
-
-	   If you are only interested in sanitization, say Y. Otherwise
-	   say N.
+	  If unsure, say N
 
 config PAGE_POISONING_ZERO
 	bool "Use zero for poisoning instead of debugging value"
mm/page_poison.c +0 -3
index dd7aeada036f..084fc3ff4c15 100644
@@ -51,9 +51,6 @@ static void check_poison_mem(unsigned char *mem, size_t bytes)
 	unsigned char *start;
 	unsigned char *end;
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
-		return;
-
 	start = memchr_inv(mem, PAGE_POISON, bytes);
 	if (!start)
 		return;
Signature · 1 line
2.29.1
Acked-by on v2 4/5
#13 lore raw

Re: [PATCH v2 4/5] mm, page_poison: remove CONFIG_PAGE_POISONING_NO_SANITY

Quoted 67 lines · Vlastimil Babka

CONFIG_PAGE_POISONING_NO_SANITY skips the check on page alloc whether the poison pattern was corrupted, suggesting a use-after-free. The motivation to introduce it in commit 8823b1dbc05f ("mm/page_poison.c: enable PAGE_POISONING as a separate option") was to simply sanitize freed pages, optimally together with CONFIG_PAGE_POISONING_ZERO.

These days we have an init_on_free=1 boot option, which makes this use case of page poisoning redundant. For sanitizing, writing zeroes is sufficient, there is pretty much no benefit from writing the 0xAA poison pattern to freed pages, without checking it back on alloc. Thus, remove this option and suggest init_on_free instead in the main config's help.

---
  drivers/virtio/virtio_balloon.c |  4 +---
  mm/Kconfig.debug                | 15 ++++-----------
  mm/page_poison.c                |  3 ---
  3 files changed, 5 insertions(+), 17 deletions(-)
drivers/virtio/virtio_balloon.c +1 -3
index e53faed6ba93..8985fc2cea86 100644
@@ -1114,9 +1114,7 @@ static int virtballoon_validate(struct virtio_device *vdev)
  	 * page reporting as it could potentially change the contents
  	 * of our free pages.
  	 */
-	if (!want_init_on_free() &&
-	    (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY) ||
-	     !page_poisoning_enabled_static()))
+	if (!want_init_on_free() && !page_poisoning_enabled_static())
  		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
  	else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
  		__virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
mm/Kconfig.debug +4 -11
index c57786ad5be9..14e29fe5bfa6 100644
@@ -74,18 +74,11 @@ config PAGE_POISONING
  	  Note that "poison" here is not the same thing as the "HWPoison"
  	  for CONFIG_MEMORY_FAILURE. This is software poisoning only.
 
-	  If unsure, say N
+	  If you are only interested in sanitization of freed pages without
+	  checking the poison pattern on alloc, you can boot the kernel with
+	  "init_on_free=1" instead of enabling this.
 
-config PAGE_POISONING_NO_SANITY
-	depends on PAGE_POISONING
-	bool "Only poison, don't sanity check"
-	help
-	   Skip the sanity checking on alloc, only fill the pages with
-	   poison on free. This reduces some of the overhead of the
-	   poisoning feature.
-
-	   If you are only interested in sanitization, say Y. Otherwise
-	   say N.
+	  If unsure, say N
 
  config PAGE_POISONING_ZERO
  	bool "Use zero for poisoning instead of debugging value"
mm/page_poison.c +0 -0
index dd7aeada036f..084fc3ff4c15 100644

mm/page_poison.c static void check_poison_mem(unsigned char *mem, size_t bytes) · line 56

Quoted 9 lines
mm/page_poison.c +0 -3
@@ -51,9 +51,6 @@ static void check_poison_mem(unsigned char *mem, size_t bytes)
  	unsigned char *start;
  	unsigned char *end;
 
-	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
-		return;
-
  	start = memchr_inv(mem, PAGE_POISON, bytes);
  	if (!start)
  		return;

This clearly simplifies things.

Signature · 3 lines
Thanks,

David / dhildenb
#14 lore raw

PATCH v2 5/5 mm, page_poison: remove CONFIG_PAGE_POISONING_ZERO

In reply to Vlastimil Babka

CONFIG_PAGE_POISONING_ZERO uses the zero pattern instead of 0xAA. It was introduced by commit 1414c7f4f7d7 ("mm/page_poisoning.c: allow for zero poisoning"), noting that using zeroes retains the benefit of sanitizing content of freed pages, with the benefit of not having to zero them again on alloc, and the downside of making some forms of corruption (stray writes of NULLs) harder to detect than with the 0xAA pattern. Together with CONFIG_PAGE_POISONING_NO_SANITY it made possible to sanitize the contents on free without checking it back on alloc.

These days we have the init_on_free() option to achieve sanitization with zeroes and to save clearing on alloc (and without checking on alloc). Arguably if someone does choose to check the poison for corruption on alloc, the savings of not clearing the page are secondary, and it makes sense to always use the 0xAA poison pattern. Thus, remove the CONFIG_PAGE_POISONING_ZERO option for being redundant.

---
 include/linux/poison.h       |  4 ----
 mm/Kconfig.debug             | 12 ------------
 mm/page_alloc.c              |  8 +-------
 tools/include/linux/poison.h |  6 +-----
 4 files changed, 2 insertions(+), 28 deletions(-)
include/linux/poison.h +0 -4
index dc8ae5d8db03..aff1c9250c82 100644
@@ -27,11 +27,7 @@
 #define TIMER_ENTRY_STATIC	((void *) 0x300 + POISON_POINTER_DELTA)
 
 /********** mm/page_poison.c **********/
-#ifdef CONFIG_PAGE_POISONING_ZERO
-#define PAGE_POISON 0x00
-#else
 #define PAGE_POISON 0xaa
-#endif
 
 /********** mm/page_alloc.c ************/
 
mm/Kconfig.debug +0 -12
index 14e29fe5bfa6..1e73717802f8 100644
@@ -80,18 +80,6 @@ config PAGE_POISONING
 
 	  If unsure, say N
 
-config PAGE_POISONING_ZERO
-	bool "Use zero for poisoning instead of debugging value"
-	depends on PAGE_POISONING
-	help
-	   Instead of using the existing poison value, fill the pages with
-	   zeros. This makes it harder to detect when errors are occurring
-	   due to sanitization but the zeroing at free means that it is
-	   no longer necessary to write zeros when GFP_ZERO is used on
-	   allocation.
-
-	   If unsure, say N
-
 config DEBUG_PAGE_REF
 	bool "Enable tracepoint to track down page reference manipulation"
 	depends on DEBUG_KERNEL
mm/page_alloc.c +1 -7
index fd7f9345adc0..2054320d3c1f 100644
@@ -2215,12 +2215,6 @@ static inline int check_new_page(struct page *page)
 	return 1;
 }
 
-static inline bool free_pages_prezeroed(void)
-{
-	return (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
-		page_poisoning_enabled_static()) || want_init_on_free();
-}
-
 #ifdef CONFIG_DEBUG_VM
 /*
  * With DEBUG_VM enabled, order-0 pages are checked for expected state when
@@ -2291,7 +2285,7 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags
 {
 	post_alloc_hook(page, order, gfp_flags);
 
-	if (!free_pages_prezeroed() && want_init_on_alloc(gfp_flags))
+	if (!want_init_on_free() && want_init_on_alloc(gfp_flags))
 		kernel_init_free_pages(page, 1 << order);
 
 	if (order && (gfp_flags & __GFP_COMP))
tools/include/linux/poison.h +1 -5
index d29725769107..2e6338ac5eed 100644
@@ -35,12 +35,8 @@
  */
 #define TIMER_ENTRY_STATIC	((void *) 0x300 + POISON_POINTER_DELTA)
 
-/********** mm/debug-pagealloc.c **********/
-#ifdef CONFIG_PAGE_POISONING_ZERO
-#define PAGE_POISON 0x00
-#else
+/********** mm/page_poison.c **********/
 #define PAGE_POISON 0xaa
-#endif
 
 /********** mm/page_alloc.c ************/
 
Signature · 1 line
2.29.1
Acked-by on v2 5/5
#15 lore raw

Re: [PATCH v2 5/5] mm, page_poison: remove CONFIG_PAGE_POISONING_ZERO

Quoted 9 lines · Vlastimil Babka

CONFIG_PAGE_POISONING_ZERO uses the zero pattern instead of 0xAA. It was introduced by commit 1414c7f4f7d7 ("mm/page_poisoning.c: allow for zero poisoning"), noting that using zeroes retains the benefit of sanitizing content of freed pages, with the benefit of not having to zero them again on alloc, and the downside of making some forms of corruption (stray writes of NULLs) harder to detect than with the 0xAA pattern. Together with CONFIG_PAGE_POISONING_NO_SANITY it made possible to sanitize the contents on free without checking it back on alloc.

Quoted 6 lines

These days we have the init_on_free() option to achieve sanitization with zeroes and to save clearing on alloc (and without checking on alloc). Arguably if someone does choose to check the poison for corruption on alloc, the savings of not clearing the page are secondary, and it makes sense to always use the 0xAA poison pattern. Thus, remove the CONFIG_PAGE_POISONING_ZERO option for being redundant.

I agree, this simplifies things ... and I don't see a need to complicate things to speed up corner-case debug mechanisms. Thanks!

Signature · 3 lines
Thanks,

David / dhildenb