Blob Blame History Raw
From 1528f231b559670445cfc427937174535981917d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
Date: Fri, 18 Sep 2020 14:46:02 +0200
Subject: [PATCH] Fix isc_atomic_xadd symbol missing

Squashed commit of the following:

commit d8afe85342f05d8fec8ee0255451568e936b7eb2
Author: Mark Andrews <marka@isc.org>
Date:   Mon Sep 7 16:12:31 2020 +1000

    Update 'init_count' atomically to silence tsan errors.

    (cherry picked from commit 90185b225f4c7acde2fbb04697d857fe496725a2)

commit f166c7fb7cc262a688ed511e40c9b0d551d5992d
Author: Mark Andrews <marka@isc.org>
Date:   Thu Sep 3 12:53:53 2020 +1000

    The node lock was released too early.

    NEGATIVE needs to be call with the node lock held.

    WARNING: ThreadSanitizer: data race
      Write of size 2 at 0x000000000001 by thread T1 (mutexes: write M1):
        #0 mark_stale_header lib/dns/rbtdb.c:1802:21
        #1 add32 lib/dns/rbtdb.c:6559:5
        #2 addrdataset lib/dns/rbtdb.c:6975:12
        #3 dns_db_addrdataset lib/dns/db.c:783:10
        #4 cache_name lib/dns/resolver.c:5829:13
        #5 cache_message lib/dns/resolver.c:5926:14
        #6 resquery_response lib/dns/resolver.c:8618:12
        #7 dispatch lib/isc/task.c:1157:7
        #8 run lib/isc/task.c:1331:2

      Previous read of size 2 at 0x000000000001 by thread T2:
        #0 cache_findrdataset lib/dns/rbtdb.c:5932:6
        #1 dns_db_findrdataset lib/dns/db.c:739:10
        #2 query_addadditional2 bin/named/query.c:2196:11
        #3 additionaldata_ns lib/dns/./rdata/generic/ns_2.c:198:10
        #4 dns_rdata_additionaldata lib/dns/rdata.c:1246:2
        #5 dns_rdataset_additionaldata lib/dns/rdataset.c:629:12
        #6 query_addrdataset bin/named/query.c:2411:8
        #7 query_addrrset bin/named/query.c:2802:2
        #8 query_addbestns bin/named/query.c:3501:2
        #9 query_find bin/named/query.c:9165:4
        #10 query_resume bin/named/query.c:4164:12
        #11 dispatch lib/isc/task.c:1157:7
        #12 run lib/isc/task.c:1331:2

    (cherry picked from commit a1dcb73f677969d99df3ccff2acf4737e18a72b1)

commit 591a80fa95b8f3f3e716c45ceb9c5e4ccfa551c8
Author: Mark Andrews <marka@isc.org>
Date:   Mon Aug 24 17:34:58 2020 +1000

    increment header->count atomically

    (cherry picked from commit 121837aa75ced489e28f8ce1dd20315487d199fa)
---
 lib/dns/rbtdb.c | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c
index 88c39bf714..31ced8e73a 100644
--- a/lib/dns/rbtdb.c
+++ b/lib/dns/rbtdb.c
@@ -399,6 +399,23 @@ typedef isc_mutex_t nodelock_t;
 #define NODE_WEAKDOWNGRADE(l)   ((void)0)
 #endif
 
+#if defined(ISC_PLATFORM_HAVESTDATOMIC)
+#if defined(__cplusplus)
+#include <isc/stdatomic.h>
+#else
+#include <stdatomic.h>
+#endif
+#define DNS_RBTDB_STDATOMIC 1
+#define DNS_RBTDB_INC(x) atomic_fetch_add(&(x), (1))
+#define DNS_RBTDB_LOAD(x) atomic_load(&(x))
+#elif defined(ISC_PLATFORM_HAVEXADD)
+#define DNS_RBTDB_INC(x) isc_atomic_xadd((int *)&(x), 1);
+#define DNS_RBTDB_LOAD(x) isc_atomic_xadd((int *)&(x), 0);
+#else
+#define DNS_RBTDB_INC(x) ((x)++)
+#define DNS_RBTDB_LOAD(x) (x)
+#endif
+
 /*%
  * Whether to rate-limit updating the LRU to avoid possible thread contention.
  * Our performance measurement has shown the cost is marginal, so it's defined
@@ -457,7 +474,11 @@ typedef struct rdatasetheader {
 	 * this rdataset.
 	 */
 
-	uint32_t                    count;
+#ifdef DNS_RBTDB_STDATOMIC
+	_Atomic(uint32_t)                count;
+#else
+	uint32_t                         count;
+#endif
 	/*%<
 	 * Monotonously increased every time this rdataset is bound so that
 	 * it is used as the base of the starting point in DNS responses
@@ -952,7 +973,11 @@ static char FILE_VERSION[32] = "\0";
  *      that indicates that the database does not implement cyclic
  *      processing.
  */
+#ifdef DNS_RBTDB_STDATOMIC
+static _Atomic(unsigned int) init_count;
+#else
 static unsigned int init_count;
+#endif
 
 /*
  * Locking
@@ -3322,7 +3347,7 @@ bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, rdatasetheader_t *header,
 	rdataset->private2 = node;
 	raw = (unsigned char *)header + sizeof(*header);
 	rdataset->private3 = raw;
-	rdataset->count = header->count++;
+	rdataset->count = DNS_RBTDB_INC(header->count);
 	if (rdataset->count == UINT32_MAX)
 		rdataset->count = 0;
 
@@ -5924,10 +5949,10 @@ cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
 		}
 	}
 
-	NODE_UNLOCK(lock, locktype);
-
-	if (found == NULL)
+	if (found == NULL) {
+		NODE_UNLOCK(lock, locktype);
 		return (ISC_R_NOTFOUND);
+	}
 
 	if (NEGATIVE(found)) {
 		/*
@@ -5939,6 +5964,8 @@ cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
 			result = DNS_R_NCACHENXRRSET;
 	}
 
+	NODE_UNLOCK(lock, locktype);
+
 	update_cachestats(rbtdb, result);
 
 	return (result);
@@ -6839,7 +6866,7 @@ addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
 		newheader->attributes |= RDATASET_ATTR_ZEROTTL;
 	newheader->noqname = NULL;
 	newheader->closest = NULL;
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
+	newheader->count = DNS_RBTDB_INC(init_count);
 	newheader->trust = rdataset->trust;
 	newheader->additional_auth = NULL;
 	newheader->additional_glue = NULL;
@@ -7035,7 +7062,7 @@ subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
 	newheader->trust = 0;
 	newheader->noqname = NULL;
 	newheader->closest = NULL;
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
+	newheader->count = DNS_RBTDB_INC(init_count);
 	newheader->additional_auth = NULL;
 	newheader->additional_glue = NULL;
 	newheader->last_used = 0;
@@ -7481,7 +7508,7 @@ loading_addrdataset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset) {
 	newheader->serial = 1;
 	newheader->noqname = NULL;
 	newheader->closest = NULL;
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
+	newheader->count = DNS_RBTDB_INC(init_count);
 	newheader->additional_auth = NULL;
 	newheader->additional_glue = NULL;
 	newheader->last_used = 0;
-- 
2.26.2