git: 9a2ae72421cd - main - libthr: switch thread and sleepq memory allocator to crt from libc malloc

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 14 Jan 2025 17:55:15 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=9a2ae72421cd75c741984f63b8c9ee89346a188d

commit 9a2ae72421cd75c741984f63b8c9ee89346a188d
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-01-14 09:06:58 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-01-14 17:55:08 +0000

    libthr: switch thread and sleepq memory allocator to crt from libc malloc
    
    There are more complex interactions between malloc and libthr
    initialization that can happen if libthr functions are called from ELF
    object' constructors, before libthr is initialized.  Break the
    dependencies loop by using the private allocator with controlled init.
    
    Reported by:    yuri
    Reviewed by:    markj, olce
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D48454
---
 lib/libthr/thread/thr_list.c   | 4 ++--
 lib/libthr/thread/thr_sleepq.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/libthr/thread/thr_list.c b/lib/libthr/thread/thr_list.c
index bca2bfc75fef..bbc1d2899cb9 100644
--- a/lib/libthr/thread/thr_list.c
+++ b/lib/libthr/thread/thr_list.c
@@ -150,7 +150,7 @@ _thr_alloc(struct pthread *curthread)
 		if (total_threads > MAX_THREADS)
 			return (NULL);
 		atomic_add_int(&total_threads, 1);
-		thread = calloc(1, sizeof(struct pthread));
+		thread = __thr_calloc(1, sizeof(struct pthread));
 		if (thread == NULL) {
 			atomic_add_int(&total_threads, -1);
 			return (NULL);
@@ -222,7 +222,7 @@ thr_destroy(struct pthread *curthread __unused, struct pthread *thread)
 		_sleepq_free(thread->sleepqueue);
 	if (thread->wake_addr != NULL)
 		_thr_release_wake_addr(thread->wake_addr);
-	free(thread);
+	__thr_free(thread);
 }
 
 /*
diff --git a/lib/libthr/thread/thr_sleepq.c b/lib/libthr/thread/thr_sleepq.c
index d7de9ab4e25a..9c680acd0ac0 100644
--- a/lib/libthr/thread/thr_sleepq.c
+++ b/lib/libthr/thread/thr_sleepq.c
@@ -62,7 +62,7 @@ _sleepq_alloc(void)
 {
 	struct sleepqueue *sq;
 
-	sq = calloc(1, sizeof(struct sleepqueue));
+	sq = __thr_calloc(1, sizeof(struct sleepqueue));
 	TAILQ_INIT(&sq->sq_blocked);
 	SLIST_INIT(&sq->sq_freeq);
 	return (sq);
@@ -71,7 +71,7 @@ _sleepq_alloc(void)
 void
 _sleepq_free(struct sleepqueue *sq)
 {
-	free(sq);
+	__thr_free(sq);
 }
 
 void