From b4667e0445b9feffd69a43c2f0b458bae70db279 Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Sat, 23 Nov 2024 00:42:47 +0100 Subject: [PATCH] drivers: rtio: bugfix fixes the following: - If a read SQE generates a CQE with an error before a buffer has been allocated, the flags of the sqe are not correctly translated into cqe flags. - rtio_cqe_get_mempool_buffer(...) sets the *buff to a non-zero value, even if no buffer is assigned Signed-off-by: Florian Weber --- include/zephyr/rtio/rtio.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/include/zephyr/rtio/rtio.h b/include/zephyr/rtio/rtio.h index 9d117813185f..a7056f2e4ca7 100644 --- a/include/zephyr/rtio/rtio.h +++ b/include/zephyr/rtio/rtio.h @@ -1137,10 +1137,14 @@ static inline uint32_t rtio_cqe_compute_flags(struct rtio_iodev_sqe *iodev_sqe) if (iodev_sqe->sqe.op == RTIO_OP_RX && iodev_sqe->sqe.flags & RTIO_SQE_MEMPOOL_BUFFER) { struct rtio *r = iodev_sqe->r; struct sys_mem_blocks *mem_pool = r->block_pool; - int blk_index = (iodev_sqe->sqe.rx.buf - mem_pool->buffer) >> - mem_pool->info.blk_sz_shift; - int blk_count = iodev_sqe->sqe.rx.buf_len >> mem_pool->info.blk_sz_shift; + unsigned int blk_index = 0; + unsigned int blk_count = 0; + if (iodev_sqe->sqe.rx.buf) { + blk_index = (iodev_sqe->sqe.rx.buf - mem_pool->buffer) >> + mem_pool->info.blk_sz_shift; + blk_count = iodev_sqe->sqe.rx.buf_len >> mem_pool->info.blk_sz_shift; + } flags = RTIO_CQE_FLAG_PREP_MEMPOOL(blk_index, blk_count); } #else @@ -1173,15 +1177,21 @@ static inline int z_impl_rtio_cqe_get_mempool_buffer(const struct rtio *r, struc { #ifdef CONFIG_RTIO_SYS_MEM_BLOCKS if (RTIO_CQE_FLAG_GET(cqe->flags) == RTIO_CQE_FLAG_MEMPOOL_BUFFER) { - int blk_idx = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_IDX(cqe->flags); - int blk_count = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_CNT(cqe->flags); + unsigned int blk_idx = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_IDX(cqe->flags); + unsigned int blk_count = RTIO_CQE_FLAG_MEMPOOL_GET_BLK_CNT(cqe->flags); uint32_t blk_size = rtio_mempool_block_size(r); - *buff = r->block_pool->buffer + blk_idx * blk_size; *buff_len = blk_count * blk_size; - __ASSERT_NO_MSG(*buff >= r->block_pool->buffer); - __ASSERT_NO_MSG(*buff < + + if (blk_count > 0) { + *buff = r->block_pool->buffer + blk_idx * blk_size; + + __ASSERT_NO_MSG(*buff >= r->block_pool->buffer); + __ASSERT_NO_MSG(*buff < r->block_pool->buffer + blk_size * r->block_pool->info.num_blocks); + } else { + *buff = NULL; + } return 0; } return -EINVAL;