#include <sys/cdefs.h>
#include "opt_ddb.h"
#include "opt_turnstile_profiling.h"
#include "opt_sched.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/sched.h>
#include <sys/sdt.h>
#include <sys/sysctl.h>
#include <sys/turnstile.h>
#include <vm/uma.h>
#ifdef DDB
#include <ddb/ddb.h>
#include <sys/lockmgr.h>
#include <sys/sx.h>
#endif
#define TC_TABLESIZE 128
#define TC_MASK (TC_TABLESIZE - 1)
#define TC_SHIFT 8
#define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
#define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
struct turnstile {
struct mtx ts_lock;
struct threadqueue ts_blocked[2];
struct threadqueue ts_pending;
LIST_ENTRY(turnstile) ts_hash;
LIST_ENTRY(turnstile) ts_link;
LIST_HEAD(, turnstile) ts_free;
struct lock_object *ts_lockobj;
struct thread *ts_owner;
};
struct turnstile_chain {
LIST_HEAD(, turnstile) tc_turnstiles;
struct mtx tc_lock;
#ifdef TURNSTILE_PROFILING
u_int tc_depth;
u_int tc_max_depth;
#endif
};
#ifdef TURNSTILE_PROFILING
u_int turnstile_max_depth;
static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
"turnstile profiling");
static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains,
CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
"turnstile chain stats");
SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
&turnstile_max_depth, 0, "maximum depth achieved of a single chain");
#endif
static struct mtx td_contested_lock;
static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
static uma_zone_t turnstile_zone;
static void init_turnstile0(void *dummy);
#ifdef TURNSTILE_PROFILING
static void init_turnstile_profiling(void *arg);
#endif
static void propagate_priority(struct thread *td);
static int turnstile_adjust_thread(struct turnstile *ts,
struct thread *td);
static struct thread *turnstile_first_waiter(struct turnstile *ts);
static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
#ifdef INVARIANTS
static void turnstile_dtor(void *mem, int size, void *arg);
#endif
static int turnstile_init(void *mem, int size, int flags);
static void turnstile_fini(void *mem, int size);
SDT_PROVIDER_DECLARE(sched);
SDT_PROBE_DEFINE(sched, , , sleep);
SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
"struct proc *");
static inline void
propagate_unlock_ts(struct turnstile *top, struct turnstile *ts)
{
if (ts != top)
mtx_unlock_spin(&ts->ts_lock);
}
static inline void
propagate_unlock_td(struct turnstile *top, struct thread *td)
{
if (td->td_lock != &top->ts_lock)
thread_unlock(td);
}
static void
propagate_priority(struct thread *td)
{
struct turnstile *ts, *top;
int pri;
THREAD_LOCK_ASSERT(td, MA_OWNED);
pri = td->td_priority;
top = ts = td->td_blocked;
THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
for (;;) {
td = ts->ts_owner;
if (td == NULL) {
propagate_unlock_ts(top, ts);
return;
}
thread_lock_block_wait(td);
if (td->td_lock != &ts->ts_lock) {
thread_lock_flags(td, MTX_DUPOK);
propagate_unlock_ts(top, ts);
}
MPASS(td->td_proc != NULL);
MPASS(td->td_proc->p_magic == P_MAGIC);
if (TD_IS_SLEEPING(td)) {
printf(
"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
td->td_tid, td->td_proc->p_pid);
kdb_backtrace_thread(td);
panic("sleeping thread holds %s",
ts->ts_lockobj->lo_name);
}
if (td->td_priority <= pri) {
propagate_unlock_td(top, td);
return;
}
sched_lend_prio(td, pri);
if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
MPASS(td->td_blocked == NULL);
propagate_unlock_td(top, td);
return;
}
#ifndef SMP
KASSERT(td != curthread, ("Deadlock detected"));
#endif
KASSERT(TD_ON_LOCK(td), (
"thread %d(%s):%d holds %s but isn't blocked on a lock\n",
td->td_tid, td->td_name, TD_GET_STATE(td),
ts->ts_lockobj->lo_name));
ts = td->td_blocked;
MPASS(ts != NULL);
THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
if (!turnstile_adjust_thread(ts, td)) {
propagate_unlock_ts(top, ts);
return;
}
}
}
static int
turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
{
struct thread *td1, *td2;
int queue;
THREAD_LOCK_ASSERT(td, MA_OWNED);
MPASS(TD_ON_LOCK(td));
if (td->td_turnstile != NULL)
return (0);
THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
td1 = TAILQ_PREV(td, threadqueue, td_lockq);
td2 = TAILQ_NEXT(td, td_lockq);
if ((td1 != NULL && td->td_priority < td1->td_priority) ||
(td2 != NULL && td->td_priority > td2->td_priority)) {
queue = td->td_tsqueue;
MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
mtx_lock_spin(&td_contested_lock);
TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
MPASS(td1->td_proc->p_magic == P_MAGIC);
if (td1->td_priority > td->td_priority)
break;
}
if (td1 == NULL)
TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
else
TAILQ_INSERT_BEFORE(td1, td, td_lockq);
mtx_unlock_spin(&td_contested_lock);
if (td1 == NULL)
CTR3(KTR_LOCK,
"turnstile_adjust_thread: td %d put at tail on [%p] %s",
td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
else
CTR4(KTR_LOCK,
"turnstile_adjust_thread: td %d moved before %d on [%p] %s",
td->td_tid, td1->td_tid, ts->ts_lockobj,
ts->ts_lockobj->lo_name);
}
return (1);
}
void
init_turnstiles(void)
{
int i;
for (i = 0; i < TC_TABLESIZE; i++) {
LIST_INIT(&turnstile_chains[i].tc_turnstiles);
mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
NULL, MTX_SPIN);
}
mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
LIST_INIT(&thread0.td_contested);
thread0.td_turnstile = NULL;
}
#ifdef TURNSTILE_PROFILING
static void
init_turnstile_profiling(void *arg)
{
struct sysctl_oid *chain_oid;
char chain_name[10];
int i;
for (i = 0; i < TC_TABLESIZE; i++) {
snprintf(chain_name, sizeof(chain_name), "%d", i);
chain_oid = SYSCTL_ADD_NODE(NULL,
SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
"turnstile chain stats");
SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
"depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
NULL);
SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
"max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
0, NULL);
}
}
SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
init_turnstile_profiling, NULL);
#endif
static void
init_turnstile0(void *dummy)
{
turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
NULL,
#ifdef INVARIANTS
turnstile_dtor,
#else
NULL,
#endif
turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
thread0.td_turnstile = turnstile_alloc();
}
SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
void
turnstile_adjust(struct thread *td, u_char oldpri)
{
struct turnstile *ts;
MPASS(TD_ON_LOCK(td));
ts = td->td_blocked;
MPASS(ts != NULL);
THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
mtx_assert(&ts->ts_lock, MA_OWNED);
if (!turnstile_adjust_thread(ts, td))
return;
MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
td->td_tsqueue == TS_SHARED_QUEUE);
if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
td->td_priority < oldpri) {
propagate_priority(td);
}
}
static void
turnstile_setowner(struct turnstile *ts, struct thread *owner)
{
mtx_assert(&td_contested_lock, MA_OWNED);
MPASS(ts->ts_owner == NULL);
if (owner == NULL)
return;
MPASS(owner->td_proc->p_magic == P_MAGIC);
ts->ts_owner = owner;
LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
}
#ifdef INVARIANTS
static void
turnstile_dtor(void *mem, int size, void *arg)
{
struct turnstile *ts;
ts = mem;
MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
MPASS(TAILQ_EMPTY(&ts->ts_pending));
}
#endif
static int
turnstile_init(void *mem, int size, int flags)
{
struct turnstile *ts;
bzero(mem, size);
ts = mem;
TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
TAILQ_INIT(&ts->ts_pending);
LIST_INIT(&ts->ts_free);
mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN);
return (0);
}
static void
turnstile_fini(void *mem, int size)
{
struct turnstile *ts;
ts = mem;
mtx_destroy(&ts->ts_lock);
}
struct turnstile *
turnstile_alloc(void)
{
return (uma_zalloc(turnstile_zone, M_WAITOK));
}
void
turnstile_free(struct turnstile *ts)
{
uma_zfree(turnstile_zone, ts);
}
void
turnstile_chain_lock(struct lock_object *lock)
{
struct turnstile_chain *tc;
tc = TC_LOOKUP(lock);
mtx_lock_spin(&tc->tc_lock);
}
struct turnstile *
turnstile_trywait(struct lock_object *lock)
{
struct turnstile_chain *tc;
struct turnstile *ts;
tc = TC_LOOKUP(lock);
mtx_lock_spin(&tc->tc_lock);
LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
if (ts->ts_lockobj == lock) {
mtx_lock_spin(&ts->ts_lock);
return (ts);
}
ts = curthread->td_turnstile;
MPASS(ts != NULL);
mtx_lock_spin(&ts->ts_lock);
KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
ts->ts_lockobj = lock;
return (ts);
}
bool
turnstile_lock(struct turnstile *ts, struct lock_object **lockp,
struct thread **tdp)
{
struct turnstile_chain *tc;
struct lock_object *lock;
if ((lock = ts->ts_lockobj) == NULL)
return (false);
tc = TC_LOOKUP(lock);
mtx_lock_spin(&tc->tc_lock);
mtx_lock_spin(&ts->ts_lock);
if (__predict_false(lock != ts->ts_lockobj)) {
mtx_unlock_spin(&tc->tc_lock);
mtx_unlock_spin(&ts->ts_lock);
return (false);
}
*lockp = lock;
*tdp = ts->ts_owner;
return (true);
}
void
turnstile_unlock(struct turnstile *ts, struct lock_object *lock)
{
struct turnstile_chain *tc;
mtx_assert(&ts->ts_lock, MA_OWNED);
mtx_unlock_spin(&ts->ts_lock);
if (ts == curthread->td_turnstile)
ts->ts_lockobj = NULL;
tc = TC_LOOKUP(lock);
mtx_unlock_spin(&tc->tc_lock);
}
void
turnstile_assert(struct turnstile *ts)
{
MPASS(ts->ts_lockobj == NULL);
}
void
turnstile_cancel(struct turnstile *ts)
{
struct turnstile_chain *tc;
struct lock_object *lock;
mtx_assert(&ts->ts_lock, MA_OWNED);
mtx_unlock_spin(&ts->ts_lock);
lock = ts->ts_lockobj;
if (ts == curthread->td_turnstile)
ts->ts_lockobj = NULL;
tc = TC_LOOKUP(lock);
mtx_unlock_spin(&tc->tc_lock);
}
struct turnstile *
turnstile_lookup(struct lock_object *lock)
{
struct turnstile_chain *tc;
struct turnstile *ts;
tc = TC_LOOKUP(lock);
mtx_assert(&tc->tc_lock, MA_OWNED);
LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
if (ts->ts_lockobj == lock) {
mtx_lock_spin(&ts->ts_lock);
return (ts);
}
return (NULL);
}
void
turnstile_chain_unlock(struct lock_object *lock)
{
struct turnstile_chain *tc;
tc = TC_LOOKUP(lock);
mtx_unlock_spin(&tc->tc_lock);
}
static struct thread *
turnstile_first_waiter(struct turnstile *ts)
{
struct thread *std, *xtd;
std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
return (std);
return (xtd);
}
void
turnstile_claim(struct turnstile *ts)
{
struct thread *td, *owner;
struct turnstile_chain *tc;
mtx_assert(&ts->ts_lock, MA_OWNED);
MPASS(ts != curthread->td_turnstile);
owner = curthread;
mtx_lock_spin(&td_contested_lock);
turnstile_setowner(ts, owner);
mtx_unlock_spin(&td_contested_lock);
td = turnstile_first_waiter(ts);
MPASS(td != NULL);
MPASS(td->td_proc->p_magic == P_MAGIC);
THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
thread_lock(owner);
if (td->td_priority < owner->td_priority)
sched_lend_prio(owner, td->td_priority);
thread_unlock(owner);
tc = TC_LOOKUP(ts->ts_lockobj);
mtx_unlock_spin(&ts->ts_lock);
mtx_unlock_spin(&tc->tc_lock);
}
void
turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
{
struct turnstile_chain *tc;
struct thread *td, *td1;
struct lock_object *lock;
td = curthread;
mtx_assert(&ts->ts_lock, MA_OWNED);
if (owner)
MPASS(owner->td_proc->p_magic == P_MAGIC);
MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
tc = TC_LOOKUP(ts->ts_lockobj);
mtx_assert(&tc->tc_lock, MA_OWNED);
if (ts == td->td_turnstile) {
#ifdef TURNSTILE_PROFILING
tc->tc_depth++;
if (tc->tc_depth > tc->tc_max_depth) {
tc->tc_max_depth = tc->tc_depth;
if (tc->tc_max_depth > turnstile_max_depth)
turnstile_max_depth = tc->tc_max_depth;
}
#endif
LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
KASSERT(TAILQ_EMPTY(&ts->ts_pending),
("thread's turnstile has pending threads"));
KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
("thread's turnstile has exclusive waiters"));
KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
("thread's turnstile has shared waiters"));
KASSERT(LIST_EMPTY(&ts->ts_free),
("thread's turnstile has a non-empty free list"));
MPASS(ts->ts_lockobj != NULL);
mtx_lock_spin(&td_contested_lock);
TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
turnstile_setowner(ts, owner);
mtx_unlock_spin(&td_contested_lock);
} else {
TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
if (td1->td_priority > td->td_priority)
break;
mtx_lock_spin(&td_contested_lock);
if (td1 != NULL)
TAILQ_INSERT_BEFORE(td1, td, td_lockq);
else
TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
MPASS(owner == ts->ts_owner);
mtx_unlock_spin(&td_contested_lock);
MPASS(td->td_turnstile != NULL);
LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
}
thread_lock(td);
thread_lock_set(td, &ts->ts_lock);
td->td_turnstile = NULL;
lock = ts->ts_lockobj;
td->td_tsqueue = queue;
td->td_blocked = ts;
td->td_lockname = lock->lo_name;
td->td_blktick = ticks;
TD_SET_LOCK(td);
mtx_unlock_spin(&tc->tc_lock);
propagate_priority(td);
if (LOCK_LOG_TEST(lock, 0))
CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
td->td_tid, lock, lock->lo_name);
SDT_PROBE0(sched, , , sleep);
THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
mi_switch(SW_VOL | SWT_TURNSTILE);
if (LOCK_LOG_TEST(lock, 0))
CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
__func__, td->td_tid, lock, lock->lo_name);
}
int
turnstile_signal(struct turnstile *ts, int queue)
{
struct turnstile_chain *tc __unused;
struct thread *td;
int empty;
MPASS(ts != NULL);
mtx_assert(&ts->ts_lock, MA_OWNED);
MPASS(curthread->td_proc->p_magic == P_MAGIC);
MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
td = TAILQ_FIRST(&ts->ts_blocked[queue]);
MPASS(td->td_proc->p_magic == P_MAGIC);
mtx_lock_spin(&td_contested_lock);
TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
mtx_unlock_spin(&td_contested_lock);
TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
if (empty) {
tc = TC_LOOKUP(ts->ts_lockobj);
mtx_assert(&tc->tc_lock, MA_OWNED);
MPASS(LIST_EMPTY(&ts->ts_free));
#ifdef TURNSTILE_PROFILING
tc->tc_depth--;
#endif
} else
ts = LIST_FIRST(&ts->ts_free);
MPASS(ts != NULL);
LIST_REMOVE(ts, ts_hash);
td->td_turnstile = ts;
return (empty);
}
void
turnstile_broadcast(struct turnstile *ts, int queue)
{
struct turnstile_chain *tc __unused;
struct turnstile *ts1;
struct thread *td;
MPASS(ts != NULL);
mtx_assert(&ts->ts_lock, MA_OWNED);
MPASS(curthread->td_proc->p_magic == P_MAGIC);
MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
tc = TC_LOOKUP(ts->ts_lockobj);
mtx_assert(&tc->tc_lock, MA_OWNED);
MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
mtx_lock_spin(&td_contested_lock);
TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
mtx_unlock_spin(&td_contested_lock);
TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
if (LIST_EMPTY(&ts->ts_free)) {
MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
ts1 = ts;
#ifdef TURNSTILE_PROFILING
tc->tc_depth--;
#endif
} else
ts1 = LIST_FIRST(&ts->ts_free);
MPASS(ts1 != NULL);
LIST_REMOVE(ts1, ts_hash);
td->td_turnstile = ts1;
}
}
static u_char
turnstile_calc_unlend_prio_locked(struct thread *td)
{
struct turnstile *nts;
u_char cp, pri;
THREAD_LOCK_ASSERT(td, MA_OWNED);
mtx_assert(&td_contested_lock, MA_OWNED);
pri = PRI_MAX;
LIST_FOREACH(nts, &td->td_contested, ts_link) {
cp = turnstile_first_waiter(nts)->td_priority;
if (cp < pri)
pri = cp;
}
return (pri);
}
void
turnstile_unpend(struct turnstile *ts)
{
TAILQ_HEAD( ,thread) pending_threads;
struct thread *td;
u_char pri;
MPASS(ts != NULL);
mtx_assert(&ts->ts_lock, MA_OWNED);
MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
MPASS(!TAILQ_EMPTY(&ts->ts_pending));
TAILQ_INIT(&pending_threads);
TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
#ifdef INVARIANTS
if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
ts->ts_lockobj = NULL;
#endif
td = curthread;
thread_lock(td);
mtx_lock_spin(&td_contested_lock);
if (ts->ts_owner != NULL) {
ts->ts_owner = NULL;
LIST_REMOVE(ts, ts_link);
}
pri = turnstile_calc_unlend_prio_locked(td);
mtx_unlock_spin(&td_contested_lock);
sched_unlend_prio(td, pri);
thread_unlock(td);
while (!TAILQ_EMPTY(&pending_threads)) {
td = TAILQ_FIRST(&pending_threads);
TAILQ_REMOVE(&pending_threads, td, td_lockq);
SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
thread_lock_block_wait(td);
THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
MPASS(td->td_proc->p_magic == P_MAGIC);
MPASS(TD_ON_LOCK(td));
TD_CLR_LOCK(td);
MPASS(TD_CAN_RUN(td));
td->td_blocked = NULL;
td->td_lockname = NULL;
td->td_blktick = 0;
#ifdef INVARIANTS
td->td_tsqueue = 0xff;
#endif
sched_add(td, SRQ_HOLD | SRQ_BORING);
}
mtx_unlock_spin(&ts->ts_lock);
}
void
turnstile_disown(struct turnstile *ts)
{
struct thread *td;
u_char pri;
MPASS(ts != NULL);
mtx_assert(&ts->ts_lock, MA_OWNED);
MPASS(ts->ts_owner == curthread);
MPASS(TAILQ_EMPTY(&ts->ts_pending));
MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
!TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
mtx_lock_spin(&td_contested_lock);
ts->ts_owner = NULL;
LIST_REMOVE(ts, ts_link);
mtx_unlock_spin(&td_contested_lock);
td = curthread;
thread_lock(td);
mtx_unlock_spin(&ts->ts_lock);
mtx_lock_spin(&td_contested_lock);
pri = turnstile_calc_unlend_prio_locked(td);
mtx_unlock_spin(&td_contested_lock);
sched_unlend_prio(td, pri);
thread_unlock(td);
}
struct thread *
turnstile_head(struct turnstile *ts, int queue)
{
#ifdef INVARIANTS
MPASS(ts != NULL);
MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
mtx_assert(&ts->ts_lock, MA_OWNED);
#endif
return (TAILQ_FIRST(&ts->ts_blocked[queue]));
}
int
turnstile_empty(struct turnstile *ts, int queue)
{
#ifdef INVARIANTS
MPASS(ts != NULL);
MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
mtx_assert(&ts->ts_lock, MA_OWNED);
#endif
return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
}
#ifdef DDB
static void
print_thread(struct thread *td, const char *prefix)
{
db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
td->td_proc->p_pid, td->td_name);
}
static void
print_queue(struct threadqueue *queue, const char *header, const char *prefix)
{
struct thread *td;
db_printf("%s:\n", header);
if (TAILQ_EMPTY(queue)) {
db_printf("%sempty\n", prefix);
return;
}
TAILQ_FOREACH(td, queue, td_lockq) {
print_thread(td, prefix);
}
}
DB_SHOW_COMMAND(turnstile, db_show_turnstile)
{
struct turnstile_chain *tc;
struct turnstile *ts;
struct lock_object *lock;
int i;
if (!have_addr)
return;
lock = (struct lock_object *)addr;
tc = TC_LOOKUP(lock);
LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
if (ts->ts_lockobj == lock)
goto found;
for (i = 0; i < TC_TABLESIZE; i++)
LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
if (ts == (struct turnstile *)addr)
goto found;
}
db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
return;
found:
lock = ts->ts_lockobj;
db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
lock->lo_name);
if (ts->ts_owner)
print_thread(ts->ts_owner, "Lock Owner: ");
else
db_printf("Lock Owner: none\n");
print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
"\t");
print_queue(&ts->ts_pending, "Pending Threads", "\t");
}
static void
print_lockchain(struct thread *td, const char *prefix)
{
struct lock_object *lock;
struct lock_class *class;
struct turnstile *ts;
struct thread *owner;
while (!db_pager_quit) {
if (td == (void *)LK_KERNPROC) {
db_printf("%sdisowned (LK_KERNPROC)\n", prefix);
return;
}
db_printf("%sthread %d (pid %d, %s) is ", prefix, td->td_tid,
td->td_proc->p_pid, td->td_name);
switch (TD_GET_STATE(td)) {
case TDS_INACTIVE:
db_printf("inactive\n");
return;
case TDS_CAN_RUN:
db_printf("runnable\n");
return;
case TDS_RUNQ:
db_printf("on a run queue\n");
return;
case TDS_RUNNING:
db_printf("running on CPU %d\n", td->td_oncpu);
return;
case TDS_INHIBITED:
if (TD_ON_LOCK(td)) {
ts = td->td_blocked;
lock = ts->ts_lockobj;
class = LOCK_CLASS(lock);
db_printf("blocked on lock %p (%s) \"%s\"\n",
lock, class->lc_name, lock->lo_name);
if (ts->ts_owner == NULL)
return;
td = ts->ts_owner;
break;
} else if (TD_ON_SLEEPQ(td)) {
if (!lockmgr_chain(td, &owner) &&
!sx_chain(td, &owner)) {
db_printf("sleeping on %p \"%s\"\n",
td->td_wchan, td->td_wmesg);
return;
}
if (owner == NULL)
return;
td = owner;
break;
}
db_printf("inhibited: %s\n", KTDSTATE(td));
return;
default:
db_printf("??? (%#x)\n", TD_GET_STATE(td));
return;
}
}
}
DB_SHOW_COMMAND(lockchain, db_show_lockchain)
{
struct thread *td;
if (have_addr)
td = db_lookup_thread(addr, true);
else
td = kdb_thread;
print_lockchain(td, "");
}
DB_SHOW_ALIAS(sleepchain, db_show_lockchain);
DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
{
struct thread *td;
struct proc *p;
int i;
i = 1;
FOREACH_PROC_IN_SYSTEM(p) {
FOREACH_THREAD_IN_PROC(p, td) {
if ((TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested))
|| (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td))) {
db_printf("chain %d:\n", i++);
print_lockchain(td, " ");
}
if (db_pager_quit)
return;
}
}
}
DB_SHOW_ALIAS_FLAGS(allchains, db_show_allchains, DB_CMD_MEMSAFE);
static void print_waiters(struct turnstile *ts, int indent);
static void
print_waiter(struct thread *td, int indent)
{
struct turnstile *ts;
int i;
if (db_pager_quit)
return;
for (i = 0; i < indent; i++)
db_printf(" ");
print_thread(td, "thread ");
LIST_FOREACH(ts, &td->td_contested, ts_link)
print_waiters(ts, indent + 1);
}
static void
print_waiters(struct turnstile *ts, int indent)
{
struct lock_object *lock;
struct lock_class *class;
struct thread *td;
int i;
if (db_pager_quit)
return;
lock = ts->ts_lockobj;
class = LOCK_CLASS(lock);
for (i = 0; i < indent; i++)
db_printf(" ");
db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
print_waiter(td, indent + 1);
TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
print_waiter(td, indent + 1);
TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
print_waiter(td, indent + 1);
}
DB_SHOW_COMMAND(locktree, db_show_locktree)
{
struct lock_object *lock;
struct lock_class *class;
struct turnstile_chain *tc;
struct turnstile *ts;
if (!have_addr)
return;
lock = (struct lock_object *)addr;
tc = TC_LOOKUP(lock);
LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
if (ts->ts_lockobj == lock)
break;
if (ts == NULL) {
class = LOCK_CLASS(lock);
db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
lock->lo_name);
} else
print_waiters(ts, 0);
}
#endif