From: sashiko-bot@kernel.org
To: "Xin Tian" <tianx@yunsilicon.com>
Cc: linux-rdma@vger.kernel.org
Subject: Re: [PATCH v2 03/11] net/xsc: add the command queue
Date: Fri, 27 Nov 2026 14:58:06 +0000 [thread overview]
Message-ID: <sashiko-outbox-237636@kernel.org> (raw)
In-Reply-To: <20261126145718.70868-4-tianx@yunsilicon.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] net/xsc: race on cmd->ent_arr in xsc_cmd_invoke() and
xsc_cmd_comp_handler()
- [Medium] net/xsc: broken CQE owner-bit readiness check in
xsc_cmd_cq_polling()
- [Medium] net/xsc: continuous idle MMIO polling in xsc_cmd_cq_polling()
- [Medium] net/xsc: clear cmdq error status before retrying in
xsc_cmd_err_handler()
--
commit 69606c4897d6d499bb3174901050bfe9f0c8a30d
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the command queue
This commit adds the mailbox-based firmware command interface for the
Yunsilicon XSC PCI driver, including command execution, completion
polling, error handling, and command queue version verification.
> 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
[ ... ]
> +static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
> + struct xsc_cmd_msg *out, u8 *status)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_cmd_layout *lay;
> + struct xsc_cmd_work_ent *ent;
> + unsigned long timeout;
> + unsigned long flags;
> + int err;
> +
> + ent = kzalloc_obj(*ent, GFP_KERNEL);
> + if (!ent)
> + return -ENOMEM;
> +
> + ent->in = in;
> + ent->out = out;
> + init_completion(&ent->done);
> + down(&cmd->sem);
> + ent->idx = xsc_alloc_ent(cmd);
> + if (ent->idx < 0) {
> + pci_err(xdev->pdev, "failed to allocate command entry\n");
> + up(&cmd->sem);
> + kfree(ent);
> + return -ENOMEM;
> + }
> + ent->token = ent->idx + 1;
> + cmd->ent_arr[ent->idx] = ent;
> +
> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
> + lay = xsc_get_inst(cmd, cmd->cmd_pid);
> + memset(lay, 0, sizeof(*lay));
> + memcpy(lay->in, in->first, sizeof(lay->in));
> + if (in->next)
> + lay->in_ptr = cpu_to_be64(in->next->dma);
> + lay->inlen = cpu_to_be32(in->len);
> + if (out->next)
> + lay->out_ptr = cpu_to_be64(out->next->dma);
> + lay->outlen = cpu_to_be32(out->len);
> + lay->type_owner = FIELD_PREP(XSC_CMD_TYPE_MASK, XSC_PCI_CMD_XPORT);
> + lay->token = ent->token;
> + lay->idx = ent->idx;
> + lay->sig = 0xff;
> +
> + /* ring doorbell after the descriptor is valid */
> + wmb();
> +
> + cmd->cmd_pid = (cmd->cmd_pid + 1) % (1 << cmd->log_sz);
> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
> +
> + 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);
> +
> + 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));
> + 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: High]
Can concurrent callers of xsc_cmd_invoke() race with xsc_cmd_comp_handler()
on cmd->ent_arr[idx]?
xsc_cmd_comp_handler() releases idx back to cmd->cmd_entry_mask via
xsc_free_ent() before cmd->ent_arr[idx] is cleared, and xsc_cmd_invoke()
assigns cmd->ent_arr[ent->idx] = ent without holding cmd->alloc_lock:
CPU0 (cq_task) CPU1 (cmd 1) CPU2 (cmd 2)
xsc_cmd_comp_handler()
xsc_free_ent(cmd, idx)
complete(&ent1->done)
xsc_cmd_invoke()
xsc_alloc_ent() -> idx
spin_lock(&alloc_lock)
if (ent_arr[idx] == ent1)
ent_arr[idx] = NULL
spin_unlock(&alloc_lock)
kfree(ent1)
/* before ent_arr[idx]
* is written, or if
* CPU1 overwrote ent2
* with NULL: */
xsc_cmd_comp_handler()
ent = cmd->ent_arr[idx]
memcpy(ent->out->first, ...) <-- NULL pointer dereference or use-after-free
Also, if a timed-out command completes late, xsc_cmd_comp_handler() frees
its slot in cmd->cmd_entry_mask without freeing ent, ent->in, or ent->out.
Any concurrent command that already passed the cmd_status check in
_xsc_cmd_exec() can then reuse idx and overwrite cmd->ent_arr[idx], leaking
the timed-out entry and its DMA pool buffers.
[ ... ]
> +static void xsc_cmd_err_handler(struct xsc_core_device *xdev, u32 raw)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + unsigned long flags;
> + int err = 0;
> + int retry = 0;
> + u32 stat = raw;
[ ... ]
> + if (retry) {
> + if (cmd->retry_cnt >= XSC_CMD_MAX_RETRY_CNT) {
> + pci_warn(xdev->pdev, "error handler retried %d times, stop retrying\n",
> + cmd->retry_cnt);
> + } else {
> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
> + cmd->retry_cnt++;
> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
> + }
> + }
> +
> + if (err)
> + xsc_clear_cmdq_err_status(xdev, raw);
> +}
[Severity: Medium]
Should xsc_cmd_err_handler() clear the error status register via
xsc_clear_cmdq_err_status() before ringing the retry doorbell with
xsc_update_cmdq_req_pid()?
Ringing the request doorbell before the write-1-to-clear on the error
status register means the hardware either sees the retry doorbell while
the error bit is still latched, or a fast error on the retried request has
its newly latched error bit immediately cleared by
xsc_clear_cmdq_err_status(xdev, raw), causing the command to time out.
> +static int xsc_cmd_cq_polling(void *data)
> +{
> + struct xsc_core_device *xdev = data;
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_rsp_layout *rsp;
> + u32 err_status;
> + u32 cq_pid;
> +
> + while (!kthread_should_stop()) {
> + if (need_resched())
> + schedule();
> +
> + err_status = xsc_read_cmdq_err_status(xdev);
> + if (err_status)
> + xsc_cmd_err_handler(xdev, err_status);
> +
> + cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
> + if (cmd->cq_cid == cq_pid) {
> + usleep_range(1000, 2000);
> + continue;
> + }
[Severity: Medium]
Does xsc_cmd_cq_polling() need to wake up every 1-2 ms and perform two
PCIe MMIO reads when no commands are in flight?
Even with event-driven interrupts deferred, this kthread runs
continuously for the lifetime of every probed PCI function, preventing
deep CPU idle states and adding a 1-2 ms polling delay to command
completions. Parking the thread on a waitqueue when no commands are
outstanding and waking it from xsc_cmd_invoke() would avoid idle MMIO
polling and poll immediately when a command is submitted.
> +
> + 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)) {
> + dev_err_ratelimited(&xdev->pdev->dev,
> + "rsp doorbell advanced but buffer not ready %u %u\n",
> + cmd->cq_cid, cq_pid);
> + msleep(20);
> + continue;
> + }
[Severity: Medium]
Does learning cmd->owner_bit from rsp right before comparing it against
xsc_rsp_owner_bit(rsp) defeat the buffer readiness check?
On the first response (!cmd->ownerbit_learned), cmd->owner_bit is set to
xsc_rsp_owner_bit(rsp) immediately before testing
cmd->owner_bit != xsc_rsp_owner_bit(rsp), so the check cannot fail. If
the MMIO response PID advances before the CQE DMA write lands in
cmd->cq_buf (which xsc_cmd_init() zeroes with memset()), the polling
thread learns cmd->owner_bit = 0 from the unwritten slot, consumes an
all-zero CQE for index 0, and latches an inverted owner_bit if the
hardware phase bit was 1. Likewise, whenever the expected owner_bit is 0,
zero-initialized slots already match cmd->owner_bit before DMA completes.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=3
next prev parent reply other threads:[~2026-11-27 14:58 UTC|newest]
Thread overview: 22+ 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-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
2026-11-27 14:58 ` sashiko-bot [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-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-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-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
2026-11-27 14:58 ` sashiko-bot
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-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
2026-11-27 14:58 ` sashiko-bot
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-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 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=sashiko-outbox-237636@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=tianx@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