From patchwork Thu Jan 27 03:50:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elena Agostini X-Patchwork-Id: 106591 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 39D11A04A3; Wed, 26 Jan 2022 20:40:49 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CC44842751; Wed, 26 Jan 2022 20:40:48 +0100 (CET) Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2064.outbound.protection.outlook.com [40.107.220.64]) by mails.dpdk.org (Postfix) with ESMTP id 3A0D24274B for ; Wed, 26 Jan 2022 20:40:47 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EbAvtWQmSxoJ0D3EKA11RnxmFdC6vPdhTUhiMEQfw5OhA8gAEueV/FzRQuI0siZlEpdQGmYYrpwWpZ3mPtA8cGuKetmTGHPNBH2G+XceotdNZUz6tBUCxWM1rxiIfNAJN0o1Ao8nAUoc7WDSX92fYbYFRW7OX0iXabxUG2ahIASV2C/ugUtFrFh3c6IGa7+ttt8giabHCB/jlp4ZJw5Qs9m4XwuEkbvDyK3AG9sJNkRZ7aLlX020dUBvJExQEVbOLgYMgdpRjIOkpPox7aY7WyQ2lWiHo4P0AprzevM3JSofOG+/zgB+fC7P8evh0R0a7+wrPyamJpEWNcIfKnfJyA== 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=iC2dVmfuTlHuLN9KOAuZlf6v6O6Or2KR4JFN39GOqp4egUQeMeSX/OoNISgSAvm+RuzTdM0LdYDgUJ+AP5LP20ITp77wFfQSwLdQNOIB2ZFARbnSGWB+5T/Jcs25juUinMcWfi9kb0Vn0Dm2j/UH3unlWmX/fTa2xpVwrklLAlxuTcylrguBKLRUYwPE5nRWcodzcpdK5YHzraqZwWQu+AtoO1gjjdncbVFAjdFjXi4hBojVlox5fk0fOUwONICYU5Qek4o9YVCKE9k4UX0iYl3g5t6F4gwZYqv+N+/ZxTdAeCYh++UiKynswRIq88jTUYfUNfWQBN4j0C8sqFJfzw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) 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=It5zaOImHQhJdIyEDQ7krG1kqqcX6jyK3gqjAdHO2P5ShVUaFkduAwe8TkFz1lVyTK3MEx/zAgHSiXB7aOjfKhkF1TaV7E01P/HYi0CzBh3E3i6hZEyZ4cbOm9+NFutoHvXXCiGG0XlWj61q0bb4nW2mzEZJdSUVBr6SxzZVixoPhTqi/Ouls0QYFZeSoL1ParhEzFcOFpCcaradxiYB0TFa59N1h5jwr+4PvJNHsNi3YOwuwGEert1ZTYdO49CMf6rNqDI53fVDmc8vOb1SNk5Ts0oltnH2pCyLCN7ti3mQ8d4hbJHoMUfDDmk9bbarUx0U4L+NVAg595m+pBmuOg== Received: from DS7PR03CA0108.namprd03.prod.outlook.com (2603:10b6:5:3b7::23) by SJ0PR12MB5609.namprd12.prod.outlook.com (2603:10b6:a03:42c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.13; Wed, 26 Jan 2022 19:40:45 +0000 Received: from DM6NAM11FT017.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b7:cafe::6f) by DS7PR03CA0108.outlook.office365.com (2603:10b6:5:3b7::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.17 via Frontend Transport; Wed, 26 Jan 2022 19:40:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) 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.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.238) by DM6NAM11FT017.mail.protection.outlook.com (10.13.172.145) 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:40:45 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 26 Jan 2022 19:40:44 +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:40:43 -0800 From: To: CC: Elena Agostini Subject: [PATCH v5 1/2] gpudev: expose GPU memory to CPU Date: Thu, 27 Jan 2022 03:50:28 +0000 Message-ID: <20220127035029.12920-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 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: 0ec8a66f-1537-4684-556c-08d9e103bf22 X-MS-TrafficTypeDiagnostic: SJ0PR12MB5609: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: /STD93JfqioFYDqkRWt7EMfvpPszgAHr+oldjOEL8q7aCoEhQId1fsbJddra6LZuXPFGkfQg7wSILuKobrTDV9td5NKoSju0EHq8mTUL7IDtZ+UhKJQ4QanqxC68w+fLKqOExURsCWSE9r0xbakP7v5jyfOh3qQFCwuoejVTwjCn1epRMJLzLKEFKPfwX65uzn0w2O3VDWKCYx1BE+qgwOBBnnfNBF1z2/x/uyylroZVeevQTepVskWgkadiB7waKz69bp5T6gL0fnHNqg3ppe/ikL20cWymJYbdcopwN1IEjgImM9UsVOOA6MFfqdjxksNTFmTzQBta+8yI4Qj+8PqIqrpC/2u/lTRDUZ6gQA3b5scpxOqup9eq21ogC0ITgQPUf3tOvoHI/IL1a9UDxyY8HOz3j+UQD6dkWM+7YXX2nFjTWZOd2zrNhvZCA9pWyBtvUSCyXghbWgAi0ivqAztcv76b0sNzB6nGy5F4WbvQRUQTLOlzIGSTlygmKtkm0NuUlR+gaQzGKc81rWMPN2+5hLsLtJxWH4Z3UZ7LzNnlQ5wwb2Xb30Zye3SZukPlssDhhJDk10mwZxvgMkqWUt9H/rU+PY34p+MftzMPK7UArAMHsj7fDPSJSAus1+89hRR/K7EvkMIUYxZ0liMWQw/tV2XrCr9KFwc1i0JYFF2DimOybc+eiSbWVLeC4VAwOaRat3CwJtd2/0esSZey/WeNg9XJWc09EmbHPlI/CYqNJrGESJ77mcwBkqHfla/TyngFxWl477vDbrx9sG2TaAR6wekD9vADLth5LkdxzFE= X-Forefront-Antispam-Report: CIP:12.22.5.238; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(4636009)(40470700004)(36840700001)(46966006)(40460700003)(70206006)(316002)(47076005)(82310400004)(8936002)(5660300002)(81166007)(86362001)(26005)(83380400001)(508600001)(336012)(16526019)(70586007)(6666004)(4326008)(107886003)(1076003)(36756003)(6916009)(186003)(356005)(36860700001)(55016003)(8676002)(2876002)(7696005)(2906002)(2616005)(6286002)(426003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2022 19:40:45.6079 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0ec8a66f-1537-4684-556c-08d9e103bf22 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.238]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT017.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5609 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; };