mm: memcontrol: convert NR_ANON_THPS account to pages
authorMuchun Song <songmuchun@bytedance.com>
Wed, 24 Feb 2021 20:03:23 +0000 (12:03 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 24 Feb 2021 21:38:29 +0000 (13:38 -0800)
Currently we use struct per_cpu_nodestat to cache the vmstat counters,
which leads to inaccurate statistics especially THP vmstat counters.  In
the systems with hundreds of processors it can be GBs of memory.  For
example, for a 96 CPUs system, the threshold is the maximum number of 125.
And the per cpu counters can cache 23.4375 GB in total.

The THP page is already a form of batched addition (it will add 512 worth
of memory in one go) so skipping the batching seems like sensible.
Although every THP stats update overflows the per-cpu counter, resorting
to atomic global updates.  But it can make the statistics more accuracy
for the THP vmstat counters.

So we convert the NR_ANON_THPS account to pages.  This patch is consistent
with 8f182270dfec ("mm/swap.c: flush lru pvecs on compound page arrival").
Doing this also can make the unit of vmstat counters more unified.
Finally, the unit of the vmstat counters are pages, kB and bytes.  The
B/KB suffix can tell us that the unit is bytes or kB.  The rest which is
without suffix are pages.

Link: https://lkml.kernel.org/r/20201228164110.2838-3-songmuchun@bytedance.com
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Rafael. J. Wysocki <rafael@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Feng Tang <feng.tang@intel.com>
Cc: NeilBrown <neilb@suse.de>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/base/node.c
fs/proc/meminfo.c
include/linux/mmzone.h
mm/huge_memory.c
mm/memcontrol.c
mm/page_alloc.c
mm/rmap.c
mm/vmstat.c

index 04f71c7bc3f83867cefd7fa9ba0cda4d236c2209..6da0c3508bc9cef6a5060cf297096be66926dbb8 100644 (file)
@@ -461,8 +461,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                             nid, K(sunreclaimable)
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
                             ,
-                            nid, K(node_page_state(pgdat, NR_ANON_THPS) *
-                                   HPAGE_PMD_NR),
+                            nid, K(node_page_state(pgdat, NR_ANON_THPS)),
                             nid, K(node_page_state(pgdat, NR_SHMEM_THPS) *
                                    HPAGE_PMD_NR),
                             nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) *
@@ -519,10 +518,14 @@ static ssize_t node_read_vmstat(struct device *dev,
                                     sum_zone_numa_state(nid, i));
 
 #endif
-       for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
-               len += sysfs_emit_at(buf, len, "%s %lu\n",
-                                    node_stat_name(i),
-                                    node_page_state_pages(pgdat, i));
+       for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
+               unsigned long pages = node_page_state_pages(pgdat, i);
+
+               if (vmstat_item_print_in_thp(i))
+                       pages /= HPAGE_PMD_NR;
+               len += sysfs_emit_at(buf, len, "%s %lu\n", node_stat_name(i),
+                                    pages);
+       }
 
        return len;
 }
