From 1f979f630b19334905ca62913c6f31b2b7c7bc70 Mon Sep 17 00:00:00 2001 From: Firas Medini Date: Tue, 13 Aug 2019 05:13:11 -0700 Subject: [PATCH] Adding synchronization primitives destroyers Environment should provide calls for destroying primitives (i.e. env_mutex_destroy()) and OCF should call these functions in its cleanup paths. Signed-off-by: Firas Medini --- env/posix/ocf_env.c | 5 - env/posix/ocf_env.h | 110 +++++-------------- src/cleaning/acp.c | 5 + src/concurrency/ocf_cache_line_concurrency.c | 6 + src/concurrency/ocf_metadata_concurrency.c | 7 ++ src/concurrency/ocf_metadata_concurrency.h | 2 + src/metadata/metadata.c | 2 + src/metadata/metadata_raw_dynamic.c | 2 + src/metadata/metadata_updater.c | 1 + src/mngt/ocf_mngt_common.c | 1 + src/ocf_ctx.c | 2 + src/ocf_queue.c | 1 + src/ocf_volume.c | 1 + src/utils/utils_async_lock.c | 2 + tests/unit/tests/ocf_env/ocf_env.c | 74 ------------- tests/unit/tests/ocf_env/ocf_env.h | 26 ----- 16 files changed, 58 insertions(+), 189 deletions(-) diff --git a/env/posix/ocf_env.c b/env/posix/ocf_env.c index f14b03d..d4c0e00 100644 --- a/env/posix/ocf_env.c +++ b/env/posix/ocf_env.c @@ -110,11 +110,6 @@ void env_allocator_destroy(env_allocator *allocator) } } -uint32_t env_allocator_item_count(env_allocator *allocator) -{ - return env_atomic_read(&allocator->count); -} - /* *** DEBUGING *** */ #define ENV_TRACE_DEPTH 16 diff --git a/env/posix/ocf_env.h b/env/posix/ocf_env.h index ea35aea..62d6e23 100644 --- a/env/posix/ocf_env.h +++ b/env/posix/ocf_env.h @@ -169,8 +169,6 @@ void *env_allocator_new(env_allocator *allocator); void env_allocator_del(env_allocator *allocator, void *item); -uint32_t env_allocator_item_count(env_allocator *allocator); - /* *** MUTEX *** */ typedef struct { @@ -198,22 +196,15 @@ static inline int env_mutex_lock_interruptible(env_mutex *mutex) return 0; } -static inline int env_mutex_trylock(env_mutex *mutex) -{ - return pthread_mutex_trylock(&mutex->m) ? -OCF_ERR_NO_LOCK : 0; -} - static inline void env_mutex_unlock(env_mutex *mutex) { ENV_BUG_ON(pthread_mutex_unlock(&mutex->m)); } -static inline int env_mutex_is_locked(env_mutex *mutex) +static inline int env_mutex_destroy(env_mutex *mutex) { - if (env_mutex_trylock(mutex) == 0) { - env_mutex_unlock(mutex); + if(pthread_mutex_destroy(&mutex->m)) return 1; - } return 0; } @@ -243,19 +234,17 @@ static inline int env_rmutex_lock_interruptible(env_rmutex *rmutex) return env_mutex_lock_interruptible(rmutex); } -static inline int env_rmutex_trylock(env_rmutex *rmutex) -{ - return env_mutex_trylock(rmutex); -} - static inline void env_rmutex_unlock(env_rmutex *rmutex) { env_mutex_unlock(rmutex); } -static inline int env_rmutex_is_locked(env_rmutex *rmutex) +static inline int env_rmutex_destroy(env_rmutex *rmutex) { - return env_mutex_is_locked(rmutex); + if(pthread_mutex_destroy(&rmutex->m)) + return 1; + + return 0; } /* *** RW SEMAPHORE *** */ @@ -298,26 +287,9 @@ static inline int env_rwsem_down_write_trylock(env_rwsem *s) return pthread_rwlock_trywrlock(&s->lock) ? -OCF_ERR_NO_LOCK : 0; } -static inline int env_rwsem_is_locked(env_rwsem *s) +static inline int env_rwsem_destroy(env_rwsem *s) { - if (env_rwsem_down_read_trylock(s) == 0) { - env_rwsem_up_read(s); - return 0; - } - - return 1; -} - -static inline int env_rwsem_down_write_interruptible(env_rwsem *s) -{ - env_rwsem_down_write(s); - return 0; -} - -static inline int env_rwsem_down_read_interruptible(env_rwsem *s) -{ - env_rwsem_down_read(s); - return 0; + return pthread_rwlock_destroy(&s->lock); } /* *** COMPLETION *** */ @@ -342,10 +314,9 @@ static inline void env_completion_complete(env_completion *completion) sem_post(&completion->sem); } -static inline void env_completion_complete_and_exit( - env_completion *completion, int ret) +static inline void env_completion_destroy(env_completion *completion) { - env_completion_complete(completion); /* TODO */ + sem_destroy(&completion->sem); } /* *** ATOMIC VARIABLES *** */ @@ -378,11 +349,6 @@ static inline void env_atomic_sub(int i, env_atomic *a) __sync_sub_and_fetch(&a->counter, i); } -static inline bool env_atomic_sub_and_test(int i, env_atomic *a) -{ - return __sync_sub_and_fetch(&a->counter, i) == 0; -} - static inline void env_atomic_inc(env_atomic *a) { env_atomic_add(1, a); @@ -398,11 +364,6 @@ static inline bool env_atomic_dec_and_test(env_atomic *a) return __sync_sub_and_fetch(&a->counter, 1) == 0; } -static inline bool env_atomic_inc_and_test(env_atomic *a) -{ - return __sync_add_and_fetch(&a->counter, 1) == 0; -} - static inline int env_atomic_add_return(int i, env_atomic *a) { return __sync_add_and_fetch(&a->counter, i); @@ -504,16 +465,6 @@ static inline void env_spinlock_unlock(env_spinlock *l) ENV_BUG_ON(pthread_spin_unlock(&l->lock)); } -static inline void env_spinlock_lock_irq(env_spinlock *l) -{ - env_spinlock_lock(l); -} - -static inline void env_spinlock_unlock_irq(env_spinlock *l) -{ - env_spinlock_unlock(l); -} - #define env_spinlock_lock_irqsave(l, flags) \ (void)flags; \ env_spinlock_lock(l) @@ -522,6 +473,11 @@ static inline void env_spinlock_unlock_irq(env_spinlock *l) (void)flags; \ env_spinlock_unlock(l) +static inline void env_spinlock_destroy(env_spinlock *l) +{ + ENV_BUG_ON(pthread_spin_destroy(&l->lock)); +} + /* *** RW LOCKS *** */ typedef struct { @@ -553,22 +509,17 @@ static inline void env_rwlock_write_unlock(env_rwlock *l) ENV_BUG_ON(pthread_rwlock_unlock(&l->lock)); } +static inline void env_rwlock_destroy(env_rwlock *l) +{ + ENV_BUG_ON(pthread_rwlock_destroy(&l->lock)); +} + /* *** WAITQUEUE *** */ typedef struct { sem_t sem; } env_waitqueue; -static inline void env_waitqueue_init(env_waitqueue *w) -{ - sem_init(&w->sem, 0, 0); -} - -static inline void env_waitqueue_wake_up(env_waitqueue *w) -{ - sem_post(&w->sem); -} - #define env_waitqueue_wait(w, condition) \ ({ \ int __ret = 0; \ @@ -577,6 +528,11 @@ static inline void env_waitqueue_wake_up(env_waitqueue *w) __ret = __ret; \ }) +static inline void env_waitqueue_destroy(env_waitqueue *w) +{ + sem_destroy(&w->sem); +} + /* *** BIT OPERATIONS *** */ static inline void env_bit_set(int nr, volatile void *addr) @@ -604,12 +560,6 @@ static inline bool env_bit_test(int nr, const volatile unsigned long *addr) return !!(*byte & mask); } -/* *** SCHEDULING *** */ -static inline void env_schedule(void) -{ - sched_yield(); -} - static inline int env_in_interrupt(void) { return 0; @@ -689,14 +639,6 @@ struct env_timeval { uint64_t sec, usec; }; -static inline void env_gettimeofday(struct env_timeval *tv) -{ - struct timeval t; - gettimeofday(&t, NULL); - tv->sec = t.tv_sec; - tv->usec = t.tv_usec; -} - uint32_t env_crc32(uint32_t crc, uint8_t const *data, size_t len); #define ENV_PRIu64 "lu" diff --git a/src/cleaning/acp.c b/src/cleaning/acp.c index 956e5b0..533f4e6 100644 --- a/src/cleaning/acp.c +++ b/src/cleaning/acp.c @@ -233,8 +233,13 @@ void cleaning_policy_acp_init_cache_block(struct ocf_cache *cache, void cleaning_policy_acp_deinitialize(struct ocf_cache *cache) { + struct acp_context *acp; + _acp_remove_cores(cache); + acp = cache->cleaner.cleaning_policy_context; + env_rwsem_destroy(&acp->chunks_lock); + env_vfree(cache->cleaner.cleaning_policy_context); cache->cleaner.cleaning_policy_context = NULL; } diff --git a/src/concurrency/ocf_cache_line_concurrency.c b/src/concurrency/ocf_cache_line_concurrency.c index 432904c..ddbb626 100644 --- a/src/concurrency/ocf_cache_line_concurrency.c +++ b/src/concurrency/ocf_cache_line_concurrency.c @@ -134,6 +134,7 @@ ocf_cache_line_concurrency_init: */ void ocf_cache_line_concurrency_deinit(struct ocf_cache *cache) { + int i; struct ocf_cache_line_concurrency *concurrency; if (!cache->device->concurrency.cache_line) @@ -143,6 +144,11 @@ void ocf_cache_line_concurrency_deinit(struct ocf_cache *cache) concurrency = cache->device->concurrency.cache_line; + env_rwlock_destroy(&concurrency->lock); + + for (i = 0; i < _WAITERS_LIST_ENTRIES; i++) + env_spinlock_destroy(&concurrency->waiters_lsts[i].lock); + if (concurrency->access) OCF_REALLOC_DEINIT(&concurrency->access, &concurrency->access_limit); diff --git a/src/concurrency/ocf_metadata_concurrency.c b/src/concurrency/ocf_metadata_concurrency.c index a262cc3..5304780 100644 --- a/src/concurrency/ocf_metadata_concurrency.c +++ b/src/concurrency/ocf_metadata_concurrency.c @@ -12,6 +12,13 @@ void ocf_metadata_concurrency_init(struct ocf_cache *cache) env_rwsem_init(&cache->metadata.lock.collision); } +void ocf_metadata_concurrency_deinit(struct ocf_cache *cache) +{ + env_spinlock_destroy(&cache->metadata.lock.eviction); + env_rwlock_destroy(&cache->metadata.lock.status); + env_rwsem_destroy(&cache->metadata.lock.collision); +} + int ocf_metadata_concurrency_attached_init(struct ocf_cache *cache) { return 0; diff --git a/src/concurrency/ocf_metadata_concurrency.h b/src/concurrency/ocf_metadata_concurrency.h index e0be9c2..85b0c10 100644 --- a/src/concurrency/ocf_metadata_concurrency.h +++ b/src/concurrency/ocf_metadata_concurrency.h @@ -12,6 +12,8 @@ void ocf_metadata_concurrency_init(struct ocf_cache *cache); +void ocf_metadata_concurrency_deinit(struct ocf_cache *cache); + int ocf_metadata_concurrency_attached_init(struct ocf_cache *cache); static inline void ocf_metadata_eviction_lock(struct ocf_cache *cache) diff --git a/src/metadata/metadata.c b/src/metadata/metadata.c index 611ffc9..3f9ae8f 100644 --- a/src/metadata/metadata.c +++ b/src/metadata/metadata.c @@ -73,6 +73,8 @@ void ocf_metadata_deinit(struct ocf_cache *cache) cache->metadata.iface.deinit(cache); } + ocf_metadata_concurrency_deinit(cache); + ocf_metadata_io_deinit(cache); } diff --git a/src/metadata/metadata_raw_dynamic.c b/src/metadata/metadata_raw_dynamic.c index 230dbfe..e45ade1 100644 --- a/src/metadata/metadata_raw_dynamic.c +++ b/src/metadata/metadata_raw_dynamic.c @@ -129,6 +129,8 @@ int raw_dynamic_deinit(ocf_cache_t cache, for (i = 0; i < raw->ssd_pages; i++) env_secure_free(ctrl->pages[i], PAGE_SIZE); + env_mutex_destroy(&ctrl->lock); + env_vfree(ctrl); raw->priv = NULL; diff --git a/src/metadata/metadata_updater.c b/src/metadata/metadata_updater.c index 4c24475..f3bb357 100644 --- a/src/metadata/metadata_updater.c +++ b/src/metadata/metadata_updater.c @@ -33,6 +33,7 @@ void ocf_metadata_updater_kick(ocf_cache_t cache) void ocf_metadata_updater_stop(ocf_cache_t cache) { ctx_metadata_updater_stop(cache->owner, &cache->metadata_updater); + env_mutex_destroy(&cache->metadata_updater.syncher.lock); } void ocf_metadata_updater_set_priv(ocf_metadata_updater_t mu, void *priv) diff --git a/src/mngt/ocf_mngt_common.c b/src/mngt/ocf_mngt_common.c index 8e2a07f..bc21cd0 100644 --- a/src/mngt/ocf_mngt_common.c +++ b/src/mngt/ocf_mngt_common.c @@ -135,6 +135,7 @@ void ocf_mngt_cache_put(ocf_cache_t cache) ctx = cache->owner; ocf_metadata_deinit(cache); ocf_mngt_cache_lock_deinit(cache); + env_mutex_destroy(&cache->flush_mutex); env_vfree(cache); ocf_ctx_put(ctx); } diff --git a/src/ocf_ctx.c b/src/ocf_ctx.c index 16c123c..cab484d 100644 --- a/src/ocf_ctx.c +++ b/src/ocf_ctx.c @@ -233,6 +233,8 @@ void ocf_ctx_put(ocf_ctx_t ctx) ocf_mngt_core_pool_deinit(ctx); ocf_ctx_unregister_volume_types(ctx); + env_rmutex_destroy(&ctx->lock); + ocf_req_allocator_deinit(ctx); ocf_logger_close(&ctx->logger); env_free(ctx); diff --git a/src/ocf_queue.c b/src/ocf_queue.c index ffd034f..7657eee 100644 --- a/src/ocf_queue.c +++ b/src/ocf_queue.c @@ -60,6 +60,7 @@ void ocf_queue_put(ocf_queue_t queue) list_del(&queue->list); queue->ops->stop(queue); ocf_mngt_cache_put(queue->cache); + env_spinlock_destroy(&queue->io_list_lock); env_free(queue); } } diff --git a/src/ocf_volume.c b/src/ocf_volume.c index 7793b33..ebb1759 100644 --- a/src/ocf_volume.c +++ b/src/ocf_volume.c @@ -308,6 +308,7 @@ static void ocf_volume_close_end(void *ctx) env_completion *cmpl = ctx; env_completion_complete(cmpl); + env_completion_destroy(cmpl); } void ocf_volume_close(ocf_volume_t volume) diff --git a/src/utils/utils_async_lock.c b/src/utils/utils_async_lock.c index d730060..748260f 100644 --- a/src/utils/utils_async_lock.c +++ b/src/utils/utils_async_lock.c @@ -72,6 +72,8 @@ void ocf_async_lock_deinit(struct ocf_async_lock *lock) list_for_each_entry_safe(iter, temp, &lock->waiters, list) list_move_tail(&iter->list, &waiters); env_mutex_unlock(&lock->mutex); + + env_mutex_destroy(&lock->mutex); _ocf_async_lock_run_waiters(lock, &waiters, -OCF_ERR_NO_LOCK); } diff --git a/tests/unit/tests/ocf_env/ocf_env.c b/tests/unit/tests/ocf_env/ocf_env.c index c3ea698..d6cbd96 100644 --- a/tests/unit/tests/ocf_env/ocf_env.c +++ b/tests/unit/tests/ocf_env/ocf_env.c @@ -127,11 +127,6 @@ void env_allocator_destroy(env_allocator *allocator) } } -uint32_t env_allocator_item_count(env_allocator *allocator) -{ - return env_atomic_read(&allocator->count); -} - /* *** COMPLETION *** */ void env_completion_init(env_completion *completion) @@ -173,26 +168,12 @@ int env_mutex_lock_interruptible(env_mutex *mutex) return mock(); } -int env_mutex_trylock(env_mutex *mutex) -{ - function_called(); - check_expected_ptr(mutex); - return mock(); -} - void env_mutex_unlock(env_mutex *mutex) { function_called(); check_expected_ptr(mutex); } -int env_mutex_is_locked(env_mutex *mutex) -{ - function_called(); - check_expected_ptr(mutex); - return mock(); -} - int env_rmutex_init(env_rmutex *rmutex) { function_called(); @@ -213,26 +194,12 @@ int env_rmutex_lock_interruptible(env_rmutex *rmutex) return mock(); } -int env_rmutex_trylock(env_rmutex *rmutex) -{ - function_called(); - check_expected_ptr(rmutex); - return mock(); -} - void env_rmutex_unlock(env_rmutex *rmutex) { function_called(); check_expected_ptr(rmutex); } -int env_rmutex_is_locked(env_rmutex *rmutex) -{ - function_called(); - check_expected_ptr(rmutex); - return mock(); -} - int env_rwsem_init(env_rwsem *s) { function_called(); @@ -298,11 +265,6 @@ void env_atomic_sub(int i, env_atomic *a) *a -= i; } -bool env_atomic_sub_and_test(int i, env_atomic *a) -{ - return *a-=i == 0; -} - void env_atomic_inc(env_atomic *a) { ++*a; @@ -318,11 +280,6 @@ bool env_atomic_dec_and_test(env_atomic *a) return --*a == 0; } -bool env_atomic_inc_and_test(env_atomic *a) -{ - return ++*a == 0; -} - int env_atomic_add_return(int i, env_atomic *a) { return *a+=i; @@ -422,18 +379,6 @@ void env_spinlock_unlock(env_spinlock *l) check_expected_ptr(l); } -void env_spinlock_lock_irq(env_spinlock *l) -{ - function_called(); - check_expected_ptr(l); -} - -void env_spinlock_unlock_irq(env_spinlock *l) -{ - function_called(); - check_expected_ptr(l); -} - void env_rwlock_init(env_rwlock *l) { function_called(); @@ -464,20 +409,6 @@ void env_rwlock_write_unlock(env_rwlock *l) check_expected_ptr(l); } -void env_waitqueue_init(env_waitqueue *w) -{ - w->completed = false; - w->waiting = false; - w->co = NULL; -} - -void env_waitqueue_wake_up(env_waitqueue *w) -{ - w->completed = true; - if (!w->waiting || !w->co) - return; -} - void env_bit_set(int nr, volatile void *addr) { char *byte = (char *) addr + (nr >> 3); @@ -510,11 +441,6 @@ void env_touch_softlockup_wd(void) function_called(); } -void env_schedule(void) -{ - function_called(); -} - int env_in_interrupt(void) { function_called(); diff --git a/tests/unit/tests/ocf_env/ocf_env.h b/tests/unit/tests/ocf_env/ocf_env.h index 06a5e9c..ebec304 100644 --- a/tests/unit/tests/ocf_env/ocf_env.h +++ b/tests/unit/tests/ocf_env/ocf_env.h @@ -120,8 +120,6 @@ void *env_allocator_new(env_allocator *allocator); void env_allocator_del(env_allocator *allocator, void *item); -uint32_t env_allocator_item_count(env_allocator *allocator); - /* *** MUTEX *** */ typedef struct { @@ -134,12 +132,8 @@ void env_mutex_lock(env_mutex *mutex); int env_mutex_lock_interruptible(env_mutex *mutex); -int env_mutex_trylock(env_mutex *mutex); - void env_mutex_unlock(env_mutex *mutex); -int env_mutex_is_locked(env_mutex *mutex); - /* *** RECURSIVE MUTEX *** */ typedef env_mutex env_rmutex; @@ -150,12 +144,8 @@ void env_rmutex_lock(env_rmutex *rmutex); int env_rmutex_lock_interruptible(env_rmutex *rmutex); -int env_rmutex_trylock(env_rmutex *rmutex); - void env_rmutex_unlock(env_rmutex *rmutex); -int env_rmutex_is_locked(env_rmutex *rmutex); - /* *** RW SEMAPHORE *** */ typedef struct { pthread_rwlock_t lock; @@ -175,8 +165,6 @@ void env_rwsem_down_write(env_rwsem *s); int env_rwsem_down_write_trylock(env_rwsem *s); -int env_rwsem_is_locked(env_rwsem *s); - /* *** ATOMIC VARIABLES *** */ typedef int env_atomic; @@ -191,16 +179,12 @@ void env_atomic_add(int i, env_atomic *a); void env_atomic_sub(int i, env_atomic *a); -bool env_atomic_sub_and_test(int i, env_atomic *a); - void env_atomic_inc(env_atomic *a); void env_atomic_dec(env_atomic *a); bool env_atomic_dec_and_test(env_atomic *a); -bool env_atomic_inc_and_test(env_atomic *a); - int env_atomic_add_return(int i, env_atomic *a); int env_atomic_sub_return(int i, env_atomic *a); @@ -253,10 +237,6 @@ void env_spinlock_lock(env_spinlock *l); void env_spinlock_unlock(env_spinlock *l); -void env_spinlock_lock_irq(env_spinlock *l); - -void env_spinlock_unlock_irq(env_spinlock *l); - #define env_spinlock_lock_irqsave(l, flags) \ env_spinlock_lock(l); (void)flags; @@ -286,10 +266,6 @@ typedef struct { Coroutine *co; } env_waitqueue; -void env_waitqueue_init(env_waitqueue *w); - -void env_waitqueue_wake_up(env_waitqueue *w); - #define env_waitqueue_wait(w, condition) \ ({ \ int __ret = 0; \ @@ -314,8 +290,6 @@ bool env_bit_test(int nr, const volatile unsigned long *addr); void env_touch_softlockup_wd(void); -void env_schedule(void); - int env_in_interrupt(void); uint64_t env_get_tick_count(void);