smalloc: alloc failure cleanups
authorJens Axboe <axboe@kernel.dk>
Tue, 29 Jan 2013 21:29:09 +0000 (22:29 +0100)
committerJens Axboe <axboe@kernel.dk>
Tue, 29 Jan 2013 21:29:09 +0000 (22:29 +0100)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
cgroup.c
diskutil.c
fio.c
flow.c
gettime-thread.c
verify.c

index 86d4d5e..34b61de 100644 (file)
--- a/cgroup.c
+++ b/cgroup.c
@@ -52,9 +52,22 @@ static void add_cgroup(struct thread_data *td, const char *name,
 {
        struct cgroup_member *cm;
 
+       if (!lock)
+               return;
+
        cm = smalloc(sizeof(*cm));
+       if (!cm) {
+err:
+               log_err("fio: failed to allocate cgroup member\n");
+               return;
+       }
+
        INIT_FLIST_HEAD(&cm->list);
        cm->root = smalloc_strdup(name);
+       if (!cm->root) {
+               sfree(cm);
+               goto err;
+       }
        if (td->o.cgroup_nodelete)
                cm->cgroup_nodelete = 1;
        fio_mutex_down(lock);
@@ -67,6 +80,9 @@ void cgroup_kill(struct flist_head *clist)
        struct flist_head *n, *tmp;
        struct cgroup_member *cm;
 
+       if (!lock)
+               return;
+
        fio_mutex_down(lock);
 
        flist_for_each_safe(n, tmp, clist) {
@@ -183,6 +199,8 @@ void cgroup_shutdown(struct thread_data *td, char **mnt)
 static void fio_init cgroup_init(void)
 {
        lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+       if (!lock)
+               log_err("fio: failed to allocate cgroup lock\n");
 }
 
 static void fio_exit cgroup_exit(void)
index 3681dde..e29d1c3 100644 (file)
@@ -281,6 +281,11 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
        dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
 
        du = smalloc(sizeof(*du));
+       if (!du) {
+               log_err("fio: smalloc() pool exhausted\n");
+               return NULL;
+       }
+
        memset(du, 0, sizeof(*du));
        INIT_FLIST_HEAD(&du->list);
        l = snprintf(du->path, sizeof(du->path), "%s/stat", path);
diff --git a/fio.c b/fio.c
index a408727..af4c12c 100644 (file)
--- a/fio.c
+++ b/fio.c
 #include <time.h>
 
 #include "fio.h"
-#include "hash.h"
 #include "smalloc.h"
-#include "verify.h"
-#include "trim.h"
-#include "diskutil.h"
-#include "profile.h"
-#include "lib/rand.h"
-#include "memalign.h"
-#include "server.h"
 
 uintptr_t page_mask;
 uintptr_t page_size;
diff --git a/flow.c b/flow.c
index 2993f4e..b7a2fb1 100644 (file)
--- a/flow.c
+++ b/flow.c
@@ -39,6 +39,9 @@ static struct fio_flow *flow_get(unsigned int id)
        struct fio_flow *flow = NULL;
        struct flist_head *n;
 
+       if (!flow_lock)
+               return NULL;
+
        fio_mutex_down(flow_lock);
 
        flist_for_each(n, flow_list) {
@@ -51,6 +54,10 @@ static struct fio_flow *flow_get(unsigned int id)
 
        if (!flow) {
                flow = smalloc(sizeof(*flow));
+               if (!flow) {
+                       log_err("fio: smalloc pool exhausted\n");
+                       return NULL;
+               }
                flow->refs = 0;
                INIT_FLIST_HEAD(&flow->list);
                flow->id = id;
@@ -66,6 +73,9 @@ static struct fio_flow *flow_get(unsigned int id)
 
 static void flow_put(struct fio_flow *flow)
 {
+       if (!flow_lock)
+               return;
+
        fio_mutex_down(flow_lock);
 
        if (!--flow->refs) {
@@ -92,13 +102,26 @@ void flow_exit_job(struct thread_data *td)
 
 void flow_init(void)
 {
-       flow_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
        flow_list = smalloc(sizeof(*flow_list));
+       if (!flow_list) {
+               log_err("fio: smalloc pool exhausted\n");
+               return;
+       }
+
+       flow_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+       if (!flow_lock) {
+               log_err("fio: failed to allocate flow lock\n");
+               sfree(flow_list);
+               return;
+       }
+
        INIT_FLIST_HEAD(flow_list);
 }
 
 void flow_exit(void)
 {
-       fio_mutex_remove(flow_lock);
-       sfree(flow_list);
+       if (flow_lock)
+               fio_mutex_remove(flow_lock);
+       if (flow_list)
+               sfree(flow_list);
 }
index da40904..c1b4b09 100644 (file)
@@ -14,12 +14,14 @@ static pthread_t gtod_thread;
 void fio_gtod_init(void)
 {
        fio_tv = smalloc(sizeof(struct timeval));
-       assert(fio_tv);
+       if (!fio_tv)
+               log_err("fio: smalloc pool exhausted\n");
 }
 
 static void fio_gtod_update(void)
 {
-       gettimeofday(fio_tv, NULL);
+       if (fio_tv)
+               gettimeofday(fio_tv, NULL);
 }
 
 static void *gtod_thread_main(void *data)
index cb13b62..daa2f04 100644 (file)
--- a/verify.c
+++ b/verify.c
@@ -10,7 +10,6 @@
 
 #include "fio.h"
 #include "verify.h"
-#include "smalloc.h"
 #include "trim.h"
 #include "lib/rand.h"
 #include "lib/hweight.h"