Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
b38d7324 UB |
2 | /* |
3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE | |
4 | * | |
5 | * Socket Closing - normal and abnormal | |
6 | * | |
7 | * Copyright IBM Corp. 2016 | |
8 | * | |
9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> | |
10 | */ | |
11 | ||
12 | #include <linux/workqueue.h> | |
c3edc401 IM |
13 | #include <linux/sched/signal.h> |
14 | ||
b38d7324 | 15 | #include <net/sock.h> |
83179760 | 16 | #include <net/tcp.h> |
b38d7324 UB |
17 | |
18 | #include "smc.h" | |
19 | #include "smc_tx.h" | |
20 | #include "smc_cdc.h" | |
21 | #include "smc_close.h" | |
22 | ||
127f4970 UB |
23 | #define SMC_CLOSE_WAIT_LISTEN_CLCSOCK_TIME (5 * HZ) |
24 | ||
fd57770d KG |
25 | /* release the clcsock that is assigned to the smc_sock */ |
26 | void smc_clcsock_release(struct smc_sock *smc) | |
27 | { | |
28 | struct socket *tcp; | |
29 | ||
30 | if (smc->listen_smc && current_work() != &smc->smc_listen_work) | |
31 | cancel_work_sync(&smc->smc_listen_work); | |
32 | mutex_lock(&smc->clcsock_release_lock); | |
33 | if (smc->clcsock) { | |
34 | tcp = smc->clcsock; | |
35 | smc->clcsock = NULL; | |
36 | sock_release(tcp); | |
37 | } | |
38 | mutex_unlock(&smc->clcsock_release_lock); | |
39 | } | |
40 | ||
b38d7324 UB |
41 | static void smc_close_cleanup_listen(struct sock *parent) |
42 | { | |
43 | struct sock *sk; | |
44 | ||
45 | /* Close non-accepted connections */ | |
46 | while ((sk = smc_accept_dequeue(parent, NULL))) | |
47 | smc_close_non_accepted(sk); | |
48 | } | |
49 | ||
b38d7324 UB |
50 | /* wait for sndbuf data being transmitted */ |
51 | static void smc_close_stream_wait(struct smc_sock *smc, long timeout) | |
52 | { | |
53 | DEFINE_WAIT_FUNC(wait, woken_wake_function); | |
54 | struct sock *sk = &smc->sk; | |
55 | ||
56 | if (!timeout) | |
57 | return; | |
58 | ||
59 | if (!smc_tx_prepared_sends(&smc->conn)) | |
60 | return; | |
61 | ||
62 | smc->wait_close_tx_prepared = 1; | |
63 | add_wait_queue(sk_sleep(sk), &wait); | |
64 | while (!signal_pending(current) && timeout) { | |
65 | int rc; | |
66 | ||
67 | rc = sk_wait_event(sk, &timeout, | |
68 | !smc_tx_prepared_sends(&smc->conn) || | |
d18963cf | 69 | sk->sk_err == ECONNABORTED || |
b2900980 UB |
70 | sk->sk_err == ECONNRESET || |
71 | smc->conn.killed, | |
b38d7324 UB |
72 | &wait); |
73 | if (rc) | |
74 | break; | |
75 | } | |
76 | remove_wait_queue(sk_sleep(sk), &wait); | |
77 | smc->wait_close_tx_prepared = 0; | |
78 | } | |
79 | ||
80 | void smc_close_wake_tx_prepared(struct smc_sock *smc) | |
81 | { | |
82 | if (smc->wait_close_tx_prepared) | |
83 | /* wake up socket closing */ | |
84 | smc->sk.sk_state_change(&smc->sk); | |
85 | } | |
86 | ||
87 | static int smc_close_wr(struct smc_connection *conn) | |
88 | { | |
89 | conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1; | |
90 | ||
91 | return smc_cdc_get_slot_and_msg_send(conn); | |
92 | } | |
93 | ||
94 | static int smc_close_final(struct smc_connection *conn) | |
95 | { | |
96 | if (atomic_read(&conn->bytes_to_rcv)) | |
97 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; | |
98 | else | |
99 | conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1; | |
b2900980 UB |
100 | if (conn->killed) |
101 | return -EPIPE; | |
b38d7324 UB |
102 | |
103 | return smc_cdc_get_slot_and_msg_send(conn); | |
104 | } | |
105 | ||
83179760 | 106 | int smc_close_abort(struct smc_connection *conn) |
b38d7324 UB |
107 | { |
108 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; | |
109 | ||
110 | return smc_cdc_get_slot_and_msg_send(conn); | |
111 | } | |
112 | ||
113 | /* terminate smc socket abnormally - active abort | |
732720fa | 114 | * link group is terminated, i.e. RDMA communication no longer possible |
b38d7324 | 115 | */ |
81cf4f47 | 116 | void smc_close_active_abort(struct smc_sock *smc) |
b38d7324 | 117 | { |
3163c507 | 118 | struct sock *sk = &smc->sk; |
81cf4f47 | 119 | bool release_clcsock = false; |
3163c507 | 120 | |
dd65d87a UB |
121 | if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) { |
122 | sk->sk_err = ECONNABORTED; | |
83179760 UB |
123 | if (smc->clcsock && smc->clcsock->sk) |
124 | tcp_abort(smc->clcsock->sk, ECONNABORTED); | |
b38d7324 | 125 | } |
3163c507 | 126 | switch (sk->sk_state) { |
46c28dbd | 127 | case SMC_ACTIVE: |
3163c507 | 128 | sk->sk_state = SMC_PEERABORTWAIT; |
611b63a1 UB |
129 | release_sock(sk); |
130 | cancel_delayed_work_sync(&smc->conn.tx_work); | |
131 | lock_sock(sk); | |
d18963cf | 132 | sk->sk_state = SMC_CLOSED; |
51f1de79 | 133 | sock_put(sk); /* passive closing */ |
b38d7324 UB |
134 | break; |
135 | case SMC_APPCLOSEWAIT1: | |
136 | case SMC_APPCLOSEWAIT2: | |
611b63a1 UB |
137 | release_sock(sk); |
138 | cancel_delayed_work_sync(&smc->conn.tx_work); | |
139 | lock_sock(sk); | |
d18963cf | 140 | sk->sk_state = SMC_CLOSED; |
81cf4f47 | 141 | sock_put(sk); /* postponed passive closing */ |
b38d7324 UB |
142 | break; |
143 | case SMC_PEERCLOSEWAIT1: | |
144 | case SMC_PEERCLOSEWAIT2: | |
d18963cf UB |
145 | case SMC_PEERFINCLOSEWAIT: |
146 | sk->sk_state = SMC_CLOSED; | |
81cf4f47 UB |
147 | smc_conn_free(&smc->conn); |
148 | release_clcsock = true; | |
51f1de79 | 149 | sock_put(sk); /* passive closing */ |
b38d7324 UB |
150 | break; |
151 | case SMC_PROCESSABORT: | |
152 | case SMC_APPFINCLOSEWAIT: | |
3163c507 | 153 | sk->sk_state = SMC_CLOSED; |
b38d7324 | 154 | break; |
dd65d87a | 155 | case SMC_INIT: |
b38d7324 UB |
156 | case SMC_PEERABORTWAIT: |
157 | case SMC_CLOSED: | |
158 | break; | |
159 | } | |
160 | ||
3163c507 UB |
161 | sock_set_flag(sk, SOCK_DEAD); |
162 | sk->sk_state_change(sk); | |
81cf4f47 UB |
163 | |
164 | if (release_clcsock) { | |
165 | release_sock(sk); | |
166 | smc_clcsock_release(smc); | |
167 | lock_sock(sk); | |
168 | } | |
b38d7324 UB |
169 | } |
170 | ||
a98bf8c0 UB |
171 | static inline bool smc_close_sent_any_close(struct smc_connection *conn) |
172 | { | |
173 | return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort || | |
174 | conn->local_tx_ctrl.conn_state_flags.peer_conn_closed; | |
175 | } | |
176 | ||
b38d7324 UB |
177 | int smc_close_active(struct smc_sock *smc) |
178 | { | |
179 | struct smc_cdc_conn_state_flags *txflags = | |
180 | &smc->conn.local_tx_ctrl.conn_state_flags; | |
b38d7324 UB |
181 | struct smc_connection *conn = &smc->conn; |
182 | struct sock *sk = &smc->sk; | |
183 | int old_state; | |
8c96feee | 184 | long timeout; |
b38d7324 UB |
185 | int rc = 0; |
186 | ||
8c96feee UB |
187 | timeout = current->flags & PF_EXITING ? |
188 | 0 : sock_flag(sk, SOCK_LINGER) ? | |
189 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; | |
b38d7324 | 190 | |
b38d7324 | 191 | old_state = sk->sk_state; |
bbb96bf2 UB |
192 | again: |
193 | switch (sk->sk_state) { | |
b38d7324 UB |
194 | case SMC_INIT: |
195 | sk->sk_state = SMC_CLOSED; | |
b38d7324 UB |
196 | break; |
197 | case SMC_LISTEN: | |
198 | sk->sk_state = SMC_CLOSED; | |
199 | sk->sk_state_change(sk); /* wake up accept */ | |
200 | if (smc->clcsock && smc->clcsock->sk) { | |
201 | rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR); | |
202 | /* wake up kernel_accept of smc_tcp_listen_worker */ | |
203 | smc->clcsock->sk->sk_data_ready(smc->clcsock->sk); | |
204 | } | |
b38d7324 | 205 | smc_close_cleanup_listen(sk); |
3d502067 UB |
206 | release_sock(sk); |
207 | flush_work(&smc->tcp_listen_work); | |
208 | lock_sock(sk); | |
b38d7324 UB |
209 | break; |
210 | case SMC_ACTIVE: | |
211 | smc_close_stream_wait(smc, timeout); | |
212 | release_sock(sk); | |
18e537cd | 213 | cancel_delayed_work_sync(&conn->tx_work); |
b38d7324 UB |
214 | lock_sock(sk); |
215 | if (sk->sk_state == SMC_ACTIVE) { | |
216 | /* send close request */ | |
217 | rc = smc_close_final(conn); | |
218 | sk->sk_state = SMC_PEERCLOSEWAIT1; | |
219 | } else { | |
220 | /* peer event has changed the state */ | |
221 | goto again; | |
222 | } | |
223 | break; | |
224 | case SMC_APPFINCLOSEWAIT: | |
225 | /* socket already shutdown wr or both (active close) */ | |
226 | if (txflags->peer_done_writing && | |
a98bf8c0 | 227 | !smc_close_sent_any_close(conn)) { |
b38d7324 UB |
228 | /* just shutdown wr done, send close request */ |
229 | rc = smc_close_final(conn); | |
230 | } | |
231 | sk->sk_state = SMC_CLOSED; | |
b38d7324 UB |
232 | break; |
233 | case SMC_APPCLOSEWAIT1: | |
234 | case SMC_APPCLOSEWAIT2: | |
235 | if (!smc_cdc_rxed_any_close(conn)) | |
236 | smc_close_stream_wait(smc, timeout); | |
237 | release_sock(sk); | |
18e537cd | 238 | cancel_delayed_work_sync(&conn->tx_work); |
b38d7324 | 239 | lock_sock(sk); |
bbb96bf2 UB |
240 | if (sk->sk_state != SMC_APPCLOSEWAIT1 && |
241 | sk->sk_state != SMC_APPCLOSEWAIT2) | |
242 | goto again; | |
243 | /* confirm close from peer */ | |
244 | rc = smc_close_final(conn); | |
51f1de79 | 245 | if (smc_cdc_rxed_any_close(conn)) { |
b38d7324 UB |
246 | /* peer has closed the socket already */ |
247 | sk->sk_state = SMC_CLOSED; | |
51f1de79 UB |
248 | sock_put(sk); /* postponed passive closing */ |
249 | } else { | |
b38d7324 UB |
250 | /* peer has just issued a shutdown write */ |
251 | sk->sk_state = SMC_PEERFINCLOSEWAIT; | |
51f1de79 | 252 | } |
b38d7324 UB |
253 | break; |
254 | case SMC_PEERCLOSEWAIT1: | |
255 | case SMC_PEERCLOSEWAIT2: | |
a98bf8c0 UB |
256 | if (txflags->peer_done_writing && |
257 | !smc_close_sent_any_close(conn)) { | |
258 | /* just shutdown wr done, send close request */ | |
259 | rc = smc_close_final(conn); | |
260 | } | |
261 | /* peer sending PeerConnectionClosed will cause transition */ | |
262 | break; | |
b38d7324 UB |
263 | case SMC_PEERFINCLOSEWAIT: |
264 | /* peer sending PeerConnectionClosed will cause transition */ | |
265 | break; | |
266 | case SMC_PROCESSABORT: | |
d18963cf | 267 | rc = smc_close_abort(conn); |
b38d7324 | 268 | sk->sk_state = SMC_CLOSED; |
b38d7324 UB |
269 | break; |
270 | case SMC_PEERABORTWAIT: | |
d18963cf UB |
271 | sk->sk_state = SMC_CLOSED; |
272 | break; | |
b38d7324 UB |
273 | case SMC_CLOSED: |
274 | /* nothing to do, add tracing in future patch */ | |
275 | break; | |
276 | } | |
277 | ||
278 | if (old_state != sk->sk_state) | |
3163c507 | 279 | sk->sk_state_change(sk); |
b38d7324 UB |
280 | return rc; |
281 | } | |
282 | ||
283 | static void smc_close_passive_abort_received(struct smc_sock *smc) | |
284 | { | |
285 | struct smc_cdc_conn_state_flags *txflags = | |
286 | &smc->conn.local_tx_ctrl.conn_state_flags; | |
287 | struct sock *sk = &smc->sk; | |
288 | ||
289 | switch (sk->sk_state) { | |
51f1de79 | 290 | case SMC_INIT: |
b38d7324 | 291 | case SMC_ACTIVE: |
b38d7324 | 292 | case SMC_APPCLOSEWAIT1: |
51f1de79 UB |
293 | sk->sk_state = SMC_PROCESSABORT; |
294 | sock_put(sk); /* passive closing */ | |
295 | break; | |
296 | case SMC_APPFINCLOSEWAIT: | |
b38d7324 UB |
297 | sk->sk_state = SMC_PROCESSABORT; |
298 | break; | |
299 | case SMC_PEERCLOSEWAIT1: | |
300 | case SMC_PEERCLOSEWAIT2: | |
301 | if (txflags->peer_done_writing && | |
51f1de79 | 302 | !smc_close_sent_any_close(&smc->conn)) |
b38d7324 | 303 | /* just shutdown, but not yet closed locally */ |
b38d7324 | 304 | sk->sk_state = SMC_PROCESSABORT; |
51f1de79 | 305 | else |
b38d7324 | 306 | sk->sk_state = SMC_CLOSED; |
51f1de79 | 307 | sock_put(sk); /* passive closing */ |
b38d7324 | 308 | break; |
51f1de79 | 309 | case SMC_APPCLOSEWAIT2: |
b38d7324 | 310 | case SMC_PEERFINCLOSEWAIT: |
51f1de79 UB |
311 | sk->sk_state = SMC_CLOSED; |
312 | sock_put(sk); /* passive closing */ | |
313 | break; | |
b38d7324 UB |
314 | case SMC_PEERABORTWAIT: |
315 | sk->sk_state = SMC_CLOSED; | |
316 | break; | |
b38d7324 UB |
317 | case SMC_PROCESSABORT: |
318 | /* nothing to do, add tracing in future patch */ | |
319 | break; | |
320 | } | |
321 | } | |
322 | ||
732720fa UB |
323 | /* Either some kind of closing has been received: peer_conn_closed, |
324 | * peer_conn_abort, or peer_done_writing | |
325 | * or the link group of the connection terminates abnormally. | |
b38d7324 | 326 | */ |
46c28dbd | 327 | static void smc_close_passive_work(struct work_struct *work) |
b38d7324 | 328 | { |
46c28dbd UB |
329 | struct smc_connection *conn = container_of(work, |
330 | struct smc_connection, | |
331 | close_work); | |
332 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); | |
333 | struct smc_cdc_conn_state_flags *rxflags; | |
fd57770d | 334 | bool release_clcsock = false; |
b38d7324 UB |
335 | struct sock *sk = &smc->sk; |
336 | int old_state; | |
337 | ||
3163c507 | 338 | lock_sock(sk); |
b38d7324 UB |
339 | old_state = sk->sk_state; |
340 | ||
3163c507 | 341 | rxflags = &conn->local_rx_ctrl.conn_state_flags; |
b38d7324 | 342 | if (rxflags->peer_conn_abort) { |
732720fa | 343 | /* peer has not received all data */ |
b38d7324 | 344 | smc_close_passive_abort_received(smc); |
611b63a1 UB |
345 | release_sock(&smc->sk); |
346 | cancel_delayed_work_sync(&conn->tx_work); | |
347 | lock_sock(&smc->sk); | |
b38d7324 UB |
348 | goto wakeup; |
349 | } | |
350 | ||
351 | switch (sk->sk_state) { | |
352 | case SMC_INIT: | |
84b799a2 | 353 | sk->sk_state = SMC_APPCLOSEWAIT1; |
b38d7324 UB |
354 | break; |
355 | case SMC_ACTIVE: | |
356 | sk->sk_state = SMC_APPCLOSEWAIT1; | |
51f1de79 UB |
357 | /* postpone sock_put() for passive closing to cover |
358 | * received SEND_SHUTDOWN as well | |
359 | */ | |
b38d7324 UB |
360 | break; |
361 | case SMC_PEERCLOSEWAIT1: | |
362 | if (rxflags->peer_done_writing) | |
363 | sk->sk_state = SMC_PEERCLOSEWAIT2; | |
7f6b437e GS |
364 | /* fall through */ |
365 | /* to check for closing */ | |
b38d7324 | 366 | case SMC_PEERCLOSEWAIT2: |
3163c507 | 367 | if (!smc_cdc_rxed_any_close(conn)) |
b38d7324 UB |
368 | break; |
369 | if (sock_flag(sk, SOCK_DEAD) && | |
a98bf8c0 | 370 | smc_close_sent_any_close(conn)) { |
b38d7324 UB |
371 | /* smc_release has already been called locally */ |
372 | sk->sk_state = SMC_CLOSED; | |
373 | } else { | |
374 | /* just shutdown, but not yet closed locally */ | |
375 | sk->sk_state = SMC_APPFINCLOSEWAIT; | |
376 | } | |
51f1de79 | 377 | sock_put(sk); /* passive closing */ |
b38d7324 | 378 | break; |
5ac92a00 | 379 | case SMC_PEERFINCLOSEWAIT: |
51f1de79 | 380 | if (smc_cdc_rxed_any_close(conn)) { |
5ac92a00 | 381 | sk->sk_state = SMC_CLOSED; |
51f1de79 UB |
382 | sock_put(sk); /* passive closing */ |
383 | } | |
5ac92a00 | 384 | break; |
b38d7324 UB |
385 | case SMC_APPCLOSEWAIT1: |
386 | case SMC_APPCLOSEWAIT2: | |
51f1de79 UB |
387 | /* postpone sock_put() for passive closing to cover |
388 | * received SEND_SHUTDOWN as well | |
389 | */ | |
390 | break; | |
b38d7324 UB |
391 | case SMC_APPFINCLOSEWAIT: |
392 | case SMC_PEERABORTWAIT: | |
393 | case SMC_PROCESSABORT: | |
394 | case SMC_CLOSED: | |
395 | /* nothing to do, add tracing in future patch */ | |
396 | break; | |
397 | } | |
398 | ||
399 | wakeup: | |
b38d7324 UB |
400 | sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */ |
401 | sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */ | |
402 | ||
a98bf8c0 UB |
403 | if (old_state != sk->sk_state) { |
404 | sk->sk_state_change(sk); | |
405 | if ((sk->sk_state == SMC_CLOSED) && | |
b03faa1f | 406 | (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) { |
3163c507 | 407 | smc_conn_free(conn); |
fd57770d KG |
408 | if (smc->clcsock) |
409 | release_clcsock = true; | |
b03faa1f | 410 | } |
b38d7324 | 411 | } |
3163c507 | 412 | release_sock(sk); |
fd57770d KG |
413 | if (release_clcsock) |
414 | smc_clcsock_release(smc); | |
51f1de79 | 415 | sock_put(sk); /* sock_hold done by schedulers of close_work */ |
b38d7324 UB |
416 | } |
417 | ||
418 | int smc_close_shutdown_write(struct smc_sock *smc) | |
419 | { | |
420 | struct smc_connection *conn = &smc->conn; | |
b38d7324 UB |
421 | struct sock *sk = &smc->sk; |
422 | int old_state; | |
8c96feee | 423 | long timeout; |
b38d7324 UB |
424 | int rc = 0; |
425 | ||
8c96feee UB |
426 | timeout = current->flags & PF_EXITING ? |
427 | 0 : sock_flag(sk, SOCK_LINGER) ? | |
428 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; | |
b38d7324 | 429 | |
b38d7324 | 430 | old_state = sk->sk_state; |
bbb96bf2 UB |
431 | again: |
432 | switch (sk->sk_state) { | |
b38d7324 UB |
433 | case SMC_ACTIVE: |
434 | smc_close_stream_wait(smc, timeout); | |
435 | release_sock(sk); | |
18e537cd | 436 | cancel_delayed_work_sync(&conn->tx_work); |
b38d7324 | 437 | lock_sock(sk); |
bbb96bf2 UB |
438 | if (sk->sk_state != SMC_ACTIVE) |
439 | goto again; | |
b38d7324 UB |
440 | /* send close wr request */ |
441 | rc = smc_close_wr(conn); | |
bbb96bf2 | 442 | sk->sk_state = SMC_PEERCLOSEWAIT1; |
b38d7324 UB |
443 | break; |
444 | case SMC_APPCLOSEWAIT1: | |
445 | /* passive close */ | |
446 | if (!smc_cdc_rxed_any_close(conn)) | |
447 | smc_close_stream_wait(smc, timeout); | |
448 | release_sock(sk); | |
18e537cd | 449 | cancel_delayed_work_sync(&conn->tx_work); |
b38d7324 | 450 | lock_sock(sk); |
bbb96bf2 UB |
451 | if (sk->sk_state != SMC_APPCLOSEWAIT1) |
452 | goto again; | |
b38d7324 UB |
453 | /* confirm close from peer */ |
454 | rc = smc_close_wr(conn); | |
455 | sk->sk_state = SMC_APPCLOSEWAIT2; | |
456 | break; | |
457 | case SMC_APPCLOSEWAIT2: | |
458 | case SMC_PEERFINCLOSEWAIT: | |
459 | case SMC_PEERCLOSEWAIT1: | |
460 | case SMC_PEERCLOSEWAIT2: | |
461 | case SMC_APPFINCLOSEWAIT: | |
462 | case SMC_PROCESSABORT: | |
463 | case SMC_PEERABORTWAIT: | |
464 | /* nothing to do, add tracing in future patch */ | |
465 | break; | |
466 | } | |
467 | ||
468 | if (old_state != sk->sk_state) | |
3163c507 | 469 | sk->sk_state_change(sk); |
b38d7324 UB |
470 | return rc; |
471 | } | |
46c28dbd UB |
472 | |
473 | /* Initialize close properties on connection establishment. */ | |
474 | void smc_close_init(struct smc_sock *smc) | |
475 | { | |
476 | INIT_WORK(&smc->conn.close_work, smc_close_passive_work); | |
477 | } |