All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net 0/n] pull-request: can 2022-08-10
@ 2022-08-10  7:14 Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once() Marc Kleine-Budde
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:14 UTC (permalink / raw)
  To: netdev; +Cc: davem, kuba, linux-can, kernel

Hello Jakub, hello David,

this is a pull request of 4 patches for net/master, with the
whitespace issue fixed.

Fedor Pchelkin contributes 2 fixes for the j1939 CAN protocol.

A patch by me for the ems_usb driver fixes an unaligned access
warning.

Sebastian Würl's patch for the mcp251x driver fixes a race condition
in the receive interrupt.

regards,
Marc

---

The following changes since commit b8c3bf0ed2edf2deaedba5f0bf0bb54c76dee71d:

  Merge tag 'for-net-2022-08-08' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth (2022-08-08 20:59:07 -0700)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git tags/linux-can-fixes-for-6.0-20220810

for you to fetch changes up to d80d60b0db6ff3dd2e29247cc2a5166d7e9ae37e:

  can: mcp251x: Fix race condition on receive interrupt (2022-08-09 21:40:22 +0200)

----------------------------------------------------------------
linux-can-fixes-for-6.0-20220810

----------------------------------------------------------------
Fedor Pchelkin (2):
      can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once()
      can: j1939: j1939_session_destroy(): fix memory leak of skbs

Marc Kleine-Budde (1):
      can: ems_usb: fix clang's -Wunaligned-access warning

Sebastian Würl (1):
      can: mcp251x: Fix race condition on receive interrupt

 drivers/net/can/spi/mcp251x.c | 18 +++++++++++++++---
 drivers/net/can/usb/ems_usb.c |  2 +-
 net/can/j1939/socket.c        |  5 ++++-
 net/can/j1939/transport.c     |  8 +++++++-
 4 files changed, 27 insertions(+), 6 deletions(-)



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once()
  2022-08-10  7:14 [PATCH net 0/n] pull-request: can 2022-08-10 Marc Kleine-Budde
@ 2022-08-10  7:14 ` Marc Kleine-Budde
  2022-08-10  8:30   ` patchwork-bot+netdevbpf
  2022-08-10  7:14 ` [PATCH net 2/4] can: j1939: j1939_session_destroy(): fix memory leak of skbs Marc Kleine-Budde
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:14 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, linux-can, kernel, Fedor Pchelkin,
	Alexey Khoroshilov, Oleksij Rempel, Marc Kleine-Budde

From: Fedor Pchelkin <pchelkin@ispras.ru>

We should warn user-space that it is doing something wrong when trying
to activate sessions with identical parameters but WARN_ON_ONCE macro
can not be used here as it serves a different purpose.

So it would be good to replace it with netdev_warn_once() message.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/all/20220729143655.1108297-1-pchelkin@ispras.ru
[mkl: fix indention]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/j1939/socket.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
index f5ecfdcf57b2..b670ba03a675 100644
--- a/net/can/j1939/socket.c
+++ b/net/can/j1939/socket.c
@@ -178,7 +178,10 @@ static void j1939_sk_queue_activate_next_locked(struct j1939_session *session)
 	if (!first)
 		return;
 
-	if (WARN_ON_ONCE(j1939_session_activate(first))) {
+	if (j1939_session_activate(first)) {
+		netdev_warn_once(first->priv->ndev,
+				 "%s: 0x%p: Identical session is already activated.\n",
+				 __func__, first);
 		first->err = -EBUSY;
 		goto activate_next;
 	} else {

base-commit: b8c3bf0ed2edf2deaedba5f0bf0bb54c76dee71d
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH net 2/4] can: j1939: j1939_session_destroy(): fix memory leak of skbs
  2022-08-10  7:14 [PATCH net 0/n] pull-request: can 2022-08-10 Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once() Marc Kleine-Budde
@ 2022-08-10  7:14 ` Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 3/4] can: ems_usb: fix clang's -Wunaligned-access warning Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt Marc Kleine-Budde
  3 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:14 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, linux-can, kernel, Fedor Pchelkin, Oleksij Rempel,
	Alexey Khoroshilov, Marc Kleine-Budde

