• KAMEZAWA Hiroyuki's avatar
    memcg: fix swap accounting leak · 03f3c433
    KAMEZAWA Hiroyuki authored
    Fix swapin charge operation of memcg.
    
    Now, memcg has hooks to swap-out operation and checks SwapCache is really
    unused or not.  That check depends on contents of struct page.  I.e.  If
    PageAnon(page) && page_mapped(page), the page is recoginized as
    still-in-use.
    
    Now, reuse_swap_page() calles delete_from_swap_cache() before establishment
    of any rmap. Then, in followinig sequence
    
    	(Page fault with WRITE)
    	try_charge() (charge += PAGESIZE)
    	commit_charge() (Check page_cgroup is used or not..)
    	reuse_swap_page()
    		-> delete_from_swapcache()
    			-> mem_cgroup_uncharge_swapcache() (charge -= PAGESIZE)
    	......
    New charge is uncharged soon....
    To avoid this,  move commit_charge() after page_mapcount() goes up to 1.
    By this,
    
    	try_charge()		(usage += PAGESIZE)
    	reuse_swap_page()	(may usage -= PAGESIZE if PCG_USED is set)
    	commit_charge()		(If page_cgroup is not marked as PCG_USED,
    				 add new charge.)
    Accounting will be correct.
    
    Changelog (v2) -> (v3)
      - fixed invalid charge to swp_entry==0.
      - updated documentation.
    Changelog (v1) -> (v2)
      - fixed comment.
    
    [nishimura@mxp.nes.nec.co.jp: swap accounting leak doc fix]
    Signed-off-by: default avatarKAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Acked-by: default avatarBalbir Singh <balbir@linux.vnet.ibm.com>
    Tested-by: default avatarBalbir Singh <balbir@linux.vnet.ibm.com>
    Cc: Hugh Dickins <hugh@veritas.com>
    Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
    Signed-off-by: default avatarDaisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    03f3c433
memory.c 85.6 KB