FAILED: patch "[PATCH] khugepaged: fix readahead lock inversion in collapse_file()" failed to apply to 5.15-stable tree
Subsystems (3)
Sasha Levin
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x eadce52a9ef657d561b3e76c9b5f178bd99735f5
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From eadce52a9ef657d561b3e76c9b5f178bd99735f5 Mon Sep 17 00:00:00 2001
From: Lance Yang <[email protected]>
Date: Mon, 26 Oct 2026 23:20:13 +0800
Subject: [PATCH] khugepaged: fix readahead lock inversion in collapse_file()
collapse_file() can call page_cache_sync_readahead() while still holding source folios locked. If truncate holds mapping->invalidate_lock exclusively and waits for one of those folios, readahead waits for invalidate_lock and neither side can make progress.
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once. Doing readahead after rollback avoids an extra pass when all folios are already present. If another miss occurs on retry, give up rather than keep retrying while truncate or reclaim removes folios.
Keep page_cache_sync_readahead() and wait for read I/O with filemap_lock_folio(), releasing each folio before moving to the next. The existing uptodate check during collapse still handles read errors.
Shmem uses shmem_get_folio() for swapin and fallocated folios without taking mapping->invalidate_lock, so it is unaffected.
--- mm/khugepaged.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-)
mm/khugepaged.c +33 -11
@@ -2240,6 +2240,8 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );enum scan_result result =SCAN_SUCCEED ;int nr_none =0 ; +bool do_readahead = false; +bool readahead_done = false;/* * MADV_COLLAPSE ignores shmem huge config, so do not check shmem
@@ -2257,6 +2259,10 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,mapping_set_update (&xas, mapping); +retry: + result =SCAN_SUCCEED ; + do_readahead = false; +xas_set_order (&xas, start,HPAGE_PMD_ORDER );__folio_set_locked (new_folio);if (is_shmem)__folio_set_swapbacked (new_folio);
@@ -2321,17 +2327,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, } }else {/* !is_shmem */ if (!folio ||xa_is_value (folio)) { -xas_unlock_irq (&xas); -page_cache_sync_readahead (mapping, &file->f_ra, - file, index, - end - index); -/* drain lru cache to help folio_isolate_lru() */ -lru_add_drain (); - folio =filemap_lock_folio (mapping, index); -if (IS_ERR (folio)) { - result =SCAN_FAIL ; - goto xa_unlocked; - } + do_readahead = !readahead_done; + result =SCAN_FAIL ; + goto xa_locked; }else if (folio_test_dirty (folio)) {/* * This page is dirty because it hasn't
@@ -2654,6 +2652,30 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, new_folio->mapping =NULL ;folio_unlock (new_folio); + +/* Readahead may take invalidate_lock; source folios are unlocked. */ +if (do_readahead) { +for (index = start; index < end;) { + folio =filemap_lock_folio (mapping, index); +if (IS_ERR (folio)) { +page_cache_sync_readahead (mapping, &file->f_ra, + file, index, end - index); + folio =filemap_lock_folio (mapping, index); +if (IS_ERR (folio)) { + result =SCAN_FAIL ; + goto out_put; + } + } + index =folio_next_index (folio); +folio_unlock (folio); +folio_put (folio); + } +/* Drain the local LRU batch before retrying isolation. */ +lru_add_drain (); + readahead_done = true; + goto retry; + } +out_put:folio_put (new_folio); out:VM_BUG_ON (!list_empty (&pagelist));
Signature · 1 line
2.53.0
Lance Yang
reviewerPATCH 1/1 khugepaged: fix readahead lock inversion in collapse_file()
collapse_file() can call page_cache_sync_readahead() while still holding source folios locked. If truncate holds mapping->invalidate_lock exclusively and waits for one of those folios, readahead waits for invalidate_lock and neither side can make progress.
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once. Doing readahead after rollback avoids an extra pass when all folios are already present. If another miss occurs on retry, give up rather than keep retrying while truncate or reclaim removes folios.
Keep page_cache_sync_readahead() and wait for read I/O with filemap_lock_folio(), releasing each folio before moving to the next. The existing uptodate check during collapse still handles read errors.
Shmem uses shmem_get_folio() for swapin and fallocated folios without taking mapping->invalidate_lock, so it is unaffected.
(cherry picked from commit eadce52a9ef657d561b3e76c9b5f178bd99735f5)
---
Backport to 5.15:
- Use find_lock_page(), unlock_page() and put_page() for the existing page-based collapse code.
mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-)
mm/khugepaged.c +32 -11
@@ -1701,6 +1701,8 @@ static void collapse_file(struct mm_struct *mm,XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );int nr_none =0 , result =SCAN_SUCCEED ;bool is_shmem =shmem_file (file); +bool do_readahead = false; +bool readahead_done = false;int nr;VM_BUG_ON (!IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) && !is_shmem);
@@ -1721,6 +1723,10 @@ static void collapse_file(struct mm_struct *mm, }count_memcg_page_event (new_page,THP_COLLAPSE_ALLOC ); +retry: + result =SCAN_SUCCEED ; + do_readahead = false; +xas_set_order (&xas, start,HPAGE_PMD_ORDER );/* This will be less messy when we use multi-index entries */ do { xas_lock_irq(&xas);
@@ -1791,17 +1797,9 @@ static void collapse_file(struct mm_struct *mm, } }else {/* !is_shmem */ if (!page ||xa_is_value (page)) { -xas_unlock_irq (&xas); -page_cache_sync_readahead (mapping, &file->f_ra, - file, index, - end - index); -/* drain pagevecs to help isolate_lru_page() */ -lru_add_drain (); - page =find_lock_page (mapping, index); -if (unlikely (page ==NULL )) { - result =SCAN_FAIL ; - goto xa_unlocked; - } + do_readahead = !readahead_done; + result =SCAN_FAIL ; + goto xa_locked; }else if (PageDirty (page)) {/* * khugepaged only works on read-only fd,
@@ -2034,6 +2032,29 @@ static void collapse_file(struct mm_struct *mm, }unlock_page (new_page); + +/* Readahead may take invalidate_lock; source pages are unlocked. */ +if (do_readahead) { +for (index = start; index < end; index++) { +struct page *page =find_lock_page (mapping, index); + +if (!page) { +page_cache_sync_readahead (mapping, &file->f_ra, + file, index, end - index); + page =find_lock_page (mapping, index); +if (!page) { + result =SCAN_FAIL ; + goto out; + } + } +unlock_page (page); +put_page (page); + } +/* Drain the local LRU batch before retrying isolation. */ +lru_add_drain (); + readahead_done = true; + goto retry; + } out:VM_BUG_ON (!list_empty (&pagelist));if (!IS_ERR_OR_NULL (*hpage))
Signature · 1 line
2.49.0
Re: [PATCH 5.15.y] khugepaged: fix readahead lock inversion in collapse_file()
Quoted 30 lines · Lance Yang
collapse_file()can callpage_cache_sync_readahead()while still holding source folios locked. If truncate holdsmapping->invalidate_lockexclusively and waits for one of those folios, readahead waits forinvalidate_lockand neither side can make progress.We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once. Doing readahead after rollback avoids an extra pass when all folios are already present. If another miss occurs on retry, give up rather than keep retrying while truncate or reclaim removes folios.
Keep
page_cache_sync_readahead()and wait for read I/O withfilemap_lock_folio(), releasing each folio before moving to the next. The existing uptodate check during collapse still handles read errors.Shmem uses
shmem_get_folio()for swapin and fallocated folios without takingmapping->invalidate_lock, so it is unaffected.Reported-by [email protected]Fixes 730633f0b7f9 ("mm: Protect operations adding pages to page cache with invalidate_lock")Signed-off-by Lance Yang <[email protected]>Reviewed-by Pedro Falcato <[email protected]>Acked-by Zi Yan <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 5 lines
(cherry picked from commit
eadce52a9ef657d561b3e76c9b5f178bd99735f5)
---
Backport to 5.15:
- Usefind_lock_page(),unlock_page()andput_page()for the existing page-based collapse code.
Does not apply :(
Lance Yang
reviewerPATCH v2 1/1 khugepaged: fix readahead lock inversion in collapse_file()
In reply to Sasha Levincollapse_file() can call page_cache_sync_readahead() while still holding source folios locked. If truncate holds mapping->invalidate_lock exclusively and waits for one of those folios, readahead waits for invalidate_lock and neither side can make progress.
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once. Doing readahead after rollback avoids an extra pass when all folios are already present. If another miss occurs on retry, give up rather than keep retrying while truncate or reclaim removes folios.
Keep page_cache_sync_readahead() and wait for read I/O with filemap_lock_folio(), releasing each folio before moving to the next. The existing uptodate check during collapse still handles read errors.
Shmem uses shmem_get_folio() for swapin and fallocated folios without taking mapping->invalidate_lock, so it is unaffected.
(cherry picked from commit eadce52a9ef657d561b3e76c9b5f178bd99735f5)
---
Backport to 5.15:
- Use find_lock_page(), unlock_page() and put_page() for the existing page-based collapse code.
v1 -> v2:
- Refresh context after the shmem_mapping() update.
- https://lore.kernel.org/all/[email protected]/
mm/khugepaged.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-)
mm/khugepaged.c +32 -11
@@ -1701,6 +1701,8 @@ static void collapse_file(struct mm_struct *mm,LIST_HEAD (pagelist);XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );int nr_none =0 , result =SCAN_SUCCEED ; +bool do_readahead = false; +bool readahead_done = false;int nr;VM_BUG_ON (!IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) && !is_shmem);
@@ -1721,6 +1723,10 @@ static void collapse_file(struct mm_struct *mm, }count_memcg_page_event (new_page,THP_COLLAPSE_ALLOC ); +retry: + result =SCAN_SUCCEED ; + do_readahead = false; +xas_set_order (&xas, start,HPAGE_PMD_ORDER );/* This will be less messy when we use multi-index entries */ do { xas_lock_irq(&xas);
@@ -1791,17 +1797,9 @@ static void collapse_file(struct mm_struct *mm, } }else {/* !is_shmem */ if (!page ||xa_is_value (page)) { -xas_unlock_irq (&xas); -page_cache_sync_readahead (mapping, &file->f_ra, - file, index, - end - index); -/* drain pagevecs to help isolate_lru_page() */ -lru_add_drain (); - page =find_lock_page (mapping, index); -if (unlikely (page ==NULL )) { - result =SCAN_FAIL ; - goto xa_unlocked; - } + do_readahead = !readahead_done; + result =SCAN_FAIL ; + goto xa_locked; }else if (PageDirty (page)) {/* * khugepaged only works on read-only fd,
@@ -2034,6 +2032,29 @@ static void collapse_file(struct mm_struct *mm, }unlock_page (new_page); + +/* Readahead may take invalidate_lock; source pages are unlocked. */ +if (do_readahead) { +for (index = start; index < end; index++) { +struct page *page =find_lock_page (mapping, index); + +if (!page) { +page_cache_sync_readahead (mapping, &file->f_ra, + file, index, end - index); + page =find_lock_page (mapping, index); +if (!page) { + result =SCAN_FAIL ; + goto out; + } + } +unlock_page (page); +put_page (page); + } +/* Drain the local LRU batch before retrying isolation. */ +lru_add_drain (); + readahead_done = true; + goto retry; + } out:VM_BUG_ON (!list_empty (&pagelist));if (!IS_ERR_OR_NULL (*hpage))
Signature · 1 line
2.49.0
Sasha Levin
maintainerRe: [PATCH 5.15.y v2] khugepaged: fix readahead lock inversion in collapse_file()
Quoted 2 lines
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once.
Both the 6.1 and the 5.15 backport retry with the same huge page ("goto retry") after collapse_file() has already inserted it into the page cache. These trees lack a2e17cc2efc7 ("mm/khugepaged: maintain page cache uptodate flag"), which keeps the new page out of the page cache until the collapse succeeds. So a racing lookup can hold a reference to the locked, not-uptodate page and wait on its lock; the rollback unlock_page() can hand it the lock, and the retry then re-locks the page with __SetPageLocked() and sets ->mapping again. Two tasks then hold PG_locked, or a waiter treats the not-uptodate THP as a normal page cache page. This is from reading the code, not reproduced.
Could you respin: either free the huge page and allocate a fresh one before retrying, or do the readahead after the rollback and return SCAN_FAIL without a retry? (5.15 already stopped reusing the huge page when page_count > 1 in 033b5d775511 ("mm/khugepaged: fix filemap page_to_pgoff(page) != offset").)
Signature · 2 lines
Thanks, Sasha
Lance Yang
reviewerRe: [PATCH 5.15.y v2] khugepaged: fix readahead lock inversion in collapse_file()
Quoted 13 lines · Sasha Levin
Quoted 2 lines
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once.
Both the 6.1 and the 5.15 backport retry with the same huge page ("goto retry") after
collapse_file()has already inserted it into the page cache. These trees lacka2e17cc2efc7("mm/khugepaged: maintain page cache uptodate flag"), which keeps the new page out of the page cache until the collapse succeeds. So a racing lookup can hold a reference to the locked, not-uptodate page and wait on its lock; the rollbackunlock_page()can hand it the lock, and the retry then re-locks the page with__SetPageLocked()and sets ->mapping again. Two tasks then holdPG_locked, or a waiter treats the not-uptodate THP as a normal page cache page. This is from reading the code, not reproduced.
Ouch ... I missed that some upstream fixes hadn't made it into 6.1.y or 5.15.y.
On 5.15, v2 also skipped an existing page_count() check on retry :(
Quoted 3 lines
Could you respin: either free the huge page and allocate a fresh one before retrying, or do the readahead after the rollback and return
SCAN_FAILwithout a retry? (5.15 already stopped reusing the
Sure. In v3, we drop our reference to that huge page and retry with a fresh one.
6.1.y:
https://lore.kernel.org/all/[email protected]/
5.15.y:
https://lore.kernel.org/all/[email protected]/
I've tested both backports. Thanks for catching this!
Cheers, Lance
Quoted 3 lines
huge page when
page_count> 1 in033b5d775511("mm/khugepaged: fix filemappage_to_pgoff(page)!= offset").)
Lance Yang
reviewerPATCH v3 1/1 khugepaged: fix readahead lock inversion in collapse_file()
In reply to Sasha Levincollapse_file() can call page_cache_sync_readahead() while still holding source folios locked. If truncate holds mapping->invalidate_lock exclusively and waits for one of those folios, readahead waits for invalidate_lock and neither side can make progress.
We need to release those folio locks before starting readahead. On a page cache miss, roll back first, populate the collapse range, and retry once. Doing readahead after rollback avoids an extra pass when all folios are already present. If another miss occurs on retry, give up rather than keep retrying while truncate or reclaim removes folios.
Keep page_cache_sync_readahead() and wait for read I/O with filemap_lock_folio(), releasing each folio before moving to the next. The existing uptodate check during collapse still handles read errors.
Shmem uses shmem_get_folio() for swapin and fallocated folios without taking mapping->invalidate_lock, so it is unaffected.
(cherry picked from commit eadce52a9ef657d561b3e76c9b5f178bd99735f5)
---
Backport to 5.15:
- Use find_lock_page(), unlock_page() and put_page() for the existing page-based collapse code.
- Drop our reference to the destination huge page before readahead, since a reader may still hold it after rollback. With CONFIG_NUMA disabled, khugepaged_alloc_page() expects a preallocated page. Call khugepaged_prealloc_page() before retrying.
v2 -> v3:
- Allocate a fresh huge page for the retry (per Sasha) - thanks!
- https://lore.kernel.org/all/[email protected]/
v1 -> v2:
- Refresh context after the shmem_mapping() update.
- https://lore.kernel.org/all/[email protected]/
mm/khugepaged.c | 52 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-)
mm/khugepaged.c +41 -11
@@ -1701,6 +1701,8 @@ static void collapse_file(struct mm_struct *mm,LIST_HEAD (pagelist);XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );int nr_none =0 , result =SCAN_SUCCEED ; +bool do_readahead = false; +bool readahead_done = false;int nr;VM_BUG_ON (!IS_ENABLED (CONFIG_READ_ONLY_THP_FOR_FS ) && !is_shmem);
@@ -1709,6 +1711,7 @@ static void collapse_file(struct mm_struct *mm,/* Only allocate from the target node */ gfp =alloc_hugepage_khugepaged_gfpmask () | __GFP_THISNODE; +retry: new_page =khugepaged_alloc_page (hpage, gfp, node);if (!new_page) { result =SCAN_ALLOC_HUGE_PAGE_FAIL ;
@@ -1721,6 +1724,9 @@ static void collapse_file(struct mm_struct *mm, }count_memcg_page_event (new_page,THP_COLLAPSE_ALLOC ); + result =SCAN_SUCCEED ; + do_readahead = false; +xas_set_order (&xas, start,HPAGE_PMD_ORDER );/* This will be less messy when we use multi-index entries */ do { xas_lock_irq(&xas);
@@ -1791,17 +1797,9 @@ static void collapse_file(struct mm_struct *mm, } }else {/* !is_shmem */ if (!page ||xa_is_value (page)) { -xas_unlock_irq (&xas); -page_cache_sync_readahead (mapping, &file->f_ra, - file, index, - end - index); -/* drain pagevecs to help isolate_lru_page() */ -lru_add_drain (); - page =find_lock_page (mapping, index); -if (unlikely (page ==NULL )) { - result =SCAN_FAIL ; - goto xa_unlocked; - } + do_readahead = !readahead_done; + result =SCAN_FAIL ; + goto xa_locked; }else if (PageDirty (page)) {/* * khugepaged only works on read-only fd,
@@ -2034,6 +2032,38 @@ static void collapse_file(struct mm_struct *mm, }unlock_page (new_page); + +/* Readahead may take invalidate_lock; source pages are unlocked. */ +if (do_readahead) { +bool wait = false; + +put_page (new_page); + *hpage =NULL ; + +for (index = start; index < end; index++) { +struct page *page =find_lock_page (mapping, index); + +if (!page) { +page_cache_sync_readahead (mapping, &file->f_ra, + file, index, end - index); + page =find_lock_page (mapping, index); +if (!page) { + result =SCAN_FAIL ; + goto out; + } + } +unlock_page (page); +put_page (page); + } +/* Drain the local LRU batch before retrying isolation. */ +lru_add_drain (); +if (!khugepaged_prealloc_page (hpage, &wait)) { + result =SCAN_ALLOC_HUGE_PAGE_FAIL ; + goto out; + } + readahead_done = true; + goto retry; + } out:VM_BUG_ON (!list_empty (&pagelist));if (!IS_ERR_OR_NULL (*hpage))
Signature · 1 line
2.49.0