1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Cong Wang <cong.wang@bytedance.com> */
4 #include <linux/skmsg.h>
7 #include <net/af_unix.h>
9 #define unix_sk_has_data(__sk, __psock) \
10 ({ !skb_queue_empty(&__sk->sk_receive_queue) || \
11 !skb_queue_empty(&__psock->ingress_skb) || \
12 !list_empty(&__psock->ingress_msg); \
15 static int unix_msg_wait_data(struct sock *sk, struct sk_psock *psock,
18 DEFINE_WAIT_FUNC(wait, woken_wake_function);
19 struct unix_sock *u = unix_sk(sk);
22 if (sk->sk_shutdown & RCV_SHUTDOWN)
28 add_wait_queue(sk_sleep(sk), &wait);
29 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
30 if (!unix_sk_has_data(sk, psock)) {
31 mutex_unlock(&u->iolock);
32 wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
33 mutex_lock(&u->iolock);
34 ret = unix_sk_has_data(sk, psock);
36 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
37 remove_wait_queue(sk_sleep(sk), &wait);
41 static int __unix_recvmsg(struct sock *sk, struct msghdr *msg,
42 size_t len, int flags)
44 if (sk->sk_type == SOCK_DGRAM)
45 return __unix_dgram_recvmsg(sk, msg, len, flags);
47 return __unix_stream_recvmsg(sk, msg, len, flags);
50 static int unix_bpf_recvmsg(struct sock *sk, struct msghdr *msg,
51 size_t len, int flags, int *addr_len)
53 struct unix_sock *u = unix_sk(sk);
54 struct sk_psock *psock;
57 psock = sk_psock_get(sk);
59 return __unix_recvmsg(sk, msg, len, flags);
61 mutex_lock(&u->iolock);
62 if (!skb_queue_empty(&sk->sk_receive_queue) &&
63 sk_psock_queue_empty(psock)) {
64 mutex_unlock(&u->iolock);
65 sk_psock_put(sk, psock);
66 return __unix_recvmsg(sk, msg, len, flags);
70 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
75 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
76 data = unix_msg_wait_data(sk, psock, timeo);
78 if (!sk_psock_queue_empty(psock))
80 mutex_unlock(&u->iolock);
81 sk_psock_put(sk, psock);
82 return __unix_recvmsg(sk, msg, len, flags);
86 mutex_unlock(&u->iolock);
87 sk_psock_put(sk, psock);
91 static struct proto *unix_dgram_prot_saved __read_mostly;
92 static DEFINE_SPINLOCK(unix_dgram_prot_lock);
93 static struct proto unix_dgram_bpf_prot;
95 static struct proto *unix_stream_prot_saved __read_mostly;
96 static DEFINE_SPINLOCK(unix_stream_prot_lock);
97 static struct proto unix_stream_bpf_prot;
99 static void unix_dgram_bpf_rebuild_protos(struct proto *prot, const struct proto *base)
102 prot->close = sock_map_close;
103 prot->recvmsg = unix_bpf_recvmsg;
104 prot->sock_is_readable = sk_msg_is_readable;
107 static void unix_stream_bpf_rebuild_protos(struct proto *prot,
108 const struct proto *base)
111 prot->close = sock_map_close;
112 prot->recvmsg = unix_bpf_recvmsg;
113 prot->sock_is_readable = sk_msg_is_readable;
114 prot->unhash = sock_map_unhash;
117 static void unix_dgram_bpf_check_needs_rebuild(struct proto *ops)
119 if (unlikely(ops != smp_load_acquire(&unix_dgram_prot_saved))) {
120 spin_lock_bh(&unix_dgram_prot_lock);
121 if (likely(ops != unix_dgram_prot_saved)) {
122 unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, ops);
123 smp_store_release(&unix_dgram_prot_saved, ops);
125 spin_unlock_bh(&unix_dgram_prot_lock);
129 static void unix_stream_bpf_check_needs_rebuild(struct proto *ops)
131 if (unlikely(ops != smp_load_acquire(&unix_stream_prot_saved))) {
132 spin_lock_bh(&unix_stream_prot_lock);
133 if (likely(ops != unix_stream_prot_saved)) {
134 unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, ops);
135 smp_store_release(&unix_stream_prot_saved, ops);
137 spin_unlock_bh(&unix_stream_prot_lock);
141 int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
143 if (sk->sk_type != SOCK_DGRAM)
147 sk->sk_write_space = psock->saved_write_space;
148 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
152 unix_dgram_bpf_check_needs_rebuild(psock->sk_proto);
153 WRITE_ONCE(sk->sk_prot, &unix_dgram_bpf_prot);
157 int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
160 sk->sk_write_space = psock->saved_write_space;
161 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
165 unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
166 WRITE_ONCE(sk->sk_prot, &unix_stream_bpf_prot);
170 void __init unix_bpf_build_proto(void)
172 unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, &unix_dgram_proto);
173 unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, &unix_stream_proto);