Skip to content
Snippets Groups Projects
memcontrol.c 188 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
     * to be already dead (as in mem_cgroup_force_empty, for instance).  This is
    
     * from mem_cgroup_count_children(), in the sense that we don't really care how
     * many children we have; we only need to know if we have any.  It also counts
     * any memcg without hierarchy as infertile.
     */
    static inline bool memcg_has_children(struct mem_cgroup *memcg)
    {
    	return memcg->use_hierarchy && __memcg_has_children(memcg);
    }
    
    
    /*
     * Reclaims as many pages from the given memcg as possible and moves
     * the rest to the parent.
     *
     * Caller is responsible for holding css reference for memcg.
     */
    static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
    {
    	int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
    	struct cgroup *cgrp = memcg->css.cgroup;
    
    	/* returns EBUSY if there is a task or if we come here twice. */
    
    	if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
    		return -EBUSY;
    
    
    	/* we call try-to-free pages for make this cgroup empty */
    	lru_add_drain_all();
    
    	/* try to free all pages in this cgroup */
    
    	while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
    
    		progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
    
    			/* maybe some writeback is necessary */
    
    			congestion_wait(BLK_RW_ASYNC, HZ/10);
    
    	lru_add_drain();
    
    	mem_cgroup_reparent_charges(memcg);
    
    	return 0;
    
    static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
    					unsigned int event)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	if (mem_cgroup_is_root(memcg))
    		return -EINVAL;
    
    	return mem_cgroup_force_empty(memcg);
    
    static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
    				     struct cftype *cft)
    
    	return mem_cgroup_from_css(css)->use_hierarchy;
    
    static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
    				      struct cftype *cft, u64 val)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    Tejun Heo's avatar
    Tejun Heo committed
    	struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
    
    	mutex_lock(&memcg_create_mutex);
    
    
    	if (memcg->use_hierarchy == val)
    		goto out;
    
    
    	 * 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_memcg || !parent_memcg->use_hierarchy) &&
    
    				(val == 1 || val == 0)) {
    
    		if (!__memcg_has_children(memcg))
    
    			memcg->use_hierarchy = val;
    
    		else
    			retval = -EBUSY;
    	} else
    		retval = -EINVAL;
    
    	mutex_unlock(&memcg_create_mutex);
    
    static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
    
    					       enum mem_cgroup_stat_index idx)
    
    	struct mem_cgroup *iter;
    
    	/* Per-cpu values can be negative, use a signed accumulator */
    
    	for_each_mem_cgroup_tree(iter, memcg)
    
    		val += mem_cgroup_read_stat(iter, idx);
    
    	if (val < 0) /* race ? */
    		val = 0;
    	return val;
    
    static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
    
    	if (!mem_cgroup_is_root(memcg)) {
    
    			return res_counter_read_u64(&memcg->res, RES_USAGE);
    
    			return res_counter_read_u64(&memcg->memsw, RES_USAGE);
    
    	/*
    	 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
    	 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
    	 */
    
    	val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
    	val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
    
    		val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
    
    static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
    			       struct cftype *cft, struct file *file,
    			       char __user *buf, size_t nbytes, loff_t *ppos)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	int name, len;
    	enum res_type type;
    
    
    	type = MEMFILE_TYPE(cft->private);
    	name = MEMFILE_ATTR(cft->private);
    
    	switch (type) {
    	case _MEM:
    
    			val = mem_cgroup_usage(memcg, false);
    
    			val = res_counter_read_u64(&memcg->res, name);
    
    		break;
    	case _MEMSWAP:
    
    			val = mem_cgroup_usage(memcg, true);
    
    			val = res_counter_read_u64(&memcg->memsw, name);
    
    	case _KMEM:
    		val = res_counter_read_u64(&memcg->kmem, name);
    		break;
    
    
    	len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
    	return simple_read_from_buffer(buf, nbytes, ppos, str, len);
    
    static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
    
    {
    	int ret = -EINVAL;
    #ifdef CONFIG_MEMCG_KMEM
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	/*
    	 * For simplicity, we won't allow this to be disabled.  It also can't
    	 * be changed if the cgroup has children already, or if tasks had
    	 * already joined.
    	 *
    	 * If tasks join before we set the limit, a person looking at
    	 * kmem.usage_in_bytes will have no way to determine when it took
    	 * place, which makes the value quite meaningless.
    	 *
    	 * After it first became limited, changes in the value of the limit are
    	 * of course permitted.
    	 */
    
    	mutex_lock(&memcg_create_mutex);
    
    	mutex_lock(&set_limit_mutex);
    
    	if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
    
    		if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
    
    			ret = -EBUSY;
    			goto out;
    		}
    		ret = res_counter_set_limit(&memcg->kmem, val);
    		VM_BUG_ON(ret);
    
    
    		ret = memcg_update_cache_sizes(memcg);
    		if (ret) {
    
    			res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
    
    		static_key_slow_inc(&memcg_kmem_enabled_key);
    		/*
    		 * setting the active bit after the inc will guarantee no one
    		 * starts accounting before all call sites are patched
    		 */
    		memcg_kmem_set_active(memcg);
    
    	} else
    		ret = res_counter_set_limit(&memcg->kmem, val);
    out:
    	mutex_unlock(&set_limit_mutex);
    
    	mutex_unlock(&memcg_create_mutex);
    
    #ifdef CONFIG_MEMCG_KMEM
    
    static int memcg_propagate_kmem(struct mem_cgroup *memcg)
    
    	struct mem_cgroup *parent = parent_mem_cgroup(memcg);
    	if (!parent)
    
    	memcg->kmem_account_flags = parent->kmem_account_flags;
    
    	/*
    	 * When that happen, we need to disable the static branch only on those
    	 * memcgs that enabled it. To achieve this, we would be forced to
    	 * complicate the code by keeping track of which memcgs were the ones
    	 * that actually enabled limits, and which ones got it from its
    	 * parents.
    	 *
    	 * It is a lot simpler just to do static_key_slow_inc() on every child
    	 * that is accounted.
    	 */
    
    	 * __mem_cgroup_free() will issue static_key_slow_dec() because this
    	 * memcg is active already. If the later initialization fails then the
    	 * cgroup core triggers the cleanup so we do not have to do it here.
    
    	 */
    	static_key_slow_inc(&memcg_kmem_enabled_key);
    
    	mutex_lock(&set_limit_mutex);
    
    	memcg_stop_kmem_account();
    
    	ret = memcg_update_cache_sizes(memcg);
    
    	memcg_resume_kmem_account();
    
    	mutex_unlock(&set_limit_mutex);
    out:
    	return ret;
    
    #endif /* CONFIG_MEMCG_KMEM */
    
    /*
     * The user of this function is...
     * RES_LIMIT.
     */
    
    static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	enum res_type type;
    	int 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 if (type == _MEMSWAP)
    
    			ret = mem_cgroup_resize_memsw_limit(memcg, val);
    
    		else if (type == _KMEM)
    
    			ret = memcg_update_kmem_limit(css, 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)
    {
    	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);
    	if (!memcg->use_hierarchy)
    		goto out;
    
    
    Tejun Heo's avatar
    Tejun Heo committed
    	while (css_parent(&memcg->css)) {
    		memcg = mem_cgroup_from_css(css_parent(&memcg->css));
    
    		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;
    }
    
    
    static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	int name;
    	enum res_type type;
    
    	type = MEMFILE_TYPE(event);
    	name = MEMFILE_ATTR(event);
    
    	switch (name) {
    
    	case RES_MAX_USAGE:
    
    		if (type == _MEM)
    
    			res_counter_reset_max(&memcg->res);
    
    		else if (type == _MEMSWAP)
    
    			res_counter_reset_max(&memcg->memsw);
    
    		else if (type == _KMEM)
    			res_counter_reset_max(&memcg->kmem);
    		else
    			return -EINVAL;
    
    		break;
    	case RES_FAILCNT:
    
    		if (type == _MEM)
    
    			res_counter_reset_failcnt(&memcg->res);
    
    		else if (type == _MEMSWAP)
    
    			res_counter_reset_failcnt(&memcg->memsw);
    
    		else if (type == _KMEM)
    			res_counter_reset_failcnt(&memcg->kmem);
    		else
    			return -EINVAL;
    
    static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
    
    	return mem_cgroup_from_css(css)->move_charge_at_immigrate;
    
    #ifdef CONFIG_MMU
    
    static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
    
    					struct cftype *cft, u64 val)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    
    	if (val >= (1 << NR_MOVE_TYPE))
    		return -EINVAL;
    
    	 * No kind of locking is needed in here, because ->can_attach() will
    	 * check this value once in the beginning of the process, and then carry
    	 * on with stale data. This means that changes to this value will only
    	 * affect task migrations starting after the change.
    
    	memcg->move_charge_at_immigrate = val;
    
    static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
    
    					struct cftype *cft, u64 val)
    {
    	return -ENOSYS;
    }
    #endif
    
    #ifdef CONFIG_NUMA
    
    static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
    				struct cftype *cft, struct seq_file *m)
    
    {
    	int nid;
    	unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
    	unsigned long node_nr;
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
    
    	seq_printf(m, "total=%lu", total_nr);
    
    	for_each_node_state(nid, N_MEMORY) {
    
    		node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
    
    		seq_printf(m, " N%d=%lu", nid, node_nr);
    	}
    	seq_putc(m, '\n');
    
    
    	file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
    
    	seq_printf(m, "file=%lu", file_nr);
    
    	for_each_node_state(nid, N_MEMORY) {
    
    		node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
    
    		seq_printf(m, " N%d=%lu", nid, node_nr);
    	}
    	seq_putc(m, '\n');
    
    
    	anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
    
    	seq_printf(m, "anon=%lu", anon_nr);
    
    	for_each_node_state(nid, N_MEMORY) {
    
    		node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
    
    		seq_printf(m, " N%d=%lu", nid, node_nr);
    	}
    	seq_putc(m, '\n');
    
    
    	unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
    
    	seq_printf(m, "unevictable=%lu", unevictable_nr);
    
    	for_each_node_state(nid, N_MEMORY) {
    
    		node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
    
    		seq_printf(m, " N%d=%lu", nid, node_nr);
    	}
    	seq_putc(m, '\n');
    	return 0;
    }
    #endif /* CONFIG_NUMA */
    
    
    static inline void mem_cgroup_lru_names_not_uptodate(void)
    {
    	BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
    }
    
    
    static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	struct mem_cgroup *mi;
    	unsigned int i;
    
    	for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
    
    		if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
    
    		seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
    			   mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
    
    	for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
    		seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
    			   mem_cgroup_read_events(memcg, i));
    
    	for (i = 0; i < NR_LRU_LISTS; i++)
    		seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
    			   mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
    
    
    	/* Hierarchical information */
    
    	{
    		unsigned long long limit, memsw_limit;
    
    		memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
    
    		seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
    
    		if (do_swap_account)
    
    			seq_printf(m, "hierarchical_memsw_limit %llu\n",
    				   memsw_limit);
    
    	for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
    		long long val = 0;
    
    
    		if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
    
    		for_each_mem_cgroup_tree(mi, memcg)
    			val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
    		seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
    	}
    
    	for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
    		unsigned long long val = 0;
    
    		for_each_mem_cgroup_tree(mi, memcg)
    			val += mem_cgroup_read_events(mi, i);
    		seq_printf(m, "total_%s %llu\n",
    			   mem_cgroup_events_names[i], val);
    	}
    
    	for (i = 0; i < NR_LRU_LISTS; i++) {
    		unsigned long long val = 0;
    
    		for_each_mem_cgroup_tree(mi, memcg)
    			val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
    		seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
    
    #ifdef CONFIG_DEBUG_VM
    	{
    		int nid, zid;
    		struct mem_cgroup_per_zone *mz;
    
    		struct zone_reclaim_stat *rstat;
    
    		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(memcg, nid, zid);
    
    				rstat = &mz->lruvec.reclaim_stat;
    
    				recent_rotated[0] += rstat->recent_rotated[0];
    				recent_rotated[1] += rstat->recent_rotated[1];
    				recent_scanned[0] += rstat->recent_scanned[0];
    				recent_scanned[1] += rstat->recent_scanned[1];
    
    		seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
    		seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
    		seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
    		seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
    
    static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
    				      struct cftype *cft)
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	return mem_cgroup_swappiness(memcg);
    
    static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
    				       struct cftype *cft, u64 val)
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    Tejun Heo's avatar
    Tejun Heo committed
    	struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
    
    Tejun Heo's avatar
    Tejun Heo committed
    	if (val > 100 || !parent)
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    		return -EINVAL;
    
    
    	mutex_lock(&memcg_create_mutex);
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	/* If under hierarchy, only empty-root can set this value */
    
    	if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
    
    		mutex_unlock(&memcg_create_mutex);
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    		return -EINVAL;
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    
    	memcg->swappiness = val;
    
    
    	mutex_unlock(&memcg_create_mutex);
    
    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 or equal to 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;
    
    
    	if (_a->threshold > _b->threshold)
    		return 1;
    
    	if (_a->threshold < _b->threshold)
    		return -1;
    
    	return 0;
    
    static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    {
    	struct mem_cgroup_eventfd_list *ev;
    
    
    	list_for_each_entry(ev, &memcg->oom_notify, list)
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		eventfd_signal(ev->eventfd, 1);
    	return 0;
    }
    
    
    static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
    
    	struct mem_cgroup *iter;
    
    
    	for_each_mem_cgroup_tree(iter, memcg)
    
    		mem_cgroup_oom_notify_cb(iter);
    
    static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	struct mem_cgroup_thresholds *thresholds;
    	struct mem_cgroup_threshold_ary *new;
    
    	enum res_type 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_subsys_state *css,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	struct mem_cgroup_thresholds *thresholds;
    	struct mem_cgroup_threshold_ary *new;
    
    	enum res_type type = MEMFILE_TYPE(cft->private);
    
    	int i, j, size;
    
    
    	mutex_lock(&memcg->thresholds_lock);
    	if (type == _MEM)
    
    		thresholds = &memcg->thresholds;
    
    	else if (type == _MEMSWAP)
    
    		thresholds = &memcg->memsw_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;
    
    	/* If all events are unregistered, free the spare array */
    	if (!new) {
    		kfree(thresholds->spare);
    		thresholds->spare = NULL;
    	}
    
    
    	rcu_assign_pointer(thresholds->primary, new);
    
    	/* To be sure that nobody uses thresholds */
    
    	synchronize_rcu();
    
    	mutex_unlock(&memcg->thresholds_lock);
    }
    
    static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct mem_cgroup_eventfd_list *event;
    
    	enum res_type type = MEMFILE_TYPE(cft->private);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    
    	BUG_ON(type != _OOM_TYPE);
    	event = kmalloc(sizeof(*event),	GFP_KERNEL);
    	if (!event)
    		return -ENOMEM;
    
    
    	spin_lock(&memcg_oom_lock);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    
    	event->eventfd = eventfd;
    	list_add(&event->list, &memcg->oom_notify);
    
    	/* already in OOM ? */
    
    	if (atomic_read(&memcg->under_oom))
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		eventfd_signal(eventfd, 1);
    
    	spin_unlock(&memcg_oom_lock);
    
    static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct mem_cgroup_eventfd_list *ev, *tmp;
    
    	enum res_type type = MEMFILE_TYPE(cft->private);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    
    	BUG_ON(type != _OOM_TYPE);
    
    
    	spin_lock(&memcg_oom_lock);
    
    	list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		if (ev->eventfd == eventfd) {
    			list_del(&ev->list);
    			kfree(ev);
    		}
    	}
    
    
    	spin_unlock(&memcg_oom_lock);
    
    static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
    
    	struct cftype *cft,  struct cgroup_map_cb *cb)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
    
    	if (atomic_read(&memcg->under_oom))
    
    		cb->fill(cb, "under_oom", 1);
    	else
    		cb->fill(cb, "under_oom", 0);
    	return 0;
    }
    
    
    static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
    
    	struct cftype *cft, u64 val)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    Tejun Heo's avatar
    Tejun Heo committed
    	struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
    
    
    	/* cannot set to root cgroup and only 0 and 1 are allowed */
    
    Tejun Heo's avatar
    Tejun Heo committed
    	if (!parent || !((val == 0) || (val == 1)))
    
    	mutex_lock(&memcg_create_mutex);
    
    	/* oom-kill-disable is a flag for subhierarchy. */
    
    	if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
    
    		mutex_unlock(&memcg_create_mutex);
    
    	memcg->oom_kill_disable = val;
    
    		memcg_oom_recover(memcg);
    
    	mutex_unlock(&memcg_create_mutex);
    
    #ifdef CONFIG_MEMCG_KMEM
    
    static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
    
    	ret = memcg_propagate_kmem(memcg);
    	if (ret)
    		return ret;
    
    	return mem_cgroup_sockets_init(memcg, ss);
    
    static void memcg_destroy_kmem(struct mem_cgroup *memcg)
    
    }
    
    static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
    {
    	if (!memcg_kmem_is_active(memcg))
    		return;
    
    	/*
    	 * kmem charges can outlive the cgroup. In the case of slab
    	 * pages, for instance, a page contain objects from various
    	 * processes. As we prevent from taking a reference for every
    	 * such allocation we have to be careful when doing uncharge
    	 * (see memcg_uncharge_kmem) and here during offlining.
    	 *
    	 * The idea is that that only the _last_ uncharge which sees
    	 * the dead memcg will drop the last reference. An additional
    	 * reference is taken here before the group is marked dead
    	 * which is then paired with css_put during uncharge resp. here.
    	 *
    	 * Although this might sound strange as this path is called from
    	 * css_offline() when the referencemight have dropped down to 0
    	 * and shouldn't be incremented anymore (css_tryget would fail)
    	 * we do not have other options because of the kmem allocations
    	 * lifetime.
    	 */
    	css_get(&memcg->css);
    
    
    	memcg_kmem_mark_dead(memcg);
    
    	if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
    		return;
    
    	if (memcg_kmem_test_and_clear_dead(memcg))
    
    		css_put(&memcg->css);
    
    static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
    
    static void memcg_destroy_kmem(struct mem_cgroup *memcg)
    {
    }
    
    static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
    
    static struct cftype mem_cgroup_files[] = {
    	{
    
    		.name = "usage_in_bytes",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
    
    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,