Skip to content

Commit 9e17f8a

Browse files
edumazetdavem330
authored andcommitted
net: make skb_set_owner_w() more robust
skb_set_owner_w() is called from various places that assume skb->sk always point to a full blown socket (as it changes sk->sk_wmem_alloc) We'd like to attach skb to request sockets, and in the future to timewait sockets as well. For these kind of pseudo sockets, we need to take a traditional refcount and use sock_edemux() as the destructor. It is now time to un-inline skb_set_owner_w(), being too big. Fixes: ca6fb06 ("tcp: attach SYNACK messages to request sockets instead of listener") Signed-off-by: Eric Dumazet <edumazet@google.com> Bisected-by: Haiyang Zhang <haiyangz@microsoft.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent eca1e00 commit 9e17f8a

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

include/net/sock.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,8 @@ static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
19511951
}
19521952
}
19531953

1954+
void skb_set_owner_w(struct sk_buff *skb, struct sock *sk);
1955+
19541956
/*
19551957
* Queue a received datagram if it will fit. Stream and sequenced
19561958
* protocols can't normally use this as they need to fit buffers in
@@ -1959,21 +1961,6 @@ static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
19591961
* Inlined as it's very short and called for pretty much every
19601962
* packet ever received.
19611963
*/
1962-
1963-
static inline void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1964-
{
1965-
skb_orphan(skb);
1966-
skb->sk = sk;
1967-
skb->destructor = sock_wfree;
1968-
skb_set_hash_from_sk(skb, sk);
1969-
/*
1970-
* We used to take a refcount on sk, but following operation
1971-
* is enough to guarantee sk_free() wont free this sock until
1972-
* all in-flight packets are completed
1973-
*/
1974-
atomic_add(skb->truesize, &sk->sk_wmem_alloc);
1975-
}
1976-
19771964
static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
19781965
{
19791966
skb_orphan(skb);

net/core/sock.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,28 @@ void sock_wfree(struct sk_buff *skb)
16561656
}
16571657
EXPORT_SYMBOL(sock_wfree);
16581658

1659+
void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1660+
{
1661+
skb_orphan(skb);
1662+
skb->sk = sk;
1663+
#ifdef CONFIG_INET
1664+
if (unlikely(!sk_fullsock(sk))) {
1665+
skb->destructor = sock_edemux;
1666+
sock_hold(sk);
1667+
return;
1668+
}
1669+
#endif
1670+
skb->destructor = sock_wfree;
1671+
skb_set_hash_from_sk(skb, sk);
1672+
/*
1673+
* We used to take a refcount on sk, but following operation
1674+
* is enough to guarantee sk_free() wont free this sock until
1675+
* all in-flight packets are completed
1676+
*/
1677+
atomic_add(skb->truesize, &sk->sk_wmem_alloc);
1678+
}
1679+
EXPORT_SYMBOL(skb_set_owner_w);
1680+
16591681
void skb_orphan_partial(struct sk_buff *skb)
16601682
{
16611683
/* TCP stack sets skb->ooo_okay based on sk_wmem_alloc,

net/ipv4/tcp_output.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,9 +2963,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
29632963
skb_reserve(skb, MAX_TCP_HEADER);
29642964

29652965
if (attach_req) {
2966-
skb->destructor = sock_edemux;
2967-
sock_hold(req_to_sk(req));
2968-
skb->sk = req_to_sk(req);
2966+
skb_set_owner_w(skb, req_to_sk(req));
29692967
} else {
29702968
/* sk is a const pointer, because we want to express multiple
29712969
* cpu might call us concurrently.

0 commit comments

Comments
 (0)