aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/kern_kthread.c
Commit message (Collapse)AuthorAgeFilesLines
* kthread: Fix a thread leakMark Johnston2026-07-241-0/+1
| | | | | | | | Fixes: 963629923308 ("kthread_add(): do not allow to attach the thread to a dead or dying process") Reviewed by: kib MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D58433
* machine/stdarg.h -> sys/stdarg.hBrooks Davis2025-06-111-2/+1
| | | | | | | | | | | | | Switch to using sys/stdarg.h for va_list type and va_* builtins. Make an attempt to insert the include in a sensible place. Where style(9) was followed this is easy, where it was ignored, aim for the first block of sys/*.h headers and don't get too fussy or try to fix other style bugs. Reviewed by: imp Exp-run by: antoine (PR 286274) Pull Request: https://github.com/freebsd/freebsd-src/pull/1595
* kthread: Set *tdptr earlier in kproc_kthread_add()Mark Johnston2024-04-251-1/+9
| | | | | | | | | | | | | | | | | | See commit ae77041e0714 ("kthread: Set *newtdp earlier in kthread_add1()") for details. That commit was incomplete since g_init()'s first call to kproc_kthread_add() will cause kproc_kthread_add() to take the `*procptr == NULL` branch, which avoids kthread_create(). To ensure that the thread pointer is initialized before the thread starts running, we have to start the kernel process with RFSTOPPED. We could perhaps go further and use RFSTOPPED only when tdptr != NULL, but it's probably better to have consistent behaviour. Reviewed by: olce, kib Reported by: syzbot+e91e798f3c088215ace6@syzkaller.appspotmail.com MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D44927
* kthread: Set *newtdp earlier in kthread_add1()Mark Johnston2023-12-091-2/+7
| | | | | | | | | | | | | | | | | | | | | | | | | syzbot reported a single boot-time crash in g_event_procbody(), a page fault when dereferencing g_event_td. g_event_td is initialized by the kproc_kthread_add() call which creates the GEOM event thread: kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td, RFHIGHPID, 0, "geom", "g_event"); I believe that the caller of kproc_kthread_add() was preempted after adding the new thread to the scheduler, and before setting *newtdp, which is equal to g_event_td. Thus, since the first action of the GEOM event thread is to lock itself, it ended up dereferencing a NULL pointer. Fix the problem simply by initializing *newtdp earlier. I see no harm in that, and it matches kproc_create1(). The scheduler provides sufficient synchronization to ensure that the store is visible to the new thread, wherever it happens to run. Reported by: syzbot+5397f4d39219b85a9409@syzkaller.appspotmail.com Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D42986
* sys: Automated cleanup of cdefs and other formattingWarner Losh2023-11-271-1/+0
| | | | | | | | | | | | | | | | Apply the following automated changes to try to eliminate no-longer-needed sys/cdefs.h includes as well as now-empty blank lines in a row. Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/ Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/ Remove /\n+#if.*\n#endif.*\n+/ Remove /^#if.*\n#endif.*\n/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/ Sponsored by: Netflix
* kthread_add(): do not allow to attach the thread to a dead or dying processKonstantin Belousov2023-09-051-0/+4
| | | | | | Reported by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week
* kern_kthread: fork1() does not handle locked GiantKonstantin Belousov2023-09-031-17/+43
| | | | | | | | | | | | | | | fork1() does not behave if called under Giant. For instance, it might need to call thread_suspend_check() which explicitly verifies that Giant is not locked. On the other hand, the kthread KPI is often called from SYSINIT() which is still Giant-locked. Correct this by dropping Giant in kthread_add() and kproc_create(). Reported by: pho Reviewed by: jhb Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41694
* kern_kthread.c: some styleKonstantin Belousov2023-09-031-14/+15
| | | | | | | Reviewed by: jhb Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41694
* sys: Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-161-2/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-121-1/+1
| | | | | | | | | The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch up to that fact and revert to their recommended match of BSD-2-Clause. Discussed with: pfg MFC After: 3 days Sponsored by: Netflix
* Update comments referencing create_thread()Mitchell Horne2023-02-091-1/+1
| | | | | | | | | | The equivalent function is now named thread_create(). Mention kthread_add() where it is also relevant. Reviewed by: kib, markj MFC after: 3 days Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D38180
* umtx: Split umtx.h on two counterparts.Dmitry Chagin2021-07-291-1/+1
| | | | | | | | | | | | To prevent umtx.h polluting by future changes split it on two headers: umtx.h - ABI header for userspace; umtxvar.h - the kernel staff. While here fix umtx_key_match style. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D31248 MFC after: 2 weeks
* Set TDP_KTHREAD before calling cpu_fork() and cpu_copy_thread().John Baldwin2021-03-121-12/+5
| | | | | | | | | | | | | This permits these routines to use special logic for initializing MD kthread state. For the kproc case, this required moving the logic to set these flags from kproc_create() into do_fork(). Reviewed by: kib MFC after: 1 week Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D29207
* Provide ABI modules hooks for process exec/exit and thread exit.Konstantin Belousov2020-11-231-0/+5
| | | | | | | | | | | | | | | Exec and exit are same as corresponding eventhandler hooks. Thread exit hook is called somewhat earlier, while thread is still owned by the process and enough context is available. Note that the process lock is owned when the hook is called. Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D27309 Notes: svn path=/head/; revision=367959
* thread: rework tidhash vs proc lock interactionMateusz Guzik2020-11-111-4/+1
| | | | | | | | Apart from minor clean up this gets rid of proc unlock/lock cycle on thread exit to work around LOR against tidhash lock. Notes: svn path=/head/; revision=367584
* Do a lockless check in kthread_suspend_checkMateusz Guzik2020-07-251-4/+7
| | | | | | | | | | Otherwise an idle system running lockstat sleep 10 reports contention on process lock comming from bufdaemon. While here fix a style nit. Notes: svn path=/head/; revision=363511
* Remove duplicated empty lines from kern/*.cMateusz Guzik2020-01-301-1/+0
| | | | | | | No functional changes. Notes: svn path=/head/; revision=357312
* schedlock 1/4Jeff Roberson2019-12-151-2/+2
| | | | | | | | | | | | | | | Eliminate recursion from most thread_lock consumers. Return from sched_add() without the thread_lock held. This eliminates unnecessary atomics and lock word loads as well as reducing the hold time for scheduler locks. This will eventually allow for lockless remote adds. Discussed with: kib Reviewed by: jhb Tested by: pho Differential Revision: https://reviews.freebsd.org/D22626 Notes: svn path=/head/; revision=355779
* proc: always store parent pid in p_oppidMateusz Guzik2018-11-161-1/+1
| | | | | | | | | | | | Doing so removes the dependency on proctree lock from sysctl process list export which further reduces contention during poudriere -j 128 runs. Reviewed by: kib (previous version) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D17825 Notes: svn path=/head/; revision=340482
* hwpmc: log name->pid, name->tid mappingsMatt Macy2018-06-051-1/+14
| | | | | | | | | | | | | | By logging all threads and processes 'pmc filter' can now filter on process or thread name, relieving the user of the burden of determining which tid or pid was which when the sample was taken. % pmc filter -T if_io_tqg -P nginx pmc.log pmc-iflib.log % pmc filter -x -T idle pmc.log pmc-noidle.log Notes: svn path=/head/; revision=334647
* Implement several enhancements to NUMA policies.Jeff Roberson2018-03-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | Add a new "interleave" allocation policy which stripes pages across domains with a stride or width keeping contiguity within a multi-page region. Move the kernel to the dedicated numbered cpuset #2 making it possible to assign kernel threads and memory policy separately from user. This also eliminates the need for the complicated interrupt binding code. Add a sysctl API for viewing and manipulating domainsets. Refactor some of the cpuset_t manipulation code using the generic bitset type so that it can be used for both. This probably belongs in a dedicated subr file. Attempt to improve the include situation. Reviewed by: kib Discussed with: jhb (cpuset parts) Tested by: pho (before review feedback) Sponsored by: Netflix, Dell/EMC Isilon Differential Revision: https://reviews.freebsd.org/D14839 Notes: svn path=/head/; revision=331723
* Instrument thread creations for the the benefit of the TSLOG framework.Colin Percival2017-12-311-0/+4
| | | | | | | | This assists in tracking time spent while the boot is being "held" waiting for something to happen. Notes: svn path=/head/; revision=327431
* sys/kern: adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-271-0/+2
| | | | | | | | | | | | | | | Mainly focus on files that use BSD 2-Clause license, however the tool I was using misidentified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Notes: svn path=/head/; revision=326271
* Re-schedule signals after kthread exits, since apparently there areKonstantin Belousov2016-08-101-4/+7
| | | | | | | | | | | | | processes which combine kernel and non-kernel threads, e.g. nfsd. For such processes, termination of a kthread must recheck signal delivery among other threads according to masks. Reported and tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=303914
* Update comments for the MD functions managing contexts for newKonstantin Belousov2016-06-161-8/+5
| | | | | | | | | | | | | | | | | | | threads, to make it less confusing and using modern kernel terms. Rename the functions to reflect current use of the functions, instead of the historic KSE conventions: cpu_set_fork_handler -> cpu_fork_kthread_handler (for kthreads) cpu_set_upcall -> cpu_copy_thread (for forks) cpu_set_upcall_kse -> cpu_set_upcall (for new threads creation) Reviewed by: jhb (previous version) Sponsored by: The FreeBSD Foundation MFC after: 1 week Approved by: re (hrs) Differential revision: https://reviews.freebsd.org/D6731 Notes: svn path=/head/; revision=301961
* kern: for pointers replace 0 with NULL.Pedro F. Giffuni2016-04-151-1/+1
| | | | | | | | | These are mostly cosmetical, no functional change. Found with devel/coccinelle. Notes: svn path=/head/; revision=298069
* Rename P_KTHREAD struct proc p_flag to P_KPROC.Konstantin Belousov2016-02-091-4/+4
| | | | | | | | | | | I left as is an apparent bug in ntoskrnl_var.h:AT_PASSIVE_LEVEL() definition. Suggested by: jhb Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=295435
* fork: pass arguments to fork1 in a dedicated structureMateusz Guzik2016-02-041-2/+6
| | | | | | | Suggested by: kib Notes: svn path=/head/; revision=295232
* Limit rights on process descriptors.Ed Schouten2015-07-311-1/+1
| | | | | | | | | | | | | | | | | | | | | On CloudABI, the rights bits returned by cap_rights_get() match up with the operations that you can actually perform on the file descriptor. Limiting the rights is good, because it makes it easier to get uniform behaviour across different operating systems. If process descriptors on FreeBSD would suddenly gain support for any new file operation, this wouldn't become exposed to CloudABI processes without first extending the rights. Extend fork1() to gain a 'struct filecaps' argument that allows you to construct process descriptors with custom rights. Use this in cloudabi_sys_proc_fork() to limit the rights to just fstat() and pdwait(). Obtained from: https://github.com/NuxiNL/freebsd Notes: svn path=/head/; revision=286122
* The si_status field of the siginfo_t, provided by the waitid(2) andKonstantin Belousov2015-07-181-1/+1
| | | | | | | | | | | | | | | | | | | SIGCHLD signal, should keep full 32 bits of the status passed to the _exit(2). Split the combined p_xstat of the struct proc into the separate exit status p_xexit for normal process exit, and signalled termination information p_xsig. Kernel-visible macro KW_EXITCODE() reconstructs old p_xstat from p_xexit and p_xsig. p_xexit contains complete status and copied out into si_status. Requested by: Joerg Schilling Reviewed by: jilles (previous version), pho Tested by: pho Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=285670
* Ansify another function. This is the last in the file, I hope.Mark Murray2015-06-281-2/+1
| | | | Notes: svn path=/head/; revision=284910
* ANSIfy the only function that uses K&R definition in this file.Mark Murray2015-06-281-2/+1
| | | | Notes: svn path=/head/; revision=284909
* Generalised support for copy-on-write structures shared by threads.Mateusz Guzik2015-06-101-1/+1
| | | | | | | | | | | | Thread credentials are maintained as follows: each thread has a pointer to creds and a reference on them. The pointer is compared with proc's creds on userspace<->kernel boundary and updated if needed. This patch introduces a counter which can be compared instead, so that more structures can use this scheme without adding more comparisons on the boundary. Notes: svn path=/head/; revision=284214
* The umtx_lock mutex is used by top-half of the kernel, but isKonstantin Belousov2015-02-281-0/+2
| | | | | | | | | | | | | | | | | | | | currently a spin lock. Apparently, the only reason for this is that umtx_thread_exit() is called under the process spinlock, which put the requirement on the umtx_lock. Note that the witness static order list is wrong for the umtx_lock, umtx_lock is explicitely before any thread lock, so it is also before sleepq locks. Change umtx_lock to be the sleepable mutex. For the reason above, the calls to umtx_thread_exit() are moved from thread_exit() earlier in each caller, when the process spin lock is not yet taken. Discussed with: jhb Tested by: pho (previous version) Sponsored by: The FreeBSD Foundation MFC after: 3 weeks Notes: svn path=/head/; revision=279390
* Do not use potentially stale thread in kthread_add()Bryan Venteicher2013-08-171-6/+4
| | | | | | | | | | | | When an existing process is provided, the thread selected to use to initialize the new thread could have exited and be reaped. Acquire the proc lock earlier to ensure the thread remains valid. Reviewed by: jhb, julian (previous version) MFC after: 3 days Notes: svn path=/head/; revision=254457
* Stop treating td_sigmask specially for the purposes of new threadKonstantin Belousov2012-05-261-2/+0
| | | | | | | | | | | | creation. Move it into the copied region of the struct thread. Update some comments. Requested by: bde X-MFC after: never Notes: svn path=/head/; revision=236117
* Revert previous revision, misunderstood the code :(Xin LI2012-05-111-1/+0
| | | | Notes: svn path=/head/; revision=235301
* Release proc lock after setting signal queue.Xin LI2012-05-111-0/+1
| | | | | | | | | PR: kern/167727 Submitted by: Jinjun Gao <gjinjun gmail com> MFC after: 2 weeks Notes: svn path=/head/; revision=235300
* Add a new sched_clear_name() method to the scheduler interface to clearJohn Baldwin2012-03-081-0/+6
| | | | | | | | | | | the cached name used for KTR_SCHED traces when a thread's name changes. This way KTR_SCHED traces (and thus schedgraph) will notice when a thread's name changes, most commonly via execve(). MFC after: 2 weeks Notes: svn path=/head/; revision=232700
* Whenever a new kernel thread is spawned, explicitly clear any CPU affinityRyan Stone2012-02-041-0/+7
| | | | | | | | | | | | set on the new thread. This prevents the thread from inadvertently inheriting affinity from a random sibling. Submitted by: attilio Tested by: pho MFC after: 1 week Notes: svn path=/head/; revision=230984
* Add experimental support for process descriptorsJonathan Anderson2011-08-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | A "process descriptor" file descriptor is used to manage processes without using the PID namespace. This is required for Capsicum's Capability Mode, where the PID namespace is unavailable. New system calls pdfork(2) and pdkill(2) offer the functional equivalents of fork(2) and kill(2). pdgetpid(2) allows querying the PID of the remote process for debugging purposes. The currently-unimplemented pdwait(2) will, in the future, allow querying rusage/exit status. In the interim, poll(2) may be used to check (and wait for) process termination. When a process is referenced by a process descriptor, it does not issue SIGCHLD to the parent, making it suitable for use in libraries---a common scenario when using library compartmentalisation from within large applications (such as web browsers). Some observers may note a similarity to Mach task ports; process descriptors provide a subset of this behaviour, but in a UNIX style. This feature is enabled by "options PROCDESC", but as with several other Capsicum kernel features, is not enabled by default in GENERIC 9.0. Reviewed by: jhb, kib Approved by: re (kib), mentor (rwatson) Sponsored by: Google Inc Notes: svn path=/head/; revision=224987
* - Properly initialize the base priority (td_base_pri) of thread0 to PVMJohn Baldwin2011-01-061-4/+5
| | | | | | | | | | | | | to match the desired priority in td_priority. Otherwise the first time thread0 used a borrowed priority it would drop down to PUSER instead of PVM. - Explicitly initialize the starting priority of new kprocs to PVM to avoid inheriting some random priority from thread0. MFC after: 2 weeks Notes: svn path=/head/; revision=217079
* kproc_exit() is already marked __dead2 so a NOTREACHED comment here isn'tJohn Baldwin2011-01-041-1/+0
| | | | | | | | | needed for lint. Submitted by: bde Notes: svn path=/head/; revision=216954
* Small whitespace nits and add a comment explaining why kthread_exit() canJohn Baldwin2011-01-031-3/+6
| | | | | | | call kproc_exit() that was lost earlier. Notes: svn path=/head/; revision=216921
* In thr_exit() and kthread_exit(), only remove thread fromDavid Xu2010-10-231-1/+5
| | | | | | | | | hash if it can directly exit, otherwise let exit1() do it. The change should be in r213950, but for unknown reason, it was lost. Notes: svn path=/head/; revision=214238
* Create a global thread hash table to speed up thread lookup, useDavid Xu2010-10-091-0/+3
| | | | | | | | | | | | | rwlock to protect the table. In old code, thread lookup is done with process lock held, to find a thread, kernel has to iterate through process and thread list, this is quite inefficient. With this change, test shows in extreme case performance is dramatically improved. Earlier patch was reviewed by: jhb, julian Notes: svn path=/head/; revision=213642
* Allow a const char * to be passed as the process name to kproc_kthread_add()John Baldwin2010-05-211-1/+1
| | | | | | | | | without generating a warning. MFC after: 1 month Notes: svn path=/head/; revision=208390
* Use the cached value within comparison.Attilio Rao2010-02-191-1/+1
| | | | | | | Submitted by: jhb Notes: svn path=/head/; revision=204089
* Fix the grammar.Attilio Rao2010-02-191-4/+4
| | | | | | | Submitted by: Brandon Gooch <bgooch at se dot edu> Notes: svn path=/head/; revision=204088
* Fix a race in regard of p_numthreads.Attilio Rao2010-02-191-9/+8
| | | | | | | | Submitted by: Giovanni Trematerra <giovanni dot trematerra at gmail dot com> Notes: svn path=/head/; revision=204087