hw/vfio: Enable hugepfnmap for non-power-of-2 device memory regions
PATCH v3 3/3 hw/vfio: align mmap to power-of-2 of region size for hugepfnmap
In reply to [email protected]From: Ankit Agrawal <[email protected]>
On Grace-based systems such as GB200, device memory is exposed as a BAR but the actual mappable size is not power-of-2 aligned. The previous algorithm aligned each sparse mmap area based on its individual size using ctz64() which prevented efficient huge page usage by the kernel.
Adjust VFIO region mapping alignment to use the next power-of-2 of the total region size and place the sparse subregions at their appropriate offset. This provides better opportunities to get huge alignment allowing the kernel to use larger page sizes for the VMA.
This enables the use of PMD-level huge pages which can significantly improve memory access performance and reduce TLB pressure for large device memory regions.
With this change:
- Create a single aligned base mapping for the entire region
- Change Alignment to be based on pow2ceil(region->size), capped at 1GiB
- Unmap gaps between sparse regions
- Use MAP_FIXED to overlay sparse mmap areas at their offsets
Example VMA for device memory of size 0x2F00F00000 on GB200:
Before (misaligned, no hugepfnmap):
ff88ff000000-ffb7fff00000 rw-s 400000000000 00:06 727 /dev/vfio/devices/vfio1
After (aligned to 1GiB boundary, hugepfnmap enabled):
ff8ac0000000-ffb9c0f00000 rw-s 400000000000 00:06 727 /dev/vfio/devices/vfio1
Requires sparse regions to be sorted by offset (done in previous patch) to correctly identify and handle gaps.
--- hw/vfio/region.c | 81 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 27 deletions(-)
hw/vfio/region.c +54 -27
@@ -344,8 +344,11 @@ static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp)int vfio_region_mmap (VFIORegion *region) { -int i, ret, prot =0 ; +void *map_base, *map_align;Error *local_err =NULL ; +int i, ret, prot =0 ; +off_t map_offset =0 ; +size_t align;char *name;int fd;
@@ -356,41 +359,56 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_READ ?PROT_READ :0 ; prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ; -for (i =0 ; i < region->nr_mmaps; i++) { -size_t align =MIN (1ULL <<ctz64 (region->mmaps[i].size),1 * GiB); -void *map_base, *map_align; +/* +* Align the mmap for more efficient mapping in the kernel. Ideally +* we'd know the PMD and PUD mapping sizes to use as discrete alignment +* intervals, but we don't. As of Linux v6.19, the largest PUD size +* supporting huge pfnmap is 1GiB (ARCH_SUPPORTS_PUD_PFNMAP is only set +* on x86_64). +* +* Align by power-of-two of the size of the entire region - capped +* by 1G - and place the sparse subregions at their appropriate offset. +* This will get maximum alignment. +* +* NB. qemu_memalign() and friends actually allocate memory, whereas +* the region size here can exceed host memory, therefore we manually +* create an oversized anonymous mapping and clean it up for alignment. +*/ -/* -* Align the mmap for more efficient mapping in the kernel. Ideally -* we'd know the PMD and PUD mapping sizes to use as discrete alignment -* intervals, but we don't. As of Linux v6.12, the largest PUD size -* supporting huge pfnmap is 1GiB (ARCH_SUPPORTS_PUD_PFNMAP is only set -* on x86_64). Align by power-of-two size, capped at 1GiB. -* -* NB. qemu_memalign() and friends actually allocate memory, whereas -* the region size here can exceed host memory, therefore we manually -* create an oversized anonymous mapping and clean it up for alignment. -*/ - map_base =mmap (0 , region->mmaps[i].size + align,PROT_NONE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); -if (map_base ==MAP_FAILED ) { - ret = -errno; - goto no_mmap; - } + align =MIN (pow2ceil (region->size),1 * GiB); - fd =vfio_device_get_region_fd (region->vbasedev, region->nr); + map_base =mmap (0 , region->size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (map_base ==MAP_FAILED ) { + ret = -errno; +trace_vfio_region_mmap_fault (memory_region_name (region->mem),-1 , + region->fd_offset, + region->fd_offset + region->size -1 , ret); +return ret; + } + + fd =vfio_device_get_region_fd (region->vbasedev, region->nr); - map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align); -munmap (map_base, map_align - map_base); -munmap (map_align + region->mmaps[i].size, - align - (map_align - map_base)); + map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align); +munmap (map_base, map_align - map_base); +munmap (map_align + region->size, + align - (map_align - map_base)); - region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, +for (i =0 ; i < region->nr_mmaps; i++) { +munmap (map_align + map_offset, region->mmaps[i].offset - map_offset); + region->mmaps[i].mmap =mmap (map_align + region->mmaps[i].offset, + region->mmaps[i].size, prot,MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) { ret = -errno; +/* +* Only unmap the rest of the region. Any mmaps that were successful +* will be unmapped in no_mmap. +*/ +munmap (map_align + region->mmaps[i].offset, + region->size - region->mmaps[i].offset); goto no_mmap; }
@@ -408,6 +426,15 @@ int vfio_region_mmap(VFIORegion *region) region->mmaps[i].offset, region->mmaps[i].offset + region->mmaps[i].size - 1); + + map_offset = region->mmaps[i].offset + region->mmaps[i].size; + } + +/* +* Unmap the rest of the region not covered by sparse mmap. +*/ +if (map_offset < region->size) { +munmap (map_align + map_offset, region->size - map_offset); }if (!vfio_region_create_dma_buf (region, &local_err)) {
Signature · 1 line
2.34.1
v3 cover hw/vfio: Enable hugepfnmap for non-power-of-2 device memory regions
From: Ankit Agrawal <[email protected]>
This series enables hugepfnmap support in QEMU for VFIO device memory regions that have non-power-of-2 sizes. This specifically addresses the needs of Grace-based systems (GB200) where device memory is exposed as a BAR.
## Problem
On Grace-based systems, device memory regions can have sizes like 0x2F00F00000 (not power-of-2). The current QEMU VFIO mapping code aligns each sparse mmap area independently using the trailing zeros of its size (ctz64), which results in suboptimal alignment for the overall VMA.
This prevents the kernel from using hugepfnmap that enables huge page mappings for device memory. Without proper alignment, the mapping falls back to PTE, significantly impacting performance due to increased TLB pressure and page table overhead for large memory regions.
## Solution
Patch 1: Sort sparse mmap regions by offset during setup and validate
that they don't overlap. This ensures predictable mapping order and enables gap detection.
Patch 2: Change the alignment strategy from per-sparse-region to
whole-region alignment using pow2ceil(region->size). Create a single aligned base mapping for the entire region, then overlay sparse areas with MAP_FIXED. Gaps between sparse regions are explicitly unmapped.
v3
* New patch 2/3 to add Error **param in vfio_region_setup (Cedric)
v2: https://lore.kernel.org/all/[email protected]/ [v2]
* Fixed the code returning early without trace (Shameer, Alex)
v1: https://lore.kernel.org/all/[email protected]/
Ankit Agrawal (3):
hw/vfio: sort and validate sparse mmap regions by offset
vfio: Add Error ** parameter to vfio_region_setup() hw/vfio: align mmap to power-of-2 of region size for hugepfnmap
hw/vfio/display.c | 2 +- hw/vfio/pci.c | 3 +- hw/vfio/region.c | 131 ++++++++++++++++++++++++++++++++---------- hw/vfio/vfio-region.h | 2 +- 4 files changed, 105 insertions(+), 33 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/3 hw/vfio: sort and validate sparse mmap regions by offset
From: Ankit Agrawal <[email protected]>
Sort sparse mmap regions by offset during region setup to ensure predictable mapping order, avoid overlaps and a proper handling of the gaps between sub-regions.
Add validation to detect overlapping sparse regions early during setup before any mapping operations begin.
The sorting is performed on the subregions ranges during vfio_setup_region_sparse_mmaps(). This also ensures that subsequent mapping code can rely on subregions being in ascending offset order.
This is preparatory work for alignment adjustments needed to support hugepfnmap on systems where device memory (e.g., Grace-based systems) may have non-power-of-2 sizes.
--- hw/vfio/region.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
hw/vfio/region.c +44 -1
@@ -149,6 +149,19 @@ static const MemoryRegionOps vfio_region_ops = { }, }; +static int vfio_mmap_compare_offset (const void *a,const void *b) +{ +const VFIOMmap *mmap_a = a; +const VFIOMmap *mmap_b = b; + +if (mmap_a->offset < mmap_b->offset) { +return -1 ; + }else if (mmap_a->offset > mmap_b->offset) { +return 1 ; + } +return 0 ; +} +static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {
@@ -182,6 +195,34 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, region->nr_mmaps = j; region->mmaps =g_realloc (region->mmaps, j *sizeof (VFIOMmap)); +/* +* Sort sparse mmaps by offset to ensure proper handling of gaps +* and predictable mapping order in vfio_region_mmap(). +*/ +if (region->nr_mmaps >1 ) { +qsort (region->mmaps, region->nr_mmaps,sizeof (VFIOMmap), + vfio_mmap_compare_offset); + +/* +* Validate that sparse regions dont overlap after sorting. +*/ +for (i =1 ; i < region->nr_mmaps; i++) { +off_t prev_end = region->mmaps[i -1 ].offset + + region->mmaps[i -1 ].size; +if (prev_end > region->mmaps[i].offset) { +error_report ("%s: overlapping sparse mmap regions detected " +"in region %d: [0x%lx-0x%lx] overlaps with [0x%lx-0x%lx]" , + __func__, region->nr, region->mmaps[i -1 ].offset, + prev_end -1 , region->mmaps[i].offset, + region->mmaps[i].offset + region->mmaps[i].size -1 ); +g_free (region->mmaps); + region->mmaps =NULL ; + region->nr_mmaps =0 ; +return -EINVAL ; + } + } + } +return 0 ; }
@@ -213,11 +254,13 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, ret =vfio_setup_region_sparse_mmaps (region, info); -if (ret) { +if (ret == -ENODEV ) { region->nr_mmaps =1 ; region->mmaps =g_new0 (VFIOMmap, region->nr_mmaps); region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; + }else if (ret) { +return ret; } } }
Signature · 1 line
2.34.1
PATCH v3 2/3 vfio: Add Error ** parameter to vfio_region_setup()
In reply to [email protected]From: Ankit Agrawal <[email protected]>
Add an Error **errp parameter to vfio_region_setup() to allow proper error handling instead of just returning error codes.
The function sets errors via error_setg_errno() when failures occur.
--- hw/vfio/display.c | 2 +- hw/vfio/pci.c | 3 +-- hw/vfio/region.c | 5 ++++- hw/vfio/vfio-region.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-)
hw/vfio/display.c +1 -1
@@ -449,7 +449,7 @@ static void vfio_display_region_update(void *opaque) ret =vfio_region_setup (OBJECT (vdev), &vdev->vbasedev, &dpy->region.buffer, plane.region_index, -"display" ); +"display" ,NULL );if (ret !=0 ) {error_report ("%s: vfio_region_setup(%d): %s" , __func__, plane.region_index,strerror (-ret));
hw/vfio/pci.c +1 -2
@@ -3056,11 +3056,10 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)char *name =g_strdup_printf ("%s BAR %d" , vbasedev->name, i); ret =vfio_region_setup (OBJECT (vdev), vbasedev, - &vdev->bars[i].region, i, name); + &vdev->bars[i].region, i, name, errp);g_free (name);if (ret) { -error_setg_errno (errp, -ret,"failed to get region %d info" , i);return false; }
hw/vfio/region.c +4 -1
@@ -227,13 +227,14 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, }int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name) +int index,const char *name,Error **errp) {struct vfio_region_info *info =NULL ;int ret; ret =vfio_device_get_region_info (vbasedev, index, &info);if (ret) { +error_setg_errno (errp, -ret,"failed to get region %d info" , index);return ret; }
@@ -260,6 +261,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; }else if (ret) { +error_setg_errno (errp, -ret,"failed to setup region %d" , + index);return ret; } }
hw/vfio/vfio-region.h +1 -1
@@ -38,7 +38,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t vfio_region_read (void *opaque,hwaddr addr,unsigned size);int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name); +int index,const char *name,Error **errp);int vfio_region_mmap (VFIORegion *region);void vfio_region_mmaps_set_enabled (VFIORegion *region,bool enabled);void vfio_region_unmap (VFIORegion *region);
Signature · 1 line
2.34.1
Re: [PATCH v3 1/3] hw/vfio: sort and validate sparse mmap regions by offset
In reply to [email protected]Quoted 60 lines · On 2/15/26 09:49, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
Sort sparse mmap regions by offset during region setup to ensure predictable mapping order, avoid overlaps and a proper handling of the gaps between sub-regions.
Add validation to detect overlapping sparse regions early during setup before any mapping operations begin.
The sorting is performed on the subregions ranges during
vfio_setup_region_sparse_mmaps(). This also ensures that subsequent mapping code can rely on subregions being in ascending offset order.This is preparatory work for alignment adjustments needed to support hugepfnmap on systems where device memory (e.g., Grace-based systems) may have non-power-of-2 sizes.
cc Alex Williamson <[email protected]>Reviewed-by Alex Williamson <[email protected]>Reviewed-by Shameer Kolothum <[email protected]>Signed-off-by Ankit Agrawal <[email protected]>--- hw/vfio/region.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)hw/vfio/region.c +20
-0@@ -149,6 +149,19 @@ static const MemoryRegionOps vfio_region_ops = { }, }; +static int vfio_mmap_compare_offset (const void *a,const void *b) +{ +const VFIOMmap *mmap_a = a; +const VFIOMmap *mmap_b = b; + +if (mmap_a->offset < mmap_b->offset) { +return -1 ; + }else if (mmap_a->offset > mmap_b->offset) { +return 1 ; + } +return 0 ; +} +static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {@@ -182,6 +195,34 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, region->nr_mmaps = j; region->mmaps =g_realloc (region->mmaps, j *sizeof (VFIOMmap)); +/* +* Sort sparse mmaps by offset to ensure proper handling of gaps +* and predictable mapping order in vfio_region_mmap(). +*/ +if (region->nr_mmaps >1 ) { +qsort (region->mmaps, region->nr_mmaps,sizeof (VFIOMmap), + vfio_mmap_compare_offset);
hw/vfio/region.c static int vfio_setup_region_sparse_mmaps(VFIORegion *region, · line 214
Quoted 10 lines
hw/vfio/region.c +10
-0+ +/* +* Validate that sparse regions dont overlap after sorting. +*/ +for (i =1 ; i < region->nr_mmaps; i++) { +off_t prev_end = region->mmaps[i -1 ].offset + + region->mmaps[i -1 ].size; +if (prev_end > region->mmaps[i].offset) { + error_report("%s: overlapping sparse mmap regions detected " + "in region %d: [0x%lx-0x%lx] overlaps with [0x%lx-0x%lx]",
please use PRIx64 instead of 'lx'
Quoted 29 lines
hw/vfio/region.c +14
-1+ __func__, region->nr, region->mmaps[i -1 ].offset, + prev_end -1 , region->mmaps[i].offset, + region->mmaps[i].offset + region->mmaps[i].size - 1); +g_free (region->mmaps); + region->mmaps =NULL ; + region->nr_mmaps =0 ; +return -EINVAL ; + } + } + } +return 0 ; }@@ -213,11 +254,13 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, ret =vfio_setup_region_sparse_mmaps (region, info); -if (ret) { +if (ret == -ENODEV ) { region->nr_mmaps =1 ; region->mmaps =g_new0 (VFIOMmap, region->nr_mmaps); region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; + }else if (ret) { +return ret; } } }
Re: [PATCH v3 2/3] vfio: Add Error ** parameter to vfio_region_setup()
In reply to [email protected]Quoted 67 lines · On 2/15/26 09:49, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
Add an Error **errp parameter to
vfio_region_setup()to allow proper error handling instead of just returning error codes.The function sets errors via
error_setg_errno()when failures occur.Signed-off-by Ankit Agrawal <[email protected]>--- hw/vfio/display.c | 2 +- hw/vfio/pci.c | 3 +-- hw/vfio/region.c | 5 ++++- hw/vfio/vfio-region.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-)hw/vfio/display.c +1
-1@@ -449,7 +449,7 @@ static void vfio_display_region_update(void *opaque) ret =vfio_region_setup (OBJECT (vdev), &vdev->vbasedev, &dpy->region.buffer, plane.region_index, -"display" ); +"display" ,NULL );if (ret !=0 ) {error_report ("%s: vfio_region_setup(%d): %s" , __func__, plane.region_index,strerror (-ret));hw/vfio/pci.c +1
-2@@ -3056,11 +3056,10 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)char *name =g_strdup_printf ("%s BAR %d" , vbasedev->name, i); ret =vfio_region_setup (OBJECT (vdev), vbasedev, - &vdev->bars[i].region, i, name); + &vdev->bars[i].region, i, name, errp);g_free (name);if (ret) { -error_setg_errno (errp, -ret,"failed to get region %d info" , i);return false; }hw/vfio/region.c +2
-1@@ -227,13 +227,14 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, }int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name) +int index,const char *name,Error **errp) {struct vfio_region_info *info =NULL ;int ret; ret =vfio_device_get_region_info (vbasedev, index, &info);if (ret) { +error_setg_errno (errp, -ret,"failed to get region %d info" , index);return ret; }
hw/vfio/region.c int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, · line 265
Quoted 5 lines
hw/vfio/region.c +2
-0@@ -260,6 +261,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; }else if (ret) { +error_setg_errno (errp, -ret,"failed to setup region %d" , + index);
vfio_setup_region_sparse_mmaps() could take an 'errp' parameter instead.
Thanks,
C.
Quoted 16 lines
hw/vfio/region.c +0
-0return ret; } }hw/vfio/vfio-region.h +1
-1@@ -38,7 +38,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t vfio_region_read (void *opaque,hwaddr addr,unsigned size);int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name); +int index,const char *name,Error **errp);int vfio_region_mmap (VFIORegion *region);void vfio_region_mmaps_set_enabled (VFIORegion *region,bool enabled);void vfio_region_unmap (VFIORegion *region);
Re: [PATCH v3 1/3] hw/vfio: sort and validate sparse mmap regions by offset
In reply to [email protected]Quoted 53 lines · On 2/15/26 09:49, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
Sort sparse mmap regions by offset during region setup to ensure predictable mapping order, avoid overlaps and a proper handling of the gaps between sub-regions.
Add validation to detect overlapping sparse regions early during setup before any mapping operations begin.
The sorting is performed on the subregions ranges during
vfio_setup_region_sparse_mmaps(). This also ensures that subsequent mapping code can rely on subregions being in ascending offset order.This is preparatory work for alignment adjustments needed to support hugepfnmap on systems where device memory (e.g., Grace-based systems) may have non-power-of-2 sizes.
cc Alex Williamson <[email protected]>Reviewed-by Alex Williamson <[email protected]>Reviewed-by Shameer Kolothum <[email protected]>Signed-off-by Ankit Agrawal <[email protected]>--- hw/vfio/region.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)hw/vfio/region.c +13
-0@@ -149,6 +149,19 @@ static const MemoryRegionOps vfio_region_ops = { }, }; +static int vfio_mmap_compare_offset (const void *a,const void *b) +{ +const VFIOMmap *mmap_a = a; +const VFIOMmap *mmap_b = b; + +if (mmap_a->offset < mmap_b->offset) { +return -1 ; + }else if (mmap_a->offset > mmap_b->offset) { +return 1 ; + } +return 0 ; +} +static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {@@ -182,6 +195,34 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, region->nr_mmaps = j; region->mmaps =g_realloc (region->mmaps, j *sizeof (VFIOMmap));
hw/vfio/region.c static int vfio_setup_region_sparse_mmaps(VFIORegion *region, · line 207
Quoted 10 lines
hw/vfio/region.c +10
-0+/* +* Sort sparse mmaps by offset to ensure proper handling of gaps +* and predictable mapping order in vfio_region_mmap(). +*/ +if (region->nr_mmaps >1 ) { +qsort (region->mmaps, region->nr_mmaps,sizeof (VFIOMmap), + vfio_mmap_compare_offset); + +/* +* Validate that sparse regions dont overlap after sorting.
dont -> don't
Quoted 36 lines
hw/vfio/region.c +21
-1+*/ +for (i =1 ; i < region->nr_mmaps; i++) { +off_t prev_end = region->mmaps[i -1 ].offset + + region->mmaps[i -1 ].size; +if (prev_end > region->mmaps[i].offset) { +error_report ("%s: overlapping sparse mmap regions detected " +"in region %d: [0x%lx-0x%lx] overlaps with [0x%lx-0x%lx]" , + __func__, region->nr, region->mmaps[i -1 ].offset, + prev_end -1 , region->mmaps[i].offset, + region->mmaps[i].offset + region->mmaps[i].size -1 ); +g_free (region->mmaps); + region->mmaps =NULL ; + region->nr_mmaps =0 ; +return -EINVAL ; + } + } + } +return 0 ; }@@ -213,11 +254,13 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, ret =vfio_setup_region_sparse_mmaps (region, info); -if (ret) { +if (ret == -ENODEV ) { region->nr_mmaps =1 ; region->mmaps =g_new0 (VFIOMmap, region->nr_mmaps); region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; + }else if (ret) { +return ret; } } }
Re: [PATCH v3 2/3] vfio: Add Error ** parameter to vfio_region_setup()
In reply to [email protected]Quoted 21 lines · On 2/15/26 09:49, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
Add an Error **errp parameter to
vfio_region_setup()to allow proper error handling instead of just returning error codes.The function sets errors via
error_setg_errno()when failures occur.Signed-off-by Ankit Agrawal <[email protected]>--- hw/vfio/display.c | 2 +- hw/vfio/pci.c | 3 +-- hw/vfio/region.c | 5 ++++- hw/vfio/vfio-region.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-)hw/vfio/display.c +0
-0
hw/vfio/display.c static void vfio_display_region_update(void *opaque) · line 455
Quoted 8 lines
hw/vfio/display.c +1
-1@@ -449,7 +449,7 @@ static void vfio_display_region_update(void *opaque) ret =vfio_region_setup (OBJECT (vdev), &vdev->vbasedev, &dpy->region.buffer, plane.region_index, -"display" ); +"display" ,NULL );if (ret !=0 ) {error_report ("%s: vfio_region_setup(%d): %s" , __func__, plane.region_index,strerror (-ret));
or better, user error_report_err().
Quoted 59 lines
hw/vfio/pci.c +1
-2@@ -3056,11 +3056,10 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)char *name =g_strdup_printf ("%s BAR %d" , vbasedev->name, i); ret =vfio_region_setup (OBJECT (vdev), vbasedev, - &vdev->bars[i].region, i, name); + &vdev->bars[i].region, i, name, errp);g_free (name);if (ret) { -error_setg_errno (errp, -ret,"failed to get region %d info" , i);return false; }hw/vfio/region.c +4
-1@@ -227,13 +227,14 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region, }int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name) +int index,const char *name,Error **errp) {struct vfio_region_info *info =NULL ;int ret; ret =vfio_device_get_region_info (vbasedev, index, &info);if (ret) { +error_setg_errno (errp, -ret,"failed to get region %d info" , index);return ret; }@@ -260,6 +261,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->mmaps[0 ].offset =0 ; region->mmaps[0 ].size = region->size; }else if (ret) { +error_setg_errno (errp, -ret,"failed to setup region %d" , + index);return ret; } }hw/vfio/vfio-region.h +1
-1@@ -38,7 +38,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t vfio_region_read (void *opaque,hwaddr addr,unsigned size);int vfio_region_setup (Object *obj,VFIODevice *vbasedev,VFIORegion *region, -int index,const char *name); +int index,const char *name,Error **errp);int vfio_region_mmap (VFIORegion *region);void vfio_region_mmaps_set_enabled (VFIORegion *region,bool enabled);void vfio_region_unmap (VFIORegion *region);
Re: [PATCH v3 3/3] hw/vfio: align mmap to power-of-2 of region size for hugepfnmap
In reply to [email protected]Quoted 25 lines · On 2/15/26 09:49, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
On Grace-based systems such as GB200, device memory is exposed as a BAR but the actual mappable size is not power-of-2 aligned. The previous algorithm aligned each sparse mmap area based on its individual size using
ctz64()which prevented efficient huge page usage by the kernel.Adjust VFIO region mapping alignment to use the next power-of-2 of the total region size and place the sparse subregions at their appropriate offset. This provides better opportunities to get huge alignment allowing the kernel to use larger page sizes for the VMA.
This enables the use of PMD-level huge pages which can significantly improve memory access performance and reduce TLB pressure for large device memory regions.
With this change:
- Create a single aligned base mapping for the entire region
- Change Alignment to be based onpow2ceil(region->size), capped at 1GiB
- Unmap gaps between sparse regions
- UseMAP_FIXEDto overlay sparse mmap areas at their offsetsExample VMA for device memory of size
0x2F00F00000on GB200:
Quoted 8 lines
Before (misaligned, no hugepfnmap):
ff88ff000000-ffb7fff00000 rw-s 400000000000 00:06 727/dev/vfio/devices/vfio1After (aligned to 1GiB boundary, hugepfnmap enabled):
ff8ac0000000-ffb9c0f00000 rw-s 400000000000 00:06 727/dev/vfio/devices/vfio1Requires sparse regions to be sorted by offset (done in previous patch) to correctly identify and handle gaps.
Please add a visible comment saying that vfio_region_mmap() assumes that the array mmap[] is sorted. May be before the loop :
for (i = 0; i < region->nr_mmaps; i++) {
...Or some other place, but it should be clear.
/*
* Regions should already be sorted by vfio_setup_region_sparse_mmaps().
* This is critical for the algorithm below which relies on ascending
* order.
*/
Thanks,
C.
Quoted 126 lines
cc Alex Williamson <[email protected]>Reviewed-by Alex Williamson <[email protected]>Reviewed-by Shameer Kolothum <[email protected]>Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Ankit Agrawal <[email protected]>--- hw/vfio/region.c | 81 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 27 deletions(-)hw/vfio/region.c +54
-27@@ -344,8 +344,11 @@ static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp)int vfio_region_mmap (VFIORegion *region) { -int i, ret, prot =0 ; +void *map_base, *map_align;Error *local_err =NULL ; +int i, ret, prot =0 ; +off_t map_offset =0 ; +size_t align;char *name;int fd;@@ -356,41 +359,56 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_READ ?PROT_READ :0 ; prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ; -for (i =0 ; i < region->nr_mmaps; i++) { -size_t align =MIN (1ULL <<ctz64 (region->mmaps[i].size),1 * GiB); -void *map_base, *map_align; +/* +* Align the mmap for more efficient mapping in the kernel. Ideally +* we'd know the PMD and PUD mapping sizes to use as discrete alignment +* intervals, but we don't. As of Linux v6.19, the largest PUD size +* supporting huge pfnmap is 1GiB (ARCH_SUPPORTS_PUD_PFNMAP is only set +* on x86_64). +* +* Align by power-of-two of the size of the entire region - capped +* by 1G - and place the sparse subregions at their appropriate offset. +* This will get maximum alignment. +* +* NB. qemu_memalign() and friends actually allocate memory, whereas +* the region size here can exceed host memory, therefore we manually +* create an oversized anonymous mapping and clean it up for alignment. +*/ -/* -* Align the mmap for more efficient mapping in the kernel. Ideally -* we'd know the PMD and PUD mapping sizes to use as discrete alignment -* intervals, but we don't. As of Linux v6.12, the largest PUD size -* supporting huge pfnmap is 1GiB (ARCH_SUPPORTS_PUD_PFNMAP is only set -* on x86_64). Align by power-of-two size, capped at 1GiB. -* -* NB. qemu_memalign() and friends actually allocate memory, whereas -* the region size here can exceed host memory, therefore we manually -* create an oversized anonymous mapping and clean it up for alignment. -*/ - map_base =mmap (0 , region->mmaps[i].size + align,PROT_NONE , -MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); -if (map_base ==MAP_FAILED ) { - ret = -errno; - goto no_mmap; - } + align =MIN (pow2ceil (region->size),1 * GiB); - fd =vfio_device_get_region_fd (region->vbasedev, region->nr); + map_base =mmap (0 , region->size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (map_base ==MAP_FAILED ) { + ret = -errno; +trace_vfio_region_mmap_fault (memory_region_name (region->mem),-1 , + region->fd_offset, + region->fd_offset + region->size -1 , ret); +return ret; + } + + fd =vfio_device_get_region_fd (region->vbasedev, region->nr); - map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align); -munmap (map_base, map_align - map_base); -munmap (map_align + region->mmaps[i].size, - align - (map_align - map_base)); + map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align); +munmap (map_base, map_align - map_base); +munmap (map_align + region->size, + align - (map_align - map_base)); - region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, +for (i =0 ; i < region->nr_mmaps; i++) { +munmap (map_align + map_offset, region->mmaps[i].offset - map_offset); + region->mmaps[i].mmap =mmap (map_align + region->mmaps[i].offset, + region->mmaps[i].size, prot,MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) { ret = -errno; +/* +* Only unmap the rest of the region. Any mmaps that were successful +* will be unmapped in no_mmap. +*/ +munmap (map_align + region->mmaps[i].offset, + region->size - region->mmaps[i].offset); goto no_mmap; }@@ -408,6 +426,15 @@ int vfio_region_mmap(VFIORegion *region) region->mmaps[i].offset, region->mmaps[i].offset + region->mmaps[i].size - 1); + + map_offset = region->mmaps[i].offset + region->mmaps[i].size; + } + +/* +* Unmap the rest of the region not covered by sparse mmap. +*/ +if (map_offset < region->size) { +munmap (map_align + map_offset, region->size - map_offset); }if (!vfio_region_create_dma_buf (region, &local_err)) {