blob: 3af522622fad1fe7b400cb7caf07425502358550 [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 /*
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 Costad1a4c0b2011-12-11 21:47:04 +000018 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
19
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)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070032 res_parent = &parent_cg->memory_allocated;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000033
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070034 res_counter_init(&cg_proto->memory_allocated, res_parent);
35 percpu_counter_init(&cg_proto->sockets_allocated, 0);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000036
Tejun Heo6bc10342012-04-01 12:09:55 -070037 return 0;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000038}
39EXPORT_SYMBOL(tcp_init_cgroup);
40
Glauber Costa1d62e432012-04-09 19:36:33 -030041void tcp_destroy_cgroup(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +000042{
Glauber Costad1a4c0b2011-12-11 21:47:04 +000043 struct cg_proto *cg_proto;
Glauber Costad1a4c0b2011-12-11 21:47:04 +000044
45 cg_proto = tcp_prot.proto_cgroup(memcg);
46 if (!cg_proto)
47 return;
48
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070049 percpu_counter_destroy(&cg_proto->sockets_allocated);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000050}
51EXPORT_SYMBOL(tcp_destroy_cgroup);
Glauber Costa3aaabe22011-12-11 21:47:06 +000052
53static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
54{
Glauber Costa3aaabe22011-12-11 21:47:06 +000055 struct cg_proto *cg_proto;
Glauber Costa3aaabe22011-12-11 21:47:06 +000056 int i;
57 int ret;
58
59 cg_proto = tcp_prot.proto_cgroup(memcg);
60 if (!cg_proto)
61 return -EINVAL;
62
Sha Zhengju6de5a8b2013-09-12 15:13:47 -070063 if (val > RES_COUNTER_MAX)
64 val = RES_COUNTER_MAX;
Glauber Costa3aaabe22011-12-11 21:47:06 +000065
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070066 ret = res_counter_set_limit(&cg_proto->memory_allocated, val);
Glauber Costa3aaabe22011-12-11 21:47:06 +000067 if (ret)
68 return ret;
69
70 for (i = 0; i < 3; i++)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070071 cg_proto->sysctl_mem[i] = min_t(long, val >> PAGE_SHIFT,
72 sysctl_tcp_mem[i]);
Glauber Costa3aaabe22011-12-11 21:47:06 +000073
Sha Zhengju6de5a8b2013-09-12 15:13:47 -070074 if (val == RES_COUNTER_MAX)
Glauber Costa3f134612012-05-29 15:07:11 -070075 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -070076 else if (val != RES_COUNTER_MAX) {
Glauber Costa3f134612012-05-29 15:07:11 -070077 /*
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 Costa3aaabe22011-12-11 21:47:06 +0000101
102 return 0;
103}
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));
Glauber Costa3aaabe22011-12-11 21:47:06 +0000109 unsigned long long val;
110 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 */
Tejun Heo451af502014-05-13 12:16:21 -0400117 ret = res_counter_memparse_write_strategy(buf, &val);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000118 if (ret)
119 break;
120 ret = tcp_update_limit(memcg, val);
121 break;
122 default:
123 ret = -EINVAL;
124 break;
125 }
Tejun Heo451af502014-05-13 12:16:21 -0400126 return ret ?: nbytes;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000127}
128
129static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
130{
Glauber Costa3aaabe22011-12-11 21:47:06 +0000131 struct cg_proto *cg_proto;
132
133 cg_proto = tcp_prot.proto_cgroup(memcg);
134 if (!cg_proto)
135 return default_val;
136
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700137 return res_counter_read_u64(&cg_proto->memory_allocated, type);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000138}
139
Glauber Costa5a6dd342011-12-11 21:47:07 +0000140static u64 tcp_read_usage(struct mem_cgroup *memcg)
141{
Glauber Costa5a6dd342011-12-11 21:47:07 +0000142 struct cg_proto *cg_proto;
143
144 cg_proto = tcp_prot.proto_cgroup(memcg);
145 if (!cg_proto)
146 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
147
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700148 return res_counter_read_u64(&cg_proto->memory_allocated, RES_USAGE);
Glauber Costa5a6dd342011-12-11 21:47:07 +0000149}
150
Tejun Heo182446d2013-08-08 20:11:24 -0400151static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000152{
Tejun Heo182446d2013-08-08 20:11:24 -0400153 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000154 u64 val;
155
156 switch (cft->private) {
157 case RES_LIMIT:
Sha Zhengju6de5a8b2013-09-12 15:13:47 -0700158 val = tcp_read_stat(memcg, RES_LIMIT, RES_COUNTER_MAX);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000159 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000160 case RES_USAGE:
161 val = tcp_read_usage(memcg);
162 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000163 case RES_FAILCNT:
Glauber Costa0850f0f2011-12-11 21:47:09 +0000164 case RES_MAX_USAGE:
165 val = tcp_read_stat(memcg, cft->private, 0);
Glauber Costaffea59e2011-12-11 21:47:08 +0000166 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000167 default:
168 BUG();
169 }
170 return val;
171}
172
Tejun Heo6770c642014-05-13 12:16:21 -0400173static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
174 char *buf, size_t nbytes, loff_t off)
Glauber Costaffea59e2011-12-11 21:47:08 +0000175{
176 struct mem_cgroup *memcg;
Glauber Costaffea59e2011-12-11 21:47:08 +0000177 struct cg_proto *cg_proto;
178
Tejun Heo6770c642014-05-13 12:16:21 -0400179 memcg = mem_cgroup_from_css(of_css(of));
Glauber Costaffea59e2011-12-11 21:47:08 +0000180 cg_proto = tcp_prot.proto_cgroup(memcg);
181 if (!cg_proto)
Tejun Heo6770c642014-05-13 12:16:21 -0400182 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000183
Tejun Heo6770c642014-05-13 12:16:21 -0400184 switch (of_cft(of)->private) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000185 case RES_MAX_USAGE:
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700186 res_counter_reset_max(&cg_proto->memory_allocated);
Glauber Costa0850f0f2011-12-11 21:47:09 +0000187 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000188 case RES_FAILCNT:
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700189 res_counter_reset_failcnt(&cg_proto->memory_allocated);
Glauber Costaffea59e2011-12-11 21:47:08 +0000190 break;
191 }
192
Tejun Heo6770c642014-05-13 12:16:21 -0400193 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000194}
195
Tejun Heo676f7c82012-04-01 12:09:55 -0700196static struct cftype tcp_files[] = {
197 {
198 .name = "kmem.tcp.limit_in_bytes",
Tejun Heo451af502014-05-13 12:16:21 -0400199 .write = tcp_cgroup_write,
Tejun Heo676f7c82012-04-01 12:09:55 -0700200 .read_u64 = tcp_cgroup_read,
201 .private = RES_LIMIT,
202 },
203 {
204 .name = "kmem.tcp.usage_in_bytes",
205 .read_u64 = tcp_cgroup_read,
206 .private = RES_USAGE,
207 },
208 {
209 .name = "kmem.tcp.failcnt",
210 .private = RES_FAILCNT,
Tejun Heo6770c642014-05-13 12:16:21 -0400211 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700212 .read_u64 = tcp_cgroup_read,
213 },
214 {
215 .name = "kmem.tcp.max_usage_in_bytes",
216 .private = RES_MAX_USAGE,
Tejun Heo6770c642014-05-13 12:16:21 -0400217 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700218 .read_u64 = tcp_cgroup_read,
219 },
Tejun Heo6bc10342012-04-01 12:09:55 -0700220 { } /* terminate */
Tejun Heo676f7c82012-04-01 12:09:55 -0700221};
Tejun Heo6bc10342012-04-01 12:09:55 -0700222
223static int __init tcp_memcontrol_init(void)
224{
Tejun Heo2cf669a2014-07-15 11:05:09 -0400225 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
Tejun Heo6bc10342012-04-01 12:09:55 -0700226 return 0;
227}
228__initcall(tcp_memcontrol_init);