from hotplugged memory rather than only from main memory. Series "implement "memmap on memory" feature on s390". - More folio conversions from Matthew Wilcox in the series "Convert memcontrol charge moving to use folios" "mm: convert mm counter to take a folio" - Chengming Zhou has optimized zswap's rbtree locking, providing significant reductions in system time and modest but measurable reductions in overall runtimes. The series is "mm/zswap: optimize the scalability of zswap rb-tree". - Chengming Zhou has also provided the series "mm/zswap: optimize zswap lru list" which provides measurable runtime benefits in some swap-intensive situations. - And Chengming Zhou further optimizes zswap in the series "mm/zswap: optimize for dynamic zswap_pools". Measured improvements are modest. - zswap cleanups and simplifications from Yosry Ahmed in the series "mm: zswap: simplify zswap_swapoff()". - In the series "Add DAX ABI for memmap_on_memory", Vishal Verma has contributed several DAX cleanups as well as adding a sysfs tunable to control the memmap_on_memory setting when the dax device is hotplugged as system memory. - Johannes Weiner has added the large series "mm: zswap: cleanups", which does that. - More DAMON work from SeongJae Park in the series "mm/damon: make DAMON debugfs interface deprecation unignorable" "selftests/damon: add more tests for core functionalities and corner cases" "Docs/mm/damon: misc readability improvements" "mm/damon: let DAMOS feeds and tame/auto-tune itself" - In the series "mm/mempolicy: weighted interleave mempolicy and sysfs extension" Rakie Kim has developed a new mempolicy interleaving policy wherein we allocate memory across nodes in a weighted fashion rather than uniformly. This is beneficial in heterogeneous memory environments appearing with CXL. - Christophe Leroy has contributed some cleanup and consolidation work against the ARM pagetable dumping code in the series "mm: ptdump: Refactor CONFIG_DEBUG_WX and check_wx_pages debugfs attribute". - Luis Chamberlain has added some additional xarray selftesting in the series "test_xarray: advanced API multi-index tests". - Muhammad Usama Anjum has reworked the selftest code to make its human-readable output conform to the TAP ("Test Anything Protocol") format. Amongst other things, this opens up the use of third-party tools to parse and process out selftesting results. - Ryan Roberts has added fork()-time PTE batching of THP ptes in the series "mm/memory: optimize fork() with PTE-mapped THP". Mainly targeted at arm64, this significantly speeds up fork() when the process has a large number of pte-mapped folios. - David Hildenbrand also gets in on the THP pte batching game in his series "mm/memory: optimize unmap/zap with PTE-mapped THP". It implements batching during munmap() and other pte teardown situations. The microbenchmark improvements are nice. - And in the series "Transparent Contiguous PTEs for User Mappings" Ryan Roberts further utilizes arm's pte's contiguous bit ("contpte mappings"). Kernel build times on arm64 improved nicely. Ryan's series "Address some contpte nits" provides some followup work. - In the series "mm/hugetlb: Restore the reservation" Breno Leitao has fixed an obscure hugetlb race which was causing unnecessary page faults. He has also added a reproducer under the selftest code. - In the series "selftests/mm: Output cleanups for the compaction test", Mark Brown did what the title claims. - Kinsey Ho has added the series "mm/mglru: code cleanup and refactoring". - Even more zswap material from Nhat Pham. The series "fix and extend zswap kselftests" does as claimed. - In the series "Introduce cpu_dcache_is_aliasing() to fix DAX regression" Mathieu Desnoyers has cleaned up and fixed rather a mess in our handling of DAX on archiecctures which have virtually aliasing data caches. The arm architecture is the main beneficiary. - Lokesh Gidra's series "per-vma locks in userfaultfd" provides dramatic improvements in worst-case mmap_lock hold times during certain userfaultfd operations. - Some page_owner enhancements and maintenance work from Oscar Salvador in his series "page_owner: print stacks and their outstanding allocations" "page_owner: Fixup and cleanup" - Uladzislau Rezki has contributed some vmalloc scalability improvements in his series "Mitigate a vmap lock contention". It realizes a 12x improvement for a certain microbenchmark. - Some kexec/crash cleanup work from Baoquan He in the series "Split crash out from kexec and clean up related config items". - Some zsmalloc maintenance work from Chengming Zhou in the series "mm/zsmalloc: fix and optimize objects/page migration" "mm/zsmalloc: some cleanup for get/set_zspage_mapping()" - Zi Yan has taught the MM to perform compaction on folios larger than order=0. This a step along the path to implementaton of the merging of large anonymous folios. The series is named "Enable >0 order folio memory compaction". - Christoph Hellwig has done quite a lot of cleanup work in the pagecache writeback code in his series "convert write_cache_pages() to an iterator". - Some modest hugetlb cleanups and speedups in Vishal Moola's series "Handle hugetlb faults under the VMA lock". - Zi Yan has changed the page splitting code so we can split huge pages into sizes other than order-0 to better utilize large folios. The series is named "Split a folio to any lower order folios". - David Hildenbrand has contributed the series "mm: remove total_mapcount()", a cleanup. - Matthew Wilcox has sought to improve the performance of bulk memory freeing in his series "Rearrange batched folio freeing". - Gang Li's series "hugetlb: parallelize hugetlb page init on boot" provides large improvements in bootup times on large machines which are configured to use large numbers of hugetlb pages. - Matthew Wilcox's series "PageFlags cleanups" does that. - Qi Zheng's series "minor fixes and supplement for ptdesc" does that also. S390 is affected. - Cleanups to our pagemap utility functions from Peter Xu in his series "mm/treewide: Replace pXd_large() with pXd_leaf()". - Nico Pache has fixed a few things with our hugepage selftests in his series "selftests/mm: Improve Hugepage Test Handling in MM Selftests". - Also, of course, many singleton patches to many things. Please see the individual changelogs for details. -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCZfJpPQAKCRDdBJ7gKXxA joxeAP9TrcMEuHnLmBlhIXkWbIR4+ki+pA3v+gNTlJiBhnfVSgD9G55t1aBaRplx TMNhHfyiHYDTx/GAV9NXW84tasJSDgA= =TG55 -----END PGP SIGNATURE----- Merge tag 'mm-stable-2024-03-13-20-04' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: - Sumanth Korikkar has taught s390 to allocate hotplug-time page frames from hotplugged memory rather than only from main memory. Series "implement "memmap on memory" feature on s390". - More folio conversions from Matthew Wilcox in the series "Convert memcontrol charge moving to use folios" "mm: convert mm counter to take a folio" - Chengming Zhou has optimized zswap's rbtree locking, providing significant reductions in system time and modest but measurable reductions in overall runtimes. The series is "mm/zswap: optimize the scalability of zswap rb-tree". - Chengming Zhou has also provided the series "mm/zswap: optimize zswap lru list" which provides measurable runtime benefits in some swap-intensive situations. - And Chengming Zhou further optimizes zswap in the series "mm/zswap: optimize for dynamic zswap_pools". Measured improvements are modest. - zswap cleanups and simplifications from Yosry Ahmed in the series "mm: zswap: simplify zswap_swapoff()". - In the series "Add DAX ABI for memmap_on_memory", Vishal Verma has contributed several DAX cleanups as well as adding a sysfs tunable to control the memmap_on_memory setting when the dax device is hotplugged as system memory. - Johannes Weiner has added the large series "mm: zswap: cleanups", which does that. - More DAMON work from SeongJae Park in the series "mm/damon: make DAMON debugfs interface deprecation unignorable" "selftests/damon: add more tests for core functionalities and corner cases" "Docs/mm/damon: misc readability improvements" "mm/damon: let DAMOS feeds and tame/auto-tune itself" - In the series "mm/mempolicy: weighted interleave mempolicy and sysfs extension" Rakie Kim has developed a new mempolicy interleaving policy wherein we allocate memory across nodes in a weighted fashion rather than uniformly. This is beneficial in heterogeneous memory environments appearing with CXL. - Christophe Leroy has contributed some cleanup and consolidation work against the ARM pagetable dumping code in the series "mm: ptdump: Refactor CONFIG_DEBUG_WX and check_wx_pages debugfs attribute". - Luis Chamberlain has added some additional xarray selftesting in the series "test_xarray: advanced API multi-index tests". - Muhammad Usama Anjum has reworked the selftest code to make its human-readable output conform to the TAP ("Test Anything Protocol") format. Amongst other things, this opens up the use of third-party tools to parse and process out selftesting results. - Ryan Roberts has added fork()-time PTE batching of THP ptes in the series "mm/memory: optimize fork() with PTE-mapped THP". Mainly targeted at arm64, this significantly speeds up fork() when the process has a large number of pte-mapped folios. - David Hildenbrand also gets in on the THP pte batching game in his series "mm/memory: optimize unmap/zap with PTE-mapped THP". It implements batching during munmap() and other pte teardown situations. The microbenchmark improvements are nice. - And in the series "Transparent Contiguous PTEs for User Mappings" Ryan Roberts further utilizes arm's pte's contiguous bit ("contpte mappings"). Kernel build times on arm64 improved nicely. Ryan's series "Address some contpte nits" provides some followup work. - In the series "mm/hugetlb: Restore the reservation" Breno Leitao has fixed an obscure hugetlb race which was causing unnecessary page faults. He has also added a reproducer under the selftest code. - In the series "selftests/mm: Output cleanups for the compaction test", Mark Brown did what the title claims. - Kinsey Ho has added the series "mm/mglru: code cleanup and refactoring". - Even more zswap material from Nhat Pham. The series "fix and extend zswap kselftests" does as claimed. - In the series "Introduce cpu_dcache_is_aliasing() to fix DAX regression" Mathieu Desnoyers has cleaned up and fixed rather a mess in our handling of DAX on archiecctures which have virtually aliasing data caches. The arm architecture is the main beneficiary. - Lokesh Gidra's series "per-vma locks in userfaultfd" provides dramatic improvements in worst-case mmap_lock hold times during certain userfaultfd operations. - Some page_owner enhancements and maintenance work from Oscar Salvador in his series "page_owner: print stacks and their outstanding allocations" "page_owner: Fixup and cleanup" - Uladzislau Rezki has contributed some vmalloc scalability improvements in his series "Mitigate a vmap lock contention". It realizes a 12x improvement for a certain microbenchmark. - Some kexec/crash cleanup work from Baoquan He in the series "Split crash out from kexec and clean up related config items". - Some zsmalloc maintenance work from Chengming Zhou in the series "mm/zsmalloc: fix and optimize objects/page migration" "mm/zsmalloc: some cleanup for get/set_zspage_mapping()" - Zi Yan has taught the MM to perform compaction on folios larger than order=0. This a step along the path to implementaton of the merging of large anonymous folios. The series is named "Enable >0 order folio memory compaction". - Christoph Hellwig has done quite a lot of cleanup work in the pagecache writeback code in his series "convert write_cache_pages() to an iterator". - Some modest hugetlb cleanups and speedups in Vishal Moola's series "Handle hugetlb faults under the VMA lock". - Zi Yan has changed the page splitting code so we can split huge pages into sizes other than order-0 to better utilize large folios. The series is named "Split a folio to any lower order folios". - David Hildenbrand has contributed the series "mm: remove total_mapcount()", a cleanup. - Matthew Wilcox has sought to improve the performance of bulk memory freeing in his series "Rearrange batched folio freeing". - Gang Li's series "hugetlb: parallelize hugetlb page init on boot" provides large improvements in bootup times on large machines which are configured to use large numbers of hugetlb pages. - Matthew Wilcox's series "PageFlags cleanups" does that. - Qi Zheng's series "minor fixes and supplement for ptdesc" does that also. S390 is affected. - Cleanups to our pagemap utility functions from Peter Xu in his series "mm/treewide: Replace pXd_large() with pXd_leaf()". - Nico Pache has fixed a few things with our hugepage selftests in his series "selftests/mm: Improve Hugepage Test Handling in MM Selftests". - Also, of course, many singleton patches to many things. Please see the individual changelogs for details. * tag 'mm-stable-2024-03-13-20-04' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (435 commits) mm/zswap: remove the memcpy if acomp is not sleepable crypto: introduce: acomp_is_async to expose if comp drivers might sleep memtest: use {READ,WRITE}_ONCE in memory scanning mm: prohibit the last subpage from reusing the entire large folio mm: recover pud_leaf() definitions in nopmd case selftests/mm: skip the hugetlb-madvise tests on unmet hugepage requirements selftests/mm: skip uffd hugetlb tests with insufficient hugepages selftests/mm: dont fail testsuite due to a lack of hugepages mm/huge_memory: skip invalid debugfs new_order input for folio split mm/huge_memory: check new folio order when split a folio mm, vmscan: retry kswapd's priority loop with cache_trim_mode off on failure mm: add an explicit smp_wmb() to UFFDIO_CONTINUE mm: fix list corruption in put_pages_list mm: remove folio from deferred split list before uncharging it filemap: avoid unnecessary major faults in filemap_fault() mm,page_owner: drop unnecessary check mm,page_owner: check for null stack_record before bumping its refcount mm: swap: fix race between free_swap_and_cache() and swapoff() mm/treewide: align up pXd_leaf() retval across archs mm/treewide: drop pXd_large() ...
393 lines
11 KiB
C
393 lines
11 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* This code is used on x86_64 to create page table identity mappings on
|
|
* demand by building up a new set of page tables (or appending to the
|
|
* existing ones), and then switching over to them when ready.
|
|
*
|
|
* Copyright (C) 2015-2016 Yinghai Lu
|
|
* Copyright (C) 2016 Kees Cook
|
|
*/
|
|
|
|
/* No MITIGATION_PAGE_TABLE_ISOLATION support needed either: */
|
|
#undef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
|
|
|
|
#include "error.h"
|
|
#include "misc.h"
|
|
|
|
/* These actually do the work of building the kernel identity maps. */
|
|
#include <linux/pgtable.h>
|
|
#include <asm/cmpxchg.h>
|
|
#include <asm/trap_pf.h>
|
|
#include <asm/trapnr.h>
|
|
#include <asm/init.h>
|
|
/* Use the static base for this part of the boot process */
|
|
#undef __PAGE_OFFSET
|
|
#define __PAGE_OFFSET __PAGE_OFFSET_BASE
|
|
#include "../../mm/ident_map.c"
|
|
|
|
#define _SETUP
|
|
#include <asm/setup.h> /* For COMMAND_LINE_SIZE */
|
|
#undef _SETUP
|
|
|
|
extern unsigned long get_cmd_line_ptr(void);
|
|
|
|
/* Used by PAGE_KERN* macros: */
|
|
pteval_t __default_kernel_pte_mask __read_mostly = ~0;
|
|
|
|
/* Used to track our page table allocation area. */
|
|
struct alloc_pgt_data {
|
|
unsigned char *pgt_buf;
|
|
unsigned long pgt_buf_size;
|
|
unsigned long pgt_buf_offset;
|
|
};
|
|
|
|
/*
|
|
* Allocates space for a page table entry, using struct alloc_pgt_data
|
|
* above. Besides the local callers, this is used as the allocation
|
|
* callback in mapping_info below.
|
|
*/
|
|
static void *alloc_pgt_page(void *context)
|
|
{
|
|
struct alloc_pgt_data *pages = (struct alloc_pgt_data *)context;
|
|
unsigned char *entry;
|
|
|
|
/* Validate there is space available for a new page. */
|
|
if (pages->pgt_buf_offset >= pages->pgt_buf_size) {
|
|
debug_putstr("out of pgt_buf in " __FILE__ "!?\n");
|
|
debug_putaddr(pages->pgt_buf_offset);
|
|
debug_putaddr(pages->pgt_buf_size);
|
|
return NULL;
|
|
}
|
|
|
|
/* Consumed more tables than expected? */
|
|
if (pages->pgt_buf_offset == BOOT_PGT_SIZE_WARN) {
|
|
debug_putstr("pgt_buf running low in " __FILE__ "\n");
|
|
debug_putstr("Need to raise BOOT_PGT_SIZE?\n");
|
|
debug_putaddr(pages->pgt_buf_offset);
|
|
debug_putaddr(pages->pgt_buf_size);
|
|
}
|
|
|
|
entry = pages->pgt_buf + pages->pgt_buf_offset;
|
|
pages->pgt_buf_offset += PAGE_SIZE;
|
|
|
|
return entry;
|
|
}
|
|
|
|
/* Used to track our allocated page tables. */
|
|
static struct alloc_pgt_data pgt_data;
|
|
|
|
/* The top level page table entry pointer. */
|
|
static unsigned long top_level_pgt;
|
|
|
|
phys_addr_t physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
|
|
|
|
/*
|
|
* Mapping information structure passed to kernel_ident_mapping_init().
|
|
* Due to relocation, pointers must be assigned at run time not build time.
|
|
*/
|
|
static struct x86_mapping_info mapping_info;
|
|
|
|
/*
|
|
* Adds the specified range to the identity mappings.
|
|
*/
|
|
void kernel_add_identity_map(unsigned long start, unsigned long end)
|
|
{
|
|
int ret;
|
|
|
|
/* Align boundary to 2M. */
|
|
start = round_down(start, PMD_SIZE);
|
|
end = round_up(end, PMD_SIZE);
|
|
if (start >= end)
|
|
return;
|
|
|
|
/* Build the mapping. */
|
|
ret = kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt, start, end);
|
|
if (ret)
|
|
error("Error: kernel_ident_mapping_init() failed\n");
|
|
}
|
|
|
|
/* Locates and clears a region for a new top level page table. */
|
|
void initialize_identity_maps(void *rmode)
|
|
{
|
|
unsigned long cmdline;
|
|
struct setup_data *sd;
|
|
|
|
/* Exclude the encryption mask from __PHYSICAL_MASK */
|
|
physical_mask &= ~sme_me_mask;
|
|
|
|
/* Init mapping_info with run-time function/buffer pointers. */
|
|
mapping_info.alloc_pgt_page = alloc_pgt_page;
|
|
mapping_info.context = &pgt_data;
|
|
mapping_info.page_flag = __PAGE_KERNEL_LARGE_EXEC | sme_me_mask;
|
|
mapping_info.kernpg_flag = _KERNPG_TABLE;
|
|
|
|
/*
|
|
* It should be impossible for this not to already be true,
|
|
* but since calling this a second time would rewind the other
|
|
* counters, let's just make sure this is reset too.
|
|
*/
|
|
pgt_data.pgt_buf_offset = 0;
|
|
|
|
/*
|
|
* If we came here via startup_32(), cr3 will be _pgtable already
|
|
* and we must append to the existing area instead of entirely
|
|
* overwriting it.
|
|
*
|
|
* With 5-level paging, we use '_pgtable' to allocate the p4d page table,
|
|
* the top-level page table is allocated separately.
|
|
*
|
|
* p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
|
|
* cases. On 4-level paging it's equal to 'top_level_pgt'.
|
|
*/
|
|
top_level_pgt = read_cr3_pa();
|
|
if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
|
|
pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
|
|
pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
|
|
memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
|
|
} else {
|
|
pgt_data.pgt_buf = _pgtable;
|
|
pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
|
|
memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
|
|
top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
|
|
}
|
|
|
|
/*
|
|
* New page-table is set up - map the kernel image, boot_params and the
|
|
* command line. The uncompressed kernel requires boot_params and the
|
|
* command line to be mapped in the identity mapping. Map them
|
|
* explicitly here in case the compressed kernel does not touch them,
|
|
* or does not touch all the pages covering them.
|
|
*/
|
|
kernel_add_identity_map((unsigned long)_head, (unsigned long)_end);
|
|
boot_params_ptr = rmode;
|
|
kernel_add_identity_map((unsigned long)boot_params_ptr,
|
|
(unsigned long)(boot_params_ptr + 1));
|
|
cmdline = get_cmd_line_ptr();
|
|
kernel_add_identity_map(cmdline, cmdline + COMMAND_LINE_SIZE);
|
|
|
|
/*
|
|
* Also map the setup_data entries passed via boot_params in case they
|
|
* need to be accessed by uncompressed kernel via the identity mapping.
|
|
*/
|
|
sd = (struct setup_data *)boot_params_ptr->hdr.setup_data;
|
|
while (sd) {
|
|
unsigned long sd_addr = (unsigned long)sd;
|
|
|
|
kernel_add_identity_map(sd_addr, sd_addr + sizeof(*sd) + sd->len);
|
|
sd = (struct setup_data *)sd->next;
|
|
}
|
|
|
|
sev_prep_identity_maps(top_level_pgt);
|
|
|
|
/* Load the new page-table. */
|
|
write_cr3(top_level_pgt);
|
|
|
|
/*
|
|
* Now that the required page table mappings are established and a
|
|
* GHCB can be used, check for SNP guest/HV feature compatibility.
|
|
*/
|
|
snp_check_features();
|
|
}
|
|
|
|
static pte_t *split_large_pmd(struct x86_mapping_info *info,
|
|
pmd_t *pmdp, unsigned long __address)
|
|
{
|
|
unsigned long page_flags;
|
|
unsigned long address;
|
|
pte_t *pte;
|
|
pmd_t pmd;
|
|
int i;
|
|
|
|
pte = (pte_t *)info->alloc_pgt_page(info->context);
|
|
if (!pte)
|
|
return NULL;
|
|
|
|
address = __address & PMD_MASK;
|
|
/* No large page - clear PSE flag */
|
|
page_flags = info->page_flag & ~_PAGE_PSE;
|
|
|
|
/* Populate the PTEs */
|
|
for (i = 0; i < PTRS_PER_PMD; i++) {
|
|
set_pte(&pte[i], __pte(address | page_flags));
|
|
address += PAGE_SIZE;
|
|
}
|
|
|
|
/*
|
|
* Ideally we need to clear the large PMD first and do a TLB
|
|
* flush before we write the new PMD. But the 2M range of the
|
|
* PMD might contain the code we execute and/or the stack
|
|
* we are on, so we can't do that. But that should be safe here
|
|
* because we are going from large to small mappings and we are
|
|
* also the only user of the page-table, so there is no chance
|
|
* of a TLB multihit.
|
|
*/
|
|
pmd = __pmd((unsigned long)pte | info->kernpg_flag);
|
|
set_pmd(pmdp, pmd);
|
|
/* Flush TLB to establish the new PMD */
|
|
write_cr3(top_level_pgt);
|
|
|
|
return pte + pte_index(__address);
|
|
}
|
|
|
|
static void clflush_page(unsigned long address)
|
|
{
|
|
unsigned int flush_size;
|
|
char *cl, *start, *end;
|
|
|
|
/*
|
|
* Hardcode cl-size to 64 - CPUID can't be used here because that might
|
|
* cause another #VC exception and the GHCB is not ready to use yet.
|
|
*/
|
|
flush_size = 64;
|
|
start = (char *)(address & PAGE_MASK);
|
|
end = start + PAGE_SIZE;
|
|
|
|
/*
|
|
* First make sure there are no pending writes on the cache-lines to
|
|
* flush.
|
|
*/
|
|
asm volatile("mfence" : : : "memory");
|
|
|
|
for (cl = start; cl != end; cl += flush_size)
|
|
clflush(cl);
|
|
}
|
|
|
|
static int set_clr_page_flags(struct x86_mapping_info *info,
|
|
unsigned long address,
|
|
pteval_t set, pteval_t clr)
|
|
{
|
|
pgd_t *pgdp = (pgd_t *)top_level_pgt;
|
|
p4d_t *p4dp;
|
|
pud_t *pudp;
|
|
pmd_t *pmdp;
|
|
pte_t *ptep, pte;
|
|
|
|
/*
|
|
* First make sure there is a PMD mapping for 'address'.
|
|
* It should already exist, but keep things generic.
|
|
*
|
|
* To map the page just read from it and fault it in if there is no
|
|
* mapping yet. kernel_add_identity_map() can't be called here because
|
|
* that would unconditionally map the address on PMD level, destroying
|
|
* any PTE-level mappings that might already exist. Use assembly here
|
|
* so the access won't be optimized away.
|
|
*/
|
|
asm volatile("mov %[address], %%r9"
|
|
:: [address] "g" (*(unsigned long *)address)
|
|
: "r9", "memory");
|
|
|
|
/*
|
|
* The page is mapped at least with PMD size - so skip checks and walk
|
|
* directly to the PMD.
|
|
*/
|
|
p4dp = p4d_offset(pgdp, address);
|
|
pudp = pud_offset(p4dp, address);
|
|
pmdp = pmd_offset(pudp, address);
|
|
|
|
if (pmd_leaf(*pmdp))
|
|
ptep = split_large_pmd(info, pmdp, address);
|
|
else
|
|
ptep = pte_offset_kernel(pmdp, address);
|
|
|
|
if (!ptep)
|
|
return -ENOMEM;
|
|
|
|
/*
|
|
* Changing encryption attributes of a page requires to flush it from
|
|
* the caches.
|
|
*/
|
|
if ((set | clr) & _PAGE_ENC) {
|
|
clflush_page(address);
|
|
|
|
/*
|
|
* If the encryption attribute is being cleared, change the page state
|
|
* to shared in the RMP table.
|
|
*/
|
|
if (clr)
|
|
snp_set_page_shared(__pa(address & PAGE_MASK));
|
|
}
|
|
|
|
/* Update PTE */
|
|
pte = *ptep;
|
|
pte = pte_set_flags(pte, set);
|
|
pte = pte_clear_flags(pte, clr);
|
|
set_pte(ptep, pte);
|
|
|
|
/*
|
|
* If the encryption attribute is being set, then change the page state to
|
|
* private in the RMP entry. The page state change must be done after the PTE
|
|
* is updated.
|
|
*/
|
|
if (set & _PAGE_ENC)
|
|
snp_set_page_private(__pa(address & PAGE_MASK));
|
|
|
|
/* Flush TLB after changing encryption attribute */
|
|
write_cr3(top_level_pgt);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int set_page_decrypted(unsigned long address)
|
|
{
|
|
return set_clr_page_flags(&mapping_info, address, 0, _PAGE_ENC);
|
|
}
|
|
|
|
int set_page_encrypted(unsigned long address)
|
|
{
|
|
return set_clr_page_flags(&mapping_info, address, _PAGE_ENC, 0);
|
|
}
|
|
|
|
int set_page_non_present(unsigned long address)
|
|
{
|
|
return set_clr_page_flags(&mapping_info, address, 0, _PAGE_PRESENT);
|
|
}
|
|
|
|
static void do_pf_error(const char *msg, unsigned long error_code,
|
|
unsigned long address, unsigned long ip)
|
|
{
|
|
error_putstr(msg);
|
|
|
|
error_putstr("\nError Code: ");
|
|
error_puthex(error_code);
|
|
error_putstr("\nCR2: 0x");
|
|
error_puthex(address);
|
|
error_putstr("\nRIP relative to _head: 0x");
|
|
error_puthex(ip - (unsigned long)_head);
|
|
error_putstr("\n");
|
|
|
|
error("Stopping.\n");
|
|
}
|
|
|
|
void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)
|
|
{
|
|
unsigned long address = native_read_cr2();
|
|
unsigned long end;
|
|
bool ghcb_fault;
|
|
|
|
ghcb_fault = sev_es_check_ghcb_fault(address);
|
|
|
|
address &= PMD_MASK;
|
|
end = address + PMD_SIZE;
|
|
|
|
/*
|
|
* Check for unexpected error codes. Unexpected are:
|
|
* - Faults on present pages
|
|
* - User faults
|
|
* - Reserved bits set
|
|
*/
|
|
if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD))
|
|
do_pf_error("Unexpected page-fault:", error_code, address, regs->ip);
|
|
else if (ghcb_fault)
|
|
do_pf_error("Page-fault on GHCB page:", error_code, address, regs->ip);
|
|
|
|
/*
|
|
* Error code is sane - now identity map the 2M region around
|
|
* the faulting address.
|
|
*/
|
|
kernel_add_identity_map(address, end);
|
|
}
|
|
|
|
void do_boot_nmi_trap(struct pt_regs *regs, unsigned long error_code)
|
|
{
|
|
spurious_nmi_count++;
|
|
}
|