From 939deb433656eadcd7e74873a32a38411367cdc8 Mon Sep 17 00:00:00 2001 From: dchagin Date: Sat, 9 Jan 2016 14:08:10 +0000 Subject: To facillitate an upcoming Linuxulator merging partially MFC r275121 (by kib). Only merge the syntax changes from r275121, PROC_*LOCK() macros still lock the same proc spinlock. The process spin lock currently has the following distinct uses: - Threads lifetime cycle, in particular, counting of the threads in the process, and interlocking with process mutex and thread lock. The main reason of this is that turnstile locks are after thread locks, so you e.g. cannot unlock blockable mutex (think process mutex) while owning thread lock. - Virtual and profiling itimers, since the timers activation is done from the clock interrupt context. Replace the p_slock by p_itimmtx and PROC_ITIMLOCK(). - Profiling code (profil(2)), for similar reason. Replace the p_slock by p_profmtx and PROC_PROFLOCK(). - Resource usage accounting. Need for the spinlock there is subtle, my understanding is that spinlock blocks context switching for the current thread, which prevents td_runtime and similar fields from changing (updates are done at the mi_switch()). Replace the p_slock by p_statmtx and PROC_STATLOCK(). Discussed with: kib --- sys/kern/kern_thread.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'sys/kern/kern_thread.c') diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index 5dc2bb0..e5ec1fb 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -472,6 +472,9 @@ thread_exit(void) PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); #endif PROC_UNLOCK(p); + PROC_STATLOCK(p); + thread_lock(td); + PROC_SUNLOCK(p); /* Do the same timestamp bookkeeping that mi_switch() would do. */ new_switchtime = cpu_ticks(); @@ -486,9 +489,8 @@ thread_exit(void) td->td_ru.ru_nvcsw++; ruxagg(p, td); rucollect(&p->p_ru, &td->td_ru); + PROC_STATUNLOCK(p); - thread_lock(td); - PROC_SUNLOCK(p); td->td_state = TDS_INACTIVE; #ifdef WITNESS witness_thread_exit(td); -- cgit v1.1 From 2b83b41438a0a93bb81e1f0dcccb0c00f296acad Mon Sep 17 00:00:00 2001 From: dchagin Date: Sat, 9 Jan 2016 14:53:08 +0000 Subject: MFC r283382: In preparation for switching linuxulator to the use the native 1:1 threads add a hook for cleaning thread resources before the thread die. --- sys/kern/kern_thread.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'sys/kern/kern_thread.c') diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index e5ec1fb..96f68609ca 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -46,6 +46,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -885,6 +886,14 @@ thread_suspend_check(int return_instead) if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { PROC_UNLOCK(p); tidhash_remove(td); + + /* + * Allow Linux emulation layer to do some work + * before thread suicide. + */ + if (__predict_false(p->p_sysent->sv_thread_detach != NULL)) + (p->p_sysent->sv_thread_detach)(td); + PROC_LOCK(p); tdsigcleanup(td); umtx_thread_exit(td); -- cgit v1.1