linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Reiji Watanabe <reijiw@google.com>
To: Marc Zyngier <maz@kernel.org>
Cc: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org,
	 Linux ARM <linux-arm-kernel@lists.infradead.org>,
	kernel-team@android.com,  Will Deacon <will@kernel.org>,
	Mark Brown <broonie@kernel.org>
Subject: Re: [PATCH 04/18] KVM: arm64: Move FP state ownership from flag to a tristate
Date: Sat, 4 Jun 2022 01:16:44 -0700	[thread overview]
Message-ID: <CAAeT=FydqQFoUcawxeR4wFD3=AJusYe16fSWhEWpQmkm8yPZTw@mail.gmail.com> (raw)
In-Reply-To: <20220528113829.1043361-5-maz@kernel.org>

On Sat, May 28, 2022 at 4:38 AM Marc Zyngier <maz@kernel.org> wrote:
>
> The KVM FP code uses a pair of flags to denote three states:
>
> - FP_ENABLED set: the guest owns the FP state
> - FP_HOST set: the host owns the FP state
> - FP_ENABLED and FP_HOST clear: nobody owns the FP state at all
>
> and both flags set is an illegal state, which nothing ever checks
> for...
>
> As it turns out, this isn't really a good match for flags, and
> we'd be better off if this was a simpler tristate, each state
> having a name that actually reflect the state:
>
> - FP_STATE_CLEAN
> - FP_STATE_HOST_DIRTY
> - FP_STATE_GUEST_DIRTY
>
> Kill the two flags, and move over to an enum encoding these
> three states. This results in less confusing code, and less risk of
> ending up in the uncharted territory of a 4th state if we forget
> to clear one of the two flags.
>
> Signed-off-by: Marc Zyngier <maz@kernel.org>

Reviewed-by: Reiji Watanabe <reijiw@google.com>

I have the same comment as I have for the patch-3 though.
(i.e. I think having kvm_arch_vcpu_load_fp() set vcpu->arch.fp_state to
FP_STATE_DIRTY_HOST only when FP is supported would be more consistent.)

Thanks,
Reiji

