From nobody Fri Sep 25 21:40:44 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1739954B1B9 for ; Tue, 8 Sep 2026 13:04:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788872660; cv=none; b=ln3gpCZ19BlF9h9VlXAYKl7Gazsm8q+oyFcvVeS86AEJlp7QtCk/BDCpJC67q52UQEewRn+AjDpQz/4IZLZ3qX0gPwnKBgWjy+7N4RYTpVdrHB7am9IY/h4D13NlMwGWzbkicJjxjmPadCiSrHy81LrR8a4JY/q8TpQkGmCqTRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788872660; c=relaxed/simple; bh=X6V7sokbB0cLy8xh2qoOtiyZEfAHNXyI//EhGCTX1U0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Pg4GZMilaJxHT27BoIj2Shpt6Ipwj9NAcal+6YephsNMotJWnJqcU5Lli9BH5YkKV5mboVoeOC1L21sAge2+lEdUeZEyi83KMfoTqg/SU5ipQxjC43Scg5JodlW0AdApj7YETeE4elSLIY4rcnMCa6chd7VrNgHCUzQoAXK3UkY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LG0F9gNm; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LG0F9gNm" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-84e84a6c4bfso3903118b3a.1 for ; Tue, 08 Sep 2026 06:04:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788872649; x=1789477449; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xeLNyhcOR1j1888DGQp/Ixbu2kpSiIXu9jd7Jj8fJWs=; b=LG0F9gNmIqtOQ+Lii76rIuhXhPaOnw//wQ5VaX4S+Oy9bkkRAkWC+FpMIf40Nw0L37 xIuGTeZVBahclxGTnN3diGLDdGKbdQPBHRX7U+pocklyXRK8enWsQM6JUmfelJqk0TCg P7/2ubY7e/tFgsxye8HnwM7e9S5aFnyF1ymVXRVR2W9b+NV4b0NLjE1KPRJ9MpaJrqLv Xu4+VUcGvTderZVA3UW3HFQS+iNKv2cKBfATxqdmjeaFlYF1GLnPf2jxuMSkExOfkpJ4 932SLEBqzOLEy3gzu9HHdy8pEr6r+voLs7CuFeBLBGr+fXw+mHqqCKO8u/TawJYhjwjb BR5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788872649; x=1789477449; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xeLNyhcOR1j1888DGQp/Ixbu2kpSiIXu9jd7Jj8fJWs=; b=r3v290TcftGNRjHAV3vY/LSZm3cPDpuTBqjIbe9jQsjQtYxXOQFrB1SCrLZb+7Q7r1 6+eTExl+a5YagawuTEegjHVRFSZXh0YjuzqOqWTEqzbs3WjP65Tj3Dx4Yy5a3Xf8Cvy4 D0kqs+1di3+Z7AE6QJ1lxuh+wj8Ey3W+5hUUGcqKqsL2w7nFvoG3ZpxKNQAmMYcnhyDy hk01/F4xD0/icYnWj6w2rGbDZ48QWnnnlhzqg+qBo+dv6kbB4kFl/cgC9AxLzcaNHB+1 SWJTUI7btOJK8cgRrnGnO6Vcb4tFl2mypXhWL2Y6DgnM9zzG0KO7CIunhhHamM+WkFEg SzzA== X-Forwarded-Encrypted: i=1; AKwUvBxAIgZob+bJLQVXjJ61N27aiqpgfDDT6ieehAy8g9WJK8Sah85fXRvWTIjCpMmLA5L6K0g3E5zG1bjDtAA=@vger.kernel.org X-Gm-Message-State: AFuF++nHKxbXATvI/GUdw/nwMQIRxbSmgkkp5WiO4XmQTnPIs+zZqxxg 7SsMejYkYZszcTCNCIq3XxX5OxWJ8bmBhzNtuoxXvc9sFt9eDN2d0mlJ X-Gm-Gg: AYBFou2jMk2TTZ5yvm/Fdx6BEgrj+P7OzvDVbhOLQE0fDjAJzHKE3YWFNFhcRq3O4wu WkXkwsyIpiiWSOvIBJKJcpZ0S0+zjw9I//ZVgw0JNtzf8tOCY5Gqu1LDVngVRvcjlOG+JcshIMQ UHUGNuGqWXWa5k47JW18M1QgjyAB1rRn0kh1oWAVIDuHZNiMEcIouJla2rmGS8h+bxYpbQQtMiN W+NoouUs1t9wxupKwkGeH2M5lLK8l6kzF9dsHusqpWO2sa19ouK06wdi95YVWgdaxM3lFSKE/wd dZwomNc6/2cCSroXKYq9jvPKknmtMiJala4YbbotLV7N+G+LE+Mkk34n9j7v3qh6nZ3EOHdrKZ1 3hCxpEhhPVh6XbI3Ja9/pTeeZ1wqblTnhjuMkPj72ZOHOCJk6oYnbiLUnklo424uhMBnNcCpMn7 kuQATihOWzkOaBJAIrzgWasRRxEOEho7I94F0UvpOwGc18vcZqdz/u7TXEJBhJpKXni7klSt70q VfBAos/ad/rgCcWXuUAhseNZi+y/A== X-Received: by 2002:a05:6a00:a1d:b0:857:73c3:446d with SMTP id d2e1a72fcca58-85fff3c57c4mr30321255b3a.28.1788872648581; Tue, 08 Sep 2026 06:04:08 -0700 (PDT) Received: from [10.10.15.228] (61-220-246-151.hinet-ip.hinet.net. [61.220.246.151]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8614f874e9csm5652398b3a.8.2026.09.08.06.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 06:04:07 -0700 (PDT) From: Potin Lai Date: Tue, 08 Sep 2026 21:01:31 +0800 Subject: [PATCH v2 1/2] net: usb: cdc_ether: add NCSI passthrough support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-ncsi-over-usb-v2-1-92dd78272fbd@gmail.com> References: <20260908-ncsi-over-usb-v2-0-92dd78272fbd@gmail.com> In-Reply-To: <20260908-ncsi-over-usb-v2-0-92dd78272fbd@gmail.com> To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Oliver Neukum , Samuel Mendoza-Jonas , Paul Fertser , Simon Horman Cc: Potin Lai , linux-usb@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Cosmo Chou , Mike Hsieh , Mik Lin , Potin Lai , Adrian Ambrozewicz X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788872639; l=9047; i=potin.lai.pt@gmail.com; s=20260522; h=from:subject:message-id; bh=qjf95BTc1sghUlnjSm3ZFIqeXFbo59xa5h6mb79MWJc=; b=yOgs2BN1lz5sbeyir6roMntH97LOb8W0VUzRxl9mFG2fkSC+tbqX5Wi3w7VFQwpl8iI1jtBVH LEWveYdU80GDDpCrY60S9c4esYuEkS9slnSQ+LFv3jC0RZdEaWguiS2 X-Developer-Key: i=potin.lai.pt@gmail.com; a=ed25519; pk=j3/nMxzz1ZPpp1revghyZ8IqOnwi6RWfuxXN2XrNMRE= From: Adrian Ambrozewicz Add NCSI (Network Controller Sideband Interface) passthrough support for USB CDC Ethernet devices. This enables BMC-to-host sideband management over USB, typically used in DPU platforms where the BMC communicates with the DPU via a dedicated USB connection. Key implementation details: - Register with NCSI subsystem in ndo_open, unregister in ndo_stop - Override netdev_ops to hook open/stop for NCSI lifecycle management - Keep carrier always on while interface is up, as NCSI control traffic shares the USB data path (unlike PHY-based drivers) - Ignore CDC status notifications since NCSI manages link state - Forward VLAN operations to NCSI subsystem The symmetric open/stop lifecycle is critical for USB drivers: open() -> ncsi_register_dev() + ncsi_start_dev() stop() -> ncsi_stop_dev() + ncsi_unregister_dev() This ensures NCSI packet handlers are removed BEFORE unregister_netdev() checks for them during USB disconnect, avoiding kernel crashes. Unlike platform drivers where unbind() runs before unregister_netdev(), USB drivers have the opposite order: usbnet_disconnect() -> unregister_netdev() -> unbind() Placing NCSI cleanup in unbind() would be too late. Supported hardware: NVIDIA DPU USB CDC Ethernet (VID:PID 0955:cf11) Signed-off-by: Adrian Ambrozewicz Signed-off-by: Potin Lai --- drivers/net/usb/Kconfig | 20 +++++ drivers/net/usb/cdc_ether.c | 187 ++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 206 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig index 52a5c0922c79..00757392128f 100644 --- a/drivers/net/usb/Kconfig +++ b/drivers/net/usb/Kconfig @@ -241,6 +241,26 @@ config USB_NET_CDCETHER IEEE 802 "local assignment" bit is set in the address, a "usbX" name is used instead. =20 +config USB_NET_CDCETHER_NCSI + bool "NCSI passthrough support for CDC Ethernet" + depends on USB_NET_CDCETHER + depends on NET_NCSI + help + This option enables NCSI (Network Controller Sideband Interface) + passthrough support for specific USB CDC Ethernet devices. + + NCSI allows a BMC (Baseboard Management Controller) to share a + network interface with the host system for out-of-band management. + This is typically used in DPU (Data Processing Unit) platforms + where the BMC communicates with the DPU via a dedicated USB + connection. + + Currently supported devices: + * NVIDIA BlueField DPU (VID:PID 0955:cf11) + + Say Y here if you have a system with USB-based NCSI connectivity + between BMC and host. If unsure, say N. + config USB_NET_CDC_EEM tristate "CDC EEM support" depends on USB_USBNET diff --git a/drivers/net/usb/cdc_ether.c b/drivers/net/usb/cdc_ether.c index a0a5740590b9..0a0eb9e0575e 100644 --- a/drivers/net/usb/cdc_ether.c +++ b/drivers/net/usb/cdc_ether.c @@ -538,6 +538,165 @@ static const struct driver_info cdc_info =3D { .manage_power =3D usbnet_manage_power, }; =20 +/* + * NCSI passthrough support for USB CDC Ethernet devices. + * + * Enables BMC-to-host sideband management over USB, typically used in + * DPU (Data Processing Unit) platforms where the BMC communicates with + * the DPU via a dedicated USB connection. + */ +#ifdef CONFIG_USB_NET_CDCETHER_NCSI +#include + +/* NCSI operates at 100 Mbps */ +#define NCSI_SPEED_BPS (100 * 1000000) + +struct cdc_ncsi_priv { + struct ncsi_dev *ndev; + struct net_device_ops netdev_ops; + const struct net_device_ops *orig_netdev_ops; +}; + +static void cdc_ncsi_handler(struct ncsi_dev *nd) +{ + if (unlikely(nd->state !=3D ncsi_dev_state_functional)) + return; + + netdev_dbg(nd->dev, "NCSI interface %s\n", + nd->link_up ? "up" : "down"); + + /* Don't toggle carrier here - it must stay on for NCSI to + * communicate over USB. Carrier was enabled in cdc_ncsi_open(). + */ +} + +static int cdc_ncsi_open(struct net_device *net) +{ + struct usbnet *dev =3D netdev_priv(net); + struct cdc_ncsi_priv *priv =3D dev->driver_priv; + int ret; + + ret =3D usbnet_open(net); + if (ret) + return ret; + + priv->ndev =3D ncsi_register_dev(net, cdc_ncsi_handler); + if (!priv->ndev) { + netdev_err(net, "failed to register NCSI device\n"); + usbnet_stop(net); + return -ENODEV; + } + + /* Carrier must stay on for NCSI to transmit/receive its control + * packets over USB. Unlike PHY-based drivers, we cannot toggle + * carrier based on NCSI link state without breaking USB I/O. + */ + netif_carrier_on(net); + ret =3D ncsi_start_dev(priv->ndev); + if (ret) { + netdev_err(net, "failed to start NCSI: %d\n", ret); + ncsi_unregister_dev(priv->ndev); + priv->ndev =3D NULL; + netif_carrier_off(net); + usbnet_stop(net); + return ret; + } + + return 0; +} + +static int cdc_ncsi_stop(struct net_device *net) +{ + struct usbnet *dev =3D netdev_priv(net); + struct cdc_ncsi_priv *priv =3D dev->driver_priv; + + if (priv->ndev) { + ncsi_stop_dev(priv->ndev); + ncsi_unregister_dev(priv->ndev); + priv->ndev =3D NULL; + } + + netif_carrier_off(net); + return usbnet_stop(net); +} + +static int cdc_ncsi_bind(struct usbnet *dev, struct usb_interface *intf) +{ + struct cdc_ncsi_priv *priv; + struct cdc_state *info; + int status; + + status =3D usbnet_ether_cdc_bind(dev, intf); + if (status < 0) + return status; + + info =3D (void *)&dev->data; + status =3D usbnet_get_ethernet_addr(dev, info->ether->iMACAddress); + if (status < 0) + goto err_unbind; + + priv =3D kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) { + status =3D -ENOMEM; + goto err_unbind; + } + + dev->driver_priv =3D priv; + + /* Override netdev_ops for NCSI lifecycle management */ + priv->orig_netdev_ops =3D dev->net->netdev_ops; + priv->netdev_ops =3D *dev->net->netdev_ops; + priv->netdev_ops.ndo_open =3D cdc_ncsi_open; + priv->netdev_ops.ndo_stop =3D cdc_ncsi_stop; + priv->netdev_ops.ndo_vlan_rx_add_vid =3D ncsi_vlan_rx_add_vid; + priv->netdev_ops.ndo_vlan_rx_kill_vid =3D ncsi_vlan_rx_kill_vid; + dev->net->netdev_ops =3D &priv->netdev_ops; + + dev->net->hw_features |=3D NETIF_F_HW_VLAN_CTAG_FILTER; + dev->net->features |=3D NETIF_F_HW_VLAN_CTAG_FILTER; + + dev->rx_speed =3D NCSI_SPEED_BPS; + dev->tx_speed =3D NCSI_SPEED_BPS; + + netdev_info(dev->net, "NCSI passthrough enabled\n"); + return 0; + +err_unbind: + usb_set_intfdata(info->data, NULL); + usb_driver_release_interface(driver_of(intf), info->data); + return status; +} + +static void cdc_ncsi_unbind(struct usbnet *dev, struct usb_interface *intf) +{ + struct cdc_ncsi_priv *priv =3D dev->driver_priv; + + if (priv) { + /* Restore original netdev_ops before freeing priv */ + dev->net->netdev_ops =3D priv->orig_netdev_ops; + kfree(priv); + dev->driver_priv =3D NULL; + } + + usbnet_cdc_unbind(dev, intf); +} + +static void cdc_ncsi_status(struct usbnet *dev, struct urb *urb) +{ + /* NCSI manages link state, ignore CDC status notifications */ +} + +static const struct driver_info cdc_ncsi_info =3D { + .description =3D "CDC Ethernet Device (NCSI)", + .flags =3D FLAG_ETHER | FLAG_POINTTOPOINT, + .bind =3D cdc_ncsi_bind, + .unbind =3D cdc_ncsi_unbind, + .status =3D cdc_ncsi_status, + .set_rx_mode =3D usbnet_cdc_update_filter, + .manage_power =3D usbnet_manage_power, +}; +#endif /* CONFIG_USB_NET_CDCETHER_NCSI */ + static const struct driver_info zte_cdc_info =3D { .description =3D "ZTE CDC Ethernet Device", .flags =3D FLAG_ETHER | FLAG_POINTTOPOINT, @@ -946,7 +1105,33 @@ static const struct usb_device_id products[] =3D { USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), .driver_info =3D (unsigned long)&wwan_info, -}, { +}, +/* + * NCSI passthrough support. + * + * This implementation enables NCSI unconditionally for matching VID/PID. + * Per-driver integration is required because the NCSI subsystem mandates + * explicit lifecycle calls (ncsi_register/start/stop/unregister_dev). + * + * OPEN QUESTION: An alternative approach using DTS "use-ncsi" property + * for conditional enablement was considered. This is viable only when + * USB topology is fixed and known at build time. Whether DPU deployments + * have fixed topologies remains to be determined. Note that DTS-based + * control would still require per-driver integration. + * + * A future generic solution could eliminate per-driver modifications by + * extending the NCSI subsystem to hook netdev lifecycle events directly, + * with interface selection configured via DTS or sysfs. + */ +#ifdef CONFIG_USB_NET_CDCETHER_NCSI +{ + USB_DEVICE_AND_INTERFACE_INFO(NVIDIA_VENDOR_ID, 0xcf11, + USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET, + USB_CDC_PROTO_NONE), + .driver_info =3D (unsigned long)&cdc_ncsi_info, +}, +#endif /* CONFIG_USB_NET_CDCETHER_NCSI */ +{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), .driver_info =3D (unsigned long) &cdc_info, --=20 2.52.0 From nobody Fri Sep 25 21:40:44 2026 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1D6C549364 for ; Tue, 8 Sep 2026 13:04:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788872660; cv=none; b=eVut8e06eR8ZyWchwy7CfddyJUA4IeeOyJmOQ3cORU7e/INvQrRf2hwo42K1Vri5XZgXhS7ibmLCCNscMeXin9NIAOS8QGHSMD3Mcsz366ASPzTNhGm2jCebJTFG3pBqCiGoJCsf06xPxrIFPLBcqX1eSpvR24cYw/ON5/CCt7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788872660; c=relaxed/simple; bh=/aCr8hgeVMYJ3KiqUNQsu2gK8nS3HDQMoxXqYTGMnYo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IuiKWuZBbM0i5LvlFmbNcBKGYJmal+R8/mJy84mXg2kBy+++BNHMijcc3ABqbB02SX8nzXr125G29v9OfhE1M2xHrcRWJCX7eOMVqRjJ45svvyOqdcafqZmCz4luNCfq+8n9fY6ddAgyD+/U2ZXX+HDCn4D9URW1SpAiTAPvyEY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jGWAbDew; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jGWAbDew" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-853c947bfefso3307819b3a.0 for ; Tue, 08 Sep 2026 06:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788872653; x=1789477453; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vu62D3vv0YYC5WBDVTSBuHunsO24noZas55Sfn0ORyc=; b=jGWAbDew/9C4ntzHTKiEA4Jfgy5TZI9/+w2iobKSmsiBuYPy7ArgOHQYUQ7dty7e+m 1SNSU2cC1lGG8h3nFVCPA6v3iZe8mNBc1gumwRATZwg1PYcXI2SqZBlbXF0GL5cjijeX qBig2Zc9j5Ya1hzjC0svme++z19I3/l8+K5yNy3d3kFobVRRWakNLOfngmMef8u2zwPS Y5m/qUqFP2Mn5eGFzlD54aUR0+/BAKg+bNAXJxc5ZRmh1yV75XFBlbZ7pgnolaONCtUE Kx4uB4uWeEGcB8GI7N5KmSkFWBMI4C05oHKtN3gKetSCYoxN7mmXoUEoD3+hkfI9cnK/ CqCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788872653; x=1789477453; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=vu62D3vv0YYC5WBDVTSBuHunsO24noZas55Sfn0ORyc=; b=R1RJz/00+iLrP+dS6Ya7Bok30mE5RC5ZUz9pUG09sNRu56n2Ue4QeRCMm9W4VAjtgA LqaqoEXSiDULKEfg1CWiBv9VRTkVfCtMs3ZXZMJgOpBUEDIF4cr7WrjzcohZxHi9gYXB k28XuIPePBwZ9jo6NoG4l1xoKUz1PExMI4focFytr/pGCR404UQEp18KM6wxhzCLMq2N wLb2hLbLO1Mv31wEcRkw+5BNcdggETBGHuthSbtWgbKAM9KWdZ3d1TMT67Jys8vDDja4 0/JS4r/uEV1V5spGtVVWzMESPnvhTnyTTFVeIe5yuEYOrS3ZylTj8sWIutSJatf+GS8s l1Ow== X-Forwarded-Encrypted: i=1; AKwUvBxxxdC/xGiYpG/aElow/zIx/rRictTNQmMLC0Zqn4XYQeghLjAFRrz81NKbE+ifHtwGDUJ539vtCCvUSEY=@vger.kernel.org X-Gm-Message-State: AFuF++kPxXFKL96a9iIwE0OTJ1RkMzLY03reWJXJFzN3oemfE8aG09FI XWz87ndajnFM967NVqFPtgTeOZAd8VEpB7DNH/ozLMqJwhhw8jLnlqjH X-Gm-Gg: AYBFou3kRjdsBsrcDVkG79gtLJdMZrJZyYdWTARXr8mNtHw8zFEKjEILaXjm6UYZ87y YObi/6JB1VlIR50q4NAo/M/jTWIgleZhBvK3Z4DI9IvVE3myZMU8FPMb0XZZdNgu64FgQDLwFiT 2TLdYVKJaxMeOuJ/rBmYSdmZZoOdhoRG3mEesAA85SaN/jx2aLCG1qAD4kRh3tmWxFdSHcvsels cb97jSgOIi6+mDhv1fZC32ja9EZXvghLDv958vVfTjFcpV5EqOruKzsF9fxL5kUoat8xNl8aOzw jJ4XQFmjJCo/3YZUdpV22oNVUhwI8hXwDfy+RX3OC2N/d7k5nOL2gSNCsvmZ3/4dUK1Qr7UWwlF +FgnHvrG04pJHNwe23cKqxrXs8ltRpZETTitVUNvujnS96yFqCJJalTG9c9kYz0V3/MXNnGGRx1 lYRtJzflb9mjzzH6rWqlyxKoZuw/OD1JPzSFyKHKFZJhj5/de5dCf0n8Oi2Ot5IXXijk3KozInr L7BXfGY32iWkqaFOTIA6p9aDyUlOg== X-Received: by 2002:a05:6a00:987:b0:857:73c3:446c with SMTP id d2e1a72fcca58-85fff3c461fmr31073167b3a.27.1788872652526; Tue, 08 Sep 2026 06:04:12 -0700 (PDT) Received: from [10.10.15.228] (61-220-246-151.hinet-ip.hinet.net. [61.220.246.151]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8614f874e9csm5652398b3a.8.2026.09.08.06.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 06:04:12 -0700 (PDT) From: Potin Lai Date: Tue, 08 Sep 2026 21:01:32 +0800 Subject: [PATCH v2 2/2] net/ncsi: fix use-after-free in ncsi_unregister_dev() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-ncsi-over-usb-v2-2-92dd78272fbd@gmail.com> References: <20260908-ncsi-over-usb-v2-0-92dd78272fbd@gmail.com> In-Reply-To: <20260908-ncsi-over-usb-v2-0-92dd78272fbd@gmail.com> To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Oliver Neukum , Samuel Mendoza-Jonas , Paul Fertser , Simon Horman Cc: Potin Lai , linux-usb@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Cosmo Chou , Mike Hsieh , Mik Lin , Potin Lai , Adrian Ambrozewicz X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788872639; l=3964; i=potin.lai.pt@gmail.com; s=20260522; h=from:subject:message-id; bh=bKp+fXghJ6cQJaZb9/dKdTBINAk3RYvGYETZo/GwPjY=; b=zdJASea/GkvdKgFd9ycSXVpHjAiOck4Qf6FmC5wriN7PkcPVxeK0Of7uZPoxbxIv9lDiuHaMb bpOB7UgUOsMBEZNJ8JbUC7iTw3mBtga44fuYe9Mf5IH7tK/XUP9JmwV X-Developer-Key: i=potin.lai.pt@gmail.com; a=ed25519; pk=j3/nMxzz1ZPpp1revghyZ8IqOnwi6RWfuxXN2XrNMRE= From: Adrian Ambrozewicz ncsi_unregister_dev() frees the ncsi_dev_priv structure while timers and workqueue may still be accessing it, causing use-after-free. The problem involves two async mechanisms: 1. Request timers (ncsi_request_timeout) - fire when NCSI responses are not received in time 2. Workqueue (ncsi_dev_work) - processes NCSI state machine These can cascade: timer handlers call ncsi_free_request() which may call schedule_work(), and work can send commands that arm new timers. The fix adds proper synchronization before kfree(): dev_remove_pack() - stop packet reception timer_delete_sync() x 256 - cancel all request timers cancel_work_sync() - wait for workqueue to complete kfree(ndp) Order matters: timers must be cancelled before work because timer handlers may schedule new work via ncsi_free_request(). Note: ncsi_dev_work() is non-blocking - it sends a command, arms a timer, and returns immediately. It does not wait for timer completion. The timer firing later triggers schedule_work() for the next state. So cancel_work_sync() will not hang waiting for cancelled timers. This relies on ncsi_stop_dev() being called first (guaranteed by the network device lifecycle). ncsi_stop_dev() sets state to ncsi_dev_state_functional, which causes ncsi_dev_work() to exit immediately without sending commands or arming timers. This breaks the timer<->work cycle and ensures the synchronization terminates. Timeline showing the race (without fix): CPU 0 (unregister) CPU 1 (async) ------------------ ------------- ncsi_unregister_dev() dev_remove_pack() ncsi_request_timeout() ncsi_free_request() schedule_work() kfree(ndp) ncsi_dev_work() ndp->... <- UAF! With fix: CPU 0 (unregister) CPU 1 (async) ------------------ ------------- ncsi_unregister_dev() dev_remove_pack() timer_delete_sync() x 256 <- waits for timer handlers ncsi_request_timeout() ncsi_free_request() schedule_work() cancel_work_sync() <- waits for work ncsi_dev_work() state=3D0x100, exits immediately kfree(ndp) <- safe Signed-off-by: Adrian Ambrozewicz Signed-off-by: Potin Lai --- net/ncsi/ncsi-manage.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c index 54d0df0a9efe..15d7e4cabf4f 100644 --- a/net/ncsi/ncsi-manage.c +++ b/net/ncsi/ncsi-manage.c @@ -1957,9 +1957,28 @@ void ncsi_unregister_dev(struct ncsi_dev *nd) struct ncsi_dev_priv *ndp =3D TO_NCSI_DEV_PRIV(nd); struct ncsi_package *np, *tmp; unsigned long flags; + int i; =20 dev_remove_pack(&ndp->ptype); =20 + /* + * Synchronize with async operations before freeing ndp. + * + * Note: The caller must have called ncsi_stop_dev() first, which + * sets nd->state to ncsi_dev_state_functional (0x100). This causes + * any running or scheduled ncsi_dev_work() to exit immediately + * without sending commands or arming new timers, breaking the + * potential cycle of: work -> arm timer -> timer -> schedule work. + * + * Order matters: + * 1. timer_delete_sync() - cancel timers, handlers may schedule work + * 2. cancel_work_sync() - cancel work scheduled by timer handlers + */ + for (i =3D 0; i < ARRAY_SIZE(ndp->requests); i++) + timer_delete_sync(&ndp->requests[i].timer); + + cancel_work_sync(&ndp->work); + list_for_each_entry_safe(np, tmp, &ndp->packages, node) ncsi_remove_package(np); =20 --=20 2.52.0