Skip to content

Commit 6f6aed2

Browse files
Stanislav Kinsburskiiliuw
authored andcommitted
mshv: Centralize guest memory region destruction
Centralize guest memory region destruction to prevent resource leaks and inconsistent cleanup across unmap and partition destruction paths. Unify region removal, encrypted partition access recovery, and region invalidation to improve maintainability and reliability. Reduce code duplication and make future updates less error-prone by encapsulating cleanup logic in a single helper. Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com> Reviewed-by: Nuno Das Neves <nunodasneves@linux.microsoft.com> Reviewed-by: Anirudh Rayabharam (Microsoft) <anirudh@anirudhrb.com> Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent df4ff5f commit 6f6aed2

1 file changed

Lines changed: 34 additions & 31 deletions

File tree

drivers/hv/mshv_root_main.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,13 +1356,42 @@ mshv_map_user_memory(struct mshv_partition *partition,
13561356
return ret;
13571357
}
13581358

1359+
static void mshv_partition_destroy_region(struct mshv_mem_region *region)
1360+
{
1361+
struct mshv_partition *partition = region->partition;
1362+
u32 unmap_flags = 0;
1363+
int ret;
1364+
1365+
hlist_del(&region->hnode);
1366+
1367+
if (mshv_partition_encrypted(partition)) {
1368+
ret = mshv_partition_region_share(region);
1369+
if (ret) {
1370+
pt_err(partition,
1371+
"Failed to regain access to memory, unpinning user pages will fail and crash the host error: %d\n",
1372+
ret);
1373+
return;
1374+
}
1375+
}
1376+
1377+
if (region->flags.large_pages)
1378+
unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE;
1379+
1380+
/* ignore unmap failures and continue as process may be exiting */
1381+
hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn,
1382+
region->nr_pages, unmap_flags);
1383+
1384+
mshv_region_invalidate(region);
1385+
1386+
vfree(region);
1387+
}
1388+
13591389
/* Called for unmapping both the guest ram and the mmio space */
13601390
static long
13611391
mshv_unmap_user_memory(struct mshv_partition *partition,
13621392
struct mshv_user_mem_region mem)
13631393
{
13641394
struct mshv_mem_region *region;
1365-
u32 unmap_flags = 0;
13661395

13671396
if (!(mem.flags & BIT(MSHV_SET_MEM_BIT_UNMAP)))
13681397
return -EINVAL;
@@ -1377,18 +1406,8 @@ mshv_unmap_user_memory(struct mshv_partition *partition,
13771406
region->nr_pages != HVPFN_DOWN(mem.size))
13781407
return -EINVAL;
13791408

1380-
hlist_del(&region->hnode);
1409+
mshv_partition_destroy_region(region);
13811410

1382-
if (region->flags.large_pages)
1383-
unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE;
1384-
1385-
/* ignore unmap failures and continue as process may be exiting */
1386-
hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn,
1387-
region->nr_pages, unmap_flags);
1388-
1389-
mshv_region_invalidate(region);
1390-
1391-
vfree(region);
13921411
return 0;
13931412
}
13941413

@@ -1724,8 +1743,8 @@ static void destroy_partition(struct mshv_partition *partition)
17241743
{
17251744
struct mshv_vp *vp;
17261745
struct mshv_mem_region *region;
1727-
int i, ret;
17281746
struct hlist_node *n;
1747+
int i;
17291748

17301749
if (refcount_read(&partition->pt_ref_count)) {
17311750
pt_err(partition,
@@ -1789,25 +1808,9 @@ static void destroy_partition(struct mshv_partition *partition)
17891808

17901809
remove_partition(partition);
17911810

1792-
/* Remove regions, regain access to the memory and unpin the pages */
17931811
hlist_for_each_entry_safe(region, n, &partition->pt_mem_regions,
1794-
hnode) {
1795-
hlist_del(&region->hnode);
1796-
1797-
if (mshv_partition_encrypted(partition)) {
1798-
ret = mshv_partition_region_share(region);
1799-
if (ret) {
1800-
pt_err(partition,
1801-
"Failed to regain access to memory, unpinning user pages will fail and crash the host error: %d\n",
1802-
ret);
1803-
return;
1804-
}
1805-
}
1806-
1807-
mshv_region_invalidate(region);
1808-
1809-
vfree(region);
1810-
}
1812+
hnode)
1813+
mshv_partition_destroy_region(region);
18111814

18121815
/* Withdraw and free all pages we deposited */
18131816
hv_call_withdraw_memory(U64_MAX, NUMA_NO_NODE, partition->pt_id);

0 commit comments

Comments
 (0)