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