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