All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: kvm-riscv@lists.infradead.org
Subject: [PATCH v3 02/19] KVM: arm64: Create a helper to check if IPA is valid
Date: Thu, 24 Feb 2022 12:06:33 +0000	[thread overview]
Message-ID: <871qzs4gfq.wl-maz@kernel.org> (raw)
In-Reply-To: <20220223041844.3984439-3-oupton@google.com>

On Wed, 23 Feb 2022 04:18:27 +0000,
Oliver Upton <oupton@google.com> wrote:
> 
> Create a helper that tests if a given IPA fits within the guest's
> address space.
> 
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---
>  arch/arm64/include/asm/kvm_mmu.h      | 9 +++++++++
>  arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 +-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 81839e9a8a24..78e8be7ea627 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -111,6 +111,7 @@ alternative_cb_end
>  #else
>  
>  #include <linux/pgtable.h>
> +#include <linux/kvm_host.h>

I'd rather you avoid that. This sort of linux->asm->linux transitive
inclusions always lead to a terrible mess at some point. Which is why
we use #defines below. And yes, the pgtable.h inclusion is a bad
precedent.

>  #include <asm/pgalloc.h>
>  #include <asm/cache.h>
>  #include <asm/cacheflush.h>
> @@ -147,6 +148,14 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
>  #define kvm_phys_size(kvm)		(_AC(1, ULL) << kvm_phys_shift(kvm))
>  #define kvm_phys_mask(kvm)		(kvm_phys_size(kvm) - _AC(1, ULL))
>  
> +/*
> + * Returns true if the provided IPA exists within the VM's IPA space.
> + */
> +static inline bool kvm_ipa_valid(struct kvm *kvm, phys_addr_t guest_ipa)
> +{
> +	return !(guest_ipa & ~kvm_phys_mask(kvm));
> +}
> +

I'm all for the helper, but just make it a #define to be consistent
with the rest of the code.

>  #include <asm/kvm_pgtable.h>
>  #include <asm/stage2_pgtable.h>
>  
> diff --git a/arch/arm64/kvm/vgic/vgic-kvm-device.c b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> index c6d52a1fd9c8..e3853a75cb00 100644
> --- a/arch/arm64/kvm/vgic/vgic-kvm-device.c
> +++ b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> @@ -27,7 +27,7 @@ int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
>  	if (addr + size < addr)
>  		return -EINVAL;
>  
> -	if (addr & ~kvm_phys_mask(kvm) || addr + size > kvm_phys_size(kvm))
> +	if (!kvm_ipa_valid(kvm, addr) || addr + size > kvm_phys_size(kvm))
>  		return -E2BIG;

I think you can pretty much use this helper everywhere something is
compared to kvm_phys_size(), and the above becomes:

 if (!kvm_ipa_valid(kvm, addr) || !kvm_ipa_valid(kvm, addr + size - 1))

Same this goes for the couple of occurrences in arch/arm64/kvm/mmu.c.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.


WARNING: multiple messages have this Message-ID (diff)
From: Marc Zyngier <maz@kernel.org>
To: Oliver Upton <oupton@google.com>
Cc: Wanpeng Li <wanpengli@tencent.com>,
	kvm@vger.kernel.org, Joerg Roedel <joro@8bytes.org>,
	Peter Shier <pshier@google.com>,
	kvm-riscv@lists.infradead.org,
	Atish Patra <atishp@atishpatra.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	kvmarm@lists.cs.columbia.edu, Jim Mattson <jmattson@google.com>
Subject: Re: [PATCH v3 02/19] KVM: arm64: Create a helper to check if IPA is valid
Date: Thu, 24 Feb 2022 12:06:33 +0000	[thread overview]
Message-ID: <871qzs4gfq.wl-maz@kernel.org> (raw)
In-Reply-To: <20220223041844.3984439-3-oupton@google.com>

On Wed, 23 Feb 2022 04:18:27 +0000,
Oliver Upton <oupton@google.com> wrote:
> 
> Create a helper that tests if a given IPA fits within the guest's
> address space.
> 
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---
>  arch/arm64/include/asm/kvm_mmu.h      | 9 +++++++++
>  arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 +-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 81839e9a8a24..78e8be7ea627 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -111,6 +111,7 @@ alternative_cb_end
>  #else
>  
>  #include <linux/pgtable.h>
> +#include <linux/kvm_host.h>