From: Fedor Pchelkin <pchelkin@ispras.ru>

We need to drop skb references taken in j1939_session_skb_queue() when
destroying a session in j1939_session_destroy(). Otherwise those skbs
would be lost.

Link to Syzkaller info and repro: https://forge.ispras.ru/issues/11743.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

V1: https://lore.kernel.org/all/20220708175949.539064-1-pchelkin@ispras.ru

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Suggested-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/all/20220805150216.66313-1-pchelkin@ispras.ru
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/j1939/transport.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 307ee1174a6e..d7d86c944d76 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -260,6 +260,8 @@ static void __j1939_session_drop(struct j1939_session *session)
 
 static void j1939_session_destroy(struct j1939_session *session)
 {
+	struct sk_buff *skb;
+
 	if (session->transmission) {
 		if (session->err)
 			j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
@@ -274,7 +276,11 @@ static void j1939_session_destroy(struct j1939_session *session)
 	WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
 	WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
 
-	skb_queue_purge(&session->skb_queue);
+	while ((skb = skb_dequeue(&session->skb_queue)) != NULL) {
+		/* drop ref taken in j1939_session_skb_queue() */
+		skb_unref(skb);
+		kfree_skb(skb);
+	}
 	__j1939_session_drop(session);
 	j1939_priv_put(session->priv);
 	kfree(session);
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH net 3/4] can: ems_usb: fix clang's -Wunaligned-access warning
  2022-08-10  7:14 [PATCH net 0/n] pull-request: can 2022-08-10 Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once() Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 2/4] can: j1939: j1939_session_destroy(): fix memory leak of skbs Marc Kleine-Budde
