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 <machine/tls.h> 34 35 #include <stdlib.h> 36 #include <string.h> 37 #include <pthread.h> 38 39 #include "libc_private.h" 40 #include "thr_private.h" 41 42 /*#define DEBUG_THREAD_LIST */ 43 #ifdef DEBUG_THREAD_LIST 44 #define DBG_MSG stdout_debug 45 #else 46 #define DBG_MSG(x...) 47 #endif 48 49 #define MAX_THREADS 100000 50 51 /* 52 * Define a high water mark for the maximum number of threads that 53 * will be cached. Once this level is reached, any extra threads 54 * will be free()'d. 55 */ 56 #define MAX_CACHED_THREADS 100 57 58 /* 59 * We've got to keep track of everything that is allocated, not only 60 * to have a speedy free list, but also so they can be deallocated 61 * after a fork(). 62 */ 63 static TAILQ_HEAD(, pthread) free_threadq; 64 static struct umutex free_thread_lock = DEFAULT_UMUTEX; 65 static struct umutex tcb_lock = DEFAULT_UMUTEX; 66 static int free_thread_count = 0; 67 static int inited = 0; 68 static int total_threads; 69 70 LIST_HEAD(thread_hash_head, pthread); 71 #define HASH_QUEUES 128 72 static struct thread_hash_head thr_hashtable[HASH_QUEUES]; 73 #define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES) 74 75 static void thr_destroy(struct pthread *curthread, struct pthread *thread); 76 77 void 78 _thr_list_init(void) 79 { 80 int i; 81 82 _gc_count = 0; 83 total_threads = 1; 84 _thr_urwlock_init(&_thr_list_lock); 85 TAILQ_INIT(&_thread_list); 86 TAILQ_INIT(&free_threadq); 87 _thr_umutex_init(&free_thread_lock); 88 _thr_umutex_init(&tcb_lock); 89 if (inited) { 90 for (i = 0; i < HASH_QUEUES; ++i) 91 LIST_INIT(&thr_hashtable[i]); 92 } 93 inited = 1; 94 } 95 96 void 97 _thr_gc(struct pthread *curthread) 98 { 99 struct pthread *td, *td_next; 100 TAILQ_HEAD(, pthread) worklist; 101 102 TAILQ_INIT(&worklist); 103 THREAD_LIST_WRLOCK(curthread); 104 105 /* Check the threads waiting for GC. */ 106 TAILQ_FOREACH_SAFE(td, &_thread_gc_list, gcle, td_next) { 107 if (td->tid != TID_TERMINATED) { 108 /* make sure we are not still in userland */ 109 continue; 110 } 111 _thr_stack_free(&td->attr); 112 THR_GCLIST_REMOVE(td); 113 TAILQ_INSERT_HEAD(&worklist, td, gcle); 114 } 115 THREAD_LIST_UNLOCK(curthread); 116 117 while ((td = TAILQ_FIRST(&worklist)) != NULL) { 118 TAILQ_REMOVE(&worklist, td, gcle); 119 /* 120 * XXX we don't free initial thread, because there might 121 * have some code referencing initial thread. 122 */ 123 if (td == _thr_initial) { 124 DBG_MSG("Initial thread won't be freed\n"); 125 continue; 126 } 127 128 _thr_free(curthread, td); 129 } 130 } 131 132 struct pthread * 133 _thr_alloc(struct pthread *curthread) 134 { 135 struct pthread *thread = NULL; 136 struct tcb *tcb; 137 138 if (curthread != NULL) { 139 if (GC_NEEDED()) 140 _thr_gc(curthread); 141 if (free_thread_count > 0) { 142 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 143 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) { 144 TAILQ_REMOVE(&free_threadq, thread, tle); 145 free_thread_count--; 146 } 147 THR_LOCK_RELEASE(curthread, &free_thread_lock); 148 } 149 } 150 if (thread == NULL) { 151 if (total_threads > MAX_THREADS) 152 return (NULL); 153 atomic_add_int(&total_threads, 1); 154 thread = __thr_aligned_alloc_offset(_Alignof(struct pthread), 155 sizeof(struct pthread), 0); 156 if (thread == NULL) { 157 atomic_add_int(&total_threads, -1); 158 return (NULL); 159 } 160 memset(thread, 0, sizeof(*thread)); 161 if ((thread->sleepqueue = _sleepq_alloc()) == NULL || 162 (thread->wake_addr = _thr_alloc_wake_addr()) == NULL) { 163 thr_destroy(curthread, thread); 164 atomic_add_int(&total_threads, -1); 165 return (NULL); 166 } 167 } else { 168 bzero(&thread->_pthread_startzero, 169 __rangeof(struct pthread, _pthread_startzero, _pthread_endzero)); 170 } 171 if (curthread != NULL) { 172 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 173 tcb = _tcb_ctor(thread, 0 /* not initial tls */); 174 THR_LOCK_RELEASE(curthread, &tcb_lock); 175 } else { 176 tcb = _tcb_ctor(thread, 1 /* initial tls */); 177 } 178 if (tcb != NULL) { 179 thread->tcb = tcb; 180 } else { 181 thr_destroy(curthread, thread); 182 atomic_add_int(&total_threads, -1); 183 thread = NULL; 184 } 185 return (thread); 186 } 187 188 void 189 _thr_free(struct pthread *curthread, struct pthread *thread) 190 { 191 DBG_MSG("Freeing thread %p\n", thread); 192 193 /* 194 * Always free tcb, as we only know it is part of RTLD TLS 195 * block, but don't know its detail and can not assume how 196 * it works, so better to avoid caching it here. 197 */ 198 if (curthread != NULL) { 199 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 200 _tcb_dtor(thread->tcb); 201 THR_LOCK_RELEASE(curthread, &tcb_lock); 202 } else { 203 _tcb_dtor(thread->tcb); 204 } 205 thread->tcb = NULL; 206 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) { 207 thr_destroy(curthread, thread); 208 atomic_add_int(&total_threads, -1); 209 } else { 210 /* 211 * Add the thread to the free thread list, this also avoids 212 * pthread id is reused too quickly, may help some buggy apps. 213 */ 214 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 215 TAILQ_INSERT_TAIL(&free_threadq, thread, tle); 216 free_thread_count++; 217 THR_LOCK_RELEASE(curthread, &free_thread_lock); 218 } 219 } 220 221 static void 222 thr_destroy(struct pthread *curthread __unused, struct pthread *thread) 223 { 224 if (thread->sleepqueue != NULL) 225 _sleepq_free(thread->sleepqueue); 226 if (thread->wake_addr != NULL) 227 _thr_release_wake_addr(thread->wake_addr); 228 __thr_free(thread); 229 } 230 231 /* 232 * Add the thread to the list of all threads and increment 233 * number of active threads. 234 */ 235 void 236 _thr_link(struct pthread *curthread, struct pthread *thread) 237 { 238 THREAD_LIST_WRLOCK(curthread); 239 THR_LIST_ADD(thread); 240 THREAD_LIST_UNLOCK(curthread); 241 atomic_add_int(&_thread_active_threads, 1); 242 } 243 244 /* 245 * Remove an active thread. 246 */ 247 void 248 _thr_unlink(struct pthread *curthread, struct pthread *thread) 249 { 250 THREAD_LIST_WRLOCK(curthread); 251 THR_LIST_REMOVE(thread); 252 THREAD_LIST_UNLOCK(curthread); 253 atomic_add_int(&_thread_active_threads, -1); 254 } 255 256 void 257 _thr_hash_add(struct pthread *thread) 258 { 259 struct thread_hash_head *head; 260 261 head = &thr_hashtable[THREAD_HASH(thread)]; 262 LIST_INSERT_HEAD(head, thread, hle); 263 } 264 265 void 266 _thr_hash_remove(struct pthread *thread) 267 { 268 LIST_REMOVE(thread, hle); 269 } 270 271 struct pthread * 272 _thr_hash_find(struct pthread *thread) 273 { 274 struct pthread *td; 275 struct thread_hash_head *head; 276 277 head = &thr_hashtable[THREAD_HASH(thread)]; 278 LIST_FOREACH(td, head, hle) { 279 if (td == thread) 280 return (thread); 281 } 282 return (NULL); 283 } 284 285 /* 286 * Find a thread in the linked list of active threads and add a reference 287 * to it. Threads with positive reference counts will not be deallocated 288 * until all references are released. 289 */ 290 int 291 _thr_ref_add(struct pthread *curthread, struct pthread *thread, 292 int include_dead) 293 { 294 int ret; 295 296 if (thread == NULL) 297 /* Invalid thread: */ 298 return (EINVAL); 299 300 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) { 301 thread->refcount++; 302 THR_CRITICAL_ENTER(curthread); 303 THR_THREAD_UNLOCK(curthread, thread); 304 } 305 306 /* Return zero if the thread exists: */ 307 return (ret); 308 } 309 310 void 311 _thr_ref_delete(struct pthread *curthread, struct pthread *thread) 312 { 313 THR_THREAD_LOCK(curthread, thread); 314 thread->refcount--; 315 _thr_try_gc(curthread, thread); 316 THR_CRITICAL_LEAVE(curthread); 317 } 318 319 /* entered with thread lock held, exit with thread lock released */ 320 void 321 _thr_try_gc(struct pthread *curthread, struct pthread *thread) 322 { 323 if (THR_SHOULD_GC(thread)) { 324 THR_REF_ADD(curthread, thread); 325 THR_THREAD_UNLOCK(curthread, thread); 326 THREAD_LIST_WRLOCK(curthread); 327 THR_THREAD_LOCK(curthread, thread); 328 THR_REF_DEL(curthread, thread); 329 if (THR_SHOULD_GC(thread)) { 330 THR_LIST_REMOVE(thread); 331 THR_GCLIST_ADD(thread); 332 } 333 THR_THREAD_UNLOCK(curthread, thread); 334 THREAD_LIST_UNLOCK(curthread); 335 } else { 336 THR_THREAD_UNLOCK(curthread, thread); 337 } 338 } 339 340 /* return with thread lock held if thread is found */ 341 int 342 _thr_find_thread(struct pthread *curthread, struct pthread *thread, 343 int include_dead) 344 { 345 struct pthread *pthread; 346 int ret; 347 348 if (thread == NULL) 349 return (EINVAL); 350 351 ret = 0; 352 THREAD_LIST_RDLOCK(curthread); 353 pthread = _thr_hash_find(thread); 354 if (pthread) { 355 THR_THREAD_LOCK(curthread, pthread); 356 if (include_dead == 0 && pthread->state == PS_DEAD) { 357 THR_THREAD_UNLOCK(curthread, pthread); 358 ret = ESRCH; 359 } 360 } else { 361 ret = ESRCH; 362 } 363 THREAD_LIST_UNLOCK(curthread); 364 return (ret); 365 } 366 367 static void 368 thr_distribute_static_tls(char *tlsbase, void *src, size_t len, 369 size_t total_len) 370 { 371 372 memcpy(tlsbase, src, len); 373 memset(tlsbase + len, 0, total_len - len); 374 } 375 376 void 377 __pthread_distribute_static_tls(size_t offset, void *src, size_t len, 378 size_t total_len) 379 { 380 struct pthread *curthread, *thrd; 381 char *tlsbase; 382 383 if (!_thr_is_inited()) { 384 #ifdef TLS_VARIANT_I 385 tlsbase = (char *)_tcb_get() + offset; 386 #else 387 tlsbase = (char *)_tcb_get() - offset; 388 #endif 389 thr_distribute_static_tls(tlsbase, src, len, total_len); 390 return; 391 } 392 curthread = _get_curthread(); 393 THREAD_LIST_RDLOCK(curthread); 394 TAILQ_FOREACH(thrd, &_thread_list, tle) { 395 #ifdef TLS_VARIANT_I 396 tlsbase = (char *)thrd->tcb + offset; 397 #else 398 tlsbase = (char *)thrd->tcb - offset; 399 #endif 400 thr_distribute_static_tls(tlsbase, src, len, total_len); 401 } 402 THREAD_LIST_UNLOCK(curthread); 403 } 404