Skip to contentSkip to conversation

cover-letter: Add virtualization support for EGM

RFC Superseded [email protected] 4 participants 28 messages Sep 4, 2025 – Sep 15, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover cover-letter: Add virtualization support for EGM

From: Ankit Agrawal <[email protected]>

Background
----------
Grace Hopper/Blackwell systems support the Extended GPU Memory (EGM) feature that enable the GPU to access the system memory allocations within and across nodes through high bandwidth path. This access path goes as: GPU <--> NVswitch <--> GPU <--> CPU. The GPU can utilize the system memory located on the same socket or from a different socket or even on a different node in a multi-node system [1]. This feature is being extended to virtualization.

Design Details
--------------
EGM when enabled in the virtualization stack, the host memory is partitioned into 2 parts: One partition for the Host OS usage called Hypervisor region, and a second Hypervisor-Invisible (HI) region for the VM. Only the hypervisor region is part of the host EFI map and is thus visible to the host OS on bootup. Since the entire VM sysmem is eligible for EGM allocations within the VM, the HI partition is interchangeably called as EGM region in the series. This HI/EGM region range base SPA and size is exposed through the ACPI DSDT properties.

Whilst the EGM region is accessible on the host, it is not added to the kernel. The HI region is assigned to a VM by mapping the QEMU VMA to the SPA using remap_pfn_range().

The following figure shows the memory map in the virtualization environment.

|---- Sysmem ----|                  |--- GPU mem ---|  VM Memory
|                |                  |               |
|IPA <-> SPA map |                  |IPA <-> SPA map|
|                |                  |               |
|--- HI / EGM ---|-- Host Mem --|   |--- GPU mem ---|  Host Memory

The patch series introduce a new nvgrace-egm auxiliary driver module to manage and map the HI/EGM region in the Grace Blackwell systems. This binds to the auxiliary device created by the parent nvgrace-gpu (in-tree module for device assignment) / nvidia-vgpu-vfio (out-of-tree open source module for SRIOV vGPU) to manage the EGM region for the VM. Note that there is a unique EGM region per socket and the auxiliary device gets created for every region. The parent module fetches the EGM region information from the ACPI tables and populate to the data structures shared with the auxiliary nvgrace-egm module.

nvgrace-egm module handles the following:
1. Fetch the EGM memory properties (base HPA, length, proximity domain) from the parent device shared EGM region structure.
2. Create a char device that can be used as memory-backend-file by Qemu for the VM and implement file operations. The char device is /dev/egmX, where X is the PXM node ID of the EGM being mapped fetched in 1.
3. Zero the EGM memory on first device open().
4. Map the QEMU VMA to the EGM region using remap_pfn_range.
5. Cleaning up state and destroying the chardev on device unbind.
6. Handle presence of retired ECC pages on the EGM region.

Since nvgrace-egm is an auxiliary module to the nvgrace-gpu, it is kept in the same directory.

Implementation
--------------
Patch 1-4 makes changes to the nvgrace-gpu module to fetch the EGM information, create auxiliary device and save the EGM region information in the shared structures.
Path 5-10 introduce the new nvgrace-egm module to manage the EGM region. The module implements a char device to expose the EGM to usermode apps such as QEMU. The module does the mapping of the QEMU VMA to the EGM SPA using remap_pfn range.
Patch 11-12 fetches the list of pages on EGM with known ECC errors. Patch 13-14 expose the EGM topology and size through sysfs.

Enablement
----------
The EGM mode is enabled through a flag in the SBIOS. The size of the Hypervisor region is modifiable through a second parameter in the SBIOS. All the remaining system memory on the host will be invisible to the Hypervisor.

Verification
------------
Applied over v6.17-rc4 and using qemu repository [3]. Tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [2] and running nvidia-smi in the VM to check for the presence of EGM capability.

To run CUDA workloads, there is a dependency on the Nested Page Table patches being worked on separately by Shameer Kolothum ([email protected]).

Recognitions
------------
Many thanks to Jason Gunthorpe, Vikram Sethi, Aniket Agashe for design suggestions and Matt Ochs, Andy Currid, Neo Jia, Kirti Wankhede among others for the review feedbacks.

Links
-----

Ankit Agrawal (14):
vfio/nvgrace-gpu: Expand module_pci_driver to allow custom module init vfio/nvgrace-gpu: Create auxiliary device for EGM vfio/nvgrace-gpu: track GPUs associated with the EGM regions vfio/nvgrace-gpu: Introduce functions to fetch and save EGM info vfio/nvgrace-egm: Introduce module to manage EGM vfio/nvgrace-egm: Introduce egm class and register char device numbers vfio/nvgrace-egm: Register auxiliary driver ops vfio/nvgrace-egm: Expose EGM region as char device vfio/nvgrace-egm: Add chardev ops for EGM management vfio/nvgrace-egm: Clear Memory before handing out to VM vfio/nvgrace-egm: Fetch EGM region retired pages list vfio/nvgrace-egm: Introduce ioctl to share retired pages vfio/nvgrace-egm: expose the egm size through sysfs vfio/nvgrace-gpu: Add link from pci to EGM

 MAINTAINERS                            |  12 +-
 drivers/vfio/pci/nvgrace-gpu/Kconfig   |  11 +
 drivers/vfio/pci/nvgrace-gpu/Makefile  |   5 +-
 drivers/vfio/pci/nvgrace-gpu/egm.c     | 418 +++++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 174 ++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  24 ++
 drivers/vfio/pci/nvgrace-gpu/main.c    | 117 ++++++-
 include/linux/nvgrace-egm.h            |  33 ++
 include/uapi/linux/egm.h               |  26 ++
 9 files changed, 816 insertions(+), 4 deletions(-)
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm.c
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.c
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.h
 create mode 100644 include/linux/nvgrace-egm.h
 create mode 100644 include/uapi/linux/egm.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

RFC 1/14 vfio/nvgrace-gpu: Expand module_pci_driver to allow custom module init

From: Ankit Agrawal <[email protected]>

Allow custom changes to the nvgrace-gpu module init functions by expanding definition of module_pci_driver.

---
 drivers/vfio/pci/nvgrace-gpu/main.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/vfio/pci/nvgrace-gpu/main.c +11 -1
index d95761dcdd58..72e7ac1fa309 100644
@@ -1009,7 +1009,17 @@ static struct pci_driver nvgrace_gpu_vfio_pci_driver = {
 	.driver_managed_dma = true,
 };
 
-module_pci_driver(nvgrace_gpu_vfio_pci_driver);
+static int __init nvgrace_gpu_vfio_pci_init(void)
+{
+	return pci_register_driver(&nvgrace_gpu_vfio_pci_driver);
+}
+module_init(nvgrace_gpu_vfio_pci_init);
+
+static void __exit nvgrace_gpu_vfio_pci_cleanup(void)
+{
+	pci_unregister_driver(&nvgrace_gpu_vfio_pci_driver);
+}
+module_exit(nvgrace_gpu_vfio_pci_cleanup);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

RFC 2/14 vfio/nvgrace-gpu: Create auxiliary device for EGM

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The Extended GPU Memory (EGM) feature enables the GPU access to the system memory across sockets and physical systems on the Grace Hopper and Grace Blackwell systems. When the feature is enabled through SBIOS, part of the system memory is made available to the GPU for access through EGM path.

The EGM functionality is separate and largely independent from the core GPU device functionality. However, the EGM region information of base SPA and size is associated with the GPU on the ACPI tables. An architecture wih EGM represented as an auxiliary device suits well in this context.

The parent GPU device creates an EGM auxiliary device to be managed independently by an auxiliary EGM driver. The EGM region information is kept as part of the shared struct nvgrace_egm_dev along with the auxiliary device handle.

Each socket has a separate EGM region and hence a multi-socket system have multiple EGM regions. Each EGM region has a separate nvgrace_egm_dev and the nvgrace-gpu keeps the EGM regions as part of a list.

Note that EGM is an optional feature enabled through SBIOS. The EGM properties are only populated in ACPI tables if the feature is enabled; they are absent otherwise. The absence of the properties is thus not considered fatal. The presence of improper set of values however are considered fatal.

It is also noteworthy that there may also be multiple GPUs present per socket and have duplicate EGM region information with them. Make sure the duplicate data does not get added.

---
 MAINTAINERS                            |  5 +-
 drivers/vfio/pci/nvgrace-gpu/Makefile  |  2 +-
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 61 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h | 17 +++++++
 drivers/vfio/pci/nvgrace-gpu/main.c    | 70 +++++++++++++++++++++++++-
 include/linux/nvgrace-egm.h            | 23 +++++++++
 6 files changed, 175 insertions(+), 3 deletions(-)
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.c
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.h
 create mode 100644 include/linux/nvgrace-egm.h
MAINTAINERS +4 -1
index 6dcfbd11efef..dd7df834b70b 100644
@@ -26471,7 +26471,10 @@ VFIO NVIDIA GRACE GPU DRIVER
 M:	Ankit Agrawal <[email protected]>
 L:	[email protected]
 S:	Supported
-F:	drivers/vfio/pci/nvgrace-gpu/
+F:	drivers/vfio/pci/nvgrace-gpu/egm_dev.c
+F:	drivers/vfio/pci/nvgrace-gpu/egm_dev.h
+F:	drivers/vfio/pci/nvgrace-gpu/main.c
+F:	include/linux/nvgrace-egm.h
 
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
drivers/vfio/pci/nvgrace-gpu/Makefile +1 -1
index 3ca8c187897a..e72cc6739ef8 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu-vfio-pci.o
-nvgrace-gpu-vfio-pci-y := main.o
+nvgrace-gpu-vfio-pci-y := main.o egm_dev.o
drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +61 -0

61 lines not on this page · raw message

drivers/vfio/pci/nvgrace-gpu/egm_dev.h new file +17 -0
new file mode 100644
index 000000000000..c00f5288f4e7
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#ifndef EGM_DEV_H
+#define EGM_DEV_H
+
+#include <linux/nvgrace-egm.h>
+
+int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm);
+
+struct nvgrace_egm_dev *
+nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
+			      u64 egmphys);
+
+#endif /* EGM_DEV_H */
drivers/vfio/pci/nvgrace-gpu/main.c +69 -1

107 lines not on this page · raw message

include/linux/nvgrace-egm.h new file +23 -0
new file mode 100644
index 000000000000..9575d4ad4338
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#ifndef NVGRACE_EGM_H
+#define NVGRACE_EGM_H
+
+#include <linux/auxiliary_bus.h>
+
+#define NVGRACE_EGM_DEV_NAME "egm"
+
+struct nvgrace_egm_dev {
+	struct auxiliary_device aux_dev;
+	u64 egmpxm;
+};
+
+struct nvgrace_egm_dev_entry {
+	struct list_head list;
+	struct nvgrace_egm_dev *egm_dev;
+};
+
+#endif /* NVGRACE_EGM_H */
Signature · 1 line
2.34.1
#3 lore raw

RE: [RFC 02/14] vfio/nvgrace-gpu: Create auxiliary device for EGM

Hi Ankit,

Quoted 125 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 02/14] vfio/nvgrace-gpu: Create auxiliary device for EGM

From: Ankit Agrawal <[email protected]>

The Extended GPU Memory (EGM) feature enables the GPU access to the system memory across sockets and physical systems on the Grace Hopper and Grace Blackwell systems. When the feature is enabled through SBIOS, part of the system memory is made available to the GPU for access through EGM path.

The EGM functionality is separate and largely independent from the core GPU device functionality. However, the EGM region information of base SPA and size is associated with the GPU on the ACPI tables. An architecture wih EGM represented as an auxiliary device suits well in this context.

The parent GPU device creates an EGM auxiliary device to be managed independently by an auxiliary EGM driver. The EGM region information is kept as part of the shared struct nvgrace_egm_dev along with the auxiliary device handle.

Each socket has a separate EGM region and hence a multi-socket system have multiple EGM regions. Each EGM region has a separate nvgrace_egm_dev
and the nvgrace-gpu keeps the EGM regions as part of a list.

Note that EGM is an optional feature enabled through SBIOS. The EGM properties are only populated in ACPI tables if the feature is enabled; they are absent otherwise. The absence of the properties is thus not considered fatal. The presence of improper set of values however are considered fatal.

It is also noteworthy that there may also be multiple GPUs present per socket and have duplicate EGM region information with them. Make sure the duplicate data does not get added.

---
 MAINTAINERS                            |  5 +-
 drivers/vfio/pci/nvgrace-gpu/Makefile  |  2 +-
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 61 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h | 17 +++++++
 drivers/vfio/pci/nvgrace-gpu/main.c    | 70 +++++++++++++++++++++++++-
 include/linux/nvgrace-egm.h            | 23 +++++++++
 6 files changed, 175 insertions(+), 3 deletions(-)
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.c
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm_dev.h
 create mode 100644 include/linux/nvgrace-egm.h
MAINTAINERS +4 -1
index 6dcfbd11efef..dd7df834b70b 100644
@@ -26471,7 +26471,10 @@ VFIO NVIDIA GRACE GPU DRIVER
 M:	Ankit Agrawal <[email protected]>
 L:	[email protected]
 S:	Supported
-F:	drivers/vfio/pci/nvgrace-gpu/
+F:	drivers/vfio/pci/nvgrace-gpu/egm_dev.c
+F:	drivers/vfio/pci/nvgrace-gpu/egm_dev.h
+F:	drivers/vfio/pci/nvgrace-gpu/main.c
+F:	include/linux/nvgrace-egm.h
 
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/Makefile +0 -0

gpu/Makefile
index 3ca8c187897a..e72cc6739ef8 100644

