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 | |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 9 | static void memcg_tcp_enter_memory_pressure(struct sock *sk) |
| 10 | { |
Dan Carpenter | c48e074 | 2011-12-15 01:05:10 +0000 | [diff] [blame] | 11 | if (sk->sk_cgrp->memory_pressure) |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 12 | sk->sk_cgrp->memory_pressure = 1; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 13 | } |
| 14 | EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure); |
| 15 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 16 | int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 17 | { |
| 18 | /* |
| 19 | * The root cgroup does not use res_counters, but rather, |
| 20 | * rely on the data already collected by the network |
| 21 | * subsystem |
| 22 | */ |
| 23 | struct res_counter *res_parent = NULL; |
| 24 | struct cg_proto *cg_proto, *parent_cg; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 25 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 26 | |
| 27 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 28 | if (!cg_proto) |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 29 | return 0; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 30 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 31 | cg_proto->sysctl_mem[0] = sysctl_tcp_mem[0]; |
| 32 | cg_proto->sysctl_mem[1] = sysctl_tcp_mem[1]; |
| 33 | cg_proto->sysctl_mem[2] = sysctl_tcp_mem[2]; |
| 34 | cg_proto->memory_pressure = 0; |
| 35 | cg_proto->memcg = memcg; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 36 | |
| 37 | parent_cg = tcp_prot.proto_cgroup(parent); |
| 38 | if (parent_cg) |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 39 | res_parent = &parent_cg->memory_allocated; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 40 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 41 | res_counter_init(&cg_proto->memory_allocated, res_parent); |
| 42 | percpu_counter_init(&cg_proto->sockets_allocated, 0); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 43 | |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 44 | return 0; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 45 | } |
| 46 | EXPORT_SYMBOL(tcp_init_cgroup); |
| 47 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 48 | void tcp_destroy_cgroup(struct mem_cgroup *memcg) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 49 | { |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 50 | struct cg_proto *cg_proto; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 51 | |
| 52 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 53 | if (!cg_proto) |
| 54 | return; |
| 55 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 56 | percpu_counter_destroy(&cg_proto->sockets_allocated); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL(tcp_destroy_cgroup); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 59 | |
| 60 | static int tcp_update_limit(struct mem_cgroup *memcg, u64 val) |
| 61 | { |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 62 | struct cg_proto *cg_proto; |
| 63 | u64 old_lim; |
| 64 | int i; |
| 65 | int ret; |
| 66 | |
| 67 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 68 | if (!cg_proto) |
| 69 | return -EINVAL; |
| 70 | |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 71 | if (val > RES_COUNTER_MAX) |
| 72 | val = RES_COUNTER_MAX; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 73 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 74 | old_lim = res_counter_read_u64(&cg_proto->memory_allocated, RES_LIMIT); |
| 75 | ret = res_counter_set_limit(&cg_proto->memory_allocated, val); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 76 | if (ret) |
| 77 | return ret; |
| 78 | |
| 79 | for (i = 0; i < 3; i++) |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 80 | cg_proto->sysctl_mem[i] = min_t(long, val >> PAGE_SHIFT, |
| 81 | sysctl_tcp_mem[i]); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 82 | |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 83 | if (val == RES_COUNTER_MAX) |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 84 | clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 85 | else if (val != RES_COUNTER_MAX) { |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 86 | /* |
| 87 | * The active bit needs to be written after the static_key |
| 88 | * update. This is what guarantees that the socket activation |
| 89 | * function is the last one to run. See sock_update_memcg() for |
| 90 | * details, and note that we don't mark any socket as belonging |
| 91 | * to this memcg until that flag is up. |
| 92 | * |
| 93 | * We need to do this, because static_keys will span multiple |
| 94 | * sites, but we can't control their order. If we mark a socket |
| 95 | * as accounted, but the accounting functions are not patched in |
| 96 | * yet, we'll lose accounting. |
| 97 | * |
| 98 | * We never race with the readers in sock_update_memcg(), |
| 99 | * because when this value change, the code to process it is not |
| 100 | * patched in yet. |
| 101 | * |
| 102 | * The activated bit is used to guarantee that no two writers |
| 103 | * will do the update in the same memcg. Without that, we can't |
| 104 | * properly shutdown the static key. |
| 105 | */ |
| 106 | if (!test_and_set_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags)) |
| 107 | static_key_slow_inc(&memcg_socket_limit_enabled); |
| 108 | set_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); |
| 109 | } |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 114 | static int tcp_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft, |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 115 | const char *buffer) |
| 116 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 117 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 118 | unsigned long long val; |
| 119 | int ret = 0; |
| 120 | |
| 121 | switch (cft->private) { |
| 122 | case RES_LIMIT: |
| 123 | /* see memcontrol.c */ |
| 124 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 125 | if (ret) |
| 126 | break; |
| 127 | ret = tcp_update_limit(memcg, val); |
| 128 | break; |
| 129 | default: |
| 130 | ret = -EINVAL; |
| 131 | break; |
| 132 | } |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val) |
| 137 | { |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 138 | struct cg_proto *cg_proto; |
| 139 | |
| 140 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 141 | if (!cg_proto) |
| 142 | return default_val; |
| 143 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 144 | return res_counter_read_u64(&cg_proto->memory_allocated, type); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 147 | static u64 tcp_read_usage(struct mem_cgroup *memcg) |
| 148 | { |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 149 | struct cg_proto *cg_proto; |
| 150 | |
| 151 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 152 | if (!cg_proto) |
| 153 | return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT; |
| 154 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 155 | return res_counter_read_u64(&cg_proto->memory_allocated, RES_USAGE); |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 158 | static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 159 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 160 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 161 | u64 val; |
| 162 | |
| 163 | switch (cft->private) { |
| 164 | case RES_LIMIT: |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 165 | val = tcp_read_stat(memcg, RES_LIMIT, RES_COUNTER_MAX); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 166 | break; |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 167 | case RES_USAGE: |
| 168 | val = tcp_read_usage(memcg); |
| 169 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 170 | case RES_FAILCNT: |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 171 | case RES_MAX_USAGE: |
| 172 | val = tcp_read_stat(memcg, cft->private, 0); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 173 | break; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 174 | default: |
| 175 | BUG(); |
| 176 | } |
| 177 | return val; |
| 178 | } |
| 179 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 180 | static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event) |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 181 | { |
| 182 | struct mem_cgroup *memcg; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 183 | struct cg_proto *cg_proto; |
| 184 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 185 | memcg = mem_cgroup_from_css(css); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 186 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 187 | if (!cg_proto) |
| 188 | return 0; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 189 | |
| 190 | switch (event) { |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 191 | case RES_MAX_USAGE: |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 192 | res_counter_reset_max(&cg_proto->memory_allocated); |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 193 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 194 | case RES_FAILCNT: |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 195 | res_counter_reset_failcnt(&cg_proto->memory_allocated); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 202 | static struct cftype tcp_files[] = { |
| 203 | { |
| 204 | .name = "kmem.tcp.limit_in_bytes", |
| 205 | .write_string = tcp_cgroup_write, |
| 206 | .read_u64 = tcp_cgroup_read, |
| 207 | .private = RES_LIMIT, |
| 208 | }, |
| 209 | { |
| 210 | .name = "kmem.tcp.usage_in_bytes", |
| 211 | .read_u64 = tcp_cgroup_read, |
| 212 | .private = RES_USAGE, |
| 213 | }, |
| 214 | { |
| 215 | .name = "kmem.tcp.failcnt", |
| 216 | .private = RES_FAILCNT, |
| 217 | .trigger = tcp_cgroup_reset, |
| 218 | .read_u64 = tcp_cgroup_read, |
| 219 | }, |
| 220 | { |
| 221 | .name = "kmem.tcp.max_usage_in_bytes", |
| 222 | .private = RES_MAX_USAGE, |
| 223 | .trigger = tcp_cgroup_reset, |
| 224 | .read_u64 = tcp_cgroup_read, |
| 225 | }, |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 226 | { } /* terminate */ |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 227 | }; |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 228 | |
| 229 | static int __init tcp_memcontrol_init(void) |
| 230 | { |
| 231 | WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, tcp_files)); |
| 232 | return 0; |
| 233 | } |
| 234 | __initcall(tcp_memcontrol_init); |