mm: memcg: fix memcg file_dirty numa stat
Subsystems (10)
PATCH v2 1/3 mm: memcg: fix memcg file_dirty numa stat
The kernel updates the per-node NR_FILE_DIRTY stats on page migration but not the memcg numa stats. That was not an issue until recently the commit 5f9a4f4a7096 ("mm: memcontrol: add the missing numa_stat interface for cgroup v2") exposed numa stats for the memcg. So fixing the file_dirty per-memcg numa stat.
---
Changes since v1:
- none
mm/migrate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/migrate.c +2 -2
@@ -500,9 +500,9 @@ int migrate_page_move_mapping(struct address_space *mapping,__inc_lruvec_state (new_lruvec,NR_SHMEM ); }if (dirty &&mapping_can_writeback (mapping)) { -__dec_node_state (oldzone->zone_pgdat,NR_FILE_DIRTY ); +__dec_lruvec_state (old_lruvec,NR_FILE_DIRTY );__dec_zone_state (oldzone,NR_ZONE_WRITE_PENDING ); -__inc_node_state (newzone->zone_pgdat,NR_FILE_DIRTY ); +__inc_lruvec_state (new_lruvec,NR_FILE_DIRTY );__inc_zone_state (newzone,NR_ZONE_WRITE_PENDING ); } }
Signature · 1 line
2.29.2.729.g45daf8777d-goog
PATCH v2 2/3 mm: fix numa stats for thp migration
Currently the kernel is not correctly updating the numa stats for NR_FILE_PAGES and NR_SHMEM on THP migration. Fix that. For NR_FILE_DIRTY and NR_ZONE_WRITE_PENDING, although at the moment there is no need to handle THP migration as kernel still does not have write support for file THP but to be more future proof, this patch adds the THP support for those stats as well.
---
Changes since v1:
- Fixed a typo
mm/migrate.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
mm/migrate.c +12 -11
@@ -402,6 +402,7 @@ int migrate_page_move_mapping(struct address_space *mapping,struct zone *oldzone, *newzone;int dirty;int expected_count =expected_page_refs (mapping, page) + extra_count; +int nr =thp_nr_pages (page);if (!mapping) {/* Anonymous page without mapping */
@@ -437,7 +438,7 @@ int migrate_page_move_mapping(struct address_space *mapping,*/ newpage->index = page->index; newpage->mapping = page->mapping; -page_ref_add (newpage,thp_nr_pages (page));/* add cache reference */ +page_ref_add (newpage, nr);/* add cache reference */ if (PageSwapBacked (page)) {__SetPageSwapBacked (newpage);if (PageSwapCache (page)) {
@@ -459,7 +460,7 @@ int migrate_page_move_mapping(struct address_space *mapping,if (PageTransHuge (page)) {int i; -for (i =1 ; i <HPAGE_PMD_NR ; i++) { +for (i =1 ; i < nr; i++) {xas_next (&xas);xas_store (&xas, newpage); }
@@ -470,7 +471,7 @@ int migrate_page_move_mapping(struct address_space *mapping,* to one less reference. * We know this isn't the last reference. */ -page_ref_unfreeze (page, expected_count -thp_nr_pages (page)); +page_ref_unfreeze (page, expected_count - nr);xas_unlock (&xas);/* Leave irq disabled to prevent preemption while updating stats */
@@ -493,17 +494,17 @@ int migrate_page_move_mapping(struct address_space *mapping, old_lruvec =mem_cgroup_lruvec (memcg, oldzone->zone_pgdat); new_lruvec =mem_cgroup_lruvec (memcg, newzone->zone_pgdat); -__dec_lruvec_state (old_lruvec,NR_FILE_PAGES ); -__inc_lruvec_state (new_lruvec,NR_FILE_PAGES ); +__mod_lruvec_state (old_lruvec,NR_FILE_PAGES , -nr); +__mod_lruvec_state (new_lruvec,NR_FILE_PAGES , nr);if (PageSwapBacked (page) && !PageSwapCache (page)) { -__dec_lruvec_state (old_lruvec,NR_SHMEM ); -__inc_lruvec_state (new_lruvec,NR_SHMEM ); +__mod_lruvec_state (old_lruvec,NR_SHMEM , -nr); +__mod_lruvec_state (new_lruvec,NR_SHMEM , nr); }if (dirty &&mapping_can_writeback (mapping)) { -__dec_lruvec_state (old_lruvec,NR_FILE_DIRTY ); -__dec_zone_state (oldzone,NR_ZONE_WRITE_PENDING ); -__inc_lruvec_state (new_lruvec,NR_FILE_DIRTY ); -__inc_zone_state (newzone,NR_ZONE_WRITE_PENDING ); +__mod_lruvec_state (old_lruvec,NR_FILE_DIRTY , -nr); +__mod_zone_page_state (oldzone,NR_ZONE_WRITE_PENDING , -nr); +__mod_lruvec_state (new_lruvec,NR_FILE_DIRTY , nr); +__mod_zone_page_state (newzone,NR_ZONE_WRITE_PENDING , nr); } }local_irq_enable ();
Signature · 1 line
2.29.2.729.g45daf8777d-goog
PATCH v2 3/3 mm: memcg: add swapcache stat for memcg v2
In reply to Shakeel ButtThis patch adds swapcache stat for the cgroup v2. The swapcache represents the memory that is accounted against both the memory and the swap limit of the cgroup. The main motivation behind exposing the swapcache stat is for enabling users to gracefully migrate from cgroup v1's memsw counter to cgroup v2's memory and swap counters.
Cgroup v1's memsw limit allows users to limit the memory+swap usage of a workload but without control on the exact proportion of memory and swap. Cgroup v2 provides separate limits for memory and swap which enables more control on the exact usage of memory and swap individually for the workload.
With some little subtleties, the v1's memsw limit can be switched with the sum of the v2's memory and swap limits. However the alternative for memsw usage is not yet available in cgroup v2. Exposing per-cgroup swapcache stat enables that alternative. Adding the memory usage and swap usage and subtracting the swapcache will approximate the memsw usage. This will help in the transparent migration of the workloads depending on memsw usage and limit to v2' memory and swap counters.
The reasons these applications are still interested in this approximate memsw usage are: (1) these applications are not really interested in two separate memory and swap usage metrics. A single usage metric is more simple to use and reason about for them.
(2) The memsw usage metric hides the underlying system's swap setup from the applications. Applications with multiple instances running in a datacenter with heterogeneous systems (some have swap and some don't) will keep seeing a consistent view of their usage.
---
Changes since v1:
- Updated commit message
Documentation/admin-guide/cgroup-v2.rst | 4 ++++ drivers/base/node.c | 6 ++++++ include/linux/mmzone.h | 3 +++ include/linux/swap.h | 6 +++++- mm/memcontrol.c | 1 + mm/migrate.c | 4 ++++ mm/swap_state.c | 28 ++----------------------- mm/vmstat.c | 3 +++ 8 files changed, 28 insertions(+), 27 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +4 -0
@@ -1299,6 +1299,10 @@ PAGE_SIZE multiple when read back. Amount of cached filesystem data that was modified and is currently being written back to disk + swapcached + Amount of swap cached in memory. The swapcache is accounted + against both memory and swap usage. + anon_thp Amount of memory used in anonymous mappings backed by transparent hugepages
drivers/base/node.c +6 -0
@@ -372,14 +372,19 @@ static ssize_t node_read_meminfo(struct device *dev,struct pglist_data *pgdat =NODE_DATA (nid);struct sysinfo i;unsigned long sreclaimable, sunreclaimable; +unsigned long swapcached =0 ;si_meminfo_node (&i, nid); sreclaimable =node_page_state_pages (pgdat,NR_SLAB_RECLAIMABLE_B ); sunreclaimable =node_page_state_pages (pgdat,NR_SLAB_UNRECLAIMABLE_B ); +#ifdef CONFIG_SWAP + swapcached =node_page_state_pages (pgdat,NR_SWAPCACHE ); +#endif len =sysfs_emit_at (buf, len,"Node %d MemTotal: %8lu kB\n" "Node %d MemFree: %8lu kB\n" "Node %d MemUsed: %8lu kB\n" +"Node %d SwapCached: %8lu kB\n" "Node %d Active: %8lu kB\n" "Node %d Inactive: %8lu kB\n" "Node %d Active(anon): %8lu kB\n"
@@ -391,6 +396,7 @@ static ssize_t node_read_meminfo(struct device *dev, nid,K (i.totalram), nid,K (i.freeram), nid,K (i.totalram - i.freeram), + nid,K (swapcached), nid,K (node_page_state (pgdat,NR_ACTIVE_ANON ) +node_page_state (pgdat,NR_ACTIVE_FILE )), nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
include/linux/mmzone.h +3 -0
@@ -206,6 +206,9 @@ enum node_stat_item { NR_KERNEL_SCS_KB, /* measured in KiB */ #endif NR_PAGETABLE, /* used for pagetables */ +#ifdef CONFIG_SWAP + NR_SWAPCACHE, +#endif NR_VM_NODE_STAT_ITEMS };
include/linux/swap.h +5 -1
@@ -408,7 +408,11 @@ extern struct address_space *swapper_spaces[];#define swap_address_space (entry) \ (&swapper_spaces[swp_type(entry)][swp_offset(entry) \ >> SWAP_ADDRESS_SPACE_SHIFT]) -extern unsigned long total_swapcache_pages (void ); +static inline unsigned long total_swapcache_pages (void ) +{ +return global_node_page_state (NR_SWAPCACHE ); +} +extern void show_swap_cache_info (void );extern int add_to_swap (struct page *page);extern void *get_shadow_from_swap_cache (swp_entry_t entry);
mm/memcontrol.c +1 -0
@@ -1521,6 +1521,7 @@ static const struct memory_stat memory_stats[] = { { "file_mapped", NR_FILE_MAPPED }, { "file_dirty", NR_FILE_DIRTY }, { "file_writeback", NR_WRITEBACK }, + { "swapcached", NR_SWAPCACHE }, #ifdef CONFIG_TRANSPARENT_HUGEPAGE { "anon_thp", NR_ANON_THPS }, { "file_thp", NR_FILE_THPS },
mm/migrate.c +4 -0
@@ -500,6 +500,10 @@ int migrate_page_move_mapping(struct address_space *mapping,__mod_lruvec_state (old_lruvec,NR_SHMEM , -nr);__mod_lruvec_state (new_lruvec,NR_SHMEM , nr); } +if (PageSwapCache (page)) { +__mod_lruvec_state (old_lruvec,NR_SWAPCACHE , -nr); +__mod_lruvec_state (new_lruvec,NR_SWAPCACHE , nr); + }if (dirty &&mapping_can_writeback (mapping)) {__mod_lruvec_state (old_lruvec,NR_FILE_DIRTY , -nr);__mod_zone_page_state (oldzone,NR_ZONE_WRITE_PENDING , -nr);
mm/swap_state.c +2 -26
@@ -68,32 +68,6 @@ static struct {unsigned long find_total; } swap_cache_info; -unsigned long total_swapcache_pages (void ) -{ -unsigned int i, j, nr; -unsigned long ret =0 ; -struct address_space *spaces; -struct swap_info_struct *si; - -for (i =0 ; i <MAX_SWAPFILES ; i++) { -swp_entry_t entry =swp_entry (i,1 ); - -/* Avoid get_swap_device() to warn for bad swap entry */ -if (!swp_swap_info (entry)) -continue ; -/* Prevent swapoff to free swapper_spaces */ - si =get_swap_device (entry); -if (!si) -continue ; - nr = nr_swapper_spaces[i]; - spaces = swapper_spaces[i]; -for (j =0 ; j < nr; j++) - ret += spaces[j].nrpages; -put_swap_device (si); - } -return ret; -} -static atomic_t swapin_readahead_hits =ATOMIC_INIT (4 );void show_swap_cache_info (void )
@@ -161,6 +135,7 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry, address_space->nrexceptional -= nr_shadows; address_space->nrpages += nr;__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , nr); +__mod_lruvec_page_state (page,NR_SWAPCACHE , nr);ADD_CACHE_INFO (add_total, nr); unlock:xas_unlock_irq (&xas);
@@ -201,6 +176,7 @@ void __delete_from_swap_cache(struct page *page, address_space->nrexceptional += nr; address_space->nrpages -= nr;__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , -nr); +__mod_lruvec_page_state (page,NR_SWAPCACHE , -nr);ADD_CACHE_INFO (del_total, nr); }
mm/vmstat.c +3 -0
@@ -1220,6 +1220,9 @@ const char * const vmstat_text[] = {"nr_shadow_call_stack" , #endif"nr_page_table_pages" , +#ifdef CONFIG_SWAP +"nr_swapcached" , +#endif/* enum writeback_stat_item counters */ "nr_dirty_threshold",
Signature · 1 line
2.29.2.729.g45daf8777d-goog
Re: [PATCH v2 3/3] mm: memcg: add swapcache stat for memcg v2
Quoted 25 lines · Shakeel Butt
This patch adds swapcache stat for the cgroup v2. The swapcache represents the memory that is accounted against both the memory and the swap limit of the cgroup. The main motivation behind exposing the swapcache stat is for enabling users to gracefully migrate from cgroup v1's memsw counter to cgroup v2's memory and swap counters.
Cgroup v1's memsw limit allows users to limit the memory+swap usage of a workload but without control on the exact proportion of memory and swap. Cgroup v2 provides separate limits for memory and swap which enables more control on the exact usage of memory and swap individually for the workload.
With some little subtleties, the v1's memsw limit can be switched with the sum of the v2's memory and swap limits. However the alternative for memsw usage is not yet available in cgroup v2. Exposing per-cgroup swapcache stat enables that alternative. Adding the memory usage and swap usage and subtracting the swapcache will approximate the memsw usage. This will help in the transparent migration of the workloads depending on memsw usage and limit to v2' memory and swap counters.
The reasons these applications are still interested in this approximate memsw usage are: (1) these applications are not really interested in two separate memory and swap usage metrics. A single usage metric is more simple to use and reason about for them.
Quoted 7 lines
(2) The memsw usage metric hides the underlying system's swap setup from the applications. Applications with multiple instances running in a datacenter with heterogeneous systems (some have swap and some don't) will keep seeing a consistent view of their usage.
Signed-off-by Shakeel Butt <[email protected]>Acked-by Michal Hocko <[email protected]>
Thanks!