2015-05-08 00:26:58

by Brian Norris

[permalink] [raw]
Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote:
> On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote:
> > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera wrote:
> > > @@ -484,7 +486,7 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
> > > if (old->open) {
> > > if (old->tr->release)
> > > old->tr->release(old);
> > > - __put_mtd_device(old->mtd);
> > > + put_mtd_device(old->mtd);
> >
> > This looks wrong. See:
> [...]
> > deregister_mtd_blktrans()
> > |_ mutex_lock(&mtd_table_mutex)
> > |_ tr->remove_dev() -> inftl_remove_dev()
> > |_ del_mtd_blktrans_dev()
> > |_ put_mtd_device()
> > |_ mutex_lock(&mtd_table_mutex) <--- AA deadlock
>
> What's more, this code in del_mtd_blktrans_dev() makes it obvious that
> this hunk is wrong:
>
> int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
> {
> unsigned long flags;
>
> if (mutex_trylock(&mtd_table_mutex)) {
> mutex_unlock(&mtd_table_mutex);
> BUG();
> }
> ...
>
> So rather than a comment, the code is showing that it's a BUG() to not
> be holding mtd_table_mutex already.

As an alternative to your patch, how about the following?

BTW, this does still leave a usecount race in
drivers/mtd/maps/vmu-flash.c. But that driver should really be using
mtd->_get_device(), if it actually wants its own refcount.

Signed-off-by: Brian Norris <[email protected]>
---

diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index 2b0c52870999..df7c6c70757a 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -197,6 +197,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/

mutex_lock(&dev->lock);
+ mutex_lock(&mtd_table_mutex);

if (dev->open)
goto unlock;
@@ -220,6 +221,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)

unlock:
dev->open++;
+ mutex_unlock(&mtd_table_mutex);
mutex_unlock(&dev->lock);
blktrans_dev_put(dev);
return ret;
@@ -230,6 +232,7 @@ error_release:
error_put:
module_put(dev->tr->owner);
kref_put(&dev->ref, blktrans_dev_release);
+ mutex_unlock(&mtd_table_mutex);
mutex_unlock(&dev->lock);
blktrans_dev_put(dev);
return ret;
@@ -243,6 +246,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode)
return;

mutex_lock(&dev->lock);
+ mutex_lock(&mtd_table_mutex);

if (--dev->open)
goto unlock;
@@ -256,6 +260,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode)
__put_mtd_device(dev->mtd);
}
unlock:
+ mutex_unlock(&mtd_table_mutex);
mutex_unlock(&dev->lock);
blktrans_dev_put(dev);
}


2015-05-11 07:45:09

by Giuseppe Cantavenera

[permalink] [raw]
Subject: RE: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

