public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tom Lendacky <thomas.lendacky@amd.com>
To: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	x86@kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	"H. Peter Anvin" <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Borislav Petkov <bp@suse.de>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCHv3 3/3] x86/mm/encrypt: Rewrite sme_pgtable_calc()
Date: Tue, 30 Jan 2018 16:51:34 -0600	[thread overview]
Message-ID: <12ffff46-474b-5fb5-c143-e2db29b3f8a0@amd.com> (raw)
In-Reply-To: <20180124163623.61765-4-kirill.shutemov@linux.intel.com>

On 1/24/2018 10:36 AM, Kirill A. Shutemov wrote:
> sme_pgtable_calc() is unnecessary complex. It can be re-written in a
> more stream-lined way.
> 
> As a side effect, we would get the code ready to boot-time switching
> between paging modes.
> 
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>

> ---
>  arch/x86/mm/mem_encrypt_identity.c | 42 +++++++++++---------------------------
>  1 file changed, 12 insertions(+), 30 deletions(-)
> 
> diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_identity.c
> index 69635a02ce9e..613686cc56ae 100644
> --- a/arch/x86/mm/mem_encrypt_identity.c
> +++ b/arch/x86/mm/mem_encrypt_identity.c
> @@ -230,8 +230,7 @@ static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
>  
>  static unsigned long __init sme_pgtable_calc(unsigned long len)
>  {
> -	unsigned long p4d_size, pud_size, pmd_size, pte_size;
> -	unsigned long total;
> +	unsigned long entries = 0, tables = 0;
>  
>  	/*
>  	 * Perform a relatively simplistic calculation of the pagetable
> @@ -245,42 +244,25 @@ static unsigned long __init sme_pgtable_calc(unsigned long len)
>  	 * Incrementing the count for each covers the case where the addresses
>  	 * cross entries.
>  	 */
> -	if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
> -		p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
> -		p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
> -		pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1;
> -		pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
> -	} else {
> -		p4d_size = 0;
> -		pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
> -		pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
> -	}
> -	pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1;
> -	pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
> -	pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE;
>  
> -	total = p4d_size + pud_size + pmd_size + pte_size;
> +	/* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */
> +	if (PTRS_PER_P4D > 1)
> +		entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D;
> +	entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD;
> +	entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD;
> +	entries += 2 * sizeof(pte_t) * PTRS_PER_PTE;
>  
>  	/*
>  	 * Now calculate the added pagetable structures needed to populate
>  	 * the new pagetables.
>  	 */
> -	if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
> -		p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
> -		p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
> -		pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE;
> -		pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
> -	} else {
> -		p4d_size = 0;
> -		pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
> -		pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
> -	}
> -	pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE;
> -	pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
>  
> -	total += p4d_size + pud_size + pmd_size;
> +	if (PTRS_PER_P4D > 1)
> +		tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D;
> +	tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD;
> +	tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD;
>  
> -	return total;
> +	return entries + tables;
>  }
>  
>  void __init __nostackprotector sme_encrypt_kernel(struct boot_params *bp)
> 

  reply	other threads:[~2018-01-30 22:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-24 16:36 [PATCHv3 0/3] x86/mm/encrypt: Cleanup and switching between paging modes Kirill A. Shutemov
2018-01-24 16:36 ` [PATCHv3 1/3] x86/mm/encrypt: Move page table helpers into separate translation unit Kirill A. Shutemov
2018-01-30 22:26   ` Tom Lendacky
2018-01-30 22:28     ` Kirill A. Shutemov
2018-01-30 22:40       ` Tom Lendacky
2018-01-24 16:36 ` [PATCHv3 2/3] x86/mm/encrypt: Rewrite sme_populate_pgd() and sme_populate_pgd_large() Kirill A. Shutemov
2018-01-30 22:48   ` Tom Lendacky
2018-01-24 16:36 ` [PATCHv3 3/3] x86/mm/encrypt: Rewrite sme_pgtable_calc() Kirill A. Shutemov
2018-01-30 22:51   ` Tom Lendacky [this message]
2018-01-30 22:52 ` [PATCHv3 0/3] x86/mm/encrypt: Cleanup and switching between paging modes Tom Lendacky

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=12ffff46-474b-5fb5-c143-e2db29b3f8a0@amd.com \
    --to=thomas.lendacky@amd.com \
    --cc=bp@suse.de \
    --cc=hpa@zytor.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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