From: Kaitao Cheng <kaitao.cheng@linux.dev>
To: "David Laight" <david.laight.linux@gmail.com>,
"Jani Nikula" <jani.nikula@linux.intel.com>,
"Christian König" <christian.koenig@amd.com>,
"David Hildenbrand" <david@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Nicolas Schier" <nsc@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>,
"Paul E . McKenney" <paulmck@kernel.org>,
David Howells <dhowells@redhat.com>,
Simona Vetter <simona.vetter@ffwll.ch>,
Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
Luca Ceresoli <luca.ceresoli@bootlin.com>,
Randy Dunlap <rdunlap@infradead.org>,
Kaitao Cheng <chengkaitao@kylinos.cn>,
Andrew Morton <akpm@linux-foundation.org>,
Philipp Stanner <phasta@kernel.org>,
Alex Williamson <alex@shazbot.org>,
David Matlack <dmatlack@google.com>,
Shuah Khan <skhan@linuxfoundation.org>,
"Joy H . J . Lee" <rkr0k0r@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Ian Rogers <irogers@google.com>,
Namhyung Kim <namhyung@kernel.org>,
Swapnil Sapkal <swapnil.sapkal@amd.com>,
linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 0/4] Prepare mutable list iterators to cache cursor state
Date: Thu, 30 Jul 2026 17:50:32 +0800 [thread overview]
Message-ID: <20260730095041.35715-1-kaitao.cheng@linux.dev> (raw)
From: Kaitao Cheng <chengkaitao@kylinos.cn>
The list_for_each*_safe() helpers are used when the loop body may remove
the current entry. Their current interface, however, forces every caller
to define a temporary cursor outside the macro and pass it in, even when
the caller never uses that cursor directly. For most call sites this
extra cursor is just boilerplate required by the macro implementation.
This is awkward because the saved next pointer is an internal detail of
the iteration. Callers that only remove or move the current entry do not
need to spell it out.
The _safe() suffix has also caused confusion. Christian Koenig pointed
out that the name is easy to read as a thread-safe variant, especially
for beginners, even though it only means that the iterator keeps enough
state to tolerate removal of the current entry. He suggested _mutable()
as a clearer description of what the loop permits.
Add *_mutable() iterator variants for list, hlist and llist. The caller
omits the temporary cursor and the macro creates a unique internal cursor
with typeof(pos) and __UNIQUE_ID(). The existing *_safe() helpers remain
available for compatibility.
For ease of discussion, I need to summarize the currently possible
approaches and briefly describe their respective pros and cons,
using the list_for_each_entry* interfaces as examples.
1. Add list_for_each_entry_mutable, while keeping list_for_each_entry
and list_for_each_entry_safe unchanged. list_for_each_entry_mutable
would be used specifically for safe deletion scenarios that do not
need to expose the temporary cursor externally. The code can refer to
the v4 and v1 versions.
Pros: Does not depend on immediate per-subsystem adaptation and can be
merged directly.
Cons: Requires adding a whole set of mutable interfaces, which makes the
code somewhat redundant.
2. Directly optimize away the temporary cursor in list_for_each_entry_safe
and define it inside the loop instead, changing the interface from four
arguments to three.
Pros: Does not add redundant interfaces.
Cons: (1) Users need to manually update special cases that use the
traversal variable of list_for_each_entry_safe, the new
list_for_each_entry_safe would no longer apply there and would
need to be open-coded.
(2) Because the macro arguments changes, all list_for_each_entry_safe
callers would need to be modified and merged together, making it
difficult to merge such a large amount of code at once.
3. Use a variadic macro approach to optimize list_for_each_entry_safe,
so that it supports both three and four arguments. The code can refer to
the v3 version.
Pros: (1) Does not add redundant interfaces.
(2) Does not depend on immediate per-subsystem adaptation and can
be merged directly.
Cons: (1) Increases compile time.
(2) Makes the interface harder for users to use.
4. Optimize list_for_each_entry by defining the temporary cursor
internally, making it compatible with the functionality of
list_for_each_entry_safe. The code can refer to the v2 version.
Pros: (1) Does not add redundant interfaces.
(2) The number of externally visible arguments of list_for_each_entry
remains unchanged, still three.
Cons: (1) list_for_each_entry and list_for_each_entry_safe would be merged
into one, and list_for_each_entry_safe would gradually be deprecated.
(2) Users need to manually update special cases that use the traversal
variable of list_for_each_entry, the new list_for_each_entry would no
longer apply there and would need to be open-coded. There are 15 such
cases in total.
5. Use a variadic macro approach to optimize list_for_each_entry, so that
it supports both three and four arguments.
Pros: (1) Does not add redundant interfaces.
(2) Does not depend on immediate per-subsystem adaptation and can be
merged directly.
Cons: (1) Increases compile time.
(2) list_for_each_entry and list_for_each_entry_safe would be merged
into one, and list_for_each_entry_safe would gradually be deprecated.
6. Make no changes, keep the current logic unchanged, and close the current
email discussion.
After broad discussion, more developers agreed to use option 1 for the
change. The implementation details follow the current patch series.
Changes in v4 (Jani Nikula):
- Abandon variadic macro approach
Changes in v3 (Christian König, Andy Shevchenko):
- Convert safe list walks to mutable iterators
Changes in v2 (Muchun Song, Andy Shevchenko):
- Drop the list_for_each_entry_mutable*() helpers from v1 and make the
cursor change directly in the existing list_for_each_entry*() helpers.
- Open-code special list walks that rely on updating the loop cursor in
the body, preserving their existing traversal semantics.
Link to v3:
https://lore.kernel.org/all/20260622040533.29824-1-kaitao.cheng@linux.dev/
Link to v2:
https://lore.kernel.org/all/20260609061347.93688-1-kaitao.cheng@linux.dev/
Link to v1:
https://lore.kernel.org/all/20260529082149.76764-1-kaitao.cheng@linux.dev/
Kaitao Cheng (4):
list: Add mutable iterator variants
llist: Add mutable iterator variants
tools/list: Add mutable iterator variants
scripts/list: Add mutable iterator variants
include/linux/list.h | 180 +++++++++++++++++++++++++++++++-
include/linux/llist.h | 67 +++++++++++-
scripts/include/list.h | 56 +++++++++-
tools/include/linux/compiler.h | 2 +
tools/include/linux/list.h | 184 +++++++++++++++++++++++++++++++--
5 files changed, 472 insertions(+), 17 deletions(-)
--
2.43.0
next reply other threads:[~2026-07-30 9:52 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-30 9:50 Kaitao Cheng [this message]
2026-07-30 9:50 ` [PATCH v4 1/4] list: Add mutable iterator variants Kaitao Cheng
2026-07-30 9:50 ` [PATCH v4 2/4] llist: " Kaitao Cheng
2026-07-30 9:50 ` [PATCH v4 3/4] tools/list: " Kaitao Cheng
2026-07-30 9:50 ` [PATCH v4 4/4] scripts/list: " Kaitao Cheng
2026-07-30 19:11 ` [PATCH v4 0/4] Prepare mutable list iterators to cache cursor state Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260730095041.35715-1-kaitao.cheng@linux.dev \
--to=kaitao.cheng@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=alex@shazbot.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=brauner@kernel.org \
--cc=chengkaitao@kylinos.cn \
--cc=christian.koenig@amd.com \
--cc=david.laight.linux@gmail.com \
--cc=david@kernel.org \
--cc=dhowells@redhat.com \
--cc=dmatlack@google.com \
--cc=irogers@google.com \
--cc=jani.nikula@linux.intel.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luca.ceresoli@bootlin.com \
--cc=namhyung@kernel.org \
--cc=nathan@kernel.org \
--cc=neeraj.upadhyay@kernel.org \
--cc=nsc@kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=phasta@kernel.org \
--cc=rdunlap@infradead.org \
--cc=rkr0k0r@gmail.com \
--cc=simona.vetter@ffwll.ch \
--cc=skhan@linuxfoundation.org \
--cc=swapnil.sapkal@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox