2018-10-01 14:09:35

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v5 0/3] Add a kernel parameter to change the padding size for KASLR

This patch series are adding an kernel parameter to change
the padding size used for KASLR. It is useful for memory hotplug
capable system. User can adjust the padding size to use it.

It is better if the padding size is calculated automatically,
however, ACPI SRAT is not available at the KASLR initialization
time. So, I add a message for user to tell the suitable padding
size. User can set it on next reboot.

This patch series don't change the current default padding size.

Change log from v4:
- Fix the padding size check (2nd patch)
- Add explanation for the parameter in the document. (3rd patch)

Change log from v3:
- Add a warning message if the padding size for KASLR is not enough.
And it says the suitable padding size to user.

Change log from v2:
- Simplify the description. As Baoquan said, this is similar SGI UV issue,
but a little different. Remove SGI UV description.

Masayoshi Mizuma (3):
x86/mm: Add a kernel parameter to change the padding used for the physical
memory mapping
ACPI / NUMA: Add warning message if the padding size for KASLR is not
enough
docs: kernel-parameters.txt: document rand_mem_physical_padding

.../admin-guide/kernel-parameters.txt | 18 ++++++++++++++++++
arch/x86/include/asm/setup.h | 2 ++
arch/x86/mm/kaslr.c | 17 ++++++++++++++++-
drivers/acpi/numa.c | 16 ++++++++++++++++
4 files changed, 52 insertions(+), 1 deletion(-)

--
2.18.0



2018-10-01 14:09:56

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v5 2/3] ACPI / NUMA: Add warning message if the padding size for KASLR is not enough

From: Masayoshi Mizuma <[email protected]>

Add warning message if the padding size for KASLR,
rand_mem_physical_padding, is not enough. The message also
says the suitable padding size.

Signed-off-by: Masayoshi Mizuma <[email protected]>
---
arch/x86/include/asm/setup.h | 2 ++
drivers/acpi/numa.c | 16 ++++++++++++++++
2 files changed, 18 insertions(+)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index ae13bc9..65a5bf8 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,6 +80,8 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

+extern int rand_mem_physical_padding;
+
/*
* Do NOT EVER look at the BIOS memory size location.
* It does not work on many machines.
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 8516760..5a11aa5 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,6 +32,7 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
+#include <asm/setup.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -435,6 +436,7 @@ acpi_table_parse_srat(enum acpi_srat_type id,
int __init acpi_numa_init(void)
{
int cnt = 0;
+ u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -463,6 +465,20 @@ int __init acpi_numa_init(void)

cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);
+
+ /* check the padding size for KASLR is enough. */
+ if (parsed_numa_memblks && kaslr_enabled()) {
+ max_actual_phys = roundup(PFN_PHYS(max_pfn),
+ 1ULL << 40);
+ max_possible_phys = roundup(PFN_PHYS(max_possible_pfn),
+ 1ULL << 40);
+ threshold = max_actual_phys +
+ ((u64)rand_mem_physical_padding << 40);
+
+ if (max_possible_phys > threshold)
+ pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
+ (max_possible_phys - max_actual_phys) >> 40);
+ }
}

/* SLIT: System Locality Information Table */
--
2.18.0


2018-10-01 14:09:57

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v5 3/3] docs: kernel-parameters.txt: document rand_mem_physical_padding

From: Masayoshi Mizuma <[email protected]>

This kernel parameter allows to change the padding used
for the physical memory mapping section when KASLR
memory is enabled.

For memory hotplug capable systems, the default padding size,
CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING, may not be enough.
The option is useful to adjust the padding size.

Signed-off-by: Masayoshi Mizuma <[email protected]>
---
.../admin-guide/kernel-parameters.txt | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 92eb1f4..4c4c5e6 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3529,6 +3529,24 @@
fully seed the kernel's CRNG. Default is controlled
by CONFIG_RANDOM_TRUST_CPU.

+ rand_mem_physical_padding=
+ [KNL] Define the padding size in terabytes
+ used for the physical memory mapping section
+ when KASLR is enabled.
+ If the padding size is not enough, you can see
+ 'Set rand_mem_physical_padding=XX ...' in system
+ boot message, so set the parameter as the message.
+
+ This parameter is useful for the memory hot-add capable
+ system. Such system may have more space than
+ actual memory size to hot-add memory. If the
+ padding size is not enough and a memory is hot-added,
+ the hot-adding will fail because it destroys the
+ system memory map. So, the padding size is needed to
+ adjust in such system.
+ The default value is
+ CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING.
+
ras=option[,option,...] [KNL] RAS-specific options

cec_disable [X86]
--
2.18.0


2018-10-01 14:11:13

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v5 1/3] x86/mm: Add a kernel parameter to change the padding used for the physical memory mapping

From: Masayoshi Mizuma <[email protected]>

If each node of physical memory layout has huge space for hotplug,
the padding used for the physical memory mapping section is not enough.
For exapmle of the layout:
SRAT: Node 6 PXM 4 [mem 0x100000000000-0x13ffffffffff] hotplug
SRAT: Node 7 PXM 5 [mem 0x140000000000-0x17ffffffffff] hotplug
SRAT: Node 2 PXM 6 [mem 0x180000000000-0x1bffffffffff] hotplug
SRAT: Node 3 PXM 7 [mem 0x1c0000000000-0x1fffffffffff] hotplug

We can increase the padding by CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING,
however, the needed padding size depends on the system environment.
The kernel option is better than changing the config.

Signed-off-by: Masayoshi Mizuma <[email protected]>
Reviewed-by: Baoquan He <[email protected]>
---
arch/x86/mm/kaslr.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 61db77b..00cf4ca 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -40,6 +40,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

