392ac7
From 1528f231b559670445cfc427937174535981917d Mon Sep 17 00:00:00 2001
392ac7
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
392ac7
Date: Fri, 18 Sep 2020 14:46:02 +0200
392ac7
Subject: [PATCH] Fix isc_atomic_xadd symbol missing
392ac7
392ac7
Squashed commit of the following:
392ac7
392ac7
commit d8afe85342f05d8fec8ee0255451568e936b7eb2
392ac7
Author: Mark Andrews <marka@isc.org>
392ac7
Date:   Mon Sep 7 16:12:31 2020 +1000
392ac7
392ac7
    Update 'init_count' atomically to silence tsan errors.
392ac7
392ac7
    (cherry picked from commit 90185b225f4c7acde2fbb04697d857fe496725a2)
392ac7
392ac7
commit f166c7fb7cc262a688ed511e40c9b0d551d5992d
392ac7
Author: Mark Andrews <marka@isc.org>
392ac7
Date:   Thu Sep 3 12:53:53 2020 +1000
392ac7
392ac7
    The node lock was released too early.
392ac7
392ac7
    NEGATIVE needs to be call with the node lock held.
392ac7
392ac7
    WARNING: ThreadSanitizer: data race
392ac7
      Write of size 2 at 0x000000000001 by thread T1 (mutexes: write M1):
392ac7
        #0 mark_stale_header lib/dns/rbtdb.c:1802:21
392ac7
        #1 add32 lib/dns/rbtdb.c:6559:5
392ac7
        #2 addrdataset lib/dns/rbtdb.c:6975:12
392ac7
        #3 dns_db_addrdataset lib/dns/db.c:783:10
392ac7
        #4 cache_name lib/dns/resolver.c:5829:13
392ac7
        #5 cache_message lib/dns/resolver.c:5926:14
392ac7
        #6 resquery_response lib/dns/resolver.c:8618:12
392ac7
        #7 dispatch lib/isc/task.c:1157:7
392ac7
        #8 run lib/isc/task.c:1331:2
392ac7
392ac7
      Previous read of size 2 at 0x000000000001 by thread T2:
392ac7
        #0 cache_findrdataset lib/dns/rbtdb.c:5932:6
392ac7
        #1 dns_db_findrdataset lib/dns/db.c:739:10
392ac7
        #2 query_addadditional2 bin/named/query.c:2196:11
392ac7
        #3 additionaldata_ns lib/dns/./rdata/generic/ns_2.c:198:10
392ac7
        #4 dns_rdata_additionaldata lib/dns/rdata.c:1246:2
392ac7
        #5 dns_rdataset_additionaldata lib/dns/rdataset.c:629:12
392ac7
        #6 query_addrdataset bin/named/query.c:2411:8
392ac7
        #7 query_addrrset bin/named/query.c:2802:2
392ac7
        #8 query_addbestns bin/named/query.c:3501:2
392ac7
        #9 query_find bin/named/query.c:9165:4
392ac7
        #10 query_resume bin/named/query.c:4164:12
392ac7
        #11 dispatch lib/isc/task.c:1157:7
392ac7
        #12 run lib/isc/task.c:1331:2
392ac7
392ac7
    (cherry picked from commit a1dcb73f677969d99df3ccff2acf4737e18a72b1)
392ac7
392ac7
commit 591a80fa95b8f3f3e716c45ceb9c5e4ccfa551c8
392ac7
Author: Mark Andrews <marka@isc.org>
392ac7
Date:   Mon Aug 24 17:34:58 2020 +1000
392ac7
392ac7
    increment header->count atomically
392ac7
392ac7
    (cherry picked from commit 121837aa75ced489e28f8ce1dd20315487d199fa)
392ac7
---
392ac7
 lib/dns/rbtdb.c | 43 +++++++++++++++++++++++++++++++++++--------
392ac7
 1 file changed, 35 insertions(+), 8 deletions(-)
392ac7
392ac7
diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c
392ac7
index 88c39bf714..31ced8e73a 100644
392ac7
--- a/lib/dns/rbtdb.c
392ac7
+++ b/lib/dns/rbtdb.c
392ac7
@@ -399,6 +399,23 @@ typedef isc_mutex_t nodelock_t;
392ac7
 #define NODE_WEAKDOWNGRADE(l)   ((void)0)
392ac7
 #endif
392ac7
 
