diff mbox series

[2/2] KVM: arm64: Minor optimization of range_is_memory

Message ID 20210728153232.1018911-3-dbrazdil@google.com (mailing list archive)
State New, archived
Headers show
Series Fix off-by-one in range_is_memory | expand

Commit Message

David Brazdil July 28, 2021, 3:32 p.m. UTC
Currently range_is_memory finds the corresponding struct memblock_region
for both the lower and upper bounds of the given address range with two
rounds of binary search, and then checks that the two memblocks are the
same. Simplify this by only doing binary search on the lower bound and
then checking that the upper bound is in the same memblock.

Signed-off-by: David Brazdil <dbrazdil@google.com>
---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Comments

Quentin Perret July 29, 2021, 5 p.m. UTC | #1
On Wednesday 28 Jul 2021 at 15:32:32 (+0000), David Brazdil wrote:
> Currently range_is_memory finds the corresponding struct memblock_region
> for both the lower and upper bounds of the given address range with two
> rounds of binary search, and then checks that the two memblocks are the
> same. Simplify this by only doing binary search on the lower bound and
> then checking that the upper bound is in the same memblock.
> 
> Signed-off-by: David Brazdil <dbrazdil@google.com>
> ---
>  arch/arm64/kvm/hyp/nvhe/mem_protect.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> index a6ce991b1467..37d73af69634 100644
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> @@ -189,13 +189,18 @@ static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
>  	return false;
>  }
>  
> +static bool is_in_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
> +{

Nit: addr@ could be u64 for consistency -- struct kvm_mem_range holds
IPAs in general.

> +	return range->start <= addr && addr < range->end;
> +}
> +
>  static bool range_is_memory(u64 start, u64 end)
>  {
> -	struct kvm_mem_range r1, r2;
> +	struct kvm_mem_range r;
>  
> -	if (!find_mem_range(start, &r1) || !find_mem_range(end - 1, &r2))
> +	if (!find_mem_range(start, &r))
>  		return false;
> -	if (r1.start != r2.start)
> +	if (!is_in_mem_range(end - 1, &r))
>  		return false;
>  
>  	return true;

Nit: maybe drop the second if and simplify to:

	return is_in_mem_range(end - 1, &r);

With that:

Reviewed-by: Quentin Perret <qperret@google.com>

Thanks,
Quentin
diff mbox series

Patch

diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index a6ce991b1467..37d73af69634 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -189,13 +189,18 @@  static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
 	return false;
 }
 
+static bool is_in_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
+{
+	return range->start <= addr && addr < range->end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
-	struct kvm_mem_range r1, r2;
+	struct kvm_mem_range r;
 
-	if (!find_mem_range(start, &r1) || !find_mem_range(end - 1, &r2))
+	if (!find_mem_range(start, &r))
 		return false;
-	if (r1.start != r2.start)
+	if (!is_in_mem_range(end - 1, &r))
 		return false;
 
 	return true;