* [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset
@ 2025-11-21 14:11 ankita
2025-11-21 14:11 ` [PATCH v3 1/7] vfio/nvgrace-gpu: Use faults to map device memory ankita
` (6 more replies)
0 siblings, 7 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
NVIDIA's Grace based system have large GPU device memory. The device
memory is mapped as VM_PFNMAP in the VMM VMA. The nvgrace-gpu
module could make use of the huge PFNMAP support added in mm [1].
To achieve this, nvgrace-gpu module is updated to implement huge_fault ops.
The implementation establishes mapping according to the order request.
Note that if the PFN or the VMA address is unaligned to the order, the
mapping fallbacks to the PTE level.
Secondly, it is expected that the mapping not be re-established until
the GPU is ready post reset. Presence of the mappings during that time
could potentially leads to harmless corrected RAS events to be logged if
the CPU attempts to do speculative reads on the GPU memory on the Grace
systems.
It can take several seconds for the GPU to be ready. So it is desirable
that the time overlaps as much of the VM startup as possible to reduce
impact on the VM bootup time. The GPU readiness state is thus checked
on the first fault/huge_fault request which amortizes the GPU readiness
time. The GPU readiness is checked through BAR0 registers as is done
at the device probe.
Patch 1 updates the mapping mechanism to be done through faults.
Patch 2 splits the code to map at the various levels.
Patch 3 implements support for huge pfnmap.
Patch 4 move the code to map the BAR to a separate function.
Path 5-7 intercepts reset request and ensures that the GP is ready
before re-establishing the mapping after reset.
Applied over 6.18-rc6.
Link: https://lore.kernel.org/all/20240826204353.2228736-1-peterx@redhat.com/ [1]
Changelog:
v3:
- Moved the code for BAR mapping to a separate function.
- Added BAR0 mapping during open. Ensures BAR0 is mapped when registers
are checked. (Thanks Alex Williamson, Jason Gunthorpe for suggestion)
- Added check for GPU readiness on nvgrace_gpu_map_device_mem. (Thanks
Alex Williamson for the suggestion.
Link: https://lore.kernel.org/all/20251118074422.58081-1-ankita@nvidia.com/ [v2]
- Fixed build kernel warning
- subject text changes
- Rebased to 6.18-rc6.
Link: https://lore.kernel.org/all/20251117124159.3560-1-ankita@nvidia.com/ [v1]
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
Ankit Agrawal (7):
vfio/nvgrace-gpu: Use faults to map device memory
vfio: export function to map the VMA
vfio/nvgrace-gpu: Add support for huge pfnmap
vfio: export vfio_find_cap_start
vfio: move barmap to a separate function and export
vfio/nvgrace-gpu: split the code to wait for GPU ready
vfio/nvgrace-gpu: wait for the GPU mem to be ready
drivers/vfio/pci/nvgrace-gpu/main.c | 183 ++++++++++++++++++++++------
drivers/vfio/pci/vfio_pci_config.c | 3 +-
drivers/vfio/pci/vfio_pci_core.c | 84 ++++++++-----
include/linux/vfio_pci_core.h | 4 +
4 files changed, 207 insertions(+), 67 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 1/7] vfio/nvgrace-gpu: Use faults to map device memory
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 14:11 ` [PATCH v3 2/7] vfio: export function to map the VMA ankita
` (5 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
To make use of the huge pfnmap support and to support zap/remap
sequence, fault/huge_fault ops based mapping mechanism needs to
be implemented.
Currently nvgrace-gpu module relies on remap_pfn_range to do
the mapping during VM bootup. Replace it to instead rely on fault
and use vmf_insert_pfn to setup the mapping.
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/nvgrace-gpu/main.c | 50 ++++++++++++++++++-----------
1 file changed, 31 insertions(+), 19 deletions(-)
diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index e346392b72f6..ecfecd0916c9 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -130,6 +130,33 @@ static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
vfio_pci_core_close_device(core_vdev);
}
+static vm_fault_t nvgrace_gpu_vfio_pci_fault(struct vm_fault *vmf)
+{
+ struct vm_area_struct *vma = vmf->vma;
+ struct nvgrace_gpu_pci_core_device *nvdev = vma->vm_private_data;
+ int index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
+ vm_fault_t ret = VM_FAULT_SIGBUS;
+ struct mem_region *memregion;
+ unsigned long pgoff, pfn;
+
+ memregion = nvgrace_gpu_memregion(index, nvdev);
+ if (!memregion)
+ return ret;
+
+ pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
+ pfn = PHYS_PFN(memregion->memphys) + pgoff;
+
+ down_read(&nvdev->core_device.memory_lock);
+ ret = vmf_insert_pfn(vmf->vma, vmf->address, pfn);
+ up_read(&nvdev->core_device.memory_lock);
+
+ return ret;
+}
+
+static const struct vm_operations_struct nvgrace_gpu_vfio_pci_mmap_ops = {
+ .fault = nvgrace_gpu_vfio_pci_fault,
+};
+
static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
struct vm_area_struct *vma)
{
@@ -137,10 +164,8 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
core_device.vdev);
struct mem_region *memregion;
- unsigned long start_pfn;
u64 req_len, pgoff, end;
unsigned int index;
- int ret = 0;
index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
@@ -157,7 +182,6 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
if (check_sub_overflow(vma->vm_end, vma->vm_start, &req_len) ||
- check_add_overflow(PHYS_PFN(memregion->memphys), pgoff, &start_pfn) ||
check_add_overflow(PFN_PHYS(pgoff), req_len, &end))
return -EOVERFLOW;
@@ -168,6 +192,8 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
if (end > memregion->memlength)
return -EINVAL;
+ vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
+
/*
* The carved out region of the device memory needs the NORMAL_NC
* property. Communicate as such to the hypervisor.
@@ -184,23 +210,9 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
}
- /*
- * Perform a PFN map to the memory and back the device BAR by the
- * GPU memory.
- *
- * The available GPU memory size may not be power-of-2 aligned. The
- * remainder is only backed by vfio_device_ops read/write handlers.
- *
- * During device reset, the GPU is safely disconnected to the CPU
- * and access to the BAR will be immediately returned preventing
- * machine check.
- */
- ret = remap_pfn_range(vma, vma->vm_start, start_pfn,
- req_len, vma->vm_page_prot);
- if (ret)
- return ret;
- vma->vm_pgoff = start_pfn;
+ vma->vm_ops = &nvgrace_gpu_vfio_pci_mmap_ops;
+ vma->vm_private_data = nvdev;
return 0;
}
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 2/7] vfio: export function to map the VMA
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
2025-11-21 14:11 ` [PATCH v3 1/7] vfio/nvgrace-gpu: Use faults to map device memory ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 14:11 ` [PATCH v3 3/7] vfio/nvgrace-gpu: Add support for huge pfnmap ankita
` (4 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
Take out the implementation to map the VMA to the PTE/PMD/PUD
as a separate function.
Export the function to be used by nvgrace-gpu module.
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/vfio_pci_core.c | 46 ++++++++++++++++++++------------
include/linux/vfio_pci_core.h | 2 ++
2 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 7dcf5439dedc..29dcf78905a6 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1640,6 +1640,34 @@ static unsigned long vma_to_pfn(struct vm_area_struct *vma)
return (pci_resource_start(vdev->pdev, index) >> PAGE_SHIFT) + pgoff;
}
+vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf,
+ unsigned long pfn,
+ unsigned int order)
+{
+ vm_fault_t ret;
+
+ switch (order) {
+ case 0:
+ ret = vmf_insert_pfn(vmf->vma, vmf->address, pfn);
+ break;
+#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
+ case PMD_ORDER:
+ ret = vmf_insert_pfn_pmd(vmf, pfn, false);
+ break;
+#endif
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP
+ case PUD_ORDER:
+ ret = vmf_insert_pfn_pud(vmf, pfn, false);
+ break;
+#endif
+ default:
+ ret = VM_FAULT_FALLBACK;
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_map_pfn);
+
static vm_fault_t vfio_pci_mmap_huge_fault(struct vm_fault *vmf,
unsigned int order)
{
@@ -1662,23 +1690,7 @@ static vm_fault_t vfio_pci_mmap_huge_fault(struct vm_fault *vmf,
if (vdev->pm_runtime_engaged || !__vfio_pci_memory_enabled(vdev))
goto out_unlock;
- switch (order) {
- case 0:
- ret = vmf_insert_pfn(vma, vmf->address, pfn);
- break;
-#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
- case PMD_ORDER:
- ret = vmf_insert_pfn_pmd(vmf, pfn, false);
- break;
-#endif
-#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP
- case PUD_ORDER:
- ret = vmf_insert_pfn_pud(vmf, pfn, false);
- break;
-#endif
- default:
- ret = VM_FAULT_FALLBACK;
- }
+ ret = vfio_pci_map_pfn(vmf, pfn, order);
out_unlock:
up_read(&vdev->memory_lock);
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index f541044e42a2..058acded858b 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -119,6 +119,8 @@ ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
size_t count, loff_t *ppos);
ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
size_t count, loff_t *ppos);
+vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf, unsigned long pfn,
+ unsigned int order);
int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 3/7] vfio/nvgrace-gpu: Add support for huge pfnmap
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
2025-11-21 14:11 ` [PATCH v3 1/7] vfio/nvgrace-gpu: Use faults to map device memory ankita
2025-11-21 14:11 ` [PATCH v3 2/7] vfio: export function to map the VMA ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 14:11 ` [PATCH v3 4/7] vfio: export vfio_find_cap_start ankita
` (3 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
NVIDIA's Grace based systems have large device memory. The device
memory is mapped as VM_PFNMAP in the VMM VMA. The nvgrace-gpu
module could make use of the huge PFNMAP support added in mm [1].
To achieve this, nvgrace-gpu module is updated to implement huge_fault ops.
The implementation establishes mapping according to the order request.
Note that if the PFN or the VMA address is unaligned to the order, the
mapping fallbacks to the PTE level.
Link: https://lore.kernel.org/all/20240826204353.2228736-1-peterx@redhat.com/ [1]
cc: Alex Williamson <alex@shazbot.org>
cc: Jason Gunthorpe <jgg@ziepe.ca>
cc: Vikram Sethi <vsethi@nvidia.com>
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/nvgrace-gpu/main.c | 44 +++++++++++++++++++++++------
1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index ecfecd0916c9..3883a9de170f 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -130,33 +130,59 @@ static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
vfio_pci_core_close_device(core_vdev);
}
-static vm_fault_t nvgrace_gpu_vfio_pci_fault(struct vm_fault *vmf)
+static vm_fault_t nvgrace_gpu_vfio_pci_huge_fault(struct vm_fault *vmf,
+ unsigned int order)
{
struct vm_area_struct *vma = vmf->vma;
struct nvgrace_gpu_pci_core_device *nvdev = vma->vm_private_data;
int index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
vm_fault_t ret = VM_FAULT_SIGBUS;
struct mem_region *memregion;
- unsigned long pgoff, pfn;
+ unsigned long pgoff, pfn, addr;
memregion = nvgrace_gpu_memregion(index, nvdev);
if (!memregion)
return ret;
- pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
+ addr = vmf->address & ~((PAGE_SIZE << order) - 1);
+ pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
pfn = PHYS_PFN(memregion->memphys) + pgoff;
+ if (order && (addr < vma->vm_start ||
+ addr + (PAGE_SIZE << order) > vma->vm_end ||
+ pfn & ((1 << order) - 1)))
+ return VM_FAULT_FALLBACK;
+
down_read(&nvdev->core_device.memory_lock);
- ret = vmf_insert_pfn(vmf->vma, vmf->address, pfn);
+ ret = vfio_pci_map_pfn(vmf, pfn, order);
up_read(&nvdev->core_device.memory_lock);
return ret;
}
+static vm_fault_t nvgrace_gpu_vfio_pci_fault(struct vm_fault *vmf)
+{
+ return nvgrace_gpu_vfio_pci_huge_fault(vmf, 0);
+}
+
static const struct vm_operations_struct nvgrace_gpu_vfio_pci_mmap_ops = {
.fault = nvgrace_gpu_vfio_pci_fault,
+#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
+ .huge_fault = nvgrace_gpu_vfio_pci_huge_fault,
+#endif
};
+static size_t nvgrace_gpu_aligned_devmem_size(size_t memlength)
+{
+#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
+ return ALIGN(memlength, PMD_SIZE);
+#endif
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP
+ return ALIGN(memlength, PUD_SIZE);
+#endif
+ return memlength;
+}
+
static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
struct vm_area_struct *vma)
{
@@ -186,10 +212,10 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
return -EOVERFLOW;
/*
- * Check that the mapping request does not go beyond available device
- * memory size
+ * Check that the mapping request does not go beyond the exposed
+ * device memory size.
*/
- if (end > memregion->memlength)
+ if (end > nvgrace_gpu_aligned_devmem_size(memregion->memlength))
return -EINVAL;
vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
@@ -210,7 +236,6 @@ static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
}
-
vma->vm_ops = &nvgrace_gpu_vfio_pci_mmap_ops;
vma->vm_private_data = nvdev;
@@ -260,7 +285,8 @@ nvgrace_gpu_ioctl_get_region_info(struct vfio_device *core_vdev,
sparse->nr_areas = 1;
sparse->areas[0].offset = 0;
- sparse->areas[0].size = memregion->memlength;
+ sparse->areas[0].size =
+ nvgrace_gpu_aligned_devmem_size(memregion->memlength);
sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
sparse->header.version = 1;
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 4/7] vfio: export vfio_find_cap_start
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
` (2 preceding siblings ...)
2025-11-21 14:11 ` [PATCH v3 3/7] vfio/nvgrace-gpu: Add support for huge pfnmap ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 14:11 ` [PATCH v3 5/7] vfio: move barmap to a separate function and export ankita
` (2 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
Export vfio_find_cap_start to be used by the nvgrace-gpu module.
This would be used to determine GPU FLR requests.
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/vfio_pci_config.c | 3 ++-
include/linux/vfio_pci_core.h | 1 +
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 333fd149c21a..50390189b586 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -1114,7 +1114,7 @@ int __init vfio_pci_init_perm_bits(void)
return ret;
}
-static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
+int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
{
u8 cap;
int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
@@ -1130,6 +1130,7 @@ static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
return pos;
}
+EXPORT_SYMBOL_GPL(vfio_find_cap_start);
static int vfio_msi_config_read(struct vfio_pci_core_device *vdev, int pos,
int count, struct perm_bits *perm,
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 058acded858b..a097a66485b4 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -132,6 +132,7 @@ void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar);
pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
pci_channel_state_t state);
+int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos);
ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
void __iomem *io, char __user *buf,
loff_t off, size_t count, size_t x_start,
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 5/7] vfio: move barmap to a separate function and export
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
` (3 preceding siblings ...)
2025-11-21 14:11 ` [PATCH v3 4/7] vfio: export vfio_find_cap_start ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 16:39 ` Alex Williamson
2025-11-21 14:11 ` [PATCH v3 6/7] vfio/nvgrace-gpu: split the code to wait for GPU ready ankita
2025-11-21 14:11 ` [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready ankita
6 siblings, 1 reply; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
Move the code to map the BAR to a separate function.
This would be reused by the nvgrace-gpu module.
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/vfio_pci_core.c | 38 ++++++++++++++++++++++----------
include/linux/vfio_pci_core.h | 1 +
2 files changed, 27 insertions(+), 12 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 29dcf78905a6..d1ff1c0aa727 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1717,6 +1717,29 @@ static const struct vm_operations_struct vfio_pci_mmap_ops = {
#endif
};
+int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index)
+{
+ struct pci_dev *pdev = vdev->pdev;
+ int ret;
+
+ if (vdev->barmap[index])
+ return 0;
+
+ ret = pci_request_selected_regions(pdev,
+ 1 << index, "vfio-pci");
+ if (ret)
+ return ret;
+
+ vdev->barmap[index] = pci_iomap(pdev, index, 0);
+ if (!vdev->barmap[index]) {
+ pci_release_selected_regions(pdev, 1 << index);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_core_barmap);
+
int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
{
struct vfio_pci_core_device *vdev =
@@ -1761,18 +1784,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
* Even though we don't make use of the barmap for the mmap,
* we need to request the region and the barmap tracks that.
*/
- if (!vdev->barmap[index]) {
- ret = pci_request_selected_regions(pdev,
- 1 << index, "vfio-pci");
- if (ret)
- return ret;
-
- vdev->barmap[index] = pci_iomap(pdev, index, 0);
- if (!vdev->barmap[index]) {
- pci_release_selected_regions(pdev, 1 << index);
- return -ENOMEM;
- }
- }
+ ret = vfio_pci_core_barmap(vdev, index);
+ if (ret)
+ return ret;
vma->vm_private_data = vdev;
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index a097a66485b4..75f04d613e0c 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -121,6 +121,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bu
size_t count, loff_t *ppos);
vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf, unsigned long pfn,
unsigned int order);
+int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index);
int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 6/7] vfio/nvgrace-gpu: split the code to wait for GPU ready
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
` (4 preceding siblings ...)
2025-11-21 14:11 ` [PATCH v3 5/7] vfio: move barmap to a separate function and export ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 14:11 ` [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready ankita
6 siblings, 0 replies; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
Split the function that check for the GPU device being ready on
the probe.
Move the code to wait for the GPU to be ready through BAR0 register
reads to a separate function. This would help reuse the code.
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/nvgrace-gpu/main.c | 33 ++++++++++++++++++-----------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index 3883a9de170f..7618c3f515cc 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -130,6 +130,24 @@ static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
vfio_pci_core_close_device(core_vdev);
}
+static int nvgrace_gpu_wait_device_ready(void __iomem *io)
+{
+ unsigned long timeout = jiffies + msecs_to_jiffies(POLL_TIMEOUT_MS);
+ int ret = -ETIME;
+
+ do {
+ if ((ioread32(io + C2C_LINK_BAR0_OFFSET) == STATUS_READY) &&
+ (ioread32(io + HBM_TRAINING_BAR0_OFFSET) == STATUS_READY)) {
+ ret = 0;
+ goto ready_check_exit;
+ }
+ msleep(POLL_QUANTUM_MS);
+ } while (!time_after(jiffies, timeout));
+
+ready_check_exit:
+ return ret;
+}
+
static vm_fault_t nvgrace_gpu_vfio_pci_huge_fault(struct vm_fault *vmf,
unsigned int order)
{
@@ -931,9 +949,8 @@ static bool nvgrace_gpu_has_mig_hw_bug(struct pci_dev *pdev)
* Ensure that the BAR0 region is enabled before accessing the
* registers.
*/
-static int nvgrace_gpu_wait_device_ready(struct pci_dev *pdev)
+static int nvgrace_gpu_check_device_ready(struct pci_dev *pdev)
{
- unsigned long timeout = jiffies + msecs_to_jiffies(POLL_TIMEOUT_MS);
void __iomem *io;
int ret = -ETIME;
@@ -951,16 +968,8 @@ static int nvgrace_gpu_wait_device_ready(struct pci_dev *pdev)
goto iomap_exit;
}
- do {
- if ((ioread32(io + C2C_LINK_BAR0_OFFSET) == STATUS_READY) &&
- (ioread32(io + HBM_TRAINING_BAR0_OFFSET) == STATUS_READY)) {
- ret = 0;
- goto reg_check_exit;
- }
- msleep(POLL_QUANTUM_MS);
- } while (!time_after(jiffies, timeout));
+ ret = nvgrace_gpu_wait_device_ready(io);
-reg_check_exit:
pci_iounmap(pdev, io);
iomap_exit:
pci_release_selected_regions(pdev, 1 << 0);
@@ -977,7 +986,7 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,
u64 memphys, memlength;
int ret;
- ret = nvgrace_gpu_wait_device_ready(pdev);
+ ret = nvgrace_gpu_check_device_ready(pdev);
if (ret)
return ret;
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
` (5 preceding siblings ...)
2025-11-21 14:11 ` [PATCH v3 6/7] vfio/nvgrace-gpu: split the code to wait for GPU ready ankita
@ 2025-11-21 14:11 ` ankita
2025-11-21 16:59 ` Alex Williamson
6 siblings, 1 reply; 12+ messages in thread
From: ankita @ 2025-11-21 14:11 UTC (permalink / raw)
To: ankita, jgg, yishaih, skolothumtho, kevin.tian, alex, aniketa,
vsethi, mochs
Cc: Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
From: Ankit Agrawal <ankita@nvidia.com>
Speculative prefetches from CPU to GPU memory until the GPU is
ready after reset can cause harmless corrected RAS events to
be logged on Grace systems. It is thus preferred that the
mapping not be re-established until the GPU is ready post reset.
The GPU readiness can be checked through BAR0 registers similar
to the checking at the time of device probe.
It can take several seconds for the GPU to be ready. So it is
desirable that the time overlaps as much of the VM startup as
possible to reduce impact on the VM bootup time. The GPU
readiness state is thus checked on the first fault/huge_fault
request which amortizes the GPU readiness time. The first fault
is checked using a flag. The flag is unset on every GPU reset
request.
Intercept the following calls to the GPU reset, unset gpu_mem_mapped.
Then use it to determine whether to wait before mapping.
1. VFIO_DEVICE_RESET ioctl call
2. FLR through config space.
cc: Alex Williamson <alex@shazbot.org>
cc: Jason Gunthorpe <jgg@ziepe.ca>
cc: Vikram Sethi <vsethi@nvidia.com>
Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
---
drivers/vfio/pci/nvgrace-gpu/main.c | 64 ++++++++++++++++++++++++++++-
1 file changed, 63 insertions(+), 1 deletion(-)
diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index 7618c3f515cc..23e3278aba25 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -58,6 +58,8 @@ struct nvgrace_gpu_pci_core_device {
/* Lock to control device memory kernel mapping */
struct mutex remap_lock;
bool has_mig_hw_bug;
+ /* Any GPU memory mapped to the VMA */
+ bool gpu_mem_mapped;
};
static void nvgrace_gpu_init_fake_bar_emu_regs(struct vfio_device *core_vdev)
@@ -102,9 +104,15 @@ static int nvgrace_gpu_open_device(struct vfio_device *core_vdev)
mutex_init(&nvdev->remap_lock);
}
+ nvdev->gpu_mem_mapped = false;
+
vfio_pci_core_finish_enable(vdev);
- return 0;
+ /*
+ * The GPU readiness is determined through BAR0 register reads.
+ * Make sure the BAR0 is mapped before any such check occur.
+ */
+ return vfio_pci_core_barmap(vdev, 0);
}
static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
@@ -158,6 +166,21 @@ static vm_fault_t nvgrace_gpu_vfio_pci_huge_fault(struct vm_fault *vmf,
struct mem_region *memregion;
unsigned long pgoff, pfn, addr;
+ /*
+ * If the GPU memory is accessed by the CPU while the GPU is
+ * not ready after reset, it can cause harmless corrected RAS
+ * events to be logged. Make sure the GPU is ready before
+ * establishing the mappings.
+ */
+ if (!nvdev->gpu_mem_mapped) {
+ struct vfio_pci_core_device *vdev = &nvdev->core_device;
+
+ if (nvgrace_gpu_wait_device_ready(vdev->barmap[0]))
+ return VM_FAULT_SIGBUS;
+
+ nvdev->gpu_mem_mapped = true;
+ }
+
memregion = nvgrace_gpu_memregion(index, nvdev);
if (!memregion)
return ret;
@@ -354,7 +377,17 @@ static long nvgrace_gpu_ioctl(struct vfio_device *core_vdev,
case VFIO_DEVICE_IOEVENTFD:
return -ENOTTY;
case VFIO_DEVICE_RESET:
+ struct nvgrace_gpu_pci_core_device *nvdev =
+ container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
+ core_device.vdev);
nvgrace_gpu_init_fake_bar_emu_regs(core_vdev);
+
+ /*
+ * GPU memory is exposed as device BAR2 (region 4,5).
+ * This would be zapped during GPU reset. Unset
+ * nvdev->gpu_mem_mapped to reflect just that.
+ */
+ nvdev->gpu_mem_mapped = false;
fallthrough;
default:
return vfio_pci_core_ioctl(core_vdev, cmd, arg);
@@ -439,11 +472,14 @@ nvgrace_gpu_write_config_emu(struct vfio_device *core_vdev,
struct nvgrace_gpu_pci_core_device *nvdev =
container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
core_device.vdev);
+ struct vfio_pci_core_device *vdev =
+ container_of(core_vdev, struct vfio_pci_core_device, vdev);
u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
struct mem_region *memregion = NULL;
size_t register_offset;
loff_t copy_offset;
size_t copy_count;
+ int cap_start = vfio_find_cap_start(vdev, pos);
if (vfio_pci_core_range_intersect_range(pos, count, PCI_BASE_ADDRESS_2,
sizeof(u64), ©_offset,
@@ -462,6 +498,23 @@ nvgrace_gpu_write_config_emu(struct vfio_device *core_vdev,
return copy_count;
}
+ if (vfio_pci_core_range_intersect_range(pos, count, cap_start + PCI_EXP_DEVCTL,
+ sizeof(u16), ©_offset,
+ ©_count, ®ister_offset)) {
+ __le16 val16;
+
+ if (copy_from_user((void *)&val16, buf, copy_count))
+ return -EFAULT;
+
+ /*
+ * GPU memory is exposed as device BAR2 (region 4,5).
+ * This would be zapped during GPU reset. Unset
+ * nvdev->gpu_mem_mapped to reflect just that.
+ */
+ if (val16 & cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR))
+ nvdev->gpu_mem_mapped = false;
+ }
+
return vfio_pci_core_write(core_vdev, buf, count, ppos);
}
@@ -478,9 +531,18 @@ static int
nvgrace_gpu_map_device_mem(int index,
struct nvgrace_gpu_pci_core_device *nvdev)
{
+ struct vfio_pci_core_device *vdev = &nvdev->core_device;
struct mem_region *memregion;
int ret = 0;
+ if (!nvdev->gpu_mem_mapped) {
+ ret = nvgrace_gpu_wait_device_ready(vdev->barmap[0]);
+ if (ret)
+ return ret;
+
+ nvdev->gpu_mem_mapped = true;
+ }
+
memregion = nvgrace_gpu_memregion(index, nvdev);
if (!memregion)
return -EINVAL;
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3 5/7] vfio: move barmap to a separate function and export
2025-11-21 14:11 ` [PATCH v3 5/7] vfio: move barmap to a separate function and export ankita
@ 2025-11-21 16:39 ` Alex Williamson
2025-11-21 20:58 ` Donald Dutile
0 siblings, 1 reply; 12+ messages in thread
From: Alex Williamson @ 2025-11-21 16:39 UTC (permalink / raw)
To: ankita
Cc: jgg, yishaih, skolothumtho, kevin.tian, aniketa, vsethi, mochs,
Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
On Fri, 21 Nov 2025 14:11:39 +0000
<ankita@nvidia.com> wrote:
> From: Ankit Agrawal <ankita@nvidia.com>
>
> Move the code to map the BAR to a separate function.
>
> This would be reused by the nvgrace-gpu module.
>
> Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
> ---
> drivers/vfio/pci/vfio_pci_core.c | 38 ++++++++++++++++++++++----------
> include/linux/vfio_pci_core.h | 1 +
> 2 files changed, 27 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 29dcf78905a6..d1ff1c0aa727 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -1717,6 +1717,29 @@ static const struct vm_operations_struct vfio_pci_mmap_ops = {
> #endif
> };
>
> +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index)
> +{
> + struct pci_dev *pdev = vdev->pdev;
> + int ret;
> +
> + if (vdev->barmap[index])
> + return 0;
> +
> + ret = pci_request_selected_regions(pdev,
> + 1 << index, "vfio-pci");
> + if (ret)
> + return ret;
> +
> + vdev->barmap[index] = pci_iomap(pdev, index, 0);
> + if (!vdev->barmap[index]) {
> + pci_release_selected_regions(pdev, 1 << index);
> + return -ENOMEM;
> + }
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(vfio_pci_core_barmap);
Looks a lot like vfio_pci_core_setup_barmap() ;)
Thanks,
Alex
> +
> int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
> {
> struct vfio_pci_core_device *vdev =
> @@ -1761,18 +1784,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
> * Even though we don't make use of the barmap for the mmap,
> * we need to request the region and the barmap tracks that.
> */
> - if (!vdev->barmap[index]) {
> - ret = pci_request_selected_regions(pdev,
> - 1 << index, "vfio-pci");
> - if (ret)
> - return ret;
> -
> - vdev->barmap[index] = pci_iomap(pdev, index, 0);
> - if (!vdev->barmap[index]) {
> - pci_release_selected_regions(pdev, 1 << index);
> - return -ENOMEM;
> - }
> - }
> + ret = vfio_pci_core_barmap(vdev, index);
> + if (ret)
> + return ret;
>
> vma->vm_private_data = vdev;
> vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
> diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
> index a097a66485b4..75f04d613e0c 100644
> --- a/include/linux/vfio_pci_core.h
> +++ b/include/linux/vfio_pci_core.h
> @@ -121,6 +121,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bu
> size_t count, loff_t *ppos);
> vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf, unsigned long pfn,
> unsigned int order);
> +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index);
> int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
> void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
> int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready
2025-11-21 14:11 ` [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready ankita
@ 2025-11-21 16:59 ` Alex Williamson
0 siblings, 0 replies; 12+ messages in thread
From: Alex Williamson @ 2025-11-21 16:59 UTC (permalink / raw)
To: ankita
Cc: jgg, yishaih, skolothumtho, kevin.tian, aniketa, vsethi, mochs,
Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
On Fri, 21 Nov 2025 14:11:41 +0000
<ankita@nvidia.com> wrote:
> From: Ankit Agrawal <ankita@nvidia.com>
>
> Speculative prefetches from CPU to GPU memory until the GPU is
> ready after reset can cause harmless corrected RAS events to
> be logged on Grace systems. It is thus preferred that the
> mapping not be re-established until the GPU is ready post reset.
>
> The GPU readiness can be checked through BAR0 registers similar
> to the checking at the time of device probe.
>
> It can take several seconds for the GPU to be ready. So it is
> desirable that the time overlaps as much of the VM startup as
> possible to reduce impact on the VM bootup time. The GPU
> readiness state is thus checked on the first fault/huge_fault
> request which amortizes the GPU readiness time. The first fault
> is checked using a flag. The flag is unset on every GPU reset
> request.
>
> Intercept the following calls to the GPU reset, unset gpu_mem_mapped.
> Then use it to determine whether to wait before mapping.
> 1. VFIO_DEVICE_RESET ioctl call
> 2. FLR through config space.
>
> cc: Alex Williamson <alex@shazbot.org>
> cc: Jason Gunthorpe <jgg@ziepe.ca>
> cc: Vikram Sethi <vsethi@nvidia.com>
> Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
> ---
> drivers/vfio/pci/nvgrace-gpu/main.c | 64 ++++++++++++++++++++++++++++-
> 1 file changed, 63 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
> index 7618c3f515cc..23e3278aba25 100644
> --- a/drivers/vfio/pci/nvgrace-gpu/main.c
> +++ b/drivers/vfio/pci/nvgrace-gpu/main.c
> @@ -58,6 +58,8 @@ struct nvgrace_gpu_pci_core_device {
> /* Lock to control device memory kernel mapping */
> struct mutex remap_lock;
> bool has_mig_hw_bug;
> + /* Any GPU memory mapped to the VMA */
> + bool gpu_mem_mapped;
> };
>
> static void nvgrace_gpu_init_fake_bar_emu_regs(struct vfio_device *core_vdev)
> @@ -102,9 +104,15 @@ static int nvgrace_gpu_open_device(struct vfio_device *core_vdev)
> mutex_init(&nvdev->remap_lock);
> }
>
> + nvdev->gpu_mem_mapped = false;
> +
> vfio_pci_core_finish_enable(vdev);
>
> - return 0;
> + /*
> + * The GPU readiness is determined through BAR0 register reads.
> + * Make sure the BAR0 is mapped before any such check occur.
> + */
> + return vfio_pci_core_barmap(vdev, 0);
I think the idea is that all variant specific open code happens between
vfio_pci_core_enable() and vfio_pci_core_finish_enable().
> }
>
> static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
> @@ -158,6 +166,21 @@ static vm_fault_t nvgrace_gpu_vfio_pci_huge_fault(struct vm_fault *vmf,
> struct mem_region *memregion;
> unsigned long pgoff, pfn, addr;
>
> + /*
> + * If the GPU memory is accessed by the CPU while the GPU is
> + * not ready after reset, it can cause harmless corrected RAS
> + * events to be logged. Make sure the GPU is ready before
> + * establishing the mappings.
> + */
> + if (!nvdev->gpu_mem_mapped) {
> + struct vfio_pci_core_device *vdev = &nvdev->core_device;
> +
> + if (nvgrace_gpu_wait_device_ready(vdev->barmap[0]))
> + return VM_FAULT_SIGBUS;
> +
> + nvdev->gpu_mem_mapped = true;
> + }
> +
> memregion = nvgrace_gpu_memregion(index, nvdev);
> if (!memregion)
> return ret;
> @@ -354,7 +377,17 @@ static long nvgrace_gpu_ioctl(struct vfio_device *core_vdev,
> case VFIO_DEVICE_IOEVENTFD:
> return -ENOTTY;
> case VFIO_DEVICE_RESET:
> + struct nvgrace_gpu_pci_core_device *nvdev =
> + container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
> + core_device.vdev);
> nvgrace_gpu_init_fake_bar_emu_regs(core_vdev);
> +
> + /*
> + * GPU memory is exposed as device BAR2 (region 4,5).
> + * This would be zapped during GPU reset. Unset
> + * nvdev->gpu_mem_mapped to reflect just that.
> + */
> + nvdev->gpu_mem_mapped = false;
Why aren't we using a reset_done callback for this, where we then might
name the flag "reset_done"? What protects this flag against races,
is it only valid under memory_lock? memory_lock is held across reset,
including .reset_done.
> fallthrough;
> default:
> return vfio_pci_core_ioctl(core_vdev, cmd, arg);
> @@ -439,11 +472,14 @@ nvgrace_gpu_write_config_emu(struct vfio_device *core_vdev,
> struct nvgrace_gpu_pci_core_device *nvdev =
> container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
> core_device.vdev);
> + struct vfio_pci_core_device *vdev =
> + container_of(core_vdev, struct vfio_pci_core_device, vdev);
> u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
> struct mem_region *memregion = NULL;
> size_t register_offset;
> loff_t copy_offset;
> size_t copy_count;
> + int cap_start = vfio_find_cap_start(vdev, pos);
>
> if (vfio_pci_core_range_intersect_range(pos, count, PCI_BASE_ADDRESS_2,
> sizeof(u64), ©_offset,
> @@ -462,6 +498,23 @@ nvgrace_gpu_write_config_emu(struct vfio_device *core_vdev,
> return copy_count;
> }
>
> + if (vfio_pci_core_range_intersect_range(pos, count, cap_start + PCI_EXP_DEVCTL,
> + sizeof(u16), ©_offset,
> + ©_count, ®ister_offset)) {
> + __le16 val16;
> +
> + if (copy_from_user((void *)&val16, buf, copy_count))
> + return -EFAULT;
> +
> + /*
> + * GPU memory is exposed as device BAR2 (region 4,5).
> + * This would be zapped during GPU reset. Unset
> + * nvdev->gpu_mem_mapped to reflect just that.
> + */
> + if (val16 & cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR))
> + nvdev->gpu_mem_mapped = false;
> + }
reset_done would significantly simplify this and avoid that we need to
export vfio_find_cap_start(). I think it might be done this way to set
the flag prior to reset, but as above, the flag seems racy as
implemented here.
> +
> return vfio_pci_core_write(core_vdev, buf, count, ppos);
> }
>
> @@ -478,9 +531,18 @@ static int
> nvgrace_gpu_map_device_mem(int index,
> struct nvgrace_gpu_pci_core_device *nvdev)
> {
> + struct vfio_pci_core_device *vdev = &nvdev->core_device;
> struct mem_region *memregion;
> int ret = 0;
>
> + if (!nvdev->gpu_mem_mapped) {
> + ret = nvgrace_gpu_wait_device_ready(vdev->barmap[0]);
> + if (ret)
> + return ret;
> +
> + nvdev->gpu_mem_mapped = true;
> + }
Similar to the code in the fault handler, should have a common wrapper?
Likely need to learn about memory_lock in this path to prevent races as
well. Thanks,
Alex
> +
> memregion = nvgrace_gpu_memregion(index, nvdev);
> if (!memregion)
> return -EINVAL;
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3 5/7] vfio: move barmap to a separate function and export
2025-11-21 16:39 ` Alex Williamson
@ 2025-11-21 20:58 ` Donald Dutile
2025-11-21 21:08 ` Alex Williamson
0 siblings, 1 reply; 12+ messages in thread
From: Donald Dutile @ 2025-11-21 20:58 UTC (permalink / raw)
To: Alex Williamson, ankita
Cc: jgg, yishaih, skolothumtho, kevin.tian, aniketa, vsethi, mochs,
Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas, peterx,
pstanner, apopple, kvm, linux-kernel, cjia, kwankhede, targupta,
zhiw, danw, dnigam, kjaju
On 11/21/25 11:39 AM, Alex Williamson wrote:
> On Fri, 21 Nov 2025 14:11:39 +0000
> <ankita@nvidia.com> wrote:
>
>> From: Ankit Agrawal <ankita@nvidia.com>
>>
>> Move the code to map the BAR to a separate function.
>>
>> This would be reused by the nvgrace-gpu module.
>>
>> Signed-off-by: Ankit Agrawal <ankita@nvidia.com>
>> ---
>> drivers/vfio/pci/vfio_pci_core.c | 38 ++++++++++++++++++++++----------
>> include/linux/vfio_pci_core.h | 1 +
>> 2 files changed, 27 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
>> index 29dcf78905a6..d1ff1c0aa727 100644
>> --- a/drivers/vfio/pci/vfio_pci_core.c
>> +++ b/drivers/vfio/pci/vfio_pci_core.c
>> @@ -1717,6 +1717,29 @@ static const struct vm_operations_struct vfio_pci_mmap_ops = {
>> #endif
>> };
>>
>> +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index)
>> +{
>> + struct pci_dev *pdev = vdev->pdev;
>> + int ret;
>> +
>> + if (vdev->barmap[index])
>> + return 0;
>> +
>> + ret = pci_request_selected_regions(pdev,
>> + 1 << index, "vfio-pci");
>> + if (ret)
>> + return ret;
>> +
>> + vdev->barmap[index] = pci_iomap(pdev, index, 0);
>> + if (!vdev->barmap[index]) {
>> + pci_release_selected_regions(pdev, 1 << index);
>> + return -ENOMEM;
>> + }
>> +
>> + return 0;
>> +}
>> +EXPORT_SYMBOL_GPL(vfio_pci_core_barmap);
>
> Looks a lot like vfio_pci_core_setup_barmap() ;)
>
> Thanks,
> Alex
>
+1 ... and below ...
>
>> +
>> int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
>> {
>> struct vfio_pci_core_device *vdev =
>> @@ -1761,18 +1784,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
>> * Even though we don't make use of the barmap for the mmap,
>> * we need to request the region and the barmap tracks that.
>> */
>> - if (!vdev->barmap[index]) {
>> - ret = pci_request_selected_regions(pdev,
>> - 1 << index, "vfio-pci");
>> - if (ret)
>> - return ret;
>> -
>> - vdev->barmap[index] = pci_iomap(pdev, index, 0);
>> - if (!vdev->barmap[index]) {
>> - pci_release_selected_regions(pdev, 1 << index);
>> - return -ENOMEM;
>> - }
>> - }
>> + ret = vfio_pci_core_barmap(vdev, index);
>> + if (ret)
>> + return ret;
>>
so, vfio_pci_core_mmap() should be calling vfio_pci_core_setup_barmap() vs. what it does above currently?
--Don
>> vma->vm_private_data = vdev;
>> vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
>> diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
>> index a097a66485b4..75f04d613e0c 100644
>> --- a/include/linux/vfio_pci_core.h
>> +++ b/include/linux/vfio_pci_core.h
>> @@ -121,6 +121,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bu
>> size_t count, loff_t *ppos);
>> vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf, unsigned long pfn,
>> unsigned int order);
>> +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index);
>> int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
>> void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
>> int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
>
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3 5/7] vfio: move barmap to a separate function and export
2025-11-21 20:58 ` Donald Dutile
@ 2025-11-21 21:08 ` Alex Williamson
0 siblings, 0 replies; 12+ messages in thread
From: Alex Williamson @ 2025-11-21 21:08 UTC (permalink / raw)
To: Donald Dutile
Cc: ankita, jgg, yishaih, skolothumtho, kevin.tian, aniketa, vsethi,
mochs, Yunxiang.Li, yi.l.liu, zhangdongdong, avihaih, bhelgaas,
peterx, pstanner, apopple, kvm, linux-kernel, cjia, kwankhede,
targupta, zhiw, danw, dnigam, kjaju
On Fri, 21 Nov 2025 15:58:19 -0500
Donald Dutile <ddutile@redhat.com> wrote:
> On 11/21/25 11:39 AM, Alex Williamson wrote:
> > On Fri, 21 Nov 2025 14:11:39 +0000
> > <ankita@nvidia.com> wrote:
> >> +
> >> int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
> >> {
> >> struct vfio_pci_core_device *vdev =
> >> @@ -1761,18 +1784,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
> >> * Even though we don't make use of the barmap for the mmap,
> >> * we need to request the region and the barmap tracks that.
> >> */
> >> - if (!vdev->barmap[index]) {
> >> - ret = pci_request_selected_regions(pdev,
> >> - 1 << index, "vfio-pci");
> >> - if (ret)
> >> - return ret;
> >> -
> >> - vdev->barmap[index] = pci_iomap(pdev, index, 0);
> >> - if (!vdev->barmap[index]) {
> >> - pci_release_selected_regions(pdev, 1 << index);
> >> - return -ENOMEM;
> >> - }
> >> - }
> >> + ret = vfio_pci_core_barmap(vdev, index);
> >> + if (ret)
> >> + return ret;
> >>
> so, vfio_pci_core_mmap() should be calling vfio_pci_core_setup_barmap() vs. what it does above currently?
Yeah, it probably should have happened in 8bccc5b80678 ("vfio/pci:
Expose vfio_pci_core_setup_barmap()") when the static function in
vfio_pci_rdwr.c was exported. Better late than never. Thanks,
Alex
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2025-11-21 21:08 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-21 14:11 [PATCH v3 0/7] vfio/nvgrace-gpu: Support huge PFNMAP and wait for GPU ready post reset ankita
2025-11-21 14:11 ` [PATCH v3 1/7] vfio/nvgrace-gpu: Use faults to map device memory ankita
2025-11-21 14:11 ` [PATCH v3 2/7] vfio: export function to map the VMA ankita
2025-11-21 14:11 ` [PATCH v3 3/7] vfio/nvgrace-gpu: Add support for huge pfnmap ankita
2025-11-21 14:11 ` [PATCH v3 4/7] vfio: export vfio_find_cap_start ankita
2025-11-21 14:11 ` [PATCH v3 5/7] vfio: move barmap to a separate function and export ankita
2025-11-21 16:39 ` Alex Williamson
2025-11-21 20:58 ` Donald Dutile
2025-11-21 21:08 ` Alex Williamson
2025-11-21 14:11 ` [PATCH v3 6/7] vfio/nvgrace-gpu: split the code to wait for GPU ready ankita
2025-11-21 14:11 ` [PATCH v3 7/7] vfio/nvgrace-gpu: wait for the GPU mem to be ready ankita
2025-11-21 16:59 ` Alex Williamson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®