Skip to content
Snippets Groups Projects
memcontrol.c 120 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			/* 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;
    }
    
    
    struct mem_cgroup_idx_data {
    	s64 val;
    	enum mem_cgroup_stat_index idx;
    };
    
    static int
    mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
    {
    	struct mem_cgroup_idx_data *d = data;
    
    	d->val += mem_cgroup_read_stat(mem, d->idx);
    
    	return 0;
    }
    
    static void
    mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
    				enum mem_cgroup_stat_index idx, s64 *val)
    {
    	struct mem_cgroup_idx_data d;
    	d.idx = idx;
    	d.val = 0;
    	mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
    	*val = d.val;
    }
    
    
    static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
    {
    	u64 idx_val, val;
    
    	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);
    	}
    
    	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
    	val = idx_val;
    	mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
    	val += idx_val;
    
    	if (swap) {
    		mem_cgroup_get_recursive_idx_stat(mem,
    				MEM_CGROUP_STAT_SWAPOUT, &idx_val);
    		val += idx_val;
    	}
    
    	return val << PAGE_SHIFT;
    }
    
    
    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 int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
    {
    	struct mcs_total_stat *s = data;
    	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;
    	return 0;
    }
    
    static void
    mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
    {
    	mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
    }
    
    
    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
    
    	spin_lock(&memcg->reclaim_param_lock);
    	memcg->swappiness = val;
    	spin_unlock(&memcg->reclaim_param_lock);
    
    
    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)
    {
    	__mem_cgroup_threshold(memcg, false);
    	if (do_swap_account)
    		__mem_cgroup_threshold(memcg, true);
    }
    
    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;
    }
    
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
    {
    	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)
    {
    	mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
    }
    
    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 */
    
    	sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
    
    			compare_thresholds, NULL);
    
    	/* Find current threshold */
    
    	new->current_threshold = -1;
    
    	for (i = 0; i < size; i++) {
    
    		if (new->entries[i].threshold < usage) {
    
    			 * new->current_threshold will not be used until
    			 * rcu_assign_pointer(), so it's safe to increment
    
    			++new->current_threshold;
    
    	/* Free old spare buffer and save old primary buffer as spare */
    	kfree(thresholds->spare);
    	thresholds->spare = thresholds->primary;
    
    	rcu_assign_pointer(thresholds->primary, new);
    
    	/* To be sure that nobody uses thresholds */
    
    	synchronize_rcu();
    
    unlock:
    	mutex_unlock(&memcg->thresholds_lock);
    
    	return ret;
    }
    
    
    static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd)
    
    {
    	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 usage;
    
    	int i, j, size;
    
    
    	mutex_lock(&memcg->thresholds_lock);
    	if (type == _MEM)
    
    		thresholds = &memcg->thresholds;
    
    	else if (type == _MEMSWAP)
    
    		thresholds = &memcg->memsw_thresholds;
    
    	else
    		BUG();
    
    	/*
    	 * Something went wrong if we trying to unregister a threshold
    	 * if we don't have thresholds
    	 */
    	BUG_ON(!thresholds);
    
    	usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
    
    	/* Check if a threshold crossed before removing */
    	__mem_cgroup_threshold(memcg, type == _MEMSWAP);
    
    	/* Calculate new number of threshold */
    
    	size = 0;
    	for (i = 0; i < thresholds->primary->size; i++) {
    		if (thresholds->primary->entries[i].eventfd != eventfd)
    
    	new = thresholds->spare;
    
    	/* Set thresholds array to NULL if we don't have thresholds */
    	if (!size) {
    
    		kfree(new);
    		new = NULL;
    
    	new->size = size;
    
    
    	/* Copy thresholds and find current threshold */
    
    	new->current_threshold = -1;
    	for (i = 0, j = 0; i < thresholds->primary->size; i++) {
    		if (thresholds->primary->entries[i].eventfd == eventfd)
    
    		new->entries[j] = thresholds->primary->entries[i];
    		if (new->entries[j].threshold < usage) {
    
    			 * new->current_threshold will not be used
    
    			 * until rcu_assign_pointer(), so it's safe to increment
    			 * it here.
    			 */
    
    			++new->current_threshold;
    
    	/* Swap primary and spare array */
    	thresholds->spare = thresholds->primary;
    	rcu_assign_pointer(thresholds->primary, new);
    
    	/* To be sure that nobody uses thresholds */
    
    	synchronize_rcu();
    
    	mutex_unlock(&memcg->thresholds_lock);
    }
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    static int mem_cgroup_oom_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_eventfd_list *event;
    	int type = MEMFILE_TYPE(cft->private);
    
    	BUG_ON(type != _OOM_TYPE);
    	event = kmalloc(sizeof(*event),	GFP_KERNEL);
    	if (!event)
    		return -ENOMEM;
    
    	mutex_lock(&memcg_oom_mutex);
    
    	event->eventfd = eventfd;
    	list_add(&event->list, &memcg->oom_notify);
    
    	/* already in OOM ? */
    	if (atomic_read(&memcg->oom_lock))
    		eventfd_signal(eventfd, 1);
    	mutex_unlock(&memcg_oom_mutex);
    
    	return 0;
    }
    
    
    static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd)
    {
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
    	struct mem_cgroup_eventfd_list *ev, *tmp;
    	int type = MEMFILE_TYPE(cft->private);
    
    	BUG_ON(type != _OOM_TYPE);
    
    	mutex_lock(&memcg_oom_mutex);
    
    	list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
    		if (ev->eventfd == eventfd) {
    			list_del(&ev->list);
    			kfree(ev);
    		}
    	}
    
    	mutex_unlock(&memcg_oom_mutex);
    }
    
    
    static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
    	struct cftype *cft,  struct cgroup_map_cb *cb)
    {
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
    
    	cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
    
    	if (atomic_read(&mem->oom_lock))
    		cb->fill(cb, "under_oom", 1);
    	else
    		cb->fill(cb, "under_oom", 0);
    	return 0;
    }
    
    static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
    	struct cftype *cft, u64 val)
    {
    	struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
    	struct mem_cgroup *parent;
    
    	/* cannot set to root cgroup and only 0 and 1 are allowed */
    	if (!cgrp->parent || !((val == 0) || (val == 1)))
    		return -EINVAL;
    
    	parent = mem_cgroup_from_cont(cgrp->parent);
    
    	cgroup_lock();
    	/* oom-kill-disable is a flag for subhierarchy. */
    	if ((parent->use_hierarchy) ||
    	    (mem->use_hierarchy && !list_empty(&cgrp->children))) {
    		cgroup_unlock();
    		return -EINVAL;
    	}
    	mem->oom_kill_disable = val;
    
    	if (!val)
    		memcg_oom_recover(mem);
    
    static struct cftype mem_cgroup_files[] = {
    	{
    
    		.name = "usage_in_bytes",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
    
    		.read_u64 = mem_cgroup_read,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		.register_event = mem_cgroup_usage_register_event,
    		.unregister_event = mem_cgroup_usage_unregister_event,
    
    	{
    		.name = "max_usage_in_bytes",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
    
    		.trigger = mem_cgroup_reset,
    
    		.read_u64 = mem_cgroup_read,
    	},
    
    		.name = "limit_in_bytes",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
    
    		.read_u64 = mem_cgroup_read,
    
    	{
    		.name = "soft_limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
    		.write_string = mem_cgroup_write,
    		.read_u64 = mem_cgroup_read,
    	},
    
    	{
    		.name = "failcnt",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
    
    		.trigger = mem_cgroup_reset,
    
    		.read_u64 = mem_cgroup_read,
    
    		.read_map = mem_control_stat_show,
    
    	{
    		.name = "force_empty",
    		.trigger = mem_cgroup_force_empty_write,
    	},
    
    	{
    		.name = "use_hierarchy",
    		.write_u64 = mem_cgroup_hierarchy_write,
    		.read_u64 = mem_cgroup_hierarchy_read,
    	},
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	{
    		.name = "swappiness",
    		.read_u64 = mem_cgroup_swappiness_read,
    		.write_u64 = mem_cgroup_swappiness_write,
    	},
    
    	{
    		.name = "move_charge_at_immigrate",
    		.read_u64 = mem_cgroup_move_charge_read,
    		.write_u64 = mem_cgroup_move_charge_write,
    	},
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	{
    		.name = "oom_control",
    
    		.read_map = mem_cgroup_oom_control_read,
    		.write_u64 = mem_cgroup_oom_control_write,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		.register_event = mem_cgroup_oom_register_event,
    		.unregister_event = mem_cgroup_oom_unregister_event,
    		.private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
    	},
    
    #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
    static struct cftype memsw_cgroup_files[] = {
    	{
    		.name = "memsw.usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
    		.read_u64 = mem_cgroup_read,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		.register_event = mem_cgroup_usage_register_event,
    		.unregister_event = mem_cgroup_usage_unregister_event,
    
    	},
    	{
    		.name = "memsw.max_usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
    		.trigger = mem_cgroup_reset,
    		.read_u64 = mem_cgroup_read,
    	},
    	{
    		.name = "memsw.limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
    		.write_string = mem_cgroup_write,
    		.read_u64 = mem_cgroup_read,
    	},
    	{
    		.name = "memsw.failcnt",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
    		.trigger = mem_cgroup_reset,
    		.read_u64 = mem_cgroup_read,
    	},
    };
    
    static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
    {
    	if (!do_swap_account)
    		return 0;
    	return cgroup_add_files(cont, ss, memsw_cgroup_files,
    				ARRAY_SIZE(memsw_cgroup_files));
    };
    #else
    static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
    {
    	return 0;
    }
    #endif
    
    
    static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
    {
    	struct mem_cgroup_per_node *pn;
    
    	int zone, tmp = node;
    
    	/*
    	 * This routine is called against possible nodes.
    	 * But it's BUG to call kmalloc() against offline node.
    	 *
    	 * TODO: this routine can waste much memory for nodes which will
    	 *       never be onlined. It's better to use memory hotplug callback
    	 *       function.
    	 */
    
    	if (!node_state(node, N_NORMAL_MEMORY))
    		tmp = -1;
    	pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
    
    	mem->info.nodeinfo[node] = pn;
    	memset(pn, 0, sizeof(*pn));
    
    
    	for (zone = 0; zone < MAX_NR_ZONES; zone++) {
    		mz = &pn->zoneinfo[zone];
    
    		for_each_lru(l)
    			INIT_LIST_HEAD(&mz->lists[l]);
    
    		mz->usage_in_excess = 0;
    
    		mz->on_tree = false;
    		mz->mem = mem;