blob: 292e4d904ab6e4afbba2cef421bce27cae5af364 [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);
Ursula Braun42bfba92019-11-14 13:02:41 +0100119}
120
Ursula Braunb38d7322017-01-09 16:55:25 +0100121/* terminate smc socket abnormally - active abort
Ursula Braun732720f2018-01-25 11:15:35 +0100122 * link group is terminated, i.e. RDMA communication no longer possible
Ursula Braunb38d7322017-01-09 16:55:25 +0100123 */
Ursula Braun81cf4f42019-10-21 16:13:15 +0200124void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +0100125{
Ursula Braun3163c502018-01-24 10:28:12 +0100126 struct sock *sk = &smc->sk;
Ursula Braun81cf4f42019-10-21 16:13:15 +0200127 bool release_clcsock = false;
Ursula Braun3163c502018-01-24 10:28:12 +0100128
Ursula Braundd65d872018-09-18 15:46:36 +0200129 if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
130 sk->sk_err = ECONNABORTED;
Ursula Braun83179762019-10-21 16:13:12 +0200131 if (smc->clcsock && smc->clcsock->sk)
132 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braunb38d7322017-01-09 16:55:25 +0100133 }
Ursula Braun3163c502018-01-24 10:28:12 +0100134 switch (sk->sk_state) {
Ursula Braun46c28db2017-04-10 14:58:01 +0200135 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 case SMC_APPCLOSEWAIT1:
137 case SMC_APPCLOSEWAIT2:
Ursula Braun5fb86422020-09-03 21:53:18 +0200138 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braun42bfba92019-11-14 13:02:41 +0100139 smc_close_cancel_work(smc);
Ursula Braun5fb86422020-09-03 21:53:18 +0200140 if (sk->sk_state != SMC_PEERABORTWAIT)
141 break;
Ursula Braund18963c2019-10-09 10:07:47 +0200142 sk->sk_state = SMC_CLOSED;
Ursula Braun5fb86422020-09-03 21:53:18 +0200143 sock_put(sk); /* (postponed) passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100144 break;
145 case SMC_PEERCLOSEWAIT1:
146 case SMC_PEERCLOSEWAIT2:
Ursula Braund18963c2019-10-09 10:07:47 +0200147 case SMC_PEERFINCLOSEWAIT:
Ursula Braun42bfba92019-11-14 13:02:41 +0100148 sk->sk_state = SMC_PEERABORTWAIT;
149 smc_close_cancel_work(smc);
Ursula Braun5fb86422020-09-03 21:53:18 +0200150 if (sk->sk_state != SMC_PEERABORTWAIT)
151 break;
Ursula Braund18963c2019-10-09 10:07:47 +0200152 sk->sk_state = SMC_CLOSED;
Ursula Braun81cf4f42019-10-21 16:13:15 +0200153 smc_conn_free(&smc->conn);
154 release_clcsock = true;
Ursula Braun51f1de72018-01-26 09:28:48 +0100155 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100156 break;
157 case SMC_PROCESSABORT:
158 case SMC_APPFINCLOSEWAIT:
Ursula Braun42bfba92019-11-14 13:02:41 +0100159 sk->sk_state = SMC_PEERABORTWAIT;
160 smc_close_cancel_work(smc);
Ursula Braun5fb86422020-09-03 21:53:18 +0200161 if (sk->sk_state != SMC_PEERABORTWAIT)
162 break;
Ursula Braun3163c502018-01-24 10:28:12 +0100163 sk->sk_state = SMC_CLOSED;
Ursula Braun42bfba92019-11-14 13:02:41 +0100164 smc_conn_free(&smc->conn);
165 release_clcsock = true;
Ursula Braunb38d7322017-01-09 16:55:25 +0100166 break;
Ursula Braundd65d872018-09-18 15:46:36 +0200167 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100168 case SMC_PEERABORTWAIT:
169 case SMC_CLOSED:
170 break;
171 }
172
Ursula Braun3163c502018-01-24 10:28:12 +0100173 sock_set_flag(sk, SOCK_DEAD);
174 sk->sk_state_change(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +0200175
176 if (release_clcsock) {
177 release_sock(sk);
178 smc_clcsock_release(smc);
179 lock_sock(sk);
180 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100181}
182
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200183static inline bool smc_close_sent_any_close(struct smc_connection *conn)
184{
185 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
186 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
187}
188
Ursula Braunb38d7322017-01-09 16:55:25 +0100189int smc_close_active(struct smc_sock *smc)
190{
191 struct smc_cdc_conn_state_flags *txflags =
192 &smc->conn.local_tx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100193 struct smc_connection *conn = &smc->conn;
194 struct sock *sk = &smc->sk;
195 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200196 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100197 int rc = 0;
Tony Lu00e158f2021-12-01 14:42:16 +0800198 int rc1 = 0;
Ursula Braunb38d7322017-01-09 16:55:25 +0100199
Ursula Braun8c96fee2017-09-21 09:16:34 +0200200 timeout = current->flags & PF_EXITING ?
201 0 : sock_flag(sk, SOCK_LINGER) ?
202 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100203
Ursula Braunb38d7322017-01-09 16:55:25 +0100204 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100205again:
206 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100207 case SMC_INIT:
208 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 break;
210 case SMC_LISTEN:
211 sk->sk_state = SMC_CLOSED;
212 sk->sk_state_change(sk); /* wake up accept */
Karsten Graulddcc9b72020-09-15 22:57:09 +0200213 if (smc->clcsock && smc->clcsock->sk) {
214 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
215 smc->clcsock->sk->sk_user_data = NULL;
Ursula Braunb38d7322017-01-09 16:55:25 +0100216 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
Karsten Graulddcc9b72020-09-15 22:57:09 +0200217 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100218 smc_close_cleanup_listen(sk);
Ursula Braun3d502062018-03-13 10:41:54 +0100219 release_sock(sk);
220 flush_work(&smc->tcp_listen_work);
221 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100222 break;
223 case SMC_ACTIVE:
224 smc_close_stream_wait(smc, timeout);
225 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200226 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100227 lock_sock(sk);
228 if (sk->sk_state == SMC_ACTIVE) {
229 /* send close request */
230 rc = smc_close_final(conn);
231 sk->sk_state = SMC_PEERCLOSEWAIT1;
Tony Lu606a63c2021-11-23 16:25:18 +0800232
233 /* actively shutdown clcsock before peer close it,
234 * prevent peer from entering TIME_WAIT state.
235 */
Tony Lu00e158f2021-12-01 14:42:16 +0800236 if (smc->clcsock && smc->clcsock->sk) {
237 rc1 = kernel_sock_shutdown(smc->clcsock,
238 SHUT_RDWR);
239 rc = rc ? rc : rc1;
240 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100241 } else {
242 /* peer event has changed the state */
243 goto again;
244 }
245 break;
246 case SMC_APPFINCLOSEWAIT:
247 /* socket already shutdown wr or both (active close) */
248 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200249 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100250 /* just shutdown wr done, send close request */
251 rc = smc_close_final(conn);
252 }
253 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100254 break;
255 case SMC_APPCLOSEWAIT1:
256 case SMC_APPCLOSEWAIT2:
257 if (!smc_cdc_rxed_any_close(conn))
258 smc_close_stream_wait(smc, timeout);
259 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200260 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100261 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100262 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
263 sk->sk_state != SMC_APPCLOSEWAIT2)
264 goto again;
265 /* confirm close from peer */
266 rc = smc_close_final(conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100267 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100268 /* peer has closed the socket already */
269 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100270 sock_put(sk); /* postponed passive closing */
271 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100272 /* peer has just issued a shutdown write */
273 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100274 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100275 break;
276 case SMC_PEERCLOSEWAIT1:
277 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200278 if (txflags->peer_done_writing &&
279 !smc_close_sent_any_close(conn)) {
280 /* just shutdown wr done, send close request */
281 rc = smc_close_final(conn);
282 }
283 /* peer sending PeerConnectionClosed will cause transition */
284 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100285 case SMC_PEERFINCLOSEWAIT:
286 /* peer sending PeerConnectionClosed will cause transition */
287 break;
288 case SMC_PROCESSABORT:
Ursula Braund18963c2019-10-09 10:07:47 +0200289 rc = smc_close_abort(conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100290 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100291 break;
292 case SMC_PEERABORTWAIT:
Ursula Braund18963c2019-10-09 10:07:47 +0200293 sk->sk_state = SMC_CLOSED;
294 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100295 case SMC_CLOSED:
296 /* nothing to do, add tracing in future patch */
297 break;
298 }
299
300 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100301 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100302 return rc;
303}
304
305static void smc_close_passive_abort_received(struct smc_sock *smc)
306{
307 struct smc_cdc_conn_state_flags *txflags =
308 &smc->conn.local_tx_ctrl.conn_state_flags;
309 struct sock *sk = &smc->sk;
310
311 switch (sk->sk_state) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100312 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100313 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100314 case SMC_APPCLOSEWAIT1:
Ursula Braun51f1de72018-01-26 09:28:48 +0100315 sk->sk_state = SMC_PROCESSABORT;
316 sock_put(sk); /* passive closing */
317 break;
318 case SMC_APPFINCLOSEWAIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100319 sk->sk_state = SMC_PROCESSABORT;
320 break;
321 case SMC_PEERCLOSEWAIT1:
322 case SMC_PEERCLOSEWAIT2:
323 if (txflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100324 !smc_close_sent_any_close(&smc->conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100325 /* just shutdown, but not yet closed locally */
Ursula Braunb38d7322017-01-09 16:55:25 +0100326 sk->sk_state = SMC_PROCESSABORT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100327 else
Ursula Braunb38d7322017-01-09 16:55:25 +0100328 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100329 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100330 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100331 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100332 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100333 sk->sk_state = SMC_CLOSED;
334 sock_put(sk); /* passive closing */
335 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100336 case SMC_PEERABORTWAIT:
337 sk->sk_state = SMC_CLOSED;
338 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100339 case SMC_PROCESSABORT:
340 /* nothing to do, add tracing in future patch */
341 break;
342 }
343}
344
Ursula Braun732720f2018-01-25 11:15:35 +0100345/* Either some kind of closing has been received: peer_conn_closed,
346 * peer_conn_abort, or peer_done_writing
347 * or the link group of the connection terminates abnormally.
Ursula Braunb38d7322017-01-09 16:55:25 +0100348 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200349static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100350{
Ursula Braun46c28db2017-04-10 14:58:01 +0200351 struct smc_connection *conn = container_of(work,
352 struct smc_connection,
353 close_work);
354 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
355 struct smc_cdc_conn_state_flags *rxflags;
Karsten Graulfd577702019-04-11 11:17:30 +0200356 bool release_clcsock = false;
Ursula Braunb38d7322017-01-09 16:55:25 +0100357 struct sock *sk = &smc->sk;
358 int old_state;
359
Ursula Braun3163c502018-01-24 10:28:12 +0100360 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100361 old_state = sk->sk_state;
362
Ursula Braun3163c502018-01-24 10:28:12 +0100363 rxflags = &conn->local_rx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100364 if (rxflags->peer_conn_abort) {
Ursula Braun732720f2018-01-25 11:15:35 +0100365 /* peer has not received all data */
Ursula Braunb38d7322017-01-09 16:55:25 +0100366 smc_close_passive_abort_received(smc);
Tony Lu45c3ff72021-11-23 16:25:16 +0800367 release_sock(sk);
Ursula Braun611b63a2018-01-25 11:15:31 +0100368 cancel_delayed_work_sync(&conn->tx_work);
Tony Lu45c3ff72021-11-23 16:25:16 +0800369 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100370 goto wakeup;
371 }
372
373 switch (sk->sk_state) {
374 case SMC_INIT:
Ursula Braun84b799a2019-02-04 13:44:47 +0100375 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100376 break;
377 case SMC_ACTIVE:
378 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100379 /* postpone sock_put() for passive closing to cover
380 * received SEND_SHUTDOWN as well
381 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100382 break;
383 case SMC_PEERCLOSEWAIT1:
384 if (rxflags->peer_done_writing)
385 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500386 fallthrough;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500387 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100388 case SMC_PEERCLOSEWAIT2:
Ursula Braun3163c502018-01-24 10:28:12 +0100389 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100390 break;
391 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200392 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100393 /* smc_release has already been called locally */
394 sk->sk_state = SMC_CLOSED;
395 } else {
396 /* just shutdown, but not yet closed locally */
397 sk->sk_state = SMC_APPFINCLOSEWAIT;
398 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100399 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100400 break;
Ursula Braun5ac92a02018-01-25 11:15:32 +0100401 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100402 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braun5ac92a02018-01-25 11:15:32 +0100403 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100404 sock_put(sk); /* passive closing */
405 }
Ursula Braun5ac92a02018-01-25 11:15:32 +0100406 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100407 case SMC_APPCLOSEWAIT1:
408 case SMC_APPCLOSEWAIT2:
Ursula Braun51f1de72018-01-26 09:28:48 +0100409 /* postpone sock_put() for passive closing to cover
410 * received SEND_SHUTDOWN as well
411 */
412 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100413 case SMC_APPFINCLOSEWAIT:
414 case SMC_PEERABORTWAIT:
415 case SMC_PROCESSABORT:
416 case SMC_CLOSED:
417 /* nothing to do, add tracing in future patch */
418 break;
419 }
420
421wakeup:
Ursula Braunb38d7322017-01-09 16:55:25 +0100422 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
423 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
424
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200425 if (old_state != sk->sk_state) {
426 sk->sk_state_change(sk);
427 if ((sk->sk_state == SMC_CLOSED) &&
Ursula Braunb03faa12019-02-07 15:56:15 +0100428 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
Ursula Braun3163c502018-01-24 10:28:12 +0100429 smc_conn_free(conn);
Karsten Graulfd577702019-04-11 11:17:30 +0200430 if (smc->clcsock)
431 release_clcsock = true;
Ursula Braunb03faa12019-02-07 15:56:15 +0100432 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100433 }
Ursula Braun3163c502018-01-24 10:28:12 +0100434 release_sock(sk);
Karsten Graulfd577702019-04-11 11:17:30 +0200435 if (release_clcsock)
436 smc_clcsock_release(smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100437 sock_put(sk); /* sock_hold done by schedulers of close_work */
Ursula Braunb38d7322017-01-09 16:55:25 +0100438}
439
440int smc_close_shutdown_write(struct smc_sock *smc)
441{
442 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100443 struct sock *sk = &smc->sk;
444 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200445 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100446 int rc = 0;
447
Ursula Braun8c96fee2017-09-21 09:16:34 +0200448 timeout = current->flags & PF_EXITING ?
449 0 : sock_flag(sk, SOCK_LINGER) ?
450 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100451
Ursula Braunb38d7322017-01-09 16:55:25 +0100452 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100453again:
454 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100455 case SMC_ACTIVE:
456 smc_close_stream_wait(smc, timeout);
457 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200458 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100459 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100460 if (sk->sk_state != SMC_ACTIVE)
461 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100462 /* send close wr request */
463 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100464 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100465 break;
466 case SMC_APPCLOSEWAIT1:
467 /* passive close */
468 if (!smc_cdc_rxed_any_close(conn))
469 smc_close_stream_wait(smc, timeout);
470 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200471 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100472 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100473 if (sk->sk_state != SMC_APPCLOSEWAIT1)
474 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100475 /* confirm close from peer */
476 rc = smc_close_wr(conn);
477 sk->sk_state = SMC_APPCLOSEWAIT2;
478 break;
479 case SMC_APPCLOSEWAIT2:
480 case SMC_PEERFINCLOSEWAIT:
481 case SMC_PEERCLOSEWAIT1:
482 case SMC_PEERCLOSEWAIT2:
483 case SMC_APPFINCLOSEWAIT:
484 case SMC_PROCESSABORT:
485 case SMC_PEERABORTWAIT:
486 /* nothing to do, add tracing in future patch */
487 break;
488 }
489
490 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100491 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100492 return rc;
493}
Ursula Braun46c28db2017-04-10 14:58:01 +0200494
495/* Initialize close properties on connection establishment. */
496void smc_close_init(struct smc_sock *smc)
497{
498 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
499}