+int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -69,6 +70,20 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+static int __init rand_mem_physical_padding_setup(char *str)
+{
+ int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
+
+ get_option(&str, &rand_mem_physical_padding);
+ if (rand_mem_physical_padding < 0)
+ rand_mem_physical_padding = 0;
+ else if (rand_mem_physical_padding > max_padding)
+ rand_mem_physical_padding = max_padding;
+
+ return 0;
+}
+early_param("rand_mem_physical_padding", rand_mem_physical_padding_setup);
+
/* Initialize base and padding for each memory region randomized with KASLR */
void __init kernel_randomize_memory(void)
{
@@ -102,7 +117,7 @@ void __init kernel_randomize_memory(void)
*/
BUG_ON(kaslr_regions[0].base != &page_offset_base);
memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
- CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+ rand_mem_physical_padding;

/* Adapt phyiscal memory region size based on available memory */
if (memory_tb < kaslr_regions[0].size_tb)
--
2.18.0


Subject: [tip:x86/boot] x86/mm: Add a kernel parameter to change the padding used for the physical memory mapping

Commit-ID: e79ac9354cf95d75ff6afd341f9c5aa5cbcd638b
Gitweb: https://git.kernel.org/tip/e79ac9354cf95d75ff6afd341f9c5aa5cbcd638b
Author: Masayoshi Mizuma <[email protected]>
AuthorDate: Mon, 1 Oct 2018 10:08:41 -0400
Committer: Ingo Molnar <[email protected]>
CommitDate: Tue, 2 Oct 2018 11:47:20 +0200

x86/mm: Add a kernel parameter to change the padding used for the physical memory mapping

If each node of physical memory layout has huge space for hotplug,
the padding used for the physical memory mapping section is not enough.
For exapmle of the layout:

SRAT: Node 6 PXM 4 [mem 0x100000000000-0x13ffffffffff] hotplug
SRAT: Node 7 PXM 5 [mem 0x140000000000-0x17ffffffffff] hotplug
SRAT: Node 2 PXM 6 [mem 0x180000000000-0x1bffffffffff] hotplug
SRAT: Node 3 PXM 7 [mem 0x1c0000000000-0x1fffffffffff] hotplug

We can increase the padding via CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING,
however, the needed padding size depends on the system environment.
The kernel option is better than changing the config.

Signed-off-by: Masayoshi Mizuma <[email protected]>
Reviewed-by: Baoquan He <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Masayoshi Mizuma <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
---
arch/x86/mm/kaslr.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 61db77b0eda9..00cf4cae38f5 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -40,6 +40,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

+int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -69,6 +70,20 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+static int __init rand_mem_physical_padding_setup(char *str)
+{
+ int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
+
+ get_option(&str, &rand_mem_physical_padding);
+ if (rand_mem_physical_padding < 0)
+ rand_mem_physical_padding = 0;
+ else if (rand_mem_physical_padding > max_padding)
+ rand_mem_physical_padding = max_padding;
+
+ return 0;
+}
+early_param("rand_mem_physical_padding", rand_mem_physical_padding_setup);
+
/* Initialize base and padding for each memory region randomized with KASLR */
void __init kernel_randomize_memory(void)
{
@@ -102,7 +117,7 @@ void __init kernel_randomize_memory(void)
*/
BUG_ON(kaslr_regions[0].base != &page_offset_base);
memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
- CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+ rand_mem_physical_padding;

/* Adapt phyiscal memory region size based on available memory */
if (memory_tb < kaslr_regions[0].size_tb)

Subject: [tip:x86/boot] Documentation/kernel-parameters.txt: Document rand_mem_physical_padding=

Commit-ID: 2d580c160ada91049fc24b244ed74d8775bd1c96
Gitweb: https://git.kernel.org/tip/2d580c160ada91049fc24b244ed74d8775bd1c96
Author: Masayoshi Mizuma <[email protected]>
AuthorDate: Mon, 1 Oct 2018 10:08:43 -0400
Committer: Ingo Molnar <[email protected]>
CommitDate: Tue, 2 Oct 2018 11:47:22 +0200

Documentation/kernel-parameters.txt: Document rand_mem_physical_padding=

This kernel parameter allows the modification of the padding used
for the physical memory mapping section when KASLR memory is enabled.

For memory hotplug capable systems, the default padding size,
CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING, may not be enough.

The option is useful to adjust the padding size.

Signed-off-by: Masayoshi Mizuma <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Masayoshi Mizuma <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
---
Documentation/admin-guide/kernel-parameters.txt | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 64a3bf54b974..45f614cd2a61 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3529,6 +3529,24 @@
fully seed the kernel's CRNG. Default is controlled
by CONFIG_RANDOM_TRUST_CPU.

+ rand_mem_physical_padding=
+ [KNL] Define the padding size in terabytes
+ used for the physical memory mapping section
+ when KASLR is enabled.
+ If the padding size is not enough, you can see
+ 'Set rand_mem_physical_padding=XX ...' in system
+ boot message, so set the parameter as the message suggests.
+
+ This parameter is useful for memory hot-add capable
+ systems. Such systems may have more space than
+ actual memory size to hot-add memory. If the
+ padding size is not enough and memory is hot-added,
+ the hot-adding will fail because it destroys the
+ system memory map. So, the padding size needs to be
+ adjusted in such a system.
+ The default value is the value of
+ CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING.
+
ras=option[,option,...] [KNL] RAS-specific options

cec_disable [X86]

Subject: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

Commit-ID: 3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
Gitweb: https://git.kernel.org/tip/3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
Author: Masayoshi Mizuma <[email protected]>
AuthorDate: Mon, 1 Oct 2018 10:08:42 -0400
Committer: Ingo Molnar <[email protected]>
CommitDate: Tue, 2 Oct 2018 11:47:21 +0200

ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

Add warning message if the padding size for KASLR,
rand_mem_physical_padding, is not enough. The message also
says the suitable padding size.

Signed-off-by: Masayoshi Mizuma <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Masayoshi Mizuma <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
---
arch/x86/include/asm/setup.h | 2 ++
drivers/acpi/numa.c | 14 ++++++++++++++
2 files changed, 16 insertions(+)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index ae13bc974416..65a5bf8f6aba 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,6 +80,8 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

+extern int rand_mem_physical_padding;
+
/*
* Do NOT EVER look at the BIOS memory size location.
* It does not work on many machines.
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 85167603b9c9..3d69834c692f 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,6 +32,7 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
+#include <asm/setup.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -435,6 +436,7 @@ acpi_table_parse_srat(enum acpi_srat_type id,
int __init acpi_numa_init(void)
{
int cnt = 0;
+ u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -463,6 +465,18 @@ int __init acpi_numa_init(void)

cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);
+
+ /* check the padding size for KASLR is enough. */
+ if (parsed_numa_memblks && kaslr_enabled()) {
+ max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
+ max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
+ threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+
+ if (max_possible_phys > threshold) {
+ pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
+ (max_possible_phys - max_actual_phys) >> 40);
+ }
+ }
}

