blob: b329803c8339dab65ab2112cb7a1971931286075 [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 Raspl9014db22018-05-03 18:12:39 +020085 smc_curs_write(&conn->local_tx_ctrl.cons, smc_curs_read(&cons, conn),
86 conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +020087
Stefan Raspl9014db22018-05-03 18:12:39 +020088 /* send consumer cursor update if required */
89 /* similar to advertising new TCP rcv_wnd if required */
Stefan Rasplde8474e2018-05-23 16:38:11 +020090 smc_tx_consumer_update(conn, force);
91
92 return rc;
93}
94
95static void smc_rx_update_cons(struct smc_sock *smc, size_t len)
96{
97 struct smc_connection *conn = &smc->conn;
98 union smc_host_cursor cons;
99
100 smc_curs_write(&cons, smc_curs_read(&conn->local_tx_ctrl.cons, conn),
101 conn);
102 smc_rx_update_consumer(smc, cons, len);
Stefan Raspl9014db22018-05-03 18:12:39 +0200103}
104
105struct smc_spd_priv {
106 struct smc_sock *smc;
107 size_t len;
108};
109
110static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe,
111 struct pipe_buffer *buf)
112{
113 struct smc_spd_priv *priv = (struct smc_spd_priv *)buf->private;
114 struct smc_sock *smc = priv->smc;
115 struct smc_connection *conn;
Stefan Raspl9014db22018-05-03 18:12:39 +0200116 struct sock *sk = &smc->sk;
117
118 if (sk->sk_state == SMC_CLOSED ||
119 sk->sk_state == SMC_PEERFINCLOSEWAIT ||
120 sk->sk_state == SMC_APPFINCLOSEWAIT)
121 goto out;
122 conn = &smc->conn;
123 lock_sock(sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200124 smc_rx_update_cons(smc, priv->len);
Stefan Raspl9014db22018-05-03 18:12:39 +0200125 release_sock(sk);
126 if (atomic_sub_and_test(priv->len, &conn->splice_pending))
127 smc_rx_wake_up(sk);
128out:
129 kfree(priv);
130 put_page(buf->page);
131 sock_put(sk);
132}
133
134static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info *pipe,
135 struct pipe_buffer *buf)
136{
137 return 1;
138}
139
140static const struct pipe_buf_operations smc_pipe_ops = {
141 .can_merge = 0,
142 .confirm = generic_pipe_buf_confirm,
143 .release = smc_rx_pipe_buf_release,
144 .steal = smc_rx_pipe_buf_nosteal,
145 .get = generic_pipe_buf_get
146};
147
148static void smc_rx_spd_release(struct splice_pipe_desc *spd,
149 unsigned int i)
150{
151 put_page(spd->pages[i]);
152}
153
154static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
155 struct smc_sock *smc)
156{
157 struct splice_pipe_desc spd;
158 struct partial_page partial;
159 struct smc_spd_priv *priv;
160 struct page *page;
161 int bytes;
162
163 page = virt_to_page(smc->conn.rmb_desc->cpu_addr);
164 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
165 if (!priv)
166 return -ENOMEM;
167 priv->len = len;
168 priv->smc = smc;
169 partial.offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
170 partial.len = len;
171 partial.private = (unsigned long)priv;
172
173 spd.nr_pages_max = 1;
174 spd.nr_pages = 1;
175 spd.pages = &page;
176 spd.partial = &partial;
177 spd.ops = &smc_pipe_ops;
178 spd.spd_release = smc_rx_spd_release;
179
180 bytes = splice_to_pipe(pipe, &spd);
181 if (bytes > 0) {
182 sock_hold(&smc->sk);
183 get_page(smc->conn.rmb_desc->pages);
184 atomic_add(bytes, &smc->conn.splice_pending);
185 }
186
187 return bytes;
188}
189
190static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn)
191{
192 return atomic_read(&conn->bytes_to_rcv) &&
193 !atomic_read(&conn->splice_pending);
194}
195
Ursula Braun952310c2017-01-09 16:55:24 +0100196/* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
197 * @smc smc socket
198 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200199 * @fcrit add'l criterion to evaluate as function pointer
Ursula Braun952310c2017-01-09 16:55:24 +0100200 * Returns:
201 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
202 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
203 */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200204int smc_rx_wait(struct smc_sock *smc, long *timeo,
205 int (*fcrit)(struct smc_connection *conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100206{
207 DEFINE_WAIT_FUNC(wait, woken_wake_function);
208 struct smc_connection *conn = &smc->conn;
209 struct sock *sk = &smc->sk;
210 int rc;
211
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200212 if (fcrit(conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100213 return 1;
214 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
215 add_wait_queue(sk_sleep(sk), &wait);
216 rc = sk_wait_event(sk, timeo,
217 sk->sk_err ||
218 sk->sk_shutdown & RCV_SHUTDOWN ||
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200219 fcrit(conn) ||
Ursula Braun952310c2017-01-09 16:55:24 +0100220 smc_cdc_rxed_any_close_or_senddone(conn),
221 &wait);
222 remove_wait_queue(sk_sleep(sk), &wait);
223 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
224 return rc;
225}
226
Stefan Rasplde8474e2018-05-23 16:38:11 +0200227static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len,
228 int flags)
229{
230 struct smc_connection *conn = &smc->conn;
231 union smc_host_cursor cons;
232 struct sock *sk = &smc->sk;
233 int rc = 0;
234
235 if (sock_flag(sk, SOCK_URGINLINE) ||
236 !(conn->urg_state == SMC_URG_VALID) ||
237 conn->urg_state == SMC_URG_READ)
238 return -EINVAL;
239
240 if (conn->urg_state == SMC_URG_VALID) {
241 if (!(flags & MSG_PEEK))
242 smc->conn.urg_state = SMC_URG_READ;
243 msg->msg_flags |= MSG_OOB;
244 if (len > 0) {
245 if (!(flags & MSG_TRUNC))
246 rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1);
247 len = 1;
248 smc_curs_write(&cons,
249 smc_curs_read(&conn->local_tx_ctrl.cons,
250 conn),
251 conn);
252 if (smc_curs_diff(conn->rmb_desc->len, &cons,
253 &conn->urg_curs) > 1)
254 conn->urg_rx_skip_pend = true;
255 /* Urgent Byte was already accounted for, but trigger
256 * skipping the urgent byte in non-inline case
257 */
258 if (!(flags & MSG_PEEK))
259 smc_rx_update_consumer(smc, cons, 0);
260 } else {
261 msg->msg_flags |= MSG_TRUNC;
262 }
263
264 return rc ? -EFAULT : len;
265 }
266
267 if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN)
268 return 0;
269
270 return -EAGAIN;
271}
272
Stefan Raspl9014db22018-05-03 18:12:39 +0200273/* smc_rx_recvmsg - receive data from RMBE
274 * @msg: copy data to receive buffer
275 * @pipe: copy data to pipe if set - indicates splice() call
276 *
277 * rcvbuf consumer: main API called by socket layer.
278 * Called under sk lock.
Ursula Braun952310c2017-01-09 16:55:24 +0100279 */
Stefan Raspl9014db22018-05-03 18:12:39 +0200280int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
281 struct pipe_inode_info *pipe, size_t len, int flags)
Ursula Braun952310c2017-01-09 16:55:24 +0100282{
283 size_t copylen, read_done = 0, read_remaining = len;
284 size_t chunk_len, chunk_off, chunk_len_sum;
285 struct smc_connection *conn = &smc->conn;
Stefan Raspl9014db22018-05-03 18:12:39 +0200286 int (*func)(struct smc_connection *conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100287 union smc_host_cursor cons;
288 int readable, chunk;
289 char *rcvbuf_base;
290 struct sock *sk;
Stefan Raspl9014db22018-05-03 18:12:39 +0200291 int splbytes;
Ursula Braun952310c2017-01-09 16:55:24 +0100292 long timeo;
293 int target; /* Read at least these many bytes */
294 int rc;
295
296 if (unlikely(flags & MSG_ERRQUEUE))
297 return -EINVAL; /* future work for sk.sk_family == AF_SMC */
Ursula Braun952310c2017-01-09 16:55:24 +0100298
299 sk = &smc->sk;
300 if (sk->sk_state == SMC_LISTEN)
301 return -ENOTCONN;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200302 if (flags & MSG_OOB)
303 return smc_rx_recv_urg(smc, msg, len, flags);
Ursula Braun952310c2017-01-09 16:55:24 +0100304 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
305 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
306
Ursula Braun952310c2017-01-09 16:55:24 +0100307 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
Hans Wippelbe244f22018-06-28 19:05:10 +0200308 rcvbuf_base = conn->rx_off + conn->rmb_desc->cpu_addr;
Ursula Braun952310c2017-01-09 16:55:24 +0100309
310 do { /* while (read_remaining) */
Stefan Raspl9014db22018-05-03 18:12:39 +0200311 if (read_done >= target || (pipe && read_done))
Ursula Braun952310c2017-01-09 16:55:24 +0100312 break;
313
314 if (atomic_read(&conn->bytes_to_rcv))
315 goto copy;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200316 else if (conn->urg_state == SMC_URG_VALID)
317 /* we received a single urgent Byte - skip */
318 smc_rx_update_cons(smc, 0);
Ursula Braun952310c2017-01-09 16:55:24 +0100319
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200320 if (sk->sk_shutdown & RCV_SHUTDOWN ||
321 smc_cdc_rxed_any_close_or_senddone(conn) ||
322 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort)
323 break;
324
Ursula Braun952310c2017-01-09 16:55:24 +0100325 if (read_done) {
326 if (sk->sk_err ||
327 sk->sk_state == SMC_CLOSED ||
Ursula Braun952310c2017-01-09 16:55:24 +0100328 !timeo ||
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200329 signal_pending(current))
Ursula Braun952310c2017-01-09 16:55:24 +0100330 break;
331 } else {
Ursula Braun952310c2017-01-09 16:55:24 +0100332 if (sk->sk_err) {
333 read_done = sock_error(sk);
334 break;
335 }
Ursula Braun952310c2017-01-09 16:55:24 +0100336 if (sk->sk_state == SMC_CLOSED) {
337 if (!sock_flag(sk, SOCK_DONE)) {
338 /* This occurs when user tries to read
339 * from never connected socket.
340 */
341 read_done = -ENOTCONN;
342 break;
343 }
344 break;
345 }
346 if (signal_pending(current)) {
347 read_done = sock_intr_errno(timeo);
348 break;
349 }
Hans Wippel846e3442017-09-21 09:16:27 +0200350 if (!timeo)
351 return -EAGAIN;
Ursula Braun952310c2017-01-09 16:55:24 +0100352 }
353
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200354 if (!smc_rx_data_available(conn)) {
355 smc_rx_wait(smc, &timeo, smc_rx_data_available);
Ursula Braun952310c2017-01-09 16:55:24 +0100356 continue;
357 }
358
359copy:
360 /* initialize variables for 1st iteration of subsequent loop */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200361 /* could be just 1 byte, even after waiting on data above */
Ursula Braun952310c2017-01-09 16:55:24 +0100362 readable = atomic_read(&conn->bytes_to_rcv);
Stefan Raspl9014db22018-05-03 18:12:39 +0200363 splbytes = atomic_read(&conn->splice_pending);
364 if (!readable || (msg && splbytes)) {
365 if (splbytes)
366 func = smc_rx_data_available_and_no_splice_pend;
367 else
368 func = smc_rx_data_available;
369 smc_rx_wait(smc, &timeo, func);
370 continue;
371 }
372
Ursula Braun952310c2017-01-09 16:55:24 +0100373 smc_curs_write(&cons,
374 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
375 conn);
Stefan Raspl9014db22018-05-03 18:12:39 +0200376 /* subsequent splice() calls pick up where previous left */
377 if (splbytes)
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200378 smc_curs_add(conn->rmb_desc->len, &cons, splbytes);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200379 if (conn->urg_state == SMC_URG_VALID &&
380 sock_flag(&smc->sk, SOCK_URGINLINE) &&
381 readable > 1)
382 readable--; /* always stop at urgent Byte */
383 /* not more than what user space asked for */
384 copylen = min_t(size_t, read_remaining, readable);
Ursula Braun952310c2017-01-09 16:55:24 +0100385 /* determine chunks where to read from rcvbuf */
386 /* either unwrapped case, or 1st chunk of wrapped case */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200387 chunk_len = min_t(size_t, copylen, conn->rmb_desc->len -
388 cons.count);
Ursula Braun952310c2017-01-09 16:55:24 +0100389 chunk_len_sum = chunk_len;
390 chunk_off = cons.count;
Ursula Braun10428dd2017-07-28 13:56:22 +0200391 smc_rmb_sync_sg_for_cpu(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100392 for (chunk = 0; chunk < 2; chunk++) {
393 if (!(flags & MSG_TRUNC)) {
Stefan Raspl9014db22018-05-03 18:12:39 +0200394 if (msg) {
395 rc = memcpy_to_msg(msg, rcvbuf_base +
396 chunk_off,
397 chunk_len);
398 } else {
399 rc = smc_rx_splice(pipe, rcvbuf_base +
400 chunk_off, chunk_len,
401 smc);
402 }
403 if (rc < 0) {
Ursula Braun952310c2017-01-09 16:55:24 +0100404 if (!read_done)
405 read_done = -EFAULT;
Ursula Braun10428dd2017-07-28 13:56:22 +0200406 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100407 goto out;
408 }
409 }
410 read_remaining -= chunk_len;
411 read_done += chunk_len;
412
413 if (chunk_len_sum == copylen)
414 break; /* either on 1st or 2nd iteration */
415 /* prepare next (== 2nd) iteration */
416 chunk_len = copylen - chunk_len; /* remainder */
417 chunk_len_sum += chunk_len;
418 chunk_off = 0; /* modulo offset in recv ring buffer */
419 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200420 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100421
422 /* update cursors */
423 if (!(flags & MSG_PEEK)) {
Ursula Braun952310c2017-01-09 16:55:24 +0100424 /* increased in recv tasklet smc_cdc_msg_rcv() */
425 smp_mb__before_atomic();
426 atomic_sub(copylen, &conn->bytes_to_rcv);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200427 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
Ursula Braun952310c2017-01-09 16:55:24 +0100428 smp_mb__after_atomic();
Stefan Rasplde8474e2018-05-23 16:38:11 +0200429 if (msg && smc_rx_update_consumer(smc, cons, copylen))
430 goto out;
Ursula Braun952310c2017-01-09 16:55:24 +0100431 }
432 } while (read_remaining);
433out:
434 return read_done;
435}
436
437/* Initialize receive properties on connection establishment. NB: not __init! */
438void smc_rx_init(struct smc_sock *smc)
439{
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200440 smc->sk.sk_data_ready = smc_rx_wake_up;
Stefan Raspl9014db22018-05-03 18:12:39 +0200441 atomic_set(&smc->conn.splice_pending, 0);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200442 smc->conn.urg_state = SMC_URG_READ;
Ursula Braun952310c2017-01-09 16:55:24 +0100443}