Skip to content

Commit 3aaabe2

Browse files
glommerdavem330
authored andcommitted
tcp buffer limitation: per-cgroup limit
This patch uses the "tcp.limit_in_bytes" field of the kmem_cgroup to effectively control the amount of kernel memory pinned by a cgroup. This value is ignored in the root cgroup, and in all others, caps the value specified by the admin in the net namespaces' view of tcp_sysctl_mem. If namespaces are being used, the admin is allowed to set a value bigger than cgroup's maximum, the same way it is allowed to set pretty much unlimited values in a real box. Signed-off-by: Glauber Costa <glommer@parallels.com> Reviewed-by: Hiroyouki Kamezawa <kamezawa.hiroyu@jp.fujitsu.com> CC: David S. Miller <davem@davemloft.net> CC: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3dc43e3 commit 3aaabe2

4 files changed

Lines changed: 152 additions & 2 deletions

File tree

‎Documentation/cgroups/memory.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Brief summary of control files.
7878

7979
memory.independent_kmem_limit # select whether or not kernel memory limits are
8080
independent of user limits
81+
memory.kmem.tcp.limit_in_bytes # set/show hard limit for tcp buf memory
8182

8283
1. History
8384

‎include/net/tcp_memcontrol.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ struct tcp_memcontrol {
1414
struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg);
1515
int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss);
1616
void tcp_destroy_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss);
17+
unsigned long long tcp_max_memory(const struct mem_cgroup *memcg);
18+
void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx);
1719
#endif /* _TCP_MEMCG_H */

‎net/ipv4/sysctl_net_ipv4.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <net/cipso_ipv4.h>
2525
#include <net/inet_frag.h>
2626
#include <net/ping.h>
27+
#include <net/tcp_memcontrol.h>
2728

