Backport of: From 17623d26e4e7b0fd45f2b39f00cd46e6044ce4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 17 Apr 2019 15:22:27 +0200 Subject: [PATCH] Replace atomic operations in bin/named/client.c with isc_refcount reference counting --- bin/named/client.c | 18 +++++++----------- bin/named/include/named/interfacemgr.h | 5 +++-- bin/named/interfacemgr.c | 7 +++++-- 3 files changed, 15 insertions(+), 15 deletions(-) Index: bind9-9.11.4+dfsg/bin/named/client.c =================================================================== --- bind9-9.11.4+dfsg.orig/bin/named/client.c 2019-04-24 15:25:11.891463104 -0400 +++ bind9-9.11.4+dfsg/bin/named/client.c 2019-04-24 15:25:42.091541114 -0400 @@ -399,12 +399,10 @@ tcpconn_detach(ns_client_t *client) { static void mark_tcp_active(ns_client_t *client, isc_boolean_t active) { if (active && !client->tcpactive) { - isc_atomic_xadd(&client->interface->ntcpactive, 1); + isc_refcount_increment0(&client->interface->ntcpactive, NULL); client->tcpactive = active; } else if (!active && client->tcpactive) { - uint32_t old = - isc_atomic_xadd(&client->interface->ntcpactive, -1); - INSIST(old > 0); + isc_refcount_decrement(&client->interface->ntcpactive, NULL); client->tcpactive = active; } } @@ -551,7 +549,7 @@ exit_check(ns_client_t *client) { if (client->mortal && TCP_CLIENT(client) && client->newstate != NS_CLIENTSTATE_FREED && !ns_g_clienttest && - isc_atomic_xadd(&client->interface->ntcpaccepting, 0) == 0) + isc_refcount_current(&client->interface->ntcpaccepting) == 0) { /* Nobody else is accepting */ client->mortal = ISC_FALSE; @@ -3314,7 +3312,6 @@ client_newconn(isc_task_t *task, isc_eve isc_result_t result; ns_client_t *client = event->ev_arg; isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event; - isc_uint32_t old; REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN); REQUIRE(NS_CLIENT_VALID(client)); @@ -3334,8 +3331,7 @@ client_newconn(isc_task_t *task, isc_eve INSIST(client->naccepts == 1); client->naccepts--; - old = isc_atomic_xadd(&client->interface->ntcpaccepting, -1); - INSIST(old > 0); + isc_refcount_decrement(&client->interface->ntcpaccepting, NULL); /* * We must take ownership of the new socket before the exit @@ -3466,8 +3462,8 @@ client_accept(ns_client_t *client) { * quota is tcp-clients plus the number of listening * interfaces plus 1.) */ - exit = (isc_atomic_xadd(&client->interface->ntcpactive, 0) > - (client->tcpactive ? 1 : 0)); + exit = (isc_refcount_current(&client->interface->ntcpactive) > + (client->tcpactive ? 1U : 0U)); if (exit) { client->newstate = NS_CLIENTSTATE_INACTIVE; (void)exit_check(client); @@ -3525,7 +3521,7 @@ client_accept(ns_client_t *client) { * listening for connections itself to prevent the interface * going dead. */ - isc_atomic_xadd(&client->interface->ntcpaccepting, 1); + isc_refcount_increment0(&client->interface->ntcpaccepting, NULL); } static void Index: bind9-9.11.4+dfsg/bin/named/include/named/interfacemgr.h =================================================================== --- bind9-9.11.4+dfsg.orig/bin/named/include/named/interfacemgr.h 2019-04-24 15:25:11.891463104 -0400 +++ bind9-9.11.4+dfsg/bin/named/include/named/interfacemgr.h 2019-04-24 15:26:03.943597701 -0400 @@ -43,6 +43,7 @@ #include #include #include +#include #include @@ -73,11 +74,11 @@ struct ns_interface { /*%< UDP dispatchers. */ isc_socket_t * tcpsocket; /*%< TCP socket. */ isc_dscp_t dscp; /*%< "listen-on" DSCP value */ - isc_int32_t ntcpaccepting; /*%< Number of clients + isc_refcount_t ntcpaccepting; /*%< Number of clients ready to accept new TCP connections on this interface */ - isc_int32_t ntcpactive; /*%< Number of clients + isc_refcount_t ntcpactive; /*%< Number of clients servicing TCP queries (whether accepting or connected) */ Index: bind9-9.11.4+dfsg/bin/named/interfacemgr.c =================================================================== --- bind9-9.11.4+dfsg.orig/bin/named/interfacemgr.c 2019-04-24 15:25:11.891463104 -0400 +++ bind9-9.11.4+dfsg/bin/named/interfacemgr.c 2019-04-24 15:25:11.891463104 -0400 @@ -384,8 +384,8 @@ ns_interface_create(ns_interfacemgr_t *m * connections will be handled in parallel even though there is * only one client initially. */ - ifp->ntcpaccepting = 0; - ifp->ntcpactive = 0; + isc_refcount_init(&ifp->ntcpaccepting, 0); + isc_refcount_init(&ifp->ntcpactive, 0); ifp->nudpdispatch = 0; @@ -616,6 +616,9 @@ ns_interface_destroy(ns_interface_t *ifp ns_interfacemgr_detach(&ifp->mgr); + isc_refcount_destroy(&ifp->ntcpactive); + isc_refcount_destroy(&ifp->ntcpaccepting); + ifp->magic = 0; isc_mem_put(mctx, ifp, sizeof(*ifp)); }