blob: 4d3ee407d2d8c205b8a68fec5124844b28122216 [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
Tejun Heo676f7c82012-04-01 12:09:55 -07009static struct cftype tcp_files[4]; /* XXX: will be removed soon */
Glauber Costa3aaabe22011-12-11 21:47:06 +000010
Glauber Costad1a4c0b2011-12-11 21:47:04 +000011static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
12{
13 return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
14}
15
16static void memcg_tcp_enter_memory_pressure(struct sock *sk)
17{
Dan Carpenterc48e0742011-12-15 01:05:10 +000018 if (sk->sk_cgrp->memory_pressure)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000019 *sk->sk_cgrp->memory_pressure = 1;
20}
21EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
22
23int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
24{
25 /*
26 * The root cgroup does not use res_counters, but rather,
27 * rely on the data already collected by the network
28 * subsystem
29 */
30 struct res_counter *res_parent = NULL;
31 struct cg_proto *cg_proto, *parent_cg;
32 struct tcp_memcontrol *tcp;
33 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
34 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costa3dc43e32011-12-11 21:47:05 +000035 struct net *net = current->nsproxy->net_ns;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000036
37 cg_proto = tcp_prot.proto_cgroup(memcg);
38 if (!cg_proto)
Glauber Costa3aaabe22011-12-11 21:47:06 +000039 goto create_files;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000040
41 tcp = tcp_from_cgproto(cg_proto);
42
Glauber Costa3dc43e32011-12-11 21:47:05 +000043 tcp->tcp_prot_mem[0] = net->ipv4.sysctl_tcp_mem[0];
44 tcp->tcp_prot_mem[1] = net->ipv4.sysctl_tcp_mem[1];
45 tcp->tcp_prot_mem[2] = net->ipv4.sysctl_tcp_mem[2];
Glauber Costad1a4c0b2011-12-11 21:47:04 +000046 tcp->tcp_memory_pressure = 0;
47
48 parent_cg = tcp_prot.proto_cgroup(parent);
49 if (parent_cg)
50 res_parent = parent_cg->memory_allocated;
51
52 res_counter_init(&tcp->tcp_memory_allocated, res_parent);
53 percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
54
55 cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure;
56 cg_proto->memory_pressure = &tcp->tcp_memory_pressure;
57 cg_proto->sysctl_mem = tcp->tcp_prot_mem;
58 cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
59 cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
60 cg_proto->memcg = memcg;
61
Glauber Costa3aaabe22011-12-11 21:47:06 +000062create_files:
63 return cgroup_add_files(cgrp, ss, tcp_files,
64 ARRAY_SIZE(tcp_files));
Glauber Costad1a4c0b2011-12-11 21:47:04 +000065}
66EXPORT_SYMBOL(tcp_init_cgroup);
67
Li Zefan761b3ef52012-01-31 13:47:36 +080068void tcp_destroy_cgroup(struct cgroup *cgrp)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000069{
70 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
71 struct cg_proto *cg_proto;
72 struct tcp_memcontrol *tcp;
Glauber Costa3aaabe22011-12-11 21:47:06 +000073 u64 val;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000074
75 cg_proto = tcp_prot.proto_cgroup(memcg);
76 if (!cg_proto)
77 return;
78
79 tcp = tcp_from_cgproto(cg_proto);
80 percpu_counter_destroy(&tcp->tcp_sockets_allocated);
Glauber Costa3aaabe22011-12-11 21:47:06 +000081
Glauber Costa1398eee2012-01-12 02:16:06 +000082 val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
Glauber Costa3aaabe22011-12-11 21:47:06 +000083
84 if (val != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +010085 static_key_slow_dec(&memcg_socket_limit_enabled);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000086}
87EXPORT_SYMBOL(tcp_destroy_cgroup);
Glauber Costa3aaabe22011-12-11 21:47:06 +000088
89static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
90{
91 struct net *net = current->nsproxy->net_ns;
92 struct tcp_memcontrol *tcp;
93 struct cg_proto *cg_proto;
94 u64 old_lim;
95 int i;
96 int ret;
97
98 cg_proto = tcp_prot.proto_cgroup(memcg);
99 if (!cg_proto)
100 return -EINVAL;
101
102 if (val > RESOURCE_MAX)
103 val = RESOURCE_MAX;
104
105 tcp = tcp_from_cgproto(cg_proto);
106
107 old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
108 ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
109 if (ret)
110 return ret;
111
112 for (i = 0; i < 3; i++)
113 tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT,
114 net->ipv4.sysctl_tcp_mem[i]);
115
116 if (val == RESOURCE_MAX && old_lim != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100117 static_key_slow_dec(&memcg_socket_limit_enabled);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000118 else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100119 static_key_slow_inc(&memcg_socket_limit_enabled);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000120
121 return 0;
122}
123
124static int tcp_cgroup_write(struct cgroup *cont, struct cftype *cft,
125 const char *buffer)
126{
127 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
128 unsigned long long val;
129 int ret = 0;
130
131 switch (cft->private) {
132 case RES_LIMIT:
133 /* see memcontrol.c */
134 ret = res_counter_memparse_write_strategy(buffer, &val);
135 if (ret)
136 break;
137 ret = tcp_update_limit(memcg, val);
138 break;
139 default:
140 ret = -EINVAL;
141 break;
142 }
143 return ret;
144}
145
146static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
147{
148 struct tcp_memcontrol *tcp;
149 struct cg_proto *cg_proto;
150
151 cg_proto = tcp_prot.proto_cgroup(memcg);
152 if (!cg_proto)
153 return default_val;
154
155 tcp = tcp_from_cgproto(cg_proto);
156 return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
157}
158
Glauber Costa5a6dd342011-12-11 21:47:07 +0000159static u64 tcp_read_usage(struct mem_cgroup *memcg)
160{
161 struct tcp_memcontrol *tcp;
162 struct cg_proto *cg_proto;
163
164 cg_proto = tcp_prot.proto_cgroup(memcg);
165 if (!cg_proto)
166 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
167
168 tcp = tcp_from_cgproto(cg_proto);
169 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
170}
171
Glauber Costa3aaabe22011-12-11 21:47:06 +0000172static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
173{
174 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
175 u64 val;
176
177 switch (cft->private) {
178 case RES_LIMIT:
179 val = tcp_read_stat(memcg, RES_LIMIT, RESOURCE_MAX);
180 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000181 case RES_USAGE:
182 val = tcp_read_usage(memcg);
183 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000184 case RES_FAILCNT:
Glauber Costa0850f0f2011-12-11 21:47:09 +0000185 case RES_MAX_USAGE:
186 val = tcp_read_stat(memcg, cft->private, 0);
Glauber Costaffea59e2011-12-11 21:47:08 +0000187 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000188 default:
189 BUG();
190 }
191 return val;
192}
193
Glauber Costaffea59e2011-12-11 21:47:08 +0000194static int tcp_cgroup_reset(struct cgroup *cont, unsigned int event)
195{
196 struct mem_cgroup *memcg;
197 struct tcp_memcontrol *tcp;
198 struct cg_proto *cg_proto;
199
200 memcg = mem_cgroup_from_cont(cont);
201 cg_proto = tcp_prot.proto_cgroup(memcg);
202 if (!cg_proto)
203 return 0;
204 tcp = tcp_from_cgproto(cg_proto);
205
206 switch (event) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000207 case RES_MAX_USAGE:
208 res_counter_reset_max(&tcp->tcp_memory_allocated);
209 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000210 case RES_FAILCNT:
211 res_counter_reset_failcnt(&tcp->tcp_memory_allocated);
212 break;
213 }
214
215 return 0;
216}
217
Glauber Costa3aaabe22011-12-11 21:47:06 +0000218unsigned long long tcp_max_memory(const struct mem_cgroup *memcg)
219{
220 struct tcp_memcontrol *tcp;
221 struct cg_proto *cg_proto;
222
223 cg_proto = tcp_prot.proto_cgroup((struct mem_cgroup *)memcg);
224 if (!cg_proto)
225 return 0;
226
227 tcp = tcp_from_cgproto(cg_proto);
228 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
229}
230
231void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx)
232{
233 struct tcp_memcontrol *tcp;
234 struct cg_proto *cg_proto;
235
236 cg_proto = tcp_prot.proto_cgroup(memcg);
237 if (!cg_proto)
238 return;
239
240 tcp = tcp_from_cgproto(cg_proto);
241
242 tcp->tcp_prot_mem[idx] = val;
243}
Tejun Heo676f7c82012-04-01 12:09:55 -0700244
245static struct cftype tcp_files[] = {
246 {
247 .name = "kmem.tcp.limit_in_bytes",
248 .write_string = tcp_cgroup_write,
249 .read_u64 = tcp_cgroup_read,
250 .private = RES_LIMIT,
251 },
252 {
253 .name = "kmem.tcp.usage_in_bytes",
254 .read_u64 = tcp_cgroup_read,
255 .private = RES_USAGE,
256 },
257 {
258 .name = "kmem.tcp.failcnt",
259 .private = RES_FAILCNT,
260 .trigger = tcp_cgroup_reset,
261 .read_u64 = tcp_cgroup_read,
262 },
263 {
264 .name = "kmem.tcp.max_usage_in_bytes",
265 .private = RES_MAX_USAGE,
266 .trigger = tcp_cgroup_reset,
267 .read_u64 = tcp_cgroup_read,
268 },
269};