Skip to contentSkip to conversation

gpu/cuda: expose GPU memory with GDRCopy

PATCH Superseded [email protected] 2 participants 8 messages Jan 11, 2022 – Feb 27, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover gpu/cuda: expose GPU memory with GDRCopy

From: Elena Agostini <[email protected]>

GPU CUDA implementation of the new gpudev functions to expose GPU memory to the CPU.

Today GDRCopy library is required to pin and DMA map the GPU memory through the BAR1 of the GPU and expose it to the CPU.

Goal here is to hide technical details GDRCopy library and expose the functionality through the generic gpudev layer.

GDRCopy can be found here: https://github.com/NVIDIA/gdrcopy

To build GPU CUDA driver with GDRCopy, you need to build DPDK indicating the gdrapi.h header file with -Dc_args="-I/path/to/gdrapi/".

To execute you need to indicate the path to libgdrapi.so library with the environment variable GDRCOPY_PATH_L=/path/to/gdrcopy/lib/

If GDRCopy is not built with GPU CUDA driver, the GPU expose functionality will not be supported by the driver.

This is an indipendent feature.
All the other GPU CUDA driver capabilities are not affected if GDRCopy is not built.

---
Dependency on https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Elena Agostini (1):
gpu/cuda: expose GPU memory with GDRCopy

 drivers/gpu/cuda/cuda.c      | 101 +++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.c   | 139 +++++++++++++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.h   |  29 ++++++++
 drivers/gpu/cuda/meson.build |   6 +-
 4 files changed, 274 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/cuda/gdrcopy.c
 create mode 100644 drivers/gpu/cuda/gdrcopy.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 gpu/cuda: expose GPU memory with GDRCopy

From: Elena Agostini <[email protected]>

GPU CUDA implementation of the new gpudev functions to expose GPU memory to the CPU.

Today GDRCopy library is required to pin and DMA map the GPU memory through the BAR1 of the GPU and expose it to the CPU.

Goal here is to hide technical details GDRCopy library and expose the functionality through the generic gpudev layer.

GDRCopy can be found here: https://github.com/NVIDIA/gdrcopy

To build GPU CUDA driver with GDRCopy, you need to build DPDK indicating the gdrapi.h header file with -Dc_args="-I/path/to/gdrapi/".

To execute you need to indicate the path to libgdrapi.so library with the environment variable GDRCOPY_PATH_L=/path/to/gdrcopy/lib/

If GDRCopy is not built with GPU CUDA driver, the GPU expose functionality will not be supported by the driver.

This is an indipendent feature.
All the other GPU CUDA driver capabilities are not affected if GDRCopy is not built.

---
Dependency on https://patches.dpdk.org/project/dpdk/patch/[email protected]/

---
 drivers/gpu/cuda/cuda.c      | 101 +++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.c   | 139 +++++++++++++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.h   |  29 ++++++++
 drivers/gpu/cuda/meson.build |   6 +-
 4 files changed, 274 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/cuda/gdrcopy.c
 create mode 100644 drivers/gpu/cuda/gdrcopy.h
drivers/gpu/cuda/cuda.c +101 -0
index 882df08e56..d66d6b76b9 100644
@@ -17,6 +17,8 @@
 #include <cuda.h>
 #include <cudaTypedefs.h>
 
+#include "gdrcopy.h"
+
 #define CUDA_DRIVER_MIN_VERSION 11040
 #define CUDA_API_MIN_VERSION 3020
 
@@ -52,6 +54,8 @@ static void *cudalib;
 static unsigned int cuda_api_version;
 static int cuda_driver_version;
 
+static gdr_t gdrc_h;
+
 /* NVIDIA GPU vendor */
 #define NVIDIA_GPU_VENDOR_ID (0x10de)
 
@@ -144,6 +148,7 @@ struct mem_entry {
 	struct rte_gpu *dev;
 	CUcontext ctx;
 	cuda_ptr_key pkey;
+	gdr_mh_t mh;
 	enum mem_type mtype;
 	struct mem_entry *prev;
 	struct mem_entry *next;
@@ -943,6 +948,87 @@ cuda_wmb(struct rte_gpu *dev)
 	return 0;
 }
 
