Skip to content

Commit d1a4c0b

Browse files
glommerdavem330
authored andcommitted
tcp memory pressure controls
This patch introduces memory pressure controls for the tcp protocol. It uses the generic socket memory pressure code introduced in earlier patches, and fills in the necessary data in cg_proto struct. Signed-off-by: Glauber Costa <glommer@parallels.com> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujtisu.com> CC: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e1aab16 commit d1a4c0b

10 files changed

Lines changed: 189 additions & 5 deletions

File tree

‎Documentation/cgroups/memory.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ to trigger slab reclaim when those limits are reached.
293293
thresholds. The Memory Controller allows them to be controlled individually
294294
per cgroup, instead of globally.
295295

296+
* tcp memory pressure: sockets memory pressure for the tcp protocol.
297+
296298
3. User Interface
297299

298300
0. Configuration

‎include/linux/memcontrol.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
8686
extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
8787

8888
extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
89+
extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
8990

9091
static inline
9192
int mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *cgroup)

‎include/net/sock.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
#include <net/dst.h>
6565
#include <net/checksum.h>
6666

67+
int mem_cgroup_sockets_init(struct cgroup *cgrp, struct cgroup_subsys *ss);
68+
void mem_cgroup_sockets_destroy(struct cgroup *cgrp, struct cgroup_subsys *ss);
6769
/*
6870
* This structure really needs to be cleaned up.
6971
* Most of it is for TCP, and not used by any of

‎include/net/tcp_memcontrol.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef _TCP_MEMCG_H
2+
#define _TCP_MEMCG_H
3+
4+
struct tcp_memcontrol {
5+
struct cg_proto cg_proto;
6+
/* per-cgroup tcp memory pressure knobs */
7+
struct res_counter tcp_memory_allocated;
8+
struct percpu_counter tcp_sockets_allocated;
9+
/* those two are read-mostly, leave them at the end */
10+
long tcp_prot_mem[3];
11+
int tcp_memory_pressure;
12+
};
13+
14+
struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg);
15+
int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss);
16+
void tcp_destroy_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss);
17+
#endif /* _TCP_MEMCG_H */

‎mm/memcontrol.c‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#include <linux/cpu.h>
5151
#include <linux/oom.h>
5252
#include "internal.h"
53+
#include <net/sock.h>
54+
#include <net/tcp_memcontrol.h>
5355

5456
#include <asm/uaccess.h>
5557

@@ -295,6 +297,10 @@ struct mem_cgroup {
295297
*/
296298
struct mem_cgroup_stat_cpu nocpu_base;
297299
spinlock_t pcp_counter_lock;
300+
301+
#ifdef CONFIG_INET
302+
struct tcp_memcontrol tcp_mem;
303+
#endif
298304
};
299305

300306
/* Stuffs for move charges at task migration. */
@@ -384,6 +390,7 @@ static void mem_cgroup_put(struct mem_cgroup *memcg);
384390
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
385391
#ifdef CONFIG_INET
386392
#include <net/sock.h>
393+
#include <net/ip.h>
387394

388395
static bool mem_cgroup_is_root(struct mem_cgroup *memcg);
389396
void sock_update_memcg(struct sock *sk)
@@ -418,6 +425,15 @@ void sock_release_memcg(struct sock *sk)
418425
mem_cgroup_put(memcg);
419426
}
420427
}
428+
429+
struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
430+
{
431+
if (!memcg || mem_cgroup_is_root(memcg))
432+
return NULL;
433+
434+
return &memcg->tcp_mem.cg_proto;
435+
}
436+
EXPORT_SYMBOL(tcp_proto_cgroup);
421437
#endif /* CONFIG_INET */
422438
#endif /* CONFIG_CGROUP_MEM_RES_CTLR_KMEM */
423439

@@ -800,7 +816,7 @@ static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
800816
preempt_enable();
801817
}
802818

803-
static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
819+
struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
804820
{
805821
return container_of(cgroup_subsys_state(cont,
806822
mem_cgroup_subsys_id), struct mem_cgroup,
@@ -4732,14 +4748,34 @@ static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
47324748

47334749
ret = cgroup_add_files(cont, ss, kmem_cgroup_files,
47344750
ARRAY_SIZE(kmem_cgroup_files));
4751+
4752+
/*
4753+
* Part of this would be better living in a separate allocation
4754+
* function, leaving us with just the cgroup tree population work.
4755+
* We, however, depend on state such as network's proto_list that
4756+
* is only initialized after cgroup creation. I found the less
4757+
* cumbersome way to deal with it to defer it all to populate time
4758+
*/
4759+
if (!ret)
4760+
ret = mem_cgroup_sockets_init(cont, ss);
47354761
return ret;
47364762
};
47374763

4764+
static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4765+
struct cgroup *cont)
4766+
{
4767+
mem_cgroup_sockets_destroy(cont, ss);
4768+
}
47384769
#else
47394770
static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
47404771
{
47414772
return 0;
47424773
}
4774+
4775+
static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4776+
struct cgroup *cont)
4777+
{
4778+
}
47434779
#endif
47444780

