All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Natalie Vock" <nat@pixelcluster.dev>
Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 09/10] drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
Date: Tue, 17 Nov 2026 15:34:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-219088@kernel.org> (raw)
In-Reply-To: <20261117-prt-explicit-sync-v2-9-bb3851cb6a0c@pixelcluster.dev>

Thank you for your contribution! Sashiko AI review found 15 potential
issues to consider:
- [High] drm/amdgpu: wrong argument to amdgpu_vm_bo_unmap() in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: syncobj_handles leak in
  amdgpu_gem_prepare_timeline_nodes()
- [High] drm/amdgpu: uninitialized fence pointer in
  amdgpu_gem_va2_ioctl() error path
- [High] drm/amdgpu: lost bo_va->last_pt_update and TLB fences in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: wrong struct in DRM_IOCTL_AMDGPU_GEM_VA2 macro
  definition
- [High] drm/amdgpu: missing runtime PM acquisition in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: unlocked BO update on CLEAR/REPLACE in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: unlocked amdgpu_vm_bo_find() call in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: gobj ref leak on lock contention/error in
  amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: memory leak and list corruption in
  amdgpu_vm_update_ctx_rollback()
- [High] drm/amdgpu: fence ref leak when merging fences in
  amdgpu_gem_va2_ioctl()
- [Medium] drm/amdgpu: delay never set to true in amdgpu_gem_va2_ioctl()
- [Medium] drm/amdgpu: wrong pointer in PTR_ERR() in
  amdgpu_gem_add_input_fence()
- [Medium] drm/amdgpu: gobj ref leak on drm_exec_lock_obj() contention
  or error
- [Medium] drm/amdgpu: in-place VA masking in amdgpu_gem_va2_ioctl()
  breaks retry
--

commit dddcd1a96bab3eebca95b4fd6d51bf06141d06e1
Author: Natalie Vock <nat@pixelcluster.dev>

drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl

This commit adds the DRM_IOCTL_AMDGPU_GEM_VA2 ioctl to batch multiple VM
mapping operations in a single call with multiple input and output binary or
timeline syncobjs.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> index 7eed5c360be20..6d6a6017d4787 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
> @@ -53,10 +53,13 @@ static int
>  amdgpu_gem_add_input_fence(struct drm_file *filp,
>  			   struct amdgpu_vm_update_ctx *update_ctx,
>  			   uint64_t syncobj_handles_array,
> +			   uint64_t syncobj_points_array,
>  			   uint32_t num_syncobj_handles)
>  {
>  	struct dma_fence *fence;
>  	uint32_t *syncobj_handles;
> +	uint64_t *syncobj_points;
> +	uint64_t point;
>  	int ret, i;
>  
>  	if (!num_syncobj_handles)
> @@ -67,6 +70,17 @@ amdgpu_gem_add_input_fence(struct drm_file *filp,
>  	if (IS_ERR(syncobj_handles))
>  		return PTR_ERR(syncobj_handles);
>  
> +	if (syncobj_points_array) {
> +		syncobj_points = memdup_user(u64_to_user_ptr(syncobj_points_array),
> +					      size_mul(sizeof(uint64_t), num_syncobj_handles));
> +		if (IS_ERR(syncobj_points)) {
> +			kfree(syncobj_handles);
> +			return PTR_ERR(syncobj_handles);
> +		}

[Severity: Medium]
Should this return PTR_ERR(syncobj_points) instead of
PTR_ERR(syncobj_handles)?  syncobj_handles is a valid kernel heap pointer
here, so returning PTR_ERR(syncobj_handles) leaks its lower 32 bits as the
ioctl return value (or returns 0 if 4 GiB-aligned).

[ ... ]

> +static int amdgpu_gem_prepare_timeline_nodes(
> +	struct drm_file *filp, uint64_t syncobj_handles_array,
> +	uint64_t syncobj_points_array, uint32_t num_syncobjs,
> +	struct amdgpu_gem_timeline_out *out)
> +{
> +	uint32_t *syncobj_handles;
> +	uint32_t i;
> +	int r;
> +
> +	syncobj_handles = memdup_user(u64_to_user_ptr(syncobj_handles_array),
> +				      size_mul(sizeof(uint32_t), num_syncobjs));
> +	if (IS_ERR(syncobj_handles))
> +		return PTR_ERR(syncobj_handles);

[ ... ]

> +	for (i = 0; i < num_syncobjs; i++) {
> +		out->syncobjs[i] = drm_syncobj_find(filp, syncobj_handles[i]);
> +		if (!out->syncobjs[i]) {
> +			r = -ENOENT;
> +			goto err_loop_rollback;
> +		}
> +
> +		if (!out->timeline_points[i])
> +			continue;
> +
> +		out->chains[i] = dma_fence_chain_alloc();
> +		if (!out->chains[i]) {
> +			r = -ENOMEM;
> +			goto err_loop_rollback;
> +		}
> +
> +		continue;
> +err_loop_rollback:
> +		for (;;) {
> +			if (out->syncobjs[i])
> +				drm_syncobj_put(out->syncobjs[i]);
> +			if (out->chains[i])
> +				dma_fence_chain_free(out->chains[i]);
> +			if (i-- == 0)
> +				goto err_free_sync;
> +		}
> +	}
> +
> +	return 0;

[Severity: High]
Does the success path here leak syncobj_handles?  kfree(syncobj_handles) is
only called on the error path at err_free_handles, so every call with
num_syncobjs > 0 leaks the memdup_user() buffer.

[ ... ]

> @@ -1100,6 +1218,232 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
>  	return r;
>  }
>  
> +int amdgpu_gem_va2_ioctl(struct drm_device *dev, void *data,
> +			 struct drm_file *filp)
> +{
> +	struct drm_amdgpu_gem_va2 *args = data;
> +	struct amdgpu_device *adev = drm_to_adev(dev);
> +	struct amdgpu_fpriv *fpriv = filp->driver_priv;
> +	struct drm_amdgpu_gem_va_mapping *mappings;
> +	struct amdgpu_gem_timeline_out tl_out;
> +	struct amdgpu_vm_update_ctx update_ctx;
> +	struct drm_gem_object *gobj;
> +	struct amdgpu_bo_va *bo_va;
> +	struct drm_exec exec;
> +	bool delay = false;
> +	u32 i;
> +	int r;
> +
> +	mappings = vmemdup_array_user(u64_to_user_ptr(args->mappings),
> +				      args->num_mappings,
> +				      sizeof(struct drm_amdgpu_gem_va_mapping));
> +	if (IS_ERR(mappings))
> +		return PTR_ERR(mappings);
> +
> +	r = amdgpu_gem_prepare_timeline_nodes(filp, args->syncobjs_out,
> +					      args->timeline_points_out,
> +					      args->num_syncobjs_out, &tl_out);
> +	if (r)
> +		goto out_free_mappings;
> +
> +	drm_exec_init(&exec,
> +		      DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES,
> +		      0);
> +	amdgpu_vm_update_ctx_init(&update_ctx, adev, &fpriv->vm);

[Severity: High]
Does amdgpu_gem_va2_ioctl() need to acquire a runtime PM reference via
PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(), like amdgpu_gem_va_ioctl() does?
Without it, non-delayed VM updates can write to VRAM, flush HDP via MMIO, or
submit SDMA jobs while the GPU is runtime-suspended.

> +	update_ctx.explicit_sync_unmap = true;
> +
> +	r = amdgpu_gem_add_input_fence(filp, &update_ctx, args->syncobjs_in,
> +				       args->timeline_points_in,
> +				       args->num_syncobjs_in);
> +	if (r)
> +		goto out;
> +
> +	drm_exec_until_all_locked(&exec)
> +	{
> +		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
> +		drm_exec_retry_on_contention(&exec);
> +		if (unlikely(r))
> +			goto out;
> +
> +		for (i = 0; i < args->num_mappings; ++i) {
> +			r = amdgpu_gem_va_validate_args(adev, mappings[i].va,
> +							mappings[i].map_size,
> +							mappings[i].flags);
> +			if (r)
> +				goto out;
> +
> +			mappings[i].va &= AMDGPU_GMC_HOLE_MASK;

[Severity: Medium]
Can masking mappings[i].va in place break retries on lock contention?  For
an address in the upper canonical range (>= AMDGPU_GMC_HOLE_END), masking it
with AMDGPU_GMC_HOLE_MASK places it inside [AMDGPU_GMC_HOLE_START,
AMDGPU_GMC_HOLE_END), so if drm_exec_until_all_locked() retries the loop,
amdgpu_gem_va_validate_args() will reject mappings[i].va with -EINVAL.

> +
> +			if (mappings[i].flags & AMDGPU_VM_DELAY_UPDATE) {
> +				/* Can't delay if we're supposed to signal fences */
> +				if (args->num_syncobjs_out) {
> +					r = -EINVAL;
> +					goto out;
> +				}
> +				/* If one mapping is delayed, all mappings must be delayed */
> +				if (i && !delay) {
> +					r = -EINVAL;
> +					goto out;
> +				}
> +			} else if (delay) {
> +				/* If one mapping is not delayed, no mappings may be delayed */
> +				r = -EINVAL;
> +				goto out;
> +			}

[Severity: Medium]
Is delay ever set to true when AMDGPU_VM_DELAY_UPDATE is set?  Because delay
stays false, any batch with two or more delayed mappings fails with -EINVAL
at if (i && !delay), a batch where only mappings[0] is delayed is not
rejected, and page table updates are still executed immediately.

> +
> +			if ((mappings[i].operation != AMDGPU_VA_OP_CLEAR) &&
> +			    !(mappings[i].flags & AMDGPU_VM_PAGE_PRT)) {
> +				gobj = drm_gem_object_lookup(
> +					filp, mappings[i].handle);
> +				if (gobj == NULL) {
> +					r = -ENOENT;
> +					goto out;
> +				}
> +				bo_va = amdgpu_vm_bo_find(
> +					&fpriv->vm, gem_to_amdgpu_bo(gobj));
> +				if (!bo_va) {
> +					drm_gem_object_put(gobj);
> +					r = -ENOENT;
> +					goto out;
> +				}

[Severity: High]
Is it safe to call amdgpu_vm_bo_find() before locking gobj with
drm_exec_lock_obj()?  amdgpu_vm_bo_find() walks bo->vm_bo, which is
protected by bo->tbo.base.resv; on a shared BO, another DRM file can
concurrently modify bo->vm_bo and free its amdgpu_bo_va in
amdgpu_vm_bo_del().

> +			} else {
> +				gobj = NULL;
> +				bo_va = mappings[i].operation !=
> +							AMDGPU_VA_OP_CLEAR ?
> +						fpriv->prt_va :
> +						NULL;
> +			}
> +
> +			if (gobj) {
> +				r = drm_exec_lock_obj(&exec, gobj);
> +				if (drm_exec_is_contended(&exec))
> +					amdgpu_vm_update_ctx_rollback(
> +						&update_ctx);

[Severity: High]
When amdgpu_vm_update_ctx_rollback() runs here on lock contention (or via
amdgpu_vm_update_ctx_fini() on error), does it leak ctx->new mappings and
corrupt ctx->cleared?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_update_ctx_rollback() {
    ...
	list_for_each_entry_safe(mapping, next, &ctx->new, pending_list)
		list_del_init(&mapping->pending_list);
	list_for_each_entry_safe(mapping, next, &ctx->deleted, pending_list)
		list_del_init(&mapping->pending_list);
    ...
}

Entries on ctx->new were allocated by amdgpu_vm_bo_map() or
amdgpu_vm_bo_replace_map() and are unlinked without kfree(mapping).
In addition, ctx->cleared is never unlinked at all, leaving live mappings in
fpriv->vm.va with mapping->pending_list pointing into update_ctx.cleared on
the stack.

> +				drm_exec_retry_on_contention(&exec);

[Severity: Medium]
Does drm_exec_retry_on_contention(&exec) leak the gobj reference from
drm_gem_object_lookup()?  drm_exec_lock_obj() takes its own reference for
exec->contended on -EDEADLK, and jumping back to the start of the loop
skips drm_gem_object_put(gobj) below.

> +				if (unlikely(r))
> +					goto out;
> +			}

[Severity: High]
Does jumping to out when drm_exec_lock_obj() returns a non-contention error
(such as -EINTR or -ENOMEM) also leak the gobj lookup reference?
drm_exec_lock_obj() does not record gobj in exec->objects on failure, and
goto out bypasses drm_gem_object_put(gobj).

> +
> +			switch (mappings[i].operation) {
> +			case AMDGPU_VA_OP_MAP:
> +				r = amdgpu_vm_bo_map(&update_ctx, bo_va,
> +						     mappings[i].va,
> +						     mappings[i].offset_in_bo,
> +						     mappings[i].map_size,
> +						     mappings[i].flags);
> +				break;
> +			case AMDGPU_VA_OP_UNMAP:
> +				r = amdgpu_vm_bo_unmap(&update_ctx, bo_va,
> +						       mappings[i].flags);
> +				break;

[Severity: High]
Should amdgpu_vm_bo_unmap() be passed mappings[i].va instead of
mappings[i].flags?  Passing flags as the virtual address makes
AMDGPU_VA_OP_UNMAP look up page 0 and fail with -ENOENT.

[ ... ]

> +	if (!delay && (!adev->debug_vm || args->num_syncobjs_out)) {
> +		struct dma_fence *fence, *merged;
> +
> +		if (!amdgpu_vm_ready(&fpriv->vm)) {
> +			fence = dma_fence_get_stub();
> +			goto insert_fence;
> +		}
> +
> +		r = amdgpu_vm_clear_freed(&update_ctx, &fpriv->vm.last_update);
> +		if (r) {
> +			drm_err(adev_to_drm(adev),
> +				"Could not clear freed PT entries: %d\n", r);
> +			goto insert_fence;
> +		}

[Severity: High]
If amdgpu_vm_clear_freed() fails, does goto insert_fence use an
uninitialized fence pointer?  fence is only initialized afterwards, so
insert_fence will pass stack garbage to drm_syncobj_replace_fence(),
drm_syncobj_add_point(), and dma_fence_put().

> +
> +		fence = dma_fence_get(fpriv->vm.last_update);
> +
> +		list_for_each_entry(bo_va, &update_ctx.dirty_bo_vas,
> +				    update_link) {
> +			r = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
> +			if (r) {
> +				drm_err(adev_to_drm(adev),
> +					"Could not update BO PT entries: %d\n", r);
> +				goto insert_fence;
> +			}
> +		}

[Severity: High]
Can this loop call amdgpu_vm_bo_update() on BOs whose dma_resv is not
locked?

AMDGPU_VA_OP_CLEAR locks no BO, and AMDGPU_VA_OP_REPLACE only locks the new
BO from mappings[i].handle.  However, amdgpu_vm_update_ctx_execute_replace()
adds the bo_va of every existing mapping that was cleared or split to
update_ctx.dirty_bo_vas, so amdgpu_vm_bo_update() then accesses
bo->tbo.resource and calls amdgpu_sync_resv() on those affected BOs without
holding their reservation locks.

> +
> +		list_for_each_entry(bo_va, &update_ctx.dirty_bo_vas,
> +				    update_link) {
> +			if (!amdgpu_vm_is_bo_always_valid(&fpriv->vm,
> +							  bo_va->base.bo)) {
> +				dma_fence_put(bo_va->last_pt_update);
> +				bo_va->last_pt_update = dma_fence_get(fence);
> +			}
> +		}

[Severity: High]
Does this discard the page table update fences just generated by
amdgpu_vm_bo_update()?

For non-always-valid BOs (and PRT mappings where bo is NULL),
amdgpu_vm_bo_update() stores the new PT update fence in
bo_va->last_pt_update rather than fpriv->vm.last_update.  Overwriting
bo_va->last_pt_update with the pre-update fence without merging
bo_va->last_pt_update (or fpriv->vm.last_tlb_inv) into fence drops those PT
update and TLB invalidation fences from both bo_va->last_pt_update and the
signaled output syncobjs.

> +
> +		/* Always update PDEs after we touched the mappings. */
> +		r = amdgpu_vm_update_pdes(adev, &fpriv->vm, false);
> +		if (r)
> +			goto insert_fence;
> +
> +		merged = dma_fence_unwrap_merge(fence, fpriv->vm.last_update);
> +		if (!merged) {
> +			/* As fallback in OOM situations */
> +			dma_fence_wait(fpriv->vm.last_update, false);
> +			dma_fence_wait(fence, false);
> +			merged = dma_fence_get_stub();
> +		}
> +		fence = merged;

[Severity: High]
Does assigning fence = merged without calling dma_fence_put(fence) leak the
fence reference acquired before the amdgpu_vm_bo_update() loop?

> diff --git a/include/uapi/drm/amdgpu_drm.h b/include/uapi/drm/amdgpu_drm.h
> index 29009f6aabde9..c9e52bfa7d028 100644
> --- a/include/uapi/drm/amdgpu_drm.h
> +++ b/include/uapi/drm/amdgpu_drm.h
> @@ -83,6 +84,7 @@ extern "C" {
>  #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
>  #define DRM_IOCTL_AMDGPU_PROC_OPTIONS	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROC_OPTIONS, struct drm_amdgpu_proc_options)
>  #define DRM_IOCTL_AMDGPU_UALINK_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_UALINK_HANDLE, union drm_amdgpu_ualink_handle)
> +#define DRM_IOCTL_AMDGPU_GEM_VA2	DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_VA2, struct drm_amdgpu_gem_va)

[Severity: High]
Should DRM_IOCTL_AMDGPU_GEM_VA2 use struct drm_amdgpu_gem_va2 instead of
struct drm_amdgpu_gem_va?  struct drm_amdgpu_gem_va is 64 bytes whereas
struct drm_amdgpu_gem_va2 is 56 bytes, so drm_ioctl() will copy 8 bytes past
the end of the user struct.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-prt-explicit-sync-v2-0-bb3851cb6a0c@pixelcluster.dev?part=9

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

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:50 [PATCH v2 00/10] drm/amdgpu: Rework VM update ops for batching and explicit sync Natalie Vock
2026-11-17 12:50 ` [PATCH v2 01/10] drm/amdgpu: Remove unused parameter from amdgpu_vm_pte_fragment Natalie Vock
2026-11-17 12:50 ` [PATCH v2 02/10] drm/amdgpu: Add and use vm_update_ctx struct Natalie Vock
2026-11-17 13:26   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 03/10] drm/amdgpu: Make VM maps pending per update context Natalie Vock
2026-11-17 13:43   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 04/10] drm/amdgpu: Make removed/cleared mappings " Natalie Vock
2026-11-17 14:05   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 05/10] drm/amdgpu: Track dirty bo_va structures in update ctx Natalie Vock
2026-11-17 14:28   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 06/10] drm/amdgpu: Add function to roll back a VM " Natalie Vock
2026-11-17 14:40   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 07/10] drm/amdgpu: Move some VA op validation to a helper Natalie Vock
2026-11-17 12:50 ` [PATCH v2 08/10] drm/amdgpu: Only reject conflicting mappings if they are not cleared Natalie Vock
2026-11-17 15:06   ` sashiko-bot
2026-11-17 12:50 ` [PATCH v2 09/10] drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl Natalie Vock
2026-11-17 15:34   ` sashiko-bot [this message]
2026-11-17 22:46   ` Dave Airlie
2026-11-17 12:50 ` [PATCH v2 10/10] drm/amdgpu: Bump minor version Natalie Vock

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-219088@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=nat@pixelcluster.dev \
    --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.