media: nxp: imx8-isi: Fix scaler rounding and downscaling limits
v3 cover media: nxp: imx8-isi: Fix scaler rounding and downscaling limits
Patch 1/2 enforces the 16x downscaling limit to prevent out-of-bounds access.
Patch 2/2 ensures the scale factor accounts for ISI's rounding-up behavior to avoid producing one extra output line.
---
Changes in v3:
- New patch 2/2 to fix scale factor rounding
- Replace CLAMP_DOWNSCALE_16 macro with inline function
- Adjust downscale threshold from 0x4000 to 0x2000
- Clarify downscaling limit in comment
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Fix possible side-effects caused by 'max_val' in CLAMP_DOWNSCALE_16 macro.
- Add space line between two paragraph in comment.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Guoniu Zhou (2):
media: nxp: imx8-isi: Fix potential out-of-bounds issues
media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h | 16 ++++++++++++++++ drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 13 +++++++++++-- drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 11 ++++++++--- drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 13 ++++++++----- 4 files changed, 43 insertions(+), 10 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260122-isi-74f87fbb9a6f
Best regards,
Signature · 1 line
Guoniu Zhou <[email protected]>
PATCH v3 1/2 media: nxp: imx8-isi: Fix potential out-of-bounds issues
From: Guoniu Zhou <[email protected]>
The maximum downscaling factor supported by ISI can be up to 16. Add minimum value constraint before applying the setting to hardware. Otherwise, the process will not respond even when Ctrl+C is executed.
---
Changes in v3:
- Replace CLAMP_DOWNSCALE_16 macro with inline function
- Adjust downscale threshold from 0x4000 to 0x2000
- Clarify downscaling limit in comment
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h | 16 ++++++++++++++++ drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 2 +- drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 11 ++++++++--- drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 13 ++++++++----- 4 files changed, 33 insertions(+), 9 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +16 -0
@@ -11,6 +11,7 @@#define __MXC_ISI_CORE_H__#include <linux/list.h> +#include <linux/math.h> #include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/types.h>
@@ -413,4 +414,19 @@ static inline void mxc_isi_debug_cleanup(struct mxc_isi_dev *isi) }#endif +/* +* ISI scaling engine works in two parts: it performs pre-decimation of +* the image followed by bilinear filtering to achieve the desired +* downscaling factor. +* +* The decimation filter provides a maximum downscaling factor of 8, and +* the subsequent bilinear filter provides a maximum downscaling factor +* of 2. Combined, the maximum scaling factor can be up to 16. +*/ +static inline unsigned int +mxc_isi_clamp_downscale_16 (unsigned int val,unsigned int max_val) +{ +return clamp (val,max (1U ,DIV_ROUND_UP (max_val,16 )), max_val); +} +#endif /* __MXC_ISI_CORE_H__ */
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +1 -1
@@ -11,7 +11,7 @@#include "imx8-isi-core.h" #include "imx8-isi-regs.h" -#define ISI_DOWNSCALE_THRESHOLD 0x4000 +#define ISI_DOWNSCALE_THRESHOLD 0x2000static inline u32 mxc_isi_read (struct mxc_isi_pipe *pipe,u32 reg) {
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c +8 -3
@@ -508,10 +508,15 @@ __mxc_isi_m2m_try_fmt_vid(struct mxc_isi_m2m_ctx *ctx,struct v4l2_pix_format_mplane *pix, const enum mxc_isi_video_type type) { + const struct v4l2_pix_format_mplane *format = + &ctx->queues.out.format; +if (type ==MXC_ISI_VIDEO_M2M_CAP ) { -/* Downscaling only */ - pix->width = min(pix->width, ctx->queues.out.format.width); - pix->height =min (pix->height, ctx->queues.out.format.height); +/* Downscaling only, by up to 16. */ + pix->width =mxc_isi_clamp_downscale_16 (pix->width, + format->width); + pix->height =mxc_isi_clamp_downscale_16 (pix->height, + format->height); }return mxc_isi_format_try (ctx->m2m->pipe, pix, type);
drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c +8 -5
@@ -641,16 +641,19 @@ static int mxc_isi_pipe_set_selection(struct v4l2_subdev *sd,/* Composing is supported on the sink only. */ return -EINVAL ; -/* The sink crop is bound by the sink format downscaling only). */ +/* +* The ISI supports downscaling only, with a factor up to 16. +* Clamp the compose rectangle size accordingly. +*/ format =mxc_isi_pipe_get_pad_format (pipe, state,MXC_ISI_PIPE_PAD_SINK ); sel->r.left =0 ; sel->r.top =0 ; - sel->r.width =clamp (sel->r.width,MXC_ISI_MIN_WIDTH , - format->width); - sel->r.height =clamp (sel->r.height,MXC_ISI_MIN_HEIGHT , - format->height); + sel->r.width =mxc_isi_clamp_downscale_16 (sel->r.width, + format->width); + sel->r.height =mxc_isi_clamp_downscale_16 (sel->r.height, + format->height); rect =mxc_isi_pipe_get_pad_compose (pipe, state,MXC_ISI_PIPE_PAD_SINK );
Signature · 1 line
2.34.1
Re: [PATCH v3 1/2] media: nxp: imx8-isi: Fix potential out-of-bounds issues
Quoted 59 lines · Guoniu Zhou
From: Guoniu Zhou <[email protected]>
The maximum downscaling factor supported by ISI can be up to 16. Add minimum value constraint before applying the setting to hardware. Otherwise, the process will not respond even when Ctrl+C is executed.
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")Reviewed-by Frank Li <[email protected]>Signed-off-by Guoniu Zhou <[email protected]>---
Changes in v3:
- ReplaceCLAMP_DOWNSCALE_16macro with inline function
- Adjust downscale threshold from0x4000to0x2000
- Clarify downscaling limit in comment--- drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h | 16 ++++++++++++++++ drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 2 +- drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 11 ++++++++--- drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 13 ++++++++----- 4 files changed, 33 insertions(+), 9 deletions(-)drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +16
-0@@ -11,6 +11,7 @@#define __MXC_ISI_CORE_H__#include <linux/list.h> +#include <linux/math.h> #include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/types.h> @@ -413,4 +414,19 @@ static inline void mxc_isi_debug_cleanup(struct mxc_isi_dev *isi) }#endif +/* +* ISI scaling engine works in two parts: it performs pre-decimation of +* the image followed by bilinear filtering to achieve the desired +* downscaling factor. +* +* The decimation filter provides a maximum downscaling factor of 8, and +* the subsequent bilinear filter provides a maximum downscaling factor +* of 2. Combined, the maximum scaling factor can be up to 16. +*/ +static inline unsigned int +mxc_isi_clamp_downscale_16 (unsigned int val,unsigned int max_val) +{ +return clamp (val,max (1U ,DIV_ROUND_UP (max_val,16 )), max_val); +} +#endif /* __MXC_ISI_CORE_H__ */ drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +0
-0
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c line 14
Quoted 5 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +1
-1@@ -11,7 +11,7 @@#include "imx8-isi-core.h" #include "imx8-isi-regs.h" -#define ISI_DOWNSCALE_THRESHOLD 0x4000 +#define ISI_DOWNSCALE_THRESHOLD 0x2000
This should be split to a separate patch as it's a separate fix.
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c __mxc_isi_m2m_try_fmt_vid(struct mxc_isi_m2m_ctx *ctx, · line 512
Quoted 12 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +0
-0static inline u32 mxc_isi_read (struct mxc_isi_pipe *pipe,u32 reg) {drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c +2
-0@@ -508,10 +508,15 @@ __mxc_isi_m2m_try_fmt_vid(struct mxc_isi_m2m_ctx *ctx,struct v4l2_pix_format_mplane *pix, const enum mxc_isi_video_type type) { + const struct v4l2_pix_format_mplane *format = + &ctx->queues.out.format;
This can go in the 'if' below.
I'll take this patch without the ISI_DOWNSCALE_THRESHOLD change in my tree, and will send a separate patch for ISI_DOWNSCALE_THRESHOLD.
Quoted 43 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c +6
-3+if (type ==MXC_ISI_VIDEO_M2M_CAP ) { -/* Downscaling only */ - pix->width =min (pix->width, ctx->queues.out.format.width); - pix->height =min (pix->height, ctx->queues.out.format.height); +/* Downscaling only, by up to 16. */ + pix->width =mxc_isi_clamp_downscale_16 (pix->width, + format->width); + pix->height =mxc_isi_clamp_downscale_16 (pix->height, + format->height); }return mxc_isi_format_try (ctx->m2m->pipe, pix, type);drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c +8
-5@@ -641,16 +641,19 @@ static int mxc_isi_pipe_set_selection(struct v4l2_subdev *sd,/* Composing is supported on the sink only. */ return -EINVAL ; -/* The sink crop is bound by the sink format downscaling only). */ +/* +* The ISI supports downscaling only, with a factor up to 16. +* Clamp the compose rectangle size accordingly. +*/ format =mxc_isi_pipe_get_pad_format (pipe, state,MXC_ISI_PIPE_PAD_SINK ); sel->r.left =0 ; sel->r.top =0 ; - sel->r.width =clamp (sel->r.width,MXC_ISI_MIN_WIDTH , - format->width); - sel->r.height =clamp (sel->r.height,MXC_ISI_MIN_HEIGHT , - format->height); + sel->r.width =mxc_isi_clamp_downscale_16 (sel->r.width, + format->width); + sel->r.height =mxc_isi_clamp_downscale_16 (sel->r.height, + format->height); rect =mxc_isi_pipe_get_pad_compose (pipe, state,MXC_ISI_PIPE_PAD_SINK );
Signature · 3 lines
Regards, Laurent Pinchart
PATCH v3 2/2 media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding
In reply to Guoniu ZhouFrom: Guoniu Zhou <[email protected]>
The ISI hardware rounds the actual output size up to an integer, as described in i.MX93 Reference Manual section 57.7.8 (Channel 0 Scale Factor). The scale factor must be calculated to ensure the theoretical output value rounds up to exactly the desired size.
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +10 -1
@@ -112,7 +112,16 @@ static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to,else *dec =8 ; -return min_t (u32, from *0x1000 / (to * *dec),ISI_DOWNSCALE_THRESHOLD ); +/* +* The ISI rounds output dimensions up to the next integer (i.MX93 RM +* section 57.7.8). Calculate the scale factor such that the theoretical +* output (input / scale_factor) rounds up to exactly the desired output. +* +* Example from the reference manual: Scaling 800 to 720 lines +* - scale = 0x11C8: 800/0x1C8 = 719.859375 -> 720 (correct) +* - scale = 0x11C7: 800/0x1C7 = 720.017578 -> 721 (one extra line) +*/ +return min_t (u32,DIV_ROUND_UP (from *0x1000 , to * *dec),ISI_DOWNSCALE_THRESHOLD ); } static void mxc_isi_channel_set_scaling(struct mxc_isi_pipe *pipe,
Signature · 1 line
2.34.1
Re: [PATCH v3 2/2] media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding
Hi Guoniu,
Thank you for the patch.
Quoted 19 lines · Guoniu Zhou
From: Guoniu Zhou <[email protected]>
The ISI hardware rounds the actual output size up to an integer, as described in i.MX93 Reference Manual section 57.7.8 (Channel 0 Scale Factor). The scale factor must be calculated to ensure the theoretical output value rounds up to exactly the desired size.
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")Signed-off-by Guoniu Zhou <[email protected]>--- drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +0
-0
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to, · line 118
Quoted 8 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +4
-1@@ -112,7 +112,16 @@ static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to,else *dec =8 ; -return min_t (u32, from *0x1000 / (to * *dec),ISI_DOWNSCALE_THRESHOLD ); +/* +* The ISI rounds output dimensions up to the next integer (i.MX93 RM +* section 57.7.8). Calculate the scale factor such that the theoretical +* output (input / scale_factor) rounds up to exactly the desired output.
I assume you have verified this applies to the ISI in the i.MX8 family.
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to, · line 122
Quoted 4 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +4
-0+* +* Example from the reference manual: Scaling 800 to 720 lines +* - scale = 0x11C8: 800/0x1C8 = 719.859375 -> 720 (correct) +* - scale = 0x11C7: 800/0x1C7 = 720.017578 -> 721 (one extra line)
I think the first paragraph is clear enough, the example is not required.
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to, · line 124
Quoted 2 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +2
-0+*/ +return min_t (u32,DIV_ROUND_UP (from *0x1000 , to * *dec),ISI_DOWNSCALE_THRESHOLD );
Line wrap please.
return min_t(u32, DIV_ROUND_UP(from * 0x1000, to * *dec), ISI_DOWNSCALE_THRESHOLD);
No need to send a new version, I'll handle those changes.
Quoted 3 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c +0
-0} static void mxc_isi_channel_set_scaling(struct mxc_isi_pipe *pipe,
Signature · 3 lines
Regards, Laurent Pinchart