Re: [PATCH v4 1/4] list: Add mutable iterator variants

From: Philipp Stanner

Date: Fri Jul 31 2026 - 02:58:36 EST


Hi,

On Thu, 2026-07-30 at 17:50 +0800, Kaitao Cheng wrote:
> From: Kaitao Cheng <chengkaitao@xxxxxxxxxx>
>
> The list_for_each*_safe() helpers allow the current entry to be removed
> while iterating, but require callers to declare and pass a temporary
> cursor. Most callers never use that cursor directly, so exposing it adds
> boilerplate and leaks an implementation detail into each call site.
>
> Add *_mutable() variants for list and hlist iterators. These variants
> keep the temporary cursor in a uniquely named local variable inside the
> macro while preserving the removal-safe iteration semantics.
>
> Keep the existing *_safe() variants for callers that need to access or
> reset the temporary cursor, and update their documentation to clarify
> which interface to use.
>
> Signed-off-by: Kaitao Cheng <chengkaitao@xxxxxxxxxx>
> ---
>  include/linux/list.h | 180 +++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 175 insertions(+), 5 deletions(-)
>
> diff --git a/include/linux/list.h b/include/linux/list.h
> index 09d979976b3b..373b08b929d3 100644
> --- a/include/linux/list.h
> +++ b/include/linux/list.h
> @@ -765,26 +765,62 @@ static inline void list_splice_tail_init(struct list_head *list,
>  
>  /**
>   * list_for_each_safe - iterate over a list safe against removal of list entry
> + * with a caller-provided temporary cursor
>   * @pos: the &struct list_head to use as a loop cursor.
>   * @n: another &struct list_head to use as temporary storage
>   * @head: the head for your list.
> + *
> + * If the caller does not need to access the temporary cursor, use
> + * list_for_each_mutable() instead.
>   */
>  #define list_for_each_safe(pos, n, head) \
>  

I as a reader would probably be confused by this. What temporary
cursor? pos? n?

I tend to agree with Andrew's comment, that we would have to wonder how
much of an improvement more functions to choose from are, when
considering the cost-benefit-ratio.

Sure the temporary variables are often not needed, but sometimes APIs
are (have to be) just like that. With spin_lock_irqsave() you're also
forced to carry an integer around that you yourself never need.

Philipp