Skip to content

Commit c0e296f

Browse files
Stanislav Kinsburskiiliuw
authored andcommitted
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 <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Signed-off-by: Wei Liu <[email protected]>
1 parent b2ae73d commit c0e296f

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

drivers/hv/mshv_regions.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,17 @@ int mshv_region_pin(struct mshv_mem_region *region)
314314
ret = pin_user_pages_fast(userspace_addr, nr_pages,
315315
FOLL_WRITE | FOLL_LONGTERM,
316316
pages);
317-
if (ret < 0)
317+
if (ret != nr_pages)
318318
goto release_pages;
319319
}
320320

321321
return 0;
322322

323323
release_pages:
324+
if (ret > 0)
325+
done_count += ret;
324326
mshv_region_invalidate_pages(region, 0, done_count);
325-
return ret;
327+
return ret < 0 ? ret : -ENOMEM;
326328
}
327329

328330
static int mshv_region_chunk_unmap(struct mshv_mem_region *region,

0 commit comments

Comments
 (0)