From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 24695A034C; Thu, 24 Feb 2022 20:02:53 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id ADEDA4114F; Thu, 24 Feb 2022 20:02:45 +0100 (CET) Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2045.outbound.protection.outlook.com [40.107.220.45]) by mails.dpdk.org (Postfix) with ESMTP id 351F841145 for ; Thu, 24 Feb 2022 20:02:43 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cr8d69NxeHSbnaRRU7PZwRlSwcXzqQ56akp9aAwG6c8FyPUNtLa3YjG9pWUHJf37nUN8ClSMrzWjAC+rxTo0WZPBArTTSXepepqxqlX6OQl19eu0o3EeipPwnH0TqRgoy8pKaNSAfalmRwyyXw6UQSi59GsZiS0jMY0lY582jpvG1dPeMw6+cFOqa20qfTUx/DN/CUHlyCs0/4noZ6wmNyBKIS1MYTWt+idIzDuIRY6y7b52PaaFL9zMXt0r+EYQjlkr8yF6K6xWaM4WTjlT/5xYBgXmfulijhQPwh8SUFx0bdeX6dNmIBDAEwkIAycXZ1WDIO6N6OCdvuju3wR0dA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wq9pI87FCc0vMfJrCL6Fa7aSQKeibiLD37U+SETK1d4=; b=cf58nweLcF9qQ1ZNdnJk0XUlBgy2sApuzitbU8ZJHbAUNVlYfgyCmUSdtf/pygpoHJGhQGuuLG7Hqz4ZY26DSEfYbrMaYeDIGrazwzumhz+pLmhvoEYCI/kJH/ssOHbb/hY9S61ULAo639iumh0pgMw2TNYLB4oixgwcDqRE3ErPpur8hUVLTHjZJ4R2wrXIj2vk4e7q4rXDGXDWDbZ90dARxxscnU236L4vuCLSs+S8DDdthf3cFn+qEu5a9CgveSpY/wqsfGKuzwljdPU8aSLPVnp2W8DdmX0s4iJrKPjcaivH0JLjvCCvofNzgMr8KI9y4HTwPM3IR8B/1AeyKA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=dpdk.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wq9pI87FCc0vMfJrCL6Fa7aSQKeibiLD37U+SETK1d4=; b=WASNd7AjygjdjpntykdhOxaYvQvn3Jz7Ccn0o3Hmqdx2/pqYcfjU9FJYStlG4ANq8F2kc89LeZS1ggLhvtFhGy4gTs6o+Eoj55kvPjvxHmfndGL/e6EWPw/Pm4g+yTed2MEiiGjMYpre25ErN92Vyj2rG4mgOid6Ra7Ac9xfoZHJhRJw2Gqc4Scj0r5HqhYYmOIOpSjEQoGztFlW4NHD0VEE5vGiy49E5KX6bNPpc/fBy1MVFU4SFGa0OvibGGKyoeKFJ33igDcdiXEHsb2DyoNuHyaz0MeWSU8wBcuMSP/fXHAidkIXN37ei7OSA1G6BDW37D0U0DGPoN43IIVSwA== Received: from DM6PR11CA0017.namprd11.prod.outlook.com (2603:10b6:5:190::30) by SA0PR12MB4447.namprd12.prod.outlook.com (2603:10b6:806:9b::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.22; Thu, 24 Feb 2022 19:02:41 +0000 Received: from DM6NAM11FT056.eop-nam11.prod.protection.outlook.com (2603:10b6:5:190:cafe::9f) by DM6PR11CA0017.outlook.office365.com (2603:10b6:5:190::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.24 via Frontend Transport; Thu, 24 Feb 2022 19:02:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.235) by DM6NAM11FT056.mail.protection.outlook.com (10.13.173.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5017.22 via Frontend Transport; Thu, 24 Feb 2022 19:02:41 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 24 Feb 2022 19:02:40 +0000 Received: from nvidia.com (10.126.231.35) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.9; Thu, 24 Feb 2022 11:02:39 -0800 From: To: CC: Elena Agostini Subject: [PATCH v4 2/2] gpu/cuda: CPU map GPU memory with GDRCopy Date: Fri, 25 Feb 2022 03:12:26 +0000 Message-ID: <20220225031226.8348-2-eagostini@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220225031226.8348-1-eagostini@nvidia.com> References: <20220221224433.26096-1-eagostini@nvidia.com> <20220225031226.8348-1-eagostini@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.126.231.35] X-ClientProxiedBy: rnnvmail201.nvidia.com (10.129.68.8) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5259a3b8-1939-4bba-e919-08d9f7c83b6b X-MS-TrafficTypeDiagnostic: SA0PR12MB4447:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9f74SHEeVV6oy/Jgo31iNKR2Zw9DXRXwZEv3chTnrgT2aCmqqlQccsPQeQexlNNEO2uVls50gxLkBdVPPKireICzBJrfR4DqiI+Rbet3hreALB3XhF4+q+1/Os6RNHIZqAAuvikCEHb5N+/hP4VSw0+d7/K1D8zK19wQq/WgO8W0ZmoTHLqbXICTNl0K+bZPq2CqNIumiSyNnmOYmT6TtIxoAtxZf0vdqjBcgKvNU6E+OoeYbxz5m7ZD4F4fDsFPCi8VNoSg+Q+Cz0aGHPyAARP4P0lxbhyWvHxeqLHMuW9ZkP7Mogf289vdp6D0Be0Eo44Cy+fifLGrOSsuDGR+RGtBN8jW7UAxbwwrf6ucDAEpNJBKnUnt9JxaJyRkMh6bpYKCF1P9BCcnb+GXuyLpIXZ+HCPqV7RAi981a6w8fXDZTUU88g2nNWyyRsHQiraOcN9KvXU0nQQOaJZs6AMhBa+t4PwGV6IbML6T+ftD2IRixGQtBSCe80Gi8MbB5dvYCXADxrXWuE97zFy96QLsrs4thnnAuPT4nGuQAfqtc6cUkKc7IzdYtejcOJNLfRAJSK53FHiGok+5x11Vsah2ICfyFfa2MCFOyT1gjKLuy2bdpM3Vxfrq6jadBxXG7GfnA5EyyHcn7UT171GQ8J4wRVddYFaV/E+Zzq/4GHxezxNvWAHLI/qcyt9isPLuE4PTMBGGTapwo/4bI3o0dINDEiyF1iSneM+4DGOB0mLdYJcdPQneuxNJleapS5DNngnmUi+eYcbx9dTkJNDeFHZuiahvccaOxKTetmTR34RzTrGBfoa2MFJOpyYoEjZJTvBZdxLyK/3RH1jx0vNfjSWYAQ== X-Forefront-Antispam-Report: CIP:12.22.5.235; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(316002)(6916009)(83380400001)(966005)(8676002)(70586007)(4326008)(47076005)(70206006)(55016003)(2876002)(2906002)(81166007)(186003)(26005)(5660300002)(82310400004)(30864003)(6286002)(2616005)(1076003)(426003)(8936002)(40460700003)(356005)(336012)(508600001)(86362001)(36756003)(36860700001)(16526019)(6666004)(7696005)(107886003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Feb 2022 19:02:41.0208 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5259a3b8-1939-4bba-e919-08d9f7c83b6b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.235]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT056.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4447 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Elena Agostini 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. Signed-off-by: Elena Agostini ---- 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 diff --git a/doc/guides/gpus/cuda.rst b/doc/guides/gpus/cuda.rst index 38e22dc2c0..5d4fe02d77 100644 --- a/doc/guides/gpus/cuda.rst +++ b/doc/guides/gpus/cuda.rst @@ -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 `_ 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 ------ diff --git a/doc/guides/gpus/features/default.ini b/doc/guides/gpus/features/default.ini index 87e9966424..817113f2c2 100644 --- a/doc/guides/gpus/features/default.ini +++ b/doc/guides/gpus/features/default.ini @@ -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 = diff --git a/drivers/gpu/cuda/common.h b/drivers/gpu/cuda/common.h new file mode 100644 index 0000000000..323bbacd6e --- /dev/null +++ b/drivers/gpu/cuda/common.h @@ -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 + +#include +#include +#include + +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 +#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 + diff --git a/drivers/gpu/cuda/cuda.c b/drivers/gpu/cuda/cuda.c index b43d5a32b7..c247ce0179 100644 --- a/drivers/gpu/cuda/cuda.c +++ b/drivers/gpu/cuda/cuda.c @@ -4,8 +4,6 @@ #include -#include -#include #include #include #include @@ -16,6 +14,7 @@ #include #include #include +#include "common.h" #define CUDA_DRIVER_MIN_VERSION 11040 #define CUDA_API_MIN_VERSION 3020 @@ -51,6 +50,7 @@ static PFN_cuFlushGPUDirectRDMAWrites pfn_cuFlushGPUDirectRDMAWrites; 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) @@ -74,16 +74,6 @@ static int cuda_driver_version; #define GPU_PAGE_SHIFT 16 #define GPU_PAGE_SIZE (1UL << GPU_PAGE_SHIFT) -static 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__) - /* NVIDIA GPU address map */ static const struct rte_pci_id pci_id_cuda_map[] = { { @@ -157,6 +147,7 @@ struct mem_entry { CUcontext ctx; cuda_ptr_key pkey; enum mem_type mtype; + gdr_mh_t mh; struct mem_entry *prev; struct mem_entry *next; }; @@ -797,6 +788,47 @@ cuda_mem_register(struct rte_gpu *dev, size_t size, void *ptr) return 0; } +static int +cuda_mem_cpu_map(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; + + 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_free(struct rte_gpu *dev, void *ptr) { @@ -874,6 +906,34 @@ cuda_mem_unregister(struct rte_gpu *dev, void *ptr) return -rte_errno; } +static int +cuda_mem_cpu_unmap(struct rte_gpu *dev, void *ptr_in) +{ + struct mem_entry *mem_item; + cuda_ptr_key hk; + + if (dev == NULL) + return -ENODEV; + + 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_dev_close(struct rte_gpu *dev) { @@ -1040,6 +1100,8 @@ cuda_gpu_probe(__rte_unused struct rte_pci_driver *pci_drv, struct rte_pci_devic rte_errno = ENOTSUP; return -rte_errno; } + + gdrc_h = NULL; } /* Fill HW specific part of device structure */ @@ -1182,8 +1244,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_cpu_map = NULL; - dev->ops.mem_cpu_unmap = NULL; + dev->ops.mem_cpu_map = cuda_mem_cpu_map; + dev->ops.mem_cpu_unmap = cuda_mem_cpu_unmap; dev->ops.wmb = cuda_wmb; rte_gpu_complete_new(dev); diff --git a/drivers/gpu/cuda/gdrcopy.c b/drivers/gpu/cuda/gdrcopy.c new file mode 100644 index 0000000000..8e6178c09d --- /dev/null +++ b/drivers/gpu/cuda/gdrcopy.c @@ -0,0 +1,148 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2022 NVIDIA Corporation & Affiliates + */ + +#include "common.h" + +static void *gdrclib; +static gdr_t (*sym_gdr_open)(void); +static int (*sym_gdr_close)(gdr_t g); +static int (*sym_gdr_pin_buffer)(gdr_t g, unsigned long addr, size_t size, + uint64_t p2p_token, uint32_t va_space, gdr_mh_t *handle); +static int (*sym_gdr_unpin_buffer)(gdr_t g, gdr_mh_t handle); +static int (*sym_gdr_map)(gdr_t g, gdr_mh_t handle, void **va, size_t size); +static int (*sym_gdr_unmap)(gdr_t g, gdr_mh_t handle, void *va, size_t size); + +static int +gdrcopy_loader(void) +{ + char gdrcopy_path[1024]; + + if (getenv("GDRCOPY_PATH_L") == NULL) + snprintf(gdrcopy_path, 1024, "%s", "libgdrapi.so"); + else + snprintf(gdrcopy_path, 1024, "%s/%s", getenv("GDRCOPY_PATH_L"), "libgdrapi.so"); + + gdrclib = dlopen(gdrcopy_path, RTLD_LAZY); + if (gdrclib == NULL) { + rte_cuda_log(ERR, "Failed to find GDRCopy library %s (GDRCOPY_PATH_L=%s)\n", + gdrcopy_path, getenv("GDRCOPY_PATH_L")); + return -1; + } + + sym_gdr_open = dlsym(gdrclib, "gdr_open"); + if (sym_gdr_open == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + sym_gdr_close = dlsym(gdrclib, "gdr_close"); + if (sym_gdr_close == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + sym_gdr_pin_buffer = dlsym(gdrclib, "gdr_pin_buffer"); + if (sym_gdr_pin_buffer == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + sym_gdr_unpin_buffer = dlsym(gdrclib, "gdr_unpin_buffer"); + if (sym_gdr_unpin_buffer == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + sym_gdr_map = dlsym(gdrclib, "gdr_map"); + if (sym_gdr_map == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + sym_gdr_unmap = dlsym(gdrclib, "gdr_unmap"); + if (sym_gdr_unmap == NULL) { + rte_cuda_log(ERR, "Failed to load GDRCopy symbols\n"); + return -1; + } + + return 0; +} + +static int +gdrcopy_open(gdr_t *g) +{ + gdr_t g_; + + g_ = sym_gdr_open(); + if (!g_) + return -1; + *g = g_; + + return 0; +} + +static int +gdrcopy_close(gdr_t *g) +{ + sym_gdr_close(*g); + return 0; +} + +int +gdrcopy_pin(gdr_t *gdrc_h, __rte_unused gdr_mh_t *mh, uint64_t d_addr, size_t size, void **h_addr) +{ +#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H + if (*gdrc_h == NULL) { + if (gdrcopy_loader()) + return -ENOTSUP; + + if (gdrcopy_open(gdrc_h)) { + rte_cuda_log(ERR, + "GDRCopy gdrdrv kernel module not found. Can't CPU map GPU memory."); + return -EPERM; + } + } + + /* Pin the device buffer */ + if (sym_gdr_pin_buffer(*gdrc_h, d_addr, size, 0, 0, mh) != 0) { + rte_cuda_log(ERR, "GDRCopy pin buffer error."); + return -1; + } + + /* Map the buffer to user space */ + if (sym_gdr_map(*gdrc_h, *mh, h_addr, size) != 0) { + rte_cuda_log(ERR, "GDRCopy map buffer error."); + sym_gdr_unpin_buffer(*gdrc_h, *mh); + return -1; + } + + return 0; +#else + rte_cuda_log(ERR, + "GDRCopy headers not provided at DPDK building time. Can't CPU map GPU memory."); + return -ENOTSUP; +#endif +} + +int +gdrcopy_unpin(gdr_t gdrc_h, __rte_unused gdr_mh_t mh, void *d_addr, size_t size) +{ + if (gdrc_h == NULL) + return -EINVAL; + +#ifdef DRIVERS_GPU_CUDA_GDRCOPY_H + /* Unmap the buffer from user space */ + if (sym_gdr_unmap(gdrc_h, mh, d_addr, size) != 0) { + rte_cuda_log(ERR, "GDRCopy unmap buffer error."); + return -1; + } + /* Unpin the device buffer */ + if (sym_gdr_unpin_buffer(gdrc_h, mh) != 0) { + rte_cuda_log(ERR, "GDRCopy unpin buffer error."); + return -1; + } +#endif + + return 0; +} diff --git a/drivers/gpu/cuda/meson.build b/drivers/gpu/cuda/meson.build index 3fe20929fa..784fa8bf0d 100644 --- a/drivers/gpu/cuda/meson.build +++ b/drivers/gpu/cuda/meson.build @@ -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') -- 2.17.1