Skip to contentSkip to conversation

list: Prepare entry iterators to cache cursor state

PATCH Superseded Kaitao Cheng 5 participants 46 messages Jun 9 – Jun 13

Subsystems (18)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover list: Prepare entry iterators to cache cursor state

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

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 v1:
https://lore.kernel.org/all/[email protected]/

Kaitao Cheng (14):
drbd: Open-code transfer log list walk
firewire: core: Open-code topology list walk drm/bridge: Open-code bridge chain list walks drm/i915/gt: Open-code active timeline walk drm/i915: Open-code DFS dependency list walk drm/ttm: Open-code reservation list walk
spi: fsi: Open-code message transfer walk
spi: stm32-ospi: Open-code message transfer walk
spi: stm32-qspi: Open-code message transfer walk
spi: tegra210-quad: Open-code message transfer walk locking/locktorture: Open-code ww mutex list walk locking/ww_mutex: Open-code stress reorder list walk
ASoC: dapm: Open-code widget invalidation walk
list: Cache cursors in entry iterators

 drivers/block/drbd/drbd_debugfs.c      |  4 ++-
 drivers/firewire/core-topology.c       |  4 ++-
 drivers/gpu/drm/drm_bridge.c           |  7 ++--
 drivers/gpu/drm/i915/gt/intel_reset.c  |  4 ++-
 drivers/gpu/drm/i915/i915_scheduler.c  |  4 ++-
 drivers/gpu/drm/ttm/ttm_execbuf_util.c |  4 ++-
 drivers/spi/spi-fsi.c                  |  5 ++-
 drivers/spi/spi-stm32-ospi.c           |  4 ++-
 drivers/spi/spi-stm32-qspi.c           |  5 ++-
 drivers/spi/spi-tegra210-quad.c        |  4 ++-
 include/linux/list.h                   | 46 ++++++++++++++++++++------
 kernel/locking/locktorture.c           |  4 ++-
 kernel/locking/test-ww_mutex.c         |  4 ++-
 sound/soc/soc-dapm.c                   |  4 ++-
 14 files changed, 78 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/14 drbd: Open-code transfer log list walk

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. That is the desired behaviour for the common case, but this transfer log walk temporarily drops resource->req_lock and revalidates the cursor before continuing.

Keep the loop open-coded so the next request is derived after the body has completed and after the cursor has been adjusted. This preserves the existing traversal semantics and prepares the code for the list iterator update.

---
 drivers/block/drbd/drbd_debugfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/block/drbd/drbd_debugfs.c +3 -1
