Skip to contentSkip to conversation

driver core: class: properly reference count class_dev_iter()

PATCH In review Greg Kroah-Hartman 3 participants 3 messages May 16, 2023 – May 18, 2023

Subsystems (2)
lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/1 driver core: class: properly reference count class_dev_iter()

When class_dev_iter is initialized, the reference count for the subsys private structure is incremented, but never decremented, causing a memory leak over time. To resolve this, save off a pointer to the internal structure into the class_dev_iter structure and then when the iterator is finished, drop the reference count.

---
 drivers/base/class.c         | 2 ++
 include/linux/device/class.h | 1 +
 2 files changed, 3 insertions(+)
drivers/base/class.c +2 -0
index ac1808d1a2e8..05d9df90f621 100644
@@ -320,6 +320,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
 		start_knode = &start->p->knode_class;
 	klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
 	iter->type = type;
+	iter->sp = sp;
 }
 EXPORT_SYMBOL_GPL(class_dev_iter_init);
 
@@ -361,6 +362,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_next);
 void class_dev_iter_exit(struct class_dev_iter *iter)
 {
 	klist_iter_exit(&iter->ki);
+	subsys_put(iter->sp);
 }
 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
 
include/linux/device/class.h +1 -0
index 9deeaeb457bb..abf3d3bfb6fe 100644
@@ -74,6 +74,7 @@ struct class {
 struct class_dev_iter {
 	struct klist_iter		ki;
 	const struct device_type	*type;
+	struct subsys_private		*sp;
 };
 
 int __must_check class_register(const struct class *class);
Signature · 1 line
2.40.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Greg Kroah-Hartman

When class_dev_iter is initialized, the reference count for the subsys private structure is incremented, but never decremented, causing a memory leak over time. To resolve this, save off a pointer to the internal structure into the class_dev_iter structure and then when the iterator is finished, drop the reference count.

Quoted 5 lines
Quoted 40 lines
---
 drivers/base/class.c         | 2 ++
 include/linux/device/class.h | 1 +
 2 files changed, 3 insertions(+)
drivers/base/class.c +2 -0
index ac1808d1a2e8..05d9df90f621 100644
@@ -320,6 +320,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
                start_knode = &start->p->knode_class;
        klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
        iter->type = type;
+       iter->sp = sp;
 }
 EXPORT_SYMBOL_GPL(class_dev_iter_init);
 
@@ -361,6 +362,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_next);
 void class_dev_iter_exit(struct class_dev_iter *iter)
 {
        klist_iter_exit(&iter->ki);
+       subsys_put(iter->sp);
 }
 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
 
include/linux/device/class.h +1 -1
index 9deeaeb457bb..abf3d3bfb6fe 100644
@@ -74,6 +74,7 @@ struct class {
 struct class_dev_iter {
        struct klist_iter               ki;
        const struct device_type        *type;
+       struct subsys_private           *sp;
 };
 
 int __must_check class_register(const struct class *class);
--

2.40.1

Tested-by on v1 1/1
#2 lore raw
In reply to Greg Kroah-Hartman
Quoted 11 lines · Greg Kroah-Hartman

When class_dev_iter is initialized, the reference count for the subsys private structure is incremented, but never decremented, causing a memory leak over time. To resolve this, save off a pointer to the internal structure into the class_dev_iter structure and then when the iterator is finished, drop the reference count.

Hi, Greg,

Did I forget to give the

I apologise if I did.

Best regards,
Mirsad

Quoted 37 lines
---
  drivers/base/class.c         | 2 ++
  include/linux/device/class.h | 1 +
  2 files changed, 3 insertions(+)
drivers/base/class.c +2 -0
index ac1808d1a2e8..05d9df90f621 100644
@@ -320,6 +320,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
  		start_knode = &start->p->knode_class;
  	klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
  	iter->type = type;
+	iter->sp = sp;
  }
  EXPORT_SYMBOL_GPL(class_dev_iter_init);
 
@@ -361,6 +362,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_next);
  void class_dev_iter_exit(struct class_dev_iter *iter)
  {
  	klist_iter_exit(&iter->ki);
+	subsys_put(iter->sp);
  }
  EXPORT_SYMBOL_GPL(class_dev_iter_exit);
 
include/linux/device/class.h +1 -0
index 9deeaeb457bb..abf3d3bfb6fe 100644
@@ -74,6 +74,7 @@ struct class {
  struct class_dev_iter {
  	struct klist_iter		ki;
  	const struct device_type	*type;
+	struct subsys_private		*sp;
  };
 
  int __must_check class_register(const struct class *class);