Skip to contentSkip to conversation

media: atomisp: harden and clean up isp configuration

PATCH Superseded Jose A. Perez de Azpillaga 3 participants 14 messages Mar 28 – Mar 31

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover media: atomisp: harden and clean up isp configuration

This series addresses stability issues and technical debt within the ISP configuration path of the AtomISP driver.

Jose A. Perez de Azpillaga (2):
media: atomisp: fix potential NULL pointer dereference in
configure_isp_from_args()
media: atomisp: remove redundant call to ia_css_output0_configure()

 drivers/staging/media/atomisp/pci/sh_css_sp.c | 48 +++++++++++--------
 1 file changed, 28 insertions(+), 20 deletions(-)

--
2.53.0

#1 lore raw

PATCH 1/2 media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

Add defensive checks for both delay_frames and tnr_frames before passing them to their respective configuration functions. This ensures that optional frames are only processed if they were actually allocated, preventing a kernel NULL pointer dereference.

Fixes a49d25364dfb ("staging/atomisp: Add support for the Intel IPU v2")
---
 drivers/staging/media/atomisp/pci/sh_css_sp.c | 44 ++++++++++++-------
 1 file changed, 27 insertions(+), 17 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_sp.c +27 -17
index 6da151e7a873..2904455b35f7 100644
@@ -775,9 +775,17 @@ static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline,
 	ret = ia_css_fpn_configure(binary,  &binary->in_frame_info);
 	if (ret)
 		return ret;
-	ret = ia_css_crop_configure(binary, ia_css_frame_get_info(args->delay_frames[0]));
-	if (ret)
-		return ret;
+
+	/*
+	 * Only configure crop if delay_frames are present. Accessing
+	 * delay_frames[0] without this check would result in a NULL deference.
+	 */
+	if (args->delay_frames[0]) {
+		ret = ia_css_crop_configure(binary, ia_css_frame_get_info(args->delay_frames[0]));
+		if (ret)
+			return ret;
+	}
+
 	ret = ia_css_qplane_configure(pipeline, binary, &binary->in_frame_info);
 	if (ret)
 		return ret;
@@ -808,21 +816,23 @@ static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline,
 		return ret;
 
 	/*
-	 * FIXME: args->delay_frames can be NULL here
-	 *
-	 * Somehow, the driver at the Intel Atom Yocto tree doesn't seem to
-	 * suffer from the same issue.
-	 *
-	 * Anyway, the function below should now handle a NULL delay_frames
-	 * without crashing, but the pipeline should likely be built without
-	 * adding it at the first place (or there are a hidden bug somewhere)
+	 * Safely handle pipelines built without delay_frames
 	 */
-	ret = ia_css_ref_configure(binary, args->delay_frames, pipeline->dvs_frame_delay);
-	if (ret)
-		return ret;
-	ret = ia_css_tnr_configure(binary, args->tnr_frames);
-	if (ret)
-		return ret;
+	if (args->delay_frames[0]) {
+		ret = ia_css_ref_configure(binary, args->delay_frames, pipeline->dvs_frame_delay);
+		if (ret)
+			return ret;
+	}
+
+	/*
+	 * Safely handle TNR frames as well
+	 */
+	if (args->tnr_frames[0]) {
+		ret = ia_css_tnr_configure(binary, args->tnr_frames);
+		if (ret)
+			return ret;
+	}
+
 	return ia_css_bayer_io_config(binary, args);
 }
 
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

Quoted 11 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

Add defensive checks for both delay_frames and tnr_frames before passing them to their respective configuration functions. This ensures that optional frames are only processed if they were actually allocated, preventing a kernel NULL pointer dereference.

Have you experienced bugs IRL?

...

drivers/staging/media/atomisp/pci/sh_css_sp.c static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline, · line 820

Quoted 11 lines
        /*
drivers/staging/media/atomisp/pci/sh_css_sp.c +1 -8
-        * FIXME: args->delay_frames can be NULL here
-        *
-        * Somehow, the driver at the Intel Atom Yocto tree doesn't seem to
-        * suffer from the same issue.
-        *
-        * Anyway, the function below should now handle a NULL delay_frames
-        * without crashing, but the pipeline should likely be built without
-        * adding it at the first place (or there are a hidden bug somewhere)
+        * Safely handle pipelines built without delay_frames
         */

This comment suggests something different. What the proposed change is doing is just skipping the invalid data without actual understanding of the root cause.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

Quoted 7 lines
Quoted 4 lines

Add defensive checks for both delay_frames and tnr_frames before passing them to their respective configuration functions. This ensures that optional frames are only processed if they were actually allocated, preventing a kernel NULL pointer dereference.

Have you experienced bugs IRL?

not really, I don't have the hardware, but while reading the code, I found it to be logically inconsistent. imo, the comment is misplaced since delay_frames can be null earlier.

Quoted 14 lines

...

drivers/staging/media/atomisp/pci/sh_css_sp.c static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline, · line 820

Quoted 11 lines
        /*
drivers/staging/media/atomisp/pci/sh_css_sp.c +1 -8
-        * FIXME: args->delay_frames can be NULL here
-        *
-        * Somehow, the driver at the Intel Atom Yocto tree doesn't seem to
-        * suffer from the same issue.
-        *
-        * Anyway, the function below should now handle a NULL delay_frames
-        * without crashing, but the pipeline should likely be built without
-        * adding it at the first place (or there are a hidden bug somewhere)
+        * Safely handle pipelines built without delay_frames
         */
Quoted 4 lines

This comment suggests something different. What the proposed change is doing is just skipping the invalid data without actual understanding of the root cause.

you are right here. I should've done a deeper analysis instead of focusing only on that function. looking more closely at how the pipeline is built, I found that these frames are intentionally skipped during allocation to save memory when a specific feature isn't enabled.

the configuration path was just ignoring those enable flags and trying to use the frames anyway. instead of a NULL check, maybe I should try gating these calls behind the actual feature flags in the binary info.

if this is a better approach, I'll send a v2 with these changes. :)

...

regards,
jose a. p-a

#4 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

Quoted 15 lines · Jose A. Perez de Azpillaga
Quoted 14 lines · Andy Shevchenko
Quoted 11 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

Add defensive checks for both delay_frames and tnr_frames before passing them to their respective configuration functions. This ensures that optional frames are only processed if they were actually allocated, preventing a kernel NULL pointer dereference.

Quoted 5 lines
Quoted 1 line

Have you experienced bugs IRL?

not really, I don't have the hardware, but while reading the code, I found it to be logically inconsistent. imo, the comment is misplaced since delay_frames can be null earlier.

Don't forget to mention this in the cover letter.

...

Quoted 21 lines
Quoted 12 lines

drivers/staging/media/atomisp/pci/sh_css_sp.c static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline, · line 820

Quoted 11 lines
        /*
drivers/staging/media/atomisp/pci/sh_css_sp.c +1 -8
-        * FIXME: args->delay_frames can be NULL here
-        *
-        * Somehow, the driver at the Intel Atom Yocto tree doesn't seem to
-        * suffer from the same issue.
-        *
-        * Anyway, the function below should now handle a NULL delay_frames
-        * without crashing, but the pipeline should likely be built without
-        * adding it at the first place (or there are a hidden bug somewhere)
+        * Safely handle pipelines built without delay_frames
         */
Quoted 3 lines

This comment suggests something different. What the proposed change is doing is just skipping the invalid data without actual understanding of the root cause.

you are right here. I should've done a deeper analysis instead of focusing only on that function. looking more closely at how the pipeline is built, I found that these frames are intentionally skipped during allocation to save memory when a specific feature isn't enabled.

Quoted 5 lines

the configuration path was just ignoring those enable flags and trying to use the frames anyway. instead of a NULL check, maybe I should try gating these calls behind the actual feature flags in the binary info.

if this is a better approach, I'll send a v2 with these changes. :)

Sounds like a plan! We would appreciate this kind of patch over some mechanical cleanups that flooded recently to this driver.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

In reply to Jose A. Perez de Azpillaga
Quoted 5 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

The comment comes later in the function and it says "FIXME:
args->delay_frames can be NULL here". "args->delay_frames" is different from "args->delay_frames[0]". Obviously args->delay_frames can't actually be NULL there since we dereference it here so the comment is wrong.

If the correct response to the FIXME were just to add a NULL check then the original author probably would have done that.

regards,
dan carpenter

#6 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

Quoted 7 lines · Dan Carpenter
Quoted 5 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

Quoted 9 lines

The comment comes later in the function and it says "FIXME:
args->delay_frames can be NULL here". "args->delay_frames" is different from "args->delay_frames[0]". Obviously args->delay_frames can't actually be NULL there since we dereference it here so the comment is wrong.

If the correct response to the FIXME were just to add a NULL check then the original author probably would have done that.

yes, I misunderstood the comment. my bad. I read more carefully.

...

regards,
jose a. p-a

#7 lore raw

Re: [PATCH v1 1/2] media: atomisp: fix potential NULL pointer dereference in configure_isp_from_args()

Quoted 14 lines · Jose A. Perez de Azpillaga
Quoted 13 lines · Dan Carpenter
Quoted 5 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() incorrectly dereferences args->delay_frames[0] to configure cropping without checking if the pointer is valid. However, as noted in a FIXME comment later in the same function, delay_frames can be NULL in certain pipeline configurations.

The comment comes later in the function and it says "FIXME:
args->delay_frames can be NULL here". "args->delay_frames" is different from "args->delay_frames[0]". Obviously args->delay_frames can't actually be NULL there since we dereference it here so the comment is wrong.

Quoted 6 lines
Quoted 3 lines

If the correct response to the FIXME were just to add a NULL check then the original author probably would have done that.

yes, I misunderstood the comment. my bad. I read more carefully.

edit: I'll read more carefully.

Quoted 4 lines

...

regards,
jose a. p-a

#8 lore raw

PATCH 2/2 media: atomisp: remove redundant call to ia_css_output0_configure()

In reply to Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

---
 drivers/staging/media/atomisp/pci/sh_css_sp.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_sp.c +0 -3
index 2904455b35f7..1612bb2fd8b0 100644
@@ -796,9 +796,6 @@ static int configure_isp_from_args(const struct sh_css_sp_pipeline *pipeline,
 	if (ret)
 		return ret;
 	ret = ia_css_copy_output_configure(binary, args->copy_output);
-	if (ret)
-		return ret;
-	ret = ia_css_output0_configure(binary, ia_css_frame_get_info(args->out_frame[0]));
 	if (ret)
 		return ret;
 	ret = ia_css_iterator_configure(binary, ia_css_frame_get_info(args->in_frame));
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v1 2/2] media: atomisp: remove redundant call to ia_css_output0_configure()

Quoted 4 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

This requires more information, in particular to explain if the order has no side effects. It might be that double configuration has side effects and removal (wrong) one may lead to other currently hidden issues.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v1 2/2] media: atomisp: remove redundant call to ia_css_output0_configure()

Quoted 12 lines · Andy Shevchenko
Quoted 4 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

This requires more information, in particular to explain if the order has no side effects. It might be that double configuration has side effects and removal (wrong) one may lead to other currently hidden issues.

mhm... my bad for that,

the ia_css_output0_configure() function acts as a configuration setter. it populates a struct ia_css_output0_configuration from the frame info and caches it in the binary parameters. calling it twice with the same out_frame[0] pointer merely overwrites the exact same state with identical values. it has no cumulative state, either does its order matter relative to ia_css_copy_output_configure().

...

regards,
jose a. p-a

#11 lore raw

Re: [PATCH v1 2/2] media: atomisp: remove redundant call to ia_css_output0_configure()

Quoted 13 lines · Jose A. Perez de Azpillaga
Quoted 11 lines · Andy Shevchenko
Quoted 4 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

This requires more information, in particular to explain if the order has no side effects. It might be that double configuration has side effects and removal (wrong) one may lead to other currently hidden issues.

Quoted 8 lines

mhm... my bad for that,

the ia_css_output0_configure() function acts as a configuration setter. it populates a struct ia_css_output0_configuration from the frame info and caches it in the binary parameters. calling it twice with the same out_frame[0] pointer merely overwrites the exact same state with identical values. it has no cumulative state, either does its order matter relative to ia_css_copy_output_configure().

So, no call in between accesses that parameter or relies on its state? With this analysis in the commit message the change looks good. Please, amend it in v2.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH v1 2/2] media: atomisp: remove redundant call to ia_css_output0_configure()

In reply to Jose A. Perez de Azpillaga
Quoted 6 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

---

This feels like a guess work patch. Sure, it looks like duplicate code but duplicate code isn't always wrong. How do you know which call to remove? How do you know that it's not a copy and paste error and the right fix is just to change the code instead of deleting it?

Patch 1 felt like an AI patch, and this patch feels even more strongly like an AI patch.

Please don't send guess work patches or if you do add a giant comment at the bottom saying --- "This patch is a GUESS. Review carefully! Untested"

regards,
dan carpenter

#13 lore raw

Re: [PATCH v1 2/2] media: atomisp: remove redundant call to ia_css_output0_configure()

Quoted 5 lines · Dan Carpenter
Quoted 4 lines · Jose A. Perez de Azpillaga

The function configure_isp_from_args() contained a duplicate call to ia_css_output0_configure() using the same output frame index. Remove the redundant call to simplify the configuration path.

Quoted 8 lines
Quoted 2 lines

---

This feels like a guess work patch. Sure, it looks like duplicate code but duplicate code isn't always wrong. How do you know which call to remove? How do you know that it's not a copy and paste error and the right fix is just to change the code instead of deleting it?

my response here would be something similar to Andy's one.

Quoted 3 lines

Patch 1 felt like an AI patch, and this patch feels even more strongly like an AI patch.

ouch.

Quoted 4 lines

Please don't send guess work patches or if you do add a giant comment at the bottom saying --- "This patch is a GUESS. Review carefully! Untested"

okay, I wasn't aware of that. I'll try to add that comment or better yet, not trying to fix things without the actual hardware. thanks.

...

regards,
jose a. p-a