Re: [PATCH 11/32] mm/khugepaged: store new folio before adding it to LRU

From: Zi Yan

Date: Mon Nov 30 2026 - 12:12:36 EST


On Sun Nov 29, 2026 at 5:06 AM EST, David Hildenbrand (Arm) wrote:
> From: Chris Mason <mason@xxxxxxxxxx>
>
> collapse_file() adds the new folio to the LRU before adding it to the
> page cache. If SHM_LOCKed, the folio will be added to the unevictable
> LRU. A concurrent SHM_UNLOCK walks the page cache to make all folios
> evictable again, and can miss the new folio:
>
> collapse_file() SHM_UNLOCK
> =============== ==========
> folio_add_lru(new_folio)
> /* unevictable */
> shmem_unlock_mapping()
> /* misses new_folio */
> xas_store(&xas, new_folio)
>
> Consequently, if SHM_UNLOCK races with a collapse in a SHM_LOCKed SysV
> shared memory segment, the new folio can get stranded on the unevictable
> LRU.
>
> Let's improve it by adding the new folio to the LRU only after adding it
> to the page cache.
>
> There is a more fundamental race that must be sorted out to fix this
> completely: check_move_unevictable_folios() skips folios that are not on
> the LRU without taking the lruvec lock, while lru_add() checks evictability
> before marking the folio as being on the LRU. That affects all folios that
> are added to the LRU, and requires a more generic fix.
>
> This has low urgency, as it only strands a folio on the unevictable LRU.
> No need to CC stable.
>
> Fixes: a2e17cc2efc7 ("mm/khugepaged: maintain page cache uptodate flag")
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ david: simplify description and comment ]
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/khugepaged.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>

Makes sense.

Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>



--
Best Regards,
Yan, Zi