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

From: Xueyuan Chen

Date: Mon Aug 10 2026 - 10:20:36 EST


On Sat, Aug 8, 2026 at 7:12 AM Barry Song <baohua@xxxxxxxxxx> wrote:
>
> On Thu, Jul 30, 2026 at 8:23 PM Xueyuan Chen <xueyuan.chen21@xxxxxxxxx> wrote:
> >
> > folio_alloc_swap() reports most allocation failures with a generic
> > negative error code. Reclaim cannot tell whether splitting a large folio
> > could make progress or whether there is no backing space at all.
> >
> > Keep the global free swap count and the remaining hierarchical memcg swap
> > margin as separate inputs. The memcg charge path reports only its own
> > margin; folio_alloc_swap() combines the two layers when classifying an
> > allocation failure.
> >
> > Return -E2BIG for large folios when a smaller allocation might still fit,
> > -ENOSPC when no global swap space is available, and -ENOMEM when the
> > failure is not helped by splitting.
> >
> > For early large-folio rejections, check global and memcg swap availability
> > instead of returning -E2BIG unconditionally. On a memcg charge failure,
> > swap slot allocation has already succeeded, so use the remaining memcg
> > margin to decide whether a smaller charge might fit.
> >
> > This only refines folio_alloc_swap() return codes. The reclaim callers are
> > updated separately.
> >
> [...]
> > +
> > +failed:
> > + if (get_nr_swap_pages() <= 0)
> > + return -ENOSPC;
>
> I wonder if we can do this earlier when swap is full or disabled.
> We could exit immediately and avoid trying many redundant paths.
> Have you tried this?
>

Hi Barry,

Regarding moving the check earlier, I currently have two concerns:
1. It would add an extra get_nr_swap_pages() atomic read to every
successful allocation path.
2. If the failure-time check is removed, the earlier snapshot could
become stale while the allocation is attempted. The failure path
could then return -E2BIG even if global swap has since been exhausted.

Thanks,
Xueyuan

> > + if (swap_margin <= 0)
> > + return -ENOMEM;
> > +
> > + return order ? -E2BIG : -ENOMEM;
>
> Best Regards
> Barry