patches.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev,
	Vincent Mailhol <mailhol.vincent@wanadoo.fr>,
	Marc Kleine-Budde <mkl@pengutronix.de>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH 4.19 10/88] can: dev: can_restart(): fix race condition between controller restart and netif_carrier_on()
Date: Wed, 15 Nov 2023 15:35:22 -0500	[thread overview]
Message-ID: <20231115191426.799403647@linuxfoundation.org> (raw)
In-Reply-To: <20231115191426.221330369@linuxfoundation.org>

4.19-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Marc Kleine-Budde <mkl@pengutronix.de>

[ Upstream commit 6841cab8c4504835e4011689cbdb3351dec693fd ]

This race condition was discovered while updating the at91_can driver
to use can_bus_off(). The following scenario describes how the
converted at91_can driver would behave.

When a CAN device goes into BUS-OFF state, the driver usually
stops/resets the CAN device and calls can_bus_off().

This function sets the netif carrier to off, and (if configured by
user space) schedules a delayed work that calls can_restart() to
restart the CAN device.

The can_restart() function first checks if the carrier is off and
triggers an error message if the carrier is OK.

Then it calls the driver's do_set_mode() function to restart the
device, then it sets the netif carrier to on. There is a race window
between these two calls.

The at91 CAN controller (observed on the sama5d3, a single core 32 bit
ARM CPU) has a hardware limitation. If the device goes into bus-off
while sending a CAN frame, there is no way to abort the sending of
this frame. After the controller is enabled again, another attempt is
made to send it.

If the bus is still faulty, the device immediately goes back to the
bus-off state. The driver calls can_bus_off(), the netif carrier is
switched off and another can_restart is scheduled. This occurs within
the race window before the original can_restart() handler marks the
netif carrier as OK. This would cause the 2nd can_restart() to be
called with an OK netif carrier, resulting in an error message.

The flow of the 1st can_restart() looks like this:

can_restart()
    // bail out if netif_carrier is OK

    netif_carrier_ok(dev)
    priv->do_set_mode(dev, CAN_MODE_START)
        // enable CAN controller
        // sama5d3 restarts sending old message

        // CAN devices goes into BUS_OFF, triggers IRQ

// IRQ handler start
    at91_irq()
        at91_irq_err_line()
            can_bus_off()
                netif_carrier_off()
                schedule_delayed_work()
// IRQ handler end

    netif_carrier_on()

The 2nd can_restart() will be called with an OK netif carrier and the
error message will be printed.

To close the race window, first set the netif carrier to on, then
restart the controller. In case the restart fails with an error code,
roll back the netif carrier to off.

Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface")
Link: https://lore.kernel.org/all/20231005-can-dev-fix-can-restart-v2-2-91b5c1fd922c@pengutronix.de
Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/net/can/dev/dev.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 487cb9acdd456..3797d4de254dd 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -590,11 +590,12 @@ static void can_restart(struct net_device *dev)
 	priv->can_stats.restarts++;
 
 	/* Now restart the device */
-	err = priv->do_set_mode(dev, CAN_MODE_START);
-
 	netif_carrier_on(dev);
-	if (err)
+	err = priv->do_set_mode(dev, CAN_MODE_START);
+	if (err) {
 		netdev_err(dev, "Error %d during restart", err);
+		netif_carrier_off(dev);
+	}
 }
 
 static void can_restart_work(struct work_struct *work)
-- 
2.42.0




  parent reply	other threads:[~2023-11-15 20:44 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-15 20:35 [PATCH 4.19 00/88] 4.19.299-rc1 review Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 01/88] vfs: fix readahead(2) on block devices Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 02/88] genirq/matrix: Exclude managed interrupts in irq_matrix_allocated() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 03/88] i40e: fix potential memory leaks in i40e_remove() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 04/88] tcp_metrics: add missing barriers on delete Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 05/88] tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 06/88] tcp_metrics: do not create an entry from tcp_init_metrics() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 07/88] wifi: rtlwifi: fix EDCA limit set by BT coexistence Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 08/88] can: dev: move driver related infrastructure into separate subdir Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 09/88] can: dev: can_restart(): dont crash kernel if carrier is OK Greg Kroah-Hartman
