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 | |
| 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 | { |
Dan Carpenter | c48e074 | 2011-12-15 01:05:10 +0000 | [diff] [blame] | 16 | if (sk->sk_cgrp->memory_pressure) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 17 | *sk->sk_cgrp->memory_pressure = 1; |
| 18 | } |
| 19 | EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure); |
| 20 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame^] | 21 | int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 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; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 31 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
Glauber Costa | 3dc43e3 | 2011-12-11 21:47:05 +0000 | [diff] [blame] | 32 | struct net *net = current->nsproxy->net_ns; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 33 | |
| 34 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 35 | if (!cg_proto) |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 36 | return 0; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 37 | |
| 38 | tcp = tcp_from_cgproto(cg_proto); |
| 39 | |
Glauber Costa | 3dc43e3 | 2011-12-11 21:47:05 +0000 | [diff] [blame] | 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]; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 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 | |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 59 | return 0; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 60 | } |
| 61 | EXPORT_SYMBOL(tcp_init_cgroup); |
| 62 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame^] | 63 | void tcp_destroy_cgroup(struct mem_cgroup *memcg) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 64 | { |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 65 | struct cg_proto *cg_proto; |
| 66 | struct tcp_memcontrol *tcp; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 67 | u64 val; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 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); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 75 | |
Glauber Costa | 1398eee | 2012-01-12 02:16:06 +0000 | [diff] [blame] | 76 | val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 77 | |
| 78 | if (val != RESOURCE_MAX) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 79 | static_key_slow_dec(&memcg_socket_limit_enabled); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 80 | } |
| 81 | EXPORT_SYMBOL(tcp_destroy_cgroup); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 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) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 111 | static_key_slow_dec(&memcg_socket_limit_enabled); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 112 | else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 113 | static_key_slow_inc(&memcg_socket_limit_enabled); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 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 | |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 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 | |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 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; |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 175 | case RES_USAGE: |
| 176 | val = tcp_read_usage(memcg); |
| 177 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 178 | case RES_FAILCNT: |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 179 | case RES_MAX_USAGE: |
| 180 | val = tcp_read_stat(memcg, cft->private, 0); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 181 | break; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 182 | default: |
| 183 | BUG(); |
| 184 | } |
| 185 | return val; |
| 186 | } |
| 187 | |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 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) { |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 201 | case RES_MAX_USAGE: |
| 202 | res_counter_reset_max(&tcp->tcp_memory_allocated); |
| 203 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 204 | case RES_FAILCNT: |
| 205 | res_counter_reset_failcnt(&tcp->tcp_memory_allocated); |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 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 | } |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 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 | }, |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 263 | { } /* terminate */ |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 264 | }; |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 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); |