Skip to contentSkip to conversation

media: rkvdec: reduce excessive stack usage in assemble_hw_pps()

PATCH RESEND Superseded Arnd Bergmann 3 participants 5 messages Mar 5

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

PATCH RESEND 1/2 media: rkvdec: reduce excessive stack usage in assemble_hw_pps()

From: Arnd Bergmann <[email protected]>

The rkvdec_pps had a large set of bitfields, all of which as misaligned. This causes clang-21 and likely other versions to produce absolutely awful object code and a warning about very large stack usage, on targets without unaligned access:

drivers/media/platform/rockchip/rkvdec/rkvdec-vp9.c:966:12: error: stack frame size (1472) exceeds limit (1280) in 'rkvdec_vp9_start' [-Werror,-Wframe-larger-than]

Part of the problem here is how all the bitfield accesses are inlined into a function that already has large structures on the stack.

Mark set_field_order_cnt() as noinline_for_stack, and split out the following accesses in assemble_hw_pps() into another noinline function, both of which now using around 800 bytes of stack in the same configuration.

There is clearly still something wrong with clang here, but splitting it into multiple functions reduces the risk of stack overflow.

Fixes fde24907570d ("media: rkvdec: Add H264 support for the VDPU383 variant")

---
Resending this along with the other patch mainly to point out that this one is still pending as well.

---
 .../rockchip/rkvdec/rkvdec-vdpu383-h264.c     | 50 ++++++++++---------
 1 file changed, 27 insertions(+), 23 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +27 -23
index 97f1efde2e47..fb4f849d7366 100644
@@ -130,7 +130,7 @@ struct rkvdec_h264_ctx {
 	struct vdpu383_regs_h26x regs;
 };
 
