vfio/container: Restrict dma_map_file() to shared RAM
PATCH 1/1 vfio/container: Restrict dma_map_file() to shared RAM
vfio_container_dma_map() uses dma_map_file() whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file() resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, using dma_map_file() on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.
This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.
Fix this by using dma_map_file() only for shared RAMBlocks.
--- hw/vfio/container.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/container.c +1 -1
@@ -82,7 +82,7 @@ int vfio_container_dma_map(VFIOContainer *bcontainer,RAMBlock *rb = mr->ram_block;int mfd = rb ?qemu_ram_get_fd (rb) :-1 ; -if (mfd >=0 && vioc->dma_map_file) { +if (mfd >=0 && vioc->dma_map_file &&qemu_ram_is_shared (rb)) {unsigned long start = vaddr -qemu_ram_get_host_addr (rb);unsigned long offset =qemu_ram_get_fd_offset (rb);
Signature · 1 line
2.43.5
Hi,
Quoted 8 lines
-----Original Message-----
From: Qiang, Chenyi <[email protected]>
Subject: [PATCH] vfio/container: Restrictdma_map_file()to shared RAM
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
Quoted 8 lines
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.
Good finding, did you see same issue with common file(not hugetlbfs file) backed private mapping?
Thanks
Zhenzhong
Quoted 26 lines
Fix this by using
dma_map_file()only for shared RAMBlocks.Fixes fb32965b6dd8 ("vfio/iommufd: use IOMMU_IOAS_MAP_FILE")Reported-by Farrah Chen <[email protected]>Signed-off-by Chenyi Qiang <[email protected]>---
hw/vfio/container.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/container.c +1
-2@@ -82,7 +82,7 @@ int vfio_container_dma_map(VFIOContainer *bcontainer,RAMBlock *rb = mr->ram_block;int mfd = rb ?qemu_ram_get_fd (rb) :-1 ; -if (mfd >=0 && vioc->dma_map_file) { +if (mfd >=0 && vioc->dma_map_file &&qemu_ram_is_shared (rb)) {unsigned long start = vaddr -qemu_ram_get_host_addr (rb);unsigned long offset =qemu_ram_get_fd_offset (rb); --2.43.5
Quoted 15 lines · Zhenzhong
Hi,
Quoted 13 lines
-----Original Message-----
From: Qiang, Chenyi <[email protected]>
Subject: [PATCH] vfio/container: Restrictdma_map_file()to shared RAM
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.
Quoted 5 lines
Quoted 3 lines
This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.Good finding, did you see same issue with common file(not hugetlbfs file) backed private mapping?
Yes, I also tried with "-object memory-backend-memfd,id=mem1,size=2G,share=off", and it can also be reproduced.
Duan, Zhenzhong
Reviewed-byQuoted 28 lines
-----Original Message-----
From: Qiang, Chenyi <[email protected]>
Subject: Re: [PATCH] vfio/container: Restrictdma_map_file()to shared RAMQuoted 19 lines · Zhenzhong
Hi,
Quoted 16 lines
-----Original Message-----
From: Qiang, Chenyi <[email protected]>
Subject: [PATCH] vfio/container: Restrictdma_map_file()to shared RAM
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.
Quoted 5 lines
Quoted 1 line
Good finding, did you see same issue with common file(not hugetlbfs file) backed
private mapping?
Yes, I also tried with "-object memory-backend- memfd,id=mem1,size=2G,share=off", and it can also be reproduced.
Hello Chenyi,
Quoted 28 lines · Chenyi Qiang
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.Fix this by using
dma_map_file()only for shared RAMBlocks.Fixes fb32965b6dd8 ("vfio/iommufd: use IOMMU_IOAS_MAP_FILE")Reported-by Farrah Chen <[email protected]>Signed-off-by Chenyi Qiang <[email protected]>--- hw/vfio/container.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/container.c +0
-0
hw/vfio/container.c int vfio_container_dma_map(VFIOContainer *bcontainer, · line 85
Quoted 5 lines
hw/vfio/container.c +1
-1@@ -82,7 +82,7 @@ int vfio_container_dma_map(VFIOContainer *bcontainer,RAMBlock *rb = mr->ram_block;int mfd = rb ?qemu_ram_get_fd (rb) :-1 ; -if (mfd >=0 && vioc->dma_map_file) { +if (mfd >=0 && vioc->dma_map_file &&qemu_ram_is_shared (rb)) {
I think we should introduce an helper to check that the conditions for using the .dma_map_file() handler are met. Something like :
if (vfio_container_can_dma_map_file(bcontainer, ...)) {
RAMBlock *rb = mr->ram_block;and
/*
* We can use IOMMU DMA mapping (IOMMU_IOAS_MAP_FILE) for :
*
* 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
* 2) RAM device sub-regions (MMIO BARs)
*
* Private RAM mappings (MAP_PRIVATE) are strictly excluded. Because
* they are subject to copy-on-write (COW) anomalies, their
* underlying PFNs can permanently diverge from the backing file */
bool vfio_container_can_dma_map_file(VFIOContainer *bcontainer, ...)
{
return ...}
How's that ?
Thanks,
C.
Quoted 60 lines · Cédric Le Goater
Hello Chenyi,
Quoted 28 lines · Chenyi Qiang
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.Fix this by using
dma_map_file()only for shared RAMBlocks.Fixes fb32965b6dd8 ("vfio/iommufd: use IOMMU_IOAS_MAP_FILE")Reported-by Farrah Chen <[email protected]>Signed-off-by Chenyi Qiang <[email protected]>---
hw/vfio/container.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/container.c +0
-0hw/vfio/container.c int vfio_container_dma_map(VFIOContainer *bcontainer, · line 85
Quoted 4 lines
hw/vfio/container.c +1
-0@@ -82,7 +82,7 @@ int vfio_container_dma_map(VFIOContainer *bcontainer, RAMBlock *rb = mr->ram_block; int mfd = rb ? qemu_ram_get_fd(rb) : -1; - if (mfd >= 0 && vioc->dma_map_file) { + if (mfd >= 0 && vioc->dma_map_file && qemu_ram_is_shared(rb)) {I think we should introduce an helper to check that the conditions for using the .dma_map_file() handler are met. Something like :
if (
vfio_container_can_dma_map_file(bcontainer, ...)) { RAMBlock *rb =mr->ram_block;
and/*
* We can use IOMMU DMA mapping (IOMMU_IOAS_MAP_FILE) for :
*
* 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
* 2) RAM device sub-regions (MMIO BARs)
*
* Private RAM mappings (MAP_PRIVATE) are strictly excluded. Because
* they are subject to copy-on-write (COW) anomalies, their
* underlying PFNs can permanently diverge from the backing file */
boolvfio_container_can_dma_map_file(VFIOContainer *bcontainer, ...){
return ...
}
Quoted 1 line
How's that ?
Thanks for the review. I'm fine with introducing the helper.
But I'm not sure if it's cleaner to include duplicated calculations in both the caller and helper.
+static bool vfio_container_can_dma_map_file(VFIOContainer *bcontainer, RAMBlock *rb) +{
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+
+ return rb && qemu_ram_get_fd(rb) >= 0 && vioc->dma_map_file &&
+ qemu_ram_is_shared(rb);
+}
+
int vfio_container_dma_map(VFIOContainer *bcontainer, ...)
{
...
RAMBlock *rb = mr->ram_block;
int mfd = rb ? qemu_ram_get_fd(rb) : -1;- if (mfd >= 0 && vioc->dma_map_file && qemu_ram_is_shared(rb)) {
+ if (vfio_container_can_dma_map_file(bcontainer, rb)) {
unsigned long start = vaddr - qemu_ram_get_host_addr(rb);
unsigned long offset = qemu_ram_get_fd_offset(rb);Perhaps it can be simplified slightly.
Quoted 88 lines · Chenyi Qiang
Quoted 60 lines · Cédric Le Goater
Hello Chenyi,
Quoted 28 lines · Chenyi Qiang
vfio_container_dma_map()usesdma_map_file()whenever a RAMBlock has an fd and the VFIO IOMMU backend supports file-based DMA mapping. That is not correct for private file-backed RAM.
dma_map_file()resolves PFNs from the backing file, but private mappings can run on different PFNs than the file itself. As a result, usingdma_map_file()on a private RAMBlock can program DMA against pages that do not back QEMU's actual guest memory.This was observed with hugetlbfs-backed guest memory and iommufd/VFIO:
share=on works, while share=off can fault because the file-backed PFNs can diverge from the PFNs backing QEMU's private mapping.Fix this by using
dma_map_file()only for shared RAMBlocks.Fixes fb32965b6dd8 ("vfio/iommufd: use IOMMU_IOAS_MAP_FILE")Reported-by Farrah Chen <[email protected]>Signed-off-by Chenyi Qiang <[email protected]>--- hw/vfio/container.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/container.c +0
-0hw/vfio/container.c int vfio_container_dma_map(VFIOContainer *bcontainer, · line 85
Quoted 4 lines
hw/vfio/container.c +1
-0@@ -82,7 +82,7 @@ int vfio_container_dma_map(VFIOContainer *bcontainer, RAMBlock *rb = mr->ram_block; int mfd = rb ? qemu_ram_get_fd(rb) : -1; - if (mfd >= 0 && vioc->dma_map_file) { + if (mfd >= 0 && vioc->dma_map_file && qemu_ram_is_shared(rb)) {I think we should introduce an helper to check that the conditions for using the .dma_map_file() handler are met. Something like :
if (
vfio_container_can_dma_map_file(bcontainer, ...)) { RAMBlock *rb =mr->ram_block;and
/*
* We can use IOMMU DMA mapping (IOMMU_IOAS_MAP_FILE) for :
*
* 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
* 2) RAM device sub-regions (MMIO BARs)
*
* Private RAM mappings (MAP_PRIVATE) are strictly excluded. Because
* they are subject to copy-on-write (COW) anomalies, their
* underlying PFNs can permanently diverge from the backing file */
boolvfio_container_can_dma_map_file(VFIOContainer *bcontainer, ...){
return ...
}Quoted 1 line
How's that ?
Thanks for the review. I'm fine with introducing the helper.
But I'm not sure if it's cleaner to include duplicated calculations in both the caller and helper.
(quoted diff) +8
-0+static bool vfio_container_can_dma_map_file(VFIOContainer *bcontainer, RAMBlock *rb) +{ + VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer); + + return rb && qemu_ram_get_fd(rb) >= 0 && vioc->dma_map_file && + qemu_ram_is_shared(rb); +} +int
vfio_container_dma_map(VFIOContainer *bcontainer, ...){ ... RAMBlock *rb = mr->ram_block; int mfd = rb ? qemu_ram_get_fd(rb) : -1;hw/vfio/container.c +1
-1-if (mfd >=0 && vioc->dma_map_file &&qemu_ram_is_shared (rb)) { +if (vfio_container_can_dma_map_file (bcontainer, rb)) {unsigned long start = vaddr -qemu_ram_get_host_addr (rb);unsigned long offset =qemu_ram_get_fd_offset (rb);
Quoted 2 lines
Perhaps it can be simplified slightly.
I was thinking moving the check on qemu_ram_get_fd(rb) within the new routine. Something like :
static bool vfio_container_can_dma_map_file(VFIOContainer *bcontainer,
MemoryRegion *mr, int *fd)
{
VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
RAMBlock *rb = mr->ram_block; if (!vioc->dma_map_file || !rb) {
return false;
}
*fd = qemu_ram_get_fd(rb);
if (*fd < 0) {
return false;
}
/*
* We can use IOMMU DMA mapping (IOMMU_IOAS_MAP_FILE) for :
*
* 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
* 2) RAM device sub-regions (MMIO BARs)
*
* Private RAM mappings (MAP_PRIVATE) are strictly excluded. Because
* they are subject to copy-on-write (COW) anomalies, their underlying
* PFNs can permanently diverge from the backing file
*/
return qemu_ram_is_shared(rb) || memory_region_is_ram_device(mr);}
or simpler :
static bool vfio_container_can_dma_map_file(MemoryRegion *mr, int *fd)
{
RAMBlock *rb = mr->ram_block; if (!rb) {
return false;
}
*fd = qemu_ram_get_fd(rb);
if (*fd < 0) {
return false;
}
/* ... */
return qemu_ram_is_shared(rb) || memory_region_is_ram_device(mr);}
I think option 2. is best. you choose.
Thanks,
C.