blob: ed45569289f58571940117411e3fc0329954d9d6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braun952310c2017-01-09 16:55:24 +01002/*
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>
Ingo Molnarc3edc402017-02-02 08:35:14 +010015#include <linux/sched/signal.h>
16
Ursula Braun952310c2017-01-09 16:55:24 +010017#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
Stefan Rasplb51fa1b2018-05-03 18:12:37 +020025/* callback implementation to wakeup consumers blocked with smc_rx_wait().
Ursula Braun952310c2017-01-09 16:55:24 +010026 * indirectly called by smc_cdc_msg_recv_action().
27 */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +020028static void smc_rx_wake_up(struct sock *sk)
Ursula Braun952310c2017-01-09 16:55:24 +010029{
30 struct socket_wq *wq;
31
32 /* derived from sock_def_readable() */
33 /* called already in smc_listen_work() */
34 rcu_read_lock();
35 wq = rcu_dereference(sk->sk_wq);
36 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -080037 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
38 EPOLLRDNORM | EPOLLRDBAND);
Ursula Braun90e9517e2017-04-10 14:58:00 +020039 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Ursula Braun952310c2017-01-09 16:55:24 +010040 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
41 (sk->sk_state == SMC_CLOSED))
42 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Ursula Braun952310c2017-01-09 16:55:24 +010043 rcu_read_unlock();
44}
45
Stefan Raspl9014db22018-05-03 18:12:39 +020046/* Update consumer cursor
47 * @conn connection to update
48 * @cons consumer cursor
49 * @len number of Bytes consumed
50 */
51static void smc_rx_update_consumer(struct smc_connection *conn,
52 union smc_host_cursor cons, size_t len)
53{
54 smc_curs_add(conn->rmbe_size, &cons, len);
55 smc_curs_write(&conn->local_tx_ctrl.cons, smc_curs_read(&cons, conn),
56 conn);
57 /* send consumer cursor update if required */
58 /* similar to advertising new TCP rcv_wnd if required */
59 smc_tx_consumer_update(conn);
60}
61
62struct smc_spd_priv {
63 struct smc_sock *smc;
64 size_t len;
65};
66
67static void smc_rx_pipe_buf_release(struct pipe_inode_info *pipe,
68 struct pipe_buffer *buf)
69{
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;
75
76 if (sk->sk_state == SMC_CLOSED ||
77 sk->sk_state == SMC_PEERFINCLOSEWAIT ||
78 sk->sk_state == SMC_APPFINCLOSEWAIT)
79 goto out;
80 conn = &smc->conn;
81 lock_sock(sk);
82 smc_curs_write(&cons, smc_curs_read(&conn->local_tx_ctrl.cons, conn),
83 conn);
84 smc_rx_update_consumer(conn, cons, priv->len);
85 release_sock(sk);
86 if (atomic_sub_and_test(priv->len, &conn->splice_pending))
87 smc_rx_wake_up(sk);
88out:
89 kfree(priv);
90 put_page(buf->page);
91 sock_put(sk);
92}
93
94static int smc_rx_pipe_buf_nosteal(struct pipe_inode_info *pipe,
95 struct pipe_buffer *buf)
96{
97 return 1;
98}
99
100static const struct pipe_buf_operations smc_pipe_ops = {
101 .can_merge = 0,
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
106};
107
108static void smc_rx_spd_release(struct splice_pipe_desc *spd,
109 unsigned int i)
110{
111 put_page(spd->pages[i]);
112}
113
114static int smc_rx_splice(struct pipe_inode_info *pipe, char *src, size_t len,
115 struct smc_sock *smc)
116{
117 struct splice_pipe_desc spd;
118 struct partial_page partial;
119 struct smc_spd_priv *priv;
120 struct page *page;
121 int bytes;
122
123 page = virt_to_page(smc->conn.rmb_desc->cpu_addr);
124 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
125 if (!priv)
126 return -ENOMEM;
127 priv->len = len;
128 priv->smc = smc;
129 partial.offset = src - (char *)smc->conn.rmb_desc->cpu_addr;
130 partial.len = len;
131 partial.private = (unsigned long)priv;
132
133 spd.nr_pages_max = 1;
134 spd.nr_pages = 1;
135 spd.pages = &page;
136 spd.partial = &partial;
137 spd.ops = &smc_pipe_ops;
138 spd.spd_release = smc_rx_spd_release;
139
140 bytes = splice_to_pipe(pipe, &spd);
141 if (bytes > 0) {
142 sock_hold(&smc->sk);
143 get_page(smc->conn.rmb_desc->pages);
144 atomic_add(bytes, &smc->conn.splice_pending);
145 }
146
147 return bytes;
148}
149
150static int smc_rx_data_available_and_no_splice_pend(struct smc_connection *conn)
151{
152 return atomic_read(&conn->bytes_to_rcv) &&
153 !atomic_read(&conn->splice_pending);
154}
155
Ursula Braun952310c2017-01-09 16:55:24 +0100156/* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
157 * @smc smc socket
158 * @timeo pointer to max seconds to wait, pointer to value 0 for no timeout
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200159 * @fcrit add'l criterion to evaluate as function pointer
Ursula Braun952310c2017-01-09 16:55:24 +0100160 * Returns:
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).
163 */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200164int smc_rx_wait(struct smc_sock *smc, long *timeo,
165 int (*fcrit)(struct smc_connection *conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100166{
167 DEFINE_WAIT_FUNC(wait, woken_wake_function);
168 struct smc_connection *conn = &smc->conn;
169 struct sock *sk = &smc->sk;
170 int rc;
171
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200172 if (fcrit(conn))
Ursula Braun952310c2017-01-09 16:55:24 +0100173 return 1;
174 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
175 add_wait_queue(sk_sleep(sk), &wait);
176 rc = sk_wait_event(sk, timeo,
177 sk->sk_err ||
178 sk->sk_shutdown & RCV_SHUTDOWN ||
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200179 fcrit(conn) ||
Ursula Braun952310c2017-01-09 16:55:24 +0100180 smc_cdc_rxed_any_close_or_senddone(conn),
181 &wait);
182 remove_wait_queue(sk_sleep(sk), &wait);
183 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
184 return rc;
185}
186
Stefan Raspl9014db22018-05-03 18:12:39 +0200187/* 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
190 *
191 * rcvbuf consumer: main API called by socket layer.
192 * Called under sk lock.
Ursula Braun952310c2017-01-09 16:55:24 +0100193 */
Stefan Raspl9014db22018-05-03 18:12:39 +0200194int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg,
195 struct pipe_inode_info *pipe, size_t len, int flags)
Ursula Braun952310c2017-01-09 16:55:24 +0100196{
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;
Stefan Raspl9014db22018-05-03 18:12:39 +0200200 int (*func)(struct smc_connection *conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100201 union smc_host_cursor cons;
202 int readable, chunk;
203 char *rcvbuf_base;
204 struct sock *sk;
Stefan Raspl9014db22018-05-03 18:12:39 +0200205 int splbytes;
Ursula Braun952310c2017-01-09 16:55:24 +0100206 long timeo;
207 int target; /* Read at least these many bytes */
208 int rc;
209
210 if (unlikely(flags & MSG_ERRQUEUE))
211 return -EINVAL; /* future work for sk.sk_family == AF_SMC */
212 if (flags & MSG_OOB)
213 return -EINVAL; /* future work */
214
215 sk = &smc->sk;
216 if (sk->sk_state == SMC_LISTEN)
217 return -ENOTCONN;
218 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
219 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
220
Ursula Braun952310c2017-01-09 16:55:24 +0100221 /* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
222 rcvbuf_base = conn->rmb_desc->cpu_addr;
223
224 do { /* while (read_remaining) */
Stefan Raspl9014db22018-05-03 18:12:39 +0200225 if (read_done >= target || (pipe && read_done))
Ursula Braun952310c2017-01-09 16:55:24 +0100226 break;
227
228 if (atomic_read(&conn->bytes_to_rcv))
229 goto copy;
230
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200231 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)
234 break;
235
Ursula Braun952310c2017-01-09 16:55:24 +0100236 if (read_done) {
237 if (sk->sk_err ||
238 sk->sk_state == SMC_CLOSED ||
Ursula Braun952310c2017-01-09 16:55:24 +0100239 !timeo ||
Stefan Rasplc8b8ec82018-05-03 18:12:36 +0200240 signal_pending(current))
Ursula Braun952310c2017-01-09 16:55:24 +0100241 break;
242 } else {
Ursula Braun952310c2017-01-09 16:55:24 +0100243 if (sk->sk_err) {
244 read_done = sock_error(sk);
245 break;
246 }
Ursula Braun952310c2017-01-09 16:55:24 +0100247 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.
251 */
252 read_done = -ENOTCONN;
253 break;
254 }
255 break;
256 }
257 if (signal_pending(current)) {
258 read_done = sock_intr_errno(timeo);
259 break;
260 }
Hans Wippel846e3442017-09-21 09:16:27 +0200261 if (!timeo)
262 return -EAGAIN;
Ursula Braun952310c2017-01-09 16:55:24 +0100263 }
264
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200265 if (!smc_rx_data_available(conn)) {
266 smc_rx_wait(smc, &timeo, smc_rx_data_available);
Ursula Braun952310c2017-01-09 16:55:24 +0100267 continue;
268 }
269
270copy:
271 /* initialize variables for 1st iteration of subsequent loop */
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200272 /* could be just 1 byte, even after waiting on data above */
Ursula Braun952310c2017-01-09 16:55:24 +0100273 readable = atomic_read(&conn->bytes_to_rcv);
Stefan Raspl9014db22018-05-03 18:12:39 +0200274 splbytes = atomic_read(&conn->splice_pending);
275 if (!readable || (msg && splbytes)) {
276 if (splbytes)
277 func = smc_rx_data_available_and_no_splice_pend;
278 else
279 func = smc_rx_data_available;
280 smc_rx_wait(smc, &timeo, func);
281 continue;
282 }
283
Ursula Braun952310c2017-01-09 16:55:24 +0100284 /* 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),
288 conn);
Stefan Raspl9014db22018-05-03 18:12:39 +0200289 /* subsequent splice() calls pick up where previous left */
290 if (splbytes)
291 smc_curs_add(conn->rmbe_size, &cons, splbytes);
Ursula Braun952310c2017-01-09 16:55:24 +0100292 /* 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;
Ursula Braun10428dd2017-07-28 13:56:22 +0200298 smc_rmb_sync_sg_for_cpu(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100299 for (chunk = 0; chunk < 2; chunk++) {
300 if (!(flags & MSG_TRUNC)) {
Stefan Raspl9014db22018-05-03 18:12:39 +0200301 if (msg) {
302 rc = memcpy_to_msg(msg, rcvbuf_base +
303 chunk_off,
304 chunk_len);
305 } else {
306 rc = smc_rx_splice(pipe, rcvbuf_base +
307 chunk_off, chunk_len,
308 smc);
309 }
310 if (rc < 0) {
Ursula Braun952310c2017-01-09 16:55:24 +0100311 if (!read_done)
312 read_done = -EFAULT;
Ursula Braun10428dd2017-07-28 13:56:22 +0200313 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100314 goto out;
315 }
316 }
317 read_remaining -= chunk_len;
318 read_done += chunk_len;
319
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 */
326 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200327 smc_rmb_sync_sg_for_device(conn);
Ursula Braun952310c2017-01-09 16:55:24 +0100328
329 /* update cursors */
330 if (!(flags & MSG_PEEK)) {
Ursula Braun952310c2017-01-09 16:55:24 +0100331 /* 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();
Stefan Raspl9014db22018-05-03 18:12:39 +0200336 if (msg)
337 smc_rx_update_consumer(conn, cons, copylen);
Ursula Braun952310c2017-01-09 16:55:24 +0100338 }
339 } while (read_remaining);
340out:
341 return read_done;
342}
343
344/* Initialize receive properties on connection establishment. NB: not __init! */
345void smc_rx_init(struct smc_sock *smc)
346{
Stefan Rasplb51fa1b2018-05-03 18:12:37 +0200347 smc->sk.sk_data_ready = smc_rx_wake_up;
Stefan Raspl9014db22018-05-03 18:12:39 +0200348 atomic_set(&smc->conn.splice_pending, 0);
Ursula Braun952310c2017-01-09 16:55:24 +0100349}