drivers/vfio/pci/nvgrace-gpu/Makefile +1 -1
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu-vfio-pci.o
-nvgrace-gpu-vfio-pci-y := main.o
+nvgrace-gpu-vfio-pci-y := main.o egm_dev.o
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/egm_dev.c +0 -0

gpu/egm_dev.c
new file mode 100644
index 000000000000..f4e27dadf1ef

drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +22 -0
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include <linux/vfio_pci_core.h>
+#include "egm_dev.h"
+
+/*
+ * Determine if the EGM feature is enabled. If disabled, there
+ * will be no EGM properties populated in the ACPI tables and this
+ * fetch would fail.
+ */
+int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm)
+{
+	return device_property_read_u64(&pdev->dev, "nvidia,egm-pxm",
+					pegmpxm);
+}
+
+static void nvgrace_gpu_release_aux_device(struct device *device)
+{
+	struct auxiliary_device *aux_dev = container_of(device, struct

auxiliary_device, dev);

drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +1 -0
+	struct nvgrace_egm_dev *egm_dev = container_of(aux_dev, struct

nvgrace_egm_dev, aux_dev);

drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +2 -0
+
+	kvfree(egm_dev);

drivers/vfio/pci/nvgrace-gpu/egm_dev.c line 35

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +10 -0
+}
+
+struct nvgrace_egm_dev *
+nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
+			      u64 egmpxm)
+{
+	struct nvgrace_egm_dev *egm_dev;
+	int ret;
+
+	egm_dev = kvzalloc(sizeof(*egm_dev), GFP_KERNEL);

Do you really need this kvzalloc variant here? Looks like kzalloc will do.

Quoted 7 lines
drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +7 -0
+	if (!egm_dev)
+		goto create_err;
+
+	egm_dev->egmpxm = egmpxm;
+	egm_dev->aux_dev.id = egmpxm;
+	egm_dev->aux_dev.name = name;
+	egm_dev->aux_dev.dev.release = nvgrace_gpu_release_aux_device;

drivers/vfio/pci/nvgrace-gpu/egm_dev.c line 52

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +10 -0
+	egm_dev->aux_dev.dev.parent = &pdev->dev;
+
+	ret = auxiliary_device_init(&egm_dev->aux_dev);
+	if (ret)
+		goto free_dev;
+
+	ret = auxiliary_device_add(&egm_dev->aux_dev);
+	if (ret) {
+		auxiliary_device_uninit(&egm_dev->aux_dev);
+		goto create_err;

Should be free_dev to free the mem allocated.

Quoted 48 lines
drivers/vfio/pci/nvgrace-gpu/egm_dev.c new file +9 -0
+	}
+
+	return egm_dev;
+
+free_dev:
+	kvfree(egm_dev);
+create_err:
+	return NULL;
+}
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/egm_dev.h +0 -0

gpu/egm_dev.h
new file mode 100644
index 000000000000..c00f5288f4e7

drivers/vfio/pci/nvgrace-gpu/egm_dev.h new file +17 -0
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#ifndef EGM_DEV_H
+#define EGM_DEV_H
+
+#include <linux/nvgrace-egm.h>
+
+int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm);
+
+struct nvgrace_egm_dev *
+nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
+			      u64 egmphys);
+
+#endif /* EGM_DEV_H */
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/main.c +0 -0

gpu/main.c
index 72e7ac1fa309..2cf851492990 100644

drivers/vfio/pci/nvgrace-gpu/main.c +2 -0
@@ -7,6 +7,8 @@
 #include <linux/vfio_pci_core.h>
 #include <linux/delay.h>
 #include <linux/jiffies.h>
+#include <linux/nvgrace-egm.h>
+#include "egm_dev.h"
 
 /*
  * The device memory usable to the workloads running in the VM is cached

drivers/vfio/pci/nvgrace-gpu/main.c struct nvgrace_gpu_pci_core_device { · line 65

Quoted 4 lines
drivers/vfio/pci/nvgrace-gpu/main.c +1 -0
@@ -60,6 +62,63 @@ struct nvgrace_gpu_pci_core_device {
 	bool has_mig_hw_bug;
 };
 
+static struct list_head egm_dev_list;

Its not clear to me why is this list a global? Is the egm not per device? May be a comment to explain this will be useful. Also do you need a lock to protect it?

Quoted 7 lines
drivers/vfio/pci/nvgrace-gpu/main.c +7 -0
+
+static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
+{
+	struct nvgrace_egm_dev_entry *egm_entry;
+	u64 egmpxm;
+	int ret = 0;
+

drivers/vfio/pci/nvgrace-gpu/main.c struct nvgrace_gpu_pci_core_device { · line 82

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +10 -0
+	/*
+	 * EGM is an optional feature enabled in SBIOS. If disabled, there
+	 * will be no EGM properties populated in the ACPI tables and this
+	 * fetch would fail. Treat this failure as non-fatal and return
+	 * early.
+	 */
+	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
+		goto exit;
+
+	egm_entry = kvzalloc(sizeof(*egm_entry), GFP_KERNEL);

Kzalloc() good enough?

Thanks,
Shameer

Quoted 119 lines
drivers/vfio/pci/nvgrace-gpu/main.c +39 -0
+	if (!egm_entry)
+		return -ENOMEM;
+
+	egm_entry->egm_dev =
+		nvgrace_gpu_create_aux_device(pdev,
 NVGRACE_EGM_DEV_NAME,
+					      egmpxm);
+	if (!egm_entry->egm_dev) {
+		kvfree(egm_entry);
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	list_add_tail(&egm_entry->list, &egm_dev_list);
+
+exit:
+	return ret;
+}
+
+static void nvgrace_gpu_destroy_egm_aux_device(struct pci_dev *pdev)
+{
+	struct nvgrace_egm_dev_entry *egm_entry, *temp_egm_entry;
+	u64 egmpxm;
+
+	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
+		return;
+
+	list_for_each_entry_safe(egm_entry, temp_egm_entry,
 &egm_dev_list, list) {
+		/*
+		 * Free the EGM region corresponding to the input GPU
+		 * device.
+		 */
+		if (egm_entry->egm_dev->egmpxm == egmpxm) {
+			auxiliary_device_destroy(&egm_entry->egm_dev-
 >aux_dev);
+			list_del(&egm_entry->list);
+			kvfree(egm_entry);
+		}
+	}
+}
+

static void nvgrace_gpu_init_fake_bar_emu_regs(struct vfio_device
*core_vdev)
{

drivers/vfio/pci/nvgrace-gpu/main.c +8 -1
 	struct nvgrace_gpu_pci_core_device *nvdev =
@@ -965,14 +1024,20 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,
 						    memphys, memlength);
 		if (ret)
 			goto out_put_vdev;
+
+		ret = nvgrace_gpu_create_egm_aux_device(pdev);
+		if (ret)
+			goto out_put_vdev;
 	}
 
 	ret = vfio_pci_core_register_device(&nvdev->core_device);
 	if (ret)
-		goto out_put_vdev;
+		goto out_reg;
 
 	return ret;
 
+out_reg:
+	nvgrace_gpu_destroy_egm_aux_device(pdev);
 out_put_vdev:
 	vfio_put_device(&nvdev->core_device.vdev);
 	return ret;
@@ -982,6 +1047,7 @@ static void nvgrace_gpu_remove(struct pci_dev
 *pdev)
 {
 	struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev-
 >dev);
 
+	nvgrace_gpu_destroy_egm_aux_device(pdev);
 	vfio_pci_core_unregister_device(core_device);
drivers/vfio/pci/nvgrace-gpu/main.c +2 -0
 	vfio_put_device(&core_device->vdev);
 }
@@ -1011,6 +1077,8 @@ static struct pci_driver nvgrace_gpu_vfio_pci_driver
 = {
 
 static int __init nvgrace_gpu_vfio_pci_init(void)
 {
+	INIT_LIST_HEAD(&egm_dev_list);
+
 	return pci_register_driver(&nvgrace_gpu_vfio_pci_driver);
 }

module_init(nvgrace_gpu_vfio_pci_init);

include/linux/nvgrace-egm.h new file +23 -1
new file mode 100644
index 000000000000..9575d4ad4338
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#ifndef NVGRACE_EGM_H
+#define NVGRACE_EGM_H
+
+#include <linux/auxiliary_bus.h>
+
+#define NVGRACE_EGM_DEV_NAME "egm"
+
+struct nvgrace_egm_dev {
+	struct auxiliary_device aux_dev;
+	u64 egmpxm;
+};
+
+struct nvgrace_egm_dev_entry {
+	struct list_head list;
+	struct nvgrace_egm_dev *egm_dev;
+};
+
+#endif /* NVGRACE_EGM_H */
--

2.34.1

author maintainer
#4 lore raw

RFC 3/14 vfio/nvgrace-gpu: track GPUs associated with the EGM regions

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Grace Blackwell systems could have multiple GPUs on a socket and thus are associated with the corresponding EGM region for that socket. Track the GPUs as a list.

On the device probe, the device pci_dev struct is added to a linked list of the appropriate EGM region.

Similarly on device remove, the pci_dev struct for the GPU is removed from the EGM region.

Since the GPUs on a socket have the same EGM region, they have the have the same set of EGM region information. Skip the EGM region information fetch if already done through a differnt GPU on the same socket.

---
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 29 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  4 +++
 drivers/vfio/pci/nvgrace-gpu/main.c    | 34 +++++++++++++++++++++++---
 include/linux/nvgrace-egm.h            |  6 +++++
 4 files changed, 70 insertions(+), 3 deletions(-)
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +29 -0
index f4e27dadf1ef..28cfd29eda56 100644
@@ -17,6 +17,33 @@ int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm)
 					pegmpxm);
 }
 
+int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
+{
+	struct gpu_node *node;
+
+	node = kvzalloc(sizeof(*node), GFP_KERNEL);
+	if (!node)
+		return -ENOMEM;
+
+	node->pdev = pdev;
+
+	list_add_tail(&node->list, &egm_dev->gpus);
+
+	return 0;
+}
+
+void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
+{
+	struct gpu_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &egm_dev->gpus, list) {
+		if (node->pdev == pdev) {
+			list_del(&node->list);
+			kvfree(node);
+		}
+	}
+}
+
 static void nvgrace_gpu_release_aux_device(struct device *device)
 {
 	struct auxiliary_device *aux_dev = container_of(device, struct auxiliary_device, dev);
@@ -37,6 +64,8 @@ nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
 		goto create_err;
 
 	egm_dev->egmpxm = egmpxm;
+	INIT_LIST_HEAD(&egm_dev->gpus);
+
 	egm_dev->aux_dev.id = egmpxm;
 	egm_dev->aux_dev.name = name;
 	egm_dev->aux_dev.dev.release = nvgrace_gpu_release_aux_device;
drivers/vfio/pci/nvgrace-gpu/egm_dev.h +4 -0
index c00f5288f4e7..1635753c9e50 100644
@@ -10,6 +10,10 @@
 
 int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm);
 
+int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
+
+void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
+
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
 			      u64 egmphys);
drivers/vfio/pci/nvgrace-gpu/main.c +31 -3
index 2cf851492990..436f0ac17332 100644
@@ -66,9 +66,10 @@ static struct list_head egm_dev_list;
 
 static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 {
-	struct nvgrace_egm_dev_entry *egm_entry;
+	struct nvgrace_egm_dev_entry *egm_entry = NULL;
 	u64 egmpxm;
 	int ret = 0;
+	bool is_new_region = false;
 
 	/*
 	 * EGM is an optional feature enabled in SBIOS. If disabled, there
@@ -79,6 +80,19 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
 		goto exit;
 
+	list_for_each_entry(egm_entry, &egm_dev_list, list) {
+		/*
+		 * A system could have multiple GPUs associated with an
+		 * EGM region and will have the same set of EGM region
+		 * information. Skip the EGM region information fetch if
+		 * already done through a differnt GPU on the same socket.
+		 */
+		if (egm_entry->egm_dev->egmpxm == egmpxm)
+			goto add_gpu;
+	}
+
+	is_new_region = true;
+
 	egm_entry = kvzalloc(sizeof(*egm_entry), GFP_KERNEL);
 	if (!egm_entry)
 		return -ENOMEM;
@@ -87,13 +101,23 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 		nvgrace_gpu_create_aux_device(pdev, NVGRACE_EGM_DEV_NAME,
 					      egmpxm);
 	if (!egm_entry->egm_dev) {
-		kvfree(egm_entry);
 		ret = -EINVAL;
+		goto free_egm_entry;
+	}
+
+add_gpu:
+	ret = add_gpu(egm_entry->egm_dev, pdev);
+	if (!ret) {
+		if (is_new_region)
+			list_add_tail(&egm_entry->list, &egm_dev_list);
 		goto exit;
 	}
 
-	list_add_tail(&egm_entry->list, &egm_dev_list);
+	if (is_new_region)
+		auxiliary_device_destroy(&egm_entry->egm_dev->aux_dev);
 
+free_egm_entry:
+	kvfree(egm_entry);
 exit:
 	return ret;
 }
@@ -112,6 +136,10 @@ static void nvgrace_gpu_destroy_egm_aux_device(struct pci_dev *pdev)
 		 * device.
 		 */
 		if (egm_entry->egm_dev->egmpxm == egmpxm) {
+			remove_gpu(egm_entry->egm_dev, pdev);
+			if (!list_empty(&egm_entry->egm_dev->gpus))
+				break;
+
 			auxiliary_device_destroy(&egm_entry->egm_dev->aux_dev);
 			list_del(&egm_entry->list);
 			kvfree(egm_entry);
include/linux/nvgrace-egm.h +6 -0
index 9575d4ad4338..e42494a2b1a6 100644
@@ -10,9 +10,15 @@
 
 #define NVGRACE_EGM_DEV_NAME "egm"
 
+struct gpu_node {
+	struct list_head list;
+	struct pci_dev *pdev;
+};
+
 struct nvgrace_egm_dev {
 	struct auxiliary_device aux_dev;
 	u64 egmpxm;
+	struct list_head gpus;
 };
 
 struct nvgrace_egm_dev_entry {
Signature · 1 line
2.34.1
#5 lore raw

RE: [RFC 03/14] vfio/nvgrace-gpu: track GPUs associated with the EGM regions

Quoted 29 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 03/14] vfio/nvgrace-gpu: track GPUs associated with the EGM regions

From: Ankit Agrawal <[email protected]>

Grace Blackwell systems could have multiple GPUs on a socket and thus are associated with the corresponding EGM region for that socket. Track the GPUs as a list.

On the device probe, the device pci_dev struct is added to a linked list of the appropriate EGM region.

Quoted 7 lines

Similarly on device remove, the pci_dev struct for the GPU is removed from the EGM region.

Since the GPUs on a socket have the same EGM region, they have the have the same set of EGM region information. Skip the EGM region information fetch if already done through a differnt GPU on the same socket.

Ok. This is probably why you are keeping egm_dev_list as global.

Quoted 126 lines
---
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 29 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  4 +++
 drivers/vfio/pci/nvgrace-gpu/main.c    | 34 +++++++++++++++++++++++---
 include/linux/nvgrace-egm.h            |  6 +++++
 4 files changed, 70 insertions(+), 3 deletions(-)
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +27 -0
index f4e27dadf1ef..28cfd29eda56 100644
@@ -17,6 +17,33 @@ int nvgrace_gpu_has_egm_property(struct pci_dev
 *pdev, u64 *pegmpxm)
 					pegmpxm);
 }
 
+int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
+{
+	struct gpu_node *node;
+
+	node = kvzalloc(sizeof(*node), GFP_KERNEL);
+	if (!node)
+		return -ENOMEM;
+
+	node->pdev = pdev;
+
+	list_add_tail(&node->list, &egm_dev->gpus);
+
+	return 0;
+}
+
+void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
+{
+	struct gpu_node *node, *tmp;
+
+	list_for_each_entry_safe(node, tmp, &egm_dev->gpus, list) {
+		if (node->pdev == pdev) {
+			list_del(&node->list);
+			kvfree(node);
+		}
+	}
+}
+
 static void nvgrace_gpu_release_aux_device(struct device *device)
 {

struct auxiliary_device *aux_dev = container_of(device, struct
auxiliary_device, dev);

drivers/vfio/pci/nvgrace-gpu/egm_dev.c +2 -0
@@ -37,6 +64,8 @@ nvgrace_gpu_create_aux_device(struct pci_dev *pdev,
 const char *name,
 		goto create_err;
 
 	egm_dev->egmpxm = egmpxm;
+	INIT_LIST_HEAD(&egm_dev->gpus);
+
 	egm_dev->aux_dev.id = egmpxm;
 	egm_dev->aux_dev.name = name;
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +0 -0
 	egm_dev->aux_dev.dev.release = nvgrace_gpu_release_aux_device;
drivers/vfio/pci/nvgrace-gpu/egm_dev.h +4 -0
index c00f5288f4e7..1635753c9e50 100644
@@ -10,6 +10,10 @@
 
 int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm);
 
+int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
+
+void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
+
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
 			      u64 egmphys);
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/main.c +0 -0

gpu/main.c
index 2cf851492990..436f0ac17332 100644

drivers/vfio/pci/nvgrace-gpu/main.c +15 -1
@@ -66,9 +66,10 @@ static struct list_head egm_dev_list;
 
 static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 {
-	struct nvgrace_egm_dev_entry *egm_entry;
+	struct nvgrace_egm_dev_entry *egm_entry = NULL;
 	u64 egmpxm;
 	int ret = 0;
+	bool is_new_region = false;
 
 	/*
 	 * EGM is an optional feature enabled in SBIOS. If disabled, there
@@ -79,6 +80,19 @@ static int nvgrace_gpu_create_egm_aux_device(struct
 pci_dev *pdev)
 	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
 		goto exit;
 
+	list_for_each_entry(egm_entry, &egm_dev_list, list) {
+		/*
+		 * A system could have multiple GPUs associated with an
+		 * EGM region and will have the same set of EGM region
+		 * information. Skip the EGM region information fetch if
+		 * already done through a differnt GPU on the same socket.
+		 */
+		if (egm_entry->egm_dev->egmpxm == egmpxm)
+			goto add_gpu;
+	}
+
+	is_new_region = true;
+
 	egm_entry = kvzalloc(sizeof(*egm_entry), GFP_KERNEL);
 	if (!egm_entry)
drivers/vfio/pci/nvgrace-gpu/main.c +4 -1
 		return -ENOMEM;
@@ -87,13 +101,23 @@ static int
 nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 		nvgrace_gpu_create_aux_device(pdev,
 NVGRACE_EGM_DEV_NAME,
 					      egmpxm);
 	if (!egm_entry->egm_dev) {
-		kvfree(egm_entry);
 		ret = -EINVAL;
+		goto free_egm_entry;
+	}
+
+add_gpu:

drivers/vfio/pci/nvgrace-gpu/main.c static int · line 119

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +6 -1
+	ret = add_gpu(egm_entry->egm_dev, pdev);
+	if (!ret) {
+		if (is_new_region)
+			list_add_tail(&egm_entry->list, &egm_dev_list);
 		goto exit;
 	}
 
-	list_add_tail(&egm_entry->list, &egm_dev_list);
+	if (is_new_region)
+		auxiliary_device_destroy(&egm_entry->egm_dev->aux_dev);

Maybe it is easier to read if you flip the ret check above. Something like below,

add_gpu:

        ret = add_gpu(egm_entry->egm_dev, pdev);
        if (ret) {
                goto free_dev;
        }

        if (is_new_region)
                list_add_tail(&egm_entry->list, &egm_dev_list);
        return 0;

free_dev:

        if (is_new_region)
                auxiliary_device_destroy(&egm_entry->egm_dev->aux_dev);

....

Thanks,
Shameer

Quoted 41 lines
drivers/vfio/pci/nvgrace-gpu/main.c +2 -0
+free_egm_entry:
+	kvfree(egm_entry);
 exit:
 	return ret;

}

drivers/vfio/pci/nvgrace-gpu/main.c +4 -0
@@ -112,6 +136,10 @@ static void
 nvgrace_gpu_destroy_egm_aux_device(struct pci_dev *pdev)
 		 * device.
 		 */
 		if (egm_entry->egm_dev->egmpxm == egmpxm) {
+			remove_gpu(egm_entry->egm_dev, pdev);
+			if (!list_empty(&egm_entry->egm_dev->gpus))
+				break;
+
 			auxiliary_device_destroy(&egm_entry->egm_dev-
 >aux_dev);
drivers/vfio/pci/nvgrace-gpu/main.c +0 -0
 			list_del(&egm_entry->list);
 			kvfree(egm_entry);
include/linux/nvgrace-egm.h +6 -1
index 9575d4ad4338..e42494a2b1a6 100644
@@ -10,9 +10,15 @@
 
 #define NVGRACE_EGM_DEV_NAME "egm"
 
+struct gpu_node {
+	struct list_head list;
+	struct pci_dev *pdev;
+};
+
 struct nvgrace_egm_dev {
 	struct auxiliary_device aux_dev;
 	u64 egmpxm;
+	struct list_head gpus;
 };
 
 struct nvgrace_egm_dev_entry {
--

2.34.1

author maintainer
#6 lore raw

RFC 4/14 vfio/nvgrace-gpu: Introduce functions to fetch and save EGM info

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The nvgrace-gpu module tracks the various EGM regions on the system. The EGM region information - Base SPA and size - are part of the ACPI tables. This can be fetched from the DSD table using the GPU handle.

When the GPUs are bound to the nvgrace-gpu module, it fetches the EGM region information from the ACPI table using the GPU's pci_dev. The EGM regions are tracked in a list and the information per region is maintained in the nvgrace_egm_dev.

---
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 24 +++++++++++++++++++++++-
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  4 +++-
 drivers/vfio/pci/nvgrace-gpu/main.c    |  8 ++++++--
 include/linux/nvgrace-egm.h            |  2 ++
 4 files changed, 34 insertions(+), 4 deletions(-)
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +23 -1
index 28cfd29eda56..ca50bc1f67a0 100644
@@ -17,6 +17,26 @@ int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm)
 					pegmpxm);
 }
 
+int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
+				   u64 *pegmlength)
+{
+	int ret;
+
+	/*
+	 * The memory information is present in the system ACPI tables as DSD
+	 * properties nvidia,egm-base-pa and nvidia,egm-size.
+	 */
+	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-size",
+				       pegmlength);
+	if (ret)
+		return ret;
+
+	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-base-pa",
+				       pegmphys);
+
+	return ret;
+}
+
 int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
 {
 	struct gpu_node *node;
@@ -54,7 +74,7 @@ static void nvgrace_gpu_release_aux_device(struct device *device)
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmpxm)
+			      u64 egmphys, u64 egmlength, u64 egmpxm)
 {
 	struct nvgrace_egm_dev *egm_dev;
 	int ret;
@@ -64,6 +84,8 @@ nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
 		goto create_err;
 
 	egm_dev->egmpxm = egmpxm;
+	egm_dev->egmphys = egmphys;
+	egm_dev->egmlength = egmlength;
 	INIT_LIST_HEAD(&egm_dev->gpus);
 
 	egm_dev->aux_dev.id = egmpxm;
drivers/vfio/pci/nvgrace-gpu/egm_dev.h +3 -1
index 1635753c9e50..2e1612445898 100644
@@ -16,6 +16,8 @@ void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmphys);
+			      u64 egmphys, u64 egmlength, u64 egmpxm);
 
+int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
+				   u64 *pegmlength);
 #endif /* EGM_DEV_H */
drivers/vfio/pci/nvgrace-gpu/main.c +6 -2
index 436f0ac17332..7486a1b49275 100644
@@ -67,7 +67,7 @@ static struct list_head egm_dev_list;
 static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 {
 	struct nvgrace_egm_dev_entry *egm_entry = NULL;
-	u64 egmpxm;
+	u64 egmphys, egmlength, egmpxm;
 	int ret = 0;
 	bool is_new_region = false;
 
@@ -80,6 +80,10 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
 		goto exit;
 
+	ret = nvgrace_gpu_fetch_egm_property(pdev, &egmphys, &egmlength);
+	if (ret)
+		goto exit;
+
 	list_for_each_entry(egm_entry, &egm_dev_list, list) {
 		/*
 		 * A system could have multiple GPUs associated with an
@@ -99,7 +103,7 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 
 	egm_entry->egm_dev =
 		nvgrace_gpu_create_aux_device(pdev, NVGRACE_EGM_DEV_NAME,
-					      egmpxm);
+					      egmphys, egmlength, egmpxm);
 	if (!egm_entry->egm_dev) {
 		ret = -EINVAL;
 		goto free_egm_entry;
include/linux/nvgrace-egm.h +2 -0
index e42494a2b1a6..a66906753267 100644
@@ -17,6 +17,8 @@ struct gpu_node {
 
 struct nvgrace_egm_dev {
 	struct auxiliary_device aux_dev;
+	phys_addr_t egmphys;
+	size_t egmlength;
 	u64 egmpxm;
 	struct list_head gpus;
 };
Signature · 1 line
2.34.1
author maintainer
#7 lore raw

RFC 5/14 vfio/nvgrace-egm: Introduce module to manage EGM

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The Extended GPU Memory (EGM) feature that enables the GPU to access the system memory allocations within and across nodes through high bandwidth path on Grace Based systems. The GPU can utilize the system memory located on the same socket or from a different socket or even on a different node in a multi-node system [1].

When the EGM mode is enabled through SBIOS, the host system memory is partitioned into 2 parts: One partition for the Host OS usage called Hypervisor region, and a second Hypervisor-Invisible (HI) region for the VM. Only the hypervisor region is part of the host EFI map and is thus visible to the host OS on bootup. Since the entire VM sysmem is eligible for EGM allocations within the VM, the HI partition is interchangeably called as EGM region in the series. This HI/EGM region range base SPA and size is exposed through the ACPI DSDT properties.

Whilst the EGM region is accessible on the host, it is not added to the kernel. The HI region is assigned to a VM by mapping the QEMU VMA to the SPA using remap_pfn_range().

The following figure shows the memory map in the virtualization environment.

|---- Sysmem ----|                  |--- GPU mem ---|  VM Memory
|                |                  |               |
|IPA <-> SPA map |                  |IPA <-> SPA map|
|                |                  |               |
|--- HI / EGM ---|-- Host Mem --|   |--- GPU mem ---|  Host Memory

Introduce a new nvgrace-egm auxiliary driver module to manage and map the HI/EGM region in the Grace Blackwell systems. This binds to the auxiliary device created by the parent nvgrace-gpu (in-tree module for device assignment) / nvidia-vgpu-vfio (out-of-tree open source module for SRIOV vGPU) to manage the EGM region for the VM. Note that there is a unique EGM region per socket and the auxiliary device gets created for every region. The parent module fetches the EGM region information from the ACPI tables and populate to the data structures shared with the auxiliary nvgrace-egm module.

nvgrace-egm module handles the following:
1. Fetch the EGM memory properties (base HPA, length, proximity domain) from the parent device shared EGM region structure.
2. Create a char device that can be used as memory-backend-file by Qemu for the VM and implement file operations. The char device is /dev/egmX, where X is the PXM node ID of the EGM being mapped fetched in 1.
3. Zero the EGM memory on first device open().
4. Map the QEMU VMA to the EGM region using remap_pfn_range.
5. Cleaning up state and destroying the chardev on device unbind.
6. Handle presence of retired ECC pages on the EGM region.

---
 MAINTAINERS                           |  6 ++++++
 drivers/vfio/pci/nvgrace-gpu/Kconfig  | 11 +++++++++++
 drivers/vfio/pci/nvgrace-gpu/Makefile |  3 +++
 drivers/vfio/pci/nvgrace-gpu/egm.c    | 22 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/main.c   |  1 +
 5 files changed, 43 insertions(+)
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm.c
MAINTAINERS +6 -0
index dd7df834b70b..ec6bc10f346d 100644
@@ -26476,6 +26476,12 @@ F:	drivers/vfio/pci/nvgrace-gpu/egm_dev.h
 F:	drivers/vfio/pci/nvgrace-gpu/main.c
 F:	include/linux/nvgrace-egm.h
 
+VFIO NVIDIA GRACE EGM DRIVER
+M:	Ankit Agrawal <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/vfio/pci/nvgrace-gpu/egm.c
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>
drivers/vfio/pci/nvgrace-gpu/Kconfig +11 -0
index a7f624b37e41..d5773bbd22f5 100644
@@ -1,8 +1,19 @@
 # SPDX-License-Identifier: GPL-2.0-only
+config NVGRACE_EGM
+	tristate "EGM driver for NVIDIA Grace Hopper and Blackwell Superchip"
+	depends on ARM64 || (COMPILE_TEST && 64BIT)
+	help
+	  Extended GPU Memory (EGM) support for the GPU in the NVIDIA Grace
+	  based chips required to avail the CPU memory as additional
+	  cross-node/cross-socket memory for GPU using KVM/qemu.
+
+	  If you don't know what to do here, say N.
+
 config NVGRACE_GPU_VFIO_PCI
 	tristate "VFIO support for the GPU in the NVIDIA Grace Hopper Superchip"
 	depends on ARM64 || (COMPILE_TEST && 64BIT)
 	select VFIO_PCI_CORE
+	select NVGRACE_EGM
 	help
 	  VFIO support for the GPU in the NVIDIA Grace Hopper Superchip is
 	  required to assign the GPU device to userspace using KVM/qemu/etc.
drivers/vfio/pci/nvgrace-gpu/Makefile +3 -0
index e72cc6739ef8..d0d191be56b9 100644
@@ -1,3 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu-vfio-pci.o
 nvgrace-gpu-vfio-pci-y := main.o egm_dev.o
+
+obj-$(CONFIG_NVGRACE_EGM) += nvgrace-egm.o
+nvgrace-egm-y := egm.o
drivers/vfio/pci/nvgrace-gpu/egm.c new file +22 -0
new file mode 100644
index 000000000000..999808807019
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include <linux/vfio_pci_core.h>
+
+static int __init nvgrace_egm_init(void)
+{
+	return 0;
+}
+
+static void __exit nvgrace_egm_cleanup(void)
+{
+}
+
+module_init(nvgrace_egm_init);
+module_exit(nvgrace_egm_cleanup);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
+MODULE_DESCRIPTION("NVGRACE EGM - Module to support Extended GPU Memory on NVIDIA Grace Based systems");
drivers/vfio/pci/nvgrace-gpu/main.c +1 -0
index 7486a1b49275..b1ccd1ac2e0a 100644
@@ -1125,3 +1125,4 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
 MODULE_AUTHOR("Aniket Agashe <[email protected]>");
 MODULE_DESCRIPTION("VFIO NVGRACE GPU PF - User Level driver for NVIDIA devices with CPU coherently accessible device memory");
+MODULE_SOFTDEP("pre: nvgrace-egm");
Signature · 1 line
2.34.1
#8 lore raw

Re: [RFC 05/14] vfio/nvgrace-egm: Introduce module to manage EGM

drivers/vfio/pci/nvgrace-gpu/main.c MODULE_LICENSE("GPL"); · line 1128

Quoted 5 lines · On Thu, Sep 04, 2025 at 04:08:19AM +0000, [email protected] wrote:
drivers/vfio/pci/nvgrace-gpu/main.c +1 -0
@@ -1125,3 +1125,4 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
 MODULE_AUTHOR("Aniket Agashe <[email protected]>");
 MODULE_DESCRIPTION("VFIO NVGRACE GPU PF - User Level driver for NVIDIA devices with CPU coherently accessible device memory");
+MODULE_SOFTDEP("pre: nvgrace-egm");

There sholdn't be softdeps, automatic struct device based probing should be sufficient.

Jason

#9 lore raw

RE: [RFC 05/14] vfio/nvgrace-egm: Introduce module to manage EGM

In reply to [email protected]
Quoted 109 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 05/14] vfio/nvgrace-egm: Introduce module to manage EGM

From: Ankit Agrawal <[email protected]>

The Extended GPU Memory (EGM) feature that enables the GPU to access the system memory allocations within and across nodes through high bandwidth path on Grace Based systems. The GPU can utilize the system memory located on the same socket or from a different socket or even on a different node in a multi-node system [1].

When the EGM mode is enabled through SBIOS, the host system memory is partitioned into 2 parts: One partition for the Host OS usage called Hypervisor region, and a second Hypervisor-Invisible (HI) region for the VM. Only the hypervisor region is part of the host EFI map and is thus visible to the host OS on bootup. Since the entire VM sysmem is eligible for EGM allocations within the VM, the HI partition is interchangeably called as EGM region in the series. This HI/EGM region range base SPA and size is exposed through the ACPI DSDT properties.

Whilst the EGM region is accessible on the host, it is not added to the kernel. The HI region is assigned to a VM by mapping the QEMU VMA to the SPA using remap_pfn_range().

The following figure shows the memory map in the virtualization environment.

|---- Sysmem ----|                  |--- GPU mem ---|  VM Memory
|                |                  |               |
|IPA <-> SPA map |                  |IPA <-> SPA map|
|                |                  |               |
|--- HI / EGM ---|-- Host Mem --|   |--- GPU mem ---|  Host Memory

Introduce a new nvgrace-egm auxiliary driver module to manage and map the HI/EGM region in the Grace Blackwell systems. This binds to the auxiliary device created by the parent nvgrace-gpu (in-tree module for device assignment) / nvidia-vgpu-vfio (out-of-tree open source module for SRIOV vGPU) to manage the EGM region for the VM. Note that there is a unique EGM region per socket and the auxiliary device gets created for every region. The parent module fetches the EGM region information from the ACPI tables and populate to the data structures shared with the auxiliary nvgrace-egm module.

nvgrace-egm module handles the following:
1. Fetch the EGM memory properties (base HPA, length, proximity domain) from the parent device shared EGM region structure.
2. Create a char device that can be used as memory-backend-file by Qemu for the VM and implement file operations. The char device is /dev/egmX, where X is the PXM node ID of the EGM being mapped fetched in 1.
3. Zero the EGM memory on first device open().
4. Map the QEMU VMA to the EGM region using remap_pfn_range.
5. Cleaning up state and destroying the chardev on device unbind.
6. Handle presence of retired ECC pages on the EGM region.

---
 MAINTAINERS                           |  6 ++++++
 drivers/vfio/pci/nvgrace-gpu/Kconfig  | 11 +++++++++++
 drivers/vfio/pci/nvgrace-gpu/Makefile |  3 +++
 drivers/vfio/pci/nvgrace-gpu/egm.c    | 22 ++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/main.c   |  1 +
 5 files changed, 43 insertions(+)
 create mode 100644 drivers/vfio/pci/nvgrace-gpu/egm.c
MAINTAINERS +0 -0
index dd7df834b70b..ec6bc10f346d 100644
@@ -26476,6 +26476,12 @@ F:	drivers/vfio/pci/nvgrace-

gpu/egm_dev.h
F: drivers/vfio/pci/nvgrace-gpu/main.c
F: include/linux/nvgrace-egm.h

MAINTAINERS +6 -0
+VFIO NVIDIA GRACE EGM DRIVER
+M:	Ankit Agrawal <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/vfio/pci/nvgrace-gpu/egm.c
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/Kconfig +0 -0

gpu/Kconfig
index a7f624b37e41..d5773bbd22f5 100644

drivers/vfio/pci/nvgrace-gpu/Kconfig +2 -0
@@ -1,8 +1,19 @@
 # SPDX-License-Identifier: GPL-2.0-only
+config NVGRACE_EGM
+	tristate "EGM driver for NVIDIA Grace Hopper and Blackwell

Superchip"

drivers/vfio/pci/nvgrace-gpu/Kconfig line 4

Quoted 1 line
drivers/vfio/pci/nvgrace-gpu/Kconfig +1 -0
+	depends on ARM64 || (COMPILE_TEST && 64BIT)

Should it depend on NVGRACE_GPU_VFIO_PCI as well?

Thanks,
Shameer

Quoted 74 lines
drivers/vfio/pci/nvgrace-gpu/Kconfig +2 -0
+	help
+	  Extended GPU Memory (EGM) support for the GPU in the NVIDIA

Grace

drivers/vfio/pci/nvgrace-gpu/Kconfig +5 -0
+	  based chips required to avail the CPU memory as additional
+	  cross-node/cross-socket memory for GPU using KVM/qemu.
+
+	  If you don't know what to do here, say N.
+
 config NVGRACE_GPU_VFIO_PCI
 	tristate "VFIO support for the GPU in the NVIDIA Grace Hopper

Superchip"

drivers/vfio/pci/nvgrace-gpu/Kconfig +1 -0
 	depends on ARM64 || (COMPILE_TEST && 64BIT)
 	select VFIO_PCI_CORE
+	select NVGRACE_EGM
 	help
 	  VFIO support for the GPU in the NVIDIA Grace Hopper Superchip is
 	  required to assign the GPU device to userspace using

KVM/qemu/etc.

drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/Makefile +0 -0

gpu/Makefile
index e72cc6739ef8..d0d191be56b9 100644

drivers/vfio/pci/nvgrace-gpu/Makefile +3 -0
@@ -1,3 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu-vfio-pci.o
 nvgrace-gpu-vfio-pci-y := main.o egm_dev.o
+
+obj-$(CONFIG_NVGRACE_EGM) += nvgrace-egm.o
+nvgrace-egm-y := egm.o
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/egm.c +0 -0

gpu/egm.c
new file mode 100644
index 000000000000..999808807019

drivers/vfio/pci/nvgrace-gpu/egm.c new file +3 -0
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights

reserved
+ */

drivers/vfio/pci/nvgrace-gpu/egm.c new file +18 -0
+
+#include <linux/vfio_pci_core.h>
+
+static int __init nvgrace_egm_init(void)
+{
+	return 0;
+}
+
+static void __exit nvgrace_egm_cleanup(void)
+{
+}
+
+module_init(nvgrace_egm_init);
+module_exit(nvgrace_egm_cleanup);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
+MODULE_DESCRIPTION("NVGRACE EGM - Module to support Extended GPU

Memory on NVIDIA Grace Based systems");

drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/main.c +0 -0

gpu/main.c
index 7486a1b49275..b1ccd1ac2e0a 100644

drivers/vfio/pci/nvgrace-gpu/main.c +0 -0
@@ -1125,3 +1125,4 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Ankit Agrawal <[email protected]>");
 MODULE_AUTHOR("Aniket Agashe <[email protected]>");
 MODULE_DESCRIPTION("VFIO NVGRACE GPU PF - User Level driver for

NVIDIA devices with CPU coherently accessible device memory"); +MODULE_SOFTDEP("pre: nvgrace-egm");
--
2.34.1

author maintainer
#10 lore raw

RFC 6/14 vfio/nvgrace-egm: Introduce egm class and register char device numbers

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The EGM regions are exposed to the userspace as char devices. A unique char device with a different minor number is assigned to EGM region belonging to a different Grace socket.

Add a new egm class and register a range of char device numbers for the same.

Setting MAX_EGM_NODES as 4 as the 4-socket is the largest configuration on Grace based systems.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 36 ++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/vfio/pci/nvgrace-gpu/egm.c +36 -0
index 999808807019..6bab4d94cb99 100644
@@ -4,14 +4,50 @@
  */
 
 #include <linux/vfio_pci_core.h>
+#include <linux/nvgrace-egm.h>
+
+#define MAX_EGM_NODES 4
+
+static dev_t dev;
+static struct class *class;
+
+static char *egm_devnode(const struct device *device, umode_t *mode)
+{
+	if (mode)
+		*mode = 0600;
+
+	return NULL;
+}
 
 static int __init nvgrace_egm_init(void)
 {
+	int ret;
+
+	/*
+	 * Each EGM region on a system is represented with a unique
+	 * char device with a different minor number. Allow a range
+	 * of char device creation.
+	 */
+	ret = alloc_chrdev_region(&dev, 0, MAX_EGM_NODES,
+				  NVGRACE_EGM_DEV_NAME);
+	if (ret < 0)
+		return ret;
+
+	class = class_create(NVGRACE_EGM_DEV_NAME);
+	if (IS_ERR(class)) {
+		unregister_chrdev_region(dev, MAX_EGM_NODES);
+		return PTR_ERR(class);
+	}
+
+	class->devnode = egm_devnode;
+
 	return 0;
 }
 
 static void __exit nvgrace_egm_cleanup(void)
 {
+	class_destroy(class);
+	unregister_chrdev_region(dev, MAX_EGM_NODES);
 }
 
 module_init(nvgrace_egm_init);
Signature · 1 line
2.34.1
author maintainer
#11 lore raw

RFC 7/14 vfio/nvgrace-egm: Register auxiliary driver ops

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Setup dummy auxiliary device ops to be able to get probed by the nvgrace-egm auxiliary driver.

Both nvgrace-gpu and the out-of-tree nvidia-vgpu-vfio will make use of the EGM for device assignment and the SRIOV vGPU virtualization solutions respectively. Hence allow auxiliary device probing for both.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 39 +++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
drivers/vfio/pci/nvgrace-gpu/egm.c +36 -3
index 6bab4d94cb99..12d4e6e83fff 100644
@@ -11,6 +11,30 @@
 static dev_t dev;
 static struct class *class;
 
+static int egm_driver_probe(struct auxiliary_device *aux_dev,
+			    const struct auxiliary_device_id *id)
+{
+	return 0;
+}
+
+static void egm_driver_remove(struct auxiliary_device *aux_dev)
+{
+}
+
+static const struct auxiliary_device_id egm_id_table[] = {
+	{ .name = "nvgrace_gpu_vfio_pci.egm" },
+	{ .name = "nvidia_vgpu_vfio.egm" },
+	{ },
+};
+MODULE_DEVICE_TABLE(auxiliary, egm_id_table);
+
+static struct auxiliary_driver egm_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = egm_id_table,
+	.probe = egm_driver_probe,
+	.remove = egm_driver_remove,
+};
+
 static char *egm_devnode(const struct device *device, umode_t *mode)
 {
 	if (mode)
@@ -35,19 +59,28 @@ static int __init nvgrace_egm_init(void)
 
 	class = class_create(NVGRACE_EGM_DEV_NAME);
 	if (IS_ERR(class)) {
-		unregister_chrdev_region(dev, MAX_EGM_NODES);
-		return PTR_ERR(class);
+		ret = PTR_ERR(class);
+		goto unregister_chrdev;
 	}
 
 	class->devnode = egm_devnode;
 
-	return 0;
+	ret = auxiliary_driver_register(&egm_driver);
+	if (!ret)
+		goto fn_exit;
+
+	class_destroy(class);
+unregister_chrdev:
+	unregister_chrdev_region(dev, MAX_EGM_NODES);
+fn_exit:
+	return ret;
 }
 
 static void __exit nvgrace_egm_cleanup(void)
 {
 	class_destroy(class);
 	unregister_chrdev_region(dev, MAX_EGM_NODES);
+	auxiliary_driver_unregister(&egm_driver);
 }
 
 module_init(nvgrace_egm_init);
Signature · 1 line
2.34.1
#12 lore raw

Re: [RFC 07/14] vfio/nvgrace-egm: Register auxiliary driver ops

drivers/vfio/pci/nvgrace-gpu/egm.c line 26

Quoted 3 lines · On Thu, Sep 04, 2025 at 04:08:21AM +0000, [email protected] wrote:
drivers/vfio/pci/nvgrace-gpu/egm.c +3 -0
+static const struct auxiliary_device_id egm_id_table[] = {
+	{ .name = "nvgrace_gpu_vfio_pci.egm" },
+	{ .name = "nvidia_vgpu_vfio.egm" },

Not in tree

Quoted 23 lines

static char *egm_devnode(const struct device *device, umode_t *mode) {

drivers/vfio/pci/nvgrace-gpu/egm.c +9 -3
 	if (mode)
@@ -35,19 +59,28 @@ static int __init nvgrace_egm_init(void)
 
 	class = class_create(NVGRACE_EGM_DEV_NAME);
 	if (IS_ERR(class)) {
-		unregister_chrdev_region(dev, MAX_EGM_NODES);
-		return PTR_ERR(class);
+		ret = PTR_ERR(class);
+		goto unregister_chrdev;
 	}
 
 	class->devnode = egm_devnode;
 
-	return 0;
+	ret = auxiliary_driver_register(&egm_driver);
+	if (!ret)
+		goto fn_exit;
+
+	class_destroy(class);
+unregister_chrdev:
+	unregister_chrdev_region(dev, MAX_EGM_NODES);

drivers/vfio/pci/nvgrace-gpu/egm.c static int __init nvgrace_egm_init(void) · line 84

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +3 -0
+fn_exit:
+	return ret;
 }
 
 static void __exit nvgrace_egm_cleanup(void)
 {
 	class_destroy(class);
 	unregister_chrdev_region(dev, MAX_EGM_NODES);
+	auxiliary_driver_unregister(&egm_driver);
 }

Out of order, the order should be the reverse of init. This will UAF the class as-is.

Jason

author maintainer
#13 lore raw

RFC 8/14 vfio/nvgrace-egm: Expose EGM region as char device

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The EGM module expose the various EGM regions as a char device. A usermode app such as Qemu may mmap to the region and use as VM sysmem. Each EGM region is represented with a unique char device /dev/egmX bearing a distinct minor number.

EGM module implements the mmap file_ops to manage the usermode app's VMA mapping to the EGM region. The appropriate region is determined from the minor number.

Note that the EGM memory region is invisible to the host kernel as it is not present in the host EFI map. The host Linux MM thus cannot manage the memory, even though it is accessible on the host SPA. The EGM module thus use remap_pfn_range() to perform the VMA mapping to the EGM region.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 99 ++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
drivers/vfio/pci/nvgrace-gpu/egm.c +99 -0
index 12d4e6e83fff..c2dce5fa797a 100644
@@ -10,15 +10,114 @@
 
 static dev_t dev;
 static struct class *class;
+static DEFINE_XARRAY(egm_chardevs);
+
+struct chardev {
+	struct device device;
+	struct cdev cdev;
+};
+
+static int nvgrace_egm_open(struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static int nvgrace_egm_release(struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static int nvgrace_egm_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	return 0;
+}
+
+static const struct file_operations file_ops = {
+	.owner = THIS_MODULE,
+	.open = nvgrace_egm_open,
+	.release = nvgrace_egm_release,
+	.mmap = nvgrace_egm_mmap,
+};
+
+static void egm_chardev_release(struct device *dev)
+{
+	struct chardev *egm_chardev = container_of(dev, struct chardev, device);
+
+	kvfree(egm_chardev);
+}
+
+static struct chardev *
+setup_egm_chardev(struct nvgrace_egm_dev *egm_dev)
+{
+	struct chardev *egm_chardev;
+	int ret;
+
+	egm_chardev = kvzalloc(sizeof(*egm_chardev), GFP_KERNEL);
+	if (!egm_chardev)
+		goto create_err;
+
+	device_initialize(&egm_chardev->device);
+
+	/*
+	 * Use the proximity domain number as the device minor
+	 * number. So the EGM corresponding to node X would be
+	 * /dev/egmX.
+	 */
+	egm_chardev->device.devt = MKDEV(MAJOR(dev), egm_dev->egmpxm);
+	egm_chardev->device.class = class;
+	egm_chardev->device.release = egm_chardev_release;
+	egm_chardev->device.parent = &egm_dev->aux_dev.dev;
+	cdev_init(&egm_chardev->cdev, &file_ops);
+	egm_chardev->cdev.owner = THIS_MODULE;
+
+	ret = dev_set_name(&egm_chardev->device, "egm%lld", egm_dev->egmpxm);
+	if (ret)
+		goto error_exit;
+
+	ret = cdev_device_add(&egm_chardev->cdev, &egm_chardev->device);
+	if (ret)
+		goto error_exit;
+
+	return egm_chardev;
+
+error_exit:
+	kvfree(egm_chardev);
+create_err:
+	return NULL;
+}
+
+static void del_egm_chardev(struct chardev *egm_chardev)
+{
+	cdev_device_del(&egm_chardev->cdev, &egm_chardev->device);
+	put_device(&egm_chardev->device);
+}
 
 static int egm_driver_probe(struct auxiliary_device *aux_dev,
 			    const struct auxiliary_device_id *id)
 {
+	struct nvgrace_egm_dev *egm_dev =
+		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
+	struct chardev *egm_chardev;
+
+	egm_chardev = setup_egm_chardev(egm_dev);
+	if (!egm_chardev)
+		return -EINVAL;
+
+	xa_store(&egm_chardevs, egm_dev->egmpxm, egm_chardev, GFP_KERNEL);
+
 	return 0;
 }
 
 static void egm_driver_remove(struct auxiliary_device *aux_dev)
 {
+	struct nvgrace_egm_dev *egm_dev =
+		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
+	struct chardev *egm_chardev = xa_erase(&egm_chardevs, egm_dev->egmpxm);
+
+	if (!egm_chardev)
+		return;
+
+	del_egm_chardev(egm_chardev);
 }
 
 static const struct auxiliary_device_id egm_id_table[] = {
Signature · 1 line
2.34.1
#14 lore raw

Re: [RFC 08/14] vfio/nvgrace-egm: Expose EGM region as char device

Quoted 26 lines · On Thu, Sep 04, 2025 at 04:08:22AM +0000, [email protected] wrote:
drivers/vfio/pci/nvgrace-gpu/egm.c +26 -0
+static struct chardev *
+setup_egm_chardev(struct nvgrace_egm_dev *egm_dev)
+{
+	struct chardev *egm_chardev;
+	int ret;
+
+	egm_chardev = kvzalloc(sizeof(*egm_chardev), GFP_KERNEL);
+	if (!egm_chardev)
+		goto create_err;
+
+	device_initialize(&egm_chardev->device);
+
+	/*
+	 * Use the proximity domain number as the device minor
+	 * number. So the EGM corresponding to node X would be
+	 * /dev/egmX.
+	 */
+	egm_chardev->device.devt = MKDEV(MAJOR(dev), egm_dev->egmpxm);
+	egm_chardev->device.class = class;
+	egm_chardev->device.release = egm_chardev_release;
+	egm_chardev->device.parent = &egm_dev->aux_dev.dev;
+	cdev_init(&egm_chardev->cdev, &file_ops);
+	egm_chardev->cdev.owner = THIS_MODULE;
+
+	ret = dev_set_name(&egm_chardev->device, "egm%lld", egm_dev->egmpxm);
+	if (ret)

drivers/vfio/pci/nvgrace-gpu/egm.c line 84

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +10 -0
+		goto error_exit;
+
+	ret = cdev_device_add(&egm_chardev->cdev, &egm_chardev->device);
+	if (ret)
+		goto error_exit;
+
+	return egm_chardev;
+
+error_exit:
+	kvfree(egm_chardev);

After calling init you have to use put_device not kvfree.

Why kvalloc anyhow? Struct chardev is not big

drivers/vfio/pci/nvgrace-gpu/egm.c line 121

Quoted 11 lines

static void egm_driver_remove(struct auxiliary_device *aux_dev) {

drivers/vfio/pci/nvgrace-gpu/egm.c +8 -0
+	struct nvgrace_egm_dev *egm_dev =
+		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
+	struct chardev *egm_chardev = xa_erase(&egm_chardevs, egm_dev->egmpxm);
+
+	if (!egm_chardev)
+		return;
+
+	del_egm_chardev(egm_chardev);
 }

This proceeds even if files are left open which is not going to be any good..

Jason

#15 lore raw

RE: [RFC 08/14] vfio/nvgrace-egm: Expose EGM region as char device

In reply to [email protected]
Quoted 19 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 08/14] vfio/nvgrace-egm: Expose EGM region as char device

[...]

Quoted 12 lines

static int egm_driver_probe(struct auxiliary_device *aux_dev,

drivers/vfio/pci/nvgrace-gpu/egm.c +9 -0
 			    const struct auxiliary_device_id *id)
 {
+	struct nvgrace_egm_dev *egm_dev =
+		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
+	struct chardev *egm_chardev;
+
+	egm_chardev = setup_egm_chardev(egm_dev);
+	if (!egm_chardev)
+		return -EINVAL;
+
+	xa_store(&egm_chardevs, egm_dev->egmpxm, egm_chardev,
Quoted 1 line

GFP_KERNEL);

Nit: May be better to check ret for xa_store here.

Thanks,
Shameer

author maintainer
#16 lore raw

RFC 9/14 vfio/nvgrace-egm: Add chardev ops for EGM management

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

EGM module implements the mmap file_ops to manage the usermode app's VMA mapping to the EGM region. The appropriate region is determined from the minor number.

Note that the EGM memory region is invisible to the host kernel as it is not present in the host EFI map. The host Linux MM thus cannot manage the memory, even though it is accessible on the host SPA. The EGM module thus use remap_pfn_range() to perform the VMA mapping to the EGM region.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/vfio/pci/nvgrace-gpu/egm.c +28 -1
index c2dce5fa797a..7bf6a05aa967 100644
@@ -17,19 +17,46 @@ struct chardev {
 	struct cdev cdev;
 };
 
+static struct nvgrace_egm_dev *
+egm_chardev_to_nvgrace_egm_dev(struct chardev *egm_chardev)
+{
+	struct auxiliary_device *aux_dev =
+		container_of(egm_chardev->device.parent, struct auxiliary_device, dev);
+
+	return container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
+}
+
 static int nvgrace_egm_open(struct inode *inode, struct file *file)
 {
+	struct chardev *egm_chardev =
+		container_of(inode->i_cdev, struct chardev, cdev);
+
+	file->private_data = egm_chardev;
+
 	return 0;
 }
 
 static int nvgrace_egm_release(struct inode *inode, struct file *file)
 {
+	file->private_data = NULL;
+
 	return 0;
 }
 
 static int nvgrace_egm_mmap(struct file *file, struct vm_area_struct *vma)
 {
-	return 0;
+	struct chardev *egm_chardev = file->private_data;
+	struct nvgrace_egm_dev *egm_dev =
+		egm_chardev_to_nvgrace_egm_dev(egm_chardev);
+
+	/*
+	 * EGM memory is invisible to the host kernel and is not managed
+	 * by it. Map the usermode VMA to the EGM region.
+	 */
+	return remap_pfn_range(vma, vma->vm_start,
+			       PHYS_PFN(egm_dev->egmphys),
+			       (vma->vm_end - vma->vm_start),
+			       vma->vm_page_prot);
 }
 
 static const struct file_operations file_ops = {
Signature · 1 line
2.34.1
#17 lore raw

Re: [RFC 09/14] vfio/nvgrace-egm: Add chardev ops for EGM management

Quoted 5 lines · On Thu, Sep 04, 2025 at 04:08:23AM +0000, [email protected] wrote:

static int nvgrace_egm_mmap(struct file *file, struct vm_area_struct *vma) {

drivers/vfio/pci/nvgrace-gpu/egm.c +2 -1
-	return 0;
+	struct chardev *egm_chardev = file->private_data;
+	struct nvgrace_egm_dev *egm_dev =

drivers/vfio/pci/nvgrace-gpu/egm.c struct chardev { · line 59

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +10 -0
+		egm_chardev_to_nvgrace_egm_dev(egm_chardev);
+
+	/*
+	 * EGM memory is invisible to the host kernel and is not managed
+	 * by it. Map the usermode VMA to the EGM region.
+	 */
+	return remap_pfn_range(vma, vma->vm_start,
+			       PHYS_PFN(egm_dev->egmphys),
+			       (vma->vm_end - vma->vm_start),
+			       vma->vm_page_prot);

This needs to handle vm_pgoff and sanity check end - start!!

It should also reject !MAP_SHARED

Jason

author maintainer
#18 lore raw

RFC 10/14 vfio/nvgrace-egm: Clear Memory before handing out to VM

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

The EGM region is invisible to the host Linux kernel and it does not manage the region. The EGM module manages the EGM memory and thus is responsible to clear out the region before handing out to the VM.

Clear EGM region on EGM chardev open. It is possible to trigger open multiple times by tools such as kvmtool. Thus ensure the region is cleared only on the first open.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/vfio/pci/nvgrace-gpu/egm.c +27 -1
index 7bf6a05aa967..bf1241ed1d60 100644
@@ -15,6 +15,7 @@ static DEFINE_XARRAY(egm_chardevs);
 struct chardev {
 	struct device device;
 	struct cdev cdev;
+	atomic_t open_count;
 };
 
 static struct nvgrace_egm_dev *
@@ -30,6 +31,26 @@ static int nvgrace_egm_open(struct inode *inode, struct file *file)
 {
 	struct chardev *egm_chardev =
 		container_of(inode->i_cdev, struct chardev, cdev);
+	struct nvgrace_egm_dev *egm_dev =
+		egm_chardev_to_nvgrace_egm_dev(egm_chardev);
+	void *memaddr;
+
+	if (atomic_inc_return(&egm_chardev->open_count) > 1)
+		return 0;
+
+	/*
+	 * nvgrace-egm module is responsible to manage the EGM memory as
+	 * the host kernel has no knowledge of it. Clear the region before
+	 * handing over to userspace.
+	 */
+	memaddr = memremap(egm_dev->egmphys, egm_dev->egmlength, MEMREMAP_WB);
+	if (!memaddr) {
+		atomic_dec(&egm_chardev->open_count);
+		return -EINVAL;
+	}
+
+	memset((u8 *)memaddr, 0, egm_dev->egmlength);
+	memunmap(memaddr);
 
 	file->private_data = egm_chardev;
 
@@ -38,7 +59,11 @@ static int nvgrace_egm_open(struct inode *inode, struct file *file)
 
 static int nvgrace_egm_release(struct inode *inode, struct file *file)
 {
-	file->private_data = NULL;
+	struct chardev *egm_chardev =
+		container_of(inode->i_cdev, struct chardev, cdev);
+
+	if (atomic_dec_and_test(&egm_chardev->open_count))
+		file->private_data = NULL;
 
 	return 0;
 }
@@ -96,6 +121,7 @@ setup_egm_chardev(struct nvgrace_egm_dev *egm_dev)
 	egm_chardev->device.parent = &egm_dev->aux_dev.dev;
 	cdev_init(&egm_chardev->cdev, &file_ops);
 	egm_chardev->cdev.owner = THIS_MODULE;
+	atomic_set(&egm_chardev->open_count, 0);
 
 	ret = dev_set_name(&egm_chardev->device, "egm%lld", egm_dev->egmpxm);
 	if (ret)
Signature · 1 line
2.34.1
#19 lore raw

Re: [RFC 10/14] vfio/nvgrace-egm: Clear Memory before handing out to VM

Quoted 9 lines · On Thu, Sep 04, 2025 at 04:08:24AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

The EGM region is invisible to the host Linux kernel and it does not manage the region. The EGM module manages the EGM memory and thus is responsible to clear out the region before handing out to the VM.

Clear EGM region on EGM chardev open. It is possible to trigger open multiple times by tools such as kvmtool. Thus ensure the region is cleared only on the first open.

It would be cleaner not to support multi-open, why is kvmtool doing this?

Jason

#20 lore raw

RE: [RFC 10/14] vfio/nvgrace-egm: Clear Memory before handing out to VM

In reply to [email protected]
Quoted 20 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 10/14] vfio/nvgrace-egm: Clear Memory before handing out to VM

[...]

Quoted 13 lines

static struct nvgrace_egm_dev *

drivers/vfio/pci/nvgrace-gpu/egm.c +7 -0
@@ -30,6 +31,26 @@ static int nvgrace_egm_open(struct inode *inode,
 struct file *file)
 {
 	struct chardev *egm_chardev =
 		container_of(inode->i_cdev, struct chardev, cdev);
+	struct nvgrace_egm_dev *egm_dev =
+		egm_chardev_to_nvgrace_egm_dev(egm_chardev);
+	void *memaddr;
+
+	if (atomic_inc_return(&egm_chardev->open_count) > 1)
+		return 0;
+

drivers/vfio/pci/nvgrace-gpu/egm.c static int nvgrace_egm_open(struct inode *inode, · line 51

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +9 -0
+	/*
+	 * nvgrace-egm module is responsible to manage the EGM memory as
+	 * the host kernel has no knowledge of it. Clear the region before
+	 * handing over to userspace.
+	 */
+	memaddr = memremap(egm_dev->egmphys, egm_dev->egmlength,
 MEMREMAP_WB);
+	if (!memaddr) {
+		atomic_dec(&egm_chardev->open_count);
+		return -EINVAL;

Nit: may be better to ret -ENOMEM here.

Thanks,
Shameer

author maintainer
#21 lore raw

RFC 11/14 vfio/nvgrace-egm: Fetch EGM region retired pages list

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

It is possible for some system memory pages on the EGM to have retired pages with uncorrectable ECC errors. A list of pages known with such errors (referred as retired pages) are maintained by the Host UEFI. The Host UEFI populates such list in a reserved region. It communicates the SPA of this region through a ACPI DSDT property.

nvgrace-egm module is responsible to store the list of retired page offsets to be made available for usermode processes. The module:
1. Get the reserved memory region SPA and maps to it to fetch the list of bad pages.
2. Calculate the retired page offsets in the EGM and stores it.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c     | 81 ++++++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 32 ++++++++--
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  5 +-
 drivers/vfio/pci/nvgrace-gpu/main.c    |  8 ++-
 include/linux/nvgrace-egm.h            |  2 +
 5 files changed, 118 insertions(+), 10 deletions(-)
drivers/vfio/pci/nvgrace-gpu/egm.c +81 -0
index bf1241ed1d60..7a026b4d98f7 100644
@@ -8,6 +8,11 @@
 
 #define MAX_EGM_NODES 4
 
+struct h_node {
+	unsigned long mem_offset;
+	struct hlist_node node;
+};
+
 static dev_t dev;
 static struct class *class;
 static DEFINE_XARRAY(egm_chardevs);
@@ -16,6 +21,7 @@ struct chardev {
 	struct device device;
 	struct cdev cdev;
 	atomic_t open_count;
+	DECLARE_HASHTABLE(htbl, 0x10);
 };
 
 static struct nvgrace_egm_dev *
@@ -145,20 +151,86 @@ static void del_egm_chardev(struct chardev *egm_chardev)
 	put_device(&egm_chardev->device);
 }
 
+static void cleanup_retired_pages(struct chardev *egm_chardev)
+{
+	struct h_node *cur_page;
+	unsigned long bkt;
+	struct hlist_node *temp_node;
+
+	hash_for_each_safe(egm_chardev->htbl, bkt, temp_node, cur_page, node) {
+		hash_del(&cur_page->node);
+		kvfree(cur_page);
+	}
+}
+
+static int nvgrace_egm_fetch_retired_pages(struct nvgrace_egm_dev *egm_dev,
+					   struct chardev *egm_chardev)
+{
+	u64 count;
+	void *memaddr;
+	int index, ret = 0;
+
+	memaddr = memremap(egm_dev->retiredpagesphys, PAGE_SIZE, MEMREMAP_WB);
+	if (!memaddr)
+		return -ENOMEM;
+
+	count = *(u64 *)memaddr;
+
+	for (index = 0; index < count; index++) {
+		struct h_node *retired_page;
+
+		/*
+		 * Since the EGM is linearly mapped, the offset in the
+		 * carveout is the same offset in the VM system memory.
+		 *
+		 * Calculate the offset to communicate to the usermode
+		 * apps.
+		 */
+		retired_page = kvzalloc(sizeof(*retired_page), GFP_KERNEL);
+		if (!retired_page) {
+			ret = -ENOMEM;
+			break;
+		}
+
+		retired_page->mem_offset = *((u64 *)memaddr + index + 1) -
+					   egm_dev->egmphys;
+		hash_add(egm_chardev->htbl, &retired_page->node,
+			 retired_page->mem_offset);
+	}
+
+	memunmap(memaddr);
+
+	if (ret)
+		cleanup_retired_pages(egm_chardev);
+
+	return ret;
+}
+
 static int egm_driver_probe(struct auxiliary_device *aux_dev,
 			    const struct auxiliary_device_id *id)
 {
 	struct nvgrace_egm_dev *egm_dev =
 		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
 	struct chardev *egm_chardev;
+	int ret;
 
 	egm_chardev = setup_egm_chardev(egm_dev);
 	if (!egm_chardev)
 		return -EINVAL;
 
+	hash_init(egm_chardev->htbl);
+
+	ret = nvgrace_egm_fetch_retired_pages(egm_dev, egm_chardev);
+	if (ret)
+		goto error_exit;
+
 	xa_store(&egm_chardevs, egm_dev->egmpxm, egm_chardev, GFP_KERNEL);
 
 	return 0;
+
+error_exit:
+	del_egm_chardev(egm_chardev);
+	return ret;
 }
 
 static void egm_driver_remove(struct auxiliary_device *aux_dev)
@@ -166,10 +238,19 @@ static void egm_driver_remove(struct auxiliary_device *aux_dev)
 	struct nvgrace_egm_dev *egm_dev =
 		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
 	struct chardev *egm_chardev = xa_erase(&egm_chardevs, egm_dev->egmpxm);
+	struct h_node *cur_page;
+	unsigned long bkt;
+	struct hlist_node *temp_node;
 
 	if (!egm_chardev)
 		return;
 
+	hash_for_each_safe(egm_chardev->htbl, bkt, temp_node, cur_page, node) {
+		hash_del(&cur_page->node);
+		kvfree(cur_page);
+	}
+
+	cleanup_retired_pages(egm_chardev);
 	del_egm_chardev(egm_chardev);
 }
 
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +27 -5
index ca50bc1f67a0..b8e143542bce 100644
@@ -18,22 +18,41 @@ int nvgrace_gpu_has_egm_property(struct pci_dev *pdev, u64 *pegmpxm)
 }
 
 int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
-				   u64 *pegmlength)
+				   u64 *pegmlength, u64 *pretiredpagesphys)
 {
 	int ret;
 
 	/*
-	 * The memory information is present in the system ACPI tables as DSD
-	 * properties nvidia,egm-base-pa and nvidia,egm-size.
+	 * The EGM memory information is present in the system ACPI tables
+	 * as DSD properties nvidia,egm-base-pa and nvidia,egm-size.
 	 */
 	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-size",
 				       pegmlength);
 	if (ret)
-		return ret;
+		goto error_exit;
 
 	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-base-pa",
 				       pegmphys);
+	if (ret)
+		goto error_exit;
+
+	/*
+	 * SBIOS puts the list of retired pages on a region. The region
+	 * SPA is exposed as "nvidia,egm-retired-pages-data-base".
+	 */
+	ret = device_property_read_u64(&pdev->dev,
+				       "nvidia,egm-retired-pages-data-base",
+				       pretiredpagesphys);
+	if (ret)
+		goto error_exit;
+
+	/* Catch firmware bug and avoid a crash */
+	if (*pretiredpagesphys == 0) {
+		dev_err(&pdev->dev, "Retired pages region is not setup\n");
+		ret = -EINVAL;
+	}
 
+error_exit:
 	return ret;
 }
 
@@ -74,7 +93,8 @@ static void nvgrace_gpu_release_aux_device(struct device *device)
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmphys, u64 egmlength, u64 egmpxm)
+			      u64 egmphys, u64 egmlength, u64 egmpxm,
+			      u64 retiredpagesphys)
 {
 	struct nvgrace_egm_dev *egm_dev;
 	int ret;
@@ -86,6 +106,8 @@ nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
 	egm_dev->egmpxm = egmpxm;
 	egm_dev->egmphys = egmphys;
 	egm_dev->egmlength = egmlength;
+	egm_dev->retiredpagesphys = retiredpagesphys;
+
 	INIT_LIST_HEAD(&egm_dev->gpus);
 
 	egm_dev->aux_dev.id = egmpxm;
drivers/vfio/pci/nvgrace-gpu/egm_dev.h +3 -2
index 2e1612445898..2f329a05685d 100644
@@ -16,8 +16,9 @@ void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev);
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmphys, u64 egmlength, u64 egmpxm);
+			      u64 egmphys, u64 egmlength, u64 egmpxm,
+			      u64 retiredpagesphys);
 
 int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
-				   u64 *pegmlength);
+				   u64 *pegmlength, u64 *pretiredpagesphys);
 #endif /* EGM_DEV_H */
drivers/vfio/pci/nvgrace-gpu/main.c +5 -3
index b1ccd1ac2e0a..534dc3ee6113 100644
@@ -67,7 +67,7 @@ static struct list_head egm_dev_list;
 static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 {
 	struct nvgrace_egm_dev_entry *egm_entry = NULL;
-	u64 egmphys, egmlength, egmpxm;
+	u64 egmphys, egmlength, egmpxm, retiredpagesphys;
 	int ret = 0;
 	bool is_new_region = false;
 
@@ -80,7 +80,8 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
 		goto exit;
 
-	ret = nvgrace_gpu_fetch_egm_property(pdev, &egmphys, &egmlength);
+	ret = nvgrace_gpu_fetch_egm_property(pdev, &egmphys, &egmlength,
+					     &retiredpagesphys);
 	if (ret)
 		goto exit;
 
@@ -103,7 +104,8 @@ static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 
 	egm_entry->egm_dev =
 		nvgrace_gpu_create_aux_device(pdev, NVGRACE_EGM_DEV_NAME,
-					      egmphys, egmlength, egmpxm);
+					      egmphys, egmlength, egmpxm,
+					      retiredpagesphys);
 	if (!egm_entry->egm_dev) {
 		ret = -EINVAL;
 		goto free_egm_entry;
include/linux/nvgrace-egm.h +2 -0
index a66906753267..197255c2a3b7 100644
@@ -7,6 +7,7 @@
 #define NVGRACE_EGM_H
 
 #include <linux/auxiliary_bus.h>
+#include <linux/hashtable.h>
 
 #define NVGRACE_EGM_DEV_NAME "egm"
 
@@ -19,6 +20,7 @@ struct nvgrace_egm_dev {
 	struct auxiliary_device aux_dev;
 	phys_addr_t egmphys;
 	size_t egmlength;
+	phys_addr_t retiredpagesphys;
 	u64 egmpxm;
 	struct list_head gpus;
 };
Signature · 1 line
2.34.1
#22 lore raw

RE: [RFC 11/14] vfio/nvgrace-egm: Fetch EGM region retired pages list

Quoted 109 lines

-----Original Message-----
From: Ankit Agrawal <[email protected]>
Sent: 04 September 2025 05:08
To: Ankit Agrawal <[email protected]>; Jason Gunthorpe <[email protected]>; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum <[email protected]>; [email protected]; [email protected]; Zhi Wang <[email protected]>
Cc: Aniket Agashe <[email protected]>; Neo Jia <[email protected]>; Kirti Wankhede <[email protected]>; Tarun Gupta (SW-GPU) <[email protected]>; Vikram Sethi <[email protected]>; Andy Currid <[email protected]>; Alistair Popple <[email protected]>; John Hubbard <[email protected]>; Dan Williams <[email protected]>; Anuj Aggarwal (SW-GPU) <[email protected]>; Matt Ochs <[email protected]>; Krishnakant Jaju <[email protected]>; Dheeraj Nigam <[email protected]>; [email protected]; [email protected]
Subject: [RFC 11/14] vfio/nvgrace-egm: Fetch EGM region retired pages list

From: Ankit Agrawal <[email protected]>

It is possible for some system memory pages on the EGM to have retired pages with uncorrectable ECC errors. A list of pages known with such errors (referred as retired pages) are maintained by the Host UEFI. The Host UEFI populates such list in a reserved region. It communicates the SPA of this region through a ACPI DSDT property.

nvgrace-egm module is responsible to store the list of retired page offsets to be made available for usermode processes. The module:
1. Get the reserved memory region SPA and maps to it to fetch the list of bad pages.
2. Calculate the retired page offsets in the EGM and stores it.

---
 drivers/vfio/pci/nvgrace-gpu/egm.c     | 81 ++++++++++++++++++++++++++
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 32 ++++++++--
 drivers/vfio/pci/nvgrace-gpu/egm_dev.h |  5 +-
 drivers/vfio/pci/nvgrace-gpu/main.c    |  8 ++-
 include/linux/nvgrace-egm.h            |  2 +
 5 files changed, 118 insertions(+), 10 deletions(-)
drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/egm.c +0 -0

gpu/egm.c
index bf1241ed1d60..7a026b4d98f7 100644

drivers/vfio/pci/nvgrace-gpu/egm.c +39 -0
@@ -8,6 +8,11 @@
 
 #define MAX_EGM_NODES 4
 
+struct h_node {
+	unsigned long mem_offset;
+	struct hlist_node node;
+};
+
 static dev_t dev;
 static struct class *class;
 static DEFINE_XARRAY(egm_chardevs);
@@ -16,6 +21,7 @@ struct chardev {
 	struct device device;
 	struct cdev cdev;
 	atomic_t open_count;
+	DECLARE_HASHTABLE(htbl, 0x10);
 };
 
 static struct nvgrace_egm_dev *
@@ -145,20 +151,86 @@ static void del_egm_chardev(struct chardev
 *egm_chardev)
 	put_device(&egm_chardev->device);
 }
 
+static void cleanup_retired_pages(struct chardev *egm_chardev)
+{
+	struct h_node *cur_page;
+	unsigned long bkt;
+	struct hlist_node *temp_node;
+
+	hash_for_each_safe(egm_chardev->htbl, bkt, temp_node, cur_page,
 node) {
+		hash_del(&cur_page->node);
+		kvfree(cur_page);
+	}
+}
+
+static int nvgrace_egm_fetch_retired_pages(struct nvgrace_egm_dev
 *egm_dev,
+					   struct chardev *egm_chardev)
+{
+	u64 count;
+	void *memaddr;
+	int index, ret = 0;
+
+	memaddr = memremap(egm_dev->retiredpagesphys, PAGE_SIZE,
 MEMREMAP_WB);
+	if (!memaddr)
+		return -ENOMEM;
+
+	count = *(u64 *)memaddr;
+
+	for (index = 0; index < count; index++) {
+		struct h_node *retired_page;
+
+		/*
+		 * Since the EGM is linearly mapped, the offset in the
+		 * carveout is the same offset in the VM system memory.
+		 *
+		 * Calculate the offset to communicate to the usermode

drivers/vfio/pci/nvgrace-gpu/egm.c static void del_egm_chardev(struct chardev · line 200

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +10 -0
+		 * apps.
+		 */
+		retired_page = kvzalloc(sizeof(*retired_page), GFP_KERNEL);
+		if (!retired_page) {
+			ret = -ENOMEM;
+			break;
+		}
+
+		retired_page->mem_offset = *((u64 *)memaddr + index + 1) -
+					   egm_dev->egmphys;

Above the mapping is only for PAGE_SIZE and there is no check for count. There is a possibility here to access goes beyond the mapped area. Please check.

Quoted 49 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +25 -0
+		hash_add(egm_chardev->htbl, &retired_page->node,
+			 retired_page->mem_offset);
+	}
+
+	memunmap(memaddr);
+
+	if (ret)
+		cleanup_retired_pages(egm_chardev);
+
+	return ret;
+}
+
 static int egm_driver_probe(struct auxiliary_device *aux_dev,
 			    const struct auxiliary_device_id *id)
 {
 	struct nvgrace_egm_dev *egm_dev =
 		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
 	struct chardev *egm_chardev;
+	int ret;
 
 	egm_chardev = setup_egm_chardev(egm_dev);
 	if (!egm_chardev)
 		return -EINVAL;
 
+	hash_init(egm_chardev->htbl);
+
+	ret = nvgrace_egm_fetch_retired_pages(egm_dev, egm_chardev);
+	if (ret)
+		goto error_exit;
+
 	xa_store(&egm_chardevs, egm_dev->egmpxm, egm_chardev,
 GFP_KERNEL);
 
 	return 0;
+
+error_exit:
+	del_egm_chardev(egm_chardev);
+	return ret;
 }
 
 static void egm_driver_remove(struct auxiliary_device *aux_dev)
