[BACK]Return to kern_mutex.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/kern_mutex.c, Revision 1.72

1.72    ! ozaki-r     1: /*     $NetBSD: kern_mutex.c,v 1.71 2018/02/05 04:25:04 ozaki-r Exp $  */
1.2       ad          2:
                      3: /*-
1.30      ad          4:  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
1.2       ad          5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jason R. Thorpe and Andrew Doran.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
                     32: /*
                     33:  * Kernel mutex implementation, modeled after those found in Solaris,
                     34:  * a description of which can be found in:
                     35:  *
                     36:  *     Solaris Internals: Core Kernel Architecture, Jim Mauro and
                     37:  *         Richard McDougall.
                     38:  */
                     39:
                     40: #define        __MUTEX_PRIVATE
                     41:
                     42: #include <sys/cdefs.h>
1.72    ! ozaki-r    43: __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.71 2018/02/05 04:25:04 ozaki-r Exp $");
1.2       ad         44:
                     45: #include <sys/param.h>
1.46      pooka      46: #include <sys/atomic.h>
1.2       ad         47: #include <sys/proc.h>
                     48: #include <sys/mutex.h>
                     49: #include <sys/sched.h>
                     50: #include <sys/sleepq.h>
                     51: #include <sys/systm.h>
                     52: #include <sys/lockdebug.h>
                     53: #include <sys/kernel.h>
1.24      ad         54: #include <sys/intr.h>
1.29      xtraeme    55: #include <sys/lock.h>
1.50      rmind      56: #include <sys/types.h>
1.72    ! ozaki-r    57: #include <sys/cpu.h>
1.2       ad         58:
                     59: #include <dev/lockstat.h>
                     60:
1.28      ad         61: #include <machine/lock.h>
                     62:
1.2       ad         63: /*
                     64:  * When not running a debug kernel, spin mutexes are not much
                     65:  * more than an splraiseipl() and splx() pair.
                     66:  */
                     67:
                     68: #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                     69: #define        FULL
                     70: #endif
                     71:
                     72: /*
                     73:  * Debugging support.
                     74:  */
                     75:
                     76: #define        MUTEX_WANTLOCK(mtx)                                     \
1.23      yamt       77:     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),              \
1.54      mlelstv    78:         (uintptr_t)__builtin_return_address(0), 0)
1.65      pgoyette   79: #define        MUTEX_TESTLOCK(mtx)                                     \
                     80:     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),              \
                     81:         (uintptr_t)__builtin_return_address(0), -1)
1.2       ad         82: #define        MUTEX_LOCKED(mtx)                                       \
1.42      ad         83:     LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL,          \
1.2       ad         84:         (uintptr_t)__builtin_return_address(0), 0)
                     85: #define        MUTEX_UNLOCKED(mtx)                                     \
1.23      yamt       86:     LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx),              \
1.2       ad         87:         (uintptr_t)__builtin_return_address(0), 0)
                     88: #define        MUTEX_ABORT(mtx, msg)                                   \
