1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
6 * copy new RMBE data into user space
8 * Copyright IBM Corp. 2016
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
13 #include <linux/net.h>
14 #include <linux/rcupdate.h>
15 #include <linux/sched/signal.h>
22 #include "smc_tx.h" /* smc_tx_consumer_update() */
25 /* callback implementation to wakeup consumers blocked with smc_rx_wait().
26 * indirectly called by smc_cdc_msg_recv_action().
28 static void smc_rx_wake_up(struct sock
*sk
)
32 /* derived from sock_def_readable() */
33 /* called already in smc_listen_work() */
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
);
46 /* Update consumer cursor
47 * @conn connection to update
48 * @cons consumer cursor
49 * @len number of Bytes consumed
51 * 1 if we should end our receive, 0 otherwise
53 static int smc_rx_update_consumer(struct smc_sock
*smc
,
54 union smc_host_cursor cons
, size_t len
)
56 struct smc_connection
*conn
= &smc
->conn
;
57 struct sock
*sk
= &smc
->sk
;
61 smc_curs_add(conn
->rmb_desc
->len
, &cons
, len
);
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
,
67 if (sock_flag(sk
, SOCK_URGINLINE
)) {
71 conn
->urg_state
= SMC_URG_READ
;
75 /* skip urgent byte */
77 smc_curs_add(conn
->rmb_desc
->len
, &cons
, 1);
78 conn
->urg_rx_skip_pend
= false;
80 /* we read past urgent byte */
81 conn
->urg_state
= SMC_URG_READ
;
85 smc_curs_copy(&conn
->local_tx_ctrl
.cons
, &cons
, conn
);
87 /* send consumer cursor update if required */
88 /* similar to advertising new TCP rcv_wnd if required */
89 smc_tx_consumer_update(conn
, force
);
94 static void smc_rx_update_cons(struct smc_sock
*smc
, size_t len
)
96 struct smc_connection
*conn
= &smc
->conn
;
97 union smc_host_cursor cons
;
99 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
100 smc_rx_update_consumer(smc
, cons
, len
);
103 struct smc_spd_priv
{
104 struct smc_sock
*smc
;
108 static void smc_rx_pipe_buf_release(struct pipe_inode_info
*pipe
,
109 struct pipe_buffer
*buf
)
111 struct smc_spd_priv
*priv
= (struct smc_spd_priv
*)buf
->private;
112 struct smc_sock
*smc
= priv
->smc
;
113 struct smc_connection
*conn
;
114 struct sock
*sk
= &smc
->sk
;
116 if (sk
->sk_state
== SMC_CLOSED
||
117 sk
->sk_state
== SMC_PEERFINCLOSEWAIT
||
118 sk
->sk_state
== SMC_APPFINCLOSEWAIT
)
122 smc_rx_update_cons(smc
, priv
->len
);
124 if (atomic_sub_and_test(priv
->len
, &conn
->splice_pending
))
132 static const struct pipe_buf_operations smc_pipe_ops
= {
133 .release
= smc_rx_pipe_buf_release
,
134 .get
= generic_pipe_buf_get
137 static void smc_rx_spd_release(struct splice_pipe_desc
*spd
,
140 put_page(spd
->pages
[i
]);
143 static int smc_rx_splice(struct pipe_inode_info
*pipe
, char *src
, size_t len
,
144 struct smc_sock
*smc
)
146 struct splice_pipe_desc spd
;
147 struct partial_page partial
;
148 struct smc_spd_priv
*priv
;
151 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
156 partial
.offset
= src
- (char *)smc
->conn
.rmb_desc
->cpu_addr
;
158 partial
.private = (unsigned long)priv
;
160 spd
.nr_pages_max
= 1;
162 spd
.pages
= &smc
->conn
.rmb_desc
->pages
;
163 spd
.partial
= &partial
;
164 spd
.ops
= &smc_pipe_ops
;
165 spd
.spd_release
= smc_rx_spd_release
;
167 bytes
= splice_to_pipe(pipe
, &spd
);
170 get_page(smc
->conn
.rmb_desc
->pages
);
171 atomic_add(bytes
, &smc
->conn
.splice_pending
);
177 static int smc_rx_data_available_and_no_splice_pend(struct smc_connection
*conn
)
179 return atomic_read(&conn
->bytes_to_rcv
) &&
180 !atomic_read(&conn
->splice_pending
);
183 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
185 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
186 * @fcrit add'l criterion to evaluate as function pointer
188 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
189 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
191 int smc_rx_wait(struct smc_sock
*smc
, long *timeo
,
192 int (*fcrit
)(struct smc_connection
*conn
))
194 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
195 struct smc_connection
*conn
= &smc
->conn
;
196 struct smc_cdc_conn_state_flags
*cflags
=
197 &conn
->local_tx_ctrl
.conn_state_flags
;
198 struct sock
*sk
= &smc
->sk
;
203 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
204 add_wait_queue(sk_sleep(sk
), &wait
);
205 rc
= sk_wait_event(sk
, timeo
,
207 cflags
->peer_conn_abort
||
208 sk
->sk_shutdown
& RCV_SHUTDOWN
||
212 remove_wait_queue(sk_sleep(sk
), &wait
);
213 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
217 static int smc_rx_recv_urg(struct smc_sock
*smc
, struct msghdr
*msg
, int len
,
220 struct smc_connection
*conn
= &smc
->conn
;
221 union smc_host_cursor cons
;
222 struct sock
*sk
= &smc
->sk
;
225 if (sock_flag(sk
, SOCK_URGINLINE
) ||
226 !(conn
->urg_state
== SMC_URG_VALID
) ||
227 conn
->urg_state
== SMC_URG_READ
)
230 if (conn
->urg_state
== SMC_URG_VALID
) {
231 if (!(flags
& MSG_PEEK
))
232 smc
->conn
.urg_state
= SMC_URG_READ
;
233 msg
->msg_flags
|= MSG_OOB
;
235 if (!(flags
& MSG_TRUNC
))
236 rc
= memcpy_to_msg(msg
, &conn
->urg_rx_byte
, 1);
238 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
239 if (smc_curs_diff(conn
->rmb_desc
->len
, &cons
,
240 &conn
->urg_curs
) > 1)
241 conn
->urg_rx_skip_pend
= true;
242 /* Urgent Byte was already accounted for, but trigger
243 * skipping the urgent byte in non-inline case
245 if (!(flags
& MSG_PEEK
))
246 smc_rx_update_consumer(smc
, cons
, 0);
248 msg
->msg_flags
|= MSG_TRUNC
;
251 return rc
? -EFAULT
: len
;
254 if (sk
->sk_state
== SMC_CLOSED
|| sk
->sk_shutdown
& RCV_SHUTDOWN
)
260 static bool smc_rx_recvmsg_data_available(struct smc_sock
*smc
)
262 struct smc_connection
*conn
= &smc
->conn
;
264 if (smc_rx_data_available(conn
))
266 else if (conn
->urg_state
== SMC_URG_VALID
)
267 /* we received a single urgent Byte - skip */
268 smc_rx_update_cons(smc
, 0);
272 /* smc_rx_recvmsg - receive data from RMBE
273 * @msg: copy data to receive buffer
274 * @pipe: copy data to pipe if set - indicates splice() call
276 * rcvbuf consumer: main API called by socket layer.
277 * Called under sk lock.
279 int smc_rx_recvmsg(struct smc_sock
*smc
, struct msghdr
*msg
,
280 struct pipe_inode_info
*pipe
, size_t len
, int flags
)
282 size_t copylen
, read_done
= 0, read_remaining
= len
;
283 size_t chunk_len
, chunk_off
, chunk_len_sum
;
284 struct smc_connection
*conn
= &smc
->conn
;
285 int (*func
)(struct smc_connection
*conn
);
286 union smc_host_cursor cons
;
292 int target
; /* Read at least these many bytes */
295 if (unlikely(flags
& MSG_ERRQUEUE
))
296 return -EINVAL
; /* future work for sk.sk_family == AF_SMC */
299 if (sk
->sk_state
== SMC_LISTEN
)
302 return smc_rx_recv_urg(smc
, msg
, len
, flags
);
303 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
304 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
306 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
307 rcvbuf_base
= conn
->rx_off
+ conn
->rmb_desc
->cpu_addr
;
309 do { /* while (read_remaining) */
310 if (read_done
>= target
|| (pipe
&& read_done
))
316 if (smc_rx_recvmsg_data_available(smc
))
319 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
320 /* smc_cdc_msg_recv_action() could have run after
321 * above smc_rx_recvmsg_data_available()
323 if (smc_rx_recvmsg_data_available(smc
))
330 sk
->sk_state
== SMC_CLOSED
||
332 signal_pending(current
))
336 read_done
= sock_error(sk
);
339 if (sk
->sk_state
== SMC_CLOSED
) {
340 if (!sock_flag(sk
, SOCK_DONE
)) {
341 /* This occurs when user tries to read
342 * from never connected socket.
344 read_done
= -ENOTCONN
;
349 if (signal_pending(current
)) {
350 read_done
= sock_intr_errno(timeo
);
357 if (!smc_rx_data_available(conn
)) {
358 smc_rx_wait(smc
, &timeo
, smc_rx_data_available
);
363 /* initialize variables for 1st iteration of subsequent loop */
364 /* could be just 1 byte, even after waiting on data above */
365 readable
= atomic_read(&conn
->bytes_to_rcv
);
366 splbytes
= atomic_read(&conn
->splice_pending
);
367 if (!readable
|| (msg
&& splbytes
)) {
369 func
= smc_rx_data_available_and_no_splice_pend
;
371 func
= smc_rx_data_available
;
372 smc_rx_wait(smc
, &timeo
, func
);
376 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
377 /* subsequent splice() calls pick up where previous left */
379 smc_curs_add(conn
->rmb_desc
->len
, &cons
, splbytes
);
380 if (conn
->urg_state
== SMC_URG_VALID
&&
381 sock_flag(&smc
->sk
, SOCK_URGINLINE
) &&
383 readable
--; /* always stop at urgent Byte */
384 /* not more than what user space asked for */
385 copylen
= min_t(size_t, read_remaining
, readable
);
386 /* determine chunks where to read from rcvbuf */
387 /* either unwrapped case, or 1st chunk of wrapped case */
388 chunk_len
= min_t(size_t, copylen
, conn
->rmb_desc
->len
-
390 chunk_len_sum
= chunk_len
;
391 chunk_off
= cons
.count
;
392 smc_rmb_sync_sg_for_cpu(conn
);
393 for (chunk
= 0; chunk
< 2; chunk
++) {
394 if (!(flags
& MSG_TRUNC
)) {
396 rc
= memcpy_to_msg(msg
, rcvbuf_base
+
400 rc
= smc_rx_splice(pipe
, rcvbuf_base
+
401 chunk_off
, chunk_len
,
407 smc_rmb_sync_sg_for_device(conn
);
411 read_remaining
-= chunk_len
;
412 read_done
+= chunk_len
;
414 if (chunk_len_sum
== copylen
)
415 break; /* either on 1st or 2nd iteration */
416 /* prepare next (== 2nd) iteration */
417 chunk_len
= copylen
- chunk_len
; /* remainder */
418 chunk_len_sum
+= chunk_len
;
419 chunk_off
= 0; /* modulo offset in recv ring buffer */
421 smc_rmb_sync_sg_for_device(conn
);
424 if (!(flags
& MSG_PEEK
)) {
425 /* increased in recv tasklet smc_cdc_msg_rcv() */
426 smp_mb__before_atomic();
427 atomic_sub(copylen
, &conn
->bytes_to_rcv
);
428 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
429 smp_mb__after_atomic();
430 if (msg
&& smc_rx_update_consumer(smc
, cons
, copylen
))
433 } while (read_remaining
);
438 /* Initialize receive properties on connection establishment. NB: not __init! */
439 void smc_rx_init(struct smc_sock
*smc
)
441 smc
->sk
.sk_data_ready
= smc_rx_wake_up
;
442 atomic_set(&smc
->conn
.splice_pending
, 0);
443 smc
->conn
.urg_state
= SMC_URG_READ
;