xref: /linux/net/smc/smc_rx.c (revision 453a4a5f97f0c95b7df458e6afb98d4ab057d90b) !
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  */
smc_rx_wake_up(struct sock * sk)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  */
smc_rx_update_consumer(struct smc_sock * smc,union smc_host_cursor cons,size_t len)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 
smc_rx_update_cons(struct smc_sock * smc,size_t len)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 
smc_rx_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)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 
smc_rx_pipe_buf_get(struct pipe_inode_info * pipe,struct pipe_buffer * buf)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 
smc_rx_spd_release(struct splice_pipe_desc * spd,unsigned int i)150 static void smc_rx_spd_release(struct splice_pipe_desc *spd,
151 			       unsigned int i)
152 {
153 	put_page(spd->pages[i]);
154 }
155 
smc_rx_splice(struct pipe_inode_info * pipe,char * src,size_t len,struct smc_sock * smc)156 static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
157 			 struct smc_sock *smc)
158 {
159 	struct smc_link_group *lgr = smc->conn.lgr;
160 	int offset = offset_in_page(src);
161 	struct partial_page *partial;
162 	struct splice_pipe_desc spd;
163 	struct smc_spd_priv **priv;
164 	struct page **pages;
165 	int bytes, nr_pages;
166 	int i;
167 
168 	nr_pages = !lgr->is_smcd && smc->conn.rmb_desc->is_vm ?
169 		   PAGE_ALIGN(len + offset) / PAGE_SIZE : 1;
170 
171 	pages = kzalloc_objs(*pages, nr_pages);
172 	if (!pages)
173 		goto out;
174 	partial = kzalloc_objs(*partial, nr_pages);
175 	if (!partial)
176 		goto out_page;
177 	priv = kzalloc_objs(*priv, nr_pages);
178 	if (!priv)
179 		goto out_part;
180 	for (i = 0; i < nr_pages; i++) {
181 		priv[i] = kzalloc_obj(**priv);
182 		if (!priv[i])
183 			goto out_priv;
184 	}
185 
186 	if (lgr->is_smcd ||
187 	    (!lgr->is_smcd && !smc->conn.rmb_desc->is_vm)) {
188 		/* smcd or smcr that uses physically contiguous RMBs */
189 		priv[0]->len = len;
190 		priv[0]->smc = smc;
191 		partial[0].offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
192 		partial[0].len = len;
193 		partial[0].private = (unsigned long)priv[0];
194 		pages[0] = smc->conn.rmb_desc->pages;
195 	} else {
196 		int size, left = len;
197 		void *buf = src;
198 		/* smcr that uses virtually contiguous RMBs*/
199 		for (i = 0; i < nr_pages; i++) {
200 			size = min_t(int, PAGE_SIZE - offset, left);
201 			priv[i]->len = size;
202 			priv[i]->smc = smc;
203 			pages[i] = vmalloc_to_page(buf);
204 			partial[i].offset = offset;
205 			partial[i].len = size;
206 			partial[i].private = (unsigned long)priv[i];
207 			buf += size;
208 			left -= size;
209 			offset = 0;
210 		}
211 	}
212 	spd.nr_pages_max = nr_pages;
213 	spd.nr_pages = nr_pages;
214 	spd.pages = pages;
215 	spd.partial = partial;
216 	spd.ops = &smc_pipe_ops;
217 	spd.spd_release = smc_rx_spd_release;
218 
219 	bytes = splice_to_pipe(pipe, &spd);
220 	if (bytes > 0) {
221 		sock_hold(&smc->sk);
222 		if (!lgr->is_smcd && smc->conn.rmb_desc->is_vm) {
223 			for (i = 0; i < PAGE_ALIGN(bytes + offset) / PAGE_SIZE; i++)
224 				get_page(pages[i]);
225 		} else {
226 			get_page(smc->conn.rmb_desc->pages);
227 		}
228 		atomic_add(bytes, &smc->conn.splice_pending);
229 	}
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 
smc_rx_data_available_and_no_splice_pend(struct smc_connection * conn,size_t peeked)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 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
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  */
smc_rx_wait(struct smc_sock * smc,long * timeo,size_t peeked,int (* fcrit)(struct smc_connection * conn,size_t baseline))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 
smc_rx_recv_urg(struct smc_sock * smc,struct msghdr * msg,int len,int flags)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 			/* Urgent Byte was already accounted for, but trigger
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 
smc_rx_recvmsg_data_available(struct smc_sock * smc,size_t peeked)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 		/* we received a single urgent Byte - skip */
341 		smc_rx_update_cons(smc, 0);
342 	return false;
343 }
344 
345 /* smc_rx_recvmsg - receive data from RMBE
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  */
smc_rx_recvmsg(struct smc_sock * smc,struct msghdr * msg,struct pipe_inode_info * pipe,size_t len,int flags)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;		/* Read at least these many bytes */
366 	int rc;
367 
368 	if (unlikely(flags & MSG_ERRQUEUE))
369 		return -EINVAL; /* future work for sk.sk_family == AF_SMC */
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 	/* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
386 	rcvbuf_base = conn->rx_off + conn->rmb_desc->cpu_addr;
387 
388 	do { /* while (read_remaining) */
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 			/* smc_cdc_msg_recv_action() could have run after
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 					/* This occurs when user tries to read
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 		/* initialize variables for 1st iteration of subsequent loop */
443 		/* could be just 1 byte, even after waiting on data above */
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 		/* subsequent splice() calls pick up where previous left */
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--;	/* always stop at urgent Byte */
465 		/* not more than what user space asked for */
466 		copylen = min_t(size_t, read_remaining, readable);
467 		/* determine chunks where to read from rcvbuf */
468 		/* either unwrapped case, or 1st chunk of wrapped case */
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; /* either on 1st or 2nd iteration */
498 			/* prepare next (== 2nd) iteration */
499 			chunk_len = copylen - chunk_len; /* remainder */
500 			chunk_len_sum += chunk_len;
501 			chunk_off = 0; /* modulo offset in recv ring buffer */
502 		}
503 
504 		/* update cursors */
505 		if (!(flags & MSG_PEEK)) {
506 			/* increased in recv tasklet smc_cdc_msg_rcv() */
507 			smp_mb__before_atomic();
508 			atomic_sub(copylen, &conn->bytes_to_rcv);
509 			/* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
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 /* Initialize receive properties on connection establishment. NB: not __init! */
smc_rx_init(struct smc_sock * smc)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 }
528