public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] media: imx-jpeg: convert kzalloc_obj() to devm_kzalloc()
@ 2026-03-08  6:05 Sanjay Chitroda
  2026-03-09 15:53 ` Marc Kleine-Budde
  0 siblings, 1 reply; 3+ messages in thread
From: Sanjay Chitroda @ 2026-03-08  6:05 UTC (permalink / raw)
  To: mirela.rabulea, mchehab, Frank.Li, s.hauer
  Cc: kernel, festevam, linux-media, linux-arm-kernel, linux-kernel,
	skhan

From: Sanjay Chitroda <sanjayembeddedse@gmail.com>

The driver allcoates memory using kzalloc_obj() and frees it in the relase
path. since the allocated memory is tied to the lifetime of the device,
devm_kzalloc() can be used instead.

Using device-managed allocation simplifies the error handling paths and
remove the need for manual cleanup.

No functional change intended.

Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
---
Changes in v2:
 - Rebase on tip of latest tip
 - Updated commit message accordingly.
 - Link to v1 https://lore.kernel.org/all/20260307210404.1428894-1-sanjayembedded@gmail.com/
---
 drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c b/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
index b442dcba02e7..bd4b5f08a85c 100644
--- a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
+++ b/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c
@@ -2200,14 +2200,12 @@ static int mxc_jpeg_open(struct file *file)
 	struct mxc_jpeg_ctx *ctx;
 	int ret = 0;
 
-	ctx = kzalloc_obj(*ctx);
+	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
-	if (mutex_lock_interruptible(&mxc_jpeg->lock)) {
-		ret = -ERESTARTSYS;
-		goto free;
-	}
+	if (mutex_lock_interruptible(&mxc_jpeg->lock))
+		return -ERESTARTSYS;
 
 	v4l2_fh_init(&ctx->fh, mxc_vfd);
 	v4l2_fh_add(&ctx->fh, file);
@@ -2246,8 +2244,6 @@ static int mxc_jpeg_open(struct file *file)
 	v4l2_fh_del(&ctx->fh, file);
 	v4l2_fh_exit(&ctx->fh);
 	mutex_unlock(&mxc_jpeg->lock);
-free:
-	kfree(ctx);
 	return ret;
 }
 
@@ -2754,7 +2750,6 @@ static int mxc_jpeg_release(struct file *file)
 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
 	v4l2_fh_del(&ctx->fh, file);
 	v4l2_fh_exit(&ctx->fh);
-	kfree(ctx);
 	mutex_unlock(&mxc_jpeg->lock);
 
 	return 0;
-- 
2.34.1


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

* Re: [PATCH v2] media: imx-jpeg: convert kzalloc_obj() to devm_kzalloc()
  2026-03-08  6:05 [PATCH v2] media: imx-jpeg: convert kzalloc_obj() to devm_kzalloc() Sanjay Chitroda
@ 2026-03-09 15:53 ` Marc Kleine-Budde
  2026-03-10  2:58   ` Sanjay Chitroda
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Kleine-Budde @ 2026-03-09 15:53 UTC (permalink / raw)
  To: Sanjay Chitroda
  Cc: mirela.rabulea, mchehab, Frank.Li, s.hauer, linux-kernel, kernel,
	skhan, festevam, linux-arm-kernel, linux-media

[-- Attachment #1: Type: text/plain, Size: 882 bytes --]

On 08.03.2026 11:35:54, Sanjay Chitroda wrote:
> From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
>
> The driver allcoates memory using kzalloc_obj() and frees it in the relase
> path. since the allocated memory is tied to the lifetime of the device,
> devm_kzalloc() can be used instead.

What happens if you issue multiple open()/close() cycles per device
lifetime? Will the memory pile up, until the mxc_jpeg_remove() function
is called?

> Using device-managed allocation simplifies the error handling paths and
> remove the need for manual cleanup.
>
> No functional change intended.

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v2] media: imx-jpeg: convert kzalloc_obj() to devm_kzalloc()
  2026-03-09 15:53 ` Marc Kleine-Budde
@ 2026-03-10  2:58   ` Sanjay Chitroda
  0 siblings, 0 replies; 3+ messages in thread
From: Sanjay Chitroda @ 2026-03-10  2:58 UTC (permalink / raw)
  To: Marc Kleine-Budde
  Cc: mirela.rabulea, mchehab, Frank.Li, s.hauer, linux-kernel, kernel,
	skhan, festevam, linux-arm-kernel, linux-media



On 9 March 2026 9:23:57 pm IST, Marc Kleine-Budde <mkl@blackshift.org> wrote:
>On 08.03.2026 11:35:54, Sanjay Chitroda wrote:
>> From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
>>
>> The driver allcoates memory using kzalloc_obj() and frees it in the relase
>> path. since the allocated memory is tied to the lifetime of the device,
>> devm_kzalloc() can be used instead.
>
>What happens if you issue multiple open()/close() cycles per device
>lifetime? Will the memory pile up, until the mxc_jpeg_remove() function
>is called?

You are correct.

Since the context structure is allocated in .open() and released in
.release(), its lifetime is tied to the file handle rather than the
device.

Using devm_kzalloc() would defer freeing the memory until device
removal, which could cause memory accumulation across multiple
open()/close() cycles.

I'll drop this change.

>
>> Using device-managed allocation simplifies the error handling paths and
>> remove the need for manual cleanup.
>>
>> No functional change intended.
>
>Marc
>

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

end of thread, other threads:[~2026-03-10  2:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-08  6:05 [PATCH v2] media: imx-jpeg: convert kzalloc_obj() to devm_kzalloc() Sanjay Chitroda
2026-03-09 15:53 ` Marc Kleine-Budde
2026-03-10  2:58   ` Sanjay Chitroda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox