Received: by 2002:a05:7412:9c07:b0:fa:6e18:a558 with SMTP id lr7csp780492rdb; Sun, 28 Jan 2024 08:37:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHODbYTZ+JM5o59YDex5s6NGGXxhRlo1z07AgstVw2NCa+iQDpttexX3aqgDXi4aubal3bz X-Received: by 2002:a17:90a:ee8d:b0:28b:de50:b4a7 with SMTP id i13-20020a17090aee8d00b0028bde50b4a7mr987169pjz.76.1706459841864; Sun, 28 Jan 2024 08:37:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706459841; cv=pass; d=google.com; s=arc-20160816; b=hU/pNoio15ebX8KVR7GKjeMNjyjXl7K9uESx8DKniLAYSnclZopnLee0Dlokd+5hUo NrUnxA5FqduR+lfEQRDAVan0bNp121ZiUUFFp5huXXDx4a0v36tlaIlInuOqF8ULg49J SNbVDKonsLJN8MIy/EPVWvvMRSl+X7O1HvkD6nJFJJriY9BULQB2iiLuuGiLDPtZwjZa Izt1PmEM9GREx/WegkiphXHwNfN7qMuxPFLYzLcva+iYjIqMIHKFHHptog4l8nSGa99P ccSRLUII1NqtTJx+k2GLg7bi3S1yAmhD1GtDUu2ctN3zr4OM7FyB45auKoZy3yAuMnK1 GKXQ== 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=xo4KJWR+ztcgr3kl5tzKBjdgLLo7++QvthCW6GI1y3U=; fh=iHNnPM6UMHcDvykAThiB+hz13FLj5GX5rR/Y/iv31ZY=; b=aAv9QHJSjZcpXu9C7BiFEwNCY1mQlnio9YW7syWmRJL/X9CsWyLGFtfanq5DEOEamr FxqIbBiH6yOsytPGE4mXhUaEU0FBbHmM8IQIFnySU8s1mZMnU92a0SlZqKhtp2Ymly36 i62fl+tskYq9r2zgMyUbRuSgDZISCoQCLcVAVM8FunjdJ8ztyzAIuu2bof6GpmXkePfd F87MrX+RTmQmAfb0NnX7v+LWvmrmxey8uELLcGwEvyA+BDEkYlO3hjFc25dMN2ejSmS1 J8AK/hSvYlukuPDt+pv3hxgZBpVPcWTz79kxTPf9dpCE9fiIPsBQx1RFKnWWwogxIBXJ 8sVg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jlLpsnCt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41771-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id t2-20020a1709028c8200b001d7137acad4si4281308plo.273.2024.01.28.08.37.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 08:37:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jlLpsnCt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41771-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 1DB8DB22E2F for ; Sun, 28 Jan 2024 16:35:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8963C7E771; Sun, 28 Jan 2024 16:14:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jlLpsnCt" 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 9D7B77CF36; Sun, 28 Jan 2024 16:14:53 +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=1706458493; cv=none; b=b9thcVqsOqlP5GSMlFOZBbK30kaynmiNTe5AbThgHt9gkhgK0X57KH+R/4DwDU/ila4aqtJPOuQUK0U+FYIqP9IH0F5isyicNPGHl/oWYBwDEScoGuzXergJ/q+pkDBNZItVxMFjJ7xnVN2UvlbJz2EACL3o3Iu+cMbOX0v2id0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706458493; c=relaxed/simple; bh=5THwZ6FBhe9LEbM/WbmWAGgr1x9Bl1CyRDURkM2vJ0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oZbwyivjguS+WvLbsi7Q1B0BECnt/6p6rdt/2K1QBdekussHupNI8bcjTVTPSgTAHRgAOPMpwR4HGwACalMUaXiju0rano/jYu/N6KCnFc4I4g/HCwK78IKWEViYUUCfWaYeQhWhTJkkM8rWbveAApyaZSysr0Sh1IN9mrobuz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jlLpsnCt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D24F3C433F1; Sun, 28 Jan 2024 16:14:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706458493; bh=5THwZ6FBhe9LEbM/WbmWAGgr1x9Bl1CyRDURkM2vJ0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jlLpsnCtcI9iNDhw/D44YP9NvqB8LY7HXcE/QmJTbztVk8WIYCzeam32bp9L2Z5j5 IVIDglUQ5rrh40bqtE7crZFMblesuf69wf2CVbWWmVXkRXdh+xXayN0Pm0L3nhQkFk LebFaVI+1KteqvzhsO7avCvBSPq3gMbaFFMH/vbGLNe3/7FO1wukU2lOd9PtHdakxE kwk0MwmwSmnpUQAE/fOChueUouOaj2fxk0CzsCMJGYNSHxpl+Yvh9olZ+DVodwiZOc Rhk2JhhPSM9oqw3SFxxqvXKQtXC/Asxtt3NC2gcDIrMJ2pXG60XXX3X1YL7AzCdB6h GlCKZ2HLD5ouw== 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.1 16/27] drm/amdkfd: Fix lock dependency warning Date: Sun, 28 Jan 2024 11:14:01 -0500 Message-ID: <20240128161424.203600-16-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128161424.203600-1-sashal@kernel.org> References: <20240128161424.203600-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.1.75 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 208812512d8a..4ecc4be1a910 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -380,14 +380,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); @@ -3310,13 +3305,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); } @@ -3331,8 +3327,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