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