2023-11-15 20:35 ` Greg Kroah-Hartman [this message]
2023-11-15 20:35 ` [PATCH 4.19 11/88] thermal: core: prevent potential string overflow Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 12/88] chtls: fix tp->rcv_tstamp initialization Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 13/88] ACPI: sysfs: Fix create_pnp_modalias() and create_of_modalias() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 14/88] ipv6: avoid atomic fragment on GSO packets Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 15/88] macsec: Fix traffic counters/statistics Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 16/88] macsec: use DEV_STATS_INC() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 17/88] net: add DEV_STATS_READ() helper Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 18/88] ipvlan: properly track tx_errors Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 19/88] regmap: debugfs: Fix a erroneous check after snprintf() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 20/88] clk: qcom: clk-rcg2: Fix clock rate overflow for high parent frequencies Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 21/88] clk: keystone: pll: fix a couple NULL vs IS_ERR() checks Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 22/88] clk: npcm7xx: Fix incorrect kfree Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 23/88] clk: mediatek: clk-mt6797: Add check for mtk_alloc_clk_data Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 24/88] clk: mediatek: clk-mt2701: " Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 25/88] platform/x86: wmi: Fix probe failure when failing to register WMI devices Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 26/88] platform/x86: wmi: remove unnecessary initializations Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 27/88] platform/x86: wmi: Fix opening of char device Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 28/88] hwmon: (coretemp) Fix potentially truncated sysfs attribute name Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 29/88] drm/rockchip: vop: Fix reset of state in duplicate state crtc funcs Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 30/88] drm/radeon: possible buffer overflow Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 31/88] drm/rockchip: cdn-dp: Fix some error handling paths in cdn_dp_probe() Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 32/88] ARM: dts: qcom: mdm9615: populate vsdcc fixed regulator Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 33/88] firmware: ti_sci: Mark driver as non removable Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 34/88] clk: scmi: Free scmi_clk allocated when the clocks with invalid info are skipped Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 35/88] hwrng: geode - fix accessing registers Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 36/88] sched/rt: Provide migrate_disable/enable() inlines Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 37/88] nd_btt: Make BTT lanes preemptible Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 38/88] HID: cp2112: Use irqchip template Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 39/88] hid: cp2112: Fix duplicate workqueue initialization Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 40/88] ARM: 9321/1: memset: cast the constant byte to unsigned char Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 41/88] ext4: move ix sanity check to corrent position Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 42/88] RDMA/hfi1: Workaround truncation compilation error Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 43/88] sh: bios: Revive earlyprintk support Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 44/88] ASoC: Intel: Skylake: Fix mem leak when parsing UUIDs fails Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 45/88] ASoC: ams-delta.c: use component after check Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 46/88] mfd: dln2: Fix double put in dln2_probe Greg Kroah-Hartman
2023-11-15 20:35 ` [PATCH 4.19 47/88] leds: pwm: simplify if condition Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 48/88] leds: pwm: convert to atomic PWM API Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 49/88] leds: pwm: Dont disable the PWM when the LED should be off Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 50/88] ledtrig-cpu: Limit to 8 CPUs Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 51/88] leds: trigger: ledtrig-cpu:: Fix output may be truncated issue for cpu Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 52/88] tty: tty_jobctrl: fix pid memleak in disassociate_ctty() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 53/88] usb: dwc2: fix possible NULL pointer dereference caused by driver concurrency Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 54/88] dmaengine: ti: edma: handle irq_of_parse_and_map() errors Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 55/88] misc: st_core: Do not call kfree_skb() under spin_lock_irqsave() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 56/88] tools: iio: privatize globals and functions in iio_generic_buffer.c file Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 57/88] tools: iio: iio_generic_buffer: Fix some integer type and calculation Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 58/88] tools: iio: iio_generic_buffer ensure alignment Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 59/88] USB: usbip: fix stub_dev hub disconnect Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 60/88] dmaengine: pxa_dma: Remove an erroneous BUG_ON() in pxad_free_desc() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 61/88] f2fs: fix to initialize map.m_pblk in f2fs_precache_extents() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 62/88] pcmcia: cs: fix possible hung task and memory leak pccardd() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 63/88] pcmcia: ds: fix refcount leak in pcmcia_device_add() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 64/88] pcmcia: ds: fix possible name leak in error path " Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 65/88] media: bttv: fix use after free error due to btv->timeout timer Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 66/88] media: s3c-camif: Avoid inappropriate kfree() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 67/88] media: dvb-usb-v2: af9035: fix missing unlock Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 68/88] pwm: sti: Avoid conditional gotos Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 69/88] pwm: sti: Reduce number of allocations and drop usage of chip_data Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 70/88] pwm: brcmstb: Utilize appropriate clock APIs in suspend/resume Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 71/88] Input: synaptics-rmi4 - fix use after free in rmi_unregister_function() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 72/88] llc: verify mac len before reading mac header Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 73/88] tipc: Change nla_policy for bearer-related names to NLA_NUL_STRING Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 74/88] dccp: Call security_inet_conn_request() after setting IPv4 addresses Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 75/88] dccp/tcp: Call security_inet_conn_request() after setting IPv6 addresses Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 76/88] r8169: improve rtl_set_rx_mode Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 77/88] net: r8169: Disable multicast filter for RTL8168H and RTL8107E Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 78/88] net/smc: postpone release of clcsock Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 79/88] net/smc: wait for pending work before clcsock release_sock Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 80/88] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 81/88] tg3: power down device only on SYSTEM_POWER_OFF Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 82/88] r8169: respect userspace disabling IFF_MULTICAST Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 83/88] netfilter: xt_recent: fix (increase) ipv6 literal buffer length Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 84/88] fbdev: imsttfb: Fix error path of imsttfb_probe() Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 85/88] fbdev: imsttfb: fix a resource leak in probe Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 86/88] fbdev: fsl-diu-fb: mark wr_reg_wa() static Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 87/88] Revert "mmc: core: Capture correct oemid-bits for eMMC cards" Greg Kroah-Hartman
2023-11-15 20:36 ` [PATCH 4.19 88/88] btrfs: use u64 for buffer sizes in the tree search ioctls Greg Kroah-Hartman
2023-11-16 11:13 ` [PATCH 4.19 00/88] 4.19.299-rc1 review Naresh Kamboju
2023-11-17  4:24 ` Guenter Roeck
2023-11-17 17:00 ` Pavel Machek

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=20231115191426.799403647@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=mkl@pengutronix.de \
    --cc=patches@lists.linux.dev \
    --cc=sashal@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;
as well as URLs for NNTP newsgroup(s).