From 792bcc4263c56c520f454875df3752b374791d6e Mon Sep 17 00:00:00 2001
From: James Morse <james.morse@arm.com>
Date: Thu, 16 Sep 2021 16:45:41 +0100
Subject: [PATCH] kobject: Add kset_get_next_obj() to allow a kset to be walked

To expose iommu_groups via the resctrl filesystem, the resctrl driver
needs to be able to walk the list of iommu_groups. These are exposed
via sysfs as a kset.

Add kset_get_next_obj() to allow resctrl to walk the kobjects in the
kset.

Signed-off-by: James Morse <james.morse@arm.com>
---
 include/linux/kobject.h |  2 ++
 lib/kobject.c           | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index bdab370a24f4e..c5f8878836e06 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -205,6 +205,8 @@ static inline const struct kobj_type *get_ktype(const struct kobject *kobj)
 
 extern struct kobject *kset_find_obj(struct kset *, const char *);
 
+struct kobject *kset_get_next_obj(struct kset *kset, struct kobject *prev);
+
 /* The global /sys/kernel/ kobject for people to chain off of */
 extern struct kobject *kernel_kobj;
 /* The global /sys/kernel/mm/ kobject for people to chain off of */
diff --git a/lib/kobject.c b/lib/kobject.c
index 6e2f0bee35604..cbf41f3be53b7 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -907,6 +907,27 @@ struct kobject *kset_find_obj(struct kset *kset, const char *name)
 }
 EXPORT_SYMBOL_GPL(kset_find_obj);
 
+struct kobject *kset_get_next_obj(struct kset *kset, struct kobject *prev)
+{
+	struct kobject *k;
+
+	spin_lock(&kset->list_lock);
+
+	if (!prev)
+		k = list_first_entry_or_null(&kset->list, typeof(*k), entry);
+	else
+		k = list_next_entry(prev, entry);
+
+	if (list_entry_is_head(k, &kset->list, entry))
+		k = NULL;
+
+	kobject_get(k);
+	spin_unlock(&kset->list_lock);
+	kobject_put(prev);
+
+	return k;
+}
+
 static void kset_release(struct kobject *kobj)
 {
 	struct kset *kset = container_of(kobj, struct kset, kobj);
-- 
GitLab