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