Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp175190lqo; Tue, 7 May 2024 16:43:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXv1XeZDTao7B1kJFeRFhEo/w4atgD71JeRo5CgEzU7hbDcMZBlMOwuWSsuANwTSVoIwC1pX+6HvgwggvJhILXrKTQTq+e/lO2SxRpEwg== X-Google-Smtp-Source: AGHT+IGlhDTvK+uR2lzl3g2uAR4DKVL6q5sfbySfAGCygBo0CJ5uYm9MuAH22sFftNGwIvNuvGJS X-Received: by 2002:a50:a6d5:0:b0:572:6849:da28 with SMTP id 4fb4d7f45d1cf-5731d9a831amr768252a12.4.1715125393596; Tue, 07 May 2024 16:43:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715125393; cv=pass; d=google.com; s=arc-20160816; b=W5XNvB4B9Rfh3fBT5PG2nrMUIdXRuSFdLGl8b1ZKgb93MkcLYP5P3UxEDWA1iA2yfw srBfCtnAAibV0Di0RHqk5StFIf5iiPkTjZMeQVgMfHURrPT1LX1c4+ZJVhmSq4HfH8w4 luveBf9DETvyd/iOmwOB6B/AB+Y/idP5kONkd70m0GXRtseGZZxnzkPQlciojvVfcX9n iXy0qozwT2u8E9pKgNZYrOJ5DU5uNmosp9thhPVXLIbQmrNbe+Y3+VvfW5jitPR+qZZN Z15GncvN6lDdVltTPI4n2oeLKBARbUxgWYDZd2BasKtcLGbN0S/6avshFROIcM40gQyk 0VEw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=oirzSwfmd5eiZj9tlnkFLcWNMf82EV0i/0Zontj8k5A=; fh=gifZI+4anRPXT7OqW6MB/CpWs6Nfx+EfObiCjHGo11g=; b=onHK972tE37+dk5n5WoVEJQKLqo+0cS3SliJyxzUj/HJ2FrIjbI2dFuAPCU56iYnVs MeY1YGd2KCuRLhusU8a4rx6BEOjD1UrvogiymhHqOeoU9sOhatysjPqQp39d69nerh3u KCWR/VVIJS4c8hvmj1uMkEnNYpZTiU6mV3PbLNjR7KvaLEavCs/9+Djwob1A0n0FQeN8 6pdefEj2hWi58xNvcmI1+t3IGn2xzqmUPe4/F4E2eUWRbDuAxhfTcfE/jYcXEKVG7W5e KSX9WjY7S1bab4DH97xd0g1z2TdXN7a6xL3nCSEceKAqOL8xhEnXlyG1T892mE5VL2Pu H3xQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WHiKDmEy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172430-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172430-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ee11-20020a056402290b00b00570241f7504si6036001edb.670.2024.05.07.16.43.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 16:43:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172430-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WHiKDmEy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172430-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172430-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0D60C1F260A5 for ; Tue, 7 May 2024 23:42:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB75A17088C; Tue, 7 May 2024 23:11:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WHiKDmEy" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D056B17085B; Tue, 7 May 2024 23:11:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715123516; cv=none; b=T2X+pa9b/cp5Af5nWVvelB9JqPSKWSu9zOnFhQrQkpJ0/S4z27URtBE+x1c/DOVPckUmqoI5vy7YIPRZjvQG0aQnNmOIlwt9r0ZfPJnE4BxCa5XHAvz+MmgBBlp3MWDmLcmYQj7nfwO6pKdOwDGf0xo289ZYqxQgXbUXQR2y+YU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715123516; c=relaxed/simple; bh=qjiSmLpHA1Dv+SZcM5vbpRQi0La7wupBj5UpNueurXE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j2ePJk7c+rb7gRsAYtILpdo4KCo3qU11PKGT5d5tm0G6UkF4hqOrnYoXLvi5a2qrofk5CXF0+QQC+ceQ4wtGnw99q1Z+NAPrb7Y2eXgX0UHtbGwFVUcchUS5/fKMPrWAIbOMP3oxXqalWlbNNZLBhqU/P8Clnrlbmp1buQfV1ng= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WHiKDmEy; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F21B8C2BBFC; Tue, 7 May 2024 23:11:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715123516; bh=qjiSmLpHA1Dv+SZcM5vbpRQi0La7wupBj5UpNueurXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WHiKDmEytbjMEs3stArdo8KjgpnnKxV0dmRr7VlG157pZj6dO4rvsGaR0GfztQVnQ y/7njJWhuIF4sDW22wIlMRTMYisW7yqrjBXxrmjytsDUAwME0uNBEEjN6IuDn7pEn4 YyUZe0S90EdAmipBB1AjOXgHZlFJ9Rvck8KLNPhtA9FWJ7XvsypPWyZvAQgQrefu1r EfLeydOxaC/d+HKGUNJ+S0+D5l8w2PwgMyZY0vwtqQlNjEVRvpgkUU7VScrRjgsCpk qK2OFiTHIr/5W7tWtxrOixpASVATVygFGIepMhSmURUeoWFrHUvbGhkH4PFDZJC8S0 0GSizEguVmThg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Lancelot SIX , Felix Kuehling , Alex Deucher , Sasha Levin , Felix.Kuehling@amd.com, christian.koenig@amd.com, Xinhui.Pan@amd.com, airlied@gmail.com, daniel@ffwll.ch, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH AUTOSEL 6.6 35/43] drm/amdkfd: Flush the process wq before creating a kfd_process Date: Tue, 7 May 2024 19:09:56 -0400 Message-ID: <20240507231033.393285-35-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240507231033.393285-1-sashal@kernel.org> References: <20240507231033.393285-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.30 Content-Transfer-Encoding: 8bit From: Lancelot SIX [ Upstream commit f5b9053398e70a0c10aa9cb4dd5910ab6bc457c5 ] There is a race condition when re-creating a kfd_process for a process. This has been observed when a process under the debugger executes exec(3). In this scenario: - The process executes exec. - This will eventually release the process's mm, which will cause the kfd_process object associated with the process to be freed (kfd_process_free_notifier decrements the reference count to the kfd_process to 0). This causes kfd_process_ref_release to enqueue kfd_process_wq_release to the kfd_process_wq. - The debugger receives the PTRACE_EVENT_EXEC notification, and tries to re-enable AMDGPU traps (KFD_IOC_DBG_TRAP_ENABLE). - When handling this request, KFD tries to re-create a kfd_process. This eventually calls kfd_create_process and kobject_init_and_add. At this point the call to kobject_init_and_add can fail because the old kfd_process.kobj has not been freed yet by kfd_process_wq_release. This patch proposes to avoid this race by making sure to drain kfd_process_wq before creating a new kfd_process object. This way, we know that any cleanup task is done executing when we reach kobject_init_and_add. Signed-off-by: Lancelot SIX Reviewed-by: Felix Kuehling Signed-off-by: Alex Deucher Signed-off-by: Sasha Levin --- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 7a1a574106fac..d98e45aec76b4 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -828,6 +828,14 @@ struct kfd_process *kfd_create_process(struct task_struct *thread) if (process) { pr_debug("Process already found\n"); } else { + /* If the process just called exec(3), it is possible that the + * cleanup of the kfd_process (following the release of the mm + * of the old process image) is still in the cleanup work queue. + * Make sure to drain any job before trying to recreate any + * resource for this process. + */ + flush_workqueue(kfd_process_wq); + process = create_process(thread); if (IS_ERR(process)) goto out; -- 2.43.0