]> git.baikalelectronics.ru Git - kernel.git/commitdiff
bpf, sock_map: Move cancel_work_sync() out of sock lock
authorCong Wang <cong.wang@bytedance.com>
Wed, 2 Nov 2022 04:34:17 +0000 (21:34 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 3 Nov 2022 12:51:06 +0000 (13:51 +0100)
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: 57ed7953ff81 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Reported-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Jakub Sitnicki <jakub@cloudflare.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Jakub Sitnicki <jakub@cloudflare.com>
Link: https://lore.kernel.org/bpf/20221102043417.279409-1-xiyou.wangcong@gmail.com
include/linux/skmsg.h
net/core/skmsg.c
net/core/sock_map.c

index 48f4b645193b7d8ec3882bbc73ddb07e212a069c..70d6cb94e5802d17f160f8383e7e38cae17dcfa1 100644 (file)
@@ -376,7 +376,7 @@ static inline void sk_psock_report_error(struct sk_psock *psock, int err)
 }
 
 struct sk_psock *sk_psock_init(struct sock *sk, int node);
-void sk_psock_stop(struct sk_psock *psock, bool wait);
+void sk_psock_stop(struct sk_psock *psock);
 
 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
index 1efdc47a999b44089e1abfee15a5a93269851997..e6b9ced3eda82f8af545c88eeeeaa899f54f5f9f 100644 (file)
@@ -803,16 +803,13 @@ static void sk_psock_link_destroy(struct sk_psock *psock)
        }
 }
 
-void sk_psock_stop(struct sk_psock *psock, bool wait)
+void sk_psock_stop(struct sk_psock *psock)
 {
        spin_lock_bh(&psock->ingress_lock);
        sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
        sk_psock_cork_free(psock);
        __sk_psock_zap_ingress(psock);
        spin_unlock_bh(&psock->ingress_lock);
-
-       if (wait)
-               cancel_work_sync(&psock->work);
 }
 
 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)
                sk_psock_stop_verdict(sk, psock);
        write_unlock_bh(&sk->sk_callback_lock);
 
-       sk_psock_stop(psock, false);
+       sk_psock_stop(psock);
 
        INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);
        queue_rcu_work(system_wq, &psock->rwork);
index a660baedd9e799e0e90bdc005144a96d84560a47..81beb16ab1ebfcb166f51f89a029fe1c28a629a4 100644 (file)
@@ -1596,7 +1596,7 @@ void sock_map_destroy(struct sock *sk)
        saved_destroy = psock->saved_destroy;
        sock_map_remove_links(sk, psock);
        rcu_read_unlock();
-       sk_psock_stop(psock, false);
+       sk_psock_stop(psock);
        sk_psock_put(sk, psock);
        saved_destroy(sk);
 }
@@ -1619,9 +1619,10 @@ void sock_map_close(struct sock *sk, long timeout)
        saved_close = psock->saved_close;
        sock_map_remove_links(sk, psock);
        rcu_read_unlock();
-       sk_psock_stop(psock, true);
-       sk_psock_put(sk, psock);
+       sk_psock_stop(psock);
        release_sock(sk);
+       cancel_work_sync(&psock->work);
+       sk_psock_put(sk, psock);
        saved_close(sk, timeout);
 }
 EXPORT_SYMBOL_GPL(sock_map_close);