unix_bpf.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2021 Cong Wang <cong.wang@bytedance.com> */
  3. #include <linux/bpf.h>
  4. #include <linux/skmsg.h>
  5. #include <net/af_unix.h>
  6. #include "af_unix.h"
  7. #define unix_sk_has_data(__sk, __psock) \
  8. ({ !skb_queue_empty(&__sk->sk_receive_queue) || \
  9. !skb_queue_empty(&__psock->ingress_skb) || \
  10. !list_empty(&__psock->ingress_msg); \
  11. })
  12. static int unix_msg_wait_data(struct sock *sk, struct sk_psock *psock,
  13. long timeo)
  14. {
  15. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  16. struct unix_sock *u = unix_sk(sk);
  17. int ret = 0;
  18. if (sk->sk_shutdown & RCV_SHUTDOWN)
  19. return 1;
  20. if (!timeo)
  21. return ret;
  22. add_wait_queue(sk_sleep(sk), &wait);
  23. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  24. if (!unix_sk_has_data(sk, psock)) {
  25. mutex_unlock(&u->iolock);
  26. wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  27. mutex_lock(&u->iolock);
  28. ret = unix_sk_has_data(sk, psock);
  29. }
  30. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  31. remove_wait_queue(sk_sleep(sk), &wait);
  32. return ret;
  33. }
  34. static int __unix_recvmsg(struct sock *sk, struct msghdr *msg,
  35. size_t len, int flags)
  36. {
  37. if (sk->sk_type == SOCK_DGRAM)
  38. return __unix_dgram_recvmsg(sk, msg, len, flags);
  39. else
  40. return __unix_stream_recvmsg(sk, msg, len, flags);
  41. }
  42. static int unix_bpf_recvmsg(struct sock *sk, struct msghdr *msg,
  43. size_t len, int flags, int *addr_len)
  44. {
  45. struct unix_sock *u = unix_sk(sk);
  46. struct sk_psock *psock;
  47. int copied;
  48. if (flags & MSG_OOB)
  49. return -EOPNOTSUPP;
  50. if (!len)
  51. return 0;
  52. psock = sk_psock_get(sk);
  53. if (unlikely(!psock))
  54. return __unix_recvmsg(sk, msg, len, flags);
  55. mutex_lock(&u->iolock);
  56. if (!skb_queue_empty(&sk->sk_receive_queue) &&
  57. sk_psock_queue_empty(psock)) {
  58. mutex_unlock(&u->iolock);
  59. sk_psock_put(sk, psock);
  60. return __unix_recvmsg(sk, msg, len, flags);
  61. }
  62. msg_bytes_ready:
  63. copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
  64. if (!copied) {
  65. long timeo;
  66. int data;
  67. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  68. data = unix_msg_wait_data(sk, psock, timeo);
  69. if (data) {
  70. if (!sk_psock_queue_empty(psock))
  71. goto msg_bytes_ready;
  72. mutex_unlock(&u->iolock);
  73. sk_psock_put(sk, psock);
  74. return __unix_recvmsg(sk, msg, len, flags);
  75. }
  76. copied = -EAGAIN;
  77. }
  78. mutex_unlock(&u->iolock);
  79. sk_psock_put(sk, psock);
  80. return copied;
  81. }
  82. static struct proto *unix_dgram_prot_saved __read_mostly;
  83. static DEFINE_SPINLOCK(unix_dgram_prot_lock);
  84. static struct proto unix_dgram_bpf_prot;
  85. static struct proto *unix_stream_prot_saved __read_mostly;
  86. static DEFINE_SPINLOCK(unix_stream_prot_lock);
  87. static struct proto unix_stream_bpf_prot;
  88. static void unix_dgram_bpf_rebuild_protos(struct proto *prot, const struct proto *base)
  89. {
  90. *prot = *base;
  91. prot->close = sock_map_close;
  92. prot->recvmsg = unix_bpf_recvmsg;
  93. prot->sock_is_readable = sk_msg_is_readable;
  94. }
  95. static void unix_stream_bpf_rebuild_protos(struct proto *prot,
  96. const struct proto *base)
  97. {
  98. *prot = *base;
  99. prot->close = sock_map_close;
  100. prot->recvmsg = unix_bpf_recvmsg;
  101. prot->sock_is_readable = sk_msg_is_readable;
  102. prot->unhash = sock_map_unhash;
  103. }
  104. static void unix_dgram_bpf_check_needs_rebuild(struct proto *ops)
  105. {
  106. if (unlikely(ops != smp_load_acquire(&unix_dgram_prot_saved))) {
  107. spin_lock_bh(&unix_dgram_prot_lock);
  108. if (likely(ops != unix_dgram_prot_saved)) {
  109. unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, ops);
  110. smp_store_release(&unix_dgram_prot_saved, ops);
  111. }
  112. spin_unlock_bh(&unix_dgram_prot_lock);
  113. }
  114. }
  115. static void unix_stream_bpf_check_needs_rebuild(struct proto *ops)
  116. {
  117. if (unlikely(ops != smp_load_acquire(&unix_stream_prot_saved))) {
  118. spin_lock_bh(&unix_stream_prot_lock);
  119. if (likely(ops != unix_stream_prot_saved)) {
  120. unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, ops);
  121. smp_store_release(&unix_stream_prot_saved, ops);
  122. }
  123. spin_unlock_bh(&unix_stream_prot_lock);
  124. }
  125. }
  126. int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
  127. {
  128. if (sk->sk_type != SOCK_DGRAM)
  129. return -EOPNOTSUPP;
  130. if (restore) {
  131. sk->sk_write_space = psock->saved_write_space;
  132. sock_replace_proto(sk, psock->sk_proto);
  133. return 0;
  134. }
  135. unix_dgram_bpf_check_needs_rebuild(psock->sk_proto);
  136. sock_replace_proto(sk, &unix_dgram_bpf_prot);
  137. return 0;
  138. }
  139. int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
  140. {
  141. struct sock *sk_pair;
  142. /* Restore does not decrement the sk_pair reference yet because we must
  143. * keep the a reference to the socket until after an RCU grace period
  144. * and any pending sends have completed.
  145. */
  146. if (restore) {
  147. sk->sk_write_space = psock->saved_write_space;
  148. sock_replace_proto(sk, psock->sk_proto);
  149. return 0;
  150. }
  151. /* psock_update_sk_prot can be called multiple times if psock is
  152. * added to multiple maps and/or slots in the same map. There is
  153. * also an edge case where replacing a psock with itself can trigger
  154. * an extra psock_update_sk_prot during the insert process. So it
  155. * must be safe to do multiple calls. Here we need to ensure we don't
  156. * increment the refcnt through sock_hold many times. There will only
  157. * be a single matching destroy operation.
  158. */
  159. if (!psock->sk_pair) {
  160. sk_pair = unix_peer(sk);
  161. sock_hold(sk_pair);
  162. psock->sk_pair = sk_pair;
  163. }
  164. unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
  165. sock_replace_proto(sk, &unix_stream_bpf_prot);
  166. return 0;
  167. }
  168. void __init unix_bpf_build_proto(void)
  169. {
  170. unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, &unix_dgram_proto);
  171. unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, &unix_stream_proto);
  172. }