linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH mm] mm, kasan: fix for "integrate page_alloc init with HW_TAGS"
@ 2021-03-30 15:31 Andrey Konovalov
  2021-03-30 15:54 ` Vlastimil Babka
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Konovalov @ 2021-03-30 15:31 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Vlastimil Babka, Sergei Trofimovich, Alexander Potapenko,
	Marco Elver, Dmitry Vyukov, Andrey Ryabinin, Andrey Konovalov,
	kasan-dev, linux-mm, linux-kernel, Andrey Konovalov

My commit "integrate page_alloc init with HW_TAGS" changed the order of
kernel_unpoison_pages() and kernel_init_free_pages() calls. This leads
to __GFP_ZERO allocations being incorrectly poisoned when page poisoning
is enabled.

Fix by restoring the initial order. Also add a warning comment.

Reported-by: Vlastimil Babka <vbabka@suse.cz>
Reported-by: Sergei Trofimovich <slyfox@gentoo.org>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 mm/page_alloc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 033bd92e8398..1fc5061f8ca1 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2328,6 +2328,12 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	arch_alloc_page(page, order);
 	debug_pagealloc_map_pages(page, 1 << order);
 
+	/*
+	 * Page unpoisoning must happen before memory initialization.
+	 * Otherwise, a __GFP_ZERO allocation will not be initialized.
+	 */
+	kernel_unpoison_pages(page, 1 << order);
+
 	/*
 	 * As memory initialization might be integrated into KASAN,
 	 * kasan_alloc_pages and kernel_init_free_pages must be
@@ -2338,7 +2344,6 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 	if (init && !kasan_has_integrated_init())
 		kernel_init_free_pages(page, 1 << order);
 
-	kernel_unpoison_pages(page, 1 << order);
 	set_page_owner(page, order, gfp_flags);
 }
 
-- 
2.31.0.291.g576ba9dcdaf-goog



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

* Re: [PATCH mm] mm, kasan: fix for "integrate page_alloc init with HW_TAGS"
  2021-03-30 15:31 [PATCH mm] mm, kasan: fix for "integrate page_alloc init with HW_TAGS" Andrey Konovalov
@ 2021-03-30 15:54 ` Vlastimil Babka
  2021-03-30 16:37   ` Andrey Konovalov
  0 siblings, 1 reply; 3+ messages in thread
From: Vlastimil Babka @ 2021-03-30 15:54 UTC (permalink / raw)
  To: Andrey Konovalov, Andrew Morton
  Cc: Sergei Trofimovich, Alexander Potapenko, Marco Elver,
	Dmitry Vyukov, Andrey Ryabinin, Andrey Konovalov, kasan-dev,
	linux-mm, linux-kernel

On 3/30/21 5:31 PM, Andrey Konovalov wrote:
> My commit "integrate page_alloc init with HW_TAGS" changed the order of
> kernel_unpoison_pages() and kernel_init_free_pages() calls. This leads
> to __GFP_ZERO allocations being incorrectly poisoned when page poisoning
> is enabled.

Correction: This leads to check_poison_mem() complain about memory corruption
because the poison pattern has already been overwritten by zeroes.

> Fix by restoring the initial order. Also add a warning comment.
> 
> Reported-by: Vlastimil Babka <vbabka@suse.cz>
> Reported-by: Sergei Trofimovich <slyfox@gentoo.org>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>

I expect this will be folded to your patch in mmotm anyway, so the changelog is
not as important...

> ---
>  mm/page_alloc.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 033bd92e8398..1fc5061f8ca1 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -2328,6 +2328,12 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
>  	arch_alloc_page(page, order);
>  	debug_pagealloc_map_pages(page, 1 << order);
>  
> +	/*
> +	 * Page unpoisoning must happen before memory initialization.
> +	 * Otherwise, a __GFP_ZERO allocation will not be initialized.

... but the comment should be corrected too:
"Otherwise, a __GFP_ZERO allocation will trigger a memory corruption report
during unpoisoning."

Thanks.

> +	 */
> +	kernel_unpoison_pages(page, 1 << order);
> +
>  	/*
>  	 * As memory initialization might be integrated into KASAN,
>  	 * kasan_alloc_pages and kernel_init_free_pages must be
> @@ -2338,7 +2344,6 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
>  	if (init && !kasan_has_integrated_init())
>  		kernel_init_free_pages(page, 1 << order);
>  
> -	kernel_unpoison_pages(page, 1 << order);
>  	set_page_owner(page, order, gfp_flags);
>  }
>  
> 



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

* Re: [PATCH mm] mm, kasan: fix for "integrate page_alloc init with HW_TAGS"
  2021-03-30 15:54 ` Vlastimil Babka
@ 2021-03-30 16:37   ` Andrey Konovalov
  0 siblings, 0 replies; 3+ messages in thread
From: Andrey Konovalov @ 2021-03-30 16:37 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Sergei Trofimovich, Alexander Potapenko,
	Marco Elver, Dmitry Vyukov, Andrey Ryabinin, Andrey Konovalov,
	kasan-dev, Linux Memory Management List, LKML

On Tue, Mar 30, 2021 at 5:54 PM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 3/30/21 5:31 PM, Andrey Konovalov wrote:
> > My commit "integrate page_alloc init with HW_TAGS" changed the order of
> > kernel_unpoison_pages() and kernel_init_free_pages() calls. This leads
> > to __GFP_ZERO allocations being incorrectly poisoned when page poisoning
> > is enabled.
>
> Correction: This leads to check_poison_mem() complain about memory corruption
> because the poison pattern has already been overwritten by zeroes.

Ah, indeed. Will send v2. Thanks!


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

end of thread, other threads:[~2021-03-30 16:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-30 15:31 [PATCH mm] mm, kasan: fix for "integrate page_alloc init with HW_TAGS" Andrey Konovalov
2021-03-30 15:54 ` Vlastimil Babka
2021-03-30 16:37   ` Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).