Skip to contentSkip to conversation

sched/isolation: Don't free memblock allocated cpumasks

PATCH In review Waiman Long 5 participants 10 messages May 5 – May 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/isolation: Don't free memblock allocated cpumasks

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
  :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
  :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
 kernel/sched/isolation.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +7 -1
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
 		WARN_ON_ONCE(cpumask_empty(omask));
 		cpumask_copy(nmask, omask);
 		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());
 	}
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 24 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
  :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

Quoted 17 lines
[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
  :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

Darn, I just saw the previous version doing this.

Quoted 5 lines

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

Pardon my ignorance, but how come this isn't the case for the other memblock users? It sounds like there is no right place for freeing this mask.

#2 lore raw
Quoted 44 lines · Valentin Schneider
Quoted 24 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
   :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

Quoted 17 lines
[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
   :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

Darn, I just saw the previous version doing this.

Quoted 8 lines
Quoted 5 lines

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

Pardon my ignorance, but how come this isn't the case for the other memblock users? It sounds like there is no right place for freeing this mask.

My current thought is to chain all the memblock memory blocks to be freed in a singly linked list first and then freed them at the right moment by the memblock code. That will require some more investigation into the memblock code. This patch is just a temporary workaround which I hope will be reverted in the future.

Cheers,
Longman

#3 lore raw
In reply to Waiman Long
Quoted 4 lines · Waiman Long

One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

We just hit the same KASAN UAF from a different caller on a v7.1-rc3 boot, which I think reinforces that the fix really needs to be in memblock rather than in each subsystem.

In our case the offender is the IMA kexec buffer release path:

    [  113.498542] BUG: KASAN: use-after-free in memblock_isolate_range+0x208/0x8f0
    [  113.514206] Read of size 8 at addr ff11001824ba4000 by task swapper/0/1
    ...
    [  113.532258]  memblock_isolate_range+0x208/0x8f0
    [  113.532267]  memblock_phys_free+0x5f/0x300
    [  113.532274]  ima_free_kexec_buffer+0x1d/0x40
    [  113.532280]  ima_load_kexec_buffer+0xbf/0xf0
    [  113.532285]  ima_init+0x42/0xa0
    [  113.532287]  init_ima+0x5e/0x190
    [  113.532290]  security_initcall_late+0xad/0x210
    [  113.532301]  do_one_initcall+0x138/0x540

Same shape as your second trace: memblock_phys_free() reads memblock.reserved.regions, which memblock_discard() has already returned to the buddy allocator (the KASAN shadow shows the page as fully poisoned, and pfn 0x1824ba4 has been reallocated). It then page-faults a moment later on the same address.

ima_init runs as a security_initcall_late, so by the time ima_free_kexec_buffer() calls memblock_phys_free() on the previous kernel's measurement buffer, memblock has long been torn down on configurations without CONFIG_ARCH_KEEP_MEMBLOCK

This regression seems to come from commit 87ce9e83ab8b ("memblock, treewide: make memblock_free() handle late freeing"), which dropped memblock_free_late() and made memblock_phys_free() unconditionally call memblock_remove_range(&memblock.reserved, ...) followed by an optional __free_reserved_area().

maintainer
#4 lore raw

Hi Breno,

Quoted 35 lines · Breno Leitao
Quoted 4 lines · Waiman Long

One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

We just hit the same KASAN UAF from a different caller on a v7.1-rc3 boot, which I think reinforces that the fix really needs to be in memblock rather than in each subsystem.

In our case the offender is the IMA kexec buffer release path:

    [  113.498542] BUG: KASAN: use-after-free in memblock_isolate_range+0x208/0x8f0
    [  113.514206] Read of size 8 at addr ff11001824ba4000 by task swapper/0/1
    ...
    [  113.532258]  memblock_isolate_range+0x208/0x8f0
    [  113.532267]  memblock_phys_free+0x5f/0x300
    [  113.532274]  ima_free_kexec_buffer+0x1d/0x40
    [  113.532280]  ima_load_kexec_buffer+0xbf/0xf0
    [  113.532285]  ima_init+0x42/0xa0
    [  113.532287]  init_ima+0x5e/0x190
    [  113.532290]  security_initcall_late+0xad/0x210
    [  113.532301]  do_one_initcall+0x138/0x540

Same shape as your second trace: memblock_phys_free() reads memblock.reserved.regions, which memblock_discard() has already returned to the buddy allocator (the KASAN shadow shows the page as fully poisoned, and pfn 0x1824ba4 has been reallocated). It then page-faults a moment later on the same address.

ima_init runs as a security_initcall_late, so by the time ima_free_kexec_buffer() calls memblock_phys_free() on the previous kernel's measurement buffer, memblock has long been torn down on configurations without CONFIG_ARCH_KEEP_MEMBLOCK

Quoted 5 lines

This regression seems to come from commit 87ce9e83ab8b ("memblock, treewide: make memblock_free() handle late freeing"), which dropped memblock_free_late() and made memblock_phys_free() unconditionally call memblock_remove_range(&memblock.reserved, ...) followed by an optional __free_reserved_area().

Oops, somehow I overlooked that late freeing can't access memblock arrays :(

Can you please test this fix:

mm/memblock.c +4 -2
index a6a1c91e276d..ccd43f3abb82 100644
@@ -989,13 +989,15 @@ void __init_memblock memblock_free(void *ptr, size_t size)
 int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
 {
 	phys_addr_t end = base + size - 1;
-	int ret;
+	int ret = 0;
 
 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
 		     &base, &end, (void *)_RET_IP_);
 
 	kmemleak_free_part_phys(base, size);
-	ret = memblock_remove_range(&memblock.reserved, base, size);
+
+	if (!slab_is_available() || IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
+		ret = memblock_remove_range(&memblock.reserved, base, size);
 
 	if (slab_is_available())
 		__free_reserved_area(base, base + size, -1);
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#5 lore raw
In reply to Waiman Long

Hi Waiman,

Quoted 61 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
  :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
  :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
 kernel/sched/isolation.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +0 -0
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
 		WARN_ON_ONCE(cpumask_empty(omask));

kernel/sched/isolation.c void __init housekeeping_init(void) · line 198

Quoted 10 lines
kernel/sched/isolation.c +7 -1
 		cpumask_copy(nmask, omask);
 		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());

Before 59bd1d914bb5 it was a silent leak. housekeeping_init() is called after memblock moves all the memory to buddy, so this would only update memblock.reserved.

The comment a few lines above says that we reallocate to be able to kfree() later. Is it possible to delay reallocation until an initcall?

Quoted 6 lines
kernel/sched/isolation.c +0 -0
 	}
 }
 
Signature · 1 line
2.53.0
Signature · 2 lines
Sincerely yours,
Mike.
#6 lore raw
Quoted 78 lines · Mike Rapoport

Hi Waiman,

Quoted 61 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
   :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
   :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
  kernel/sched/isolation.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +0 -0
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
  		WARN_ON_ONCE(cpumask_empty(omask));

kernel/sched/isolation.c void __init housekeeping_init(void) · line 198

Quoted 10 lines
kernel/sched/isolation.c +7 -1
  		cpumask_copy(nmask, omask);
  		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());

Before 59bd1d914bb5 it was a silent leak. housekeeping_init() is called after memblock moves all the memory to buddy, so this would only update memblock.reserved.

Quoted 2 lines

The comment a few lines above says that we reallocate to be able to kfree() later. Is it possible to delay reallocation until an initcall?

My original thought was to defer the freeing to init call. That changes led to the KASAN bug splat listed in the commit log, I think the right window to free memblock memory is currently just too narrow. Do you mean that with the fix patch you sent to Breno, memblock freeing in initcall will work without bug report? If so, I can send another patch to defer memblock freeing after the fix patch is merged as the KASAN bug is more serious than the memblock warning. I will do some testing tomorrow with your fix patch.

Cheers,
Longman

maintainer
#7 lore raw
Quoted 79 lines · Waiman Long
Quoted 78 lines · Mike Rapoport

Hi Waiman,

Quoted 61 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
   :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
   :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
  kernel/sched/isolation.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +0 -0
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
  		WARN_ON_ONCE(cpumask_empty(omask));

kernel/sched/isolation.c void __init housekeeping_init(void) · line 198

Quoted 10 lines
kernel/sched/isolation.c +7 -1
  		cpumask_copy(nmask, omask);
  		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());

Before 59bd1d914bb5 it was a silent leak. housekeeping_init() is called after memblock moves all the memory to buddy, so this would only update memblock.reserved.

Quoted 8 lines
Quoted 2 lines

The comment a few lines above says that we reallocate to be able to kfree() later. Is it possible to delay reallocation until an initcall?

My original thought was to defer the freeing to init call. That changes led to the KASAN bug splat listed in the commit log, I think the right window to free memblock memory is currently just too narrow. Do you mean that with the fix patch you sent to Breno, memblock freeing in initcall will work without bug report?

Yes, with the fix I sent to Breno memblock_free() should work in an initcall and "do the right thing".

Quoted 7 lines

If so, I can send another patch to defer memblock freeing after the fix patch is merged as the KASAN bug is more serious than the memblock warning. I will do some testing tomorrow with your fix patch.

Cheers,
Longman

Signature · 2 lines
Sincerely yours,
Mike.
author Tested-by
#8 lore raw
Quoted 82 lines · Mike Rapoport
Quoted 81 lines · Waiman Long
Quoted 80 lines · Mike Rapoport

Hi Waiman,

Quoted 61 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
    :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
    :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
   kernel/sched/isolation.c | 8 +++++++-
   1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +0 -0
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
   		WARN_ON_ONCE(cpumask_empty(omask));

kernel/sched/isolation.c void __init housekeeping_init(void) · line 198

Quoted 10 lines
kernel/sched/isolation.c +7 -1
   		cpumask_copy(nmask, omask);
   		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());

Before 59bd1d914bb5 it was a silent leak. housekeeping_init() is called after memblock moves all the memory to buddy, so this would only update memblock.reserved.

The comment a few lines above says that we reallocate to be able to kfree() later. Is it possible to delay reallocation until an initcall?

Quoted 7 lines
Quoted 5 lines

My original thought was to defer the freeing to init call. That changes led to the KASAN bug splat listed in the commit log, I think the right window to free memblock memory is currently just too narrow. Do you mean that with the fix patch you sent to Breno, memblock freeing in initcall will work without bug report?

Yes, with the fix I sent to Breno memblock_free() should work in an initcall and "do the right thing".

Thanks for the confirmation. I have tested your patch with my patch to defer the memblock_free() to initcall. There is no longer any KASAN splat when booting up a debug test kernel. You can add the following tag when you send out your patch.

#9 lore raw
Quoted 91 lines · Waiman Long
Quoted 82 lines · Mike Rapoport
Quoted 81 lines · Waiman Long
Quoted 80 lines · Mike Rapoport

Hi Waiman,

Quoted 61 lines · Waiman Long

When testing a v7.1 kernel with commit 59bd1d914bb5 ("memblock: warn when freeing reserved memory before memory map is initialized"), the following warning was hit when there was a "nohz_full" kernel boot parameter.

[    0.080911] Cannot free reserved memory because of deferred initialization of the memory map
[    0.080911] WARNING: mm/memblock.c:904 at __free_reserved_area+0xde/0xf0, CPU#0: swapper/0/0
    :
[    0.080945] Call Trace:
[    0.080947]  <TASK>
[    0.080949]  memblock_phys_free+0xcb/0x100
[    0.080953]  housekeeping_init+0x14c/0x170
[    0.080957]  start_kernel+0x207/0x450
[    0.080961]  x86_64_start_reservations+0x24/0x30
[    0.080965]  x86_64_start_kernel+0xda/0xe0
[    0.080967]  common_startup_64+0x13e/0x141
[    0.080972]  </TASK>

The commit states that freeing of reserved memory before the memory map is fully initialized in deferred_init_memmap() would cause access to uninitialized struct pages and may crash when accessing spurious list pointers. However, if the memblock_free() call is deferred to the start of initcall processing in the bootup process, for instance, the following KASAN warning can appear.

[    8.514775] BUG: KASAN: use-after-free in memblock_isolate_range+0x4ac/0x650
[    8.514775] Read of size 8 at addr ffff88a07fe6a000 by task swapper/0/1
    :
[    8.514775] Call Trace:
[    8.514775]  <TASK>
[    8.514775]  kasan_report+0xb2/0x1b0
[    8.514775]  memblock_isolate_range+0x4ac/0x650
[    8.514775]  memblock_phys_free+0xc4/0x190
[    8.514775]  housekeeping_late_init+0x257/0x280
[    8.514775]  do_one_initcall+0xaa/0x470
[    8.514775]  do_initcalls+0x1b4/0x1f0
[    8.514775]  kernel_init_freeable+0x4b5/0x550
[    8.514775]  kernel_init+0x1c/0x150
[    8.514775]  ret_from_fork+0x5dc/0x8e0
[    8.514775]  ret_from_fork_asm+0x1a/0x30
[    8.514775]  </TASK>

It is likely that memblock_discard() may discard memblock data needed for memblock_free(). One workaround for now to avoid these warning/bug messages is to keep the memblock allocated cpumasks even if they are no longer needed until the memblock subsystem is properly updated to handle memblock_free().

On most systems, memory occuipied by a cpumask is pretty small. So not much memory will be wasted if the memblock cpumasks are not freed.

---
   kernel/sched/isolation.c | 8 +++++++-
   1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/isolation.c +0 -0
index ef152d401fe2..ad9b1a1104e3 100644
@@ -189,7 +189,13 @@ void __init housekeeping_init(void)
   		WARN_ON_ONCE(cpumask_empty(omask));

kernel/sched/isolation.c void __init housekeeping_init(void) · line 198

Quoted 10 lines
kernel/sched/isolation.c +7 -1
   		cpumask_copy(nmask, omask);
   		RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
-		memblock_free(omask, cpumask_size());
+
+		/*
+		 * TODO: Don't free memblock allocated cpumasks until the
+		 * memblock subystem is able to handle the memblock_free()
+		 * properly.
+		 */
+		// memblock_free(omask, cpumask_size());

Before 59bd1d914bb5 it was a silent leak. housekeeping_init() is called after memblock moves all the memory to buddy, so this would only update memblock.reserved.

The comment a few lines above says that we reallocate to be able to kfree() later. Is it possible to delay reallocation until an initcall?

Quoted 7 lines
Quoted 5 lines

My original thought was to defer the freeing to init call. That changes led to the KASAN bug splat listed in the commit log, I think the right window to free memblock memory is currently just too narrow. Do you mean that with the fix patch you sent to Breno, memblock freeing in initcall will work without bug report?

Yes, with the fix I sent to Breno memblock_free() should work in an initcall and "do the right thing".

Quoted 6 lines

Thanks for the confirmation. I have tested your patch with my patch to defer the memblock_free() to initcall. There is no longer any KASAN splat when booting up a debug test kernel. You can add the following tag when you send out your patch.

Thanks a lot guys!

Signature · 2 lines
Frederic Weisbecker
SUSE Labs