media: venus: venc: avoid modulo by zero when computing B-frames
Subsystems (5)
PATCH 1/1 media: venus: venc: avoid modulo by zero when computing B-frames
venc_calc_bpframes() computes half = (gop_size - 1) >> 1, then starts b = p = half and runs
for (; b <= gop_size - 1; b++, p--) {
if (b % p)
...p decreases by one per iteration while b increases. For small GOP sizes p reaches 0 before the loop bound is hit, so b % p becomes b % 0 and the kernel raises a divide error. This happens for gop_size 1..6 (e.g. gop 3 with conseq_b >= 2: b=1,p=1 passes, then b=2,p=0 -> 2 % 0). Both V4L2_CID_MPEG_VIDEO_GOP_SIZE (0..65535) and V4L2_CID_MPEG_VIDEO_B_FRAMES (0..4) are user controls, so userspace can trigger it through the V4L2 control API.
Stop the loop once p reaches 0; no valid B/P split can have p == 0, and the existing !found path then rejects the configuration.
--- drivers/media/platform/qcom/venus/venc_ctrls.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/venus/venc_ctrls.c +7 -1
@@ -40,7 +40,13 @@ static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf) b = p = half; -for (; b <= gop_size -1 ; b++, p--) { +/* +* p starts at half and decreases by one each iteration. For small GOP +* sizes it reaches 0 while b <= gop_size - 1, and the modulo below would +* then be b % 0. Stop the loop before that; a layout that needs p == 0 +* cannot be satisfied anyway and is rejected below. +*/ +for (; b <= gop_size -1 && p; b++, p--) {if (b % p)continue ;
Signature · 1 line
2.53.0
Quoted 15 lines · Shirong Zhao
venc_calc_bpframes()computes half = (gop_size- 1) >> 1, then starts b = p = half and runsfor (; b <= gop_size - 1; b++, p--) { if (b % p) ...p decreases by one per iteration while b increases. For small GOP sizes p reaches 0 before the loop bound is hit, so b % p becomes b % 0 and the kernel raises a divide error. This happens for
gop_size1..6 (e.g. gop 3 withconseq_b>= 2: b=1,p=1 passes, then b=2,p=0 -> 2 % 0). BothV4L2_CID_MPEG_VIDEO_GOP_SIZE(0..65535) andV4L2_CID_MPEG_VIDEO_B_FRAMES(0..4) are user controls, so userspace can trigger it through the V4L2 control API.
Quoted 4 lines
Stop the loop once p reaches 0; no valid B/P split can have p == 0, and the existing !found path then rejects the configuration.
Fixes 8fc58186bb41 ("media: venus: venc: set correctly GOP size and number of B-frames")
Quoted 10 lines
Signed-off-by Shirong Zhao <[email protected]>--- drivers/media/platform/qcom/venus/venc_ctrls.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/media/platform/qcom/venus/venc_ctrls.c +0
-0
drivers/media/platform/qcom/venus/venc_ctrls.c static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf) · line 48
Quoted 10 lines
drivers/media/platform/qcom/venus/venc_ctrls.c +6
-1@@ -40,7 +40,13 @@ static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf) b = p = half; -for (; b <= gop_size -1 ; b++, p--) { +/* +* p starts at half and decreases by one each iteration. For small GOP +* sizes it reaches 0 while b <= gop_size - 1, and the modulo below would +* then be b % 0. Stop the loop before that; a layout that needs p == 0 +* cannot be satisfied anyway and is rejected below. +*/
The commit log is enough.
Quoted 6 lines
drivers/media/platform/qcom/venus/venc_ctrls.c +1
-1+for (; b <= gop_size -1 && p; b++, p--) {if (b % p)continue ; --2.53.0
Once fixed.
PATCH v2 1/1 media: venus: venc: avoid modulo by zero when computing B-frames
In reply to Shirong Zhaovenc_calc_bpframes() computes half = (gop_size - 1) >> 1, then starts b = p = half and runs
for (; b <= gop_size - 1; b++, p--) {
if (b % p)
...p decreases by one per iteration while b increases. For small GOP sizes p reaches 0 before the loop bound is hit, so b % p becomes b % 0 and the kernel raises a divide error. This happens for gop_size 1..6 (e.g. gop 3 with conseq_b >= 2: b=1,p=1 passes, then b=2,p=0 -> 2 % 0). Both V4L2_CID_MPEG_VIDEO_GOP_SIZE (0..65535) and V4L2_CID_MPEG_VIDEO_B_FRAMES (0..4) are user controls, so userspace can trigger it through the V4L2 control API.
Stop the loop once p reaches 0; no valid B/P split can have p == 0, and the existing !found path then rejects the configuration.
--- drivers/media/platform/qcom/venus/venc_ctrls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/venc_ctrls.c +1 -1
@@ -40,7 +40,7 @@ static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf) b = p = half; -for (; b <= gop_size -1 ; b++, p--) { +for (; b <= gop_size -1 && p; b++, p--) {if (b % p)continue ;
Signature · 1 line
2.53.0