Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Stanislav Kinsburskii and committed by
Wei Liu
6f6aed2c df4ff5f6

+34 -31
+34 -31
drivers/hv/mshv_root_main.c
··· 1356 1356 return ret; 1357 1357 } 1358 1358 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 + 1359 1389 /* Called for unmapping both the guest ram and the mmio space */ 1360 1390 static long 1361 1391 mshv_unmap_user_memory(struct mshv_partition *partition, 1362 1392 struct mshv_user_mem_region mem) 1363 1393 { 1364 1394 struct mshv_mem_region *region; 1365 - u32 unmap_flags = 0; 1366 1395 1367 1396 if (!(mem.flags & BIT(MSHV_SET_MEM_BIT_UNMAP))) 1368 1397 return -EINVAL; ··· 1406 1377 region->nr_pages != HVPFN_DOWN(mem.size)) 1407 1378 return -EINVAL; 1408 1379 1409 - hlist_del(&region->hnode); 1380 + mshv_partition_destroy_region(region); 1410 1381 1411 - if (region->flags.large_pages) 1412 - unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE; 1413 - 1414 - /* ignore unmap failures and continue as process may be exiting */ 1415 - hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn, 1416 - region->nr_pages, unmap_flags); 1417 - 1418 - mshv_region_invalidate(region); 1419 - 1420 - vfree(region); 1421 1382 return 0; 1422 1383 } 1423 1384 ··· 1743 1724 { 1744 1725 struct mshv_vp *vp; 1745 1726 struct mshv_mem_region *region; 1746 - int i, ret; 1747 1727 struct hlist_node *n; 1728 + int i; 1748 1729 1749 1730 if (refcount_read(&partition->pt_ref_count)) { 1750 1731 pt_err(partition, ··· 1808 1789 1809 1790 remove_partition(partition); 1810 1791 1811 - /* Remove regions, regain access to the memory and unpin the pages */ 1812 1792 hlist_for_each_entry_safe(region, n, &partition->pt_mem_regions, 1813 - hnode) { 1814 - hlist_del(&region->hnode); 1815 - 1816 - if (mshv_partition_encrypted(partition)) { 1817 - ret = mshv_partition_region_share(region); 1818 - if (ret) { 1819 - pt_err(partition, 1820 - "Failed to regain access to memory, unpinning user pages will fail and crash the host error: %d\n", 1821 - ret); 1822 - return; 1823 - } 1824 - } 1825 - 1826 - mshv_region_invalidate(region); 1827 - 1828 - vfree(region); 1829 - } 1793 + hnode) 1794 + mshv_partition_destroy_region(region); 1830 1795 1831 1796 /* Withdraw and free all pages we deposited */ 1832 1797 hv_call_withdraw_memory(U64_MAX, NUMA_NO_NODE, partition->pt_id);