From 2efbe2828bc507896248598ffb1026653cbfed2c Mon Sep 17 00:00:00 2001 From: riastradh Date: Mon, 23 Jan 2023 15:53:58 +0000 Subject: [PATCH] Use store-release, load-consume operations. (#128) Co-authored-by: Taylor R Campbell --- src/kern/npf_conf.c | 11 +++++------ src/kern/npf_conn.c | 9 ++++----- src/kern/npf_ifaddr.c | 4 +++- src/kern/stand/npf_stand.h | 2 ++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/kern/npf_conf.c b/src/kern/npf_conf.c index db3ea79..6158c65 100644 --- a/src/kern/npf_conf.c +++ b/src/kern/npf_conf.c @@ -131,8 +131,7 @@ npf_config_load(npf_t *npf, npf_config_t *nc, npf_conndb_t *conns, bool flush) /* * Set the new config and release the lock. */ - membar_sync(); - atomic_store_relaxed(&npf->config, nc); + atomic_store_release(&npf->config, nc); if (onc == NULL) { /* Initial load, done. */ npf_ifmap_flush(npf); @@ -225,7 +224,7 @@ npf_config_read_exit(npf_t *npf, int s) npf_ruleset_t * npf_config_ruleset(npf_t *npf) { - npf_config_t *config = atomic_load_relaxed(&npf->config); + npf_config_t *config = atomic_load_consume(&npf->config); KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr)); return config->ruleset; } @@ -233,7 +232,7 @@ npf_config_ruleset(npf_t *npf) npf_ruleset_t * npf_config_natset(npf_t *npf) { - npf_config_t *config = atomic_load_relaxed(&npf->config); + npf_config_t *config = atomic_load_consume(&npf->config); KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr)); return config->nat_ruleset; } @@ -241,7 +240,7 @@ npf_config_natset(npf_t *npf) npf_tableset_t * npf_config_tableset(npf_t *npf) { - npf_config_t *config = atomic_load_relaxed(&npf->config); + npf_config_t *config = atomic_load_consume(&npf->config); KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr)); return config->tableset; } @@ -249,7 +248,7 @@ npf_config_tableset(npf_t *npf) bool npf_default_pass(npf_t *npf) { - npf_config_t *config = atomic_load_relaxed(&npf->config); + npf_config_t *config = atomic_load_consume(&npf->config); KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr)); return config->default_pass; } diff --git a/src/kern/npf_conn.c b/src/kern/npf_conn.c index 791a66c..585f6fd 100644 --- a/src/kern/npf_conn.c +++ b/src/kern/npf_conn.c @@ -199,8 +199,7 @@ npf_conn_load(npf_t *npf, npf_conndb_t *ndb, bool track) KASSERT(atomic_load_relaxed(&npf->conn_tracking) == CONN_TRACKING_OFF); odb = atomic_load_relaxed(&npf->conn_db); - membar_sync(); - atomic_store_relaxed(&npf->conn_db, ndb); + atomic_store_release(&npf->conn_db, ndb); } if (track) { /* After this point lookups start flying in. */ @@ -470,7 +469,7 @@ npf_conn_establish(npf_cache_t *npc, const unsigned di, bool global) * the connection later. */ mutex_enter(&con->c_lock); - conn_db = atomic_load_relaxed(&npf->conn_db); + conn_db = atomic_load_consume(&npf->conn_db); if (!npf_conndb_insert(conn_db, fw, con, NPF_FLOW_FORW)) { error = EISCONN; goto err; @@ -575,7 +574,7 @@ npf_conn_setnat(const npf_cache_t *npc, npf_conn_t *con, } /* Remove the "backwards" key. */ - conn_db = atomic_load_relaxed(&npf->conn_db); + conn_db = atomic_load_consume(&npf->conn_db); bk = npf_conn_getbackkey(con, con->c_alen); ret = npf_conndb_remove(conn_db, bk); KASSERT(ret == con); @@ -740,7 +739,7 @@ npf_conn_remove(npf_conndb_t *cd, npf_conn_t *con) void npf_conn_worker(npf_t *npf) { - npf_conndb_t *conn_db = atomic_load_relaxed(&npf->conn_db); + npf_conndb_t *conn_db = atomic_load_consume(&npf->conn_db); npf_conndb_gc(npf, conn_db, false, true); } diff --git a/src/kern/npf_ifaddr.c b/src/kern/npf_ifaddr.c index 57720fd..cd94306 100644 --- a/src/kern/npf_ifaddr.c +++ b/src/kern/npf_ifaddr.c @@ -65,9 +65,11 @@ lookup_ifnet_table(npf_t *npf, ifnet_t *ifp) static void replace_ifnet_table(npf_t *npf, npf_table_t *newt) { - npf_tableset_t *ts = npf->config->tableset; + npf_tableset_t *ts = atomic_load_relaxed(&npf->config)->tableset; npf_table_t *oldt; + KASSERT(npf_config_locked_p(npf)); + KERNEL_UNLOCK_ONE(NULL); /* diff --git a/src/kern/stand/npf_stand.h b/src/kern/stand/npf_stand.h index 9538530..c43900b 100644 --- a/src/kern/stand/npf_stand.h +++ b/src/kern/stand/npf_stand.h @@ -154,6 +154,8 @@ npfkern_atomic_swap_ptr(volatile void *ptr, void *newval) #define atomic_load_acquire(x) \ atomic_load_explicit((x), memory_order_acquire) +#define atomic_load_consume(x) \ + atomic_load_explicit((x), memory_order_consume) #define atomic_store_release(x, y) \ atomic_store_explicit((x), (y), memory_order_release)