Skip to content

Commit 8a9478c

Browse files
hkamezawatorvalds
authored andcommitted
memcg: fix swap accounting
This patch fixes mis-accounting of swap usage in memcg. In the current implementation, memcg's swap account is uncharged only when swap is completely freed. But there are several cases where swap cannot be freed cleanly. For handling that, this patch changes that memcg uncharges swap account when swap has no references other than cache. By this, memcg's swap entry accounting can be fully synchronous with the application's behavior. This patch also changes memcg's hooks for swap-out. Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Acked-by: Balbir Singh <balbir@in.ibm.com> Cc: Hugh Dickins <hugh.dickins@tiscali.co.uk> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Dhaval Giani <dhaval@linux.vnet.ibm.com> Cc: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 20ebcdd commit 8a9478c

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,11 @@ static inline void disable_swap_token(void)
319319
}
320320

321321
#ifdef CONFIG_CGROUP_MEM_RES_CTLR
322-
extern void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent);
322+
extern void
323+
mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout);
323324
#else
324325
static inline void
325-
mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
326+
mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
326327
{
327328
}
328329
#endif

‎mm/memcontrol.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ enum charge_type {
189189
MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
190190
MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
191191
MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
192+
MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
192193
NR_CHARGE_TYPE,
193194
};
194195

@@ -1493,6 +1494,7 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
14931494

14941495
switch (ctype) {
14951496
case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1497+
case MEM_CGROUP_CHARGE_TYPE_DROP:
14961498
if (page_mapped(page))
14971499
goto unlock_out;
14981500
break;
@@ -1556,18 +1558,23 @@ void mem_cgroup_uncharge_cache_page(struct page *page)
15561558
* called after __delete_from_swap_cache() and drop "page" account.
15571559
* memcg information is recorded to swap_cgroup of "ent"
15581560
*/
1559-
void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1561+
void
1562+
mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
15601563
{
15611564
struct mem_cgroup *memcg;
1565+
int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1566+
1567+
if (!swapout) /* this was a swap cache but the swap is unused ! */
1568+
ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1569+
1570+
memcg = __mem_cgroup_uncharge_common(page, ctype);
15621571

1563-
memcg = __mem_cgroup_uncharge_common(page,
1564-
MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
15651572
/* record memcg information */
1566-
if (do_swap_account && memcg) {
1573+
if (do_swap_account && swapout && memcg) {
15671574
swap_cgroup_record(ent, css_id(&memcg->css));
15681575
mem_cgroup_get(memcg);
15691576
}
1570-
if (memcg)
1577+
if (swapout && memcg)
15711578
css_put(&memcg->css);
15721579
}
15731580
#endif

‎mm/swapfile.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ static int swap_entry_free(struct swap_info_struct *p,
583583
swap_list.next = p - swap_info;
584584
nr_swap_pages++;
585585
p->inuse_pages--;
586-
mem_cgroup_uncharge_swap(ent);
587586
}
587+
if (!swap_count(count))
588+
mem_cgroup_uncharge_swap(ent);
588589
return count;
589590
}
590591

@@ -609,12 +610,19 @@ void swap_free(swp_entry_t entry)
609610
void swapcache_free(swp_entry_t entry, struct page *page)
610611
{
611612
struct swap_info_struct *p;
613+
int ret;
612614

613-
if (page)
614-
mem_cgroup_uncharge_swapcache(page, entry);
615615
p = swap_info_get(entry);
616616
if (p) {
617-
swap_entry_free(p, entry, SWAP_CACHE);
617+
ret = swap_entry_free(p, entry, SWAP_CACHE);
618+
if (page) {
619+
bool swapout;
620+
if (ret)
621+
swapout = true; /* the end of swap out */
622+
else
623+
swapout = false; /* no more swap users! */
624+
mem_cgroup_uncharge_swapcache(page, entry, swapout);
625+
}
618626
spin_unlock(&swap_lock);
619627
}
620628
return;

0 commit comments

Comments
 (0)