1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 5 * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/queue.h> 32 33 #include <stdlib.h> 34 #include <string.h> 35 #include <pthread.h> 36 37 #include "libc_private.h" 38 #include "thr_private.h" 39 #include "static_tls.h" 40 41 /*#define DEBUG_THREAD_LIST */ 42 #ifdef DEBUG_THREAD_LIST 43 #define DBG_MSG stdout_debug 44 #else 45 #define DBG_MSG(x...) 46 #endif 47 48 #define MAX_THREADS 100000 49 50 /* 51 * Define a high water mark for the maximum number of threads that 52 * will be cached. Once this level is reached, any extra threads 53 * will be free()'d. 54 */ 55 #define MAX_CACHED_THREADS 100 56 57 /* 58 * We've got to keep track of everything that is allocated, not only 59 * to have a speedy free list, but also so they can be deallocated 60 * after a fork(). 61 */ 62 static TAILQ_HEAD(, pthread) free_threadq; 63 static struct umutex free_thread_lock = DEFAULT_UMUTEX; 64 static struct umutex tcb_lock = DEFAULT_UMUTEX; 65 static int free_thread_count = 0; 66 static int inited = 0; 67 static int total_threads; 68 69 LIST_HEAD(thread_hash_head, pthread); 70 #define HASH_QUEUES 128 71 static struct thread_hash_head thr_hashtable[HASH_QUEUES]; 72 #define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES) 73 74 static void thr_destroy(struct pthread *curthread, struct pthread *thread); 75 76 void 77 _thr_list_init(void) 78 { 79 int i; 80 81 _gc_count = 0; 82 total_threads = 1; 83 _thr_urwlock_init(&_thr_list_lock); 84 TAILQ_INIT(&_thread_list); 85 TAILQ_INIT(&free_threadq); 86 _thr_umutex_init(&free_thread_lock); 87 _thr_umutex_init(&tcb_lock); 88 if (inited) { 89 for (i = 0; i < HASH_QUEUES; ++i) 90 LIST_INIT(&thr_hashtable[i]); 91 } 92 inited = 1; 93 } 94 95 void 96 _thr_gc(struct pthread *curthread) 97 { 98 struct pthread *td, *td_next; 99 TAILQ_HEAD(, pthread) worklist; 100 101 TAILQ_INIT(&worklist); 102 THREAD_LIST_WRLOCK(curthread); 103 104 /* Check the threads waiting for GC. */ 105 TAILQ_FOREACH_SAFE(td, &_thread_gc_list, gcle, td_next) { 106 if (td->tid != TID_TERMINATED) { 107 /* make sure we are not still in userland */ 108 continue; 109 } 110 _thr_stack_free(&td->attr); 111 THR_GCLIST_REMOVE(td); 112 TAILQ_INSERT_HEAD(&worklist, td, gcle); 113 } 114 THREAD_LIST_UNLOCK(curthread); 115 116 while ((td = TAILQ_FIRST(&worklist)) != NULL) { 117 TAILQ_REMOVE(&worklist, td, gcle); 118 /* 119 * XXX we don't free initial thread, because there might 120 * have some code referencing initial thread. 121 */ 122 if (td == _thr_initial) { 123 DBG_MSG("Initial thread won't be freed\n"); 124 continue; 125 } 126 127 _thr_free(curthread, td); 128 } 129 } 130 131 struct pthread * 132 _thr_alloc(struct pthread *curthread) 133 { 134 struct pthread *thread = NULL; 135 struct tcb *tcb; 136 137 if (curthread != NULL) { 138 if (GC_NEEDED()) 139 _thr_gc(curthread); 140 if (free_thread_count > 0) { 141 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 142 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) { 143 TAILQ_REMOVE(&free_threadq, thread, tle); 144 free_thread_count--; 145 } 146 THR_LOCK_RELEASE(curthread, &free_thread_lock); 147 } 148 } 149 if (thread == NULL) { 150 if (total_threads > MAX_THREADS) 151 return (NULL); 152 atomic_fetchadd_int(&total_threads, 1); 153 thread = calloc(1, sizeof(struct pthread)); 154 if (thread == NULL) { 155 atomic_fetchadd_int(&total_threads, -1); 156 return (NULL); 157 } 158 if ((thread->sleepqueue = _sleepq_alloc()) == NULL || 159 (thread->wake_addr = _thr_alloc_wake_addr()) == NULL) { 160 thr_destroy(curthread, thread); 161 atomic_fetchadd_int(&total_threads, -1); 162 return (NULL); 163 } 164 } else { 165 bzero(&thread->_pthread_startzero, 166 __rangeof(struct pthread, _pthread_startzero, _pthread_endzero)); 167 } 168 if (curthread != NULL) { 169 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 170 tcb = _tcb_ctor(thread, 0 /* not initial tls */); 171 THR_LOCK_RELEASE(curthread, &tcb_lock); 172 } else { 173 tcb = _tcb_ctor(thread, 1 /* initial tls */); 174 } 175 if (tcb != NULL) { 176 thread->tcb = tcb; 177 } else { 178 thr_destroy(curthread, thread); 179 atomic_fetchadd_int(&total_threads, -1); 180 thread = NULL; 181 } 182 return (thread); 183 } 184 185 void 186 _thr_free(struct pthread *curthread, struct pthread *thread) 187 { 188 DBG_MSG("Freeing thread %p\n", thread); 189 190 /* 191 * Always free tcb, as we only know it is part of RTLD TLS 192 * block, but don't know its detail and can not assume how 193 * it works, so better to avoid caching it here. 194 */ 195 if (curthread != NULL) { 196 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 197 _tcb_dtor(thread->tcb); 198 THR_LOCK_RELEASE(curthread, &tcb_lock); 199 } else { 200 _tcb_dtor(thread->tcb); 201 } 202 thread->tcb = NULL; 203 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) { 204 thr_destroy(curthread, thread); 205 atomic_fetchadd_int(&total_threads, -1); 206 } else { 207 /* 208 * Add the thread to the free thread list, this also avoids 209 * pthread id is reused too quickly, may help some buggy apps. 210 */ 211 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 212 TAILQ_INSERT_TAIL(&free_threadq, thread, tle); 213 free_thread_count++; 214 THR_LOCK_RELEASE(curthread, &free_thread_lock); 215 } 216 } 217 218 static void 219 thr_destroy(struct pthread *curthread __unused, struct pthread *thread) 220 { 221 if (thread->sleepqueue != NULL) 222 _sleepq_free(thread->sleepqueue); 223 if (thread->wake_addr != NULL) 224 _thr_release_wake_addr(thread->wake_addr); 225 free(thread); 226 } 227 228 /* 229 * Add the thread to the list of all threads and increment 230 * number of active threads. 231 */ 232 void 233 _thr_link(struct pthread *curthread, struct pthread *thread) 234 { 235 THREAD_LIST_WRLOCK(curthread); 236 THR_LIST_ADD(thread); 237 THREAD_LIST_UNLOCK(curthread); 238 atomic_add_int(&_thread_active_threads, 1); 239 } 240 241 /* 242 * Remove an active thread. 243 */ 244 void 245 _thr_unlink(struct pthread *curthread, struct pthread *thread) 246 { 247 THREAD_LIST_WRLOCK(curthread); 248 THR_LIST_REMOVE(thread); 249 THREAD_LIST_UNLOCK(curthread); 250 atomic_add_int(&_thread_active_threads, -1); 251 } 252 253 void 254 _thr_hash_add(struct pthread *thread) 255 { 256 struct thread_hash_head *head; 257 258 head = &thr_hashtable[THREAD_HASH(thread)]; 259 LIST_INSERT_HEAD(head, thread, hle); 260 } 261 262 void 263 _thr_hash_remove(struct pthread *thread) 264 { 265 LIST_REMOVE(thread, hle); 266 } 267 268 struct pthread * 269 _thr_hash_find(struct pthread *thread) 270 { 271 struct pthread *td; 272 struct thread_hash_head *head; 273 274 head = &thr_hashtable[THREAD_HASH(thread)]; 275 LIST_FOREACH(td, head, hle) { 276 if (td == thread) 277 return (thread); 278 } 279 return (NULL); 280 } 281 282 /* 283 * Find a thread in the linked list of active threads and add a reference 284 * to it. Threads with positive reference counts will not be deallocated 285 * until all references are released. 286 */ 287 int 288 _thr_ref_add(struct pthread *curthread, struct pthread *thread, 289 int include_dead) 290 { 291 int ret; 292 293 if (thread == NULL) 294 /* Invalid thread: */ 295 return (EINVAL); 296 297 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) { 298 thread->refcount++; 299 THR_CRITICAL_ENTER(curthread); 300 THR_THREAD_UNLOCK(curthread, thread); 301 } 302 303 /* Return zero if the thread exists: */ 304 return (ret); 305 } 306 307 void 308 _thr_ref_delete(struct pthread *curthread, struct pthread *thread) 309 { 310 THR_THREAD_LOCK(curthread, thread); 311 thread->refcount--; 312 _thr_try_gc(curthread, thread); 313 THR_CRITICAL_LEAVE(curthread); 314 } 315 316 /* entered with thread lock held, exit with thread lock released */ 317 void 318 _thr_try_gc(struct pthread *curthread, struct pthread *thread) 319 { 320 if (THR_SHOULD_GC(thread)) { 321 THR_REF_ADD(curthread, thread); 322 THR_THREAD_UNLOCK(curthread, thread); 323 THREAD_LIST_WRLOCK(curthread); 324 THR_THREAD_LOCK(curthread, thread); 325 THR_REF_DEL(curthread, thread); 326 if (THR_SHOULD_GC(thread)) { 327 THR_LIST_REMOVE(thread); 328 THR_GCLIST_ADD(thread); 329 } 330 THR_THREAD_UNLOCK(curthread, thread); 331 THREAD_LIST_UNLOCK(curthread); 332 } else { 333 THR_THREAD_UNLOCK(curthread, thread); 334 } 335 } 336 337 /* return with thread lock held if thread is found */ 338 int 339 _thr_find_thread(struct pthread *curthread, struct pthread *thread, 340 int include_dead) 341 { 342 struct pthread *pthread; 343 int ret; 344 345 if (thread == NULL) 346 return (EINVAL); 347 348 ret = 0; 349 THREAD_LIST_RDLOCK(curthread); 350 pthread = _thr_hash_find(thread); 351 if (pthread) { 352 THR_THREAD_LOCK(curthread, pthread); 353 if (include_dead == 0 && pthread->state == PS_DEAD) { 354 THR_THREAD_UNLOCK(curthread, pthread); 355 ret = ESRCH; 356 } 357 } else { 358 ret = ESRCH; 359 } 360 THREAD_LIST_UNLOCK(curthread); 361 return (ret); 362 } 363 364 #include "pthread_tls.h" 365 366 static void 367 thr_distribute_static_tls(uintptr_t tlsbase, void *src, size_t len, 368 size_t total_len) 369 { 370 371 memcpy((void *)tlsbase, src, len); 372 memset((char *)tlsbase + len, 0, total_len - len); 373 } 374 375 void 376 __pthread_distribute_static_tls(size_t offset, void *src, size_t len, 377 size_t total_len) 378 { 379 struct pthread *curthread, *thrd; 380 uintptr_t tlsbase; 381 382 if (!_thr_is_inited()) { 383 tlsbase = _libc_get_static_tls_base(offset); 384 thr_distribute_static_tls(tlsbase, src, len, total_len); 385 return; 386 } 387 curthread = _get_curthread(); 388 THREAD_LIST_RDLOCK(curthread); 389 TAILQ_FOREACH(thrd, &_thread_list, tle) { 390 tlsbase = _get_static_tls_base(thrd, offset); 391 thr_distribute_static_tls(tlsbase, src, len, total_len); 392 } 393 THREAD_LIST_UNLOCK(curthread); 394 } 395