Skip to contentSkip to conversation

dm-raid1.c fix a race bug in __rh_alloc()

PATCH In review Zhao Qian 4 participants 4 messages Jun 17, 2005 – Jun 29, 2005 Device mapper

lore
Share & tools
lore

Zhao Qian

author
#0 lore raw

PATCH 1/1 dm-raid1.c fix a race bug in __rh_alloc()

after write_unlock_irq and just before read_lock, there's a small window which enables a race causing deletion of the region struct in function rh_update_states(). then in rh_dec(), the __rh_lookup() will return null, causing kernel panic.

[root@darkstar md]# diff -u dm-raid1.orig.c dm-raid1.c

dm-raid1.c 2005-06-17 10:02:04.000000000 +0800 renamed from dm-raid1.orig.c 2005-06-16 14:17:04.000000000 +0800 +6 -5
@@ -252,15 +252,16 @@
 
        else {
                __rh_insert(rh, nreg);
-               if (nreg->state == RH_CLEAN) {
-                       spin_lock(&rh->region_lock);
-                       list_add(&nreg->list, &rh->clean_regions);
-                       spin_unlock(&rh->region_lock);
-               }
                reg = nreg;
        }
        write_unlock_irq(&rh->hash_lock);
        read_lock(&rh->hash_lock);
+       if (reg->state == RH_CLEAN) {
+               spin_lock(&rh->region_lock);
+               if ( list_empty(&reg->list) )
+                       list_add(&reg->list, &rh->clean_regions);
+               spin_unlock(&rh->region_lock);
+       }
 
        return reg;
 }

Sincerely,
Johnson <[email protected]>
AiM9 <[email protected]>

#1 lore raw

