xref: /freebsd/sys/kern/subr_kobj.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1f7b77691SDoug Rabson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
446ba7a35SDoug Rabson  * Copyright (c) 2000,2003 Doug Rabson
5f7b77691SDoug Rabson  * All rights reserved.
6f7b77691SDoug Rabson  *
7f7b77691SDoug Rabson  * Redistribution and use in source and binary forms, with or without
8f7b77691SDoug Rabson  * modification, are permitted provided that the following conditions
9f7b77691SDoug Rabson  * are met:
10f7b77691SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
11f7b77691SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
12f7b77691SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
13f7b77691SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
14f7b77691SDoug Rabson  *    documentation and/or other materials provided with the distribution.
15f7b77691SDoug Rabson  *
16f7b77691SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f7b77691SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f7b77691SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f7b77691SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f7b77691SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f7b77691SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f7b77691SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f7b77691SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f7b77691SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f7b77691SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f7b77691SDoug Rabson  * SUCH DAMAGE.
27f7b77691SDoug Rabson  */
28f7b77691SDoug Rabson 
29f7b77691SDoug Rabson #include <sys/param.h>
30f7b77691SDoug Rabson #include <sys/kernel.h>
3146ba7a35SDoug Rabson #include <sys/kobj.h>
3246ba7a35SDoug Rabson #include <sys/lock.h>
3346ba7a35SDoug Rabson #include <sys/malloc.h>
3446ba7a35SDoug Rabson #include <sys/mutex.h>
35828447e0SCameron Grant #include <sys/sysctl.h>
36f7b77691SDoug Rabson #ifndef TEST
37f7b77691SDoug Rabson #include <sys/systm.h>
38f7b77691SDoug Rabson #endif
39f7b77691SDoug Rabson 
40f7b77691SDoug Rabson #ifdef TEST
41f7b77691SDoug Rabson #include "usertest.h"
42f7b77691SDoug Rabson #endif
43f7b77691SDoug Rabson 
44f7b77691SDoug Rabson static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
45f7b77691SDoug Rabson 
46f7b77691SDoug Rabson #ifdef KOBJ_STATS
47f7b77691SDoug Rabson 
483316a80bSKelly Yancey u_int kobj_lookup_hits;
493316a80bSKelly Yancey u_int kobj_lookup_misses;
50f7b77691SDoug Rabson 
513316a80bSKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
52828447e0SCameron Grant 	   &kobj_lookup_hits, 0, "");
533316a80bSKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
54828447e0SCameron Grant 	   &kobj_lookup_misses, 0, "");
55f7b77691SDoug Rabson 
56f7b77691SDoug Rabson #endif
57f7b77691SDoug Rabson 
5846ba7a35SDoug Rabson static struct mtx kobj_mtx;
59e000e001SPeter Grehan static int kobj_mutex_inited;
60f7b77691SDoug Rabson static int kobj_next_id = 1;
61f7b77691SDoug Rabson 
62d7ecd801SMarius Strobl #define	KOBJ_LOCK()		mtx_lock(&kobj_mtx)
63d7ecd801SMarius Strobl #define	KOBJ_UNLOCK()		mtx_unlock(&kobj_mtx)
64d7ecd801SMarius Strobl #define	KOBJ_ASSERT(what)	mtx_assert(&kobj_mtx, what);
6591416fb2SNathan Whitehorn 
66fbbb13f9SMatthew D Fleming SYSCTL_INT(_kern, OID_AUTO, kobj_methodcount, CTLFLAG_RD,
67b05ca429SPawel Biernacki     &kobj_next_id, 0,
68b05ca429SPawel Biernacki     "Number of kernel object methods registered");
69828447e0SCameron Grant 
7046ba7a35SDoug Rabson static void
kobj_init_mutex(void * arg)7146ba7a35SDoug Rabson kobj_init_mutex(void *arg)
7246ba7a35SDoug Rabson {
73e000e001SPeter Grehan 	if (!kobj_mutex_inited) {
7446ba7a35SDoug Rabson 		mtx_init(&kobj_mtx, "kobj", NULL, MTX_DEF);
75e000e001SPeter Grehan 		kobj_mutex_inited = 1;
76e000e001SPeter Grehan 	}
7746ba7a35SDoug Rabson }
7846ba7a35SDoug Rabson 
7946ba7a35SDoug Rabson SYSINIT(kobj, SI_SUB_LOCK, SI_ORDER_ANY, kobj_init_mutex, NULL);
8046ba7a35SDoug Rabson 
8146ba7a35SDoug Rabson /*
8246ba7a35SDoug Rabson  * This method structure is used to initialise new caches. Since the
8346ba7a35SDoug Rabson  * desc pointer is NULL, it is guaranteed never to match any read
8446ba7a35SDoug Rabson  * descriptors.
8546ba7a35SDoug Rabson  */
863f3f6bc3SEd Schouten static const struct kobj_method null_method = {
8746ba7a35SDoug Rabson 	0, 0,
8846ba7a35SDoug Rabson };
8946ba7a35SDoug Rabson 
9046ba7a35SDoug Rabson int
kobj_error_method(void)91f7b77691SDoug Rabson kobj_error_method(void)
92f7b77691SDoug Rabson {
9346ba7a35SDoug Rabson 
94f7b77691SDoug Rabson 	return ENXIO;
95f7b77691SDoug Rabson }
96f7b77691SDoug Rabson 
97f7b77691SDoug Rabson static void
kobj_class_compile_common(kobj_class_t cls,kobj_ops_t ops)98f80e4547SDoug Rabson kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
99f7b77691SDoug Rabson {
100f7b77691SDoug Rabson 	kobj_method_t *m;
101f7b77691SDoug Rabson 	int i;
102f7b77691SDoug Rabson 
103f7b77691SDoug Rabson 	/*
104f7b77691SDoug Rabson 	 * Don't do anything if we are already compiled.
105f7b77691SDoug Rabson 	 */
106f7b77691SDoug Rabson 	if (cls->ops)
107f7b77691SDoug Rabson 		return;
108f7b77691SDoug Rabson 
109f7b77691SDoug Rabson 	/*
110f7b77691SDoug Rabson 	 * First register any methods which need it.
111f7b77691SDoug Rabson 	 */
1121eebd615SWarner Losh 	for (m = cls->methods; m->desc; m++) {
113d7ecd801SMarius Strobl 		if (m->desc->id == 0)
114d7ecd801SMarius Strobl 			m->desc->id = kobj_next_id++;
115d7ecd801SMarius Strobl 	}
116f7b77691SDoug Rabson 
117f7b77691SDoug Rabson 	/*
118f80e4547SDoug Rabson 	 * Then initialise the ops table.
119f80e4547SDoug Rabson 	 */
12046ba7a35SDoug Rabson 	for (i = 0; i < KOBJ_CACHE_SIZE; i++)
12146ba7a35SDoug Rabson 		ops->cache[i] = &null_method;
122f80e4547SDoug Rabson 	ops->cls = cls;
123f80e4547SDoug Rabson 	cls->ops = ops;
124f80e4547SDoug Rabson }
125f80e4547SDoug Rabson 
126919e7b53SMark Johnston static int
kobj_class_compile1(kobj_class_t cls,int mflags)127919e7b53SMark Johnston kobj_class_compile1(kobj_class_t cls, int mflags)
128f80e4547SDoug Rabson {
129f80e4547SDoug Rabson 	kobj_ops_t ops;
130f80e4547SDoug Rabson 
13191416fb2SNathan Whitehorn 	KOBJ_ASSERT(MA_NOTOWNED);
13246ba7a35SDoug Rabson 
133919e7b53SMark Johnston 	ops = malloc(sizeof(struct kobj_ops), M_KOBJ, mflags);
134919e7b53SMark Johnston 	if (ops == NULL)
135919e7b53SMark Johnston 		return (ENOMEM);
13646ba7a35SDoug Rabson 
13746ba7a35SDoug Rabson 	/*
13846ba7a35SDoug Rabson 	 * We may have lost a race for kobj_class_compile here - check
13946ba7a35SDoug Rabson 	 * to make sure someone else hasn't already compiled this
14046ba7a35SDoug Rabson 	 * class.
14146ba7a35SDoug Rabson 	 */
142919e7b53SMark Johnston 	KOBJ_LOCK();
14346ba7a35SDoug Rabson 	if (cls->ops) {
14491416fb2SNathan Whitehorn 		KOBJ_UNLOCK();
14546ba7a35SDoug Rabson 		free(ops, M_KOBJ);
146919e7b53SMark Johnston 		return (0);
14746ba7a35SDoug Rabson 	}
148f80e4547SDoug Rabson 	kobj_class_compile_common(cls, ops);
14991416fb2SNathan Whitehorn 	KOBJ_UNLOCK();
150919e7b53SMark Johnston 	return (0);
151919e7b53SMark Johnston }
152919e7b53SMark Johnston 
153919e7b53SMark Johnston void
kobj_class_compile(kobj_class_t cls)154919e7b53SMark Johnston kobj_class_compile(kobj_class_t cls)
155919e7b53SMark Johnston {
156b2835fa2SMateusz Guzik 	int error __diagused;
157919e7b53SMark Johnston 
158919e7b53SMark Johnston 	error = kobj_class_compile1(cls, M_WAITOK);
159919e7b53SMark Johnston 	KASSERT(error == 0, ("kobj_class_compile1 returned %d", error));
160f80e4547SDoug Rabson }
161f80e4547SDoug Rabson 
162f80e4547SDoug Rabson void
kobj_class_compile_static(kobj_class_t cls,kobj_ops_t ops)163f80e4547SDoug Rabson kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
164f80e4547SDoug Rabson {
16546ba7a35SDoug Rabson 
166d7ecd801SMarius Strobl 	KASSERT(kobj_mutex_inited == 0,
167d7ecd801SMarius Strobl 	    ("%s: only supported during early cycles", __func__));
16846ba7a35SDoug Rabson 
169f80e4547SDoug Rabson 	/*
170f80e4547SDoug Rabson 	 * Increment refs to make sure that the ops table is not freed.
171f80e4547SDoug Rabson 	 */
172f80e4547SDoug Rabson 	cls->refs++;
173f80e4547SDoug Rabson 	kobj_class_compile_common(cls, ops);
174f7b77691SDoug Rabson }
175f7b77691SDoug Rabson 
17646ba7a35SDoug Rabson static kobj_method_t*
kobj_lookup_method_class(kobj_class_t cls,kobjop_desc_t desc)17746ba7a35SDoug Rabson kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc)
17846ba7a35SDoug Rabson {
17946ba7a35SDoug Rabson 	kobj_method_t *methods = cls->methods;
18046ba7a35SDoug Rabson 	kobj_method_t *ce;
18146ba7a35SDoug Rabson 
18246ba7a35SDoug Rabson 	for (ce = methods; ce && ce->desc; ce++) {
18346ba7a35SDoug Rabson 		if (ce->desc == desc) {
18446ba7a35SDoug Rabson 			return ce;
18546ba7a35SDoug Rabson 		}
18646ba7a35SDoug Rabson 	}
18746ba7a35SDoug Rabson 
1882c204a16SWarner Losh 	return NULL;
18946ba7a35SDoug Rabson }
19046ba7a35SDoug Rabson 
19146ba7a35SDoug Rabson static kobj_method_t*
kobj_lookup_method_mi(kobj_class_t cls,kobjop_desc_t desc)19246ba7a35SDoug Rabson kobj_lookup_method_mi(kobj_class_t cls,
193f7b77691SDoug Rabson 		      kobjop_desc_t desc)
194f7b77691SDoug Rabson {
19546ba7a35SDoug Rabson 	kobj_method_t *ce;
19646ba7a35SDoug Rabson 	kobj_class_t *basep;
19746ba7a35SDoug Rabson 
19846ba7a35SDoug Rabson 	ce = kobj_lookup_method_class(cls, desc);
19946ba7a35SDoug Rabson 	if (ce)
20046ba7a35SDoug Rabson 		return ce;
20146ba7a35SDoug Rabson 
20246ba7a35SDoug Rabson 	basep = cls->baseclasses;
20346ba7a35SDoug Rabson 	if (basep) {
20446ba7a35SDoug Rabson 		for (; *basep; basep++) {
20546ba7a35SDoug Rabson 			ce = kobj_lookup_method_mi(*basep, desc);
20646ba7a35SDoug Rabson 			if (ce)
20746ba7a35SDoug Rabson 				return ce;
208f7b77691SDoug Rabson 		}
209f7b77691SDoug Rabson 	}
21046ba7a35SDoug Rabson 
2112c204a16SWarner Losh 	return NULL;
21246ba7a35SDoug Rabson }
21346ba7a35SDoug Rabson 
21446ba7a35SDoug Rabson kobj_method_t*
kobj_lookup_method(kobj_class_t cls,kobj_method_t ** cep,kobjop_desc_t desc)21546ba7a35SDoug Rabson kobj_lookup_method(kobj_class_t cls,
21646ba7a35SDoug Rabson 		   kobj_method_t **cep,
21746ba7a35SDoug Rabson 		   kobjop_desc_t desc)
21846ba7a35SDoug Rabson {
21946ba7a35SDoug Rabson 	kobj_method_t *ce;
22046ba7a35SDoug Rabson 
22146ba7a35SDoug Rabson 	ce = kobj_lookup_method_mi(cls, desc);
22246ba7a35SDoug Rabson 	if (!ce)
223d09ebcecSEd Schouten 		ce = &desc->deflt;
22426d877f5SMarius Strobl 	if (cep)
22546ba7a35SDoug Rabson 		*cep = ce;
22646ba7a35SDoug Rabson 	return ce;
227f7b77691SDoug Rabson }
228f7b77691SDoug Rabson 
229f7b77691SDoug Rabson void
kobj_class_free(kobj_class_t cls)230f7b77691SDoug Rabson kobj_class_free(kobj_class_t cls)
231f7b77691SDoug Rabson {
2322c204a16SWarner Losh 	void* ops = NULL;
233f7b77691SDoug Rabson 
23491416fb2SNathan Whitehorn 	KOBJ_ASSERT(MA_NOTOWNED);
23591416fb2SNathan Whitehorn 	KOBJ_LOCK();
23646ba7a35SDoug Rabson 
23746ba7a35SDoug Rabson 	/*
23846ba7a35SDoug Rabson 	 * Protect against a race between kobj_create and
23946ba7a35SDoug Rabson 	 * kobj_delete.
24046ba7a35SDoug Rabson 	 */
24146ba7a35SDoug Rabson 	if (cls->refs == 0) {
242f7b77691SDoug Rabson 		/*
243d7ecd801SMarius Strobl 		 * For now we don't do anything to unregister any methods
244d7ecd801SMarius Strobl 		 * which are no longer used.
245f7b77691SDoug Rabson 		 */
246f7b77691SDoug Rabson 
247f7b77691SDoug Rabson 		/*
248f7b77691SDoug Rabson 		 * Free memory and clean up.
249f7b77691SDoug Rabson 		 */
25046ba7a35SDoug Rabson 		ops = cls->ops;
2512c204a16SWarner Losh 		cls->ops = NULL;
252f7b77691SDoug Rabson 	}
253f7b77691SDoug Rabson 
25491416fb2SNathan Whitehorn 	KOBJ_UNLOCK();
25546ba7a35SDoug Rabson 
25646ba7a35SDoug Rabson 	if (ops)
25746ba7a35SDoug Rabson 		free(ops, M_KOBJ);
25846ba7a35SDoug Rabson }
25946ba7a35SDoug Rabson 
260d7ecd801SMarius Strobl static void
kobj_init_common(kobj_t obj,kobj_class_t cls)261d7ecd801SMarius Strobl kobj_init_common(kobj_t obj, kobj_class_t cls)
262d7ecd801SMarius Strobl {
263d7ecd801SMarius Strobl 
264d7ecd801SMarius Strobl 	obj->ops = cls->ops;
265d7ecd801SMarius Strobl 	cls->refs++;
266d7ecd801SMarius Strobl }
267d7ecd801SMarius Strobl 
268919e7b53SMark Johnston static int
kobj_init1(kobj_t obj,kobj_class_t cls,int mflags)269919e7b53SMark Johnston kobj_init1(kobj_t obj, kobj_class_t cls, int mflags)
270f7b77691SDoug Rabson {
271919e7b53SMark Johnston 	int error;
27246ba7a35SDoug Rabson 
273919e7b53SMark Johnston 	KOBJ_LOCK();
274919e7b53SMark Johnston 	while (cls->ops == NULL) {
27546ba7a35SDoug Rabson 		/*
27646ba7a35SDoug Rabson 		 * kobj_class_compile doesn't want the lock held
27746ba7a35SDoug Rabson 		 * because of the call to malloc - we drop the lock
27846ba7a35SDoug Rabson 		 * and re-try.
27946ba7a35SDoug Rabson 		 */
28091416fb2SNathan Whitehorn 		KOBJ_UNLOCK();
281919e7b53SMark Johnston 		error = kobj_class_compile1(cls, mflags);
282919e7b53SMark Johnston 		if (error != 0)
283919e7b53SMark Johnston 			return (error);
284919e7b53SMark Johnston 		KOBJ_LOCK();
285919e7b53SMark Johnston 	}
286919e7b53SMark Johnston 	kobj_init_common(obj, cls);
287919e7b53SMark Johnston 	KOBJ_UNLOCK();
288919e7b53SMark Johnston 	return (0);
28946ba7a35SDoug Rabson }
290f7b77691SDoug Rabson 
291919e7b53SMark Johnston kobj_t
kobj_create(kobj_class_t cls,struct malloc_type * mtype,int mflags)292919e7b53SMark Johnston kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags)
293919e7b53SMark Johnston {
294919e7b53SMark Johnston 	kobj_t obj;
29546ba7a35SDoug Rabson 
296919e7b53SMark Johnston 	obj = malloc(cls->size, mtype, mflags | M_ZERO);
297919e7b53SMark Johnston 	if (obj == NULL)
298919e7b53SMark Johnston 		return (NULL);
299919e7b53SMark Johnston 	if (kobj_init1(obj, cls, mflags) != 0) {
300919e7b53SMark Johnston 		free(obj, mtype);
301919e7b53SMark Johnston 		return (NULL);
302919e7b53SMark Johnston 	}
303919e7b53SMark Johnston 	return (obj);
304919e7b53SMark Johnston }
305919e7b53SMark Johnston 
306919e7b53SMark Johnston void
kobj_init(kobj_t obj,kobj_class_t cls)307919e7b53SMark Johnston kobj_init(kobj_t obj, kobj_class_t cls)
308919e7b53SMark Johnston {
309919e7b53SMark Johnston 	int error;
310919e7b53SMark Johnston 
311919e7b53SMark Johnston 	error = kobj_init1(obj, cls, M_NOWAIT);
312919e7b53SMark Johnston 	if (error != 0)
313919e7b53SMark Johnston 		panic("kobj_init1 failed: error %d", error);
314f7b77691SDoug Rabson }
315f7b77691SDoug Rabson 
316f7b77691SDoug Rabson void
kobj_init_static(kobj_t obj,kobj_class_t cls)317d7ecd801SMarius Strobl kobj_init_static(kobj_t obj, kobj_class_t cls)
318d7ecd801SMarius Strobl {
319d7ecd801SMarius Strobl 
320d7ecd801SMarius Strobl 	KASSERT(kobj_mutex_inited == 0,
321d7ecd801SMarius Strobl 	    ("%s: only supported during early cycles", __func__));
322d7ecd801SMarius Strobl 
323d7ecd801SMarius Strobl 	kobj_init_common(obj, cls);
324d7ecd801SMarius Strobl }
325d7ecd801SMarius Strobl 
326d7ecd801SMarius Strobl void
kobj_delete(kobj_t obj,struct malloc_type * mtype)327f7b77691SDoug Rabson kobj_delete(kobj_t obj, struct malloc_type *mtype)
328f7b77691SDoug Rabson {
329f7b77691SDoug Rabson 	kobj_class_t cls = obj->ops->cls;
33046ba7a35SDoug Rabson 	int refs;
331f7b77691SDoug Rabson 
332f7b77691SDoug Rabson 	/*
333f7b77691SDoug Rabson 	 * Consider freeing the compiled method table for the class
334f7b77691SDoug Rabson 	 * after its last instance is deleted. As an optimisation, we
335f7b77691SDoug Rabson 	 * should defer this for a short while to avoid thrashing.
336f7b77691SDoug Rabson 	 */
33791416fb2SNathan Whitehorn 	KOBJ_ASSERT(MA_NOTOWNED);
33891416fb2SNathan Whitehorn 	KOBJ_LOCK();
3394b4a49fdSDoug Rabson 	cls->refs--;
34046ba7a35SDoug Rabson 	refs = cls->refs;
34191416fb2SNathan Whitehorn 	KOBJ_UNLOCK();
34246ba7a35SDoug Rabson 
34346ba7a35SDoug Rabson 	if (!refs)
344f7b77691SDoug Rabson 		kobj_class_free(cls);
345f7b77691SDoug Rabson 
3462c204a16SWarner Losh 	obj->ops = NULL;
347f7b77691SDoug Rabson 	if (mtype)
348f7b77691SDoug Rabson 		free(obj, mtype);
349f7b77691SDoug Rabson }
350