2829
static int zero;
2930
static int tcp_retr1_max = 255;
@@ -182,6 +183,9 @@ static int ipv4_tcp_mem(ctl_table *ctl, int write,
182183
int ret;
183184
unsigned long vec[3];
184185
struct net *net = current->nsproxy->net_ns;
186+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
187+
struct mem_cgroup *memcg;
188+
#endif
185189

186190
ctl_table tmp = {
187191
.data = &vec,
@@ -198,6 +202,16 @@ static int ipv4_tcp_mem(ctl_table *ctl, int write,
198202
if (ret)
199203
return ret;
200204

205+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
206+
rcu_read_lock();
207+
memcg = mem_cgroup_from_task(current);
208+
209+
tcp_prot_mem(memcg, vec[0], 0);
210+
tcp_prot_mem(memcg, vec[1], 1);
211+
tcp_prot_mem(memcg, vec[2], 2);
212+
rcu_read_unlock();
213+
#endif
214+
201215
net->ipv4.sysctl_tcp_mem[0] = vec[0];
202216
net->ipv4.sysctl_tcp_mem[1] = vec[1];
203217
net->ipv4.sysctl_tcp_mem[2] = vec[2];

‎net/ipv4/tcp_memcontrol.c‎

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
#include <linux/memcontrol.h>
77
#include <linux/module.h>
88

9+
static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft);
10+
static int tcp_cgroup_write(struct cgroup *cont, struct cftype *cft,
11+
const char *buffer);
12+
13+
static struct cftype tcp_files[] = {
14+
{
15+
.name = "kmem.tcp.limit_in_bytes",
16+
.write_string = tcp_cgroup_write,
17+
.read_u64 = tcp_cgroup_read,
18+
.private = RES_LIMIT,
19+
},
20+
};
21+
922
static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
1023
{
1124
return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
@@ -34,7 +47,7 @@ int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
3447

3548
cg_proto = tcp_prot.proto_cgroup(memcg);
3649
if (!cg_proto)
37-
return 0;
50+
goto create_files;
3851

3952
tcp = tcp_from_cgproto(cg_proto);
4053

@@ -57,7 +70,9 @@ int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
5770
cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
5871
cg_proto->memcg = memcg;
5972

60-
return 0;
73+
create_files:
74+
return cgroup_add_files(cgrp, ss, tcp_files,
75+
ARRAY_SIZE(tcp_files));
6176
}
6277
EXPORT_SYMBOL(tcp_init_cgroup);
6378

@@ -66,12 +81,130 @@ void tcp_destroy_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
6681
struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
6782
struct cg_proto *cg_proto;
6883
struct tcp_memcontrol *tcp;
84+
u64 val;
6985

7086
cg_proto = tcp_prot.proto_cgroup(memcg);
7187
if (!cg_proto)
7288
return;
7389

7490
tcp = tcp_from_cgproto(cg_proto);
7591
percpu_counter_destroy(&tcp->tcp_sockets_allocated);
92+
93+
val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
94+
95+
if (val != RESOURCE_MAX)
96+
jump_label_dec(&memcg_socket_limit_enabled);
7697
}
7798
EXPORT_SYMBOL(tcp_destroy_cgroup);
99+
100+
static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
101+
{
102+
struct net *net = current->nsproxy->net_ns;
103+
struct tcp_memcontrol *tcp;
104+
struct cg_proto *cg_proto;
105+
u64 old_lim;
106+
int i;
107+
int ret;
108+
109+
cg_proto = tcp_prot.proto_cgroup(memcg);
110+
if (!cg_proto)
111+
return -EINVAL;
112+
113+
if (val > RESOURCE_MAX)
114+
val = RESOURCE_MAX;
115+
116+
tcp = tcp_from_cgproto(cg_proto);
117+
118+
old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
119+
ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
120+
if (ret)
121+
return ret;
122+
123+
for (i = 0; i < 3; i++)
124+
tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT,
125+
net->ipv4.sysctl_tcp_mem[i]);
126+
127+
if (val == RESOURCE_MAX && old_lim != RESOURCE_MAX)
128+
jump_label_dec(&memcg_socket_limit_enabled);
129+
else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX)
130+
jump_label_inc(&memcg_socket_limit_enabled);
131+
132+
return 0;
133+
}
134+
135+
static int tcp_cgroup_write(struct cgroup *cont, struct cftype *cft,
136+
const char *buffer)
137+
{
138+
struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
139+
unsigned long long val;
140+
int ret = 0;
141+
142+
switch (cft->private) {
143+
case RES_LIMIT:
144+
/* see memcontrol.c */
145+
ret = res_counter_memparse_write_strategy(buffer, &val);
146+
if (ret)
147+
break;
148+
ret = tcp_update_limit(memcg, val);
149+
break;
150+
default:
151+
ret = -EINVAL;
152+
break;
153+
}
154+
return ret;
155+
}
156+
157+
static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
158+
{
159+
struct tcp_memcontrol *tcp;
160+
struct cg_proto *cg_proto;
161+
162+
cg_proto = tcp_prot.proto_cgroup(memcg);
163+
if (!cg_proto)
164+
return default_val;
165+
166+
tcp = tcp_from_cgproto(cg_proto);
167+
return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
168+
}
169+
170+
static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
171+
{
172+
struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
173+
u64 val;
174+
175+
switch (cft->private) {
176+
case RES_LIMIT:
177+
val = tcp_read_stat(memcg, RES_LIMIT, RESOURCE_MAX);
178+
break;
179+
default:
180+
BUG();
181+
}
182+
return val;
183+
}
184+
185+
unsigned long long tcp_max_memory(const struct mem_cgroup *memcg)
186+
{
187+
struct tcp_memcontrol *tcp;
188+
struct cg_proto *cg_proto;
189+
190+
cg_proto = tcp_prot.proto_cgroup((struct mem_cgroup *)memcg);
191+
if (!cg_proto)
192+
return 0;
193+
194+
tcp = tcp_from_cgproto(cg_proto);
195+
return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
196+
}
197+
198+
void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx)
199+
{
200+
struct tcp_memcontrol *tcp;
201+
struct cg_proto *cg_proto;
202+
203+
cg_proto = tcp_prot.proto_cgroup(memcg);
204+
if (!cg_proto)
205+
return;
206+
207+
tcp = tcp_from_cgproto(cg_proto);
208+
209+
tcp->tcp_prot_mem[idx] = val;
210+
}

0 commit comments

Comments
 (0)