/* SLIT: System Locality Information Table */

2018-10-02 15:05:45

by Borislav Petkov

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

On Tue, Oct 02, 2018 at 03:18:41AM -0700, tip-bot for Masayoshi Mizuma wrote:
> Commit-ID: 3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> Gitweb: https://git.kernel.org/tip/3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> Author: Masayoshi Mizuma <[email protected]>
> AuthorDate: Mon, 1 Oct 2018 10:08:42 -0400
> Committer: Ingo Molnar <[email protected]>
> CommitDate: Tue, 2 Oct 2018 11:47:21 +0200
>
> ACPI/NUMA: Add warning message if the padding size for KASLR is not enough
>
> Add warning message if the padding size for KASLR,
> rand_mem_physical_padding, is not enough. The message also
> says the suitable padding size.
>
> Signed-off-by: Masayoshi Mizuma <[email protected]>
> Cc: Baoquan He <[email protected]>
> Cc: Borislav Petkov <[email protected]>
> Cc: Linus Torvalds <[email protected]>
> Cc: Masayoshi Mizuma <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Ingo Molnar <[email protected]>
> ---
> arch/x86/include/asm/setup.h | 2 ++
> drivers/acpi/numa.c | 14 ++++++++++++++
> 2 files changed, 16 insertions(+)
>
> diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
> index ae13bc974416..65a5bf8f6aba 100644
> --- a/arch/x86/include/asm/setup.h
> +++ b/arch/x86/include/asm/setup.h
> @@ -80,6 +80,8 @@ static inline unsigned long kaslr_offset(void)
> return (unsigned long)&_text - __START_KERNEL;
> }
>
> +extern int rand_mem_physical_padding;
> +
> /*
> * Do NOT EVER look at the BIOS memory size location.
> * It does not work on many machines.
> diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
> index 85167603b9c9..3d69834c692f 100644
> --- a/drivers/acpi/numa.c
> +++ b/drivers/acpi/numa.c
> @@ -32,6 +32,7 @@
> #include <linux/numa.h>
> #include <linux/nodemask.h>
> #include <linux/topology.h>
> +#include <asm/setup.h>
>
> static nodemask_t nodes_found_map = NODE_MASK_NONE;
>
> @@ -435,6 +436,7 @@ acpi_table_parse_srat(enum acpi_srat_type id,
> int __init acpi_numa_init(void)
> {
> int cnt = 0;
> + u64 max_possible_phys, max_actual_phys, threshold;
>
> if (acpi_disabled)
> return -EINVAL;
> @@ -463,6 +465,18 @@ int __init acpi_numa_init(void)
>
> cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
> acpi_parse_memory_affinity, 0);
> +
> + /* check the padding size for KASLR is enough. */
> + if (parsed_numa_memblks && kaslr_enabled()) {
> + max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
> + max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
> + threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);

Nope, not really:

ld: drivers/acpi/numa.o: in function `acpi_numa_init':
/home/boris/kernel/linux/drivers/acpi/numa.c:473: undefined reference to `rand_mem_physical_padding'
make: *** [Makefile:1030: vmlinux] Error 1

due to CONFIG_RANDOMIZE_MEMORY=n in my .config.

You need to add a fix ontop which adds a function
get_rand_mem_physical_padding() which is defined in CONFIG_RANDOMIZE_MEMORY=y
and outside returns 0.

And then make that rand_mem_physical_padding static and do not export it
to anything outside of kaslr.c but use the accessor.

Thx.

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

2018-10-02 21:20:28

by Masayoshi Mizuma

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

