]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use regular reference counting macro for isc_nm_t structure
authorOndřej Surý <ondrej@isc.org>
Wed, 9 Jul 2025 09:01:15 +0000 (11:01 +0200)
committerOndřej Surý <ondrej@isc.org>
Wed, 9 Jul 2025 19:22:48 +0000 (21:22 +0200)
Instead of having hand crafted attach/detach/destroy functions, replace
them with the standard ISC_REFCOUNT macro.  This also have advantage
that delayed netmgr detach (from dns_dispatch) now doesn't cause
assertion failure.  This can happen with delayed (call_rcu) shutdown of
dns_adb.

lib/isc/include/isc/netmgr.h
lib/isc/managers.c
lib/isc/netmgr/netmgr.c
tests/dns/dispatch_test.c
tests/isc/doh_test.c
tests/isc/netmgr_common.c
tests/libtest/isc.c

index 14987514eaab0ce1e2cfd96547bea1cb4a3e7021..1201bea7ef48dca06d43a8c99013a7077de36740 100644 (file)
@@ -123,16 +123,17 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netgmrp);
  * Creates a new network manager and starts it running when loopmgr is started.
  */
 
-void
-isc_netmgr_destroy(isc_nm_t **netmgrp);
-/*%<
- * Similar to isc_nm_detach(), but requires all other references to be gone.
- */
+#if ISC_NETMGR_TRACE
+#define isc_nm_ref(ptr)          isc_nm__ref(ptr, __func__, __FILE__, __LINE__)
+#define isc_nm_unref(ptr) isc_nm__unref(ptr, __func__, __FILE__, __LINE__)
+#define isc_nm_attach(ptr, ptrp) \
+       isc_nm__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
+#define isc_nm_detach(ptrp) isc_nm__detach(ptrp, __func__, __FILE__, __LINE__)
+ISC_REFCOUNT_TRACE_DECL(isc_nm);
+#else
+ISC_REFCOUNT_DECL(isc_nm);
+#endif
 
-void
-isc_nm_attach(isc_nm_t *mgr, isc_nm_t **dst);
-void
-isc_nm_detach(isc_nm_t **mgr0);
 /*%<
  * Attach/detach a network manager. When all references have been
  * released, the network manager is shut down, freeing all resources.
index 0dc856b7cf28a9506455aa787878924f9ef4388a..c06c1f130c4f1a221cdb0f52a08782d4005453c2 100644 (file)
@@ -45,7 +45,7 @@ isc_managers_destroy(isc_mem_t **mctxp, isc_loopmgr_t **loopmgrp,
         * The sequence of operations here is important:
         */
 
-       isc_netmgr_destroy(netmgrp);
+       isc_nm_detach(netmgrp);
        isc_loopmgr_destroy(loopmgrp);
        isc_mem_detach(mctxp);
 }
