[PATCH] serial: core: Fix kmemleak issue for serial core device remove

Tony Lindgren posted 1 patch 2 years, 1 month ago
drivers/tty/serial/serial_base_bus.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] serial: core: Fix kmemleak issue for serial core device remove
Posted by Tony Lindgren 2 years, 1 month ago
Kmemleak reports issues for serial8250 ports after the hardware specific
driver takes over on boot as noted by Tomi.

The kerneldoc for device_initialize() says we must call device_put()
after calling device_initialize(). We are calling device_put() on the
error path, but are missing it from the device remove path. This causes
release() to never get called for the devices on remove.

Let's add the missing put_device() calls for both serial ctrl and
port devices.

Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
Reported-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/tty/serial/serial_base_bus.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
--- a/drivers/tty/serial/serial_base_bus.c
+++ b/drivers/tty/serial/serial_base_bus.c
@@ -99,6 +99,7 @@ void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
 		return;
 
 	device_del(&ctrl_dev->dev);
+	put_device(&ctrl_dev->dev);
 }
 
 struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
@@ -174,6 +175,7 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
 		return;
 
 	device_del(&port_dev->dev);
+	put_device(&port_dev->dev);
 }
 
 static int serial_base_init(void)
-- 
2.41.0
Re: [PATCH] serial: core: Fix kmemleak issue for serial core device remove
Posted by Tomi Valkeinen 2 years, 1 month ago
On 04/08/2023 12:09, Tony Lindgren wrote:
> Kmemleak reports issues for serial8250 ports after the hardware specific
> driver takes over on boot as noted by Tomi.
> 
> The kerneldoc for device_initialize() says we must call device_put()
> after calling device_initialize(). We are calling device_put() on the
> error path, but are missing it from the device remove path. This causes
> release() to never get called for the devices on remove.
> 
> Let's add the missing put_device() calls for both serial ctrl and
> port devices.
> 
> Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
> Reported-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> Signed-off-by: Tony Lindgren <tony@atomide.com>
> ---
>   drivers/tty/serial/serial_base_bus.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
> --- a/drivers/tty/serial/serial_base_bus.c
> +++ b/drivers/tty/serial/serial_base_bus.c
> @@ -99,6 +99,7 @@ void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
>   		return;
>   
>   	device_del(&ctrl_dev->dev);
> +	put_device(&ctrl_dev->dev);
>   }
>   
>   struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
> @@ -174,6 +175,7 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
>   		return;
>   
>   	device_del(&port_dev->dev);
> +	put_device(&port_dev->dev);
>   }
>   
>   static int serial_base_init(void)

Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>

  Tomi
Re: [PATCH] serial: core: Fix kmemleak issue for serial core device remove
Posted by Jiri Slaby 2 years, 1 month ago
On 04. 08. 23, 11:09, Tony Lindgren wrote:
> Kmemleak reports issues for serial8250 ports after the hardware specific
> driver takes over on boot as noted by Tomi.
> 
> The kerneldoc for device_initialize() says we must call device_put()
> after calling device_initialize(). We are calling device_put() on the
> error path, but are missing it from the device remove path. This causes
> release() to never get called for the devices on remove.
> 
> Let's add the missing put_device() calls for both serial ctrl and
> port devices.
> 
> Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
> Reported-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> Signed-off-by: Tony Lindgren <tony@atomide.com>
> ---
>   drivers/tty/serial/serial_base_bus.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
> --- a/drivers/tty/serial/serial_base_bus.c
> +++ b/drivers/tty/serial/serial_base_bus.c
> @@ -99,6 +99,7 @@ void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
>   		return;
>   
>   	device_del(&ctrl_dev->dev);
> +	put_device(&ctrl_dev->dev);
>   }
>   
>   struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
> @@ -174,6 +175,7 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
>   		return;
>   
>   	device_del(&port_dev->dev);
> +	put_device(&port_dev->dev);

I didn't check the code, but device_unregister()?

-- 
js
suse labs
Re: [PATCH] serial: core: Fix kmemleak issue for serial core device remove
Posted by Tony Lindgren 2 years, 1 month ago
* Jiri Slaby <jirislaby@kernel.org> [230804 09:16]:
> On 04. 08. 23, 11:09, Tony Lindgren wrote:
> > Kmemleak reports issues for serial8250 ports after the hardware specific
> > driver takes over on boot as noted by Tomi.
> > 
> > The kerneldoc for device_initialize() says we must call device_put()
> > after calling device_initialize(). We are calling device_put() on the
> > error path, but are missing it from the device remove path. This causes
> > release() to never get called for the devices on remove.
> > 
> > Let's add the missing put_device() calls for both serial ctrl and
> > port devices.
> > 
> > Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
> > Reported-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> > Signed-off-by: Tony Lindgren <tony@atomide.com>
> > ---
> >   drivers/tty/serial/serial_base_bus.c | 2 ++
> >   1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
> > --- a/drivers/tty/serial/serial_base_bus.c
> > +++ b/drivers/tty/serial/serial_base_bus.c
> > @@ -99,6 +99,7 @@ void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
> >   		return;
> >   	device_del(&ctrl_dev->dev);
> > +	put_device(&ctrl_dev->dev);
> >   }
> >   struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
> > @@ -174,6 +175,7 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
> >   		return;
> >   	device_del(&port_dev->dev);
> > +	put_device(&port_dev->dev);
> 
> I didn't check the code, but device_unregister()?

I thought about that as it does the same, but since we're not calling
device_register() I felt it would be and unpaired call. No objections to
changing to use device_unregister() naturally if folks prefer that.

Regards,

Tony
Re: [PATCH] serial: core: Fix kmemleak issue for serial core device remove
Posted by Greg Kroah-Hartman 2 years, 1 month ago
On Fri, Aug 04, 2023 at 12:21:05PM +0300, Tony Lindgren wrote:
> * Jiri Slaby <jirislaby@kernel.org> [230804 09:16]:
> > On 04. 08. 23, 11:09, Tony Lindgren wrote:
> > > Kmemleak reports issues for serial8250 ports after the hardware specific
> > > driver takes over on boot as noted by Tomi.
> > > 
> > > The kerneldoc for device_initialize() says we must call device_put()
> > > after calling device_initialize(). We are calling device_put() on the
> > > error path, but are missing it from the device remove path. This causes
> > > release() to never get called for the devices on remove.
> > > 
> > > Let's add the missing put_device() calls for both serial ctrl and
> > > port devices.
> > > 
> > > Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
> > > Reported-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> > > Signed-off-by: Tony Lindgren <tony@atomide.com>
> > > ---
> > >   drivers/tty/serial/serial_base_bus.c | 2 ++
> > >   1 file changed, 2 insertions(+)
> > > 
> > > diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
> > > --- a/drivers/tty/serial/serial_base_bus.c
> > > +++ b/drivers/tty/serial/serial_base_bus.c
> > > @@ -99,6 +99,7 @@ void serial_base_ctrl_device_remove(struct serial_ctrl_device *ctrl_dev)
> > >   		return;
> > >   	device_del(&ctrl_dev->dev);
> > > +	put_device(&ctrl_dev->dev);
> > >   }
> > >   struct serial_ctrl_device *serial_base_ctrl_add(struct uart_port *port,
> > > @@ -174,6 +175,7 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
> > >   		return;
> > >   	device_del(&port_dev->dev);
> > > +	put_device(&port_dev->dev);
> > 
> > I didn't check the code, but device_unregister()?
> 
> I thought about that as it does the same, but since we're not calling
> device_register() I felt it would be and unpaired call. No objections to
> changing to use device_unregister() naturally if folks prefer that.

This is fine as device_register() isn't happening, thanks.

greg k-h