From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.27 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796547532; b=UUGuOuyzyEQyYeyJ0YRYLW/mDnla6tT+NZz83l1U4Lv1tmZ//zcpDevu1A5bm1ZU48lN 9Ufa331QC213PAR8zTVHZz5ou3Z8StNIyr3ldJD0nUdn1yzfh2SovnHKV0jQhUcQ+MuGT OXj9i5x35e0IXPvrEdjaCvv5SkkjOT1fDG/TkRmp00njih/yiFmiacrAScT8nv2nD8pcV A8mq+pBWhpiKYa7swDcOI1WASm+CWy+TqWw93VtWyHYJfXAjRwBkZokKqTvFeKJdvqQYw tnMgUMbWRNFe+volyJMaYJ7l+XGb9Kb57rfJNcJ1o1OUWg6JZ6EyJqECGEpJ7BFSGpQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796547532; h=DMARC-Filter:DKIM-Signature:From:In-Reply-To:References:User-Agent: Cc:Date:Message-Id:X-Lms-Return-Path:Content-Transfer-Encoding: Received:X-Original-From:To:Subject:Mime-Version:Content-Type; bh=WLBvzfoEdKOs5sks496xsYLNMiyz8TLxXGlAUGih72o=; b=C+P5q7ziM4fzP9YuTA+DTWUhX9GTedhTHwAq0dFIKowYkU+D0zvGBUwo3wthGxIQG9dX IeLarpXyDQRvhvvLdOeD3hQluzow/pJWxFy3DS0NA7nsAt+OPK4HAvZLstxOuL0FbgEDx PNw8KNVxomVr1aieGh3N3IXK8SvdXcRSKtCCCWjtnH4/T3KEn1sDbqKWx1bL5WHJGGrix qGoUdJJ8GNGt6W/QvWyVwLIBjPJ6qC+gpHzp3jYJaKMiHTkk4yehNafYtpQisr5dpT3Gs sJKXcIecBDm7vNJ2ChQ5LhYzdcUsF2o2RM6ZDxV/wBLAM8C/erveG8ciWtAO8M3mXCg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=yunsilicon.com; spf=pass smtp.mailfrom=yunsilicon.com; dkim=pass header.d=yunsilicon.com header.i=@yunsilicon.com header.a=rsa-sha256 header.s=feishu2403070942 header.b=MyQSl0ca; arc=none smtp.remote-ip=209.127.231.27 Received: from va-2-27.ptr.blmpb.com (va-2-27.ptr.blmpb.com [209.127.231.27]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1983606A322 for ; Sun, 6 Dec 2026 08:58:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C1983606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=yunsilicon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=yunsilicon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=yunsilicon.com header.i=@yunsilicon.com header.a=rsa-sha256 header.s=feishu2403070942 header.b=MyQSl0ca DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=feishu2403070942; d=yunsilicon.com; t=1796547524; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=WLBvzfoEdKOs5sks496xsYLNMiyz8TLxXGlAUGih72o=; b=MyQSl0cazKJ1F7AbJobDdHP0HkbzPJKDB8UwChfMBLfRTFkQ8WAeRd3K74Ca0BO/648pOq nDCQV5+Gzi70Cy1F9XDlMOd0daT8cVx58EVdwcvu1IBGlxomk+CqPn62E6w3AaVycaVNgb /vt5TSTl2b7RaAw2JhzBYgn7EI2+Y+Cf7AGvmRt+qjrMa8C+A2Ty+zKxWspG1qGnG9qAgD ovIvii+mjpyZTweq8/KlF3VOE/GUKpUmJqccV+roNOqZVJsCFr67xElxHwkCU5QvHiWgZg mG1rBjiuXo0egzDo54yM+g9FoUYIGvP8pZb9pvyBuzhxKCxtW71DERPdwYk/mg== From: "Xin Tian" In-Reply-To: <179579507193.3135.14971802624950078162@kernel.org> References: <20261126145718.70868-4-tianx@yunsilicon.com> <179579507193.3135.14971802624950078162@kernel.org> User-Agent: Mozilla Thunderbird Cc: , , , , , , , , , , , , Date: Sun, 6 Dec 2026 16:58:36 +0800 Message-Id: X-Lms-Return-Path: Content-Transfer-Encoding: quoted-printable Received: from [127.0.0.1] ([183.193.56.225]) by smtp.feishu.cn with ESMTPS; Sun, 06 Dec 2026 16:58:41 +0800 X-Original-From: Xin Tian To: Subject: Re: [PATCH v2 03/11] net/xsc: add the command queue X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 On 11/27/2026 11:57 PM, netdev-bot+sashiko@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 8 potential > issue(s) to consider. > > Critical: 0 =C2=B7 High: 2 =C2=B7 Medium: 5 =C2=B7 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 patchwor= k > know by including "pw-bot: cr" as a separate line at the end of your repl= y > (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=E2= =80=A6 > - [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=E2=80=A6 > - [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 =3D ent->idx + 1; >> + cmd->ent_arr[ent->idx] =3D ent; >> + > [ ... ] > >> + timeout =3D msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC); >> + if (!wait_for_completion_timeout(&ent->done, timeout)) { >> + cmd->cmd_status =3D 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 =3D ent->ret; >> + *status =3D ent->status; >> + >> + spin_lock_irqsave(&cmd->alloc_lock, flags); >> + if (cmd->ent_arr[ent->idx] =3D=3D ent) >> + cmd->ent_arr[ent->idx] =3D 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 i= t > calls complete(). So xsc_alloc_ent() can give the same idx to another cal= ler > 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] =3D ent store above, outside alloc_lock. That make= s > 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] =3D=3D ent1 -> true > cmd->ent_arr[0] =3D ent2; > ent_arr[0] =3D NULL > spin_unlock(alloc_lock) > > When T2's response arrives, xsc_cmd_comp_handler() sees bit 0 allocated > and does this with ent =3D=3D NULL: > > ent =3D 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 holde= rs > 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. Fixed. ent_arr[idx] is published under alloc_lock before the slot is visible to the poller, and the poller skips NULL slots. >> + >> + return err; >> +} >> + >> +static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx, >> + struct xsc_rsp_layout *rsp) >> +{ >> + struct xsc_cmd *cmd =3D xdev->cmd; >> + struct xsc_cmd_work_ent *ent; >> + >> + if (idx >=3D 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 =3D 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 rea= d > 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 =3D ent->token; > > The token is idx + 1, so even if it were checked it could not tell two us= es > 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 instanc= e. > 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 aft= er > 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? Our firmware confirms token/sig/ctrl_sig/type are unused, so those became reserved bytes and type_owner is renamed to owner (only bit 7 means anything), dropping the token plumbing. Added a NULL check on ent_arr[idx]: the poller reads it without alloc_lock and it can be NULL between xsc_alloc_ent() marking the slot busy and the publish. A duplicate response after completion is rejected by the entry-mask check. A stale response landing on a legitimately reused idx is a firmware anomaly token comparison cannot tell apart; not defended. >> + ent->ret =3D 0; >> + ent->status =3D ((struct xsc_outbox_hdr *)ent->out->first)->status; >> + cmd->retry_cnt =3D 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. I= t > 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(), f= or > example one sleeping in xsc_alloc_msg() or blocked in down(). > xsc_cmd_invoke() then overwrites the slot unconditionally: > > cmd->ent_arr[ent->idx] =3D 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. Declined. The timeout is 10s; a response that late means the firmware is faulty, so the leak chain's "late response" step is a fault event, not normal flow. On top of that a timeout does not free the slot: the entry stays in ent_arr[idx] and its bit stays allocated, so xsc_alloc_ent() cannot hand the idx out. xsc_cmd_cleanup() sweeps ent_arr before dma_pool_destroy(), so the entry and both mailboxes are freed at unload. > [ ... ] > >> +static int xsc_cmd_cq_polling(void *data) >> +{ > [ ... ] > >> + cq_pid =3D xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK; >> + if (cmd->cq_cid =3D=3D 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 erro= r > 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? Intentional. It only serves the control plane (QP/CQ/EQ setup, queries, config); the data path does not go through it, so poll latency is invisible. Single channel this version and polling is enough; event-driven completion comes with multi-channel/RDMA. >> + >> + rsp =3D xsc_get_cq_inst(cmd, cmd->cq_cid); >> + if (!cmd->ownerbit_learned) { >> + cmd->ownerbit_learned =3D 1; >> + cmd->owner_bit =3D xsc_rsp_owner_bit(rsp); >> + } >> + if (cmd->owner_bit !=3D 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 firs= t > response after init: > > - The zeroed cq_buf slot is consumed with rsp->idx =3D=3D 0 and status 0. > - The version check completes with a zeroed payload (cmdq_ver =3D=3D 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 time= s > out. Declined. The window needs three things to line up (first response of an instance, doorbell ahead of the buffer, stale slot with a different phase), and the stale expectation dies with the xsc_cmd it lives in. No closed-loop fix exists: the firmware token/sig are unusable and no way to reset the cmdq, so there is nothing to re-sync against. > [ ... ] > >> +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 =3D=3D 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 =3D XSC_CMD_STATUS_TIMEDOUT on time= out. > 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 ad= min > status, vport state and event query. > > The commit message only says "Event-driven mode is deferred". It does not > mention that timeouts are terminal. Declined. A timeout means a response never came, so which slots the firmware still holds is unknown. Re-enabling would feed new commands into that state. Leaving the interface down and requiring a rebind (function reset) is the only recovery that starts from a known-clean device. > [ ... ] > >> +int xsc_cmd_init(struct xsc_core_device *xdev) >> +{ > [ ... ] > >> + cmd->log_sz =3D XSC_CMDQ_Q_DEPTH_LOG; >> + cmd->log_stride =3D XSC_CMDQ_ELEMENT_SIZE_LOG; >> + cmd->max_reg_cmds =3D 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 on= e? > > max_reg_cmds and the semaphore both allow 32 outstanding commands. The > request and response rings have exactly 32 slots, and indices are compare= d > modulo 32 (XSC_CMDQ_INDEX_MASK). A permit is only returned after the poll= er > consumes the response. > > xsc_cmd_cq_polling() treats cq_cid =3D=3D cq_pid as empty: > > if (cmd->cq_cid =3D=3D 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 respon= ses > are skipped. Every waiter then times out, the XSC_CMD_STATUS_TIMEDOUT lat= ch > disables the interface, and the owner phase no longer matches the hardwar= e. > > 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. Woul= d > limiting outstanding commands to (1 << log_sz) - 1 avoid this? Fixed. max_reg_cmds is now (1 << log_sz) - 1, so pc and cc can never be a full ring apart; that would wrap to the same value as empty and stall the poller. > [ ... ] > >> +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 =3D 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] =3D NULL; >> + } >> + >> + dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dm= a); >> + 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 entr= y. > The device, though, still holds the ring and mailbox addresses. Nothing h= ere > 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 i= t > 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 thi= s > 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 bas= e > 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)= ; Fixed. cleanup stops the kthread, then pci_clear_master() before freeing anything. The init failure paths do the same once the ring has been programmed into the device. Thanks, Xin