2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2017-11-27 15:20:12 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2003-04-01 00:30:30 +00:00
|
|
|
* Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* 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 unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
#include "opt_compat.h"
|
2006-07-13 06:26:43 +00:00
|
|
|
#include "opt_posix.h"
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/proc.h>
|
2006-11-11 16:46:31 +00:00
|
|
|
#include <sys/posix4.h>
|
2016-07-15 15:32:09 +00:00
|
|
|
#include <sys/ptrace.h>
|
2011-03-31 19:22:11 +00:00
|
|
|
#include <sys/racct.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2010-10-23 13:16:39 +00:00
|
|
|
#include <sys/rwlock.h>
|
2003-04-11 19:24:37 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-07 06:33:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#include <sys/smp.h>
|
2007-06-07 19:45:19 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/signalvar.h>
|
2011-02-23 13:50:24 +00:00
|
|
|
#include <sys/sysctl.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
2006-07-11 05:34:35 +00:00
|
|
|
#include <sys/rtprio.h>
|
2005-10-26 06:55:46 +00:00
|
|
|
#include <sys/umtx.h>
|
|
|
|
#include <sys/limits.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
#include <machine/frame.h>
|
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
#include <security/audit/audit.h>
|
|
|
|
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
|
|
|
|
"thread allocation");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
|
|
|
static int max_threads_per_proc = 1500;
|
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
|
2011-12-13 00:38:50 +00:00
|
|
|
&max_threads_per_proc, 0, "Limit on threads per proc");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
|
|
|
static int max_threads_hits;
|
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
|
2011-12-13 00:38:50 +00:00
|
|
|
&max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2006-09-22 15:04:28 +00:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
suword_lwpid(void *addr, lwpid_t lwpid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-11-22 12:36:15 +00:00
|
|
|
if (SV_CURPROC_FLAG(SV_LP64))
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword(addr, lwpid);
|
|
|
|
else
|
|
|
|
error = suword32(addr, lwpid);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define suword_lwpid suword
|
|
|
|
#endif
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* System call interface.
|
|
|
|
*/
|
2015-07-20 10:20:04 +00:00
|
|
|
|
|
|
|
struct thr_create_initthr_args {
|
|
|
|
ucontext_t ctx;
|
|
|
|
long *tid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
thr_create_initthr(struct thread *td, void *thunk)
|
|
|
|
{
|
|
|
|
struct thr_create_initthr_args *args;
|
|
|
|
|
|
|
|
/* Copy out the child tid. */
|
|
|
|
args = thunk;
|
|
|
|
if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
|
|
|
|
return (EFAULT);
|
|
|
|
|
|
|
|
return (set_mcontext(td, &args->ctx.uc_mcontext));
|
|
|
|
}
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_create(struct thread *td, struct thr_create_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* ucontext_t *ctx, long *id, int flags */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2015-07-20 10:20:04 +00:00
|
|
|
struct thr_create_initthr_args args;
|
2003-04-01 00:30:30 +00:00
|
|
|
int error;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2015-07-20 10:20:04 +00:00
|
|
|
if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
2015-07-20 10:20:04 +00:00
|
|
|
args.tid = uap->id;
|
|
|
|
return (thread_create(td, NULL, thr_create_initthr, &args));
|
2005-04-23 02:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_new(struct thread *td, struct thr_new_args *uap)
|
2005-04-23 02:36:07 +00:00
|
|
|
/* struct thr_param * */
|
|
|
|
{
|
|
|
|
struct thr_param param;
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
if (uap->param_size < 0 || uap->param_size > sizeof(param))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (EINVAL);
|
2006-09-21 04:18:46 +00:00
|
|
|
bzero(¶m, sizeof(param));
|
2006-09-22 15:04:28 +00:00
|
|
|
if ((error = copyin(uap->param, ¶m, uap->param_size)))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
return (kern_thr_new(td, ¶m));
|
|
|
|
}
|
|
|
|
|
2015-07-20 10:20:04 +00:00
|
|
|
static int
|
|
|
|
thr_new_initthr(struct thread *td, void *thunk)
|
|
|
|
{
|
|
|
|
stack_t stack;
|
|
|
|
struct thr_param *param;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we copy out tid to two places, one for child and one
|
|
|
|
* for parent, because pthread can create a detached thread,
|
|
|
|
* if parent wants to safely access child tid, it has to provide
|
|
|
|
* its storage, because child thread may exit quickly and
|
|
|
|
* memory is freed before parent thread can access it.
|
|
|
|
*/
|
|
|
|
param = thunk;
|
|
|
|
if ((param->child_tid != NULL &&
|
|
|
|
suword_lwpid(param->child_tid, td->td_tid)) ||
|
|
|
|
(param->parent_tid != NULL &&
|
|
|
|
suword_lwpid(param->parent_tid, td->td_tid)))
|
|
|
|
return (EFAULT);
|
|
|
|
|
|
|
|
/* Set up our machine context. */
|
|
|
|
stack.ss_sp = param->stack_base;
|
|
|
|
stack.ss_size = param->stack_size;
|
|
|
|
/* Set upcall address to user thread entry function. */
|
2016-06-16 12:05:44 +00:00
|
|
|
cpu_set_upcall(td, param->start_func, param->arg, &stack);
|
2015-07-20 10:20:04 +00:00
|
|
|
/* Setup user TLS address and TLS pointer register. */
|
|
|
|
return (cpu_set_user_tls(td, param->tls_base));
|
|
|
|
}
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
int
|
|
|
|
kern_thr_new(struct thread *td, struct thr_param *param)
|
|
|
|
{
|
|
|
|
struct rtprio rtp, *rtpp;
|
|
|
|
int error;
|
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = NULL;
|
2006-09-22 15:04:28 +00:00
|
|
|
if (param->rtp != 0) {
|
|
|
|
error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
|
2008-10-13 21:04:52 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = &rtp;
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
2015-07-20 10:20:04 +00:00
|
|
|
return (thread_create(td, rtpp, thr_new_initthr, param));
|
2005-04-23 02:36:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 10:20:04 +00:00
|
|
|
int
|
|
|
|
thread_create(struct thread *td, struct rtprio *rtp,
|
|
|
|
int (*initialize_thread)(struct thread *, void *), void *thunk)
|
2005-04-23 02:36:07 +00:00
|
|
|
{
|
|
|
|
struct thread *newtd;
|
2004-09-05 02:09:54 +00:00
|
|
|
struct proc *p;
|
2006-07-10 23:14:07 +00:00
|
|
|
int error;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
p = td->td_proc;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
switch(rtp->type) {
|
|
|
|
case RTP_PRIO_REALTIME:
|
|
|
|
case RTP_PRIO_FIFO:
|
2006-07-11 08:19:57 +00:00
|
|
|
/* Only root can set scheduler policy */
|
2006-11-06 13:42:10 +00:00
|
|
|
if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EPERM);
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp->prio > RTP_PRIO_MAX)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EINVAL);
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
2006-09-21 04:18:46 +00:00
|
|
|
case RTP_PRIO_NORMAL:
|
|
|
|
rtp->prio = 0;
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 20:06:44 +00:00
|
|
|
#ifdef RACCT
|
2015-04-29 10:23:02 +00:00
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
error = racct_add(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error != 0)
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
2011-07-06 20:06:44 +00:00
|
|
|
#endif
|
2011-03-31 19:22:11 +00:00
|
|
|
|
2006-12-06 06:34:57 +00:00
|
|
|
/* Initialize our td */
|
2015-05-24 14:37:45 +00:00
|
|
|
error = kern_thr_alloc(p, 0, &newtd);
|
|
|
|
if (error)
|
2011-03-31 19:22:11 +00:00
|
|
|
goto fail;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2016-06-16 12:05:44 +00:00
|
|
|
cpu_copy_thread(newtd, td);
|
2011-12-09 17:19:41 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newtd->td_startzero,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startzero, td_endzero));
|
2004-09-05 02:09:54 +00:00
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startcopy, td_endcopy));
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_proc = td->td_proc;
|
2016-06-25 11:31:25 +00:00
|
|
|
newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
|
2015-06-10 10:43:59 +00:00
|
|
|
thread_cow_get(newtd, td);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2015-07-20 10:20:04 +00:00
|
|
|
error = initialize_thread(newtd, thunk);
|
|
|
|
if (error != 0) {
|
|
|
|
thread_cow_free(newtd);
|
|
|
|
thread_free(newtd);
|
|
|
|
goto fail;
|
2004-07-02 00:40:07 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2015-04-26 17:22:59 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_flag |= P_HADTHREADS;
|
|
|
|
thread_link(newtd, p);
|
2007-11-15 06:35:26 +00:00
|
|
|
bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-09-05 02:09:54 +00:00
|
|
|
/* let the scheduler know about these things. */
|
|
|
|
sched_fork_thread(td, newtd);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2008-03-21 08:23:25 +00:00
|
|
|
if (P_SHOULDSTOP(p))
|
|
|
|
newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
|
2016-07-15 15:32:09 +00:00
|
|
|
if (p->p_ptevents & PTRACE_LWP)
|
2015-12-29 23:25:26 +00:00
|
|
|
newtd->td_dbgflags |= TDB_BORN;
|
Add an initial NUMA affinity/policy configuration for threads and processes.
This is based on work done by jeff@ and jhb@, as well as the numa.diff
patch that has been circulating when someone asks for first-touch NUMA
on -10 or -11.
* Introduce a simple set of VM policy and iterator types.
* tie the policy types into the vm_phys path for now, mirroring how
the initial first-touch allocation work was enabled.
* add syscalls to control changing thread and process defaults.
* add a global NUMA VM domain policy.
* implement a simple cascade policy order - if a thread policy exists, use it;
if a process policy exists, use it; use the default policy.
* processes inherit policies from their parent processes, threads inherit
policies from their parent threads.
* add a simple tool (numactl) to query and modify default thread/process
policities.
* add documentation for the new syscalls, for numa and for numactl.
* re-enable first touch NUMA again by default, as now policies can be
set in a variety of methods.
This is only relevant for very specific workloads.
This doesn't pretend to be a final NUMA solution.
The previous defaults in -HEAD (with MAXMEMDOM set) can be achieved by
'sysctl vm.default_policy=rr'.
This is only relevant if MAXMEMDOM is set to something other than 1.
Ie, if you're using GENERIC or a modified kernel with non-NUMA, then
this is a glorified no-op for you.
Thank you to Norse Corp for giving me access to rather large
(for FreeBSD!) NUMA machines in order to develop and verify this.
Thank you to Dell for providing me with dual socket sandybridge
and westmere v3 hardware to do NUMA development with.
Thank you to Scott Long at Netflix for providing me with access
to the two-socket, four-domain haswell v3 hardware.
Thank you to Peter Holm for running the stress testing suite
against the NUMA branch during various stages of development!
Tested:
* MIPS (regression testing; non-NUMA)
* i386 (regression testing; non-NUMA GENERIC)
* amd64 (regression testing; non-NUMA GENERIC)
* westmere, 2 socket (thankyou norse!)
* sandy bridge, 2 socket (thankyou dell!)
* ivy bridge, 2 socket (thankyou norse!)
* westmere-EX, 4 socket / 1TB RAM (thankyou norse!)
* haswell, 2 socket (thankyou norse!)
* haswell v3, 2 socket (thankyou dell)
* haswell v3, 2x18 core (thankyou scott long / netflix!)
* Peter Holm ran a stress test suite on this work and found one
issue, but has not been able to verify it (it doesn't look NUMA
related, and he only saw it once over many testing runs.)
* I've tested bhyve instances running in fixed NUMA domains and cpusets;
all seems to work correctly.
Verified:
* intel-pcm - pcm-numa.x and pcm-memory.x, whilst selecting different
NUMA policies for processes under test.
Review:
This was reviewed through phabricator (https://reviews.freebsd.org/D2559)
as well as privately and via emails to freebsd-arch@. The git history
with specific attributes is available at https://github.com/erikarn/freebsd/
in the NUMA branch (https://github.com/erikarn/freebsd/compare/local/adrian_numa_policy).
This has been reviewed by a number of people (stas, rpaulo, kib, ngie,
wblock) but not achieved a clear consensus. My hope is that with further
exposure and testing more functionality can be implemented and evaluated.
Notes:
* The VM doesn't handle unbalanced domains very well, and if you have an overly
unbalanced memory setup whilst under high memory pressure, VM page allocation
may fail leading to a kernel panic. This was a problem in the past, but it's
much more easily triggered now with these tools.
* This work only controls the path through vm_phys; it doesn't yet strongly/predictably
affect contigmalloc, KVA placement, UMA, etc. So, driver placement of memory
isn't really guaranteed in any way. That's next on my plate.
Sponsored by: Norse Corp, Inc.; Dell
2015-07-11 15:21:37 +00:00
|
|
|
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
PROC_UNLOCK(p);
|
2010-10-09 02:50:23 +00:00
|
|
|
|
|
|
|
tidhash_add(newtd);
|
|
|
|
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(newtd);
|
2006-10-26 21:42:22 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
if (!(td->td_pri_class == PRI_TIMESHARE &&
|
|
|
|
rtp->type == RTP_PRIO_NORMAL)) {
|
|
|
|
rtp_to_pri(rtp, newtd);
|
|
|
|
sched_prio(newtd, newtd->td_user_pri);
|
|
|
|
} /* ignore timesharing class */
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
TD_SET_CAN_RUN(newtd);
|
2008-03-23 02:03:06 +00:00
|
|
|
sched_add(newtd, SRQ_BORING);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2009-02-26 15:51:54 +00:00
|
|
|
return (0);
|
2011-03-31 19:22:11 +00:00
|
|
|
|
|
|
|
fail:
|
2011-07-06 20:06:44 +00:00
|
|
|
#ifdef RACCT
|
2015-04-29 10:23:02 +00:00
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
racct_sub(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
2011-07-06 20:06:44 +00:00
|
|
|
#endif
|
2011-03-31 19:22:11 +00:00
|
|
|
return (error);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_self(struct thread *td, struct thr_self_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long *id */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword_lwpid(uap->id, (unsigned)td->td_tid);
|
|
|
|
if (error == -1)
|
|
|
|
return (EFAULT);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
|
2004-10-06 14:23:00 +00:00
|
|
|
/* long *state */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
2004-10-06 14:23:00 +00:00
|
|
|
/* Signal userland that it can free the stack. */
|
2005-10-26 06:55:46 +00:00
|
|
|
if ((void *)uap->state != NULL) {
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(uap->state, 1);
|
2008-04-29 05:48:05 +00:00
|
|
|
kern_umtx_wake(td, uap->state, INT_MAX, 0);
|
2005-10-26 06:55:46 +00:00
|
|
|
}
|
2004-10-06 14:23:00 +00:00
|
|
|
|
2015-05-24 14:36:33 +00:00
|
|
|
return (kern_thr_exit(td));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_exit(struct thread *td)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
2011-03-31 19:22:11 +00:00
|
|
|
|
2015-05-24 14:36:33 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
|
2015-12-29 23:25:26 +00:00
|
|
|
/*
|
|
|
|
* If all of the threads in a process call this routine to
|
|
|
|
* exit (e.g. all threads call pthread_exit()), exactly one
|
|
|
|
* thread should return to the caller to terminate the process
|
|
|
|
* instead of the thread.
|
|
|
|
*
|
|
|
|
* Checking p_numthreads alone is not sufficient since threads
|
|
|
|
* might be committed to terminating while the PROC_LOCK is
|
|
|
|
* dropped in either ptracestop() or while removing this thread
|
|
|
|
* from the tidhash. Instead, the p_pendingexits field holds
|
|
|
|
* the count of threads in either of those states and a thread
|
|
|
|
* is considered the "last" thread if all of the other threads
|
|
|
|
* in a process are already terminating.
|
|
|
|
*/
|
2003-04-01 00:30:30 +00:00
|
|
|
PROC_LOCK(p);
|
2015-12-29 23:25:26 +00:00
|
|
|
if (p->p_numthreads == p->p_pendingexits + 1) {
|
|
|
|
/*
|
|
|
|
* Ignore attempts to shut down last thread in the
|
|
|
|
* proc. This will actually call _exit(2) in the
|
|
|
|
* usermode trampoline when it returns.
|
|
|
|
*/
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (0);
|
2004-09-05 02:09:54 +00:00
|
|
|
}
|
2014-11-03 11:29:08 +00:00
|
|
|
|
2015-12-29 23:25:26 +00:00
|
|
|
p->p_pendingexits++;
|
|
|
|
td->td_dbgflags |= TDB_EXIT;
|
2016-07-15 15:32:09 +00:00
|
|
|
if (p->p_ptevents & PTRACE_LWP)
|
2017-02-20 15:53:16 +00:00
|
|
|
ptracestop(td, SIGTRAP, NULL);
|
2015-12-29 23:25:26 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
tidhash_remove(td);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_pendingexits--;
|
|
|
|
|
2014-11-03 11:29:08 +00:00
|
|
|
/*
|
2015-12-29 23:25:26 +00:00
|
|
|
* The check above should prevent all other threads from this
|
|
|
|
* process from exiting while the PROC_LOCK is dropped, so
|
|
|
|
* there must be at least one other thread other than the
|
|
|
|
* current thread.
|
2014-11-03 11:29:08 +00:00
|
|
|
*/
|
2015-12-29 23:25:26 +00:00
|
|
|
KASSERT(p->p_numthreads > 1, ("too few threads"));
|
|
|
|
racct_sub(p, RACCT_NTHR, 1);
|
|
|
|
tdsigcleanup(td);
|
|
|
|
PROC_SLOCK(p);
|
|
|
|
thread_stopped(p);
|
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id, int sig */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_t ksi;
|
2003-04-01 00:30:30 +00:00
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_init(&ksi);
|
|
|
|
ksi.ksi_signo = uap->sig;
|
2010-08-24 07:22:24 +00:00
|
|
|
ksi.ksi_code = SI_LWP;
|
2010-03-01 14:27:16 +00:00
|
|
|
ksi.ksi_pid = p->p_pid;
|
|
|
|
ksi.ksi_uid = td->td_ucred->cr_ruid;
|
2006-01-07 03:15:21 +00:00
|
|
|
if (uap->id == -1) {
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_LOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
2010-06-29 20:41:52 +00:00
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2006-01-07 03:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-09 02:50:23 +00:00
|
|
|
error = 0;
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
2006-01-07 03:15:21 +00:00
|
|
|
if (ttd == NULL)
|
2010-10-09 02:50:23 +00:00
|
|
|
return (ESRCH);
|
|
|
|
if (uap->sig == 0)
|
2006-01-07 03:15:21 +00:00
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
2010-10-09 02:50:23 +00:00
|
|
|
else
|
2010-06-29 20:41:52 +00:00
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(ttd->td_proc);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
|
2007-08-16 05:26:42 +00:00
|
|
|
/* pid_t pid, long id, int sig */
|
|
|
|
{
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_t ksi;
|
2007-08-16 05:26:42 +00:00
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
2009-06-27 13:58:44 +00:00
|
|
|
AUDIT_ARG_SIGNUM(uap->sig);
|
2007-08-16 05:26:42 +00:00
|
|
|
|
2010-10-09 02:50:23 +00:00
|
|
|
ksiginfo_init(&ksi);
|
|
|
|
ksi.ksi_signo = uap->sig;
|
|
|
|
ksi.ksi_code = SI_LWP;
|
|
|
|
ksi.ksi_pid = td->td_proc->p_pid;
|
|
|
|
ksi.ksi_uid = td->td_ucred->cr_ruid;
|
|
|
|
if (uap->id == -1) {
|
|
|
|
if ((p = pfind(uap->pid)) == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
AUDIT_ARG_PROCESS(p);
|
|
|
|
error = p_cansignal(td, p, uap->sig);
|
|
|
|
if (error) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2007-08-16 05:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
} else {
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, uap->pid);
|
|
|
|
if (ttd == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
p = ttd->td_proc;
|
|
|
|
AUDIT_ARG_PROCESS(p);
|
|
|
|
error = p_cansignal(td, p, uap->sig);
|
|
|
|
if (uap->sig == 0)
|
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
|
|
|
PROC_UNLOCK(p);
|
2007-08-16 05:26:42 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-03-27 14:30:43 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
|
2004-03-27 14:30:43 +00:00
|
|
|
/* const struct timespec *timeout */
|
|
|
|
{
|
2006-09-22 15:04:28 +00:00
|
|
|
struct timespec ts, *tsp;
|
2004-03-27 14:30:43 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = NULL;
|
2004-03-27 14:30:43 +00:00
|
|
|
if (uap->timeout != NULL) {
|
2011-12-03 12:35:13 +00:00
|
|
|
error = umtx_copyin_timeout(uap->timeout, &ts);
|
2004-03-27 14:30:43 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (kern_thr_suspend(td, tsp));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_suspend(struct thread *td, struct timespec *tsp)
|
|
|
|
{
|
2010-10-20 00:41:38 +00:00
|
|
|
struct proc *p = td->td_proc;
|
2006-09-22 15:04:28 +00:00
|
|
|
struct timeval tv;
|
2010-08-24 07:29:55 +00:00
|
|
|
int error = 0;
|
|
|
|
int timo = 0;
|
2006-09-22 15:04:28 +00:00
|
|
|
|
2006-12-04 14:15:12 +00:00
|
|
|
if (td->td_pflags & TDP_WAKEUP) {
|
|
|
|
td->td_pflags &= ~TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-10-20 00:41:38 +00:00
|
|
|
if (tsp != NULL) {
|
2010-08-24 07:29:55 +00:00
|
|
|
if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
|
|
|
|
error = EWOULDBLOCK;
|
|
|
|
else {
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, tsp);
|
|
|
|
timo = tvtohz(&tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
|
|
|
|
error = msleep((void *)td, &p->p_mtx,
|
|
|
|
PCATCH, "lthr", timo);
|
|
|
|
|
2004-12-01 13:50:04 +00:00
|
|
|
if (td->td_flags & TDF_THRWAKEUP) {
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-12-01 13:50:04 +00:00
|
|
|
td->td_flags &= ~TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-12-01 13:50:04 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else if (error == ERESTART) {
|
2010-08-24 07:29:55 +00:00
|
|
|
if (timo != 0)
|
2004-12-01 13:50:04 +00:00
|
|
|
error = EINTR;
|
|
|
|
}
|
|
|
|
return (error);
|
2004-03-27 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id */
|
2004-03-27 14:30:43 +00:00
|
|
|
{
|
2005-11-03 01:34:08 +00:00
|
|
|
struct proc *p;
|
2004-07-02 00:40:07 +00:00
|
|
|
struct thread *ttd;
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2006-12-04 14:15:12 +00:00
|
|
|
if (uap->id == td->td_tid) {
|
|
|
|
td->td_pflags |= TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-11-03 01:34:08 +00:00
|
|
|
p = td->td_proc;
|
2010-10-09 02:50:23 +00:00
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
|
|
|
if (ttd == NULL)
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ESRCH);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(ttd);
|
2004-07-02 00:40:07 +00:00
|
|
|
ttd->td_flags |= TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(ttd);
|
2004-12-01 13:50:04 +00:00
|
|
|
wakeup((void *)ttd);
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-02-05 02:18:46 +00:00
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
|
2006-02-05 02:18:46 +00:00
|
|
|
{
|
2010-10-09 02:50:23 +00:00
|
|
|
struct proc *p;
|
2006-02-05 02:18:46 +00:00
|
|
|
char name[MAXCOMLEN + 1];
|
|
|
|
struct thread *ttd;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
name[0] = '\0';
|
|
|
|
if (uap->name != NULL) {
|
2016-12-03 01:14:21 +00:00
|
|
|
error = copyinstr(uap->name, name, sizeof(name), NULL);
|
|
|
|
if (error == ENAMETOOLONG) {
|
|
|
|
error = copyin(uap->name, name, sizeof(name) - 1);
|
|
|
|
name[sizeof(name) - 1] = '\0';
|
|
|
|
}
|
2006-02-05 02:18:46 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
|
|
|
if (ttd == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
strcpy(ttd->td_name, name);
|
2012-03-08 19:41:05 +00:00
|
|
|
#ifdef KTR
|
|
|
|
sched_clear_tdname(ttd);
|
|
|
|
#endif
|
2006-02-05 02:18:46 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
2015-05-24 14:37:45 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Have race condition but it is cheap. */
|
|
|
|
if (p->p_numthreads >= max_threads_per_proc) {
|
|
|
|
++max_threads_hits;
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ntd = thread_alloc(pages);
|
|
|
|
if (*ntd == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|