Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 1 | #include <net/tcp.h> |
| 2 | #include <net/tcp_memcontrol.h> |
| 3 | #include <net/sock.h> |
Glauber Costa | 3dc43e3 | 2011-12-11 21:47:05 +0000 | [diff] [blame] | 4 | #include <net/ip.h> |
| 5 | #include <linux/nsproxy.h> |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 6 | #include <linux/memcontrol.h> |
| 7 | #include <linux/module.h> |
| 8 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame^] | 9 | static struct cftype tcp_files[4]; /* XXX: will be removed soon */ |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 10 | |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 11 | static 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 | |
| 16 | static void memcg_tcp_enter_memory_pressure(struct sock *sk) |
| 17 | { |
Dan Carpenter | c48e074 | 2011-12-15 01:05:10 +0000 | [diff] [blame] | 18 | if (sk->sk_cgrp->memory_pressure) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 19 | *sk->sk_cgrp->memory_pressure = 1; |
| 20 | } |
| 21 | EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure); |
| 22 | |
| 23 | int 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 Costa | 3dc43e3 | 2011-12-11 21:47:05 +0000 | [diff] [blame] | 35 | struct net *net = current->nsproxy->net_ns; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 36 | |
| 37 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 38 | if (!cg_proto) |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 39 | goto create_files; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 40 | |
| 41 | tcp = tcp_from_cgproto(cg_proto); |
| 42 | |
Glauber Costa | 3dc43e3 | 2011-12-11 21:47:05 +0000 | [diff] [blame] | 43 | 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 Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 46 | 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 Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 62 | create_files: |
| 63 | return cgroup_add_files(cgrp, ss, tcp_files, |
| 64 | ARRAY_SIZE(tcp_files)); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 65 | } |
| 66 | EXPORT_SYMBOL(tcp_init_cgroup); |
| 67 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 68 | void tcp_destroy_cgroup(struct cgroup *cgrp) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 69 | { |
| 70 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 71 | struct cg_proto *cg_proto; |
| 72 | struct tcp_memcontrol *tcp; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 73 | u64 val; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 74 | |
| 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 Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 81 | |
Glauber Costa | 1398eee | 2012-01-12 02:16:06 +0000 | [diff] [blame] | 82 | val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 83 | |
| 84 | if (val != RESOURCE_MAX) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 85 | static_key_slow_dec(&memcg_socket_limit_enabled); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL(tcp_destroy_cgroup); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 88 | |
| 89 | static 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 Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 117 | static_key_slow_dec(&memcg_socket_limit_enabled); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 118 | else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 119 | static_key_slow_inc(&memcg_socket_limit_enabled); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static 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 | |
| 146 | static 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 Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 159 | static 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 Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 172 | static 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 Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 181 | case RES_USAGE: |
| 182 | val = tcp_read_usage(memcg); |
| 183 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 184 | case RES_FAILCNT: |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 185 | case RES_MAX_USAGE: |
| 186 | val = tcp_read_stat(memcg, cft->private, 0); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 187 | break; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 188 | default: |
| 189 | BUG(); |
| 190 | } |
| 191 | return val; |
| 192 | } |
| 193 | |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 194 | static 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 Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 207 | case RES_MAX_USAGE: |
| 208 | res_counter_reset_max(&tcp->tcp_memory_allocated); |
| 209 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 210 | case RES_FAILCNT: |
| 211 | res_counter_reset_failcnt(&tcp->tcp_memory_allocated); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 218 | unsigned 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 | |
| 231 | void 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 Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame^] | 244 | |
| 245 | static 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 | }; |