From: netdev-bot+sashiko@kernel.org
To: tianx@yunsilicon.com
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, jgg@ziepe.ca,
leon@kernel.org, huangjunxian6@hisilicon.com,
weihg@yunsilicon.com, linux-kernel@vger.kernel.org,
netdev@vger.kernel.org, linux-rdma@vger.kernel.org,
kerneljasonxing@gmail.com
Subject: Re: [PATCH v2 03/11] net/xsc: add the command queue
Date: Fri, 27 Nov 2026 15:57:51 +0000 [thread overview]
Message-ID: <179579507193.3135.14971802624950078162@kernel.org> (raw)
In-Reply-To: <20261126145718.70868-4-tianx@yunsilicon.com>
Thank you for your contribution! Sashiko AI review found 8 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 5 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] Race on cmd->ent_arr[idx] can leave a live command with a NULL
slot, and the polling kthread then dereferences NULL.
- [High] xsc_cmd_cleanup() frees the request ring (cmd_buf), the response
ring (cq_buf), the dma_pool and the mailboxes of timed-out commands…
- [Medium] One command timeout permanently disables the command
interface.
- [Medium] A timed-out command's xsc_cmd_work_ent and its inb/outb
mailboxes can leak permanently.
- [Medium] xsc_cmd_comp_handler() matches a response to a waiter using
only the device-supplied rsp->idx.
- [Medium] The ring is sized so that a full ring cannot be told apart
from an empty one.
- [Medium] xsc_cmd_cq_polling() learns the expected owner/phase bit from
the slot it is about to validate (cmd.c:450-452: `if…
- [Low] The command-queue poller is one kthread per device, and it polls
even when no command is outstanding.
--
net/xsc: add the command queue
This commit adds the mailbox-based firmware command interface for the xsc
driver. A single polling kthread collects responses, command status is
mapped to errno values, and the cmdq version is checked at probe time.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> new file mode 100644
> index 0000000000000..45dcdb2b47eb0
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> @@ -0,0 +1,708 @@
[ ... ]
> +static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
> + struct xsc_cmd_msg *out, u8 *status)
> +{
[ ... ]
> + ent->token = ent->idx + 1;
> + cmd->ent_arr[ent->idx] = ent;
> +
[ ... ]
> + timeout = msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC);
> + if (!wait_for_completion_timeout(&ent->done, timeout)) {
> + cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
> + pci_err(xdev->pdev, "wait for opcode 0x%x response timeout\n",
> + xsc_msg_to_opcode(in));
> + /* the entry stays registered for the completion handler */
> + return -ETIMEDOUT;
> + }
> +
> + err = ent->ret;
> + *status = ent->status;
> +
> + spin_lock_irqsave(&cmd->alloc_lock, flags);
> + if (cmd->ent_arr[ent->idx] == ent)
> + cmd->ent_arr[ent->idx] = NULL;
> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
> + kfree(ent);
[Severity: High]
Can a live command end up with a NULL slot here?
xsc_cmd_comp_handler() releases the slot bit with xsc_free_ent() before it
calls complete(). So xsc_alloc_ent() can give the same idx to another caller
while this waiter still has its pointer in ent_arr[]. find_first_bit()
always picks the lowest free bit, so reusing the same idx is the normal
case.
The new owner publishes its entry with the plain
cmd->ent_arr[ent->idx] = ent store above, outside alloc_lock. That makes
this compare-and-clear atomic only against other locked accessors:
T1 (old waiter, idx 0) T2 (new owner of idx 0)
spin_lock(alloc_lock)
ent_arr[0] == ent1 -> true
cmd->ent_arr[0] = ent2;
ent_arr[0] = NULL
spin_unlock(alloc_lock)
When T2's response arrives, xsc_cmd_comp_handler() sees bit 0 allocated
and does this with ent == NULL:
ent = cmd->ent_arr[idx];
memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
Wouldn't that oops the polling kthread? Every later command would then
time out.
If T2 times out instead, xsc_cmd_cleanup() can no longer reach its entry
and mailboxes.
The window is narrow. On PREEMPT_RT it is wider, because spinlock_t holders
can be preempted. In this patch only the serial probe-time version check
runs. However, the semaphore and bitmap allow 32 concurrent commands, and
later patches in the series add concurrent xsc_cmd_exec() callers.
> +
> + return err;
> +}
> +
> +static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx,
> + struct xsc_rsp_layout *rsp)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_cmd_work_ent *ent;
> +
> + if (idx >= cmd->max_reg_cmds || (cmd->cmd_entry_mask & BIT(idx))) {
> + pci_err(xdev->pdev, "idx[%d] exceeds max cmds or has no request\n",
> + idx);
> + return;
> + }
> + ent = cmd->ent_arr[idx];
> +
> + memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
[Severity: Medium]
Can a stale response be given to the wrong command here?
The only matching is on the device-supplied rsp->idx, plus a lockless read
of cmd_entry_mask. ent_arr[idx] is not checked for NULL.
rsp->token is never compared with ent->token, even though xsc_cmd_invoke()
writes the token into the request:
lay->token = ent->token;
The token is idx + 1, so even if it were checked it could not tell two uses
of the same slot apart.
A late response can come from a command that timed out in this instance,
or from a request the firmware still holds from a previous driver instance.
Either one would complete whichever command now owns that idx.
xsc_cmd_handle_rsp_before_reload() only skips responses that were already
posted at load time. Allocation starts at bit 0, so the version check is the
likely victim.
There is also a duplicate-response case. Suppose the response arrives after
the idx has been re-allocated, but while ent_arr[idx] still points at the
previous entry that its waiter is freeing. Could the memcpy() into
ent->out->first and complete(&ent->done) then write into freed memory?
> + ent->ret = 0;
> + ent->status = ((struct xsc_outbox_hdr *)ent->out->first)->status;
> + cmd->retry_cnt = 0;
> +
> + xsc_free_ent(cmd, ent->idx);
> + complete(&ent->done);
> + up(&cmd->sem);
> +}
[Severity: Medium]
Does this leak the timed-out command's xsc_cmd_work_ent and its inb/outb
mailboxes?
After a timeout, entA stays in cmd->ent_arr[X] and owns both mailboxes
("the command owns both mailboxes now" in _xsc_cmd_exec()). When the late
response arrives, this path frees bit X and returns a semaphore permit. It
does not clear ent_arr[X] or free entA.
The timeout check at the top of _xsc_cmd_exec() is a plain lockless read. A
caller that already passed it can still get idx X from xsc_alloc_ent(), for
example one sleeping in xsc_alloc_msg() or blocked in down().
xsc_cmd_invoke() then overwrites the slot unconditionally:
cmd->ent_arr[ent->idx] = ent;
That command NULLs the slot when it finishes. From then on, entA, its two
xsc_cmd_msg chains and their dma_pool blocks are unreachable.
xsc_cmd_cleanup() cannot free them, and dma_pool_destroy() would report a
busy pool.
[ ... ]
> +static int xsc_cmd_cq_polling(void *data)
> +{
[ ... ]
> + cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
> + if (cmd->cq_cid == cq_pid) {
> + usleep_range(1000, 2000);
> + continue;
> + }
[Severity: Low]
This isn't a bug, but this loop keeps polling when no command is
outstanding. Each idle iteration does two non-posted MMIO reads (the error
status and RSP_PID), then usleep_range(1000, 2000).
That is roughly 500-1000 wakeups and 1000-2000 PCIe reads per second for
each bound device, for as long as it stays bound. In this patch, commands
are only issued at probe.
The commit message mentions "a single polling thread" but not this idle
cost. Could the thread poll only while commands are outstanding, or back
off when idle?
> +
> + rsp = xsc_get_cq_inst(cmd, cmd->cq_cid);
> + if (!cmd->ownerbit_learned) {
> + cmd->ownerbit_learned = 1;
> + cmd->owner_bit = xsc_rsp_owner_bit(rsp);
> + }
> + if (cmd->owner_bit != xsc_rsp_owner_bit(rsp)) {
[Severity: Medium]
The expected owner bit is learned from the same slot being validated, so the
readiness check always passes for the first response. Is that safe?
The "rsp doorbell advanced but buffer not ready" branch suggests RSP_PID can
become visible before the slot contents. Suppose that happens on the first
response after init:
- The zeroed cq_buf slot is consumed with rsp->idx == 0 and status 0.
- The version check completes with a zeroed payload (cmdq_ver == 0), so
probe fails.
- owner_bit is learned as 0.
The queue is never reset, so the hardware phase carries over from earlier
driver instances. If that phase is 1, every later slot fails the owner
check. The poller then loops on msleep(20) forever and every command times
out.
[ ... ]
> +static int _xsc_cmd_exec(struct xsc_core_device *xdev,
> + void *in, unsigned int in_size,
> + void *out, unsigned int out_size)
> +{
[ ... ]
> + if (cmd->cmd_status == XSC_CMD_STATUS_TIMEDOUT)
> + return -ETIMEDOUT;
[Severity: Medium]
Is it intended that one timeout disables the command interface for good?
xsc_cmd_invoke() sets cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT on timeout.
Nothing outside xsc_cmd_init() ever sets it back to XSC_CMD_STATUS_NORMAL.
That holds even when the late response arrives and xsc_cmd_comp_handler()
frees the slot and calls up(&cmd->sem).
After one firmware stall longer than XSC_CMD_TIMEOUT_MSEC, every later
xsc_cmd_exec() returns -ETIMEDOUT until the device is unbound and rebound.
Later in the series this covers CQ/QP/EQ create and destroy, MTU, port admin
status, vport state and event query.
The commit message only says "Event-driven mode is deferred". It does not
mention that timeouts are terminal.
[ ... ]
> +int xsc_cmd_init(struct xsc_core_device *xdev)
> +{
[ ... ]
> + cmd->log_sz = XSC_CMDQ_Q_DEPTH_LOG;
> + cmd->log_stride = XSC_CMDQ_ELEMENT_SIZE_LOG;
> + cmd->max_reg_cmds = 1 << cmd->log_sz;
> + bitmap_fill(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
> +
> + spin_lock_init(&cmd->alloc_lock);
> + spin_lock_init(&cmd->doorbell_lock);
> + sema_init(&cmd->sem, cmd->max_reg_cmds);
[Severity: Medium]
With this sizing, can a full response ring be told apart from an empty one?
max_reg_cmds and the semaphore both allow 32 outstanding commands. The
request and response rings have exactly 32 slots, and indices are compared
modulo 32 (XSC_CMDQ_INDEX_MASK). A permit is only returned after the poller
consumes the response.
xsc_cmd_cq_polling() treats cq_cid == cq_pid as empty:
if (cmd->cq_cid == cq_pid) {
usleep_range(1000, 2000);
continue;
}
Suppose the device posts 32 responses while the poller is in
usleep_range(). The masked RSP_PID wraps back to cq_cid, so all 32 responses
are skipped. Every waiter then times out, the XSC_CMD_STATUS_TIMEDOUT latch
disables the interface, and the owner phase no longer matches the hardware.
The request side has the same issue. xsc_cmd_invoke() never compares cmd_pid
with the hardware consumer index, so a 32nd unconsumed request also makes
REQ_PID equal to the consumer index.
The code does not show whether the hardware posts into all 32 slots. Would
limiting outstanding commands to (1 << log_sz) - 1 avoid this?
[ ... ]
> +void xsc_cmd_cleanup(struct xsc_core_device *xdev)
> +{
[ ... ]
> + /* a timed-out command stays registered for a response that may never
> + * come; with the polling thread stopped nothing can touch it anymore
> + */
> + for (i = 0; i < cmd->max_reg_cmds; i++) {
> + if (!cmd->ent_arr[i])
> + continue;
> + xsc_free_msg(xdev, cmd->ent_arr[i]->in);
> + xsc_free_msg(xdev, cmd->ent_arr[i]->out);
> + kfree(cmd->ent_arr[i]);
> + cmd->ent_arr[i] = NULL;
> + }
> +
> + dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
> + dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cmd_buf, cmd->dma);
> + dma_pool_destroy(cmd->pool);
[Severity: High]
Can the device still DMA into these buffers after they are freed?
With the polling thread stopped, the host will not touch a timed-out entry.
The device, though, still holds the ring and mailbox addresses. Nothing here
does any of the following:
- reset the queue
- clear the CMDQM_HOST_REQ/RSP_BUF_BASE registers
- drain outstanding commands
Bus mastering is also still enabled, because xsc_pci_fini() only clears it
afterwards:
xsc_cmd_cleanup(xdev);
pci_iounmap(pdev, xdev->bar);
pci_clear_master(pdev);
The xsc_pci_init() unwind uses the same order. The probe path reaches this
directly when xsc_cmd_version_check() times out and jumps to
err_cmd_cleanup.
On rebind, pci_set_master() runs before xsc_cmd_init() reprograms the base
addresses. xsc_cmd_handle_rsp_before_reload() also writes RSP_CID before
that. A response the firmware still holds could then be written into the
previous instance's freed, and possibly reused, pages.
The kthread_create() failure path in xsc_cmd_init() has the same problem,
because the rings were already programmed into the device:
err_free_cq:
dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
next prev parent reply other threads:[~2026-11-27 15:57 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko [this message]
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-27 14:58 ` [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver 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=179579507193.3135.14971802624950078162@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=huangjunxian6@hisilicon.com \
--cc=jgg@ziepe.ca \
--cc=kerneljasonxing@gmail.com \
--cc=kuba@kernel.org \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=tianx@yunsilicon.com \
--cc=weihg@yunsilicon.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