media: imx8-isi: fix resource cleanup issues
v3 cover media: imx8-isi: fix resource cleanup issues
This series fixes several resource management bugs in the imx8-isi driver:
- A use-after-free triggered during rmmod because crossbar pads were freed before media links referencing them were removed.
- A memory leak of subdev active state due to missing v4l2_subdev_cleanup() calls in crossbar and pipe cleanup paths.
- A memory leak of isi->pipes on probe failure and normal unbind.
- Missing cleanup of already-initialized pipes when probe fails partway through the channel loop or during v4l2 init.
All four issues trace back to the original driver introduction in cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver").
Changes in V3:
- Dropped the kzalloc_objs() to devm_kcalloc() conversion patch, as there is
ongoing work to add devm_kzalloc_objs(). Will resend once it's available.
- Added patch 3/4 to fix missing v4l2_subdev_cleanup() in pipe init error path.
- Link to V2: https://patchwork.linuxtv.org/project/linux-media/cover/[email protected]/
Changes in V2:
- Split the previous patch 3 ("media: imx8-isi: fix isi->pipes memory leak
in probe and remove") into two separate patches:
- Patch 3/4: fix isi->pipes memory leak by switching to devm_kcalloc()
- Patch 4/4: clean up already-initialized pipes on probe failure
- Link to V1: https://patchwork.linuxtv.org/project/linux-media/cover/[email protected]/
Xiaolei Wang (4):
media: imx8-isi: fix use-after-free on remove
media: imx8-isi: add missing v4l2_subdev_cleanup() in crossbar and
pipe
media: nxp: imx8-isi: Fix missing v4l2_subdev_cleanup() in pipe init
error path
media: imx8-isi: clean up already-initialized pipes on probe failure
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 4 +++- drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 1 + drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 7 +++++-- 3 files changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/4 media: imx8-isi: fix use-after-free on remove
KASAN reports a slab-use-after-free in __media_entity_remove_link() during rmmod of imx8_isi:
BUG: KASAN: slab-use-after-free in __media_entity_remove_link+0x608/0x650 Read of size 2 at addr ffff0000d47cb02a by task rmmod/724 Call trace: __media_entity_remove_link+0x608/0x650 __media_entity_remove_links+0x78/0x144 __media_device_unregister_entity+0x150/0x280 media_device_unregister_entity+0x48/0x68 v4l2_device_unregister_subdev+0x158/0x300 v4l2_async_unbind_subdev_one+0x22c/0x358 v4l2_async_nf_unbind_all_subdevs+0xfc/0x1c0 v4l2_async_nf_unregister+0x5c/0x14c mxc_isi_remove+0x124/0x2a0 [imx8_isi] Allocated by task 249: __kmalloc_noprof+0x27c/0x690 mxc_isi_crossbar_init+0x22c/0x560 [imx8_isi] Freed by task 724: kfree+0x1e4/0x5b0 mxc_isi_crossbar_cleanup+0x34/0x80 [imx8_isi] mxc_isi_remove+0x11c/0x2a0 [imx8_isi]
The problem is that mxc_isi_remove() calls mxc_isi_crossbar_cleanup() before mxc_isi_v4l2_cleanup(). The crossbar cleanup frees the media entity pads, but the subsequent v4l2 cleanup still tries to remove media links that reference those pads.
Fix this by calling mxc_isi_v4l2_cleanup() before mxc_isi_crossbar_cleanup() to ensure all media entities are properly unregistered while the pads are still valid.
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -1
@@ -556,8 +556,8 @@ static void mxc_isi_remove(struct platform_device *pdev)mxc_isi_pipe_cleanup (pipe); } -mxc_isi_crossbar_cleanup (&isi->crossbar);mxc_isi_v4l2_cleanup (isi); +mxc_isi_crossbar_cleanup (&isi->crossbar); }static const struct of_device_id mxc_isi_of_match[] = {
Signature · 1 line
2.43.0
PATCH v3 2/4 media: imx8-isi: add missing v4l2_subdev_cleanup() in crossbar and pipe
In reply to Xiaolei WangBoth mxc_isi_crossbar_init() and mxc_isi_pipe_init() call v4l2_subdev_init_finalize() which allocates the subdev active state, but neither mxc_isi_crossbar_cleanup() nor mxc_isi_pipe_cleanup() calls v4l2_subdev_cleanup() to free it.
This causes a memory leak on every rmmod, reported by kmemleak:
unreferenced object 0xffff0000d06fc800 (size 192):
comm "(udev-worker)", pid 254, jiffies 4294913455
backtrace (crc 36eeae58):
kmemleak_alloc+0x34/0x40
__kvmalloc_node_noprof+0x5f8/0x7d8
__v4l2_subdev_state_alloc+0x1fc/0x30c
__v4l2_subdev_init_finalize+0x178/0x368Add the missing v4l2_subdev_cleanup() calls before media_entity_cleanup() in both crossbar and pipe cleanup paths.
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 1 + drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 1 + 2 files changed, 2 insertions(+)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +1 -0
@@ -491,6 +491,7 @@ int mxc_isi_crossbar_init(struct mxc_isi_dev *isi)void mxc_isi_crossbar_cleanup (struct mxc_isi_crossbar *xbar) { +v4l2_subdev_cleanup (&xbar->sd);media_entity_cleanup (&xbar->sd.entity);kfree (xbar->pads);kfree (xbar->inputs);
drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c +1 -0
@@ -819,6 +819,7 @@ void mxc_isi_pipe_cleanup(struct mxc_isi_pipe *pipe) {struct v4l2_subdev *sd = &pipe->sd; +v4l2_subdev_cleanup (sd);media_entity_cleanup (&sd->entity);mutex_destroy (&pipe->lock); }
Signature · 1 line
2.43.0
PATCH v3 3/4 media: nxp: imx8-isi: Fix missing v4l2_subdev_cleanup() in pipe init error path
In reply to Xiaolei WangAfter v4l2_subdev_init_finalize() succeeds in mxc_isi_pipe_init(), if platform_get_irq() or devm_request_irq() fails, the error path jumps to a label that only calls media_entity_cleanup() and mutex_destroy(), missing the v4l2_subdev_cleanup() call needed to free the subdev active state allocated by v4l2_subdev_init_finalize().
Add an error_subdev label that calls v4l2_subdev_cleanup() before falling through to the existing error cleanup.
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c +4 -2
@@ -796,18 +796,20 @@ int mxc_isi_pipe_init(struct mxc_isi_dev *isi, unsigned int id) irq =platform_get_irq (to_platform_device (isi->dev), id);if (irq <0 ) { ret = irq; - goto error; + goto error_subdev; } ret =devm_request_irq (isi->dev, irq, mxc_isi_pipe_irq_handler,0 ,dev_name (isi->dev), pipe);if (ret <0 ) {dev_err (isi->dev,"failed to request IRQ (%d)\n" , ret); - goto error; + goto error_subdev; }return 0 ; +error_subdev: +v4l2_subdev_cleanup (sd); error:media_entity_cleanup (&sd->entity);mutex_destroy (&pipe->lock);
Signature · 1 line
2.43.0
Re: [PATCH v3 3/4] media: nxp: imx8-isi: Fix missing v4l2_subdev_cleanup() in pipe init error path
Quoted 11 lines · Xiaolei Wang
After
v4l2_subdev_init_finalize()succeeds inmxc_isi_pipe_init(), ifplatform_get_irq()ordevm_request_irq()fails, the error path jumps to a label that only callsmedia_entity_cleanup()andmutex_destroy(), missing thev4l2_subdev_cleanup()call needed to free the subdev active state allocated byv4l2_subdev_init_finalize().Add an
error_subdevlabel that callsv4l2_subdev_cleanup()before falling through to the existing error cleanup.Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")Signed-off-by Xiaolei Wang <[email protected]>
Quoted 31 lines
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c +4
-2@@ -796,18 +796,20 @@ int mxc_isi_pipe_init(struct mxc_isi_dev *isi, unsigned int id) irq =platform_get_irq (to_platform_device (isi->dev), id);if (irq <0 ) { ret = irq; - goto error; + goto error_subdev; } ret =devm_request_irq (isi->dev, irq, mxc_isi_pipe_irq_handler,0 ,dev_name (isi->dev), pipe);if (ret <0 ) {dev_err (isi->dev,"failed to request IRQ (%d)\n" , ret); - goto error; + goto error_subdev; }return 0 ; +error_subdev: +v4l2_subdev_cleanup (sd); error:media_entity_cleanup (&sd->entity);mutex_destroy (&pipe->lock);
Signature · 3 lines
Regards, Laurent Pinchart
PATCH v3 4/4 media: imx8-isi: clean up already-initialized pipes on probe failure
In reply to Xiaolei WangWhen mxc_isi_pipe_init() fails partway through the channel loop or when mxc_isi_v4l2_init() fails, the already initialized pipes are not cleaned up.
Fix this by calling mxc_isi_pipe_cleanup() for each already-initialized pipe in the err_xbar error path.
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +2 -0
@@ -538,6 +538,8 @@ static int mxc_isi_probe(struct platform_device *pdev)return 0 ; err_xbar: +while (i--) +mxc_isi_pipe_cleanup (&isi->pipes[i]);mxc_isi_crossbar_cleanup (&isi->crossbar);return ret;
Signature · 1 line
2.43.0