2 #include <net/tcp_memcontrol.h>
5 #include <linux/nsproxy.h>
6 #include <linux/memcontrol.h>
7 #include <linux/module.h>
9 static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
11 return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
14 static void memcg_tcp_enter_memory_pressure(struct sock *sk)
16 if (sk->sk_cgrp->memory_pressure)
17 *sk->sk_cgrp->memory_pressure = 1;
19 EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
21 int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
24 * The root cgroup does not use res_counters, but rather,
25 * rely on the data already collected by the network
28 struct res_counter *res_parent = NULL;
29 struct cg_proto *cg_proto, *parent_cg;
30 struct tcp_memcontrol *tcp;
31 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
32 struct net *net = current->nsproxy->net_ns;
34 cg_proto = tcp_prot.proto_cgroup(memcg);
38 tcp = tcp_from_cgproto(cg_proto);
40 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];
43 tcp->tcp_memory_pressure = 0;
45 parent_cg = tcp_prot.proto_cgroup(parent);
47 res_parent = parent_cg->memory_allocated;
49 res_counter_init(&tcp->tcp_memory_allocated, res_parent);
50 percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
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;
61 EXPORT_SYMBOL(tcp_init_cgroup);
63 void tcp_destroy_cgroup(struct mem_cgroup *memcg)
65 struct cg_proto *cg_proto;
66 struct tcp_memcontrol *tcp;
69 cg_proto = tcp_prot.proto_cgroup(memcg);
73 tcp = tcp_from_cgproto(cg_proto);
74 percpu_counter_destroy(&tcp->tcp_sockets_allocated);
76 val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
78 if (val != RESOURCE_MAX)
79 static_key_slow_dec(&memcg_socket_limit_enabled);
81 EXPORT_SYMBOL(tcp_destroy_cgroup);
83 static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
85 struct net *net = current->nsproxy->net_ns;
86 struct tcp_memcontrol *tcp;
87 struct cg_proto *cg_proto;
92 cg_proto = tcp_prot.proto_cgroup(memcg);
96 if (val > RESOURCE_MAX)
99 tcp = tcp_from_cgproto(cg_proto);
101 old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
102 ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
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]);
110 if (val == RESOURCE_MAX && old_lim != RESOURCE_MAX)
111 static_key_slow_dec(&memcg_socket_limit_enabled);
112 else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX)
113 static_key_slow_inc(&memcg_socket_limit_enabled);
118 static int tcp_cgroup_write(struct cgroup *cont, struct cftype *cft,
121 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
122 unsigned long long val;
125 switch (cft->private) {
127 /* see memcontrol.c */
128 ret = res_counter_memparse_write_strategy(buffer, &val);
131 ret = tcp_update_limit(memcg, val);
140 static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
142 struct tcp_memcontrol *tcp;
143 struct cg_proto *cg_proto;
145 cg_proto = tcp_prot.proto_cgroup(memcg);
149 tcp = tcp_from_cgproto(cg_proto);
150 return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
153 static u64 tcp_read_usage(struct mem_cgroup *memcg)
155 struct tcp_memcontrol *tcp;
156 struct cg_proto *cg_proto;
158 cg_proto = tcp_prot.proto_cgroup(memcg);
160 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
162 tcp = tcp_from_cgproto(cg_proto);
163 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
166 static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
168 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
171 switch (cft->private) {
173 val = tcp_read_stat(memcg, RES_LIMIT, RESOURCE_MAX);
176 val = tcp_read_usage(memcg);
180 val = tcp_read_stat(memcg, cft->private, 0);
188 static int tcp_cgroup_reset(struct cgroup *cont, unsigned int event)
190 struct mem_cgroup *memcg;
191 struct tcp_memcontrol *tcp;
192 struct cg_proto *cg_proto;
194 memcg = mem_cgroup_from_cont(cont);
195 cg_proto = tcp_prot.proto_cgroup(memcg);
198 tcp = tcp_from_cgproto(cg_proto);
202 res_counter_reset_max(&tcp->tcp_memory_allocated);
205 res_counter_reset_failcnt(&tcp->tcp_memory_allocated);
212 unsigned long long tcp_max_memory(const struct mem_cgroup *memcg)
214 struct tcp_memcontrol *tcp;
215 struct cg_proto *cg_proto;
217 cg_proto = tcp_prot.proto_cgroup((struct mem_cgroup *)memcg);
221 tcp = tcp_from_cgproto(cg_proto);
222 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
225 void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx)
227 struct tcp_memcontrol *tcp;
228 struct cg_proto *cg_proto;
230 cg_proto = tcp_prot.proto_cgroup(memcg);
234 tcp = tcp_from_cgproto(cg_proto);
236 tcp->tcp_prot_mem[idx] = val;
239 static struct cftype tcp_files[] = {
241 .name = "kmem.tcp.limit_in_bytes",
242 .write_string = tcp_cgroup_write,
243 .read_u64 = tcp_cgroup_read,
244 .private = RES_LIMIT,
247 .name = "kmem.tcp.usage_in_bytes",
248 .read_u64 = tcp_cgroup_read,
249 .private = RES_USAGE,
252 .name = "kmem.tcp.failcnt",
253 .private = RES_FAILCNT,
254 .trigger = tcp_cgroup_reset,
255 .read_u64 = tcp_cgroup_read,
258 .name = "kmem.tcp.max_usage_in_bytes",
259 .private = RES_MAX_USAGE,
260 .trigger = tcp_cgroup_reset,
261 .read_u64 = tcp_cgroup_read,
266 static int __init tcp_memcontrol_init(void)
268 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, tcp_files));
271 __initcall(tcp_memcontrol_init);