mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arvind Sankar <nivedita@alum.mit.edu>
To: Arvind Sankar <nivedita@alum.mit.edu>
Cc: x86@kernel.org, Joerg Roedel <jroedel@suse.de>,
	Borislav Petkov <bp@alien8.de>,
	linux-kernel@vger.kernel.org,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Subject: Re: [PATCH] x86/boot/64: Initialize 5-level paging variables earlier
Date: Sat, 10 Oct 2020 15:26:24 -0400	[thread overview]
Message-ID: <20201010192624.GA4062867@rani.riverdale.lan> (raw)
In-Reply-To: <20201010191110.4060905-1-nivedita@alum.mit.edu>

On Sat, Oct 10, 2020 at 03:11:10PM -0400, Arvind Sankar wrote:
> Commit
>   ca0e22d4f011 ("x86/boot/compressed/64: Always switch to own page table")
> started using a new set of pagetables even without KASLR.
> 
> After that commit, initialize_identity_maps() is called before the
> 5-level paging variables are setup in choose_random_location(), which
> will not work if 5-level paging is actually enabled.

Note that I don't have hardware that supports 5-level paging, so this
is not actually tested with 5-level, but based on code inspection, it
shouldn't work.

> 
> Fix this by moving the initialization of __pgtable_l5_enabled,
> pgdir_shift and ptrs_per_p4d into cleanup_trampoline(), which is called
> immediately after the finalization of whether the kernel is executing
> with 4- or 5-level paging. This will be earlier than anything that might
> require those variables, and keeps the 4- vs 5-level paging code all in
> one place.
> 
> Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu>
> ---
>  arch/x86/boot/compressed/ident_map_64.c |  6 ------
>  arch/x86/boot/compressed/kaslr.c        |  8 --------
>  arch/x86/boot/compressed/pgtable_64.c   | 16 ++++++++++++++++
>  3 files changed, 16 insertions(+), 14 deletions(-)
> 
> diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c
> index a3613857c532..505d6299b76e 100644
> --- a/arch/x86/boot/compressed/ident_map_64.c
> +++ b/arch/x86/boot/compressed/ident_map_64.c
> @@ -34,12 +34,6 @@
>  #define __PAGE_OFFSET __PAGE_OFFSET_BASE
>  #include "../../mm/ident_map.c"
>  
> -#ifdef CONFIG_X86_5LEVEL
> -unsigned int __pgtable_l5_enabled;
> -unsigned int pgdir_shift = 39;
> -unsigned int ptrs_per_p4d = 1;
> -#endif
> -
>  /* Used by PAGE_KERN* macros: */
>  pteval_t __default_kernel_pte_mask __read_mostly = ~0;
>  
> diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
> index 489fabc051d7..9eabd8bc7673 100644
> --- a/arch/x86/boot/compressed/kaslr.c
> +++ b/arch/x86/boot/compressed/kaslr.c
> @@ -835,14 +835,6 @@ void choose_random_location(unsigned long input,
>  		return;
>  	}
>  
> -#ifdef CONFIG_X86_5LEVEL
> -	if (__read_cr4() & X86_CR4_LA57) {
> -		__pgtable_l5_enabled = 1;
> -		pgdir_shift = 48;
> -		ptrs_per_p4d = 512;
> -	}
> -#endif
> -
>  	boot_params->hdr.loadflags |= KASLR_FLAG;
>  
>  	if (IS_ENABLED(CONFIG_X86_32))
> diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c
> index 46d761f7faa6..0976c2d2ab2f 100644
> --- a/arch/x86/boot/compressed/pgtable_64.c
> +++ b/arch/x86/boot/compressed/pgtable_64.c
> @@ -9,6 +9,13 @@
>  #define BIOS_START_MIN		0x20000U	/* 128K, less than this is insane */
>  #define BIOS_START_MAX		0x9f000U	/* 640K, absolute maximum */
>  
> +#ifdef CONFIG_X86_5LEVEL
> +/* __pgtable_l5_enabled needs to be in .data to avoid being cleared along with .bss */
> +unsigned int __section(.data) __pgtable_l5_enabled;
> +unsigned int __section(.data) pgdir_shift = 39;
> +unsigned int __section(.data) ptrs_per_p4d = 1;
> +#endif
> +
>  struct paging_config {
>  	unsigned long trampoline_start;
>  	unsigned long l5_required;
> @@ -195,4 +202,13 @@ void cleanup_trampoline(void *pgtable)
>  
>  	/* Restore trampoline memory */
>  	memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE);
> +
> +	/* Initialize variables for 5-level paging */
> +#ifdef CONFIG_X86_5LEVEL
> +	if (__read_cr4() & X86_CR4_LA57) {
> +		__pgtable_l5_enabled = 1;
> +		pgdir_shift = 48;
> +		ptrs_per_p4d = 512;
> +	}
> +#endif
>  }
> -- 
> 2.26.2
> 

  reply	other threads:[~2020-10-10 23:11 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-08 19:16 [PATCH v2 0/5] Couple of bugfixes to sev-es series Arvind Sankar
2020-10-08 19:16 ` [PATCH v2 1/5] x86/boot: Initialize boot_params in startup code Arvind Sankar
2020-10-08 19:16 ` [PATCH v2 2/5] x86/boot: Split out command-line related declarations Arvind Sankar
2020-10-08 19:16 ` [PATCH v2 3/5] x86/boot/64: Show original faulting address in case of error Arvind Sankar
2020-10-09 14:42   ` Joerg Roedel
2020-10-08 19:16 ` [PATCH v2 4/5] x86/boot/64: Explicitly map boot_params and command line Arvind Sankar
2020-10-09 14:49   ` Joerg Roedel
2020-10-16 16:27   ` Borislav Petkov
2020-10-16 16:47     ` Arvind Sankar
2020-10-16 17:07       ` Borislav Petkov
2020-10-16 17:20         ` Arvind Sankar
2020-10-16 17:32           ` Borislav Petkov
2020-10-16 20:04             ` [PATCH v3 1/4] " Arvind Sankar
2020-10-16 20:04               ` [PATCH v3 2/4] x86/boot: Initialize boot_params in startup code Arvind Sankar
2020-10-16 20:04               ` [PATCH v3 3/4] x86/boot: Split out command-line related declarations Arvind Sankar
2020-10-16 20:04               ` [PATCH v3 4/4] x86/boot/64: Show original faulting address in case of error Arvind Sankar
2020-10-19 14:51               ` [PATCH v3 1/4] x86/boot/64: Explicitly map boot_params and command line Borislav Petkov
2020-10-19 17:12                 ` Arvind Sankar
2020-10-19 17:31                   ` Borislav Petkov
2020-10-19 19:44               ` [tip: x86/seves] " tip-bot2 for Arvind Sankar
2020-10-16 21:18             ` [PATCH v2 4/5] " Arvind Sankar
2020-10-16 21:23               ` Borislav Petkov
2020-10-08 19:16 ` [PATCH v2 5/5] x86/head/64: Disable stack protection for head$(BITS).o Arvind Sankar
2020-10-09 14:49   ` Joerg Roedel
2020-10-16 11:17   ` Borislav Petkov
2020-10-16 12:43     ` Arvind Sankar
2020-10-16 13:15       ` Borislav Petkov
2020-10-16 14:16         ` Arvind Sankar
2020-10-19 19:44   ` [tip: x86/seves] " tip-bot2 for Arvind Sankar
2020-10-10 19:11 ` [PATCH] x86/boot/64: Initialize 5-level paging variables earlier Arvind Sankar
2020-10-10 19:26   ` Arvind Sankar [this message]
2020-10-12 14:08     ` Kirill A. Shutemov
2020-10-12 15:35       ` Arvind Sankar
2020-10-13  8:11         ` Borislav Petkov
2020-10-13  8:20           ` Kirill A. Shutemov
2020-10-13  8:33             ` Borislav Petkov
2020-10-13  9:12               ` Kirill A. Shutemov
2020-10-13  9:46                 ` Borislav Petkov
2020-10-15 13:52               ` Kirill A. Shutemov
2020-10-16 10:21                 ` Borislav Petkov
2020-10-13  8:59   ` Joerg Roedel
2020-10-19 19:44   ` [tip: x86/seves] " tip-bot2 for Arvind Sankar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201010192624.GA4062867@rani.riverdale.lan \
    --to=nivedita@alum.mit.edu \
    --cc=bp@alien8.de \
    --cc=jroedel@suse.de \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®