Home | History | Annotate | Line # | Download | only in kern
kern_mutex_obj.c revision 1.15
      1  1.15        ad /*	$NetBSD: kern_mutex_obj.c,v 1.15 2023/10/02 21:03:55 ad Exp $	*/
      2   1.1     pooka 
      3   1.1     pooka /*-
      4  1.14        ad  * Copyright (c) 2008, 2019, 2023 The NetBSD Foundation, Inc.
      5   1.1     pooka  * All rights reserved.
      6   1.1     pooka  *
      7   1.1     pooka  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2        ad  * by Andrew Doran.
      9   1.1     pooka  *
     10   1.1     pooka  * Redistribution and use in source and binary forms, with or without
     11   1.1     pooka  * modification, are permitted provided that the following conditions
     12   1.1     pooka  * are met:
     13   1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     14   1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     15   1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     pooka  *    documentation and/or other materials provided with the distribution.
     18   1.1     pooka  *
     19   1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     pooka  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     pooka  */
     31   1.1     pooka 
     32   1.1     pooka #include <sys/cdefs.h>
     33  1.15        ad __KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.15 2023/10/02 21:03:55 ad Exp $");
     34   1.1     pooka 
     35   1.1     pooka #include <sys/param.h>
     36   1.1     pooka #include <sys/atomic.h>
     37   1.1     pooka #include <sys/mutex.h>
     38  1.14        ad #include <sys/kmem.h>
     39   1.1     pooka 
     40   1.1     pooka /* Mutex cache */
     41   1.1     pooka #define	MUTEX_OBJ_MAGIC	0x5aa3c85d
     42   1.1     pooka struct kmutexobj {
     43   1.1     pooka 	kmutex_t	mo_lock;
     44   1.1     pooka 	u_int		mo_magic;
     45   1.1     pooka 	u_int		mo_refcnt;
     46  1.14        ad 	uint8_t		mo_pad[COHERENCY_UNIT - sizeof(kmutex_t) -
     47  1.14        ad 	    sizeof(u_int) * 2];
     48   1.1     pooka };
     49   1.1     pooka 
     50   1.1     pooka /*
     51   1.1     pooka  * mutex_obj_alloc:
     52   1.1     pooka  *
     53   1.7        ad  *	Allocate a single lock object, waiting for memory if needed.
     54   1.1     pooka  */
     55   1.1     pooka kmutex_t *
     56   1.1     pooka mutex_obj_alloc(kmutex_type_t type, int ipl)
     57   1.1     pooka {
     58   1.1     pooka 	struct kmutexobj *mo;
     59   1.1     pooka 
     60  1.15        ad 	mo = kmem_intr_alloc(sizeof(*mo), KM_SLEEP);
     61  1.14        ad 	KASSERT(ALIGNED_POINTER(mo, coherency_unit));
     62   1.6     ozaki 	_mutex_init(&mo->mo_lock, type, ipl,
     63   1.6     ozaki 	    (uintptr_t)__builtin_return_address(0));
     64  1.14        ad 	mo->mo_magic = MUTEX_OBJ_MAGIC;
     65   1.1     pooka 	mo->mo_refcnt = 1;
     66   1.1     pooka 
     67   1.1     pooka 	return (kmutex_t *)mo;
     68   1.1     pooka }
     69   1.1     pooka 
     70   1.1     pooka /*
     71   1.7        ad  * mutex_obj_alloc:
     72   1.7        ad  *
     73   1.7        ad  *	Allocate a single lock object, failing if no memory available.
     74   1.7        ad  */
     75   1.7        ad kmutex_t *
     76   1.7        ad mutex_obj_tryalloc(kmutex_type_t type, int ipl)
     77   1.7        ad {
     78   1.7        ad 	struct kmutexobj *mo;
     79   1.7        ad 
     80  1.15        ad 	mo = kmem_intr_alloc(sizeof(*mo), KM_NOSLEEP);
     81  1.14        ad 	KASSERT(ALIGNED_POINTER(mo, coherency_unit));
     82   1.7        ad 	if (__predict_true(mo != NULL)) {
     83   1.7        ad 		_mutex_init(&mo->mo_lock, type, ipl,
     84   1.7        ad 		    (uintptr_t)__builtin_return_address(0));
     85  1.14        ad 		mo->mo_magic = MUTEX_OBJ_MAGIC;
     86   1.7        ad 		mo->mo_refcnt = 1;
     87   1.7        ad 	}
     88   1.7        ad 
     89   1.7        ad 	return (kmutex_t *)mo;
     90   1.7        ad }
     91   1.7        ad 
     92   1.7        ad /*
     93   1.1     pooka  * mutex_obj_hold:
     94   1.1     pooka  *
     95   1.1     pooka  *	Add a single reference to a lock object.  A reference to the object
     96   1.1     pooka  *	must already be held, and must be held across this call.
     97   1.1     pooka  */
     98   1.1     pooka void
     99   1.1     pooka mutex_obj_hold(kmutex_t *lock)
    100   1.1     pooka {
    101   1.1     pooka 	struct kmutexobj *mo = (struct kmutexobj *)lock;
    102   1.1     pooka 
    103   1.4      matt 	KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
    104   1.5       jym 	    "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
    105   1.5       jym 	     __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
    106   1.4      matt 	KASSERTMSG(mo->mo_refcnt > 0,
    107   1.5       jym 	    "%s: lock %p: mo->mo_refcnt (%#x) == 0",
    108   1.5       jym 	     __func__, mo, mo->mo_refcnt);
    109   1.1     pooka 
    110   1.1     pooka 	atomic_inc_uint(&mo->mo_refcnt);
    111   1.1     pooka }
    112   1.1     pooka 
    113   1.1     pooka /*
    114   1.1     pooka  * mutex_obj_free:
    115   1.1     pooka  *
    116   1.1     pooka  *	Drop a reference from a lock object.  If the last reference is being
    117   1.1     pooka  *	dropped, free the object and return true.  Otherwise, return false.
    118   1.1     pooka  */
    119   1.1     pooka bool
    120   1.1     pooka mutex_obj_free(kmutex_t *lock)
    121   1.1     pooka {
    122   1.1     pooka 	struct kmutexobj *mo = (struct kmutexobj *)lock;
    123   1.1     pooka 
    124   1.4      matt 	KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
    125   1.5       jym 	    "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
    126   1.5       jym 	     __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
    127   1.4      matt 	KASSERTMSG(mo->mo_refcnt > 0,
    128   1.5       jym 	    "%s: lock %p: mo->mo_refcnt (%#x) == 0",
    129   1.5       jym 	     __func__, mo, mo->mo_refcnt);
    130   1.1     pooka 
    131   1.9  riastrad 	membar_release();
    132   1.1     pooka 	if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) {
    133   1.1     pooka 		return false;
    134   1.1     pooka 	}
    135   1.9  riastrad 	membar_acquire();
    136   1.1     pooka 	mutex_destroy(&mo->mo_lock);
    137  1.15        ad 	kmem_intr_free(mo, sizeof(*mo));
    138   1.1     pooka 	return true;
    139   1.1     pooka }
    140   1.7        ad 
    141   1.7        ad /*
    142   1.7        ad  * mutex_obj_refcnt:
    143   1.7        ad  *
    144   1.7        ad  *	Return the reference count on a lock object.
    145   1.7        ad  */
    146   1.7        ad u_int
    147   1.7        ad mutex_obj_refcnt(kmutex_t *lock)
    148   1.7        ad {
    149   1.7        ad 	struct kmutexobj *mo = (struct kmutexobj *)lock;
    150   1.7        ad 
    151   1.7        ad 	return mo->mo_refcnt;
    152   1.7        ad }
    153