From cb1d2e57e91f5df3acc53d086a61b02b2152028a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 5 Feb 2024 08:52:35 +0100 Subject: [PATCH 1/3] Remove unused mutex from netmgr The netmgr->lock was dead code, remove it. --- lib/isc/netmgr/netmgr-int.h | 1 - lib/isc/netmgr/netmgr.c | 3 --- 2 files changed, 4 deletions(-) diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index 9363119d3e..596333d1dd 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -328,7 +328,6 @@ struct isc_nm { isc_mem_t *mctx; isc_loopmgr_t *loopmgr; uint32_t nloops; - isc_mutex_t lock; isc__networker_t *workers; isc_stats_t *stats; diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index ea1c561a62..6cbc8a4bf2 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -179,7 +179,6 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) { }; isc_mem_attach(mctx, &netmgr->mctx); - isc_mutex_init(&netmgr->lock); isc_refcount_init(&netmgr->references, 1); atomic_init(&netmgr->maxudp, 0); atomic_init(&netmgr->shuttingdown, false); @@ -253,8 +252,6 @@ nm_destroy(isc_nm_t **mgr0) { isc_stats_detach(&mgr->stats); } - isc_mutex_destroy(&mgr->lock); - isc_mem_cput(mgr->mctx, mgr->workers, mgr->nloops, sizeof(mgr->workers[0])); isc_mem_putanddetach(&mgr->mctx, mgr, sizeof(*mgr)); From 01038d894fdf6df85aba8b4fdb2cc149f0476da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 5 Feb 2024 08:57:23 +0100 Subject: [PATCH 2/3] Always use adaptive mutexes on Linux When adaptive mutexes are available (with glibc), always use them. Remove the autoconf switch and also fix the static initializer. --- configure.ac | 24 ------------------------ lib/isc/include/isc/mutex.h | 4 ++++ lib/isc/mutex.c | 2 +- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/configure.ac b/configure.ac index 92f40c17ac..cd05a39e3d 100644 --- a/configure.ac +++ b/configure.ac @@ -562,30 +562,6 @@ AC_ARG_WITH([locktype], (adaptive or standard)]), [], [with_locktype="adaptive"]) -AS_CASE([$with_locktype], - [adaptive],[ - AC_MSG_CHECKING([for PTHREAD_MUTEX_ADAPTIVE_NP]) - AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[ - #ifndef _GNU_SOURCE - #define _GNU_SOURCE - #endif - #include - ]], - [[ - return (PTHREAD_MUTEX_ADAPTIVE_NP); - ]] - )], - [AC_MSG_RESULT([using adaptive lock type]) - AC_DEFINE([HAVE_PTHREAD_MUTEX_ADAPTIVE_NP], 1, - [Support for PTHREAD_MUTEX_ADAPTIVE_NP]) ], - [AC_MSG_RESULT([using standard lock type])] - )], - [standard],[AC_MSG_RESULT([using standard lock type])], - [AC_MSG_ERROR([You must specify "adaptive" or "standard" for --with-locktype.])] - ) - AC_CHECK_HEADERS([sched.h]) AC_SEARCH_LIBS([sched_yield],[rt]) diff --git a/lib/isc/include/isc/mutex.h b/lib/isc/include/isc/mutex.h index 45256a6fac..904747e335 100644 --- a/lib/isc/include/isc/mutex.h +++ b/lib/isc/include/isc/mutex.h @@ -23,7 +23,11 @@ #include /* for ISC_R_ codes */ #include +#if defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) +#define ISC_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP +#else #define ISC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#endif ISC_LANG_BEGINDECLS diff --git a/lib/isc/mutex.c b/lib/isc/mutex.c index e591ffe872..ff9053793d 100644 --- a/lib/isc/mutex.c +++ b/lib/isc/mutex.c @@ -33,7 +33,7 @@ static isc_once_t init_once = ISC_ONCE_INIT; static void mutex_initialize(void) { RUNTIME_CHECK(pthread_mutexattr_init(&isc__mutex_init_attr) == 0); -#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP +#if defined(PTHREAD_MUTEX_ADAPTIVE_NP) RUNTIME_CHECK(pthread_mutexattr_settype(&isc__mutex_init_attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0); From 2c98ccbdbac1202a3bf719e34c11e0eb04bb931d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 5 Feb 2024 09:03:41 +0100 Subject: [PATCH 3/3] Use error checking mutex in developer mode on Linux When developer mode is enabled, use error checking mutex type, so we can discover wrong use of mutexes faster. --- configure.ac | 2 +- lib/isc/include/isc/mutex.h | 4 +++- lib/isc/mutex.c | 6 +++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index cd05a39e3d..a27b22543c 100644 --- a/configure.ac +++ b/configure.ac @@ -171,7 +171,7 @@ AC_ARG_ENABLE([developer], AS_IF([test "$enable_developer" = "yes"], [DEVELOPER_MODE=yes - STD_CPPFLAGS="$STD_CPPFLAGS -DISC_MEM_DEFAULTFILL=1 -DISC_MEM_TRACKLINES=1 -DISC_LIST_CHECKINIT=1 -DISC_STATS_CHECKUNDERFLOW=1 -DDNS_RBTDB_STRONG_RWLOCK_CHECK=1" + STD_CPPFLAGS="$STD_CPPFLAGS -DISC_MEM_DEFAULTFILL=1 -DISC_MEM_TRACKLINES=1 -DISC_LIST_CHECKINIT=1 -DISC_STATS_CHECKUNDERFLOW=1 -DDNS_RBTDB_STRONG_RWLOCK_CHECK=1 -DISC_MUTEX_ERROR_CHECK=1" test "${enable_fixed_rrset+set}" = set || enable_fixed_rrset=yes test "${enable_querytrace+set}" = set || enable_querytrace=yes test "${with_cmocka+set}" = set || with_cmocka=yes diff --git a/lib/isc/include/isc/mutex.h b/lib/isc/include/isc/mutex.h index 904747e335..341148a753 100644 --- a/lib/isc/include/isc/mutex.h +++ b/lib/isc/include/isc/mutex.h @@ -23,7 +23,9 @@ #include /* for ISC_R_ codes */ #include -#if defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) +#if ISC_MUTEX_ERROR_CHECK && defined(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) +#define ISC_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP +#elif defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) #define ISC_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP #else #define ISC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER diff --git a/lib/isc/mutex.c b/lib/isc/mutex.c index ff9053793d..67815eb7e9 100644 --- a/lib/isc/mutex.c +++ b/lib/isc/mutex.c @@ -33,7 +33,11 @@ static isc_once_t init_once = ISC_ONCE_INIT; static void mutex_initialize(void) { RUNTIME_CHECK(pthread_mutexattr_init(&isc__mutex_init_attr) == 0); -#if defined(PTHREAD_MUTEX_ADAPTIVE_NP) +#if ISC_MUTEX_ERROR_CHECK && defined(PTHREAD_MUTEX_ERRORCHECK_NP) + RUNTIME_CHECK(pthread_mutexattr_settype(&isc__mutex_init_attr, + PTHREAD_MUTEX_ERRORCHECK_NP) == + 0); +#elif defined(PTHREAD_MUTEX_ADAPTIVE_NP) RUNTIME_CHECK(pthread_mutexattr_settype(&isc__mutex_init_attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);