Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Cong Wang <cong.wang@bytedance.com> */
0003 
0004 #include <linux/skmsg.h>
0005 #include <linux/bpf.h>
0006 #include <net/sock.h>
0007 #include <net/af_unix.h>
0008 
0009 #define unix_sk_has_data(__sk, __psock)                 \
0010         ({  !skb_queue_empty(&__sk->sk_receive_queue) ||    \
0011             !skb_queue_empty(&__psock->ingress_skb) ||  \
0012             !list_empty(&__psock->ingress_msg);     \
0013         })
0014 
0015 static int unix_msg_wait_data(struct sock *sk, struct sk_psock *psock,
0016                   long timeo)
0017 {
0018     DEFINE_WAIT_FUNC(wait, woken_wake_function);
0019     struct unix_sock *u = unix_sk(sk);
0020     int ret = 0;
0021 
0022     if (sk->sk_shutdown & RCV_SHUTDOWN)
0023         return 1;
0024 
0025     if (!timeo)
0026         return ret;
0027 
0028     add_wait_queue(sk_sleep(sk), &wait);
0029     sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
0030     if (!unix_sk_has_data(sk, psock)) {
0031         mutex_unlock(&u->iolock);
0032         wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
0033         mutex_lock(&u->iolock);
0034         ret = unix_sk_has_data(sk, psock);
0035     }
0036     sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
0037     remove_wait_queue(sk_sleep(sk), &wait);
0038     return ret;
0039 }
0040 
0041 static int __unix_recvmsg(struct sock *sk, struct msghdr *msg,
0042               size_t len, int flags)
0043 {
0044     if (sk->sk_type == SOCK_DGRAM)
0045         return __unix_dgram_recvmsg(sk, msg, len, flags);
0046     else
0047         return __unix_stream_recvmsg(sk, msg, len, flags);
0048 }
0049 
0050 static int unix_bpf_recvmsg(struct sock *sk, struct msghdr *msg,
0051                 size_t len, int flags, int *addr_len)
0052 {
0053     struct unix_sock *u = unix_sk(sk);
0054     struct sk_psock *psock;
0055     int copied;
0056 
0057     psock = sk_psock_get(sk);
0058     if (unlikely(!psock))
0059         return __unix_recvmsg(sk, msg, len, flags);
0060 
0061     mutex_lock(&u->iolock);
0062     if (!skb_queue_empty(&sk->sk_receive_queue) &&
0063         sk_psock_queue_empty(psock)) {
0064         mutex_unlock(&u->iolock);
0065         sk_psock_put(sk, psock);
0066         return __unix_recvmsg(sk, msg, len, flags);
0067     }
0068 
0069 msg_bytes_ready:
0070     copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
0071     if (!copied) {
0072         long timeo;
0073         int data;
0074 
0075         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0076         data = unix_msg_wait_data(sk, psock, timeo);
0077         if (data) {
0078             if (!sk_psock_queue_empty(psock))
0079                 goto msg_bytes_ready;
0080             mutex_unlock(&u->iolock);
0081             sk_psock_put(sk, psock);
0082             return __unix_recvmsg(sk, msg, len, flags);
0083         }
0084         copied = -EAGAIN;
0085     }
0086     mutex_unlock(&u->iolock);
0087     sk_psock_put(sk, psock);
0088     return copied;
0089 }
0090 
0091 static struct proto *unix_dgram_prot_saved __read_mostly;
0092 static DEFINE_SPINLOCK(unix_dgram_prot_lock);
0093 static struct proto unix_dgram_bpf_prot;
0094 
0095 static struct proto *unix_stream_prot_saved __read_mostly;
0096 static DEFINE_SPINLOCK(unix_stream_prot_lock);
0097 static struct proto unix_stream_bpf_prot;
0098 
0099 static void unix_dgram_bpf_rebuild_protos(struct proto *prot, const struct proto *base)
0100 {
0101     *prot        = *base;
0102     prot->close  = sock_map_close;
0103     prot->recvmsg = unix_bpf_recvmsg;
0104     prot->sock_is_readable = sk_msg_is_readable;
0105 }
0106 
0107 static void unix_stream_bpf_rebuild_protos(struct proto *prot,
0108                        const struct proto *base)
0109 {
0110     *prot        = *base;
0111     prot->close  = sock_map_close;
0112     prot->recvmsg = unix_bpf_recvmsg;
0113     prot->sock_is_readable = sk_msg_is_readable;
0114     prot->unhash  = sock_map_unhash;
0115 }
0116 
0117 static void unix_dgram_bpf_check_needs_rebuild(struct proto *ops)
0118 {
0119     if (unlikely(ops != smp_load_acquire(&unix_dgram_prot_saved))) {
0120         spin_lock_bh(&unix_dgram_prot_lock);
0121         if (likely(ops != unix_dgram_prot_saved)) {
0122             unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, ops);
0123             smp_store_release(&unix_dgram_prot_saved, ops);
0124         }
0125         spin_unlock_bh(&unix_dgram_prot_lock);
0126     }
0127 }
0128 
0129 static void unix_stream_bpf_check_needs_rebuild(struct proto *ops)
0130 {
0131     if (unlikely(ops != smp_load_acquire(&unix_stream_prot_saved))) {
0132         spin_lock_bh(&unix_stream_prot_lock);
0133         if (likely(ops != unix_stream_prot_saved)) {
0134             unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, ops);
0135             smp_store_release(&unix_stream_prot_saved, ops);
0136         }
0137         spin_unlock_bh(&unix_stream_prot_lock);
0138     }
0139 }
0140 
0141 int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
0142 {
0143     if (sk->sk_type != SOCK_DGRAM)
0144         return -EOPNOTSUPP;
0145 
0146     if (restore) {
0147         sk->sk_write_space = psock->saved_write_space;
0148         WRITE_ONCE(sk->sk_prot, psock->sk_proto);
0149         return 0;
0150     }
0151 
0152     unix_dgram_bpf_check_needs_rebuild(psock->sk_proto);
0153     WRITE_ONCE(sk->sk_prot, &unix_dgram_bpf_prot);
0154     return 0;
0155 }
0156 
0157 int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
0158 {
0159     if (restore) {
0160         sk->sk_write_space = psock->saved_write_space;
0161         WRITE_ONCE(sk->sk_prot, psock->sk_proto);
0162         return 0;
0163     }
0164 
0165     unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
0166     WRITE_ONCE(sk->sk_prot, &unix_stream_bpf_prot);
0167     return 0;
0168 }
0169 
0170 void __init unix_bpf_build_proto(void)
0171 {
0172     unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, &unix_dgram_proto);
0173     unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, &unix_stream_proto);
0174 
0175 }