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