Skip to content

Commit b603894

Browse files
shakeelbtorvalds
authored andcommitted
mm: memcg: add swapcache stat for memcg v2
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. (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. [akpm@linux-foundation.org: fix CONFIG_SWAP=n build] Link: https://lkml.kernel.org/r/20210108155813.2914586-3-shakeelb@google.com Signed-off-by: Shakeel Butt <shakeelb@google.com> Acked-by: Michal Hocko <mhocko@suse.com> Reviewed-by: Roman Gushchin <guro@fb.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Muchun Song <songmuchun@bytedance.com> Cc: Yang Shi <shy828301@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f9b1038 commit b603894

8 files changed

Lines changed: 32 additions & 27 deletions

File tree

‎Documentation/admin-guide/cgroup-v2.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,10 @@ PAGE_SIZE multiple when read back.
12991299
Amount of cached filesystem data that was modified and
13001300
is currently being written back to disk
13011301

1302+
swapcached
1303+
Amount of swap cached in memory. The swapcache is accounted
1304+
against both memory and swap usage.
1305+
13021306
anon_thp
13031307
Amount of memory used in anonymous mappings backed by
13041308
transparent hugepages

‎drivers/base/node.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,19 @@ static ssize_t node_read_meminfo(struct device *dev,
372372
struct pglist_data *pgdat = NODE_DATA(nid);
373373
struct sysinfo i;
374374
unsigned long sreclaimable, sunreclaimable;
375+
unsigned long swapcached = 0;
375376

376377
si_meminfo_node(&i, nid);
377378
sreclaimable = node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B);
378379
sunreclaimable = node_page_state_pages(pgdat, NR_SLAB_UNRECLAIMABLE_B);
380+
#ifdef CONFIG_SWAP
381+
swapcached = node_page_state_pages(pgdat, NR_SWAPCACHE);
382+
#endif
379383
len = sysfs_emit_at(buf, len,
380384
"Node %d MemTotal: %8lu kB\n"
381385
"Node %d MemFree: %8lu kB\n"
382386
"Node %d MemUsed: %8lu kB\n"
387+
"Node %d SwapCached: %8lu kB\n"
383388
"Node %d Active: %8lu kB\n"
384389
"Node %d Inactive: %8lu kB\n"
385390
"Node %d Active(anon): %8lu kB\n"
@@ -391,6 +396,7 @@ static ssize_t node_read_meminfo(struct device *dev,
391396
nid, K(i.totalram),
392397
nid, K(i.freeram),
393398
nid, K(i.totalram - i.freeram),
399+
nid, K(swapcached),
394400
nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
395401
node_page_state(pgdat, NR_ACTIVE_FILE)),
396402
nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +

‎include/linux/mmzone.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ enum node_stat_item {
206206
NR_KERNEL_SCS_KB, /* measured in KiB */
207207
#endif
208208
NR_PAGETABLE, /* used for pagetables */
209+
#ifdef CONFIG_SWAP
210+
NR_SWAPCACHE,
211+
#endif
209212
NR_VM_NODE_STAT_ITEMS
210213
};
211214

‎include/linux/swap.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,11 @@ extern struct address_space *swapper_spaces[];
408408
#define swap_address_space(entry) \
409409
(&swapper_spaces[swp_type(entry)][swp_offset(entry) \
410410
>> SWAP_ADDRESS_SPACE_SHIFT])
411-
extern unsigned long total_swapcache_pages(void);
411+
static inline unsigned long total_swapcache_pages(void)
412+
{
413+
return global_node_page_state(NR_SWAPCACHE);
414+
}
415+
412416
extern void show_swap_cache_info(void);
413417
extern int add_to_swap(struct page *page);
414418
extern void *get_shadow_from_swap_cache(swp_entry_t entry);

‎mm/memcontrol.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,9 @@ static const struct memory_stat memory_stats[] = {
15211521
{ "file_mapped", NR_FILE_MAPPED },
15221522
{ "file_dirty", NR_FILE_DIRTY },
15231523
{ "file_writeback", NR_WRITEBACK },
1524+
#ifdef CONFIG_SWAP
1525+
{ "swapcached", NR_SWAPCACHE },
1526+
#endif
15241527
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
15251528
{ "anon_thp", NR_ANON_THPS },
15261529
{ "file_thp", NR_FILE_THPS },

‎mm/migrate.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ int migrate_page_move_mapping(struct address_space *mapping,
500500
__mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
501501
__mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
502502
}
503+
#ifdef CONFIG_SWAP
504+
if (PageSwapCache(page)) {
505+
__mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
506+
__mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
507+
}
508+
#endif
503509
if (dirty && mapping_can_writeback(mapping)) {
504510
__mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
505511
__mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);

‎mm/swap_state.c‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,6 @@ static struct {
6868
unsigned long find_total;
6969
} swap_cache_info;
7070

71-
unsigned long total_swapcache_pages(void)
72-
{
73-
unsigned int i, j, nr;
74-
unsigned long ret = 0;
75-
struct address_space *spaces;
76-
struct swap_info_struct *si;
77-
78-
for (i = 0; i < MAX_SWAPFILES; i++) {
79-
swp_entry_t entry = swp_entry(i, 1);
80-
81-
/* Avoid get_swap_device() to warn for bad swap entry */
82-
if (!swp_swap_info(entry))
83-
continue;
84-
/* Prevent swapoff to free swapper_spaces */
85-
si = get_swap_device(entry);
86-
if (!si)
87-
continue;
88-
nr = nr_swapper_spaces[i];
89-
spaces = swapper_spaces[i];
90-
for (j = 0; j < nr; j++)
91-
ret += spaces[j].nrpages;
92-
put_swap_device(si);
93-
}
94-
return ret;
95-
}
96-
9771
static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
9872

9973
void show_swap_cache_info(void)
@@ -163,6 +137,7 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry,
163137
address_space->nrexceptional -= nr_shadows;
164138
address_space->nrpages += nr;
165139
__mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
140+
__mod_lruvec_page_state(page, NR_SWAPCACHE, nr);
166141
ADD_CACHE_INFO(add_total, nr);
167142
unlock:
168143
xas_unlock_irq(&xas);
@@ -203,6 +178,7 @@ void __delete_from_swap_cache(struct page *page,
203178
address_space->nrexceptional += nr;
204179
address_space->nrpages -= nr;
205180
__mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
181+
__mod_lruvec_page_state(page, NR_SWAPCACHE, -nr);
206182
ADD_CACHE_INFO(del_total, nr);
207183
}
208184

‎mm/vmstat.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,9 @@ const char * const vmstat_text[] = {
12151215
"nr_shadow_call_stack",
12161216
#endif
12171217
"nr_page_table_pages",
1218+
#ifdef CONFIG_SWAP
1219+
"nr_swapcached",
1220+
#endif
12181221

12191222
/* enum writeback_stat_item counters */
12201223
"nr_dirty_threshold",

0 commit comments

Comments
 (0)