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 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 12 | * The root cgroup does not use page_counters, but rather, |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 13 | * rely on the data already collected by the network |
| 14 | * subsystem |
| 15 | */ |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 16 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 17 | struct page_counter *counter_parent = NULL; |
| 18 | struct cg_proto *cg_proto, *parent_cg; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 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 | 2e685cad | 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) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 32 | counter_parent = &parent_cg->memory_allocated; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 33 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 34 | page_counter_init(&cg_proto->memory_allocated, counter_parent); |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 35 | percpu_counter_init(&cg_proto->sockets_allocated, 0, GFP_KERNEL); |
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 | 2e685cad | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 49 | percpu_counter_destroy(&cg_proto->sockets_allocated); |
Vladimir Davydov | f48b80a | 2015-02-12 14:59:56 -0800 | [diff] [blame^] | 50 | |
| 51 | if (test_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags)) |
| 52 | static_key_slow_dec(&memcg_socket_limit_enabled); |
| 53 | |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 54 | } |
| 55 | EXPORT_SYMBOL(tcp_destroy_cgroup); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 56 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 57 | static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages) |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 58 | { |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 59 | struct cg_proto *cg_proto; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 60 | int i; |
| 61 | int ret; |
| 62 | |
| 63 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 64 | if (!cg_proto) |
| 65 | return -EINVAL; |
| 66 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 67 | ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 68 | if (ret) |
| 69 | return ret; |
| 70 | |
| 71 | for (i = 0; i < 3; i++) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 72 | cg_proto->sysctl_mem[i] = min_t(long, nr_pages, |
Eric W. Biederman | 2e685cad | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 73 | sysctl_tcp_mem[i]); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 74 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 75 | if (nr_pages == PAGE_COUNTER_MAX) |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 76 | clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 77 | else { |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 78 | /* |
| 79 | * The active bit needs to be written after the static_key |
| 80 | * update. This is what guarantees that the socket activation |
| 81 | * function is the last one to run. See sock_update_memcg() for |
| 82 | * details, and note that we don't mark any socket as belonging |
| 83 | * to this memcg until that flag is up. |
| 84 | * |
| 85 | * We need to do this, because static_keys will span multiple |
| 86 | * sites, but we can't control their order. If we mark a socket |
| 87 | * as accounted, but the accounting functions are not patched in |
| 88 | * yet, we'll lose accounting. |
| 89 | * |
| 90 | * We never race with the readers in sock_update_memcg(), |
| 91 | * because when this value change, the code to process it is not |
| 92 | * patched in yet. |
| 93 | * |
| 94 | * The activated bit is used to guarantee that no two writers |
| 95 | * will do the update in the same memcg. Without that, we can't |
| 96 | * properly shutdown the static key. |
| 97 | */ |
| 98 | if (!test_and_set_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags)) |
| 99 | static_key_slow_inc(&memcg_socket_limit_enabled); |
| 100 | set_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); |
| 101 | } |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 106 | enum { |
| 107 | RES_USAGE, |
| 108 | RES_LIMIT, |
| 109 | RES_MAX_USAGE, |
| 110 | RES_FAILCNT, |
| 111 | }; |
| 112 | |
| 113 | static DEFINE_MUTEX(tcp_limit_mutex); |
| 114 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 115 | static ssize_t tcp_cgroup_write(struct kernfs_open_file *of, |
| 116 | char *buf, size_t nbytes, loff_t off) |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 117 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 118 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 119 | unsigned long nr_pages; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 120 | int ret = 0; |
| 121 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 122 | buf = strstrip(buf); |
| 123 | |
| 124 | switch (of_cft(of)->private) { |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 125 | case RES_LIMIT: |
| 126 | /* see memcontrol.c */ |
Johannes Weiner | 650c5e5 | 2015-02-11 15:26:03 -0800 | [diff] [blame] | 127 | ret = page_counter_memparse(buf, "-1", &nr_pages); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 128 | if (ret) |
| 129 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 130 | mutex_lock(&tcp_limit_mutex); |
| 131 | ret = tcp_update_limit(memcg, nr_pages); |
| 132 | mutex_unlock(&tcp_limit_mutex); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | ret = -EINVAL; |
| 136 | break; |
| 137 | } |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 138 | return ret ?: nbytes; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 141 | 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] | 142 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 143 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 144 | struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg); |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 145 | u64 val; |
| 146 | |
| 147 | switch (cft->private) { |
| 148 | case RES_LIMIT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 149 | if (!cg_proto) |
| 150 | return PAGE_COUNTER_MAX; |
| 151 | val = cg_proto->memory_allocated.limit; |
| 152 | val *= PAGE_SIZE; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 153 | break; |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 154 | case RES_USAGE: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 155 | if (!cg_proto) |
| 156 | val = atomic_long_read(&tcp_memory_allocated); |
| 157 | else |
| 158 | val = page_counter_read(&cg_proto->memory_allocated); |
| 159 | val *= PAGE_SIZE; |
Glauber Costa | 5a6dd34 | 2011-12-11 21:47:07 +0000 | [diff] [blame] | 160 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 161 | case RES_FAILCNT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 162 | if (!cg_proto) |
| 163 | return 0; |
| 164 | val = cg_proto->memory_allocated.failcnt; |
| 165 | break; |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 166 | case RES_MAX_USAGE: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 167 | if (!cg_proto) |
| 168 | return 0; |
| 169 | val = cg_proto->memory_allocated.watermark; |
| 170 | val *= PAGE_SIZE; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 171 | break; |
Glauber Costa | 3aaabe2 | 2011-12-11 21:47:06 +0000 | [diff] [blame] | 172 | default: |
| 173 | BUG(); |
| 174 | } |
| 175 | return val; |
| 176 | } |
| 177 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 178 | static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of, |
| 179 | char *buf, size_t nbytes, loff_t off) |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 180 | { |
| 181 | struct mem_cgroup *memcg; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 182 | struct cg_proto *cg_proto; |
| 183 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 184 | memcg = mem_cgroup_from_css(of_css(of)); |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 185 | cg_proto = tcp_prot.proto_cgroup(memcg); |
| 186 | if (!cg_proto) |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 187 | return nbytes; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 188 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 189 | switch (of_cft(of)->private) { |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 190 | case RES_MAX_USAGE: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 191 | page_counter_reset_watermark(&cg_proto->memory_allocated); |
Glauber Costa | 0850f0f | 2011-12-11 21:47:09 +0000 | [diff] [blame] | 192 | break; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 193 | case RES_FAILCNT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 194 | cg_proto->memory_allocated.failcnt = 0; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 198 | return nbytes; |
Glauber Costa | ffea59e | 2011-12-11 21:47:08 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 201 | static struct cftype tcp_files[] = { |
| 202 | { |
| 203 | .name = "kmem.tcp.limit_in_bytes", |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 204 | .write = tcp_cgroup_write, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 205 | .read_u64 = tcp_cgroup_read, |
| 206 | .private = RES_LIMIT, |
| 207 | }, |
| 208 | { |
| 209 | .name = "kmem.tcp.usage_in_bytes", |
| 210 | .read_u64 = tcp_cgroup_read, |
| 211 | .private = RES_USAGE, |
| 212 | }, |
| 213 | { |
| 214 | .name = "kmem.tcp.failcnt", |
| 215 | .private = RES_FAILCNT, |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 216 | .write = tcp_cgroup_reset, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 217 | .read_u64 = tcp_cgroup_read, |
| 218 | }, |
| 219 | { |
| 220 | .name = "kmem.tcp.max_usage_in_bytes", |
| 221 | .private = RES_MAX_USAGE, |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 222 | .write = tcp_cgroup_reset, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 223 | .read_u64 = tcp_cgroup_read, |
| 224 | }, |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 225 | { } /* terminate */ |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 226 | }; |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 227 | |
| 228 | static int __init tcp_memcontrol_init(void) |
| 229 | { |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 230 | WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files)); |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | __initcall(tcp_memcontrol_init); |