blob: 272327134a1b45d9b78f5d4c49d1bf94133780a6 [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);
Tejun Heo908c7f12014-09-08 09:51:29 +090035 percpu_counter_init(&cg_proto->sockets_allocated, 0, GFP_KERNEL);
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
Johannes Weiner3e32cb22014-12-10 15:42:31 -080053static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa3aaabe22011-12-11 21:47:06 +000054{
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
Johannes Weiner3e32cb22014-12-10 15:42:31 -080063 ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +000064 if (ret)
65 return ret;
66
67 for (i = 0; i < 3; i++)
Johannes Weiner3e32cb22014-12-10 15:42:31 -080068 cg_proto->sysctl_mem[i] = min_t(long, nr_pages,
Eric W. Biederman2e685ca2013-10-19 16:26:19 -070069 sysctl_tcp_mem[i]);
Glauber Costa3aaabe22011-12-11 21:47:06 +000070
Johannes Weiner3e32cb22014-12-10 15:42:31 -080071 if (nr_pages == PAGE_COUNTER_MAX)
Glauber Costa3f134612012-05-29 15:07:11 -070072 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080073 else {
Glauber Costa3f134612012-05-29 15:07:11 -070074 /*
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 Costa3aaabe22011-12-11 21:47:06 +000098
99 return 0;
100}
101
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800102enum {
103 RES_USAGE,
104 RES_LIMIT,
105 RES_MAX_USAGE,
106 RES_FAILCNT,
107};
108
109static DEFINE_MUTEX(tcp_limit_mutex);
110
Tejun Heo451af502014-05-13 12:16:21 -0400111static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
112 char *buf, size_t nbytes, loff_t off)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000113{
Tejun Heo451af502014-05-13 12:16:21 -0400114 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800115 unsigned long nr_pages;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000116 int ret = 0;
117
Tejun Heo451af502014-05-13 12:16:21 -0400118 buf = strstrip(buf);
119
120 switch (of_cft(of)->private) {
Glauber Costa3aaabe22011-12-11 21:47:06 +0000121 case RES_LIMIT:
122 /* see memcontrol.c */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800123 ret = page_counter_memparse(buf, &nr_pages);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000124 if (ret)
125 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800126 mutex_lock(&tcp_limit_mutex);
127 ret = tcp_update_limit(memcg, nr_pages);
128 mutex_unlock(&tcp_limit_mutex);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000129 break;
130 default:
131 ret = -EINVAL;
132 break;
133 }
Tejun Heo451af502014-05-13 12:16:21 -0400134 return ret ?: nbytes;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000135}
136
Tejun Heo182446d2013-08-08 20:11:24 -0400137static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
Glauber Costa3aaabe22011-12-11 21:47:06 +0000138{
Tejun Heo182446d2013-08-08 20:11:24 -0400139 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800140 struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
Glauber Costa3aaabe22011-12-11 21:47:06 +0000141 u64 val;
142
143 switch (cft->private) {
144 case RES_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800145 if (!cg_proto)
146 return PAGE_COUNTER_MAX;
147 val = cg_proto->memory_allocated.limit;
148 val *= PAGE_SIZE;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000149 break;
Glauber Costa5a6dd342011-12-11 21:47:07 +0000150 case RES_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800151 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 Costa5a6dd342011-12-11 21:47:07 +0000156 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000157 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800158 if (!cg_proto)
159 return 0;
160 val = cg_proto->memory_allocated.failcnt;
161 break;
Glauber Costa0850f0f2011-12-11 21:47:09 +0000162 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800163 if (!cg_proto)
164 return 0;
165 val = cg_proto->memory_allocated.watermark;
166 val *= PAGE_SIZE;
Glauber Costaffea59e2011-12-11 21:47:08 +0000167 break;
Glauber Costa3aaabe22011-12-11 21:47:06 +0000168 default:
169 BUG();
170 }
171 return val;
172}
173
Tejun Heo6770c642014-05-13 12:16:21 -0400174static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
175 char *buf, size_t nbytes, loff_t off)
Glauber Costaffea59e2011-12-11 21:47:08 +0000176{
177 struct mem_cgroup *memcg;
Glauber Costaffea59e2011-12-11 21:47:08 +0000178 struct cg_proto *cg_proto;
179
Tejun Heo6770c642014-05-13 12:16:21 -0400180 memcg = mem_cgroup_from_css(of_css(of));
Glauber Costaffea59e2011-12-11 21:47:08 +0000181 cg_proto = tcp_prot.proto_cgroup(memcg);
182 if (!cg_proto)
Tejun Heo6770c642014-05-13 12:16:21 -0400183 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000184
Tejun Heo6770c642014-05-13 12:16:21 -0400185 switch (of_cft(of)->private) {
Glauber Costa0850f0f2011-12-11 21:47:09 +0000186 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800187 page_counter_reset_watermark(&cg_proto->memory_allocated);
Glauber Costa0850f0f2011-12-11 21:47:09 +0000188 break;
Glauber Costaffea59e2011-12-11 21:47:08 +0000189 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800190 cg_proto->memory_allocated.failcnt = 0;
Glauber Costaffea59e2011-12-11 21:47:08 +0000191 break;
192 }
193
Tejun Heo6770c642014-05-13 12:16:21 -0400194 return nbytes;
Glauber Costaffea59e2011-12-11 21:47:08 +0000195}
196
Tejun Heo676f7c82012-04-01 12:09:55 -0700197static struct cftype tcp_files[] = {
198 {
199 .name = "kmem.tcp.limit_in_bytes",
Tejun Heo451af502014-05-13 12:16:21 -0400200 .write = tcp_cgroup_write,
Tejun Heo676f7c82012-04-01 12:09:55 -0700201 .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 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 },
215 {
216 .name = "kmem.tcp.max_usage_in_bytes",
217 .private = RES_MAX_USAGE,
Tejun Heo6770c642014-05-13 12:16:21 -0400218 .write = tcp_cgroup_reset,
Tejun Heo676f7c82012-04-01 12:09:55 -0700219 .read_u64 = tcp_cgroup_read,
220 },
Tejun Heo6bc10342012-04-01 12:09:55 -0700221 { } /* terminate */
Tejun Heo676f7c82012-04-01 12:09:55 -0700222};
Tejun Heo6bc10342012-04-01 12:09:55 -0700223
224static int __init tcp_memcontrol_init(void)
225{
Tejun Heo2cf669a2014-07-15 11:05:09 -0400226 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
Tejun Heo6bc10342012-04-01 12:09:55 -0700227 return 0;
228}
229__initcall(tcp_memcontrol_init);