From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012005.outbound.protection.outlook.com [40.107.209.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54EC332B102 for ; Wed, 2 Sep 2026 15:48:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.5 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788364135; cv=fail; b=II1PC098skIc+7Y+nTp2RJAwitTI9XzxxzRma/tlw7jKvgbQkQiBIc17198aHsb2D6JL2YvWUPPOGPYyxfTF0QdlO2c1z9bBbdTxTkiuC3Y6GhRslmqm655+kRe9bjaxGDmzRhJ+dt+iwOgK62npswb3WjNJI3XNWV3DS0kN840= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788364135; c=relaxed/simple; bh=8lPE3x346Ix1oyY29PwH2+JaINySDXHmaaWpIrFDcrI=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ZSY9vNKkA2xWDzYm/M8jVQS88kOTdTOOQVgqkBXz+wWx1ldlQK+iIRyDcGqiyoXoALTLBnr4/h25oRGsUMJJSsFoa/rwzb+bQMhrSbGuMJ2Lf3YE6xIc+dZWcEv5mjYwOU3r6YiVdDAv0trZ/pyRkfRH7M6TXLdzwrhnKsoH+aE= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=iY4LeH8Y; arc=fail smtp.client-ip=40.107.209.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="iY4LeH8Y" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vk1cAQlUupdTC8eAFmghWwx6RjthzKHQ4d5HB6ZiXiwHXGA6hpEPZmRd4NoJZIXHDLnXsNm0e/PgojWStOXNzc/mATgAPjlLzSUVtrIViU9xqRRzQi8ALgi2tfAmA4y65jwI1G4LZv0kbEPWWMs2wafPsRxFsSJl7bYXQW4XXuUQQH1AL8wCU3vHFEjcN9lqdfkFKibBVE8edm5u9IuJC+Qz6qfj52r9TVNk+MFWeLiwsZtT06fqF0Wbn9xikL74AwhgmLmbyiXrKlcVzEN76+FHATcyXboVyOddihZJkfIM7JbedoVxBVJc+SQTqRVOg28cbHj/D3ZwFgmad6LkmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=TQ8xdHqLAmHz0xeMMBVvJRLwrpBXTcQCfgKdJYi/7pc=; b=YJ2nIJB+epUpeXhOvpTHZYOwelq56mwR3V/7MEFwlHVDJS12QuOj3l5qpZD0EdYmXrgwJv0GwNq/Sc3pfMIRF51JTN7f/xiO8PrB6KsGzsPHK20biRQO6BZP4izhC5o5ggAFag7L4ganr0riFi1r76ifv001KyqTEe5837p1k5uDQZgBX6dbIxBvhWi88s8juCHV+m22tQ04N168nGZUYbUOs9D8bui8brcciWf+0gXdoorherd15sjPI8CFxfSQqBC45IDroJoeNb3ZvK15thj8hoFrhqzXM56k1mmQ/60fuKE9cTdCMSzHBi0MlWScrEJN8K3l2eSvhwmJGPCejA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TQ8xdHqLAmHz0xeMMBVvJRLwrpBXTcQCfgKdJYi/7pc=; b=iY4LeH8Y6G7vHhSTda/edQVyKpBzScfHIcp69oGy5AKJMBrJ82LkJdP7ymZCftKByaBKhPG4HSJWwbW2imZGQBY3as+zKzMAsMpPTKKAw7UiG5c+rCv0f+g/KjQd4R8iqb35phwnvRpUrrW1mS0rCSocom7aCMPAvlkHfdepPY8= Received: from CH0P223CA0030.NAMP223.PROD.OUTLOOK.COM (2603:10b6:610:116::6) by SJ0PR12MB6925.namprd12.prod.outlook.com (2603:10b6:a03:483::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.360.13; Wed, 2 Sep 2026 15:48:33 +0000 Received: from DS2PEPF000061C6.namprd02.prod.outlook.com (2603:10b6:610:116:cafe::9d) by CH0P223CA0030.outlook.office365.com (2603:10b6:610:116::6) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.382.11 via Frontend Transport; Wed, 2 Sep 2026 15:48:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by DS2PEPF000061C6.mail.protection.outlook.com (10.167.23.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.382.8 via Frontend Transport; Wed, 2 Sep 2026 15:48:32 +0000 Received: from srishanm-Cloudripper.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.46; Wed, 2 Sep 2026 10:48:30 -0500 From: Srinivasan Shanmugam To: Subject: [PATCH 1/4] drm: Add drm_work_fence helper Date: Wed, 2 Sep 2026 21:18:14 +0530 Message-ID: <20260902154817.185499-1-srinivasan.shanmugam@amd.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF000061C6:EE_|SJ0PR12MB6925:EE_ X-MS-Office365-Filtering-Correlation-Id: b3e1f945-0eca-4348-27b3-08df0909a405 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|82310400026|23010399003|36860700016|1800799024|10067099003|18002099003|56012099006|11063799006; X-Microsoft-Antispam-Message-Info: 4Bs9ulUT7M8k2o12INoTHCD0Zak8mAa0cBzudPfF9FKNj5/kbmdpMg+drp1zADCvoD+N8CkAHvLfHNAsNxYx/B/8lfjqWfW68ijQfrxYNg7oegjhHsF8eDzahdiwQ2jUJhd9o34/UgyL/kAhyVad/PKCh+AQBny0f2JNP+Hla8ztJD16QlCWo0+jqtmv5WaYLdazQrbAJ2Hp999Z80NYrhlDTWSnFRitP2ProOaOsBwzn/aIgTJcwfdy38i8Qcgu+nPtEzHMGRtYKoR8fh3iTmgDGzX/xgGR+fwqRyqFcamXLAoTsZU+8KeSX3Y1Bmooqe2k/wfYhZ3IPmgPZvbBUwM1bYCO73VkcdSw8Q3h6++OyZj+pkOJwiCy5KnSGM+iI1HodzKPo3ZWR4tcBhrTN73YTJbCoffY5ou+Z3XQR+9rSCXoBmoL7iBPndCSNUQkg0CmWkBb6WGy0/6ilAuZHrZsJffd/VCOv7cv3onuScoFhwq3YxWyK8XiSZeNYwgGEmhhUSxpnnGunXQcvO0GkHlUji3959NZdTaoegvGRHCypcu9toN4m2x9BLnk31GWtOzbRrBQ1t0iWrVgxQs0/+lW0Av0jve473RODRrJP0kDroCt5TnIgNWaRf7fkuiRdx0Jdg/eVBkQdhqoWM2qhW8pE95qPaGx4qPYd7eAXq1odqHa9ycXFhJ2J7JuduaEBxW4Ziw3ylfY7BPAUFDRpw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(82310400026)(23010399003)(36860700016)(1800799024)(10067099003)(18002099003)(56012099006)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: P1rWIGiH4qWdTVc8n0aKv7X0htkgmKzcDUYcrNEa/ZIdSLBTyA1Y9q5KYVpY+A5mpLEJSgOU8wodArh8E3kzTMU+gpSpVPt7rKN9ByjHcUj+Fc7Vbfj+5mLsLllC+wWydW7wHKk2QhAH1VjSelIEWK4NtrQOTC47jOdFFm63axfo59u/R4b4STSbB4C3wrlAaZ14ysvRVgwgQKLHlu14ZDs0jPIrlWzlOAC2QAgLTm8AmV/j2qH8NRQiAMtgOuM6MkoOIVhtHEXSpPNjf6VAJjE0yu+0xhLk0DCb9U2Z2o6ZrNwe0b5ERVX6vw6DpesaiX05illDq39jPzdZtipFxmlUC/q2kkrKj86F9bkiKhKV8vWV/iMgaPTeXMDSnyNzoGzketT2YXcggNI3ZuF/VLhPxoxsYp2EKPh+cqgeB5AWDtxKaDag981Alc2Qn22r X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Sep 2026 15:48:32.0221 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b3e1f945-0eca-4348-27b3-08df0909a405 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF000061C6.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6925 GPU drivers often need to queue work when a dma-fence signals because certain operations (copy_to_user, eventfd_signal, memory allocation) cannot run in IRQ context. This pattern is currently open-coded in multiple drivers. Introduce drm_work_fence — an embeddable base structure that handles the dma-fence-callback-to-workqueue pattern in one place. Drivers embed this in their own structure and implement ops->writeback() for the deferred work and ops->destroy() for cleanup. The helper manages: - kref lifetime - dma-fence callback registration - workqueue dispatch on fence signal - safe cancellation before driver teardown For work that additionally requires borrowing the process MM via kthread_use_mm(), see drm_user_fence which builds on top of this. Suggested-by: Matthew Brost Cc: Maarten Lankhorst Cc: Christian König Cc: dri-devel@lists.freedesktop.org Cc: intel-xe@lists.freedesktop.org Cc: amd-gfx@lists.freedesktop.org Signed-off-by: Srinivasan Shanmugam --- drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_work_fence.c | 184 +++++++++++++++++++++++++++++++ include/drm/drm_work_fence.h | 69 ++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 drivers/gpu/drm/drm_work_fence.c create mode 100644 include/drm/drm_work_fence.h diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index e97faabcd783..c5be8e80d0c8 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -72,6 +72,7 @@ drm-y := \ drm_vblank.o \ drm_vblank_work.o \ drm_vma_manager.o \ + drm_work_fence.o \ drm_writeback.o drm-$(CONFIG_DRM_CLIENT) += \ drm_client.o \ diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c new file mode 100644 index 000000000000..482eb3d4475f --- /dev/null +++ b/drivers/gpu/drm/drm_work_fence.c @@ -0,0 +1,184 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright © 2024 The Linux Foundation + * + * Common DRM work fence helper. + * + * When a GPU dma-fence signals, drivers often need to perform work that + * cannot run in IRQ context (e.g., memory allocation, copy_to_user, + * eventfd_signal). This helper queues a work item when a dma-fence + * signals, allowing that work to run safely in a workqueue context. + * + * NOTE: This helper consumes dma_fences but CANNOT implement + * dma_fence_ops. Work items queued here may sleep; dma_fence_ops + * callbacks are called under the fence spinlock and must not sleep. + * + * For work that additionally requires accessing userspace memory via + * kthread_use_mm(), see drm_user_fence which builds on top of this. + */ + +#include + +#include + +static void drm_work_fence_destroy(struct kref *kref) +{ + struct drm_work_fence *wfence = + container_of(kref, struct drm_work_fence, refcount); + struct dma_fence *fence = wfence->fence; + + wfence->ops->destroy(wfence); + dma_fence_put(fence); /* NULL-safe */ +} + +/** + * drm_work_fence_get - Acquire a reference to a work fence + * @wfence: work fence + */ +void drm_work_fence_get(struct drm_work_fence *wfence) +{ + kref_get(&wfence->refcount); +} +EXPORT_SYMBOL_GPL(drm_work_fence_get); + +/** + * drm_work_fence_put - Release a reference to a work fence + * @wfence: work fence + */ +void drm_work_fence_put(struct drm_work_fence *wfence) +{ + kref_put(&wfence->refcount, drm_work_fence_destroy); +} +EXPORT_SYMBOL_GPL(drm_work_fence_put); + +static void drm_work_fence_work(struct work_struct *w) +{ + struct drm_work_fence *wfence = + container_of(w, struct drm_work_fence, work); + + wfence->ops->writeback(wfence); + drm_work_fence_put(wfence); +} + +static void drm_work_fence_queue(struct drm_work_fence *wfence) +{ + queue_work(wfence->wq, &wfence->work); +} + +static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb) +{ + struct drm_work_fence *wfence = + container_of(cb, struct drm_work_fence, cb); + + drm_work_fence_queue(wfence); + /* Single ref: wfence->fence released in drm_work_fence_destroy(). */ +} + +/** + * drm_work_fence_init - Initialize a work fence + * @wfence: work fence to initialize + * @wq: workqueue to run the worker on (must be ordered if sequencing matters) + * @ops: driver operations + */ +void drm_work_fence_init(struct drm_work_fence *wfence, + struct workqueue_struct *wq, + const struct drm_work_fence_ops *ops) +{ + kref_init(&wfence->refcount); + wfence->wq = wq; + wfence->ops = ops; + wfence->fence = NULL; + INIT_WORK(&wfence->work, drm_work_fence_work); +} +EXPORT_SYMBOL_GPL(drm_work_fence_init); + +/** + * drm_work_fence_add_callback - Attach a work fence to a dma-fence + * @wfence: work fence; caller retains their reference and must release + * it via drm_work_fence_put() when no longer needed + * @fence: dma-fence to watch; one reference is consumed on any return value + * + * When @fence signals, a work item is queued that calls ops->writeback(). + * If @fence has already signaled, the work item is queued immediately. + * + * Return: 0 on success, negative errno on error. + */ +int drm_work_fence_add_callback(struct drm_work_fence *wfence, + struct dma_fence *fence) +{ + int err; + + drm_work_fence_get(wfence); + wfence->fence = fence; /* transfer caller's ref — single ref, no get */ + + err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb); + if (err == -ENOENT) { + drm_work_fence_queue(wfence); + err = 0; + } else if (err) { + wfence->fence = NULL; + dma_fence_put(fence); + drm_work_fence_put(wfence); + } + + return err; +} +EXPORT_SYMBOL_GPL(drm_work_fence_add_callback); + +/** + * drm_work_fence_cancel - Cancel a pending work fence callback + * @wfence: work fence + * + * Attempts to remove the pending callback before driver context teardown. + * The caller must hold a reference to @wfence across this call. + * + * If the callback has already fired this returns false and all cleanup + * has been handled internally. + * + * If removal succeeds the callback reference is released internally. + * The caller must still release its own reference via drm_work_fence_put(). + * + * This function is safe to call from atomic context as it only acquires + * the dma-fence spinlock internally. If the caller also needs to wait + * for the worker to finish, use drm_work_fence_cancel_sync() instead, + * which may sleep. + * + * Return: true if callback was removed, false if it had already fired. + */ +bool drm_work_fence_cancel(struct drm_work_fence *wfence) +{ + struct dma_fence *fence = wfence->fence; + + if (!fence) + return false; + + if (dma_fence_remove_callback(fence, &wfence->cb)) { + drm_work_fence_put(wfence); /* drop ref from add_callback */ + return true; + } + + return false; +} +EXPORT_SYMBOL_GPL(drm_work_fence_cancel); + +/** + * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish + * @wfence: work fence + * + * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee + * the worker has fully completed before returning. + * + * This function may sleep. Must not be called from atomic or interrupt + * context. Use drm_work_fence_cancel() instead when sleeping is not allowed. + * + * Drivers must call this during teardown before freeing any resources + * accessed by ops->writeback(). + */ +void drm_work_fence_cancel_sync(struct drm_work_fence *wfence) +{ + if (drm_work_fence_cancel(wfence)) + return; + if (cancel_work_sync(&wfence->work)) + drm_work_fence_put(wfence); +} +EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync); diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h new file mode 100644 index 000000000000..c8e3c5b9f0c4 --- /dev/null +++ b/include/drm/drm_work_fence.h @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright © 2024 The Linux Foundation + */ + +#ifndef __DRM_WORK_FENCE_H__ +#define __DRM_WORK_FENCE_H__ + +#include +#include +#include + +struct drm_work_fence; + +/** + * struct drm_work_fence_ops - driver callbacks for a DRM work fence + */ +struct drm_work_fence_ops { + /** + * @writeback: Called from workqueue context when the dma-fence signals. + * + * Perform the deferred work here (copy_to_user, eventfd_signal, etc.). + * May sleep. Must not requeue the fence. + */ + void (*writeback)(struct drm_work_fence *wfence); + + /** + * @destroy: Called when the last reference is dropped. + * Free the containing structure here. + */ + void (*destroy)(struct drm_work_fence *wfence); +}; + +/** + * struct drm_work_fence - DRM dma-fence-to-workqueue helper + * + * Embeddable base structure that queues a work item when a dma-fence signals. + * Drivers embed this in their own structure and implement ops->writeback() + * for the deferred work and ops->destroy() for cleanup. + * + * Call drm_work_fence_init() at creation and drm_work_fence_add_callback() + * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown. + */ +struct drm_work_fence { + /** @refcount: Reference count. */ + struct kref refcount; + /** @wq: Workqueue on which to run the worker. */ + struct workqueue_struct *wq; + /** @ops: Driver operations. */ + const struct drm_work_fence_ops *ops; + /** @fence: The watched dma-fence; holds a single reference. */ + struct dma_fence *fence; + /** @work: Work item queued when the fence signals. */ + struct work_struct work; + /** @cb: Callback registered on the dma-fence. */ + struct dma_fence_cb cb; +}; + +void drm_work_fence_init(struct drm_work_fence *wfence, + struct workqueue_struct *wq, + const struct drm_work_fence_ops *ops); +void drm_work_fence_get(struct drm_work_fence *wfence); +void drm_work_fence_put(struct drm_work_fence *wfence); +int drm_work_fence_add_callback(struct drm_work_fence *wfence, + struct dma_fence *fence); +bool drm_work_fence_cancel(struct drm_work_fence *wfence); +void drm_work_fence_cancel_sync(struct drm_work_fence *wfence); + +#endif /* __DRM_WORK_FENCE_H__ */ -- 2.34.1