smc_rx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Manage RMBE
  6. * copy new RMBE data into user space
  7. *
  8. * Copyright IBM Corp. 2016
  9. *
  10. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  11. */
  12. #include <linux/net.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/sched/signal.h>
  15. #include <net/sock.h>
  16. #include "smc.h"
  17. #include "smc_core.h"
  18. #include "smc_cdc.h"
  19. #include "smc_tx.h" /* smc_tx_consumer_update() */
  20. #include "smc_rx.h"
  21. /* callback implementation to wakeup consumers blocked with smc_rx_wait().
  22. * indirectly called by smc_cdc_msg_recv_action().
  23. */
  24. static void smc_rx_wake_up(struct sock *sk)
  25. {
  26. struct socket_wq *wq;
  27. /* derived from sock_def_readable() */
  28. /* called already in smc_listen_work() */
  29. rcu_read_lock();
  30. wq = rcu_dereference(sk->sk_wq);
  31. if (skwq_has_sleeper(wq))
  32. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
  33. EPOLLRDNORM | EPOLLRDBAND);
  34. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  35. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  36. (sk->sk_state == SMC_CLOSED))
  37. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
  38. rcu_read_unlock();
  39. }
  40. /* Update consumer cursor
  41. * @conn connection to update
  42. * @cons consumer cursor
  43. * @len number of Bytes consumed
  44. * Returns:
  45. * 1 if we should end our receive, 0 otherwise
  46. */
  47. static int smc_rx_update_consumer(struct smc_sock *smc,
  48. union smc_host_cursor cons, size_t len)
  49. {
  50. struct smc_connection *conn = &smc->conn;
  51. struct sock *sk = &smc->sk;
  52. bool force = false;
  53. int diff, rc = 0;
  54. smc_curs_add(conn->rmb_desc->len, &cons, len);
  55. /* did we process urgent data? */
  56. if (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) {
  57. diff = smc_curs_comp(conn->rmb_desc->len, &cons,
  58. &conn->urg_curs);
  59. if (sock_flag(sk, SOCK_URGINLINE)) {
  60. if (diff == 0) {
  61. force = true;
  62. rc = 1;
  63. conn->urg_state = SMC_URG_READ;
  64. }
  65. } else {
  66. if (diff == 1) {
  67. /* skip urgent byte */
  68. force = true;
  69. smc_curs_add(conn->rmb_desc->len, &cons, 1);
  70. conn->urg_rx_skip_pend = false;
  71. } else if (diff < -1)
  72. /* we read past urgent byte */
  73. conn->urg_state = SMC_URG_READ;
  74. }
  75. }
  76. smc_curs_write(&conn->local_tx_ctrl.cons, smc_curs_read(&cons, conn),
  77. conn);
  78. /* send consumer cursor update if required */
  79. /* similar to advertising new TCP rcv_wnd if required */
  80. smc_tx_consumer_update(conn, force);
  81. return rc;
  82. }
  83. static void smc_rx_update_cons(struct smc_sock *smc, size_t len)
  84. {
  85. struct smc_connection *conn = &smc->conn;
  86. union smc_host_cursor cons;
  87. smc_curs_write(&cons, smc_curs_read(&conn->local_tx_ctrl.cons, conn),
  88. conn);
  89. smc_rx_update_consumer(smc, cons, len);
  90. }
  91. struct smc_spd_priv {
  92. struct smc_sock *smc;
  93. size_t len;
  94. };
  95. static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe,
  96. struct pipe_buffer *buf)
  97. {
  98. struct smc_spd_priv *priv = (struct smc_spd_priv *)buf->private;
  99. struct smc_sock *smc = priv->smc;
  100. struct smc_connection *conn;
  101. struct sock *sk = &smc->sk;
  102. if (sk->sk_state == SMC_CLOSED ||
  103. sk->sk_state == SMC_PEERFINCLOSEWAIT ||
  104. sk->sk_state == SMC_APPFINCLOSEWAIT)
  105. goto out;
  106. conn = &smc->conn;
  107. lock_sock(sk);
  108. smc_rx_update_cons(smc, priv->len);
  109. release_sock(sk);
  110. if (atomic_sub_and_test(priv->len, &conn->splice_pending))
  111. smc_rx_wake_up(sk);
  112. out:
  113. kfree(priv);
  114. put_page(buf->page);
  115. sock_put(sk);
  116. }
  117. static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info *pipe,
  118. struct pipe_buffer *buf)
  119. {
  120. return 1;
  121. }
  122. static const struct pipe_buf_operations smc_pipe_ops = {
  123. .can_merge = 0,
  124. .confirm = generic_pipe_buf_confirm,
  125. .release = smc_rx_pipe_buf_release,
  126. .steal = smc_rx_pipe_buf_nosteal,
  127. .get = generic_pipe_buf_get
  128. };
  129. static void smc_rx_spd_release(struct splice_pipe_desc *spd,
  130. unsigned int i)
  131. {
  132. put_page(spd->pages[i]);
  133. }
  134. static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
  135. struct smc_sock *smc)
  136. {
  137. struct splice_pipe_desc spd;
  138. struct partial_page partial;
  139. struct smc_spd_priv *priv;
  140. struct page *page;
  141. int bytes;
  142. page = virt_to_page(smc->conn.rmb_desc->cpu_addr);
  143. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. priv->len = len;
  147. priv->smc = smc;
  148. partial.offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
  149. partial.len = len;
  150. partial.private = (unsigned long)priv;
  151. spd.nr_pages_max = 1;
  152. spd.nr_pages = 1;
  153. spd.pages = &page;
  154. spd.partial = &partial;
  155. spd.ops = &smc_pipe_ops;
  156. spd.spd_release = smc_rx_spd_release;
  157. bytes = splice_to_pipe(pipe, &spd);
  158. if (bytes > 0) {
  159. sock_hold(&smc->sk);
  160. get_page(smc->conn.rmb_desc->pages);
  161. atomic_add(bytes, &smc->conn.splice_pending);
  162. }
  163. return bytes;
  164. }
  165. static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn)
  166. {
  167. return atomic_read(&conn->bytes_to_rcv) &&
  168. !atomic_read(&conn->splice_pending);
  169. }
  170. /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
  171. * @smc smc socket
  172. * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
  173. * @fcrit add'l criterion to evaluate as function pointer
  174. * Returns:
  175. * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
  176. * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
  177. */
  178. int smc_rx_wait(struct smc_sock *smc, long *timeo,
  179. int (*fcrit)(struct smc_connection *conn))
  180. {
  181. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  182. struct smc_connection *conn = &smc->conn;
  183. struct sock *sk = &smc->sk;
  184. int rc;
  185. if (fcrit(conn))
  186. return 1;
  187. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  188. add_wait_queue(sk_sleep(sk), &wait);
  189. rc = sk_wait_event(sk, timeo,
  190. sk->sk_err ||
  191. sk->sk_shutdown & RCV_SHUTDOWN ||
  192. fcrit(conn) ||
  193. smc_cdc_rxed_any_close_or_senddone(conn),
  194. &wait);
  195. remove_wait_queue(sk_sleep(sk), &wait);
  196. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  197. return rc;
  198. }
  199. static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len,
  200. int flags)
  201. {
  202. struct smc_connection *conn = &smc->conn;
  203. union smc_host_cursor cons;
  204. struct sock *sk = &smc->sk;
  205. int rc = 0;
  206. if (sock_flag(sk, SOCK_URGINLINE) ||
  207. !(conn->urg_state == SMC_URG_VALID) ||
  208. conn->urg_state == SMC_URG_READ)
  209. return -EINVAL;
  210. if (conn->urg_state == SMC_URG_VALID) {
  211. if (!(flags & MSG_PEEK))
  212. smc->conn.urg_state = SMC_URG_READ;
  213. msg->msg_flags |= MSG_OOB;
  214. if (len > 0) {
  215. if (!(flags & MSG_TRUNC))
  216. rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1);
  217. len = 1;
  218. smc_curs_write(&cons,
  219. smc_curs_read(&conn->local_tx_ctrl.cons,
  220. conn),
  221. conn);
  222. if (smc_curs_diff(conn->rmb_desc->len, &cons,
  223. &conn->urg_curs) > 1)
  224. conn->urg_rx_skip_pend = true;
  225. /* Urgent Byte was already accounted for, but trigger
  226. * skipping the urgent byte in non-inline case
  227. */
  228. if (!(flags & MSG_PEEK))
  229. smc_rx_update_consumer(smc, cons, 0);
  230. } else {
  231. msg->msg_flags |= MSG_TRUNC;
  232. }
  233. return rc ? -EFAULT : len;
  234. }
  235. if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN)
  236. return 0;
  237. return -EAGAIN;
  238. }
  239. /* smc_rx_recvmsg - receive data from RMBE
  240. * @msg: copy data to receive buffer
  241. * @pipe: copy data to pipe if set - indicates splice() call
  242. *
  243. * rcvbuf consumer: main API called by socket layer.
  244. * Called under sk lock.
  245. */
  246. int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
  247. struct pipe_inode_info *pipe, size_t len, int flags)
  248. {
  249. size_t copylen, read_done = 0, read_remaining = len;
  250. size_t chunk_len, chunk_off, chunk_len_sum;
  251. struct smc_connection *conn = &smc->conn;
  252. int (*func)(struct smc_connection *conn);
  253. union smc_host_cursor cons;
  254. int readable, chunk;
  255. char *rcvbuf_base;
  256. struct sock *sk;
  257. int splbytes;
  258. long timeo;
  259. int target; /* Read at least these many bytes */
  260. int rc;
  261. if (unlikely(flags & MSG_ERRQUEUE))
  262. return -EINVAL; /* future work for sk.sk_family == AF_SMC */
  263. sk = &smc->sk;
  264. if (sk->sk_state == SMC_LISTEN)
  265. return -ENOTCONN;
  266. if (flags & MSG_OOB)
  267. return smc_rx_recv_urg(smc, msg, len, flags);
  268. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  269. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  270. /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
  271. rcvbuf_base = conn->rmb_desc->cpu_addr;
  272. do { /* while (read_remaining) */
  273. if (read_done >= target || (pipe && read_done))
  274. break;
  275. if (atomic_read(&conn->bytes_to_rcv))
  276. goto copy;
  277. else if (conn->urg_state == SMC_URG_VALID)
  278. /* we received a single urgent Byte - skip */
  279. smc_rx_update_cons(smc, 0);
  280. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  281. smc_cdc_rxed_any_close_or_senddone(conn) ||
  282. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort)
  283. break;
  284. if (read_done) {
  285. if (sk->sk_err ||
  286. sk->sk_state == SMC_CLOSED ||
  287. !timeo ||
  288. signal_pending(current))
  289. break;
  290. } else {
  291. if (sk->sk_err) {
  292. read_done = sock_error(sk);
  293. break;
  294. }
  295. if (sk->sk_state == SMC_CLOSED) {
  296. if (!sock_flag(sk, SOCK_DONE)) {
  297. /* This occurs when user tries to read
  298. * from never connected socket.
  299. */
  300. read_done = -ENOTCONN;
  301. break;
  302. }
  303. break;
  304. }
  305. if (signal_pending(current)) {
  306. read_done = sock_intr_errno(timeo);
  307. break;
  308. }
  309. if (!timeo)
  310. return -EAGAIN;
  311. }
  312. if (!smc_rx_data_available(conn)) {
  313. smc_rx_wait(smc, &timeo, smc_rx_data_available);
  314. continue;
  315. }
  316. copy:
  317. /* initialize variables for 1st iteration of subsequent loop */
  318. /* could be just 1 byte, even after waiting on data above */
  319. readable = atomic_read(&conn->bytes_to_rcv);
  320. splbytes = atomic_read(&conn->splice_pending);
  321. if (!readable || (msg && splbytes)) {
  322. if (splbytes)
  323. func = smc_rx_data_available_and_no_splice_pend;
  324. else
  325. func = smc_rx_data_available;
  326. smc_rx_wait(smc, &timeo, func);
  327. continue;
  328. }
  329. smc_curs_write(&cons,
  330. smc_curs_read(&conn->local_tx_ctrl.cons, conn),
  331. conn);
  332. /* subsequent splice() calls pick up where previous left */
  333. if (splbytes)
  334. smc_curs_add(conn->rmb_desc->len, &cons, splbytes);
  335. if (conn->urg_state == SMC_URG_VALID &&
  336. sock_flag(&smc->sk, SOCK_URGINLINE) &&
  337. readable > 1)
  338. readable--; /* always stop at urgent Byte */
  339. /* not more than what user space asked for */
  340. copylen = min_t(size_t, read_remaining, readable);
  341. /* determine chunks where to read from rcvbuf */
  342. /* either unwrapped case, or 1st chunk of wrapped case */
  343. chunk_len = min_t(size_t, copylen, conn->rmb_desc->len -
  344. cons.count);
  345. chunk_len_sum = chunk_len;
  346. chunk_off = cons.count;
  347. smc_rmb_sync_sg_for_cpu(conn);
  348. for (chunk = 0; chunk < 2; chunk++) {
  349. if (!(flags & MSG_TRUNC)) {
  350. if (msg) {
  351. rc = memcpy_to_msg(msg, rcvbuf_base +
  352. chunk_off,
  353. chunk_len);
  354. } else {
  355. rc = smc_rx_splice(pipe, rcvbuf_base +
  356. chunk_off, chunk_len,
  357. smc);
  358. }
  359. if (rc < 0) {
  360. if (!read_done)
  361. read_done = -EFAULT;
  362. smc_rmb_sync_sg_for_device(conn);
  363. goto out;
  364. }
  365. }
  366. read_remaining -= chunk_len;
  367. read_done += chunk_len;
  368. if (chunk_len_sum == copylen)
  369. break; /* either on 1st or 2nd iteration */
  370. /* prepare next (== 2nd) iteration */
  371. chunk_len = copylen - chunk_len; /* remainder */
  372. chunk_len_sum += chunk_len;
  373. chunk_off = 0; /* modulo offset in recv ring buffer */
  374. }
  375. smc_rmb_sync_sg_for_device(conn);
  376. /* update cursors */
  377. if (!(flags & MSG_PEEK)) {
  378. /* increased in recv tasklet smc_cdc_msg_rcv() */
  379. smp_mb__before_atomic();
  380. atomic_sub(copylen, &conn->bytes_to_rcv);
  381. /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
  382. smp_mb__after_atomic();
  383. if (msg && smc_rx_update_consumer(smc, cons, copylen))
  384. goto out;
  385. }
  386. } while (read_remaining);
  387. out:
  388. return read_done;
  389. }
  390. /* Initialize receive properties on connection establishment. NB: not __init! */
  391. void smc_rx_init(struct smc_sock *smc)
  392. {
  393. smc->sk.sk_data_ready = smc_rx_wake_up;
  394. atomic_set(&smc->conn.splice_pending, 0);
  395. smc->conn.urg_state = SMC_URG_READ;
  396. }