Netdev List
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
	kernel@pengutronix.de, Marc Kleine-Budde <mkl@pengutronix.de>,
	stable@vger.kernel.org
Subject: [PATCH net-next 2/9] can: gs_usb: gs_usb_receive_bulk_callback(): update state even if SKB allocations fails
Date: Mon, 30 Nov 2026 10:15:28 +0100	[thread overview]
Message-ID: <20261130092003.1946901-3-mkl@pengutronix.de> (raw)
In-Reply-To: <20261130092003.1946901-1-mkl@pengutronix.de>

If the device sends a CAN error frame and the SKB allocation fails, the CAN
state is not updated.

To avoid losing CAN state updates, analyze CAN error frames before
allocating the skb. Update gs_update_state() to work on a struct
gs_host_frame instead of a struct can_frame. Check length of received
struct gs_host_frame::data before accessing it.

Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices")
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261130-gs_usb-fixes-v12-2-9ba1f84c179a@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/usb/gs_usb.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 965151ab50a1..996029853235 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -495,23 +495,24 @@ static void gs_usb_timestamp_stop(struct gs_usb *parent)
 	cancel_delayed_work_sync(&parent->timestamp);
 }
 
-static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
+static void gs_update_state(struct gs_can *dev, const struct gs_host_frame *hf,
+			    unsigned int data_length)
 {
 	struct can_device_stats *can_stats = &dev->can.can_stats;
 
-	if (cf->can_id & CAN_ERR_RESTARTED) {
+	if (hf->can_id & cpu_to_le32(CAN_ERR_RESTARTED)) {
 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
 		can_stats->restarts++;
-	} else if (cf->can_id & CAN_ERR_BUSOFF) {
+	} else if (hf->can_id & cpu_to_le32(CAN_ERR_BUSOFF)) {
 		dev->can.state = CAN_STATE_BUS_OFF;
 		can_stats->bus_off++;
-	} else if (cf->can_id & CAN_ERR_CRTL) {
-		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
-		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
+	} else if (hf->can_id & cpu_to_le32(CAN_ERR_CRTL) && data_length > 1) {
+		if ((hf->classic_can->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
+		    (hf->classic_can->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
 			dev->can.state = CAN_STATE_ERROR_WARNING;
 			can_stats->error_warning++;
-		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
-			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
+		} else if ((hf->classic_can->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
+			   (hf->classic_can->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
 			can_stats->error_passive++;
 		} else {
@@ -685,6 +686,10 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
 
 			memcpy(cfd->data, hf->canfd->data, data_length);
 		} else {
+			/* ERROR frames tell us information about the controller */
+			if (hf->can_id & cpu_to_le32(CAN_ERR_FLAG))
+				gs_update_state(dev, hf, data_length);
+
 			skb = alloc_can_skb(netdev, &cf);
 			if (!skb)
 				goto resubmit_urb;
@@ -693,10 +698,6 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
 
 			memcpy(cf->data, hf->classic_can->data, data_length);
-
-			/* ERROR frames tell us information about the controller */
-			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
-				gs_update_state(dev, cf);
 		}
 
 		gs_usb_rx_offload(dev, skb, hf);
-- 
2.53.0


  parent reply	other threads:[~2026-11-30  9:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  9:15 [PATCH net-next 0/9] pull-request: can-next 2026-11-30 Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 1/9] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-30  9:23   ` netdev-bot+sinfo
2026-11-30  9:15 ` Marc Kleine-Budde [this message]
2026-11-30  9:15 ` [PATCH net-next 3/9] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 4/9] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 5/9] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 6/9] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 7/9] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 8/9] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info_ratelimited() to print re-submit info messages Marc Kleine-Budde
2026-11-30  9:15 ` [PATCH net-next 9/9] can: gs_usb: gs_usb_receive_bulk_callback(): emit error message on invalid channel instead of disconnecting interfaces Marc Kleine-Budde

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=20261130092003.1946901-3-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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