Commit 89e8f601 authored by Hugh Dickins's avatar Hugh Dickins Committed by Euler
Browse files

mm: shmem: fix ShmemHugePages at swapout

mainline inclusion
from mainline-v6.12-rc1
commit dad2dc9c92e0f93f33cebcb0595b8daa3d57473f
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBGFBA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=dad2dc9c92e0f93f33cebcb0595b8daa3d57473f

--------------------------------

/proc/meminfo ShmemHugePages has been showing overlarge amounts (more than
Shmem) after swapping out THPs: we forgot to update NR_SHMEM_THPS.

Add shmem_update_stats(), to avoid repetition, and risk of making that
mistake again: the call from shmem_delete_from_page_cache() is the bugfix;
the call from shmem_replace_folio() is reassuring, but not really a bugfix
(replace corrects misplaced swapin readahead, but huge swapin readahead
would be a mistake).

Link: https://lkml.kernel.org/r/5ba477c8-a569-70b5-923e-09ab221af45b@google.com


Fixes: 809bc86517cc ("mm: shmem: support large folio swap out")
Signed-off-by: default avatarHugh Dickins <hughd@google.com>
Reviewed-by: default avatarShakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: default avatarYosry Ahmed <yosryahmed@google.com>
Reviewed-by: default avatarBaolin Wang <baolin.wang@linux.alibaba.com>
Tested-by: default avatarBaolin Wang <baolin.wang@linux.alibaba.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Conflicts:
    mm/shmem.c
[Context conflicts in shmem.c due to miss commit 7aad25b4b47e]
Signed-off-by: default avatarWang Lian <dev01404@linx-info.com>
---
parent 6ac04fc4
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -773,6 +773,14 @@ static bool shmem_huge_global_enabled(struct inode *inode, pgoff_t index,
}
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

static void shmem_update_stats(struct folio *folio, int nr_pages)
{
	if (folio_test_pmd_mappable(folio))
		__lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr_pages);
	__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages);
	__lruvec_stat_mod_folio(folio, NR_SHMEM, nr_pages);
}

/*
 * Somewhat like filemap_add_folio, but error if expected item has gone.
 */
@@ -807,10 +815,7 @@ static int shmem_add_to_page_cache(struct folio *folio,
		xas_store(&xas, folio);
		if (xas_error(&xas))
			goto unlock;
		if (folio_test_pmd_mappable(folio))
			__lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr);
		__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr);
		__lruvec_stat_mod_folio(folio, NR_SHMEM, nr);
		shmem_update_stats(folio, nr);
		shmem_reliable_folio_add(folio, nr);
		mapping->nrpages += nr;
unlock:
@@ -839,8 +844,7 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap)
	error = shmem_replace_entry(mapping, folio->index, folio, radswap);
	folio->mapping = NULL;
	mapping->nrpages -= nr;
	__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
	__lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
	shmem_update_stats(folio, -nr);
	shmem_reliable_folio_add(folio, -nr);
	xa_unlock_irq(&mapping->i_pages);
	folio_put_refs(folio, nr);
@@ -1966,11 +1970,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
	}
	if (!error) {
		mem_cgroup_replace_folio(old, new);
		__lruvec_stat_mod_folio(new, NR_FILE_PAGES, nr_pages);
		__lruvec_stat_mod_folio(new, NR_SHMEM, nr_pages);
		shmem_update_stats(new, nr_pages);
		shmem_reliable_folio_add(new, nr_pages);
		__lruvec_stat_mod_folio(old, NR_FILE_PAGES, -nr_pages);
		__lruvec_stat_mod_folio(old, NR_SHMEM, -nr_pages);
		shmem_update_stats(old, -nr_pages);
		shmem_reliable_folio_add(old, -nr_pages);
	}
	xa_unlock_irq(&swap_mapping->i_pages);