From 15ce1737fa39f8c5e68da5fb7edfd478cbda3a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Tue, 7 Dec 2021 11:15:27 +0100 Subject: [PATCH] Fix the isc_hp initialization and memory usage Previously, the isc_hp_init() could not lower the value of isc__hp_max_threads, but because of a mistake the isc__hp_max_threads would be set to HP_MAX_THREADS (e.g. 128 threads) thus it would be always set to 128. This would result in increased memory usage even when small number of workers were in use. Change the default value of isc__hp_max_threads to be 1. Additionally, enforce the max_hps value in isc_hp_new() to be smaller or equal to HP_MAX_HPS. The only user is isc_queue which uses just 1 hazard pointer, so it's only theoretical issue. --- lib/isc/hp.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/isc/hp.c b/lib/isc/hp.c index 20646a8a74..aaa59f6c45 100644 --- a/lib/isc/hp.c +++ b/lib/isc/hp.c @@ -54,7 +54,7 @@ #include #define HP_MAX_THREADS 128 -static int isc__hp_max_threads = HP_MAX_THREADS; +static int isc__hp_max_threads = 1; #define HP_MAX_HPS 4 /* This is named 'K' in the HP paper */ #define CLPAD (128 / sizeof(uintptr_t)) #define HP_THRESHOLD_R 0 /* This is named 'R' in the HP paper */ @@ -82,9 +82,12 @@ tid(void) { void isc_hp_init(int max_threads) { + REQUIRE(max_threads > 0); + if (isc__hp_max_threads > max_threads) { return; } + isc__hp_max_threads = max_threads; isc__hp_max_retired = max_threads * HP_MAX_HPS; } @@ -93,6 +96,9 @@ isc_hp_t * isc_hp_new(isc_mem_t *mctx, size_t max_hps, isc_hp_deletefunc_t *deletefunc) { isc_hp_t *hp = isc_mem_get(mctx, sizeof(*hp)); + REQUIRE(isc__hp_max_threads > 0); + REQUIRE(max_hps <= HP_MAX_HPS); + if (max_hps == 0) { max_hps = HP_MAX_HPS; }