> ---
>  arch/arm64/include/asm/kvm_host.h       |  9 +++++++--
>  arch/arm64/kvm/fpsimd.c                 | 11 +++++------
>  arch/arm64/kvm/hyp/include/hyp/switch.h |  8 +++-----
>  arch/arm64/kvm/hyp/nvhe/switch.c        |  4 ++--
>  arch/arm64/kvm/hyp/vhe/switch.c         |  2 +-
>  5 files changed, 18 insertions(+), 16 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index 9252d71b4ac5..a46f952b97f6 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -328,6 +328,13 @@ struct kvm_vcpu_arch {
>         /* Exception Information */
>         struct kvm_vcpu_fault_info fault;
>
> +       /* Ownership of the FP regs */
> +       enum {
> +               FP_STATE_CLEAN,
> +               FP_STATE_DIRTY_HOST,
> +               FP_STATE_DIRTY_GUEST,
> +       } fp_state;
> +
>         /* Miscellaneous vcpu state flags */
>         u64 flags;
>
> @@ -433,8 +440,6 @@ struct kvm_vcpu_arch {
>
>  /* vcpu_arch flags field values: */
>  #define KVM_ARM64_DEBUG_DIRTY          (1 << 0)
> -#define KVM_ARM64_FP_ENABLED           (1 << 1) /* guest FP regs loaded */
> -#define KVM_ARM64_FP_HOST              (1 << 2) /* host FP regs loaded */
>  #define KVM_ARM64_HOST_SVE_ENABLED     (1 << 4) /* SVE enabled for EL0 */
>  #define KVM_ARM64_GUEST_HAS_SVE                (1 << 5) /* SVE exposed to guest */
>  #define KVM_ARM64_VCPU_SVE_FINALIZED   (1 << 6) /* SVE config completed */
> diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c
> index 9ebd89541281..0d82f6c5b110 100644
> --- a/arch/arm64/kvm/fpsimd.c
> +++ b/arch/arm64/kvm/fpsimd.c
> @@ -77,8 +77,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
>         BUG_ON(!current->mm);
>         BUG_ON(test_thread_flag(TIF_SVE));
>
> -       vcpu->arch.flags &= ~KVM_ARM64_FP_ENABLED;
> -       vcpu->arch.flags |= KVM_ARM64_FP_HOST;
> +       vcpu->arch.fp_state = FP_STATE_DIRTY_HOST;



>
>         vcpu->arch.flags &= ~KVM_ARM64_HOST_SVE_ENABLED;
>         if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
> @@ -100,7 +99,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
>
>                 if (read_sysreg_s(SYS_SVCR_EL0) &
>                     (SYS_SVCR_EL0_SM_MASK | SYS_SVCR_EL0_ZA_MASK)) {
> -                       vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
> +                       vcpu->arch.fp_state = FP_STATE_CLEAN;
>                         fpsimd_save_and_flush_cpu_state();
>                 }
>         }
> @@ -119,7 +118,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
>  void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
>  {
>         if (!system_supports_fpsimd() || test_thread_flag(TIF_FOREIGN_FPSTATE))
> -               vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED | KVM_ARM64_FP_HOST);
> +               vcpu->arch.fp_state = FP_STATE_CLEAN;
>  }
>
>  /*
> @@ -133,7 +132,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
>  {
>         WARN_ON_ONCE(!irqs_disabled());
>
> -       if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
> +       if (vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST) {
>                 /*
>                  * Currently we do not support SME guests so SVCR is
>                  * always 0 and we just need a variable to point to.
> @@ -176,7 +175,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
>                                          CPACR_EL1_SMEN_EL1EN);
>         }
>
> -       if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
> +       if (vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST) {
>                 if (vcpu_has_sve(vcpu)) {
>                         __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
>
> diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h
> index 1209248d2a3d..b22378abfb57 100644
> --- a/arch/arm64/kvm/hyp/include/hyp/switch.h
> +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h
> @@ -40,7 +40,7 @@ extern struct kvm_exception_table_entry __stop___kvm_ex_table;
>  /* Check whether the FP regs are owned by the guest */
>  static inline bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
>  {
> -       return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
> +       return vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST;
>  }
>
>  /* Save the 32-bit only FPSIMD system register state */
> @@ -179,10 +179,8 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
>         isb();
>
>         /* Write out the host state if it's in the registers */
> -       if (vcpu->arch.flags & KVM_ARM64_FP_HOST) {
> +       if (vcpu->arch.fp_state == FP_STATE_DIRTY_HOST)
>                 __fpsimd_save_state(vcpu->arch.host_fpsimd_state);
> -               vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
> -       }
>
>         /* Restore the guest state */
>         if (sve_guest)
> @@ -194,7 +192,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
>         if (!(read_sysreg(hcr_el2) & HCR_RW))
>                 write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
>
> -       vcpu->arch.flags |= KVM_ARM64_FP_ENABLED;
> +       vcpu->arch.fp_state = FP_STATE_DIRTY_GUEST;
>
>         return true;
>  }
> diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
> index a6b9f1186577..89e0f88c9006 100644
> --- a/arch/arm64/kvm/hyp/nvhe/switch.c
> +++ b/arch/arm64/kvm/hyp/nvhe/switch.c
> @@ -123,7 +123,7 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
>         }
>
>         cptr = CPTR_EL2_DEFAULT;
> -       if (vcpu_has_sve(vcpu) && (vcpu->arch.flags & KVM_ARM64_FP_ENABLED))
> +       if (vcpu_has_sve(vcpu) && (vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST))
>                 cptr |= CPTR_EL2_TZ;
>         if (cpus_have_final_cap(ARM64_SME))
>                 cptr &= ~CPTR_EL2_TSM;
> @@ -335,7 +335,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
>
>         __sysreg_restore_state_nvhe(host_ctxt);
>
> -       if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
> +       if (vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST)
>                 __fpsimd_save_fpexc32(vcpu);
>
>         __debug_switch_to_host(vcpu);
> diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
> index 46f365254e9f..258e87325c95 100644
> --- a/arch/arm64/kvm/hyp/vhe/switch.c
> +++ b/arch/arm64/kvm/hyp/vhe/switch.c
> @@ -175,7 +175,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
>
>         sysreg_restore_host_state_vhe(host_ctxt);
>
> -       if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
> +       if (vcpu->arch.fp_state == FP_STATE_DIRTY_GUEST)
>                 __fpsimd_save_fpexc32(vcpu);
>
>         __debug_switch_to_host(vcpu);
> --
> 2.34.1
>
> _______________________________________________
> kvmarm mailing list
> kvmarm@lists.cs.columbia.edu
> https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2022-06-04  8:18 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-28 11:38 [PATCH 00/18] KVM/arm64: Refactoring the vcpu flags Marc Zyngier
2022-05-28 11:38 ` [PATCH 01/18] KVM: arm64: Always start with clearing SVE flag on load Marc Zyngier
2022-05-30 14:41   ` Mark Brown
2022-06-06 11:28     ` Marc Zyngier
2022-06-06 12:16       ` Mark Brown
2022-05-28 11:38 ` [PATCH 02/18] KVM: arm64: Always start with clearing SME " Marc Zyngier
2022-05-30 14:51   ` Mark Brown
2022-05-28 11:38 ` [PATCH 03/18] KVM: arm64: Drop FP_FOREIGN_STATE from the hypervisor code Marc Zyngier
2022-06-03  5:23   ` Reiji Watanabe
2022-06-04  8:10     ` Marc Zyngier
2022-06-07  4:47       ` Reiji Watanabe
2022-06-03  9:09   ` Mark Brown
2022-05-28 11:38 ` [PATCH 04/18] KVM: arm64: Move FP state ownership from flag to a tristate Marc Zyngier
2022-06-03  9:14   ` Mark Brown
2022-06-06  8:41     ` Marc Zyngier
2022-06-06 10:31       ` Mark Brown
2022-06-04  8:16   ` Reiji Watanabe [this message]
2022-05-28 11:38 ` [PATCH 05/18] KVM: arm64: Add helpers to manipulate vcpu flags among a set Marc Zyngier
2022-06-08  5:26   ` Reiji Watanabe
2022-06-08  6:51     ` Marc Zyngier
2022-06-09  2:25       ` Reiji Watanabe
2022-05-28 11:38 ` [PATCH 06/18] KVM: arm64: Add three sets of flags to the vcpu state Marc Zyngier
2022-06-08 15:23   ` Fuad Tabba
2022-06-09  6:10   ` Reiji Watanabe
2022-06-09  7:46     ` Marc Zyngier
2022-06-09 17:24       ` Reiji Watanabe
2022-06-10  7:48         ` Marc Zyngier
2022-05-28 11:38 ` [PATCH 07/18] KVM: arm64: Move vcpu configuration flags into their own set Marc Zyngier
2022-06-09  6:15   ` Reiji Watanabe
2022-05-28 11:38 ` [PATCH 08/18] KVM: arm64: Move vcpu PC/Exception flags to the input flag set Marc Zyngier
2022-06-10  6:13   ` Reiji Watanabe
2022-05-28 11:38 ` [PATCH 09/18] KVM: arm64: Move vcpu debug/SPE/TRBE " Marc Zyngier
2022-06-08 15:16   ` Fuad Tabba
2022-06-08 16:01     ` Marc Zyngier
2022-05-28 11:38 ` [PATCH 10/18] KVM: arm64: Move vcpu SVE/SME flags to the state " Marc Zyngier
2022-05-28 11:38 ` [PATCH 11/18] KVM: arm64: Move vcpu ON_UNSUPPORTED_CPU flag " Marc Zyngier
2022-05-28 11:38 ` [PATCH 12/18] KVM: arm64: Move vcpu WFIT " Marc Zyngier
2022-05-28 11:38 ` [PATCH 13/18] KVM: arm64: Kill unused vcpu flags field Marc Zyngier
2022-05-28 11:38 ` [PATCH 14/18] KVM: arm64: Convert vcpu sysregs_loaded_on_cpu to a state flag Marc Zyngier
2022-05-28 11:38 ` [PATCH 15/18] KVM: arm64: Warn when PENDING_EXCEPTION and INCREMENT_PC are set together Marc Zyngier
2022-06-08 15:16   ` Fuad Tabba
2022-06-08 16:42     ` Marc Zyngier
2022-05-28 11:38 ` [PATCH 16/18] KVM: arm64: Add build-time sanity checks for flags Marc Zyngier
2022-05-28 11:38 ` [PATCH 17/18] KVM: arm64: Reduce the size of the vcpu flag members Marc Zyngier
2022-05-28 11:38 ` [PATCH 18/18] KVM: arm64: Document why pause cannot be turned into a flag Marc Zyngier
2022-05-30  8:28 ` [PATCH 00/18] KVM/arm64: Refactoring the vcpu flags Marc Zyngier
2022-06-07 13:43 ` Marc Zyngier

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='CAAeT=FydqQFoUcawxeR4wFD3=AJusYe16fSWhEWpQmkm8yPZTw@mail.gmail.com' \
    --to=reijiw@google.com \
    --cc=broonie@kernel.org \
    --cc=kernel-team@android.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=maz@kernel.org \
    --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: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).