I'd rather you avoid that. This sort of linux->asm->linux transitive
inclusions always lead to a terrible mess at some point. Which is why
we use #defines below. And yes, the pgtable.h inclusion is a bad
precedent.

>  #include <asm/pgalloc.h>
>  #include <asm/cache.h>
>  #include <asm/cacheflush.h>
> @@ -147,6 +148,14 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
>  #define kvm_phys_size(kvm)		(_AC(1, ULL) << kvm_phys_shift(kvm))
>  #define kvm_phys_mask(kvm)		(kvm_phys_size(kvm) - _AC(1, ULL))
>  
> +/*
> + * Returns true if the provided IPA exists within the VM's IPA space.
> + */
> +static inline bool kvm_ipa_valid(struct kvm *kvm, phys_addr_t guest_ipa)
> +{
> +	return !(guest_ipa & ~kvm_phys_mask(kvm));
> +}
> +

I'm all for the helper, but just make it a #define to be consistent
with the rest of the code.

>  #include <asm/kvm_pgtable.h>
>  #include <asm/stage2_pgtable.h>
>  
> diff --git a/arch/arm64/kvm/vgic/vgic-kvm-device.c b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> index c6d52a1fd9c8..e3853a75cb00 100644
> --- a/arch/arm64/kvm/vgic/vgic-kvm-device.c
> +++ b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> @@ -27,7 +27,7 @@ int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
>  	if (addr + size < addr)
>  		return -EINVAL;
>  
> -	if (addr & ~kvm_phys_mask(kvm) || addr + size > kvm_phys_size(kvm))
> +	if (!kvm_ipa_valid(kvm, addr) || addr + size > kvm_phys_size(kvm))
>  		return -E2BIG;

I think you can pretty much use this helper everywhere something is
compared to kvm_phys_size(), and the above becomes:

 if (!kvm_ipa_valid(kvm, addr) || !kvm_ipa_valid(kvm, addr + size - 1))

Same this goes for the couple of occurrences in arch/arm64/kvm/mmu.c.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

WARNING: multiple messages have this Message-ID (diff)
From: Marc Zyngier <maz@kernel.org>
To: Oliver Upton <oupton@google.com>
Cc: kvmarm@lists.cs.columbia.edu, Paolo Bonzini <pbonzini@redhat.com>,
	James Morse <james.morse@arm.com>,
	Alexandru Elisei <alexandru.elisei@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Anup Patel <anup@brainfault.org>,
	Atish Patra <atishp@atishpatra.org>,
	Sean Christopherson <seanjc@google.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
	kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	Peter Shier <pshier@google.com>,
	Reiji Watanabe <reijiw@google.com>,
	Ricardo Koller <ricarkol@google.com>,
	Raghavendra Rao Ananta <rananta@google.com>,
	Jing Zhang <jingzhangos@google.com>
Subject: Re: [PATCH v3 02/19] KVM: arm64: Create a helper to check if IPA is valid
Date: Thu, 24 Feb 2022 12:06:33 +0000	[thread overview]
Message-ID: <871qzs4gfq.wl-maz@kernel.org> (raw)
In-Reply-To: <20220223041844.3984439-3-oupton@google.com>

On Wed, 23 Feb 2022 04:18:27 +0000,
Oliver Upton <oupton@google.com> wrote:
> 
> Create a helper that tests if a given IPA fits within the guest's
> address space.
> 
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---
>  arch/arm64/include/asm/kvm_mmu.h      | 9 +++++++++
>  arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 +-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 81839e9a8a24..78e8be7ea627 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -111,6 +111,7 @@ alternative_cb_end
>  #else
>  
>  #include <linux/pgtable.h>
> +#include <linux/kvm_host.h>

I'd rather you avoid that. This sort of linux->asm->linux transitive
inclusions always lead to a terrible mess at some point. Which is why
we use #defines below. And yes, the pgtable.h inclusion is a bad
precedent.

