KVM: s390: pv: implement lazy destroy for reboot
Subsystems (5)
Claudio Imbrenda
maintainerv7 cover KVM: s390: pv: implement lazy destroy for reboot
Previously, when a protected VM was rebooted or when it was shut down, its memory was made unprotected, and then the protected VM itself was destroyed. Looping over the whole address space can take some time, considering the overhead of the various Ultravisor Calls (UVCs). This means that a reboot or a shutdown would take a potentially long amount of time, depending on the amount of used memory.
This patchseries implements a deferred destroy mechanism for protected guests. When a protected guest is destroyed, its memory can be cleared in background, allowing the guest to restart or terminate significantly faster than before.
There are 2 possibilities when a protected VM is torn down:
* it still has an address space associated (reboot case)
* it does not have an address space anymore (shutdown case)
For the reboot case, two new commands are available for the KVM_S390_PV_COMMAND:
KVM_PV_ASYNC_DISABLE_PREPARE: prepares the current protected VM for asynchronous teardown. The current VM will then continue immediately as non-protected. If a protected VM had already been set aside without starting the teardown process, this call will fail. In this case the userspace process should issue a normal KVM_PV_DISABLE
KVM_PV_ASYNC_DISABLE: tears down the protected VM previously set aside for asychronous teardown. This PV command should ideally be issued by userspace from a separate thread. If a fatal signal is received (or the process terminates naturally), the command will terminate immediately without completing.
The idea is that userspace should first issue the KVM_PV_ASYNC_DISABLE_PREPARE command, and in case of success, create a new thread and issue KVM_PV_ASYNC_DISABLE from there. This also allows for proper accounting of the CPU time needed for the asynchronous teardown.
This means that the same address space can have memory belonging to more than one protected guest, although only one will be running, the others will in fact not even have any CPUs.
The shutdown case should be dealt with in userspace (e.g. using clone(CLONE_VM)).
A module parameter is also provided to disable the new functionality, which is otherwise enabled by default. This should not be an issue since the new functionality is opt-in anyway. This is mainly thought to aid debugging.
v6->v7
* moved INIT_LIST_HEAD inside spinlock in patch 1
* improved commit messages in patch 2
* added missing locks in patch 3
* added and expanded some comments in patch 11
* rebased
v5->v6
* completely reworked the series
* removed kernel thread for asynchronous teardown
* added new commands to KVM_S390_PV_COMMAND ioctl
v4->v5
* fixed and improved some patch descriptions
* added some comments to better explain what's going on
* use vma_lookup instead of find_vma
* rename is_protected to protected_count since now it's used as a counter
v3->v4
* added patch 2
* split patch 3
* removed the shutdown part -- will be a separate patchseries
* moved the patch introducing the module parameter
v2->v3
* added definitions for CC return codes for the UVC instruction
* improved make_secure_pte:
- renamed rc to cc
- added comments to explain why returning -EAGAIN is ok
* fixed kvm_s390_pv_replace_asce and kvm_s390_pv_remove_old_asce:
- renamed
- added locking
- moved to gmap.c
* do proper error management in do_secure_storage_access instead of trying again hoping to get a different exception
* fix outdated patch descriptions
v1->v2
* rebased on a more recent kernel
* improved/expanded some patch descriptions
* improves/expanded some comments
* added patch 1, which prevents stall notification when the system is under heavy load.
* rename some members of struct deferred_priv to improve readability
* avoid an use-after-free bug of the struct mm in case of shutdown
* add missing return when lazy destroy is disabled
* add support for OOM notifier
Claudio Imbrenda (17):
KVM: s390: pv: leak the topmost page table when destroy fails
KVM: s390: pv: handle secure storage violations for protected guests
KVM: s390: pv: handle secure storage exceptions for normal guests
KVM: s390: pv: refactor s390_reset_acc
KVM: s390: pv: usage counter instead of flag
KVM: s390: pv: add export before import
KVM: s390: pv: module parameter to fence lazy destroy
KVM: s390: pv: make kvm_s390_cpus_from_pv global
KVM: s390: pv: clear the state without memset
KVM: s390: pv: add mmu_notifier
s390/mm: KVM: pv: when tearing down, try to destroy protected pages
KVM: s390: pv: refactoring of kvm_s390_pv_deinit_vm
KVM: s390: pv: cleanup leftover protected VMs if needed
KVM: s390: pv: asynchronous destroy for reboot
KVM: s390: pv: api documentation for asynchronous destroy
KVM: s390: pv: add KVM_CAP_S390_PROT_REBOOT_ASYNC
KVM: s390: pv: avoid export before import if possible
Documentation/virt/kvm/api.rst | 21 ++- arch/s390/include/asm/gmap.h | 38 +++- arch/s390/include/asm/kvm_host.h | 4 + arch/s390/include/asm/mmu.h | 2 +- arch/s390/include/asm/mmu_context.h | 2 +- arch/s390/include/asm/pgtable.h | 20 ++- arch/s390/include/asm/uv.h | 1 + arch/s390/kernel/uv.c | 64 +++++++ arch/s390/kvm/kvm-s390.c | 59 ++++++- arch/s390/kvm/kvm-s390.h | 3 + arch/s390/kvm/pv.c | 259 ++++++++++++++++++++++++++-- arch/s390/mm/fault.c | 22 ++- arch/s390/mm/gmap.c | 156 ++++++++++++++--- include/uapi/linux/kvm.h | 3 + 14 files changed, 606 insertions(+), 48 deletions(-)
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 1/17 KVM: s390: pv: leak the topmost page table when destroy fails
Each secure guest must have a unique ASCE (address space control element); we must avoid that new guests use the same page for their ASCE, to avoid errors.
Since the ASCE mostly consists of the address of the topmost page table (plus some flags), we must not return that memory to the pool unless the ASCE is no longer in use.
Only a successful Destroy Secure Configuration UVC will make the ASCE reusable again.
If the Destroy Configuration UVC fails, the ASCE cannot be reused for a secure guest (either for the ASCE or for other memory areas). To avoid a collision, it must not be used again. This is a permanent error and the page becomes in practice unusable, so we set it aside and leak it. On failure we already leak other memory that belongs to the ultravisor (i.e. the variable and base storage for a guest) and not leaking the topmost page table was an oversight.
This error (and thus the leakage) should not happen unless the hardware is broken or KVM has some unknown serious bug.
--- arch/s390/include/asm/gmap.h | 2 ++ arch/s390/kvm/pv.c | 9 +++-- arch/s390/mm/gmap.c | 69 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-)
arch/s390/include/asm/gmap.h +2 -0
@@ -148,4 +148,6 @@ void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long dirty_bitmap[4], unsigned long gaddr, unsigned long vmaddr);int gmap_mark_unmergeable (void );void s390_reset_acc (struct mm_struct *mm); +void s390_remove_old_asce (struct gmap *gmap); +int s390_replace_asce (struct gmap *gmap);#endif /* _ASM_S390_GMAP_H */
arch/s390/kvm/pv.c +6 -3
@@ -166,10 +166,13 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)atomic_set (&kvm->mm->context.is_protected,0 );KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc);WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); -/* Inteded memory leak on "impossible" error */ -if (!cc) +/* Intended memory leak on "impossible" error */ +if (!cc) {kvm_s390_pv_dealloc_vm (kvm); -return cc ? -EIO :0 ; +return 0 ; + } +s390_replace_asce (kvm->arch.gmap); +return -EIO ; }int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc)
arch/s390/mm/gmap.c +69 -0
72 lines not on this page · raw message
Signature · 1 line
2.34.1
Janosch Frank
maintainerRe: [PATCH v7 01/17] KVM: s390: pv: leak the topmost page table when destroy fails
Quoted 84 lines · Claudio Imbrenda
Each secure guest must have a unique ASCE (address space control element); we must avoid that new guests use the same page for their ASCE, to avoid errors.
Since the ASCE mostly consists of the address of the topmost page table (plus some flags), we must not return that memory to the pool unless the ASCE is no longer in use.
Only a successful Destroy Secure Configuration UVC will make the ASCE reusable again.
If the Destroy Configuration UVC fails, the ASCE cannot be reused for a secure guest (either for the ASCE or for other memory areas). To avoid a collision, it must not be used again. This is a permanent error and the page becomes in practice unusable, so we set it aside and leak it. On failure we already leak other memory that belongs to the ultravisor (i.e. the variable and base storage for a guest) and not leaking the topmost page table was an oversight.
This error (and thus the leakage) should not happen unless the hardware is broken or KVM has some unknown serious bug.
Signed-off-by Claudio Imbrenda <[email protected]>Fixes 29b40f105ec8d55 ("KVM: s390: protvirt: Add initial vm and cpu lifecycle handling")--- arch/s390/include/asm/gmap.h | 2 ++ arch/s390/kvm/pv.c | 9 +++-- arch/s390/mm/gmap.c | 69 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-)arch/s390/include/asm/gmap.h +2
-0@@ -148,4 +148,6 @@ void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long dirty_bitmap[4], unsigned long gaddr, unsigned long vmaddr);int gmap_mark_unmergeable (void );void s390_reset_acc (struct mm_struct *mm); +void s390_remove_old_asce (struct gmap *gmap); +int s390_replace_asce (struct gmap *gmap);#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +6
-3@@ -166,10 +166,13 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)atomic_set (&kvm->mm->context.is_protected,0 );KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc);WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); -/* Inteded memory leak on "impossible" error */ -if (!cc) +/* Intended memory leak on "impossible" error */ +if (!cc) {kvm_s390_pv_dealloc_vm (kvm); -return cc ? -EIO :0 ; +return 0 ; + } +s390_replace_asce (kvm->arch.gmap); +return -EIO ; }int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc)arch/s390/mm/gmap.c +14
-0@@ -2714,3 +2714,72 @@ void s390_reset_acc(struct mm_struct *mm)mmput (mm); }EXPORT_SYMBOL_GPL (s390_reset_acc); + +/** +* s390_remove_old_asce - Remove the topmost level of page tables from the +* list of page tables of the gmap. +* @gmap the gmap whose table is to be removed +* +* This means that it will not be freed when the VM is torn down, and needs +* to be handled separately by the caller, unless an intentional leak is +* intended. +*/ +void s390_remove_old_asce (struct gmap *gmap) +{ +struct page *old; +
arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2740
Quoted 10 lines
arch/s390/mm/gmap.c +10
-0+ old =virt_to_page (gmap->table); +spin_lock (&gmap->guest_table_lock); +list_del (&old->lru); +/* +* in case the ASCE needs to be "removed" multiple times, for example +* if the VM is rebooted into secure mode several times +* concurrently. +*/ +INIT_LIST_HEAD (&old->lru); +spin_unlock (&gmap->guest_table_lock);
The patch itself looks fine to me, but there's one oddity which made me look twice:
You're not overwriting gmap->table here so you can use it in the function below. I guess that's intentional so it can still be used as a reference until we switch over to the new ASCE page?
Quoted 27 lines
arch/s390/mm/gmap.c +27
-0+} +EXPORT_SYMBOL_GPL (s390_remove_old_asce); + +/** +* s390_replace_asce - Try to replace the current ASCE of a gmap with +* another equivalent one. +* @gmap the gmap +* +* If the allocation of the new top level page table fails, the ASCE is not +* replaced. +* In any case, the old ASCE is always removed from the list. Therefore the +* caller has to make sure to save a pointer to it beforehands, unless an > + * intentional leak is intended. +*/ +int s390_replace_asce (struct gmap *gmap) +{ +unsigned long asce; +struct page *page; +void *table; + +s390_remove_old_asce (gmap); + + page =alloc_pages (GFP_KERNEL_ACCOUNT ,CRST_ALLOC_ORDER ); +if (!page) +return -ENOMEM ; + table =page_to_virt (page); +memcpy (table, gmap->table,1UL << (CRST_ALLOC_ORDER +PAGE_SHIFT )); +
arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2778
Quoted 10 lines
arch/s390/mm/gmap.c +10
-0+/* +* The caller has to deal with the old ASCE, but here we make sure +* the new one is properly added to the list of page tables, so that +* it will be freed when the VM is torn down. +*/ +spin_lock (&gmap->guest_table_lock); +list_add (&page->lru, &gmap->crst_list); +spin_unlock (&gmap->guest_table_lock); + + asce = (gmap->asce & ~PAGE_MASK ) |__pa (table);
Please add a comment:
Set the new table origin while preserving ASCE control bits like table type and length.
Quoted 8 lines
arch/s390/mm/gmap.c +7
-0+WRITE_ONCE (gmap->asce, asce); +WRITE_ONCE (gmap->mm->context.gmap_asce, asce); +WRITE_ONCE (gmap->table, table); + +return 0 ; +} +EXPORT_SYMBOL_GPL (s390_replace_asce);
Claudio Imbrenda
maintainerRe: [PATCH v7 01/17] KVM: s390: pv: leak the topmost page table when destroy fails
Quoted 99 lines · On Mon, 7 Feb 2022 09:56:39 +0100 Janosch Frank <[email protected]> wrote:
Quoted 84 lines · Claudio Imbrenda
Each secure guest must have a unique ASCE (address space control element); we must avoid that new guests use the same page for their ASCE, to avoid errors.
Since the ASCE mostly consists of the address of the topmost page table (plus some flags), we must not return that memory to the pool unless the ASCE is no longer in use.
Only a successful Destroy Secure Configuration UVC will make the ASCE reusable again.
If the Destroy Configuration UVC fails, the ASCE cannot be reused for a secure guest (either for the ASCE or for other memory areas). To avoid a collision, it must not be used again. This is a permanent error and the page becomes in practice unusable, so we set it aside and leak it. On failure we already leak other memory that belongs to the ultravisor (i.e. the variable and base storage for a guest) and not leaking the topmost page table was an oversight.
This error (and thus the leakage) should not happen unless the hardware is broken or KVM has some unknown serious bug.
Signed-off-by Claudio Imbrenda <[email protected]>Fixes 29b40f105ec8d55 ("KVM: s390: protvirt: Add initial vm and cpu lifecycle handling")--- arch/s390/include/asm/gmap.h | 2 ++ arch/s390/kvm/pv.c | 9 +++-- arch/s390/mm/gmap.c | 69 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-)arch/s390/include/asm/gmap.h +2
-0@@ -148,4 +148,6 @@ void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long dirty_bitmap[4], unsigned long gaddr, unsigned long vmaddr);int gmap_mark_unmergeable (void );void s390_reset_acc (struct mm_struct *mm); +void s390_remove_old_asce (struct gmap *gmap); +int s390_replace_asce (struct gmap *gmap);#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +6
-3@@ -166,10 +166,13 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)atomic_set (&kvm->mm->context.is_protected,0 );KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc);WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); -/* Inteded memory leak on "impossible" error */ -if (!cc) +/* Intended memory leak on "impossible" error */ +if (!cc) {kvm_s390_pv_dealloc_vm (kvm); -return cc ? -EIO :0 ; +return 0 ; + } +s390_replace_asce (kvm->arch.gmap); +return -EIO ; }int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc)arch/s390/mm/gmap.c +14
-0@@ -2714,3 +2714,72 @@ void s390_reset_acc(struct mm_struct *mm)mmput (mm); }EXPORT_SYMBOL_GPL (s390_reset_acc); + +/** +* s390_remove_old_asce - Remove the topmost level of page tables from the +* list of page tables of the gmap. +* @gmap the gmap whose table is to be removed +* +* This means that it will not be freed when the VM is torn down, and needs +* to be handled separately by the caller, unless an intentional leak is +* intended. +*/ +void s390_remove_old_asce (struct gmap *gmap) +{ +struct page *old; +arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2740
Quoted 10 lines
arch/s390/mm/gmap.c +10
-0+ old =virt_to_page (gmap->table); +spin_lock (&gmap->guest_table_lock); +list_del (&old->lru); +/* +* in case the ASCE needs to be "removed" multiple times, for example +* if the VM is rebooted into secure mode several times +* concurrently. +*/ +INIT_LIST_HEAD (&old->lru); +spin_unlock (&gmap->guest_table_lock);The patch itself looks fine to me, but there's one oddity which made me look twice:
Quoted 3 lines
You're not overwriting
gmap->tablehere so you can use it in the function below. I guess that's intentional so it can still be used as a reference until we switch over to the new ASCE page?
yes. maybe I should rename the function or add more comments explaining that the page is only removed from the list, so that it will not be freed at teardown, but it's still in use by the VM (because we always need an ASCE)
Quoted 35 lines
Quoted 35 lines
arch/s390/mm/gmap.c +33
-0+} +EXPORT_SYMBOL_GPL (s390_remove_old_asce); + +/** +* s390_replace_asce - Try to replace the current ASCE of a gmap with +* another equivalent one. +* @gmap the gmap +* +* If the allocation of the new top level page table fails, the ASCE is not +* replaced. +* In any case, the old ASCE is always removed from the list. Therefore the +* caller has to make sure to save a pointer to it beforehands, unless an > + * intentional leak is intended. +*/ +int s390_replace_asce (struct gmap *gmap) +{ +unsigned long asce; +struct page *page; +void *table; + +s390_remove_old_asce (gmap); + + page =alloc_pages (GFP_KERNEL_ACCOUNT ,CRST_ALLOC_ORDER ); +if (!page) +return -ENOMEM ; + table =page_to_virt (page); +memcpy (table, gmap->table,1UL << (CRST_ALLOC_ORDER +PAGE_SHIFT )); + +/* +* The caller has to deal with the old ASCE, but here we make sure +* the new one is properly added to the list of page tables, so that +* it will be freed when the VM is torn down. +*/ +spin_lock (&gmap->guest_table_lock);
Quoted 8 lines
arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2778
Quoted 4 lines
arch/s390/mm/gmap.c +4
-0+list_add (&page->lru, &gmap->crst_list); +spin_unlock (&gmap->guest_table_lock); + + asce = (gmap->asce & ~PAGE_MASK ) |__pa (table);Please add a comment:
Set the new table origin while preserving ASCE control bits like table type and length.
will do
Quoted 10 lines
Quoted 9 lines
arch/s390/mm/gmap.c +7
-0+WRITE_ONCE (gmap->asce, asce); +WRITE_ONCE (gmap->mm->context.gmap_asce, asce); +WRITE_ONCE (gmap->table, table); + +return 0 ; +} +EXPORT_SYMBOL_GPL (s390_replace_asce);
Heiko Carstens
maintainerRe: [PATCH v7 01/17] KVM: s390: pv: leak the topmost page table when destroy fails
In reply to Janosch FrankQuoted 5 lines
arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2778
Quoted 1 line
arch/s390/mm/gmap.c +1
-0+ asce = (gmap->asce & ~PAGE_MASK ) |__pa (table);Please add a comment:
Set the new table origin while preserving ASCE control bits like table type and length.
And while touching this anyway, please make use of _ASCE_ORIGIN instead of PAGE_MASK.
Claudio Imbrenda
maintainerRe: [PATCH v7 01/17] KVM: s390: pv: leak the topmost page table when destroy fails
Quoted 8 lines · On Mon, 7 Feb 2022 15:33:15 +0100 Heiko Carstens <[email protected]> wrote:
Quoted 5 lines
arch/s390/mm/gmap.c void s390_reset_acc(struct mm_struct *mm) · line 2778
Quoted 1 line
arch/s390/mm/gmap.c +1
-0+ asce = (gmap->asce & ~PAGE_MASK ) |__pa (table);Please add a comment:
Set the new table origin while preserving ASCE control bits like table type and length.And while touching this anyway, please make use of
_ASCE_ORIGINinstead ofPAGE_MASK.
will fix
Claudio Imbrenda
maintainerPATCH v7 2/17 KVM: s390: pv: handle secure storage violations for protected guests
In reply to Claudio ImbrendaWith upcoming patches, protected guests will be able to trigger secure storage violations in normal operation.
With upcoming patches, protected guests will be able to trigger secure storage violations in normal operation. This happens for example if a protected guest is rebooted with lazy destroy enabled and the new guest is also protected.
When the new protected guest touches pages that have not yet been destroyed, and thus are accounted to the previous protected guest, a secure storage violation is raised.
This patch adds handling of secure storage violations for protected guests.
This exception is handled by first trying to destroy the page, because it is expected to belong to a defunct protected guest where a destroy should be possible. If that fails, a normal export of the page is attempted.
Therefore, pages that trigger the exception will be made non-secure before attempting to use them again for a different secure guest.
--- arch/s390/include/asm/uv.h | 1 + arch/s390/kernel/uv.c | 55 ++++++++++++++++++++++++++++++++++++++ arch/s390/mm/fault.c | 10 +++++++ 3 files changed, 66 insertions(+)
arch/s390/include/asm/uv.h +1 -0
@@ -356,6 +356,7 @@ static inline int is_prot_virt_host(void) }int gmap_make_secure (struct gmap *gmap,unsigned long gaddr,void *uvcb); +int gmap_destroy_page (struct gmap *gmap,unsigned long gaddr);int uv_destroy_owned_page (unsigned long paddr);int uv_convert_from_secure (unsigned long paddr);int uv_convert_owned_from_secure (unsigned long paddr);
arch/s390/kernel/uv.c +55 -0
61 lines not on this page · raw message
arch/s390/mm/fault.c +10 -0
@@ -853,6 +853,16 @@ NOKPROBE_SYMBOL(do_non_secure_storage_access);void do_secure_storage_violation (struct pt_regs *regs) { +unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK; +struct gmap *gmap = (struct gmap *)S390_lowcore.gmap; + +/* +* If the VM has been rebooted, its address space might still contain +* secure pages from the previous boot. +* Clear the page so it can be reused. +*/ +if (!gmap_destroy_page (gmap, gaddr)) +return ;/* * Either KVM messed up the secure guest mapping or the same * page is mapped into multiple secure guests.
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 3/17 KVM: s390: pv: handle secure storage exceptions for normal guests
In reply to Claudio ImbrendaWith upcoming patches, normal guests might touch secure pages.
This patch extends the existing exception handler to convert the pages to non secure also when the exception is triggered by a normal guest.
This can happen for example when a secure guest reboots; the first stage of a secure guest is non secure, and in general a secure guest can reboot into non-secure mode.
If the secure memory of the previous boot has not been cleared up completely yet (which will be allowed to happen in an upcoming patch), a non-secure guest might touch secure memory, which will need to be handled properly.
--- arch/s390/mm/fault.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
arch/s390/mm/fault.c +11 -1
@@ -770,6 +770,7 @@ void do_secure_storage_access(struct pt_regs *regs)struct vm_area_struct *vma;struct mm_struct *mm;struct page *page; +struct gmap *gmap;int rc;/*
@@ -799,6 +800,16 @@ void do_secure_storage_access(struct pt_regs *regs) }switch (get_fault_type (regs)) { +case GMAP_FAULT : + gmap = (struct gmap *)S390_lowcore.gmap; +mmap_read_lock (mm); + addr =__gmap_translate (gmap, addr); +mmap_read_unlock (mm); +if (IS_ERR_VALUE (addr)) { +do_fault_error (regs,VM_ACCESS_FLAGS ,VM_FAULT_BADMAP ); +break ; + } + fallthrough;case USER_FAULT : mm = current->mm;mmap_read_lock (mm);
@@ -827,7 +838,6 @@ void do_secure_storage_access(struct pt_regs *regs)if (rc)BUG ();break ; -case GMAP_FAULT :default :do_fault_error (regs,VM_READ |VM_WRITE ,VM_FAULT_BADMAP );WARN_ON_ONCE (1 );
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v7 03/17] KVM: s390: pv: handle secure storage exceptions for normal guests
I love your patch! Perhaps something to improve:
Hi Claudio,
I love your patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on v5.17-rc2 next-20220204]
[cannot apply to kvms390/next s390/features]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: s390-randconfig-r044-20220131 (https://download.01.org/0day-ci/archive/20220205/202202050319.tZ4OT36V-lkp(a)intel.com/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project a73e4ce6a59b01f0e37037761c1e6889d539d233)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/0day-ci/linux/commit/cc87a31d00bc8f7a4e95369503a5ce184747a32b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 git checkout cc87a31d00bc8f7a4e95369503a5ce184747a32b
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash arch/s390/kvm/ arch/s390/mm/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from arch/s390/mm/fault.c:36:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:464:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:477:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
^
include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
#define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
^
In file included from arch/s390/mm/fault.c:36:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:490:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
^
include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
#define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
^
In file included from arch/s390/mm/fault.c:36:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:501:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:511:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:521:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:609:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:617:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:625:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:634:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:643:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:652:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
>> arch/s390/mm/fault.c:805:18: warning: variable 'mm' is uninitialized when used here [-Wuninitialized]
mmap_read_lock(mm);
^~
arch/s390/mm/fault.c:771:22: note: initialize the variable 'mm' to silence this warning
struct mm_struct *mm;
^
= NULL13 warnings generated.
vim +/mm +805 arch/s390/mm/fault.c
766
767 void do_secure_storage_access(struct pt_regs *regs)
768 {
769 unsigned long addr = regs->int_parm_long & __FAIL_ADDR_MASK;
770 struct vm_area_struct *vma;
771 struct mm_struct *mm;
772 struct page *page;
773 struct gmap *gmap;
774 int rc;
775
776 /*
777 * bit 61 tells us if the address is valid, if it's not we
778 * have a major problem and should stop the kernel or send a
779 * SIGSEGV to the process. Unfortunately bit 61 is not
780 * reliable without the misc UV feature so we need to check
781 * for that as well.
782 */
783 if (test_bit_inv(BIT_UV_FEAT_MISC, &uv_info.uv_feature_indications) &&
784 !test_bit_inv(61, ®s->int_parm_long)) {
785 /*
786 * When this happens, userspace did something that it
787 * was not supposed to do, e.g. branching into secure
788 * memory. Trigger a segmentation fault.
789 */
790 if (user_mode(regs)) {
791 send_sig(SIGSEGV, current, 0);
792 return;
793 }
794
795 /*
796 * The kernel should never run into this case and we
797 * have no way out of this situation.
798 */
799 panic("Unexpected PGM 0x3d with TEID bit 61=0");
800 }
801
802 switch (get_fault_type(regs)) {
803 case GMAP_FAULT:
804 gmap = (struct gmap *)S390_lowcore.gmap;
> 805 mmap_read_lock(mm);
806 addr = __gmap_translate(gmap, addr);
807 mmap_read_unlock(mm);
808 if (IS_ERR_VALUE(addr)) {
809 do_fault_error(regs, VM_ACCESS_FLAGS, VM_FAULT_BADMAP);
810 break;
811 }
812 fallthrough;
813 case USER_FAULT:
814 mm = current->mm;
815 mmap_read_lock(mm);
816 vma = find_vma(mm, addr);
817 if (!vma) {
818 mmap_read_unlock(mm);
819 do_fault_error(regs, VM_READ | VM_WRITE, VM_FAULT_BADMAP);
820 break;
821 }
822 page = follow_page(vma, addr, FOLL_WRITE | FOLL_GET);
823 if (IS_ERR_OR_NULL(page)) {
824 mmap_read_unlock(mm);
825 break;
826 }
827 if (arch_make_page_accessible(page))
828 send_sig(SIGSEGV, current, 0);
829 put_page(page);
830 mmap_read_unlock(mm);
831 break;
832 case KERNEL_FAULT:
833 page = phys_to_page(addr);
834 if (unlikely(!try_get_page(page)))
835 break;
836 rc = arch_make_page_accessible(page);
837 put_page(page);
838 if (rc)
839 BUG();
840 break;
841 default:
842 do_fault_error(regs, VM_READ | VM_WRITE, VM_FAULT_BADMAP);
843 WARN_ON_ONCE(1);
844 }
845 }
846 NOKPROBE_SYMBOL(do_secure_storage_access);
847
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH v7 03/17] KVM: s390: pv: handle secure storage exceptions for normal guests
In reply to Claudio ImbrendaQuoted 9 lines · Claudio Imbrenda
With upcoming patches, normal guests might touch secure pages.
This patch extends the existing exception handler to convert the pages to non secure also when the exception is triggered by a normal guest.
This can happen for example when a secure guest reboots; the first stage of a secure guest is non secure, and in general a secure guest can reboot into non-secure mode.
Quoted 6 lines
If the secure memory of the previous boot has not been cleared up completely yet (which will be allowed to happen in an upcoming patch), a non-secure guest might touch secure memory, which will need to be handled properly.
Signed-off-by Claudio Imbrenda <[email protected]>
Once you fix the compiler warning:
Quoted 42 lines
--- arch/s390/mm/fault.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)arch/s390/mm/fault.c +11
-1@@ -770,6 +770,7 @@ void do_secure_storage_access(struct pt_regs *regs)struct vm_area_struct *vma;struct mm_struct *mm;struct page *page; +struct gmap *gmap;int rc;/* @@ -799,6 +800,16 @@ void do_secure_storage_access(struct pt_regs *regs) }switch (get_fault_type (regs)) { +case GMAP_FAULT : + gmap = (struct gmap *)S390_lowcore.gmap; +mmap_read_lock (mm); + addr =__gmap_translate (gmap, addr); +mmap_read_unlock (mm); +if (IS_ERR_VALUE (addr)) { +do_fault_error (regs,VM_ACCESS_FLAGS ,VM_FAULT_BADMAP ); +break ; + } + fallthrough;case USER_FAULT : mm = current->mm;mmap_read_lock (mm);@@ -827,7 +838,6 @@ void do_secure_storage_access(struct pt_regs *regs)if (rc)BUG ();break ; -case GMAP_FAULT :default :do_fault_error (regs,VM_READ |VM_WRITE ,VM_FAULT_BADMAP );WARN_ON_ONCE (1 );
Claudio Imbrenda
maintainerPATCH v7 4/17 KVM: s390: pv: refactor s390_reset_acc
In reply to Claudio ImbrendaRefactor s390_reset_acc so that it can be reused in upcoming patches.
We don't want to hold all the locks used in a walk_page_range for too long, and the destroy page UVC does take some time to complete. Therefore we quickly gather the pages to destroy, and then destroy them without holding all the locks.
The new refactored function optionally allows to return early without completing if a fatal signal is pending (and return and appropriate error code). Two wrappers are provided to call the new function.
(dropping Janosch's Ack because of major changes to the patch)
--- arch/s390/include/asm/gmap.h | 36 +++++++++++++- arch/s390/kvm/pv.c | 12 ++++- arch/s390/mm/gmap.c | 95 +++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 32 deletions(-)
arch/s390/include/asm/gmap.h +35 -1
@@ -147,7 +147,41 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr);int gmap_mark_unmergeable (void ); -void s390_reset_acc (struct mm_struct *mm);void s390_remove_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap); +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns); +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible); + +/** +* s390_uv_destroy_range - Destroy a range of pages in the given mm. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls, but it +* will otherwise only return when it completed. +*/ +static inline void s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ + (void )__s390_uv_destroy_range (mm, start, end,0 ); +} + +/** +* s390_uv_destroy_range_interruptibe - Destroy a range of pages in the +* given mm, but stop when a fatal signal is received. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls. It +* will return -EINTR if a fatal signal is received, or 0 if the whole range +* has been destroyed. +*/ +static inline int s390_uv_destroy_range_interruptible (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ +return __s390_uv_destroy_range (mm, start, end,1 ); +}#endif /* _ASM_S390_GMAP_H */
arch/s390/kvm/pv.c +10 -2
@@ -12,6 +12,8 @@#include <asm/gmap.h> #include <asm/uv.h> #include <asm/mman.h> +#include <linux/pagewalk.h> +#include <linux/sched/mm.h> #include "kvm-s390.h" int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc)
@@ -157,8 +159,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) {int cc; -/* make all pages accessible before destroying the guest */ -s390_reset_acc (kvm->mm); +/* +* if the mm still has a mapping, make all its pages accessible +* before destroying the guest +*/ +if (mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);
arch/s390/mm/gmap.c +66 -29
110 lines not on this page · raw message
Signature · 1 line
2.34.1
Janosch Frank
maintainerRe: [PATCH v7 04/17] KVM: s390: pv: refactor s390_reset_acc
Quoted 24 lines · Claudio Imbrenda
Refactor
s390_reset_accso that it can be reused in upcoming patches.We don't want to hold all the locks used in a
walk_page_rangefor too long, and the destroy page UVC does take some time to complete. Therefore we quickly gather the pages to destroy, and then destroy them without holding all the locks.The new refactored function optionally allows to return early without completing if a fatal signal is pending (and return and appropriate error code). Two wrappers are provided to call the new function.
Signed-off-by Claudio Imbrenda <[email protected]>(dropping Janosch's Ack because of major changes to the patch)
--- arch/s390/include/asm/gmap.h | 36 +++++++++++++- arch/s390/kvm/pv.c | 12 ++++- arch/s390/mm/gmap.c | 95 +++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 32 deletions(-)arch/s390/include/asm/gmap.h +0
-0
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 154
Quoted 9 lines
arch/s390/include/asm/gmap.h +3
-1@@ -147,7 +147,41 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr);int gmap_mark_unmergeable (void ); -void s390_reset_acc (struct mm_struct *mm);void s390_remove_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap); +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns); +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible);
s/int/bool/ ?
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 162
Quoted 8 lines
arch/s390/include/asm/gmap.h +8
-0+ +/** +* s390_uv_destroy_range - Destroy a range of pages in the given mm. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls, but it
This function will call ?
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 172
Quoted 10 lines
arch/s390/include/asm/gmap.h +10
-0+* will otherwise only return when it completed. +*/ +static inline void s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ + (void )__s390_uv_destroy_range (mm, start, end,0 ); +} + +/** +* s390_uv_destroy_range_interruptibe - Destroy a range of pages in the
interruptible
Quoted 161 lines
arch/s390/include/asm/gmap.h +14
-0+* given mm, but stop when a fatal signal is received. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls. It +* will return -EINTR if a fatal signal is received, or 0 if the whole range +* has been destroyed. +*/ +static inline int s390_uv_destroy_range_interruptible (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ +return __s390_uv_destroy_range (mm, start, end,1 ); +}#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +10
-2@@ -12,6 +12,8 @@#include <asm/gmap.h> #include <asm/uv.h> #include <asm/mman.h> +#include <linux/pagewalk.h> +#include <linux/sched/mm.h> #include "kvm-s390.h" int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc)@@ -157,8 +159,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) {int cc; -/* make all pages accessible before destroying the guest */ -s390_reset_acc (kvm->mm); +/* +* if the mm still has a mapping, make all its pages accessible +* before destroying the guest +*/ +if (mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);arch/s390/mm/gmap.c +66
-29@@ -2676,44 +2676,81 @@ void s390_reset_cmma(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (s390_reset_cmma); -/* -* make inaccessible pages accessible again -*/ -static int __s390_reset_acc (pte_t *ptep,unsigned long addr, -unsigned long next,struct mm_walk *walk) +#define DESTROY_LOOP_THRESHOLD 32 + +struct reset_walk_state { +unsigned long next; +unsigned long count; +unsigned long pfns[DESTROY_LOOP_THRESHOLD ]; +}; + +static int s390_gather_pages (pte_t *ptep,unsigned long addr, +unsigned long next,struct mm_walk *walk) { +struct reset_walk_state *p = walk->private;pte_t pte =READ_ONCE (*ptep); -/* There is a reference through the mapping */ -if (pte_present (pte)) -WARN_ON_ONCE (uv_destroy_owned_page (pte_val (pte) &PAGE_MASK )); - -return 0 ; +if (pte_present (pte)) { +/* we have a reference from the mapping, take an extra one */ +get_page (phys_to_page (pte_val (pte))); + p->pfns[p->count] =phys_to_pfn (pte_val (pte)); + p->next = next; + p->count++; + } +return p->count >=DESTROY_LOOP_THRESHOLD ; } -static const struct mm_walk_ops reset_acc_walk_ops = { - .pte_entry = __s390_reset_acc, +static const struct mm_walk_ops gather_pages_ops = { + .pte_entry = s390_gather_pages, }; -#include <linux/sched/mm.h> -void s390_reset_acc (struct mm_struct *mm) +/* +* Call the Destroy secure page UVC on each page in the given array of PFNs. +* Each page needs to have an extra reference, which will be released here. +*/ +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns) { -if (!mm_is_protected (mm)) -return ; -/* -* we might be called during -* reset: we walk the pages and clear -* close of all kvm file descriptors: we walk the pages and clear -* exit of process on fd closure: vma already gone, do nothing -*/ -if (!mmget_not_zero (mm)) -return ; -mmap_read_lock (mm); -walk_page_range (mm,0 ,TASK_SIZE , &reset_acc_walk_ops,NULL ); -mmap_read_unlock (mm); -mmput (mm); +unsigned long i; + +for (i =0 ; i < count; i++) { +/* we always have an extra reference */ +uv_destroy_owned_page (pfn_to_phys (pfns[i])); +/* get rid of the extra reference */ +put_page (pfn_to_page (pfns[i])); +cond_resched (); + } +} +EXPORT_SYMBOL_GPL (s390_uv_destroy_pfns); + +/** +* __s390_uv_destroy_range - Walk the given range of the given address +* space, and call the destroy secure page UVC on each page. +* Optionally exit early if a fatal signal is pending. +* @mm the mm to operate on +* @start the start of the range +* @end the end of the range +* @interruptible if not 0, stop when a fatal signal is received +* Return: 0 on success, -EINTR if the function stopped before completing +*/ +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible) +{ +struct reset_walk_state state = { .next = start }; +int r =1 ; + +while (r >0 ) { + state.count =0 ; +mmap_read_lock (mm); + r =walk_page_range (mm, state.next, end, &gather_pages_ops, &state); +mmap_read_unlock (mm); +cond_resched (); +s390_uv_destroy_pfns (state.count, state.pfns); +if (interruptible &&fatal_signal_pending (current)) +return -EINTR ; + } +return 0 ; } -EXPORT_SYMBOL_GPL (s390_reset_acc); +EXPORT_SYMBOL_GPL (__s390_uv_destroy_range);/** * s390_remove_old_asce - Remove the topmost level of page tables from the
Claudio Imbrenda
maintainerRe: [PATCH v7 04/17] KVM: s390: pv: refactor s390_reset_acc
Quoted 20 lines · On Mon, 7 Feb 2022 11:02:28 +0100 Janosch Frank <[email protected]> wrote:
Quoted 19 lines · Claudio Imbrenda
Refactor
s390_reset_accso that it can be reused in upcoming patches.We don't want to hold all the locks used in a
walk_page_rangefor too long, and the destroy page UVC does take some time to complete. Therefore we quickly gather the pages to destroy, and then destroy them without holding all the locks.The new refactored function optionally allows to return early without completing if a fatal signal is pending (and return and appropriate error code). Two wrappers are provided to call the new function.
Signed-off-by Claudio Imbrenda <[email protected]>(dropping Janosch's Ack because of major changes to the patch)
--- arch/s390/include/asm/gmap.h | 36 +++++++++++++- arch/s390/kvm/pv.c | 12 ++++- arch/s390/mm/gmap.c | 95 +++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 32 deletions(-)
Quoted 16 lines
Quoted 5 lines
arch/s390/include/asm/gmap.h +0
-0arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 154
Quoted 9 lines
arch/s390/include/asm/gmap.h +3
-1@@ -147,7 +147,41 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr);int gmap_mark_unmergeable (void ); -void s390_reset_acc (struct mm_struct *mm);void s390_remove_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap); +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns); +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible);s/int/bool/ ?
I don't know, I like the fact that the function could return different error codes in the future if needed.
Quoted 11 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 162
Quoted 9 lines
arch/s390/include/asm/gmap.h +8
-0+ +/** +* s390_uv_destroy_range - Destroy a range of pages in the given mm. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls, but it This function will call ?
will fix
Quoted 13 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 172
Quoted 11 lines
arch/s390/include/asm/gmap.h +10
-0+* will otherwise only return when it completed. +*/ +static inline void s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ + (void )__s390_uv_destroy_range (mm, start, end,0 ); +} + +/** +* s390_uv_destroy_range_interruptibe - Destroy a range of pages in the interruptible
will fix
Quoted 163 lines
Quoted 162 lines
arch/s390/include/asm/gmap.h +14
-0+* given mm, but stop when a fatal signal is received. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls. It +* will return -EINTR if a fatal signal is received, or 0 if the whole range +* has been destroyed. +*/ +static inline int s390_uv_destroy_range_interruptible (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ +return __s390_uv_destroy_range (mm, start, end,1 ); +}#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +10
-2@@ -12,6 +12,8 @@#include <asm/gmap.h> #include <asm/uv.h> #include <asm/mman.h> +#include <linux/pagewalk.h> +#include <linux/sched/mm.h> #include "kvm-s390.h" int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc)@@ -157,8 +159,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) {int cc; -/* make all pages accessible before destroying the guest */ -s390_reset_acc (kvm->mm); +/* +* if the mm still has a mapping, make all its pages accessible +* before destroying the guest +*/ +if (mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);arch/s390/mm/gmap.c +66
-29@@ -2676,44 +2676,81 @@ void s390_reset_cmma(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (s390_reset_cmma); -/* -* make inaccessible pages accessible again -*/ -static int __s390_reset_acc (pte_t *ptep,unsigned long addr, -unsigned long next,struct mm_walk *walk) +#define DESTROY_LOOP_THRESHOLD 32 + +struct reset_walk_state { +unsigned long next; +unsigned long count; +unsigned long pfns[DESTROY_LOOP_THRESHOLD ]; +}; + +static int s390_gather_pages (pte_t *ptep,unsigned long addr, +unsigned long next,struct mm_walk *walk) { +struct reset_walk_state *p = walk->private;pte_t pte =READ_ONCE (*ptep); -/* There is a reference through the mapping */ -if (pte_present (pte)) -WARN_ON_ONCE (uv_destroy_owned_page (pte_val (pte) &PAGE_MASK )); - -return 0 ; +if (pte_present (pte)) { +/* we have a reference from the mapping, take an extra one */ +get_page (phys_to_page (pte_val (pte))); + p->pfns[p->count] =phys_to_pfn (pte_val (pte)); + p->next = next; + p->count++; + } +return p->count >=DESTROY_LOOP_THRESHOLD ; } -static const struct mm_walk_ops reset_acc_walk_ops = { - .pte_entry = __s390_reset_acc, +static const struct mm_walk_ops gather_pages_ops = { + .pte_entry = s390_gather_pages, }; -#include <linux/sched/mm.h> -void s390_reset_acc (struct mm_struct *mm) +/* +* Call the Destroy secure page UVC on each page in the given array of PFNs. +* Each page needs to have an extra reference, which will be released here. +*/ +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns) { -if (!mm_is_protected (mm)) -return ; -/* -* we might be called during -* reset: we walk the pages and clear -* close of all kvm file descriptors: we walk the pages and clear -* exit of process on fd closure: vma already gone, do nothing -*/ -if (!mmget_not_zero (mm)) -return ; -mmap_read_lock (mm); -walk_page_range (mm,0 ,TASK_SIZE , &reset_acc_walk_ops,NULL ); -mmap_read_unlock (mm); -mmput (mm); +unsigned long i; + +for (i =0 ; i < count; i++) { +/* we always have an extra reference */ +uv_destroy_owned_page (pfn_to_phys (pfns[i])); +/* get rid of the extra reference */ +put_page (pfn_to_page (pfns[i])); +cond_resched (); + } +} +EXPORT_SYMBOL_GPL (s390_uv_destroy_pfns); + +/** +* __s390_uv_destroy_range - Walk the given range of the given address +* space, and call the destroy secure page UVC on each page. +* Optionally exit early if a fatal signal is pending. +* @mm the mm to operate on +* @start the start of the range +* @end the end of the range +* @interruptible if not 0, stop when a fatal signal is received +* Return: 0 on success, -EINTR if the function stopped before completing +*/ +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible) +{ +struct reset_walk_state state = { .next = start }; +int r =1 ; + +while (r >0 ) { + state.count =0 ; +mmap_read_lock (mm); + r =walk_page_range (mm, state.next, end, &gather_pages_ops, &state); +mmap_read_unlock (mm); +cond_resched (); +s390_uv_destroy_pfns (state.count, state.pfns); +if (interruptible &&fatal_signal_pending (current)) +return -EINTR ; + } +return 0 ; } -EXPORT_SYMBOL_GPL (s390_reset_acc); +EXPORT_SYMBOL_GPL (__s390_uv_destroy_range);/** * s390_remove_old_asce - Remove the topmost level of page tables from the
Janosch Frank
maintainerRe: [PATCH v7 04/17] KVM: s390: pv: refactor s390_reset_acc
Quoted 40 lines · Claudio Imbrenda
Quoted 20 lines · On Mon, 7 Feb 2022 11:02:28 +0100 Janosch Frank <[email protected]> wrote:
Quoted 19 lines · Claudio Imbrenda
Refactor
s390_reset_accso that it can be reused in upcoming patches.We don't want to hold all the locks used in a
walk_page_rangefor too long, and the destroy page UVC does take some time to complete. Therefore we quickly gather the pages to destroy, and then destroy them without holding all the locks.The new refactored function optionally allows to return early without completing if a fatal signal is pending (and return and appropriate error code). Two wrappers are provided to call the new function.
Signed-off-by Claudio Imbrenda <[email protected]>(dropping Janosch's Ack because of major changes to the patch)
--- arch/s390/include/asm/gmap.h | 36 +++++++++++++- arch/s390/kvm/pv.c | 12 ++++- arch/s390/mm/gmap.c | 95 +++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 32 deletions(-)Quoted 16 lines
Quoted 5 lines
arch/s390/include/asm/gmap.h +0
-0arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 154
Quoted 9 lines
arch/s390/include/asm/gmap.h +3
-1@@ -147,7 +147,41 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr);int gmap_mark_unmergeable (void ); -void s390_reset_acc (struct mm_struct *mm);void s390_remove_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap); +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns); +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible);s/int/bool/ ?
Quoted 2 lines
I don't know, I like the fact that the function could return different error codes in the future if needed.
I meant the interruptible parameter or do you expect to have various levels of interruptibility (is that even a word)?
Quoted 195 lines
Quoted 12 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 162
Quoted 10 lines
arch/s390/include/asm/gmap.h +8
-0+ +/** +* s390_uv_destroy_range - Destroy a range of pages in the given mm. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls, but it This function will call ?
will fix
Quoted 13 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 172
Quoted 11 lines
arch/s390/include/asm/gmap.h +10
-0+* will otherwise only return when it completed. +*/ +static inline void s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ + (void )__s390_uv_destroy_range (mm, start, end,0 ); +} + +/** +* s390_uv_destroy_range_interruptibe - Destroy a range of pages in the interruptible
will fix
Quoted 163 lines
Quoted 162 lines
arch/s390/include/asm/gmap.h +14
-0+* given mm, but stop when a fatal signal is received. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls. It +* will return -EINTR if a fatal signal is received, or 0 if the whole range +* has been destroyed. +*/ +static inline int s390_uv_destroy_range_interruptible (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ +return __s390_uv_destroy_range (mm, start, end,1 ); +}#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +10
-2@@ -12,6 +12,8 @@#include <asm/gmap.h> #include <asm/uv.h> #include <asm/mman.h> +#include <linux/pagewalk.h> +#include <linux/sched/mm.h> #include "kvm-s390.h" int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc)@@ -157,8 +159,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) {int cc; -/* make all pages accessible before destroying the guest */ -s390_reset_acc (kvm->mm); +/* +* if the mm still has a mapping, make all its pages accessible +* before destroying the guest +*/ +if (mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);arch/s390/mm/gmap.c +66
-29@@ -2676,44 +2676,81 @@ void s390_reset_cmma(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (s390_reset_cmma); -/* -* make inaccessible pages accessible again -*/ -static int __s390_reset_acc (pte_t *ptep,unsigned long addr, -unsigned long next,struct mm_walk *walk) +#define DESTROY_LOOP_THRESHOLD 32 + +struct reset_walk_state { +unsigned long next; +unsigned long count; +unsigned long pfns[DESTROY_LOOP_THRESHOLD ]; +}; + +static int s390_gather_pages (pte_t *ptep,unsigned long addr, +unsigned long next,struct mm_walk *walk) { +struct reset_walk_state *p = walk->private;pte_t pte =READ_ONCE (*ptep); -/* There is a reference through the mapping */ -if (pte_present (pte)) -WARN_ON_ONCE (uv_destroy_owned_page (pte_val (pte) &PAGE_MASK )); - -return 0 ; +if (pte_present (pte)) { +/* we have a reference from the mapping, take an extra one */ +get_page (phys_to_page (pte_val (pte))); + p->pfns[p->count] =phys_to_pfn (pte_val (pte)); + p->next = next; + p->count++; + } +return p->count >=DESTROY_LOOP_THRESHOLD ; } -static const struct mm_walk_ops reset_acc_walk_ops = { - .pte_entry = __s390_reset_acc, +static const struct mm_walk_ops gather_pages_ops = { + .pte_entry = s390_gather_pages, }; -#include <linux/sched/mm.h> -void s390_reset_acc (struct mm_struct *mm) +/* +* Call the Destroy secure page UVC on each page in the given array of PFNs. +* Each page needs to have an extra reference, which will be released here. +*/ +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns) { -if (!mm_is_protected (mm)) -return ; -/* -* we might be called during -* reset: we walk the pages and clear -* close of all kvm file descriptors: we walk the pages and clear -* exit of process on fd closure: vma already gone, do nothing -*/ -if (!mmget_not_zero (mm)) -return ; -mmap_read_lock (mm); -walk_page_range (mm,0 ,TASK_SIZE , &reset_acc_walk_ops,NULL ); -mmap_read_unlock (mm); -mmput (mm); +unsigned long i; + +for (i =0 ; i < count; i++) { +/* we always have an extra reference */ +uv_destroy_owned_page (pfn_to_phys (pfns[i])); +/* get rid of the extra reference */ +put_page (pfn_to_page (pfns[i])); +cond_resched (); + } +} +EXPORT_SYMBOL_GPL (s390_uv_destroy_pfns); + +/** +* __s390_uv_destroy_range - Walk the given range of the given address +* space, and call the destroy secure page UVC on each page. +* Optionally exit early if a fatal signal is pending. +* @mm the mm to operate on +* @start the start of the range +* @end the end of the range +* @interruptible if not 0, stop when a fatal signal is received +* Return: 0 on success, -EINTR if the function stopped before completing +*/ +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible) +{ +struct reset_walk_state state = { .next = start }; +int r =1 ; + +while (r >0 ) { + state.count =0 ; +mmap_read_lock (mm); + r =walk_page_range (mm, state.next, end, &gather_pages_ops, &state); +mmap_read_unlock (mm); +cond_resched (); +s390_uv_destroy_pfns (state.count, state.pfns); +if (interruptible &&fatal_signal_pending (current)) +return -EINTR ; + } +return 0 ; } -EXPORT_SYMBOL_GPL (s390_reset_acc); +EXPORT_SYMBOL_GPL (__s390_uv_destroy_range);/** * s390_remove_old_asce - Remove the topmost level of page tables from the
Claudio Imbrenda
maintainerRe: [PATCH v7 04/17] KVM: s390: pv: refactor s390_reset_acc
Quoted 41 lines · On Mon, 7 Feb 2022 11:56:11 +0100 Janosch Frank <[email protected]> wrote:
Quoted 40 lines · Claudio Imbrenda
Quoted 36 lines · On Mon, 7 Feb 2022 11:02:28 +0100 Janosch Frank <[email protected]> wrote:
Quoted 24 lines · Claudio Imbrenda
Refactor
s390_reset_accso that it can be reused in upcoming patches.We don't want to hold all the locks used in a
walk_page_rangefor too long, and the destroy page UVC does take some time to complete. Therefore we quickly gather the pages to destroy, and then destroy them without holding all the locks.The new refactored function optionally allows to return early without completing if a fatal signal is pending (and return and appropriate error code). Two wrappers are provided to call the new function.
Signed-off-by Claudio Imbrenda <[email protected]>(dropping Janosch's Ack because of major changes to the patch)
--- arch/s390/include/asm/gmap.h | 36 +++++++++++++- arch/s390/kvm/pv.c | 12 ++++- arch/s390/mm/gmap.c | 95 +++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 32 deletions(-)arch/s390/include/asm/gmap.h +0
-0arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 154
Quoted 9 lines
arch/s390/include/asm/gmap.h +3
-1@@ -147,7 +147,41 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr);int gmap_mark_unmergeable (void ); -void s390_reset_acc (struct mm_struct *mm);void s390_remove_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap); +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns); +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible);s/int/bool/ ?
Quoted 5 lines
Quoted 2 lines
I don't know, I like the fact that the function could return different error codes in the future if needed.
I meant the interruptible parameter or do you expect to have various levels of interruptibility (is that even a word)?
ooohhh, that one.
I guess it could be a bool, yes
Quoted 197 lines
Quoted 196 lines
Quoted 13 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 162
Quoted 11 lines
arch/s390/include/asm/gmap.h +8
-0+ +/** +* s390_uv_destroy_range - Destroy a range of pages in the given mm. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls, but it This function will call ?
will fix
Quoted 13 lines
arch/s390/include/asm/gmap.h int gmap_mprotect_notify(struct gmap *, unsigned long start, · line 172
Quoted 11 lines
arch/s390/include/asm/gmap.h +10
-0+* will otherwise only return when it completed. +*/ +static inline void s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ + (void )__s390_uv_destroy_range (mm, start, end,0 ); +} + +/** +* s390_uv_destroy_range_interruptibe - Destroy a range of pages in the interruptible
will fix
Quoted 163 lines
Quoted 162 lines
arch/s390/include/asm/gmap.h +14
-0+* given mm, but stop when a fatal signal is received. +* @mm the mm on which to operate on +* @start the start of the range +* @end the end of the range +* +* This call will call cond_sched, so it should not generate stalls. It +* will return -EINTR if a fatal signal is received, or 0 if the whole range +* has been destroyed. +*/ +static inline int s390_uv_destroy_range_interruptible (struct mm_struct *mm,unsigned long start, +unsigned long end) +{ +return __s390_uv_destroy_range (mm, start, end,1 ); +}#endif /* _ASM_S390_GMAP_H */ arch/s390/kvm/pv.c +10
-2@@ -12,6 +12,8 @@#include <asm/gmap.h> #include <asm/uv.h> #include <asm/mman.h> +#include <linux/pagewalk.h> +#include <linux/sched/mm.h> #include "kvm-s390.h" int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc)@@ -157,8 +159,14 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) {int cc; -/* make all pages accessible before destroying the guest */ -s390_reset_acc (kvm->mm); +/* +* if the mm still has a mapping, make all its pages accessible +* before destroying the guest +*/ +if (mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);arch/s390/mm/gmap.c +66
-29@@ -2676,44 +2676,81 @@ void s390_reset_cmma(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (s390_reset_cmma); -/* -* make inaccessible pages accessible again -*/ -static int __s390_reset_acc (pte_t *ptep,unsigned long addr, -unsigned long next,struct mm_walk *walk) +#define DESTROY_LOOP_THRESHOLD 32 + +struct reset_walk_state { +unsigned long next; +unsigned long count; +unsigned long pfns[DESTROY_LOOP_THRESHOLD ]; +}; + +static int s390_gather_pages (pte_t *ptep,unsigned long addr, +unsigned long next,struct mm_walk *walk) { +struct reset_walk_state *p = walk->private;pte_t pte =READ_ONCE (*ptep); -/* There is a reference through the mapping */ -if (pte_present (pte)) -WARN_ON_ONCE (uv_destroy_owned_page (pte_val (pte) &PAGE_MASK )); - -return 0 ; +if (pte_present (pte)) { +/* we have a reference from the mapping, take an extra one */ +get_page (phys_to_page (pte_val (pte))); + p->pfns[p->count] =phys_to_pfn (pte_val (pte)); + p->next = next; + p->count++; + } +return p->count >=DESTROY_LOOP_THRESHOLD ; } -static const struct mm_walk_ops reset_acc_walk_ops = { - .pte_entry = __s390_reset_acc, +static const struct mm_walk_ops gather_pages_ops = { + .pte_entry = s390_gather_pages, }; -#include <linux/sched/mm.h> -void s390_reset_acc (struct mm_struct *mm) +/* +* Call the Destroy secure page UVC on each page in the given array of PFNs. +* Each page needs to have an extra reference, which will be released here. +*/ +void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns) { -if (!mm_is_protected (mm)) -return ; -/* -* we might be called during -* reset: we walk the pages and clear -* close of all kvm file descriptors: we walk the pages and clear -* exit of process on fd closure: vma already gone, do nothing -*/ -if (!mmget_not_zero (mm)) -return ; -mmap_read_lock (mm); -walk_page_range (mm,0 ,TASK_SIZE , &reset_acc_walk_ops,NULL ); -mmap_read_unlock (mm); -mmput (mm); +unsigned long i; + +for (i =0 ; i < count; i++) { +/* we always have an extra reference */ +uv_destroy_owned_page (pfn_to_phys (pfns[i])); +/* get rid of the extra reference */ +put_page (pfn_to_page (pfns[i])); +cond_resched (); + } +} +EXPORT_SYMBOL_GPL (s390_uv_destroy_pfns); + +/** +* __s390_uv_destroy_range - Walk the given range of the given address +* space, and call the destroy secure page UVC on each page. +* Optionally exit early if a fatal signal is pending. +* @mm the mm to operate on +* @start the start of the range +* @end the end of the range +* @interruptible if not 0, stop when a fatal signal is received +* Return: 0 on success, -EINTR if the function stopped before completing +*/ +int __s390_uv_destroy_range (struct mm_struct *mm,unsigned long start, +unsigned long end,int interruptible) +{ +struct reset_walk_state state = { .next = start }; +int r =1 ; + +while (r >0 ) { + state.count =0 ; +mmap_read_lock (mm); + r =walk_page_range (mm, state.next, end, &gather_pages_ops, &state); +mmap_read_unlock (mm); +cond_resched (); +s390_uv_destroy_pfns (state.count, state.pfns); +if (interruptible &&fatal_signal_pending (current)) +return -EINTR ; + } +return 0 ; } -EXPORT_SYMBOL_GPL (s390_reset_acc); +EXPORT_SYMBOL_GPL (__s390_uv_destroy_range);/** * s390_remove_old_asce - Remove the topmost level of page tables from the
Claudio Imbrenda
maintainerPATCH v7 5/17 KVM: s390: pv: usage counter instead of flag
In reply to Claudio ImbrendaUse the new protected_count field as a counter instead of the old is_protected flag. This will be used in upcoming patches.
Increment the counter when a secure configuration is created, and decrement it when it is destroyed. Previously the flag was set when the set secure parameters UVC was performed.
--- arch/s390/include/asm/mmu.h | 2 +- arch/s390/include/asm/mmu_context.h | 2 +- arch/s390/include/asm/pgtable.h | 2 +- arch/s390/kvm/pv.c | 12 +++++++----- 4 files changed, 10 insertions(+), 8 deletions(-)
arch/s390/include/asm/mmu.h +1 -1
@@ -17,7 +17,7 @@ typedef struct {unsigned long asce_limit;unsigned long vdso_base;/* The mmu context belongs to a secure guest. */ -atomic_t is_protected; +atomic_t protected_count;/* * The following bitfields need a down_write on the mm * semaphore when they are written to. As they are only
arch/s390/include/asm/mmu_context.h +1 -1
@@ -26,7 +26,7 @@ static inline int init_new_context(struct task_struct *tsk,INIT_LIST_HEAD (&mm->context.gmap_list);cpumask_clear (&mm->context.cpu_attach_mask);atomic_set (&mm->context.flush_count,0 ); -atomic_set (&mm->context.is_protected,0 ); +atomic_set (&mm->context.protected_count,0 ); mm->context.gmap_asce =0 ; mm->context.flush_mm =0 ;#ifdef CONFIG_PGSTE
arch/s390/include/asm/pgtable.h +1 -1
@@ -523,7 +523,7 @@ static inline int mm_has_pgste(struct mm_struct *mm)static inline int mm_is_protected (struct mm_struct *mm) {#ifdef CONFIG_PGSTE -if (unlikely (atomic_read (&mm->context.is_protected))) +if (unlikely (atomic_read (&mm->context.protected_count)))return 1 ;#endif return 0 ;
arch/s390/kvm/pv.c +7 -5
@@ -171,7 +171,8 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); -atomic_set (&kvm->mm->context.is_protected,0 ); +if (!cc) +atomic_dec (&kvm->mm->context.protected_count);KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc);WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc);/* Intended memory leak on "impossible" error */
@@ -213,11 +214,14 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)/* Outputs */ kvm->arch.pv.handle = uvcb.guest_handle; +atomic_inc (&kvm->mm->context.protected_count);if (cc) { -if (uvcb.header.rc &UVC_RC_NEED_DESTROY ) +if (uvcb.header.rc &UVC_RC_NEED_DESTROY ) {kvm_s390_pv_deinit_vm (kvm, &dummy, &dummy); -else + }else { +atomic_dec (&kvm->mm->context.protected_count);kvm_s390_pv_dealloc_vm (kvm); + }return -EIO ; } kvm->arch.gmap->guest_handle = uvcb.guest_handle;
@@ -240,8 +244,6 @@ int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc, *rrc = uvcb.header.rrc;KVM_UV_EVENT (kvm,3 ,"PROTVIRT VM SET PARMS: rc %x rrc %x" , *rc, *rrc); -if (!cc) -atomic_set (&kvm->mm->context.is_protected,1 );return cc ? -EINVAL :0 ; }
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 6/17 KVM: s390: pv: add export before import
In reply to Claudio ImbrendaDue to upcoming changes, it will be possible to temporarily have multiple protected VMs in the same address space, although only one will be actually active.
In that scenario, it is necessary to perform an export of every page that is to be imported, since the hardware does not allow a page belonging to a protected guest to be imported into a different protected guest.
This also applies to pages that are shared, and thus accessible by the host.
--- arch/s390/kernel/uv.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/s390/kernel/uv.c +8 -0
@@ -234,6 +234,12 @@ static int make_secure_pte(pte_t *ptep, unsigned long addr,return uvcb->rc ==0x10a ? -ENXIO : -EINVAL ; } +static bool should_export_before_import (struct uv_cb_header *uvcb,struct mm_struct *mm) +{ +return uvcb->cmd !=UVC_CMD_UNPIN_PAGE_SHARED && +atomic_read (&mm->context.protected_count) >1 ; +} +/* * Requests the Ultravisor to make a page accessible to a guest. * If it's brought in the first time, it will be cleared. If
@@ -277,6 +283,8 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)lock_page (page); ptep =get_locked_pte (gmap->mm, uaddr, &ptelock); +if (should_export_before_import (uvcb, gmap->mm)) +uv_convert_from_secure (page_to_phys (page)); rc =make_secure_pte (ptep, uaddr, page, uvcb);pte_unmap_unlock (ptep, ptelock);unlock_page (page);
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 7/17 KVM: s390: pv: module parameter to fence lazy destroy
In reply to Claudio ImbrendaAdd the module parameter "lazy_destroy", to allow the asynchronous destroy mechanism to be switched off. This might be useful for debugging purposes.
The parameter is enabled by default.
--- arch/s390/kvm/kvm-s390.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/s390/kvm/kvm-s390.c +5 -0
@@ -207,6 +207,11 @@ unsigned int diag9c_forwarding_hz;module_param (diag9c_forwarding_hz, uint,0644 );MODULE_PARM_DESC (diag9c_forwarding_hz,"Maximum diag9c forwarding per second, 0 to turn off" ); +/* allow asynchronous deinit for protected guests */ +static int lazy_destroy =1 ; +module_param (lazy_destroy, int,0444 ); +MODULE_PARM_DESC (lazy_destroy,"Asynchronous destroy for protected guests" ); +/* * For now we handle at most 16 double words as this is what the s390 base * kernel handles and stores in the prefix page. If we ever need to go beyond
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 8/17 KVM: s390: pv: make kvm_s390_cpus_from_pv global
In reply to Claudio ImbrendaThe functions kvm_s390_cpus_from_pv needs to be called from pv.c, so make it global.
Take the opportunity to add documentation.
--- arch/s390/kvm/kvm-s390.c | 26 +++++++++++++++++++++++++- arch/s390/kvm/kvm-s390.h | 1 + 2 files changed, 26 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +25 -1
@@ -2175,7 +2175,20 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,return r; } -static int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp) +/** +* kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to +* non protected. +* @kvm the VM whose protected vCPUs are to be converted +* @rcp return value for the RC field of the UVC (in case of error) +* @rrcp return value for the RRC field of the UVC (in case of error) +* +* Does not stop in case of error, tries to convert as many +* CPUs as possible. In case of error, the RC and RRC of the last error are +* returned. +* +* Return: 0 in case of success, otherwise -EIO +*/ +int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp) {struct kvm_vcpu *vcpu;u16 rc, rrc;
@@ -2202,6 +2215,17 @@ static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)return ret; } +/** +* kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM +* to protected. +* @kvm the VM whose protected vCPUs are to be converted +* @rcp return value for the RC field of the UVC (in case of error) +* @rrcp return value for the RRC field of the UVC (in case of error) +* +* Tries to undo the conversion in case of error. +* +* Return: 0 in case of success, otherwise -EIO +*/ static int kvm_s390_cpus_to_pv (struct kvm *kvm,u16 *rc,u16 *rrc) {unsigned long i;
arch/s390/kvm/kvm-s390.h +1 -0
@@ -365,6 +365,7 @@ int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);void kvm_s390_vcpu_unsetup_cmma (struct kvm_vcpu *vcpu);void kvm_s390_set_cpu_timer (struct kvm_vcpu *vcpu,__u64 cputm);__u64 kvm_s390_get_cpu_timer (struct kvm_vcpu *vcpu); +int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp);/* implemented in diag.c */ int kvm_s390_handle_diag (struct kvm_vcpu *vcpu);
Signature · 1 line
2.34.1
Re: [PATCH v7 08/17] KVM: s390: pv: make kvm_s390_cpus_from_pv global
Quoted 2 lines · Claudio Imbrenda
The functions
kvm_s390_cpus_from_pvneeds to be called frompv.c, so make it global.
KVM: s390: pv: Add kvm_s390_cpus_from_pv to kvm-s390.h and provide documentation
Future changes make it necessary to call this function from pv.c.
While we're add it let's properly document kvm_s390_cpus_from_pv() and kvm_s390_cpus_to_pv().
Also could you swap patches 8 and 9 so this one is closer to patch #10?
Quoted 66 lines
Take the opportunity to add documentation.
Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.c | 26 +++++++++++++++++++++++++- arch/s390/kvm/kvm-s390.h | 1 + 2 files changed, 26 insertions(+), 1 deletion(-)arch/s390/kvm/kvm-s390.c +25
-1@@ -2175,7 +2175,20 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,return r; } -static int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp) +/** +* kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to +* non protected. +* @kvm the VM whose protected vCPUs are to be converted +* @rcp return value for the RC field of the UVC (in case of error) +* @rrcp return value for the RRC field of the UVC (in case of error) +* +* Does not stop in case of error, tries to convert as many +* CPUs as possible. In case of error, the RC and RRC of the last error are +* returned. +* +* Return: 0 in case of success, otherwise -EIO +*/ +int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp) {struct kvm_vcpu *vcpu;u16 rc, rrc;@@ -2202,6 +2215,17 @@ static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)return ret; } +/** +* kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM +* to protected. +* @kvm the VM whose protected vCPUs are to be converted +* @rcp return value for the RC field of the UVC (in case of error) +* @rrcp return value for the RRC field of the UVC (in case of error) +* +* Tries to undo the conversion in case of error. +* +* Return: 0 in case of success, otherwise -EIO +*/ static int kvm_s390_cpus_to_pv (struct kvm *kvm,u16 *rc,u16 *rrc) {unsigned long i;arch/s390/kvm/kvm-s390.h +1
-0@@ -365,6 +365,7 @@ int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);void kvm_s390_vcpu_unsetup_cmma (struct kvm_vcpu *vcpu);void kvm_s390_set_cpu_timer (struct kvm_vcpu *vcpu,__u64 cputm);__u64 kvm_s390_get_cpu_timer (struct kvm_vcpu *vcpu); +int kvm_s390_cpus_from_pv (struct kvm *kvm,u16 *rcp,u16 *rrcp);/* implemented in diag.c */ int kvm_s390_handle_diag (struct kvm_vcpu *vcpu);
Claudio Imbrenda
maintainerPATCH v7 9/17 KVM: s390: pv: clear the state without memset
In reply to Claudio ImbrendaDo not use memset to clean the whole struct kvm_s390_pv; instead, explicitly clear the fields that need to be cleared.
Upcoming patches will introduce new fields in the struct kvm_s390_pv that will not need to be cleared.
--- arch/s390/kvm/pv.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/pv.c +10 -1
@@ -16,6 +16,15 @@#include <linux/sched/mm.h> #include "kvm-s390.h" +static void kvm_s390_clear_pv_state (struct kvm *kvm) +{ + kvm->arch.pv.handle =0 ; + kvm->arch.pv.guest_len =0 ; + kvm->arch.pv.stor_base =0 ; + kvm->arch.pv.stor_var =NULL ; + kvm->arch.pv.handle =0 ; +} +int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc) {int cc;
@@ -110,7 +119,7 @@ static void kvm_s390_pv_dealloc_vm(struct kvm *kvm)vfree (kvm->arch.pv.stor_var);free_pages (kvm->arch.pv.stor_base,get_order (uv_info.guest_base_stor_len)); -memset (&kvm->arch.pv,0 ,sizeof (kvm->arch.pv)); +kvm_s390_clear_pv_state (kvm); }static int kvm_s390_pv_alloc_vm (struct kvm *kvm)
Signature · 1 line
2.34.1
Re: [PATCH v7 09/17] KVM: s390: pv: clear the state without memset
Quoted 16 lines · Claudio Imbrenda
Do not use memset to clean the whole struct
kvm_s390_pv; instead, explicitly clear the fields that need to be cleared.Upcoming patches will introduce new fields in the struct
kvm_s390_pvthat will not need to be cleared.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/pv.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)arch/s390/kvm/pv.c +0
-0
arch/s390/kvm/pv.c line 25
Quoted 10 lines
arch/s390/kvm/pv.c +7
-0@@ -16,6 +16,15 @@#include <linux/sched/mm.h> #include "kvm-s390.h" +static void kvm_s390_clear_pv_state (struct kvm *kvm) +{ + kvm->arch.pv.handle =0 ; + kvm->arch.pv.guest_len =0 ; + kvm->arch.pv.stor_base =0 ; + kvm->arch.pv.stor_var =NULL ; + kvm->arch.pv.handle =0 ;
You really want to make sure the handle is zero :)
With this fixed:
Quoted 15 lines
arch/s390/kvm/pv.c +3
-1+} +int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc) {int cc;@@ -110,7 +119,7 @@ static void kvm_s390_pv_dealloc_vm(struct kvm *kvm)vfree (kvm->arch.pv.stor_var);free_pages (kvm->arch.pv.stor_base,get_order (uv_info.guest_base_stor_len)); -memset (&kvm->arch.pv,0 ,sizeof (kvm->arch.pv)); +kvm_s390_clear_pv_state (kvm); }static int kvm_s390_pv_alloc_vm (struct kvm *kvm)
Claudio Imbrenda
maintainerPATCH v7 10/17 KVM: s390: pv: add mmu_notifier
In reply to Claudio ImbrendaAdd an mmu_notifier for protected VMs. The callback function is triggered when the mm is torn down, and will attempt to convert all protected vCPUs to non-protected. This allows the mm teardown to use the destroy page UVC instead of export.
--- arch/s390/include/asm/kvm_host.h | 2 ++ arch/s390/kvm/pv.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)
arch/s390/include/asm/kvm_host.h +2 -0
@@ -19,6 +19,7 @@#include <linux/kvm.h> #include <linux/seqlock.h> #include <linux/module.h> +#include <linux/mmu_notifier.h> #include <asm/debug.h> #include <asm/cpu.h> #include <asm/fpu/api.h>
@@ -921,6 +922,7 @@ struct kvm_s390_pv {u64 guest_len;unsigned long stor_base;void *stor_var; +struct mmu_notifier mmu_notifier; };struct kvm_arch {
arch/s390/kvm/pv.c +20 -0
@@ -193,6 +193,21 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; } +static void kvm_s390_pv_mmu_notifier_release (struct mmu_notifier *subscription, +struct mm_struct *mm) +{ + struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier); +u16 dummy; + +mutex_lock (&kvm->lock); +kvm_s390_cpus_from_pv (kvm, &dummy, &dummy); +mutex_unlock (&kvm->lock); +} + +static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = { + .release = kvm_s390_pv_mmu_notifier_release, +}; +int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc) {struct uv_cb_cgc uvcb = {
@@ -234,6 +249,11 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; } kvm->arch.gmap->guest_handle = uvcb.guest_handle; +/* Add the notifier only once. No races because we hold kvm->lock */ +if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) { + kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops; +mmu_notifier_register (&kvm->arch.pv.mmu_notifier, kvm->mm); + }return 0 ; }
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v7 10/17] KVM: s390: pv: add mmu_notifier
I love your patch! Yet something to improve:
Hi Claudio,
I love your patch! Yet something to improve:
[auto build test ERROR on kvm/queue]
[also build test ERROR on v5.17-rc2 next-20220204]
[cannot apply to kvms390/next s390/features]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: s390-randconfig-r044-20220131 (https://download.01.org/0day-ci/archive/20220205/202202050525.5A9HirW8-lkp(a)intel.com/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project a73e4ce6a59b01f0e37037761c1e6889d539d233)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/0day-ci/linux/commit/9ee65f25ad996d38f6935360c99a89e72024174b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 git checkout 9ee65f25ad996d38f6935360c99a89e72024174b
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash arch/s390/kvm/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from arch/s390/kvm/pv.c:9:
In file included from include/linux/kvm_host.h:41:
In file included from include/linux/kvm_para.h:5:
In file included from include/uapi/linux/kvm_para.h:37:
In file included from arch/s390/include/asm/kvm_para.h:25:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:464:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:477:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
^
include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
#define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
^
In file included from arch/s390/kvm/pv.c:9:
In file included from include/linux/kvm_host.h:41:
In file included from include/linux/kvm_para.h:5:
In file included from include/uapi/linux/kvm_para.h:37:
In file included from arch/s390/include/asm/kvm_para.h:25:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:490:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
^
include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
#define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
^
In file included from arch/s390/kvm/pv.c:9:
In file included from include/linux/kvm_host.h:41:
In file included from include/linux/kvm_para.h:5:
In file included from include/uapi/linux/kvm_para.h:37:
In file included from arch/s390/include/asm/kvm_para.h:25:
In file included from arch/s390/include/asm/diag.h:12:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:31:
In file included from include/linux/dma-mapping.h:10:
In file included from include/linux/scatterlist.h:9:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:501:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:511:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:521:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:609:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:617:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:625:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:634:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:643:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:652:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
>> arch/s390/kvm/pv.c:255:3: error: implicit declaration of function 'mmu_notifier_register' [-Werror,-Wimplicit-function-declaration]
mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
^
arch/s390/kvm/pv.c:255:3: note: did you mean 'mmu_notifier_release'?
include/linux/mmu_notifier.h:679:20: note: 'mmu_notifier_release' declared here
static inline void mmu_notifier_release(struct mm_struct *mm)
^12 warnings and 1 error generated.
vim +/mmu_notifier_register +255 arch/s390/kvm/pv.c
210
211 int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
212 {
213 struct uv_cb_cgc uvcb = {
214 .header.cmd = UVC_CMD_CREATE_SEC_CONF,
215 .header.len = sizeof(uvcb)
216 };
217 int cc, ret;
218 u16 dummy;
219
220 ret = kvm_s390_pv_alloc_vm(kvm);
221 if (ret)
222 return ret;
223
224 /* Inputs */
225 uvcb.guest_stor_origin = 0; /* MSO is 0 for KVM */
226 uvcb.guest_stor_len = kvm->arch.pv.guest_len;
227 uvcb.guest_asce = kvm->arch.gmap->asce;
228 uvcb.guest_sca = (unsigned long)kvm->arch.sca;
229 uvcb.conf_base_stor_origin = (u64)kvm->arch.pv.stor_base;
230 uvcb.conf_virt_stor_origin = (u64)kvm->arch.pv.stor_var;
231
232 cc = uv_call_sched(0, (u64)&uvcb);
233 *rc = uvcb.header.rc;
234 *rrc = uvcb.header.rrc;
235 KVM_UV_EVENT(kvm, 3, "PROTVIRT CREATE VM: handle %llx len %llx rc %x rrc %x",
236 uvcb.guest_handle, uvcb.guest_stor_len, *rc, *rrc);
237
238 /* Outputs */
239 kvm->arch.pv.handle = uvcb.guest_handle;
240
241 atomic_inc(&kvm->mm->context.protected_count);
242 if (cc) {
243 if (uvcb.header.rc & UVC_RC_NEED_DESTROY) {
244 kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
245 } else {
246 atomic_dec(&kvm->mm->context.protected_count);
247 kvm_s390_pv_dealloc_vm(kvm);
248 }
249 return -EIO;
250 }
251 kvm->arch.gmap->guest_handle = uvcb.guest_handle;
252 /* Add the notifier only once. No races because we hold kvm->lock */
253 if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
254 kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
> 255 mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
256 }
257 return 0;
258 }
259
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH v7 10/17] KVM: s390: pv: add mmu_notifier
In reply to Claudio ImbrendaI love your patch! Yet something to improve:
Hi Claudio,
I love your patch! Yet something to improve:
[auto build test ERROR on kvm/queue]
[also build test ERROR on v5.17-rc2 next-20220204]
[cannot apply to kvms390/next s390/features]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: s390-randconfig-r044-20220203 (https://download.01.org/0day-ci/archive/20220205/202202050519.HMLLILGz-lkp(a)intel.com/config) compiler: s390-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/9ee65f25ad996d38f6935360c99a89e72024174b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Claudio-Imbrenda/KVM-s390-pv-implement-lazy-destroy-for-reboot/20220204-235609 git checkout 9ee65f25ad996d38f6935360c99a89e72024174b
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash arch/s390/kvm/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/s390/kvm/pv.c: In function 'kvm_s390_pv_init_vm':
>> arch/s390/kvm/pv.c:255:17: error: implicit declaration of function 'mmu_notifier_register'; did you mean 'mmu_notifier_release'? [-Werror=implicit-function-declaration]
255 | mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
| ^~~~~~~~~~~~~~~~~~~~~
| mmu_notifier_releasecc1: some warnings being treated as errors
vim +255 arch/s390/kvm/pv.c
210
211 int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
212 {
213 struct uv_cb_cgc uvcb = {
214 .header.cmd = UVC_CMD_CREATE_SEC_CONF,
215 .header.len = sizeof(uvcb)
216 };
217 int cc, ret;
218 u16 dummy;
219
220 ret = kvm_s390_pv_alloc_vm(kvm);
221 if (ret)
222 return ret;
223
224 /* Inputs */
225 uvcb.guest_stor_origin = 0; /* MSO is 0 for KVM */
226 uvcb.guest_stor_len = kvm->arch.pv.guest_len;
227 uvcb.guest_asce = kvm->arch.gmap->asce;
228 uvcb.guest_sca = (unsigned long)kvm->arch.sca;
229 uvcb.conf_base_stor_origin = (u64)kvm->arch.pv.stor_base;
230 uvcb.conf_virt_stor_origin = (u64)kvm->arch.pv.stor_var;
231
232 cc = uv_call_sched(0, (u64)&uvcb);
233 *rc = uvcb.header.rc;
234 *rrc = uvcb.header.rrc;
235 KVM_UV_EVENT(kvm, 3, "PROTVIRT CREATE VM: handle %llx len %llx rc %x rrc %x",
236 uvcb.guest_handle, uvcb.guest_stor_len, *rc, *rrc);
237
238 /* Outputs */
239 kvm->arch.pv.handle = uvcb.guest_handle;
240
241 atomic_inc(&kvm->mm->context.protected_count);
242 if (cc) {
243 if (uvcb.header.rc & UVC_RC_NEED_DESTROY) {
244 kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
245 } else {
246 atomic_dec(&kvm->mm->context.protected_count);
247 kvm_s390_pv_dealloc_vm(kvm);
248 }
249 return -EIO;
250 }
251 kvm->arch.gmap->guest_handle = uvcb.guest_handle;
252 /* Add the notifier only once. No races because we hold kvm->lock */
253 if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
254 kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
> 255 mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
256 }
257 return 0;
258 }
259
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Janosch Frank
maintainerRe: [PATCH v7 10/17] KVM: s390: pv: add mmu_notifier
In reply to Claudio ImbrendaQuoted 36 lines · Claudio Imbrenda
Add an
mmu_notifierfor protected VMs. The callback function is triggered when the mm is torn down, and will attempt to convert all protected vCPUs to non-protected. This allows the mm teardown to use the destroy page UVC instead of export.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/include/asm/kvm_host.h | 2 ++ arch/s390/kvm/pv.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)arch/s390/include/asm/kvm_host.h +2
-0@@ -19,6 +19,7 @@#include <linux/kvm.h> #include <linux/seqlock.h> #include <linux/module.h> +#include <linux/mmu_notifier.h> #include <asm/debug.h> #include <asm/cpu.h> #include <asm/fpu/api.h> @@ -921,6 +922,7 @@ struct kvm_s390_pv {u64 guest_len;unsigned long stor_base;void *stor_var; +struct mmu_notifier mmu_notifier; };struct kvm_arch {arch/s390/kvm/pv.c +0
-0
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 199
Quoted 7 lines
arch/s390/kvm/pv.c +4
-0@@ -193,6 +193,21 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; } +static void kvm_s390_pv_mmu_notifier_release (struct mmu_notifier *subscription, +struct mm_struct *mm) +{ + struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);
Are we sure that the kvm pointer is still valid at this point?
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 202
Quoted 3 lines
arch/s390/kvm/pv.c +3
-0+u16 dummy; + +mutex_lock (&kvm->lock);
Against what are we locking here?
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 203
Quoted 1 line
arch/s390/kvm/pv.c +1
-0+kvm_s390_cpus_from_pv (kvm, &dummy, &dummy);
I'd guess that we can't really have a second kvm_s390_cpus_from_pv() call in flight, right? If the mm is being torn down there would be no code left that can execute the IOCTL.
Quoted 23 lines
arch/s390/kvm/pv.c +12
-0+mutex_unlock (&kvm->lock); +} + +static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = { + .release = kvm_s390_pv_mmu_notifier_release, +}; +int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc) {struct uv_cb_cgc uvcb = {@@ -234,6 +249,11 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; } kvm->arch.gmap->guest_handle = uvcb.guest_handle; +/* Add the notifier only once. No races because we hold kvm->lock */ +if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) { + kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops; +mmu_notifier_register (&kvm->arch.pv.mmu_notifier, kvm->mm); + }return 0 ; }
Claudio Imbrenda
maintainerRe: [PATCH v7 10/17] KVM: s390: pv: add mmu_notifier
Quoted 40 lines · On Mon, 7 Feb 2022 12:04:56 +0100 Janosch Frank <[email protected]> wrote:
Quoted 39 lines · Claudio Imbrenda
Add an
mmu_notifierfor protected VMs. The callback function is triggered when the mm is torn down, and will attempt to convert all protected vCPUs to non-protected. This allows the mm teardown to use the destroy page UVC instead of export.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/include/asm/kvm_host.h | 2 ++ arch/s390/kvm/pv.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)arch/s390/include/asm/kvm_host.h +2
-0@@ -19,6 +19,7 @@#include <linux/kvm.h> #include <linux/seqlock.h> #include <linux/module.h> +#include <linux/mmu_notifier.h> #include <asm/debug.h> #include <asm/cpu.h> #include <asm/fpu/api.h> @@ -921,6 +922,7 @@ struct kvm_s390_pv {u64 guest_len;unsigned long stor_base;void *stor_var; +struct mmu_notifier mmu_notifier; };struct kvm_arch {arch/s390/kvm/pv.c +0
-0@@ -193,6 +193,21 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; }
Quoted 6 lines
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 199
Quoted 4 lines
arch/s390/kvm/pv.c +4
-0+static void kvm_s390_pv_mmu_notifier_release (struct mmu_notifier *subscription, +struct mm_struct *mm) +{ + struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);Are we sure that the kvm pointer is still valid at this point?
it should be, because KVM is torn down after the mm. which is the whole reason why the notifier is needed.
on the other hand, I realized that I should have unregistered the notifier somewhere, which I forgot to do. the best place would be KVM teardown, which then also guarantees that the notifier can only be called with a valid struct kvm
Quoted 7 lines
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 202
Quoted 4 lines
arch/s390/kvm/pv.c +3
-0+u16 dummy; + +mutex_lock (&kvm->lock);Against what are we locking here?
Quoted 5 lines
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 203
Quoted 1 line
arch/s390/kvm/pv.c +1
-0+kvm_s390_cpus_from_pv (kvm, &dummy, &dummy);I'd guess that we can't really have a second
kvm_s390_cpus_from_pv()call in flight, right? If the mm is being torn down there would be no code left that can execute the IOCTL.
yeah makes sense
Quoted 25 lines
Quoted 24 lines
arch/s390/kvm/pv.c +12
-0+mutex_unlock (&kvm->lock); +} + +static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = { + .release = kvm_s390_pv_mmu_notifier_release, +}; +int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc) {struct uv_cb_cgc uvcb = {@@ -234,6 +249,11 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return -EIO ; } kvm->arch.gmap->guest_handle = uvcb.guest_handle; +/* Add the notifier only once. No races because we hold kvm->lock */ +if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) { + kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops; +mmu_notifier_register (&kvm->arch.pv.mmu_notifier, kvm->mm); + }return 0 ; }
Claudio Imbrenda
maintainerPATCH v7 11/17 s390/mm: KVM: pv: when tearing down, try to destroy protected pages
In reply to Claudio ImbrendaWhen ptep_get_and_clear_full is called for a mm teardown, we will now attempt to destroy the secure pages. This will be faster than export.
In case it was not a teardown, or if for some reason the destroy page UVC failed, we try with an export page, like before.
--- arch/s390/include/asm/pgtable.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
arch/s390/include/asm/pgtable.h +15 -3
@@ -1118,9 +1118,21 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, }else { res =ptep_xchg_lazy (mm, addr, ptep,__pte (_PAGE_INVALID)); } -/* At this point the reference through the mapping is still present */ -if (mm_is_protected (mm) &&pte_present (res)) -uv_convert_owned_from_secure (pte_val (res) &PAGE_MASK ); +/* Nothing to do */ +if (!mm_is_protected (mm) || !pte_present (res)) +return res; +/* +* At this point the reference through the mapping is still present. +* The notifier should have destroyed all protected vCPUs at this +* point, so the destroy should be successful. +*/ +if (full && !uv_destroy_owned_page (pte_val (res) &PAGE_MASK )) +return res; +/* +* But if something went wrong and the pages could not be destroyed, +* the slower export is used as fallback instead. +*/ +uv_convert_owned_from_secure (pte_val (res) &PAGE_MASK );return res; }
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 12/17 KVM: s390: pv: refactoring of kvm_s390_pv_deinit_vm
In reply to Claudio ImbrendaRefactor kvm_s390_pv_deinit_vm to improve readability and simplify the improvements that are coming in subsequent patches.
No functional change intended.
[note: this can potentially be squashed into the next patch, I factored it out to simplify the review process]
--- arch/s390/kvm/pv.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
arch/s390/kvm/pv.c +8 -8
@@ -180,17 +180,17 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); -if (!cc) -atomic_dec (&kvm->mm->context.protected_count); -KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc); -WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); -/* Intended memory leak on "impossible" error */ if (!cc) { +atomic_dec (&kvm->mm->context.protected_count);kvm_s390_pv_dealloc_vm (kvm); -return 0 ; + }else { +/* Intended memory leak on "impossible" error */ +s390_replace_asce (kvm->arch.gmap); } -s390_replace_asce (kvm->arch.gmap); -return -EIO ; +KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc); +WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); + +return cc ? -EIO :0 ; } static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,
Signature · 1 line
2.34.1
Re: [PATCH v7 12/17] KVM: s390: pv: refactoring of kvm_s390_pv_deinit_vm
Quoted 8 lines · Claudio Imbrenda
Refactor
kvm_s390_pv_deinit_vmto improve readability and simplify the improvements that are coming in subsequent patches.No functional change intended.
[note: this can potentially be squashed into the next patch, I factored it out to simplify the review process]
Quoted 36 lines
Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/pv.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)arch/s390/kvm/pv.c +8
-8@@ -180,17 +180,17 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) cc =uv_cmd_nodata (kvm_s390_pv_get_handle (kvm),UVC_CMD_DESTROY_SEC_CONF , rc, rrc);WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); -if (!cc) -atomic_dec (&kvm->mm->context.protected_count); -KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc); -WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); -/* Intended memory leak on "impossible" error */ if (!cc) { +atomic_dec (&kvm->mm->context.protected_count);kvm_s390_pv_dealloc_vm (kvm); -return 0 ; + }else { +/* Intended memory leak on "impossible" error */ +s390_replace_asce (kvm->arch.gmap); } -s390_replace_asce (kvm->arch.gmap); -return -EIO ; +KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM: rc %x rrc %x" , *rc, *rrc); +WARN_ONCE (cc,"protvirt destroy vm failed rc %x rrc %x" , *rc, *rrc); + +return cc ? -EIO :0 ; } static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,
Claudio Imbrenda
maintainerPATCH v7 13/17 KVM: s390: pv: cleanup leftover protected VMs if needed
In reply to Claudio ImbrendaIn upcoming patches it will be possible to start tearing down a protected VM, and finish the teardown concurrently in a different thread.
Protected VMs that are pending for tear down ("leftover") need to be cleaned properly when the userspace process (e.g. qemu) terminates.
This patch makes sure that all "leftover" protected VMs are always properly torn down.
--- arch/s390/include/asm/kvm_host.h | 2 + arch/s390/kvm/kvm-s390.c | 1 + arch/s390/kvm/pv.c | 69 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+)
arch/s390/include/asm/kvm_host.h +2 -0
@@ -922,6 +922,8 @@ struct kvm_s390_pv {u64 guest_len;unsigned long stor_base;void *stor_var; +void *async_deinit; +struct list_head need_cleanup;struct mmu_notifier mmu_notifier; };
arch/s390/kvm/kvm-s390.c +1 -0
@@ -2787,6 +2787,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)kvm_s390_vsie_init (kvm);if (use_gisa)kvm_s390_gisa_init (kvm); +INIT_LIST_HEAD (&kvm->arch.pv.need_cleanup);KVM_EVENT (3 ,"vm 0x%pK created by pid %u" , kvm, current->pid);return 0 ;
arch/s390/kvm/pv.c +69 -0
87 lines not on this page · raw message
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 14/17 KVM: s390: pv: asynchronous destroy for reboot
In reply to Claudio ImbrendaUntil now, destroying a protected guest was an entirely synchronous operation that could potentially take a very long time, depending on the size of the guest, due to the time needed to clean up the address space from protected pages.
This patch implements an asynchronous destroy mechanism, that allows a protected guest to reboot significantly faster than previously.
This is achieved by clearing the pages of the old guest in background. In case of reboot, the new guest will be able to run in the same address space almost immediately.
The old protected guest is then only destroyed when all of its memory has been destroyed or otherwise made non protected.
Two new PV commands are added for the KVM_S390_PV_COMMAND ioctl:
KVM_PV_ASYNC_DISABLE_PREPARE: prepares the current protected VM for asynchronous teardown. The current VM will then continue immediately as non-protected. If a protected VM had already been set aside without starting the teardown process, this call will fail.
KVM_PV_ASYNC_DISABLE: tears down the protected VM previously set aside for asynchronous teardown. This PV command should ideally be issued by userspace from a separate thread. If a fatal signal is received (or the process terminates naturally), the command will terminate immediately without completing.
Leftover protected VMs are cleaned up when a KVM VM is torn down normally (either via IOCTL or when the process terminates); this cleanup has been implemented in a previous patch.
--- arch/s390/kvm/kvm-s390.c | 24 ++++++++ arch/s390/kvm/kvm-s390.h | 2 + arch/s390/kvm/pv.c | 126 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 2 + 4 files changed, 154 insertions(+)
arch/s390/kvm/kvm-s390.c +24 -0
@@ -2284,6 +2284,30 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)set_bit (IRQ_PEND_EXT_SERVICE , &kvm->arch.float_int.masked_irqs);break ; } +case KVM_PV_ASYNC_DISABLE_PREPARE : + r = -EINVAL ; +if (!kvm_s390_pv_is_protected (kvm) || !lazy_destroy) +break ; + + r =kvm_s390_cpus_from_pv (kvm, &cmd->rc, &cmd->rrc); +/* +* If a CPU could not be destroyed, destroy VM will also fail. +* There is no point in trying to destroy it. Instead return +* the rc and rrc from the first CPU that failed destroying. +*/ +if (r) +break ; + r =kvm_s390_pv_deinit_vm_async_prepare (kvm, &cmd->rc, &cmd->rrc); + +/* no need to block service interrupts any more */ +clear_bit (IRQ_PEND_EXT_SERVICE , &kvm->arch.float_int.masked_irqs); +break ; +case KVM_PV_ASYNC_DISABLE : + r = -EINVAL ; +if (!kvm->arch.pv.async_deinit) +break ; + r =kvm_s390_pv_deinit_vm_async (kvm, &cmd->rc, &cmd->rrc); +break ;case KVM_PV_DISABLE : { r = -EINVAL ;if (!kvm_s390_pv_is_protected (kvm))
arch/s390/kvm/kvm-s390.h +2 -0
@@ -234,6 +234,8 @@ static inline unsigned long kvm_s390_get_gfn_end(struct kvm_memslots *slots)/* implemented in pv.c */ int kvm_s390_pv_destroy_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc);int kvm_s390_pv_create_cpu (struct kvm_vcpu *vcpu,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_vm_async_prepare (struct kvm *kvm,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_vm_async (struct kvm *kvm,u16 *rc,u16 *rrc);int kvm_s390_pv_deinit_vm (struct kvm *kvm,u16 *rc,u16 *rrc);int kvm_s390_pv_init_vm (struct kvm *kvm,u16 *rc,u16 *rrc); int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc,
arch/s390/kvm/pv.c +126 -0
132 lines not on this page · raw message
include/uapi/linux/kvm.h +2 -0
@@ -1646,6 +1646,8 @@ enum pv_cmd_id {KVM_PV_VERIFY ,KVM_PV_PREP_RESET ,KVM_PV_UNSHARE_ALL , +KVM_PV_ASYNC_DISABLE_PREPARE , + KVM_PV_ASYNC_DISABLE, }; struct kvm_pv_cmd {
Signature · 1 line
2.34.1
Claudio Imbrenda
maintainerPATCH v7 15/17 KVM: s390: pv: api documentation for asynchronous destroy
In reply to Claudio ImbrendaAdd documentation for the new commands added to the KVM_S390_PV_COMMAND ioctl.
--- Documentation/virt/kvm/api.rst | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +18 -3
@@ -5010,11 +5010,13 @@ KVM_PV_ENABLE ===== ============================= KVM_PV_DISABLE - Deregister the VM from the Ultravisor and reclaim the memory that had been donated to the Ultravisor, making it usable by the kernel - again. All registered VCPUs are converted back to non-protected - ones. + again. All registered VCPUs are converted back to non-protected + ones. If a previous VM had been prepared for asynchonous teardown + with KVM_PV_ASYNC_DISABLE_PREPARE and not actually torn down with + KVM_PV_ASYNC_DISABLE, it will be torn down in this call together with + the current VM. KVM_PV_VM_SET_SEC_PARMS Pass the image header from VM memory to the Ultravisor in
@@ -5027,6 +5029,19 @@ KVM_PV_VM_VERIFY Verify the integrity of the unpacked image. Only if this succeeds, KVM is allowed to start protected VCPUs. +KVM_PV_ASYNC_DISABLE_PREPARE + Prepare the current protected VM for asynchronous teardown. The current + VM will then continue immediately as non-protected. If a protected VM had + already been set aside without starting the teardown process, this call + will fail. In this case the userspace process should issue a normal + KVM_PV_DISABLE. + +KVM_PV_ASYNC_DISABLE + Tear down the protected VM previously set aside for asynchronous teardown. + This PV command should ideally be issued by userspace from a separate + thread. If a fatal signal is received (or the process terminates + naturally), the command will terminate immediately without completing. + 4.126 KVM_X86_SET_MSR_FILTER ----------------------------
Signature · 1 line
2.34.1
Janosch Frank
maintainerRe: [PATCH v7 15/17] KVM: s390: pv: api documentation for asynchronous destroy
Quoted 30 lines · Claudio Imbrenda
Add documentation for the new commands added to the
KVM_S390_PV_COMMANDioctl.Signed-off-by Claudio Imbrenda <[email protected]>--- Documentation/virt/kvm/api.rst | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)Documentation/virt/kvm/api.rst +5
-3@@ -5010,11 +5010,13 @@ KVM_PV_ENABLE ===== ============================= KVM_PV_DISABLE - Deregister the VM from the Ultravisor and reclaim the memory that had been donated to the Ultravisor, making it usable by the kernel - again. All registered VCPUs are converted back to non-protected - ones. + again. All registered VCPUs are converted back to non-protected + ones. If a previous VM had been prepared for asynchonous teardown + with KVM_PV_ASYNC_DISABLE_PREPARE and not actually torn down with + KVM_PV_ASYNC_DISABLE, it will be torn down in this call together with + the current VM. KVM_PV_VM_SET_SEC_PARMS Pass the image header from VM memory to the Ultravisor in
Documentation/virt/kvm/api.rst KVM_PV_VM_VERIFY · line 5033
Quoted 5 lines
Documentation/virt/kvm/api.rst +2
-0@@ -5027,6 +5029,19 @@ KVM_PV_VM_VERIFY Verify the integrity of the unpacked image. Only if this succeeds, KVM is allowed to start protected VCPUs. +KVM_PV_ASYNC_DISABLE_PREPARE + Prepare the current protected VM for asynchronous teardown. The current
I think the first sentence needs a few more examples of what we do so the second sentence makes more sense.
...by setting aside the pointers to the donated storage, replacing the top most page table, destroying the first 2GB of memory and zeroing the KVM PV structs.
Or something which sounds a bit nicer.
Quoted 15 lines
Documentation/virt/kvm/api.rst +11
-0+ VM will then continue immediately as non-protected. If a protected VM had + already been set aside without starting the teardown process, this call + will fail. In this case the userspace process should issue a normal + KVM_PV_DISABLE. + +KVM_PV_ASYNC_DISABLE + Tear down the protected VM previously set aside for asynchronous teardown. + This PV command should ideally be issued by userspace from a separate + thread. If a fatal signal is received (or the process terminates + naturally), the command will terminate immediately without completing. + 4.126 KVM_X86_SET_MSR_FILTER ----------------------------
Claudio Imbrenda
maintainerRe: [PATCH v7 15/17] KVM: s390: pv: api documentation for asynchronous destroy
Quoted 40 lines · On Mon, 7 Feb 2022 15:52:37 +0100 Janosch Frank <[email protected]> wrote:
Quoted 30 lines · Claudio Imbrenda
Add documentation for the new commands added to the
KVM_S390_PV_COMMANDioctl.Signed-off-by Claudio Imbrenda <[email protected]>--- Documentation/virt/kvm/api.rst | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)Documentation/virt/kvm/api.rst +5
-3@@ -5010,11 +5010,13 @@ KVM_PV_ENABLE ===== ============================= KVM_PV_DISABLE - Deregister the VM from the Ultravisor and reclaim the memory that had been donated to the Ultravisor, making it usable by the kernel - again. All registered VCPUs are converted back to non-protected - ones. + again. All registered VCPUs are converted back to non-protected + ones. If a previous VM had been prepared for asynchonous teardown + with KVM_PV_ASYNC_DISABLE_PREPARE and not actually torn down with + KVM_PV_ASYNC_DISABLE, it will be torn down in this call together with + the current VM. KVM_PV_VM_SET_SEC_PARMS Pass the image header from VM memory to the Ultravisor inDocumentation/virt/kvm/api.rst KVM_PV_VM_VERIFY · line 5033
Quoted 5 lines
Documentation/virt/kvm/api.rst +2
-0@@ -5027,6 +5029,19 @@ KVM_PV_VM_VERIFY Verify the integrity of the unpacked image. Only if this succeeds, KVM is allowed to start protected VCPUs. +KVM_PV_ASYNC_DISABLE_PREPARE + Prepare the current protected VM for asynchronous teardown. The currentI think the first sentence needs a few more examples of what we do so the second sentence makes more sense.
Quoted 3 lines
...by setting aside the pointers to the donated storage, replacing the top most page table, destroying the first 2GB of memory and zeroing the KVM PV structs.
I'm not sure we should give out implementation details, which might change with newer kernel and/or hardware versions
Quoted 20 lines
Or something which sounds a bit nicer.
Quoted 15 lines
Documentation/virt/kvm/api.rst +11
-0+ VM will then continue immediately as non-protected. If a protected VM had + already been set aside without starting the teardown process, this call + will fail. In this case the userspace process should issue a normal + KVM_PV_DISABLE. + +KVM_PV_ASYNC_DISABLE + Tear down the protected VM previously set aside for asynchronous teardown. + This PV command should ideally be issued by userspace from a separate + thread. If a fatal signal is received (or the process terminates + naturally), the command will terminate immediately without completing. + 4.126 KVM_X86_SET_MSR_FILTER ----------------------------
Claudio Imbrenda
maintainerPATCH v7 16/17 KVM: s390: pv: add KVM_CAP_S390_PROT_REBOOT_ASYNC
In reply to Claudio ImbrendaAdd KVM_CAP_S390_PROT_REBOOT_ASYNC to signal that the KVM_PV_ASYNC_DISABLE and KVM_PV_ASYNC_DISABLE_PREPARE commands for the KVM_S390_PV_COMMAND ioctl are available.
--- arch/s390/kvm/kvm-s390.c | 3 +++ include/uapi/linux/kvm.h | 1 + 2 files changed, 4 insertions(+)
arch/s390/kvm/kvm-s390.c +3 -0
@@ -608,6 +608,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_S390_BPB : r =test_facility (82 );break ; +case KVM_CAP_S390_PROT_REBOOT_ASYNC : + r = lazy_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;
include/uapi/linux/kvm.h +1 -0
@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_S390_PROT_REBOOT_ASYNC 215#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.34.1
Janosch Frank
maintainerRe: [PATCH v7 16/17] KVM: s390: pv: add KVM_CAP_S390_PROT_REBOOT_ASYNC
Quoted 15 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROT_REBOOT_ASYNCto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.c | 3 +++ include/uapi/linux/kvm.h | 1 + 2 files changed, 4 insertions(+)arch/s390/kvm/kvm-s390.c +0
-0
arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 612
Quoted 5 lines
arch/s390/kvm/kvm-s390.c +2
-0@@ -608,6 +608,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_S390_BPB : r =test_facility (82 );break ; +case KVM_CAP_S390_PROT_REBOOT_ASYNC : + r = lazy_destroy &&is_prot_virt_host ();
While reboot might be the best use-case for the async disable I don't think we should name the capability this way.
KVM_CAP_S390_PROTECTED_ASYNC_DESTR ?
It's a bit long but the initial capability didn't abbreviate the protected part so it is what it is.
Quoted 17 lines
arch/s390/kvm/kvm-s390.c +1
-0+break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +1
-0@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_S390_PROT_REBOOT_ASYNC 215#ifdef KVM_CAP_IRQ_ROUTING
Claudio Imbrenda
maintainerRe: [PATCH v7 16/17] KVM: s390: pv: add KVM_CAP_S390_PROT_REBOOT_ASYNC
Quoted 25 lines · On Mon, 7 Feb 2022 15:37:48 +0100 Janosch Frank <[email protected]> wrote:
Quoted 15 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROT_REBOOT_ASYNCto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.c | 3 +++ include/uapi/linux/kvm.h | 1 + 2 files changed, 4 insertions(+)arch/s390/kvm/kvm-s390.c +0
-0arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 612
Quoted 5 lines
arch/s390/kvm/kvm-s390.c +2
-0@@ -608,6 +608,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_S390_BPB : r =test_facility (82 );break ; +case KVM_CAP_S390_PROT_REBOOT_ASYNC : + r = lazy_destroy &&is_prot_virt_host ();While reboot might be the best use-case for the async disable I don't think we should name the capability this way.
Quoted 1 line
KVM_CAP_S390_PROTECTED_ASYNC_DESTR?
then maybe
KVM_CAP_S390_PROTECTED_ASYNC_DISABLE ?
Quoted 23 lines
It's a bit long but the initial capability didn't abbreviate the protected part so it is what it is.
Quoted 17 lines
arch/s390/kvm/kvm-s390.c +1
-0+break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +1
-0@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_S390_PROT_REBOOT_ASYNC 215#ifdef KVM_CAP_IRQ_ROUTING
Janosch Frank
maintainerRe: [PATCH v7 16/17] KVM: s390: pv: add KVM_CAP_S390_PROT_REBOOT_ASYNC
Quoted 28 lines · Claudio Imbrenda
Quoted 25 lines · On Mon, 7 Feb 2022 15:37:48 +0100 Janosch Frank <[email protected]> wrote:
Quoted 15 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROT_REBOOT_ASYNCto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.c | 3 +++ include/uapi/linux/kvm.h | 1 + 2 files changed, 4 insertions(+)arch/s390/kvm/kvm-s390.c +0
-0arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 612
Quoted 5 lines
arch/s390/kvm/kvm-s390.c +2
-0@@ -608,6 +608,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_S390_BPB : r =test_facility (82 );break ; +case KVM_CAP_S390_PROT_REBOOT_ASYNC : + r = lazy_destroy &&is_prot_virt_host ();While reboot might be the best use-case for the async disable I don't think we should name the capability this way.
Quoted 5 lines
Quoted 1 line
KVM_CAP_S390_PROTECTED_ASYNC_DESTR?then maybe
KVM_CAP_S390_PROTECTED_ASYNC_DISABLE?
Sounds good to me
Quoted 25 lines
Quoted 24 lines
It's a bit long but the initial capability didn't abbreviate the protected part so it is what it is.
Quoted 17 lines
arch/s390/kvm/kvm-s390.c +1
-0+break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +1
-0@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_S390_PROT_REBOOT_ASYNC 215#ifdef KVM_CAP_IRQ_ROUTING
Claudio Imbrenda
maintainerPATCH v7 17/17 KVM: s390: pv: avoid export before import if possible
In reply to Claudio ImbrendaIf the appropriate UV feature bit is set, there is no need to perform an export before import.
--- arch/s390/kernel/uv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kernel/uv.c +2 -1
@@ -236,7 +236,8 @@ static int make_secure_pte(pte_t *ptep, unsigned long addr,static bool should_export_before_import (struct uv_cb_header *uvcb,struct mm_struct *mm) { -return uvcb->cmd !=UVC_CMD_UNPIN_PAGE_SHARED && +return !test_bit_inv (BIT_UV_FEAT_MISC , &uv_info.uv_feature_indications) && + uvcb->cmd !=UVC_CMD_UNPIN_PAGE_SHARED &&atomic_read (&mm->context.protected_count) >1 ; }
Signature · 1 line
2.34.1