Received: by 2002:a05:7412:9c07:b0:fa:6e18:a558 with SMTP id lr7csp777539rdb; Sun, 28 Jan 2024 08:29:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnjFmppy0+UP1kp3HeOYEcO8B0yxL1UqKHOPPVmAg5pRgTA7F7wJpIjjJL+idVqmpBVQMv X-Received: by 2002:aa7:db50:0:b0:55e:ea14:7912 with SMTP id n16-20020aa7db50000000b0055eea147912mr1396358edt.0.1706459346898; Sun, 28 Jan 2024 08:29:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706459346; cv=pass; d=google.com; s=arc-20160816; b=giAAshtLBqBlkp7GOF+w0XedDgj6O0RRlSxyPsmcqwmsGiFZy+u+Ne5qTjLP5yOPGi eCcp+DTdIH9EM1LQUgbmMjdpmZPG5IBMj61antMdNJzoHTBgpt5thxSZqpVwKz2sbLfD cdrGVmZ+DbJajDVFlguTwP6FVwdwHXo+mXDbiBIXlT11H4k/VAQu1fJc078/z++gLWY/ oSLBFUsuARJTuAwOc3A+5pIUBaXkGpmUGml3rQ7SiNH21BVfZDtimYkzLE4zmSEJvg9H nQiRzsZjonkZhoqvfQ9DiNGwibVvoZsivURjrG6I41g5njLi4D+5moffln2mMpZ5xDDD M6LQ== 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=IIbw0V42DEhLdWJHg2BF17WL3I120mYbRn9nEgrsf+o=; fh=iHNnPM6UMHcDvykAThiB+hz13FLj5GX5rR/Y/iv31ZY=; b=Mc6gqWqzUuCSYpL8UyAkI5bX8HOeDI+K/X4mSEgnsdV7wP4JVM0InQXg8FQUg69QwR xqgWD7oh4XgNlVgawC3zuDR2gAVxccVa2h94XZELyNtH7eoH65Cwvv22zl+/6790K1KY HqVnXqXtZqzrXGP8vUsY9prbBJcLiZ9KbqMIOFIMK1iTXb88Q+IbKBgGCdKpUU9p4rG7 tAhxNqiTR26GkIxSu2JoDhsIDlmRrcTA2QaRDJHBuEE+ROpi/aM143fKeDmyXMytWJYi rrhwgj7goucbXw4Uv7xQM5kG6yX6+GiTfNwATPw9sVTEqXYXctTe6GzCnTjGveUsfr7u N72g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=heSEq6tq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41740-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. [147.75.80.249]) by mx.google.com with ESMTPS id n9-20020a056402434900b0055ed8147b00si985813edc.394.2024.01.28.08.29.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 08:29:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=heSEq6tq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41740-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 844B11F25909 for ; Sun, 28 Jan 2024 16:27:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B5C5963513; Sun, 28 Jan 2024 16:13:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="heSEq6tq" 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 4D717633E8; Sun, 28 Jan 2024 16:13:48 +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=1706458428; cv=none; b=F4dtdlVEp+TvjA43kie1vPlDlz0RrP4pXOOIE6wHwVZfuEwjWVYhuMn+Dgor8akIzs5UDd+IfRMRe9tPyl//6RhatINIxriJ2aPMxtcbn8aM+83AXgMA9afP7gvGOZIL5MvvZWuzQoHiluOUFTmm5hkgg/t5/dcuEa4P72OKVtk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706458428; c=relaxed/simple; bh=i/X8sJUEdMa6mxFYT3RNkS2xTbTEwS3rLCNeAa6jM5Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T8q65/XZDYnwcsHeOFyGmhK8vU2bPT3SdjWOzZYft2fKQkVQ7WoHJYLAn+3cEzABAmcajzrP/QAuOieqpivnx7owyLPo5hPyWJsrcHvqdmGok8BzQKkJ+WOh6x8PyCFEI+HGIXuTyulWolHI6Nt/gnORYS1tGSLK8PsA4BkfaJs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=heSEq6tq; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5CD6C433C7; Sun, 28 Jan 2024 16:13:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706458428; bh=i/X8sJUEdMa6mxFYT3RNkS2xTbTEwS3rLCNeAa6jM5Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=heSEq6tqMoaLt2BYdJRb2HbpgtB3yRWsW5CSDkt57CUajOCm1HHwxsdgD5B/3yjKF 8eGnjqJ/kJQomftA0cY6z55e0QCPuzRZf9Yr9bnBrPtkV5FjOOobybt8kjNpPcULPD nAhDFBgzmXBBpJ+Woav/9nmADRZltFHStEMMli0ayp3lsWRDaEN+//efCqrNKPZPDF rl8PZroIUWe7pMu09iBrhzIpQImwiM8arWJy8sQAFAVMOzZL3/lc9dAnCAlqxr+rLb eLmR/AL4uO/znBBOjE20TCegv0+aDf4yOFDgSjD/5+gK1asmvZ0X1/+7oGAroWU7W9 jofhqwdOp7ErQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Felix Kuehling , Philip Yang , 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 19/31] drm/amdkfd: Fix lock dependency warning Date: Sun, 28 Jan 2024 11:12:49 -0500 Message-ID: <20240128161315.201999-19-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128161315.201999-1-sashal@kernel.org> References: <20240128161315.201999-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.14 Content-Transfer-Encoding: 8bit From: Felix Kuehling [ Upstream commit 47bf0f83fc86df1bf42b385a91aadb910137c5c9 ] ====================================================== WARNING: possible circular locking dependency detected 6.5.0-kfd-fkuehlin #276 Not tainted ------------------------------------------------------ kworker/8:2/2676 is trying to acquire lock: ffff9435aae95c88 ((work_completion)(&svm_bo->eviction_work)){+.+.}-{0:0}, at: __flush_work+0x52/0x550 but task is already holding lock: ffff9435cd8e1720 (&svms->lock){+.+.}-{3:3}, at: svm_range_deferred_list_work+0xe8/0x340 [amdgpu] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #2 (&svms->lock){+.+.}-{3:3}: __mutex_lock+0x97/0xd30 kfd_ioctl_alloc_memory_of_gpu+0x6d/0x3c0 [amdgpu] kfd_ioctl+0x1b2/0x5d0 [amdgpu] __x64_sys_ioctl+0x86/0xc0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd -> #1 (&mm->mmap_lock){++++}-{3:3}: down_read+0x42/0x160 svm_range_evict_svm_bo_worker+0x8b/0x340 [amdgpu] process_one_work+0x27a/0x540 worker_thread+0x53/0x3e0 kthread+0xeb/0x120 ret_from_fork+0x31/0x50 ret_from_fork_asm+0x11/0x20 -> #0 ((work_completion)(&svm_bo->eviction_work)){+.+.}-{0:0}: __lock_acquire+0x1426/0x2200 lock_acquire+0xc1/0x2b0 __flush_work+0x80/0x550 __cancel_work_timer+0x109/0x190 svm_range_bo_release+0xdc/0x1c0 [amdgpu] svm_range_free+0x175/0x180 [amdgpu] svm_range_deferred_list_work+0x15d/0x340 [amdgpu] process_one_work+0x27a/0x540 worker_thread+0x53/0x3e0 kthread+0xeb/0x120 ret_from_fork+0x31/0x50 ret_from_fork_asm+0x11/0x20 other info that might help us debug this: Chain exists of: (work_completion)(&svm_bo->eviction_work) --> &mm->mmap_lock --> &svms->lock Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&svms->lock); lock(&mm->mmap_lock); lock(&svms->lock); lock((work_completion)(&svm_bo->eviction_work)); I believe this cannot really lead to a deadlock in practice, because svm_range_evict_svm_bo_worker only takes the mmap_read_lock if the BO refcount is non-0. That means it's impossible that svm_range_bo_release is running concurrently. However, there is no good way to annotate this. To avoid the problem, take a BO reference in svm_range_schedule_evict_svm_bo instead of in the worker. That way it's impossible for a BO to get freed while eviction work is pending and the cancel_work_sync call in svm_range_bo_release can be eliminated. v2: Use svm_bo_ref_unless_zero and explained why that's safe. Also removed redundant checks that are already done in amdkfd_fence_enable_signaling. Signed-off-by: Felix Kuehling Reviewed-by: Philip Yang Signed-off-by: Alex Deucher Signed-off-by: Sasha Levin --- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c index 8e368e4659fd..a4c911fa1675 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -391,14 +391,9 @@ static void svm_range_bo_release(struct kref *kref) spin_lock(&svm_bo->list_lock); } spin_unlock(&svm_bo->list_lock); - if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base)) { - /* We're not in the eviction worker. - * Signal the fence and synchronize with any - * pending eviction work. - */ + if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base)) + /* We're not in the eviction worker. Signal the fence. */ dma_fence_signal(&svm_bo->eviction_fence->base); - cancel_work_sync(&svm_bo->eviction_work); - } dma_fence_put(&svm_bo->eviction_fence->base); amdgpu_bo_unref(&svm_bo->bo); kfree(svm_bo); @@ -3424,13 +3419,14 @@ svm_range_trigger_migration(struct mm_struct *mm, struct svm_range *prange, int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence) { - if (!fence) - return -EINVAL; - - if (dma_fence_is_signaled(&fence->base)) - return 0; - - if (fence->svm_bo) { + /* Dereferencing fence->svm_bo is safe here because the fence hasn't + * signaled yet and we're under the protection of the fence->lock. + * After the fence is signaled in svm_range_bo_release, we cannot get + * here any more. + * + * Reference is dropped in svm_range_evict_svm_bo_worker. + */ + if (svm_bo_ref_unless_zero(fence->svm_bo)) { WRITE_ONCE(fence->svm_bo->evicting, 1); schedule_work(&fence->svm_bo->eviction_work); } @@ -3445,8 +3441,6 @@ static void svm_range_evict_svm_bo_worker(struct work_struct *work) int r = 0; svm_bo = container_of(work, struct svm_range_bo, eviction_work); - if (!svm_bo_ref_unless_zero(svm_bo)) - return; /* svm_bo was freed while eviction was pending */ if (mmget_not_zero(svm_bo->eviction_fence->mm)) { mm = svm_bo->eviction_fence->mm; -- 2.43.0