Commit 8a59f9d1 authored by Cong Wang's avatar Cong Wang Committed by Alexei Starovoitov
Browse files

sock: Introduce sk->sk_prot->psock_update_sk_prot()



Currently sockmap calls into each protocol to update the struct
proto and replace it. This certainly won't work when the protocol
is implemented as a module, for example, AF_UNIX.

Introduce a new ops sk->sk_prot->psock_update_sk_prot(), so each
protocol can implement its own way to replace the struct proto.
This also helps get rid of symbol dependencies on CONFIG_INET.

Signed-off-by: default avatarCong Wang <cong.wang@bytedance.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210331023237.41094-11-xiyou.wangcong@gmail.com
parent a7ba4558
Loading
Loading
Loading
Loading
+3 −15
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ struct sk_psock {
	void (*saved_close)(struct sock *sk, long timeout);
	void (*saved_write_space)(struct sock *sk);
	void (*saved_data_ready)(struct sock *sk);
	int  (*psock_update_sk_prot)(struct sock *sk, bool restore);
	struct proto			*sk_proto;
	struct mutex			work_mutex;
	struct sk_psock_work_state	work_state;
@@ -395,25 +396,12 @@ static inline void sk_psock_cork_free(struct sk_psock *psock)
	}
}

static inline void sk_psock_update_proto(struct sock *sk,
					 struct sk_psock *psock,
					 struct proto *ops)
{
	/* Pairs with lockless read in sk_clone_lock() */
	WRITE_ONCE(sk->sk_prot, ops);
}

static inline void sk_psock_restore_proto(struct sock *sk,
					  struct sk_psock *psock)
{
	sk->sk_prot->unhash = psock->saved_unhash;
	if (inet_csk_has_ulp(sk)) {
		tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
	} else {
		sk->sk_write_space = psock->saved_write_space;
		/* Pairs with lockless read in sk_clone_lock() */
		WRITE_ONCE(sk->sk_prot, psock->sk_proto);
	}
	if (psock->psock_update_sk_prot)
		psock->psock_update_sk_prot(sk, true);
}

static inline void sk_psock_set_state(struct sk_psock *psock,
+3 −0
Original line number Diff line number Diff line
@@ -1184,6 +1184,9 @@ struct proto {
	void			(*unhash)(struct sock *sk);
	void			(*rehash)(struct sock *sk);
	int			(*get_port)(struct sock *sk, unsigned short snum);
#ifdef CONFIG_BPF_SYSCALL
	int			(*psock_update_sk_prot)(struct sock *sk, bool restore);
#endif

	/* Keeping track of sockets in use */
#ifdef CONFIG_PROC_FS
+1 −0
Original line number Diff line number Diff line
@@ -2203,6 +2203,7 @@ struct sk_psock;

#ifdef CONFIG_BPF_SYSCALL
struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
int tcp_bpf_update_proto(struct sock *sk, bool restore);
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
#endif /* CONFIG_BPF_SYSCALL */

+1 −0
Original line number Diff line number Diff line
@@ -518,6 +518,7 @@ static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
#ifdef CONFIG_BPF_SYSCALL
struct sk_psock;
struct proto *udp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
int udp_bpf_update_proto(struct sock *sk, bool restore);
#endif

#endif	/* _UDP_H */
+0 −5
Original line number Diff line number Diff line
@@ -562,11 +562,6 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)

	write_lock_bh(&sk->sk_callback_lock);

	if (inet_csk_has_ulp(sk)) {
		psock = ERR_PTR(-EINVAL);
		goto out;
	}

	if (sk->sk_user_data) {
		psock = ERR_PTR(-EBUSY);
		goto out;
Loading