From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013014.outbound.protection.outlook.com [40.93.196.14]) (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 713D83769F0 for ; Wed, 2 Sep 2026 14:57:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.14 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788361055; cv=fail; b=qrEzoaqmgm9L/jC5/fxryTH9CdRuzUh60vy99HwpRlboZgvB2geyggKHaz+FbT23CrqpXr+8ACrGig5bMawtQrT4C9KfcsMKYFogATvJSAfxaHN2hPDXyefMphdSAKQGDQVcpV+naaVZqP4ONeywA5D8E14D9kWHEYSsyxatW3g= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788361055; c=relaxed/simple; bh=J7n62gm6Fb0GVgFV+G0WRE+dbNJMGwkimvrs7X7Aymo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=m0NFR2NDknPpkTrq7ovWxHS7k0LDqZvU7AJ3QjdtwpW6XWaHnhJ6w9fFlABgF3ZKuoKtohXTLi8oRBsxw0qwCqMehPD36JTZNPNoCB/seyJcQGSI2RBvZqZVwK4Ft+flFh5QrXsqsq9fP7cuU1hd3Il8iYFBYNZtnGSGyilCz5I= 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=t7gBu9pm; arc=fail smtp.client-ip=40.93.196.14 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="t7gBu9pm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hAd92FLdUqPgr+IV32LJsJhbAn/ee6tcBZVRl8E189zQs6o6rmmjKu1eJIupmPrg2aX0Q9DCOOZuKTFrGjnVm5VGNhPlt1AfiQ/ZmPSkNXAzXk14czyIPr91tEQ/FLtXC3OlMQEpqNieVFxBOc/SERv+nRUY5IEowBJp8LmiqXTIXiVq2zek6wd5n8xKSh5exBkyaatFXnn4vOS91LMcrHB1gbwBIOuexTuYjNMKxQPx/W+fmf+Uch+JLgtLw7tW14Gd1BqqV0cVlVzViOjm+/6KUmgtsSU+9ec2soakY1k+r6murxwKcbEQDnrELGaDYtss90QMBSKmqLJpsxiQaQ== 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=9rfsSZsgbkKzIyr0WfckW95CuDUdTvgL/CZrJkk73J4=; b=QjmRC+CqncTmVjOc2w5YESO+MWPh1V2J0rq+k/EWbj1GqsDTnLbbK2ONWavloeaWbc7jrJOoxcPtqTydx7kPy5bOgHmfuX+O7V6CU8mvbRMajZuReKwJ5j8KO3PNl7whY4N31ZYObdLWpZcurHi6brPAdo+iRm6N5iAr3c8uebboNvO6IO9LdydCH6L5Pvx6waq35smIMf4Jv0iG/BXLsn72TILeK7o/cJ0/mlnGBxjiOfDmPcF4ilgnSEvRojXtgWPfWfagdwD3oJSjqBEuq4qp7Vr/NoxA2fND5hAoezZ7HRXFrPfVWjGJdX03xyhFffYgNs4iLU3Dk28S9E3MMw== 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=9rfsSZsgbkKzIyr0WfckW95CuDUdTvgL/CZrJkk73J4=; b=t7gBu9pmYb4+zmt+gMUeX3BStjEwJgD/Nfx5U1h5mXKJGIXYwNmLNailSa0wDYHK5tmJW4jh1IdNFH71MRVMHawJK2N4DY08+HNJj9i+BpGx8LFv6sHcS15P7vmj8oNPgjcLsIs5IPHCfiZBfV4lJD4tx8duoiKL084KYb+LxvA= Received: from SJ0PR13CA0158.namprd13.prod.outlook.com (2603:10b6:a03:2c7::13) by DM4PR12MB6613.namprd12.prod.outlook.com (2603:10b6:8:b8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.360.10; Wed, 2 Sep 2026 14:57:14 +0000 Received: from SJ1PEPF0000231A.namprd03.prod.outlook.com (2603:10b6:a03:2c7:cafe::66) by SJ0PR13CA0158.outlook.office365.com (2603:10b6:a03:2c7::13) 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 14:57:14 +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 SJ1PEPF0000231A.mail.protection.outlook.com (10.167.242.231) 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 14:57:14 +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 09:57:12 -0500 From: Srinivasan Shanmugam To: Subject: [PATCH 2/4] drm: Add drm_user_fence helper Date: Wed, 2 Sep 2026 20:26:56 +0530 Message-ID: <20260902145658.183267-2-srinivasan.shanmugam@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260902145658.183267-1-srinivasan.shanmugam@amd.com> References: <20260902145658.183267-1-srinivasan.shanmugam@amd.com> 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: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF0000231A:EE_|DM4PR12MB6613:EE_ X-MS-Office365-Filtering-Correlation-Id: 1e71c83d-a97d-47ae-a8b2-08df0902797e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|1800799024|376014|82310400026|36860700016|56012099006|22082099003|18002099003|11063799006|10067099003; X-Microsoft-Antispam-Message-Info: BJ2xvfgSbGavNWuO16MFRCLqytftpo0rGU3sEfVuwOXDLte350YYbfYn+tICaqGTFUk5fzujJsj0rKjyOtf1+UX5hB3fzAXTxp3IKsdbM7v1/AbpzgaNdvvDROekX68H6g91TxDzDRwQfqOupw6+a/128ymq9mkGTze2BYTA+BjD9ZyvrGELs648L+8wFlronG+r/+2hH/nq4Vxxa/a7WJTWkUTOaPK3EyIBr7DkdFnUHvx4AsqOHRS+oFZPMRwdjfF25SCnhKo0ZJV7TohcDIr2rBotgozsCa5PC+T4V7Pz0enC0c+IbhYy83z14CLhDGN42YV9hmzhy5PYOuc2BPFpq9GjxMq2mwnyHMZxK4PzDaseNYj/FQ1S7KU/runyJR/FGBQ5PtaCF99lUwT2Suedklf/PQCn+s+ttQvBl6UFrBH8zUvtWoVSWlULzOMpu2UA+wd3IyLgDtz8SsWAGv/qu7/srdspSRtbqUEa/Y3+PaOh+g2T4NUotq00EumMeap7uyRwzd0S7M0EM1rdyhxVwIKFL1Bo5DE1iA+im+9tlKTZj7+sAWNLEasZcGKlCGOj5KDx/7JdQwchIHYm6Qzq9l0br3ffZSH7PtDy5X9hOnP5Wu7jUinB6Llnx1YUzIkcxaswp30wd5lxs4606G3qMCdNqjgRcQ/mafrdoqL/K2LYdOjMHWHi+0f56d/BLNmlK2NaT9v9bYM7r9hFsQ== 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)(23010399003)(1800799024)(376014)(82310400026)(36860700016)(56012099006)(22082099003)(18002099003)(11063799006)(10067099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k+XtEFe9qJx56uscX+ZfhmAuQJL9pnPa457AjgqNZ6VsQ42N5iRS59iiV00a/vEr9xbs2g0AN+A4uiVR/RzxTam36hbIs4jfroiG55MEKjQY5PgzY5g/CMJqiPWCGCsLAEJhlUGQYU2vPG/laAwFePhpV5j9yIjpKFl33XPNUKrfIMF9UTlwvuYgjojWlicwyvho2qIPIfjwzUajWiJbEUda2eCCFCxDn0ugsiER2bn0eJUYsRk4JqUrSyEkLPTKxByCsxL2mV/GbJtEM25CbkBTOLVzYwHyK+y/F4+/ARJUzAF4eLTZ47SJVL76mmzsKA7x8ngZ+wuuLdfgHH0AnFzdb6nbf7ccU5STfpth+mdZWUFtQDfr6BY475FFxbuTAW38HnrPozMszDY659Xy50RonRMUeRU7q2IZJBi2y/kg0pAlOM/5S2LdhWy8Q3P5 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Sep 2026 14:57:14.1560 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1e71c83d-a97d-47ae-a8b2-08df0902797e 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: SJ1PEPF0000231A.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6613 Introduce a common DRM user fence helper providing the kref-managed, MM-borrowing dma-fence-callback-to-workqueue pattern used by drivers that must access userspace memory from a kthread context when a GPU fence signals. XE uses this pattern (xe_sync.c) to write a fence completion value to a userspace VA. AMDGPU will use the same pattern to signal a per-queue eventfd from a user-queue EOP fence callback. The helper provides: - struct drm_user_fence: embeddable base structure - struct drm_user_fence_ops: worker/destroy callbacks - drm_user_fence_init(): initialize and grab the process MM - drm_user_fence_get/put(): reference counting - drm_user_fence_add_callback(): attach to a dma-fence The worker callback receives a bool indicating whether the process MM was successfully obtained, allowing drivers to handle the unavailable-MM case (log, skip the userspace write, etc.) without duplicating the mmget/kthread_use_mm/mmput boilerplate. Suggested-by: Christian König Cc: Matthew Brost Cc: Maarten Lankhorst 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_user_fence.c | 70 +++++++++++++++++++ include/drm/drm_user_fence.h | 116 +++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 drivers/gpu/drm/drm_user_fence.c create mode 100644 include/drm/drm_user_fence.h diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index c5be8e80d0c8..ddb770738992 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -69,6 +69,7 @@ drm-y := \ drm_syncobj.o \ drm_sysfs.o \ drm_trace_points.o \ + drm_user_fence.o \ drm_vblank.o \ drm_vblank_work.o \ drm_vma_manager.o \ diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c new file mode 100644 index 000000000000..0f229b7210a9 --- /dev/null +++ b/drivers/gpu/drm/drm_user_fence.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright © 2024 The Linux Foundation + * + * DRM user fence — extends drm_work_fence with kthread_use_mm() support. + * + * Use this when a GPU fence signals and work needs to access userspace + * memory (copy_to_user, fault-able operations) from a kthread context. + * For work that does not require userspace memory access, use + * drm_work_fence directly. + */ + +#include +#include + +#include + +static void drm_user_fence_do_destroy(struct drm_work_fence *wfence) +{ + struct drm_user_fence *ufence = + container_of(wfence, struct drm_user_fence, base); + struct mm_struct *mm = ufence->mm; + + ufence->ops->destroy(ufence); + mmdrop(mm); +} + +static void drm_user_fence_do_work(struct drm_work_fence *wfence) +{ + struct drm_user_fence *ufence = + container_of(wfence, struct drm_user_fence, base); + struct mm_struct *mm = NULL; + + if (mmget_not_zero(ufence->mm)) { + mm = ufence->mm; + kthread_use_mm(mm); + } + + ufence->ops->worker(ufence, !!mm); + + if (mm) { + kthread_unuse_mm(mm); + mmput_async(mm); + } +} + +static const struct drm_work_fence_ops drm_user_fence_wfence_ops = { + .writeback = drm_user_fence_do_work, + .destroy = drm_user_fence_do_destroy, +}; + +/** + * drm_user_fence_init - Initialize a user fence + * @ufence: user fence to initialize + * @wq: workqueue on which to run the worker + * @ops: driver operations + * + * Must be called from process context with a valid current->mm. + * Grabs a reference to current->mm via mmgrab(). + */ +void drm_user_fence_init(struct drm_user_fence *ufence, + struct workqueue_struct *wq, + const struct drm_user_fence_ops *ops) +{ + drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops); + ufence->mm = current->mm; + mmgrab(ufence->mm); + ufence->ops = ops; +} +EXPORT_SYMBOL_GPL(drm_user_fence_init); diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h new file mode 100644 index 000000000000..36c61aa7d102 --- /dev/null +++ b/include/drm/drm_user_fence.h @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright © 2024 The Linux Foundation + */ + +#ifndef __DRM_USER_FENCE_H__ +#define __DRM_USER_FENCE_H__ + +#include + +#include + +struct drm_user_fence; + +/** + * struct drm_user_fence_ops - driver callbacks for a DRM user fence + */ +struct drm_user_fence_ops { + /** + * @worker: Called from workqueue context with the process MM active. + * + * If @mm_ok is true, kthread_use_mm() is active and userspace memory + * may be accessed safely. If @mm_ok is false, the process MM was + * already gone; skip the userspace write. + * + * wake_up() or other post-signal housekeeping should also happen here. + */ + void (*worker)(struct drm_user_fence *ufence, bool mm_ok); + + /** + * @destroy: Called when the last reference is dropped. + * Free the containing structure here. + */ + void (*destroy)(struct drm_user_fence *ufence); +}; + +/** + * struct drm_user_fence - DRM user fence with MM borrowing + * + * Extends drm_work_fence with kthread_use_mm() support for drivers + * that need to access userspace memory when a GPU fence signals. + * + * Call drm_user_fence_init() at creation and drm_user_fence_add_callback() + * to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown. + */ +struct drm_user_fence { + /** @base: Base work fence. Must be first. */ + struct drm_work_fence base; + /** @mm: Process MM grabbed at init time. */ + struct mm_struct *mm; + /** @ops: Driver operations. */ + const struct drm_user_fence_ops *ops; +}; + +void drm_user_fence_init(struct drm_user_fence *ufence, + struct workqueue_struct *wq, + const struct drm_user_fence_ops *ops); + +/** + * drm_user_fence_get - Acquire a reference to a user fence + * @ufence: user fence + */ +static inline void drm_user_fence_get(struct drm_user_fence *ufence) +{ + drm_work_fence_get(&ufence->base); +} + +/** + * drm_user_fence_put - Release a reference to a user fence + * @ufence: user fence + */ +static inline void drm_user_fence_put(struct drm_user_fence *ufence) +{ + drm_work_fence_put(&ufence->base); +} + +/** + * drm_user_fence_add_callback - Attach a user fence to a dma-fence + * @ufence: user fence; caller retains their reference and must release + * it via drm_user_fence_put() when no longer needed + * @fence: dma-fence to watch; one reference is consumed on any return value + * + * When @fence signals, ops->worker() is called from workqueue context. + * If @fence has already signaled, the worker is queued immediately. + * + * Return: 0 on success, negative errno on error. + */ +static inline int drm_user_fence_add_callback(struct drm_user_fence *ufence, + struct dma_fence *fence) +{ + return drm_work_fence_add_callback(&ufence->base, fence); +} + +/** + * drm_user_fence_cancel - Cancel a pending user fence callback + * @ufence: user fence + * + * Return: true if callback was removed, false if it had already fired. + */ +static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence) +{ + return drm_work_fence_cancel(&ufence->base); +} + +/** + * drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish + * @ufence: user fence + * + * Must be called during teardown before freeing resources. May sleep. + */ +static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence) +{ + drm_work_fence_cancel_sync(&ufence->base); +} + +#endif /* __DRM_USER_FENCE_H__ */ -- 2.34.1