Skip to content
Snippets Groups Projects
memcontrol.c 178 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	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,
    
    		.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 = memcg_stat_show,
    
    	{
    		.name = "force_empty",
    		.trigger = mem_cgroup_force_empty_write,
    	},
    
    	{
    		.name = "use_hierarchy",
    
    		.flags = CFTYPE_INSANE,
    
    		.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),
    	},
    
    	{
    		.name = "pressure_level",
    		.register_event = vmpressure_register_event,
    		.unregister_event = vmpressure_unregister_event,
    	},
    
    #ifdef CONFIG_NUMA
    	{
    		.name = "numa_stat",
    
    		.read_seq_string = memcg_numa_stat_show,
    
    #ifdef CONFIG_MEMCG_KMEM
    	{
    		.name = "kmem.limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
    		.write_string = mem_cgroup_write,
    		.read = mem_cgroup_read,
    	},
    	{
    		.name = "kmem.usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
    		.read = mem_cgroup_read,
    	},
    	{
    		.name = "kmem.failcnt",
    		.private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
    		.trigger = mem_cgroup_reset,
    		.read = mem_cgroup_read,
    	},
    	{
    		.name = "kmem.max_usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
    		.trigger = mem_cgroup_reset,
    		.read = mem_cgroup_read,
    	},
    
    #ifdef CONFIG_SLABINFO
    	{
    		.name = "kmem.slabinfo",
    		.read_seq_string = mem_cgroup_slabinfo_read,
    	},
    #endif
    
    #ifdef CONFIG_MEMCG_SWAP
    static struct cftype memsw_cgroup_files[] = {
    	{
    		.name = "memsw.usage_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
    		.read = mem_cgroup_read,
    		.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 = mem_cgroup_read,
    	},
    	{
    		.name = "memsw.limit_in_bytes",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
    		.write_string = mem_cgroup_write,
    		.read = mem_cgroup_read,
    	},
    	{
    		.name = "memsw.failcnt",
    		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
    		.trigger = mem_cgroup_reset,
    		.read = mem_cgroup_read,
    	},
    	{ },	/* terminate */
    };
    #endif
    
    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);
    
    		mz->memcg = memcg;
    
    	memcg->nodeinfo[node] = pn;
    
    static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
    
    	kfree(memcg->nodeinfo[node]);
    
    static struct mem_cgroup *mem_cgroup_alloc(void)
    {
    
    	struct mem_cgroup *memcg;
    
    	size_t size = memcg_size();
    
    	/* Can be very big if nr_node_ids 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)
    
     * 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)
    
    	size_t size = memcg_size();
    
    	free_css_id(&mem_cgroup_subsys, &memcg->css);
    
    	for_each_node(node)
    		free_mem_cgroup_per_zone_info(memcg, node);
    
    	free_percpu(memcg->stat);
    
    
    	/*
    	 * 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_static_keys(memcg);
    
    	if (size < PAGE_SIZE)
    		kfree(memcg);
    	else
    		vfree(memcg);
    
    /*
     * 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);
    
    Li Zefan's avatar
    Li Zefan committed
    static struct cgroup_subsys_state * __ref
    
    mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
    
    	struct mem_cgroup *memcg;
    
    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))
    
    		root_mem_cgroup = memcg;
    
    		res_counter_init(&memcg->res, NULL);
    		res_counter_init(&memcg->memsw, NULL);
    		res_counter_init(&memcg->kmem, NULL);
    
    	memcg->last_scanned_node = MAX_NUMNODES;
    	INIT_LIST_HEAD(&memcg->oom_notify);
    	memcg->move_charge_at_immigrate = 0;
    	mutex_init(&memcg->thresholds_lock);
    	spin_lock_init(&memcg->move_lock);
    
    	vmpressure_init(&memcg->vmpressure);
    
    
    	return &memcg->css;
    
    free_out:
    	__mem_cgroup_free(memcg);
    	return ERR_PTR(error);
    }
    
    static int
    
    mem_cgroup_css_online(struct cgroup_subsys_state *css)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    	struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
    
    Tejun Heo's avatar
    Tejun Heo committed
    	if (!parent)
    
    	mutex_lock(&memcg_create_mutex);
    
    
    	memcg->use_hierarchy = parent->use_hierarchy;
    	memcg->oom_kill_disable = parent->oom_kill_disable;
    	memcg->swappiness = mem_cgroup_swappiness(parent);
    
    	if (parent->use_hierarchy) {
    
    		res_counter_init(&memcg->res, &parent->res);
    		res_counter_init(&memcg->memsw, &parent->memsw);
    
    		res_counter_init(&memcg->kmem, &parent->kmem);
    
    		 * No need to take a reference to the parent because cgroup
    		 * core guarantees its existence.
    
    		res_counter_init(&memcg->res, NULL);
    		res_counter_init(&memcg->memsw, NULL);
    
    		res_counter_init(&memcg->kmem, NULL);
    
    		/*
    		 * Deeper hierachy with use_hierarchy == false doesn't make
    		 * much sense so let cgroup subsystem know about this
    		 * unfortunate state in our controller.
    		 */
    
    		if (parent != root_mem_cgroup)
    
    
    	error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
    
    	mutex_unlock(&memcg_create_mutex);
    
    /*
     * Announce all parents that a group from their hierarchy is gone.
     */
    static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
    {
    	struct mem_cgroup *parent = memcg;
    
    	while ((parent = parent_mem_cgroup(parent)))
    
    		mem_cgroup_iter_invalidate(parent);
    
    
    	/*
    	 * if the root memcg is not hierarchical we have to check it
    	 * explicitely.
    	 */
    	if (!root_mem_cgroup->use_hierarchy)
    
    		mem_cgroup_iter_invalidate(root_mem_cgroup);
    
    static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	kmem_cgroup_css_offline(memcg);
    
    
    	mem_cgroup_invalidate_reclaim_iterators(memcg);
    
    	mem_cgroup_reparent_charges(memcg);
    
    	mem_cgroup_destroy_all_caches(memcg);
    
    	vmpressure_cleanup(&memcg->vmpressure);
    
    static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
    
    	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    
    	memcg_destroy_kmem(memcg);
    
    	__mem_cgroup_free(memcg);