linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc
@ 2025-08-27 23:11 Osama Abdelkader
  2025-08-28  5:51 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Osama Abdelkader @ 2025-08-27 23:11 UTC (permalink / raw)
  To: gregkh, jirislaby, andriy.shevchenko, elder, benjamin.larsson,
	u.kleine-koenig
  Cc: linux-kernel, linux-serial, Osama Abdelkader

Use devm_kzalloc for automatic memory cleanup.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
---
 drivers/tty/serial/8250/8250_of.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_of.c b/drivers/tty/serial/8250/8250_of.c
index d178b6c54ea1..4fe3b79d0e4f 100644
--- a/drivers/tty/serial/8250/8250_of.c
+++ b/drivers/tty/serial/8250/8250_of.c
@@ -217,14 +217,14 @@ static int of_platform_serial_probe(struct platform_device *ofdev)
 	if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
 		return -EBUSY;
 
-	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	info = devm_kzalloc(&ofdev->dev, sizeof(*info), GFP_KERNEL);
 	if (info == NULL)
 		return -ENOMEM;
 
 	memset(&port8250, 0, sizeof(port8250));
 	ret = of_platform_serial_setup(ofdev, port_type, &port8250, info);
 	if (ret)
-		goto err_free;
+		return ret;
 
 	if (port8250.port.fifosize)
 		port8250.capabilities = UART_CAP_FIFO;
@@ -266,8 +266,6 @@ static int of_platform_serial_probe(struct platform_device *ofdev)
 err_dispose:
 	pm_runtime_put_sync(&ofdev->dev);
 	pm_runtime_disable(&ofdev->dev);
-err_free:
-	kfree(info);
 	return ret;
 }
 
@@ -286,7 +284,6 @@ static void of_platform_serial_remove(struct platform_device *ofdev)
 	reset_control_assert(info->rst);
 	pm_runtime_put_sync(&ofdev->dev);
 	pm_runtime_disable(&ofdev->dev);
-	kfree(info);
 }
 
 #ifdef CONFIG_PM_SLEEP
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc
  2025-08-27 23:11 [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc Osama Abdelkader
@ 2025-08-28  5:51 ` Greg KH
  2025-08-29  9:49   ` Osama Abdelkader
  0 siblings, 1 reply; 5+ messages in thread
From: Greg KH @ 2025-08-28  5:51 UTC (permalink / raw)
  To: Osama Abdelkader
  Cc: jirislaby, andriy.shevchenko, elder, benjamin.larsson,
	u.kleine-koenig, linux-kernel, linux-serial

On Thu, Aug 28, 2025 at 01:11:05AM +0200, Osama Abdelkader wrote:
> Use devm_kzalloc for automatic memory cleanup.

Why?

I do not see a good reason here as to how this makes anything better
overall?  How was it tested?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc
  2025-08-28  5:51 ` Greg KH
@ 2025-08-29  9:49   ` Osama Abdelkader
  2025-08-29 12:44     ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Osama Abdelkader @ 2025-08-29  9:49 UTC (permalink / raw)
  To: Greg KH
  Cc: jirislaby, andriy.shevchenko, elder, benjamin.larsson,
	u.kleine-koenig, linux-kernel, linux-serial


On 8/28/25 7:51 AM, Greg KH wrote:
> On Thu, Aug 28, 2025 at 01:11:05AM +0200, Osama Abdelkader wrote:
>> Use devm_kzalloc for automatic memory cleanup.
> Why?
>
> I do not see a good reason here as to how this makes anything better
> overall?  How was it tested?
>
> thanks,
>
> greg k-h

Hi Greg,

Thanks for the feedback, the change to devm_kzalloc ensures the allocated
memory is tied to the device's lifetime. This removed the need for explicit
kfree() calls in the remove path and avoids potential leaks in probe error 
paths. It also aligns the driver with others in the 8250 subsystem which 
already use devm-managed resources.

For testing, I built the kernel and booted it on QEMU riscv with of_serial
enabled. The driver probed successfully and the serial console worked as
expected, also tested unbinding/rebinding the driver via sysfs to confirm
no leaks or errors occur.

Thanks,
Osama


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc
  2025-08-29  9:49   ` Osama Abdelkader
@ 2025-08-29 12:44     ` Greg KH
  2025-08-29 14:19       ` Osama Abdelkader
  0 siblings, 1 reply; 5+ messages in thread
From: Greg KH @ 2025-08-29 12:44 UTC (permalink / raw)
  To: Osama Abdelkader
  Cc: jirislaby, andriy.shevchenko, elder, benjamin.larsson,
	u.kleine-koenig, linux-kernel, linux-serial

On Fri, Aug 29, 2025 at 11:49:41AM +0200, Osama Abdelkader wrote:
> 
> On 8/28/25 7:51 AM, Greg KH wrote:
> > On Thu, Aug 28, 2025 at 01:11:05AM +0200, Osama Abdelkader wrote:
> >> Use devm_kzalloc for automatic memory cleanup.
> > Why?
> >
> > I do not see a good reason here as to how this makes anything better
> > overall?  How was it tested?
> >
> > thanks,
> >
> > greg k-h
> 
> Hi Greg,
> 
> Thanks for the feedback, the change to devm_kzalloc ensures the allocated
> memory is tied to the device's lifetime. This removed the need for explicit
> kfree() calls in the remove path and avoids potential leaks in probe error 
> paths.

But there are no existing errors, so why change working code?

> It also aligns the driver with others in the 8250 subsystem which 
> already use devm-managed resources.

This code is older than the devm api :)

> For testing, I built the kernel and booted it on QEMU riscv with of_serial
> enabled. The driver probed successfully and the serial console worked as
> expected, also tested unbinding/rebinding the driver via sysfs to confirm
> no leaks or errors occur.

But did you test the error paths?  That is what you changed here.

And changes like this, for old, working, code, is usually not needed
unless you are fixing a bug somewhere.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc
  2025-08-29 12:44     ` Greg KH
@ 2025-08-29 14:19       ` Osama Abdelkader
  0 siblings, 0 replies; 5+ messages in thread
From: Osama Abdelkader @ 2025-08-29 14:19 UTC (permalink / raw)
  To: Greg KH
  Cc: jirislaby, andriy.shevchenko, elder, benjamin.larsson,
	u.kleine-koenig, linux-kernel, linux-serial


On 8/29/25 2:44 PM, Greg KH wrote:
> On Fri, Aug 29, 2025 at 11:49:41AM +0200, Osama Abdelkader wrote:
>> On 8/28/25 7:51 AM, Greg KH wrote:
>>> On Thu, Aug 28, 2025 at 01:11:05AM +0200, Osama Abdelkader wrote:
>>>> Use devm_kzalloc for automatic memory cleanup.
>>> Why?
>>>
>>> I do not see a good reason here as to how this makes anything better
>>> overall?  How was it tested?
>>>
>>> thanks,
>>>
>>> greg k-h
>> Hi Greg,
>>
>> Thanks for the feedback, the change to devm_kzalloc ensures the allocated
>> memory is tied to the device's lifetime. This removed the need for explicit
>> kfree() calls in the remove path and avoids potential leaks in probe error 
>> paths.
> But there are no existing errors, so why change working code?
>
>> It also aligns the driver with others in the 8250 subsystem which 
>> already use devm-managed resources.
> This code is older than the devm api :)
>
>> For testing, I built the kernel and booted it on QEMU riscv with of_serial
>> enabled. The driver probed successfully and the serial console worked as
>> expected, also tested unbinding/rebinding the driver via sysfs to confirm
>> no leaks or errors occur.
> But did you test the error paths?  That is what you changed here.
>
> And changes like this, for old, working, code, is usually not needed
> unless you are fixing a bug somewhere.
>
> thanks,
>
> greg k-h

Thanks Greg, totally understandable. I was looking for a simple patch to be honest
though looked for modernizing some APIs but looks like I chose the wrong driver:D
If you guide me to a simple bug or a simple contribution here would be much appreciated.

Thanks,
Osama


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-29 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-27 23:11 [PATCH] serial: 8250_of: replace kzalloc with devm_kzalloc Osama Abdelkader
2025-08-28  5:51 ` Greg KH
2025-08-29  9:49   ` Osama Abdelkader
2025-08-29 12:44     ` Greg KH
2025-08-29 14:19       ` Osama Abdelkader

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).