smalloc: Remove read/write lock race condition which will result in segmentation...
authorShaozhi Shawn Ye <yeshao@google.com>
Wed, 10 Sep 2008 07:02:51 +0000 (09:02 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 10 Sep 2008 07:02:51 +0000 (09:02 +0200)
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
smalloc.c

index 1c975df47ae3f6a6fcf6088ed1bc25e679e9eb2b..7f3c10b137498d01f812e4807298b2f2b06e6870 100644 (file)
--- a/smalloc.c
+++ b/smalloc.c
@@ -226,9 +226,7 @@ static int add_pool(struct pool *pool, unsigned int alloc_size)
 
        pool->fd = fd;
 
 
        pool->fd = fd;
 
-       global_write_lock();
        nr_pools++;
        nr_pools++;
-       global_write_unlock();
        return 0;
 out_unlink:
        fprintf(stderr, "smalloc: failed adding pool\n");
        return 0;
 out_unlink:
        fprintf(stderr, "smalloc: failed adding pool\n");
@@ -442,7 +440,7 @@ void *smalloc(unsigned int size)
 {
        unsigned int i;
 
 {
        unsigned int i;
 
-       global_read_lock();
+       global_write_lock();
        i = last_pool;
 
        do {
        i = last_pool;
 
        do {
@@ -451,7 +449,7 @@ void *smalloc(unsigned int size)
 
                        if (ptr) {
                                last_pool = i;
 
                        if (ptr) {
                                last_pool = i;
-                               global_read_unlock();
+                               global_write_unlock();
                                return ptr;
                        }
                }
                                return ptr;
                        }
                }
@@ -464,15 +462,13 @@ void *smalloc(unsigned int size)
                        break;
                else {
                        i = nr_pools;
                        break;
                else {
                        i = nr_pools;
-                       global_read_unlock();
                        if (add_pool(&mp[nr_pools], size))
                                goto out;
                        if (add_pool(&mp[nr_pools], size))
                                goto out;
-                       global_read_lock();
                }
        } while (1);
 
                }
        } while (1);
 
-       global_read_unlock();
 out:
 out:
+       global_write_unlock();
        return NULL;
 }
 
        return NULL;
 }