Commit 39950d6682 for bind

commit 39950d6682211bb76a9ed81e929fedbef1c84909
Author: OndÅ™ej Surý <ondrej@isc.org>
Date:   Thu Nov 26 08:25:27 2026 +0100

    Keep the delegation database in a plain qp-trie under a rwlock

    Each referral was stored in its own dns_qpmulti_t write transaction,
    which copies every twigs vector between the root and the changed
    node.  The zone cuts are chosen by remote servers, so the owner of a
    zone could make every insertion copy thousands of trie cells under
    the trie's mutex and stall the resolver.  Use a plain dns_qp_t
    protected by a rwlock instead, as the cache does.

diff --git a/lib/dns/deleg.c b/lib/dns/deleg.c
index 470d9df06f..0c98d5b8cb 100644
--- a/lib/dns/deleg.c
+++ b/lib/dns/deleg.c
@@ -13,6 +13,7 @@
 #include <isc/magic.h>
 #include <isc/mem.h>
 #include <isc/netaddr.h>
+#include <isc/rwlock.h>
 #include <isc/sieve.h>
 #include <isc/stdtime.h>
 #include <isc/urcu.h>
@@ -39,8 +40,11 @@ typedef struct delegdb_node delegdb_node_t;
 typedef struct qplru {
 	isc_mem_t *mctx;
 	isc_refcount_t references;
-	dns_qpmulti_t *nodes;
+
+	isc_rwlock_t lock;
+	dns_qp_t *nodes;
 	ISC_SIEVE(delegdb_node_t) lru;
+
 	struct rcu_head rcu_head;
 } qplru_t;

@@ -220,7 +224,8 @@ dns_delegdb_create(dns_delegdb_t **delegdbp) {
 		.references = ISC_REFCOUNT_INITIALIZER(1),
 	};

-	dns_qpmulti_create(mctx, &qpmethods, &qplru->nodes, &qplru->nodes);
+	isc_rwlock_init(&qplru->lock);
+	dns_qp_create(mctx, &qpmethods, &qplru->nodes, &qplru->nodes);
 	ISC_SIEVE_INIT(qplru->lru);

 	delegdb->qplru = MOVE_OWNERSHIP(qplru);
