Skip to content

Commit 8bbabb3

Browse files
Cong Wangborkmann
authored andcommitted
bpf, sock_map: Move cancel_work_sync() out of sock lock
Stanislav reported a lockdep warning, which is caused by the cancel_work_sync() called inside sock_map_close(), as analyzed below by Jakub: psock->work.func = sk_psock_backlog() ACQUIRE psock->work_mutex sk_psock_handle_skb() skb_send_sock() __skb_send_sock() sendpage_unlocked() kernel_sendpage() sock->ops->sendpage = inet_sendpage() sk->sk_prot->sendpage = tcp_sendpage() ACQUIRE sk->sk_lock tcp_sendpage_locked() RELEASE sk->sk_lock RELEASE psock->work_mutex sock_map_close() ACQUIRE sk->sk_lock sk_psock_stop() sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED) cancel_work_sync() __cancel_work_timer() __flush_work() // wait for psock->work to finish RELEASE sk->sk_lock We can move the cancel_work_sync() out of the sock lock protection, but still before saved_close() was called. Fixes: 799aa7f ("skmsg: Avoid lock_sock() in sk_psock_backlog()") Reported-by: Stanislav Fomichev <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Jakub Sitnicki <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a778f5d commit 8bbabb3

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

include/linux/skmsg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static inline void sk_psock_report_error(struct sk_psock *psock, int err)
376376
}
377377

378378
struct sk_psock *sk_psock_init(struct sock *sk, int node);
379-
void sk_psock_stop(struct sk_psock *psock, bool wait);
379+
void sk_psock_stop(struct sk_psock *psock);
380380

381381
#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
382382
int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);

net/core/skmsg.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,16 +803,13 @@ static void sk_psock_link_destroy(struct sk_psock *psock)
803803
}
804804
}
805805

806-
void sk_psock_stop(struct sk_psock *psock, bool wait)
806+
void sk_psock_stop(struct sk_psock *psock)
807807
{
808808
spin_lock_bh(&psock->ingress_lock);
809809
sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
810810
sk_psock_cork_free(psock);
811811
__sk_psock_zap_ingress(psock);
812812
spin_unlock_bh(&psock->ingress_lock);
813-
814-
if (wait)
815-
cancel_work_sync(&psock->work);
816813
}
817814

818815
static void sk_psock_done_strp(struct sk_psock *psock);
@@ -850,7 +847,7 @@ void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
850847
sk_psock_stop_verdict(sk, psock);
851848
write_unlock_bh(&sk->sk_callback_lock);
852849

853-
sk_psock_stop(psock, false);
850+
sk_psock_stop(psock);
854851

855852
INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);
856853
queue_rcu_work(system_wq, &psock->rwork);

net/core/sock_map.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ void sock_map_destroy(struct sock *sk)
15961596
saved_destroy = psock->saved_destroy;
15971597
sock_map_remove_links(sk, psock);
15981598
rcu_read_unlock();
1599-
sk_psock_stop(psock, false);
1599+
sk_psock_stop(psock);
16001600
sk_psock_put(sk, psock);
16011601
saved_destroy(sk);
16021602
}
@@ -1619,9 +1619,10 @@ void sock_map_close(struct sock *sk, long timeout)
16191619
saved_close = psock->saved_close;
16201620
sock_map_remove_links(sk, psock);
16211621
rcu_read_unlock();
1622-
sk_psock_stop(psock, true);
1623-
sk_psock_put(sk, psock);
1622+
sk_psock_stop(psock);
16241623
release_sock(sk);
1624+
cancel_work_sync(&psock->work);
1625+
sk_psock_put(sk, psock);
16251626
saved_close(sk, timeout);
16261627
}
16271628
EXPORT_SYMBOL_GPL(sock_map_close);

0 commit comments

Comments
 (0)