xref: /netbsd-src/sys/kern/kern_rwlock_obj.c (revision 2246c1eb4d5bd980bce9feef7bfdd436b03afc2d)
1*2246c1ebSad /*	$NetBSD: kern_rwlock_obj.c,v 1.13 2023/10/02 21:03:55 ad Exp $	*/
283685e65Spooka 
383685e65Spooka /*-
459e0001fSad  * Copyright (c) 2008, 2009, 2019, 2023 The NetBSD Foundation, Inc.
583685e65Spooka  * All rights reserved.
683685e65Spooka  *
783685e65Spooka  * This code is derived from software contributed to The NetBSD Foundation
878f9946cSad  * by Andrew Doran.
983685e65Spooka  *
1083685e65Spooka  * Redistribution and use in source and binary forms, with or without
1183685e65Spooka  * modification, are permitted provided that the following conditions
1283685e65Spooka  * are met:
1383685e65Spooka  * 1. Redistributions of source code must retain the above copyright
1483685e65Spooka  *    notice, this list of conditions and the following disclaimer.
1583685e65Spooka  * 2. Redistributions in binary form must reproduce the above copyright
1683685e65Spooka  *    notice, this list of conditions and the following disclaimer in the
1783685e65Spooka  *    documentation and/or other materials provided with the distribution.
1883685e65Spooka  *
1983685e65Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2083685e65Spooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2183685e65Spooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2283685e65Spooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2383685e65Spooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2483685e65Spooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2583685e65Spooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2683685e65Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2783685e65Spooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2883685e65Spooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2983685e65Spooka  * POSSIBILITY OF SUCH DAMAGE.
3083685e65Spooka  */
3183685e65Spooka 
3283685e65Spooka #include <sys/cdefs.h>
33*2246c1ebSad __KERNEL_RCSID(0, "$NetBSD: kern_rwlock_obj.c,v 1.13 2023/10/02 21:03:55 ad Exp $");
3483685e65Spooka 
3583685e65Spooka #include <sys/param.h>
3683685e65Spooka #include <sys/atomic.h>
3759e0001fSad #include <sys/kmem.h>
3883685e65Spooka #include <sys/rwlock.h>
3983685e65Spooka 
4083685e65Spooka /* Mutex cache */
4183685e65Spooka #define	RW_OBJ_MAGIC	0x85d3c85d
4283685e65Spooka struct krwobj {
4383685e65Spooka 	krwlock_t	ro_lock;
4483685e65Spooka 	u_int		ro_magic;
4583685e65Spooka 	u_int		ro_refcnt;
4659e0001fSad 	uint8_t		mo_pad[COHERENCY_UNIT - sizeof(krwlock_t) -
4759e0001fSad 	    sizeof(u_int) * 2];
4883685e65Spooka };
4983685e65Spooka 
5083685e65Spooka /*
5183685e65Spooka  * rw_obj_alloc:
5283685e65Spooka  *
538ce608c9Sad  *	Allocate a single lock object, waiting for memory if needed.
5483685e65Spooka  */
5583685e65Spooka krwlock_t *
rw_obj_alloc(void)5683685e65Spooka rw_obj_alloc(void)
5783685e65Spooka {
5883685e65Spooka 	struct krwobj *ro;
5983685e65Spooka 
60*2246c1ebSad 	ro = kmem_intr_alloc(sizeof(*ro), KM_SLEEP);
6159e0001fSad 	KASSERT(ALIGNED_POINTER(ro, coherency_unit));
625e1cf642Sozaki-r 	_rw_init(&ro->ro_lock, (uintptr_t)__builtin_return_address(0));
6359e0001fSad 	ro->ro_magic = RW_OBJ_MAGIC;
6483685e65Spooka 	ro->ro_refcnt = 1;
6583685e65Spooka 
6683685e65Spooka 	return (krwlock_t *)ro;
6783685e65Spooka }
6883685e65Spooka 
6983685e65Spooka /*
708ce608c9Sad  * rw_obj_tryalloc:
718ce608c9Sad  *
728ce608c9Sad  *	Allocate a single lock object, but fail if no memory is available.
738ce608c9Sad  */
748ce608c9Sad krwlock_t *
rw_obj_tryalloc(void)758ce608c9Sad rw_obj_tryalloc(void)
768ce608c9Sad {
778ce608c9Sad 	struct krwobj *ro;
788ce608c9Sad 
79*2246c1ebSad 	ro = kmem_intr_alloc(sizeof(*ro), KM_NOSLEEP);
8059e0001fSad 	KASSERT(ALIGNED_POINTER(ro, coherency_unit));
818ce608c9Sad 	if (__predict_true(ro != NULL)) {
828ce608c9Sad 		_rw_init(&ro->ro_lock, (uintptr_t)__builtin_return_address(0));
8359e0001fSad 		ro->ro_magic = RW_OBJ_MAGIC;
848ce608c9Sad 		ro->ro_refcnt = 1;
858ce608c9Sad 	}
868ce608c9Sad 
878ce608c9Sad 	return (krwlock_t *)ro;
888ce608c9Sad }
898ce608c9Sad 
908ce608c9Sad /*
9183685e65Spooka  * rw_obj_hold:
9283685e65Spooka  *
9383685e65Spooka  *	Add a single reference to a lock object.  A reference to the object
9483685e65Spooka  *	must already be held, and must be held across this call.
9583685e65Spooka  */
9683685e65Spooka void
rw_obj_hold(krwlock_t * lock)9783685e65Spooka rw_obj_hold(krwlock_t *lock)
9883685e65Spooka {
9983685e65Spooka 	struct krwobj *ro = (struct krwobj *)lock;
10083685e65Spooka 
10183685e65Spooka 	KASSERT(ro->ro_magic == RW_OBJ_MAGIC);
10283685e65Spooka 	KASSERT(ro->ro_refcnt > 0);
10383685e65Spooka 
10483685e65Spooka 	atomic_inc_uint(&ro->ro_refcnt);
10583685e65Spooka }
10683685e65Spooka 
10783685e65Spooka /*
10883685e65Spooka  * rw_obj_free:
10983685e65Spooka  *
11083685e65Spooka  *	Drop a reference from a lock object.  If the last reference is being
11183685e65Spooka  *	dropped, free the object and return true.  Otherwise, return false.
11283685e65Spooka  */
11383685e65Spooka bool
rw_obj_free(krwlock_t * lock)11483685e65Spooka rw_obj_free(krwlock_t *lock)
11583685e65Spooka {
11683685e65Spooka 	struct krwobj *ro = (struct krwobj *)lock;
11783685e65Spooka 
11883685e65Spooka 	KASSERT(ro->ro_magic == RW_OBJ_MAGIC);
11983685e65Spooka 	KASSERT(ro->ro_refcnt > 0);
12083685e65Spooka 
121ef3476fbSriastradh 	membar_release();
12283685e65Spooka 	if (atomic_dec_uint_nv(&ro->ro_refcnt) > 0) {
12383685e65Spooka 		return false;
12483685e65Spooka 	}
125ef3476fbSriastradh 	membar_acquire();
12683685e65Spooka 	rw_destroy(&ro->ro_lock);
127*2246c1ebSad 	kmem_intr_free(ro, sizeof(*ro));
12883685e65Spooka 	return true;
12983685e65Spooka }
1308ce608c9Sad 
1318ce608c9Sad /*
1328ce608c9Sad  * rw_obj_refcnt:
1338ce608c9Sad  *
1348ce608c9Sad  *	Return the reference count for a lock object.
1358ce608c9Sad  */
1368ce608c9Sad u_int
rw_obj_refcnt(krwlock_t * lock)1378ce608c9Sad rw_obj_refcnt(krwlock_t *lock)
1388ce608c9Sad {
1398ce608c9Sad 	struct krwobj *ro = (struct krwobj *)lock;
1408ce608c9Sad 
1418ce608c9Sad 	return ro->ro_refcnt;
1428ce608c9Sad }
143