1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Socket Closing - normal and abnormal
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
12 #include <linux/workqueue.h>
13 #include <linux/sched/signal.h>
21 #include "smc_close.h"
23 /* release the clcsock that is assigned to the smc_sock */
24 void smc_clcsock_release(struct smc_sock
*smc
)
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
);
36 mutex_unlock(&smc
->clcsock_release_lock
);
39 static void smc_close_cleanup_listen(struct sock
*parent
)
43 /* Close non-accepted connections */
44 while ((sk
= smc_accept_dequeue(parent
, NULL
)))
45 smc_close_non_accepted(sk
);
48 /* wait for sndbuf data being transmitted */
49 static void smc_close_stream_wait(struct smc_sock
*smc
, long timeout
)
51 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
52 struct sock
*sk
= &smc
->sk
;
57 if (!smc_tx_prepared_sends(&smc
->conn
))
60 smc
->wait_close_tx_prepared
= 1;
61 add_wait_queue(sk_sleep(sk
), &wait
);
62 while (!signal_pending(current
) && timeout
) {
65 rc
= sk_wait_event(sk
, &timeout
,
66 !smc_tx_prepared_sends(&smc
->conn
) ||
67 sk
->sk_err
== ECONNABORTED
||
68 sk
->sk_err
== ECONNRESET
||
74 remove_wait_queue(sk_sleep(sk
), &wait
);
75 smc
->wait_close_tx_prepared
= 0;
78 void smc_close_wake_tx_prepared(struct smc_sock
*smc
)
80 if (smc
->wait_close_tx_prepared
)
81 /* wake up socket closing */
82 smc
->sk
.sk_state_change(&smc
->sk
);
85 static int smc_close_wr(struct smc_connection
*conn
)
87 conn
->local_tx_ctrl
.conn_state_flags
.peer_done_writing
= 1;
89 return smc_cdc_get_slot_and_msg_send(conn
);
92 static int smc_close_final(struct smc_connection
*conn
)
94 if (atomic_read(&conn
->bytes_to_rcv
))
95 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
97 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_closed
= 1;
101 return smc_cdc_get_slot_and_msg_send(conn
);
104 int smc_close_abort(struct smc_connection
*conn
)
106 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
108 return smc_cdc_get_slot_and_msg_send(conn
);
111 static void smc_close_cancel_work(struct smc_sock
*smc
)
113 struct sock
*sk
= &smc
->sk
;
116 cancel_work_sync(&smc
->conn
.close_work
);
117 cancel_delayed_work_sync(&smc
->conn
.tx_work
);
119 sk
->sk_state
= SMC_CLOSED
;
122 /* terminate smc socket abnormally - active abort
123 * link group is terminated, i.e. RDMA communication no longer possible
125 void smc_close_active_abort(struct smc_sock
*smc
)
127 struct sock
*sk
= &smc
->sk
;
128 bool release_clcsock
= false;
130 if (sk
->sk_state
!= SMC_INIT
&& smc
->clcsock
&& smc
->clcsock
->sk
) {
131 sk
->sk_err
= ECONNABORTED
;
132 if (smc
->clcsock
&& smc
->clcsock
->sk
)
133 tcp_abort(smc
->clcsock
->sk
, ECONNABORTED
);
135 switch (sk
->sk_state
) {
137 sk
->sk_state
= SMC_PEERABORTWAIT
;
138 smc_close_cancel_work(smc
);
139 sk
->sk_state
= SMC_CLOSED
;
140 sock_put(sk
); /* passive closing */
142 case SMC_APPCLOSEWAIT1
:
143 case SMC_APPCLOSEWAIT2
:
144 smc_close_cancel_work(smc
);
145 sk
->sk_state
= SMC_CLOSED
;
146 sock_put(sk
); /* postponed passive closing */
148 case SMC_PEERCLOSEWAIT1
:
149 case SMC_PEERCLOSEWAIT2
:
150 case SMC_PEERFINCLOSEWAIT
:
151 sk
->sk_state
= SMC_PEERABORTWAIT
;
152 smc_close_cancel_work(smc
);
153 sk
->sk_state
= SMC_CLOSED
;
154 smc_conn_free(&smc
->conn
);
155 release_clcsock
= true;
156 sock_put(sk
); /* passive closing */
158 case SMC_PROCESSABORT
:
159 case SMC_APPFINCLOSEWAIT
:
160 sk
->sk_state
= SMC_PEERABORTWAIT
;
161 smc_close_cancel_work(smc
);
162 sk
->sk_state
= SMC_CLOSED
;
163 smc_conn_free(&smc
->conn
);
164 release_clcsock
= true;
167 case SMC_PEERABORTWAIT
:
172 sock_set_flag(sk
, SOCK_DEAD
);
173 sk
->sk_state_change(sk
);
175 if (release_clcsock
) {
177 smc_clcsock_release(smc
);
182 static inline bool smc_close_sent_any_close(struct smc_connection
*conn
)
184 return conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
||
185 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_closed
;
188 int smc_close_active(struct smc_sock
*smc
)
190 struct smc_cdc_conn_state_flags
*txflags
=
191 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
192 struct smc_connection
*conn
= &smc
->conn
;
193 struct sock
*sk
= &smc
->sk
;
198 timeout
= current
->flags
& PF_EXITING
?
199 0 : sock_flag(sk
, SOCK_LINGER
) ?
200 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
202 old_state
= sk
->sk_state
;
204 switch (sk
->sk_state
) {
206 sk
->sk_state
= SMC_CLOSED
;
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
);
216 smc_close_cleanup_listen(sk
);
218 flush_work(&smc
->tcp_listen_work
);
222 smc_close_stream_wait(smc
, timeout
);
224 cancel_delayed_work_sync(&conn
->tx_work
);
226 if (sk
->sk_state
== SMC_ACTIVE
) {
227 /* send close request */
228 rc
= smc_close_final(conn
);
229 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
231 /* peer event has changed the state */
235 case SMC_APPFINCLOSEWAIT
:
236 /* socket already shutdown wr or both (active close) */
237 if (txflags
->peer_done_writing
&&
238 !smc_close_sent_any_close(conn
)) {
239 /* just shutdown wr done, send close request */
240 rc
= smc_close_final(conn
);
242 sk
->sk_state
= SMC_CLOSED
;
244 case SMC_APPCLOSEWAIT1
:
245 case SMC_APPCLOSEWAIT2
:
246 if (!smc_cdc_rxed_any_close(conn
))
247 smc_close_stream_wait(smc
, timeout
);
249 cancel_delayed_work_sync(&conn
->tx_work
);
251 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
&&
252 sk
->sk_state
!= SMC_APPCLOSEWAIT2
)
254 /* confirm close from peer */
255 rc
= smc_close_final(conn
);
256 if (smc_cdc_rxed_any_close(conn
)) {
257 /* peer has closed the socket already */
258 sk
->sk_state
= SMC_CLOSED
;
259 sock_put(sk
); /* postponed passive closing */
261 /* peer has just issued a shutdown write */
262 sk
->sk_state
= SMC_PEERFINCLOSEWAIT
;
265 case SMC_PEERCLOSEWAIT1
:
266 case SMC_PEERCLOSEWAIT2
:
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
);
272 /* peer sending PeerConnectionClosed will cause transition */
274 case SMC_PEERFINCLOSEWAIT
:
275 /* peer sending PeerConnectionClosed will cause transition */
277 case SMC_PROCESSABORT
:
278 rc
= smc_close_abort(conn
);
279 sk
->sk_state
= SMC_CLOSED
;
281 case SMC_PEERABORTWAIT
:
282 sk
->sk_state
= SMC_CLOSED
;
285 /* nothing to do, add tracing in future patch */
289 if (old_state
!= sk
->sk_state
)
290 sk
->sk_state_change(sk
);
294 static void smc_close_passive_abort_received(struct smc_sock
*smc
)
296 struct smc_cdc_conn_state_flags
*txflags
=
297 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
298 struct sock
*sk
= &smc
->sk
;
300 switch (sk
->sk_state
) {
303 case SMC_APPCLOSEWAIT1
:
304 sk
->sk_state
= SMC_PROCESSABORT
;
305 sock_put(sk
); /* passive closing */
307 case SMC_APPFINCLOSEWAIT
:
308 sk
->sk_state
= SMC_PROCESSABORT
;
310 case SMC_PEERCLOSEWAIT1
:
311 case SMC_PEERCLOSEWAIT2
:
312 if (txflags
->peer_done_writing
&&
313 !smc_close_sent_any_close(&smc
->conn
))
314 /* just shutdown, but not yet closed locally */
315 sk
->sk_state
= SMC_PROCESSABORT
;
317 sk
->sk_state
= SMC_CLOSED
;
318 sock_put(sk
); /* passive closing */
320 case SMC_APPCLOSEWAIT2
:
321 case SMC_PEERFINCLOSEWAIT
:
322 sk
->sk_state
= SMC_CLOSED
;
323 sock_put(sk
); /* passive closing */
325 case SMC_PEERABORTWAIT
:
326 sk
->sk_state
= SMC_CLOSED
;
328 case SMC_PROCESSABORT
:
329 /* nothing to do, add tracing in future patch */
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.
338 static void smc_close_passive_work(struct work_struct
*work
)
340 struct smc_connection
*conn
= container_of(work
,
341 struct smc_connection
,
343 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
344 struct smc_cdc_conn_state_flags
*rxflags
;
345 bool release_clcsock
= false;
346 struct sock
*sk
= &smc
->sk
;
350 old_state
= sk
->sk_state
;
352 rxflags
= &conn
->local_rx_ctrl
.conn_state_flags
;
353 if (rxflags
->peer_conn_abort
) {
354 /* peer has not received all data */
355 smc_close_passive_abort_received(smc
);
356 release_sock(&smc
->sk
);
357 cancel_delayed_work_sync(&conn
->tx_work
);
362 switch (sk
->sk_state
) {
364 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
367 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
368 /* postpone sock_put() for passive closing to cover
369 * received SEND_SHUTDOWN as well
372 case SMC_PEERCLOSEWAIT1
:
373 if (rxflags
->peer_done_writing
)
374 sk
->sk_state
= SMC_PEERCLOSEWAIT2
;
376 /* to check for closing */
377 case SMC_PEERCLOSEWAIT2
:
378 if (!smc_cdc_rxed_any_close(conn
))
380 if (sock_flag(sk
, SOCK_DEAD
) &&
381 smc_close_sent_any_close(conn
)) {
382 /* smc_release has already been called locally */
383 sk
->sk_state
= SMC_CLOSED
;
385 /* just shutdown, but not yet closed locally */
386 sk
->sk_state
= SMC_APPFINCLOSEWAIT
;
388 sock_put(sk
); /* passive closing */
390 case SMC_PEERFINCLOSEWAIT
:
391 if (smc_cdc_rxed_any_close(conn
)) {
392 sk
->sk_state
= SMC_CLOSED
;
393 sock_put(sk
); /* passive closing */
396 case SMC_APPCLOSEWAIT1
:
397 case SMC_APPCLOSEWAIT2
:
398 /* postpone sock_put() for passive closing to cover
399 * received SEND_SHUTDOWN as well
402 case SMC_APPFINCLOSEWAIT
:
403 case SMC_PEERABORTWAIT
:
404 case SMC_PROCESSABORT
:
406 /* nothing to do, add tracing in future patch */
411 sk
->sk_data_ready(sk
); /* wakeup blocked rcvbuf consumers */
412 sk
->sk_write_space(sk
); /* wakeup blocked sndbuf producers */
414 if (old_state
!= sk
->sk_state
) {
415 sk
->sk_state_change(sk
);
416 if ((sk
->sk_state
== SMC_CLOSED
) &&
417 (sock_flag(sk
, SOCK_DEAD
) || !sk
->sk_socket
)) {
420 release_clcsock
= true;
425 smc_clcsock_release(smc
);
426 sock_put(sk
); /* sock_hold done by schedulers of close_work */
429 int smc_close_shutdown_write(struct smc_sock
*smc
)
431 struct smc_connection
*conn
= &smc
->conn
;
432 struct sock
*sk
= &smc
->sk
;
437 timeout
= current
->flags
& PF_EXITING
?
438 0 : sock_flag(sk
, SOCK_LINGER
) ?
439 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
441 old_state
= sk
->sk_state
;
443 switch (sk
->sk_state
) {
445 smc_close_stream_wait(smc
, timeout
);
447 cancel_delayed_work_sync(&conn
->tx_work
);
449 if (sk
->sk_state
!= SMC_ACTIVE
)
451 /* send close wr request */
452 rc
= smc_close_wr(conn
);
453 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
455 case SMC_APPCLOSEWAIT1
:
457 if (!smc_cdc_rxed_any_close(conn
))
458 smc_close_stream_wait(smc
, timeout
);
460 cancel_delayed_work_sync(&conn
->tx_work
);
462 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
)
464 /* confirm close from peer */
465 rc
= smc_close_wr(conn
);
466 sk
->sk_state
= SMC_APPCLOSEWAIT2
;
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 */
479 if (old_state
!= sk
->sk_state
)
480 sk
->sk_state_change(sk
);
484 /* Initialize close properties on connection establishment. */
485 void smc_close_init(struct smc_sock
*smc
)
487 INIT_WORK(&smc
->conn
.close_work
, smc_close_passive_work
);