392ac7
+#if defined(ISC_PLATFORM_HAVESTDATOMIC)
392ac7
+#if defined(__cplusplus)
392ac7
+#include <isc/stdatomic.h>
392ac7
+#else
392ac7
+#include <stdatomic.h>
392ac7
+#endif
392ac7
+#define DNS_RBTDB_STDATOMIC 1
392ac7
+#define DNS_RBTDB_INC(x) atomic_fetch_add(&(x), (1))
392ac7
+#define DNS_RBTDB_LOAD(x) atomic_load(&(x))
392ac7
+#elif defined(ISC_PLATFORM_HAVEXADD)
392ac7
+#define DNS_RBTDB_INC(x) isc_atomic_xadd((int *)&(x), 1);
392ac7
+#define DNS_RBTDB_LOAD(x) isc_atomic_xadd((int *)&(x), 0);
392ac7
+#else
392ac7
+#define DNS_RBTDB_INC(x) ((x)++)
392ac7
+#define DNS_RBTDB_LOAD(x) (x)
392ac7
+#endif
392ac7
+
392ac7
 /*%
392ac7
  * Whether to rate-limit updating the LRU to avoid possible thread contention.
392ac7
  * Our performance measurement has shown the cost is marginal, so it's defined
392ac7
@@ -457,7 +474,11 @@ typedef struct rdatasetheader {
392ac7
 	 * this rdataset.
392ac7
 	 */
392ac7
 
392ac7
-	uint32_t                    count;
392ac7
+#ifdef DNS_RBTDB_STDATOMIC
392ac7
+	_Atomic(uint32_t)                count;
392ac7
+#else
392ac7
+	uint32_t                         count;
392ac7
+#endif
392ac7
 	/*%<
392ac7
 	 * Monotonously increased every time this rdataset is bound so that
392ac7
 	 * it is used as the base of the starting point in DNS responses
392ac7
@@ -952,7 +973,11 @@ static char FILE_VERSION[32] = "\0";
392ac7
  *      that indicates that the database does not implement cyclic
392ac7
  *      processing.
392ac7
  */
392ac7
+#ifdef DNS_RBTDB_STDATOMIC
392ac7
+static _Atomic(unsigned int) init_count;
392ac7
+#else
392ac7
 static unsigned int init_count;
392ac7
+#endif
392ac7
 
392ac7
 /*
392ac7
  * Locking
392ac7
@@ -3322,7 +3347,7 @@ bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, rdatasetheader_t *header,
392ac7
 	rdataset->private2 = node;
392ac7
 	raw = (unsigned char *)header + sizeof(*header);
392ac7
 	rdataset->private3 = raw;
392ac7
-	rdataset->count = header->count++;
392ac7
+	rdataset->count = DNS_RBTDB_INC(header->count);
392ac7
 	if (rdataset->count == UINT32_MAX)
392ac7
 		rdataset->count = 0;
392ac7
 
392ac7
@@ -5924,10 +5949,10 @@ cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
392ac7
 		}
392ac7
 	}
392ac7
 
392ac7
-	NODE_UNLOCK(lock, locktype);
392ac7
-
392ac7
-	if (found == NULL)
392ac7
+	if (found == NULL) {
392ac7
+		NODE_UNLOCK(lock, locktype);
392ac7
 		return (ISC_R_NOTFOUND);
392ac7
+	}
392ac7
 
392ac7
 	if (NEGATIVE(found)) {
392ac7
 		/*
392ac7
@@ -5939,6 +5964,8 @@ cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
392ac7
 			result = DNS_R_NCACHENXRRSET;
392ac7
 	}
392ac7
 
392ac7
+	NODE_UNLOCK(lock, locktype);
392ac7
+
392ac7
 	update_cachestats(rbtdb, result);
392ac7
 
392ac7
 	return (result);
392ac7
@@ -6839,7 +6866,7 @@ addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
392ac7
 		newheader->attributes |= RDATASET_ATTR_ZEROTTL;
392ac7
 	newheader->noqname = NULL;
392ac7
 	newheader->closest = NULL;
392ac7
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
392ac7
+	newheader->count = DNS_RBTDB_INC(init_count);
392ac7
 	newheader->trust = rdataset->trust;
392ac7
 	newheader->additional_auth = NULL;
392ac7
 	newheader->additional_glue = NULL;
392ac7
@@ -7035,7 +7062,7 @@ subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
392ac7
 	newheader->trust = 0;
392ac7
 	newheader->noqname = NULL;
392ac7
 	newheader->closest = NULL;
392ac7
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
392ac7
+	newheader->count = DNS_RBTDB_INC(init_count);
392ac7
 	newheader->additional_auth = NULL;
392ac7
 	newheader->additional_glue = NULL;
392ac7
 	newheader->last_used = 0;
392ac7
@@ -7481,7 +7508,7 @@ loading_addrdataset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset) {
392ac7
 	newheader->serial = 1;
392ac7
 	newheader->noqname = NULL;
392ac7
 	newheader->closest = NULL;
392ac7
-	newheader->count = isc_atomic_xadd((int32_t*)&init_count, 1);
392ac7
+	newheader->count = DNS_RBTDB_INC(init_count);
392ac7
 	newheader->additional_auth = NULL;
392ac7
 	newheader->additional_glue = NULL;
392ac7
 	newheader->last_used = 0;
392ac7
-- 
392ac7
2.26.2
392ac7