tcp_memcontrol.c 4.8 KB
Newer Older
G
Glauber Costa 已提交
1 2 3
#include <net/tcp.h>
#include <net/tcp_memcontrol.h>
#include <net/sock.h>
G
Glauber Costa 已提交
4 5
#include <net/ip.h>
#include <linux/nsproxy.h>
G
Glauber Costa 已提交
6 7 8
#include <linux/memcontrol.h>
#include <linux/module.h>

9
int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
G
Glauber Costa 已提交
10 11
{
	/*
12
	 * The root cgroup does not use page_counters, but rather,
G
Glauber Costa 已提交
13 14 15 16
	 * rely on the data already collected by the network
	 * subsystem
	 */
	struct mem_cgroup *parent = parent_mem_cgroup(memcg);
17 18
	struct page_counter *counter_parent = NULL;
	struct cg_proto *cg_proto, *parent_cg;
G
Glauber Costa 已提交
19 20 21

	cg_proto = tcp_prot.proto_cgroup(memcg);
	if (!cg_proto)
22
		return 0;
G
Glauber Costa 已提交
23

24 25
	cg_proto->memory_pressure = 0;
	cg_proto->memcg = memcg;
G
Glauber Costa 已提交
26 27 28

	parent_cg = tcp_prot.proto_cgroup(parent);
	if (parent_cg)
29
		counter_parent = &parent_cg->memory_allocated;
G
Glauber Costa 已提交
30

31
	page_counter_init(&cg_proto->memory_allocated, counter_parent);
G
Glauber Costa 已提交
32

33
	return 0;
G
Glauber Costa 已提交
34 35 36
}
EXPORT_SYMBOL(tcp_init_cgroup);

37
void tcp_destroy_cgroup(struct mem_cgroup *memcg)
G
Glauber Costa 已提交
38 39 40 41 42 43 44
{
	struct cg_proto *cg_proto;

	cg_proto = tcp_prot.proto_cgroup(memcg);
	if (!cg_proto)
		return;

45
	if (cg_proto->active)
46 47
		static_key_slow_dec(&memcg_socket_limit_enabled);

G
Glauber Costa 已提交
48 49
}
EXPORT_SYMBOL(tcp_destroy_cgroup);
50

51
static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
52 53 54 55 56 57 58 59
{
	struct cg_proto *cg_proto;
	int ret;

	cg_proto = tcp_prot.proto_cgroup(memcg);
	if (!cg_proto)
		return -EINVAL;

60
	ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
61 62 63
	if (ret)
		return ret;

64
	if (!cg_proto->active) {
65
		/*
66
		 * The active flag needs to be written after the static_key
67 68 69 70 71 72 73 74 75 76 77 78 79 80
		 * update. This is what guarantees that the socket activation
		 * function is the last one to run. See sock_update_memcg() for
		 * details, and note that we don't mark any socket as belonging
		 * to this memcg until that flag is up.
		 *
		 * We need to do this, because static_keys will span multiple
		 * sites, but we can't control their order. If we mark a socket
		 * as accounted, but the accounting functions are not patched in
		 * yet, we'll lose accounting.
		 *
		 * We never race with the readers in sock_update_memcg(),
		 * because when this value change, the code to process it is not
		 * patched in yet.
		 */
81 82
		static_key_slow_inc(&memcg_socket_limit_enabled);
		cg_proto->active = true;
83
	}
84 85 86 87

	return 0;
}

88 89 90 91 92 93 94 95 96
enum {
	RES_USAGE,
	RES_LIMIT,
	RES_MAX_USAGE,
	RES_FAILCNT,
};

static DEFINE_MUTEX(tcp_limit_mutex);

97 98
static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
				char *buf, size_t nbytes, loff_t off)
99
{
100
	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
101
	unsigned long nr_pages;
102 103
	int ret = 0;

104 105 106
	buf = strstrip(buf);

	switch (of_cft(of)->private) {
107 108
	case RES_LIMIT:
		/* see memcontrol.c */
109
		ret = page_counter_memparse(buf, "-1", &nr_pages);
110 111
		if (ret)
			break;
112 113 114
		mutex_lock(&tcp_limit_mutex);
		ret = tcp_update_limit(memcg, nr_pages);
		mutex_unlock(&tcp_limit_mutex);
115 116 117 118 119
		break;
	default:
		ret = -EINVAL;
		break;
	}
120
	return ret ?: nbytes;
121 122
}

123
static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
124
{
125
	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
126
	struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
127 128 129 130
	u64 val;

	switch (cft->private) {
	case RES_LIMIT:
131 132 133 134
		if (!cg_proto)
			return PAGE_COUNTER_MAX;
		val = cg_proto->memory_allocated.limit;
		val *= PAGE_SIZE;
135
		break;
136
	case RES_USAGE:
137 138 139 140 141
		if (!cg_proto)
			val = atomic_long_read(&tcp_memory_allocated);
		else
			val = page_counter_read(&cg_proto->memory_allocated);
		val *= PAGE_SIZE;
142
		break;
143
	case RES_FAILCNT:
144 145 146 147
		if (!cg_proto)
			return 0;
		val = cg_proto->memory_allocated.failcnt;
		break;
148
	case RES_MAX_USAGE:
149 150 151 152
		if (!cg_proto)
			return 0;
		val = cg_proto->memory_allocated.watermark;
		val *= PAGE_SIZE;
153
		break;
154 155 156 157 158 159
	default:
		BUG();
	}
	return val;
}

160 161
static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
				char *buf, size_t nbytes, loff_t off)
162 163 164 165
{
	struct mem_cgroup *memcg;
	struct cg_proto *cg_proto;

166
	memcg = mem_cgroup_from_css(of_css(of));
167 168
	cg_proto = tcp_prot.proto_cgroup(memcg);
	if (!cg_proto)
169
		return nbytes;
170

171
	switch (of_cft(of)->private) {
172
	case RES_MAX_USAGE:
173
		page_counter_reset_watermark(&cg_proto->memory_allocated);
174
		break;
175
	case RES_FAILCNT:
176
		cg_proto->memory_allocated.failcnt = 0;
177 178 179
		break;
	}

180
	return nbytes;
181 182
}

183 184 185
static struct cftype tcp_files[] = {
	{
		.name = "kmem.tcp.limit_in_bytes",
186
		.write = tcp_cgroup_write,
187 188 189 190 191 192 193 194 195 196 197
		.read_u64 = tcp_cgroup_read,
		.private = RES_LIMIT,
	},
	{
		.name = "kmem.tcp.usage_in_bytes",
		.read_u64 = tcp_cgroup_read,
		.private = RES_USAGE,
	},
	{
		.name = "kmem.tcp.failcnt",
		.private = RES_FAILCNT,
198
		.write = tcp_cgroup_reset,
199 200 201 202 203
		.read_u64 = tcp_cgroup_read,
	},
	{
		.name = "kmem.tcp.max_usage_in_bytes",
		.private = RES_MAX_USAGE,
204
		.write = tcp_cgroup_reset,
205 206
		.read_u64 = tcp_cgroup_read,
	},
207
	{ }	/* terminate */
208
};
209 210 211

static int __init tcp_memcontrol_init(void)
{
212
	WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
213 214 215
	return 0;
}
__initcall(tcp_memcontrol_init);