Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 162A1606A327 for ; Tue, 1 Dec 2026 05:32:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=fM3S+Uyq Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A83A510E0A5; Tue, 1 Dec 2026 05:32:22 +0000 (UTC) Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010008.outbound.protection.outlook.com [52.101.85.8]) by gabe.freedesktop.org (Postfix) with ESMTPS id C9DC589048 for ; Tue, 1 Dec 2026 05:32:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=H6Xqtnkwyl+/OdMzGG2t/VQIdbijwVyhLtqJU/235nhjhlEy4SBMBPFPPJ0njnfrkWAkEBa+EouweiTAQmfnq6yMZYjlRu+AbM0cDz8w2ccdQX4FcBnEJkmsolp6QsvTGcL6U7ICuTTfvk9zZM6cF976Ii1jsKyPAqWOFjSna2l8hfggEWEPKmgd0uTi3wxU8nHYvL3ZCyMNJ7lRpYmt2qGVLVgDZwMNPkdrCshHDHNnnkmRaTJOqYbx0JkP3mG1Mqy1Zb+EqWy7XVZCKM30GbjG7gcRA0DcYTjrgtZcEGJnNEFIqhPjVO75yXMqpJ5V/y5PdEaTBNkuwhk3jZnmGw== 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=uST28lQje22pxcv6GXG46hWNQQBfpv+Mu83CcC2vjkM=; b=BnQl5Q4iAqpNYyM0HLvGZo5yQHMOih72wMGb4TP99/cSim8Tdwio0FVn7C5U/uf+3X5Glxro62NiAjr0gAAaTOs71SCHIFEf3RI/LV6P2kxyk4EKVfuEx+ZVgWkkvDJyIt5ruTWMDG+cO8KZ01fNNj/IVWvSQ0O70Zyo+5MhBb+fywklIK9OukHj64gVtxxF9LkoC9wVgl76wKVkvq8lrwWo3qoPrX12gmCphyxnQbgLbyykyrIA6AS/+LUVMSsuiLgPo4rzIR/eZ7VRKGgc9HrqHhBo0n4xWQfUZ3kpZOHxTRLbn6nbUSORByhK9ooqHku2iDNSyKwBrzUoiJkWrA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.freedesktop.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=uST28lQje22pxcv6GXG46hWNQQBfpv+Mu83CcC2vjkM=; b=fM3S+Uyq+RWr3j9QzVG7p7VWmNOpoaQO3SQUdROaeTyGasiQioB9BM44UytBoD+8Kcs72S4XUcCbKJF+3Zr9PXJSKggb45MjodX0tm3eZREaME+RnZLIZOsQ62ZI68vu+iUQBWQAXUtM50H0htCIwxM7bGD+Poidtw0yyvJesoE= Received: from MW4PR03CA0027.namprd03.prod.outlook.com (2603:10b6:303:8f::32) by BL1PR12MB5969.namprd12.prod.outlook.com (2603:10b6:208:398::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.13; Tue, 1 Dec 2026 05:32:10 +0000 Received: from SJ5PEPF000001EA.namprd05.prod.outlook.com (2603:10b6:303:8f:cafe::62) by MW4PR03CA0027.outlook.office365.com (2603:10b6:303:8f::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.10 via Frontend Transport; Tue, 1 Dec 2026 05:32:10 +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 SJ5PEPF000001EA.mail.protection.outlook.com (10.167.242.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Tue, 1 Dec 2026 05:32:10 +0000 Received: from Satlexmb09.amd.com (10.181.42.218) 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.57; Mon, 30 Nov 2026 23:32:09 -0600 Received: from satlexmb08.amd.com (10.181.42.217) by satlexmb09.amd.com (10.181.42.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Mon, 30 Nov 2026 23:32:09 -0600 Received: from JesseDEV.amd.com (10.180.168.240) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server id 15.2.2562.57 via Frontend Transport; Mon, 30 Nov 2026 23:32:08 -0600 From: Jesse Zhang To: CC: , Christian Koenig , Srinivasan Shanmugam Subject: [PATCH v2 02/11] drm/amdgpu: Stop queue notifications before event cleanup Date: Tue, 1 Dec 2026 13:30:45 +0800 Message-ID: <20261201053159.2550315-3-Jesse.Zhang@amd.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261201053159.2550315-1-Jesse.Zhang@amd.com> References: <20261201053159.2550315-1-Jesse.Zhang@amd.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ5PEPF000001EA:EE_|BL1PR12MB5969:EE_ X-MS-Office365-Filtering-Correlation-Id: 992492b9-d13f-4dea-5ad6-08df4f6c7233 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|376014|36860700016|23010399003|1800799024|22082099003|18002099003|11063799006|10067099003|56012099006|261026140227099003|261026121527099003|261028144727099003|261009223027099003|261021123027099003|261022075727099003|261102123227099003|261022105027099003|261021121427099003; X-Microsoft-Antispam-Message-Info: 6g+iG+yMQGcH6qlVxt7uoxz6I6bS/A/afZHUeIR88D0em1Ouqh8un9sEJzCX+MoclZ41T/MW7p+AOyPDdipS4BRHze0pCTSfXnn2waAUuDw/pYjoMK46DWY8x8amyx4OALKlJRYFMJkv+3bXZ2Fh7E+zBIFjJx95tYxtSXD9tQubltBBeSUYU6NPnWuWysOeu7r/JekRxGl0bOy07wZz9VraiEiDB8dNwWbbciCbQm1sqR7l+l9/CLo+CislAwc3sCfTvqXt3iafBjuGEJgc8Zb4TnIMNh4t9LlSNjbjf09yfeW7cnh1XAKIoEx+4FYpkQ7DwVpR/ACQLVdicLHaLMFfwmu2Qyvi3kDWv9hRCfvIRbc48mX6TfApAAwsCkl+Zs60CkNII40O6un1LzbgtSVVwYO9wfPZ3azShTIHUkyIRWVenwQ5/aElvTLZb6A5VeU/GX5VADLImr60iSxClpuDm8dBOFmrFYO7CSMvSnANdzOHB+a6F2fp01k53zJXMmaLn+GrceMulbhGiU7P5LE+p5DlfWhro7NH85WWyBOHzo9vCdXGerDk408toCAvcpjM7DZy6ZzcWUeL5yQN91udkE7h1yoVgrCUIzbZAbrlKvnij0AV7j6BYQDOLDUiehIGrrgomT+FmR0/TC3DRPAdUhGJi8d4qBGHDbU4s7a79NumgBXySWGzNluSZFqShstlLwfPlT9EV39lfC+lJQ== 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)(82310400026)(376014)(36860700016)(23010399003)(1800799024)(22082099003)(18002099003)(11063799006)(10067099003)(56012099006)(261026140227099003)(261026121527099003)(261028144727099003)(261009223027099003)(261021123027099003)(261022075727099003)(261102123227099003)(261022105027099003)(261021121427099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rarBqB05i5bKTOhMsU8G8lbPqkt1Y9g6Y54IuiemlNbn+T+DY2SaUEd5OSDayZhqNlyFG5qzNZoXGT3YKRFkB1PJ9qPzf7Tw/mXe5pD2GjwP/bGDmUd7+pFshbEV2fvEnxOmueQo5HW4tcZCoBvAhcxEPETu0XMYiC5fvvwPCRdExbZbQeQEjEl9rM9BT65Y2gkGVcNfITNPDLM0bcS5+t5uJ5zNbAUD8U3HYqlDy17EkRr2cT1DweXZJF7wGGXdPonOrJNGQVQcHpn9HfUZ2qL/Sqbeq0+Er9YDn+pb+wYJiJsn0pan7CzFx4VeSd0pDafY+I7ibzEABaM0Psiu2CvosajIZls2lqK1b8FQ5IvkScQoj0HNZby1f3sjp1pQiHehPowgd/hd0ortLQM3WiYmtXWYb+QFAaWNhDquse2Rz14pgq0VOZ68RocK26Df X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 05:32:10.0157 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 992492b9-d13f-4dea-5ad6-08df4f6c7233 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: SJ5PEPF000001EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5969 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Srinivasan Shanmugam Queue interrupts can publish WAIT_EVENT records after cleanup has removed existing records, potentially acquiring a queue reference after its reference count has reached zero. Close queue notifications under the existing doorbell and EVENTFD locks before removing subscriptions and pending records. Check this state in the EOP and queue-reset producers while preserving fence completion. Use the common cleanup helper for queue deletion, file teardown and creation failures after doorbell publication. Signed-off-by: Srinivasan Shanmugam --- drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c | 12 +++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 50 +++++++++++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 6 +++ .../drm/amd/amdgpu/amdgpu_userq_internal.h | 2 + 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c index 4288ee6b5409..9a60b5a73fe3 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c @@ -272,10 +272,15 @@ int amdgpu_eventfd_bind(struct amdgpu_eventfd_mgr *mgr, e->queue = queue; /* - * Re-check duplicate under lock to close the race with another bind() - * for the same (event_type, queue, ctx). + * Serialize subscription insertion with queue cleanup and competing + * binds for the same (event_type, queue, ctx). */ xa_lock_irqsave(&mgr->ids, flags); + if (queue && amdgpu_userq_events_closed(queue)) { + xa_unlock_irqrestore(&mgr->ids, flags); + r = -ENOENT; + goto out_free_entry; + } hlist_for_each_entry(it, &id->entries, hnode) { if (it->ctx == ctx && it->queue == queue) { dup = true; @@ -385,7 +390,8 @@ int amdgpu_eventfd_unbind(struct amdgpu_eventfd_mgr *mgr, * @mgr: eventfd manager * @queue: queue being destroyed/released * - * Remove all subscriptions that hold a reference to @queue. + * Remove all subscriptions referencing @queue. The caller must first + * stop new binds for this queue. * This is called when the queue id is released so EVENTFD can drop * its queue references before the queue is finally destroyed. */ diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c index 1aa638fb4578..f60a9b437cff 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c @@ -57,6 +57,11 @@ amdgpu_userq_eventfd_mgr(struct amdgpu_userq_mgr *userq_mgr) return &fpriv->eventfd_mgr; } +bool amdgpu_userq_events_closed(struct amdgpu_usermode_queue *queue) +{ + return READ_ONCE(queue->events_closed); +} + /* * Get the per-file wait-event manager associated with this userq manager. */ @@ -75,6 +80,30 @@ amdgpu_userq_wait_event_mgr(struct amdgpu_userq_mgr *userq_mgr) return &fpriv->wait_event_mgr; } +/* + * Stop notification producers before removing their subscriptions and records. + * The caller holds a queue reference and keeps its DRM file alive. + */ +static void amdgpu_userq_stop_events(struct amdgpu_usermode_queue *queue) +{ + struct amdgpu_userq_mgr *userq_mgr = queue->userq_mgr; + struct amdgpu_eventfd_mgr *eventfd_mgr = + amdgpu_userq_eventfd_mgr(userq_mgr); + struct xarray *xa = &userq_mgr->adev->userq_doorbell_xa; + unsigned long flags; + + /* Match the IRQ producer's lock order. */ + xa_lock_irqsave(xa, flags); + xa_lock(&eventfd_mgr->ids); + queue->events_closed = true; + xa_unlock(&eventfd_mgr->ids); + xa_unlock_irqrestore(xa, flags); + + amdgpu_eventfd_remove_queue(eventfd_mgr, queue); + amdgpu_wait_event_remove_queue(amdgpu_userq_wait_event_mgr(userq_mgr), + queue); +} + u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) { int i; @@ -186,7 +215,7 @@ amdgpu_userq_handle_hung_queue(struct amdgpu_device *adev, xa_lock_irqsave(xa, flags); lookup = xa_load(xa, queue->doorbell_index); - if (lookup != queue) + if (lookup != queue || queue->events_closed) goto out_unlock; wait_event_mgr = amdgpu_userq_wait_event_mgr(queue->userq_mgr); @@ -321,6 +350,9 @@ void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell) if (r == 1) amdgpu_userq_start_hang_detect_work(queue); + if (queue->events_closed) + goto out_unlock; + wait_event_mgr = amdgpu_userq_wait_event_mgr(queue->userq_mgr); amdgpu_wait_event_add(wait_event_mgr, DRM_AMDGPU_EVENT_TYPE_USERQ_EOP, @@ -331,6 +363,7 @@ void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell) DRM_AMDGPU_EVENT_TYPE_USERQ_EOP, queue); } +out_unlock: xa_unlock_irqrestore(xa, flags); } @@ -1015,9 +1048,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT), GFP_KERNEL); if (r) { + amdgpu_userq_stop_events(queue); /* - * This drops the last reference which should take care of - * all cleanup. + * Drop the creator's reference after removing event references. */ trace_amdgpu_userq_create_end(queue, r); amdgpu_userq_put(queue); @@ -1030,6 +1063,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) return 0; erase_doorbell: + amdgpu_userq_stop_events(queue); if (queue->queue_type == AMDGPU_HW_IP_GFX || queue->queue_type == AMDGPU_HW_IP_COMPUTE) { if (queue->is_xnack_plus) @@ -1244,8 +1278,7 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data, if (!queue) return -ENOENT; - amdgpu_eventfd_remove_queue(&fpriv->eventfd_mgr, queue); - amdgpu_wait_event_remove_queue(&fpriv->wait_event_mgr, queue); + amdgpu_userq_stop_events(queue); amdgpu_userq_put(queue); break; @@ -1705,12 +1738,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) if (!queue) break; - amdgpu_eventfd_remove_queue(amdgpu_userq_eventfd_mgr(userq_mgr), - queue); - - amdgpu_wait_event_remove_queue( - amdgpu_userq_wait_event_mgr(userq_mgr), - queue); + amdgpu_userq_stop_events(queue); amdgpu_userq_put(queue); } diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h index 3cd0c97698a8..f2788f1eb2f1 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h @@ -101,6 +101,12 @@ struct amdgpu_usermode_queue { struct delayed_work hang_detect_work; struct kref refcount; + /* + * Readers hold userq_doorbell_xa or the EVENTFD manager's ids lock. + * Writers hold both, in that order. + */ + bool events_closed; + union { struct { u64 queue_rb; diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_internal.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_internal.h index 4279b1470b3c..5d7634bd745f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_internal.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_internal.h @@ -35,4 +35,6 @@ amdgpu_userq_get(struct amdgpu_userq_mgr *userq_mgr, u32 queue_id); void amdgpu_userq_put(struct amdgpu_usermode_queue *queue); +bool amdgpu_userq_events_closed(struct amdgpu_usermode_queue *queue); + #endif /* __AMDGPU_USERQ_INTERNAL_H__ */ -- 2.50.1