Linux Audio

Check our new training course

Linux kernel drivers training

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