index 4603f3c8eb18f6270683380893575eafa2230253..da7ad9924ea25ebfc896574b276525cfb529a31e 100644 (file)
@@ -244,62 +244,27 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) {
  * Free the resources of the network manager.
  */
 static void
-nm_destroy(isc_nm_t **mgr0) {
-       REQUIRE(VALID_NM(*mgr0));
+nm_destroy(isc_nm_t *netmgr) {
+       REQUIRE(VALID_NM(netmgr));
 
-       isc_nm_t *mgr = *mgr0;
-       *mgr0 = NULL;
+       isc_refcount_destroy(&netmgr->references);
 
-       isc_refcount_destroy(&mgr->references);
+       netmgr->magic = 0;
 
-       mgr->magic = 0;
-
-       if (mgr->stats != NULL) {
-               isc_stats_detach(&mgr->stats);
+       if (netmgr->stats != NULL) {
+               isc_stats_detach(&netmgr->stats);
        }
 
-       isc_mem_cput(mgr->mctx, mgr->workers, mgr->nloops,
-                    sizeof(mgr->workers[0]));
-       isc_mem_putanddetach(&mgr->mctx, mgr, sizeof(*mgr));
-}
-
-void
-isc_nm_attach(isc_nm_t *mgr, isc_nm_t **dst) {
-       REQUIRE(VALID_NM(mgr));
-       REQUIRE(dst != NULL && *dst == NULL);
-
-       isc_refcount_increment(&mgr->references);
-
-       *dst = mgr;
-}
-
-void
-isc_nm_detach(isc_nm_t **mgr0) {
-       isc_nm_t *mgr = NULL;
-
-       REQUIRE(mgr0 != NULL);
-       REQUIRE(VALID_NM(*mgr0));
-
-       mgr = *mgr0;
-       *mgr0 = NULL;
-
-       if (isc_refcount_decrement(&mgr->references) == 1) {
-               nm_destroy(&mgr);
-       }
+       isc_mem_cput(netmgr->mctx, netmgr->workers, netmgr->nloops,
+                    sizeof(netmgr->workers[0]));
+       isc_mem_putanddetach(&netmgr->mctx, netmgr, sizeof(*netmgr));
 }
 
-void
-isc_netmgr_destroy(isc_nm_t **netmgrp) {
-       isc_nm_t *mgr = NULL;
-
-       REQUIRE(VALID_NM(*netmgrp));
-
-       mgr = *netmgrp;
-       *netmgrp = NULL;
-
-       REQUIRE(isc_refcount_decrement(&mgr->references) == 1);
-       nm_destroy(&mgr);
-}
+#if ISC_NETMGR_TRACE
+ISC_REFCOUNT_TRACE_IMPL(isc_nm, nm_destroy)
+#else
+ISC_REFCOUNT_IMPL(isc_nm, nm_destroy);
+#endif
 
 void
 isc_nm_maxudp(isc_nm_t *mgr, uint32_t maxudp) {
index 343f4416bc4c38190e7d22726c052c8ff7b16bcf..f3189be4dd15d21b92b04a1f8148852814f9107b 100644 (file)
@@ -245,7 +245,7 @@ teardown_test(void **state) {
        isc_tlsctx_cache_detach(&tls_tlsctx_client_cache);
        isc_tlsctx_free(&tls_listen_tlsctx);
 
-       isc_netmgr_destroy(&connect_nm);
+       isc_nm_detach(&connect_nm);
 
        teardown_netmgr(state);
        teardown_loopmgr(state);
index 2e07c6f6ad859046e10bb4023dbd871a5cbd1db8..8b73879aeec225d25b699b75a02c4d7b27386f0d 100644 (file)
@@ -377,7 +377,7 @@ setup_test(void **state) {
 static int
 teardown_test(void **state ISC_ATTR_UNUSED) {
        for (size_t i = 0; i < MAX_NM; i++) {
-               isc_netmgr_destroy(&nm[i]);
+               isc_nm_detach(&nm[i]);
                assert_null(nm[i]);
        }
        isc_mem_cput(mctx, nm, MAX_NM, sizeof(nm[0]));
index 9e92dd480cf5874bb1108b8540df2cf141f879a9..ffb1ccb4abc86ac12701d9f78d88a80acb5f0d28 100644 (file)
@@ -226,10 +226,10 @@ teardown_netmgr_test(void **state ISC_ATTR_UNUSED) {
        isc_tlsctx_free(&tcp_connect_tlsctx);
        isc_tlsctx_free(&tcp_listen_tlsctx);
 
-       isc_netmgr_destroy(&connect_nm);
+       isc_nm_detach(&connect_nm);
        assert_null(connect_nm);
 
-       isc_netmgr_destroy(&listen_nm);
+       isc_nm_detach(&listen_nm);
        assert_null(listen_nm);
 
        teardown_loopmgr(state);
index 4c2fcc6f2c2b4962d87201f386f43fc036078411..db40e5f007d25f7bacf6d30497429462aca2be66 100644 (file)
@@ -121,7 +121,7 @@ int
 teardown_netmgr(void **state ISC_ATTR_UNUSED) {
        REQUIRE(loopmgr != NULL);
 
-       isc_netmgr_destroy(&netmgr);
+       isc_nm_detach(&netmgr);
 
        return 0;
 }