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 6D540A0C4D; Mon, 8 Nov 2021 11:47:21 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9035841122; Mon, 8 Nov 2021 11:47:08 +0100 (CET) Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam08on2083.outbound.protection.outlook.com [40.107.102.83]) by mails.dpdk.org (Postfix) with ESMTP id C4066410E4 for ; Mon, 8 Nov 2021 11:47:05 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JWf7ktbb6+PMl07uiAIEPGDSX0ZfH2P5vepLxNOSBEdwn8zMre6u+i3hDMDeFCJp+kqMqzbHWi4yF6vqZ1bnVgPIsgDpDir9LtxIKonff/LNQz/mEg9T/HTel7NT1Ak6xuDgynFvwAWBERWaGszrBphc4H/3M7X9eqxOH9GWuWlDFUOre+/BuQ/reNjVQvRsFYLCPKdaqUmnUUvClbsEL4EYnGCxevN/lcpPGuYZBNuEFEdXFCxB0kElGc0e/Kdk3lAHzObHqKM6KXqezXOBc/X57GchgS9hyVpwjagNmmo6o7uQaTHKx9lWWWQLNJ61ulwownyQTxv6RK6Kl2N+fg== 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=Y6WzzOVaJDEQN6r0+nyZE+iVv62w3kb2RicpotIK7Z0=; b=f5Xds7SyVrODL1c1Qq5Avtx/vAc86Q1qPAXiHNImdjsKLIJnSyPjMxTlKJtOKl/FbWr7agEHuOCiyS9XrzvIiBjD+DPrHddFr3RzRtNao2/ATVaPQfYsRMwsgKG90zc9FsoJ6amC7twtiCsx97/tP1mRNzboMTbewHkMNv/Oft42cJrEq68pkPTWCXR2vqCR+VPiJp5G+7HEb5QWYBe79wotWXZrjVVPFtumarfveuniq3ux+WCGhqm5tDpEUag+4DQooNq30ouxIvZAXjQZ7e0NRC98mlcK436vjx03dr8gWtF5SWzE5l7GyazoWOnYESUtq9uoCJBt3QPPrBQcrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.34) smtp.rcpttodomain=monjalon.net smtp.mailfrom=nvidia.com; dmarc=pass (p=quarantine sp=quarantine 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=Y6WzzOVaJDEQN6r0+nyZE+iVv62w3kb2RicpotIK7Z0=; b=jN95WoUZibywmZga73cFbPyKSZZMUvA5PlOKxlqIkLvdeLllHvAsBeaUgD/F5dp4lg3bOvkcpnBcoNAi28zYBJiM9Kh5YjTTMk7n0CufM2Kz7vCdjnCQ83OIZAzEUowChfAxBP40wqmw/FI3OcdFz5pnSE5jX2uOax4b6XGiHtoGF5huXsXgWzjBP01M9yQI9o8779Ww3qDrBxHpGouwSrlVtsf+bLE6h+LZHDXukorIawS0PWPda59JKIEl7Gxd2LQtGVlKfgIKuMGx2o7IGHpIlRT15SIAe+udgfGsEYG4rh6TxyMDLcKvnJ9lLfwWet6o8pgKL0z6aUTvBS00aA== Received: from MW2PR16CA0061.namprd16.prod.outlook.com (2603:10b6:907:1::38) by BY5PR12MB4289.namprd12.prod.outlook.com (2603:10b6:a03:204::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.10; Mon, 8 Nov 2021 10:47:04 +0000 Received: from CO1NAM11FT064.eop-nam11.prod.protection.outlook.com (2603:10b6:907:1:cafe::11) by MW2PR16CA0061.outlook.office365.com (2603:10b6:907:1::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.10 via Frontend Transport; Mon, 8 Nov 2021 10:47:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34) smtp.mailfrom=nvidia.com; monjalon.net; dkim=none (message not signed) header.d=none;monjalon.net; dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.112.34 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.112.34; helo=mail.nvidia.com; Received: from mail.nvidia.com (216.228.112.34) by CO1NAM11FT064.mail.protection.outlook.com (10.13.175.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4669.10 via Frontend Transport; Mon, 8 Nov 2021 10:47:03 +0000 Received: from nvidia.com (172.20.187.6) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 8 Nov 2021 10:47:01 +0000 From: To: CC: Thomas Monjalon Date: Mon, 8 Nov 2021 18:57:58 +0000 Message-ID: <20211108185805.3887-3-eagostini@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211108185805.3887-1-eagostini@nvidia.com> References: <20210602203531.2288645-1-thomas@monjalon.net> <20211108185805.3887-1-eagostini@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.20.187.6] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 574e3bec-5bcf-46ed-4168-08d9a2a519e1 X-MS-TrafficTypeDiagnostic: BY5PR12MB4289: X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: L8k3PP2XUvxvFKiW5YMOFh1znCxnvlO9gtvdKV0fJ+UWCSP/B6VgO/jjwvuJsQBHZneIOBsyIm2IvOXm2Abl3rjU4ZDnRSCm43LU6FSiTlF/bOuxVXMPLdFZSTXtfwEC93p3tdAGsemInG7solYVnMBivpDA+irtt86l1ZFKzWT0d5nzLpc+tCl2mhVcisimgZKGe/ToTPgAXGdC5A+6imAs/FZRJre2Cv1BcYwhXaMJoWHN3UC7DW+myS8l/uxEXHdouDyIO3Dkma452cj0dUrI2LBQpSWGp9X8WMdE6kmg6Db4QDVk+BL1Nd37x7qk0kiQ6ThR0VMfCnujSvD7aI77P1OA6YsqHFarU7HFpixJ/gBhAXmrXBJywpq0Uw1iYp6MVrmiCgZE8mc8lCzvlbooWiMVv5C6QH6yD94WwZK8KExrj1Lv7Z5cZRUiEw3nx4F8d0s9L3B+QLT3fOI7uczilWUYq4irk2SrJb1CEzY+zRlI41SBPpsR7WoSm2SI9UM4TGfDUFy1xupGpm9XcLT1E+XUWeGG6otta1BlqpsMjiVBGdJnNJND2GXvSEy3VG9Rz/VbJyHggVmyIGyGPzpwMSD/WlGCi/IGGOjz4kPDDIxHLIjard2UpQecaprRrDecrzvoj2co4rk1ZiolTefK8ebLVAoUxDDetUk4AEpUzs0P16qJoF9M2YhnMYZlY5IqD+FN6RVVZy1tOIYfig== X-Forefront-Antispam-Report: CIP:216.228.112.34; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid03.nvidia.com; CAT:NONE; SFS:(4636009)(36840700001)(46966006)(8936002)(55016002)(186003)(36756003)(83380400001)(26005)(508600001)(2616005)(2876002)(86362001)(426003)(6286002)(6916009)(6666004)(16526019)(70586007)(336012)(5660300002)(4326008)(82310400003)(7696005)(356005)(8676002)(7636003)(70206006)(36860700001)(15650500001)(316002)(2906002)(47076005)(1076003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2021 10:47:03.6092 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 574e3bec-5bcf-46ed-4168-08d9a2a519e1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.112.34]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT064.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4289 Subject: [dpdk-dev] [PATCH v5 2/9] gpudev: add event notification 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 Sender: "dev" From: Thomas Monjalon Callback functions may be registered for a device event. Callback management is per-process and not thread-safe. The events RTE_GPU_EVENT_NEW and RTE_GPU_EVENT_DEL are notified respectively after creation and before removal of a device, as part of the library functions. Some future events may be emitted from drivers. Signed-off-by: Thomas Monjalon --- lib/gpudev/gpudev.c | 148 +++++++++++++++++++++++++++++++++++++ lib/gpudev/gpudev_driver.h | 7 ++ lib/gpudev/rte_gpudev.h | 70 ++++++++++++++++++ lib/gpudev/version.map | 3 + 4 files changed, 228 insertions(+) diff --git a/lib/gpudev/gpudev.c b/lib/gpudev/gpudev.c index aeb021f2cc..07572ae040 100644 --- a/lib/gpudev/gpudev.c +++ b/lib/gpudev/gpudev.c @@ -3,6 +3,7 @@ */ #include +#include #include #include #include @@ -27,6 +28,16 @@ static int16_t gpu_max; /* Number of currently valid devices */ static int16_t gpu_count; +/* Event callback object */ +struct rte_gpu_callback { + TAILQ_ENTRY(rte_gpu_callback) next; + rte_gpu_callback_t *function; + void *user_data; + enum rte_gpu_event event; +}; +static rte_rwlock_t gpu_callback_lock = RTE_RWLOCK_INITIALIZER; +static void gpu_free_callbacks(struct rte_gpu *dev); + int rte_gpu_init(size_t dev_max) { @@ -166,6 +177,7 @@ rte_gpu_allocate(const char *name) dev->info.name = dev->name; dev->info.dev_id = dev_id; dev->info.numa_node = -1; + TAILQ_INIT(&dev->callbacks); gpu_count++; GPU_LOG(DEBUG, "new device %s (id %d) of total %d", @@ -180,6 +192,8 @@ rte_gpu_complete_new(struct rte_gpu *dev) return; dev->state = RTE_GPU_STATE_INITIALIZED; + dev->state = RTE_GPU_STATE_INITIALIZED; + rte_gpu_notify(dev, RTE_GPU_EVENT_NEW); } int @@ -192,6 +206,9 @@ rte_gpu_release(struct rte_gpu *dev) GPU_LOG(DEBUG, "free device %s (id %d)", dev->info.name, dev->info.dev_id); + rte_gpu_notify(dev, RTE_GPU_EVENT_DEL); + + gpu_free_callbacks(dev); dev->state = RTE_GPU_STATE_UNUSED; gpu_count--; @@ -224,6 +241,137 @@ rte_gpu_close(int16_t dev_id) return firsterr; } +int +rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event, + rte_gpu_callback_t *function, void *user_data) +{ + int16_t next_dev, last_dev; + struct rte_gpu_callback_list *callbacks; + struct rte_gpu_callback *callback; + + if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) { + GPU_LOG(ERR, "register callback of invalid ID %d", dev_id); + rte_errno = ENODEV; + return -rte_errno; + } + if (function == NULL) { + GPU_LOG(ERR, "cannot register callback without function"); + rte_errno = EINVAL; + return -rte_errno; + } + + if (dev_id == RTE_GPU_ID_ANY) { + next_dev = 0; + last_dev = gpu_max - 1; + } else { + next_dev = last_dev = dev_id; + } + + rte_rwlock_write_lock(&gpu_callback_lock); + do { + callbacks = &gpus[next_dev].callbacks; + + /* check if not already registered */ + TAILQ_FOREACH(callback, callbacks, next) { + if (callback->event == event && + callback->function == function && + callback->user_data == user_data) { + GPU_LOG(INFO, "callback already registered"); + return 0; + } + } + + callback = malloc(sizeof(*callback)); + if (callback == NULL) { + GPU_LOG(ERR, "cannot allocate callback"); + return -ENOMEM; + } + callback->function = function; + callback->user_data = user_data; + callback->event = event; + TAILQ_INSERT_TAIL(callbacks, callback, next); + + } while (++next_dev <= last_dev); + rte_rwlock_write_unlock(&gpu_callback_lock); + + return 0; +} + +int +rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event, + rte_gpu_callback_t *function, void *user_data) +{ + int16_t next_dev, last_dev; + struct rte_gpu_callback_list *callbacks; + struct rte_gpu_callback *callback, *nextcb; + + if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) { + GPU_LOG(ERR, "unregister callback of invalid ID %d", dev_id); + rte_errno = ENODEV; + return -rte_errno; + } + if (function == NULL) { + GPU_LOG(ERR, "cannot unregister callback without function"); + rte_errno = EINVAL; + return -rte_errno; + } + + if (dev_id == RTE_GPU_ID_ANY) { + next_dev = 0; + last_dev = gpu_max - 1; + } else { + next_dev = last_dev = dev_id; + } + + rte_rwlock_write_lock(&gpu_callback_lock); + do { + callbacks = &gpus[next_dev].callbacks; + RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) { + if (callback->event != event || + callback->function != function || + (callback->user_data != user_data && + user_data != (void *)-1)) + continue; + TAILQ_REMOVE(callbacks, callback, next); + free(callback); + } + } while (++next_dev <= last_dev); + rte_rwlock_write_unlock(&gpu_callback_lock); + + return 0; +} + +static void +gpu_free_callbacks(struct rte_gpu *dev) +{ + struct rte_gpu_callback_list *callbacks; + struct rte_gpu_callback *callback, *nextcb; + + callbacks = &dev->callbacks; + rte_rwlock_write_lock(&gpu_callback_lock); + RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) { + TAILQ_REMOVE(callbacks, callback, next); + free(callback); + } + rte_rwlock_write_unlock(&gpu_callback_lock); +} + +void +rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event) +{ + int16_t dev_id; + struct rte_gpu_callback *callback; + + dev_id = dev->info.dev_id; + rte_rwlock_read_lock(&gpu_callback_lock); + TAILQ_FOREACH(callback, &dev->callbacks, next) { + if (callback->event != event || callback->function == NULL) + continue; + callback->function(dev_id, event, callback->user_data); + } + rte_rwlock_read_unlock(&gpu_callback_lock); +} + int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info) { diff --git a/lib/gpudev/gpudev_driver.h b/lib/gpudev/gpudev_driver.h index 9e096e3b64..2a7089aa52 100644 --- a/lib/gpudev/gpudev_driver.h +++ b/lib/gpudev/gpudev_driver.h @@ -12,6 +12,7 @@ #define RTE_GPUDEV_DRIVER_H #include +#include #include @@ -43,6 +44,8 @@ struct rte_gpu { struct rte_gpu_info info; /* Driver functions. */ struct rte_gpu_ops ops; + /* Event callback list. */ + TAILQ_HEAD(rte_gpu_callback_list, rte_gpu_callback) callbacks; /* Current state (used or not) in the running process. */ enum rte_gpu_state state; /* Updated by this library. */ /* Driver-specific private data for the running process. */ @@ -64,4 +67,8 @@ void rte_gpu_complete_new(struct rte_gpu *dev); __rte_internal int rte_gpu_release(struct rte_gpu *dev); +/* Call registered callbacks. No multi-process event. */ +__rte_internal +void rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event); + #endif /* RTE_GPUDEV_DRIVER_H */ diff --git a/lib/gpudev/rte_gpudev.h b/lib/gpudev/rte_gpudev.h index eb7cfa8c59..e1702fbfe4 100644 --- a/lib/gpudev/rte_gpudev.h +++ b/lib/gpudev/rte_gpudev.h @@ -31,6 +31,11 @@ extern "C" { /** Empty device ID. */ #define RTE_GPU_ID_NONE -1 +/** Catch-all device ID. */ +#define RTE_GPU_ID_ANY INT16_MIN + +/** Catch-all callback data. */ +#define RTE_GPU_CALLBACK_ANY_DATA ((void *)-1) /** Store device info. */ struct rte_gpu_info { @@ -46,6 +51,18 @@ struct rte_gpu_info { int16_t numa_node; }; +/** Flags passed in notification callback. */ +enum rte_gpu_event { + /** Device is just initialized. */ + RTE_GPU_EVENT_NEW, + /** Device is going to be released. */ + RTE_GPU_EVENT_DEL, +}; + +/** Prototype of event callback function. */ +typedef void (rte_gpu_callback_t)(int16_t dev_id, + enum rte_gpu_event event, void *user_data); + /** * @warning * @b EXPERIMENTAL: this API may change without prior notice. @@ -141,6 +158,59 @@ int16_t rte_gpu_find_next(int16_t dev_id); __rte_experimental int rte_gpu_close(int16_t dev_id); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Register a function as event callback. + * A function may be registered multiple times for different events. + * + * @param dev_id + * Device ID to get notified about. + * RTE_GPU_ID_ANY means all devices. + * @param event + * Device event to be registered for. + * @param function + * Callback function to be called on event. + * @param user_data + * Optional parameter passed in the callback. + * + * @return + * 0 on success, -rte_errno otherwise: + * - ENODEV if invalid dev_id + * - EINVAL if NULL function + * - ENOMEM if out of memory + */ +__rte_experimental +int rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event, + rte_gpu_callback_t *function, void *user_data); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Unregister for an event. + * + * @param dev_id + * Device ID to be silenced. + * RTE_GPU_ID_ANY means all devices. + * @param event + * Registered event. + * @param function + * Registered function. + * @param user_data + * Optional parameter as registered. + * RTE_GPU_CALLBACK_ANY_DATA is a catch-all. + * + * @return + * 0 on success, -rte_errno otherwise: + * - ENODEV if invalid dev_id + * - EINVAL if NULL function + */ +__rte_experimental +int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event, + rte_gpu_callback_t *function, void *user_data); + /** * @warning * @b EXPERIMENTAL: this API may change without prior notice. diff --git a/lib/gpudev/version.map b/lib/gpudev/version.map index 6ac6b327e2..b3b6b76c1c 100644 --- a/lib/gpudev/version.map +++ b/lib/gpudev/version.map @@ -2,6 +2,8 @@ EXPERIMENTAL { global: # added in 21.11 + rte_gpu_callback_register; + rte_gpu_callback_unregister; rte_gpu_close; rte_gpu_count_avail; rte_gpu_find_next; @@ -16,5 +18,6 @@ INTERNAL { rte_gpu_allocate; rte_gpu_complete_new; rte_gpu_get_by_name; + rte_gpu_notify; rte_gpu_release; }; -- 2.17.1