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
);
121 /* terminate smc socket abnormally - active abort
122 * link group is terminated, i.e. RDMA communication no longer possible
124 void smc_close_active_abort(struct smc_sock
*smc
)
126 struct sock
*sk
= &smc
->sk
;
127 bool release_clcsock
= false;
129 if (sk
->sk_state
!= SMC_INIT
&& smc
->clcsock
&& smc
->clcsock
->sk
) {
130 sk
->sk_err
= ECONNABORTED
;
131 if (smc
->clcsock
&& smc
->clcsock
->sk
)
132 tcp_abort(smc
->clcsock
->sk
, ECONNABORTED
);
134 switch (sk
->sk_state
) {
136 case SMC_APPCLOSEWAIT1
:
137 case SMC_APPCLOSEWAIT2
:
138 sk
->sk_state
= SMC_PEERABORTWAIT
;
139 smc_close_cancel_work(smc
);
140 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
142 sk
->sk_state
= SMC_CLOSED
;
143 sock_put(sk
); /* (postponed) passive closing */
145 case SMC_PEERCLOSEWAIT1
:
146 case SMC_PEERCLOSEWAIT2
:
147 case SMC_PEERFINCLOSEWAIT
:
148 sk
->sk_state
= SMC_PEERABORTWAIT
;
149 smc_close_cancel_work(smc
);
150 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
152 sk
->sk_state
= SMC_CLOSED
;
153 smc_conn_free(&smc
->conn
);
154 release_clcsock
= true;
155 sock_put(sk
); /* passive closing */
157 case SMC_PROCESSABORT
:
158 case SMC_APPFINCLOSEWAIT
:
159 sk
->sk_state
= SMC_PEERABORTWAIT
;
160 smc_close_cancel_work(smc
);
161 if (sk
->sk_state
!= SMC_PEERABORTWAIT
)
163 sk
->sk_state
= SMC_CLOSED
;
164 smc_conn_free(&smc
->conn
);
165 release_clcsock
= true;
168 case SMC_PEERABORTWAIT
:
173 sock_set_flag(sk
, SOCK_DEAD
);
174 sk
->sk_state_change(sk
);
176 if (release_clcsock
) {
178 smc_clcsock_release(smc
);
183 static inline bool smc_close_sent_any_close(struct smc_connection
*conn
)
185 return conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
||
186 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_closed
;
189 int smc_close_active(struct smc_sock
*smc
)
191 struct smc_cdc_conn_state_flags
*txflags
=
192 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
193 struct smc_connection
*conn
= &smc
->conn
;
194 struct sock
*sk
= &smc
->sk
;
199 timeout
= current
->flags
& PF_EXITING
?
200 0 : sock_flag(sk
, SOCK_LINGER
) ?
201 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
203 old_state
= sk
->sk_state
;
205 switch (sk
->sk_state
) {
207 sk
->sk_state
= SMC_CLOSED
;
210 sk
->sk_state
= SMC_CLOSED
;
211 sk
->sk_state_change(sk
); /* wake up accept */
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
;
215 rc
= kernel_sock_shutdown(smc
->clcsock
, SHUT_RDWR
);
217 smc_close_cleanup_listen(sk
);
219 flush_work(&smc
->tcp_listen_work
);
223 smc_close_stream_wait(smc
, timeout
);
225 cancel_delayed_work_sync(&conn
->tx_work
);
227 if (sk
->sk_state
== SMC_ACTIVE
) {
228 /* send close request */
229 rc
= smc_close_final(conn
);
230 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
232 /* peer event has changed the state */
236 case SMC_APPFINCLOSEWAIT
:
237 /* socket already shutdown wr or both (active close) */
238 if (txflags
->peer_done_writing
&&
239 !smc_close_sent_any_close(conn
)) {
240 /* just shutdown wr done, send close request */
241 rc
= smc_close_final(conn
);
243 sk
->sk_state
= SMC_CLOSED
;
245 case SMC_APPCLOSEWAIT1
:
246 case SMC_APPCLOSEWAIT2
:
247 if (!smc_cdc_rxed_any_close(conn
))
248 smc_close_stream_wait(smc
, timeout
);
250 cancel_delayed_work_sync(&conn
->tx_work
);
252 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
&&
253 sk
->sk_state
!= SMC_APPCLOSEWAIT2
)
255 /* confirm close from peer */
256 rc
= smc_close_final(conn
);
257 if (smc_cdc_rxed_any_close(conn
)) {
258 /* peer has closed the socket already */
259 sk
->sk_state
= SMC_CLOSED
;
260 sock_put(sk
); /* postponed passive closing */
262 /* peer has just issued a shutdown write */
263 sk
->sk_state
= SMC_PEERFINCLOSEWAIT
;
266 case SMC_PEERCLOSEWAIT1
:
267 case SMC_PEERCLOSEWAIT2
:
268 if (txflags
->peer_done_writing
&&
269 !smc_close_sent_any_close(conn
)) {
270 /* just shutdown wr done, send close request */
271 rc
= smc_close_final(conn
);
273 /* peer sending PeerConnectionClosed will cause transition */
275 case SMC_PEERFINCLOSEWAIT
:
276 /* peer sending PeerConnectionClosed will cause transition */
278 case SMC_PROCESSABORT
:
279 rc
= smc_close_abort(conn
);
280 sk
->sk_state
= SMC_CLOSED
;
282 case SMC_PEERABORTWAIT
:
283 sk
->sk_state
= SMC_CLOSED
;
286 /* nothing to do, add tracing in future patch */
290 if (old_state
!= sk
->sk_state
)
291 sk
->sk_state_change(sk
);
295 static void smc_close_passive_abort_received(struct smc_sock
*smc
)
297 struct smc_cdc_conn_state_flags
*txflags
=
298 &smc
->conn
.local_tx_ctrl
.conn_state_flags
;
299 struct sock
*sk
= &smc
->sk
;
301 switch (sk
->sk_state
) {
304 case SMC_APPCLOSEWAIT1
:
305 sk
->sk_state
= SMC_PROCESSABORT
;
306 sock_put(sk
); /* passive closing */
308 case SMC_APPFINCLOSEWAIT
:
309 sk
->sk_state
= SMC_PROCESSABORT
;
311 case SMC_PEERCLOSEWAIT1
:
312 case SMC_PEERCLOSEWAIT2
:
313 if (txflags
->peer_done_writing
&&
314 !smc_close_sent_any_close(&smc
->conn
))
315 /* just shutdown, but not yet closed locally */
316 sk
->sk_state
= SMC_PROCESSABORT
;
318 sk
->sk_state
= SMC_CLOSED
;
319 sock_put(sk
); /* passive closing */
321 case SMC_APPCLOSEWAIT2
:
322 case SMC_PEERFINCLOSEWAIT
:
323 sk
->sk_state
= SMC_CLOSED
;
324 sock_put(sk
); /* passive closing */
326 case SMC_PEERABORTWAIT
:
327 sk
->sk_state
= SMC_CLOSED
;
329 case SMC_PROCESSABORT
:
330 /* nothing to do, add tracing in future patch */
335 /* Either some kind of closing has been received: peer_conn_closed,
336 * peer_conn_abort, or peer_done_writing
337 * or the link group of the connection terminates abnormally.
339 static void smc_close_passive_work(struct work_struct
*work
)
341 struct smc_connection
*conn
= container_of(work
,
342 struct smc_connection
,
344 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
345 struct smc_cdc_conn_state_flags
*rxflags
;
346 bool release_clcsock
= false;
347 struct sock
*sk
= &smc
->sk
;
351 old_state
= sk
->sk_state
;
353 rxflags
= &conn
->local_rx_ctrl
.conn_state_flags
;
354 if (rxflags
->peer_conn_abort
) {
355 /* peer has not received all data */
356 smc_close_passive_abort_received(smc
);
357 release_sock(&smc
->sk
);
358 cancel_delayed_work_sync(&conn
->tx_work
);
363 switch (sk
->sk_state
) {
365 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
368 sk
->sk_state
= SMC_APPCLOSEWAIT1
;
369 /* postpone sock_put() for passive closing to cover
370 * received SEND_SHUTDOWN as well
373 case SMC_PEERCLOSEWAIT1
:
374 if (rxflags
->peer_done_writing
)
375 sk
->sk_state
= SMC_PEERCLOSEWAIT2
;
377 /* to check for closing */
378 case SMC_PEERCLOSEWAIT2
:
379 if (!smc_cdc_rxed_any_close(conn
))
381 if (sock_flag(sk
, SOCK_DEAD
) &&
382 smc_close_sent_any_close(conn
)) {
383 /* smc_release has already been called locally */
384 sk
->sk_state
= SMC_CLOSED
;
386 /* just shutdown, but not yet closed locally */
387 sk
->sk_state
= SMC_APPFINCLOSEWAIT
;
389 sock_put(sk
); /* passive closing */
391 case SMC_PEERFINCLOSEWAIT
:
392 if (smc_cdc_rxed_any_close(conn
)) {
393 sk
->sk_state
= SMC_CLOSED
;
394 sock_put(sk
); /* passive closing */
397 case SMC_APPCLOSEWAIT1
:
398 case SMC_APPCLOSEWAIT2
:
399 /* postpone sock_put() for passive closing to cover
400 * received SEND_SHUTDOWN as well
403 case SMC_APPFINCLOSEWAIT
:
404 case SMC_PEERABORTWAIT
:
405 case SMC_PROCESSABORT
:
407 /* nothing to do, add tracing in future patch */
412 sk
->sk_data_ready(sk
); /* wakeup blocked rcvbuf consumers */
413 sk
->sk_write_space(sk
); /* wakeup blocked sndbuf producers */
415 if (old_state
!= sk
->sk_state
) {
416 sk
->sk_state_change(sk
);
417 if ((sk
->sk_state
== SMC_CLOSED
) &&
418 (sock_flag(sk
, SOCK_DEAD
) || !sk
->sk_socket
)) {
421 release_clcsock
= true;
426 smc_clcsock_release(smc
);
427 sock_put(sk
); /* sock_hold done by schedulers of close_work */
430 int smc_close_shutdown_write(struct smc_sock
*smc
)
432 struct smc_connection
*conn
= &smc
->conn
;
433 struct sock
*sk
= &smc
->sk
;
438 timeout
= current
->flags
& PF_EXITING
?
439 0 : sock_flag(sk
, SOCK_LINGER
) ?
440 sk
->sk_lingertime
: SMC_MAX_STREAM_WAIT_TIMEOUT
;
442 old_state
= sk
->sk_state
;
444 switch (sk
->sk_state
) {
446 smc_close_stream_wait(smc
, timeout
);
448 cancel_delayed_work_sync(&conn
->tx_work
);
450 if (sk
->sk_state
!= SMC_ACTIVE
)
452 /* send close wr request */
453 rc
= smc_close_wr(conn
);
454 sk
->sk_state
= SMC_PEERCLOSEWAIT1
;
456 case SMC_APPCLOSEWAIT1
:
458 if (!smc_cdc_rxed_any_close(conn
))
459 smc_close_stream_wait(smc
, timeout
);
461 cancel_delayed_work_sync(&conn
->tx_work
);
463 if (sk
->sk_state
!= SMC_APPCLOSEWAIT1
)
465 /* confirm close from peer */
466 rc
= smc_close_wr(conn
);
467 sk
->sk_state
= SMC_APPCLOSEWAIT2
;
469 case SMC_APPCLOSEWAIT2
:
470 case SMC_PEERFINCLOSEWAIT
:
471 case SMC_PEERCLOSEWAIT1
:
472 case SMC_PEERCLOSEWAIT2
:
473 case SMC_APPFINCLOSEWAIT
:
474 case SMC_PROCESSABORT
:
475 case SMC_PEERABORTWAIT
:
476 /* nothing to do, add tracing in future patch */
480 if (old_state
!= sk
->sk_state
)
481 sk
->sk_state_change(sk
);
485 /* Initialize close properties on connection establishment. */
486 void smc_close_init(struct smc_sock
*smc
)
488 INIT_WORK(&smc
->conn
.close_work
, smc_close_passive_work
);