-static void set_field_order_cnt(struct rkvdec_pps *pps, const struct v4l2_h264_dpb_entry *dpb)
+static noinline_for_stack void set_field_order_cnt(struct rkvdec_pps *pps, const struct v4l2_h264_dpb_entry *dpb)
 {
 	pps->top_field_order_cnt0 = dpb[0].top_field_order_cnt;
 	pps->bot_field_order_cnt0 = dpb[0].bottom_field_order_cnt;
@@ -166,6 +166,31 @@ static void set_field_order_cnt(struct rkvdec_pps *pps, const struct v4l2_h264_d
 	pps->bot_field_order_cnt15 = dpb[15].bottom_field_order_cnt;
 }
 
+static noinline_for_stack void set_dec_params(struct rkvdec_pps *pps, const struct v4l2_ctrl_h264_decode_params *dec_params)
+{
+	const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
+
+	for (int i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
+		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
+			pps->is_longterm |= (1 << i);
+		pps->ref_field_flags |=
+		 (!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD)) << i;
+		pps->ref_colmv_use_flag |=
+		 (!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) << i;
+		pps->ref_topfield_used |=
+		 (!!(dpb[i].fields & V4L2_H264_TOP_FIELD_REF)) << i;
+		pps->ref_botfield_used |=
+			(!!(dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)) << i;
+	}
+	pps->pic_field_flag =
+		!!(dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC);
+	pps->pic_associated_flag =
+		!!(dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD);
+
+	pps->cur_top_field = dec_params->top_field_order_cnt;
+	pps->cur_bot_field = dec_params->bottom_field_order_cnt;
+}
+
 static void assemble_hw_pps(struct rkvdec_ctx *ctx,
 			    struct rkvdec_h264_run *run)
 {
@@ -177,7 +202,6 @@ static void assemble_hw_pps(struct rkvdec_ctx *ctx,
 	struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
 	struct rkvdec_sps_pps *hw_ps;
 	u32 pic_width, pic_height;
-	u32 i;
 
 	/*
 	 * HW read the SPS/PPS information from PPS packet index by PPS id.
@@ -261,28 +285,8 @@ static void assemble_hw_pps(struct rkvdec_ctx *ctx,
 		!!(pps->flags & V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT);
 
 	set_field_order_cnt(&hw_ps->pps, dpb);
+	set_dec_params(&hw_ps->pps, dec_params);
 
-	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
-		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
-			hw_ps->pps.is_longterm |= (1 << i);
-
-		hw_ps->pps.ref_field_flags |=
-			(!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD)) << i;
-		hw_ps->pps.ref_colmv_use_flag |=
-			(!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) << i;
-		hw_ps->pps.ref_topfield_used |=
-			(!!(dpb[i].fields & V4L2_H264_TOP_FIELD_REF)) << i;
-		hw_ps->pps.ref_botfield_used |=
-			(!!(dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)) << i;
-	}
-
-	hw_ps->pps.pic_field_flag =
-		!!(dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC);
-	hw_ps->pps.pic_associated_flag =
-		!!(dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD);
-
-	hw_ps->pps.cur_top_field = dec_params->top_field_order_cnt;
-	hw_ps->pps.cur_bot_field = dec_params->bottom_field_order_cnt;
 }
 
 static void rkvdec_write_regs(struct rkvdec_ctx *ctx)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 2/2 media: rkvdec: reduce stack usage in rkvdec_init_v4l2_vp9_count_tbl()

From: Arnd Bergmann <[email protected]>

The deeply nested loop in rkvdec_init_v4l2_vp9_count_tbl() needs a lot of registers, so when the clang register allocator runs out, it ends up spilling countless temporaries to the stack:

drivers/media/platform/rockchip/rkvdec/rkvdec-vp9.c:966:12: error: stack frame size (1472) exceeds limit (1280) in 'rkvdec_vp9_start' [-Werror,-Wframe-larger-than]

Split out the innermost loop into a separate function that is marked noinline_for_stack. I tried out all combinations of having some of the inner loops inside of the separate function, but this was the only veriant that creates reasonable code with clang-22 on arm64.

--
v2: rework after sering more of the same warning with v1 applied.

My earlier version was much simpler but still exceeded 1280 bytes of stack space in some configurations for unnecessary variable spills.

---
 .../platform/rockchip/rkvdec/rkvdec-vp9.c     | 48 ++++++++++---------
 1 file changed, 26 insertions(+), 22 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec-vp9.c +26 -22
index e4cdd2122873..ecb2819bd566 100644
@@ -893,12 +893,36 @@ static void rkvdec_vp9_done(struct rkvdec_ctx *ctx,
 	update_ctx_last_info(vp9_ctx);
 }
 
+/* noinline to ensure clang's register allocator doesn't run out of registers */
+static noinline void
+rkvdec_init_v4l2_vp9_count_tbl_loop(struct rkvdec_vp9_ctx *vp9_ctx, int i, int j, int k, int l)
+{
+	struct rkvdec_vp9_intra_frame_symbol_counts *intra_cnts = vp9_ctx->count_tbl.cpu;
+	struct rkvdec_vp9_inter_frame_symbol_counts *inter_cnts = vp9_ctx->count_tbl.cpu;
+
+	for (int m = 0; m < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff[0][0][0][0]); ++m) {
+		vp9_ctx->inter_cnts.coeff[i][j][k][l][m] =
+			&inter_cnts->ref_cnt[k][i][j][l][m].coeff;
+		vp9_ctx->inter_cnts.eob[i][j][k][l][m][0] =
+			&inter_cnts->ref_cnt[k][i][j][l][m].eob[0];
+		vp9_ctx->inter_cnts.eob[i][j][k][l][m][1] =
+			&inter_cnts->ref_cnt[k][i][j][l][m].eob[1];
+										\
+		vp9_ctx->intra_cnts.coeff[i][j][k][l][m] =
+			&intra_cnts->ref_cnt[k][i][j][l][m].coeff;
+		vp9_ctx->intra_cnts.eob[i][j][k][l][m][0] =
+			&intra_cnts->ref_cnt[k][i][j][l][m].eob[0];
+		vp9_ctx->intra_cnts.eob[i][j][k][l][m][1] =
+			&intra_cnts->ref_cnt[k][i][j][l][m].eob[1];
+	}
+}
+
 static void rkvdec_init_v4l2_vp9_count_tbl(struct rkvdec_ctx *ctx)
 {
 	struct rkvdec_vp9_ctx *vp9_ctx = ctx->priv;
 	struct rkvdec_vp9_intra_frame_symbol_counts *intra_cnts = vp9_ctx->count_tbl.cpu;
 	struct rkvdec_vp9_inter_frame_symbol_counts *inter_cnts = vp9_ctx->count_tbl.cpu;
-	int i, j, k, l, m;
+	int i, j, k, l;
 
 	vp9_ctx->inter_cnts.partition = &inter_cnts->partition;
 	vp9_ctx->inter_cnts.skip = &inter_cnts->skip;
@@ -936,31 +960,11 @@ static void rkvdec_init_v4l2_vp9_count_tbl(struct rkvdec_ctx *ctx)
 	vp9_ctx->inter_cnts.class0_hp = &inter_cnts->class0_hp;
 	vp9_ctx->inter_cnts.hp = &inter_cnts->hp;
 
-#define INNERMOST_LOOP \
-	do {										\
-		for (m = 0; m < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff[0][0][0][0]); ++m) {\
-			vp9_ctx->inter_cnts.coeff[i][j][k][l][m] =			\
-				&inter_cnts->ref_cnt[k][i][j][l][m].coeff;		\
-			vp9_ctx->inter_cnts.eob[i][j][k][l][m][0] =			\
-				&inter_cnts->ref_cnt[k][i][j][l][m].eob[0];		\
-			vp9_ctx->inter_cnts.eob[i][j][k][l][m][1] =			\
-				&inter_cnts->ref_cnt[k][i][j][l][m].eob[1];		\
-											\
-			vp9_ctx->intra_cnts.coeff[i][j][k][l][m] =			\
-				&intra_cnts->ref_cnt[k][i][j][l][m].coeff;		\
-			vp9_ctx->intra_cnts.eob[i][j][k][l][m][0] =			\
-				&intra_cnts->ref_cnt[k][i][j][l][m].eob[0];		\
-			vp9_ctx->intra_cnts.eob[i][j][k][l][m][1] =			\
-				&intra_cnts->ref_cnt[k][i][j][l][m].eob[1];		\
-		}									\
-	} while (0)
-
 	for (i = 0; i < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff); ++i)
 		for (j = 0; j < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff[0]); ++j)
 			for (k = 0; k < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff[0][0]); ++k)
 				for (l = 0; l < ARRAY_SIZE(vp9_ctx->inter_cnts.coeff[0][0][0]); ++l)
