From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BN8PR05CU002.outbound.protection.outlook.com (mail-eastus2azon11011022.outbound.protection.outlook.com [52.101.57.22]) (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 5B5981EE7B7 for ; Thu, 3 Sep 2026 05:31:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.57.22 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788413516; cv=fail; b=odf1luYS0Fw+zfSQs4+eeVkUn3Ftp/9bTCvDYcwqtieoPWOfixxxMf+PbIV38zwtvBlavrf4vV4O3GC+S9IDfhAVTKOvx5wUcTTcTb8VlDpCk6wg3xGD5VaseOF2ntm45JpZ2+Mr4iXVntpVxyqLs8EkQp+68VWSr+zfKfygUPo= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788413516; c=relaxed/simple; bh=F5ksDY6ZeP4ghUFZ3fZ3r+VhD3/IyRQWIkOX6sMRfFo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YdKfy/8KppXmakzmVk9ftBwHz8g64Kab4tb4DklB4leesdi6hO5JqxEOefam/C1T9caDBbih+Olzx+YYmtY7w9VSgC2+l9PnrPVHWKyfvnFMnRL1Fv2Pg+lSWyqtB+fwjupOy7yIKHtDt+AX1161XiO32/+WCTO2nnyUxOQ4lbI= 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=3pFee3KZ; arc=fail smtp.client-ip=52.101.57.22 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="3pFee3KZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OPy7KBXejKtUn9Bdk6BPgcBFgQZubMUm4U/muibSULh3umWl2i4Gm40pvf4AZ296UPwQx/X2cVveHkfO05P2hRdTO0o1fJNEK6u30swZC6hz2DfTcMqhVs2cDOA4n4dhubZlVNPgOLqqyvKSDhNbzrltN55prJSoq9Jsji+EKabCKWtmalO8aBkUH9VjBNnS+8JCi3I0s5vt0qm5CdtNI140Vm4CpOZajXEtWxaWW72PdIbLrR0ZhMCLQiXvBL1HimkRjdBH1fyst12w9MKt6uaN3K7O4QhtQwYWYgTvuGxuXtA2H7JSmqMzgkXw6xeSpmcr0wpZtc+xs+BKnMvuZQ== 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=imG/RYoa56zQF4B2p39XCb/7A1PTc4xOSWxv+e/Y0a0=; b=eGdg8xSnn66pq/ETz5oiL8/gVMwMumToy+FJiFgXY5kvMTLQ1O7qh7+Cn32WSp0FJLZ07fIy30t9VkFT2DDNPUhWAJZD8F2BdQk4EWeTWhlPoiQhazHaISjuQhsmoB/R0EX1gOc0Ed14beltGxsxcy1gubmIeoMiXds2jePjEh1K17oIyXi1bgJudfifDUOi1GpCZWmFFxBD35T0DTf1VAWgcmicYrjzOnPn5yXa7UreX84nIL0QGr3niZBK4s6WczaQCZTGPcZc9wV91fAGvSVmTaqrEMKuYUJGXVxUov6mw4N3bbWgAtzpOi1xyhcVODr2yW+DN2f0NBxXxPa5DQ== 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=imG/RYoa56zQF4B2p39XCb/7A1PTc4xOSWxv+e/Y0a0=; b=3pFee3KZ1r4AhXOQJaQK8ueDRk1ZEwCtR8BvBpD+OZm85P+sTujPmHaZ+aO5b6prixXSE1CvLx7bUQRhOUielkt0f1LIb9Sp5DUJT+ZI0LJ7FtoEsZgV26aw8TUX8hKRWwVvXaVoj3mT/VZattkUj8uSgQUHUOendkVZydvu/50= Received: from BN0PR04CA0101.namprd04.prod.outlook.com (2603:10b6:408:ec::16) by BY1PR12MB8446.namprd12.prod.outlook.com (2603:10b6:a03:52d::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.360.9; Thu, 3 Sep 2026 05:31:37 +0000 Received: from BN2PEPF000055DB.namprd21.prod.outlook.com (2603:10b6:408:ec:cafe::98) by BN0PR04CA0101.outlook.office365.com (2603:10b6:408:ec::16) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.382.12 via Frontend Transport; Thu, 3 Sep 2026 05:31:37 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; 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 BN2PEPF000055DB.mail.protection.outlook.com (10.167.245.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.382.0 via Frontend Transport; Thu, 3 Sep 2026 05:31:36 +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; Thu, 3 Sep 2026 00:31:02 -0500 From: Srinivasan Shanmugam To: Subject: [PATCH 3/4] drm/xe: Convert xe_user_fence to drm_user_fence Date: Thu, 3 Sep 2026 10:59:44 +0530 Message-ID: <20260903052945.190435-3-srinivasan.shanmugam@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260903052945.190435-1-srinivasan.shanmugam@amd.com> References: <20260903052945.190435-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: BN2PEPF000055DB:EE_|BY1PR12MB8446:EE_ X-MS-Office365-Filtering-Correlation-Id: d8429c52-9ba2-43a5-17ef-08df097c9fa1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|82310400026|36860700016|23010399003|6133799003|10067099003|56012099006|11063799006|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: 3OsyOEpKPdoxYu0yPWJ7Z5RQsc+hBSiqMw3aGF+EO1LhZbfoq7LdOsXYcxy14onedbpTqHhWrwEGQgyMo1oeH6oA3CZEHPOZEy/u2bIcWSV2tvjEen63r2tG5w/gR+LZGK1fAqsrYpfdDncwgwn+2Dj2eWHHys7msigmztpQDr1qKzzNo/36C6qsbpbKbuZVF9K+BCeMWlctvJeOlE5KjR/b2OpRnxkb2sX+RaM0GnfYpNvJl2pAtXqTYVcaWzz9dCpgW6MOohPbXO2QdnBzfj19Nze7Rg0AQrGyXpLOT1DI9NjDyLALEJ/R0jnG5/uYqWdY7OEuzHJrEkQko172gT7zJB8WS3fnd0cbXY79bUsiTY9dMnxR/wB5XjUicN0Yf6bA6B1ZEisrL/db+vLLSsKdKXYDN6fmodbpMsEF60Md50nGkg8Or/g8NlWm9SRy013be5xt5VUONtJBHaN007zDMPGdgIwE8vBXUM00P6TCVAY/W1UZHoKGZHLHaTmj9UgTiqiG6dAjr9hW9iJvudytLdlocnoRKg9lqJ01Wse9h72A/pTE1Z8UCbwWt6k408kmL2L7pJq989H2jYwQaNgckMSGFqcO+BTtIShnrYjNK7T2jiz+QeGTXsvN4Pyy6HqZ/gmFk+m8RAyxtApUChVMUW6VuppYNulZv+qJVQrBxdH4M78HlhjBhC4ZNhk4fC2GkixXxwbMfjG6Mmpzkg== 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)(1800799024)(82310400026)(36860700016)(23010399003)(6133799003)(10067099003)(56012099006)(11063799006)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HFGUE+t0PCV5UTWGoNgbEVmZPsxquBLzG4n9Hk2Yag61q9fwYyFZZ+BhlzE4fcR6rreFss11g3NnqlaDCpzP/dcheFhYzk9+SkSjh9bONijQvqMsfHD4WYgkhpUJfkgIZSnEO4fUxbrFU069gFRBJkf34gcQgt6U/S3sVsNE4LNKLEeBtjjy8+cQ+je8fHKTGW3jaLiuY6zNnS1X/IEYaQssVt1hEnuNfXuA1QdLcNRbPRBndMvVFmcm0NKh2Oh7PV4Q6QUgMaHPNjMUbMvks0PQ5XTvHf1QMbDLZP/BjyUQD3wbRpWiboP5dRbQsf4Zkntq9xB8L10+Z4MBm8k31dQ7amMF0ztIB2dgS+IejgPFOEW2QpeUR2RI5v5ESGEcK4VaLbbzuEKlce0YL3vks3vS2q8A0l+xy0h5mDWYwtgOoO1Wk2C+MAEvc/VxZLmm X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2026 05:31:36.8215 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d8429c52-9ba2-43a5-17ef-08df097c9fa1 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: BN2PEPF000055DB.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY1PR12MB8446 Replace the open-coded user fence implementation in xe_sync.c with the new common drm_user_fence helper. struct xe_user_fence now embeds struct drm_user_fence as its base. XE-specific fields (xe_device pointer for the ufence_wq wake-up, userspace VA, expected value, signalled flag) remain in the wrapper. The local user_fence_destroy/get/put/worker/kick_ufence/user_fence_cb functions are removed. Their logic moves to xe_ufence_ops.worker and xe_ufence_ops.destroy, which are called by the drm_user_fence helper. Cc: Matthew Brost Cc: Mika Kuoppala Cc: Thomas Hellström Cc: Maarten Lankhorst Cc: Christian König Cc: dri-devel@lists.freedesktop.org Cc: intel-xe@lists.freedesktop.org Signed-off-by: Srinivasan Shanmugam --- drivers/gpu/drm/xe/xe_sync.c | 147 ++++++++++++++++------------- drivers/gpu/drm/xe/xe_sync.h | 2 + drivers/gpu/drm/xe/xe_sync_types.h | 1 - drivers/gpu/drm/xe/xe_vm.c | 1 + 4 files changed, 82 insertions(+), 69 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_sync.c b/drivers/gpu/drm/xe/xe_sync.c index 37866768d64c..a67ace940931 100644 --- a/drivers/gpu/drm/xe/xe_sync.c +++ b/drivers/gpu/drm/xe/xe_sync.c @@ -6,12 +6,11 @@ #include "xe_sync.h" #include -#include -#include #include #include #include +#include #include #include "xe_device.h" @@ -19,36 +18,58 @@ #include "xe_macros.h" #include "xe_sched_job_types.h" +/* + * xe_user_fence wraps drm_user_fence with XE-specific fields. + * The drm_user_fence base handles MM borrowing and work-item lifetime. + */ struct xe_user_fence { - struct xe_device *xe; - struct kref refcount; - struct dma_fence_cb cb; - struct work_struct worker; - struct mm_struct *mm; - u64 __user *addr; - u64 value; - int signalled; + struct drm_user_fence base; + struct xe_device *xe; + u64 __user *addr; + u64 value; + int signalled; }; -static void user_fence_destroy(struct kref *kref) +static void xe_ufence_worker(struct drm_user_fence *base, bool mm_ok) { - struct xe_user_fence *ufence = container_of(kref, struct xe_user_fence, - refcount); + struct xe_user_fence *ufence = container_of(base, struct xe_user_fence, base); - mmdrop(ufence->mm); - kfree(ufence); -} + /* + * Mark signalled before the userspace write so that if userspace + * observes the written value and immediately issues a VM unmap, + * check_ufence() will see signalled=1 and not return -EBUSY. + * + * NOTE: copy_to_user() may block if userspace backs the address with + * userfaultfd or FUSE. Drivers requiring non-blocking semantics should + * use a dedicated workqueue instead of xe->ordered_wq. + */ + WRITE_ONCE(ufence->signalled, 1); -static void user_fence_get(struct xe_user_fence *ufence) -{ - kref_get(&ufence->refcount); + if (mm_ok) { + if (copy_to_user(ufence->addr, &ufence->value, + sizeof(ufence->value))) + drm_dbg(&ufence->xe->drm, + "copy_to_user failed, user fence wasn't signaled\n"); + } else { + drm_dbg(&ufence->xe->drm, + "mmget_not_zero() failed, ufence wasn't signaled\n"); + } + + wake_up_all(&ufence->xe->ufence_wq); } -static void user_fence_put(struct xe_user_fence *ufence) +static void xe_ufence_destroy(struct drm_user_fence *base) { - kref_put(&ufence->refcount, user_fence_destroy); + struct xe_user_fence *ufence = container_of(base, struct xe_user_fence, base); + + kfree(ufence); } +static const struct drm_user_fence_ops xe_ufence_ops = { + .worker = xe_ufence_worker, + .destroy = xe_ufence_destroy, +}; + static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr, u64 value) { @@ -63,51 +84,22 @@ static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr, if (!ufence) return ERR_PTR(-ENOMEM); - ufence->xe = xe; - kref_init(&ufence->refcount); - ufence->addr = ptr; + ufence->xe = xe; + ufence->addr = ptr; ufence->value = value; - ufence->mm = current->mm; - mmgrab(ufence->mm); + drm_user_fence_init(&ufence->base, xe->ordered_wq, &xe_ufence_ops); return ufence; } -static void user_fence_worker(struct work_struct *w) -{ - struct xe_user_fence *ufence = container_of(w, struct xe_user_fence, worker); - - WRITE_ONCE(ufence->signalled, 1); - if (mmget_not_zero(ufence->mm)) { - kthread_use_mm(ufence->mm); - if (copy_to_user(ufence->addr, &ufence->value, sizeof(ufence->value))) - XE_WARN_ON("Copy to user failed"); - kthread_unuse_mm(ufence->mm); - mmput(ufence->mm); - } else { - drm_dbg(&ufence->xe->drm, "mmget_not_zero() failed, ufence wasn't signaled\n"); - } - - /* - * Wake up waiters only after updating the ufence state, allowing the UMD - * to safely reuse the same ufence without encountering -EBUSY errors. - */ - wake_up_all(&ufence->xe->ufence_wq); - user_fence_put(ufence); -} - -static void kick_ufence(struct xe_user_fence *ufence, struct dma_fence *fence) +static void user_fence_get(struct xe_user_fence *ufence) { - INIT_WORK(&ufence->worker, user_fence_worker); - queue_work(ufence->xe->ordered_wq, &ufence->worker); - dma_fence_put(fence); + drm_user_fence_get(&ufence->base); } -static void user_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb) +static void user_fence_put(struct xe_user_fence *ufence) { - struct xe_user_fence *ufence = container_of(cb, struct xe_user_fence, cb); - - kick_ufence(ufence, fence); + drm_user_fence_put(&ufence->base); } int xe_sync_entry_parse(struct xe_device *xe, struct xe_file *xef, @@ -282,24 +274,15 @@ void xe_sync_entry_signal(struct xe_sync_entry *sync, struct dma_fence *fence) } else if (sync->syncobj) { drm_syncobj_replace_fence(sync->syncobj, fence); } else if (sync->ufence) { - int err; - drm_syncobj_add_point(sync->ufence_syncobj, sync->ufence_chain_fence, fence, sync->ufence_timeline_value); sync->ufence_chain_fence = NULL; fence = drm_syncobj_fence_get(sync->ufence_syncobj); - user_fence_get(sync->ufence); - err = dma_fence_add_callback(fence, &sync->ufence->cb, - user_fence_cb); - if (err == -ENOENT) { - kick_ufence(sync->ufence, fence); - } else if (err) { + if (drm_user_fence_add_callback(&sync->ufence->base, fence)) XE_WARN_ON("failed to add user fence"); - user_fence_put(sync->ufence); - dma_fence_put(fence); - } + /* fence ref consumed by drm_user_fence_add_callback */ } } @@ -434,6 +417,34 @@ void xe_sync_ufence_put(struct xe_user_fence *ufence) user_fence_put(ufence); } +/** + * xe_sync_ufence_cancel() - Non-blocking cancel of user fence callback + * @ufence: user fence reference + * + * Attempts to cancel the pending callback without waiting for the worker. + * Safe to call while holding dma_resv_lock or vm->lock. If the callback + * has already fired, the worker runs independently — xe_ufence_worker + * only accesses device-level and userspace resources, both safe after + * VMA teardown. + */ +void xe_sync_ufence_cancel(struct xe_user_fence *ufence) +{ + drm_user_fence_cancel(&ufence->base); +} + +/** + * xe_sync_ufence_cancel_sync() - Cancel user fence callback and wait for worker + * @ufence: user fence reference + * + * Cancels any pending dma-fence callback and waits for the worker to fully + * complete before returning. Must be called during teardown before freeing + * any resources accessed by the worker. + */ +void xe_sync_ufence_cancel_sync(struct xe_user_fence *ufence) +{ + drm_user_fence_cancel_sync(&ufence->base); +} + /** * xe_sync_ufence_get_status() - Get user fence status * @ufence: user fence diff --git a/drivers/gpu/drm/xe/xe_sync.h b/drivers/gpu/drm/xe/xe_sync.h index 6b949194acff..b5ff3387c592 100644 --- a/drivers/gpu/drm/xe/xe_sync.h +++ b/drivers/gpu/drm/xe/xe_sync.h @@ -44,6 +44,8 @@ static inline bool xe_sync_is_ufence(struct xe_sync_entry *sync) struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence); struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync); void xe_sync_ufence_put(struct xe_user_fence *ufence); +void xe_sync_ufence_cancel(struct xe_user_fence *ufence); +void xe_sync_ufence_cancel_sync(struct xe_user_fence *ufence); int xe_sync_ufence_get_status(struct xe_user_fence *ufence); #endif diff --git a/drivers/gpu/drm/xe/xe_sync_types.h b/drivers/gpu/drm/xe/xe_sync_types.h index b88f1833e28c..67048199b070 100644 --- a/drivers/gpu/drm/xe/xe_sync_types.h +++ b/drivers/gpu/drm/xe/xe_sync_types.h @@ -12,7 +12,6 @@ struct drm_syncobj; struct dma_fence; struct dma_fence_chain; struct drm_xe_sync; -struct user_fence; struct xe_sync_entry { struct drm_syncobj *syncobj; diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c index b01f31ed4417..fbd2c28de8a6 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/xe/xe_vm.c @@ -1181,6 +1181,7 @@ static void xe_vma_destroy_late(struct xe_vma *vma) struct xe_bo *bo = xe_vma_bo(vma); if (vma->ufence) { + xe_sync_ufence_cancel(vma->ufence); xe_sync_ufence_put(vma->ufence); vma->ufence = NULL; } -- 2.34.1