[PATCH RFC 8/8] mm/huge_memory: walk the folios reversely for the release loop
From: Kairui Song via B4 Relay
Date: Fri Nov 13 2026 - 16:27:26 EST
From: Kairui Song <kasong@xxxxxxxxxxx>
The release loop unlocks and puts every after-split folio except the one
holding @lock_at. Walk it backwards with folio_prev() instead of
forwards with folio_next().
This also helps avoid a for loop pitfall mentioned by Zi Yan [1]:
folio_next() needs the folio it is given to stay alive until the
increment, folio_prev() does not. So the cached next pointer can go, and
the folio that was just put is never read again.
Same folios visited, same work on each, only the direction changes.
Link: https://lore.kernel.org/r/DKJSFCLP967N.YBR4DNK1NM2N@xxxxxxxxxx [1]
Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
---
mm/huge_memory.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2ff201a5e5f3..bb09465a3948 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4366,7 +4366,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
const bool is_anon = folio_test_anon(folio);
const bool is_swapcache = folio_test_swapcache(folio);
int old_order = folio_order(folio);
- struct folio *new_folio, *next;
+ struct folio *new_folio, *lock_folio;
int ret;
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -4399,9 +4399,10 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
* Unlock all after-split folios except the one containing
* @lock_at page. If @folio is not split, it will be kept locked.
*/
- for (new_folio = folio; new_folio != end_folio; new_folio = next) {
- next = folio_next(new_folio);
- if (new_folio == page_folio(lock_at))
+ lock_folio = page_folio(lock_at);
+ for (new_folio = folio_prev(end_folio); new_folio != folio;
+ new_folio = folio_prev(new_folio)) {
+ if (new_folio == lock_folio)
continue;
/*
--
2.56.1