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 13 #include <linux/net.h> 14 #include <linux/rcupdate.h> 15 #include <linux/sched/signal.h> 16 #include <linux/splice.h> 17 18 #include <net/sock.h> 19 #include <trace/events/sock.h> 20 21 #include "smc.h" 22 #include "smc_core.h" 23 #include "smc_cdc.h" 24 #include "smc_tx.h" /* smc_tx_consumer_update() */ 25 #include "smc_rx.h" 26 #include "smc_stats.h" 27 #include "smc_tracepoint.h" 28 29 /* callback implementation to wakeup consumers blocked with smc_rx_wait(). 30 * indirectly called by smc_cdc_msg_recv_action(). 31 */ 32 static void smc_rx_wake_up(struct sock *sk) 33 { 34 struct socket_wq *wq; 35 36 trace_sk_data_ready(sk); 37 38 /* derived from sock_def_readable() */ 39 /* called already in smc_listen_work() */ 40 rcu_read_lock(); 41 wq = rcu_dereference(sk->sk_wq); 42 if (skwq_has_sleeper(wq)) 43 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI | 44 EPOLLRDNORM | EPOLLRDBAND); 45 sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); 46 if ((sk->sk_shutdown == SHUTDOWN_MASK) || 47 (sk->sk_state == SMC_CLOSED)) 48 sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_HUP); 49 rcu_read_unlock(); 50 } 51 52 /* Update consumer cursor 53 * @conn connection to update 54 * @cons consumer cursor 55 * @len number of Bytes consumed 56 * Returns: 57 * 1 if we should end our receive, 0 otherwise 58 */ 59 static int smc_rx_update_consumer(struct smc_sock *smc, 60 union smc_host_cursor cons, size_t len) 61 { 62 struct smc_connection *conn = &smc->conn; 63 struct sock *sk = &smc->sk; 64 bool force = false; 65 int diff, rc = 0; 66 67 smc_curs_add(conn->rmb_desc->len, &cons, len); 68 69 /* did we process urgent data? */ 70 if (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) { 71 diff = smc_curs_comp(conn->rmb_desc->len, &cons, 72 &conn->urg_curs); 73 if (sock_flag(sk, SOCK_URGINLINE)) { 74 if (diff == 0) { 75 force = true; 76 rc = 1; 77 conn->urg_state = SMC_URG_READ; 78 } 79 } else { 80 if (diff == 1) { 81 /* skip urgent byte */ 82 force = true; 83 smc_curs_add(conn->rmb_desc->len, &cons, 1); 84 conn->urg_rx_skip_pend = false; 85 } else if (diff < -1) 86 /* we read past urgent byte */ 87 conn->urg_state = SMC_URG_READ; 88 } 89 } 90 91 smc_curs_copy(&conn->local_tx_ctrl.cons, &cons, conn); 92 93 /* send consumer cursor update if required */ 94 /* similar to advertising new TCP rcv_wnd if required */ 95 smc_tx_consumer_update(conn, force); 96 97 return rc; 98 } 99 100 static void smc_rx_update_cons(struct smc_sock *smc, size_t len) 101 { 102 struct smc_connection *conn = &smc->conn; 103 union smc_host_cursor cons; 104 105 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); 106 smc_rx_update_consumer(smc, cons, len); 107 } 108 109 struct smc_spd_priv { 110 struct smc_sock *smc; 111 size_t len; 112 }; 113 114 static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe, 115 struct pipe_buffer *buf) 116 { 117 struct smc_spd_priv *priv = (struct smc_spd_priv *)buf->private; 118 struct smc_sock *smc = priv->smc; 119 struct smc_connection *conn; 120 struct sock *sk = &smc->sk; 121 122 if (sk->sk_state == SMC_CLOSED || 123 sk->sk_state == SMC_PEERFINCLOSEWAIT || 124 sk->sk_state == SMC_APPFINCLOSEWAIT) 125 goto out; 126 conn = &smc->conn; 127 lock_sock(sk); 128 smc_rx_update_cons(smc, priv->len); 129 release_sock(sk); 130 if (atomic_sub_and_test(priv->len, &conn->splice_pending)) 131 smc_rx_wake_up(sk); 132 out: 133 kfree(priv); 134 put_page(buf->page); 135 sock_put(sk); 136 } 137 138 static bool smc_rx_pipe_buf_get(struct pipe_inode_info *pipe, 139 struct pipe_buffer *buf) 140 { 141 /* smc_spd_priv in buf->private is not shareable; disallow cloning. */ 142 return false; 143 } 144 145 static const struct pipe_buf_operations smc_pipe_ops = { 146 .release = smc_rx_pipe_buf_release, 147 .get = smc_rx_pipe_buf_get, 148 }; 149 150 static void smc_rx_spd_release(struct splice_pipe_desc *spd, 151 unsigned int i) 152 { 153 struct smc_spd_priv *priv = (struct smc_spd_priv *)spd->partial[i].private; 154 struct sock *sk = &priv->smc->sk; 155 156 kfree(priv); 157 put_page(spd->pages[i]); 158 sock_put(sk); 159 } 160 161 static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len, 162 struct smc_sock *smc) 163 { 164 struct smc_link_group *lgr = smc->conn.lgr; 165 int offset = offset_in_page(src); 166 struct partial_page *partial; 167 struct splice_pipe_desc spd; 168 struct smc_spd_priv **priv; 169 struct page **pages; 170 int bytes, nr_pages; 171 int i; 172 173 nr_pages = !lgr->is_smcd && smc->conn.rmb_desc->is_vm ? 174 PAGE_ALIGN(len + offset) / PAGE_SIZE : 1; 175 176 pages = kzalloc_objs(*pages, nr_pages); 177 if (!pages) 178 goto out; 179 partial = kzalloc_objs(*partial, nr_pages); 180 if (!partial) 181 goto out_page; 182 priv = kzalloc_objs(*priv, nr_pages); 183 if (!priv) 184 goto out_part; 185 for (i = 0; i < nr_pages; i++) { 186 priv[i] = kzalloc_obj(**priv); 187 if (!priv[i]) 188 goto out_priv; 189 } 190 191 if (lgr->is_smcd || 192 (!lgr->is_smcd && !smc->conn.rmb_desc->is_vm)) { 193 /* smcd or smcr that uses physically contiguous RMBs */ 194 priv[0]->len = len; 195 priv[0]->smc = smc; 196 partial[0].offset = src - (char *)smc->conn.rmb_desc->cpu_addr; 197 partial[0].len = len; 198 partial[0].private = (unsigned long)priv[0]; 199 pages[0] = smc->conn.rmb_desc->pages; 200 } else { 201 int size, left = len; 202 void *buf = src; 203 /* smcr that uses virtually contiguous RMBs*/ 204 for (i = 0; i < nr_pages; i++) { 205 size = min_t(int, PAGE_SIZE - offset, left); 206 priv[i]->len = size; 207 priv[i]->smc = smc; 208 pages[i] = vmalloc_to_page(buf); 209 partial[i].offset = offset; 210 partial[i].len = size; 211 partial[i].private = (unsigned long)priv[i]; 212 buf += size; 213 left -= size; 214 offset = 0; 215 } 216 } 217 for (i = 0; i < nr_pages; i++) { 218 get_page(pages[i]); 219 sock_hold(&smc->sk); 220 } 221 spd.nr_pages_max = nr_pages; 222 spd.nr_pages = nr_pages; 223 spd.pages = pages; 224 spd.partial = partial; 225 spd.ops = &smc_pipe_ops; 226 spd.spd_release = smc_rx_spd_release; 227 228 bytes = splice_to_pipe(pipe, &spd); 229 if (bytes > 0) 230 atomic_add(bytes, &smc->conn.splice_pending); 231 kfree(priv); 232 kfree(partial); 233 kfree(pages); 234 235 return bytes; 236 237 out_priv: 238 for (i = (i - 1); i >= 0; i--) 239 kfree(priv[i]); 240 kfree(priv); 241 out_part: 242 kfree(partial); 243 out_page: 244 kfree(pages); 245 out: 246 return -ENOMEM; 247 } 248 249 static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn, size_t peeked) 250 { 251 return smc_rx_data_available(conn, peeked) && 252 !atomic_read(&conn->splice_pending); 253 } 254 255 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted 256 * @smc smc socket 257 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout 258 * @peeked number of bytes already peeked 259 * @fcrit add'l criterion to evaluate as function pointer 260 * Returns: 261 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown. 262 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted). 263 */ 264 int smc_rx_wait(struct smc_sock *smc, long *timeo, size_t peeked, 265 int (*fcrit)(struct smc_connection *conn, size_t baseline)) 266 { 267 DEFINE_WAIT_FUNC(wait, woken_wake_function); 268 struct smc_connection *conn = &smc->conn; 269 struct smc_cdc_conn_state_flags *cflags = 270 &conn->local_tx_ctrl.conn_state_flags; 271 struct sock *sk = &smc->sk; 272 int rc; 273 274 if (fcrit(conn, peeked)) 275 return 1; 276 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 277 add_wait_queue(sk_sleep(sk), &wait); 278 rc = sk_wait_event(sk, timeo, 279 READ_ONCE(sk->sk_err) || 280 cflags->peer_conn_abort || 281 READ_ONCE(sk->sk_shutdown) & RCV_SHUTDOWN || 282 conn->killed || 283 fcrit(conn, peeked), 284 &wait); 285 remove_wait_queue(sk_sleep(sk), &wait); 286 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 287 return rc; 288 } 289 290 static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len, 291 int flags) 292 { 293 struct smc_connection *conn = &smc->conn; 294 union smc_host_cursor cons; 295 struct sock *sk = &smc->sk; 296 int rc = 0; 297 298 if (sock_flag(sk, SOCK_URGINLINE) || 299 !(conn->urg_state == SMC_URG_VALID) || 300 conn->urg_state == SMC_URG_READ) 301 return -EINVAL; 302 303 SMC_STAT_INC(smc, urg_data_cnt); 304 if (conn->urg_state == SMC_URG_VALID) { 305 if (!(flags & MSG_PEEK)) 306 smc->conn.urg_state = SMC_URG_READ; 307 msg->msg_flags |= MSG_OOB; 308 if (len > 0) { 309 if (!(flags & MSG_TRUNC)) 310 rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1); 311 len = 1; 312 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); 313 if (smc_curs_diff(conn->rmb_desc->len, &cons, 314 &conn->urg_curs) > 1) 315 conn->urg_rx_skip_pend = true; 316 /* Urgent Byte was already accounted for, but trigger 317 * skipping the urgent byte in non-inline case 318 */ 319 if (!(flags & MSG_PEEK)) 320 smc_rx_update_consumer(smc, cons, 0); 321 } else { 322 msg->msg_flags |= MSG_TRUNC; 323 } 324 325 return rc ? -EFAULT : len; 326 } 327 328 if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN) 329 return 0; 330 331 return -EAGAIN; 332 } 333 334 static bool smc_rx_recvmsg_data_available(struct smc_sock *smc, size_t peeked) 335 { 336 struct smc_connection *conn = &smc->conn; 337 338 if (smc_rx_data_available(conn, peeked)) 339 return true; 340 else if (conn->urg_state == SMC_URG_VALID) 341 /* we received a single urgent Byte - skip */ 342 smc_rx_update_cons(smc, 0); 343 return false; 344 } 345 346 /* smc_rx_recvmsg - receive data from RMBE 347 * @msg: copy data to receive buffer 348 * @pipe: copy data to pipe if set - indicates splice() call 349 * 350 * rcvbuf consumer: main API called by socket layer. 351 * Called under sk lock. 352 */ 353 int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg, 354 struct pipe_inode_info *pipe, size_t len, int flags) 355 { 356 size_t copylen, read_done = 0, read_remaining = len, peeked_bytes = 0; 357 size_t chunk_len, chunk_off, chunk_len_sum; 358 struct smc_connection *conn = &smc->conn; 359 int (*func)(struct smc_connection *conn, size_t baseline); 360 union smc_host_cursor cons; 361 int readable, chunk; 362 char *rcvbuf_base; 363 struct sock *sk; 364 int splbytes; 365 long timeo; 366 int target; /* Read at least these many bytes */ 367 int rc; 368 369 if (unlikely(flags & MSG_ERRQUEUE)) 370 return -EINVAL; /* future work for sk.sk_family == AF_SMC */ 371 372 sk = &smc->sk; 373 if (sk->sk_state == SMC_LISTEN) 374 return -ENOTCONN; 375 if (flags & MSG_OOB) 376 return smc_rx_recv_urg(smc, msg, len, flags); 377 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 378 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 379 380 readable = atomic_read(&conn->bytes_to_rcv); 381 if (readable >= conn->rmb_desc->len) 382 SMC_STAT_RMB_RX_FULL(smc, !conn->lnk); 383 384 if (len < readable) 385 SMC_STAT_RMB_RX_SIZE_SMALL(smc, !conn->lnk); 386 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */ 387 rcvbuf_base = conn->rx_off + conn->rmb_desc->cpu_addr; 388 389 do { /* while (read_remaining) */ 390 if (read_done >= target || (pipe && read_done)) 391 break; 392 393 if (conn->killed) 394 break; 395 396 if (smc_rx_recvmsg_data_available(smc, peeked_bytes)) 397 goto copy; 398 399 if (sk->sk_shutdown & RCV_SHUTDOWN) { 400 /* smc_cdc_msg_recv_action() could have run after 401 * above smc_rx_recvmsg_data_available() 402 */ 403 if (smc_rx_recvmsg_data_available(smc, peeked_bytes)) 404 goto copy; 405 break; 406 } 407 408 if (read_done) { 409 if (sk->sk_err || 410 sk->sk_state == SMC_CLOSED || 411 !timeo || 412 signal_pending(current)) 413 break; 414 } else { 415 if (sk->sk_err) { 416 read_done = sock_error(sk); 417 break; 418 } 419 if (sk->sk_state == SMC_CLOSED) { 420 if (!sock_flag(sk, SOCK_DONE)) { 421 /* This occurs when user tries to read 422 * from never connected socket. 423 */ 424 read_done = -ENOTCONN; 425 break; 426 } 427 break; 428 } 429 if (!timeo) 430 return -EAGAIN; 431 if (signal_pending(current)) { 432 read_done = sock_intr_errno(timeo); 433 break; 434 } 435 } 436 437 if (!smc_rx_data_available(conn, peeked_bytes)) { 438 smc_rx_wait(smc, &timeo, peeked_bytes, smc_rx_data_available); 439 continue; 440 } 441 442 copy: 443 /* initialize variables for 1st iteration of subsequent loop */ 444 /* could be just 1 byte, even after waiting on data above */ 445 readable = smc_rx_data_available(conn, peeked_bytes); 446 splbytes = atomic_read(&conn->splice_pending); 447 if (!readable || (msg && splbytes)) { 448 if (splbytes) 449 func = smc_rx_data_available_and_no_splice_pend; 450 else 451 func = smc_rx_data_available; 452 smc_rx_wait(smc, &timeo, peeked_bytes, func); 453 continue; 454 } 455 456 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); 457 if ((flags & MSG_PEEK) && peeked_bytes) 458 smc_curs_add(conn->rmb_desc->len, &cons, peeked_bytes); 459 /* subsequent splice() calls pick up where previous left */ 460 if (splbytes) 461 smc_curs_add(conn->rmb_desc->len, &cons, splbytes); 462 if (conn->urg_state == SMC_URG_VALID && 463 sock_flag(&smc->sk, SOCK_URGINLINE) && 464 readable > 1) 465 readable--; /* always stop at urgent Byte */ 466 /* not more than what user space asked for */ 467 copylen = min_t(size_t, read_remaining, readable); 468 /* determine chunks where to read from rcvbuf */ 469 /* either unwrapped case, or 1st chunk of wrapped case */ 470 chunk_len = min_t(size_t, copylen, conn->rmb_desc->len - 471 cons.count); 472 chunk_len_sum = chunk_len; 473 chunk_off = cons.count; 474 smc_rmb_sync_sg_for_cpu(conn); 475 for (chunk = 0; chunk < 2; chunk++) { 476 if (!(flags & MSG_TRUNC)) { 477 if (msg) { 478 rc = memcpy_to_msg(msg, rcvbuf_base + 479 chunk_off, 480 chunk_len); 481 } else { 482 rc = smc_rx_splice(pipe, rcvbuf_base + 483 chunk_off, chunk_len, 484 smc); 485 } 486 if (rc < 0) { 487 if (!read_done) 488 read_done = -EFAULT; 489 goto out; 490 } 491 } 492 read_remaining -= chunk_len; 493 read_done += chunk_len; 494 if (flags & MSG_PEEK) 495 peeked_bytes += chunk_len; 496 497 if (chunk_len_sum == copylen) 498 break; /* either on 1st or 2nd iteration */ 499 /* prepare next (== 2nd) iteration */ 500 chunk_len = copylen - chunk_len; /* remainder */ 501 chunk_len_sum += chunk_len; 502 chunk_off = 0; /* modulo offset in recv ring buffer */ 503 } 504 505 /* update cursors */ 506 if (!(flags & MSG_PEEK)) { 507 /* increased in recv tasklet smc_cdc_msg_rcv() */ 508 smp_mb__before_atomic(); 509 atomic_sub(copylen, &conn->bytes_to_rcv); 510 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */ 511 smp_mb__after_atomic(); 512 if (msg && smc_rx_update_consumer(smc, cons, copylen)) 513 goto out; 514 } 515 516 trace_smc_rx_recvmsg(smc, copylen); 517 } while (read_remaining); 518 out: 519 return read_done; 520 } 521 522 /* Initialize receive properties on connection establishment. NB: not __init! */ 523 void smc_rx_init(struct smc_sock *smc) 524 { 525 smc->sk.sk_data_ready = smc_rx_wake_up; 526 atomic_set(&smc->conn.splice_pending, 0); 527 smc->conn.urg_state = SMC_URG_READ; 528 } 529