Skip to content

Commit c262ffd

Browse files
ryncsnakpm00
authored andcommitted
mm/shmem, swap: tidy up THP swapin checks
Move all THP swapin related checks under CONFIG_TRANSPARENT_HUGEPAGE, so they will be trimmed off by the compiler if not needed. And add a WARN if shmem sees a order > 0 entry when CONFIG_TRANSPARENT_HUGEPAGE is disabled, that should never happen unless things went very wrong. There should be no observable feature change except the new added WARN. Link: https://lkml.kernel.org/r/20250728075306.12704-4-ryncsn@gmail.com Signed-off-by: Kairui Song <kasong@tencent.com> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Baoquan He <bhe@redhat.com> Cc: Barry Song <baohua@kernel.org> Cc: Chris Li <chrisl@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Hugh Dickins <hughd@google.com> Cc: Kemeng Shi <shikemeng@huaweicloud.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Nhat Pham <nphamcs@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 0cfc0e7 commit c262ffd

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

mm/shmem.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,26 +2017,38 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode,
20172017
swp_entry_t entry, int order, gfp_t gfp)
20182018
{
20192019
struct shmem_inode_info *info = SHMEM_I(inode);
2020+
int nr_pages = 1 << order;
20202021
struct folio *new;
20212022
void *shadow;
2022-
int nr_pages;
20232023

20242024
/*
20252025
* We have arrived here because our zones are constrained, so don't
20262026
* limit chance of success with further cpuset and node constraints.
20272027
*/
20282028
gfp &= ~GFP_CONSTRAINT_MASK;
2029-
if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && order > 0) {
2030-
gfp_t huge_gfp = vma_thp_gfp_mask(vma);
2029+
if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
2030+
if (WARN_ON_ONCE(order))
2031+
return ERR_PTR(-EINVAL);
2032+
} else if (order) {
2033+
/*
2034+
* If uffd is active for the vma, we need per-page fault
2035+
* fidelity to maintain the uffd semantics, then fallback
2036+
* to swapin order-0 folio, as well as for zswap case.
2037+
* Any existing sub folio in the swap cache also blocks
2038+
* mTHP swapin.
2039+
*/
2040+
if ((vma && unlikely(userfaultfd_armed(vma))) ||
2041+
!zswap_never_enabled() ||
2042+
non_swapcache_batch(entry, nr_pages) != nr_pages)
2043+
return ERR_PTR(-EINVAL);
20312044

2032-
gfp = limit_gfp_mask(huge_gfp, gfp);
2045+
gfp = limit_gfp_mask(vma_thp_gfp_mask(vma), gfp);
20332046
}
20342047

20352048
new = shmem_alloc_folio(gfp, order, info, index);
20362049
if (!new)
20372050
return ERR_PTR(-ENOMEM);
20382051

2039-
nr_pages = folio_nr_pages(new);
20402052
if (mem_cgroup_swapin_charge_folio(new, vma ? vma->vm_mm : NULL,
20412053
gfp, entry)) {
20422054
folio_put(new);
@@ -2320,30 +2332,15 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
23202332
/* Look it up and read it in.. */
23212333
folio = swap_cache_get_folio(swap, NULL, 0);
23222334
if (!folio) {
2323-
int nr_pages = 1 << order;
2324-
bool fallback_order0 = false;
2325-
23262335
/* Or update major stats only when swapin succeeds?? */
23272336
if (fault_type) {
23282337
*fault_type |= VM_FAULT_MAJOR;
23292338
count_vm_event(PGMAJFAULT);
23302339
count_memcg_event_mm(fault_mm, PGMAJFAULT);
23312340
}
23322341

2333-
/*
2334-
* If uffd is active for the vma, we need per-page fault
2335-
* fidelity to maintain the uffd semantics, then fallback
2336-
* to swapin order-0 folio, as well as for zswap case.
2337-
* Any existing sub folio in the swap cache also blocks
2338-
* mTHP swapin.
2339-
*/
2340-
if (order > 0 && ((vma && unlikely(userfaultfd_armed(vma))) ||
2341-
!zswap_never_enabled() ||
2342-
non_swapcache_batch(swap, nr_pages) != nr_pages))
2343-
fallback_order0 = true;
2344-
23452342
/* Skip swapcache for synchronous device. */
2346-
if (!fallback_order0 && data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
2343+
if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
23472344
folio = shmem_swap_alloc_folio(inode, vma, index, swap, order, gfp);
23482345
if (!IS_ERR(folio)) {
23492346
skip_swapcache = true;

0 commit comments

Comments
 (0)