Skip to content

Commit dad2dc9

Browse files
Hugh Dickinsakpm00
authored andcommitted
mm: shmem: fix ShmemHugePages at swapout
/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: 809bc86 ("mm: shmem: support large folio swap out") Signed-off-by: Hugh Dickins <hughd@google.com> Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com> Tested-by: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7782e3b commit dad2dc9

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

mm/shmem.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,14 @@ static bool shmem_huge_global_enabled(struct inode *inode, pgoff_t index,
787787
}
788788
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
789789

790+
static void shmem_update_stats(struct folio *folio, int nr_pages)
791+
{
792+
if (folio_test_pmd_mappable(folio))
793+
__lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr_pages);
794+
__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages);
795+
__lruvec_stat_mod_folio(folio, NR_SHMEM, nr_pages);
796+
}
797+
790798
/*
791799
* Somewhat like filemap_add_folio, but error if expected item has gone.
792800
*/
@@ -821,10 +829,7 @@ static int shmem_add_to_page_cache(struct folio *folio,
821829
xas_store(&xas, folio);
822830
if (xas_error(&xas))
823831
goto unlock;
824-
if (folio_test_pmd_mappable(folio))
825-
__lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr);
826-
__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr);
827-
__lruvec_stat_mod_folio(folio, NR_SHMEM, nr);
832+
shmem_update_stats(folio, nr);
828833
mapping->nrpages += nr;
829834
unlock:
830835
xas_unlock_irq(&xas);
@@ -852,8 +857,7 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap)
852857
error = shmem_replace_entry(mapping, folio->index, folio, radswap);
853858
folio->mapping = NULL;
854859
mapping->nrpages -= nr;
855-
__lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
856-
__lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
860+
shmem_update_stats(folio, -nr);
857861
xa_unlock_irq(&mapping->i_pages);
858862
folio_put_refs(folio, nr);
859863
BUG_ON(error);
@@ -1969,10 +1973,8 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
19691973
}
19701974
if (!error) {
19711975
mem_cgroup_replace_folio(old, new);
1972-
__lruvec_stat_mod_folio(new, NR_FILE_PAGES, nr_pages);
1973-
__lruvec_stat_mod_folio(new, NR_SHMEM, nr_pages);
1974-
__lruvec_stat_mod_folio(old, NR_FILE_PAGES, -nr_pages);
1975-
__lruvec_stat_mod_folio(old, NR_SHMEM, -nr_pages);
1976+
shmem_update_stats(new, nr_pages);
1977+
shmem_update_stats(old, -nr_pages);
19761978
}
19771979
xa_unlock_irq(&swap_mapping->i_pages);
19781980

0 commit comments

Comments
 (0)