All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Zhu" <James.Zhu@amd.com>, amd-gfx@lists.freedesktop.org
Subject: Re: [RFC PATCH 2/3] drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance
Date: Tue, 17 Nov 2026 17:21:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-219228@kernel.org> (raw)
In-Reply-To: <20261117165952.3558762-3-James.Zhu@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdgpu: physical xcc_mask used as logical index in
  amdgpu_vm_spm_update()
- [High] drm/amdgpu: amdgpu_vm_fini() only checks GFXHUB(0) before
  waiting on jobs
- [Medium] drm/amdgpu: unlocked allocation and unsafe rollback in
  amdgpu_vmid_alloc_spm_gfxhubs()
--

commit 0df4e49e9ba90dc0c43a497872e17c100a8d4894
Author: James Zhu <James.Zhu@amd.com>

drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance

This commit updates SPM VMID reservation, binding, and unbinding to operate
per GFXHUB instance across XCCs rather than only on AMDGPU_GFXHUB(0).

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> index 9c879ac683520..41f3dba530e38 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c

[ ... ]

> +int amdgpu_vmid_alloc_spm_gfxhubs(struct amdgpu_device *adev,
> +				  struct amdgpu_vm *vm, uint32_t xcc_mask)
> +{
> +	int i, r;
> +
> +	for_each_inst(i, xcc_mask) {
> +		r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(i));
> +		if (r) {
> +			amdgpu_vmid_free_spm_gfxhubs(adev, vm, xcc_mask);
> +			return r;
> +		}
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
If amdgpu_vmid_alloc_spm() fails on a later hub, can rolling back with
amdgpu_vmid_free_spm_gfxhubs(adev, vm, xcc_mask) unbind an active SPM VMID
to 0 while GPU jobs are still using it?

amdgpu_vmid_alloc_spm() returns 0 when vm->reserved_vmid[vmhub] is already
set, and AMDGPU_VM_OP_RESERVE_VMID calls amdgpu_vmid_alloc_spm_gfxhubs()
before reserving vm->root.bo.  If vm already held a reservation on an
earlier hub, or a concurrent job submission grabs a newly published
vm->reserved_vmid[AMDGPU_GFXHUB(i)] before a subsequent hub fails with
-ENOENT, the rollback frees and unbinds those earlier hubs to 0 without
waiting via dma_resv_wait_timeout().

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 94b1cfe05e3e0..e1691145da903 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -589,30 +589,36 @@ uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm)
>   * The SPM VMID is fixed (adev->vm_manager.spm_vmid), so its page-table-base
>   * register must track whichever VM currently holds the reservation instead
>   * of being set per-job like a normal VMID. Bind it to @vm's real root PD
> - * address if @vm holds the reservation; otherwise do nothing, since @vm is
> - * not the one that could legitimately change the current binding.
> + * address on every GFXHUB @vm holds the reservation on; hubs reserved by
> + * other VMs or not reserved at all are left alone, since @vm is not the
> + * one that could legitimately change their binding.
>   *
>   * @vm's root PD must be locked by the caller.
>   */
>  void amdgpu_vm_spm_update(struct amdgpu_device *adev, struct amdgpu_vm *vm)
>  {
> -	struct amdgpu_kq_vmid_mgr *id_mgr =
> -		&adev->vm_manager.id_mgr[AMDGPU_GFXHUB(0)];
>  	uint64_t pd_addr;
> +	int i;
>  
>  	if (!adev->vm_manager.spm_vmid || !adev->gfxhub.funcs)
>  		return;
>  
> -	mutex_lock(&id_mgr->lock);
> -	if (vm->reserved_vmid[AMDGPU_GFXHUB(0)] !=
> -	    &id_mgr->ids[adev->vm_manager.spm_vmid] || !amdgpu_vm_ready(vm)) {
> -		mutex_unlock(&id_mgr->lock);
> +	if (!amdgpu_vm_ready(vm))
>  		return;
> -	}
>  
>  	pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
> -	amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0), adev->vm_manager.spm_vmid, pd_addr);
> -	mutex_unlock(&id_mgr->lock);
> +	for_each_inst(i, adev->gfx.xcc_mask) {
> +		struct amdgpu_kq_vmid_mgr *id_mgr =
> +			&adev->vm_manager.id_mgr[AMDGPU_GFXHUB(i)];
> +
> +		mutex_lock(&id_mgr->lock);
> +		if (vm->reserved_vmid[AMDGPU_GFXHUB(i)] ==
> +		    &id_mgr->ids[adev->vm_manager.spm_vmid])
> +			amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(i),
> +					     adev->vm_manager.spm_vmid,
> +					     pd_addr);
> +		mutex_unlock(&id_mgr->lock);
> +	}
>  }

[Severity: High]
Can iterating over adev->gfx.xcc_mask here and in amdgpu_vm_ioctl() index
adev->reg_offset[GC_HWIP] out of bounds when lower-numbered physical XCCs
are harvested?

adev->gfx.xcc_mask is a bitmask of physical XCC instances, whereas
AMDGPU_GFXHUB(i), adev->vmhub[], ring->vm_hub, and setup_vm_pt_regs_xcc()
all use contiguous logical XCC indices 0 .. NUM_XCC(adev->gfx.xcc_mask) - 1
and translate them via GET_INST(GC, i):

drivers/gpu/drm/amd/amdgpu/gfxhub_v1_2.c:gfxhub_v1_2_xcc_setup_vm_pt_regs() {
	for_each_inst(i, xcc_mask) {
		hub = &adev->vmhub[AMDGPU_GFXHUB(i)];
		WREG32_SOC15_OFFSET(GC, GET_INST(GC, i),
				    regVM_CONTEXT0_PAGE_TABLE_BASE_ADDR_LO32,
				    hub->ctx_addr_distance * vmid,
				    lower_32_bits(page_table_base));
    ...
}

When a lower physical XCC is harvested, amdgpu_populate_ip_map() sets
adev->ip_map.dev_inst[GC_HWIP][i] to -1 for i >= NUM_XCC(adev->gfx.xcc_mask).
Passing a physical bit position i >= NUM_XCC(adev->gfx.xcc_mask) through
amdgpu_gmc_bind_vmid() causes GET_INST(GC, i) to return 0xffffffff, reading
adev->reg_offset[GC_HWIP][0xffffffff] out of bounds and flushing an
uninitialized adev->vmhub[AMDGPU_GFXHUB(i)], while skipping the logical
GFXHUBs at the harvested bit positions.  Using
GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0) avoids mixing physical and
logical XCC masks.

[ ... ]

> @@ -3027,14 +3034,19 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
>  	}
>  	case AMDGPU_VM_OP_UNRESERVE_VMID: {
>  		struct amdgpu_bo *root = vm->root.bo;
> +		bool reserved = false;
> +		int i;
>  
>  		amdgpu_bo_reserve(root, true);
> +		for_each_inst(i, adev->gfx.xcc_mask)
> +			reserved |= amdgpu_vmid_uses_reserved(vm,
> +							AMDGPU_GFXHUB(i));
>  		/* Wait for the jobs which might still use the reserved VMID */
> -		if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
> +		if (reserved)
>  			dma_resv_wait_timeout(root->tbo.base.resv,
>  					      DMA_RESV_USAGE_READ, false,
>  					      MAX_SCHEDULE_TIMEOUT);
> -		amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
> +		amdgpu_vmid_free_spm_gfxhubs(adev, vm, adev->gfx.xcc_mask);
>  		amdgpu_bo_unreserve(root);
>  		break;
>  	}

[Severity: High]
Should amdgpu_vm_fini() also check all GFXHUBs before waiting on
root->tbo.base.resv, matching this change in AMDGPU_VM_OP_UNRESERVE_VMID?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_fini() {
    ...
	/* Wait for the jobs which might still use the reserved VMID */
	if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
		dma_resv_wait_timeout(root->tbo.base.resv, DMA_RESV_USAGE_READ,
				      false, MAX_SCHEDULE_TIMEOUT);
    ...
	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) {
		amdgpu_vmid_free_spm(adev, vm, i);
	}
    ...
}

If a VM holds an SPM reservation on a non-zero GFXHUB while
vm->reserved_vmid[AMDGPU_GFXHUB(0)] is NULL (for example when reserved on a
subset of GFXHUBs via amdgpu_vmid_alloc_spm_gfxhubs()), closing the DRM file
skips dma_resv_wait_timeout(), frees the root PD in amdgpu_vm_pt_free_root(),
and unbinds the SPM VMID to 0 while jobs using that VMID are still running.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117165952.3558762-1-James.Zhu@amd.com?part=2

  reply	other threads:[~2026-11-17 17:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 16:59 [RFC PATCH 0/3] drm/amdgpu: make the reserved SPM VMID per-partition James Zhu
2026-11-17 16:59 ` [RFC PATCH 1/3] drm/amdgpu: expose the XCC-masked setup_vm_pt_regs as a gfxhub callback James Zhu
2026-11-17 16:59 ` [RFC PATCH 2/3] drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance James Zhu
2026-11-17 17:21   ` sashiko-bot [this message]
2026-11-17 16:59 ` [RFC PATCH 3/3] drm/amdgpu: scope AMDGPU_VM_OP_RESERVE_VMID to the caller's partition James Zhu
2026-11-17 17:18   ` sashiko-bot
2026-11-17 18:19 ` [RFC PATCH v2 0/3] drm/amdgpu: make the reserved SPM VMID per-partition James Zhu
2026-11-17 18:19   ` [RFC PATCH v2 1/3] drm/amdgpu: expose the XCC-masked setup_vm_pt_regs as a gfxhub callback James Zhu
2026-11-17 18:19   ` [RFC PATCH v2 2/3] drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance James Zhu
2026-11-17 18:47     ` sashiko-bot
2026-11-17 18:19   ` [RFC PATCH v2 3/3] drm/amdgpu: scope AMDGPU_VM_OP_RESERVE_VMID to the caller's partition James Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-219228@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=James.Zhu@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.