| 1 | |
| 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 <[email protected]>
|
| 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"
|
| 25 | #include "smc_rx.h"
|
| 26 | #include "smc_stats.h"
|
| 27 | #include "smc_tracepoint.h"
|
| 28 |
|
| 29 | |
| 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 |
|
| 39 |
|
| 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 | |
| 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 |
|
| 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 |
|
| 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 |
|
| 87 | conn->urg_state = SMC_URG_READ;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | smc_curs_copy(&conn->local_tx_ctrl.cons, &cons, conn);
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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_connection *conn = &priv->smc->conn;
|
| 119 | struct smc_sock *smc = priv->smc;
|
| 120 | struct sock *sk = &smc->sk;
|
| 121 |
|
| 122 | lock_sock(sk);
|
| 123 | if (conn->freed) {
|
| 124 | release_sock(sk);
|
| 125 | goto out;
|
| 126 | }
|
| 127 | smc_rx_update_cons(smc, priv->len);
|
| 128 | release_sock(sk);
|
| 129 | if (atomic_sub_and_test(priv->len, &conn->splice_pending))
|
| 130 | smc_rx_wake_up(sk);
|
| 131 | out:
|
| 132 | kfree(priv);
|
| 133 | put_page(buf->page);
|
| 134 | sock_put(sk);
|
| 135 | }
|
| 136 |
|
| 137 | static bool smc_rx_pipe_buf_get(struct pipe_inode_info *pipe,
|
| 138 | struct pipe_buffer *buf)
|
| 139 | {
|
| 140 |
|
| 141 | return false;
|
| 142 | }
|
| 143 |
|
| 144 | static const struct pipe_buf_operations smc_pipe_ops = {
|
| 145 | .release = smc_rx_pipe_buf_release,
|
| 146 | .get = smc_rx_pipe_buf_get,
|
| 147 | };
|
| 148 |
|
| 149 | static void smc_rx_spd_release(struct splice_pipe_desc *spd,
|
| 150 | unsigned int i)
|
| 151 | {
|
| 152 | struct smc_spd_priv *priv = (struct smc_spd_priv *)spd->partial[i].private;
|
| 153 | struct sock *sk = &priv->smc->sk;
|
| 154 |
|
| 155 | kfree(priv);
|
| 156 | put_page(spd->pages[i]);
|
| 157 | sock_put(sk);
|
| 158 | }
|
| 159 |
|
| 160 | static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
|
| 161 | struct smc_sock *smc)
|
| 162 | {
|
| 163 | struct smc_link_group *lgr = smc->conn.lgr;
|
| 164 | int offset = offset_in_page(src);
|
| 165 | struct partial_page *partial;
|
| 166 | struct splice_pipe_desc spd;
|
| 167 | struct smc_spd_priv **priv;
|
| 168 | struct page **pages;
|
| 169 | int bytes, nr_pages;
|
| 170 | int i;
|
| 171 |
|
| 172 | nr_pages = !lgr->is_smcd && smc->conn.rmb_desc->is_vm ?
|
| 173 | PAGE_ALIGN(len + offset) / PAGE_SIZE : 1;
|
| 174 |
|
| 175 | pages = kzalloc_objs(*pages, nr_pages);
|
| 176 | if (!pages)
|
| 177 | goto out;
|
| 178 | partial = kzalloc_objs(*partial, nr_pages);
|
| 179 | if (!partial)
|
| 180 | goto out_page;
|
| 181 | priv = kzalloc_objs(*priv, nr_pages);
|
| 182 | if (!priv)
|
| 183 | goto out_part;
|
| 184 | for (i = 0; i < nr_pages; i++) {
|
| 185 | priv[i] = kzalloc_obj(**priv);
|
| 186 | if (!priv[i])
|
| 187 | goto out_priv;
|
| 188 | }
|
| 189 |
|
| 190 | if (lgr->is_smcd ||
|
| 191 | (!lgr->is_smcd && !smc->conn.rmb_desc->is_vm)) {
|
| 192 |
|
| 193 | priv[0]->len = len;
|
| 194 | priv[0]->smc = smc;
|
| 195 | partial[0].offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
|
| 196 | partial[0].len = len;
|
| 197 | partial[0].private = (unsigned long)priv[0];
|
| 198 | pages[0] = smc->conn.rmb_desc->pages;
|
| 199 | } else {
|
| 200 | int size, left = len;
|
| 201 | void *buf = src;
|
| 202 |
|
| 203 | for (i = 0; i < nr_pages; i++) {
|
| 204 | size = min_t(int, PAGE_SIZE - offset, left);
|
| 205 | priv[i]->len = size;
|
| 206 | priv[i]->smc = smc;
|
| 207 | pages[i] = vmalloc_to_page(buf);
|
| 208 | partial[i].offset = offset;
|
| 209 | partial[i].len = size;
|
| 210 | partial[i].private = (unsigned long)priv[i];
|
| 211 | buf += size;
|
| 212 | left -= size;
|
| 213 | offset = 0;
|
| 214 | }
|
| 215 | }
|
| 216 | for (i = 0; i < nr_pages; i++) {
|
| 217 | get_page(pages[i]);
|
| 218 | sock_hold(&smc->sk);
|
| 219 | }
|
| 220 | spd.nr_pages_max = nr_pages;
|
| 221 | spd.nr_pages = nr_pages;
|
| 222 | spd.pages = pages;
|
| 223 | spd.partial = partial;
|
| 224 | spd.ops = &smc_pipe_ops;
|
| 225 | spd.spd_release = smc_rx_spd_release;
|
| 226 |
|
| 227 | bytes = splice_to_pipe(pipe, &spd);
|
| 228 | if (bytes > 0)
|
| 229 | atomic_add(bytes, &smc->conn.splice_pending);
|
| 230 | kfree(priv);
|
| 231 | kfree(partial);
|
| 232 | kfree(pages);
|
| 233 |
|
| 234 | return bytes;
|
| 235 |
|
| 236 | out_priv:
|
| 237 | for (i = (i - 1); i >= 0; i--)
|
| 238 | kfree(priv[i]);
|
| 239 | kfree(priv);
|
| 240 | out_part:
|
| 241 | kfree(partial);
|
| 242 | out_page:
|
| 243 | kfree(pages);
|
| 244 | out:
|
| 245 | return -ENOMEM;
|
| 246 | }
|
| 247 |
|
| 248 | static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn, size_t peeked)
|
| 249 | {
|
| 250 | return smc_rx_data_available(conn, peeked) &&
|
| 251 | !atomic_read(&conn->splice_pending);
|
| 252 | }
|
| 253 |
|
| 254 | |
| 255 | * @smc smc socket
|
| 256 | * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
|
| 257 | * @peeked number of bytes already peeked
|
| 258 | * @fcrit add'l criterion to evaluate as function pointer
|
| 259 | * Returns:
|
| 260 | * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
|
| 261 | * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
|
| 262 |
|
| 263 | int smc_rx_wait(struct smc_sock *smc, long *timeo, size_t peeked,
|
| 264 | int (*fcrit)(struct smc_connection *conn, size_t baseline))
|
| 265 | {
|
| 266 | DEFINE_WAIT_FUNC(wait, woken_wake_function);
|
| 267 | struct smc_connection *conn = &smc->conn;
|
| 268 | struct smc_cdc_conn_state_flags *cflags =
|
| 269 | &conn->local_tx_ctrl.conn_state_flags;
|
| 270 | struct sock *sk = &smc->sk;
|
| 271 | int rc;
|
| 272 |
|
| 273 | if (fcrit(conn, peeked))
|
| 274 | return 1;
|
| 275 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
|
| 276 | add_wait_queue(sk_sleep(sk), &wait);
|
| 277 | rc = sk_wait_event(sk, timeo,
|
| 278 | READ_ONCE(sk->sk_err) ||
|
| 279 | cflags->peer_conn_abort ||
|
| 280 | READ_ONCE(sk->sk_shutdown) & RCV_SHUTDOWN ||
|
| 281 | conn->killed ||
|
| 282 | fcrit(conn, peeked),
|
| 283 | &wait);
|
| 284 | remove_wait_queue(sk_sleep(sk), &wait);
|
| 285 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
|
| 286 | return rc;
|
| 287 | }
|
| 288 |
|
| 289 | static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len,
|
| 290 | int flags)
|
| 291 | {
|
| 292 | struct smc_connection *conn = &smc->conn;
|
| 293 | union smc_host_cursor cons;
|
| 294 | struct sock *sk = &smc->sk;
|
| 295 | int rc = 0;
|
| 296 |
|
| 297 | if (sock_flag(sk, SOCK_URGINLINE) ||
|
| 298 | !(conn->urg_state == SMC_URG_VALID) ||
|
| 299 | conn->urg_state == SMC_URG_READ)
|
| 300 | return -EINVAL;
|
| 301 |
|
| 302 | SMC_STAT_INC(smc, urg_data_cnt);
|
| 303 | if (conn->urg_state == SMC_URG_VALID) {
|
| 304 | if (!(flags & MSG_PEEK))
|
| 305 | smc->conn.urg_state = SMC_URG_READ;
|
| 306 | msg->msg_flags |= MSG_OOB;
|
| 307 | if (len > 0) {
|
| 308 | if (!(flags & MSG_TRUNC))
|
| 309 | rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1);
|
| 310 | len = 1;
|
| 311 | smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
|
| 312 | if (smc_curs_diff(conn->rmb_desc->len, &cons,
|
| 313 | &conn->urg_curs) > 1)
|
| 314 | conn->urg_rx_skip_pend = true;
|
| 315 | |
| 316 | * skipping the urgent byte in non-inline case
|
| 317 |
|
| 318 | if (!(flags & MSG_PEEK))
|
| 319 | smc_rx_update_consumer(smc, cons, 0);
|
| 320 | } else {
|
| 321 | msg->msg_flags |= MSG_TRUNC;
|
| 322 | }
|
| 323 |
|
| 324 | return rc ? -EFAULT : len;
|
| 325 | }
|
| 326 |
|
| 327 | if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN)
|
| 328 | return 0;
|
| 329 |
|
| 330 | return -EAGAIN;
|
| 331 | }
|
| 332 |
|
| 333 | static bool smc_rx_recvmsg_data_available(struct smc_sock *smc, size_t peeked)
|
| 334 | {
|
| 335 | struct smc_connection *conn = &smc->conn;
|
| 336 |
|
| 337 | if (smc_rx_data_available(conn, peeked))
|
| 338 | return true;
|
| 339 | else if (conn->urg_state == SMC_URG_VALID)
|
| 340 |
|
| 341 | smc_rx_update_cons(smc, 0);
|
| 342 | return false;
|
| 343 | }
|
| 344 |
|
| 345 | |
| 346 | * @msg: copy data to receive buffer
|
| 347 | * @pipe: copy data to pipe if set - indicates splice() call
|
| 348 | *
|
| 349 | * rcvbuf consumer: main API called by socket layer.
|
| 350 | * Called under sk lock.
|
| 351 |
|
| 352 | int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
|
| 353 | struct pipe_inode_info *pipe, size_t len, int flags)
|
| 354 | {
|
| 355 | size_t copylen, read_done = 0, read_remaining = len, peeked_bytes = 0;
|
| 356 | size_t chunk_len, chunk_off, chunk_len_sum;
|
| 357 | struct smc_connection *conn = &smc->conn;
|
| 358 | int (*func)(struct smc_connection *conn, size_t baseline);
|
| 359 | union smc_host_cursor cons;
|
| 360 | int readable, chunk;
|
| 361 | char *rcvbuf_base;
|
| 362 | struct sock *sk;
|
| 363 | int splbytes;
|
| 364 | long timeo;
|
| 365 | int target;
|
| 366 | int rc;
|
| 367 |
|
| 368 | if (unlikely(flags & MSG_ERRQUEUE))
|
| 369 | return -EINVAL;
|
| 370 |
|
| 371 | sk = &smc->sk;
|
| 372 | if (sk->sk_state == SMC_LISTEN)
|
| 373 | return -ENOTCONN;
|
| 374 | if (flags & MSG_OOB)
|
| 375 | return smc_rx_recv_urg(smc, msg, len, flags);
|
| 376 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
|
| 377 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
|
| 378 |
|
| 379 | readable = atomic_read(&conn->bytes_to_rcv);
|
| 380 | if (readable >= conn->rmb_desc->len)
|
| 381 | SMC_STAT_RMB_RX_FULL(smc, !conn->lnk);
|
| 382 |
|
| 383 | if (len < readable)
|
| 384 | SMC_STAT_RMB_RX_SIZE_SMALL(smc, !conn->lnk);
|
| 385 |
|
| 386 | rcvbuf_base = conn->rx_off + conn->rmb_desc->cpu_addr;
|
| 387 |
|
| 388 | do {
|
| 389 | if (read_done >= target || (pipe && read_done))
|
| 390 | break;
|
| 391 |
|
| 392 | if (conn->killed)
|
| 393 | break;
|
| 394 |
|
| 395 | if (smc_rx_recvmsg_data_available(smc, peeked_bytes))
|
| 396 | goto copy;
|
| 397 |
|
| 398 | if (sk->sk_shutdown & RCV_SHUTDOWN) {
|
| 399 | |
| 400 | * above smc_rx_recvmsg_data_available()
|
| 401 |
|
| 402 | if (smc_rx_recvmsg_data_available(smc, peeked_bytes))
|
| 403 | goto copy;
|
| 404 | break;
|
| 405 | }
|
| 406 |
|
| 407 | if (read_done) {
|
| 408 | if (sk->sk_err ||
|
| 409 | sk->sk_state == SMC_CLOSED ||
|
| 410 | !timeo ||
|
| 411 | signal_pending(current))
|
| 412 | break;
|
| 413 | } else {
|
| 414 | if (sk->sk_err) {
|
| 415 | read_done = sock_error(sk);
|
| 416 | break;
|
| 417 | }
|
| 418 | if (sk->sk_state == SMC_CLOSED) {
|
| 419 | if (!sock_flag(sk, SOCK_DONE)) {
|
| 420 | |
| 421 | * from never connected socket.
|
| 422 |
|
| 423 | read_done = -ENOTCONN;
|
| 424 | break;
|
| 425 | }
|
| 426 | break;
|
| 427 | }
|
| 428 | if (!timeo)
|
| 429 | return -EAGAIN;
|
| 430 | if (signal_pending(current)) {
|
| 431 | read_done = sock_intr_errno(timeo);
|
| 432 | break;
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | if (!smc_rx_data_available(conn, peeked_bytes)) {
|
| 437 | smc_rx_wait(smc, &timeo, peeked_bytes, smc_rx_data_available);
|
| 438 | continue;
|
| 439 | }
|
| 440 |
|
| 441 | copy:
|
| 442 |
|
| 443 |
|
| 444 | readable = smc_rx_data_available(conn, peeked_bytes);
|
| 445 | splbytes = atomic_read(&conn->splice_pending);
|
| 446 | if (!readable || (msg && splbytes)) {
|
| 447 | if (splbytes)
|
| 448 | func = smc_rx_data_available_and_no_splice_pend;
|
| 449 | else
|
| 450 | func = smc_rx_data_available;
|
| 451 | smc_rx_wait(smc, &timeo, peeked_bytes, func);
|
| 452 | continue;
|
| 453 | }
|
| 454 |
|
| 455 | smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
|
| 456 | if ((flags & MSG_PEEK) && peeked_bytes)
|
| 457 | smc_curs_add(conn->rmb_desc->len, &cons, peeked_bytes);
|
| 458 |
|
| 459 | if (splbytes)
|
| 460 | smc_curs_add(conn->rmb_desc->len, &cons, splbytes);
|
| 461 | if (conn->urg_state == SMC_URG_VALID &&
|
| 462 | sock_flag(&smc->sk, SOCK_URGINLINE) &&
|
| 463 | readable > 1)
|
| 464 | readable--;
|
| 465 |
|
| 466 | copylen = min_t(size_t, read_remaining, readable);
|
| 467 |
|
| 468 |
|
| 469 | chunk_len = min_t(size_t, copylen, conn->rmb_desc->len -
|
| 470 | cons.count);
|
| 471 | chunk_len_sum = chunk_len;
|
| 472 | chunk_off = cons.count;
|
| 473 | smc_rmb_sync_sg_for_cpu(conn);
|
| 474 | for (chunk = 0; chunk < 2; chunk++) {
|
| 475 | if (!(flags & MSG_TRUNC)) {
|
| 476 | if (msg) {
|
| 477 | rc = memcpy_to_msg(msg, rcvbuf_base +
|
| 478 | chunk_off,
|
| 479 | chunk_len);
|
| 480 | } else {
|
| 481 | rc = smc_rx_splice(pipe, rcvbuf_base +
|
| 482 | chunk_off, chunk_len,
|
| 483 | smc);
|
| 484 | }
|
| 485 | if (rc < 0) {
|
| 486 | if (!read_done)
|
| 487 | read_done = -EFAULT;
|
| 488 | goto out;
|
| 489 | }
|
| 490 | }
|
| 491 | read_remaining -= chunk_len;
|
| 492 | read_done += chunk_len;
|
| 493 | if (flags & MSG_PEEK)
|
| 494 | peeked_bytes += chunk_len;
|
| 495 |
|
| 496 | if (chunk_len_sum == copylen)
|
| 497 | break;
|
| 498 |
|
| 499 | chunk_len = copylen - chunk_len;
|
| 500 | chunk_len_sum += chunk_len;
|
| 501 | chunk_off = 0;
|
| 502 | }
|
| 503 |
|
| 504 |
|
| 505 | if (!(flags & MSG_PEEK)) {
|
| 506 |
|
| 507 | smp_mb__before_atomic();
|
| 508 | atomic_sub(copylen, &conn->bytes_to_rcv);
|
| 509 |
|
| 510 | smp_mb__after_atomic();
|
| 511 | if (msg && smc_rx_update_consumer(smc, cons, copylen))
|
| 512 | goto out;
|
| 513 | }
|
| 514 |
|
| 515 | trace_smc_rx_recvmsg(smc, copylen);
|
| 516 | } while (read_remaining);
|
| 517 | out:
|
| 518 | return read_done;
|
| 519 | }
|
| 520 |
|
| 521 |
|
| 522 | void smc_rx_init(struct smc_sock *smc)
|
| 523 | {
|
| 524 | smc->sk.sk_data_ready = smc_rx_wake_up;
|
| 525 | atomic_set(&smc->conn.splice_pending, 0);
|
| 526 | smc->conn.urg_state = SMC_URG_READ;
|
| 527 | }
|