Transition memory to shared during a hot-remove operation so that it can be re-used by the hypervisor. During lazy acceptance, only memory that was used has been accepted, therefore during hot-remove only mark pages as shared that were previously accepted / made private. Signed-off-by: Pratik R. Sampat --- arch/x86/coco/sev/core.c | 23 +++++++++++++++ arch/x86/include/asm/sev.h | 2 ++ arch/x86/include/asm/unaccepted_memory.h | 9 ++++++ drivers/firmware/efi/unaccepted_memory.c | 37 ++++++++++++++++++++++++ include/linux/mm.h | 7 +++++ mm/memory_hotplug.c | 2 ++ 6 files changed, 80 insertions(+) diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index a5c9615a6e0c..c05fc91d10a1 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -621,6 +621,29 @@ void snp_accept_memory(phys_addr_t start, phys_addr_t end) set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE, 0); } +void snp_unaccept_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long vaddr, npages; + + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return; + + vaddr = (unsigned long)__va(start); + npages = (end - start) >> PAGE_SHIFT; + + /* + * Hotplugged memory can be set to shared externally. Attempting to + * re-share the memory (during hot-remove) will cause the pvalidate + * operation to not make any changes to the RMP table triggering the + * PVALIDATE_FAIL_NOUPDATE condition + * + * Since the memory hotplug case is unique, specify this intent so that + * if the page is part of hotplugged memory a pvalidate rescind + * operation is not performed + */ + set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED, SNP_PSC_SHARED_TO_SHARED); +} + int snp_extend_hotplug_memory_state_bitmap(phys_addr_t start, unsigned long size, uint64_t unit_size) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index eb605892645c..8f3c5b878fd7 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -547,6 +547,7 @@ void __noreturn snp_abort(void); void snp_dmi_setup(void); int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input); void snp_accept_memory(phys_addr_t start, phys_addr_t end); +void snp_unaccept_memory(phys_addr_t start, phys_addr_t end); u64 snp_get_unsupported_features(u64 status); u64 sev_get_status(void); void sev_show_status(void); @@ -639,6 +640,7 @@ static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, return -ENOTTY; } static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } +static inline void snp_unaccept_memory(phys_addr_t start, phys_addr_t end) { } static inline u64 snp_get_unsupported_features(u64 status) { return 0; } static inline u64 sev_get_status(void) { return 0; } static inline void sev_show_status(void) { } diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h index abdf5472de9e..ad392294b71b 100644 --- a/arch/x86/include/asm/unaccepted_memory.h +++ b/arch/x86/include/asm/unaccepted_memory.h @@ -18,6 +18,15 @@ static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) } } +static inline void arch_unaccept_memory(phys_addr_t start, phys_addr_t end) +{ + if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + snp_unaccept_memory(start, end); + } else { + panic("Cannot accept memory: unknown platform\n"); + } +} + static inline struct efi_unaccepted_memory *efi_get_unaccepted_table(void) { if (efi.unaccepted == EFI_INVALID_TABLE_ADDR) diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c index 6796042a64aa..662cf0d6715f 100644 --- a/drivers/firmware/efi/unaccepted_memory.c +++ b/drivers/firmware/efi/unaccepted_memory.c @@ -301,6 +301,43 @@ int accept_hotplug_memory(phys_addr_t mem_range_start, unsigned long mem_range_s return 0; } +void unaccept_hotplug_memory(phys_addr_t mem_range_start, unsigned long mem_range_size) +{ + u64 unit_size, phys_base, bit_start, bit_end, addr; + struct efi_unaccepted_memory *unacc_tbl; + unsigned long flags, *bitmap; + phys_addr_t start, end; + int i; + + unacc_tbl = efi_get_unaccepted_table(); + if (!unacc_tbl) + return; + + phys_base = unacc_tbl->phys_base; + unit_size = unacc_tbl->unit_size; + + start = mem_range_start - phys_base; + end = (mem_range_start + mem_range_size) - phys_base; + + bit_start = start / unit_size; + bit_end = end / unit_size; + + /* Only unaccept memory that was previously accepted in the range */ + for (i = bit_start; i < bit_end; i++) { + spin_lock_irqsave(&unaccepted_memory_lock, flags); + bitmap = efi_get_unaccepted_bitmap(); + if (!bitmap || test_bit(i, bitmap)) { + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); + continue; + } + + addr = phys_base + i * unit_size; + + arch_unaccept_memory(addr, addr + unit_size); + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); + } +} + #ifdef CONFIG_PROC_VMCORE static bool unaccepted_memory_vmcore_pfn_is_ram(struct vmcore_cb *cb, unsigned long pfn) diff --git a/include/linux/mm.h b/include/linux/mm.h index bb43876e6c47..34d48693dc86 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4079,6 +4079,8 @@ bool range_contains_unaccepted_memory(phys_addr_t start, unsigned long size); void accept_memory(phys_addr_t start, unsigned long size); int accept_hotplug_memory(phys_addr_t mem_range_start, unsigned long mem_range_size); +void unaccept_hotplug_memory(phys_addr_t mem_range_start, + unsigned long mem_range_size); bool mm_lazy_accept_enabled(void); #else @@ -4099,6 +4101,11 @@ static inline int accept_hotplug_memory(phys_addr_t mem_range_start, return 0; } +static inline void unaccept_hotplug_memory(phys_addr_t mem_range_start, + unsigned long mem_range_size) +{ +} + static inline bool mm_lazy_accept_enabled(void) { return false; } #endif diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index bf8086682b66..0b14b14e53fe 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -2254,6 +2254,8 @@ static int try_remove_memory(u64 start, u64 size) mem_hotplug_begin(); + unaccept_hotplug_memory(start, size); + rc = memory_blocks_have_altmaps(start, size); if (rc < 0) { mem_hotplug_done(); -- 2.51.1