Re: [PATCH v2 03/13] mm: cachestat: restart the walk after each entry
From: Nhat Pham
Date: Sat Nov 14 2026 - 13:08:55 EST
On Fri, Nov 13, 2026 at 2:46 PM Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx> wrote:
>
> From: Chris Mason <mason@xxxxxxxxxx>
>
> cachestat() can leave cached pages out of its counts. This happens if
> the entry that the call has just counted is replaced by a larger folio
> that starts in the same XArray slot.
>
> The issue is that filemap_cachestat() walks the page cache under the RCU
> read lock, so racing concurrent collapse/split is possible.
>
> Thus, an entry can be replaced immediately after the walk has returned
> it. When that happens, the next xas_find() skips the sibling entries that
> point to the slot of that entry, since ordinarily they belong to that
> entry.
>
> However a concurrent collapse_file() or __filemap_add_folio() (when
> replacing a shadow entry) can store a larger folio in the slot, in which
> case the sibling entries belong to the new folio and the walk still skips
> them.
>
> Consider:
>
> cachestat collapse
> ========= ========
> count a 64-page folio in slot 8
> store a 512-page folio in slot 8,
> with sibling entries in slots 9-15
> xas_find() starts at slot 8
> and skips slots 9-15
> /* 448 pages are never counted */
>
> Also consider:
>
> filemap_cachestat() __filemap_add_folio()
> =================== =====================
> counts the shadow entry in slot 0
> slot 0: a folio of 32 pages
> slots 1-31: siblings of slot 0
> xas_find() skips slots 1-31
> /* 31 pages are never counted */
>
> Essentially the fact that large folios might be present means we cannot
> safely continue the XArray walk, and must reset the iterator each time.
>
> Fix this by resetting the walk in each loop. This incurs a performance
> penalty, but given that only the RCU lock is held here, it can't be
> avoided.
>
> Fixes: cf264e1329fb ("cachestat: implement cachestat syscall")
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ ljs: squashed 2 commits, fixed up comment in code, and commit message ]
> Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> Reviewed-by: Johannes Weiner <hannes@xxxxxxxxxxx>
> Reviewed-by: Pedro Falcato <pfalcato@xxxxxxx>
> Acked-by: Usama Arif <usama.arif@xxxxxxxxx>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
Acked-by: Nhat Pham <nphamcs@xxxxxxxxx>