From 2a4af9191d25a3c5ef21f6f837d480749fc2ae5d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 25 Oct 2018 14:58:17 +0200 Subject: ratelimiter: refcounter doesn't need to be atomic Suggested-by: Jann Horn --- src/ratelimiter.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ratelimiter.c b/src/ratelimiter.c index a3d334b..b3413e4 100644 --- a/src/ratelimiter.c +++ b/src/ratelimiter.c @@ -13,7 +13,7 @@ static struct kmem_cache *entry_cache; static hsiphash_key_t key; static spinlock_t table_lock = __SPIN_LOCK_UNLOCKED("ratelimiter_table_lock"); static DEFINE_MUTEX(init_lock); -static atomic64_t refcnt = ATOMIC64_INIT(0); +static u64 init_refcnt; /* Protected by init_lock, hence not atomic. */ static atomic_t total_entries = ATOMIC_INIT(0); static unsigned int max_entries, table_size; static void wg_ratelimiter_gc_entries(struct work_struct *); @@ -155,7 +155,7 @@ err_oom: int wg_ratelimiter_init(void) { mutex_lock(&init_lock); - if (atomic64_inc_return(&refcnt) != 1) + if (++init_refcnt != 1) goto out; entry_cache = KMEM_CACHE(ratelimiter_entry, 0); @@ -194,7 +194,7 @@ out: err_kmemcache: kmem_cache_destroy(entry_cache); err: - atomic64_dec(&refcnt); + --init_refcnt; mutex_unlock(&init_lock); return -ENOMEM; } @@ -202,7 +202,7 @@ err: void wg_ratelimiter_uninit(void) { mutex_lock(&init_lock); - if (atomic64_dec_if_positive(&refcnt)) + if (!init_refcnt || --init_refcnt) goto out; cancel_delayed_work_sync(&gc_work); -- cgit v1.2.3-59-g8ed1b