Skip to contentSkip to conversation

ACPI: fix acpi table use after free

PATCH In review George Kennedy 5 participants 33 messages Mar 4, 2021 – Mar 24, 2021

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

PATCH 1/1 ACPI: fix acpi table use after free

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when acpi tables are accessed.

BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
Read of size 4 at addr ffff8880be453004 by task swapper/0/1
CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
Call Trace:
 dump_stack+0xf6/0x158
 print_address_description.constprop.9+0x41/0x60
 kasan_report.cold.14+0x7b/0xd4
 __asan_report_load_n_noabort+0xf/0x20
 ibft_init+0x134/0xc49
 do_one_initcall+0xc4/0x3e0
 kernel_init_freeable+0x5af/0x66b
 kernel_init+0x16/0x1d0
 ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator. Use memblock_reserve() to reserve all the ACPI table pages.

---
 arch/x86/kernel/setup.c        | 3 +--
 drivers/acpi/acpica/tbinstal.c | 4 ++++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +1 -2
index d883176..97deea3 100644
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
 	cleanup_highmap();
 
 	memblock_set_current_limit(ISA_END_ADDRESS);
+	acpi_boot_table_init();
 	e820__memblock_setup();
 
 	/*
@@ -1139,8 +1140,6 @@ void __init setup_arch(char **cmdline_p)
 	/*
 	 * Parse the ACPI tables for possible boot-time SMP configuration.
 	 */
-	acpi_boot_table_init();
-
 	early_acpi_boot_init();
 
 	initmem_init();
drivers/acpi/acpica/tbinstal.c +4 -0
index 8d1e5b5..4e32b22 100644
@@ -8,6 +8,7 @@
  *****************************************************************************/
 
 #include <acpi/acpi.h>
+#include <linux/memblock.h>
 #include "accommon.h"
 #include "actables.h"
 
@@ -58,6 +59,9 @@
 				      new_table_desc->flags,
 				      new_table_desc->pointer);
 
+	memblock_reserve(new_table_desc->address,
+			 PAGE_ALIGN(new_table_desc->pointer->length));
+
 	acpi_tb_print_table_header(new_table_desc->address,
 				   new_table_desc->pointer);
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 19 lines · George Kennedy

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when acpi tables are accessed.

BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
Read of size 4 at addr ffff8880be453004 by task swapper/0/1
CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
Call Trace:
 dump_stack+0xf6/0x158
 print_address_description.constprop.9+0x41/0x60
 kasan_report.cold.14+0x7b/0xd4
 __asan_report_load_n_noabort+0xf/0x20
 ibft_init+0x134/0xc49
 do_one_initcall+0xc4/0x3e0
 kernel_init_freeable+0x5af/0x66b
 kernel_init+0x16/0x1d0
 ret_from_fork+0x22/0x30
Quoted 2 lines

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

What do you mean by this?

Quoted 1 line

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

Quoted 11 lines
Signed-off-by George Kennedy <george.kennedy(a)oracle.com>
---
 arch/x86/kernel/setup.c        | 3 +--
 drivers/acpi/acpica/tbinstal.c | 4 ++++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +0 -0
index d883176..97deea3 100644

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 1049

Quoted 4 lines
arch/x86/kernel/setup.c +1 -0
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
        cleanup_highmap();
 
        memblock_set_current_limit(ISA_END_ADDRESS);
+       acpi_boot_table_init();

This cannot be moved before the acpi_table_upgrade() invocation AFAICS.

Why exactly do you want to move it?

Quoted 25 lines
arch/x86/kernel/setup.c +0 -2
        e820__memblock_setup();
 
        /*
@@ -1139,8 +1140,6 @@ void __init setup_arch(char **cmdline_p)
        /*
         * Parse the ACPI tables for possible boot-time SMP configuration.
         */
-       acpi_boot_table_init();
-
        early_acpi_boot_init();
 
        initmem_init();
drivers/acpi/acpica/tbinstal.c +1 -0
index 8d1e5b5..4e32b22 100644
@@ -8,6 +8,7 @@
  *****************************************************************************/
 
 #include <acpi/acpi.h>
+#include <linux/memblock.h>
 #include "accommon.h"
 #include "actables.h"
 

drivers/acpi/acpica/tbinstal.c line 64

Quoted 6 lines
drivers/acpi/acpica/tbinstal.c +3 -0
@@ -58,6 +59,9 @@
                                      new_table_desc->flags,
                                      new_table_desc->pointer);
 
+       memblock_reserve(new_table_desc->address,
+                        PAGE_ALIGN(new_table_desc->pointer->length));
+

Why do you want to do this here in the first place?

Things like that cannot be done in the ACPICA code in general.

Quoted 4 lines
drivers/acpi/acpica/tbinstal.c +0 -1
        acpi_tb_print_table_header(new_table_desc->address,
                                   new_table_desc->pointer);
 
--
#2 lore raw

Hello Rafael,

Quoted 19 lines · Rafael J. Wysocki
Quoted 18 lines · George Kennedy

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when acpi tables are accessed.

BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
Read of size 4 at addr ffff8880be453004 by task swapper/0/1
CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
Call Trace:
  dump_stack+0xf6/0x158
  print_address_description.constprop.9+0x41/0x60
  kasan_report.cold.14+0x7b/0xd4
  __asan_report_load_n_noabort+0xf/0x20
  ibft_init+0x134/0xc49
  do_one_initcall+0xc4/0x3e0
  kernel_init_freeable+0x5af/0x66b
  kernel_init+0x16/0x1d0
  ret_from_fork+0x22/0x30
Quoted 3 lines
Quoted 2 lines

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

What do you mean by this?

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

Quoted 3 lines
Quoted 2 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

[    0.477319]  memmap_init_range+0x33b/0x4e2
[    0.479053]  memmap_init_zone+0x1e0/0x243
[    0.485276]  free_area_init_node+0xa4e/0xac5 [    0.498242]  free_area_init+0xf4a/0x107a
[    0.509958]  zone_sizes_init+0xd9/0x111
[    0.511731]  paging_init+0x4a/0x4c
[    0.512417]  setup_arch+0x14f8/0x1758
[    0.519193]  start_kernel+0x6c/0x46f
[    0.519921]  x86_64_start_reservations+0x37/0x39 [    0.520847]  x86_64_start_kernel+0x7b/0x7e
[    0.521666]  secondary_startup_64_no_verify+0xb0/0xbb

Quoted 14 lines
Quoted 14 lines
---
  arch/x86/kernel/setup.c        | 3 +--
  drivers/acpi/acpica/tbinstal.c | 4 ++++
  2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +0 -0
index d883176..97deea3 100644
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
         cleanup_highmap();
 
Quoted 5 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 1049

Quoted 2 lines
arch/x86/kernel/setup.c +1 -0
         memblock_set_current_limit(ISA_END_ADDRESS);
+       acpi_boot_table_init();

This cannot be moved before the acpi_table_upgrade() invocation AFAICS.

Why exactly do you want to move it?

Want to make sure there are slots for memblock_reserve() to be able to reserve the page.

Quoted 25 lines
Quoted 25 lines
arch/x86/kernel/setup.c +0 -2
         e820__memblock_setup();
 
         /*
@@ -1139,8 +1140,6 @@ void __init setup_arch(char **cmdline_p)
         /*
          * Parse the ACPI tables for possible boot-time SMP configuration.
          */
-       acpi_boot_table_init();
-
         early_acpi_boot_init();
 
         initmem_init();
drivers/acpi/acpica/tbinstal.c +1 -0
index 8d1e5b5..4e32b22 100644
@@ -8,6 +8,7 @@
   *****************************************************************************/
 
  #include <acpi/acpi.h>
+#include <linux/memblock.h>
  #include "accommon.h"
  #include "actables.h"
 
Quoted 8 lines

drivers/acpi/acpica/tbinstal.c line 64

Quoted 7 lines
drivers/acpi/acpica/tbinstal.c +3 -0
@@ -58,6 +59,9 @@
                                       new_table_desc->flags,
                                       new_table_desc->pointer);
 
+       memblock_reserve(new_table_desc->address,
+                        PAGE_ALIGN(new_table_desc->pointer->length));
+

Why do you want to do this here in the first place?

If there is a better place to do it, I can move the memblock_reserve() there. The memblock_reserve() cannot be done from the ibft code - it's too late - the ibft table page has already ended up on the freelist by the time ibft_init() is called.

Quoted 2 lines

Things like that cannot be done in the ACPICA code in general.

Can you recommend a better place to do the memblock_reserve() from?

Thank you,
George

Quoted 5 lines
Quoted 5 lines
drivers/acpi/acpica/tbinstal.c +0 -1
         acpi_tb_print_table_header(new_table_desc->address,
                                    new_table_desc->pointer);
 
--
#3 lore raw
Quoted 23 lines · George Kennedy

Hello Rafael,

Quoted 19 lines · Rafael J. Wysocki
Quoted 18 lines · George Kennedy

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when acpi tables are accessed.

BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
Read of size 4 at addr ffff8880be453004 by task swapper/0/1
CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
Call Trace:
  dump_stack+0xf6/0x158
  print_address_description.constprop.9+0x41/0x60
  kasan_report.cold.14+0x7b/0xd4
  __asan_report_load_n_noabort+0xf/0x20
  ibft_init+0x134/0xc49
  do_one_initcall+0xc4/0x3e0
  kernel_init_freeable+0x5af/0x66b
  kernel_init+0x16/0x1d0
  ret_from_fork+0x22/0x30
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

What do you mean by this?

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

Quoted 11 lines
Quoted 3 lines
Quoted 2 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

David, is commit 7fef431be9c9 related to this and if so, then how?

