[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.52

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

CVSweb <webmaster@jp.NetBSD.org>