mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-30 14:07:59 +00:00
lib/dns/resolver.c: Convert (dns_view_t *)->weakrefs to isc_refcount_t
There's a deadlock in BIND 9 code where (dns_view_t){ .lock } and (dns_resolver_t){ .buckets[i].lock } gets locked in different order. When view->weakrefs gets converted to a reference counting we can reduce the locking in dns_view_weakdetach only to cases where it's the last instance of the dns_view_t object. (cherry picked from commita7c9a52c89
) (cherry picked from commit232140edae
)
This commit is contained in:
@@ -197,9 +197,9 @@ struct dns_view {
|
|||||||
|
|
||||||
/* Locked by themselves. */
|
/* Locked by themselves. */
|
||||||
isc_refcount_t references;
|
isc_refcount_t references;
|
||||||
|
isc_refcount_t weakrefs;
|
||||||
|
|
||||||
/* Locked by lock. */
|
/* Locked by lock. */
|
||||||
unsigned int weakrefs;
|
|
||||||
unsigned int attributes;
|
unsigned int attributes;
|
||||||
/* Under owner's locking control. */
|
/* Under owner's locking control. */
|
||||||
ISC_LINK(struct dns_view) link;
|
ISC_LINK(struct dns_view) link;
|
||||||
|
@@ -139,7 +139,7 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
|||||||
view->frozen = false;
|
view->frozen = false;
|
||||||
view->task = NULL;
|
view->task = NULL;
|
||||||
isc_refcount_init(&view->references, 1);
|
isc_refcount_init(&view->references, 1);
|
||||||
view->weakrefs = 0;
|
isc_refcount_init(&view->weakrefs, 0);
|
||||||
view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
|
view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
|
||||||
DNS_VIEWATTR_REQSHUTDOWN);
|
DNS_VIEWATTR_REQSHUTDOWN);
|
||||||
view->statickeys = NULL;
|
view->statickeys = NULL;
|
||||||
@@ -149,7 +149,7 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
|||||||
view->matchrecursiveonly = false;
|
view->matchrecursiveonly = false;
|
||||||
result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
|
result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
|
||||||
if (result != ISC_R_SUCCESS)
|
if (result != ISC_R_SUCCESS)
|
||||||
goto cleanup_references;
|
goto cleanup_weakrefs;
|
||||||
view->peers = NULL;
|
view->peers = NULL;
|
||||||
view->order = NULL;
|
view->order = NULL;
|
||||||
view->delonly = NULL;
|
view->delonly = NULL;
|
||||||
@@ -304,8 +304,10 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
|||||||
dns_tsigkeyring_detach(&view->dynamickeys);
|
dns_tsigkeyring_detach(&view->dynamickeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup_references:
|
cleanup_weakrefs:
|
||||||
INSIST(isc_refcount_decrement(&view->references) > 0);
|
isc_refcount_destroy(&view->weakrefs);
|
||||||
|
|
||||||
|
isc_refcount_decrement(&view->references);
|
||||||
isc_refcount_destroy(&view->references);
|
isc_refcount_destroy(&view->references);
|
||||||
|
|
||||||
if (view->fwdtable != NULL) {
|
if (view->fwdtable != NULL) {
|
||||||
@@ -337,12 +339,13 @@ destroy(dns_view_t *view) {
|
|||||||
dns_dlzdb_t *dlzdb;
|
dns_dlzdb_t *dlzdb;
|
||||||
|
|
||||||
REQUIRE(!ISC_LINK_LINKED(view, link));
|
REQUIRE(!ISC_LINK_LINKED(view, link));
|
||||||
REQUIRE(isc_refcount_current(&view->references) == 0);
|
|
||||||
REQUIRE(view->weakrefs == 0);
|
|
||||||
REQUIRE(RESSHUTDOWN(view));
|
REQUIRE(RESSHUTDOWN(view));
|
||||||
REQUIRE(ADBSHUTDOWN(view));
|
REQUIRE(ADBSHUTDOWN(view));
|
||||||
REQUIRE(REQSHUTDOWN(view));
|
REQUIRE(REQSHUTDOWN(view));
|
||||||
|
|
||||||
|
isc_refcount_destroy(&view->references);
|
||||||
|
isc_refcount_destroy(&view->weakrefs);
|
||||||
|
|
||||||
if (view->order != NULL)
|
if (view->order != NULL)
|
||||||
dns_order_detach(&view->order);
|
dns_order_detach(&view->order);
|
||||||
if (view->peers != NULL)
|
if (view->peers != NULL)
|
||||||
@@ -535,6 +538,8 @@ destroy(dns_view_t *view) {
|
|||||||
dns_badcache_destroy(&view->failcache);
|
dns_badcache_destroy(&view->failcache);
|
||||||
isc_mutex_destroy(&view->new_zone_lock);
|
isc_mutex_destroy(&view->new_zone_lock);
|
||||||
isc_mutex_destroy(&view->lock);
|
isc_mutex_destroy(&view->lock);
|
||||||
|
isc_refcount_destroy(&view->references);
|
||||||
|
isc_refcount_destroy(&view->weakrefs);
|
||||||
isc_mem_free(view->mctx, view->nta_file);
|
isc_mem_free(view->mctx, view->nta_file);
|
||||||
isc_mem_free(view->mctx, view->name);
|
isc_mem_free(view->mctx, view->name);
|
||||||
if (view->hooktable != NULL && view->hooktable_free != NULL) {
|
if (view->hooktable != NULL && view->hooktable_free != NULL) {
|
||||||
@@ -554,9 +559,11 @@ static bool
|
|||||||
all_done(dns_view_t *view) {
|
all_done(dns_view_t *view) {
|
||||||
|
|
||||||
if (isc_refcount_current(&view->references) == 0 &&
|
if (isc_refcount_current(&view->references) == 0 &&
|
||||||
view->weakrefs == 0 &&
|
isc_refcount_current(&view->weakrefs) == 0 &&
|
||||||
RESSHUTDOWN(view) && ADBSHUTDOWN(view) && REQSHUTDOWN(view))
|
RESSHUTDOWN(view) && ADBSHUTDOWN(view) && REQSHUTDOWN(view))
|
||||||
|
{
|
||||||
return (true);
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
@@ -671,9 +678,7 @@ dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
|
|||||||
REQUIRE(DNS_VIEW_VALID(source));
|
REQUIRE(DNS_VIEW_VALID(source));
|
||||||
REQUIRE(targetp != NULL && *targetp == NULL);
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
||||||
|
|
||||||
LOCK(&source->lock);
|
isc_refcount_increment0(&source->weakrefs);
|
||||||
source->weakrefs++;
|
|
||||||
UNLOCK(&source->lock);
|
|
||||||
|
|
||||||
*targetp = source;
|
*targetp = source;
|
||||||
}
|
}
|
||||||
@@ -681,24 +686,21 @@ dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
|
|||||||
void
|
void
|
||||||
dns_view_weakdetach(dns_view_t **viewp) {
|
dns_view_weakdetach(dns_view_t **viewp) {
|
||||||
dns_view_t *view;
|
dns_view_t *view;
|
||||||
bool done = false;
|
|
||||||
|
|
||||||
REQUIRE(viewp != NULL);
|
REQUIRE(viewp != NULL);
|
||||||
view = *viewp;
|
view = *viewp;
|
||||||
REQUIRE(DNS_VIEW_VALID(view));
|
REQUIRE(DNS_VIEW_VALID(view));
|
||||||
|
|
||||||
LOCK(&view->lock);
|
|
||||||
|
|
||||||
INSIST(view->weakrefs > 0);
|
|
||||||
view->weakrefs--;
|
|
||||||
done = all_done(view);
|
|
||||||
|
|
||||||
UNLOCK(&view->lock);
|
|
||||||
|
|
||||||
*viewp = NULL;
|
*viewp = NULL;
|
||||||
|
|
||||||
if (done)
|
if (isc_refcount_decrement(&view->weakrefs) == 1) {
|
||||||
destroy(view);
|
bool done = false;
|
||||||
|
LOCK(&view->lock);
|
||||||
|
done = all_done(view);
|
||||||
|
UNLOCK(&view->lock);
|
||||||
|
if (done) {
|
||||||
|
destroy(view);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Reference in New Issue
Block a user