From: Anirudh Rayabharam <anirudh@anirudhrb.com>
To: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Cc: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org,
decui@microsoft.com, linux-hyperv@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v7 2/7] Drivers: hv: Centralize guest memory region destruction
Date: Mon, 1 Dec 2025 11:12:19 +0000 [thread overview]
Message-ID: <aS14E-QpVccRc6Gz@anirudh-surface.localdomain> (raw)
In-Reply-To: <176412293764.447063.2221992979416155779.stgit@skinsburskii-cloud-desktop.internal.cloudapp.net>
On Wed, Nov 26, 2025 at 02:08:57AM +0000, Stanislav Kinsburskii wrote:
> 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>
> ---
> drivers/hv/mshv_root_main.c | 65 ++++++++++++++++++++++---------------------
> 1 file changed, 34 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> index fec82619684a..ec18984c3f2d 100644
> --- a/drivers/hv/mshv_root_main.c
> +++ b/drivers/hv/mshv_root_main.c
> @@ -1356,13 +1356,42 @@ mshv_map_user_memory(struct mshv_partition *partition,
> return ret;
> }
>
> +static void mshv_partition_destroy_region(struct mshv_mem_region *region)
> +{
> + struct mshv_partition *partition = region->partition;
> + u32 unmap_flags = 0;
> + int ret;
> +
> + hlist_del(®ion->hnode);
> +
> + if (mshv_partition_encrypted(partition)) {
> + ret = mshv_partition_region_share(region);
> + if (ret) {
> + pt_err(partition,
> + "Failed to regain access to memory, unpinning user pages will fail and crash the host error: %d\n",
> + ret);
> + return;
> + }
> + }
> +
> + if (region->flags.large_pages)
> + unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE;
> +
> + /* ignore unmap failures and continue as process may be exiting */
> + hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn,
> + region->nr_pages, unmap_flags);
> +
> + mshv_region_invalidate(region);
> +
> + vfree(region);
> +}
> +
> /* Called for unmapping both the guest ram and the mmio space */
> static long
> mshv_unmap_user_memory(struct mshv_partition *partition,
> struct mshv_user_mem_region mem)
> {
> struct mshv_mem_region *region;
> - u32 unmap_flags = 0;
>
> if (!(mem.flags & BIT(MSHV_SET_MEM_BIT_UNMAP)))
> return -EINVAL;
> @@ -1377,18 +1406,8 @@ mshv_unmap_user_memory(struct mshv_partition *partition,
> region->nr_pages != HVPFN_DOWN(mem.size))
> return -EINVAL;
>
> - hlist_del(®ion->hnode);
> + mshv_partition_destroy_region(region);
>
> - if (region->flags.large_pages)
> - unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE;
> -
> - /* ignore unmap failures and continue as process may be exiting */
> - hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn,
> - region->nr_pages, unmap_flags);
> -
> - mshv_region_invalidate(region);
> -
> - vfree(region);
> return 0;
> }
>
> @@ -1724,8 +1743,8 @@ static void destroy_partition(struct mshv_partition *partition)
> {
> struct mshv_vp *vp;
> struct mshv_mem_region *region;
> - int i, ret;
> struct hlist_node *n;
> + int i;
>
> if (refcount_read(&partition->pt_ref_count)) {
> pt_err(partition,
> @@ -1789,25 +1808,9 @@ static void destroy_partition(struct mshv_partition *partition)
>
> remove_partition(partition);
>
> - /* Remove regions, regain access to the memory and unpin the pages */
> hlist_for_each_entry_safe(region, n, &partition->pt_mem_regions,
> - hnode) {
> - hlist_del(®ion->hnode);
> -
> - if (mshv_partition_encrypted(partition)) {
> - ret = mshv_partition_region_share(region);
> - if (ret) {
> - pt_err(partition,
> - "Failed to regain access to memory, unpinning user pages will fail and crash the host error: %d\n",
> - ret);
> - return;
> - }
> - }
> -
> - mshv_region_invalidate(region);
> -
> - vfree(region);
> - }
> + hnode)
> + mshv_partition_destroy_region(region);
>
> /* Withdraw and free all pages we deposited */
> hv_call_withdraw_memory(U64_MAX, NUMA_NO_NODE, partition->pt_id);
>
>
Reviewed-by: Anirudh Rayabharam (Microsoft) <anirudh@anirudhrb.com>
next prev parent reply other threads:[~2025-12-01 11:12 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-26 2:08 [PATCH v7 0/7] Introduce movable pages for Hyper-V guests Stanislav Kinsburskii
2025-11-26 2:08 ` [PATCH v7 1/7] Drivers: hv: Refactor and rename memory region handling functions Stanislav Kinsburskii
2025-12-01 11:20 ` Anirudh Rayabharam
2025-11-26 2:08 ` [PATCH v7 2/7] Drivers: hv: Centralize guest memory region destruction Stanislav Kinsburskii
2025-12-01 11:12 ` Anirudh Rayabharam [this message]
2025-11-26 2:09 ` [PATCH v7 3/7] Drivers: hv: Move region management to mshv_regions.c Stanislav Kinsburskii
2025-12-01 11:06 ` Anirudh Rayabharam
2025-12-01 16:46 ` Stanislav Kinsburskii
2025-12-03 18:13 ` Nuno Das Neves
2025-12-03 18:20 ` Stanislav Kinsburskii
2025-11-26 2:09 ` [PATCH v7 4/7] Drivers: hv: Fix huge page handling in memory region traversal Stanislav Kinsburskii
2025-11-27 10:59 ` kernel test robot
2025-12-01 15:09 ` Anirudh Rayabharam
2025-12-01 18:26 ` Stanislav Kinsburskii
2025-12-03 18:50 ` Nuno Das Neves
2025-12-04 16:03 ` Michael Kelley
2025-12-04 21:08 ` Stanislav Kinsburskii
2025-12-11 17:37 ` Michael Kelley
2025-12-15 20:12 ` Stanislav Kinsburskii
2025-12-17 0:54 ` Stanislav Kinsburskii
2025-11-26 2:09 ` [PATCH v7 5/7] Drivers: hv: Improve region overlap detection in partition create Stanislav Kinsburskii
2025-12-01 15:06 ` Anirudh Rayabharam
2025-12-02 18:39 ` Michael Kelley
2025-12-03 17:46 ` Stanislav Kinsburskii
2025-12-03 18:58 ` Nuno Das Neves
2025-12-03 19:36 ` Nuno Das Neves
2025-11-26 2:09 ` [PATCH v7 6/7] Drivers: hv: Add refcount and locking to mem regions Stanislav Kinsburskii
2025-12-04 16:48 ` Michael Kelley
2025-12-04 21:23 ` Stanislav Kinsburskii
2025-11-26 2:09 ` [PATCH v7 7/7] Drivers: hv: Add support for movable memory regions Stanislav Kinsburskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aS14E-QpVccRc6Gz@anirudh-surface.localdomain \
--to=anirudh@anirudhrb.com \
--cc=decui@microsoft.com \
--cc=haiyangz@microsoft.com \
--cc=kys@microsoft.com \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=skinsburskii@linux.microsoft.com \
--cc=wei.liu@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox