Skip to content
Snippets Groups Projects
memcontrol.c 143 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	memcg->move_charge_at_immigrate = val;
    
    #else
    static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
    					struct cftype *cft, u64 val)
    {
    	return -ENOSYS;
    }
    #endif
    
    #ifdef CONFIG_NUMA
    
    static int mem_control_numa_stat_show(struct cgroup *cont, 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_cont(cont);
    
    	total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
    
    	seq_printf(m, "total=%lu", total_nr);
    	for_each_node_state(nid, N_HIGH_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_HIGH_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_HIGH_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_HIGH_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 const char * const mem_cgroup_lru_names[] = {
    	"inactive_anon",
    	"active_anon",
    	"inactive_file",
    	"active_file",
    	"unevictable",
    };
    
    static inline void mem_cgroup_lru_names_not_uptodate(void)
    {
    	BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
    }
    
    
    static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
    
    	struct mem_cgroup *mi;
    	unsigned int i;
    
    	for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
    		if (i == MEM_CGROUP_STAT_SWAPOUT && !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_SWAPOUT && !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]);
    
    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 mem_cgroup_swappiness(memcg);
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    }
    
    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 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;
    
    	return _a->threshold - _b->threshold;
    }
    
    
    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);
    
    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 */
    
    	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;
    
    	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);
    }
    
    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;
    
    
    	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 *cgrp,
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct cftype *cft, struct eventfd_ctx *eventfd)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	struct mem_cgroup_eventfd_list *ev, *tmp;
    	int type = MEMFILE_TYPE(cft->private);
    
    	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 *cgrp,
    	struct cftype *cft,  struct cgroup_map_cb *cb)
    {
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
    
    	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 *cgrp,
    	struct cftype *cft, u64 val)
    {
    
    	struct mem_cgroup *memcg = 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) ||
    
    	    (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
    
    		cgroup_unlock();
    		return -EINVAL;
    	}
    
    	memcg->oom_kill_disable = val;
    
    		memcg_oom_recover(memcg);
    
    #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
    
    static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
    
    	return mem_cgroup_sockets_init(memcg, ss);
    
    static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
    
    static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
    
    static void kmem_cgroup_destroy(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,
    
    		.name = "limit_in_bytes",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
    
    	{
    		.name = "soft_limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
    		.write_string = mem_cgroup_write,
    
    	{
    		.name = "failcnt",
    
    		.private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
    
    		.trigger = mem_cgroup_reset,
    
    		.read_seq_string = 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_NUMA
    	{
    		.name = "numa_stat",
    
    		.read_seq_string = mem_control_numa_stat_show,
    
    #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
    	{
    		.name = "memsw.usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
    
    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,
    
    	},
    	{
    		.name = "memsw.limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
    		.write_string = mem_cgroup_write,
    
    	},
    	{
    		.name = "memsw.failcnt",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
    		.trigger = mem_cgroup_reset,
    
    static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
    
    	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 = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
    
    
    	for (zone = 0; zone < MAX_NR_ZONES; zone++) {
    		mz = &pn->zoneinfo[zone];
    
    		lruvec_init(&mz->lruvec, &NODE_DATA(node)->node_zones[zone]);
    
    		mz->usage_in_excess = 0;
    
    		mz->memcg = memcg;
    
    	memcg->info.nodeinfo[node] = pn;
    
    static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
    
    	kfree(memcg->info.nodeinfo[node]);
    
    static struct mem_cgroup *mem_cgroup_alloc(void)
    {
    
    	struct mem_cgroup *memcg;
    
    	int size = sizeof(struct mem_cgroup);
    
    	/* Can be very big if MAX_NUMNODES is very big */
    
    	if (size < PAGE_SIZE)
    
    		memcg = kzalloc(size, GFP_KERNEL);
    
    		memcg = vzalloc(size);
    
    	memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
    	if (!memcg->stat)
    
    	spin_lock_init(&memcg->pcp_counter_lock);
    	return memcg;
    
    
    out_free:
    	if (size < PAGE_SIZE)
    
     * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
    
     * but in process context.  The work_freeing structure is overlaid
     * on the rcu_freeing structure, which itself is overlaid on memsw.
     */
    
    static void free_work(struct work_struct *work)
    
    {
    	struct mem_cgroup *memcg;
    
    	int size = sizeof(struct mem_cgroup);
    
    
    	memcg = container_of(work, struct mem_cgroup, work_freeing);
    
    	/*
    	 * We need to make sure that (at least for now), the jump label
    	 * destruction code runs outside of the cgroup lock. This is because
    	 * get_online_cpus(), which is called from the static_branch update,
    	 * can't be called inside the cgroup_lock. cpusets are the ones
    	 * enforcing this dependency, so if they ever change, we might as well.
    	 *
    	 * schedule_work() will guarantee this happens. Be careful if you need
    	 * to move this code around, and make sure it is outside
    	 * the cgroup_lock.
    	 */
    	disarm_sock_keys(memcg);
    
    	if (size < PAGE_SIZE)
    		kfree(memcg);
    	else
    		vfree(memcg);
    
    
    static void free_rcu(struct rcu_head *rcu_head)
    
    {
    	struct mem_cgroup *memcg;
    
    	memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
    
    	INIT_WORK(&memcg->work_freeing, free_work);
    
    	schedule_work(&memcg->work_freeing);
    }
    
    
    /*
     * At destroying mem_cgroup, references from swap_cgroup can remain.
     * (scanning all at force_empty is too costly...)
     *
     * Instead of clearing all references at force_empty, we remember
     * the number of reference from swap_cgroup and free mem_cgroup when
     * it goes down to 0.
     *
     * Removal of cgroup itself succeeds regardless of refs from swap.
     */
    
    
    static void __mem_cgroup_free(struct mem_cgroup *memcg)
    
    	mem_cgroup_remove_from_trees(memcg);
    	free_css_id(&mem_cgroup_subsys, &memcg->css);
    
    	for_each_node(node)
    
    		free_mem_cgroup_per_zone_info(memcg, node);
    
    	free_percpu(memcg->stat);
    
    	call_rcu(&memcg->rcu_freeing, free_rcu);
    
    static void mem_cgroup_get(struct mem_cgroup *memcg)
    
    	atomic_inc(&memcg->refcnt);
    
    static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
    
    	if (atomic_sub_and_test(count, &memcg->refcnt)) {
    		struct mem_cgroup *parent = parent_mem_cgroup(memcg);
    		__mem_cgroup_free(memcg);
    
    		if (parent)
    			mem_cgroup_put(parent);
    	}
    
    static void mem_cgroup_put(struct mem_cgroup *memcg)
    
    	__mem_cgroup_put(memcg, 1);
    
    /*
     * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
     */
    
    struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
    
    	if (!memcg->res.parent)
    
    	return mem_cgroup_from_res_counter(memcg->res.parent, res);
    
    EXPORT_SYMBOL(parent_mem_cgroup);
    
    #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
    static void __init enable_swap_cgroup(void)
    {
    
    	if (!mem_cgroup_disabled() && really_do_swap_account)
    
    		do_swap_account = 1;
    }
    #else
    static void __init enable_swap_cgroup(void)
    {
    }
    #endif
    
    
    static int mem_cgroup_soft_limit_tree_init(void)
    {
    	struct mem_cgroup_tree_per_node *rtpn;
    	struct mem_cgroup_tree_per_zone *rtpz;
    	int tmp, node, zone;
    
    
    	for_each_node(node) {
    
    		tmp = node;
    		if (!node_state(node, N_NORMAL_MEMORY))
    			tmp = -1;
    		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
    		if (!rtpn)
    
    
    		soft_limit_tree.rb_tree_per_node[node] = rtpn;
    
    		for (zone = 0; zone < MAX_NR_ZONES; zone++) {
    			rtpz = &rtpn->rb_tree_per_zone[zone];
    			rtpz->rb_root = RB_ROOT;
    			spin_lock_init(&rtpz->lock);
    		}
    	}
    	return 0;
    
    	for_each_node(node) {
    
    		if (!soft_limit_tree.rb_tree_per_node[node])
    			break;
    		kfree(soft_limit_tree.rb_tree_per_node[node]);
    		soft_limit_tree.rb_tree_per_node[node] = NULL;
    	}
    	return 1;
    
    
    Li Zefan's avatar
    Li Zefan committed
    static struct cgroup_subsys_state * __ref
    
    mem_cgroup_create(struct cgroup *cont)
    
    	struct mem_cgroup *memcg, *parent;
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	long error = -ENOMEM;
    
    	memcg = mem_cgroup_alloc();
    	if (!memcg)
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    		return ERR_PTR(error);
    
    	for_each_node(node)
    
    		if (alloc_mem_cgroup_per_zone_info(memcg, node))
    
    	if (cont->parent == NULL) {
    
    		enable_swap_cgroup();
    
    		if (mem_cgroup_soft_limit_tree_init())
    			goto free_out;
    
    		root_mem_cgroup = memcg;
    
    		for_each_possible_cpu(cpu) {
    			struct memcg_stock_pcp *stock =
    						&per_cpu(memcg_stock, cpu);
    			INIT_WORK(&stock->work, drain_local_stock);
    		}
    
    		hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
    
    		parent = mem_cgroup_from_cont(cont->parent);
    
    		memcg->use_hierarchy = parent->use_hierarchy;
    		memcg->oom_kill_disable = parent->oom_kill_disable;
    
    	if (parent && parent->use_hierarchy) {
    
    		res_counter_init(&memcg->res, &parent->res);
    		res_counter_init(&memcg->memsw, &parent->memsw);
    
    		/*
    		 * We increment refcnt of the parent to ensure that we can
    		 * safely access it on res_counter_charge/uncharge.
    		 * This refcnt will be decremented when freeing this
    		 * mem_cgroup(see mem_cgroup_put).
    		 */
    		mem_cgroup_get(parent);
    
    		res_counter_init(&memcg->res, NULL);
    		res_counter_init(&memcg->memsw, NULL);
    
    	memcg->last_scanned_node = MAX_NUMNODES;
    	INIT_LIST_HEAD(&memcg->oom_notify);
    
    KOSAKI Motohiro's avatar
    KOSAKI Motohiro committed
    	if (parent)
    
    		memcg->swappiness = mem_cgroup_swappiness(parent);
    	atomic_set(&memcg->refcnt, 1);
    	memcg->move_charge_at_immigrate = 0;
    	mutex_init(&memcg->thresholds_lock);
    
    	spin_lock_init(&memcg->move_lock);
    
    
    	error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
    	if (error) {
    		/*
    		 * We call put now because our (and parent's) refcnts
    		 * are already in place. mem_cgroup_put() will internally
    		 * call __mem_cgroup_free, so return directly
    		 */
    		mem_cgroup_put(memcg);
    		return ERR_PTR(error);
    	}
    
    	return &memcg->css;
    
    	__mem_cgroup_free(memcg);
    
    KAMEZAWA Hiroyuki's avatar
    KAMEZAWA Hiroyuki committed
    	return ERR_PTR(error);
    
    static int mem_cgroup_pre_destroy(struct cgroup *cont)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
    
    	return mem_cgroup_force_empty(memcg, false);
    
    static void mem_cgroup_destroy(struct cgroup *cont)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
    
    	mem_cgroup_put(memcg);
    
    #ifdef CONFIG_MMU
    
    /* Handlers for move charge at task migration. */
    
    #define PRECHARGE_COUNT_AT_ONCE	256
    static int mem_cgroup_do_precharge(unsigned long count)
    
    	int ret = 0;
    	int batch_count = PRECHARGE_COUNT_AT_ONCE;
    
    	struct mem_cgroup *memcg = mc.to;
    
    	if (mem_cgroup_is_root(memcg)) {
    
    		mc.precharge += count;
    		/* we don't need css_get for root */
    		return ret;
    	}
    	/* try to charge at once */
    	if (count > 1) {
    		struct res_counter *dummy;
    		/*
    
    		 * "memcg" cannot be under rmdir() because we've already checked
    
    		 * by cgroup_lock_live_cgroup() that it is not removed and we
    		 * are still under the same cgroup_mutex. So we can postpone
    		 * css_get().
    		 */
    
    		if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
    
    		if (do_swap_account && res_counter_charge(&memcg->memsw,
    
    						PAGE_SIZE * count, &dummy)) {
    
    			res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
    
    			goto one_by_one;
    		}
    		mc.precharge += count;
    		return ret;
    	}
    one_by_one:
    	/* fall back to one by one charge */
    	while (count--) {
    		if (signal_pending(current)) {
    			ret = -EINTR;
    			break;
    		}
    		if (!batch_count--) {
    			batch_count = PRECHARGE_COUNT_AT_ONCE;
    			cond_resched();
    		}
    
    		ret = __mem_cgroup_try_charge(NULL,
    					GFP_KERNEL, 1, &memcg, false);
    
    			/* mem_cgroup_clear_mc() will do uncharge later */