Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D19CC61CE3 for ; Tue, 25 Aug 2026 05:58:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 146FA10E12E; Tue, 25 Aug 2026 05:58:13 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.b="phABz33b"; dkim-atps=neutral Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010040.outbound.protection.outlook.com [52.101.193.40]) by gabe.freedesktop.org (Postfix) with ESMTPS id B776C10E12E for ; Tue, 25 Aug 2026 05:58:11 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DxcLNhFy9CyIvYxIuuh6ZUQ6+aZXznqC6fC7wNUrPb3HYXaG6siVq3/P7McEGbmsgiZuOEwNhuVRTGVKhsQh9tEotDguWdxaa7UxzsyjkmNoCCg4zLKhiENfUBOABc/CQcS+Xh6z1SUHpfXqjf8g6xO0Pd0P3BqHYhxKnAHSrt+UJ0oKfLie5gHgf1yOPezYvy1XB7j8w+qyMWtghamLn+ZqVJM5ZAonIMTXNOgygc0VM4izQVYyz//yz1sfhQ+xhChtc5g0O1f4lHuhwbxxGZ8kzK0gHoZM7IM2A4WQRwV8cBGl6MzAX92rqjcRbEnyXw8P/GFqSEN/Y5i07HKU1A== 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=Om4HsTxSW1cwaX2oBVp4Mrj6pSj1tBJBto3456KjVVc=; b=FiCkMvpLDqCu1ukn/OkJ+B2SULVQl8ENhYGNRKM1hNSv8ztUxoHSSiP6ZdZCGUgOUuElYbv7FY58xjaCuknuvTtXgwF58b5RoyruEEqxIfnAUjtLfslHcmPdkUg1o02Aqru0hMlyk87W+Y/QoI3LHl9BxOWrFJ/SQ0U2IdS+tNTwP0v3Itd3LR34eKqKqaw4tPgo0F5dgIZa63J4kYSOeIDjhUTCQJZCUATR6pe90waCwK5B1jhaRsXOHfxd6+8kT2KRezGfz73LTj1mbrR11zGNwCsQE9jPP0LRKquQ5SiUog8n1rwBc36P6Ly5ikArvPzdxFeCJRScqb/sJUqIPQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none 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=Om4HsTxSW1cwaX2oBVp4Mrj6pSj1tBJBto3456KjVVc=; b=phABz33b4N8WvsvMCYQHrojzab69A8ipLD8XFSfJe4DDUmnuoMWTynbrs/i2i6LJFTgI4FZRYRa4VGRbcgZbsdeFVuoQgAbY068Xg4LSy38oGzx3JKCB3FUcdOSaldIMa2PVnAVu2aTimLpbHcbuYunLEOs5HKuU3JtiZjrpTq0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB9457.namprd12.prod.outlook.com (2603:10b6:610:27c::7) by CY3PR12MB9556.namprd12.prod.outlook.com (2603:10b6:930:10a::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.339.12; Tue, 25 Aug 2026 05:58:08 +0000 Received: from CH2PR12MB9457.namprd12.prod.outlook.com ([fe80::85a8:1df:840a:cd4c]) by CH2PR12MB9457.namprd12.prod.outlook.com ([fe80::85a8:1df:840a:cd4c%4]) with mapi id 15.21.0339.012; Tue, 25 Aug 2026 05:58:08 +0000 Content-Type: multipart/alternative; boundary="------------7EXRCXrX0rIHfMnyMzHs70f7" Message-ID: Date: Tue, 25 Aug 2026 13:58:02 +0800 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] drm/amdgpu: keep the userq manager alive as long as its queues To: =?UTF-8?Q?Christian_K=C3=B6nig?= , Alexander.Deucher@amd.com, felix.kuehling@amd.com Cc: Ray.Huang@amd.com, amd-gfx@lists.freedesktop.org References: <20260818115753.127358-1-lingshan.zhu@amd.com> Content-Language: en-US From: "Zhu, Lingshan" In-Reply-To: X-ClientProxiedBy: TYCP286CA0083.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2b3::11) To CH2PR12MB9457.namprd12.prod.outlook.com (2603:10b6:610:27c::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB9457:EE_|CY3PR12MB9556:EE_ X-MS-Office365-Filtering-Correlation-Id: 1b6cd916-7731-4e57-8393-08df026dd647 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024|376014|23010399003|18002099003|22082099003|56012099006|11063799006|8096899003|6133799003|4143699003|10067099003; X-Microsoft-Antispam-Message-Info: zdQ2bJ5HYRVOOAW4a/t1DLRl3X7WZqwShiFBNyPEnmnES7tMks4LT0X02KQGDm1XNl5jC6hEhwwFKS6f+DaMGDlmZR5n+mxHtO1ZxxkPnjwT4NLhcsdvtqNHUOF+Z8ZYEGLBs93zT/auWRmokXJwZhzZLzArYAGDXUCrlG/Dq/SJ3wMlqulZL9WDuBhLLoDiv8OuJJ4VuPQYJPSwZyGeaNGRr3WB1RAC2cRqj7fzDjOPtt9Bje/64OXyPUMAD1rGnK6tCjg8WV1/Vng+SVzfHhonUpkgci0wIwiA0OvVRAZFD2Q5auYOi48Ct2H/NKA+hVtFOtJ058sg7BhY1AM1b2jr2KXTDQK75p5u4shV/BHHpjH/BoHSv/cb1M2TnXnJMcOQMDU6L8uDnFvfQMwX4JR7a9ly1GiOUuIMsbN4X8G2z6cAPsdaRgJIlZPrneOj7X62Oawpna+efT4QigxFOilGIv45ZhKge2m2Xf9E/y4R64YFxIWk2GLrtZbzrPQyoB9tRmP2KhVudUqG1hTI7Cfr0qw04SSPRcYdpkQ3BEk8EnJinrxoF4g2qUiu0UoR6wheTkwNf75gSEV2xBHBN9QRhltnt/p9ld0E/f026uGgfHxpiGhGia45jaIissWlkfUcNUxf+1qdUA9C5ZQYo2oqoZUobUbA7Jcm5a25NjM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR12MB9457.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(376014)(23010399003)(18002099003)(22082099003)(56012099006)(11063799006)(8096899003)(6133799003)(4143699003)(10067099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?bVZxTjNFY0x1R1RUWFFXdWprUkpRVm5RQ1NBUDZCS0dYWE1ORlhodFROYzNy?= =?utf-8?B?WGh5VlB3NC9pZ2hWM2tIK0piL1RCVi9CMkt4b2Y5SkdkNzNWbmR2cWNPb3JP?= =?utf-8?B?SzRBR1pjNTVTdTZvQ3U4bXZUQndGc3EvV003VVNwV0hIQzRIRndpaDFxbE1R?= =?utf-8?B?VjROdzBYVkFuMFVCY0ZBZXVTRS9zQkNQZTlxdFhCRG5OVXlUMExSSVJERkpO?= =?utf-8?B?THArSWxoSzlmNEYyZmpIOVJjYVZ6U2RJL1NadzJrYTRaNnExemRKTkh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UJtQmNLTWdRUlRReGMv?= =?utf-8?B?cHBiZDNQSGZFaWI0VDQxNndjbDZMalZDMkxZNXBGbW1aTFVPd1M2Y3AxV2JE?= =?utf-8?B?eGg1a2t6eFdocFoxU1YyQ3cwL0Z4ckduelZ4TkZPUldRR1dNY3dLM3RIN3J2?= =?utf-8?B?d2JVb2pmY1A5cjcwbGcrMTdtbDFhY0Q2T0c2N1N1OTVXOG9oWmIweDRUU2JC?= =?utf-8?Q?65x7qMmIVH05ykE64+?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1b6cd916-7731-4e57-8393-08df026dd647 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB9457.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2026 05:58:08.1283 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: NodLloMCNeYnQTWW3rPaxXBKCbdPmtZ8/2TFGUYAH8k8w6dFfLyi52ytCEz/EpyWwKNNOwwxzMzxw3+6UFLzHw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY3PR12MB9556 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" --------------7EXRCXrX0rIHfMnyMzHs70f7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 8/24/2026 10:38 PM, Christian König wrote: > > On 8/18/26 13:57, Zhu Lingshan wrote: >> The userq manager is embedded in fpriv, so any code >> paths trigger kfree(fpriv) would destroy the userq_mgr, >> even some of its queues are still alive, >> resuting in userq->userq_mgr use-after-free issues. >> >> This commit fixes this problem by introduce a new >> counter refs representing for the number of its queues, >> and only free the userq_manager when refs == 0 > Well the userqueues are kept alive as long as there are IOCTLs using it. > > And and IOCTL in turn keeps a reference on the file and so the fpriv which contains the userq manager. > > So that here doesn't make any sense at all. > > If there is still an userq alive when the userq manager is destroyed we should probably call BUG(), cause that mean a major reference count issue in a core kernel component. This can be more complex, what if the amdgpu_userq_put in amdgpu_userq_mgr_fini is not the last put of a queue, that means the queue is still alive even when the userq manager has been destroyed. I will put this patch in my next series where the problem is better demonstrated and more reachable. Thanks Lingshan > > Regards, > Christian. > >> Signed-off-by: Zhu Lingshan >> --- >> drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 27 +++++++++++++++++++++++ >> drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 19 ++++++++++++++++ >> 2 files changed, 46 insertions(+) >> >> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c >> index 24adad7be251..434384b53b85 100644 >> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c >> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c >> @@ -533,6 +533,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, >> return r; >> } >> >> +static void amdgpu_userq_mgr_inc_refs(struct amdgpu_userq_mgr *uq_mgr) >> +{ >> + atomic_inc(&uq_mgr->refs); >> +} >> + >> +static void amdgpu_userq_mgr_dec_refs(struct amdgpu_userq_mgr *uq_mgr) >> +{ >> + if (atomic_dec_and_test(&uq_mgr->refs)) >> + wake_up(&uq_mgr->refs_wq); >> +} >> + >> static int >> amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) >> { >> @@ -594,6 +605,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) >> r = amdgpu_userq_destroy(uq_mgr, queue); >> if (r) >> drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r); >> + >> + amdgpu_userq_mgr_dec_refs(uq_mgr); >> } >> >> struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid) >> @@ -677,6 +690,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) >> queue->xcp_id = (fpriv->xcp_id != AMDGPU_XCP_NO_PARTITION) ? >> fpriv->xcp_id : 0; >> queue->userq_mgr = uq_mgr; >> + amdgpu_userq_mgr_inc_refs(uq_mgr); >> INIT_DELAYED_WORK(&queue->hang_detect_work, >> amdgpu_userq_hang_detect_work); >> >> @@ -789,6 +803,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) >> free_queue: >> trace_amdgpu_userq_create_end(queue, r); >> kfree(queue); >> + amdgpu_userq_mgr_dec_refs(uq_mgr); >> err_pm_runtime: >> pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); >> return r; >> @@ -1277,6 +1292,8 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f >> { >> mutex_init(&userq_mgr->userq_mutex); >> xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC); >> + atomic_set(&userq_mgr->refs, 0); >> + init_waitqueue_head(&userq_mgr->refs_wq); >> userq_mgr->adev = adev; >> userq_mgr->file = file_priv; >> userq_mgr->proc_ctx_allocated = false; >> @@ -1326,6 +1343,16 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) >> amdgpu_userq_put(queue); >> } >> >> + /* >> + * userq_mgr is embedded in struct amdgpu_fpriv and get >> + * freed when kfree(fpriv). This wait_evet() blocks >> + * amdgpu_userq_mgr_fini() which is drm_release path, >> + * so keep userq_mgr alive while any queues holding it. >> + * >> + * This prevents queue->userq_mgr use-after-free issues. >> + */ >> + wait_event(userq_mgr->refs_wq, !atomic_read(&userq_mgr->refs)); >> + >> xa_destroy(&userq_mgr->userq_xa); >> >> /* >> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h >> index 6412a7f7b6ef..7141383ea2ae 100644 >> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h >> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h >> @@ -24,6 +24,8 @@ >> >> #ifndef AMDGPU_USERQ_H_ >> #define AMDGPU_USERQ_H_ >> +#include >> + >> #include "amdgpu_eviction_fence.h" >> >> #define AMDGPU_MAX_USERQ_COUNT 512 >> @@ -126,6 +128,23 @@ struct amdgpu_userq_mgr { >> */ >> struct xarray userq_xa; >> struct mutex userq_mutex; >> + >> + /** >> + * @refs: >> + * >> + * Each queue increase this counter when join this manager, >> + * and decreash this counter when leave this manager. >> + */ >> + atomic_t refs; >> + >> + /** >> + * @refs_wq: >> + * >> + * Waited on by amdgpu_userq_mgr_fini() until @refs drops to zero. >> + * This wait prevents userq->userq_mgr UAF >> + */ >> + wait_queue_head_t refs_wq; >> + >> struct amdgpu_device *adev; >> struct delayed_work resume_work; >> struct drm_file *file; --------------7EXRCXrX0rIHfMnyMzHs70f7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 8/24/2026 10:38 PM, Christian König wrote:

On 8/18/26 13:57, Zhu Lingshan wrote:
The userq manager is embedded in fpriv, so any code
paths trigger kfree(fpriv) would destroy the userq_mgr,
even some of its queues are still alive,
resuting in userq->userq_mgr use-after-free issues.

This commit fixes this problem by introduce a new
counter refs representing for the number of its queues, and only free the userq_manager when refs == 0
Well the userqueues are kept alive as long as there are IOCTLs using it.

And and IOCTL in turn keeps a reference on the file and so the fpriv which contains the userq manager.

So that here doesn't make any sense at all.

If there is still an userq alive when the userq manager is destroyed we should probably call BUG(), cause that mean a major reference count issue in a core kernel component.
This can be more complex, what if the amdgpu_userq_put in amdgpu_userq_mgr_fini is not the last put of a queue, that means the queue is still alive even when
the userq manager has been destroyed. I will put this patch in my next series where the problem is better demonstrated and more reachable.

Thanks
Lingshan

Regards,
Christian.

Signed-off-by: Zhu Lingshan <lingshan.zhu@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 27 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 19 ++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index 24adad7be251..434384b53b85 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -533,6 +533,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,
 	return r;
 }
 
+static void amdgpu_userq_mgr_inc_refs(struct amdgpu_userq_mgr *uq_mgr)
+{
+	atomic_inc(&uq_mgr->refs);
+}
+
+static void amdgpu_userq_mgr_dec_refs(struct amdgpu_userq_mgr *uq_mgr)
+{
+	if (atomic_dec_and_test(&uq_mgr->refs))
+		wake_up(&uq_mgr->refs_wq);
+}
+
 static int
 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
 {
@@ -594,6 +605,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref)
 	r = amdgpu_userq_destroy(uq_mgr, queue);
 	if (r)
 		drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r);
