PageRenderTime 97ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/net/core/skmsg.c

https://github.com/kvaneesh/linux
C | 1183 lines | 963 code | 158 blank | 62 comment | 160 complexity | 6968c3a5eebece2ee21ee49cf572e6d2 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
  3. #include <linux/skmsg.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/scatterlist.h>
  6. #include <net/sock.h>
  7. #include <net/tcp.h>
  8. #include <net/tls.h>
  9. static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
  10. {
  11. if (msg->sg.end > msg->sg.start &&
  12. elem_first_coalesce < msg->sg.end)
  13. return true;
  14. if (msg->sg.end < msg->sg.start &&
  15. (elem_first_coalesce > msg->sg.start ||
  16. elem_first_coalesce < msg->sg.end))
  17. return true;
  18. return false;
  19. }
  20. int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
  21. int elem_first_coalesce)
  22. {
  23. struct page_frag *pfrag = sk_page_frag(sk);
  24. int ret = 0;
  25. len -= msg->sg.size;
  26. while (len > 0) {
  27. struct scatterlist *sge;
  28. u32 orig_offset;
  29. int use, i;
  30. if (!sk_page_frag_refill(sk, pfrag))
  31. return -ENOMEM;
  32. orig_offset = pfrag->offset;
  33. use = min_t(int, len, pfrag->size - orig_offset);
  34. if (!sk_wmem_schedule(sk, use))
  35. return -ENOMEM;
  36. i = msg->sg.end;
  37. sk_msg_iter_var_prev(i);
  38. sge = &msg->sg.data[i];
  39. if (sk_msg_try_coalesce_ok(msg, elem_first_coalesce) &&
  40. sg_page(sge) == pfrag->page &&
  41. sge->offset + sge->length == orig_offset) {
  42. sge->length += use;
  43. } else {
  44. if (sk_msg_full(msg)) {
  45. ret = -ENOSPC;
  46. break;
  47. }
  48. sge = &msg->sg.data[msg->sg.end];
  49. sg_unmark_end(sge);
  50. sg_set_page(sge, pfrag->page, use, orig_offset);
  51. get_page(pfrag->page);
  52. sk_msg_iter_next(msg, end);
  53. }
  54. sk_mem_charge(sk, use);
  55. msg->sg.size += use;
  56. pfrag->offset += use;
  57. len -= use;
  58. }
  59. return ret;
  60. }
  61. EXPORT_SYMBOL_GPL(sk_msg_alloc);
  62. int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
  63. u32 off, u32 len)
  64. {
  65. int i = src->sg.start;
  66. struct scatterlist *sge = sk_msg_elem(src, i);
  67. struct scatterlist *sgd = NULL;
  68. u32 sge_len, sge_off;
  69. while (off) {
  70. if (sge->length > off)
  71. break;
  72. off -= sge->length;
  73. sk_msg_iter_var_next(i);
  74. if (i == src->sg.end && off)
  75. return -ENOSPC;
  76. sge = sk_msg_elem(src, i);
  77. }
  78. while (len) {
  79. sge_len = sge->length - off;
  80. if (sge_len > len)
  81. sge_len = len;
  82. if (dst->sg.end)
  83. sgd = sk_msg_elem(dst, dst->sg.end - 1);
  84. if (sgd &&
  85. (sg_page(sge) == sg_page(sgd)) &&
  86. (sg_virt(sge) + off == sg_virt(sgd) + sgd->length)) {
  87. sgd->length += sge_len;
  88. dst->sg.size += sge_len;
  89. } else if (!sk_msg_full(dst)) {
  90. sge_off = sge->offset + off;
  91. sk_msg_page_add(dst, sg_page(sge), sge_len, sge_off);
  92. } else {
  93. return -ENOSPC;
  94. }
  95. off = 0;
  96. len -= sge_len;
  97. sk_mem_charge(sk, sge_len);
  98. sk_msg_iter_var_next(i);
  99. if (i == src->sg.end && len)
  100. return -ENOSPC;
  101. sge = sk_msg_elem(src, i);
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(sk_msg_clone);
  106. void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes)
  107. {
  108. int i = msg->sg.start;
  109. do {
  110. struct scatterlist *sge = sk_msg_elem(msg, i);
  111. if (bytes < sge->length) {
  112. sge->length -= bytes;
  113. sge->offset += bytes;
  114. sk_mem_uncharge(sk, bytes);
  115. break;
  116. }
  117. sk_mem_uncharge(sk, sge->length);
  118. bytes -= sge->length;
  119. sge->length = 0;
  120. sge->offset = 0;
  121. sk_msg_iter_var_next(i);
  122. } while (bytes && i != msg->sg.end);
  123. msg->sg.start = i;
  124. }
  125. EXPORT_SYMBOL_GPL(sk_msg_return_zero);
  126. void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes)
  127. {
  128. int i = msg->sg.start;
  129. do {
  130. struct scatterlist *sge = &msg->sg.data[i];
  131. int uncharge = (bytes < sge->length) ? bytes : sge->length;
  132. sk_mem_uncharge(sk, uncharge);
  133. bytes -= uncharge;
  134. sk_msg_iter_var_next(i);
  135. } while (i != msg->sg.end);
  136. }
  137. EXPORT_SYMBOL_GPL(sk_msg_return);
  138. static int sk_msg_free_elem(struct sock *sk, struct sk_msg *msg, u32 i,
  139. bool charge)
  140. {
  141. struct scatterlist *sge = sk_msg_elem(msg, i);
  142. u32 len = sge->length;
  143. /* When the skb owns the memory we free it from consume_skb path. */
  144. if (!msg->skb) {
  145. if (charge)
  146. sk_mem_uncharge(sk, len);
  147. put_page(sg_page(sge));
  148. }
  149. memset(sge, 0, sizeof(*sge));
  150. return len;
  151. }
  152. static int __sk_msg_free(struct sock *sk, struct sk_msg *msg, u32 i,
  153. bool charge)
  154. {
  155. struct scatterlist *sge = sk_msg_elem(msg, i);
  156. int freed = 0;
  157. while (msg->sg.size) {
  158. msg->sg.size -= sge->length;
  159. freed += sk_msg_free_elem(sk, msg, i, charge);
  160. sk_msg_iter_var_next(i);
  161. sk_msg_check_to_free(msg, i, msg->sg.size);
  162. sge = sk_msg_elem(msg, i);
  163. }
  164. consume_skb(msg->skb);
  165. sk_msg_init(msg);
  166. return freed;
  167. }
  168. int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg)
  169. {
  170. return __sk_msg_free(sk, msg, msg->sg.start, false);
  171. }
  172. EXPORT_SYMBOL_GPL(sk_msg_free_nocharge);
  173. int sk_msg_free(struct sock *sk, struct sk_msg *msg)
  174. {
  175. return __sk_msg_free(sk, msg, msg->sg.start, true);
  176. }
  177. EXPORT_SYMBOL_GPL(sk_msg_free);
  178. static void __sk_msg_free_partial(struct sock *sk, struct sk_msg *msg,
  179. u32 bytes, bool charge)
  180. {
  181. struct scatterlist *sge;
  182. u32 i = msg->sg.start;
  183. while (bytes) {
  184. sge = sk_msg_elem(msg, i);
  185. if (!sge->length)
  186. break;
  187. if (bytes < sge->length) {
  188. if (charge)
  189. sk_mem_uncharge(sk, bytes);
  190. sge->length -= bytes;
  191. sge->offset += bytes;
  192. msg->sg.size -= bytes;
  193. break;
  194. }
  195. msg->sg.size -= sge->length;
  196. bytes -= sge->length;
  197. sk_msg_free_elem(sk, msg, i, charge);
  198. sk_msg_iter_var_next(i);
  199. sk_msg_check_to_free(msg, i, bytes);
  200. }
  201. msg->sg.start = i;
  202. }
  203. void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes)
  204. {
  205. __sk_msg_free_partial(sk, msg, bytes, true);
  206. }
  207. EXPORT_SYMBOL_GPL(sk_msg_free_partial);
  208. void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
  209. u32 bytes)
  210. {
  211. __sk_msg_free_partial(sk, msg, bytes, false);
  212. }
  213. void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len)
  214. {
  215. int trim = msg->sg.size - len;
  216. u32 i = msg->sg.end;
  217. if (trim <= 0) {
  218. WARN_ON(trim < 0);
  219. return;
  220. }
  221. sk_msg_iter_var_prev(i);
  222. msg->sg.size = len;
  223. while (msg->sg.data[i].length &&
  224. trim >= msg->sg.data[i].length) {
  225. trim -= msg->sg.data[i].length;
  226. sk_msg_free_elem(sk, msg, i, true);
  227. sk_msg_iter_var_prev(i);
  228. if (!trim)
  229. goto out;
  230. }
  231. msg->sg.data[i].length -= trim;
  232. sk_mem_uncharge(sk, trim);
  233. /* Adjust copybreak if it falls into the trimmed part of last buf */
  234. if (msg->sg.curr == i && msg->sg.copybreak > msg->sg.data[i].length)
  235. msg->sg.copybreak = msg->sg.data[i].length;
  236. out:
  237. sk_msg_iter_var_next(i);
  238. msg->sg.end = i;
  239. /* If we trim data a full sg elem before curr pointer update
  240. * copybreak and current so that any future copy operations
  241. * start at new copy location.
  242. * However trimed data that has not yet been used in a copy op
  243. * does not require an update.
  244. */
  245. if (!msg->sg.size) {
  246. msg->sg.curr = msg->sg.start;
  247. msg->sg.copybreak = 0;
  248. } else if (sk_msg_iter_dist(msg->sg.start, msg->sg.curr) >=
  249. sk_msg_iter_dist(msg->sg.start, msg->sg.end)) {
  250. sk_msg_iter_var_prev(i);
  251. msg->sg.curr = i;
  252. msg->sg.copybreak = msg->sg.data[i].length;
  253. }
  254. }
  255. EXPORT_SYMBOL_GPL(sk_msg_trim);
  256. int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
  257. struct sk_msg *msg, u32 bytes)
  258. {
  259. int i, maxpages, ret = 0, num_elems = sk_msg_elem_used(msg);
  260. const int to_max_pages = MAX_MSG_FRAGS;
  261. struct page *pages[MAX_MSG_FRAGS];
  262. ssize_t orig, copied, use, offset;
  263. orig = msg->sg.size;
  264. while (bytes > 0) {
  265. i = 0;
  266. maxpages = to_max_pages - num_elems;
  267. if (maxpages == 0) {
  268. ret = -EFAULT;
  269. goto out;
  270. }
  271. copied = iov_iter_get_pages(from, pages, bytes, maxpages,
  272. &offset);
  273. if (copied <= 0) {
  274. ret = -EFAULT;
  275. goto out;
  276. }
  277. iov_iter_advance(from, copied);
  278. bytes -= copied;
  279. msg->sg.size += copied;
  280. while (copied) {
  281. use = min_t(int, copied, PAGE_SIZE - offset);
  282. sg_set_page(&msg->sg.data[msg->sg.end],
  283. pages[i], use, offset);
  284. sg_unmark_end(&msg->sg.data[msg->sg.end]);
  285. sk_mem_charge(sk, use);
  286. offset = 0;
  287. copied -= use;
  288. sk_msg_iter_next(msg, end);
  289. num_elems++;
  290. i++;
  291. }
  292. /* When zerocopy is mixed with sk_msg_*copy* operations we
  293. * may have a copybreak set in this case clear and prefer
  294. * zerocopy remainder when possible.
  295. */
  296. msg->sg.copybreak = 0;
  297. msg->sg.curr = msg->sg.end;
  298. }
  299. out:
  300. /* Revert iov_iter updates, msg will need to use 'trim' later if it
  301. * also needs to be cleared.
  302. */
  303. if (ret)
  304. iov_iter_revert(from, msg->sg.size - orig);
  305. return ret;
  306. }
  307. EXPORT_SYMBOL_GPL(sk_msg_zerocopy_from_iter);
  308. int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
  309. struct sk_msg *msg, u32 bytes)
  310. {
  311. int ret = -ENOSPC, i = msg->sg.curr;
  312. struct scatterlist *sge;
  313. u32 copy, buf_size;
  314. void *to;
  315. do {
  316. sge = sk_msg_elem(msg, i);
  317. /* This is possible if a trim operation shrunk the buffer */
  318. if (msg->sg.copybreak >= sge->length) {
  319. msg->sg.copybreak = 0;
  320. sk_msg_iter_var_next(i);
  321. if (i == msg->sg.end)
  322. break;
  323. sge = sk_msg_elem(msg, i);
  324. }
  325. buf_size = sge->length - msg->sg.copybreak;
  326. copy = (buf_size > bytes) ? bytes : buf_size;
  327. to = sg_virt(sge) + msg->sg.copybreak;
  328. msg->sg.copybreak += copy;
  329. if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY)
  330. ret = copy_from_iter_nocache(to, copy, from);
  331. else
  332. ret = copy_from_iter(to, copy, from);
  333. if (ret != copy) {
  334. ret = -EFAULT;
  335. goto out;
  336. }
  337. bytes -= copy;
  338. if (!bytes)
  339. break;
  340. msg->sg.copybreak = 0;
  341. sk_msg_iter_var_next(i);
  342. } while (i != msg->sg.end);
  343. out:
  344. msg->sg.curr = i;
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(sk_msg_memcopy_from_iter);
  348. /* Receive sk_msg from psock->ingress_msg to @msg. */
  349. int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
  350. int len, int flags)
  351. {
  352. struct iov_iter *iter = &msg->msg_iter;
  353. int peek = flags & MSG_PEEK;
  354. struct sk_msg *msg_rx;
  355. int i, copied = 0;
  356. msg_rx = sk_psock_peek_msg(psock);
  357. while (copied != len) {
  358. struct scatterlist *sge;
  359. if (unlikely(!msg_rx))
  360. break;
  361. i = msg_rx->sg.start;
  362. do {
  363. struct page *page;
  364. int copy;
  365. sge = sk_msg_elem(msg_rx, i);
  366. copy = sge->length;
  367. page = sg_page(sge);
  368. if (copied + copy > len)
  369. copy = len - copied;
  370. copy = copy_page_to_iter(page, sge->offset, copy, iter);
  371. if (!copy)
  372. return copied ? copied : -EFAULT;
  373. copied += copy;
  374. if (likely(!peek)) {
  375. sge->offset += copy;
  376. sge->length -= copy;
  377. if (!msg_rx->skb)
  378. sk_mem_uncharge(sk, copy);
  379. msg_rx->sg.size -= copy;
  380. if (!sge->length) {
  381. sk_msg_iter_var_next(i);
  382. if (!msg_rx->skb)
  383. put_page(page);
  384. }
  385. } else {
  386. /* Lets not optimize peek case if copy_page_to_iter
  387. * didn't copy the entire length lets just break.
  388. */
  389. if (copy != sge->length)
  390. return copied;
  391. sk_msg_iter_var_next(i);
  392. }
  393. if (copied == len)
  394. break;
  395. } while (i != msg_rx->sg.end);
  396. if (unlikely(peek)) {
  397. msg_rx = sk_psock_next_msg(psock, msg_rx);
  398. if (!msg_rx)
  399. break;
  400. continue;
  401. }
  402. msg_rx->sg.start = i;
  403. if (!sge->length && msg_rx->sg.start == msg_rx->sg.end) {
  404. msg_rx = sk_psock_dequeue_msg(psock);
  405. kfree_sk_msg(msg_rx);
  406. }
  407. msg_rx = sk_psock_peek_msg(psock);
  408. }
  409. return copied;
  410. }
  411. EXPORT_SYMBOL_GPL(sk_msg_recvmsg);
  412. static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
  413. struct sk_buff *skb)
  414. {
  415. struct sk_msg *msg;
  416. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
  417. return NULL;
  418. if (!sk_rmem_schedule(sk, skb, skb->truesize))
  419. return NULL;
  420. msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_KERNEL);
  421. if (unlikely(!msg))
  422. return NULL;
  423. sk_msg_init(msg);
  424. return msg;
  425. }
  426. static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
  427. struct sk_psock *psock,
  428. struct sock *sk,
  429. struct sk_msg *msg)
  430. {
  431. int num_sge, copied;
  432. /* skb linearize may fail with ENOMEM, but lets simply try again
  433. * later if this happens. Under memory pressure we don't want to
  434. * drop the skb. We need to linearize the skb so that the mapping
  435. * in skb_to_sgvec can not error.
  436. */
  437. if (skb_linearize(skb))
  438. return -EAGAIN;
  439. num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len);
  440. if (unlikely(num_sge < 0))
  441. return num_sge;
  442. copied = skb->len;
  443. msg->sg.start = 0;
  444. msg->sg.size = copied;
  445. msg->sg.end = num_sge;
  446. msg->skb = skb;
  447. sk_psock_queue_msg(psock, msg);
  448. sk_psock_data_ready(sk, psock);
  449. return copied;
  450. }
  451. static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb);
  452. static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
  453. {
  454. struct sock *sk = psock->sk;
  455. struct sk_msg *msg;
  456. int err;
  457. /* If we are receiving on the same sock skb->sk is already assigned,
  458. * skip memory accounting and owner transition seeing it already set
  459. * correctly.
  460. */
  461. if (unlikely(skb->sk == sk))
  462. return sk_psock_skb_ingress_self(psock, skb);
  463. msg = sk_psock_create_ingress_msg(sk, skb);
  464. if (!msg)
  465. return -EAGAIN;
  466. /* This will transition ownership of the data from the socket where
  467. * the BPF program was run initiating the redirect to the socket
  468. * we will eventually receive this data on. The data will be released
  469. * from skb_consume found in __tcp_bpf_recvmsg() after its been copied
  470. * into user buffers.
  471. */
  472. skb_set_owner_r(skb, sk);
  473. err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
  474. if (err < 0)
  475. kfree(msg);
  476. return err;
  477. }
  478. /* Puts an skb on the ingress queue of the socket already assigned to the
  479. * skb. In this case we do not need to check memory limits or skb_set_owner_r
  480. * because the skb is already accounted for here.
  481. */
  482. static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb)
  483. {
  484. struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
  485. struct sock *sk = psock->sk;
  486. int err;
  487. if (unlikely(!msg))
  488. return -EAGAIN;
  489. sk_msg_init(msg);
  490. skb_set_owner_r(skb, sk);
  491. err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
  492. if (err < 0)
  493. kfree(msg);
  494. return err;
  495. }
  496. static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb,
  497. u32 off, u32 len, bool ingress)
  498. {
  499. if (!ingress) {
  500. if (!sock_writeable(psock->sk))
  501. return -EAGAIN;
  502. return skb_send_sock(psock->sk, skb, off, len);
  503. }
  504. return sk_psock_skb_ingress(psock, skb);
  505. }
  506. static void sk_psock_skb_state(struct sk_psock *psock,
  507. struct sk_psock_work_state *state,
  508. struct sk_buff *skb,
  509. int len, int off)
  510. {
  511. spin_lock_bh(&psock->ingress_lock);
  512. if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
  513. state->skb = skb;
  514. state->len = len;
  515. state->off = off;
  516. } else {
  517. sock_drop(psock->sk, skb);
  518. }
  519. spin_unlock_bh(&psock->ingress_lock);
  520. }
  521. static void sk_psock_backlog(struct work_struct *work)
  522. {
  523. struct sk_psock *psock = container_of(work, struct sk_psock, work);
  524. struct sk_psock_work_state *state = &psock->work_state;
  525. struct sk_buff *skb = NULL;
  526. bool ingress;
  527. u32 len, off;
  528. int ret;
  529. mutex_lock(&psock->work_mutex);
  530. if (unlikely(state->skb)) {
  531. spin_lock_bh(&psock->ingress_lock);
  532. skb = state->skb;
  533. len = state->len;
  534. off = state->off;
  535. state->skb = NULL;
  536. spin_unlock_bh(&psock->ingress_lock);
  537. }
  538. if (skb)
  539. goto start;
  540. while ((skb = skb_dequeue(&psock->ingress_skb))) {
  541. len = skb->len;
  542. off = 0;
  543. start:
  544. ingress = skb_bpf_ingress(skb);
  545. skb_bpf_redirect_clear(skb);
  546. do {
  547. ret = -EIO;
  548. if (!sock_flag(psock->sk, SOCK_DEAD))
  549. ret = sk_psock_handle_skb(psock, skb, off,
  550. len, ingress);
  551. if (ret <= 0) {
  552. if (ret == -EAGAIN) {
  553. sk_psock_skb_state(psock, state, skb,
  554. len, off);
  555. goto end;
  556. }
  557. /* Hard errors break pipe and stop xmit. */
  558. sk_psock_report_error(psock, ret ? -ret : EPIPE);
  559. sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
  560. sock_drop(psock->sk, skb);
  561. goto end;
  562. }
  563. off += ret;
  564. len -= ret;
  565. } while (len);
  566. if (!ingress)
  567. kfree_skb(skb);
  568. }
  569. end:
  570. mutex_unlock(&psock->work_mutex);
  571. }
  572. struct sk_psock *sk_psock_init(struct sock *sk, int node)
  573. {
  574. struct sk_psock *psock;
  575. struct proto *prot;
  576. write_lock_bh(&sk->sk_callback_lock);
  577. if (sk->sk_user_data) {
  578. psock = ERR_PTR(-EBUSY);
  579. goto out;
  580. }
  581. psock = kzalloc_node(sizeof(*psock), GFP_ATOMIC | __GFP_NOWARN, node);
  582. if (!psock) {
  583. psock = ERR_PTR(-ENOMEM);
  584. goto out;
  585. }
  586. prot = READ_ONCE(sk->sk_prot);
  587. psock->sk = sk;
  588. psock->eval = __SK_NONE;
  589. psock->sk_proto = prot;
  590. psock->saved_unhash = prot->unhash;
  591. psock->saved_close = prot->close;
  592. psock->saved_write_space = sk->sk_write_space;
  593. INIT_LIST_HEAD(&psock->link);
  594. spin_lock_init(&psock->link_lock);
  595. INIT_WORK(&psock->work, sk_psock_backlog);
  596. mutex_init(&psock->work_mutex);
  597. INIT_LIST_HEAD(&psock->ingress_msg);
  598. spin_lock_init(&psock->ingress_lock);
  599. skb_queue_head_init(&psock->ingress_skb);
  600. sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED);
  601. refcount_set(&psock->refcnt, 1);
  602. rcu_assign_sk_user_data_nocopy(sk, psock);
  603. sock_hold(sk);
  604. out:
  605. write_unlock_bh(&sk->sk_callback_lock);
  606. return psock;
  607. }
  608. EXPORT_SYMBOL_GPL(sk_psock_init);
  609. struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock)
  610. {
  611. struct sk_psock_link *link;
  612. spin_lock_bh(&psock->link_lock);
  613. link = list_first_entry_or_null(&psock->link, struct sk_psock_link,
  614. list);
  615. if (link)
  616. list_del(&link->list);
  617. spin_unlock_bh(&psock->link_lock);
  618. return link;
  619. }
  620. static void __sk_psock_purge_ingress_msg(struct sk_psock *psock)
  621. {
  622. struct sk_msg *msg, *tmp;
  623. list_for_each_entry_safe(msg, tmp, &psock->ingress_msg, list) {
  624. list_del(&msg->list);
  625. sk_msg_free(psock->sk, msg);
  626. kfree(msg);
  627. }
  628. }
  629. static void __sk_psock_zap_ingress(struct sk_psock *psock)
  630. {
  631. struct sk_buff *skb;
  632. while ((skb = skb_dequeue(&psock->ingress_skb)) != NULL) {
  633. skb_bpf_redirect_clear(skb);
  634. sock_drop(psock->sk, skb);
  635. }
  636. kfree_skb(psock->work_state.skb);
  637. /* We null the skb here to ensure that calls to sk_psock_backlog
  638. * do not pick up the free'd skb.
  639. */
  640. psock->work_state.skb = NULL;
  641. __sk_psock_purge_ingress_msg(psock);
  642. }
  643. static void sk_psock_link_destroy(struct sk_psock *psock)
  644. {
  645. struct sk_psock_link *link, *tmp;
  646. list_for_each_entry_safe(link, tmp, &psock->link, list) {
  647. list_del(&link->list);
  648. sk_psock_free_link(link);
  649. }
  650. }
  651. void sk_psock_stop(struct sk_psock *psock, bool wait)
  652. {
  653. spin_lock_bh(&psock->ingress_lock);
  654. sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
  655. sk_psock_cork_free(psock);
  656. __sk_psock_zap_ingress(psock);
  657. spin_unlock_bh(&psock->ingress_lock);
  658. if (wait)
  659. cancel_work_sync(&psock->work);
  660. }
  661. static void sk_psock_done_strp(struct sk_psock *psock);
  662. static void sk_psock_destroy(struct work_struct *work)
  663. {
  664. struct sk_psock *psock = container_of(to_rcu_work(work),
  665. struct sk_psock, rwork);
  666. /* No sk_callback_lock since already detached. */
  667. sk_psock_done_strp(psock);
  668. cancel_work_sync(&psock->work);
  669. mutex_destroy(&psock->work_mutex);
  670. psock_progs_drop(&psock->progs);
  671. sk_psock_link_destroy(psock);
  672. sk_psock_cork_free(psock);
  673. if (psock->sk_redir)
  674. sock_put(psock->sk_redir);
  675. sock_put(psock->sk);
  676. kfree(psock);
  677. }
  678. void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
  679. {
  680. write_lock_bh(&sk->sk_callback_lock);
  681. sk_psock_restore_proto(sk, psock);
  682. rcu_assign_sk_user_data(sk, NULL);
  683. if (psock->progs.stream_parser)
  684. sk_psock_stop_strp(sk, psock);
  685. else if (psock->progs.stream_verdict || psock->progs.skb_verdict)
  686. sk_psock_stop_verdict(sk, psock);
  687. write_unlock_bh(&sk->sk_callback_lock);
  688. sk_psock_stop(psock, false);
  689. INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);
  690. queue_rcu_work(system_wq, &psock->rwork);
  691. }
  692. EXPORT_SYMBOL_GPL(sk_psock_drop);
  693. static int sk_psock_map_verd(int verdict, bool redir)
  694. {
  695. switch (verdict) {
  696. case SK_PASS:
  697. return redir ? __SK_REDIRECT : __SK_PASS;
  698. case SK_DROP:
  699. default:
  700. break;
  701. }
  702. return __SK_DROP;
  703. }
  704. int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
  705. struct sk_msg *msg)
  706. {
  707. struct bpf_prog *prog;
  708. int ret;
  709. rcu_read_lock();
  710. prog = READ_ONCE(psock->progs.msg_parser);
  711. if (unlikely(!prog)) {
  712. ret = __SK_PASS;
  713. goto out;
  714. }
  715. sk_msg_compute_data_pointers(msg);
  716. msg->sk = sk;
  717. ret = bpf_prog_run_pin_on_cpu(prog, msg);
  718. ret = sk_psock_map_verd(ret, msg->sk_redir);
  719. psock->apply_bytes = msg->apply_bytes;
  720. if (ret == __SK_REDIRECT) {
  721. if (psock->sk_redir)
  722. sock_put(psock->sk_redir);
  723. psock->sk_redir = msg->sk_redir;
  724. if (!psock->sk_redir) {
  725. ret = __SK_DROP;
  726. goto out;
  727. }
  728. sock_hold(psock->sk_redir);
  729. }
  730. out:
  731. rcu_read_unlock();
  732. return ret;
  733. }
  734. EXPORT_SYMBOL_GPL(sk_psock_msg_verdict);
  735. static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb)
  736. {
  737. struct sk_psock *psock_other;
  738. struct sock *sk_other;
  739. sk_other = skb_bpf_redirect_fetch(skb);
  740. /* This error is a buggy BPF program, it returned a redirect
  741. * return code, but then didn't set a redirect interface.
  742. */
  743. if (unlikely(!sk_other)) {
  744. sock_drop(from->sk, skb);
  745. return -EIO;
  746. }
  747. psock_other = sk_psock(sk_other);
  748. /* This error indicates the socket is being torn down or had another
  749. * error that caused the pipe to break. We can't send a packet on
  750. * a socket that is in this state so we drop the skb.
  751. */
  752. if (!psock_other || sock_flag(sk_other, SOCK_DEAD)) {
  753. skb_bpf_redirect_clear(skb);
  754. sock_drop(from->sk, skb);
  755. return -EIO;
  756. }
  757. spin_lock_bh(&psock_other->ingress_lock);
  758. if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
  759. spin_unlock_bh(&psock_other->ingress_lock);
  760. skb_bpf_redirect_clear(skb);
  761. sock_drop(from->sk, skb);
  762. return -EIO;
  763. }
  764. skb_queue_tail(&psock_other->ingress_skb, skb);
  765. schedule_work(&psock_other->work);
  766. spin_unlock_bh(&psock_other->ingress_lock);
  767. return 0;
  768. }
  769. static void sk_psock_tls_verdict_apply(struct sk_buff *skb,
  770. struct sk_psock *from, int verdict)
  771. {
  772. switch (verdict) {
  773. case __SK_REDIRECT:
  774. sk_psock_skb_redirect(from, skb);
  775. break;
  776. case __SK_PASS:
  777. case __SK_DROP:
  778. default:
  779. break;
  780. }
  781. }
  782. int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
  783. {
  784. struct bpf_prog *prog;
  785. int ret = __SK_PASS;
  786. rcu_read_lock();
  787. prog = READ_ONCE(psock->progs.stream_verdict);
  788. if (likely(prog)) {
  789. skb->sk = psock->sk;
  790. skb_dst_drop(skb);
  791. skb_bpf_redirect_clear(skb);
  792. ret = bpf_prog_run_pin_on_cpu(prog, skb);
  793. ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
  794. skb->sk = NULL;
  795. }
  796. sk_psock_tls_verdict_apply(skb, psock, ret);
  797. rcu_read_unlock();
  798. return ret;
  799. }
  800. EXPORT_SYMBOL_GPL(sk_psock_tls_strp_read);
  801. static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
  802. int verdict)
  803. {
  804. struct sock *sk_other;
  805. int err = 0;
  806. switch (verdict) {
  807. case __SK_PASS:
  808. err = -EIO;
  809. sk_other = psock->sk;
  810. if (sock_flag(sk_other, SOCK_DEAD) ||
  811. !sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
  812. goto out_free;
  813. }
  814. skb_bpf_set_ingress(skb);
  815. /* If the queue is empty then we can submit directly
  816. * into the msg queue. If its not empty we have to
  817. * queue work otherwise we may get OOO data. Otherwise,
  818. * if sk_psock_skb_ingress errors will be handled by
  819. * retrying later from workqueue.
  820. */
  821. if (skb_queue_empty(&psock->ingress_skb)) {
  822. err = sk_psock_skb_ingress_self(psock, skb);
  823. }
  824. if (err < 0) {
  825. spin_lock_bh(&psock->ingress_lock);
  826. if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
  827. skb_queue_tail(&psock->ingress_skb, skb);
  828. schedule_work(&psock->work);
  829. err = 0;
  830. }
  831. spin_unlock_bh(&psock->ingress_lock);
  832. if (err < 0) {
  833. skb_bpf_redirect_clear(skb);
  834. goto out_free;
  835. }
  836. }
  837. break;
  838. case __SK_REDIRECT:
  839. err = sk_psock_skb_redirect(psock, skb);
  840. break;
  841. case __SK_DROP:
  842. default:
  843. out_free:
  844. sock_drop(psock->sk, skb);
  845. }
  846. return err;
  847. }
  848. static void sk_psock_write_space(struct sock *sk)
  849. {
  850. struct sk_psock *psock;
  851. void (*write_space)(struct sock *sk) = NULL;
  852. rcu_read_lock();
  853. psock = sk_psock(sk);
  854. if (likely(psock)) {
  855. if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
  856. schedule_work(&psock->work);
  857. write_space = psock->saved_write_space;
  858. }
  859. rcu_read_unlock();
  860. if (write_space)
  861. write_space(sk);
  862. }
  863. #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
  864. static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
  865. {
  866. struct sk_psock *psock;
  867. struct bpf_prog *prog;
  868. int ret = __SK_DROP;
  869. struct sock *sk;
  870. rcu_read_lock();
  871. sk = strp->sk;
  872. psock = sk_psock(sk);
  873. if (unlikely(!psock)) {
  874. sock_drop(sk, skb);
  875. goto out;
  876. }
  877. prog = READ_ONCE(psock->progs.stream_verdict);
  878. if (likely(prog)) {
  879. skb->sk = sk;
  880. skb_dst_drop(skb);
  881. skb_bpf_redirect_clear(skb);
  882. ret = bpf_prog_run_pin_on_cpu(prog, skb);
  883. ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
  884. skb->sk = NULL;
  885. }
  886. sk_psock_verdict_apply(psock, skb, ret);
  887. out:
  888. rcu_read_unlock();
  889. }
  890. static int sk_psock_strp_read_done(struct strparser *strp, int err)
  891. {
  892. return err;
  893. }
  894. static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
  895. {
  896. struct sk_psock *psock = container_of(strp, struct sk_psock, strp);
  897. struct bpf_prog *prog;
  898. int ret = skb->len;
  899. rcu_read_lock();
  900. prog = READ_ONCE(psock->progs.stream_parser);
  901. if (likely(prog)) {
  902. skb->sk = psock->sk;
  903. ret = bpf_prog_run_pin_on_cpu(prog, skb);
  904. skb->sk = NULL;
  905. }
  906. rcu_read_unlock();
  907. return ret;
  908. }
  909. /* Called with socket lock held. */
  910. static void sk_psock_strp_data_ready(struct sock *sk)
  911. {
  912. struct sk_psock *psock;
  913. rcu_read_lock();
  914. psock = sk_psock(sk);
  915. if (likely(psock)) {
  916. if (tls_sw_has_ctx_rx(sk)) {
  917. psock->saved_data_ready(sk);
  918. } else {
  919. write_lock_bh(&sk->sk_callback_lock);
  920. strp_data_ready(&psock->strp);
  921. write_unlock_bh(&sk->sk_callback_lock);
  922. }
  923. }
  924. rcu_read_unlock();
  925. }
  926. int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
  927. {
  928. static const struct strp_callbacks cb = {
  929. .rcv_msg = sk_psock_strp_read,
  930. .read_sock_done = sk_psock_strp_read_done,
  931. .parse_msg = sk_psock_strp_parse,
  932. };
  933. return strp_init(&psock->strp, sk, &cb);
  934. }
  935. void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
  936. {
  937. if (psock->saved_data_ready)
  938. return;
  939. psock->saved_data_ready = sk->sk_data_ready;
  940. sk->sk_data_ready = sk_psock_strp_data_ready;
  941. sk->sk_write_space = sk_psock_write_space;
  942. }
  943. void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
  944. {
  945. if (!psock->saved_data_ready)
  946. return;
  947. sk->sk_data_ready = psock->saved_data_ready;
  948. psock->saved_data_ready = NULL;
  949. strp_stop(&psock->strp);
  950. }
  951. static void sk_psock_done_strp(struct sk_psock *psock)
  952. {
  953. /* Parser has been stopped */
  954. if (psock->progs.stream_parser)
  955. strp_done(&psock->strp);
  956. }
  957. #else
  958. static void sk_psock_done_strp(struct sk_psock *psock)
  959. {
  960. }
  961. #endif /* CONFIG_BPF_STREAM_PARSER */
  962. static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb,
  963. unsigned int offset, size_t orig_len)
  964. {
  965. struct sock *sk = (struct sock *)desc->arg.data;
  966. struct sk_psock *psock;
  967. struct bpf_prog *prog;
  968. int ret = __SK_DROP;
  969. int len = skb->len;
  970. /* clone here so sk_eat_skb() in tcp_read_sock does not drop our data */
  971. skb = skb_clone(skb, GFP_ATOMIC);
  972. if (!skb) {
  973. desc->error = -ENOMEM;
  974. return 0;
  975. }
  976. rcu_read_lock();
  977. psock = sk_psock(sk);
  978. if (unlikely(!psock)) {
  979. len = 0;
  980. sock_drop(sk, skb);
  981. goto out;
  982. }
  983. prog = READ_ONCE(psock->progs.stream_verdict);
  984. if (!prog)
  985. prog = READ_ONCE(psock->progs.skb_verdict);
  986. if (likely(prog)) {
  987. skb->sk = sk;
  988. skb_dst_drop(skb);
  989. skb_bpf_redirect_clear(skb);
  990. ret = bpf_prog_run_pin_on_cpu(prog, skb);
  991. ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
  992. skb->sk = NULL;
  993. }
  994. if (sk_psock_verdict_apply(psock, skb, ret) < 0)
  995. len = 0;
  996. out:
  997. rcu_read_unlock();
  998. return len;
  999. }
  1000. static void sk_psock_verdict_data_ready(struct sock *sk)
  1001. {
  1002. struct socket *sock = sk->sk_socket;
  1003. read_descriptor_t desc;
  1004. if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
  1005. return;
  1006. desc.arg.data = sk;
  1007. desc.error = 0;
  1008. desc.count = 1;
  1009. sock->ops->read_sock(sk, &desc, sk_psock_verdict_recv);
  1010. }
  1011. void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
  1012. {
  1013. if (psock->saved_data_ready)
  1014. return;
  1015. psock->saved_data_ready = sk->sk_data_ready;
  1016. sk->sk_data_ready = sk_psock_verdict_data_ready;
  1017. sk->sk_write_space = sk_psock_write_space;
  1018. }
  1019. void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)
  1020. {
  1021. if (!psock->saved_data_ready)
  1022. return;
  1023. sk->sk_data_ready = psock->saved_data_ready;
  1024. psock->saved_data_ready = NULL;
  1025. }