From: Tony Finch Date: Fri, 28 Apr 2023 13:10:03 +0000 (+0100) Subject: Wait for RCU to finish before destroying a memory context X-Git-Tag: v9.19.14~52^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2e0c954806ea478554f34c0c222230344016d955;p=thirdparty%2Fbind9.git Wait for RCU to finish before destroying a memory context Memory reclamation by `call_rcu()` is asynchronous, so during shutdown it can lose a race with the destruction of its memory context. When we defer memory reclamation, we need to attach to the memory context to indicate that it is still in use, but that is not enough to delay its destruction. So, call `rcu_barrier()` in `isc_mem_destroy()` to wait for pending RCU work to finish before proceeding to destroy the memory context. --- diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 0b2a3172446..31052364781 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #ifdef HAVE_LIBXML2 @@ -578,6 +579,12 @@ isc__mem_destroy(isc_mem_t **ctxp FLARG) { ctx = *ctxp; *ctxp = NULL; + /* + * wait for asynchronous memory reclamation to complete + * before checking for memory leaks + */ + rcu_barrier(); + #if ISC_MEM_TRACKLINES if ((ctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file,