From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796130311; b=3kNhrWGrt0cSCoAzg6E2HYTf97iIhZcEsGq/uyMwWx//5X5uJaPzKT9nGAZjuuoLrotV aVQHc+g+DzdffD0lkIhtn82zl7V5Oguc7AZJIjCjzGVF5EUQrrJxxNOnO2mikJDQOHrpX y8Pw+BPxszepne8HWZj2QAxVxLZWD443oDGC4Ngf5nu0uMCZ7RscMR86H/CzR6Q4FsL5q BDeIf8ss+XG9qOrAOpKEa3qf6ipQEdQJDOK/pjqtPiL8OmbcXevsYXYS2uPqZzUbtcBwR t8XwOL+p8OCw6DQFIuKa+80GqTiulr7IGkYuGDPteXXFnScHrvwJaLQCRpUIZMFa7Dg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796130311; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=3knVTsXYEbx7G/jhCJwhsOB5x/D85/vCyPiqcyT7AQM=; b=xwyl9wlk2iMI1YmIhjMh1d7u6KOveS75pl8vfz1AVtDt9kWbTAOaXW8tc2XomHzfmxYS aNasIAdjfjNMSL8TqS5KJ5ovv3Z1SWwgHXGX/vYFLwQgF6M01/WIXQSb6kKxwQs1VGnkm 6CAGtm4hlDZLqmsrzIUqWhqqedBk3tVltEAaEGeY4/4xSpiwOVH7LoWrpksztEmn6M981 eR80QPH41SyrfNAZLaZ4/vvq4ABvsbgNm+Cd+35fpf+NU17c//aiT6yec4yaHLeN32ehs IbevKjUjgffVjdboFHYlBuhR0Mo8cvO/Pb3m9EFa5mmmul1ePvDbfmPs4/2ZeQ2qSlw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=Q7KbRE9+; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BB2A606A32A; Tue, 1 Dec 2026 13:05:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8BB2A606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=Q7KbRE9+ Received: from pengutronix.de (p20030102af17cb00c4330b233ae7cbb8.dip0.t-ipconnect.de [IPv6:2003:102:af17:cb00:c433:b23:3ae7:cbb8]) (Authenticated sender: mkl@pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id C17DA20284F; Tue, 01 Dec 2026 14:05:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796130304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3knVTsXYEbx7G/jhCJwhsOB5x/D85/vCyPiqcyT7AQM=; b=Q7KbRE9+l70Otp8WT62y9ClpfhWgQbA+VDM6Z4kUjz79Qx1cSJ3muSagcvUjLeWZ/FMUd7 6ebShizwJLM+azhTMphX7yAQcSJIpUEmARB28fIEBzBE4KXu2ZkDc9yO4weDCEK0pF+ZqL m1YpPWrYKH52hPHlkK2bVQ5JEDJRkIk9Fjvee3wm9MYv4KKE4MEA2UywuYw7Fe9nOf55+T flK1L6bp5HidzNmTISpIMmHccslp04N6/LfSBIZHqteEw9ccCBTVTUBbi+tahm+NyBtX4G VzPUzkGZ0zv+AyF3p0xDXMcbFoIb8bSZdgVQUI5inM3X+TTy3e7mygZriMtzJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796130304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3knVTsXYEbx7G/jhCJwhsOB5x/D85/vCyPiqcyT7AQM=; b=PDeQBgm2Eht5mAP4gXlOEJ7IzMVPEWgasE2iiD8fuqy4VfqGkNvYHLjMXKvZ8aDvcT3elL BqQSegKyFFxfGTQx/VAryK3XyCiTeasQKhxM6vzhQE8aoHTDC+nf1DMbFc+mXvEWdOZ2cx tFpxWrRbHJ3rR6QSP5fnPWly4ngjBfYLmhyKMRQtEEmM9V0uqJCMFA76z2lVnWRJv7OCnm pqUPsD42gxwKo9j8oPkJNlAbOJCD+WZZmtdKtYHObTOSQ7ActnJsqyRhKG44XeVE47Dthl 30FMYOf5Yj3s07G+w2foSU+x5UMWRZNmG8wV9gGd83v+EUMKgIVLCaEyUKMBAw== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796130304; a=rsa-sha256; cv=none; b=DU2EhmwxGRIMX8Kh8C2q4vkA50XIz4Ph/EnyIWMFMuj7DwIaCMPQCV2QY4CsNEYbFzAkWS l+TBSUgtco2NR3oO4om/DUziACvvuUCiVKD7IP84Czaxi12GFpWCcBKFjsaGA+op5bvfiX CML0Xk7oAb+CW8csoNA7gLlngTga31TZQqJu37ZCWSqmTHEKwuAlEmNBYzn1HdR9xH+N30 SKuawhySM3L5B3KmmqWpH1R+zfDVgixwv6U2e1kqmAY7OT/l5mbyz7jHVb7i0E928s3PNE 3vDyrI5ekRMvg196LCj6w/iM2nCQjcoK/EPezD4oWmUEqv0fNSvohRnHMI+s/Q== From: Marc Kleine-Budde To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, Vincent Mailhol , Marc Kleine-Budde , stable@vger.kernel.org Subject: [PATCH net-next 1/9] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Date: Tue, 1 Dec 2026 14:02:55 +0100 Message-ID: <20261201130450.2195418-2-mkl@pengutronix.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201130450.2195418-1-mkl@pengutronix.de> References: <20261201130450.2195418-1-mkl@pengutronix.de> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In gs_usb_probe(), gs_make_candev() is called to allocate and set up the CAN channel, as well as to call register_candev(). However, gs_usb_probe() defers the assignment of dev->parent and parent->hf_size_rx until after gs_make_candev() returns. This opens a race window for the user space to bring up the CAN interface, while it's not completely setup. To close the race window, initialize all CAN interfaces in a first loop and then register them in a second loop: Don't call register_candev() from gs_make_candev() but explicitly from gs_usb_probe(). Adjust gs_destroy_candev() likewise, move unregister_candev() to gs_usb_disconnect() and the error paths in gs_usb_probe(), to keep it inverse to symmetric to gs_make_candev(). While moving the error message, use "%u" instead of "%d" to print the channel number, which is an unsigned int. However, there is still a small time window. Between the registration of the first and the second channel, the first channel is already visible to the user space and can be activated. If the device sends a CAN frame for an unregistered channel, the check by `!netif_device_present()` triggers, and the URB is not re-submitted and is therefore lost. Instead of returning early, re-submit the URB. This should be no problem during shutdown, since during usb_kill_anchored_urbs() an usb_submit_urb() will fail. Closes: https://sashiko.dev/#/bug/linux-be54de7d-0b75-408c-b3ee-dd90c9756857 Closes: https://sashiko.dev/#/bug/linux-71076d59-0843-4749-a5ab-fdd98a08582f Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices") Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261201-gs_usb-fixes-v13-1-f28c5cf468da@pengutronix.de Signed-off-by: Marc Kleine-Budde --- drivers/net/can/usb/gs_usb.c | 66 ++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c index 15f28833f2de..dae4e07e8519 100644 --- a/drivers/net/can/usb/gs_usb.c +++ b/drivers/net/can/usb/gs_usb.c @@ -652,7 +652,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb) stats = &netdev->stats; if (!netif_device_present(netdev)) - return; + goto resubmit_urb; if (!netif_running(netdev)) goto resubmit_urb; @@ -1490,18 +1490,8 @@ static struct gs_can *gs_make_candev(unsigned int channel, can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT); SET_NETDEV_DEV(netdev, &intf->dev); - rc = register_candev(dev->netdev); - if (rc) { - dev_err(&intf->dev, - "Couldn't register candev for channel %d (%pe)\n", - channel, ERR_PTR(rc)); - goto out_can_rx_offload_del; - } - return dev; -out_can_rx_offload_del: - can_rx_offload_del(&dev->offload); out_free_candev: free_candev(dev->netdev); return ERR_PTR(rc); @@ -1509,7 +1499,6 @@ static struct gs_can *gs_make_candev(unsigned int channel, static void gs_destroy_candev(struct gs_can *dev) { - unregister_candev(dev->netdev); can_rx_offload_del(&dev->offload); free_candev(dev->netdev); } @@ -1587,21 +1576,16 @@ static int gs_usb_probe(struct usb_interface *intf, for (i = 0; i < icount; i++) { unsigned int hf_size_rx = 0; + struct gs_can *dev; - parent->canch[i] = gs_make_candev(i, intf, &dconf); - if (IS_ERR_OR_NULL(parent->canch[i])) { + dev = gs_make_candev(i, intf, &dconf); + if (IS_ERR_OR_NULL(dev)) { /* save error code to return later */ - rc = PTR_ERR(parent->canch[i]); + rc = PTR_ERR(dev); - /* on failure destroy previously created candevs */ - icount = i; - for (i = 0; i < icount; i++) - gs_destroy_candev(parent->canch[i]); - - usb_kill_anchored_urbs(&parent->rx_submitted); - kfree(parent); - return rc; + goto out_gs_destroy_candev; } + parent->canch[i] = dev; parent->canch[i]->parent = parent; /* set RX packet size based on FD and if hardware @@ -1621,7 +1605,37 @@ static int gs_usb_probe(struct usb_interface *intf, parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx); } + for (i = 0; i < icount; i++) { + struct gs_can *dev = parent->canch[i]; + + rc = register_candev(dev->netdev); + if (rc) { + const unsigned int cur = i; + + dev_err(&intf->dev, + "Couldn't register candev for channel %u (%pe)\n", + i, ERR_PTR(rc)); + + for (i = 0; i < cur; i++) + unregister_candev(parent->canch[i]->netdev); + + goto out_gs_destroy_candev; + } + } + return 0; + +out_gs_destroy_candev: + for (i = 0; i < parent->channel_cnt; i++) { + if (!parent->canch[i]) + continue; + + gs_destroy_candev(parent->canch[i]); + } + + kfree(parent); + + return rc; } static void gs_usb_disconnect(struct usb_interface *intf) @@ -1637,8 +1651,10 @@ static void gs_usb_disconnect(struct usb_interface *intf) } for (i = 0; i < parent->channel_cnt; i++) - if (parent->canch[i]) - gs_destroy_candev(parent->canch[i]); + unregister_candev(parent->canch[i]->netdev); + + for (i = 0; i < parent->channel_cnt; i++) + gs_destroy_candev(parent->canch[i]); kfree(parent); } base-commit: d7f7fac1c3220a430559c6d43904e27917ae1eb0 -- 2.53.0