Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] net/ncsi: bound channel walks by a valid Channel Count
@ 2026-11-23 12:27 Sihyeon Park
  0 siblings, 0 replies; only message in thread
From: Sihyeon Park @ 2026-11-23 12:27 UTC (permalink / raw)
  To: sam, fercerpav
  Cc: netdev, linux-kernel, davem, edumazet, kuba, pabeni, horms,
	willemb, delphine_cc_chiu, Sihyeon Park

ncsi_probe_channel() and ncsi_suspend_channel() (state
ncsi_dev_state_suspend_gls) walk the channels of a package by
incrementing ndp->channel_probe_id until it equals ndp->channel_count.
channel_count is a single device-wide value, copied unchecked from the
Channel Count field of the most recent Get Capabilities response in
ncsi_rsp_handler_gc(). A controller reporting a value outside the valid
range breaks these walks:

- Channel Count 0: in the suspend_gls state channel_probe_id is already
  1 when it is first compared, so the walk never ends. The driver keeps
  sending Get Link Status to channel IDs 0, 1, 2, ..., a single
  link-down AEN never completes the reconfiguration, and NC-SI does not
  recover when the link returns. A down/up/down flap in that window also
  logs "Wrong NCSI state 0x100 in workqueue" and stops channel
  monitoring. Because the value is not reset between packages, the next
  package's probe also stops after Clear Initial State on channel 0,
  so its version, capabilities and link state are never read.

- Channel Count above NCSI_RESERVED_CHANNEL: the walks send commands to
  channel IDs past the 5-bit channel field. NCSI_TO_CHANNEL() does not
  mask the channel, so channel 31 becomes the reserved channel and
  channel 32 on package 0 goes out as package 1, channel 0. If a second
  package answers that Clear Initial State before it has been added by
  the Select Package response, ncsi_rsp_handler_cis() calls
  ncsi_add_channel() with a NULL package and the kernel oopses.

DSP0222 defines Channel Count as the number of channels the controller
supports, so a conforming controller reports a value from 1 to 31; these
are robustness fixes for non-conforming controllers, firmware bugs and
emulators (libslirp's NC-SI responder, used by QEMU user networking on
the Aspeed machines, reports 0).

Clamp the stored value to [1, NCSI_RESERVED_CHANNEL] and warn when the
controller reports something else: a controller that answered on a
channel has at least one, and channel IDs stop at 0x1e. Also terminate
both walks with ">=" instead of "==", so that a later Get Capabilities
response that lowers channel_count in the middle of a walk cannot make
it run past the new bound.

Tested on QEMU 11.1.3 romulus-bmc (aspeed_g5_defconfig; unpatched: net
tree at e203d8a4751d, patched: af6737b90668, no net/ncsi changes in
between) with a minimal initramfs and a loopback NC-SI controller
emulator with one or two packages of one channel each (pN = Channel
Count reported by package N):

  kernel     count       event   result
  unpatched  p0=33, p1=1  boot    oops: ncsi_add_channel(NULL package)
  patched    p0=33, p1=1  boot    warning; p0 and p1 probed normally
  unpatched  p0=0,  p1=1  boot    p1 channel 0 gets CIS only
  patched    p0=0,  p1=1  boot    warning; p1 gets CIS, GVI, GC, GLS
  unpatched  p0=0         A, B    no recovery (GLS to ch 0,1,2,...)
  patched    p0=0         A, B    recovers; same commands as count 1
  patched    p0=1         A, B    unchanged; ch0 GLS 58 per 60 s

(A) link-down AEN, link-up 15 s later; (B) down, up, down 0.5 s apart,
link-up 15 s later; measured 180 s after the final link-up.

(CIS: Clear Initial State, GVI: Get Version ID, GC: Get Capabilities,
GLS: Get Link Status)

An AI coding assistant (Claude Code) was used to analyze the problem,
reproduce it in QEMU and draft this patch; I reviewed the change and
the test results.

Fixes: e85e271dec02 ("net/ncsi: Fix the multi thread manner of NCSI driver")
Signed-off-by: Sihyeon Park <me@7xz.dev>
---

Notes:
    v2:
    - Clamp Channel Count to [1, NCSI_RESERVED_CHANNEL] in
      ncsi_rsp_handler_gc() (Sashiko review). With only the ">=" change, a
      count of 0 still cut the next package's probe short after CIS, and a
      count above 31 aliased into package 1 and could oops in
      ncsi_add_channel(); both are reproduced and fixed above.
    - Drop the v1 claim that Channel Count 0 behaves like 1 with the loop
      change alone; retitle accordingly.
    - Link to v1: https://lore.kernel.org/r/20261119100625.72505-1-me@7xz.dev

 net/ncsi/ncsi-manage.c |  4 ++--
 net/ncsi/ncsi-rsp.c    | 12 +++++++++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 1d63958c4429..c5cf2ca0385c 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -520,7 +520,7 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
 			goto error;
 		ndp->channel_probe_id++;
 
-		if (ndp->channel_probe_id == ndp->channel_count) {
+		if (ndp->channel_probe_id >= ndp->channel_count) {
 			ndp->channel_probe_id = 0;
 			nd->state = ncsi_dev_state_suspend_dcnt;
 		}
@@ -1491,7 +1491,7 @@ static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
 			ndp->channel_probe_id++;
 		}
 
-		if (ndp->channel_probe_id == ndp->channel_count) {
+		if (ndp->channel_probe_id >= ndp->channel_count) {
 			ndp->channel_probe_id = 0;
 			nd->state = ncsi_dev_state_probe_dp;
 		}
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
index 33921dc169e3..84cc8ac0ca2f 100644
--- a/net/ncsi/ncsi-rsp.c
+++ b/net/ncsi/ncsi-rsp.c
@@ -829,7 +829,17 @@ static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
 	 */
 	nc->vlan_filter.bitmap = U64_MAX;
 	nc->vlan_filter.n_vids = rsp->vlan_cnt;
-	np->ndp->channel_count = rsp->channel_cnt;
+	/* Channel Count bounds the per-channel walks in ncsi-manage.c, and
+	 * the channel ID is only 5 bits wide with 0x1f reserved. A controller
+	 * that answered on this channel has at least one channel, so keep
+	 * the value in [1, NCSI_RESERVED_CHANNEL].
+	 */
+	if (!rsp->channel_cnt || rsp->channel_cnt > NCSI_RESERVED_CHANNEL)
+		netdev_warn(ndp->ndev.dev,
+			    "NCSI: Package %u reports invalid Channel Count %u\n",
+			    np->id, rsp->channel_cnt);
+	ndp->channel_count = clamp_t(unsigned char, rsp->channel_cnt, 1,
+				     NCSI_RESERVED_CHANNEL);
 
 	return 0;
 }
-- 
2.56.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-23 12:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 12:27 [PATCH net v2] net/ncsi: bound channel walks by a valid Channel Count Sihyeon Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox