mshv: Do not map mmio in map user memory ioctl
Subsystems (2)
PATCH 1/1 mshv: Do not map mmio in map user memory ioctl
Currently, mshv_map_user_memory() snoops into vma to get the mmio pfn via vm_pgoff. The mmio pfn is no longer stored there and will cause the system to crash. Moreover, it is not legit to obtain mmio pfn via that method. So, remove upfront mmio mappings, just create region and do basic checks. The mmio entries will be populated on demand during intercepts in future.
--- drivers/hv/mshv_root.h | 1 - drivers/hv/mshv_root_hv_call.c | 15 --------------- drivers/hv/mshv_root_main.c | 28 +++++++++++----------------- 3 files changed, 11 insertions(+), 33 deletions(-)
drivers/hv/mshv_root.h +0 -1
@@ -281,7 +281,6 @@ int hv_call_create_partition(u64 flags,int hv_call_initialize_partition (u64 partition_id);int hv_call_finalize_partition (u64 partition_id);int hv_call_delete_partition (u64 partition_id); -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs);int hv_call_map_gpa_pages (u64 partition_id,u64 gpa_target,u64 page_count,u32 flags,struct page **pages); int hv_call_unmap_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count,
drivers/hv/mshv_root_hv_call.c +0 -15
@@ -327,21 +327,6 @@ int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, flags, pages, 0); } -/* Ask the hypervisor to map guest mmio space */ -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs) -{ -int i; -u32 flags =HV_MAP_GPA_READABLE |HV_MAP_GPA_WRITABLE | -HV_MAP_GPA_NOT_CACHED ; - -for (i =0 ; i < numpgs; i++) -if (page_is_ram (mmio_spa + i)) -return -EINVAL ; - -return hv_do_map_gpa_hcall (partition_id, gfn, numpgs, flags,NULL , - mmio_spa); -} -int hv_call_unmap_gpa_pages (u64 partition_id,u64 gfn,u64 page_count_4k,u32 flags) {
drivers/hv/mshv_root_main.c +11 -17
@@ -1345,16 +1345,8 @@ static int mshv_prepare_pinned_region(struct mshv_mem_region *region) }/* -* This maps two things: guest RAM and for pci passthru mmio space. -* -* mmio: -* - vfio overloads vm_pgoff to store the mmio start pfn/spa. -* - Two things need to happen for mapping mmio range: -* 1. mapped in the uaddr so VMM can access it. -* 2. mapped in the hwpt (gfn <-> mmio phys addr) so guest can access it. -* -* This function takes care of the second. The first one is managed by vfio, -* and hence is taken care of via vfio_pci_mmap_fault(). +* This is called for both user ram and mmio space. The mmio space is not +* mapped here, that should happen later during intercept on demand. */ static long mshv_map_user_memory(struct mshv_partition *partition,
@@ -1363,7 +1355,7 @@ mshv_map_user_memory(struct mshv_partition *partition,struct mshv_mem_region *region;struct vm_area_struct *vma;bool is_mmio; -ulong mmio_pfn; +size_t vma_sz;long ret; if (mem->flags & BIT(MSHV_SET_MEM_BIT_UNMAP) ||
@@ -1373,12 +1365,18 @@ mshv_map_user_memory(struct mshv_partition *partition,mmap_read_lock (current->mm); vma =vma_lookup (current->mm, mem->userspace_addr); is_mmio = vma ? !!(vma->vm_flags & (VM_IO |VM_PFNMAP )) :0 ; - mmio_pfn = is_mmio ? vma->vm_pgoff :0 ; +if (vma && is_mmio) + vma_sz = vma->vm_end - mem->userspace_addr;mmap_read_unlock (current->mm);if (!vma)return -EINVAL ; +if (is_mmio) +/* Upon mmio intercept, entire region is mapped */ +if (mem->size > vma_sz) +return -EPERM ; + ret =mshv_partition_create_region (partition, mem, ®ion, is_mmio);if (ret)
@@ -1399,11 +1397,7 @@ mshv_map_user_memory(struct mshv_partition *partition, region->nr_pages, HV_MAP_GPA_NO_ACCESS, NULL);break ; -case MSHV_REGION_TYPE_MMIO : - ret =hv_call_map_mmio_pages (partition->pt_id, - region->start_gfn, - mmio_pfn, - region->nr_pages); +default :break ; }
Signature · 1 line
2.51.2.vfs.0.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mshv: missing Fixes trailer on commit fixing crash in mshv_map_user_memory() It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mshv: missing Fixes trailer on commit fixing crash in mshv_map_user_memory()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Medium] mshv: ignored mshv_region_map() error in mshv_prepare_pinned_region() [1]
--
commit f8208ba7fc5113c6d67967e4650125ea72fda573
Author: Mukesh R <[email protected]>
Quoted 10 lines
mshv: Do not map mmio in map user memory ioctl
Currently,
mshv_map_user_memory()snoops into vma to get the mmio pfn viavm_pgoff. The mmio pfn is no longer stored there and will cause the system to crash. Moreover, it is not legit to obtain mmio pfn via that method. So, remove upfront mmio mappings, just create region and do basic checks. The mmio entries will be populated on demand during intercepts in future.Signed-off-by Mukesh R <[email protected]>
[Severity: Low]
This fixes a system crash when mapping MMIO memory regions. Should this include:
Quoted 54 lines
drivers/hv/mshv_root_main.c +10
-12@@ -1345,16 +1345,8 @@ static int mshv_prepare_pinned_region(struct mshv_mem_region *region) }/* -* This maps two things: guest RAM and for pci passthru mmio space. -* -* mmio: -* - vfio overloads vm_pgoff to store the mmio start pfn/spa. -* - Two things need to happen for mapping mmio range: -* 1. mapped in the uaddr so VMM can access it. -* 2. mapped in the hwpt (gfn <-> mmio phys addr) so guest can access it. -* -* This function takes care of the second. The first one is managed by vfio, -* and hence is taken care of via vfio_pci_mmap_fault(). +* This is called for both user ram and mmio space. The mmio space is not +* mapped here, that should happen later during intercept on demand. */ static long mshv_map_user_memory(struct mshv_partition *partition,@@ -1363,7 +1355,7 @@ mshv_map_user_memory(struct mshv_partition *partition,struct mshv_mem_region *region;struct vm_area_struct *vma;bool is_mmio; -ulong mmio_pfn; +size_t vma_sz;long ret; if (mem->flags & BIT(MSHV_SET_MEM_BIT_UNMAP) ||@@ -1373,12 +1365,18 @@ mshv_map_user_memory(struct mshv_partition *partition,mmap_read_lock (current->mm); vma =vma_lookup (current->mm, mem->userspace_addr); is_mmio = vma ? !!(vma->vm_flags & (VM_IO |VM_PFNMAP )) :0 ; - mmio_pfn = is_mmio ? vma->vm_pgoff :0 ; +if (vma && is_mmio) + vma_sz = vma->vm_end - mem->userspace_addr;mmap_read_unlock (current->mm);if (!vma)return -EINVAL ; +if (is_mmio) +/* Upon mmio intercept, entire region is mapped */ +if (mem->size > vma_sz) +return -EPERM ; + ret =mshv_partition_create_region (partition, mem, ®ion, is_mmio);if (ret)@@ -1399,11 +1397,7 @@ mshv_map_user_memory(struct mshv_partition *partition, region->nr_pages,
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1402
Quoted 10 lines
drivers/hv/mshv_root_main.c +1
-5HV_MAP_GPA_NO_ACCESS, NULL);break ; -case MSHV_REGION_TYPE_MMIO : - ret =hv_call_map_mmio_pages (partition->pt_id, - region->start_gfn, - mmio_pfn, - region->nr_pages); +default :break ; }
[Severity: Medium]
This problem wasn't introduced by this patch, but does mshv_prepare_pinned_region() ignore mshv_region_map() errors on non-encrypted partitions [1]?
drivers/hv/mshv_root_main.c:mshv_prepare_pinned_region() {
...
ret = mshv_region_map(region);
if (ret && mshv_partition_encrypted(partition)) {
int shrc;
shrc = mshv_region_share(region);
if (!shrc)
goto invalidate_region;
...
goto err_out;
}
return 0;invalidate_region:
mshv_region_invalidate(region); err_out: return ret; }
When mshv_region_movable_init() fails on a non-encrypted partition, mshv_partition_create_region() falls back to MSHV_REGION_TYPE_MEM_PINNED. If mshv_region_map() then fails, mshv_prepare_pinned_region() skips mshv_region_invalidate() and returns 0, so mshv_map_user_memory() links the unmapped region into partition->pt_mem_regions with its pages left pinned and returns success to userspace.
[1] https://sashiko.dev/#/bug/linux-a3d211d1-2dbc-4947-880d-f5809ec51869
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
From: Mukesh R <[email protected]> Sent: Thursday, November 12, 2026 7:14 PM
Quoted 3 lines
Currently,
mshv_map_user_memory()snoops into vma to get the mmio pfn viavm_pgoff. The mmio pfn is no longer stored there and will
Did you identify a specific commit that changed how the mmio pfn is stored? Given that the issue fixed by this patch causes a system crash, it seems like this patch should have a Fixes: tag so it can be backported.
Is the mmio pfn now stored someplace else that is accessible? I'm wondering if this patch is a temporary band-aid to prevent the crash, with upfront mapping of MMIO space coming back again later via a more complex/more complete fix.
Quoted 37 lines
cause the system to crash. Moreover, it is not legit to obtain mmio pfn via that method. So, remove upfront mmio mappings, just create region and do basic checks. The mmio entries will be populated on demand during intercepts in future.
Signed-off-by Mukesh R <[email protected]>--- drivers/hv/mshv_root.h | 1 - drivers/hv/mshv_root_hv_call.c | 15 --------------- drivers/hv/mshv_root_main.c | 28 +++++++++++----------------- 3 files changed, 11 insertions(+), 33 deletions(-)drivers/hv/mshv_root.h +0
-1@@ -281,7 +281,6 @@ int hv_call_create_partition(u64 flags,int hv_call_initialize_partition (u64 partition_id);int hv_call_finalize_partition (u64 partition_id);int hv_call_delete_partition (u64 partition_id); -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs);int hv_call_map_gpa_pages (u64 partition_id,u64 gpa_target,u64 page_count,u32 flags,struct page **pages); int hv_call_unmap_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count,drivers/hv/mshv_root_hv_call.c +0
-5@@ -327,21 +327,6 @@ int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, flags, pages, 0); } -/* Ask the hypervisor to map guest mmio space */ -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs) -{ -int i; - u32 flags = HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE |
drivers/hv/mshv_root_hv_call.c int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, · line 344
Quoted 10 lines
drivers/hv/mshv_root_hv_call.c +0
-10-HV_MAP_GPA_NOT_CACHED ; - -for (i =0 ; i < numpgs; i++) -if (page_is_ram (mmio_spa + i)) -return -EINVAL ; - -return hv_do_map_gpa_hcall (partition_id, gfn, numpgs, flags,NULL , - mmio_spa); -} -
With this code removed, hv_call_map_gpa_pages() is now the only caller of hv_do_map_gpa_hcall(), and it is only a wrapper. Is there a reason to not collapse these into one function? And the last parameter of hv_do_map_gpa_hcall() is "mmio_spa", which becomes never used and could be removed.
Quoted 36 lines
drivers/hv/mshv_root_hv_call.c +0
-0int hv_call_unmap_gpa_pages (u64 partition_id,u64 gfn,u64 page_count_4k,u32 flags) {drivers/hv/mshv_root_main.c +3
-11@@ -1345,16 +1345,8 @@ static int mshv_prepare_pinned_region(struct mshv_mem_region *region) }/* -* This maps two things: guest RAM and for pci passthru mmio space. -* -* mmio: -* - vfio overloads vm_pgoff to store the mmio start pfn/spa. -* - Two things need to happen for mapping mmio range: -* 1. mapped in the uaddr so VMM can access it. -* 2. mapped in the hwpt (gfn <-> mmio phys addr) so guest can access it. -* -* This function takes care of the second. The first one is managed by vfio, -* and hence is taken care of via vfio_pci_mmap_fault(). +* This is called for both user ram and mmio space. The mmio space is not +* mapped here, that should happen later during intercept on demand. */ static long mshv_map_user_memory(struct mshv_partition *partition,@@ -1363,7 +1355,7 @@ mshv_map_user_memory(struct mshv_partition *partition,struct mshv_mem_region *region;struct vm_area_struct *vma;bool is_mmio; -ulong mmio_pfn; +size_t vma_sz;long ret; if (mem->flags & BIT(MSHV_SET_MEM_BIT_UNMAP) ||
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1368
Quoted 5 lines
drivers/hv/mshv_root_main.c +1
-1@@ -1373,12 +1365,18 @@ mshv_map_user_memory(struct mshv_partition *partition,mmap_read_lock (current->mm); vma =vma_lookup (current->mm, mem->userspace_addr); is_mmio = vma ? !!(vma->vm_flags & (VM_IO |VM_PFNMAP )) :0 ; - mmio_pfn = is_mmio ? vma->vm_pgoff :0 ; +if (vma && is_mmio)
Just testing is_mmio is sufficient here. is_mmio can be true only when vma is non-NULL. Indeed, the existing code uses exactly that dependency when accessing vma->vm_pgoff.
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1377
Quoted 9 lines
drivers/hv/mshv_root_main.c +4
-0+ vma_sz = vma->vm_end - mem->userspace_addr;mmap_read_unlock (current->mm);if (!vma)return -EINVAL ; +if (is_mmio) +/* Upon mmio intercept, entire region is mapped */ +if (mem->size > vma_sz)
Combine these two tests instead of nesting? I.e.,
if (is_mmio && mem->size > vma_sz)
Quoted 6 lines
drivers/hv/mshv_root_main.c +2
-0+return -EPERM ; + ret =mshv_partition_create_region (partition, mem, ®ion, is_mmio);if (ret)
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1401
Quoted 10 lines
drivers/hv/mshv_root_main.c +1
-5@@ -1399,11 +1397,7 @@ mshv_map_user_memory(struct mshv_partition *partition, region->nr_pages, HV_MAP_GPA_NO_ACCESS, NULL);break ; -case MSHV_REGION_TYPE_MMIO : - ret =hv_call_map_mmio_pages (partition->pt_id, - region->start_gfn, - mmio_pfn, - region->nr_pages); +default :break ;
A default case with just "break" seems useless. Omitting it entirely produces the same result. The existing code doesn't have a default. If you want to be explicit that no mapping is done for MMIO, perhaps leave the case MSHV_REGION_TYPE_MMIO
with just "break".
Michael
Quoted 5 lines
drivers/hv/mshv_root_main.c +0
-1} --2.51.2.vfs.0.1
Quoted 9 lines · Michael Kelley
From: Mukesh R <[email protected]> Sent: Thursday, November 12, 2026 7:14 PM
Quoted 3 lines
Currently,
mshv_map_user_memory()snoops into vma to get the mmio pfn viavm_pgoff. The mmio pfn is no longer stored there and willDid you identify a specific commit that changed how the mmio pfn is stored? Given that the issue fixed by this patch causes a system crash, it seems like this patch should have a Fixes: tag so it can be backported.
No, because that didn't break it, it was never supposed to be that way. vfio folks strongly rejected anyone outside the vfio driver snooping into it, but the code to do that here un-intentionally got in via initial big driver checkin. The change in vfio happened somewhere after 6.6, but like i said, our lookup was illegit to begin with.
Quoted 4 lines
Is the mmio pfn now stored someplace else that is accessible? I'm wondering if this patch is a temporary band-aid to prevent the crash, with upfront mapping of MMIO space coming back again later via a more complex/more complete fix.
It is stored differently, but the patch to make the lookup vfio function public was rejected. Instead, i suppose we will do what kvm does which is look up via user address. That will all be part of upcoming "guest pci-passthru using iommufd" patches....
Quoted 45 lines
Quoted 45 lines
cause the system to crash. Moreover, it is not legit to obtain mmio pfn via that method. So, remove upfront mmio mappings, just create region and do basic checks. The mmio entries will be populated on demand during intercepts in future.
Signed-off-by Mukesh R <[email protected]>--- drivers/hv/mshv_root.h | 1 - drivers/hv/mshv_root_hv_call.c | 15 --------------- drivers/hv/mshv_root_main.c | 28 +++++++++++----------------- 3 files changed, 11 insertions(+), 33 deletions(-)drivers/hv/mshv_root.h +0
-1@@ -281,7 +281,6 @@ int hv_call_create_partition(u64 flags,int hv_call_initialize_partition (u64 partition_id);int hv_call_finalize_partition (u64 partition_id);int hv_call_delete_partition (u64 partition_id); -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs);int hv_call_map_gpa_pages (u64 partition_id,u64 gpa_target,u64 page_count,u32 flags,struct page **pages); int hv_call_unmap_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count,drivers/hv/mshv_root_hv_call.c +0
-13@@ -327,21 +327,6 @@ int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, flags, pages, 0); } -/* Ask the hypervisor to map guest mmio space */ -int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs) -{ -int i; -u32 flags =HV_MAP_GPA_READABLE |HV_MAP_GPA_WRITABLE | -HV_MAP_GPA_NOT_CACHED ; - -for (i =0 ; i < numpgs; i++) -if (page_is_ram (mmio_spa + i)) -return -EINVAL ; - -return hv_do_map_gpa_hcall (partition_id, gfn, numpgs, flags,NULL , - mmio_spa);
Quoted 8 lines
drivers/hv/mshv_root_hv_call.c int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, · line 344
Quoted 2 lines
drivers/hv/mshv_root_hv_call.c +0
-2-} -With this code removed,
hv_call_map_gpa_pages()is now the only caller ofhv_do_map_gpa_hcall(), and it is only a wrapper. Is there a reason to not collapse these into one function? And the last parameter ofhv_do_map_gpa_hcall()is "mmio_spa", which becomes never used and could be removed.
Actually, the upcoming guest passthru support via iommufd calls hv_do_map_gpa_hcall() for mapping mmio pages. so it will be removing then adding back in... so i thought i will leave it there.
Quoted 34 lines
Quoted 34 lines
drivers/hv/mshv_root_hv_call.c +0
-0int hv_call_unmap_gpa_pages (u64 partition_id,u64 gfn,u64 page_count_4k,u32 flags) {drivers/hv/mshv_root_main.c +3
-11@@ -1345,16 +1345,8 @@ static int mshv_prepare_pinned_region(struct mshv_mem_region *region) }/* -* This maps two things: guest RAM and for pci passthru mmio space. -* -* mmio: -* - vfio overloads vm_pgoff to store the mmio start pfn/spa. -* - Two things need to happen for mapping mmio range: -* 1. mapped in the uaddr so VMM can access it. -* 2. mapped in the hwpt (gfn <-> mmio phys addr) so guest can access it. -* -* This function takes care of the second. The first one is managed by vfio, -* and hence is taken care of via vfio_pci_mmap_fault(). +* This is called for both user ram and mmio space. The mmio space is not +* mapped here, that should happen later during intercept on demand. */ static long mshv_map_user_memory(struct mshv_partition *partition,@@ -1363,7 +1355,7 @@ mshv_map_user_memory(struct mshv_partition *partition,struct mshv_mem_region *region;struct vm_area_struct *vma;bool is_mmio; -ulong mmio_pfn; +size_t vma_sz;long ret;
Quoted 11 lines
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1368
Quoted 7 lines
drivers/hv/mshv_root_main.c +1
-1if (mem->flags & BIT(MSHV_SET_MEM_BIT_UNMAP) ||@@ -1373,12 +1365,18 @@ mshv_map_user_memory(struct mshv_partition *partition,mmap_read_lock (current->mm); vma =vma_lookup (current->mm, mem->userspace_addr); is_mmio = vma ? !!(vma->vm_flags & (VM_IO |VM_PFNMAP )) :0 ; - mmio_pfn = is_mmio ? vma->vm_pgoff :0 ; +if (vma && is_mmio)Just testing
is_mmiois sufficient here.is_mmiocan be true only when vma is non-NULL. Indeed, the existing code uses exactly that dependency when accessingvma->vm_pgoff.
AI was generating comment that vma was not being checked. i could remove it and we can just ignore the AI comment..
Quoted 6 lines
Quoted 6 lines
drivers/hv/mshv_root_main.c +1
-0+ vma_sz = vma->vm_end - mem->userspace_addr;mmap_read_unlock (current->mm);if (!vma)return -EINVAL ;
Quoted 7 lines
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1377
Quoted 3 lines
drivers/hv/mshv_root_main.c +3
-0+if (is_mmio) +/* Upon mmio intercept, entire region is mapped */ +if (mem->size > vma_sz)Combine these two tests instead of nesting? I.e.,
if (is_mmio && mem->size > vma_sz)
ok.
Quoted 14 lines
Quoted 14 lines
drivers/hv/mshv_root_main.c +2
-5+return -EPERM ; + ret =mshv_partition_create_region (partition, mem, ®ion, is_mmio);if (ret)@@ -1399,11 +1397,7 @@ mshv_map_user_memory(struct mshv_partition *partition, region->nr_pages, HV_MAP_GPA_NO_ACCESS, NULL);break ; -case MSHV_REGION_TYPE_MMIO : - ret =hv_call_map_mmio_pages (partition->pt_id, - region->start_gfn, - mmio_pfn, - region->nr_pages);
Quoted 8 lines
drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1401
Quoted 2 lines
drivers/hv/mshv_root_main.c +1
-0+default :break ;A default case with just "break" seems useless. Omitting it entirely produces the same result. The existing code doesn't have a default. If you want to be explicit that no mapping is done for MMIO, perhaps leave the case
MSHV_REGION_TYPE_MMIO
with just "break".
Yeah, I had MSHV_REGION_TYPE_MMIO with just break, but another reviewer internally asked i remove it. i can remove default also, not sure if it is there because some tool chain complaining of no default..
Thanks,
-Mukesh
Quoted 7 lines
Michael
Quoted 5 lines
drivers/hv/mshv_root_main.c +0
-1} --2.51.2.vfs.0.1