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 2/2] accel/amdxdna: use READ_ONCE/WRITE_ONCE on the command header
Date: Wed, 26 Aug 2026 16:16:16 -0700 [thread overview]
Message-ID: <e67eebfc-9b32-115b-e605-09db03b06119@amd.com> (raw)
In-Reply-To: <20260826153121.133507-3-taimuraz@kaitmazov.com>
On 8/26/26 08:31, Taimuraz Kaitmazov wrote:
> struct amdxdna_cmd::header packs STATE, OPCODE, COUNT and EXTRA_CU_MASK
> into one u32 that lives in a BO user space keeps mapped. The driver
> re-reads it on every accessor call and read-modify-writes STATE in place,
> with plain accesses that let the compiler split or refetch either side.
>
> Annotate them, as the UMQ ring indices already are. This does not make
> the update atomic against user space, it only stops the compiler from
> making it worse.
The user space should not change the BO content after the command is
submitted. Otherwise, the command could fail.
In another word, driver/hardware treat this as invalid command. It is ok
as long as kernel/firmware does not crash. So the bad user application
only messes up itself.
Thanks,
Lizhi
>
> Signed-off-by: Taimuraz Kaitmazov <taimuraz@kaitmazov.com>
> ---
> drivers/accel/amdxdna/amdxdna_ctx.c | 14 +++++++++-----
> drivers/accel/amdxdna/amdxdna_ctx.h | 11 +++++++----
> 2 files changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/amdxdna_ctx.c
> index 9f44e3918bc1..148ae51db0f1 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.c
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.c
> @@ -123,9 +123,10 @@ void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size)
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN)
> num_masks = 0;
> else
> - num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header);
> + num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK,
> + READ_ONCE(cmd->header));
>
> - count = FIELD_GET(AMDXDNA_CMD_COUNT, cmd->header);
> + count = FIELD_GET(AMDXDNA_CMD_COUNT, READ_ONCE(cmd->header));
> if (unlikely(count <= num_masks ||
> count * sizeof(u32) +
> offsetof(struct amdxdna_cmd, data[0]) >
> @@ -173,7 +174,7 @@ u32 amdxdna_cmd_get_cu_idx(struct amdxdna_gem_obj *abo)
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN)
> return INVALID_CU_IDX;
>
> - num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header);
> + num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, READ_ONCE(cmd->header));
> cu_mask = cmd->data;
> for (i = 0; i < num_masks; i++) {
> if (cu_mask[i])
> @@ -191,12 +192,15 @@ int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
> struct amdxdna_client *client = job->hwctx->client;
> struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
> struct amdxdna_cmd_chain *cc = NULL;
> + u32 header;
>
> if (!cmd)
> return -ENOMEM;
>
> - cmd->header &= ~AMDXDNA_CMD_STATE;
> - cmd->header |= FIELD_PREP(AMDXDNA_CMD_STATE, error_state);
> + header = READ_ONCE(cmd->header);
> + header &= ~AMDXDNA_CMD_STATE;
> + header |= FIELD_PREP(AMDXDNA_CMD_STATE, error_state);
> + WRITE_ONCE(cmd->header, header);
>
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN) {
> u32 ccnt;
> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.h b/drivers/accel/amdxdna/amdxdna_ctx.h
> index f6529d512217..48e1fcfd818f 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.h
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.h
> @@ -169,19 +169,22 @@ amdxdna_cmd_get_op(struct amdxdna_gem_obj *abo)
> if (!cmd)
> return ERT_INVALID_CMD;
>
> - return FIELD_GET(AMDXDNA_CMD_OPCODE, cmd->header);
> + return FIELD_GET(AMDXDNA_CMD_OPCODE, READ_ONCE(cmd->header));
> }
>
> static inline void
> amdxdna_cmd_set_state(struct amdxdna_gem_obj *abo, enum ert_cmd_state s)
> {
> struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
> + u32 header;
>
> if (!cmd)
> return;
>
> - cmd->header &= ~AMDXDNA_CMD_STATE;
> - cmd->header |= FIELD_PREP(AMDXDNA_CMD_STATE, s);
> + header = READ_ONCE(cmd->header);
> + header &= ~AMDXDNA_CMD_STATE;
> + header |= FIELD_PREP(AMDXDNA_CMD_STATE, s);
> + WRITE_ONCE(cmd->header, header);
> }
>
> static inline enum ert_cmd_state
> @@ -192,7 +195,7 @@ amdxdna_cmd_get_state(struct amdxdna_gem_obj *abo)
> if (!cmd)
> return ERT_CMD_STATE_INVALID;
>
> - return FIELD_GET(AMDXDNA_CMD_STATE, cmd->header);
> + return FIELD_GET(AMDXDNA_CMD_STATE, READ_ONCE(cmd->header));
> }
>
> void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size);
prev parent reply other threads:[~2026-08-26 23:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260826143036.100089-1-taimuraz@kaitmazov.com>
[not found] ` <20260826153121.133507-1-taimuraz@kaitmazov.com>
[not found] ` <20260826153121.133507-2-taimuraz@kaitmazov.com>
2026-08-26 21:32 ` [PATCH v2 1/2] accel/amdxdna: check the command chain payload before using it Lizhi Hou
[not found] ` <20260826153121.133507-3-taimuraz@kaitmazov.com>
2026-08-26 23:16 ` Lizhi Hou [this message]
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=e67eebfc-9b32-115b-e605-09db03b06119@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®