All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Zhang" <yidong.zhang@amd.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH V3 16/19] accel/amdxdna: Implement AIE4 command packet building and submission
Date: Thu, 08 Oct 2026 04:10:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-163605@kernel.org> (raw)
In-Reply-To: <20261008032348.2044667-17-yidong.zhang@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] amdxdna: DMA UAF via user-writable UMQ read index spoofing
- [High] amdxdna: out-of-bounds read of dpu->chained before payload size validation
- [High] drm: dma_fence_context_alloc() per job breaks dma_resv timeline coalescing
--

commit 2abb65f20d471579d835973557557cc2b94cacf8
Author: David Zhang <yidong.zhang@amd.com>
Subject: accel/amdxdna: Implement AIE4 command packet building and submission

This patch implements kernel-mode command submission and hardware queue packet
assembly for AIE4. It adds functions to validate command buffers, build
direct and indirect packets, and manage fence contexts for jobs. It also
holds references to sub-command BOs during chained submissions to prevent
use-after-free conditions.

> diff --git a/drivers/accel/amdxdna/aie4_ctx.c b/drivers/accel/amdxdna/aie4_ctx.c
> index 09c92b1b5134e..31bf30b9dc86a 100644
> --- a/drivers/accel/amdxdna/aie4_ctx.c
> +++ b/drivers/accel/amdxdna/aie4_ctx.c

[ ... ]

> @@ -469,3 +502,717 @@ int aie4_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout)

[ ... ]

