public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] mtdblock: introduce mtdblks_lock
@ 2009-07-14 20:04 Matthias Kaehlcke
  2009-07-15  7:41 ` Artem Bityutskiy
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Kaehlcke @ 2009-07-14 20:04 UTC (permalink / raw)
  To: dwmw2; +Cc: linux-mtd

The mtdblks array and its content are prone to race conditions. Introduce
the mutex mtdblks_lock in order to solve this.

Signed-off-by: Matthias Kaehlcke <matthias@kaehlcke.net>
---
 drivers/mtd/mtdblock.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c
index 208c6fa..ca0c6b8 100644
--- a/drivers/mtd/mtdblock.c
+++ b/drivers/mtd/mtdblock.c
@@ -29,6 +29,8 @@ static struct mtdblk_dev {
 	enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
 } *mtdblks[MAX_MTD_DEVICES];
 
+static struct mutex mtdblks_lock;
+
 /*
  * Cache stuff...
  *
@@ -270,6 +272,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
 
 	DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
 
+	mutex_lock(&mtdblks_lock);
+
 	if (mtdblks[dev]) {
 		mtdblks[dev]->count++;
 		return 0;
@@ -277,8 +281,10 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
 
 	/* OK, it's not open. Create cache info for it */
 	mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
-	if (!mtdblk)
+	if (!mtdblk) {
+		mutex_unlock(&mtdblks_lock);
 		return -ENOMEM;
+	}
 
 	mtdblk->count = 1;
 	mtdblk->mtd = mtd;
@@ -292,6 +298,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
 
 	mtdblks[dev] = mtdblk;
 
+	mutex_unlock(&mtdblks_lock);
+
 	DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
 
 	return 0;
@@ -304,6 +312,8 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
 
    	DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
 
+	mutex_lock(&mtdblks_lock);
+
 	mutex_lock(&mtdblk->cache_mutex);
 	write_cached_data(mtdblk);
 	mutex_unlock(&mtdblk->cache_mutex);
@@ -316,6 +326,9 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
 		vfree(mtdblk->cache_data);
 		kfree(mtdblk);
 	}
+
+	mutex_unlock(&mtdblks_lock);
+
 	DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
 
 	return 0;
@@ -376,6 +389,8 @@ static struct mtd_blktrans_ops mtdblock_tr = {
 
 static int __init init_mtdblock(void)
 {
+	mutex_init(&mtdblks_lock);
+
 	return register_mtd_blktrans(&mtdblock_tr);
 }
 
-- 
1.6.3.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtdblock: introduce mtdblks_lock
  2009-07-14 20:04 [PATCH] mtdblock: introduce mtdblks_lock Matthias Kaehlcke
@ 2009-07-15  7:41 ` Artem Bityutskiy
  2009-07-15  8:36   ` Matthias Kaehlcke
  0 siblings, 1 reply; 5+ messages in thread
From: Artem Bityutskiy @ 2009-07-15  7:41 UTC (permalink / raw)
  To: Matthias Kaehlcke; +Cc: linux-mtd, dwmw2

On Tue, 2009-07-14 at 22:04 +0200, Matthias Kaehlcke wrote:
> +static struct mutex mtdblks_lock;
> +
>  /*
>   * Cache stuff...
>   *
> @@ -270,6 +272,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
>  
>  	DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
>  
> +	mutex_lock(&mtdblks_lock);
> +
>  	if (mtdblks[dev]) {
>  		mtdblks[dev]->count++;
>  		return 0;

Now about unlocking the mutex here?

-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtdblock: introduce mtdblks_lock
  2009-07-15  7:41 ` Artem Bityutskiy
@ 2009-07-15  8:36   ` Matthias Kaehlcke
  2009-07-15  8:39     ` Artem Bityutskiy
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Kaehlcke @ 2009-07-15  8:36 UTC (permalink / raw)
  To: Artem Bityutskiy; +Cc: linux-mtd, dwmw2

El Wed, Jul 15, 2009 at 10:41:54AM +0300 Artem Bityutskiy ha dit:

