1 | /* Copyright (C) 2002-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ctype.h> |
20 | #include <errno.h> |
21 | #include <stdbool.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | #include <stdint.h> |
25 | #include "pthreadP.h" |
26 | #include <hp-timing.h> |
27 | #include <ldsodefs.h> |
28 | #include <atomic.h> |
29 | #include <libc-diag.h> |
30 | #include <libc-internal.h> |
31 | #include <resolv.h> |
32 | #include <kernel-features.h> |
33 | #include <exit-thread.h> |
34 | #include <default-sched.h> |
35 | #include <futex-internal.h> |
36 | #include <tls-setup.h> |
37 | #include "libioP.h" |
38 | #include <sys/single_threaded.h> |
39 | |
40 | #include <shlib-compat.h> |
41 | |
42 | #include <stap-probe.h> |
43 | |
44 | |
45 | /* Nozero if debugging mode is enabled. */ |
46 | int __pthread_debug; |
47 | |
48 | /* Globally enabled events. */ |
49 | static td_thr_events_t __nptl_threads_events __attribute_used__; |
50 | |
51 | /* Pointer to descriptor with the last event. */ |
52 | static struct pthread *__nptl_last_event __attribute_used__; |
53 | |
54 | /* Number of threads running. */ |
55 | unsigned int __nptl_nthreads = 1; |
56 | |
57 | |
58 | /* Code to allocate and deallocate a stack. */ |
59 | #include "allocatestack.c" |
60 | |
61 | /* CONCURRENCY NOTES: |
62 | |
63 | Understanding who is the owner of the 'struct pthread' or 'PD' |
64 | (refers to the value of the 'struct pthread *pd' function argument) |
65 | is critically important in determining exactly which operations are |
66 | allowed and which are not and when, particularly when it comes to the |
67 | implementation of pthread_create, pthread_join, pthread_detach, and |
68 | other functions which all operate on PD. |
69 | |
70 | The owner of PD is responsible for freeing the final resources |
71 | associated with PD, and may examine the memory underlying PD at any |
72 | point in time until it frees it back to the OS or to reuse by the |
73 | runtime. |
74 | |
75 | The thread which calls pthread_create is called the creating thread. |
76 | The creating thread begins as the owner of PD. |
77 | |
78 | During startup the new thread may examine PD in coordination with the |
79 | owner thread (which may be itself). |
80 | |
81 | The four cases of ownership transfer are: |
82 | |
83 | (1) Ownership of PD is released to the process (all threads may use it) |
84 | after the new thread starts in a joinable state |
85 | i.e. pthread_create returns a usable pthread_t. |
86 | |
87 | (2) Ownership of PD is released to the new thread starting in a detached |
88 | state. |
89 | |
90 | (3) Ownership of PD is dynamically released to a running thread via |
91 | pthread_detach. |
92 | |
93 | (4) Ownership of PD is acquired by the thread which calls pthread_join. |
94 | |
95 | Implementation notes: |
96 | |
97 | The PD->stopped_start and thread_ran variables are used to determine |
98 | exactly which of the four ownership states we are in and therefore |
99 | what actions can be taken. For example after (2) we cannot read or |
100 | write from PD anymore since the thread may no longer exist and the |
101 | memory may be unmapped. |
102 | |
103 | It is important to point out that PD->lock is being used both |
104 | similar to a one-shot semaphore and subsequently as a mutex. The |
105 | lock is taken in the parent to force the child to wait, and then the |
106 | child releases the lock. However, this semaphore-like effect is used |
107 | only for synchronizing the parent and child. After startup the lock |
108 | is used like a mutex to create a critical section during which a |
109 | single owner modifies the thread parameters. |
110 | |
111 | The most complicated cases happen during thread startup: |
112 | |
113 | (a) If the created thread is in a detached (PTHREAD_CREATE_DETACHED), |
114 | or joinable (default PTHREAD_CREATE_JOINABLE) state and |
115 | STOPPED_START is true, then the creating thread has ownership of |
116 | PD until the PD->lock is released by pthread_create. If any |
117 | errors occur we are in states (c), (d), or (e) below. |
118 | |
119 | (b) If the created thread is in a detached state |
120 | (PTHREAD_CREATED_DETACHED), and STOPPED_START is false, then the |
121 | creating thread has ownership of PD until it invokes the OS |
122 | kernel's thread creation routine. If this routine returns |
123 | without error, then the created thread owns PD; otherwise, see |
124 | (c) and (e) below. |
125 | |
126 | (c) If the detached thread setup failed and THREAD_RAN is true, then |
127 | the creating thread releases ownership to the new thread by |
128 | sending a cancellation signal. All threads set THREAD_RAN to |
129 | true as quickly as possible after returning from the OS kernel's |
130 | thread creation routine. |
131 | |
132 | (d) If the joinable thread setup failed and THREAD_RAN is true, then |
133 | then the creating thread retains ownership of PD and must cleanup |
134 | state. Ownership cannot be released to the process via the |
135 | return of pthread_create since a non-zero result entails PD is |
136 | undefined and therefore cannot be joined to free the resources. |
137 | We privately call pthread_join on the thread to finish handling |
138 | the resource shutdown (Or at least we should, see bug 19511). |
139 | |
140 | (e) If the thread creation failed and THREAD_RAN is false, then the |
141 | creating thread retains ownership of PD and must cleanup state. |
142 | No waiting for the new thread is required because it never |
143 | started. |
144 | |
145 | The nptl_db interface: |
146 | |
147 | The interface with nptl_db requires that we enqueue PD into a linked |
148 | list and then call a function which the debugger will trap. The PD |
149 | will then be dequeued and control returned to the thread. The caller |
150 | at the time must have ownership of PD and such ownership remains |
151 | after control returns to thread. The enqueued PD is removed from the |
152 | linked list by the nptl_db callback td_thr_event_getmsg. The debugger |
153 | must ensure that the thread does not resume execution, otherwise |
154 | ownership of PD may be lost and examining PD will not be possible. |
155 | |
156 | Note that the GNU Debugger as of (December 10th 2015) commit |
157 | c2c2a31fdb228d41ce3db62b268efea04bd39c18 no longer uses |
158 | td_thr_event_getmsg and several other related nptl_db interfaces. The |
159 | principal reason for this is that nptl_db does not support non-stop |
160 | mode where other threads can run concurrently and modify runtime |
161 | structures currently in use by the debugger and the nptl_db |
162 | interface. |
163 | |
164 | Axioms: |
165 | |
166 | * The create_thread function can never set stopped_start to false. |
167 | * The created thread can read stopped_start but never write to it. |
168 | * The variable thread_ran is set some time after the OS thread |
169 | creation routine returns, how much time after the thread is created |
170 | is unspecified, but it should be as quickly as possible. |
171 | |
172 | */ |
173 | |
174 | /* CREATE THREAD NOTES: |
175 | |
176 | createthread.c defines the create_thread function, and two macros: |
177 | START_THREAD_DEFN and START_THREAD_SELF (see below). |
178 | |
179 | create_thread must initialize PD->stopped_start. It should be true |
180 | if the STOPPED_START parameter is true, or if create_thread needs the |
181 | new thread to synchronize at startup for some other implementation |
182 | reason. If STOPPED_START will be true, then create_thread is obliged |
183 | to lock PD->lock before starting the thread. Then pthread_create |
184 | unlocks PD->lock which synchronizes-with START_THREAD_DEFN in the |
185 | child thread which does an acquire/release of PD->lock as the last |
186 | action before calling the user entry point. The goal of all of this |
187 | is to ensure that the required initial thread attributes are applied |
188 | (by the creating thread) before the new thread runs user code. Note |
189 | that the the functions pthread_getschedparam, pthread_setschedparam, |
190 | pthread_setschedprio, __pthread_tpp_change_priority, and |
191 | __pthread_current_priority reuse the same lock, PD->lock, for a |
192 | similar purpose e.g. synchronizing the setting of similar thread |
193 | attributes. These functions are never called before the thread is |
194 | created, so don't participate in startup syncronization, but given |
195 | that the lock is present already and in the unlocked state, reusing |
196 | it saves space. |
197 | |
198 | The return value is zero for success or an errno code for failure. |
199 | If the return value is ENOMEM, that will be translated to EAGAIN, |
200 | so create_thread need not do that. On failure, *THREAD_RAN should |
201 | be set to true iff the thread actually started up and then got |
202 | canceled before calling user code (*PD->start_routine). */ |
203 | static int create_thread (struct pthread *pd, const struct pthread_attr *attr, |
204 | bool *stopped_start, STACK_VARIABLES_PARMS, |
205 | bool *thread_ran); |
206 | |
207 | #include <createthread.c> |
208 | |
209 | |
210 | struct pthread * |
211 | __find_in_stack_list (struct pthread *pd) |
212 | { |
213 | list_t *entry; |
214 | struct pthread *result = NULL; |
215 | |
216 | lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE); |
217 | |
218 | list_for_each (entry, &GL (dl_stack_used)) |
219 | { |
220 | struct pthread *curp; |
221 | |
222 | curp = list_entry (entry, struct pthread, list); |
223 | if (curp == pd) |
224 | { |
225 | result = curp; |
226 | break; |
227 | } |
228 | } |
229 | |
230 | if (result == NULL) |
231 | list_for_each (entry, &GL (dl_stack_user)) |
232 | { |
233 | struct pthread *curp; |
234 | |
235 | curp = list_entry (entry, struct pthread, list); |
236 | if (curp == pd) |
237 | { |
238 | result = curp; |
239 | break; |
240 | } |
241 | } |
242 | |
243 | lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE); |
244 | |
245 | return result; |
246 | } |
247 | |
248 | |
249 | /* Deallocate POSIX thread-local-storage. */ |
250 | void |
251 | attribute_hidden |
252 | __nptl_deallocate_tsd (void) |
253 | { |
254 | struct pthread *self = THREAD_SELF; |
255 | |
256 | /* Maybe no data was ever allocated. This happens often so we have |
257 | a flag for this. */ |
258 | if (THREAD_GETMEM (self, specific_used)) |
259 | { |
260 | size_t round; |
261 | size_t cnt; |
262 | |
263 | round = 0; |
264 | do |
265 | { |
266 | size_t idx; |
267 | |
268 | /* So far no new nonzero data entry. */ |
269 | THREAD_SETMEM (self, specific_used, false); |
270 | |
271 | for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) |
272 | { |
273 | struct pthread_key_data *level2; |
274 | |
275 | level2 = THREAD_GETMEM_NC (self, specific, cnt); |
276 | |
277 | if (level2 != NULL) |
278 | { |
279 | size_t inner; |
280 | |
281 | for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE; |
282 | ++inner, ++idx) |
283 | { |
284 | void *data = level2[inner].data; |
285 | |
286 | if (data != NULL) |
287 | { |
288 | /* Always clear the data. */ |
289 | level2[inner].data = NULL; |
290 | |
291 | /* Make sure the data corresponds to a valid |
292 | key. This test fails if the key was |
293 | deallocated and also if it was |
294 | re-allocated. It is the user's |
295 | responsibility to free the memory in this |
296 | case. */ |
297 | if (level2[inner].seq |
298 | == __pthread_keys[idx].seq |
299 | /* It is not necessary to register a destructor |
300 | function. */ |
301 | && __pthread_keys[idx].destr != NULL) |
302 | /* Call the user-provided destructor. */ |
303 | __pthread_keys[idx].destr (data); |
304 | } |
305 | } |
306 | } |
307 | else |
308 | idx += PTHREAD_KEY_1STLEVEL_SIZE; |
309 | } |
310 | |
311 | if (THREAD_GETMEM (self, specific_used) == 0) |
312 | /* No data has been modified. */ |
313 | goto just_free; |
314 | } |
315 | /* We only repeat the process a fixed number of times. */ |
316 | while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0)); |
317 | |
318 | /* Just clear the memory of the first block for reuse. */ |
319 | memset (&THREAD_SELF->specific_1stblock, '\0', |
320 | sizeof (self->specific_1stblock)); |
321 | |
322 | just_free: |
323 | /* Free the memory for the other blocks. */ |
324 | for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) |
325 | { |
326 | struct pthread_key_data *level2; |
327 | |
328 | level2 = THREAD_GETMEM_NC (self, specific, cnt); |
329 | if (level2 != NULL) |
330 | { |
331 | /* The first block is allocated as part of the thread |
332 | descriptor. */ |
333 | free (level2); |
334 | THREAD_SETMEM_NC (self, specific, cnt, NULL); |
335 | } |
336 | } |
337 | |
338 | THREAD_SETMEM (self, specific_used, false); |
339 | } |
340 | } |
341 | |
342 | |
343 | /* Deallocate a thread's stack after optionally making sure the thread |
344 | descriptor is still valid. */ |
345 | void |
346 | __free_tcb (struct pthread *pd) |
347 | { |
348 | /* The thread is exiting now. */ |
349 | if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling, |
350 | TERMINATED_BIT) == 0, 1)) |
351 | { |
352 | /* Remove the descriptor from the list. */ |
353 | if (DEBUGGING_P && __find_in_stack_list (pd) == NULL) |
354 | /* Something is really wrong. The descriptor for a still |
355 | running thread is gone. */ |
356 | abort (); |
357 | |
358 | /* Free TPP data. */ |
359 | if (__glibc_unlikely (pd->tpp != NULL)) |
360 | { |
361 | struct priority_protection_data *tpp = pd->tpp; |
362 | |
363 | pd->tpp = NULL; |
364 | free (tpp); |
365 | } |
366 | |
367 | /* Queue the stack memory block for reuse and exit the process. The |
368 | kernel will signal via writing to the address returned by |
369 | QUEUE-STACK when the stack is available. */ |
370 | __deallocate_stack (pd); |
371 | } |
372 | } |
373 | |
374 | /* Local function to start thread and handle cleanup. |
375 | createthread.c defines the macro START_THREAD_DEFN to the |
376 | declaration that its create_thread function will refer to, and |
377 | START_THREAD_SELF to the expression to optimally deliver the new |
378 | thread's THREAD_SELF value. */ |
379 | START_THREAD_DEFN |
380 | { |
381 | struct pthread *pd = START_THREAD_SELF; |
382 | |
383 | /* Initialize resolver state pointer. */ |
384 | __resp = &pd->res; |
385 | |
386 | /* Initialize pointers to locale data. */ |
387 | __ctype_init (); |
388 | |
389 | #ifndef __ASSUME_SET_ROBUST_LIST |
390 | if (__set_robust_list_avail >= 0) |
391 | #endif |
392 | { |
393 | /* This call should never fail because the initial call in init.c |
394 | succeeded. */ |
395 | INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head, |
396 | sizeof (struct robust_list_head)); |
397 | } |
398 | |
399 | /* This is where the try/finally block should be created. For |
400 | compilers without that support we do use setjmp. */ |
401 | struct pthread_unwind_buf unwind_buf; |
402 | |
403 | int not_first_call; |
404 | DIAG_PUSH_NEEDS_COMMENT; |
405 | #if __GNUC_PREREQ (7, 0) |
406 | /* This call results in a -Wstringop-overflow warning because struct |
407 | pthread_unwind_buf is smaller than jmp_buf. setjmp and longjmp |
408 | do not use anything beyond the common prefix (they never access |
409 | the saved signal mask), so that is a false positive. */ |
410 | DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overflow=" ); |
411 | #endif |
412 | not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf); |
413 | DIAG_POP_NEEDS_COMMENT; |
414 | |
415 | /* No previous handlers. NB: This must be done after setjmp since the |
416 | private space in the unwind jump buffer may overlap space used by |
417 | setjmp to store extra architecture-specific information which is |
418 | never used by the cancellation-specific __libc_unwind_longjmp. |
419 | |
420 | The private space is allowed to overlap because the unwinder never |
421 | has to return through any of the jumped-to call frames, and thus |
422 | only a minimum amount of saved data need be stored, and for example, |
423 | need not include the process signal mask information. This is all |
424 | an optimization to reduce stack usage when pushing cancellation |
425 | handlers. */ |
426 | unwind_buf.priv.data.prev = NULL; |
427 | unwind_buf.priv.data.cleanup = NULL; |
428 | |
429 | __libc_signal_restore_set (&pd->sigmask); |
430 | |
431 | /* Allow setxid from now onwards. */ |
432 | if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2)) |
433 | futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE); |
434 | |
435 | if (__glibc_likely (! not_first_call)) |
436 | { |
437 | /* Store the new cleanup handler info. */ |
438 | THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf); |
439 | |
440 | /* We are either in (a) or (b), and in either case we either own |
441 | PD already (2) or are about to own PD (1), and so our only |
442 | restriction would be that we can't free PD until we know we |
443 | have ownership (see CONCURRENCY NOTES above). */ |
444 | if (__glibc_unlikely (pd->stopped_start)) |
445 | { |
446 | int oldtype = CANCEL_ASYNC (); |
447 | |
448 | /* Get the lock the parent locked to force synchronization. */ |
449 | lll_lock (pd->lock, LLL_PRIVATE); |
450 | |
451 | /* We have ownership of PD now. */ |
452 | |
453 | /* And give it up right away. */ |
454 | lll_unlock (pd->lock, LLL_PRIVATE); |
455 | |
456 | CANCEL_RESET (oldtype); |
457 | } |
458 | |
459 | LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg); |
460 | |
461 | /* Run the code the user provided. */ |
462 | void *ret; |
463 | if (pd->c11) |
464 | { |
465 | /* The function pointer of the c11 thread start is cast to an incorrect |
466 | type on __pthread_create_2_1 call, however it is casted back to correct |
467 | one so the call behavior is well-defined (it is assumed that pointers |
468 | to void are able to represent all values of int. */ |
469 | int (*start)(void*) = (int (*) (void*)) pd->start_routine; |
470 | ret = (void*) (uintptr_t) start (pd->arg); |
471 | } |
472 | else |
473 | ret = pd->start_routine (pd->arg); |
474 | THREAD_SETMEM (pd, result, ret); |
475 | } |
476 | |
477 | /* Call destructors for the thread_local TLS variables. */ |
478 | #ifndef SHARED |
479 | if (&__call_tls_dtors != NULL) |
480 | #endif |
481 | __call_tls_dtors (); |
482 | |
483 | /* Run the destructor for the thread-local data. */ |
484 | __nptl_deallocate_tsd (); |
485 | |
486 | /* Clean up any state libc stored in thread-local variables. */ |
487 | __libc_thread_freeres (); |
488 | |
489 | /* If this is the last thread we terminate the process now. We |
490 | do not notify the debugger, it might just irritate it if there |
491 | is no thread left. */ |
492 | if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads))) |
493 | /* This was the last thread. */ |
494 | exit (0); |
495 | |
496 | /* Report the death of the thread if this is wanted. */ |
497 | if (__glibc_unlikely (pd->report_events)) |
498 | { |
499 | /* See whether TD_DEATH is in any of the mask. */ |
500 | const int idx = __td_eventword (TD_DEATH); |
501 | const uint32_t mask = __td_eventmask (TD_DEATH); |
502 | |
503 | if ((mask & (__nptl_threads_events.event_bits[idx] |
504 | | pd->eventbuf.eventmask.event_bits[idx])) != 0) |
505 | { |
506 | /* Yep, we have to signal the death. Add the descriptor to |
507 | the list but only if it is not already on it. */ |
508 | if (pd->nextevent == NULL) |
509 | { |
510 | pd->eventbuf.eventnum = TD_DEATH; |
511 | pd->eventbuf.eventdata = pd; |
512 | |
513 | do |
514 | pd->nextevent = __nptl_last_event; |
515 | while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event, |
516 | pd, pd->nextevent)); |
517 | } |
518 | |
519 | /* Now call the function which signals the event. See |
520 | CONCURRENCY NOTES for the nptl_db interface comments. */ |
521 | __nptl_death_event (); |
522 | } |
523 | } |
524 | |
525 | /* The thread is exiting now. Don't set this bit until after we've hit |
526 | the event-reporting breakpoint, so that td_thr_get_info on us while at |
527 | the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */ |
528 | atomic_bit_set (&pd->cancelhandling, EXITING_BIT); |
529 | |
530 | #ifndef __ASSUME_SET_ROBUST_LIST |
531 | /* If this thread has any robust mutexes locked, handle them now. */ |
532 | # if __PTHREAD_MUTEX_HAVE_PREV |
533 | void *robust = pd->robust_head.list; |
534 | # else |
535 | __pthread_slist_t *robust = pd->robust_list.__next; |
536 | # endif |
537 | /* We let the kernel do the notification if it is able to do so. |
538 | If we have to do it here there for sure are no PI mutexes involved |
539 | since the kernel support for them is even more recent. */ |
540 | if (__set_robust_list_avail < 0 |
541 | && __builtin_expect (robust != (void *) &pd->robust_head, 0)) |
542 | { |
543 | do |
544 | { |
545 | struct __pthread_mutex_s *this = (struct __pthread_mutex_s *) |
546 | ((char *) robust - offsetof (struct __pthread_mutex_s, |
547 | __list.__next)); |
548 | robust = *((void **) robust); |
549 | |
550 | # if __PTHREAD_MUTEX_HAVE_PREV |
551 | this->__list.__prev = NULL; |
552 | # endif |
553 | this->__list.__next = NULL; |
554 | |
555 | atomic_or (&this->__lock, FUTEX_OWNER_DIED); |
556 | futex_wake ((unsigned int *) &this->__lock, 1, |
557 | /* XYZ */ FUTEX_SHARED); |
558 | } |
559 | while (robust != (void *) &pd->robust_head); |
560 | } |
561 | #endif |
562 | |
563 | if (!pd->user_stack) |
564 | advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd, |
565 | pd->guardsize); |
566 | |
567 | if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK)) |
568 | { |
569 | /* Some other thread might call any of the setXid functions and expect |
570 | us to reply. In this case wait until we did that. */ |
571 | do |
572 | /* XXX This differs from the typical futex_wait_simple pattern in that |
573 | the futex_wait condition (setxid_futex) is different from the |
574 | condition used in the surrounding loop (cancelhandling). We need |
575 | to check and document why this is correct. */ |
576 | futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE); |
577 | while (pd->cancelhandling & SETXID_BITMASK); |
578 | |
579 | /* Reset the value so that the stack can be reused. */ |
580 | pd->setxid_futex = 0; |
581 | } |
582 | |
583 | /* If the thread is detached free the TCB. */ |
584 | if (IS_DETACHED (pd)) |
585 | /* Free the TCB. */ |
586 | __free_tcb (pd); |
587 | |
588 | /* We cannot call '_exit' here. '_exit' will terminate the process. |
589 | |
590 | The 'exit' implementation in the kernel will signal when the |
591 | process is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID |
592 | flag. The 'tid' field in the TCB will be set to zero. |
593 | |
594 | The exit code is zero since in case all threads exit by calling |
595 | 'pthread_exit' the exit status must be 0 (zero). */ |
596 | __exit_thread (); |
597 | |
598 | /* NOTREACHED */ |
599 | } |
600 | |
601 | |
602 | /* Return true iff obliged to report TD_CREATE events. */ |
603 | static bool |
604 | report_thread_creation (struct pthread *pd) |
605 | { |
606 | if (__glibc_unlikely (THREAD_GETMEM (THREAD_SELF, report_events))) |
607 | { |
608 | /* The parent thread is supposed to report events. |
609 | Check whether the TD_CREATE event is needed, too. */ |
610 | const size_t idx = __td_eventword (TD_CREATE); |
611 | const uint32_t mask = __td_eventmask (TD_CREATE); |
612 | |
613 | return ((mask & (__nptl_threads_events.event_bits[idx] |
614 | | pd->eventbuf.eventmask.event_bits[idx])) != 0); |
615 | } |
616 | return false; |
617 | } |
618 | |
619 | |
620 | int |
621 | __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, |
622 | void *(*start_routine) (void *), void *arg) |
623 | { |
624 | STACK_VARIABLES; |
625 | |
626 | /* Avoid a data race in the multi-threaded case. */ |
627 | if (__libc_single_threaded) |
628 | __libc_single_threaded = 0; |
629 | |
630 | const struct pthread_attr *iattr = (struct pthread_attr *) attr; |
631 | union pthread_attr_transparent default_attr; |
632 | bool destroy_default_attr = false; |
633 | bool c11 = (attr == ATTR_C11_THREAD); |
634 | if (iattr == NULL || c11) |
635 | { |
636 | int ret = __pthread_getattr_default_np (&default_attr.external); |
637 | if (ret != 0) |
638 | return ret; |
639 | destroy_default_attr = true; |
640 | iattr = &default_attr.internal; |
641 | } |
642 | |
643 | struct pthread *pd = NULL; |
644 | int err = ALLOCATE_STACK (iattr, &pd); |
645 | int retval = 0; |
646 | |
647 | if (__glibc_unlikely (err != 0)) |
648 | /* Something went wrong. Maybe a parameter of the attributes is |
649 | invalid or we could not allocate memory. Note we have to |
650 | translate error codes. */ |
651 | { |
652 | retval = err == ENOMEM ? EAGAIN : err; |
653 | goto out; |
654 | } |
655 | |
656 | |
657 | /* Initialize the TCB. All initializations with zero should be |
658 | performed in 'get_cached_stack'. This way we avoid doing this if |
659 | the stack freshly allocated with 'mmap'. */ |
660 | |
661 | #if TLS_TCB_AT_TP |
662 | /* Reference to the TCB itself. */ |
663 | pd->header.self = pd; |
664 | |
665 | /* Self-reference for TLS. */ |
666 | pd->header.tcb = pd; |
667 | #endif |
668 | |
669 | /* Store the address of the start routine and the parameter. Since |
670 | we do not start the function directly the stillborn thread will |
671 | get the information from its thread descriptor. */ |
672 | pd->start_routine = start_routine; |
673 | pd->arg = arg; |
674 | pd->c11 = c11; |
675 | |
676 | /* Copy the thread attribute flags. */ |
677 | struct pthread *self = THREAD_SELF; |
678 | pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) |
679 | | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))); |
680 | |
681 | /* Initialize the field for the ID of the thread which is waiting |
682 | for us. This is a self-reference in case the thread is created |
683 | detached. */ |
684 | pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL; |
685 | |
686 | /* The debug events are inherited from the parent. */ |
687 | pd->eventbuf = self->eventbuf; |
688 | |
689 | |
690 | /* Copy the parent's scheduling parameters. The flags will say what |
691 | is valid and what is not. */ |
692 | pd->schedpolicy = self->schedpolicy; |
693 | pd->schedparam = self->schedparam; |
694 | |
695 | /* Copy the stack guard canary. */ |
696 | #ifdef THREAD_COPY_STACK_GUARD |
697 | THREAD_COPY_STACK_GUARD (pd); |
698 | #endif |
699 | |
700 | /* Copy the pointer guard value. */ |
701 | #ifdef THREAD_COPY_POINTER_GUARD |
702 | THREAD_COPY_POINTER_GUARD (pd); |
703 | #endif |
704 | |
705 | /* Setup tcbhead. */ |
706 | tls_setup_tcbhead (pd); |
707 | |
708 | /* Verify the sysinfo bits were copied in allocate_stack if needed. */ |
709 | #ifdef NEED_DL_SYSINFO |
710 | CHECK_THREAD_SYSINFO (pd); |
711 | #endif |
712 | |
713 | /* Determine scheduling parameters for the thread. */ |
714 | if (__builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0) |
715 | && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0) |
716 | { |
717 | /* Use the scheduling parameters the user provided. */ |
718 | if (iattr->flags & ATTR_FLAG_POLICY_SET) |
719 | { |
720 | pd->schedpolicy = iattr->schedpolicy; |
721 | pd->flags |= ATTR_FLAG_POLICY_SET; |
722 | } |
723 | if (iattr->flags & ATTR_FLAG_SCHED_SET) |
724 | { |
725 | /* The values were validated in pthread_attr_setschedparam. */ |
726 | pd->schedparam = iattr->schedparam; |
727 | pd->flags |= ATTR_FLAG_SCHED_SET; |
728 | } |
729 | |
730 | if ((pd->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) |
731 | != (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) |
732 | collect_default_sched (pd); |
733 | } |
734 | |
735 | if (__glibc_unlikely (__nptl_nthreads == 1)) |
736 | _IO_enable_locks (); |
737 | |
738 | /* Pass the descriptor to the caller. */ |
739 | *newthread = (pthread_t) pd; |
740 | |
741 | LIBC_PROBE (pthread_create, 4, newthread, attr, start_routine, arg); |
742 | |
743 | /* One more thread. We cannot have the thread do this itself, since it |
744 | might exist but not have been scheduled yet by the time we've returned |
745 | and need to check the value to behave correctly. We must do it before |
746 | creating the thread, in case it does get scheduled first and then |
747 | might mistakenly think it was the only thread. In the failure case, |
748 | we momentarily store a false value; this doesn't matter because there |
749 | is no kosher thing a signal handler interrupting us right here can do |
750 | that cares whether the thread count is correct. */ |
751 | atomic_increment (&__nptl_nthreads); |
752 | |
753 | /* Our local value of stopped_start and thread_ran can be accessed at |
754 | any time. The PD->stopped_start may only be accessed if we have |
755 | ownership of PD (see CONCURRENCY NOTES above). */ |
756 | bool stopped_start = false; bool thread_ran = false; |
757 | |
758 | /* Block all signals, so that the new thread starts out with |
759 | signals disabled. This avoids race conditions in the thread |
760 | startup. */ |
761 | sigset_t original_sigmask; |
762 | __libc_signal_block_all (&original_sigmask); |
763 | |
764 | if (iattr->extension != NULL && iattr->extension->sigmask_set) |
765 | /* Use the signal mask in the attribute. The internal signals |
766 | have already been filtered by the public |
767 | pthread_attr_setsigmask_np interface. */ |
768 | pd->sigmask = iattr->extension->sigmask; |
769 | else |
770 | { |
771 | /* Conceptually, the new thread needs to inherit the signal mask |
772 | of this thread. Therefore, it needs to restore the saved |
773 | signal mask of this thread, so save it in the startup |
774 | information. */ |
775 | pd->sigmask = original_sigmask; |
776 | |
777 | /* Reset the cancellation signal mask in case this thread is |
778 | running cancellation. */ |
779 | __sigdelset (&pd->sigmask, SIGCANCEL); |
780 | } |
781 | |
782 | /* Start the thread. */ |
783 | if (__glibc_unlikely (report_thread_creation (pd))) |
784 | { |
785 | stopped_start = true; |
786 | |
787 | /* We always create the thread stopped at startup so we can |
788 | notify the debugger. */ |
789 | retval = create_thread (pd, iattr, &stopped_start, |
790 | STACK_VARIABLES_ARGS, &thread_ran); |
791 | if (retval == 0) |
792 | { |
793 | /* We retain ownership of PD until (a) (see CONCURRENCY NOTES |
794 | above). */ |
795 | |
796 | /* Assert stopped_start is true in both our local copy and the |
797 | PD copy. */ |
798 | assert (stopped_start); |
799 | assert (pd->stopped_start); |
800 | |
801 | /* Now fill in the information about the new thread in |
802 | the newly created thread's data structure. We cannot let |
803 | the new thread do this since we don't know whether it was |
804 | already scheduled when we send the event. */ |
805 | pd->eventbuf.eventnum = TD_CREATE; |
806 | pd->eventbuf.eventdata = pd; |
807 | |
808 | /* Enqueue the descriptor. */ |
809 | do |
810 | pd->nextevent = __nptl_last_event; |
811 | while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event, |
812 | pd, pd->nextevent) |
813 | != 0); |
814 | |
815 | /* Now call the function which signals the event. See |
816 | CONCURRENCY NOTES for the nptl_db interface comments. */ |
817 | __nptl_create_event (); |
818 | } |
819 | } |
820 | else |
821 | retval = create_thread (pd, iattr, &stopped_start, |
822 | STACK_VARIABLES_ARGS, &thread_ran); |
823 | |
824 | /* Return to the previous signal mask, after creating the new |
825 | thread. */ |
826 | __libc_signal_restore_set (&original_sigmask); |
827 | |
828 | if (__glibc_unlikely (retval != 0)) |
829 | { |
830 | if (thread_ran) |
831 | /* State (c) or (d) and we may not have PD ownership (see |
832 | CONCURRENCY NOTES above). We can assert that STOPPED_START |
833 | must have been true because thread creation didn't fail, but |
834 | thread attribute setting did. */ |
835 | /* See bug 19511 which explains why doing nothing here is a |
836 | resource leak for a joinable thread. */ |
837 | assert (stopped_start); |
838 | else |
839 | { |
840 | /* State (e) and we have ownership of PD (see CONCURRENCY |
841 | NOTES above). */ |
842 | |
843 | /* Oops, we lied for a second. */ |
844 | atomic_decrement (&__nptl_nthreads); |
845 | |
846 | /* Perhaps a thread wants to change the IDs and is waiting for this |
847 | stillborn thread. */ |
848 | if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) |
849 | == -2)) |
850 | futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE); |
851 | |
852 | /* Free the resources. */ |
853 | __deallocate_stack (pd); |
854 | } |
855 | |
856 | /* We have to translate error codes. */ |
857 | if (retval == ENOMEM) |
858 | retval = EAGAIN; |
859 | } |
860 | else |
861 | { |
862 | /* We don't know if we have PD ownership. Once we check the local |
863 | stopped_start we'll know if we're in state (a) or (b) (see |
864 | CONCURRENCY NOTES above). */ |
865 | if (stopped_start) |
866 | /* State (a), we own PD. The thread blocked on this lock either |
867 | because we're doing TD_CREATE event reporting, or for some |
868 | other reason that create_thread chose. Now let it run |
869 | free. */ |
870 | lll_unlock (pd->lock, LLL_PRIVATE); |
871 | |
872 | /* We now have for sure more than one thread. The main thread might |
873 | not yet have the flag set. No need to set the global variable |
874 | again if this is what we use. */ |
875 | THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); |
876 | } |
877 | |
878 | out: |
879 | if (destroy_default_attr) |
880 | __pthread_attr_destroy (&default_attr.external); |
881 | |
882 | return retval; |
883 | } |
884 | versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1); |
885 | |
886 | |
887 | #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1) |
888 | int |
889 | __pthread_create_2_0 (pthread_t *newthread, const pthread_attr_t *attr, |
890 | void *(*start_routine) (void *), void *arg) |
891 | { |
892 | /* The ATTR attribute is not really of type `pthread_attr_t *'. It has |
893 | the old size and access to the new members might crash the program. |
894 | We convert the struct now. */ |
895 | struct pthread_attr new_attr; |
896 | |
897 | if (attr != NULL) |
898 | { |
899 | struct pthread_attr *iattr = (struct pthread_attr *) attr; |
900 | size_t ps = __getpagesize (); |
901 | |
902 | /* Copy values from the user-provided attributes. */ |
903 | new_attr.schedparam = iattr->schedparam; |
904 | new_attr.schedpolicy = iattr->schedpolicy; |
905 | new_attr.flags = iattr->flags; |
906 | |
907 | /* Fill in default values for the fields not present in the old |
908 | implementation. */ |
909 | new_attr.guardsize = ps; |
910 | new_attr.stackaddr = NULL; |
911 | new_attr.stacksize = 0; |
912 | new_attr.extension = NULL; |
913 | |
914 | /* We will pass this value on to the real implementation. */ |
915 | attr = (pthread_attr_t *) &new_attr; |
916 | } |
917 | |
918 | return __pthread_create_2_1 (newthread, attr, start_routine, arg); |
919 | } |
920 | compat_symbol (libpthread, __pthread_create_2_0, pthread_create, |
921 | GLIBC_2_0); |
922 | #endif |
923 | |
924 | /* Information for libthread_db. */ |
925 | |
926 | #include "../nptl_db/db_info.c" |
927 | |
928 | /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread |
929 | functions to be present as well. */ |
930 | PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_lock) |
931 | PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_trylock) |
932 | PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_unlock) |
933 | |
934 | PTHREAD_STATIC_FN_REQUIRE (__pthread_once) |
935 | PTHREAD_STATIC_FN_REQUIRE (__pthread_cancel) |
936 | |
937 | PTHREAD_STATIC_FN_REQUIRE (__pthread_key_create) |
938 | PTHREAD_STATIC_FN_REQUIRE (__pthread_key_delete) |
939 | PTHREAD_STATIC_FN_REQUIRE (__pthread_setspecific) |
940 | PTHREAD_STATIC_FN_REQUIRE (__pthread_getspecific) |
941 | |