]> git.baikalelectronics.ru Git - kernel.git/commit
can: grcan: grcan_close(): fix deadlock
authorDuoming Zhou <duoming@zju.edu.cn>
Mon, 25 Apr 2022 04:24:00 +0000 (12:24 +0800)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Fri, 29 Apr 2022 10:09:19 +0000 (12:09 +0200)
commite7d3c10f01e90cf8b72f56a59a1a4c699a36fef8
treef385a5fbed2d7b79da328eedd6c8ddcea9522ee7
parente2e88b7952b5892049a096a5230b9eae36247cea
can: grcan: grcan_close(): fix deadlock

There are deadlocks caused by del_timer_sync(&priv->hang_timer) and
del_timer_sync(&priv->rr_timer) in grcan_close(), one of the deadlocks
are shown below:

   (Thread 1)              |      (Thread 2)
                           | grcan_reset_timer()
grcan_close()              |  mod_timer()
 spin_lock_irqsave() //(1) |  (wait a time)
 ...                       | grcan_initiate_running_reset()
 del_timer_sync()          |  spin_lock_irqsave() //(2)
 (wait timer to stop)      |  ...

We hold priv->lock in position (1) of thread 1 and use
del_timer_sync() to wait timer to stop, but timer handler also need
priv->lock in position (2) of thread 2. As a result, grcan_close()
will block forever.

This patch extracts del_timer_sync() from the protection of
spin_lock_irqsave(), which could let timer handler to obtain the
needed lock.

Link: https://lore.kernel.org/all/20220425042400.66517-1-duoming@zju.edu.cn
Fixes: c7e7f7ef6626 ("can: grcan: Add device driver for GRCAN and GRHCAN cores")
Cc: stable@vger.kernel.org
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Reviewed-by: Andreas Larsson <andreas@gaisler.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/grcan.c