2018-10-03 01:33:59

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v6 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 v5:
- Fix build error if CONFIG_RANDOMIZE_MEMORY is not defined.

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
Documentation/kernel-parameters.txt: Document
rand_mem_physical_padding=

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

--
2.18.0



2018-10-03 01:34:04

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v6 1/3] 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]>
---
arch/x86/include/asm/setup.h | 9 +++++++++
arch/x86/mm/kaslr.c | 22 +++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)

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

+#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.
* It does not work on many machines.
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 61db77b..eb47f05 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;

+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
@@ -69,6 +70,25 @@ 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;
+
+ 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 +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) +
- CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+ get_rand_mem_physical_padding();

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


2018-10-03 01:34:09

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v6 2/3] 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]>
---
drivers/acpi/numa.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 8516760..420ed2c 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)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",
+ (max_possible_phys - max_actual_phys) >> 40);
+ }
}

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


2018-10-03 01:34:11

by Masayoshi Mizuma

[permalink] [raw]
Subject: [PATCH v6 3/3] 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]>
---
.../admin-guide/kernel-parameters.txt | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 92eb1f4..f0930e3 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3529,6 +3529,25 @@
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]
--
2.18.0


2018-10-03 06:53:13

by Ingo Molnar

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


* Masayoshi Mizuma <[email protected]> wrote:

> 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 v5:
> - Fix build error if CONFIG_RANDOMIZE_MEMORY is not defined.

Please send a delta patch on top of tip:x86/boot with the get_rand_mem_physical_padding() fix,
when doing that you'll see that I did some minor cleanups when applying the patches which your
v6 series undoes.

Thanks,

Ingo

2018-10-03 11:01:28

by Masayoshi Mizuma

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

On Wed, Oct 03, 2018 at 08:52:19AM +0200, Ingo Molnar wrote:
>
> * Masayoshi Mizuma <[email protected]> wrote:
>
> > 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 v5:
> > - Fix build error if CONFIG_RANDOMIZE_MEMORY is not defined.
>
> Please send a delta patch on top of tip:x86/boot with the get_rand_mem_physical_padding() fix,
> when doing that you'll see that I did some minor cleanups when applying the patches which your
> v6 series undoes.

I have sent the delta patch. Thanks!

- Masa