2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2001-03-05 19:59:41 +00:00
|
|
|
* Copyright (C) 2001 Jason Evans <jasone@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(s), this list of conditions and the following disclaimer as
|
|
|
|
* the first lines of this file unmodified other than the possible
|
|
|
|
* addition of one or more copyright notices.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice(s), 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shared/exclusive locks. This implementation assures deterministic lock
|
|
|
|
* granting behavior, so that slocks and xlocks are interleaved.
|
|
|
|
*
|
|
|
|
* Priority propagation will not generally raise the priority of lock holders,
|
|
|
|
* so should not be relied upon in combination with sx locks.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-12-13 23:14:35 +00:00
|
|
|
#include "opt_ddb.h"
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/ktr.h>
|
2005-12-13 23:14:35 +00:00
|
|
|
#include <sys/linker_set.h>
|
2001-03-05 19:59:41 +00:00
|
|
|
#include <sys/condvar.h>
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
#include <sys/lock.h>
|
2001-03-05 19:59:41 +00:00
|
|
|
#include <sys/mutex.h>
|
2005-12-13 23:14:35 +00:00
|
|
|
#include <sys/proc.h>
|
2001-03-05 19:59:41 +00:00
|
|
|
#include <sys/sx.h>
|
2006-11-11 03:18:07 +00:00
|
|
|
#include <sys/lock_profile.h>
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2006-08-15 18:29:01 +00:00
|
|
|
#ifdef DDB
|
2005-12-13 23:14:35 +00:00
|
|
|
#include <ddb/ddb.h>
|
|
|
|
|
|
|
|
static void db_show_sx(struct lock_object *lock);
|
|
|
|
#endif
|
|
|
|
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
struct lock_class lock_class_sx = {
|
|
|
|
"sx",
|
2005-12-13 23:14:35 +00:00
|
|
|
LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
|
|
|
|
#ifdef DDB
|
|
|
|
db_show_sx
|
|
|
|
#endif
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
};
|
|
|
|
|
2001-10-24 14:18:33 +00:00
|
|
|
#ifndef INVARIANTS
|
|
|
|
#define _sx_assert(sx, what, file, line)
|
|
|
|
#endif
|
|
|
|
|
2002-04-02 16:05:43 +00:00
|
|
|
void
|
|
|
|
sx_sysinit(void *arg)
|
|
|
|
{
|
|
|
|
struct sx_args *sargs = arg;
|
|
|
|
|
|
|
|
sx_init(sargs->sa_sx, sargs->sa_desc);
|
|
|
|
}
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
void
|
|
|
|
sx_init(struct sx *sx, const char *description)
|
|
|
|
{
|
2006-01-17 16:55:17 +00:00
|
|
|
|
2003-07-13 01:22:21 +00:00
|
|
|
sx->sx_lock = mtx_pool_find(mtxpool_lockbuilder, sx);
|
2001-03-05 19:59:41 +00:00
|
|
|
sx->sx_cnt = 0;
|
|
|
|
cv_init(&sx->sx_shrd_cv, description);
|
|
|
|
sx->sx_shrd_wcnt = 0;
|
|
|
|
cv_init(&sx->sx_excl_cv, description);
|
|
|
|
sx->sx_excl_wcnt = 0;
|
2001-03-06 06:17:05 +00:00
|
|
|
sx->sx_xholder = NULL;
|
2006-11-13 05:41:46 +00:00
|
|
|
lock_profile_object_init(&sx->sx_object, &lock_class_sx, description);
|
2006-01-17 16:55:17 +00:00
|
|
|
lock_init(&sx->sx_object, &lock_class_sx, description, NULL,
|
|
|
|
LO_WITNESS | LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sx_destroy(struct sx *sx)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((sx->sx_cnt == 0 && sx->sx_shrd_wcnt == 0 && sx->sx_excl_wcnt ==
|
2001-12-10 05:51:45 +00:00
|
|
|
0), ("%s (%s): holders or waiters\n", __func__,
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
sx->sx_object.lo_name));
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
sx->sx_lock = NULL;
|
2001-03-05 19:59:41 +00:00
|
|
|
cv_destroy(&sx->sx_shrd_cv);
|
|
|
|
cv_destroy(&sx->sx_excl_cv);
|
2006-11-11 03:18:07 +00:00
|
|
|
|
|
|
|
lock_profile_object_destroy(&sx->sx_object);
|
2006-01-17 16:55:17 +00:00
|
|
|
lock_destroy(&sx->sx_object);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
_sx_slock(struct sx *sx, const char *file, int line)
|
2001-03-05 19:59:41 +00:00
|
|
|
{
|
2006-11-11 03:18:07 +00:00
|
|
|
uint64_t waittime = 0;
|
2007-02-26 08:26:44 +00:00
|
|
|
int contested = 0;
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(sx->sx_xholder != curthread,
|
2001-12-10 05:51:45 +00:00
|
|
|
("%s (%s): slock while xlock is held @ %s:%d\n", __func__,
|
2001-06-27 06:39:37 +00:00
|
|
|
sx->sx_object.lo_name, file, line));
|
2004-01-28 20:39:57 +00:00
|
|
|
WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER, file, line);
|
2001-03-05 19:59:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop in case we lose the race for lock acquisition.
|
|
|
|
*/
|
|
|
|
while (sx->sx_cnt < 0) {
|
|
|
|
sx->sx_shrd_wcnt++;
|
2007-02-26 08:26:44 +00:00
|
|
|
lock_profile_obtain_lock_failed(&sx->sx_object, &contested, &waittime);
|
2001-11-13 21:55:13 +00:00
|
|
|
cv_wait(&sx->sx_shrd_cv, sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
sx->sx_shrd_wcnt--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Acquire a shared lock. */
|
|
|
|
sx->sx_cnt++;
|
|
|
|
|
2006-11-11 03:18:07 +00:00
|
|
|
if (sx->sx_cnt == 1)
|
2007-02-26 08:26:44 +00:00
|
|
|
lock_profile_obtain_lock_success(&sx->sx_object, contested, waittime, file, line);
|
2006-11-11 03:18:07 +00:00
|
|
|
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line);
|
|
|
|
WITNESS_LOCK(&sx->sx_object, 0, file, line);
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks++;
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
|
|
|
|
2001-06-27 06:39:37 +00:00
|
|
|
int
|
|
|
|
_sx_try_slock(struct sx *sx, const char *file, int line)
|
|
|
|
{
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
if (sx->sx_cnt >= 0) {
|
|
|
|
sx->sx_cnt++;
|
|
|
|
LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
|
|
|
|
WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks++;
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
void
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
_sx_xlock(struct sx *sx, const char *file, int line)
|
2001-03-05 19:59:41 +00:00
|
|
|
{
|
2007-02-26 08:26:44 +00:00
|
|
|
int contested = 0;
|
2006-11-11 03:18:07 +00:00
|
|
|
uint64_t waittime = 0;
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2001-03-06 06:17:05 +00:00
|
|
|
/*
|
|
|
|
* With sx locks, we're absolutely not permitted to recurse on
|
|
|
|
* xlocks, as it is fatal (deadlock). Normally, recursion is handled
|
|
|
|
* by WITNESS, but as it is not semantically correct to hold the
|
|
|
|
* xlock while in here, we consider it API abuse and put it under
|
|
|
|
* INVARIANTS.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(sx->sx_xholder != curthread,
|
2001-12-10 05:51:45 +00:00
|
|
|
("%s (%s): xlock already held @ %s:%d", __func__,
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
sx->sx_object.lo_name, file, line));
|
2004-01-28 20:39:57 +00:00
|
|
|
WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
|
|
|
|
line);
|
2001-03-06 06:17:05 +00:00
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
/* Loop in case we lose the race for lock acquisition. */
|
|
|
|
while (sx->sx_cnt != 0) {
|
|
|
|
sx->sx_excl_wcnt++;
|
2007-02-26 08:26:44 +00:00
|
|
|
lock_profile_obtain_lock_failed(&sx->sx_object, &contested, &waittime);
|
2001-11-13 21:55:13 +00:00
|
|
|
cv_wait(&sx->sx_excl_cv, sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
sx->sx_excl_wcnt--;
|
|
|
|
}
|
|
|
|
|
2001-03-06 06:17:05 +00:00
|
|
|
MPASS(sx->sx_cnt == 0);
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
/* Acquire an exclusive lock. */
|
|
|
|
sx->sx_cnt--;
|
2001-09-12 08:38:13 +00:00
|
|
|
sx->sx_xholder = curthread;
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2007-02-26 08:26:44 +00:00
|
|
|
lock_profile_obtain_lock_success(&sx->sx_object, contested, waittime, file, line);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
|
2001-05-04 17:15:16 +00:00
|
|
|
WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks++;
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
|
|
|
|
2001-06-27 06:39:37 +00:00
|
|
|
int
|
|
|
|
_sx_try_xlock(struct sx *sx, const char *file, int line)
|
|
|
|
{
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
if (sx->sx_cnt == 0) {
|
|
|
|
sx->sx_cnt--;
|
2001-09-12 08:38:13 +00:00
|
|
|
sx->sx_xholder = curthread;
|
2001-06-27 06:39:37 +00:00
|
|
|
LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
|
|
|
|
WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
|
|
|
|
line);
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks++;
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-06-27 06:39:37 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
void
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
_sx_sunlock(struct sx *sx, const char *file, int line)
|
2001-03-05 19:59:41 +00:00
|
|
|
{
|
|
|
|
|
2001-10-23 22:39:11 +00:00
|
|
|
_sx_assert(sx, SX_SLOCKED, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks--;
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
|
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
/* Release. */
|
|
|
|
sx->sx_cnt--;
|
|
|
|
|
2006-11-11 03:18:07 +00:00
|
|
|
if (sx->sx_cnt == 0)
|
|
|
|
lock_profile_release_lock(&sx->sx_object);
|
2001-03-05 19:59:41 +00:00
|
|
|
/*
|
|
|
|
* If we just released the last shared lock, wake any waiters up, giving
|
|
|
|
* exclusive lockers precedence. In order to make sure that exclusive
|
|
|
|
* lockers won't be blocked forever, don't wake shared lock waiters if
|
|
|
|
* there are exclusive lock waiters.
|
|
|
|
*/
|
|
|
|
if (sx->sx_excl_wcnt > 0) {
|
|
|
|
if (sx->sx_cnt == 0)
|
|
|
|
cv_signal(&sx->sx_excl_cv);
|
|
|
|
} else if (sx->sx_shrd_wcnt > 0)
|
|
|
|
cv_broadcast(&sx->sx_shrd_cv);
|
|
|
|
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
_sx_xunlock(struct sx *sx, const char *file, int line)
|
2001-03-05 19:59:41 +00:00
|
|
|
{
|
|
|
|
|
2001-10-23 22:39:11 +00:00
|
|
|
_sx_assert(sx, SX_XLOCKED, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-03-06 06:17:05 +00:00
|
|
|
MPASS(sx->sx_cnt == -1);
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2006-07-27 21:45:55 +00:00
|
|
|
curthread->td_locks--;
|
2001-05-04 17:15:16 +00:00
|
|
|
WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
|
2001-03-05 19:59:41 +00:00
|
|
|
/* Release. */
|
|
|
|
sx->sx_cnt++;
|
2001-03-06 06:17:05 +00:00
|
|
|
sx->sx_xholder = NULL;
|
2001-03-05 19:59:41 +00:00
|
|
|
|
2006-11-11 03:18:07 +00:00
|
|
|
lock_profile_release_lock(&sx->sx_object);
|
2001-03-05 19:59:41 +00:00
|
|
|
/*
|
|
|
|
* Wake up waiters if there are any. Give precedence to slock waiters.
|
|
|
|
*/
|
|
|
|
if (sx->sx_shrd_wcnt > 0)
|
|
|
|
cv_broadcast(&sx->sx_shrd_cv);
|
|
|
|
else if (sx->sx_excl_wcnt > 0)
|
|
|
|
cv_signal(&sx->sx_excl_cv);
|
|
|
|
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-03-05 19:59:41 +00:00
|
|
|
}
|
2001-08-13 21:25:30 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
_sx_try_upgrade(struct sx *sx, const char *file, int line)
|
|
|
|
{
|
|
|
|
|
2001-10-23 22:39:11 +00:00
|
|
|
_sx_assert(sx, SX_SLOCKED, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-08-13 21:25:30 +00:00
|
|
|
|
|
|
|
if (sx->sx_cnt == 1) {
|
|
|
|
sx->sx_cnt = -1;
|
2001-09-12 08:38:13 +00:00
|
|
|
sx->sx_xholder = curthread;
|
2001-08-13 21:25:30 +00:00
|
|
|
|
|
|
|
LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 1, file, line);
|
2001-08-23 22:51:22 +00:00
|
|
|
WITNESS_UPGRADE(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
|
|
|
|
file, line);
|
2001-08-13 21:25:30 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-08-13 21:25:30 +00:00
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 0, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-08-13 21:25:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_sx_downgrade(struct sx *sx, const char *file, int line)
|
|
|
|
{
|
|
|
|
|
2001-10-23 22:39:11 +00:00
|
|
|
_sx_assert(sx, SX_XLOCKED, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-08-13 21:25:30 +00:00
|
|
|
MPASS(sx->sx_cnt == -1);
|
|
|
|
|
2001-08-23 22:51:22 +00:00
|
|
|
WITNESS_DOWNGRADE(&sx->sx_object, 0, file, line);
|
2001-08-13 21:25:30 +00:00
|
|
|
|
|
|
|
sx->sx_cnt = 1;
|
2001-08-23 17:57:37 +00:00
|
|
|
sx->sx_xholder = NULL;
|
2001-08-13 21:25:30 +00:00
|
|
|
if (sx->sx_shrd_wcnt > 0)
|
|
|
|
cv_broadcast(&sx->sx_shrd_cv);
|
|
|
|
|
|
|
|
LOCK_LOG_LOCK("XDOWNGRADE", &sx->sx_object, 0, 0, file, line);
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-08-13 21:25:30 +00:00
|
|
|
}
|
2001-10-23 22:39:11 +00:00
|
|
|
|
|
|
|
#ifdef INVARIANT_SUPPORT
|
2001-10-24 14:18:33 +00:00
|
|
|
#ifndef INVARIANTS
|
|
|
|
#undef _sx_assert
|
|
|
|
#endif
|
|
|
|
|
2001-10-23 22:39:11 +00:00
|
|
|
/*
|
|
|
|
* In the non-WITNESS case, sx_assert() can only detect that at least
|
|
|
|
* *some* thread owns an slock, but it cannot guarantee that *this*
|
|
|
|
* thread owns an slock.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_sx_assert(struct sx *sx, int what, const char *file, int line)
|
|
|
|
{
|
|
|
|
|
2004-02-27 16:13:44 +00:00
|
|
|
if (panicstr != NULL)
|
|
|
|
return;
|
2001-10-23 22:39:11 +00:00
|
|
|
switch (what) {
|
|
|
|
case SX_LOCKED:
|
|
|
|
case SX_SLOCKED:
|
|
|
|
#ifdef WITNESS
|
|
|
|
witness_assert(&sx->sx_object, what, file, line);
|
|
|
|
#else
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-10-23 22:39:11 +00:00
|
|
|
if (sx->sx_cnt <= 0 &&
|
2002-01-18 09:21:15 +00:00
|
|
|
(what == SX_SLOCKED || sx->sx_xholder != curthread))
|
2004-02-27 16:13:44 +00:00
|
|
|
panic("Lock %s not %slocked @ %s:%d\n",
|
2001-10-23 22:39:11 +00:00
|
|
|
sx->sx_object.lo_name, (what == SX_SLOCKED) ?
|
|
|
|
"share " : "", file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-10-23 22:39:11 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case SX_XLOCKED:
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2001-10-23 22:39:11 +00:00
|
|
|
if (sx->sx_xholder != curthread)
|
2004-02-27 16:13:44 +00:00
|
|
|
panic("Lock %s not exclusively locked @ %s:%d\n",
|
2001-10-23 22:39:11 +00:00
|
|
|
sx->sx_object.lo_name, file, line);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(sx->sx_lock);
|
2001-10-23 22:39:11 +00:00
|
|
|
break;
|
2004-02-04 08:14:58 +00:00
|
|
|
case SX_UNLOCKED:
|
|
|
|
#ifdef WITNESS
|
|
|
|
witness_assert(&sx->sx_object, what, file, line);
|
|
|
|
#else
|
2004-02-19 14:19:31 +00:00
|
|
|
/*
|
|
|
|
* We are able to check only exclusive lock here,
|
|
|
|
* we cannot assert that *this* thread owns slock.
|
|
|
|
*/
|
2004-02-04 08:14:58 +00:00
|
|
|
mtx_lock(sx->sx_lock);
|
2004-02-19 14:19:31 +00:00
|
|
|
if (sx->sx_xholder == curthread)
|
2004-02-27 16:13:44 +00:00
|
|
|
panic("Lock %s exclusively locked @ %s:%d\n",
|
2004-02-04 08:14:58 +00:00
|
|
|
sx->sx_object.lo_name, file, line);
|
|
|
|
mtx_unlock(sx->sx_lock);
|
|
|
|
#endif
|
|
|
|
break;
|
2001-10-23 22:39:11 +00:00
|
|
|
default:
|
|
|
|
panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* INVARIANT_SUPPORT */
|
2005-12-13 23:14:35 +00:00
|
|
|
|
|
|
|
#ifdef DDB
|
|
|
|
void
|
|
|
|
db_show_sx(struct lock_object *lock)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
struct sx *sx;
|
|
|
|
|
|
|
|
sx = (struct sx *)lock;
|
|
|
|
|
|
|
|
db_printf(" state: ");
|
|
|
|
if (sx->sx_cnt < 0) {
|
|
|
|
td = sx->sx_xholder;
|
|
|
|
db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
|
|
|
|
td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
|
|
|
|
} else if (sx->sx_cnt > 0)
|
|
|
|
db_printf("SLOCK: %d locks\n", sx->sx_cnt);
|
|
|
|
else
|
|
|
|
db_printf("UNLOCKED\n");
|
|
|
|
db_printf(" waiters: %d shared, %d exclusive\n", sx->sx_shrd_wcnt,
|
|
|
|
sx->sx_excl_wcnt);
|
|
|
|
}
|
2006-08-15 18:29:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if a thread that is blocked on a sleep queue is actually
|
|
|
|
* blocked on an sx lock. If so, output some details and return true.
|
|
|
|
* If the lock has an exclusive owner, return that in *ownerp.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sx_chain(struct thread *td, struct thread **ownerp)
|
|
|
|
{
|
|
|
|
struct sx *sx;
|
|
|
|
struct cv *cv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, see if it looks like td is blocked on a condition
|
|
|
|
* variable.
|
|
|
|
*/
|
|
|
|
cv = td->td_wchan;
|
|
|
|
if (cv->cv_description != td->td_wmesg)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, see if it looks like td is blocked on the exclusive
|
|
|
|
* condition variable.
|
|
|
|
*/
|
|
|
|
sx = (struct sx *)((char *)cv - offsetof(struct sx, sx_excl_cv));
|
|
|
|
if (LOCK_CLASS(&sx->sx_object) == &lock_class_sx &&
|
|
|
|
sx->sx_excl_wcnt > 0)
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second, see if it looks like td is blocked on the shared
|
|
|
|
* condition variable.
|
|
|
|
*/
|
|
|
|
sx = (struct sx *)((char *)cv - offsetof(struct sx, sx_shrd_cv));
|
|
|
|
if (LOCK_CLASS(&sx->sx_object) == &lock_class_sx &&
|
|
|
|
sx->sx_shrd_wcnt > 0)
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
/* Doesn't seem to be an sx lock. */
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
ok:
|
|
|
|
/* We think we have an sx lock, so output some details. */
|
|
|
|
db_printf("blocked on sx \"%s\" ", td->td_wmesg);
|
|
|
|
if (sx->sx_cnt >= 0) {
|
|
|
|
db_printf("SLOCK (count %d)\n", sx->sx_cnt);
|
|
|
|
*ownerp = NULL;
|
|
|
|
} else {
|
|
|
|
db_printf("XLOCK\n");
|
|
|
|
*ownerp = sx->sx_xholder;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
2005-12-13 23:14:35 +00:00
|
|
|
#endif
|