Skip to contentSkip to conversation

lib/list_sort: Clean up list_sort() scheduling workarounds

PATCH Applied Kuan-Wei Chiu 5 participants 7 messages Mar 20 – Mar 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover lib/list_sort: Clean up list_sort() scheduling workarounds

Historically, list_sort() included a hack in merge_final() that periodically invoked dummy cmp(priv, b, b) calls when merging highly unbalanced lists. This allowed the caller to invoke cond_resched() within their comparison callbacks to avoid soft lockups.

However, an audit of the kernel tree shows that fs/ubifs/ has been the sole user of this mechanism. For all other generic list_sort() users, this results in wasted function calls and unnecessary overhead in a tight loop.

Recent discussions and code inspection confirmed that the lists being sorted in UBIFS are bounded in size (a few thousand elements at most), and the comparison functions are extremely lightweight. Therefore, UBIFS does not actually need to rely on this mechanism.
---
Changes in v3:
- Abandoned the idea of introducing a new list_sort_nonatomic() API.
- Removed the dummy cmp() hack.
- Dropped the cond_resched() calls from UBIFS.
- Split the changes into a 2-patch series.
- Dropped Zhihao Cheng's Reviewed-by tag due to the fundamental change.

Changes in v2:
- Dropped the u8 count rate-limiter in merge() and merge_final().
- Removed cond_resched() calls from UBIFS comparison callbacks.

v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Kuan-Wei Chiu (2):
ubifs: Remove unnecessary cond_resched() from list_sort() compare lib/list_sort: Remove dummy cmp() calls to speed up merge_final()

 fs/ubifs/gc.c     | 2 --
 fs/ubifs/replay.c | 1 -
 lib/list_sort.c   | 9 ---------
 3 files changed, 12 deletions(-)
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
#1 lore raw

PATCH v3 1/2 ubifs: Remove unnecessary cond_resched() from list_sort() compare

Historically, UBIFS embedded cond_resched() calls inside its list_sort() comparison callbacks (data_nodes_cmp, nondata_nodes_cmp, and replay_entries_cmp) to prevent soft lockups when sorting long lists.

However, further inspection by Richard Weinberger reveals that these compare functions are extremely lightweight and do not perform any blocking MTD I/O. Furthermore, the lists being sorted are strictly bounded in size:
- In the GC case, the list contains at most the number of nodes that fit into a single LEB.
- In the replay case, the list spans across a few LEBs from the UBIFS journal, amounting to at most a few thousand elements.

Since the compare functions are called a few thousand times at most, the overhead of frequent scheduling points is unjustified. Removing the cond_resched() calls simplifies the comparison logic and reduces unnecessary context switch checks during the sort.

---
 fs/ubifs/gc.c     | 2 --
 fs/ubifs/replay.c | 1 -
 2 files changed, 3 deletions(-)