+static int
+cuda_mem_expose(struct rte_gpu *dev, __rte_unused size_t size, void *ptr_in, void **ptr_out)
+{
+	struct mem_entry *mem_item;
+	cuda_ptr_key hk;
+
+	if (dev == NULL)
+		return -ENODEV;
+
+	if (gdrc_h == NULL) {
+		rte_cuda_log(ERR, "GDRCopy not built or loaded. Can't expose GPU memory.");
+		rte_errno = ENOTSUP;
+		return -rte_errno;
+	}
+
+	hk = get_hash_from_ptr((void *)ptr_in);
+
+	mem_item = mem_list_find_item(hk);
+	if (mem_item == NULL) {
+		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory.", ptr_in);
+		rte_errno = EPERM;
+		return -rte_errno;
+	}
+
+	if (mem_item->mtype == GPU_MEM) {
+		rte_cuda_log(ERR, "Memory address 0x%p is not GPU memory type.", ptr_in);
+		rte_errno = EPERM;
+		return -rte_errno;
+	}
+
+	if (mem_item->size != size)
+		rte_cuda_log(WARNING,
+				"Can't expose memory area with size (%zd) different from original size (%zd).",
+				size, mem_item->size);
+
+	if (gdrcopy_pin(gdrc_h, &(mem_item->mh), (uint64_t)mem_item->ptr_d,
+			mem_item->size, &(mem_item->ptr_h))) {
+		rte_cuda_log(ERR, "Error exposing GPU memory address 0x%p.", ptr_in);
+		rte_errno = EPERM;
+		return -rte_errno;
+	}
+
+	*ptr_out = mem_item->ptr_h;
+
+	return 0;
+}
+
+static int
+cuda_mem_unexpose(struct rte_gpu *dev, void *ptr_in)
+{
+	struct mem_entry *mem_item;
+	cuda_ptr_key hk;
+
+	if (dev == NULL)
+		return -ENODEV;
+
+	if (gdrc_h == NULL) {
+		rte_cuda_log(ERR, "GDRCopy not built or loaded. Can't unexpose GPU memory.");
+		rte_errno = ENOTSUP;
+		return -rte_errno;
+	}
+
+	hk = get_hash_from_ptr((void *)ptr_in);
+
+	mem_item = mem_list_find_item(hk);
+	if (mem_item == NULL) {
+		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory.", ptr_in);
+		rte_errno = EPERM;
+		return -rte_errno;
+	}
+
+	if (gdrcopy_unpin(gdrc_h, mem_item->mh, (void *)mem_item->ptr_d,
+			mem_item->size)) {
+		rte_cuda_log(ERR, "Error unexposing GPU memory address 0x%p.", ptr_in);
+		rte_errno = EPERM;
+		return -rte_errno;
+	}
+
+	return 0;
+}
+
 static int
 cuda_gpu_probe(__rte_unused struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 {
@@ -1018,6 +1104,19 @@ cuda_gpu_probe(__rte_unused struct rte_pci_driver *pci_drv, struct rte_pci_devic
 			rte_errno = ENOTSUP;
 			return -rte_errno;
 		}
+
+		gdrc_h = NULL;
+
+		#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H
+			if (gdrcopy_loader())
+				rte_cuda_log(ERR, "GDRCopy shared library not found.\n");
+			else {
+				if (gdrcopy_open(&gdrc_h))
+					rte_cuda_log(ERR, "GDRCopy handler can't be created. Is gdrdrv driver installed and loaded?\n");
+			}
+		#else
+			gdrc_h = NULL;
+		#endif
 	}
 
 	/* Fill HW specific part of device structure */
@@ -1160,6 +1259,8 @@ cuda_gpu_probe(__rte_unused struct rte_pci_driver *pci_drv, struct rte_pci_devic
 	dev->ops.mem_free = cuda_mem_free;
 	dev->ops.mem_register = cuda_mem_register;
 	dev->ops.mem_unregister = cuda_mem_unregister;
+	dev->ops.mem_expose = cuda_mem_expose;
+	dev->ops.mem_unexpose = cuda_mem_unexpose;
 	dev->ops.wmb = cuda_wmb;
 
 	rte_gpu_complete_new(dev);
drivers/gpu/cuda/gdrcopy.c new file +139 -0

139 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.h new file +29 -0
new file mode 100644
index 0000000000..e5c1997731
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2022 NVIDIA Corporation & Affiliates
+ */
+
+#ifndef _CUDA_GDRCOPY_H_
+#define _CUDA_GDRCOPY_H_
+
+#include <dlfcn.h>
+
+#include <rte_common.h>
+#include <rte_log.h>
+#include <rte_errno.h>
+
+#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H
+	#include <gdrapi.h>
+#else
+	struct gdr;
+	typedef struct gdr *gdr_t;
+	struct gdr_mh_s;
+	typedef struct gdr_mh_s gdr_mh_t;
+#endif
+
+int gdrcopy_loader(void);
+int gdrcopy_open(gdr_t *g);
+int gdrcopy_close(gdr_t *g);
+int gdrcopy_pin(gdr_t g, gdr_mh_t *mh, uint64_t d_addr, size_t size, void **h_addr);
+int gdrcopy_unpin(gdr_t g, gdr_mh_t mh, void *d_addr, size_t size);
+
+#endif
drivers/gpu/cuda/meson.build +5 -1
index 3fe20929fa..784fa8bf0d 100644
@@ -17,5 +17,9 @@ if not cc.has_header('cudaTypedefs.h')
         subdir_done()
 endif
 
+if cc.has_header('gdrapi.h')
+        dpdk_conf.set('DRIVERS_GPU_CUDA_GDRCOPY_H', 1)
+endif
+
 deps += ['gpudev', 'pci', 'bus_pci']
-sources = files('cuda.c')
+sources = files('cuda.c', 'gdrcopy.c')
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/1 gpu/cuda: CPU map GPU memory with GDRCopy

In reply to [email protected]

From: Elena Agostini <[email protected]>

To enable the gpudev rte_gpu_mem_cpu_map feature to expose GPU memory to the CPU, the GPU CUDA driver library needs the GDRCopy library and driver.

If DPDK is built without GDRCopy, the GPU CUDA driver returns error if the is invoked rte_gpu_mem_cpu_map.

All the others GPU CUDA driver functionalities are not affected by the absence of GDRCopy, thus this is an optional functionality that can be enabled in the GPU CUDA driver.

CUDA driver documentation has been updated accordingly.

---
 doc/guides/gpus/cuda.rst             |  52 +++++++++
 doc/guides/gpus/features/default.ini |   2 +
 drivers/gpu/cuda/cuda.c              |  78 +++++++++++++-
 drivers/gpu/cuda/gdrcopy.c           | 154 +++++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.h           |  27 +++++
 drivers/gpu/cuda/meson.build         |   6 +-
 6 files changed, 316 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/cuda/gdrcopy.c
 create mode 100644 drivers/gpu/cuda/gdrcopy.h
doc/guides/gpus/cuda.rst +52 -0
index 38e22dc2c0..edf6f93008 100644
@@ -29,6 +29,34 @@ Three ways:
 
 If headers are not found, the CUDA GPU driver library is not built.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~
+
+To enable this gpudev feature (i.e. implement the ``rte_gpu_mem_cpu_map``),
+you need the `GDRCopy <https://github.com/NVIDIA/gdrcopy>`_ library and driver
+installed on your system.
+
+A quick recipe to download, build and run GDRCopy library and driver:
+
+.. code-block:: console
+
+  $ git clone https://github.com/NVIDIA/gdrcopy.git
+  $ make
+  $ # make install to install GDRCopy library system wide
+  $ # Launch gdrdrv kernel module on the system
+  $ sudo ./insmod.sh
+
+You need to indicate to meson where GDRCopy headers files as in case of CUDA headers.
+An example would be:
+
+.. code-block:: console
+
+  $ meson build -Dc_args="-I/usr/local/cuda/include -I/path/to/gdrcopy/include"
+
+If headers are not found, the CUDA GPU driver library is built without the CPU map capability
+and will retun error if the application invokes the gpude ``rte_gpu_mem_cpu_map`` function.
+
+
 CUDA Shared Library
 -------------------
 
@@ -46,6 +74,30 @@ All CUDA API symbols are loaded at runtime as well.
 For this reason, to build the CUDA driver library,
 no need to install the CUDA library.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~
+
+Similarly to CUDA shared library, if the **libgdrapi.so** shared library is not
+installed in default locations (e.g. /usr/local/lib) you can use the
+``GDRCOPY_PATH_L``.
+
+As an example, to enable the CPU map feature sanity check, run the ``app/test-gpudev``
+application with:
+
+.. code-block:: console
+
+  $ sudo CUDA_PATH_L=/path/to/libcuda GDRCOPY_PATH_L=/path/to/libgdrapi ./build/app/dpdk-test-gpudev
+
+Additionally, the ``gdrdrv`` kernel module built with the GDRCopy project has to loaded
+on the system:
+
+.. code-block:: console
+
+  $ lsmod | egrep gdrdrv
+  gdrdrv                 20480  0
+  nvidia              35307520  19 nvidia_uvm,nv_peer_mem,gdrdrv,nvidia_modeset
+
+
 Design
 ------
 
doc/guides/gpus/features/default.ini +2 -0
index 87e9966424..817113f2c2 100644
@@ -11,3 +11,5 @@ Get device info                =
 Share CPU memory with device   =
 Allocate device memory         =
 Free memory                    =
+CPU map device memory          =
+CPU unmap device memory        =
drivers/gpu/cuda/cuda.c +76 -2

120 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.c new file +154 -0

154 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.h new file +27 -0
new file mode 100644
index 0000000000..11960424c9
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2022 NVIDIA Corporation & Affiliates
+ */
+
+#ifndef _CUDA_GDRCOPY_H_
+#define _CUDA_GDRCOPY_H_
+
+#include <dlfcn.h>
+
+#include <rte_common.h>
+#include <rte_log.h>
+#include <rte_errno.h>
+
+#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H
+	#include <gdrapi.h>
+#else
+	struct gdr;
+	typedef struct gdr *gdr_t;
+	struct gdr_mh_s;
+	typedef struct gdr_mh_s gdr_mh_t;
+#endif
+
+int gdrcopy_pin(gdr_t *gdrc_h, __rte_unused gdr_mh_t *mh, uint64_t d_addr, size_t size, void **h_addr);
+int gdrcopy_unpin(gdr_t gdrc_h, __rte_unused gdr_mh_t mh, void *d_addr, size_t size);
+
+#endif
+
drivers/gpu/cuda/meson.build +5 -1
index 3fe20929fa..784fa8bf0d 100644
@@ -17,5 +17,9 @@ if not cc.has_header('cudaTypedefs.h')
         subdir_done()
 endif
 
+if cc.has_header('gdrapi.h')
+        dpdk_conf.set('DRIVERS_GPU_CUDA_GDRCOPY_H', 1)
+endif
+
 deps += ['gpudev', 'pci', 'bus_pci']
-sources = files('cuda.c')
+sources = files('cuda.c', 'gdrcopy.c')
Signature · 1 line
2.17.1
#3 lore raw

PATCH v3 1/1 gpu/cuda: CPU map GPU memory with GDRCopy

From: Elena Agostini <[email protected]>

To enable the gpudev rte_gpu_mem_cpu_map feature to expose GPU memory to the CPU, the GPU CUDA driver library needs the GDRCopy library and driver.

If DPDK is built without GDRCopy, the GPU CUDA driver returns error if the is invoked rte_gpu_mem_cpu_map.

All the others GPU CUDA driver functionalities are not affected by the absence of GDRCopy, thus this is an optional functionality that can be enabled in the GPU CUDA driver.

CUDA driver documentation has been updated accordingly.

----

Changelog:
- Fix checkpatch and doc build issue

---
 doc/guides/gpus/cuda.rst             |  52 +++++++++
 doc/guides/gpus/features/default.ini |   2 +
 drivers/gpu/cuda/cuda.c              |  78 +++++++++++++-
 drivers/gpu/cuda/gdrcopy.c           | 155 +++++++++++++++++++++++++++
 drivers/gpu/cuda/gdrcopy.h           |  29 +++++
 drivers/gpu/cuda/meson.build         |   6 +-
 6 files changed, 319 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/cuda/gdrcopy.c
 create mode 100644 drivers/gpu/cuda/gdrcopy.h
doc/guides/gpus/cuda.rst +52 -0
index 38e22dc2c0..5d4fe02d77 100644
@@ -29,6 +29,34 @@ Three ways:
 
 If headers are not found, the CUDA GPU driver library is not built.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~~
+
+To enable this gpudev feature (i.e. implement the ``rte_gpu_mem_cpu_map``),
+you need the `GDRCopy <https://github.com/NVIDIA/gdrcopy>`_ library and driver
+installed on your system.
+
+A quick recipe to download, build and run GDRCopy library and driver:
+
+.. code-block:: console
+
+  $ git clone https://github.com/NVIDIA/gdrcopy.git
+  $ make
+  $ # make install to install GDRCopy library system wide
+  $ # Launch gdrdrv kernel module on the system
+  $ sudo ./insmod.sh
+
+You need to indicate to meson where GDRCopy headers files as in case of CUDA headers.
+An example would be:
+
+.. code-block:: console
+
+  $ meson build -Dc_args="-I/usr/local/cuda/include -I/path/to/gdrcopy/include"
+
+If headers are not found, the CUDA GPU driver library is built without the CPU map capability
+and will return error if the application invokes the gpude ``rte_gpu_mem_cpu_map`` function.
+
+
 CUDA Shared Library
 -------------------
 
@@ -46,6 +74,30 @@ All CUDA API symbols are loaded at runtime as well.
 For this reason, to build the CUDA driver library,
 no need to install the CUDA library.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~~
+
+Similarly to CUDA shared library, if the **libgdrapi.so** shared library is not
+installed in default locations (e.g. /usr/local/lib) you can use the
+``GDRCOPY_PATH_L``.
+
+As an example, to enable the CPU map feature sanity check, run the ``app/test-gpudev``
+application with:
+
+.. code-block:: console
+
+  $ sudo CUDA_PATH_L=/path/to/libcuda GDRCOPY_PATH_L=/path/to/libgdrapi ./build/app/dpdk-test-gpudev
+
+Additionally, the ``gdrdrv`` kernel module built with the GDRCopy project has to loaded
+on the system:
+
+.. code-block:: console
+
+  $ lsmod | egrep gdrdrv
+  gdrdrv                 20480  0
+  nvidia              35307520  19 nvidia_uvm,nv_peer_mem,gdrdrv,nvidia_modeset
+
+
 Design
 ------
 
doc/guides/gpus/features/default.ini +2 -0
index 87e9966424..817113f2c2 100644
@@ -11,3 +11,5 @@ Get device info                =
 Share CPU memory with device   =
 Allocate device memory         =
 Free memory                    =
+CPU map device memory          =
+CPU unmap device memory        =
drivers/gpu/cuda/cuda.c +76 -2

120 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.c new file +155 -0

155 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.h new file +29 -0
new file mode 100644
index 0000000000..68b80de805
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2022 NVIDIA Corporation & Affiliates
+ */
+
+#ifndef _CUDA_GDRCOPY_H_
+#define _CUDA_GDRCOPY_H_
+
+#include <dlfcn.h>
+
+#include <rte_common.h>
+#include <rte_log.h>
+#include <rte_errno.h>
+
+#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H
+	#include <gdrapi.h>
+#else
+	struct gdr;
+	typedef struct gdr *gdr_t;
+	struct gdr_mh_s;
+	typedef struct gdr_mh_s gdr_mh_t;
+#endif
+
+int gdrcopy_pin(gdr_t *gdrc_h, __rte_unused gdr_mh_t *mh,
+		uint64_t d_addr, size_t size, void **h_addr);
+int gdrcopy_unpin(gdr_t gdrc_h, __rte_unused gdr_mh_t mh,
+		void *d_addr, size_t size);
+
+#endif
+
drivers/gpu/cuda/meson.build +5 -1
index 3fe20929fa..784fa8bf0d 100644
@@ -17,5 +17,9 @@ if not cc.has_header('cudaTypedefs.h')
         subdir_done()
 endif
 
+if cc.has_header('gdrapi.h')
+        dpdk_conf.set('DRIVERS_GPU_CUDA_GDRCOPY_H', 1)
+endif
+
 deps += ['gpudev', 'pci', 'bus_pci']
-sources = files('cuda.c')
+sources = files('cuda.c', 'gdrcopy.c')
Signature · 1 line
2.17.1
#4 lore raw

PATCH v4 1/2 doc/gpus: add cuda.ini into features

In reply to [email protected]

From: Elena Agostini <[email protected]>

---
 doc/guides/gpus/features/cuda.ini | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 doc/guides/gpus/features/cuda.ini
doc/guides/gpus/features/cuda.ini new file +12 -0
new file mode 100644
index 0000000000..eb1aff9a80
@@ -0,0 +1,12 @@
+;
+; Supported features of the 'cuda' gpu driver.
+;
+; Refer to default.ini for the full list of available PMD features.
+;
+[Features]
+Get device info                = Y
+Share CPU memory with device   = Y
+Allocate device memory         = Y
+Free memory                    = Y
+CPU map device memory          = Y
+CPU unmap device memory        = Y
Signature · 1 line
2.17.1
#5 lore raw

PATCH v4 2/2 gpu/cuda: CPU map GPU memory with GDRCopy

From: Elena Agostini <[email protected]>

To enable the gpudev rte_gpu_mem_cpu_map feature to expose GPU memory to the CPU, the GPU CUDA driver library needs the GDRCopy library and driver.

If DPDK is built without GDRCopy, the GPU CUDA driver returns error if the is invoked rte_gpu_mem_cpu_map.

All the others GPU CUDA driver functionalities are not affected by the absence of GDRCopy, thus this is an optional functionality that can be enabled in the GPU CUDA driver.

CUDA driver documentation has been updated accordingly.

----

Changelog:
- Fix checkpatch and doc build issue
- Added common header to cuda.c and gdrcopy.c

---
 doc/guides/gpus/cuda.rst             |  52 ++++++++++
 doc/guides/gpus/features/default.ini |   2 +
 drivers/gpu/cuda/common.h            |  39 +++++++
 drivers/gpu/cuda/cuda.c              |  90 +++++++++++++---
 drivers/gpu/cuda/gdrcopy.c           | 148 +++++++++++++++++++++++++++
 drivers/gpu/cuda/meson.build         |   6 +-
 6 files changed, 322 insertions(+), 15 deletions(-)
 create mode 100644 drivers/gpu/cuda/common.h
 create mode 100644 drivers/gpu/cuda/gdrcopy.c
doc/guides/gpus/cuda.rst +52 -0
index 38e22dc2c0..5d4fe02d77 100644
@@ -29,6 +29,34 @@ Three ways:
 
 If headers are not found, the CUDA GPU driver library is not built.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~~
+
+To enable this gpudev feature (i.e. implement the ``rte_gpu_mem_cpu_map``),
+you need the `GDRCopy <https://github.com/NVIDIA/gdrcopy>`_ library and driver
+installed on your system.
+
+A quick recipe to download, build and run GDRCopy library and driver:
+
+.. code-block:: console
+
+  $ git clone https://github.com/NVIDIA/gdrcopy.git
+  $ make
+  $ # make install to install GDRCopy library system wide
+  $ # Launch gdrdrv kernel module on the system
+  $ sudo ./insmod.sh
+
+You need to indicate to meson where GDRCopy headers files as in case of CUDA headers.
+An example would be:
+
+.. code-block:: console
+
+  $ meson build -Dc_args="-I/usr/local/cuda/include -I/path/to/gdrcopy/include"
+
+If headers are not found, the CUDA GPU driver library is built without the CPU map capability
+and will return error if the application invokes the gpude ``rte_gpu_mem_cpu_map`` function.
+
+
 CUDA Shared Library
 -------------------
 
@@ -46,6 +74,30 @@ All CUDA API symbols are loaded at runtime as well.
 For this reason, to build the CUDA driver library,
 no need to install the CUDA library.
 
+CPU map GPU memory
+~~~~~~~~~~~~~~~~~~
+
+Similarly to CUDA shared library, if the **libgdrapi.so** shared library is not
+installed in default locations (e.g. /usr/local/lib) you can use the
+``GDRCOPY_PATH_L``.
+
+As an example, to enable the CPU map feature sanity check, run the ``app/test-gpudev``
+application with:
+
+.. code-block:: console
+
+  $ sudo CUDA_PATH_L=/path/to/libcuda GDRCOPY_PATH_L=/path/to/libgdrapi ./build/app/dpdk-test-gpudev
+
+Additionally, the ``gdrdrv`` kernel module built with the GDRCopy project has to loaded
+on the system:
+
+.. code-block:: console
+
+  $ lsmod | egrep gdrdrv
+  gdrdrv                 20480  0
+  nvidia              35307520  19 nvidia_uvm,nv_peer_mem,gdrdrv,nvidia_modeset
+
+
 Design
 ------
 
doc/guides/gpus/features/default.ini +2 -0
index 87e9966424..817113f2c2 100644
@@ -11,3 +11,5 @@ Get device info                =
 Share CPU memory with device   =
 Allocate device memory         =
 Free memory                    =
+CPU map device memory          =
+CPU unmap device memory        =
drivers/gpu/cuda/common.h new file +39 -0
new file mode 100644
index 0000000000..323bbacd6e
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2022 NVIDIA Corporation & Affiliates
+ */
+
+#ifndef _CUDA_COMMON_H_
+#define _CUDA_COMMON_H_
+
+#include <dlfcn.h>
+
+#include <rte_common.h>
+#include <rte_log.h>
+#include <rte_errno.h>
+
+RTE_LOG_REGISTER_DEFAULT(cuda_logtype, NOTICE);
+
+/* Helper macro for logging */
+#define rte_cuda_log(level, fmt, ...) \
+	rte_log(RTE_LOG_ ## level, cuda_logtype, fmt "\n", ##__VA_ARGS__)
+
+#define rte_cuda_debug(fmt, ...) \
+	rte_cuda_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
+		##__VA_ARGS__)
+
+#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H
+	#include <gdrapi.h>
+#else
+	struct gdr;
+	typedef struct gdr *gdr_t;
+	struct gdr_mh_s;
+	typedef struct gdr_mh_s gdr_mh_t;
+#endif
+
+int gdrcopy_pin(gdr_t *gdrc_h, __rte_unused gdr_mh_t *mh,
+		uint64_t d_addr, size_t size, void **h_addr);
+int gdrcopy_unpin(gdr_t gdrc_h, __rte_unused gdr_mh_t mh,
+		void *d_addr, size_t size);
+
+#endif
+
drivers/gpu/cuda/cuda.c +76 -14

144 lines not on this page · raw message

drivers/gpu/cuda/gdrcopy.c new file +148 -0

148 lines not on this page · raw message

drivers/gpu/cuda/meson.build +5 -1
index 3fe20929fa..784fa8bf0d 100644
@@ -17,5 +17,9 @@ if not cc.has_header('cudaTypedefs.h')
         subdir_done()
 endif
 
+if cc.has_header('gdrapi.h')
+        dpdk_conf.set('DRIVERS_GPU_CUDA_GDRCOPY_H', 1)
+endif
+
 deps += ['gpudev', 'pci', 'bus_pci']
-sources = files('cuda.c')
+sources = files('cuda.c', 'gdrcopy.c')
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v4 2/2] gpu/cuda: CPU map GPU memory with GDRCopy

25/02/2022 04:12, [email protected]:

Quoted 13 lines

From: Elena Agostini <[email protected]>

To enable the gpudev rte_gpu_mem_cpu_map feature to expose GPU memory to the CPU, the GPU CUDA driver library needs the GDRCopy library and driver.

If DPDK is built without GDRCopy, the GPU CUDA driver returns error if the is invoked rte_gpu_mem_cpu_map.

All the others GPU CUDA driver functionalities are not affected by the absence of GDRCopy, thus this is an optional functionality that can be enabled in the GPU CUDA driver.

Quoted 5 lines

CUDA driver documentation has been updated accordingly.

----

Should be only 3 dashes to be interpreted by git.

Quoted 4 lines

Changelog:
- Fix checkpatch and doc build issue
- Added common header to cuda.c and gdrcopy.c

Applied, thanks.

#7 lore raw

Re: [PATCH v4 1/2] doc/gpus: add cuda.ini into features

In reply to [email protected]

25/02/2022 04:12, [email protected]:

The features list were missed when introducing the driver.

Fixes 1306a73b1958 ("gpu/cuda: introduce CUDA driver")
Quoted 14 lines
---
 doc/guides/gpus/features/cuda.ini | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 doc/guides/gpus/features/cuda.ini
doc/guides/gpus/features/cuda.ini new file +2 -0
new file mode 100644
index 0000000000..eb1aff9a80
@@ -0,0 +1,12 @@
+;
+; Supported features of the 'cuda' gpu driver.

doc/guides/gpus/features/cuda.ini line 12

Quoted 10 lines
doc/guides/gpus/features/cuda.ini new file +10 -0
+;
+; Refer to default.ini for the full list of available PMD features.
+;
+[Features]
+Get device info                = Y
+Share CPU memory with device   = Y
+Allocate device memory         = Y
+Free memory                    = Y
+CPU map device memory          = Y
+CPU unmap device memory        = Y

The last 2 lines will be moved in the next patch implementing the feature.