From fba5c5ff89163062922c3e560e871c087f2177c3 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 29 Jan 2013 22:29:09 +0100 Subject: [PATCH] smalloc: alloc failure cleanups Signed-off-by: Jens Axboe --- cgroup.c | 18 ++++++++++++++++++ diskutil.c | 5 +++++ fio.c | 8 -------- flow.c | 29 ++++++++++++++++++++++++++--- gettime-thread.c | 6 ++++-- verify.c | 1 - 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/cgroup.c b/cgroup.c index 86d4d5ea..34b61ded 100644 --- 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) diff --git a/diskutil.c b/diskutil.c index 3681dde5..e29d1c34 100644 --- a/diskutil.c +++ b/diskutil.c @@ -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 a4087273..af4c12cb 100644 --- a/fio.c +++ b/fio.c @@ -26,15 +26,7 @@ #include #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 2993f4e8..b7a2fb12 100644 --- 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); } diff --git a/gettime-thread.c b/gettime-thread.c index da409042..c1b4b096 100644 --- a/gettime-thread.c +++ b/gettime-thread.c @@ -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) diff --git a/verify.c b/verify.c index cb13b629..daa2f04a 100644 --- 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" -- 2.25.1