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 59D00A04A3; Wed, 26 Jan 2022 20:38:05 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B1EF542752; Wed, 26 Jan 2022 20:38:04 +0100 (CET) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2078.outbound.protection.outlook.com [40.107.93.78]) by mails.dpdk.org (Postfix) with ESMTP id BD7354274B for ; Wed, 26 Jan 2022 20:38:03 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eL5SXyvW+7w32DumATv3hEoRuAMHkFDZCOxBC4W3oC4u1C/1u4hsGLobyUoPAuLxhLNsLU5z5d7v0cSnwwkSd8P8t1s1B/SDo5RRcPcPo4WbOmgUD6CQPhzyzgLrXw6yjILAS92GBUvbcVzkKSRnk6yx0jTDloJBqZxsJqHe+3fvixR3fZt2SnjyAyaNtjM2o0clX0dq4dM0HeRDhhgmeGbv5bQX0J5VUzsTw2NEJ6T0oa9N0DOUaiDBJpyNhvnepg1lCsKtsPfWs1nh3h+noZFkj7APobGD1d8fBgk/c30IHIn1Tp6TsWlmiUuN41PJgY4Ily4XnKWZokvnU1X8LQ== 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=jgkZjzReSpwX9Iamekp5aDjgvu256BHfz9qbcApptzo=; b=DUPe0WzwxwtBdIYx+A93wM8KHuIJtsQ2RF+nZBIRHz3QSYrbZKrHDq26sW1Bz4hnDOzFtc2KGf4jo2Wp8tiJHNQh4DtnyEco9/ydaF3UlajrSdaKK5UY2UzGZtxhFICl8uz+mEP0nlvaH94W6yWPyk86jUXS9I1lpC+yDdc7Gg0g+A0VjmsnzwnvnSfAW2Wcg+R2fBahFCju3s5ghHM7ONhH2m/6//I5U8bGmKWNLR90TjhRh9qmJqvc8aXOT0cm5g+biZF1IAM+Y0HY6eUGL9vtOeL/FEmAw86IHW11yPowbSd3+TTpkDqkpubYgm5svxldH3ngqPMlr7DxNH+Cxg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) 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=jgkZjzReSpwX9Iamekp5aDjgvu256BHfz9qbcApptzo=; b=rL6tmkgHzRAjl8KjqTw1Z5s4nVGykuk+Pkqr+9DatWWzwF2EyWd7lX9xNvm/q5kxmUxRacvfEOosXi5EOyHZan6EBp2Wk92zUbRCc59nrUEaIyONvo7LvR2z9k+tfSud9UYrr461Y0HeaK1M39i5RqIS1usw5YPatxRGg1eNztzHdGgy0qf8dcUX6dtFB+CJRnhrKL7rOoNM/bcjqb8AY12tYA8RC0RLonhwLahgBhO/xDNqUKElW5yJzL5YgB7QCM3TkfCdkBzHfVBay1Qjn25UPivn4wgrNdc8tn2mncq42LdLANhHSyeh+IDglmXtjYXYh0A2dwWiHiroh99UwQ== Received: from BN9PR03CA0957.namprd03.prod.outlook.com (2603:10b6:408:108::32) by MN2PR12MB4488.namprd12.prod.outlook.com (2603:10b6:208:24e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.15; Wed, 26 Jan 2022 19:38:02 +0000 Received: from BN8NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:408:108:cafe::ac) by BN9PR03CA0957.outlook.office365.com (2603:10b6:408:108::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.15 via Frontend Transport; Wed, 26 Jan 2022 19:38:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) 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.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.236) by BN8NAM11FT034.mail.protection.outlook.com (10.13.176.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4930.15 via Frontend Transport; Wed, 26 Jan 2022 19:38:01 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 26 Jan 2022 19:37:52 +0000 Received: from nvidia.com (10.126.230.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; Wed, 26 Jan 2022 11:37:51 -0800 From: To: CC: Elena Agostini Subject: [PATCH v4 1/2] gpudev: expose GPU memory to CPU Date: Thu, 27 Jan 2022 03:47:15 +0000 Message-ID: <20220127034716.12497-1-eagostini@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220104023408.13379-1-eagostini@nvidia.com> References: <20220104023408.13379-1-eagostini@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.126.230.35] X-ClientProxiedBy: drhqmail202.nvidia.com (10.126.190.181) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2ac5f6db-7474-48e9-9cea-08d9e1035d72 X-MS-TrafficTypeDiagnostic: MN2PR12MB4488:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CGE0PIDpoNgEYqIW50M7jKSPXe3ryPfTMKhTgAorXkMwBxqfE58Yde3vm2HPPnLMmoo5C3Mrmkmd3SPtdNCK3k3p3uIY5JSADmmOmHEkPGFAxuPpVt8/ZlTv8mY+V4SyFqx05tRhjoI5rOKKZYrFbfaDec4BqS4iEkZdIjnSJagH0u/t2ic+II0wdOPK18dncEFReeftUwAEkFoG693JGI0PzamBSu8q7qDyFAyX6dqr8pg6JQMOXmk0dYZh3ONMsB8hQ7jQOE/Hm2rZyYr+k/7naFHcYRR+CNPMVVNubCbQOlNqrwhBgpf/cmPocn7UVo0H1ajsWluiW/jNP2mAuFIJ5PSqOBrPYqWvriygPx1MpsrM38LILpmSO0Gw/CD+IW/+gRWciajaoCHEegVxDrROWPengSkZxOSGJys4Ai/uN63HchPdNnE2yYPyMqL7JuxiNaDrT1K24xL54AeW8eJustiiPOLKvz1pXb3HFaWZSuls5toEtBsFlyNXq2H1zQlRTxHdeUIySAAiGUhMfsE7cqnTZEID5Oz/4z5LoBgjYqiFfma1rQrPsZNzKbowEoMHm+6fj1+qSXN0oKCFYUoJn7XYuGnCddm5u15DXEafzDBvD/hcb0IMct0ZRO+a62mO8Ej99qJJsCUKkS08pmNk3x536+qczZom5S/Kl5UN6ClF2l1j06Zka4pPrMs/t7D+qStjZihXFEK3QACj3A== X-Forefront-Antispam-Report: CIP:12.22.5.236; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230001)(4636009)(40470700004)(36840700001)(46966006)(36860700001)(36756003)(2876002)(70586007)(70206006)(2616005)(2906002)(7696005)(508600001)(5660300002)(6666004)(356005)(81166007)(55016003)(4326008)(8676002)(26005)(426003)(82310400004)(107886003)(6286002)(336012)(316002)(40460700003)(6916009)(83380400001)(8936002)(16526019)(1076003)(47076005)(186003)(86362001)(36900700001)(20210929001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2022 19:38:01.6704 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2ac5f6db-7474-48e9-9cea-08d9e1035d72 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.236]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4488 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 Enable the possibility to expose a GPU memory area and make it accessible from the CPU. GPU memory has to be allocated via rte_gpu_mem_alloc(). This patch allows the gpudev library to map (and unmap), through the GPU driver, a chunk of GPU memory and to return a memory pointer usable by the CPU to access the GPU memory area. Signed-off-by: Elena Agostini --- doc/guides/prog_guide/gpudev.rst | 9 +++++ drivers/gpu/cuda/cuda.c | 2 ++ lib/gpudev/gpudev.c | 61 ++++++++++++++++++++++++++++++++ lib/gpudev/gpudev_driver.h | 6 ++++ lib/gpudev/rte_gpudev.h | 49 +++++++++++++++++++++++++ lib/gpudev/version.map | 2 ++ 6 files changed, 129 insertions(+) diff --git a/doc/guides/prog_guide/gpudev.rst b/doc/guides/prog_guide/gpudev.rst index ff4626812b..b774ec77f9 100644 --- a/doc/guides/prog_guide/gpudev.rst +++ b/doc/guides/prog_guide/gpudev.rst @@ -73,6 +73,15 @@ Later, it's also possible to unregister that memory with gpudev. CPU memory registered outside of the gpudev library (e.g. with GPU specific library) cannot be unregistered by the gpudev library. +CPU mapping +~~~~~~~~~~~~~~~~~~~ + +gpudev can map into the CPU address space a GPU memory address allocated with gpudev. +gpudev returns a pointer the CPU can use to access (ready or write) GPU memory. +Later, it's also possible to unmap that memory with gpudev. +GPU memory CPU mapped outside of the gpudev library (e.g. with GPU specific library) +cannot be unmapped by the gpudev library. + Memory Barrier ~~~~~~~~~~~~~~ diff --git a/drivers/gpu/cuda/cuda.c b/drivers/gpu/cuda/cuda.c index 0ece1bb612..408b659fce 100644 --- a/drivers/gpu/cuda/cuda.c +++ b/drivers/gpu/cuda/cuda.c @@ -1177,6 +1177,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.wmb = cuda_wmb; rte_gpu_complete_new(dev); diff --git a/lib/gpudev/gpudev.c b/lib/gpudev/gpudev.c index 59e2169292..ce92d63257 100644 --- a/lib/gpudev/gpudev.c +++ b/lib/gpudev/gpudev.c @@ -640,6 +640,67 @@ rte_gpu_mem_unregister(int16_t dev_id, void *ptr) return GPU_DRV_RET(dev->ops.mem_unregister(dev, ptr)); } +void * +rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr) +{ + struct rte_gpu *dev; + void *ptr_out; + int ret; + + dev = gpu_get_by_id(dev_id); + if (dev == NULL) { + GPU_LOG(ERR, "mem CPU map for invalid device ID %d", dev_id); + rte_errno = ENODEV; + return NULL; + } + + if (dev->ops.mem_cpu_map == NULL) { + GPU_LOG(ERR, "mem CPU map not supported"); + rte_errno = ENOTSUP; + return NULL; + } + + if (ptr == NULL || size == 0) /* dry-run */ + return NULL; + + ret = GPU_DRV_RET(dev->ops.mem_cpu_map(dev, size, ptr, &ptr_out)); + + switch (ret) { + case 0: + return ptr_out; + case -ENOMEM: + case -E2BIG: + rte_errno = -ret; + return NULL; + default: + rte_errno = -EPERM; + return NULL; + } +} + +int +rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr) +{ + struct rte_gpu *dev; + + dev = gpu_get_by_id(dev_id); + if (dev == NULL) { + GPU_LOG(ERR, "cpu_unmap mem for invalid device ID %d", dev_id); + rte_errno = ENODEV; + return -rte_errno; + } + + if (dev->ops.mem_cpu_unmap == NULL) { + rte_errno = ENOTSUP; + return -rte_errno; + } + + if (ptr == NULL) /* dry-run */ + return 0; + + return GPU_DRV_RET(dev->ops.mem_cpu_unmap(dev, ptr)); +} + int rte_gpu_wmb(int16_t dev_id) { diff --git a/lib/gpudev/gpudev_driver.h b/lib/gpudev/gpudev_driver.h index 0ed7478e9b..0e55b00bfe 100644 --- a/lib/gpudev/gpudev_driver.h +++ b/lib/gpudev/gpudev_driver.h @@ -31,6 +31,8 @@ typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t size, unsigned int typedef int (rte_gpu_mem_free_t)(struct rte_gpu *dev, void *ptr); typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void *ptr); typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr); +typedef int (rte_gpu_mem_cpu_map_t)(struct rte_gpu *dev, size_t size, void *ptr_in, void **ptr_out); +typedef int (rte_gpu_mem_cpu_unmap_t)(struct rte_gpu *dev, void *ptr); typedef int (rte_gpu_wmb_t)(struct rte_gpu *dev); struct rte_gpu_ops { @@ -46,6 +48,10 @@ struct rte_gpu_ops { rte_gpu_mem_register_t *mem_register; /* Unregister CPU memory from device. */ rte_gpu_mem_unregister_t *mem_unregister; + /* Map GPU memory for CPU visibility. */ + rte_gpu_mem_cpu_map_t *mem_cpu_map; + /* Unmap GPU memory for CPU visibility. */ + rte_gpu_mem_cpu_unmap_t *mem_cpu_unmap; /* Enforce GPU write memory barrier. */ rte_gpu_wmb_t *wmb; }; diff --git a/lib/gpudev/rte_gpudev.h b/lib/gpudev/rte_gpudev.h index ff3ca78c89..5cc4eb5828 100644 --- a/lib/gpudev/rte_gpudev.h +++ b/lib/gpudev/rte_gpudev.h @@ -452,6 +452,55 @@ int rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr); __rte_experimental int rte_gpu_mem_unregister(int16_t dev_id, void *ptr); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Map a chunk of GPU memory to make it accessible from the CPU + * using the memory pointer returned by the function. + * GPU memory has to be allocated via rte_gpu_mem_alloc(). + * + * @param dev_id + * Device ID requiring mapped memory. + * @param size + * Number of bytes to map. + * Requesting 0 will do nothing. + * @param ptr + * Pointer to the GPU memory area to be mapped. + * NULL is a no-op accepted value. + + * @return + * A pointer to the mapped GPU memory usable by the CPU, otherwise NULL and rte_errno is set: + * - ENODEV if invalid dev_id + * - ENOTSUP if operation not supported by the driver + * - E2BIG if size is higher than limit + * - ENOMEM if out of space + * - EPERM if driver error + */ +__rte_experimental +void *rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Unmap a chunk of GPU memory previously mapped with rte_gpu_mem_cpu_map() + * + * @param dev_id + * Reference device ID. + * @param ptr + * Pointer to the memory area to be unmapped. + * NULL is a no-op accepted value. + * + * @return + * 0 on success, -rte_errno otherwise: + * - ENODEV if invalid dev_id + * - ENOTSUP if operation not supported by the driver + * - EPERM if driver error + */ +__rte_experimental +int rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr); + /** * @warning * @b EXPERIMENTAL: this API may change without prior notice. diff --git a/lib/gpudev/version.map b/lib/gpudev/version.map index 2e414c65cc..5bc5d154cd 100644 --- a/lib/gpudev/version.map +++ b/lib/gpudev/version.map @@ -20,8 +20,10 @@ EXPERIMENTAL { rte_gpu_init; rte_gpu_is_valid; rte_gpu_mem_alloc; + rte_gpu_mem_cpu_map; rte_gpu_mem_free; rte_gpu_mem_register; + rte_gpu_mem_cpu_unmap; rte_gpu_mem_unregister; rte_gpu_wmb; }; -- 2.17.1