xref: /openbmc/linux/net/smc/smc_rx.c (revision a86854d0)
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 
25 /* callback implementation to wakeup consumers blocked with smc_rx_wait().
26  * indirectly called by smc_cdc_msg_recv_action().
27  */
28 static void smc_rx_wake_up(struct sock *sk)
29 {
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))
37 		wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
38 						EPOLLRDNORM | EPOLLRDBAND);
39 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
40 	if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
41 	    (sk->sk_state == SMC_CLOSED))
42 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
43 	rcu_read_unlock();
44 }
45 
46 /* Update consumer cursor
47  *   @conn   connection to update
48  *   @cons   consumer cursor
49  *   @len    number of Bytes consumed
50  *   Returns:
51  *   1 if we should end our receive, 0 otherwise
52  */
53 static int smc_rx_update_consumer(struct smc_sock *smc,
54 				  union smc_host_cursor cons, size_t len)
55 {
56 	struct smc_connection *conn = &smc->conn;
57 	struct sock *sk = &smc->sk;
58 	bool force = false;
59 	int diff, rc = 0;
60 
61 	smc_curs_add(conn->rmb_desc->len, &cons, len);
62 
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 
85 	smc_curs_write(&conn->local_tx_ctrl.cons, smc_curs_read(&cons, conn),
86 		       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_write(&cons, smc_curs_read(&conn->local_tx_ctrl.cons, conn),
101 		       conn);
102 	smc_rx_update_consumer(smc, cons, len);
103 }
104 
105 struct smc_spd_priv {
106 	struct smc_sock *smc;
107 	size_t		 len;
108 };
109 
110 static 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;
116 	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);
124 	smc_rx_update_cons(smc, priv->len);
125 	release_sock(sk);
126 	if (atomic_sub_and_test(priv->len, &conn->splice_pending))
127 		smc_rx_wake_up(sk);
128 out:
129 	kfree(priv);
130 	put_page(buf->page);
131 	sock_put(sk);
132 }
133 
134 static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info *pipe,
135 				   struct pipe_buffer *buf)
136 {
137 	return 1;
138 }
139 
140 static 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 
148 static void smc_rx_spd_release(struct splice_pipe_desc *spd,
149 			       unsigned int i)
150 {
151 	put_page(spd->pages[i]);
152 }
153 
154 static 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 
190 static 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 
196 /* 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
199  *   @fcrit  add'l criterion to evaluate as function pointer
200  * 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  */
204 int smc_rx_wait(struct smc_sock *smc, long *timeo,
205 		int (*fcrit)(struct smc_connection *conn))
206 {
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 
212 	if (fcrit(conn))
213 		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 ||
219 			   fcrit(conn) ||
220 			   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 
227 static 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 
273 /* 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.
279  */
280 int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
281 		   struct pipe_inode_info *pipe, size_t len, int flags)
282 {
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;
286 	int (*func)(struct smc_connection *conn);
287 	union smc_host_cursor cons;
288 	int readable, chunk;
289 	char *rcvbuf_base;
290 	struct sock *sk;
291 	int splbytes;
292 	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 */
298 
299 	sk = &smc->sk;
300 	if (sk->sk_state == SMC_LISTEN)
301 		return -ENOTCONN;
302 	if (flags & MSG_OOB)
303 		return smc_rx_recv_urg(smc, msg, len, flags);
304 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
305 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
306 
307 	/* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
308 	rcvbuf_base = conn->rmb_desc->cpu_addr;
309 
310 	do { /* while (read_remaining) */
311 		if (read_done >= target || (pipe && read_done))
312 			break;
313 
314 		if (atomic_read(&conn->bytes_to_rcv))
315 			goto copy;
316 		else if (conn->urg_state == SMC_URG_VALID)
317 			/* we received a single urgent Byte - skip */
318 			smc_rx_update_cons(smc, 0);
319 
320 		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 
325 		if (read_done) {
326 			if (sk->sk_err ||
327 			    sk->sk_state == SMC_CLOSED ||
328 			    !timeo ||
329 			    signal_pending(current))
330 				break;
331 		} else {
332 			if (sk->sk_err) {
333 				read_done = sock_error(sk);
334 				break;
335 			}
336 			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 			}
350 			if (!timeo)
351 				return -EAGAIN;
352 		}
353 
354 		if (!smc_rx_data_available(conn)) {
355 			smc_rx_wait(smc, &timeo, smc_rx_data_available);
356 			continue;
357 		}
358 
359 copy:
360 		/* initialize variables for 1st iteration of subsequent loop */
361 		/* could be just 1 byte, even after waiting on data above */
362 		readable = atomic_read(&conn->bytes_to_rcv);
363 		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 
373 		smc_curs_write(&cons,
374 			       smc_curs_read(&conn->local_tx_ctrl.cons, conn),
375 			       conn);
376 		/* subsequent splice() calls pick up where previous left */
377 		if (splbytes)
378 			smc_curs_add(conn->rmb_desc->len, &cons, splbytes);
379 		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);
385 		/* determine chunks where to read from rcvbuf */
386 		/* either unwrapped case, or 1st chunk of wrapped case */
387 		chunk_len = min_t(size_t, copylen, conn->rmb_desc->len -
388 				  cons.count);
389 		chunk_len_sum = chunk_len;
390 		chunk_off = cons.count;
391 		smc_rmb_sync_sg_for_cpu(conn);
392 		for (chunk = 0; chunk < 2; chunk++) {
393 			if (!(flags & MSG_TRUNC)) {
394 				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) {
404 					if (!read_done)
405 						read_done = -EFAULT;
406 					smc_rmb_sync_sg_for_device(conn);
407 					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 		}
420 		smc_rmb_sync_sg_for_device(conn);
421 
422 		/* update cursors */
423 		if (!(flags & MSG_PEEK)) {
424 			/* increased in recv tasklet smc_cdc_msg_rcv() */
425 			smp_mb__before_atomic();
426 			atomic_sub(copylen, &conn->bytes_to_rcv);
427 			/* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
428 			smp_mb__after_atomic();
429 			if (msg && smc_rx_update_consumer(smc, cons, copylen))
430 				goto out;
431 		}
432 	} while (read_remaining);
433 out:
434 	return read_done;
435 }
436 
437 /* Initialize receive properties on connection establishment. NB: not __init! */
438 void smc_rx_init(struct smc_sock *smc)
439 {
440 	smc->sk.sk_data_ready = smc_rx_wake_up;
441 	atomic_set(&smc->conn.splice_pending, 0);
442 	smc->conn.urg_state = SMC_URG_READ;
443 }
444