From db3e51f612069b31712083eee09be4f466a1033e Mon Sep 17 00:00:00 2001 From: Mr NeilBrown Date: Sun, 7 Jun 2020 19:24:27 -0400 Subject: [PATCH] LU-12678 socklnd: convert various refcounts to refcount_t Each of these refcounts exactly follows the expectations of refcount_t, so change the atomic_t to refcoun_t. We can remove the LASSERTs on incref/decref as they can now be enabled at build time with CONFIG_REFCOUNT_FULL Test-Parameters: trivial testlist=sanity-lnet Signed-off-by: Mr NeilBrown Change-Id: I50f13465b588c30a70568a5800619cc7ec26293d Reviewed-on: https://review.whamcloud.com/39130 Tested-by: jenkins Tested-by: Maloo Reviewed-by: James Simmons Reviewed-by: Shaun Tancheff Reviewed-by: Chris Horn Reviewed-by: Oleg Drokin --- lnet/klnds/socklnd/socklnd.c | 30 ++++++------- lnet/klnds/socklnd/socklnd.h | 97 +++++++++++++++++++---------------------- lnet/klnds/socklnd/socklnd_cb.c | 6 +-- 3 files changed, 64 insertions(+), 69 deletions(-) diff --git a/lnet/klnds/socklnd/socklnd.c b/lnet/klnds/socklnd/socklnd.c index b6e87ce..1389d4f 100644 --- a/lnet/klnds/socklnd/socklnd.c +++ b/lnet/klnds/socklnd/socklnd.c @@ -122,7 +122,7 @@ ksocknal_create_route(__u32 ipaddr, int port) if (route == NULL) return (NULL); - atomic_set (&route->ksnr_refcount, 1); + refcount_set(&route->ksnr_refcount, 1); route->ksnr_peer = NULL; route->ksnr_retry_interval = 0; /* OK to connect at any time */ route->ksnr_ipaddr = ipaddr; @@ -141,7 +141,7 @@ ksocknal_create_route(__u32 ipaddr, int port) void ksocknal_destroy_route(struct ksock_route *route) { - LASSERT (atomic_read(&route->ksnr_refcount) == 0); + LASSERT(refcount_read(&route->ksnr_refcount) == 0); if (route->ksnr_peer != NULL) ksocknal_peer_decref(route->ksnr_peer); @@ -173,7 +173,7 @@ ksocknal_create_peer(struct lnet_ni *ni, struct lnet_process_id id) peer_ni->ksnp_ni = ni; peer_ni->ksnp_id = id; - atomic_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */ + refcount_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */ peer_ni->ksnp_closing = 0; peer_ni->ksnp_accepting = 0; peer_ni->ksnp_proto = NULL; @@ -197,7 +197,7 @@ ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni) CDEBUG (D_NET, "peer_ni %s %p deleted\n", libcfs_id2str(peer_ni->ksnp_id), peer_ni); - LASSERT(atomic_read(&peer_ni->ksnp_refcount) == 0); + LASSERT(refcount_read(&peer_ni->ksnp_refcount) == 0); LASSERT(peer_ni->ksnp_accepting == 0); LASSERT(list_empty(&peer_ni->ksnp_conns)); LASSERT(list_empty(&peer_ni->ksnp_routes)); @@ -233,7 +233,7 @@ ksocknal_find_peer_locked(struct lnet_ni *ni, struct lnet_process_id id) CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d)\n", peer_ni, libcfs_id2str(id), - atomic_read(&peer_ni->ksnp_refcount)); + refcount_read(&peer_ni->ksnp_refcount)); return peer_ni; } return NULL; @@ -1081,10 +1081,10 @@ ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route, conn->ksnc_sock = sock; /* 2 ref, 1 for conn, another extra ref prevents socket * being closed before establishment of connection */ - atomic_set (&conn->ksnc_sock_refcount, 2); + refcount_set(&conn->ksnc_sock_refcount, 2); conn->ksnc_type = type; ksocknal_lib_save_callback(sock, conn); - atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */ + refcount_set(&conn->ksnc_conn_refcount, 1); /* 1 ref for me */ conn->ksnc_rx_ready = 0; conn->ksnc_rx_scheduled = 0; @@ -1657,7 +1657,7 @@ void ksocknal_queue_zombie_conn(struct ksock_conn *conn) { /* Queue the conn for the reaper to destroy */ - LASSERT(atomic_read(&conn->ksnc_conn_refcount) == 0); + LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0); spin_lock_bh(&ksocknal_data.ksnd_reaper_lock); list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns); @@ -1674,8 +1674,8 @@ ksocknal_destroy_conn(struct ksock_conn *conn) /* Final coup-de-grace of the reaper */ CDEBUG (D_NET, "connection %p\n", conn); - LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0); - LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0); + LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0); + LASSERT(refcount_read(&conn->ksnc_sock_refcount) == 0); LASSERT (conn->ksnc_sock == NULL); LASSERT (conn->ksnc_route == NULL); LASSERT (!conn->ksnc_tx_scheduled); @@ -2419,7 +2419,7 @@ ksocknal_debug_peerhash(struct lnet_ni *ni) CWARN("Active peer_ni on shutdown: %s, ref %d, " "closing %d, accepting %d, err %d, zcookie %llu, " "txq %d, zc_req %d\n", libcfs_id2str(peer_ni->ksnp_id), - atomic_read(&peer_ni->ksnp_refcount), + refcount_read(&peer_ni->ksnp_refcount), peer_ni->ksnp_closing, peer_ni->ksnp_accepting, peer_ni->ksnp_error, peer_ni->ksnp_zc_next_cookie, @@ -2427,16 +2427,16 @@ ksocknal_debug_peerhash(struct lnet_ni *ni) !list_empty(&peer_ni->ksnp_zc_req_list)); list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) { - CWARN("Route: ref %d, schd %d, conn %d, cnted %d, " - "del %d\n", atomic_read(&route->ksnr_refcount), + CWARN("Route: ref %d, schd %d, conn %d, cnted %d, del %d\n", + refcount_read(&route->ksnr_refcount), route->ksnr_scheduled, route->ksnr_connecting, route->ksnr_connected, route->ksnr_deleted); } list_for_each_entry(conn, &peer_ni->ksnp_conns, ksnc_list) { CWARN("Conn: ref %d, sref %d, t %d, c %d\n", - atomic_read(&conn->ksnc_conn_refcount), - atomic_read(&conn->ksnc_sock_refcount), + refcount_read(&conn->ksnc_conn_refcount), + refcount_read(&conn->ksnc_sock_refcount), conn->ksnc_type, conn->ksnc_closing); } break; diff --git a/lnet/klnds/socklnd/socklnd.h b/lnet/klnds/socklnd/socklnd.h index b5b3425..c539d1b 100644 --- a/lnet/klnds/socklnd/socklnd.h +++ b/lnet/klnds/socklnd/socklnd.h @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -247,18 +248,18 @@ struct ksock_route; /* forward ref */ struct ksock_proto; /* forward ref */ struct ksock_tx { /* transmit packet */ - struct list_head tx_list; /* queue on conn for transmission etc */ - struct list_head tx_zc_list; /* queue on peer_ni for ZC request */ - atomic_t tx_refcount; /* tx reference count */ - int tx_nob; /* # packet bytes */ - int tx_resid; /* residual bytes */ - int tx_niov; /* # packet kvec frags */ - int tx_nkiov; /* # packet page frags */ - unsigned short tx_zc_aborted; /* aborted ZC request */ - unsigned short tx_zc_capable:1; /* payload is large enough for ZC */ - unsigned short tx_zc_checked:1; /* Have I checked if I should ZC? */ - unsigned short tx_nonblk:1; /* it's a non-blocking ACK */ - struct bio_vec *tx_kiov; /* packet page frags */ + struct list_head tx_list; /* queue on conn for transmission etc */ + struct list_head tx_zc_list; /* queue on peer_ni for ZC request */ + refcount_t tx_refcount; /* tx reference count */ + int tx_nob; /* # packet bytes */ + int tx_resid; /* residual bytes */ + int tx_niov; /* # packet kvec frags */ + int tx_nkiov; /* # packet page frags */ + unsigned short tx_zc_aborted; /* aborted ZC request */ + unsigned short tx_zc_capable:1; /* payload is large enough for ZC */ + unsigned short tx_zc_checked:1; /* Have I checked if I should ZC? */ + unsigned short tx_nonblk:1; /* it's a non-blocking ACK */ + struct bio_vec *tx_kiov; /* packet page frags */ struct ksock_conn *tx_conn; /* owning conn */ struct lnet_msg *tx_lnetmsg; /* lnet message for lnet_finalize() */ time64_t tx_deadline; /* when (in secs) tx times out */ @@ -286,23 +287,27 @@ union ksock_rxiovspace { #define SOCKNAL_RX_SLOP 6 /* skipping body */ struct ksock_conn { - struct ksock_peer_ni *ksnc_peer; /* owning peer_ni */ - struct ksock_route *ksnc_route; /* owning route */ - struct list_head ksnc_list; /* stash on peer_ni's conn list */ - struct socket *ksnc_sock; /* actual socket */ - void *ksnc_saved_data_ready; /* socket's original data_ready() callback */ - void *ksnc_saved_write_space; /* socket's original write_space() callback */ - atomic_t ksnc_conn_refcount; /* conn refcount */ - atomic_t ksnc_sock_refcount; /* sock refcount */ - struct ksock_sched *ksnc_scheduler; /* who schedules this connection */ - __u32 ksnc_myipaddr; /* my IP */ - __u32 ksnc_ipaddr; /* peer_ni's IP */ - int ksnc_port; /* peer_ni's port */ - signed int ksnc_type:3; /* type of connection, - * should be signed value */ - unsigned int ksnc_closing:1; /* being shut down */ - unsigned int ksnc_flip:1; /* flip or not, only for V2.x */ - unsigned int ksnc_zc_capable:1; /* enable to ZC */ + struct ksock_peer_ni *ksnc_peer; /* owning peer_ni */ + struct ksock_route *ksnc_route; /* owning route */ + struct list_head ksnc_list; /* on peer_ni's conn list */ + struct socket *ksnc_sock; /* actual socket */ + void *ksnc_saved_data_ready; /* socket's original + * data_ready() cb */ + void *ksnc_saved_write_space; /* socket's original + * write_space() cb */ + refcount_t ksnc_conn_refcount; /* conn refcount */ + refcount_t ksnc_sock_refcount; /* sock refcount */ + struct ksock_sched *ksnc_scheduler; /* who schedules this + * connection */ + __u32 ksnc_myipaddr; /* my IP */ + __u32 ksnc_ipaddr; /* peer_ni's IP */ + int ksnc_port; /* peer_ni's port */ + signed int ksnc_type:3; /* type of connection, + * should be signed + * value */ + unsigned int ksnc_closing:1; /* being shut down */ + unsigned int ksnc_flip:1; /* flip or not, only for V2.x */ + unsigned int ksnc_zc_capable:1; /* enable to ZC */ const struct ksock_proto *ksnc_proto; /* protocol for the connection */ /* READER */ @@ -356,7 +361,7 @@ struct ksock_route { struct list_head ksnr_list; /* chain on peer_ni route list*/ struct list_head ksnr_connd_list;/* chain on ksnr_connd_routes */ struct ksock_peer_ni *ksnr_peer; /* owning peer_ni */ - atomic_t ksnr_refcount; /* # users */ + refcount_t ksnr_refcount; /* # users */ time64_t ksnr_timeout; /* when (in secs) reconnection * can happen next */ @@ -378,7 +383,7 @@ struct ksock_peer_ni { struct hlist_node ksnp_list; /* stash on global peer_ni list */ time64_t ksnp_last_alive;/* when (in seconds) I was last alive */ struct lnet_process_id ksnp_id; /* who's on the other end(s) */ - atomic_t ksnp_refcount; /* # users */ + refcount_t ksnp_refcount; /* # users */ int ksnp_closing; /* being closed */ int ksnp_accepting; /* # passive connections pending */ int ksnp_error; /* errno on closing last conn */ @@ -468,8 +473,7 @@ ksocknal_route_mask(void) static inline void ksocknal_conn_addref(struct ksock_conn *conn) { - LASSERT(atomic_read(&conn->ksnc_conn_refcount) > 0); - atomic_inc(&conn->ksnc_conn_refcount); + refcount_inc(&conn->ksnc_conn_refcount); } extern void ksocknal_queue_zombie_conn(struct ksock_conn *conn); @@ -478,8 +482,7 @@ extern void ksocknal_finalize_zcreq(struct ksock_conn *conn); static inline void ksocknal_conn_decref(struct ksock_conn *conn) { - LASSERT(atomic_read(&conn->ksnc_conn_refcount) > 0); - if (atomic_dec_and_test(&conn->ksnc_conn_refcount)) + if (refcount_dec_and_test(&conn->ksnc_conn_refcount)) ksocknal_queue_zombie_conn(conn); } @@ -490,8 +493,7 @@ ksocknal_connsock_addref(struct ksock_conn *conn) read_lock(&ksocknal_data.ksnd_global_lock); if (!conn->ksnc_closing) { - LASSERT(atomic_read(&conn->ksnc_sock_refcount) > 0); - atomic_inc(&conn->ksnc_sock_refcount); + refcount_inc(&conn->ksnc_sock_refcount); rc = 0; } read_unlock(&ksocknal_data.ksnd_global_lock); @@ -502,8 +504,7 @@ ksocknal_connsock_addref(struct ksock_conn *conn) static inline void ksocknal_connsock_decref(struct ksock_conn *conn) { - LASSERT(atomic_read(&conn->ksnc_sock_refcount) > 0); - if (atomic_dec_and_test(&conn->ksnc_sock_refcount)) { + if (refcount_dec_and_test(&conn->ksnc_sock_refcount)) { LASSERT (conn->ksnc_closing); sock_release(conn->ksnc_sock); conn->ksnc_sock = NULL; @@ -514,8 +515,7 @@ ksocknal_connsock_decref(struct ksock_conn *conn) static inline void ksocknal_tx_addref(struct ksock_tx *tx) { - LASSERT(atomic_read(&tx->tx_refcount) > 0); - atomic_inc(&tx->tx_refcount); + refcount_inc(&tx->tx_refcount); } extern void ksocknal_tx_prep(struct ksock_conn *, struct ksock_tx *tx); @@ -524,16 +524,14 @@ extern void ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int error) static inline void ksocknal_tx_decref(struct ksock_tx *tx) { - LASSERT(atomic_read(&tx->tx_refcount) > 0); - if (atomic_dec_and_test(&tx->tx_refcount)) + if (refcount_dec_and_test(&tx->tx_refcount)) ksocknal_tx_done(NULL, tx, 0); } static inline void ksocknal_route_addref(struct ksock_route *route) { - LASSERT(atomic_read(&route->ksnr_refcount) > 0); - atomic_inc(&route->ksnr_refcount); + refcount_inc(&route->ksnr_refcount); } extern void ksocknal_destroy_route(struct ksock_route *route); @@ -541,16 +539,14 @@ extern void ksocknal_destroy_route(struct ksock_route *route); static inline void ksocknal_route_decref(struct ksock_route *route) { - LASSERT(atomic_read(&route->ksnr_refcount) > 0); - if (atomic_dec_and_test(&route->ksnr_refcount)) + if (refcount_dec_and_test(&route->ksnr_refcount)) ksocknal_destroy_route (route); } static inline void ksocknal_peer_addref(struct ksock_peer_ni *peer_ni) { - LASSERT(atomic_read(&peer_ni->ksnp_refcount) > 0); - atomic_inc(&peer_ni->ksnp_refcount); + refcount_inc(&peer_ni->ksnp_refcount); } extern void ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni); @@ -558,8 +554,7 @@ extern void ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni); static inline void ksocknal_peer_decref(struct ksock_peer_ni *peer_ni) { - LASSERT (atomic_read (&peer_ni->ksnp_refcount) > 0); - if (atomic_dec_and_test(&peer_ni->ksnp_refcount)) + if (refcount_dec_and_test(&peer_ni->ksnp_refcount)) ksocknal_destroy_peer(peer_ni); } diff --git a/lnet/klnds/socklnd/socklnd_cb.c b/lnet/klnds/socklnd/socklnd_cb.c index a7441fa..2cb93a7 100644 --- a/lnet/klnds/socklnd/socklnd_cb.c +++ b/lnet/klnds/socklnd/socklnd_cb.c @@ -54,7 +54,7 @@ ksocknal_alloc_tx(int type, int size) if (tx == NULL) return NULL; - atomic_set(&tx->tx_refcount, 1); + refcount_set(&tx->tx_refcount, 1); tx->tx_zc_aborted = 0; tx->tx_zc_capable = 0; tx->tx_zc_checked = 0; @@ -455,7 +455,7 @@ ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error) tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR; } - LASSERT(atomic_read(&tx->tx_refcount) == 1); + LASSERT(refcount_read(&tx->tx_refcount) == 1); ksocknal_tx_done(ni, tx, error); } } @@ -1164,7 +1164,7 @@ ksocknal_process_receive(struct ksock_conn *conn, struct lnet_process_id *id; int rc; - LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0); + LASSERT(refcount_read(&conn->ksnc_conn_refcount) > 0); /* NB: sched lock NOT held */ /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */ -- 1.8.3.1