From 764dfe0b8c158293b2905f5a1fe213bd91d4e3f0 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 24 Aug 2004 22:30:41 -0700 Subject: [PATCH] kobject: convert struct kobject use kref. Signed-off-by: Greg Kroah-Hartman --- include/linux/kobject.h | 3 ++- lib/Makefile | 5 +---- lib/kobject.c | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/linux/kobject.h b/include/linux/kobject.h index 906d3bf0ff737..4ec94fa8163ff 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #define KOBJ_NAME_LEN 20 @@ -26,7 +27,7 @@ struct kobject { char * k_name; char name[KOBJ_NAME_LEN]; - atomic_t refcount; + struct kref kref; struct list_head entry; struct kobject * parent; struct kset * kset; diff --git a/lib/Makefile b/lib/Makefile index 295e70bcd223c..a35967155d289 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -5,12 +5,9 @@ lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \ bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \ - kobject.o idr.o div64.o parser.o int_sqrt.o \ + kobject.o kref.o idr.o div64.o parser.o int_sqrt.o \ bitmap.o extable.o -# hack for now till some static code uses krefs, then it can move up above... -obj-y += kref.o - lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o diff --git a/lib/kobject.c b/lib/kobject.c index e1aa086c6a3ba..a971b8e55e6b7 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -243,10 +243,9 @@ void kobject_hotplug(const char *action, struct kobject *kobj) * kobject_init - initialize object. * @kobj: object in question. */ - void kobject_init(struct kobject * kobj) { - atomic_set(&kobj->refcount,1); + kref_init(&kobj->kref); INIT_LIST_HEAD(&kobj->entry); kobj->kset = kset_get(kobj->kset); } @@ -447,10 +446,8 @@ void kobject_unregister(struct kobject * kobj) struct kobject * kobject_get(struct kobject * kobj) { - if (kobj) { - WARN_ON(!atomic_read(&kobj->refcount)); - atomic_inc(&kobj->refcount); - } + if (kobj) + kref_get(&kobj->kref); return kobj; } @@ -477,17 +474,21 @@ void kobject_cleanup(struct kobject * kobj) kobject_put(parent); } +static void kobject_release(struct kref *kref) +{ + kobject_cleanup(container_of(kref, struct kobject, kref)); +} + /** * kobject_put - decrement refcount for object. * @kobj: object. * * Decrement the refcount, and if 0, call kobject_cleanup(). */ - void kobject_put(struct kobject * kobj) { - if (atomic_dec_and_test(&kobj->refcount)) - kobject_cleanup(kobj); + if (kobj) + kref_put(&kobj->kref, kobject_release); } -- 2.53.0