]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/smc/smc_close.c
net/rds: correct socket tunable error in rds_tcp_tune()
[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
UB
197 int rc = 0;
198
8c96feee
UB
199 timeout = current->flags & PF_EXITING ?
200 0 : sock_flag(sk, SOCK_LINGER) ?
201 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
b38d7324 202
b38d7324 203 old_state = sk->sk_state;
bbb96bf2
UB
204again:
205 switch (sk->sk_state) {
b38d7324
UB
206 case SMC_INIT:
207 sk->sk_state = SMC_CLOSED;
b38d7324
UB
208 break;
209 case SMC_LISTEN:
210 sk->sk_state = SMC_CLOSED;
211 sk->sk_state_change(sk); /* wake up accept */
ddcc9b7f
KG
212 if (smc->clcsock && smc->clcsock->sk) {
213 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
214 smc->clcsock->sk->sk_user_data = NULL;
b38d7324 215 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
ddcc9b7f 216 }
b38d7324 217 smc_close_cleanup_listen(sk);
3d502067
UB
218 release_sock(sk);
219 flush_work(&smc->tcp_listen_work);
220 lock_sock(sk);
b38d7324
UB
221 break;
222 case SMC_ACTIVE:
223 smc_close_stream_wait(smc, timeout);
224 release_sock(sk);
18e537cd 225 cancel_delayed_work_sync(&conn->tx_work);
b38d7324
UB
226 lock_sock(sk);
227 if (sk->sk_state == SMC_ACTIVE) {
228 /* send close request */
229 rc = smc_close_final(conn);
230 sk->sk_state = SMC_PEERCLOSEWAIT1;
e24fce04
TL
231
232 /* actively shutdown clcsock before peer close it,
233 * prevent peer from entering TIME_WAIT state.
234 */
235 if (smc->clcsock && smc->clcsock->sk)
236 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
b38d7324
UB
237 } else {
238 /* peer event has changed the state */
239 goto again;
240 }
241 break;
242 case SMC_APPFINCLOSEWAIT:
243 /* socket already shutdown wr or both (active close) */
244 if (txflags->peer_done_writing &&
a98bf8c0 245 !smc_close_sent_any_close(conn)) {
b38d7324
UB
246 /* just shutdown wr done, send close request */
247 rc = smc_close_final(conn);
248 }
249 sk->sk_state = SMC_CLOSED;
b38d7324
UB
250 break;
251 case SMC_APPCLOSEWAIT1:
252 case SMC_APPCLOSEWAIT2:
253 if (!smc_cdc_rxed_any_close(conn))
254 smc_close_stream_wait(smc, timeout);
255 release_sock(sk);
18e537cd 256 cancel_delayed_work_sync(&conn->tx_work);
b38d7324 257 lock_sock(sk);
bbb96bf2
UB
258 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
259 sk->sk_state != SMC_APPCLOSEWAIT2)
260 goto again;
261 /* confirm close from peer */
262 rc = smc_close_final(conn);
51f1de79 263 if (smc_cdc_rxed_any_close(conn)) {
b38d7324
UB
264 /* peer has closed the socket already */
265 sk->sk_state = SMC_CLOSED;
51f1de79
UB
266 sock_put(sk); /* postponed passive closing */
267 } else {
b38d7324
UB
268 /* peer has just issued a shutdown write */
269 sk->sk_state = SMC_PEERFINCLOSEWAIT;
51f1de79 270 }
b38d7324
UB
271 break;
272 case SMC_PEERCLOSEWAIT1:
273 case SMC_PEERCLOSEWAIT2:
a98bf8c0
UB
274 if (txflags->peer_done_writing &&
275 !smc_close_sent_any_close(conn)) {
276 /* just shutdown wr done, send close request */
277 rc = smc_close_final(conn);
278 }
279 /* peer sending PeerConnectionClosed will cause transition */
280 break;
b38d7324
UB
281 case SMC_PEERFINCLOSEWAIT:
282 /* peer sending PeerConnectionClosed will cause transition */
283 break;
284 case SMC_PROCESSABORT:
d18963cf 285 rc = smc_close_abort(conn);
b38d7324 286 sk->sk_state = SMC_CLOSED;
b38d7324
UB
287 break;
288 case SMC_PEERABORTWAIT:
d18963cf
UB
289 sk->sk_state = SMC_CLOSED;
290 break;
b38d7324
UB
291 case SMC_CLOSED:
292 /* nothing to do, add tracing in future patch */
293 break;
294 }
295
296 if (old_state != sk->sk_state)
3163c507 297 sk->sk_state_change(sk);
b38d7324
UB
298 return rc;
299}
300
301static void smc_close_passive_abort_received(struct smc_sock *smc)
302{
303 struct smc_cdc_conn_state_flags *txflags =
304 &smc->conn.local_tx_ctrl.conn_state_flags;
305 struct sock *sk = &smc->sk;
306
307 switch (sk->sk_state) {
51f1de79 308 case SMC_INIT:
b38d7324 309 case SMC_ACTIVE:
b38d7324 310 case SMC_APPCLOSEWAIT1:
51f1de79
UB
311 sk->sk_state = SMC_PROCESSABORT;
312 sock_put(sk); /* passive closing */
313 break;
314 case SMC_APPFINCLOSEWAIT:
b38d7324
UB
315 sk->sk_state = SMC_PROCESSABORT;
316 break;
317 case SMC_PEERCLOSEWAIT1:
318 case SMC_PEERCLOSEWAIT2:
319 if (txflags->peer_done_writing &&
51f1de79 320 !smc_close_sent_any_close(&smc->conn))
b38d7324 321 /* just shutdown, but not yet closed locally */
b38d7324 322 sk->sk_state = SMC_PROCESSABORT;
51f1de79 323 else
b38d7324 324 sk->sk_state = SMC_CLOSED;
51f1de79 325 sock_put(sk); /* passive closing */
b38d7324 326 break;
51f1de79 327 case SMC_APPCLOSEWAIT2:
b38d7324 328 case SMC_PEERFINCLOSEWAIT:
51f1de79
UB
329 sk->sk_state = SMC_CLOSED;
330 sock_put(sk); /* passive closing */
331 break;
b38d7324
UB
332 case SMC_PEERABORTWAIT:
333 sk->sk_state = SMC_CLOSED;
334 break;
b38d7324
UB
335 case SMC_PROCESSABORT:
336 /* nothing to do, add tracing in future patch */
337 break;
338 }
339}
340
732720fa
UB
341/* Either some kind of closing has been received: peer_conn_closed,
342 * peer_conn_abort, or peer_done_writing
343 * or the link group of the connection terminates abnormally.
b38d7324 344 */
46c28dbd 345static void smc_close_passive_work(struct work_struct *work)
b38d7324 346{
46c28dbd
UB
347 struct smc_connection *conn = container_of(work,
348 struct smc_connection,
349 close_work);
350 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
351 struct smc_cdc_conn_state_flags *rxflags;
fd57770d 352 bool release_clcsock = false;
b38d7324
UB
353 struct sock *sk = &smc->sk;
354 int old_state;
355
3163c507 356 lock_sock(sk);
b38d7324
UB
357 old_state = sk->sk_state;
358
3163c507 359 rxflags = &conn->local_rx_ctrl.conn_state_flags;
b38d7324 360 if (rxflags->peer_conn_abort) {
732720fa 361 /* peer has not received all data */
b38d7324 362 smc_close_passive_abort_received(smc);
611b63a1
UB
363 release_sock(&smc->sk);
364 cancel_delayed_work_sync(&conn->tx_work);
365 lock_sock(&smc->sk);
b38d7324
UB
366 goto wakeup;
367 }
368
369 switch (sk->sk_state) {
370 case SMC_INIT:
84b799a2 371 sk->sk_state = SMC_APPCLOSEWAIT1;
b38d7324
UB
372 break;
373 case SMC_ACTIVE:
374 sk->sk_state = SMC_APPCLOSEWAIT1;
51f1de79
UB
375 /* postpone sock_put() for passive closing to cover
376 * received SEND_SHUTDOWN as well
377 */
b38d7324
UB
378 break;
379 case SMC_PEERCLOSEWAIT1:
380 if (rxflags->peer_done_writing)
381 sk->sk_state = SMC_PEERCLOSEWAIT2;
df561f66 382 fallthrough;
7f6b437e 383 /* to check for closing */
b38d7324 384 case SMC_PEERCLOSEWAIT2:
3163c507 385 if (!smc_cdc_rxed_any_close(conn))
b38d7324
UB
386 break;
387 if (sock_flag(sk, SOCK_DEAD) &&
a98bf8c0 388 smc_close_sent_any_close(conn)) {
b38d7324
UB
389 /* smc_release has already been called locally */
390 sk->sk_state = SMC_CLOSED;
391 } else {
392 /* just shutdown, but not yet closed locally */
393 sk->sk_state = SMC_APPFINCLOSEWAIT;
394 }
51f1de79 395 sock_put(sk); /* passive closing */
b38d7324 396 break;
5ac92a00 397 case SMC_PEERFINCLOSEWAIT:
51f1de79 398 if (smc_cdc_rxed_any_close(conn)) {
5ac92a00 399 sk->sk_state = SMC_CLOSED;
51f1de79
UB
400 sock_put(sk); /* passive closing */
401 }
5ac92a00 402 break;
b38d7324
UB
403 case SMC_APPCLOSEWAIT1:
404 case SMC_APPCLOSEWAIT2:
51f1de79
UB
405 /* postpone sock_put() for passive closing to cover
406 * received SEND_SHUTDOWN as well
407 */
408 break;
b38d7324
UB
409 case SMC_APPFINCLOSEWAIT:
410 case SMC_PEERABORTWAIT:
411 case SMC_PROCESSABORT:
412 case SMC_CLOSED:
413 /* nothing to do, add tracing in future patch */
414 break;
415 }
416
417wakeup:
b38d7324
UB
418 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
419 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
420
a98bf8c0
UB
421 if (old_state != sk->sk_state) {
422 sk->sk_state_change(sk);
423 if ((sk->sk_state == SMC_CLOSED) &&
b03faa1f 424 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
3163c507 425 smc_conn_free(conn);
fd57770d
KG
426 if (smc->clcsock)
427 release_clcsock = true;
b03faa1f 428 }
b38d7324 429 }
3163c507 430 release_sock(sk);
fd57770d
KG
431 if (release_clcsock)
432 smc_clcsock_release(smc);
51f1de79 433 sock_put(sk); /* sock_hold done by schedulers of close_work */
b38d7324
UB
434}
435
436int smc_close_shutdown_write(struct smc_sock *smc)
437{
438 struct smc_connection *conn = &smc->conn;
b38d7324
UB
439 struct sock *sk = &smc->sk;
440 int old_state;
8c96feee 441 long timeout;
b38d7324
UB
442 int rc = 0;
443
8c96feee
UB
444 timeout = current->flags & PF_EXITING ?
445 0 : sock_flag(sk, SOCK_LINGER) ?
446 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
b38d7324 447
b38d7324 448 old_state = sk->sk_state;
bbb96bf2
UB
449again:
450 switch (sk->sk_state) {
b38d7324
UB
451 case SMC_ACTIVE:
452 smc_close_stream_wait(smc, timeout);
453 release_sock(sk);
18e537cd 454 cancel_delayed_work_sync(&conn->tx_work);
b38d7324 455 lock_sock(sk);
bbb96bf2
UB
456 if (sk->sk_state != SMC_ACTIVE)
457 goto again;
b38d7324
UB
458 /* send close wr request */
459 rc = smc_close_wr(conn);
bbb96bf2 460 sk->sk_state = SMC_PEERCLOSEWAIT1;
b38d7324
UB
461 break;
462 case SMC_APPCLOSEWAIT1:
463 /* passive close */
464 if (!smc_cdc_rxed_any_close(conn))
465 smc_close_stream_wait(smc, timeout);
466 release_sock(sk);
18e537cd 467 cancel_delayed_work_sync(&conn->tx_work);
b38d7324 468 lock_sock(sk);
bbb96bf2
UB
469 if (sk->sk_state != SMC_APPCLOSEWAIT1)
470 goto again;
b38d7324
UB
471 /* confirm close from peer */
472 rc = smc_close_wr(conn);
473 sk->sk_state = SMC_APPCLOSEWAIT2;
474 break;
475 case SMC_APPCLOSEWAIT2:
476 case SMC_PEERFINCLOSEWAIT:
477 case SMC_PEERCLOSEWAIT1:
478 case SMC_PEERCLOSEWAIT2:
479 case SMC_APPFINCLOSEWAIT:
480 case SMC_PROCESSABORT:
481 case SMC_PEERABORTWAIT:
482 /* nothing to do, add tracing in future patch */
483 break;
484 }
485
486 if (old_state != sk->sk_state)
3163c507 487 sk->sk_state_change(sk);
b38d7324
UB
488 return rc;
489}
46c28dbd
UB
490
491/* Initialize close properties on connection establishment. */
492void smc_close_init(struct smc_sock *smc)
493{
494 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
495}