@ 2022-08-10  7:14 ` Marc Kleine-Budde
  2022-08-10  7:14 ` [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt Marc Kleine-Budde
  3 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:14 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, linux-can, kernel, Marc Kleine-Budde,
	Gerhard Uttenthaler, Sebastian Haas

clang emits a -Wunaligned-access warning on struct __packed
ems_cpc_msg.

The reason is that the anonymous union msg (not declared as packed) is
being packed right after some non naturally aligned variables (3*8
bits + 2*32) inside a packed struct:

| struct __packed ems_cpc_msg {
| 	u8 type;	/* type of message */
| 	u8 length;	/* length of data within union 'msg' */
| 	u8 msgid;	/* confirmation handle */
| 	__le32 ts_sec;	/* timestamp in seconds */
| 	__le32 ts_nsec;	/* timestamp in nano seconds */
|	/* ^ not naturally aligned */
|
| 	union {
| 	/* ^ not declared as packed */
| 		u8 generic[64];
| 		struct cpc_can_msg can_msg;
| 		struct cpc_can_params can_params;
| 		struct cpc_confirm confirmation;
| 		struct cpc_overrun overrun;
| 		struct cpc_can_error error;
| 		struct cpc_can_err_counter err_counter;
| 		u8 can_state;
| 	} msg;
| };

Starting from LLVM 14, having an unpacked struct nested in a packed
struct triggers a warning. c.f. [1].

Fix the warning by marking the anonymous union as packed.

[1] https://github.com/llvm/llvm-project/issues/55520

Fixes: 702171adeed3 ("ems_usb: Added support for EMS CPC-USB/ARM7 CAN/USB interface")
Link: https://lore.kernel.org/all/20220802094021.959858-1-mkl@pengutronix.de
Cc: Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
Cc: Sebastian Haas <haas@ems-wuensche.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/usb/ems_usb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index d1e1a459c045..d31191686a54 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -195,7 +195,7 @@ struct __packed ems_cpc_msg {
 	__le32 ts_sec;	/* timestamp in seconds */
 	__le32 ts_nsec;	/* timestamp in nano seconds */
 
-	union {
+	union __packed {
 		u8 generic[64];
 		struct cpc_can_msg can_msg;
 		struct cpc_can_params can_params;
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt
  2022-08-10  7:14 [PATCH net 0/n] pull-request: can 2022-08-10 Marc Kleine-Budde
                   ` (2 preceding siblings ...)
  2022-08-10  7:14 ` [PATCH net 3/4] can: ems_usb: fix clang's -Wunaligned-access warning Marc Kleine-Budde
@ 2022-08-10  7:14 ` Marc Kleine-Budde
  3 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:14 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, linux-can, kernel, Sebastian Würl, Marc Kleine-Budde

From: Sebastian Würl <sebastian.wuerl@ororatech.com>

The mcp251x driver uses both receiving mailboxes of the CAN controller
chips. For retrieving the CAN frames from the controller via SPI, it checks
once per interrupt which mailboxes have been filled and will retrieve the
messages accordingly.

This introduces a race condition, as another CAN frame can enter mailbox 1
while mailbox 0 is emptied. If now another CAN frame enters mailbox 0 until
the interrupt handler is called next, mailbox 0 is emptied before
mailbox 1, leading to out-of-order CAN frames in the network device.

This is fixed by checking the interrupt flags once again after freeing
mailbox 0, to correctly also empty mailbox 1 before leaving the handler.

For reproducing the bug I created the following setup:
 - Two CAN devices, one Raspberry Pi with MCP2515, the other can be any.
 - Setup CAN to 1 MHz
 - Spam bursts of 5 CAN-messages with increasing CAN-ids
 - Continue sending the bursts while sleeping a second between the bursts
 - Check on the RPi whether the received messages have increasing CAN-ids
 - Without this patch, every burst of messages will contain a flipped pair

v3: https://lore.kernel.org/all/20220804075914.67569-1-sebastian.wuerl@ororatech.com
v2: https://lore.kernel.org/all/20220804064803.63157-1-sebastian.wuerl@ororatech.com
v1: https://lore.kernel.org/all/20220803153300.58732-1-sebastian.wuerl@ororatech.com

Fixes: bf66f3736a94 ("can: mcp251x: Move to threaded interrupts instead of workqueues.")
Signed-off-by: Sebastian Würl <sebastian.wuerl@ororatech.com>
Link: https://lore.kernel.org/all/20220804081411.68567-1-sebastian.wuerl@ororatech.com
[mkl: reduce scope of intf1, eflag1]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/spi/mcp251x.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index e750d13c8841..c320de474f40 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -1070,9 +1070,6 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 
 		mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
 
-		/* mask out flags we don't care about */
-		intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
-
 		/* receive buffer 0 */
 		if (intf & CANINTF_RX0IF) {
 			mcp251x_hw_rx(spi, 0);
@@ -1082,6 +1079,18 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 			if (mcp251x_is_2510(spi))
 				mcp251x_write_bits(spi, CANINTF,
 						   CANINTF_RX0IF, 0x00);
+
+			/* check if buffer 1 is already known to be full, no need to re-read */
+			if (!(intf & CANINTF_RX1IF)) {
+				u8 intf1, eflag1;
+
+				/* intf needs to be read again to avoid a race condition */
+				mcp251x_read_2regs(spi, CANINTF, &intf1, &eflag1);
+
+				/* combine flags from both operations for error handling */
+				intf |= intf1;
+				eflag |= eflag1;
+			}
 		}
 
 		/* receive buffer 1 */
@@ -1092,6 +1101,9 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 				clear_intf |= CANINTF_RX1IF;
 		}
 
+		/* mask out flags we don't care about */
+		intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
+
 		/* any error or tx interrupt we need to clear? */
 		if (intf & (CANINTF_ERR | CANINTF_TX))
 			clear_intf |= intf & (CANINTF_ERR | CANINTF_TX);
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once()
  2022-08-10  7:14 ` [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once() Marc Kleine-Budde
@ 2022-08-10  8:30   ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 9+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-08-10  8:30 UTC (permalink / raw)
  To: Marc Kleine-Budde
  Cc: netdev, davem, kuba, linux-can, kernel, pchelkin, khoroshilov, o.rempel

Hello:

This series was applied to netdev/net.git (master)
by Marc Kleine-Budde <mkl@pengutronix.de>:

On Wed, 10 Aug 2022 09:14:45 +0200 you wrote:
> From: Fedor Pchelkin <pchelkin@ispras.ru>
> 
> We should warn user-space that it is doing something wrong when trying
> to activate sessions with identical parameters but WARN_ON_ONCE macro
> can not be used here as it serves a different purpose.
> 
> So it would be good to replace it with netdev_warn_once() message.
> 
> [...]

Here is the summary with links:
  - [net,1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once()
    https://git.kernel.org/netdev/net/c/8ef49f7f8244
  - [net,2/4] can: j1939: j1939_session_destroy(): fix memory leak of skbs
    https://git.kernel.org/netdev/net/c/8c21c54a53ab
  - [net,3/4] can: ems_usb: fix clang's -Wunaligned-access warning
    https://git.kernel.org/netdev/net/c/a4cb6e62ea4d
  - [net,4/4] can: mcp251x: Fix race condition on receive interrupt
    https://git.kernel.org/netdev/net/c/d80d60b0db6f

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt
  2022-08-09 18:50   ` Jakub Kicinski
@ 2022-08-10  7:19     ` Marc Kleine-Budde
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-10  7:19 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: netdev, davem, linux-can, kernel, Sebastian Würl

[-- Attachment #1: Type: text/plain, Size: 1067 bytes --]

On 09.08.2022 11:50:16, Jakub Kicinski wrote:
> On Tue,  9 Aug 2022 09:53:17 +0200 Marc Kleine-Budde wrote:
> > @@ -1082,6 +1079,18 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
> >  			if (mcp251x_is_2510(spi))
> >  				mcp251x_write_bits(spi, CANINTF,
> >  						   CANINTF_RX0IF, 0x00);
> > +
> > +			/* check if buffer 1 is already known to be full, no need to re-read */
> > +			if (!(intf & CANINTF_RX1IF)) {
> > +				u8 intf1, eflag1;
> > +				
> 
> This line is full of trailing whitespace, could you add a fix on top to
> remove it and resend?

Doh! It was me moving both variables there to reduce their scope and
somehow the whitespace slipped in. Here's an updated PR:

https://lore.kernel.org/all/20220810071448.1627857-1-mkl@pengutronix.de/

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt
  2022-08-09  7:53 ` [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt Marc Kleine-Budde
@ 2022-08-09 18:50   ` Jakub Kicinski
  2022-08-10  7:19     ` Marc Kleine-Budde
  0 siblings, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2022-08-09 18:50 UTC (permalink / raw)
  To: Marc Kleine-Budde; +Cc: netdev, davem, linux-can, kernel, Sebastian Würl

On Tue,  9 Aug 2022 09:53:17 +0200 Marc Kleine-Budde wrote:
> @@ -1082,6 +1079,18 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
>  			if (mcp251x_is_2510(spi))
>  				mcp251x_write_bits(spi, CANINTF,
>  						   CANINTF_RX0IF, 0x00);
> +
> +			/* check if buffer 1 is already known to be full, no need to re-read */
> +			if (!(intf & CANINTF_RX1IF)) {
> +				u8 intf1, eflag1;
> +				

This line is full of trailing whitespace, could you add a fix on top to
remove it and resend?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt
  2022-08-09  7:53 [PATCH net 0/4] pull-request: can 2022-08-09 Marc Kleine-Budde
@ 2022-08-09  7:53 ` Marc Kleine-Budde
  2022-08-09 18:50   ` Jakub Kicinski
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Kleine-Budde @ 2022-08-09  7:53 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, linux-can, kernel, Sebastian Würl, Marc Kleine-Budde

From: Sebastian Würl <sebastian.wuerl@ororatech.com>

The mcp251x driver uses both receiving mailboxes of the CAN controller
chips. For retrieving the CAN frames from the controller via SPI, it checks
once per interrupt which mailboxes have been filled and will retrieve the
messages accordingly.

This introduces a race condition, as another CAN frame can enter mailbox 1
while mailbox 0 is emptied. If now another CAN frame enters mailbox 0 until
the interrupt handler is called next, mailbox 0 is emptied before
mailbox 1, leading to out-of-order CAN frames in the network device.

This is fixed by checking the interrupt flags once again after freeing
mailbox 0, to correctly also empty mailbox 1 before leaving the handler.

For reproducing the bug I created the following setup:
 - Two CAN devices, one Raspberry Pi with MCP2515, the other can be any.
 - Setup CAN to 1 MHz
 - Spam bursts of 5 CAN-messages with increasing CAN-ids
 - Continue sending the bursts while sleeping a second between the bursts
 - Check on the RPi whether the received messages have increasing CAN-ids
 - Without this patch, every burst of messages will contain a flipped pair

v3: https://lore.kernel.org/all/20220804075914.67569-1-sebastian.wuerl@ororatech.com
v2: https://lore.kernel.org/all/20220804064803.63157-1-sebastian.wuerl@ororatech.com
v1: https://lore.kernel.org/all/20220803153300.58732-1-sebastian.wuerl@ororatech.com

Fixes: bf66f3736a94 ("can: mcp251x: Move to threaded interrupts instead of workqueues.")
Signed-off-by: Sebastian Würl <sebastian.wuerl@ororatech.com>
Link: https://lore.kernel.org/all/20220804081411.68567-1-sebastian.wuerl@ororatech.com
[mkl: reduce scope of intf1, eflag1]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/spi/mcp251x.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index e750d13c8841..e4d0bdd8981a 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -1070,9 +1070,6 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 
 		mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
 
-		/* mask out flags we don't care about */
-		intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
-
 		/* receive buffer 0 */
 		if (intf & CANINTF_RX0IF) {
 			mcp251x_hw_rx(spi, 0);
@@ -1082,6 +1079,18 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 			if (mcp251x_is_2510(spi))
 				mcp251x_write_bits(spi, CANINTF,
 						   CANINTF_RX0IF, 0x00);
+
+			/* check if buffer 1 is already known to be full, no need to re-read */
+			if (!(intf & CANINTF_RX1IF)) {
+				u8 intf1, eflag1;
+				
+				/* intf needs to be read again to avoid a race condition */
+				mcp251x_read_2regs(spi, CANINTF, &intf1, &eflag1);
+
+				/* combine flags from both operations for error handling */
+				intf |= intf1;
+				eflag |= eflag1;
+			}
 		}
 
 		/* receive buffer 1 */
@@ -1092,6 +1101,9 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 				clear_intf |= CANINTF_RX1IF;
 		}
 
+		/* mask out flags we don't care about */
+		intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
+
 		/* any error or tx interrupt we need to clear? */
 		if (intf & (CANINTF_ERR | CANINTF_TX))
 			clear_intf |= intf & (CANINTF_ERR | CANINTF_TX);
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-08-10  8:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10  7:14 [PATCH net 0/n] pull-request: can 2022-08-10 Marc Kleine-Budde
2022-08-10  7:14 ` [PATCH net 1/4] can: j1939: j1939_sk_queue_activate_next_locked(): replace WARN_ON_ONCE with netdev_warn_once() Marc Kleine-Budde
2022-08-10  8:30   ` patchwork-bot+netdevbpf
2022-08-10  7:14 ` [PATCH net 2/4] can: j1939: j1939_session_destroy(): fix memory leak of skbs Marc Kleine-Budde
2022-08-10  7:14 ` [PATCH net 3/4] can: ems_usb: fix clang's -Wunaligned-access warning Marc Kleine-Budde
2022-08-10  7:14 ` [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt Marc Kleine-Budde
  -- strict thread matches above, loose matches on Subject: below --
2022-08-09  7:53 [PATCH net 0/4] pull-request: can 2022-08-09 Marc Kleine-Budde
2022-08-09  7:53 ` [PATCH net 4/4] can: mcp251x: Fix race condition on receive interrupt Marc Kleine-Budde
2022-08-09 18:50   ` Jakub Kicinski
2022-08-10  7:19     ` Marc Kleine-Budde

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.