mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Larsson <andreas@gaisler.com>
To: Oliver Hartkopp <socketcan@hartkopp.net>,
	Duoming Zhou <duoming@zju.edu.cn>,
	linux-kernel@vger.kernel.org
Cc: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net,
	kuba@kernel.org, pabeni@redhat.com, linux-can@vger.kernel.org,
	netdev@vger.kernel.org
Subject: Re: [PATCH net] drivers: net: can: Fix deadlock in grcan_close()
Date: Wed, 27 Apr 2022 14:47:12 +0200	[thread overview]
Message-ID: <2f5295c1-d787-84a5-1b3e-813f96dd4ae2@gaisler.com> (raw)
In-Reply-To: <caaa6059-6172-e562-e48e-5987884052b9@hartkopp.net>

On 2022-04-26 21:12, Oliver Hartkopp wrote:
> On 25.04.22 06:24, Duoming Zhou wrote:
>> 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.
>>
>> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
>> ---
>>   drivers/net/can/grcan.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
>> index d0c5a7a60da..1189057b5d6 100644
>> --- a/drivers/net/can/grcan.c
>> +++ b/drivers/net/can/grcan.c
>> @@ -1102,8 +1102,10 @@ static int grcan_close(struct net_device *dev)
>>       priv->closing = true;
>>       if (priv->need_txbug_workaround) {
>> +        spin_unlock_irqrestore(&priv->lock, flags);
>>           del_timer_sync(&priv->hang_timer);
>>           del_timer_sync(&priv->rr_timer);
>> +        spin_lock_irqsave(&priv->lock, flags);
> 
> It looks weird to unlock and re-lock the operations like this. This 
> breaks the intended locking for the closing process.
> 
> Isn't there any possibility to e.g. move that entire if-section before 
> the lock?

All functions wishing to start the timers both check priv->closing and
then, if false, start the timer within the priv->lock spinlock. Given
that, it should be ok that del_timer_sync is not done within the
spinlock as therefore no one can restart any timers after priv->closing
has been set to true.

It looks a bit weird, but setting priv->closing to true needs to happen
within the priv->lock spinlock protection and needs to happen before
del_timer_sync to avoid a race between grcan_close and someone starting
the timer.

Reviewed-by: Andreas Larsson <andreas@gaisler.com>

-- 
Andreas Larsson

  reply	other threads:[~2022-04-27 12:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-25  4:24 Duoming Zhou
2022-04-26 19:12 ` Oliver Hartkopp
2022-04-27 12:47   ` Andreas Larsson [this message]
2022-04-27 16:25     ` Oliver Hartkopp

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=2f5295c1-d787-84a5-1b3e-813f96dd4ae2@gaisler.com \
    --to=andreas@gaisler.com \
    --cc=davem@davemloft.net \
    --cc=duoming@zju.edu.cn \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=socketcan@hartkopp.net \
    --cc=wg@grandegger.com \
    /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

all inboxes | Powered by JetHome®