From: Evgeniy Baskov <baskov@ispras.ru>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>, Andy Lutomirski <luto@kernel.org>,
Dave Hansen <dave.hansen@linux.intel.com>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Alexey Khoroshilov <khoroshilov@ispras.ru>,
Peter Jones <pjones@redhat.com>,
"Limonciello, Mario" <mario.limonciello@amd.com>,
joeyli <jlee@suse.com>,
lvc-project@linuxtesting.org, x86@kernel.org,
linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH v4 05/26] x86/boot: Support 4KB pages for identity mapping
Date: Wed, 08 Mar 2023 19:11:27 +0300 [thread overview]
Message-ID: <14c0426b5d777964cbdbc9d289482a93@ispras.ru> (raw)
In-Reply-To: <CAMj1kXFtyuktQmbhoHctikwyH-eJfE8d2nzs1-2csYy4+and4Q@mail.gmail.com>
On 2023-03-08 12:42, Ard Biesheuvel wrote:
> On Thu, 15 Dec 2022 at 13:38, Evgeniy Baskov <baskov@ispras.ru> wrote:
>>
>> Current identity mapping code only supports 2M and 1G pages.
>> 4KB pages are desirable for better memory protection granularity
>> in compressed kernel code.
>>
>> Change identity mapping code to support 4KB pages and
>> memory remapping with different attributes.
>>
>> Tested-by: Mario Limonciello <mario.limonciello@amd.com>
>> Tested-by: Peter Jones <pjones@redhat.com>
>> Signed-off-by: Evgeniy Baskov <baskov@ispras.ru>
>
> This patch triggers an error reported by the build bots:
>
> arch/x86/mm/ident_map.c:19:8: warning: no previous prototype for
> 'ident_split_large_pmd'
Thanks! I'll fix them (and all of the others from the bot emails)
>
>
>> ---
>> arch/x86/include/asm/init.h | 1 +
>> arch/x86/mm/ident_map.c | 185
>> +++++++++++++++++++++++++++++-------
>> 2 files changed, 154 insertions(+), 32 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/init.h b/arch/x86/include/asm/init.h
>> index 5f1d3c421f68..a8277ee82c51 100644
>> --- a/arch/x86/include/asm/init.h
>> +++ b/arch/x86/include/asm/init.h
>> @@ -8,6 +8,7 @@ struct x86_mapping_info {
>> unsigned long page_flag; /* page flag for PMD or PUD
>> entry */
>> unsigned long offset; /* ident mapping offset */
>> bool direct_gbpages; /* PUD level 1GB page support
>> */
>> + bool allow_4kpages; /* Allow more granular
>> mappings with 4K pages */
>> unsigned long kernpg_flag; /* kernel pagetable flag
>> override */
>> };
>>
>> diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c
>> index 968d7005f4a7..662e794a325d 100644
>> --- a/arch/x86/mm/ident_map.c
>> +++ b/arch/x86/mm/ident_map.c
>> @@ -4,24 +4,127 @@
>> * included by both the compressed kernel and the regular kernel.
>> */
>>
>> -static void ident_pmd_init(struct x86_mapping_info *info, pmd_t
>> *pmd_page,
>> - unsigned long addr, unsigned long end)
>> +static void ident_pte_init(struct x86_mapping_info *info, pte_t
>> *pte_page,
>> + unsigned long addr, unsigned long end,
>> + unsigned long flags)
>> {
>> - addr &= PMD_MASK;
>> - for (; addr < end; addr += PMD_SIZE) {
>> + addr &= PAGE_MASK;
>> + for (; addr < end; addr += PAGE_SIZE) {
>> + pte_t *pte = pte_page + pte_index(addr);
>> +
>> + set_pte(pte, __pte((addr - info->offset) | flags));
>> + }
>> +}
>> +
>> +pte_t *ident_split_large_pmd(struct x86_mapping_info *info,
>> + pmd_t *pmdp, unsigned long page_addr)
>> +{
>> + unsigned long pmd_addr, page_flags;
>> + pte_t *pte;
>> +
>> + pte = (pte_t *)info->alloc_pgt_page(info->context);
>> + if (!pte)
>> + return NULL;
>> +
>> + pmd_addr = page_addr & PMD_MASK;
>> +
>> + /* Not a large page - clear PSE flag */
>> + page_flags = pmd_flags(*pmdp) & ~_PSE;
>> + ident_pte_init(info, pte, pmd_addr, pmd_addr + PMD_SIZE,
>> page_flags);
>> +
>> + return pte;
>> +}
>> +
>> +static int ident_pmd_init(struct x86_mapping_info *info, pmd_t
>> *pmd_page,
>> + unsigned long addr, unsigned long end,
>> + unsigned long flags)
>> +{
>> + unsigned long next;
>> + bool new_table = 0;
>> +
>> + for (; addr < end; addr = next) {
>> pmd_t *pmd = pmd_page + pmd_index(addr);
>> + pte_t *pte;
>>
>> - if (pmd_present(*pmd))
>> + next = (addr & PMD_MASK) + PMD_SIZE;
>> + if (next > end)
>> + next = end;
>> +
>> + /*
>> + * Use 2M pages if 4k pages are not allowed or
>> + * we are not mapping extra, i.e. address and size are
>> aligned.
>> + */
>> +
>> + if (!info->allow_4kpages ||
>> + (!(addr & ~PMD_MASK) && next == addr + PMD_SIZE))
>> {
>> +
>> + pmd_t pmdval;
>> +
>> + addr &= PMD_MASK;
>> + pmdval = __pmd((addr - info->offset) | flags |
>> _PSE);
>> + set_pmd(pmd, pmdval);
>> continue;
>> + }
>> +
>> + /*
>> + * If currently mapped page is large, we need to split
>> it.
>> + * The case when we don't can remap 2M page to 2M page
>> + * with different flags is already covered above.
>> + *
>> + * If there's nothing mapped to desired address,
>> + * we need to allocate new page table.
>> + */
>>
>> - set_pmd(pmd, __pmd((addr - info->offset) |
>> info->page_flag));
>> + if (pmd_large(*pmd)) {
>> + pte = ident_split_large_pmd(info, pmd, addr);
>> + new_table = 1;
>> + } else if (!pmd_present(*pmd)) {
>> + pte = (pte_t
>> *)info->alloc_pgt_page(info->context);
>> + new_table = 1;
>> + } else {
>> + pte = pte_offset_kernel(pmd, 0);
>> + new_table = 0;
>> + }
>> +
>> + if (!pte)
>> + return -ENOMEM;
>> +
>> + ident_pte_init(info, pte, addr, next, flags);
>> +
>> + if (new_table)
>> + set_pmd(pmd, __pmd(__pa(pte) |
>> info->kernpg_flag));
>> }
>> +
>> + return 0;
>> }
>>
>> +
>> +pmd_t *ident_split_large_pud(struct x86_mapping_info *info,
>> + pud_t *pudp, unsigned long page_addr)
>> +{
>> + unsigned long pud_addr, page_flags;
>> + pmd_t *pmd;
>> +
>> + pmd = (pmd_t *)info->alloc_pgt_page(info->context);
>> + if (!pmd)
>> + return NULL;
>> +
>> + pud_addr = page_addr & PUD_MASK;
>> +
>> + /* Not a large page - clear PSE flag */
>> + page_flags = pud_flags(*pudp) & ~_PSE;
>> + ident_pmd_init(info, pmd, pud_addr, pud_addr + PUD_SIZE,
>> page_flags);
>> +
>> + return pmd;
>> +}
>> +
>> +
>> static int ident_pud_init(struct x86_mapping_info *info, pud_t
>> *pud_page,
>> unsigned long addr, unsigned long end)
>> {
>> unsigned long next;
>> + bool new_table = 0;
>> + int result;
>>
>> for (; addr < end; addr = next) {
>> pud_t *pud = pud_page + pud_index(addr);
>> @@ -31,28 +134,39 @@ static int ident_pud_init(struct x86_mapping_info
>> *info, pud_t *pud_page,
>> if (next > end)
>> next = end;
>>
>> + /* Use 1G pages only if forced, even if they are
>> supported. */
>> if (info->direct_gbpages) {
>> pud_t pudval;
>> -
>> - if (pud_present(*pud))
>> - continue;
>> + unsigned long flags;
>>
>> addr &= PUD_MASK;
>> - pudval = __pud((addr - info->offset) |
>> info->page_flag);
>> + flags = info->page_flag | _PSE;
>> + pudval = __pud((addr - info->offset) | flags);
>> +
>> set_pud(pud, pudval);
>> continue;
>> }
>>
>> - if (pud_present(*pud)) {
>> + if (pud_large(*pud)) {
>> + pmd = ident_split_large_pud(info, pud, addr);
>> + new_table = 1;
>> + } else if (!pud_present(*pud)) {
>> + pmd = (pmd_t
>> *)info->alloc_pgt_page(info->context);
>> + new_table = 1;
>> + } else {
>> pmd = pmd_offset(pud, 0);
>> - ident_pmd_init(info, pmd, addr, next);
>> - continue;
>> + new_table = 0;
>> }
>> - pmd = (pmd_t *)info->alloc_pgt_page(info->context);
>> +
>> if (!pmd)
>> return -ENOMEM;
>> - ident_pmd_init(info, pmd, addr, next);
>> - set_pud(pud, __pud(__pa(pmd) | info->kernpg_flag));
>> +
>> + result = ident_pmd_init(info, pmd, addr, next,
>> info->page_flag);
>> + if (result)
>> + return result;
>> +
>> + if (new_table)
>> + set_pud(pud, __pud(__pa(pmd) |
>> info->kernpg_flag));
>> }
>>
>> return 0;
>> @@ -63,6 +177,7 @@ static int ident_p4d_init(struct x86_mapping_info
>> *info, p4d_t *p4d_page,
>> {
>> unsigned long next;
>> int result;
>> + bool new_table = 0;
>>
>> for (; addr < end; addr = next) {
>> p4d_t *p4d = p4d_page + p4d_index(addr);
>> @@ -72,15 +187,14 @@ static int ident_p4d_init(struct x86_mapping_info
>> *info, p4d_t *p4d_page,
>> if (next > end)
>> next = end;
>>
>> - if (p4d_present(*p4d)) {
>> + if (!p4d_present(*p4d)) {
>> + pud = (pud_t
>> *)info->alloc_pgt_page(info->context);
>> + new_table = 1;
>> + } else {
>> pud = pud_offset(p4d, 0);
>> - result = ident_pud_init(info, pud, addr,
>> next);
>> - if (result)
>> - return result;
>> -
>> - continue;
>> + new_table = 0;
>> }
>> - pud = (pud_t *)info->alloc_pgt_page(info->context);
>> +
>> if (!pud)
>> return -ENOMEM;
>>
>> @@ -88,19 +202,22 @@ static int ident_p4d_init(struct x86_mapping_info
>> *info, p4d_t *p4d_page,
>> if (result)
>> return result;
>>
>> - set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag));
>> + if (new_table)
>> + set_p4d(p4d, __p4d(__pa(pud) |
>> info->kernpg_flag));
>> }
>>
>> return 0;
>> }
>>
>> -int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t
>> *pgd_page,
>> - unsigned long pstart, unsigned long
>> pend)
>> +int kernel_ident_mapping_init(struct x86_mapping_info *info,
>> + pgd_t *pgd_page, unsigned long pstart,
>> + unsigned long pend)
>> {
>> unsigned long addr = pstart + info->offset;
>> unsigned long end = pend + info->offset;
>> unsigned long next;
>> int result;
>> + bool new_table;
>>
>> /* Set the default pagetable flags if not supplied */
>> if (!info->kernpg_flag)
>> @@ -117,20 +234,24 @@ int kernel_ident_mapping_init(struct
>> x86_mapping_info *info, pgd_t *pgd_page,
>> if (next > end)
>> next = end;
>>
>> - if (pgd_present(*pgd)) {
>> + if (!pgd_present(*pgd)) {
>> + p4d = (p4d_t
>> *)info->alloc_pgt_page(info->context);
>> + new_table = 1;
>> + } else {
>> p4d = p4d_offset(pgd, 0);
>> - result = ident_p4d_init(info, p4d, addr,
>> next);
>> - if (result)
>> - return result;
>> - continue;
>> + new_table = 0;
>> }
>>
>> - p4d = (p4d_t *)info->alloc_pgt_page(info->context);
>> if (!p4d)
>> return -ENOMEM;
>> +
>> result = ident_p4d_init(info, p4d, addr, next);
>> if (result)
>> return result;
>> +
>> + if (!new_table)
>> + continue;
>> +
>> if (pgtable_l5_enabled()) {
>> set_pgd(pgd, __pgd(__pa(p4d) |
>> info->kernpg_flag));
>> } else {
>> --
>> 2.37.4
>>
next prev parent reply other threads:[~2023-03-08 16:12 UTC|newest]
Thread overview: 78+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-15 12:37 [PATCH v4 00/26] x86_64: Improvements at compressed kernel stage Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 01/26] x86/boot: Align vmlinuz sections on page size Evgeniy Baskov
2023-03-10 14:43 ` Ard Biesheuvel
2023-03-11 14:30 ` Evgeniy Baskov
2023-03-11 14:42 ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 02/26] x86/build: Remove RWX sections and align on 4KB Evgeniy Baskov
2023-03-10 14:45 ` Ard Biesheuvel
2023-03-11 14:31 ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 03/26] x86/boot: Set cr0 to known state in trampoline Evgeniy Baskov
2023-03-10 14:48 ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 04/26] x86/boot: Increase boot page table size Evgeniy Baskov
2023-03-08 9:24 ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 05/26] x86/boot: Support 4KB pages for identity mapping Evgeniy Baskov
2023-03-08 9:42 ` Ard Biesheuvel
2023-03-08 16:11 ` Evgeniy Baskov [this message]
2022-12-15 12:37 ` [PATCH v4 06/26] x86/boot: Setup memory protection for bzImage code Evgeniy Baskov
2023-03-08 10:47 ` Ard Biesheuvel
2023-03-08 16:15 ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 07/26] x86/build: Check W^X of vmlinux during build Evgeniy Baskov
2023-03-08 9:34 ` Ard Biesheuvel
2023-03-08 16:05 ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 08/26] x86/boot: Map memory explicitly Evgeniy Baskov
2023-03-08 9:38 ` Ard Biesheuvel
2023-03-08 10:28 ` Ard Biesheuvel
2023-03-08 16:09 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 09/26] x86/boot: Remove mapping from page fault handler Evgeniy Baskov
2023-03-10 14:49 ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 10/26] efi/libstub: Move helper function to related file Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 11/26] x86/boot: Make console interface more abstract Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 12/26] x86/boot: Make kernel_add_identity_map() a pointer Evgeniy Baskov
2023-03-10 14:52 ` Ard Biesheuvel
2023-03-11 14:34 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 13/26] x86/boot: Split trampoline and pt init code Evgeniy Baskov
2023-03-10 14:56 ` Ard Biesheuvel
2023-03-11 14:37 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 14/26] x86/boot: Add EFI kernel extraction interface Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 15/26] efi/x86: Support extracting kernel from libstub Evgeniy Baskov
2023-03-09 16:00 ` Ard Biesheuvel
2023-03-09 17:05 ` Evgeniy Baskov
2023-03-09 16:49 ` Ard Biesheuvel
2023-03-09 17:10 ` Evgeniy Baskov
2023-03-09 17:11 ` Ard Biesheuvel
2023-03-10 15:08 ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 16/26] x86/boot: Reduce lower limit of physical KASLR Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 17/26] x86/boot: Reduce size of the DOS stub Evgeniy Baskov
2023-03-10 14:59 ` Ard Biesheuvel
2023-03-11 14:49 ` Evgeniy Baskov
2023-03-11 17:27 ` Ard Biesheuvel
2023-03-12 12:10 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 18/26] tools/include: Add simplified version of pe.h Evgeniy Baskov
2023-03-10 15:01 ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 19/26] x86/build: Cleanup tools/build.c Evgeniy Baskov
2023-03-09 15:57 ` Ard Biesheuvel
2023-03-09 16:25 ` Evgeniy Baskov
2023-03-09 16:50 ` Ard Biesheuvel
2023-03-09 17:22 ` Evgeniy Baskov
2023-03-09 17:37 ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 20/26] x86/build: Make generated PE more spec compliant Evgeniy Baskov
2023-03-10 15:17 ` Ard Biesheuvel
2023-03-11 15:02 ` Evgeniy Baskov
2023-03-11 17:31 ` Ard Biesheuvel
2023-03-12 12:01 ` Evgeniy Baskov
2023-03-12 13:09 ` Ard Biesheuvel
2023-03-13 9:11 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 21/26] efi/x86: Explicitly set sections memory attributes Evgeniy Baskov
2023-03-10 15:20 ` Ard Biesheuvel
2023-03-11 15:09 ` Evgeniy Baskov
2023-03-11 17:39 ` Ard Biesheuvel
2023-03-12 12:10 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 22/26] efi/libstub: Add memory attribute protocol definitions Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 23/26] efi/libstub: Use memory attribute protocol Evgeniy Baskov
2023-03-10 16:13 ` Ard Biesheuvel
2023-03-11 15:14 ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 24/26] efi/libstub: make memory protection warnings include newlines Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 25/26] efi/x86: don't try to set page attributes on 0-sized regions Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 26/26] efi/x86: don't set unsupported memory attributes Evgeniy Baskov
2022-12-15 19:21 ` [PATCH v4 00/26] x86_64: Improvements at compressed kernel stage Peter Jones
2022-12-19 14:08 ` Evgeniy Baskov
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=14c0426b5d777964cbdbc9d289482a93@ispras.ru \
--to=baskov@ispras.ru \
--cc=ardb@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=jlee@suse.com \
--cc=khoroshilov@ispras.ru \
--cc=linux-efi@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=lvc-project@linuxtesting.org \
--cc=mario.limonciello@amd.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=pjones@redhat.com \
--cc=tglx@linutronix.de \
--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;
as well as URLs for NNTP newsgroup(s).