3333#include <linux/rbtree.h>
3434#include <linux/slab.h>
3535#include <linux/swap.h>
36+ #include <linux/swapops.h>
3637#include <linux/spinlock.h>
3738#include <linux/fs.h>
3839#include <linux/seq_file.h>
@@ -2270,6 +2271,54 @@ void mem_cgroup_uncharge_swap(swp_entry_t ent)
22702271 }
22712272 rcu_read_unlock ();
22722273}
2274+
2275+ /**
2276+ * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2277+ * @entry: swap entry to be moved
2278+ * @from: mem_cgroup which the entry is moved from
2279+ * @to: mem_cgroup which the entry is moved to
2280+ *
2281+ * It succeeds only when the swap_cgroup's record for this entry is the same
2282+ * as the mem_cgroup's id of @from.
2283+ *
2284+ * Returns 0 on success, -EINVAL on failure.
2285+ *
2286+ * The caller must have charged to @to, IOW, called res_counter_charge() about
2287+ * both res and memsw, and called css_get().
2288+ */
2289+ static int mem_cgroup_move_swap_account (swp_entry_t entry ,
2290+ struct mem_cgroup * from , struct mem_cgroup * to )
2291+ {
2292+ unsigned short old_id , new_id ;
2293+
2294+ old_id = css_id (& from -> css );
2295+ new_id = css_id (& to -> css );
2296+
2297+ if (swap_cgroup_cmpxchg (entry , old_id , new_id ) == old_id ) {
2298+ if (!mem_cgroup_is_root (from ))
2299+ res_counter_uncharge (& from -> memsw , PAGE_SIZE );
2300+ mem_cgroup_swap_statistics (from , false);
2301+ mem_cgroup_put (from );
2302+ /*
2303+ * we charged both to->res and to->memsw, so we should uncharge
2304+ * to->res.
2305+ */
2306+ if (!mem_cgroup_is_root (to ))
2307+ res_counter_uncharge (& to -> res , PAGE_SIZE );
2308+ mem_cgroup_swap_statistics (to , true);
2309+ mem_cgroup_get (to );
2310+ css_put (& to -> css );
2311+
2312+ return 0 ;
2313+ }
2314+ return - EINVAL ;
2315+ }
2316+ #else
2317+ static inline int mem_cgroup_move_swap_account (swp_entry_t entry ,
2318+ struct mem_cgroup * from , struct mem_cgroup * to )
2319+ {
2320+ return - EINVAL ;
2321+ }
22732322#endif
22742323
22752324/*
@@ -2949,6 +2998,7 @@ static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
29492998 return mem_cgroup_from_cont (cgrp )-> move_charge_at_immigrate ;
29502999}
29513000
3001+ #ifdef CONFIG_MMU
29523002static int mem_cgroup_move_charge_write (struct cgroup * cgrp ,
29533003 struct cftype * cft , u64 val )
29543004{
@@ -2967,6 +3017,13 @@ static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
29673017
29683018 return 0 ;
29693019}
3020+ #else
3021+ static int mem_cgroup_move_charge_write (struct cgroup * cgrp ,
3022+ struct cftype * cft , u64 val )
3023+ {
3024+ return - ENOSYS ;
3025+ }
3026+ #endif
29703027
29713028
29723029/* For read statistics */
@@ -3489,6 +3546,7 @@ static int mem_cgroup_populate(struct cgroup_subsys *ss,
34893546 return ret ;
34903547}
34913548
3549+ #ifdef CONFIG_MMU
34923550/* Handlers for move charge at task migration. */
34933551#define PRECHARGE_COUNT_AT_ONCE 256
34943552static int mem_cgroup_do_precharge (unsigned long count )
@@ -3544,77 +3602,124 @@ static int mem_cgroup_do_precharge(unsigned long count)
35443602 }
35453603 return ret ;
35463604}
3605+ #else /* !CONFIG_MMU */
3606+ static int mem_cgroup_can_attach (struct cgroup_subsys * ss ,
3607+ struct cgroup * cgroup ,
3608+ struct task_struct * p ,
3609+ bool threadgroup )
3610+ {
3611+ return 0 ;
3612+ }
3613+ static void mem_cgroup_cancel_attach (struct cgroup_subsys * ss ,
3614+ struct cgroup * cgroup ,
3615+ struct task_struct * p ,
3616+ bool threadgroup )
3617+ {
3618+ }
3619+ static void mem_cgroup_move_task (struct cgroup_subsys * ss ,
3620+ struct cgroup * cont ,
3621+ struct cgroup * old_cont ,
3622+ struct task_struct * p ,
3623+ bool threadgroup )
3624+ {
3625+ }
3626+ #endif
35473627
35483628/**
35493629 * is_target_pte_for_mc - check a pte whether it is valid for move charge
35503630 * @vma: the vma the pte to be checked belongs
35513631 * @addr: the address corresponding to the pte to be checked
35523632 * @ptent: the pte to be checked
3553- * @target: the pointer the target page will be stored(can be NULL)
3633+ * @target: the pointer the target page or swap ent will be stored(can be NULL)
35543634 *
35553635 * Returns
35563636 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
35573637 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
35583638 * move charge. if @target is not NULL, the page is stored in target->page
35593639 * with extra refcnt got(Callers should handle it).
3640+ * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
3641+ * target for charge migration. if @target is not NULL, the entry is stored
3642+ * in target->ent.
35603643 *
35613644 * Called with pte lock held.
35623645 */
3563- /* We add a new member later. */
35643646union mc_target {
35653647 struct page * page ;
3648+ swp_entry_t ent ;
35663649};
35673650
3568- /* We add a new type later. */
35693651enum mc_target_type {
35703652 MC_TARGET_NONE , /* not used */
35713653 MC_TARGET_PAGE ,
3654+ MC_TARGET_SWAP ,
35723655};
35733656
35743657static int is_target_pte_for_mc (struct vm_area_struct * vma ,
35753658 unsigned long addr , pte_t ptent , union mc_target * target )
35763659{
3577- struct page * page ;
3660+ struct page * page = NULL ;
35783661 struct page_cgroup * pc ;
35793662 int ret = 0 ;
3663+ swp_entry_t ent = { .val = 0 };
3664+ int usage_count = 0 ;
35803665 bool move_anon = test_bit (MOVE_CHARGE_TYPE_ANON ,
35813666 & mc .to -> move_charge_at_immigrate );
35823667
3583- if (!pte_present (ptent ))
3584- return 0 ;
3585-
3586- page = vm_normal_page (vma , addr , ptent );
3587- if (!page || !page_mapped (page ))
3588- return 0 ;
3589- /*
3590- * TODO: We don't move charges of file(including shmem/tmpfs) pages for
3591- * now.
3592- */
3593- if (!move_anon || !PageAnon (page ))
3594- return 0 ;
3595- /*
3596- * TODO: We don't move charges of shared(used by multiple processes)
3597- * pages for now.
3598- */
3599- if (page_mapcount (page ) > 1 )
3600- return 0 ;
3601- if (!get_page_unless_zero (page ))
3668+ if (!pte_present (ptent )) {
3669+ /* TODO: handle swap of shmes/tmpfs */
3670+ if (pte_none (ptent ) || pte_file (ptent ))
3671+ return 0 ;
3672+ else if (is_swap_pte (ptent )) {
3673+ ent = pte_to_swp_entry (ptent );
3674+ if (!move_anon || non_swap_entry (ent ))
3675+ return 0 ;
3676+ usage_count = mem_cgroup_count_swap_user (ent , & page );
3677+ }
3678+ } else {
3679+ page = vm_normal_page (vma , addr , ptent );
3680+ if (!page || !page_mapped (page ))
3681+ return 0 ;
3682+ /*
3683+ * TODO: We don't move charges of file(including shmem/tmpfs)
3684+ * pages for now.
3685+ */
3686+ if (!move_anon || !PageAnon (page ))
3687+ return 0 ;
3688+ if (!get_page_unless_zero (page ))
3689+ return 0 ;
3690+ usage_count = page_mapcount (page );
3691+ }
3692+ if (usage_count > 1 ) {
3693+ /*
3694+ * TODO: We don't move charges of shared(used by multiple
3695+ * processes) pages for now.
3696+ */
3697+ if (page )
3698+ put_page (page );
36023699 return 0 ;
3603-
3604- pc = lookup_page_cgroup (page );
3605- /*
3606- * Do only loose check w/o page_cgroup lock. mem_cgroup_move_account()
3607- * checks the pc is valid or not under the lock.
3608- */
3609- if (PageCgroupUsed (pc ) && pc -> mem_cgroup == mc .from ) {
3610- ret = MC_TARGET_PAGE ;
3700+ }
3701+ if (page ) {
3702+ pc = lookup_page_cgroup (page );
3703+ /*
3704+ * Do only loose check w/o page_cgroup lock.
3705+ * mem_cgroup_move_account() checks the pc is valid or not under
3706+ * the lock.
3707+ */
3708+ if (PageCgroupUsed (pc ) && pc -> mem_cgroup == mc .from ) {
3709+ ret = MC_TARGET_PAGE ;
3710+ if (target )
3711+ target -> page = page ;
3712+ }
3713+ if (!ret || !target )
3714+ put_page (page );
3715+ }
3716+ /* throught */
3717+ if (ent .val && do_swap_account && !ret &&
3718+ css_id (& mc .from -> css ) == lookup_swap_cgroup (ent )) {
3719+ ret = MC_TARGET_SWAP ;
36113720 if (target )
3612- target -> page = page ;
3721+ target -> ent = ent ;
36133722 }
3614-
3615- if (!ret || !target )
3616- put_page (page );
3617-
36183723 return ret ;
36193724}
36203725
@@ -3754,6 +3859,7 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
37543859 int type ;
37553860 struct page * page ;
37563861 struct page_cgroup * pc ;
3862+ swp_entry_t ent ;
37573863
37583864 if (!mc .precharge )
37593865 break ;
@@ -3775,6 +3881,11 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
37753881put : /* is_target_pte_for_mc() gets the page */
37763882 put_page (page );
37773883 break ;
3884+ case MC_TARGET_SWAP :
3885+ ent = target .ent ;
3886+ if (!mem_cgroup_move_swap_account (ent , mc .from , mc .to ))
3887+ mc .precharge -- ;
3888+ break ;
37783889 default :
37793890 break ;
37803891 }
0 commit comments