aboutsummaryrefslogtreecommitdiff
path: root/lib/libthr/thread
Commit message (Collapse)AuthorAgeFilesLines
* libthr: GC unused _sigsuspend()Brooks Davis2025-08-062-9/+0
| | | | | | | There are no consumers and it isn't a public symbol. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D51670
* libthr: fix typo in commentKonstantin Belousov2025-07-291-1/+1
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 3 days
* libc,libthr: Remove __pthread_distribute_static_tlsJessica Clarke2025-07-102-40/+0
| | | | | | | This private API is no longer used by rtld-elf so can be removed. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D50921
* libthr/amd64: do not set THR_C_RUNTIME for thr_new() if the main thread did ↵Konstantin Belousov2025-06-223-2/+6
| | | | | | | | | | | | | | used AMD64_SET_TLSBASE It is up to the code that organizes the runtime to properly set the signal handler, and to set %fsbase if libthr signal handler is to be called. The change should leave the CPU state on the signal handler entry identical to what it was before introduction of TLSBASE, for code that provides its own startup and thread pointer, but still calls into libthr as a hack. Sponsored by: The FreeBSD Foundation MFC after: 1 week
* Implement CLOCK_TAINathan Whitehorn2025-06-121-0/+1
| | | | | | | | | | | | | | Provide a clock through clock_gettime() that returns the current TAI time (UTC without leap seconds) as a complement to CLOCK_REALTIME. This provides compatibility with Linux, which also provides a CLOCK_TAI since kernel 2.6.26, and this seems to be becoming the standard way to acquire TAI time. Unlike Linux, this code will return EINVAL if the TAI offset (set by ntpd, ptpd, etc.) is not known since it seems pathological for CLOCK_TAI to silently give the wrong (UTC) time if the offset is not known as it does on Linux. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D46268
* C runtime: add kernel version guards on exterrctlBrooks Davis2025-06-042-2/+9
| | | | | | | | | | | | This allows userspace to run on a (somewhat) out of date kernel. Avoid a __FreeBSD_version bump and use the bump from a02180cf60a6 which has occured since exterrctl was added. Reviewed by: kevans Fixes: b9c8a07d4dd9 ("C runtime: enable extended error reporting from kernel") Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D50687
* Provide user interface to retrieve reported extended errorsKonstantin Belousov2025-05-311-2/+12
| | | | | | | Reviewed by: brooks Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D50483
* C runtime: enable extended error reporting from kernelKonstantin Belousov2025-05-313-0/+10
| | | | | | | Reviewed by: brooks Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D50483
* thr_new: add a flag indicating that the thread is created by C runtimeKonstantin Belousov2025-05-311-1/+1
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libc, libthr: Ditch MD __pthread_distribute_static_tls helpersJessica Clarke2025-05-291-9/+16
| | | | | | | | | | | | | | | | | | _libc_get_static_tls_base() is just _tcb_get() followed by adding (for Variant I) or subtracting (for Variant II) the offset, so just inline that as the implementation (like we do in rtld-elf) rather than having another copy (or equivalent) of _tcb_get()'s assembly. _get_static_tls_base() doesn't even have any MD assembly as it's reading thr->tcb, the only difference is whether to add or subtract, so again just inline that. Whilst here add some missing blank lines to comply with style(9) for elf_utils.c's includes, and use a pointer type rather than uintptr_t to reduce the need to cast, as is done in rtld-elf. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D50592
* libthr: add stable user interface for sigfastblock(2)Konstantin Belousov2025-05-191-0/+19
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D50377
* pthread_switch_add_np(3): removeKonstantin Belousov2025-05-191-1/+4
| | | | | | | | | | | | The interface is not functional with libthr and returns not supported error always. There is no point in providing it to userspace or document. The symbols are kept for ABI compatibility, of course. Reviewed by: emaste, markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D50376
* thr_rtld: accept read lock requests while owning the lock for writeKonstantin Belousov2025-05-021-5/+21
| | | | | | | | PR: 286505 Reviewed by: olce Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D50117
* libthr: properly align struct pthreadsKonstantin Belousov2025-04-251-1/+3
| | | | | | | | | | For instance, the structure contains the struct _Unwind_Exception, and it seems that libgcc requires specific alignment for it. PR: 285711 Tested by: Oleg Sidorkin <osidorkin@gmail.com> Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libthr: add __thr_aligned_alloc_offset()Konstantin Belousov2025-04-252-0/+14
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week
* pthread_create(): styleKonstantin Belousov2025-01-141-3/+3
| | | | | | | | | Use NULL instead of 0 for null pointer. Use != 0 for non-bool, as in the rest of the function. Remove unneeded (). Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libpthread_init(): ensure curthread == NULL until set explicitlyKonstantin Belousov2025-01-141-0/+2
| | | | | | | | | | | | | Otherwise libthr::_get_curthread() returns a garbage kept there from allocate_initial_tls(), until libthr initialization proceeds enough to set initial pcb->pcb_thread. The garbage pcb_thread was dereferenced as struct pthread and some memory read as TID. Since the read might not be consistent between reads, thr_malloc_umtx unlock sometimes returned EPERM instead of clearing the lock word. Reported by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libthr: switch thread and sleepq memory allocator to crt from libc mallocKonstantin Belousov2025-01-142-4/+4
| | | | | | | | | | | | | There are more complex interactions between malloc and libthr initialization that can happen if libthr functions are called from ELF object' constructors, before libthr is initialized. Break the dependencies loop by using the private allocator with controlled init. Reported by: yuri Reviewed by: markj, olce Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48454
* pthread_mutex_trylock(): init libthr if neededKonstantin Belousov2025-01-141-0/+1
| | | | | | | | Reported by: yuri Reviewed by: markj, olce Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48454
* libthr: use atomic_add_int() instead of atomic_fetchadd_int()Konstantin Belousov2025-01-141-5/+5
| | | | | | | | | the return value is ignored. Reviewed by: markj, olce Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48454
* check_cancel: when in_sigsuspend, send SIGCANCEL unconditionallyKonstantin Belousov2024-12-281-3/+5
| | | | | | | | | | | | | | not only when check_cancel is called from the signal handler. _thr_wake() is not enough to get the sigsuspend(2) syscall out of the loop, since it only terminates sleep when an unblocked signal is delivered. PR: 283101 In collaboration with: pho Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* pthread_setcancelstate(3): make it async-signal-safeKonstantin Belousov2024-12-281-6/+6
| | | | | | | | | | | | | | | | by setting new cancel state and reading old cancel state from the curthread structure atomic. Note that this does not play well with async cancellation, since if cancellation is enabled from a signal handler and cancellation request is pending, the thread is cancelled immediately, calling user-defined destructors, which all must be async-signal-safe (but this is a general requirement for async cancellation anyway). Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* thr_cancel.c: use testcancel() instead of manually expanding itKonstantin Belousov2024-12-281-3/+2
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* libthr: move dlerror_msg into zeroed part of struct threadKonstantin Belousov2024-12-281-5/+5
| | | | | | | | | | struct thread reuse might cause stalled rtld error message to propagate to the new thread. Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* thr_sig.c: styleKonstantin Belousov2024-12-281-12/+10
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* thr_cancel.c: styleKonstantin Belousov2024-12-281-4/+4
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D48200
* libc, libthr: coordinate stubs for pthread_{suspend,resume}_all_npKyle Evans2024-11-144-4/+10
| | | | | | | | | If libthr isn't linked into the process, then we don't have any pthreads to worry about and our stubs can just return success -- there are none to suspend/resume. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D47350
* libthr: allow very early atfork registrationKyle Evans2024-11-142-9/+12
| | | | | | | | | | | | | | | | LSan wants to be able to register atfork handlers at __lsan_init time, which can happen either at the first intercepted function call or in a .preinit_array function. Both of these end up being very early in rtld and executed with the bind lock held, which ends up causing problems when we go to _libpthread_init(). Instead of requiring libpthread to be initialized, just insert the new atfork handler straight into the list if it's not ready yet. The critical section and locking should not be necessary if we're really executing this early, as there won't be any threads to contend with. Reviewed by: kib (earlier version), markj Differential Revision: https://reviews.freebsd.org/D47349
* lib{c,sys}: stop exposing errno symbolBrooks Davis2024-09-271-4/+3
| | | | | | | | | | | | | | | | | Officially since C11 (and in reality FreeBSD since 3.0 with commit 1b46cb523df3) errno has been defined to be a macro. Rename the symbol to __libsys_errno and move it to FBSDprivate_1.0 and confine it entierly to libsys for use by libthr. Add a FBSD_1.0 compat symbol for existing binaries that were incorrectly linked to the errno symbol during libc.so.7's lifetime. This deliberately breaks linking software that directly links to errno. Such software is broken and will fail in surprising ways if it becomes threaded (e.g., if it triggers loading of a pam or nss module that uses threads.) Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D46780
* Remove "All Rights Reserved" from FreeBSD Foundation copyrightsEd Maste2024-07-301-1/+0
| | | | | | | These ones were unambiguous cases where the Foundation was the only listed copyright holder. Sponsored by: The FreeBSD Foundation
* rtld: avoid division in __thr_map_stacks_exec()Konstantin Belousov2024-07-291-4/+1
| | | | | | | | | | | | | | | The function is called by rtld with the rtld bind lock write-locked, when fixing the stack permission during dso load. Not every ARMv7 CPU supports the div, which causes the recursive entry into rtld to resolve the __aeabi_uidiv symbol, causing self-lock. Workaround the problem by using roundup2() instead of open-coding less efficient formula. Diagnosed by: mmel Based on submission by: John F Carr <jfc@mit.edu> Sponsored by: The FreeBSD Foundation MFC after: 1 week
* Revert "Avoid division in round_up."Brooks Davis2024-07-251-1/+4
| | | | | | This reverts commit 2b229739e7ca43b36e53f47147ac05a91333f7b9. Pull Request: https://github.com/freebsd/freebsd-src/pull/1343
* Avoid division in round_up.John F. Carr2024-07-251-4/+1
| | | | | | | Division may require a function call, leading to dynamic function lookup, leading to deadlock in rtld. Pull Request: https://github.com/freebsd/freebsd-src/pull/1343
* libthr: Preresolve selected EABI symbols on arm.Michal Meloun2024-07-252-0/+4
| | | | | | | | | | | Add the ability to pre-resolve architecture-specific EABI symbols and use it on arm for selected EABI functions. These functions can be called with rtld bind lock write-locked, so they should be resolved in forward. Reported by: Mark Millard <marklmi@yahoo.com>, John F Carr <jfc@mit.edu> Reviewed by: kib, imp MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D46104
* Remove residual blank line at start of MakefileWarner Losh2024-07-151-1/+0
| | | | | | | This is a residual of the $FreeBSD$ removal. MFC After: 3 days (though I'll just run the command on the branches) Sponsored by: Netflix
* rtld: quiet gcc -WrestrictRyan Libby2024-07-071-3/+3
| | | | | | Reported by: GCC -Wrestrict Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D45892
* libthr: move __always_inline to canonical positionRyan Libby2024-06-241-1/+1
| | | | | | | | | Ahead of including inline in __always_inline, move __always_inline to where inline goes. Reviewed by: imp, kib, olce Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D45710
* Simplify signal handling code in libthr by removing use of SYS_sigreturnDapeng Gao2024-06-061-5/+3
| | | | | | | | | | | | | | The use of SYS_sigreturn is unnecessary here. If handle_signal is called when a signal is delivered, it can just return normally back to sigcode which will call sigreturn anyway. In case handle_signal is called by check_deferred_signal, using setcontext is better than SYS_sigreturn because that is the correct system call to pair with the earlier getcontext. Reviewed by: imp, kib Differential Revision: https://reviews.freebsd.org/D44893
* libthr: avoid varargs in fcntl and openat interposersBrooks Davis2024-05-082-22/+7
| | | | | | | | | | | Align these signatures with the ones in syscalls.master (and thus libsys.h). There's no reason to do va_args twice and in some ABIs (e.g,, CheriABI) you can't access fixed arguments as varargs if you weren't called with varargs signature. Reviewed by: imp, kib, jhibbits Obtained from: CheriBSD Differential Revision: https://reviews.freebsd.org/D45126
* libthr: add pthread_sigqueue(3)Konstantin Belousov2024-04-233-0/+81
| | | | | | | | PR: 278459 Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D44867
* libthr: always use __libc_interposing_slot()Brooks Davis2024-04-221-1/+1
| | | | | | | | | Use __libc_interposing_slot() in favor of __libsys_interposing_slot() so that the interposing interface is entierly between libc and libthr with libsys only involved as an implementation detail. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D44880
* syscalls.master: make __sys_fcntl take an intptr_tBrooks Davis2024-03-191-2/+2
| | | | | | | | | | | | | The (optional) third argument of fcntl is sometimes a pointer so change the type to intptr_t. Update the libc-internal defintion (actually used by libthr) to take a fixed intptr_t argument rather than pretending it's a variadic function. (That worked because all supported architectures pass variadic arguments as though the function was declared with those types. In CheriBSD that changes because variadic arguments are passed via a bounded array.) Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D44381
* libthr: restore _pthread_cond_timedwaitBrooks Davis2024-03-121-0/+1
| | | | | | | | | The function was renamed to _thr_cond_timedwait in commit 0ab1bfc7b28f and for some reason did not get the same __weak_reference treatment as other _pthread_cond symbols. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D44244
* libthr: remove explicit sys/cdefs.h includesKonstantin Belousov2024-03-0649-50/+0
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libthr: move _umtx_op_err() to libsysBrooks Davis2024-02-052-11/+0
| | | | | | | | | | | Declare in sys/umtx.h and implement in libsys. Explicitly link libthr with libsys. When building libthr static include _umtx_op_err so we don't break static linkage with -lpthread. Reviewed by: kib, emaste, imp Pull Request: https://github.com/freebsd/freebsd-src/pull/908
* libc: split libc and syscall interposing (1/2)Brooks Davis2024-02-051-5/+10
| | | | | | | | | | | | | | System calls or their wrappers are now interposed by __libsys_interposing with purely libc entries remaining in __libc_interposing. Use __libsys_interposing_slot in libthr to update __libsys_interposing, but also make __libc_interposing_slot fall back to __libsys_interposing_slot so an out of date libc has a chance of working during updates. Reviewed by: kib, emaste, imp Pull Request: https://github.com/freebsd/freebsd-src/pull/908
* libthr: Force the thr_wake() symbol to be resolved during initializationMark Johnston2024-02-021-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | Otherwise the lock upgrade performed by rtld's load_filtees() can result in infinite recursion, wherein: 1. _rtld_bind() acquires the bind read lock, 2. the source DSO's filtees haven't been loaded yet, so the lock upgrade in load_filtees() cause rtld to jump to _rtld_bind() and release the bind lock, 3. _thr_rtld_lock_release() calls _thr_ast(), which calls thr_wake(), which hasn't been resolved yet, 4. _rtld_bind() acquires the bind read lock in order to resolve thr_wake(), 5. ... See the linked pull request for an instance of this problem arising with libsys. That particular instance is also worked around by commit e7951d0b04e6. Reported by: brooks Reviewed by: kib Pull Request: https://github.com/freebsd/freebsd-src/pull/908 MFC after: 1 week Sponsored by: Innovate UK
* pthread_attr_get_np(): Use malloc(), report ENOMEM, don't tamper on errorOlivier Certner2024-01-102-20/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Similarly as in the previous commit, using calloc() instead of malloc() is useless here in the regular case since the subsequent call to cpuset_getaffinify() is going to completely fill the allocated memory. However, there is an additional complication. This function tries to allocate memory to hold the cpuset if it previously wasn't, and does so before the thread lock is acquired, which can fail on a bad thread ID. In this case, it is necessary to deallocate the memory allocated in this function so that the attributes object appears unmodified to the caller when an error is returned. Without this, a subsequent call to pthread_attr_getaffinity_np() would expose uninitialized memory (not a security problem per se, since it comes from the same process) instead of returning a full mask as it would before the failing call to pthread_attr_get_np(). So the caller would be able to notice a change in the state of the attributes object even if pthread_attr_get_np() reported failure, which would be quite surprising. A similar problem that could occur on failure of cpuset_setaffinity() has been fixed. Finally, we shall always report memory allocation failure. This already goes for pthread_attr_init(), so, if for nothing else, just be consistent. Reviewed by: emaste, kib Approved by: emaste (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D43329
* libhtr: pthread_attr_setaffinity_np(): Replace calloc() with malloc()Olivier Certner2024-01-091-1/+1
| | | | | | | | | | | Using calloc() instead of malloc() is useless here since the allocated memory is to be wholly crushed by the memcpy() call that follows. Suggested by: kib Reviewed by: emaste, kib Approved by: emaste (mentor) MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D43328
* libthr: thr_attr.c: More style and clarity fixesOlivier Certner2024-01-091-7/+7
| | | | | | | | | | | | | | | | | | | | The change of argument for sizeof() (from a type to an object) is to be consistent with the change done for the malloc() code just above in the preceding commit touching this file. Consider bit flags as integers and test whether they are set with an explicit comparison with 0. Use an explicit flag value (PTHREAD_SCOPE_SYSTEM) in place of a variable that has this value at point of substitution. All other changes are straightforward. Suggested by: kib Reviewed by: kib Approved by: emaste (mentor) MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D43327