> -----Original Message-----
> From: ext Brian Norris [mailto:[email protected]]
> Sent: Friday, May 08, 2015 2:27 AM
> To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> DE/Ulm); [email protected]; [email protected]; Sverdlin,
> Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> [email protected]; [email protected]
> Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd-
> >usecount
>
> On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote:
> > On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote:
> > > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera
> wrote:
> > > > @@ -484,7 +486,7 @@ int del_mtd_blktrans_dev(struct
> mtd_blktrans_dev *old)
> > > > if (old->open) {
> > > > if (old->tr->release)
> > > > old->tr->release(old);
> > > > - __put_mtd_device(old->mtd);
> > > > + put_mtd_device(old->mtd);
> > >
> > > This looks wrong. See:
> > [...]
> > > deregister_mtd_blktrans()
> > > |_ mutex_lock(&mtd_table_mutex)
> > > |_ tr->remove_dev() -> inftl_remove_dev()
> > > |_ del_mtd_blktrans_dev()
> > > |_ put_mtd_device()
> > > |_ mutex_lock(&mtd_table_mutex) <--- AA deadlock
> >
> > What's more, this code in del_mtd_blktrans_dev() makes it obvious
> that
> > this hunk is wrong:
> >
> > int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
> > {
> > unsigned long flags;
> >
> > if (mutex_trylock(&mtd_table_mutex)) {
> > mutex_unlock(&mtd_table_mutex);
> > BUG();
> > }
> > ...
> >
> > So rather than a comment, the code is showing that it's a BUG() to
> not
> > be holding mtd_table_mutex already.
>

Hello,
Thanks for your comments and for pointing this out.
Definitely yes.. we shouldn't change del_mtd_blktrans_dev().

> As an alternative to your patch, how about the following?

I think it's the right way to go now.

Thanks!
Giuseppe

2015-05-11 22:25:17

by Brian Norris

[permalink] [raw]
Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

On Mon, May 11, 2015 at 07:44:26AM +0000, Cantavenera, Giuseppe (EXT-Other - DE/Ulm) wrote:
> > -----Original Message-----
> > From: ext Brian Norris [mailto:[email protected]]
> > Sent: Friday, May 08, 2015 2:27 AM
> > To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> > Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> > DE/Ulm); [email protected]; [email protected]; Sverdlin,
> > Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> > [email protected]; [email protected]
> > Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd-
> > >usecount
> >
> > On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote:
> > > On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote:
> > > > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera
> > wrote:
> > > > > @@ -484,7 +486,7 @@ int del_mtd_blktrans_dev(struct
> > mtd_blktrans_dev *old)
> > > > > if (old->open) {
> > > > > if (old->tr->release)
> > > > > old->tr->release(old);
> > > > > - __put_mtd_device(old->mtd);
> > > > > + put_mtd_device(old->mtd);
> > > >
> > > > This looks wrong. See:
> > > [...]
> > > > deregister_mtd_blktrans()
> > > > |_ mutex_lock(&mtd_table_mutex)
> > > > |_ tr->remove_dev() -> inftl_remove_dev()
> > > > |_ del_mtd_blktrans_dev()
> > > > |_ put_mtd_device()
> > > > |_ mutex_lock(&mtd_table_mutex) <--- AA deadlock
> > >
> > > What's more, this code in del_mtd_blktrans_dev() makes it obvious
> > that
> > > this hunk is wrong:
> > >
> > > int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
> > > {
> > > unsigned long flags;
> > >
> > > if (mutex_trylock(&mtd_table_mutex)) {
> > > mutex_unlock(&mtd_table_mutex);
> > > BUG();
> > > }
> > > ...
> > >
> > > So rather than a comment, the code is showing that it's a BUG() to
> > not
> > > be holding mtd_table_mutex already.
> >
>
> Hello,
> Thanks for your comments and for pointing this out.
> Definitely yes.. we shouldn't change del_mtd_blktrans_dev().
>
> > As an alternative to your patch, how about the following?
>
> I think it's the right way to go now.

Can I get a 'Tested-by', or at least an 'Acked-by' for the patch? I
tested it, but I don't think I can reproduce your original problem very
easily.

Brian

2015-05-12 06:39:12

by Giuseppe Cantavenera

[permalink] [raw]
Subject: RE: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

> -----Original Message-----
> From: ext Brian Norris [mailto:[email protected]]
> Sent: Tuesday, May 12, 2015 12:25 AM
> To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> DE/Ulm); [email protected]; [email protected]; Sverdlin,
> Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> [email protected]; [email protected]
> Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd-
> >usecount
>
> On Mon, May 11, 2015 at 07:44:26AM +0000, Cantavenera, Giuseppe (EXT-
> Other - DE/Ulm) wrote:
> > > -----Original Message-----
> > > From: ext Brian Norris [mailto:[email protected]]
> > > Sent: Friday, May 08, 2015 2:27 AM
> > > To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> > > Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> > > DE/Ulm); [email protected]; [email protected]; Sverdlin,
> > > Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> > > [email protected]; [email protected]
> > > Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing
> mtd-
> > > >usecount
> > >
> > > On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote:
> > > > On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote:
> > > > > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera
> > > wrote:

> Can I get a 'Tested-by', or at least an 'Acked-by' for the patch? I
> tested it, but I don't think I can reproduce your original problem very
> easily.
>
> Brian

Hello Brian,

We were able to do some long runs last night and as expected
this patch seems to address the issue pretty well.

Thanks,
Giuseppe

Tested-by: Giuseppe Cantavenera <[email protected]>

2015-05-12 16:37:57

by Alexander Sverdlin

[permalink] [raw]
Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

On 08/05/15 02:26, ext Brian Norris wrote:
> As an alternative to your patch, how about the following?
>
> BTW, this does still leave a usecount race in
> drivers/mtd/maps/vmu-flash.c. But that driver should really be using
> mtd->_get_device(), if it actually wants its own refcount.
>
> Signed-off-by: Brian Norris <[email protected]>

Acked-by: Alexander Sverdlin <[email protected]>

> ---
>
> diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
> index 2b0c52870999..df7c6c70757a 100644
> --- a/drivers/mtd/mtd_blkdevs.c
> +++ b/drivers/mtd/mtd_blkdevs.c
> @@ -197,6 +197,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
> return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
>
> mutex_lock(&dev->lock);
> + mutex_lock(&mtd_table_mutex);
>
> if (dev->open)
> goto unlock;
> @@ -220,6 +221,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
>
> unlock:
> dev->open++;
> + mutex_unlock(&mtd_table_mutex);
> mutex_unlock(&dev->lock);
> blktrans_dev_put(dev);
> return ret;
> @@ -230,6 +232,7 @@ error_release:
> error_put:
> module_put(dev->tr->owner);
> kref_put(&dev->ref, blktrans_dev_release);
> + mutex_unlock(&mtd_table_mutex);
> mutex_unlock(&dev->lock);
> blktrans_dev_put(dev);
> return ret;
> @@ -243,6 +246,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode)
> return;
>
> mutex_lock(&dev->lock);
> + mutex_lock(&mtd_table_mutex);
>
> if (--dev->open)
> goto unlock;
> @@ -256,6 +260,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode)
> __put_mtd_device(dev->mtd);
> }
> unlock:
> + mutex_unlock(&mtd_table_mutex);
> mutex_unlock(&dev->lock);
> blktrans_dev_put(dev);
> }

