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)
11 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
12 struct page_counter *counter_parent = NULL;
14 * The root cgroup does not use page_counters, but rather,
15 * rely on the data already collected by the network
18 if (memcg == root_mem_cgroup)
21 memcg->tcp_mem.memory_pressure = 0;
24 counter_parent = &parent->tcp_mem.memory_allocated;
26 page_counter_init(&memcg->tcp_mem.memory_allocated, counter_parent);
31 void tcp_destroy_cgroup(struct mem_cgroup *memcg)
33 if (memcg == root_mem_cgroup)
36 if (memcg->tcp_mem.active)
37 static_key_slow_dec(&memcg_socket_limit_enabled);
40 static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
44 if (memcg == root_mem_cgroup)
47 ret = page_counter_limit(&memcg->tcp_mem.memory_allocated, nr_pages);
51 if (!memcg->tcp_mem.active) {
53 * The active flag needs to be written after the static_key
54 * update. This is what guarantees that the socket activation
55 * function is the last one to run. See sock_update_memcg() for
56 * details, and note that we don't mark any socket as belonging
57 * to this memcg until that flag is up.
59 * We need to do this, because static_keys will span multiple
60 * sites, but we can't control their order. If we mark a socket
61 * as accounted, but the accounting functions are not patched in
62 * yet, we'll lose accounting.
64 * We never race with the readers in sock_update_memcg(),
65 * because when this value change, the code to process it is not
68 static_key_slow_inc(&memcg_socket_limit_enabled);
69 memcg->tcp_mem.active = true;
82 static DEFINE_MUTEX(tcp_limit_mutex);
84 static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
85 char *buf, size_t nbytes, loff_t off)
87 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
88 unsigned long nr_pages;
93 switch (of_cft(of)->private) {
95 /* see memcontrol.c */
96 ret = page_counter_memparse(buf, "-1", &nr_pages);
99 mutex_lock(&tcp_limit_mutex);
100 ret = tcp_update_limit(memcg, nr_pages);
101 mutex_unlock(&tcp_limit_mutex);
107 return ret ?: nbytes;
110 static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
112 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
115 switch (cft->private) {
117 if (memcg == root_mem_cgroup)
118 val = PAGE_COUNTER_MAX;
120 val = memcg->tcp_mem.memory_allocated.limit;
124 if (memcg == root_mem_cgroup)
125 val = atomic_long_read(&tcp_memory_allocated);
127 val = page_counter_read(&memcg->tcp_mem.memory_allocated);
131 if (memcg == root_mem_cgroup)
133 val = memcg->tcp_mem.memory_allocated.failcnt;
136 if (memcg == root_mem_cgroup)
138 val = memcg->tcp_mem.memory_allocated.watermark;
147 static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
148 char *buf, size_t nbytes, loff_t off)
150 struct mem_cgroup *memcg;
152 memcg = mem_cgroup_from_css(of_css(of));
153 if (memcg == root_mem_cgroup)
156 switch (of_cft(of)->private) {
158 page_counter_reset_watermark(&memcg->tcp_mem.memory_allocated);
161 memcg->tcp_mem.memory_allocated.failcnt = 0;
168 static struct cftype tcp_files[] = {
170 .name = "kmem.tcp.limit_in_bytes",
171 .write = tcp_cgroup_write,
172 .read_u64 = tcp_cgroup_read,
173 .private = RES_LIMIT,
176 .name = "kmem.tcp.usage_in_bytes",
177 .read_u64 = tcp_cgroup_read,
178 .private = RES_USAGE,
181 .name = "kmem.tcp.failcnt",
182 .private = RES_FAILCNT,
183 .write = tcp_cgroup_reset,
184 .read_u64 = tcp_cgroup_read,
187 .name = "kmem.tcp.max_usage_in_bytes",
188 .private = RES_MAX_USAGE,
189 .write = tcp_cgroup_reset,
190 .read_u64 = tcp_cgroup_read,
195 static int __init tcp_memcontrol_init(void)
197 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
200 __initcall(tcp_memcontrol_init);