All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
@ 2019-03-08  2:56 Baoquan He
  2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Baoquan He @ 2019-03-08  2:56 UTC (permalink / raw)
  To: linux-kernel, kirill.shutemov
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, hpa, x86, keescook,
	thgarnie, Baoquan He

This is v3 post, v2 post is here:
http://lkml.kernel.org/r/20190228003522.9957-1-bhe@redhat.com
v1 can be found here:
http://lkml.kernel.org/r/20190224132231.4878-1-bhe@redhat.com

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/20180912100135.GB3333@gmail.com

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(-)

-- 
2.17.2


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline
  2019-03-08  2:56 [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
@ 2019-03-08  2:56 ` Baoquan He
  2019-03-12  7:38   ` Kirill A. Shutemov
  2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Use only one PUD entry " tip-bot for Baoquan He
  2019-03-08  2:56 ` [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2019-04-04  2:18 ` [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2 siblings, 2 replies; 9+ messages in thread
From: Baoquan He @ 2019-03-08  2:56 UTC (permalink / raw)
  To: linux-kernel, kirill.shutemov
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, hpa, x86, keescook,
	thgarnie, 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.

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 <bhe@redhat.com>
---
 arch/x86/mm/kaslr.c | 88 +++++++++++++++++++++------------------------
 1 file changed, 41 insertions(+), 47 deletions(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 3f452ffed7e9..8bd4192c68b9 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -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();
 }
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
  2019-03-08  2:56 [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
@ 2019-03-08  2:56 ` Baoquan He
  2019-03-12  7:39   ` Kirill A. Shutemov
  2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB tip-bot for Baoquan He
  2019-04-04  2:18 ` [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2 siblings, 2 replies; 9+ messages in thread
From: Baoquan He @ 2019-03-08  2:56 UTC (permalink / raw)
  To: linux-kernel, kirill.shutemov
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, hpa, x86, keescook,
	thgarnie, 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 <bhe@redhat.com>
---
 arch/x86/mm/kaslr.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 8bd4192c68b9..9a8756517504 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -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.
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline
  2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
@ 2019-03-12  7:38   ` Kirill A. Shutemov
  2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Use only one PUD entry " tip-bot for Baoquan He
  1 sibling, 0 replies; 9+ messages in thread
From: Kirill A. Shutemov @ 2019-03-12  7:38 UTC (permalink / raw)
  To: Baoquan He
  Cc: linux-kernel, kirill.shutemov, dave.hansen, luto, peterz, tglx,
	mingo, bp, hpa, x86, keescook, thgarnie

On Fri, Mar 08, 2019 at 10:56:15AM +0800, Baoquan He wrote:
> 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.
> 
> Signed-off-by: Baoquan He <bhe@redhat.com>

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

-- 
 Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
  2019-03-08  2:56 ` [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
@ 2019-03-12  7:39   ` Kirill A. Shutemov
  2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB tip-bot for Baoquan He
  1 sibling, 0 replies; 9+ messages in thread
From: Kirill A. Shutemov @ 2019-03-12  7:39 UTC (permalink / raw)
  To: Baoquan He
  Cc: linux-kernel, kirill.shutemov, dave.hansen, luto, peterz, tglx,
	mingo, bp, hpa, x86, keescook, thgarnie

On Fri, Mar 08, 2019 at 10:56:16AM +0800, Baoquan He wrote:
> 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 <bhe@redhat.com>

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

-- 
 Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
  2019-03-08  2:56 [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
  2019-03-08  2:56 ` [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
@ 2019-04-04  2:18 ` Baoquan He
  2 siblings, 0 replies; 9+ messages in thread
From: Baoquan He @ 2019-04-04  2:18 UTC (permalink / raw)
  To: linux-kernel, kirill.shutemov
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, hpa, x86, keescook, thgarnie

PING

Is there any comment for this patchset, or could we consider to merge
them?

On 03/08/19 at 10:56am, Baoquan He wrote:
> This is v3 post, v2 post is here:
> http://lkml.kernel.org/r/20190228003522.9957-1-bhe@redhat.com
> v1 can be found here:
> http://lkml.kernel.org/r/20190224132231.4878-1-bhe@redhat.com
> 
> 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/20180912100135.GB3333@gmail.com
> 
> 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(-)
> 
> -- 
> 2.17.2
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [tip:x86/mm] x86/mm/KASLR: Use only one PUD entry for real mode trampoline
  2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
  2019-03-12  7:38   ` Kirill A. Shutemov
@ 2019-04-05 20:19   ` tip-bot for Baoquan He
  1 sibling, 0 replies; 9+ messages in thread
From: tip-bot for Baoquan He @ 2019-04-05 20:19 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: hpa, kirill.shutemov, tglx, bhe, mingo, linux-kernel

Commit-ID:  0925dda5962e9b55e4d38a72eba93858f24bac41
Gitweb:     https://git.kernel.org/tip/0925dda5962e9b55e4d38a72eba93858f24bac41
Author:     Baoquan He <bhe@redhat.com>
AuthorDate: Fri, 8 Mar 2019 10:56:15 +0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
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 ]

Signed-off-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: dave.hansen@linux.intel.com
Cc: luto@kernel.org
Cc: peterz@infradead.org
Cc: bp@alien8.de
Cc: hpa@zytor.com
Cc: keescook@chromium.org
Cc: thgarnie@google.com
Link: https://lkml.kernel.org/r/20190308025616.21440-2-bhe@redhat.com

---
 arch/x86/mm/kaslr.c | 84 +++++++++++++++++++++++------------------------------
 1 file changed, 37 insertions(+), 47 deletions(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 3f452ffed7e9..97813751340d 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -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();
 }

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [tip:x86/mm] x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB
  2019-03-08  2:56 ` [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  2019-03-12  7:39   ` Kirill A. Shutemov
@ 2019-04-05 20:19   ` tip-bot for Baoquan He
  1 sibling, 0 replies; 9+ messages in thread
From: tip-bot for Baoquan He @ 2019-04-05 20:19 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: bhe, mingo, kirill.shutemov, linux-kernel, tglx, hpa

Commit-ID:  b569c18434987163a05f05a12cdf6a9975c55ff3
Gitweb:     https://git.kernel.org/tip/b569c18434987163a05f05a12cdf6a9975c55ff3
Author:     Baoquan He <bhe@redhat.com>
AuthorDate: Fri, 8 Mar 2019 10:56:16 +0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
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 ]

Signed-off-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: dave.hansen@linux.intel.com
Cc: luto@kernel.org
Cc: peterz@infradead.org
Cc: bp@alien8.de
Cc: hpa@zytor.com
Cc: keescook@chromium.org
Cc: thgarnie@google.com
Link: https://lkml.kernel.org/r/20190308025616.21440-3-bhe@redhat.com
---
 arch/x86/mm/kaslr.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 97813751340d..f6ba2791eeb5 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -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;
 	}
 }

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level
  2019-02-28  9:10 ` Kirill A. Shutemov
@ 2019-02-28  9:29   ` Baoquan He
  0 siblings, 0 replies; 9+ messages in thread
From: Baoquan He @ 2019-02-28  9:29 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: linux-kernel, kirill.shutemov, dave.hansen, luto, peterz, tglx,
	mingo, bp, hpa, x86, keescook, thgarnie

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 <bhe@redhat.com>
---
v2->v3:
  The v2 was based on another patchset. Rebase it on the latest linus's
  tree.

 arch/x86/mm/kaslr.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 4e7f07b466c5..4ef9a27a1361 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -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.
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-04-05 20:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-08  2:56 [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
2019-03-08  2:56 ` [PATCH v3 1/2] x86/mm/KASLR: Only build one PUD entry of area for real mode trampoline Baoquan He
2019-03-12  7:38   ` Kirill A. Shutemov
2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Use only one PUD entry " tip-bot for Baoquan He
2019-03-08  2:56 ` [PATCH v3 2/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
2019-03-12  7:39   ` Kirill A. Shutemov
2019-04-05 20:19   ` [tip:x86/mm] x86/mm/KASLR: Reduce randomization granularity for 5-level paging to 1GB tip-bot for Baoquan He
2019-04-04  2:18 ` [PATCH v3 0/2] x86/mm/KASLR: Change the granularity of randomization to PUD size in 5-level Baoquan He
  -- strict thread matches above, loose matches on Subject: below --
2019-02-28  0:35 [PATCH v2 " Baoquan He
2019-02-28  9:10 ` Kirill A. Shutemov
2019-02-28  9:29   ` [PATCH v3 " Baoquan He

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.