accel/amdxdna: eliminate GFP_KERNEL allocation from mailbox send path
PATCH 1/1 accel/amdxdna: eliminate GFP_KERNEL allocation from mailbox send path
Replace the per-message kzalloc() with a pre-allocated pool of N slots. Each slot is rb_size bytes which is the combined size of the mailbox_msg metadata and the package payload must not exceed rb_size. No dynamic allocation is needed in the send hot path.
msg_id encodes the slot index as (slot | MAGIC_VAL), allowing O(1) lookup on response. A bool busy field tracks slot occupancy; it is set after the memset() that initialises the slot so the flag is not zeroed out, and cleared in mailbox_msg_done() before invoking the callback so the slot is available for reuse by the time the callback returns.
mailbox_msg_done() is introduced as a helper that saves the callback pointer, clears busy via smp_store_release(), then invokes the callback. The release barrier ensures the loads of notify_cb and handle complete before busy is cleared. Without it, a concurrent sender could overwrite those fields after observing busy == false. mailbox_acquire_msgid() uses smp_load_acquire() on busy to pair with the release store. Callers hold a per-channel mutex, so only one sender runs at a time and no atomic slot claim is needed.
NULL notify_cb is handled inside mailbox_msg_done() so fire-and-forget sends (e.g. aie2_config_cu with no callback) are correctly tracked as in-flight until the response arrives.
mailbox_get_resp() guards against duplicate or stale firmware responses by checking busy before dispatching. A spurious response for an idle slot is logged as a warning and ignored to keep the channel alive.
xdna_mailbox_wait_ack() on timeout logs a warning and returns success. The message is already published to the ring buffer and firmware may still consume it, so returning an error would mislead callers into freeing resources that firmware can still DMA into.
The management channel slot count and async event pool size both use a static AMDXDNA_MAX_ASYNC_EVENT_BUFS of 4. The channel must be initialised before total_col is known from firmware, so the value cannot be derived from ndev. Both counts must stay equal. The channel is sized AMDXDNA_MAX_ASYNC_EVENT_BUFS + 1 (one extra for other management commands), so the pool cannot exceed that.
--- drivers/accel/amdxdna/aie2_error.c | 9 +- drivers/accel/amdxdna/aie2_message.c | 16 +- drivers/accel/amdxdna/aie2_pci.c | 10 +- drivers/accel/amdxdna/aie2_pci.h | 2 +- drivers/accel/amdxdna/aie4_pci.c | 8 +- drivers/accel/amdxdna/amdxdna_error.h | 10 + drivers/accel/amdxdna/amdxdna_mailbox.c | 224 +++++++++++++----- drivers/accel/amdxdna/amdxdna_mailbox.h | 10 +- .../accel/amdxdna/amdxdna_mailbox_helper.c | 2 +- 9 files changed, 208 insertions(+), 83 deletions(-)
drivers/accel/amdxdna/aie2_error.c +4 -5
@@ -343,15 +343,14 @@ void aie2_error_async_events_free(struct amdxdna_dev_hdl *ndev)kfree (events); } -int aie2_error_async_events_alloc (struct amdxdna_dev_hdl *ndev) +int aie2_error_async_events_alloc (struct amdxdna_dev_hdl *ndev,u32 num_events) {struct amdxdna_dev *xdna = ndev->aie.xdna; -u32 total_col = ndev->total_col; -u32 total_size =ASYNC_BUF_SIZE * total_col; +u32 total_size =ASYNC_BUF_SIZE * num_events;struct async_events *events;int i, ret; - events =kzalloc_flex (*events, event, total_col); + events =kzalloc_flex (*events, event, num_events);if (!events)return -ENOMEM ;
@@ -361,7 +360,7 @@ int aie2_error_async_events_alloc(struct amdxdna_dev_hdl *ndev) goto free_events; } events->size = total_size; - events->event_cnt = total_col; + events->event_cnt = num_events; events->wq =alloc_ordered_workqueue ("async_wq" ,0 );if (!events->wq) {
drivers/accel/amdxdna/aie2_message.c +9 -7
@@ -261,8 +261,9 @@ int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwct goto del_ctx_req; } +/* +1 for the config_cu message that can be in-flight concurrently */ ret =xdna_mailbox_start_channel (hwctx->priv->mbox_chann, &x2i, &i2x, - intr_reg, ret); + intr_reg, ret,HWCTX_MAX_CMDS +1 );if (ret) {XDNA_ERR (xdna,"Not able to create channel" ); ret = -EINVAL ;
@@ -545,7 +546,8 @@ int aie2_config_cu(struct amdxdna_hwctx *hwctx, msg.handle = hwctx; msg.opcode =MSG_OP_CONFIG_CU ; msg.notify_cb = notify_cb; -return xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); + +return xdna_mailbox_send_msg (chann, &msg,0 ); } static int aie2_init_exec_cu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
@@ -950,7 +952,7 @@ int aie2_execbuf(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,print_hex_dump_debug ("cmd: " ,DUMP_PREFIX_OFFSET ,16 ,4 , &req,0x40 , false); - ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); + ret =xdna_mailbox_send_msg (chann, &msg,0 );if (ret) {XDNA_ERR (xdna,"Send message failed" );return ret;
@@ -1039,7 +1041,7 @@ int aie2_cmdlist_multi_execbuf(struct amdxdna_hwctx *hwctx, msg.send_size =sizeof (req);print_hex_dump_debug ("cmdlist msg: " ,DUMP_PREFIX_OFFSET ,16 ,4 , &req, msg.send_size, false); - ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); + ret =xdna_mailbox_send_msg (chann, &msg,0 );if (ret) {XDNA_ERR (xdna,"Send message failed" );return ret;
@@ -1086,7 +1088,7 @@ int aie2_cmdlist_single_execbuf(struct amdxdna_hwctx *hwctx, msg.send_size =sizeof (req);print_hex_dump_debug ("cmdlist msg: " ,DUMP_PREFIX_OFFSET ,16 ,4 , &req, msg.send_size, false); - ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); + ret =xdna_mailbox_send_msg (chann, &msg,0 );if (ret) {XDNA_ERR (hwctx->client->xdna,"Send message failed" );return ret;
@@ -1122,7 +1124,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, msg.send_size =sizeof (req); msg.opcode =MSG_OP_SYNC_BO ; - ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); + ret =xdna_mailbox_send_msg (chann, &msg,0 );if (ret) {XDNA_ERR (xdna,"Send message failed" );return ret;
@@ -1157,7 +1159,7 @@ int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job * msg.send_size =sizeof (req); msg.opcode =MSG_OP_CONFIG_DEBUG_BO ; -return xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +return xdna_mailbox_send_msg (chann, &msg,0 ); } int aie2_query_app_health(struct amdxdna_dev_hdl *ndev, u32 context_id,
drivers/accel/amdxdna/aie2_pci.c +8 -2
@@ -24,6 +24,7 @@#include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" +#include "amdxdna_error.h" #include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h"
@@ -385,11 +386,16 @@ static int aie2_hw_start(struct amdxdna_dev *xdna) } xdna_mailbox_intr_reg = ndev->aie.mgmt_i2x.mb_head_ptr_reg +4 ; +/* +* At any given time, at most AMDXDNA_MAX_ASYNC_EVENT_BUFS async event +* messages plus 1 other management command can be unresponded. +*/ ret =xdna_mailbox_start_channel (ndev->aie.mgmt_chann, &ndev->aie.mgmt_x2i, &ndev->aie.mgmt_i2x, xdna_mailbox_intr_reg, - mgmt_mb_irq); + mgmt_mb_irq, +AMDXDNA_MAX_ASYNC_EVENT_BUFS +1 );if (ret) {XDNA_ERR (xdna,"failed to start management mailbox channel" ); ret = -EINVAL ;
@@ -414,7 +420,7 @@ static int aie2_hw_start(struct amdxdna_dev *xdna) goto stop_fw; } - ret =aie2_error_async_events_alloc (ndev); + ret =aie2_error_async_events_alloc (ndev,AMDXDNA_MAX_ASYNC_EVENT_BUFS );if (ret) {XDNA_ERR (xdna,"Allocate async events failed, ret %d" , ret); goto stop_fw;
drivers/accel/amdxdna/aie2_pci.h +1 -1
@@ -244,7 +244,7 @@ int aie2_pm_set_mode(struct amdxdna_dev_hdl *ndev, enum amdxdna_power_mode_typeint aie2_pm_set_dpm (struct amdxdna_dev_hdl *ndev,u32 dpm_level);/* aie2_error.c */ -int aie2_error_async_events_alloc (struct amdxdna_dev_hdl *ndev); +int aie2_error_async_events_alloc (struct amdxdna_dev_hdl *ndev,u32 num_events);void aie2_error_async_events_free (struct amdxdna_dev_hdl *ndev);int aie2_error_async_msg_thread (void *data); int aie2_get_array_async_error(struct amdxdna_dev_hdl *ndev,
drivers/accel/amdxdna/aie4_pci.c +7 -1
@@ -12,6 +12,7 @@#include "aie.h" #include "aie4_msg_priv.h" #include "aie4_pci.h" +#include "amdxdna_error.h" #include "amdxdna_mailbox.h" #include "amdxdna_mailbox_helper.h" #include "amdxdna_pci_drv.h"
@@ -181,11 +182,16 @@ static int aie4_mailbox_start(struct amdxdna_dev *xdna, goto free_channel; } +/* +* At any given time, at most AMDXDNA_MAX_ASYNC_EVENT_BUFS async event +* messages plus 1 other management command can be unresponded. +*/ ret =xdna_mailbox_start_channel (ndev->aie.mgmt_chann, &ndev->aie.mgmt_x2i, &ndev->aie.mgmt_i2x,NO_IOHUB , - mgmt_mb_irq); + mgmt_mb_irq, +AMDXDNA_MAX_ASYNC_EVENT_BUFS +1 );if (ret) {XDNA_ERR (xdna,"failed to start management mailbox channel" ); ret = -EINVAL ;
drivers/accel/amdxdna/amdxdna_error.h +10 -0
@@ -56,4 +56,14 @@ enum amdxdna_error_module { (FIELD_PREP (AMDXDNA_EXTRA_ERR_COL_MASK , col) | \FIELD_PREP (AMDXDNA_EXTRA_ERR_ROW_MASK , row)) +/* +* Maximum number of async event buffers. This value is used in two places: +* (1) to size the management mailbox channel, which must happen before +* total_col is known from firmware; (2) to allocate the async event pool, +* which runs after total_col is known. Both counts must match. The channel +* is sized AMDXDNA_MAX_ASYNC_EVENT_BUFS + 1 (one extra for other management +* commands), so the pool cannot exceed AMDXDNA_MAX_ASYNC_EVENT_BUFS slots. + */ +#define AMDXDNA_MAX_ASYNC_EVENT_BUFS 4 +#endif /* _AMDXDNA_ERROR_H_ */
drivers/accel/amdxdna/amdxdna_mailbox.c +161 -63
368 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_mailbox.h +7 -3
@@ -88,6 +88,10 @@ struct mailbox_channel *xdna_mailbox_alloc_channel(struct mailbox *mb);* @i2x: firmware to host mailbox resources * @xdna_mailbox_intr_reg: register addr of MSI-X interrupt * @mb_irq: Linux IRQ number associated with mailbox MSI-X interrupt vector index +* @n_msg: number of message slots to pre-allocate; must be non-zero for +* the PCI transport (the platform transport ignores this argument). +* Each slot is rb_size bytes. The caller must ensure at most n_msg +* messages are in flight at any time. * * Return: If success, return a handle of mailbox channel. Otherwise, return NULL. */
@@ -96,7 +100,7 @@ xdna_mailbox_start_channel(struct mailbox_channel *mb_chann,const struct xdna_mailbox_chann_res *x2i, const struct xdna_mailbox_chann_res *i2x, u32 xdna_mailbox_intr_reg, - int mb_irq); + int mb_irq, u32 n_msg);/* * xdna_mailbox_free_channel() -- free mailbox channel
@@ -117,11 +121,11 @@ void xdna_mailbox_stop_channel(struct mailbox_channel *mailbox_chann);* * @mailbox_chann: Mailbox channel handle * @msg: message struct for message information -* @tx_timeout: the timeout value for sending the message in ms. +* @tx_timeout_ms: the timeout value for sending the message in ms. * * Return: If success return 0, otherwise, return error code */ int xdna_mailbox_send_msg (struct mailbox_channel *mailbox_chann, -const struct xdna_mailbox_msg *msg,u64 tx_timeout); +const struct xdna_mailbox_msg *msg,u64 tx_timeout_ms);#endif /* _AIE_MAILBOX_ */
drivers/accel/amdxdna/amdxdna_mailbox_helper.c +1 -1
@@ -44,7 +44,7 @@ int xdna_send_msg_wait(struct amdxdna_dev *xdna, struct mailbox_channel *chann,struct xdna_notify *hdl = msg->handle;int ret; - ret =xdna_mailbox_send_msg (chann, msg,TX_TIMEOUT ); + ret =xdna_mailbox_send_msg (chann, msg,0 );if (ret) {XDNA_ERR (xdna,"Send message failed, ret %d" , ret);return ret;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] amdxdna: ABA vulnerability in msg_id accepts stale firmware responses - [High] amdxdna: management…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] amdxdna: ABA vulnerability in msg_id accepts stale firmware responses
- [High] amdxdna: management channel bottleneck causes concurrent -ENOBUFS failures
- [Medium] amdxdna: xdna_mailbox_wait_ack() misses completion due to TOCTOU on tail --
commit ea210dde67519c35a295be58a57ebd45f0948e98
Author: Max Zhen <[email protected]>
accel/amdxdna: eliminate GFP_KERNEL allocation from mailbox send path
This patch replaces the per-message dynamic allocations with a pre-allocated pool of N slots. The message ID is updated to encode the slot index directly for O(1) lookup, and the management channel size is bound statically based on the maximum number of asynchronous events plus one.
Quoted 7 lines
drivers/accel/amdxdna/aie2_pci.c +1
-0@@ -385,11 +386,16 @@ static int aie2_hw_start(struct amdxdna_dev *xdna) } xdna_mailbox_intr_reg = ndev->aie.mgmt_i2x.mb_head_ptr_reg +4 ; +/*
drivers/accel/amdxdna/aie2_pci.c static int aie2_hw_start(struct amdxdna_dev *xdna) · line 398
Quoted 10 lines
drivers/accel/amdxdna/aie2_pci.c +5
-1+* At any given time, at most AMDXDNA_MAX_ASYNC_EVENT_BUFS async event +* messages plus 1 other management command can be unresponded. +*/ ret =xdna_mailbox_start_channel (ndev->aie.mgmt_chann, &ndev->aie.mgmt_x2i, &ndev->aie.mgmt_i2x, xdna_mailbox_intr_reg, - mgmt_mb_irq); + mgmt_mb_irq, +AMDXDNA_MAX_ASYNC_EVENT_BUFS +1 );
[Severity: High]
Could this hardcoded capacity lead to spurious -ENOBUFS failures for userspace?
Since 4 of the 5 slots are permanently consumed by asynchronous event buffers waiting for firmware errors, only 1 slot is left for other management commands. If two threads execute concurrent IOCTLs that issue management commands, won't the second thread immediately fail with -ENOBUFS?
Quoted 2 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +0
-0
[ ... ]
Quoted 3 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +0
-0@@ -248,8 +297,9 @@ mailbox_get_resp(struct mailbox_channel *mb_chann, struct xdna_msg_header *heade void __iomem *data) {
[ ... ]
Quoted 12 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +4
-4- msg_id &= ~MAGIC_VAL_MASK ; - mb_msg =xa_erase_irq (&mb_chann->chan_xa, msg_id); -if (!mb_msg) { -MB_ERR (mb_chann,"Cannot find msg 0x%x" , msg_id); + slot = msg_id & ~MAGIC_VAL_MASK ; +if (unlikely (slot >= mb_chann->msg_buf_num)) { +MB_ERR (mb_chann,"Invalid msg_id 0x%x" , msg_id);return -EINVAL ; } + mb_msg =mailbox_msg_ptr (mb_chann, slot); MB_DBG(mb_chann, "opcode 0x%x size %d id 0x%x",
drivers/accel/amdxdna/amdxdna_mailbox.c mailbox_get_resp(struct mailbox_channel *mb_chann, struct xdna_msg_header *heade · line 323
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +4
-4header->opcode, header->total_size, header->id); -if (mb_msg->notify_cb) { - ret = mb_msg->notify_cb (mb_msg->handle, data, header->total_size); -if (unlikely (ret)) -MB_ERR (mb_chann,"Message callback ret %d" , ret); + +if (unlikely (!mb_msg->busy)) { +MB_WARN_ONCE (mb_chann,"Unexpected response for idle slot 0x%x" , msg_id); +return 0 ; }
[Severity: High]
Does replacing xa_alloc_cyclic_irq with a static ID using slot | MAGIC_VAL introduce an ABA vulnerability here?
If a stale or duplicate firmware response arrives after a slot has been reused for a completely new message, mb_msg->busy will be true again. Since the msg_id no longer contains a sequence number to differentiate between the old and new message, won't this allow a stale firmware response to bypass the check and invoke the wrong callback with invalid data?
[ ... ]
Quoted 7 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +3
-0@@ -398,8 +451,33 @@ static void mailbox_rx_worker(struct work_struct *rx_work) goto again; } +static int xdna_mailbox_wait_ack (struct mailbox_channel *mb_chann,u64 tx_timeout_ms) +{ +u32 tail = mb_chann->x2i_tail;
drivers/accel/amdxdna/amdxdna_mailbox.c static void mailbox_rx_worker(struct work_struct *rx_work) · line 466
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +10
-0+u32 head; +int ret; + +/* +* Poll until firmware advances the head pointer past our message, +* confirming it has consumed (acknowledged) the send. +*/ + ret =read_poll_timeout (mailbox_get_headptr, head, + head == tail,1000 , tx_timeout_ms *1000 , + false, mb_chann,CHAN_RES_X2I );
[Severity: Medium]
Can this polling loop stall for the full timeout duration due to a race condition on the tail pointer?
The tail pointer is read outside the lock and might reflect a subsequent message's tail if another thread concurrently sends a message. When the firmware processes both messages, the head pointer will advance past the expected tail.
Since the completion condition uses strict equality (head == tail), won't it permanently fail and cause the caller to stall until timeout if the head overtakes the captured tail?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Max,
drivers/accel/amdxdna/amdxdna_mailbox.c
Quoted 5 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +4
-0+#define MSG_BUF_SZ (chann) \ + (mailbox_get_ringbuf_size(chann, CHAN_RES_X2I) + \ + sizeof(struct mailbox_msg)) + mb_chann->msg_buf =kcalloc (n_msg,MSG_BUF_SZ (mb_chann),GFP_KERNEL );
rb_size comes from the firmware-provided channel info, and with this change it now sizes a host allocation as well as the ring itself. Nothing bounds it before the kcalloc(), so a bad or compromised firmware value turns directly into an arbitrarily large kernel allocation. Could rb_size be checked against the ring window it describes before the pool is allocated?
drivers/accel/amdxdna/amdxdna_mailbox.c xdna_mailbox_start_channel(struct mailbox_channel *mb_chann, · line 610
Quoted 2 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +2
-0+if (!mb_chann->msg_buf) +return -ENOMEM ;
This adds another failure path out of xdna_mailbox_start_channel() before request_irq(). On failure the caller still holds the freed channel pointer, which "accel/amdxdna: clear the mailbox channel pointer when starting it fails" (Reviewed-by: Lizhi Hou) fixes, so the two patches should be fine in either order.
Thanks,
Eva Crystal (0xiviel)
XSource Security
https://xsourcesec.com
Quoted 6 lines · Eva Crystal
Hi Max,
Quoted 3 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +3
-0+#define MSG_BUF_SZ (chann) \ + (mailbox_get_ringbuf_size(chann, CHAN_RES_X2I) + \ + sizeof(struct mailbox_msg))
Quoted 8 lines
Quoted 1 line
+
mb_chann->msg_buf=kcalloc(n_msg, MSG_BUF_SZ(mb_chann), GFP_KERNEL);
rb_sizecomes from the firmware-provided channel info, and with this change it now sizes a host allocation as well as the ring itself. Nothing bounds it before thekcalloc(), so a bad or compromised firmware value turns directly into an arbitrarily large kernel allocation. Couldrb_sizebe checked against the ring window it describes before the pool is allocated?
The firmware is authenticated and signed by AMD. By design, driver should trust this data returned from firmware. There is no need to perform this check.
Quoted 9 lines
drivers/accel/amdxdna/amdxdna_mailbox.c xdna_mailbox_start_channel(struct mailbox_channel *mb_chann, · line 610
Quoted 3 lines
drivers/accel/amdxdna/amdxdna_mailbox.c +2
-0+if (!mb_chann->msg_buf) +return -ENOMEM ;This adds another failure path out of
xdna_mailbox_start_channel()beforerequest_irq(). On failure the caller still holds the freed channel pointer, which "accel/amdxdna: clear the mailbox channel pointer when starting it fails" (Reviewed-by: Lizhi Hou) fixes, so the two patches should be fine in either order.
The patch (accel/amdxdna: clear the mailbox channel
Quoted 1 line
pointer when starting it fails) has already been applied to drm-misc-next (see https://lore.kernel.org/dri-devel/[email protected]/). My patch is based on the applied patch, so there should be no order issue.
Thanks,
Max
Quoted 6 lines
Thanks,
Eva Crystal (0xiviel)
XSource Security
https://xsourcesec.com