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

Philipp Stanner <[email protected]> Fri, 31 Jul 2026 08:54:14 +0200
Newsgroups org.kernel.vger.linux-kbuild,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Hi,

On Thu, 2026-07-30 at 17:50 +0800, Kaitao Cheng wrote:
> From: Kaitao Cheng <[email protected]>
>=20
> 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.
>=20
> 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.
>=20
> 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.
>=20
> Signed-off-by: Kaitao Cheng <[email protected]>
> ---
> =C2=A0include/linux/list.h | 180 ++++++++++++++++++++++++++++++++++++++++=
+--
> =C2=A01 file changed, 175 insertions(+), 5 deletions(-)
>=20
> 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 lis=
t_head *list,
> =C2=A0
> =C2=A0/**
> =C2=A0 * list_for_each_safe - iterate over a list safe against removal of=
 list entry
> + *	with a caller-provided temporary cursor
> =C2=A0 * @pos:	the &struct list_head to use as a loop cursor.
> =C2=A0 * @n:		another &struct list_head to use as temporary storage
> =C2=A0 * @head:	the head for your list.
> + *
> + * If the caller does not need to access the temporary cursor, use
> + * list_for_each_mutable() instead.
> =C2=A0 */
> =C2=A0#define list_for_each_safe(pos, n, head) \
> =C2=A0

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