Skip to content

Commit 9f1c267

Browse files
edumazetdavem330
authored andcommitted
net: memcontrol: defer call to mem_cgroup_sk_alloc()
Instead of calling mem_cgroup_sk_alloc() from BH context, it is better to call it from inet_csk_accept() in process context. Not only this removes code in mem_cgroup_sk_alloc(), but it also fixes a bug since listener might have been dismantled and css_get() might cause a use-after-free. Fixes: e994b2f ("tcp: do not lock listener to process SYN packets") Signed-off-by: Eric Dumazet <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Tejun Heo <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 529a86e commit 9f1c267

File tree

3 files changed

+5
-16
lines changed

3 files changed

+5
-16
lines changed

mm/memcontrol.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,21 +5828,6 @@ void mem_cgroup_sk_alloc(struct sock *sk)
58285828
if (!mem_cgroup_sockets_enabled)
58295829
return;
58305830

5831-
/*
5832-
* Socket cloning can throw us here with sk_memcg already
5833-
* filled. It won't however, necessarily happen from
5834-
* process context. So the test for root memcg given
5835-
* the current task's memcg won't help us in this case.
5836-
*
5837-
* Respecting the original socket's memcg is a better
5838-
* decision in this case.
5839-
*/
5840-
if (sk->sk_memcg) {
5841-
BUG_ON(mem_cgroup_is_root(sk->sk_memcg));
5842-
css_get(&sk->sk_memcg->css);
5843-
return;
5844-
}
5845-
58465831
rcu_read_lock();
58475832
memcg = mem_cgroup_from_task(current);
58485833
if (memcg == root_mem_cgroup)

net/core/sock.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,10 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
16771677
newsk->sk_dst_pending_confirm = 0;
16781678
newsk->sk_wmem_queued = 0;
16791679
newsk->sk_forward_alloc = 0;
1680+
1681+
/* sk->sk_memcg will be populated at accept() time */
1682+
newsk->sk_memcg = NULL;
1683+
16801684
atomic_set(&newsk->sk_drops, 0);
16811685
newsk->sk_send_head = NULL;
16821686
newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
@@ -1714,7 +1718,6 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
17141718
newsk->sk_incoming_cpu = raw_smp_processor_id();
17151719
atomic64_set(&newsk->sk_cookie, 0);
17161720

1717-
mem_cgroup_sk_alloc(newsk);
17181721
cgroup_sk_alloc(&newsk->sk_cgrp_data);
17191722

17201723
/*

net/ipv4/inet_connection_sock.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
475475
}
476476
spin_unlock_bh(&queue->fastopenq.lock);
477477
}
478+
mem_cgroup_sk_alloc(newsk);
478479
out:
479480
release_sock(sk);
480481
if (req)

0 commit comments

Comments
 (0)