Re: [PATCH 11/28] mm, swap: clean up and consolidate helper for mTHP swapin check
From: Barry Song
Date: Mon May 19 2025 - 03:11:54 EST
> From: Kairui Song <kasong@xxxxxxxxxxx>
> -static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages)
> +static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep,
> + unsigned long addr, unsigned int nr_pages)
> + if (unlikely(addr < max(addr & PMD_MASK, vmf->vma->vm_start) ||
> + addr_end > pmd_addr_end(addr, vmf->vma->vm_end)))
> @@ -4731,27 +4732,18 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> page_idx = 0;
> address = vmf->address;
> ptep = vmf->pte;
> +
> if (folio_test_large(folio) && folio_test_swapcache(folio)) {
> - int nr = folio_nr_pages(folio);
> + unsigned long nr = folio_nr_pages(folio);
> unsigned long idx = folio_page_idx(folio, page);
> - unsigned long folio_start = address - idx * PAGE_SIZE;
> - unsigned long folio_end = folio_start + nr * PAGE_SIZE;
> - pte_t *folio_ptep;
> - pte_t folio_pte;
> + unsigned long folio_address = address - idx * PAGE_SIZE;
> + pte_t *folio_ptep = vmf->pte - idx;
>
> - if (unlikely(folio_start < max(address & PMD_MASK, vma->vm_start)))
> - goto check_folio;
We are handling a corner case a large folio is remapped to an unaligned address.
For example,
A 64KiB mTHP at address: XGB + 2MB +4KB,
Its start address will be XGB + 2MB - 60KB which is another PMD.
The previous code will return false; now your can_swapin_thp() will return true
as you are using XGB + 2MB - 60KB as the argument "addr" in can_swapin_thp().
> - if (unlikely(folio_end > pmd_addr_end(address, vma->vm_end)))
> - goto check_folio;
> -
> - folio_ptep = vmf->pte - idx;
> - folio_pte = ptep_get(folio_ptep);
> - if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) ||
> - swap_pte_batch(folio_ptep, nr, folio_pte) != nr)
> + if (!can_swapin_thp(vmf, folio_ptep, folio_address, nr))
Thanks
Barry