>  #include <asm/pgalloc.h>
>  #include <asm/cache.h>
>  #include <asm/cacheflush.h>
> @@ -147,6 +148,14 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
>  #define kvm_phys_size(kvm)		(_AC(1, ULL) << kvm_phys_shift(kvm))
>  #define kvm_phys_mask(kvm)		(kvm_phys_size(kvm) - _AC(1, ULL))
>  
> +/*
> + * Returns true if the provided IPA exists within the VM's IPA space.
> + */
> +static inline bool kvm_ipa_valid(struct kvm *kvm, phys_addr_t guest_ipa)
> +{
> +	return !(guest_ipa & ~kvm_phys_mask(kvm));
> +}
> +

I'm all for the helper, but just make it a #define to be consistent
with the rest of the code.

>  #include <asm/kvm_pgtable.h>
>  #include <asm/stage2_pgtable.h>
>  
> diff --git a/arch/arm64/kvm/vgic/vgic-kvm-device.c b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> index c6d52a1fd9c8..e3853a75cb00 100644
> --- a/arch/arm64/kvm/vgic/vgic-kvm-device.c
> +++ b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> @@ -27,7 +27,7 @@ int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
>  	if (addr + size < addr)
>  		return -EINVAL;
>  
> -	if (addr & ~kvm_phys_mask(kvm) || addr + size > kvm_phys_size(kvm))
> +	if (!kvm_ipa_valid(kvm, addr) || addr + size > kvm_phys_size(kvm))
>  		return -E2BIG;

I think you can pretty much use this helper everywhere something is
compared to kvm_phys_size(), and the above becomes:

 if (!kvm_ipa_valid(kvm, addr) || !kvm_ipa_valid(kvm, addr + size - 1))

Same this goes for the couple of occurrences in arch/arm64/kvm/mmu.c.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

  parent reply	other threads:[~2022-02-24 12:06 UTC|newest]