index 12460b584bcb..e90cead90e9d 100644
@@ -308,7 +308,9 @@ static void seq_print_resource_transfer_log_summary(struct seq_file *m,
 
 	seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
 	spin_lock_irq(&resource->req_lock);
-	list_for_each_entry(req, &connection->transfer_log, tl_requests) {
+	for (req = list_first_entry(&connection->transfer_log, typeof(*req), tl_requests);
+	     !list_entry_is_head(req, &connection->transfer_log, tl_requests);
+	     req = list_next_entry(req, tl_requests)) {
 		unsigned int tmp = 0;
 		unsigned int s;
 		++count;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 2/14 firewire: core: Open-code topology list walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. for_each_fw_node() intentionally appends newly discovered child nodes to the temporary walk list while the list is being traversed.

Keep the loop open-coded so the next node is looked up only after children have been appended. This preserves the current breadth-first traversal semantics and prepares the code for the list iterator update.

---
 drivers/firewire/core-topology.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firewire/core-topology.c +3 -1
index bb2d2db30795..df2ac0dab106 100644
@@ -272,7 +272,9 @@ static void for_each_fw_node(struct fw_card *card, struct fw_node *root,
 	fw_node_get(root);
 	list_add_tail(&root->link, &list);
 	parent = NULL;
-	list_for_each_entry(node, &list, link) {
+	for (node = list_first_entry(&list, typeof(*node), link);
+	     !list_entry_is_head(node, &list, link);
+	     node = list_next_entry(node, link)) {
 		node->color = card->color;
 
 		for (i = 0; i < node->port_count; i++) {
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 3/14 drm/bridge: Open-code bridge chain list walks

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry_from() and list_for_each_entry_reverse() cache the next or previous element before entering the loop body. The bridge enable and disable ordering code adjusts its cursor to skip ranges that have already been handled.

Keep those walks open-coded so the loop step observes the cursor selected by the body. This preserves the existing bridge ordering semantics and prepares the code for the list iterator update.

---
 drivers/gpu/drm/drm_bridge.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_bridge.c +5 -2
index d6f512b73389..a538aabc4e0b 100644
@@ -868,7 +868,8 @@ void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
 
 	encoder = bridge->encoder;
 
-	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
+	for (; !list_entry_is_head(bridge, &encoder->bridge_chain, chain_node);
+	     bridge = list_next_entry(bridge, chain_node)) {
 		limit = NULL;
 
 		if (!list_is_last(&bridge->chain_node, &encoder->bridge_chain)) {
@@ -962,7 +963,9 @@ void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
 
 	encoder = bridge->encoder;
 
-	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
+	for (iter = list_last_entry(&encoder->bridge_chain, typeof(*iter), chain_node);
+	     !list_entry_is_head(iter, &encoder->bridge_chain, chain_node);
+	     iter = list_prev_entry(iter, chain_node)) {
 		if (iter->pre_enable_prev_first) {
 			next = iter;
 			limit = bridge;
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 4/14 drm/i915/gt: Open-code active timeline walk

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. __intel_gt_unset_wedged() drops timelines->lock while waiting on a fence and then restarts the walk from the list head after the lock is reacquired.

Keep the loop open-coded so the next timeline is selected after that restart logic has run. This preserves the existing lock-drop traversal semantics and prepares the code for the list iterator update.

---
 drivers/gpu/drm/i915/gt/intel_reset.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_reset.c +3 -1
index adff482a6c9c..fe0d87e248a7 100644
@@ -1077,7 +1077,9 @@ static bool __intel_gt_unset_wedged(struct intel_gt *gt)
 	 * No more can be submitted until we reset the wedged bit.
 	 */
 	spin_lock(&timelines->lock);
-	list_for_each_entry(tl, &timelines->active_list, link) {
+	for (tl = list_first_entry(&timelines->active_list, typeof(*tl), link);
+	     !list_entry_is_head(tl, &timelines->active_list, link);
+	     tl = list_next_entry(tl, link)) {
 		struct dma_fence *fence;
 
 		fence = i915_active_fence_get(&tl->last_request);
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 5/14 drm/i915: Open-code DFS dependency list walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. __i915_schedule() builds its DFS work list while walking it by moving newly discovered dependencies to the tail.

Keep the DFS walk open-coded so the next dependency is resolved after any tail moves performed by the body. This preserves the existing traversal semantics and prepares the code for the list iterator update.

---
 drivers/gpu/drm/i915/i915_scheduler.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_scheduler.c +3 -1
index aec1342402ca..da1f60282df8 100644
@@ -190,7 +190,9 @@ static void __i915_schedule(struct i915_sched_node *node,
 	 * end result is a topological list of requests in reverse order, the
 	 * last element in the list is the request we must execute first.
 	 */
-	list_for_each_entry(dep, &dfs, dfs_link) {
+	for (dep = list_first_entry(&dfs, typeof(*dep), dfs_link);
+	     !list_entry_is_head(dep, &dfs, dfs_link);
+	     dep = list_next_entry(dep, dfs_link)) {
 		struct i915_sched_node *node = dep->signaler;
 
 		/* If we are already flying, we know we have no signalers */
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 6/14 drm/ttm: Open-code reservation list walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. ttm_eu_reserve_buffers() may move the current validation buffer to the duplicates list and then rewinds the cursor before continuing.

Keep the reservation walk open-coded so the loop step uses the cursor selected by that duplicate handling. This preserves the existing traversal semantics and prepares the code for the list iterator update.

---
 drivers/gpu/drm/ttm/ttm_execbuf_util.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_execbuf_util.c +3 -1
index bc7a83a9fe44..8072f07d5557 100644
@@ -86,7 +86,9 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 	if (ticket)
 		ww_acquire_init(ticket, &reservation_ww_class);
 
-	list_for_each_entry(entry, list, head) {
+	for (entry = list_first_entry(list, typeof(*entry), head);
+	     !list_entry_is_head(entry, list, head);
+	     entry = list_next_entry(entry, head)) {
 		struct ttm_buffer_object *bo = entry->bo;
 		unsigned int num_fences;
 
Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 7/14 spi: fsi: Open-code message transfer walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. fsi_spi_transfer_one_message() can combine the current transfer with the following transfer and then advance the cursor to that consumed entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

---
 drivers/spi/spi-fsi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/spi/spi-fsi.c +4 -1
index f6a75f0184c4..44999f00f5f6 100644
@@ -434,7 +434,10 @@ static int fsi_spi_transfer_one_message(struct spi_controller *ctlr,
 	if (rc)
 		goto error;
 
-	list_for_each_entry(transfer, &mesg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&mesg->transfers,
+					 typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &mesg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		struct fsi_spi_sequence seq;
 		struct spi_transfer *next = NULL;
 
Signature · 1 line
2.43.0
#14 lore raw

PATCH v2 8/14 spi: stm32-ospi: Open-code message transfer walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. stm32_ospi_transfer_one_message() can consume the following transfer as part of the current operation and then advance the loop cursor to that entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

---
 drivers/spi/spi-stm32-ospi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-stm32-ospi.c +3 -1
index 4461c6e24b9e..4dc2b56b4c20 100644
@@ -675,7 +675,9 @@ static int stm32_ospi_transfer_one_message(struct spi_controller *ctrl,
 
 	gpiod_set_value_cansleep(cs_gpiod, true);
 
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers, typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		u8 dummy_bytes = 0;
 
 		memset(&op, 0, sizeof(op));
Signature · 1 line
2.43.0
#16 lore raw

PATCH v2 9/14 spi: stm32-qspi: Open-code message transfer walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. stm32_qspi_transfer_one_message() can consume the following transfer as part of the current operation and then advance the loop cursor to that entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

---
 drivers/spi/spi-stm32-qspi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/spi/spi-stm32-qspi.c +4 -1
index df1bbacec90a..27d82a578c9f 100644
@@ -577,7 +577,10 @@ static int stm32_qspi_transfer_one_message(struct spi_controller *ctrl,
 
 	gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), true);
 
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers,
+					 typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		u8 dummy_bytes = 0;
 
 		memset(&op, 0, sizeof(op));
Signature · 1 line
2.43.0
#18 lore raw

PATCH v2 10/14 spi: tegra210-quad: Open-code message transfer walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. tegra_qspi_non_combined_seq_xfer() can consume the following transfer as part of the current operation and then advance the loop cursor to that entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

---
 drivers/spi/spi-tegra210-quad.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-tegra210-quad.c +3 -1
index db28dd556484..42dd5cf53c67 100644
@@ -1302,7 +1302,9 @@ static int tegra_qspi_non_combined_seq_xfer(struct tegra_qspi *tqspi,
 	if (tqspi->soc_data->supports_tpm)
 		val &= ~QSPI_TPM_WAIT_POLL_EN;
 	tegra_qspi_writel(tqspi, val, QSPI_GLOBAL_CONFIG);
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers, typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		struct spi_transfer *xfer = transfer;
 		u8 dummy_bytes = 0;
 		u32 cmd1;
Signature · 1 line
2.43.0
#19 lore raw

PATCH v2 11/14 locking/locktorture: Open-code ww mutex list walk

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. The ww-mutex torture path can move list entries while it resolves a wound/wait conflict and then continue from the adjusted cursor.

Keep the list walk open-coded so the loop step observes the cursor selected by the body. This preserves the existing stress-test traversal semantics and prepares the code for the list iterator update.

---
 kernel/locking/locktorture.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/locking/locktorture.c +3 -1
index e618bcf75e2d..0eb75e9bccaa 100644
@@ -644,7 +644,9 @@ __acquires(torture_ww_mutex_2)
 
 	ww_acquire_init(ctx, &torture_ww_class);
 
-	list_for_each_entry(ll, &list, link) {
+	for (ll = list_first_entry(&list, typeof(*ll), link);
+	     !list_entry_is_head(ll, &list, link);
+	     ll = list_next_entry(ll, link)) {
 		int err;
 
 		err = ww_mutex_lock(ll->lock, ctx);
Signature · 1 line
2.43.0
#21 lore raw

PATCH v2 12/14 locking/ww_mutex: Open-code stress reorder list walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. stress_reorder_work() can move list entries while handling wound/wait locking conflicts and then continue from the adjusted cursor.

Keep the list walk open-coded so the loop step observes the cursor selected by the body. This preserves the existing stress-test traversal semantics and prepares the code for the list iterator update.

---
 kernel/locking/test-ww_mutex.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/locking/test-ww_mutex.c +3 -1
index 838d631544ed..08a6ab5ac041 100644
@@ -519,7 +519,9 @@ static void stress_reorder_work(struct work_struct *work)
 	do {
 		ww_acquire_init(&ctx, stress->class);
 
-		list_for_each_entry(ll, &locks, link) {
+		for (ll = list_first_entry(&locks, typeof(*ll), link);
+		     !list_entry_is_head(ll, &locks, link);
+		     ll = list_next_entry(ll, link)) {
 			err = ww_mutex_lock(ll->lock, &ctx);
 			if (!err)
 				continue;
Signature · 1 line
2.43.0
#23 lore raw

PATCH v2 13/14 ASoC: dapm: Open-code widget invalidation walk

In reply to Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. dapm_widget_invalidate_paths() appends newly reached widgets to the temporary work list while walking it.

Keep the work-list walk open-coded so the next widget is looked up after new widgets have been appended. This preserves the existing invalidation traversal semantics and prepares the code for the list iterator update.

---
 sound/soc/soc-dapm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
sound/soc/soc-dapm.c +3 -1
index d6192204e613..5bd921fca132 100644
@@ -255,7 +255,9 @@ static __always_inline void dapm_widget_invalidate_paths(
 	list_add_tail(&w->work_list, &list);
 	w->endpoints[dir] = -1;
 
-	list_for_each_entry(w, &list, work_list) {
+	for (w = list_first_entry(&list, typeof(*w), work_list);
+	     !list_entry_is_head(w, &list, work_list);
+	     w = list_next_entry(w, work_list)) {
 		snd_soc_dapm_widget_for_each_path(w, dir, p) {
 			if (p->is_supply || !p->connect)
 				continue;
Signature · 1 line
2.43.0
#24 lore raw

PATCH v2 14/14 list: Cache cursors in entry iterators

From: Kaitao Cheng <[email protected]>

The non-safe list_for_each_entry() family advances by deriving the next element from the current cursor in the loop step. If the loop body unlinks the current entry, the step can no longer rely on the current entry's list pointers.

Callers can use the _safe variants today, but those interfaces require a temporary cursor to be declared outside the macro. That is necessary when the caller actually needs the temporary cursor, but it looks redundant and awkward when the cursor is only there to satisfy the macro and is never otherwise used.

Add private next and previous cursors for the common entry iterators and use unique internal names so callers keep the same interface. This lets the loop step use a cursor captured before the body runs, while callers that need to alter traversal state can still open-code the walk.

The safe variants remain useful when the caller needs access to the temporary cursor or has stronger mutation requirements. Update their comments to steer users toward the simpler iterators when that temporary cursor is not needed.

---
 include/linux/list.h | 46 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 11 deletions(-)
include/linux/list.h +35 -11
index 09d979976b3b..9df84a56a789 100644
@@ -809,6 +809,29 @@ static inline size_t list_count_nodes(struct list_head *head)
 #define list_entry_is_head(pos, head, member)				\
 	list_is_head(&pos->member, (head))
 
+#define __list_for_each_entry(pos, next, head, member)			\
+	for (typeof(pos) next = list_next_entry(pos =			\
+		list_first_entry(head, typeof(*pos), member), member);	\
+	     !list_entry_is_head(pos, head, member);			\
+	     pos = next, next = list_next_entry(next, member))
+
+#define __list_for_each_entry_reverse(pos, prev, head, member)		\
+	for (typeof(pos) prev = list_prev_entry(pos =			\
+		list_last_entry(head, typeof(*pos), member), member);	\
+	     !list_entry_is_head(pos, head, member);			\
+	     pos = prev, prev = list_prev_entry(prev, member))
+
+#define __list_for_each_entry_continue(pos, next, head, member)		\
+	for (typeof(pos) next = list_next_entry(pos =			\
+		list_next_entry(pos, member), member);			\
+	     !list_entry_is_head(pos, head, member);			\
+	     pos = next, next = list_next_entry(next, member))
+
+#define __list_for_each_entry_from(pos, next, head, member)		\
+	for (typeof(pos) next = list_next_entry(pos, member);		\
+	     !list_entry_is_head(pos, head, member);			\
+	     pos = next, next = list_next_entry(next, member))
+
 /**
  * list_for_each_entry	-	iterate over list of given type
  * @pos:	the type * to use as a loop cursor.
@@ -816,9 +839,7 @@ static inline size_t list_count_nodes(struct list_head *head)
  * @member:	the name of the list_head within the struct.
  */
 #define list_for_each_entry(pos, head, member)				\
-	for (pos = list_first_entry(head, typeof(*pos), member);	\
-	     !list_entry_is_head(pos, head, member);			\
-	     pos = list_next_entry(pos, member))
+	__list_for_each_entry(pos, __UNIQUE_ID(next), head, member)
 
 /**
  * list_for_each_entry_reverse - iterate backwards over list of given type.
@@ -827,9 +848,7 @@ static inline size_t list_count_nodes(struct list_head *head)
  * @member:	the name of the list_head within the struct.
  */
 #define list_for_each_entry_reverse(pos, head, member)			\
-	for (pos = list_last_entry(head, typeof(*pos), member);		\
-	     !list_entry_is_head(pos, head, member); 			\
-	     pos = list_prev_entry(pos, member))
+	__list_for_each_entry_reverse(pos, __UNIQUE_ID(prev), head, member)
 
 /**
  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
@@ -852,9 +871,7 @@ static inline size_t list_count_nodes(struct list_head *head)
  * the current position.
  */
 #define list_for_each_entry_continue(pos, head, member) 		\
-	for (pos = list_next_entry(pos, member);			\
-	     !list_entry_is_head(pos, head, member);			\
-	     pos = list_next_entry(pos, member))
+	__list_for_each_entry_continue(pos, __UNIQUE_ID(next), head, member)
 
 /**
  * list_for_each_entry_continue_reverse - iterate backwards from the given point
@@ -879,8 +896,7 @@ static inline size_t list_count_nodes(struct list_head *head)
  * Iterate over list of given type, continuing from current position.
  */
 #define list_for_each_entry_from(pos, head, member) 			\
-	for (; !list_entry_is_head(pos, head, member);			\
-	     pos = list_next_entry(pos, member))
+	__list_for_each_entry_from(pos, __UNIQUE_ID(next), head, member)
 
 /**
  * list_for_each_entry_from_reverse - iterate backwards over list of given type
@@ -901,6 +917,8 @@ static inline size_t list_count_nodes(struct list_head *head)
  * @n:		another type * to use as temporary storage
  * @head:	the head for your list.
  * @member:	the name of the list_head within the struct.
+ *
+ * Prefer list_for_each_entry() unless the temporary cursor is needed.
  */
 #define list_for_each_entry_safe(pos, n, head, member)			\
 	for (pos = list_first_entry(head, typeof(*pos), member),	\
@@ -917,6 +935,8 @@ static inline size_t list_count_nodes(struct list_head *head)
  *
  * Iterate over list of given type, continuing after current point,
  * safe against removal of list entry.
+ *
+ * Prefer list_for_each_entry_continue() unless the temporary cursor is needed.
  */
 #define list_for_each_entry_safe_continue(pos, n, head, member) 		\
 	for (pos = list_next_entry(pos, member), 				\
@@ -933,6 +953,8 @@ static inline size_t list_count_nodes(struct list_head *head)
  *
  * Iterate over list of given type from current point, safe against
  * removal of list entry.
+ *
+ * Prefer list_for_each_entry_from() unless the temporary cursor is needed.
  */
 #define list_for_each_entry_safe_from(pos, n, head, member) 			\
 	for (n = list_next_entry(pos, member);					\
@@ -948,6 +970,8 @@ static inline size_t list_count_nodes(struct list_head *head)
  *
  * Iterate backwards over list of given type, safe against removal
  * of list entry.
+ *
+ * Prefer list_for_each_entry_reverse() unless the temporary cursor is needed.
  */
 #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
 	for (pos = list_last_entry(head, typeof(*pos), member),		\
Signature · 1 line
2.43.0
#27 lore raw
In reply to Kaitao Cheng
Quoted 11 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Something is really wrong with the patch series email chaining. Patches 3, 10, and 13 start the subthreads. Please, check your tools and fix them accordingly.

Note, git format-patch ... should not have this "side-effect" when used correctly.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH v2 06/14] drm/ttm: Open-code reservation list walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing lock release when dma_resv_reserve_fences() fails, leaving the current buffer object permanently locked. --

commit 4a941c2e22e18c4c60dbeb3162fc85117e45e7a8
Author: Kaitao Cheng <[email protected]>

drm/ttm: Open-code reservation list walk

This patch modifies ttm_eu_reserve_buffers() to open-code the list_for_each_entry() loop. This prepares the code for future list iterator updates while preserving the existing traversal semantics where the cursor can be rewound during duplicate handling.

drivers/gpu/drm/ttm/ttm_execbuf_util.c int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket, · line 91

Quoted 12 lines
drivers/gpu/drm/ttm/ttm_execbuf_util.c +3 -1
index bc7a83a9fe443..8072f07d55579 100644
@@ -86,7 +86,9 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 	if (ticket)
 		ww_acquire_init(ticket, &reservation_ww_class);
 
-	list_for_each_entry(entry, list, head) {
+	for (entry = list_first_entry(list, typeof(*entry), head);
+	     !list_entry_is_head(entry, list, head);
+	     entry = list_next_entry(entry, head)) {

[Severity: High]
This is a pre-existing issue, but does the error path in this loop leak the lock on entry->bo when dma_resv_reserve_fences() fails?

If ttm_bo_reserve() successfully acquires the lock on the current entry->bo, and then dma_resv_reserve_fences() fails (for example, returning -ENOMEM), the code cleans up using ttm_eu_backoff_reservation_reverse():

ttm_eu_reserve_buffers() {
    ...
    if (!ret) {
        ret = dma_resv_reserve_fences(bo->base.resv, num_fences);
        if (!ret)
            continue;
    }

    /* uh oh, we lost out, drop every reservation and try... */
    ttm_eu_backoff_reservation_reverse(list, entry);
    ...
}

However, ttm_eu_backoff_reservation_reverse() explicitly skips unlocking the current entry itself because it uses list_for_each_entry_continue_reverse().

The code then bypasses the slowpath because ret is -ENOMEM (not -EDEADLK) and returns without unlocking entry->bo:

ttm_eu_reserve_buffers() {
    ...
    if (ret == -EDEADLK) {
        ret = ttm_bo_reserve_slowpath(bo, intr, ticket);
    }
    ...
    if (unlikely(ret != 0)) {
        if (ticket) {
            ww_acquire_done(ticket);
            ww_acquire_fini(ticket);
        }
        return ret;
    }
    ...
}

Could this sequence leave the ww_mutex for entry->bo permanently locked?

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_execbuf_util.c +0 -0
 		struct ttm_buffer_object *bo = entry->bo;
 		unsigned int num_fences;
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#4 lore raw

Re: [PATCH v2 02/14] firewire: core: Open-code topology list walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Memory leak in build_tree() where dynamically allocated fw_node subtrees on the local stack list are leaked when returning NULL on error paths.
- [High] Unconditional dereference of card->root_node in fw_core_handle_bus_reset() leading to a kernel panic if build_tree() fails on the first bus reset. --

commit 814723ea200ca545cf7dcf5b3f733df13cc3c3f2
Author: Kaitao Cheng <[email protected]>

firewire: core: Open-code topology list walk

This commit open-codes the topology list walk in for_each_fw_node() to defer looking up the next node until after children are appended. It preserves breadth-first traversal semantics while preparing the code for a future list iterator update.

Quoted 4 lines
drivers/firewire/core-topology.c +0 -0
index bb2d2db30795e..df2ac0dab1060 100644

[ ... ]

drivers/firewire/core-topology.c:build_tree() {
    ...
		if (child_port_count > stack_depth) {
			fw_err(card, "topology stack underflow\n");
			return NULL;
		}
    ...
}

[Severity: High]
This is a pre-existing issue, but does this code leak the dynamically allocated fw_node subtrees on the local stack list when returning NULL? Since the stack list goes out of scope on this error path, it seems the attached nodes are abandoned without being freed.

drivers/firewire/core-topology.c:fw_core_handle_bus_reset() {
    ...
	scoped_guard(spinlock, &card->topology_map.lock) {
		update_topology_map(card->topology_map.buffer, sizeof(card->topology_map.buffer),
				    card->root_node->node_id, self_ids, self_id_count);
	}
    ...
}

[Severity: High]
This is a pre-existing issue, but can this sequence cause a panic?

If build_tree() fails and returns NULL during the first bus reset, card->root_node would be left as NULL. Would passing card->root_node->node_id unconditionally to update_topology_map() then result in a NULL pointer dereference?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#2 lore raw

Re: [PATCH v2 01/14] drbd: Open-code transfer log list walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Dropping req_lock during transfer log iteration allows the current request to be moved to a different list, causing out-of-bounds reads and an infinite loop. --

commit eaa3f881c5752fd80fb9f58149972d66629f2d5b
Author: Kaitao Cheng <[email protected]>

drbd: Open-code transfer log list walk

This commit open-codes the transfer log list walk in drbd_debugfs.c to preserve the existing traversal semantics where the cursor is revalidated after temporarily dropping a lock, in preparation for an upcoming change to list_for_each_entry().

drivers/block/drbd/drbd_debugfs.c static void seq_print_resource_transfer_log_summary(struct seq_file *m, · line 313

Quoted 12 lines
drivers/block/drbd/drbd_debugfs.c +3 -1
index 12460b584bcb9..e90cead90e9db 100644
@@ -308,7 +308,9 @@ static void seq_print_resource_transfer_log_summary(struct seq_file *m,
 
 	seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
 	spin_lock_irq(&resource->req_lock);
-	list_for_each_entry(req, &connection->transfer_log, tl_requests) {
+	for (req = list_first_entry(&connection->transfer_log, typeof(*req), tl_requests);
+	     !list_entry_is_head(req, &connection->transfer_log, tl_requests);
+	     req = list_next_entry(req, tl_requests)) {

[Severity: High]
This is a pre-existing issue, but can this loop run infinitely or read out-of-bounds memory when resource->req_lock is dropped inside the loop body?

Later in seq_print_resource_transfer_log_summary(), the lock is dropped to yield:

			spin_unlock_irq(&resource->req_lock);
			cond_resched();
			spin_lock_irq(&resource->req_lock);
			req_next = list_next_entry(req, tl_requests);

While the lock is dropped, if another thread calls drbd_restart_request() in drivers/block/drbd/drbd_main.c, the current req can be moved from connection->transfer_log to the global retry.writes list via list_move_tail():

drivers/block/drbd/drbd_main.c:drbd_restart_request() {
    ...
	spin_lock_irqsave(&retry.lock, flags);
	list_move_tail(&req->tl_requests, &retry.writes);
	spin_unlock_irqrestore(&retry.lock, flags);
    ...
}

When the lock is reacquired, req remains valid because of the kref_get() call, but list_next_entry() will now follow retry.writes instead of transfer_log.

Because the loop terminates by checking against &connection->transfer_log, traversing retry.writes bypasses the break condition.

Furthermore, if do_retry() concurrently splices retry.writes onto a local stack list, can this traversal continue into unmapped or overwritten stack memory?

Quoted 3 lines
drivers/block/drbd/drbd_debugfs.c +0 -0
 		unsigned int tmp = 0;
 		unsigned int s;
 		++count;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#20 lore raw

Re: [PATCH v2 11/14] locking/locktorture: Open-code ww mutex list walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The WW mutex documentation promotes an idiomatic pattern that relies on `list_for_each_entry()`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The WW mutex documentation promotes an idiomatic pattern that relies on list_for_each_entry() not caching the next element, which is broken by the upcoming list iterator update. --

commit 803c4bd20ba4e9475a66f9600c81114e4079f384
Author: Kaitao Cheng <[email protected]>

locking/locktorture: Open-code ww mutex list walk

This commit open-codes the ww mutex list walk in locktorture. It prepares the code for an upcoming change that makes list_for_each_entry() cache the next element before entering the loop body, which preserves the existing dynamic traversal semantics.

Quoted 5 lines
kernel/locking/locktorture.c +0 -0
index e618bcf75e2d8..0eb75e9bccaa6 100644

kernel/locking/locktorture.c __acquires(torture_ww_mutex_2) · line 652

Quoted 10 lines
kernel/locking/locktorture.c +3 -1
@@ -644,7 +644,9 @@ __acquires(torture_ww_mutex_2)
 
 	ww_acquire_init(ctx, &torture_ww_class);
 
-	list_for_each_entry(ll, &list, link) {
+	for (ll = list_first_entry(&list, typeof(*ll), link);
+	     !list_entry_is_head(ll, &list, link);
+	     ll = list_next_entry(ll, link)) {
 		int err;
 
 		err = ww_mutex_lock(ll->lock, ctx);

[Severity: Medium]
Since this patch updates locktorture to avoid regressions in the ww-mutex list walk under the new list iterator semantics, does Documentation/locking/ww-mutex-design.rst also need to be updated?

The documentation for "Method 2" explicitly encourages using list_for_each_entry() for dynamic list reordering:

Documentation/locking/ww-mutex-design.rst:lock_objs() {
...

			/* we lost out in a seqno race, lock and retry.. */
			ww_mutex_lock_slow(&entry->obj->lock, ctx);

			/*
			 * Move buf to head of the list, this will point
			 * buf->next to the first unlocked entry,
			 * restarting the for loop.
			 */
			list_del(&entry->head);
			list_add(&entry->head, list);

...
}

If a developer follows this documented pattern with the new list iterator semantics, moving the list entry to the head of the list will no longer restart the loop. Instead, the loop will continue from the cached next element, skipping over unlocked entries and silently failing to acquire all required locks. Could leaving this unchanged cause regressions in drivers that copy this pattern?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#26 lore raw

Re: [PATCH v2 13/14] ASoC: dapm: Open-code widget invalidation walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Incomplete preparation for list iterator semantics change causes DAPM power loop to terminate…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incomplete preparation for list iterator semantics change causes DAPM power loop to terminate prematurely. --

commit 43f2e05998002333ccbe3a3f202cf3f76c35d1b0
Author: Kaitao Cheng <[email protected]>

ASoC: dapm: Open-code widget invalidation walk

This commit prepares dapm_widget_invalidate_paths() for an upcoming change to list_for_each_entry() that will cache the next element. It open-codes the loop to preserve the existing semantics where new widgets can be appended during traversal.

sound/soc/soc-dapm.c static __always_inline void dapm_widget_invalidate_paths( · line 260

Quoted 12 lines
sound/soc/soc-dapm.c +3 -1
index d6192204e613d..5bd921fca1323 100644
@@ -255,7 +255,9 @@ static __always_inline void dapm_widget_invalidate_paths(
 	list_add_tail(&w->work_list, &list);
 	w->endpoints[dir] = -1;
 
-	list_for_each_entry(w, &list, work_list) {
+	for (w = list_first_entry(&list, typeof(*w), work_list);
+	     !list_entry_is_head(w, &list, work_list);
+	     w = list_next_entry(w, work_list)) {

[Severity: High]
Does this open-coding also need to be applied to the loop in dapm_power_widgets()?

dapm_power_widgets() loops over card->dapm_dirty using list_for_each_entry():

sound/soc/soc-dapm.c:dapm_power_widgets() {
	...
	list_for_each_entry(w, &card->dapm_dirty, dirty) {
		dapm_power_one_widget(w, &up_list, &down_list);
	}
	...
}

Inside that loop, dapm_power_one_widget() can invoke dapm_widget_set_peer_power(), which calls dapm_mark_dirty() to append peer widgets to the tail of card->dapm_dirty.

If the upcoming list_for_each_entry() changes cache the next element, won't the loop in dapm_power_widgets() terminate early and fail to process newly dirtied widgets if they are appended while processing the current tail element?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#17 lore raw

Re: [PATCH v2 09/14] spi: stm32-qspi: Open-code message transfer walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Early returns in stm32_qspi_transfer_one_message fail to call spi_finalize_current_message(), leaking the SPI message and hanging the caller.
- [High] The driver mishandles SPI transfers where both tx_buf and rx_buf are NULL but len > 0, causing a NULL pointer dereference. --

commit 0feb4c5cf513d6c2af724ffe0a636c0f0b92a880
Author: Kaitao Cheng <[email protected]>

spi: stm32-qspi: Open-code message transfer walk

A later change will make list_for_each_entry() cache the next element before entering the loop body. stm32_qspi_transfer_one_message() can consume the following transfer as part of the current operation and then advance the loop cursor to that entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

Quoted 6 lines
drivers/spi/spi-stm32-qspi.c +0 -0
index ea69fe25686f3..e264fee2bd2d7 100644
@@ -576,7 +576,10 @@ static int stm32_qspi_transfer_one_message(struct spi_controller *ctrl,
 

drivers/spi/spi-stm32-qspi.c static int stm32_qspi_transfer_one_message(struct spi_controller *ctrl, · line 585

Quoted 10 lines
drivers/spi/spi-stm32-qspi.c +4 -1
 	gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), true);
 
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers,
+					 typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		u8 dummy_bytes = 0;
 
 		memset(&op, 0, sizeof(op));

[Severity: High]
This is a pre-existing issue, but I noticed early returns in stm32_qspi_transfer_one_message() might fail to call spi_finalize_current_message() and leak the SPI message:

	if (!spi_get_csgpiod(spi, 0))
		return -EOPNOTSUPP;

	ret = pm_runtime_resume_and_get(qspi->dev);
	if (ret < 0)
		return ret;

Could these early returns cause spi_sync() to hang indefinitely on wait_for_completion() and permanently lock the SPI bus? Should we arrange for spi_finalize_current_message() to be called in these error paths?

[Severity: High]
This isn't a bug introduced by this patch, but there might be an issue with how the driver handles SPI transfers where both tx_buf and rx_buf are NULL but len > 0. In stm32_qspi_transfer_one_message():

		} else {
			qspi->fmode = CCR_FMODE_INDW;
			op.data.buswidth = transfer->tx_nbits;
			op.data.dir = SPI_MEM_DATA_OUT;
			op.data.buf.out = transfer->tx_buf;
		}

If a local user sends a message via spidev with tx_buf = 0, rx_buf = 0, and len > 0, the driver assumes it is a transmit operation and assigns op.data.buf.out to NULL.

Does the operation then fall back to stm32_qspi_tx_poll() which loops over len, and since the transmit FIFO is empty, immediately satisfies the FIFO Threshold Flag?

If so, could this result in dereferencing the NULL buf pointer in stm32_qspi_tx_poll():

		fifo(buf, qspi->io_base + QSPI_DR, step);
		len -= step;
		buf += step;

and triggering a kernel panic?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#15 lore raw

Re: [PATCH v2 08/14] spi: stm32-ospi: Open-code message transfer walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] A valid SPI message containing a transfer with length > 0 but both tx_buf and rx_buf set to NULL will cause a kernel crash when the driver dereferences the NULL buffer during FIFO writes.
- [High] Early error returns in stm32_ospi_transfer_one_message fail to finalize the SPI message, resulting in a permanent hang for callers.
--

commit 77e013496da647c31add0f5085a07d31c7c250b2
Author: Kaitao Cheng <[email protected]>

spi: stm32-ospi: Open-code message transfer walk

This commit modifies stm32_ospi_transfer_one_message() to open-code the list iteration over message transfers. It prepares the loop to observe cursor updates explicitly, anticipating a later list iterator update.

drivers/spi/spi-stm32-ospi.c static int stm32_ospi_transfer_one_message(struct spi_controller *ctrl,

Quoted 5 lines
drivers/spi/spi-stm32-ospi.c +0 -0
index 3757f6ba8fc61..fe92bb06251be 100644
@@ -674,7 +674,9 @@ static int stm32_ospi_transfer_one_message(struct spi_controller *ctrl,

[Severity: High]
This is a pre-existing issue, but do the early error returns in stm32_ospi_transfer_one_message() fail to finalize the SPI message?

If cs_gpiod is null or pm_runtime_resume_and_get() fails, the function returns early:

	if (!cs_gpiod)
		return -EOPNOTSUPP;

	ret = pm_runtime_resume_and_get(ospi->dev);
	if (ret < 0)
		return ret;

Since these paths bypass spi_finalize_current_message(), does this leave the SPI core waiting indefinitely and cause a deadlock for callers?

[Severity: High]
This is also a pre-existing issue, but can this driver crash if an SPI message contains a dummy transfer with both tx_buf and rx_buf set to NULL?

The SPI core allows dummy transfers with both buffers set to NULL. If a transfer reaches stm32_ospi_transfer_one_message() without the dummy_data flag set, the driver defaults to a write operation and assigns the NULL tx_buf to the operation:

		} else {
			ospi->fmode = CR_FMODE_INDW;
			op.data.buswidth = transfer->tx_nbits;
			op.data.dir = SPI_MEM_DATA_OUT;
			op.data.buf.out = transfer->tx_buf;
		}

This eventually leads to stm32_ospi_send() and stm32_ospi_xfer(). Because DMA mapping of a NULL pointer fails, it falls back to PIO mode by calling stm32_ospi_poll().

Inside stm32_ospi_poll(), the stm32_ospi_write_fifo() callback is invoked, which dereferences the NULL pointer directly:

	switch (len) {
	case sizeof(u32):
		writel_relaxed(*((u32 *)val), addr);
		break;

Could this trigger a kernel panic and allow a local denial of service?

Quoted 10 lines
drivers/spi/spi-stm32-ospi.c +3 -1
 	gpiod_set_value_cansleep(cs_gpiod, true);
 
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers, typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		u8 dummy_bytes = 0;
 
 		memset(&op, 0, sizeof(op));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#22 lore raw

Re: [PATCH v2 10/14] spi: tegra210-quad: Open-code message transfer walk

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Pre-existing issues:
- [High] Hardware Chip Select (CS) is left asserted indefinitely if the final message transfer is absorbed as a dummy transfer.
- [High] DMA memory mapping leak on error paths in tegra_qspi_start_dma_based_transfer.
- [High] DMA memory mapping leak upon transfer timeout or hardware error.
- [High] DMA maps memory beyond the end of the SPI transfer buffer when using packed DMA transfers.
- [High] Unconditional DMA unmap on error path in tegra_qspi_dma_map_xfer.
--

commit dedc4ce6598360fa2a4de3d4413aaecfda3051a4
Author: Kaitao Cheng <[email protected]>

spi: tegra210-quad: Open-code message transfer walk

This commit converts the list_for_each_entry() loop in tegra_qspi_non_combined_seq_xfer() to an open-coded for-loop. This preserves semantics when consuming the next transfer in the loop body, preparing the code for future list iterator updates.

Quoted 5 lines
drivers/spi/spi-tegra210-quad.c +0 -0
index 588a929a97850..102562893d227 100644

drivers/spi/spi-tegra210-quad.c static int tegra_qspi_non_combined_seq_xfer(struct tegra_qspi *tqspi, · line 1317

Quoted 10 lines
drivers/spi/spi-tegra210-quad.c +3 -1
@@ -1309,7 +1309,9 @@ static int tegra_qspi_non_combined_seq_xfer(struct tegra_qspi *tqspi,
 	if (tqspi->soc_data->supports_tpm)
 		val &= ~QSPI_TPM_WAIT_POLL_EN;
 	tegra_qspi_writel(tqspi, val, QSPI_GLOBAL_CONFIG);
-	list_for_each_entry(transfer, &msg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&msg->transfers, typeof(*transfer), transfer_list);
+	     !list_entry_is_head(transfer, &msg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
 		struct spi_transfer *xfer = transfer;
 		u8 dummy_bytes = 0;
 		u32 cmd1;

[Severity: High]
This is a pre-existing issue, but does this logic fail to deactivate the chip select if the final transfer is absorbed as a dummy transfer?

In tegra_qspi_non_combined_seq_xfer(), if the final SPI transfer in the message is a dummy transfer, the loop pointer advances to it, effectively consuming it into the current xfer:

    if (next_xfer->dummy_data) {
        ...
        transfer = next_xfer;
    }

When checking if CS should be deactivated at the end of the loop body, the code uses xfer instead of the updated transfer pointer:

    if (list_is_last(&xfer->transfer_list, &msg->transfers)) {
        if (!xfer->cs_change) {
            tegra_qspi_transfer_end(spi);
            ...
        }
    } else if (xfer->cs_change) {
        ...

Since xfer is the second-to-last transfer, list_is_last() evaluates to false. The code falls through to the else branch, and if cs_change is false, tegra_qspi_transfer_end() is skipped.

The loop then terminates, leaving the hardware chip select asserted indefinitely. Could this corrupt subsequent bus communications?

[Severity: High]
This is a pre-existing issue, but do we leak DMA memory mappings if a transfer times out or encounters a hardware error?

Looking at tegra_qspi_non_combined_seq_xfer(), if the transfer times out, tegra_qspi_handle_timeout() confirms the timeout, stops the DMA engine channels, and jumps to complete_xfer:

    if (tqspi->is_curr_dma_xfer)
        tegra_qspi_dma_stop(tqspi);
    ...
    goto complete_xfer;

Similarly, if a hardware error is detected, we also jump to complete_xfer:

    if (tqspi->tx_status || tqspi->rx_status) {
        ...
        goto complete_xfer;
    }

In both paths, the code exits without calling tegra_qspi_dma_unmap_xfer(). Are these DMA mappings permanently leaked?

[Severity: High]
This isn't a bug introduced by this patch, but does tegra_qspi_start_dma_based_transfer() leak DMA memory mappings on its error paths?

When tqspi->is_packed is true, tegra_qspi_dma_map_xfer() successfully maps the TX and RX DMA buffers.

If any subsequent DMA setup operations fail (like dmaengine_slave_config(), tegra_qspi_start_tx_dma(), or tegra_qspi_start_rx_dma()), the function returns the error directly:

    ret = dmaengine_slave_config(tqspi->rx_dma_chan, &dma_sconfig);
    if (ret < 0) {
        dev_err(tqspi->dev, "failed DMA slave config: %d\n", ret);
        return ret;
    }

The caller tegra_qspi_non_combined_seq_xfer() then propagates the error and exits, never calling tegra_qspi_dma_unmap_xfer().

Does this permanently leak the DMA mapped memory and IOMMU space?

[Severity: High]
This is a pre-existing issue, but does the DMA mapping calculation risk crossing an unmapped physical page boundary?

In tegra_qspi_dma_map_xfer(), when tqspi->is_packed is true, the mapped length is artificially inflated to a multiple of 4:

    len = DIV_ROUND_UP(tqspi->curr_dma_words * tqspi->bytes_per_word, 4) * 4;

    if (t->tx_buf) {
        t->tx_dma = dma_map_single(tqspi->tx_dma_dev, (void *)tx_buf,
                                   len, DMA_TO_DEVICE);

Because the original client-provided buffer might be exactly t->len bytes long, mapping beyond its exact length could cross into restricted or unallocated physical memory.

Could this trigger an IOMMU fault or a kernel panic?

[Severity: High]
This isn't a bug introduced by this patch, but does the error path in tegra_qspi_dma_map_xfer() unconditionally unmap the TX buffer even when it was never mapped?

If t->tx_buf is NULL during a read-only transfer, the TX buffer mapping is correctly skipped.

However, if the subsequent dma_map_single() for t->rx_buf fails, the error handling block unmaps tx_dma without checking:

    if (dma_mapping_error(tqspi->rx_dma_dev, t->rx_dma)) {
        dma_unmap_single(tqspi->tx_dma_dev, t->tx_dma, len, DMA_TO_DEVICE);
        return -ENOMEM;
    }

Since t->tx_buf was NULL, t->tx_dma was never mapped by this function.

Could unmapping it here violate the DMA API contract, potentially unmapping page 0 from the IOMMU or corrupting DMA tracking state?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#25 lore raw

Re: [PATCH v2 14/14] list: Cache cursors in entry iterators

In reply to Kaitao Cheng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Modifying the standard list iterators to pre-cache the `next` pointer fundamentally changes…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Modifying the standard list iterators to pre-cache the next pointer fundamentally changes their traversal semantics from dynamic to static evaluation, causing use-after-free (UAF) vulnerabilities and silently breaking loops that modify the list ahead of the cursor or skip elements by advancing pos.
- [High] Eliminating the distinction between safe and non-safe list iterators obscures code intent, removes a vital visual guardrail for destructive operations, and introduces severe backporting risks.
--

commit e1c09f66ccd28cca66865681febeb6f8233f0a36
Author: Kaitao Cheng <[email protected]>

list: Cache cursors in entry iterators

This commit introduces private next and previous cursor variables into the standard list iteration macros. It changes the semantics of the list_for_each_entry() family to cache the next pointer before executing the loop body. It also updates comments to recommend the non-safe variants over the safe ones when a temporary cursor is not needed.

Quoted 13 lines
include/linux/list.h +5 -0
index 09d979976b3b8..9df84a56a789b 100644
@@ -809,6 +809,29 @@ static inline size_t list_count_nodes(struct list_head *head)
 #define list_entry_is_head(pos, head, member)				\
 	list_is_head(&pos->member, (head))
 
+#define __list_for_each_entry(pos, next, head, member)			\
+	for (typeof(pos) next = list_next_entry(pos =			\
+		list_first_entry(head, typeof(*pos), member), member);	\
+	     !list_entry_is_head(pos, head, member);			\
+	     pos = next, next = list_next_entry(next, member))

[ ... ]

include/linux/list.h static inline size_t list_count_nodes(struct list_head *head) · line 842

Quoted 8 lines
include/linux/list.h +1 -3
@@ -816,9 +839,7 @@ static inline size_t list_count_nodes(struct list_head *head)
  * @member:	the name of the list_head within the struct.
  */
 #define list_for_each_entry(pos, head, member)				\
-	for (pos = list_first_entry(head, typeof(*pos), member);	\
-	     !list_entry_is_head(pos, head, member);			\
-	     pos = list_next_entry(pos, member))
+	__list_for_each_entry(pos, __UNIQUE_ID(next), head, member)

[Severity: Critical]
Does caching the next pointer fundamentally change the dynamic traversal semantics of the standard list iterators kernel-wide?

A common kernel pattern is pinning the current entry (pos), dropping the lock to perform a blocking operation, and reacquiring the lock. With dynamic evaluation, pos->next is safely fetched under the lock.

With the new cached next pointer, if the next element was removed and freed by a concurrent thread while the lock was dropped, next becomes a dangling pointer, which could lead to a use-after-free when the loop steps.

Will this also silently skip dynamically inserted elements? If a loop body inserts a new element immediately after pos, the old macro would naturally visit it next. The new macro appears to silently skip it because it advances using the pre-cached next.

Could this also overwrite manual cursor updates? If the loop body explicitly alters pos to skip elements, the loop increment (pos = next) might overwrite the updated cursor with the pre-cached next, negating the skip and potentially causing incorrect traversal behavior.

[ ... ]

include/linux/list.h static inline size_t list_count_nodes(struct list_head *head) · line 923

Quoted 8 lines
include/linux/list.h +2 -0
@@ -901,6 +917,8 @@ static inline size_t list_count_nodes(struct list_head *head)
  * @n:		another type * to use as temporary storage
  * @head:	the head for your list.
  * @member:	the name of the list_head within the struct.
+ *
+ * Prefer list_for_each_entry() unless the temporary cursor is needed.
  */
 #define list_for_each_entry_safe(pos, n, head, member)			\

[Severity: High]
Does advising developers to prefer list_for_each_entry() over the _safe variant for removals obscure the intent of the code?

The _safe variant explicitly signals to reviewers and static analysis tools that the loop body may mutate the list. Conflating variable-scoping convenience with traversal safety constraints removes a visual guardrail for destructive operations.

Additionally, does this introduce a backporting hazard? If developers write new code that unlinks elements using list_for_each_entry(), backporting those patches to older stable kernels (which lack this caching patch) will compile cleanly but might reliably introduce use-after-free vulnerabilities, as older kernels evaluate pos->next after pos has been unlinked.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#8 lore raw

Re: [PATCH v2 04/14] drm/i915/gt: Open-code active timeline walk

In reply to Kaitao Cheng
Quoted 9 lines · Kaitao Cheng

A later change will make list_for_each_entry() cache the next element before entering the loop body. __intel_gt_unset_wedged() drops timelines->lock while waiting on a fence and then restarts the walk from the list head after the lock is reacquired.

Keep the loop open-coded so the next timeline is selected after that restart logic has run. This preserves the existing lock-drop traversal semantics and prepares the code for the list iterator update.

...

drivers/gpu/drm/i915/gt/intel_reset.c static bool __intel_gt_unset_wedged(struct intel_gt *gt) · line 1082

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_reset.c +3 -1
 	spin_lock(&timelines->lock);
-	list_for_each_entry(tl, &timelines->active_list, link) {
+	for (tl = list_first_entry(&timelines->active_list, typeof(*tl), link);
+	     !list_entry_is_head(tl, &timelines->active_list, link);
+	     tl = list_next_entry(tl, link)) {

Yeah, these cases should rather be converted to do {} while or while-loop. This will make the intention clearer and reduces the possibility that someone mistakenly changes these back to use list_for_each_entry().

See, for example, deferred_probe_work_func() implementation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

Re: [PATCH v2 07/14] spi: fsi: Open-code message transfer walk

In reply to Kaitao Cheng
Quoted 10 lines · Kaitao Cheng

A later change will make list_for_each_entry() cache the next element before entering the loop body. fsi_spi_transfer_one_message() can combine the current transfer with the following transfer and then advance the cursor to that consumed entry.

Keep the transfer walk open-coded so the loop step observes that cursor update and skips the consumed transfer. This preserves the existing message sequencing semantics and prepares the code for the list iterator update.

...

drivers/spi/spi-fsi.c static int fsi_spi_transfer_one_message(struct spi_controller *ctlr, · line 438

Quoted 3 lines
drivers/spi/spi-fsi.c +2 -1
-	list_for_each_entry(transfer, &mesg->transfers, transfer_list) {
+	for (transfer = list_first_entry(&mesg->transfers,
+					 typeof(*transfer), transfer_list);

You can keep this on a single line for more logical split.

	for (transfer = list_first_entry(&mesg->transfers, typeof(*transfer), transfer_list);

it's under relaxed limits for the line length.

Quoted 2 lines
drivers/spi/spi-fsi.c +2 -0
+	     !list_entry_is_head(transfer, &mesg->transfers, transfer_list);
+	     transfer = list_next_entry(transfer, transfer_list)) {
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#28 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

While code looks okay, this doesn't explain "why?" aspects.

Quoted 17 lines
Quoted 10 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Something is really wrong with the patch series email chaining. Patches 3, 10, and 13 start the subthreads. Please, check your tools and fix them accordingly.

Note, git format-patch ... should not have this "side-effect" when used correctly.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#29 lore raw
In reply to Kaitao Cheng
Quoted 5 lines · Kaitao Cheng

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Regards,
Christian.

Quoted 50 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

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 v1:
https://lore.kernel.org/all/[email protected]/

Kaitao Cheng (14):
drbd: Open-code transfer log list walk
firewire: core: Open-code topology list walk drm/bridge: Open-code bridge chain list walks drm/i915/gt: Open-code active timeline walk drm/i915: Open-code DFS dependency list walk drm/ttm: Open-code reservation list walk
spi: fsi: Open-code message transfer walk
spi: stm32-ospi: Open-code message transfer walk
spi: stm32-qspi: Open-code message transfer walk
spi: tegra210-quad: Open-code message transfer walk locking/locktorture: Open-code ww mutex list walk locking/ww_mutex: Open-code stress reorder list walk
ASoC: dapm: Open-code widget invalidation walk
list: Cache cursors in entry iterators

 drivers/block/drbd/drbd_debugfs.c      |  4 ++-
 drivers/firewire/core-topology.c       |  4 ++-
 drivers/gpu/drm/drm_bridge.c           |  7 ++--
 drivers/gpu/drm/i915/gt/intel_reset.c  |  4 ++-
 drivers/gpu/drm/i915/i915_scheduler.c  |  4 ++-
 drivers/gpu/drm/ttm/ttm_execbuf_util.c |  4 ++-
 drivers/spi/spi-fsi.c                  |  5 ++-
 drivers/spi/spi-stm32-ospi.c           |  4 ++-
 drivers/spi/spi-stm32-qspi.c           |  5 ++-
 drivers/spi/spi-tegra210-quad.c        |  4 ++-
 include/linux/list.h                   | 46 ++++++++++++++++++++------
 kernel/locking/locktorture.c           |  4 ++-
 kernel/locking/test-ww_mutex.c         |  4 ++-
 sound/soc/soc-dapm.c                   |  4 ++-
 14 files changed, 78 insertions(+), 25 deletions(-)

--
2.43.0

#30 lore raw
Quoted 13 lines · Christian König
Quoted 5 lines · Kaitao Cheng

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 51 lines
Quoted 50 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

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 v1:
https://lore.kernel.org/all/[email protected]/

Kaitao Cheng (14):
drbd: Open-code transfer log list walk
firewire: core: Open-code topology list walk drm/bridge: Open-code bridge chain list walks drm/i915/gt: Open-code active timeline walk drm/i915: Open-code DFS dependency list walk drm/ttm: Open-code reservation list walk
spi: fsi: Open-code message transfer walk
spi: stm32-ospi: Open-code message transfer walk
spi: stm32-qspi: Open-code message transfer walk
spi: tegra210-quad: Open-code message transfer walk locking/locktorture: Open-code ww mutex list walk locking/ww_mutex: Open-code stress reorder list walk
ASoC: dapm: Open-code widget invalidation walk
list: Cache cursors in entry iterators

 drivers/block/drbd/drbd_debugfs.c      |  4 ++-
 drivers/firewire/core-topology.c       |  4 ++-
 drivers/gpu/drm/drm_bridge.c           |  7 ++--
 drivers/gpu/drm/i915/gt/intel_reset.c  |  4 ++-
 drivers/gpu/drm/i915/i915_scheduler.c  |  4 ++-
 drivers/gpu/drm/ttm/ttm_execbuf_util.c |  4 ++-
 drivers/spi/spi-fsi.c                  |  5 ++-
 drivers/spi/spi-stm32-ospi.c           |  4 ++-
 drivers/spi/spi-stm32-qspi.c           |  5 ++-
 drivers/spi/spi-tegra210-quad.c        |  4 ++-
 include/linux/list.h                   | 46 ++++++++++++++++++++------
 kernel/locking/locktorture.c           |  4 ++-
 kernel/locking/test-ww_mutex.c         |  4 ++-
 sound/soc/soc-dapm.c                   |  4 ++-
 14 files changed, 78 insertions(+), 25 deletions(-)

--
2.43.0

Signature · 2 lines
Thanks
Kaitao Cheng
#31 lore raw
Quoted 53 lines · Kaitao Cheng
Quoted 13 lines · Christian König
Quoted 5 lines · Kaitao Cheng

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Quoted 5 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Well the distinction between list_for_each_entry() and list_for_each_entry_safe() is *not* there because you need an extra variable to hold the next pointer, but because just 'iterating the list' and 'iterating the list while you modify it' are two distinct use cases.

Apart from the technical implications this also has some documentation value for the code using it.

What we could consider with C99 at hand is to have _safe() variants who uses a local hidden variable to hold the next element.

Or maybe come up with a better name instead of _safe() because people seem to misunderstand that quite often.

But mangling the two use cases together just because it is now technical possible is among the worst ideas I've ever heard.

Regards,
Christian.

#32 lore raw
Quoted 62 lines · Christian König
Quoted 53 lines · Kaitao Cheng
Quoted 13 lines · Christian König
Quoted 5 lines · Kaitao Cheng

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Quoted 5 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Well the distinction between list_for_each_entry() and list_for_each_entry_safe() is *not* there because you need an extra variable to hold the next pointer, but because just 'iterating the list' and 'iterating the list while you modify it' are two distinct use cases.

Quoted 8 lines

Apart from the technical implications this also has some documentation value for the code using it.

What we could consider with C99 at hand is to have _safe() variants who uses a local hidden variable to hold the next element.

Or maybe come up with a better name instead of _safe() because people seem to misunderstand that quite often.

But mangling the two use cases together just because it is now technical possible is among the worst ideas I've ever heard.

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

Signature · 2 lines
Thanks
Kaitao Cheng
#33 lore raw
Quoted 74 lines · Kaitao Cheng
Quoted 62 lines · Christian König
Quoted 53 lines · Kaitao Cheng
Quoted 13 lines · Christian König
Quoted 5 lines · Kaitao Cheng

From: Kaito Cheng <[email protected]>

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Quoted 5 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Well the distinction between list_for_each_entry() and list_for_each_entry_safe() is *not* there because you need an extra variable to hold the next pointer, but because just 'iterating the list' and 'iterating the list while you modify it' are two distinct use cases.

Quoted 8 lines

Apart from the technical implications this also has some documentation value for the code using it.

What we could consider with C99 at hand is to have _safe() variants who uses a local hidden variable to hold the next element.

Or maybe come up with a better name instead of _safe() because people seem to misunderstand that quite often.

But mangling the two use cases together just because it is now technical possible is among the worst ideas I've ever heard.

Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

Regards,
Christian.

#40 lore raw
In reply to Kaitao Cheng
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 4 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Sorry if I didn't get the whole idea from your previous contributions.

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Quoted 10 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 4 lines
Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#34 lore raw
In reply to Christian König
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 10 lines
Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

Quoted 5 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#41 lore raw
In reply to Andy Shevchenko
Quoted 57 lines · Andy Shevchenko
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 4 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Quoted 5 lines

Sorry if I didn't get the whole idea from your previous contributions.

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Thanks for your suggestions. I've written a demo based on your feedback. Could you please review it and share your thoughts on this approach?

include/linux/list.h +24 -8
index 9df84a56a789..306554ab1841 100644
@@ -7,6 +7,7 @@
 #include <linux/stddef.h>
 #include <linux/poison.h>
 #include <linux/const.h>
+#include <linux/args.h>
 
 #include <asm/barrier.h>
 
@@ -911,20 +912,34 @@ static inline size_t list_count_nodes(struct list_head *head)
        for (; !list_entry_is_head(pos, head, member);                  \
             pos = list_prev_entry(pos, member))
 
+#define __list_for_each_entry_safe_internal(pos, next, head, member)   \
+       for (typeof(pos) next = list_next_entry(pos =                   \
+               list_first_entry(head, typeof(*pos), member), member);  \
+            !list_entry_is_head(pos, head, member);                    \
+            pos = next, next = list_next_entry(next, member))
+
+#define __list_for_each_entry_safe2(pos, head, member)                 \
+       __list_for_each_entry_safe_internal(pos, __UNIQUE_ID(next), head, member)
+
+#define __list_for_each_entry_safe3(pos, next, head, member)           \
+       for (pos = list_first_entry(head, typeof(*pos), member),        \
+               next = list_next_entry(pos, member);                    \
+            !list_entry_is_head(pos, head, member);                    \
+            pos = next, next = list_next_entry(next, member))
+
 /**
  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  * @pos:       the type * to use as a loop cursor.
- * @n:         another type * to use as temporary storage
- * @head:      the head for your list.
- * @member:    the name of the list_head within the struct.
+ * @...:       either (head, member) or (next, head, member)
+ *     @next:  another type * to use as optional temporary storage. The temporary
+ *             cursor is internal unless explicitly supplied by the caller.
+ *     @head:  the head for your list.
+ *     @member:the name of the list_head within the struct.
  *
  */
-#define list_for_each_entry_safe(pos, n, head, member)                 \
-       for (pos = list_first_entry(head, typeof(*pos), member),        \
-               n = list_next_entry(pos, member);                       \
-            !list_entry_is_head(pos, head, member);                    \
-            pos = n, n = list_next_entry(n, member))
+#define list_for_each_entry_safe(pos, ...)                             \
+       CONCATENATE(__list_for_each_entry_safe, COUNT_ARGS(__VA_ARGS__))\
+               (pos, __VA_ARGS__)
 
 /**
  * list_for_each_entry_safe_continue - continue list iteration safe against removal
Quoted 12 lines
Quoted 10 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 4 lines
Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Signature · 2 lines
Thanks
Kaitao Cheng
#42 lore raw
Quoted 58 lines · Kaitao Cheng
Quoted 57 lines · Andy Shevchenko
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 3 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Quoted 8 lines
Quoted 5 lines

Sorry if I didn't get the whole idea from your previous contributions.

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Thanks for your suggestions. I've written a demo based on your feedback. Could you please review it and share your thoughts on this approach?

Have you checked how many users actually need the temporary storage?

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 4 lines
Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#43 lore raw
Quoted 61 lines · Andy Shevchenko
Quoted 60 lines · Kaitao Cheng
Quoted 59 lines · Andy Shevchenko
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 3 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Sorry if I didn't get the whole idea from your previous contributions.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Thanks for your suggestions. I've written a demo based on your feedback. Could you please review it and share your thoughts on this approach?

Have you checked how many users actually need the temporary storage?

In Muchun's reply, he mentioned the following:

There are 9,925 list_for_each_entry() call sites in total. Among them, 9,919 do not require any adaptation, and only 6 need to be refactored:

As for list_for_each_entry_safe(), there are 4,572 callers. 4,550 of them can be directly replaced by the new list_for_each_entry(), while 22 cannot be replaced

https://lore.kernel.org/all/[email protected]/

I only used Coccinelle to scan for list_for_each_entry() call sites, and found the 13 call sites shown in the current patch series, which cover the 6 cases mentioned in Muchun's email. I have not yet run the Coccinelle scan for list_for_each_entry_safe().

If we need to handle all 9,925 list_for_each_entry() call sites or all 4,572 list_for_each_entry_safe() call sites in one go, would such a change be too large? I expect it would affect almost every kernel subsystem.

I wonder whether it would be better to first provide the necessary compatibility APIs, and then let each subsystem owner update their code as appropriate. That would make the impact more controlled, similar to how the current folio replacement of page is being handled.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 4 lines
Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Signature · 2 lines
Thanks
Kaitao Cheng
#44 lore raw
Quoted 82 lines · Kaitao Cheng
Quoted 61 lines · Andy Shevchenko
Quoted 60 lines · Kaitao Cheng
Quoted 59 lines · Andy Shevchenko
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 3 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Sorry if I didn't get the whole idea from your previous contributions.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Thanks for your suggestions. I've written a demo based on your feedback. Could you please review it and share your thoughts on this approach?

Have you checked how many users actually need the temporary storage?

In Muchun's reply, he mentioned the following:

There are 9,925 list_for_each_entry() call sites in total. Among them, 9,919 do not require any adaptation, and only 6 need to be refactored:

As for list_for_each_entry_safe(), there are 4,572 callers. 4,550 of them can be directly replaced by the new list_for_each_entry(), while 22 cannot be replaced

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

I only used Coccinelle to scan for list_for_each_entry() call sites, and found the 13 call sites shown in the current patch series, which cover the 6 cases mentioned in Muchun's email. I have not yet run the Coccinelle scan for list_for_each_entry_safe().

If we need to handle all 9,925 list_for_each_entry() call sites or all 4,572 list_for_each_entry_safe() call sites in one go, would such a change be too large? I expect it would affect almost every kernel subsystem.

If it's done by Linus himself during the day when he prepares -rc1, it's fine. You would need to provide a good justification for the change, though.

But in the above statistics the 4572 vs 4550, so the first step is to investigate why temporary cursor is used in those 22 cases and what we can do to avoid that.

Quoted 15 lines

I wonder whether it would be better to first provide the necessary compatibility APIs, and then let each subsystem owner update their code as appropriate. That would make the impact more controlled, similar to how the current folio replacement of page is being handled.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

With the updated list_for_each_entry() implementation, that extra cursor can be kept inside the iterator itself. Callers that only want to walk the list, including callers that delete or consume the current entry, no longer need to carry an otherwise-unused temporary variable just to make the macro work.

Quoted 4 lines
Quoted 4 lines

The final patch changes include/linux/list.h to keep a private cursor in the common entry iterators while preserving the public macro interface. The safe variants remain available when callers need the temporary cursor explicitly or have stronger mutation requirements.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#35 lore raw
In reply to Andy Shevchenko
Quoted 13 lines · Andy Shevchenko
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 7 lines
Quoted 7 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

As far as I can see that is an extremely bad idea.

The distinction between the use cases of 'iterating the list' and 'iterating the list while you modify it' is completely intentional.

See the bool type can be implemented by int as well, but it is just a different use case.

Regards,
Christian.

Quoted 7 lines
Quoted 6 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

#36 lore raw
Quoted 4 lines · Christian König
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

Quoted 7 lines
Quoted 2 lines

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

As far as I can see that is an extremely bad idea.

The distinction between the use cases of 'iterating the list' and 'iterating the list while you modify it' is completely intentional.

What I meant is to keep the name, just drop the parameter (make it hidden and being defined inside list_for_each_*_safe() cases).

Quoted 10 lines

See the bool type can be implemented by int as well, but it is just a different use case.

Quoted 6 lines
Quoted 5 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#37 lore raw
Quoted 22 lines · Andy Shevchenko
Quoted 4 lines · Christian König
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

Quoted 7 lines
Quoted 4 lines

As far as I can see that is an extremely bad idea.

The distinction between the use cases of 'iterating the list' and 'iterating the list while you modify it' is completely intentional.

What I meant is to keep the name, just drop the parameter (make it hidden and being defined inside list_for_each_*_safe() cases).

Ah, sorry I was still thinking the suggestion is to merge list_for_each_entry() and list_for_each_entry_safe().

If the modification is done all at once or in steps doesn't really matter for me as long as the patch can be re-created reproducible.

But I'm wondering if we couldn't improve the name at the same time. The _safe() postfix has caused tons of confusion where especially beginners thought that it is a thread-safe variant, which it clearly isn't.

The _mutable() postfix sounds like a much better description to what happens here.

Regards,
Christian.

Quoted 12 lines
Quoted 11 lines

See the bool type can be implemented by int as well, but it is just a different use case.

Quoted 6 lines
Quoted 5 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

#45 lore raw
In reply to Andy Shevchenko
Quoted 92 lines · Andy Shevchenko
Quoted 82 lines · Kaitao Cheng
Quoted 61 lines · Andy Shevchenko
Quoted 60 lines · Kaitao Cheng
Quoted 59 lines · Andy Shevchenko
Quoted 45 lines · Kaitao Cheng
Quoted 12 lines · Christian König
Quoted 3 lines · Kaitao Cheng

This series prepares for, and then updates, the list_for_each_entry() family so the common entry iterators cache their next or previous cursor before the loop body runs.

Why in the world would we want to do that?

The safe and non-safe variants have very distinct use cases and that is completely intentional.

What we could improve maybe is the documentation, from my experience an astonishing large amount of people have misconceptions about the safe variants.

Quoted 7 lines
Quoted 5 lines

The first 13 patches open-code loops that intentionally depend on the old "derive the next entry from the current cursor at the end of the iteration" behaviour. These loops append work to the list being walked, restart traversal after dropping a lock, skip an entry consumed by the current iteration, or otherwise adjust the cursor in the loop body.

Well I have to clearly reject the changes for subsystems/components I'm maintaining, that just looks horrible to me and I clearly don't see a good reason for that.

Hi Christian and Andy Shevchenko,

Thanks for taking a look. I would like to clarify the point you raised.

The reason I started looking at this is the original motivation behind the _safe() variants. They exist because some users need to remove, move or otherwise consume the current entry while walking the list. In that case the next cursor has to be preserved before the loop body can modify the current entry.

The unfortunate part is that this could not be expressed with the existing list_for_each_entry() interface without changing its calling convention. The _safe() variants had to grow an extra argument for the temporary cursor, and that is why we ended up with a separate family of macros.

But conceptually, the distinction does not have to be exposed as two different iterator families forever. The difference is an implementation
detail: whether the iterator keeps the next/previous cursor before the body runs. This series makes the common list_for_each_entry() iterators do that internally, so the safe and non-safe forms can effectively be folded together, or at least the need for a separate public _safe() interface becomes much weaker.

Quoted 6 lines

There is also a usability issue with the current _safe() interface. The caller is forced to define a temporary cursor outside the macro and pass it in, even though almost all users never use that cursor directly. It is just boilerplate required by the macro implementation. I find that redundant and awkward: the temporary cursor is an internal detail of the iteration, but every caller has to spell it out.

Ah, I think the distinct macro families is that what we want. But the hiding of the parameter can be done inside list_for_each_*_safe(). You can do a treewide change with coccinelle.

Sorry if I didn't get the whole idea from your previous contributions.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Note, even cases that would need a temporary cursor may be switched to new list_for_each_*_safe(), see how PCI macros for iterating over resources are implemented (include/linux/pci.h).

Thanks for your suggestions. I've written a demo based on your feedback. Could you please review it and share your thoughts on this approach?

Have you checked how many users actually need the temporary storage?

In Muchun's reply, he mentioned the following:

There are 9,925 list_for_each_entry() call sites in total. Among them, 9,919 do not require any adaptation, and only 6 need to be refactored:

As for list_for_each_entry_safe(), there are 4,572 callers. 4,550 of them can be directly replaced by the new list_for_each_entry(), while 22 cannot be replaced

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

I only used Coccinelle to scan for list_for_each_entry() call sites, and found the 13 call sites shown in the current patch series, which cover the 6 cases mentioned in Muchun's email. I have not yet run the Coccinelle scan for list_for_each_entry_safe().

If we need to handle all 9,925 list_for_each_entry() call sites or all 4,572 list_for_each_entry_safe() call sites in one go, would such a change be too large? I expect it would affect almost every kernel subsystem.

Quoted 5 lines

If it's done by Linus himself during the day when he prepares -rc1, it's fine. You would need to provide a good justification for the change, though.

But in the above statistics the 4572 vs 4550, so the first step is to investigate why temporary cursor is used in those 22 cases and what we can do to avoid that.

Here is one example: in shmem_unuse() in mm/shmem.c, list_for_each_entry_safe() is used. In this case, the caller releases shmem_swaplist_lock inside the loop. During that window, the list may be modified, and the previously saved next may become stale. Therefore, next needs to be recomputed so that subsequent iteration is based on the latest list state.

This leads to two possible approaches:

1. Change list_for_each_entry_safe(pos, n, head, member) directly to list_for_each_entry_safe(pos, head, member). If we do this, the case above would need to be converted to an open-coded form.

2. Support both forms, list_for_each_entry_safe(pos, n, head, member) and list_for_each_entry_safe(pos, head, member), as described in the link below.
https://lore.kernel.org/all/[email protected]/

Do you have any other thoughts on this?

Quoted 4 lines
Quoted 4 lines

I wonder whether it would be better to first provide the necessary compatibility APIs, and then let each subsystem owner update their code as appropriate. That would make the impact more controlled, similar to how the current folio replacement of page is being handled.

Signature · 2 lines
Thanks
Kaitao Cheng
#39 lore raw
In reply to Andy Shevchenko
Quoted 19 lines · Andy Shevchenko
Quoted 4 lines · Christian König
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

As far as I can see that is an extremely bad idea.

The distinction between the use cases of 'iterating the list' and 'iterating the list while you modify it' is completely intentional.

I agree with this point. It is very reasonable for list_for_each_entry() to be used only for 'iterating the list'. In practice, however, we do not have an effective way to enforce that rule for users, whereas the distinction between bool and int can be enforced by the compiler. The 13 patches in the current series are all real examples where users modify the list while using list_for_each_entry(). Is a rule that cannot actually be enforced reasonable? This is just my humble opinion, and I am raising it here only for discussion.

Quoted 2 lines

What I meant is to keep the name, just drop the parameter (make it hidden and being defined inside list_for_each_*_safe() cases).

I agree with this approach, but the specific details still need to be settled, including the issue described in the link below.

https://lore.kernel.org/all/[email protected]/

Of course, there is also the suffix-renaming issue raised by Christian.

Quoted 11 lines
Quoted 10 lines

See the bool type can be implemented by int as well, but it is just a different use case.

Quoted 6 lines
Quoted 5 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

Signature · 2 lines
Thanks
Kaitao Cheng
#38 lore raw
In reply to Christian König
Quoted 6 lines · Christian König
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Christian König
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian König
Quoted 1 line · Kaitao Cheng

在 2026/6/10 16:07, Christian König 写道:

...

Quoted 25 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
Quoted 8 lines

Should we revert to v1, or keep list_for_each_entry() and list_for_each_entry_safe() as they are, close this thread, and make no changes?

Link to v1:
https://lore.kernel.org/all/[email protected]/

Or do you have any better suggestions?

v1 looks perfectly reasonable to me.

But why not just hiding that once for all (in case they don't use the temporary iterator)? Easy to automate, robust — everyone is happy?

Quoted 7 lines
Quoted 4 lines

As far as I can see that is an extremely bad idea.

The distinction between the use cases of 'iterating the list' and 'iterating the list while you modify it' is completely intentional.

What I meant is to keep the name, just drop the parameter (make it hidden and being defined inside list_for_each_*_safe() cases).

Ah, sorry I was still thinking the suggestion is to merge list_for_each_entry() and list_for_each_entry_safe().

Quoted 8 lines

If the modification is done all at once or in steps doesn't really matter for me as long as the patch can be re-created reproducible.

But I'm wondering if we couldn't improve the name at the same time. The _safe() postfix has caused tons of confusion where especially beginners thought that it is a thread-safe variant, which it clearly isn't.

The _mutable() postfix sounds like a much better description to what happens here.

I see, no objections from my side, but with the new name we don't need to have treewide change, the downside that one should undertake this to finish the job, otherwise we will have _safe() and _mutable() for a long time.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

See the bool type can be implemented by int as well, but it is just a different use case.

Quoted 5 lines
Quoted 5 lines

You should just include some patches in the same patch set to actually use the new macros.

If you modify the files under drivers/dma-buf or drivers/gpu/drm/amd to use the new macro I'm happy to review that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v2 02/14] firewire: core: Open-code topology list walk

In reply to Kaitao Cheng

Hi,

Quoted 7 lines · Kaitao Cheng

From: Kaitao Cheng <[email protected]>

A later change will make list_for_each_entry() cache the next element before entering the loop body. for_each_fw_node() intentionally appends newly discovered child nodes to the temporary walk list while the list is being traversed.

Quoted 8 lines

Keep the loop open-coded so the next node is looked up only after children have been appended. This preserves the current breadth-first traversal semantics and prepares the code for the list iterator update.

---
 drivers/firewire/core-topology.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Thanks for the patch.

Last September I've realized the issue but not solved yet[1]. A pointer array would be another candidate to store the found nodes, since IEEE 1394 bus a restriction about the maximum number of nodes up to 256. But It is too large if put in kernel stack, while it is slightly difficult to keep it in heap dynamically since the function is called under holding spinning lock.

Anyway, there is no objection to your change. Let me apply it to for-next branch so that your further work goes well with no blocks locating in this subsystem.

[1] https://social.kernel.org/notice/AyDqvLkpwUvI5eyokK

Thanks

Takashi Sakamoto