summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_kobj.c
diff options
context:
space:
mode:
authormarius <marius@FreeBSD.org>2011-11-15 20:11:03 +0000
committermarius <marius@FreeBSD.org>2011-11-15 20:11:03 +0000
commit4b6458192ff06c2470fb447cad0f59bf228acf93 (patch)
treec7a8cedd82429cdd44f1bd8e89a8d2e2a89ea485 /sys/kern/subr_kobj.c
parent1db8769eb7c79b6958450e127d5e040d29907391 (diff)
downloadFreeBSD-src-4b6458192ff06c2470fb447cad0f59bf228acf93.zip
FreeBSD-src-4b6458192ff06c2470fb447cad0f59bf228acf93.tar.gz
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
Diffstat (limited to 'sys/kern/subr_kobj.c')
-rw-r--r--sys/kern/subr_kobj.c75
1 files changed, 31 insertions, 44 deletions
diff --git a/sys/kern/subr_kobj.c b/sys/kern/subr_kobj.c
index 821a712..5be746a 100644
--- a/sys/kern/subr_kobj.c
+++ b/sys/kern/subr_kobj.c
@@ -60,18 +60,9 @@ static struct mtx kobj_mtx;
static int kobj_mutex_inited;
static int kobj_next_id = 1;
-/*
- * In the event that kobj_mtx has not been initialized yet,
- * we will ignore it, and run without locks in order to support
- * use of KOBJ before mutexes are available. This early in the boot
- * process, everything is single threaded and so races should not
- * happen. This is used to provide the PMAP layer on PowerPC, as well
- * as board support.
- */
-
-#define KOBJ_LOCK() if (kobj_mutex_inited) mtx_lock(&kobj_mtx);
-#define KOBJ_UNLOCK() if (kobj_mutex_inited) mtx_unlock(&kobj_mtx);
-#define KOBJ_ASSERT(what) if (kobj_mutex_inited) mtx_assert(&kobj_mtx,what);
+#define KOBJ_LOCK() mtx_lock(&kobj_mtx)
+#define KOBJ_UNLOCK() mtx_unlock(&kobj_mtx)
+#define KOBJ_ASSERT(what) mtx_assert(&kobj_mtx, what);
SYSCTL_INT(_kern, OID_AUTO, kobj_methodcount, CTLFLAG_RD,
&kobj_next_id, 0, "");
@@ -104,28 +95,11 @@ kobj_error_method(void)
}
static void
-kobj_register_method(struct kobjop_desc *desc)
-{
- KOBJ_ASSERT(MA_OWNED);
-
- if (desc->id == 0) {
- desc->id = kobj_next_id++;
- }
-}
-
-static void
-kobj_unregister_method(struct kobjop_desc *desc)
-{
-}
-
-static void
kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
{
kobj_method_t *m;
int i;
- KOBJ_ASSERT(MA_OWNED);
-
/*
* Don't do anything if we are already compiled.
*/
@@ -135,8 +109,10 @@ kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
/*
* First register any methods which need it.
*/
- for (i = 0, m = cls->methods; m->desc; i++, m++)
- kobj_register_method(m->desc);
+ for (i = 0, m = cls->methods; m->desc; i++, m++) {
+ if (m->desc->id == 0)
+ m->desc->id = kobj_next_id++;
+ }
/*
* Then initialise the ops table.
@@ -159,7 +135,7 @@ kobj_class_compile(kobj_class_t cls)
*/
ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT);
if (!ops)
- panic("kobj_compile_methods: out of memory");
+ panic("%s: out of memory", __func__);
KOBJ_LOCK();
@@ -182,17 +158,14 @@ void
kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
{
- KOBJ_ASSERT(MA_NOTOWNED);
+ KASSERT(kobj_mutex_inited == 0,
+ ("%s: only supported during early cycles", __func__));
/*
* Increment refs to make sure that the ops table is not freed.
*/
- KOBJ_LOCK();
-
cls->refs++;
kobj_class_compile_common(cls, ops);
-
- KOBJ_UNLOCK();
}
static kobj_method_t*
@@ -259,8 +232,6 @@ kobj_lookup_method(kobj_class_t cls,
void
kobj_class_free(kobj_class_t cls)
{
- int i;
- kobj_method_t *m;
void* ops = NULL;
KOBJ_ASSERT(MA_NOTOWNED);
@@ -272,10 +243,9 @@ kobj_class_free(kobj_class_t cls)
*/
if (cls->refs == 0) {
/*
- * Unregister any methods which are no longer used.
+ * For now we don't do anything to unregister any methods
+ * which are no longer used.
*/
- for (i = 0, m = cls->methods; m->desc; i++, m++)
- kobj_unregister_method(m->desc);
/*
* Free memory and clean up.
@@ -308,6 +278,14 @@ kobj_create(kobj_class_t cls,
return obj;
}
+static void
+kobj_init_common(kobj_t obj, kobj_class_t cls)
+{
+
+ obj->ops = cls->ops;
+ cls->refs++;
+}
+
void
kobj_init(kobj_t obj, kobj_class_t cls)
{
@@ -329,13 +307,22 @@ kobj_init(kobj_t obj, kobj_class_t cls)
goto retry;
}
- obj->ops = cls->ops;
- cls->refs++;
+ kobj_init_common(obj, cls);
KOBJ_UNLOCK();
}
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);
+}
+
+void
kobj_delete(kobj_t obj, struct malloc_type *mtype)
{
kobj_class_t cls = obj->ops->cls;
OpenPOWER on IntegriCloud