1.64      christos   89:     mutex_abort(__func__, __LINE__, mtx, msg)
1.2       ad         90:
                     91: #if defined(LOCKDEBUG)
                     92:
                     93: #define        MUTEX_DASSERT(mtx, cond)                                \
                     94: do {                                                           \
                     95:        if (!(cond))                                            \
                     96:                MUTEX_ABORT(mtx, "assertion failed: " #cond);   \
                     97: } while (/* CONSTCOND */ 0);
                     98:
                     99: #else  /* LOCKDEBUG */
                    100:
                    101: #define        MUTEX_DASSERT(mtx, cond)        /* nothing */
                    102:
                    103: #endif /* LOCKDEBUG */
                    104:
                    105: #if defined(DIAGNOSTIC)
                    106:
                    107: #define        MUTEX_ASSERT(mtx, cond)                                 \
                    108: do {                                                           \
                    109:        if (!(cond))                                            \
                    110:                MUTEX_ABORT(mtx, "assertion failed: " #cond);   \
                    111: } while (/* CONSTCOND */ 0)
                    112:
                    113: #else  /* DIAGNOSTIC */
                    114:
                    115: #define        MUTEX_ASSERT(mtx, cond) /* nothing */
                    116:
                    117: #endif /* DIAGNOSTIC */
                    118:
                    119: /*
1.60      matt      120:  * Some architectures can't use __cpu_simple_lock as is so allow a way
                    121:  * for them to use an alternate definition.
                    122:  */
                    123: #ifndef MUTEX_SPINBIT_LOCK_INIT
                    124: #define MUTEX_SPINBIT_LOCK_INIT(mtx)   __cpu_simple_lock_init(&(mtx)->mtx_lock)
                    125: #endif
                    126: #ifndef MUTEX_SPINBIT_LOCKED_P
                    127: #define MUTEX_SPINBIT_LOCKED_P(mtx)    __SIMPLELOCK_LOCKED_P(&(mtx)->mtx_lock)
                    128: #endif
                    129: #ifndef MUTEX_SPINBIT_LOCK_TRY
                    130: #define MUTEX_SPINBIT_LOCK_TRY(mtx)    __cpu_simple_lock_try(&(mtx)->mtx_lock)
                    131: #endif
                    132: #ifndef MUTEX_SPINBIT_LOCK_UNLOCK
                    133: #define MUTEX_SPINBIT_LOCK_UNLOCK(mtx) __cpu_simple_unlock(&(mtx)->mtx_lock)
                    134: #endif
                    135:
                    136: #ifndef MUTEX_INITIALIZE_SPIN_IPL
                    137: #define MUTEX_INITIALIZE_SPIN_IPL(mtx, ipl) \
                    138:                                        ((mtx)->mtx_ipl = makeiplcookie((ipl)))
                    139: #endif
                    140:
                    141: /*
1.2       ad        142:  * Spin mutex SPL save / restore.
                    143:  */
                    144:
                    145: #define        MUTEX_SPIN_SPLRAISE(mtx)                                        \
                    146: do {                                                                   \
1.36      ad        147:        struct cpu_info *x__ci;                                         \
1.2       ad        148:        int x__cnt, s;                                                  \
1.60      matt      149:        s = splraiseipl(MUTEX_SPIN_IPL(mtx));                           \
1.36      ad        150:        x__ci = curcpu();                                               \
1.2       ad        151:        x__cnt = x__ci->ci_mtx_count--;                                 \
1.37      ad        152:        __insn_barrier();                                               \
1.51      rmind     153:        if (x__cnt == 0)                                                \
1.2       ad        154:                x__ci->ci_mtx_oldspl = (s);                             \
                    155: } while (/* CONSTCOND */ 0)
                    156:
                    157: #define        MUTEX_SPIN_SPLRESTORE(mtx)                                      \
                    158: do {                                                                   \
                    159:        struct cpu_info *x__ci = curcpu();                              \
                    160:        int s = x__ci->ci_mtx_oldspl;                                   \
                    161:        __insn_barrier();                                               \
1.51      rmind     162:        if (++(x__ci->ci_mtx_count) == 0)                       \
1.2       ad        163:                splx(s);                                                \
                    164: } while (/* CONSTCOND */ 0)
                    165:
                    166: /*
                    167:  * For architectures that provide 'simple' mutexes: they provide a
                    168:  * CAS function that is either MP-safe, or does not need to be MP
                    169:  * safe.  Adaptive mutexes on these architectures do not require an
                    170:  * additional interlock.
                    171:  */
                    172:
                    173: #ifdef __HAVE_SIMPLE_MUTEXES
                    174:
                    175: #define        MUTEX_OWNER(owner)                                              \
                    176:        (owner & MUTEX_THREAD)
                    177: #define        MUTEX_HAS_WAITERS(mtx)                                          \
                    178:        (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
                    179:
1.23      yamt      180: #define        MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug)                         \
1.49      skrll     181:        if (!dodebug)                                                   \
                    182:                (mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;                  \
1.2       ad        183: do {                                                                   \
                    184: } while (/* CONSTCOND */ 0);
                    185:
1.23      yamt      186: #define        MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl)                        \
1.2       ad        187: do {                                                                   \
                    188:        (mtx)->mtx_owner = MUTEX_BIT_SPIN;                              \
1.49      skrll     189:        if (!dodebug)                                                   \
                    190:                (mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;                  \
1.60      matt      191:        MUTEX_INITIALIZE_SPIN_IPL((mtx), (ipl));                        \
                    192:        MUTEX_SPINBIT_LOCK_INIT((mtx));                                 \
1.2       ad        193: } while (/* CONSTCOND */ 0)
                    194:
                    195: #define        MUTEX_DESTROY(mtx)                                              \
                    196: do {                                                                   \
                    197:        (mtx)->mtx_owner = MUTEX_THREAD;                                \
                    198: } while (/* CONSTCOND */ 0);
                    199:
                    200: #define        MUTEX_SPIN_P(mtx)               \
                    201:     (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
                    202: #define        MUTEX_ADAPTIVE_P(mtx)           \
                    203:     (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
                    204:
1.49      skrll     205: #define        MUTEX_DEBUG_P(mtx)      (((mtx)->mtx_owner & MUTEX_BIT_NODEBUG) == 0)
1.23      yamt      206: #if defined(LOCKDEBUG)
1.49      skrll     207: #define        MUTEX_OWNED(owner)              (((owner) & ~MUTEX_BIT_NODEBUG) != 0)
1.59      matt      208: #define        MUTEX_INHERITDEBUG(n, o)        (n) |= (o) & MUTEX_BIT_NODEBUG
1.23      yamt      209: #else /* defined(LOCKDEBUG) */
                    210: #define        MUTEX_OWNED(owner)              ((owner) != 0)
1.59      matt      211: #define        MUTEX_INHERITDEBUG(n, o)        /* nothing */
1.23      yamt      212: #endif /* defined(LOCKDEBUG) */
1.2       ad        213:
                    214: static inline int
                    215: MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
                    216: {
                    217:        int rv;
1.59      matt      218:        uintptr_t oldown = 0;
                    219:        uintptr_t newown = curthread;
1.23      yamt      220:
1.59      matt      221:        MUTEX_INHERITDEBUG(oldown, mtx->mtx_owner);
                    222:        MUTEX_INHERITDEBUG(newown, oldown);
                    223:        rv = MUTEX_CAS(&mtx->mtx_owner, oldown, newown);
1.7       itohy     224:        MUTEX_RECEIVE(mtx);
1.2       ad        225:        return rv;
                    226: }
                    227:
                    228: static inline int
                    229: MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
                    230: {
                    231:        int rv;
                    232:        rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
1.7       itohy     233:        MUTEX_RECEIVE(mtx);
1.2       ad        234:        return rv;
                    235: }
                    236:
                    237: static inline void
                    238: MUTEX_RELEASE(kmutex_t *mtx)
                    239: {
1.59      matt      240:        uintptr_t newown;
1.23      yamt      241:
1.7       itohy     242:        MUTEX_GIVE(mtx);
1.59      matt      243:        newown = 0;
                    244:        MUTEX_INHERITDEBUG(newown, mtx->mtx_owner);
                    245:        mtx->mtx_owner = newown;
1.2       ad        246: }
                    247: #endif /* __HAVE_SIMPLE_MUTEXES */
                    248:
                    249: /*
                    250:  * Patch in stubs via strong alias where they are not available.
                    251:  */
                    252:
                    253: #if defined(LOCKDEBUG)
                    254: #undef __HAVE_MUTEX_STUBS
                    255: #undef __HAVE_SPIN_MUTEX_STUBS
                    256: #endif
                    257:
                    258: #ifndef __HAVE_MUTEX_STUBS
1.8       itohy     259: __strong_alias(mutex_enter,mutex_vector_enter);
                    260: __strong_alias(mutex_exit,mutex_vector_exit);
1.2       ad        261: #endif
                    262:
                    263: #ifndef __HAVE_SPIN_MUTEX_STUBS
1.8       itohy     264: __strong_alias(mutex_spin_enter,mutex_vector_enter);
                    265: __strong_alias(mutex_spin_exit,mutex_vector_exit);
1.2       ad        266: #endif
                    267:
1.67      christos  268: static void    mutex_abort(const char *, size_t, const kmutex_t *,
                    269:     const char *);
                    270: static void    mutex_dump(const volatile void *);
1.2       ad        271:
                    272: lockops_t mutex_spin_lockops = {
1.68      ozaki-r   273:        .lo_name = "Mutex",
                    274:        .lo_type = LOCKOPS_SPIN,
                    275:        .lo_dump = mutex_dump,
1.2       ad        276: };
                    277:
                    278: lockops_t mutex_adaptive_lockops = {
1.68      ozaki-r   279:        .lo_name = "Mutex",
                    280:        .lo_type = LOCKOPS_SLEEP,
                    281:        .lo_dump = mutex_dump,
1.2       ad        282: };
                    283:
1.5       yamt      284: syncobj_t mutex_syncobj = {
1.70      ozaki-r   285:        .sobj_flag      = SOBJ_SLEEPQ_SORTED,
                    286:        .sobj_unsleep   = turnstile_unsleep,
                    287:        .sobj_changepri = turnstile_changepri,
                    288:        .sobj_lendpri   = sleepq_lendpri,
                    289:        .sobj_owner     = (void *)mutex_owner,
1.5       yamt      290: };
                    291:
1.2       ad        292: /*
                    293:  * mutex_dump:
                    294:  *
                    295:  *     Dump the contents of a mutex structure.
                    296:  */
                    297: void
1.67      christos  298: mutex_dump(const volatile void *cookie)
1.2       ad        299: {
1.67      christos  300:        const volatile kmutex_t *mtx = cookie;
1.2       ad        301:
                    302:        printf_nolog("owner field  : %#018lx wait/spin: %16d/%d\n",
                    303:            (long)MUTEX_OWNER(mtx->mtx_owner), MUTEX_HAS_WAITERS(mtx),
                    304:            MUTEX_SPIN_P(mtx));
                    305: }
                    306:
                    307: /*
                    308:  * mutex_abort:
                    309:  *
1.3       ad        310:  *     Dump information about an error and panic the system.  This
                    311:  *     generates a lot of machine code in the DIAGNOSTIC case, so
                    312:  *     we ask the compiler to not inline it.
1.2       ad        313:  */
1.43      ad        314: void __noinline
1.67      christos  315: mutex_abort(const char *func, size_t line, const kmutex_t *mtx, const char *msg)
1.2       ad        316: {
                    317:
1.64      christos  318:        LOCKDEBUG_ABORT(func, line, mtx, (MUTEX_SPIN_P(mtx) ?
                    319:            &mutex_spin_lockops : &mutex_adaptive_lockops), msg);
1.2       ad        320: }
                    321:
                    322: /*
                    323:  * mutex_init:
                    324:  *
                    325:  *     Initialize a mutex for use.  Note that adaptive mutexes are in
                    326:  *     essence spin mutexes that can sleep to avoid deadlock and wasting
                    327:  *     CPU time.  We can't easily provide a type of mutex that always
                    328:  *     sleeps - see comments in mutex_vector_enter() about releasing
                    329:  *     mutexes unlocked.
                    330:  */
1.71      ozaki-r   331: void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t);
1.2       ad        332: void
1.71      ozaki-r   333: _mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl,
                    334:     uintptr_t return_address)
1.2       ad        335: {
1.23      yamt      336:        bool dodebug;
1.2       ad        337:
                    338:        memset(mtx, 0, sizeof(*mtx));
                    339:
1.15      ad        340:        switch (type) {
                    341:        case MUTEX_ADAPTIVE:
                    342:                KASSERT(ipl == IPL_NONE);
                    343:                break;
1.22      ad        344:        case MUTEX_DEFAULT:
1.15      ad        345:        case MUTEX_DRIVER:
1.26      ad        346:                if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
                    347:                    ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
                    348:                    ipl == IPL_SOFTSERIAL) {
1.22      ad        349:                        type = MUTEX_ADAPTIVE;
1.26      ad        350:                } else {
1.22      ad        351:                        type = MUTEX_SPIN;
                    352:                }
1.15      ad        353:                break;
                    354:        default:
                    355:                break;
                    356:        }
1.2       ad        357:
                    358:        switch (type) {
1.11      ad        359:        case MUTEX_NODEBUG:
1.71      ozaki-r   360:                dodebug = LOCKDEBUG_ALLOC(mtx, NULL, return_address);
1.23      yamt      361:                MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
1.11      ad        362:                break;
1.2       ad        363:        case MUTEX_ADAPTIVE:
1.23      yamt      364:                dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_adaptive_lockops,
1.71      ozaki-r   365:                    return_address);
1.23      yamt      366:                MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
1.2       ad        367:                break;
                    368:        case MUTEX_SPIN:
1.23      yamt      369:                dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_spin_lockops,
1.71      ozaki-r   370:                    return_address);
1.23      yamt      371:                MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
1.2       ad        372:                break;
                    373:        default:
                    374:                panic("mutex_init: impossible type");
                    375:                break;
                    376:        }
                    377: }
                    378:
1.71      ozaki-r   379: void
                    380: mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
                    381: {
                    382:
                    383:        _mutex_init(mtx, type, ipl, (uintptr_t)__builtin_return_address(0));
                    384: }
                    385:
1.2       ad        386: /*
                    387:  * mutex_destroy:
                    388:  *
                    389:  *     Tear down a mutex.
                    390:  */
                    391: void
                    392: mutex_destroy(kmutex_t *mtx)
                    393: {
                    394:
                    395:        if (MUTEX_ADAPTIVE_P(mtx)) {
                    396:                MUTEX_ASSERT(mtx, !MUTEX_OWNED(mtx->mtx_owner) &&
                    397:                    !MUTEX_HAS_WAITERS(mtx));
                    398:        } else {
1.60      matt      399:                MUTEX_ASSERT(mtx, !MUTEX_SPINBIT_LOCKED_P(mtx));
1.2       ad        400:        }
                    401:
1.23      yamt      402:        LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
1.2       ad        403:        MUTEX_DESTROY(mtx);
                    404: }
                    405:
1.50      rmind     406: #ifdef MULTIPROCESSOR
1.2       ad        407: /*
1.50      rmind     408:  * mutex_oncpu:
1.2       ad        409:  *
                    410:  *     Return true if an adaptive mutex owner is running on a CPU in the
                    411:  *     system.  If the target is waiting on the kernel big lock, then we
1.15      ad        412:  *     must release it.  This is necessary to avoid deadlock.
1.2       ad        413:  */
1.50      rmind     414: static bool
                    415: mutex_oncpu(uintptr_t owner)
1.2       ad        416: {
                    417:        struct cpu_info *ci;
1.50      rmind     418:        lwp_t *l;
1.2       ad        419:
1.50      rmind     420:        KASSERT(kpreempt_disabled());
                    421:
                    422:        if (!MUTEX_OWNED(owner)) {
                    423:                return false;
                    424:        }
1.2       ad        425:
1.50      rmind     426:        /*
                    427:         * See lwp_dtor() why dereference of the LWP pointer is safe.
                    428:         * We must have kernel preemption disabled for that.
                    429:         */
                    430:        l = (lwp_t *)MUTEX_OWNER(owner);
                    431:        ci = l->l_cpu;
1.2       ad        432:
1.50      rmind     433:        if (ci && ci->ci_curlwp == l) {
                    434:                /* Target is running; do we need to block? */
                    435:                return (ci->ci_biglock_wanted != l);
                    436:        }
1.15      ad        437:
1.50      rmind     438:        /* Not running.  It may be safe to block now. */
                    439:        return false;
1.2       ad        440: }
1.15      ad        441: #endif /* MULTIPROCESSOR */
1.2       ad        442:
                    443: /*
                    444:  * mutex_vector_enter:
                    445:  *
1.45      rmind     446:  *     Support routine for mutex_enter() that must handle all cases.  In
1.2       ad        447:  *     the LOCKDEBUG case, mutex_enter() is always aliased here, even if
1.62      prlw1     448:  *     fast-path stubs are available.  If a mutex_spin_enter() stub is
1.2       ad        449:  *     not available, then it is also aliased directly here.
                    450:  */
                    451: void
                    452: mutex_vector_enter(kmutex_t *mtx)
                    453: {
                    454:        uintptr_t owner, curthread;
                    455:        turnstile_t *ts;
                    456: #ifdef MULTIPROCESSOR
                    457:        u_int count;
                    458: #endif
                    459:        LOCKSTAT_COUNTER(spincnt);
                    460:        LOCKSTAT_COUNTER(slpcnt);
                    461:        LOCKSTAT_TIMER(spintime);
                    462:        LOCKSTAT_TIMER(slptime);
                    463:        LOCKSTAT_FLAG(lsflag);
                    464:
                    465:        /*
                    466:         * Handle spin mutexes.
                    467:         */
                    468:        if (MUTEX_SPIN_P(mtx)) {
                    469: #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
                    470:                u_int spins = 0;
                    471: #endif
                    472:                MUTEX_SPIN_SPLRAISE(mtx);
                    473:                MUTEX_WANTLOCK(mtx);
                    474: #ifdef FULL
1.60      matt      475:                if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
1.2       ad        476:                        MUTEX_LOCKED(mtx);
                    477:                        return;
                    478:                }
                    479: #if !defined(MULTIPROCESSOR)
                    480:                MUTEX_ABORT(mtx, "locking against myself");
                    481: #else /* !MULTIPROCESSOR */
                    482:
                    483:                LOCKSTAT_ENTER(lsflag);
                    484:                LOCKSTAT_START_TIMER(lsflag, spintime);
                    485:                count = SPINLOCK_BACKOFF_MIN;
                    486:
                    487:                /*
                    488:                 * Spin testing the lock word and do exponential backoff
                    489:                 * to reduce cache line ping-ponging between CPUs.
                    490:                 */
                    491:                do {
                    492:                        if (panicstr != NULL)
                    493:                                break;
1.60      matt      494:                        while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
1.63      msaitoh   495:                                SPINLOCK_BACKOFF(count);
1.2       ad        496: #ifdef LOCKDEBUG
                    497:                                if (SPINLOCK_SPINOUT(spins))
                    498:                                        MUTEX_ABORT(mtx, "spinout");
                    499: #endif /* LOCKDEBUG */
                    500:                        }
1.60      matt      501:                } while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
1.2       ad        502:
                    503:                if (count != SPINLOCK_BACKOFF_MIN) {
                    504:                        LOCKSTAT_STOP_TIMER(lsflag, spintime);
                    505:                        LOCKSTAT_EVENT(lsflag, mtx,
                    506:                            LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
                    507:                }
                    508:                LOCKSTAT_EXIT(lsflag);
                    509: #endif /* !MULTIPROCESSOR */
                    510: #endif /* FULL */
                    511:                MUTEX_LOCKED(mtx);
                    512:                return;
                    513:        }
                    514:
                    515:        curthread = (uintptr_t)curlwp;
                    516:
                    517:        MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
                    518:        MUTEX_ASSERT(mtx, curthread != 0);
1.72    ! ozaki-r   519:        MUTEX_ASSERT(mtx, !cpu_intr_p());
1.2       ad        520:        MUTEX_WANTLOCK(mtx);
                    521:
                    522:        if (panicstr == NULL) {
                    523:                LOCKDEBUG_BARRIER(&kernel_lock, 1);
                    524:        }
                    525:
                    526:        LOCKSTAT_ENTER(lsflag);
                    527:
                    528:        /*
                    529:         * Adaptive mutex; spin trying to acquire the mutex.  If we
                    530:         * determine that the owner is not running on a processor,
                    531:         * then we stop spinning, and sleep instead.
                    532:         */
1.50      rmind     533:        KPREEMPT_DISABLE(curlwp);
1.34      ad        534:        for (owner = mtx->mtx_owner;;) {
1.2       ad        535:                if (!MUTEX_OWNED(owner)) {
                    536:                        /*
                    537:                         * Mutex owner clear could mean two things:
                    538:                         *
                    539:                         *      * The mutex has been released.
                    540:                         *      * The owner field hasn't been set yet.
                    541:                         *
                    542:                         * Try to acquire it again.  If that fails,
                    543:                         * we'll just loop again.
                    544:                         */
                    545:                        if (MUTEX_ACQUIRE(mtx, curthread))
                    546:                                break;
1.34      ad        547:                        owner = mtx->mtx_owner;
1.2       ad        548:                        continue;
                    549:                }
1.50      rmind     550:                if (__predict_false(panicstr != NULL)) {
1.61      uebayasi  551:                        KPREEMPT_ENABLE(curlwp);
1.2       ad        552:                        return;
1.50      rmind     553:                }
                    554:                if (__predict_false(MUTEX_OWNER(owner) == curthread)) {
1.2       ad        555:                        MUTEX_ABORT(mtx, "locking against myself");
1.50      rmind     556:                }
1.2       ad        557: #ifdef MULTIPROCESSOR
                    558:                /*
                    559:                 * Check to see if the owner is running on a processor.
                    560:                 * If so, then we should just spin, as the owner will
                    561:                 * likely release the lock very soon.
                    562:                 */
1.50      rmind     563:                if (mutex_oncpu(owner)) {
1.2       ad        564:                        LOCKSTAT_START_TIMER(lsflag, spintime);
                    565:                        count = SPINLOCK_BACKOFF_MIN;
1.50      rmind     566:                        do {
1.53      rmind     567:                                KPREEMPT_ENABLE(curlwp);
1.34      ad        568:                                SPINLOCK_BACKOFF(count);
1.53      rmind     569:                                KPREEMPT_DISABLE(curlwp);
1.2       ad        570:                                owner = mtx->mtx_owner;
1.50      rmind     571:                        } while (mutex_oncpu(owner));
1.2       ad        572:                        LOCKSTAT_STOP_TIMER(lsflag, spintime);
                    573:                        LOCKSTAT_COUNT(spincnt, 1);
                    574:                        if (!MUTEX_OWNED(owner))
                    575:                                continue;
                    576:                }
                    577: #endif
                    578:
                    579:                ts = turnstile_lookup(mtx);
                    580:
                    581:                /*
                    582:                 * Once we have the turnstile chain interlock, mark the
1.69      skrll     583:                 * mutex as having waiters.  If that fails, spin again:
1.2       ad        584:                 * chances are that the mutex has been released.
                    585:                 */
                    586:                if (!MUTEX_SET_WAITERS(mtx, owner)) {
                    587:                        turnstile_exit(mtx);
1.34      ad        588:                        owner = mtx->mtx_owner;
1.2       ad        589:                        continue;
                    590:                }
                    591:
                    592: #ifdef MULTIPROCESSOR
                    593:                /*
                    594:                 * mutex_exit() is permitted to release the mutex without
                    595:                 * any interlocking instructions, and the following can
                    596:                 * occur as a result:
                    597:                 *
                    598:                 *  CPU 1: MUTEX_SET_WAITERS()      CPU2: mutex_exit()
                    599:                 * ---------------------------- ----------------------------
                    600:                 *              ..                  acquire cache line
                    601:                 *              ..                   test for waiters
                    602:                 *      acquire cache line    <-      lose cache line
                    603:                 *       lock cache line                   ..
                    604:                 *     verify mutex is held                ..
                    605:                 *          set waiters                    ..
                    606:                 *       unlock cache line                 ..
                    607:                 *        lose cache line     ->    acquire cache line
                    608:                 *              ..                clear lock word, waiters
                    609:                 *        return success
                    610:                 *
1.50      rmind     611:                 * There is another race that can occur: a third CPU could
1.2       ad        612:                 * acquire the mutex as soon as it is released.  Since
                    613:                 * adaptive mutexes are primarily spin mutexes, this is not
                    614:                 * something that we need to worry about too much.  What we
                    615:                 * do need to ensure is that the waiters bit gets set.
                    616:                 *
                    617:                 * To allow the unlocked release, we need to make some
                    618:                 * assumptions here:
                    619:                 *
                    620:                 * o Release is the only non-atomic/unlocked operation
                    621:                 *   that can be performed on the mutex.  (It must still
                    622:                 *   be atomic on the local CPU, e.g. in case interrupted
                    623:                 *   or preempted).
                    624:                 *
                    625:                 * o At any given time, MUTEX_SET_WAITERS() can only ever
1.21      pooka     626:                 *   be in progress on one CPU in the system - guaranteed
1.2       ad        627:                 *   by the turnstile chain lock.
                    628:                 *
                    629:                 * o No other operations other than MUTEX_SET_WAITERS()
                    630:                 *   and release can modify a mutex with a non-zero
                    631:                 *   owner field.
                    632:                 *
                    633:                 * o The result of a successful MUTEX_SET_WAITERS() call
                    634:                 *   is an unbuffered write that is immediately visible
                    635:                 *   to all other processors in the system.
                    636:                 *
                    637:                 * o If the holding LWP switches away, it posts a store
                    638:                 *   fence before changing curlwp, ensuring that any
                    639:                 *   overwrite of the mutex waiters flag by mutex_exit()
                    640:                 *   completes before the modification of curlwp becomes
                    641:                 *   visible to this CPU.
                    642:                 *
1.14      yamt      643:                 * o mi_switch() posts a store fence before setting curlwp
1.2       ad        644:                 *   and before resuming execution of an LWP.
                    645:                 *
                    646:                 * o _kernel_lock() posts a store fence before setting
                    647:                 *   curcpu()->ci_biglock_wanted, and after clearing it.
                    648:                 *   This ensures that any overwrite of the mutex waiters
                    649:                 *   flag by mutex_exit() completes before the modification
                    650:                 *   of ci_biglock_wanted becomes visible.
                    651:                 *
                    652:                 * We now post a read memory barrier (after setting the
                    653:                 * waiters field) and check the lock holder's status again.
                    654:                 * Some of the possible outcomes (not an exhaustive list):
                    655:                 *
1.50      rmind     656:                 * 1. The on-CPU check returns true: the holding LWP is
1.2       ad        657:                 *    running again.  The lock may be released soon and
                    658:                 *    we should spin.  Importantly, we can't trust the
                    659:                 *    value of the waiters flag.
                    660:                 *
1.50      rmind     661:                 * 2. The on-CPU check returns false: the holding LWP is
1.39      yamt      662:                 *    not running.  We now have the opportunity to check
1.2       ad        663:                 *    if mutex_exit() has blatted the modifications made
                    664:                 *    by MUTEX_SET_WAITERS().
                    665:                 *
1.50      rmind     666:                 * 3. The on-CPU check returns false: the holding LWP may
1.2       ad        667:                 *    or may not be running.  It has context switched at
                    668:                 *    some point during our check.  Again, we have the
                    669:                 *    chance to see if the waiters bit is still set or
                    670:                 *    has been overwritten.
                    671:                 *
1.50      rmind     672:                 * 4. The on-CPU check returns false: the holding LWP is
1.2       ad        673:                 *    running on a CPU, but wants the big lock.  It's OK
                    674:                 *    to check the waiters field in this case.
                    675:                 *
                    676:                 * 5. The has-waiters check fails: the mutex has been
                    677:                 *    released, the waiters flag cleared and another LWP
                    678:                 *    now owns the mutex.
                    679:                 *
                    680:                 * 6. The has-waiters check fails: the mutex has been
                    681:                 *    released.
                    682:                 *
                    683:                 * If the waiters bit is not set it's unsafe to go asleep,
                    684:                 * as we might never be awoken.
                    685:                 */
1.50      rmind     686:                if ((membar_consumer(), mutex_oncpu(owner)) ||
1.24      ad        687:                    (membar_consumer(), !MUTEX_HAS_WAITERS(mtx))) {
1.2       ad        688:                        turnstile_exit(mtx);
1.34      ad        689:                        owner = mtx->mtx_owner;
1.2       ad        690:                        continue;
                    691:                }
                    692: #endif /* MULTIPROCESSOR */
                    693:
                    694:                LOCKSTAT_START_TIMER(lsflag, slptime);
                    695:
1.5       yamt      696:                turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
1.2       ad        697:
                    698:                LOCKSTAT_STOP_TIMER(lsflag, slptime);
                    699:                LOCKSTAT_COUNT(slpcnt, 1);
1.34      ad        700:
                    701:                owner = mtx->mtx_owner;
1.2       ad        702:        }
1.50      rmind     703:        KPREEMPT_ENABLE(curlwp);
1.2       ad        704:
                    705:        LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
                    706:            slpcnt, slptime);
                    707:        LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
                    708:            spincnt, spintime);
                    709:        LOCKSTAT_EXIT(lsflag);
                    710:
                    711:        MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
                    712:        MUTEX_LOCKED(mtx);
                    713: }
                    714:
                    715: /*
                    716:  * mutex_vector_exit:
                    717:  *
                    718:  *     Support routine for mutex_exit() that handles all cases.
                    719:  */
                    720: void
                    721: mutex_vector_exit(kmutex_t *mtx)
                    722: {
                    723:        turnstile_t *ts;
                    724:        uintptr_t curthread;
                    725:
                    726:        if (MUTEX_SPIN_P(mtx)) {
                    727: #ifdef FULL
1.60      matt      728:                if (__predict_false(!MUTEX_SPINBIT_LOCKED_P(mtx))) {
1.33      ad        729:                        if (panicstr != NULL)
                    730:                                return;
1.2       ad        731:                        MUTEX_ABORT(mtx, "exiting unheld spin mutex");
1.33      ad        732:                }
1.2       ad        733:                MUTEX_UNLOCKED(mtx);
1.60      matt      734:                MUTEX_SPINBIT_LOCK_UNLOCK(mtx);
1.2       ad        735: #endif
                    736:                MUTEX_SPIN_SPLRESTORE(mtx);
                    737:                return;
                    738:        }
                    739:
1.11      ad        740:        if (__predict_false((uintptr_t)panicstr | cold)) {
1.2       ad        741:                MUTEX_UNLOCKED(mtx);
                    742:                MUTEX_RELEASE(mtx);
                    743:                return;
                    744:        }
                    745:
                    746:        curthread = (uintptr_t)curlwp;
                    747:        MUTEX_DASSERT(mtx, curthread != 0);
                    748:        MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
                    749:        MUTEX_UNLOCKED(mtx);
1.58      mrg       750: #if !defined(LOCKDEBUG)
                    751:        __USE(curthread);
                    752: #endif
1.2       ad        753:
1.15      ad        754: #ifdef LOCKDEBUG
                    755:        /*
                    756:         * Avoid having to take the turnstile chain lock every time
                    757:         * around.  Raise the priority level to splhigh() in order
                    758:         * to disable preemption and so make the following atomic.
                    759:         */
                    760:        {
                    761:                int s = splhigh();
                    762:                if (!MUTEX_HAS_WAITERS(mtx)) {
                    763:                        MUTEX_RELEASE(mtx);
                    764:                        splx(s);
                    765:                        return;
                    766:                }
                    767:                splx(s);
                    768:        }
                    769: #endif
                    770:
1.2       ad        771:        /*
                    772:         * Get this lock's turnstile.  This gets the interlock on
                    773:         * the sleep queue.  Once we have that, we can clear the
                    774:         * lock.  If there was no turnstile for the lock, there
                    775:         * were no waiters remaining.
                    776:         */
                    777:        ts = turnstile_lookup(mtx);
                    778:
                    779:        if (ts == NULL) {
                    780:                MUTEX_RELEASE(mtx);
                    781:                turnstile_exit(mtx);
                    782:        } else {
                    783:                MUTEX_RELEASE(mtx);
                    784:                turnstile_wakeup(ts, TS_WRITER_Q,
                    785:                    TS_WAITERS(ts, TS_WRITER_Q), NULL);
                    786:        }
                    787: }
                    788:
1.4       ad        789: #ifndef __HAVE_SIMPLE_MUTEXES
                    790: /*
                    791:  * mutex_wakeup:
                    792:  *
                    793:  *     Support routine for mutex_exit() that wakes up all waiters.
                    794:  *     We assume that the mutex has been released, but it need not
                    795:  *     be.
                    796:  */
                    797: void
                    798: mutex_wakeup(kmutex_t *mtx)
                    799: {
                    800:        turnstile_t *ts;
                    801:
                    802:        ts = turnstile_lookup(mtx);
                    803:        if (ts == NULL) {
                    804:                turnstile_exit(mtx);
                    805:                return;
                    806:        }
                    807:        MUTEX_CLEAR_WAITERS(mtx);
                    808:        turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
                    809: }
                    810: #endif /* !__HAVE_SIMPLE_MUTEXES */
                    811:
1.2       ad        812: /*
                    813:  * mutex_owned:
                    814:  *
1.3       ad        815:  *     Return true if the current LWP (adaptive) or CPU (spin)
                    816:  *     holds the mutex.
1.2       ad        817:  */
                    818: int
1.66      christos  819: mutex_owned(const kmutex_t *mtx)
1.2       ad        820: {
                    821:
1.35      ad        822:        if (mtx == NULL)
                    823:                return 0;
1.2       ad        824:        if (MUTEX_ADAPTIVE_P(mtx))
                    825:                return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
                    826: #ifdef FULL
1.60      matt      827:        return MUTEX_SPINBIT_LOCKED_P(mtx);
1.2       ad        828: #else
                    829:        return 1;
                    830: #endif
                    831: }
                    832:
                    833: /*
                    834:  * mutex_owner:
                    835:  *
1.6       ad        836:  *     Return the current owner of an adaptive mutex.  Used for
                    837:  *     priority inheritance.
1.2       ad        838:  */
1.27      ad        839: lwp_t *
1.66      christos  840: mutex_owner(const kmutex_t *mtx)
1.2       ad        841: {
                    842:
                    843:        MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
                    844:        return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
                    845: }
                    846:
                    847: /*
1.65      pgoyette  848:  * mutex_ownable:
                    849:  *
                    850:  *     When compiled with DEBUG and LOCKDEBUG defined, ensure that
                    851:  *     the mutex is available.  We cannot use !mutex_owned() since
                    852:  *     that won't work correctly for spin mutexes.
                    853:  */
                    854: int
1.66      christos  855: mutex_ownable(const kmutex_t *mtx)
1.65      pgoyette  856: {
                    857:
                    858: #ifdef LOCKDEBUG
                    859:        MUTEX_TESTLOCK(mtx);
                    860: #endif
                    861:        return 1;
                    862: }
                    863:
                    864: /*
1.2       ad        865:  * mutex_tryenter:
                    866:  *
                    867:  *     Try to acquire the mutex; return non-zero if we did.
                    868:  */
                    869: int
                    870: mutex_tryenter(kmutex_t *mtx)
                    871: {
                    872:        uintptr_t curthread;
                    873:
                    874:        /*
                    875:         * Handle spin mutexes.
                    876:         */
                    877:        if (MUTEX_SPIN_P(mtx)) {
                    878:                MUTEX_SPIN_SPLRAISE(mtx);
                    879: #ifdef FULL
1.60      matt      880:                if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
1.4       ad        881:                        MUTEX_WANTLOCK(mtx);
1.2       ad        882:                        MUTEX_LOCKED(mtx);
                    883:                        return 1;
                    884:                }
                    885:                MUTEX_SPIN_SPLRESTORE(mtx);
                    886: #else
1.4       ad        887:                MUTEX_WANTLOCK(mtx);
1.2       ad        888:                MUTEX_LOCKED(mtx);
                    889:                return 1;
                    890: #endif
                    891:        } else {
                    892:                curthread = (uintptr_t)curlwp;
                    893:                MUTEX_ASSERT(mtx, curthread != 0);
                    894:                if (MUTEX_ACQUIRE(mtx, curthread)) {
1.4       ad        895:                        MUTEX_WANTLOCK(mtx);
1.2       ad        896:                        MUTEX_LOCKED(mtx);
                    897:                        MUTEX_DASSERT(mtx,
                    898:                            MUTEX_OWNER(mtx->mtx_owner) == curthread);
                    899:                        return 1;
                    900:                }
                    901:        }
                    902:
                    903:        return 0;
                    904: }
                    905:
                    906: #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
                    907: /*
                    908:  * mutex_spin_retry:
                    909:  *
                    910:  *     Support routine for mutex_spin_enter().  Assumes that the caller
                    911:  *     has already raised the SPL, and adjusted counters.
                    912:  */
                    913: void
                    914: mutex_spin_retry(kmutex_t *mtx)
                    915: {
                    916: #ifdef MULTIPROCESSOR
                    917:        u_int count;
                    918:        LOCKSTAT_TIMER(spintime);
                    919:        LOCKSTAT_FLAG(lsflag);
                    920: #ifdef LOCKDEBUG
                    921:        u_int spins = 0;
                    922: #endif /* LOCKDEBUG */
                    923:
                    924:        MUTEX_WANTLOCK(mtx);
                    925:
                    926:        LOCKSTAT_ENTER(lsflag);
                    927:        LOCKSTAT_START_TIMER(lsflag, spintime);
                    928:        count = SPINLOCK_BACKOFF_MIN;
                    929:
                    930:        /*
                    931:         * Spin testing the lock word and do exponential backoff
                    932:         * to reduce cache line ping-ponging between CPUs.
                    933:         */
                    934:        do {
                    935:                if (panicstr != NULL)
                    936:                        break;
1.60      matt      937:                while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
1.63      msaitoh   938:                        SPINLOCK_BACKOFF(count);
1.2       ad        939: #ifdef LOCKDEBUG
                    940:                        if (SPINLOCK_SPINOUT(spins))
                    941:                                MUTEX_ABORT(mtx, "spinout");
                    942: #endif /* LOCKDEBUG */
                    943:                }
1.60      matt      944:        } while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
1.2       ad        945:
                    946:        LOCKSTAT_STOP_TIMER(lsflag, spintime);
                    947:        LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
                    948:        LOCKSTAT_EXIT(lsflag);
                    949:
                    950:        MUTEX_LOCKED(mtx);
                    951: #else  /* MULTIPROCESSOR */
                    952:        MUTEX_ABORT(mtx, "locking against myself");
                    953: #endif /* MULTIPROCESSOR */
                    954: }
                    955: #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */

CVSweb <webmaster@jp.NetBSD.org>