2003-04-01 01:10:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, 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$");
|
|
|
|
|
2003-04-01 01:10:42 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
2003-06-03 05:24:46 +00:00
|
|
|
#include <sys/malloc.h>
|
2003-04-01 01:10:42 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
2003-06-03 05:24:46 +00:00
|
|
|
#include <sys/sx.h>
|
2003-04-01 01:10:42 +00:00
|
|
|
#include <sys/thr.h>
|
|
|
|
#include <sys/umtx.h>
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
struct umtx_q {
|
|
|
|
LIST_ENTRY(umtx_q) uq_next; /* Linked list for the hash. */
|
|
|
|
TAILQ_HEAD(, thread) uq_tdq; /* List of threads blocked here. */
|
|
|
|
struct umtx *uq_umtx; /* Pointer key component. */
|
|
|
|
pid_t uq_pid; /* Pid key component. */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define UMTX_QUEUES 128
|
|
|
|
#define UMTX_HASH(pid, umtx) \
|
|
|
|
(((uintptr_t)pid + ((uintptr_t)umtx & ~65535)) % UMTX_QUEUES)
|
|
|
|
|
|
|
|
LIST_HEAD(umtx_head, umtx_q);
|
|
|
|
static struct umtx_head queues[UMTX_QUEUES];
|
|
|
|
static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
|
|
|
|
|
|
|
|
static struct mtx umtx_lock;
|
|
|
|
MTX_SYSINIT(umtx, &umtx_lock, "umtx", MTX_DEF);
|
|
|
|
|
2003-05-25 18:18:32 +00:00
|
|
|
#define UMTX_LOCK() mtx_lock(&umtx_lock);
|
|
|
|
#define UMTX_UNLOCK() mtx_unlock(&umtx_lock);
|
|
|
|
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
static struct umtx_q *umtx_lookup(struct thread *, struct umtx *umtx);
|
|
|
|
static struct umtx_q *umtx_insert(struct thread *, struct umtx *umtx);
|
|
|
|
|
|
|
|
static struct umtx_q *
|
|
|
|
umtx_lookup(struct thread *td, struct umtx *umtx)
|
|
|
|
{
|
|
|
|
struct umtx_head *head;
|
|
|
|
struct umtx_q *uq;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pid = td->td_proc->p_pid;
|
|
|
|
|
|
|
|
head = &queues[UMTX_HASH(td->td_proc->p_pid, umtx)];
|
|
|
|
|
|
|
|
LIST_FOREACH(uq, head, uq_next) {
|
|
|
|
if (uq->uq_pid == pid && uq->uq_umtx == umtx)
|
|
|
|
return (uq);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a thread onto the umtx queue.
|
|
|
|
*/
|
|
|
|
static struct umtx_q *
|
|
|
|
umtx_insert(struct thread *td, struct umtx *umtx)
|
|
|
|
{
|
|
|
|
struct umtx_head *head;
|
|
|
|
struct umtx_q *uq;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pid = td->td_proc->p_pid;
|
|
|
|
|
|
|
|
if ((uq = umtx_lookup(td, umtx)) == NULL) {
|
|
|
|
struct umtx_q *ins;
|
|
|
|
|
|
|
|
UMTX_UNLOCK();
|
|
|
|
ins = malloc(sizeof(*uq), M_UMTX, M_ZERO | M_WAITOK);
|
|
|
|
UMTX_LOCK();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some one else could have succeeded while we were blocked
|
|
|
|
* waiting on memory.
|
|
|
|
*/
|
|
|
|
if ((uq = umtx_lookup(td, umtx)) == NULL) {
|
|
|
|
head = &queues[UMTX_HASH(pid, umtx)];
|
|
|
|
uq = ins;
|
|
|
|
uq->uq_pid = pid;
|
|
|
|
uq->uq_umtx = umtx;
|
|
|
|
LIST_INSERT_HEAD(head, uq, uq_next);
|
|
|
|
TAILQ_INIT(&uq->uq_tdq);
|
|
|
|
} else
|
|
|
|
free(ins, M_UMTX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert us onto the end of the TAILQ.
|
|
|
|
*/
|
|
|
|
TAILQ_INSERT_TAIL(&uq->uq_tdq, td, td_umtx);
|
|
|
|
|
|
|
|
return (uq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
umtx_remove(struct umtx_q *uq, struct thread *td)
|
|
|
|
{
|
|
|
|
TAILQ_REMOVE(&uq->uq_tdq, td, td_umtx);
|
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&uq->uq_tdq)) {
|
|
|
|
LIST_REMOVE(uq, uq_next);
|
|
|
|
free(uq, M_UMTX);
|
|
|
|
}
|
|
|
|
}
|
2003-05-25 18:18:32 +00:00
|
|
|
|
2003-04-01 01:10:42 +00:00
|
|
|
int
|
|
|
|
_umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
|
|
|
|
/* struct umtx *umtx */
|
|
|
|
{
|
2003-06-03 05:24:46 +00:00
|
|
|
struct umtx_q *uq;
|
2003-04-01 01:10:42 +00:00
|
|
|
struct umtx *umtx;
|
|
|
|
intptr_t owner;
|
2003-04-02 08:02:27 +00:00
|
|
|
intptr_t old;
|
2003-04-01 01:10:42 +00:00
|
|
|
int error;
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
uq = NULL;
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Care must be exercised when dealing with this structure. It
|
|
|
|
* can fault on any access.
|
|
|
|
*/
|
|
|
|
umtx = uap->umtx;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Try the uncontested case. This should be done in userland.
|
|
|
|
*/
|
|
|
|
owner = casuptr((intptr_t *)&umtx->u_owner,
|
|
|
|
UMTX_UNOWNED, (intptr_t)td);
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
/* The address was invalid. */
|
|
|
|
if (owner == -1)
|
|
|
|
return (EFAULT);
|
|
|
|
|
2003-04-01 01:10:42 +00:00
|
|
|
/* The acquire succeeded. */
|
2003-06-03 05:24:46 +00:00
|
|
|
if (owner == UMTX_UNOWNED)
|
|
|
|
return (0);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
/* If no one owns it but it is contested try to acquire it. */
|
|
|
|
if (owner == UMTX_CONTESTED) {
|
|
|
|
owner = casuptr((intptr_t *)&umtx->u_owner,
|
|
|
|
UMTX_CONTESTED, ((intptr_t)td | UMTX_CONTESTED));
|
|
|
|
|
|
|
|
/* The address was invalid. */
|
|
|
|
if (owner == -1)
|
|
|
|
return (EFAULT);
|
|
|
|
|
|
|
|
if (owner == MTX_CONTESTED);
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* If this failed the lock has changed, restart. */
|
|
|
|
continue;
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
|
|
|
|
UMTX_LOCK();
|
|
|
|
uq = umtx_insert(td, umtx);
|
|
|
|
UMTX_UNLOCK();
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the contested bit so that a release in user space
|
|
|
|
* knows to use the system call for unlock. If this fails
|
|
|
|
* either some one else has acquired the lock or it has been
|
|
|
|
* released.
|
|
|
|
*/
|
2003-04-02 08:02:27 +00:00
|
|
|
old = casuptr((intptr_t *)&umtx->u_owner, owner,
|
|
|
|
owner | UMTX_CONTESTED);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/* The address was invalid. */
|
2003-04-02 08:02:27 +00:00
|
|
|
if (old == -1) {
|
2003-06-03 05:24:46 +00:00
|
|
|
UMTX_LOCK();
|
|
|
|
umtx_remove(uq, td);
|
|
|
|
UMTX_UNLOCK();
|
|
|
|
return (EFAULT);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-06-03 05:24:46 +00:00
|
|
|
* We set the contested bit, sleep. Otherwise the lock changed
|
|
|
|
* and we need to retry.
|
2003-04-01 01:10:42 +00:00
|
|
|
*/
|
2003-06-03 05:24:46 +00:00
|
|
|
UMTX_LOCK();
|
|
|
|
if (old == owner)
|
|
|
|
error = msleep(td, &umtx_lock,
|
|
|
|
td->td_priority | PCATCH, "umtx", 0);
|
|
|
|
else
|
2003-04-01 01:10:42 +00:00
|
|
|
error = 0;
|
2003-06-03 05:24:46 +00:00
|
|
|
|
|
|
|
umtx_remove(uq, td);
|
|
|
|
UMTX_UNLOCK();
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/*
|
2003-06-03 05:24:46 +00:00
|
|
|
* If we caught a signal we might have to retry or exit
|
|
|
|
* immediately.
|
2003-04-01 01:10:42 +00:00
|
|
|
*/
|
|
|
|
if (error)
|
2003-06-03 05:24:46 +00:00
|
|
|
return (error);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
return (0);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
|
|
|
|
/* struct umtx *umtx */
|
|
|
|
{
|
2003-06-03 05:24:46 +00:00
|
|
|
struct thread *blocked;
|
2003-04-01 01:10:42 +00:00
|
|
|
struct umtx *umtx;
|
2003-06-03 05:24:46 +00:00
|
|
|
struct umtx_q *uq;
|
2003-04-01 01:10:42 +00:00
|
|
|
intptr_t owner;
|
2003-04-02 08:02:27 +00:00
|
|
|
intptr_t old;
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
umtx = uap->umtx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we own this mtx.
|
|
|
|
*
|
|
|
|
* XXX Need a {fu,su}ptr this is not correct on arch where
|
|
|
|
* sizeof(intptr_t) != sizeof(long).
|
|
|
|
*/
|
2003-06-03 05:24:46 +00:00
|
|
|
if ((owner = fuword(&umtx->u_owner)) == -1)
|
|
|
|
return (EFAULT);
|
|
|
|
|
|
|
|
if ((struct thread *)(owner & ~UMTX_CONTESTED) != td)
|
|
|
|
return (EPERM);
|
2003-04-01 01:10:42 +00:00
|
|
|
/*
|
|
|
|
* If we own it but it isn't contested then we can just release and
|
|
|
|
* return.
|
|
|
|
*/
|
|
|
|
if ((owner & UMTX_CONTESTED) == 0) {
|
|
|
|
owner = casuptr((intptr_t *)&umtx->u_owner,
|
|
|
|
(intptr_t)td, UMTX_UNOWNED);
|
|
|
|
|
|
|
|
if (owner == -1)
|
2003-06-03 05:24:46 +00:00
|
|
|
return (EFAULT);
|
2003-04-01 01:10:42 +00:00
|
|
|
/*
|
|
|
|
* If this failed someone modified the memory without going
|
|
|
|
* through this api.
|
|
|
|
*/
|
2003-06-03 05:24:46 +00:00
|
|
|
if (owner != (intptr_t)td)
|
|
|
|
return (EINVAL);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
return (0);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
old = casuptr((intptr_t *)&umtx->u_owner, owner, UMTX_CONTESTED);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
if (old == -1)
|
|
|
|
return (EFAULT);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/*
|
2003-06-03 05:24:46 +00:00
|
|
|
* This will only happen if someone modifies the lock without going
|
|
|
|
* through this api.
|
2003-04-01 01:10:42 +00:00
|
|
|
*/
|
2003-06-03 05:24:46 +00:00
|
|
|
if (old != owner)
|
|
|
|
return (EINVAL);
|
2003-04-01 01:10:42 +00:00
|
|
|
|
|
|
|
/*
|
2003-06-03 05:24:46 +00:00
|
|
|
* We have to wake up one of the blocked threads.
|
2003-04-01 01:10:42 +00:00
|
|
|
*/
|
2003-06-03 05:24:46 +00:00
|
|
|
UMTX_LOCK();
|
|
|
|
uq = umtx_lookup(td, umtx);
|
|
|
|
if (uq != NULL) {
|
|
|
|
blocked = TAILQ_FIRST(&uq->uq_tdq);
|
|
|
|
wakeup(blocked);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 18:18:32 +00:00
|
|
|
UMTX_UNLOCK();
|
2003-04-01 01:10:42 +00:00
|
|
|
|
2003-06-03 05:24:46 +00:00
|
|
|
return (0);
|
2003-04-01 01:10:42 +00:00
|
|
|
}
|