47454781
static struct cftype mem_cgroup_files[] = {
@@ -5098,6 +5134,8 @@ static void mem_cgroup_destroy(struct cgroup_subsys *ss,
50985134
{
50995135
struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
51005136

5137+
kmem_cgroup_destroy(ss, cont);
5138+
51015139
mem_cgroup_put(memcg);
51025140
}
51035141

‎net/core/sock.c‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,46 @@
136136
#include <net/tcp.h>
137137
#endif
138138

139+
static DEFINE_RWLOCK(proto_list_lock);
140+
static LIST_HEAD(proto_list);
141+
142+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
143+
int mem_cgroup_sockets_init(struct cgroup *cgrp, struct cgroup_subsys *ss)
144+
{
145+
struct proto *proto;
146+
int ret = 0;
147+
148+
read_lock(&proto_list_lock);
149+
list_for_each_entry(proto, &proto_list, node) {
150+
if (proto->init_cgroup) {
151+
ret = proto->init_cgroup(cgrp, ss);
152+
if (ret)
153+
goto out;
154+
}
155+
}
156+
157+
read_unlock(&proto_list_lock);
158+
return ret;
159+
out:
160+
list_for_each_entry_continue_reverse(proto, &proto_list, node)
161+
if (proto->destroy_cgroup)
162+
proto->destroy_cgroup(cgrp, ss);
163+
read_unlock(&proto_list_lock);
164+
return ret;
165+
}
166+
167+
void mem_cgroup_sockets_destroy(struct cgroup *cgrp, struct cgroup_subsys *ss)
168+
{
169+
struct proto *proto;
170+
171+
read_lock(&proto_list_lock);
172+
list_for_each_entry_reverse(proto, &proto_list, node)
173+
if (proto->destroy_cgroup)
174+
proto->destroy_cgroup(cgrp, ss);
175+
read_unlock(&proto_list_lock);
176+
}
177+
#endif
178+
139179
/*
140180
* Each address family might have different locking rules, so we have
141181
* one slock key per address family:
@@ -2291,9 +2331,6 @@ void sk_common_release(struct sock *sk)
22912331
}
22922332
EXPORT_SYMBOL(sk_common_release);
22932333

2294-
static DEFINE_RWLOCK(proto_list_lock);
2295-
static LIST_HEAD(proto_list);
2296-
22972334
#ifdef CONFIG_PROC_FS
22982335
#define PROTO_INUSE_NR 64 /* should be enough for the first time */
22992336
struct prot_inuse {

‎net/ipv4/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ obj-$(CONFIG_TCP_CONG_SCALABLE) += tcp_scalable.o
4848
obj-$(CONFIG_TCP_CONG_LP) += tcp_lp.o
4949
obj-$(CONFIG_TCP_CONG_YEAH) += tcp_yeah.o
5050
obj-$(CONFIG_TCP_CONG_ILLINOIS) += tcp_illinois.o
51+
obj-$(CONFIG_CGROUP_MEM_RES_CTLR_KMEM) += tcp_memcontrol.o
5152
obj-$(CONFIG_NETLABEL) += cipso_ipv4.o
5253

5354
obj-$(CONFIG_XFRM) += xfrm4_policy.o xfrm4_state.o xfrm4_input.o \

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include <net/xfrm.h>
7474
#include <net/netdma.h>
7575
#include <net/secure_seq.h>
76+
#include <net/tcp_memcontrol.h>
7677

7778
#include <linux/inet.h>
7879
#include <linux/ipv6.h>
@@ -1917,6 +1918,7 @@ static int tcp_v4_init_sock(struct sock *sk)
19171918
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
19181919

19191920
local_bh_disable();
1921+
sock_update_memcg(sk);
19201922
sk_sockets_allocated_inc(sk);
19211923
local_bh_enable();
19221924

@@ -1974,6 +1976,7 @@ void tcp_v4_destroy_sock(struct sock *sk)
19741976
}
19751977

19761978
sk_sockets_allocated_dec(sk);
1979+
sock_release_memcg(sk);
19771980
}
19781981
EXPORT_SYMBOL(tcp_v4_destroy_sock);
19791982

@@ -2634,10 +2637,14 @@ struct proto tcp_prot = {
26342637
.compat_setsockopt = compat_tcp_setsockopt,
26352638
.compat_getsockopt = compat_tcp_getsockopt,
26362639
#endif
2640+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
2641+
.init_cgroup = tcp_init_cgroup,
2642+
.destroy_cgroup = tcp_destroy_cgroup,
2643+
.proto_cgroup = tcp_proto_cgroup,
2644+
#endif
26372645
};
26382646
EXPORT_SYMBOL(tcp_prot);
26392647

2640-
26412648
static int __net_init tcp_sk_init(struct net *net)
26422649
{
26432650
return inet_ctl_sock_create(&net->ipv4.tcp_sock,

‎net/ipv4/tcp_memcontrol.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <net/tcp.h>
2+
#include <net/tcp_memcontrol.h>
3+
#include <net/sock.h>
4+
#include <linux/memcontrol.h>
5+
#include <linux/module.h>
6+
7+
static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
8+
{
9+
return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
10+
}
11+
12+
static void memcg_tcp_enter_memory_pressure(struct sock *sk)
13+
{
14+
if (!sk->sk_cgrp->memory_pressure)
15+
*sk->sk_cgrp->memory_pressure = 1;
16+
}
17+
EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
18+
19+
int tcp_init_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
20+
{
21+
/*
22+
* The root cgroup does not use res_counters, but rather,
23+
* rely on the data already collected by the network
24+
* subsystem
25+
*/
26+
struct res_counter *res_parent = NULL;
27+
struct cg_proto *cg_proto, *parent_cg;
28+
struct tcp_memcontrol *tcp;
29+
struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
30+
struct mem_cgroup *parent = parent_mem_cgroup(memcg);
31+
32+
cg_proto = tcp_prot.proto_cgroup(memcg);
33+
if (!cg_proto)
34+
return 0;
35+
36+
tcp = tcp_from_cgproto(cg_proto);
37+
38+
tcp->tcp_prot_mem[0] = sysctl_tcp_mem[0];
39+
tcp->tcp_prot_mem[1] = sysctl_tcp_mem[1];
40+
tcp->tcp_prot_mem[2] = sysctl_tcp_mem[2];
41+
tcp->tcp_memory_pressure = 0;
42+
43+
parent_cg = tcp_prot.proto_cgroup(parent);
44+
if (parent_cg)
45+
res_parent = parent_cg->memory_allocated;
46+
47+
res_counter_init(&tcp->tcp_memory_allocated, res_parent);
48+
percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
49+
50+
cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure;
51+
cg_proto->memory_pressure = &tcp->tcp_memory_pressure;
52+
cg_proto->sysctl_mem = tcp->tcp_prot_mem;
53+
cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
54+
cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
55+
cg_proto->memcg = memcg;
56+
57+
return 0;
58+
}
59+
EXPORT_SYMBOL(tcp_init_cgroup);
60+
61+
void tcp_destroy_cgroup(struct cgroup *cgrp, struct cgroup_subsys *ss)
62+
{
63+
struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
64+
struct cg_proto *cg_proto;
65+
struct tcp_memcontrol *tcp;
66+
67+
cg_proto = tcp_prot.proto_cgroup(memcg);
68+
if (!cg_proto)
69+
return;
70+
71+
tcp = tcp_from_cgproto(cg_proto);
72+
percpu_counter_destroy(&tcp->tcp_sockets_allocated);
73+
}
74+
EXPORT_SYMBOL(tcp_destroy_cgroup);

‎net/ipv6/tcp_ipv6.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <net/netdma.h>
6363
#include <net/inet_common.h>
6464
#include <net/secure_seq.h>
65+
#include <net/tcp_memcontrol.h>
6566

6667
#include <asm/uaccess.h>
6768

@@ -1994,6 +1995,7 @@ static int tcp_v6_init_sock(struct sock *sk)
19941995
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
19951996

19961997
local_bh_disable();
1998+
sock_update_memcg(sk);
19971999
sk_sockets_allocated_inc(sk);
19982000
local_bh_enable();
19992001

@@ -2227,6 +2229,9 @@ struct proto tcpv6_prot = {
22272229
.compat_setsockopt = compat_tcp_setsockopt,
22282230
.compat_getsockopt = compat_tcp_getsockopt,
22292231
#endif
2232+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
2233+
.proto_cgroup = tcp_proto_cgroup,
2234+
#endif
22302235
};
22312236

22322237
static const struct inet6_protocol tcpv6_protocol = {

0 commit comments

Comments
 (0)