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 36BD1A0C53; Wed, 3 Nov 2021 12:05:03 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 990A7411CE; Wed, 3 Nov 2021 12:05:00 +0100 (CET) Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2065.outbound.protection.outlook.com [40.107.92.65]) by mails.dpdk.org (Postfix) with ESMTP id 71F8F41134 for ; Wed, 3 Nov 2021 12:04:57 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KOYAA1hjRjgHOqEhmpk8R2z+R+GpnsSp2Mb2YQCwJrJo8bA5A19qJSwwjeb+ii4avSWrh5eyEFbgANZmQoQzL8cBkj9nC2H4vCxTlpbeyODqalKZhNyQ9cnJflkw1Dnut+uofPT0hNvBmIC7zqdXd2cfXI0YRdWO+TBCIShW27h3yEpSH7dNv4darbjvEr39Hxw/p9/Xvhk81D37zFChG1XWlt34+lvBC6/WbK7Enu4oqeCp/KCVM+Qkz4cvGLSBe/dDsG2tNBh4ACAahjD27i8gwrnRBdg+q1IEZbuZZhpxrN0yborVAdzN+t0LqfqwDEQZASbmeTB7MzS1Q8jsVA== 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=2TJJfWuag8aNaZVVL61bL7JJLJN39ujkKZD8whqRu/U=; b=GreeSahpyByrRmu+Rzlr10JO5KPvM0Uh6WcNtimCOxPlFfh85TQkGiBGz+vw32QyHHs5bP46GRBICETHj06Fjxhol9Kfo+S+k3ok0S5Rf+sIHtJ/J3E9saMOOEtsxQr3LVoTGSM7hfXRV8N3xi4TJM6X70Ib5JeO3VqRjJ+5uSQTqUxgIbkL+X15jcFbqcHCjwA0wHPEqyAYTTRpg+t7rlbUMRUwrOTQyFlVu/OkBFzF6uSXRm9uCjLjlJXLNHd6PIdtbKLuz2QT9w4EmNZ51foEmo4CwjHJPdeHvu0DYALEP0zWoxxZTfoJKN9I6Qf7Mk5gssWSD4QrtOa5/JmFOw== 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=2TJJfWuag8aNaZVVL61bL7JJLJN39ujkKZD8whqRu/U=; b=CAAPvvDyUilzlcFd75wRElkhrc1gL/K3TCQuu+Lg81cIKJD/TDF+lMy9oRdzvoVgYBUmD6kZjhot19JyBbxWU5C8NF1IDI5XD5kz7b5m1l13MsUa6EcFPBkD1I81FtN4PPJrVGDCuyffJzx0xM+bOUTyyCosJEb71LSKAajZBovUnIkGsfoXLJjq4ol8mglesTvZA9l9hNtcvvy06UbEPL7Zw1icbZ40pqRWwreVcBoCXM8RkWxWfGui3uN/T4G4NHiCpSR5BZMZBjiKdEV568jx2IBlAouZw1kaCndO/IfUVfuWJLaTNUMQexO/+mMdsxP50JpJo9IM9iVoR7Dw4A== Received: from DM6PR03CA0101.namprd03.prod.outlook.com (2603:10b6:5:333::34) by BN7PR12MB2705.namprd12.prod.outlook.com (2603:10b6:408:25::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.10; Wed, 3 Nov 2021 11:04:56 +0000 Received: from DM6NAM11FT064.eop-nam11.prod.protection.outlook.com (2603:10b6:5:333:cafe::6e) by DM6PR03CA0101.outlook.office365.com (2603:10b6:5:333::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.10 via Frontend Transport; Wed, 3 Nov 2021 11:04:55 +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 DM6NAM11FT064.mail.protection.outlook.com (10.13.172.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4669.10 via Frontend Transport; Wed, 3 Nov 2021 11:04:55 +0000 Received: from nvidia.com (172.20.187.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 3 Nov 2021 11:04:54 +0000 From: To: CC: Thomas Monjalon Date: Wed, 3 Nov 2021 19:15:47 +0000 Message-ID: <20211103191554.16449-3-eagostini@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211103191554.16449-1-eagostini@nvidia.com> References: <20210602203531.2288645-1-thomas@monjalon.net> <20211103191554.16449-1-eagostini@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.20.187.5] X-ClientProxiedBy: HQMAIL111.nvidia.com (172.20.187.18) To HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7d558292-a0fd-4f93-3be5-08d99eb9c4e4 X-MS-TrafficTypeDiagnostic: BN7PR12MB2705: 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: wpzO3y5YVfXjlEk8mdqoWVXaw8rtdXcqN3rx3em9lOuF3p/pUV+FqM9tFG6a1aq0qSKWOatPUHrVFuFd4nt2QFsYtkeXNJ1SChXV2lq5MqIWADNmvZWHzMt0Wd5gcUQVz+8lAbFSyWhcuGPI6S8pG7caHryW10yYBufXGMa7DU3xq0WVFGGEXD7HTHjx+4DIaGx+t1EM8lNmNV6zmF9K+0XoIFTZ3FtkIDT7iKeUsXseBADuE00QOwqzx77TEEb87c+K6kYCUgcDD6qrfVpYfllSkeDtQrsBOzmTMUYG3AaQtB4TZ4Xv7fo2wbFzDvMXOcQ9EaT4XOU6f6/7yjiWM94NEDUkzQyMepdFDchiad00kHFdn9Q21rje/V15aBbTIKpiDlLRMO/+rx1BofUYTKFFoBAgqTYohxnFrZbrddwhTPSI3F+YavnbTejVh/jFBvZPPu9TMPmBc8v+19lCFUDImRzPu6mnZ5VWIeS691QCf4dZSOlTJ2uS4Kql7kv5Q5YGENW5foEGkSt3GINMSADmlloUQfvHrWpQG/CwAxMRatGj9UunA+UuITjm/DeVckZYit0SHAqhf2QQUIz01E/MQUWNfyORTr/2MhqJ4/HvhAvGD5ArgPRE9yrZ8gX6HSRX6mSzKxEa1tRb3RhJj4HsG4YLl4xz3dLdayNo4PY3mvgvihbdldz/eaDJw4qh/GO2/7hdoI823kbhX+evQw== 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)(46966006)(36840700001)(356005)(8676002)(55016002)(70206006)(7636003)(6666004)(6916009)(1076003)(82310400003)(4326008)(7696005)(86362001)(70586007)(508600001)(2876002)(2906002)(83380400001)(2616005)(336012)(26005)(36860700001)(5660300002)(316002)(36906005)(16526019)(426003)(8936002)(6286002)(47076005)(36756003)(15650500001)(186003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2021 11:04:55.7886 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7d558292-a0fd-4f93-3be5-08d99eb9c4e4 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: DM6NAM11FT064.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR12MB2705 Subject: [dpdk-dev] [PATCH v4 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 c839c530c8..d57e23df7c 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