+
+	amdgpu_userq_mgr_dec_refs(uq_mgr);
 }
 
 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid)
@@ -677,6 +690,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	queue->xcp_id = (fpriv->xcp_id != AMDGPU_XCP_NO_PARTITION) ?
 				fpriv->xcp_id : 0;
 	queue->userq_mgr = uq_mgr;
+	amdgpu_userq_mgr_inc_refs(uq_mgr);
 	INIT_DELAYED_WORK(&queue->hang_detect_work,
 			  amdgpu_userq_hang_detect_work);
 
@@ -789,6 +803,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 free_queue:
 	trace_amdgpu_userq_create_end(queue, r);
 	kfree(queue);
+	amdgpu_userq_mgr_dec_refs(uq_mgr);
 err_pm_runtime:
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 	return r;
@@ -1277,6 +1292,8 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
 {
 	mutex_init(&userq_mgr->userq_mutex);
 	xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
+	atomic_set(&userq_mgr->refs, 0);
+	init_waitqueue_head(&userq_mgr->refs_wq);
 	userq_mgr->adev = adev;
 	userq_mgr->file = file_priv;
 	userq_mgr->proc_ctx_allocated = false;
@@ -1326,6 +1343,16 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 		amdgpu_userq_put(queue);
 	}
 
+	/*
+	 * userq_mgr is embedded in struct amdgpu_fpriv and get
+	 * freed when kfree(fpriv). This wait_evet() blocks
+	 * amdgpu_userq_mgr_fini() which is drm_release path,
+	 * so keep userq_mgr alive while any queues holding it.
+	 *
+	 * This prevents queue->userq_mgr use-after-free issues.
+	 */
+	wait_event(userq_mgr->refs_wq, !atomic_read(&userq_mgr->refs));
+
 	xa_destroy(&userq_mgr->userq_xa);
 
 	/*
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 6412a7f7b6ef..7141383ea2ae 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
@@ -24,6 +24,8 @@
 
 #ifndef AMDGPU_USERQ_H_
 #define AMDGPU_USERQ_H_
+#include <linux/wait.h>
+
 #include "amdgpu_eviction_fence.h"
 
 #define AMDGPU_MAX_USERQ_COUNT 512
@@ -126,6 +128,23 @@ struct amdgpu_userq_mgr {
 	 */
 	struct xarray			userq_xa;
 	struct mutex			userq_mutex;
+
+	/**
+	 * @refs:
+	 *
+	 * Each queue increase this counter when join this manager,
+	 * and decreash this counter when leave this manager.
+	 */
+	atomic_t			refs;
+
+	/**
+	 * @refs_wq:
+	 *
+	 * Waited on by amdgpu_userq_mgr_fini() until @refs drops to zero.
+	 * This wait prevents userq->userq_mgr UAF
+	 */
+	wait_queue_head_t		refs_wq;
+
 	struct amdgpu_device		*adev;
 	struct delayed_work		resume_work;
 	struct drm_file			*file;

    
--------------7EXRCXrX0rIHfMnyMzHs70f7--