> On Tue, 2009-07-14 at 22:04 +0200, Matthias Kaehlcke wrote:
> > +static struct mutex mtdblks_lock;
> > +
> >  /*
> >   * Cache stuff...
> >   *
> > @@ -270,6 +272,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
> >  
> >  	DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
> >  
> > +	mutex_lock(&mtdblks_lock);
> > +
> >  	if (mtdblks[dev]) {
> >  		mtdblks[dev]->count++;
> >  		return 0;
> 
> Now about unlocking the mutex here?

i missed this one ...

thanks for your review. i'm going fix that and send out an updated
version of the patch

-- 
Matthias Kaehlcke
Embedded Linux Engineer
Barcelona

        They that can give up essential liberty to obtain a little
           temporary safety deserve neither liberty nor safety
                         (Benjamin Franklin)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtdblock: introduce mtdblks_lock
  2009-07-15  8:36   ` Matthias Kaehlcke
@ 2009-07-15  8:39     ` Artem Bityutskiy
  2009-07-15  9:15       ` Matthias Kaehlcke
  0 siblings, 1 reply; 5+ messages in thread
From: Artem Bityutskiy @ 2009-07-15  8:39 UTC (permalink / raw)
  To: Matthias Kaehlcke; +Cc: linux-mtd, dwmw2

On Wed, 2009-07-15 at 10:36 +0200, Matthias Kaehlcke wrote:
> El Wed, Jul 15, 2009 at 10:41:54AM +0300 Artem Bityutskiy ha dit:
> 
> > On Tue, 2009-07-14 at 22:04 +0200, Matthias Kaehlcke wrote:
> > > +static struct mutex mtdblks_lock;
> > > +
> > >  /*
> > >   * Cache stuff...
> > >   *
> > > @@ -270,6 +272,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
> > >  
> > >  	DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
> > >  
> > > +	mutex_lock(&mtdblks_lock);
> > > +
> > >  	if (mtdblks[dev]) {
> > >  		mtdblks[dev]->count++;
> > >  		return 0;
> > 
> > Now about unlocking the mutex here?
> 
> i missed this one ...
> 
> thanks for your review. i'm going fix that and send out an updated
> version of the patch

Err, I've already amended your patch and put it to my l2-mtd-2.6.git:
http://git.infradead.org/users/dedekind/l2-mtd-2.6.git?a=commit;h=2a4d4fdb6393fdcbfea546b2474baf41d434302a

-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mtdblock: introduce mtdblks_lock
  2009-07-15  8:39     ` Artem Bityutskiy
@ 2009-07-15  9:15       ` Matthias Kaehlcke
  0 siblings, 0 replies; 5+ messages in thread
From: Matthias Kaehlcke @ 2009-07-15  9:15 UTC (permalink / raw)
  To: Artem Bityutskiy; +Cc: linux-mtd, dwmw2

El Wed, Jul 15, 2009 at 11:39:22AM +0300 Artem Bityutskiy ha dit:

> On Wed, 2009-07-15 at 10:36 +0200, Matthias Kaehlcke wrote:
> > El Wed, Jul 15, 2009 at 10:41:54AM +0300 Artem Bityutskiy ha dit:
> > 
> > > On Tue, 2009-07-14 at 22:04 +0200, Matthias Kaehlcke wrote:
> > > > +static struct mutex mtdblks_lock;
> > > > +
> > > >  /*
> > > >   * Cache stuff...
> > > >   *
> > > > @@ -270,6 +272,8 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
> > > >  
> > > >  	DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
> > > >  
> > > > +	mutex_lock(&mtdblks_lock);
> > > > +
> > > >  	if (mtdblks[dev]) {
> > > >  		mtdblks[dev]->count++;
> > > >  		return 0;
> > > 
> > > Now about unlocking the mutex here?
> > 
> > i missed this one ...
> > 
> > thanks for your review. i'm going fix that and send out an updated
> > version of the patch
> 
> Err, I've already amended your patch and put it to my l2-mtd-2.6.git:
> http://git.infradead.org/users/dedekind/l2-mtd-2.6.git?a=commit;h=2a4d4fdb6393fdcbfea546b2474baf41d434302a

ok, thx! 

-- 
Matthias Kaehlcke
Embedded Linux Engineer
Barcelona

             If liberty means anything at all, it means the
           right to tell people what they do not want to hear
                            (George Orwell)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-15  9:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-14 20:04 [PATCH] mtdblock: introduce mtdblks_lock Matthias Kaehlcke
2009-07-15  7:41 ` Artem Bityutskiy
2009-07-15  8:36   ` Matthias Kaehlcke
2009-07-15  8:39     ` Artem Bityutskiy
2009-07-15  9:15       ` Matthias Kaehlcke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox