Skip to content

Commit df4ff5f

Browse files
Stanislav Kinsburskiiliuw
authored andcommitted
mshv: Refactor and rename memory region handling functions
Simplify and unify memory region management to improve code clarity and reliability. Consolidate pinning and invalidation logic, adopt consistent naming, and remove redundant checks to reduce complexity. Enhance documentation and update call sites for maintainability. 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 9d70ef7 commit df4ff5f

1 file changed

Lines changed: 36 additions & 44 deletions

File tree

drivers/hv/mshv_root_main.c

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,8 @@ mshv_region_map(struct mshv_mem_region *region)
11141114
}
11151115

11161116
static void
1117-
mshv_region_evict_pages(struct mshv_mem_region *region,
1118-
u64 page_offset, u64 page_count)
1117+
mshv_region_invalidate_pages(struct mshv_mem_region *region,
1118+
u64 page_offset, u64 page_count)
11191119
{
11201120
if (region->flags.range_pinned)
11211121
unpin_user_pages(region->pages + page_offset, page_count);
@@ -1125,29 +1125,24 @@ mshv_region_evict_pages(struct mshv_mem_region *region,
11251125
}
11261126

11271127
static void
1128-
mshv_region_evict(struct mshv_mem_region *region)
1128+
mshv_region_invalidate(struct mshv_mem_region *region)
11291129
{
1130-
mshv_region_evict_pages(region, 0, region->nr_pages);
1130+
mshv_region_invalidate_pages(region, 0, region->nr_pages);
11311131
}
11321132

11331133
static int
1134-
mshv_region_populate_pages(struct mshv_mem_region *region,
1135-
u64 page_offset, u64 page_count)
1134+
mshv_region_pin(struct mshv_mem_region *region)
11361135
{
11371136
u64 done_count, nr_pages;
11381137
struct page **pages;
11391138
__u64 userspace_addr;
11401139
int ret;
11411140

1142-
if (page_offset + page_count > region->nr_pages)
1143-
return -EINVAL;
1144-
1145-
for (done_count = 0; done_count < page_count; done_count += ret) {
1146-
pages = region->pages + page_offset + done_count;
1141+
for (done_count = 0; done_count < region->nr_pages; done_count += ret) {
1142+
pages = region->pages + done_count;
11471143
userspace_addr = region->start_uaddr +
1148-
(page_offset + done_count) *
1149-
HV_HYP_PAGE_SIZE;
1150-
nr_pages = min(page_count - done_count,
1144+
done_count * HV_HYP_PAGE_SIZE;
1145+
nr_pages = min(region->nr_pages - done_count,
11511146
MSHV_PIN_PAGES_BATCH_SIZE);
11521147

11531148
/*
@@ -1158,34 +1153,23 @@ mshv_region_populate_pages(struct mshv_mem_region *region,
11581153
* with the FOLL_LONGTERM flag does a large temporary
11591154
* allocation of contiguous memory.
11601155
*/
1161-
if (region->flags.range_pinned)
1162-
ret = pin_user_pages_fast(userspace_addr,
1163-
nr_pages,
1164-
FOLL_WRITE | FOLL_LONGTERM,
1165-
pages);
1166-
else
1167-
ret = -EOPNOTSUPP;
1168-
1156+
ret = pin_user_pages_fast(userspace_addr, nr_pages,
1157+
FOLL_WRITE | FOLL_LONGTERM,
1158+
pages);
11691159
if (ret < 0)
11701160
goto release_pages;
11711161
}
11721162

1173-
if (PageHuge(region->pages[page_offset]))
1163+
if (PageHuge(region->pages[0]))
11741164
region->flags.large_pages = true;
11751165

11761166
return 0;
11771167

11781168
release_pages:
1179-
mshv_region_evict_pages(region, page_offset, done_count);
1169+
mshv_region_invalidate_pages(region, 0, done_count);
11801170
return ret;
11811171
}
11821172

1183-
static int
1184-
mshv_region_populate(struct mshv_mem_region *region)
1185-
{
1186-
return mshv_region_populate_pages(region, 0, region->nr_pages);
1187-
}
1188-
11891173
static struct mshv_mem_region *
11901174
mshv_partition_region_by_gfn(struct mshv_partition *partition, u64 gfn)
11911175
{
@@ -1245,19 +1229,27 @@ static int mshv_partition_create_region(struct mshv_partition *partition,
12451229
return 0;
12461230
}
12471231

1248-
/*
1249-
* Map guest ram. if snp, make sure to release that from the host first
1250-
* Side Effects: In case of failure, pages are unpinned when feasible.
1232+
/**
1233+
* mshv_prepare_pinned_region - Pin and map memory regions
1234+
* @region: Pointer to the memory region structure
1235+
*
1236+
* This function processes memory regions that are explicitly marked as pinned.
1237+
* Pinned regions are preallocated, mapped upfront, and do not rely on fault-based
1238+
* population. The function ensures the region is properly populated, handles
1239+
* encryption requirements for SNP partitions if applicable, maps the region,
1240+
* and performs necessary sharing or eviction operations based on the mapping
1241+
* result.
1242+
*
1243+
* Return: 0 on success, negative error code on failure.
12511244
*/
1252-
static int
1253-
mshv_partition_mem_region_map(struct mshv_mem_region *region)
1245+
static int mshv_prepare_pinned_region(struct mshv_mem_region *region)
12541246
{
12551247
struct mshv_partition *partition = region->partition;
12561248
int ret;
12571249

1258-
ret = mshv_region_populate(region);
1250+
ret = mshv_region_pin(region);
12591251
if (ret) {
1260-
pt_err(partition, "Failed to populate memory region: %d\n",
1252+
pt_err(partition, "Failed to pin memory region: %d\n",
12611253
ret);
12621254
goto err_out;
12631255
}
@@ -1275,7 +1267,7 @@ mshv_partition_mem_region_map(struct mshv_mem_region *region)
12751267
pt_err(partition,
12761268
"Failed to unshare memory region (guest_pfn: %llu): %d\n",
12771269
region->start_gfn, ret);
1278-
goto evict_region;
1270+
goto invalidate_region;
12791271
}
12801272
}
12811273

@@ -1285,7 +1277,7 @@ mshv_partition_mem_region_map(struct mshv_mem_region *region)
12851277

12861278
shrc = mshv_partition_region_share(region);
12871279
if (!shrc)
1288-
goto evict_region;
1280+
goto invalidate_region;
12891281

12901282
pt_err(partition,
12911283
"Failed to share memory region (guest_pfn: %llu): %d\n",
@@ -1299,8 +1291,8 @@ mshv_partition_mem_region_map(struct mshv_mem_region *region)
12991291

13001292
return 0;
13011293

1302-
evict_region:
1303-
mshv_region_evict(region);
1294+
invalidate_region:
1295+
mshv_region_invalidate(region);
13041296
err_out:
13051297
return ret;
13061298
}
@@ -1349,7 +1341,7 @@ mshv_map_user_memory(struct mshv_partition *partition,
13491341
ret = hv_call_map_mmio_pages(partition->pt_id, mem.guest_pfn,
13501342
mmio_pfn, HVPFN_DOWN(mem.size));
13511343
else
1352-
ret = mshv_partition_mem_region_map(region);
1344+
ret = mshv_prepare_pinned_region(region);
13531345

13541346
if (ret)
13551347
goto errout;
@@ -1394,7 +1386,7 @@ mshv_unmap_user_memory(struct mshv_partition *partition,
13941386
hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn,
13951387
region->nr_pages, unmap_flags);
13961388

1397-
mshv_region_evict(region);
1389+
mshv_region_invalidate(region);
13981390

13991391
vfree(region);
14001392
return 0;
@@ -1812,7 +1804,7 @@ static void destroy_partition(struct mshv_partition *partition)
18121804
}
18131805
}
18141806

1815-
mshv_region_evict(region);
1807+
mshv_region_invalidate(region);
18161808

18171809
vfree(region);
18181810
}

0 commit comments

Comments
 (0)