Move mshv_prepare_pinned_region() from mshv_root_main.c to mshv_regions.c and rename it to mshv_map_pinned_region(). This co-locates the pinned region logic with the rest of the memory region operations.
Make mshv_region_pin(), mshv_region_map(), mshv_region_share(), mshv_region_unshare(), and mshv_region_invalidate() static, as they are no longer called outside of mshv_regions.c. No functional change. Signed-off-by: Stanislav Kinsburskii <[email protected]> --- drivers/hv/mshv_regions.c | 83 ++++++++++++++++++++++++++++++++++++++++--- drivers/hv/mshv_root.h | 6 +-- drivers/hv/mshv_root_main.c | 75 +-------------------------------------- 3 files changed, 80 insertions(+), 84 deletions(-) diff --git a/drivers/hv/mshv_regions.c b/drivers/hv/mshv_regions.c index 057fc83895d37..d9e33e9ef8550 100644 --- a/drivers/hv/mshv_regions.c +++ b/drivers/hv/mshv_regions.c @@ -240,7 +240,7 @@ static int mshv_region_chunk_share(struct mshv_mem_region *region, flags, true); } -int mshv_region_share(struct mshv_mem_region *region) +static int mshv_region_share(struct mshv_mem_region *region) { u32 flags = HV_MODIFY_SPA_PAGE_HOST_ACCESS_MAKE_SHARED; @@ -266,7 +266,7 @@ static int mshv_region_chunk_unshare(struct mshv_mem_region *region, flags, false); } -int mshv_region_unshare(struct mshv_mem_region *region) +static int mshv_region_unshare(struct mshv_mem_region *region) { u32 flags = HV_MODIFY_SPA_PAGE_HOST_ACCESS_MAKE_EXCLUSIVE; @@ -306,7 +306,7 @@ static int mshv_region_remap_pfns(struct mshv_mem_region *region, mshv_region_chunk_remap); } -int mshv_region_map(struct mshv_mem_region *region) +static int mshv_region_map(struct mshv_mem_region *region) { u32 map_flags = region->hv_map_flags; @@ -330,12 +330,12 @@ static void mshv_region_invalidate_pfns(struct mshv_mem_region *region, mshv_region_init_pfns_range(region, pfn_offset, pfn_count); } -void mshv_region_invalidate(struct mshv_mem_region *region) +static void mshv_region_invalidate(struct mshv_mem_region *region) { mshv_region_invalidate_pfns(region, 0, region->nr_pfns); } -int mshv_region_pin(struct mshv_mem_region *region) +static int mshv_region_pin(struct mshv_mem_region *region) { u64 done_count, nr_pfns, i; unsigned long *pfns; @@ -691,3 +691,76 @@ bool mshv_region_movable_init(struct mshv_mem_region *region) return true; } + +/** + * mshv_map_pinned_region - Pin and map memory regions + * @region: Pointer to the memory region structure + * + * This function processes memory regions that are explicitly marked as pinned. + * Pinned regions are preallocated, mapped upfront, and do not rely on fault-based + * population. The function ensures the region is properly populated, handles + * encryption requirements for SNP partitions if applicable, maps the region, + * and performs necessary sharing or eviction operations based on the mapping + * result. + * + * Return: 0 on success, negative error code on failure. + */ +int mshv_map_pinned_region(struct mshv_mem_region *region) +{ + struct mshv_partition *partition = region->partition; + int ret; + + ret = mshv_region_pin(region); + if (ret) { + pt_err(partition, "Failed to pin memory region: %d\n", + ret); + goto err_out; + } + + /* + * For an SNP partition it is a requirement that for every memory region + * that we are going to map for this partition we should make sure that + * host access to that region is released. This is ensured by doing an + * additional hypercall which will update the SLAT to release host + * access to guest memory regions. + */ + if (mshv_partition_encrypted(partition)) { + ret = mshv_region_unshare(region); + if (ret) { + pt_err(partition, + "Failed to unshare memory region (guest_pfn: %llu): %d\n", + region->start_gfn, ret); + goto err_out; + } + } + + ret = mshv_region_map(region); + if (ret) + goto share_region; + + return 0; + +share_region: + if (mshv_partition_encrypted(partition)) { + int shrc; + + shrc = mshv_region_share(region); + if (!shrc) + goto err_out; + + pt_err(partition, + "Failed to share memory region (guest_pfn: %llu): %d\n", + region->start_gfn, shrc); + /* + * Re-sharing failed — the pages remain inaccessible to the + * host. Zero the page array so that mshv_region_destroy() + * won't attempt to unpin them (leaking the page references + * is intentional; unpinning host-inaccessible pages would be + * unsafe). + */ + mshv_region_init_pfns(region); + goto err_out; + } +err_out: + return ret; +} diff --git a/drivers/hv/mshv_root.h b/drivers/hv/mshv_root.h index 34c9b57c50f47..2a4eff27917f2 100644 --- a/drivers/hv/mshv_root.h +++ b/drivers/hv/mshv_root.h @@ -371,16 +371,12 @@ extern u8 * __percpu *hv_synic_eventring_tail; struct mshv_mem_region *mshv_region_create(u64 guest_pfn, u64 nr_pages, u64 uaddr, u32 flags); -int mshv_region_share(struct mshv_mem_region *region); -int mshv_region_unshare(struct mshv_mem_region *region); -int mshv_region_map(struct mshv_mem_region *region); -void mshv_region_invalidate(struct mshv_mem_region *region); void mshv_region_init_pfns(struct mshv_mem_region *region); -int mshv_region_pin(struct mshv_mem_region *region); void mshv_region_put(struct mshv_mem_region *region); int mshv_region_get(struct mshv_mem_region *region); bool mshv_region_handle_gfn_fault(struct mshv_mem_region *region, u64 gfn); void mshv_region_movable_fini(struct mshv_mem_region *region); bool mshv_region_movable_init(struct mshv_mem_region *region); +int mshv_map_pinned_region(struct mshv_mem_region *region); #endif /* _MSHV_ROOT_H_ */ diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c index 986cb9a4c428e..4af2b98738ee2 100644 --- a/drivers/hv/mshv_root_main.c +++ b/drivers/hv/mshv_root_main.c @@ -1346,79 +1346,6 @@ static int mshv_partition_create_region(struct mshv_partition *partition, return 0; } -/** - * mshv_prepare_pinned_region - Pin and map memory regions - * @region: Pointer to the memory region structure - * - * This function processes memory regions that are explicitly marked as pinned. - * Pinned regions are preallocated, mapped upfront, and do not rely on fault-based - * population. The function ensures the region is properly populated, handles - * encryption requirements for SNP partitions if applicable, maps the region, - * and performs necessary sharing or eviction operations based on the mapping - * result. - * - * Return: 0 on success, negative error code on failure. - */ -static int mshv_prepare_pinned_region(struct mshv_mem_region *region) -{ - struct mshv_partition *partition = region->partition; - int ret; - - ret = mshv_region_pin(region); - if (ret) { - pt_err(partition, "Failed to pin memory region: %d\n", - ret); - goto err_out; - } - - /* - * For an SNP partition it is a requirement that for every memory region - * that we are going to map for this partition we should make sure that - * host access to that region is released. This is ensured by doing an - * additional hypercall which will update the SLAT to release host - * access to guest memory regions. - */ - if (mshv_partition_encrypted(partition)) { - ret = mshv_region_unshare(region); - if (ret) { - pt_err(partition, - "Failed to unshare memory region (guest_pfn: %llu): %d\n", - region->start_gfn, ret); - goto err_out; - } - } - - ret = mshv_region_map(region); - if (ret) - goto share_region; - - return 0; - -share_region: - if (mshv_partition_encrypted(partition)) { - int shrc; - - shrc = mshv_region_share(region); - if (!shrc) - goto err_out; - - pt_err(partition, - "Failed to share memory region (guest_pfn: %llu): %d\n", - region->start_gfn, shrc); - /* - * Re-sharing failed — the pages remain inaccessible to the - * host. Zero the page array so that mshv_region_destroy() - * won't attempt to unpin them (leaking the page references - * is intentional; unpinning host-inaccessible pages would be - * unsafe). - */ - mshv_region_init_pfns(region); - goto err_out; - } -err_out: - return ret; -} - /* * This maps two things: guest RAM and for pci passthru mmio space. * @@ -1461,7 +1388,7 @@ mshv_map_user_memory(struct mshv_partition *partition, switch (region->mreg_type) { case MSHV_REGION_TYPE_MEM_PINNED: - ret = mshv_prepare_pinned_region(region); + ret = mshv_map_pinned_region(region); break; case MSHV_REGION_TYPE_MEM_MOVABLE: /*

