blob: 6759e0d6bba1b52815a5517ed4f71d5dd5d9aa0d [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->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 Costad1a4c0b2011-12-11 21:47:04 +000029
30 parent_cg = tcp_prot.proto_cgroup(parent);
31 if (parent_cg)
Johannes Weiner3e32cb22014-12-10 15:42:31 -080032 counter_parent = &parent_cg->memory_allocated;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000033
Johannes Weiner3e32cb22014-12-10 15:42:31 -080034 page_counter_init(&cg_proto->memory_allocated, counter_parent);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000035
Tejun Heo6bc10342012-04-01 12:09:55 -070036 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000037}
38EXPORT_SYMBOL(tcp_init_cgroup);
39
Glauber Costa1d62e432012-04-09 19:36:33 -030040void tcp_destroy_cgroup(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000041{
Glauber Costad1a4c0b2011-12-11 21:47:04 +000042 struct cg_proto *cg_proto;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000043
44 cg_proto = tcp_prot.proto_cgroup(memcg);
45 if (!cg_proto)
46 return;
47
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080048 if (cg_proto->active)
Vladimir Davydovf48b80a2015-02-12 14:59:56 -080049 static_key_slow_dec(&memcg_socket_limit_enabled);
50
Glauber Costad1a4c0b2011-12-11 21:47:04 +000051}
52EXPORT_SYMBOL(tcp_destroy_cgroup);
Glauber Costa3aaabe22011-12-11 21:47:06 +000053
Johannes Weiner3e32cb22014-12-10 15:42:31 -080054static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa3aaabe22011-12-11 21:47:06 +000055{
Glauber Costa3aaabe22011-12-11 21:47:06 +000056 struct cg_proto *cg_proto;
Glauber Costa3aaabe22011-12-11 21:47:06 +000057 int i;
58 int ret;
59
60 cg_proto = tcp_prot.proto_cgroup(memcg);
61 if (!cg_proto)
62 return -EINVAL;
63
Johannes Weiner3e32cb22014-12-10 15:42:31 -080064 ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +000065 if (ret)
66 return ret;
67
68 for (i = 0; i < 3; i++)
Johannes Weiner3e32cb22014-12-10 15:42:31 -080069 cg_proto->sysctl_mem[i] = min_t(long, nr_pages,
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070070 sysctl_tcp_mem[i]);
Glauber Costa3aaabe22011-12-11 21:47:06 +000071
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080072 if (!cg_proto->active) {
Glauber Costa3f134612012-05-29 15:07:11 -070073 /*
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080074 * The active flag needs to be written after the static_key
Glauber Costa3f134612012-05-29 15:07:11 -070075 * update. This is what guarantees that the socket activation
76 * function is the last one to run. See sock_update_memcg() for
77 * details, and note that we don't mark any socket as belonging
78 * to this memcg until that flag is up.
79 *
80 * We need to do this, because static_keys will span multiple
81 * sites, but we can't control their order. If we mark a socket
82 * as accounted, but the accounting functions are not patched in
83 * yet, we'll lose accounting.
84 *
85 * We never race with the readers in sock_update_memcg(),
86 * because when this value change, the code to process it is not
87 * patched in yet.
Glauber Costa3f134612012-05-29 15:07:11 -070088 */
Vladimir Davydov9ee11ba2016-01-14 15:19:41 -080089 static_key_slow_inc(&memcg_socket_limit_enabled);
90 cg_proto->active = true;
Glauber Costa3f134612012-05-29 15:07:11 -070091 }
Glauber Costa3aaabe22011-12-11 21:47:06 +000092
93 return 0;
94}
95
Johannes Weiner3e32cb22014-12-10 15:42:31 -080096enum {
97 RES_USAGE,
98 RES_LIMIT,
99 RES_MAX_USAGE,
100 RES_FAILCNT,
101};
102
103static DEFINE_MUTEX(tcp_limit_mutex);
104
Tejun Heo451af502014-05-13 12:16:21 -0400105static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
106 char *buf, size_t nbytes, loff_t off)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000107{
Tejun Heo451af502014-05-13 12:16:21 -0400108 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800109 unsigned long nr_pages;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000110 int ret = 0;
111
Tejun Heo451af502014-05-13 12:16:21 -0400112 buf = strstrip(buf);
113
114 switch (of_cft(of)->private) {
Glauber Costa3aaabe22011-12-11 21:47:06 +0000115 case RES_LIMIT:
116 /* see memcontrol.c */
Johannes Weiner650c5e52015-02-11 15:26:03 -0800117 ret = page_counter_memparse(buf, "-1", &nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000118 if (ret)
119 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800120 mutex_lock(&tcp_limit_mutex);
121 ret = tcp_update_limit(memcg, nr_pages);
122 mutex_unlock(&tcp_limit_mutex);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000123 break;
124 default:
125 ret = -EINVAL;
126 break;
127 }
Tejun Heo451af502014-05-13 12:16:21 -0400128 return ret ?: nbytes;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000129}
130
Tejun Heo182446d2013-08-08 20:11:24 -0400131static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000132{
Tejun Heo182446d2013-08-08 20:11:24 -0400133 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800134 struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000135 u64 val;
136
137 switch (cft->private) {
138 case RES_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800139 if (!cg_proto)
140 return PAGE_COUNTER_MAX;
141 val = cg_proto->memory_allocated.limit;
142 val *= PAGE_SIZE;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000143 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000144 case RES_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800145 if (!cg_proto)
146 val = atomic_long_read(&tcp_memory_allocated);
147 else
148 val = page_counter_read(&cg_proto->memory_allocated);
149 val *= PAGE_SIZE;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000150 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000151 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800152 if (!cg_proto)
153 return 0;
154 val = cg_proto->memory_allocated.failcnt;
155 break;
Glauber Costa0850f0f2011-12-11 21:47:09 +0000156 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800157 if (!cg_proto)
158 return 0;
159 val = cg_proto->memory_allocated.watermark;
160 val *= PAGE_SIZE;
Glauber Costaffea59e2011-12-11 21:47:08 +0000161 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000162 default:
163 BUG();
164 }
165 return val;
166}
167
Tejun Heo6770c642014-05-13 12:16:21 -0400168static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
169 char *buf, size_t nbytes, loff_t off)
Glauber Costaffea59e2011-12-11 21:47:08 +0000170{
171 struct mem_cgroup *memcg;
Glauber Costaffea59e2011-12-11 21:47:08 +0000172 struct cg_proto *cg_proto;
173
Tejun Heo6770c642014-05-13 12:16:21 -0400174 memcg = mem_cgroup_from_css(of_css(of));
Glauber Costaffea59e2011-12-11 21:47:08 +0000175 cg_proto = tcp_prot.proto_cgroup(memcg);
176 if (!cg_proto)
Tejun Heo6770c642014-05-13 12:16:21 -0400177 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000178
Tejun Heo6770c642014-05-13 12:16:21 -0400179 switch (of_cft(of)->private) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000180 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800181 page_counter_reset_watermark(&cg_proto->memory_allocated);
Glauber Costa0850f0f2011-12-11 21:47:09 +0000182 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000183 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800184 cg_proto->memory_allocated.failcnt = 0;
Glauber Costaffea59e2011-12-11 21:47:08 +0000185 break;
186 }
187
Tejun Heo6770c642014-05-13 12:16:21 -0400188 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000189}
190
Tejun Heo676f7c82012-04-01 12:09:55 -0700191static struct cftype tcp_files[] = {
192 {
193 .name = "kmem.tcp.limit_in_bytes",
Tejun Heo451af502014-05-13 12:16:21 -0400194 .write = tcp_cgroup_write,
Tejun Heo676f7c82012-04-01 12:09:55 -0700195 .read_u64 = tcp_cgroup_read,
196 .private = RES_LIMIT,
197 },
198 {
199 .name = "kmem.tcp.usage_in_bytes",
200 .read_u64 = tcp_cgroup_read,
201 .private = RES_USAGE,
202 },
203 {
204 .name = "kmem.tcp.failcnt",
205 .private = RES_FAILCNT,
Tejun Heo6770c642014-05-13 12:16:21 -0400206 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700207 .read_u64 = tcp_cgroup_read,
208 },
209 {
210 .name = "kmem.tcp.max_usage_in_bytes",
211 .private = RES_MAX_USAGE,
Tejun Heo6770c642014-05-13 12:16:21 -0400212 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700213 .read_u64 = tcp_cgroup_read,
214 },
Tejun Heo6bc10342012-04-01 12:09:55 -0700215 { } /* terminate */
Tejun Heo676f7c82012-04-01 12:09:55 -0700216};
Tejun Heo6bc10342012-04-01 12:09:55 -0700217
218static int __init tcp_memcontrol_init(void)
219{
Tejun Heo2cf669a2014-07-15 11:05:09 -0400220 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
Tejun Heo6bc10342012-04-01 12:09:55 -0700221 return 0;
222}
223__initcall(tcp_memcontrol_init);