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>
16 #include <linux/splice.h>
19 #include <trace/events/sock.h>
24 #include "smc_tx.h" /* smc_tx_consumer_update() */
26 #include "smc_stats.h"
27 #include "smc_tracepoint.h"
29 /* callback implementation to wakeup consumers blocked with smc_rx_wait().
30 * indirectly called by smc_cdc_msg_recv_action().
32 static void smc_rx_wake_up(struct sock
*sk
)
36 trace_sk_data_ready(sk
);
38 /* derived from sock_def_readable() */
39 /* called already in smc_listen_work() */
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
);
52 /* Update consumer cursor
53 * @conn connection to update
54 * @cons consumer cursor
55 * @len number of Bytes consumed
57 * 1 if we should end our receive, 0 otherwise
59 static int smc_rx_update_consumer(struct smc_sock
*smc
,
60 union smc_host_cursor cons
, size_t len
)
62 struct smc_connection
*conn
= &smc
->conn
;
63 struct sock
*sk
= &smc
->sk
;
67 smc_curs_add(conn
->rmb_desc
->len
, &cons
, len
);
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
,
73 if (sock_flag(sk
, SOCK_URGINLINE
)) {
77 conn
->urg_state
= SMC_URG_READ
;
81 /* skip urgent byte */
83 smc_curs_add(conn
->rmb_desc
->len
, &cons
, 1);
84 conn
->urg_rx_skip_pend
= false;
86 /* we read past urgent byte */
87 conn
->urg_state
= SMC_URG_READ
;
91 smc_curs_copy(&conn
->local_tx_ctrl
.cons
, &cons
, conn
);
93 /* send consumer cursor update if required */
94 /* similar to advertising new TCP rcv_wnd if required */
95 smc_tx_consumer_update(conn
, force
);
100 static void smc_rx_update_cons(struct smc_sock
*smc
, size_t len
)
102 struct smc_connection
*conn
= &smc
->conn
;
103 union smc_host_cursor cons
;
105 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
106 smc_rx_update_consumer(smc
, cons
, len
);
109 struct smc_spd_priv
{
110 struct smc_sock
*smc
;
114 static void smc_rx_pipe_buf_release(struct pipe_inode_info
*pipe
,
115 struct pipe_buffer
*buf
)
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
;
122 if (sk
->sk_state
== SMC_CLOSED
||
123 sk
->sk_state
== SMC_PEERFINCLOSEWAIT
||
124 sk
->sk_state
== SMC_APPFINCLOSEWAIT
)
128 smc_rx_update_cons(smc
, priv
->len
);
130 if (atomic_sub_and_test(priv
->len
, &conn
->splice_pending
))
138 static const struct pipe_buf_operations smc_pipe_ops
= {
139 .release
= smc_rx_pipe_buf_release
,
140 .get
= generic_pipe_buf_get
143 static void smc_rx_spd_release(struct splice_pipe_desc
*spd
,
146 put_page(spd
->pages
[i
]);
149 static int smc_rx_splice(struct pipe_inode_info
*pipe
, char *src
, size_t len
,
150 struct smc_sock
*smc
)
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
;
161 nr_pages
= !lgr
->is_smcd
&& smc
->conn
.rmb_desc
->is_vm
?
162 PAGE_ALIGN(len
+ offset
) / PAGE_SIZE
: 1;
164 pages
= kcalloc(nr_pages
, sizeof(*pages
), GFP_KERNEL
);
167 partial
= kcalloc(nr_pages
, sizeof(*partial
), GFP_KERNEL
);
170 priv
= kcalloc(nr_pages
, sizeof(*priv
), GFP_KERNEL
);
173 for (i
= 0; i
< nr_pages
; i
++) {
174 priv
[i
] = kzalloc(sizeof(**priv
), GFP_KERNEL
);
180 (!lgr
->is_smcd
&& !smc
->conn
.rmb_desc
->is_vm
)) {
181 /* smcd or smcr that uses physically contiguous RMBs */
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
;
189 int size
, left
= len
;
191 /* smcr that uses virtually contiguous RMBs*/
192 for (i
= 0; i
< nr_pages
; i
++) {
193 size
= min_t(int, PAGE_SIZE
- offset
, left
);
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
);
205 spd
.nr_pages_max
= nr_pages
;
206 spd
.nr_pages
= nr_pages
;
208 spd
.partial
= partial
;
209 spd
.ops
= &smc_pipe_ops
;
210 spd
.spd_release
= smc_rx_spd_release
;
212 bytes
= splice_to_pipe(pipe
, &spd
);
215 if (!lgr
->is_smcd
&& smc
->conn
.rmb_desc
->is_vm
) {
216 for (i
= 0; i
< PAGE_ALIGN(bytes
+ offset
) / PAGE_SIZE
; i
++)
219 get_page(smc
->conn
.rmb_desc
->pages
);
221 atomic_add(bytes
, &smc
->conn
.splice_pending
);
230 for (i
= (i
- 1); i
>= 0; i
--)
241 static int smc_rx_data_available_and_no_splice_pend(struct smc_connection
*conn
)
243 return atomic_read(&conn
->bytes_to_rcv
) &&
244 !atomic_read(&conn
->splice_pending
);
247 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
249 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
250 * @fcrit add'l criterion to evaluate as function pointer
252 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
253 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
255 int smc_rx_wait(struct smc_sock
*smc
, long *timeo
,
256 int (*fcrit
)(struct smc_connection
*conn
))
258 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
259 struct smc_connection
*conn
= &smc
->conn
;
260 struct smc_cdc_conn_state_flags
*cflags
=
261 &conn
->local_tx_ctrl
.conn_state_flags
;
262 struct sock
*sk
= &smc
->sk
;
267 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
268 add_wait_queue(sk_sleep(sk
), &wait
);
269 rc
= sk_wait_event(sk
, timeo
,
270 READ_ONCE(sk
->sk_err
) ||
271 cflags
->peer_conn_abort
||
272 READ_ONCE(sk
->sk_shutdown
) & RCV_SHUTDOWN
||
276 remove_wait_queue(sk_sleep(sk
), &wait
);
277 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
281 static int smc_rx_recv_urg(struct smc_sock
*smc
, struct msghdr
*msg
, int len
,
284 struct smc_connection
*conn
= &smc
->conn
;
285 union smc_host_cursor cons
;
286 struct sock
*sk
= &smc
->sk
;
289 if (sock_flag(sk
, SOCK_URGINLINE
) ||
290 !(conn
->urg_state
== SMC_URG_VALID
) ||
291 conn
->urg_state
== SMC_URG_READ
)
294 SMC_STAT_INC(smc
, urg_data_cnt
);
295 if (conn
->urg_state
== SMC_URG_VALID
) {
296 if (!(flags
& MSG_PEEK
))
297 smc
->conn
.urg_state
= SMC_URG_READ
;
298 msg
->msg_flags
|= MSG_OOB
;
300 if (!(flags
& MSG_TRUNC
))
301 rc
= memcpy_to_msg(msg
, &conn
->urg_rx_byte
, 1);
303 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
304 if (smc_curs_diff(conn
->rmb_desc
->len
, &cons
,
305 &conn
->urg_curs
) > 1)
306 conn
->urg_rx_skip_pend
= true;
307 /* Urgent Byte was already accounted for, but trigger
308 * skipping the urgent byte in non-inline case
310 if (!(flags
& MSG_PEEK
))
311 smc_rx_update_consumer(smc
, cons
, 0);
313 msg
->msg_flags
|= MSG_TRUNC
;
316 return rc
? -EFAULT
: len
;
319 if (sk
->sk_state
== SMC_CLOSED
|| sk
->sk_shutdown
& RCV_SHUTDOWN
)
325 static bool smc_rx_recvmsg_data_available(struct smc_sock
*smc
)
327 struct smc_connection
*conn
= &smc
->conn
;
329 if (smc_rx_data_available(conn
))
331 else if (conn
->urg_state
== SMC_URG_VALID
)
332 /* we received a single urgent Byte - skip */
333 smc_rx_update_cons(smc
, 0);
337 /* smc_rx_recvmsg - receive data from RMBE
338 * @msg: copy data to receive buffer
339 * @pipe: copy data to pipe if set - indicates splice() call
341 * rcvbuf consumer: main API called by socket layer.
342 * Called under sk lock.
344 int smc_rx_recvmsg(struct smc_sock
*smc
, struct msghdr
*msg
,
345 struct pipe_inode_info
*pipe
, size_t len
, int flags
)
347 size_t copylen
, read_done
= 0, read_remaining
= len
;
348 size_t chunk_len
, chunk_off
, chunk_len_sum
;
349 struct smc_connection
*conn
= &smc
->conn
;
350 int (*func
)(struct smc_connection
*conn
);
351 union smc_host_cursor cons
;
357 int target
; /* Read at least these many bytes */
360 if (unlikely(flags
& MSG_ERRQUEUE
))
361 return -EINVAL
; /* future work for sk.sk_family == AF_SMC */
364 if (sk
->sk_state
== SMC_LISTEN
)
367 return smc_rx_recv_urg(smc
, msg
, len
, flags
);
368 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
369 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
371 readable
= atomic_read(&conn
->bytes_to_rcv
);
372 if (readable
>= conn
->rmb_desc
->len
)
373 SMC_STAT_RMB_RX_FULL(smc
, !conn
->lnk
);
376 SMC_STAT_RMB_RX_SIZE_SMALL(smc
, !conn
->lnk
);
377 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
378 rcvbuf_base
= conn
->rx_off
+ conn
->rmb_desc
->cpu_addr
;
380 do { /* while (read_remaining) */
381 if (read_done
>= target
|| (pipe
&& read_done
))
387 if (smc_rx_recvmsg_data_available(smc
))
390 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
391 /* smc_cdc_msg_recv_action() could have run after
392 * above smc_rx_recvmsg_data_available()
394 if (smc_rx_recvmsg_data_available(smc
))
401 sk
->sk_state
== SMC_CLOSED
||
403 signal_pending(current
))
407 read_done
= sock_error(sk
);
410 if (sk
->sk_state
== SMC_CLOSED
) {
411 if (!sock_flag(sk
, SOCK_DONE
)) {
412 /* This occurs when user tries to read
413 * from never connected socket.
415 read_done
= -ENOTCONN
;
422 if (signal_pending(current
)) {
423 read_done
= sock_intr_errno(timeo
);
428 if (!smc_rx_data_available(conn
)) {
429 smc_rx_wait(smc
, &timeo
, smc_rx_data_available
);
434 /* initialize variables for 1st iteration of subsequent loop */
435 /* could be just 1 byte, even after waiting on data above */
436 readable
= atomic_read(&conn
->bytes_to_rcv
);
437 splbytes
= atomic_read(&conn
->splice_pending
);
438 if (!readable
|| (msg
&& splbytes
)) {
440 func
= smc_rx_data_available_and_no_splice_pend
;
442 func
= smc_rx_data_available
;
443 smc_rx_wait(smc
, &timeo
, func
);
447 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
448 /* subsequent splice() calls pick up where previous left */
450 smc_curs_add(conn
->rmb_desc
->len
, &cons
, splbytes
);
451 if (conn
->urg_state
== SMC_URG_VALID
&&
452 sock_flag(&smc
->sk
, SOCK_URGINLINE
) &&
454 readable
--; /* always stop at urgent Byte */
455 /* not more than what user space asked for */
456 copylen
= min_t(size_t, read_remaining
, readable
);
457 /* determine chunks where to read from rcvbuf */
458 /* either unwrapped case, or 1st chunk of wrapped case */
459 chunk_len
= min_t(size_t, copylen
, conn
->rmb_desc
->len
-
461 chunk_len_sum
= chunk_len
;
462 chunk_off
= cons
.count
;
463 smc_rmb_sync_sg_for_cpu(conn
);
464 for (chunk
= 0; chunk
< 2; chunk
++) {
465 if (!(flags
& MSG_TRUNC
)) {
467 rc
= memcpy_to_msg(msg
, rcvbuf_base
+
471 rc
= smc_rx_splice(pipe
, rcvbuf_base
+
472 chunk_off
, chunk_len
,
481 read_remaining
-= chunk_len
;
482 read_done
+= chunk_len
;
484 if (chunk_len_sum
== copylen
)
485 break; /* either on 1st or 2nd iteration */
486 /* prepare next (== 2nd) iteration */
487 chunk_len
= copylen
- chunk_len
; /* remainder */
488 chunk_len_sum
+= chunk_len
;
489 chunk_off
= 0; /* modulo offset in recv ring buffer */
493 if (!(flags
& MSG_PEEK
)) {
494 /* increased in recv tasklet smc_cdc_msg_rcv() */
495 smp_mb__before_atomic();
496 atomic_sub(copylen
, &conn
->bytes_to_rcv
);
497 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
498 smp_mb__after_atomic();
499 if (msg
&& smc_rx_update_consumer(smc
, cons
, copylen
))
503 trace_smc_rx_recvmsg(smc
, copylen
);
504 } while (read_remaining
);
509 /* Initialize receive properties on connection establishment. NB: not __init! */
510 void smc_rx_init(struct smc_sock
*smc
)
512 smc
->sk
.sk_data_ready
= smc_rx_wake_up
;
513 atomic_set(&smc
->conn
.splice_pending
, 0);
514 smc
->conn
.urg_state
= SMC_URG_READ
;