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: 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>

authored by

Stanislav Kinsburskii and committed by
Wei Liu
df4ff5f6 9d70ef7a

+36 -44
+36 -44
drivers/hv/mshv_root_main.c
··· 1114 1114 } 1115 1115 1116 1116 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) 1119 1119 { 1120 1120 if (region->flags.range_pinned) 1121 1121 unpin_user_pages(region->pages + page_offset, page_count); ··· 1125 1125 } 1126 1126 1127 1127 static void 1128 - mshv_region_evict(struct mshv_mem_region *region) 1128 + mshv_region_invalidate(struct mshv_mem_region *region) 1129 1129 { 1130 - mshv_region_evict_pages(region, 0, region->nr_pages); 1130 + mshv_region_invalidate_pages(region, 0, region->nr_pages); 1131 1131 } 1132 1132 1133 1133 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) 1136 1135 { 1137 1136 u64 done_count, nr_pages; 1138 1137 struct page **pages; 1139 1138 __u64 userspace_addr; 1140 1139 int ret; 1141 1140 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; 1147 1143 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, 1151 1146 MSHV_PIN_PAGES_BATCH_SIZE); 1152 1147 1153 1148 /* ··· 1153 1158 * with the FOLL_LONGTERM flag does a large temporary 1154 1159 * allocation of contiguous memory. 1155 1160 */ 1156 - if (region->flags.range_pinned) 1157 - ret = pin_user_pages_fast(userspace_addr, 1158 - nr_pages, 1159 - FOLL_WRITE | FOLL_LONGTERM, 1160 - pages); 1161 - else 1162 - ret = -EOPNOTSUPP; 1163 - 1161 + ret = pin_user_pages_fast(userspace_addr, nr_pages, 1162 + FOLL_WRITE | FOLL_LONGTERM, 1163 + pages); 1164 1164 if (ret < 0) 1165 1165 goto release_pages; 1166 1166 } 1167 1167 1168 - if (PageHuge(region->pages[page_offset])) 1168 + if (PageHuge(region->pages[0])) 1169 1169 region->flags.large_pages = true; 1170 1170 1171 1171 return 0; 1172 1172 1173 1173 release_pages: 1174 - mshv_region_evict_pages(region, page_offset, done_count); 1174 + mshv_region_invalidate_pages(region, 0, done_count); 1175 1175 return ret; 1176 - } 1177 - 1178 - static int 1179 - mshv_region_populate(struct mshv_mem_region *region) 1180 - { 1181 - return mshv_region_populate_pages(region, 0, region->nr_pages); 1182 1176 } 1183 1177 1184 1178 static struct mshv_mem_region * ··· 1229 1245 return 0; 1230 1246 } 1231 1247 1232 - /* 1233 - * Map guest ram. if snp, make sure to release that from the host first 1234 - * Side Effects: In case of failure, pages are unpinned when feasible. 1248 + /** 1249 + * mshv_prepare_pinned_region - Pin and map memory regions 1250 + * @region: Pointer to the memory region structure 1251 + * 1252 + * This function processes memory regions that are explicitly marked as pinned. 1253 + * Pinned regions are preallocated, mapped upfront, and do not rely on fault-based 1254 + * population. The function ensures the region is properly populated, handles 1255 + * encryption requirements for SNP partitions if applicable, maps the region, 1256 + * and performs necessary sharing or eviction operations based on the mapping 1257 + * result. 1258 + * 1259 + * Return: 0 on success, negative error code on failure. 1235 1260 */ 1236 - static int 1237 - mshv_partition_mem_region_map(struct mshv_mem_region *region) 1261 + static int mshv_prepare_pinned_region(struct mshv_mem_region *region) 1238 1262 { 1239 1263 struct mshv_partition *partition = region->partition; 1240 1264 int ret; 1241 1265 1242 - ret = mshv_region_populate(region); 1266 + ret = mshv_region_pin(region); 1243 1267 if (ret) { 1244 - pt_err(partition, "Failed to populate memory region: %d\n", 1268 + pt_err(partition, "Failed to pin memory region: %d\n", 1245 1269 ret); 1246 1270 goto err_out; 1247 1271 } ··· 1267 1275 pt_err(partition, 1268 1276 "Failed to unshare memory region (guest_pfn: %llu): %d\n", 1269 1277 region->start_gfn, ret); 1270 - goto evict_region; 1278 + goto invalidate_region; 1271 1279 } 1272 1280 } 1273 1281 ··· 1277 1285 1278 1286 shrc = mshv_partition_region_share(region); 1279 1287 if (!shrc) 1280 - goto evict_region; 1288 + goto invalidate_region; 1281 1289 1282 1290 pt_err(partition, 1283 1291 "Failed to share memory region (guest_pfn: %llu): %d\n", ··· 1291 1299 1292 1300 return 0; 1293 1301 1294 - evict_region: 1295 - mshv_region_evict(region); 1302 + invalidate_region: 1303 + mshv_region_invalidate(region); 1296 1304 err_out: 1297 1305 return ret; 1298 1306 } ··· 1341 1349 ret = hv_call_map_mmio_pages(partition->pt_id, mem.guest_pfn, 1342 1350 mmio_pfn, HVPFN_DOWN(mem.size)); 1343 1351 else 1344 - ret = mshv_partition_mem_region_map(region); 1352 + ret = mshv_prepare_pinned_region(region); 1345 1353 1346 1354 if (ret) 1347 1355 goto errout; ··· 1386 1394 hv_call_unmap_gpa_pages(partition->pt_id, region->start_gfn, 1387 1395 region->nr_pages, unmap_flags); 1388 1396 1389 - mshv_region_evict(region); 1397 + mshv_region_invalidate(region); 1390 1398 1391 1399 vfree(region); 1392 1400 return 0; ··· 1804 1812 } 1805 1813 } 1806 1814 1807 - mshv_region_evict(region); 1815 + mshv_region_invalidate(region); 1808 1816 1809 1817 vfree(region); 1810 1818 }