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 /* Send out corked data remaining in sndbuf */
61 smc_tx_pending(&smc
->conn
);
63 smc
->wait_close_tx_prepared
= 1;
64 add_wait_queue(sk_sleep(sk
), &wait
);
65 while (!signal_pending(current
) && timeout
) {
68 rc
= sk_wait_event(sk
, &timeout
,
69 !smc_tx_prepared_sends(&smc
->conn
) ||
70 READ_ONCE(sk
->sk_err
) == ECONNABORTED
||
71 READ_ONCE(sk
->sk_err
) == ECONNRESET
||
77 remove_wait_queue(sk_sleep(sk
), &wait
);
78 smc
->wait_close_tx_prepared
= 0;
81 void smc_close_wake_tx_prepared(struct smc_sock
*smc
)
83 if (smc
->wait_close_tx_prepared
)
84 /* wake up socket closing */
85 smc
->sk
.sk_state_change(&smc
->sk
);
88 static int smc_close_wr(struct smc_connection
*conn
)
90 conn
->local_tx_ctrl
.conn_state_flags
.peer_done_writing
= 1;
92 return smc_cdc_get_slot_and_msg_send(conn
);
95 static int smc_close_final(struct smc_connection
*conn
)
97 if (atomic_read(&conn
->bytes_to_rcv
))
98 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
100 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_closed
= 1;
104 return smc_cdc_get_slot_and_msg_send(conn
);
107 int smc_close_abort(struct smc_connection
*conn
)
109 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
111 return smc_cdc_get_slot_and_msg_send(conn
);
114 static void smc_close_cancel_work(struct smc_sock
*smc
)
116 struct sock
*sk
= &smc
->sk
;
119 if (cancel_work_sync(&smc
->conn
.close_work
))
121 cancel_delayed_work_sync(&smc
->conn
.tx_work
);
125 /* terminate smc socket abnormally - active abort
126 * link group is terminated, i.e. RDMA communication no longer possible
128 void smc_close_active_abort(struct smc_sock
*smc
)
130 struct sock
*sk
= &smc
->sk
;
131 bool release_clcsock
= false;
133 if (sk
->sk_state
!= SMC_INIT
&& smc
->clcsock
&& smc
->clcsock
->sk
) {
134 sk
->sk_err
= ECONNABORTED
;
135 if (smc
->clcsock
&& smc
->clcsock
->sk
)
136 tcp_abort(smc
->clcsock
->sk
, ECONNABORTED
);
138 switch (sk
->sk_state
) {
140 case SMC_APPCLOSEWAIT1
:
141 case SMC_APPCLOSEWAIT2
:
142 sk
->sk_state
= SMC_PEERABORTWAIT
;
143 smc_close_cancel_work(smc
);
144 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
146 sk
->sk_state
= SMC_CLOSED
;
147 sock_put(sk
); /* (postponed) passive closing */
149 case SMC_PEERCLOSEWAIT1
:
150 case SMC_PEERCLOSEWAIT2
:
151 case SMC_PEERFINCLOSEWAIT
:
152 sk
->sk_state
= SMC_PEERABORTWAIT
;
153 smc_close_cancel_work(smc
);
154 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
156 sk
->sk_state
= SMC_CLOSED
;
157 smc_conn_free(&smc
->conn
);
158 release_clcsock
= true;
159 sock_put(sk
); /* passive closing */
161 case SMC_PROCESSABORT
:
162 case SMC_APPFINCLOSEWAIT
:
163 sk
->sk_state
= SMC_PEERABORTWAIT
;
164 smc_close_cancel_work(smc
);
165 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
167 sk
->sk_state
= SMC_CLOSED
;
168 smc_conn_free(&smc
->conn
);
169 release_clcsock
= true;
172 case SMC_PEERABORTWAIT
:
177 smc_sock_set_flag(sk
, SOCK_DEAD
);
178 sk
->sk_state_change(sk
);
180 if (release_clcsock
) {
182 smc_clcsock_release(smc
);
187 static inline bool smc_close_sent_any_close(struct smc_connection
*conn
)
189 return conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
||
190 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_closed
;
193 int smc_close_active(struct smc_sock
*smc
)
195 struct smc_cdc_conn_state_flags
*txflags
=
196 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
197 struct smc_connection
*conn
= &smc
->conn
;
198 struct sock
*sk
= &smc
->sk
;
204 timeout
= current
->flags
& PF_EXITING
?
205 0 : sock_flag(sk
, SOCK_LINGER
) ?
206 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
208 old_state
= sk
->sk_state
;
210 switch (sk
->sk_state
) {
212 sk
->sk_state
= SMC_CLOSED
;
215 sk
->sk_state
= SMC_CLOSED
;
216 sk
->sk_state_change(sk
); /* wake up accept */
217 if (smc
->clcsock
&& smc
->clcsock
->sk
) {
218 write_lock_bh(&smc
->clcsock
->sk
->sk_callback_lock
);
219 smc_clcsock_restore_cb(&smc
->clcsock
->sk
->sk_data_ready
,
220 &smc
->clcsk_data_ready
);
221 smc
->clcsock
->sk
->sk_user_data
= NULL
;
222 write_unlock_bh(&smc
->clcsock
->sk
->sk_callback_lock
);
223 rc
= kernel_sock_shutdown(smc
->clcsock
, SHUT_RDWR
);
225 smc_close_cleanup_listen(sk
);
227 flush_work(&smc
->tcp_listen_work
);
231 smc_close_stream_wait(smc
, timeout
);
233 cancel_delayed_work_sync(&conn
->tx_work
);
235 if (sk
->sk_state
== SMC_ACTIVE
) {
236 /* send close request */
237 rc
= smc_close_final(conn
);
238 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
240 /* actively shutdown clcsock before peer close it,
241 * prevent peer from entering TIME_WAIT state.
243 if (smc
->clcsock
&& smc
->clcsock
->sk
) {
244 rc1
= kernel_sock_shutdown(smc
->clcsock
,
249 /* peer event has changed the state */
253 case SMC_APPFINCLOSEWAIT
:
254 /* socket already shutdown wr or both (active close) */
255 if (txflags
->peer_done_writing
&&
256 !smc_close_sent_any_close(conn
)) {
257 /* just shutdown wr done, send close request */
258 rc
= smc_close_final(conn
);
260 sk
->sk_state
= SMC_CLOSED
;
262 case SMC_APPCLOSEWAIT1
:
263 case SMC_APPCLOSEWAIT2
:
264 if (!smc_cdc_rxed_any_close(conn
))
265 smc_close_stream_wait(smc
, timeout
);
267 cancel_delayed_work_sync(&conn
->tx_work
);
269 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
&&
270 sk
->sk_state
!= SMC_APPCLOSEWAIT2
)
272 /* confirm close from peer */
273 rc
= smc_close_final(conn
);
274 if (smc_cdc_rxed_any_close(conn
)) {
275 /* peer has closed the socket already */
276 sk
->sk_state
= SMC_CLOSED
;
277 sock_put(sk
); /* postponed passive closing */
279 /* peer has just issued a shutdown write */
280 sk
->sk_state
= SMC_PEERFINCLOSEWAIT
;
283 case SMC_PEERCLOSEWAIT1
:
284 case SMC_PEERCLOSEWAIT2
:
285 if (txflags
->peer_done_writing
&&
286 !smc_close_sent_any_close(conn
)) {
287 /* just shutdown wr done, send close request */
288 rc
= smc_close_final(conn
);
290 /* peer sending PeerConnectionClosed will cause transition */
292 case SMC_PEERFINCLOSEWAIT
:
293 /* peer sending PeerConnectionClosed will cause transition */
295 case SMC_PROCESSABORT
:
296 rc
= smc_close_abort(conn
);
297 sk
->sk_state
= SMC_CLOSED
;
299 case SMC_PEERABORTWAIT
:
300 sk
->sk_state
= SMC_CLOSED
;
303 /* nothing to do, add tracing in future patch */
307 if (old_state
!= sk
->sk_state
)
308 sk
->sk_state_change(sk
);
312 static void smc_close_passive_abort_received(struct smc_sock
*smc
)
314 struct smc_cdc_conn_state_flags
*txflags
=
315 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
316 struct sock
*sk
= &smc
->sk
;
318 switch (sk
->sk_state
) {
321 case SMC_APPCLOSEWAIT1
:
322 sk
->sk_state
= SMC_PROCESSABORT
;
323 sock_put(sk
); /* passive closing */
325 case SMC_APPFINCLOSEWAIT
:
326 sk
->sk_state
= SMC_PROCESSABORT
;
328 case SMC_PEERCLOSEWAIT1
:
329 case SMC_PEERCLOSEWAIT2
:
330 if (txflags
->peer_done_writing
&&
331 !smc_close_sent_any_close(&smc
->conn
))
332 /* just shutdown, but not yet closed locally */
333 sk
->sk_state
= SMC_PROCESSABORT
;
335 sk
->sk_state
= SMC_CLOSED
;
336 sock_put(sk
); /* passive closing */
338 case SMC_APPCLOSEWAIT2
:
339 case SMC_PEERFINCLOSEWAIT
:
340 sk
->sk_state
= SMC_CLOSED
;
341 sock_put(sk
); /* passive closing */
343 case SMC_PEERABORTWAIT
:
344 sk
->sk_state
= SMC_CLOSED
;
346 case SMC_PROCESSABORT
:
347 /* nothing to do, add tracing in future patch */
352 /* Either some kind of closing has been received: peer_conn_closed,
353 * peer_conn_abort, or peer_done_writing
354 * or the link group of the connection terminates abnormally.
356 static void smc_close_passive_work(struct work_struct
*work
)
358 struct smc_connection
*conn
= container_of(work
,
359 struct smc_connection
,
361 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
362 struct smc_cdc_conn_state_flags
*rxflags
;
363 bool release_clcsock
= false;
364 struct sock
*sk
= &smc
->sk
;
368 old_state
= sk
->sk_state
;
370 rxflags
= &conn
->local_rx_ctrl
.conn_state_flags
;
371 if (rxflags
->peer_conn_abort
) {
372 /* peer has not received all data */
373 smc_close_passive_abort_received(smc
);
375 cancel_delayed_work_sync(&conn
->tx_work
);
380 switch (sk
->sk_state
) {
382 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
385 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
386 /* postpone sock_put() for passive closing to cover
387 * received SEND_SHUTDOWN as well
390 case SMC_PEERCLOSEWAIT1
:
391 if (rxflags
->peer_done_writing
)
392 sk
->sk_state
= SMC_PEERCLOSEWAIT2
;
394 /* to check for closing */
395 case SMC_PEERCLOSEWAIT2
:
396 if (!smc_cdc_rxed_any_close(conn
))
398 if (sock_flag(sk
, SOCK_DEAD
) &&
399 smc_close_sent_any_close(conn
)) {
400 /* smc_release has already been called locally */
401 sk
->sk_state
= SMC_CLOSED
;
403 /* just shutdown, but not yet closed locally */
404 sk
->sk_state
= SMC_APPFINCLOSEWAIT
;
406 sock_put(sk
); /* passive closing */
408 case SMC_PEERFINCLOSEWAIT
:
409 if (smc_cdc_rxed_any_close(conn
)) {
410 sk
->sk_state
= SMC_CLOSED
;
411 sock_put(sk
); /* passive closing */
414 case SMC_APPCLOSEWAIT1
:
415 case SMC_APPCLOSEWAIT2
:
416 /* postpone sock_put() for passive closing to cover
417 * received SEND_SHUTDOWN as well
420 case SMC_APPFINCLOSEWAIT
:
421 case SMC_PEERABORTWAIT
:
422 case SMC_PROCESSABORT
:
424 /* nothing to do, add tracing in future patch */
429 sk
->sk_data_ready(sk
); /* wakeup blocked rcvbuf consumers */
430 sk
->sk_write_space(sk
); /* wakeup blocked sndbuf producers */
432 if (old_state
!= sk
->sk_state
) {
433 sk
->sk_state_change(sk
);
434 if ((sk
->sk_state
== SMC_CLOSED
) &&
435 (sock_flag(sk
, SOCK_DEAD
) || !sk
->sk_socket
)) {
438 release_clcsock
= true;
443 smc_clcsock_release(smc
);
444 sock_put(sk
); /* sock_hold done by schedulers of close_work */
447 int smc_close_shutdown_write(struct smc_sock
*smc
)
449 struct smc_connection
*conn
= &smc
->conn
;
450 struct sock
*sk
= &smc
->sk
;
455 timeout
= current
->flags
& PF_EXITING
?
456 0 : sock_flag(sk
, SOCK_LINGER
) ?
457 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
459 old_state
= sk
->sk_state
;
461 switch (sk
->sk_state
) {
463 smc_close_stream_wait(smc
, timeout
);
465 cancel_delayed_work_sync(&conn
->tx_work
);
467 if (sk
->sk_state
!= SMC_ACTIVE
)
469 /* send close wr request */
470 rc
= smc_close_wr(conn
);
471 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
473 case SMC_APPCLOSEWAIT1
:
475 if (!smc_cdc_rxed_any_close(conn
))
476 smc_close_stream_wait(smc
, timeout
);
478 cancel_delayed_work_sync(&conn
->tx_work
);
480 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
)
482 /* confirm close from peer */
483 rc
= smc_close_wr(conn
);
484 sk
->sk_state
= SMC_APPCLOSEWAIT2
;
486 case SMC_APPCLOSEWAIT2
:
487 case SMC_PEERFINCLOSEWAIT
:
488 case SMC_PEERCLOSEWAIT1
:
489 case SMC_PEERCLOSEWAIT2
:
490 case SMC_APPFINCLOSEWAIT
:
491 case SMC_PROCESSABORT
:
492 case SMC_PEERABORTWAIT
:
493 /* nothing to do, add tracing in future patch */
497 if (old_state
!= sk
->sk_state
)
498 sk
->sk_state_change(sk
);
502 /* Initialize close properties on connection establishment. */
503 void smc_close_init(struct smc_sock
*smc
)
505 INIT_WORK(&smc
->conn
.close_work
, smc_close_passive_work
);