Re: [PATCH v2 04/11] mm, swap: add support for stable large allocation in swap cache directly
From: Kairui Song
Date: Thu Apr 16 2026 - 23:20:18 EST
On Fri, Apr 17, 2026 at 2:38 AM Kairui Song via B4 Relay
<devnull+kasong.tencent.com@xxxxxxxxxx> wrote:
> +/*
> + * Try to allocate a folio of given order in the swap cache.
> + *
> + * This helper resolves the potential races of swap allocation
> + * and prepares a folio to be used for swap IO. May return following
> + * value:
> + *
> + * -ENOMEM / -EBUSY: Order is too large or in conflict with sub slot,
> + * caller should shrink the order and retry.
> + * -ENOENT / -EEXIST: Target swap entry is unavailable or already cached,
> + * caller should abort or try use that folio instead.
> + */
> +static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
> + swp_entry_t targ_entry, gfp_t gfp,
> + unsigned int order, struct vm_fault *vmf,
> + struct mempolicy *mpol, pgoff_t ilx)
> +{
> + int err;
> + swp_entry_t entry;
> + struct folio *folio;
> + void *shadow = NULL;
> + unsigned long address, nr_pages = 1 << order;
> + struct vm_area_struct *vma = vmf ? vmf->vma : NULL;
> +
> + entry.val = round_down(targ_entry.val, nr_pages);
> +
> + /* Check if the slot and range are available, skip allocation if not */
> + spin_lock(&ci->lock);
> + err = __swap_cache_add_check(ci, targ_entry, nr_pages, NULL);
> + spin_unlock(&ci->lock);
> + if (unlikely(err))
> + return ERR_PTR(err);
> +
> + /*
> + * Limit THP gfp. The limitation is a no-op for typical
> + * GFP_HIGHUSER_MOVABLE but matters for shmem.
> + */
> + if (order)
> + gfp = thp_limit_gfp_mask(vma_thp_gfp_mask(vma), gfp);
> +
> + if (mpol) {
> + folio = folio_alloc_mpol(gfp, order, mpol, ilx, numa_node_id());
> + } else if (vmf) {
> + address = round_down(vmf->address, PAGE_SIZE << order);
> + folio = vma_alloc_folio(gfp, order, vmf->vma, address);
> + } else {
> + WARN_ON_ONCE(1);
> + return ERR_PTR(-EINVAL);
> + }
Checking sashiko's review, most are false positives but this part need
an update indeed, this part should be:
if (mpol || !vmf) {
folio = folio_alloc_mpol(gfp, order, mpol, ilx, numa_node_id());
} else {
address = round_down(vmf->address, PAGE_SIZE << order);
folio = vma_alloc_folio(gfp, order, vmf->vma, address);
}