Skip to content

Commit 1070730

Browse files
kirylIngo Molnar
authored andcommitted
x86/mm/encrypt: Simplify sme_pgtable_calc()
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. Tested-by: Tom Lendacky <thomas.lendacky@amd.com> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20180131135404.40692-4-kirill.shutemov@linux.intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent aad9839 commit 1070730

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

arch/x86/mm/mem_encrypt_identity.c

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
231231

232232
static unsigned long __init sme_pgtable_calc(unsigned long len)
233233
{
234-
unsigned long p4d_size, pud_size, pmd_size, pte_size;
235-
unsigned long total;
234+
unsigned long entries = 0, tables = 0;
236235

237236
/*
238237
* Perform a relatively simplistic calculation of the pagetable
@@ -246,42 +245,25 @@ static unsigned long __init sme_pgtable_calc(unsigned long len)
246245
* Incrementing the count for each covers the case where the addresses
247246
* cross entries.
248247
*/
249-
if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
250-
p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
251-
p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
252-
pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1;
253-
pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
254-
} else {
255-
p4d_size = 0;
256-
pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
257-
pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
258-
}
259-
pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1;
260-
pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
261-
pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE;
262248

263-
total = p4d_size + pud_size + pmd_size + pte_size;
249+
/* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */
250+
if (PTRS_PER_P4D > 1)
251+
entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D;
252+
entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD;
253+
entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD;
254+
entries += 2 * sizeof(pte_t) * PTRS_PER_PTE;
264255

265256
/*
266257
* Now calculate the added pagetable structures needed to populate
267258
* the new pagetables.
268259
*/
269-
if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
270-
p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
271-
p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
272-
pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE;
273-
pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
274-
} else {
275-
p4d_size = 0;
276-
pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
277-
pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
278-
}
279-
pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE;
280-
pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
281260

282-
total += p4d_size + pud_size + pmd_size;
261+
if (PTRS_PER_P4D > 1)
262+
tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D;
263+
tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD;
264+
tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD;
283265

284-
return total;
266+
return entries + tables;
285267
}
286268

287269
void __init __nostackprotector sme_encrypt_kernel(struct boot_params *bp)

0 commit comments

Comments
 (0)