[PATCH 1/2] kobject: add kobject_read()

From: Kuninori Morimoto
Date: Tue Apr 10 2018 - 02:54:04 EST



From: Kuninori Morimoto <kuninori.morimoto.gx@xxxxxxxxxxx>

We are counting kobject by using kobject_get/put(), but
not have kobject_read() which can be used to check count.
Let's add

Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@xxxxxxxxxxx>
---
include/linux/kobject.h | 1 +
lib/kobject.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)

diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index 7f6f93c..6f74d4b 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -108,6 +108,7 @@ extern struct kobject * __must_check kobject_create_and_add(const char *name,
extern int __must_check kobject_rename(struct kobject *, const char *new_name);
extern int __must_check kobject_move(struct kobject *, struct kobject *);

+extern unsigned int kobject_read(struct kobject *kobj);
extern struct kobject *kobject_get(struct kobject *kobj);
extern struct kobject * __must_check kobject_get_unless_zero(
struct kobject *kobj);
diff --git a/lib/kobject.c b/lib/kobject.c
index afd5a3f..103c8c8 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -561,6 +561,24 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent)
EXPORT_SYMBOL_GPL(kobject_move);

/**
+ * kobject_read - read refcount count for object.
+ * @kobj: object.
+ */
+unsigned int kobject_read(struct kobject *kobj)
+{
+ if (kobj) {
+ if (!kobj->state_initialized)
+ WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
+ "initialized, yet kobject_put() is being "
+ "called.\n", kobject_name(kobj), kobj);
+ return kref_read(&kobj->kref);
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(kobject_read);
+
+/**
* kobject_del - unlink kobject from hierarchy.
* @kobj: object.
*/
--
1.9.1