I believe this also fixes Jun'ichi's issue ([dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

brassow

Quoted 29 lines · Zhao Qian

after write_unlock_irq and just before read_lock, there's a small window which enables a race causing deletion of the region struct in function rh_update_states(). then in rh_dec(), the __rh_lookup() will return null, causing kernel panic.

[root@darkstar md]# diff -u dm-raid1.orig.c dm-raid1.c

dm-raid1.c 2005-06-17 10:02:04.000000000 +0800 renamed from dm-raid1.orig.c 2005-06-16 14:17:04.000000000 +0800 +6 -5
@@ -252,15 +252,16 @@
 
        else {
                __rh_insert(rh, nreg);
-               if (nreg->state == RH_CLEAN) {
-                       spin_lock(&rh->region_lock);
-                       list_add(&nreg->list, &rh->clean_regions);
-                       spin_unlock(&rh->region_lock);
-               }
                reg = nreg;
        }
        write_unlock_irq(&rh->hash_lock);
        read_lock(&rh->hash_lock);
+       if (reg->state == RH_CLEAN) {
+               spin_lock(&rh->region_lock);
+               if ( list_empty(&reg->list) )
+                       list_add(&reg->list, &rh->clean_regions);
+               spin_unlock(&rh->region_lock);
+       }
 
        return reg;
 }
Quoted 9 lines

Sincerely,
Johnson <[email protected]>
AiM9 <[email protected]>

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

I believe this also fixes Jun'ichi's issue (<bold>[dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

brassow

</bold>

On Jun 16, 2005, at 9:21 PM, Zhao Qian wrote:

<excerpt>after write_unlock_irq and just before read_lock, there's a small window which enables a race causing deletion of the region struct in function rh_update_states(). then in rh_dec(), the __rh_lookup() will return null, causing kernel panic.

[root@darkstar md]# diff -u dm-raid1.orig.c dm-raid1.c

--- dm-raid1.orig.c 2005-06-16 14:17:04.000000000 +0800

+++ dm-raid1.c 2005-06-17 10:02:04.000000000 +0800

@@ -252,15 +252,16 @@

        else {

                __rh_insert(rh, nreg);

- if (nreg->state == RH_CLEAN) {

- spin_lock(&rh->region_lock);

- list_add(&nreg->list, &rh->clean_regions);

- spin_unlock(&rh->region_lock);

- }

                reg = nreg;

        }

        write_unlock_irq(&rh->hash_lock);

        read_lock(&rh->hash_lock);

+ if (reg->state == RH_CLEAN) {

+ spin_lock(&rh->region_lock);

+ if ( list_empty(&reg->list) )

+ list_add(&reg->list, &rh->clean_regions);

+ spin_unlock(&rh->region_lock);

+ }

        return reg;

}

Sincerely,

Johnson <<[email protected]>

AiM9 <<[email protected]>

--

dm-devel mailing list

[email protected]

https://www.redhat.com/mailman/listinfo/dm-devel

</excerpt>

#2 lore raw

Hi,

Quoted 2 lines · Jonathan E Brassow

I believe this also fixes Jun'ichi's issue (*[dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

Unfortunately, it doesn't eliminate the possibility of the region being freed during lock conversion (though the possibility becomes very very low) and also it will cause other problems.

I modified his patch with fixes below
- spin_lock should be spin_lock_irq to avoid deadlock,
- needs to check pending count to avoid moving dirty region to clean list,
and added retry code.

This one should work. How about this?

However, I think we need to find smarter fix not to depend on retry.

Thanks,

Quoted 10 lines · Jonathan E Brassow

I believe this also fixes Jun'ichi's issue (*[dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

brassow
*
On Jun 16, 2005, at 9:21 PM, Zhao Qian wrote:

after write_unlock_irq and just before read_lock, there's a small window which enables a race causing deletion of the region struct in function rh_update_states(). then in rh_dec(), the __rh_lookup() will return null, causing kernel panic.

drivers/md/dm-raid1.c +10 -5
@@ -232,6 +232,7 @@ static struct region *__rh_alloc(struct 
 {
 	struct region *reg, *nreg;
 
+retry:
 	read_unlock(&rh->hash_lock);
 	nreg = mempool_alloc(rh->region_pool, GFP_NOIO);
 	nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
@@ -252,15 +253,19 @@ static struct region *__rh_alloc(struct 
 
 	else {
 		__rh_insert(rh, nreg);
-		if (nreg->state == RH_CLEAN) {
-			spin_lock(&rh->region_lock);
-			list_add(&nreg->list, &rh->clean_regions);
-			spin_unlock(&rh->region_lock);
-		}
 		reg = nreg;
 	}
 	write_unlock_irq(&rh->hash_lock);
 	read_lock(&rh->hash_lock);
+	/* rare, but it's possible the region is freed by other cpu */
+	if (reg != __rh_lookup(rh, region))
+		goto retry;
+	if (reg->state == RH_CLEAN) {
+		spin_lock_irq(&rh->region_lock);
+		if (!atomic_read(&reg->pending) && list_empty(&reg->list))
+			list_add(&reg->list, &rh->clean_regions);
+		spin_unlock_irq(&rh->region_lock);
+	}
 
 	return reg;
 }
#3 lore raw

the problem lies on that there's a small window between the write_unlock and read_lock. for a smarter fix mentioned by Jun'ichi, I think we need a downgradable rw lock to eliminate the race window.

----- Original Message -----
From: "Jun'ichi Nomura" <[email protected]>
To: "device-mapper development" <[email protected]>
Sent: Tuesday, June 28, 2005 9:46 PM
Subject: Re: [dm-devel] [patch] dm-raid1.c fix a race bug in __rh_alloc()

Quoted 6 lines

Hi,

Quoted 2 lines · Jonathan E Brassow

I believe this also fixes Jun'ichi's issue (*[dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

Quoted 7 lines

Unfortunately, it doesn't eliminate the possibility of the region being freed during lock conversion (though the possibility becomes very very low) and also it will cause other problems.

I modified his patch with fixes below
- spin_lock should be spin_lock_irq to avoid deadlock,
- needs to check pending count to avoid moving dirty region to clean

list,

Quoted 22 lines

and added retry code.

This one should work. How about this?

However, I think we need to find smarter fix not to depend on retry.

Thanks,

Quoted 11 lines · Jonathan E Brassow

I believe this also fixes Jun'ichi's issue (*[dm-devel] [PATCH] 2.6.12-rc6: fix __rh_alloc()/rh_update_states() race in dm-raid1.c)

brassow
*
On Jun 16, 2005, at 9:21 PM, Zhao Qian wrote:

after write_unlock_irq and just before read_lock, there's a small window which enables a race causing deletion of the region struct in function rh_update_states(). then in rh_dec(), the __rh_lookup() will return null, causing kernel panic.

----------------------------------------------------------------------------
----

Quoted 27 lines
drivers/md/dm-raid1.c +3 -5
@@ -232,6 +232,7 @@ static struct region *__rh_alloc(struct
 {
  struct region *reg, *nreg;
 
+retry:
  read_unlock(&rh->hash_lock);
  nreg = mempool_alloc(rh->region_pool, GFP_NOIO);
  nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
@@ -252,15 +253,19 @@ static struct region *__rh_alloc(struct
 
  else {
  __rh_insert(rh, nreg);
- if (nreg->state == RH_CLEAN) {
- spin_lock(&rh->region_lock);
- list_add(&nreg->list, &rh->clean_regions);
- spin_unlock(&rh->region_lock);
- }
  reg = nreg;
  }
  write_unlock_irq(&rh->hash_lock);
  read_lock(&rh->hash_lock);
+ /* rare, but it's possible the region is freed by other cpu */
+ if (reg != __rh_lookup(rh, region))

drivers/md/dm-raid1.c static struct region *__rh_alloc(struct · line 271

Quoted 10 lines
drivers/md/dm-raid1.c +7 -0
+ goto retry;
+ if (reg->state == RH_CLEAN) {
+ spin_lock_irq(&rh->region_lock);
+ if (!atomic_read(&reg->pending) && list_empty(&reg->list))
+ list_add(&reg->list, &rh->clean_regions);
+ spin_unlock_irq(&rh->region_lock);
+ }
 
  return reg;
 }

----------------------------------------------------------------------------
----

Quoted 4 lines

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel