]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - net/ipv4/tcp_memcontrol.c
151703791bb0d43818700349fb0a585736c7dc19
[~shefty/rdma-dev.git] / net / ipv4 / tcp_memcontrol.c
1 #include <net/tcp.h>
2 #include <net/tcp_memcontrol.h>
3 #include <net/sock.h>
4 #include <net/ip.h>
5 #include <linux/nsproxy.h>
6 #include <linux/memcontrol.h>
7 #include <linux/module.h>
8
9 static 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
14 static void memcg_tcp_enter_memory_pressure(struct sock *sk)
15 {
16         if (sk->sk_cgrp->memory_pressure)
17                 *sk->sk_cgrp->memory_pressure = 1;
18 }
19 EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
20
21 int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
22 {
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;
31         struct mem_cgroup *parent = parent_mem_cgroup(memcg);
32         struct net *net = current->nsproxy->net_ns;
33
34         cg_proto = tcp_prot.proto_cgroup(memcg);
35         if (!cg_proto)
36                 return 0;
37
38         tcp = tcp_from_cgproto(cg_proto);
39
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;
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
59         return 0;
60 }
61 EXPORT_SYMBOL(tcp_init_cgroup);
62
63 void tcp_destroy_cgroup(struct mem_cgroup *memcg)
64 {
65         struct cg_proto *cg_proto;
66         struct tcp_memcontrol *tcp;
67         u64 val;
68
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);
75
76         val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
77
78         if (val != RESOURCE_MAX)
79                 static_key_slow_dec(&memcg_socket_limit_enabled);
80 }
81 EXPORT_SYMBOL(tcp_destroy_cgroup);
82
83 static 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)
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);
114
115         return 0;
116 }
117
118 static 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
140 static 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
153 static 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
166 static 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;
175         case RES_USAGE:
176                 val = tcp_read_usage(memcg);
177                 break;
178         case RES_FAILCNT:
179         case RES_MAX_USAGE:
180                 val = tcp_read_stat(memcg, cft->private, 0);
181                 break;
182         default:
183                 BUG();
184         }
185         return val;
186 }
187
188 static 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) {
201         case RES_MAX_USAGE:
202                 res_counter_reset_max(&tcp->tcp_memory_allocated);
203                 break;
204         case RES_FAILCNT:
205                 res_counter_reset_failcnt(&tcp->tcp_memory_allocated);
206                 break;
207         }
208
209         return 0;
210 }
211
212 unsigned 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
225 void 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 }
238
239 static 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         },
263         { }     /* terminate */
264 };
265
266 static 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);