From: Lizhi Hou <lizhi.hou@amd.com>
To: Taimuraz Kaitmazov <taimuraz@kaitmazov.com>,
Min Ma <mamin506@gmail.com>, Oded Gabbay <ogabbay@kernel.org>
Cc: <dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 1/2] accel/amdxdna: check the command chain payload before using it
Date: Wed, 26 Aug 2026 14:32:04 -0700 [thread overview]
Message-ID: <a203be1c-d28d-c33e-0a94-da725ec2d5aa@amd.com> (raw)
In-Reply-To: <20260826153121.133507-2-taimuraz@kaitmazov.com>
On 8/26/26 08:31, Taimuraz Kaitmazov wrote:
> amdxdna_cmd_get_payload() only bounds-checks the payload when a size
> pointer is passed, so its two callers get different guarantees from one
> function. amdxdna_cmd_set_error() takes the unchecked form and then reads
> cc->command_count and cc->data[0], neither of which has been shown to
> lie inside the BO.
AMDXDNA_CMD_EXTRA_CU_MASK is 2 bits. So cc->command_count and
cc->data[0] will be in the BO scope.
This is IO path. I would change it only when there is a real issue.
Thanks,
Lizhi
>
> Make the size mandatory and add amdxdna_cmd_get_chain(), which returns
> the chain only once the declared command count is known to fit.
>
> Signed-off-by: Taimuraz Kaitmazov <taimuraz@kaitmazov.com>
> ---
> drivers/accel/amdxdna/amdxdna_ctx.c | 51 ++++++++++++++++++++++-------
> drivers/accel/amdxdna/amdxdna_ctx.h | 2 ++
> 2 files changed, 41 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/amdxdna_ctx.c
> index 855da8c79a1c..9f44e3918bc1 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.c
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.c
> @@ -125,20 +125,42 @@ void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size)
> else
> num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header);
>
> - if (size) {
> - count = FIELD_GET(AMDXDNA_CMD_COUNT, cmd->header);
> - if (unlikely(count <= num_masks ||
> - count * sizeof(u32) +
> - offsetof(struct amdxdna_cmd, data[0]) >
> - abo->mem.size)) {
> - *size = 0;
> - return NULL;
> - }
> - *size = (count - num_masks) * sizeof(u32);
> + count = FIELD_GET(AMDXDNA_CMD_COUNT, cmd->header);
> + if (unlikely(count <= num_masks ||
> + count * sizeof(u32) +
> + offsetof(struct amdxdna_cmd, data[0]) >
> + abo->mem.size)) {
> + *size = 0;
> + return NULL;
> }
> + *size = (count - num_masks) * sizeof(u32);
> +
> return &cmd->data[num_masks];
> }
>
> +/*
> + * Returns the chain payload of @abo, with @count set to a command count that
> + * has been checked to fit. The chain fields live in a BO user space keeps
> + * mapped, so nothing may read them without going through here.
> + */
> +struct amdxdna_cmd_chain *
> +amdxdna_cmd_get_chain(struct amdxdna_gem_obj *abo, u32 *count)
> +{
> + struct amdxdna_cmd_chain *cc;
> + u32 len, ccnt;
> +
> + cc = amdxdna_cmd_get_payload(abo, &len);
> + if (!cc || len < sizeof(*cc))
> + return NULL;
> +
> + ccnt = READ_ONCE(cc->command_count);
> + if (len < struct_size(cc, data, ccnt))
> + return NULL;
> +
> + *count = ccnt;
> + return cc;
> +}
> +
> u32 amdxdna_cmd_get_cu_idx(struct amdxdna_gem_obj *abo)
> {
> struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
> @@ -177,8 +199,13 @@ int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
> cmd->header |= FIELD_PREP(AMDXDNA_CMD_STATE, error_state);
>
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN) {
> - cc = amdxdna_cmd_get_payload(abo, NULL);
> - cc->error_index = (cmd_idx < cc->command_count) ? cmd_idx : 0;
> + u32 ccnt;
> +
> + cc = amdxdna_cmd_get_chain(abo, &ccnt);
> + if (!cc || !ccnt)
> + return -EINVAL;
> +
> + cc->error_index = (cmd_idx < ccnt) ? cmd_idx : 0;
> abo = amdxdna_gem_get_obj(client, cc->data[0], AMDXDNA_BO_SHARE);
> if (!abo)
> return -EINVAL;
> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.h b/drivers/accel/amdxdna/amdxdna_ctx.h
> index b6bef3af7dab..f6529d512217 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.h
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.h
> @@ -196,6 +196,8 @@ amdxdna_cmd_get_state(struct amdxdna_gem_obj *abo)
> }
>
> void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size);
> +struct amdxdna_cmd_chain *
> +amdxdna_cmd_get_chain(struct amdxdna_gem_obj *abo, u32 *count);
> u32 amdxdna_cmd_get_cu_idx(struct amdxdna_gem_obj *abo);
> int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
> struct amdxdna_sched_job *job, u32 cmd_idx,
next prev parent reply other threads:[~2026-08-26 21:32 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-26 14:30 [PATCH 0/2] accel/amdxdna: make the shared command header safe to read Taimuraz Kaitmazov
2026-08-26 14:30 ` [PATCH 1/2] accel/amdxdna: check the command chain payload before using it Taimuraz Kaitmazov
2026-08-26 14:43 ` sashiko-bot
2026-08-26 14:30 ` [PATCH 2/2] accel/amdxdna: use READ_ONCE/WRITE_ONCE on the command header Taimuraz Kaitmazov
2026-08-26 14:50 ` sashiko-bot
2026-08-26 15:31 ` [PATCH v2 0/2] accel/amdxdna: make the shared command header safe to read Taimuraz Kaitmazov
2026-08-26 15:31 ` [PATCH v2 1/2] accel/amdxdna: check the command chain payload before using it Taimuraz Kaitmazov
2026-08-26 15:48 ` sashiko-bot
2026-08-26 21:32 ` Lizhi Hou [this message]
2026-08-26 22:13 ` [PATCH v3] accel/amdxdna: use READ_ONCE/WRITE_ONCE on the command header Taimuraz Kaitmazov
2026-08-26 22:27 ` sashiko-bot
2026-08-26 15:31 ` [PATCH v2 2/2] " Taimuraz Kaitmazov
2026-08-26 23:16 ` Lizhi Hou
2026-08-27 0:20 ` Taimuraz Kaitmazov
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=a203be1c-d28d-c33e-0a94-da725ec2d5aa@amd.com \
--to=lizhi.hou@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mamin506@gmail.com \
--cc=ogabbay@kernel.org \
--cc=taimuraz@kaitmazov.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.