diff options
author | jhb <jhb@FreeBSD.org> | 2001-03-09 07:24:17 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2001-03-09 07:24:17 +0000 |
commit | f108bc4208540dee14fdc0004145f51fbd89e4ac (patch) | |
tree | c17b215dead08d254c13ea2b0af0c3b27e263d82 /sys/kern/kern_mutex.c | |
parent | 60a613e429ca74441c30dd73cee4855e47b71114 (diff) | |
download | FreeBSD-src-f108bc4208540dee14fdc0004145f51fbd89e4ac.zip FreeBSD-src-f108bc4208540dee14fdc0004145f51fbd89e4ac.tar.gz |
Fix mtx_legal2block. The only time that it is bad to block on a mutex is
if we hold a spin mutex, since we can trivially get into deadlocks if we
start switching out of processes that hold spinlocks. Checking to see if
interrupts were disabled was a sort of cheap way of doing this since most
of the time interrupts were only disabled when holding a spin lock. At
least on the i386. To fix this properly, use a per-process counter
p_spinlocks that counts the number of spin locks currently held, and
instead of checking to see if interrupts are disabled in the witness code,
check to see if we hold any spin locks. Since child processes always
start up with the sched lock magically held in fork_exit(), we initialize
p_spinlocks to 1 for child processes. Note that proc0 doesn't go through
fork_exit(), so it starts with no spin locks held.
Consulting from: cp
Diffstat (limited to 'sys/kern/kern_mutex.c')
-rw-r--r-- | sys/kern/kern_mutex.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 1ca5cca..6540b56 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -1094,6 +1094,8 @@ witness_enter(struct mtx *m, int flags, const char *file, int line) } PCPU_SET(witness_spin_check, i | w->w_level); mtx_unlock_spin_flags(&w_mtx, MTX_QUIET); + p->p_spinlocks++; + MPASS(p->p_spinlocks > 0); w->w_file = file; w->w_line = line; m->mtx_line = line; @@ -1116,7 +1118,7 @@ witness_enter(struct mtx *m, int flags, const char *file, int line) if (cold) goto out; - if (!mtx_legal2block()) + if (p->p_spinlocks != 0) panic("blockable mtx_lock() of %s when not legal @ %s:%d", m->mtx_description, file, line); /* @@ -1263,10 +1265,12 @@ void witness_exit(struct mtx *m, int flags, const char *file, int line) { struct witness *w; + struct proc *p; if (witness_cold || m->mtx_witness == NULL || panicstr) return; w = m->mtx_witness; + p = curproc; if (flags & MTX_SPIN) { if ((m->mtx_flags & MTX_SPIN) == 0) @@ -1283,6 +1287,8 @@ witness_exit(struct mtx *m, int flags, const char *file, int line) PCPU_SET(witness_spin_check, PCPU_GET(witness_spin_check) & ~w->w_level); mtx_unlock_spin_flags(&w_mtx, MTX_QUIET); + MPASS(p->p_spinlocks > 0); + p->p_spinlocks--; return; } if ((m->mtx_flags & MTX_SPIN) != 0) @@ -1297,7 +1303,7 @@ witness_exit(struct mtx *m, int flags, const char *file, int line) return; } - if ((flags & MTX_NOSWITCH) == 0 && !mtx_legal2block() && !cold) + if ((flags & MTX_NOSWITCH) == 0 && p->p_spinlocks != 0 && !cold) panic("switchable mtx_unlock() of %s when not legal @ %s:%d", m->mtx_description, file, line); LIST_REMOVE(m, mtx_held); |