Skip to contentSkip to conversation

media: venus: venc: avoid modulo by zero when computing B-frames

PATCH Superseded Shirong Zhao 2 participants 3 messages Dec 5

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 8fc58186bb41 ("media: venus: venc: set correctly GOP size and number of B-frames")
---
 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
index 4d36c44..49f3d62 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Shirong Zhao

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.

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
---
  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
index 4d36c44..49f3d62 100644

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.

#2 lore raw

PATCH v2 1/1 media: venus: venc: avoid modulo by zero when computing B-frames

In reply to Shirong Zhao

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.

Fixes 8fc58186bb41 ("media: venus: venc: set correctly GOP size and number of B-frames")
---
 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
index 4d36c44..14ab805 100644
@@ -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