blob: ef4268d12e43d04a75ccd75bd472cf89e8d3b16d [file] [log] [blame]
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001#include <net/tcp.h>
2#include <net/tcp_memcontrol.h>
3#include <net/sock.h>
Glauber Costa3dc43e32011-12-11 21:47:05 +00004#include <net/ip.h>
5#include <linux/nsproxy.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006#include <linux/memcontrol.h>
7#include <linux/module.h>
8
Glauber Costa1d62e432012-04-09 19:36:33 -03009int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000010{
11 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -080012 * The root cgroup does not use page_counters, but rather,
Glauber Costad1a4c0b2011-12-11 21:47:04 +000013 * rely on the data already collected by the network
14 * subsystem
15 */
Glauber Costad1a4c0b2011-12-11 21:47:04 +000016 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080017 struct page_counter *counter_parent = NULL;
18 struct cg_proto *cg_proto, *parent_cg;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000019
20 cg_proto = tcp_prot.proto_cgroup(memcg);
21 if (!cg_proto)
Tejun Heo6bc10342012-04-01 12:09:55 -070022 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000023
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070024 cg_proto->memory_pressure = 0;
25 cg_proto->memcg = memcg;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000026
27 parent_cg = tcp_prot.proto_cgroup(parent);
28 if (parent_cg)
Johannes Weiner3e32cb22014-12-10 15:42:31 -080029 counter_parent = &parent_cg->memory_allocated;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000030
Johannes Weiner3e32cb22014-12-10 15:42:31 -080031 page_counter_init(&cg_proto->memory_allocated, counter_parent);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000032
Tejun Heo6bc10342012-04-01 12:09:55 -070033 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000034}
35EXPORT_SYMBOL(tcp_init_cgroup);
36
Glauber Costa1d62e432012-04-09 19:36:33 -030037void tcp_destroy_cgroup(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000038{
Glauber Costad1a4c0b2011-12-11 21:47:04 +000039 struct cg_proto *cg_proto;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000040
41 cg_proto = tcp_prot.proto_cgroup(memcg);
42 if (!cg_proto)
43 return;
44
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080045 if (cg_proto->active)
Vladimir Davydovf48b80a2015-02-12 14:59:56 -080046 static_key_slow_dec(&memcg_socket_limit_enabled);
47
Glauber Costad1a4c0b2011-12-11 21:47:04 +000048}
49EXPORT_SYMBOL(tcp_destroy_cgroup);
Glauber Costa3aaabe22011-12-11 21:47:06 +000050
Johannes Weiner3e32cb22014-12-10 15:42:31 -080051static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa3aaabe22011-12-11 21:47:06 +000052{
Glauber Costa3aaabe22011-12-11 21:47:06 +000053 struct cg_proto *cg_proto;
Glauber Costa3aaabe22011-12-11 21:47:06 +000054 int ret;
55
56 cg_proto = tcp_prot.proto_cgroup(memcg);
57 if (!cg_proto)
58 return -EINVAL;
59
Johannes Weiner3e32cb22014-12-10 15:42:31 -080060 ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +000061 if (ret)
62 return ret;
63
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080064 if (!cg_proto->active) {
Glauber Costa3f134612012-05-29 15:07:11 -070065 /*
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080066 * The active flag needs to be written after the static_key
Glauber Costa3f134612012-05-29 15:07:11 -070067 * update. This is what guarantees that the socket activation
68 * function is the last one to run. See sock_update_memcg() for
69 * details, and note that we don't mark any socket as belonging
70 * to this memcg until that flag is up.
71 *
72 * We need to do this, because static_keys will span multiple
73 * sites, but we can't control their order. If we mark a socket
74 * as accounted, but the accounting functions are not patched in
75 * yet, we'll lose accounting.
76 *
77 * We never race with the readers in sock_update_memcg(),
78 * because when this value change, the code to process it is not
79 * patched in yet.
Glauber Costa3f134612012-05-29 15:07:11 -070080 */
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080081 static_key_slow_inc(&memcg_socket_limit_enabled);
82 cg_proto->active = true;
Glauber Costa3f134612012-05-29 15:07:11 -070083 }
Glauber Costa3aaabe22011-12-11 21:47:06 +000084
85 return 0;
86}
87
Johannes Weiner3e32cb22014-12-10 15:42:31 -080088enum {
89 RES_USAGE,
90 RES_LIMIT,
91 RES_MAX_USAGE,
92 RES_FAILCNT,
93};
94
95static DEFINE_MUTEX(tcp_limit_mutex);
96
Tejun Heo451af502014-05-13 12:16:21 -040097static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
98 char *buf, size_t nbytes, loff_t off)
Glauber Costa3aaabe22011-12-11 21:47:06 +000099{
Tejun Heo451af502014-05-13 12:16:21 -0400100 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800101 unsigned long nr_pages;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000102 int ret = 0;
103
Tejun Heo451af502014-05-13 12:16:21 -0400104 buf = strstrip(buf);
105
106 switch (of_cft(of)->private) {
Glauber Costa3aaabe22011-12-11 21:47:06 +0000107 case RES_LIMIT:
108 /* see memcontrol.c */
Johannes Weiner650c5e52015-02-11 15:26:03 -0800109 ret = page_counter_memparse(buf, "-1", &nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000110 if (ret)
111 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800112 mutex_lock(&tcp_limit_mutex);
113 ret = tcp_update_limit(memcg, nr_pages);
114 mutex_unlock(&tcp_limit_mutex);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000115 break;
116 default:
117 ret = -EINVAL;
118 break;
119 }
Tejun Heo451af502014-05-13 12:16:21 -0400120 return ret ?: nbytes;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000121}
122
Tejun Heo182446d2013-08-08 20:11:24 -0400123static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000124{
Tejun Heo182446d2013-08-08 20:11:24 -0400125 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800126 struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000127 u64 val;
128
129 switch (cft->private) {
130 case RES_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800131 if (!cg_proto)
132 return PAGE_COUNTER_MAX;
133 val = cg_proto->memory_allocated.limit;
134 val *= PAGE_SIZE;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000135 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000136 case RES_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800137 if (!cg_proto)
138 val = atomic_long_read(&tcp_memory_allocated);
139 else
140 val = page_counter_read(&cg_proto->memory_allocated);
141 val *= PAGE_SIZE;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000142 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000143 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800144 if (!cg_proto)
145 return 0;
146 val = cg_proto->memory_allocated.failcnt;
147 break;
Glauber Costa0850f0f2011-12-11 21:47:09 +0000148 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800149 if (!cg_proto)
150 return 0;
151 val = cg_proto->memory_allocated.watermark;
152 val *= PAGE_SIZE;
Glauber Costaffea59e2011-12-11 21:47:08 +0000153 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000154 default:
155 BUG();
156 }
157 return val;
158}
159
Tejun Heo6770c642014-05-13 12:16:21 -0400160static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
161 char *buf, size_t nbytes, loff_t off)
Glauber Costaffea59e2011-12-11 21:47:08 +0000162{
163 struct mem_cgroup *memcg;
Glauber Costaffea59e2011-12-11 21:47:08 +0000164 struct cg_proto *cg_proto;
165
Tejun Heo6770c642014-05-13 12:16:21 -0400166 memcg = mem_cgroup_from_css(of_css(of));
Glauber Costaffea59e2011-12-11 21:47:08 +0000167 cg_proto = tcp_prot.proto_cgroup(memcg);
168 if (!cg_proto)
Tejun Heo6770c642014-05-13 12:16:21 -0400169 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000170
Tejun Heo6770c642014-05-13 12:16:21 -0400171 switch (of_cft(of)->private) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000172 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800173 page_counter_reset_watermark(&cg_proto->memory_allocated);
Glauber Costa0850f0f2011-12-11 21:47:09 +0000174 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000175 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800176 cg_proto->memory_allocated.failcnt = 0;
Glauber Costaffea59e2011-12-11 21:47:08 +0000177 break;
178 }
179
Tejun Heo6770c642014-05-13 12:16:21 -0400180 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000181}
182
Tejun Heo676f7c82012-04-01 12:09:55 -0700183static struct cftype tcp_files[] = {
184 {
185 .name = "kmem.tcp.limit_in_bytes",
Tejun Heo451af502014-05-13 12:16:21 -0400186 .write = tcp_cgroup_write,
Tejun Heo676f7c82012-04-01 12:09:55 -0700187 .read_u64 = tcp_cgroup_read,
188 .private = RES_LIMIT,
189 },
190 {
191 .name = "kmem.tcp.usage_in_bytes",
192 .read_u64 = tcp_cgroup_read,
193 .private = RES_USAGE,
194 },
195 {
196 .name = "kmem.tcp.failcnt",
197 .private = RES_FAILCNT,
Tejun Heo6770c642014-05-13 12:16:21 -0400198 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700199 .read_u64 = tcp_cgroup_read,
200 },
201 {
202 .name = "kmem.tcp.max_usage_in_bytes",
203 .private = RES_MAX_USAGE,
Tejun Heo6770c642014-05-13 12:16:21 -0400204 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700205 .read_u64 = tcp_cgroup_read,
206 },
Tejun Heo6bc10342012-04-01 12:09:55 -0700207 { } /* terminate */
Tejun Heo676f7c82012-04-01 12:09:55 -0700208};
Tejun Heo6bc10342012-04-01 12:09:55 -0700209
210static int __init tcp_memcontrol_init(void)
211{
Tejun Heo2cf669a2014-07-15 11:05:09 -0400212 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
Tejun Heo6bc10342012-04-01 12:09:55 -0700213 return 0;
214}
215__initcall(tcp_memcontrol_init);