Skip to content

Commit 4b1327b

Browse files
tracywwnjdavem330
authored andcommitted
net-memcg: pass in gfp_t mask to mem_cgroup_charge_skmem()
Add gfp_t mask as an input parameter to mem_cgroup_charge_skmem(), to give more control to the networking stack and enable it to change memcg charging behavior. In the future, the networking stack may decide to avoid oom-kills when fallbacks are more appropriate. One behavior change in mem_cgroup_charge_skmem() by this patch is to avoid force charging by default and let the caller decide when and if force charging is needed through the presence or absence of __GFP_NOFAIL. Signed-off-by: Wei Wang <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ab44035 commit 4b1327b

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

include/linux/memcontrol.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,8 @@ static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
15811581
#endif /* CONFIG_CGROUP_WRITEBACK */
15821582

15831583
struct sock;
1584-
bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1584+
bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1585+
gfp_t gfp_mask);
15851586
void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
15861587
#ifdef CONFIG_MEMCG
15871588
extern struct static_key_false memcg_sockets_enabled_key;

include/net/sock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,11 @@ static inline gfp_t gfp_any(void)
24002400
return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
24012401
}
24022402

2403+
static inline gfp_t gfp_memcg_charge(void)
2404+
{
2405+
return in_softirq() ? GFP_NOWAIT : GFP_KERNEL;
2406+
}
2407+
24032408
static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
24042409
{
24052410
return noblock ? 0 : sk->sk_rcvtimeo;

mm/memcontrol.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7048,36 +7048,34 @@ void mem_cgroup_sk_free(struct sock *sk)
70487048
* mem_cgroup_charge_skmem - charge socket memory
70497049
* @memcg: memcg to charge
70507050
* @nr_pages: number of pages to charge
7051+
* @gfp_mask: reclaim mode
70517052
*
70527053
* Charges @nr_pages to @memcg. Returns %true if the charge fit within
7053-
* @memcg's configured limit, %false if the charge had to be forced.
7054+
* @memcg's configured limit, %false if it doesn't.
70547055
*/
7055-
bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
7056+
bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
7057+
gfp_t gfp_mask)
70567058
{
7057-
gfp_t gfp_mask = GFP_KERNEL;
7058-
70597059
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
70607060
struct page_counter *fail;
70617061

70627062
if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
70637063
memcg->tcpmem_pressure = 0;
70647064
return true;
70657065
}
7066-
page_counter_charge(&memcg->tcpmem, nr_pages);
70677066
memcg->tcpmem_pressure = 1;
7067+
if (gfp_mask & __GFP_NOFAIL) {
7068+
page_counter_charge(&memcg->tcpmem, nr_pages);
7069+
return true;
7070+
}
70687071
return false;
70697072
}
70707073

7071-
/* Don't block in the packet receive path */
7072-
if (in_softirq())
7073-
gfp_mask = GFP_NOWAIT;
7074-
7075-
mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
7076-
7077-
if (try_charge(memcg, gfp_mask, nr_pages) == 0)
7074+
if (try_charge(memcg, gfp_mask, nr_pages) == 0) {
7075+
mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
70787076
return true;
7077+
}
70797078

7080-
try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages);
70817079
return false;
70827080
}
70837081

net/core/sock.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,10 +2728,12 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
27282728
{
27292729
struct proto *prot = sk->sk_prot;
27302730
long allocated = sk_memory_allocated_add(sk, amt);
2731+
bool memcg_charge = mem_cgroup_sockets_enabled && sk->sk_memcg;
27312732
bool charged = true;
27322733

2733-
if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
2734-
!(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt)))
2734+
if (memcg_charge &&
2735+
!(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt,
2736+
gfp_memcg_charge())))
27352737
goto suppress_allocation;
27362738

27372739
/* Under limit. */
@@ -2785,16 +2787,22 @@ int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
27852787
/* Fail only if socket is _under_ its sndbuf.
27862788
* In this case we cannot block, so that we have to fail.
27872789
*/
2788-
if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
2790+
if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) {
2791+
/* Force charge with __GFP_NOFAIL */
2792+
if (memcg_charge && !charged) {
2793+
mem_cgroup_charge_skmem(sk->sk_memcg, amt,
2794+
gfp_memcg_charge() | __GFP_NOFAIL);
2795+
}
27892796
return 1;
2797+
}
27902798
}
27912799

27922800
if (kind == SK_MEM_SEND || (kind == SK_MEM_RECV && charged))
27932801
trace_sock_exceed_buf_limit(sk, prot, allocated, kind);
27942802

27952803
sk_memory_allocated_sub(sk, amt);
27962804

2797-
if (mem_cgroup_sockets_enabled && sk->sk_memcg)
2805+
if (memcg_charge && charged)
27982806
mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
27992807

28002808
return 0;

net/ipv4/inet_connection_sock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
534534
atomic_read(&newsk->sk_rmem_alloc));
535535
mem_cgroup_sk_alloc(newsk);
536536
if (newsk->sk_memcg && amt)
537-
mem_cgroup_charge_skmem(newsk->sk_memcg, amt);
537+
mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
538+
GFP_KERNEL | __GFP_NOFAIL);
538539

539540
release_sock(newsk);
540541
}

net/ipv4/tcp_output.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,8 @@ void sk_forced_mem_schedule(struct sock *sk, int size)
33733373
sk_memory_allocated_add(sk, amt);
33743374

33753375
if (mem_cgroup_sockets_enabled && sk->sk_memcg)
3376-
mem_cgroup_charge_skmem(sk->sk_memcg, amt);
3376+
mem_cgroup_charge_skmem(sk->sk_memcg, amt,
3377+
gfp_memcg_charge() | __GFP_NOFAIL);
33773378
}
33783379

33793380
/* Send a FIN. The caller locks the socket for us.

0 commit comments

Comments
 (0)