xref: /linux/net/smc/smc_close.c (revision d198b34f3855eee2571dda03eea75a09c7c31480)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
13 #include <linux/sched/signal.h>
14 
15 #include <net/sock.h>
16 #include <net/tcp.h>
17 
18 #include "smc.h"
19 #include "smc_tx.h"
20 #include "smc_cdc.h"
21 #include "smc_close.h"
22 
23 /* release the clcsock that is assigned to the smc_sock */
24 void 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 
39 static 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 
48 /* wait for sndbuf data being transmitted */
49 static 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) ||
67 				   sk->sk_err == ECONNABORTED ||
68 				   sk->sk_err == ECONNRESET ||
69 				   smc->conn.killed,
70 				   &wait);
71 		if (rc)
72 			break;
73 	}
74 	remove_wait_queue(sk_sleep(sk), &wait);
75 	smc->wait_close_tx_prepared = 0;
76 }
77 
78 void 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 
85 static 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 
92 static 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;
98 	if (conn->killed)
99 		return -EPIPE;
100 
101 	return smc_cdc_get_slot_and_msg_send(conn);
102 }
103 
104 int smc_close_abort(struct smc_connection *conn)
105 {
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 
111 static 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 
122 /* terminate smc socket abnormally - active abort
123  * link group is terminated, i.e. RDMA communication no longer possible
124  */
125 void smc_close_active_abort(struct smc_sock *smc)
126 {
127 	struct sock *sk = &smc->sk;
128 	bool release_clcsock = false;
129 
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);
134 	}
135 	switch (sk->sk_state) {
136 	case SMC_ACTIVE:
137 		sk->sk_state = SMC_PEERABORTWAIT;
138 		smc_close_cancel_work(smc);
139 		sk->sk_state = SMC_CLOSED;
140 		sock_put(sk); /* passive closing */
141 		break;
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 */
147 		break;
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 */
157 		break;
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;
165 		break;
166 	case SMC_INIT:
167 	case SMC_PEERABORTWAIT:
168 	case SMC_CLOSED:
169 		break;
170 	}
171 
172 	sock_set_flag(sk, SOCK_DEAD);
173 	sk->sk_state_change(sk);
174 
175 	if (release_clcsock) {
176 		release_sock(sk);
177 		smc_clcsock_release(smc);
178 		lock_sock(sk);
179 	}
180 }
181 
182 static 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 
188 int 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;
192 	struct smc_connection *conn = &smc->conn;
193 	struct sock *sk = &smc->sk;
194 	int old_state;
195 	long timeout;
196 	int rc = 0;
197 
198 	timeout = current->flags & PF_EXITING ?
199 		  0 : sock_flag(sk, SOCK_LINGER) ?
200 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
201 
202 	old_state = sk->sk_state;
203 again:
204 	switch (sk->sk_state) {
205 	case SMC_INIT:
206 		sk->sk_state = SMC_CLOSED;
207 		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 		}
216 		smc_close_cleanup_listen(sk);
217 		release_sock(sk);
218 		flush_work(&smc->tcp_listen_work);
219 		lock_sock(sk);
220 		break;
221 	case SMC_ACTIVE:
222 		smc_close_stream_wait(smc, timeout);
223 		release_sock(sk);
224 		cancel_delayed_work_sync(&conn->tx_work);
225 		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 &&
238 		    !smc_close_sent_any_close(conn)) {
239 			/* just shutdown wr done, send close request */
240 			rc = smc_close_final(conn);
241 		}
242 		sk->sk_state = SMC_CLOSED;
243 		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);
249 		cancel_delayed_work_sync(&conn->tx_work);
250 		lock_sock(sk);
251 		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);
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 */
260 		} else {
261 			/* peer has just issued a shutdown write */
262 			sk->sk_state = SMC_PEERFINCLOSEWAIT;
263 		}
264 		break;
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);
271 		}
272 		/* peer sending PeerConnectionClosed will cause transition */
273 		break;
274 	case SMC_PEERFINCLOSEWAIT:
275 		/* peer sending PeerConnectionClosed will cause transition */
276 		break;
277 	case SMC_PROCESSABORT:
278 		rc = smc_close_abort(conn);
279 		sk->sk_state = SMC_CLOSED;
280 		break;
281 	case SMC_PEERABORTWAIT:
282 		sk->sk_state = SMC_CLOSED;
283 		break;
284 	case SMC_CLOSED:
285 		/* nothing to do, add tracing in future patch */
286 		break;
287 	}
288 
289 	if (old_state != sk->sk_state)
290 		sk->sk_state_change(sk);
291 	return rc;
292 }
293 
294 static 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) {
301 	case SMC_INIT:
302 	case SMC_ACTIVE:
303 	case SMC_APPCLOSEWAIT1:
304 		sk->sk_state = SMC_PROCESSABORT;
305 		sock_put(sk); /* passive closing */
306 		break;
307 	case SMC_APPFINCLOSEWAIT:
308 		sk->sk_state = SMC_PROCESSABORT;
309 		break;
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;
316 		else
317 			sk->sk_state = SMC_CLOSED;
318 		sock_put(sk); /* passive closing */
319 		break;
320 	case SMC_APPCLOSEWAIT2:
321 	case SMC_PEERFINCLOSEWAIT:
322 		sk->sk_state = SMC_CLOSED;
323 		sock_put(sk); /* passive closing */
324 		break;
325 	case SMC_PEERABORTWAIT:
326 		sk->sk_state = SMC_CLOSED;
327 		break;
328 	case SMC_PROCESSABORT:
329 	/* nothing to do, add tracing in future patch */
330 		break;
331 	}
332 }
333 
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.
337  */
338 static void smc_close_passive_work(struct work_struct *work)
339 {
340 	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;
345 	bool release_clcsock = false;
346 	struct sock *sk = &smc->sk;
347 	int old_state;
348 
349 	lock_sock(sk);
350 	old_state = sk->sk_state;
351 
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);
358 		lock_sock(&smc->sk);
359 		goto wakeup;
360 	}
361 
362 	switch (sk->sk_state) {
363 	case SMC_INIT:
364 		sk->sk_state = SMC_APPCLOSEWAIT1;
365 		break;
366 	case SMC_ACTIVE:
367 		sk->sk_state = SMC_APPCLOSEWAIT1;
368 		/* postpone sock_put() for passive closing to cover
369 		 * received SEND_SHUTDOWN as well
370 		 */
371 		break;
372 	case SMC_PEERCLOSEWAIT1:
373 		if (rxflags->peer_done_writing)
374 			sk->sk_state = SMC_PEERCLOSEWAIT2;
375 		/* fall through */
376 		/* to check for closing */
377 	case SMC_PEERCLOSEWAIT2:
378 		if (!smc_cdc_rxed_any_close(conn))
379 			break;
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;
384 		} else {
385 			/* just shutdown, but not yet closed locally */
386 			sk->sk_state = SMC_APPFINCLOSEWAIT;
387 		}
388 		sock_put(sk); /* passive closing */
389 		break;
390 	case SMC_PEERFINCLOSEWAIT:
391 		if (smc_cdc_rxed_any_close(conn)) {
392 			sk->sk_state = SMC_CLOSED;
393 			sock_put(sk); /* passive closing */
394 		}
395 		break;
396 	case SMC_APPCLOSEWAIT1:
397 	case SMC_APPCLOSEWAIT2:
398 		/* postpone sock_put() for passive closing to cover
399 		 * received SEND_SHUTDOWN as well
400 		 */
401 		break;
402 	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 
410 wakeup:
411 	sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
412 	sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
413 
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)) {
418 			smc_conn_free(conn);
419 			if (smc->clcsock)
420 				release_clcsock = true;
421 		}
422 	}
423 	release_sock(sk);
424 	if (release_clcsock)
425 		smc_clcsock_release(smc);
426 	sock_put(sk); /* sock_hold done by schedulers of close_work */
427 }
428 
429 int smc_close_shutdown_write(struct smc_sock *smc)
430 {
431 	struct smc_connection *conn = &smc->conn;
432 	struct sock *sk = &smc->sk;
433 	int old_state;
434 	long timeout;
435 	int rc = 0;
436 
437 	timeout = current->flags & PF_EXITING ?
438 		  0 : sock_flag(sk, SOCK_LINGER) ?
439 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
440 
441 	old_state = sk->sk_state;
442 again:
443 	switch (sk->sk_state) {
444 	case SMC_ACTIVE:
445 		smc_close_stream_wait(smc, timeout);
446 		release_sock(sk);
447 		cancel_delayed_work_sync(&conn->tx_work);
448 		lock_sock(sk);
449 		if (sk->sk_state != SMC_ACTIVE)
450 			goto again;
451 		/* send close wr request */
452 		rc = smc_close_wr(conn);
453 		sk->sk_state = SMC_PEERCLOSEWAIT1;
454 		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);
460 		cancel_delayed_work_sync(&conn->tx_work);
461 		lock_sock(sk);
462 		if (sk->sk_state != SMC_APPCLOSEWAIT1)
463 			goto again;
464 		/* 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)
480 		sk->sk_state_change(sk);
481 	return rc;
482 }
483 
484 /* Initialize close properties on connection establishment. */
485 void smc_close_init(struct smc_sock *smc)
486 {
487 	INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
488 }
489