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
->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
;
30 parent_cg
= tcp_prot
.proto_cgroup(parent
);
32 counter_parent
= &parent_cg
->memory_allocated
;
34 page_counter_init(&cg_proto
->memory_allocated
, counter_parent
);
35 percpu_counter_init(&cg_proto
->sockets_allocated
, 0, GFP_KERNEL
);
39 EXPORT_SYMBOL(tcp_init_cgroup
);
41 void tcp_destroy_cgroup(struct mem_cgroup
*memcg
)
43 struct cg_proto
*cg_proto
;
45 cg_proto
= tcp_prot
.proto_cgroup(memcg
);
49 percpu_counter_destroy(&cg_proto
->sockets_allocated
);
51 EXPORT_SYMBOL(tcp_destroy_cgroup
);
53 static int tcp_update_limit(struct mem_cgroup
*memcg
, unsigned long nr_pages
)
55 struct cg_proto
*cg_proto
;
59 cg_proto
= tcp_prot
.proto_cgroup(memcg
);
63 ret
= page_counter_limit(&cg_proto
->memory_allocated
, nr_pages
);
67 for (i
= 0; i
< 3; i
++)
68 cg_proto
->sysctl_mem
[i
] = min_t(long, nr_pages
,
71 if (nr_pages
== PAGE_COUNTER_MAX
)
72 clear_bit(MEMCG_SOCK_ACTIVE
, &cg_proto
->flags
);
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.
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.
86 * We never race with the readers in sock_update_memcg(),
87 * because when this value change, the code to process it is not
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.
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
);
109 static DEFINE_MUTEX(tcp_limit_mutex
);
111 static ssize_t
tcp_cgroup_write(struct kernfs_open_file
*of
,
112 char *buf
, size_t nbytes
, loff_t off
)
114 struct mem_cgroup
*memcg
= mem_cgroup_from_css(of_css(of
));
115 unsigned long nr_pages
;
120 switch (of_cft(of
)->private) {
122 /* see memcontrol.c */
123 ret
= page_counter_memparse(buf
, &nr_pages
);
126 mutex_lock(&tcp_limit_mutex
);
127 ret
= tcp_update_limit(memcg
, nr_pages
);
128 mutex_unlock(&tcp_limit_mutex
);
134 return ret
?: nbytes
;
137 static u64
tcp_cgroup_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
139 struct mem_cgroup
*memcg
= mem_cgroup_from_css(css
);
140 struct cg_proto
*cg_proto
= tcp_prot
.proto_cgroup(memcg
);
143 switch (cft
->private) {
146 return PAGE_COUNTER_MAX
;
147 val
= cg_proto
->memory_allocated
.limit
;
152 val
= atomic_long_read(&tcp_memory_allocated
);
154 val
= page_counter_read(&cg_proto
->memory_allocated
);
160 val
= cg_proto
->memory_allocated
.failcnt
;
165 val
= cg_proto
->memory_allocated
.watermark
;
174 static ssize_t
tcp_cgroup_reset(struct kernfs_open_file
*of
,
175 char *buf
, size_t nbytes
, loff_t off
)
177 struct mem_cgroup
*memcg
;
178 struct cg_proto
*cg_proto
;
180 memcg
= mem_cgroup_from_css(of_css(of
));
181 cg_proto
= tcp_prot
.proto_cgroup(memcg
);
185 switch (of_cft(of
)->private) {
187 page_counter_reset_watermark(&cg_proto
->memory_allocated
);
190 cg_proto
->memory_allocated
.failcnt
= 0;
197 static struct cftype tcp_files
[] = {
199 .name
= "kmem.tcp.limit_in_bytes",
200 .write
= tcp_cgroup_write
,
201 .read_u64
= tcp_cgroup_read
,
202 .private = RES_LIMIT
,
205 .name
= "kmem.tcp.usage_in_bytes",
206 .read_u64
= tcp_cgroup_read
,
207 .private = RES_USAGE
,
210 .name
= "kmem.tcp.failcnt",
211 .private = RES_FAILCNT
,
212 .write
= tcp_cgroup_reset
,
213 .read_u64
= tcp_cgroup_read
,
216 .name
= "kmem.tcp.max_usage_in_bytes",
217 .private = RES_MAX_USAGE
,
218 .write
= tcp_cgroup_reset
,
219 .read_u64
= tcp_cgroup_read
,
224 static int __init
tcp_memcontrol_init(void)
226 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys
, tcp_files
));
229 __initcall(tcp_memcontrol_init
);