1913ced85SMichael Neumann /* 2913ced85SMichael Neumann * Copyright (c) 2015 Michael Neumann <mneumann@ntecs.de> 3913ced85SMichael Neumann * All rights reserved. 4913ced85SMichael Neumann * 5913ced85SMichael Neumann * Redistribution and use in source and binary forms, with or without 6913ced85SMichael Neumann * modification, are permitted provided that the following conditions 7913ced85SMichael Neumann * are met: 8913ced85SMichael Neumann * 1. Redistributions of source code must retain the above copyright 9913ced85SMichael Neumann * notice unmodified, this list of conditions, and the following 10913ced85SMichael Neumann * disclaimer. 11913ced85SMichael Neumann * 2. Redistributions in binary form must reproduce the above copyright 12913ced85SMichael Neumann * notice, this list of conditions and the following disclaimer in the 13913ced85SMichael Neumann * documentation and/or other materials provided with the distribution. 14913ced85SMichael Neumann * 15913ced85SMichael Neumann * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16913ced85SMichael Neumann * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17913ced85SMichael Neumann * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18913ced85SMichael Neumann * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19913ced85SMichael Neumann * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20913ced85SMichael Neumann * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21913ced85SMichael Neumann * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22913ced85SMichael Neumann * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23913ced85SMichael Neumann * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24913ced85SMichael Neumann * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25913ced85SMichael Neumann */ 26913ced85SMichael Neumann 27913ced85SMichael Neumann #ifndef _LINUX_WW_MUTEX_H_ 28913ced85SMichael Neumann #define _LINUX_WW_MUTEX_H_ 29913ced85SMichael Neumann 30913ced85SMichael Neumann /* 31913ced85SMichael Neumann * A basic, unoptimized implementation of wound/wait mutexes for DragonFly 32913ced85SMichael Neumann * modelled after the Linux API [1]. 33913ced85SMichael Neumann * 34913ced85SMichael Neumann * [1]: http://lxr.free-electrons.com/source/include/linux/ww_mutex.h 35913ced85SMichael Neumann */ 36913ced85SMichael Neumann 37913ced85SMichael Neumann #include <sys/errno.h> 38913ced85SMichael Neumann #include <sys/types.h> 39913ced85SMichael Neumann #include <machine/atomic.h> 40913ced85SMichael Neumann #include <sys/spinlock.h> 41913ced85SMichael Neumann #include <sys/spinlock2.h> 42*6c27df07SSascha Wildner #include <sys/stdbool.h> 43913ced85SMichael Neumann 44913ced85SMichael Neumann struct ww_class { 45913ced85SMichael Neumann volatile u_long stamp; 46913ced85SMichael Neumann const char *name; 47913ced85SMichael Neumann }; 48913ced85SMichael Neumann 49913ced85SMichael Neumann struct ww_acquire_ctx { 50913ced85SMichael Neumann u_long stamp; 51913ced85SMichael Neumann struct ww_class *ww_class; 52913ced85SMichael Neumann }; 53913ced85SMichael Neumann 54913ced85SMichael Neumann struct ww_mutex { 55913ced85SMichael Neumann struct spinlock lock; 56913ced85SMichael Neumann volatile int acquired; 57913ced85SMichael Neumann volatile struct ww_acquire_ctx *ctx; 58913ced85SMichael Neumann volatile struct thread *owner; 59913ced85SMichael Neumann }; 60913ced85SMichael Neumann 61913ced85SMichael Neumann #define DEFINE_WW_CLASS(classname) \ 62913ced85SMichael Neumann struct ww_class classname { \ 63913ced85SMichael Neumann .stamp = 0, \ 64913ced85SMichael Neumann .name = #classname \ 65913ced85SMichael Neumann } 66913ced85SMichael Neumann 67913ced85SMichael Neumann static inline void 68913ced85SMichael Neumann ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class) { 69913ced85SMichael Neumann ctx->stamp = atomic_fetchadd_long(&ww_class->stamp, 1); 70913ced85SMichael Neumann ctx->ww_class = ww_class; 71913ced85SMichael Neumann } 72913ced85SMichael Neumann 73913ced85SMichael Neumann static inline void 74913ced85SMichael Neumann ww_acquire_done(__unused struct ww_acquire_ctx *ctx) { 75913ced85SMichael Neumann } 76913ced85SMichael Neumann 77913ced85SMichael Neumann static inline void 78913ced85SMichael Neumann ww_acquire_fini(__unused struct ww_acquire_ctx *ctx) { 79913ced85SMichael Neumann } 80913ced85SMichael Neumann 81913ced85SMichael Neumann static inline void 82913ced85SMichael Neumann ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class) { 83913ced85SMichael Neumann spin_init(&lock->lock, ww_class->name); 84913ced85SMichael Neumann lock->acquired = 0; 85913ced85SMichael Neumann lock->ctx = NULL; 86913ced85SMichael Neumann lock->owner = NULL; 87913ced85SMichael Neumann } 88913ced85SMichael Neumann 89913ced85SMichael Neumann static inline bool 90913ced85SMichael Neumann ww_mutex_is_locked(struct ww_mutex *lock) { 91913ced85SMichael Neumann bool res = false; 92913ced85SMichael Neumann spin_lock(&lock->lock); 93913ced85SMichael Neumann if (lock->acquired > 0) res = true; 94913ced85SMichael Neumann spin_unlock(&lock->lock); 95913ced85SMichael Neumann return res; 96913ced85SMichael Neumann } 97913ced85SMichael Neumann 98913ced85SMichael Neumann /* 99913ced85SMichael Neumann * Return 1 if lock could be acquired, else 0 (contended). 100913ced85SMichael Neumann */ 101913ced85SMichael Neumann static inline int 102913ced85SMichael Neumann ww_mutex_trylock(struct ww_mutex *lock) { 103913ced85SMichael Neumann int res = 1; 104913ced85SMichael Neumann KKASSERT(curthread); 105913ced85SMichael Neumann 106913ced85SMichael Neumann spin_lock(&lock->lock); 107913ced85SMichael Neumann /* 108913ced85SMichael Neumann * In case no one holds the ww_mutex yet, we acquire it. 109913ced85SMichael Neumann */ 110913ced85SMichael Neumann if (lock->acquired == 0) { 111913ced85SMichael Neumann KKASSERT(lock->ctx == NULL); 112913ced85SMichael Neumann lock->acquired += 1; 113913ced85SMichael Neumann lock->owner = curthread; 114913ced85SMichael Neumann } 115913ced85SMichael Neumann /* 116913ced85SMichael Neumann * In case we already hold the ww_mutex, increase a count. 117913ced85SMichael Neumann */ 118913ced85SMichael Neumann else if (lock->owner == curthread) { 119913ced85SMichael Neumann lock->acquired += 1; 120913ced85SMichael Neumann } 121913ced85SMichael Neumann else { 122913ced85SMichael Neumann res = 0; 123913ced85SMichael Neumann } 124913ced85SMichael Neumann spin_unlock(&lock->lock); 125913ced85SMichael Neumann return res; 126913ced85SMichael Neumann } 127913ced85SMichael Neumann 128913ced85SMichael Neumann /* 129913ced85SMichael Neumann * When `slow` is `true`, it will always block if the ww_mutex is contended. 130913ced85SMichael Neumann * It is assumed that the called will not hold any (ww_mutex) resources when 131913ced85SMichael Neumann * calling the slow path as this could lead to deadlocks. 132913ced85SMichael Neumann * 133913ced85SMichael Neumann * When `intr` is `true`, the ssleep will be interruptable. 134913ced85SMichael Neumann */ 135913ced85SMichael Neumann static inline int 136913ced85SMichael Neumann __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx, bool slow, bool intr) { 137913ced85SMichael Neumann int err; 138913ced85SMichael Neumann 139913ced85SMichael Neumann KKASSERT(curthread); 140913ced85SMichael Neumann 141913ced85SMichael Neumann spin_lock(&lock->lock); 142913ced85SMichael Neumann for (;;) { 143913ced85SMichael Neumann /* 144913ced85SMichael Neumann * In case no one holds the ww_mutex yet, we acquire it. 145913ced85SMichael Neumann */ 146913ced85SMichael Neumann if (lock->acquired == 0) { 147913ced85SMichael Neumann KKASSERT(lock->ctx == NULL); 148913ced85SMichael Neumann lock->acquired += 1; 149913ced85SMichael Neumann lock->ctx = ctx; 150913ced85SMichael Neumann lock->owner = curthread; 151913ced85SMichael Neumann err = 0; 152913ced85SMichael Neumann break; 153913ced85SMichael Neumann } 154913ced85SMichael Neumann /* 155913ced85SMichael Neumann * In case we already hold the ww_mutex, simply increase 156913ced85SMichael Neumann * a count and return -ALREADY. 157913ced85SMichael Neumann */ 158913ced85SMichael Neumann else if (lock->owner == curthread) { 159913ced85SMichael Neumann KKASSERT(lock->ctx == ctx); 160913ced85SMichael Neumann lock->acquired += 1; 161913ced85SMichael Neumann err = -EALREADY; 162913ced85SMichael Neumann break; 163913ced85SMichael Neumann } 164913ced85SMichael Neumann /* 165913ced85SMichael Neumann * This is the contention case where the ww_mutex is 166913ced85SMichael Neumann * already held by another context. 167913ced85SMichael Neumann */ 168913ced85SMichael Neumann else { 169913ced85SMichael Neumann /* 170913ced85SMichael Neumann * Three cases: 171913ced85SMichael Neumann * 172913ced85SMichael Neumann * - We are in the slow-path (first lock to obtain). 173913ced85SMichael Neumann * 174913ced85SMichael Neumann * - No context was specified. We assume a single 175913ced85SMichael Neumann * resouce, so there is no danger of a deadlock. 176913ced85SMichael Neumann * 177913ced85SMichael Neumann * - An `older` process (`ctx`) tries to acquire a 178913ced85SMichael Neumann * lock already held by a `younger` process. 179913ced85SMichael Neumann * We put the `older` process to sleep until 180913ced85SMichael Neumann * the `younger` process gives up all it's 181913ced85SMichael Neumann * resources. 182913ced85SMichael Neumann */ 183913ced85SMichael Neumann if (slow || ctx == NULL || ctx->stamp < lock->ctx->stamp) { 184913ced85SMichael Neumann int s = ssleep(lock, &lock->lock, 185913ced85SMichael Neumann intr ? PCATCH : 0, 186913ced85SMichael Neumann ctx ? ctx->ww_class->name : "ww_mutex_lock", 0); 187913ced85SMichael Neumann if (intr && (s == EINTR || s == ERESTART)) { 188913ced85SMichael Neumann // XXX: Should we handle ERESTART? 189913ced85SMichael Neumann err = -EINTR; 190913ced85SMichael Neumann break; 191913ced85SMichael Neumann } 192913ced85SMichael Neumann } 193913ced85SMichael Neumann /* 194913ced85SMichael Neumann * If a `younger` process tries to acquire a lock 195913ced85SMichael Neumann * already held by an `older` process, we `wound` it, 196913ced85SMichael Neumann * i.e. we return -EDEADLK because there is a potential 197913ced85SMichael Neumann * risk for a deadlock. The `younger` process then 198913ced85SMichael Neumann * should give up all it's resources and try again to 199913ced85SMichael Neumann * acquire the lock in question, this time in a 200913ced85SMichael Neumann * blocking manner. 201913ced85SMichael Neumann */ 202913ced85SMichael Neumann else { 203913ced85SMichael Neumann err = -EDEADLK; 204913ced85SMichael Neumann break; 205913ced85SMichael Neumann } 206913ced85SMichael Neumann } 207913ced85SMichael Neumann 208913ced85SMichael Neumann } /* for */ 209913ced85SMichael Neumann spin_unlock(&lock->lock); 210913ced85SMichael Neumann return err; 211913ced85SMichael Neumann } 212913ced85SMichael Neumann 213913ced85SMichael Neumann static inline int 214913ced85SMichael Neumann ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx, bool slow) { 215913ced85SMichael Neumann return __ww_mutex_lock(lock, ctx, false, false); 216913ced85SMichael Neumann } 217913ced85SMichael Neumann 218913ced85SMichael Neumann static inline void 219913ced85SMichael Neumann ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) { 220913ced85SMichael Neumann (void)__ww_mutex_lock(lock, ctx, true, false); 221913ced85SMichael Neumann } 222913ced85SMichael Neumann 223913ced85SMichael Neumann static inline int 224913ced85SMichael Neumann ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx, bool slow) { 225913ced85SMichael Neumann return __ww_mutex_lock(lock, ctx, false, true); 226913ced85SMichael Neumann } 227913ced85SMichael Neumann 228913ced85SMichael Neumann static inline void 229913ced85SMichael Neumann ww_mutex_lock_slow_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) { 230913ced85SMichael Neumann (void)__ww_mutex_lock(lock, ctx, true, true); 231913ced85SMichael Neumann } 232913ced85SMichael Neumann 233913ced85SMichael Neumann static inline void 234913ced85SMichael Neumann ww_mutex_unlock(struct ww_mutex *lock) { 235913ced85SMichael Neumann spin_lock(&lock->lock); 236913ced85SMichael Neumann KKASSERT(lock->owner == curthread); 237913ced85SMichael Neumann KKASSERT(lock->acquired > 0); 238913ced85SMichael Neumann 239913ced85SMichael Neumann --lock->acquired; 240913ced85SMichael Neumann if (lock->acquired > 0) { 241913ced85SMichael Neumann spin_unlock(&lock->lock); 242913ced85SMichael Neumann return; 243913ced85SMichael Neumann } 244913ced85SMichael Neumann 245913ced85SMichael Neumann KKASSERT(lock->acquired == 0); 246913ced85SMichael Neumann lock->ctx = NULL; 247913ced85SMichael Neumann lock->owner = NULL; 248913ced85SMichael Neumann spin_unlock(&lock->lock); 249913ced85SMichael Neumann wakeup(lock); 250913ced85SMichael Neumann } 251913ced85SMichael Neumann 252913ced85SMichael Neumann static inline void 253913ced85SMichael Neumann ww_mutex_destroy(struct ww_mutex *lock) { 254913ced85SMichael Neumann KKASSERT(lock->acquired == 0); 255913ced85SMichael Neumann KKASSERT(lock->ctx == NULL); 256913ced85SMichael Neumann KKASSERT(lock->owner == NULL); 257913ced85SMichael Neumann spin_uninit(&lock->lock); 258913ced85SMichael Neumann } 259913ced85SMichael Neumann 260913ced85SMichael Neumann #endif /* _LINUX_WW_MUTEX_H_ */ 261