blob: fcfac59f8b7280e81836a703a010f3a5cd541f3a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braun952310c2017-01-09 16:55:24 +01002/*
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>
Ingo Molnarc3edc402017-02-02 08:35:14 +010015#include <linux/sched/signal.h>
16
Ursula Braun952310c2017-01-09 16:55:24 +010017#include <net/sock.h>
18
19#include "smc.h"
20#include "smc_core.h"
21#include "smc_cdc.h"
22#include "smc_tx.h" /* smc_tx_consumer_update() */
23#include "smc_rx.h"
24
Stefan Rasplb51fa1b2018-05-03 18:12:37 +020025/* callback implementation to wakeup consumers blocked with smc_rx_wait().
Ursula Braun952310c2017-01-09 16:55:24 +010026 * indirectly called by smc_cdc_msg_recv_action().
27 */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +020028static void smc_rx_wake_up(struct sock *sk)
Ursula Braun952310c2017-01-09 16:55:24 +010029{
30 struct socket_wq *wq;
31
32 /* derived from sock_def_readable() */
33 /* called already in smc_listen_work() */
34 rcu_read_lock();
35 wq = rcu_dereference(sk->sk_wq);
36 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -080037 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
38 EPOLLRDNORM | EPOLLRDBAND);
Ursula Braun90e9517e2017-04-10 14:58:00 +020039 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Ursula Braun952310c2017-01-09 16:55:24 +010040 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
41 (sk->sk_state == SMC_CLOSED))
42 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Ursula Braun952310c2017-01-09 16:55:24 +010043 rcu_read_unlock();
44}
45
Stefan Raspl9014db22018-05-03 18:12:39 +020046/* Update consumer cursor
47 * @conn connection to update
48 * @cons consumer cursor
49 * @len number of Bytes consumed
Stefan Rasplde8474e2018-05-23 16:38:11 +020050 * Returns:
51 * 1 if we should end our receive, 0 otherwise
Stefan Raspl9014db22018-05-03 18:12:39 +020052 */
Stefan Rasplde8474e2018-05-23 16:38:11 +020053static int smc_rx_update_consumer(struct smc_sock *smc,
54 union smc_host_cursor cons, size_t len)
Stefan Raspl9014db22018-05-03 18:12:39 +020055{
Stefan Rasplde8474e2018-05-23 16:38:11 +020056 struct smc_connection *conn = &smc->conn;
57 struct sock *sk = &smc->sk;
58 bool force = false;
59 int diff, rc = 0;
60
Hans Wippel69cb7dc2018-05-18 09:34:10 +020061 smc_curs_add(conn->rmb_desc->len, &cons, len);
Stefan Rasplde8474e2018-05-23 16:38:11 +020062
63 /* did we process urgent data? */
64 if (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) {
65 diff = smc_curs_comp(conn->rmb_desc->len, &cons,
66 &conn->urg_curs);
67 if (sock_flag(sk, SOCK_URGINLINE)) {
68 if (diff == 0) {
69 force = true;
70 rc = 1;
71 conn->urg_state = SMC_URG_READ;
72 }
73 } else {
74 if (diff == 1) {
75 /* skip urgent byte */
76 force = true;
77 smc_curs_add(conn->rmb_desc->len, &cons, 1);
78 conn->urg_rx_skip_pend = false;
79 } else if (diff < -1)
80 /* we read past urgent byte */
81 conn->urg_state = SMC_URG_READ;
82 }
83 }
84
Stefan Rasplbac6de72018-07-23 13:53:09 +020085 smc_curs_copy(&conn->local_tx_ctrl.cons, &cons, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +020086
Stefan Raspl9014db22018-05-03 18:12:39 +020087 /* send consumer cursor update if required */
88 /* similar to advertising new TCP rcv_wnd if required */
Stefan Rasplde8474e2018-05-23 16:38:11 +020089 smc_tx_consumer_update(conn, force);
90
91 return rc;
92}
93
94static void smc_rx_update_cons(struct smc_sock *smc, size_t len)
95{
96 struct smc_connection *conn = &smc->conn;
97 union smc_host_cursor cons;
98
Stefan Rasplbac6de72018-07-23 13:53:09 +020099 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200100 smc_rx_update_consumer(smc, cons, len);
Stefan Raspl9014db22018-05-03 18:12:39 +0200101}
102
103struct smc_spd_priv {
104 struct smc_sock *smc;
105 size_t len;
106};
107
108static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe,
109 struct pipe_buffer *buf)
110{
111 struct smc_spd_priv *priv = (struct smc_spd_priv *)buf->private;
112 struct smc_sock *smc = priv->smc;
113 struct smc_connection *conn;
Stefan Raspl9014db22018-05-03 18:12:39 +0200114 struct sock *sk = &smc->sk;
115
116 if (sk->sk_state == SMC_CLOSED ||
117 sk->sk_state == SMC_PEERFINCLOSEWAIT ||
118 sk->sk_state == SMC_APPFINCLOSEWAIT)
119 goto out;
120 conn = &smc->conn;
121 lock_sock(sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200122 smc_rx_update_cons(smc, priv->len);
Stefan Raspl9014db22018-05-03 18:12:39 +0200123 release_sock(sk);
124 if (atomic_sub_and_test(priv->len, &conn->splice_pending))
125 smc_rx_wake_up(sk);
126out:
127 kfree(priv);
128 put_page(buf->page);
129 sock_put(sk);
130}
131
Stefan Raspl9014db22018-05-03 18:12:39 +0200132static const struct pipe_buf_operations smc_pipe_ops = {
Stefan Raspl9014db22018-05-03 18:12:39 +0200133 .release = smc_rx_pipe_buf_release,
Stefan Raspl9014db22018-05-03 18:12:39 +0200134 .get = generic_pipe_buf_get
135};
136
137static void smc_rx_spd_release(struct splice_pipe_desc *spd,
138 unsigned int i)
139{
140 put_page(spd->pages[i]);
141}
142
143static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
144 struct smc_sock *smc)
145{
146 struct splice_pipe_desc spd;
147 struct partial_page partial;
148 struct smc_spd_priv *priv;
Stefan Raspl9014db22018-05-03 18:12:39 +0200149 int bytes;
150
Stefan Raspl9014db22018-05-03 18:12:39 +0200151 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
152 if (!priv)
153 return -ENOMEM;
154 priv->len = len;
155 priv->smc = smc;
156 partial.offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
157 partial.len = len;
158 partial.private = (unsigned long)priv;
159
160 spd.nr_pages_max = 1;
161 spd.nr_pages = 1;
Ursula Braun48bf5232018-07-23 13:53:12 +0200162 spd.pages = &smc->conn.rmb_desc->pages;
Stefan Raspl9014db22018-05-03 18:12:39 +0200163 spd.partial = &partial;
164 spd.ops = &smc_pipe_ops;
165 spd.spd_release = smc_rx_spd_release;
166
167 bytes = splice_to_pipe(pipe, &spd);
168 if (bytes > 0) {
169 sock_hold(&smc->sk);
170 get_page(smc->conn.rmb_desc->pages);
171 atomic_add(bytes, &smc->conn.splice_pending);
172 }
173
174 return bytes;
175}
176
177static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn)
178{
179 return atomic_read(&conn->bytes_to_rcv) &&
180 !atomic_read(&conn->splice_pending);
181}
182
Ursula Braun952310c2017-01-09 16:55:24 +0100183/* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
184 * @smc smc socket
185 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200186 * @fcrit add'l criterion to evaluate as function pointer
Ursula Braun952310c2017-01-09 16:55:24 +0100187 * Returns:
188 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
189 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
190 */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200191int smc_rx_wait(struct smc_sock *smc, long *timeo,
192 int (*fcrit)(struct smc_connection *conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100193{
194 DEFINE_WAIT_FUNC(wait, woken_wake_function);
195 struct smc_connection *conn = &smc->conn;
Ursula Braunb2900982019-10-21 16:13:08 +0200196 struct smc_cdc_conn_state_flags *cflags =
197 &conn->local_tx_ctrl.conn_state_flags;
Ursula Braun952310c2017-01-09 16:55:24 +0100198 struct sock *sk = &smc->sk;
199 int rc;
200
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200201 if (fcrit(conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100202 return 1;
203 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
204 add_wait_queue(sk_sleep(sk), &wait);
205 rc = sk_wait_event(sk, timeo,
206 sk->sk_err ||
Ursula Braunb2900982019-10-21 16:13:08 +0200207 cflags->peer_conn_abort ||
Ursula Braun952310c2017-01-09 16:55:24 +0100208 sk->sk_shutdown & RCV_SHUTDOWN ||
Ursula Braunb2900982019-10-21 16:13:08 +0200209 conn->killed ||
Karsten Graul882dcfe2019-10-10 10:16:10 +0200210 fcrit(conn),
Ursula Braun952310c2017-01-09 16:55:24 +0100211 &wait);
212 remove_wait_queue(sk_sleep(sk), &wait);
213 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
214 return rc;
215}
216
Stefan Rasplde8474e2018-05-23 16:38:11 +0200217static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len,
218 int flags)
219{
220 struct smc_connection *conn = &smc->conn;
221 union smc_host_cursor cons;
222 struct sock *sk = &smc->sk;
223 int rc = 0;
224
225 if (sock_flag(sk, SOCK_URGINLINE) ||
226 !(conn->urg_state == SMC_URG_VALID) ||
227 conn->urg_state == SMC_URG_READ)
228 return -EINVAL;
229
230 if (conn->urg_state == SMC_URG_VALID) {
231 if (!(flags & MSG_PEEK))
232 smc->conn.urg_state = SMC_URG_READ;
233 msg->msg_flags |= MSG_OOB;
234 if (len > 0) {
235 if (!(flags & MSG_TRUNC))
236 rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1);
237 len = 1;
Stefan Rasplbac6de72018-07-23 13:53:09 +0200238 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200239 if (smc_curs_diff(conn->rmb_desc->len, &cons,
240 &conn->urg_curs) > 1)
241 conn->urg_rx_skip_pend = true;
242 /* Urgent Byte was already accounted for, but trigger
243 * skipping the urgent byte in non-inline case
244 */
245 if (!(flags & MSG_PEEK))
246 smc_rx_update_consumer(smc, cons, 0);
247 } else {
248 msg->msg_flags |= MSG_TRUNC;
249 }
250
251 return rc ? -EFAULT : len;
252 }
253
254 if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN)
255 return 0;
256
257 return -EAGAIN;
258}
259
Karsten Graul107529e2019-10-10 10:16:11 +0200260static bool smc_rx_recvmsg_data_available(struct smc_sock *smc)
261{
262 struct smc_connection *conn = &smc->conn;
263
264 if (smc_rx_data_available(conn))
265 return true;
266 else if (conn->urg_state == SMC_URG_VALID)
267 /* we received a single urgent Byte - skip */
268 smc_rx_update_cons(smc, 0);
269 return false;
270}
271
Stefan Raspl9014db22018-05-03 18:12:39 +0200272/* smc_rx_recvmsg - receive data from RMBE
273 * @msg: copy data to receive buffer
274 * @pipe: copy data to pipe if set - indicates splice() call
275 *
276 * rcvbuf consumer: main API called by socket layer.
277 * Called under sk lock.
Ursula Braun952310c2017-01-09 16:55:24 +0100278 */
Stefan Raspl9014db22018-05-03 18:12:39 +0200279int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
280 struct pipe_inode_info *pipe, size_t len, int flags)
Ursula Braun952310c2017-01-09 16:55:24 +0100281{
282 size_t copylen, read_done = 0, read_remaining = len;
283 size_t chunk_len, chunk_off, chunk_len_sum;
284 struct smc_connection *conn = &smc->conn;
Stefan Raspl9014db22018-05-03 18:12:39 +0200285 int (*func)(struct smc_connection *conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100286 union smc_host_cursor cons;
287 int readable, chunk;
288 char *rcvbuf_base;
289 struct sock *sk;
Stefan Raspl9014db22018-05-03 18:12:39 +0200290 int splbytes;
Ursula Braun952310c2017-01-09 16:55:24 +0100291 long timeo;
292 int target; /* Read at least these many bytes */
293 int rc;
294
295 if (unlikely(flags & MSG_ERRQUEUE))
296 return -EINVAL; /* future work for sk.sk_family == AF_SMC */
Ursula Braun952310c2017-01-09 16:55:24 +0100297
298 sk = &smc->sk;
299 if (sk->sk_state == SMC_LISTEN)
300 return -ENOTCONN;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200301 if (flags & MSG_OOB)
302 return smc_rx_recv_urg(smc, msg, len, flags);
Ursula Braun952310c2017-01-09 16:55:24 +0100303 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
304 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
305
Ursula Braun952310c2017-01-09 16:55:24 +0100306 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
Hans Wippelbe244f22018-06-28 19:05:10 +0200307 rcvbuf_base = conn->rx_off + conn->rmb_desc->cpu_addr;
Ursula Braun952310c2017-01-09 16:55:24 +0100308
309 do { /* while (read_remaining) */
Stefan Raspl9014db22018-05-03 18:12:39 +0200310 if (read_done >= target || (pipe && read_done))
Ursula Braun952310c2017-01-09 16:55:24 +0100311 break;
312
Ursula Braunb2900982019-10-21 16:13:08 +0200313 if (conn->killed)
314 break;
315
Karsten Graul107529e2019-10-10 10:16:11 +0200316 if (smc_rx_recvmsg_data_available(smc))
Ursula Braun952310c2017-01-09 16:55:24 +0100317 goto copy;
318
Ursula Braunb2900982019-10-21 16:13:08 +0200319 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Karsten Graul107529e2019-10-10 10:16:11 +0200320 /* smc_cdc_msg_recv_action() could have run after
321 * above smc_rx_recvmsg_data_available()
322 */
323 if (smc_rx_recvmsg_data_available(smc))
324 goto copy;
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200325 break;
Karsten Graul107529e2019-10-10 10:16:11 +0200326 }
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200327
Ursula Braun952310c2017-01-09 16:55:24 +0100328 if (read_done) {
329 if (sk->sk_err ||
330 sk->sk_state == SMC_CLOSED ||
Ursula Braun952310c2017-01-09 16:55:24 +0100331 !timeo ||
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200332 signal_pending(current))
Ursula Braun952310c2017-01-09 16:55:24 +0100333 break;
334 } else {
Ursula Braun952310c2017-01-09 16:55:24 +0100335 if (sk->sk_err) {
336 read_done = sock_error(sk);
337 break;
338 }
Ursula Braun952310c2017-01-09 16:55:24 +0100339 if (sk->sk_state == SMC_CLOSED) {
340 if (!sock_flag(sk, SOCK_DONE)) {
341 /* This occurs when user tries to read
342 * from never connected socket.
343 */
344 read_done = -ENOTCONN;
345 break;
346 }
347 break;
348 }
349 if (signal_pending(current)) {
350 read_done = sock_intr_errno(timeo);
351 break;
352 }
Hans Wippel846e3442017-09-21 09:16:27 +0200353 if (!timeo)
354 return -EAGAIN;
Ursula Braun952310c2017-01-09 16:55:24 +0100355 }
356
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200357 if (!smc_rx_data_available(conn)) {
358 smc_rx_wait(smc, &timeo, smc_rx_data_available);
Ursula Braun952310c2017-01-09 16:55:24 +0100359 continue;
360 }
361
362copy:
363 /* initialize variables for 1st iteration of subsequent loop */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200364 /* could be just 1 byte, even after waiting on data above */
Ursula Braun952310c2017-01-09 16:55:24 +0100365 readable = atomic_read(&conn->bytes_to_rcv);
Stefan Raspl9014db22018-05-03 18:12:39 +0200366 splbytes = atomic_read(&conn->splice_pending);
367 if (!readable || (msg && splbytes)) {
368 if (splbytes)
369 func = smc_rx_data_available_and_no_splice_pend;
370 else
371 func = smc_rx_data_available;
372 smc_rx_wait(smc, &timeo, func);
373 continue;
374 }
375
Stefan Rasplbac6de72018-07-23 13:53:09 +0200376 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
Stefan Raspl9014db22018-05-03 18:12:39 +0200377 /* subsequent splice() calls pick up where previous left */
378 if (splbytes)
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200379 smc_curs_add(conn->rmb_desc->len, &cons, splbytes);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200380 if (conn->urg_state == SMC_URG_VALID &&
381 sock_flag(&smc->sk, SOCK_URGINLINE) &&
382 readable > 1)
383 readable--; /* always stop at urgent Byte */
384 /* not more than what user space asked for */
385 copylen = min_t(size_t, read_remaining, readable);
Ursula Braun952310c2017-01-09 16:55:24 +0100386 /* determine chunks where to read from rcvbuf */
387 /* either unwrapped case, or 1st chunk of wrapped case */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200388 chunk_len = min_t(size_t, copylen, conn->rmb_desc->len -
389 cons.count);
Ursula Braun952310c2017-01-09 16:55:24 +0100390 chunk_len_sum = chunk_len;
391 chunk_off = cons.count;
Ursula Braun10428dd2017-07-28 13:56:22 +0200392 smc_rmb_sync_sg_for_cpu(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100393 for (chunk = 0; chunk < 2; chunk++) {
394 if (!(flags & MSG_TRUNC)) {
Stefan Raspl9014db22018-05-03 18:12:39 +0200395 if (msg) {
396 rc = memcpy_to_msg(msg, rcvbuf_base +
397 chunk_off,
398 chunk_len);
399 } else {
400 rc = smc_rx_splice(pipe, rcvbuf_base +
401 chunk_off, chunk_len,
402 smc);
403 }
404 if (rc < 0) {
Ursula Braun952310c2017-01-09 16:55:24 +0100405 if (!read_done)
406 read_done = -EFAULT;
Ursula Braun10428dd2017-07-28 13:56:22 +0200407 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100408 goto out;
409 }
410 }
411 read_remaining -= chunk_len;
412 read_done += chunk_len;
413
414 if (chunk_len_sum == copylen)
415 break; /* either on 1st or 2nd iteration */
416 /* prepare next (== 2nd) iteration */
417 chunk_len = copylen - chunk_len; /* remainder */
418 chunk_len_sum += chunk_len;
419 chunk_off = 0; /* modulo offset in recv ring buffer */
420 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200421 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100422
423 /* update cursors */
424 if (!(flags & MSG_PEEK)) {
Ursula Braun952310c2017-01-09 16:55:24 +0100425 /* increased in recv tasklet smc_cdc_msg_rcv() */
426 smp_mb__before_atomic();
427 atomic_sub(copylen, &conn->bytes_to_rcv);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200428 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
Ursula Braun952310c2017-01-09 16:55:24 +0100429 smp_mb__after_atomic();
Stefan Rasplde8474e2018-05-23 16:38:11 +0200430 if (msg && smc_rx_update_consumer(smc, cons, copylen))
431 goto out;
Ursula Braun952310c2017-01-09 16:55:24 +0100432 }
433 } while (read_remaining);
434out:
435 return read_done;
436}
437
438/* Initialize receive properties on connection establishment. NB: not __init! */
439void smc_rx_init(struct smc_sock *smc)
440{
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200441 smc->sk.sk_data_ready = smc_rx_wake_up;
Stefan Raspl9014db22018-05-03 18:12:39 +0200442 atomic_set(&smc->conn.splice_pending, 0);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200443 smc->conn.urg_state = SMC_URG_READ;
Ursula Braun952310c2017-01-09 16:55:24 +0100444}