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