Thread overview: 141+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-23  4:18 [PATCH v3 00/19] KVM: arm64: Implement PSCI SYSTEM_SUSPEND Oliver Upton
2022-02-23  4:18 ` Oliver Upton
2022-02-23  4:18 ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 01/19] KVM: arm64: Drop unused param from kvm_psci_version() Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24  6:14   ` Reiji Watanabe
2022-02-24  6:14     ` Reiji Watanabe
2022-02-24  6:14     ` Reiji Watanabe
2022-02-23  4:18 ` [PATCH v3 02/19] KVM: arm64: Create a helper to check if IPA is valid Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24  6:32   ` Reiji Watanabe
2022-02-24  6:32     ` Reiji Watanabe
2022-02-24  6:32     ` Reiji Watanabe
2022-02-24 12:06   ` Marc Zyngier [this message]
2022-02-24 12:06     ` Marc Zyngier
2022-02-24 12:06     ` Marc Zyngier
2022-02-23  4:18 ` [PATCH v3 03/19] KVM: arm64: Reject invalid addresses for CPU_ON PSCI call Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24  6:55   ` Reiji Watanabe
2022-02-24  6:55     ` Reiji Watanabe
2022-02-24  6:55     ` Reiji Watanabe
2022-02-24 12:30   ` Marc Zyngier
2022-02-24 12:30     ` Marc Zyngier
2022-02-24 12:30     ` Marc Zyngier
2022-02-24 19:21     ` Oliver Upton
2022-02-24 19:21       ` Oliver Upton
2022-02-24 19:21       ` Oliver Upton
2022-02-25 15:35       ` Marc Zyngier
2022-02-25 15:35         ` Marc Zyngier
2022-02-25 15:35         ` Marc Zyngier
2022-02-23  4:18 ` [PATCH v3 04/19] KVM: arm64: Clean up SMC64 PSCI filtering for AArch32 guests Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 05/19] KVM: arm64: Dedupe vCPU power off helpers Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24  7:07   ` Reiji Watanabe
2022-02-24  7:07     ` Reiji Watanabe
2022-02-24  7:07     ` Reiji Watanabe
2022-02-23  4:18 ` [PATCH v3 06/19] KVM: arm64: Track vCPU power state using MP state values Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24 13:25   ` Marc Zyngier
2022-02-24 13:25     ` Marc Zyngier
2022-02-24 13:25     ` Marc Zyngier
2022-02-24 22:08     ` Oliver Upton
2022-02-24 22:08       ` Oliver Upton
2022-02-24 22:08       ` Oliver Upton
2022-02-25 15:37       ` Marc Zyngier
2022-02-25 15:37         ` Marc Zyngier
2022-02-25 15:37         ` Marc Zyngier
2022-02-23  4:18 ` [PATCH v3 07/19] KVM: arm64: Rename the KVM_REQ_SLEEP handler Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 08/19] KVM: arm64: Add reset helper that accepts caller-provided reset state Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 09/19] KVM: arm64: Implement PSCI SYSTEM_SUSPEND Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24 14:02   ` Marc Zyngier
2022-02-24 14:02     ` Marc Zyngier
2022-02-24 14:02     ` Marc Zyngier
2022-02-24 19:35     ` Oliver Upton
2022-02-24 19:35       ` Oliver Upton
2022-02-24 19:35       ` Oliver Upton
2022-02-25 18:58       ` Marc Zyngier
2022-02-25 18:58         ` Marc Zyngier
2022-02-25 18:58         ` Marc Zyngier
2022-03-03  1:01         ` Oliver Upton
2022-03-03  1:01           ` Oliver Upton
2022-03-03  1:01           ` Oliver Upton
2022-03-03 11:37           ` Marc Zyngier
2022-03-03 11:37             ` Marc Zyngier
2022-03-03 11:37             ` Marc Zyngier
2022-02-23  4:18 ` [PATCH v3 10/19] KVM: Create helper for setting a system event exit Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  6:37   ` Anup Patel
2022-02-23  6:37     ` Anup Patel
2022-02-23  6:37     ` Anup Patel
2022-02-24 14:07   ` Marc Zyngier
2022-02-24 14:07     ` Marc Zyngier
2022-02-24 14:07     ` Marc Zyngier
2022-02-23  4:18 ` [PATCH v3 11/19] KVM: arm64: Return a value from check_vcpu_requests() Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 12/19] KVM: arm64: Add support for userspace to suspend a vCPU Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24 15:12   ` Marc Zyngier
2022-02-24 15:12     ` Marc Zyngier
2022-02-24 15:12     ` Marc Zyngier
2022-02-24 19:47     ` Oliver Upton
2022-02-24 19:47       ` Oliver Upton
2022-02-24 19:47       ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 13/19] KVM: arm64: Add support KVM_SYSTEM_EVENT_SUSPEND to PSCI SYSTEM_SUSPEND Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-24 15:40   ` Marc Zyngier
2022-02-24 15:40     ` Marc Zyngier
2022-02-24 15:40     ` Marc Zyngier
2022-02-24 20:05     ` Oliver Upton
2022-02-24 20:05       ` Oliver Upton
2022-02-24 20:05       ` Oliver Upton
2022-02-26 11:29       ` Marc Zyngier
2022-02-26 11:29         ` Marc Zyngier
2022-02-26 11:29         ` Marc Zyngier
2022-02-26 18:28         ` Oliver Upton
2022-02-26 18:28           ` Oliver Upton
2022-02-26 18:28           ` Oliver Upton
2022-03-02  9:52           ` Marc Zyngier
2022-03-02  9:52             ` Marc Zyngier
2022-03-02  9:52             ` Marc Zyngier
2022-03-02  9:57             ` Oliver Upton
2022-03-02  9:57               ` Oliver Upton
2022-03-02  9:57               ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 14/19] KVM: arm64: Raise default PSCI version to v1.1 Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:26   ` Oliver Upton
2022-02-23  4:26     ` Oliver Upton
2022-02-23  4:26     ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 15/19] selftests: KVM: Rename psci_cpu_on_test to psci_test Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 16/19] selftests: KVM: Create helper for making SMCCC calls Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 17/19] selftests: KVM: Use KVM_SET_MP_STATE to power off vCPU in psci_test Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 18/19] selftests: KVM: Refactor psci_test to make it amenable to new tests Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18 ` [PATCH v3 19/19] selftests: KVM: Test SYSTEM_SUSPEND PSCI call Oliver Upton
2022-02-23  4:18   ` Oliver Upton
2022-02-23  4:18   ` Oliver Upton

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=871qzs4gfq.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=kvm-riscv@lists.infradead.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 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.