Skip to content
Snippets Groups Projects
memcontrol.c 131 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
     * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
     * not from the memcg which this page would be charged to.
     * try_charge_swapin does all of these works properly.
    
    int mem_cgroup_shmem_charge_fallback(struct page *page,
    
    			    struct mm_struct *mm,
    			    gfp_t gfp_mask)
    
    	if (mem_cgroup_disabled())
    
    	ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
    	if (!ret)
    		mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
    
    #ifdef CONFIG_DEBUG_VM
    static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
    {
    	struct page_cgroup *pc;
    
    	pc = lookup_page_cgroup(page);
    	if (likely(pc) && PageCgroupUsed(pc))
    		return pc;
    	return NULL;
    }
    
    bool mem_cgroup_bad_page_check(struct page *page)
    {
    	if (mem_cgroup_disabled())
    		return false;
    
    	return lookup_page_cgroup_used(page) != NULL;
    }
    
    void mem_cgroup_print_bad_page(struct page *page)
    {
    	struct page_cgroup *pc;
    
    	pc = lookup_page_cgroup_used(page);
    	if (pc) {
    		int ret = -1;
    		char *path;
    
    		printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
    		       pc, pc->flags, pc->mem_cgroup);
    
    		path = kmalloc(PATH_MAX, GFP_KERNEL);
    		if (path) {
    			rcu_read_lock();
    			ret = cgroup_path(pc->mem_cgroup->css.cgroup,
    							path, PATH_MAX);
    			rcu_read_unlock();
    		}
    
    		printk(KERN_CONT "(%s)\n",
    				(ret < 0) ? "cannot get the path" : path);
    		kfree(path);
    	}
    }
    #endif
    
    
    static DEFINE_MUTEX(set_limit_mutex);
    
    
    static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
    
    				unsigned long long val)
    
    	u64 memswlimit, memlimit;
    
    	int ret = 0;
    
    	int children = mem_cgroup_count_children(memcg);
    	u64 curusage, oldusage;
    
    
    	/*
    	 * For keeping hierarchical_reclaim simple, how long we should retry
    	 * is depends on callers. We set our retry-count to be function
    	 * of # of children which we should visit in this loop.
    	 */
    	retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
    
    	oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
    
    	while (retry_count) {
    
    		if (signal_pending(current)) {
    			ret = -EINTR;
    			break;
    		}
    
    		/*
    		 * Rather than hide all in some function, I do this in
    		 * open coded manner. You see what this really does.
    		 * We have to guarantee mem->res.limit < mem->memsw.limit.
    		 */
    		mutex_lock(&set_limit_mutex);
    		memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
    		if (memswlimit < val) {
    			ret = -EINVAL;
    			mutex_unlock(&set_limit_mutex);
    
    
    		memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
    		if (memlimit < val)
    			enlarge = 1;
    
    
    		ret = res_counter_set_limit(&memcg->res, val);
    
    		if (!ret) {
    			if (memswlimit == val)
    				memcg->memsw_is_minimum = true;
    			else
    				memcg->memsw_is_minimum = false;
    		}
    
    		mutex_unlock(&set_limit_mutex);
    
    		if (!ret)
    			break;
    
    
    		mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
    
    						MEM_CGROUP_RECLAIM_SHRINK);
    
    		curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
    		/* Usage is reduced ? */
      		if (curusage >= oldusage)
    			retry_count--;
    		else
    			oldusage = curusage;
    
    	if (!ret && enlarge)
    		memcg_oom_recover(memcg);
    
    static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
    					unsigned long long val)
    
    	u64 memlimit, memswlimit, oldusage, curusage;
    
    	int children = mem_cgroup_count_children(memcg);
    	int ret = -EBUSY;
    
    	int enlarge = 0;
    
    	/* see mem_cgroup_resize_res_limit */
     	retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
    	oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
    
    	while (retry_count) {
    		if (signal_pending(current)) {
    			ret = -EINTR;
    			break;
    		}
    		/*
    		 * Rather than hide all in some function, I do this in
    		 * open coded manner. You see what this really does.
    		 * We have to guarantee mem->res.limit < mem->memsw.limit.
    		 */
    		mutex_lock(&set_limit_mutex);
    		memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
    		if (memlimit > val) {
    			ret = -EINVAL;
    			mutex_unlock(&set_limit_mutex);
    			break;
    		}
    
    		memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
    		if (memswlimit < val)
    			enlarge = 1;
    
    		ret = res_counter_set_limit(&memcg->memsw, val);
    
    		if (!ret) {
    			if (memlimit == val)
    				memcg->memsw_is_minimum = true;
    			else
    				memcg->memsw_is_minimum = false;
    		}
    
    		mutex_unlock(&set_limit_mutex);
    
    		if (!ret)
    			break;
    
    
    		mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
    
    						MEM_CGROUP_RECLAIM_NOSWAP |
    						MEM_CGROUP_RECLAIM_SHRINK);
    
    		curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
    
    		if (curusage >= oldusage)
    
    			retry_count--;
    
    	if (!ret && enlarge)
    		memcg_oom_recover(memcg);
    
    unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
    
    {
    	unsigned long nr_reclaimed = 0;
    	struct mem_cgroup_per_zone *mz, *next_mz = NULL;
    	unsigned long reclaimed;
    	int loop = 0;
    	struct mem_cgroup_tree_per_zone *mctz;
    
    	unsigned long long excess;
    
    	mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
    
    	/*
    	 * This loop can run a while, specially if mem_cgroup's continuously
    	 * keep exceeding their soft limit and putting the system under
    	 * pressure
    	 */
    	do {
    		if (next_mz)
    			mz = next_mz;
    		else
    			mz = mem_cgroup_largest_soft_limit_node(mctz);
    		if (!mz)
    			break;
    
    		reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
    						gfp_mask,
    						MEM_CGROUP_RECLAIM_SOFT);
    		nr_reclaimed += reclaimed;
    		spin_lock(&mctz->lock);
    
    		/*
    		 * If we failed to reclaim anything from this memory cgroup
    		 * it is time to move on to the next cgroup
    		 */
    		next_mz = NULL;
    		if (!reclaimed) {
    			do {
    				/*
    				 * Loop until we find yet another one.
    				 *
    				 * By the time we get the soft_limit lock
    				 * again, someone might have aded the
    				 * group back on the RB tree. Iterate to
    				 * make sure we get a different mem.
    				 * mem_cgroup_largest_soft_limit_node returns
    				 * NULL if no other cgroup is present on
    				 * the tree
    				 */
    				next_mz =
    				__mem_cgroup_largest_soft_limit_node(mctz);
    				if (next_mz == mz) {
    					css_put(&next_mz->mem->css);
    					next_mz = NULL;
    				} else /* next_mz == NULL or other memcg */
    					break;
    			} while (1);
    		}
    		__mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
    
    		excess = res_counter_soft_limit_excess(&mz->mem->res);
    
    		/*
    		 * One school of thought says that we should not add
    		 * back the node to the tree if reclaim returns 0.
    		 * But our reclaim could return 0, simply because due
    		 * to priority we are exposing a smaller subset of
    		 * memory to reclaim from. Consider this as a longer
    		 * term TODO.
    		 */
    
    		/* If excess == 0, no tree ops */
    		__mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
    
    		spin_unlock(&mctz->lock);
    		css_put(&mz->mem->css);
    		loop++;
    		/*
    		 * Could not reclaim anything and there are no more
    		 * mem cgroups to try or we seem to be looping without
    		 * reclaiming anything.
    		 */
    		if (!nr_reclaimed &&
    			(next_mz == NULL ||
    			loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
    			break;
    	} while (!nr_reclaimed);
    	if (next_mz)
    		css_put(&next_mz->mem->css);
    	return nr_reclaimed;
    }
    
    
    /*
     * This routine traverse page_cgroup in given list and drop them all.
     * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
     */
    
    static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
    
    				int node, int zid, enum lru_list lru)
    
    	struct zone *zone;
    	struct mem_cgroup_per_zone *mz;
    
    	struct page_cgroup *pc, *busy;
    
    	unsigned long flags, loop;
    
    	zone = &NODE_DATA(node)->node_zones[zid];
    	mz = mem_cgroup_zoneinfo(mem, node, zid);
    
    	list = &mz->lists[lru];
    
    	loop = MEM_CGROUP_ZSTAT(mz, lru);
    	/* give some margin against EBUSY etc...*/
    	loop += 256;
    	busy = NULL;
    	while (loop--) {
    
    		spin_lock_irqsave(&zone->lru_lock, flags);
    
    		if (list_empty(list)) {
    
    			spin_unlock_irqrestore(&zone->lru_lock, flags);
    
    		}
    		pc = list_entry(list->prev, struct page_cgroup, lru);
    		if (busy == pc) {
    			list_move(&pc->lru, list);
    
    			spin_unlock_irqrestore(&zone->lru_lock, flags);
    
    		spin_unlock_irqrestore(&zone->lru_lock, flags);
    
    		page = lookup_cgroup_page(pc);
    
    
    		ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
    
    
    		if (ret == -EBUSY || ret == -EINVAL) {
    			/* found lock contention or "pc" is obsolete. */
    			busy = pc;
    			cond_resched();
    		} else
    			busy = NULL;
    
    	if (!ret && !list_empty(list))
    		return -EBUSY;
    	return ret;
    
    }
    
    /*
     * make mem_cgroup's charge to be 0 if there is no task.
     * This enables deleting this mem_cgroup.
     */
    
    static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
    
    	int ret;
    	int node, zid, shrink;
    	int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
    
    	struct cgroup *cgrp = mem->css.cgroup;
    
    	/* should free all ? */
    	if (free_all)
    		goto try_to_free;
    
    		if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
    			goto out;
    		ret = -EINTR;
    		if (signal_pending(current))
    
    		/* This is for making all *used* pages to be on LRU. */
    		lru_add_drain_all();
    
    		drain_all_stock_sync();
    
    		for_each_node_state(node, N_HIGH_MEMORY) {
    
    			for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
    
    				for_each_lru(l) {
    					ret = mem_cgroup_force_empty_list(mem,
    
    							node, zid, l);
    
    		memcg_oom_recover(mem);
    
    		/* it seems parent cgroup doesn't have enough mem */
    		if (ret == -ENOMEM)
    			goto try_to_free;
    
    	/* "ret" should also be checked to ensure all lists are empty. */
    	} while (mem->res.usage > 0 || ret);
    
    	/* returns EBUSY if there is a task or if we come here twice. */
    	if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
    
    	/* we call try-to-free pages for make this cgroup empty */
    	lru_add_drain_all();
    
    	/* try to free all pages in this cgroup */
    	shrink = 1;
    	while (nr_retries && mem->res.usage > 0) {
    		int progress;
    
    
    		if (signal_pending(current)) {
    			ret = -EINTR;
    			goto out;
    		}
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    		progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
    						false, get_swappiness(mem));
    
    			/* maybe some writeback is necessary */
    
    			congestion_wait(BLK_RW_ASYNC, HZ/10);
    
    	lru_add_drain();
    
    	/* try move_account...there may be some *locked* pages. */
    
    	goto move_account;
    
    int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
    {
    	return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
    }
    
    
    
    static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
    {
    	return mem_cgroup_from_cont(cont)->use_hierarchy;
    }
    
    static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
    					u64 val)
    {
    	int retval = 0;
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
    	struct cgroup *parent = cont->parent;
    	struct mem_cgroup *parent_mem = NULL;
    
    	if (parent)
    		parent_mem = mem_cgroup_from_cont(parent);
    
    	cgroup_lock();
    	/*
    
    	 * If parent's use_hierarchy is set, we can't make any modifications
    
    	 * in the child subtrees. If it is unset, then the change can
    	 * occur, provided the current cgroup has no children.
    	 *
    	 * For the root cgroup, parent_mem is NULL, we allow value to be
    	 * set if there are no children.
    	 */
    	if ((!parent_mem || !parent_mem->use_hierarchy) &&
    				(val == 1 || val == 0)) {
    		if (list_empty(&cont->children))
    			mem->use_hierarchy = val;
    		else
    			retval = -EBUSY;
    	} else
    		retval = -EINVAL;
    	cgroup_unlock();
    
    	return retval;
    }
    
    
    static u64 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
    				enum mem_cgroup_stat_index idx)
    
    	struct mem_cgroup *iter;
    	s64 val = 0;
    
    	/* each per cpu's value can be minus.Then, use s64 */
    	for_each_mem_cgroup_tree(iter, mem)
    		val += mem_cgroup_read_stat(iter, idx);
    
    	if (val < 0) /* race ? */
    		val = 0;
    	return val;
    
    static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
    {
    
    
    	if (!mem_cgroup_is_root(mem)) {
    		if (!swap)
    			return res_counter_read_u64(&mem->res, RES_USAGE);
    		else
    			return res_counter_read_u64(&mem->memsw, RES_USAGE);
    	}
    
    
    	val = mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE);
    	val += mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS);
    
    	if (swap)
    		val += mem_cgroup_get_recursive_idx_stat(mem,
    				MEM_CGROUP_STAT_SWAPOUT);
    
    static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
    
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
    
    	int type, name;
    
    	type = MEMFILE_TYPE(cft->private);
    	name = MEMFILE_ATTR(cft->private);
    	switch (type) {
    	case _MEM:
    
    		if (name == RES_USAGE)
    			val = mem_cgroup_usage(mem, false);
    		else
    
    			val = res_counter_read_u64(&mem->res, name);
    
    		break;
    	case _MEMSWAP:
    
    		if (name == RES_USAGE)
    			val = mem_cgroup_usage(mem, true);
    		else
    
    			val = res_counter_read_u64(&mem->memsw, name);
    
    		break;
    	default:
    		BUG();
    		break;
    	}
    	return val;
    
    /*
     * The user of this function is...
     * RES_LIMIT.
     */
    
    static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
    			    const char *buffer)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
    
    	int type, name;
    
    	unsigned long long val;
    	int ret;
    
    
    	type = MEMFILE_TYPE(cft->private);
    	name = MEMFILE_ATTR(cft->private);
    	switch (name) {
    
    	case RES_LIMIT:
    
    		if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
    			ret = -EINVAL;
    			break;
    		}
    
    		/* This function does all necessary parse...reuse it */
    		ret = res_counter_memparse_write_strategy(buffer, &val);
    
    		if (ret)
    			break;
    		if (type == _MEM)
    
    			ret = mem_cgroup_resize_limit(memcg, val);
    
    		else
    			ret = mem_cgroup_resize_memsw_limit(memcg, val);
    
    	case RES_SOFT_LIMIT:
    		ret = res_counter_memparse_write_strategy(buffer, &val);
    		if (ret)
    			break;
    		/*
    		 * For memsw, soft limits are hard to implement in terms
    		 * of semantics, for now, we support soft limits for
    		 * control without swap
    		 */
    		if (type == _MEM)
    			ret = res_counter_set_soft_limit(&memcg->res, val);
    		else
    			ret = -EINVAL;
    		break;
    
    	default:
    		ret = -EINVAL; /* should be BUG() ? */
    		break;
    	}
    	return ret;
    
    static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
    		unsigned long long *mem_limit, unsigned long long *memsw_limit)
    {
    	struct cgroup *cgroup;
    	unsigned long long min_limit, min_memsw_limit, tmp;
    
    	min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
    	min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
    	cgroup = memcg->css.cgroup;
    	if (!memcg->use_hierarchy)
    		goto out;
    
    	while (cgroup->parent) {
    		cgroup = cgroup->parent;
    		memcg = mem_cgroup_from_cont(cgroup);
    		if (!memcg->use_hierarchy)
    			break;
    		tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
    		min_limit = min(min_limit, tmp);
    		tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
    		min_memsw_limit = min(min_memsw_limit, tmp);
    	}
    out:
    	*mem_limit = min_limit;
    	*memsw_limit = min_memsw_limit;
    	return;
    }
    
    
    static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
    
    	int type, name;
    
    
    	mem = mem_cgroup_from_cont(cont);
    
    	type = MEMFILE_TYPE(event);
    	name = MEMFILE_ATTR(event);
    	switch (name) {
    
    	case RES_MAX_USAGE:
    
    		if (type == _MEM)
    			res_counter_reset_max(&mem->res);
    		else
    			res_counter_reset_max(&mem->memsw);
    
    		break;
    	case RES_FAILCNT:
    
    		if (type == _MEM)
    			res_counter_reset_failcnt(&mem->res);
    		else
    			res_counter_reset_failcnt(&mem->memsw);
    
    static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
    					struct cftype *cft)
    {
    	return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
    }
    
    
    #ifdef CONFIG_MMU
    
    static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
    					struct cftype *cft, u64 val)
    {
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
    
    	if (val >= (1 << NR_MOVE_TYPE))
    		return -EINVAL;
    	/*
    	 * We check this value several times in both in can_attach() and
    	 * attach(), so we need cgroup lock to prevent this value from being
    	 * inconsistent.
    	 */
    	cgroup_lock();
    	mem->move_charge_at_immigrate = val;
    	cgroup_unlock();
    
    	return 0;
    }
    
    #else
    static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
    					struct cftype *cft, u64 val)
    {
    	return -ENOSYS;
    }
    #endif
    
    
    /* For read statistics */
    enum {
    	MCS_CACHE,
    	MCS_RSS,
    
    	MCS_PGPGIN,
    	MCS_PGPGOUT,
    
    	MCS_INACTIVE_ANON,
    	MCS_ACTIVE_ANON,
    	MCS_INACTIVE_FILE,
    	MCS_ACTIVE_FILE,
    	MCS_UNEVICTABLE,
    	NR_MCS_STAT,
    };
    
    struct mcs_total_stat {
    	s64 stat[NR_MCS_STAT];
    
    struct {
    	char *local_name;
    	char *total_name;
    } memcg_stat_strings[NR_MCS_STAT] = {
    	{"cache", "total_cache"},
    	{"rss", "total_rss"},
    
    	{"mapped_file", "total_mapped_file"},
    
    	{"pgpgin", "total_pgpgin"},
    	{"pgpgout", "total_pgpgout"},
    
    	{"swap", "total_swap"},
    
    	{"inactive_anon", "total_inactive_anon"},
    	{"active_anon", "total_active_anon"},
    	{"inactive_file", "total_inactive_file"},
    	{"active_file", "total_active_file"},
    	{"unevictable", "total_unevictable"}
    };
    
    
    
    static void
    mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
    
    {
    	s64 val;
    
    	/* per cpu stat */
    
    	val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
    
    	s->stat[MCS_CACHE] += val * PAGE_SIZE;
    
    	val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
    
    	s->stat[MCS_RSS] += val * PAGE_SIZE;
    
    	val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
    
    	s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
    
    	val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
    
    	s->stat[MCS_PGPGIN] += val;
    
    	val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
    
    	s->stat[MCS_PGPGOUT] += val;
    
    	if (do_swap_account) {
    
    		val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
    
    		s->stat[MCS_SWAP] += val * PAGE_SIZE;
    	}
    
    
    	/* per zone stat */
    	val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
    	s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
    	val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
    	s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
    	val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
    	s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
    	val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
    	s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
    	val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
    	s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
    }
    
    static void
    mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
    {
    
    	struct mem_cgroup *iter;
    
    	for_each_mem_cgroup_tree(iter, mem)
    		mem_cgroup_get_local_stat(iter, s);
    
    static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
    				 struct cgroup_map_cb *cb)
    
    {
    	struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
    
    	struct mcs_total_stat mystat;
    
    	memset(&mystat, 0, sizeof(mystat));
    	mem_cgroup_get_local_stat(mem_cont, &mystat);
    
    	for (i = 0; i < NR_MCS_STAT; i++) {
    		if (i == MCS_SWAP && !do_swap_account)
    			continue;
    
    		cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
    
    	/* Hierarchical information */
    
    	{
    		unsigned long long limit, memsw_limit;
    		memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
    		cb->fill(cb, "hierarchical_memory_limit", limit);
    		if (do_swap_account)
    			cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
    	}
    
    	memset(&mystat, 0, sizeof(mystat));
    	mem_cgroup_get_total_stat(mem_cont, &mystat);
    
    	for (i = 0; i < NR_MCS_STAT; i++) {
    		if (i == MCS_SWAP && !do_swap_account)
    			continue;
    
    		cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
    
    #ifdef CONFIG_DEBUG_VM
    
    	cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
    
    
    	{
    		int nid, zid;
    		struct mem_cgroup_per_zone *mz;
    		unsigned long recent_rotated[2] = {0, 0};
    		unsigned long recent_scanned[2] = {0, 0};
    
    		for_each_online_node(nid)
    			for (zid = 0; zid < MAX_NR_ZONES; zid++) {
    				mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
    
    				recent_rotated[0] +=
    					mz->reclaim_stat.recent_rotated[0];
    				recent_rotated[1] +=
    					mz->reclaim_stat.recent_rotated[1];
    				recent_scanned[0] +=
    					mz->reclaim_stat.recent_scanned[0];
    				recent_scanned[1] +=
    					mz->reclaim_stat.recent_scanned[1];
    			}
    		cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
    		cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
    		cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
    		cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
    	}
    #endif
    
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
    {
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
    
    	return get_swappiness(memcg);
    }
    
    static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
    				       u64 val)
    {
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
    	struct mem_cgroup *parent;
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	if (val > 100)
    		return -EINVAL;
    
    	if (cgrp->parent == NULL)
    		return -EINVAL;
    
    	parent = mem_cgroup_from_cont(cgrp->parent);
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	/* If under hierarchy, only empty-root can set this value */
    	if ((parent->use_hierarchy) ||
    
    	    (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
    		cgroup_unlock();
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    		return -EINVAL;
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    
    	memcg->swappiness = val;
    
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	return 0;
    }
    
    
    static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
    {
    	struct mem_cgroup_threshold_ary *t;
    	u64 usage;
    	int i;
    
    	rcu_read_lock();
    	if (!swap)
    
    		t = rcu_dereference(memcg->thresholds.primary);
    
    		t = rcu_dereference(memcg->memsw_thresholds.primary);
    
    
    	if (!t)
    		goto unlock;
    
    	usage = mem_cgroup_usage(memcg, swap);
    
    	/*
    	 * current_threshold points to threshold just below usage.
    	 * If it's not true, a threshold was crossed after last
    	 * call of __mem_cgroup_threshold().
    	 */
    
    	i = t->current_threshold;
    
    
    	/*
    	 * Iterate backward over array of thresholds starting from
    	 * current_threshold and check if a threshold is crossed.
    	 * If none of thresholds below usage is crossed, we read
    	 * only one element of the array here.
    	 */
    	for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
    		eventfd_signal(t->entries[i].eventfd, 1);
    
    	/* i = current_threshold + 1 */
    	i++;
    
    	/*
    	 * Iterate forward over array of thresholds starting from
    	 * current_threshold+1 and check if a threshold is crossed.
    	 * If none of thresholds above usage is crossed, we read
    	 * only one element of the array here.
    	 */
    	for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
    		eventfd_signal(t->entries[i].eventfd, 1);
    
    	/* Update current_threshold */
    
    	t->current_threshold = i - 1;
    
    unlock:
    	rcu_read_unlock();
    }
    
    static void mem_cgroup_threshold(struct mem_cgroup *memcg)
    {
    
    	while (memcg) {
    		__mem_cgroup_threshold(memcg, false);
    		if (do_swap_account)
    			__mem_cgroup_threshold(memcg, true);
    
    		memcg = parent_mem_cgroup(memcg);
    	}
    
    }
    
    static int compare_thresholds(const void *a, const void *b)
    {
    	const struct mem_cgroup_threshold *_a = a;
    	const struct mem_cgroup_threshold *_b = b;
    
    	return _a->threshold - _b->threshold;
    }
    
    
    static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    {
    	struct mem_cgroup_eventfd_list *ev;
    
    	list_for_each_entry(ev, &mem->oom_notify, list)
    		eventfd_signal(ev->eventfd, 1);
    	return 0;
    }
    
    static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
    {
    
    	struct mem_cgroup *iter;
    
    	for_each_mem_cgroup_tree(iter, mem)
    		mem_cgroup_oom_notify_cb(iter);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    }
    
    static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
    	struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
    
    {
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
    
    	struct mem_cgroup_thresholds *thresholds;
    	struct mem_cgroup_threshold_ary *new;
    
    	int type = MEMFILE_TYPE(cft->private);
    	u64 threshold, usage;
    
    	int i, size, ret;
    
    
    	ret = res_counter_memparse_write_strategy(args, &threshold);
    	if (ret)
    		return ret;
    
    	mutex_lock(&memcg->thresholds_lock);
    
    	if (type == _MEM)
    
    		thresholds = &memcg->thresholds;
    
    	else if (type == _MEMSWAP)
    
    		thresholds = &memcg->memsw_thresholds;
    
    	else
    		BUG();
    
    	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
    
    	/* Check if a threshold crossed before adding a new one */
    
    	if (thresholds->primary)
    
    		__mem_cgroup_threshold(memcg, type == _MEMSWAP);
    
    
    	size = thresholds->primary ? thresholds->primary->size + 1 : 1;
    
    
    	/* Allocate memory for new array of thresholds */
    
    	new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
    
    		ret = -ENOMEM;
    		goto unlock;
    	}
    
    	new->size = size;
    
    
    	/* Copy thresholds (if any) to new array */
    
    	if (thresholds->primary) {
    		memcpy(new->entries, thresholds->primary->entries, (size - 1) *
    
    				sizeof(struct mem_cgroup_threshold));
    
    	/* Add new threshold */
    
    	new->entries[size - 1].eventfd = eventfd;
    	new->entries[size - 1].threshold = threshold;
    
    
    	/* Sort thresholds. Registering of new threshold isn't time-critical */