On Tue, Oct 02, 2018 at 05:05:18PM +0200, Borislav Petkov wrote:
> On Tue, Oct 02, 2018 at 03:18:41AM -0700, tip-bot for Masayoshi Mizuma wrote:
> > Commit-ID: 3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> > Gitweb: https://git.kernel.org/tip/3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> > Author: Masayoshi Mizuma <[email protected]>
> > AuthorDate: Mon, 1 Oct 2018 10:08:42 -0400
> > Committer: Ingo Molnar <[email protected]>
> > CommitDate: Tue, 2 Oct 2018 11:47:21 +0200
> >
> > ACPI/NUMA: Add warning message if the padding size for KASLR is not enough
> >
> > Add warning message if the padding size for KASLR,
> > rand_mem_physical_padding, is not enough. The message also
> > says the suitable padding size.
> >
> > Signed-off-by: Masayoshi Mizuma <[email protected]>
> > Cc: Baoquan He <[email protected]>
> > Cc: Borislav Petkov <[email protected]>
> > Cc: Linus Torvalds <[email protected]>
> > Cc: Masayoshi Mizuma <[email protected]>
> > Cc: Peter Zijlstra <[email protected]>
> > Cc: Thomas Gleixner <[email protected]>
> > Link: http://lkml.kernel.org/r/[email protected]
> > Signed-off-by: Ingo Molnar <[email protected]>
> > ---
> > arch/x86/include/asm/setup.h | 2 ++
> > drivers/acpi/numa.c | 14 ++++++++++++++
> > 2 files changed, 16 insertions(+)
> >
> > diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
> > index ae13bc974416..65a5bf8f6aba 100644
> > --- a/arch/x86/include/asm/setup.h
> > +++ b/arch/x86/include/asm/setup.h
> > @@ -80,6 +80,8 @@ static inline unsigned long kaslr_offset(void)
> > return (unsigned long)&_text - __START_KERNEL;
> > }
> >
> > +extern int rand_mem_physical_padding;
> > +
> > /*
> > * Do NOT EVER look at the BIOS memory size location.
> > * It does not work on many machines.
> > diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
> > index 85167603b9c9..3d69834c692f 100644
> > --- a/drivers/acpi/numa.c
> > +++ b/drivers/acpi/numa.c
> > @@ -32,6 +32,7 @@
> > #include <linux/numa.h>
> > #include <linux/nodemask.h>
> > #include <linux/topology.h>
> > +#include <asm/setup.h>
> >
> > static nodemask_t nodes_found_map = NODE_MASK_NONE;
> >
> > @@ -435,6 +436,7 @@ acpi_table_parse_srat(enum acpi_srat_type id,
> > int __init acpi_numa_init(void)
> > {
> > int cnt = 0;
> > + u64 max_possible_phys, max_actual_phys, threshold;
> >
> > if (acpi_disabled)
> > return -EINVAL;
> > @@ -463,6 +465,18 @@ int __init acpi_numa_init(void)
> >
> > cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
> > acpi_parse_memory_affinity, 0);
> > +
> > + /* check the padding size for KASLR is enough. */
> > + if (parsed_numa_memblks && kaslr_enabled()) {
> > + max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
> > + max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
> > + threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
>
> Nope, not really:
>
> ld: drivers/acpi/numa.o: in function `acpi_numa_init':
> /home/boris/kernel/linux/drivers/acpi/numa.c:473: undefined reference to `rand_mem_physical_padding'
> make: *** [Makefile:1030: vmlinux] Error 1
>
> due to CONFIG_RANDOMIZE_MEMORY=n in my .config.
>
> You need to add a fix ontop which adds a function
> get_rand_mem_physical_padding() which is defined in CONFIG_RANDOMIZE_MEMORY=y
> and outside returns 0.
>
> And then make that rand_mem_physical_padding static and do not export it
> to anything outside of kaslr.c but use the accessor.

Thank you for the report and suggetions!
I'll fix it and resubmit soon.

Thanks,
Masa

2018-10-03 10:58:58

by Masayoshi Mizuma

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

From: Masayoshi Mizuma <[email protected]>
Subject: [PATCH] Fix for build error if CONFIG_RANDOMIZE_BASE is not defined.

Fix the following build error.

ld: drivers/acpi/numa.o: in function `acpi_numa_init':
drivers/acpi/numa.c:473: undefined reference to `rand_mem_physical_padding'
make: *** [Makefile:1030: vmlinux] Error 1

- Add get_rand_mem_physical_padding() which returns
rand_mem_physical_padding or 0 if
CONFIG_RANDOMIZE_MEMORY is not defined.
- Make rand_mem_physical_padding static

Signed-off-by: Masayoshi Mizuma <[email protected]>
---
arch/x86/include/asm/setup.h | 9 ++++++++-
arch/x86/mm/kaslr.c | 9 +++++++--
drivers/acpi/numa.c | 3 ++-
3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 65a5bf8..1765a15 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,7 +80,14 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

-extern int rand_mem_physical_padding;
+#ifdef CONFIG_RANDOMIZE_MEMORY
+extern inline int __init get_rand_mem_physical_padding(void);
+#else
+static inline int __init get_rand_mem_physical_padding(void)
+{
+ return 0;
+}
+#endif

/*
* Do NOT EVER look at the BIOS memory size location.
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 00cf4ca..eb47f05 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -40,7 +40,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

-int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+static int rand_mem_physical_padding __initdata = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -70,6 +70,11 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+inline int __init get_rand_mem_physical_padding(void)
+{
+ return rand_mem_physical_padding;
+}
+
static int __init rand_mem_physical_padding_setup(char *str)
{
int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
@@ -117,7 +122,7 @@ void __init kernel_randomize_memory(void)
*/
BUG_ON(kaslr_regions[0].base != &page_offset_base);
memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
- rand_mem_physical_padding;
+ get_rand_mem_physical_padding();

/* Adapt phyiscal memory region size based on available memory */
if (memory_tb < kaslr_regions[0].size_tb)
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 3d69834..303b024 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -470,7 +470,8 @@ int __init acpi_numa_init(void)
if (parsed_numa_memblks && kaslr_enabled()) {
max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
- threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+ threshold = max_actual_phys +
+ ((u64)get_rand_mem_physical_padding() << 40);

if (max_possible_phys > threshold) {
pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
--
2.18.0


On Tue, Oct 02, 2018 at 03:18:41AM -0700, tip-bot for Masayoshi Mizuma wrote:
> Commit-ID: 3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> Gitweb: https://git.kernel.org/tip/3b054ca88c4f4dd5f516a12d4b6d6bd0ae826f41
> Author: Masayoshi Mizuma <[email protected]>
> AuthorDate: Mon, 1 Oct 2018 10:08:42 -0400
> Committer: Ingo Molnar <[email protected]>
> CommitDate: Tue, 2 Oct 2018 11:47:21 +0200
>
> ACPI/NUMA: Add warning message if the padding size for KASLR is not enough
>
> Add warning message if the padding size for KASLR,
> rand_mem_physical_padding, is not enough. The message also
> says the suitable padding size.
>
> Signed-off-by: Masayoshi Mizuma <[email protected]>
> Cc: Baoquan He <[email protected]>
> Cc: Borislav Petkov <[email protected]>
> Cc: Linus Torvalds <[email protected]>
> Cc: Masayoshi Mizuma <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Ingo Molnar <[email protected]>
> ---
> arch/x86/include/asm/setup.h | 2 ++
> drivers/acpi/numa.c | 14 ++++++++++++++
> 2 files changed, 16 insertions(+)
>
> diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
> index ae13bc974416..65a5bf8f6aba 100644
> --- a/arch/x86/include/asm/setup.h
> +++ b/arch/x86/include/asm/setup.h
> @@ -80,6 +80,8 @@ static inline unsigned long kaslr_offset(void)
> return (unsigned long)&_text - __START_KERNEL;
> }
>
> +extern int rand_mem_physical_padding;
> +
> /*
> * Do NOT EVER look at the BIOS memory size location.
> * It does not work on many machines.
> diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
> index 85167603b9c9..3d69834c692f 100644
> --- a/drivers/acpi/numa.c
> +++ b/drivers/acpi/numa.c
> @@ -32,6 +32,7 @@
> #include <linux/numa.h>
> #include <linux/nodemask.h>
> #include <linux/topology.h>
> +#include <asm/setup.h>
>
> static nodemask_t nodes_found_map = NODE_MASK_NONE;
>
> @@ -435,6 +436,7 @@ acpi_table_parse_srat(enum acpi_srat_type id,
> int __init acpi_numa_init(void)
> {
> int cnt = 0;
> + u64 max_possible_phys, max_actual_phys, threshold;
>
> if (acpi_disabled)
> return -EINVAL;
> @@ -463,6 +465,18 @@ int __init acpi_numa_init(void)
>
> cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
> acpi_parse_memory_affinity, 0);
> +
> + /* check the padding size for KASLR is enough. */
> + if (parsed_numa_memblks && kaslr_enabled()) {
> + max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
> + max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
> + threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
> +
> + if (max_possible_phys > threshold) {
> + pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
> + (max_possible_phys - max_actual_phys) >> 40);
> + }
> + }
> }
>
> /* SLIT: System Locality Information Table */

2018-10-03 12:34:59

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough


Subject: ACPI/NUMA: Fix KASLR build error

There is no point in trying to compile KASLR specific code when there is
no KASLR.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
---
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 3d69834c692f..5767733976b3 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -436,7 +436,6 @@ acpi_table_parse_srat(enum acpi_srat_type id,
int __init acpi_numa_init(void)
{
int cnt = 0;
- u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -466,8 +465,11 @@ int __init acpi_numa_init(void)
cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);

+#ifdef CONFIG_RANDOMIZE_MEMORY
/* check the padding size for KASLR is enough. */
if (parsed_numa_memblks && kaslr_enabled()) {
+ u64 max_possible_phys, max_actual_phys, threshold;
+
max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
@@ -477,6 +479,7 @@ int __init acpi_numa_init(void)
(max_possible_phys - max_actual_phys) >> 40);
}
}
+#endif
}

/* SLIT: System Locality Information Table */

2018-10-03 12:49:57

by Borislav Petkov

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

On Wed, Oct 03, 2018 at 02:34:02PM +0200, Peter Zijlstra wrote:
>
> Subject: ACPI/NUMA: Fix KASLR build error
>
> There is no point in trying to compile KASLR specific code when there is
> no KASLR.
>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> ---

Yeah, Peter and I were just talking on IRC and he gave me a much better
idea how to fix this, see below. I'll run this through the *config builder and
commit it if no complaints.

---
From: "Peter Zijlstra (Intel)" <[email protected]>
Date: Wed, 3 Oct 2018 14:41:27 +0200
Subject: [PATCH] ACPI/NUMA: Fix KASLR build error

There is no point in trying to compile KASLR specific code when there is
no KASLR.

[ bp: Move the whole crap into kaslr.c and make
rand_mem_physical_padding static. ]

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
---
arch/x86/include/asm/kaslr.h | 2 ++
arch/x86/include/asm/setup.h | 2 --
arch/x86/mm/kaslr.c | 18 +++++++++++++++++-
drivers/acpi/numa.c | 15 +++------------
4 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/arch/x86/include/asm/kaslr.h b/arch/x86/include/asm/kaslr.h
index db7ba2feb947..95ef3fc01d12 100644
--- a/arch/x86/include/asm/kaslr.h
+++ b/arch/x86/include/asm/kaslr.h
@@ -6,8 +6,10 @@ unsigned long kaslr_get_random_long(const char *purpose);

#ifdef CONFIG_RANDOMIZE_MEMORY
void kernel_randomize_memory(void);
+void kaslr_check_padding(void);
#else
static inline void kernel_randomize_memory(void) { }
+static inline void kaslr_check_padding(void) { }
#endif /* CONFIG_RANDOMIZE_MEMORY */

#endif
diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 65a5bf8f6aba..ae13bc974416 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,8 +80,6 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

-extern int rand_mem_physical_padding;
-
/*
* Do NOT EVER look at the BIOS memory size location.
* It does not work on many machines.
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 00cf4cae38f5..d58b7da0d55c 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -40,7 +40,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

-int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+static int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -70,6 +70,22 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+/*
+ * Check the padding size for KASLR is enough.
+ */
+void kaslr_check_padding(void)
+{
+ u64 max_possible_phys, max_actual_phys, threshold;
+
+ max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
+ max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
+ threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+
+ if (max_possible_phys > threshold)
+ pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
+ (max_possible_phys - max_actual_phys) >> 40);
+}
+
static int __init rand_mem_physical_padding_setup(char *str)
{
int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 3d69834c692f..4408e37600ef 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,7 +32,7 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
-#include <asm/setup.h>
+#include <asm/kaslr.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -436,7 +436,6 @@ acpi_table_parse_srat(enum acpi_srat_type id,
int __init acpi_numa_init(void)
{
int cnt = 0;
- u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -466,17 +465,9 @@ int __init acpi_numa_init(void)
cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);

- /* check the padding size for KASLR is enough. */
- if (parsed_numa_memblks && kaslr_enabled()) {
- max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
- max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
- threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+ if (parsed_numa_memblks)
+ kaslr_check_padding();

- if (max_possible_phys > threshold) {
- pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
- (max_possible_phys - max_actual_phys) >> 40);
- }
- }
}

/* SLIT: System Locality Information Table */
--
2.19.0.271.gfe8321ec057f

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

2018-10-03 13:03:25

by Masayoshi Mizuma

[permalink] [raw]
Subject: Re: [tip:x86/boot] ACPI/NUMA: Add warning message if the padding size for KASLR is not enough

On Wed, Oct 03, 2018 at 02:48:14PM +0200, Borislav Petkov wrote:
> On Wed, Oct 03, 2018 at 02:34:02PM +0200, Peter Zijlstra wrote:
> >
> > Subject: ACPI/NUMA: Fix KASLR build error
> >
> > There is no point in trying to compile KASLR specific code when there is
> > no KASLR.
> >
> > Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> > ---
>
> Yeah, Peter and I were just talking on IRC and he gave me a much better
> idea how to fix this, see below. I'll run this through the *config builder and
> commit it if no complaints.

Hi Boris and Peter,

Thank you for the fix. It is great!

- Masa

>
> ---
> From: "Peter Zijlstra (Intel)" <[email protected]>
> Date: Wed, 3 Oct 2018 14:41:27 +0200
> Subject: [PATCH] ACPI/NUMA: Fix KASLR build error
>
> There is no point in trying to compile KASLR specific code when there is
> no KASLR.
>
> [ bp: Move the whole crap into kaslr.c and make
> rand_mem_physical_padding static. ]
>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> Signed-off-by: Borislav Petkov <[email protected]>
> Cc: <[email protected]>
> Cc: <[email protected]>
> Cc: <[email protected]>
> Cc: <[email protected]>
> Cc: <[email protected]>
> Cc: <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> ---
> arch/x86/include/asm/kaslr.h | 2 ++
> arch/x86/include/asm/setup.h | 2 --
> arch/x86/mm/kaslr.c | 18 +++++++++++++++++-
> drivers/acpi/numa.c | 15 +++------------
> 4 files changed, 22 insertions(+), 15 deletions(-)
>
> diff --git a/arch/x86/include/asm/kaslr.h b/arch/x86/include/asm/kaslr.h
> index db7ba2feb947..95ef3fc01d12 100644
> --- a/arch/x86/include/asm/kaslr.h
> +++ b/arch/x86/include/asm/kaslr.h
> @@ -6,8 +6,10 @@ unsigned long kaslr_get_random_long(const char *purpose);
>
> #ifdef CONFIG_RANDOMIZE_MEMORY
> void kernel_randomize_memory(void);
> +void kaslr_check_padding(void);
> #else
> static inline void kernel_randomize_memory(void) { }
> +static inline void kaslr_check_padding(void) { }
> #endif /* CONFIG_RANDOMIZE_MEMORY */
>
> #endif
> diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
> index 65a5bf8f6aba..ae13bc974416 100644
> --- a/arch/x86/include/asm/setup.h
> +++ b/arch/x86/include/asm/setup.h
> @@ -80,8 +80,6 @@ static inline unsigned long kaslr_offset(void)
> return (unsigned long)&_text - __START_KERNEL;
> }
>
> -extern int rand_mem_physical_padding;
> -
> /*
> * Do NOT EVER look at the BIOS memory size location.
> * It does not work on many machines.
> diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
> index 00cf4cae38f5..d58b7da0d55c 100644
> --- a/arch/x86/mm/kaslr.c
> +++ b/arch/x86/mm/kaslr.c
> @@ -40,7 +40,7 @@
> */
> static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;
>
> -int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
> +static int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
> /*
> * Memory regions randomized by KASLR (except modules that use a separate logic
> * earlier during boot). The list is ordered based on virtual addresses. This
> @@ -70,6 +70,22 @@ static inline bool kaslr_memory_enabled(void)
> return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
> }
>
> +/*
> + * Check the padding size for KASLR is enough.
> + */
> +void kaslr_check_padding(void)
> +{
> + u64 max_possible_phys, max_actual_phys, threshold;
> +
> + max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
> + max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
> + threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
> +
> + if (max_possible_phys > threshold)
> + pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
> + (max_possible_phys - max_actual_phys) >> 40);
> +}
> +
> static int __init rand_mem_physical_padding_setup(char *str)
> {
> int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
> diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
> index 3d69834c692f..4408e37600ef 100644
> --- a/drivers/acpi/numa.c
> +++ b/drivers/acpi/numa.c
> @@ -32,7 +32,7 @@
> #include <linux/numa.h>
> #include <linux/nodemask.h>
> #include <linux/topology.h>
> -#include <asm/setup.h>
> +#include <asm/kaslr.h>
>
> static nodemask_t nodes_found_map = NODE_MASK_NONE;
>
> @@ -436,7 +436,6 @@ acpi_table_parse_srat(enum acpi_srat_type id,
> int __init acpi_numa_init(void)
> {
> int cnt = 0;
> - u64 max_possible_phys, max_actual_phys, threshold;
>
> if (acpi_disabled)
> return -EINVAL;
> @@ -466,17 +465,9 @@ int __init acpi_numa_init(void)
> cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
> acpi_parse_memory_affinity, 0);
>
> - /* check the padding size for KASLR is enough. */
> - if (parsed_numa_memblks && kaslr_enabled()) {
> - max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
> - max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
> - threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
> + if (parsed_numa_memblks)
> + kaslr_check_padding();
>
> - if (max_possible_phys > threshold) {
> - pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
> - (max_possible_phys - max_actual_phys) >> 40);
> - }
> - }
> }
>
> /* SLIT: System Locality Information Table */
> --
> 2.19.0.271.gfe8321ec057f
>
> --
> Regards/Gruss,
> Boris.
>
> Good mailing practices for 400: avoid top-posting and trim the reply.

Subject: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

Commit-ID: 3a387c6d96e69f1710a3804eb68e1253263298f2
Gitweb: https://git.kernel.org/tip/3a387c6d96e69f1710a3804eb68e1253263298f2
Author: Peter Zijlstra (Intel) <[email protected]>
AuthorDate: Wed, 3 Oct 2018 14:41:27 +0200
Committer: Borislav Petkov <[email protected]>
CommitDate: Wed, 3 Oct 2018 16:15:49 +0200

x86/kaslr, ACPI/NUMA: Fix KASLR build error

There is no point in trying to compile KASLR-specific code when there is
no KASLR.

[ bp: Move the whole crap into kaslr.c and make
rand_mem_physical_padding static. ]

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
---
arch/x86/include/asm/kaslr.h | 2 ++
arch/x86/include/asm/setup.h | 2 --
arch/x86/mm/kaslr.c | 19 ++++++++++++++++++-
drivers/acpi/numa.c | 15 +++------------
4 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/arch/x86/include/asm/kaslr.h b/arch/x86/include/asm/kaslr.h
index db7ba2feb947..95ef3fc01d12 100644
--- a/arch/x86/include/asm/kaslr.h
+++ b/arch/x86/include/asm/kaslr.h
@@ -6,8 +6,10 @@ unsigned long kaslr_get_random_long(const char *purpose);

#ifdef CONFIG_RANDOMIZE_MEMORY
void kernel_randomize_memory(void);
+void kaslr_check_padding(void);
#else
static inline void kernel_randomize_memory(void) { }
+static inline void kaslr_check_padding(void) { }
#endif /* CONFIG_RANDOMIZE_MEMORY */

#endif
diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 65a5bf8f6aba..ae13bc974416 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,8 +80,6 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

-extern int rand_mem_physical_padding;
-
/*
* Do NOT EVER look at the BIOS memory size location.
* It does not work on many machines.
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 00cf4cae38f5..b3471388288d 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -23,6 +23,7 @@
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/random.h>
+#include <linux/bootmem.h>

#include <asm/pgalloc.h>
#include <asm/pgtable.h>
@@ -40,7 +41,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

-int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+static int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -70,6 +71,22 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+/*
+ * Check the padding size for KASLR is enough.
+ */
+void __init kaslr_check_padding(void)
+{
+ u64 max_possible_phys, max_actual_phys, threshold;
+
+ max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
+ max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
+ threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+
+ if (max_possible_phys > threshold)
+ pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
+ (max_possible_phys - max_actual_phys) >> 40);
+}
+
static int __init rand_mem_physical_padding_setup(char *str)
{
int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 3d69834c692f..4408e37600ef 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,7 +32,7 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
-#include <asm/setup.h>
+#include <asm/kaslr.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -436,7 +436,6 @@ acpi_table_parse_srat(enum acpi_srat_type id,
int __init acpi_numa_init(void)
{
int cnt = 0;
- u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -466,17 +465,9 @@ int __init acpi_numa_init(void)
cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);

- /* check the padding size for KASLR is enough. */
- if (parsed_numa_memblks && kaslr_enabled()) {
- max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
- max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
- threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+ if (parsed_numa_memblks)
+ kaslr_check_padding();

- if (max_possible_phys > threshold) {
- pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
- (max_possible_phys - max_actual_phys) >> 40);
- }
- }
}

/* SLIT: System Locality Information Table */

2018-10-08 11:05:17

by Naresh Kamboju

[permalink] [raw]
Subject: Re: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

Linux next build for arm64 failed due to
numa.c:34:10: fatal error: asm/kaslr.h: No such file or directory

On Wed, 3 Oct 2018 at 19:52, tip-bot for Peter Zijlstra (Intel)
<[email protected]> wrote:
>
> Commit-ID: 3a387c6d96e69f1710a3804eb68e1253263298f2
> Gitweb: https://git.kernel.org/tip/3a387c6d96e69f1710a3804eb68e1253263298f2
> Author: Peter Zijlstra (Intel) <[email protected]>
> AuthorDate: Wed, 3 Oct 2018 14:41:27 +0200
> Committer: Borislav Petkov <[email protected]>
> CommitDate: Wed, 3 Oct 2018 16:15:49 +0200
>
> x86/kaslr, ACPI/NUMA: Fix KASLR build error
>
> There is no point in trying to compile KASLR-specific code when there is
> no KASLR.
>
> [ bp: Move the whole crap into kaslr.c and make
> rand_mem_physical_padding static. ]
>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>

<trim>

> diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
> index 3d69834c692f..4408e37600ef 100644
> --- a/drivers/acpi/numa.c
> +++ b/drivers/acpi/numa.c
> @@ -32,7 +32,7 @@
> #include <linux/numa.h>
> #include <linux/nodemask.h>
> #include <linux/topology.h>
> -#include <asm/setup.h>
> +#include <asm/kaslr.h>

kernel-source/drivers/acpi/numa.c:34:10: fatal error: asm/kaslr.h: No
such file or directory
#include <asm/kaslr.h>
^~~~~~~~~~~~~
compilation terminated.
kernel-source/scripts/Makefile.build:295: recipe for target
'drivers/acpi/numa.o' failed
make[4]: *** [drivers/acpi/numa.o] Error 1
make[4]: *** Waiting for unfinished jobs....
CC lib/win_minmax.o
CC drivers/base/soc.o
kernel-source/scripts/Makefile.build:520: recipe for target
'drivers/acpi' failed
make[3]: *** [drivers/acpi] Error 2

2018-10-08 13:01:40

by Borislav Petkov

[permalink] [raw]
Subject: Re: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

On Mon, Oct 08, 2018 at 04:33:29PM +0530, Naresh Kamboju wrote:
> Linux next build for arm64 failed due to
> numa.c:34:10: fatal error: asm/kaslr.h: No such file or directory

Thanks for letting me know, I guess we could fix that with a weak
function:

---
arch/x86/include/asm/kaslr.h | 2 --
drivers/acpi/numa.c | 4 +++-
2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/kaslr.h b/arch/x86/include/asm/kaslr.h
index 95ef3fc01d12..db7ba2feb947 100644
--- a/arch/x86/include/asm/kaslr.h
+++ b/arch/x86/include/asm/kaslr.h
@@ -6,10 +6,8 @@ unsigned long kaslr_get_random_long(const char *purpose);

#ifdef CONFIG_RANDOMIZE_MEMORY
void kernel_randomize_memory(void);
-void kaslr_check_padding(void);
#else
static inline void kernel_randomize_memory(void) { }
-static inline void kaslr_check_padding(void) { }
#endif /* CONFIG_RANDOMIZE_MEMORY */

#endif
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 4408e37600ef..ba62004f4d86 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,7 +32,6 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
-#include <asm/kaslr.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -433,6 +432,9 @@ acpi_table_parse_srat(enum acpi_srat_type id,
handler, max_entries);
}

