blob: 151703791bb0d43818700349fb0a585736c7dc19 [file] [log] [blame]
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001#include <net/tcp.h>
2#include <net/tcp_memcontrol.h>
3#include <net/sock.h>
Glauber Costa3dc43e32011-12-11 21:47:05 +00004#include <net/ip.h>
5#include <linux/nsproxy.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006#include <linux/memcontrol.h>
7#include <linux/module.h>
8
9static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
10{
11 return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
12}
13
14static void memcg_tcp_enter_memory_pressure(struct sock *sk)
15{
Dan Carpenterc48e0742011-12-15 01:05:10 +000016 if (sk->sk_cgrp->memory_pressure)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000017 *sk->sk_cgrp->memory_pressure = 1;
18}
19EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
20
Glauber Costa1d62e432012-04-09 19:36:33 -030021int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000022{
23 /*
24 * The root cgroup does not use res_counters, but rather,
25 * rely on the data already collected by the network
26 * subsystem
27 */
28 struct res_counter *res_parent = NULL;
29 struct cg_proto *cg_proto, *parent_cg;
30 struct tcp_memcontrol *tcp;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000031 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costa3dc43e32011-12-11 21:47:05 +000032 struct net *net = current->nsproxy->net_ns;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000033
34 cg_proto = tcp_prot.proto_cgroup(memcg);
35 if (!cg_proto)
Tejun Heo6bc10342012-04-01 12:09:55 -070036 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000037
38 tcp = tcp_from_cgproto(cg_proto);
39
Glauber Costa3dc43e32011-12-11 21:47:05 +000040 tcp->tcp_prot_mem[0] = net->ipv4.sysctl_tcp_mem[0];
41 tcp->tcp_prot_mem[1] = net->ipv4.sysctl_tcp_mem[1];
42 tcp->tcp_prot_mem[2] = net->ipv4.sysctl_tcp_mem[2];
Glauber Costad1a4c0b2011-12-11 21:47:04 +000043 tcp->tcp_memory_pressure = 0;
44
45 parent_cg = tcp_prot.proto_cgroup(parent);
46 if (parent_cg)
47 res_parent = parent_cg->memory_allocated;
48
49 res_counter_init(&tcp->tcp_memory_allocated, res_parent);
50 percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
51
52 cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure;
53 cg_proto->memory_pressure = &tcp->tcp_memory_pressure;
54 cg_proto->sysctl_mem = tcp->tcp_prot_mem;
55 cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
56 cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
57 cg_proto->memcg = memcg;
58
Tejun Heo6bc10342012-04-01 12:09:55 -070059 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000060}
61EXPORT_SYMBOL(tcp_init_cgroup);
62
Glauber Costa1d62e432012-04-09 19:36:33 -030063void tcp_destroy_cgroup(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000064{
Glauber Costad1a4c0b2011-12-11 21:47:04 +000065 struct cg_proto *cg_proto;
66 struct tcp_memcontrol *tcp;
Glauber Costa3aaabe22011-12-11 21:47:06 +000067 u64 val;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000068
69 cg_proto = tcp_prot.proto_cgroup(memcg);
70 if (!cg_proto)
71 return;
72
73 tcp = tcp_from_cgproto(cg_proto);
74 percpu_counter_destroy(&tcp->tcp_sockets_allocated);
Glauber Costa3aaabe22011-12-11 21:47:06 +000075
Glauber Costa1398eee2012-01-12 02:16:06 +000076 val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
Glauber Costa3aaabe22011-12-11 21:47:06 +000077
78 if (val != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +010079 static_key_slow_dec(&memcg_socket_limit_enabled);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000080}
81EXPORT_SYMBOL(tcp_destroy_cgroup);
Glauber Costa3aaabe22011-12-11 21:47:06 +000082
83static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
84{
85 struct net *net = current->nsproxy->net_ns;
86 struct tcp_memcontrol *tcp;
87 struct cg_proto *cg_proto;
88 u64 old_lim;
89 int i;
90 int ret;
91
92 cg_proto = tcp_prot.proto_cgroup(memcg);
93 if (!cg_proto)
94 return -EINVAL;
95
96 if (val > RESOURCE_MAX)
97 val = RESOURCE_MAX;
98
99 tcp = tcp_from_cgproto(cg_proto);
100
101 old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
102 ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
103 if (ret)
104 return ret;
105
106 for (i = 0; i < 3; i++)
107 tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT,
108 net->ipv4.sysctl_tcp_mem[i]);
109
110 if (val == RESOURCE_MAX && old_lim != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100111 static_key_slow_dec(&memcg_socket_limit_enabled);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000112 else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100113 static_key_slow_inc(&memcg_socket_limit_enabled);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000114
115 return 0;
116}
117
118static int tcp_cgroup_write(struct cgroup *cont, struct cftype *cft,
119 const char *buffer)
120{
121 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
122 unsigned long long val;
123 int ret = 0;
124
125 switch (cft->private) {
126 case RES_LIMIT:
127 /* see memcontrol.c */
128 ret = res_counter_memparse_write_strategy(buffer, &val);
129 if (ret)
130 break;
131 ret = tcp_update_limit(memcg, val);
132 break;
133 default:
134 ret = -EINVAL;
135 break;
136 }
137 return ret;
138}
139
140static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
141{
142 struct tcp_memcontrol *tcp;
143 struct cg_proto *cg_proto;
144
145 cg_proto = tcp_prot.proto_cgroup(memcg);
146 if (!cg_proto)
147 return default_val;
148
149 tcp = tcp_from_cgproto(cg_proto);
150 return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
151}
152
Glauber Costa5a6dd342011-12-11 21:47:07 +0000153static u64 tcp_read_usage(struct mem_cgroup *memcg)
154{
155 struct tcp_memcontrol *tcp;
156 struct cg_proto *cg_proto;
157
158 cg_proto = tcp_prot.proto_cgroup(memcg);
159 if (!cg_proto)
160 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
161
162 tcp = tcp_from_cgproto(cg_proto);
163 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
164}
165
Glauber Costa3aaabe22011-12-11 21:47:06 +0000166static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
167{
168 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
169 u64 val;
170
171 switch (cft->private) {
172 case RES_LIMIT:
173 val = tcp_read_stat(memcg, RES_LIMIT, RESOURCE_MAX);
174 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000175 case RES_USAGE:
176 val = tcp_read_usage(memcg);
177 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000178 case RES_FAILCNT:
Glauber Costa0850f0f2011-12-11 21:47:09 +0000179 case RES_MAX_USAGE:
180 val = tcp_read_stat(memcg, cft->private, 0);
Glauber Costaffea59e2011-12-11 21:47:08 +0000181 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000182 default:
183 BUG();
184 }
185 return val;
186}
187
Glauber Costaffea59e2011-12-11 21:47:08 +0000188static int tcp_cgroup_reset(struct cgroup *cont, unsigned int event)
189{
190 struct mem_cgroup *memcg;
191 struct tcp_memcontrol *tcp;
192 struct cg_proto *cg_proto;
193
194 memcg = mem_cgroup_from_cont(cont);
195 cg_proto = tcp_prot.proto_cgroup(memcg);
196 if (!cg_proto)
197 return 0;
198 tcp = tcp_from_cgproto(cg_proto);
199
200 switch (event) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000201 case RES_MAX_USAGE:
202 res_counter_reset_max(&tcp->tcp_memory_allocated);
203 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000204 case RES_FAILCNT:
205 res_counter_reset_failcnt(&tcp->tcp_memory_allocated);
206 break;
207 }
208
209 return 0;
210}
211
Glauber Costa3aaabe22011-12-11 21:47:06 +0000212unsigned long long tcp_max_memory(const struct mem_cgroup *memcg)
213{
214 struct tcp_memcontrol *tcp;
215 struct cg_proto *cg_proto;
216
217 cg_proto = tcp_prot.proto_cgroup((struct mem_cgroup *)memcg);
218 if (!cg_proto)
219 return 0;
220
221 tcp = tcp_from_cgproto(cg_proto);
222 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
223}
224
225void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx)
226{
227 struct tcp_memcontrol *tcp;
228 struct cg_proto *cg_proto;
229
230 cg_proto = tcp_prot.proto_cgroup(memcg);
231 if (!cg_proto)
232 return;
233
234 tcp = tcp_from_cgproto(cg_proto);
235
236 tcp->tcp_prot_mem[idx] = val;
237}
Tejun Heo676f7c82012-04-01 12:09:55 -0700238
239static struct cftype tcp_files[] = {
240 {
241 .name = "kmem.tcp.limit_in_bytes",
242 .write_string = tcp_cgroup_write,
243 .read_u64 = tcp_cgroup_read,
244 .private = RES_LIMIT,
245 },
246 {
247 .name = "kmem.tcp.usage_in_bytes",
248 .read_u64 = tcp_cgroup_read,
249 .private = RES_USAGE,
250 },
251 {
252 .name = "kmem.tcp.failcnt",
253 .private = RES_FAILCNT,
254 .trigger = tcp_cgroup_reset,
255 .read_u64 = tcp_cgroup_read,
256 },
257 {
258 .name = "kmem.tcp.max_usage_in_bytes",
259 .private = RES_MAX_USAGE,
260 .trigger = tcp_cgroup_reset,
261 .read_u64 = tcp_cgroup_read,
262 },
Tejun Heo6bc10342012-04-01 12:09:55 -0700263 { } /* terminate */
Tejun Heo676f7c82012-04-01 12:09:55 -0700264};
Tejun Heo6bc10342012-04-01 12:09:55 -0700265
266static int __init tcp_memcontrol_init(void)
267{
268 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, tcp_files));
269 return 0;
270}
271__initcall(tcp_memcontrol_init);