summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-10-25 14:58:17 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-10-25 23:19:50 +0200
commit935d0d09114f28e9aeda293fa6c1bcb110bf9c3b (patch)
tree82a15515544a172f3ed6844421ce3cb60a13e9dc /src
parent18eebcca8234bcfd1176721e6adc5e63af675291 (diff)
ratelimiter: refcounter doesn't need to be atomic
Suggested-by: Jann Horn <jann@thejh.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src')
-rw-r--r--src/ratelimiter.c8
1 files 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);