Skip to contentSkip to conversation

mm: Always release pages to the buddy allocator in memblock_free_late().

PATCH Superseded Aaron Thompson 2 participants 3 messages Jan 4, 2023 – Jan 5, 2023 1 message not in the archive

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 mm: Always release pages to the buddy allocator in memblock_free_late().

If CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, memblock_free_pages() only releases pages to the buddy allocator if they are not in the deferred range. This is correct for free pages (as defined by for_each_free_mem_pfn_range_in_zone()) because free pages in the deferred range will be initialized and released as part of the deferred init process. memblock_free_pages() is called by memblock_free_late(), which is used to free reserved ranges after memblock_free_all() has run. memblock_free_all() initializes all pages in reserved ranges, and accordingly, those pages are not touched by the deferred init process. This means that currently, if the pages that memblock_free_late() intends to release are in the deferred range, they will never be released to the buddy allocator. They will forever be reserved.

In addition, memblock_free_pages() calls kmsan_memblock_free_pages(), which is also correct for free pages but is not correct for reserved pages. KMSAN metadata for reserved pages is initialized by kmsan_init_shadow(), which runs shortly before memblock_free_all().

For both of these reasons, memblock_free_pages() should only be called for free pages, and memblock_free_late() should call __free_pages_core() directly instead.

Fixes 3a80a7fa7989 ("mm: meminit: initialise a subset of struct pages if CONFIG_DEFERRED_STRUCT_PAGE_INIT is set")
---
 mm/memblock.c                     | 2 +-
 tools/testing/memblock/internal.h | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
mm/memblock.c +1 -1
index 511d4783dcf1..56a5b6086c50 100644
@@ -1640,7 +1640,7 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
 	end = PFN_DOWN(base + size);
 
 	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
+		__free_pages_core(pfn_to_page(cursor), 0);
 		totalram_pages_inc();
 	}
 }
tools/testing/memblock/internal.h +4 -0
index fdb7f5db7308..85973e55489e 100644
@@ -15,6 +15,10 @@ bool mirrored_kernelcore = false;
 
 struct page {};
 
+void __free_pages_core(struct page *page, unsigned int order)
+{
+}
+
 void memblock_free_pages(struct page *page, unsigned long pfn,
 			 unsigned int order)
 {
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Hi,

Quoted 8 lines · Aaron Thompson

If CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, memblock_free_pages() only releases pages to the buddy allocator if they are not in the deferred range. This is correct for free pages (as defined by for_each_free_mem_pfn_range_in_zone()) because free pages in the deferred range will be initialized and released as part of the deferred init process. memblock_free_pages() is called by memblock_free_late(), which is used to free reserved ranges after memblock_free_all() has run. memblock_free_all() initializes all pages in reserved ranges, and

To be precise, memblock_free_all() frees pages, or releases them to the pages allocator, rather than initializes.

Quoted 6 lines

accordingly, those pages are not touched by the deferred init process. This means that currently, if the pages that memblock_free_late() intends to release are in the deferred range, they will never be released to the buddy allocator. They will forever be reserved.

Quoted 8 lines

In addition, memblock_free_pages() calls kmsan_memblock_free_pages(), which is also correct for free pages but is not correct for reserved pages. KMSAN metadata for reserved pages is initialized by kmsan_init_shadow(), which runs shortly before memblock_free_all().

For both of these reasons, memblock_free_pages() should only be called for free pages, and memblock_free_late() should call __free_pages_core() directly instead.

Overall looks fine to me and I couldn't spot potential issues.

I'd appreciate if you add a paragraph about the actual issue with EFI boot you described in the cover letter to the commit message.

Quoted 12 lines
Fixes 3a80a7fa7989 ("mm: meminit: initialise a subset of struct pages if CONFIG_DEFERRED_STRUCT_PAGE_INIT is set")
---
 mm/memblock.c                     | 2 +-
 tools/testing/memblock/internal.h | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
mm/memblock.c +0 -0
index 511d4783dcf1..56a5b6086c50 100644

mm/memblock.c void __init memblock_free_late(phys_addr_t base, phys_addr_t size) · line 1643

Quoted 5 lines
mm/memblock.c +1 -1
@@ -1640,7 +1640,7 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
 	end = PFN_DOWN(base + size);
 
 	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
+		__free_pages_core(pfn_to_page(cursor), 0);

Please add a comment that explains why it is safe to call __free_pages_core() here. Something like

/*
* Reserved pages are always initialized by the end of
* memblock_free_all() either during memmap_init() or, with deferred
* initialization if struct page in reserve_bootmem_region() */

Quoted 21 lines
mm/memblock.c +0 -0
 		totalram_pages_inc();
 	}
 }
tools/testing/memblock/internal.h +4 -0
index fdb7f5db7308..85973e55489e 100644
@@ -15,6 +15,10 @@ bool mirrored_kernelcore = false;
 
 struct page {};
 
+void __free_pages_core(struct page *page, unsigned int order)
+{
+}
+
 void memblock_free_pages(struct page *page, unsigned long pfn,
 			 unsigned int order)
 {
Signature · 1 line
2.30.2
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw

Hi Mike,

Quoted 12 lines · Mike Rapoport

Hi,

Quoted 8 lines · Aaron Thompson

If CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, memblock_free_pages() only releases pages to the buddy allocator if they are not in the deferred range. This is correct for free pages (as defined by for_each_free_mem_pfn_range_in_zone()) because free pages in the deferred range will be initialized and released as part of the deferred init process. memblock_free_pages() is called by memblock_free_late(), which is used to free reserved ranges after memblock_free_all() has run. memblock_free_all() initializes all pages in reserved ranges, and

Quoted 2 lines

To be precise, memblock_free_all() frees pages, or releases them to the pages allocator, rather than initializes.

As you mentioned in the comment below, whether memblock_free_all() does any initializing depends on the particular deferred init situation. memblock_free_all() does ultimately call init_reserved_page() for every reserved page (via reserve_bootmem_region()), but that only actually initializes the page if it's in the deferred range. In either case, all I was trying to say here is that we can be certain that all reserved pages have been initialized after memblock_free_all() has run, so I'll rephrase that.

Quoted 11 lines
Quoted 11 lines

accordingly, those pages are not touched by the deferred init process. This means that currently, if the pages that memblock_free_late() intends to release are in the deferred range, they will never be released to the buddy allocator. They will forever be reserved.

In addition, memblock_free_pages() calls kmsan_memblock_free_pages(), which is also correct for free pages but is not correct for reserved pages. KMSAN metadata for reserved pages is initialized by kmsan_init_shadow(), which runs shortly before memblock_free_all().

Quoted 8 lines
Quoted 3 lines

For both of these reasons, memblock_free_pages() should only be called for free pages, and memblock_free_late() should call __free_pages_core() directly instead.

Overall looks fine to me and I couldn't spot potential issues.

I'd appreciate if you add a paragraph about the actual issue with EFI boot you described in the cover letter to the commit message.

Sure, will do.

Quoted 23 lines
Quoted 12 lines
Fixes 3a80a7fa7989 ("mm: meminit: initialise a subset of struct pages if CONFIG_DEFERRED_STRUCT_PAGE_INIT is set")
---
 mm/memblock.c                     | 2 +-
 tools/testing/memblock/internal.h | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
mm/memblock.c +0 -0
index 511d4783dcf1..56a5b6086c50 100644

mm/memblock.c void __init memblock_free_late(phys_addr_t base, · line 1644

Quoted 6 lines
mm/memblock.c +1 -1
@@ -1640,7 +1640,7 @@ void __init memblock_free_late(phys_addr_t base, 
 phys_addr_t size)
 	end = PFN_DOWN(base + size);
 
 	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
+		__free_pages_core(pfn_to_page(cursor), 0);

Please add a comment that explains why it is safe to call __free_pages_core() here.
Something like

Quoted 5 lines

/*
* Reserved pages are always initialized by the end of
* memblock_free_all() either during memmap_init() or, with deferred
* initialization if struct page in reserve_bootmem_region() */

Will do. Thanks for the review.

Quoted 11 lines
Quoted 11 lines
mm/memblock.c +0 -0
 		totalram_pages_inc();
 	}

}

tools/testing/memblock/internal.h +0 -0
index fdb7f5db7308..85973e55489e 100644
@@ -15,6 +15,10 @@ bool mirrored_kernelcore = false;
 
 struct page {};
 
Quoted 10 lines
Quoted 10 lines
tools/testing/memblock/internal.h +4 -1
+void __free_pages_core(struct page *page, unsigned int order)
+{
+}
+
 void memblock_free_pages(struct page *page, unsigned long pfn,
 			 unsigned int order)
 {
--

2.30.2

Thanks,
-- Aaron