Re: [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin

From: Pedro Falcato

Date: Fri Nov 27 2026 - 17:15:01 EST


On Fri, Nov 27, 2026 at 07:45:41PM +0000, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@xxxxxxxxxx>
>
> MADV_WILLNEED on memory in swap can OOM kill a task when the memcg that
> the swap entries are charged to is at its limit.
>
> The hint is only a prefetch, so it has to give up on a page when memory
> is short. But swapin_walk_pmd_entry() and shmem_swapin_range() call
> read_swap_cache_async() with GFP_HIGHUSER_MOVABLE and with the gfp mask
> of the mapping, so the allocation and the memcg charge can both go as
> far as the OOM killer.
>
> The charge goes to the memcg of the swap entry, which need not be the
> caller's.
>
> The fix used here is to allocate with the gfp mask of readahead, and to
> flush the block plug when a page fails. madvise_do_behavior() plugs the
> whole walk, so the swap writes of reclaim can sit in that plug. Their
> folios stay under writeback, and with no retries the rest of the range
> can fail as well.

How about we don't do that? Something something separation of concerns.
If we want this functionality (and we very well might), I would suggest
we do this in reclaim code itself. Something like

blk_finish_plug_or_flush().

Because I really don't want to set the precedent of making every direct reclaim
caller aware of block plugs.

Either that, or we let it fail.

>
> The hint can now skip pages when memory is short, whether in a memcg at
> its limit or on the system as a whole, since neither the allocation nor
> the memcg charge is retried any longer.
>
> This applies in particular where reclaim has to write to swap to make
> room. Skipped pages stay in swap until a fault reads them.
>
> Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation")
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ ljs: commit message tweak ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>

Aside from the above:
Reviewed-by: Pedro Falcato <pfalcato@xxxxxxx>

--
Pedro