-					INNERMOST_LOOP;
-#undef INNERMOST_LOOP
+					rkvdec_init_v4l2_vp9_count_tbl_loop(vp9_ctx, i, j, k, l);
 }
 
 static int rkvdec_vp9_start(struct rkvdec_ctx *ctx)
Signature · 1 line
2.39.5
#2 lore raw
In reply to Arnd Bergmann

Hi,

Le jeudi 05 mars 2026 à 16:26 +0100, Arnd Bergmann a écrit :

Quoted 6 lines

From: Arnd Bergmann <[email protected]>

The rkvdec_pps had a large set of bitfields, all of which as misaligned. This causes clang-21 and likely other versions to produce absolutely awful object code and a warning about very large stack usage, on targets without unaligned access:

I'm a bit surprised you felt the need for resend. Perhaps you can help us understand what made you think your patch wasn't being processed ?

My PR:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

And Hans/Mauro did logistic on the #linux-maint IRC channel this morning. I believe I've marked all the relevant patches on patchwork our of "New" state and you have my Rb. What else would help you ?

Nicolas

Quoted 126 lines

drivers/media/platform/rockchip/rkvdec/rkvdec-vp9.c:966:12: error: stack frame size (1472) exceeds limit (1280) in 'rkvdec_vp9_start' [-Werror,-Wframe- larger-than]

