x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
v3 cover x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
This is v3 post, v2 post is here:
http://lkml.kernel.org/r/[email protected]
v1 can be found here:
http://lkml.kernel.org/r/[email protected]
Background:
***
Earlier, during a series of KASLR patch reviewing, Ingo got the current memory region KASLR only has granularity of randomization in PUD size in 4-level paging mode, and P4D size in 5-level paging mode, He suggested me to try to change both of them to be PMD size at granularity:
http://lkml.kernel.org/r/[email protected]
Later, I changed code to support PMD level of randomization for both 4-level and 5-level.
https://github.com/baoquan-he/linux/commits/mm-kaslr-2m-aligned
The test passed on my KVM guest with 1 GB RAM, but failed when I increased the RAM to 4 GB, and failed either on larger RAM.
After analyzing, it's because that 1 GB page mapping need be mapped at 1 GB aligned physical address for intel CPU. The 2 MB level of randomization will break it and cause error. Please check below table in intel IA32 manual.
Table 4-15. Format of an IA-32e Page-Directory-Pointer-Table Entry (PDPTE) that Maps a 1-GByte Page
So PMD level of randomization for mm KASLR is not doable.
However, during investigation and testing above code, it turns out that the current code is misleading to build identity mapping for the real mode trampoline in case KASLR enabled. From code, only a small area (which is smaller than 1 MB) need be identity mapped. Please check below patch which is from above mm-kaslr-2m-aligned patch series. it only builds up 2 MB identity maping for real mode trampoline, and test passed on machines with 32 GB RAM of 4-level and on KVM guest of 5-level.
https://github.com/baoquan-he/linux/commit/e120e67fbf9a5aa818d20084d8dea5b4a27ecf97
Result:
Make a patchset to:
1)change code to only build 1 GB of area for real mode trampoline,
namely only copy one PUD entry where physical address 0 resides;
2)improve the randomization granularity of 5-level from P4D size to PUD size.
Changelog:v2->v3:
Improve patch 1/2 according to Kirill's comments:
*) Adjust code change of 1/2;
*) Add code comment to explain the two kinds of mapping thing for
real mode;v1->v2:
Improve patch according to Kirill's suggestions:
*)Add more information to code comment for better understanding;
*)Improve code to save one low memory page in 4-level;Baoquan He (2):
x86/mm/KASLR: Only build one PUD entry of area for real mode
trampoline
x86/mm/KASLR: Change the granularity of randomization to PUD size in
5-level
arch/x86/mm/kaslr.c | 98 ++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 55 deletions(-)
Signature · 1 line
2.17.2
PATCH v3 1/2 x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline
The current code builds identity mapping for real mode treampoline by borrowing page tables from the direct mapping section if KASLR is enabled. It will copy present entries of the first PUD table in 4-level paging mode, or the first P4D table in 5-level paging mode.
However, there's only a very small area under low 1 MB reserved for real mode trampoline in reserve_real_mode(). Makes no sense to build up so large area of mapping for it. Since the randomization granularity in 4-level is 1 GB, and 512 GB in 5-level, only copying one PUD entry is enough.
Hence, only copy one PUD entry of area where physical address 0 resides. And this is preparation for later changing the randomization granularity of 5-level paging mode from 512 GB to 1 GB.
--- arch/x86/mm/kaslr.c | 88 +++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 47 deletions(-)
arch/x86/mm/kaslr.c +41 -47
@@ -147,74 +147,68 @@ void __init kernel_randomize_memory(void)static void __meminit init_trampoline_pud (void ) { -unsigned long paddr, paddr_next; +pud_t *pud_page_tramp, *pud, *pud_tramp; +p4d_t *p4d_page_tramp, *p4d, *p4d_tramp; +unsigned long paddr, vaddr;pgd_t *pgd; -pud_t *pud_page, *pud_page_tramp; -int i; pud_page_tramp =alloc_low_page (); +/* +* There are two kinds of mapping in the handling for the same +* place of low 1 MB area. They are the direct mapping and the +* 1:1 mapping for real mode trampoline. +* ------------------------------------------------------------ +* Kind Physical address Virtual address +* Direct mapping paddr vaddr=paddr+PAGE_OFFSET +* 1:1 mapping paddr paddr +*/ paddr =0 ; - pgd =pgd_offset_k ((unsigned long )__va (paddr)); - pud_page = (pud_t *)pgd_page_vaddr (*pgd); - -for (i =pud_index (paddr); i <PTRS_PER_PUD ; i++, paddr = paddr_next) { -pud_t *pud, *pud_tramp; -unsigned long vaddr = (unsigned long )__va (paddr); - - pud_tramp = pud_page_tramp +pud_index (paddr); - pud = pud_page +pud_index (vaddr); - paddr_next = (paddr &PUD_MASK ) +PUD_SIZE ; - - *pud_tramp = *pud; - } - -set_pgd (&trampoline_pgd_entry, -__pgd (_KERNPG_TABLE |__pa (pud_page_tramp))); -} + vaddr = (unsigned long )__va (paddr); + pgd =pgd_offset_k (vaddr); -static void __meminit init_trampoline_p4d (void ) -{ -unsigned long paddr, paddr_next; -pgd_t *pgd; -p4d_t *p4d_page, *p4d_page_tramp; -int i; + p4d =p4d_offset (pgd, vaddr); + pud =pud_offset (p4d, vaddr); - p4d_page_tramp =alloc_low_page (); - - paddr =0 ; - pgd =pgd_offset_k ((unsigned long )__va (paddr)); - p4d_page = (p4d_t *)pgd_page_vaddr (*pgd); + pud_tramp = pud_page_tramp +pud_index (paddr); + *pud_tramp = *pud; -for (i =p4d_index (paddr); i <PTRS_PER_P4D ; i++, paddr = paddr_next) { -p4d_t *p4d, *p4d_tramp; -unsigned long vaddr = (unsigned long )__va (paddr); +if (pgtable_l5_enabled ()) { + p4d_page_tramp =alloc_low_page (); p4d_tramp = p4d_page_tramp +p4d_index (paddr); - p4d = p4d_page +p4d_index (vaddr); - paddr_next = (paddr &P4D_MASK ) +P4D_SIZE ; - *p4d_tramp = *p4d; - } +set_p4d (p4d_tramp, +__p4d (_KERNPG_TABLE |__pa (pud_page_tramp))); -set_pgd (&trampoline_pgd_entry, -__pgd (_KERNPG_TABLE |__pa (p4d_page_tramp))); +set_pgd (&trampoline_pgd_entry, +__pgd (_KERNPG_TABLE |__pa (p4d_page_tramp))); + }else { +set_pgd (&trampoline_pgd_entry, +__pgd (_KERNPG_TABLE |__pa (pud_page_tramp))); + } }/* -* Create PGD aligned trampoline table to allow real mode initialization -* of additional CPUs. Consume only 1 low memory page. +* Real mode trampoline only occupies a small area under low 1 MB +* (please check codes in reserve_real_mode() for details). For +* APs' booting up, we just borrow as few page tables as possible +* from the direct physical mapping to build 1:1 mapping to cover +* that area. In case KASLR disabled, the 1st PGD entry of the +* direct mapping is copied directly. If KASLR is enabled, only +* copy the 1st PUD entry where physical address 0 resides since +* the granularity of randomization is PUD size in 4-level, and +* P4D size in 5-level. +* +* This consumes one low memory page in 4-level case, and extra one +* in 5-level. */ void __meminit init_trampoline (void ) { -if (!kaslr_memory_enabled ()) {init_trampoline_default ();return ; } -if (pgtable_l5_enabled ()) -init_trampoline_p4d (); -else -init_trampoline_pud (); +init_trampoline_pud (); }
Signature · 1 line
2.17.2
Re: [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline
Quoted 11 lines · Baoquan He
The current code builds identity mapping for real mode treampoline by borrowing page tables from the direct mapping section if KASLR is enabled. It will copy present entries of the first PUD table in 4-level paging mode, or the first P4D table in 5-level paging mode.
However, there's only a very small area under low 1 MB reserved for real mode trampoline in
reserve_real_mode(). Makes no sense to build up so large area of mapping for it. Since the randomization granularity in 4-level is 1 GB, and 512 GB in 5-level, only copying one PUD entry is enough.
Quoted 5 lines
Hence, only copy one PUD entry of area where physical address 0 resides. And this is preparation for later changing the randomization granularity of 5-level paging mode from 512 GB to 1 GB.
Signed-off-by Baoquan He <[email protected]>
Signature · 1 line
Kirill A. Shutemov
tip-bot for Baoquan He
merge bot[tip:x86/mm] x86/mm/KASLR: Use only one PUD entry for real mode trampoline
In reply to Baoquan Hex86/mm/KASLR: Use only one PUD entry for real mode trampoline
Commit-ID: 0925dda5962e9b55e4d38a72eba93858f24bac41 Gitweb: https://git.kernel.org/tip/0925dda5962e9b55e4d38a72eba93858f24bac41 Author: Baoquan He <[email protected]> AuthorDate: Fri, 8 Mar 2019 10:56:15 +0800 Committer: Thomas Gleixner <[email protected]> CommitDate: Fri, 5 Apr 2019 22:13:00 +0200
x86/mm/KASLR: Use only one PUD entry for real mode trampoline
The current code builds identity mapping for the real mode trampoline by borrowing page tables from the direct mapping section if KASLR is enabled. It copies present entries of the first PUD table in 4-level paging mode, or the first P4D table in 5-level paging mode.
However, there's only a very small area under low 1 MB reserved for the real mode trampoline in reserve_real_mode() so it makes no sense to build up a really large mapping for it.
Reduce it to one PUD (1GB) entry. This matches the randomization granularity in 4-level paging mode and allows to change the randomization granularity in 5-level paging mode from 512GB to 1GB later.
[ tglx: Massaged changelog and comments ]
--- arch/x86/mm/kaslr.c | 84 +++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 47 deletions(-)
arch/x86/mm/kaslr.c +37 -47
@@ -147,74 +147,64 @@ void __init kernel_randomize_memory(void)static void __meminit init_trampoline_pud (void ) { -unsigned long paddr, paddr_next; +pud_t *pud_page_tramp, *pud, *pud_tramp; +p4d_t *p4d_page_tramp, *p4d, *p4d_tramp; +unsigned long paddr, vaddr;pgd_t *pgd; -pud_t *pud_page, *pud_page_tramp; -int i; pud_page_tramp =alloc_low_page (); +/* +* There are two mappings for the low 1MB area, the direct mapping +* and the 1:1 mapping for the real mode trampoline: +* +* Direct mapping: virt_addr = phys_addr + PAGE_OFFSET +* 1:1 mapping: virt_addr = phys_addr +*/ paddr =0 ; - pgd =pgd_offset_k ((unsigned long )__va (paddr)); - pud_page = (pud_t *)pgd_page_vaddr (*pgd); - -for (i =pud_index (paddr); i <PTRS_PER_PUD ; i++, paddr = paddr_next) { -pud_t *pud, *pud_tramp; -unsigned long vaddr = (unsigned long )__va (paddr); + vaddr = (unsigned long )__va (paddr); + pgd =pgd_offset_k (vaddr); - pud_tramp = pud_page_tramp +pud_index (paddr); - pud = pud_page +pud_index (vaddr); - paddr_next = (paddr &PUD_MASK ) +PUD_SIZE ; - - *pud_tramp = *pud; - } + p4d =p4d_offset (pgd, vaddr); + pud =pud_offset (p4d, vaddr); -set_pgd (&trampoline_pgd_entry, -__pgd (_KERNPG_TABLE |__pa (pud_page_tramp))); -} - -static void __meminit init_trampoline_p4d (void ) -{ -unsigned long paddr, paddr_next; -pgd_t *pgd; -p4d_t *p4d_page, *p4d_page_tramp; -int i; + pud_tramp = pud_page_tramp +pud_index (paddr); + *pud_tramp = *pud; - p4d_page_tramp =alloc_low_page (); - - paddr =0 ; - pgd =pgd_offset_k ((unsigned long )__va (paddr)); - p4d_page = (p4d_t *)pgd_page_vaddr (*pgd); - -for (i =p4d_index (paddr); i <PTRS_PER_P4D ; i++, paddr = paddr_next) { -p4d_t *p4d, *p4d_tramp; -unsigned long vaddr = (unsigned long )__va (paddr); +if (pgtable_l5_enabled ()) { + p4d_page_tramp =alloc_low_page (); p4d_tramp = p4d_page_tramp +p4d_index (paddr); - p4d = p4d_page +p4d_index (vaddr); - paddr_next = (paddr &P4D_MASK ) +P4D_SIZE ; - *p4d_tramp = *p4d; - } +set_p4d (p4d_tramp, +__p4d (_KERNPG_TABLE |__pa (pud_page_tramp))); -set_pgd (&trampoline_pgd_entry, -__pgd (_KERNPG_TABLE |__pa (p4d_page_tramp))); +set_pgd (&trampoline_pgd_entry, +__pgd (_KERNPG_TABLE |__pa (p4d_page_tramp))); + }else { +set_pgd (&trampoline_pgd_entry, +__pgd (_KERNPG_TABLE |__pa (pud_page_tramp))); + } }/* -* Create PGD aligned trampoline table to allow real mode initialization -* of additional CPUs. Consume only 1 low memory page. +* The real mode trampoline, which is required for bootstrapping CPUs +* occupies only a small area under the low 1MB. See reserve_real_mode() +* for details. +* +* If KASLR is disabled the first PGD entry of the direct mapping is copied +* to map the real mode trampoline. +* +* If KASLR is enabled, copy only the PUD which covers the low 1MB +* area. This limits the randomization granularity to 1GB for both 4-level +* and 5-level paging. */ void __meminit init_trampoline (void ) { -if (!kaslr_memory_enabled ()) {init_trampoline_default ();return ; } -if (pgtable_l5_enabled ()) -init_trampoline_p4d (); -else -init_trampoline_pud (); +init_trampoline_pud (); }
PATCH v3 2/2 x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
In reply to Baoquan HeThe current randomization granularity of 5-level is 512 GB. Improve it to 1 GB. This can add more randomness to memory region KASLR in 5-level paging mode
--- arch/x86/mm/kaslr.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
arch/x86/mm/kaslr.c +4 -10
@@ -125,10 +125,7 @@ void __init kernel_randomize_memory(void)*/ entropy = remain_entropy / (ARRAY_SIZE (kaslr_regions) - i);prandom_bytes_state (&rand_state, &rand,sizeof (rand)); -if (pgtable_l5_enabled ()) - entropy = (rand % (entropy +1 )) &P4D_MASK ; -else - entropy = (rand % (entropy +1 )) &PUD_MASK ; + entropy = (rand % (entropy +1 )) &PUD_MASK ; vaddr += entropy; *kaslr_regions[i].base = vaddr;
@@ -137,10 +134,7 @@ void __init kernel_randomize_memory(void)* randomization alignment. */ vaddr +=get_padding (&kaslr_regions[i]); -if (pgtable_l5_enabled ()) - vaddr =round_up (vaddr +1 ,P4D_SIZE ); -else - vaddr =round_up (vaddr +1 ,PUD_SIZE ); + vaddr =round_up (vaddr +1 ,PUD_SIZE ); remain_entropy -= entropy; } }
@@ -197,8 +191,8 @@ static void __meminit init_trampoline_pud(void)* that area. In case KASLR disabled, the 1st PGD entry of the * direct mapping is copied directly. If KASLR is enabled, only * copy the 1st PUD entry where physical address 0 resides since -* the granularity of randomization is PUD size in 4-level, and -* P4D size in 5-level. +* the granularity of randomization is PUD size in both 4-level and +* 5-level. * * This consumes one low memory page in 4-level case, and extra one * in 5-level.
Signature · 1 line
2.17.2
Quoted 5 lines · Baoquan He
The current randomization granularity of 5-level is 512 GB. Improve it to 1 GB. This can add more randomness to memory region KASLR in 5-level paging mode
Signed-off-by Baoquan He <[email protected]>
Signature · 1 line
Kirill A. Shutemov
tip-bot for Baoquan He
merge bot[tip:x86/mm] x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB
In reply to Baoquan Hex86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB
Commit-ID: b569c18434987163a05f05a12cdf6a9975c55ff3 Gitweb: https://git.kernel.org/tip/b569c18434987163a05f05a12cdf6a9975c55ff3 Author: Baoquan He <[email protected]> AuthorDate: Fri, 8 Mar 2019 10:56:16 +0800 Committer: Thomas Gleixner <[email protected]> CommitDate: Fri, 5 Apr 2019 22:13:52 +0200
x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB
The current randomization granularity of 5-level is 512 GB. The mapping of the real mode trampoline has been reduced to one PUD entry, so there is no restriction anymore.
Reduce the granularity to 1GB for 5-level paging mode which allows better randomization.
[ tglx: Massaged changelog ]
--- arch/x86/mm/kaslr.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
arch/x86/mm/kaslr.c +2 -8
@@ -125,10 +125,7 @@ void __init kernel_randomize_memory(void)*/ entropy = remain_entropy / (ARRAY_SIZE (kaslr_regions) - i);prandom_bytes_state (&rand_state, &rand,sizeof (rand)); -if (pgtable_l5_enabled ()) - entropy = (rand % (entropy +1 )) &P4D_MASK ; -else - entropy = (rand % (entropy +1 )) &PUD_MASK ; + entropy = (rand % (entropy +1 )) &PUD_MASK ; vaddr += entropy; *kaslr_regions[i].base = vaddr;
@@ -137,10 +134,7 @@ void __init kernel_randomize_memory(void)* randomization alignment. */ vaddr +=get_padding (&kaslr_regions[i]); -if (pgtable_l5_enabled ()) - vaddr =round_up (vaddr +1 ,P4D_SIZE ); -else - vaddr =round_up (vaddr +1 ,PUD_SIZE ); + vaddr =round_up (vaddr +1 ,PUD_SIZE ); remain_entropy -= entropy; } }
PING
Is there any comment for this patchset, or could we consider to merge them?
Quoted 71 lines · Baoquan He
This is v3 post, v2 post is here:
http://lkml.kernel.org/r/[email protected]
v1 can be found here:
http://lkml.kernel.org/r/[email protected]Background:
***
Earlier, during a series of KASLR patch reviewing, Ingo got the current memory region KASLR only has granularity of randomization in PUD size in 4-level paging mode, and P4D size in 5-level paging mode, He suggested me to try to change both of them to be PMD size at granularity:http://lkml.kernel.org/r/[email protected]
Later, I changed code to support PMD level of randomization for both 4-level and 5-level.
https://github.com/baoquan-he/linux/commits/mm-kaslr-2m-aligned
The test passed on my KVM guest with 1 GB RAM, but failed when I increased the RAM to 4 GB, and failed either on larger RAM.
After analyzing, it's because that 1 GB page mapping need be mapped at 1 GB aligned physical address for intel CPU. The 2 MB level of randomization will break it and cause error. Please check below table in intel IA32 manual.
Table 4-15. Format of an IA-32e Page-Directory-Pointer-Table Entry (PDPTE) that Maps a 1-GByte Page
So PMD level of randomization for mm KASLR is not doable.
However, during investigation and testing above code, it turns out that the current code is misleading to build identity mapping for the real mode trampoline in case KASLR enabled. From code, only a small area (which is smaller than 1 MB) need be identity mapped. Please check below patch which is from above mm-kaslr-2m-aligned patch series. it only builds up 2 MB identity maping for real mode trampoline, and test passed on machines with 32 GB RAM of 4-level and on KVM guest of 5-level.
https://github.com/baoquan-he/linux/commit/e120e67fbf9a5aa818d20084d8dea5b4a27ecf97
Result:
Make a patchset to:
1)change code to only build 1 GB of area for real mode trampoline,namely only copy one PUD entry where physical address 0 resides;2)improve the randomization granularity of 5-level from P4D size to PUD size.
Changelog:
v2->v3:
Improve patch 1/2 according to Kirill's comments:*) Adjust code change of 1/2; *) Add code comment to explain the two kinds of mapping thing for real mode;
v1->v2:
Improve patch according to Kirill's suggestions:*)Add more information to code comment for better understanding; *)Improve code to save one low memory page in 4-level;Baoquan He (2):
x86/mm/KASLR: Only build one PUD entry of area for real mode
trampoline
x86/mm/KASLR: Change the granularity of randomization to PUD size in5-levelarch/x86/mm/kaslr.c | 98 ++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 55 deletions(-)Signature · 1 line
2.17.2