Commit ddaf098e authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

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.

Reported-and-tested-by: syzbot+e7afd76ad060fa0d2605@syzkaller.appspotmail.com
Fixes: 7b884b7f ("driver core: class.c: convert to only use class_to_subsys")
Reported-by: default avatarMirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Cc: Alan Stern <stern@rowland.harvard.edu>
Acked-by: default avatarRafael J. Wysocki <rafael@kernel.org>
Tested-by: default avatarMirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Link: https://lore.kernel.org/r/2023051610-stove-condense-9a77@gregkhSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 1e94be78
...@@ -320,6 +320,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, ...@@ -320,6 +320,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
start_knode = &start->p->knode_class; start_knode = &start->p->knode_class;
klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
iter->type = type; iter->type = type;
iter->sp = sp;
} }
EXPORT_SYMBOL_GPL(class_dev_iter_init); EXPORT_SYMBOL_GPL(class_dev_iter_init);
...@@ -361,6 +362,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_next); ...@@ -361,6 +362,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_next);
void class_dev_iter_exit(struct class_dev_iter *iter) void class_dev_iter_exit(struct class_dev_iter *iter)
{ {
klist_iter_exit(&iter->ki); klist_iter_exit(&iter->ki);
subsys_put(iter->sp);
} }
EXPORT_SYMBOL_GPL(class_dev_iter_exit); EXPORT_SYMBOL_GPL(class_dev_iter_exit);
......
...@@ -74,6 +74,7 @@ struct class { ...@@ -74,6 +74,7 @@ struct class {
struct class_dev_iter { struct class_dev_iter {
struct klist_iter ki; struct klist_iter ki;
const struct device_type *type; const struct device_type *type;
struct subsys_private *sp;
}; };
int __must_check class_register(const struct class *class); int __must_check class_register(const struct class *class);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment