mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: duoming@zju.edu.cn
To: "Max Filippov" <jcmvbkbc@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	"Chris Zankel" <chris@zankel.net>,
	mustafa.ismail@intel.com, shiraz.saleem@intel.com, jgg@ziepe.ca,
	wg@grandegger.com, mkl@pengutronix.de,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	pabeni@redhat.com, jes@trained-monkey.org,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jiri Slaby" <jirislaby@kernel.org>,
	alexander.deucher@amd.com,
	"open list:TENSILICA XTENSA PORT (xtensa)" 
	<linux-xtensa@linux-xtensa.org>,
	linux-rdma@vger.kernel.org, linux-can@vger.kernel.org,
	netdev <netdev@vger.kernel.org>,
	linux-hippi@sunsite.dk, linux-staging@lists.linux.dev,
	linux-serial@vger.kernel.org,
	"USB list" <linux-usb@vger.kernel.org>,
	linma@zju.edu.cn
Subject: Re: Re: [PATCH 11/11] arch: xtensa: platforms: Fix deadlock in rs_close()
Date: Thu, 7 Apr 2022 19:05:54 +0800 (GMT+08:00)	[thread overview]
Message-ID: <569b0b8a.3f281.18003b3c5c4.Coremail.duoming@zju.edu.cn> (raw)
In-Reply-To: <CAMo8BfLG_u2z2HwY9Qo6cFgNoSrrz2mS2iD+rtj-uyrKhZYmLw@mail.gmail.com>

Hello,

On Thu, 7 Apr 2022 00:21:58 -0700 Max Filippov wrote:

> > There is a deadlock in rs_close(), which is shown
> > below:
> >
> >    (Thread 1)              |      (Thread 2)
> >                            | rs_open()
> > rs_close()                 |  mod_timer()
> >  spin_lock_bh() //(1)      |  (wait a time)
> >  ...                       | rs_poll()
> >  del_timer_sync()          |  spin_lock() //(2)
> >  (wait timer to stop)      |  ...
> >
> > We hold timer_lock in position (1) of thread 1 and
> > use del_timer_sync() to wait timer to stop, but timer handler
> > also need timer_lock in position (2) of thread 2.
> > As a result, rs_close() will block forever.
> 
> I agree with this.
> 
> > This patch extracts del_timer_sync() from the protection of
> > spin_lock_bh(), which could let timer handler to obtain
> > the needed lock.
> 
> Looking at the timer_lock I don't really understand what it protects.
> It looks like it is not needed at all.

There is no race condition between rs_close and rs_poll(timer handler),
I think we could remove the timer_lock in rs_close(), rs_open() and rs_poll().

> Also, I see that rs_poll rewinds the timer regardless of whether del_timer_sync
> was called or not, which violates del_timer_sync requirements.

I wrote a kernel module to test whether del_timer_sync() could finish a timer handler
that use mod_timer() to rewind itself. The following is the result.

# insmod del_timer_sync.ko 
[  929.374405] my_timer will be create.
[  929.374738] the jiffies is :4295595572
[  930.411581] In my_timer_function
[  930.411956] the jiffies is 4295596609
[  935.466643] In my_timer_function
[  935.467505] the jiffies is 4295601665
[  940.586538] In my_timer_function
[  940.586916] the jiffies is 4295606784
[  945.706579] In my_timer_function
[  945.706885] the jiffies is 4295611904

# 
# rmmod del_timer_sync.ko
[  948.507692] the del_timer_sync is :1
[  948.507692] 
# 
# 

The result of the experiment shows that the timer handler could
be killed after we execute del_timer_sync().

> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> > ---
> >  arch/xtensa/platforms/iss/console.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/xtensa/platforms/iss/console.c b/arch/xtensa/platforms/iss/console.c
> > index 81d7c7e8f7e..d431b61ae3c 100644
> > --- a/arch/xtensa/platforms/iss/console.c
> > +++ b/arch/xtensa/platforms/iss/console.c
> > @@ -51,8 +51,10 @@ static int rs_open(struct tty_struct *tty, struct file * filp)
> >  static void rs_close(struct tty_struct *tty, struct file * filp)
> >  {
> >         spin_lock_bh(&timer_lock);
> > -       if (tty->count == 1)
> > +       if (tty->count == 1) {
> > +               spin_unlock_bh(&timer_lock);
> >                 del_timer_sync(&serial_timer);
> > +       }
> >         spin_unlock_bh(&timer_lock);
> 
> Now in case tty->count == 1 the timer_lock would be unlocked twice.

I will remove the timer_lock in rs_close(), rs_open() and rs_poll().

Thanks a lot for your time and advice!

Best regards,
Duoming Zhou

  reply	other threads:[~2022-04-07 11:06 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07  6:33 [PATCH 00/11] Fix deadlocks caused by del_timer_sync() Duoming Zhou
2022-04-07  6:33 ` [PATCH 01/11] drivers: tty: serial: Fix deadlock in sa1100_set_termios() Duoming Zhou
2022-04-07  7:02   ` Jiri Slaby
2022-04-07 14:03     ` duoming
2022-04-07  6:33 ` [PATCH 02/11] drivers: usb: host: Fix deadlock in oxu_bus_suspend() Duoming Zhou
2022-04-07  8:01   ` Oliver Neukum
2022-04-07 12:02     ` duoming
2022-04-07  6:33 ` [PATCH 03/11] drivers: staging: rtl8192u: Fix deadlock in ieee80211_beacons_stop() Duoming Zhou
2022-04-07  6:33 ` [PATCH 04/11] drivers: staging: rtl8723bs: Fix deadlock in rtw_surveydone_event_callback() Duoming Zhou
2022-04-07  6:36 ` [PATCH 05/11] drivers: staging: rtl8192e: Fix deadlock in rtllib_beacons_stop() Duoming Zhou
2022-04-07  6:36 ` [PATCH 06/11] drivers: staging: rtl8192e: Fix deadlock in rtw_joinbss_event_prehandle() Duoming Zhou
2022-04-07  6:36 ` [PATCH 07/11] drivers: net: hippi: Fix deadlock in rr_close() Duoming Zhou
2022-04-07  6:37 ` [PATCH 08/11] drivers: net: can: Fix deadlock in grcan_close() Duoming Zhou
2022-04-07  6:37 ` [PATCH 09/11] drivers: infiniband: hw: Fix deadlock in irdma_cleanup_cm_core() Duoming Zhou
2022-04-07 11:24   ` Dan Carpenter
2022-04-07 12:54     ` duoming
2022-04-07 14:23       ` Jason Gunthorpe
2022-04-07 14:38         ` duoming
2022-04-07 17:35           ` Saleem, Shiraz
2022-04-07 15:24       ` Dan Carpenter
2022-04-07 17:36         ` Saleem, Shiraz
2022-04-08  0:35           ` duoming
2022-04-08  2:21             ` Saleem, Shiraz
2022-04-08  2:52               ` duoming
2022-04-07  6:37 ` [PATCH 10/11] arch: xtensa: platforms: Fix deadlock in iss_net_close() Duoming Zhou
2022-04-07  6:37 ` [PATCH 11/11] arch: xtensa: platforms: Fix deadlock in rs_close() Duoming Zhou
2022-04-07  7:21   ` Max Filippov
2022-04-07 11:05     ` duoming [this message]
2022-04-07  9:42   ` Sergey Shtylyov
2022-04-07 11:12     ` duoming

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=569b0b8a.3f281.18003b3c5c4.Coremail.duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=alexander.deucher@amd.com \
    --cc=chris@zankel.net \
    --cc=davem@davemloft.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jcmvbkbc@gmail.com \
    --cc=jes@trained-monkey.org \
    --cc=jgg@ziepe.ca \
    --cc=jirislaby@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linma@zju.edu.cn \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-hippi@sunsite.dk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux-xtensa@linux-xtensa.org \
    --cc=mkl@pengutronix.de \
    --cc=mustafa.ismail@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shiraz.saleem@intel.com \
    --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®