fs/ubifs/gc.c +0 -2
index 0bf08b7755b8..933c79b5cd6b 100644
@@ -109,7 +109,6 @@ static int data_nodes_cmp(void *priv, const struct list_head *a,
 	struct ubifs_info *c = priv;
 	struct ubifs_scan_node *sa, *sb;
 
-	cond_resched();
 	if (a == b)
 		return 0;
 
@@ -153,7 +152,6 @@ static int nondata_nodes_cmp(void *priv, const struct list_head *a,
 	struct ubifs_info *c = priv;
 	struct ubifs_scan_node *sa, *sb;
 
-	cond_resched();
 	if (a == b)
 		return 0;
 
fs/ubifs/replay.c +0 -1
index a9a568f4a868..263045e05cf1 100644
@@ -305,7 +305,6 @@ static int replay_entries_cmp(void *priv, const struct list_head *a,
 	struct ubifs_info *c = priv;
 	struct replay_entry *ra, *rb;
 
-	cond_resched();
 	if (a == b)
 		return 0;
 
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
reviewer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] ubifs: Remove unnecessary cond_resched() from list_sort() compare

Quoted 19 lines · Kuan-Wei Chiu

Historically, UBIFS embedded cond_resched() calls inside its list_sort() comparison callbacks (data_nodes_cmp, nondata_nodes_cmp, and replay_entries_cmp) to prevent soft lockups when sorting long lists.

However, further inspection by Richard Weinberger reveals that these compare functions are extremely lightweight and do not perform any blocking MTD I/O. Furthermore, the lists being sorted are strictly bounded in size:
- In the GC case, the list contains at most the number of nodes that fit into a single LEB.
- In the replay case, the list spans across a few LEBs from the UBIFS journal, amounting to at most a few thousand elements.

Since the compare functions are called a few thousand times at most, the overhead of frequent scheduling points is unjustified. Removing the cond_resched() calls simplifies the comparison logic and reduces unnecessary context switch checks during the sort.

Quoted 5 lines
---
  fs/ubifs/gc.c     | 2 --
  fs/ubifs/replay.c | 1 -
  2 files changed, 3 deletions(-)
Quoted 34 lines
fs/ubifs/gc.c +0 -2
index 0bf08b7755b8..933c79b5cd6b 100644
@@ -109,7 +109,6 @@ static int data_nodes_cmp(void *priv, const struct list_head *a,
  	struct ubifs_info *c = priv;
  	struct ubifs_scan_node *sa, *sb;
 
-	cond_resched();
  	if (a == b)
  		return 0;
 
@@ -153,7 +152,6 @@ static int nondata_nodes_cmp(void *priv, const struct list_head *a,
  	struct ubifs_info *c = priv;
  	struct ubifs_scan_node *sa, *sb;
 
-	cond_resched();
  	if (a == b)
  		return 0;
 
fs/ubifs/replay.c +0 -1
index a9a568f4a868..263045e05cf1 100644
@@ -305,7 +305,6 @@ static int replay_entries_cmp(void *priv, const struct list_head *a,
  	struct ubifs_info *c = priv;
  	struct replay_entry *ra, *rb;
 
-	cond_resched();
  	if (a == b)
  		return 0;
 
maintainer Acked-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] ubifs: Remove unnecessary cond_resched() from list_sort() compare

In reply to Kuan-Wei Chiu

----- Ursprüngliche Mail -----

Quoted 22 lines

Von: "Kuan-Wei Chiu" <[email protected]>
An: "richard" <[email protected]>, "Andrew Morton" <[email protected]>
CC: "chengzhihao1" <[email protected]>, "Christoph Hellwig" <[email protected]>, "jserv" <[email protected]>, "eleanor15x" <[email protected]>, "marscheng" <[email protected]>, "linux-mtd" <[email protected]>, "linux-kernel" <[email protected]>, "Kuan-Wei Chiu" <[email protected]>
Gesendet: Freitag, 20. März 2026 19:09:37
Betreff: [PATCH v3 1/2] ubifs: Remove unnecessary cond_resched() from list_sort() compare

Historically, UBIFS embedded cond_resched() calls inside its list_sort() comparison callbacks (data_nodes_cmp, nondata_nodes_cmp, and replay_entries_cmp) to prevent soft lockups when sorting long lists.

However, further inspection by Richard Weinberger reveals that these compare functions are extremely lightweight and do not perform any blocking MTD I/O. Furthermore, the lists being sorted are strictly bounded in size:
- In the GC case, the list contains at most the number of nodes that fit into a single LEB.
- In the replay case, the list spans across a few LEBs from the UBIFS journal, amounting to at most a few thousand elements.

Quoted 6 lines

Since the compare functions are called a few thousand times at most, the overhead of frequent scheduling points is unjustified. Removing the cond_resched() calls simplifies the comparison logic and reduces unnecessary context switch checks during the sort.

Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#4 lore raw

PATCH v3 2/2 lib/list_sort: Remove dummy cmp() calls to speed up merge_final()

In reply to Kuan-Wei Chiu

Historically, list_sort() implemented a hack in merge_final():

    if (unlikely(!++count))
        cmp(priv, b, b);

This was introduced 16 years ago in commit 835cc0c8477f ("lib: more scalable list_sort()") so that callers could periodically invoke cond_resched() within their comparison functions when merging highly unbalanced lists.

An audit of the kernel tree reveals that fs/ubifs/ was the sole user of this mechanism. Recent discussions and inspections by Richard Weinberger confirm that UBIFS lists are strictly bounded in size (a few thousand elements at most), meaning it does not strictly rely on these dummy callbacks to prevent soft lockups.

For the vast majority of list_sort() users (such as block layer IO schedulers and file systems), this hack results in completely wasted function calls. In the worst-case scenario (merging an already sorted list where 'a' is exhausted quickly), it results in approximately (N/2)/256 unnecessary cmp() invocations.

Remove the dummy cmp(priv, b, b) fallback from merge_final(). This saves unnecessary function calls, avoids branching overhead in the tight loop, and slightly speeds up the final merge step for all generic list_sort() users.

---
 lib/list_sort.c | 9 ---------
 1 file changed, 9 deletions(-)
lib/list_sort.c +0 -9
index a310ecb7ccc0..e8ff17c9b2d0 100644
@@ -76,15 +76,6 @@ static void merge_final(void *priv, list_cmp_func_t cmp, struct list_head *head,
 	/* Finish linking remainder of list b on to tail */
 	tail->next = b;
 	do {
-		/*
-		 * If the merge is highly unbalanced (e.g. the input is
-		 * already sorted), this loop may run many iterations.
-		 * Continue callbacks to the client even though no
-		 * element comparison is needed, so the client's cmp()
-		 * routine can invoke cond_resched() periodically.
-		 */
-		if (unlikely(!++count))
-			cmp(priv, b, b);
 		b->prev = tail;
 		tail = b;
 		b = b->next;
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
maintainer
#6 lore raw
In reply to Kuan-Wei Chiu
Quoted 10 lines · On Fri, 20 Mar 2026 18:09:36 +0000 Kuan-Wei Chiu <[email protected]> wrote:

Historically, list_sort() included a hack in merge_final() that periodically invoked dummy cmp(priv, b, b) calls when merging highly unbalanced lists. This allowed the caller to invoke cond_resched() within their comparison callbacks to avoid soft lockups.

However, an audit of the kernel tree shows that fs/ubifs/ has been the sole user of this mechanism. For all other generic list_sort() users, this results in wasted function calls and unnecessary overhead in a tight loop.

Quoted 4 lines

Recent discussions and code inspection confirmed that the lists being sorted in UBIFS are bounded in size (a few thousand elements at most), and the comparison functions are extremely lightweight. Therefore, UBIFS does not actually need to rely on this mechanism.

Thanks. AI review found a now-unused local, which I'll fix.

https://sashiko.dev/#/patchset/[email protected]