From b5aad86710be57833879ac0e8609021828949682 Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Wed, 9 Oct 2013 22:19:06 +0200 Subject: [PATCH] libdm: Fix a data race in dm_pool_{create,destroy}. --- libdm/mm/pool-fast.c | 4 ++++ libdm/mm/pool.c | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/libdm/mm/pool-fast.c b/libdm/mm/pool-fast.c index 2b494d629..edb31a07c 100644 --- a/libdm/mm/pool-fast.c +++ b/libdm/mm/pool-fast.c @@ -62,7 +62,9 @@ struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint) while (new_size < p->chunk_size) new_size <<= 1; p->chunk_size = new_size; + pthread_mutex_lock(&_dm_pools_mutex); dm_list_add(&_dm_pools, &p->list); + pthread_mutex_unlock(&_dm_pools_mutex); return p; } @@ -77,7 +79,9 @@ void dm_pool_destroy(struct dm_pool *p) c = pr; } + pthread_mutex_lock(&_dm_pools_mutex); dm_list_del(&p->list); + pthread_mutex_unlock(&_dm_pools_mutex); dm_free(p); } diff --git a/libdm/mm/pool.c b/libdm/mm/pool.c index fd08307f6..ef006a444 100644 --- a/libdm/mm/pool.c +++ b/libdm/mm/pool.c @@ -15,9 +15,10 @@ #include "dmlib.h" #include +#include -/* FIXME: thread unsafe */ static DM_LIST_INIT(_dm_pools); +static pthread_mutex_t _dm_pools_mutex = PTHREAD_MUTEX_INITIALIZER; void dm_pools_check_leaks(void); #ifdef DEBUG_ENFORCE_POOL_LOCKING @@ -81,8 +82,11 @@ void dm_pools_check_leaks(void) { struct dm_pool *p; - if (dm_list_empty(&_dm_pools)) + pthread_mutex_lock(&_dm_pools_mutex); + if (dm_list_empty(&_dm_pools)) { + pthread_mutex_unlock(&_dm_pools_mutex); return; + } log_error("You have a memory leak (not released memory pool):"); dm_list_iterate_items(p, &_dm_pools) { @@ -94,6 +98,7 @@ void dm_pools_check_leaks(void) log_error(" [%p] %s", p, p->name); #endif } + pthread_mutex_unlock(&_dm_pools_mutex); log_error(INTERNAL_ERROR "Unreleased memory pool(s) found."); }