KVM: s390: pv: implement lazy destroy for reboot
Subsystems (4)
Claudio Imbrenda
maintainerv14 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_CLEANUP_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_CLEANUP_PERFORM: 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 rest of the normal KVM teardown process will take care of properly cleaning up all leftovers.
The idea is that userspace should first issue the KVM_PV_ASYNC_CLEANUP_PREPARE command, and in case of success, create a new thread and issue KVM_PV_ASYNC_CLEANUP_PERFORM 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.
v13->v14
* improve wording of commit messages
* improve wording of documentation
* improve wording of comments
* add if (!async_destroy) check in ioctl handler
* use UVC_RC_EXECUTED macro instead of hardcoded value
* use kzalloc instead of kmalloc with __GFP_ZERO flag
* rebase
v12->v13
* drop the patches that have been already merged
* rebase
Claudio Imbrenda (6):
KVM: s390: pv: asynchronous destroy for reboot
KVM: s390: pv: api documentation for asynchronous destroy
KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
KVM: s390: pv: avoid export before import if possible
KVM: s390: pv: support for Destroy fast UVC
KVM: s390: pv: module parameter to fence asynchronous destroy
Documentation/virt/kvm/api.rst | 37 +++- arch/s390/include/asm/kvm_host.h | 2 + arch/s390/include/asm/uv.h | 10 + arch/s390/kernel/uv.c | 7 + arch/s390/kvm/kvm-s390.c | 58 +++++- arch/s390/kvm/kvm-s390.h | 3 + arch/s390/kvm/pv.c | 331 ++++++++++++++++++++++++++++++- include/uapi/linux/kvm.h | 3 + 8 files changed, 429 insertions(+), 22 deletions(-)
Signature · 1 line
2.37.3
Claudio Imbrenda
maintainerPATCH v14 1/6 KVM: s390: pv: asynchronous destroy for reboot
Until 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_CLEANUP_PREPARE: set aside the current protected VM for later asynchronous teardown. The current KVM VM will then continue immediately as non-protected. If a protected VM had already been set aside for asynchronous teardown, but without starting the teardown process, this call will fail. There can be at most one VM set aside at any time. Once it is set aside, the protected VM only exists in the context of the Ultravisor, it is not associated with the KVM VM anymore. Its protected CPUs have already been destroyed, but not its memory. This command can be issued again immediately after starting KVM_PV_ASYNC_CLEANUP_PERFORM, without having to wait for completion.
KVM_PV_ASYNC_CLEANUP_PERFORM: tears down the protected VM previously set aside using KVM_PV_ASYNC_CLEANUP_PREPARE. Ideally the KVM_PV_ASYNC_CLEANUP_PERFORM PV command should be issued by userspace from a separate thread. If a fatal signal is received (or if the process terminates naturally), the command will terminate immediately without completing. All protected VMs whose teardown was interrupted will be put in the need_cleanup list. The rest of the normal KVM teardown process will take care of properly cleaning up all remaining protected VMs, including the ones on the need_cleanup list.
--- arch/s390/include/asm/kvm_host.h | 2 + arch/s390/kvm/kvm-s390.c | 49 +++++- arch/s390/kvm/kvm-s390.h | 3 + arch/s390/kvm/pv.c | 290 +++++++++++++++++++++++++++++-- include/uapi/linux/kvm.h | 2 + 5 files changed, 328 insertions(+), 18 deletions(-)
arch/s390/include/asm/kvm_host.h +2 -0
@@ -942,6 +942,8 @@ struct kvm_s390_pv {unsigned long stor_base;void *stor_var;bool dumping; +void *set_aside; +struct list_head need_cleanup;struct mmu_notifier mmu_notifier; };
arch/s390/kvm/kvm-s390.c +41 -8
101 lines not on this page · raw message
arch/s390/kvm/kvm-s390.h +3 -0
@@ -243,6 +243,9 @@ static inline u32 kvm_s390_get_gisa_desc(struct kvm *kvm)/* 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_set_aside (struct kvm *kvm,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_aside_vm (struct kvm *kvm,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_cleanup_all (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 +280 -10
315 lines not on this page · raw message
include/uapi/linux/kvm.h +2 -0
@@ -1739,6 +1739,8 @@ enum pv_cmd_id {KVM_PV_UNSHARE_ALL ,KVM_PV_INFO ,KVM_PV_DUMP , +KVM_PV_ASYNC_CLEANUP_PREPARE , + KVM_PV_ASYNC_CLEANUP_PERFORM, }; struct kvm_pv_cmd {
Signature · 1 line
2.37.3
Re: [PATCH v14 1/6] KVM: s390: pv: asynchronous destroy for reboot
Hi Claudio,
LGTM, but I have some nits.
Quoted 38 lines · Claudio Imbrenda
Until 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_COMMANDioctl:
KVM_PV_ASYNC_CLEANUP_PREPARE: set aside the current protected VM for later asynchronous teardown. The current KVM VM will then continue immediately as non-protected. If a protected VM had already been set aside for asynchronous teardown, but without starting the teardown process, this call will fail. There can be at most one VM set aside at any time. Once it is set aside, the protected VM only exists in the context of the Ultravisor, it is not associated with the KVM VM anymore. Its protected CPUs have already been destroyed, but not its memory. This command can be issued again immediately after startingKVM_PV_ASYNC_CLEANUP_PERFORM, without having to wait for completion.
KVM_PV_ASYNC_CLEANUP_PERFORM: tears down the protected VM previously set aside usingKVM_PV_ASYNC_CLEANUP_PREPARE. Ideally theKVM_PV_ASYNC_CLEANUP_PERFORMPV command should be issued by userspace from a separate thread. If a fatal signal is received (or if the process terminates naturally), the command will terminate immediately without completing. All protected VMs whose teardown was interrupted will be put in theneed_cleanuplist. The rest of the normal KVM teardown process will take care of properly cleaning up all remaining protected VMs, including the ones on theneed_cleanuplist.
Quoted 2 lines
Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>
With nits fixed/discussed:
Quoted 26 lines
--- arch/s390/include/asm/kvm_host.h | 2 + arch/s390/kvm/kvm-s390.c | 49 +++++- arch/s390/kvm/kvm-s390.h | 3 + arch/s390/kvm/pv.c | 290 +++++++++++++++++++++++++++++-- include/uapi/linux/kvm.h | 2 + 5 files changed, 328 insertions(+), 18 deletions(-)arch/s390/include/asm/kvm_host.h +2
-0@@ -942,6 +942,8 @@ struct kvm_s390_pv {unsigned long stor_base;void *stor_var;bool dumping; +void *set_aside; +struct list_head need_cleanup;struct mmu_notifier mmu_notifier; };arch/s390/kvm/kvm-s390.c +0
-0
arch/s390/kvm/kvm-s390.c unsigned int diag9c_forwarding_hz; · line 211
Quoted 3 lines
arch/s390/kvm/kvm-s390.c +0
-0@@ -209,6 +209,8 @@ 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" );
IMO it would be better to initialize this variable explicitly here. You do this later in patch [6/6].
Whats the reason to do this not at this point.
arch/s390/kvm/kvm-s390.c static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd, · line 2509
Quoted 10 lines
arch/s390/kvm/kvm-s390.c +3
-0+static int async_destroy; +/* * 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 @@ -2504,9 +2506,13 @@ static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,static int kvm_s390_handle_pv (struct kvm *kvm,struct kvm_pv_cmd *cmd) { +const bool needslock = (cmd->cmd !=KVM_PV_ASYNC_CLEANUP_PERFORM );
need_lock ? or just lock ?
Quoted 162 lines
arch/s390/kvm/kvm-s390.c +38
-8+void __user *argp = (void __user *)cmd->data;int r =0 ;u16 dummy; -void __user *argp = (void __user *)cmd->data; + +if (needslock) +mutex_lock (&kvm->lock);switch (cmd->cmd) {case KVM_PV_ENABLE : {@@ -2540,6 +2546,31 @@ 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_CLEANUP_PREPARE : + r = -EINVAL ; +if (!kvm_s390_pv_is_protected (kvm) || !async_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_set_aside (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_CLEANUP_PERFORM : + r = -EINVAL ; +if (!async_destroy) +break ; +/* kvm->lock must not be held; this is asserted inside the function. */ + r =kvm_s390_pv_deinit_aside_vm (kvm, &cmd->rc, &cmd->rrc); +break ;case KVM_PV_DISABLE : { r = -EINVAL ;if (!kvm_s390_pv_is_protected (kvm))@@ -2553,7 +2584,7 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)*/ if (r)break ; - r =kvm_s390_pv_deinit_vm (kvm, &cmd->rc, &cmd->rrc); + r =kvm_s390_pv_deinit_cleanup_all (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);@@ -2703,6 +2734,9 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)default : r = -ENOTTY ; } +if (needslock) +mutex_unlock (&kvm->lock); +return r; }@@ -2907,9 +2941,8 @@ long kvm_arch_vm_ioctl(struct file *filp, r = -EINVAL ;break ; } -mutex_lock (&kvm->lock); +/* must be called without kvm->lock */ r =kvm_s390_handle_pv (kvm, &args); -mutex_unlock (&kvm->lock);if (copy_to_user (argp, &args,sizeof (args))) { r = -EFAULT ;break ;@@ -3228,6 +3261,8 @@ 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->arch.pv.set_aside =NULL ;KVM_EVENT (3 ,"vm 0x%pK created by pid %u" , kvm, current->pid);return 0 ;@@ -3272,11 +3307,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm)/* * We are already at the end of life and kvm->lock is not taken. * This is ok as the file descriptor is closed by now and nobody -* can mess with the pv state. To avoid lockdep_assert_held from -* complaining we do not use kvm_s390_pv_is_protected. +* can mess with the pv state. */ -if (kvm_s390_pv_get_handle (kvm)) -kvm_s390_pv_deinit_vm (kvm, &rc, &rrc); +kvm_s390_pv_deinit_cleanup_all (kvm, &rc, &rrc);/* * Remove the mmu notifier only when the whole KVM VM is torn down, * and only if one was registered to begin with. If the VM is arch/s390/kvm/kvm-s390.h +3
-0@@ -243,6 +243,9 @@ static inline u32 kvm_s390_get_gisa_desc(struct kvm *kvm)/* 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_set_aside (struct kvm *kvm,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_aside_vm (struct kvm *kvm,u16 *rc,u16 *rrc); +int kvm_s390_pv_deinit_cleanup_all (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 +36
-2@@ -18,6 +18,29 @@#include <linux/mmu_notifier.h> #include "kvm-s390.h" +/** +* struct pv_vm_to_be_destroyed - Represents a protected VM that needs to +* be destroyed +* +* @list: list head for the list of leftover VMs +* @old_gmap_table: the gmap table of the leftover protected VM +* @handle: the handle of the leftover protected VM +* @stor_var: pointer to the variable storage of the leftover protected VM +* @stor_base: address of the base storage of the leftover protected VM +* +* Represents a protected VM that is still registered with the Ultravisor, +* but which does not correspond any longer to an active KVM VM. It should +* be destroyed at some point later, either asynchronously or when the +* process terminates. +*/ +struct pv_vm_to_be_destroyed { +struct list_head list; +unsigned long old_gmap_table; +u64 handle; +void *stor_var; +unsigned long stor_base; +}; +static void kvm_s390_clear_pv_state (struct kvm *kvm) { kvm->arch.pv.handle =0 ;@@ -159,23 +182,149 @@ static int kvm_s390_pv_alloc_vm(struct kvm *kvm)return -ENOMEM ; } -/* this should not fail, but if it does, we must not free the donated memory */ -int kvm_s390_pv_deinit_vm (struct kvm *kvm,u16 *rc,u16 *rrc) +/** +* kvm_s390_pv_dispose_one_leftover - Clean up one leftover protected VM. +* @kvm: the KVM that was associated with this leftover protected VM +* @leftover: details about the leftover protected VM that needs a clean up +* @rc: the RC code of the Destroy Secure Configuration UVC +* @rrc: the RRC code of the Destroy Secure Configuration UVC +* +* Destroy one leftover protected VM. +* On success, kvm->mm->context.protected_count will be decremented atomically +* and all other resources used by the VM will be freed. +* +* Return: 0 in case of success, otherwise 1 +*/
arch/s390/kvm/pv.c static int kvm_s390_pv_alloc_vm(struct kvm *kvm) · line 204
Quoted 10 lines
arch/s390/kvm/pv.c +4
-3+static int kvm_s390_pv_dispose_one_leftover (struct kvm *kvm, +struct pv_vm_to_be_destroyed *leftover, +u16 *rc,u16 *rrc) {int cc; - 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 ); + cc = uv_cmd_nodata(leftover->handle, UVC_CMD_DESTROY_SEC_CONF, rc, rrc); > + KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY LEFTOVER VM: rc %x rrc %x",
*rc, *rrc);
arch/s390/kvm/pv.c static int kvm_s390_pv_alloc_vm(struct kvm *kvm) · line 208
Quoted 3 lines
arch/s390/kvm/pv.c +3
-0+WARN_ONCE (cc,"protvirt destroy leftover vm failed rc %x rrc %x" , *rc, *rrc); +if (cc) +return cc;
maybe set the handle to zero here to be extra sure. leftover will get free'd directly after this function, but that might change and I assume we do not want to end up with invalid handles hanging arround.
Quoted 167 lines
arch/s390/kvm/pv.c +151
-5/* -* if the mm still has a mapping, make all its pages accessible -* before destroying the guest +* Intentionally leak unusable memory. If the UVC fails, the memory +* used for the VM and its metadata is permanently unusable. +* This can only happen in case of a serious KVM or hardware bug; it +* is not expected to happen in normal operation. */ -if (mmget_not_zero (kvm->mm)) { -s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); -mmput (kvm->mm); +free_pages (leftover->stor_base,get_order (uv_info.guest_base_stor_len)); +free_pages (leftover->old_gmap_table,CRST_ALLOC_ORDER ); +vfree (leftover->stor_var); +atomic_dec (&kvm->mm->context.protected_count); +return 0 ; +} + +/** +* kvm_s390_destroy_lower_2g - Destroy the first 2GB of protected guest memory. +* @kvm: the VM whose memory is to be cleared. +* +* Destroy the first 2GB of guest memory, to avoid prefix issues after reboot. +* The CPUs of the protected VM need to be destroyed beforehand. +*/ +static void kvm_s390_destroy_lower_2g (struct kvm *kvm) +{ +const unsigned long pages_2g =SZ_2G /PAGE_SIZE ; +struct kvm_memory_slot *slot; +unsigned long len; +int srcu_idx; + + srcu_idx =srcu_read_lock (&kvm->srcu); + +/* Take the memslot containing guest absolute address 0 */ + slot =gfn_to_memslot (kvm,0 ); +/* Clear all slots or parts thereof that are below 2GB */ +while (slot && slot->base_gfn < pages_2g) { + len =min_t (u64, slot->npages, pages_2g - slot->base_gfn) *PAGE_SIZE ; +s390_uv_destroy_range (kvm->mm, slot->userspace_addr, slot->userspace_addr + len); +/* Take the next memslot */ + slot =gfn_to_memslot (kvm, slot->base_gfn + slot->npages); + } + +srcu_read_unlock (&kvm->srcu, srcu_idx); +} + +/** +* kvm_s390_pv_set_aside - Set aside a protected VM for later teardown. +* @kvm: the VM +* @rc: return value for the RC field of the UVCB +* @rrc: return value for the RRC field of the UVCB +* +* Set aside the protected VM for a subsequent teardown. The VM will be able +* to continue immediately as a non-secure VM, and the information needed to +* properly tear down the protected VM is set aside. If another protected VM +* was already set aside without starting its teardown, this function will +* fail. +* The CPUs of the protected VM need to be destroyed beforehand. +* +* Context: kvm->lock needs to be held +* +* Return: 0 in case of success, -EINVAL if another protected VM was already set +* aside, -ENOMEM if the system ran out of memory. +*/ +int kvm_s390_pv_set_aside (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +struct pv_vm_to_be_destroyed *priv; + +/* +* If another protected VM was already prepared for teardown, refuse. +* A normal deinitialization has to be performed instead. +*/ +if (kvm->arch.pv.set_aside) +return -EINVAL ; + priv =kzalloc (sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->stor_var = kvm->arch.pv.stor_var; + priv->stor_base = kvm->arch.pv.stor_base; + priv->handle =kvm_s390_pv_get_handle (kvm); + priv->old_gmap_table = (unsigned long )kvm->arch.gmap->table; +WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); +if (s390_replace_asce (kvm->arch.gmap)) { +kfree (priv); +return -ENOMEM ; } +kvm_s390_destroy_lower_2g (kvm); +kvm_s390_clear_pv_state (kvm); + kvm->arch.pv.set_aside = priv; + + *rc =UVC_RC_EXECUTED ; + *rrc =42 ; +return 0 ; +} + +/** +* kvm_s390_pv_deinit_vm - Deinitialize the current protected VM +* @kvm: the KVM whose protected VM needs to be deinitialized +* @rc: the RC code of the UVC +* @rrc: the RRC code of the UVC +* +* Deinitialize the current protected VM. This function will destroy and +* cleanup the current protected VM, but it will not cleanup the guest +* memory. This function should only be called when the protected VM has +* just been created and therefore does not have any guest memory, or when +* the caller cleans up the guest memory separately. +* +* This function should not fail, but if it does, the donated memory must +* not be freed. +* +* Context: kvm->lock needs to be held +* +* Return: 0 in case of success, otherwise -EIO +*/ +int kvm_s390_pv_deinit_vm (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +int cc; + + 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_s390_pv_dealloc_vm (kvm);@@ -189,6 +338,127 @@ int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)return cc ? -EIO :0 ; } +/** +* kvm_s390_pv_deinit_cleanup_all - Clean up all protected VMs associated +* with a specific KVM. +* @kvm: the KVM to be cleaned up +* @rc: the RC code of the first failing UVC +* @rrc: the RRC code of the first failing UVC +* +* This function will clean up all protected VMs associated with a KVM. +* This includes the active one, the one prepared for deinitialization with +* kvm_s390_pv_set_aside, and any still pending in the need_cleanup list. +* +* Context: kvm->lock needs to be held unless being called from +* kvm_arch_destroy_vm. +* +* Return: 0 if all VMs are successfully cleaned up, otherwise -EIO +*/ +int kvm_s390_pv_deinit_cleanup_all (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +struct pv_vm_to_be_destroyed *cur; +bool need_zap = false; +u16 _rc, _rrc; +int cc =0 ; + +/* Make sure the counter does not reach 0 before calling s390_uv_destroy_range */ +atomic_inc (&kvm->mm->context.protected_count); + + *rc =1 ; +/* If the current VM is protected, destroy it */ +if (kvm_s390_pv_get_handle (kvm)) { + cc =kvm_s390_pv_deinit_vm (kvm, rc, rrc); + need_zap = true; + } + +/* If a previous protected VM was set aside, put it in the need_cleanup list */ +if (kvm->arch.pv.set_aside) { +list_add (kvm->arch.pv.set_aside, &kvm->arch.pv.need_cleanup);
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 386
Quoted 10 lines
arch/s390/kvm/pv.c +10
-0+ kvm->arch.pv.set_aside =NULL ; + } + +/* Cleanup all protected VMs in the need_cleanup list */ +while (!list_empty (&kvm->arch.pv.need_cleanup)) { + cur =list_first_entry (&kvm->arch.pv.need_cleanup,typeof (*cur), list); + need_zap = true; +if (kvm_s390_pv_dispose_one_leftover (kvm, cur, &_rc, &_rrc)) { + cc =1 ; +/* do not overwrite a previous error code */
use UVC_RC_EXECUTED
Quoted 59 lines
arch/s390/kvm/pv.c +59
-0+if (*rc ==1 ) { + *rc = _rc; + *rrc = _rrc; + } + } +list_del (&cur->list); +kfree (cur); + } + +/* +* If the mm still has a mapping, try to mark all its pages as +* accessible. The counter should not reach zero before this +* cleanup has been performed. +*/ +if (need_zap &&mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } + +/* Now the counter can safely reach 0 */ +atomic_dec (&kvm->mm->context.protected_count); +return cc ? -EIO :0 ; +} + +/** +* kvm_s390_pv_deinit_aside_vm - Teardown a previously set aside protected VM. +* @kvm the VM previously associated with the protected VM +* @rc return value for the RC field of the UVCB +* @rrc return value for the RRC field of the UVCB +* +* Tear down the protected VM that had been previously prepared for teardown +* using kvm_s390_pv_set_aside_vm. Ideally this should be called by +* userspace asynchronously from a separate thread. +* +* Context: kvm->lock must not be held. +* +* Return: 0 in case of success, -EINVAL if no protected VM had been +* prepared for asynchronous teardowm, -EIO in case of other errors. +*/ +int kvm_s390_pv_deinit_aside_vm (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +struct pv_vm_to_be_destroyed *p; +int ret =0 ; + +lockdep_assert_not_held (&kvm->lock); +mutex_lock (&kvm->lock); + p = kvm->arch.pv.set_aside; + kvm->arch.pv.set_aside =NULL ; +mutex_unlock (&kvm->lock); +if (!p) +return -EINVAL ; + +/* When a fatal signal is received, stop immediately */ +if (s390_uv_destroy_range_interruptible (kvm->mm,0 ,TASK_SIZE_MAX )) + goto done; +if (kvm_s390_pv_dispose_one_leftover (kvm, p, rc, rrc)) + ret = -EIO ; +kfree (p); + p =NULL ;
arch/s390/kvm/pv.c int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc) · line 455
Quoted 10 lines
arch/s390/kvm/pv.c +10
-0+done: +/* +* p is not NULL if we aborted because of a fatal signal, in which +* case queue the leftover for later cleanup. +*/ +if (p) { +mutex_lock (&kvm->lock); +list_add (&p->list, &kvm->arch.pv.need_cleanup); +mutex_unlock (&kvm->lock); +/* Did not finish, but pretend things went well */
use UVC_RC_EXECUTED
Quoted 22 lines
arch/s390/kvm/pv.c +6
-0+ *rc =1 ; + *rrc =42 ; + } +return ret; +} +static void kvm_s390_pv_mmu_notifier_release (struct mmu_notifier *subscription,struct mm_struct *mm) {include/uapi/linux/kvm.h +2
-0@@ -1739,6 +1739,8 @@ enum pv_cmd_id {KVM_PV_UNSHARE_ALL ,KVM_PV_INFO ,KVM_PV_DUMP , +KVM_PV_ASYNC_CLEANUP_PREPARE , + KVM_PV_ASYNC_CLEANUP_PERFORM, }; struct kvm_pv_cmd {
Claudio Imbrenda
maintainerRe: [PATCH v14 1/6] KVM: s390: pv: asynchronous destroy for reboot
Quoted 3 lines · On Tue, 4 Oct 2022 15:43:29 +0200 Steffen Eiden <[email protected]> wrote:
Hi Claudio,
LGTM, but I have some nits.
[...]
Quoted 10 lines
arch/s390/kvm/kvm-s390.c unsigned int diag9c_forwarding_hz; · line 211
Quoted 8 lines
arch/s390/kvm/kvm-s390.c +0
-0@@ -209,6 +209,8 @@ 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" );IMO it would be better to initialize this variable explicitly here.
and that's how it was a few versions ago, but
Quoted 2 lines
You do this later in patch [6/6].
Whats the reason to do this not at this point.
Janosch asked to split it this way :)
Quoted 9 lines
Quoted 9 lines
arch/s390/kvm/kvm-s390.c +2
-0+static int async_destroy; +/* * 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 @@ -2504,9 +2506,13 @@ static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
Quoted 4 lines
arch/s390/kvm/kvm-s390.c static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd, · line 2509
Quoted 3 lines
arch/s390/kvm/kvm-s390.c +1
-0static int kvm_s390_handle_pv (struct kvm *kvm,struct kvm_pv_cmd *cmd) { +const bool needslock = (cmd->cmd !=KVM_PV_ASYNC_CLEANUP_PERFORM );
need_lock? or justlock?
hmm, maybe need_lock
Quoted 14 lines
Quoted 14 lines
arch/s390/kvm/kvm-s390.c +4
-1+void __user *argp = (void __user *)cmd->data;int r =0 ;u16 dummy; -void __user *argp = (void __user *)cmd->data; + +if (needslock) +mutex_lock (&kvm->lock);switch (cmd->cmd) {case KVM_PV_ENABLE : {@@ -2540,6 +2546,31 @@ 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 ; }
[...]
Quoted 26 lines
Quoted 15 lines
arch/s390/kvm/kvm-s390.c +13
-2-/* this should not fail, but if it does, we must not free the donated memory */ -int kvm_s390_pv_deinit_vm (struct kvm *kvm,u16 *rc,u16 *rrc) +/** +* kvm_s390_pv_dispose_one_leftover - Clean up one leftover protected VM. +* @kvm: the KVM that was associated with this leftover protected VM +* @leftover: details about the leftover protected VM that needs a clean up +* @rc: the RC code of the Destroy Secure Configuration UVC +* @rrc: the RRC code of the Destroy Secure Configuration UVC +* +* Destroy one leftover protected VM. +* On success, kvm->mm->context.protected_count will be decremented atomically +* and all other resources used by the VM will be freed. +* +* Return: 0 in case of success, otherwise 1 +*/ arch/s390/kvm/kvm-s390.c
Quoted 10 lines
arch/s390/kvm/kvm-s390.c +4
-3+static int kvm_s390_pv_dispose_one_leftover (struct kvm *kvm, +struct pv_vm_to_be_destroyed *leftover, +u16 *rc,u16 *rrc) {int cc; - 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 ); + cc = uv_cmd_nodata(leftover->handle, UVC_CMD_DESTROY_SEC_CONF, rc, rrc); > + KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY LEFTOVER VM: rc %x rrc %x",*rc, *rrc);
Quoted 6 lines
arch/s390/kvm/kvm-s390.c
Quoted 3 lines
arch/s390/kvm/kvm-s390.c +3
-0+WARN_ONCE (cc,"protvirt destroy leftover vm failed rc %x rrc %x" , *rc, *rrc); +if (cc) +return cc;maybe set the handle to zero here to be extra sure.
leftoverwill get free'd directly after this function, but that might change and I assume we do not want to end up with
I don't see a reason why it should change.
Quoted 18 lines
invalid handles hanging arround.
Quoted 17 lines
/*
arch/s390/kvm/kvm-s390.c +10
-5-* if the mm still has a mapping, make all its pages accessible -* before destroying the guest +* Intentionally leak unusable memory. If the UVC fails, the memory +* used for the VM and its metadata is permanently unusable. +* This can only happen in case of a serious KVM or hardware bug; it +* is not expected to happen in normal operation. */ -if (mmget_not_zero (kvm->mm)) { -s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); -mmput (kvm->mm); +free_pages (leftover->stor_base,get_order (uv_info.guest_base_stor_len)); +free_pages (leftover->old_gmap_table,CRST_ALLOC_ORDER ); +vfree (leftover->stor_var); +atomic_dec (&kvm->mm->context.protected_count); +return 0 ; +}
[...]
Quoted 8 lines
arch/s390/kvm/kvm-s390.c
Quoted 7 lines
arch/s390/kvm/kvm-s390.c +7
-0+/* Cleanup all protected VMs in the need_cleanup list */ +while (!list_empty (&kvm->arch.pv.need_cleanup)) { + cur =list_first_entry (&kvm->arch.pv.need_cleanup,typeof (*cur), list); + need_zap = true; +if (kvm_s390_pv_dispose_one_leftover (kvm, cur, &_rc, &_rrc)) { + cc =1 ; +/* do not overwrite a previous error code */ use
UVC_RC_EXECUTED
will fix
Quoted 62 lines
Quoted 62 lines
arch/s390/kvm/kvm-s390.c +62
-0+if (*rc ==1 ) { + *rc = _rc; + *rrc = _rrc; + } + } +list_del (&cur->list); +kfree (cur); + } + +/* +* If the mm still has a mapping, try to mark all its pages as +* accessible. The counter should not reach zero before this +* cleanup has been performed. +*/ +if (need_zap &&mmget_not_zero (kvm->mm)) { +s390_uv_destroy_range (kvm->mm,0 ,TASK_SIZE ); +mmput (kvm->mm); + } + +/* Now the counter can safely reach 0 */ +atomic_dec (&kvm->mm->context.protected_count); +return cc ? -EIO :0 ; +} + +/** +* kvm_s390_pv_deinit_aside_vm - Teardown a previously set aside protected VM. +* @kvm the VM previously associated with the protected VM +* @rc return value for the RC field of the UVCB +* @rrc return value for the RRC field of the UVCB +* +* Tear down the protected VM that had been previously prepared for teardown +* using kvm_s390_pv_set_aside_vm. Ideally this should be called by +* userspace asynchronously from a separate thread. +* +* Context: kvm->lock must not be held. +* +* Return: 0 in case of success, -EINVAL if no protected VM had been +* prepared for asynchronous teardowm, -EIO in case of other errors. +*/ +int kvm_s390_pv_deinit_aside_vm (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +struct pv_vm_to_be_destroyed *p; +int ret =0 ; + +lockdep_assert_not_held (&kvm->lock); +mutex_lock (&kvm->lock); + p = kvm->arch.pv.set_aside; + kvm->arch.pv.set_aside =NULL ; +mutex_unlock (&kvm->lock); +if (!p) +return -EINVAL ; + +/* When a fatal signal is received, stop immediately */ +if (s390_uv_destroy_range_interruptible (kvm->mm,0 ,TASK_SIZE_MAX )) + goto done; +if (kvm_s390_pv_dispose_one_leftover (kvm, p, rc, rrc)) + ret = -EIO ; +kfree (p); + p =NULL ; +done: +/* +* p is not NULL if we aborted because of a fatal signal, in which
Quoted 8 lines
arch/s390/kvm/kvm-s390.c
Quoted 7 lines
arch/s390/kvm/kvm-s390.c +7
-0+* case queue the leftover for later cleanup. +*/ +if (p) { +mutex_lock (&kvm->lock); +list_add (&p->list, &kvm->arch.pv.need_cleanup); +mutex_unlock (&kvm->lock); +/* Did not finish, but pretend things went well */ use
UVC_RC_EXECUTED
will fix as well
Quoted 22 lines
Quoted 22 lines
arch/s390/kvm/kvm-s390.c +6
-0+ *rc =1 ; + *rrc =42 ; + } +return ret; +} +static void kvm_s390_pv_mmu_notifier_release (struct mmu_notifier *subscription,struct mm_struct *mm) {include/uapi/linux/kvm.h +2
-0@@ -1739,6 +1739,8 @@ enum pv_cmd_id {KVM_PV_UNSHARE_ALL ,KVM_PV_INFO ,KVM_PV_DUMP , +KVM_PV_ASYNC_CLEANUP_PREPARE , + KVM_PV_ASYNC_CLEANUP_PERFORM, }; struct kvm_pv_cmd {
Re: [PATCH v14 1/6] KVM: s390: pv: asynchronous destroy for reboot
In reply to Claudio ImbrendaQuoted 38 lines · Claudio Imbrenda
Until 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_COMMANDioctl:
KVM_PV_ASYNC_CLEANUP_PREPARE: set aside the current protected VM for later asynchronous teardown. The current KVM VM will then continue immediately as non-protected. If a protected VM had already been set aside for asynchronous teardown, but without starting the teardown process, this call will fail. There can be at most one VM set aside at any time. Once it is set aside, the protected VM only exists in the context of the Ultravisor, it is not associated with the KVM VM anymore. Its protected CPUs have already been destroyed, but not its memory. This command can be issued again immediately after startingKVM_PV_ASYNC_CLEANUP_PERFORM, without having to wait for completion.
KVM_PV_ASYNC_CLEANUP_PERFORM: tears down the protected VM previously set aside usingKVM_PV_ASYNC_CLEANUP_PREPARE. Ideally theKVM_PV_ASYNC_CLEANUP_PERFORMPV command should be issued by userspace from a separate thread. If a fatal signal is received (or if the process terminates naturally), the command will terminate immediately without completing. All protected VMs whose teardown was interrupted will be put in theneed_cleanuplist. The rest of the normal KVM teardown process will take care of properly cleaning up all remaining protected VMs, including the ones on theneed_cleanuplist.
Quoted 2 lines
Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>
That was quite a read.
With Steffen's nits fixed:
Claudio Imbrenda
maintainerPATCH v14 2/6 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 | 37 ++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-)
Documentation/virt/kvm/api.rst +33 -4
@@ -5161,10 +5161,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. + 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. If a + previous protected VM had been prepared for asynchonous teardown with + KVM_PV_ASYNC_CLEANUP_PREPARE and not subsequently torn down with + KVM_PV_ASYNC_CLEANUP_PERFORM, it will be torn down in this call + together with the current protected VM. KVM_PV_VM_SET_SEC_PARMS Pass the image header from VM memory to the Ultravisor in
@@ -5287,6 +5290,32 @@ KVM_PV_DUMP authentication tag all of which are needed to decrypt the dump at a later time. +KVM_PV_ASYNC_CLEANUP_PREPARE + Prepare the current protected VM for asynchronous teardown. Most + resources used by the current protected VM will be set aside for a + subsequent asynchronous teardown. The current protected VM will then + resume execution immediately as non-protected. There can be at most + one protected VM prepared for asynchronous teardown at any time. If + a protected VM had already been prepared for teardown without + subsequently calling KVM_PV_ASYNC_CLEANUP_PERFORM, this call will + fail. In that case, the userspace process should issue a normal + KVM_PV_DISABLE. The resources set aside with this call will need to + be cleaned up with a subsequent call to KVM_PV_ASYNC_CLEANUP_PERFORM + or KVM_PV_DISABLE, otherwise they will be cleaned up when KVM + terminates. KVM_PV_ASYNC_CLEANUP_PREPARE can be called again as soon + as cleanup starts, i.e. before KVM_PV_ASYNC_CLEANUP_PERFORM finishes. + +KVM_PV_ASYNC_CLEANUP_PERFORM + Tear down the protected VM previously prepared for teardown with + KVM_PV_ASYNC_CLEANUP_PREPARE. The resources that had been set aside + will be freed during the execution of this command. 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, and the normal + KVM shutdown procedure will take care of cleaning up all remaining + protected VMs, including the ones whose teardown was interrupted by + process termination. + 4.126 KVM_X86_SET_MSR_FILTER ----------------------------
Signature · 1 line
2.37.3
Re: [PATCH v14 2/6] KVM: s390: pv: api documentation for asynchronous destroy
Quoted 5 lines · Claudio Imbrenda
Add documentation for the new commands added to the
KVM_S390_PV_COMMANDioctl.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>LGTM.
Quoted 59 lines
--- Documentation/virt/kvm/api.rst | 37 ++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-)Documentation/virt/kvm/api.rst +33
-4@@ -5161,10 +5161,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. + 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. If a + previous protected VM had been prepared for asynchonous teardown with + KVM_PV_ASYNC_CLEANUP_PREPARE and not subsequently torn down with + KVM_PV_ASYNC_CLEANUP_PERFORM, it will be torn down in this call + together with the current protected VM. KVM_PV_VM_SET_SEC_PARMS Pass the image header from VM memory to the Ultravisor in@@ -5287,6 +5290,32 @@ KVM_PV_DUMP authentication tag all of which are needed to decrypt the dump at a later time. +KVM_PV_ASYNC_CLEANUP_PREPARE + Prepare the current protected VM for asynchronous teardown. Most + resources used by the current protected VM will be set aside for a + subsequent asynchronous teardown. The current protected VM will then + resume execution immediately as non-protected. There can be at most + one protected VM prepared for asynchronous teardown at any time. If + a protected VM had already been prepared for teardown without + subsequently calling KVM_PV_ASYNC_CLEANUP_PERFORM, this call will + fail. In that case, the userspace process should issue a normal + KVM_PV_DISABLE. The resources set aside with this call will need to + be cleaned up with a subsequent call to KVM_PV_ASYNC_CLEANUP_PERFORM + or KVM_PV_DISABLE, otherwise they will be cleaned up when KVM + terminates. KVM_PV_ASYNC_CLEANUP_PREPARE can be called again as soon + as cleanup starts, i.e. before KVM_PV_ASYNC_CLEANUP_PERFORM finishes. + +KVM_PV_ASYNC_CLEANUP_PERFORM + Tear down the protected VM previously prepared for teardown with + KVM_PV_ASYNC_CLEANUP_PREPARE. The resources that had been set aside + will be freed during the execution of this command. 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, and the normal + KVM shutdown procedure will take care of cleaning up all remaining + protected VMs, including the ones whose teardown was interrupted by + process termination. + 4.126 KVM_X86_SET_MSR_FILTER ----------------------------
Re: [PATCH v14 2/6] KVM: s390: pv: api documentation for asynchronous destroy
In reply to Claudio ImbrendaQuoted 5 lines · Claudio Imbrenda
Add documentation for the new commands added to the
KVM_S390_PV_COMMANDioctl.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>
Claudio Imbrenda
maintainerPATCH v14 3/6 KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
In reply to Claudio ImbrendaAdd KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 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
@@ -618,6 +618,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_PROTECTED_ASYNC_DISABLE : + r = async_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;
include/uapi/linux/kvm.h +1 -0
@@ -1177,6 +1177,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 225#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.37.3
Re: [PATCH v14 3/6] KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
Quoted 6 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROTECTED_ASYNC_DISABLEto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>
LGTM.
Quoted 31 lines
--- 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@@ -618,6 +618,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_PROTECTED_ASYNC_DISABLE : + r = async_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +1
-0@@ -1177,6 +1177,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 225#ifdef KVM_CAP_IRQ_ROUTING
Janosch Frank
maintainerRe: [PATCH v14 3/6] KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
In reply to Claudio ImbrendaQuoted 30 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROTECTED_ASYNC_DISABLEto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[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 +3
-0@@ -618,6 +618,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_PROTECTED_ASYNC_DISABLE : + r = async_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +0
-0
include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 1180
Quoted 4 lines
include/uapi/linux/kvm.h +1
-0@@ -1177,6 +1177,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 225
I can see 223 in Paolo's next, is there a 224 that I've missed?
Quoted 3 lines
include/uapi/linux/kvm.h +0
-0#ifdef KVM_CAP_IRQ_ROUTING
Claudio Imbrenda
maintainerRe: [PATCH v14 3/6] KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
Quoted 29 lines · On Mon, 10 Oct 2022 13:45:54 +0200 Janosch Frank <[email protected]> wrote:
Quoted 28 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROTECTED_ASYNC_DISABLEto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[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 +3
-0@@ -618,6 +618,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_PROTECTED_ASYNC_DISABLE : + r = async_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +0
-1--- a/include/uapi/linux/kvm.h
Quoted 8 lines
include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 1180
Quoted 6 lines
+++
b/include/uapi/linux/kvm.hinclude/uapi/linux/kvm.h +1
-0@@ -1177,6 +1177,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 225I can see 223 in Paolo's next, is there a 224 that I've missed?
no, I set this to an arbitrarily high value to avoid conficts
seems like I got it more or less right :)
feel free to change the value of the macro when merging, so it's contiguous.
Quoted 6 lines
Quoted 5 lines
include/uapi/linux/kvm.h +0
-0#ifdef KVM_CAP_IRQ_ROUTING
Re: [PATCH v14 3/6] KVM: s390: pv: add KVM_CAP_S390_PROTECTED_ASYNC_DISABLE
Quoted 41 lines · Claudio Imbrenda
Quoted 29 lines · On Mon, 10 Oct 2022 13:45:54 +0200 Janosch Frank <[email protected]> wrote:
Quoted 28 lines · Claudio Imbrenda
Add
KVM_CAP_S390_PROTECTED_ASYNC_DISABLEto signal that theKVM_PV_ASYNC_DISABLEandKVM_PV_ASYNC_DISABLE_PREPAREcommands for theKVM_S390_PV_COMMANDioctl are available.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[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 +3
-0@@ -618,6 +618,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_PROTECTED_ASYNC_DISABLE : + r = async_destroy &&is_prot_virt_host (); +break ;case KVM_CAP_S390_PROTECTED : r =is_prot_virt_host ();break ;include/uapi/linux/kvm.h +0
-1--- a/include/uapi/linux/kvm.hQuoted 8 lines
include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 1180
Quoted 6 lines
+++
b/include/uapi/linux/kvm.hinclude/uapi/linux/kvm.h +1
-0@@ -1177,6 +1177,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220#define KVM_CAP_S390_ZPCI_OP 221#define KVM_CAP_S390_CPU_TOPOLOGY 222 +#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 225I can see 223 in Paolo's next, is there a 224 that I've missed?
Quoted 6 lines
no, I set this to an arbitrarily high value to avoid conficts
seems like I got it more or less right :)
feel free to change the value of the macro when merging, so it's contiguous.
Claudio Imbrenda
maintainerPATCH v14 4/6 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.
The misc feature indicates, among other things, that importing a shared page from a different protected VM will automatically also transfer its ownership.
--- arch/s390/kernel/uv.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/s390/kernel/uv.c +7 -0
@@ -255,6 +255,13 @@ 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) { +/* +* The misc feature indicates, among other things, that importing a +* shared page from a different protected VM will automatically also +* transfer its ownership. +*/ +if (test_bit_inv (BIT_UV_FEAT_MISC , &uv_info.uv_feature_indications)) +return false;if (uvcb->cmd ==UVC_CMD_UNPIN_PAGE_SHARED )return false;return atomic_read (&mm->context.protected_count) >1 ;
Signature · 1 line
2.37.3
Re: [PATCH v14 4/6] KVM: s390: pv: avoid export before import if possible
Quoted 10 lines · Claudio Imbrenda
If the appropriate UV feature bit is set, there is no need to perform an export before import.
The misc feature indicates, among other things, that importing a shared page from a different protected VM will automatically also transfer its ownership.
Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>Reviewed-by Janosch Frank <[email protected]>
LGTM
Quoted 22 lines
--- arch/s390/kernel/uv.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/s390/kernel/uv.c +7
-0@@ -255,6 +255,13 @@ 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) { +/* +* The misc feature indicates, among other things, that importing a +* shared page from a different protected VM will automatically also +* transfer its ownership. +*/ +if (test_bit_inv (BIT_UV_FEAT_MISC , &uv_info.uv_feature_indications)) +return false;if (uvcb->cmd ==UVC_CMD_UNPIN_PAGE_SHARED )return false;return atomic_read (&mm->context.protected_count) >1 ;
Claudio Imbrenda
maintainerPATCH v14 5/6 KVM: s390: pv: support for Destroy fast UVC
In reply to Claudio ImbrendaAdd support for the Destroy Secure Configuration Fast Ultravisor call, and take advantage of it for asynchronous destroy.
When supported, the protected guest is destroyed immediately using the new UVC, leaving only the memory to be cleaned up asynchronously.
--- arch/s390/include/asm/uv.h | 10 +++++++ arch/s390/kvm/pv.c | 57 ++++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 8 deletions(-)
arch/s390/include/asm/uv.h +10 -0
@@ -34,6 +34,7 @@#define UVC_CMD_INIT_UV 0x000f#define UVC_CMD_CREATE_SEC_CONF 0x0100#define UVC_CMD_DESTROY_SEC_CONF 0x0101 +#define UVC_CMD_DESTROY_SEC_CONF_FAST 0x0102#define UVC_CMD_CREATE_SEC_CPU 0x0120#define UVC_CMD_DESTROY_SEC_CPU 0x0121#define UVC_CMD_CONV_TO_SEC_STOR 0x0200
@@ -81,6 +82,7 @@ enum uv_cmds_inst {BIT_UVC_CMD_UNSHARE_ALL =20 ,BIT_UVC_CMD_PIN_PAGE_SHARED =21 ,BIT_UVC_CMD_UNPIN_PAGE_SHARED =22 , +BIT_UVC_CMD_DESTROY_SEC_CONF_FAST =23 ,BIT_UVC_CMD_DUMP_INIT =24 ,BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE =25 , BIT_UVC_CMD_DUMP_CPU = 26,
@@ -230,6 +232,14 @@ struct uv_cb_nodata {u64 reserved20[4 ]; } __packed__aligned(8) ; +/* Destroy Configuration Fast */ +struct uv_cb_destroy_fast { +struct uv_cb_header header; +u64 reserved08[2 ]; +u64 handle; +u64 reserved20[5 ]; +} __packed__aligned(8) ; +/* Set Shared Access */ struct uv_cb_share {struct uv_cb_header header;
arch/s390/kvm/pv.c +49 -8
100 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v14 5/6] KVM: s390: pv: support for Destroy fast UVC
Quoted 9 lines · Claudio Imbrenda
Add support for the Destroy Secure Configuration Fast Ultravisor call, and take advantage of it for asynchronous destroy.
When supported, the protected guest is destroyed immediately using the new UVC, leaving only the memory to be cleaned up asynchronously.
Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nico Boehr <[email protected]>Reviewed-by Janosch Frank <[email protected]>
LGTM
Quoted 151 lines
--- arch/s390/include/asm/uv.h | 10 +++++++ arch/s390/kvm/pv.c | 57 ++++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 8 deletions(-)arch/s390/include/asm/uv.h +10
-0@@ -34,6 +34,7 @@#define UVC_CMD_INIT_UV 0x000f#define UVC_CMD_CREATE_SEC_CONF 0x0100#define UVC_CMD_DESTROY_SEC_CONF 0x0101 +#define UVC_CMD_DESTROY_SEC_CONF_FAST 0x0102#define UVC_CMD_CREATE_SEC_CPU 0x0120#define UVC_CMD_DESTROY_SEC_CPU 0x0121#define UVC_CMD_CONV_TO_SEC_STOR 0x0200@@ -81,6 +82,7 @@ enum uv_cmds_inst {BIT_UVC_CMD_UNSHARE_ALL =20 ,BIT_UVC_CMD_PIN_PAGE_SHARED =21 ,BIT_UVC_CMD_UNPIN_PAGE_SHARED =22 , +BIT_UVC_CMD_DESTROY_SEC_CONF_FAST =23 ,BIT_UVC_CMD_DUMP_INIT =24 ,BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE =25 , BIT_UVC_CMD_DUMP_CPU = 26,@@ -230,6 +232,14 @@ struct uv_cb_nodata {u64 reserved20[4 ]; } __packed__aligned(8) ; +/* Destroy Configuration Fast */ +struct uv_cb_destroy_fast { +struct uv_cb_header header; +u64 reserved08[2 ]; +u64 handle; +u64 reserved20[5 ]; +} __packed__aligned(8) ; +/* Set Shared Access */ struct uv_cb_share {struct uv_cb_header header;arch/s390/kvm/pv.c +49
-8@@ -201,6 +201,9 @@ static int kvm_s390_pv_dispose_one_leftover(struct kvm *kvm, {int cc; +/* It used the destroy-fast UVC, nothing left to do here */ +if (!leftover->handle) + goto done_fast; cc =uv_cmd_nodata (leftover->handle,UVC_CMD_DESTROY_SEC_CONF , rc, rrc);KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY LEFTOVER VM: rc %x rrc %x" , *rc, *rrc);WARN_ONCE (cc,"protvirt destroy leftover vm failed rc %x rrc %x" , *rc, *rrc);@@ -215,6 +218,7 @@ static int kvm_s390_pv_dispose_one_leftover(struct kvm *kvm,free_pages (leftover->stor_base,get_order (uv_info.guest_base_stor_len));free_pages (leftover->old_gmap_table,CRST_ALLOC_ORDER );vfree (leftover->stor_var); +done_fast:atomic_dec (&kvm->mm->context.protected_count);return 0 ; }@@ -248,6 +252,32 @@ static void kvm_s390_destroy_lower_2g(struct kvm *kvm)srcu_read_unlock (&kvm->srcu, srcu_idx); } +static int kvm_s390_pv_deinit_vm_fast (struct kvm *kvm,u16 *rc,u16 *rrc) +{ +struct uv_cb_destroy_fast uvcb = { + .header.cmd =UVC_CMD_DESTROY_SEC_CONF_FAST , + .header.len =sizeof (uvcb), + .handle =kvm_s390_pv_get_handle (kvm), + }; +int cc; + + cc =uv_call_sched (0 , (u64)&uvcb); + *rc = uvcb.header.rc; + *rrc = uvcb.header.rrc; +WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); +KVM_UV_EVENT (kvm,3 ,"PROTVIRT DESTROY VM FAST: rc %x rrc %x" , *rc, *rrc); +WARN_ONCE (cc,"protvirt destroy vm fast failed rc %x rrc %x" , *rc, *rrc); +/* Inteded memory leak on "impossible" error */ +if (!cc) +kvm_s390_pv_dealloc_vm (kvm); +return cc ? -EIO :0 ; +} + +static inline bool is_destroy_fast_available (void ) +{ +return test_bit_inv (BIT_UVC_CMD_DESTROY_SEC_CONF_FAST , uv_info.inst_calls_list); +} +/** * kvm_s390_pv_set_aside - Set aside a protected VM for later teardown. * @kvm: the VM @@ -269,6 +299,7 @@ static void kvm_s390_destroy_lower_2g(struct kvm *kvm)int kvm_s390_pv_set_aside (struct kvm *kvm,u16 *rc,u16 *rrc) {struct pv_vm_to_be_destroyed *priv; +int res =0 ;/* * If another protected VM was already prepared for teardown, refuse. @@ -280,14 +311,21 @@ int kvm_s390_pv_set_aside(struct kvm *kvm, u16 *rc, u16 *rrc)if (!priv)return -ENOMEM ; - priv->stor_var = kvm->arch.pv.stor_var; - priv->stor_base = kvm->arch.pv.stor_base; - priv->handle =kvm_s390_pv_get_handle (kvm); - priv->old_gmap_table = (unsigned long )kvm->arch.gmap->table; -WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); -if (s390_replace_asce (kvm->arch.gmap)) { +if (is_destroy_fast_available ()) { + res =kvm_s390_pv_deinit_vm_fast (kvm, rc, rrc); + }else { + priv->stor_var = kvm->arch.pv.stor_var; + priv->stor_base = kvm->arch.pv.stor_base; + priv->handle =kvm_s390_pv_get_handle (kvm); + priv->old_gmap_table = (unsigned long )kvm->arch.gmap->table; +WRITE_ONCE (kvm->arch.gmap->guest_handle,0 ); +if (s390_replace_asce (kvm->arch.gmap)) + res = -ENOMEM ; + } + +if (res) {kfree (priv); -return -ENOMEM ; +return res; }kvm_s390_destroy_lower_2g (kvm);@@ -464,6 +502,7 @@ static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription, { struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);u16 dummy; +int r;/* * No locking is needed since this is the last thread of the last user of this @@ -472,7 +511,9 @@ static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,* unregistered. This means that if this notifier runs, then the * struct kvm is still valid. */ -kvm_s390_cpus_from_pv (kvm, &dummy, &dummy); + r =kvm_s390_cpus_from_pv (kvm, &dummy, &dummy); +if (!r &&is_destroy_fast_available ()) +kvm_s390_pv_deinit_vm_fast (kvm, &dummy, &dummy); }static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = {
Claudio Imbrenda
maintainerPATCH v14 6/6 KVM: s390: pv: module parameter to fence asynchronous destroy
In reply to Claudio ImbrendaAdd the module parameter "async_destroy", to allow the asynchronous destroy mechanism to be switched off. This might be useful for debugging purposes.
The parameter is enabled by default since the feature is opt-in anyway.
--- arch/s390/kvm/kvm-s390.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +7 -1
@@ -209,7 +209,13 @@ 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" ); -static int async_destroy; +/* +* allow asynchronous deinit for protected guests; enable by default since +* the feature is opt-in anyway +*/ +static int async_destroy =1 ; +module_param (async_destroy, int,0444 ); +MODULE_PARM_DESC (async_destroy,"Asynchronous destroy for protected guests" );/* * For now we handle at most 16 double words as this is what the s390 base
Signature · 1 line
2.37.3
Re: [PATCH v14 6/6] KVM: s390: pv: module parameter to fence asynchronous destroy
Quoted 8 lines · Claudio Imbrenda
Add the module parameter "
async_destroy", to allow the asynchronous destroy mechanism to be switched off. This might be useful for debugging purposes.The parameter is enabled by default since the feature is opt-in anyway.
Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Janosch Frank <[email protected]>
LGTM
Quoted 23 lines
--- arch/s390/kvm/kvm-s390.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)arch/s390/kvm/kvm-s390.c +7
-1@@ -209,7 +209,13 @@ 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" ); -static int async_destroy; +/* +* allow asynchronous deinit for protected guests; enable by default since +* the feature is opt-in anyway +*/ +static int async_destroy =1 ; +module_param (async_destroy, int,0444 ); +MODULE_PARM_DESC (async_destroy,"Asynchronous destroy for protected guests" );/* * For now we handle at most 16 double words as this is what the s390 base