2011-02-12 18:17:12 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2004 Tim J. Robbins
|
|
|
|
* Copyright (c) 2002 Doug Rabson
|
|
|
|
* Copyright (c) 2000 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include "opt_compat.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/imgact.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <sys/ktr.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <sys/racct.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
#include <sys/sched.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
#include <sys/sx.h>
|
Add implementation of robust mutexes, hopefully close enough to the
intention of the POSIX IEEE Std 1003.1TM-2008/Cor 1-2013.
A robust mutex is guaranteed to be cleared by the system upon either
thread or process owner termination while the mutex is held. The next
mutex locker is then notified about inconsistent mutex state and can
execute (or abandon) corrective actions.
The patch mostly consists of small changes here and there, adding
neccessary checks for the inconsistent and abandoned conditions into
existing paths. Additionally, the thread exit handler was extended to
iterate over the userspace-maintained list of owned robust mutexes,
unlocking and marking as terminated each of them.
The list of owned robust mutexes cannot be maintained atomically
synchronous with the mutex lock state (it is possible in kernel, but
is too expensive). Instead, for the duration of lock or unlock
operation, the current mutex is remembered in a special slot that is
also checked by the kernel at thread termination.
Kernel must be aware about the per-thread location of the heads of
robust mutex lists and the current active mutex slot. When a thread
touches a robust mutex for the first time, a new umtx op syscall is
issued which informs about location of lists heads.
The umtx sleep queues for PP and PI mutexes are split between
non-robust and robust.
Somewhat unrelated changes in the patch:
1. Style.
2. The fix for proper tdfind() call use in umtxq_sleep_pi() for shared
pi mutexes.
3. Removal of the userspace struct pthread_mutex m_owner field.
4. The sysctl kern.ipc.umtx_vnode_persistent is added, which controls
the lifetime of the shared mutex associated with a vnode' page.
Reviewed by: jilles (previous version, supposedly the objection was fixed)
Discussed with: brooks, Martin Simmons <martin@lispworks.com> (some aspects)
Tested by: pho
Sponsored by: The FreeBSD Foundation
2016-05-17 09:56:22 +00:00
|
|
|
#include <sys/umtx.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
#include <sys/unistd.h>
|
2015-05-24 14:33:19 +00:00
|
|
|
#include <sys/wait.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
|
2011-02-12 18:17:12 +00:00
|
|
|
#ifdef COMPAT_LINUX32
|
|
|
|
#include <machine/../linux32/linux.h>
|
|
|
|
#include <machine/../linux32/linux32_proto.h>
|
|
|
|
#else
|
|
|
|
#include <machine/../linux/linux.h>
|
|
|
|
#include <machine/../linux/linux_proto.h>
|
|
|
|
#endif
|
|
|
|
#include <compat/linux/linux_emul.h>
|
2015-05-24 15:54:58 +00:00
|
|
|
#include <compat/linux/linux_futex.h>
|
2013-01-29 18:41:30 +00:00
|
|
|
#include <compat/linux/linux_misc.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <compat/linux/linux_util.h>
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
linux_fork(struct thread *td, struct linux_fork_args *args)
|
|
|
|
{
|
2016-02-04 04:22:18 +00:00
|
|
|
struct fork_req fr;
|
2011-02-12 18:17:12 +00:00
|
|
|
int error;
|
|
|
|
struct proc *p2;
|
|
|
|
struct thread *td2;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(fork))
|
|
|
|
printf(ARGS(fork, ""));
|
|
|
|
#endif
|
|
|
|
|
2016-02-04 04:22:18 +00:00
|
|
|
bzero(&fr, sizeof(fr));
|
|
|
|
fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
|
|
|
|
fr.fr_procp = &p2;
|
|
|
|
if ((error = fork1(td, &fr)) != 0)
|
2011-02-12 18:17:12 +00:00
|
|
|
return (error);
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
td2 = FIRST_THREAD_IN_PROC(p2);
|
2011-02-12 18:17:12 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
linux_proc_init(td, td2, 0);
|
2011-02-12 18:17:12 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_retval[0] = p2->p_pid;
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make this runnable after we are finished with it.
|
|
|
|
*/
|
|
|
|
thread_lock(td2);
|
|
|
|
TD_SET_CAN_RUN(td2);
|
|
|
|
sched_add(td2, SRQ_BORING);
|
|
|
|
thread_unlock(td2);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_vfork(struct thread *td, struct linux_vfork_args *args)
|
|
|
|
{
|
2016-02-04 04:22:18 +00:00
|
|
|
struct fork_req fr;
|
2011-02-12 18:17:12 +00:00
|
|
|
int error;
|
|
|
|
struct proc *p2;
|
|
|
|
struct thread *td2;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(vfork))
|
|
|
|
printf(ARGS(vfork, ""));
|
|
|
|
#endif
|
|
|
|
|
2016-02-04 04:22:18 +00:00
|
|
|
bzero(&fr, sizeof(fr));
|
|
|
|
fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
|
|
|
|
fr.fr_procp = &p2;
|
|
|
|
if ((error = fork1(td, &fr)) != 0)
|
2011-02-12 18:17:12 +00:00
|
|
|
return (error);
|
2011-02-12 20:16:25 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
td2 = FIRST_THREAD_IN_PROC(p2);
|
|
|
|
|
|
|
|
linux_proc_init(td, td2, 0);
|
2011-02-12 18:17:12 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_retval[0] = p2->p_pid;
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make this runnable after we are finished with it.
|
|
|
|
*/
|
|
|
|
thread_lock(td2);
|
|
|
|
TD_SET_CAN_RUN(td2);
|
|
|
|
sched_add(td2, SRQ_BORING);
|
|
|
|
thread_unlock(td2);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
static int
|
|
|
|
linux_clone_proc(struct thread *td, struct linux_clone_args *args)
|
2011-02-12 18:17:12 +00:00
|
|
|
{
|
2016-02-04 04:22:18 +00:00
|
|
|
struct fork_req fr;
|
2011-02-12 18:17:12 +00:00
|
|
|
int error, ff = RFPROC | RFSTOPPED;
|
|
|
|
struct proc *p2;
|
|
|
|
struct thread *td2;
|
|
|
|
int exit_signal;
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(clone)) {
|
|
|
|
printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
|
|
|
|
"child tid: %p"), (unsigned)args->flags,
|
|
|
|
args->stack, args->parent_tidptr, args->child_tidptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit_signal = args->flags & 0x000000ff;
|
|
|
|
if (LINUX_SIG_VALID(exit_signal)) {
|
2015-05-24 17:47:20 +00:00
|
|
|
exit_signal = linux_to_bsd_signal(exit_signal);
|
2011-02-12 18:17:12 +00:00
|
|
|
} else if (exit_signal != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_VM)
|
|
|
|
ff |= RFMEM;
|
|
|
|
if (args->flags & LINUX_CLONE_SIGHAND)
|
|
|
|
ff |= RFSIGSHARE;
|
|
|
|
/*
|
|
|
|
* XXX: In Linux, sharing of fs info (chroot/cwd/umask)
|
2016-04-30 00:53:10 +00:00
|
|
|
* and open files is independent. In FreeBSD, its in one
|
2011-02-12 18:17:12 +00:00
|
|
|
* structure but in reality it does not cause any problems
|
|
|
|
* because both of these flags are usually set together.
|
|
|
|
*/
|
|
|
|
if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
|
|
|
|
ff |= RFFDG;
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT_SETTID)
|
|
|
|
if (args->parent_tidptr == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2015-10-07 19:10:38 +00:00
|
|
|
if (args->flags & LINUX_CLONE_VFORK)
|
|
|
|
ff |= RFPPWAIT;
|
|
|
|
|
2016-02-04 04:22:18 +00:00
|
|
|
bzero(&fr, sizeof(fr));
|
|
|
|
fr.fr_flags = ff;
|
|
|
|
fr.fr_procp = &p2;
|
|
|
|
error = fork1(td, &fr);
|
2011-02-12 18:17:12 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
td2 = FIRST_THREAD_IN_PROC(p2);
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
/* create the emuldata */
|
2015-05-24 14:53:16 +00:00
|
|
|
linux_proc_init(td, td2, args->flags);
|
|
|
|
|
|
|
|
em = em_find(td2);
|
|
|
|
KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_CHILD_SETTID)
|
|
|
|
em->child_set_tid = args->child_tidptr;
|
|
|
|
else
|
|
|
|
em->child_set_tid = NULL;
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
|
|
|
|
em->child_clear_tid = args->child_tidptr;
|
|
|
|
else
|
|
|
|
em->child_clear_tid = NULL;
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT_SETTID) {
|
|
|
|
error = copyout(&p2->p_pid, args->parent_tidptr,
|
|
|
|
sizeof(p2->p_pid));
|
|
|
|
if (error)
|
|
|
|
printf(LMSG("copyout failed!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
PROC_LOCK(p2);
|
|
|
|
p2->p_sigparent = exit_signal;
|
|
|
|
PROC_UNLOCK(p2);
|
|
|
|
/*
|
|
|
|
* In a case of stack = NULL, we are supposed to COW calling process
|
|
|
|
* stack. This is what normal fork() does, so we just keep tf_rsp arg
|
|
|
|
* intact.
|
|
|
|
*/
|
2015-05-24 14:53:16 +00:00
|
|
|
linux_set_upcall_kse(td2, PTROUT(args->stack));
|
2011-02-12 18:17:12 +00:00
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_SETTLS)
|
|
|
|
linux_set_cloned_tls(td2, args->tls);
|
|
|
|
|
2016-03-08 15:08:22 +00:00
|
|
|
/*
|
|
|
|
* If CLONE_PARENT is set, then the parent of the new process will be
|
|
|
|
* the same as that of the calling process.
|
|
|
|
*/
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT) {
|
|
|
|
sx_xlock(&proctree_lock);
|
|
|
|
PROC_LOCK(p2);
|
|
|
|
proc_reparent(p2, td->td_proc->p_pptr);
|
|
|
|
PROC_UNLOCK(p2);
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:17:12 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(clone))
|
|
|
|
printf(LMSG("clone: successful rfork to %d, "
|
|
|
|
"stack %p sig = %d"), (int)p2->p_pid, args->stack,
|
|
|
|
exit_signal);
|
|
|
|
#endif
|
2015-05-24 14:53:16 +00:00
|
|
|
|
2011-02-12 18:17:12 +00:00
|
|
|
/*
|
|
|
|
* Make this runnable after we are finished with it.
|
|
|
|
*/
|
|
|
|
thread_lock(td2);
|
|
|
|
TD_SET_CAN_RUN(td2);
|
|
|
|
sched_add(td2, SRQ_BORING);
|
|
|
|
thread_unlock(td2);
|
|
|
|
|
|
|
|
td->td_retval[0] = p2->p_pid;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2015-05-24 14:33:19 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
static int
|
|
|
|
linux_clone_thread(struct thread *td, struct linux_clone_args *args)
|
2015-05-24 14:33:19 +00:00
|
|
|
{
|
2015-05-24 14:53:16 +00:00
|
|
|
struct linux_emuldata *em;
|
|
|
|
struct thread *newtd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(clone)) {
|
|
|
|
printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
|
|
|
|
"child tid: %p"), (unsigned)args->flags,
|
|
|
|
args->stack, args->parent_tidptr, args->child_tidptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_tid, (unsigned)args->flags,
|
|
|
|
args->parent_tidptr, args->child_tidptr);
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT_SETTID)
|
|
|
|
if (args->parent_tidptr == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Threads should be created with own stack */
|
|
|
|
if (args->stack == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
|
2015-09-02 14:04:13 +00:00
|
|
|
#ifdef RACCT
|
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
error = racct_add(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error != 0)
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
/* Initialize our td */
|
|
|
|
error = kern_thr_alloc(p, 0, &newtd);
|
|
|
|
if (error)
|
2015-09-02 14:04:13 +00:00
|
|
|
goto fail;
|
2016-06-16 12:05:44 +00:00
|
|
|
|
|
|
|
cpu_copy_thread(newtd, td);
|
2015-05-24 14:53:16 +00:00
|
|
|
|
|
|
|
bzero(&newtd->td_startzero,
|
|
|
|
__rangeof(struct thread, td_startzero, td_endzero));
|
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
|
|
|
__rangeof(struct thread, td_startcopy, td_endcopy));
|
|
|
|
|
|
|
|
newtd->td_proc = p;
|
2015-06-10 15:34:43 +00:00
|
|
|
thread_cow_get(newtd, td);
|
2015-05-24 14:53:16 +00:00
|
|
|
|
|
|
|
/* create the emuldata */
|
|
|
|
linux_proc_init(td, newtd, args->flags);
|
|
|
|
|
|
|
|
em = em_find(newtd);
|
|
|
|
KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_SETTLS)
|
|
|
|
linux_set_cloned_tls(newtd, args->tls);
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_CHILD_SETTID)
|
|
|
|
em->child_set_tid = args->child_tidptr;
|
|
|
|
else
|
|
|
|
em->child_set_tid = NULL;
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
|
|
|
|
em->child_clear_tid = args->child_tidptr;
|
|
|
|
else
|
|
|
|
em->child_clear_tid = NULL;
|
|
|
|
|
|
|
|
cpu_thread_clean(newtd);
|
|
|
|
|
|
|
|
linux_set_upcall_kse(newtd, PTROUT(args->stack));
|
|
|
|
|
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_flag |= P_HADTHREADS;
|
|
|
|
bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT)
|
|
|
|
thread_link(newtd, p->p_pptr);
|
|
|
|
else
|
|
|
|
thread_link(newtd, p);
|
|
|
|
|
|
|
|
thread_lock(td);
|
|
|
|
/* let the scheduler know about these things. */
|
|
|
|
sched_fork_thread(td, newtd);
|
|
|
|
thread_unlock(td);
|
|
|
|
if (P_SHOULDSTOP(p))
|
|
|
|
newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
|
|
|
|
tidhash_add(newtd);
|
2015-05-24 14:33:19 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-05-24 14:53:16 +00:00
|
|
|
if (ldebug(clone))
|
|
|
|
printf(ARGS(clone, "successful clone to %d, stack %p"),
|
|
|
|
(int)newtd->td_tid, args->stack);
|
2015-05-24 14:33:19 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_tid, newtd->td_tid);
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_PARENT_SETTID) {
|
|
|
|
error = copyout(&newtd->td_tid, args->parent_tidptr,
|
|
|
|
sizeof(newtd->td_tid));
|
|
|
|
if (error)
|
|
|
|
printf(LMSG("clone_thread: copyout failed!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make this runnable after we are finished with it.
|
|
|
|
*/
|
|
|
|
thread_lock(newtd);
|
|
|
|
TD_SET_CAN_RUN(newtd);
|
|
|
|
sched_add(newtd, SRQ_BORING);
|
|
|
|
thread_unlock(newtd);
|
|
|
|
|
|
|
|
td->td_retval[0] = newtd->td_tid;
|
|
|
|
|
|
|
|
return (0);
|
2015-09-02 14:04:13 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
#ifdef RACCT
|
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
racct_sub(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (error);
|
2015-05-24 14:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_clone(struct thread *td, struct linux_clone_args *args)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (args->flags & LINUX_CLONE_THREAD)
|
|
|
|
return (linux_clone_thread(td, args));
|
|
|
|
else
|
|
|
|
return (linux_clone_proc(td, args));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_exit(struct thread *td, struct linux_exit_args *args)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
|
|
|
em = em_find(td);
|
|
|
|
KASSERT(em != NULL, ("exit: emuldata not found.\n"));
|
|
|
|
|
|
|
|
LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
|
|
|
|
|
Add implementation of robust mutexes, hopefully close enough to the
intention of the POSIX IEEE Std 1003.1TM-2008/Cor 1-2013.
A robust mutex is guaranteed to be cleared by the system upon either
thread or process owner termination while the mutex is held. The next
mutex locker is then notified about inconsistent mutex state and can
execute (or abandon) corrective actions.
The patch mostly consists of small changes here and there, adding
neccessary checks for the inconsistent and abandoned conditions into
existing paths. Additionally, the thread exit handler was extended to
iterate over the userspace-maintained list of owned robust mutexes,
unlocking and marking as terminated each of them.
The list of owned robust mutexes cannot be maintained atomically
synchronous with the mutex lock state (it is possible in kernel, but
is too expensive). Instead, for the duration of lock or unlock
operation, the current mutex is remembered in a special slot that is
also checked by the kernel at thread termination.
Kernel must be aware about the per-thread location of the heads of
robust mutex lists and the current active mutex slot. When a thread
touches a robust mutex for the first time, a new umtx op syscall is
issued which informs about location of lists heads.
The umtx sleep queues for PP and PI mutexes are split between
non-robust and robust.
Somewhat unrelated changes in the patch:
1. Style.
2. The fix for proper tdfind() call use in umtxq_sleep_pi() for shared
pi mutexes.
3. Removal of the userspace struct pthread_mutex m_owner field.
4. The sysctl kern.ipc.umtx_vnode_persistent is added, which controls
the lifetime of the shared mutex associated with a vnode' page.
Reviewed by: jilles (previous version, supposedly the objection was fixed)
Discussed with: brooks, Martin Simmons <martin@lispworks.com> (some aspects)
Tested by: pho
Sponsored by: The FreeBSD Foundation
2016-05-17 09:56:22 +00:00
|
|
|
umtx_thread_exit(td);
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
linux_thread_detach(td);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX. When the last two threads of a process
|
|
|
|
* exit via pthread_exit() try thr_exit() first.
|
|
|
|
*/
|
|
|
|
kern_thr_exit(td);
|
2015-07-18 09:02:50 +00:00
|
|
|
exit1(td, args->rval, 0);
|
2015-05-24 14:33:19 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2015-05-24 15:54:58 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
|
|
|
em = em_find(td);
|
|
|
|
KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
|
|
|
|
|
|
|
|
em->child_clear_tid = args->tidptr;
|
|
|
|
|
|
|
|
td->td_retval[0] = em->em_tid;
|
|
|
|
|
|
|
|
LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
|
|
|
|
em->em_tid, args->tidptr, td->td_retval[0]);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
linux_thread_detach(struct thread *td)
|
|
|
|
{
|
|
|
|
struct linux_sys_futex_args cup;
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
int *child_clear_tid;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
em = em_find(td);
|
|
|
|
KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
|
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
|
2015-05-24 15:54:58 +00:00
|
|
|
|
|
|
|
release_futexes(td, em);
|
|
|
|
|
|
|
|
child_clear_tid = em->child_clear_tid;
|
|
|
|
|
|
|
|
if (child_clear_tid != NULL) {
|
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR2(thread_detach, "thread(%d) %p",
|
2015-05-24 15:54:58 +00:00
|
|
|
em->em_tid, child_clear_tid);
|
|
|
|
|
|
|
|
error = suword32(child_clear_tid, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cup.uaddr = child_clear_tid;
|
|
|
|
cup.op = LINUX_FUTEX_WAKE;
|
|
|
|
cup.val = 1; /* wake one */
|
|
|
|
cup.timeout = NULL;
|
|
|
|
cup.uaddr2 = NULL;
|
|
|
|
cup.val3 = 0;
|
|
|
|
error = linux_sys_futex(td, &cup);
|
|
|
|
/*
|
|
|
|
* this cannot happen at the moment and if this happens it
|
|
|
|
* probably means there is a user space bug
|
|
|
|
*/
|
|
|
|
if (error != 0)
|
|
|
|
linux_msg(td, "futex stuff in thread_detach failed.");
|
|
|
|
}
|
|
|
|
}
|