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
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 static void smc_rx_update_consumer(struct smc_connection *conn,
52 union smc_host_cursor cons, size_t len)
54 smc_curs_add(conn->rmbe_size, &cons, len);
55 smc_curs_write(&conn->local_tx_ctrl.cons, smc_curs_read(&cons, conn),
57 /* send consumer cursor update if required */
58 /* similar to advertising new TCP rcv_wnd if required */
59 smc_tx_consumer_update(conn);
67 static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe,
68 struct pipe_buffer *buf)
70 struct smc_spd_priv *priv = (struct smc_spd_priv *)buf->private;
71 struct smc_sock *smc = priv->smc;
72 struct smc_connection *conn;
73 union smc_host_cursor cons;
74 struct sock *sk = &smc->sk;
76 if (sk->sk_state == SMC_CLOSED ||
77 sk->sk_state == SMC_PEERFINCLOSEWAIT ||
78 sk->sk_state == SMC_APPFINCLOSEWAIT)
82 smc_curs_write(&cons, smc_curs_read(&conn->local_tx_ctrl.cons, conn),
84 smc_rx_update_consumer(conn, cons, priv->len);
86 if (atomic_sub_and_test(priv->len, &conn->splice_pending))
94 static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info *pipe,
95 struct pipe_buffer *buf)
100 static const struct pipe_buf_operations smc_pipe_ops = {
102 .confirm = generic_pipe_buf_confirm,
103 .release = smc_rx_pipe_buf_release,
104 .steal = smc_rx_pipe_buf_nosteal,
105 .get = generic_pipe_buf_get
108 static void smc_rx_spd_release(struct splice_pipe_desc *spd,
111 put_page(spd->pages[i]);
114 static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
115 struct smc_sock *smc)
117 struct splice_pipe_desc spd;
118 struct partial_page partial;
119 struct smc_spd_priv *priv;
123 page = virt_to_page(smc->conn.rmb_desc->cpu_addr);
124 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
129 partial.offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
131 partial.private = (unsigned long)priv;
133 spd.nr_pages_max = 1;
136 spd.partial = &partial;
137 spd.ops = &smc_pipe_ops;
138 spd.spd_release = smc_rx_spd_release;
140 bytes = splice_to_pipe(pipe, &spd);
143 get_page(smc->conn.rmb_desc->pages);
144 atomic_add(bytes, &smc->conn.splice_pending);
150 static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn)
152 return atomic_read(&conn->bytes_to_rcv) &&
153 !atomic_read(&conn->splice_pending);
156 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
158 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
159 * @fcrit add'l criterion to evaluate as function pointer
161 * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
162 * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
164 int smc_rx_wait(struct smc_sock *smc, long *timeo,
165 int (*fcrit)(struct smc_connection *conn))
167 DEFINE_WAIT_FUNC(wait, woken_wake_function);
168 struct smc_connection *conn = &smc->conn;
169 struct sock *sk = &smc->sk;
174 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
175 add_wait_queue(sk_sleep(sk), &wait);
176 rc = sk_wait_event(sk, timeo,
178 sk->sk_shutdown & RCV_SHUTDOWN ||
180 smc_cdc_rxed_any_close_or_senddone(conn),
182 remove_wait_queue(sk_sleep(sk), &wait);
183 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
187 /* smc_rx_recvmsg - receive data from RMBE
188 * @msg: copy data to receive buffer
189 * @pipe: copy data to pipe if set - indicates splice() call
191 * rcvbuf consumer: main API called by socket layer.
192 * Called under sk lock.
194 int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
195 struct pipe_inode_info *pipe, size_t len, int flags)
197 size_t copylen, read_done = 0, read_remaining = len;
198 size_t chunk_len, chunk_off, chunk_len_sum;
199 struct smc_connection *conn = &smc->conn;
200 int (*func)(struct smc_connection *conn);
201 union smc_host_cursor cons;
207 int target; /* Read at least these many bytes */
210 if (unlikely(flags & MSG_ERRQUEUE))
211 return -EINVAL; /* future work for sk.sk_family == AF_SMC */
213 return -EINVAL; /* future work */
216 if (sk->sk_state == SMC_LISTEN)
218 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
219 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
221 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
222 rcvbuf_base = conn->rmb_desc->cpu_addr;
224 do { /* while (read_remaining) */
225 if (read_done >= target || (pipe && read_done))
228 if (atomic_read(&conn->bytes_to_rcv))
231 if (sk->sk_shutdown & RCV_SHUTDOWN ||
232 smc_cdc_rxed_any_close_or_senddone(conn) ||
233 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort)
238 sk->sk_state == SMC_CLOSED ||
240 signal_pending(current))
244 read_done = sock_error(sk);
247 if (sk->sk_state == SMC_CLOSED) {
248 if (!sock_flag(sk, SOCK_DONE)) {
249 /* This occurs when user tries to read
250 * from never connected socket.
252 read_done = -ENOTCONN;
257 if (signal_pending(current)) {
258 read_done = sock_intr_errno(timeo);
265 if (!smc_rx_data_available(conn)) {
266 smc_rx_wait(smc, &timeo, smc_rx_data_available);
271 /* initialize variables for 1st iteration of subsequent loop */
272 /* could be just 1 byte, even after waiting on data above */
273 readable = atomic_read(&conn->bytes_to_rcv);
274 splbytes = atomic_read(&conn->splice_pending);
275 if (!readable || (msg && splbytes)) {
277 func = smc_rx_data_available_and_no_splice_pend;
279 func = smc_rx_data_available;
280 smc_rx_wait(smc, &timeo, func);
284 /* not more than what user space asked for */
285 copylen = min_t(size_t, read_remaining, readable);
286 smc_curs_write(&cons,
287 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
289 /* subsequent splice() calls pick up where previous left */
291 smc_curs_add(conn->rmbe_size, &cons, splbytes);
292 /* determine chunks where to read from rcvbuf */
293 /* either unwrapped case, or 1st chunk of wrapped case */
294 chunk_len = min_t(size_t,
295 copylen, conn->rmbe_size - cons.count);
296 chunk_len_sum = chunk_len;
297 chunk_off = cons.count;
298 smc_rmb_sync_sg_for_cpu(conn);
299 for (chunk = 0; chunk < 2; chunk++) {
300 if (!(flags & MSG_TRUNC)) {
302 rc = memcpy_to_msg(msg, rcvbuf_base +
306 rc = smc_rx_splice(pipe, rcvbuf_base +
307 chunk_off, chunk_len,
313 smc_rmb_sync_sg_for_device(conn);
317 read_remaining -= chunk_len;
318 read_done += chunk_len;
320 if (chunk_len_sum == copylen)
321 break; /* either on 1st or 2nd iteration */
322 /* prepare next (== 2nd) iteration */
323 chunk_len = copylen - chunk_len; /* remainder */
324 chunk_len_sum += chunk_len;
325 chunk_off = 0; /* modulo offset in recv ring buffer */
327 smc_rmb_sync_sg_for_device(conn);
330 if (!(flags & MSG_PEEK)) {
331 /* increased in recv tasklet smc_cdc_msg_rcv() */
332 smp_mb__before_atomic();
333 atomic_sub(copylen, &conn->bytes_to_rcv);
334 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
335 smp_mb__after_atomic();
337 smc_rx_update_consumer(conn, cons, copylen);
339 } while (read_remaining);
344 /* Initialize receive properties on connection establishment. NB: not __init! */
345 void smc_rx_init(struct smc_sock *smc)
347 smc->sk.sk_data_ready = smc_rx_wake_up;
348 atomic_set(&smc->conn.splice_pending, 0);