[PATCH v2 07/30] KVM: arm64: Simplify nested VMA shift calculation

Anshuman Khandual anshuman.khandual at arm.com
Mon Mar 30 20:56:08 PDT 2026


On 27/03/26 5:05 PM, Marc Zyngier wrote:
> From: Fuad Tabba <tabba at google.com>
> 
> In the kvm_s2_resolve_vma_size() helper, the local variable vma_pagesize
> is calculated from vma_shift, only to be used to bound the vma_pagesize
> by max_map_size and subsequently convert it back to a shift via __ffs().
> 
> Because vma_pagesize and max_map_size are both powers of two, we can
> simplify the logic by omitting vma_pagesize entirely and bounding the
> vma_shift directly using the shift of max_map_size. This achieves the
> same result while keeping the size-to-shift conversion out of the helper
> logic.
> 
> Signed-off-by: Fuad Tabba <tabba at google.com>
> Signed-off-by: Marc Zyngier <maz at kernel.org>

Reviewed-by: Anshuman Khandual <anshuman.khandual at arm.com>

> ---
>  arch/arm64/kvm/mmu.c | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 164f1160ea33d..5572b127f8663 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -1646,7 +1646,6 @@ static short kvm_s2_resolve_vma_size(struct vm_area_struct *vma,
>  				     bool *force_pte, phys_addr_t *ipa)
>  {
>  	short vma_shift;
> -	long vma_pagesize;
>  
>  	if (*force_pte)
>  		vma_shift = PAGE_SHIFT;
> @@ -1677,8 +1676,6 @@ static short kvm_s2_resolve_vma_size(struct vm_area_struct *vma,
>  		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
>  	}
>  
> -	vma_pagesize = 1UL << vma_shift;
> -
>  	if (nested) {
>  		unsigned long max_map_size;
>  
> @@ -1703,8 +1700,7 @@ static short kvm_s2_resolve_vma_size(struct vm_area_struct *vma,
>  			max_map_size = PAGE_SIZE;
>  
>  		*force_pte = (max_map_size == PAGE_SIZE);
> -		vma_pagesize = min_t(long, vma_pagesize, max_map_size);
> -		vma_shift = __ffs(vma_pagesize);
> +		vma_shift = min_t(short, vma_shift, __ffs(max_map_size));
>  	}
>  
>  	return vma_shift;




More information about the linux-arm-kernel mailing list