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