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 DE0D6606A69F for ; Thu, 3 Dec 2026 20:55:21 +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=UjgJ0FN0 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 80DD0112BD7; Thu, 3 Dec 2026 20:55:16 +0000 (UTC) Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012011.outbound.protection.outlook.com [40.107.209.11]) by gabe.freedesktop.org (Postfix) with ESMTPS id 718F8112C20 for ; Thu, 3 Dec 2026 20:55:10 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WsnlwcEiR0YENLA975rG9Fh6824rZqNw/dbERSQ81FNQ07V4KEpMz/W3GnXC0KizREWNsclYKCNPjg7e7Uqt+lV9BXmeeMssZ1K/qt/9exgnHicnYhzeDdHRmp6YSLlo6j1JV5W6mbs0m32jBgw0FtKcgYt/Gx6fLOHwiSQKiruHGzwIs0X7smnhVM4xlBEFuNml96W/+KbeZQVfvvq3kahUrynTGE2yh1o3/5qXvTHl6oVj8nR8531iU3cnAce2BZFBPw8GMd8IIzzluqd9QMfyThJClWnE+JcTO5E2DqDsxkR9s/mTecG5XQbIq2aEukZn97XtuqMfjr/7sXx4Rw== 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=8aftlISzqn/wF+jG5oQqg03+m7TmbIk/vnskh68j3II=; b=i/OmkQYkCOwnEW+OO0qCqXi9P0J0fidYpOTIlDJojb8wltU47HD6r+gBAyoH2r5Uu5k9g34pmrlIbeic0YKI1trQQQcAazE8+KrFVo6sNHqZESGZ+YbjA8AMSmydN06qOgn2o5HLyU7QXJw5v5hyIZSp2030mM7iU2y8wYq9UaJ3twe4XFSyexA6dqWI7X9BdVt45wKxM3JsGDOYzFA71ao4ctGAHIutMmuCTXqHSEgVRC/gFGaSQ7L1suhrOl+Fb4uh1j/S3ztaGQ9K/HLVUoCCPlMFJlIHCi43Ey4UOk/Tk5HgUAntT5CHLprkjSMQ69Q/ole3zcDrlHjHGnV3/A== 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=8aftlISzqn/wF+jG5oQqg03+m7TmbIk/vnskh68j3II=; b=UjgJ0FN0I8gWqJu0qzCBZ7ABoRcSbrTHd/b9UiAFsrTWvCuS/G25Wz8aCfkjEAMewNbB9rMW58AJZxL6UG52CJWhsahJD1hhx2JQzz3fbd7MddDg1+/1Uc5Wdh9xaaVkkWkNdI98HB0NKq4gRuEhI5Y9PKpYfqkPFYiPFxzq67M= Received: from MW4PR03CA0284.namprd03.prod.outlook.com (2603:10b6:303:b5::19) by IA0PR12MB8424.namprd12.prod.outlook.com (2603:10b6:208:40c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Thu, 3 Dec 2026 20:55:06 +0000 Received: from SJ5PEPF000001CC.namprd05.prod.outlook.com (2603:10b6:303:b5:cafe::53) by MW4PR03CA0284.outlook.office365.com (2603:10b6:303:b5::19) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.11 via Frontend Transport; Thu, 3 Dec 2026 20:55:06 +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 SJ5PEPF000001CC.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.6 via Frontend Transport; Thu, 3 Dec 2026 20:55:06 +0000 Received: from 0yonsun-linux-dev.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.57; Thu, 3 Dec 2026 14:55:05 -0600 From: Yongqiang Sun To: CC: Yongqiang Sun Subject: [PATCH 1/2] drm/amdkfd: fix CREATE_QUEUE on a released process Date: Thu, 3 Dec 2026 15:54:52 -0500 Message-ID: <20261203205452.62176-1-Yongqiang.Sun@amd.com> X-Mailer: git-send-email 2.43.0 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: SJ5PEPF000001CC:EE_|IA0PR12MB8424:EE_ X-MS-Office365-Filtering-Correlation-Id: de9de4a5-783f-47ff-8cc3-08df517fb5b3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|376014|1800799024|36860700016|23010399003|5023799004|11063799006|261009223027099003|18002099003|56012099006|10067099003|261026121527099003|261102123227099003|261026140227099003|261021121427099003|261022105027099003|261021123027099003|261028144727099003; X-Microsoft-Antispam-Message-Info: 3Gpg83HdX5PBOf2PbznrsHAbxZvPuewY1iEx9c0PuZUT3Q6TaxmF8Wf04+IiFUR8Qxs0gz9jIDaEqE/QpNI4EH4/hboIkkkRJvIfeyMblDZ6yYjbR6vXlbX2wVH9iODF4dIk7QLLSO0f5dWvmb3c1Ucrwv+FttG0MdKRMH55MkFWwE+WRM8TJDxFOEgUeQv28M3N5d4T618lsHz/rsxsVA6kTSf2Y2Bj0kq0SPieLuu9uF/PxKcbJEReJS1amL6fz4kuCdWfxTFlUauzcH4jdLRclxUTfQpN0xtKYrpPi0UabFIZBw72S0ckUsbry+9B32r53ezsXJaZHnjPIZZNTJJhF4gR0GywH+HjiGJfd/BBeg6Hig5en7Hzh+smTGomXGcRLpOZhAEMgQMxCbWwFPeiWHNFcuJdsAvaElE6rchZEEUyyAynBl5OyLW3p+M+wsQG1icMZImHrfYMBrjhmTP0QS5PJQ0Y8k7wsnPUyzFfYm9hOcvXm/b0t6vEJ3lUkkUyjsKCVvg30EQpPnWXBpusXJKcuRyJxc5vFOdRutdOMcvXv0UgWpOwDIr40g4JKfeX0dCyIq4yo3MIHFlHuF1fgNkWkzSF6S34zHckraW9/1neg/7sE/dns8GGGRpITeQPTtUmSzRPpHb0y74z8xdWBg9oO3ZL4sHSx2oY4v2k/PQyS1ks1hCt1+NxNOfXGZMNCFFcosFcn8bcFDJswg== 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)(1800799024)(36860700016)(23010399003)(5023799004)(11063799006)(261009223027099003)(18002099003)(56012099006)(10067099003)(261026121527099003)(261102123227099003)(261026140227099003)(261021121427099003)(261022105027099003)(261021123027099003)(261028144727099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: M3hzcsYSKSUkox4/vG/5HL36k/Q0p17uC7u3DmRbrZ4BRh34uLWAZaoqZ/PU7rYCc/CPVMr7v8EuaEGnKEd+NpxiCn0b1F/c4eD16MZhSgIJjf1Hv6u5/iiYGtXSqML7/XkJmFKqFZl66vRVB9vCJ5TdnYLZKb/v6Kc1Wx1Wd9FPgbB3IvKCGG7ZRCmLT+x80F6vY9yEPxd9q34DjCAKLNVua7NqXlFh2tNs6FvWNRvdiKNESeZjsFiQLuI5L8bDh6EuOnco/eQpsf++7Z1U+iOZsLS6MF1W3vSM8GwGsv/aI6A3iYUFspU0V+ZGopUS3/OBn5tLgGGlO4MW5l/wyn16n9wzTzQIncKqnxzPBB/rQkqaTxfyrcU6bI4ev/O0AP2jLMDf+erpk9NkSkKEgdGDVi235O+rDmQEWKmYqgXO6gSQmxa39aTTLxJHMd9d X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2026 20:55:06.0013 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de9de4a5-783f-47ff-8cc3-08df517fb5b3 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: SJ5PEPF000001CC.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8424 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 After execve() by the group leader, an open /dev/kfd fd stays valid, but the MMU notifier release has already destroyed the process queue manager and set p->mm to NULL. kfd_ioctl() only checks the group leader, so AMDKFD_IOC_CREATE_QUEUE reaches find_available_queue_slot() with a NULL queue_slot_bitmap and oopses with p->mutex held. The mutex is never released, and the release worker can block on it. Refuse ioctls and mmap on a process whose mm has been released. Take p->mutex in the release path around the queue teardown and the p->mm = NULL store. CREATE_QUEUE re-checks p->mm under the mutex, so a queue cannot be added after the teardown. pqm_create_queue() also returns -ENODEV when the slot bitmap is gone, which covers both the normal slot allocation and the CRIU restore path. Signed-off-by: Yongqiang Sun --- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 26 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 13 +++++++++- .../amd/amdkfd/kfd_process_queue_manager.c | 7 +++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c index 24a243f6b6f47..4e43435c1aa1f 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c @@ -401,6 +401,17 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, mutex_lock(&p->mutex); + /* + * The MMU notifier release destroys the queue manager and clears p->mm + * under p->mutex. The p->mm check in kfd_ioctl() is done without the + * lock, so the release may have run since then. Check again with the + * lock held and refuse to create a queue on a released process. + */ + if (!p->mm) { + err = -ENODEV; + goto err_pdd; + } + pdd = kfd_process_device_data_by_id(p, args->gpu_id); if (!pdd) { pr_debug("Could not find gpu id 0x%x\n", args->gpu_id); @@ -3740,6 +3751,17 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) goto err_i1; } + /* + * The address space of this process has been torn down (MMU notifier + * release) and its queue manager has been destroyed. The fd can stay + * open across execve() of the group leader, so refuse the ioctl. + */ + if (!READ_ONCE(process->mm)) { + dev_dbg(kfd_device, "KFD process address space already released\n"); + retcode = -ENODEV; + goto err_i1; + } + /* Do not trust userspace, use our own definition */ func = ioctl->func; @@ -3860,6 +3882,10 @@ static int kfd_mmap(struct file *filep, struct vm_area_struct *vma) if (process->lead_thread != current->group_leader) return -EBADF; + /* Same as kfd_ioctl(): the context is dead once its mm is released */ + if (!READ_ONCE(process->mm)) + return -ENODEV; + mmap_offset = vma->vm_pgoff << PAGE_SHIFT; gpu_id = KFD_MMAP_GET_GPU_ID(mmap_offset); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 5dc657f32d38c..0a76ae0768074 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -1500,6 +1500,15 @@ void kfd_process_notifier_release_internal(struct kfd_process *p) srcu_read_unlock(&kfd_processes_srcu, idx); } + /* + * Hold p->mutex so that an ioctl on a file that survived execve() of + * the group leader (e.g. CREATE_QUEUE) either completes before the + * queues are torn down or sees p->mm == NULL afterwards. Otherwise it + * could use the queue manager that pqm_uninit() destroys, or add a + * queue that nothing will ever remove. + */ + mutex_lock(&p->mutex); + /* * Dequeue and destroy user queues, it is not safe for GPU to access * system memory after mmu release notifier callback returns because @@ -1517,7 +1526,9 @@ void kfd_process_notifier_release_internal(struct kfd_process *p) } /* Indicate to other users that MM is no longer valid */ - p->mm = NULL; + WRITE_ONCE(p->mm, NULL); + + mutex_unlock(&p->mutex); if (p->context_id == KFD_CONTEXT_ID_PRIMARY) mmu_notifier_put(&p->mmu_notifier); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c index 6a9d7d31c8d43..1585c84f2774d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c @@ -378,6 +378,13 @@ int pqm_create_queue(struct process_queue_manager *pqm, if (pdd->qpd.queue_count >= max_queues) return -ENOSPC; + /* + * pqm_uninit() frees the queue slot bitmap when the process address + * space is released. Do not touch the bitmap after that. + */ + if (!pqm->queue_slot_bitmap) + return -ENODEV; + if (q_data) { retval = assign_queue_slot_by_qid(pqm, q_data->q_id); *qid = q_data->q_id; -- 2.43.0