Skip to contentSkip to conversation

bpf,sockmap: fix sk->sk_forward_alloc warn_on in sk_stream_kill_queues

PATCH Applied Wang Yufen 5 participants 7 messages May 24, 2022 – May 31, 2022

Subsystems (7)
Networking Network drivers BPF TCP +3 more subsystems
lore
Share & tools
lore

Wang Yufen

author
#0 lore raw

PATCH 1/1 bpf,sockmap: fix sk->sk_forward_alloc warn_on in sk_stream_kill_queues

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
 inet_csk_destroy_sock+0x55/0x110
 inet_csk_listen_stop+0xbb/0x380
 tcp_close+0x41b/0x480
 inet_release+0x42/0x80
 __sock_release+0x3d/0xa0
 sock_close+0x11/0x20
 __fput+0x9d/0x240
 task_work_run+0x62/0x90
 exit_to_user_mode_prepare+0x110/0x120
 syscall_exit_to_user_mode+0x27/0x190
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

To fix, add sock_map_destroy to release psocks.

---
 include/linux/bpf.h   |  1 +
 include/linux/skmsg.h |  1 +
 net/core/skmsg.c      |  1 +
 net/core/sock_map.c   | 23 +++++++++++++++++++++++
 net/ipv4/tcp_bpf.c    |  1 +
 5 files changed, 27 insertions(+)
include/linux/bpf.h +1 -0
index cc4d5e394031..c4de82d5e72c 100644
@@ -2092,6 +2092,7 @@ int sock_map_bpf_prog_query(const union bpf_attr *attr,
 			    union bpf_attr __user *uattr);
 
 void sock_map_unhash(struct sock *sk);
+void sock_map_destroy(struct sock *sk);
 void sock_map_close(struct sock *sk, long timeout);
 #else
 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
include/linux/skmsg.h +1 -0
index c5a2d6f50f25..153b6dec9b6a 100644
@@ -95,6 +95,7 @@ struct sk_psock {
 	spinlock_t			link_lock;
 	refcount_t			refcnt;
 	void (*saved_unhash)(struct sock *sk);
+	void (*saved_destroy)(struct sock *sk);
 	void (*saved_close)(struct sock *sk, long timeout);
 	void (*saved_write_space)(struct sock *sk);
 	void (*saved_data_ready)(struct sock *sk);
net/core/skmsg.c +1 -0
index 22b983ade0e7..7e03f96e441b 100644
@@ -715,6 +715,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)
 	psock->eval = __SK_NONE;
 	psock->sk_proto = prot;
 	psock->saved_unhash = prot->unhash;
+	psock->saved_destroy = prot->destroy;
 	psock->saved_close = prot->close;
 	psock->saved_write_space = sk->sk_write_space;
 
