x86/e820: apply 'mem=' boot command while reserving memory using boot_params
PATCH 1/1 x86/e820: apply 'mem=' boot command while reserving memory using boot_params
I might miss something. Please lemme know if I go wrong. Thanks.
Byungchul
--->8---
From 51f3b5b9bf9685aa431c00908771151edd702483 Mon Sep 17 00:00:00 2001
From: Byungchul Park <[email protected]>
Date: Tue, 23 Apr 2024 18:54:48 +0900
Subject: [PATCH] x86/e820: apply 'mem=' boot command while reserving memory
using boot_params
When a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using boot_params.
Applied the restriction when reserving memory using boot_params. While at it, renamed mem_size to a more specific name, boot_mem_limit.
--- arch/x86/kernel/e820.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
arch/x86/kernel/e820.c +8 -6
@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit =U64_MAX ; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;
@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;
@@ -1036,6 +1036,8 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); } +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);pr_info ("extended physical RAM map:\n" );
Signature · 1 line
2.17.1
Quoted 43 lines · Byungchul Park
I might miss something. Please lemme know if I go wrong. Thanks.
Byungchul
--->8---
Quoted 1 line
From
51f3b5b9bf9685aa431c00908771151edd702483Mon Sep 17 00:00:00 2001From: Byungchul Park <[email protected]>
Date: Tue, 23 Apr 2024 18:54:48 +0900
Subject: [PATCH] x86/e820: apply 'mem=' boot command while reserving memory
usingboot_paramsWhen a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using
boot_params.Applied the restriction when reserving memory using
boot_params. While at it, renamedmem_sizeto a more specific name,boot_mem_limit.Signed-off-by Byungchul Park <[email protected]>--- arch/x86/kernel/e820.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)arch/x86/kernel/e820.c +2
-2@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit =U64_MAX ; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;
arch/x86/kernel/e820.c static int __init parse_memopt(char *p) · line 905
Quoted 9 lines
arch/x86/kernel/e820.c +2
-2@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )
I should've handled the case that the return value is 0. I will fix it. Before going ahead, it'd be appreciated to tell if this approach is correct. Thank you.
Byungchul
Quoted 22 lines
arch/x86/kernel/e820.c +4
-2return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;@@ -1036,6 +1036,8 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); } +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);pr_info ("extended physical RAM map:\n" );Signature · 1 line
2.17.1
PATCH v2 1/1 x86/e820: apply 'mem=' boot command while reserving memory using boot_params
I might miss something. Please lemme know if I go wrong. Thanks.
Byungchul
Changes from v1
1. before - handle boot_mem_limit assuming the default is U64_MAX. after - handle boot_mem_limit assuming the default is 0.
--->8---
From e8bf247d6024b35af5300914dcff9135df9c1d66 Mon Sep 17 00:00:00 2001
From: Byungchul Park <[email protected]>
Date: Wed, 24 Apr 2024 09:55:25 +0900
Subject: [PATCH v2] x86/e820: apply 'mem=' boot command while reserving memory using boot_params
When a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using boot_params.
Applied the restriction when reserving memory using boot_params. While at it, renamed mem_size to a more specific name, boot_mem_limit.
--- arch/x86/kernel/e820.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kernel/e820.c +9 -6
@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;
@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;
@@ -1036,6 +1036,9 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); } +if (boot_mem_limit) +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);pr_info ("extended physical RAM map:\n" );
Signature · 1 line
2.17.1
Quoted 1 line · Byungchul Park
I might miss something. Please lemme know if I go wrong. Thanks.
I started to work on it since I wanted to limit memory boundary using 'mem=' boot command but it doesn't work. However, while looking around the code in more detail, I found the issue is about which one should have higher priority between:
1. boot command limiting memory boundary e.g. 'mem=',
2. setup data of memory map from bootloader, boot_params.
Based on the current code, setup data from bootloader has higher priority than boot command so the setup data can overwrite the user defined limit specified in boot command. Is it inteded?
If yes, I should stop posting.
If not, I will keep posting with the following - v3.
Byungchul
---
arch/x86/kernel/e820.c +14 -2
@@ -879,6 +879,7 @@ static void __init early_panic(char *msg) }static int userdef__initdata ; +static u64 userdef_mem_limit;/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p)
@@ -905,7 +906,10 @@ static int __init parse_memopt(char *p)if (mem_size ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size;#ifdef CONFIG_MEMORY_HOTPLUG max_mem_size = mem_size;
@@ -966,7 +970,10 @@ static int __init parse_memmap_one(char *p)else e820__range_remove (start_at, mem_size,0 ,0 ); }else { -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size; }return *p =='\0' ?0 : -EINVAL ;
@@ -1050,6 +1057,11 @@ void __init e820__reserve_setup_data(void)void __init e820__finish_early_params (void ) {if (userdef) { +if (userdef_mem_limit) +e820__range_remove (userdef_mem_limit, +ULLONG_MAX - userdef_mem_limit, +E820_TYPE_RAM ,1 ); +if (e820__update_table (e820_table) <0 )early_panic ("Invalid user supplied memory map" );
---
Quoted 76 lines
Byungchul
Changes from v1
1. before - handleboot_mem_limitassuming the default isU64_MAX. after - handleboot_mem_limitassuming the default is 0.--->8---
Quoted 1 line
From
e8bf247d6024b35af5300914dcff9135df9c1d66Mon Sep 17 00:00:00 2001From: Byungchul Park <[email protected]>
Date: Wed, 24 Apr 2024 09:55:25 +0900
Subject: [PATCH v2] x86/e820: apply 'mem=' boot command while reserving memory usingboot_paramsWhen a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using
boot_params.Applied the restriction when reserving memory using
boot_params. While at it, renamedmem_sizeto a more specific name,boot_mem_limit.Signed-off-by Byungchul Park <[email protected]>--- arch/x86/kernel/e820.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)arch/x86/kernel/e820.c +9
-6@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;@@ -1036,6 +1036,9 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); } +if (boot_mem_limit) +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);pr_info ("extended physical RAM map:\n" );Signature · 1 line
2.17.1
H. Peter Anvin
reviewerQuoted 139 lines · Byungchul Park
Quoted 1 line · Byungchul Park
I might miss something. Please lemme know if I go wrong. Thanks.
I started to work on it since I wanted to limit memory boundary using 'mem=' boot command but it doesn't work. However, while looking around the code in more detail, I found the issue is about which one should have higher priority between:
1. boot command limiting memory boundary e.g. 'mem=',
2. setup data of memory map from bootloader,boot_params.Based on the current code, setup data from bootloader has higher priority than boot command so the setup data can overwrite the user defined limit specified in boot command. Is it inteded?
If yes, I should stop posting.
If not, I will keep posting with the following - v3.Byungchul
---
arch/x86/kernel/e820.c +14
-3@@ -879,6 +879,7 @@ static void __init early_panic(char *msg) }static int userdef__initdata ; +static u64 userdef_mem_limit;/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p)@@ -905,7 +906,10 @@ static int __init parse_memopt(char *p)if (mem_size ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size;#ifdef CONFIG_MEMORY_HOTPLUG max_mem_size = mem_size;@@ -966,7 +970,10 @@ static int __init parse_memmap_one(char *p)else e820__range_remove (start_at, mem_size,0 ,0 ); }else { -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size; }return *p =='\0' ?0 : -EINVAL ;@@ -1050,6 +1057,11 @@ void __init e820__reserve_setup_data(void)void __init e820__finish_early_params (void ) {if (userdef) { +if (userdef_mem_limit) +e820__range_remove (userdef_mem_limit, +ULLONG_MAX - userdef_mem_limit, +E820_TYPE_RAM ,1 ); +if (e820__update_table (e820_table) <0 )early_panic ("Invalid user supplied memory map" ); ---Quoted 68 lines
Byungchul
Changes from v1
1. before - handleboot_mem_limitassuming the default isU64_MAX. after - handleboot_mem_limitassuming the default is 0.--->8---
Quoted 1 line
From
e8bf247d6024b35af5300914dcff9135df9c1d66Mon Sep 17 00:00:00 2001From: Byungchul Park <[email protected]>
Date: Wed, 24 Apr 2024 09:55:25 +0900
Subject: [PATCH v2] x86/e820: apply 'mem=' boot command while reserving memory usingboot_paramsWhen a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using
boot_params.Applied the restriction when reserving memory using
boot_params. While at it, renamedmem_sizeto a more specific name,boot_mem_limit.Signed-off-by Byungchul Park <[email protected]>--- arch/x86/kernel/e820.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)arch/x86/kernel/e820.c +6
-6@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;@@ -1036,6 +1036,9 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); }
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/e820.c +3
-0+if (boot_mem_limit) +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);pr_info ("extended physical RAM map:\n" );Signature · 1 line
2.17.1
mem= typically should cap the usable memory at that address. At one point in history we also allowed it to add memory at the top, but modern systems have too complex memory maps; the memmap= option can be used for that, however.
Quoted 145 lines · H. Peter Anvin
Quoted 144 lines · Byungchul Park
Quoted 1 line · Byungchul Park
I might miss something. Please lemme know if I go wrong. Thanks.
I started to work on it since I wanted to limit memory boundary using 'mem=' boot command but it doesn't work. However, while looking around the code in more detail, I found the issue is about which one should have higher priority between:
1. boot command limiting memory boundary e.g. 'mem=',
2. setup data of memory map from bootloader,boot_params.Based on the current code, setup data from bootloader has higher priority than boot command so the setup data can overwrite the user defined limit specified in boot command. Is it inteded?
If yes, I should stop posting.
If not, I will keep posting with the following - v3.Byungchul
---
arch/x86/kernel/e820.c +14
-3@@ -879,6 +879,7 @@ static void __init early_panic(char *msg) }static int userdef__initdata ; +static u64 userdef_mem_limit;/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p)@@ -905,7 +906,10 @@ static int __init parse_memopt(char *p)if (mem_size ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size;#ifdef CONFIG_MEMORY_HOTPLUG max_mem_size = mem_size;@@ -966,7 +970,10 @@ static int __init parse_memmap_one(char *p)else e820__range_remove (start_at, mem_size,0 ,0 ); }else { -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +if (userdef_mem_limit) + userdef_mem_limit =min (userdef_mem_limit, mem_size); +else + userdef_mem_limit = mem_size; }return *p =='\0' ?0 : -EINVAL ;@@ -1050,6 +1057,11 @@ void __init e820__reserve_setup_data(void)void __init e820__finish_early_params (void ) {if (userdef) { +if (userdef_mem_limit) +e820__range_remove (userdef_mem_limit, +ULLONG_MAX - userdef_mem_limit, +E820_TYPE_RAM ,1 ); +if (e820__update_table (e820_table) <0 )early_panic ("Invalid user supplied memory map" ); ---Quoted 73 lines
Byungchul
Changes from v1
1. before - handleboot_mem_limitassuming the default isU64_MAX. after - handleboot_mem_limitassuming the default is 0.--->8---
Quoted 1 line
From
e8bf247d6024b35af5300914dcff9135df9c1d66Mon Sep 17 00:00:00 2001From: Byungchul Park <[email protected]>
Date: Wed, 24 Apr 2024 09:55:25 +0900
Subject: [PATCH v2] x86/e820: apply 'mem=' boot command while reserving memory usingboot_paramsWhen a user specifies 'mem=' boot command, it's expected to limit the maximum address of usable memory for the kernel no matter what the memory map source is. However, 'mem=' boot command doesn't work since it doesn't respect it when reserving memory using
boot_params.Applied the restriction when reserving memory using
boot_params. While at it, renamedmem_sizeto a more specific name,boot_mem_limit.Signed-off-by Byungchul Park <[email protected]>--- arch/x86/kernel/e820.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)arch/x86/kernel/e820.c +9
-6@@ -880,11 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +static u64 boot_mem_limit; +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) { -u64 mem_size; -if (!p)return -EINVAL ;@@ -899,16 +899,16 @@ static int __init parse_memopt(char *p) } userdef =1 ; - mem_size =memparse (p, &p); + boot_mem_limit =memparse (p, &p);/* Don't remove all memory when getting "mem={invalid}" parameter: */ -if (mem_size ==0 ) +if (boot_mem_limit ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit,E820_TYPE_RAM ,1 );#ifdef CONFIG_MEMORY_HOTPLUG - max_mem_size = mem_size; + max_mem_size = boot_mem_limit;#endif return 0 ;@@ -1036,6 +1036,9 @@ void __init e820__reserve_setup_data(void)early_memunmap (data, len); } +if (boot_mem_limit) +e820__range_remove (boot_mem_limit,ULLONG_MAX - boot_mem_limit, +E820_TYPE_RESERVED_KERN ,1 );e820__update_table (e820_table);
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
arch/x86/kernel/e820.c +0
-0pr_info ("extended physical RAM map:\n" );Signature · 1 line
2.17.1mem= typically should cap the usable memory at that address. At one point in history we also allowed it to add memory at the top, but
Ah.. I just needed to cap for a test.
Quoted 2 lines
modern systems have too complex memory maps; the memmap= option can be used for that, however.
Even with memmap=, I still cannot cap the usable memory. However, with a tricky way like by adding a dummy reserve memory, I could manage to make it.
So.. Is the following approach not acceptable then?
https://lore.kernel.org/lkml/[email protected]/
Byungchul