* [PATCH] driver core: class: properly reference count class_dev_iter()
@ 2023-05-16 19:20 Greg Kroah-Hartman
2023-05-17 10:41 ` Rafael J. Wysocki
2023-05-18 19:42 ` Mirsad Goran Todorovac
0 siblings, 2 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2023-05-16 19:20 UTC (permalink / raw)
To: linux-kernel
Cc: linux-usb, Greg Kroah-Hartman, syzbot+e7afd76ad060fa0d2605,
Mirsad Goran Todorovac, Rafael J. Wysocki, Alan Stern
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
Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/base/class.c | 2 ++
include/linux/device/class.h | 1 +
2 files changed, 3 insertions(+)
diff --git a/drivers/base/class.c b/drivers/base/class.c
index ac1808d1a2e8..05d9df90f621 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -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);
diff --git a/include/linux/device/class.h b/include/linux/device/class.h
index 9deeaeb457bb..abf3d3bfb6fe 100644
--- a/include/linux/device/class.h
+++ b/include/linux/device/class.h
@@ -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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] driver core: class: properly reference count class_dev_iter()
2023-05-16 19:20 [PATCH] driver core: class: properly reference count class_dev_iter() Greg Kroah-Hartman
@ 2023-05-17 10:41 ` Rafael J. Wysocki
2023-05-18 19:42 ` Mirsad Goran Todorovac
1 sibling, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2023-05-17 10:41 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-kernel, linux-usb, syzbot+e7afd76ad060fa0d2605,
Mirsad Goran Todorovac, Rafael J. Wysocki, Alan Stern
On Tue, May 16, 2023 at 9:20 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> 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
> Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Alan Stern <stern@rowland.harvard.edu>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
> ---
> drivers/base/class.c | 2 ++
> include/linux/device/class.h | 1 +
> 2 files changed, 3 insertions(+)
>
> diff --git a/drivers/base/class.c b/drivers/base/class.c
> index ac1808d1a2e8..05d9df90f621 100644
> --- a/drivers/base/class.c
> +++ b/drivers/base/class.c
> @@ -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);
>
> diff --git a/include/linux/device/class.h b/include/linux/device/class.h
> index 9deeaeb457bb..abf3d3bfb6fe 100644
> --- a/include/linux/device/class.h
> +++ b/include/linux/device/class.h
> @@ -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
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] driver core: class: properly reference count class_dev_iter()
2023-05-16 19:20 [PATCH] driver core: class: properly reference count class_dev_iter() Greg Kroah-Hartman
2023-05-17 10:41 ` Rafael J. Wysocki
@ 2023-05-18 19:42 ` Mirsad Goran Todorovac
1 sibling, 0 replies; 3+ messages in thread
From: Mirsad Goran Todorovac @ 2023-05-18 19:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, linux-kernel
Cc: linux-usb, syzbot+e7afd76ad060fa0d2605, Rafael J. Wysocki,
Alan Stern
On 5/16/23 21:20, Greg Kroah-Hartman wrote:
> 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
> Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Alan Stern <stern@rowland.harvard.edu>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Hi, Greg,
Did I forget to give the
Tested-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
I apologise if I did.
Best regards,
Mirsad
> ---
> drivers/base/class.c | 2 ++
> include/linux/device/class.h | 1 +
> 2 files changed, 3 insertions(+)
>
> diff --git a/drivers/base/class.c b/drivers/base/class.c
> index ac1808d1a2e8..05d9df90f621 100644
> --- a/drivers/base/class.c
> +++ b/drivers/base/class.c
> @@ -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);
>
> diff --git a/include/linux/device/class.h b/include/linux/device/class.h
> index 9deeaeb457bb..abf3d3bfb6fe 100644
> --- a/include/linux/device/class.h
> +++ b/include/linux/device/class.h
> @@ -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);
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-05-18 19:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-16 19:20 [PATCH] driver core: class: properly reference count class_dev_iter() Greg Kroah-Hartman
2023-05-17 10:41 ` Rafael J. Wysocki
2023-05-18 19:42 ` Mirsad Goran Todorovac
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).