@@ -166,10 +238,19 @@ static void egm_driver_remove(struct
 auxiliary_device *aux_dev)
 	struct nvgrace_egm_dev *egm_dev =
 		container_of(aux_dev, struct nvgrace_egm_dev, aux_dev);
 	struct chardev *egm_chardev = xa_erase(&egm_chardevs, egm_dev-
 >egmpxm);
+	struct h_node *cur_page;
+	unsigned long bkt;

drivers/vfio/pci/nvgrace-gpu/egm.c static void egm_driver_remove(struct · line 254

Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +5 -0
+	struct hlist_node *temp_node;
 
 	if (!egm_chardev)
 		return;
 
+	hash_for_each_safe(egm_chardev->htbl, bkt, temp_node, cur_page,
 node) {
+		hash_del(&cur_page->node);
+		kvfree(cur_page);
+	}

The above is not required as the below cleanup also does the same thing.

Also, do we really need a hash table here? Since this is just storing page info and returning it via an IOCTL, a simple array or linked list would suffice. Or is there any plan to use it later for lookups?

Thanks,
Shameer

Quoted 156 lines
drivers/vfio/pci/nvgrace-gpu/egm.c +1 -0
+	cleanup_retired_pages(egm_chardev);
 	del_egm_chardev(egm_chardev);
 }
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +25 -5
index ca50bc1f67a0..b8e143542bce 100644
@@ -18,22 +18,41 @@ int nvgrace_gpu_has_egm_property(struct pci_dev
 *pdev, u64 *pegmpxm)
 }
 
 int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
-				   u64 *pegmlength)
+				   u64 *pegmlength, u64 *pretiredpagesphys)
 {
 	int ret;
 
 	/*
-	 * The memory information is present in the system ACPI tables as DSD
-	 * properties nvidia,egm-base-pa and nvidia,egm-size.
+	 * The EGM memory information is present in the system ACPI tables
+	 * as DSD properties nvidia,egm-base-pa and nvidia,egm-size.
 	 */
 	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-size",
 				       pegmlength);
 	if (ret)
-		return ret;
+		goto error_exit;
 
 	ret = device_property_read_u64(&pdev->dev, "nvidia,egm-base-pa",
 				       pegmphys);
+	if (ret)
+		goto error_exit;
+
+	/*
+	 * SBIOS puts the list of retired pages on a region. The region
+	 * SPA is exposed as "nvidia,egm-retired-pages-data-base".
+	 */
+	ret = device_property_read_u64(&pdev->dev,
+				       "nvidia,egm-retired-pages-data-base",
+				       pretiredpagesphys);
+	if (ret)
+		goto error_exit;
+
+	/* Catch firmware bug and avoid a crash */
+	if (*pretiredpagesphys == 0) {
+		dev_err(&pdev->dev, "Retired pages region is not setup\n");
+		ret = -EINVAL;
+	}
 
+error_exit:
 	return ret;
 }
@@ -74,7 +93,8 @@ static void nvgrace_gpu_release_aux_device(struct
 device *device)
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmphys, u64 egmlength, u64 egmpxm)
+			      u64 egmphys, u64 egmlength, u64 egmpxm,
+			      u64 retiredpagesphys)
 {
 	struct nvgrace_egm_dev *egm_dev;
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +2 -0
 	int ret;
@@ -86,6 +106,8 @@ nvgrace_gpu_create_aux_device(struct pci_dev *pdev,
 const char *name,
 	egm_dev->egmpxm = egmpxm;
 	egm_dev->egmphys = egmphys;
 	egm_dev->egmlength = egmlength;
+	egm_dev->retiredpagesphys = retiredpagesphys;
+
 	INIT_LIST_HEAD(&egm_dev->gpus);
 
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +0 -0
 	egm_dev->aux_dev.id = egmpxm;
drivers/vfio/pci/nvgrace-gpu/egm_dev.h +3 -2
index 2e1612445898..2f329a05685d 100644
@@ -16,8 +16,9 @@ void remove_gpu(struct nvgrace_egm_dev *egm_dev,
 struct pci_dev *pdev);
 
 struct nvgrace_egm_dev *
 nvgrace_gpu_create_aux_device(struct pci_dev *pdev, const char *name,
-			      u64 egmphys, u64 egmlength, u64 egmpxm);
+			      u64 egmphys, u64 egmlength, u64 egmpxm,
+			      u64 retiredpagesphys);
 
 int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
-				   u64 *pegmlength);
+				   u64 *pegmlength, u64 *pretiredpagesphys);

#endif /* EGM_DEV_H */

drivers/vfio/pci/nvgrace- renamed from drivers/vfio/pci/nvgrace-gpu/main.c +0 -0

gpu/main.c
index b1ccd1ac2e0a..534dc3ee6113 100644

