[PATCH 1/1] lib: sbi_list: add a helper for safe list iteration

Anup Patel anup at brainfault.org
Sun Jul 20 08:17:25 PDT 2025


On Wed, Jun 18, 2025 at 8:24 AM Yong-Xuan Wang <yongxuan.wang at sifive.com> wrote:
>
> Some use cases require iterating safe against removal of list entry.
>
> Signed-off-by: Yong-Xuan Wang <yongxuan.wang at sifive.com>

LGTM.

Reviewed-by: Anup Patel <anup at brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  include/sbi/sbi_list.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/include/sbi/sbi_list.h b/include/sbi/sbi_list.h
> index 9e56c526faf2..be69fc4314e0 100644
> --- a/include/sbi/sbi_list.h
> +++ b/include/sbi/sbi_list.h
> @@ -160,4 +160,17 @@ static inline void sbi_list_del_init(struct sbi_dlist *entry)
>              &pos->member != (head);    \
>              pos = sbi_list_entry(pos->member.next, typeof(*pos), member))
>
> +/**
> + * Iterate over list of given type safe against removal of list entry
> + * @param pos the type * to use as a loop cursor.
> + * @param n another type * to use as temporary storage.
> + * @param head the head for your list.
> + * @param member the name of the list_struct within the struct.
> + */
> +#define sbi_list_for_each_entry_safe(pos, n, head, member) \
> +       for (pos = sbi_list_entry((head)->next, typeof(*pos), member),  \
> +            n = sbi_list_entry(pos->member.next, typeof(*pos), member);        \
> +            &pos->member != (head);    \
> +            pos = n, n = sbi_list_entry(pos->member.next, typeof(*pos), member))
> +
>  #endif
> --
> 2.17.1
>



More information about the opensbi mailing list