1328653Shselasky/*- 2328653Shselasky * Copyright (c) 2017 Mellanox Technologies, Ltd. 3328653Shselasky * All rights reserved. 4328653Shselasky * 5328653Shselasky * Redistribution and use in source and binary forms, with or without 6328653Shselasky * modification, are permitted provided that the following conditions 7328653Shselasky * are met: 8328653Shselasky * 1. Redistributions of source code must retain the above copyright 9328653Shselasky * notice unmodified, this list of conditions, and the following 10328653Shselasky * disclaimer. 11328653Shselasky * 2. Redistributions in binary form must reproduce the above copyright 12328653Shselasky * notice, this list of conditions and the following disclaimer in the 13328653Shselasky * documentation and/or other materials provided with the distribution. 14328653Shselasky * 15328653Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16328653Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17328653Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18328653Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19328653Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20328653Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21328653Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22328653Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23328653Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24328653Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25328653Shselasky * 26328653Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ww_mutex.h 366879 2020-10-20 08:23:24Z hselasky $ 27328653Shselasky */ 28328653Shselasky#ifndef _LINUX_WW_MUTEX_H_ 29328653Shselasky#define _LINUX_WW_MUTEX_H_ 30328653Shselasky 31328653Shselasky#include <sys/param.h> 32328653Shselasky#include <sys/proc.h> 33328653Shselasky#include <sys/condvar.h> 34328653Shselasky#include <sys/kernel.h> 35328653Shselasky 36328653Shselasky#include <linux/mutex.h> 37328653Shselasky 38328653Shselaskystruct ww_class { 39328653Shselasky const char *mutex_name; 40328653Shselasky}; 41328653Shselasky 42328653Shselaskystruct ww_acquire_ctx { 43328653Shselasky}; 44328653Shselasky 45328653Shselaskystruct ww_mutex { 46328653Shselasky struct mutex base; 47328653Shselasky struct cv condvar; 48366879Shselasky struct ww_acquire_ctx *ctx; 49328653Shselasky}; 50328653Shselasky 51328653Shselasky#define DEFINE_WW_CLASS(name) \ 52328653Shselasky struct ww_class name = { \ 53328653Shselasky .mutex_name = mutex_name(#name "_mutex") \ 54328653Shselasky } 55328653Shselasky 56328653Shselasky#define DEFINE_WW_MUTEX(name, ww_class) \ 57328653Shselasky struct ww_mutex name; \ 58328653Shselasky static void name##_init(void *arg) \ 59328653Shselasky { \ 60328653Shselasky ww_mutex_init(&name, &ww_class); \ 61328653Shselasky } \ 62328653Shselasky SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL) 63328653Shselasky 64328653Shselasky#define ww_mutex_is_locked(_m) \ 65328653Shselasky sx_xlocked(&(_m)->base.sx) 66328653Shselasky 67328653Shselasky#define ww_mutex_lock_slow(_m, _x) \ 68328653Shselasky ww_mutex_lock(_m, _x) 69328653Shselasky 70328653Shselasky#define ww_mutex_lock_slow_interruptible(_m, _x) \ 71328653Shselasky ww_mutex_lock_interruptible(_m, _x) 72328653Shselasky 73328653Shselaskystatic inline int __must_check 74328653Shselaskyww_mutex_trylock(struct ww_mutex *lock) 75328653Shselasky{ 76328653Shselasky return (mutex_trylock(&lock->base)); 77328653Shselasky} 78328653Shselasky 79366879Shselaskyextern int linux_ww_mutex_lock_sub(struct ww_mutex *, 80366879Shselasky struct ww_acquire_ctx *, int catch_signal); 81328653Shselasky 82328653Shselaskystatic inline int 83328653Shselaskyww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 84328653Shselasky{ 85328653Shselasky if (MUTEX_SKIP()) 86328653Shselasky return (0); 87328653Shselasky else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread) 88328653Shselasky return (-EALREADY); 89328653Shselasky else 90366879Shselasky return (linux_ww_mutex_lock_sub(lock, ctx, 0)); 91328653Shselasky} 92328653Shselasky 93328653Shselaskystatic inline int 94328653Shselaskyww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 95328653Shselasky{ 96328653Shselasky if (MUTEX_SKIP()) 97328653Shselasky return (0); 98328653Shselasky else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread) 99328653Shselasky return (-EALREADY); 100328653Shselasky else 101366879Shselasky return (linux_ww_mutex_lock_sub(lock, ctx, 1)); 102328653Shselasky} 103328653Shselasky 104328653Shselaskyextern void linux_ww_mutex_unlock_sub(struct ww_mutex *); 105328653Shselasky 106328653Shselaskystatic inline void 107328653Shselaskyww_mutex_unlock(struct ww_mutex *lock) 108328653Shselasky{ 109328653Shselasky if (MUTEX_SKIP()) 110328653Shselasky return; 111328653Shselasky else 112328653Shselasky linux_ww_mutex_unlock_sub(lock); 113328653Shselasky} 114328653Shselasky 115328653Shselaskystatic inline void 116328653Shselaskyww_mutex_destroy(struct ww_mutex *lock) 117328653Shselasky{ 118328653Shselasky cv_destroy(&lock->condvar); 119328653Shselasky mutex_destroy(&lock->base); 120328653Shselasky} 121328653Shselasky 122328653Shselaskystatic inline void 123328653Shselaskyww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class) 124328653Shselasky{ 125328653Shselasky} 126328653Shselasky 127328653Shselaskystatic inline void 128328653Shselaskyww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class) 129328653Shselasky{ 130328653Shselasky linux_mutex_init(&lock->base, ww_class->mutex_name, SX_NOWITNESS); 131328653Shselasky cv_init(&lock->condvar, "lkpi-ww"); 132328653Shselasky} 133328653Shselasky 134328653Shselaskystatic inline void 135328653Shselaskyww_acquire_fini(struct ww_acquire_ctx *ctx) 136328653Shselasky{ 137328653Shselasky} 138328653Shselasky 139328653Shselaskystatic inline void 140328653Shselaskyww_acquire_done(struct ww_acquire_ctx *ctx) 141328653Shselasky{ 142328653Shselasky} 143328653Shselasky 144328653Shselasky#endif /* _LINUX_WW_MUTEX_H_ */ 145