blob: 290270c821ca47765dc82bd1909810795ad21804 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braunb38d7322017-01-09 16:55:25 +01002/*
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>
Ingo Molnarc3edc402017-02-02 08:35:14 +010013#include <linux/sched/signal.h>
14
Ursula Braunb38d7322017-01-09 16:55:25 +010015#include <net/sock.h>
Ursula Braun83179762019-10-21 16:13:12 +020016#include <net/tcp.h>
Ursula Braunb38d7322017-01-09 16:55:25 +010017
18#include "smc.h"
19#include "smc_tx.h"
20#include "smc_cdc.h"
21#include "smc_close.h"
22
Karsten Graulfd577702019-04-11 11:17:30 +020023/* 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
Ursula Braunb38d7322017-01-09 16:55:25 +010039static 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
Ursula Braunb38d7322017-01-09 16:55:25 +010048/* 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) ||
Ursula Braund18963c2019-10-09 10:07:47 +020067 sk->sk_err == ECONNABORTED ||
Ursula Braunb2900982019-10-21 16:13:08 +020068 sk->sk_err == ECONNRESET ||
69 smc->conn.killed,
Ursula Braunb38d7322017-01-09 16:55:25 +010070 &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;
Ursula Braunb2900982019-10-21 16:13:08 +020098 if (conn->killed)
99 return -EPIPE;
Ursula Braunb38d7322017-01-09 16:55:25 +0100100
101 return smc_cdc_get_slot_and_msg_send(conn);
102}
103
Ursula Braun83179762019-10-21 16:13:12 +0200104int smc_close_abort(struct smc_connection *conn)
Ursula Braunb38d7322017-01-09 16:55:25 +0100105{
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
Ursula Braun42bfba92019-11-14 13:02:41 +0100111static 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
Ursula Braunb38d7322017-01-09 16:55:25 +0100122/* terminate smc socket abnormally - active abort
Ursula Braun732720f2018-01-25 11:15:35 +0100123 * link group is terminated, i.e. RDMA communication no longer possible
Ursula Braunb38d7322017-01-09 16:55:25 +0100124 */
Ursula Braun81cf4f42019-10-21 16:13:15 +0200125void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +0100126{
Ursula Braun3163c502018-01-24 10:28:12 +0100127 struct sock *sk = &smc->sk;
Ursula Braun81cf4f42019-10-21 16:13:15 +0200128 bool release_clcsock = false;
Ursula Braun3163c502018-01-24 10:28:12 +0100129
Ursula Braundd65d872018-09-18 15:46:36 +0200130 if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
131 sk->sk_err = ECONNABORTED;
Ursula Braun83179762019-10-21 16:13:12 +0200132 if (smc->clcsock && smc->clcsock->sk)
133 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braunb38d7322017-01-09 16:55:25 +0100134 }
Ursula Braun3163c502018-01-24 10:28:12 +0100135 switch (sk->sk_state) {
Ursula Braun46c28db2017-04-10 14:58:01 +0200136 case SMC_ACTIVE:
Ursula Braun3163c502018-01-24 10:28:12 +0100137 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braun42bfba92019-11-14 13:02:41 +0100138 smc_close_cancel_work(smc);
Ursula Braund18963c2019-10-09 10:07:47 +0200139 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100140 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 break;
142 case SMC_APPCLOSEWAIT1:
143 case SMC_APPCLOSEWAIT2:
Ursula Braun42bfba92019-11-14 13:02:41 +0100144 smc_close_cancel_work(smc);
Ursula Braund18963c2019-10-09 10:07:47 +0200145 sk->sk_state = SMC_CLOSED;
Ursula Braun81cf4f42019-10-21 16:13:15 +0200146 sock_put(sk); /* postponed passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100147 break;
148 case SMC_PEERCLOSEWAIT1:
149 case SMC_PEERCLOSEWAIT2:
Ursula Braund18963c2019-10-09 10:07:47 +0200150 case SMC_PEERFINCLOSEWAIT:
Ursula Braun42bfba92019-11-14 13:02:41 +0100151 sk->sk_state = SMC_PEERABORTWAIT;
152 smc_close_cancel_work(smc);
Ursula Braund18963c2019-10-09 10:07:47 +0200153 sk->sk_state = SMC_CLOSED;
Ursula Braun81cf4f42019-10-21 16:13:15 +0200154 smc_conn_free(&smc->conn);
155 release_clcsock = true;
Ursula Braun51f1de72018-01-26 09:28:48 +0100156 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 break;
158 case SMC_PROCESSABORT:
159 case SMC_APPFINCLOSEWAIT:
Ursula Braun42bfba92019-11-14 13:02:41 +0100160 sk->sk_state = SMC_PEERABORTWAIT;
161 smc_close_cancel_work(smc);
Ursula Braun3163c502018-01-24 10:28:12 +0100162 sk->sk_state = SMC_CLOSED;
Ursula Braun42bfba92019-11-14 13:02:41 +0100163 smc_conn_free(&smc->conn);
164 release_clcsock = true;
Ursula Braunb38d7322017-01-09 16:55:25 +0100165 break;
Ursula Braundd65d872018-09-18 15:46:36 +0200166 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100167 case SMC_PEERABORTWAIT:
168 case SMC_CLOSED:
169 break;
170 }
171
Ursula Braun3163c502018-01-24 10:28:12 +0100172 sock_set_flag(sk, SOCK_DEAD);
173 sk->sk_state_change(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +0200174
175 if (release_clcsock) {
176 release_sock(sk);
177 smc_clcsock_release(smc);
178 lock_sock(sk);
179 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100180}
181
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200182static 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
Ursula Braunb38d7322017-01-09 16:55:25 +0100188int 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;
Ursula Braunb38d7322017-01-09 16:55:25 +0100192 struct smc_connection *conn = &smc->conn;
193 struct sock *sk = &smc->sk;
194 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200195 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100196 int rc = 0;
197
Ursula Braun8c96fee2017-09-21 09:16:34 +0200198 timeout = current->flags & PF_EXITING ?
199 0 : sock_flag(sk, SOCK_LINGER) ?
200 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100201
Ursula Braunb38d7322017-01-09 16:55:25 +0100202 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100203again:
204 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100205 case SMC_INIT:
206 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100207 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 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100216 smc_close_cleanup_listen(sk);
Ursula Braun3d502062018-03-13 10:41:54 +0100217 release_sock(sk);
218 flush_work(&smc->tcp_listen_work);
219 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100220 break;
221 case SMC_ACTIVE:
222 smc_close_stream_wait(smc, timeout);
223 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200224 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100225 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 &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200238 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100239 /* just shutdown wr done, send close request */
240 rc = smc_close_final(conn);
241 }
242 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100243 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);
Ursula Braun18e537c2017-09-21 09:16:33 +0200249 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100250 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100251 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);
Ursula Braun51f1de72018-01-26 09:28:48 +0100256 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100257 /* peer has closed the socket already */
258 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100259 sock_put(sk); /* postponed passive closing */
260 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100261 /* peer has just issued a shutdown write */
262 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100263 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100264 break;
265 case SMC_PEERCLOSEWAIT1:
266 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200267 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;
Ursula Braunb38d7322017-01-09 16:55:25 +0100274 case SMC_PEERFINCLOSEWAIT:
275 /* peer sending PeerConnectionClosed will cause transition */
276 break;
277 case SMC_PROCESSABORT:
Ursula Braund18963c2019-10-09 10:07:47 +0200278 rc = smc_close_abort(conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100279 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100280 break;
281 case SMC_PEERABORTWAIT:
Ursula Braund18963c2019-10-09 10:07:47 +0200282 sk->sk_state = SMC_CLOSED;
283 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100284 case SMC_CLOSED:
285 /* nothing to do, add tracing in future patch */
286 break;
287 }
288
289 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100290 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100291 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) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100301 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100302 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100303 case SMC_APPCLOSEWAIT1:
Ursula Braun51f1de72018-01-26 09:28:48 +0100304 sk->sk_state = SMC_PROCESSABORT;
305 sock_put(sk); /* passive closing */
306 break;
307 case SMC_APPFINCLOSEWAIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100308 sk->sk_state = SMC_PROCESSABORT;
309 break;
310 case SMC_PEERCLOSEWAIT1:
311 case SMC_PEERCLOSEWAIT2:
312 if (txflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100313 !smc_close_sent_any_close(&smc->conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100314 /* just shutdown, but not yet closed locally */
Ursula Braunb38d7322017-01-09 16:55:25 +0100315 sk->sk_state = SMC_PROCESSABORT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100316 else
Ursula Braunb38d7322017-01-09 16:55:25 +0100317 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100318 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100319 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100320 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100321 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100322 sk->sk_state = SMC_CLOSED;
323 sock_put(sk); /* passive closing */
324 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100325 case SMC_PEERABORTWAIT:
326 sk->sk_state = SMC_CLOSED;
327 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100328 case SMC_PROCESSABORT:
329 /* nothing to do, add tracing in future patch */
330 break;
331 }
332}
333
Ursula Braun732720f2018-01-25 11:15:35 +0100334/* 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.
Ursula Braunb38d7322017-01-09 16:55:25 +0100337 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200338static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100339{
Ursula Braun46c28db2017-04-10 14:58:01 +0200340 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;
Karsten Graulfd577702019-04-11 11:17:30 +0200345 bool release_clcsock = false;
Ursula Braunb38d7322017-01-09 16:55:25 +0100346 struct sock *sk = &smc->sk;
347 int old_state;
348
Ursula Braun3163c502018-01-24 10:28:12 +0100349 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100350 old_state = sk->sk_state;
351
Ursula Braun3163c502018-01-24 10:28:12 +0100352 rxflags = &conn->local_rx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100353 if (rxflags->peer_conn_abort) {
Ursula Braun732720f2018-01-25 11:15:35 +0100354 /* peer has not received all data */
Ursula Braunb38d7322017-01-09 16:55:25 +0100355 smc_close_passive_abort_received(smc);
Ursula Braun611b63a2018-01-25 11:15:31 +0100356 release_sock(&smc->sk);
357 cancel_delayed_work_sync(&conn->tx_work);
358 lock_sock(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100359 goto wakeup;
360 }
361
362 switch (sk->sk_state) {
363 case SMC_INIT:
Ursula Braun84b799a2019-02-04 13:44:47 +0100364 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100365 break;
366 case SMC_ACTIVE:
367 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100368 /* postpone sock_put() for passive closing to cover
369 * received SEND_SHUTDOWN as well
370 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100371 break;
372 case SMC_PEERCLOSEWAIT1:
373 if (rxflags->peer_done_writing)
374 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500375 /* fall through */
376 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100377 case SMC_PEERCLOSEWAIT2:
Ursula Braun3163c502018-01-24 10:28:12 +0100378 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100379 break;
380 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200381 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100382 /* 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 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100388 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100389 break;
Ursula Braun5ac92a02018-01-25 11:15:32 +0100390 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100391 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braun5ac92a02018-01-25 11:15:32 +0100392 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100393 sock_put(sk); /* passive closing */
394 }
Ursula Braun5ac92a02018-01-25 11:15:32 +0100395 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100396 case SMC_APPCLOSEWAIT1:
397 case SMC_APPCLOSEWAIT2:
Ursula Braun51f1de72018-01-26 09:28:48 +0100398 /* postpone sock_put() for passive closing to cover
399 * received SEND_SHUTDOWN as well
400 */
401 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100402 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:
Ursula Braunb38d7322017-01-09 16:55:25 +0100411 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
412 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
413
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200414 if (old_state != sk->sk_state) {
415 sk->sk_state_change(sk);
416 if ((sk->sk_state == SMC_CLOSED) &&
Ursula Braunb03faa12019-02-07 15:56:15 +0100417 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
Ursula Braun3163c502018-01-24 10:28:12 +0100418 smc_conn_free(conn);
Karsten Graulfd577702019-04-11 11:17:30 +0200419 if (smc->clcsock)
420 release_clcsock = true;
Ursula Braunb03faa12019-02-07 15:56:15 +0100421 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100422 }
Ursula Braun3163c502018-01-24 10:28:12 +0100423 release_sock(sk);
Karsten Graulfd577702019-04-11 11:17:30 +0200424 if (release_clcsock)
425 smc_clcsock_release(smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100426 sock_put(sk); /* sock_hold done by schedulers of close_work */
Ursula Braunb38d7322017-01-09 16:55:25 +0100427}
428
429int smc_close_shutdown_write(struct smc_sock *smc)
430{
431 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100432 struct sock *sk = &smc->sk;
433 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200434 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100435 int rc = 0;
436
Ursula Braun8c96fee2017-09-21 09:16:34 +0200437 timeout = current->flags & PF_EXITING ?
438 0 : sock_flag(sk, SOCK_LINGER) ?
439 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100440
Ursula Braunb38d7322017-01-09 16:55:25 +0100441 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100442again:
443 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100444 case SMC_ACTIVE:
445 smc_close_stream_wait(smc, timeout);
446 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200447 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100448 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100449 if (sk->sk_state != SMC_ACTIVE)
450 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100451 /* send close wr request */
452 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100453 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100454 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);
Ursula Braun18e537c2017-09-21 09:16:33 +0200460 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100461 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100462 if (sk->sk_state != SMC_APPCLOSEWAIT1)
463 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100464 /* 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)
Ursula Braun3163c502018-01-24 10:28:12 +0100480 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100481 return rc;
482}
Ursula Braun46c28db2017-04-10 14:58:01 +0200483
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}