From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC8EA3F0767 for ; Wed, 20 May 2026 15:17:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779290273; cv=none; b=BRkNx/BJ0OseFCZZp9UzNGbHv6dUEwk4/pwfPkTUYyv4+LD1Y98vrYHQUFO8wAmWSSgC519q/cofVwej5fOEbq964UeLIZDT3vCRCSSeY0BpFrSgBfPLV6lJkX/OIsgm/h3e3HdoXZVXhU98xwPjLiXPDGIuW6z98Ons+qvdBDE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779290273; c=relaxed/simple; bh=EFeEJkQiEqspN2K1pxO/W1A+nKY7wi9XdYP/GPgLqCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QalsUdkEzoCUrhqBTYTuCuIvxzbKKy9E7FlTzm7pAwlEt/DrscoC4e8xuiNFTNw7q0PfADjESxT/ydLeh8bHuYxfK3ILK6/rg1tPwWW1aUyqbAdVRO3qW8KFVrpTY/6p5/Kq8strJYTXgSiNxJeWQ0+YJfSKI7JLt+2dPHDzDzI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UwZfEvNn; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UwZfEvNn" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-5a877510541so5971503e87.2 for ; Wed, 20 May 2026 08:17:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779290270; x=1779895070; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fyVPZSX7MwDddJkv/HM4B88jEPRd96zpfMtgKanmjaM=; b=UwZfEvNnrk71SErlbZYM0Z8OAgOvv9x5KsvbRBz9WIn2H8ODK0/PNId9vX+AuXpV3t +eLfPMMNLvQZRC03RSgVu8O+530mFVJ4ksizXHHz/SUtzLvPKm0M95RtuiD0U0rlvcFM 7eXi+kq/OF8znjlgZvQdmOkfuUSyi2FSOAWamQRGjqtsaV1nt3d51b/yLbZfSaGy+vvm jqtJCSQoJfL8ISaUkeKuWHocJJhQrutJ3OBW71BuWYnHwvV3wHqQud1ac85/AdL4ig73 UgPshFH1vfF0GIqz2Jqx3NHUAoXjtezhkT1dlJ0TwvI0DhVTNUAhk1F9vYY9kowQvpeq 9tmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779290270; x=1779895070; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fyVPZSX7MwDddJkv/HM4B88jEPRd96zpfMtgKanmjaM=; b=KVs124HV7x4VRUknrUDnOUp4r5nU/yxKzhbkb0atK4QdLneMuM4jPkioXYbWXV6t+m 3bQttABwr8ZEXCdnNVeBVqWlRh1vCOS4tLeSWHXD6T3J71mezu7DpIQys2uq9c6Dopv7 HyybZV/iWpQZ/JCtun0Ib0zsa4F/BLyBOGCi6JImocTX7RrXol0iZxEEJbbYzwErq2YM LqqxSkn9s1TGdVpxIqxdhbFdN/uF4xC5+3v1SmZ3qIXPHb+PF7xEXS9TRM7LTBJExb6B eUw6qfzBZ6gLm2Zc+Gfled7zqBZDl45lUd/qDu2DVhTr+LHfrK+eTUPRBLtukNboUrhJ ud6A== X-Forwarded-Encrypted: i=1; AFNElJ+F3vMqacixo4PLB368bW2/i1U1uFwATRwo1KbFEiRI6YylCPGFOlnRKsljHZ2TkDfQyCHJXY/LeeFL5KU=@vger.kernel.org X-Gm-Message-State: AOJu0YxHoN6Wc4eCa4K5zVfZzBJClcb3EwzttSARI+PGJOxAYzjfFAnK 0fW7sCF12X6IjzMv01Y43aRkmRnLydnQsjV6gyU8IeAOL8nY3Be9UbTd X-Gm-Gg: Acq92OGQwthsFXHeQmEn1mfDnx1WjTE6xXKitAkS2e+alSQ5XK8isPqkZlgfmD4fyxv 4KPSfUgcAA7bSpBnlAF4zQguVq1ipdBGmOchEjOyC338qHhQMrlIgqUG/PGB1LTPFxjq7eX64UZ vn688VR8PCeY2Ei4+9/AX5PWQkd2WMux34bplaGqVU2tSmzMLIhxVxUGqTecFhGJyc47kwBevWH 4ySWFsDz67/5F/a6X62opd69lXq6arZLEakMLOZQNKte6poLlk0TsPfVjIudb/Y14n+79EKOG9p FrCr4WJnwsryL5GYTCM4MTZdJKOKgo+COZWesyUJtRr+eLESvaGlxpEgF27wynP0J4/GqXgl6kD M0P1bk6pzW8A1BDtdwszJP+dfj29cPGkQvI4qvJ9LOdepFE3n+69PCMd9ju+NrbpUSIZHiL7jRx NDbaPOIiu9bGGu7/w3jWbNfP551RoXFG0c X-Received: by 2002:a05:6512:1053:b0:5a3:ffed:8440 with SMTP id 2adb3069b0e04-5aa0e73d549mr8445759e87.12.1779290269709; Wed, 20 May 2026 08:17:49 -0700 (PDT) Received: from localhost ([188.234.148.119]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5a90f10c8b2sm5022470e87.17.2026.05.20.08.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 08:17:49 -0700 (PDT) From: Mikhail Gavrilov To: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Cc: Alex Deucher , =?UTF-8?q?Christian=20K=C3=B6nig?= , David Airlie , Simona Vetter , Pierre-Eric Pelloux-Prayer , Sumit Semwal , linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, Mikhail Gavrilov Subject: [PATCH v3 1/2] drm/amdgpu: convert amdgpu_vm_lock_by_pasid() to drm_exec Date: Wed, 20 May 2026 20:17:39 +0500 Message-ID: <20260520151741.50575-2-mikhail.v.gavrilov@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260520151741.50575-1-mikhail.v.gavrilov@gmail.com> References: <20260429143743.50743-1-mikhail.v.gavrilov@gmail.com> <20260520151741.50575-1-mikhail.v.gavrilov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit amdgpu_vm_lock_by_pasid() looks up a VM by PASID and reserves its root PD with a bare amdgpu_bo_reserve(), returning the still-reserved root to the caller. A caller that then needs to reserve further BOs (for example the devcoredump IB dump) ends up nesting reservation_ww_class_mutex acquires without a ww_acquire_ctx, which lockdep flags as recursive locking. Convert the helper to take a drm_exec context and lock the root PD via amdgpu_vm_lock_pd() instead. Callers now run it inside a drm_exec_until_all_locked() loop and can lock additional BOs in the same ww ticket, so there is no nested ww_mutex acquire. The only existing caller, amdgpu_vm_handle_fault(), is updated accordingly. Its is_compute_context path, which previously dropped the root reservation around svm_range_restore_pages() and re-took it, now finalises the drm_exec context and re-initialises a fresh one; behaviour is otherwise unchanged. No functional change intended for the page-fault path. Signed-off-by: Mikhail Gavrilov --- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 72 ++++++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 3 +- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c index 9ba9de16a27a..3a22670b733f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -2950,14 +2950,22 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) } /** - * amdgpu_vm_lock_by_pasid - return an amdgpu_vm and its root bo from a pasid, if possible. + * amdgpu_vm_lock_by_pasid - look up a VM by PASID and lock its root PD * @adev: amdgpu device pointer - * @root: root BO of the VM + * @root: out: reference to the VM's root BO, dropped by the caller * @pasid: PASID of the VM - * The caller needs to unreserve and unref the root bo on success. + * @exec: drm_exec context to lock the root PD in + * + * Must be called from within a drm_exec_until_all_locked() loop; the caller + * runs drm_exec_retry_on_contention() afterwards and drops the *root + * reference once the drm_exec context is finalised. + * + * Return: the VM on success, or NULL if the PASID has no VM, the VM is being + * torn down, or locking the root PD failed. */ struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev, - struct amdgpu_bo **root, u32 pasid) + struct amdgpu_bo **root, u32 pasid, + struct drm_exec *exec) { unsigned long irqflags; struct amdgpu_vm *vm; @@ -2971,9 +2979,11 @@ struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev, if (!*root) return NULL; - r = amdgpu_bo_reserve(*root, true); - if (r) - goto error_unref; + r = amdgpu_vm_lock_pd(vm, exec, 0); + if (r) { + amdgpu_bo_unref(root); + return NULL; + } /* Double check that the VM still exists */ xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); @@ -2981,16 +2991,12 @@ struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev, if (vm && vm->root.bo != *root) vm = NULL; xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); - if (!vm) - goto error_unlock; + if (!vm) { + amdgpu_bo_unref(root); + return NULL; + } return vm; -error_unlock: - amdgpu_bo_unreserve(*root); - -error_unref: - amdgpu_bo_unref(root); - return NULL; } /** @@ -3013,20 +3019,32 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, { bool is_compute_context = false; struct amdgpu_bo *root; + struct drm_exec exec; uint64_t value, flags; struct amdgpu_vm *vm; int r; - vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid); - if (!vm) + drm_exec_init(&exec, 0, 0); + drm_exec_until_all_locked(&exec) { + vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid, &exec); + drm_exec_retry_on_contention(&exec); + if (!vm) + break; + } + if (!vm) { + drm_exec_fini(&exec); return false; + } is_compute_context = vm->is_compute_context; if (is_compute_context) { - /* Unreserve root since svm_range_restore_pages might try to reserve it. */ - /* TODO: rework svm_range_restore_pages so that this isn't necessary. */ - amdgpu_bo_unreserve(root); + /* Release the root PD lock since svm_range_restore_pages + * might try to take it. + * TODO: rework svm_range_restore_pages so that this isn't + * necessary. + */ + drm_exec_fini(&exec); if (!svm_range_restore_pages(adev, pasid, vmid, node_id, addr >> PAGE_SHIFT, ts, write_fault)) { @@ -3036,9 +3054,17 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, amdgpu_bo_unref(&root); /* Re-acquire the VM lock, could be that the VM was freed in between. */ - vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid); - if (!vm) + drm_exec_init(&exec, 0, 0); + drm_exec_until_all_locked(&exec) { + vm = amdgpu_vm_lock_by_pasid(adev, &root, pasid, &exec); + drm_exec_retry_on_contention(&exec); + if (!vm) + break; + } + if (!vm) { + drm_exec_fini(&exec); return false; + } } addr /= AMDGPU_GPU_PAGE_SIZE; @@ -3076,7 +3102,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, r = amdgpu_vm_update_pdes(adev, vm, true); error_unlock: - amdgpu_bo_unreserve(root); + drm_exec_fini(&exec); if (r < 0) dev_err(adev->dev, "Can't handle page fault (%d)\n", r); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h index d083d7aab75c..af292c2fc521 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h @@ -593,7 +593,8 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, bool write_fault); struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev, - struct amdgpu_bo **root, u32 pasid); + struct amdgpu_bo **root, u32 pasid, + struct drm_exec *exec); void amdgpu_vm_set_task_info(struct amdgpu_vm *vm); -- 2.54.0