Part of the problem here is how all the bitfield accesses are inlined into a function that already has large structures on the stack.

Mark set_field_order_cnt() as noinline_for_stack, and split out the following accesses in assemble_hw_pps() into another noinline function, both of which now using around 800 bytes of stack in the same configuration.

There is clearly still something wrong with clang here, but splitting it into multiple functions reduces the risk of stack overflow.

Fixes fde24907570d ("media: rkvdec: Add H264 support for the VDPU383

variant")

---
Resending this along with the other patch mainly to point out that this one is still pending as well.
---
 .../rockchip/rkvdec/rkvdec-vdpu383-h264.c     | 50 ++++++++++---------

 1 file changed, 27 insertions(+), 23 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -0
index 97f1efde2e47..fb4f849d7366 100644
@@ -130,7 +130,7 @@ struct rkvdec_h264_ctx {
  	struct vdpu383_regs_h26x regs;
  };


-static void set_field_order_cnt(struct rkvdec_pps *pps, const struct v4l2_h264_dpb_entry *dpb)
+static noinline_for_stack void set_field_order_cnt(struct rkvdec_pps *pps, const struct v4l2_h264_dpb_entry *dpb)
 {
  pps->top_field_order_cnt0 = dpb[0].top_field_order_cnt; pps->bot_field_order_cnt0 = dpb[0].bottom_field_order_cnt;

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -0
@@ -166,6 +166,31 @@ static void set_field_order_cnt(struct rkvdec_pps *pps,

const struct v4l2_h264_d
  pps->bot_field_order_cnt15 = dpb[15].bottom_field_order_cnt; }

+static noinline_for_stack void set_dec_params(struct rkvdec_pps *pps, const struct v4l2_ctrl_h264_decode_params *dec_params)

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +14 -0
+{
+	const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
+
+	for (int i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
+		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
+			pps->is_longterm |= (1 << i);
+		pps->ref_field_flags |=
+		 (!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD)) << i;
+		pps->ref_colmv_use_flag |=
+		 (!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) << i;
+		pps->ref_topfield_used |=
+		 (!!(dpb[i].fields & V4L2_H264_TOP_FIELD_REF)) << i;
+		pps->ref_botfield_used |=
+			(!!(dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)) <<

i;

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +3 -0
+	}
+	pps->pic_field_flag =
+		!!(dec_params->flags &

V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +2 -0
+	pps->pic_associated_flag =
+		!!(dec_params->flags &

V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +5 -0
+
+	pps->cur_top_field = dec_params->top_field_order_cnt;
+	pps->cur_bot_field = dec_params->bottom_field_order_cnt;
+}
+

 static void assemble_hw_pps(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run) {

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -1
@@ -177,7 +202,6 @@ static void assemble_hw_pps(struct rkvdec_ctx *ctx,
  	struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
  	struct rkvdec_sps_pps *hw_ps;
  	u32 pic_width, pic_height;
-	u32 i;


  /*
  * HW read the SPS/PPS information from PPS packet index by PPS id.

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -0
@@ -261,28 +285,8 @@ static void assemble_hw_pps(struct rkvdec_ctx *ctx,
  		!!(pps->flags & V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT);


  set_field_order_cnt(&hw_ps->pps, dpb);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +1 -0
+	set_dec_params(&hw_ps->pps, dec_params);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -6
-	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
-		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
-			hw_ps->pps.is_longterm |= (1 << i);
-
-		hw_ps->pps.ref_field_flags |=
-			(!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD))

<< i;

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -2
-		hw_ps->pps.ref_colmv_use_flag |=
-			(!!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))

<< i;

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -4
-		hw_ps->pps.ref_topfield_used |=
-			(!!(dpb[i].fields & V4L2_H264_TOP_FIELD_REF)) << i;
-		hw_ps->pps.ref_botfield_used |=
-			(!!(dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)) <<

i;

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -4
-	}
-
-	hw_ps->pps.pic_field_flag =
-		!!(dec_params->flags &

V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -2
-	hw_ps->pps.pic_associated_flag =
-		!!(dec_params->flags &

V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD);

drivers/media/platform/rockchip/rkvdec/rkvdec-vdpu383-h264.c +0 -3
-
-	hw_ps->pps.cur_top_field = dec_params->top_field_order_cnt;
-	hw_ps->pps.cur_bot_field = dec_params->bottom_field_order_cnt;

 }

 static void rkvdec_write_regs(struct rkvdec_ctx *ctx)

#3 lore raw
Quoted 11 lines · Nicolas Dufresne

Le jeudi 05 mars 2026 à 16:26 +0100, Arnd Bergmann a écrit :

Quoted 6 lines

From: Arnd Bergmann <[email protected]>

The rkvdec_pps had a large set of bitfields, all of which as misaligned. This causes clang-21 and likely other versions to produce absolutely awful object code and a warning about very large stack usage, on targets without unaligned access:

I'm a bit surprised you felt the need for resend. Perhaps you can help us understand what made you think your patch wasn't being processed ?

I updated the second patch today after I found a corner case that wasn't addressed by the first version. As I had sent both as a series a month ago, and neither was in linux-next yet, it seemed more helpful to send an updated series rather than replace only one of the two.

Quoted 6 lines

My PR:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

And Hans/Mauro did logistic on the #linux-maint IRC channel this morning. I believe I've marked all the relevant patches on patchwork our of "New" state and you have my Rb. What else would help you ?

That's fine then, I did not mean to seem impatient. I assume the original patches will be in linux-next then, and [v2 2/2] will conflict. I'll let you review that one first, but can send a rebased version if you think we should merge it on top.

Arnd

#4 lore raw

Hi,

Le jeudi 05 mars 2026 à 18:10 +0100, Arnd Bergmann a écrit :

Quoted 10 lines · Nicolas Dufresne
Quoted 9 lines

Le jeudi 05 mars 2026 à 16:26 +0100, Arnd Bergmann a écrit :

Quoted 6 lines

From: Arnd Bergmann <[email protected]>

The rkvdec_pps had a large set of bitfields, all of which as misaligned. This causes clang-21 and likely other versions to produce absolutely awful object code and a warning about very large stack usage, on targets without unaligned access:

Quoted 7 lines
Quoted 2 lines

I'm a bit surprised you felt the need for resend. Perhaps you can help us understand what made you think your patch wasn't being processed ?

I updated the second patch today after I found a corner case that wasn't addressed by the first version. As I had sent both as a series a month ago, and neither was in linux-next yet, it seemed more helpful to send an updated series rather than replace only one of the two.

If you updated the code I'd prefer if it is sent as a new version, not a resend. As for the media merge window, its been about a week since rc1 got merged into media tree, with couple of weeks before that waiting for rc1 to land. I believe your month gap is there and accurate. I did also took a small break on review and PR concurrently.

Quoted 9 lines
Quoted 8 lines

My PR:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

And Hans/Mauro did logistic on the #linux-maint IRC channel this morning. I believe I've marked all the relevant patches on patchwork our of "New" state and
you have my Rb. What else would help you ?

Quoted 4 lines

That's fine then, I did not mean to seem impatient. I assume the original patches will be in linux-next then, and [v2 2/2] will conflict. I'll let you review that one first, but can send a rebased version if you think we should merge it on top.

It will first reach media media-fixes (and not media-next, to avoid duplicating the patches), but I have no idea if someone picks from media-fixes into linux- next. I recall there was a gap to be fixed in this "pre-integration" process. Though, patch from fixes reaches RCs quickly, which are linux-next base.

Now, concurrently, Detlev is working on removing the bitfield for the RPS, and the SPS will come later. Perhaps you want to sync to make sure we don't just cancel out the work.

To solve the patch conflict issue, you can work on top of your existing series (just put a comment in the cover). I'll request a merge of rc2 / rc3 into media- next, before I pick it up.

regards,
Nicolas