mshv: Fix error handling in mshv_region_pin

The current error handling has two issues:

First, pin_user_pages_fast() can return a short pin count (less than
requested but greater than zero) when it cannot pin all requested pages.
This is treated as success, leading to partially pinned regions being
used, which causes memory corruption.

Second, when an error occurs mid-loop, already pinned pages from the
current batch are not properly accounted for before calling
mshv_region_invalidate_pages(), causing a page reference leak.

Treat short pins as errors and fix partial batch accounting before
cleanup.

Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Reviewed-by: Michael Kelley <mhklinux@outlook.com>
Signed-off-by: Wei Liu <wei.liu@kernel.org>
This commit is contained in:
Stanislav Kinsburskii
2026-03-17 15:04:55 +00:00
committed by Wei Liu
parent b2ae73d954
commit c0e296f257

View File

@@ -314,15 +314,17 @@ int mshv_region_pin(struct mshv_mem_region *region)
ret = pin_user_pages_fast(userspace_addr, nr_pages, ret = pin_user_pages_fast(userspace_addr, nr_pages,
FOLL_WRITE | FOLL_LONGTERM, FOLL_WRITE | FOLL_LONGTERM,
pages); pages);
if (ret < 0) if (ret != nr_pages)
goto release_pages; goto release_pages;
} }
return 0; return 0;
release_pages: release_pages:
if (ret > 0)
done_count += ret;
mshv_region_invalidate_pages(region, 0, done_count); mshv_region_invalidate_pages(region, 0, done_count);
return ret; return ret < 0 ? ret : -ENOMEM;
} }
static int mshv_region_chunk_unmap(struct mshv_mem_region *region, static int mshv_region_chunk_unmap(struct mshv_mem_region *region,