Quoted 26 lines
[    0.477319]  memmap_init_range+0x33b/0x4e2
[    0.479053]  memmap_init_zone+0x1e0/0x243
[    0.485276]  free_area_init_node+0xa4e/0xac5
[    0.498242]  free_area_init+0xf4a/0x107a
[    0.509958]  zone_sizes_init+0xd9/0x111
[    0.511731]  paging_init+0x4a/0x4c
[    0.512417]  setup_arch+0x14f8/0x1758
[    0.519193]  start_kernel+0x6c/0x46f
[    0.519921]  x86_64_start_reservations+0x37/0x39
[    0.520847]  x86_64_start_kernel+0x7b/0x7e
[    0.521666]  secondary_startup_64_no_verify+0xb0/0xbb
Quoted 14 lines
Quoted 14 lines
Signed-off-by George Kennedy <george.kennedy(a)oracle.com>
---
  arch/x86/kernel/setup.c        | 3 +--
  drivers/acpi/acpica/tbinstal.c | 4 ++++
  2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +0 -0
index d883176..97deea3 100644
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
         cleanup_highmap();
 
Quoted 8 lines
Quoted 5 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 1049

Quoted 2 lines
arch/x86/kernel/setup.c +1 -0
         memblock_set_current_limit(ISA_END_ADDRESS);
+       acpi_boot_table_init();

This cannot be moved before the acpi_table_upgrade() invocation AFAICS.

Why exactly do you want to move it?

Want to make sure there are slots for memblock_reserve() to be able to reserve the page.

Well, that may not require reordering the initialization this way.

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
arch/x86/kernel/setup.c +0 -2
         e820__memblock_setup();
 
         /*
@@ -1139,8 +1140,6 @@ void __init setup_arch(char **cmdline_p)
         /*
          * Parse the ACPI tables for possible boot-time SMP configuration.
          */
-       acpi_boot_table_init();
-
         early_acpi_boot_init();
 
         initmem_init();
drivers/acpi/acpica/tbinstal.c +1 -0
index 8d1e5b5..4e32b22 100644
@@ -8,6 +8,7 @@
   *****************************************************************************/
 
  #include <acpi/acpi.h>
+#include <linux/memblock.h>
  #include "accommon.h"
  #include "actables.h"
 
Quoted 8 lines

drivers/acpi/acpica/tbinstal.c line 64

Quoted 7 lines
drivers/acpi/acpica/tbinstal.c +3 -0
@@ -58,6 +59,9 @@
                                       new_table_desc->flags,
                                       new_table_desc->pointer);
 
+       memblock_reserve(new_table_desc->address,
+                        PAGE_ALIGN(new_table_desc->pointer->length));
+

Why do you want to do this here in the first place?

Quoted 4 lines

If there is a better place to do it, I can move the memblock_reserve() there. The memblock_reserve() cannot be done from the ibft code - it's too late - the ibft table page has already ended up on the freelist by the time ibft_init() is called.

I see.

Quoted 4 lines
Quoted 2 lines

Things like that cannot be done in the ACPICA code in general.

Can you recommend a better place to do the memblock_reserve() from?

Maybe. I need to understand the problem better, though.

Thanks!

#4 lore raw
Quoted 7 lines
Quoted 2 lines

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

If the memory is indicated by the BIOS/firmware as valid memory (!reserved) but contains actual tables that have to remain untouched what happens is:

1) Memblock thinks the memory should be given to the buddy, because it is valid memory and was not reserved by anyone (i.e., the bios, early allocations).

2) Memblock will expose the pages to the buddy, adding them to the free page list.

3) Anybody can allocate them, e.g., via alloc_pages().

The root issue is that pages that should not get exposed to the buddy as free pages get exposed to the buddy as free pages. We have to teach memblock that these pages are not actually to be used, but instead, area reserved.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

Quoted 8 lines
Quoted 5 lines

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

David, is commit 7fef431be9c9 related to this and if so, then how?

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG. The same could be reproduced via zone shuffling with a little luck.

Signature · 3 lines
Thanks,

David / dhildenb
#5 lore raw
Quoted 31 lines · David Hildenbrand
Quoted 7 lines
Quoted 2 lines

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely.  It is also necessary to reserve it upfront, for example with the help of memblock_reserve().  Is that correct?  If so, is there an alternative way to reserve a page frame?

If the memory is indicated by the BIOS/firmware as valid memory (!reserved) but contains actual tables that have to remain untouched what happens is:

1) Memblock thinks the memory should be given to the buddy, because it is valid memory and was not reserved by anyone (i.e., the bios, early allocations).

2) Memblock will expose the pages to the buddy, adding them to the free page list.

3) Anybody can allocate them, e.g., via alloc_pages().

The root issue is that pages that should not get exposed to the buddy as free pages get exposed to the buddy as free pages. We have to teach memblock that these pages are not actually to be used, but instead, area reserved.

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

David, is commit 7fef431be9c9 related to this and if so, then how?

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG. The same could be reproduced via zone shuffling with a little luck.

Thank you David for the detailed problem description, George

#6 lore raw
In reply to David Hildenbrand
Quoted 37 lines · David Hildenbrand
Quoted 8 lines
Quoted 3 lines

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

If the memory is indicated by the BIOS/firmware as valid memory (!reserved) but contains actual tables that have to remain untouched what happens is:

1) Memblock thinks the memory should be given to the buddy, because it is valid memory and was not reserved by anyone (i.e., the bios, early allocations).

2) Memblock will expose the pages to the buddy, adding them to the free page list.

3) Anybody can allocate them, e.g., via alloc_pages().

The root issue is that pages that should not get exposed to the buddy as free pages get exposed to the buddy as free pages. We have to teach memblock that these pages are not actually to be used, but instead, area reserved.

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

David, is commit 7fef431be9c9 related to this and if so, then how?

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG.

Well, you can call it that, or you can say that things worked under certain assumptions regarding the memory allocation order which are not met any more.

Quoted 1 line

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

#7 lore raw
Quoted 56 lines · Rafael J. Wysocki
Quoted 37 lines · David Hildenbrand
Quoted 8 lines
Quoted 3 lines

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

If the memory is indicated by the BIOS/firmware as valid memory (!reserved) but contains actual tables that have to remain untouched what happens is:

1) Memblock thinks the memory should be given to the buddy, because it is valid memory and was not reserved by anyone (i.e., the bios, early allocations).

2) Memblock will expose the pages to the buddy, adding them to the free page list.

3) Anybody can allocate them, e.g., via alloc_pages().

The root issue is that pages that should not get exposed to the buddy as free pages get exposed to the buddy as free pages. We have to teach memblock that these pages are not actually to be used, but instead, area reserved.

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

Use memblock_reserve() to reserve all the ACPI table pages.

How is this going to help?

If the ibft table page is not reserved, it will end up on the freelist and potentially be allocated before ibft_init() is called.

I believe this is the call that causes the ibft table page (in this case pfn=0xbe453) to end up on the freelist:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

memmap_init_range: size=bd49b, nid=0, zone=1, start_pfn=1000, zone_end_pfn=100000

David, is commit 7fef431be9c9 related to this and if so, then how?

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG.

Well, you can call it that, or you can say that things worked under certain assumptions regarding the memory allocation order which are not met any more.

Quoted 1 line

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

Quoted 7 lines

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

Of course, long-term this needs to be addressed in the ACPI initialization code, because it clearly is not robust enough, but in the meantime there's practical breakage observable in the field, so what can be done about that?

#8 lore raw
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG.

Quoted 8 lines
Quoted 8 lines

Well, you can call it that, or you can say that things worked under certain assumptions regarding the memory allocation order which are not met any more.

Quoted 1 line

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

Dan will most certainly object. And I don't know what makes you speak in absolute words here.

Quoted 8 lines
Quoted 7 lines

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

Quoted 4 lines

Of course, long-term this needs to be addressed in the ACPI initialization code, because it clearly is not robust enough, but in the meantime there's practical breakage observable in the field, so what can be done about that?

*joke* enable zone shuffling.

No seriously, fix the latent BUG. What again is problematic about excluding these pages from the page allcoator, for example, via memblock_reserve()?

@Mike?

Signature · 3 lines
Thanks,

David / dhildenb
#9 lore raw
Quoted 7 lines · David Hildenbrand
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Memory gets allocated and used in a different order, which seems to have exposed (yet another) latent BUG.

Well, you can call it that, or you can say that things worked under certain assumptions regarding the memory allocation order which are not met any more.

Regardless of the assumptions in the page allocator we had a page used by the firmware on a free list, which is a bug.

Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

Dan will most certainly object. And I don't know what makes you speak in absolute words here.

Quoted 8 lines
Quoted 7 lines

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

Quoted 4 lines

Of course, long-term this needs to be addressed in the ACPI initialization code, because it clearly is not robust enough, but in the meantime there's practical breakage observable in the field, so what can be done about that?

Quoted 6 lines

*joke* enable zone shuffling.

No seriously, fix the latent BUG. What again is problematic about excluding these pages from the page allcoator, for example, via memblock_reserve()?

@Mike?

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw
Quoted 31 lines
Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

Dan will most certainly object. And I don't know what makes you speak in absolute words here.

Quoted 8 lines
Quoted 7 lines

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

Quoted 4 lines

Of course, long-term this needs to be addressed in the ACPI initialization code, because it clearly is not robust enough, but in the meantime there's practical breakage observable in the field, so what can be done about that?

Quoted 6 lines

*joke* enable zone shuffling.

No seriously, fix the latent BUG. What again is problematic about excluding these pages from the page allcoator, for example, via memblock_reserve()?

@Mike?

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Quoted 9 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

FWIW, something like below would hide our latent BUG again properly (lol). But I guess I don't have to express how ugly and wrong that is. Not to mention what happens if memblock decides to allocate that memory area earlier for some other user (including CMA, ...).

mm/page_alloc.c +15 -0
index 3e4b29ee2b1e..ec71b7c63dbe 100644
@@ -1566,6 +1566,21 @@ void __free_pages_core(struct page *page, unsigned int order)
 
        atomic_long_add(nr_pages, &page_zone(page)->managed_pages);
 
+       /*
+        * BUG ALERT: x86-64 ACPI code has latent BUGs where ACPI tables
+        * that must not get allocated/modified will get exposed to the buddy
+        * as free pages; anybody can allocate and use them once in the free
+        * lists.
+        *
+        * Instead of fixing the BUG, revert the change to the
+        * freeing/allocation order during boot that revealed it and cross
+        * fingers that everything will be fine.
+        */
+       if (system_state < SYSTEM_RUNNING) {
+               __free_pages_ok(page, order, FPI_NONE);
+               return;
+       }
+
        /*
         * Bypass PCP and place fresh pages right to the tail, primarily
         * relevant for memory onlining.
Signature · 3 lines
Thanks,

David / dhildenb
#11 lore raw
Quoted 24 lines · David Hildenbrand
Quoted 24 lines
Quoted 24 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

The same could be reproduced via zone shuffling with a little luck.

But nobody does that in practice.

Dan will most certainly object. And I don't know what makes you speak in absolute words here.

Quoted 8 lines
Quoted 7 lines

This would be relatively straightforward to address if ACPICA was not involved in it, but unfortunately that's not the case.

Changing this part of ACPICA is risky, because such changes may affect other OSes using it, so that requires some serious consideration. Alternatively, the previous memory allocation order in Linux could be restored.

Quoted 4 lines

Of course, long-term this needs to be addressed in the ACPI initialization code, because it clearly is not robust enough, but in the meantime there's practical breakage observable in the field, so what can be done about that?

*joke* enable zone shuffling.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

No seriously, fix the latent BUG. What again is problematic about excluding these pages from the page allcoator, for example, via memblock_reserve()?

@Mike?

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Right.

Quoted 5 lines

FWIW, something like below would hide our latent BUG again properly (lol). But I guess I don't have to express how ugly and wrong that is. Not to mention what happens if memblock decides to allocate that memory area earlier for some other user (including CMA, ...).

Fair enough.

Quoted 29 lines
mm/page_alloc.c +15 -1
index 3e4b29ee2b1e..ec71b7c63dbe 100644
@@ -1566,6 +1566,21 @@ void __free_pages_core(struct page *page, unsigned int order)
 
         atomic_long_add(nr_pages, &page_zone(page)->managed_pages);
 
+       /*
+        * BUG ALERT: x86-64 ACPI code has latent BUGs where ACPI tables
+        * that must not get allocated/modified will get exposed to the buddy
+        * as free pages; anybody can allocate and use them once in the free
+        * lists.
+        *
+        * Instead of fixing the BUG, revert the change to the
+        * freeing/allocation order during boot that revealed it and cross
+        * fingers that everything will be fine.
+        */
+       if (system_state < SYSTEM_RUNNING) {
+               __free_pages_ok(page, order, FPI_NONE);
+               return;
+       }
+
         /*
          * Bypass PCP and place fresh pages right to the tail, primarily
          * relevant for memory onlining.
 
 
--
#12 lore raw
Quoted 17 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice. Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

--------------------------------------------------------------

arch/x86/kernel/setup.c +2 -0
index d883176ef2ce..48bcb1c355ad 100644
@@ -910,6 +910,8 @@ void __init setup_arch(char **cmdline_p)
 
 	parse_early_param();
 
+	acpi_reserve_tables();
+
 	if (efi_enabled(EFI_BOOT))
 		efi_memblock_x86_reserve_range();
 #ifdef CONFIG_MEMORY_HOTPLUG
drivers/acpi/acpica/actables.h +3 -0
index e2d0046799a2..6cb5bcf3fb49 100644
@@ -133,6 +133,9 @@ void
 acpi_tb_install_table_with_override(struct acpi_table_desc *new_table_desc,
 				    u8 override, u32 *table_index);
 
+acpi_physical_address
+acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
+
 acpi_status acpi_tb_parse_root_table(acpi_physical_address rsdp_address);
 
 acpi_status
drivers/acpi/acpica/tbutils.c +1 -5
index 4b9b329a5a92..2ad3c08915d4 100644
@@ -14,10 +14,6 @@
 #define _COMPONENT          ACPI_TABLES
 ACPI_MODULE_NAME("tbutils")
 
-/* Local prototypes */
-static acpi_physical_address
-acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
-
 #if (!ACPI_REDUCED_HARDWARE)
 /*******************************************************************************
  *
@@ -162,7 +158,7 @@ struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  *
  ******************************************************************************/
 
