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 86d4d5ea3b4331c1758561249b8583bce1e2ecc8..34b61ded08d0fa5e22527012c979e31163b1b8ec 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 3681dde5d8cedb8190e4dcbd61f60274bba76179..e29d1c34af24d116f9a601437adb798a8ec194cf 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 a408727399d388dc4ba5f993163cf13552eb0f80..af4c12cbe1815f7119511a6bdb44b39421c85161 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 2993f4e8f8c48dbdc86ad25afc0653fcef31a481..b7a2fb12287332eb01d7fe48cf0f0ad5eb7902dc 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 da409042f2ec6eb7344b6f943dad0eba84c54cd1..c1b4b0967c28f354b8a3e6c9ad82fc2845c41e56 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 cb13b629be29886b5dbfa4318102c7b05e7c1ca6..daa2f04a7419146dbf98b74b68fcf432860a0abe 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"