> +static int validate_cmd_abo(struct amdxdna_dev *xdna, struct amdxdna_gem_obj *cmd_abo,
> +			    struct amdxdna_cmd_start_dpu **dpu_out, u16 *chained_cnt)
> +{
> +	struct amdxdna_cmd_start_dpu *dpu;
> +	u32 payload_size;
> +	u16 chained;
> +	u32 op;
> +	u16 i;
> +
> +	op = amdxdna_cmd_get_op(cmd_abo);
> +	if (op != ERT_START_DPU) {
> +		XDNA_ERR(xdna, "Invalid exec buf op, %d", op);
> +		return -EINVAL;
> +	}
> +
> +	dpu = amdxdna_cmd_get_payload(cmd_abo, &payload_size);
> +	if (!dpu) {
> +		XDNA_ERR(xdna, "Invalid DPU payload");
> +		return -EINVAL;
> +	}
> +	chained = READ_ONCE(dpu->chained);
> +	if (chained >= HSA_MAX_LEVEL1_INDIRECT_ENTRIES ||
> +	    payload_size < (u32)(chained + 1) * sizeof(*dpu)) {

[Severity: High]
Does this code risk an out-of-bounds read if the user supplies a command BO
where the payload pointer falls at the very end of a mapped page?

The speculative read of dpu->chained happens before the payload_size is
validated to be at least sizeof(*dpu). If the payload leaves fewer bytes
than sizeof(*dpu) at the end of a page and the adjacent page is unmapped,
accessing dpu->chained could cause a fatal kernel page fault.

Should the payload_size check for at least sizeof(*dpu) happen before
accessing any fields from dpu?

> +		XDNA_ERR(xdna, "Invalid DPU chained entries %u, payload %u",
> +			 chained, payload_size);
> +		return -EINVAL;
> +	}

[ ... ]

> +static void job_worker(struct work_struct *work)
> +{
> +	struct amdxdna_hwctx_priv *priv =
> +		container_of(work, struct amdxdna_hwctx_priv, job_work);
> +	struct amdxdna_hwctx *hwctx = priv->hwctx;
> +	struct amdxdna_sched_job *job;
> +
> +	while ((job = peek_running_job(hwctx))) {
> +		wait_till_seq_completed(hwctx, job->seq);
> +		if (get_read_index(hwctx) > job->seq) {

[Severity: Critical]
Can this implementation be tricked by userspace into releasing BOs while the
hardware is still executing?

The kernel determines job completion by reading the hardware completion status
from a memory location via get_read_index(), which points to a GEM object
allocated by the user.

If malicious userspace maps this object as writable and manually advances the
read_index to match the write_index, the worker would see completion and
call job_complete(). This would prematurely signal the job's fence and drop
references to the command and argument BOs.

Since the fence is signaled, userspace can free the BOs immediately, causing
the physical pages to be returned to the system while the NPU hardware might
continue executing and DMA-ing to those pages. Could this lead to memory
corruption on systems using iommu=pt?

> +			dequeue_running_job(hwctx, job);
> +			/* Abort partially submitted jobs; complete fully submitted ones. */
> +			if (job->aie4_job_state != AIE4_JOB_STATE_SUBMITTED)
> +				job_abort(job);
> +			else
> +				job_complete(job);
> +		} else if (aie4_hwctx_has_error(hwctx)) {
> +			dequeue_running_job(hwctx, job);
> +			job_abort(job);
> +		} else {
> +			/* suspend/resume */
> +			break;
> +		}
> +	}
> +}

[ ... ]

> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/amdxdna_ctx.c
> index 888e857ec5582..608b86fe8f6bb 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.c
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.c

[ ... ]

> @@ -55,9 +51,9 @@ static struct dma_fence *amdxdna_fence_create(struct amdxdna_hwctx *hwctx)
>  	if (!fence)
>  		return NULL;
>  
> -	fence->hwctx = hwctx;
>  	spin_lock_init(&fence->lock);
> -	dma_fence_init(&fence->base, &fence_ops, &fence->lock, hwctx->id, 0);
> +	/* Unique timeline context prevents eviction from shared BO reservation. */
> +	dma_fence_init(&fence->base, &fence_ops, &fence->lock, dma_fence_context_alloc(1), 0);

[Severity: High]
Can allocating a unique context per job break dma_resv timeline coalescing
and cause unbounded kernel memory allocation?

By using dma_fence_context_alloc(1) for every job, each job gets a unique
timeline context. When dma_resv_add_fence() adds these fences to a shared BO
in fence_job_bos(), it cannot identify older fences as belonging to the same
timeline because dma_fence_is_later_or_same() will fail to match contexts.

If userspace spawns multiple threads to concurrently submit jobs referencing
the same shared BO, could dma_resv accumulate every unsignaled fence and grow
its internal array linearly until the jobs complete, potentially leading to
OOM or page allocation failures?

>  	return &fence->base;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261008032348.2044667-1-yidong.zhang@amd.com?part=16

  reply	other threads:[~2026-10-08  4:10 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-08  3:23 [PATCH V3 00/19] accel/amdxdna: Kernel submission and PM for AIE4 David Zhang
2026-10-08  3:23 ` [PATCH V3 01/19] accel/amdxdna: Rename NPU3 firmware files David Zhang
2026-10-08 16:23   ` Lizhi Hou
2026-10-08  3:23 ` [PATCH V3 02/19] accel/amdxdna: Remove mmap for doorbell David Zhang
2026-10-08  3:23 ` [PATCH V3 03/19] accel/amdxdna: Add CERT firmware version support David Zhang
2026-10-08 17:12   ` Lizhi Hou
2026-10-08 17:26     ` Zhang, Yidong (David)
2026-10-08  3:23 ` [PATCH V3 04/19] accel/amdxdna: Upgrade firmware version to 6.0 David Zhang
2026-10-08 17:15   ` Lizhi Hou
2026-10-08  3:23 ` [PATCH V3 05/19] accel/amdxdna: Add NPU3 classic device support David Zhang
2026-10-08  3:44   ` sashiko-bot
2026-10-08 17:25     ` Zhang, Yidong (David)
2026-10-08  3:23 ` [PATCH V3 06/19] accel/amdxdna: Add AIE version query to aie4_get_info David Zhang
2026-10-08  3:23 ` [PATCH V3 07/19] accel/amdxdna: Add get and set power_mode for AIE4 David Zhang
2026-10-08  3:23 ` [PATCH V3 08/19] accel/amdxdna: Add clock, DPM frequency, and resource info queries " David Zhang
2026-10-08  3:48   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 09/19] accel/amdxdna: Add context switch hysteresis with debugfs control David Zhang
2026-10-08  3:23 ` [PATCH V3 10/19] accel/amdxdna: Refactor AIE4 hardware initialization sequence David Zhang
2026-10-08  3:23 ` [PATCH V3 11/19] accel/amdxdna: Decouple AIE4 doorbell and MSI-X notify transport hooks David Zhang
2026-10-08  3:51   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 12/19] accel/amdxdna: Implement AIE4 kernel queue lifecycle and memory layout David Zhang
2026-10-08  3:59   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 13/19] accel/amdxdna: Prepare for AIE4 command submission David Zhang
2026-10-08  4:04   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 14/19] accel/amdxdna: Move HMM invalidate wait into common GEM code David Zhang
2026-10-08  3:56   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 15/19] accel/amdxdna: Make populate_range common for AIE2 and AIE4 David Zhang
2026-10-08  3:23 ` [PATCH V3 16/19] accel/amdxdna: Implement AIE4 command packet building and submission David Zhang
2026-10-08  4:10   ` sashiko-bot [this message]
2026-10-08  3:23 ` [PATCH V3 17/19] accel/amdxdna: Enable AIE4 firmware logging to DRAM David Zhang
2026-10-08  3:23 ` [PATCH V3 18/19] accel/amdxdna: Implement AIE4 suspend and resume David Zhang
2026-10-08  4:10   ` sashiko-bot
2026-10-08  3:23 ` [PATCH V3 19/19] accel/amdxdna: Implement runtime suspend and resume support David Zhang
2026-10-08  4:22   ` sashiko-bot

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-163605@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yidong.zhang@amd.com \
    /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.