2 #include <net/tcp_memcontrol.h>
5 #include <linux/nsproxy.h>
6 #include <linux/memcontrol.h>
7 #include <linux/module.h>
9 int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
12 * The root cgroup does not use page_counters, but rather,
13 * rely on the data already collected by the network
16 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
17 struct page_counter *counter_parent = NULL;
18 struct cg_proto *cg_proto, *parent_cg;
20 cg_proto = tcp_prot.proto_cgroup(memcg);
24 cg_proto->memory_pressure = 0;
25 cg_proto->memcg = memcg;
27 parent_cg = tcp_prot.proto_cgroup(parent);
29 counter_parent = &parent_cg->memory_allocated;
31 page_counter_init(&cg_proto->memory_allocated, counter_parent);
35 EXPORT_SYMBOL(tcp_init_cgroup);
37 void tcp_destroy_cgroup(struct mem_cgroup *memcg)
39 struct cg_proto *cg_proto;
41 cg_proto = tcp_prot.proto_cgroup(memcg);
46 static_key_slow_dec(&memcg_socket_limit_enabled);
49 EXPORT_SYMBOL(tcp_destroy_cgroup);
51 static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
53 struct cg_proto *cg_proto;
56 cg_proto = tcp_prot.proto_cgroup(memcg);
60 ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
64 if (!cg_proto->active) {
66 * The active flag needs to be written after the static_key
67 * 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.
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.
77 * We never race with the readers in sock_update_memcg(),
78 * because when this value change, the code to process it is not
81 static_key_slow_inc(&memcg_socket_limit_enabled);
82 cg_proto->active = true;
95 static DEFINE_MUTEX(tcp_limit_mutex);
97 static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
98 char *buf, size_t nbytes, loff_t off)
100 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
101 unsigned long nr_pages;
106 switch (of_cft(of)->private) {
108 /* see memcontrol.c */
109 ret = page_counter_memparse(buf, "-1", &nr_pages);
112 mutex_lock(&tcp_limit_mutex);
113 ret = tcp_update_limit(memcg, nr_pages);
114 mutex_unlock(&tcp_limit_mutex);
120 return ret ?: nbytes;
123 static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
125 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
126 struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
129 switch (cft->private) {
132 return PAGE_COUNTER_MAX;
133 val = cg_proto->memory_allocated.limit;
138 val = atomic_long_read(&tcp_memory_allocated);
140 val = page_counter_read(&cg_proto->memory_allocated);
146 val = cg_proto->memory_allocated.failcnt;
151 val = cg_proto->memory_allocated.watermark;
160 static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
161 char *buf, size_t nbytes, loff_t off)
163 struct mem_cgroup *memcg;
164 struct cg_proto *cg_proto;
166 memcg = mem_cgroup_from_css(of_css(of));
167 cg_proto = tcp_prot.proto_cgroup(memcg);
171 switch (of_cft(of)->private) {
173 page_counter_reset_watermark(&cg_proto->memory_allocated);
176 cg_proto->memory_allocated.failcnt = 0;
183 static struct cftype tcp_files[] = {
185 .name = "kmem.tcp.limit_in_bytes",
186 .write = tcp_cgroup_write,
187 .read_u64 = tcp_cgroup_read,
188 .private = RES_LIMIT,
191 .name = "kmem.tcp.usage_in_bytes",
192 .read_u64 = tcp_cgroup_read,
193 .private = RES_USAGE,
196 .name = "kmem.tcp.failcnt",
197 .private = RES_FAILCNT,
198 .write = tcp_cgroup_reset,
199 .read_u64 = tcp_cgroup_read,
202 .name = "kmem.tcp.max_usage_in_bytes",
203 .private = RES_MAX_USAGE,
204 .write = tcp_cgroup_reset,
205 .read_u64 = tcp_cgroup_read,
210 static int __init tcp_memcontrol_init(void)
212 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
215 __initcall(tcp_memcontrol_init);