Re: [PATCH 4/7] mshv: Optimize memory region mapping operations
From: Anirudh Rayabharam
Date: Mon Apr 06 2026 - 12:58:07 EST
On Wed, Apr 01, 2026 at 10:12:46PM +0000, Stanislav Kinsburskii wrote:
> Two specific operations don't require PFN iteration: region unmapping
> and region remapping with no access. For unmapping, all frames in MSHV
> memory regions are guaranteed to be mapped with page access, so we can
> unmap them all without checking individual PFNs. For remapping with no
> access, all frames are already mapped with page access, allowing us to
> unmap them all in one pass.
>
> Since neither operation needs PFN validation, iterating over PFNs is
> redundant. Batch operations into large page-aligned chunks followed by
> remaining pages. This eliminates PFN traversal for these operations,
> requires no additional hypercalls compared to the PFN-checking approach,
> and provides the simplest possible sequential execution path.
>
> The optimization utilizes HV_MAP_GPA_LARGE_PAGE and
> HV_UNMAP_GPA_LARGE_PAGE flags for aligned portions, processing only the
> remainder with base page granularity. This removes mshv_region_chunk_unmap()
> and mshv_region_process_range() helper functions, reducing code complexity.
>
> Signed-off-by: Stanislav Kinsburskii <skinsburskii@xxxxxxxxxxxxxxxxxxx>
> ---
> drivers/hv/mshv_regions.c | 65 ++++++++++++++++++++++++++++++++-------------
> 1 file changed, 46 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/hv/mshv_regions.c b/drivers/hv/mshv_regions.c
> index 2c4215381e0b..a92381219758 100644
> --- a/drivers/hv/mshv_regions.c
> +++ b/drivers/hv/mshv_regions.c
> @@ -449,27 +449,27 @@ static int mshv_region_pin(struct mshv_region *region)
> return ret < 0 ? ret : -ENOMEM;
> }
>
> -static int mshv_region_chunk_unmap(struct mshv_region *region,
> - u32 flags,
> - u64 pfn_offset, u64 pfn_count,
> - bool huge_page)
> +static int mshv_region_unmap(struct mshv_region *region)
> {
> - if (!pfn_valid(region->mreg_pfns[pfn_offset]))
> - return 0;
> + u64 aligned_pages, remaining_pages;
> + int ret = 0;
>
> - if (huge_page)
> - flags |= HV_UNMAP_GPA_LARGE_PAGE;
> + aligned_pages = ALIGN_DOWN(region->nr_pfns, PTRS_PER_PMD);
Why is it sufficient to check just the number of pages to determine
whether we need to use the HV_UNMAP_GPA_LARGE_PAGE? Don't we need to
check the alignment of the start address as well?
Thanks,
Anirudh.