2000-04-08 14:17:18 +00:00
|
|
|
/*-
|
2003-10-16 09:16:28 +00:00
|
|
|
* Copyright (c) 2000,2003 Doug Rabson
|
2000-04-08 14:17:18 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-04-08 14:17:18 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
2003-10-16 09:16:28 +00:00
|
|
|
#include <sys/kobj.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
2003-08-14 21:16:46 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-04-08 14:17:18 +00:00
|
|
|
#ifndef TEST
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
#include "usertest.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
|
|
|
|
|
|
|
|
#ifdef KOBJ_STATS
|
|
|
|
|
2002-06-10 22:40:26 +00:00
|
|
|
u_int kobj_lookup_hits;
|
|
|
|
u_int kobj_lookup_misses;
|
2000-04-08 14:17:18 +00:00
|
|
|
|
2002-06-10 22:40:26 +00:00
|
|
|
SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
|
2003-08-14 21:16:46 +00:00
|
|
|
&kobj_lookup_hits, 0, "");
|
2002-06-10 22:40:26 +00:00
|
|
|
SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
|
2003-08-14 21:16:46 +00:00
|
|
|
&kobj_lookup_misses, 0, "");
|
2000-04-08 14:17:18 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-10-16 09:16:28 +00:00
|
|
|
static struct mtx kobj_mtx;
|
2005-08-07 02:20:35 +00:00
|
|
|
static int kobj_mutex_inited;
|
2000-04-08 14:17:18 +00:00
|
|
|
static int kobj_next_id = 1;
|
|
|
|
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
#define KOBJ_LOCK() mtx_lock(&kobj_mtx)
|
|
|
|
#define KOBJ_UNLOCK() mtx_unlock(&kobj_mtx)
|
|
|
|
#define KOBJ_ASSERT(what) mtx_assert(&kobj_mtx, what);
|
2008-12-20 00:33:10 +00:00
|
|
|
|
2011-01-12 19:54:19 +00:00
|
|
|
SYSCTL_INT(_kern, OID_AUTO, kobj_methodcount, CTLFLAG_RD,
|
2003-08-14 21:16:46 +00:00
|
|
|
&kobj_next_id, 0, "");
|
|
|
|
|
2003-10-16 09:16:28 +00:00
|
|
|
static void
|
|
|
|
kobj_init_mutex(void *arg)
|
|
|
|
{
|
2005-08-07 02:20:35 +00:00
|
|
|
if (!kobj_mutex_inited) {
|
|
|
|
mtx_init(&kobj_mtx, "kobj", NULL, MTX_DEF);
|
|
|
|
kobj_mutex_inited = 1;
|
|
|
|
}
|
2003-10-16 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(kobj, SI_SUB_LOCK, SI_ORDER_ANY, kobj_init_mutex, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This method structure is used to initialise new caches. Since the
|
|
|
|
* desc pointer is NULL, it is guaranteed never to match any read
|
|
|
|
* descriptors.
|
|
|
|
*/
|
2011-11-08 15:38:21 +00:00
|
|
|
static const struct kobj_method null_method = {
|
2003-10-16 09:16:28 +00:00
|
|
|
0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2000-04-08 14:17:18 +00:00
|
|
|
kobj_error_method(void)
|
|
|
|
{
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2000-04-08 14:17:18 +00:00
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
2000-08-28 21:11:12 +00:00
|
|
|
static void
|
|
|
|
kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
|
2000-04-08 14:17:18 +00:00
|
|
|
{
|
|
|
|
kobj_method_t *m;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't do anything if we are already compiled.
|
|
|
|
*/
|
|
|
|
if (cls->ops)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First register any methods which need it.
|
|
|
|
*/
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
for (i = 0, m = cls->methods; m->desc; i++, m++) {
|
|
|
|
if (m->desc->id == 0)
|
|
|
|
m->desc->id = kobj_next_id++;
|
|
|
|
}
|
2000-04-08 14:17:18 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-28 21:11:12 +00:00
|
|
|
* Then initialise the ops table.
|
2000-04-08 14:17:18 +00:00
|
|
|
*/
|
2003-10-16 09:16:28 +00:00
|
|
|
for (i = 0; i < KOBJ_CACHE_SIZE; i++)
|
|
|
|
ops->cache[i] = &null_method;
|
2000-04-08 14:17:18 +00:00
|
|
|
ops->cls = cls;
|
|
|
|
cls->ops = ops;
|
|
|
|
}
|
|
|
|
|
2000-08-28 21:11:12 +00:00
|
|
|
void
|
|
|
|
kobj_class_compile(kobj_class_t cls)
|
|
|
|
{
|
|
|
|
kobj_ops_t ops;
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_ASSERT(MA_NOTOWNED);
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2000-08-28 21:11:12 +00:00
|
|
|
/*
|
|
|
|
* Allocate space for the compiled ops table.
|
|
|
|
*/
|
|
|
|
ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT);
|
|
|
|
if (!ops)
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
panic("%s: out of memory", __func__);
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_LOCK();
|
2003-10-16 09:16:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We may have lost a race for kobj_class_compile here - check
|
|
|
|
* to make sure someone else hasn't already compiled this
|
|
|
|
* class.
|
|
|
|
*/
|
|
|
|
if (cls->ops) {
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2003-10-16 09:16:28 +00:00
|
|
|
free(ops, M_KOBJ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-08-28 21:11:12 +00:00
|
|
|
kobj_class_compile_common(cls, ops);
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2000-08-28 21:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
|
|
|
|
{
|
2003-10-16 09:16:28 +00:00
|
|
|
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
KASSERT(kobj_mutex_inited == 0,
|
|
|
|
("%s: only supported during early cycles", __func__));
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2000-08-28 21:11:12 +00:00
|
|
|
/*
|
|
|
|
* Increment refs to make sure that the ops table is not freed.
|
|
|
|
*/
|
|
|
|
cls->refs++;
|
|
|
|
kobj_class_compile_common(cls, ops);
|
|
|
|
}
|
|
|
|
|
2003-10-16 09:16:28 +00:00
|
|
|
static kobj_method_t*
|
|
|
|
kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc)
|
2000-04-08 14:17:18 +00:00
|
|
|
{
|
2003-10-16 09:16:28 +00:00
|
|
|
kobj_method_t *methods = cls->methods;
|
|
|
|
kobj_method_t *ce;
|
|
|
|
|
|
|
|
for (ce = methods; ce && ce->desc; ce++) {
|
|
|
|
if (ce->desc == desc) {
|
|
|
|
return ce;
|
2000-04-08 14:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2009-02-03 07:54:42 +00:00
|
|
|
return NULL;
|
2003-10-16 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static kobj_method_t*
|
|
|
|
kobj_lookup_method_mi(kobj_class_t cls,
|
|
|
|
kobjop_desc_t desc)
|
|
|
|
{
|
|
|
|
kobj_method_t *ce;
|
|
|
|
kobj_class_t *basep;
|
|
|
|
|
|
|
|
ce = kobj_lookup_method_class(cls, desc);
|
|
|
|
if (ce)
|
|
|
|
return ce;
|
|
|
|
|
|
|
|
basep = cls->baseclasses;
|
|
|
|
if (basep) {
|
|
|
|
for (; *basep; basep++) {
|
|
|
|
ce = kobj_lookup_method_mi(*basep, desc);
|
|
|
|
if (ce)
|
|
|
|
return ce;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 07:54:42 +00:00
|
|
|
return NULL;
|
2003-10-16 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kobj_method_t*
|
|
|
|
kobj_lookup_method(kobj_class_t cls,
|
|
|
|
kobj_method_t **cep,
|
|
|
|
kobjop_desc_t desc)
|
|
|
|
{
|
|
|
|
kobj_method_t *ce;
|
|
|
|
|
|
|
|
#ifdef KOBJ_STATS
|
|
|
|
/*
|
|
|
|
* Correct for the 'hit' assumption in KOBJOPLOOKUP and record
|
|
|
|
* a 'miss'.
|
|
|
|
*/
|
|
|
|
kobj_lookup_hits--;
|
2005-12-29 18:00:42 +00:00
|
|
|
kobj_lookup_misses++;
|
2003-10-16 09:16:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ce = kobj_lookup_method_mi(cls, desc);
|
|
|
|
if (!ce)
|
2011-11-09 11:00:29 +00:00
|
|
|
ce = &desc->deflt;
|
2003-10-16 09:16:28 +00:00
|
|
|
*cep = ce;
|
|
|
|
return ce;
|
2000-04-08 14:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kobj_class_free(kobj_class_t cls)
|
|
|
|
{
|
2009-02-03 07:54:42 +00:00
|
|
|
void* ops = NULL;
|
2000-04-08 14:17:18 +00:00
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_ASSERT(MA_NOTOWNED);
|
|
|
|
KOBJ_LOCK();
|
2000-04-08 14:17:18 +00:00
|
|
|
|
|
|
|
/*
|
2003-10-16 09:16:28 +00:00
|
|
|
* Protect against a race between kobj_create and
|
|
|
|
* kobj_delete.
|
2000-04-08 14:17:18 +00:00
|
|
|
*/
|
2003-10-16 09:16:28 +00:00
|
|
|
if (cls->refs == 0) {
|
|
|
|
/*
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
* For now we don't do anything to unregister any methods
|
|
|
|
* which are no longer used.
|
2003-10-16 09:16:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free memory and clean up.
|
|
|
|
*/
|
|
|
|
ops = cls->ops;
|
2009-02-03 07:54:42 +00:00
|
|
|
cls->ops = NULL;
|
2003-10-16 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2003-10-16 09:16:28 +00:00
|
|
|
|
|
|
|
if (ops)
|
|
|
|
free(ops, M_KOBJ);
|
2000-04-08 14:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kobj_t
|
|
|
|
kobj_create(kobj_class_t cls,
|
|
|
|
struct malloc_type *mtype,
|
|
|
|
int mflags)
|
|
|
|
{
|
|
|
|
kobj_t obj;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and initialise the new object.
|
|
|
|
*/
|
2000-12-08 21:51:06 +00:00
|
|
|
obj = malloc(cls->size, mtype, mflags | M_ZERO);
|
2000-04-08 14:17:18 +00:00
|
|
|
if (!obj)
|
2009-02-03 07:54:42 +00:00
|
|
|
return NULL;
|
2000-04-08 14:17:18 +00:00
|
|
|
kobj_init(obj, cls);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
static void
|
|
|
|
kobj_init_common(kobj_t obj, kobj_class_t cls)
|
|
|
|
{
|
|
|
|
|
|
|
|
obj->ops = cls->ops;
|
|
|
|
cls->refs++;
|
|
|
|
}
|
|
|
|
|
2000-04-08 14:17:18 +00:00
|
|
|
void
|
|
|
|
kobj_init(kobj_t obj, kobj_class_t cls)
|
|
|
|
{
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_ASSERT(MA_NOTOWNED);
|
2003-10-16 09:16:28 +00:00
|
|
|
retry:
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_LOCK();
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2000-04-08 14:17:18 +00:00
|
|
|
/*
|
|
|
|
* Consider compiling the class' method table.
|
|
|
|
*/
|
2003-10-16 09:16:28 +00:00
|
|
|
if (!cls->ops) {
|
|
|
|
/*
|
|
|
|
* kobj_class_compile doesn't want the lock held
|
|
|
|
* because of the call to malloc - we drop the lock
|
|
|
|
* and re-try.
|
|
|
|
*/
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2000-04-08 14:17:18 +00:00
|
|
|
kobj_class_compile(cls);
|
2003-10-16 09:16:28 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2000-04-08 14:17:18 +00:00
|
|
|
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
kobj_init_common(obj, cls);
|
2003-10-16 09:16:28 +00:00
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2000-04-08 14:17:18 +00:00
|
|
|
}
|
|
|
|
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
void
|
|
|
|
kobj_init_static(kobj_t obj, kobj_class_t cls)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(kobj_mutex_inited == 0,
|
|
|
|
("%s: only supported during early cycles", __func__));
|
|
|
|
|
|
|
|
kobj_init_common(obj, cls);
|
|
|
|
}
|
|
|
|
|
2000-04-08 14:17:18 +00:00
|
|
|
void
|
|
|
|
kobj_delete(kobj_t obj, struct malloc_type *mtype)
|
|
|
|
{
|
|
|
|
kobj_class_t cls = obj->ops->cls;
|
2003-10-16 09:16:28 +00:00
|
|
|
int refs;
|
2000-04-08 14:17:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Consider freeing the compiled method table for the class
|
|
|
|
* after its last instance is deleted. As an optimisation, we
|
|
|
|
* should defer this for a short while to avoid thrashing.
|
|
|
|
*/
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_ASSERT(MA_NOTOWNED);
|
|
|
|
KOBJ_LOCK();
|
2000-05-01 10:45:15 +00:00
|
|
|
cls->refs--;
|
2003-10-16 09:16:28 +00:00
|
|
|
refs = cls->refs;
|
2008-12-20 00:33:10 +00:00
|
|
|
KOBJ_UNLOCK();
|
2003-10-16 09:16:28 +00:00
|
|
|
|
|
|
|
if (!refs)
|
2000-04-08 14:17:18 +00:00
|
|
|
kobj_class_free(cls);
|
|
|
|
|
2009-02-03 07:54:42 +00:00
|
|
|
obj->ops = NULL;
|
2000-04-08 14:17:18 +00:00
|
|
|
if (mtype)
|
|
|
|
free(obj, mtype);
|
|
|
|
}
|