Skip to contentSkip to conversation

bpf: net: Set sk_bpf_storage back to NULL for cloned sk

PATCH Applied Martin KaFai Lau 4 participants 6 messages Jun 11, 2019 – Jul 16, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: net: Set sk_bpf_storage back to NULL for cloned sk

The cloned sk should not carry its parent-listener's sk_bpf_storage. This patch fixes it by setting it back to NULL.

Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +3 -0
index 2b3701958486..d90fd04622e5 100644
@@ -1850,6 +1850,9 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 			goto out;
 		}
 		RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+#ifdef CONFIG_BPF_SYSCALL
+		RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
+#endif
 
 		newsk->sk_err	   = 0;
 		newsk->sk_err_soft = 0;
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Martin KaFai Lau

The cloned sk should not carry its parent-listener's sk_bpf_storage. This patch fixes it by setting it back to NULL.

Makes sense.

Quoted 24 lines
Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +3 -1
index 2b3701958486..d90fd04622e5 100644
@@ -1850,6 +1850,9 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
                        goto out;
                }
                RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+#ifdef CONFIG_BPF_SYSCALL
+               RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
+#endif
 
                newsk->sk_err      = 0;
                newsk->sk_err_soft = 0;
--

2.17.1

#3 lore raw
In reply to Martin KaFai Lau
Quoted 2 lines · Martin KaFai Lau

The cloned sk should not carry its parent-listener's sk_bpf_storage. This patch fixes it by setting it back to NULL.

Have you thought about some kind of inheritance for listener sockets' storage? Suppose I have a situation where I write something to listener's sk storage (directly or via recently added sockopts hooks) and I want to inherit that state for a freshly established connection.

I was looking into adding possibility to call bpf_get_listener_sock form BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB callback to manually copy some data form the listener socket, but I don't think at this point there is any association between newly established socket and the listener.

Thoughts/ideas?

(Btw, sorry for digging up this old mail, but it feels relevant).

Quoted 23 lines
Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
---
 net/core/sock.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock.c +3 -0
index 2b3701958486..d90fd04622e5 100644
@@ -1850,6 +1850,9 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 			goto out;
 		}
 		RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+#ifdef CONFIG_BPF_SYSCALL
+		RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
+#endif
 
 		newsk->sk_err	   = 0;
 		newsk->sk_err_soft = 0;
Signature · 1 line
2.17.1
author
#4 lore raw
Quoted 8 lines · Stanislav Fomichev
Quoted 2 lines · Martin KaFai Lau

The cloned sk should not carry its parent-listener's sk_bpf_storage. This patch fixes it by setting it back to NULL.

Have you thought about some kind of inheritance for listener sockets' storage? Suppose I have a situation where I write something to listener's sk storage (directly or via recently added sockopts hooks) and I want to inherit that state for a freshly established connection.

Quoted 5 lines

I was looking into adding possibility to call bpf_get_listener_sock form BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB callback to manually copy some data form the listener socket, but I don't think at this point there is any association between newly established socket and the listener.

Right, at that point, the child sk has no reference back to the listener's sk.

After a quick look, the listener sk may not always be available also (e.g. the backlog processing case). Hence, adding the listener sk to the bpf running ctx is not obvious either.

Quoted 2 lines

Thoughts/ideas?

I think cloning the listener's bpf sk storage could be added to the existing sk cloning logic. It seems to be a more straight forward approach instead of figuring out the right place to call another bpf prog to clone it.

Quick thoughts out of my head:
1. Default should be not-to-clone. Have a way (a map's flag?) to opt-in.
2. The listener's sk storage could be being modified while being cloned. One possibility is to check if the value has bpf_spin_lock. If there is, lock it before cloning.

#5 lore raw
Quoted 29 lines · Martin Lau
Quoted 8 lines · Stanislav Fomichev
Quoted 2 lines · Martin KaFai Lau

The cloned sk should not carry its parent-listener's sk_bpf_storage. This patch fixes it by setting it back to NULL.

Have you thought about some kind of inheritance for listener sockets' storage? Suppose I have a situation where I write something to listener's sk storage (directly or via recently added sockopts hooks) and I want to inherit that state for a freshly established connection.

Quoted 5 lines

I was looking into adding possibility to call bpf_get_listener_sock form BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB callback to manually copy some data form the listener socket, but I don't think at this point there is any association between newly established socket and the listener.

Right, at that point, the child sk has no reference back to the listener's sk.

After a quick look, the listener sk may not always be available also (e.g. the backlog processing case). Hence, adding the listener sk to the bpf running ctx is not obvious either.

Quoted 2 lines

Thoughts/ideas?

I think cloning the listener's bpf sk storage could be added to the existing sk cloning logic. It seems to be a more straight forward approach instead of figuring out the right place to call another bpf prog to clone it.

Quoted 5 lines

Quick thoughts out of my head:
1. Default should be not-to-clone. Have a way (a map's flag?) to opt-in.
2. The listener's sk storage could be being modified while being cloned. One possibility is to check if the value has bpf_spin_lock. If there is, lock it before cloning.

Thanks for suggestion! An optional inherit/clone flag to bpf_sk_storage_get seems like a good option. I'll try to play with it, will probably get back with an rfc at some point.