+/* To be overridden by architectures */
+void __init __weak kaslr_check_padding(void) { }
+
int __init acpi_numa_init(void)
{
int cnt = 0;
--
2.19.0.271.gfe8321ec057f

--
Regards/Gruss,
Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)

2018-10-08 13:49:42

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

On Mon, Oct 8, 2018 at 3:01 PM Borislav Petkov <[email protected]> wrote:
>
> On Mon, Oct 08, 2018 at 04:33:29PM +0530, Naresh Kamboju wrote:
> > Linux next build for arm64 failed due to
> > numa.c:34:10: fatal error: asm/kaslr.h: No such file or directory
>
> Thanks for letting me know, I guess we could fix that with a weak
> function:
>

> +/* To be overridden by architectures */
> +void __init __weak kaslr_check_padding(void) { }
> +
> int __init acpi_numa_init(void)
> {
> int cnt = 0;
>

I think __weak functions are too fragile, when you do this and it
turns out that another architecture does need to do something,
you won't ever get any indication of it.

If we know that arm64 doesn't need to do anything here, just
add an arch/arm64/include/asm/kaslr.h with an empty function
there.

Arnd

2018-10-08 14:13:18

by Borislav Petkov

[permalink] [raw]
Subject: Re: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

On Mon, Oct 08, 2018 at 03:48:28PM +0200, Arnd Bergmann wrote:
> I think __weak functions are too fragile, when you do this and it
> turns out that another architecture does need to do something,
> you won't ever get any indication of it.
>
> If we know that arm64 doesn't need to do anything here, just
> add an arch/arm64/include/asm/kaslr.h with an empty function
> there.

I'm looking at fs/proc/vmcore.c and all those other weak functions there
and just doing the same.

Also, judging by

$ git grep CONFIG_PROC_VMCORE

output, ia64, mips and powerpc would need that include too.

--
Regards/Gruss,
Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)

Subject: [tip:x86/boot] x86/kaslr, ACPI/NUMA: Fix KASLR build error

Commit-ID: 9d94e8b1d4f94a3c4cee5ad11a1be460cd070839
Gitweb: https://git.kernel.org/tip/9d94e8b1d4f94a3c4cee5ad11a1be460cd070839
Author: Peter Zijlstra (Intel) <[email protected]>
AuthorDate: Wed, 3 Oct 2018 14:41:27 +0200
Committer: Borislav Petkov <[email protected]>
CommitDate: Tue, 9 Oct 2018 12:30:25 +0200

x86/kaslr, ACPI/NUMA: Fix KASLR build error

There is no point in trying to compile KASLR-specific code when there is
no KASLR.

[ bp: Move the whole crap into kaslr.c and make
rand_mem_physical_padding static. Make kaslr_check_padding()
weak to avoid build breakage on other architectures. ]

Reported-by: Naresh Kamboju <[email protected]>
Reported-by: Mark Brown <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
---
arch/x86/include/asm/setup.h | 2 --
arch/x86/mm/kaslr.c | 19 ++++++++++++++++++-
drivers/acpi/numa.c | 17 +++++------------
3 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 65a5bf8f6aba..ae13bc974416 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -80,8 +80,6 @@ static inline unsigned long kaslr_offset(void)
return (unsigned long)&_text - __START_KERNEL;
}

-extern int rand_mem_physical_padding;
-
/*
* Do NOT EVER look at the BIOS memory size location.
* It does not work on many machines.
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 00cf4cae38f5..b3471388288d 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -23,6 +23,7 @@
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/random.h>
+#include <linux/bootmem.h>

#include <asm/pgalloc.h>
#include <asm/pgtable.h>
@@ -40,7 +41,7 @@
*/
static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;

-int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+static int __initdata rand_mem_physical_padding = CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
/*
* Memory regions randomized by KASLR (except modules that use a separate logic
* earlier during boot). The list is ordered based on virtual addresses. This
@@ -70,6 +71,22 @@ static inline bool kaslr_memory_enabled(void)
return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
}

+/*
+ * Check the padding size for KASLR is enough.
+ */
+void __init kaslr_check_padding(void)
+{
+ u64 max_possible_phys, max_actual_phys, threshold;
+
+ max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
+ max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
+ threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+
+ if (max_possible_phys > threshold)
+ pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
+ (max_possible_phys - max_actual_phys) >> 40);
+}
+
static int __init rand_mem_physical_padding_setup(char *str)
{
int max_padding = (1 << (MAX_PHYSMEM_BITS - TB_SHIFT)) - 1;
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 3d69834c692f..ba62004f4d86 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -32,7 +32,6 @@
#include <linux/numa.h>
#include <linux/nodemask.h>
#include <linux/topology.h>
-#include <asm/setup.h>

static nodemask_t nodes_found_map = NODE_MASK_NONE;

@@ -433,10 +432,12 @@ acpi_table_parse_srat(enum acpi_srat_type id,
handler, max_entries);
}

+/* To be overridden by architectures */
+void __init __weak kaslr_check_padding(void) { }
+
int __init acpi_numa_init(void)
{
int cnt = 0;
- u64 max_possible_phys, max_actual_phys, threshold;

if (acpi_disabled)
return -EINVAL;
@@ -466,17 +467,9 @@ int __init acpi_numa_init(void)
cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
acpi_parse_memory_affinity, 0);

- /* check the padding size for KASLR is enough. */
- if (parsed_numa_memblks && kaslr_enabled()) {
- max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << 40);
- max_possible_phys = roundup(PFN_PHYS(max_possible_pfn), 1ULL << 40);
- threshold = max_actual_phys + ((u64)rand_mem_physical_padding << 40);
+ if (parsed_numa_memblks)
+ kaslr_check_padding();

- if (max_possible_phys > threshold) {
- pr_warn("Set 'rand_mem_physical_padding=%llu' to avoid memory hotadd failure.\n",
- (max_possible_phys - max_actual_phys) >> 40);
- }
- }
}

/* SLIT: System Locality Information Table */