drivers/vfio/pci/nvgrace-gpu/main.c +3 -2
@@ -67,7 +67,7 @@ static struct list_head egm_dev_list;
 static int nvgrace_gpu_create_egm_aux_device(struct pci_dev *pdev)
 {
 	struct nvgrace_egm_dev_entry *egm_entry = NULL;
-	u64 egmphys, egmlength, egmpxm;
+	u64 egmphys, egmlength, egmpxm, retiredpagesphys;
 	int ret = 0;
 	bool is_new_region = false;
 
@@ -80,7 +80,8 @@ static int nvgrace_gpu_create_egm_aux_device(struct
 pci_dev *pdev)
 	if (nvgrace_gpu_has_egm_property(pdev, &egmpxm))
 		goto exit;
 
-	ret = nvgrace_gpu_fetch_egm_property(pdev, &egmphys,
 &egmlength);
+	ret = nvgrace_gpu_fetch_egm_property(pdev, &egmphys,
 &egmlength,
+					     &retiredpagesphys);
drivers/vfio/pci/nvgrace-gpu/main.c +2 -1
 	if (ret)
 		goto exit;
@@ -103,7 +104,8 @@ static int nvgrace_gpu_create_egm_aux_device(struct
 pci_dev *pdev)
 
 	egm_entry->egm_dev =
 		nvgrace_gpu_create_aux_device(pdev,
 NVGRACE_EGM_DEV_NAME,
-					      egmphys, egmlength, egmpxm);
+					      egmphys, egmlength, egmpxm,
+					      retiredpagesphys);
 	if (!egm_entry->egm_dev) {
drivers/vfio/pci/nvgrace-gpu/main.c +0 -0
 		ret = -EINVAL;
 		goto free_egm_entry;
include/linux/nvgrace-egm.h +2 -1
index a66906753267..197255c2a3b7 100644
@@ -7,6 +7,7 @@
 #define NVGRACE_EGM_H
 
 #include <linux/auxiliary_bus.h>
+#include <linux/hashtable.h>
 
 #define NVGRACE_EGM_DEV_NAME "egm"
 
@@ -19,6 +20,7 @@ struct nvgrace_egm_dev {
 	struct auxiliary_device aux_dev;
 	phys_addr_t egmphys;
 	size_t egmlength;
+	phys_addr_t retiredpagesphys;
 	u64 egmpxm;
 	struct list_head gpus;
 };
--

2.34.1

author maintainer
#23 lore raw

RFC 12/14 vfio/nvgrace-egm: Introduce ioctl to share retired pages

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

nvgrace-egm module stores the list of retired page offsets to be made available for usermode processes. Introduce an ioctl to share the information with the userspace.

The ioctl is called by usermode apps such as QEMU to get the retired page offsets. The usermode apps are expected to take appropriate action to communicate the list to the VM.

---
 MAINTAINERS                        |  1 +
 drivers/vfio/pci/nvgrace-gpu/egm.c | 67 ++++++++++++++++++++++++++++++
 include/uapi/linux/egm.h           | 26 ++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 include/uapi/linux/egm.h
MAINTAINERS +1 -0
index ec6bc10f346d..bd2d2d309d92 100644
@@ -26481,6 +26481,7 @@ M:	Ankit Agrawal <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	drivers/vfio/pci/nvgrace-gpu/egm.c
+F:	include/uapi/linux/egm.h
 
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
drivers/vfio/pci/nvgrace-gpu/egm.c +67 -0

84 lines not on this page · raw message

include/uapi/linux/egm.h new file +26 -0
new file mode 100644
index 000000000000..d157fbb5e305
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#ifndef _UAPIEGM_H
+#define _UAPIEGM_H
+
+#define EGM_TYPE ('E')
+
+struct egm_retired_pages_info {
+	__aligned_u64 offset;
+	__aligned_u64 size;
+};
+
+struct egm_retired_pages_list {
+	__u32 argsz;
+	/* out */
+	__u32 count;
+	/* out */
+	struct egm_retired_pages_info retired_pages[];
+};
+
+#define EGM_RETIRED_PAGES_LIST     _IO(EGM_TYPE, 100)
+
+#endif /* _UAPIEGM_H */
Signature · 1 line
2.34.1
#24 lore raw

Re: [RFC 12/14] vfio/nvgrace-egm: Introduce ioctl to share retired pages

kernel test robot noticed the following build errors:

Hi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on awilliam-vfio/next]
[also build test ERROR on linus/master awilliam-vfio/for-linus v6.17-rc4 next-20250905] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/ankita-nvidia-com/vfio-nvgrace-gpu-Expand-module_pci_driver-to-allow-custom-module-init/20250904-121328
base:   https://github.com/awilliam/linux-vfio.git next
patch link:    https://lore.kernel.org/r/20250904040828.319452-13-ankita%40nvidia.com
patch subject: [RFC 12/14] vfio/nvgrace-egm: Introduce ioctl to share retired pages
config: i386-buildonly-randconfig-002-20250905 (https://download.01.org/0day-ci/archive/20250905/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250905/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from <built-in>:1:

>> ./usr/include/linux/egm.h:12:2: error: unknown type name '__aligned_u64'
      12 |         __aligned_u64 offset;
         |         ^
   ./usr/include/linux/egm.h:13:2: error: unknown type name '__aligned_u64'
      13 |         __aligned_u64 size;
         |         ^
>> ./usr/include/linux/egm.h:17:2: error: unknown type name '__u32'
      17 |         __u32 argsz;
         |         ^
   ./usr/include/linux/egm.h:19:2: error: unknown type name '__u32'
      19 |         __u32 count;
         |         ^

4 errors generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#25 lore raw

RFC 13/14 vfio/nvgrace-egm: expose the egm size through sysfs

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

To allocate the EGM, the userspace need to know its size. Currently, there is no easy way for the userspace to determine that.

Make nvgrace-egm expose the size through sysfs that can be queried by the userspace from <aux_dev_path>/egm_size.

On a 2-socket, 4 GPU Grace Blackwell setup, it shows up as:
Socket0:
/sys/devices/pci0008:00/0008:00:00.0/0008:01:00.0/nvgrace_gpu_vfio_pci.egm.4/egm/egm4/egm_size /sys/devices/pci0009:00/0009:00:00.0/0009:01:00.0/nvgrace_gpu_vfio_pci.egm.4/egm/egm4/egm_size

Socket1:
/sys/devices/pci0018:00/0018:00:00.0/0018:01:00.0/nvgrace_gpu_vfio_pci.egm.5/egm/egm5/egm_size /sys/devices/pci0019:00/0019:00:00.0/0019:01:00.0/nvgrace_gpu_vfio_pci.egm.5/egm/egm5/egm_size

---
 drivers/vfio/pci/nvgrace-gpu/egm.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/vfio/pci/nvgrace-gpu/egm.c +27 -0
index 2cb100e39c4b..346607eeb0f9 100644
@@ -343,6 +343,32 @@ static char *egm_devnode(const struct device *device, umode_t *mode)
 	return NULL;
 }
 
+static ssize_t egm_size_show(struct device *dev, struct device_attribute *attr,
+			     char *buf)
+{
+	struct chardev *egm_chardev = container_of(dev, struct chardev, device);
+	struct nvgrace_egm_dev *egm_dev =
+		egm_chardev_to_nvgrace_egm_dev(egm_chardev);
+
+	return sysfs_emit(buf, "0x%lx\n", egm_dev->egmlength);
+}
+
+static DEVICE_ATTR_RO(egm_size);
+
+static struct attribute *attrs[] = {
+	&dev_attr_egm_size.attr,
+	NULL,
+};
+
+static struct attribute_group attr_group = {
+	.attrs = attrs,
+};
+
+static const struct attribute_group *attr_groups[2] = {
+	&attr_group,
+	NULL
+};
+
 static int __init nvgrace_egm_init(void)
 {
 	int ret;
@@ -364,6 +390,7 @@ static int __init nvgrace_egm_init(void)
 	}
 
 	class->devnode = egm_devnode;
+	class->dev_groups = attr_groups;
 
 	ret = auxiliary_driver_register(&egm_driver);
 	if (!ret)
Signature · 1 line
2.34.1
author maintainer
#26 lore raw

RFC 14/14 vfio/nvgrace-gpu: Add link from pci to EGM

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

To replicate the host EGM topology in the VM in terms of the GPU affinity, the userspace need to be aware of which GPUs belong to the same socket as the EGM region.

Expose the list of GPUs associated with an EGM region through sysfs. The list can be queried from the auxiliary device path.

On a 2-socket, 4 GPU Grace Blackwell setup, it shows up as the following:
/sys/devices/pci0008:00/0008:00:00.0/0008:01:00.0/nvgrace_gpu_vfio_pci.egm.4 /sys/devices/pci0009:00/0009:00:00.0/0009:01:00.0/nvgrace_gpu_vfio_pci.egm.4 pointing to egm4.

/sys/devices/pci0018:00/0018:00:00.0/0018:01:00.0/nvgrace_gpu_vfio_pci.egm.5 /sys/devices/pci0019:00/0019:00:00.0/0019:01:00.0/nvgrace_gpu_vfio_pci.egm.5 pointing to egm5.

Moreover /sys/devices/pci0008:00/0008:00:00.0/0008:01:00.0/nvgrace_gpu_vfio_pci.egm.4 /sys/devices/pci0009:00/0009:00:00.0/0009:01:00.0/nvgrace_gpu_vfio_pci.egm.4 lists links to both the 0008:01:00.0 & 0009:01:00.0 GPU devices.

and /sys/devices/pci0018:00/0018:00:00.0/0018:01:00.0/nvgrace_gpu_vfio_pci.egm.5 /sys/devices/pci0019:00/0019:00:00.0/0019:01:00.0/nvgrace_gpu_vfio_pci.egm.5 lists links to both the 0018:01:00.0 & 0019:01:00.0.

---
 drivers/vfio/pci/nvgrace-gpu/egm_dev.c | 42 +++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
drivers/vfio/pci/nvgrace-gpu/egm_dev.c +41 -1
index b8e143542bce..20e9213aa0ac 100644
@@ -56,6 +56,36 @@ int nvgrace_gpu_fetch_egm_property(struct pci_dev *pdev, u64 *pegmphys,
 	return ret;
 }
 
+static int create_egm_symlinks(struct nvgrace_egm_dev *egm_dev,
+			       struct pci_dev *pdev)
+{
+	int ret_l1, ret_l2;
+
+	ret_l1 = sysfs_create_link_nowarn(&pdev->dev.kobj,
+					  &egm_dev->aux_dev.dev.kobj,
+					  dev_name(&egm_dev->aux_dev.dev));
+
+	/*
+	 * Allow if Link already exists - created since GPU is the auxiliary
+	 * device's parent; flag the error otherwise.
+	 */
+	if (ret_l1 && ret_l1 != -EEXIST)
+		return ret_l1;
+
+	ret_l2 = sysfs_create_link(&egm_dev->aux_dev.dev.kobj,
+				   &pdev->dev.kobj,
+				   dev_name(&pdev->dev));
+
+	/*
+	 * Remove the aux dev link only if wasn't already present.
+	 */
+	if (ret_l2 && !ret_l1)
+		sysfs_remove_link(&pdev->dev.kobj,
+				  dev_name(&egm_dev->aux_dev.dev));
+
+	return ret_l2;
+}
+
 int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
 {
 	struct gpu_node *node;
@@ -68,7 +98,16 @@ int add_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
 
 	list_add_tail(&node->list, &egm_dev->gpus);
 
-	return 0;
+	return create_egm_symlinks(egm_dev, pdev);
+}
+
+static void remove_egm_symlinks(struct nvgrace_egm_dev *egm_dev,
+				struct pci_dev *pdev)
+{
+	sysfs_remove_link(&pdev->dev.kobj,
+			  dev_name(&egm_dev->aux_dev.dev));
+	sysfs_remove_link(&egm_dev->aux_dev.dev.kobj,
+			  dev_name(&pdev->dev));
 }
 
 void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
@@ -77,6 +116,7 @@ void remove_gpu(struct nvgrace_egm_dev *egm_dev, struct pci_dev *pdev)
 
 	list_for_each_entry_safe(node, tmp, &egm_dev->gpus, list) {
 		if (node->pdev == pdev) {
+			remove_egm_symlinks(egm_dev, pdev);
 			list_del(&node->list);
 			kvfree(node);
 		}
Signature · 1 line
2.34.1
#27 lore raw

Re: [RFC 14/14] vfio/nvgrace-gpu: Add link from pci to EGM

Quoted 24 lines · On Thu, Sep 04, 2025 at 04:08:28AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

To replicate the host EGM topology in the VM in terms of the GPU affinity, the userspace need to be aware of which GPUs belong to the same socket as the EGM region.

Expose the list of GPUs associated with an EGM region through sysfs. The list can be queried from the auxiliary device path.

On a 2-socket, 4 GPU Grace Blackwell setup, it shows up as the following:
/sys/devices/pci0008:00/0008:00:00.0/0008:01:00.0/nvgrace_gpu_vfio_pci.egm.4 /sys/devices/pci0009:00/0009:00:00.0/0009:01:00.0/nvgrace_gpu_vfio_pci.egm.4 pointing to egm4.

/sys/devices/pci0018:00/0018:00:00.0/0018:01:00.0/nvgrace_gpu_vfio_pci.egm.5 /sys/devices/pci0019:00/0019:00:00.0/0019:01:00.0/nvgrace_gpu_vfio_pci.egm.5 pointing to egm5.

Moreover /sys/devices/pci0008:00/0008:00:00.0/0008:01:00.0/nvgrace_gpu_vfio_pci.egm.4 /sys/devices/pci0009:00/0009:00:00.0/0009:01:00.0/nvgrace_gpu_vfio_pci.egm.4 lists links to both the 0008:01:00.0 & 0009:01:00.0 GPU devices.

Quoted 4 lines

and /sys/devices/pci0018:00/0018:00:00.0/0018:01:00.0/nvgrace_gpu_vfio_pci.egm.5 /sys/devices/pci0019:00/0019:00:00.0/0019:01:00.0/nvgrace_gpu_vfio_pci.egm.5 lists links to both the 0018:01:00.0 & 0019:01:00.0.

This seems backwards, I would rather the egm chardev itself have a directory of links to the PCI devices not have EGM manipulate the sysfs belonging to some other driver and subsystem..

Jason