@@ -232,6 +237,7 @@ dns_delegdb_create(dns_delegdb_t **delegdbp) {

 static void
 qplru_destroy(qplru_t *qplru) {
+	isc_rwlock_destroy(&qplru->lock);
 	isc_mem_putanddetach(&qplru->mctx, qplru, sizeof(*qplru));
 }

@@ -271,10 +277,10 @@ getparentnode(dns_qpchain_t *chain, delegdb_node_t **node, dns_ttl_t now) {
 }

 /*
- * NOTE: Caller needs to hold a RCU read critical section.
+ * NOTE: Caller needs to hold the qplru read-lock
  */
 static isc_result_t
-deleg_lookup(dns_delegdb_t *delegdb, dns_qpread_t *qpr, const dns_name_t *name,
+deleg_lookup(dns_delegdb_t *delegdb, dns_qp_t *qp, const dns_name_t *name,
 	     isc_stdtime_t optnow, unsigned int options, dns_name_t *zonecut,
 	     dns_name_t *deepestzonecut, dns_delegset_t **delegsetp) {
 	isc_result_t result = ISC_R_SUCCESS;
@@ -290,7 +296,7 @@ deleg_lookup(dns_delegdb_t *delegdb, dns_qpread_t *qpr, const dns_name_t *name,
 	REQUIRE(zonecut == NULL || dns_name_hasbuffer(zonecut));
 	REQUIRE(deepestzonecut == NULL || dns_name_hasbuffer(deepestzonecut));

-	result = dns_qp_lookup(qpr, name, DNS_DBNAMESPACE_NORMAL, NULL, &chain,
+	result = dns_qp_lookup(qp, name, DNS_DBNAMESPACE_NORMAL, NULL, &chain,
 			       (void **)&node, NULL);

 	if (result != ISC_R_SUCCESS && result != DNS_R_PARTIALMATCH) {
@@ -366,7 +372,7 @@ dns_delegdb_lookup(dns_delegdb_t *delegdb, const dns_name_t *name,
 		   isc_stdtime_t now, unsigned int options, dns_name_t *zonecut,
 		   dns_name_t *deepestzonecut, dns_delegset_t **delegsetp) {
 	isc_result_t result = ISC_R_SHUTTINGDOWN;
-	dns_qpread_t qpr = {};
+	qplru_t *qplru = delegdb->qplru;
 	char namebuf[DNS_NAME_FORMATSIZE];

 	if (LIBDNS_DELEGDB_LOOKUP_START_ENABLED() ||
@@ -376,10 +382,10 @@ dns_delegdb_lookup(dns_delegdb_t *delegdb, const dns_name_t *name,
 	}
 	LIBDNS_DELEGDB_LOOKUP_START(delegdb, namebuf);

-	dns_qpmulti_query(delegdb->qplru->nodes, &qpr);
-	result = deleg_lookup(delegdb, &qpr, name, now, options, zonecut,
-			      deepestzonecut, delegsetp);
-	dns_qpread_destroy(delegdb->qplru->nodes, &qpr);
+	RWLOCK(&qplru->lock, isc_rwlocktype_read);
+	result = deleg_lookup(delegdb, qplru->nodes, name, now, options,
+			      zonecut, deepestzonecut, delegsetp);
+	RWUNLOCK(&qplru->lock, isc_rwlocktype_read);

 	LIBDNS_DELEGDB_LOOKUP_DONE(delegdb, namebuf, result);

@@ -490,6 +496,9 @@ dns_delegset_addns(dns_delegset_t *delegset, dns_deleg_t *deleg,
 static size_t
 delegset_size(dns_delegset_t *delegset);

+/*
+ * NOTE: Caller needs to hold the qplru write-lock.
+ */
 static void
 delegdb_cleanup(dns_delegdb_t *delegdb, dns_qp_t *qp, size_t requested) {
 	delegdb_node_t *node = NULL;
@@ -603,32 +612,24 @@ static void
 insertroot(dns_delegdb_t *delegdb, isc_stdtime_t now, dns_ttl_t ttl,
 	   dns_delegset_t *delegset) {
 	delegdb_node_t *node = NULL;
-	dns_qp_t *qp = NULL;
+	qplru_t *qplru = delegdb->qplru;

 	(void)delegdb_node_prepare(delegdb, now, ttl, dns_rootname, delegset,
 				   &node);

-	dns_qpmulti_write(delegdb->qplru->nodes, &qp);
+	RWLOCK(&qplru->lock, isc_rwlocktype_write);

-	(void)dns_qp_deletename(qp, dns_rootname, DNS_DBNAMESPACE_NORMAL, NULL,
-				NULL);
-	(void)dns_qp_insert(qp, node, 0);
+	(void)dns_qp_deletename(qplru->nodes, dns_rootname,
+				DNS_DBNAMESPACE_NORMAL, NULL, NULL);
+	(void)dns_qp_insert(qplru->nodes, node, 0);
 	delegdb_node_unref(node);

-	dns_qp_compact(qp, DNS_QPGC_MAYBE);
-	dns_qpmulti_commit(delegdb->qplru->nodes, &qp);
+	RWUNLOCK(&qplru->lock, isc_rwlocktype_write);
 }

 isc_result_t
 dns_delegset_insert(dns_delegdb_t *delegdb, const dns_name_t *zonecut,
 		    dns_ttl_t ttl, dns_delegset_t *delegset) {
-	isc_result_t result;
-	delegdb_node_t *node = NULL;
-	dns_qp_t *qp = NULL;
-	dns_qpread_t qpr = {};
-	isc_stdtime_t now = isc_stdtime_now();
-	char zonecutbuf[DNS_NAME_FORMATSIZE];
-
 	REQUIRE(VALID_DELEGDB(delegdb));
 	REQUIRE(DNS_NAME_VALID(zonecut));
 	REQUIRE(DNS_DELEGSET_VALID(delegset));
@@ -640,6 +641,12 @@ dns_delegset_insert(dns_delegdb_t *delegdb, const dns_name_t *zonecut,
 	 */
 	REQUIRE(delegset->mctx == delegdb->mctx);

+	isc_result_t result;
+	delegdb_node_t *node = NULL;
+	qplru_t *qplru = delegdb->qplru;
+	isc_stdtime_t now = isc_stdtime_now();
+	char zonecutbuf[DNS_NAME_FORMATSIZE];
+
 	if (LIBDNS_DELEGDB_INSERT_START_ENABLED() ||
 	    LIBDNS_DELEGDB_INSERT_DONE_ENABLED())
 	{
@@ -660,75 +667,51 @@ dns_delegset_insert(dns_delegdb_t *delegdb, const dns_name_t *zonecut,
 		goto cleanup;
 	}

-	/*
-	 * First, check (without write txn) if the node already exists and is
-	 * still valid.
-	 */
-	dns_qpmulti_query(delegdb->qplru->nodes, &qpr);
-	result = dns_qp_lookup(&qpr, zonecut, DNS_DBNAMESPACE_NORMAL, NULL,
-			       NULL, (void **)&node, NULL);
-	if (result == ISC_R_SUCCESS) {
-		INSIST(VALID_DELEGDB_NODE(node));
-		if (node->delegset->expires > now) {
-			dns_qpread_destroy(delegdb->qplru->nodes, &qpr);
-			CLEANUP(ISC_R_EXISTS);
-		}
-	}
-	dns_qpread_destroy(delegdb->qplru->nodes, &qpr);
-
-	/*
-	 * We're about to add a new delegation, check for state of overmem, and
-	 * clean up expired/least recently used delegation, then allocate and
-	 * initialize a new node.
-	 */
 	ttl = normalize_ttl(delegdb, ttl);
 	size_t requested = delegdb_node_prepare(delegdb, now, ttl, zonecut,
 						delegset, &node) +
 			   delegset_size(delegset);

-	/*
-	 * Add the node in the DB
-	 */
-	dns_qpmulti_write(delegdb->qplru->nodes, &qp);
-
-	delegdb_cleanup(delegdb, qp, requested);
+	RWLOCK(&qplru->lock, isc_rwlocktype_write);

-	if (result == ISC_R_SUCCESS) {
-		delegdb_node_t *old_node = NULL;
-		result = dns_qp_deletename(qp, zonecut, DNS_DBNAMESPACE_NORMAL,
-					   (void *)&old_node, NULL);
-		if (result == ISC_R_SUCCESS) {
-			ISC_SIEVE_UNLINK(delegdb->qplru->lru, old_node, link);
-			delegdb_node_detach(&old_node);
-		}
-	}
-
-	result = dns_qp_insert(qp, node, 0);
-	if (result != ISC_R_SUCCESS) {
+	result = dns_qp_insert(qplru->nodes, node, 0);
+	if (result == ISC_R_EXISTS) {
 		/*
-		 * Someone else added the node before (and there was no node to
-		 * delete).
+		 * Keep a delegation that is still valid, replace an expired
+		 * one.
 		 */
+		delegdb_node_t *old_node = NULL;
+		result = dns_qp_getname(qplru->nodes, zonecut,
+					DNS_DBNAMESPACE_NORMAL,
+					(void **)&old_node, NULL);
+		INSIST(result == ISC_R_SUCCESS);
+		if (isactive(old_node, now)) {
+			RWUNLOCK(&qplru->lock, isc_rwlocktype_write);
+			delegdb_node_unref(node);
+			CLEANUP(ISC_R_EXISTS);
+		}

-		delegdb_node_unref(node);
+		result = dns_qp_deletename(qplru->nodes, zonecut,
+					   DNS_DBNAMESPACE_NORMAL, NULL, NULL);
+		INSIST(result == ISC_R_SUCCESS);
+		ISC_SIEVE_UNLINK(qplru->lru, old_node, link);
+		delegdb_node_detach(&old_node);

-		/*
-		 * Since not using an update (but write) transaction,
-		 * _rollback() won't work here.
-		 */
-		dns_qpmulti_commit(delegdb->qplru->nodes, &qp);
-		CLEANUP(ISC_R_EXISTS);
+		result = dns_qp_insert(qplru->nodes, node, 0);
 	}
+	INSIST(result == ISC_R_SUCCESS);

 	/*
-	 * The new delegation is added, and can be referenced by SIEVE
+	 * The new node is not on the SIEVE list yet, so it cannot be evicted
+	 * here.
 	 */
+	delegdb_cleanup(delegdb, qplru->nodes, requested);
+
 	delegdb_node_ref(node);
-	ISC_SIEVE_INSERT(delegdb->qplru->lru, node, link);
+	ISC_SIEVE_INSERT(qplru->lru, node, link);

 	delegdb_node_unref(node);
-	dns_qp_compact(qp, DNS_QPGC_MAYBE);
-	dns_qpmulti_commit(delegdb->qplru->nodes, &qp);
+	RWUNLOCK(&qplru->lock, isc_rwlocktype_write);

 cleanup:
 	LIBDNS_DELEGDB_INSERT_DONE(delegdb, zonecutbuf, result);
@@ -883,13 +866,17 @@ dns_delegdb_dump(dns_delegdb_t *delegdb, bool expired, FILE *fp) {
 	REQUIRE(fp != NULL);

 	dns_qpiter_t it;
-	dns_qpread_t qpr = {};
+	qplru_t *qplru = delegdb->qplru;
 	delegdb_node_t *node = NULL;
 	isc_stdtime_t now = isc_stdtime_now();

-	dns_qpmulti_query(delegdb->qplru->nodes, &qpr);
+	/*
+	 * The iterator is only valid while the trie does not change, so the
+	 * read lock is held for the whole dump.
+	 */
+	RWLOCK(&qplru->lock, isc_rwlocktype_read);

-	dns_qpiter_init(&qpr, &it);
+	dns_qpiter_init(qplru->nodes, &it);
 	while (dns_qpiter_next(&it, (void **)&node, NULL) == ISC_R_SUCCESS) {
 		if (!expired && !isactive(node, now) && !isrootnode(node)) {
 			continue;
@@ -898,7 +885,7 @@ dns_delegdb_dump(dns_delegdb_t *delegdb, bool expired, FILE *fp) {
 		delegset_tostring(&node->zonecut, node->delegset, now, fp);
 	}

-	dns_qpread_destroy(delegdb->qplru->nodes, &qpr);
+	RWUNLOCK(&qplru->lock, isc_rwlocktype_read);
 }

 void
@@ -981,14 +968,14 @@ dns_delegset_copy(dns_delegset_t *src, dns_delegdb_t *db,
 }

 static isc_result_t
-deleg_deletetree(qplru_t *qplru, dns_qp_t *qp, const dns_name_t *name) {
+deleg_deletetree(qplru_t *qplru, const dns_name_t *name) {
 	isc_result_t result;
 	delegdb_node_t *node = NULL;
 	dns_qpiter_t it;
 	ISC_LIST(delegdb_node_t) deadnodes = ISC_LIST_INITIALIZER;

-	result = dns_qp_lookup(qp, name, DNS_DBNAMESPACE_NORMAL, &it, NULL,
-			       (void **)&node, NULL);
+	result = dns_qp_lookup(qplru->nodes, name, DNS_DBNAMESPACE_NORMAL, &it,
+			       NULL, (void **)&node, NULL);
 	if (result != ISC_R_SUCCESS) {
 		goto out;
 	}
@@ -1025,7 +1012,8 @@ out:
 		 */
 		ISC_LIST_FOREACH(deadnodes, deadnode, deadlink) {
 			delegdb_node_t *old_node = NULL;
-			result = dns_qp_deletename(qp, &deadnode->zonecut,
+			result = dns_qp_deletename(qplru->nodes,
+						   &deadnode->zonecut,
 						   DNS_DBNAMESPACE_NORMAL,
 						   (void *)&old_node, NULL);
 			INSIST(result == ISC_R_SUCCESS);
@@ -1039,10 +1027,11 @@ out:
 }

 static isc_result_t
-deleg_deletenode(qplru_t *qplru, dns_qp_t *qp, const dns_name_t *name) {
+deleg_deletenode(qplru_t *qplru, const dns_name_t *name) {
 	delegdb_node_t *old_node = NULL;
-	isc_result_t result = dns_qp_deletename(
-		qp, name, DNS_DBNAMESPACE_NORMAL, (void *)&old_node, NULL);
+	isc_result_t result = dns_qp_deletename(qplru->nodes, name,
+						DNS_DBNAMESPACE_NORMAL,
+						(void *)&old_node, NULL);
 	if (result == ISC_R_SUCCESS) {
 		ISC_SIEVE_UNLINK(qplru->lru, old_node, link);
 		delegdb_node_detach(&old_node);
@@ -1056,7 +1045,7 @@ dns_delegdb_delete(dns_delegdb_t *delegdb, const dns_name_t *name, bool tree) {
 	REQUIRE(VALID_DELEGDB(delegdb));
 	REQUIRE(DNS_NAME_VALID(name));

-	dns_qp_t *qp = NULL;
+	qplru_t *qplru = delegdb->qplru;
 	isc_result_t result = ISC_R_SHUTTINGDOWN;
 	char namebuf[DNS_NAME_FORMATSIZE];

@@ -1071,16 +1060,13 @@ dns_delegdb_delete(dns_delegdb_t *delegdb, const dns_name_t *name, bool tree) {
 		dns_name_format(name, namebuf, sizeof(namebuf));
 	}

-	dns_qpmulti_write(delegdb->qplru->nodes, &qp);
+	RWLOCK(&qplru->lock, isc_rwlocktype_write);
 	if (tree) {
-		result = deleg_deletetree(delegdb->qplru, qp, name);
+		result = deleg_deletetree(qplru, name);
 	} else {
-		result = deleg_deletenode(delegdb->qplru, qp, name);
-	}
-	if (result == ISC_R_SUCCESS) {
-		dns_qp_compact(qp, DNS_QPGC_MAYBE);
+		result = deleg_deletenode(qplru, name);
 	}
-	dns_qpmulti_commit(delegdb->qplru->nodes, &qp);
+	RWUNLOCK(&qplru->lock, isc_rwlocktype_write);

 	LIBDNS_DELEGDB_DELETE(delegdb, namebuf, (int)tree, result);

@@ -1096,7 +1082,7 @@ qplru_shutdown_rcu(struct rcu_head *rcu_head) {
 		delegdb_node_detach(&node);
 	}

-	dns_qpmulti_destroy(&qplru->nodes);
+	dns_qp_destroy(&qplru->nodes);

 	qplru_detach(&qplru);
 }
diff --git a/tests/dns/deleg_test.c b/tests/dns/deleg_test.c
index 4e7a451684..f49c192bc7 100644
--- a/tests/dns/deleg_test.c
+++ b/tests/dns/deleg_test.c
@@ -40,7 +40,6 @@ isc_stdtime_now(void) {
 #include <isc/loop.h>
 #include <isc/netaddr.h>
 #include <isc/stdtime.h>
-#include <isc/urcu.h>

 #include <dns/callbacks.h>
 #include <dns/deleg.h>
@@ -701,13 +700,9 @@ cleanuptests(ISC_ATTR_UNUSED void *arg) {
 	deleg = NULL;

 	/*
-	 * stuff. internal node (and delegset) is now removed.  Node
-	 * destruction runs synchronously inside the QP-trie chunk reclamation,
-	 * so rcu_barrier() is enough: once it returns, the evicted nodes have
-	 * been detached and freed.
+	 * stuff. internal node (and delegset) is now removed. The evicted
+	 * nodes are detached from the QP-trie and freed during the insertion.
 	 */
-	rcu_barrier();
-
 	assert_int_in_range(isc_mem_inuse(db->mctx), ENTRIES_MEM(NENTRIES),
 			    ENTRIES_MEM(NENTRIES) + 100000);

@@ -735,13 +730,11 @@ cleanuptests(ISC_ATTR_UNUSED void *arg) {
 	deleg = NULL;

 	/*
-	 * Re-adding baz. hit the hiwater mark and evicted bar.; wait for the
-	 * reclamation to free it before checking memory and final state.
+	 * Re-adding baz. hit the hiwater mark and evicted bar., which has
+	 * been freed, so only the new baz. delegset is left.
 	 */
-	rcu_barrier();
-
-	assert_int_in_range(isc_mem_inuse(db->mctx), ENTRIES_MEM(2 * NENTRIES),
-			    ENTRIES_MEM(2 * NENTRIES) + 100000);
+	assert_int_in_range(isc_mem_inuse(db->mctx), ENTRIES_MEM(NENTRIES),
+			    ENTRIES_MEM(NENTRIES) + 100000);

 	/*
 	 * baz. is there, but bar. is gone, as it has been