2015-05-12 18:04:49

by Brian Norris

[permalink] [raw]
Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount

On Tue, May 12, 2015 at 06:38:34AM +0000, Cantavenera, Giuseppe (EXT-Other - DE/Ulm) wrote:
> > -----Original Message-----
> > From: ext Brian Norris [mailto:[email protected]]
> > Sent: Tuesday, May 12, 2015 12:25 AM
> > To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> > Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> > DE/Ulm); [email protected]; [email protected]; Sverdlin,
> > Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> > [email protected]; [email protected]
> > Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd-
> > >usecount
> >
> > On Mon, May 11, 2015 at 07:44:26AM +0000, Cantavenera, Giuseppe (EXT-
> > Other - DE/Ulm) wrote:
> > > > -----Original Message-----
> > > > From: ext Brian Norris [mailto:[email protected]]
> > > > Sent: Friday, May 08, 2015 2:27 AM
> > > > To: Cantavenera, Giuseppe (EXT-Other - DE/Ulm)
> > > > Cc: [email protected]; Restelli, Lorenzo (EXT-Other -
> > > > DE/Ulm); [email protected]; [email protected]; Sverdlin,
> > > > Alexander (Nokia - DE/Ulm); zhangxingcai; [email protected];
> > > > [email protected]; [email protected]
> > > > Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing
> > mtd-
> > > > >usecount
> > > >
> > > > On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote:
> > > > > On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote:
> > > > > > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera
> > > > wrote:
>
> > Can I get a 'Tested-by', or at least an 'Acked-by' for the patch? I
> > tested it, but I don't think I can reproduce your original problem very
> > easily.
> >
> > Brian
>
> Hello Brian,
>
> We were able to do some long runs last night and as expected
> this patch seems to address the issue pretty well.
>
> Thanks,
> Giuseppe
>
> Tested-by: Giuseppe Cantavenera <[email protected]>

Great, thanks to all of you. I reworked the patch description and
applied this to l2-mtd.git.

Brian