linux/drivers/net/can
Vincent Mailhol f4896248e9 can: etas_es58x: change opened_channel_cnt's type from atomic_t to u8
The driver uses an atomic_t variable: struct
es58x_device::opened_channel_cnt to keep track of the number of opened
channels in order to only allocate memory for the URBs when this count
changes from zero to one.

While the intent was to prevent race conditions, the choice of an
atomic_t turns out to be a bad idea for several reasons:

- implementation is incorrect and fails to decrement
  opened_channel_cnt when the URB allocation fails as reported in
  [1].

- even if opened_channel_cnt were to be correctly decremented,
  atomic_t is insufficient to cover edge cases: there can be a race
  condition in which 1/ a first process fails to allocate URBs
  memory 2/ a second process enters es58x_open() before the first
  process does its cleanup and decrements opened_channed_cnt. In
  which case, the second process would successfully return despite
  the URBs memory not being allocated.

- actually, any kind of locking mechanism was useless here because
  it is redundant with the network stack big kernel lock
  (a.k.a. rtnl_lock) which is being hold by all the callers of
  net_device_ops:ndo_open() and net_device_ops:ndo_close(). c.f. the
  ASSERST_RTNL() calls in __dev_open() [2] and __dev_close_many()
  [3].

The atmomic_t is thus replaced by a simple u8 type and the logic to
increment and decrement es58x_device:opened_channel_cnt is simplified
accordingly fixing the bug reported in [1]. We do not check again for
ASSERST_RTNL() as this is already done by the callers.

[1] https://lore.kernel.org/linux-can/20220201140351.GA2548@kili/T/#u
[2] https://elixir.bootlin.com/linux/v5.16/source/net/core/dev.c#L1463
[3] https://elixir.bootlin.com/linux/v5.16/source/net/core/dev.c#L1541

Fixes: 8537257874 ("can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces")
Link: https://lore.kernel.org/all/20220212112713.577957-1-mailhol.vincent@wanadoo.fr
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2022-02-25 09:14:34 +01:00
..
c_can can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
cc770 can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
dev can: netlink: report the CAN controller mode supported flags 2022-01-05 12:09:06 +01:00
flexcan can: flexcan: mark RX via mailboxes as supported on MCF5441X 2022-01-24 18:27:43 +01:00
ifi_canfd can: do not increase rx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
m_can can: tcan4x5x: regmap: fix max register value 2022-01-24 18:27:43 +01:00
mscan can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
peak_canfd can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
rcar Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2022-01-09 17:00:17 -08:00
sja1000 can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
softing Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2022-01-09 17:00:17 -08:00
spi can: mcp251xfd: introduce and make use of mcp251xfd_is_fd_mode() 2022-01-08 20:17:42 +01:00
usb can: etas_es58x: change opened_channel_cnt's type from atomic_t to u8 2022-02-25 09:14:34 +01:00
at91_can.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
grcan.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
janz-ican3.c can: janz-ican3: initialize dlc variable 2022-01-08 20:17:41 +01:00
Kconfig can: flexcan: update Kconfig to enable coldfire 2021-07-25 11:36:29 +02:00
kvaser_pciefd.c can: do not increase rx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
led.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
Makefile can: flexcan: move driver into separate sub directory 2022-01-08 20:17:42 +01:00
pch_can.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
slcan.c TTY/Serial driver updates for 5.17-rc1 2022-01-12 11:21:52 -08:00
sun4i_can.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
ti_hecc.c can: ti_hecc: ti_hecc_probe(): use platform_get_irq() to get the interrupt 2022-01-05 12:08:19 +01:00
vcan.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
vxcan.c can: do not increase tx_bytes statistics for RTR frames 2022-01-05 12:09:05 +01:00
xilinx_can.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2022-01-09 17:00:17 -08:00