net/core/sock_map.c +23 -0
index 81d4b4756a02..9f08ccfaf6da 100644
@@ -1561,6 +1561,29 @@ void sock_map_unhash(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(sock_map_unhash);
 
+void sock_map_destroy(struct sock *sk)
+{
+	void (*saved_destroy)(struct sock *sk);
+	struct sk_psock *psock;
+
+	rcu_read_lock();
+	psock = sk_psock_get(sk);
+	if (unlikely(!psock)) {
+		rcu_read_unlock();
+		if (sk->sk_prot->destroy)
+			sk->sk_prot->destroy(sk);
+		return;
+	}
+
+	saved_destroy = psock->saved_destroy;
+	sock_map_remove_links(sk, psock);
+	rcu_read_unlock();
+	sk_psock_stop(psock, true);
+	sk_psock_put(sk, psock);
+	saved_destroy(sk);
+}
+EXPORT_SYMBOL_GPL(sock_map_destroy);
+
 void sock_map_close(struct sock *sk, long timeout)
 {
 	void (*saved_close)(struct sock *sk, long timeout);
net/ipv4/tcp_bpf.c +1 -0
index be3947e70fec..38550bb1b90b 100644
@@ -540,6 +540,7 @@ static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
 				   struct proto *base)
 {
 	prot[TCP_BPF_BASE]			= *base;
+	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
 	prot[TCP_BPF_BASE].close		= sock_map_close;
 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
Signature · 1 line
2.25.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 22 lines · Wang Yufen

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
 inet_csk_destroy_sock+0x55/0x110
 inet_csk_listen_stop+0xbb/0x380
 tcp_close+0x41b/0x480
 inet_release+0x42/0x80
 __sock_release+0x3d/0xa0
 sock_close+0x11/0x20
 __fput+0x9d/0x240
 task_work_run+0x62/0x90
 exit_to_user_mode_prepare+0x110/0x120
 syscall_exit_to_user_mode+0x27/0x190
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

Quoted 4 lines

To fix, add sock_map_destroy to release psocks.

---

Thanks for the fix.

#2 lore raw
In reply to Wang Yufen
Quoted 16 lines · Wang Yufen

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
 inet_csk_destroy_sock+0x55/0x110
 inet_csk_listen_stop+0xbb/0x380
 tcp_close+0x41b/0x480
 inet_release+0x42/0x80
 __sock_release+0x3d/0xa0
 sock_close+0x11/0x20
 __fput+0x9d/0x240
 task_work_run+0x62/0x90
 exit_to_user_mode_prepare+0x110/0x120
 syscall_exit_to_user_mode+0x27/0x190
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 6 lines

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

Hm, in this scenario, how does the child socket end up in the sockmap? Clearly user-space does not have a chance to get an fd yet.

And, how does your patch work? Since the child sock does not even inheirt the sock proto after clone (see the comments above tcp_bpf_clone()) at all?

Thanks.

author
#3 lore raw
Quoted 26 lines · Cong Wang
Quoted 16 lines · Wang Yufen

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
  inet_csk_destroy_sock+0x55/0x110
  inet_csk_listen_stop+0xbb/0x380
  tcp_close+0x41b/0x480
  inet_release+0x42/0x80
  __sock_release+0x3d/0xa0
  sock_close+0x11/0x20
  __fput+0x9d/0x240
  task_work_run+0x62/0x90
  exit_to_user_mode_prepare+0x110/0x120
  syscall_exit_to_user_mode+0x27/0x190
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 6 lines

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

Hm, in this scenario, how does the child socket end up in the sockmap? Clearly user-space does not have a chance to get an fd yet.

Quoted 6 lines

And, how does your patch work? Since the child sock does not even inheirt the sock proto after clone (see the comments above tcp_bpf_clone()) at all?

Thanks.
.

My test cases are as follows:

__section("sockops")
int bpf_sockmap(struct bpf_sock_ops *skops)
{
    switch (skops->op) {
        case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
        case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
            ...
            bpf_sock_hash_update(skops, &sock_ops_map, &key, BPF_NOEXIST); break;
        ...
}

__section("sk_msg")
int bpf_redir(struct sk_msg_md *msg)
{
    ...
    bpf_msg_redirect_hash(msg, &sock_ops_map, &key, BPF_F_INGRESS); return SK_PASS;
}

//tcp_server
int main(char **argv)
{
    int sk = 0;
    int port, ret;
    struct sockaddr_in addr;

    signal(SIGCHLD, SIG_IGN);

    sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sk < 0) {
        perror("Can't create socket"); return -1;
    }

    port = atoi(argv[1]);
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);

    printf("Binding to port %d\n", port);

    ret = bind(sk, (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) {
        perror("Can't bind socket");
        return -1;
    }

    ret = listen(sk, size);
    if (ret < 0) {
        perror("Can't put sock to listen");
        return -1;
    }

    printf("Waiting for connections\n");
    while (1) {
        //not accpet
        sleep(1);
    }
}

//tcp_client
int main(char **argv)
{
    int port, write_size;
    int val[10], rval[10];
    int sk = 0;

    port = atoi(argv[2]);
    val[0] = 1;

    sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sk < 0) {
        perror("Can't create socket"); return -1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    inet_aton(argv[1], &addr.sin_addr);
    addr.sin_port = htons(port);

    ret = connect(sk[i], (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) {
        perror("Can't connect");
        return -1;
    }

   while (1) {
        printf("send %d -> %d\n", val[0], val[0]); write(sk, &val, sizeof(val)); val[0]++;
        sleep(1);
   }
}

1. start tcp_server
2. start tcp_client
3. kill tcp_server
The problem can be reproduced easily.

maintainer
#4 lore raw
Quoted 35 lines · wangyufen
Quoted 26 lines · Cong Wang
Quoted 16 lines · Wang Yufen

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
  inet_csk_destroy_sock+0x55/0x110
  inet_csk_listen_stop+0xbb/0x380
  tcp_close+0x41b/0x480
  inet_release+0x42/0x80
  __sock_release+0x3d/0xa0
  sock_close+0x11/0x20
  __fput+0x9d/0x240
  task_work_run+0x62/0x90
  exit_to_user_mode_prepare+0x110/0x120
  syscall_exit_to_user_mode+0x27/0x190
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 6 lines

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

Hm, in this scenario, how does the child socket end up in the sockmap? Clearly user-space does not have a chance to get an fd yet.

Quoted 6 lines

And, how does your patch work? Since the child sock does not even inheirt the sock proto after clone (see the comments above tcp_bpf_clone()) at all?

Thanks.
.

My test cases are as follows:

Quoted 11 lines

__section("sockops")
int bpf_sockmap(struct bpf_sock_ops *skops)
{
    switch (skops->op) {
        case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
        case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
            ...
            bpf_sock_hash_update(skops, &sock_ops_map, &key, BPF_NOEXIST); break;
        ...
}

Right, when processing the final ACK in tcp_rcv_state_process(), we invoke the BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB BPF callback.

This gives a chance to install sockmap sk_prot callbacks.

An accept() without ever calling accept() ;-)

[...]

maintainer Acked-by
#5 lore raw
Quoted 48 lines · Jakub Sitnicki
Quoted 35 lines · wangyufen
Quoted 26 lines · Cong Wang
Quoted 16 lines · Wang Yufen

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
  inet_csk_destroy_sock+0x55/0x110
  inet_csk_listen_stop+0xbb/0x380
  tcp_close+0x41b/0x480
  inet_release+0x42/0x80
  __sock_release+0x3d/0xa0
  sock_close+0x11/0x20
  __fput+0x9d/0x240
  task_work_run+0x62/0x90
  exit_to_user_mode_prepare+0x110/0x120
  syscall_exit_to_user_mode+0x27/0x190
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 6 lines

The reason we observed is that:
When the listener is closing, a connection may have completed the three-way handshake but not accepted, and the client has sent some packets. The child sks in accept queue release by inet_child_forget()->inet_csk_destroy_sock(), but psocks of child sks have not released.

Hm, in this scenario, how does the child socket end up in the sockmap? Clearly user-space does not have a chance to get an fd yet.

Quoted 6 lines

And, how does your patch work? Since the child sock does not even inheirt the sock proto after clone (see the comments above tcp_bpf_clone()) at all?

Thanks.
.

My test cases are as follows:

Quoted 11 lines

__section("sockops")
int bpf_sockmap(struct bpf_sock_ops *skops)
{
    switch (skops->op) {
        case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
        case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
            ...
            bpf_sock_hash_update(skops, &sock_ops_map, &key, BPF_NOEXIST); break;
        ...
}

Quoted 8 lines

Right, when processing the final ACK in tcp_rcv_state_process(), we invoke the BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB BPF callback.

This gives a chance to install sockmap sk_prot callbacks.

An accept() without ever calling accept() ;-)

[...]

LGTM as well.

#6 lore raw
In reply to Wang Yufen
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 16 lines · On Tue, 24 May 2022 15:53:11 +0800 you wrote:

During TCP sockmap redirect pressure test, the following warning is triggered:

WARNING: CPU: 3 PID: 2145 at net/core/stream.c:205 sk_stream_kill_queues+0xbc/0xd0
CPU: 3 PID: 2145 Comm: iperf Kdump: loaded Tainted: G        W         5.10.0+ #9
Call Trace:
 inet_csk_destroy_sock+0x55/0x110
 inet_csk_listen_stop+0xbb/0x380
 tcp_close+0x41b/0x480
 inet_release+0x42/0x80
 __sock_release+0x3d/0xa0
 sock_close+0x11/0x20
 __fput+0x9d/0x240
 task_work_run+0x62/0x90
 exit_to_user_mode_prepare+0x110/0x120
 syscall_exit_to_user_mode+0x27/0x190
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next] bpf,sockmap: fix sk->sk_forward_alloc warn_on in sk_stream_kill_queues

    https://git.kernel.org/bpf/bpf-next/c/84dc313f7b79

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html