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_write(&conn
->local_tx_ctrl
.cons
, smc_curs_read(&cons
, conn
),
88 /* send consumer cursor update if required */
89 /* similar to advertising new TCP rcv_wnd if required */
90 smc_tx_consumer_update(conn
, force
);
95 static void smc_rx_update_cons(struct smc_sock
*smc
, size_t len
)
97 struct smc_connection
*conn
= &smc
->conn
;
98 union smc_host_cursor cons
;
100 smc_curs_write(&cons
, smc_curs_read(&conn
->local_tx_ctrl
.cons
, conn
),
102 smc_rx_update_consumer(smc
, cons
, len
);
105 struct smc_spd_priv
{
106 struct smc_sock
*smc
;
110 static void smc_rx_pipe_buf_release(struct pipe_inode_info
*pipe
,
111 struct pipe_buffer
*buf
)
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
;
118 if (sk
->sk_state
== SMC_CLOSED
||
119 sk
->sk_state
== SMC_PEERFINCLOSEWAIT
||
120 sk
->sk_state
== SMC_APPFINCLOSEWAIT
)
124 smc_rx_update_cons(smc
, priv
->len
);
126 if (atomic_sub_and_test(priv
->len
, &conn
->splice_pending
))
134 static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info
*pipe
,
135 struct pipe_buffer
*buf
)
140 static const struct pipe_buf_operations smc_pipe_ops
= {
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
148 static void smc_rx_spd_release(struct splice_pipe_desc
*spd
,
151 put_page(spd
->pages
[i
]);
154 static int smc_rx_splice(struct pipe_inode_info
*pipe
, char *src
, size_t len
,
155 struct smc_sock
*smc
)
157 struct splice_pipe_desc spd
;
158 struct partial_page partial
;
159 struct smc_spd_priv
*priv
;
163 page
= virt_to_page(smc
->conn
.rmb_desc
->cpu_addr
);
164 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
169 partial
.offset
= src
- (char *)smc
->conn
.rmb_desc
->cpu_addr
;
171 partial
.private = (unsigned long)priv
;
173 spd
.nr_pages_max
= 1;
176 spd
.partial
= &partial
;
177 spd
.ops
= &smc_pipe_ops
;
178 spd
.spd_release
= smc_rx_spd_release
;
180 bytes
= splice_to_pipe(pipe
, &spd
);
183 get_page(smc
->conn
.rmb_desc
->pages
);
184 atomic_add(bytes
, &smc
->conn
.splice_pending
);
190 static int smc_rx_data_available_and_no_splice_pend(struct smc_connection
*conn
)
192 return atomic_read(&conn
->bytes_to_rcv
) &&
193 !atomic_read(&conn
->splice_pending
);
196 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
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
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).
204 int smc_rx_wait(struct smc_sock
*smc
, long *timeo
,
205 int (*fcrit
)(struct smc_connection
*conn
))
207 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
208 struct smc_connection
*conn
= &smc
->conn
;
209 struct sock
*sk
= &smc
->sk
;
214 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
215 add_wait_queue(sk_sleep(sk
), &wait
);
216 rc
= sk_wait_event(sk
, timeo
,
218 sk
->sk_shutdown
& RCV_SHUTDOWN
||
220 smc_cdc_rxed_any_close_or_senddone(conn
),
222 remove_wait_queue(sk_sleep(sk
), &wait
);
223 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
227 static int smc_rx_recv_urg(struct smc_sock
*smc
, struct msghdr
*msg
, int len
,
230 struct smc_connection
*conn
= &smc
->conn
;
231 union smc_host_cursor cons
;
232 struct sock
*sk
= &smc
->sk
;
235 if (sock_flag(sk
, SOCK_URGINLINE
) ||
236 !(conn
->urg_state
== SMC_URG_VALID
) ||
237 conn
->urg_state
== SMC_URG_READ
)
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
;
245 if (!(flags
& MSG_TRUNC
))
246 rc
= memcpy_to_msg(msg
, &conn
->urg_rx_byte
, 1);
248 smc_curs_write(&cons
,
249 smc_curs_read(&conn
->local_tx_ctrl
.cons
,
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
258 if (!(flags
& MSG_PEEK
))
259 smc_rx_update_consumer(smc
, cons
, 0);
261 msg
->msg_flags
|= MSG_TRUNC
;
264 return rc
? -EFAULT
: len
;
267 if (sk
->sk_state
== SMC_CLOSED
|| sk
->sk_shutdown
& RCV_SHUTDOWN
)
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
277 * rcvbuf consumer: main API called by socket layer.
278 * Called under sk lock.
280 int smc_rx_recvmsg(struct smc_sock
*smc
, struct msghdr
*msg
,
281 struct pipe_inode_info
*pipe
, size_t len
, int flags
)
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
;
293 int target
; /* Read at least these many bytes */
296 if (unlikely(flags
& MSG_ERRQUEUE
))
297 return -EINVAL
; /* future work for sk.sk_family == AF_SMC */
300 if (sk
->sk_state
== SMC_LISTEN
)
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
);
307 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
308 rcvbuf_base
= conn
->rmb_desc
->cpu_addr
;
310 do { /* while (read_remaining) */
311 if (read_done
>= target
|| (pipe
&& read_done
))
314 if (atomic_read(&conn
->bytes_to_rcv
))
316 else if (conn
->urg_state
== SMC_URG_VALID
)
317 /* we received a single urgent Byte - skip */
318 smc_rx_update_cons(smc
, 0);
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
)
327 sk
->sk_state
== SMC_CLOSED
||
329 signal_pending(current
))
333 read_done
= sock_error(sk
);
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.
341 read_done
= -ENOTCONN
;
346 if (signal_pending(current
)) {
347 read_done
= sock_intr_errno(timeo
);
354 if (!smc_rx_data_available(conn
)) {
355 smc_rx_wait(smc
, &timeo
, smc_rx_data_available
);
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
)) {
366 func
= smc_rx_data_available_and_no_splice_pend
;
368 func
= smc_rx_data_available
;
369 smc_rx_wait(smc
, &timeo
, func
);
373 smc_curs_write(&cons
,
374 smc_curs_read(&conn
->local_tx_ctrl
.cons
, conn
),
376 /* subsequent splice() calls pick up where previous left */
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
) &&
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
-
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
)) {
395 rc
= memcpy_to_msg(msg
, rcvbuf_base
+
399 rc
= smc_rx_splice(pipe
, rcvbuf_base
+
400 chunk_off
, chunk_len
,
406 smc_rmb_sync_sg_for_device(conn
);
410 read_remaining
-= chunk_len
;
411 read_done
+= chunk_len
;
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 */
420 smc_rmb_sync_sg_for_device(conn
);
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
))
432 } while (read_remaining
);
437 /* Initialize receive properties on connection establishment. NB: not __init! */
438 void smc_rx_init(struct smc_sock
*smc
)
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
;