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