index d6fc746196255c38bce3104f942116a3f76f612a..a635c8a84ddf50254eb046e03d6953838fddee06 100644 (file)
@@ -129,7 +129,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
        show_val_kb(m, "AnonHugePages:  ",
-                   global_node_page_state(NR_ANON_THPS) * HPAGE_PMD_NR);
+                   global_node_page_state(NR_ANON_THPS));
        show_val_kb(m, "ShmemHugePages: ",
                    global_node_page_state(NR_SHMEM_THPS) * HPAGE_PMD_NR);
        show_val_kb(m, "ShmemPmdMapped: ",
index b593316bff3d68c08ffd9137c507926750908edc..67d50ef5dd2050baf9da4f4de92ca7bf0b4358fe 100644 (file)
@@ -209,6 +209,19 @@ enum node_stat_item {
        NR_VM_NODE_STAT_ITEMS
 };
 
+/*
+ * Returns true if the item should be printed in THPs (/proc/vmstat
+ * currently prints number of anon, file and shmem THPs. But the item
+ * is charged in pages).
+ */
+static __always_inline bool vmstat_item_print_in_thp(enum node_stat_item item)
+{
+       if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+               return false;
+
+       return item == NR_ANON_THPS;
+}
+
 /*
  * Returns true if the value is measured in bytes (most vmstat values are
  * measured in pages). This defines the API part, the internal representation
index f655137536eb75cbec36243ce170a76dfe359da4..3691e863070a5aef3f6b1763b6c0e590bf1d344f 100644 (file)
@@ -2176,7 +2176,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
                lock_page_memcg(page);
                if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
                        /* Last compound_mapcount is gone. */
-                       __dec_lruvec_page_state(page, NR_ANON_THPS);
+                       __mod_lruvec_page_state(page, NR_ANON_THPS,
+                                               -HPAGE_PMD_NR);
                        if (TestClearPageDoubleMap(page)) {
                                /* No need in mapcount reference anymore */
                                for (i = 0; i < HPAGE_PMD_NR; i++)
index e31e47e7bab244dd037f17f98c543db90fd4b285..b2405f049006a4a8157170116ba7a7d41496775b 100644 (file)
@@ -1533,7 +1533,7 @@ static struct memory_stat memory_stats[] = {
         * on some architectures, the macro of HPAGE_PMD_SIZE is not
         * constant(e.g. powerpc).
         */
-       { "anon_thp", 0, NR_ANON_THPS },
+       { "anon_thp", PAGE_SIZE, NR_ANON_THPS },
        { "file_thp", 0, NR_FILE_THPS },
        { "shmem_thp", 0, NR_SHMEM_THPS },
 #endif
@@ -1566,8 +1566,7 @@ static int __init memory_stats_init(void)
 
        for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
-               if (memory_stats[i].idx == NR_ANON_THPS ||
-                   memory_stats[i].idx == NR_FILE_THPS ||
+               if (memory_stats[i].idx == NR_FILE_THPS ||
                    memory_stats[i].idx == NR_SHMEM_THPS)
                        memory_stats[i].ratio = HPAGE_PMD_SIZE;
 #endif
@@ -4087,10 +4086,6 @@ static int memcg_stat_show(struct seq_file *m, void *v)
                if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
                        continue;
                nr = memcg_page_state_local(memcg, memcg1_stats[i]);
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-               if (memcg1_stats[i] == NR_ANON_THPS)
-                       nr *= HPAGE_PMD_NR;
-#endif
                seq_printf(m, "%s %lu\n", memcg1_stat_names[i], nr * PAGE_SIZE);
        }
 
@@ -4121,10 +4116,6 @@ static int memcg_stat_show(struct seq_file *m, void *v)
                if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
                        continue;
                nr = memcg_page_state(memcg, memcg1_stats[i]);
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-               if (memcg1_stats[i] == NR_ANON_THPS)
-                       nr *= HPAGE_PMD_NR;
-#endif
                seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
                                                (u64)nr * PAGE_SIZE);
        }
@@ -5652,10 +5643,11 @@ static int mem_cgroup_move_account(struct page *page,
                        __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
                        __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
                        if (PageTransHuge(page)) {
-                               __dec_lruvec_state(from_vec, NR_ANON_THPS);
-                               __inc_lruvec_state(to_vec, NR_ANON_THPS);
+                               __mod_lruvec_state(from_vec, NR_ANON_THPS,
+                                                  -nr_pages);
+                               __mod_lruvec_state(to_vec, NR_ANON_THPS,
+                                                  nr_pages);
                        }
-
                }
        } else {
                __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
index ef5070fed76b97605ddeaae8b8c532ea81d7db54..2e2e47f8714b092b1a0924acae037a26fcb99c20 100644 (file)
@@ -5587,7 +5587,7 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
                        K(node_page_state(pgdat, NR_SHMEM_THPS) * HPAGE_PMD_NR),
                        K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)
                                        * HPAGE_PMD_NR),
-                       K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR),
+                       K(node_page_state(pgdat, NR_ANON_THPS)),
 #endif
                        K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
                        node_page_state(pgdat, NR_KERNEL_STACK_KB),
index 08c56aaf72ebe612f8509d85343ebb1a45034ff3..c4d5c63cfd29dd50ff4335915f75c33830c4707c 100644 (file)
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1144,7 +1144,7 @@ void do_page_add_anon_rmap(struct page *page,
                 * disabled.
                 */
                if (compound)
-                       __inc_lruvec_page_state(page, NR_ANON_THPS);
+                       __mod_lruvec_page_state(page, NR_ANON_THPS, nr);
                __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr);
        }
 
@@ -1186,7 +1186,7 @@ void page_add_new_anon_rmap(struct page *page,
                if (hpage_pincount_available(page))
                        atomic_set(compound_pincount_ptr(page), 0);
 
-               __inc_lruvec_page_state(page, NR_ANON_THPS);
+               __mod_lruvec_page_state(page, NR_ANON_THPS, nr);
        } else {
                /* Anon THP always mapped first with PMD */
                VM_BUG_ON_PAGE(PageTransCompound(page), page);
@@ -1292,7 +1292,7 @@ static void page_remove_anon_compound_rmap(struct page *page)
        if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
                return;
 
-       __dec_lruvec_page_state(page, NR_ANON_THPS);
+       __mod_lruvec_page_state(page, NR_ANON_THPS, -thp_nr_pages(page));
 
        if (TestClearPageDoubleMap(page)) {
                /*
index f8942160fc95f56f6e95daf80bbb0589dbe2fd12..07dc0af50cf03f237afc2a3bfee8d774f9e1f67e 100644 (file)
@@ -1619,8 +1619,12 @@ static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
        if (is_zone_first_populated(pgdat, zone)) {
                seq_printf(m, "\n  per-node stats");
                for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
+                       unsigned long pages = node_page_state_pages(pgdat, i);
+
+                       if (vmstat_item_print_in_thp(i))
+                               pages /= HPAGE_PMD_NR;
                        seq_printf(m, "\n      %-12s %lu", node_stat_name(i),
-                                  node_page_state_pages(pgdat, i));
+                                  pages);
                }
        }
        seq_printf(m,
@@ -1740,8 +1744,11 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
        v += NR_VM_NUMA_STAT_ITEMS;
 #endif
 
-       for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
+       for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
                v[i] = global_node_page_state_pages(i);
+               if (vmstat_item_print_in_thp(i))
+                       v[i] /= HPAGE_PMD_NR;
+       }
        v += NR_VM_NODE_STAT_ITEMS;
 
        global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,