From: Gavin Shan <gshan@redhat.com> To: kvmarm@lists.cs.columbia.edu Cc: linux-kernel@vger.kernel.org, eauger@redhat.com, shannon.zhaosl@gmail.com, maz@kernel.org, Jonathan.Cameron@huawei.com, will@kernel.org, pbonzini@redhat.com, james.morse@arm.com, mark.rutland@arm.com, drjones@redhat.com, vkuznets@redhat.com, shan.gavin@gmail.com Subject: [PATCH v5 07/22] KVM: arm64: Support SDEI_EVENT_UNREGISTER hypercall Date: Tue, 22 Mar 2022 16:06:55 +0800 [thread overview] Message-ID: <20220322080710.51727-8-gshan@redhat.com> (raw) In-Reply-To: <20220322080710.51727-1-gshan@redhat.com> This supports SDEI_EVENT_UNREGISTER hypercall. It's used by the guest to unregister SDEI event. The SDEI event won't be raised to the guest after it's unregistered. The SDEI event is disabled automatically on unregistration. The currently handled events can't be unregistered. We set the unregistration pending state for the event so that it can be unregistered when the event handler is completed by receiving SDEI_EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall. Signed-off-by: Gavin Shan <gshan@redhat.com> --- arch/arm64/kvm/sdei.c | 133 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 4488d3f044f2..36eda31e0392 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -325,6 +325,135 @@ static unsigned long hypercall_context(struct kvm_vcpu *vcpu) return ret; } +static unsigned long +unregister_one_event(struct kvm *kvm, struct kvm_vcpu *vcpu, + struct kvm_sdei_registered_event *registered_event) +{ + struct kvm_vcpu *vcpup; + struct kvm_sdei_vcpu *vsdei; + struct kvm_sdei_exposed_event *exposed_event; + int index; + bool pending; + unsigned long i, ret = SDEI_SUCCESS; + + /* + * Cancel all vcpu events that have been queued, but not dispatched + * yet. If the vcpu event has been dispatched, we should mark it + * as pending for unregistration. The unregistration will be executed + * when the event handler is to be completed. + */ + exposed_event = registered_event->exposed_event; + kvm_for_each_vcpu(i, vcpup, kvm) { + vsdei = vcpup->arch.sdei; + if (!vsdei) + continue; + + /* + * The private vcpu events are requested to be unregistered + * on the specific vcpu or all vcpus. @vcpu is used to + * identify the cases. For the shared vcpu events, we need + * to unregister them on all vcpus. + */ + if (kvm_sdei_is_private(exposed_event->state.type) && + vcpu && vcpu != vcpup) + continue; + + if (registered_event->vcpu_event_count > 0) { + spin_lock(&vsdei->lock); + pending = remove_all_vcpu_events(vcpup, + registered_event->state.num); + spin_unlock(&vsdei->lock); + } else { + pending = false; + } + + /* + * For the private event, the pending state for unregistration + * is scattered and we need to update them individually. + * However, that same state for the shared event has to be + * updated at once according to @ret after the iteration is + * done. + */ + ret = pending ? SDEI_PENDING : ret; + if (!kvm_sdei_is_private(exposed_event->state.type)) + continue; + + index = kvm_sdei_vcpu_index(vcpup, exposed_event); + if (pending) { + kvm_sdei_set_unregister_pending(registered_event, + index); + } else { + kvm_sdei_clear_enabled(registered_event, index); + kvm_sdei_clear_registered(registered_event, index); + } + } + + /* + * Update the pending state for unregistration for the shared event + * at once. + */ + if (kvm_sdei_is_shared(exposed_event->state.type)) { + index = kvm_sdei_vcpu_index(vcpu, exposed_event); + if (ret == SDEI_PENDING) { + kvm_sdei_set_unregister_pending(registered_event, + index); + } else { + kvm_sdei_clear_enabled(registered_event, index); + kvm_sdei_clear_registered(registered_event, index); + } + } + + /* Destroy the registered event instance if needed */ + if (kvm_sdei_none_registered(registered_event)) + remove_one_registered_event(kvm, registered_event); + + return ret; +} + +static unsigned long hypercall_unregister(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_sdei_kvm *ksdei = kvm->arch.sdei; + struct kvm_sdei_exposed_event *exposed_event; + struct kvm_sdei_registered_event *registered_event; + unsigned long event_num = smccc_get_arg1(vcpu); + int index; + unsigned long ret = SDEI_SUCCESS; + + if (!kvm_sdei_is_supported(event_num)) + return SDEI_INVALID_PARAMETERS; + + spin_lock(&ksdei->lock); + + /* Check if the registered event exists */ + registered_event = find_registered_event(kvm, event_num); + if (!registered_event) { + ret = SDEI_INVALID_PARAMETERS; + goto unlock; + } + + /* Check if the event has been registered */ + exposed_event = registered_event->exposed_event; + index = kvm_sdei_vcpu_index(vcpu, exposed_event); + if (!kvm_sdei_is_registered(registered_event, index)) { + ret = SDEI_DENIED; + goto unlock; + } + + /* Check if the event has been pending for unregistration */ + if (kvm_sdei_is_unregister_pending(registered_event, index)) { + ret = SDEI_PENDING; + goto unlock; + } + + ret = unregister_one_event(kvm, vcpu, registered_event); + +unlock: + spin_unlock(&ksdei->lock); + + return ret; +} + int kvm_sdei_hypercall(struct kvm_vcpu *vcpu) { struct kvm *kvm = vcpu->kvm; @@ -365,7 +494,11 @@ int kvm_sdei_hypercall(struct kvm_vcpu *vcpu) break; case SDEI_1_0_FN_SDEI_EVENT_COMPLETE: case SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME: + ret = SDEI_NOT_SUPPORTED; + break; case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: + ret = hypercall_unregister(vcpu); + break; case SDEI_1_0_FN_SDEI_EVENT_STATUS: case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: case SDEI_1_0_FN_SDEI_EVENT_ROUTING_SET: -- 2.23.0
WARNING: multiple messages have this Message-ID (diff)
From: Gavin Shan <gshan@redhat.com> To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, shan.gavin@gmail.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org Subject: [PATCH v5 07/22] KVM: arm64: Support SDEI_EVENT_UNREGISTER hypercall Date: Tue, 22 Mar 2022 16:06:55 +0800 [thread overview] Message-ID: <20220322080710.51727-8-gshan@redhat.com> (raw) In-Reply-To: <20220322080710.51727-1-gshan@redhat.com> This supports SDEI_EVENT_UNREGISTER hypercall. It's used by the guest to unregister SDEI event. The SDEI event won't be raised to the guest after it's unregistered. The SDEI event is disabled automatically on unregistration. The currently handled events can't be unregistered. We set the unregistration pending state for the event so that it can be unregistered when the event handler is completed by receiving SDEI_EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall. Signed-off-by: Gavin Shan <gshan@redhat.com> --- arch/arm64/kvm/sdei.c | 133 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 4488d3f044f2..36eda31e0392 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -325,6 +325,135 @@ static unsigned long hypercall_context(struct kvm_vcpu *vcpu) return ret; } +static unsigned long +unregister_one_event(struct kvm *kvm, struct kvm_vcpu *vcpu, + struct kvm_sdei_registered_event *registered_event) +{ + struct kvm_vcpu *vcpup; + struct kvm_sdei_vcpu *vsdei; + struct kvm_sdei_exposed_event *exposed_event; + int index; + bool pending; + unsigned long i, ret = SDEI_SUCCESS; + + /* + * Cancel all vcpu events that have been queued, but not dispatched + * yet. If the vcpu event has been dispatched, we should mark it + * as pending for unregistration. The unregistration will be executed + * when the event handler is to be completed. + */ + exposed_event = registered_event->exposed_event; + kvm_for_each_vcpu(i, vcpup, kvm) { + vsdei = vcpup->arch.sdei; + if (!vsdei) + continue; + + /* + * The private vcpu events are requested to be unregistered + * on the specific vcpu or all vcpus. @vcpu is used to + * identify the cases. For the shared vcpu events, we need + * to unregister them on all vcpus. + */ + if (kvm_sdei_is_private(exposed_event->state.type) && + vcpu && vcpu != vcpup) + continue; + + if (registered_event->vcpu_event_count > 0) { + spin_lock(&vsdei->lock); + pending = remove_all_vcpu_events(vcpup, + registered_event->state.num); + spin_unlock(&vsdei->lock); + } else { + pending = false; + } + + /* + * For the private event, the pending state for unregistration + * is scattered and we need to update them individually. + * However, that same state for the shared event has to be + * updated at once according to @ret after the iteration is + * done. + */ + ret = pending ? SDEI_PENDING : ret; + if (!kvm_sdei_is_private(exposed_event->state.type)) + continue; + + index = kvm_sdei_vcpu_index(vcpup, exposed_event); + if (pending) { + kvm_sdei_set_unregister_pending(registered_event, + index); + } else { + kvm_sdei_clear_enabled(registered_event, index); + kvm_sdei_clear_registered(registered_event, index); + } + } + + /* + * Update the pending state for unregistration for the shared event + * at once. + */ + if (kvm_sdei_is_shared(exposed_event->state.type)) { + index = kvm_sdei_vcpu_index(vcpu, exposed_event); + if (ret == SDEI_PENDING) { + kvm_sdei_set_unregister_pending(registered_event, + index); + } else { + kvm_sdei_clear_enabled(registered_event, index); + kvm_sdei_clear_registered(registered_event, index); + } + } + + /* Destroy the registered event instance if needed */ + if (kvm_sdei_none_registered(registered_event)) + remove_one_registered_event(kvm, registered_event); + + return ret; +} + +static unsigned long hypercall_unregister(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_sdei_kvm *ksdei = kvm->arch.sdei; + struct kvm_sdei_exposed_event *exposed_event; + struct kvm_sdei_registered_event *registered_event; + unsigned long event_num = smccc_get_arg1(vcpu); + int index; + unsigned long ret = SDEI_SUCCESS; + + if (!kvm_sdei_is_supported(event_num)) + return SDEI_INVALID_PARAMETERS; + + spin_lock(&ksdei->lock); + + /* Check if the registered event exists */ + registered_event = find_registered_event(kvm, event_num); + if (!registered_event) { + ret = SDEI_INVALID_PARAMETERS; + goto unlock; + } + + /* Check if the event has been registered */ + exposed_event = registered_event->exposed_event; + index = kvm_sdei_vcpu_index(vcpu, exposed_event); + if (!kvm_sdei_is_registered(registered_event, index)) { + ret = SDEI_DENIED; + goto unlock; + } + + /* Check if the event has been pending for unregistration */ + if (kvm_sdei_is_unregister_pending(registered_event, index)) { + ret = SDEI_PENDING; + goto unlock; + } + + ret = unregister_one_event(kvm, vcpu, registered_event); + +unlock: + spin_unlock(&ksdei->lock); + + return ret; +} + int kvm_sdei_hypercall(struct kvm_vcpu *vcpu) { struct kvm *kvm = vcpu->kvm; @@ -365,7 +494,11 @@ int kvm_sdei_hypercall(struct kvm_vcpu *vcpu) break; case SDEI_1_0_FN_SDEI_EVENT_COMPLETE: case SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME: + ret = SDEI_NOT_SUPPORTED; + break; case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: + ret = hypercall_unregister(vcpu); + break; case SDEI_1_0_FN_SDEI_EVENT_STATUS: case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: case SDEI_1_0_FN_SDEI_EVENT_ROUTING_SET: -- 2.23.0 _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
next prev parent reply other threads:[~2022-03-22 8:09 UTC|newest] Thread overview: 98+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-03-22 8:06 [PATCH v5 00/22] Support SDEI Virtualization Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 01/22] KVM: arm64: Introduce template for inline functions Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 19:42 ` Oliver Upton 2022-03-22 19:42 ` Oliver Upton 2022-03-23 12:16 ` Gavin Shan 2022-03-23 12:16 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 02/22] KVM: arm64: Add SDEI virtualization infrastructure Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 22:43 ` Oliver Upton 2022-03-22 22:43 ` Oliver Upton 2022-03-23 12:40 ` Gavin Shan 2022-03-23 12:40 ` Gavin Shan 2022-03-23 17:11 ` Oliver Upton 2022-03-23 17:11 ` Oliver Upton 2022-03-24 6:54 ` Gavin Shan 2022-03-24 6:54 ` Gavin Shan 2022-03-24 9:04 ` Oliver Upton 2022-03-24 9:04 ` Oliver Upton 2022-03-25 6:07 ` Gavin Shan 2022-03-25 6:07 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 03/22] KVM: arm64: Support SDEI_VERSION hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 18:04 ` Oliver Upton 2022-03-22 18:04 ` Oliver Upton 2022-03-23 12:46 ` Gavin Shan 2022-03-23 12:46 ` Gavin Shan 2022-03-23 16:31 ` Oliver Upton 2022-03-23 16:31 ` Oliver Upton 2022-03-24 4:07 ` Gavin Shan 2022-03-24 4:07 ` Gavin Shan 2022-03-24 7:48 ` Oliver Upton 2022-03-24 7:48 ` Oliver Upton 2022-03-25 6:11 ` Gavin Shan 2022-03-25 6:11 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 04/22] KVM: arm64: Support SDEI_EVENT_REGISTER hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 05/22] KVM: arm64: Support SDEI_EVENT_{ENABLE, DISABLE} hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 06/22] KVM: arm64: Support SDEI_EVENT_CONTEXT hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` Gavin Shan [this message] 2022-03-22 8:06 ` [PATCH v5 07/22] KVM: arm64: Support SDEI_EVENT_UNREGISTER hypercall Gavin Shan 2022-03-22 8:06 ` [PATCH v5 08/22] KVM: arm64: Support SDEI_EVENT_STATUS hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 09/22] KVM: arm64: Support SDEI_EVENT_GET_INFO hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 10/22] KVM: arm64: Support SDEI_EVENT_ROUTING_SET hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:06 ` [PATCH v5 11/22] KVM: arm64: Support SDEI_PE_{MASK, UNMASK} hypercall Gavin Shan 2022-03-22 8:06 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 12/22] KVM: arm64: Support SDEI_{PRIVATE, SHARED}_RESET Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 13/22] KVM: arm64: Support SDEI_FEATURES hypercall Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 14/22] KVM: arm64: Support SDEI event injection, delivery and cancellation Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 15/22] KVM: arm64: Support SDEI_EVENT_SIGNAL hypercall Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 23:06 ` Oliver Upton 2022-03-22 23:06 ` Oliver Upton 2022-03-23 12:52 ` Gavin Shan 2022-03-23 12:52 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 16/22] KVM: arm64: Support SDEI_EVENT_{COMPLETE,COMPLETE_AND_RESUME} hypercall Gavin Shan 2022-03-22 8:07 ` [PATCH v5 16/22] KVM: arm64: Support SDEI_EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall Gavin Shan 2022-03-22 8:07 ` [PATCH v5 17/22] KVM: arm64: Support SDEI event notifier Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 18/22] KVM: arm64: Support SDEI ioctl commands on VM Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-23 17:28 ` Oliver Upton 2022-03-23 17:28 ` Oliver Upton 2022-03-25 6:59 ` Gavin Shan 2022-03-25 6:59 ` Gavin Shan 2022-03-25 7:35 ` Oliver Upton 2022-03-25 7:35 ` Oliver Upton 2022-03-25 10:14 ` Gavin Shan 2022-03-25 10:14 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 19/22] KVM: arm64: Support SDEI ioctl commands on vCPU Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-23 17:55 ` Oliver Upton 2022-03-23 17:55 ` Oliver Upton 2022-03-25 7:59 ` Gavin Shan 2022-03-25 7:59 ` Gavin Shan 2022-03-25 8:37 ` Oliver Upton 2022-03-25 8:37 ` Oliver Upton 2022-03-25 10:23 ` Gavin Shan 2022-03-25 10:23 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 20/22] KVM: arm64: Export SDEI capability Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 21/22] KVM: arm64: Add SDEI document Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 8:07 ` [PATCH v5 22/22] KVM: selftests: Add SDEI test case Gavin Shan 2022-03-22 8:07 ` Gavin Shan 2022-03-22 18:13 ` [PATCH v5 00/22] Support SDEI Virtualization Oliver Upton 2022-03-22 18:13 ` Oliver Upton 2022-03-23 12:57 ` Gavin Shan 2022-03-23 12:57 ` Gavin Shan
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220322080710.51727-8-gshan@redhat.com \ --to=gshan@redhat.com \ --cc=Jonathan.Cameron@huawei.com \ --cc=drjones@redhat.com \ --cc=eauger@redhat.com \ --cc=james.morse@arm.com \ --cc=kvmarm@lists.cs.columbia.edu \ --cc=linux-kernel@vger.kernel.org \ --cc=mark.rutland@arm.com \ --cc=maz@kernel.org \ --cc=pbonzini@redhat.com \ --cc=shan.gavin@gmail.com \ --cc=shannon.zhaosl@gmail.com \ --cc=vkuznets@redhat.com \ --cc=will@kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.