-static acpi_physical_address
+acpi_physical_address
 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
 {
 	u64 address64;
drivers/acpi/tables.c +104 -0

116 lines not on this page · raw message

include/linux/acpi.h +2 -0
index 9f432411e988..d8688e4b6726 100644
@@ -226,6 +226,8 @@ void acpi_boot_table_init (void);
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
+void acpi_reserve_tables(void);
+
 int acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
Signature · 2 lines
Sincerely yours,
Mike.
#13 lore raw
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

However, it looks to me that something like the following could be done to start with:

(a) Make __acpi_map_table() call memblock_reserve() in addition to early_memremap().

My assumption here is that the memblock_reserve() will simply be ignored if it is called too late.

(b) Introduce acpi_reserve_tables() as something like

void __init acpi_table_reserve(void)

{
        acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
}

Because initial_tables is passed to acpi_initialize_tables() above and allow_resize is 0, the array used by it will simply get overwritten when acpi_table_init() gets called.

(c) Make setup_arch() call acpi_table_reserve() like in the original patch from George.

Would that work?

If so, I'll cut a patch along these lines.

#14 lore raw
Quoted 65 lines · Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

However, it looks to me that something like the following could be done to start with:

(a) Make __acpi_map_table() call memblock_reserve() in addition to early_memremap().

My assumption here is that the memblock_reserve() will simply be ignored if it is called too late.

(b) Introduce acpi_reserve_tables() as something like

void __init acpi_table_reserve(void)

{
        acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
}
Quoted 8 lines

Because initial_tables is passed to acpi_initialize_tables() above and allow_resize is 0, the array used by it will simply get overwritten when acpi_table_init() gets called.

(c) Make setup_arch() call acpi_table_reserve() like in the original patch from George.

Would that work?

Well, that doesn't work, so more digging ...

#15 lore raw
In reply to Rafael J. Wysocki
Quoted 41 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 6 lines
Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

Also this still may not play well with initrd-based table overrides. Erik, do you have any insights here?

And ia64 needs to be updated too.

---
 arch/x86/kernel/acpi/boot.c |   12 +++++++++---
 arch/x86/kernel/setup.c     |    3 +++
 drivers/acpi/tables.c       |   24 +++++++++++++++++++++---
 include/linux/acpi.h        |    9 +++++++--
 4 files changed, 40 insertions(+), 8 deletions(-)
arch/x86/kernel/acpi/boot.c +9 -3
Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================
@@ -1541,7 +1541,7 @@ static const struct dmi_system_id acpi_d
  *	...
  */
 
-void __init acpi_boot_table_init(void)
+void __init acpi_boot_table_prepare(void)
 {
 	dmi_check_system(acpi_dmi_table);
 
@@ -1554,10 +1554,16 @@ void __init acpi_boot_table_init(void)
 	/*
 	 * Initialize the ACPI boot-time table parser.
 	 */
-	if (acpi_table_init()) {
+	if (acpi_table_prepare())
 		disable_acpi();
+}
+
+void __init acpi_boot_table_init(void)
+{
+	if (acpi_disabled)
 		return;
-	}
+
+	acpi_table_init();
 
 	acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
arch/x86/kernel/setup.c +3 -0
Index: linux-pm/arch/x86/kernel/setup.c
===================================================================
@@ -1070,6 +1070,9 @@ void __init setup_arch(char **cmdline_p)
 	/* preallocate 4k for mptable mpc */
 	e820__memblock_alloc_reserved_mpc_new();
 
+	/* Look for ACPI tables and reserve memory occupied by them. */
+	acpi_boot_table_prepare();
+
 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
 	setup_bios_corruption_check();
 #endif
include/linux/acpi.h +7 -2
Index: linux-pm/include/linux/acpi.h
===================================================================
@@ -222,11 +222,13 @@ void __iomem *__acpi_map_table(unsigned
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
-int acpi_table_init (void);
+int acpi_table_prepare (void);
+void acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
 			      int entry_id,
@@ -814,9 +816,12 @@ static inline int acpi_boot_init(void)
 	return 0;
 }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
 static inline void acpi_boot_table_init(void)
 {
-	return;
 }
 
 static inline int acpi_mps_check(void)
drivers/acpi/tables.c +21 -3
Index: linux-pm/drivers/acpi/tables.c
===================================================================
@@ -788,9 +788,10 @@ acpi_status acpi_os_table_override(struc
  * result: sdt_entry[] is initialized
  */
 
-int __init acpi_table_init(void)
+int __init acpi_table_prepare(void)
 {
 	acpi_status status;
+	int i;
 
 	if (acpi_verify_table_checksum) {
 		pr_info("Early table checksum verification enabled\n");
@@ -803,12 +804,29 @@ int __init acpi_table_init(void)
 	status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
 	if (ACPI_FAILURE(status))
 		return -EINVAL;
-	acpi_table_initrd_scan();
 
-	check_multiple_madt();
+	for (i = 0; i < ACPI_MAX_TABLES; i++) {
+		struct acpi_table_desc *table_desc = &initial_tables[i];
+
+		if (!table_desc->address || !table_desc->length)
+			break;
+
+		pr_info("Reserving %4s table memory at [0x%llx - 0x%llx]\n",
+			table_desc->signature.ascii, table_desc->address,
+			table_desc->address + table_desc->length - 1);
+
+		memblock_reserve(table_desc->address, table_desc->length);
+	}
+
 	return 0;
 }
 
+void __init acpi_table_init(void)
+{
+	acpi_table_initrd_scan();
+	check_multiple_madt();
+}
+
 static int __init acpi_parse_apic_instance(char *str)
 {
 	if (!str)
#16 lore raw
Quoted 34 lines · Rafael J. Wysocki
Quoted 33 lines · CET Rafael J. Wysocki
Quoted 15 lines · Mike Rapoport
Quoted 14 lines · Rafael J. Wysocki
Quoted 2 lines · David Hildenbrand
Quoted 2 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

So maybe something like the patch below?

Do you want me to try it out in the failing setup?

George

Quoted 145 lines

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

Also this still may not play well with initrd-based table overrides. Erik, do you have any insights here?

And ia64 needs to be updated too.

---
  arch/x86/kernel/acpi/boot.c |   12 +++++++++---
  arch/x86/kernel/setup.c     |    3 +++
  drivers/acpi/tables.c       |   24 +++++++++++++++++++++---
  include/linux/acpi.h        |    9 +++++++--
  4 files changed, 40 insertions(+), 8 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +9 -3
@@ -1541,7 +1541,7 @@ static const struct dmi_system_id acpi_d
   *	...
   */
 
-void __init acpi_boot_table_init(void)
+void __init acpi_boot_table_prepare(void)
  {
  	dmi_check_system(acpi_dmi_table);
 
@@ -1554,10 +1554,16 @@ void __init acpi_boot_table_init(void)
  	/*
  	 * Initialize the ACPI boot-time table parser.
  	 */
-	if (acpi_table_init()) {
+	if (acpi_table_prepare())
  		disable_acpi();
+}
+
+void __init acpi_boot_table_init(void)
+{
+	if (acpi_disabled)
  		return;
-	}
+
+	acpi_table_init();
 
  	acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -0
@@ -1070,6 +1070,9 @@ void __init setup_arch(char **cmdline_p)
  	/* preallocate 4k for mptable mpc */
  	e820__memblock_alloc_reserved_mpc_new();
 
+	/* Look for ACPI tables and reserve memory occupied by them. */
+	acpi_boot_table_prepare();
+
  #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
  	setup_bios_corruption_check();
  #endif

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +7 -2
@@ -222,11 +222,13 @@ void __iomem *__acpi_map_table(unsigned
  void __acpi_unmap_table(void __iomem *map, unsigned long size);
  int early_acpi_boot_init(void);
  int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
  void acpi_boot_table_init (void);
  int acpi_mps_check (void);
  int acpi_numa_init (void);
 
-int acpi_table_init (void);
+int acpi_table_prepare (void);
+void acpi_table_init (void);
  int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
  int __init acpi_table_parse_entries(char *id, unsigned long table_size,
  			      int entry_id,
@@ -814,9 +816,12 @@ static inline int acpi_boot_init(void)
  	return 0;
  }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
  static inline void acpi_boot_table_init(void)
  {
-	return;
  }
 
  static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +21 -3
@@ -788,9 +788,10 @@ acpi_status acpi_os_table_override(struc
   * result: sdt_entry[] is initialized
   */
 
-int __init acpi_table_init(void)
+int __init acpi_table_prepare(void)
  {
  	acpi_status status;
+	int i;
 
  	if (acpi_verify_table_checksum) {
  		pr_info("Early table checksum verification enabled\n");
@@ -803,12 +804,29 @@ int __init acpi_table_init(void)
  	status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
  	if (ACPI_FAILURE(status))
  		return -EINVAL;
-	acpi_table_initrd_scan();
 
-	check_multiple_madt();
+	for (i = 0; i < ACPI_MAX_TABLES; i++) {
+		struct acpi_table_desc *table_desc = &initial_tables[i];
+
+		if (!table_desc->address || !table_desc->length)
+			break;
+
+		pr_info("Reserving %4s table memory at [0x%llx - 0x%llx]\n",
+			table_desc->signature.ascii, table_desc->address,
+			table_desc->address + table_desc->length - 1);
+
+		memblock_reserve(table_desc->address, table_desc->length);
+	}
+
  	return 0;
  }
 
+void __init acpi_table_init(void)
+{
+	acpi_table_initrd_scan();
+	check_multiple_madt();
+}
+
  static int __init acpi_parse_apic_instance(char *str)
  {
  	if (!str)
#17 lore raw
Quoted 38 lines · George Kennedy
Quoted 34 lines · Rafael J. Wysocki
Quoted 33 lines · CET Rafael J. Wysocki
Quoted 15 lines · Mike Rapoport
Quoted 14 lines · Rafael J. Wysocki
Quoted 2 lines · David Hildenbrand
Quoted 2 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

So maybe something like the patch below?

Do you want me to try it out in the failing setup?

Yes, please!

#18 lore raw
In reply to Rafael J. Wysocki
Quoted 47 lines · Rafael J. Wysocki
Quoted 46 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

Quoted 5 lines
Quoted 1 line

I agree that this should be the direction to go into.

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

To be 100% safe it should be called before e820__memblock_setup(). It is possible to call memblock_reserve() at any time, even before the actual memory is detected as long as all reservations fit into the static array that currently has 128 entries on x86.

As e820__memblock_setup() essentially enables memblock allocations, theoretically the memory occupied by ACPI tables can be allocated even in x86::setup_arch() unless it is reserved before e820__memblock_setup().

Quoted 4 lines

Also this still may not play well with initrd-based table overrides. Erik, do you have any insights here?

And ia64 needs to be updated too.

I think arm64 as well.

Quoted 58 lines
---
 arch/x86/kernel/acpi/boot.c |   12 +++++++++---
 arch/x86/kernel/setup.c     |    3 +++
 drivers/acpi/tables.c       |   24 +++++++++++++++++++++---
 include/linux/acpi.h        |    9 +++++++--
 4 files changed, 40 insertions(+), 8 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +9 -3
@@ -1541,7 +1541,7 @@ static const struct dmi_system_id acpi_d
  *	...
  */
 
-void __init acpi_boot_table_init(void)
+void __init acpi_boot_table_prepare(void)
 {
 	dmi_check_system(acpi_dmi_table);
 
@@ -1554,10 +1554,16 @@ void __init acpi_boot_table_init(void)
 	/*
 	 * Initialize the ACPI boot-time table parser.
 	 */
-	if (acpi_table_init()) {
+	if (acpi_table_prepare())
 		disable_acpi();
+}
+
+void __init acpi_boot_table_init(void)
+{
+	if (acpi_disabled)
 		return;
-	}
+
+	acpi_table_init();
 
 	acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -0
@@ -1070,6 +1070,9 @@ void __init setup_arch(char **cmdline_p)
 	/* preallocate 4k for mptable mpc */
 	e820__memblock_alloc_reserved_mpc_new();
 
+	/* Look for ACPI tables and reserve memory occupied by them. */
+	acpi_boot_table_prepare();
+
 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
 	setup_bios_corruption_check();
 #endif

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h void __iomem *__acpi_map_table(unsigned · line 226

Quoted 5 lines
include/linux/acpi.h +1 -0
@@ -222,11 +222,13 @@ void __iomem *__acpi_map_table(unsigned
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);

Not related to this patch, but it feels to me like there are too many acpi_boot_something() :)

Quoted 74 lines
include/linux/acpi.h +6 -2
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
-int acpi_table_init (void);
+int acpi_table_prepare (void);
+void acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
 			      int entry_id,
@@ -814,9 +816,12 @@ static inline int acpi_boot_init(void)
 	return 0;
 }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
 static inline void acpi_boot_table_init(void)
 {
-	return;
 }
 
 static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +21 -3
@@ -788,9 +788,10 @@ acpi_status acpi_os_table_override(struc
  * result: sdt_entry[] is initialized
  */
 
-int __init acpi_table_init(void)
+int __init acpi_table_prepare(void)
 {
 	acpi_status status;
+	int i;
 
 	if (acpi_verify_table_checksum) {
 		pr_info("Early table checksum verification enabled\n");
@@ -803,12 +804,29 @@ int __init acpi_table_init(void)
 	status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
 	if (ACPI_FAILURE(status))
 		return -EINVAL;
-	acpi_table_initrd_scan();
 
-	check_multiple_madt();
+	for (i = 0; i < ACPI_MAX_TABLES; i++) {
+		struct acpi_table_desc *table_desc = &initial_tables[i];
+
+		if (!table_desc->address || !table_desc->length)
+			break;
+
+		pr_info("Reserving %4s table memory at [0x%llx - 0x%llx]\n",
+			table_desc->signature.ascii, table_desc->address,
+			table_desc->address + table_desc->length - 1);
+
+		memblock_reserve(table_desc->address, table_desc->length);
+	}
+
 	return 0;
 }
 
+void __init acpi_table_init(void)
+{
+	acpi_table_initrd_scan();
+	check_multiple_madt();
+}
+
 static int __init acpi_parse_apic_instance(char *str)
 {
 	if (!str)
Signature · 2 lines
Sincerely yours,
Mike.
#19 lore raw
Quoted 49 lines · Mike Rapoport
Quoted 47 lines · Rafael J. Wysocki
Quoted 46 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I agree that this should be the direction to go into.

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

To be 100% safe it should be called before e820__memblock_setup().

OK

Quoted 8 lines

It is possible to call memblock_reserve() at any time, even before the actual memory is detected as long as all reservations fit into the static array that currently has 128 entries on x86.

As e820__memblock_setup() essentially enables memblock allocations, theoretically the memory occupied by ACPI tables can be allocated even in x86::setup_arch() unless it is reserved before e820__memblock_setup().

Quoted 6 lines
Quoted 4 lines

Also this still may not play well with initrd-based table overrides. Erik, do you have any insights here?

And ia64 needs to be updated too.

I think arm64 as well.

Right.

Quoted 58 lines
Quoted 58 lines
---
 arch/x86/kernel/acpi/boot.c |   12 +++++++++---
 arch/x86/kernel/setup.c     |    3 +++
 drivers/acpi/tables.c       |   24 +++++++++++++++++++++---
 include/linux/acpi.h        |    9 +++++++--
 4 files changed, 40 insertions(+), 8 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +9 -3
@@ -1541,7 +1541,7 @@ static const struct dmi_system_id acpi_d
  *   ...
  */
 
-void __init acpi_boot_table_init(void)
+void __init acpi_boot_table_prepare(void)
 {
      dmi_check_system(acpi_dmi_table);
 
@@ -1554,10 +1554,16 @@ void __init acpi_boot_table_init(void)
      /*
       * Initialize the ACPI boot-time table parser.
       */
-     if (acpi_table_init()) {
+     if (acpi_table_prepare())
              disable_acpi();
+}
+
+void __init acpi_boot_table_init(void)
+{
+     if (acpi_disabled)
              return;
-     }
+
+     acpi_table_init();
 
      acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -0
@@ -1070,6 +1070,9 @@ void __init setup_arch(char **cmdline_p)
      /* preallocate 4k for mptable mpc */
      e820__memblock_alloc_reserved_mpc_new();
 
+     /* Look for ACPI tables and reserve memory occupied by them. */
+     acpi_boot_table_prepare();
+
 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
      setup_bios_corruption_check();
 #endif

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +0 -0
@@ -222,11 +222,13 @@ void __iomem *__acpi_map_table(unsigned
Quoted 8 lines

include/linux/acpi.h void __iomem *__acpi_map_table(unsigned · line 226

Quoted 5 lines
include/linux/acpi.h +1 -0
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);

Not related to this patch, but it feels to me like there are too many acpi_boot_something() :)

Well, there was one initially, but it has been split for a few times due to ordering issues similar to the one at hand.

It could be cleaned up I suppose, though.

#20 lore raw
Quoted 53 lines · Rafael J. Wysocki
Quoted 51 lines · Mike Rapoport
Quoted 49 lines · Rafael J. Wysocki
Quoted 47 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

To be 100% safe it should be called before e820__memblock_setup().

OK

Well, that said, reserve_bios_regions() doesn't seem to have concerns like this and I'm not sure why ACPI tables should be reserved before this runs. That applies to efi_reserve_boot_services() too.

I can put the new call before e820__memblock_alloc_reserved_mpc_new(), but I'm not sure why to put it before efi_reserve_boot_services(), say?

#21 lore raw
Quoted 62 lines · Rafael J. Wysocki
Quoted 53 lines · Rafael J. Wysocki
Quoted 51 lines · Mike Rapoport
Quoted 49 lines · Rafael J. Wysocki
Quoted 47 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

To be 100% safe it should be called before e820__memblock_setup().

OK

Quoted 7 lines

Well, that said, reserve_bios_regions() doesn't seem to have concerns like this and I'm not sure why ACPI tables should be reserved before this runs. That applies to efi_reserve_boot_services() too.

I can put the new call before e820__memblock_alloc_reserved_mpc_new(), but I'm not sure why to put it before efi_reserve_boot_services(), say?

The general idea is to reserve all the memory used by the firmware before memblock allocations are possible, i.e. before e820__memblock_setup(). Currently this is not the case, but it does not make it more correct.

Theoretically, it is possible that reserve_bios_regions() will cause a memory allocation and the allocated memory will be exactly at the area where ACPI tables reside.

In practice I believe this is very unlikely, but who knows.

Another advantage of having ACPI tables handy by the time we do the memory detection is that we will be able to SRAT earlier and simplify NUMA initialization.

Signature · 2 lines
Sincerely yours,
Mike.
#22 lore raw
Quoted 68 lines · Mike Rapoport
Quoted 66 lines · Rafael J. Wysocki
Quoted 53 lines · Rafael J. Wysocki
Quoted 51 lines · Mike Rapoport
Quoted 49 lines · Rafael J. Wysocki
Quoted 47 lines · CET Rafael J. Wysocki
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Rafael J. Wysocki
Quoted 3 lines · David Hildenbrand
Quoted 3 lines

There is some care that should be taken to make sure we get the order right, but I don't see a fundamental issue here.

Me neither.

Quoted 3 lines
Quoted 3 lines

If I understand correctly, Rafael's concern is about changing the parts of ACPICA that should be OS agnostic, so I think we just need another place to call memblock_reserve() rather than acpi_tb_install_table_with_override().

Something like this.

There is also the problem that memblock_reserve() needs to be called for all of the tables early enough, which will require some reordering of the early init code.

Quoted 4 lines
Quoted 4 lines

Since the reservation should be done early in x86::setup_arch() (and probably in arm64::setup_arch()) we might just have a function that parses table headers and reserves them, similarly to how we parse the tables during KASLR setup.

Quoted 4 lines
Quoted 1 line

Right.

I've looked at it a bit more and we do something like the patch below that nearly duplicates acpi_tb_parse_root_table() which is not very nice.

It looks to me that the code need not be duplicated (see below).

Quoted 3 lines

Besides, reserving ACPI tables early and then calling acpi_table_init() (and acpi_tb_parse_root_table() again would mean doing the dance with early_memremap() twice for no good reason.

That'd be simply inefficient which is kind of acceptable to me to start with.

And I changing the ACPICA code can be avoided at least initially, it by itself would be a good enough reason.

Quoted 4 lines

I believe the most effective way to deal with this would be to have a function that does parsing, reservation and installs the tables supplied by the firmware which can be called really early and then another function that overrides tables if needed a some later point.

I agree that this should be the direction to go into.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So maybe something like the patch below?

I'm not sure if acpi_boot_table_prepare() gets called early enough, though.

To be 100% safe it should be called before e820__memblock_setup().

OK

Well, that said, reserve_bios_regions() doesn't seem to have concerns like this and I'm not sure why ACPI tables should be reserved before this runs. That applies to efi_reserve_boot_services() too.

Quoted 7 lines
Quoted 3 lines

I can put the new call before e820__memblock_alloc_reserved_mpc_new(), but I'm not sure why to put it before efi_reserve_boot_services(), say?

The general idea is to reserve all the memory used by the firmware before memblock allocations are possible, i.e. before e820__memblock_setup(). Currently this is not the case, but it does not make it more correct.

I see.

Quoted 9 lines

Theoretically, it is possible that reserve_bios_regions() will cause a memory allocation and the allocated memory will be exactly at the area where ACPI tables reside.

In practice I believe this is very unlikely, but who knows.

Another advantage of having ACPI tables handy by the time we do the memory detection is that we will be able to SRAT earlier and simplify NUMA initialization.

OK, fair enough.

#23 lore raw

PATCH 1/1 ACPI: tables: x86: Reserve memory occupied by ACPI tables

In reply to Mike Rapoport

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

 BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
 Read of size 4 at addr ffff8880be453004 by task swapper/0/1
 CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
 Call Trace:
  dump_stack+0xf6/0x158
  print_address_description.constprop.9+0x41/0x60
  kasan_report.cold.14+0x7b/0xd4
  __asan_report_load_n_noabort+0xf/0x20
  ibft_init+0x134/0xc49
  do_one_initcall+0xc4/0x3e0
  kernel_init_freeable+0x5af/0x66b
  kernel_init+0x16/0x1d0
  ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

The other architectures using ACPI should not be affected by this change.

---
 arch/x86/kernel/acpi/boot.c |   25 ++++++++++++-------------
 arch/x86/kernel/setup.c     |    8 +++-----
 drivers/acpi/tables.c       |   42 +++++++++++++++++++++++++++++++++++++++---
 include/linux/acpi.h        |    9 ++++++++-
 4 files changed, 62 insertions(+), 22 deletions(-)
arch/x86/kernel/acpi/boot.c +12 -13
Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================
@@ -1554,10 +1554,18 @@ void __init acpi_boot_table_init(void)
 	/*
 	 * Initialize the ACPI boot-time table parser.
 	 */
-	if (acpi_table_init()) {
+	if (acpi_locate_initial_tables())
 		disable_acpi();
-		return;
-	}
+	else
+		acpi_reserve_initial_tables();
+}
+
+int __init early_acpi_boot_init(void)
+{
+	if (acpi_disabled)
+		return 1;
+
+	acpi_table_init_complete();
 
 	acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
@@ -1570,18 +1578,9 @@ void __init acpi_boot_table_init(void)
 		} else {
 			printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
 			disable_acpi();
-			return;
+			return 1;
 		}
 	}
-}
-
-int __init early_acpi_boot_init(void)
-{
-	/*
-	 * If acpi_disabled, bail out
-	 */
-	if (acpi_disabled)
-		return 1;
 
 	/*
 	 * Process the Multiple APIC Description Table (MADT), if present
arch/x86/kernel/setup.c +3 -5
Index: linux-pm/arch/x86/kernel/setup.c
===================================================================
@@ -1045,6 +1045,9 @@ void __init setup_arch(char **cmdline_p)
 
 	cleanup_highmap();
 
+	/* Look for ACPI tables and reserve memory occupied by them. */
+	acpi_boot_table_init();
+
 	memblock_set_current_limit(ISA_END_ADDRESS);
 	e820__memblock_setup();
 
@@ -1136,11 +1139,6 @@ void __init setup_arch(char **cmdline_p)
 
 	early_platform_quirks();
 
-	/*
-	 * Parse the ACPI tables for possible boot-time SMP configuration.
-	 */
-	acpi_boot_table_init();
-
 	early_acpi_boot_init();
 
 	initmem_init();
include/linux/acpi.h +8 -1
Index: linux-pm/include/linux/acpi.h
===================================================================
@@ -222,10 +222,14 @@ void __iomem *__acpi_map_table(unsigned
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
+int acpi_locate_initial_tables (void);
+void acpi_reserve_initial_tables (void);
+void acpi_table_init_complete (void);
 int acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
@@ -814,9 +818,12 @@ static inline int acpi_boot_init(void)
 	return 0;
 }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
 static inline void acpi_boot_table_init(void)
 {
-	return;
 }
 
 static inline int acpi_mps_check(void)
drivers/acpi/tables.c +39 -3

62 lines not on this page · raw message

Reviewed-by on v1 1/1
#24 lore raw

Re: [PATCH] ACPI: tables: x86: Reserve memory occupied by ACPI tables

Quoted 35 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

 BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
 Read of size 4 at addr ffff8880be453004 by task swapper/0/1
 CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
 Call Trace:
  dump_stack+0xf6/0x158
  print_address_description.constprop.9+0x41/0x60
  kasan_report.cold.14+0x7b/0xd4
  __asan_report_load_n_noabort+0xf/0x20
  ibft_init+0x134/0xc49
  do_one_initcall+0xc4/0x3e0
  kernel_init_freeable+0x5af/0x66b
  kernel_init+0x16/0x1d0
  ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

Quoted 6 lines

The other architectures using ACPI should not be affected by this change.

FWIW:

Quoted 184 lines
---
 arch/x86/kernel/acpi/boot.c |   25 ++++++++++++-------------
 arch/x86/kernel/setup.c     |    8 +++-----
 drivers/acpi/tables.c       |   42 +++++++++++++++++++++++++++++++++++++++---
 include/linux/acpi.h        |    9 ++++++++-
 4 files changed, 62 insertions(+), 22 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +12 -13
@@ -1554,10 +1554,18 @@ void __init acpi_boot_table_init(void)
 	/*
 	 * Initialize the ACPI boot-time table parser.
 	 */
-	if (acpi_table_init()) {
+	if (acpi_locate_initial_tables())
 		disable_acpi();
-		return;
-	}
+	else
+		acpi_reserve_initial_tables();
+}
+
+int __init early_acpi_boot_init(void)
+{
+	if (acpi_disabled)
+		return 1;
+
+	acpi_table_init_complete();
 
 	acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
@@ -1570,18 +1578,9 @@ void __init acpi_boot_table_init(void)
 		} else {
 			printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
 			disable_acpi();
-			return;
+			return 1;
 		}
 	}
-}
-
-int __init early_acpi_boot_init(void)
-{
-	/*
-	 * If acpi_disabled, bail out
-	 */
-	if (acpi_disabled)
-		return 1;
 
 	/*
 	 * Process the Multiple APIC Description Table (MADT), if present

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -5
@@ -1045,6 +1045,9 @@ void __init setup_arch(char **cmdline_p)
 
 	cleanup_highmap();
 
+	/* Look for ACPI tables and reserve memory occupied by them. */
+	acpi_boot_table_init();
+
 	memblock_set_current_limit(ISA_END_ADDRESS);
 	e820__memblock_setup();
 
@@ -1136,11 +1139,6 @@ void __init setup_arch(char **cmdline_p)
 
 	early_platform_quirks();
 
-	/*
-	 * Parse the ACPI tables for possible boot-time SMP configuration.
-	 */
-	acpi_boot_table_init();
-
 	early_acpi_boot_init();
 
 	initmem_init();

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +8 -1
@@ -222,10 +222,14 @@ void __iomem *__acpi_map_table(unsigned
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
+int acpi_locate_initial_tables (void);
+void acpi_reserve_initial_tables (void);
+void acpi_table_init_complete (void);
 int acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
@@ -814,9 +818,12 @@ static inline int acpi_boot_init(void)
 	return 0;
 }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
 static inline void acpi_boot_table_init(void)
 {
-	return;
 }
 
 static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +39 -3
@@ -780,7 +780,7 @@ acpi_status acpi_os_table_override(struc
 }
 
 /*
- * acpi_table_init()
+ * acpi_locate_initial_tables()
  *
  * find RSDP, find and checksum SDT/XSDT.
  * checksum all tables, print SDT/XSDT
@@ -788,7 +788,7 @@ acpi_status acpi_os_table_override(struc
  * result: sdt_entry[] is initialized
  */
 
-int __init acpi_table_init(void)
+int __init acpi_locate_initial_tables(void)
 {
 	acpi_status status;
 
@@ -803,9 +803,45 @@ int __init acpi_table_init(void)
 	status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
 	if (ACPI_FAILURE(status))
 		return -EINVAL;
-	acpi_table_initrd_scan();
 
+	return 0;
+}
+
+void __init acpi_reserve_initial_tables(void)
+{
+	int i;
+
+	for (i = 0; i < ACPI_MAX_TABLES; i++) {
+		struct acpi_table_desc *table_desc = &initial_tables[i];
+		u64 start = table_desc->address;
+		u64 size = table_desc->length;
+
+		if (!start || !size)
+			break;
+
+		pr_info("Reserving %4s table memory at [mem 0x%llx-0x%llx]\n",
+			table_desc->signature.ascii, start, start + size - 1);
+
+		memblock_reserve(start, size);
+	}
+}
+
+void __init acpi_table_init_complete(void)
+{
+	acpi_table_initrd_scan();
 	check_multiple_madt();
+}
+
+int __init acpi_table_init(void)
+{
+	int ret;
+
+	ret = acpi_locate_initial_tables();
+	if (ret)
+		return ret;
+
+	acpi_table_init_complete();
+
 	return 0;
 }
 
Signature · 2 lines
Sincerely yours,
Mike.
#25 lore raw

Re: [PATCH] ACPI: tables: x86: Reserve memory occupied by ACPI tables

Quoted 40 lines · Mike Rapoport
Quoted 38 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

 BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
 Read of size 4 at addr ffff8880be453004 by task swapper/0/1
 CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
 Call Trace:
  dump_stack+0xf6/0x158
  print_address_description.constprop.9+0x41/0x60
  kasan_report.cold.14+0x7b/0xd4
  __asan_report_load_n_noabort+0xf/0x20
  ibft_init+0x134/0xc49
  do_one_initcall+0xc4/0x3e0
  kernel_init_freeable+0x5af/0x66b
  kernel_init+0x16/0x1d0
  ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

The other architectures using ACPI should not be affected by this change.

Quoted 6 lines
Quoted 3 lines

FWIW:

Thank you!

George, can you please try this patch on the affected system?

Quoted 188 lines
Quoted 174 lines
---
 arch/x86/kernel/acpi/boot.c |   25 ++++++++++++-------------
 arch/x86/kernel/setup.c     |    8 +++-----
 drivers/acpi/tables.c       |   42 +++++++++++++++++++++++++++++++++++++++---
 include/linux/acpi.h        |    9 ++++++++-
 4 files changed, 62 insertions(+), 22 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +12 -13
@@ -1554,10 +1554,18 @@ void __init acpi_boot_table_init(void)
      /*
       * Initialize the ACPI boot-time table parser.
       */
-     if (acpi_table_init()) {
+     if (acpi_locate_initial_tables())
              disable_acpi();
-             return;
-     }
+     else
+             acpi_reserve_initial_tables();
+}
+
+int __init early_acpi_boot_init(void)
+{
+     if (acpi_disabled)
+             return 1;
+
+     acpi_table_init_complete();
 
      acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
@@ -1570,18 +1578,9 @@ void __init acpi_boot_table_init(void)
              } else {
                      printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
                      disable_acpi();
-                     return;
+                     return 1;
              }
      }
-}
-
-int __init early_acpi_boot_init(void)
-{
-     /*
-      * If acpi_disabled, bail out
-      */
-     if (acpi_disabled)
-             return 1;
 
      /*
       * Process the Multiple APIC Description Table (MADT), if present

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -5
@@ -1045,6 +1045,9 @@ void __init setup_arch(char **cmdline_p)
 
      cleanup_highmap();
 
+     /* Look for ACPI tables and reserve memory occupied by them. */
+     acpi_boot_table_init();
+
      memblock_set_current_limit(ISA_END_ADDRESS);
      e820__memblock_setup();
 
@@ -1136,11 +1139,6 @@ void __init setup_arch(char **cmdline_p)
 
      early_platform_quirks();
 
-     /*
-      * Parse the ACPI tables for possible boot-time SMP configuration.
-      */
-     acpi_boot_table_init();
-
      early_acpi_boot_init();
 
      initmem_init();

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +8 -1
@@ -222,10 +222,14 @@ void __iomem *__acpi_map_table(unsigned
 void __acpi_unmap_table(void __iomem *map, unsigned long size);
 int early_acpi_boot_init(void);
 int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
 void acpi_boot_table_init (void);
 int acpi_mps_check (void);
 int acpi_numa_init (void);
 
+int acpi_locate_initial_tables (void);
+void acpi_reserve_initial_tables (void);
+void acpi_table_init_complete (void);
 int acpi_table_init (void);
 int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
@@ -814,9 +818,12 @@ static inline int acpi_boot_init(void)
      return 0;
 }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
 static inline void acpi_boot_table_init(void)
 {
-     return;
 }
 
 static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +32 -3
@@ -780,7 +780,7 @@ acpi_status acpi_os_table_override(struc
 }
 
 /*
- * acpi_table_init()
+ * acpi_locate_initial_tables()
  *
  * find RSDP, find and checksum SDT/XSDT.
  * checksum all tables, print SDT/XSDT
@@ -788,7 +788,7 @@ acpi_status acpi_os_table_override(struc
  * result: sdt_entry[] is initialized
  */
 
-int __init acpi_table_init(void)
+int __init acpi_locate_initial_tables(void)
 {
      acpi_status status;
 
@@ -803,9 +803,45 @@ int __init acpi_table_init(void)
      status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
      if (ACPI_FAILURE(status))
              return -EINVAL;
-     acpi_table_initrd_scan();
 
+     return 0;
+}
+
+void __init acpi_reserve_initial_tables(void)
+{
+     int i;
+
+     for (i = 0; i < ACPI_MAX_TABLES; i++) {
+             struct acpi_table_desc *table_desc = &initial_tables[i];
+             u64 start = table_desc->address;
+             u64 size = table_desc->length;
+
+             if (!start || !size)
+                     break;
+
+             pr_info("Reserving %4s table memory at [mem 0x%llx-0x%llx]\n",
+                     table_desc->signature.ascii, start, start + size - 1);
+
+             memblock_reserve(start, size);
+     }
+}
+
+void __init acpi_table_init_complete(void)
+{
+     acpi_table_initrd_scan();
      check_multiple_madt();
+}
+
+int __init acpi_table_init(void)
+{
+     int ret;

drivers/acpi/tables.c int __init acpi_table_init(void) · line 847

Quoted 10 lines
drivers/acpi/tables.c +7 -0
+
+     ret = acpi_locate_initial_tables();
+     if (ret)
+             return ret;
+
+     acpi_table_init_complete();
+
      return 0;
 }
 

--
Sincerely yours,
Mike.

#26 lore raw

Re: [PATCH] ACPI: tables: x86: Reserve memory occupied by ACPI tables

Quoted 40 lines · Rafael J. Wysocki
Quoted 39 lines · Mike Rapoport
Quoted 38 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

  BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
  Read of size 4 at addr ffff8880be453004 by task swapper/0/1
  CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
  Call Trace:
   dump_stack+0xf6/0x158
   print_address_description.constprop.9+0x41/0x60
   kasan_report.cold.14+0x7b/0xd4
   __asan_report_load_n_noabort+0xf/0x20
   ibft_init+0x134/0xc49
   do_one_initcall+0xc4/0x3e0
   kernel_init_freeable+0x5af/0x66b
   kernel_init+0x16/0x1d0
   ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

The other architectures using ACPI should not be affected by this change.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

FWIW:

Thank you!

George, can you please try this patch on the affected system?

Will do.

George

Quoted 188 lines
Quoted 188 lines
Quoted 175 lines
---
  arch/x86/kernel/acpi/boot.c |   25 ++++++++++++-------------
  arch/x86/kernel/setup.c     |    8 +++-----
  drivers/acpi/tables.c       |   42 +++++++++++++++++++++++++++++++++++++++---
  include/linux/acpi.h        |    9 ++++++++-
  4 files changed, 62 insertions(+), 22 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +12 -13
@@ -1554,10 +1554,18 @@ void __init acpi_boot_table_init(void)
       /*
        * Initialize the ACPI boot-time table parser.
        */
-     if (acpi_table_init()) {
+     if (acpi_locate_initial_tables())
               disable_acpi();
-             return;
-     }
+     else
+             acpi_reserve_initial_tables();
+}
+
+int __init early_acpi_boot_init(void)
+{
+     if (acpi_disabled)
+             return 1;
+
+     acpi_table_init_complete();
 
       acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
@@ -1570,18 +1578,9 @@ void __init acpi_boot_table_init(void)
               } else {
                       printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
                       disable_acpi();
-                     return;
+                     return 1;
               }
       }
-}
-
-int __init early_acpi_boot_init(void)
-{
-     /*
-      * If acpi_disabled, bail out
-      */
-     if (acpi_disabled)
-             return 1;
 
       /*
        * Process the Multiple APIC Description Table (MADT), if present

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -5
@@ -1045,6 +1045,9 @@ void __init setup_arch(char **cmdline_p)
 
       cleanup_highmap();
 
+     /* Look for ACPI tables and reserve memory occupied by them. */
+     acpi_boot_table_init();
+
       memblock_set_current_limit(ISA_END_ADDRESS);
       e820__memblock_setup();
 
@@ -1136,11 +1139,6 @@ void __init setup_arch(char **cmdline_p)
 
       early_platform_quirks();
 
-     /*
-      * Parse the ACPI tables for possible boot-time SMP configuration.
-      */
-     acpi_boot_table_init();
-
       early_acpi_boot_init();
 
       initmem_init();

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +8 -1
@@ -222,10 +222,14 @@ void __iomem *__acpi_map_table(unsigned
  void __acpi_unmap_table(void __iomem *map, unsigned long size);
  int early_acpi_boot_init(void);
  int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
  void acpi_boot_table_init (void);
  int acpi_mps_check (void);
  int acpi_numa_init (void);
 
+int acpi_locate_initial_tables (void);
+void acpi_reserve_initial_tables (void);
+void acpi_table_init_complete (void);
  int acpi_table_init (void);
  int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
  int __init acpi_table_parse_entries(char *id, unsigned long table_size,
@@ -814,9 +818,12 @@ static inline int acpi_boot_init(void)
       return 0;
  }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
  static inline void acpi_boot_table_init(void)
  {
-     return;
  }
 
  static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +32 -3
@@ -780,7 +780,7 @@ acpi_status acpi_os_table_override(struc
  }
 
  /*
- * acpi_table_init()
+ * acpi_locate_initial_tables()
   *
   * find RSDP, find and checksum SDT/XSDT.
   * checksum all tables, print SDT/XSDT
@@ -788,7 +788,7 @@ acpi_status acpi_os_table_override(struc
   * result: sdt_entry[] is initialized
   */
 
-int __init acpi_table_init(void)
+int __init acpi_locate_initial_tables(void)
  {
       acpi_status status;
 
@@ -803,9 +803,45 @@ int __init acpi_table_init(void)
       status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
       if (ACPI_FAILURE(status))
               return -EINVAL;
-     acpi_table_initrd_scan();
 
+     return 0;
+}
+
+void __init acpi_reserve_initial_tables(void)
+{
+     int i;
+
+     for (i = 0; i < ACPI_MAX_TABLES; i++) {
+             struct acpi_table_desc *table_desc = &initial_tables[i];
+             u64 start = table_desc->address;
+             u64 size = table_desc->length;
+
+             if (!start || !size)
+                     break;
+
+             pr_info("Reserving %4s table memory at [mem 0x%llx-0x%llx]\n",
+                     table_desc->signature.ascii, start, start + size - 1);
+
+             memblock_reserve(start, size);
+     }
+}
+
+void __init acpi_table_init_complete(void)
+{
+     acpi_table_initrd_scan();
       check_multiple_madt();
+}
+
+int __init acpi_table_init(void)
+{
+     int ret;

drivers/acpi/tables.c int __init acpi_table_init(void) · line 847

Quoted 10 lines
drivers/acpi/tables.c +7 -0
+
+     ret = acpi_locate_initial_tables();
+     if (ret)
+             return ret;
+
+     acpi_table_init_complete();
+
       return 0;
  }
 

--
Sincerely yours,
Mike.

#27 lore raw

Re: [PATCH] ACPI: tables: x86: Reserve memory occupied by ACPI tables

In reply to Rafael J. Wysocki
Quoted 40 lines · Rafael J. Wysocki
Quoted 39 lines · Mike Rapoport
Quoted 38 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

  BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
  Read of size 4 at addr ffff8880be453004 by task swapper/0/1
  CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
  Call Trace:
   dump_stack+0xf6/0x158
   print_address_description.constprop.9+0x41/0x60
   kasan_report.cold.14+0x7b/0xd4
   __asan_report_load_n_noabort+0xf/0x20
   ibft_init+0x134/0xc49
   do_one_initcall+0xc4/0x3e0
   kernel_init_freeable+0x5af/0x66b
   kernel_init+0x16/0x1d0
   ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

The other architectures using ACPI should not be affected by this change.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

FWIW:

Thank you!

George, can you please try this patch on the affected system?

Rafael,

10 for 10 successful reboots with your patch.

First, verified the failure is still there with latest 5.12.0-rc4.

George
P.S. Thanks Mike, Rafael & David

Quoted 188 lines
Quoted 188 lines
Quoted 175 lines
---
  arch/x86/kernel/acpi/boot.c |   25 ++++++++++++-------------
  arch/x86/kernel/setup.c     |    8 +++-----
  drivers/acpi/tables.c       |   42 +++++++++++++++++++++++++++++++++++++++---
  include/linux/acpi.h        |    9 ++++++++-
  4 files changed, 62 insertions(+), 22 deletions(-)

Index: linux-pm/arch/x86/kernel/acpi/boot.c
===================================================================

arch/x86/kernel/acpi/boot.c +12 -13
@@ -1554,10 +1554,18 @@ void __init acpi_boot_table_init(void)
       /*
        * Initialize the ACPI boot-time table parser.
        */
-     if (acpi_table_init()) {
+     if (acpi_locate_initial_tables())
               disable_acpi();
-             return;
-     }
+     else
+             acpi_reserve_initial_tables();
+}
+
+int __init early_acpi_boot_init(void)
+{
+     if (acpi_disabled)
+             return 1;
+
+     acpi_table_init_complete();
 
       acpi_table_parse(ACPI_SIG_BOOT, acpi_parse_sbf);
 
@@ -1570,18 +1578,9 @@ void __init acpi_boot_table_init(void)
               } else {
                       printk(KERN_WARNING PREFIX "Disabling ACPI support\n");
                       disable_acpi();
-                     return;
+                     return 1;
               }
       }
-}
-
-int __init early_acpi_boot_init(void)
-{
-     /*
-      * If acpi_disabled, bail out
-      */
-     if (acpi_disabled)
-             return 1;
 
       /*
        * Process the Multiple APIC Description Table (MADT), if present

Index: linux-pm/arch/x86/kernel/setup.c
===================================================================

arch/x86/kernel/setup.c +3 -5
@@ -1045,6 +1045,9 @@ void __init setup_arch(char **cmdline_p)
 
       cleanup_highmap();
 
+     /* Look for ACPI tables and reserve memory occupied by them. */
+     acpi_boot_table_init();
+
       memblock_set_current_limit(ISA_END_ADDRESS);
       e820__memblock_setup();
 
@@ -1136,11 +1139,6 @@ void __init setup_arch(char **cmdline_p)
 
       early_platform_quirks();
 
-     /*
-      * Parse the ACPI tables for possible boot-time SMP configuration.
-      */
-     acpi_boot_table_init();
-
       early_acpi_boot_init();
 
       initmem_init();

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +8 -1
@@ -222,10 +222,14 @@ void __iomem *__acpi_map_table(unsigned
  void __acpi_unmap_table(void __iomem *map, unsigned long size);
  int early_acpi_boot_init(void);
  int acpi_boot_init (void);
+void acpi_boot_table_prepare (void);
  void acpi_boot_table_init (void);
  int acpi_mps_check (void);
  int acpi_numa_init (void);
 
+int acpi_locate_initial_tables (void);
+void acpi_reserve_initial_tables (void);
+void acpi_table_init_complete (void);
  int acpi_table_init (void);
  int acpi_table_parse(char *id, acpi_tbl_table_handler handler);
  int __init acpi_table_parse_entries(char *id, unsigned long table_size,
@@ -814,9 +818,12 @@ static inline int acpi_boot_init(void)
       return 0;
  }
 
+static inline void acpi_boot_table_prepare(void)
+{
+}
+
  static inline void acpi_boot_table_init(void)
  {
-     return;
  }
 
  static inline int acpi_mps_check(void)

Index: linux-pm/drivers/acpi/tables.c
===================================================================

drivers/acpi/tables.c +32 -3
@@ -780,7 +780,7 @@ acpi_status acpi_os_table_override(struc
  }
 
  /*
- * acpi_table_init()
+ * acpi_locate_initial_tables()
   *
   * find RSDP, find and checksum SDT/XSDT.
   * checksum all tables, print SDT/XSDT
@@ -788,7 +788,7 @@ acpi_status acpi_os_table_override(struc
   * result: sdt_entry[] is initialized
   */
 
-int __init acpi_table_init(void)
+int __init acpi_locate_initial_tables(void)
  {
       acpi_status status;
 
@@ -803,9 +803,45 @@ int __init acpi_table_init(void)
       status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
       if (ACPI_FAILURE(status))
               return -EINVAL;
-     acpi_table_initrd_scan();
 
+     return 0;
+}
+
+void __init acpi_reserve_initial_tables(void)
+{
+     int i;
+
+     for (i = 0; i < ACPI_MAX_TABLES; i++) {
+             struct acpi_table_desc *table_desc = &initial_tables[i];
+             u64 start = table_desc->address;
+             u64 size = table_desc->length;
+
+             if (!start || !size)
+                     break;
+
+             pr_info("Reserving %4s table memory at [mem 0x%llx-0x%llx]\n",
+                     table_desc->signature.ascii, start, start + size - 1);
+
+             memblock_reserve(start, size);
+     }
+}
+
+void __init acpi_table_init_complete(void)
+{
+     acpi_table_initrd_scan();
       check_multiple_madt();
+}
+
+int __init acpi_table_init(void)
+{
+     int ret;

drivers/acpi/tables.c int __init acpi_table_init(void) · line 847

Quoted 10 lines
drivers/acpi/tables.c +7 -0
+
+     ret = acpi_locate_initial_tables();
+     if (ret)
+             return ret;
+
+     acpi_table_init_complete();
+
       return 0;
  }
 

--
Sincerely yours,
Mike.

#28 lore raw

Re: [PATCH] ACPI: tables: x86: Reserve memory occupied by ACPI tables

Quoted 51 lines · George Kennedy
Quoted 47 lines · Rafael J. Wysocki
Quoted 36 lines · Mike Rapoport
Quoted 35 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The following problem has been reported by George Kennedy:

Since commit 7fef431be9c9 ("mm/page_alloc: place pages to tail in __free_pages_core()") the following use after free occurs intermittently when ACPI tables are accessed.

  BUG: KASAN: use-after-free in ibft_init+0x134/0xc49
  Read of size 4 at addr ffff8880be453004 by task swapper/0/1
  CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1-7a7fd0d #1
  Call Trace:
   dump_stack+0xf6/0x158
   print_address_description.constprop.9+0x41/0x60
   kasan_report.cold.14+0x7b/0xd4
   __asan_report_load_n_noabort+0xf/0x20
   ibft_init+0x134/0xc49
   do_one_initcall+0xc4/0x3e0
   kernel_init_freeable+0x5af/0x66b
   kernel_init+0x16/0x1d0
   ret_from_fork+0x22/0x30

ACPI tables mapped via kmap() do not have their mapped pages reserved and the pages can be "stolen" by the buddy allocator.

Apparently, on the affected system, the ACPI table in question is not located in "reserved" memory, like ACPI NVS or ACPI Data, that will not be used by the buddy allocator, so the memory occupied by that table has to be explicitly reserved to prevent the buddy allocator from using it.

In order to address this problem, rearrange the initialization of the ACPI tables on x86 to locate the initial tables earlier and reserve the memory occupied by them.

Quoted 8 lines
Quoted 6 lines

The other architectures using ACPI should not be affected by this change.

FWIW:

Thank you!

Quoted 7 lines
Quoted 1 line

George, can you please try this patch on the affected system?

Rafael,

10 for 10 successful reboots with your patch.

First, verified the failure is still there with latest 5.12.0-rc4.

Thank you!

I'll add a Tested-by from you to it, then.

#29 lore raw
In reply to Rafael J. Wysocki

Hello Rafael,

Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · George Kennedy

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

Like David said in the other reply, if a BIOS does not mark the memory that contains an ACPI table as used (e.g. reserved or ACPI data), we need to make sure the kernel knows that such memory is in use and an early call to memblock_reserve() is exactly what we need here.
George had this issue with iBFT, but in general this could be any table that a buggy BIOS forgot to mark as ACPI data.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 1049

Quoted 9 lines
arch/x86/kernel/setup.c +1 -0
index d883176..97deea3 100644
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
         cleanup_highmap();
 
         memblock_set_current_limit(ISA_END_ADDRESS);
+       acpi_boot_table_init();

This cannot be moved before the acpi_table_upgrade() invocation AFAICS.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why exactly do you want to move it?

Want to make sure there are slots for memblock_reserve() to be able to reserve the page.

Well, that may not require reordering the initialization this way.

The memory that is used by the firmware should be reserved before memblock allocations are allowed so that ACPI tables won't get trampled by some random allocation.

On x86 this essentially means that the early reservations need to be complete before the call to e820__memblock_setup().

We probably need more precise refactoring of ACPI init than simply moving acpi_boot_table_init() earlier.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/x86/kernel/setup.c +0 -0
         e820__memblock_setup();
 
Signature · 2 lines
Sincerely yours,
Mike.
#30 lore raw
Quoted 13 lines · Mike Rapoport

Hello Rafael,

Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · George Kennedy

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

Quoted 6 lines

Like David said in the other reply, if a BIOS does not mark the memory that contains an ACPI table as used (e.g. reserved or ACPI data), we need to make sure the kernel knows that such memory is in use and an early call to memblock_reserve() is exactly what we need here.
George had this issue with iBFT, but in general this could be any table that a buggy BIOS forgot to mark as ACPI data.

BTW, I wonder is there a fundamental reason to use ioremap() to access ACPI tables at all?
In the end, they reside in RAM and, apparently, they live at the same DIMM as neighboring "normal memory" so why cannot we just map them normally as read-only not executable?

Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 1049

Quoted 9 lines
arch/x86/kernel/setup.c +1 -0
index d883176..97deea3 100644
@@ -1046,6 +1046,7 @@ void __init setup_arch(char **cmdline_p)
         cleanup_highmap();
 
         memblock_set_current_limit(ISA_END_ADDRESS);
+       acpi_boot_table_init();

This cannot be moved before the acpi_table_upgrade() invocation AFAICS.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why exactly do you want to move it?

Want to make sure there are slots for memblock_reserve() to be able to reserve the page.

Well, that may not require reordering the initialization this way.

The memory that is used by the firmware should be reserved before memblock allocations are allowed so that ACPI tables won't get trampled by some random allocation.

On x86 this essentially means that the early reservations need to be complete before the call to e820__memblock_setup().

We probably need more precise refactoring of ACPI init than simply moving acpi_boot_table_init() earlier.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/x86/kernel/setup.c +0 -0
         e820__memblock_setup();
 
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
#31 lore raw
Quoted 22 lines · Mike Rapoport
Quoted 13 lines · Mike Rapoport

Hello Rafael,

Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · George Kennedy

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

Quoted 6 lines

Like David said in the other reply, if a BIOS does not mark the memory that contains an ACPI table as used (e.g. reserved or ACPI data), we need to make sure the kernel knows that such memory is in use and an early call to memblock_reserve() is exactly what we need here.
George had this issue with iBFT, but in general this could be any table that a buggy BIOS forgot to mark as ACPI data.

Quoted 5 lines

BTW, I wonder is there a fundamental reason to use ioremap() to access ACPI tables at all?
In the end, they reside in RAM and, apparently, they live at the same DIMM as neighboring "normal memory" so why cannot we just map them normally as read-only not executable?

This may be NVS memory (depending on the configuration of the system) which isn't "normal" RAM AFAICS.

#32 lore raw
Quoted 23 lines · Rafael J. Wysocki
Quoted 22 lines · Mike Rapoport
Quoted 19 lines · Mike Rapoport

Hello Rafael,

Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · George Kennedy

The ibft table, for example, is mapped in via acpi_map() and kmap(). The page for the ibft table is not reserved, so it can end up on the freelist.

You appear to be saying that it is not sufficient to kmap() a page in order to use it safely. It is also necessary to reserve it upfront, for example with the help of memblock_reserve(). Is that correct? If so, is there an alternative way to reserve a page frame?

Like David said in the other reply, if a BIOS does not mark the memory that contains an ACPI table as used (e.g. reserved or ACPI data), we need to make sure the kernel knows that such memory is in use and an early call to memblock_reserve() is exactly what we need here.
George had this issue with iBFT, but in general this could be any table that a buggy BIOS forgot to mark as ACPI data.

Quoted 8 lines
Quoted 5 lines

BTW, I wonder is there a fundamental reason to use ioremap() to access ACPI tables at all?
In the end, they reside in RAM and, apparently, they live at the same DIMM as neighboring "normal memory" so why cannot we just map them normally as read-only not executable?

This may be NVS memory (depending on the configuration of the system) which isn't "normal" RAM AFAICS.

Hmm, according to the description of "ACPI NVS" in ACPI 6.3

ACPI NVS Memory. This range of addresses is in use or reserved by
the system and must not be used by the operating system. This range is required to be saved and restored across an NVS sleep.

it behaves more like "normal" RAM rather than actual non-volatile storage.

There are other places in ACPI text that imply that "ACPI NVS" is actually RAM, it's just reserved by the firmware.

And judging by the example below both "ACPI data" and "ACPI NVS" live in the very same DIMM as "usable" RAM.

[    0.000000] BIOS-e820: [mem 0x0000000029931000-0x0000000029932fff] usable
[    0.000000] BIOS-e820: [mem 0x0000000029933000-0x000000002993afff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000002993b000-0x000000002993bfff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x000000002993c000-0x0000000029940fff] ACPI data
[    0.000000] BIOS-e820: [mem 0x0000000029941000-0x0000000029944fff] usable

Unfortunately, both UEFI and ACPI standards are very vague about the meaning of "ACPI NVS" so there may be systems that use real non-volatile storage for it...

Signature · 2 lines
Sincerely yours,
Mike.