Re: [RFC PATCH v3 2/4] mm: distinguish large folio swap allocation failures

From: Xueyuan Chen

Date: Thu Jul 23 2026 - 11:35:15 EST



On Wed, Jul 22, 2026 at 01:13:09AM +0900, Youngjun Park wrote:
>On Fri, Jul 17, 2026 at 08:25:12PM +0800, Xueyuan Chen wrote:
>...
>> local_lock(&percpu_swap_cluster.lock);
>> if (!swap_alloc_fast(folio))
>> @@ -1730,11 +1734,16 @@ int folio_alloc_swap(struct folio *folio)
>> }
>> /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
>> - if (unlikely(mem_cgroup_try_charge_swap(folio, folio->swap)))
>> + if (unlikely(mem_cgroup_try_charge_swap(folio, folio->swap,
>> + &nr_swap_pages))) {
>> swap_cache_del_folio(folio);
>
>IMHO,
>If we get here the allocation succeeded and only the charge failed, so
>we already know global swap space exists.
>(And also swap_cache_del_folio right above even puts the slots back,
>so nr_swap_pages goes up again)
>
>Could we just take the margin and decide -E2BIG or -ENOMEM from that?
>
>Youngjun
>

Thank you for the review, Youngjun. You're right.

When the memcg charge fails at this point, the swap slot allocation has
already succeeded. After swap_cache_del_folio() releases those slots,
re-reading the global free swap count may also include the slots that
were just returned, so it is not useful for classifying the charge
failure.

I changed the charge failure path to use the remaining memcg margin
alone:

if (unlikely(mem_cgroup_try_charge_swap(folio, folio->swap,
&swap_margin))) {
swap_cache_del_folio(folio);
return order && swap_margin > 0 ? -E2BIG : -ENOMEM;
}

The global free swap count is now considered separately when classifying
swap allocation failures and the early rejection paths.

Thanks for catching this. I will include the change in the next version.

Thanks
Xueyuan