From nobody Fri Sep 5 20:25:38 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9BDDC32772 for ; Tue, 23 Aug 2022 11:52:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358663AbiHWLwe (ORCPT ); Tue, 23 Aug 2022 07:52:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358257AbiHWLtz (ORCPT ); Tue, 23 Aug 2022 07:49:55 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F166692F4B; Tue, 23 Aug 2022 02:31:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 126FBB81C66; Tue, 23 Aug 2022 09:31:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F39CC433D6; Tue, 23 Aug 2022 09:31:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661247061; bh=8l5hSGeYSih64kUsq+WUZDdJtmqEdmuHwlFS06RQjCM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K/8kwN2HRsBvU9XJ2vRiMyAYEHSCfqUhHncr2Pyus37aghwr5OzkFkZHXfMEgcl6R 1Od/J2g3AZRObchTe20a6g+ziik+ly7DRN8fsw9rGSbpqv22ITznVajWnERVHUaux+ rMNEPwDVJNPyT0UDSFiWJRVXIEjGCVwQEFjjyIxA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Sebastian=20W=C3=BCrl?= , Marc Kleine-Budde Subject: [PATCH 5.4 307/389] can: mcp251x: Fix race condition on receive interrupt Date: Tue, 23 Aug 2022 10:26:25 +0200 Message-Id: <20220823080128.404948971@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080115.331990024@linuxfoundation.org> References: <20220823080115.331990024@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sebastian W=C3=BCrl commit d80d60b0db6ff3dd2e29247cc2a5166d7e9ae37e upstream. 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@oror= atech.com v2: https://lore.kernel.org/all/20220804064803.63157-1-sebastian.wuerl@oror= atech.com v1: https://lore.kernel.org/all/20220803153300.58732-1-sebastian.wuerl@oror= atech.com Fixes: bf66f3736a94 ("can: mcp251x: Move to threaded interrupts instead of = workqueues.") Signed-off-by: Sebastian W=C3=BCrl Link: https://lore.kernel.org/all/20220804081411.68567-1-sebastian.wuerl@or= oratech.com [mkl: reduce scope of intf1, eflag1] Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- drivers/net/can/spi/mcp251x.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) --- a/drivers/net/can/spi/mcp251x.c +++ b/drivers/net/can/spi/mcp251x.c @@ -756,9 +756,6 @@ static irqreturn_t mcp251x_can_ist(int i =20 mcp251x_read_2regs(spi, CANINTF, &intf, &eflag); =20 - /* mask out flags we don't care about */ - intf &=3D CANINTF_RX | CANINTF_TX | CANINTF_ERR; - /* receive buffer 0 */ if (intf & CANINTF_RX0IF) { mcp251x_hw_rx(spi, 0); @@ -768,6 +765,18 @@ static irqreturn_t mcp251x_can_ist(int i 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 |=3D intf1; + eflag |=3D eflag1; + } } =20 /* receive buffer 1 */ @@ -778,6 +787,9 @@ static irqreturn_t mcp251x_can_ist(int i clear_intf |=3D CANINTF_RX1IF; } =20 + /* mask out flags we don't care about */ + intf &=3D CANINTF_RX | CANINTF_TX | CANINTF_ERR; + /* any error or tx interrupt we need to clear? */ if (intf & (CANINTF_ERR | CANINTF_TX)) clear_intf |=3D intf & (CANINTF_ERR | CANINTF_TX);