Skip to contentSkip to conversation

media: cedrus: h264/h265: Improve aux buffer management

PATCH New Jernej Skrabec 1 participant 3 messages Oct 19, 2022

Subsystems (4)
lore
Share & tools
lore

Jernej Skrabec

author maintainer
#0 lore raw

cover media: cedrus: h264/h265: Improve aux buffer management

Current handling of mv col aux buffers in h264 and h265 code is not optimal. In h264 case, it allocates pool for worst case possible which basically never happens with typical videos. This also prevents it from working well on very memory constrained devices, where CMA pool is very small. In h265 case, number of buffers in a pool is equal to number of capture buffers allocated before streaming is started. If any capture buffer is allocated afterwards, Cedrus gets bogus pointers. This might cause memory corruption.

Solution is the same in both cases. Instead of using memory pool, do individual allocations later, right before aux buffer is first used. Such approach allocates just right amount of buffers of optimal size.

Proposed solution frees aux buffers in streaming off callback, which is first possible occasion. I can move this to .buf_cleanup too, if you think that it's more appropriate.

Finally, h265 patch can be considered as a fix (prevents overwriting random memory). If deemed appropriate, fixes tag can be added.

Let me know what you think.

Best regards,
Jernej

Jernej Skrabec (2):
media: cedrus: h265: Associate mv col buffers with buffer
media: cedrus: h264: Optimize mv col buffer allocation

 drivers/staging/media/sunxi/cedrus/cedrus.h   |  16 +--
 .../staging/media/sunxi/cedrus/cedrus_h264.c  | 118 +++++++++---------
 .../staging/media/sunxi/cedrus/cedrus_h265.c  |  63 +++++-----
 3 files changed, 99 insertions(+), 98 deletions(-)

--
2.38.0

author maintainer
#1 lore raw

PATCH 1/2 media: cedrus: h265: Associate mv col buffers with buffer

Currently mv col aux buffers are allocated as a pool. This is not optimal because pool size equals number of buffers before stream is started. Buffers can easily be allocated afterwards. In such cases, invalid pointer is assigned to the decoding frame and Cedrus might overwrite memory location which is allocated to different task.

Solve this issue with allocating mv col buffer once capture buffer is actually used.

---
 drivers/staging/media/sunxi/cedrus/cedrus.h   |  9 +--
 .../staging/media/sunxi/cedrus/cedrus_h265.c  | 63 ++++++++++---------
 2 files changed, 38 insertions(+), 34 deletions(-)
drivers/staging/media/sunxi/cedrus/cedrus.h +5 -4
index 93a2196006f7..cb99610f3e12 100644
@@ -109,6 +109,11 @@ struct cedrus_buffer {
 			unsigned int			position;
 			enum cedrus_h264_pic_type	pic_type;
 		} h264;
+		struct {
+			void		*mv_col_buf;
+			dma_addr_t	mv_col_buf_dma;
+			ssize_t		mv_col_buf_size;
+		} h265;
 	} codec;
 };
 
@@ -142,10 +147,6 @@ struct cedrus_ctx {
 			ssize_t		intra_pred_buf_size;
 		} h264;
 		struct {
-			void		*mv_col_buf;
-			dma_addr_t	mv_col_buf_addr;
-			ssize_t		mv_col_buf_size;
-			ssize_t		mv_col_buf_unit_size;
 			void		*neighbor_info_buf;
 			dma_addr_t	neighbor_info_buf_addr;
 			void		*entry_points_buf;
drivers/staging/media/sunxi/cedrus/cedrus_h265.c +33 -30

120 lines not on this page · raw message

Signature · 1 line
2.38.0
author maintainer
#2 lore raw

PATCH 2/2 media: cedrus: h264: Optimize mv col buffer allocation

In reply to Jernej Skrabec

Currently allocation for mv col buffer pool is very wasteful. It allocates memory for worst case which is a lot more than it's needed for typical use. Fix that by replacing pool with individual allocations when such buffer is really needed. At that time all needed information for determining optimal mv col buffer size is also known.

---
 drivers/staging/media/sunxi/cedrus/cedrus.h   |   7 +-
 .../staging/media/sunxi/cedrus/cedrus_h264.c  | 118 +++++++++---------
 2 files changed, 61 insertions(+), 64 deletions(-)
drivers/staging/media/sunxi/cedrus/cedrus.h +3 -4
index cb99610f3e12..9cffaf228422 100644
@@ -108,6 +108,9 @@ struct cedrus_buffer {
 		struct {
 			unsigned int			position;
 			enum cedrus_h264_pic_type	pic_type;
+			void				*mv_col_buf;
+			dma_addr_t			mv_col_buf_dma;
+			ssize_t				mv_col_buf_size;
 		} h264;
 		struct {
 			void		*mv_col_buf;
@@ -130,10 +133,6 @@ struct cedrus_ctx {
 
 	union {
 		struct {
-			void		*mv_col_buf;
-			dma_addr_t	mv_col_buf_dma;
-			ssize_t		mv_col_buf_field_size;
-			ssize_t		mv_col_buf_size;
 			void		*pic_info_buf;
 			dma_addr_t	pic_info_buf_dma;
 			ssize_t		pic_info_buf_size;
drivers/staging/media/sunxi/cedrus/cedrus_h264.c +58 -60

197 lines not on this page · raw message

Signature · 1 line
2.38.0